-
Notifications
You must be signed in to change notification settings - Fork 29
/
parser.cpp
6416 lines (5753 loc) · 220 KB
/
parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <math.h>
#include "intrin.h"
#include "fold.h"
#include "ast.h"
#include "parser.h"
#define PARSER_HT_LOCALS 2
#define PARSER_HT_SIZE 512
#define TYPEDEF_HT_SIZE 512
static void parser_enterblock(parser_t *parser);
static bool parser_leaveblock(parser_t *parser);
static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
static void parser_addlocal(parser_t *parser, const std::string &name, ast_expression *e);
static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e);
static void parser_addglobal(parser_t *parser, const std::string &name, ast_expression *e);
static bool parse_typedef(parser_t *parser);
static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef, bool noref, bool is_static, uint32_t qflags, char *vstring);
static bool parse_block_into(parser_t *parser, ast_block *block);
static bool parse_statement_or_block(parser_t *parser, ast_expression **out);
static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels);
static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool with_labels);
static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname);
static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef, bool *is_vararg);
static void parseerror_(parser_t *parser, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vcompile_error(parser->lex->tok.ctx, fmt, ap);
va_end(ap);
}
template<typename... Ts>
static inline void parseerror(parser_t *parser, const char *fmt, const Ts&... ts) {
return parseerror_(parser, fmt, formatNormalize(ts)...);
}
// returns true if it counts as an error
static bool GMQCC_WARN parsewarning_(parser_t *parser, int warntype, const char *fmt, ...)
{
bool r;
va_list ap;
va_start(ap, fmt);
r = vcompile_warning(parser->lex->tok.ctx, warntype, fmt, ap);
va_end(ap);
return r;
}
template<typename... Ts>
static inline bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, const Ts&... ts) {
return parsewarning_(parser, warntype, fmt, formatNormalize(ts)...);
}
/**********************************************************************
* parsing
*/
static bool parser_next(parser_t *parser)
{
/* lex_do kills the previous token */
parser->tok = lex_do(parser->lex);
if (parser->tok == TOKEN_EOF)
return true;
if (parser->tok >= TOKEN_ERROR) {
parseerror(parser, "lex error");
return false;
}
return true;
}
#define parser_tokval(p) ((p)->lex->tok.value)
#define parser_token(p) (&((p)->lex->tok))
char *parser_strdup(const char *str)
{
if (str && !*str) {
/* actually dup empty strings */
char *out = (char*)mem_a(1);
*out = 0;
return out;
}
return util_strdup(str);
}
static ast_expression* parser_find_field(parser_t *parser, const char *name) {
return (ast_expression*)util_htget(parser->htfields, name);
}
static ast_expression* parser_find_field(parser_t *parser, const std::string &name) {
return parser_find_field(parser, name.c_str());
}
static ast_expression* parser_find_label(parser_t *parser, const char *name)
{
for (auto &it : parser->labels)
if (it->m_name == name)
return it;
return nullptr;
}
static inline ast_expression* parser_find_label(parser_t *parser, const std::string &name) {
return parser_find_label(parser, name.c_str());
}
ast_expression* parser_find_global(parser_t *parser, const char *name)
{
ast_expression *var = (ast_expression*)util_htget(parser->aliases, parser_tokval(parser));
if (var)
return var;
return (ast_expression*)util_htget(parser->htglobals, name);
}
ast_expression* parser_find_global(parser_t *parser, const std::string &name) {
return parser_find_global(parser, name.c_str());
}
static ast_expression* parser_find_param(parser_t *parser, const char *name)
{
ast_value *fun;
if (!parser->function)
return nullptr;
fun = parser->function->m_function_type;
for (auto &it : fun->m_type_params) {
if (it->m_name == name)
return it.get();
}
return nullptr;
}
static ast_expression* parser_find_local(parser_t *parser, const char *name, size_t upto, bool *isparam)
{
size_t i, hash;
ast_expression *e;
hash = util_hthash(parser->htglobals, name);
*isparam = false;
for (i = parser->variables.size(); i > upto;) {
--i;
if ( (e = (ast_expression*)util_htgeth(parser->variables[i], name, hash)) )
return e;
}
*isparam = true;
return parser_find_param(parser, name);
}
static ast_expression* parser_find_local(parser_t *parser, const std::string &name, size_t upto, bool *isparam) {
return parser_find_local(parser, name.c_str(), upto, isparam);
}
static ast_expression* parser_find_var(parser_t *parser, const char *name)
{
bool dummy;
ast_expression *v;
v = parser_find_local(parser, name, PARSER_HT_LOCALS, &dummy);
if (!v) v = parser_find_global(parser, name);
return v;
}
static inline ast_expression* parser_find_var(parser_t *parser, const std::string &name) {
return parser_find_var(parser, name.c_str());
}
static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t upto)
{
size_t i, hash;
ast_value *e;
hash = util_hthash(parser->typedefs[0], name);
for (i = parser->typedefs.size(); i > upto;) {
--i;
if ( (e = (ast_value*)util_htgeth(parser->typedefs[i], name, hash)) )
return e;
}
return nullptr;
}
static ast_value* parser_find_typedef(parser_t *parser, const std::string &name, size_t upto) {
return parser_find_typedef(parser, name.c_str(), upto);
}
struct sy_elem {
size_t etype; /* 0 = expression, others are operators */
bool isparen;
size_t off;
ast_expression *out;
ast_block *block; /* for commas and function calls */
lex_ctx_t ctx;
};
enum {
PAREN_EXPR,
PAREN_FUNC,
PAREN_INDEX,
PAREN_TERNARY1,
PAREN_TERNARY2
};
struct shunt {
std::vector<sy_elem> out;
std::vector<sy_elem> ops;
std::vector<size_t> argc;
std::vector<unsigned int> paren;
};
static sy_elem syexp(lex_ctx_t ctx, ast_expression *v) {
sy_elem e;
e.etype = 0;
e.off = 0;
e.out = v;
e.block = nullptr;
e.ctx = ctx;
e.isparen = false;
return e;
}
static sy_elem syblock(lex_ctx_t ctx, ast_block *v) {
sy_elem e;
e.etype = 0;
e.off = 0;
e.out = v;
e.block = v;
e.ctx = ctx;
e.isparen = false;
return e;
}
static sy_elem syop(lex_ctx_t ctx, const oper_info *op) {
sy_elem e;
e.etype = 1 + (op - operators);
e.off = 0;
e.out = nullptr;
e.block = nullptr;
e.ctx = ctx;
e.isparen = false;
return e;
}
static sy_elem syparen(lex_ctx_t ctx, size_t off) {
sy_elem e;
e.etype = 0;
e.off = off;
e.out = nullptr;
e.block = nullptr;
e.ctx = ctx;
e.isparen = true;
return e;
}
/* With regular precedence rules, ent.foo[n] is the same as (ent.foo)[n],
* so we need to rotate it to become ent.(foo[n]).
*/
static bool rotate_entfield_array_index_nodes(ast_expression **out)
{
ast_array_index *index, *oldindex;
ast_entfield *entfield;
ast_value *field;
ast_expression *sub;
ast_expression *entity;
lex_ctx_t ctx = (*out)->m_context;
if (!ast_istype(*out, ast_array_index))
return false;
index = (ast_array_index*)*out;
if (!ast_istype(index->m_array, ast_entfield))
return false;
entfield = (ast_entfield*)index->m_array;
if (!ast_istype(entfield->m_field, ast_value))
return false;
field = (ast_value*)entfield->m_field;
sub = index->m_index;
entity = entfield->m_entity;
oldindex = index;
index = ast_array_index::make(ctx, field, sub);
entfield = new ast_entfield(ctx, entity, index);
*out = entfield;
oldindex->m_array = nullptr;
oldindex->m_index = nullptr;
delete oldindex;
return true;
}
static int store_op_for(ast_expression* expr)
{
if (OPTS_FLAG(ADJUST_VECTOR_FIELDS) && expr->m_vtype == TYPE_FIELD && expr->m_next->m_vtype == TYPE_VECTOR) {
if (ast_istype(expr, ast_entfield)) {
return type_storep_instr[TYPE_VECTOR];
} else {
return type_store_instr[TYPE_VECTOR];
}
}
if (ast_istype(expr, ast_member) && ast_istype(((ast_member*)expr)->m_owner, ast_entfield)) {
return type_storep_instr[expr->m_vtype];
}
if (ast_istype(expr, ast_entfield)) {
return type_storep_instr[expr->m_vtype];
}
return type_store_instr[expr->m_vtype];
}
static bool check_write_to(lex_ctx_t ctx, ast_expression *expr)
{
if (ast_istype(expr, ast_value)) {
ast_value *val = (ast_value*)expr;
if (val->m_cvq == CV_CONST) {
if (val->m_name[0] == '#') {
compile_error(ctx, "invalid assignment to a literal constant");
return false;
}
/*
* To work around quakeworld we must elide the error and make it
* a warning instead.
*/
if (OPTS_OPTION_U32(OPTION_STANDARD) != COMPILER_QCC)
compile_error(ctx, "assignment to constant `%s`", val->m_name);
else
(void)!compile_warning(ctx, WARN_CONST_OVERWRITE, "assignment to constant `%s`", val->m_name);
return false;
}
}
return true;
}
static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
{
const oper_info *op;
lex_ctx_t ctx;
ast_expression *out = nullptr;
ast_expression *exprs[3] = { 0, 0, 0 };
ast_block *blocks[3];
ast_binstore *asbinstore;
size_t i, assignop, addop, subop;
qcint_t generated_op = 0;
char ty1[1024];
char ty2[1024];
if (sy->ops.empty()) {
parseerror(parser, "internal error: missing operator");
return false;
}
if (sy->ops.back().isparen) {
parseerror(parser, "unmatched parenthesis");
return false;
}
op = &operators[sy->ops.back().etype - 1];
ctx = sy->ops.back().ctx;
if (sy->out.size() < op->operands) {
if (op->flags & OP_PREFIX)
compile_error(ctx, "expected expression after unary operator `%s`", op->op, (int)op->id);
else /* this should have errored previously already */
compile_error(ctx, "expected expression after operator `%s`", op->op, (int)op->id);
return false;
}
sy->ops.pop_back();
/* op(:?) has no input and no output */
if (!op->operands)
return true;
sy->out.erase(sy->out.end() - op->operands, sy->out.end());
for (i = 0; i < op->operands; ++i) {
exprs[i] = sy->out[sy->out.size()+i].out;
blocks[i] = sy->out[sy->out.size()+i].block;
if (exprs[i]->m_vtype == TYPE_NOEXPR &&
!(i != 0 && op->id == opid2('?',':')) &&
!(i == 1 && op->id == opid1('.')))
{
if (ast_istype(exprs[i], ast_label))
compile_error(exprs[i]->m_context, "expected expression, got an unknown identifier");
else
compile_error(exprs[i]->m_context, "not an expression");
(void)!compile_warning(exprs[i]->m_context, WARN_DEBUG, "expression %u\n", (unsigned int)i);
}
}
if (blocks[0] && blocks[0]->m_exprs.empty() && op->id != opid1(',')) {
compile_error(ctx, "internal error: operator cannot be applied on empty blocks");
return false;
}
#define NotSameType(T) \
(exprs[0]->m_vtype != exprs[1]->m_vtype || \
exprs[0]->m_vtype != T)
switch (op->id)
{
default:
compile_error(ctx, "internal error: unhandled operator: %s (%i)", op->op, (int)op->id);
return false;
case opid1('.'):
if (exprs[0]->m_vtype == TYPE_VECTOR &&
exprs[1]->m_vtype == TYPE_NOEXPR)
{
if (exprs[1] == parser->const_vec[0])
out = ast_member::make(ctx, exprs[0], 0, "");
else if (exprs[1] == parser->const_vec[1])
out = ast_member::make(ctx, exprs[0], 1, "");
else if (exprs[1] == parser->const_vec[2])
out = ast_member::make(ctx, exprs[0], 2, "");
else {
compile_error(ctx, "access to invalid vector component");
return false;
}
}
else if (exprs[0]->m_vtype == TYPE_ENTITY) {
if (exprs[1]->m_vtype != TYPE_FIELD) {
compile_error(exprs[1]->m_context, "type error: right hand of member-operand should be an entity-field");
return false;
}
out = new ast_entfield(ctx, exprs[0], exprs[1]);
}
else if (exprs[0]->m_vtype == TYPE_VECTOR) {
compile_error(exprs[1]->m_context, "vectors cannot be accessed this way");
return false;
}
else {
compile_error(exprs[1]->m_context, "type error: member-of operator on something that is not an entity or vector");
return false;
}
break;
case opid1('['):
if (exprs[0]->m_vtype != TYPE_ARRAY &&
!(exprs[0]->m_vtype == TYPE_FIELD &&
exprs[0]->m_next->m_vtype == TYPE_ARRAY))
{
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
compile_error(exprs[0]->m_context, "cannot index value of type %s", ty1);
return false;
}
if (exprs[1]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
compile_error(exprs[1]->m_context, "index must be of type float, not %s", ty1);
return false;
}
out = ast_array_index::make(ctx, exprs[0], exprs[1]);
rotate_entfield_array_index_nodes(&out);
break;
case opid1(','):
if (sy->paren.size() && sy->paren.back() == PAREN_FUNC) {
sy->out.push_back(syexp(ctx, exprs[0]));
sy->out.push_back(syexp(ctx, exprs[1]));
sy->argc.back()++;
return true;
}
if (blocks[0]) {
if (!blocks[0]->addExpr(exprs[1]))
return false;
} else {
blocks[0] = new ast_block(ctx);
if (!blocks[0]->addExpr(exprs[0]) ||
!blocks[0]->addExpr(exprs[1]))
{
return false;
}
}
blocks[0]->setType(*exprs[1]);
sy->out.push_back(syblock(ctx, blocks[0]));
return true;
case opid2('+','P'):
out = exprs[0];
break;
case opid2('-','P'):
if ((out = parser->m_fold.op(op, exprs)))
break;
if (exprs[0]->m_vtype != TYPE_FLOAT &&
exprs[0]->m_vtype != TYPE_VECTOR) {
compile_error(ctx, "invalid types used in unary expression: cannot negate type %s",
type_name[exprs[0]->m_vtype]);
return false;
}
if (exprs[0]->m_vtype == TYPE_FLOAT)
out = ast_unary::make(ctx, VINSTR_NEG_F, exprs[0]);
else
out = ast_unary::make(ctx, VINSTR_NEG_V, exprs[0]);
break;
case opid2('!','P'):
if (!(out = parser->m_fold.op(op, exprs))) {
switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
break;
case TYPE_VECTOR:
out = ast_unary::make(ctx, INSTR_NOT_V, exprs[0]);
break;
case TYPE_STRING:
if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
out = ast_unary::make(ctx, INSTR_NOT_F, exprs[0]);
else
out = ast_unary::make(ctx, INSTR_NOT_S, exprs[0]);
break;
/* we don't constant-fold NOT for these types */
case TYPE_ENTITY:
out = ast_unary::make(ctx, INSTR_NOT_ENT, exprs[0]);
break;
case TYPE_FUNCTION:
out = ast_unary::make(ctx, INSTR_NOT_FNC, exprs[0]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot logically negate type %s",
type_name[exprs[0]->m_vtype]);
return false;
}
}
break;
case opid1('+'):
if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
(exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT) )
{
compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
out = fold::binary(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
out = fold::binary(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot add type %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
}
break;
case opid1('-'):
if (exprs[0]->m_vtype != exprs[1]->m_vtype ||
(exprs[0]->m_vtype != TYPE_VECTOR && exprs[0]->m_vtype != TYPE_FLOAT))
{
compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
type_name[exprs[1]->m_vtype],
type_name[exprs[0]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
out = fold::binary(ctx, INSTR_SUB_F, exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
out = fold::binary(ctx, INSTR_SUB_V, exprs[0], exprs[1]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot subtract type %s from %s",
type_name[exprs[1]->m_vtype],
type_name[exprs[0]->m_vtype]);
return false;
}
}
break;
case opid1('*'):
if (exprs[0]->m_vtype != exprs[1]->m_vtype &&
!(exprs[0]->m_vtype == TYPE_VECTOR &&
exprs[1]->m_vtype == TYPE_FLOAT) &&
!(exprs[1]->m_vtype == TYPE_VECTOR &&
exprs[0]->m_vtype == TYPE_FLOAT)
)
{
compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
type_name[exprs[1]->m_vtype],
type_name[exprs[0]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
switch (exprs[0]->m_vtype) {
case TYPE_FLOAT:
if (exprs[1]->m_vtype == TYPE_VECTOR)
out = fold::binary(ctx, INSTR_MUL_FV, exprs[0], exprs[1]);
else
out = fold::binary(ctx, INSTR_MUL_F, exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
if (exprs[1]->m_vtype == TYPE_FLOAT)
out = fold::binary(ctx, INSTR_MUL_VF, exprs[0], exprs[1]);
else
out = fold::binary(ctx, INSTR_MUL_V, exprs[0], exprs[1]);
break;
default:
compile_error(ctx, "invalid types used in expression: cannot multiply types %s and %s",
type_name[exprs[1]->m_vtype],
type_name[exprs[0]->m_vtype]);
return false;
}
}
break;
case opid1('/'):
if (exprs[1]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
if (exprs[0]->m_vtype == TYPE_FLOAT)
out = fold::binary(ctx, INSTR_DIV_F, exprs[0], exprs[1]);
else {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types used in expression: cannot divide types %s and %s", ty1, ty2);
return false;
}
}
break;
case opid1('%'):
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
} else if (!(out = parser->m_fold.op(op, exprs))) {
/* generate a call to __builtin_mod */
ast_expression *mod = parser->m_intrin.func("mod");
ast_call *call = nullptr;
if (!mod) return false; /* can return null for missing floor */
call = ast_call::make(parser_ctx(parser), mod);
call->m_params.push_back(exprs[0]);
call->m_params.push_back(exprs[1]);
out = call;
}
break;
case opid2('%','='):
compile_error(ctx, "%= is unimplemented");
return false;
case opid1('|'):
case opid1('&'):
case opid1('^'):
if ( !(exprs[0]->m_vtype == TYPE_FLOAT && exprs[1]->m_vtype == TYPE_FLOAT) &&
!(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_FLOAT) &&
!(exprs[0]->m_vtype == TYPE_VECTOR && exprs[1]->m_vtype == TYPE_VECTOR))
{
compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
/*
* IF the first expression is float, the following will be too
* since scalar ^ vector is not allowed.
*/
if (exprs[0]->m_vtype == TYPE_FLOAT) {
out = fold::binary(ctx,
(op->id == opid1('^') ? VINSTR_BITXOR : op->id == opid1('|') ? INSTR_BITOR : INSTR_BITAND),
exprs[0], exprs[1]);
} else {
/*
* The first is a vector: vector is allowed to bitop with vector and
* with scalar, branch here for the second operand.
*/
if (exprs[1]->m_vtype == TYPE_VECTOR) {
/*
* Bitop all the values of the vector components against the
* vectors components in question.
*/
out = fold::binary(ctx,
(op->id == opid1('^') ? VINSTR_BITXOR_V : op->id == opid1('|') ? VINSTR_BITOR_V : VINSTR_BITAND_V),
exprs[0], exprs[1]);
} else {
out = fold::binary(ctx,
(op->id == opid1('^') ? VINSTR_BITXOR_VF : op->id == opid1('|') ? VINSTR_BITOR_VF : VINSTR_BITAND_VF),
exprs[0], exprs[1]);
}
}
}
break;
case opid2('<','<'):
case opid2('>','>'):
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform shift between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
ast_expression *shift = parser->m_intrin.func((op->id == opid2('<','<')) ? "__builtin_lshift" : "__builtin_rshift");
ast_call *call = ast_call::make(parser_ctx(parser), shift);
call->m_params.push_back(exprs[0]);
call->m_params.push_back(exprs[1]);
out = call;
}
break;
case opid3('<','<','='):
case opid3('>','>','='):
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform shift operation between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if(!(out = parser->m_fold.op(op, exprs))) {
ast_expression *shift = parser->m_intrin.func((op->id == opid3('<','<','=')) ? "__builtin_lshift" : "__builtin_rshift");
ast_call *call = ast_call::make(parser_ctx(parser), shift);
call->m_params.push_back(exprs[0]);
call->m_params.push_back(exprs[1]);
out = new ast_store(
parser_ctx(parser),
INSTR_STORE_F,
exprs[0],
call
);
}
break;
case opid2('|','|'):
generated_op += 1; /* INSTR_OR */
[[fallthrough]];
case opid2('&','&'):
generated_op += INSTR_AND;
if (!(out = parser->m_fold.op(op, exprs))) {
if (OPTS_FLAG(PERL_LOGIC) && !exprs[0]->compareType(*exprs[1])) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types for logical operation with -fperl-logic: %s and %s", ty1, ty2);
return false;
}
for (i = 0; i < 2; ++i) {
if (OPTS_FLAG(CORRECT_LOGIC) && exprs[i]->m_vtype == TYPE_VECTOR) {
out = ast_unary::make(ctx, INSTR_NOT_V, exprs[i]);
if (!out) break;
out = ast_unary::make(ctx, INSTR_NOT_F, out);
if (!out) break;
exprs[i] = out; out = nullptr;
if (OPTS_FLAG(PERL_LOGIC)) {
/* here we want to keep the right expressions' type */
break;
}
}
else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && exprs[i]->m_vtype == TYPE_STRING) {
out = ast_unary::make(ctx, INSTR_NOT_S, exprs[i]);
if (!out) break;
out = ast_unary::make(ctx, INSTR_NOT_F, out);
if (!out) break;
exprs[i] = out; out = nullptr;
if (OPTS_FLAG(PERL_LOGIC)) {
/* here we want to keep the right expressions' type */
break;
}
}
}
out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
}
break;
case opid2('?',':'):
if (sy->paren.back() != PAREN_TERNARY2) {
compile_error(ctx, "mismatched parenthesis/ternary");
return false;
}
sy->paren.pop_back();
if (!exprs[1]->compareType(*exprs[2])) {
ast_type_to_string(exprs[1], ty1, sizeof(ty1));
ast_type_to_string(exprs[2], ty2, sizeof(ty2));
compile_error(ctx, "operands of ternary expression must have the same type, got %s and %s", ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
out = new ast_ternary(ctx, exprs[0], exprs[1], exprs[2]);
break;
case opid2('*', '*'):
if (NotSameType(TYPE_FLOAT)) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types used in exponentiation: %s and %s",
ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
ast_call *gencall = ast_call::make(parser_ctx(parser), parser->m_intrin.func("pow"));
gencall->m_params.push_back(exprs[0]);
gencall->m_params.push_back(exprs[1]);
out = gencall;
}
break;
case opid2('>', '<'):
if (NotSameType(TYPE_VECTOR)) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types used in cross product: %s and %s",
ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
out = fold::binary(
parser_ctx(parser),
VINSTR_CROSS,
exprs[0],
exprs[1]
);
}
break;
case opid3('<','=','>'): /* -1, 0, or 1 */
if (NotSameType(TYPE_FLOAT)) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types used in comparision: %s and %s",
ty1, ty2);
return false;
}
if (!(out = parser->m_fold.op(op, exprs))) {
/* This whole block is NOT fold_binary safe */
ast_binary *eq = new ast_binary(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
eq->m_refs = AST_REF_NONE;
/* if (lt) { */
out = new ast_ternary(ctx,
new ast_binary(ctx, INSTR_LT, exprs[0], exprs[1]),
/* out = -1 */
parser->m_fold.imm_float(2),
/* } else { */
/* if (eq) { */
new ast_ternary(ctx, eq,
/* out = 0 */
parser->m_fold.imm_float(0),
/* } else { */
/* out = 1 */
parser->m_fold.imm_float(1)
/* } */
)
/* } */
);
}
break;
case opid1('>'):
generated_op += 1; /* INSTR_GT */
[[fallthrough]];
case opid1('<'):
generated_op += 1; /* INSTR_LT */
[[fallthrough]];
case opid2('>', '='):
generated_op += 1; /* INSTR_GE */
[[fallthrough]];
case opid2('<', '='):
generated_op += INSTR_LE;
if (NotSameType(TYPE_FLOAT)) {
compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
out = fold::binary(ctx, generated_op, exprs[0], exprs[1]);
break;
case opid2('!', '='):
#define NotComparable \
exprs[0]->m_vtype != TYPE_NIL && \
exprs[1]->m_vtype != TYPE_NIL && \
exprs[0]->m_vtype != exprs[1]->m_vtype
if (NotComparable) {
compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
out = fold::binary(ctx, type_ne_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
break;
case opid2('=', '='):
if (NotComparable) {
compile_error(ctx, "invalid types used in expression: cannot perform comparison between types %s and %s",
type_name[exprs[0]->m_vtype],
type_name[exprs[1]->m_vtype]);
return false;
}
if (!(out = parser->m_fold.op(op, exprs)))
out = fold::binary(ctx, type_eq_instr[exprs[0]->m_vtype], exprs[0], exprs[1]);
break;
case opid1('='):
if (ast_istype(exprs[0], ast_entfield)) {
ast_expression *field = ((ast_entfield*)exprs[0])->m_field;
assignop = store_op_for(exprs[0]);
if (assignop == VINSTR_END || !field->m_next->compareType(*exprs[1]))
{
ast_type_to_string(field->m_next, ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
field->m_next->m_vtype == TYPE_FUNCTION &&
exprs[1]->m_vtype == TYPE_FUNCTION)
{
(void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
"invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
else
compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
}
else
{
assignop = store_op_for(exprs[0]);
if (assignop == VINSTR_END) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
else if (!exprs[0]->compareType(*exprs[1]))
{
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
ast_type_to_string(exprs[1], ty2, sizeof(ty2));
if (OPTS_FLAG(ASSIGN_FUNCTION_TYPES) &&
exprs[0]->m_vtype == TYPE_FUNCTION &&
exprs[1]->m_vtype == TYPE_FUNCTION)
{
(void)!compile_warning(ctx, WARN_ASSIGN_FUNCTION_TYPES,
"invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
else
compile_error(ctx, "invalid types in assignment: cannot assign %s to %s", ty2, ty1);
}
}
(void)check_write_to(ctx, exprs[0]);
/* When we're a vector of part of an entity field we use STOREP */
if (ast_istype(exprs[0], ast_member) && ast_istype(((ast_member*)exprs[0])->m_owner, ast_entfield))
assignop = INSTR_STOREP_F;
out = new ast_store(ctx, assignop, exprs[0], exprs[1]);
break;
case opid3('+','+','P'):
case opid3('-','-','P'):
/* prefix ++ */
if (exprs[0]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
compile_error(exprs[0]->m_context, "invalid type for prefix increment: %s", ty1);
return false;
}
if (op->id == opid3('+','+','P'))
addop = INSTR_ADD_F;
else
addop = INSTR_SUB_F;
(void)check_write_to(exprs[0]->m_context, exprs[0]);
if (ast_istype(exprs[0], ast_entfield)) {
out = new ast_binstore(ctx, INSTR_STOREP_F, addop,
exprs[0],
parser->m_fold.imm_float(1));
} else {
out = new ast_binstore(ctx, INSTR_STORE_F, addop,
exprs[0],
parser->m_fold.imm_float(1));
}
break;
case opid3('S','+','+'):
case opid3('S','-','-'):
/* prefix ++ */
if (exprs[0]->m_vtype != TYPE_FLOAT) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));