nwasminl.pas 12 KB

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