nwasmcnv.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,left.resultdef,resultdef,false);
  115. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  116. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  117. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  118. end;
  119. procedure twasmtypeconvnode.second_ansistring_to_pchar;
  120. var
  121. hr : treference;
  122. begin
  123. thlcgwasm(hlcg).a_cmp_const_loc_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE,0,left.location);
  124. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  125. thlcgwasm(hlcg).incblock;
  126. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  127. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  128. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_else));
  129. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  130. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  131. reference_reset(hr,2,[]);
  132. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR',AT_DATA);
  133. current_module.add_extern_asmsym('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  134. thlcgwasm(hlcg).a_loadaddr_ref_stack(current_asmdata.CurrAsmList,cwidechartype,resultdef,hr);
  135. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  136. thlcgwasm(hlcg).decblock;
  137. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  138. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  139. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  140. end;
  141. procedure twasmtypeconvnode.second_class_to_intf;
  142. var
  143. hd : tobjectdef;
  144. ImplIntf : TImplementedInterface;
  145. begin
  146. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  147. case left.location.loc of
  148. LOC_CREFERENCE,
  149. LOC_REFERENCE:
  150. begin
  151. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  152. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  153. location_freetemp(current_asmdata.CurrAsmList,left.location);
  154. end;
  155. LOC_CREGISTER:
  156. begin
  157. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  158. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  159. end;
  160. LOC_REGISTER:
  161. begin
  162. location.register:=left.location.register;
  163. hlcg.g_ptrtypecast_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,location.register);
  164. end;
  165. LOC_CONSTANT:
  166. begin
  167. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  168. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,left.location.value,location.register);
  169. end
  170. else
  171. internalerror(121120001);
  172. end;
  173. hd:=tobjectdef(left.resultdef);
  174. while assigned(hd) do
  175. begin
  176. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  177. if assigned(ImplIntf) then
  178. begin
  179. case ImplIntf.IType of
  180. etStandard:
  181. begin
  182. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,resultdef,OC_NE,0,location.register);
  183. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  184. thlcgwasm(hlcg).incblock;
  185. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  186. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,ImplIntf.ioffset,location.register);
  187. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  188. thlcgwasm(hlcg).decblock;
  189. break;
  190. end;
  191. else
  192. internalerror(200802163);
  193. end;
  194. end;
  195. hd:=hd.childof;
  196. end;
  197. if hd=nil then
  198. internalerror(2002081301);
  199. end;
  200. begin
  201. ctypeconvnode:=twasmtypeconvnode;
  202. end.