nwasmadd.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // allocating temporary variable (via reference) to hold the result
  132. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1,[]);
  133. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,0,tt_normal,location.reference);
  134. commutative:=false;
  135. case nodetype of
  136. ltn :
  137. begin
  138. if location.size=OS_F64 then
  139. op:=a_f64_lt
  140. else
  141. op:=a_f32_lt;
  142. end;
  143. lten :
  144. begin
  145. if location.size=OS_F64 then
  146. op:=a_f64_le
  147. else
  148. op:=a_f32_le;
  149. end;
  150. gtn :
  151. begin
  152. if location.size=OS_F64 then
  153. op:=a_f64_gt
  154. else
  155. op:=a_f32_gt;
  156. end;
  157. gten :
  158. begin
  159. if location.size=OS_F64 then
  160. op:=a_f64_ge
  161. else
  162. op:=a_f32_ge;
  163. end;
  164. equaln :
  165. begin
  166. if location.size=OS_F64 then
  167. op:=a_f64_eq
  168. else
  169. op:=a_f32_eq;
  170. commutative:=true;
  171. end;
  172. unequaln :
  173. begin
  174. if location.size=OS_F64 then
  175. op:=a_f64_ne
  176. else
  177. op:=a_f32_ne;
  178. commutative:=true;
  179. end;
  180. else
  181. internalerror(2011010402);
  182. end;
  183. { swap the operands to make it easier for the optimizer to optimize
  184. the operand stack slot reloading (non-commutative operations must
  185. always be in the correct order though) }
  186. if (commutative and
  187. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  188. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  189. (not commutative and
  190. (nf_swapped in flags)) then
  191. swapleftright;
  192. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  193. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  194. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  195. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  196. { could be optimized in the future by keeping the results on the stack,
  197. if we add code to swap the operands when necessary (a_swap for
  198. singles, store/load/load for doubles since there is no swap for
  199. 2-slot elements -- also adjust expectloc in that case! }
  200. thlcgwasm(hlcg).a_load_stack_ref(current_asmdata.CurrAsmList,resultdef,location.reference,0);
  201. end;
  202. procedure twasmaddnode.second_cmpboolean;
  203. begin
  204. //second_generic_compare(true);
  205. end;
  206. procedure twasmaddnode.second_cmp64bit;
  207. begin
  208. //second_generic_compare(not is_signed(left.resultdef));
  209. end;
  210. procedure twasmaddnode.second_add64bit;
  211. begin
  212. //second_opordinal;
  213. end;
  214. procedure twasmaddnode.second_cmpordinal;
  215. begin
  216. second_generic_compare(not is_signed(left.resultdef));
  217. end;
  218. procedure twasmaddnode.second_addboolean;
  219. begin
  220. if (nodetype in [orn,andn]) and
  221. (not(cs_full_boolean_eval in current_settings.localswitches) or
  222. (nf_short_bool in flags)) then
  223. begin
  224. secondpass(left);
  225. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_if) );
  226. case nodetype of
  227. andn :
  228. begin
  229. // inside of IF (the condition evaluated as true)
  230. // for "and" must evaluate the right section
  231. secondpass(right);
  232. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  233. // inside of ELSE (the condition evaluated as false)
  234. // for "and" must end evaluation immediately
  235. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  236. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  237. end;
  238. orn :
  239. begin
  240. // inside of IF (the condition evaluated as true)
  241. // for "or" must end evalaution immediately - satified!
  242. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 1) );
  243. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  244. // inside of ELSE (the condition evaluated as false)
  245. // for "or" must evaluate the right part
  246. secondpass(right);
  247. // inside of ELSE (the condition evaluated as false)
  248. // for "and" must end evaluation immediately
  249. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  250. end;
  251. else
  252. Internalerror(2019091902);
  253. end;
  254. // todo: need to reset location to the stack?
  255. //Internalerror(2019091901);
  256. end else
  257. inherited;
  258. end;
  259. procedure twasmaddnode.second_generic_compare(unsigned: boolean);
  260. var
  261. truelabel,
  262. falselabel: tasmlabel;
  263. cmpop: TOpCmp;
  264. begin
  265. truelabel:=nil;
  266. falselabel:=nil;
  267. pass_left_right;
  268. { swap the operands to make it easier for the optimizer to optimize
  269. the operand stack slot reloading in case both are in a register }
  270. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  271. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  272. swapleftright;
  273. cmpop:=cmpnode2topcmp(unsigned);
  274. if (nf_swapped in flags) then
  275. cmpop:=swap_opcmp(cmpop);
  276. // must generate those labels...
  277. current_asmdata.getjumplabel(truelabel);
  278. current_asmdata.getjumplabel(falselabel);
  279. location_reset_jump(location,truelabel,falselabel);
  280. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  281. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,location.truelabel)
  282. else case right.location.loc of
  283. LOC_REGISTER,LOC_CREGISTER:
  284. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,location.truelabel);
  285. LOC_REFERENCE,LOC_CREFERENCE:
  286. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,location.truelabel);
  287. LOC_CONSTANT:
  288. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,location.truelabel);
  289. else
  290. internalerror(2011010413);
  291. end;
  292. end;
  293. begin
  294. caddnode:=twasmaddnode;
  295. end.