BaseTacGen.java 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package IntermediaryCode;
  7. import API.Api;
  8. import API.Functions;
  9. import API.Instruction;
  10. import API.Tipos;
  11. import API.Utils;
  12. import API.Variaveis;
  13. import ast.AbstractSyntaxTree;
  14. import ast.Node;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.LinkedHashMap;
  18. import java.util.LinkedList;
  19. import java.util.Map;
  20. import java.util.Stack;
  21. import java.util.regex.Pattern;
  22. // a0 a1
  23. // sync recebe mandando
  24. // exec recebe mandando
  25. // synexec recebe mandando
  26. /**
  27. *
  28. * @author Eugenio
  29. */
  30. public final class BaseTacGen implements TacGenInterface {
  31. protected boolean inicializado = false;
  32. protected int incLabel = 0;
  33. protected int tmpVarCount = 1;
  34. protected int OrTestAndOccurrence = 0;
  35. protected AbstractSyntaxTree ast;
  36. protected Code code;
  37. protected Node calEndAtt;
  38. protected Node compileParams;
  39. protected String label;
  40. protected String calEndVar;
  41. protected String calEndSize;
  42. protected String calEndTipo;
  43. protected Stack<Node> master = new Stack<>();
  44. protected ArrayList<String> line = new ArrayList<>();
  45. protected LinkedList<Boolean> branckInvert = new LinkedList<>();
  46. protected HashMap<String, String> arrayAcesso = new HashMap<>();
  47. protected HashMap<String, String> varGenMap = new HashMap<>();
  48. protected HashMap<String, String> funcGenMap = new HashMap<>();
  49. protected HashMap<String, Integer> systemCalls = new HashMap<>();
  50. protected ArrayList<String> currentMatrixvalues = new ArrayList<>();
  51. protected HashMap<String, ArrayList<String>> matrixValues = new HashMap<>();
  52. // protected ArrayList<CodeProcessing> afterCreate = new ArrayList<CodeProcessing>();
  53. protected HashMap<String, String> Complements = new HashMap<String, String>() {
  54. {
  55. put("!=", "==");
  56. put("==", "!=");
  57. put("<", ">=");
  58. put("<=", ">");
  59. put(">", "<=");
  60. put(">=", "<");
  61. }
  62. };
  63. public static Pattern AddressOperator = Pattern.compile("(\\&|\\*)");
  64. protected boolean enableOtimization = true;
  65. protected HashMap<String, String> ArrayShiftMap = new HashMap<>();
  66. public BaseTacGen() throws Exception {
  67. for (long i = 0; i < 31; i++) {
  68. power2.put(Long.valueOf(1 << i), Long.valueOf(i));
  69. }
  70. }
  71. protected int calc(ArrayList<Integer> data) {
  72. Integer op1, op2, operacao;
  73. switch (data.size()) {
  74. case 1:
  75. return data.get(0);
  76. case 3:
  77. op1 = data.remove(0);
  78. operacao = data.remove(0);
  79. op2 = data.remove(0);
  80. if (operacao == 1) {
  81. return op1 - op2;
  82. // throw new Exception("Quantidade de entradas invalidas!" + data.size());
  83. } else {
  84. return op1 + op2;
  85. }
  86. default:
  87. op1 = data.remove(0);
  88. operacao = data.remove(0);
  89. if (operacao == 1) {
  90. return op1 - calc(data);
  91. // throw new Exception("Quantidade de entradas invalidas!" + data.size());
  92. } else {
  93. return op1 + calc(data);
  94. }
  95. }
  96. }
  97. public BaseTacGen(AbstractSyntaxTree ast) {
  98. Ast(ast);
  99. }
  100. public void Ast(AbstractSyntaxTree ast) {
  101. this.ast = ast;
  102. code = new Code("TAC", ast);
  103. code.AfterClose("clear.1", new CodeClearProcessor());
  104. // code.AfterClose("oti.1", new CodeOtimizadorProcessor(enableOtimization));
  105. // code.AfterClose("oti.1", new CodeOtimizadorProcessor(false));
  106. compileParams = ast.BuildParams();
  107. code.Template(new template.Template())
  108. .Formats(new HashMap<String, String>() {
  109. {
  110. String base = "{[PAD(global.position,_addressLen,' ')]':'}{[T(1)]}{[PAD(block.position,_addressLen,' ')]':'}{[T(2)]}";
  111. String end = "{[T(2)]}{' T< '[type]' >'}";
  112. put("label", "{[PAD(global.position,_addressLen,' ')]':'}{[T(2)]}{'<'[label]'>'}':'");
  113. put("assign", base + "{[dst]}' := '{[p1]}{' '[op]' '}{[p2]}{' --'[comment]}" + end);
  114. put("pointer_assignment", base + "{[dst.pointer]}{[dst]' := '}{[op]}{[p1]}{' --'[comment]}" + end);
  115. put("unary", base + " {[dst]} ' := '{[op]' '}{[p1]} {' --'[comment]}" + end);
  116. put("copy", base + " {[dst]} ' := '{[p1]} {' --'[comment]}" + end);
  117. put("jump", base + " 'goto ' {'<'[label]'>'} {' --'[comment]}" + end);
  118. put("branch", base + " 'if '{[p1]} {' '[op]' '} {[p2]} ' goto ' {'<'[label]'>'} {' --'[comment]}" + end);
  119. put("call", base + " {[dst]' := '} 'call <' {[funcname]} '>, ' {[nump]} {' --'[comment]}" + end);
  120. put("return", base + " 'return ' {[p1]} {' --'[comment]}" + end);
  121. put("push_param", base + " 'push_param '{[p1]} {' --'[comment]}" + end);
  122. put("push_return", base + " 'push_return '{[p1]} {' --'[comment]}" + end);
  123. put("pop_param", base + " 'pop_param '{[p1]} {' --'[comment]}" + end);
  124. put("pop_return", base + " 'pop_return '{[p1]} {' --'[comment]}" + end);
  125. }
  126. });
  127. }
  128. @Override
  129. public Code Create(AbstractSyntaxTree ast, HashMap<String, String> settings) throws Exception {
  130. Ast(ast);
  131. // setOtimizacao(settings.G("clearCode").equals("true"));
  132. Code code = run();
  133. // Executa todos os tratametos definidos apos geracao do codigo
  134. // AfterCreate();
  135. printVarMap();
  136. // code.PrintData();
  137. code.Print();
  138. code.printOcorrences();
  139. return code;
  140. }
  141. protected Node visit(Node n) throws Exception {
  142. if (null == n || n.eq("visited", "true")) {
  143. return n;
  144. }
  145. n.S("visited", "true");
  146. // System.out.println("Visit:" + n.Class());
  147. switch (n.Class()) {
  148. case "dec.const":
  149. gerarConstante(n);
  150. break;
  151. case "if.stmt":
  152. IfStmts(n);
  153. break;
  154. case "selector":
  155. Selector(n);
  156. break;
  157. case "dec.var":
  158. // Log.PrintInfo("TAC", new Instruction().S("msg", "DECVAR." + n));
  159. if (n.find("exprs") != null) {
  160. n.S("class", "dec.var.short")
  161. .S("value", "dec.var.short");
  162. genAssign(n);
  163. break;
  164. }
  165. case "def.assign":
  166. genAssign(n);
  167. break;
  168. case "call":
  169. genCall(n);
  170. break;
  171. case "ID":
  172. genID(n);
  173. break;
  174. case "incdec":
  175. IncDecStmt(n);
  176. break;
  177. case "for.stmt":
  178. genFor(n);
  179. break;
  180. case "expr":
  181. _visitExpr(n);
  182. break;
  183. case "unary":
  184. unaryStmt(n);
  185. break;
  186. case "switch.stmt":
  187. switchStmt(n);
  188. break;
  189. case "test":
  190. test(n);
  191. break;
  192. case "return":
  193. visitChildrensFirst(n);
  194. break;
  195. default:
  196. visitRootFirst(n);
  197. break;
  198. }
  199. return n;
  200. }
  201. protected void genID(Node n) throws Exception {
  202. // System.out.println("GenId:" + n);
  203. Node var = Variaveis.Get(n.getText());
  204. n.S("_return", gerarVariavel(var));
  205. }
  206. protected void test(Node n) throws Exception {
  207. Node x = null;
  208. ArrayList<Node> chils = n.childrens();
  209. int size = chils.size();
  210. for (int i = 0; i < size; i++) {
  211. x = chils.get(i);
  212. visit(x);
  213. }
  214. n.copy("_return", x);
  215. }
  216. protected void IncDecStmt(Node n) throws Exception {
  217. Instruction operacao = new Instruction();
  218. String dst = gerarVariavel(n.findByClass("selector"));
  219. operacao.S("type", "assign")
  220. .S("format", "assign")
  221. .S("cat", "exp")
  222. .S("_return", "exp")
  223. .S("dst", dst)
  224. .S("p1", dst)
  225. .S("p2", "1")
  226. .S("p1value", "" + Api.IsValue(dst))
  227. .S("p2value", "true")
  228. .S("op", n.findByClass("operator").eq("value", "++") ? "+" : "-");
  229. code.Add(operacao);
  230. }
  231. protected void Selector(Node n) throws Exception {
  232. String tmp;
  233. String shift = null;
  234. n.S("copymode", "copy");
  235. switch (n.G("subclass")) {
  236. case "operand":
  237. if (n.eq("classType", "value")) {
  238. // n.S("value", Api.Value(n.getText()));
  239. n.S("_return", Api.Value(n.getText()));
  240. } else {
  241. String v = gerarVariavel(n);
  242. if (n.eq("pointer", "*") && n.eq("access", "read")) {
  243. String t = gerarVariavel("T");
  244. Copy(t, v, false);
  245. v = t;
  246. }
  247. n.S("_return", v);
  248. }
  249. // System.out.println("Seletor operand:" + n);
  250. break;
  251. case "index":
  252. // shift = n.childrens().G(0).getInt("value");
  253. // shift = n.childrens().G(0).getInt("value");
  254. shift = ArrayShift(n);
  255. case "selector":
  256. if (shift == null) {
  257. shift = "" + Variaveis.Shift(n.getText());
  258. }
  259. tmp = gerarVariavel(n) + "[" + shift + "]";
  260. if (!n.eq("access", "write")) {
  261. String t = gerarVariavel("T");
  262. Copy(t, tmp, false);
  263. tmp = t;
  264. }
  265. n.S("_return", tmp);
  266. break;
  267. case "arguments":
  268. break;
  269. }
  270. }
  271. protected ArrayList<Integer> ShiftPath(int size) {
  272. ArrayList<Integer> ret = new ArrayList<>();
  273. FindShiftPath(size, ret);
  274. return ret;
  275. }
  276. protected ArrayList<Integer> ShiftInterval(int size) {
  277. ArrayList<Integer> ret = new ArrayList<>();
  278. Integer op = 1, i = 1, after, before;
  279. //
  280. while (true) {
  281. after = 1 << (i++);
  282. if (after == size) {
  283. before = after;
  284. break;
  285. } else if (after > size) {
  286. before = after >> 1;
  287. break;
  288. }
  289. }
  290. // before = Math.abs(before - size);
  291. // after = Math.abs(after - size);
  292. if (Math.abs(before - size) <= Math.abs(after - size)) {
  293. // Se o valor anterios for menor que o posterior
  294. op = 0;
  295. }
  296. // else {
  297. // // Se o valor posterior for menor que o anterior
  298. // }
  299. ret.add(before);
  300. ret.add(after);
  301. ret.add(2, op);
  302. // System.out.println("Interval:{" + size + "}" + ret + "::" + op);
  303. return ret;
  304. }
  305. protected LinkedHashMap<Long, Long> power2 = new LinkedHashMap<Long, Long>();
  306. protected void FindShiftPath(int size, ArrayList<Integer> path) {
  307. size = Math.abs(size);
  308. ArrayList<Integer> shift = ShiftInterval(size);
  309. int op = shift.get(2), s1 = shift.get(op);
  310. // System.out.println("FindShiftPath:(" + size + "|" + s1 + ")\t\t" + shift);
  311. if (power2.containsKey(s1)) {
  312. path.add(s1);
  313. path.add(op);
  314. }
  315. if (size == 1) {
  316. // path.add(size);
  317. }
  318. if (path.size() % 2 == 0 && Math.abs(size - s1) == 0) {
  319. path.remove(path.size() - 1);
  320. return;
  321. }
  322. FindShiftPath(size - s1, path);
  323. // int i = 1, after, before;
  324. // while (true) {
  325. // after = 1 << (i++);
  326. // if (after == size) {
  327. //
  328. // break;
  329. // } else if (after > size) {
  330. // break;
  331. // }
  332. // }
  333. // before = after >> 1;
  334. //
  335. // if (Math.abs(size - before) < Math.abs(after - size)) {
  336. // // Se o valor anterios for menor que o posterior
  337. // op = 1;
  338. // m4 = before;
  339. // } else {
  340. // // Se o valor posterior for menor que o anterior
  341. // op = 0;
  342. // m4 = after;
  343. // }
  344. // FindShiftPath(size, m4, op, path);
  345. }
  346. protected int Size(String type) {
  347. return Tipos.Size(type) * 4;
  348. }
  349. protected String ArrayShift(Node n) throws Exception {
  350. // Cria a chave para a ocorrencia de um acesso ao array, evitando recalcular o endereco
  351. String key = n.getText();
  352. for (Node node : n.childrens()) {
  353. key += "_" + node.getText();
  354. }
  355. if (!ArrayShiftMap.containsKey(key)) {
  356. // String shift;
  357. Node var = Variaveis.Get(n.getText());
  358. // System.out.println("INDEX shift:" + n.childrens());
  359. for (int i = 0, j = 1; j < var.childrens().size(); j++) {
  360. n.childrens().get(i++).S("collNumber", var.childrens().get(j).getText());
  361. }
  362. String ret = CalcArrayAddress(n.childrens(), Size(var.G("type")));
  363. // Se o offset for calculado em um inteiro atribui o mesmo como shift
  364. // if (Utils.isNumber(ret)) {
  365. // shift = ret;
  366. // } else {
  367. //// Se for um endereci faz a soma com o offset da varivel
  368. //// __exp("+", ret, gerarVariavel(n), ret);
  369. // shift = ret;
  370. // }
  371. ArrayShiftMap.put(key, ret);
  372. }
  373. return ArrayShiftMap.get(key);
  374. }
  375. protected String GenShiftLeftLogical(String var, int size, boolean mps) throws Exception {
  376. ArrayList<Integer> path = ShiftPath(size);
  377. ArrayList<Integer> reverse = new ArrayList<>();
  378. for (int i = path.size() - 1; i >= 0; i--) {
  379. reverse.add(path.remove(i));
  380. }
  381. System.out.println(">>>>>(var:" + var + ", size:" + size + ") arithpath:" + reverse);
  382. var = GSLL(var, reverse, mps, size);
  383. return var;
  384. }
  385. protected String GSLL(String var, ArrayList<Integer> path, boolean mps, int size) throws Exception {
  386. int op1, operacao, op2;
  387. String T1 = gerarVariavel("T"),
  388. T2 = gerarVariavel("T"),
  389. T3 = gerarVariavel("T");
  390. switch (path.size()) {
  391. case 1:
  392. if (!mps) {
  393. Copy(T1, var, false)
  394. .S("comment", "Copy value of index")
  395. .S("copy", "true");
  396. } else {
  397. T1 = var;
  398. }
  399. if (mps) {
  400. __exp("<<", T1, "" + power2.get(size), T2).S("comment", "");
  401. return T2;
  402. }
  403. return T1;
  404. case 3:
  405. op1 = path.remove(0);
  406. operacao = path.remove(0);
  407. op2 = path.remove(0);
  408. // String T4 = gerarVariavel("T");
  409. if (!mps) {
  410. Copy(T1, var, false)
  411. .S("comment", "Copy value of index")
  412. .S("copy", "true");
  413. }
  414. if (op1 != 1) {
  415. __exp("<<", T1, "" + power2.get(op1), T1).S("comment", "");
  416. }
  417. // if (op2 == 1) {
  418. // } else {
  419. // Copy(T3, var, false).S("comment", "Copy value of index");
  420. if (op2 != 1) {
  421. __exp("<<", T1, "" + (power2.get(op2) - power2.get(op1)), T2).S("comment", "");
  422. // deve realizar um novo deslocamento
  423. } else {
  424. T2 = T1;
  425. }
  426. // Soma o deslocamento com o tamanho
  427. __exp("+", T1, T2, T1).S("comment", "");
  428. return T1;
  429. default:
  430. // op1 = path.remove(0);
  431. // operacao = path.remove(0);
  432. //
  433. // T2 = GSLL(var, path);
  434. //
  435. // Copy(T1, var, false).S("comment", "Copy value of index");
  436. //
  437. // __exp("<<", T1, "" + power2.get(op1), T1).S("comment", "");
  438. // __exp("+", T1, T2, T1).S("comment", "");
  439. }
  440. return var;
  441. }
  442. protected String CalcArrayAddress(ArrayList<Node> childrens, int Size) throws Exception {
  443. System.out.println("CalcArrayAddressINIT:" + Size);
  444. boolean onlyNumber = true;
  445. for (Node node : childrens) {
  446. if (!node.isNumber("value")) {
  447. onlyNumber = false;
  448. break;
  449. }
  450. }
  451. if (onlyNumber) {
  452. return "" + (CalcArrayAddressNumber(childrens, Size) * Size);
  453. } else {
  454. // System.out.println("CAA:" + childrens);
  455. String current = CalcArrayAddressAux(childrens, Size);
  456. System.out.println("Size {" + current + "}:----------->" + Size);
  457. // multiplica pelo tamanho
  458. return GenShiftLeftLogical(current, Size, true);
  459. }
  460. }
  461. protected Integer CalcArrayAddressNumber(ArrayList<Node> childrens, int Size) throws Exception {
  462. Node ind;
  463. ind = childrens.remove(0);
  464. Integer value = ind.getInt("value");
  465. // ultimo elemento da idexacao
  466. if (childrens.size() == 0) {
  467. return value;
  468. }
  469. Integer collNumber = ind.getInt("collNumber"),
  470. ret = CalcArrayAddressNumber(childrens, Size);
  471. return ((value * collNumber) + ret);
  472. }
  473. protected String CalcArrayAddressAux(ArrayList<Node> childrens, int Size) throws Exception {
  474. String current;
  475. // boolean multPerSize = true;
  476. Node ind, nextIndex;
  477. ind = childrens.remove(0);
  478. System.out.println("CalcArrayAddressAux:" + childrens.size() + ":" + Size);
  479. // ultimo elemento da idexacao
  480. if (childrens.size() == 0) {
  481. if (Utils.isNumber(ind.getText())) {
  482. return "" + ind.getInt("value");
  483. } else {
  484. return GenShiftLeftLogical(gerarVariavel(ind), Size, false);
  485. }
  486. }
  487. int collNumber = ind.getInt("collNumber");
  488. nextIndex = childrens.get(0);
  489. if (ind.isNumber("value") && nextIndex.isNumber("value")) {
  490. // System.out.println("CalcArray:" + ind.getInt("value") + ":" + ind.getInt("collNumber") + ":" + Integer.parseInt(ret));
  491. current = "" + (ind.getInt("value") * collNumber);
  492. // multPerSize = false;
  493. } else {
  494. // System.out.println("collNumber:" + collNumber);
  495. current = GenShiftLeftLogical(gerarVariavel(ind), collNumber, false);
  496. // System.out.println("Gerando index calc:" + retl);
  497. // current = GenShiftLeftLogical(retl, Size);
  498. // current = retl;
  499. }
  500. String ret = CalcArrayAddressAux(childrens, Size);
  501. __exp("+u", current, ret, current).S("comment", "colls shift");
  502. // if (multPerSize) {
  503. // // Realiza a multiplicacao pelo tamanho do elemento
  504. // current = GenShiftLeftLogical(current, Size);
  505. // }
  506. return current;
  507. }
  508. // protected String CalcArrayAddress(ArrayList<Node> childrens, int Size, int index) throws Exception {
  509. // System.out.println("CalcArrayAddress:" + childrens.size() + ":" + Size);
  510. //
  511. // int nindex = index + 1;
  512. // String retl;
  513. // Node ind = childrens.remove(0);
  514. // if (childrens.size() >= 1) {
  515. // String ret = CalcArrayAddress(childrens, Size, nindex);
  516. // int collNumber = ind.getInt("collNumber");
  517. //
  518. // if (Utils.isNumber(ret) && ind.isNumber("value")) {
  519. //// System.out.println("CalcArray:" + ind.getInt("value") + ":" + ind.getInt("collNumber") + ":" + Integer.parseInt(ret));
  520. // return "" + ((ind.getInt("value") * collNumber) + Integer.parseInt(ret));
  521. // } else {
  522. //// System.out.println("collNumber:" + collNumber);
  523. //
  524. // retl = GenShiftLeftLogical(gerarVariavel(ind), collNumber);
  525. //
  526. // retl = GenShiftLeftLogical(retl, Size);
  527. //
  528. // __exp("+", retl, ret, retl).S("comment", "colls shift");
  529. // return retl;
  530. // }
  531. //
  532. // /*
  533. // Implementacao com mult **
  534. // int collNumber = ind.getInt("collNumber");
  535. //
  536. // if (Utils.isNumber(ret) && ind.isNumber("value")) {
  537. // System.out.println("CalcArray:" + ind.getInt("value") + ":" + collNumber + ":" + Integer.parseInt(ret));
  538. // return "" + ((ind.getInt("value") * collNumber) + Integer.parseInt(ret));
  539. // } else {
  540. // String retl = gerarVariavel("T");
  541. // __exp("*", gerarVariavel(ind), "" + collNumber, retl)
  542. // .S("comment", "lines shift");
  543. // __exp("+", retl, ret, retl)
  544. // .S("comment", "colls shift");
  545. // return retl;
  546. // }
  547. // */
  548. // } else {
  549. //
  550. // if (Utils.isNumber(ind.getText())) {
  551. // return ind.getText();
  552. // } else {
  553. // // ultimo elemento da idexacao
  554. //
  555. // return GenShiftLeftLogical(gerarVariavel(ind), Size);
  556. // }
  557. //
  558. // }
  559. // }
  560. /**
  561. * ProceSSa um node:<br>
  562. * Ordem:<br>
  563. * ->Primeiro filhoS;<br>
  564. * ->No principal;<br>
  565. *
  566. * @param n
  567. * @throws Exception
  568. */
  569. protected void visitChildrensFirst(Node n) throws Exception {
  570. // System.out.println("Visit:" + n.getText());
  571. for (Node n1 : n.childrens()) {
  572. // System.out.println("Visit:" + n1.getText());
  573. visit(n1);
  574. }
  575. // System.out.println("Process:" + n.getText());
  576. processarNode(n);
  577. }
  578. /**
  579. * ProceSSa um node:<br>
  580. * Ordem:<br>
  581. * ->Primeiro filho;<br>
  582. * ->No principal;<br>
  583. * ->Segundo filho;<br>
  584. *
  585. * @param n
  586. * @throws Exception
  587. */
  588. protected void visitNoMeio(Node n) throws Exception {
  589. visit(n.getFilho(0));
  590. processarNode(n);
  591. visit(n.getFilho(1));
  592. }
  593. /**
  594. * ProceSSa um node:<br>
  595. * Ordem:<br>
  596. * ->No princial;<br>
  597. * ->DepoiS oS filhoS
  598. *
  599. * @param n
  600. * @throws Exception
  601. */
  602. protected void visitRootFirst(Node n) throws Exception {
  603. processarNode(n);
  604. for (Node n1 : n.childrens()) {
  605. visit(n1);
  606. }
  607. }
  608. /**
  609. * Aloca endereço no inicio da memoria para conStanteS ApenaS tipoS<br>
  610. * [inteiro, booleano e char]
  611. *
  612. * @param n
  613. * @throws Exception
  614. */
  615. protected void gerarConstante(Node n) throws Exception {
  616. gerarVariavel(n);
  617. // getLabel("C", n);
  618. // if (n.closest("block", "class") != null) {
  619. // code.registerVar(n.G("_return"), 1, DataFrame.TYPE_VAR);
  620. // }
  621. }
  622. /**
  623. * Executa a extração fo block principal
  624. *
  625. * @throws Exception
  626. */
  627. public void main() throws Exception {
  628. Node main = ast.getMain();
  629. // main.S("_return", Threads.getId("main"));
  630. Node stmts = main.find("stmts");
  631. if (code.OpenBlock("main")) {
  632. PushLabel("main", "block");
  633. visit(stmts);
  634. PushLabel("main-end", "block");
  635. code.CloseBlock();
  636. }
  637. }
  638. public Code run() throws Exception {
  639. if (!inicializado) {
  640. inicializado = true;
  641. main();
  642. }
  643. code.UpdatePositions();
  644. return code;
  645. }
  646. /**
  647. * ProceSSa If (ElSe'S)*
  648. *
  649. * @param n
  650. * @throws Exception
  651. */
  652. protected void IfStmts(Node n) throws Exception {
  653. String end = gerarLabel(), next;
  654. boolean ifChain = n.childrens().size() > 1;
  655. if (ifChain) { //(If)+ (else)?
  656. next = gerarLabel();
  657. } else {
  658. next = end;
  659. }
  660. n.S("next", next).S("end", end);
  661. for (Node stmt : n.childrens()) {
  662. switch (stmt.Class()) {
  663. case "if.case.stmt":
  664. IfCaseStmt(n, stmt, ifChain);
  665. break;
  666. // Else case
  667. case "stmts":
  668. // String next = gerarLabel();
  669. // n.S("next", next);
  670. visit(stmt.S("next", next));
  671. // Cria um PushLabel final
  672. // PushLabel(n.G("end"), "branch");
  673. // PushLabel(end);
  674. break;
  675. }
  676. }
  677. // Cria um PushLabel final
  678. // System.out.println("Label end:" + n.G("end"));
  679. PushLabel(n.G("end"), "branch");
  680. }
  681. /**
  682. * ProceSSa If, eSSe metodo deve Ser chamado pelo metodo IfStmtS
  683. *
  684. * @param n
  685. * @param _if
  686. * @param ifChain
  687. * @throws Exception
  688. */
  689. protected void IfCaseStmt(Node n, Node _if, boolean ifChain) throws Exception {
  690. Node test = _if.first().first();
  691. Node stmts = _if.find("stmts");
  692. n.S("start", gerarLabel());
  693. test.copy("start,next,end", n);
  694. Branch(test, 0);
  695. // marca com um label o inicio do bloco do if
  696. PushLabel(n.G("start"), "branch");
  697. // gera as instruções do bloco
  698. visit(stmts);
  699. // se for uma cadeia de condiçoes com ou sem else
  700. if (ifChain) {
  701. // Salta os outros testes
  702. _goto(n.G("end"));
  703. //Adiciona o label de proximo elemento da cadeia de if's e else
  704. PushLabel(n.G("next"), "branch");
  705. //Atualiza o ponteiro para o proximo elemento
  706. n.S("next", gerarLabel());
  707. }
  708. }
  709. /**
  710. * Executa tratamento de um no eSpecifico, Selecionado pelo atributo claSS
  711. *
  712. * @param n
  713. * @throws Exception
  714. */
  715. protected void processarNode(Node n) throws Exception {
  716. switch (n.Class()) {
  717. case "return":
  718. genReturn(n);
  719. break;
  720. case "ctrl.stmt":
  721. ctrlStmt(n);
  722. break;
  723. case "label.stmt":
  724. PushLabel(n.getText(), "user");
  725. break;
  726. }
  727. }
  728. protected void _visitExpr(Node n) throws Exception {
  729. // System.out.println("VisitExpr:" + n);
  730. visit(n.childrens().get(0));
  731. visit(n.childrens().get(1));
  732. switch (n.G("subclass")) {
  733. case "arith":// ( + | - | * | / ) integer
  734. genExpArith(n);
  735. break;
  736. }
  737. }
  738. protected void BranchAnd(Node n, int index) throws Exception {
  739. // int pos = 0;
  740. OrTestAndOccurrence++;
  741. Instruction instruction = null;
  742. ArrayList<Node> x = n.childrens();
  743. Node op1 = x.get(0), op2 = x.get(1);
  744. String type = n.G("test.type"),
  745. step = gerarLabel(),
  746. attrs = "next,start,end,inc,test.type,skip.test";
  747. n.S("skip.test", step);
  748. op1.copy(attrs, n);
  749. op2.copy(attrs, n);
  750. instruction = Branch(op1, 1);
  751. if (instruction != null) {
  752. switch (type) {
  753. case "jtb":
  754. instruction.S("op", Complement(instruction.G("op")));
  755. if (n.parent.eq("value", "||")) {
  756. if (index == 1) {
  757. instruction.S("label", n.parent.G("skip.test"));
  758. } else if (index == 2) {
  759. instruction.S("label", n.parent.G("end"));
  760. }
  761. // } else if (n.parent.eq("value", "&&")) {
  762. } else {
  763. instruction.S("label", n.G("end"));
  764. }
  765. }
  766. }
  767. // System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
  768. // if (index == 0 || (op1.in("value", new String[]{"||", "&&"}) && !n.eq("value", op1.G("value")))) {
  769. // Registra o label para o saldo dos testes do primeiro parametro
  770. PushLabel(step, "branch");
  771. // }
  772. instruction = Branch(op2, 2);
  773. if (instruction != null) {
  774. switch (type) {
  775. case "jtb":
  776. if (n.parent.eq("value", "||")) {
  777. if (index == 1) { // teste 1 do parametro 2 com pai ||
  778. instruction.S("op", Complement(instruction.G("op")));
  779. instruction.S("label", n.G("end"));
  780. } else if (index == 2) { // teste 2 do parametro 2
  781. instruction.S("label", n.G("start"));
  782. }
  783. // } else if (n.parent.eq("value", "&&")) {
  784. // if (index == 1) { // teste 1 do parametro 2 com pai &&
  785. //// throw new Exception("// teste 1 do parametro 2 com pai &&");
  786. //
  787. // } else if (index == 2) { // teste 2 do parametro 2 com pai &&
  788. // instruction.S("op", Complement(instruction.G("op")));
  789. // instruction.S("label", n.G("end"));
  790. // }
  791. } else {
  792. if (index == 1) { // teste 1 do parametro 2 geral
  793. instruction.S("op", Complement(instruction.G("op")));
  794. instruction.S("label", n.G("end"));
  795. } else if (index == 2) {
  796. // throw new Exception("teste 2 do parametro 2 geral");
  797. }
  798. }
  799. }
  800. }
  801. }
  802. protected void BranchOr(Node n, int index) throws Exception {
  803. // int pos = 0;
  804. OrTestAndOccurrence++;
  805. Instruction instruction = null;
  806. ArrayList<Node> x = n.childrens();
  807. Node op1 = x.get(0), op2 = x.get(1);
  808. String type = n.G("test.type"),
  809. step = gerarLabel(),
  810. attrs = "next,start,end,inc,test.type,skip.test";
  811. n.S("skip.test", step);
  812. op1.copy(attrs, n);
  813. op2.copy(attrs, n);
  814. System.out.println("BranchOr:" + n.parent);
  815. // Executa o lado esquerdo do ||
  816. instruction = Branch(op1, 1);
  817. if (instruction != null) {
  818. switch (type) {
  819. case "jtb":
  820. if (n.parent.eq("value", "&&")) {
  821. if (index == 1) {
  822. instruction.S("label", n.parent.G("skip.test"));
  823. }
  824. }
  825. }
  826. }
  827. // System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
  828. // Registra o label para o saldo dos testes do primeiro parametro
  829. PushLabel(step, "branch");
  830. // Executa o lado direito do ||
  831. instruction = Branch(op2, 2);
  832. if (instruction != null) {
  833. switch (type) {
  834. case "jtb":
  835. if (n.parent.eq("value", "&&")) {
  836. if (index == 1) {
  837. instruction.S("op", Complement(instruction.G("op")));
  838. instruction.S("label", n.parent.G("end"));
  839. }
  840. }
  841. }
  842. }
  843. }
  844. protected Instruction BranchTest(Node n, int index) throws Exception {
  845. ArrayList<Node> x = n.childrens();
  846. String type = n.G("test.type");
  847. boolean invert = false;
  848. String op, p0, p1, next = "";
  849. switch (type) {
  850. case "jb":
  851. invert = true;
  852. next = n.G("end");
  853. break;
  854. case "jtb":
  855. next = n.G("start");
  856. break;
  857. default:
  858. throw new Exception("test.type '".concat(type).concat("' not suported. Values {jtb,jb}"));
  859. }
  860. visit(n);
  861. // Resultado do test class ou acesso a um id ou bool
  862. if (!n.eq("class", "expr")) {
  863. op = "==";
  864. p0 = n.G("_return");
  865. p1 = "1";
  866. } else { //(<, <=, >, >=, ==, !=)
  867. op = n.getText();
  868. p0 = visit(x.get(0)).G("_return");
  869. p1 = visit(x.get(1)).G("_return");
  870. }
  871. // Inverte o teste e salta o bloco
  872. if (invert) {
  873. op = Complement(op);
  874. }
  875. return Branch(op, p0, p1, next);
  876. // switch (n.parent.G("value")) {
  877. // case "||":
  878. // if (index == 1) {
  879. // invert = false;
  880. // next = n.G("start");
  881. // }
  882. // break;
  883. // case "&&":
  884. // break;
  885. // default:
  886. // // Sem combinação ( || / && )
  887. // }
  888. // System.out.println("BranchTest:[" + index + ":" + op + ":" + invert
  889. // + ":" + n.eq("invert.test", "false") + "]" + n);
  890. // Instruction instruction = Branch(op, p0, p1, label);
  891. // instruction. ;
  892. // if (n.Has("false")) {
  893. // _goto(n.G("false"));
  894. // }
  895. // if (empty) {
  896. // branckInvert.pop();
  897. // }
  898. }
  899. protected Instruction Branch(String op, final String p1, final String p2, String label) throws Exception {
  900. Instruction r1 = new Instruction();
  901. r1.S("format", "branch")
  902. .S("type", "branch")
  903. .S("op", op)
  904. .S("p1", p1)
  905. .S("p2", p2)
  906. .S("p1value", "" + Api.IsValue(p1))
  907. .S("p2value", "" + Api.IsValue(p2))
  908. .S("label", label);
  909. // System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
  910. code.Add(r1);
  911. return r1;
  912. }
  913. protected Instruction Branch(Node n, int index) throws Exception {
  914. // ArrayList<Node> x = n.childrens();
  915. Instruction instruction = null;
  916. switch (n.getText()) {
  917. case "&&":
  918. BranchAnd(n, index);
  919. break;
  920. case "||":
  921. BranchOr(n, index);
  922. break;
  923. default:
  924. instruction = BranchTest(n, index);
  925. }
  926. if (index == 0 && instruction != null) {
  927. }
  928. return instruction;
  929. // if (and) {
  930. // n.remove("or");
  931. // }
  932. }
  933. protected String Complement(String op) {
  934. return Complements.get(op);
  935. }
  936. // /**
  937. // * ProceSSa um teSte logico AND
  938. // *
  939. // * @param n
  940. // * @throws Exception
  941. // */
  942. // protected void _and(Node n) throws Exception {
  943. // String attrs = "next,start,end,test,skip.test";
  944. // Node b1 = n.getFilho(0).copy(attrs, n);
  945. // Node b2 = n.getFilho(1).copy(attrs, n);
  946. //
  947. //// boolean isFor = n.eq("test.type", "for");
  948. //// System.out.println("_AND{" + isFor + "}:" + n.G(isFor ? "and" : "next"));
  949. //// String l = n.G(n.eq("test.type", "for") ? "and" : "next");
  950. // b1.S("true", n.G("next"));
  951. //// b1.S("false", b.G("false"));
  952. //// System.out.println("_AND:" + label + "-s" + b1);
  953. // b2.S("true", n.G("next"));
  954. //// b2.S("false", b.G("false"));
  955. //// b1.S("true", gerarLabel());
  956. //// b1.S("false", b.G("false"));
  957. ////
  958. //// b2.S("true", b.G("true"));
  959. //// b2.S("false", b.G("false"));
  960. // }
  961. // /**
  962. // * ProceSSa um teSte logico OR
  963. // *
  964. // * @param n
  965. // * @throws Exception
  966. // */
  967. // protected void _or(Node n) throws Exception {
  968. //
  969. // String attrs = "next,start,end,test,skip.test";
  970. // Node b1 = n.getFilho(0).copy(attrs, n);
  971. // Node b2 = n.getFilho(1).copy(attrs, n);
  972. //
  973. // b1.S("true", n.G("start"));
  974. // b2.S("true", n.G("start"));
  975. //
  976. // }
  977. /**
  978. * Cria uma linha de Salto para PuShLabel eSpecificado no parametro.
  979. *
  980. * @param label
  981. * @throws Exception
  982. */
  983. protected void _goto(String label) throws Exception {
  984. Instruction r = new Instruction();
  985. r.S("label", label)
  986. .S("type", "jump")
  987. .S("format", "jump")
  988. .set("locker", getIdMaster());
  989. code.Add(r);
  990. }
  991. /*Inicio das avaliacoes booleanas*/
  992. /**
  993. * Gera um linha de inStrução de retorno no block de codigo
  994. *
  995. * @param n
  996. * @throws Exception
  997. */
  998. protected void genReturn(Node n) throws Exception {
  999. Instruction nreg;
  1000. ArrayList<Node> returns = n.find("exprs").childrens();
  1001. for (Node ret : returns) {
  1002. // System.out.println("Return p:" + ret);
  1003. nreg = new Instruction()
  1004. .S("type", "push_return")
  1005. .S("format", "push_return")
  1006. .S("p1value", "" + Api.IsValue(ret.getText()))
  1007. .S("p1", ret.G("_return"));
  1008. nreg.set("locker", getIdMaster());
  1009. code.Add(nreg);
  1010. }
  1011. code.Add(new Instruction()
  1012. .S("type", "return")
  1013. .S("format", "return")
  1014. .set("p1", returns.size())
  1015. .S("p1value", "true")
  1016. .set("locker", getIdMaster()));
  1017. }
  1018. //
  1019. // protected String updateLabel(Node n) {
  1020. // String PushLabel = n.G("_return");
  1021. //// if (!PushLabel.substring(0, 1).equals("_R")) {
  1022. //// }
  1023. //// n.S("_return", PushLabel);
  1024. // return PushLabel;
  1025. // }
  1026. /**
  1027. * Gera todoS oS SaltoS de um loop for
  1028. *
  1029. * @param n
  1030. * @throws Exception
  1031. */
  1032. protected void genFor(Node n) throws Exception {
  1033. String attrs = "next,start,end,inc,test";
  1034. Node test = n.find("test").first();
  1035. setMaster(n);
  1036. String start = gerarLabel(),
  1037. // next = gerarLabel(),
  1038. end = gerarLabel(),
  1039. inc = gerarLabel(),
  1040. testLabel = gerarLabel();
  1041. n.S("start", start)
  1042. .S("end", end)
  1043. .S("inc", inc)
  1044. .S("test", testLabel);
  1045. Node stmt = n.find("stmts");
  1046. stmt.copy(attrs, n);
  1047. test.copy(attrs, n).S("test.type", "jtb");
  1048. /*Inicializacoes e atribuicoes*/
  1049. visit(n.find("initialization"));
  1050. /*Salta para o teste do laco*/
  1051. _goto(testLabel);
  1052. /*Marca o inicio do bloco do laco*/
  1053. PushLabel(start, "branch");
  1054. /*Executa o bloco do for*/
  1055. visit(stmt);
  1056. /*Label que marca os incrementos dos laco*/
  1057. PushLabel(inc, "branch");
  1058. /*Executa os incrementos*/
  1059. visit(n.find("increment"));
  1060. /*Label que marca o teste do laco*/
  1061. PushLabel(testLabel, "branch");
  1062. // test.type = ((JumpToBlock in LOOP) | (JumpBlock in IF || SWITCH))
  1063. // test.type = JumpToBlock
  1064. // test.S("test.type", "for").S("next", next).S("end", end).S("or", next);
  1065. // System.out.println("Teste:" + test);
  1066. Branch(test, 0);
  1067. // if (n.Has("set.end_label")) {
  1068. // PushLabel(n.G("end"), "branch");
  1069. PushLabel(end, "branch");
  1070. // }
  1071. setNullMaster();
  1072. }
  1073. // protected void genFor(Node n) throws Exception {
  1074. // setMaster(n);
  1075. // String start = gerarLabel(), next = gerarLabel();
  1076. //
  1077. // n.S("next", next);
  1078. // n.S("start", start);
  1079. //
  1080. // Node stmt = n.find("stmts");
  1081. // stmt.S("next", start);
  1082. // /*Inicializacoes e atribuicoes*/
  1083. // visit(n.find("initialization"));
  1084. // // Label que marca o inicio do for
  1085. // PushLabel(start, "branch");
  1086. //
  1087. // Node test = n.find("test").first();
  1088. //// if (test.hasChildrens())
  1089. //// test.S("true", gerarLabel());
  1090. //// test.S("false", n.G("next"));
  1091. //// test.S("true", next);
  1092. //// test.S("false", n.G("next"));
  1093. // test.S("and", next);
  1094. // test.S("next", next);
  1095. // test.S("or", gerarLabel());
  1096. //
  1097. // Branch(test);
  1098. //
  1099. // if (test.Has("or")) {
  1100. // PushLabel(test.G("or"), "branch");
  1101. // }
  1102. //
  1103. //// PushLabel(test.G("true"), "branch");
  1104. // /*Executa o bloco do for*/
  1105. // visit(stmt);
  1106. // /*Executa os incrementos*/
  1107. // visit(n.find("increment"));
  1108. // /*Salta para inicio do laço*/
  1109. // _goto(start);
  1110. // /*Marca o endereço final para sair do laço*/
  1111. // PushLabel(test.G("next"), "branch");
  1112. //
  1113. // setNullMaster();
  1114. // }
  1115. protected void genExpArith(Node n) throws Exception {
  1116. Node op1 = n.childrens.get(0);
  1117. Node op2 = n.childrens.get(1);
  1118. String _op = n.getText();
  1119. //
  1120. //
  1121. // // se os dois valores são
  1122. // if (op1.eq("type", "value") && op2.eq("type", "value")) {
  1123. //
  1124. // n.S("_return", resolveExpr(_op, op1.G("value"), op2.G("value")));
  1125. // }
  1126. // if (op1.isNumber("value") && op2.isNumber("value")) {
  1127. //
  1128. // } else {
  1129. // Inverte a ordem dos operandos se o segundo for seletor e o primeiro for valor
  1130. if (op1.isNumber("value") && !op2.isNumber("value")) {
  1131. Node aux = op1;
  1132. op1 = op2;
  1133. op2 = aux;
  1134. }
  1135. // System.out.println("GerarArit" + op1 + "-" + op2);
  1136. String _p1 = op1.G("_return");
  1137. String _p2 = op2.G("_return");
  1138. String _return = gerarVariavel("T");
  1139. n.S("_return", _return);
  1140. Instruction instruction = __exp(_op, _p1, _p2, _return);
  1141. if (instruction.eq("type", "copy")) {
  1142. n.S("_p1", instruction.G("p1"));
  1143. n.S("value", instruction.G("p1"));
  1144. } else {
  1145. n.S("_op", _op);
  1146. n.S("_p1", _p1);
  1147. n.S("_p2", _p2);
  1148. }
  1149. }
  1150. /**
  1151. * Para cima verificado <==>
  1152. */
  1153. protected void genArray(Node n) {
  1154. if (n.eq("raiz", "t")) {
  1155. currentMatrixvalues = new ArrayList<String>();
  1156. matrixValues.put(n.G("id"), currentMatrixvalues);
  1157. for (Node c : n.childrens()) {
  1158. genArray(c);
  1159. }
  1160. } else if (n.eq("terminal", "t")) {
  1161. currentMatrixvalues.add(n.getText());
  1162. } else {
  1163. for (Node c : n.childrens()) {
  1164. genArray(c);
  1165. }
  1166. }
  1167. }
  1168. protected ArrayList<String> initValues(Node n) {
  1169. ArrayList<String> values = new ArrayList<>();
  1170. switch (n.G("class")) {
  1171. case "_array":
  1172. values = matrixValues.get(n.G("id"));
  1173. break;
  1174. /*valores e constantes*/
  1175. default:
  1176. String _return = n.G("_return");
  1177. if (_return.matches("^_.*")) {
  1178. _return = "0";
  1179. }
  1180. values.add(_return);
  1181. }
  1182. return values;
  1183. }
  1184. /**
  1185. * Gera a chamada para o metodo de deSalocacao de memoria
  1186. *
  1187. * @param n
  1188. * @throws Exception
  1189. */
  1190. protected void genDelete(Node n) throws Exception {
  1191. for (Node address : n.childrens()) {
  1192. visit(address);
  1193. __genParam(address.G("_return"));
  1194. __gerarCall(Functions.FREE, "1", getIdMaster());
  1195. }
  1196. }
  1197. /**
  1198. * Gera a Switch caSe
  1199. *
  1200. * @param n
  1201. */
  1202. protected void switchStmt(Node n) throws Exception {
  1203. /*Label que sera consultado pelo break para sair do case*/
  1204. String end = gerarLabel();
  1205. String dst = null, next = null;
  1206. Node test = n.find("test");
  1207. n.S("next", end);
  1208. // Verifica se existe entrada para selecao
  1209. if (test.childrens().size() > 0) {
  1210. test = test.first();
  1211. visit(test);
  1212. dst = test.G("_return");
  1213. }
  1214. ArrayList<Node> cases = n.findAll("def.case", "class", 1);
  1215. int id = 0, count = cases.size() - 1;
  1216. for (Node _case : cases) {
  1217. if (next != null) {
  1218. _case.S("start", next);
  1219. }
  1220. _case.S("_input", dst);
  1221. _case.S("end", end);
  1222. genCase(_case, id == count);
  1223. next = _case.G("next");
  1224. id++;
  1225. }
  1226. /*Marca o endereço final para sair do switch*/
  1227. PushLabel(end, "branch");
  1228. }
  1229. protected Node NodeBoolExpr(String op, Node e1, Node e2) {
  1230. return new Node(op)
  1231. .S("type", "bool")
  1232. .S("class", "expr")
  1233. .S("subclass", "bool")
  1234. .addFilho(e1)
  1235. .addFilho(e2);
  1236. }
  1237. protected ArrayList<Node> ExprsEq(Node n, Node input) throws Exception {
  1238. ArrayList<Node> list = new ArrayList<>();
  1239. switch (n.Class()) {
  1240. case "exprs":
  1241. for (Node e : n.childrens()) {
  1242. list.add(NodeBoolExpr("==", input, e));
  1243. }
  1244. break;
  1245. default:
  1246. list.add(NodeBoolExpr("==", input, n));
  1247. }
  1248. return list;
  1249. }
  1250. /**
  1251. * Gera oS caSeS de um Switch
  1252. *
  1253. * @param n
  1254. */
  1255. protected void genCase(Node n, boolean last) throws Exception {
  1256. String start = n.Has("start") ? n.G("start") : gerarLabel();
  1257. String next = gerarLabel(), end;
  1258. // System.out.println("GenCase:" + start + ":" + next);
  1259. ArrayList<Node> itens = n.childrens();
  1260. Node stmts = n.find("stmts");
  1261. Node or;
  1262. if (itens.size() >= 2) { // Tem teste
  1263. String input = n.G("_input");
  1264. Node test = n.first();
  1265. ArrayList<Node> exprs;
  1266. if (input != null) {
  1267. exprs = ExprsEq(test, new Node(input).S("_return", input));
  1268. } else {
  1269. exprs = test.childrens();
  1270. }
  1271. /*Seleciona o endereco de saida se é no proximo case ou no fim do switch*/
  1272. if (stmts.last().eq("value", "fallthrough")) {
  1273. end = gerarLabel();
  1274. n.S("next", end);
  1275. } else {
  1276. end = n.G("end");
  1277. }
  1278. // Atualiza o PushLabel de sucesso das exprs
  1279. for (Node expr : exprs) {
  1280. expr.S("true", !last ? next : end);
  1281. // expr.S("false", next);
  1282. // expr.S("false", start);
  1283. }
  1284. Branch(OR(exprs).S("next", next), 0);
  1285. /*Se nenhum dos casos for compativel salta para o proximo*/
  1286. // _goto(!last ? next : end);
  1287. /*Marca o PushLabel inicial do caso*/
  1288. PushLabel(start, "branch");
  1289. /*Visita o bloco do case*/
  1290. visit(stmts);
  1291. if (!last) {
  1292. /*Salta para o proximo case ou para o fim do switchcase*/
  1293. _goto(end);
  1294. /*Label do inicio do proximo case*/
  1295. PushLabel(next, "branch");
  1296. }
  1297. } else { // Default statiment
  1298. visit(stmts);
  1299. }
  1300. }
  1301. protected Node OR(ArrayList<Node> n) throws Exception {
  1302. switch (n.size()) {
  1303. case 0:
  1304. return null;
  1305. case 1:
  1306. return n.get(0);
  1307. default:
  1308. Node x = n.get(0);
  1309. n.remove(x);
  1310. return NodeBoolExpr("||", x, OR(n)).copy("true,false", x);
  1311. }
  1312. }
  1313. protected int qtdByIndex(Node n) throws Exception {
  1314. Node indexes = n.find("indexes");
  1315. if (indexes == null) {
  1316. return 1;
  1317. } else {
  1318. int sum = 0;
  1319. for (Node index : indexes.childrens()) {
  1320. sum += index.getInt("_return");
  1321. }
  1322. return sum;
  1323. }
  1324. }
  1325. /**
  1326. * Gera inStrução de atribuicao
  1327. *
  1328. * @param n
  1329. * @throws Exception
  1330. */
  1331. protected void genAssign(Node n) throws Exception {
  1332. ArrayList<String> localAttribs = new ArrayList<>(),
  1333. // copymode = new ArrayList<>(),
  1334. // operators = new ArrayList<>(),
  1335. returns;
  1336. ArrayList<Boolean> copyaddress = new ArrayList<>();
  1337. String p1, declare = "" + n.getText().equals("dec.var.short");
  1338. Node dsts = n.childrens().get(0);
  1339. if (n.in("value", new String[]{"dec.var.short", "def.assign"})) {
  1340. for (Node attrib : n.childrens().get(1).childrens()) {
  1341. visit(attrib);
  1342. // System.out.println("n:" + n);
  1343. // System.out.println("genAssign:" + attrib.G("class") + ":" + attrib.G("value"));
  1344. if (attrib.eq("class", "call")) {
  1345. returns = attrib.getList("returns");
  1346. if (returns != null) {
  1347. for (String _return : returns) {
  1348. localAttribs.add(_return);
  1349. copyaddress.add(attrib.eq("subclass", "address"));
  1350. }
  1351. }
  1352. } else if (attrib.eq("class", "index")) {
  1353. // System.out.println("Attr <- :" + attrib);
  1354. } else if (attrib.eq("class", "literal")) {
  1355. // System.out.println("literal<- :" + attrib);
  1356. switch (attrib.G("subclass")) {
  1357. case "array":
  1358. for (Node element : attrib.find("exprs").childrens()) {
  1359. visit(element);
  1360. copyaddress.add(element.eq("subclass", "address"));
  1361. localAttribs.add(element.G("_return"));
  1362. }
  1363. break;
  1364. }
  1365. } else {
  1366. // System.out.println("Assign:" + attrib);
  1367. copyaddress.add(attrib.eq("subclass", "address"));
  1368. localAttribs.add(attrib.G("_return"));
  1369. }
  1370. }
  1371. }
  1372. // System.out.println("Copy::" + localAttribs);
  1373. // System.out.println("Lista de atribuicoes locais:\n" + dsts + "\n>>\n" + n);
  1374. // System.out.println("Lista de atribuicoes locais:\n" + dsts + "\n>>\n" + localAttribs);
  1375. // Executa a copia dos valores
  1376. for (int dstPosition = 0, i = 0; i < localAttribs.size(); dstPosition++) {
  1377. Node d = dsts.childrens().get(dstPosition);
  1378. d.S("declare", declare);
  1379. visit(d);
  1380. // System.out.println("D:" + d.childrens());
  1381. if (d.eq("class", "unary") && d.eq("dst.pointer", "*")) {
  1382. p1 = localAttribs.get(i);
  1383. code.Add(new Instruction()
  1384. .S("format", "pointer_assignment")
  1385. .S("type", "pointer_assignment")
  1386. .S("dst", d.childrens().get(1).G("_return"))
  1387. .S("p1", p1)
  1388. .S("p1value", "" + Api.IsValue(p1))
  1389. .S("dst.pointer", "*")
  1390. .set("locker", getIdMaster()));
  1391. i++;
  1392. // Salva o valor em memoria
  1393. } else if (d.eq("array", "true")) {
  1394. for (int j = 0; j < d.getInt("array_size"); j++, i++) {
  1395. // System.out.println("ARRAY:" + d.G("_return") + "[" + j + "]" + " -- " + localAttribs.get(i));
  1396. Copy(d.G("_return") + "[" + j + "]", localAttribs.get(i), copyaddress.get(i));
  1397. }
  1398. } else {
  1399. // Copia valor para endereco
  1400. // System.out.println("Attrib:" + d.G("_return") + "<" + localAttribs.get(i));
  1401. Copy(d.G("_return"), localAttribs.get(i), copyaddress.get(i));
  1402. i++;
  1403. }
  1404. }
  1405. }
  1406. protected void __genParam(String param) throws Exception {
  1407. Instruction r = new Instruction();
  1408. r.S("format", "push_param")
  1409. .S("type", "push_param")
  1410. .S("p1", param)
  1411. .S("p1value", "" + Api.IsValue(param))
  1412. .set("locker", getIdMaster());
  1413. code.Add(r);
  1414. }
  1415. protected void genFunction(String id, Node func) throws Exception {
  1416. if (!funcGenMap.containsKey(id)) {
  1417. funcGenMap.put(id, "true");
  1418. Node block = func.find("stmts");
  1419. code.OpenBlock(id);
  1420. /*Cria a PushLabel inicial da funcao*/
  1421. PushLabel(id, "block");
  1422. /*Declara as variaveis de parametro*/
  1423. genPopParams(func);
  1424. /*Declara as variaveis de retorno caso sejam nomeadas*/
  1425. // visit(func.find("def::return"));
  1426. // System.out.println(Variaveis.printScope(func.G("scope")));
  1427. /*Processa o bloco de instrucoes*/
  1428. visit(block);
  1429. /*Cria a PushLabel final da funcao*/
  1430. PushLabel(id + "-end", "block");
  1431. code.CloseBlock();
  1432. }
  1433. }
  1434. protected void genCall(Node n) throws Exception {
  1435. int argsNum = 0;
  1436. // Se for metodo carrega o contexto no primeiro parametro
  1437. // System.out.println("GenCall:" + n);
  1438. if (!n.eq("ctx", "")) {
  1439. Node var = Variaveis.Get(n.G("ctx"));
  1440. // System.out.println("");
  1441. // __genParam("&" + gerarVariavel(var));
  1442. // code.Add(new Instruction()
  1443. // .S("", PushLabel));
  1444. // gerarVariavel(var)
  1445. __genParam(gerarVariavel(var));
  1446. argsNum++;
  1447. }
  1448. // Gera os argumentos
  1449. Node args = n.find("arguments").find("exprs");
  1450. if (args != null) {
  1451. for (Node param : args.childrens()) {
  1452. // Processa o argumento para obter o _return
  1453. visit(param);
  1454. // Cria a instrução de parametro
  1455. __genParam(param.G("_return"));
  1456. }
  1457. argsNum += args.getChildrenCount();
  1458. }
  1459. // System.out.println("GenCall:" + n.getText());
  1460. Node def = Functions.Get(n.getText());
  1461. genFunction(n.getText(), def);
  1462. // Cria a instrução de chamada
  1463. Instruction r = __gerarCall(n.getText(), "" + argsNum, getIdMaster(), def.find("dec.return"));
  1464. // System.out.println("CALL -> " + r.getList("returns"));
  1465. n.addList("returns", r.getList("returns"));
  1466. }
  1467. protected String UnaryResolve(String op, String value) throws Exception {
  1468. switch (op) {
  1469. case "-":
  1470. return "-" + value;
  1471. case "!":
  1472. return "" + (value.equalsIgnoreCase("false") || value.equals("0") ? "1" : "0");
  1473. }
  1474. throw new Exception(String.format("Invalid [%s]%s ", op, value));
  1475. }
  1476. /*Fim dos laços*/
  1477. /*Inicio das operacoes aritimeticas*/
  1478. /**
  1479. * Unary pode Ser uma expreSSão do tipo <br>
  1480. * (&|*)addreSS - operacao de ponteiroS <br>
  1481. * (-) addreSS|value - negação aritimetica <br>
  1482. * (!) addreSS|value - negação booleana
  1483. *
  1484. * @param n
  1485. * @throws Exception
  1486. */
  1487. protected void unaryStmt(Node n) throws Exception {
  1488. String op = n.childrens().get(0).getText().trim();
  1489. Node value = n.childrens().get(1);
  1490. visit(value);
  1491. String _ret,
  1492. _val = value.G("_return");
  1493. // Se for escrita verifica se é escrita no ponteir ou no lugar referenciado
  1494. if (Api.IsValue(_val)) {
  1495. _ret = UnaryResolve(op, _val);
  1496. // } else {
  1497. } else if (n.eq("access", "write")) {
  1498. _ret = "ERR";
  1499. n.S("dst.pointer", op);
  1500. } else {
  1501. // System.out.println("N:[" + n.G("id") + "]");
  1502. _ret = gerarVariavel("T");
  1503. // System.out.println("Unary:" + n.childrens().G(1));
  1504. String type;
  1505. if (AddressOperator.matcher(op).matches()) {
  1506. type = "pointer_assignment";
  1507. } else {
  1508. type = "unary";
  1509. }
  1510. Instruction r = new Instruction()
  1511. .S("type", type)
  1512. .S("format", type)
  1513. .S("dst", _ret)
  1514. .S("p1", _val)
  1515. .S("op", op)
  1516. .S("copymode", "unary")
  1517. .S("p1value", "" + Api.IsValue(_val))
  1518. .set("locker", getIdMaster());
  1519. code.Add(r);
  1520. // System.out.println("N:" + n);
  1521. // throw new Exception("Unary Stmt not defined");
  1522. }
  1523. n.S("_return", _ret);
  1524. // System.out.println("UnaryStmt:[" + _val + ":" + op + "]" + n);
  1525. }
  1526. protected Integer _ExprResolve(String op, String p1, String p2) throws Exception {
  1527. int p1v = Integer.parseInt(p1),
  1528. p2v = Integer.parseInt(p2);
  1529. switch (op.substring(0, 1)) {
  1530. case "+":
  1531. return p1v + p2v;
  1532. case "-":
  1533. return p1v - p2v;
  1534. case "*":
  1535. return p1v * p2v;
  1536. case "/":
  1537. return p1v / p2v;
  1538. case "%":
  1539. return p1v % p2v;
  1540. }
  1541. return null;
  1542. }
  1543. protected Instruction __exp(String op, String p1, String p2, String ret) throws Exception {
  1544. boolean p1value = Api.IsValue(p1),
  1545. p2value = Api.IsValue(p2);
  1546. String unsigned = "false";
  1547. if (op.contains("u")) {
  1548. op = op.replace("u", "");
  1549. unsigned = "true";
  1550. }
  1551. Instruction r = new Instruction();
  1552. if (p1value && p2value) {
  1553. r.S("type", "copy")
  1554. .S("format", "copy")
  1555. .S("p1value", "true")
  1556. .S("p1", "" + _ExprResolve(op, p1, p2));
  1557. } else {
  1558. r.S("type", "assign")
  1559. .S("format", "assign")
  1560. .S("cat", "exp")
  1561. .S("p1", p1)
  1562. .S("p2", p2)
  1563. .S("copymode", "assign")
  1564. .S("p1value", "" + p1value)
  1565. .S("p2value", "" + p2value);
  1566. // System.out.println("__EXP:"+p1value+":"+p2value);
  1567. }
  1568. r.S("dst", ret)
  1569. .S("op", op)
  1570. .S("op.unsigned", unsigned)
  1571. .set("locker", getIdMaster());
  1572. return code.Add(r);
  1573. }
  1574. /**
  1575. * Gera Salto para inStruçõeS de continue e break;
  1576. *
  1577. * @param n
  1578. * @throws Exception
  1579. */
  1580. protected void ctrlStmt(Node n) throws Exception {
  1581. Node container = n.closest("for.stmt,switch.stmt", "class");
  1582. switch (n.getText()) {
  1583. case "continue":
  1584. String go;
  1585. if (container.eq("class", "for.stmt")) {
  1586. go = container.G("inc");
  1587. } else {
  1588. go = container.G("end");
  1589. }
  1590. _goto(go);
  1591. break;
  1592. case "break":
  1593. _goto(container.G("end"));
  1594. break;
  1595. case "goto":
  1596. _goto(n.G("label"));
  1597. break;
  1598. }
  1599. }
  1600. /**
  1601. * Executa a extração do codigo baSeado na aSt, e retorna um objeto
  1602. * CodigoTreSEnderecoS contendo oS blockS em Formato de treS endereçoS.
  1603. *
  1604. * @return
  1605. * @throws Exception
  1606. */
  1607. public Code getCodigoDeTresEnderecos() throws Exception {
  1608. return run();
  1609. }
  1610. /**
  1611. * Ativa ou deSativa a otimizacao
  1612. *
  1613. * @param lc
  1614. */
  1615. public void setOtimizacao(boolean lc) {
  1616. code.setOtimizacao(lc);
  1617. }
  1618. protected void setMaster(Node n) {
  1619. master.push(n);
  1620. }
  1621. protected Node getMaster() {
  1622. return master.peek();
  1623. }
  1624. protected void setNullMaster() {
  1625. master.pop();
  1626. }
  1627. protected int getIdMaster() throws Exception {
  1628. if (master.empty()) {
  1629. return 0;
  1630. }
  1631. return master.peek().getInt("id");
  1632. }
  1633. /**
  1634. * Adiciona aS declaraçõeS bem como carregaoS parametroS e referenciaS
  1635. *
  1636. * @param n
  1637. * @throws Exception
  1638. */
  1639. protected void genPopParams(Node func) throws Exception {
  1640. int reg = 0;
  1641. String destino, atribuido;
  1642. /**
  1643. * Se for method declara e atribui o endereco a variavel
  1644. */
  1645. Node receive = func.find("receive.type");
  1646. if (receive != null) {
  1647. // atribuido = "_A" + (reg++);
  1648. // destino = clearPointer(atribuido, gerarVariavel(receive.childrens().G(0)));
  1649. destino = gerarVariavel(receive.childrens().get(0));
  1650. // copy_pointer?
  1651. Copy(Api.clearID(destino), __popParam().G("_return"), false);
  1652. }
  1653. for (Node argument : func.find("arguments").childrens()) {
  1654. for (Node arg : argument.find("ids").childrens()) {
  1655. /**
  1656. * Gera uma instrução de copia do registrador a{i},i = [0-4],
  1657. * para o endereço de memoria dos parametros
  1658. */
  1659. // atribuido = "_Ra" + (reg++);
  1660. // atribuido = "_A" + (reg++);
  1661. // System.out.println("PopArg:" + arg);
  1662. // destino = clearPointer(atribuido, gerarVariavel(arg));
  1663. // copy_pointer?
  1664. Copy(Api.clearID(gerarVariavel(arg)), __popParam().G("_return"), false);
  1665. }
  1666. }
  1667. }
  1668. protected Instruction __popParam() throws Exception {
  1669. String varname = gerarVariavel("T");
  1670. Instruction r = new Instruction()
  1671. .S("type", "pop_param")
  1672. .S("format", "pop_param")
  1673. .S("p1", varname)
  1674. .S("p1value", "false")
  1675. .S("_return", varname);
  1676. r.set("locker", getIdMaster());
  1677. code.Add(r);
  1678. return r;
  1679. }
  1680. // protected void inicializarVariaveis() throws Exception {
  1681. //// System.out.println("INICIALIZAR VARIAVEIS:");
  1682. //
  1683. // for (Node container : ast.getRoot().childrens()) {
  1684. // for (Node constant : container.findAll("dec::const,dec::var", "class", 1)) {
  1685. // visit(constant);
  1686. // }
  1687. // }
  1688. // }
  1689. protected Instruction Copy(String dst, String src, boolean address) throws Exception {
  1690. // System.out.println("TAC:Copy:" + dst + "=" + src);
  1691. String type, format, arrayPattern = ".*\\[.*\\]";
  1692. Instruction r = new Instruction();
  1693. if (dst.matches(arrayPattern) || src.matches(arrayPattern)) {
  1694. type = "indexed_assign";
  1695. format = "copy";
  1696. if (Variaveis.isArray(src)) {
  1697. r.S("src_indexed", "true");
  1698. r.S("p1.indice", Api.getIndice(src));
  1699. }
  1700. if (Variaveis.isArray(dst)) {
  1701. r.S("dst_indexed", "true");
  1702. r.S("dst.indice", Api.getIndice(dst));
  1703. } else {
  1704. r.S("dst_indexed", "false");
  1705. }
  1706. if (Variaveis.isArray(src)) {
  1707. r.S("indice", Api.getIndice(src));
  1708. }
  1709. } else {
  1710. type = format = "copy";
  1711. }
  1712. // Identifica que o valor copiado é um endereco ou valor
  1713. if (address) {
  1714. r.S("src.address", "true");
  1715. }
  1716. r.S("format", format)
  1717. .S("type", type)
  1718. .S("dst", dst)
  1719. .S("p1", src)
  1720. .S("p1value", Api.IsValue(src) + "")
  1721. .set("locker", getIdMaster());
  1722. code.Add(r);
  1723. return r;
  1724. }
  1725. /**
  1726. * Gera um PuShLabel unico
  1727. *
  1728. * @param n
  1729. * @return
  1730. */
  1731. protected String gerarLabel(Node n) {
  1732. // return code.genLabel();
  1733. return code.getCurrentBlockName() + "+_i" + (incLabel++);
  1734. }
  1735. protected String gerarLabel() {
  1736. // return code.genLabel();
  1737. return code.getCurrentBlockName() + "+_i" + (incLabel++);
  1738. }
  1739. /**
  1740. * Cria uma linha de PuShLabel no block de codigo corrente
  1741. *
  1742. * @param label String
  1743. * @throws Exception
  1744. */
  1745. protected Instruction PushLabel(String label, String type) throws Exception {
  1746. Instruction r = new Instruction()
  1747. .S("label", label)
  1748. .S("type", "label")
  1749. .S("label_type", type)
  1750. .S("format", "label")
  1751. .set("locker", getIdMaster());
  1752. return code.Add(r);
  1753. }
  1754. /**
  1755. * Gera um nome de variavel unico,
  1756. *
  1757. * @param pre prefixo da variavel
  1758. * @param n
  1759. * @return
  1760. */
  1761. protected String gerarVariavel(String pre) {
  1762. return "_" + pre + (tmpVarCount++);
  1763. }
  1764. protected String gerarVariavel(Node n) throws Exception {
  1765. String id = n.getText();
  1766. if (!varGenMap.containsKey(id)) {
  1767. varGenMap.put(id, gerarVariavelSet(id, n));
  1768. }
  1769. return varGenMap.get(id);
  1770. }
  1771. protected String gerarVariavelSet(String id, Node n) throws Exception {
  1772. Node var = Variaveis.Get(id);
  1773. String varname, alias, vn;
  1774. if (var.eq("constant", "true")) {
  1775. varname = id;
  1776. } else {
  1777. varname = var.getText();
  1778. }
  1779. // System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
  1780. // if (var.eq("constant", "true")) {
  1781. // System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
  1782. // }
  1783. if (varGenMap.containsKey(varname)) {
  1784. alias = varGenMap.get(varname);
  1785. // System.out.printf("GerarVariavelSet[MAPPED]{%s,%s}\n", varname, alias);
  1786. } else {
  1787. String prefix;
  1788. // System.out.println("GerarVariavelSet\n" + varname + "\n" + id + "\n" + var);
  1789. if (var.eq("class", "dec.const")) {
  1790. prefix = "C";
  1791. } else if (var.eq("global", "true")) {
  1792. prefix = "G";
  1793. } else {
  1794. prefix = "V";
  1795. }
  1796. // System.out.println("DecVar:" + var);
  1797. // alias = var.G("pointer") + gerarVariavel(prefix);
  1798. alias = gerarVariavel(prefix);
  1799. n.S("alias", alias);
  1800. n.copy("pointer", var);
  1801. varGenMap.put(varname, alias);
  1802. // vn = var.G("scope");
  1803. int size = Tipos.Size(var.G("type")), b = 1;
  1804. // Se for um array considera os filhos como index
  1805. if (!var.eq("constant", "true") && var.childrens().size() > 0) {
  1806. for (Node node : var.childrens()) {
  1807. b *= node.getInt("value");
  1808. }
  1809. }
  1810. size *= b;
  1811. var.set("size", size);
  1812. // System.out.println("gerarVariave[" + prefix + "|" + id + "]:" + var);
  1813. (prefix.equals("G") ? code.GData() : code.Block().Data()).Add(alias, var);
  1814. }
  1815. return alias;
  1816. }
  1817. /**
  1818. * Imprime o codigo de treS endereçoS
  1819. *
  1820. * @return
  1821. */
  1822. @Override
  1823. public String toString() {
  1824. return printVarMap() + code.toString();
  1825. }
  1826. public String printVarMap() {
  1827. StringBuilder s = new StringBuilder();
  1828. for (Map.Entry<String, String> e : varGenMap.entrySet()) {
  1829. s.append(e.getKey() + " : " + e.getValue() + "\n");
  1830. }
  1831. System.out.println("Var Map:\n" + s.toString());
  1832. return varGenMap.toString();
  1833. }
  1834. /**
  1835. * Executa a extração da aSt para codigo de treS endereçoS
  1836. *
  1837. * @throws Exception
  1838. */
  1839. public void extrair() throws Exception {
  1840. if (!inicializado) {
  1841. inicializado = true;
  1842. }
  1843. // inicializarVariaveis();
  1844. // extrairTraps();
  1845. // extrairThreads();
  1846. // extractFunctions();
  1847. // extracMethods();
  1848. main();
  1849. }
  1850. protected Instruction __gerarCall(String funcname, String numparam, int idMaster, Node returns) throws Exception {
  1851. Instruction call = __gerarCall(funcname, numparam, idMaster);
  1852. // Atribuição dos enderecos de retorn
  1853. if (returns != null) {
  1854. ArrayList<String> rts = new ArrayList<>();
  1855. String t;
  1856. for (Node r : returns.childrens()) {
  1857. t = gerarVariavel("T");
  1858. rts.add(t);
  1859. code.Add(new Instruction()
  1860. .S("type", "pop_return")
  1861. .S("format", "pop_return")
  1862. .S("p1value", "false")
  1863. .S("p1", t));
  1864. }
  1865. call.addList("returns", rts);
  1866. }
  1867. return call;
  1868. }
  1869. /**
  1870. * Gera inStrução de call
  1871. *
  1872. * @param funcname
  1873. * @param numparam
  1874. * @param idMaster
  1875. * @param variavelRetorno
  1876. * @throws Exception
  1877. */
  1878. protected Instruction __gerarCall(String funcname, String numparam, int idMaster) throws Exception {
  1879. Instruction r = new Instruction();
  1880. r.S("format", "call")
  1881. .S("type", "call")
  1882. .S("funcname", funcname)
  1883. .S("nump", numparam).set("locker", idMaster);
  1884. // if (!variavelRetorno.equals("")) {
  1885. // r.S("dst", variavelRetorno);
  1886. // }
  1887. code.Add(r);
  1888. return r;
  1889. }
  1890. }
  1891. // case "||":
  1892. // and = false;
  1893. // // Não deve inverter operacao no caso de ||
  1894. // branckInvert.push(and);
  1895. //
  1896. // _or(n);
  1897. //
  1898. // Branch(x.get(0));
  1899. // Branch(x.get(1));
  1900. //
  1901. // // Inverte a ultima operacao
  1902. // if (n.parent.eq("value", "||")) {
  1903. // }
  1904. //
  1905. //// if (!loop && branckInvert.size() == 1) {
  1906. //// Instruction last = code.Last();
  1907. //// last.S("op", Complement(last.G("op"))).S("label", n.G("next"));
  1908. //// }
  1909. // if (!x.get(0).Has("and")) {
  1910. // n.remove("and");
  1911. // }
  1912. // branckInvert.pop();
  1913. //
  1914. // break;
  1915. // default:
  1916. //// boolean empty = branckInvert.isEmpty();
  1917. ////
  1918. //// if (empty) {
  1919. //// branckInvert.push(true);
  1920. //// }
  1921. //
  1922. // visit(n);
  1923. // // Resultado do test class ou acesso a um id ou bool
  1924. // if (!n.eq("class", "expr")) {
  1925. // op = "==";
  1926. // p0 = n.G("_return");
  1927. // p1 = "1";
  1928. // } else { //(<, <=, >, >=, ==, !=)
  1929. // p0 = visit(x.get(0)).G("_return");
  1930. // p1 = visit(x.get(1)).G("_return");
  1931. // }
  1932. //
  1933. // Instruction instruction = Branch(op, p0, p1, n.G("next"));
  1934. //// instruction. ;
  1935. //// if (n.Has("false")) {
  1936. // // _goto(n.G("false"));
  1937. // // }
  1938. // // if (empty) {
  1939. // // branckInvert.pop();
  1940. // // }
  1941. // @Override
  1942. // public TacGenInterface AfterCreate(CodeProcessing p) {
  1943. // this.afterCreate.add(p);
  1944. // return this;
  1945. // }
  1946. //
  1947. // public TacGenInterface AfterCreate() throws Exception {
  1948. // for (CodeProcessing x : afterCreate) {
  1949. // x.Exec(code);
  1950. // }
  1951. // return this;
  1952. // }