nwasmadd.pas 12 KB

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