-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgdb_input.html
592 lines (557 loc) · 18.2 KB
/
gdb_input.html
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
<!Doctype html>
<html lang="en">
<head>
<title>Input Files</title>
<meta charset="UTF-8">
<!--<link rel="stylesheet" href="css/bootstrap.min.css">-->
<link rel="stylesheet" href="css/style_new.css">
<script src="js/jquery-1.12.1.min.js" charset="utf-8"></script>
<link rel="stylesheet" href="js/embed-2cd369fa1c0830bd3aa06c21d4f14a13e060d2d31bbaae740f4af4.css"><div id="gist28627206" class="gist">
<link rel="stylesheet" href="js/embed-cbe5b40fa72b0964f90d4919c2da8f8f94d7c9f6c2aa49c07f6fa3.css"><div id="gist28627206" class="gist">
<script src="js/bootstrap.min.js" charset="utf-8"></script>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<div class="container">
<header id="navtop">
<a href="index.html" class="logo fleft"><img src="img/logo.png" alt=""></a>
<nav class="fright">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<!-- <li><a href="help.html">Help</a></li> -->
<li><a href="roadmap.html">Roadmap</a></li>
<li><a href="documentation.html" class="navactive">Documentation</a></li>
</ul>
</nav>
</header>
<body>
<div class="Services-page main grid-wrap">
<header class="grid col-full">
<hr/>
<p class="fleft">Input Files</p>
<br>
<br>
</header>
<aside class="grid col-one-quarter mq2-col-full sticky_sidebar" style="position: static;">
<menu>
<ul>
<li><a class="sec" href="#nav-lex">lex.l file</a></li>
<li><a class="sec" href="#nav-parser">parser.y file</a></li>
<li><a class="sec" href="#nav-function">infixtoprefix function</a></li>
<li><a class="sec" href="#nav-input">input file</a></li>
<li><a class="sec" href="#nav-treeh">tree.h file</a></li>
<li><a class="sec" href="#nav-treec">tree.c file</a></li>
</ul>
</menu>
<!-- <a class="button" href="">Download as PDF</a> -->
</aside>
<section class="grid col-three-quarters mq2-col-full">
<div class="grid-wrap">
<article class="grid col-full" id="nav-lex">
<h2>lex.l file</h2>
<div class="well" style="background-color: rgb(243, 239, 234); border-radius: 5px; padding: 10px;">
<pre>
%{
#include <stdio.h>
#include "y.tab.h"
#include "tree.h"
#include <stdlib.h>
int number;
char*temp;
%}
%%
begin {return begin;}
end {return END;}
break {return BREAK;}
continue {return CONTINUE;}
read {return READ;}
write {return WRITE;}
then {return then;}
else {return ELSE;}
endif {return endif;}
if {return IF;}
do {return DO;}
endwhile {return endwhile;}
while {return WHILE;}
[0-9]+ {return NUM;}
[a-z]+ {yylval.p=createTree(-1,tINT,yytext,tVAR,NULL,NULL,NULL); return ID;}
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return MUL;}
"/" {return DIV;}
"<=" {return LE;}
">=" {return GE;}
"<" {return LT;}
">" {return GT;}
"!=" {return NE;}
"==" {return EQ;}
"=" {return ASSIGN;}
";" {return EOL;}
[ \t] {}
[' '] {}
[()] {return *yytext;}
[\n] {}
. {}
%%
int yywrap(void) {
return 1;
}
</pre>
</div>
</article>
<article class="grid col-full" id="nav-parser">
<h2>parser.y file</h2>
<div class="well" style="background-color: rgb(243, 239, 234); border-radius: 5px; padding: 10px;">
<pre>
%{
#include <stdlib.h>
#include <stdio.h>
//#define YYSTYPE tnode*
#include "tree.h"
#include "tree.c"
extern struct tnode* idptr;
FILE* targetFile;
int yylex(void);
extern FILE *yyin;
extern char* yytext;
%}
%union{
struct tnode* p;
}
%token NUM PLUS MINUS MUL DIV ID begin END READ WRITE EOL IF then ELSE endif WHILE DO endwhile GT LT LE GE NE EQ ASSIGN BREAK CONTINUE
%type<p> expr Slist Stmt Breakstmt Continuestmt InputStmt OutputStmt AsgStmt Ifstmt Whilestmt ID
%left PLUS MINUS
%left MUL DIV
%nonassoc GT LT LE GE EQ NE
%%
Program : begin Slist END EOL {fprintf(targetFile,"%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\nMOV SP, 4121\n",0,2056,0,0,0,0,1,0);
//codeGen(targetFile,$2);
infixtoprefix(targetFile,$2);
printExit(targetFile);
return 0;
}
| begin END EOL {printf("Empty program\n;");return 0;}
;
Slist : Slist Stmt {$$=createTree(-1,-1,NULL,tCONNECT,$1,$2,NULL);}
| Stmt {$$=$1;}
;
Stmt : InputStmt {$$=$1;}
| OutputStmt {$$=$1;}
| AsgStmt {$$=$1;}
| Ifstmt {$$=$1;}
| Whilestmt {$$=$1;}
| Breakstmt {$$=$1;}
| Continuestmt {$$=$1;}
;
Breakstmt : BREAK EOL {$$=createTree(-1,-1,NULL,tBREAK,NULL,NULL,NULL);}
;
Continuestmt : CONTINUE EOL {$$=createTree(-1,-1,NULL,tCONTINUE,NULL,NULL,NULL);}
;
InputStmt : READ '(' ID ')' EOL {$$=createTree(-1,-1,NULL,tREAD,$3,NULL,NULL);}
;
OutputStmt : WRITE '(' expr ')' EOL {$$=createTree(-1,-1,NULL,tWRITE,$3,NULL,NULL);}
;
AsgStmt : ID ASSIGN expr EOL {$$ = createTree(-1,-1,NULL,tASSIGN,$1,$3,NULL);}
;
Ifstmt : IF '(' expr ')' then Slist ELSE Slist endif EOL {$$=createTree(-1,-1,NULL,tIF,$3,$6,$8);}
| IF '(' expr ')' then Slist endif EOL {$$=createTree(-1,-1,NULL,tIF,$3,$6,NULL);}
;
Whilestmt : WHILE '(' expr ')' DO Slist endwhile EOL {$$=createTree(-1,-1,NULL,tWHILE,$3,$6,NULL);}
;
expr : expr PLUS expr {$$ = createTree(-1,-1,"+",tADD,$1,$3,NULL);}
| expr MINUS expr {$$ = createTree(-1,-1,"-",tSUB,$1,$3,NULL);}
| expr MUL expr {$$ = createTree(-1,-1,"*",tMUL,$1,$3,NULL);}
| expr DIV expr {$$ = createTree(-1,-1,"/",tDIV,$1,$3,NULL);}
| '(' expr ')' {$$ = $2;}
| expr LT expr {$$ = createTree(-1,-1,NULL,tLT,$1,$3,NULL);}
| expr GT expr {$$ = createTree(-1,-1,NULL,tGT,$1,$3,NULL);}
| expr LE expr {$$ = createTree(-1,-1,NULL,tLE,$1,$3,NULL);}
| expr GE expr {$$ = createTree(-1,-1,NULL,tGE,$1,$3,NULL);}
| expr NE expr {$$ = createTree(-1,-1,NULL,tNE,$1,$3,NULL);}
| expr EQ expr {$$ = createTree(-1,-1,NULL,tEQ,$1,$3,NULL);}
| NUM {$$ = createTree(atoi(yytext),tINT,NULL,tNUM,NULL,NULL,NULL);}
| ID {$$=$1;}
;
%%
yyerror(char const *s)
{
printf("yyerror %s and %s",s,yytext);
}
int main(int argc, char*argv[]) {
targetFile=fopen("targetFile.xsm","w");
if(targetFile==NULL){
printf("file error\n");
}
yyin=fopen(argv[1],"r");
yyparse();
fclose(targetFile);
return 0;
}
</pre>
</div>
</article>
<article class="grid col-full" id="nav-function">
<h2>infixtoprefix function</h2>
<div class="well" style="background-color: rgb(243, 239, 234); border-radius: 5px; padding: 10px;">
<pre>
void infixtoprefix(FILE* fp, struct tnode* root){
if(root!=NULL){
printf("%s ",root->symbol);
fprintf(fp,"%s ",root->symbol);
infixtoprefix(fp,root->left);
infixtoprefix(fp,root->right);
}
}
</pre>
</div>
</article>
<article class="grid col-full" id="nav-input">
<h2>input file</h2>
<div class="well" style="background-color: rgb(243, 239, 234); border-radius: 5px; padding: 10px; padding-bottom: 3px;">
<pre>
abc+(bcd-efg)*hij
</pre>
</div>
</article>
<article class="grid col-full" id="nav-treeh">
<h2>tree.h file</h2>
<p>This is the header file for tree.c file</p>
<div class="well" style="background-color: rgb(243, 239, 234); border-radius: 5px; padding: 10px; padding-bottom: 3px;">
<pre>
#define tNUM 0
#define tVAR 1
#define tADD 2
#define tSUB 3
#define tMUL 4
#define tDIV 5
#define tREAD 6
#define tWRITE 7
#define tASSIGN 8
#define tCONNECT 9
#define tINT 10
#define tLT 11
#define tGT 12
#define tLE 13
#define tGE 14
#define tNE 15
#define tEQ 16
#define tBOOL 17
#define tWHILE 18
#define tIF 19
#define tBREAK 20
#define tCONTINUE 21
#define varLoc 4096
#define SPLoc 4121
typedef struct tnode {
int val; // value of a number for NUM nodes.
int type; //type of variable
char* symbol; //name of a variable for ID nodes
int nodetype; // information about non-leaf nodes - read/write/connector/+/* etc.
struct tnode *left,*right,*third; //left and right branches
}tnode;
void printExit(FILE* targetFile);
int getLabel();
int getReg();
void freeReg();
void printRead(FILE* targetFile, int varAddr);
void printWrite(FILE* targetFile, int regNum);
int getVarAddr(struct tnode*root);
int codeGen(FILE* fp, struct tnode* root);
void infixtoprefix(FILE* fp, struct tnode* root);
/*Create a node tnode*/
struct tnode* createTree(int val, int type, char* c, int nodeType, struct tnode *l, struct tnode *r,struct tnode *third);
</pre>
</div>
</article>
<article class="grid col-full" id="nav-treec">
<h2>tree.c file</h2>
<p>This file contains the helper functions for the yacc file, like the createTree(), infixtoprefix() etc.</p>
<p>The yacc file imports the tree.c file and tree.h file</p>
<div class="well" style="background-color: rgb(243, 239, 234); border-radius: 5px; padding: 10px; padding-bottom: 3px;">
<pre>
int reg=0;
int label_no=0;
int stacktop[20]; //for continue
int stackend[20]; //for break
int stack1=-1; //for stacktop
int stack2=-1; //for stackend
struct tnode* createTree(int val, int type, char* c, int nodeType, struct tnode *l, struct tnode *r, struct tnode* third){
switch(nodeType){
case tADD:
case tSUB:
case tMUL:
case tDIV:
if(l->type!= r->type || l->type != tINT){
printf("\ntype mismatch error\n");
exit(0);
}
else{
type=tINT;
}
break;
case tLT:
case tGT:
case tLE:
case tGE:
case tNE:
case tEQ:
if(l->type!= r->type || l->type !=tINT){
printf("\ntype mismatch error\n");
exit(0);
}
else{
type=tBOOL;
}break;
case tASSIGN:
if(r->type!=tINT || l->nodetype!=tVAR){
printf("\ntype mismatch error\n");
exit(0);
}
break;
case tWHILE:
case tIF:
if(l->type!=tBOOL){
printf("\ntype mismatch error\n");
exit(0);
}
break;
}
struct tnode *temp;
temp = (struct tnode*)malloc(sizeof(struct tnode));
temp->val = val;
temp->type = type;
temp->symbol = c;
temp->nodetype = nodeType;
temp->left=l;
temp->right = r;
temp->third=third;
return temp;
}
void printExit(FILE* targetFile){
fprintf(targetFile,"INT 10\n");
}
int getLabel(){
int curr_label=label_no;
label_no++;
return curr_label;
}
int getReg(){
int r=reg;
reg++;
if(r>20){
printf("out of registers\n");
exit(1);
}
return r;
}
void freeReg(){
reg--;
if(reg<0){
reg=0;
}
}
void printRead(FILE* targetFile, int varAddr){
int r=getReg();
fprintf(targetFile,"MOV R%d, \"Read\"\nPUSH R%d\nMOV R%d, -1\nPUSH R%d\nMOV R%d,%d\nPUSH R%d\nPUSH R%d\nPUSH R%d\nCALL 0\nPOP R%d\nPOP R%d\nPOP R%d\nPOP R%d\nPOP R%d\n",r,r,r,r,r,varAddr,r,r,r,r,r,r,r,r);
freeReg();
}
void printWrite(FILE* targetFile, int regNum){
int r=getReg();
fprintf(targetFile,"MOV R%d, \"Write\"\nPUSH R%d\nMOV R%d, -2\nPUSH R%d\nMOV R%d,R%d\nPUSH R%d\nPUSH R%d\nPUSH R%d\nCALL 0\nPOP R%d\nPOP R%d\nPOP R%d\nPOP R%d\nPOP R%d\nBRKP\n",r,r,r,r,r,regNum,r,r,r,r,r,r,r,r);
freeReg();
}
int getVarAddr(struct tnode*root){
char* symbol=root->symbol;
int varAddr = 4096 + (symbol[0] - 'a');
return varAddr;
}
void infixtoprefix(FILE* fp, struct tnode* root){
if(root!=NULL){
printf("%s ",root->symbol);
fprintf(fp,"%s ",root->symbol);
infixtoprefix(fp,root->left);
infixtoprefix(fp,root->right);
}
}
int codeGen(FILE* fp, struct tnode* root){
int r,sourceReg,destReg,varAddr,label_1,label_2,label_3;
char* symbol;
if(root==NULL) return -1;
switch(root->nodetype){
case tCONNECT:
codeGen(fp,root->left);
codeGen(fp,root->right);
return -1;
case tREAD:
if(root->left==NULL) {printf("\ninvalid read stmt\n");exit(0);}
symbol = (root->left)->symbol;
varAddr= 4096 + (symbol[0] - 'a');
//printf("var addr = %d\n",varAddr);
printRead(fp,varAddr);
return -1;
case tNUM:
r=getReg();
fprintf(fp,"MOV R%d, %d\n",r,root->val);
return r;
case tVAR:
r=getReg();
symbol=root->symbol;
varAddr = 4096 + (symbol[0] - 'a');
fprintf(fp,"MOV R%d,[%d]\n",r,varAddr);
return r;
case tWRITE:
r=codeGen(fp,root->left);
printWrite(fp,r);
return -1;
case tASSIGN:
sourceReg=getVarAddr(root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"MOV [%d],R%d\n",sourceReg,destReg);
return -1;
case tADD:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"ADD R%d,R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tSUB:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"SUB R%d,R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tMUL:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"MUL R%d,R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tDIV:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"DIV R%d,R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tLT:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"LT R%d, R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tGT:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"GT R%d, R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tLE:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"LE R%d, R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tGE:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"GE R%d, R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tNE:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"NE R%d, R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tBREAK:
if(stack2<0) {printf("\ninvalid Break statement\n");exit(0);}
fprintf(fp,"JMP L%d\n",stackend[stack2]);
return -1;
case tCONTINUE:
if(stack1<0) {printf("\ninvalid continue statement\n");exit(0);}
fprintf(fp,"JMP L%d\n",stacktop[stack1]);
return -1;
case tEQ:
sourceReg=codeGen(fp,root->left);
destReg=codeGen(fp,root->right);
fprintf(fp,"EQ R%d, R%d\n",sourceReg,destReg);
freeReg();
return sourceReg;
case tWHILE:
label_1 = getLabel();
label_2 = getLabel();
fprintf (fp, "L%d:\n", label_1); // Place the first label here.
stack1++;
stacktop[stack1]=label_1;
stack2++;
stackend[stack2]=label_2;
sourceReg=codeGen(fp,root->left);
fprintf(fp,"JZ R%d, L%d\n",sourceReg,label_2);
codeGen(fp,root->right);
fprintf(fp, "JMP L%d\n", label_1); // return to the beginning of the loop.
fprintf(fp, "L%d:\n", label_2); // Place the second label here
stack1--;
stack2--;
return -1;
case tIF:
//label_1 = getLabel();
if(root->third!=NULL)
label_2 = getLabel();
label_3 = getLabel();
//fprintf (fp, "L%d:\n", label_1); // Place the first label here.
//if
sourceReg=codeGen(fp,root->left);
if(root->third!=NULL)
fprintf(fp,"JZ R%d, L%d\n",sourceReg,label_2);
else
fprintf(fp,"JZ R%d, L%d\n",sourceReg,label_3);
//then
codeGen(fp,root->right);
fprintf(fp, "JMP L%d\n", label_3); // jump to endif.
//else
if(root->third!=NULL){
fprintf(fp, "L%d:\n", label_2); // Place the second label here
//else
codeGen(fp,root->third);
fprintf(fp, "JMP L%d\n", label_3); // jump to endif.
}
fprintf(fp, "L%d:\n", label_3); // Place the third label here
return -1;
defualt:
printf("\nsome error in codegen fn : %d\n",root->nodetype);
exit(0);
}
}
</pre>
</div>
</article>
</div>
</section>
</div>
</div>
</body>
<footer class="center part clearfix">
<ul class="grid col-one-third social">
<li><a href="http://github.com/silcnitc">Github</a></li>
<li> <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="img/creativecommons.png" /></a></li>
</ul>
<div class="grid col-one-third" style="color:black;">
<p style="font-weight: bold;">Contributed By : <a href="https://www.linkedin.com/in/in/siddharth-ravi-6a1876198/">S R Siddharth</a>
</p>
</div>
<nav class="grid col-one-third ">
<ul >
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<!-- <li><a href="uc.html">Contact</a></li> -->
</ul>
</nav>
<br>
</footer>
<script>window.jQuery || document.write('<script src="js/jquery-1.7.2.min.js"><\/script>')</script>
<script src="js/scripts.js"></script>
<script src="js/inject.js"></script>
</html>