-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyNew.cpp
1376 lines (1315 loc) · 39 KB
/
tinyNew.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <list>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;
//global variables
int line, debug; // source line number
bool parseerror; // stop after parsing if set to true
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics: constants vvvvvvvvvvvvvvvvv
int stats;
#define NUMREGS 200
#define LAT_MOV_rl 1
#define LAT_MOV_m 5
#define LAT_INT_rl 1
#define LAT_INT_m 6
#define LAT_FP_rl 3
#define LAT_FP_m 8
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics: constants ^^^^^^^^^^^^^^^^^
int latestTime = 0;
void passert(bool condition, string errormsg){
if (!condition) {
cout<<"error on line "<<line<<" : "<<errormsg<<'\n';
parseerror = true;
}
}
void fatal(string errormsg){
cout<<"error on line "<<line<<" : "<<errormsg<<'\n';
exit(1);
}
class machine {
int ival1, ival2;
float rval1,rval2;
bool comparingint;
int registeri[NUMREGS];
float registerr[NUMREGS];
int registerFreeTime[NUMREGS];
public:
machine(){
ival1=0;ival2=0;rval1=0;rval2=0;
for (int i=0; i<NUMREGS; i++)
{
registeri[i]=0;
registerr[i]=0;
registerFreeTime[i]=0;
}
}
void setstatusi(int i1, int i2)
{
ival1=i1; ival2=i2; comparingint=true;
}
void setstatusr(float r1, float r2){
rval1=r1; rval2=r2; comparingint=false;
}
bool gt() {if (comparingint) return ival1>ival2; else return rval1>rval2;}
bool ge() {if (comparingint) return ival1>=ival2; else return rval1>=rval2;}
bool lt() {if (comparingint) return ival1<ival2; else return rval1<rval2;}
bool le() {if (comparingint) return ival1<=ival2; else return rval1<=rval2;}
bool eq() {if (comparingint) return ival1==ival2; else return rval1==rval2;}
bool ne() {if (comparingint) return ival1!=ival2; else return rval1!=rval2;}
void setregi(int reg, int i){registeri[reg]=i;}
void setregr(int reg, float r){registerr[reg]=r;}
int regi(int reg){return registeri[reg];}
float regr(int reg){return registerr[reg];}
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
void setFreeTime(int reg, int ft)
{
registerFreeTime[reg] = ft;
latestTime = max(latestTime,ft);
}
int getFreeTime(int reg) { return registerFreeTime[reg]; }
void printFreeTime(int reg)
{
cout << registerFreeTime[reg];
}
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics ^^^^^^^^^^^^^^^^^
void print(){
cout<<"status:"<<ival1<<','<<ival2<<" "<<rval1<<','<<rval2;
if (comparingint) cout<<" int\n"; else cout<<" float\n";
cout<<"regs "<<registeri[0]<<' '<<registeri[1]<<' '<<registeri[2]<<' '<<registeri[3]<<" ";
cout<<registerr[0]<<' '<<registerr[1]<<' '<<registerr[2]<<' '<<registerr[3]<<'\n';
}
};
namespace opcodes {
enum optype {var,str, label, move, addi,addr, subi,subr, muli,mulr, divi,divr,
inci, deci, cmpi, push, pop, ret, link, unlnk, cmpr,
jsr, jmp, jgt, jlt, jge, jle, jeq, jne,
// all jumps must be between jsr..jne
sys, endofprogram, emptyline, unknown };
}
enum operandtype {id,stackref,reg,num,strval,empty1,nonknown};
enum syscalls {SCreadi,SCreadr,SCwritei,SCwriter,SCwrites,SChalt,SCunknown};
class opcode;
class StackElement {
int ivalue;
float rvalue;
list<opcode>::iterator address;
enum stackcontent {dataitem, adritem, fpitem} elementtype;
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
int freeTime;
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics ^^^^^^^^^^^^^^^^^
public:
StackElement(int ii, float ff){freeTime = 0; ivalue=ii; rvalue=ff; elementtype=dataitem;}
StackElement(int fp){freeTime = 0; ivalue=fp; elementtype=fpitem;}
StackElement(list<opcode>::iterator adr){freeTime = 0; address=adr; elementtype=adritem;}
void setival(int i){
passert(elementtype==dataitem,"illegal data stack reference");
ivalue=i;
}
void setrval(float r){
passert(elementtype==dataitem,"illegal data stack reference");
rvalue=r;
}
int ival() {passert(elementtype==dataitem,"illegal int stack reference");
return ivalue;}
float rval(){passert(elementtype==dataitem,"illegal float stack reference");
return rvalue;}
int fval(){passert(elementtype==fpitem,"illegal fp stack reference");
return ivalue;}
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
int getFreeTime()
{
return freeTime;
}
void setFreeTime(int ft)
{
freeTime = ft;
}
void printFreeTime()
{
cout << freeTime;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics ^^^^^^^^^^^^^^^^^
list<opcode>::iterator aval(){
passert(elementtype==adritem,"illegal pc stack reference");
return address;}
};
// GLOBAL VARIABLE. MACHINE AND SYSTEM STATE:
machine cpu;
vector<StackElement> stack;
int fp = 0;
//for the user, the stack will grow downwards, however the implementation
// is a list that starts from 1. Offsets will need to be negated.
class Symbol {
string nam;
int ivalue;
float rvalue;
string svalue;
bool sconst;
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
int freeTime; // Time this variable will be free.
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics ^^^^^^^^^^^^^^^^^
public:
Symbol(string s){freeTime=0; nam=s; ivalue=0;rvalue=0;sconst=false;}
Symbol(string s1, string s2){freeTime=0; nam=s1; ivalue=0;rvalue=0;svalue=s2;sconst=true;}
void setival(int i){ivalue=i;}
void setrval(float r){rvalue=r;}
string name(){return nam;}
int ival(){return ivalue;}
float rval(){return rvalue;}
string sval(){return svalue;}
bool strconst() {return sconst;}
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
void setFreeTime(int ft)
{
freeTime = ft;
latestTime = max(latestTime,freeTime);
}
int getFreeTime(){return freeTime;}
void printFreeTime(){
cout << freeTime;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics ^^^^^^^^^^^^^^^^^
void print(){cout<<nam<<':'; if (sconst) cout<<svalue; else cout<<ivalue<<'/'<<rvalue; }
};
class operand {
string opname;
int stackoffset;
float literalvalue;
list<Symbol>::iterator varref;
operandtype ot;
public:
int registernumber;
string name(){return opname;}
// functions used during parsing
void settype(operandtype t){ot=t;}
void setreg(int regno){ot=reg; registernumber=regno;}
void setlit(float r){ot=num; literalvalue=r;}
void setstackref(int ofs){ot=stackref; stackoffset=ofs;}
void setname(string ss){opname=ss;}
void setvarref(list<Symbol>::iterator vr){varref=vr;}
operandtype type(){return ot;}
// functions used during "execution"
// generic operations (operate on both int and float field)
void setval(operand &op){setival(op.ival());setrval(op.rval());}
void add(operand &op){
setival(ival()+op.ival());
setrval(rval()+op.rval());
}
void sub(operand &op){
setival(ival()-op.ival());
setrval(rval()-op.rval());
}
void mul(operand &op){
setival(ival()*op.ival());
setrval(rval()*op.rval());
}
void divi(operand &op){setival(ival()/op.ival());}
void divr(operand &op){setrval(rval()/op.rval());}
void inci(){setival(ival()+1);}
void deci(){setival(ival()-1);}
// set/get individual int and float field
void setival(int i){
if (ot==id) {varref->setival(i);}
else if (ot==reg) cpu.setregi(registernumber,i);
else if (ot==stackref) stack[fp-stackoffset].setival(i);
else fatal("setival: illegal operand type");
}
void setrval(float r){
if (ot==id) {varref->setrval(r);}
else if (ot==stackref) stack[fp-stackoffset].setrval(r);
else if (ot==reg) cpu.setregr(registernumber,r);
else fatal("setrval: illegal operand type");
}
int ival(){
if (ot==id) {return varref->ival();}
else if (ot==stackref) return stack[fp-stackoffset].ival();
else if (ot==reg) return cpu.regi(registernumber);
else if (ot==num) return (int) literalvalue;
else fatal("operand::ival: illegal operand type");
}
float rval(){
if (ot==id) {return varref->rval();}
else if (ot==stackref) return stack[fp-stackoffset].rval();
else if (ot==reg) return cpu.regr(registernumber);
else if (ot=num) return literalvalue;
else fatal("operand::rval: illegal operand type");
}
string sval(){
if (ot==id) {return varref->sval();}
else fatal("operand::sval: illegal operand type");
}
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
void setFreeTime(int ft){
switch (ot) {
case id:
{
varref->setFreeTime(ft);
break;
}
case reg:
{
cpu.setFreeTime(registernumber,ft);
break;
}
case stackref:
{
stack[fp-stackoffset].setFreeTime(ft);
break;
}
}
}
int getFreeTime(){
switch(ot) {
case id: {return varref->getFreeTime();}
case reg: {return cpu.getFreeTime(registernumber);}
case stackref: {return stack[fp-stackoffset].getFreeTime();}
default: {return 0;}
}
}
void printFreeTime(){
switch (ot) {
case id: {varref->printFreeTime();break;}
case reg: {cpu.printFreeTime(registernumber);break;}
case stackref: {stack[fp-stackoffset].printFreeTime(); break; }
}
}
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics ^^^^^^^^^^^^^^^^^
void print(){
switch (ot) {
case id: {cout<<"id:"<<opname;break;}
case stackref: {cout<<"stackref:"<<opname;break;}
case reg: {cout<<"reg"<<registernumber;break;}
case num: {cout<<"num:"<<literalvalue;break;}
case strval: {cout<<"str:"<<opname;break;}
case empty1: {if (debug>=4) cout<<"emptyop"; break;}
case nonknown: {cout<<"unknownop";break;}
}
}
};
class opcode {
int srcline;
opcodes::optype typ;
operand op1,op2;
list<opcode>::iterator jumptarget; //for jump instructions
string opname(opcodes::optype t){
switch (t) {
case opcodes::var: {return "var";}
case opcodes::str: {return "str";}
case opcodes::label: {return "label";}
case opcodes::move: {return "move";}
case opcodes::addi: {return "addi";}
case opcodes::addr: {return "addr";}
case opcodes::subi: {return "subi";}
case opcodes::subr: {return "subr";}
case opcodes::muli: {return "muli";}
case opcodes::mulr: {return "mulr";}
case opcodes::divi: {return "divi";}
case opcodes::divr: {return "divr";}
case opcodes::inci: {return "inci";}
case opcodes::deci: {return "deci";}
case opcodes::cmpi: {return "cmpi";}
case opcodes::cmpr: {return "cmpr";}
case opcodes::push: {return "push";}
case opcodes::pop: {return "pop";}
case opcodes::jsr: {return "jsr";}
case opcodes::ret: {return "ret";}
case opcodes::link: {return "link";}
case opcodes::unlnk: {return "unlnk";}
case opcodes::jmp: {return "jmp";}
case opcodes::jgt: {return "jgt";}
case opcodes::jlt: {return "jlt";}
case opcodes::jge: {return "jge";}
case opcodes::jle: {return "jle";}
case opcodes::jeq: {return "jeq";}
case opcodes::jne: {return "jne";}
case opcodes::sys: {return "sys";}
default: {return "unknown";}
}
}
public:
opcode(opcodes::optype t,operand o1, operand o2): typ(t),op1(o1),op2(o2),srcline(line){}
int src(){return srcline;}
void settarget(list<opcode>::iterator pc){jumptarget=pc;}
list<opcode>::iterator target(){return jumptarget;}
string name() {return opname(typ);}
opcodes::optype code(){return typ;}
operand & o1(){return op1;}
operand & o2(){return op2;}
void print(){
cout<<"line:"<<srcline<<" "<<opname(typ)<<" ";
op1.print(); cout<<" ";
op2.print();
}
void print(int thyme){
cout<<"time:"<<thyme<<" line:"<<srcline<<" "<<opname(typ)<<" ";
op1.print(); cout<<" ";
op2.print();
}
};
// class jumptarget {
// string labl;
// list<opcode>::iterator target;
// public:
// jumptarget(string l, list<opcode>::iterator t){labl=l,target=t;}
// string label(){return labl;}
// list<opcode>::iterator targetpc(){return target;}
// };
int isRegister(char * s, int& regno) {
// return true if s is a string of the form r<no> or R<no>,
// where <no> is a register number in the range 0..NUMREGs-1
if (tolower(s[0])!='r') return false;
for (int i=1;s[i]!=0;i++) if (!isdigit(s[i])) return false;
regno = atoi(&s[1]);
return (regno>=0 && regno<NUMREGS);
}
void parseline(opcodes::optype & code, operand& op1, operand& op2, ifstream & src, int line) {
op1.settype(empty1);
op2.settype(empty1);
if (src.eof()) {code=opcodes::endofprogram; return;}
string _str;
char *p;
char sc[40], ch;
int is;
string ss;
int ip =0;
//src.get(buf,200,'\n'); // read a source line
getline (src, _str);
// while (src.get(ch)) if (! isspace(ch)){ src.putback(ch); break;} // skip end of line
//src.get(ch);
//if (ch!='\n' && !src.eof()) {cout<<"warning: '"<<(int) ch<<"' found at end of line "<<line<<'\n';}
if (debug==4) cout << "debug: "<< _str;
const char * buf = _str.c_str();
// get opcode
while (buf[ip]==' '){ip++;} // eat white space
is = 0;
while (isalpha(buf[ip])){sc[is++]=tolower(buf[ip++]);} // move opcode to ss, move to lower case
sc[is]=0;
ss=sc; // for some operations it's more convenient to have it in a string type
if (debug==4) cout << "debug: "<<ss;
if (ss=="end") code = opcodes::endofprogram;
else if (ss=="") code = opcodes::emptyline;
else if (ss=="var") code = opcodes::var;
else if (ss=="str") code = opcodes::str;
else if (ss=="label") code = opcodes::label;
else if (ss=="move") code = opcodes::move;
else if (ss=="addi") code = opcodes::addi;
else if (ss=="addr") code = opcodes::addr;
else if (ss=="subi") code = opcodes::subi;
else if (ss=="subr") code = opcodes::subr;
else if (ss=="muli") code = opcodes::muli;
else if (ss=="mulr") code = opcodes::mulr;
else if (ss=="divi") code = opcodes::divi;
else if (ss=="divr") code = opcodes::divr;
else if (ss=="inci") code = opcodes::inci;
else if (ss=="deci") code = opcodes::deci;
else if (ss=="cmpi") code = opcodes::cmpi;
else if (ss=="cmpr") code = opcodes::cmpr;
else if (ss=="push") code = opcodes::push;
else if (ss=="pop") code = opcodes::pop;
else if (ss=="jsr") code = opcodes::jsr;
else if (ss=="ret") code = opcodes::ret;
else if (ss=="link") code = opcodes::link;
else if (ss=="unlnk") code = opcodes::unlnk;
else if (ss=="jmp") code = opcodes::jmp;
else if (ss=="jgt") code = opcodes::jgt;
else if (ss=="jlt") code = opcodes::jlt;
else if (ss=="jge") code = opcodes::jge;
else if (ss=="jle") code = opcodes::jle;
else if (ss=="jeq") code = opcodes::jeq;
else if (ss=="jne") code = opcodes::jne;
else if (ss=="sys") code = opcodes::sys;
else code = opcodes::unknown;
// read first operand
while (buf[ip]==' '){ip++;} // eat white space
is = 0;
if (buf[ip]!=';') {
while (isgraph(buf[ip])) {sc[is++] = buf[ip++];} // move operand to sc
}
sc[is]=0;
ss=sc;
if (debug==4) cout << "debug: "<<ss;
int regno;
if (isRegister(sc,regno)) {op1.setreg(regno);}
else if (isalpha(sc[0])) {op1.settype(id);}
else if (sc[0]=='$') {op1.setstackref(atoi(&sc[1]));}
else if (sc[0]=='+' || sc[0]=='-' || isdigit(sc[0])) { // assume it's a number
op1.setlit(strtod(sc,0)); // literals are always represented as floats
}
else if (ss!="") {op1.settype(nonknown);}
op1.setname(ss);
// read second operand
while (buf[ip]==' '){ip++;} // eat white space
is = 0;
if (isalpha(buf[ip])||buf[ip]=='$') { // alpha,$ followed by alphanum with punctuation
while (isgraph(buf[ip])) {sc[is++]=buf[ip++];}
sc[is]=0;
ss=sc;
if (debug==4) cout << "debug: "<<ss;
if (isRegister(sc,regno)) {op2.setreg(regno);}
else if (sc[0]=='$') {op2.setstackref(atoi(&sc[1]));}
else {op2.settype(id);}
op2.setname(ss);
}
else if (buf[ip]=='"') { // doublequote-delimited string
op2.settype(strval);
ip++; // skip leading
is=0;
ss = "";
while (buf[ip]!='"') {
if (buf[ip-1]=='\\' && buf[ip]=='n') {ss[is-1]='\n'; ip++;}
else {ss += buf[ip++]; is++;}
// else sc[is++]=buf[ip++];
}
// sc[is]=0;
ip++;
op2.setname(ss);
}
while (isspace(buf[ip])){ip++;} // eat white space
if (buf[ip] != ';' && buf[ip] != 0) cout<< "line "<<line<<" warning: non comment found at end of line\n";
}
syscalls checksyscall(operand op){
if (op.name()=="readi"){return SCreadi;}
if (op.name()=="readr"){return SCreadr;}
if (op.name()=="writei"){return SCwritei;}
if (op.name()=="writer"){return SCwriter;}
if (op.name()=="writes"){return SCwrites;}
if (op.name()=="halt"){return SChalt;}
return SCunknown;
}
void linkvariable(list<Symbol>&table, operand & o, int srcline){
// if the operand is an id, find the variable in the table and make a
// reference to it in the operand
if (o.type()==id) {
for (list<Symbol>::iterator it=table.begin();it!=table.end();it++){
if (it->name()==o.name()) {
o.setvarref(it);
return;
}
}
cout << "error on line "<<srcline<<" identifier "<<o.name()<<" not defined\n";
parseerror = true;
}
}
int main(int argc, char *argv[]){
if (argc==1) {cerr<<"usage: tiny srcfile [stats|nostats|d1|d2|d3 [mix]]\n"; exit(1);}
bool mix = false; // allow mixed declarations and code
debug = 0;
stats = 1;
if (argc>=3){
if (string(argv[2])=="d1") debug=1;
else if (string(argv[2])=="d2") debug=2;
else if (string(argv[2])=="d3") debug=3;
else if (string(argv[2])=="d4") debug=4;
else if (string(argv[2])=="stats") stats=1; // Print statistics
else if (string(argv[2])=="nostats") stats=0; // Don't print statistics
}
if (argc>=4){
if (string(argv[3])=="mix") mix=true;
}
ifstream src(argv[1]);
if (!src) {cerr<<argv[1]<<" not found\n"; exit(1);}
line = 0;
parseerror = false;
list<opcode> program;
list<Symbol> symboltable;
// list<jumptarget> labeltab;
operand op1,op2;
opcodes::optype code;
bool done = false;
bool declarations = true; // parsing is in the declarations section
while (!done) {
line ++;
parseline(code,op1,op2,src,line);
if (code==opcodes::endofprogram) {done=true;}
else if (code==opcodes::emptyline) {}
else {
// checkoperands(op1,op2);
// op1 can be num/reg/id, op2 can be reg/id/strval/empty
// only one of them can be an id
// there must be 1 or 2 operands
switch (code) {
case opcodes::var: {
passert(declarations||mix,"declarations must preceed all code");
passert(op1.type()==id,"identifier operand expected");
passert(op2.type()==empty1,"only one operand expected");
symboltable.push_back(Symbol(op1.name())); break;
}
case opcodes::str : {
passert(declarations||mix,"declarations must preceed all code");
passert(op1.type()==id,"1st operand must be indentifier");
passert(op2.type()==strval,"2nd operand must be string");
symboltable.push_back(Symbol(op1.name(),op2.name()));
break;
}
case opcodes::label : {
passert(op1.type()==id,"1st operand must be indentifier");
passert(op2.type()==empty1,"only one operand expected");
// labeltab.push_back(op1.name()); // may not need this
declarations = false;
program.push_back(opcode(opcodes::label,op1,op2));
break;
}
case opcodes::move: {
passert( (op1.type()==num || op1.type()==reg ||
op1.type()== id || op1.type()== stackref)
&& (op2.type()==reg || op2.type()== id|| op2.type()== stackref),
"illegal operand type");
passert(!((op1.type()==id || op1.type()== stackref)&&
(op2.type()==id || op2.type()== stackref)),
"both operands are memory refs");
declarations = false;
program.push_back(opcode(code,op1,op2));
break;
}
case opcodes::inci:
case opcodes::deci: {
passert(op1.type()==reg,"operand must be a register");
passert(op2.type()==empty1,"only one operand expected");
declarations = false;
program.push_back(opcode(code,op1,op2));
break;
}
case opcodes::addi:
case opcodes::addr:
case opcodes::subr:
case opcodes::subi:
case opcodes::muli:
case opcodes::mulr:
case opcodes::divi:
case opcodes::divr:
case opcodes::cmpi:
case opcodes::cmpr: {
passert( (op1.type()==num || op1.type()==reg ||
op1.type()== id || op1.type()== stackref)
&& (op2.type()==reg), "illegal operand type");
declarations = false;
program.push_back(opcode(code,op1,op2));
break;
}
case opcodes::push: {
passert(op2.type()==empty1,"zero or one operand expected");
passert(op1.type()==num || op1.type()==reg || op1.type()== id
|| op1.type()== stackref || op1.type()==empty1 ,
"illegal operand type");
program.push_back(opcode(code,op1,op2));
declarations = false;
break;
}
case opcodes::pop:{
passert(op2.type()==empty1,"zero or one operand expected");
passert(op1.type()==reg || op1.type()== id ||
op1.type()==stackref ||op1.type()==empty1,
"illegal operand type");
program.push_back(opcode(code,op1,op2));
declarations = false;
break;
}
case opcodes::jsr: {
passert(op2.type()==empty1,"only one operand expected");
passert(op1.type()==id,"operand must be an identifier");
program.push_back(opcode(code,op1,op2));
declarations = false;
break;
}
case opcodes::ret: {
passert(op1.type()==empty1 && op2.type()==empty1,"no operand expected");
program.push_back(opcode(code,op1,op2));
declarations = false;
break;
}
case opcodes::link: {
passert(op1.type()==num && op2.type()==empty1,"illegal operand");
program.push_back(opcode(code,op1,op2));
declarations = false;
break;
}
case opcodes::unlnk: {
passert(op1.type()==empty1 && op2.type()==empty1,"no operand expected");
program.push_back(opcode(code,op1,op2));
declarations = false;
break;
}
case opcodes::jmp:
case opcodes::jgt:
case opcodes::jlt:
case opcodes::jge:
case opcodes::jle:
case opcodes::jne:
case opcodes::jeq: {
passert(op1.type()==id,"operand must be an identifier");
passert(op2.type()==empty1,"only one operand expected");
declarations = false;
program.push_back(opcode(code,op1,op2));
break;
}
case opcodes::sys : {
syscalls scl = checksyscall(op1);
passert(scl != SCunknown,"unknown system call");
if (scl == SChalt) {
passert(op2.type()==empty1,"only one operand expected");
}
// operand types are not fully checked
declarations = false;
program.push_back(opcode(code,op1,op2)); break;
}
case opcodes::unknown : {
passert(false,"unknown opcode");
}
}
}
}
if (debug>=1) { // print all identifiers
for (list<Symbol>::iterator it = symboltable.begin(); it!=symboltable.end(); it++) {
cout<<"id "; it->print();cout<<'\n';
}
for (list<opcode>::iterator it = program.begin(); it!=program.end(); it++) {
it->print(); cout<<'\n';
}
}
// check if all ids are defined; set jump target pcs:
for (list<opcode>::iterator it = program.begin(); it!=program.end(); it++) {
if (it->code()>=opcodes::jsr && it->code()<=opcodes::jne) {
// for all jump instructions, insert the jump target
list<opcode>::iterator jt = program.begin();
// find the opcode that has this label
bool labelfound = false;
for ( ; jt!=program.end() && ! labelfound; jt++) {
if (jt->code()==opcodes::label && jt->o1().name()==it->o1().name()) {
it->settarget(jt);
labelfound = true;
}
}
if (!labelfound) {
cout<<"error on line "<<it->src()<<" jump target is not defined\n";
parseerror=true;
}
}
else if (it->code()==opcodes::sys){
// for sys calls the first operand is the syscall name, although its operand
// type id "id", so only check the 2nd operand
linkvariable(symboltable,it->o2(),it->src());
}
else if (it->code()!=opcodes::label) {
// in all other cases, except labels, check both operands
linkvariable(symboltable,it->o1(),it->src());
linkvariable(symboltable,it->o2(),it->src());
}
}
if (parseerror) exit(1);
// execute the program
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics: initialization vvvvvvvvvvvvvvvvv
int CNT_register[NUMREGS];
int CNT_mem = 0;
int CNT_iOps = 0; // Count integer instructions.
int CNT_rOps = 0; // Count real instructions.
int CNT_peeps = 0; // Count peephole inci/deci optimizations.
int CNT_branches = 0; // Count branches.
int CNT_instructions = 0; // Count total # of instructions.
int CNT_cycles = 0; // The cycles.
int CNT_target = 0; // The target of the command, if any.
// CNT_target ==0 means no target, ==1 means op1, ==2 means op2.
// Use CNT_target to update the time a symbol/register is free.
int CNT_cmp = 0; // Since cmpi ... jge ... is a two-part command, need to keep track of the time the cmpi ends.
int CNT_latency = 0; // Latency for an instruction.
int CNT_MOV_rl = 0;
int CNT_MOV_m = 0;
int CNT_INT_rl = 0;
int CNT_INT_m = 0;
int CNT_FP_rl = 0;
int CNT_FP_m = 0;
int i,allregs;
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics: initialization ^^^^^^^^^^^^^^^^^
bool eop = false;
int itemp;
float rtemp;
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics: initialization vvvvvvvvvvvvvvvvv
if(stats) {
for (i=0;i<NUMREGS;i++)
{
CNT_register[i]=0;
}
}
// ^^^^^^^^^^^^^^^^^^^^^^^^ Statistics: initialization ^^^^^^^^^^^^^^^^^
list<opcode>::iterator pc = program.begin();
while (!eop && pc != program.end()) {
if (debug>=3) {
cpu.print();
for (list<Symbol>::iterator it = symboltable.begin(); it!=symboltable.end(); it++) {
it->print(); cout <<" ";
}
cout<<'\n';
}
if (debug >=2)
{
if(stats)
{
pc->print(CNT_cycles); cout<<'\n';
} else {
pc->print(); cout<<'\n';
}
}
line = pc->src();
// vvvvvvvvvvvvvvvvvvvvvvvv Statistics vvvvvvvvvvvvvvvvv
if(stats) {
// I just do statistics in this first switch statement
CNT_latency = 0;
CNT_target = 0;
switch (pc->code())
{
case opcodes::label:
// No time
break;
case opcodes::move:
{
CNT_cycles = max(pc->o1().getFreeTime(),CNT_cycles);
CNT_cycles = max(pc->o2().getFreeTime(),CNT_cycles);
if(debug >= 2) {
cout << "FREETIME "<< CNT_cycles << " move (";
pc->o1().printFreeTime();
cout << ",";
pc->o2().printFreeTime();
cout << ")\n";
}
CNT_instructions++;
switch(pc->o1().type())
{
case id:
CNT_mem++;
CNT_latency = LAT_MOV_m;
break;
case reg:
CNT_register[pc->o1().registernumber]++;
default:
CNT_latency = LAT_MOV_rl;
}
switch(pc->o2().type())
{
case id:
CNT_mem++;
CNT_latency = max(LAT_MOV_m,CNT_latency);
break;
case reg:
CNT_register[pc->o2().registernumber]++;
default:
CNT_latency = max(LAT_MOV_rl,CNT_latency);
}
if(CNT_latency > LAT_MOV_rl)
{
CNT_MOV_m++;
} else {
CNT_MOV_rl++;
}
CNT_target = 2;
break;
}
// Peephole Ops
case opcodes::inci:
case opcodes::deci:
CNT_cycles = max(pc->o1().getFreeTime(),CNT_cycles);
if(debug >= 2) {
cout << "FREETIME "<< CNT_cycles << " inci/deci (";
pc->o1().printFreeTime();
cout << ")\n";
}
CNT_instructions++;
CNT_iOps++;
CNT_peeps++;
CNT_INT_rl++;
CNT_latency = LAT_INT_rl;
CNT_target = 1;
break;
case opcodes::cmpi:
{
CNT_cycles = max(pc->o1().getFreeTime(),CNT_cycles);
CNT_cycles = max(pc->o2().getFreeTime(),CNT_cycles);
if(debug >= 2) {
cout << "FREETIME "<< CNT_cycles << " intOp (";
pc->o1().printFreeTime();
cout << ",";
pc->o2().printFreeTime();
cout << ")\n";
}
CNT_instructions++;
CNT_iOps++;
switch(pc->o1().type())
{
case reg:
CNT_register[pc->o1().registernumber]++;
case num:
CNT_latency = LAT_INT_rl;
break;
case stackref:
case id:
CNT_mem++;
CNT_latency = LAT_INT_m;
break;
default:
fatal("STATISTICS: unknown op1 used in an integer op");
}
switch(pc->o2().type())
{
case reg:
CNT_register[pc->o2().registernumber]++;
case num:
CNT_latency = max(CNT_latency,LAT_INT_rl);
break;
case stackref:
case id:
CNT_mem++;
CNT_latency = max(LAT_INT_m,CNT_latency);
break;
default:
fatal("STATISTICS: unknown op2 used in an integer op");
}
if (CNT_latency > LAT_INT_rl) {
CNT_INT_m++;
} else {
CNT_INT_rl++;
}
// Compares have internal registers as the target.
CNT_cmp = CNT_cycles + CNT_latency; // jge, ... must wait for the compare.
break;
}
// Integer Ops
case opcodes::addi:
case opcodes::subi:
case opcodes::muli:
case opcodes::divi:
{
CNT_cycles = max(pc->o1().getFreeTime(),CNT_cycles);
CNT_cycles = max(pc->o2().getFreeTime(),CNT_cycles);
if(debug >= 2) {
cout << "FREETIME "<< CNT_cycles << " intOp (";
pc->o1().printFreeTime();
cout << ",";
pc->o2().printFreeTime();
cout << ")\n";
}
CNT_instructions++;
CNT_iOps++;
switch(pc->o1().type())
{
case reg:
CNT_register[pc->o1().registernumber]++;
case num:
CNT_latency = LAT_INT_rl;
break;
case stackref:
case id:
CNT_mem++;
CNT_latency = LAT_INT_m;
break;
default:
fatal("STATISTICS: unknown op1 used in an integer op");
}
switch(pc->o2().type())
{
case reg:
CNT_register[pc->o2().registernumber]++;
case num:
CNT_latency = max(CNT_latency,LAT_INT_rl);
break;
case stackref:
case id:
CNT_mem++;
CNT_latency = max(LAT_INT_m,CNT_latency);
break;
default:
fatal("STATISTICS: unknown op2 used in an integer op");
}
if (CNT_latency > LAT_INT_rl) {
CNT_INT_m++;
} else {
CNT_INT_rl++;
}
CNT_target = 2;
break;
}
case opcodes::cmpr:
{
CNT_cycles = max(pc->o1().getFreeTime(),CNT_cycles);
CNT_cycles = max(pc->o2().getFreeTime(),CNT_cycles);
if(debug>=2){
cout << "FREETIME "<< CNT_cycles << " fpOp (";
pc->o1().printFreeTime();
cout << ",";
pc->o2().printFreeTime();
cout << ")\n";