nwasminl.pas 13 KB

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