nwasmmat.pas 9.2 KB

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