nwasmflw.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // todo: there should be a better approach
  55. if not (lnf_checknegate in loopflags) then begin
  56. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const,1) );
  57. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_xor) );
  58. end;
  59. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
  60. end;
  61. procedure twasmwhilerepeatnode.pass_generate_code;
  62. var
  63. lcont,lbreak,lloop,
  64. oldclabel,oldblabel : tasmlabel;
  65. truelabel,falselabel : tasmlabel;
  66. oldflowcontrol : tflowcontrol;
  67. oldloopbroffset: 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. oldloopbroffset:=thlcgwasm(hlcg).loopContBr;
  75. oldclabel:=current_procinfo.CurrContinueLabel;
  76. oldblabel:=current_procinfo.CurrBreakLabel;
  77. include(flowcontrol,fc_inflowcontrol);
  78. exclude(flowcontrol,fc_unwind_loop);
  79. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  80. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
  81. if lnf_testatbegin in loopflags then
  82. begin
  83. pass_generate_code_condition;
  84. thlcgwasm(hlcg).loopContBr:=1;
  85. end else
  86. thlcgwasm(hlcg).loopContBr:=0;
  87. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  88. current_procinfo.CurrContinueLabel:=lcont;
  89. current_procinfo.CurrBreakLabel:=lbreak;
  90. secondpass(right);
  91. if (lnf_testatbegin in loopflags) then
  92. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
  93. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  94. if not (lnf_testatbegin in loopflags) then begin
  95. pass_generate_code_condition;
  96. end;
  97. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
  98. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  99. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  100. current_procinfo.CurrContinueLabel:=oldclabel;
  101. current_procinfo.CurrBreakLabel:=oldblabel;
  102. thlcgwasm(hlcg).loopContBr:=oldloopbroffset;
  103. { a break/continue in a while/repeat block can't be seen outside }
  104. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  105. end;
  106. { twasmifnode }
  107. procedure twasmifnode.pass_generate_code;
  108. var
  109. oldflowcontrol: tflowcontrol;
  110. begin
  111. // left - condition
  112. // right - then
  113. // t1 - else (optional)
  114. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  115. secondpass(left); // condition exprssions
  116. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if)); // IF
  117. thlcgwasm(hlcg).incblock;
  118. secondpass(right); // then branchs
  119. if Assigned(t1) then // else branch
  120. begin
  121. // 0 const on stack if used to return IF value
  122. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 1));
  123. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  124. secondpass(t1);
  125. end
  126. else // else dummy-branch
  127. begin
  128. // dummy else branch! todo: to be removed, when it's decided
  129. // how to handle typeless-IF instructions (If without else)
  130. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  131. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  132. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_nop));
  133. end;
  134. // 0 const on stack if used to return IF value
  135. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  136. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  137. thlcgwasm(hlcg).decblock;
  138. // clearing IF return value
  139. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_drop));
  140. end;
  141. initialization
  142. //cfornode:=tjvmfornode;
  143. //craisenode:=tjvmraisenode;
  144. //ctryexceptnode:=tjvmtryexceptnode;
  145. //ctryfinallynode:=tjvmtryfinallynode;
  146. //connode:=tjvmonnode;
  147. cifnode:=twasmifnode;
  148. cwhilerepeatnode:=twasmwhilerepeatnode;
  149. end.