nwasminl.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. {
  2. Copyright (c) 1998-2002, 2021 by Florian Klaempfl and Nikolay Nikolov
  3. Generate WebAssembly inline 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. }
  17. unit nwasminl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncginl;
  22. type
  23. { twasminlinenode }
  24. twasminlinenode = class(tcginlinenode)
  25. private
  26. procedure second_memory_size;
  27. procedure second_memory_grow;
  28. procedure second_unreachable;
  29. public
  30. function pass_typecheck_cpu: tnode; override;
  31. function first_cpu: tnode; override;
  32. procedure pass_generate_code_cpu; override;
  33. procedure second_length;override;
  34. end;
  35. implementation
  36. uses
  37. ninl,compinnr,
  38. cpubase,
  39. aasmbase,aasmdata,aasmcpu,
  40. cgbase,cgutils,
  41. hlcgobj,hlcgcpu,
  42. defutil,pass_2,
  43. symtype,symdef;
  44. {*****************************************************************************
  45. twasminlinenode
  46. *****************************************************************************}
  47. procedure twasminlinenode.second_memory_size;
  48. begin
  49. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_memory_size));
  50. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  51. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  52. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  53. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  54. end;
  55. procedure twasminlinenode.second_memory_grow;
  56. begin
  57. secondpass(left);
  58. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  59. thlcgwasm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,left.resultdef,left.location.register);
  60. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_memory_grow));
  61. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  62. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  63. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  64. end;
  65. procedure twasminlinenode.second_unreachable;
  66. begin
  67. location_reset(location,LOC_VOID,OS_NO);
  68. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_unreachable));
  69. end;
  70. function twasminlinenode.pass_typecheck_cpu: tnode;
  71. begin
  72. Result:=nil;
  73. case inlinenumber of
  74. in_wasm32_memory_size:
  75. begin
  76. CheckParameters(0);
  77. resultdef:=u32inttype;
  78. end;
  79. in_wasm32_memory_grow:
  80. begin
  81. CheckParameters(1);
  82. resultdef:=u32inttype;
  83. end;
  84. in_wasm32_unreachable:
  85. begin
  86. CheckParameters(0);
  87. resultdef:=voidtype;
  88. end;
  89. else
  90. Result:=inherited pass_typecheck_cpu;
  91. end;
  92. end;
  93. function twasminlinenode.first_cpu: tnode;
  94. begin
  95. Result:=nil;
  96. case inlinenumber of
  97. in_wasm32_memory_size,
  98. in_wasm32_memory_grow:
  99. expectloc:=LOC_REGISTER;
  100. in_wasm32_unreachable:
  101. expectloc:=LOC_VOID;
  102. else
  103. Result:=inherited first_cpu;
  104. end;
  105. end;
  106. procedure twasminlinenode.pass_generate_code_cpu;
  107. begin
  108. case inlinenumber of
  109. in_wasm32_memory_size:
  110. second_memory_size;
  111. in_wasm32_memory_grow:
  112. second_memory_grow;
  113. in_wasm32_unreachable:
  114. second_unreachable;
  115. else
  116. inherited pass_generate_code_cpu;
  117. end;
  118. end;
  119. procedure twasminlinenode.second_length;
  120. var
  121. lendef : tdef;
  122. href : treference;
  123. extra_slots: LongInt;
  124. begin
  125. secondpass(left);
  126. if is_shortstring(left.resultdef) then
  127. begin
  128. location_copy(location,left.location);
  129. location.size:=OS_8;
  130. end
  131. else
  132. begin
  133. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  134. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  135. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register);
  136. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  137. thlcgwasm(hlcg).incblock;
  138. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  139. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_const,0));
  140. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  141. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  142. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  143. { the length of a widestring is a 32 bit unsigned int. Since every
  144. character occupies 2 bytes, on a 32 bit platform you can express
  145. the maximum length using 31 bits. On a 64 bit platform, it may be
  146. 32 bits. This means that regardless of the platform, a location
  147. with size OS_SINT/ossinttype can hold the length without
  148. overflowing (this code returns an ossinttype value) }
  149. if is_widestring(left.resultdef) then
  150. lendef:=u32inttype
  151. else
  152. lendef:=ossinttype;
  153. { volatility of the ansistring/widestring refers to the volatility of the
  154. string pointer, not of the string data }
  155. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  156. extra_slots:=thlcgwasm(hlcg).prepare_stack_for_ref(current_asmdata.CurrAsmList,href,false);
  157. thlcgwasm(hlcg).a_load_ref_stack(current_asmdata.CurrAsmList,lendef,href,extra_slots);
  158. if is_widestring(left.resultdef) then
  159. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SHR,resultdef,1);
  160. { Dynamic arrays do not have their length attached but their maximum index }
  161. if is_dynamic_array(left.resultdef) then
  162. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_ADD,resultdef,1);
  163. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  164. thlcgwasm(hlcg).decblock;
  165. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  166. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  167. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  168. end;
  169. end;
  170. begin
  171. cinlinenode:=twasminlinenode;
  172. end.