-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegest.cc
executable file
·788 lines (728 loc) · 23.4 KB
/
codegest.cc
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
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <cstdlib>
using namespace std;
#include "util.hh"
#include "codegest.hh"
int securemode=0;
map<string,string>numops;
// Local functions:
bool isint(string s);
bool isvar(string s);
int checkcodopcorrect(string s);
void initnumops();
void securemodeon()
{
securemode=1;
}
void securemodeof()
{
securemode=0;
}
codechain::codechain()
{
first=last=0;
}
codechain operator||(codechain c1,codechain c2)
{
if (c1.first==0) return c2;
if (c2.first==0) return c1;
if (securemode) {
codenode *cn=c1.first;
while (cn!=0) {
cn->check=0;
cn=cn->next;
}
cn=c2.first;
while (cn!=0) {
cn->check=0;
cn=cn->next;
}
cn=c1.first;
while (cn!=0) {
if (cn->check!=0) {
cout<<"Extremelly fatal error: concat of overlapping chains."<<endl;
exit(1);
}
cn->check=1;
cn=cn->next;
}
cn=c2.first;
while (cn!=0) {
if (cn->check!=0) {
cout<<"Extremelly fatal error: concat of overlapping chains."<<endl;
exit(1);
}
cn->check=1;
cn=cn->next;
}
}
c1.last->next=c2.first;
c1.last=c2.last;
return c1;
}
codechain::codechain(string codop,string op1)
{
codenode *cn=new codenode;
cn->codop=codop;
cn->op.push_back(op1);
cn->next=0;
first=last=cn;
}
codechain::codechain(string codop,string op1,string op2)
{
codenode *cn=new codenode;
cn->codop=codop;
cn->op.push_back(op1);
cn->op.push_back(op2);
cn->next=0;
first=last=cn;
}
codechain::codechain(string codop,string op1,string op2,string op3)
{
codenode *cn=new codenode;
cn->codop=codop;
cn->op.push_back(op1);
cn->op.push_back(op2);
cn->op.push_back(op3);
cn->next=0;
first=last=cn;
}
bool isint(string s)
{
for (int i=0;i<(int)s.size();i++) {
if (s[i]<'0'||s[i]>'9') return false;
}
return true;
}
bool isextendedint(string s)
{
if (!isint(s)) {
return s.substr(0,7)=="offset(";
}
return true;
}
bool iskeyword(string s)
{
return (s=="program" || s=="parameters" ||
s=="endparameters" || s=="variables" || s=="endvariables" ||
s=="endprogram" || s=="subroutine" || s=="endsubroutine");
}
bool isinstruction(string s)
{
return numops.find(s)!=numops.end();
}
bool isstring(string s)
{
if (s.size()<2) return false;
if (s[0]!='\"' || s[(int)s.size()-1]!='\"') return false;
for (int i=1;i<(int)s.size()-1;i++) {
if (s[i]=='\"') return false;
}
return true;
}
bool istemporal(string s)
{
if (s.size()==0 || s[0]!='t') return false;
for (int i=1;i<(int)s.size();i++) {
if (s[i]<'0'||s[i]>'9') return false;
}
return true;
}
bool isvariable(string s)
{
if (s.size()==0) return false;
if (iskeyword(s)) return false;
if (!((s[0]>='a' && s[0]<='z') ||
(s[0]>='A' && s[0]<='Z') || s[0]=='_')) return false;
for (int i=1;i<(int)s.size();i++) {
if (!((s[i]>='a' && s[i]<='z') ||
(s[i]>='A' && s[i]<='Z') ||
(s[i]>='0' && s[i]<='9') || s[i]=='_')) {
return false;
}
}
return true;
}
bool isaddress(string s)
{
return istemporal(s) || isvariable(s);
}
bool isextendedintoraddress(string s)
{
return isextendedint(s) || isaddress(s);
}
codechain::codechain(char *cp)
{
string s=cp;
codechain c(s);
first=c.first;
last=c.last;
}
void jumpwhitespaces(string &s,int &i)
{
while (i<(int)s.size() && s[i]==' ') i++;
}
void getbasicunits(string &s,vector<string> &basicunits)
{
int i=0;
string word;
jumpwhitespaces(s,i);
while (i<(int)s.size()) {
word="";
if (s[i]=='\"') {
int iini=i;
i++;
while (i<(int)s.size() && s[i]!='\"') {
i++;
}
if (i==(int)s.size()) {
cout<<"CODE GENERATION FATAL ERROR, non-terminated string in: "<<s<<endl;
} else {
basicunits.push_back(s.substr(iini,i-iini+1));
i++;
}
} else {
while (i<(int)s.size() && s[i]!=' ') {
word=word+string(1,s[i]);
i++;
}
basicunits.push_back(word);
}
jumpwhitespaces(s,i);
}
}
codechain::codechain(string s)
{
vector<string> basicunits;
getbasicunits(s,basicunits);
codechain c;
int i=0;
while (i<(int)basicunits.size()) {
if (numops.find(basicunits[i])==numops.end()) {
cout<<"CODE GENERATION FATAL ERROR, a non-operation tried as operation: "<<basicunits[i]<<endl;
} else {
if (numops[basicunits[i]]=="") {
codenode *cn=new codenode;
cn->codop=basicunits[i];
cn->next=0;
codechain caux;
caux.first=caux.last=cn;
c=c||caux;
} else if ((int)numops[basicunits[i]].size()>(int)basicunits.size()-i-1) {
cout<<"CODE GENERATION FATAL ERROR, not enough operators for operation: "<<basicunits[i]<<endl;
} else {
bool error=false;
for (int j=0;j<(int)numops[basicunits[i]].size();j++) {
if (numops[basicunits[i]][j]=='s' && !isstring(basicunits[i+1+j])) {
cout<<"CODE GENERATION FATAL ERROR, string expected instead of: "<<basicunits[i+1+j]<<endl;
error=true;
} else if (numops[basicunits[i]][j]=='a' && !isaddress(basicunits[i+1+j])) {
cout<<"CODE GENERATION FATAL ERROR, temporal or variable expected instead of: "<<basicunits[i+1+j]<<endl;
error=true;
} else if (numops[basicunits[i]][j]=='c' && !isextendedint(basicunits[i+1+j])) {
cout<<"CODE GENERATION FATAL ERROR, integer constant expected instead of: "<<basicunits[i+1+j]<<endl;
error=true;
} else if (numops[basicunits[i]][j]=='i' && !isextendedintoraddress(basicunits[i+1+j])) {
cout<<"CODE GENERATION FATAL ERROR, value expected instead of: "<<basicunits[i+1+j]<<endl;
error=true;
} else if (numops[basicunits[i]][j]=='t' && !istemporal(basicunits[i+1+j])) {
cout<<"CODE GENERATION FATAL ERROR, temporal expected instead of: "<<basicunits[i+1+j]<<endl;
error=true;
} else if (numops[basicunits[i]][j]=='v' && !isvariable(basicunits[i+1+j])) {
cout<<"CODE GENERATION FATAL ERROR, variable expected instead of: "<<basicunits[i+1+j]<<endl;
error=true;
}
}
if (!error) {
switch ((int)numops[basicunits[i]].size()) {
case 1: c=c||codechain(basicunits[i],basicunits[i+1]);break;
case 2: c=c||codechain(basicunits[i],basicunits[i+1],basicunits[i+2]);break;
case 3: c=c||codechain(basicunits[i],basicunits[i+1],basicunits[i+2],basicunits[i+3]);break;
}
}
i+=(int)numops[basicunits[i]].size();
}
}
i++;
}
first=c.first;
last=c.last;
}
void writecodechain(codechain &c,ostream &os)
{
codenode *cn=c.first;
while (cn!=0) {
if (cn->codop!="etiq" && cn->codop!="ETIQ") {
os<<" "<<cn->codop;
} else {
os<<" "<<cn->codop;
}
for (int i=0;i<(int)cn->op.size();i++) {
os<<" "<<cn->op[i];
}
os<<endl;
cn=cn->next;
}
}
void writesubroutinecontents(codesubroutine &cs,ostream &os)
{
os<<" parameters"<<endl;
for (list<string>::iterator it=cs.parameters.begin();it!=cs.parameters.end();it++) {
os<<" "<<*it<<endl;
}
os<<" endparameters"<<endl<<endl;
os<<" variables"<<endl;
for (list<variable_data>::iterator it=cs.localvariables.begin();it!=cs.localvariables.end();it++) {
os<<" "<<(*it).name<<" "<<(*it).size<<endl;
}
os<<" endvariables"<<endl<<endl;
writecodechain(cs.c,os);
}
void writecodeglobal(codeglobal &cg,ostream &os)
{
os<<"program"<<endl;
writesubroutinecontents(cg.mainsub,os);
os<<"endprogram"<<endl;
for (list<codesubroutine>::iterator it=cg.l.begin();it!=cg.l.end();it++) {
os<<endl<<"subroutine "<<(*it).name<<endl;
writesubroutinecontents(*it,os);
os<<"endsubroutine"<<endl;
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// An interpreter of T-code, for direct execution:
//////////////////////////////////////////////////
//////////////////////////////////////////////////
#define MAXSTACK 1000000
class memorygest {
public:
map<int,int> m;
int stacksize;
map<int,int> allocatedsize;
int freeallocatedsize;
memorygest() {
stacksize=0;
freeallocatedsize=MAXSTACK+1;
}
int &operator[](int i) {
if (m.find(i)==m.end()) {
cout<<"Execution error: out of memory access."<<endl;
exit(1);
}
return m[i];
}
void executepush(int value) {
m[stacksize]=value;
stacksize++;
if (stacksize>=MAXSTACK) {
cout<<"Execution error: stack size used is bigger than the allowed size."<<endl;
exit(1);
}
}
void executepushsize(int size) {
for (int i=0;i<size/4;i++) {
executepush(0);
}
}
int executepop() {
int v=m[stacksize-1];
m.erase(stacksize-1);
stacksize--;
return v;
}
void executepopsize(int size) {
for (int i=0;i<size/4;i++) {
executepop();
}
}
void write() {
int first=true;
for (map<int,int>::iterator it=m.begin();it!=m.end();it++) {
if (!first) cout<<",";
first=false;
cout<<"M["<<4*it->first<<"]="<<it->second;
}
cout<<endl;
}
int malloc(int size) {
for (int i=0;i<size/4;i++) {
m[freeallocatedsize+i]=0;
}
allocatedsize[freeallocatedsize]=size;
int aux=freeallocatedsize;
freeallocatedsize+=size/4;
return 4*aux;
}
void free(int memposition) {
memposition=memposition/4;
if (allocatedsize.find(memposition)==allocatedsize.end()) {
cout<<"Execution error: free of a non-allocated memory position."<<endl;
exit(1);
}
for (int i=0;i<allocatedsize[memposition]/4;i++) {
m.erase(memposition+i);
}
allocatedsize.erase(memposition);
}
};
memorygest memory;
//vector<int> memory;
map<string,map<string,int> > relativeoffset;
map<string,int> relativeoffsetextern;
map<string,codesubroutine*> code;
map<string,int> variablesize;
map<string,map<string,codenode *> > label;
map<int,string> inttosubroutinename;
map<int,string> inttolabelname;
map<string,int> subroutinetoint;
map<string,map<string,int> > labeltoint;
int timelimit;
int stepbystep;
// In case of a temporal as variablename it is assumed to contain a memory address.
int memoryaddress(int scopebase,string subroutinename,string variablename,
map<string,int> &temporal)
{
if (istemporal(variablename)) {
return temporal[variablename];
} else {
return scopebase+relativeoffset[subroutinename][variablename];
}
}
// In case of a temporal as variablename it is assumed to contain a memory address.
int &memoryvalue(int scopebase,string subroutinename,string variablename,
map<string,int> &temporal)
{
if (istemporal(variablename)) {
return memory[temporal[variablename]/4];
} else {
return memory[(scopebase+relativeoffset[subroutinename][variablename])/4];
}
}
int &valuereferenceable(int scopebase,string subroutinename,string variablename,
map<string,int> &temporal)
{
if (istemporal(variablename)) {
return temporal[variablename];
} else {
return memory[(scopebase+relativeoffset[subroutinename][variablename])/4];
}
}
int value(int scopebase,string subroutinename,string variablename,
map<string,int> &temporal)
{
if (isint(variablename)) {
return stringtoint(variablename);
} else if (istemporal(variablename)) {
return temporal[variablename];
} else if (relativeoffsetextern.find(variablename)!=
relativeoffsetextern.end()) {
return relativeoffsetextern[variablename];
} else if (relativeoffset[subroutinename].find(variablename)!=
relativeoffset[subroutinename].end()) {
return memory[(scopebase+relativeoffset[subroutinename][variablename])/4];
} else if (labeltoint[subroutinename].find(variablename)!=
labeltoint[subroutinename].end()) {
return labeltoint[subroutinename][variablename];
} else if (subroutinetoint.find(variablename)!=
subroutinetoint.end()) {
return subroutinetoint[variablename];
} else {
cout<<"EXECUTION ERROR: expression "<<variablename<<" has not a value."<<endl;
exit(1);
}
}
string obtainlabelname(int scopebase,string subroutinename,string variablename,
map<string,int> &temporal)
{
if (label[subroutinename].find(variablename)!=label[subroutinename].end()) {
return variablename;
} else {
return inttolabelname[value(scopebase,subroutinename,variablename,temporal)];
}
}
string obtainsubroutinename(int scopebase,string subroutinename,string variablename,
map<string,int> &temporal)
{
if (code.find(variablename)!=code.end()) {
return variablename;
} else {
return inttosubroutinename[value(scopebase,subroutinename,variablename,temporal)];
}
}
void writememoryandtemporalcontents(map<string,int> &temporal)
{
cout<<"Memory:";
memory.write();
cout<<"temporal:";
for (map<string,int>::iterator it=temporal.begin();it!=temporal.end();it++) {
cout<<","<<it->first<<"="<<it->second;
}
cout<<endl;
}
int executesubroutine(codesubroutine *cs)
{
int scopebase=4*(memory.stacksize-1);
memory.executepushsize(variablesize[cs->name]);
map<string,int> temporal;
for (codenode *instruction=cs->c.first;
instruction!=NULL;instruction=instruction->next) {
if (stepbystep) {
cout<<endl;
writememoryandtemporalcontents(temporal);
cout<<"executing: "<<instruction->codop;
for (int i=0;i<(int)instruction->op.size();i++) {
cout<<" "<<instruction->op[i];
}
cout<<endl;
}
if (--timelimit==0) {
cout<<endl<<"Time limit exceeded: the execution is aborted"<<endl;
return 1;
}
if (instruction->codop=="addi") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)+
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="aload") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
memoryaddress(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="call") {
if (executesubroutine(code[obtainsubroutinename(scopebase,
cs->name,
instruction->op[0],
temporal)])) {
memory.executepopsize(variablesize[cs->name]);
return 1;
}
} else if (instruction->codop=="copy") {
for (int i=0;i<value(scopebase,cs->name,instruction->op[2],temporal)/4;i++) {
memory[memoryaddress(scopebase,cs->name,instruction->op[1],temporal)/4+i]=
memory[memoryaddress(scopebase,cs->name,instruction->op[0],temporal)/4+i];
}
} else if (instruction->codop=="divi") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)/
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="equi") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)==
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="etiq") {
} else if (instruction->codop=="fjmp") {
if (!value(scopebase,cs->name,instruction->op[0],temporal))
instruction=label[cs->name][obtainlabelname(scopebase,
cs->name,
instruction->op[1],
temporal)];
} else if (instruction->codop=="free") {
memory.free(value(scopebase,cs->name,instruction->op[0],temporal));
} else if (instruction->codop=="grti") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)>
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="iload") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="killparam") {
memory.executepop();
} else if (instruction->codop=="land") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)&&
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="lesi") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)<
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="lnot") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
!value(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="load") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
memoryvalue(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="loor") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)||
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="mini") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
-value(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="move") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="muli") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)*
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="new") {
valuereferenceable(scopebase,cs->name,instruction->op[1],temporal)=
memory.malloc(value(scopebase,cs->name,instruction->op[0],temporal));
} else if (instruction->codop=="popparam") {
valuereferenceable(scopebase,cs->name,instruction->op[0],temporal)=memory.executepop();
} else if (instruction->codop=="pushparam") {
memory.executepush(value(scopebase,cs->name,instruction->op[0],temporal));
} else if (instruction->codop=="reai") {
cin>>valuereferenceable(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="retu") {
memory.executepopsize(variablesize[cs->name]);
return 0;
} else if (instruction->codop=="stop") {
memory.executepopsize(variablesize[cs->name]);
return 1;
} else if (instruction->codop=="stor") {
memoryvalue(scopebase,cs->name,instruction->op[1],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="subi") {
valuereferenceable(scopebase,cs->name,instruction->op[2],temporal)=
value(scopebase,cs->name,instruction->op[0],temporal)-
value(scopebase,cs->name,instruction->op[1],temporal);
} else if (instruction->codop=="ujmp") {
instruction=label[cs->name][obtainlabelname(scopebase,
cs->name,
instruction->op[0],
temporal)];
} else if (instruction->codop=="wrii") {
cout<<value(scopebase,cs->name,instruction->op[0],temporal);
} else if (instruction->codop=="wris") {
cout<<instruction->op[0].substr(1,int(instruction->op[0].size())-2);
} else if (instruction->codop=="wrln") {
cout<<endl;
} else {
cout<<endl<<"EXECUTION ERROR: non existing instruction "<<instruction->codop<<endl;
return 1;
}
}
cout<<endl<<"Error, list of instructions does not end with stop or retu"<<endl;
return 1;
}
void initializeexecution(codeglobal &cg)
{
memory=memorygest();//initializes the memory gestor.
// gives the offset for every pair subroutine name-variable name.
relativeoffset= map<string,map<string,int> > ();
// The same, but the pair is combined in one string.
relativeoffsetextern= map<string,int> ();
// Gives the codesubroutine information for every subroutine name.
code=map<string,codesubroutine*>();
// Gives the required size of local variables for every subroutine name.
variablesize=map<string,int> ();
// Gives the codenode address for every pair subroutine name-label name.
label=map<string,map<string,codenode *> > ();
// Gives the subroutine name associated to a subroutine integer identifier.
inttosubroutinename=map<int,string>();
// Gives the label name associated to a label integer identifier.
inttolabelname=map<int,string>();
// The converse of the two previous mappings.
subroutinetoint=map<string,int>();
labeltoint=map<string,map<string,int> >();
int indexsubroutinename=0;
int indexlabelname=0;
int offsetparameter=4-4*int(cg.mainsub.parameters.size());
for (list<string>::iterator it=cg.mainsub.parameters.begin();
it!=cg.mainsub.parameters.end();it++) {
relativeoffset["program"][*it]=offsetparameter;
relativeoffsetextern["offset(program:"+*it+")"]=offsetparameter;
offsetparameter+=4;
}
int offsetvariables=4;
for (list<variable_data>::iterator it=cg.mainsub.localvariables.begin();
it!=cg.mainsub.localvariables.end();it++) {
relativeoffset["program"][(*it).name]=offsetvariables;
relativeoffsetextern["offset(program:"+(*it).name+")"]=offsetvariables;
offsetvariables+=(*it).size;
}
code["program"]=&(cg.mainsub);
variablesize["program"]=offsetvariables-4;
for (codenode *c=cg.mainsub.c.first;c!=NULL;c=c->next) {
if (c->codop=="etiq") {
label["program"][c->op[0]]=c;
labeltoint["program"][c->op[0]]=indexlabelname;
inttolabelname[indexlabelname++]=c->op[0];
}
}
subroutinetoint["program"]=indexsubroutinename;
inttosubroutinename[indexsubroutinename++]="program";
for (list<codesubroutine>::iterator it2=cg.l.begin();
it2!=cg.l.end();it2++) {
offsetparameter=4-4*int((*it2).parameters.size());
for (list<string>::iterator it=(*it2).parameters.begin();
it!=(*it2).parameters.end();it++) {
relativeoffset[(*it2).name][*it]=offsetparameter;
relativeoffsetextern["offset("+(*it2).name+":"+*it+")"]=offsetparameter;
offsetparameter+=4;
}
offsetvariables=4;
for (list<variable_data>::iterator it=(*it2).localvariables.begin();
it!=(*it2).localvariables.end();it++) {
relativeoffset[(*it2).name][(*it).name]=offsetvariables;
relativeoffsetextern["offset("+(*it2).name+":"+(*it).name+")"]=offsetvariables;
offsetvariables+=(*it).size;
}
code[(*it2).name]=&(*it2);
variablesize[(*it2).name]=offsetvariables-4;
for (codenode *c=(*it2).c.first;c!=NULL;c=c->next) {
if (c->codop=="etiq") {
label[(*it2).name][c->op[0]]=c;
labeltoint[(*it2).name][c->op[0]]=indexlabelname;
inttolabelname[indexlabelname++]=c->op[0];
}
}
subroutinetoint[(*it2).name]=indexsubroutinename;
inttosubroutinename[indexsubroutinename++]=(*it2).name;
}
if (stepbystep) {
cout<<"Computed information:"<<endl;
for (map<string,int>::iterator it=relativeoffsetextern.begin();
it!=relativeoffsetextern.end();it++) {
cout<<it->first<<"="<<it->second<<endl;
}
}
}
int executecodeglobal(codeglobal &cg,int executiontime,int doitstepbystep)
{
timelimit=executiontime;
stepbystep=doitstepbystep;
initializeexecution(cg);
return executesubroutine(&cg.mainsub);
}
// a=address (temporal or variable), s=string, i=integer value (address or int)
// t=temporal, l=label, v=variable, c=intconst.
void initnumops()
{
numops["addi"]="iia";
numops["aload"]="aa";
numops["call"]="a";
numops["copy"]="aai";
numops["divi"]="iia";
numops["equi"]="iia";
numops["etiq"]="v";
numops["fjmp"]="ia";
numops["free"]="a";
numops["grti"]="iia";
numops["iload"]="ca";
numops["killparam"]="";
numops["land"]="iia";
numops["lesi"]="iia";
numops["lnot"]="ia";
numops["load"]="at";
numops["loor"]="iia";
numops["mini"]="ia";
numops["move"]="vt";
numops["muli"]="iia";
numops["new"]="ia";
numops["popparam"]="a";
numops["pushparam"]="i";
numops["reai"]="a";
numops["retu"]="";
numops["stop"]="";
numops["stor"]="vv";
numops["subi"]="iia";
numops["ujmp"]="a";
numops["wrii"]="i";
numops["wris"]="s";
numops["wrln"]="";
}