nwasmcnv.pas 10 KB

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