nwasmflw.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. {
  2. Copyright (c) 2019 by Dmitry Boyarintsev
  3. Generate assembler for nodes that influence the flow for the JVM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nwasmflw;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmbase,node,nflw,ncgflw, cutils;
  22. type
  23. { twasmifnode }
  24. // Wasm doesn't have any jump(+offset) operations
  25. // It only provide structured blockes to handle jumps
  26. // (It's possible to jump-out-of-block at any time)
  27. // "If" is also implemented as a block, identical to high-level language.
  28. // Another thing to consider is "if" block also "returns" a value on the stack.
  29. // Such value should be substituteed (it's hard-coded to be type i32)
  30. twasmifnode = class(tcgifnode)
  31. public
  32. procedure pass_generate_code;override;
  33. end;
  34. { twasmwhilerepeatnode }
  35. twasmwhilerepeatnode = class(tcgwhilerepeatnode)
  36. public
  37. procedure pass_generate_code_condition;
  38. procedure pass_generate_code;override;
  39. end;
  40. implementation
  41. uses
  42. verbose,globals,systems,globtype,constexp,
  43. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,defcmp,
  44. procinfo,cgbase,pass_1,pass_2,parabase,
  45. cpubase,cpuinfo,
  46. nbas,nld,ncon,ncnv,
  47. tgobj,paramgr,
  48. cgutils,hlcgobj,hlcgcpu;
  49. { twasmwhilerepeatnode }
  50. procedure twasmwhilerepeatnode.pass_generate_code_condition;
  51. begin
  52. secondpass(left);
  53. // reversing the condition
  54. if not (lnf_checknegate in loopflags) then
  55. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_eqz));
  56. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
  57. end;
  58. procedure twasmwhilerepeatnode.pass_generate_code;
  59. var
  60. lcont,lbreak,lloop,
  61. oldclabel,oldblabel : tasmlabel;
  62. truelabel,falselabel : tasmlabel;
  63. oldflowcontrol : tflowcontrol;
  64. oldloopcontbroffset: Integer;
  65. oldloopbreakbroffset: Integer;
  66. begin
  67. location_reset(location,LOC_VOID,OS_NO);
  68. current_asmdata.getjumplabel(lloop);
  69. current_asmdata.getjumplabel(lcont);
  70. current_asmdata.getjumplabel(lbreak);
  71. oldflowcontrol:=flowcontrol;
  72. oldloopcontbroffset:=thlcgwasm(hlcg).loopContBr;
  73. oldloopbreakbroffset:=thlcgwasm(hlcg).loopBreakBr;
  74. oldclabel:=current_procinfo.CurrContinueLabel;
  75. oldblabel:=current_procinfo.CurrBreakLabel;
  76. include(flowcontrol,fc_inflowcontrol);
  77. exclude(flowcontrol,fc_unwind_loop);
  78. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  79. thlcgwasm(hlcg).incblock;
  80. thlcgwasm(hlcg).loopBreakBr:=thlcgwasm(hlcg).br_blocks;
  81. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
  82. thlcgwasm(hlcg).incblock;
  83. if lnf_testatbegin in loopflags then
  84. begin
  85. pass_generate_code_condition;
  86. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks;
  87. end else
  88. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks+1;
  89. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  90. thlcgwasm(hlcg).incblock;
  91. current_procinfo.CurrContinueLabel:=lcont;
  92. current_procinfo.CurrBreakLabel:=lbreak;
  93. secondpass(right);
  94. if (lnf_testatbegin in loopflags) then
  95. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
  96. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  97. thlcgwasm(hlcg).decblock;
  98. if not (lnf_testatbegin in loopflags) then begin
  99. pass_generate_code_condition;
  100. end;
  101. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
  102. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_loop));
  103. thlcgwasm(hlcg).decblock;
  104. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  105. thlcgwasm(hlcg).decblock;
  106. current_procinfo.CurrContinueLabel:=oldclabel;
  107. current_procinfo.CurrBreakLabel:=oldblabel;
  108. thlcgwasm(hlcg).loopContBr:=oldloopcontbroffset;
  109. thlcgwasm(hlcg).loopBreakBr:=oldloopbreakbroffset;
  110. { a break/continue in a while/repeat block can't be seen outside }
  111. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  112. end;
  113. { twasmifnode }
  114. procedure twasmifnode.pass_generate_code;
  115. var
  116. oldflowcontrol: tflowcontrol;
  117. begin
  118. // left - condition
  119. // right - then
  120. // t1 - else (optional)
  121. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  122. secondpass(left); // condition exprssions
  123. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  124. thlcgwasm(hlcg).incblock;
  125. secondpass(right); // then branchs
  126. if Assigned(t1) then // else branch
  127. begin
  128. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  129. secondpass(t1);
  130. end;
  131. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  132. thlcgwasm(hlcg).decblock;
  133. end;
  134. initialization
  135. //cfornode:=tjvmfornode;
  136. //craisenode:=tjvmraisenode;
  137. //ctryexceptnode:=tjvmtryexceptnode;
  138. //ctryfinallynode:=tjvmtryfinallynode;
  139. //connode:=tjvmonnode;
  140. cifnode:=twasmifnode;
  141. cwhilerepeatnode:=twasmwhilerepeatnode;
  142. end.