summaryrefslogtreecommitdiff
path: root/boil/library/BoilSimp.java
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-17 20:47:59 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-17 20:47:59 -0600
commita2629207350dbad42251431e58bace2ff333d613 (patch)
treeea3b6394d130ff5dcf8bf9ae13719496bb9b7e81 /boil/library/BoilSimp.java
parentbb87a9b63707d171eb105b8d66fe9109f8e540e4 (diff)
Fix bugs in dereferencing, minor code cleanup
Diffstat (limited to 'boil/library/BoilSimp.java')
-rw-r--r--boil/library/BoilSimp.java72
1 files changed, 50 insertions, 22 deletions
diff --git a/boil/library/BoilSimp.java b/boil/library/BoilSimp.java
index 4de7633..8d36c8e 100644
--- a/boil/library/BoilSimp.java
+++ b/boil/library/BoilSimp.java
@@ -24,6 +24,19 @@ public class BoilSimp extends GJDepthFirst<String,String> {
return this.vapor;
}
+ public int getVarIndex(ClassInstance cls, TypeInstance type) {
+ /**
+ * Returns the index of the attribute in the class, or a negative number
+ * if it is not included.
+ */
+ int attr_index = -1;
+ if (cls != null &&
+ ((attr_index = cls.getLocals().indexOf(type)) >= 0)) {
+ attr_index += cls.getMethods().size() * 4;
+ }
+ return attr_index;
+ }
+
//
// Auto class visitors--probably don't need to be overridden.
//
@@ -219,11 +232,13 @@ public class BoilSimp extends GJDepthFirst<String,String> {
String id = n.f1.f0.tokenImage;
TypeInstance t = this.symt.getType(id);
- this.vapor += String.format(" %s = HeapAllocZ(%d)\n",
- this.tf.alias(t.getName()),
- t.getSize());
- // this.vapor += String.format(" if0 %s goto :error\n",
- // this.tf.alias(this.symt.getType(id).getName()));
+ if (!cls.isEmpty()) {
+ this.vapor += String.format(" %s = HeapAllocZ(%d)\n",
+ this.tf.alias(t.getName()),
+ t.getSize());
+ // this.vapor += String.format(" if0 %s goto :error\n",
+ // this.tf.alias(this.symt.getType(id).getName()));
+ }
n.f2.accept(this, args);
return mod;
}
@@ -276,10 +291,18 @@ public class BoilSimp extends GJDepthFirst<String,String> {
// minor cleanup
if (n.f10.f0.which == 8 && // primary expression
- ((PrimaryExpression) n.f10.f0.choice).f0.which == 3)
- ret = String.format("[%s+%d]",
- ret,
- 0);
+ ((PrimaryExpression) n.f10.f0.choice).f0.which == 3) {
+
+ TypeInstance sym = this.symt.getType(((Identifier) ((PrimaryExpression) n.f10.f0.choice).f0.choice).f0.tokenImage);
+ ClassInstance cls = symt.getClass(symt.getActive(TypeEnum.classname));
+
+ int attr_index;
+ if ((attr_index = this.getVarIndex(cls, sym)) >=0) {
+ ret = String.format("[%s+%d]",
+ ret,
+ attr_index);
+ }
+ }
this.vapor += String.format(" %s = %s\n ret %s\n\n",
this.tf.alias(Integer.toString(this.id++)),
@@ -406,24 +429,29 @@ public class BoilSimp extends GJDepthFirst<String,String> {
public String visit(AssignmentStatement n, String args) {
String mod = "";
- String id = n.f0.accept(this, args);
-
- ClassInstance cls;
- int attr_index = (cls = this.symt.getClass(this.symt.getActive(TypeEnum.classname)))
- .getLocals().indexOf(this.symt.getType(id)) * 4;
-
- if (attr_index < 0)
- attr_index = 0;
- else
- attr_index += cls.getMethods().size() * 4;
+ String lhs = n.f0.accept(this, args);
+ String id = n.f0.f0.tokenImage;
n.f1.accept(this, args);
String expr = n.f2.accept(this, args);
n.f3.accept(this, args);
- this.vapor += String.format(" [%s+%d] = %s\n",
- this.tf.retrieveRecentList(1),
- attr_index,
+ TypeInstance t = this.symt.getType(id);
+ ClassInstance cls = this.symt.getClass(this.symt.getActive(TypeEnum.classname));
+
+ int attr_index = 0;
+ if (expr.contains("functable") || // is this an allocation... :)
+ (attr_index = this.getVarIndex(cls, t)) >=0
+
+ ) {
+ lhs = String.format("[%s+%d]",
+ lhs,
+ attr_index);
+ }
+
+
+ this.vapor += String.format(" %s = %s\n",
+ lhs,
expr);
return mod;