nwasmadd.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. {
  2. Copyright (c) 2019 by Dmitry Boyarintsev based on JVM by Jonas Maebe
  3. Code generation for add nodes on the WebAssembly
  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 nwasmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,
  22. node,ncgadd,cpubase, globals, pass_2;
  23. type
  24. { twasmaddnode }
  25. twasmaddnode = class(tcgaddnode)
  26. protected
  27. procedure second_generic_compare(unsigned: boolean);
  28. procedure pass_left_right;override;
  29. procedure second_addfloat;override;
  30. procedure second_cmpfloat;override;
  31. procedure second_cmpboolean;override;
  32. procedure second_cmp64bit;override;
  33. procedure second_add64bit; override;
  34. procedure second_cmpordinal;override;
  35. // special treatement for short-boolean expressions
  36. // using IF block, instead of direct labels
  37. procedure second_addboolean; override;
  38. end;
  39. implementation
  40. uses
  41. systems,
  42. cutils,verbose,constexp,globtype,compinnr,
  43. symconst,symtable,symdef,symcpu,
  44. paramgr,procinfo,pass_1,
  45. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,
  46. hlcgobj,hlcgcpu,cgutils,
  47. cpupara,
  48. nbas,ncon,nset,nadd,ncal,ncnv,ninl,nld,nmat,nmem,
  49. //njvmcon,
  50. cgobj, symtype, tgobj;
  51. {*****************************************************************************
  52. tjvmaddnode
  53. *****************************************************************************}
  54. procedure twasmaddnode.pass_left_right;
  55. begin
  56. //if not((nodetype in [orn,andn]) and
  57. // is_boolean(left.resultdef)) then
  58. // swapleftright;
  59. inherited pass_left_right;
  60. end;
  61. procedure twasmaddnode.second_addfloat;
  62. var
  63. op : TAsmOp;
  64. commutative : boolean;
  65. begin
  66. pass_left_right;
  67. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  68. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  69. commutative:=false;
  70. case nodetype of
  71. addn :
  72. begin
  73. if location.size=OS_F64 then
  74. op:=a_f64_add
  75. else
  76. op:=a_f32_add;
  77. commutative:=true;
  78. end;
  79. muln :
  80. begin
  81. if location.size=OS_F64 then
  82. op:=a_f64_mul
  83. else
  84. op:=a_f32_mul;
  85. commutative:=true;
  86. end;
  87. subn :
  88. begin
  89. if location.size=OS_F64 then
  90. op:=a_f64_sub
  91. else
  92. op:=a_f32_sub;
  93. end;
  94. slashn :
  95. begin
  96. if location.size=OS_F64 then
  97. op:=a_f64_div
  98. else
  99. op:=a_f32_div;
  100. end;
  101. else
  102. internalerror(2011010402);
  103. end;
  104. { swap the operands to make it easier for the optimizer to optimize
  105. the operand stack slot reloading (non-commutative operations must
  106. always be in the correct order though) }
  107. if (commutative and
  108. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  109. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  110. (not commutative and
  111. (nf_swapped in flags)) then
  112. swapleftright;
  113. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  114. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  115. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  116. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  117. { could be optimized in the future by keeping the results on the stack,
  118. if we add code to swap the operands when necessary (a_swap for
  119. singles, store/load/load for doubles since there is no swap for
  120. 2-slot elements -- also adjust expectloc in that case! }
  121. thlcgwasm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  122. end;
  123. procedure twasmaddnode.second_cmpfloat;
  124. var
  125. op : TAsmOp;
  126. commutative : boolean;
  127. cmpResultType : tdef;
  128. begin
  129. cmpResultType := s32inttype;
  130. pass_left_right;
  131. commutative:=false;
  132. case nodetype of
  133. ltn :
  134. begin
  135. if location.size=OS_F64 then
  136. op:=a_f64_lt
  137. else
  138. op:=a_f32_lt;
  139. end;
  140. lten :
  141. begin
  142. if location.size=OS_F64 then
  143. op:=a_f64_le
  144. else
  145. op:=a_f32_le;
  146. end;
  147. gtn :
  148. begin
  149. if location.size=OS_F64 then
  150. op:=a_f64_gt
  151. else
  152. op:=a_f32_gt;
  153. end;
  154. gten :
  155. begin
  156. if location.size=OS_F64 then
  157. op:=a_f64_ge
  158. else
  159. op:=a_f32_ge;
  160. end;
  161. equaln :
  162. begin
  163. if location.size=OS_F64 then
  164. op:=a_f64_eq
  165. else
  166. op:=a_f32_eq;
  167. commutative:=true;
  168. end;
  169. unequaln :
  170. begin
  171. if location.size=OS_F64 then
  172. op:=a_f64_ne
  173. else
  174. op:=a_f32_ne;
  175. commutative:=true;
  176. end;
  177. else
  178. internalerror(2011010402);
  179. end;
  180. { swap the operands to make it easier for the optimizer to optimize
  181. the operand stack slot reloading (non-commutative operations must
  182. always be in the correct order though) }
  183. if (commutative and
  184. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  185. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  186. (not commutative and
  187. (nf_swapped in flags)) then
  188. swapleftright;
  189. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  190. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  191. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  192. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  193. { could be optimized in the future by keeping the results on the stack,
  194. if we add code to swap the operands when necessary (a_swap for
  195. singles, store/load/load for doubles since there is no swap for
  196. 2-slot elements -- also adjust expectloc in that case! }
  197. set_result_location_reg;
  198. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  199. end;
  200. procedure twasmaddnode.second_cmpboolean;
  201. begin
  202. second_generic_compare(true);
  203. end;
  204. procedure twasmaddnode.second_cmp64bit;
  205. begin
  206. second_generic_compare(not is_signed(left.resultdef));
  207. end;
  208. procedure twasmaddnode.second_add64bit;
  209. begin
  210. second_opordinal;
  211. end;
  212. procedure twasmaddnode.second_cmpordinal;
  213. begin
  214. second_generic_compare(not is_signed(left.resultdef));
  215. end;
  216. procedure twasmaddnode.second_addboolean;
  217. begin
  218. if (nodetype in [orn,andn]) and
  219. (not(cs_full_boolean_eval in current_settings.localswitches) or
  220. (nf_short_bool in flags)) then
  221. begin
  222. secondpass(left);
  223. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  224. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  225. thlcgwasm(hlcg).incblock;
  226. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  227. case nodetype of
  228. andn :
  229. begin
  230. // inside of IF (the condition evaluated as true)
  231. // for "and" must evaluate the right section
  232. secondpass(right);
  233. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  234. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  235. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  236. // inside of ELSE (the condition evaluated as false)
  237. // for "and" must end evaluation immediately
  238. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  239. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  240. end;
  241. orn :
  242. begin
  243. // inside of IF (the condition evaluated as true)
  244. // for "or" must end evalaution immediately - satified!
  245. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 1) );
  246. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  247. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  248. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  249. // inside of ELSE (the condition evaluated as false)
  250. // for "or" must evaluate the right part
  251. secondpass(right);
  252. // inside of ELSE (the condition evaluated as false)
  253. // for "and" must end evaluation immediately
  254. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  255. end;
  256. else
  257. Internalerror(2019091902);
  258. end;
  259. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  260. thlcgwasm(hlcg).decblock;
  261. set_result_location_reg;
  262. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  263. end else
  264. inherited;
  265. end;
  266. procedure twasmaddnode.second_generic_compare(unsigned: boolean);
  267. var
  268. truelabel,
  269. falselabel: tasmlabel;
  270. cmpop: TOpCmp;
  271. begin
  272. truelabel:=nil;
  273. falselabel:=nil;
  274. pass_left_right;
  275. { swap the operands to make it easier for the optimizer to optimize
  276. the operand stack slot reloading in case both are in a register }
  277. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  278. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  279. swapleftright;
  280. cmpop:=cmpnode2topcmp(unsigned);
  281. if (nf_swapped in flags) then
  282. cmpop:=swap_opcmp(cmpop);
  283. // must generate those labels...
  284. current_asmdata.getjumplabel(truelabel);
  285. current_asmdata.getjumplabel(falselabel);
  286. location_reset_jump(location,truelabel,falselabel);
  287. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  288. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,location.truelabel)
  289. else case right.location.loc of
  290. LOC_REGISTER,LOC_CREGISTER:
  291. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,location.truelabel);
  292. LOC_REFERENCE,LOC_CREFERENCE:
  293. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,location.truelabel);
  294. LOC_CONSTANT:
  295. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,location.truelabel);
  296. else
  297. internalerror(2011010413);
  298. end;
  299. set_result_location_reg;
  300. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  301. end;
  302. begin
  303. caddnode:=twasmaddnode;
  304. end.