nwasmflw.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. { twasmraisenode }
  39. twasmraisenode = class(tcgraisenode)
  40. public
  41. function pass_1 : tnode;override;
  42. end;
  43. { twasmtryexceptnode }
  44. twasmtryexceptnode = class(tcgtryexceptnode)
  45. public
  46. procedure pass_generate_code;override;
  47. end;
  48. { twasmtryfinallynode }
  49. twasmtryfinallynode = class(tcgtryfinallynode)
  50. public
  51. procedure pass_generate_code;override;
  52. end;
  53. implementation
  54. uses
  55. verbose,globals,systems,globtype,constexp,
  56. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,defcmp,
  57. procinfo,cgbase,pass_1,pass_2,parabase,compinnr,
  58. cpubase,cpuinfo,
  59. nbas,nld,ncon,ncnv,ncal,ninl,nmem,nadd,
  60. tgobj,paramgr,
  61. cgutils,hlcgobj,hlcgcpu;
  62. {*****************************************************************************
  63. twasmwhilerepeatnode
  64. *****************************************************************************}
  65. procedure twasmwhilerepeatnode.pass_generate_code_condition;
  66. begin
  67. secondpass(left);
  68. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  69. // reversing the condition
  70. if not (lnf_checknegate in loopflags) then
  71. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_eqz));
  72. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
  73. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  74. end;
  75. procedure twasmwhilerepeatnode.pass_generate_code;
  76. var
  77. lcont,lbreak,lloop,
  78. oldclabel,oldblabel : tasmlabel;
  79. truelabel,falselabel : tasmlabel;
  80. oldflowcontrol : tflowcontrol;
  81. oldloopcontbroffset: Integer;
  82. oldloopbreakbroffset: Integer;
  83. begin
  84. location_reset(location,LOC_VOID,OS_NO);
  85. current_asmdata.getjumplabel(lloop);
  86. current_asmdata.getjumplabel(lcont);
  87. current_asmdata.getjumplabel(lbreak);
  88. oldflowcontrol:=flowcontrol;
  89. oldloopcontbroffset:=thlcgwasm(hlcg).loopContBr;
  90. oldloopbreakbroffset:=thlcgwasm(hlcg).loopBreakBr;
  91. oldclabel:=current_procinfo.CurrContinueLabel;
  92. oldblabel:=current_procinfo.CurrBreakLabel;
  93. include(flowcontrol,fc_inflowcontrol);
  94. exclude(flowcontrol,fc_unwind_loop);
  95. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  96. thlcgwasm(hlcg).incblock;
  97. thlcgwasm(hlcg).loopBreakBr:=thlcgwasm(hlcg).br_blocks;
  98. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
  99. thlcgwasm(hlcg).incblock;
  100. if lnf_testatbegin in loopflags then
  101. begin
  102. pass_generate_code_condition;
  103. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks;
  104. end else
  105. thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks+1;
  106. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  107. thlcgwasm(hlcg).incblock;
  108. current_procinfo.CurrContinueLabel:=lcont;
  109. current_procinfo.CurrBreakLabel:=lbreak;
  110. secondpass(right);
  111. if (lnf_testatbegin in loopflags) then
  112. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
  113. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  114. thlcgwasm(hlcg).decblock;
  115. if not (lnf_testatbegin in loopflags) then begin
  116. pass_generate_code_condition;
  117. end;
  118. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
  119. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_loop));
  120. thlcgwasm(hlcg).decblock;
  121. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  122. thlcgwasm(hlcg).decblock;
  123. current_procinfo.CurrContinueLabel:=oldclabel;
  124. current_procinfo.CurrBreakLabel:=oldblabel;
  125. thlcgwasm(hlcg).loopContBr:=oldloopcontbroffset;
  126. thlcgwasm(hlcg).loopBreakBr:=oldloopbreakbroffset;
  127. { a break/continue in a while/repeat block can't be seen outside }
  128. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  129. end;
  130. {*****************************************************************************
  131. twasmifnode
  132. *****************************************************************************}
  133. procedure twasmifnode.pass_generate_code;
  134. var
  135. oldflowcontrol: tflowcontrol;
  136. begin
  137. // left - condition
  138. // right - then
  139. // t1 - else (optional)
  140. location_reset(location,LOC_VOID,OS_NO);
  141. oldflowcontrol := flowcontrol;
  142. include(flowcontrol,fc_inflowcontrol);
  143. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  144. secondpass(left); // condition exprssions
  145. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  146. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  147. thlcgwasm(hlcg).incblock;
  148. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  149. if Assigned(right) then
  150. secondpass(right); // then branchs
  151. if Assigned(t1) then // else branch
  152. begin
  153. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  154. secondpass(t1);
  155. end;
  156. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  157. thlcgwasm(hlcg).decblock;
  158. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  159. end;
  160. {*****************************************************************************
  161. twasmraisenode
  162. *****************************************************************************}
  163. function twasmraisenode.pass_1 : tnode;
  164. var
  165. statements : tstatementnode;
  166. //current_addr : tlabelnode;
  167. raisenode : tcallnode;
  168. begin
  169. result:=internalstatements(statements);
  170. if assigned(left) then
  171. begin
  172. { first para must be a class }
  173. firstpass(left);
  174. { insert needed typeconvs for addr,frame }
  175. if assigned(right) then
  176. begin
  177. { addr }
  178. firstpass(right);
  179. { frame }
  180. if assigned(third) then
  181. firstpass(third)
  182. else
  183. third:=cpointerconstnode.Create(0,voidpointertype);
  184. end
  185. else
  186. begin
  187. third:=cinlinenode.create(in_get_frame,false,nil);
  188. //current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  189. //addstatement(statements,current_addr);
  190. //right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  191. right:=cnilnode.create;
  192. { raise address off by one so we are for sure inside the action area for the raise }
  193. if tf_use_psabieh in target_info.flags then
  194. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  195. end;
  196. raisenode:=ccallnode.createintern('fpc_raiseexception',
  197. ccallparanode.create(third,
  198. ccallparanode.create(right,
  199. ccallparanode.create(left,nil)))
  200. );
  201. include(raisenode.callnodeflags,cnf_call_never_returns);
  202. addstatement(statements,raisenode);
  203. end
  204. else
  205. begin
  206. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  207. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  208. include(raisenode.callnodeflags,cnf_call_never_returns);
  209. addstatement(statements,raisenode);
  210. end;
  211. left:=nil;
  212. right:=nil;
  213. third:=nil;
  214. end;
  215. {*****************************************************************************
  216. twasmtryexceptnode
  217. *****************************************************************************}
  218. procedure twasmtryexceptnode.pass_generate_code;
  219. begin
  220. location_reset(location,LOC_VOID,OS_NO);
  221. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..except, try')));
  222. secondpass(left);
  223. //if codegenerror then
  224. // goto errorexit;
  225. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..except, end')));
  226. end;
  227. {*****************************************************************************
  228. twasmtryfinallynode
  229. *****************************************************************************}
  230. procedure twasmtryfinallynode.pass_generate_code;
  231. begin
  232. location_reset(location,LOC_VOID,OS_NO);
  233. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..finally, try')));
  234. { try code }
  235. if assigned(left) then
  236. begin
  237. secondpass(left);
  238. if codegenerror then
  239. exit;
  240. end;
  241. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..finally, finally')));
  242. { finally code (don't unconditionally set fc_inflowcontrol, since the
  243. finally code is unconditionally executed; we do have to filter out
  244. flags regarding break/contrinue/etc. because we have to give an
  245. error in case one of those is used in the finally-code }
  246. //flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  247. secondpass(right);
  248. { goto is allowed if it stays inside the finally block,
  249. this is checked using the exception block number }
  250. //if (flowcontrol-[fc_gotolabel])<>(finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions]) then
  251. // CGMessage(cg_e_control_flow_outside_finally);
  252. if codegenerror then
  253. exit;
  254. current_asmdata.CurrAsmList.concat(tai_comment.Create(strpnew('TODO: try..finally, end')));
  255. end;
  256. initialization
  257. cifnode:=twasmifnode;
  258. cwhilerepeatnode:=twasmwhilerepeatnode;
  259. craisenode:=twasmraisenode;
  260. ctryexceptnode:=twasmtryexceptnode;
  261. ctryfinallynode:=twasmtryfinallynode;
  262. end.