From 37a90ac2f6960b9006ccd23966ed510116cb937a Mon Sep 17 00:00:00 2001 From: Apoorva Ranade Date: Fri, 7 May 2021 20:32:06 +0530 Subject: [PATCH] Optimization --- src/main/java/chocopy/pa3/CodeGenImpl.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index d13182c..192854b 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -42,6 +42,8 @@ public class CodeGenImpl extends CodeGenBase protected final Label initcharsLabel = new Label("initchars"); protected final Label allCharsLabel = new Label("allChars"); + /** Variable to indicate if Strings are used in the program for optimization */ + private boolean flag = false; /** Operation on None. */ private final Label errorNone = new Label("error.None"); @@ -454,6 +456,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(StringLiteral node) { + flag=true; Label l = constants.getStrConstant(node.value); backend.emitLA(Register.A0, l, "Load string literal"); return Register.A0; @@ -498,6 +501,8 @@ public class CodeGenImpl extends CodeGenBase private Object getVar(Identifier id) { + if(id.getInferredType().equals(Type.STR_TYPE)) + flag=true; SymbolInfo info = sym.get(id.name); if(info instanceof StackVarInfo) return getStackVar((StackVarInfo)info); @@ -1145,9 +1150,13 @@ public class CodeGenImpl extends CodeGenBase emitStdFunc("strneql"); emitStdFunc("makeint"); emitStdFunc("makebool"); - emitStdFunc("initchars"); - emitStdFunc("allChars"); - + if(flag) + { + emitStdFunc("initchars"); + emitStdFunc("allChars"); + } + else + emitStdFunc("dummy"); emitErrorFunc(errorNone, "Operation on None"); emitErrorFunc(errorDiv, "Division by zero"); emitErrorFunc(errorOob, "Index out of bounds");