-
Notifications
You must be signed in to change notification settings - Fork 29
/
ast.cpp
3125 lines (2696 loc) · 91.5 KB
/
ast.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 <new>
#include <stdlib.h>
#include <string.h>
#include "gmqcc.h"
#include "ast.h"
#include "fold.h"
//#include "parser.h"
#include "algo.h"
/* Initialize main ast node aprts */
ast_node::ast_node(lex_ctx_t ctx, int node_type)
: m_context(ctx)
, m_node_type(node_type)
, m_keep_node(false)
, m_side_effects(false)
{
}
ast_node::~ast_node()
{
}
/* weight and side effects */
void ast_node::propagateSideEffects(const ast_node *other)
{
if (other->m_side_effects)
m_side_effects = true;
}
/* General expression initialization */
ast_expression::ast_expression(lex_ctx_t ctx, int nodetype, qc_type type)
: ast_node(ctx, nodetype)
, m_vtype(type)
{
if (OPTS_OPTION_BOOL(OPTION_COVERAGE))
m_flags |= AST_FLAG_BLOCK_COVERAGE;
if (OPTS_FLAG(DEFAULT_ERASEABLE))
m_flags |= AST_FLAG_ERASEABLE;
}
ast_expression::ast_expression(lex_ctx_t ctx, int nodetype)
: ast_expression(ctx, nodetype, TYPE_VOID)
{}
ast_expression::~ast_expression()
{
if (m_next)
delete m_next;
if (m_varparam)
delete m_varparam;
}
ast_expression::ast_expression(ast_copy_type_t, const ast_expression &other)
: ast_expression(ast_copy_type, other.m_context, other)
{}
ast_expression::ast_expression(ast_copy_type_t, lex_ctx_t ctx, const ast_expression &other)
: ast_expression(ast_copy_type, TYPE_ast_expression, ctx, other)
{}
ast_expression::ast_expression(ast_copy_type_t, int nodetype, const ast_expression &other)
: ast_expression(ast_copy_type, nodetype, other.m_context, other)
{}
ast_expression::ast_expression(ast_copy_type_t, int nodetype, lex_ctx_t ctx, const ast_expression &other)
: ast_expression(ctx, nodetype)
{
m_vtype = other.m_vtype;
m_count = other.m_count;
m_flags = other.m_flags;
if (other.m_next)
m_next = new ast_expression(ast_copy_type, *other.m_next);
m_type_params.reserve(other.m_type_params.size());
for (auto &it : other.m_type_params)
m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
}
ast_expression *ast_expression::shallowType(lex_ctx_t ctx, qc_type vtype) {
auto expr = new ast_expression(ctx, TYPE_ast_expression);
expr->m_vtype = vtype;
return expr;
}
void ast_expression::adoptType(const ast_expression &other)
{
m_vtype = other.m_vtype;
if (other.m_next)
m_next = new ast_expression(ast_copy_type, *other.m_next);
m_count = other.m_count;
m_flags = other.m_flags;
m_type_params.clear();
m_type_params.reserve(other.m_type_params.size());
for (auto &it : other.m_type_params)
m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
}
bool ast_expression::compareType(const ast_expression &other) const
{
if (m_vtype == TYPE_NIL ||
other.m_vtype == TYPE_NIL)
return true;
if (m_vtype != other.m_vtype)
return false;
if (!m_next != !other.m_next)
return false;
if (m_type_params.size() != other.m_type_params.size())
return false;
if ((m_flags & AST_FLAG_TYPE_MASK) !=
(other.m_flags & AST_FLAG_TYPE_MASK) )
{
return false;
}
if (m_type_params.size()) {
size_t i;
for (i = 0; i < m_type_params.size(); ++i) {
if (!m_type_params[i]->compareType(*other.m_type_params[i]))
return false;
}
}
if (m_next)
return m_next->compareType(*other.m_next);
return true;
}
bool ast_expression::codegen(ast_function*, bool, ir_value**) {
compile_error(m_context, "ast_expression::codegen called!");
abort();
return false;
}
ast_value::ast_value(ast_copy_type_t, const ast_value &other, const std::string &name)
: ast_value(ast_copy_type, static_cast<const ast_expression&>(other), name)
{
m_keep_node = true; // keep values, always
memset(&m_constval, 0, sizeof(m_constval));
}
ast_value::ast_value(ast_copy_type_t, const ast_value &other)
: ast_value(ast_copy_type, static_cast<const ast_expression&>(other), other.m_name)
{
m_keep_node = true; // keep values, always
memset(&m_constval, 0, sizeof(m_constval));
}
ast_value::ast_value(ast_copy_type_t, const ast_expression &other, const std::string &name)
: ast_expression(ast_copy_type, TYPE_ast_value, other)
, m_name(name)
{
m_keep_node = true; // keep values, always
memset(&m_constval, 0, sizeof(m_constval));
}
ast_value::ast_value(lex_ctx_t ctx, const std::string &name, qc_type t)
: ast_expression(ctx, TYPE_ast_value, t)
, m_name(name)
{
m_keep_node = true; // keep values, always
memset(&m_constval, 0, sizeof(m_constval));
}
ast_value::~ast_value()
{
if (m_argcounter)
mem_d((void*)m_argcounter);
if (m_hasvalue) {
switch (m_vtype)
{
case TYPE_STRING:
mem_d((void*)m_constval.vstring);
break;
case TYPE_FUNCTION:
// unlink us from the function node
m_constval.vfunc->m_function_type = nullptr;
break;
// NOTE: delete function? currently collected in
// the parser structure
default:
break;
}
}
// initlist imples an array which implies .next in the expression exists.
if (m_initlist.size() && m_next->m_vtype == TYPE_STRING) {
for (auto &it : m_initlist)
if (it.vstring)
mem_d(it.vstring);
}
}
static size_t ast_type_to_string_impl(const ast_expression *e, char *buf, size_t bufsize, size_t pos)
{
const char *typestr;
size_t typelen;
size_t i;
if (!e) {
if (pos + 6 >= bufsize)
goto full;
util_strncpy(buf + pos, "(null)", 6);
return pos + 6;
}
if (pos + 1 >= bufsize)
goto full;
switch (e->m_vtype) {
case TYPE_VARIANT:
util_strncpy(buf + pos, "(variant)", 9);
return pos + 9;
case TYPE_FIELD:
buf[pos++] = '.';
return ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
case TYPE_POINTER:
if (pos + 3 >= bufsize)
goto full;
buf[pos++] = '*';
buf[pos++] = '(';
pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
if (pos + 1 >= bufsize)
goto full;
buf[pos++] = ')';
return pos;
case TYPE_FUNCTION:
pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
if (pos + 2 >= bufsize)
goto full;
if (e->m_type_params.empty()) {
buf[pos++] = '(';
buf[pos++] = ')';
return pos;
}
buf[pos++] = '(';
pos = ast_type_to_string_impl(e->m_type_params[0].get(), buf, bufsize, pos);
for (i = 1; i < e->m_type_params.size(); ++i) {
if (pos + 2 >= bufsize)
goto full;
buf[pos++] = ',';
buf[pos++] = ' ';
pos = ast_type_to_string_impl(e->m_type_params[i].get(), buf, bufsize, pos);
}
if (pos + 1 >= bufsize)
goto full;
buf[pos++] = ')';
return pos;
case TYPE_ARRAY:
pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
if (pos + 1 >= bufsize)
goto full;
buf[pos++] = '[';
pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->m_count);
if (pos + 1 >= bufsize)
goto full;
buf[pos++] = ']';
return pos;
default:
typestr = type_name[e->m_vtype];
typelen = strlen(typestr);
if (pos + typelen >= bufsize)
goto full;
util_strncpy(buf + pos, typestr, typelen);
return pos + typelen;
}
full:
buf[bufsize-3] = '.';
buf[bufsize-2] = '.';
buf[bufsize-1] = '.';
return bufsize;
}
void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize)
{
size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
buf[pos] = 0;
}
void ast_value::addParam(ast_value *p)
{
m_type_params.emplace_back(p);
}
ast_binary::ast_binary(lex_ctx_t ctx, int op,
ast_expression* left, ast_expression* right)
: ast_expression(ctx, TYPE_ast_binary)
, m_op(op)
// m_left/m_right happen after the peephole step right below
, m_right_first(false)
{
if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
ast_unary *unary = ((ast_unary*)right);
ast_expression *normal = unary->m_operand;
/* make a-(-b) => a + b */
if (unary->m_op == VINSTR_NEG_F || unary->m_op == VINSTR_NEG_V) {
if (op == INSTR_SUB_F) {
op = m_op = INSTR_ADD_F;
right = normal;
++opts_optimizationcount[OPTIM_PEEPHOLE];
} else if (op == INSTR_SUB_V) {
op = m_op = INSTR_ADD_V;
right = normal;
++opts_optimizationcount[OPTIM_PEEPHOLE];
}
}
}
m_left = left;
m_right = right;
propagateSideEffects(left);
propagateSideEffects(right);
if (op >= INSTR_EQ_F && op <= INSTR_GT)
m_vtype = TYPE_FLOAT;
else if (op == INSTR_AND || op == INSTR_OR) {
if (OPTS_FLAG(PERL_LOGIC))
adoptType(*right);
else
m_vtype = TYPE_FLOAT;
}
else if (op == INSTR_BITAND || op == INSTR_BITOR)
m_vtype = TYPE_FLOAT;
else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
m_vtype = TYPE_VECTOR;
else if (op == INSTR_MUL_V)
m_vtype = TYPE_FLOAT;
else
m_vtype = left->m_vtype;
// references all
m_refs = AST_REF_ALL;
}
ast_binary::~ast_binary()
{
if (m_refs & AST_REF_LEFT) ast_unref(m_left);
if (m_refs & AST_REF_RIGHT) ast_unref(m_right);
}
ast_binstore::ast_binstore(lex_ctx_t ctx, int storop, int mathop,
ast_expression* left, ast_expression* right)
: ast_expression(ctx, TYPE_ast_binstore)
, m_opstore(storop)
, m_opbin(mathop)
, m_dest(left)
, m_source(right)
, m_keep_dest(false)
{
m_side_effects = true;
adoptType(*left);
}
ast_binstore::~ast_binstore()
{
if (!m_keep_dest)
ast_unref(m_dest);
ast_unref(m_source);
}
ast_unary* ast_unary::make(lex_ctx_t ctx, int op, ast_expression *expr)
{
// handle double negation, double bitwise or logical not
if (op == opid2('!','P') ||
op == opid2('~','P') ||
op == opid2('-','P'))
{
if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
ast_unary *unary = reinterpret_cast<ast_unary*>(expr);
if (unary->m_op == op) {
auto out = reinterpret_cast<ast_unary*>(unary->m_operand);
unary->m_operand = nullptr;
delete unary;
++opts_optimizationcount[OPTIM_PEEPHOLE];
return out;
}
}
}
return new ast_unary(ctx, op, expr);
}
ast_unary::ast_unary(lex_ctx_t ctx, int op, ast_expression *expr)
: ast_expression(ctx, TYPE_ast_unary)
, m_op(op)
, m_operand(expr)
{
propagateSideEffects(expr);
if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
m_vtype = TYPE_FLOAT;
} else if (op == VINSTR_NEG_V) {
m_vtype = TYPE_VECTOR;
} else {
compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
}
}
ast_unary::~ast_unary()
{
if (m_operand)
ast_unref(m_operand);
}
ast_return::ast_return(lex_ctx_t ctx, ast_expression *expr)
: ast_expression(ctx, TYPE_ast_return)
, m_operand(expr)
{
if (expr)
propagateSideEffects(expr);
}
ast_return::~ast_return()
{
if (m_operand)
ast_unref(m_operand);
}
ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
: ast_entfield(ctx, entity, field, field->m_next)
{
if (field->m_vtype != TYPE_FIELD)
compile_error(ctx, "ast_entfield with expression not of type field");
}
ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
: ast_expression(ctx, TYPE_ast_entfield)
, m_entity(entity)
, m_field(field)
{
propagateSideEffects(m_entity);
propagateSideEffects(m_field);
if (!outtype) {
compile_error(ctx, "ast_entfield: field has no type");
m_vtype = TYPE_VOID;
}
else
adoptType(*outtype);
}
ast_entfield::~ast_entfield()
{
ast_unref(m_entity);
ast_unref(m_field);
}
ast_member *ast_member::make(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
{
if (field >= 3) {
compile_error(ctx, "ast_member: invalid field (>=3): %u", field);
return nullptr;
}
if (owner->m_vtype != TYPE_VECTOR &&
owner->m_vtype != TYPE_FIELD)
{
compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->m_vtype]);
return nullptr;
}
return new ast_member(ctx, owner, field, name);
}
ast_member::ast_member(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
: ast_expression(ctx, TYPE_ast_member)
, m_owner(owner)
, m_field(field)
, m_name(name)
, m_rvalue(false)
{
m_keep_node = true;
if (m_owner->m_vtype == TYPE_VECTOR) {
m_vtype = TYPE_FLOAT;
m_next = nullptr;
} else {
m_vtype = TYPE_FIELD;
m_next = ast_expression::shallowType(ctx, TYPE_FLOAT);
}
propagateSideEffects(owner);
}
ast_member::~ast_member()
{
// The owner is always an ast_value, which has .keep_node=true,
// also: ast_members are usually deleted after the owner, thus
// this will cause invalid access
//ast_unref(self->m_owner);
// once we allow (expression).x to access a vector-member, we need
// to change this: preferably by creating an alternate ast node for this
// purpose that is not garbage-collected.
}
ast_array_index* ast_array_index::make(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
{
ast_expression *outtype = array->m_next;
if (!outtype) {
// field has no type
return nullptr;
}
return new ast_array_index(ctx, array, index);
}
ast_array_index::ast_array_index(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
: ast_expression(ctx, TYPE_ast_array_index)
, m_array(array)
, m_index(index)
{
propagateSideEffects(array);
propagateSideEffects(index);
ast_expression *outtype = m_array->m_next;
adoptType(*outtype);
if (array->m_vtype == TYPE_FIELD && outtype->m_vtype == TYPE_ARRAY) {
// FIXME: investigate - this is not possible after adoptType
//if (m_vtype != TYPE_ARRAY) {
// compile_error(self->m_context, "array_index node on type");
// ast_array_index_delete(self);
// return nullptr;
//}
m_array = outtype;
m_vtype = TYPE_FIELD;
}
}
ast_array_index::~ast_array_index()
{
if (m_array)
ast_unref(m_array);
if (m_index)
ast_unref(m_index);
}
ast_argpipe::ast_argpipe(lex_ctx_t ctx, ast_expression *index)
: ast_expression(ctx, TYPE_ast_argpipe)
, m_index(index)
{
m_vtype = TYPE_NOEXPR;
}
ast_argpipe::~ast_argpipe()
{
if (m_index)
ast_unref(m_index);
}
ast_store::ast_store(lex_ctx_t ctx, int op, ast_expression *dest, ast_expression *source)
: ast_expression(ctx, TYPE_ast_store)
, m_op(op)
, m_dest(dest)
, m_source(source)
{
m_side_effects = true;
adoptType(*dest);
}
ast_store::~ast_store()
{
ast_unref(m_dest);
ast_unref(m_source);
}
ast_ifthen::ast_ifthen(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
: ast_expression(ctx, TYPE_ast_ifthen)
, m_cond(cond)
, m_on_true(ontrue)
, m_on_false(onfalse)
{
propagateSideEffects(cond);
if (ontrue)
propagateSideEffects(ontrue);
if (onfalse)
propagateSideEffects(onfalse);
}
ast_ifthen::~ast_ifthen()
{
ast_unref(m_cond);
if (m_on_true)
ast_unref(m_on_true);
if (m_on_false)
ast_unref(m_on_false);
}
ast_ternary::ast_ternary(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
: ast_expression(ctx, TYPE_ast_ternary)
, m_cond(cond)
, m_on_true(ontrue)
, m_on_false(onfalse)
{
propagateSideEffects(cond);
propagateSideEffects(ontrue);
propagateSideEffects(onfalse);
if (ontrue->m_vtype == TYPE_NIL)
adoptType(*onfalse);
else
adoptType(*ontrue);
}
ast_ternary::~ast_ternary()
{
/* the if()s are only there because computed-gotos can set them
* to nullptr
*/
if (m_cond) ast_unref(m_cond);
if (m_on_true) ast_unref(m_on_true);
if (m_on_false) ast_unref(m_on_false);
}
ast_loop::ast_loop(lex_ctx_t ctx,
ast_expression *initexpr,
ast_expression *precond, bool pre_not,
ast_expression *postcond, bool post_not,
ast_expression *increment,
ast_expression *body)
: ast_expression(ctx, TYPE_ast_loop)
, m_initexpr(initexpr)
, m_precond(precond)
, m_postcond(postcond)
, m_increment(increment)
, m_body(body)
, m_pre_not(pre_not)
, m_post_not(post_not)
{
if (initexpr)
propagateSideEffects(initexpr);
if (precond)
propagateSideEffects(precond);
if (postcond)
propagateSideEffects(postcond);
if (increment)
propagateSideEffects(increment);
if (body)
propagateSideEffects(body);
}
ast_loop::~ast_loop()
{
if (m_initexpr)
ast_unref(m_initexpr);
if (m_precond)
ast_unref(m_precond);
if (m_postcond)
ast_unref(m_postcond);
if (m_increment)
ast_unref(m_increment);
if (m_body)
ast_unref(m_body);
}
ast_breakcont::ast_breakcont(lex_ctx_t ctx, bool iscont, unsigned int levels)
: ast_expression(ctx, TYPE_ast_breakcont)
, m_is_continue(iscont)
, m_levels(levels)
{
}
ast_breakcont::~ast_breakcont()
{
}
ast_switch::ast_switch(lex_ctx_t ctx, ast_expression *op)
: ast_expression(ctx, TYPE_ast_switch)
, m_operand(op)
{
propagateSideEffects(op);
}
ast_switch::~ast_switch()
{
ast_unref(m_operand);
for (auto &it : m_cases) {
if (it.m_value)
ast_unref(it.m_value);
ast_unref(it.m_code);
}
}
ast_label::ast_label(lex_ctx_t ctx, const std::string &name, bool undefined)
: ast_expression(ctx, TYPE_ast_label)
, m_name(name)
, m_irblock(nullptr)
, m_undefined(undefined)
{
m_vtype = TYPE_NOEXPR;
}
ast_label::~ast_label()
{
}
void ast_label::registerGoto(ast_goto *g)
{
m_gotos.push_back(g);
}
ast_goto::ast_goto(lex_ctx_t ctx, const std::string &name)
: ast_expression(ctx, TYPE_ast_goto)
, m_name(name)
, m_target(nullptr)
, m_irblock_from(nullptr)
{
}
ast_goto::~ast_goto()
{
}
void ast_goto::setLabel(ast_label *label)
{
m_target = label;
}
ast_state::ast_state(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
: ast_expression(ctx, TYPE_ast_expression)
, m_framenum(frame)
, m_nextthink(think)
{
}
ast_state::~ast_state()
{
if (m_framenum)
ast_unref(m_framenum);
if (m_nextthink)
ast_unref(m_nextthink);
}
ast_call *ast_call::make(lex_ctx_t ctx, ast_expression *funcexpr)
{
if (!funcexpr->m_next) {
compile_error(ctx, "not a function");
return nullptr;
}
return new ast_call(ctx, funcexpr);
}
ast_call::ast_call(lex_ctx_t ctx, ast_expression *funcexpr)
: ast_expression(ctx, TYPE_ast_call)
, m_func(funcexpr)
, m_va_count(nullptr)
{
m_side_effects = true;
adoptType(*funcexpr->m_next);
}
ast_call::~ast_call()
{
for (auto &it : m_params)
ast_unref(it);
if (m_func)
ast_unref(m_func);
if (m_va_count)
ast_unref(m_va_count);
}
bool ast_call::checkVararg(ast_expression *va_type, ast_expression *exp_type) const
{
char texp[1024];
char tgot[1024];
if (!exp_type)
return true;
if (!va_type || !va_type->compareType(*exp_type))
{
if (va_type && exp_type)
{
ast_type_to_string(va_type, tgot, sizeof(tgot));
ast_type_to_string(exp_type, texp, sizeof(texp));
if (OPTS_FLAG(UNSAFE_VARARGS)) {
if (compile_warning(m_context, WARN_UNSAFE_TYPES,
"piped variadic argument differs in type: constrained to type %s, expected type %s",
tgot, texp))
return false;
} else {
compile_error(m_context,
"piped variadic argument differs in type: constrained to type %s, expected type %s",
tgot, texp);
return false;
}
}
else
{
ast_type_to_string(exp_type, texp, sizeof(texp));
if (OPTS_FLAG(UNSAFE_VARARGS)) {
if (compile_warning(m_context, WARN_UNSAFE_TYPES,
"piped variadic argument may differ in type: expected type %s",
texp))
return false;
} else {
compile_error(m_context,
"piped variadic argument may differ in type: expected type %s",
texp);
return false;
}
}
}
return true;
}
bool ast_call::checkTypes(ast_expression *va_type) const
{
char texp[1024];
char tgot[1024];
size_t i;
bool retval = true;
size_t count = m_params.size();
if (count > m_func->m_type_params.size())
count = m_func->m_type_params.size();
for (i = 0; i < count; ++i) {
if (ast_istype(m_params[i], ast_argpipe)) {
/* warn about type safety instead */
if (i+1 != count) {
compile_error(m_context, "argpipe must be the last parameter to a function call");
return false;
}
if (!checkVararg(va_type, m_func->m_type_params[i].get()))
retval = false;
}
else if (!m_params[i]->compareType(*m_func->m_type_params[i]))
{
ast_type_to_string(m_params[i], tgot, sizeof(tgot));
ast_type_to_string(m_func->m_type_params[i].get(), texp, sizeof(texp));
compile_error(m_context, "invalid type for parameter %u in function call: expected %s, got %s",
(unsigned int)(i+1), texp, tgot);
/* we don't immediately return */
retval = false;
}
}
count = m_params.size();
if (count > m_func->m_type_params.size() && m_func->m_varparam) {
for (; i < count; ++i) {
if (ast_istype(m_params[i], ast_argpipe)) {
/* warn about type safety instead */
if (i+1 != count) {
compile_error(m_context, "argpipe must be the last parameter to a function call");
return false;
}
if (!checkVararg(va_type, m_func->m_varparam))
retval = false;
}
else if (!m_params[i]->compareType(*m_func->m_varparam))
{
ast_type_to_string(m_params[i], tgot, sizeof(tgot));
ast_type_to_string(m_func->m_varparam, texp, sizeof(texp));
compile_error(m_context, "invalid type for variadic parameter %u in function call: expected %s, got %s",
(unsigned int)(i+1), texp, tgot);
/* we don't immediately return */
retval = false;
}
}
}
return retval;
}
ast_block::ast_block(lex_ctx_t ctx)
: ast_expression(ctx, TYPE_ast_block)
{
}
ast_block::~ast_block()
{
for (auto &it : m_exprs) ast_unref(it);
for (auto &it : m_locals) delete it;
for (auto &it : m_collect) delete it;
}
void ast_block::setType(const ast_expression &from)
{
if (m_next)
delete m_next;
adoptType(from);
}
bool ast_block::addExpr(ast_expression *e)
{
propagateSideEffects(e);
m_exprs.push_back(e);
if (m_next) {
delete m_next;
m_next = nullptr;
}
adoptType(*e);
return true;
}
void ast_block::collect(ast_expression *expr)
{
m_collect.push_back(expr);
expr->m_keep_node = true;
}
ast_function *ast_function::make(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
{
if (!vtype) {
compile_error(ctx, "internal error: ast_function_new condition 0");
return nullptr;
} else if (vtype->m_hasvalue || vtype->m_vtype != TYPE_FUNCTION) {
compile_error(ctx, "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
(int)!vtype,
(int)vtype->m_hasvalue,
vtype->m_vtype);
return nullptr;
}
return new ast_function(ctx, name, vtype);
}
ast_function::ast_function(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
: ast_node(ctx, TYPE_ast_function)
, m_function_type(vtype)
, m_name(name)
, m_builtin(0)
, m_static_count(0)
, m_ir_func(nullptr)
, m_curblock(nullptr)
, m_labelcount(0)
, m_varargs(nullptr)
, m_argc(nullptr)
, m_fixedparams(nullptr)
, m_return_value(nullptr)
{
vtype->m_hasvalue = true;
vtype->m_constval.vfunc = this;
}
ast_function::~ast_function()
{
if (m_function_type) {
// ast_value_delete(m_function_type);
m_function_type->m_hasvalue = false;
m_function_type->m_constval.vfunc = nullptr;
// We use unref - if it was stored in a global table it is supposed
// to be deleted from *there*
ast_unref(m_function_type);
}
if (m_fixedparams)
ast_unref(m_fixedparams);
if (m_return_value)
ast_unref(m_return_value);
// force this to be cleared before m_varargs/m_argc as blocks might
// try to access them via ast_unref()
m_blocks.clear();
}
const char* ast_function::makeLabel(const char *prefix)
{
size_t id;
size_t len;
char *from;
if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
!OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
!OPTS_OPTION_BOOL(OPTION_DEBUG))
{
return nullptr;
}
id = (m_labelcount++);
len = strlen(prefix);
from = m_labelbuf + sizeof(m_labelbuf)-1;
*from-- = 0;
do {
*from-- = (id%10) + '0';
id /= 10;
} while (id);
++from;
memcpy(from - len, prefix, len);
return from - len;
}
/*********************************************************************/
/* AST codegen part
* by convention you must never pass nullptr to the 'ir_value **out'
* parameter. If you really don't care about the output, pass a dummy.
* But I can't imagine a pituation where the output is truly unnecessary.
*/
static void codegen_output_type(ast_expression *self, ir_value *out)
{
if (out->m_vtype == TYPE_FIELD)
out->m_fieldtype = self->m_next->m_vtype;
if (out->m_vtype == TYPE_FUNCTION)