nwasmflw.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  54. // reversing the condition
  55. if not (lnf_checknegate in loopflags) then
  56. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_eqz));
  57. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
  58. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  59. end;
  60. procedure twasmwhilerepeatnode.pass_generate_code;
  61. var
  62. lcont,lbreak,lloop,
  63. oldclabel,oldblabel : tasmlabel;
  64. truelabel,falselabel : tasmlabel;
  65. oldflowcontrol : tflowcontrol;
  66. oldloopcontbroffset: Integer;
  67. oldloopbreakbroffset: Integer;
  68. begin
  69. location_reset(location,LOC_VOID,OS_NO);
  70. current_asmdata.getjumplabel(lloop);
  71. current_asmdata.getjumplabel(lcont);
  72. current_asmdata.getjumplabel(lbreak);
  73. oldflowcontrol:=flowcontrol;
  74. oldloopcontbroffset:=thlcgwasm(hlcg).loopContBr;
  75. oldloopbreakbroffset:=thlcgwasm(hlcg).loopBreakBr;
  76. oldclabel:=current_procinfo.CurrContinueLabel;
  77. oldblabel:=current_procinfo.CurrBreakLabel;
  78. include(flowcontrol,fc_inflowcontrol);
  79. exclude(flowcontrol,fc_unwind_loop);
  80. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  81. thlcgwasm(hlcg).incblock;
  82. thlcgwasm(hlcg).loopBreakBr:=thlcgwasm(hlcg).br_blocks;
  83. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
  84. thlcgwasm(hlcg).incblock;
  85. if lnf_testatbegin in loopflags then
  86. begin
  87. pass_generate_code_condition;
  88. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks;
  89. end else
  90. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks+1;
  91. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  92. thlcgwasm(hlcg).incblock;
  93. current_procinfo.CurrContinueLabel:=lcont;
  94. current_procinfo.CurrBreakLabel:=lbreak;
  95. secondpass(right);
  96. if (lnf_testatbegin in loopflags) then
  97. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
  98. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  99. thlcgwasm(hlcg).decblock;
  100. if not (lnf_testatbegin in loopflags) then begin
  101. pass_generate_code_condition;
  102. end;
  103. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
  104. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_loop));
  105. thlcgwasm(hlcg).decblock;
  106. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  107. thlcgwasm(hlcg).decblock;
  108. current_procinfo.CurrContinueLabel:=oldclabel;
  109. current_procinfo.CurrBreakLabel:=oldblabel;
  110. thlcgwasm(hlcg).loopContBr:=oldloopcontbroffset;
  111. thlcgwasm(hlcg).loopBreakBr:=oldloopbreakbroffset;
  112. { a break/continue in a while/repeat block can't be seen outside }
  113. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  114. end;
  115. { twasmifnode }
  116. procedure twasmifnode.pass_generate_code;
  117. var
  118. oldflowcontrol: tflowcontrol;
  119. begin
  120. // left - condition
  121. // right - then
  122. // t1 - else (optional)
  123. location_reset(location,LOC_VOID,OS_NO);
  124. oldflowcontrol := flowcontrol;
  125. include(flowcontrol,fc_inflowcontrol);
  126. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  127. secondpass(left); // condition exprssions
  128. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  129. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  130. thlcgwasm(hlcg).incblock;
  131. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  132. if Assigned(right) then
  133. secondpass(right); // then branchs
  134. if Assigned(t1) then // else branch
  135. begin
  136. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  137. secondpass(t1);
  138. end;
  139. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  140. thlcgwasm(hlcg).decblock;
  141. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  142. end;
  143. initialization
  144. cifnode:=twasmifnode;
  145. cwhilerepeatnode:=twasmwhilerepeatnode;
  146. end.