|
@@ -15,6 +15,7 @@ import ast.AbstractSyntaxTree;
|
|
|
import ast.Node;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.Map;
|
|
|
import java.util.Stack;
|
|
@@ -68,19 +69,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
protected HashMap<String, String> ArrayShiftMap = new HashMap<>();
|
|
|
|
|
|
public BaseTacGen() throws Exception {
|
|
|
-
|
|
|
-// for (int i = 44; i > 0; i--) {
|
|
|
-//
|
|
|
-// System.out.println("----- " + i);
|
|
|
-//
|
|
|
-// ShiftPath(i);
|
|
|
-//
|
|
|
-// }
|
|
|
-// ShiftPath(48);
|
|
|
-// ShiftPath(52);
|
|
|
-// ShiftPath(56);
|
|
|
-// ShiftPath(108);
|
|
|
-// throw new Exception("SHIFT");
|
|
|
+ for (long i = 0; i < 31; i++) {
|
|
|
+ power2.put(Long.valueOf(1 << i), Long.valueOf(i));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
protected int calc(ArrayList<Integer> data) {
|
|
@@ -112,27 +103,14 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
public BaseTacGen(AbstractSyntaxTree ast) {
|
|
|
-
|
|
|
Ast(ast);
|
|
|
}
|
|
|
|
|
|
-// @Override
|
|
|
-// public TacGenInterface AfterCreate(CodeProcessing p) {
|
|
|
-// this.afterCreate.add(p);
|
|
|
-// return this;
|
|
|
-// }
|
|
|
-//
|
|
|
-// public TacGenInterface AfterCreate() throws Exception {
|
|
|
-// for (CodeProcessing x : afterCreate) {
|
|
|
-// x.Exec(code);
|
|
|
-// }
|
|
|
-// return this;
|
|
|
-// }
|
|
|
public void Ast(AbstractSyntaxTree ast) {
|
|
|
this.ast = ast;
|
|
|
code = new Code("TAC", ast);
|
|
|
code.AfterClose("clear.1", new CodeClearProcessor());
|
|
|
- code.AfterClose("oti.1", new CodeOtimizadorProcessor(enableOtimization));
|
|
|
+// code.AfterClose("oti.1", new CodeOtimizadorProcessor(enableOtimization));
|
|
|
// code.AfterClose("oti.1", new CodeOtimizadorProcessor(false));
|
|
|
compileParams = ast.BuildParams();
|
|
|
|
|
@@ -323,26 +301,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
protected ArrayList<Integer> ShiftPath(int size) {
|
|
|
-
|
|
|
-// ArrayList<Integer> ret1 = new ArrayList<>(), ret2 = new ArrayList<>();
|
|
|
-// ArrayList<Integer> ret = new ArrayList<>();
|
|
|
-// ArrayList<Integer> shift = ShiftInterval(size);
|
|
|
-// Integer op = shift.get(2);
|
|
|
-//
|
|
|
-// int s1 = shift.get(op);
|
|
|
-//
|
|
|
-// ret.add(s1);
|
|
|
-// ret.add(op);
|
|
|
-//
|
|
|
-// FindShiftPath(size - s1, ret);
|
|
|
-//
|
|
|
-// System.out.println("Find SHIFT path:{" + size + "}" + ret);
|
|
|
-// return ret;
|
|
|
ArrayList<Integer> ret = new ArrayList<>();
|
|
|
-
|
|
|
FindShiftPath(size, ret);
|
|
|
-
|
|
|
-// System.out.println("Find SHIFT path:{" + size + "}" + ret + "[" + (calc(ret) == size) + "]");
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -376,42 +336,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- protected HashMap<Integer, Integer> power2 = new HashMap<Integer, Integer>() {
|
|
|
- {
|
|
|
- put(1, 0);
|
|
|
- put(2, 1);
|
|
|
- put(4, 2);
|
|
|
- put(8, 3);
|
|
|
- put(16, 4);
|
|
|
- put(32, 5);
|
|
|
- put(64, 6);
|
|
|
- put(128, 7);
|
|
|
- put(256, 8);
|
|
|
- put(512, 9);
|
|
|
- put(1024, 10);
|
|
|
- put(2048, 11);
|
|
|
- put(4096, 12);
|
|
|
- put(8192, 13);
|
|
|
- put(16384, 14);
|
|
|
- put(32768, 15);
|
|
|
- put(65536, 16);
|
|
|
- put(131072, 17);
|
|
|
- put(262144, 18);
|
|
|
- put(524288, 19);
|
|
|
- put(1048576, 20);
|
|
|
- put(2097152, 21);
|
|
|
- put(4194304, 22);
|
|
|
- put(8388608, 23);
|
|
|
- put(16777216, 24);
|
|
|
- put(33554432, 25);
|
|
|
- put(67108864, 26);
|
|
|
- put(134217728, 27);
|
|
|
- put(268435456, 28);
|
|
|
- put(536870912, 29);
|
|
|
- put(1073741824, 30);
|
|
|
-// put(2147483648, true);
|
|
|
- }
|
|
|
- };
|
|
|
+ protected LinkedHashMap<Long, Long> power2 = new LinkedHashMap<Long, Long>();
|
|
|
|
|
|
protected void FindShiftPath(int size, ArrayList<Integer> path) {
|
|
|
size = Math.abs(size);
|
|
@@ -458,6 +383,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// FindShiftPath(size, m4, op, path);
|
|
|
}
|
|
|
|
|
|
+ protected int Size(String type) {
|
|
|
+ return Tipos.Size(type) * 4;
|
|
|
+ }
|
|
|
+
|
|
|
protected String ArrayShift(Node n) throws Exception {
|
|
|
// Cria a chave para a ocorrencia de um acesso ao array, evitando recalcular o endereco
|
|
|
String key = n.getText();
|
|
@@ -475,7 +404,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
n.childrens().get(i++).S("collNumber", var.childrens().get(j).getText());
|
|
|
}
|
|
|
|
|
|
- String ret = CalcArrayAddress(n.childrens(), Tipos.Size(var.G("type")));
|
|
|
+ String ret = CalcArrayAddress(n.childrens(), Size(var.G("type")));
|
|
|
// Se o offset for calculado em um inteiro atribui o mesmo como shift
|
|
|
// if (Utils.isNumber(ret)) {
|
|
|
// shift = ret;
|
|
@@ -497,7 +426,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
for (int i = path.size() - 1; i >= 0; i--) {
|
|
|
reverse.add(path.remove(i));
|
|
|
}
|
|
|
- System.out.println(">>>>>(" + var + ", " + size + ")" + reverse);
|
|
|
+ System.out.println(">>>>>(var:" + var + ", size:" + size + ") arithpath:" + reverse);
|
|
|
|
|
|
var = GSLL(var, reverse, mps, size);
|
|
|
|
|
@@ -513,7 +442,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
switch (path.size()) {
|
|
|
case 1:
|
|
|
if (!mps) {
|
|
|
- Copy(T1, var, false).S("comment", "Copy value of index").S("copy", "true");
|
|
|
+ Copy(T1, var, false)
|
|
|
+ .S("comment", "Copy value of index")
|
|
|
+ .S("copy", "true");
|
|
|
} else {
|
|
|
T1 = var;
|
|
|
}
|
|
@@ -529,7 +460,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
// String T4 = gerarVariavel("T");
|
|
|
if (!mps) {
|
|
|
- Copy(T1, var, false).S("comment", "Copy value of index").S("copy", "true");
|
|
|
+ Copy(T1, var, false)
|
|
|
+ .S("comment", "Copy value of index")
|
|
|
+ .S("copy", "true");
|
|
|
}
|
|
|
|
|
|
if (op1 != 1) {
|
|
@@ -563,7 +496,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
protected String CalcArrayAddress(ArrayList<Node> childrens, int Size) throws Exception {
|
|
|
-
|
|
|
+ System.out.println("CalcArrayAddressINIT:" + Size);
|
|
|
boolean onlyNumber = true;
|
|
|
|
|
|
for (Node node : childrens) {
|
|
@@ -580,8 +513,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// System.out.println("CAA:" + childrens);
|
|
|
|
|
|
String current = CalcArrayAddressAux(childrens, Size);
|
|
|
+
|
|
|
System.out.println("Size {" + current + "}:----------->" + Size);
|
|
|
- // multiplica pelo tamanho
|
|
|
+
|
|
|
+// multiplica pelo tamanho
|
|
|
return GenShiftLeftLogical(current, Size, true);
|
|
|
}
|
|
|
}
|
|
@@ -900,138 +835,140 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
protected void BranchAnd(Node n, int index) throws Exception {
|
|
|
// int pos = 0;
|
|
|
OrTestAndOccurrence++;
|
|
|
+ Instruction instruction = null;
|
|
|
ArrayList<Node> x = n.childrens();
|
|
|
Node op1 = x.get(0), op2 = x.get(1);
|
|
|
+ String type = n.G("test.type"),
|
|
|
+ step = gerarLabel(),
|
|
|
+ attrs = "next,start,end,inc,test.type,skip.test";
|
|
|
|
|
|
- String step = gerarLabel(), attrs = "next,start,end,skip.test,invert.test";
|
|
|
n.S("skip.test", step);
|
|
|
|
|
|
op1.copy(attrs, n);
|
|
|
op2.copy(attrs, n);
|
|
|
|
|
|
- switch (n.parent.G("value")) {
|
|
|
- case "||":
|
|
|
- if (index == 1) {
|
|
|
- // Se for o no atual for o operando 1 do pai
|
|
|
- op1.S("next", n.parent.G("skip.test")).S("invert.test", "true");
|
|
|
- op2.S("next", n.G("start")).S("invert.test", "false");
|
|
|
-
|
|
|
- } else if (index == 2) {
|
|
|
- op1.S("invert.test", "true");
|
|
|
- op2.S("invert.test", "true");
|
|
|
-
|
|
|
- System.out.println("BA:" + n + "\b" + op1);
|
|
|
- // Se for o no atual for o operando 1 do pai
|
|
|
-// op1.S("next", n.G("next"));
|
|
|
-// op2.S("next", n.G("next"));
|
|
|
- }
|
|
|
-// pos = 2;
|
|
|
- break;
|
|
|
- case "&&":
|
|
|
- if (index == 1) {
|
|
|
- // Se for o no atual for o operando 1 do pai
|
|
|
- op1.S("invert.test", "true");
|
|
|
- op2.S("invert.test", "true");
|
|
|
- } else if (index == 2) {
|
|
|
- op2.S("invert.test", "true");
|
|
|
- op2.S("invert.test", "true");
|
|
|
- }
|
|
|
-// pos = 1;
|
|
|
- default:
|
|
|
+ instruction = Branch(op1, 1);
|
|
|
|
|
|
- }
|
|
|
+ if (instruction != null) {
|
|
|
+ switch (type) {
|
|
|
+ case "jtb":
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
|
|
|
- Branch(op1, 1);
|
|
|
+ if (n.parent.eq("value", "||")) {
|
|
|
+ if (index == 1) {
|
|
|
+ instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ } else if (index == 2) {
|
|
|
+ instruction.S("label", n.parent.G("end"));
|
|
|
+ }
|
|
|
+// } else if (n.parent.eq("value", "&&")) {
|
|
|
|
|
|
- if (index == 0 || (op1.in("value", new String[]{"||", "&&"}) && !n.eq("value", op1.G("value")))) {
|
|
|
- // Registra o label para o saldo dos testes do primeiro parametro
|
|
|
- PushLabel(step, "branch");
|
|
|
+ } else {
|
|
|
+ instruction.S("label", n.G("end"));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+// System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
|
|
|
|
|
|
- Branch(op2, 2);
|
|
|
-
|
|
|
+// if (index == 0 || (op1.in("value", new String[]{"||", "&&"}) && !n.eq("value", op1.G("value")))) {
|
|
|
+ // Registra o label para o saldo dos testes do primeiro parametro
|
|
|
+ PushLabel(step, "branch");
|
|
|
+// }
|
|
|
+ instruction = Branch(op2, 2);
|
|
|
+ if (instruction != null) {
|
|
|
+ switch (type) {
|
|
|
+ case "jtb":
|
|
|
+ if (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"));
|
|
|
+ } else if (index == 2) { // teste 2 do parametro 2
|
|
|
+ instruction.S("label", n.G("start"));
|
|
|
+ }
|
|
|
+// } else if (n.parent.eq("value", "&&")) {
|
|
|
+// if (index == 1) { // teste 1 do parametro 2 com pai &&
|
|
|
+//// throw new Exception("// teste 1 do parametro 2 com pai &&");
|
|
|
+//
|
|
|
+// } else if (index == 2) { // teste 2 do parametro 2 com pai &&
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("label", 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"));
|
|
|
+ } else if (index == 2) {
|
|
|
+// throw new Exception("teste 2 do parametro 2 geral");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
protected void BranchOr(Node n, int index) throws Exception {
|
|
|
- boolean root = index == 0;
|
|
|
- String step = gerarLabel(), attrs = "next,start,end,skip.test,invert.test";
|
|
|
+ // int pos = 0;
|
|
|
+ OrTestAndOccurrence++;
|
|
|
+ Instruction instruction = null;
|
|
|
ArrayList<Node> x = n.childrens();
|
|
|
Node op1 = x.get(0), op2 = x.get(1);
|
|
|
+ String type = n.G("test.type"),
|
|
|
+ step = gerarLabel(),
|
|
|
+ attrs = "next,start,end,inc,test.type,skip.test";
|
|
|
|
|
|
- if (root) {
|
|
|
- OrTestAndOccurrence = 0;
|
|
|
- }
|
|
|
n.S("skip.test", step);
|
|
|
|
|
|
op1.copy(attrs, n);
|
|
|
op2.copy(attrs, n);
|
|
|
|
|
|
- switch (n.parent.G("value")) {
|
|
|
- case "||":
|
|
|
- if (index == 1) {
|
|
|
- // Se for o no atual for o operando 1 do pai
|
|
|
-// op1.S("true", n.G("skip.test"));
|
|
|
- } else if (index == 2) {
|
|
|
- // Se for o no atual for o operando 2 do pai
|
|
|
-// op2.S("true", n.G("start"));
|
|
|
- } else {
|
|
|
-//
|
|
|
- }
|
|
|
-
|
|
|
- case "&&":
|
|
|
- if (index == 1) {
|
|
|
- // Se for o no atual for o operando 1 do pai
|
|
|
- op1.S("next", n.parent.G("skip.test"));
|
|
|
- op1.S("invert.test", "true");
|
|
|
-
|
|
|
- } else if (index == 2) {
|
|
|
-// op1.S("op", Complement(op1.G("op")));
|
|
|
- // Se for o no atual for o operando 2 do pai
|
|
|
- op2.S("next", n.G("start"));
|
|
|
-// op2.S("invert.test2", "true");
|
|
|
- } else {
|
|
|
-
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- Branch(op1, 1);
|
|
|
+ System.out.println("BranchOr:" + n.parent);
|
|
|
+ // Executa o lado esquerdo do ||
|
|
|
+ instruction = Branch(op1, 1);
|
|
|
|
|
|
- if (index == 0 || (op1.in("value", new String[]{"||", "&&"}) && !n.eq("value", op1.G("value")))) {
|
|
|
- // Registra o label para o saldo dos testes do primeiro parametro
|
|
|
- PushLabel(step, "branch");
|
|
|
+ if (instruction != null) {
|
|
|
+ switch (type) {
|
|
|
+ case "jtb":
|
|
|
+ if (n.parent.eq("value", "&&")) {
|
|
|
+ if (index == 1) {
|
|
|
+ instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- Branch(op2, 2);
|
|
|
-// if (pos == 2) {
|
|
|
-// // Registra o label para o saldo dos testes do primeiro parametro
|
|
|
-// PushLabel(step, "branch");
|
|
|
-// }
|
|
|
- // Se o n for a raiz do test e não ocorre uma combinação and inverte o ultimo
|
|
|
- // test para saltar o bloco
|
|
|
- if (root && OrTestAndOccurrence == 0) {
|
|
|
- Instruction inst = code.Last();
|
|
|
- code.RemoveLabelReference(inst.G("label"));
|
|
|
- String next = n.G("next");
|
|
|
- inst.S("op", Complement(inst.G("op"))).S("label", next);
|
|
|
-
|
|
|
- code.AddLabelReference(next);
|
|
|
-
|
|
|
+// System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
|
|
|
+ // Registra o label para o saldo dos testes do primeiro parametro
|
|
|
+ PushLabel(step, "branch");
|
|
|
+
|
|
|
+ // Executa o lado direito do ||
|
|
|
+ instruction = Branch(op2, 2);
|
|
|
+ if (instruction != null) {
|
|
|
+ switch (type) {
|
|
|
+ case "jtb":
|
|
|
+ if (n.parent.eq("value", "&&")) {
|
|
|
+ if (index == 1) {
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.parent.G("end"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-// branckInvert.pop();
|
|
|
}
|
|
|
|
|
|
- protected void BranchTest(Node n, int index) throws Exception {
|
|
|
-// boolean empty = branckInvert.isEmpty();
|
|
|
-//
|
|
|
-// if (empty) {
|
|
|
-// branckInvert.push(true);
|
|
|
-// }
|
|
|
-
|
|
|
- String op = n.getText(), p0, p1, label = n.G("next");
|
|
|
+ protected Instruction BranchTest(Node n, int index) throws Exception {
|
|
|
ArrayList<Node> x = n.childrens();
|
|
|
- boolean invert = true;
|
|
|
+ String type = n.G("test.type");
|
|
|
+ boolean invert = false;
|
|
|
+ String op, p0, p1, next = "";
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case "jb":
|
|
|
+ invert = true;
|
|
|
+ next = n.G("end");
|
|
|
+ break;
|
|
|
+ case "jtb":
|
|
|
+ next = n.G("start");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception("test.type '".concat(type).concat("' not suported. Values {jtb,jb}"));
|
|
|
+ }
|
|
|
|
|
|
visit(n);
|
|
|
|
|
@@ -1041,34 +978,33 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
p0 = n.G("_return");
|
|
|
p1 = "1";
|
|
|
} else { //(<, <=, >, >=, ==, !=)
|
|
|
+ op = n.getText();
|
|
|
p0 = visit(x.get(0)).G("_return");
|
|
|
p1 = visit(x.get(1)).G("_return");
|
|
|
}
|
|
|
|
|
|
- switch (n.parent.G("value")) {
|
|
|
- case "||":
|
|
|
- if (index == 1) {
|
|
|
- invert = false;
|
|
|
- label = n.G("start");
|
|
|
- }
|
|
|
- break;
|
|
|
- case "&&":
|
|
|
- break;
|
|
|
- default:
|
|
|
- // Sem combinação ( || / && )
|
|
|
- }
|
|
|
- if (n.eq("invert.test", "false")) {
|
|
|
- invert = false;
|
|
|
- }
|
|
|
-
|
|
|
+ // Inverte o teste e salta o bloco
|
|
|
if (invert) {
|
|
|
- // Inverte o teste e salta o bloco
|
|
|
op = Complement(op);
|
|
|
}
|
|
|
+
|
|
|
+ return Branch(op, p0, p1, next);
|
|
|
+
|
|
|
+// switch (n.parent.G("value")) {
|
|
|
+// case "||":
|
|
|
+// if (index == 1) {
|
|
|
+// invert = false;
|
|
|
+// next = n.G("start");
|
|
|
+// }
|
|
|
+// break;
|
|
|
+// case "&&":
|
|
|
+// break;
|
|
|
+// default:
|
|
|
+// // Sem combinação ( || / && )
|
|
|
+// }
|
|
|
// System.out.println("BranchTest:[" + index + ":" + op + ":" + invert
|
|
|
// + ":" + n.eq("invert.test", "false") + "]" + n);
|
|
|
-
|
|
|
- Instruction instruction = Branch(op, p0, p1, label);
|
|
|
+// Instruction instruction = Branch(op, p0, p1, label);
|
|
|
// instruction. ;
|
|
|
// if (n.Has("false")) {
|
|
|
// _goto(n.G("false"));
|
|
@@ -1083,7 +1019,6 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Instruction r1 = new Instruction();
|
|
|
r1.S("format", "branch")
|
|
|
.S("type", "branch")
|
|
|
- // .S("op", (!branckInvert.isEmpty() && branckInvert.peek()) ? Complement(op) : op)
|
|
|
.S("op", op)
|
|
|
.S("p1", p1)
|
|
|
.S("p2", p2)
|
|
@@ -1096,11 +1031,11 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
return r1;
|
|
|
}
|
|
|
|
|
|
- protected void Branch(Node n, int index) throws Exception {
|
|
|
- ArrayList<Node> x = n.childrens();
|
|
|
+ protected Instruction Branch(Node n, int index) throws Exception {
|
|
|
+// ArrayList<Node> x = n.childrens();
|
|
|
+ Instruction instruction = null;
|
|
|
|
|
|
switch (n.getText()) {
|
|
|
-
|
|
|
case "&&":
|
|
|
BranchAnd(n, index);
|
|
|
break;
|
|
@@ -1108,58 +1043,14 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
BranchOr(n, index);
|
|
|
break;
|
|
|
default:
|
|
|
- BranchTest(n, index);
|
|
|
-// case "||":
|
|
|
-// and = false;
|
|
|
-// // Não deve inverter operacao no caso de ||
|
|
|
-// branckInvert.push(and);
|
|
|
-//
|
|
|
-// _or(n);
|
|
|
-//
|
|
|
-// Branch(x.get(0));
|
|
|
-// Branch(x.get(1));
|
|
|
-//
|
|
|
-// // Inverte a ultima operacao
|
|
|
-// if (n.parent.eq("value", "||")) {
|
|
|
-// }
|
|
|
-//
|
|
|
-//// if (!loop && branckInvert.size() == 1) {
|
|
|
-//// Instruction last = code.Last();
|
|
|
-//// last.S("op", Complement(last.G("op"))).S("label", n.G("next"));
|
|
|
-//// }
|
|
|
-// if (!x.get(0).Has("and")) {
|
|
|
-// n.remove("and");
|
|
|
-// }
|
|
|
-// branckInvert.pop();
|
|
|
-//
|
|
|
-// break;
|
|
|
-// default:
|
|
|
-//// boolean empty = branckInvert.isEmpty();
|
|
|
-////
|
|
|
-//// if (empty) {
|
|
|
-//// branckInvert.push(true);
|
|
|
-//// }
|
|
|
-//
|
|
|
-// visit(n);
|
|
|
-// // Resultado do test class ou acesso a um id ou bool
|
|
|
-// if (!n.eq("class", "expr")) {
|
|
|
-// op = "==";
|
|
|
-// p0 = n.G("_return");
|
|
|
-// p1 = "1";
|
|
|
-// } else { //(<, <=, >, >=, ==, !=)
|
|
|
-// p0 = visit(x.get(0)).G("_return");
|
|
|
-// p1 = visit(x.get(1)).G("_return");
|
|
|
-// }
|
|
|
-//
|
|
|
-// Instruction instruction = Branch(op, p0, p1, n.G("next"));
|
|
|
-//// instruction. ;
|
|
|
-//// if (n.Has("false")) {
|
|
|
-// // _goto(n.G("false"));
|
|
|
-// // }
|
|
|
-// // if (empty) {
|
|
|
-// // branckInvert.pop();
|
|
|
-// // }
|
|
|
+ instruction = BranchTest(n, index);
|
|
|
}
|
|
|
+
|
|
|
+ if (index == 0 && instruction != null) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return instruction;
|
|
|
// if (and) {
|
|
|
// n.remove("or");
|
|
|
// }
|
|
@@ -1272,22 +1163,26 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
protected void genFor(Node n) throws Exception {
|
|
|
+ String attrs = "next,start,end,inc,test";
|
|
|
Node test = n.find("test").first();
|
|
|
|
|
|
setMaster(n);
|
|
|
|
|
|
String start = gerarLabel(),
|
|
|
- next = gerarLabel(),
|
|
|
+ // next = gerarLabel(),
|
|
|
end = gerarLabel(),
|
|
|
+ inc = gerarLabel(),
|
|
|
testLabel = gerarLabel();
|
|
|
|
|
|
- n.S("next", next)
|
|
|
- .S("start", start)
|
|
|
+ n.S("start", start)
|
|
|
.S("end", end)
|
|
|
+ .S("inc", inc)
|
|
|
.S("test", testLabel);
|
|
|
|
|
|
Node stmt = n.find("stmts");
|
|
|
- stmt.copy("next,start,end,test", n);
|
|
|
+
|
|
|
+ stmt.copy(attrs, n);
|
|
|
+ test.copy(attrs, n).S("test.type", "jtb");
|
|
|
|
|
|
/*Inicializacoes e atribuicoes*/
|
|
|
visit(n.find("initialization"));
|
|
@@ -1301,21 +1196,25 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
/*Executa o bloco do for*/
|
|
|
visit(stmt);
|
|
|
|
|
|
+ /*Label que marca os incrementos dos laco*/
|
|
|
+ PushLabel(inc, "branch");
|
|
|
+
|
|
|
/*Executa os incrementos*/
|
|
|
visit(n.find("increment"));
|
|
|
|
|
|
/*Label que marca o teste do laco*/
|
|
|
PushLabel(testLabel, "branch");
|
|
|
|
|
|
- test.copy("next,start,end,test", n).S("test.type", "for");
|
|
|
-
|
|
|
+ // test.type = ((JumpToBlock in LOOP) | (JumpBlock in IF || SWITCH))
|
|
|
+ // test.type = JumpToBlock
|
|
|
// test.S("test.type", "for").S("next", next).S("end", end).S("or", next);
|
|
|
// System.out.println("Teste:" + test);
|
|
|
Branch(test, 0);
|
|
|
|
|
|
- if (n.Has("set.end_label")) {
|
|
|
- PushLabel(n.G("end"), "branch");
|
|
|
- }
|
|
|
+// if (n.Has("set.end_label")) {
|
|
|
+// PushLabel(n.G("end"), "branch");
|
|
|
+ PushLabel(end, "branch");
|
|
|
+// }
|
|
|
|
|
|
setNullMaster();
|
|
|
}
|
|
@@ -1909,10 +1808,16 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
switch (n.getText()) {
|
|
|
case "continue":
|
|
|
- _goto(container.G("start"));
|
|
|
+ String go;
|
|
|
+ if (container.eq("class", "for.stmt")) {
|
|
|
+ go = container.G("inc");
|
|
|
+ } else {
|
|
|
+ go = container.G("end");
|
|
|
+ }
|
|
|
+ _goto(go);
|
|
|
break;
|
|
|
case "break":
|
|
|
- _goto(container.G("next"));
|
|
|
+ _goto(container.G("end"));
|
|
|
break;
|
|
|
case "goto":
|
|
|
_goto(n.G("label"));
|
|
@@ -2253,3 +2158,66 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+// case "||":
|
|
|
+// and = false;
|
|
|
+// // Não deve inverter operacao no caso de ||
|
|
|
+// branckInvert.push(and);
|
|
|
+//
|
|
|
+// _or(n);
|
|
|
+//
|
|
|
+// Branch(x.get(0));
|
|
|
+// Branch(x.get(1));
|
|
|
+//
|
|
|
+// // Inverte a ultima operacao
|
|
|
+// if (n.parent.eq("value", "||")) {
|
|
|
+// }
|
|
|
+//
|
|
|
+//// if (!loop && branckInvert.size() == 1) {
|
|
|
+//// Instruction last = code.Last();
|
|
|
+//// last.S("op", Complement(last.G("op"))).S("label", n.G("next"));
|
|
|
+//// }
|
|
|
+// if (!x.get(0).Has("and")) {
|
|
|
+// n.remove("and");
|
|
|
+// }
|
|
|
+// branckInvert.pop();
|
|
|
+//
|
|
|
+// break;
|
|
|
+// default:
|
|
|
+//// boolean empty = branckInvert.isEmpty();
|
|
|
+////
|
|
|
+//// if (empty) {
|
|
|
+//// branckInvert.push(true);
|
|
|
+//// }
|
|
|
+//
|
|
|
+// visit(n);
|
|
|
+// // Resultado do test class ou acesso a um id ou bool
|
|
|
+// if (!n.eq("class", "expr")) {
|
|
|
+// op = "==";
|
|
|
+// p0 = n.G("_return");
|
|
|
+// p1 = "1";
|
|
|
+// } else { //(<, <=, >, >=, ==, !=)
|
|
|
+// p0 = visit(x.get(0)).G("_return");
|
|
|
+// p1 = visit(x.get(1)).G("_return");
|
|
|
+// }
|
|
|
+//
|
|
|
+// Instruction instruction = Branch(op, p0, p1, n.G("next"));
|
|
|
+//// instruction. ;
|
|
|
+//// if (n.Has("false")) {
|
|
|
+// // _goto(n.G("false"));
|
|
|
+// // }
|
|
|
+// // if (empty) {
|
|
|
+// // branckInvert.pop();
|
|
|
+// // }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public TacGenInterface AfterCreate(CodeProcessing p) {
|
|
|
+// this.afterCreate.add(p);
|
|
|
+// return this;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public TacGenInterface AfterCreate() throws Exception {
|
|
|
+// for (CodeProcessing x : afterCreate) {
|
|
|
+// x.Exec(code);
|
|
|
+// }
|
|
|
+// return this;
|
|
|
+// }
|