nwasmmat.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. {
  2. Copyright (c) 1998-2011, 2019 by Florian Klaempfl and Jonas Maebe, Dmitry Boyarintsev
  3. Generate WebAssembly code for math nodes
  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 nwasmmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat,ncghlmat;
  22. type
  23. twasmmoddivnode = class(tmoddivnode)
  24. protected
  25. function use_moddiv64bitint_helper: boolean; override;
  26. public
  27. procedure pass_generate_code;override;
  28. end;
  29. twasmshlshrnode = class(tshlshrnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. twasmnotnode = class(tcgnotnode)
  33. protected
  34. procedure second_boolean;override;
  35. end;
  36. twasmunaryminusnode = class(tcgunaryminusnode)
  37. procedure second_float;override;
  38. end;
  39. implementation
  40. uses
  41. globtype,systems,constexp,
  42. cutils,verbose,globals,compinnr,
  43. symconst,symdef,
  44. aasmbase,aasmcpu,aasmtai,aasmdata,
  45. defutil,
  46. cgbase,cgobj,pass_2,procinfo,
  47. ncon,
  48. cpubase,
  49. hlcgobj,hlcgcpu,cgutils;
  50. {*****************************************************************************
  51. twasmmoddivnode
  52. *****************************************************************************}
  53. function twasmmoddivnode.use_moddiv64bitint_helper: boolean;
  54. begin
  55. result:=
  56. (left.resultdef.typ=orddef) and
  57. (right.resultdef.typ=orddef) and
  58. ((torddef(left.resultdef).ordtype=u64bit) or
  59. (torddef(right.resultdef).ordtype=u64bit));
  60. end;
  61. procedure twasmmoddivnode.pass_generate_code;
  62. var
  63. tmpreg: tregister;
  64. lab: tasmlabel;
  65. ovloc: tlocation;
  66. op: topcg;
  67. isu32int: boolean;
  68. begin
  69. secondpass(left);
  70. secondpass(right);
  71. location_reset(location,LOC_REGISTER,left.location.size);
  72. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  73. if nodetype=divn then
  74. begin
  75. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  76. if is_signed(resultdef) then
  77. op:=OP_IDIV
  78. else
  79. op:=OP_DIV;
  80. thlcgwasm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,op,right.resultdef,right.location)
  81. end
  82. else
  83. begin
  84. { must be handled via a helper }
  85. if torddef(resultdef).ordtype=u64bit then
  86. internalerror(2011010416);
  87. if (torddef(resultdef).ordtype<>u32bit) then
  88. begin
  89. isu32int:=false;
  90. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  91. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  92. end
  93. else
  94. begin
  95. isu32int:=true;
  96. if left.location.loc=LOC_CONSTANT then
  97. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s64inttype,left.location.value,R_INTREGISTER)
  98. else
  99. begin
  100. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  101. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,u32inttype,s64inttype,false);
  102. end;
  103. if right.location.loc=LOC_CONSTANT then
  104. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,s64inttype,right.location.value,R_INTREGISTER)
  105. else
  106. begin
  107. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  108. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,u32inttype,s64inttype,false);
  109. end;
  110. end;
  111. if isu32int or
  112. (torddef(resultdef).ordtype=s64bit) then
  113. begin
  114. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i64_rem_s));
  115. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  116. end
  117. else
  118. begin
  119. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_rem_s));
  120. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  121. end;
  122. if isu32int then
  123. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,s64inttype,u32inttype,false);
  124. end;
  125. thlcgwasm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  126. if (cs_check_overflow in current_settings.localswitches) and
  127. is_signed(resultdef) then
  128. begin
  129. { the JVM raises an exception for integer div-iby-zero -> only
  130. overflow in case left is low(inttype) and right is -1 ->
  131. check by adding high(inttype) to left and and'ing with right
  132. -> result is -1 only in case above conditions are fulfilled)
  133. }
  134. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  135. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  136. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,torddef(resultdef).high,right.location.register,tmpreg);
  137. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  138. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_AND,resultdef,left.location.register,tmpreg);
  139. current_asmdata.getjumplabel(lab);
  140. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  141. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_NE,-1,tmpreg,lab);
  142. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_overflow',[],nil);
  143. hlcg.a_label(current_asmdata.CurrAsmList,lab);
  144. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  145. end;
  146. end;
  147. {*****************************************************************************
  148. twasmshlshrnode
  149. *****************************************************************************}
  150. procedure twasmshlshrnode.pass_generate_code;
  151. var
  152. op : topcg;
  153. begin
  154. secondpass(left);
  155. secondpass(right);
  156. location_reset(location,LOC_REGISTER,left.location.size);
  157. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  158. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  159. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  160. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,false);
  161. if nodetype=shln then
  162. op:=OP_SHL
  163. else
  164. op:=OP_SHR;
  165. thlcgwasm(hlcg).a_op_stack(current_asmdata.CurrAsmList,op,resultdef);
  166. thlcgwasm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  167. end;
  168. {*****************************************************************************
  169. twasmnotnode
  170. *****************************************************************************}
  171. procedure twasmnotnode.second_boolean;
  172. begin
  173. secondpass(left);
  174. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  175. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  176. location_reset(location,LOC_REGISTER,left.location.size);
  177. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  178. { perform the NOT operation }
  179. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,left.location.register,location.register);
  180. end;
  181. {*****************************************************************************
  182. twasmunaryminustnode
  183. *****************************************************************************}
  184. procedure twasmunaryminusnode.second_float;
  185. var
  186. opc: tasmop;
  187. begin
  188. secondpass(left);
  189. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  190. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  191. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  192. if (tfloatdef(left.resultdef).floattype=s32real) then
  193. opc:=a_f32_neg
  194. else
  195. opc:=a_f64_neg;
  196. current_asmdata.CurrAsmList.concat(taicpu.op_none(opc));
  197. thlcgwasm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  198. end;
  199. begin
  200. cmoddivnode:=twasmmoddivnode;
  201. cshlshrnode:=twasmshlshrnode;
  202. cnotnode:=twasmnotnode;
  203. cunaryminusnode:=twasmunaryminusnode;
  204. end.