-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.y
934 lines (884 loc) · 27 KB
/
compiler.y
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
%{
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ID_SIZE 100
#define MAX_CHILDREN 3
#define STATEMENT 201
#define ARRAY 202
#define ARRAYINST 203
#define FUNCTIONDECL 204
#define FUNCTIONCALL 205
#define MAIN 206
int varCount = 0;
int loopCount = 0;
//file to be printed to
FILE *fp;
struct entry{
char *key;
float value;
int count;
};
int yywrap( );
void yyerror(const char* str);
struct Node* make_node(int type, double value, char* id);
void attach_node(struct Node* parent, struct Node* child);
void addVar(struct entry table[1000],char *var,float value);
void eval_stmt(struct Node* node,struct entry hashtable[1000]);
float eval_expr(struct Node* node,struct entry hashtable[1000]);
float getVar(struct entry hashtable[1000],char *name);
int floatToInt(float a);
struct Node* tree;
/* a tree node definition */
struct Node {
/* the type of the node */
int type;
/* the value of the node if it can have one */
double value;
/* the id of the node (used for identifiers only) */
char id[ID_SIZE];
/* at most three children nodes */
int num_children;
struct Node* children[MAX_CHILDREN];
};
%}
%token ENDLINE 115
%token ASSIGNMENT 116
%token START 119
%token END 120
%token IF 121
%token THEN 122
%token ELSE 123
%token WHILE 124
%token DO 125
%token PRINT 126
%token INPUT 127
%token FLOAT 132
%token ARRAYTYPE 133
%token <str> IDENTIFIER 100
%type <node> identifier
%token <val> VALUE 101
%right THEN
%right ELSE
%left OR 113
%left AND 112
%left LESS 106 LESSEQUAL 108 GREAT 107 GREATEQUAL 109 EQUAL 110 NOTEQUAL 111
%left PLUS 102 MINUS 103
%left STAR 105 SLASH 104
%right NOT 114
%right OPENPAREN 117
%left CLOSEPAREN 118
%right OPENBRACKET 128
%left CLOSEBRACKET 129
%right OPENCURLY 130
%left CLOSECURLY 131
%left COMMA 134
%union{
double val;
struct Node* node;
char str[100];
}
%type <node> type
%type <node> exprs
%type <node> args
%type <node> arrayStmt
%type <node> assignmentStmt
%type <node> ifStmt
%type <node> whileStmt
%type <node> ifElseStmt
%type <node> printStmt
%type <node> stmtSeq
%type <node> expr
%type <node> stmt
%type <node> stmts
%type <node> main
%type <node> functionStmt
%type <node> functions
%type <node> functionCall
/* give us more detailed errors */
%error-verbose
%%
program: main{
tree = $1;
}
|main functions {
/*tree = make_node(0,0,"");
*/
tree = $1;
attach_node($1,$2);
}
main: stmts{
//$$=$1;
$$=make_node(MAIN,0,"");
attach_node($$,$1);
}
functions: functionStmt {
$$=$1;
}
|functions functionStmt{
$$=$1;
attach_node($$,$2);
}
stmt: assignmentStmt {
$$=$1;
}
| ifStmt {
$$=$1;
}
| ifElseStmt {
$$=$1;
}
| whileStmt {
$$=$1;
}
| printStmt {
//$$=make_node(STATEMENT,0,"");
//attach_node($$,$1);
$$=$1;
}
| stmtSeq {
$$=$1;
}
| arrayStmt{
$$=$1;
}
|functionCall{
$$=$1;
}/*
|functionStmt{
$$=$1;
}*/
assignmentStmt: identifier ASSIGNMENT expr ENDLINE {
$$=make_node(ASSIGNMENT,0,"");
//struct Node* temp = make_node(IDENTIFIER,0,$1);
//attach_node($$,temp);
attach_node($$,$1);
attach_node($$,$3);
}
ifElseStmt: IF expr THEN stmt ELSE stmt {
$$=make_node(IF,0,"");
attach_node($$,$2);
attach_node($$,$4);
attach_node($$,$6);
}
ifStmt: IF expr THEN stmt {
$$=make_node(IF,0,"");
attach_node($$,$2);
attach_node($$,$4);
}
whileStmt: WHILE expr DO stmt {
$$=make_node(WHILE,0,"");
attach_node($$,$2);
attach_node($$,$4);
}
printStmt: PRINT expr ENDLINE {
$$=make_node(PRINT,0,"");
attach_node($$,$2);
}
arrayStmt: IDENTIFIER OPENBRACKET expr CLOSEBRACKET ENDLINE{
$$=make_node(ARRAYINST,0,$1);
attach_node($$,$3);
}
functionStmt: IDENTIFIER OPENPAREN args CLOSEPAREN OPENCURLY stmts CLOSECURLY{
$$=make_node(FUNCTIONDECL,0,$1);
attach_node($$,$3);
attach_node($$,$6);
}
functionCall:IDENTIFIER OPENPAREN exprs CLOSEPAREN ENDLINE{
$$=make_node(FUNCTIONCALL,0,$1);
attach_node($$,$3);
}
args: type IDENTIFIER{
$$=make_node(IDENTIFIER,0,$2);
attach_node($$,$1);
} | type IDENTIFIER COMMA args{
$$=make_node(IDENTIFIER,0,$2);
attach_node($$,$1);
attach_node($$,$4);
}|{$$=NULL;}
type: FLOAT{
$$=make_node(FLOAT,0,"");
}|ARRAYTYPE{
$$=make_node(ARRAYTYPE,0,"");
}
stmtSeq: START stmts END {
$$=$2;
}
stmts: stmt {
$$=make_node(STATEMENT,0,"");
attach_node($$,$1);
}
| stmt stmts {
$$=make_node(STATEMENT,0,"");
attach_node($$,$1);
attach_node($$,$2);
}
exprs: expr{
$$=$1;
}
|expr COMMA exprs{
$$=$1;
attach_node($$,$3);
}
/* an expression uses + or - or neither */
expr: NOT expr{
$$=make_node(NOT,0,"");
attach_node($$,$2);
}
| expr PLUS expr {
$$ = make_node(PLUS,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr MINUS expr {
$$ = make_node(MINUS,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr STAR expr {
$$ = make_node(STAR,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr SLASH expr {
$$ = make_node(SLASH,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr LESS expr {
$$ = make_node(LESS,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr LESSEQUAL expr {
$$ = make_node(LESSEQUAL,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr GREAT expr {
$$ = make_node(GREAT,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr GREATEQUAL expr {
$$ = make_node(GREATEQUAL,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr EQUAL expr {
$$ = make_node(EQUAL,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr NOTEQUAL expr {
$$ = make_node(NOTEQUAL,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr AND expr {
$$ = make_node(AND,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| expr OR expr {
$$ = make_node(OR,0,"");
attach_node($$,$1);
attach_node($$,$3);
}
| OPENPAREN expr CLOSEPAREN {
$$=$2;
}
| identifier {
$$=$1;
}
| VALUE {
$$=make_node(VALUE,$1,"");
}
| INPUT {
$$=make_node(INPUT,0,"");
}
identifier: IDENTIFIER OPENBRACKET expr CLOSEBRACKET{
$$=make_node(IDENTIFIER,0,$1);
attach_node($$,$3);
}
| IDENTIFIER{
$$=make_node(IDENTIFIER,0,$1);
}
%%
void print_tree(struct Node* node, int tabs) {
int i;
/* base case */
if(!node){
printf("!node");
return;
}
/* print leading tabs */
for(i = 0; i < tabs; i++) {
printf(" ");
}
switch(node->type) {
case IDENTIFIER: printf("IDENTIFIER: %s\n", node->id); break;
case VALUE: printf("VALUE: %lf\n", node->value); break;
case PLUS: printf("PLUS:\n"); break;
case MINUS: printf("MINUS:\n"); break;
case SLASH: printf("DIVIDE:\n"); break;
case STAR: printf("TIMES:\n"); break;
case LESS: printf("LESS THAN:\n"); break;
case GREAT: printf("GREATER:\n"); break;
case LESSEQUAL: printf("LESS EQUAL:\n"); break;
case GREATEQUAL: printf("GREATER EQUAL:\n"); break;
case EQUAL: printf("EQUALS:\n"); break;
case NOTEQUAL: printf("NOT EQUALS:\n"); break;
case AND: printf("AND:\n"); break;
case OR: printf("OR:\n"); break;
case NOT: printf("NOT:\n"); break;
case ASSIGNMENT: printf("ASSIGN:\n"); break;
case IF: printf("IF:\n"); break;
case WHILE: printf("WHILE:\n"); break;
case PRINT: printf("PRINT:\n"); break;
case INPUT: printf("INPUT:\n"); break;
case STATEMENT: printf("STATEMENT:\n"); break;
case ARRAY: printf("ARRAY:\n"); break;
case ARRAYINST: printf("ARRAYINST: %s\n",node->id); break;
case FUNCTIONDECL: printf("FUNCTIONDECL: %s\n",node->id); break;
case FUNCTIONCALL: printf("FUNCTIONCALL: %s\n",node->id); break;
case MAIN: printf("MAIN:\n"); break;
case ARRAYTYPE: printf("ARRAYTYPE:\n");break;
case FLOAT: printf("FLOAT:\n");break;
default:
printf("Error, %d not a valid node type.\n", node->type);
exit(1);
}
/* print all children nodes underneath */
for(i = 0; i < node->num_children; i++) {
print_tree(node->children[i], tabs + 1);
}
}
/* creates a new node and returns it */
struct Node* make_node(int type, double value, char* id) {
int i;
/* allocate space */
struct Node* node = malloc(sizeof(struct Node));
/* set properties */
node->type = type;
node->value = value;
strcpy(node->id, id);
node->num_children = 0;
for(i = 0; i < MAX_CHILDREN; i++) {
node->children[i] = NULL;
}
/* return new node */
return node;
}
/* attach an existing node onto a parent */
void attach_node(struct Node* parent, struct Node* child) {
/* connect it */
parent->children[parent->num_children] = child;
parent->num_children++;
assert(parent->num_children <= MAX_CHILDREN);
}
int yywrap( ) {
return 1;
}
void yyerror(const char* str) {
fprintf(stderr, "Compiler error: '%s'.\n", str);
}
void eval_stmt(struct Node* node,struct entry hashtable[1000]){
int i;
if(!node){
printf("Base case");
return;
}
switch(node->type) {
case MAIN:{
break;
}
case FUNCTIONCALL:
{
fprintf(fp,"#FUNCTIONCALL\n");
fprintf(fp,"call _%s\n",node->id);
break;
}
case FUNCTIONDECL:
{
fprintf(fp,"#FUNCTIONDECL\n");
fprintf(fp,"_%s:\n",node->id);
eval_stmt(node->children[1],hashtable);
fprintf(fp,"ret\n");
break;
}
case ARRAYINST:
fprintf(fp,"#ARRAYINST\n");
addVar(hashtable,node->id,0);
eval_expr(node->children[0],hashtable);//after this, the size of the array should be in xmm7
//four bytes per float
fprintf(fp,"movl $%d, %%eax\n", floatToInt(.5));
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm6\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"addss %%xmm6, %%xmm7\n");
fprintf(fp,"cvttss2si %%xmm7, %%edi\n");
fprintf(fp,"imul $4, %%edi\n");
//INSERT move amount of bytes into rdi register using CVTTSS2SI
fprintf(fp,"call malloc\n");//after this, rax should hold pointer to array
fprintf(fp,"movq %%rax, .var%s(%%rip)\n",node->id);//hopefully moves pointer to array variable
break;
case ASSIGNMENT:
fprintf(fp,"#ASSIGNMENT\n");
//printf("left child:%s\n",node->children[0]->id);
if(node->children[0]->num_children==0){
addVar(hashtable,node->children[0]->id,eval_expr(node->children[1],hashtable));
fprintf(fp,"movss %%xmm7, .var%s(%%rip)\n",node->children[0]->id);
}else{
//use same code, but add (size of float * array index) to variable address
eval_expr(node->children[0]->children[0],hashtable);//index should be in xmm7
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
eval_expr(node->children[1],hashtable);//value should be in xmm7
fprintf(fp,"lea .var%s(%%rip), %%rax\n",node->children[0]->id);
fprintf(fp,"cvttss2si (%%rsp), %%rdi\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"imul $32, %%rdi\n");
fprintf(fp,"lea (%%rax,%%rdi,1), %%rax\n");
fprintf(fp,"movq %%xmm7, (%%rax)\n");
}
break;
case IF:
{//scope operator for temporary IF counter
fprintf(fp,"#IF\n");
int thisLoopNumber = loopCount;
loopCount++;
eval_expr(node->children[0],hashtable);
fprintf(fp,"movl $0, %%eax\n");
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm0\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"ucomiss %%xmm0, %%xmm7\n");
fprintf(fp,"je .if%d\n",thisLoopNumber);
eval_stmt(node->children[1],hashtable);
if(node->num_children>2)
fprintf(fp,"jmp .ifEnd%d\n",thisLoopNumber);
fprintf(fp,".if%d:\n",thisLoopNumber);
if(node->num_children>2){
eval_stmt(node->children[2],hashtable);
fprintf(fp,".ifEnd%d:\n",thisLoopNumber);
}
/*
if((int)eval_expr(node->children[0],hashtable)){
eval_stmt(node->children[1],hashtable);
} else if (node->num_children>2){//check for else
eval_stmt(node->children[2],hashtable);
}*/
break;
}
case WHILE:
{
fprintf(fp,"#WHILE\n");
int thisLoopNumber = loopCount;
loopCount++;
fprintf(fp,".while%d:\n",thisLoopNumber);
eval_expr(node->children[0],hashtable);
fprintf(fp,"movl $0, %%eax\n");
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm0\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"ucomiss %%xmm0, %%xmm7\n");
fprintf(fp,"je .whileEnd%d\n",thisLoopNumber);
eval_stmt(node->children[1],hashtable);
fprintf(fp,"jmp .while%d\n",thisLoopNumber);
fprintf(fp,".whileEnd%d:\n",thisLoopNumber);
/* while((int)eval_expr(node->children[0],hashtable)){
eval_stmt(node->children[1],hashtable);
}*/
}
break;
case PRINT:
fprintf(fp,"#PRINT\n");
printf("%f\n",eval_expr(node->children[0],hashtable));
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"cvtps2pd %%xmm0, %%xmm0\n");
fprintf(fp,"movq $doubleformatstr, %%rdi\n");
fprintf(fp,"movl $1, %%eax\n");
fprintf(fp,"call printf\n");
break;
case STATEMENT:
for (i=0;i<node->num_children;i++){
eval_stmt(node->children[i],hashtable);
}
break;
default:
printf("Error, %d not a valid node type.\n", node->type);
exit(1);
}
}
float eval_expr(struct Node* node,struct entry hashtable[1000]){
float input;
switch(node->type){
case ARRAY:
fprintf(fp,"#ARRAY\n");
break;
case INPUT:
scanf("%f",&input);
fprintf(fp,"#INPUT\n");
fprintf(fp,"movq $inputstr, %%rdi #input\n");
fprintf(fp,"movq $.input, %%rsi #input\n");
fprintf(fp,"movl $0, %%eax #input\n");
fprintf(fp,"call scanf #input\n");
fprintf(fp,"movq .input(%%rip), %%xmm7 #input\n");
//printf("Inpute: %f",input);
return input;
break;
case IDENTIFIER:
//printf("Node ID: %s",node->id);
fprintf(fp,"#IDENTIFIER\n");
if(node->num_children>0){
eval_expr(node->children[0],hashtable);
fprintf(fp,"cvttss2si %%xmm7, %%rdi\n");
fprintf(fp,"lea .var%s(%%rip), %%rax\n",node->id);
fprintf(fp,"imul $32, %%rdi\n");
fprintf(fp,"lea (%%rax,%%rdi,1), %%rax\n");
fprintf(fp,"movss (%%rax), %%xmm7\n");
}else{
fprintf(fp,"movss .var%s(%%rip), %%xmm7\n",node->id);
}
return getVar(hashtable,node->id);
break;
case VALUE:
fprintf(fp,"#VALUE\n");
fprintf(fp,"movl $%d, %%eax\n", floatToInt(node->value));
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm7\n");
fprintf(fp,"addq $4, %%rsp\n");
return node->value;
break;
case PLUS:
{
fprintf(fp,"#PLUS\n");
float firstArg = eval_expr(node->children[0],hashtable);
//printf("firstArg: %f\n",firstArg);
//fprintf(fp,"movss %%xmm7, %%xmm1\n");
//fprintf(fp,"pushq %%xmm7\n");
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
//fprintf(fp,"movl $%d, %%eax\n",firstArg);
//fprintf(fp,
float secondArg = eval_expr(node->children[1],hashtable);
//printf("secondArg: %f\n",secondArg);
//fprintf(fp,"popq %%xmm1\n");
fprintf(fp,"movss (%%rsp), %%xmm1\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"addss %%xmm1, %%xmm0\n");
fprintf(fp,"movss %%xmm0, %%xmm7\n");
printf("%f PLUS %f: %f\n",firstArg,secondArg,(firstArg + secondArg));
return firstArg + secondArg;
//return (eval_expr(node->children[0],hashtable)+eval_expr(node->children[1],hashtable));
/*read eax
push watever
eval_expr(090)
read eax
eax = stack + eax*/
break;
}
case MINUS:
{
fprintf(fp,"#MINUS\n");
float firstArg = eval_expr(node->children[0],hashtable);
//printf("firstArg: %f\n",firstArg);
//fprintf(fp,"movss %%xmm7, %%xmm1\n");
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
//printf("secondArg: %f\n",secondArg);
fprintf(fp,"movss (%%rsp), %%xmm1\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"subss %%xmm0, %%xmm1\n");
fprintf(fp,"movss %%xmm1, %%xmm7\n");
printf("%f MINUS %f: %f\n",firstArg,secondArg,(firstArg - secondArg));
return firstArg - secondArg;
//return (eval_expr(node->children[0],hashtable)-eval_expr(node->children[1],hashtable));
break;
}
case SLASH:
{
fprintf(fp,"#SLASH\n");
float firstArg = eval_expr(node->children[0],hashtable);
//printf("firstArg: %f\n",firstArg);
//fprintf(fp,"movss %%xmm7, %%xmm1\n");
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
//printf("secondArg: %f\n",secondArg);
fprintf(fp,"movss (%%rsp), %%xmm1\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"divss %%xmm0, %%xmm1\n");
fprintf(fp,"movss %%xmm1, %%xmm7\n");
printf("%f SLASH %f: %f\n",firstArg,secondArg,(firstArg / secondArg));
return firstArg / secondArg;
//return (eval_expr(node->children[0],hashtable)/eval_expr(node->children[1],hashtable));
break;
}
case STAR:
{
fprintf(fp,"#STAR\n");
float firstArg = eval_expr(node->children[0],hashtable);
//printf("firstArg: %f\n",firstArg);
//fprintf(fp,"movss %%xmm7, %%xmm1\n");
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
//printf("secondArg: %f\n",secondArg);
fprintf(fp,"movss (%%rsp), %%xmm1\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"mulss %%xmm1, %%xmm0\n");
fprintf(fp,"movss %%xmm0, %%xmm7\n");
printf("%f STAR %f: %f\n",firstArg,secondArg,(firstArg * secondArg));
return firstArg * secondArg;
//return (eval_expr(node->children[0],hashtable)*eval_expr(node->children[1],hashtable));
break;
}
case LESS:
{
fprintf(fp,"#LESS\n");
float firstArg = eval_expr(node->children[0],hashtable);
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
fprintf(fp,"movss (%%rsp), %%xmm1\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"call LESS\n");
if(firstArg<secondArg)
return 1;
else
return 0;
break;
}
case GREAT:
{
fprintf(fp,"#GREAT\n");
float firstArg = eval_expr(node->children[0],hashtable);
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
fprintf(fp,"movss (%%rsp), %%xmm0\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm1\n");
fprintf(fp,"call LESS\n");
if(firstArg>secondArg)
return 1;
else
return 0;
break;
}
case LESSEQUAL:
{
fprintf(fp,"#LESSEQUAL\n");
float firstArg = eval_expr(node->children[0],hashtable);
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
fprintf(fp,"movss (%%rsp), %%xmm1\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm0\n");
fprintf(fp,"call LESSEQUAL\n");
if(firstArg<=secondArg)
return 1;
else
return 0;
break;
}
case GREATEQUAL:
{
fprintf(fp,"#GREATEQUAL\n");
float firstArg = eval_expr(node->children[0],hashtable);
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, (%%rsp)\n");
float secondArg = eval_expr(node->children[1],hashtable);
fprintf(fp,"movss (%%rsp), %%xmm0\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"movss %%xmm7, %%xmm1\n");
fprintf(fp,"call LESSEQUAL\n");
if(firstArg>=secondArg)
return 1;
else
return 0;
break;
}
case EQUAL:
if (eval_expr(node->children[0],hashtable)==eval_expr(node->children[1],hashtable))
return 1;
else
return 0;
break;
case NOTEQUAL:
if(eval_expr(node->children[0],hashtable)!=eval_expr(node->children[1],hashtable))
return 1;
else
return 0;
break;
case AND:
if(eval_expr(node->children[0],hashtable)&&eval_expr(node->children[1],hashtable))
return 1;
else
return 0;
break;
case OR:
if(eval_expr(node->children[0],hashtable)||eval_expr(node->children[1],hashtable))
return 1;
else
return 0;
break;
case NOT:if((int)eval_expr(node->children[0],hashtable)==0)
return 1;
else
return 0;
break;
default:
printf("Error, %d not a valid node type/expression.\n",node->type);
exit(1);
}
}
int hash_function(struct entry table[1000],char *varName){
int hash=((varName[0]*26)+varName[1]+varName[2])%1000;
while ((table[hash].key!=NULL)&&(strcmp(table[hash].key,varName))){
//printf("%d %d\n",table[hash].key!=NULL,table[hash].key!=varName);
//printf("Keys: Passed: \"%s\" Found: \"%s\"",varName,table[hash].key);
hash=((hash+1)%1000);
}
if(table[hash].key==NULL){
varCount+=1;
table[hash].count=varCount;
}
//printf("Hash: %d\n",hash);
return hash;
}
void addVar(struct entry table[1000],char *var,float value){
int index = hash_function(table,var);
table[index].key=var;
table[index].value=value;
//printf("Var: %s added with Value: %f\n", var,value);
}
float getVar(struct entry table[1000],char *var){
int index = hash_function(table,var);
//printf("Var: %s, Value: %f\n",var,table[index].value);
return table[index].value;
}
int main(int argc, char *argv[]) {
if(argc<2){
printf("error: pass sloth source");
return 0;
}
FILE* orig_stdin=stdin;
stdin = fopen(argv[1],"r");
if(stdin==NULL){
printf("Error: file does not exist\n");
return 0;
}
yyparse();
print_tree(tree,1);
fclose(stdin);
stdin=orig_stdin;
struct entry hashtable[1000];//should've just used a global var
int i;
for (i=0;i<1000;i++){
//printf("%d",i);
hashtable[i].key=NULL;
hashtable[i].value=0;
//hashtable[i].count=0;
}
/* hashtable debugging
addVar(hashtable,"x",1);
addVar(hashtable,"y",2);
addVar(hashtable,"a",3);
addVar(hashtable,"b",4);
addVar(hashtable,"temp",5);
addVar(hashtable,"foo",6);
addVar(hashtable,"bar",7);
addVar(hashtable,"x",12);
printf("%f\n",getVar(hashtable,"x"));
printf("%f\n",getVar(hashtable,"y"));
printf("%f\n",getVar(hashtable,"a"));
printf("%f\n",getVar(hashtable,"b"));
printf("%f\n",getVar(hashtable,"temp"));
printf("%f\n",getVar(hashtable,"foo"));
printf("%f\n",getVar(hashtable,"bar"));
*/
fp = fopen("filename.s","w");
//Initial compiled code file setup
//write initial data sections
fprintf(fp,".file \"filename.sl\"\n");
fprintf(fp,".text\n");
fprintf(fp,".globl main\n");
//fprintf(fp,".type main, @function\n");
fprintf(fp,"\n"); //newline for readability
fprintf(fp,"main:\n");
fprintf(fp,"pushq %%rbp\nmovq %%rsp, %%rbp\n");
fprintf(fp,"movl $0, %%eax\n");
eval_stmt(tree,hashtable);
fprintf(fp,"leave\n");
//fprintf(fp,"popq %%rbp\n");
fprintf(fp,"ret\n");
//LESS procedure
fprintf(fp,"LESS:\n");
fprintf(fp,"ucomiss %%xmm0, %%xmm1\n");
fprintf(fp,"jb .setOne\n");
//setZero
fprintf(fp,"movl $%d, %%eax\n", floatToInt(0.0));
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm7\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"jmp .end\n");
fprintf(fp,".setOne:\n");//setOne
fprintf(fp,"movl $%d, %%eax\n", floatToInt(1.0));
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm7\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,".end:\n");
fprintf(fp,"ret\n");
//LESSEQUAL procedure
fprintf(fp,"LESSEQUAL:\n");
fprintf(fp,"ucomiss %%xmm0, %%xmm1\n");
fprintf(fp,"jbe .setOne2\n");
//setZero
fprintf(fp,"movl $%d, %%eax\n", floatToInt(0.0));
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm7\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,"jmp .end2\n");
fprintf(fp,".setOne2:\n");//setOne
fprintf(fp,"movl $%d, %%eax\n", floatToInt(1.0));
fprintf(fp,"subq $4, %%rsp\n");
fprintf(fp,"movl %%eax, (%%rsp)\n");
fprintf(fp,"movss (%%rsp), %%xmm7\n");
fprintf(fp,"addq $4, %%rsp\n");
fprintf(fp,".end2:\n");
fprintf(fp,"ret\n");
fprintf(fp,".size main, .-main\n");
fprintf(fp,".data\n");
fprintf(fp,"doubleformatstr: .string \"double: %%f\\n\"\n");
fprintf(fp,"inputstr: .string \"%%f\"\n");
for (i=0;i<1000;i++){
if(hashtable[i].key!=NULL)
fprintf(fp,".var%s:\n\t.long 0\n.long 0\n\t.align 4\n",hashtable[i].key);
}
fprintf(fp,".input:\n\t.long 0\n\t.align 4\n");
fprintf(fp,".ident \"jstanesa sloth compiler\"\n");
//Cleanup
fclose(fp);
return 0;
}
int floatToInt(float a){
return (*(int*) &a);
}