123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223 |
- /*
- * 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.Utils;
- import API.Variaveis;
- 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;
- import java.util.regex.Pattern;
- // a0 a1
- // sync recebe mandando
- // exec recebe mandando
- // synexec recebe mandando
- /**
- *
- * @author Eugenio
- */
- public final class BaseTacGen implements TacGenInterface {
- 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;
- protected String calEndTipo;
- protected Stack<Node> master = new Stack<>();
- protected ArrayList<String> line = new ArrayList<>();
- protected LinkedList<Boolean> branckInvert = new LinkedList<>();
- protected HashMap<String, String> arrayAcesso = new HashMap<>();
- protected HashMap<String, String> varGenMap = new HashMap<>();
- protected HashMap<String, String> funcGenMap = new HashMap<>();
- 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>() {
- {
- put("!=", "==");
- put("==", "!=");
- put("<", ">=");
- put("<=", ">");
- put(">", "<=");
- put(">=", "<");
- }
- };
- public static Pattern AddressOperator = Pattern.compile("(\\&|\\*)");
- protected boolean enableOtimization = true;
- protected HashMap<String, String> ArrayShiftMap = new HashMap<>();
- public BaseTacGen() throws Exception {
- for (long i = 0; i < 31; i++) {
- power2.put(Long.valueOf(1 << i), Long.valueOf(i));
- }
- }
- protected int calc(ArrayList<Integer> data) {
- Integer op1, op2, operacao;
- switch (data.size()) {
- case 1:
- return data.get(0);
- case 3:
- op1 = data.remove(0);
- operacao = data.remove(0);
- op2 = data.remove(0);
- if (operacao == 1) {
- return op1 - op2;
- // throw new Exception("Quantidade de entradas invalidas!" + data.size());
- } else {
- return op1 + op2;
- }
- default:
- op1 = data.remove(0);
- operacao = data.remove(0);
- if (operacao == 1) {
- return op1 - calc(data);
- // throw new Exception("Quantidade de entradas invalidas!" + data.size());
- } else {
- return op1 + calc(data);
- }
- }
- }
- public BaseTacGen(AbstractSyntaxTree ast) {
- 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())
- .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]' >'}";
- 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("unary", base + " {[dst]} ' := '{[op]' '}{[p1]} {' --'[comment]}" + end);
- put("copy", base + " {[dst]} ' := '{[p1]} {' --'[comment]}" + end);
- put("jump", base + " 'goto ' {'<'[label]'>'} {' --'[comment]}" + end);
- put("branch", base + " 'if '{[p1]} {' '[op]' '} {[p2]} ' goto ' {'<'[label]'>'} {' --'[comment]}" + end);
- put("call", base + " {[dst]' := '} 'call <' {[funcname]} '>, ' {[nump]} {' --'[comment]}" + end);
- put("return", base + " 'return ' {[p1]} {' --'[comment]}" + end);
- put("push_param", base + " 'push_param '{[p1]} {' --'[comment]}" + end);
- put("push_return", base + " 'push_return '{[p1]} {' --'[comment]}" + end);
- put("pop_param", base + " 'pop_param '{[p1]} {' --'[comment]}" + end);
- put("pop_return", base + " 'pop_return '{[p1]} {' --'[comment]}" + end);
- }
- });
- }
- @Override
- public Code Create(AbstractSyntaxTree ast, HashMap<String, String> settings) throws Exception {
- Ast(ast);
- // 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;
- }
- protected Node visit(Node n) throws Exception {
- if (null == n || n.eq("visited", "true")) {
- return n;
- }
- n.S("visited", "true");
- // System.out.println("Visit:" + n.Class());
- switch (n.Class()) {
- case "dec.const":
- gerarConstante(n);
- break;
- case "if.stmt":
- IfStmts(n);
- break;
- case "selector":
- Selector(n);
- break;
- 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");
- genAssign(n);
- break;
- }
- case "def.assign":
- genAssign(n);
- break;
- case "call":
- genCall(n);
- break;
- case "ID":
- genID(n);
- break;
- case "incdec":
- IncDecStmt(n);
- break;
- case "for.stmt":
- genFor(n);
- break;
- case "expr":
- _visitExpr(n);
- break;
- case "unary":
- unaryStmt(n);
- break;
- case "switch.stmt":
- switchStmt(n);
- break;
- case "test":
- test(n);
- break;
- case "return":
- visitChildrensFirst(n);
- break;
- default:
- visitRootFirst(n);
- break;
- }
- return n;
- }
- protected void genID(Node n) throws Exception {
- // System.out.println("GenId:" + n);
- Node var = Variaveis.Get(n.getText());
- n.S("_return", gerarVariavel(var));
- }
- protected void test(Node n) throws Exception {
- Node x = null;
- ArrayList<Node> chils = n.childrens();
- int size = chils.size();
- for (int i = 0; i < size; i++) {
- x = chils.get(i);
- visit(x);
- }
- n.copy("_return", x);
- }
- protected void IncDecStmt(Node n) throws Exception {
- Instruction operacao = new Instruction();
- String dst = gerarVariavel(n.findByClass("selector"));
- operacao.S("type", "assign")
- .S("format", "assign")
- .S("cat", "exp")
- .S("_return", "exp")
- .S("dst", dst)
- .S("p1", dst)
- .S("p2", "1")
- .S("p1value", "" + Api.IsValue(dst))
- .S("p2value", "true")
- .S("op", n.findByClass("operator").eq("value", "++") ? "+" : "-");
- code.Add(operacao);
- }
- protected void Selector(Node n) throws Exception {
- String tmp;
- String shift = null;
- n.S("copymode", "copy");
- switch (n.G("subclass")) {
- case "operand":
- if (n.eq("classType", "value")) {
- // n.S("value", Api.Value(n.getText()));
- n.S("_return", Api.Value(n.getText()));
- } else {
- String v = gerarVariavel(n);
- if (n.eq("pointer", "*") && n.eq("access", "read")) {
- String t = gerarVariavel("T");
- Copy(t, v, false);
- v = t;
- }
- n.S("_return", v);
- }
- // System.out.println("Seletor operand:" + n);
- break;
- case "index":
- // shift = n.childrens().G(0).getInt("value");
- // shift = n.childrens().G(0).getInt("value");
- shift = ArrayShift(n);
- case "selector":
- if (shift == null) {
- shift = "" + Variaveis.Shift(n.getText());
- }
- tmp = gerarVariavel(n) + "[" + shift + "]";
- if (!n.eq("access", "write")) {
- String t = gerarVariavel("T");
- Copy(t, tmp, false);
- tmp = t;
- }
- n.S("_return", tmp);
- break;
- case "arguments":
- break;
- }
- }
- protected ArrayList<Integer> ShiftPath(int size) {
- ArrayList<Integer> ret = new ArrayList<>();
- FindShiftPath(size, ret);
- return ret;
- }
- protected ArrayList<Integer> ShiftInterval(int size) {
- ArrayList<Integer> ret = new ArrayList<>();
- Integer op = 1, i = 1, after, before;
- //
- while (true) {
- after = 1 << (i++);
- if (after == size) {
- before = after;
- break;
- } else if (after > size) {
- before = after >> 1;
- break;
- }
- }
- // before = Math.abs(before - size);
- // after = Math.abs(after - size);
- if (Math.abs(before - size) <= Math.abs(after - size)) {
- // Se o valor anterios for menor que o posterior
- op = 0;
- }
- // else {
- // // Se o valor posterior for menor que o anterior
- // }
- ret.add(before);
- ret.add(after);
- ret.add(2, op);
- // System.out.println("Interval:{" + size + "}" + ret + "::" + op);
- return ret;
- }
- protected LinkedHashMap<Long, Long> power2 = new LinkedHashMap<Long, Long>();
- protected void FindShiftPath(int size, ArrayList<Integer> path) {
- size = Math.abs(size);
- ArrayList<Integer> shift = ShiftInterval(size);
- int op = shift.get(2), s1 = shift.get(op);
- // System.out.println("FindShiftPath:(" + size + "|" + s1 + ")\t\t" + shift);
- if (power2.containsKey(s1)) {
- path.add(s1);
- path.add(op);
- }
- if (size == 1) {
- // path.add(size);
- }
- if (path.size() % 2 == 0 && Math.abs(size - s1) == 0) {
- path.remove(path.size() - 1);
- return;
- }
- FindShiftPath(size - s1, path);
- // int i = 1, after, before;
- // while (true) {
- // after = 1 << (i++);
- // if (after == size) {
- //
- // break;
- // } else if (after > size) {
- // break;
- // }
- // }
- // before = after >> 1;
- //
- // if (Math.abs(size - before) < Math.abs(after - size)) {
- // // Se o valor anterios for menor que o posterior
- // op = 1;
- // m4 = before;
- // } else {
- // // Se o valor posterior for menor que o anterior
- // op = 0;
- // m4 = after;
- // }
- // 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();
- for (Node node : n.childrens()) {
- key += "_" + node.getText();
- }
- if (!ArrayShiftMap.containsKey(key)) {
- // String shift;
- Node var = Variaveis.Get(n.getText());
- // System.out.println("INDEX shift:" + n.childrens());
- for (int i = 0, j = 1; j < var.childrens().size(); j++) {
- n.childrens().get(i++).S("collNumber", var.childrens().get(j).getText());
- }
- 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;
- // } else {
- //// Se for um endereci faz a soma com o offset da varivel
- //// __exp("+", ret, gerarVariavel(n), ret);
- // shift = ret;
- // }
- ArrayShiftMap.put(key, ret);
- }
- return ArrayShiftMap.get(key);
- }
- protected String GenShiftLeftLogical(String var, int size, boolean mps) 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);
- var = GSLL(var, reverse, mps, size);
- return var;
- }
- protected String GSLL(String var, ArrayList<Integer> path, boolean mps, int size) throws Exception {
- int op1, operacao, op2;
- String T1 = gerarVariavel("T"),
- T2 = gerarVariavel("T"),
- T3 = gerarVariavel("T");
- switch (path.size()) {
- case 1:
- if (!mps) {
- Copy(T1, var, false)
- .S("comment", "Copy value of index")
- .S("copy", "true");
- } else {
- T1 = var;
- }
- if (mps) {
- __exp("<<", T1, "" + power2.get(size), T2).S("comment", "");
- return T2;
- }
- return T1;
- case 3:
- op1 = path.remove(0);
- operacao = path.remove(0);
- op2 = path.remove(0);
- // String T4 = gerarVariavel("T");
- if (!mps) {
- Copy(T1, var, false)
- .S("comment", "Copy value of index")
- .S("copy", "true");
- }
- if (op1 != 1) {
- __exp("<<", T1, "" + power2.get(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", "");
- // deve realizar um novo deslocamento
- } else {
- T2 = T1;
- }
- // Soma o deslocamento com o tamanho
- __exp("+", T1, T2, T1).S("comment", "");
- return T1;
- default:
- // op1 = path.remove(0);
- // operacao = path.remove(0);
- //
- // T2 = GSLL(var, path);
- //
- // Copy(T1, var, false).S("comment", "Copy value of index");
- //
- // __exp("<<", T1, "" + power2.get(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);
- boolean onlyNumber = true;
- for (Node node : childrens) {
- if (!node.isNumber("value")) {
- onlyNumber = false;
- break;
- }
- }
- if (onlyNumber) {
- return "" + (CalcArrayAddressNumber(childrens, Size) * Size);
- } else {
- // System.out.println("CAA:" + childrens);
- String current = CalcArrayAddressAux(childrens, Size);
- System.out.println("Size {" + current + "}:----------->" + Size);
- // multiplica pelo tamanho
- return GenShiftLeftLogical(current, Size, true);
- }
- }
- protected Integer CalcArrayAddressNumber(ArrayList<Node> childrens, int Size) throws Exception {
- Node ind;
- ind = childrens.remove(0);
- Integer value = ind.getInt("value");
- // ultimo elemento da idexacao
- if (childrens.size() == 0) {
- return value;
- }
- Integer collNumber = ind.getInt("collNumber"),
- ret = CalcArrayAddressNumber(childrens, Size);
- return ((value * collNumber) + ret);
- }
- 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);
- // ultimo elemento da idexacao
- if (childrens.size() == 0) {
- if (Utils.isNumber(ind.getText())) {
- return "" + ind.getInt("value");
- } else {
- return GenShiftLeftLogical(gerarVariavel(ind), Size, false);
- }
- }
- int collNumber = ind.getInt("collNumber");
- nextIndex = childrens.get(0);
- if (ind.isNumber("value") && nextIndex.isNumber("value")) {
- // System.out.println("CalcArray:" + ind.getInt("value") + ":" + ind.getInt("collNumber") + ":" + Integer.parseInt(ret));
- current = "" + (ind.getInt("value") * collNumber);
- // multPerSize = false;
- } else {
- // System.out.println("collNumber:" + collNumber);
- current = GenShiftLeftLogical(gerarVariavel(ind), collNumber, false);
- // System.out.println("Gerando index calc:" + retl);
- // current = GenShiftLeftLogical(retl, Size);
- // current = retl;
- }
- String ret = CalcArrayAddressAux(childrens, Size);
- __exp("+u", current, ret, current).S("comment", "colls shift");
- // if (multPerSize) {
- // // Realiza a multiplicacao pelo tamanho do elemento
- // current = GenShiftLeftLogical(current, Size);
- // }
- return current;
- }
- // protected String CalcArrayAddress(ArrayList<Node> childrens, int Size, int index) throws Exception {
- // System.out.println("CalcArrayAddress:" + childrens.size() + ":" + Size);
- //
- // int nindex = index + 1;
- // String retl;
- // Node ind = childrens.remove(0);
- // if (childrens.size() >= 1) {
- // String ret = CalcArrayAddress(childrens, Size, nindex);
- // int collNumber = ind.getInt("collNumber");
- //
- // if (Utils.isNumber(ret) && ind.isNumber("value")) {
- //// System.out.println("CalcArray:" + ind.getInt("value") + ":" + ind.getInt("collNumber") + ":" + Integer.parseInt(ret));
- // return "" + ((ind.getInt("value") * collNumber) + Integer.parseInt(ret));
- // } else {
- //// System.out.println("collNumber:" + collNumber);
- //
- // retl = GenShiftLeftLogical(gerarVariavel(ind), collNumber);
- //
- // retl = GenShiftLeftLogical(retl, Size);
- //
- // __exp("+", retl, ret, retl).S("comment", "colls shift");
- // return retl;
- // }
- //
- // /*
- // Implementacao com mult **
- // int collNumber = ind.getInt("collNumber");
- //
- // if (Utils.isNumber(ret) && ind.isNumber("value")) {
- // System.out.println("CalcArray:" + ind.getInt("value") + ":" + collNumber + ":" + Integer.parseInt(ret));
- // return "" + ((ind.getInt("value") * collNumber) + Integer.parseInt(ret));
- // } else {
- // String retl = gerarVariavel("T");
- // __exp("*", gerarVariavel(ind), "" + collNumber, retl)
- // .S("comment", "lines shift");
- // __exp("+", retl, ret, retl)
- // .S("comment", "colls shift");
- // return retl;
- // }
- // */
- // } else {
- //
- // if (Utils.isNumber(ind.getText())) {
- // return ind.getText();
- // } else {
- // // ultimo elemento da idexacao
- //
- // return GenShiftLeftLogical(gerarVariavel(ind), Size);
- // }
- //
- // }
- // }
- /**
- * ProceSSa um node:<br>
- * Ordem:<br>
- * ->Primeiro filhoS;<br>
- * ->No principal;<br>
- *
- * @param n
- * @throws Exception
- */
- protected void visitChildrensFirst(Node n) throws Exception {
- // System.out.println("Visit:" + n.getText());
- for (Node n1 : n.childrens()) {
- // System.out.println("Visit:" + n1.getText());
- visit(n1);
- }
- // System.out.println("Process:" + n.getText());
- processarNode(n);
- }
- /**
- * ProceSSa um node:<br>
- * Ordem:<br>
- * ->Primeiro filho;<br>
- * ->No principal;<br>
- * ->Segundo filho;<br>
- *
- * @param n
- * @throws Exception
- */
- protected void visitNoMeio(Node n) throws Exception {
- visit(n.getFilho(0));
- processarNode(n);
- visit(n.getFilho(1));
- }
- /**
- * ProceSSa um node:<br>
- * Ordem:<br>
- * ->No princial;<br>
- * ->DepoiS oS filhoS
- *
- * @param n
- * @throws Exception
- */
- protected void visitRootFirst(Node n) throws Exception {
- processarNode(n);
- for (Node n1 : n.childrens()) {
- visit(n1);
- }
- }
- /**
- * Aloca endereço no inicio da memoria para conStanteS ApenaS tipoS<br>
- * [inteiro, booleano e char]
- *
- * @param n
- * @throws Exception
- */
- protected void gerarConstante(Node n) throws Exception {
- gerarVariavel(n);
- // getLabel("C", n);
- // if (n.closest("block", "class") != null) {
- // code.registerVar(n.G("_return"), 1, DataFrame.TYPE_VAR);
- // }
- }
- /**
- * Executa a extração fo block principal
- *
- * @throws Exception
- */
- public void main() throws Exception {
- Node main = ast.getMain();
- // main.S("_return", Threads.getId("main"));
- Node stmts = main.find("stmts");
- if (code.OpenBlock("main")) {
- PushLabel("main", "block");
- visit(stmts);
- PushLabel("main-end", "block");
- code.CloseBlock();
- }
- }
- public Code run() throws Exception {
- if (!inicializado) {
- inicializado = true;
- main();
- }
- code.UpdatePositions();
- return code;
- }
- /**
- * ProceSSa If (ElSe'S)*
- *
- * @param n
- * @throws Exception
- */
- protected void IfStmts(Node n) throws Exception {
- String end = gerarLabel(), next;
- boolean ifChain = n.childrens().size() > 1;
- if (ifChain) { //(If)+ (else)?
- next = gerarLabel();
- } else {
- next = end;
- }
- n.S("next", next).S("end", end);
- for (Node stmt : n.childrens()) {
- switch (stmt.Class()) {
- case "if.case.stmt":
- IfCaseStmt(n, stmt, ifChain);
- break;
- // Else case
- case "stmts":
- // String next = gerarLabel();
- // n.S("next", next);
- visit(stmt.S("next", next));
- // Cria um PushLabel final
- // PushLabel(n.G("end"), "branch");
- // PushLabel(end);
- break;
- }
- }
- // Cria um PushLabel final
- // System.out.println("Label end:" + n.G("end"));
- PushLabel(n.G("end"), "branch");
- }
- /**
- * ProceSSa If, eSSe metodo deve Ser chamado pelo metodo IfStmtS
- *
- * @param n
- * @param _if
- * @param ifChain
- * @throws Exception
- */
- protected void IfCaseStmt(Node n, Node _if, boolean ifChain) throws Exception {
- Node test = _if.first().first();
- Node stmts = _if.find("stmts");
- n.S("start", gerarLabel());
- test.copy("start,next,end", n);
- Branch(test, 0);
- // marca com um label o inicio do bloco do if
- PushLabel(n.G("start"), "branch");
- // gera as instruções do bloco
- visit(stmts);
- // se for uma cadeia de condiçoes com ou sem else
- if (ifChain) {
- // Salta os outros testes
- _goto(n.G("end"));
- //Adiciona o label de proximo elemento da cadeia de if's e else
- PushLabel(n.G("next"), "branch");
- //Atualiza o ponteiro para o proximo elemento
- n.S("next", gerarLabel());
- }
- }
- /**
- * Executa tratamento de um no eSpecifico, Selecionado pelo atributo claSS
- *
- * @param n
- * @throws Exception
- */
- protected void processarNode(Node n) throws Exception {
- switch (n.Class()) {
- case "return":
- genReturn(n);
- break;
- case "ctrl.stmt":
- ctrlStmt(n);
- break;
- case "label.stmt":
- PushLabel(n.getText(), "user");
- break;
- }
- }
- protected void _visitExpr(Node n) throws Exception {
- // System.out.println("VisitExpr:" + n);
- visit(n.childrens().get(0));
- visit(n.childrens().get(1));
- switch (n.G("subclass")) {
- case "arith":// ( + | - | * | / ) integer
- genExpArith(n);
- break;
- }
- }
- 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";
- n.S("skip.test", step);
- op1.copy(attrs, n);
- op2.copy(attrs, n);
- instruction = Branch(op1, 1);
- if (instruction != null) {
- switch (type) {
- case "jtb":
- instruction.S("op", Complement(instruction.G("op")));
- 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", "&&")) {
- } else {
- instruction.S("label", n.G("end"));
- }
- }
- }
- // System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
- // 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 {
- // 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";
- n.S("skip.test", step);
- op1.copy(attrs, n);
- op2.copy(attrs, n);
- System.out.println("BranchOr:" + n.parent);
- // Executa o lado esquerdo do ||
- instruction = Branch(op1, 1);
- if (instruction != null) {
- switch (type) {
- case "jtb":
- if (n.parent.eq("value", "&&")) {
- if (index == 1) {
- instruction.S("label", n.parent.G("skip.test"));
- }
- }
- }
- }
- // 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"));
- }
- }
- }
- }
- }
- protected Instruction BranchTest(Node n, int index) throws Exception {
- ArrayList<Node> x = n.childrens();
- 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);
- // Resultado do test class ou acesso a um id ou bool
- if (!n.eq("class", "expr")) {
- op = "==";
- p0 = n.G("_return");
- p1 = "1";
- } else { //(<, <=, >, >=, ==, !=)
- op = n.getText();
- p0 = visit(x.get(0)).G("_return");
- p1 = visit(x.get(1)).G("_return");
- }
- // Inverte o teste e salta o bloco
- 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 {
- 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;
- }
- 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;
- case "||":
- BranchOr(n, index);
- break;
- 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.
- *
- * @param label
- * @throws Exception
- */
- protected void _goto(String label) throws Exception {
- Instruction r = new Instruction();
- r.S("label", label)
- .S("type", "jump")
- .S("format", "jump")
- .set("locker", getIdMaster());
- code.Add(r);
- }
- /*Inicio das avaliacoes booleanas*/
- /**
- * Gera um linha de inStrução de retorno no block de codigo
- *
- * @param n
- * @throws Exception
- */
- protected void genReturn(Node n) throws Exception {
- Instruction nreg;
- ArrayList<Node> returns = n.find("exprs").childrens();
- for (Node ret : returns) {
- // System.out.println("Return p:" + ret);
- nreg = new Instruction()
- .S("type", "push_return")
- .S("format", "push_return")
- .S("p1value", "" + Api.IsValue(ret.getText()))
- .S("p1", ret.G("_return"));
- nreg.set("locker", getIdMaster());
- code.Add(nreg);
- }
- code.Add(new Instruction()
- .S("type", "return")
- .S("format", "return")
- .set("p1", returns.size())
- .S("p1value", "true")
- .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
- *
- * @param n
- * @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(),
- end = gerarLabel(),
- inc = gerarLabel(),
- testLabel = gerarLabel();
- n.S("start", start)
- .S("end", end)
- .S("inc", inc)
- .S("test", testLabel);
- Node stmt = n.find("stmts");
- stmt.copy(attrs, n);
- test.copy(attrs, n).S("test.type", "jtb");
- /*Inicializacoes e atribuicoes*/
- visit(n.find("initialization"));
- /*Salta para o teste do laco*/
- _goto(testLabel);
- /*Marca o inicio do bloco do laco*/
- PushLabel(start, "branch");
- /*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.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");
- PushLabel(end, "branch");
- // }
- setNullMaster();
- }
- // protected void genFor(Node n) throws Exception {
- // setMaster(n);
- // String start = gerarLabel(), next = gerarLabel();
- //
- // n.S("next", next);
- // n.S("start", start);
- //
- // Node stmt = n.find("stmts");
- // stmt.S("next", start);
- // /*Inicializacoes e atribuicoes*/
- // visit(n.find("initialization"));
- // // Label que marca o inicio do for
- // PushLabel(start, "branch");
- //
- // Node test = n.find("test").first();
- //// if (test.hasChildrens())
- //// test.S("true", gerarLabel());
- //// test.S("false", n.G("next"));
- //// test.S("true", next);
- //// test.S("false", n.G("next"));
- // test.S("and", next);
- // test.S("next", next);
- // test.S("or", gerarLabel());
- //
- // Branch(test);
- //
- // if (test.Has("or")) {
- // PushLabel(test.G("or"), "branch");
- // }
- //
- //// PushLabel(test.G("true"), "branch");
- // /*Executa o bloco do for*/
- // visit(stmt);
- // /*Executa os incrementos*/
- // visit(n.find("increment"));
- // /*Salta para inicio do laço*/
- // _goto(start);
- // /*Marca o endereço final para sair do laço*/
- // PushLabel(test.G("next"), "branch");
- //
- // setNullMaster();
- // }
- protected void genExpArith(Node n) throws Exception {
- Node op1 = n.childrens.get(0);
- Node op2 = n.childrens.get(1);
- String _op = n.getText();
- //
- //
- // // se os dois valores são
- // if (op1.eq("type", "value") && op2.eq("type", "value")) {
- //
- // n.S("_return", resolveExpr(_op, op1.G("value"), op2.G("value")));
- // }
- // if (op1.isNumber("value") && op2.isNumber("value")) {
- //
- // } else {
- // Inverte a ordem dos operandos se o segundo for seletor e o primeiro for valor
- if (op1.isNumber("value") && !op2.isNumber("value")) {
- Node aux = op1;
- op1 = op2;
- op2 = aux;
- }
- // System.out.println("GerarArit" + op1 + "-" + op2);
- String _p1 = op1.G("_return");
- String _p2 = op2.G("_return");
- String _return = gerarVariavel("T");
- n.S("_return", _return);
- 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);
- }
- }
- /**
- * Para cima verificado <==>
- */
- protected void genArray(Node n) {
- if (n.eq("raiz", "t")) {
- currentMatrixvalues = new ArrayList<String>();
- matrixValues.put(n.G("id"), currentMatrixvalues);
- for (Node c : n.childrens()) {
- genArray(c);
- }
- } else if (n.eq("terminal", "t")) {
- currentMatrixvalues.add(n.getText());
- } else {
- for (Node c : n.childrens()) {
- genArray(c);
- }
- }
- }
- protected ArrayList<String> initValues(Node n) {
- ArrayList<String> values = new ArrayList<>();
- switch (n.G("class")) {
- case "_array":
- values = matrixValues.get(n.G("id"));
- break;
- /*valores e constantes*/
- default:
- String _return = n.G("_return");
- if (_return.matches("^_.*")) {
- _return = "0";
- }
- values.add(_return);
- }
- return values;
- }
- /**
- * Gera a chamada para o metodo de deSalocacao de memoria
- *
- * @param n
- * @throws Exception
- */
- protected void genDelete(Node n) throws Exception {
- for (Node address : n.childrens()) {
- visit(address);
- __genParam(address.G("_return"));
- __gerarCall(Functions.FREE, "1", getIdMaster());
- }
- }
- /**
- * Gera a Switch caSe
- *
- * @param n
- */
- 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");
- n.S("next", end);
- // Verifica se existe entrada para selecao
- if (test.childrens().size() > 0) {
- test = test.first();
- visit(test);
- dst = test.G("_return");
- }
- 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++;
- }
- /*Marca o endereço final para sair do switch*/
- PushLabel(end, "branch");
- }
- 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;
- }
- /**
- * Gera oS caSeS de um Switch
- *
- * @param n
- */
- protected void genCase(Node n, boolean last) throws Exception {
- 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 OR(ArrayList<Node> n) throws Exception {
- switch (n.size()) {
- case 0:
- return null;
- case 1:
- return n.get(0);
- default:
- Node x = n.get(0);
- n.remove(x);
- return NodeBoolExpr("||", x, OR(n)).copy("true,false", x);
- }
- }
- protected int qtdByIndex(Node n) throws Exception {
- Node indexes = n.find("indexes");
- if (indexes == null) {
- return 1;
- } else {
- int sum = 0;
- for (Node index : indexes.childrens()) {
- sum += index.getInt("_return");
- }
- return sum;
- }
- }
- /**
- * Gera inStrução de atribuicao
- *
- * @param n
- * @throws Exception
- */
- protected void genAssign(Node n) throws Exception {
- ArrayList<String> localAttribs = new ArrayList<>(),
- // copymode = new ArrayList<>(),
- // operators = new ArrayList<>(),
- returns;
- ArrayList<Boolean> copyaddress = new ArrayList<>();
- String p1, declare = "" + n.getText().equals("dec.var.short");
- Node dsts = n.childrens().get(0);
- if (n.in("value", new String[]{"dec.var.short", "def.assign"})) {
- for (Node attrib : n.childrens().get(1).childrens()) {
- visit(attrib);
- // System.out.println("n:" + n);
- // System.out.println("genAssign:" + attrib.G("class") + ":" + attrib.G("value"));
- if (attrib.eq("class", "call")) {
- returns = attrib.getList("returns");
- if (returns != null) {
- for (String _return : returns) {
- localAttribs.add(_return);
- copyaddress.add(attrib.eq("subclass", "address"));
- }
- }
- } else if (attrib.eq("class", "index")) {
- // System.out.println("Attr <- :" + attrib);
- } else if (attrib.eq("class", "literal")) {
- // System.out.println("literal<- :" + attrib);
- switch (attrib.G("subclass")) {
- case "array":
- for (Node element : attrib.find("exprs").childrens()) {
- visit(element);
- copyaddress.add(element.eq("subclass", "address"));
- localAttribs.add(element.G("_return"));
- }
- break;
- }
- } else {
- // System.out.println("Assign:" + attrib);
- copyaddress.add(attrib.eq("subclass", "address"));
- localAttribs.add(attrib.G("_return"));
- }
- }
- }
- // 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);
- // Executa a copia dos valores
- for (int dstPosition = 0, i = 0; i < localAttribs.size(); dstPosition++) {
- Node d = dsts.childrens().get(dstPosition);
- d.S("declare", declare);
- visit(d);
- // System.out.println("D:" + d.childrens());
- 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")
- .S("dst", d.childrens().get(1).G("_return"))
- .S("p1", p1)
- .S("p1value", "" + Api.IsValue(p1))
- .S("dst.pointer", "*")
- .set("locker", getIdMaster()));
- i++;
- // Salva o valor em memoria
- } else if (d.eq("array", "true")) {
- for (int j = 0; j < d.getInt("array_size"); j++, i++) {
- // System.out.println("ARRAY:" + d.G("_return") + "[" + j + "]" + " -- " + localAttribs.get(i));
- Copy(d.G("_return") + "[" + j + "]", localAttribs.get(i), copyaddress.get(i));
- }
- } 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));
- i++;
- }
- }
- }
- protected void __genParam(String param) throws Exception {
- Instruction r = new Instruction();
- r.S("format", "push_param")
- .S("type", "push_param")
- .S("p1", param)
- .S("p1value", "" + Api.IsValue(param))
- .set("locker", getIdMaster());
- code.Add(r);
- }
- protected void genFunction(String id, Node func) throws Exception {
- if (!funcGenMap.containsKey(id)) {
- funcGenMap.put(id, "true");
- Node block = func.find("stmts");
- code.OpenBlock(id);
- /*Cria a PushLabel inicial da funcao*/
- PushLabel(id, "block");
- /*Declara as variaveis de parametro*/
- genPopParams(func);
- /*Declara as variaveis de retorno caso sejam nomeadas*/
- // visit(func.find("def::return"));
- // System.out.println(Variaveis.printScope(func.G("scope")));
- /*Processa o bloco de instrucoes*/
- visit(block);
- /*Cria a PushLabel final da funcao*/
- PushLabel(id + "-end", "block");
- code.CloseBlock();
- }
- }
- protected void genCall(Node n) throws Exception {
- int argsNum = 0;
- // Se for metodo carrega o contexto no primeiro parametro
- // System.out.println("GenCall:" + n);
- if (!n.eq("ctx", "")) {
- Node var = Variaveis.Get(n.G("ctx"));
- // System.out.println("");
- // __genParam("&" + gerarVariavel(var));
- // code.Add(new Instruction()
- // .S("", PushLabel));
- // gerarVariavel(var)
- __genParam(gerarVariavel(var));
- argsNum++;
- }
- // Gera os argumentos
- Node args = n.find("arguments").find("exprs");
- if (args != null) {
- for (Node param : args.childrens()) {
- // Processa o argumento para obter o _return
- visit(param);
- // Cria a instrução de parametro
- __genParam(param.G("_return"));
- }
- argsNum += args.getChildrenCount();
- }
- // System.out.println("GenCall:" + n.getText());
- Node def = Functions.Get(n.getText());
- genFunction(n.getText(), def);
- // Cria a instrução de chamada
- Instruction r = __gerarCall(n.getText(), "" + argsNum, getIdMaster(), def.find("dec.return"));
- // System.out.println("CALL -> " + r.getList("returns"));
- n.addList("returns", r.getList("returns"));
- }
- protected String UnaryResolve(String op, String value) throws Exception {
- switch (op) {
- case "-":
- return "-" + value;
- case "!":
- return "" + (value.equalsIgnoreCase("false") || value.equals("0") ? "1" : "0");
- }
- throw new Exception(String.format("Invalid [%s]%s ", op, value));
- }
- /*Fim dos laços*/
- /*Inicio das operacoes aritimeticas*/
- /**
- * Unary pode Ser uma expreSSão do tipo <br>
- * (&|*)addreSS - operacao de ponteiroS <br>
- * (-) addreSS|value - negação aritimetica <br>
- * (!) addreSS|value - negação booleana
- *
- * @param n
- * @throws Exception
- */
- protected void unaryStmt(Node n) throws Exception {
- String op = n.childrens().get(0).getText().trim();
- Node value = n.childrens().get(1);
- visit(value);
- String _ret,
- _val = value.G("_return");
- // Se for escrita verifica se é escrita no ponteir ou no lugar referenciado
- if (Api.IsValue(_val)) {
- _ret = UnaryResolve(op, _val);
- // } else {
- } else if (n.eq("access", "write")) {
- _ret = "ERR";
- n.S("dst.pointer", op);
- } else {
- // System.out.println("N:[" + n.G("id") + "]");
- _ret = gerarVariavel("T");
- // System.out.println("Unary:" + n.childrens().G(1));
- String type;
- if (AddressOperator.matcher(op).matches()) {
- type = "pointer_assignment";
- } else {
- type = "unary";
- }
- Instruction r = new Instruction()
- .S("type", type)
- .S("format", type)
- .S("dst", _ret)
- .S("p1", _val)
- .S("op", op)
- .S("copymode", "unary")
- .S("p1value", "" + Api.IsValue(_val))
- .set("locker", getIdMaster());
- code.Add(r);
- // System.out.println("N:" + n);
- // throw new Exception("Unary Stmt not defined");
- }
- n.S("_return", _ret);
- // 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));
- } else {
- r.S("type", "assign")
- .S("format", "assign")
- .S("cat", "exp")
- .S("p1", p1)
- .S("p2", p2)
- .S("copymode", "assign")
- .S("p1value", "" + p1value)
- .S("p2value", "" + p2value);
- // System.out.println("__EXP:"+p1value+":"+p2value);
- }
- r.S("dst", ret)
- .S("op", op)
- .S("op.unsigned", unsigned)
- .set("locker", getIdMaster());
- return code.Add(r);
- }
- /**
- * Gera Salto para inStruçõeS de continue e break;
- *
- * @param n
- * @throws Exception
- */
- protected void ctrlStmt(Node n) throws Exception {
- Node container = 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);
- break;
- case "break":
- _goto(container.G("end"));
- break;
- case "goto":
- _goto(n.G("label"));
- break;
- }
- }
- /**
- * Executa a extração do codigo baSeado na aSt, e retorna um objeto
- * CodigoTreSEnderecoS contendo oS blockS em Formato de treS endereçoS.
- *
- * @return
- * @throws Exception
- */
- public Code getCodigoDeTresEnderecos() throws Exception {
- return run();
- }
- /**
- * Ativa ou deSativa a otimizacao
- *
- * @param lc
- */
- public void setOtimizacao(boolean lc) {
- code.setOtimizacao(lc);
- }
- protected void setMaster(Node n) {
- master.push(n);
- }
- protected Node getMaster() {
- return master.peek();
- }
- protected void setNullMaster() {
- master.pop();
- }
- protected int getIdMaster() throws Exception {
- if (master.empty()) {
- return 0;
- }
- return master.peek().getInt("id");
- }
- /**
- * Adiciona aS declaraçõeS bem como carregaoS parametroS e referenciaS
- *
- * @param n
- * @throws Exception
- */
- protected void genPopParams(Node func) throws Exception {
- int reg = 0;
- String destino, atribuido;
- /**
- * Se for method declara e atribui o endereco a variavel
- */
- Node receive = func.find("receive.type");
- if (receive != null) {
- // atribuido = "_A" + (reg++);
- // destino = clearPointer(atribuido, gerarVariavel(receive.childrens().G(0)));
- destino = gerarVariavel(receive.childrens().get(0));
- // copy_pointer?
- Copy(Api.clearID(destino), __popParam().G("_return"), false);
- }
- for (Node argument : func.find("arguments").childrens()) {
- for (Node arg : argument.find("ids").childrens()) {
- /**
- * Gera uma instrução de copia do registrador a{i},i = [0-4],
- * para o endereço de memoria dos parametros
- */
- // atribuido = "_Ra" + (reg++);
- // atribuido = "_A" + (reg++);
- // System.out.println("PopArg:" + arg);
- // destino = clearPointer(atribuido, gerarVariavel(arg));
- // copy_pointer?
- Copy(Api.clearID(gerarVariavel(arg)), __popParam().G("_return"), false);
- }
- }
- }
- protected Instruction __popParam() throws Exception {
- String varname = gerarVariavel("T");
- Instruction r = new Instruction()
- .S("type", "pop_param")
- .S("format", "pop_param")
- .S("p1", varname)
- .S("p1value", "false")
- .S("_return", varname);
- r.set("locker", getIdMaster());
- code.Add(r);
- return r;
- }
- // protected void inicializarVariaveis() throws Exception {
- //// System.out.println("INICIALIZAR VARIAVEIS:");
- //
- // for (Node container : ast.getRoot().childrens()) {
- // for (Node constant : container.findAll("dec::const,dec::var", "class", 1)) {
- // visit(constant);
- // }
- // }
- // }
- protected Instruction Copy(String dst, String src, boolean address) throws Exception {
- // System.out.println("TAC:Copy:" + dst + "=" + src);
- String type, format, arrayPattern = ".*\\[.*\\]";
- Instruction r = new Instruction();
- if (dst.matches(arrayPattern) || src.matches(arrayPattern)) {
- type = "indexed_assign";
- format = "copy";
- if (Variaveis.isArray(src)) {
- r.S("src_indexed", "true");
- r.S("p1.indice", Api.getIndice(src));
- }
- if (Variaveis.isArray(dst)) {
- r.S("dst_indexed", "true");
- r.S("dst.indice", Api.getIndice(dst));
- } else {
- r.S("dst_indexed", "false");
- }
- if (Variaveis.isArray(src)) {
- r.S("indice", Api.getIndice(src));
- }
- } else {
- type = format = "copy";
- }
- // Identifica que o valor copiado é um endereco ou valor
- if (address) {
- r.S("src.address", "true");
- }
- r.S("format", format)
- .S("type", type)
- .S("dst", dst)
- .S("p1", src)
- .S("p1value", Api.IsValue(src) + "")
- .set("locker", getIdMaster());
- code.Add(r);
- return r;
- }
- /**
- * Gera um PuShLabel unico
- *
- * @param n
- * @return
- */
- protected String gerarLabel(Node n) {
- // return code.genLabel();
- return code.getCurrentBlockName() + "+_i" + (incLabel++);
- }
- protected String gerarLabel() {
- // return code.genLabel();
- return code.getCurrentBlockName() + "+_i" + (incLabel++);
- }
- /**
- * Cria uma linha de PuShLabel no block de codigo corrente
- *
- * @param label String
- * @throws Exception
- */
- protected Instruction PushLabel(String label, String type) throws Exception {
- Instruction r = new Instruction()
- .S("label", label)
- .S("type", "label")
- .S("label_type", type)
- .S("format", "label")
- .set("locker", getIdMaster());
- return code.Add(r);
- }
- /**
- * Gera um nome de variavel unico,
- *
- * @param pre prefixo da variavel
- * @param n
- * @return
- */
- protected String gerarVariavel(String pre) {
- return "_" + pre + (tmpVarCount++);
- }
- protected String gerarVariavel(Node n) throws Exception {
- String id = n.getText();
- if (!varGenMap.containsKey(id)) {
- varGenMap.put(id, gerarVariavelSet(id, n));
- }
- return varGenMap.get(id);
- }
- protected String gerarVariavelSet(String id, Node n) throws Exception {
- Node var = Variaveis.Get(id);
- String varname, alias, vn;
- if (var.eq("constant", "true")) {
- varname = id;
- } else {
- varname = var.getText();
- }
- // 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);
- // }
- if (varGenMap.containsKey(varname)) {
- alias = varGenMap.get(varname);
- // System.out.printf("GerarVariavelSet[MAPPED]{%s,%s}\n", varname, alias);
- } else {
- String prefix;
- // System.out.println("GerarVariavelSet\n" + varname + "\n" + id + "\n" + var);
- if (var.eq("class", "dec.const")) {
- prefix = "C";
- } else if (var.eq("global", "true")) {
- prefix = "G";
- } else {
- prefix = "V";
- }
- // System.out.println("DecVar:" + var);
- // alias = var.G("pointer") + gerarVariavel(prefix);
- alias = gerarVariavel(prefix);
- n.S("alias", alias);
- n.copy("pointer", var);
- varGenMap.put(varname, alias);
- // vn = var.G("scope");
- int size = Tipos.Size(var.G("type")), b = 1;
- // Se for um array considera os filhos como index
- if (!var.eq("constant", "true") && var.childrens().size() > 0) {
- for (Node node : var.childrens()) {
- b *= node.getInt("value");
- }
- }
- size *= b;
- var.set("size", size);
- // System.out.println("gerarVariave[" + prefix + "|" + id + "]:" + var);
- (prefix.equals("G") ? code.GData() : code.Block().Data()).Add(alias, var);
- }
- return alias;
- }
- /**
- * Imprime o codigo de treS endereçoS
- *
- * @return
- */
- @Override
- public String toString() {
- return printVarMap() + code.toString();
- }
- public String printVarMap() {
- StringBuilder s = new StringBuilder();
- for (Map.Entry<String, String> e : varGenMap.entrySet()) {
- s.append(e.getKey() + " : " + e.getValue() + "\n");
- }
- System.out.println("Var Map:\n" + s.toString());
- return varGenMap.toString();
- }
- /**
- * Executa a extração da aSt para codigo de treS endereçoS
- *
- * @throws Exception
- */
- public void extrair() throws Exception {
- if (!inicializado) {
- inicializado = true;
- }
- // inicializarVariaveis();
- // extrairTraps();
- // extrairThreads();
- // extractFunctions();
- // extracMethods();
- main();
- }
- protected Instruction __gerarCall(String funcname, String numparam, int idMaster, Node returns) throws Exception {
- Instruction call = __gerarCall(funcname, numparam, idMaster);
- // Atribuição dos enderecos de retorn
- if (returns != null) {
- ArrayList<String> rts = new ArrayList<>();
- String t;
- for (Node r : returns.childrens()) {
- t = gerarVariavel("T");
- rts.add(t);
- code.Add(new Instruction()
- .S("type", "pop_return")
- .S("format", "pop_return")
- .S("p1value", "false")
- .S("p1", t));
- }
- call.addList("returns", rts);
- }
- return call;
- }
- /**
- * Gera inStrução de call
- *
- * @param funcname
- * @param numparam
- * @param idMaster
- * @param variavelRetorno
- * @throws Exception
- */
- protected Instruction __gerarCall(String funcname, String numparam, int idMaster) throws Exception {
- Instruction r = new Instruction();
- r.S("format", "call")
- .S("type", "call")
- .S("funcname", funcname)
- .S("nump", numparam).set("locker", idMaster);
- // if (!variavelRetorno.equals("")) {
- // r.S("dst", variavelRetorno);
- // }
- code.Add(r);
- return r;
- }
- }
- // 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;
- // }
|