Skip to content

Commit

Permalink
-o optimization WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
susanalima committed Jun 3, 2019
1 parent 20ae490 commit e0125b2
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
!AlphaMain.java
!IoFunctions.java
!Type.java
!Optimization.java
!State.java
!Symbol.java
!Var.java
Expand Down
9 changes: 8 additions & 1 deletion src/FunctionBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ int getVarSize(String varName) {
return contents.get(varName).size;
}


void setVarSize(String varName, int size) {
contents.get(varName).setSize(size);
}

String getVarConstValue(String varName) {
return contents.get(varName).getConstValue();
}

void setVarConstValue(String varName, String value) {
contents.get(varName).setConstValue(value);
}


public void printFunctionBlock() {
System.out.println("function return type : " + this.returnType);
Expand Down
27 changes: 22 additions & 5 deletions src/JasminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class JasminTest {
boolean unreachableCode;
int stackSize;

Optimization optimization;

JasminTest(SymbolTable sT) {
this.symbolTable = sT;
this.code = "";
Expand All @@ -20,6 +22,8 @@ public class JasminTest {
this.unreachableCode = false;
this.stackSize = 0;
this.finalCode = "";
this.optimization = Optimization.O;

}

public String process(SimpleNode node, String symbol, String funcname, State state, String possibleReturnType) {
Expand Down Expand Up @@ -301,11 +305,19 @@ private String process_nodeIdentifier(SimpleNode node, String symbol, String fun
symbol += symbolTable.getVarType(funcname, node.val);
if (symbolTable.isVarLocal(funcname, node.val)) {
String type = symbolTable.getVarType(funcname, node.val);
if (type.equals("int") || type.equals("boolean"))
code += "iload ";
else
if (type.equals("int") || type.equals("boolean")) {
String cValue = symbolTable.getSymbolConstValue(funcname, node.val);
if(this.optimization == Optimization.O && !cValue.equals(Symbol.UNDEFINED_CVALUE)) {
code += "ldc " + cValue + "\n";
} else {
code += "iload ";
code += symbolTable.getCounter(funcname, node.val) + "\n";
}
}
else {
code += "aload ";
code += symbolTable.getCounter(funcname, node.val) + "\n"; // MUDAR CONSOANTE O TIPO
code += symbolTable.getCounter(funcname, node.val) + "\n";
}
} else {
code += "aload_0\n" + "getfield " + symbolTable.getClassName() + "/" + node.val + " "
+ this.paramType(symbolTable.getVarType(funcname, node.val)) + "\n";
Expand Down Expand Up @@ -844,8 +856,13 @@ private String process_nodeEqual(SimpleNode node, String symbol, String funcname
isArray = true;
} else {
type = symbolTable.getVarType(funcname, left_child_node.val);
if (type.equals("int") || type.equals("boolean"))
if (type.equals("int") || type.equals("boolean")) {
String cValue = symbolTable.getSymbolConstValue(funcname, left_child_node.val);
if(optimization == Optimization.O && !cValue.equals(Symbol.UNDEFINED_CVALUE)) {
return symbol;
}
storeType = "istore ";
}
else
storeType = "astore ";
storeType += symbolTable.getCounter(funcname, left_child_node.val);
Expand Down
4 changes: 4 additions & 0 deletions src/Optimization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Optimization {
NONE,
O,
}
12 changes: 12 additions & 0 deletions src/Symbol.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@

public class Symbol {

static final String UNDEFINED_CVALUE = "@";
String type;
String name;
String value;
int counter;
int size;
String constValue;

Symbol(String type_, String name_, String value_) {
this.type = type_;
this.name = name_;
this.value = value_;
this.counter = 0;
this.size = -1;
this.constValue = UNDEFINED_CVALUE;
}

void setCounter(int counter_) {
Expand All @@ -31,6 +34,14 @@ int getSize() {
return this.size;
}

void setConstValue(String value) {
this.constValue = value;
}

String getConstValue() {
return this.constValue;
}

@Override
public String toString() {
return "Symbol{" +
Expand All @@ -39,6 +50,7 @@ public String toString() {
", value=" + value +
", counter=" + counter +
", size=" + size +
", constValue=" + constValue +
'}';
}

Expand Down
59 changes: 52 additions & 7 deletions src/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public class SymbolTable {
String className;
boolean extends_;
String parentClass;
Optimization optimization;

SymbolTable() {
this.symbolTable = new HashMap<>();
this.extends_ = false;
this.parentClass = "java/lang/Object";
this.optimization = Optimization.O;
}

public void setExtends(SimpleNode node) {
Expand Down Expand Up @@ -235,6 +237,23 @@ int getSymbolSize(String funcName, String varName) {
return this.symbolTable.get(GLOBAL).getVarSize(varName);
}


void setSymbolConstValue(String funcName, String varName, String value) {

if (isVarLocal(funcName, varName)) {
this.symbolTable.get(funcName).setVarConstValue(varName, value);
} else
this.symbolTable.get(GLOBAL).setVarConstValue(varName, value);
}

String getSymbolConstValue(String funcName, String varName) {
if (isVarLocal(funcName, varName)) {
return this.symbolTable.get(funcName).getVarConstValue(varName);
} else
return this.symbolTable.get(GLOBAL).getVarConstValue(varName);
}


public void printSymbolTable() {
System.out.println("\n\n---SYMBOL TABLE---\n\n");
System.out.println("Class name: " + this.className);
Expand Down Expand Up @@ -691,16 +710,42 @@ else if (currState == State.PROCESS)
public void evalNodeEqual_build(SimpleNode node, String funcname) {
SimpleNode grand_child_node, child_node = (SimpleNode) node.jjtGetChild(0); // identifier
String varname = child_node.val;
child_node = (SimpleNode) node.jjtGetChild(1);
if (child_node.getId() == AlphaTreeConstants.JJTNEWFUNC) { // new func
grand_child_node = (SimpleNode) child_node.jjtGetChild(1);
if (grand_child_node.getId() == AlphaTreeConstants.JJTINT) { // se for array
grand_child_node = (SimpleNode) child_node.jjtGetChild(2);
if (grand_child_node.getId() == AlphaTreeConstants.JJTINTEGER) {
setSymbolSize(funcname, varname, Integer.parseInt(grand_child_node.val));
int id = child_node.getId();
child_node = (SimpleNode) node.jjtGetChild(1); //right child

String cValue = Symbol.UNDEFINED_CVALUE;
int child_id = child_node.getId();
if (child_id == AlphaTreeConstants.JJTINTEGER) {
cValue = child_node.val;
} else if(child_id == AlphaTreeConstants.JJTTRUE) {
cValue = "1";
} else if(child_id == AlphaTreeConstants.JJTFALSE) {
cValue = "0";
} else {
if (child_id == AlphaTreeConstants.JJTNEWFUNC) { // new func
grand_child_node = (SimpleNode) child_node.jjtGetChild(1);
if (grand_child_node.getId() == AlphaTreeConstants.JJTINT) { // se for array
grand_child_node = (SimpleNode) child_node.jjtGetChild(2);
if (grand_child_node.getId() == AlphaTreeConstants.JJTINTEGER) {
setSymbolSize(funcname, varname, Integer.parseInt(grand_child_node.val));
}
}
}
}

if(id == AlphaTreeConstants.JJTIDENTIFIER) {

SimpleNode parent_node = (SimpleNode) node.jjtGetParent();
int parent_id = parent_node.getId();
SimpleNode grandParent_node = (SimpleNode) parent_node.jjtGetParent();
int grandParent_id = grandParent_node.getId();

if(parent_id == AlphaTreeConstants.JJTBODY && (grandParent_id == AlphaTreeConstants.JJTIF || grandParent_id == AlphaTreeConstants.JJTELSE))
cValue = Symbol.UNDEFINED_CVALUE;


setSymbolConstValue(funcname, varname, cValue);
}
}

private void setUndefinedArgsType(String expression_undefined, String expression) {
Expand Down
6 changes: 6 additions & 0 deletions todosAndExs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ TODO LIST:
30. cena das variaveis da stack dentro de loops
32. se a variavel vier do extends como fica o getfield e o putfield?
33. while(!true) || while(false) -> da erro, nao sei que fazer, deveria estar na analise semantica?
34. testar parametros $name
35. mudar a funcao que faz set do constValue da variavel para adicionar a variavel a tabela de simbolos caso ela nao exista
36. ver melhor cena do ldc vs iconst, especialmente nos booleanos
37. comparar com o javac mesmo
38. adiciona o optimization como argumento/parametro -> esta escrito a mao agr


0 comments on commit e0125b2

Please sign in to comment.