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
21dd85b1
There was a problem fetching the pipeline stages.
Verified
Commit
21dd85b1
authored
6 years ago
by
Recolic Keghart
Browse files
Options
Downloads
Patches
Plain Diff
fix id_local
parent
f4fae353
No related branches found
No related tags found
No related merge requests found
Pipeline
#393
canceled with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/chocopy/pa3/CodeGenImpl.java
+18
-16
18 additions, 16 deletions
src/main/java/chocopy/pa3/CodeGenImpl.java
with
18 additions
and
16 deletions
src/main/java/chocopy/pa3/CodeGenImpl.java
+
18
−
16
View file @
21dd85b1
...
...
@@ -214,9 +214,7 @@ public class CodeGenImpl extends CodeGenBase {
betterBackend
.
emitFunctionBegin
(
funcInfo
.
getFuncName
(),
null
);
stmtAnalyzer
.
initArgsMap
(
funcInfo
.
getParams
());
for
(
StackVarInfo
local
:
funcInfo
.
getLocals
())
{
// TODO: Do some thing to local variable
}
stmtAnalyzer
.
initLocalVars
(
funcInfo
.
getLocals
(),
-
3
*
backend
.
getWordSize
());
// Emit code for function stmts
for
(
Stmt
stmt
:
funcInfo
.
getStatements
())
{
...
...
@@ -273,11 +271,21 @@ public class CodeGenImpl extends CodeGenBase {
// Extend StackVarInfo to add more info.
private
Map
<
String
,
Integer
>
funcArgs
Map
=
new
HashMap
<>();
// Map from StackVarInfo to offset of FP.
private
Map
<
String
,
Integer
>
local
Map
=
new
HashMap
<>();
// Map from StackVarInfo to offset of FP.
public
void
initArgsMap
(
List
<
String
>
args
)
{
for
(
int
i
=
0
;
i
<
args
.
size
();
++
i
)
{
int
offset
=
(
args
.
size
()
-
i
-
1
)
*
backend
.
getWordSize
();
funcArgsMap
.
put
(
args
.
get
(
i
),
offset
);
localMap
.
put
(
args
.
get
(
i
),
offset
);
}
}
public
void
initLocalVars
(
List
<
StackVarInfo
>
locals
,
int
firstLocalOffset
)
{
for
(
int
i
=
0
;
i
<
locals
.
size
();
++
i
)
{
StackVarInfo
theLocal
=
locals
.
get
(
i
);
theLocal
.
getInitialValue
().
dispatch
(
this
);
// The literal initial value is pushed to stack. That's the local!
// No need to push it again.
int
offset
=
firstLocalOffset
-
i
*
backend
.
getWordSize
();
localMap
.
put
(
theLocal
.
getVarName
(),
offset
);
}
}
...
...
@@ -336,9 +344,9 @@ public class CodeGenImpl extends CodeGenBase {
backend
.
emitSW
(
tmpReg
,
globalVarInfo
.
getLabel
(),
tmpReg2
,
"ASSIGN: Assign to global var"
);
}
else
if
(
symbolInfo
instanceof
StackVarInfo
)
{
StackVarInfo
stackVarInfo
=
(
StackVarInfo
)
symbolInfo
;
if
(
funcArgs
Map
.
containsKey
(
stackVarInfo
.
getVarName
()))
{
if
(
local
Map
.
containsKey
(
stackVarInfo
.
getVarName
()))
{
// is a function argument.
backend
.
emitSW
(
tmpReg
,
FP
,
funcArgs
Map
.
get
(
stackVarInfo
.
getVarName
()),
"Assign to function param var"
);
backend
.
emitSW
(
tmpReg
,
FP
,
local
Map
.
get
(
stackVarInfo
.
getVarName
()),
"Assign to function param var"
);
}
else
throw
new
RuntimeException
(
"ASSIGN to stack variable: NOT IMPLEMENTED: StackVarInfo not implemented."
);
...
...
@@ -474,18 +482,12 @@ public class CodeGenImpl extends CodeGenBase {
betterBackend
.
emitPush
(
A0
,
"Ident: GlobalVar"
);
}
else
if
(
ident
instanceof
StackVarInfo
){
// local variable
StackVarInfo
identVar
=
(
StackVarInfo
)
ident
;
if
(
funcArgs
Map
.
containsKey
(
identVar
.
getVarName
()))
{
// is a function arg.
backend
.
emitLW
(
T6
,
FP
,
funcArgs
Map
.
get
(
identVar
.
getVarName
()),
"Idnet:get FuncArg identifier value to reg"
);
if
(
local
Map
.
containsKey
(
identVar
.
getVarName
()))
{
// is a function arg
or local var
.
backend
.
emitLW
(
T6
,
FP
,
local
Map
.
get
(
identVar
.
getVarName
()),
"Idnet:get FuncArg identifier value to reg"
);
betterBackend
.
emitPush
(
T6
,
"Ident: Arg"
);
return
null
;
// DO NOT RUN THE FUCKING SILLY CODE BELOW
}
// TODO: Function arg works, but local var won't work.
if
(
id
.
getInferredType
().
equals
(
SymbolType
.
INT_TYPE
))
{
IntegerLiteral
value
=
(
IntegerLiteral
)
(
identVar
.
getInitialValue
());
backend
.
emitLI
(
A0
,
value
.
value
,
"Load initial value of StackVar"
);
betterBackend
.
emitPush
(
A0
,
"Ident: LocalVar"
);
}
}
return
null
;
}
...
...
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