nwasmflw.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. twasmifnode = class(tcgifnode)
  29. public
  30. procedure pass_generate_code;override;
  31. end;
  32. { twasmwhilerepeatnode }
  33. twasmwhilerepeatnode = class(tcgwhilerepeatnode)
  34. public
  35. procedure pass_generate_code_condition;
  36. procedure pass_generate_code;override;
  37. end;
  38. { twasmtryexceptnode }
  39. twasmtryexceptnode = class(tcgtryexceptnode)
  40. public
  41. procedure pass_generate_code;override;
  42. end;
  43. { twasmtryfinallynode }
  44. twasmtryfinallynode = class(tcgtryfinallynode)
  45. public
  46. procedure pass_generate_code;override;
  47. end;
  48. implementation
  49. uses
  50. verbose,globals,systems,globtype,constexp,
  51. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,defcmp,
  52. procinfo,cgbase,pass_1,pass_2,parabase,
  53. cpubase,cpuinfo,
  54. nbas,nld,ncon,ncnv,
  55. tgobj,paramgr,
  56. cgutils,hlcgobj,hlcgcpu;
  57. {*****************************************************************************
  58. twasmwhilerepeatnode
  59. *****************************************************************************}
  60. procedure twasmwhilerepeatnode.pass_generate_code_condition;
  61. begin
  62. secondpass(left);
  63. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  64. // reversing the condition
  65. if not (lnf_checknegate in loopflags) then
  66. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_eqz));
  67. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
  68. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  69. end;
  70. procedure twasmwhilerepeatnode.pass_generate_code;
  71. var
  72. lcont,lbreak,lloop,
  73. oldclabel,oldblabel : tasmlabel;
  74. truelabel,falselabel : tasmlabel;
  75. oldflowcontrol : tflowcontrol;
  76. oldloopcontbroffset: Integer;
  77. oldloopbreakbroffset: Integer;
  78. begin
  79. location_reset(location,LOC_VOID,OS_NO);
  80. current_asmdata.getjumplabel(lloop);
  81. current_asmdata.getjumplabel(lcont);
  82. current_asmdata.getjumplabel(lbreak);
  83. oldflowcontrol:=flowcontrol;
  84. oldloopcontbroffset:=thlcgwasm(hlcg).loopContBr;
  85. oldloopbreakbroffset:=thlcgwasm(hlcg).loopBreakBr;
  86. oldclabel:=current_procinfo.CurrContinueLabel;
  87. oldblabel:=current_procinfo.CurrBreakLabel;
  88. include(flowcontrol,fc_inflowcontrol);
  89. exclude(flowcontrol,fc_unwind_loop);
  90. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  91. thlcgwasm(hlcg).incblock;
  92. thlcgwasm(hlcg).loopBreakBr:=thlcgwasm(hlcg).br_blocks;
  93. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
  94. thlcgwasm(hlcg).incblock;
  95. if lnf_testatbegin in loopflags then
  96. begin
  97. pass_generate_code_condition;
  98. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks;
  99. end else
  100. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks+1;
  101. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  102. thlcgwasm(hlcg).incblock;
  103. current_procinfo.CurrContinueLabel:=lcont;
  104. current_procinfo.CurrBreakLabel:=lbreak;
  105. secondpass(right);
  106. if (lnf_testatbegin in loopflags) then
  107. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
  108. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  109. thlcgwasm(hlcg).decblock;
  110. if not (lnf_testatbegin in loopflags) then begin
  111. pass_generate_code_condition;
  112. end;
  113. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
  114. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_loop));
  115. thlcgwasm(hlcg).decblock;
  116. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  117. thlcgwasm(hlcg).decblock;
  118. current_procinfo.CurrContinueLabel:=oldclabel;
  119. current_procinfo.CurrBreakLabel:=oldblabel;
  120. thlcgwasm(hlcg).loopContBr:=oldloopcontbroffset;
  121. thlcgwasm(hlcg).loopBreakBr:=oldloopbreakbroffset;
  122. { a break/continue in a while/repeat block can't be seen outside }
  123. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  124. end;
  125. {*****************************************************************************
  126. twasmifnode
  127. *****************************************************************************}
  128. procedure twasmifnode.pass_generate_code;
  129. var
  130. oldflowcontrol: tflowcontrol;
  131. begin
  132. // left - condition
  133. // right - then
  134. // t1 - else (optional)
  135. location_reset(location,LOC_VOID,OS_NO);
  136. oldflowcontrol := flowcontrol;
  137. include(flowcontrol,fc_inflowcontrol);
  138. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  139. secondpass(left); // condition exprssions
  140. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  141. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  142. thlcgwasm(hlcg).incblock;
  143. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  144. if Assigned(right) then
  145. secondpass(right); // then branchs
  146. if Assigned(t1) then // else branch
  147. begin
  148. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  149. secondpass(t1);
  150. end;
  151. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  152. thlcgwasm(hlcg).decblock;
  153. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  154. end;
  155. {*****************************************************************************
  156. twasmtryexceptnode
  157. *****************************************************************************}
  158. procedure twasmtryexceptnode.pass_generate_code;
  159. begin
  160. location_reset(location,LOC_VOID,OS_NO);
  161. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..except, try')));
  162. secondpass(left);
  163. //if codegenerror then
  164. // goto errorexit;
  165. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..except, end')));
  166. end;
  167. {*****************************************************************************
  168. twasmtryfinallynode
  169. *****************************************************************************}
  170. procedure twasmtryfinallynode.pass_generate_code;
  171. begin
  172. location_reset(location,LOC_VOID,OS_NO);
  173. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..finally, try')));
  174. { try code }
  175. if assigned(left) then
  176. begin
  177. secondpass(left);
  178. if codegenerror then
  179. exit;
  180. end;
  181. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..finally, finally')));
  182. { finally code (don't unconditionally set fc_inflowcontrol, since the
  183. finally code is unconditionally executed; we do have to filter out
  184. flags regarding break/contrinue/etc. because we have to give an
  185. error in case one of those is used in the finally-code }
  186. //flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  187. secondpass(right);
  188. { goto is allowed if it stays inside the finally block,
  189. this is checked using the exception block number }
  190. //if (flowcontrol-[fc_gotolabel])<>(finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions]) then
  191. // CGMessage(cg_e_control_flow_outside_finally);
  192. if codegenerror then
  193. exit;
  194. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..finally, end')));
  195. end;
  196. initialization
  197. cifnode:=twasmifnode;
  198. cwhilerepeatnode:=twasmwhilerepeatnode;
  199. ctryexceptnode:=twasmtryexceptnode;
  200. ctryfinallynode:=twasmtryfinallynode;
  201. end.