nwasmadd.pas 14 KB

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