Skip to content

Commit

Permalink
Added Support for While(Loop) Statements in HLL
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsingh-24 committed Nov 3, 2022
1 parent 47ee3e2 commit 3cf19f5
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 59 deletions.
18 changes: 13 additions & 5 deletions compiler/compiler-output/data.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
259 1
260 2
261 3
264 10
265 100
262 1
263 1
264 1
265 1
266 1
267 1
268 11
270 1
272 1
274 1
276 1
278 1
280 1
3 changes: 3 additions & 0 deletions compiler/compiler-output/declarations-address-mapping.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
c 256
b 257
a 258
k 259
j 260
i 261
67 changes: 48 additions & 19 deletions compiler/compiler-output/final-assembly-code.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
LOAD R7 259 R2
LOAD R7 262 R2
STORE R7 261 R2
LOAD R7 263 R2
STORE R7 260 R2
LOAD R7 264 R2
STORE R7 259 R2
LOAD R7 265 R2
STORE R7 258 R2
LOAD R7 260 R2
LOAD R7 266 R2
STORE R7 257 R2
LOAD R7 261 R2
LOAD R7 267 R2
STORE R7 256 R2
LOAD R1 258 R2
LOAD R3 257 R2
LOAD R1 261 R2
LOAD R3 268 R2
LT R1 R1 R3
STORE R1 262 R2
LOAD R7 262 R2
MVI R6 28
STORE R1 269 R2
LOAD R7 269 R2
MVI R6 57
JZ R7 R6
LOAD R1 261 R2
LOAD R3 270 R2
ADD R1 R1 R3
STORE R1 271 R2
LOAD R7 271 R2
STORE R7 261 R2
LOAD R1 260 R2
LOAD R3 272 R2
ADD R1 R1 R3
STORE R1 273 R2
LOAD R7 273 R2
STORE R7 260 R2
LOAD R1 259 R2
LOAD R3 274 R2
ADD R1 R1 R3
STORE R1 275 R2
LOAD R7 275 R2
STORE R7 259 R2
LOAD R1 258 R2
LOAD R3 276 R2
ADD R1 R1 R3
STORE R1 277 R2
LOAD R7 277 R2
STORE R7 258 R2
LOAD R1 257 R2
LOAD R3 256 R2
LT R1 R1 R3
STORE R1 263 R2
LOAD R7 263 R2
MVI R6 24
JZ R7 R6
LOAD R7 264 R2
LOAD R3 278 R2
ADD R1 R1 R3
STORE R1 279 R2
LOAD R7 279 R2
STORE R7 257 R2
MVI R6 24
JUMP R6
LOAD R7 265 R2
LOAD R1 256 R2
LOAD R3 280 R2
ADD R1 R1 R3
STORE R1 281 R2
LOAD R7 281 R2
STORE R7 256 R2
MVI R6 28
MVI R6 12
JUMP R6
HLT
73 changes: 49 additions & 24 deletions compiler/compiler-output/intermediate-assembly-code.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
LOAD R7 259 R2
LOAD R7 262 R2
STORE R7 261 R2
LOAD R7 263 R2
STORE R7 260 R2
LOAD R7 264 R2
STORE R7 259 R2
LOAD R7 265 R2
STORE R7 258 R2
LOAD R7 260 R2
LOAD R7 266 R2
STORE R7 257 R2
LOAD R7 261 R2
LOAD R7 267 R2
STORE R7 256 R2
LOAD R1 258 R2
LOAD R3 257 R2
L0 :
LOAD R1 261 R2
LOAD R3 268 R2
LT R1 R1 R3
STORE R1 262 R2
LOAD R7 262 R2
MVI R6 L0
STORE R1 269 R2
LOAD R7 269 R2
MVI R6 L1
JZ R7 R6
LOAD R1 261 R2
LOAD R3 270 R2
ADD R1 R1 R3
STORE R1 271 R2
LOAD R7 271 R2
STORE R7 261 R2
LOAD R1 260 R2
LOAD R3 272 R2
ADD R1 R1 R3
STORE R1 273 R2
LOAD R7 273 R2
STORE R7 260 R2
LOAD R1 259 R2
LOAD R3 274 R2
ADD R1 R1 R3
STORE R1 275 R2
LOAD R7 275 R2
STORE R7 259 R2
LOAD R1 258 R2
LOAD R3 276 R2
ADD R1 R1 R3
STORE R1 277 R2
LOAD R7 277 R2
STORE R7 258 R2
LOAD R1 257 R2
LOAD R3 256 R2
LT R1 R1 R3
STORE R1 263 R2
LOAD R7 263 R2
MVI R6 L2
JZ R7 R6
LOAD R7 264 R2
LOAD R3 278 R2
ADD R1 R1 R3
STORE R1 279 R2
LOAD R7 279 R2
STORE R7 257 R2
MVI R6 L3
JUMP R6
L2 :

L3 :
LOAD R7 265 R2
LOAD R1 256 R2
LOAD R3 280 R2
ADD R1 R1 R3
STORE R1 281 R2
LOAD R7 281 R2
STORE R7 256 R2
MVI R6 L1
MVI R6 L0
JUMP R6
L0 :

L1 :
HLT
2 changes: 1 addition & 1 deletion compiler/compiler-output/memory-image
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
v2.0 raw
11C40207 19C40205 11C40209 19C40203 11C4020B 19C40201 10440205 10C40203 58423000 1844020D 11C4020D 41800039 680E6000 10440203 10C40201 58423000 1844020F 11C4020F 41800031 680E6000 11C40211 19C40203 41800031 60006000 11C40213 19C40201 41800039 60006000 00000000 230*0 1 2 3 2*0 A 64
11C4020D 19C4020B 11C4020F 19C40209 11C40211 19C40207 11C40213 19C40205 11C40215 19C40203 11C40217 19C40201 1044020B 10C40219 58423000 1844021B 11C4021B 41800073 680E6000 1044020B 10C4021D 20423000 1844021F 11C4021F 19C4020B 10440209 10C40221 20423000 18440223 11C40223 19C40209 10440207 10C40225 20423000 18440227 11C40227 19C40207 10440205 10C40229 20423000 1844022B 11C4022B 19C40205 10440203 10C4022D 20423000 1844022F 11C4022F 19C40203 10440201 10C40231 20423000 18440233 11C40233 19C40201 41800019 60006000 00000000 204*0 1 1 1 1 1 1 B 1*0 1 1*0 1 1*0 1 1*0 1 1*0 1 1*0 1
12 changes: 12 additions & 0 deletions compiler/fileutils.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* Responsible for substituting Labels generated for lines(like L0, L1, L2) with Address of actual statement in memory.
Example:
JUMP L0
L0: ADD R1, R2, R3
will be replaced with -
JUMP 1
ADD R1, R2, R3
*/

#include <iostream>
#include <fstream>
#include <map>
Expand Down
23 changes: 14 additions & 9 deletions compiler/input-code/input1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ global
// assignment statements

def
a, b, c: int
i,j,k,a,b,c: int
end

i := 1;
j := 1;
k := 1;
a := 1;
b := 2;
c := 3;
b := 1;
c := 1;

if a < b:
if b < c:
b := 10
end;
c := 100
end
while i < 11:
i := i + 1;
j := j + 1;
k := k + 1;
a := a + 1;
b := b + 1;
c := c + 1
end

// Arithmetic Expression - ans = 155 (0x 9B)
// c := (a + b) + (100 - 75)
Expand Down
1 change: 1 addition & 0 deletions compiler/lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int return INT;
\+ return PLUS;
\) return RIGHT_PAREN;
; return SEMICOLON;
while return WHILE;

{digit}{digit}*(E[+-]?{digit}{digit}*)? {
int temp = atoi(yytext);
Expand Down
29 changes: 28 additions & 1 deletion compiler/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
}

%locations
%token ASSIGN COMMA COLON DEF ELSE END EQ GLOBAL GE GT ID IF INT INT_CONST LEFT_PAREN LE LT MINUS NE PLUS RIGHT_PAREN SEMICOLON
%token ASSIGN COMMA COLON DEF ELSE END EQ GLOBAL GE GT ID IF INT INT_CONST LEFT_PAREN LE LT MINUS NE PLUS RIGHT_PAREN SEMICOLON WHILE

%left PLUS MINUS
%left AND OR
Expand Down Expand Up @@ -123,6 +123,7 @@ stmtList: stmtList SEMICOLON stmt

stmt: assignmentStmt
| ifStmt
| whileStmt
;

// TODO: IF STMT, WHILE STMT and bExp relop bExp, Add Jump Statement in processor
Expand Down Expand Up @@ -183,6 +184,32 @@ elsePart: ELSE stmtList
}
;

whileStmt: WHILE {
string lineNumber1 = lineTagGenerate();
string lineNumber2 = lineTagGenerate();

code << lineNumber1 << " :" << endl;
st.push(lineNumber2);
st.push(lineNumber1);
st.push(lineNumber2);
}
bExp {
string bExpResult = $<wrapper>3->label;
code << "LOAD R7 " << bExpResult << " R2" << endl;
code << "MVI R6 " << st.top() << endl;
code << "JZ R7 R6" << endl; // We will substitute label with address for PC later.
st.pop();
}
COLON stmtList {
code << "MVI R6 " << st.top() << endl;
code << "JUMP R6" << endl;
st.pop();
code << st.top() << " :" << endl;
st.pop();
}
END
;

exp: exp PLUS exp {
$<wrapper>$ = printOperationCycle($<wrapper>1->label, $<wrapper>3->label,"ADD");
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/stringwrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
The main Wrapper class which holds addresses(labels) of temporary variables generated in Three Address Code generation.
*/

#include <iostream>
using namespace std;
class Wrapper {
Expand Down

0 comments on commit 3cf19f5

Please sign in to comment.