/
emit.c
1615 lines (1462 loc) · 55.1 KB
/
emit.c
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 <stdlib.h>
#include <string.h>
#include "gwion_util.h"
#include "gwion_ast.h"
#include "oo.h"
#include "vm.h"
#include "env.h"
#include "type.h"
#include "nspc.h"
#include "value.h"
#include "instr.h"
#include "emit.h"
#include "object.h"
#include "array.h"
#include "func.h"
#include "traverse.h"
#include "template.h"
#include "escape.h"
#include "parse.h"
#include "memoize.h"
#include "operator.h"
#include "import.h"
#include "switch.h"
typedef struct Local_ {
m_uint size;
m_uint offset;
m_bool is_obj;
} Local;
static inline void emit_pop_type(const Emitter emit) { nspc_pop_type(emit->env->curr); }
static inline void emit_pop(const Emitter emit, const m_uint scope) { env_pop(emit->env, scope); }
static inline m_uint emit_push(const Emitter emit, const Type type, const Nspc nspc) {
return env_push(emit->env, type, nspc);
}
static inline m_uint emit_push_type(const Emitter emit, const Type type) {
return env_push_type(emit->env, type);
}
static inline m_uint emit_push_global(const Emitter emit) {
return env_push_global(emit->env);
}
ANEW static Frame* new_frame() {
Frame* frame = mp_alloc(Frame);
vector_init(&frame->stack);
vector_add(&frame->stack, (vtype)NULL);
return frame;
}
ANN static void free_frame(Frame* a) {
LOOP_OPTIM
for(vtype i = vector_size(&a->stack) + 1; --i;)
if(vector_at(&a->stack, i - 1))
mp_free(Local, (Local*)vector_at(&a->stack, i - 1));
vector_release(&a->stack);
mp_free(Frame, a);
}
ANN static Local* new_local(const m_uint size, const m_bool is_obj) {
Local* local = mp_alloc(Local);
local->size = size;
local->is_obj = is_obj;
return local;
}
ANN static m_uint frame_local(Frame* frame, const m_uint size, const m_bool is_obj) {
Local* local = new_local(size, is_obj);
local->offset = frame->curr_offset;
frame->curr_offset += size;
vector_add(&frame->stack, (vtype)local);
return local->offset;
}
ANN static inline void frame_push(Frame* frame) {
vector_add(&frame->stack, (vtype)NULL);
}
ANN static m_int frame_pop(Frame* frame) {
const Local* l = (Local*)vector_pop(&frame->stack);
CHECK_OB(l)
frame->curr_offset -= l->size;
return l->is_obj ? (m_int)l->offset : frame_pop(frame);
}
ANN static m_bool emit_exp(const Emitter emit, Exp exp, const m_bool add_ref);
ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop);
ANN static m_bool emit_stmt_list(const Emitter emit, Stmt_List list);
ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot* member);
ANN static m_bool emit_func_def(const Emitter emit, const Func_Def func_def);
ANEW static Code* new_code(const Emitter emit, const m_str name) {
Code* code = mp_alloc(Code);
code->name = code_name_set(name, emit->env->name);
code->stack_depth = 0;
vector_init(&code->instr);
vector_init(&code->stack_break);
vector_init(&code->stack_cont);
vector_init(&code->stack_return);
code->frame = new_frame();
return code;
}
ANN static void free_code(Code* code) {
vector_release(&code->instr);
vector_release(&code->stack_break);
vector_release(&code->stack_cont);
vector_release(&code->stack_return);
free_frame(code->frame);
free(code->name);
mp_free(Code, code);
}
ANN static void emit_pop_scope(const Emitter emit) { GWDEBUG_EXE
m_int offset;
while((offset = frame_pop(emit->code->frame)) > -1) {
Instr instr = emit_add_instr(emit, ObjectRelease);
instr->m_val = (m_uint)offset;
}
}
ANN static inline void emit_push_code(const Emitter emit, const m_str name) {
vector_add(&emit->stack, (vtype)emit->code);
emit->code = new_code(emit, name);
}
ANN static inline void emit_pop_code(const Emitter emit) {
emit->code = (Code*)vector_pop(&emit->stack);
}
ANN static inline void emit_push_scope(const Emitter emit) {
frame_push(emit->code->frame);
}
ANN static inline m_uint emit_code_size(const Emitter emit) {
return vector_size(&emit->code->instr);
}
ANN static inline m_uint emit_code_offset(const Emitter emit) {
return emit->code->frame->curr_offset;
}
ANN static inline m_uint emit_local(const Emitter emit, const m_uint size, const m_bool is_obj) { GWDEBUG_EXE
return frame_local(emit->code->frame, size, is_obj);
}
ANN static void emit_pre_ctor_inner(const Emitter emit, const Type type) { GWDEBUG_EXE
const Instr instr = emit_add_instr(emit, PreCtor);
instr->m_val = (m_uint)type->nspc->pre_ctor;
instr->m_val2 = (m_uint)emit_code_offset(emit);
}
ANN static void emit_pre_ctor(const Emitter emit, const Type type) { GWDEBUG_EXE
if(type->parent)
emit_pre_ctor(emit, type->parent);
if(type->nspc->pre_ctor)
emit_pre_ctor_inner(emit, type);
if(GET_FLAG(type, template) && GET_FLAG(type, builtin)) {
const Type t = template_parent(type);
if(t->nspc->pre_ctor)
emit_pre_ctor_inner(emit, t);
}
}
ANN static void emit_pre_constructor_array(const Emitter emit, const Type type) {
const m_uint start_index = emit_code_size(emit);
const Instr top = emit_add_instr(emit, ArrayTop);
top->m_val2 = (m_uint)type;
emit_pre_ctor(emit, type);
const Instr bottom = emit_add_instr(emit, ArrayBottom);
top->m_val = emit_code_size(emit);
bottom->m_val = start_index;
emit_add_instr(emit, ArrayPost);
}
ANN ArrayInfo* emit_array_extend_inner(const Emitter emit, const Type t, const Exp e) { GWDEBUG_EXE
CHECK_BO(emit_exp(emit, e, 0))
const Type base = array_base(t);
ArrayInfo* info = mp_alloc(ArrayInfo);
vector_init(&info->type);
for(m_uint i = 1; i < t->array_depth; ++i)
vector_add(&info->type, (vtype)array_type(base, i));
vector_add(&info->type, (vtype)t);
info->depth = (m_int)t->array_depth;
info->base = base;
const Instr alloc = emit_add_instr(emit, ArrayAlloc);
*(ArrayInfo**)alloc->ptr = info;
if(isa(base, t_object) > 0) {
emit_pre_constructor_array(emit, base);
info->is_obj = 1;
}
return info;
}
ANN void emit_ext_ctor(const Emitter emit, const VM_Code code) { GWDEBUG_EXE
emit_add_instr(emit, RegDup);
const Instr push_f = emit_add_instr(emit, RegPushImm);
push_f->m_val = (m_uint)code;
const Instr offset = emit_add_instr(emit, RegPushImm);
offset->m_val = emit_code_offset(emit);
emit_add_instr(emit, FuncMember);
}
ANN m_bool emit_array_extend(const Emitter emit, const Type t, const Exp e) { GWDEBUG_EXE
CHECK_OB(emit_array_extend_inner(emit, t, e))
emit_add_instr(emit, PopArrayClass);
return 1;
}
ANN2(1,2) m_bool emit_instantiate_object(const Emitter emit, const Type type,
const Array_Sub array, const uint is_ref) {
if(type->array_depth) {
assert(array && array->exp);
ArrayInfo* info = emit_array_extend_inner(emit, type, array->exp);
CHECK_OB(info)
info->is_ref = !!is_ref;
} else if(!is_ref) {
const Instr instr = emit_add_instr(emit, ObjectInstantiate);
instr->m_val = (m_uint)type;
emit_pre_ctor(emit, type);
}
return GW_OK;
}
static inline enum Kind kindof(const m_uint size, const uint emit_var) {
if(emit_var)
return KIND_ADDR;
return size == SZ_INT ? KIND_INT : size == SZ_FLOAT ? KIND_FLOAT : KIND_OTHER;
}
ANN static Instr emit_kind(Emitter emit, const m_uint size, const uint addr, const f_instr func[]) {
const enum Kind kind = kindof(size, addr);
const Instr instr = emit_add_instr(emit, func[kind]);
instr->m_val2 = size;
return instr;
}
static const f_instr regpushimm[] = { RegPushImm, RegPushImm2, RegPushImm3, RegPushDeref }; // caution last
static const f_instr regpushderef[] = { RegPushDeref, RegPushDeref2, RegPushDeref3, RegPushDeref }; // caution last
static const f_instr regpushmem[] = { RegPushMem, RegPushMem2, RegPushMem3, RegPushMem4 };
static const f_instr regpushbase[] = { RegPushBase, RegPushBase2, RegPushBase3, RegPushBase4 };
static const f_instr dotstatic[] = { DotStatic, DotStatic2, DotStatic3, DotStatic4 };
static const f_instr dotimport[] = { DotImport, DotImport2, DotImport3, DotImport4 };
static const f_instr dotmember[] = { DotMember, DotMember2, DotMember3, DotMember4 };
static const f_instr allocmember[] = { PushNull, PushNull2, PushNull3, AllocMember4 };
static const f_instr allocword[] = { AllocWord, AllocWord2, AllocWord3, AllocWord4 };
ANN static m_bool emit_symbol_owned(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE
const Value v = prim->value;
const Exp exp = new_exp_prim_id(insert_symbol("this"), prim->self->pos);
const Exp dot = new_exp_dot(exp, prim->d.var);
exp->type = v->owner_class;
dot->d.exp_dot.t_base = v->owner_class;
dot->type = v->type;
dot->emit_var = prim->self->emit_var;
const m_bool ret = emit_exp_dot(emit, &dot->d.exp_dot);
free_exp(exp);
return ret;
}
ANN static m_bool emit_symbol_builtin(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE
const Value v = prim->value;
if(GET_FLAG(v, func)) {
const Instr instr = emit_add_instr(emit, RegPushImm);
instr->m_val = (m_uint)v->d.func_ref;
return GW_OK;
}
if(GET_FLAG(v, union)) {
const m_uint size = v->type->size;
const Instr instr = emit_kind(emit, size, prim->self->emit_var, regpushderef);
*(m_uint*)instr->ptr = (m_uint)v->d.ptr;
} else {
const m_uint size = v->type->size;
const Instr instr = emit_kind(emit, size, prim->self->emit_var, regpushimm);
if(size == SZ_INT) {
instr->execute = RegPushImm;
instr->m_val = (m_uint)v->d.ptr;
} else if(v->d.ptr)
memcpy(instr->ptr, v->d.ptr, v->type->size);
else
*(m_uint**)instr->ptr = v->d.ptr;
}
return GW_OK;
}
ANN static m_bool emit_symbol(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE
const Value v = prim->value;
if(v->owner_class)
return emit_symbol_owned(emit, prim);
if(GET_FLAG(v, builtin) || GET_FLAG(v, enum) ||
GET_FLAG(v, union))
return emit_symbol_builtin(emit, prim);
const m_uint size = v->type->size;
const Instr instr = emit_kind(emit, size, prim->self->emit_var, !GET_FLAG(v, global) ? regpushmem : regpushbase);
instr->m_val = v->offset;
return GW_OK;
}
ANEW ANN VM_Code emit_code(const Emitter emit) { GWDEBUG_EXE
Code* c = emit->code;
const VM_Code code = new_vm_code(&c->instr, c->stack_depth,
GET_FLAG(c, member), c->name);
free_code(c);
return code;
}
ANN static VM_Code finalyze(const Emitter emit) {
emit_add_instr(emit, EOC);
const VM_Code code = emit_code(emit);
emit_pop_code(emit);
return code;
}
ANN static m_bool prim_array(const Emitter emit, const Exp_Primary * primary) {
const Array_Sub array = primary->d.array;
Exp e = array->exp;
CHECK_BB(emit_exp(emit, e, 0))
m_uint count = 0;
do ++count;
while((e = e->next));
const Type type = array->type;
const Type base = array_base(type);
const Instr instr = emit_add_instr(emit, ArrayInit);
instr->m_val = count;
instr->m_val2 = base->size;
*(Type*)instr->ptr = type;
return GW_OK;
}
ANN static m_uint get_depth(Type t) {
m_uint depth = 0;
do depth += t->array_depth;
while((t = t->parent));
return depth;
}
ANN static m_bool emit_exp_array(const Emitter emit, const Exp_Array* array) { GWDEBUG_EXE
const m_uint is_var = array->self->emit_var;
const m_uint depth = get_depth(array->base->type) - array->self->type->array_depth;
CHECK_BB(emit_exp(emit, array->base, 0))
CHECK_BB(emit_exp(emit, array->array->exp, 0))
if(depth == 1) {
const Instr instr = emit_add_instr(emit, ArrayAccess);
instr->m_val = is_var;
instr->m_val2 = is_var ? SZ_INT : array->self->type->size;
} else {
const Instr instr = emit_add_instr(emit, ArrayAccessMulti);
instr->m_val = is_var || array->self->type->array_depth;
instr->m_val2 = (is_var || array->self->type->array_depth) ?
SZ_INT : array_base(array->base->type)->size;
*(m_uint*)instr->ptr = depth;
}
return GW_OK;
}
ANN static m_bool prim_vec(const Emitter emit, const Exp_Primary * primary) { GWDEBUG_EXE
const Vec * vec = &primary->d.vec;
const ae_prim_t t = primary->primary_type;
CHECK_BB(emit_exp(emit, vec->exp, 0));
m_int n = (m_int)((t == ae_primary_vec ? 3 : 2) - vec->dim + 1);
while(--n > 0)
emit_add_instr(emit, PushNull2);
return GW_OK;
}
ANN static m_bool prim_id(const Emitter emit, const Exp_Primary* prim) {
if(prim->d.var == insert_symbol("this"))
emit_add_instr(emit, RegPushMem);
else if(prim->d.var == insert_symbol("me"))
emit_add_instr(emit, RegPushMe);
else if(prim->d.var == insert_symbol("now"))
emit_add_instr(emit, RegPushNow);
else if(prim->d.var == insert_symbol("maybe"))
emit_add_instr(emit, RegPushMaybe);
else if(prim->d.var == insert_symbol("__func__")) {
const Instr instr = emit_add_instr(emit, RegPushStr);
instr->m_val = (m_uint)s_name(insert_symbol(emit->env->func ?
emit->env->func->name : emit->env->class_def ?
emit->env->class_def->name : emit->env->name));
} else
emit_symbol(emit, prim);
return GW_OK;
}
ANN static m_bool prim_num(const Emitter emit, const Exp_Primary * primary) {
const Instr instr = emit_add_instr(emit, RegPushImm);
instr->m_val = primary->d.num;
return GW_OK;
}
ANN static m_bool prim_float(const Emitter emit, const Exp_Primary* primary) {
const Instr instr = emit_add_instr(emit, RegPushImm2);
*(m_float*)instr->ptr = primary->d.fnum;
return GW_OK;
}
ANN static m_bool prim_char(const Emitter emit, const Exp_Primary* prim) {
const m_int c = str2char(prim->d.chr, prim->self->pos);
CHECK_BB(c);
const Instr instr = emit_add_instr(emit, RegPushImm);
instr->m_val = (m_uint)c;
return GW_OK;
}
ANN static m_bool prim_str(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE
char c[strlen(prim->d.str)];
strcpy(c, prim->d.str);
CHECK_BB(escape_str(c, prim->self->pos));
const Instr instr = emit_add_instr(emit, RegPushStr);
instr->m_val = (m_uint)s_name(insert_symbol(c));
return GW_OK;
}
ANN static m_bool prim_gack(const Emitter emit, const Exp_Primary* primary) {
const Exp exp = primary->d.exp;
const Vector v = new_vector();
m_uint offset = 0;
Exp e = exp;
do {
vector_add(v, (vtype)e->type);
offset += e->type->size;
if(e->type != emit->env->class_def)
ADD_REF(e->type);
} while((e = e->next));
if(emit_exp(emit, exp, 0) < 0) {
free_vector(v);
ERR_B(exp->pos, "\t... in 'gack' expression.")
}
const Instr instr = emit_add_instr(emit, Gack);
*(Vector*)instr->ptr = v;
instr->m_val = offset;
return GW_OK;
}
#ifdef OPTIMIZE
ANN static m_bool prim_constprop(Emitter emit, const Exp_Primary* prim) {
const Instr instr = emit_add_instr(emit, ConstPropSet);
instr->m_val = prim->value->offset;
instr->m_val2 = prim->d.num;
*(m_bool*)instr->ptr = prim->self->emit_var;
return GW_OK;
}
#endif
static const _exp_func prim_func[] = {
(_exp_func)prim_id, (_exp_func)prim_num, (_exp_func)prim_float, (_exp_func)prim_str,
(_exp_func)prim_array, (_exp_func)prim_gack, (_exp_func)prim_vec, (_exp_func)prim_vec,
(_exp_func)prim_vec, (_exp_func)prim_char, (_exp_func)dummy_func,
#ifdef OPTIMIZE
(_exp_func)prim_constprop
#endif
};
ANN static m_bool emit_exp_primary(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE
return prim_func[prim->primary_type](emit, prim);
}
ANN static m_bool emit_dot_static_data(const Emitter emit, const Value v, const uint emit_var) { GWDEBUG_EXE
const Instr push = emit_add_instr(emit, RegPushImm);
push->m_val = (m_uint)v->owner_class;
const m_uint size = v->type->size;
const Instr instr = emit_kind(emit, size, emit_var, dotstatic);
instr->m_val = v->offset;
return GW_OK;
}
ANN static m_bool decl_static(const Emitter emit, const Var_Decl var_decl, const uint is_ref) {
const Value v = var_decl->value;
Code* code = emit->code;
emit->code = (Code*)vector_back(&emit->stack);
CHECK_BB(emit_instantiate_object(emit, v->type, var_decl->array, is_ref))
CHECK_BB(emit_dot_static_data(emit, v, 1))
emit_add_instr(emit, ObjectAssign);
emit->code = code;
return GW_OK;
}
ANN static m_bool emit_exp_decl_static(const Emitter emit, const Var_Decl var_decl, const uint is_ref) { GWDEBUG_EXE
const Value value = var_decl->value;
if(isa(value->type, t_object) > 0 && !is_ref)
CHECK_BB(decl_static(emit, var_decl, 0))
return emit_dot_static_data(emit, value, 1);
}
ANN static m_bool emit_exp_decl_non_static(const Emitter emit, const Var_Decl var_decl,
const uint is_ref, const uint emit_var) { GWDEBUG_EXE
const Value v = var_decl->value;
const Type type = v->type;
const Array_Sub array = var_decl->array;
const m_bool is_array = array && array->exp;
const m_bool is_obj = isa(type, t_object) > 0;
const uint emit_addr = ((is_ref && !array) || isa(type, t_object) < 0) ?
emit_var : 1;
if(is_obj && (is_array || !is_ref))
CHECK_BB(emit_instantiate_object(emit, type, array, is_ref))
f_instr *exec = (f_instr*)allocmember;
if(!GET_FLAG(v, member)) {
v->offset = emit_local(emit, v->type->size, is_obj);
exec = (f_instr*)allocword;
}
const Instr instr = emit_kind(emit, v->type->size, emit_addr, exec);
instr->m_val = v->offset;
if(is_obj && (is_array || !is_ref)) {
const Instr assign = emit_add_instr(emit, ObjectAssign);
assign->m_val = (m_uint)emit_var;
if(is_array && !emit->env->scope)
ADD_REF(type)
}
return GW_OK;
}
ANN static m_bool emit_class_def(const Emitter, const Class_Def);
ANN static m_bool emit_exp_decl_template(const Emitter emit, const Exp_Decl* decl) { GWDEBUG_EXE
const Type t = typedef_base(decl->type);
if(!GET_FLAG(t, emit)) {
CHECK_BB(template_push_types(emit->env, t->def->tmpl->list.list, t->def->tmpl->base))
CHECK_BB(emit_class_def(emit, t->def))
emit_pop_type(emit);
}
return GW_OK;
}
ANN static m_bool emit_exp_decl(const Emitter emit, const Exp_Decl* decl) { GWDEBUG_EXE
Var_Decl_List list = decl->list;
const uint ref = GET_FLAG(decl->td, ref) || type_ref(decl->type);
const uint var = decl->self->emit_var;
if(GET_FLAG(decl->type, template))
CHECK_BB(emit_exp_decl_template(emit, decl))
m_uint scope;
const m_bool global = GET_FLAG(decl->td, global);
if(global)
scope = emit_push_global(emit);
do {
const uint r = (uint)(GET_FLAG(list->self->value, ref) + ref);
if(!GET_FLAG(list->self->value, used))
continue;
if(GET_FLAG(decl->td, static))
CHECK_BB(emit_exp_decl_static(emit, list->self, r))
else
CHECK_BB(emit_exp_decl_non_static(emit, list->self, r, var))
} while((list = list->next));
if(global)
emit_pop(emit, scope);
return GW_OK;
}
ANN static m_uint vararg_size(const Exp_Call* exp_call, const Vector kinds) {
Exp e = exp_call->args;
Arg_List l = exp_call->m_func->def->arg_list;
m_uint size = 0;
while(e) {
if(!l) {
size += e->type->size;
vector_add(kinds, e->type->size);
} else
l = l->next;
e = e->next;
}
return size;
}
ANN static void emit_func_arg_vararg(const Emitter emit, const Exp_Call* exp_call) { GWDEBUG_EXE
const Instr instr = emit_add_instr(emit, VarargIni);
const Vector kinds = new_vector();
if((instr->m_val = vararg_size(exp_call, kinds)))
instr->m_val2 = (m_uint)kinds;
else {
instr->execute = VarargEmpty;
free_vector(kinds);
}
}
ANN static m_bool emit_func_args(const Emitter emit, const Exp_Call* exp_call) { GWDEBUG_EXE
if(exp_call->args)
CHECK_BB(emit_exp(emit, exp_call->args, 1))
if(GET_FLAG(exp_call->m_func->def, variadic))
emit_func_arg_vararg(emit, exp_call);
return GW_OK;
}
ANN static m_bool prepare_call(const Emitter emit, const Exp_Call* exp_call) { GWDEBUG_EXE
CHECK_BB(emit_func_args(emit, exp_call))
return emit_exp(emit, exp_call->func, 0);
}
ANN static inline m_int push_tmpl_func(const Emitter emit, const Func f,
const Type_List types) {
const Value v = f->value_ref;
const m_uint scope = emit_push(emit, v->owner_class, v->owner);
CHECK_BB(traverse_func_template(emit->env, f->def, types))
return (m_int)scope;
}
ANN static m_bool emit_exp_call_template(const Emitter emit, const Exp_Call* exp_call) {
if(emit->env->func && emit->env->func == exp_call->m_func)
return prepare_call(emit, exp_call);
m_int scope = push_tmpl_func(emit, exp_call->m_func, exp_call->tmpl->types);
CHECK_BB(scope);
CHECK_BB(prepare_call(emit, exp_call))
emit_pop_type(emit);
emit_pop(emit, (m_uint)scope);
UNSET_FLAG(exp_call->m_func, checked);
return GW_OK;
}
ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call* exp_call) { GWDEBUG_EXE
if(!exp_call->tmpl)
CHECK_BB(prepare_call(emit, exp_call))
else
CHECK_BB(emit_exp_call_template(emit, exp_call))
return emit_exp_call1(emit, exp_call->m_func);
}
ANN static m_bool emit_binary_func(const Emitter emit, const Exp_Binary* bin) { GWDEBUG_EXE
const Exp_Call exp = { .func=bin->rhs, .args=bin->lhs, .m_func=bin->func, .tmpl=bin->tmpl, .self=bin->self };
return emit_exp_call(emit, &exp);
}
ANN static m_bool emit_exp_binary(const Emitter emit, const Exp_Binary* bin) { GWDEBUG_EXE
const Exp lhs = bin->lhs;
const Exp rhs = bin->rhs;
struct Op_Import opi = { .op=bin->op, .lhs=lhs->type, .rhs=rhs->type, .data = (uintptr_t)bin };
if(bin->op == op_chuck && isa(rhs->type, t_function) > 0)
return emit_binary_func(emit, bin);
CHECK_BB(emit_exp(emit, lhs, 1))
CHECK_BB(emit_exp(emit, rhs, 1))
return op_emit(emit, &opi);
}
ANN static m_bool emit_exp_cast(const Emitter emit, const Exp_Cast* cast) {
struct Op_Import opi = { .op=op_cast, .lhs=cast->exp->type, .rhs=cast->self->type, .data=(uintptr_t)cast};
CHECK_BB(emit_exp(emit, cast->exp, 0))
(void)op_emit(emit, &opi);
return GW_OK;
}
ANN static m_bool emit_exp_post(const Emitter emit, const Exp_Postfix* post) { GWDEBUG_EXE
struct Op_Import opi = { .op=post->op, .lhs=post->exp->type, .data=(uintptr_t)post };
CHECK_BB(emit_exp(emit, post->exp, 1))
return op_emit(emit, &opi);
}
ANN static m_bool emit_exp_dur(const Emitter emit, const Exp_Dur* dur) { GWDEBUG_EXE
CHECK_BB(emit_exp(emit, dur->base, 0))
CHECK_BB(emit_exp(emit, dur->unit, 0))
emit_add_instr(emit, FloatTimes);
return GW_OK;
}
static inline m_bool push_func_code(const Emitter emit, const Func f) {
const Instr back = (Instr)vector_back(&emit->code->instr);
if(back->execute == RegPushBase) {
back->execute = RegPushImm;
back->m_val = (m_uint)f->code;
return GW_OK;
}
const Instr instr = emit_add_instr(emit, RegPushPtr);
return !!(instr->m_val = (m_uint)f->code);
}
static m_bool emit_template_code(const Emitter emit, const Func f) {
if(GET_FLAG(f, ref))
CHECK_BB(traverse_template(emit->env, f->value_ref->owner_class->def))
CHECK_BB(emit_func_def(emit, f->def))
return push_func_code(emit, f);
}
ANN static Instr emit_call(const Emitter emit, const Func f) {
MEMOIZE_CALL
const Type t = actual_type(f->value_ref->type);
const f_instr exec = isa(t, t_fptr) < 0 ? GET_FLAG(f->def, builtin) ?
GET_FLAG(f, member) ? FuncMember : FuncStatic : FuncUsr : FuncPtr;
return emit_add_instr(emit, exec);
}
ANN m_bool emit_exp_call1(const Emitter emit, const Func f) { GWDEBUG_EXE
if(!f->code || (GET_FLAG(f, ref) && !GET_FLAG(f, builtin))) {
if(GET_FLAG(f, template) && emit->env->func != f)
CHECK_BB(emit_template_code(emit, f))
} else
push_func_code(emit, f);
const Instr offset = emit_add_instr(emit, RegPushImm);
offset->m_val = emit_code_offset(emit);
const Instr instr = emit_call(emit, f);
const m_uint size = instr->m_val = f->def->ret_type->size;
return (m_bool)(instr->m_val2 = kindof(size, !size));
}
ANN2(1,2) static m_bool emit_exp_spork_finish(const Emitter emit, const VM_Code code,
const m_uint depth, const m_bool f) {
const Instr spork = emit_add_instr(emit, f ? SporkExp : SporkFunc);
spork->m_val = depth;
spork->m_val2 = (m_uint)code;
return GW_OK;
}
static inline void stack_alloc(const Emitter emit) {
emit_local(emit, SZ_INT, 0);
emit->code->stack_depth += SZ_INT;
}
static inline void stack_alloc_this(const Emitter emit) {
SET_FLAG(emit->code, member);
stack_alloc(emit);
}
static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) {
++emit->env->scope;
emit_push_scope(emit);
const m_bool pure = SAFE_FLAG(emit->env->func, pure);
if(!pure)
emit_add_instr(emit, GcIni);
CHECK_BB(emit_stmt(emit, stmt, pop))
if(!pure)
emit_add_instr(emit, GcEnd);
emit_pop_scope(emit);
--emit->env->scope;
return GW_OK;
}
#define SPORK_FUNC_PREFIX "spork~func:%i"
#define SPORK_CODE_PREFIX "spork~code:%i"
static void push_spork_code(const Emitter emit, const m_str prefix, const int pos) {
char c[strlen(SPORK_FUNC_PREFIX) + num_digit(pos) + 1];
sprintf(c, prefix, pos);
emit_push_code(emit, c);
}
ANN static m_bool spork_func(const Emitter emit, const Exp_Call* exp) { GWDEBUG_EXE
CHECK_BB(prepare_call(emit, exp))
push_spork_code(emit, SPORK_FUNC_PREFIX, exp->self->pos);
if(GET_FLAG(exp->m_func, member))
SET_FLAG(emit->code, member);
const Instr op = emit_add_instr(emit, MemPushImm);
op->m_val = emit->code->stack_depth;
emit_add_instr(emit, RegPushImm); // should push func
CHECK_BB(emit_exp_call1(emit, exp->m_func))
const VM_Code code = finalyze(emit);
const m_uint size = exp->m_func->def->stack_depth - (GET_FLAG(exp->m_func,
member) ? SZ_INT : 0);
return emit_exp_spork_finish(emit, code, size, 0);
}
ANN static m_bool spork_code(const Emitter emit, const Stmt stmt) { GWDEBUG_EXE
emit_add_instr(emit, RegPushImm);
push_spork_code(emit, SPORK_CODE_PREFIX, stmt->pos);
if(SAFE_FLAG(emit->env->func, member))
stack_alloc_this(emit);
CHECK_BB(scoped_stmt(emit, stmt, 0))
const VM_Code code = finalyze(emit);
return emit_exp_spork_finish(emit, code, emit->code->stack_depth, 1);
}
ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary* unary) {
return unary->code ? spork_code(emit, unary->code) :
spork_func(emit, &unary->exp->d.exp_call);
}
ANN static m_bool emit_exp_unary(const Emitter emit, const Exp_Unary* unary) { GWDEBUG_EXE
struct Op_Import opi = { .op=unary->op, .data=(uintptr_t)unary };
if(unary->op != op_spork && unary->exp) {
CHECK_BB(emit_exp(emit, unary->exp, 1))
opi.rhs = unary->exp->type;
}
return op_emit(emit, &opi);
}
ANN static m_bool emit_implicit_cast(const Emitter emit,
const restrict Type from, const restrict Type to) { GWDEBUG_EXE
struct Op_Import opi = { .op=op_impl, .lhs=from, .rhs=to, .data=(m_uint)from };
return op_emit(emit, &opi);
}
ANN static Instr emit_flow(const Emitter emit, const Type type,
const f_instr f1, const f_instr f2) { GWDEBUG_EXE
if(isa(type, t_float) > 0) {
emit_add_instr(emit, PushNull2);
return emit_add_instr(emit, f2);
}
emit_add_instr(emit, PushNull);
return emit_add_instr(emit, f1);
}
ANN static m_bool emit_exp_if(const Emitter emit, const Exp_If* exp_if) { GWDEBUG_EXE
CHECK_BB(emit_exp(emit, exp_if->cond, 0))
const Instr op = emit_flow(emit, exp_if->cond->type, BranchEqInt, BranchEqFloat);
CHECK_BB(emit_exp(emit, exp_if->if_exp, 0))
const Instr op2 = emit_add_instr(emit, Goto);
op->m_val = emit_code_size(emit);
const m_bool ret = emit_exp(emit, exp_if->else_exp, 0);
op2->m_val = emit_code_size(emit);
return ret;
}
#ifdef OPTIMIZE
ANN static void emit_exp_constprop(const Emitter emit, const Exp e) {
if(!e->emit_var) {
if(e->exp_type == ae_exp_constprop) {
const Instr instr = emit_add_instr(emit, RegPushMem);
instr->m_val = e->d.exp_primary.value->offset;
*(m_uint*)instr->ptr = GET_FLAG(e->d.exp_primary.value, global);
return;
}
const Instr instr = emit_add_instr(emit, ConstPropGet);
instr->m_val2 = e->d.exp_primary.value->offset;
instr->m_val = e->d.exp_primary.d.num;
*(m_uint*)instr->ptr = 1;
} else {
const Instr instr = emit_add_instr(emit, ConstPropSet);
instr->m_val = e->d.exp_primary.value->offset;
*(m_bool*)instr->ptr = 1;
instr->m_val2 = e->d.exp_primary.d.num;
}
}
#endif
DECL_EXP_FUNC(emit)
ANN2(1) static m_bool emit_exp(const Emitter emit, Exp exp, const m_bool ref) { GWDEBUG_EXE
do {
CHECK_BB(exp_func[exp->exp_type](emit, &exp->d))
if(exp->cast_to)
CHECK_BB(emit_implicit_cast(emit, exp->type, exp->cast_to))
if(ref && isa(exp->type, t_object) > 0) {
const Instr instr = emit_add_instr(emit, RegAddRef);
instr->m_val = exp->emit_var;
}
if(emit->env->func && isa(exp->type, t_function) > 0 &&
!GET_FLAG(exp->type->d.func->value_ref->d.func_ref, pure))
UNSET_FLAG(emit->env->func, pure);
} while((exp = exp->next));
return GW_OK;
}
ANN static m_bool emit_stmt_if(const Emitter emit, const Stmt_If stmt) { GWDEBUG_EXE
emit_push_scope(emit);
CHECK_BB(emit_exp(emit, stmt->cond, 0))
const Instr op = emit_flow(emit, isa(stmt->cond->type, t_object) > 0 ?
t_int : stmt->cond->type, BranchEqInt, BranchEqFloat);
CHECK_OB(op)
CHECK_BB(scoped_stmt(emit, stmt->if_body, 1))
const Instr op2 = emit_add_instr(emit, Goto);
op->m_val = emit_code_size(emit);
if(stmt->else_body)
CHECK_BB(scoped_stmt(emit, stmt->else_body, 1))
op2->m_val = emit_code_size(emit);
emit_pop_scope(emit);
return GW_OK;
}
ANN static m_bool emit_stmt_code(const Emitter emit, const Stmt_Code stmt) { GWDEBUG_EXE
++emit->env->scope;
const m_bool ret = stmt->stmt_list ? emit_stmt_list(emit, stmt->stmt_list) : 1;
--emit->env->scope;
return ret;
}
#ifdef OPTIMIZE
ANN static m_bool optimize_taill_call(const Emitter emit, const Exp_Call* e) {
Exp arg = e->args;
if(arg)
CHECK_BB(emit_exp(emit, e->args, 0))
const Instr instr = emit_add_instr(emit, PutArgsInMem);
while(arg) {
instr->m_val += arg->type->size;
arg = arg->next;
}
emit_add_instr(emit, Goto);
return GW_OK;
}
#define OPTIMIZE_TCO\
if(stmt->val->exp_type == ae_exp_call && emit->env->func == stmt->val->d.exp_call.m_func)\
return optimize_taill_call(emit, &stmt->val->d.exp_call);
#else
#define OPTIMIZE_TCO
#endif
ANN static m_bool emit_stmt_return(const Emitter emit, const Stmt_Exp stmt) { GWDEBUG_EXE
if(stmt->val) {
OPTIMIZE_TCO
CHECK_BB(emit_exp(emit, stmt->val, 0))
}
vector_add(&emit->code->stack_return, (vtype)emit_add_instr(emit, Goto));
return GW_OK;
}
ANN static inline m_bool emit_stmt_continue(const Emitter emit, const Stmt stmt __attribute__((unused))) { GWDEBUG_EXE
vector_add(&emit->code->stack_cont, (vtype)emit_add_instr(emit, Goto));
return GW_OK;
}
ANN static inline m_bool emit_stmt_break(const Emitter emit, const Stmt stmt __attribute__((unused))) { GWDEBUG_EXE
vector_add(&emit->code->stack_break, (vtype)emit_add_instr(emit, Goto));
return GW_OK;
}
ANN static inline void emit_push_stack(const Emitter emit) {
emit_push_scope(emit);
vector_add(&emit->code->stack_cont, (vtype)NULL);
vector_add(&emit->code->stack_break, (vtype)NULL);
}
ANN static void pop_vector(Vector v, const m_uint pc) {
Instr instr;
while((instr = (Instr)vector_pop(v)))
instr->m_val = pc;
}
ANN static void emit_pop_stack(const Emitter emit, const m_uint index) {
pop_vector(&emit->code->stack_cont, index);
pop_vector(&emit->code->stack_break, emit_code_size(emit));
emit_pop_scope(emit);
}
ANN static Instr _flow(const Emitter emit, const Exp e, const m_bool b) {
CHECK_BO(emit_exp(emit, e, 0))
const f_instr instr_i = b ? BranchEqInt : BranchNeqInt;
const f_instr instr_f = b ? BranchEqFloat : BranchNeqFloat;
return emit_flow(emit, e->type, instr_i, instr_f);
}
ANN static m_bool emit_stmt_flow(const Emitter emit, const Stmt_Flow stmt) { GWDEBUG_EXE
const m_uint index = emit_code_size(emit);
Instr op = NULL;
emit_push_stack(emit);
if(!stmt->is_do)
op = _flow(emit, stmt->cond, stmt->self->stmt_type == ae_stmt_while);
CHECK_BB(scoped_stmt(emit, stmt->body, 1))
if(stmt->is_do) {
CHECK_OB((op = _flow(emit, stmt->cond, stmt->self->stmt_type != ae_stmt_while)))
op->m_val = index;
} else {
const Instr goto_ = emit_add_instr(emit, Goto);
goto_->m_val = index;
op->m_val = emit_code_size(emit);
}
emit_pop_stack(emit, index);
return GW_OK;
}
ANN static m_uint get_decl_size(Var_Decl_List a) {
m_uint size = 0;
do if(GET_FLAG(a->self->value, used))
size += a->self->value->type->size;
while((a = a->next));
return size;
}
ANN static m_uint pop_exp_size(const Emitter emit, Exp e) {
m_uint size = 0;
do {
if(e->exp_type == ae_exp_primary &&
e->d.exp_primary.primary_type == ae_primary_hack) {
size += pop_exp_size(emit, e->d.exp_primary.d.exp);
continue;
}
size += (e->exp_type == ae_exp_decl ?
get_decl_size(e->d.exp_decl.list) : e->type->size);
} while((e = e->next));
return size;
}
ANN static void pop_exp(const Emitter emit, Exp e) {
const m_uint size = pop_exp_size(emit, e);
if(size) {
const Instr instr = emit_add_instr(emit, RegPop);
instr->m_val = size;
}
}
ANN static m_bool emit_stmt_for(const Emitter emit, const Stmt_For stmt) { GWDEBUG_EXE
emit_push_stack(emit);
CHECK_BB(emit_stmt(emit, stmt->c1, 1))
const m_uint index = emit_code_size(emit);
CHECK_BB(emit_stmt(emit, stmt->c2, 0))
const Instr op = emit_flow(emit, stmt->c2->d.stmt_exp.val->type,
BranchEqInt, BranchEqFloat);
CHECK_BB(scoped_stmt(emit, stmt->body, 1))
const m_uint action_index = emit_code_size(emit);
if(stmt->c3) {
CHECK_BB(emit_exp(emit, stmt->c3, 0))
pop_exp(emit, stmt->c3);
}
const Instr _goto = emit_add_instr(emit, Goto);
_goto->m_val = index;
op->m_val = emit_code_size(emit);
emit_pop_stack(emit, action_index);
return GW_OK;
}
ANN static m_bool emit_stmt_auto(const Emitter emit, const Stmt_Auto stmt) { GWDEBUG_EXE
CHECK_BB(emit_exp(emit, stmt->exp, 0))
const Instr s1 = emit_add_instr(emit, MemSetImm);
const Instr s2 = emit_add_instr(emit, MemSetImm);
const m_uint ini_pc = emit_code_size(emit);
emit_push_stack(emit);
const Instr loop = emit_add_instr(emit, AutoLoopStart);