nwasmcnv.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. {
  2. Copyright (c) 1998-2020 by Florian Klaempfl and Nikolay Nikolov
  3. Generate WebAssembly code for type converting 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. unit nwasmcnv;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. node,ncnv,ncgcnv;
  21. type
  22. { twasmtypeconvnode }
  23. twasmtypeconvnode = class(tcgtypeconvnode)
  24. protected
  25. function first_int_to_real: tnode; override;
  26. procedure second_int_to_real;override;
  27. procedure second_int_to_bool;override;
  28. procedure second_ansistring_to_pchar;override;
  29. procedure second_class_to_intf;override;
  30. end;
  31. implementation
  32. uses
  33. verbose,globals,globtype,aasmdata,
  34. defutil,defcmp,fmodule,cpubase,
  35. cgbase,cgutils,pass_1,pass_2,
  36. aasmbase,aasmcpu,
  37. symdef,symconst,
  38. tgobj,
  39. hlcgobj,hlcgcpu;
  40. { twasmtypeconvnode }
  41. function twasmtypeconvnode.first_int_to_real: tnode;
  42. begin
  43. first_int_to_real:=nil;
  44. if left.resultdef.size<4 then
  45. begin
  46. inserttypeconv(left,s32inttype);
  47. firstpass(left);
  48. end;
  49. expectloc:=LOC_FPUREGISTER;
  50. end;
  51. procedure twasmtypeconvnode.second_int_to_real;
  52. var
  53. op: TAsmOp;
  54. begin
  55. case tfloatdef(resultdef).floattype of
  56. s32real:
  57. begin
  58. if is_64bitint(left.resultdef) or
  59. is_currency(left.resultdef) then
  60. begin
  61. if is_signed(left.resultdef) then
  62. op:=a_f32_convert_i64_s
  63. else
  64. op:=a_f32_convert_i64_u;
  65. end
  66. else
  67. { other integers are supposed to be 32 bit }
  68. begin
  69. if is_signed(left.resultdef) then
  70. op:=a_f32_convert_i32_s
  71. else
  72. op:=a_f32_convert_i32_u;
  73. end;
  74. end;
  75. s64real:
  76. begin
  77. if is_64bitint(left.resultdef) or
  78. is_currency(left.resultdef) then
  79. begin
  80. if is_signed(left.resultdef) then
  81. op:=a_f64_convert_i64_s
  82. else
  83. op:=a_f64_convert_i64_u;
  84. end
  85. else
  86. { other integers are supposed to be 32 bit }
  87. begin
  88. if is_signed(left.resultdef) then
  89. op:=a_f64_convert_i32_s
  90. else
  91. op:=a_f64_convert_i32_u;
  92. end;
  93. end;
  94. else
  95. internalerror(2021010501);
  96. end;
  97. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  98. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  99. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  100. location.register := hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  101. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  102. end;
  103. procedure twasmtypeconvnode.second_int_to_bool;
  104. begin
  105. secondpass(left);
  106. if codegenerror then
  107. exit;
  108. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  109. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,left.resultdef,0,R_INTREGISTER);
  110. thlcgwasm(hlcg).a_cmp_stack_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE);
  111. if is_cbool(resultdef) then
  112. begin
  113. if is_64bit(resultdef) then
  114. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i64])))
  115. else
  116. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  117. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  118. if is_64bit(resultdef) then
  119. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, -1) )
  120. else if is_32bit(resultdef) then
  121. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, -1) )
  122. else if is_16bit(resultdef) then
  123. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 65535) )
  124. else if is_8bit(resultdef) then
  125. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 255) )
  126. else
  127. internalerror(2021100101);
  128. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  129. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  130. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  131. if is_64bit(resultdef) then
  132. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, 0) )
  133. else
  134. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  135. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  136. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  137. end
  138. else
  139. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,u32inttype,resultdef,false);
  140. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  141. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  142. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  143. end;
  144. procedure twasmtypeconvnode.second_ansistring_to_pchar;
  145. var
  146. hr : treference;
  147. begin
  148. thlcgwasm(hlcg).a_cmp_const_loc_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE,0,left.location);
  149. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  150. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  151. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  152. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_else));
  153. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  154. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  155. reference_reset(hr,2,[]);
  156. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR',AT_DATA);
  157. current_module.add_extern_asmsym('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  158. thlcgwasm(hlcg).a_loadaddr_ref_stack(current_asmdata.CurrAsmList,cwidechartype,resultdef,hr);
  159. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  160. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  161. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  162. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  163. end;
  164. procedure twasmtypeconvnode.second_class_to_intf;
  165. var
  166. hd : tobjectdef;
  167. ImplIntf : TImplementedInterface;
  168. begin
  169. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  170. case left.location.loc of
  171. LOC_CREFERENCE,
  172. LOC_REFERENCE:
  173. begin
  174. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  175. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  176. location_freetemp(current_asmdata.CurrAsmList,left.location);
  177. end;
  178. LOC_CREGISTER:
  179. begin
  180. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  181. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  182. end;
  183. LOC_REGISTER:
  184. begin
  185. location.register:=left.location.register;
  186. hlcg.g_ptrtypecast_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,location.register);
  187. end;
  188. LOC_CONSTANT:
  189. begin
  190. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  191. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,left.location.value,location.register);
  192. end
  193. else
  194. internalerror(121120001);
  195. end;
  196. hd:=tobjectdef(left.resultdef);
  197. while assigned(hd) do
  198. begin
  199. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  200. if assigned(ImplIntf) then
  201. begin
  202. case ImplIntf.IType of
  203. etStandard:
  204. begin
  205. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,resultdef,OC_NE,0,location.register);
  206. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  207. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  208. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,ImplIntf.ioffset,location.register);
  209. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  210. break;
  211. end;
  212. else
  213. internalerror(200802163);
  214. end;
  215. end;
  216. hd:=hd.childof;
  217. end;
  218. if hd=nil then
  219. internalerror(2002081301);
  220. end;
  221. begin
  222. ctypeconvnode:=twasmtypeconvnode;
  223. end.