Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs164_lab3_cg
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
recolic-hust
cs164_lab3_cg
Commits
e6c9f9ad
There was an error fetching the commit references. Please try again later.
Verified
Commit
e6c9f9ad
authored
6 years ago
by
Recolic Keghart
Browse files
Options
Downloads
Patches
Plain Diff
reconstruct done
parent
e77ef040
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/chocopy/pa3/CodeGenImpl.java
+4
-28
4 additions, 28 deletions
src/main/java/chocopy/pa3/CodeGenImpl.java
with
4 additions
and
28 deletions
src/main/java/chocopy/pa3/CodeGenImpl.java
+
4
−
28
View file @
e6c9f9ad
...
...
@@ -39,11 +39,15 @@ public class CodeGenImpl extends CodeGenBase {
}
public
void
emitPush
(
RiscVBackend
.
Register
reg
,
String
comment
)
{
if
(
comment
==
null
)
comment
=
""
;
comment
=
"PUSH: "
+
comment
;
backend
.
emitADDI
(
SP
,
SP
,
-
1
*
backend
.
getWordSize
(),
comment
);
backend
.
emitSW
(
reg
,
SP
,
0
,
comment
);
}
public
void
emitPop
(
RiscVBackend
.
Register
reg
,
String
comment
)
{
if
(
comment
==
null
)
comment
=
""
;
comment
=
"POP: "
+
comment
;
if
(
reg
!=
null
)
backend
.
emitLW
(
reg
,
SP
,
0
,
comment
);
backend
.
emitADDI
(
SP
,
SP
,
backend
.
getWordSize
(),
comment
);
...
...
@@ -491,9 +495,7 @@ public class CodeGenImpl extends CodeGenBase {
switch
(
binaryExpr
.
operator
)
{
case
"+"
:
if
(
binaryExpr
.
left
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -504,9 +506,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"-"
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -517,9 +517,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"*"
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -530,9 +528,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"//"
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -543,9 +539,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"%"
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -557,9 +551,7 @@ public class CodeGenImpl extends CodeGenBase {
case
"=="
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
BOOL_TYPE
)
||
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
)
)
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -573,9 +565,7 @@ public class CodeGenImpl extends CodeGenBase {
case
"!="
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
BOOL_TYPE
)
||
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
)
)
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -589,9 +579,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"<="
:
if
(
binaryExpr
.
left
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -603,9 +591,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
">="
:
if
(
binaryExpr
.
left
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -617,9 +603,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"<"
:
if
(
binaryExpr
.
left
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -630,9 +614,7 @@ public class CodeGenImpl extends CodeGenBase {
}
case
">"
:
if
(
binaryExpr
.
left
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
...
...
@@ -643,13 +625,11 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"and"
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
BOOL_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T1
,
"Load operand 1 into t1"
);
Label
skipSecond
=
generateLocalLabel
();
backend
.
emitBEQZ
(
T1
,
skipSecond
,
"If operand 1 is FALSE, don't eval second"
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
backend
.
emitAND
(
T1
,
T1
,
T0
,
"AND operands"
);
...
...
@@ -659,13 +639,11 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"or"
:
if
(
binaryExpr
.
getInferredType
().
equals
(
SymbolType
.
BOOL_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
binaryExpr
.
left
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 1 into t0"
);
Label
skipSecond
=
generateLocalLabel
();
backend
.
emitBNEZ
(
T0
,
skipSecond
,
"If first is not zero (it's true), skip second"
);
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for second operand"
);
binaryExpr
.
right
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand 2 into t0"
);
backend
.
emitOR
(
T0
,
T1
,
T0
,
"OR operands"
);
...
...
@@ -683,7 +661,6 @@ public class CodeGenImpl extends CodeGenBase {
switch
(
unaryExpr
.
operator
)
{
case
"-"
:
if
(
unaryExpr
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
unaryExpr
.
operand
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand into t0"
);
...
...
@@ -693,7 +670,6 @@ public class CodeGenImpl extends CodeGenBase {
}
case
"not"
:
if
(
unaryExpr
.
getInferredType
().
equals
(
SymbolType
.
BOOL_TYPE
))
{
backend
.
emitADDI
(
SP
,
SP
,
-
4
,
"Increment stack ptr for first operand"
);
unaryExpr
.
operand
.
dispatch
(
this
);
betterBackend
.
emitPop
(
T0
,
"Load operand into t0"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment