Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs444
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Xun Yang
cs444
Commits
1aae298a
Commit
1aae298a
authored
4 years ago
by
Nicholas Wesley Robinson
Browse files
Options
Downloads
Patches
Plain Diff
more expr and literals
parent
a7da5205
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ExprPrimaryNodes.py
+50
-8
50 additions, 8 deletions
ExprPrimaryNodes.py
UnitNodes.py
+29
-5
29 additions, 5 deletions
UnitNodes.py
with
79 additions
and
13 deletions
ExprPrimaryNodes.py
+
50
−
8
View file @
1aae298a
...
...
@@ -433,13 +433,22 @@ class ExprNode(ASTNode):
def
codeGen
(
self
):
if
hasattr
(
self
,
"
code
"
)
and
self
.
code
!=
""
:
return
self
.
code
=
""
# Unary:
# Unary
operations
:
if
not
self
.
left
:
self
.
code
=
""
return
# get right output
if
not
hasattr
(
self
.
right
,
"
code
"
):
self
.
right
.
codeGen
()
self
.
code
+=
self
.
right
.
code
if
self
.
op
==
'
-
'
:
self
.
code
+=
p
(
'
neg
'
,
'
eax
'
)
return
if
self
.
op
==
'
!
'
:
self
.
code
+=
p
(
'
setz
'
,
'
eax
'
)
return
self
.
code
+=
self
.
codeGenLeftRight
()
...
...
@@ -447,15 +456,48 @@ class ExprNode(ASTNode):
# ebx = left result
# eax = right result
# so, basically do "eax = ebx op eax"
if
self
.
op
in
[
'
+
'
,
'
-
'
]:
#, '*', '/', '%']:
# Binary operations:
# Add, Subtract, Multiply
if
self
.
op
in
[
'
+
'
,
'
-
'
,
'
*
'
]:
# operation -> generated code
ops
=
{
'
+
'
:
"
add
"
,
'
-
'
:
"
sub
"
'
-
'
:
"
sub
"
,
'
*
'
:
"
imul
"
}
self
.
code
+=
p
(
ops
[
self
.
op
],
"
ebx
"
,
"
eax
"
,
"
"
+
ops
[
self
.
op
]
+
"
left and
right
"
)
self
.
code
+=
p
(
ops
[
self
.
op
],
"
ebx
"
,
"
eax
"
,
"
left:ebx
"
+
self
.
op
+
"
right
:eax
"
)
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
ebx
"
,
"
move result to eax
"
)
return
# Divide, Modulus
if
self
.
op
in
[
'
/
'
,
'
%
'
]:
# switch eax and ebx, because "idiv ebx" -> eax = edx:eax / ebx
self
.
code
+=
p
(
'
xchg
'
,
'
eax
'
,
'
ebx
'
)
self
.
code
+=
p
(
'
push
'
,
'
edx
'
)
# save edx in case someone else was using it
self
.
code
+=
p
(
'
cdq
'
,
''
,
None
,
"
set edx to the sign of eax
"
)
self
.
code
+=
p
(
"
idiv
"
,
"
ebx
"
,
""
,
"
left:eax
"
+
self
.
op
+
"
right:ebx
"
)
# eax = eax / abx
# edx = eax % ebx
if
self
.
op
==
'
%
'
:
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
edx
"
,
"
move quotient result to eax
"
)
# quotient is in edx
self
.
code
+=
p
(
'
pop
'
,
'
edx
'
)
# restore edx
return
# Comparisons:
if
self
.
op
in
[
'
&&
'
,
'
&
'
]:
self
.
code
+=
p
(
"
and
"
,
"
eax
"
,
"
ebx
"
,
"
right:eax
"
+
self
.
op
+
"
left:ebx
"
)
return
if
self
.
op
in
[
'
|
'
,
'
||
'
]:
self
.
code
+=
p
(
"
por
"
,
"
eax
"
,
"
ebx
"
,
"
right:eax
"
+
self
.
op
+
"
left:ebx
"
)
return
# if self.op in ['==', '!=', '<=', '>=', '>', '<']: TODO
# return
# if self.op == 'instanceof': TODO
# return
# generate shorter code if self.op = comparison
def
getIfFalse
(
self
,
label
):
...
...
This diff is collapsed.
Click to expand it.
UnitNodes.py
+
29
−
5
View file @
1aae298a
...
...
@@ -42,16 +42,40 @@ class LiteralNode(ASTNode):
self
.
myType
=
TypeStruct
(
self
.
name
,
node
)
def
codeGen
(
self
):
if
hasattr
(
self
,
"
code
"
)
and
self
.
code
!=
""
:
return
self
.
code
=
""
# int
if
self
.
name
==
'
int
'
:
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
str
(
self
.
getConstant
()),
"
set to literal int
"
)
# boolean
if
self
.
name
==
'
boolean
'
:
if
self
.
getConstant
:
self
.
code
=
p
(
"
mov
"
,
"
eax
"
,
"
1
"
,
"
set to literal true
"
)
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
1
"
,
"
set to literal true
"
)
return
else
:
self
.
code
=
p
(
"
mov
"
,
"
eax
"
,
"
0
"
,
"
set to literal false
"
)
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
0
"
,
"
set to literal false
"
)
return
# char TODO
# if self.name == 'char':
# self.code += p("dd", str(self.getConstant()), "", " set to literal string")
# self.code += p("pop", "eax")
# return
# string TODO
# if self.name == 'java.lang.String':
# self.code += p("dd", str(self.getConstant()), "", " set to literal char")
# self.code += p("pop", "eax")
# return
# null
if
self
.
name
==
'
null
'
:
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
0
"
,
"
set to literal null
"
)
return
# int
if
self
.
name
==
'
int
'
:
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
str
(
self
.
getConstant
()),
"
set to literal int
"
)
return
def
getConstant
(
self
):
if
self
.
name
==
'
int
'
:
...
...
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