nwasmcnv.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. end;
  30. implementation
  31. uses
  32. verbose,globals,globtype,aasmdata,
  33. defutil,fmodule,cpubase,
  34. cgbase,cgutils,pass_1,pass_2,
  35. aasmbase,aasmcpu,
  36. symdef,
  37. hlcgobj,hlcgcpu;
  38. { twasmtypeconvnode }
  39. function twasmtypeconvnode.first_int_to_real: tnode;
  40. begin
  41. first_int_to_real:=nil;
  42. if left.resultdef.size<4 then
  43. begin
  44. inserttypeconv(left,s32inttype);
  45. firstpass(left);
  46. end;
  47. expectloc:=LOC_FPUREGISTER;
  48. end;
  49. procedure twasmtypeconvnode.second_int_to_real;
  50. var
  51. op: TAsmOp;
  52. begin
  53. secondpass(left);
  54. if codegenerror then
  55. exit;
  56. case tfloatdef(resultdef).floattype of
  57. s32real:
  58. begin
  59. if is_64bitint(left.resultdef) or
  60. is_currency(left.resultdef) then
  61. begin
  62. if is_signed(left.resultdef) then
  63. op:=a_f32_convert_i64_s
  64. else
  65. op:=a_f32_convert_i64_u;
  66. end
  67. else
  68. { other integers are supposed to be 32 bit }
  69. begin
  70. if is_signed(left.resultdef) then
  71. op:=a_f32_convert_i32_s
  72. else
  73. op:=a_f32_convert_i32_u;
  74. end;
  75. end;
  76. s64real:
  77. begin
  78. if is_64bitint(left.resultdef) or
  79. is_currency(left.resultdef) then
  80. begin
  81. if is_signed(left.resultdef) then
  82. op:=a_f64_convert_i64_s
  83. else
  84. op:=a_f64_convert_i64_u;
  85. end
  86. else
  87. { other integers are supposed to be 32 bit }
  88. begin
  89. if is_signed(left.resultdef) then
  90. op:=a_f64_convert_i32_s
  91. else
  92. op:=a_f64_convert_i32_u;
  93. end;
  94. end;
  95. else
  96. internalerror(2021010501);
  97. end;
  98. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  99. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  100. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  101. location.register := hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  102. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  103. end;
  104. procedure twasmtypeconvnode.second_int_to_bool;
  105. begin
  106. secondpass(left);
  107. if codegenerror then
  108. exit;
  109. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  110. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,left.resultdef,0,R_INTREGISTER);
  111. thlcgwasm(hlcg).a_cmp_stack_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE);
  112. thlcgwasm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,left.resultdef,resultdef,false);
  113. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  114. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  115. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  116. end;
  117. procedure twasmtypeconvnode.second_ansistring_to_pchar;
  118. var
  119. hr : treference;
  120. begin
  121. thlcgwasm(hlcg).a_cmp_const_loc_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE,0,left.location);
  122. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  123. thlcgwasm(hlcg).incblock;
  124. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  125. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  126. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_else));
  127. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  128. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  129. reference_reset(hr,2,[]);
  130. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR',AT_DATA);
  131. current_module.add_extern_asmsym('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  132. thlcgwasm(hlcg).a_loadaddr_ref_stack(current_asmdata.CurrAsmList,cwidechartype,resultdef,hr);
  133. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  134. thlcgwasm(hlcg).decblock;
  135. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  136. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  137. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  138. end;
  139. begin
  140. ctypeconvnode:=twasmtypeconvnode;
  141. end.