This repository was archived by the owner on Nov 22, 2023. It is now read-only.
forked from gek169/seabass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathastexec.c
3032 lines (2876 loc) · 101 KB
/
astexec.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 "stringutil.h"
#include "targspecifics.h"
#include "libmin.h"
#include "parser.h"
#include "data.h"
#include "metaprogramming.h"
#include "astexec.h"
static uint64_t is_debugging = 1; //only used in debug_print
static uint64_t executing_function = 0; //the symbol (index into the symbol table) of the function where code is currently being executed.
static scope_astexec** scope_executing_stack; //stack of all scopes. every function, every if statement, etcetera add
static uint64_t n_scopes_executings = 0; //depth of the scope_executing_stack.
typedef struct{
uint64_t pos;
uint64_t is_in_loop;
uint64_t is_else_chaining;
}vm_scope_position_execution_info;
static vm_scope_position_execution_info *scope_positions;
void vm_allocate_needed_memory(size_t amt){
//allocate tons of memory
scope_positions = calloc(1,amt * sizeof(vm_scope_position_execution_info));
scope_executing_stack = calloc(1,amt * sizeof(scope_astexec*));
}
static inline void scope_executing_stack_push(scope_astexec* s){
//scope_executing_stack = realloc(scope_executing_stack, (++n_scopes_executings) * sizeof(scope*));
++n_scopes_executings;
scope_executing_stack[n_scopes_executings-1] = s;
}
static inline void scope_executing_stack_pop(){
n_scopes_executings--;
}
static inline scope_astexec* scope_executing_stack_gettop(){ //This function causes @global to crash... hmmm
return scope_executing_stack[n_scopes_executings-1];
}
static void debug_print(char* msg, uint64_t printme1, uint64_t printme2){
if(is_debugging == 0) return;
char buf[100];
puts(msg);
if(printme1){
mutoa(buf, printme1);
puts(buf);
}
if(printme2){
mutoa(buf, printme2);
puts(buf);
}
}
/*
VM Stack, used for the codegen stack.
*/
static ast_vm_stack_elem vm_stack[0x1000000];
static unsigned char vm_bigstack[0x1000000];
static uint64_t vm_stackpointer = 0;
static uint64_t vm_bigstackptr = 0;
/*
How much memory is the frame using?
what about the expression?
*/
uint64_t cur_func_frame_size = 0;
uint64_t cur_expr_stack_usage = 0;
static uint64_t ast_vm_stack_push(){
return vm_stackpointer++;
}
static uint64_t ast_vm_stack_push_lvar(symdecl_astexec* s){
uint64_t placement;
uint64_t qq;
if(cur_expr_stack_usage){
puts("VM error");
puts("Tried to push local variable in the middle of an expression? Huh?");
}
placement = ast_vm_stack_push();
cur_func_frame_size++;
// vm_stack[placement].vname = s->name;
vm_stack[placement].identification = VM_VARIABLE;
//vm_stack[placement].t = s->t; //NOTE: Includes lvalue information.
qq = type_getsz(s->t);
//align qq to 32...
qq = qq + 31;
qq = qq & ~(uint64_t)31;
if(s->t.arraylen){
vm_stack[placement].allocationsize = qq;
vm_stack[placement].ldata = vm_bigstack + vm_bigstackptr;
vm_bigstackptr += qq;
}
/*structs also get placement on the stack.*/
if(s->t.arraylen == 0)
if(s->t.pointerlevel == 0)
if(s->t.basetype == BASE_STRUCT){
//debug_print("Allocating a struct!",0,0);
//vm_stack[placement].ldata = calloc(type_getsz(s->t),1);
//actually...
vm_stack[placement].allocationsize = qq;
vm_stack[placement].ldata = vm_bigstack + vm_bigstackptr;
vm_bigstackptr += qq;
}
return placement;
}
uint64_t ast_vm_stack_push_temporary(){
uint64_t placement = ast_vm_stack_push();
ast_vm_stack_elem tt;
tt.identification = VM_EXPRESSION_TEMPORARY;
tt.ldata = NULL; //forbidden to have a struct or
vm_stack[placement] = tt;
cur_expr_stack_usage++;
return placement;
}
void ast_vm_stack_pop(){
if(vm_stack[vm_stackpointer-1].identification == VM_EXPRESSION_TEMPORARY) cur_expr_stack_usage--;
if(vm_stack[vm_stackpointer-1].identification == VM_VARIABLE) cur_func_frame_size--;
if(vm_stack[vm_stackpointer-1].ldata) {
vm_bigstackptr -= vm_stack[vm_stackpointer-1].allocationsize;
}
vm_stack[vm_stackpointer-1].ldata = NULL;
vm_stackpointer--;
}
static void ast_vm_stack_pop_temporaries(){
while(cur_expr_stack_usage > 0) ast_vm_stack_pop();
}
static void ast_vm_stack_pop_lvars(){
while(cur_func_frame_size > 0) ast_vm_stack_pop();
}
static void* do_ptradd(char* ptr, int64_t num, uint64_t sz){
return ptr + num * sz;
}
/*
used for EXPR_MEMBER
*/
static inline uint64_t get_offsetof(
typedecl* the_struct,
char* membername
)
{
uint64_t off = 0;
uint64_t i;
if(the_struct->is_union) return 0;
for(i = 0; i < the_struct->nmembers; i++){
if(streq(the_struct->members[i].membername, membername))
return off;
off = off + type_getsz(the_struct->members[i]);
}
}
uint64_t vm_get_offsetof(
typedecl* the_struct,
char* membername
){
return get_offsetof(the_struct, membername);
}
static uint64_t do_deref(void* ptr, unsigned sz){
/*if(sz > 8){
puts("VM Internal error");
puts("VM was asked to dereference more than 8 bytes...");
exit(1);
}*/
memcpy(
&sz,
ptr,
sz
);
return sz;
}
static void do_assignment(
void* ptr,
uint64_t val,
uint64_t sz
){
memcpy(ptr, &val, sz);
}
/*
Function to do type conversions. Converts between
any of the primitives.
*/
//TODO: speed up this function.
static uint64_t do_primitive_type_conversion(
uint64_t thing,
uint64_t srcbase, //source base.
uint64_t whatbase //target base
){
int8_t i8data;
uint8_t u8data;
int16_t i16data;
uint16_t u16data;
int32_t i32data;
uint32_t u32data;
int64_t i64data;
uint64_t u64data;
double f64data;
float f32data;
if(sizeof(double) != 8 || sizeof(float) != 4 || sizeof(void*) != 8){
puts("<Seabass platform error>");
puts("The interpreter was written with heavy assumption of the sizes of built-in types.");
puts("Double must be 64 bit, 8 bytes");
puts("Float must be 32 bit, 4 bytes");
puts("Pointers must be 8 bytes.");
puts("You can write a code generator that doesn't respect this,");
puts("But `codegen` functions must always execute in an environment that abides by these limitations.");
puts("If your goal was to get a self-hosted implementation on a 32 bit system,");
puts("You will have to re-write or seriously modify the existing code.");
exit(1);
}
if(srcbase == whatbase) return thing;
/*if(srcbase == BASE_VOID ||
whatbase == BASE_VOID ||
srcbase > BASE_F64 ||
whatbase > BASE_F64 ){
puts("VM Error:");
puts("Tried to do conversions between INVALID types!");
if(srcbase == BASE_STRUCT) puts("srcbase was BASE_STRUCT");
if(whatbase == BASE_STRUCT) puts("whatbase was BASE_STRUCT");
exit(1);
}*/
/*integers of the same size are just a reinterpret cast...*/
if(whatbase == BASE_U8 && srcbase == BASE_I8) return thing;
if(whatbase == BASE_U16 && srcbase == BASE_I16) return thing;
if(whatbase == BASE_U32 && srcbase == BASE_I32) return thing;
if(whatbase == BASE_U64 && srcbase == BASE_I64) return thing;
/*Generate all possible input interpretations.*/
i64data = thing;
u64data = thing;
memcpy(&f64data, &thing, 8);
memcpy(&f32data, &thing, 4);
memcpy(&u32data, &thing,4);
i32data = u32data;
memcpy(&u16data, &thing,2);
i16data = u16data;
memcpy(&u8data, &thing,1);
i8data = u8data;
if(whatbase < srcbase){
if(whatbase == BASE_U8 ||whatbase == BASE_I8){
if(srcbase == BASE_U16 || srcbase == BASE_I16) u8data=u16data;
if(srcbase == BASE_U32 || srcbase == BASE_I32) u8data=u32data;
if(srcbase == BASE_U64 || srcbase == BASE_I64) u8data=u64data;
if(srcbase == BASE_F32) {i8data=f32data;u8data=i8data;}
if(srcbase == BASE_F64) {i8data=f64data;u8data=i8data;}
memcpy(&thing, &u8data, 1);
return thing;
}
if(whatbase == BASE_U16 ||whatbase == BASE_I16){
if(srcbase == BASE_U32 || srcbase == BASE_I32) u16data=u32data;
if(srcbase == BASE_U64 || srcbase == BASE_I64) u16data=u64data;
if(srcbase == BASE_F32) {i16data=f32data;u16data=i16data;}
if(srcbase == BASE_F64) {i16data=f64data;u16data=i16data;}
memcpy(&thing, &u16data, 2);
return thing;
}
if(whatbase == BASE_U32 ||whatbase == BASE_I32){
if(srcbase == BASE_U64 || srcbase == BASE_I64) u32data=u64data;
if(srcbase == BASE_F32) {i32data=f32data;u32data=i32data;}
if(srcbase == BASE_F64) {i32data=f64data;u32data=i32data;}
memcpy(&thing, &u32data, 4);
return thing;
}
if(whatbase == BASE_U64 ||whatbase == BASE_I64){
if(srcbase == BASE_F32) {i64data=f32data;u64data=i64data;}
if(srcbase == BASE_F64) {i64data=f64data;u64data=i64data;}
//memcpy(&thing, &u64data, 8);
return u64data;
}
if(whatbase == BASE_F32){
if(srcbase == BASE_F64) f32data=f64data;
memcpy(&thing, &f32data, 4);
return thing;
}
//base_f64 is equal to itself f64data=f64data;
}
//Promoting up?
if(whatbase == BASE_F64){
if(srcbase == BASE_I8) f64data = i8data;
if(srcbase == BASE_U8) f64data = u8data;
if(srcbase == BASE_I16) f64data = i16data;
if(srcbase == BASE_U16) f64data = u16data;
if(srcbase == BASE_I32) f64data = i32data;
if(srcbase == BASE_U32) f64data = u32data;
if(srcbase == BASE_I64) f64data = i64data;
if(srcbase == BASE_U64) f64data = u64data;
if(srcbase == BASE_I64) f64data = i64data;
if(srcbase == BASE_U64) f64data = u64data;
if(srcbase == BASE_F32) f64data = f32data;
memcpy(&thing, &f64data, 8);
return thing;
}
if(whatbase == BASE_F32){
if(srcbase == BASE_I8) f32data = i8data;
if(srcbase == BASE_U8) f32data = u8data;
if(srcbase == BASE_I16) f32data = i16data;
if(srcbase == BASE_U16) f32data = u16data;
if(srcbase == BASE_I32) f32data = i32data;
if(srcbase == BASE_U32) f32data = u32data;
if(srcbase == BASE_I64) f32data = i64data;
if(srcbase == BASE_U64) f32data = u64data;
if(srcbase == BASE_U64) f32data = u64data;
if(srcbase == BASE_I64) f32data = i64data;
memcpy(&thing, &f32data, 4);
return thing;
}
if(whatbase == BASE_I64){
if(srcbase == BASE_I8) i64data = i8data;
if(srcbase == BASE_U8) i64data = u8data;
if(srcbase == BASE_I16) i64data = i16data;
if(srcbase == BASE_U16) i64data = u16data;
if(srcbase == BASE_I32) i64data = i32data;
if(srcbase == BASE_U32) i64data = u32data;
//memcpy(&thing, &f32data, 4);
return i64data;
}
if(whatbase == BASE_U64){
if(srcbase == BASE_I8) {i64data = i8data;u64data=i64data;}//must enforce sign extension.
if(srcbase == BASE_U8) u64data = u8data;
if(srcbase == BASE_I16) {i64data = i16data;u64data=i64data;}//must enforce sign extension.
if(srcbase == BASE_U16) u64data = u16data;
if(srcbase == BASE_I32) {i64data = i32data;u64data=i64data;}//must enforce sign extension.
if(srcbase == BASE_U32) u64data = u32data;
//memcpy(&thing, &u64data, 8);
return u64data;
}
if(whatbase == BASE_I32){
if(srcbase == BASE_U8) i32data = u8data;
if(srcbase == BASE_I8) i32data = i8data;//must enforce sign extension.
if(srcbase == BASE_U16) i32data = u16data;
if(srcbase == BASE_I16) i32data = i16data;//must enforce sign extension.
memcpy(&thing, &i32data, 4);
return thing;
}
if(whatbase == BASE_U32){
if(srcbase == BASE_U8) u32data = u8data;
if(srcbase == BASE_I8) {i32data = i8data;u32data =i32data;}//must enforce sign extension.
if(srcbase == BASE_U16) u32data = u16data;
if(srcbase == BASE_I16) {i32data = i16data;u32data =i32data;}//must enforce sign extension.
memcpy(&thing, &u32data, 4);
return thing;
}
if(whatbase == BASE_I16){
if(srcbase == BASE_U8) i16data = u8data;
if(srcbase == BASE_I8) i16data = i8data;//must enforce sign extension.
memcpy(&thing, &i16data, 2);
return thing;
}
if(whatbase == BASE_U16){
if(srcbase == BASE_U8) u16data = u8data;
if(srcbase == BASE_I8) {i16data = i8data;u16data=i16data;}//must enforce sign extension.
memcpy(&thing, &u16data, 2);
return thing;
}
/*
puts("VM internal error");
puts("Unhandled type conversion.");
if(srcbase == BASE_U8) puts("from u8");
if(srcbase == BASE_I8) puts("from i8");
if(srcbase == BASE_U16) puts("from u16");
if(srcbase == BASE_I16) puts("from i16");
if(srcbase == BASE_U32) puts("from u32");
if(srcbase == BASE_I32) puts("from i32");
if(srcbase == BASE_U64) puts("from u64");
if(srcbase == BASE_I64) puts("from i64");
if(srcbase == BASE_F32) puts("from f32");
if(srcbase == BASE_F64) puts("from f64");
if(srcbase == BASE_VOID) puts("from void");
if(srcbase >= BASE_FUNCTION) puts("from invalid");
if(whatbase == BASE_U8) puts("to u8");
if(whatbase == BASE_I8) puts("to i8");
if(whatbase == BASE_U16) puts("to u16");
if(whatbase == BASE_I16) puts("to i16");
if(whatbase == BASE_U32) puts("to u32");
if(whatbase == BASE_I32) puts("to i32");
if(whatbase == BASE_U64) puts("to u64");
if(whatbase == BASE_I64) puts("to i64");
if(whatbase == BASE_F32) puts("to f32");
if(whatbase == BASE_F64) puts("to f64");
if(whatbase == BASE_VOID) puts("to void");
if(whatbase >= BASE_FUNCTION) puts("to invalid");
exit(1);
*/
}
/*a
static float do_fmod(float a, float b){return a % b;}
static double do_dmod(double a, double b){return a % b;}
*/
/*Get a pointer to the memory!*/
static void* retrieve_variable_memory_pointer(
uint64_t symid,
int is_global
){
int64_t i;
if(is_global){
i = symid;
{
/*
if(symbol_table[i]->is_incomplete){
puts("VM Error");
puts("This global:");
puts(symbol_table[i]->name);
puts("Was incomplete at access time.");
exit(1);
}
if(symbol_table[i]->t.is_function){
puts("VM Error");
puts("This global:");
puts(symbol_table[i]->name);
puts("Was accessed as a variable, but it's a function!");
exit(1);
}
*/
/*if it has no cdata- initialize it!*/
if(symbol_table[i]->cdata == NULL){
uint64_t sz = type_getsz(symbol_table[i]->t);
// debug_print("\nHaving to allocate global storage...",0,0);
symbol_table[i]->cdata = calloc(
sz,1
);
symbol_table[i]->cdata_sz = sz;
}
return symbol_table[i]->cdata;
}
puts("VM ERROR");
puts("Could not find global");
exit(1);
}
/*Search for local variables in the vstack...*/
i = vm_stackpointer-1- cur_expr_stack_usage-symid; /**/
if(vm_stack[i].ldata)
return vm_stack[i].ldata;
return &(vm_stack[i].smalldata);
}
void do_expr(expr_node_astexec* ee){
int64_t i = 0;
int64_t n_subexpressions = 0;
/*Function calls and method calls both require saving information.*/
uint64_t saved_cur_func_frame_size = 0;
uint64_t saved_cur_expr_stack_usage = 0;
uint64_t saved_executing_function=0;
/*This one is only used for sanity checking.*/
uint64_t saved_vstack_pointer;
/*
* Evaluate all subnodes first.
* Keep track of how much the stack pointer changes from each node. It should always shift by one- we even keep track of voids.
* if this is a function call...
* We have to save the information about our current execution state. Particularly, the i
*/
/*Do the subnodes in reverse order!
The result is that the arguments are placed on the stack in the reverse order,
which is the VM's ABI.
fn myFunc(int a1, int a2, int a3):
int a4;
int a5;
end
becomes:
a5
a4
a1
a2
a3
*/
//debug_print("Entered an Expression!",0,0);
if( ee->kind == EXPR_LOGAND
){
uint64_t a;
uint64_t b;
do_expr(ee->subnodes[0]);
a = vm_stack[vm_stackpointer-1].smalldata;
if(a == 0){
//Short circuit!
vm_stack[vm_stackpointer-1].smalldata = 0;
return;
}
//else, we do this...
do_expr(ee->subnodes[1]);
b = vm_stack[vm_stackpointer-1].smalldata;
a = a&&b;
vm_stack[vm_stackpointer-2].smalldata = a;
ast_vm_stack_pop(); //we no longer need the second operand.
return;
}else if(ee->kind == EXPR_LOGOR){
uint64_t a;
uint64_t b;
do_expr(ee->subnodes[0]);
a = vm_stack[vm_stackpointer-1].smalldata;
if(a != 0){
//Short circuit!
vm_stack[vm_stackpointer-1].smalldata = 1;
return;
}
//else, we do this...
do_expr(ee->subnodes[1]);
b = vm_stack[vm_stackpointer-1].smalldata;
a = a||b;
vm_stack[vm_stackpointer-2].smalldata = a;
ast_vm_stack_pop(); //we no longer need the second operand.
return;
}else if( ee->kind == EXPR_FCALL ||
ee->kind == EXPR_METHOD ||
ee->kind == EXPR_BUILTIN_CALL ||
ee->kind == EXPR_CALLFNPTR
){
ast_vm_stack_push_temporary(); //(?) for the return value (?)
saved_vstack_pointer = vm_stackpointer; //NOTE: saved after pushing the temporary. Before subexpressions.
for(i = MAX_FARGS-1; i >= 0; i--)
{
if(ee->subnodes[i])
{
do_expr(ee->subnodes[i]);
n_subexpressions++;
}
if(i == 0) break;
}
//IMPORTANT NOTE: for expr_callfnptr, the function pointer itself is subnodes[0],
//so it ends up on top!
}else{ /*Otherwise, in-order.*/
saved_vstack_pointer = vm_stackpointer; //saved before calling sub expressions.
for(i = 0; i < MAX_FARGS; i++){
if(ee->subnodes[i]){
do_expr(ee->subnodes[i]);
n_subexpressions++;
}
if(ee->subnodes[i] == NULL)
break;
}
}
/*TODO: remove this if execution is too slow.*/
/*
if( (vm_stackpointer - saved_vstack_pointer) != (uint64_t)n_subexpressions){
puts("VM Internal Error");
puts("Expression evaluation vstack pointer check failed.");
exit(1);
}
*/
if(ee->kind == EXPR_GETFNPTR){
uint64_t general;
//push a temporary.
general = ast_vm_stack_push_temporary();
vm_stack[general].smalldata = (uint64_t) ((symbol_table + ee->symid)[0]);
return;
}
if(ee->kind == EXPR_SIZEOF ||
ee->kind == EXPR_INTLIT ||
ee->kind == EXPR_CONSTEXPR_INT
){
uint64_t general;
//push a temporary.
general = ast_vm_stack_push_temporary();
vm_stack[general].smalldata = ee->idata;
return;
}
if(ee->kind == EXPR_FLOATLIT ||
ee->kind == EXPR_CONSTEXPR_FLOAT
){
uint64_t general;
//push a temporary.
general = ast_vm_stack_push_temporary();
memcpy(&vm_stack[general].smalldata,&ee->fdata,8);
return;
}
if(ee->kind == EXPR_FCALL || ee->kind == EXPR_METHOD){
saved_cur_func_frame_size = cur_func_frame_size;
saved_cur_expr_stack_usage = cur_expr_stack_usage;
saved_executing_function= executing_function;
saved_vstack_pointer = vm_stackpointer;
ast_execute_function((symbol_table + ee->symid)[0]);
cur_func_frame_size = saved_cur_func_frame_size;
cur_expr_stack_usage = saved_cur_expr_stack_usage;
executing_function = saved_executing_function;
vm_stackpointer = saved_vstack_pointer;
//Finally ends with popping everything off.
for(i = 0; i < n_subexpressions; i++) {ast_vm_stack_pop();}
//Don't pop the last thing off because THAT'S WHERE THE RETURN VALUE IS!
//ast_vm_stack_pop();
return;
}
if(ee->kind == EXPR_CALLFNPTR){
// #ifdef SEABASS_CODEGEN_64
uint64_t retrieved_pointer;
// #endif
// #ifdef SEABASS_CODEGEN_32
// uint32_t retrieved_pointer;
// #endif
int64_t i;
//int found = 0;
// #ifdef SEABASS_CODEGEN_64
retrieved_pointer = vm_stack[vm_stackpointer-1].smalldata; //function pointer was on top.
// #else
// memcpy(
// &retrieved_pointer,
// &vm_stack[vm_stackpointer-1].smalldata,
// POINTER_SIZE
// );
// #endif
ast_vm_stack_pop();
saved_cur_func_frame_size = cur_func_frame_size;
saved_cur_expr_stack_usage = cur_expr_stack_usage;
saved_executing_function= executing_function;
saved_vstack_pointer = vm_stackpointer;
ast_execute_function((symdecl*)retrieved_pointer);
cur_func_frame_size = saved_cur_func_frame_size;
cur_expr_stack_usage = saved_cur_expr_stack_usage;
executing_function = saved_executing_function;
vm_stackpointer = saved_vstack_pointer;
for(i = 0; i < (int64_t)ee->fnptr_nargs; i++) {ast_vm_stack_pop();}
return;
}
if(ee->kind == EXPR_LSYM || ee->kind == EXPR_GSYM || ee->kind == EXPR_GETGLOBALPTR){
void* p;
uint64_t general;
if(ee->kind == EXPR_LSYM)
p = retrieve_variable_memory_pointer(ee->symid, 0);
if(ee->kind == EXPR_GSYM || ee->kind == EXPR_GETGLOBALPTR)
p = retrieve_variable_memory_pointer(ee->symid, 1);
general = ast_vm_stack_push_temporary();
memcpy(
&vm_stack[general].smalldata,
&p,
POINTER_SIZE
);
return;
}
if(ee->kind == EXPR_STRINGLIT){
void* p;
uint64_t general;
//p = symbol_table[ee->symid]->cdata;
p = ee->symname; //new convention.
//debug_print("Found String Literal. Address:",(uint64_t)p,0);
general = ast_vm_stack_push_temporary();
memcpy(&vm_stack[general].smalldata,&p,POINTER_SIZE);
return;
}
if(ee->kind == EXPR_MEMBER){ /*the actual operation is to get a pointer to the member.*/
void* p;
uint64_t pt;
uint64_t levels_of_indirection;
pt = vm_stack[vm_stackpointer-1].smalldata;
levels_of_indirection = ee->subnodes[0]->t.pointerlevel;
//if(ee->subnodes[0]->t.is_lvalue != 0) levels_of_indirection++;
/*
You can access the members of a struct with arbitrary levels of indirection.
and if it is more than one, then we need to dereference a pointer-to-pointer.
*/
// debug_print("Pointer to struct was (before):", (uint64_t)pt,0);
while(levels_of_indirection > 1){
memcpy(&p, &pt, POINTER_SIZE);
memcpy(
&pt,
p,
POINTER_SIZE
);
levels_of_indirection--;
//debug_print("Pointer to struct was (iter):", (uint64_t)pt,0);
}
// debug_print("Pointer to struct was (after):", (uint64_t)pt,0);
/*
pt holds single-level pointer-to-struct.
Now, we need to add the offset of the member...
*/
{
uint64_t off_of;
/*off_of = get_offsetof(
type_table + ee->subnodes[0]->t.structid,
ee->symname
);*/
off_of = ee->idata;
//debug_print("Accessing member, off_of was...", off_of,0);
pt = pt + off_of;
}
// debug_print("<EXPR_MEMBER> here's the final address:", (uint64_t)pt,0);
// if(((int*)pt)[0] == 3){
// debug_print("<Overhead> I can see it... It's here:", pt,0);
// }
vm_stack[vm_stackpointer-1].smalldata = pt;
//vm_stack[vm_stackpointer-1].t = ee->t;
return;
}
if(ee->kind == EXPR_MEMBERPTR){ /*the actual operation is to get a pointer to the member.*/
void* p;
uint64_t pt;
uint64_t levels_of_indirection;
pt = vm_stack[vm_stackpointer-1].smalldata;
levels_of_indirection = ee->subnodes[0]->t.pointerlevel;
//if(ee->subnodes[0]->t.is_lvalue != 0) levels_of_indirection++;
/*
You can access the members of a struct with arbitrary levels of indirection.
and if it is more than one, then we need to dereference a pointer-to-pointer.
*/
// debug_print("Pointer to struct was (before):", (uint64_t)pt,0);
while(levels_of_indirection > 1){
memcpy(&p, &pt, POINTER_SIZE);
memcpy(
&pt,
p,
POINTER_SIZE
);
levels_of_indirection--;
//debug_print("Pointer to struct was (iter):", (uint64_t)pt,0);
}
// debug_print("Pointer to struct was (after):", (uint64_t)pt,0);
/*
pt holds single-level pointer-to-struct.
Now, we need to add the offset of the member...
*/
{
uint64_t off_of;
/*off_of = get_offsetof(
type_table + ee->subnodes[0]->t.structid,
ee->symname
);*/
off_of = ee->idata;
//debug_print("Accessing member, off_of was...", off_of,0);
pt = pt + off_of;
}
// debug_print("<EXPR_MEMBER> here's the final address:", (uint64_t)pt,0);
// if(((int*)pt)[0] == 3){
// debug_print("<Overhead> I can see it... It's here:", pt,0);
// }
vm_stack[vm_stackpointer-1].smalldata = pt;
//vm_stack[vm_stackpointer-1].t = ee->t;
return;
}
if(ee->kind == EXPR_MOVE){
//POINTER_SIZE
void* p1;
void* p2;
type t;
memcpy(&p1, &vm_stack[vm_stackpointer-2].smalldata, POINTER_SIZE);
memcpy(&p2, &vm_stack[vm_stackpointer-1].smalldata, POINTER_SIZE);
t = ee->t;
if(0){
if(ee->subnodes[1]->constint_propagator){
//TODO: expr_move memset?
}}
t.pointerlevel--; //it's actually one less.
memcpy(p1, p2, type_getsz(t));
ast_vm_stack_pop(); //no longer need the second operand.
//
vm_stack[vm_stackpointer-1].smalldata = (uint64_t)p2;
//vm_stack[vm_stackpointer-1].t = ee->t;
return;
}
if(ee->kind == EXPR_ASSIGN){
void* p1;
uint64_t val;
//it's an lvalue... therefore, a pointer
val = vm_stack[vm_stackpointer-2].smalldata;
p1 = (void*)val;
/*
memcpy(
&p1,
&vm_stack[vm_stackpointer-2].smalldata,
POINTER_SIZE
);*/
//grab some number of bytes...
//debug_print("@Assignment! Pointer is:",(uint64_t)p1,0);
//If it's a pointer, we copy 8 bytes.
if(ee->subnodes[0]->t.pointerlevel > 0){
/*memcpy(
&val,
&vm_stack[vm_stackpointer-1].smalldata,
POINTER_SIZE
);*/
val = vm_stack[vm_stackpointer-1].smalldata;
memcpy(p1, &val, POINTER_SIZE);
//debug_print("<DEBUG ASSIGNMENT> Val was:",val,0);
goto end_expr_assign;
}
if(
ee->subnodes[0]->t.basetype == BASE_I64 ||
ee->subnodes[0]->t.basetype == BASE_F64 ||
ee->subnodes[0]->t.basetype == BASE_U64
)
{
/*
memcpy(
&val,
&vm_stack[vm_stackpointer-1].smalldata,
8
);
*/
val = vm_stack[vm_stackpointer-1].smalldata;
memcpy(p1, &val, 8);
goto end_expr_assign;
}
if(
ee->subnodes[0]->t.basetype == BASE_I32 ||
ee->subnodes[0]->t.basetype == BASE_F32 ||
ee->subnodes[0]->t.basetype == BASE_U32
)
{
memcpy(
&val,
&vm_stack[vm_stackpointer-1].smalldata,
4
);
memcpy(p1, &val, 4);
goto end_expr_assign;
}
if(
ee->subnodes[0]->t.basetype == BASE_I16 ||
ee->subnodes[0]->t.basetype == BASE_U16
)
{
memcpy(
&val,
&vm_stack[vm_stackpointer-1].smalldata,
2
);
memcpy(p1, &val, 2);
goto end_expr_assign;
}
if(
ee->subnodes[0]->t.basetype == BASE_I8 ||
ee->subnodes[0]->t.basetype == BASE_U8
)
{
memcpy(
&val,
&vm_stack[vm_stackpointer-1].smalldata,
1
);
memcpy(p1, &val, 1);
goto end_expr_assign;
}
/*
puts("VM internal error");
puts("Unhandled assignment type.");
exit(1);
*/
end_expr_assign:;
ast_vm_stack_pop(); //no longer need the second operand.
//assignment now yields a void...
vm_stack[vm_stackpointer-1].smalldata = 0;
//vm_stack[vm_stackpointer-1].t = type_init();
return;
}
if(
ee->kind == EXPR_PRE_INCR ||
ee->kind == EXPR_POST_INCR ||
ee->kind == EXPR_PRE_DECR ||
ee->kind == EXPR_POST_DECR
){
void* p1;
uint64_t val;
uint64_t valpre;
int32_t i32data;
int16_t i16data;
int8_t i8data;
float f32data;
double f64data;
int is_incr;
int is_pre;
is_incr=
(
ee->kind == EXPR_PRE_INCR ||
ee->kind == EXPR_POST_INCR
);
is_pre =
(
ee->kind == EXPR_PRE_INCR ||
ee->kind == EXPR_PRE_DECR
);
//get lvalue's address.
memcpy(
&p1,
&vm_stack[vm_stackpointer-1].smalldata,
POINTER_SIZE
);
//incrementing a pointer.
// #ifdef SEABASS_CODEGEN_64
if(ee->t.pointerlevel > 0){
uint64_t how_much_to_change;
//grab the actual value at that address...
memcpy(&val, p1, 8); valpre = val;
//type we are pointing to...
type t2;
t2 = ee->t;
t2.pointerlevel--;
//we shift by that size
how_much_to_change = type_getsz(t2);
//shift
if(is_incr)
val = val + how_much_to_change;
if(!is_incr)
val = val - how_much_to_change;
memcpy(p1, &val, 8);
goto end_expr_pre_incr;
}
// #endif
// #ifdef SEABASS_CODEGEN_32
// if(ee->t.pointerlevel > 0){
//
// /*
// memcpy(&val, p1, 4); valpre = val;
// memcpy(&i32data, &val, 4);
// if(is_incr) i32data++;
// if(!is_incr) i32data--;
// memcpy(p1, &i32data, 4);
// goto end_expr_pre_incr;
// */
// uint64_t how_much_to_change;
// //grab the actual value at that address...
// memcpy(&val, p1, 4); valpre = val;
// memcpy(&i32data, &val, 4);
// //type we are pointing to...
// type t2;
// t2 = ee->t;
// t2.pointerlevel--;
//
// how_much_to_change = type_getsz(t2);
//
// if(is_incr)
// i32data = i32data + how_much_to_change;
// if(!is_incr)
// i32data = i32data - how_much_to_change;
//
// memcpy(p1, &i32data, 4);
// goto end_expr_pre_incr;
// }
// #endif
if(
ee->t.basetype == BASE_I64 ||
ee->t.basetype == BASE_U64
)
{
memcpy(&val, p1, 8); valpre = val;
//debug_print("Valpre:", valpre,0);