|
@@ -1,18 +1,14 @@
|
|
|
-/*
|
|
|
- * To change this license header, choose License Headers in Project Properties.
|
|
|
- * To change this template file, choose Tools | Templates
|
|
|
- * and open the template in the editor.
|
|
|
- */
|
|
|
package IntermediaryCode;
|
|
|
|
|
|
import API.Api;
|
|
|
import API.Functions;
|
|
|
-import API.Instruction;
|
|
|
-import API.Tipos;
|
|
|
+import API.Types;
|
|
|
import API.Utils;
|
|
|
import API.Variaveis;
|
|
|
import ast.AbstractSyntaxTree;
|
|
|
import ast.Node;
|
|
|
+import common.Code;
|
|
|
+import common.Instruction;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
@@ -20,27 +16,20 @@ import java.util.LinkedList;
|
|
|
import java.util.Map;
|
|
|
import java.util.Stack;
|
|
|
import java.util.regex.Pattern;
|
|
|
-// a0 a1
|
|
|
-// sync recebe mandando
|
|
|
-// 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
|
|
|
*/
|
|
|
-public final class BaseTacGen implements TacGenInterface {
|
|
|
+public final class IRGenerator implements IRGenInterface {
|
|
|
|
|
|
protected boolean inicializado = false;
|
|
|
protected int incLabel = 0;
|
|
|
protected int tmpVarCount = 1;
|
|
|
-// protected int OrTestAndOccurrence = 0;
|
|
|
-
|
|
|
protected AbstractSyntaxTree ast;
|
|
|
protected Code code;
|
|
|
protected Node calEndAtt;
|
|
|
- protected Node compileParams;
|
|
|
protected String label;
|
|
|
protected String calEndVar;
|
|
|
protected String calEndSize;
|
|
@@ -54,26 +43,32 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
protected HashMap<String, Integer> systemCalls = new HashMap<>();
|
|
|
protected ArrayList<String> currentMatrixvalues = new ArrayList<>();
|
|
|
protected HashMap<String, ArrayList<String>> matrixValues = new HashMap<>();
|
|
|
-// protected ArrayList<CodeProcessing> afterCreate = new ArrayList<CodeProcessing>();
|
|
|
- protected HashMap<String, String> Complements = new HashMap<String, String>() {
|
|
|
+
|
|
|
+ protected LinkedHashMap<String, Long> power2 = new LinkedHashMap<String, Long>() {
|
|
|
{
|
|
|
- put("!=", "==");
|
|
|
- put("==", "!=");
|
|
|
- put("<", ">=");
|
|
|
- put("<=", ">");
|
|
|
- put(">", "<=");
|
|
|
- put(">=", "<");
|
|
|
+ for (long i = 0; i < 31; i++) {
|
|
|
+ put(Long.toString(Long.valueOf(1 << i)), Long.valueOf(i));
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
public static Pattern AddressOperator = Pattern.compile("(\\&|\\*)");
|
|
|
protected boolean enableOtimization = true;
|
|
|
protected HashMap<String, String> ArrayShiftMap = new HashMap<>();
|
|
|
- private int jb = 0;
|
|
|
+ protected int jb = 0;
|
|
|
|
|
|
- public BaseTacGen() throws Exception {
|
|
|
- for (long i = 0; i < 31; i++) {
|
|
|
- power2.put(Long.toString(Long.valueOf(1 << i)), Long.valueOf(i));
|
|
|
+ protected Stack<Boolean> loopScope = new Stack<Boolean>() {
|
|
|
+ {
|
|
|
+ push(false);
|
|
|
}
|
|
|
+ };
|
|
|
+
|
|
|
+ public IRGenerator() throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public IRGenerator(AbstractSyntaxTree ast) {
|
|
|
+ Ast(ast);
|
|
|
}
|
|
|
|
|
|
protected int calc(ArrayList<Integer> data) {
|
|
@@ -104,31 +99,29 @@ 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);
|
|
|
- }
|
|
|
-
|
|
|
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(false));
|
|
|
- compileParams = ast.BuildParams();
|
|
|
-
|
|
|
- code.Template(new template.Template())
|
|
|
+ code = new Code("IR", ast);
|
|
|
+ code
|
|
|
+ // Remove labels não referenciadas
|
|
|
+ .AfterClose("ot.UnusedLabels", new RemoveUnusedLabelsProcessor())
|
|
|
+ // Remove instrucoes mortas
|
|
|
+ // Remove variaveis que são escrias apenas uma vez. Transformando em constantes
|
|
|
+ // .AfterClose("ot.VariablesConstants", new VariablesConstantsProcessor())
|
|
|
+ // .AfterClose("ot.DeadInstructions", new DeadInstructionsProcessor())
|
|
|
+ // // Remove copias
|
|
|
+ .AfterClose("oti.1", new CodeOtimizadorProcessor(enableOtimization))
|
|
|
+ // Especifica o template engine
|
|
|
+ .Template(new template.Template())
|
|
|
+ // Especifica os templates das instruções
|
|
|
.Formats(new HashMap<String, String>() {
|
|
|
{
|
|
|
-
|
|
|
String base = "{[PAD(global.position,_addressLen,' ')]':'}{[T(1)]}{[PAD(block.position,_addressLen,' ')]':'}{[T(2)]}";
|
|
|
- String end = "{[T(2)]}{' T< '[type]' >'}";
|
|
|
+ String end = "{[T(2)]}{' T< '[type]' >'} {[T(2)]}{[basicBlock]}";
|
|
|
|
|
|
put("label", "{[PAD(global.position,_addressLen,' ')]':'}{[T(2)]}{'<'[label]'>'}':'");
|
|
|
put("assign", base + "{[dst]}' := '{[p1]}{' '[op]' '}{[p2]}{' --'[comment]}" + end);
|
|
|
- put("pointer_assignment", base + "{[dst.pointer]}{[dst]' := '}{[op]}{[p1]}{' --'[comment]}" + end);
|
|
|
+ put("pointer_assign", base + "{[dst.pointer]}{[dst]' := '}{[op]}{[p1]}{' --'[comment]}" + end);
|
|
|
put("unary", base + " {[dst]} ' := '{[op]' '}{[p1]} {' --'[comment]}" + end);
|
|
|
put("copy", base + " {[dst]} ' := '{[p1]} {' --'[comment]}" + end);
|
|
|
put("jump", base + " 'goto ' {'<'[label]'>'} {' --'[comment]}" + end);
|
|
@@ -159,13 +152,11 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// setOtimizacao(settings.G("clearCode").equals("true"));
|
|
|
Code code = run();
|
|
|
// Executa todos os tratametos definidos apos geracao do codigo
|
|
|
-// AfterCreate();
|
|
|
|
|
|
printVarMap();
|
|
|
|
|
|
-// code.PrintData();
|
|
|
code.Print();
|
|
|
- code.printOcorrences();
|
|
|
+
|
|
|
return code;
|
|
|
}
|
|
|
|
|
@@ -188,8 +179,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
case "dec.var":
|
|
|
// Log.PrintInfo("TAC", new Instruction().S("msg", "DECVAR." + n));
|
|
|
if (n.find("exprs") != null) {
|
|
|
- n.S("class", "dec.var.short")
|
|
|
- .S("value", "dec.var.short");
|
|
|
+ n.S("class", "dec.var.short").S("value", "dec.var.short");
|
|
|
genAssign(n);
|
|
|
break;
|
|
|
}
|
|
@@ -262,7 +252,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("p1value", "" + Api.IsValue(dst))
|
|
|
.S("p2value", "true")
|
|
|
.S("op", n.findByClass("operator").eq("value", "++") ? "+" : "-");
|
|
|
- code.Add(operacao);
|
|
|
+ Add(operacao);
|
|
|
}
|
|
|
|
|
|
protected void Selector(Node n) throws Exception {
|
|
@@ -352,8 +342,6 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- protected LinkedHashMap<String, Long> power2 = new LinkedHashMap<String, Long>();
|
|
|
-
|
|
|
protected void FindShiftPath(int size, ArrayList<Integer> path) {
|
|
|
size = Math.abs(size);
|
|
|
ArrayList<Integer> shift = ShiftInterval(size);
|
|
@@ -404,7 +392,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
protected int Size(String type) {
|
|
|
- return Tipos.Size(type) * 4;
|
|
|
+ return Types.Size(type) * 4;
|
|
|
}
|
|
|
|
|
|
protected String ArrayShift(Node n) throws Exception {
|
|
@@ -761,11 +749,16 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Node stmts = main.find("stmts");
|
|
|
|
|
|
if (stmts.childrens().size() > 0 && code.OpenBlock("main")) {
|
|
|
+// basicBlock.push("main");
|
|
|
+// basicBlockCount.push(0);
|
|
|
|
|
|
PushLabel("main", "block");
|
|
|
visit(stmts);
|
|
|
|
|
|
PushLabel("main-end", "block");
|
|
|
+
|
|
|
+// basicBlock.pop();
|
|
|
+// basicBlockCount.pop();
|
|
|
code.CloseBlock();
|
|
|
return true;
|
|
|
}
|
|
@@ -787,7 +780,6 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
}
|
|
|
code.UpdatePositions();
|
|
|
-
|
|
|
return code;
|
|
|
}
|
|
|
|
|
@@ -836,12 +828,22 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
protected void IfCaseStmt(Node n, Node _if, boolean ifChain) throws Exception {
|
|
|
- Node test = _if.first().first();
|
|
|
+ Node test = _if.first();
|
|
|
+ String testType = "jb";
|
|
|
+
|
|
|
+ if (test.childrens().size() > 1) {
|
|
|
+ visit(test.childrens().get(0));
|
|
|
+ test = test.last();
|
|
|
+ } else {
|
|
|
+ test = test.first();
|
|
|
+ }
|
|
|
+
|
|
|
Node stmts = _if.find("stmts");
|
|
|
|
|
|
// Se o teste falha deve saltar o bloco
|
|
|
n.S("start", gerarLabel());
|
|
|
- test.S("test.type", "jb").copy("next,start,end", n);
|
|
|
+
|
|
|
+ test.S("test.type", testType).copy("next,start,end", n);
|
|
|
|
|
|
Branch(test, 0);
|
|
|
// marca com um label o inicio do bloco do if
|
|
@@ -882,10 +884,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
|
|
|
protected void _visitExpr(Node n) throws Exception {
|
|
|
-// System.out.println("VisitExpr:" + n);
|
|
|
visit(n.childrens().get(0));
|
|
|
visit(n.childrens().get(1));
|
|
|
|
|
|
+// System.out.println("VisitExpr:" + n);
|
|
|
switch (n.G("subclass")) {
|
|
|
case "arith":// ( + | - | * | / ) integer
|
|
|
genExpArith(n);
|
|
@@ -916,7 +918,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (instruction != null) {
|
|
|
switch (type) {
|
|
|
case "jtb":
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
|
|
|
if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index == 1) {
|
|
@@ -937,7 +939,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
|
|
|
} else if (index == 2) {
|
|
|
-// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
}
|
|
@@ -958,7 +960,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
case "jtb":
|
|
|
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("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("end"));
|
|
|
} else if (index == 2) { // teste 2 do parametro 2
|
|
|
UpdateLabel(instruction, n.G("next"));
|
|
@@ -968,12 +970,12 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
//// 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("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
// UpdateLabel(instruction, n.G("end"));
|
|
|
// }
|
|
|
} else {
|
|
|
if (index == 1) { // teste 1 do parametro 2 geral
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("end"));
|
|
|
} else if (index == 2) {
|
|
|
// throw new Exception("teste 2 do parametro 2 geral");
|
|
@@ -987,11 +989,11 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index <= 1) {
|
|
|
// UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("start"));
|
|
|
|
|
|
} else if (index == 2) {
|
|
|
-// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("next"));
|
|
|
|
|
|
}
|
|
@@ -1050,20 +1052,20 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// if (n.parent != null) {
|
|
|
if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
// if (index == 2) {
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("start"));
|
|
|
// }
|
|
|
} else if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index <= 1) {
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.parent.G("skip.test"));
|
|
|
} else if (index == 2) {
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.parent.G("start"));
|
|
|
}
|
|
|
// }
|
|
|
} else {
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
if (index <= 1) {
|
|
|
UpdateLabel(instruction, n.G("start"));
|
|
|
} else if (index == 2) {
|
|
@@ -1083,7 +1085,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
case "jtb":
|
|
|
if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index == 1) {
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.parent.G("end"));
|
|
|
}
|
|
|
}
|
|
@@ -1093,25 +1095,25 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (n.parent != null && n.parent.eq("value", "||")) {
|
|
|
if (index <= 1) {
|
|
|
|
|
|
- instruction.S("op", Complement(instruction.G("op")));
|
|
|
+ instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("start"));
|
|
|
|
|
|
} else if (index == 2) {
|
|
|
//
|
|
|
-// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
} else if (n.parent != null && n.parent.eq("value", "&&")) {
|
|
|
if (index == 1) {
|
|
|
-// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
// UpdateLabel(instruction, n.parent.G("start"));
|
|
|
} else if (index == 2) {
|
|
|
-// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
// UpdateLabel(instruction, n.parent.G("start"));
|
|
|
}
|
|
|
// }
|
|
|
} else {
|
|
|
-// instruction.S("op", Complement(instruction.G("op")));
|
|
|
+// instruction.S("op", Utils.ComplementOperation(instruction.G("op")));
|
|
|
// UpdateLabel(instruction, n.G("next"));
|
|
|
}
|
|
|
}
|
|
@@ -1149,7 +1151,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
// Inverte o teste e salta o bloco
|
|
|
if (invert) {
|
|
|
- op = Complement(op);
|
|
|
+ op = Utils.ComplementOperation(op);
|
|
|
}
|
|
|
// return Branch(op, p0, p1, next);
|
|
|
Instruction r1 = new Instruction();
|
|
@@ -1164,13 +1166,30 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("label", n.G("next"));
|
|
|
|
|
|
// System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
|
|
|
- code.Add(r1);
|
|
|
+ Add(r1);
|
|
|
return r1;
|
|
|
}
|
|
|
|
|
|
+ protected Instruction Add(Instruction i) throws Exception {
|
|
|
+
|
|
|
+ return code.Add(
|
|
|
+ i.S("inloop", "" + loopScope.peek())
|
|
|
+ // .S("basicBlock", basicBlock.peek() + "." + basicBlockCount.peek())
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
protected Instruction Branch(Node n, int index) throws Exception {
|
|
|
Instruction instruction = null;
|
|
|
|
|
|
+// System.out.println("test:" + n);
|
|
|
+// ArrayList<Node> childrens = n.parent.childrens();
|
|
|
+// if (childrens.size() > 1) {
|
|
|
+//// visit(childrens.get(0));
|
|
|
+// Node c = n;
|
|
|
+// n = childrens.get(1);
|
|
|
+// n.copy("test.type,next,start,end", c);
|
|
|
+// }
|
|
|
+// System.out.println("n:" + n);
|
|
|
switch (n.getText()) {
|
|
|
case "&&":
|
|
|
BranchAnd(n, index);
|
|
@@ -1184,10 +1203,6 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
return instruction;
|
|
|
}
|
|
|
|
|
|
- protected String Complement(String op) {
|
|
|
- return Complements.get(op);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Cria uma linha de Salto para PuShLabel eSpecificado no parametro.
|
|
|
*
|
|
@@ -1200,7 +1215,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("type", "jump")
|
|
|
.S("format", "jump")
|
|
|
.set("locker", getIdMaster());
|
|
|
- code.Add(r);
|
|
|
+ Add(r);
|
|
|
|
|
|
}
|
|
|
|
|
@@ -1223,10 +1238,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("p1value", "" + Api.IsValue(ret.getText()))
|
|
|
.S("p1", ret.G("_return"));
|
|
|
nreg.set("locker", getIdMaster());
|
|
|
- code.Add(nreg);
|
|
|
+ Add(nreg);
|
|
|
}
|
|
|
|
|
|
- code.Add(new Instruction()
|
|
|
+ Add(new Instruction()
|
|
|
.S("type", "return")
|
|
|
.S("format", "return")
|
|
|
.set("p1", returns.size())
|
|
@@ -1243,7 +1258,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
*/
|
|
|
protected void genFor(Node n) throws Exception {
|
|
|
String attrs = "next,end,inc,test";
|
|
|
- Node test = n.find("test").first();
|
|
|
+
|
|
|
+ loopScope.push(true);
|
|
|
|
|
|
setMaster(n);
|
|
|
|
|
@@ -1261,13 +1277,22 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Node stmt = n.find("stmts");
|
|
|
|
|
|
stmt.copy(attrs, n);
|
|
|
- test.copy(attrs, n).S("test.type", "jtb");
|
|
|
+
|
|
|
+ boolean hasTest = false;
|
|
|
+ Node t = n.find("test"), test = null;
|
|
|
+
|
|
|
+ if (t.childrens().size() > 0) {
|
|
|
+ hasTest = true;
|
|
|
+ test = t.first();
|
|
|
+ test.copy(attrs, n).S("test.type", "jtb");
|
|
|
+ }
|
|
|
|
|
|
/*Inicializacoes e atribuicoes*/
|
|
|
visit(n.find("initialization"));
|
|
|
-
|
|
|
- /*Salta para o teste do laco*/
|
|
|
- _goto(testLabel);
|
|
|
+ if (hasTest) {
|
|
|
+ /*Salta para o teste do laco*/
|
|
|
+ _goto(testLabel);
|
|
|
+ }
|
|
|
|
|
|
/*Marca o inicio do bloco do laco*/
|
|
|
PushLabel(next, "branch");
|
|
@@ -1280,22 +1305,24 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
/*Executa os incrementos*/
|
|
|
visit(n.find("increment"));
|
|
|
-
|
|
|
- /*Label que marca o teste do laco*/
|
|
|
- PushLabel(testLabel, "branch");
|
|
|
-
|
|
|
// 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 (hasTest) {
|
|
|
+ /*Label que marca o teste do laco*/
|
|
|
+ PushLabel(testLabel, "branch");
|
|
|
+
|
|
|
+ Branch(test, 0);
|
|
|
+ } else {
|
|
|
+ _goto(next);
|
|
|
+ }
|
|
|
|
|
|
-// if (n.Has("set.end_label")) {
|
|
|
-// PushLabel(n.G("end"), "branch");
|
|
|
PushLabel(end, "branch");
|
|
|
-// }
|
|
|
|
|
|
setNullMaster();
|
|
|
+
|
|
|
+ loopScope.pop();
|
|
|
}
|
|
|
// protected void genFor(Node n) throws Exception {
|
|
|
// setMaster(n);
|
|
@@ -1345,6 +1372,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Node op2 = n.childrens.get(1);
|
|
|
|
|
|
String _op = n.getText();
|
|
|
+// System.out.println("Gen genExpArith:" + _op);
|
|
|
//
|
|
|
//
|
|
|
|
|
@@ -1374,13 +1402,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Instruction instruction = __exp(_op, _p1, _p2, _return);
|
|
|
|
|
|
if (instruction.eq("type", "copy")) {
|
|
|
- n.S("_p1", instruction.G("p1"));
|
|
|
- n.S("value", instruction.G("p1"));
|
|
|
- } else {
|
|
|
|
|
|
- n.S("_op", _op);
|
|
|
- n.S("_p1", _p1);
|
|
|
- n.S("_p2", _p2);
|
|
|
+ n.S("_p1", instruction.G("p1")).S("value", instruction.G("p1"));
|
|
|
+ } else {
|
|
|
+ n.S("_op", _op).S("_p1", _p1).S("_p2", _p2);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1654,6 +1679,10 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
protected void genAssign(Node n) throws Exception {
|
|
|
+
|
|
|
+// System.out.println("GENAssign :" + n);
|
|
|
+ ArrayList<Node> localExpr = new ArrayList<>();
|
|
|
+
|
|
|
ArrayList<String> localAttribs = new ArrayList<>(),
|
|
|
// copymode = new ArrayList<>(),
|
|
|
// operators = new ArrayList<>(),
|
|
@@ -1669,8 +1698,9 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
if (n.in("value", new String[]{"dec.var.short", "def.assign"})) {
|
|
|
|
|
|
for (Node attrib : n.childrens().get(1).childrens()) {
|
|
|
+// System.out.println(">n:" + attrib);
|
|
|
visit(attrib);
|
|
|
-// System.out.println("n:" + n);
|
|
|
+// System.out.println("n:" + attrib);
|
|
|
|
|
|
// System.out.println("genAssign:" + attrib.G("class") + ":" + attrib.G("value"));
|
|
|
if (attrib.eq("class", "call")) {
|
|
@@ -1703,12 +1733,14 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
copyaddress.add(attrib.eq("subclass", "address"));
|
|
|
localAttribs.add(attrib.G("_return"));
|
|
|
}
|
|
|
+ localExpr.add(attrib);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// System.out.println("Copy::" + localAttribs);
|
|
|
// System.out.println("Lista de atribuicoes locais:\n" + dsts + "\n>>\n" + n);
|
|
|
// System.out.println("Lista de atribuicoes locais:\n" + dsts + "\n>>\n" + localAttribs);
|
|
|
+ Node attrib;
|
|
|
// Executa a copia dos valores
|
|
|
for (int dstPosition = 0, i = 0; i < localAttribs.size(); dstPosition++) {
|
|
|
|
|
@@ -1716,13 +1748,12 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
d.S("declare", declare);
|
|
|
visit(d);
|
|
|
|
|
|
-// System.out.println("D:" + d.childrens());
|
|
|
+// System.out.println("D:" + d);
|
|
|
if (d.eq("class", "unary") && d.eq("dst.pointer", "*")) {
|
|
|
-
|
|
|
p1 = localAttribs.get(i);
|
|
|
- code.Add(new Instruction()
|
|
|
- .S("format", "pointer_assignment")
|
|
|
- .S("type", "pointer_assignment")
|
|
|
+ Add(new Instruction()
|
|
|
+ .S("format", "pointer_assign")
|
|
|
+ .S("type", "pointer_assign")
|
|
|
.S("dst", d.childrens().get(1).G("_return"))
|
|
|
.S("p1", p1)
|
|
|
.S("p1value", "" + Api.IsValue(p1))
|
|
@@ -1738,12 +1769,20 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
}
|
|
|
} else {
|
|
|
// Copia valor para endereco
|
|
|
-// System.out.println("Attrib:" + d.G("_return") + "<" + localAttribs.get(i));
|
|
|
- if (!modify.equals("")) {
|
|
|
+// System.out.println("Attrib[" + modify + ":" + (modify.equals("")) + "]:" + d.G("_return") + "<" + localAttribs.get(i));
|
|
|
|
|
|
+ if (!modify.equals("")) {
|
|
|
__exp(modify, d.G("_return"), localAttribs.get(i), d.G("_return")).S("comment", "");
|
|
|
+
|
|
|
} else {
|
|
|
+ attrib = localExpr.get(i);
|
|
|
+
|
|
|
+// if (attrib.eq("class", "expr")) {
|
|
|
+//// System.out.println("Attrib:" + attrib);
|
|
|
+// __exp(attrib.G("_op"), attrib.G("_p1"), attrib.G("_p2"), d.G("_return")).S("comment", "");
|
|
|
+// } else {
|
|
|
Copy(d.G("_return"), localAttribs.get(i), copyaddress.get(i));
|
|
|
+// }
|
|
|
}
|
|
|
i++;
|
|
|
}
|
|
@@ -1757,7 +1796,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("p1", param)
|
|
|
.S("p1value", "" + Api.IsValue(param))
|
|
|
.set("locker", getIdMaster());
|
|
|
- code.Add(r);
|
|
|
+ Add(r);
|
|
|
}
|
|
|
|
|
|
protected void genFunction(String id, Node func) throws Exception {
|
|
@@ -1766,6 +1805,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
|
|
|
Node block = func.find("stmts");
|
|
|
|
|
|
+// basicBlock.push(id);
|
|
|
+// basicBlockCount.push(0);
|
|
|
code.OpenBlock(id);
|
|
|
/*Cria a PushLabel inicial da funcao*/
|
|
|
PushLabel(id, "block");
|
|
@@ -1782,6 +1823,8 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
/*Cria a PushLabel final da funcao*/
|
|
|
PushLabel(id + "-end", "block");
|
|
|
code.CloseBlock();
|
|
|
+// basicBlock.pop();
|
|
|
+// basicBlockCount.pop();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1794,7 +1837,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
Node var = Variaveis.Get(n.G("ctx"));
|
|
|
// System.out.println("");
|
|
|
// __genParam("&" + gerarVariavel(var));
|
|
|
-// code.Add(new Instruction()
|
|
|
+// Add(new Instruction()
|
|
|
// .S("", PushLabel));
|
|
|
// gerarVariavel(var)
|
|
|
__genParam(gerarVariavel(var));
|
|
@@ -1871,7 +1914,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// System.out.println("Unary:" + n.childrens().G(1));
|
|
|
String type;
|
|
|
if (AddressOperator.matcher(op).matches()) {
|
|
|
- type = "pointer_assignment";
|
|
|
+ type = "pointer_assign";
|
|
|
} else {
|
|
|
type = "unary";
|
|
|
}
|
|
@@ -1885,7 +1928,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("copymode", "unary")
|
|
|
.S("p1value", "" + Api.IsValue(_val))
|
|
|
.set("locker", getIdMaster());
|
|
|
- code.Add(r);
|
|
|
+ Add(r);
|
|
|
// System.out.println("N:" + n);
|
|
|
// throw new Exception("Unary Stmt not defined");
|
|
|
}
|
|
@@ -1894,38 +1937,25 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// System.out.println("UnaryStmt:[" + _val + ":" + op + "]" + n);
|
|
|
}
|
|
|
|
|
|
- protected Integer _ExprResolve(String op, String p1, String p2) throws Exception {
|
|
|
- int p1v = Integer.parseInt(p1),
|
|
|
- p2v = Integer.parseInt(p2);
|
|
|
- switch (op.substring(0, 1)) {
|
|
|
- case "+":
|
|
|
- return p1v + p2v;
|
|
|
- case "-":
|
|
|
- return p1v - p2v;
|
|
|
- case "*":
|
|
|
- return p1v * p2v;
|
|
|
- case "/":
|
|
|
- return p1v / p2v;
|
|
|
- case "%":
|
|
|
- return p1v % p2v;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
protected Instruction __exp(String op, String p1, String p2, String ret) throws Exception {
|
|
|
+
|
|
|
boolean p1value = Api.IsValue(p1),
|
|
|
p2value = Api.IsValue(p2);
|
|
|
+
|
|
|
String unsigned = "false";
|
|
|
+
|
|
|
if (op.contains("u")) {
|
|
|
op = op.replace("u", "");
|
|
|
unsigned = "true";
|
|
|
}
|
|
|
+
|
|
|
Instruction r = new Instruction();
|
|
|
+
|
|
|
if (p1value && p2value) {
|
|
|
r.S("type", "copy")
|
|
|
.S("format", "copy")
|
|
|
.S("p1value", "true")
|
|
|
- .S("p1", "" + _ExprResolve(op, p1, p2));
|
|
|
+ .S("p1", "" + Utils.ResolveExpr(op, p1, p2));
|
|
|
|
|
|
} else {
|
|
|
r.S("type", "assign")
|
|
@@ -1936,14 +1966,14 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("copymode", "assign")
|
|
|
.S("p1value", "" + p1value)
|
|
|
.S("p2value", "" + p2value);
|
|
|
-// System.out.println("__EXP:"+p1value+":"+p2value);
|
|
|
+// System.out.println("__EXP:" + r);
|
|
|
}
|
|
|
r.S("dst", ret)
|
|
|
.S("op", op)
|
|
|
.S("op.unsigned", unsigned)
|
|
|
.set("locker", getIdMaster());
|
|
|
|
|
|
- return code.Add(r);
|
|
|
+ return Add(r);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -2060,7 +2090,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("p1value", "false")
|
|
|
.S("_return", varname);
|
|
|
r.set("locker", getIdMaster());
|
|
|
- code.Add(r);
|
|
|
+ Add(r);
|
|
|
return r;
|
|
|
}
|
|
|
|
|
@@ -2113,7 +2143,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("p1", src)
|
|
|
.S("p1value", Api.IsValue(src) + "")
|
|
|
.set("locker", getIdMaster());
|
|
|
- code.Add(r);
|
|
|
+ Add(r);
|
|
|
return r;
|
|
|
}
|
|
|
|
|
@@ -2147,7 +2177,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
.S("format", "label")
|
|
|
.set("locker", getIdMaster());
|
|
|
|
|
|
- return code.Add(r);
|
|
|
+ return Add(r);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -2217,7 +2247,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
varGenMap.put(varname, alias);
|
|
|
// vn = var.G("scope");
|
|
|
|
|
|
- int size = Tipos.Size(var.G("type")), elementCount = 1;
|
|
|
+ int size = Types.Size(var.G("type")), elementCount = 1;
|
|
|
var.set("size", size);
|
|
|
|
|
|
// System.out.println("gerarVariavelSetVAR:" + var);
|
|
@@ -2297,7 +2327,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
for (Node r : returns.childrens()) {
|
|
|
t = gerarVariavel("T");
|
|
|
rts.add(t);
|
|
|
- code.Add(new Instruction()
|
|
|
+ Add(new Instruction()
|
|
|
.S("type", "pop_return")
|
|
|
.S("format", "pop_return")
|
|
|
.S("p1value", "false")
|
|
@@ -2328,7 +2358,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// if (!variavelRetorno.equals("")) {
|
|
|
// r.S("dst", variavelRetorno);
|
|
|
// }
|
|
|
- code.Add(r);
|
|
|
+ Add(r);
|
|
|
return r;
|
|
|
}
|
|
|
|
|
@@ -2349,7 +2379,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
//
|
|
|
//// if (!loop && branckInvert.size() == 1) {
|
|
|
//// Instruction last = code.Last();
|
|
|
-//// last.S("op", Complement(last.G("op"))).S("label", n.G("next"));
|
|
|
+//// last.S("op", Utils.ComplementOperation(last.G("op"))).S("label", n.G("next"));
|
|
|
//// }
|
|
|
// if (!x.get(0).Has("and")) {
|
|
|
// n.remove("and");
|
|
@@ -2385,12 +2415,12 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// // }
|
|
|
|
|
|
// @Override
|
|
|
-// public TacGenInterface AfterCreate(CodeProcessing p) {
|
|
|
+// public IRGenInterface AfterCreate(CodeProcessing p) {
|
|
|
// this.afterCreate.add(p);
|
|
|
// return this;
|
|
|
// }
|
|
|
//
|
|
|
-// public TacGenInterface AfterCreate() throws Exception {
|
|
|
+// public IRGenInterface AfterCreate() throws Exception {
|
|
|
// for (CodeProcessing x : afterCreate) {
|
|
|
// x.Exec(code);
|
|
|
// }
|
|
@@ -2409,7 +2439,7 @@ public final class BaseTacGen implements TacGenInterface {
|
|
|
// .S("label", label);
|
|
|
//
|
|
|
//// System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
|
|
|
-// code.Add(r1);
|
|
|
+// Add(r1);
|
|
|
// return r1;
|
|
|
// }
|
|
|
// /**
|