nwasmcnv.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. secondpass(left);
  56. if codegenerror then
  57. exit;
  58. case tfloatdef(resultdef).floattype of
  59. s32real:
  60. begin
  61. if is_64bitint(left.resultdef) or
  62. is_currency(left.resultdef) then
  63. begin
  64. if is_signed(left.resultdef) then
  65. op:=a_f32_convert_i64_s
  66. else
  67. op:=a_f32_convert_i64_u;
  68. end
  69. else
  70. { other integers are supposed to be 32 bit }
  71. begin
  72. if is_signed(left.resultdef) then
  73. op:=a_f32_convert_i32_s
  74. else
  75. op:=a_f32_convert_i32_u;
  76. end;
  77. end;
  78. s64real:
  79. begin
  80. if is_64bitint(left.resultdef) or
  81. is_currency(left.resultdef) then
  82. begin
  83. if is_signed(left.resultdef) then
  84. op:=a_f64_convert_i64_s
  85. else
  86. op:=a_f64_convert_i64_u;
  87. end
  88. else
  89. { other integers are supposed to be 32 bit }
  90. begin
  91. if is_signed(left.resultdef) then
  92. op:=a_f64_convert_i32_s
  93. else
  94. op:=a_f64_convert_i32_u;
  95. end;
  96. end;
  97. else
  98. internalerror(2021010501);
  99. end;
  100. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  101. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  102. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  103. location.register := hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  104. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  105. end;
  106. procedure twasmtypeconvnode.second_int_to_bool;
  107. begin
  108. secondpass(left);
  109. if codegenerror then
  110. exit;
  111. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  112. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,left.resultdef,0,R_INTREGISTER);
  113. thlcgwasm(hlcg).a_cmp_stack_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE);
  114. if is_cbool(resultdef) then
  115. begin
  116. if is_64bit(resultdef) then
  117. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i64])))
  118. else
  119. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  120. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  121. if is_64bit(resultdef) then
  122. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, -1) )
  123. else if is_32bit(resultdef) then
  124. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, -1) )
  125. else if is_16bit(resultdef) then
  126. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 65535) )
  127. else if is_8bit(resultdef) then
  128. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 255) )
  129. else
  130. internalerror(2021100101);
  131. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  132. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  133. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  134. if is_64bit(resultdef) then
  135. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i64_const, 0) )
  136. else
  137. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  138. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  139. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  140. end
  141. else
  142. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,u32inttype,resultdef,false);
  143. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  144. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  145. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  146. end;
  147. procedure twasmtypeconvnode.second_ansistring_to_pchar;
  148. var
  149. hr : treference;
  150. begin
  151. thlcgwasm(hlcg).a_cmp_const_loc_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE,0,left.location);
  152. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  153. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  154. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  155. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_else));
  156. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  157. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  158. reference_reset(hr,2,[]);
  159. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR',AT_DATA);
  160. current_module.add_extern_asmsym('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  161. thlcgwasm(hlcg).a_loadaddr_ref_stack(current_asmdata.CurrAsmList,cwidechartype,resultdef,hr);
  162. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  163. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  164. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  165. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  166. end;
  167. procedure twasmtypeconvnode.second_class_to_intf;
  168. var
  169. hd : tobjectdef;
  170. ImplIntf : TImplementedInterface;
  171. begin
  172. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  173. case left.location.loc of
  174. LOC_CREFERENCE,
  175. LOC_REFERENCE:
  176. begin
  177. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  178. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  179. location_freetemp(current_asmdata.CurrAsmList,left.location);
  180. end;
  181. LOC_CREGISTER:
  182. begin
  183. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  184. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  185. end;
  186. LOC_REGISTER:
  187. begin
  188. location.register:=left.location.register;
  189. hlcg.g_ptrtypecast_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,location.register);
  190. end;
  191. LOC_CONSTANT:
  192. begin
  193. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  194. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,left.location.value,location.register);
  195. end
  196. else
  197. internalerror(121120001);
  198. end;
  199. hd:=tobjectdef(left.resultdef);
  200. while assigned(hd) do
  201. begin
  202. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  203. if assigned(ImplIntf) then
  204. begin
  205. case ImplIntf.IType of
  206. etStandard:
  207. begin
  208. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,resultdef,OC_NE,0,location.register);
  209. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  210. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  211. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,ImplIntf.ioffset,location.register);
  212. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  213. break;
  214. end;
  215. else
  216. internalerror(200802163);
  217. end;
  218. end;
  219. hd:=hd.childof;
  220. end;
  221. if hd=nil then
  222. internalerror(2002081301);
  223. end;
  224. begin
  225. ctypeconvnode:=twasmtypeconvnode;
  226. end.