nwasmadd.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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;
  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+ord(location.size=OS_F64));
  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. // truelabel,
  126. // falselabel: tasmlabel;
  127. // op: tasmop;
  128. // cmpop: TOpCmp;
  129. begin
  130. //truelabel:=nil;
  131. //falselabel:=nil;
  132. //pass_left_right;
  133. //{ swap the operands to make it easier for the optimizer to optimize
  134. // the operand stack slot reloading in case both are in a register }
  135. //if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  136. // (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  137. // swapleftright;
  138. //cmpop:=cmpnode2topcmp(false);
  139. //if (nf_swapped in flags) then
  140. // cmpop:=swap_opcmp(cmpop);
  141. //
  142. //current_asmdata.getjumplabel(truelabel);
  143. //current_asmdata.getjumplabel(falselabel);
  144. //location_reset_jump(location,truelabel,falselabel);
  145. //
  146. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  147. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  148. //
  149. //{ compares two floating point values and puts 1/0/-1 on stack depending
  150. // on whether value1 >/=/< value2 }
  151. //if left.location.size=OS_F64 then
  152. // { make sure that comparisons with NaNs always return false for </> }
  153. // if nodetype in [ltn,lten] then
  154. // op:=a_dcmpg
  155. // else
  156. // op:=a_dcmpl
  157. //else if nodetype in [ltn,lten] then
  158. // op:=a_fcmpg
  159. //else
  160. // op:=a_fcmpl;
  161. //current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  162. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  163. //
  164. //current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],location.truelabel));
  165. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  166. //hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  167. end;
  168. procedure twasmaddnode.second_cmpboolean;
  169. begin
  170. //second_generic_compare(true);
  171. end;
  172. procedure twasmaddnode.second_cmp64bit;
  173. begin
  174. //second_generic_compare(not is_signed(left.resultdef));
  175. end;
  176. procedure twasmaddnode.second_add64bit;
  177. begin
  178. //second_opordinal;
  179. end;
  180. procedure twasmaddnode.second_cmpordinal;
  181. begin
  182. second_generic_compare(not is_signed(left.resultdef));
  183. end;
  184. procedure twasmaddnode.second_addboolean;
  185. begin
  186. if (nodetype in [orn,andn]) and
  187. (not(cs_full_boolean_eval in current_settings.localswitches) or
  188. (nf_short_bool in flags)) then
  189. begin
  190. secondpass(left);
  191. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_if) );
  192. case nodetype of
  193. andn :
  194. begin
  195. // inside of IF (the condition evaluated as true)
  196. // for "and" must evaluate the right section
  197. secondpass(right);
  198. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  199. // inside of ELSE (the condition evaluated as false)
  200. // for "and" must end evaluation immediately
  201. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  202. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end) );
  203. end;
  204. orn :
  205. begin
  206. // inside of IF (the condition evaluated as true)
  207. // for "or" must end evalaution immediately - satified!
  208. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 1) );
  209. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  210. // inside of ELSE (the condition evaluated as false)
  211. // for "or" must evaluate the right part
  212. secondpass(right);
  213. // inside of ELSE (the condition evaluated as false)
  214. // for "and" must end evaluation immediately
  215. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end) );
  216. end;
  217. else
  218. Internalerror(2019091902);
  219. end;
  220. // todo: need to reset location to the stack?
  221. //Internalerror(2019091901);
  222. end else
  223. inherited;
  224. end;
  225. procedure twasmaddnode.second_generic_compare(unsigned: boolean);
  226. var
  227. truelabel,
  228. falselabel: tasmlabel;
  229. cmpop: TOpCmp;
  230. begin
  231. truelabel:=nil;
  232. falselabel:=nil;
  233. pass_left_right;
  234. { swap the operands to make it easier for the optimizer to optimize
  235. the operand stack slot reloading in case both are in a register }
  236. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  237. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  238. swapleftright;
  239. cmpop:=cmpnode2topcmp(unsigned);
  240. if (nf_swapped in flags) then
  241. cmpop:=swap_opcmp(cmpop);
  242. // must generate those labels...
  243. current_asmdata.getjumplabel(truelabel);
  244. current_asmdata.getjumplabel(falselabel);
  245. location_reset_jump(location,truelabel,falselabel);
  246. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  247. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,location.truelabel)
  248. else case right.location.loc of
  249. LOC_REGISTER,LOC_CREGISTER:
  250. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,location.truelabel);
  251. LOC_REFERENCE,LOC_CREFERENCE:
  252. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,location.truelabel);
  253. LOC_CONSTANT:
  254. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,location.truelabel);
  255. else
  256. internalerror(2011010413);
  257. end;
  258. end;
  259. begin
  260. caddnode:=twasmaddnode;
  261. end.