nwasminl.pas 12 KB

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