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
5f77ea28
Commit
5f77ea28
authored
5 years ago
by
Xun Yang
Browse files
Options
Downloads
Patches
Plain Diff
cast and instanceof
parent
2f31b4d1
No related branches found
No related tags found
2 merge requests
!22
Subtype test
,
!20
Master
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ExprPrimaryNodes.py
+37
-0
37 additions, 0 deletions
ExprPrimaryNodes.py
with
37 additions
and
0 deletions
ExprPrimaryNodes.py
+
37
−
0
View file @
5f77ea28
...
@@ -303,6 +303,29 @@ class CastNode(ASTNode):
...
@@ -303,6 +303,29 @@ class CastNode(ASTNode):
return
return
raise
Exception
(
"
ERROR: Cannot cast type {} to type {}.
"
.
format
(
self
.
right
.
myType
.
name
,
self
.
left
.
myType
.
name
))
raise
Exception
(
"
ERROR: Cannot cast type {} to type {}.
"
.
format
(
self
.
right
.
myType
.
name
,
self
.
left
.
myType
.
name
))
def
codeGen
(
self
):
if
hasattr
(
self
,
"
code
"
)
and
self
.
code
!=
""
:
return
self
.
code
=
""
print
(
self
.
left
.
myType
.
name
,
self
.
typeName
)
offset
=
self
.
left
.
myType
.
typePointer
.
subTypeOffset
# get object at right
self
.
code
+=
"
; casting
\n
"
self
.
right
.
codeGen
()
self
.
code
+=
self
.
right
.
code
# subtype test
self
.
code
+=
p
(
"
mov
"
,
"
ebx
"
,
"
[eax]
"
,
"
start subtype test
"
)
# access class tag of left object
self
.
code
+=
p
(
"
mov
"
,
"
ebx
"
,
"
[ebx + 4]
"
)
# access subtype testing column
self
.
code
+=
p
(
"
mov
"
,
"
ebx
"
,
"
[ebx +
"
+
str
(
offset
)
+
"
]
"
)
# ebx has isSubtype
# exception if not subtype, else do nothing (object is already in eax)
self
.
code
+=
p
(
"
cmp
"
,
"
[G__Zero]
"
,
"
ebx
"
)
self
.
code
+=
p
(
"
je
"
,
"
H__Throw_Exception
"
)
self
.
code
+=
"
; end of casting
\n
"
###################################################################################
###################################################################################
# unqualCreate NEW name LPAREN args RPAREN
# unqualCreate NEW name LPAREN args RPAREN
class
ClassCreateNode
(
ASTNode
):
class
ClassCreateNode
(
ASTNode
):
...
@@ -475,6 +498,20 @@ class ExprNode(ASTNode):
...
@@ -475,6 +498,20 @@ class ExprNode(ASTNode):
return
return
self
.
code
=
""
self
.
code
=
""
# instanceOf
if
self
.
op
==
"
instanceof
"
:
offset
=
self
.
right
.
myType
.
typePointer
.
subTypeOffset
self
.
code
+=
(
"
; evaluate instanceof
\n
"
)
self
.
left
.
codeGen
()
self
.
code
+=
self
.
left
.
code
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
[eax]
"
)
# access class tag of left object
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
[eax + 4]
"
)
# access subtype testing column
self
.
code
+=
p
(
"
mov
"
,
"
eax
"
,
"
[eax +
"
+
str
(
offset
)
+
"
]
"
)
# subType column stores 0 or 1 already
self
.
code
+=
(
"
; end of instanceof
\n
"
)
return
# Unary operations:
# Unary operations:
if
not
self
.
left
:
if
not
self
.
left
:
# get right output
# get right output
...
...
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