nwasminl.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_high; override;
  27. procedure second_memory_size;
  28. procedure second_memory_grow;
  29. procedure second_unreachable;
  30. public
  31. function pass_typecheck_cpu: tnode; override;
  32. function first_cpu: tnode; override;
  33. procedure pass_generate_code_cpu; override;
  34. procedure second_length;override;
  35. end;
  36. implementation
  37. uses
  38. ninl,compinnr,
  39. cpubase,
  40. aasmbase,aasmdata,aasmcpu,
  41. cgbase,cgutils,
  42. hlcgobj,hlcgcpu,
  43. defutil,pass_2,verbose,
  44. symtype,symdef;
  45. {*****************************************************************************
  46. twasminlinenode
  47. *****************************************************************************}
  48. procedure twasminlinenode.second_high;
  49. var
  50. hightype: TWasmBasicType;
  51. begin
  52. secondpass(left);
  53. if not(is_dynamic_array(left.resultdef)) then
  54. Internalerror(2019122801);
  55. { determine the WasmBasicType of the result }
  56. if is_64bit(resultdef) then
  57. hightype:=wbt_i64
  58. else
  59. hightype:=wbt_i32;
  60. { length in dynamic arrays is at offset -sizeof(pint) }
  61. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  62. { 64-bit pointer values need a <>0 comparison to produce a 32-bit int on the stack (0 or 1) for the 'if' instruction.
  63. 32-bit pointer values don't need it, because 'if' already expects and pops a 32-bit int and checks for <>0. }
  64. if is_64bit(left.resultdef) then
  65. begin
  66. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,left.resultdef,0,R_INTREGISTER);
  67. thlcgwasm(hlcg).a_cmp_stack_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE);
  68. end;
  69. { if not nil }
  70. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[hightype])));
  71. thlcgwasm(hlcg).incblock;
  72. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  73. { volatility of the dyn. array refers to the volatility of the
  74. string pointer, not of the string data }
  75. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  76. { length in dynamic arrays is at offset -ossinttype.size }
  77. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,left.resultdef,ossinttype.size);
  78. { load length }
  79. if ossinttype.size=8 then
  80. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i64_load,0))
  81. else
  82. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_load,0));
  83. { else }
  84. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_else));
  85. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  86. { high=-1 }
  87. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,resultdef,-1,R_INTREGISTER);
  88. { endif }
  89. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_end_if));
  90. thlcgwasm(hlcg).decblock;
  91. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  92. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  93. if location.size in [OS_64,OS_S64] then
  94. begin
  95. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  96. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  97. end
  98. else
  99. {$endif}
  100. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  101. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  102. end;
  103. procedure twasminlinenode.second_memory_size;
  104. begin
  105. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_memory_size));
  106. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  107. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  108. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  109. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  110. end;
  111. procedure twasminlinenode.second_memory_grow;
  112. begin
  113. secondpass(left);
  114. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  115. thlcgwasm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,left.resultdef,left.location.register);
  116. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_memory_grow));
  117. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  118. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  119. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  120. end;
  121. procedure twasminlinenode.second_unreachable;
  122. begin
  123. location_reset(location,LOC_VOID,OS_NO);
  124. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_unreachable));
  125. end;
  126. function twasminlinenode.pass_typecheck_cpu: tnode;
  127. begin
  128. Result:=nil;
  129. case inlinenumber of
  130. in_wasm32_memory_size:
  131. begin
  132. CheckParameters(0);
  133. resultdef:=u32inttype;
  134. end;
  135. in_wasm32_memory_grow:
  136. begin
  137. CheckParameters(1);
  138. resultdef:=u32inttype;
  139. end;
  140. in_wasm32_unreachable:
  141. begin
  142. CheckParameters(0);
  143. resultdef:=voidtype;
  144. end;
  145. else
  146. Result:=inherited pass_typecheck_cpu;
  147. end;
  148. end;
  149. function twasminlinenode.first_cpu: tnode;
  150. begin
  151. Result:=nil;
  152. case inlinenumber of
  153. in_wasm32_memory_size,
  154. in_wasm32_memory_grow:
  155. expectloc:=LOC_REGISTER;
  156. in_wasm32_unreachable:
  157. expectloc:=LOC_VOID;
  158. else
  159. Result:=inherited first_cpu;
  160. end;
  161. end;
  162. procedure twasminlinenode.pass_generate_code_cpu;
  163. begin
  164. case inlinenumber of
  165. in_wasm32_memory_size:
  166. second_memory_size;
  167. in_wasm32_memory_grow:
  168. second_memory_grow;
  169. in_wasm32_unreachable:
  170. second_unreachable;
  171. else
  172. inherited pass_generate_code_cpu;
  173. end;
  174. end;
  175. procedure twasminlinenode.second_length;
  176. var
  177. lendef : tdef;
  178. href : treference;
  179. extra_slots: LongInt;
  180. begin
  181. secondpass(left);
  182. if is_shortstring(left.resultdef) then
  183. begin
  184. location_copy(location,left.location);
  185. location.size:=OS_8;
  186. end
  187. else
  188. begin
  189. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  190. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  191. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register);
  192. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  193. thlcgwasm(hlcg).incblock;
  194. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  195. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_const,0));
  196. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  197. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  198. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  199. { the length of a widestring is a 32 bit unsigned int. Since every
  200. character occupies 2 bytes, on a 32 bit platform you can express
  201. the maximum length using 31 bits. On a 64 bit platform, it may be
  202. 32 bits. This means that regardless of the platform, a location
  203. with size OS_SINT/ossinttype can hold the length without
  204. overflowing (this code returns an ossinttype value) }
  205. if is_widestring(left.resultdef) then
  206. lendef:=u32inttype
  207. else
  208. lendef:=ossinttype;
  209. { volatility of the ansistring/widestring refers to the volatility of the
  210. string pointer, not of the string data }
  211. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  212. extra_slots:=thlcgwasm(hlcg).prepare_stack_for_ref(current_asmdata.CurrAsmList,href,false);
  213. thlcgwasm(hlcg).a_load_ref_stack(current_asmdata.CurrAsmList,lendef,href,extra_slots);
  214. if is_widestring(left.resultdef) then
  215. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SHR,resultdef,1);
  216. { Dynamic arrays do not have their length attached but their maximum index }
  217. if is_dynamic_array(left.resultdef) then
  218. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_ADD,resultdef,1);
  219. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  220. thlcgwasm(hlcg).decblock;
  221. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  222. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  223. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  224. end;
  225. end;
  226. begin
  227. cinlinenode:=twasminlinenode;
  228. end.