|
@@ -25,6 +25,7 @@ import java.util.regex.Pattern;
|
|
|
// exec recebe mandando
|
|
|
// synexec recebe mandando
|
|
|
|
|
|
+// Atualizei as labels mas não atualizei as referencias por isso esta sendo removida quando faz a limpeza
|
|
|
/**
|
|
|
*
|
|
|
* @author Eugenio
|
|
@@ -34,7 +35,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
protected boolean inicializado = false;
|
|
|
protected int incLabel = 0;
|
|
|
protected int tmpVarCount = 1;
|
|
|
- protected int OrTestAndOccurrence = 0;
|
|
|
+// protected int OrTestAndOccurrence = 0;
|
|
|
|
|
|
protected AbstractSyntaxTree ast;
|
|
|
protected Code code;
|
|
@@ -67,10 +68,11 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
public static Pattern AddressOperator = Pattern.compile("(\\&|\\*)");
|
|
|
protected boolean enableOtimization = true;
|
|
|
protected HashMap<String, String> ArrayShiftMap = new HashMap<>();
|
|
|
+ private int jb = 0;
|
|
|
|
|
|
public BaseTacGen() throws Exception {
|
|
|
for (long i = 0; i < 31; i++) {
|
|
|
- power2.put(Long.valueOf(1 << i), Long.valueOf(i));
|
|
|
+ power2.put(Long.toString(Long.valueOf(1 << i)), Long.valueOf(i));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -103,6 +105,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
public BaseTacGen(AbstractSyntaxTree ast) {
|
|
|
+ for (long i = 0; i < 31; i++) {
|
|
|
+ power2.put(Long.toString(Long.valueOf(1 << i)), Long.valueOf(i));
|
|
|
+ }
|
|
|
Ast(ast);
|
|
|
}
|
|
|
|
|
@@ -136,11 +141,21 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
put("pop_return", base + " 'pop_return '{[p1]} {' --'[comment]}" + end);
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+// public void DeclareConstants(Node root) throws Exception {
|
|
|
+//// System.out.println("DeclareConstants() ...");
|
|
|
+// for (Node constants : root.findAll("dec.const", "class", 0)) {
|
|
|
+//// System.out.println("Declare constant ..." + constants);
|
|
|
+// visit(constants);
|
|
|
+// }
|
|
|
+// }
|
|
|
@Override
|
|
|
public Code Create(AbstractSyntaxTree ast, HashMap<String, String> settings) throws Exception {
|
|
|
Ast(ast);
|
|
|
+
|
|
|
+// DeclareConstants(ast.getRoot());
|
|
|
// setOtimizacao(settings.G("clearCode").equals("true"));
|
|
|
Code code = run();
|
|
|
// Executa todos os tratametos definidos apos geracao do codigo
|
|
@@ -263,6 +278,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
} else {
|
|
|
|
|
|
String v = gerarVariavel(n);
|
|
|
+
|
|
|
if (n.eq("pointer", "*") && n.eq("access", "read")) {
|
|
|
String t = gerarVariavel("T");
|
|
|
Copy(t, v, false);
|
|
@@ -336,7 +352,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- protected LinkedHashMap<Long, Long> power2 = new LinkedHashMap<Long, Long>();
|
|
|
+ protected LinkedHashMap<String, Long> power2 = new LinkedHashMap<String, Long>();
|
|
|
|
|
|
protected void FindShiftPath(int size, ArrayList<Integer> path) {
|
|
|
size = Math.abs(size);
|
|
@@ -344,14 +360,19 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
int op = shift.get(2), s1 = shift.get(op);
|
|
|
|
|
|
-// System.out.println("FindShiftPath:(" + size + "|" + s1 + ")\t\t" + shift);
|
|
|
- if (power2.containsKey(s1)) {
|
|
|
+// System.out.println("FindShiftPath:(" + power2.containsKey("" + s1) + "|" + size + "|" + s1 + ")\t\t" + shift);
|
|
|
+// System.out.println(power2);
|
|
|
+ if (power2.containsKey("" + s1)) {
|
|
|
path.add(s1);
|
|
|
path.add(op);
|
|
|
}
|
|
|
+
|
|
|
if (size == 1) {
|
|
|
-// path.add(size);
|
|
|
+// path.add(size);
|
|
|
}
|
|
|
+
|
|
|
+ System.out.println("Pathj:" + path);
|
|
|
+
|
|
|
if (path.size() % 2 == 0 && Math.abs(size - s1) == 0) {
|
|
|
path.remove(path.size() - 1);
|
|
|
return;
|
|
@@ -398,7 +419,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
// String shift;
|
|
|
Node var = Variaveis.Get(n.getText());
|
|
|
-// System.out.println("INDEX shift:" + n.childrens());
|
|
|
+// System.out.println("INDEX shift:" + n + var);
|
|
|
|
|
|
for (int i = 0, j = 1; j < var.childrens().size(); j++) {
|
|
|
n.childrens().get(i++).S("collNumber", var.childrens().get(j).getText());
|
|
@@ -419,21 +440,29 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
return ArrayShiftMap.get(key);
|
|
|
}
|
|
|
|
|
|
- protected String GenShiftLeftLogical(String var, int size, boolean mps) throws Exception {
|
|
|
+ protected String GenShiftLeftLogical(String var, int size, boolean mps, boolean last) throws Exception {
|
|
|
ArrayList<Integer> path = ShiftPath(size);
|
|
|
|
|
|
ArrayList<Integer> reverse = new ArrayList<>();
|
|
|
for (int i = path.size() - 1; i >= 0; i--) {
|
|
|
reverse.add(path.remove(i));
|
|
|
}
|
|
|
- System.out.println(">>>>>(var:" + var + ", size:" + size + ") arithpath:" + reverse);
|
|
|
+// System.out.println(">>>>>(var:" + var + ", size:" + size + ") arithpath:" + reverse);
|
|
|
|
|
|
- var = GSLL(var, reverse, mps, size);
|
|
|
+ var = GSLL(var, reverse, mps, size, last);
|
|
|
|
|
|
return var;
|
|
|
}
|
|
|
|
|
|
- protected String GSLL(String var, ArrayList<Integer> path, boolean mps, int size) throws Exception {
|
|
|
+ protected long pow2(long p) {
|
|
|
+ return pow2("" + p);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected long pow2(String p) {
|
|
|
+ return power2.get(p);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected String GSLL(String var, ArrayList<Integer> path, boolean mps, int size, boolean last) throws Exception {
|
|
|
int op1, operacao, op2;
|
|
|
String T1 = gerarVariavel("T"),
|
|
|
T2 = gerarVariavel("T"),
|
|
@@ -448,8 +477,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
} else {
|
|
|
T1 = var;
|
|
|
}
|
|
|
- if (mps) {
|
|
|
- __exp("<<", T1, "" + power2.get(size), T2).S("comment", "");
|
|
|
+ if (!last) {
|
|
|
+ __exp("<<", T1, "" + pow2(size), T2).S("comment", "");
|
|
|
return T2;
|
|
|
}
|
|
|
return T1;
|
|
@@ -466,13 +495,17 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
if (op1 != 1) {
|
|
|
- __exp("<<", T1, "" + power2.get(op1), T1).S("comment", "");
|
|
|
+ __exp("<<", T1, "" + pow2(op1), T1).S("comment", "");
|
|
|
}
|
|
|
// if (op2 == 1) {
|
|
|
// } else {
|
|
|
// Copy(T3, var, false).S("comment", "Copy value of index");
|
|
|
if (op2 != 1) {
|
|
|
- __exp("<<", T1, "" + (power2.get(op2) - power2.get(op1)), T2).S("comment", "");
|
|
|
+// System.out.println("GSLL:" + op2 + ":" + op1);
|
|
|
+// System.out.println("GSLL:op2" + pow2(op2));
|
|
|
+// System.out.println("GSLL:op1" + pow2(op1));
|
|
|
+
|
|
|
+ __exp("<<", T1, "" + (pow2(op2) - pow2(op1)), T2).S("comment", "");
|
|
|
// deve realizar um novo deslocamento
|
|
|
} else {
|
|
|
T2 = T1;
|
|
@@ -489,14 +522,14 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
//
|
|
|
// Copy(T1, var, false).S("comment", "Copy value of index");
|
|
|
//
|
|
|
-// __exp("<<", T1, "" + power2.get(op1), T1).S("comment", "");
|
|
|
+// __exp("<<", T1, "" + pow2(op1), T1).S("comment", "");
|
|
|
// __exp("+", T1, T2, T1).S("comment", "");
|
|
|
}
|
|
|
return var;
|
|
|
}
|
|
|
|
|
|
protected String CalcArrayAddress(ArrayList<Node> childrens, int Size) throws Exception {
|
|
|
- System.out.println("CalcArrayAddressINIT:" + Size);
|
|
|
+// System.out.println("CalcArrayAddressINIT(" + Size + "):");
|
|
|
boolean onlyNumber = true;
|
|
|
|
|
|
for (Node node : childrens) {
|
|
@@ -514,10 +547,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
String current = CalcArrayAddressAux(childrens, Size);
|
|
|
|
|
|
- System.out.println("Size {" + current + "}:----------->" + Size);
|
|
|
-
|
|
|
+// System.out.println("Size {" + current + "}:----------->" + Size);
|
|
|
// multiplica pelo tamanho
|
|
|
- return GenShiftLeftLogical(current, Size, true);
|
|
|
+ return GenShiftLeftLogical(current, Size, true, false);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -538,19 +570,34 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
protected String CalcArrayAddressAux(ArrayList<Node> childrens, int Size) throws Exception {
|
|
|
- String current;
|
|
|
// boolean multPerSize = true;
|
|
|
Node ind, nextIndex;
|
|
|
ind = childrens.remove(0);
|
|
|
- System.out.println("CalcArrayAddressAux:" + childrens.size() + ":" + Size);
|
|
|
-
|
|
|
+// System.out.println("CalcArrayAddressAux:" + childrens.size() + ":" + Size + "\n" + ind);
|
|
|
+ String current, varname;
|
|
|
+
|
|
|
+ if (ind.eq("class", "expr")) {
|
|
|
+ varname = gerarVariavel(
|
|
|
+ new Node(gerarVariavel("TMP"))
|
|
|
+ .S("array", "false")
|
|
|
+ .S("type", "int")
|
|
|
+ .S("var.tmp", "true")
|
|
|
+ );
|
|
|
+
|
|
|
+ Node p1 = visit(ind.childrens().get(0)),
|
|
|
+ p2 = visit(ind.childrens().get(1));
|
|
|
+ __exp(ind.getText(), p1.G("_return"), p2.G("_return"), varname);
|
|
|
+ } else {
|
|
|
+ varname = gerarVariavel(ind);
|
|
|
+ }
|
|
|
+// if (ind)
|
|
|
// ultimo elemento da idexacao
|
|
|
if (childrens.size() == 0) {
|
|
|
|
|
|
if (Utils.isNumber(ind.getText())) {
|
|
|
return "" + ind.getInt("value");
|
|
|
} else {
|
|
|
- return GenShiftLeftLogical(gerarVariavel(ind), Size, false);
|
|
|
+ return GenShiftLeftLogical(varname, Size, false, true);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -565,7 +612,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
} else {
|
|
|
// System.out.println("collNumber:" + collNumber);
|
|
|
|
|
|
- current = GenShiftLeftLogical(gerarVariavel(ind), collNumber, false);
|
|
|
+// current = GenShiftLeftLogical(gerarVariavel(ind), collNumber, false, false);
|
|
|
+ current = GenShiftLeftLogical(varname, collNumber, false, false);
|
|
|
// System.out.println("Gerando index calc:" + retl);
|
|
|
// current = GenShiftLeftLogical(retl, Size);
|
|
|
// current = retl;
|
|
@@ -693,7 +741,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
protected void gerarConstante(Node n) throws Exception {
|
|
|
- gerarVariavel(n);
|
|
|
+
|
|
|
+// System.out.println("Gerando constant:" + n.childrens().get(0) + n.childrens().get(1));
|
|
|
+// gerarVariavel(n.get(label, label));
|
|
|
// getLabel("C", n);
|
|
|
// if (n.closest("block", "class") != null) {
|
|
|
// code.registerVar(n.G("_return"), 1, DataFrame.TYPE_VAR);
|
|
@@ -832,7 +882,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
protected void BranchAnd(Node n, int index) throws Exception {
|
|
|
// int pos = 0;
|
|
|
- OrTestAndOccurrence++;
|
|
|
+// if (n.parent == null) {
|
|
|
+// System.out.println("BranchAnd:" + n);
|
|
|
+// }
|
|
|
+// OrTestAndOccurrence++;
|
|
|
Instruction instruction = null;
|
|
|
ArrayList<Node> x = n.childrens();
|
|
|
Node op1 = x.get(0), op2 = x.get(1);
|
|
@@ -852,29 +905,32 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
case "jtb":
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
|
|
|
- if (n.parent.eq("value", "||")) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index == 1) {
|
|
|
- instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
} else if (index == 2) {
|
|
|
- instruction.S("label", n.parent.G("end"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("end"));
|
|
|
}
|
|
|
// } else if (n.parent.eq("value", "&&")) {
|
|
|
|
|
|
} else {
|
|
|
- instruction.S("label", n.G("end"));
|
|
|
+ UpdateLabel(instruction, n.G("end"));
|
|
|
}
|
|
|
break;
|
|
|
case "jb":
|
|
|
- if (n.parent.eq("value", "||")) {
|
|
|
+// if (n.parent != null) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index <= 1) {
|
|
|
- instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
+
|
|
|
} else if (index == 2) {
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("next"));
|
|
|
+ UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
- } else {
|
|
|
-
|
|
|
}
|
|
|
+// } else {
|
|
|
+
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
// System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
|
|
@@ -887,12 +943,12 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (instruction != null) {
|
|
|
switch (type) {
|
|
|
case "jtb":
|
|
|
- if (n.parent.eq("value", "||")) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index == 1) { // teste 1 do parametro 2 com pai ||
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("end"));
|
|
|
+ UpdateLabel(instruction, n.G("end"));
|
|
|
} else if (index == 2) { // teste 2 do parametro 2
|
|
|
- instruction.S("label", n.G("next"));
|
|
|
+ UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
// } else if (n.parent.eq("value", "&&")) {
|
|
|
// if (index == 1) { // teste 1 do parametro 2 com pai &&
|
|
@@ -900,12 +956,12 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
//
|
|
|
// } else if (index == 2) { // teste 2 do parametro 2 com pai &&
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
-// instruction.S("label", n.G("end"));
|
|
|
+// UpdateLabel(instruction, n.G("end"));
|
|
|
// }
|
|
|
} else {
|
|
|
if (index == 1) { // teste 1 do parametro 2 geral
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("end"));
|
|
|
+ UpdateLabel(instruction, n.G("end"));
|
|
|
} else if (index == 2) {
|
|
|
// throw new Exception("teste 2 do parametro 2 geral");
|
|
|
}
|
|
@@ -913,28 +969,42 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
break;
|
|
|
case "jb":
|
|
|
|
|
|
- System.out.println("I(jb:and:2)[" + index + "]\n" + instruction + "\n" + n);
|
|
|
-
|
|
|
- if (n.parent.eq("value", "||")) {
|
|
|
+// System.out.println("I(jb:and:2)[" + index + "]\n" + instruction + "\n" + n);
|
|
|
+// if (n.parent != null) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index <= 1) {
|
|
|
-// instruction.S("label", n.parent.G("skip.test"));
|
|
|
+// UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("start"));
|
|
|
+ UpdateLabel(instruction, n.G("start"));
|
|
|
|
|
|
} else if (index == 2) {
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("next"));
|
|
|
- }
|
|
|
- } else {
|
|
|
+ UpdateLabel(instruction, n.G("next"));
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
+// } else {
|
|
|
+
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void UpdateLabel(Instruction I, String label) {
|
|
|
+ String copy = I.G("label");
|
|
|
+ if (!copy.equals("")) {
|
|
|
+ code.RemoveLabelReference(copy);
|
|
|
+ }
|
|
|
+ I.S("label", label);
|
|
|
+ code.AddLabelReference(label);
|
|
|
+ }
|
|
|
+
|
|
|
protected void BranchOr(Node n, int index) throws Exception {
|
|
|
+// if (n.parent == null) {
|
|
|
+// System.out.println("BranchOr:" + n);
|
|
|
+// }
|
|
|
// int pos = 0;
|
|
|
- OrTestAndOccurrence++;
|
|
|
+// OrTestAndOccurrence++;
|
|
|
Instruction instruction = null;
|
|
|
ArrayList<Node> x = n.childrens();
|
|
|
Node op1 = x.get(0), op2 = x.get(1);
|
|
@@ -954,32 +1024,35 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (instruction != null) {
|
|
|
switch (type) {
|
|
|
case "jtb":
|
|
|
- if (n.parent.eq("value", "&&")) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index == 1) {
|
|
|
- instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
case "jb":
|
|
|
// System.out.println("I(jb:1)[" + index + "]\n" + instruction + "\n" + n);
|
|
|
|
|
|
- if (n.parent.eq("value", "||")) {
|
|
|
+// System.out.println("Entrei no jb ||" + (jb++) + instruction + n.parent);
|
|
|
+// if (n.parent != null) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
// if (index == 2) {
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("start"));
|
|
|
+ UpdateLabel(instruction, n.G("start"));
|
|
|
// }
|
|
|
- } else if (n.parent.eq("value", "&&")) {
|
|
|
+ } else if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index <= 1) {
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
} else if (index == 2) {
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.parent.G("start"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("start"));
|
|
|
}
|
|
|
+// }
|
|
|
} else {
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
if (index <= 1) {
|
|
|
- instruction.S("label", n.G("start"));
|
|
|
+ UpdateLabel(instruction, n.G("start"));
|
|
|
} else if (index == 2) {
|
|
|
|
|
|
}
|
|
@@ -995,36 +1068,38 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (instruction != null) {
|
|
|
switch (type) {
|
|
|
case "jtb":
|
|
|
- if (n.parent.eq("value", "&&")) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index == 1) {
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.parent.G("end"));
|
|
|
+ UpdateLabel(instruction, n.parent.G("end"));
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
case "jb":
|
|
|
- if (n.parent.eq("value", "||")) {
|
|
|
+// if (n.parent != null) {
|
|
|
+ if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index <= 1) {
|
|
|
|
|
|
instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("start"));
|
|
|
+ UpdateLabel(instruction, n.G("start"));
|
|
|
|
|
|
} else if (index == 2) {
|
|
|
//
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
- instruction.S("label", n.G("next"));
|
|
|
+ UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
- } else if (n.parent.eq("value", "&&")) {
|
|
|
+ } else if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index == 1) {
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
-// instruction.S("label", n.parent.G("start"));
|
|
|
+// UpdateLabel(instruction, n.parent.G("start"));
|
|
|
} else if (index == 2) {
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
-// instruction.S("label", n.parent.G("start"));
|
|
|
+// UpdateLabel(instruction, n.parent.G("start"));
|
|
|
}
|
|
|
+// }
|
|
|
} else {
|
|
|
// instruction.S("op", Complement(instruction.G("op")));
|
|
|
-// instruction.S("label", n.G("next"));
|
|
|
+// UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -1354,12 +1429,16 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
*/
|
|
|
protected void switchStmt(Node n) throws Exception {
|
|
|
/*Label que sera consultado pelo break para sair do case*/
|
|
|
- String end = gerarLabel();
|
|
|
- String dst = null, next = null;
|
|
|
- Node test = n.find("test");
|
|
|
+
|
|
|
+ String start,
|
|
|
+ next,
|
|
|
+ end = gerarLabel(),
|
|
|
+ dst = null;
|
|
|
|
|
|
n.S("next", end);
|
|
|
|
|
|
+ Node test = n.find("test");
|
|
|
+
|
|
|
// Verifica se existe entrada para selecao
|
|
|
if (test.childrens().size() > 0) {
|
|
|
test = test.first();
|
|
@@ -1367,46 +1446,26 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
dst = test.G("_return");
|
|
|
}
|
|
|
|
|
|
+// test;
|
|
|
ArrayList<Node> cases = n.findAll("def.case", "class", 1);
|
|
|
- int id = 0, count = cases.size() - 1;
|
|
|
|
|
|
for (Node _case : cases) {
|
|
|
- if (next != null) {
|
|
|
- _case.S("start", next);
|
|
|
- }
|
|
|
- _case.S("_input", dst);
|
|
|
- _case.S("end", end);
|
|
|
|
|
|
- genCase(_case, id == count);
|
|
|
- next = _case.G("next");
|
|
|
- id++;
|
|
|
- }
|
|
|
+ start = gerarLabel();
|
|
|
+ next = gerarLabel();
|
|
|
|
|
|
- /*Marca o endereço final para sair do switch*/
|
|
|
- PushLabel(end, "branch");
|
|
|
- }
|
|
|
+ _case.S("start", start)
|
|
|
+ .S("_input", dst)
|
|
|
+ .S("next", next)
|
|
|
+ .S("end", end)
|
|
|
+ .S("test.type", "jb");
|
|
|
|
|
|
- protected Node NodeBoolExpr(String op, Node e1, Node e2) {
|
|
|
- return new Node(op)
|
|
|
- .S("type", "bool")
|
|
|
- .S("class", "expr")
|
|
|
- .S("subclass", "bool")
|
|
|
- .addFilho(e1)
|
|
|
- .addFilho(e2);
|
|
|
- }
|
|
|
+ genCase(_case);
|
|
|
|
|
|
- protected ArrayList<Node> ExprsEq(Node n, Node input) throws Exception {
|
|
|
- ArrayList<Node> list = new ArrayList<>();
|
|
|
- switch (n.Class()) {
|
|
|
- case "exprs":
|
|
|
- for (Node e : n.childrens()) {
|
|
|
- list.add(NodeBoolExpr("==", input, e));
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- list.add(NodeBoolExpr("==", input, n));
|
|
|
}
|
|
|
- return list;
|
|
|
+
|
|
|
+ /*Marca o endereço final para sair do switch*/
|
|
|
+ PushLabel(end, "branch");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1414,60 +1473,138 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
*
|
|
|
* @param n
|
|
|
*/
|
|
|
- protected void genCase(Node n, boolean last) throws Exception {
|
|
|
+ protected void genCase(Node n) throws Exception {
|
|
|
+ ArrayList<Node> exprs;
|
|
|
+ Node test;
|
|
|
+// System.out.println("GEN_CASE:" + n);
|
|
|
|
|
|
- String start = n.Has("start") ? n.G("start") : gerarLabel();
|
|
|
- String next = gerarLabel(), end;
|
|
|
-
|
|
|
-// System.out.println("GenCase:" + start + ":" + next);
|
|
|
+// String start = n.Has("start") ? n.G("start") : gerarLabel();
|
|
|
+// String next = gerarLabel(), end;
|
|
|
+//
|
|
|
+//// System.out.println("GenCase:" + start + ":" + next);
|
|
|
ArrayList<Node> itens = n.childrens();
|
|
|
Node stmts = n.find("stmts");
|
|
|
- Node or;
|
|
|
-
|
|
|
+// Node or;
|
|
|
+//
|
|
|
if (itens.size() >= 2) { // Tem teste
|
|
|
- String input = n.G("_input");
|
|
|
- Node test = n.first();
|
|
|
- ArrayList<Node> exprs;
|
|
|
+ test = n.first();
|
|
|
+ String input = n.G("_input"),
|
|
|
+ start = n.G("start"),
|
|
|
+ end = n.G("end"),
|
|
|
+ next = n.G("next");
|
|
|
|
|
|
+//
|
|
|
if (input != null) {
|
|
|
exprs = ExprsEq(test, new Node(input).S("_return", input));
|
|
|
} else {
|
|
|
exprs = test.childrens();
|
|
|
}
|
|
|
|
|
|
- /*Seleciona o endereco de saida se é no proximo case ou no fim do switch*/
|
|
|
- if (stmts.last().eq("value", "fallthrough")) {
|
|
|
- end = gerarLabel();
|
|
|
- n.S("next", end);
|
|
|
- } else {
|
|
|
- end = n.G("end");
|
|
|
- }
|
|
|
-
|
|
|
- // Atualiza o PushLabel de sucesso das exprs
|
|
|
+// /*Seleciona o endereco de saida se é no proximo case ou no fim do switch*/
|
|
|
+// if (stmts.last().eq("value", "fallthrough")) {
|
|
|
+// end = gerarLabel();
|
|
|
+// n.S("next", end);
|
|
|
+// } else {
|
|
|
+// end = n.G("end");
|
|
|
+// }
|
|
|
+//
|
|
|
+// // Atualiza o PushLabel de sucesso das exprs
|
|
|
for (Node expr : exprs) {
|
|
|
- expr.S("true", !last ? next : end);
|
|
|
-// expr.S("false", next);
|
|
|
-// expr.S("false", start);
|
|
|
+ expr.copy("start,next,end,test.type", n);
|
|
|
}
|
|
|
-
|
|
|
+//
|
|
|
Branch(OR(exprs).S("next", next), 0);
|
|
|
|
|
|
/*Se nenhum dos casos for compativel salta para o proximo*/
|
|
|
-// _goto(!last ? next : end);
|
|
|
+
|
|
|
/*Marca o PushLabel inicial do caso*/
|
|
|
PushLabel(start, "branch");
|
|
|
/*Visita o bloco do case*/
|
|
|
visit(stmts);
|
|
|
+ /*Salta para o proximo case ou para o fim do switchcase*/
|
|
|
+ _goto(end);
|
|
|
+ /*Label do inicio do proximo case*/
|
|
|
+ PushLabel(next, "branch");
|
|
|
|
|
|
- if (!last) {
|
|
|
- /*Salta para o proximo case ou para o fim do switchcase*/
|
|
|
- _goto(end);
|
|
|
- /*Label do inicio do proximo case*/
|
|
|
- PushLabel(next, "branch");
|
|
|
- }
|
|
|
} else { // Default statiment
|
|
|
visit(stmts);
|
|
|
}
|
|
|
+
|
|
|
+// String start = n.Has("start") ? n.G("start") : gerarLabel();
|
|
|
+// String next = gerarLabel(), end;
|
|
|
+//
|
|
|
+//// System.out.println("GenCase:" + start + ":" + next);
|
|
|
+// ArrayList<Node> itens = n.childrens();
|
|
|
+// Node stmts = n.find("stmts");
|
|
|
+// Node or;
|
|
|
+//
|
|
|
+// if (itens.size() >= 2) { // Tem teste
|
|
|
+// String input = n.G("_input");
|
|
|
+// Node test = n.first();
|
|
|
+// ArrayList<Node> exprs;
|
|
|
+//
|
|
|
+// if (input != null) {
|
|
|
+// exprs = ExprsEq(test, new Node(input).S("_return", input));
|
|
|
+// } else {
|
|
|
+// exprs = test.childrens();
|
|
|
+// }
|
|
|
+//
|
|
|
+// /*Seleciona o endereco de saida se é no proximo case ou no fim do switch*/
|
|
|
+// if (stmts.last().eq("value", "fallthrough")) {
|
|
|
+// end = gerarLabel();
|
|
|
+// n.S("next", end);
|
|
|
+// } else {
|
|
|
+// end = n.G("end");
|
|
|
+// }
|
|
|
+//
|
|
|
+// // Atualiza o PushLabel de sucesso das exprs
|
|
|
+// for (Node expr : exprs) {
|
|
|
+// expr.S("true", !last ? next : end);
|
|
|
+//// expr.S("false", next);
|
|
|
+//// expr.S("false", start);
|
|
|
+// }
|
|
|
+//
|
|
|
+// Branch(OR(exprs).S("next", next), 0);
|
|
|
+//
|
|
|
+// /*Se nenhum dos casos for compativel salta para o proximo*/
|
|
|
+//// _goto(!last ? next : end);
|
|
|
+// /*Marca o PushLabel inicial do caso*/
|
|
|
+// PushLabel(start, "branch");
|
|
|
+// /*Visita o bloco do case*/
|
|
|
+// visit(stmts);
|
|
|
+//
|
|
|
+// if (!last) {
|
|
|
+// /*Salta para o proximo case ou para o fim do switchcase*/
|
|
|
+// _goto(end);
|
|
|
+// /*Label do inicio do proximo case*/
|
|
|
+// PushLabel(next, "branch");
|
|
|
+// }
|
|
|
+// } else { // Default statiment
|
|
|
+// visit(stmts);
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected Node NodeBoolExpr(String op, Node e1, Node e2) {
|
|
|
+ return new Node(op)
|
|
|
+ .S("type", "bool")
|
|
|
+ .S("class", "expr")
|
|
|
+ .S("subclass", "bool")
|
|
|
+ .addFilho(e1)
|
|
|
+ .addFilho(e2);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ArrayList<Node> ExprsEq(Node n, Node input) throws Exception {
|
|
|
+ ArrayList<Node> list = new ArrayList<>();
|
|
|
+ switch (n.Class()) {
|
|
|
+ case "exprs":
|
|
|
+ for (Node e : n.childrens()) {
|
|
|
+ list.add(NodeBoolExpr("==", input, e));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ list.add(NodeBoolExpr("==", input, n));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
protected Node OR(ArrayList<Node> n) throws Exception {
|
|
@@ -1479,7 +1616,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
default:
|
|
|
Node x = n.get(0);
|
|
|
n.remove(x);
|
|
|
- return NodeBoolExpr("||", x, OR(n)).copy("true,false", x);
|
|
|
+ return NodeBoolExpr("||", x, OR(n)).copy("start,next,end,test.type", x);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1509,8 +1646,11 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// operators = new ArrayList<>(),
|
|
|
returns;
|
|
|
ArrayList<Boolean> copyaddress = new ArrayList<>();
|
|
|
- String p1, declare = "" + n.getText().equals("dec.var.short");
|
|
|
+ String p1,
|
|
|
+ modify = n.G("modify"),
|
|
|
+ declare = "" + n.getText().equals("dec.var.short");
|
|
|
|
|
|
+// System.out.println("genAssign:" + n.childrens().get(0).childrens().get(0));
|
|
|
Node dsts = n.childrens().get(0);
|
|
|
|
|
|
if (n.in("value", new String[]{"dec.var.short", "def.assign"})) {
|
|
@@ -1586,7 +1726,12 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
} else {
|
|
|
// Copia valor para endereco
|
|
|
// System.out.println("Attrib:" + d.G("_return") + "<" + localAttribs.get(i));
|
|
|
- Copy(d.G("_return"), localAttribs.get(i), copyaddress.get(i));
|
|
|
+ if (!modify.equals("")) {
|
|
|
+
|
|
|
+ __exp(modify, d.G("_return"), localAttribs.get(i), d.G("_return")).S("comment", "");
|
|
|
+ } else {
|
|
|
+ Copy(d.G("_return"), localAttribs.get(i), copyaddress.get(i));
|
|
|
+ }
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
@@ -1795,26 +1940,23 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
protected void ctrlStmt(Node n) throws Exception {
|
|
|
- Node container = n.closest("for.stmt,switch.stmt", "class");
|
|
|
+ String go;
|
|
|
+ Node ctn = n.closest("for.stmt,switch.stmt", "class");
|
|
|
|
|
|
switch (n.getText()) {
|
|
|
case "continue":
|
|
|
- String go;
|
|
|
- if (container.eq("class", "for.stmt")) {
|
|
|
- go = container.G("inc");
|
|
|
- } else {
|
|
|
- go = container.G("end");
|
|
|
- }
|
|
|
- _goto(go);
|
|
|
+ go = ctn.G(ctn.eq("class", "for.stmt") ? "inc" : "end");
|
|
|
break;
|
|
|
case "break":
|
|
|
- _goto(container.G("end"));
|
|
|
+ go = ctn.G("end");
|
|
|
break;
|
|
|
case "goto":
|
|
|
- _goto(n.G("label"));
|
|
|
+ go = n.G("label");
|
|
|
break;
|
|
|
+ default:
|
|
|
+ throw new Exception("Undefined ctrl stmt '" + n.getText() + "'.");
|
|
|
}
|
|
|
-
|
|
|
+ _goto(go);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -2015,11 +2157,20 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Node var = Variaveis.Get(id);
|
|
|
String varname, alias, vn;
|
|
|
|
|
|
- if (var.eq("constant", "true")) {
|
|
|
- varname = id;
|
|
|
+ if (var != null) {
|
|
|
+ if (var.eq("constant", "true")) {
|
|
|
+ varname = id;
|
|
|
+ } else {
|
|
|
+ varname = var.getText();
|
|
|
+ }
|
|
|
+// System.out.println("Variavel definida:" + varname);
|
|
|
} else {
|
|
|
- varname = var.getText();
|
|
|
+ varname = n.getText();
|
|
|
+ var = n;
|
|
|
+// System.out.println("Variavel do no consultado:" + varname);
|
|
|
+// Variaveis.Add(id, n);
|
|
|
}
|
|
|
+
|
|
|
// System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
|
|
|
// if (var.eq("constant", "true")) {
|
|
|
// System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
|
|
@@ -2034,10 +2185,13 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
prefix = "C";
|
|
|
} else if (var.eq("global", "true")) {
|
|
|
prefix = "G";
|
|
|
+ } else if (var.eq("var.tmp", "true")) {
|
|
|
+ prefix = "T";
|
|
|
} else {
|
|
|
prefix = "V";
|
|
|
}
|
|
|
|
|
|
+// System.out.println("gerarVariavelSet:" + id + var);
|
|
|
// System.out.println("DecVar:" + var);
|
|
|
// alias = var.G("pointer") + gerarVariavel(prefix);
|
|
|
alias = gerarVariavel(prefix);
|
|
@@ -2049,10 +2203,19 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
int size = Tipos.Size(var.G("type")), b = 1;
|
|
|
|
|
|
+// System.out.println("gerarVariavelSetVAR:" + var);
|
|
|
// Se for um array considera os filhos como index
|
|
|
- if (!var.eq("constant", "true") && var.childrens().size() > 0) {
|
|
|
+ if (var.eq("array", "true") && var.childrens().size() > 0) {
|
|
|
for (Node node : var.childrens()) {
|
|
|
- b *= node.getInt("value");
|
|
|
+
|
|
|
+// System.out.println("gerarVariavelSetArray:"
|
|
|
+// + node.eq("constant", "true")
|
|
|
+// + "\n" + node
|
|
|
+// + "\nVARIABLE:\n" + var
|
|
|
+// );
|
|
|
+ b *= (node.eq("constant", "true")
|
|
|
+ ? node.getInt("valueConstant")
|
|
|
+ : node.getInt("value"));
|
|
|
}
|
|
|
}
|
|
|
size *= b;
|
|
@@ -2060,7 +2223,13 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
var.set("size", size);
|
|
|
|
|
|
// System.out.println("gerarVariave[" + prefix + "|" + id + "]:" + var);
|
|
|
- (prefix.equals("G") ? code.GData() : code.Block().Data()).Add(alias, var);
|
|
|
+ if (!var.eq("constant", "true") || !var.in("type", new String[]{"int", "char", "bool"})) {
|
|
|
+
|
|
|
+ (prefix.equals("G") ? code.GData() : code.Block().Data()).Add(alias, var);
|
|
|
+ } else {
|
|
|
+// System.out.println("var:" + var);
|
|
|
+ alias = var.G("valueConstant");
|
|
|
+ }
|
|
|
}
|
|
|
return alias;
|
|
|
}
|