nwasmadd.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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;
  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. end;
  36. implementation
  37. uses
  38. systems,
  39. cutils,verbose,constexp,globtype,compinnr,
  40. symconst,symtable,symdef,symcpu,
  41. paramgr,procinfo,pass_1,
  42. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,
  43. hlcgobj,hlcgcpu,cgutils,
  44. cpupara,
  45. nbas,ncon,nset,nadd,ncal,ncnv,ninl,nld,nmat,nmem,
  46. //njvmcon,
  47. cgobj;
  48. {*****************************************************************************
  49. tjvmaddnode
  50. *****************************************************************************}
  51. procedure twasmaddnode.pass_left_right;
  52. begin
  53. //if not((nodetype in [orn,andn]) and
  54. // is_boolean(left.resultdef)) then
  55. // swapleftright;
  56. inherited pass_left_right;
  57. end;
  58. procedure twasmaddnode.second_addfloat;
  59. //var
  60. // op : TAsmOp;
  61. // commutative : boolean;
  62. begin
  63. //pass_left_right;
  64. //
  65. //location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  66. //location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  67. //
  68. //commutative:=false;
  69. //case nodetype of
  70. // addn :
  71. // begin
  72. // if location.size=OS_F64 then
  73. // op:=a_dadd
  74. // else
  75. // op:=a_fadd;
  76. // commutative:=true;
  77. // end;
  78. // muln :
  79. // begin
  80. // if location.size=OS_F64 then
  81. // op:=a_dmul
  82. // else
  83. // op:=a_fmul;
  84. // commutative:=true;
  85. // end;
  86. // subn :
  87. // begin
  88. // if location.size=OS_F64 then
  89. // op:=a_dsub
  90. // else
  91. // op:=a_fsub;
  92. // end;
  93. // slashn :
  94. // begin
  95. // if location.size=OS_F64 then
  96. // op:=a_ddiv
  97. // else
  98. // op:=a_fdiv;
  99. // end;
  100. // else
  101. // internalerror(2011010402);
  102. //end;
  103. //
  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. //
  114. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  115. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  116. //
  117. //current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  118. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
  119. //{ could be optimized in the future by keeping the results on the stack,
  120. // if we add code to swap the operands when necessary (a_swap for
  121. // singles, store/load/load for doubles since there is no swap for
  122. // 2-slot elements -- also adjust expectloc in that case! }
  123. //thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  124. end;
  125. procedure twasmaddnode.second_cmpfloat;
  126. //var
  127. // truelabel,
  128. // falselabel: tasmlabel;
  129. // op: tasmop;
  130. // cmpop: TOpCmp;
  131. begin
  132. //truelabel:=nil;
  133. //falselabel:=nil;
  134. //pass_left_right;
  135. //{ swap the operands to make it easier for the optimizer to optimize
  136. // the operand stack slot reloading in case both are in a register }
  137. //if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  138. // (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  139. // swapleftright;
  140. //cmpop:=cmpnode2topcmp(false);
  141. //if (nf_swapped in flags) then
  142. // cmpop:=swap_opcmp(cmpop);
  143. //
  144. //current_asmdata.getjumplabel(truelabel);
  145. //current_asmdata.getjumplabel(falselabel);
  146. //location_reset_jump(location,truelabel,falselabel);
  147. //
  148. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  149. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  150. //
  151. //{ compares two floating point values and puts 1/0/-1 on stack depending
  152. // on whether value1 >/=/< value2 }
  153. //if left.location.size=OS_F64 then
  154. // { make sure that comparisons with NaNs always return false for </> }
  155. // if nodetype in [ltn,lten] then
  156. // op:=a_dcmpg
  157. // else
  158. // op:=a_dcmpl
  159. //else if nodetype in [ltn,lten] then
  160. // op:=a_fcmpg
  161. //else
  162. // op:=a_fcmpl;
  163. //current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  164. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  165. //
  166. //current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],location.truelabel));
  167. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  168. //hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  169. end;
  170. procedure twasmaddnode.second_cmpboolean;
  171. begin
  172. //second_generic_compare(true);
  173. end;
  174. procedure twasmaddnode.second_cmp64bit;
  175. begin
  176. //second_generic_compare(not is_signed(left.resultdef));
  177. end;
  178. procedure twasmaddnode.second_add64bit;
  179. begin
  180. //second_opordinal;
  181. end;
  182. procedure twasmaddnode.second_cmpordinal;
  183. begin
  184. second_generic_compare(not is_signed(left.resultdef));
  185. end;
  186. procedure twasmaddnode.second_generic_compare(unsigned: boolean);
  187. var
  188. truelabel,
  189. falselabel: tasmlabel;
  190. cmpop: TOpCmp;
  191. begin
  192. truelabel:=nil;
  193. falselabel:=nil;
  194. pass_left_right;
  195. { swap the operands to make it easier for the optimizer to optimize
  196. the operand stack slot reloading in case both are in a register }
  197. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  198. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  199. swapleftright;
  200. cmpop:=cmpnode2topcmp(unsigned);
  201. if (nf_swapped in flags) then
  202. cmpop:=swap_opcmp(cmpop);
  203. // must generate those labels...
  204. current_asmdata.getjumplabel(truelabel);
  205. current_asmdata.getjumplabel(falselabel);
  206. location_reset_jump(location,truelabel,falselabel);
  207. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  208. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,location.truelabel)
  209. else case right.location.loc of
  210. LOC_REGISTER,LOC_CREGISTER:
  211. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,location.truelabel);
  212. LOC_REFERENCE,LOC_CREFERENCE:
  213. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,location.truelabel);
  214. LOC_CONSTANT:
  215. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,location.truelabel);
  216. else
  217. internalerror(2011010413);
  218. end;
  219. end;
  220. begin
  221. caddnode:=twasmaddnode;
  222. end.