BaseTacGen.java 80 KB

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