|
@@ -109,7 +109,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
public void Ast(AbstractSyntaxTree ast) {
|
|
|
this.ast = ast;
|
|
|
code = new Code("TAC", ast);
|
|
|
- code.AfterClose("clear.1", new CodeClearProcessor());
|
|
|
+// code.AfterClose("clear.1", new CodeClearProcessor());
|
|
|
// code.AfterClose("oti.1", new CodeOtimizadorProcessor(enableOtimization));
|
|
|
// code.AfterClose("oti.1", new CodeOtimizadorProcessor(false));
|
|
|
compileParams = ast.BuildParams();
|
|
@@ -738,11 +738,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
String end = gerarLabel(), next;
|
|
|
boolean ifChain = n.childrens().size() > 1;
|
|
|
|
|
|
- if (ifChain) { //(If)+ (else)?
|
|
|
- next = gerarLabel();
|
|
|
- } else {
|
|
|
- next = end;
|
|
|
- }
|
|
|
+ //(If)+ (else)?
|
|
|
+ next = ifChain ? gerarLabel() : end;
|
|
|
|
|
|
n.S("next", next).S("end", end);
|
|
|
|
|
@@ -779,8 +776,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Node test = _if.first().first();
|
|
|
Node stmts = _if.find("stmts");
|
|
|
|
|
|
+ // Se o teste falha deve saltar o bloco
|
|
|
n.S("start", gerarLabel());
|
|
|
- test.copy("start,next,end", n);
|
|
|
+ test.S("test.type", "jb").copy("next,start,end", n);
|
|
|
|
|
|
Branch(test, 0);
|
|
|
// marca com um label o inicio do bloco do if
|
|
@@ -840,7 +838,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
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";
|
|
|
+ attrs = "next,end,inc,test.type,skip.test";
|
|
|
|
|
|
n.S("skip.test", step);
|
|
|
|
|
@@ -865,6 +863,18 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
} else {
|
|
|
instruction.S("label", n.G("end"));
|
|
|
}
|
|
|
+ break;
|
|
|
+ case "jb":
|
|
|
+ if (n.parent.eq("value", "||")) {
|
|
|
+ if (index <= 1) {
|
|
|
+ instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ } else if (index == 2) {
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.G("next"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
// System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
|
|
@@ -882,7 +892,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
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"));
|
|
|
+ instruction.S("label", n.G("next"));
|
|
|
}
|
|
|
// } else if (n.parent.eq("value", "&&")) {
|
|
|
// if (index == 1) { // teste 1 do parametro 2 com pai &&
|
|
@@ -900,6 +910,24 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// throw new Exception("teste 2 do parametro 2 geral");
|
|
|
}
|
|
|
}
|
|
|
+ break;
|
|
|
+ case "jb":
|
|
|
+
|
|
|
+ System.out.println("I(jb:and:2)[" + index + "]\n" + instruction + "\n" + n);
|
|
|
+
|
|
|
+ if (n.parent.eq("value", "||")) {
|
|
|
+ if (index <= 1) {
|
|
|
+// instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.G("start"));
|
|
|
+
|
|
|
+ } else if (index == 2) {
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.G("next"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -919,7 +947,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
op1.copy(attrs, n);
|
|
|
op2.copy(attrs, n);
|
|
|
|
|
|
- System.out.println("BranchOr:" + n.parent);
|
|
|
+// System.out.println("BranchOr:" + n.parent);
|
|
|
// Executa o lado esquerdo do ||
|
|
|
instruction = Branch(op1, 1);
|
|
|
|
|
@@ -931,6 +959,31 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
instruction.S("label", n.parent.G("skip.test"));
|
|
|
}
|
|
|
}
|
|
|
+ break;
|
|
|
+ case "jb":
|
|
|
+// System.out.println("I(jb:1)[" + index + "]\n" + instruction + "\n" + n);
|
|
|
+
|
|
|
+ if (n.parent.eq("value", "||")) {
|
|
|
+// if (index == 2) {
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.G("start"));
|
|
|
+// }
|
|
|
+ } else if (n.parent.eq("value", "&&")) {
|
|
|
+ if (index <= 1) {
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.parent.G("skip.test"));
|
|
|
+ } else if (index == 2) {
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.parent.G("start"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ if (index <= 1) {
|
|
|
+ instruction.S("label", n.G("start"));
|
|
|
+ } else if (index == 2) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
// System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
|
|
@@ -948,6 +1001,31 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
instruction.S("label", n.parent.G("end"));
|
|
|
}
|
|
|
}
|
|
|
+ break;
|
|
|
+ case "jb":
|
|
|
+ if (n.parent.eq("value", "||")) {
|
|
|
+ if (index <= 1) {
|
|
|
+
|
|
|
+ instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.G("start"));
|
|
|
+
|
|
|
+ } else if (index == 2) {
|
|
|
+//
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("label", n.G("next"));
|
|
|
+ }
|
|
|
+ } else if (n.parent.eq("value", "&&")) {
|
|
|
+ if (index == 1) {
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("label", n.parent.G("start"));
|
|
|
+ } else if (index == 2) {
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("label", n.parent.G("start"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("label", n.G("next"));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -956,15 +1034,13 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
ArrayList<Node> x = n.childrens();
|
|
|
String type = n.G("test.type");
|
|
|
boolean invert = false;
|
|
|
- String op, p0, p1, next = "";
|
|
|
+ String op, p0, p1;
|
|
|
|
|
|
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}"));
|
|
@@ -987,44 +1063,17 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (invert) {
|
|
|
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. ;
|
|
|
-// if (n.Has("false")) {
|
|
|
- // _goto(n.G("false"));
|
|
|
- // }
|
|
|
- // if (empty) {
|
|
|
- // branckInvert.pop();
|
|
|
- // }
|
|
|
- }
|
|
|
-
|
|
|
- protected Instruction Branch(String op, final String p1, final String p2, String label) throws Exception {
|
|
|
-
|
|
|
+// return Branch(op, p0, p1, next);
|
|
|
Instruction r1 = new Instruction();
|
|
|
+
|
|
|
r1.S("format", "branch")
|
|
|
.S("type", "branch")
|
|
|
.S("op", op)
|
|
|
- .S("p1", p1)
|
|
|
- .S("p2", p2)
|
|
|
- .S("p1value", "" + Api.IsValue(p1))
|
|
|
- .S("p2value", "" + Api.IsValue(p2))
|
|
|
- .S("label", label);
|
|
|
+ .S("p1", p0)
|
|
|
+ .S("p2", p1)
|
|
|
+ .S("p1value", "" + Api.IsValue(p0))
|
|
|
+ .S("p2value", "" + Api.IsValue(p1))
|
|
|
+ .S("label", n.G("next"));
|
|
|
|
|
|
// System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
|
|
|
code.Add(r1);
|
|
@@ -1032,7 +1081,6 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
protected Instruction Branch(Node n, int index) throws Exception {
|
|
|
-// ArrayList<Node> x = n.childrens();
|
|
|
Instruction instruction = null;
|
|
|
|
|
|
switch (n.getText()) {
|
|
@@ -1045,62 +1093,13 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
default:
|
|
|
instruction = BranchTest(n, index);
|
|
|
}
|
|
|
-
|
|
|
- if (index == 0 && instruction != null) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
return instruction;
|
|
|
-// if (and) {
|
|
|
-// n.remove("or");
|
|
|
-// }
|
|
|
}
|
|
|
|
|
|
protected String Complement(String op) {
|
|
|
return Complements.get(op);
|
|
|
}
|
|
|
|
|
|
-// /**
|
|
|
-// * ProceSSa um teSte logico AND
|
|
|
-// *
|
|
|
-// * @param n
|
|
|
-// * @throws Exception
|
|
|
-// */
|
|
|
-// protected void _and(Node n) throws Exception {
|
|
|
-// String attrs = "next,start,end,test,skip.test";
|
|
|
-// Node b1 = n.getFilho(0).copy(attrs, n);
|
|
|
-// Node b2 = n.getFilho(1).copy(attrs, n);
|
|
|
-//
|
|
|
-//// boolean isFor = n.eq("test.type", "for");
|
|
|
-//// System.out.println("_AND{" + isFor + "}:" + n.G(isFor ? "and" : "next"));
|
|
|
-//// String l = n.G(n.eq("test.type", "for") ? "and" : "next");
|
|
|
-// b1.S("true", n.G("next"));
|
|
|
-//// b1.S("false", b.G("false"));
|
|
|
-//// System.out.println("_AND:" + label + "-s" + b1);
|
|
|
-// b2.S("true", n.G("next"));
|
|
|
-//// b2.S("false", b.G("false"));
|
|
|
-//// b1.S("true", gerarLabel());
|
|
|
-//// b1.S("false", b.G("false"));
|
|
|
-////
|
|
|
-//// b2.S("true", b.G("true"));
|
|
|
-//// b2.S("false", b.G("false"));
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * ProceSSa um teSte logico OR
|
|
|
-// *
|
|
|
-// * @param n
|
|
|
-// * @throws Exception
|
|
|
-// */
|
|
|
-// protected void _or(Node n) throws Exception {
|
|
|
-//
|
|
|
-// String attrs = "next,start,end,test,skip.test";
|
|
|
-// Node b1 = n.getFilho(0).copy(attrs, n);
|
|
|
-// Node b2 = n.getFilho(1).copy(attrs, n);
|
|
|
-//
|
|
|
-// b1.S("true", n.G("start"));
|
|
|
-// b2.S("true", n.G("start"));
|
|
|
-//
|
|
|
-// }
|
|
|
/**
|
|
|
* Cria uma linha de Salto para PuShLabel eSpecificado no parametro.
|
|
|
*
|
|
@@ -1147,14 +1146,6 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.set("locker", getIdMaster()));
|
|
|
|
|
|
}
|
|
|
-//
|
|
|
-// protected String updateLabel(Node n) {
|
|
|
-// String PushLabel = n.G("_return");
|
|
|
-//// if (!PushLabel.substring(0, 1).equals("_R")) {
|
|
|
-//// }
|
|
|
-//// n.S("_return", PushLabel);
|
|
|
-// return PushLabel;
|
|
|
-// }
|
|
|
|
|
|
/**
|
|
|
* Gera todoS oS SaltoS de um loop for
|
|
@@ -1163,18 +1154,18 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
protected void genFor(Node n) throws Exception {
|
|
|
- String attrs = "next,start,end,inc,test";
|
|
|
+ String attrs = "next,end,inc,test";
|
|
|
Node test = n.find("test").first();
|
|
|
|
|
|
setMaster(n);
|
|
|
|
|
|
- String start = gerarLabel(),
|
|
|
+ String next = gerarLabel(),
|
|
|
// next = gerarLabel(),
|
|
|
end = gerarLabel(),
|
|
|
inc = gerarLabel(),
|
|
|
testLabel = gerarLabel();
|
|
|
|
|
|
- n.S("start", start)
|
|
|
+ n.S("next", next)
|
|
|
.S("end", end)
|
|
|
.S("inc", inc)
|
|
|
.S("test", testLabel);
|
|
@@ -1191,7 +1182,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
_goto(testLabel);
|
|
|
|
|
|
/*Marca o inicio do bloco do laco*/
|
|
|
- PushLabel(start, "branch");
|
|
|
+ PushLabel(next, "branch");
|
|
|
|
|
|
/*Executa o bloco do for*/
|
|
|
visit(stmt);
|
|
@@ -2221,3 +2212,67 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// }
|
|
|
// return this;
|
|
|
// }
|
|
|
+// protected Instruction Branch(String op, final String p1, final String p2, String label) throws Exception {
|
|
|
+//
|
|
|
+// Instruction r1 = new Instruction();
|
|
|
+// r1.S("format", "branch")
|
|
|
+// .S("type", "branch")
|
|
|
+// .S("op", op)
|
|
|
+// .S("p1", p1)
|
|
|
+// .S("p2", p2)
|
|
|
+// .S("p1value", "" + Api.IsValue(p1))
|
|
|
+// .S("p2value", "" + Api.IsValue(p2))
|
|
|
+// .S("label", label);
|
|
|
+//
|
|
|
+//// System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
|
|
|
+// code.Add(r1);
|
|
|
+// return r1;
|
|
|
+// }
|
|
|
+// /**
|
|
|
+// * ProceSSa um teSte logico AND
|
|
|
+// *
|
|
|
+// * @param n
|
|
|
+// * @throws Exception
|
|
|
+// */
|
|
|
+// protected void _and(Node n) throws Exception {
|
|
|
+// String attrs = "next,start,end,test,skip.test";
|
|
|
+// Node b1 = n.getFilho(0).copy(attrs, n);
|
|
|
+// Node b2 = n.getFilho(1).copy(attrs, n);
|
|
|
+//
|
|
|
+//// boolean isFor = n.eq("test.type", "for");
|
|
|
+//// System.out.println("_AND{" + isFor + "}:" + n.G(isFor ? "and" : "next"));
|
|
|
+//// String l = n.G(n.eq("test.type", "for") ? "and" : "next");
|
|
|
+// b1.S("true", n.G("next"));
|
|
|
+//// b1.S("false", b.G("false"));
|
|
|
+//// System.out.println("_AND:" + label + "-s" + b1);
|
|
|
+// b2.S("true", n.G("next"));
|
|
|
+//// b2.S("false", b.G("false"));
|
|
|
+//// b1.S("true", gerarLabel());
|
|
|
+//// b1.S("false", b.G("false"));
|
|
|
+////
|
|
|
+//// b2.S("true", b.G("true"));
|
|
|
+//// b2.S("false", b.G("false"));
|
|
|
+// }
|
|
|
+// /**
|
|
|
+// * ProceSSa um teSte logico OR
|
|
|
+// *
|
|
|
+// * @param n
|
|
|
+// * @throws Exception
|
|
|
+// */
|
|
|
+// protected void _or(Node n) throws Exception {
|
|
|
+//
|
|
|
+// String attrs = "next,start,end,test,skip.test";
|
|
|
+// Node b1 = n.getFilho(0).copy(attrs, n);
|
|
|
+// Node b2 = n.getFilho(1).copy(attrs, n);
|
|
|
+//
|
|
|
+// b1.S("true", n.G("start"));
|
|
|
+// b2.S("true", n.G("start"));
|
|
|
+//
|
|
|
+// }//
|
|
|
+// protected String updateLabel(Node n) {
|
|
|
+// String PushLabel = n.G("_return");
|
|
|
+//// if (!PushLabel.substring(0, 1).equals("_R")) {
|
|
|
+//// }
|
|
|
+//// n.S("_return", PushLabel);
|
|
|
+// return PushLabel;
|
|
|
+// }
|