-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcode.c
1119 lines (961 loc) · 25.9 KB
/
tcode.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 "tcode.h"
#define TABLE_SIZE 1024
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
extern quad* quads;
extern unsigned int total;
/* Total elements in each specific Table */
unsigned int totalNumConsts = 0;
unsigned int totalStringConsts = 0;
unsigned int totalUserFuncs = 0;
unsigned int totalNamedLibfuncs = 0;
/* Instruction table etc. */
struct instruction* instructions = (struct instruction*) 0;
unsigned int totalInstructions = 0;
unsigned int currInstruction = 0;
unsigned int currprocessedquads;
unsigned int userfunc_table_size = 0;
/* Initing incomplete jumps list */
struct incomplete_jump* ij_head = (struct incomplete_jump*) 0;
unsigned int ij_total = 0;
/* Userfunc Stack */
struct userfunc* funcstack=NULL;
int stack_size = 0;
int stack_top =0;
func_start* fstart = (func_start *)0;
func_end* fend = (func_end*)0;
unsigned int totalVars = 0;
unsigned int nextinstructionlabel(){
return currInstruction;
}
void reset_operand(struct vmarg *arg)
{
arg=NULL;
}
typedef void (*generator_func_t)(quad*);
generator_func_t generators[] = {
generate_ASSIGN,
generate_ADD,
generate_SUB,
generate_MUL,
generate_DIV,
generate_MOD,
generate_NOP,
generate_AND,
generate_OR,
generate_NOT,
generate_IF_EQ,
generate_IF_NOTEQ,
generate_IF_LESSEQ,
generate_IF_GREATEREQ,
generate_IF_LESS,
generate_IF_GREATER,
generate_CALL,
generate_PARAM,
generate_RETURN,
generate_GETRETVAL,
generate_FUNCSTART,
generate_FUNCEND,
generate_NEWTABLE,
generate_TABLEGETELEM,
generate_JUMP,
generate_TABLESETELEM
};
void expand_instructions(){
assert(totalInstructions == currInstruction);
struct instruction* t = NULL;
t = (struct instruction*)malloc(NEW_SIZE);
if(instructions){
memcpy(t,instructions,CURR_SIZE);
free(instructions);
}
instructions = t;
totalInstructions += EXPAND_SIZE;
}
void emit_instruction(struct instruction* t){
if(currInstruction == totalInstructions)
expand_instructions();
struct instruction* inst = instructions+currInstruction++;
inst->opcode = t->opcode;
inst->result = t->result;
inst->arg1 = t->arg1;
inst->arg2 = t->arg2;
inst->srcLine = t->srcLine;
}
void make_operand(expr* e,struct vmarg* arg){
/*if(!e)
{
arg->type = nil_a;
return;
}*/
// printf("name:%s and type:%d\n",e->name);
switch(e->type){
case var_e:{
arg->val = e->sym->offset;
newvar(e->sym->offset,e->sym->name);
switch(e->sym->space){
case programvar: arg->type = global_a; break;
case functionlocal: arg->type = local_a; break;
case formalarg: arg->type = formal_a; break;
default: assert(0);
}
break; /*n from case newtable_e */
}
case tableitem_e:{
arg->val = e->sym->offset;
switch(e->sym->space){
case programvar: arg->type = global_a; break;
case functionlocal: arg->type = local_a; break;
case formalarg: arg->type = formal_a; break;
default: assert(0);
}
break; /*n from case newtable_e */
}
case boolexpr_e:{
arg->val = e->sym->offset;
newvar(e->sym->offset,e->sym->name);
switch(e->sym->space){
case programvar: arg->type = global_a; break;
case functionlocal: arg->type = local_a; break;
case formalarg: arg->type = formal_a; break;
default: assert(0);
}
break; /*n from case newtable_e */
}
case assignexpr_e:{
arg->val = e->sym->offset;
newvar(e->sym->offset,e->sym->name);
switch(e->sym->space){
case programvar: arg->type = global_a; break;
case functionlocal: arg->type = local_a; break;
case formalarg: arg->type = formal_a; break;
default: assert(0);
}
break; /*n from case newtable_e */
}
case newtable_e:{
arg->val = e->sym->offset;
switch(e->sym->space){
case programvar: arg->type = global_a; break;
case functionlocal: arg->type = local_a; break;
case formalarg: arg->type = formal_a; break;
default: assert(0);
}
break; /*n from case newtable_e */
}
/* Constants */
case constbool_e:{
unsigned int temp = (unsigned int)e->boolConst;
arg->val =e->boolConst;
arg->type = bool_a; break;
}
case conststring_e:{
arg->val = consts_newstring(e->strConst);
arg->type = string_a; break;
}
case constnum_e:{
arg->val = consts_newnumber(e->numConst);
arg->type = number_a; break;
}
case nil_e: arg->type = nil_a; break;
/* Functions */
case programfunc_e:{
// printf("in tcode address is:%d\n",e->sym->taddress);
arg->val = userfuncs_newfunc(e->sym);
arg->type = userfunc_a;
break;
}
case libraryfunc_e:{
arg->type = libfunc_a;
arg->val = libfuncs_newused(e->sym->name);
break;
}
default: assert(0);
}
}
void newvar(unsigned int offset, char* name){
if(!varhead){
varhead = (struct var_table*)malloc(sizeof(struct var_table));
varhead->offset = offset;
varhead->var_name = name;
varhead->next = NULL;
}
else{
struct var_table* temp=NULL;
temp = varhead;
while(temp->next){
temp = temp->next;
}
struct var_table* newnode = (struct var_table*)malloc(sizeof(struct var_table));
newnode->offset = offset;
newnode->var_name = name;
newnode->next = NULL;
temp->next = newnode;
}
totalVars++;
}
unsigned int consts_newstring(char* s){
assert(totalStringConsts < TABLE_SIZE);
unsigned int index;
assert(s);
for(index=0; index<totalStringConsts; index++){
if(strcmp(*(stringConsts+index),s)==0)
return index;
}
stringConsts[totalStringConsts++] = s;
return index;
}
unsigned int consts_newnumber(double n){
assert(totalNumConsts < TABLE_SIZE);
unsigned int index;
for(index=0; index < totalNumConsts; index++){
if(numConsts[index] == n)
return index;
}
numConsts[totalNumConsts] = n;
return totalNumConsts++;
}
unsigned int userfuncs_newfunc(struct symbol* sym){
if(totalUserFuncs == userfunc_table_size){
struct userfunc* temp = NULL;
temp = (struct userfunc*)malloc(sizeof(struct userfunc)*50+userfunc_table_size);
if(userFuncs){
memcpy(temp, userFuncs, userfunc_table_size);
free(userFuncs);
}
userFuncs = temp;
userfunc_table_size += 50;
}
unsigned int index;
for(index =0; index<totalUserFuncs; index++){
if(sym->taddress == (userFuncs+index)->address){
return index;
}
}
(userFuncs+totalUserFuncs)->address = sym->taddress;
(userFuncs+totalUserFuncs)->id = sym->name;
(userFuncs+totalUserFuncs)->localSize = sym->totallocals;
return totalUserFuncs;
}
unsigned int libfuncs_newused(char* s){
assert(totalNamedLibfuncs < TABLE_SIZE);
unsigned int index;
for(index=0; index < totalNamedLibfuncs;index++){
if(strcmp(*(namedLibfuncs+index),s)==0)
return index;
}
namedLibfuncs[totalNamedLibfuncs] = s;
return totalNamedLibfuncs++;
}
/*
Helper functions to produce common arguments for generated instructions, like 1, 0, "true", "false"
and funciton return values.
*/
void make_numberoperand(struct vmarg* arg, double val){
arg->val = consts_newnumber(val);
arg->type = number_a;
}
void make_booloperand(struct vmarg* arg, unsigned char val){
arg->val = (unsigned int)val;
arg->type = bool_a;
}
void make_retvaloperand(struct vmarg* arg){
arg->type = retval_a;
}
void generate_instr(enum vmopcode op,quad* quad){
struct instruction t;
t.opcode = op;
if(op==23){
printf("element:%lf,type:%d\n",quad->arg1->numConst,quad->arg1->type );
}
make_operand(quad->arg1, &(t.arg1));
make_operand(quad->arg2, &(t.arg2));
make_operand(quad->result, &(t.result));
quad->taddress = nextinstructionlabel();
emit_instruction(&t);
}
void generate(void){
unsigned int i;
for(i=0;i<nextquadlabel();i++){
(*generators[quads[i].op])(quads+i);
}
patch_incomplete_jumps(nextquadlabel(), currInstruction);
}
void generate_ADD (quad* quad) { generate_instr(add_v, quad); }
void generate_SUB (quad* quad) { generate_instr(sub_v, quad); }
void generate_MUL (quad* quad) { generate_instr(mul_v, quad); }
void generate_DIV (quad* quad) { generate_instr(div_v, quad); }
void generate_MOD (quad* quad) { generate_instr(mod_v, quad); }
void generate_NEWTABLE (quad* quad) { generate_instr(newtable_v, quad); }
void generate_TABLEGETELEM (quad* quad) { generate_instr(tablegetelem_v, quad); }
void generate_TABLESETELEM (quad* quad) { generate_instr(tablesetelem_v, quad); }
void generate_ASSIGN (quad* quad) { generate_instr(assign_v, quad); }
void generate_NOP () { struct instruction t; t.opcode=nop_v; emit_instruction(&t); }
void generate_JUMP (quad* quad) { generate_relational(jump_v, quad); }
void generate_IF_EQ (quad* quad) { generate_relational(jeq_v, quad); }
void generate_IF_NOTEQ(quad* quad) { generate_relational(jne_v, quad); }
void generate_IF_GREATER (quad* quad) { generate_relational(jgt_v, quad); }
void generate_IF_GREATEREQ(quad* quad) { generate_relational(jge_v, quad); }
void generate_IF_LESS (quad* quad) { generate_relational(jlt_v, quad); }
void generate_IF_LESSEQ (quad* quad) { generate_relational(jle_v, quad); }
void generate_relational (int op,quad* quad) {
struct instruction t;
t.opcode = op;
// printf("arg1:%d,arg2:%d\n", quad->arg1->boolConst,quad->arg2->boolConst);
make_operand(quad->arg1, &t.arg1);
make_operand(quad->arg2, &t.arg2);
t.result.type = label_a;
if (quad->label < currprocessedquad())
t.result.val = (quads+quad->label)->taddress;
else
add_incomplete_jump(nextinstructionlabel(), quad->label);
quad->taddress = nextinstructionlabel();
emit_instruction(&t);
}
unsigned int currprocessedquad(){
return currprocessedquads;
}
void generate_AND (quad *quad) {
struct instruction t;
quad->taddress = nextinstructionlabel();
t.opcode = jeq_v;
make_operand(quad->arg1, &t.arg1);
make_booloperand(&t.arg2, '1');
t.result.type = label_a;
t.result.val = nextinstructionlabel()+4;
emit_instruction(&t);
make_operand(quad->arg2, &t.arg1);
t.result.val = nextinstructionlabel()+3;
emit_instruction(&t);
t.opcode = assign_v;
make_booloperand(&t.arg1, '0');
reset_operand(&t.arg2);
make_operand(quad->result, &t.result);
emit_instruction(&t);
t.opcode = jump_v;
reset_operand (&t.arg1);
reset_operand(&t.arg2);
t.result.type = label_a;
t.result.val = nextinstructionlabel()+2;
emit_instruction(&t);
t.opcode = assign_v;
make_booloperand(&t.arg1, '1');
reset_operand(&t.arg2);
make_operand(quad->result, &t.result);
emit_instruction(&t);
}
void generate_NOT (quad* quad) {
quad->taddress = nextinstructionlabel();
struct instruction t;
t.opcode = jeq_v;
make_operand(quad->arg1, &t.arg1);
make_booloperand(&t.arg2, '0');
t.result.type = label_a;
t.result.val = nextinstructionlabel()+3;
emit_instruction(&t);
t.opcode = assign_v;
make_booloperand(&t.arg1, '0');
reset_operand(&t.arg2);
make_operand(quad->result, &t.result);
emit_instruction(&t);
t.opcode = jump_v;
reset_operand (&t.arg1);
reset_operand(&t.arg2);
t.result.type = label_a;
t.result.val = nextinstructionlabel()+2;
emit_instruction(&t);
t.opcode = assign_v;
make_booloperand(&t.arg1, '1');
reset_operand(&t.arg2);
make_operand(quad->result, &t.result);
emit_instruction(&t);
}
void generate_OR (quad* quad) {
quad->taddress = nextinstructionlabel();
struct instruction t;
t.opcode = jeq_v;
make_operand(quad->arg1, &t.arg1);
make_booloperand(&t.arg2, '1');
t.result.type = label_a;
t.result.val = nextinstructionlabel()+4;
emit_instruction(&t);
make_operand(quad->arg2, &t.arg1);
t.result.val = nextinstructionlabel()+3;
emit_instruction(&t);
t.opcode = assign_v;
make_booloperand(&t.arg1, '0');
reset_operand(&t.arg2);
make_operand(quad->result, &t.result);
emit_instruction(&t);
t.opcode = jump_v;
reset_operand (&t.arg1);
reset_operand(&t.arg2);
t.result.type = label_a;
t.result.val = nextinstructionlabel()+2;
emit_instruction(&t);
t.opcode = assign_v;
make_booloperand(&t.arg1, '1');
reset_operand(&t.arg2);
make_operand(quad->result, &t.result);
emit_instruction(&t);
}
void generate_PARAM(quad* quad) {
quad->taddress = nextinstructionlabel();
struct instruction t;
t.arg1.type = nil_a;
t.arg2.type = nil_a;
t.opcode = pusharg_v;
make_operand(quad->result, &t.result);
emit_instruction(&t);
}
void generate_CALL(quad* quad) {
quad->taddress = nextinstructionlabel();
struct instruction t;
t.opcode = call_v;
t.arg1.type = nil_a;
t.arg2.type = nil_a;
make_operand(quad->result, &t.result);
emit_instruction(&t);
}
void generate_GETRETVAL(quad* quad) {
quad->taddress = nextinstructionlabel();
struct instruction t;
t.opcode = assign_v;
make_operand(quad->result, &t.result);
make_retvaloperand(&t.arg1);
emit_instruction(&t);
}
void generate_FUNCSTART(quad* quad){
struct instruction t0;
t0.arg1.type = nil_a;
t0.arg2.type = nil_a;
t0.result.type= label_a;
t0.result.val = 0;
t0.opcode = jump_v;
add_func_jump(nextinstructionlabel());
emit_instruction(&t0);
struct userfunc* f;
f = (struct userfunc*)malloc(sizeof(struct userfunc));
f->id=quad->result->sym->name;
f->address = nextinstructionlabel();
quad->taddress = nextinstructionlabel();
userfuncs_newfunc(quad->result->sym); //added 6/5 ore
(userFuncs+totalUserFuncs)->address = f->address;
(userFuncs+totalUserFuncs)->id = f->id;
(userFuncs+totalUserFuncs)->localSize = quad->result->sym->totallocals;
f->index = totalUserFuncs-1;
funcpush(f);
struct instruction t;
totalUserFuncs++;
t.arg1.type = nil_a;
t.arg2.type = nil_a;
t.opcode = funcenter_v;
make_operand(quad->result, &t.result);
t.result.val = totalUserFuncs-1; //EDW
emit_instruction(&t);
}
void generate_RETURN(quad* quad){
quad->taddress = nextinstructionlabel();
struct instruction t;
t.opcode = assign_v;
make_retvaloperand(&t.result);
t.arg2.type=nil_a;
make_operand(quad->arg1, &t.arg1);
emit_instruction(&t);
struct userfunc* f;
f = functop(funcstack);
f->returnList = (struct userfunc*)malloc(sizeof(struct userfunc));
append(f->returnList, nextinstructionlabel());
t.opcode = jump_v;
reset_operand(&t.arg1);
t.arg1.type = nil_a;
reset_operand(&t.arg2);
t.result.type = label_a;
//printf("%d LABEL_A\n",f->returnList->address);
emit_instruction(&t);
}
void generate_FUNCEND(quad* quad){
struct userfunc* f;
f=funcpop(funcstack);
patch_returns(f->returnList, nextinstructionlabel());
quad->taddress = nextinstructionlabel();
struct instruction t;
t.opcode = funcexit_v;
t.arg1.type = nil_a;
t.arg2.type = nil_a;
make_operand(quad->result, &t.result);
t.result.val = f->index;
patch_func_jump(quad->taddress);
emit_instruction(&t);
}
void append(struct userfunc* head, unsigned int instNo){
if(!head){
head = (struct userfunc*)malloc(sizeof(struct userfunc));
head->address = instNo;
}
else{
struct userfunc* temp = head;
while(temp->returnList){
temp = temp->returnList;
}
temp->returnList = (struct userfunc*)malloc(sizeof(struct userfunc));
temp->returnList->address = instNo;
}
}
void patch_returns(struct userfunc* head, unsigned int instrNo){
while(head){
if((head->address)>0){
(instructions+(head->address))->result.val = instrNo;
}
head = head->returnList;
}
}
void add_func_jump(unsigned int instrNo){
func_start* temp;
temp = fstart;
if(!temp){
fstart = (struct func_start*)malloc(sizeof(struct func_start));
fstart->instrNo = instrNo;
fstart->next = NULL;
temp = fstart;
}
else{
while(temp->next){
temp = temp->next;
}
temp->next = (struct func_start*)malloc(sizeof(struct func_start));
temp = temp->next;
temp->instrNo = instrNo;
printf("insinf:%d\n",instrNo);
}
}
void patch_func_jump(unsigned int i){
func_start* temp;
temp = fstart;
while(temp->next){
temp = temp->next;
}
(instructions+temp->instrNo)->result.val = i+1;
free(temp);
}
void add_incomplete_jump(unsigned instrNo, unsigned iaddress){
struct incomplete_jump* temp;
temp = ij_head;
if(!temp){
ij_head = (struct incomplete_jump*)malloc(sizeof(struct incomplete_jump));
ij_head->instrNo = instrNo;
ij_head->iaddress = iaddress;
ij_head->next = NULL;
temp = ij_head;
}
else{
while(temp->next){
temp = temp->next;
}
temp->next = (struct incomplete_jump*)malloc(sizeof(struct incomplete_jump));
temp = temp->next;
temp->instrNo = instrNo;
temp->iaddress = iaddress;
}
}
void patch_incomplete_jumps(unsigned int intermediate_code_size,unsigned int target_code_size){ /* Check code place */
if(ij_head){
while(ij_head){
if (ij_head->iaddress == intermediate_code_size){
(instructions+(ij_head->instrNo))->result.val = target_code_size;
}
else
(instructions+(ij_head->instrNo))->result.val = (quads+ij_head->iaddress)->taddress;
ij_head = ij_head->next;
}
}
}
void funcpush(struct userfunc* f){
assert(stack_size>=stack_top);
if(stack_size == stack_top){
struct userfunc* temp = NULL;
temp = (struct userfunc*)malloc(sizeof(struct userfunc)*50+(sizeof(funcstack)));
if(funcstack){
memcpy(temp,funcstack, stack_size);
free(funcstack);
}
funcstack = temp;
stack_size += 50;
}
(funcstack+stack_top)->address = f->address;
(funcstack+stack_top)->id = f->id;
(funcstack+stack_top)->localSize = f->localSize;
(funcstack+stack_top)->index = f->index+1;
(funcstack+stack_top)->returnList = NULL;
stack_top++;
}
struct userfunc* funcpop(){
assert(stack_top);
stack_top--;
return (funcstack+stack_top);
}
struct userfunc* functop(){
assert(stack_top);
return (funcstack+(stack_top-1));
}
/*<---------------------------------------- Print code start here-------------------------------------->*/
void printVar(unsigned int offset){
struct var_table* temp = NULL;
temp = varhead;
if(temp){
// printf("\n\n\n\n\n");
while(temp){
// printf("%s(%d)\t\n",temp->var_name,offset);
if(temp->offset == offset){
printf("%s(%d)\t",temp->var_name,offset);
break;
}
temp = temp->next;
}
}
}
void printTables(){
printNumTable();
printStringTable();
printUserfuncTable();
printLibfuncTable();
}
void printTables_tofile(){
unsigned int magicNumber=23522275;
FILE* fp = fopen("target.txt","w+");
fprintf(fp,"%d\n",magicNumber);
printStringTable_tofile(fp);
printNumTable_tofile(fp);
printUserfuncTable_tofile(fp);
printLibfuncTable_tofile(fp);
printInstructions_tofile(fp);
fclose(fp);
}
void printStringTable_tofile(FILE* fp){
int i=0;
fprintf(fp,"Strings\t%d\n",totalStringConsts );
for(i=0;i<totalStringConsts;i++){
fprintf(fp,"%s\n",stringConsts[i]);
}
}
void printNumTable_tofile(FILE* fp){
int i=0;
fprintf(fp,"Number_Constants %d\n",totalNumConsts);
double z;
int y;
double x;
for(i=0;i<totalNumConsts;i++){
x = numConsts[i];
y = (int)x;
z = (x-y);
if(z == 0)
fprintf(fp,"%.f\t\n",x);
else
fprintf(fp,"%.3f\t\n",x);
}
}
void printUserfuncTable_tofile(FILE* fp){
fprintf(fp,"User_Functions %d\n",totalUserFuncs);
int index;
for(index=0; index<totalUserFuncs; index++)
{
fprintf(fp,"%d\t%d\t%s\n",userFuncs[index].address,userFuncs[index].localSize,userFuncs[index].id);
}
}
void printLibfuncTable_tofile(FILE* fp){
fprintf(fp,"Library_Functions %d\n",totalNamedLibfuncs);
unsigned int index,strsize;
for(index=0; index<totalNamedLibfuncs; index++)
{
fprintf(fp,"%s\n",namedLibfuncs[index]);
}
}
void printInstructions_tofile(FILE* fp){
struct instruction* temp = NULL;
temp = instructions;
unsigned int i=0;
fprintf(fp,"total_instructions %d\n",currInstruction+1);
printf("total instr %d\n",currInstruction);
while(temp <= (instructions+nextinstructionlabel()-1)){
printInstrOp_tofile(temp->opcode,fp);
if(&temp->arg1){
printVMarg_tofile(&temp->arg1,fp);
}
if(&temp->arg2){
printVMarg_tofile(&temp->arg2,fp);
}
if(&temp->result){
printVMarg_tofile(&temp->result,fp);
}
i++;
temp = instructions+i;
fprintf(fp,"\n");
}
}
void printVMarg_tofile(struct vmarg* arg,FILE* fp){
switch(arg->type){
case label_a:
fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // label_a
case global_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // global_a
case formal_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // formal_a
case local_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // local_a
case number_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // number_a
case string_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // string_a
case bool_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // bool_a
case nil_a: fprintf(fp,"%d\t\t\t",arg->type);
break; //nil_a
case userfunc_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // userfunc_a
case libfunc_a: fprintf(fp,"%d,%d\t\t\t",arg->type,arg->val);
break; // libfunc_a
case retval_a: fprintf(fp,"%d\t\t\t",arg->type);
break; // retval_a
default: ;//printf("Asserting for arg->val=%d\n",arg->val);assert(0);
}
}
void printInstrOp_tofile(enum vmopcode op,FILE* fp){
switch(op){
case assign_v:
fprintf(fp,"%d\t\t",op);
break;
case add_v:
fprintf(fp,"%d\t\t",op);
break;
case sub_v:
fprintf(fp,"%d\t\t",op);
break;
case mul_v:
fprintf(fp,"%d\t\t",op);
break;
case div_v:
fprintf(fp,"%d\t\t",op);
break;
case mod_v:
fprintf(fp,"%d\t\t",op);
break;
case uminus_v:
fprintf(fp,"%d\t\t",op);
break;
case and_v:
fprintf(fp,"%d\t\t",op);
break;
case or_v:
fprintf(fp,"%d\t\t",op);
break;
case not_v:
fprintf(fp,"%d\t\t",op);
break;
case jump_v:
fprintf(fp,"%d\t\t",op);
break;
case jeq_v:
fprintf(fp,"%d\t\t",op);
break;
case jne_v:
fprintf(fp,"%d\t\t",op);
break;
case jle_v:
fprintf(fp,"%d\t\t",op);
break;
case jge_v:
fprintf(fp,"%d\t\t",op);
break;
case jlt_v:
fprintf(fp,"%d\t\t",op);
break;
case jgt_v:
fprintf(fp,"%d\t\t",op);
break;
case call_v:
fprintf(fp,"%d\t\t",op);
break;
case pusharg_v:
fprintf(fp,"%d\t\t",op);
break;
case funcenter_v:
fprintf(fp,"%d\t\t",op);
break;
case funcexit_v:
fprintf(fp,"%d\t\t",op);
break;
case newtable_v:
fprintf(fp,"%d\t\t",op);
break;
case tablegetelem_v:
fprintf(fp,"%d\t\t",op);
break;
case tablesetelem_v:
fprintf(fp,"%d\t\t",op);
break;
case nop_v:
fprintf(fp,"%d\t\t",op);
break;
}
}
void printInstructions(){
struct instruction* temp = NULL;
temp = instructions;
unsigned int i=0;
printf("\n^^^^^^^^^^^^^ Start printing Instruction Table ^^^^^^^^^^^^^\n");
while(temp <= (instructions+nextinstructionlabel()-1)){
printf("%d:",i);
printInstrOp(temp->opcode);
if(&temp->arg1){
printVMarg(&temp->arg1);
}
if(&temp->arg2){
printVMarg(&temp->arg2);
}
if(&temp->result){
printVMarg(&temp->result);
}
i++;
temp = instructions+i;
printf("\n");
}
}
void printVMarg(struct vmarg* arg){
switch(arg->type){
case label_a: printf("00, %d\t",arg->val);break; // label_a
case global_a: printf("01, %d:\t",arg->val);printVar(arg->val);break; //global_a
case formal_a: printf("02, %d:\t",arg->val);printVar(arg->val);break; //formal_a
case local_a: printf("03, %d:\t",arg->val);printVar(arg->val);break; // local_a
case number_a: printf("04, %d:\t",arg->val);printDouble(numConsts[arg->val]);break; // number_a
case string_a: printf("05, %d:%s\t",arg->val, stringConsts[arg->val]);break; // string_a
case bool_a: printf("06, %c\t",arg->val);break; // bool_a
case nil_a: /*printf("\t(nil_a)\t");*/break; //nil_a
case userfunc_a: printf("08, %d: %s\t",arg->val, userFuncs[arg->val].id);break; // userfunc_a
case libfunc_a: printf("09, %d: %s\t",arg->val, namedLibfuncs[arg->val]);break; // libfunc_a
case retval_a: printf("10, retval\t");break; // retval_a
default: ;//printf("Asserting for arg->val=%d\n",arg->val);assert(0);
}
}
void printDouble(double x){
double z;
int y;
y = (int)x;
z = (x-y);
if(z == 0)