nwasminl.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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_abs_real:tnode;override;
  27. function first_int_real:tnode;override;
  28. function first_sqrt_real:tnode;override;
  29. function first_trunc_real:tnode;override;
  30. function first_round_real:tnode;override;
  31. procedure second_abs_real;override;
  32. procedure second_int_real;override;
  33. procedure second_sqrt_real;override;
  34. procedure second_trunc_real;override;
  35. procedure second_round_real;override;
  36. procedure second_high; override;
  37. procedure second_memory_size;
  38. procedure second_memory_grow;
  39. procedure second_unreachable;
  40. procedure second_throw_fpcexception;
  41. protected
  42. function first_sqr_real: tnode; override;
  43. public
  44. function pass_typecheck_cpu: tnode; override;
  45. function first_cpu: tnode; override;
  46. procedure pass_generate_code_cpu; override;
  47. procedure second_length;override;
  48. procedure second_sqr_real; override;
  49. end;
  50. implementation
  51. uses
  52. ninl,compinnr,
  53. cpubase,
  54. aasmbase,aasmdata,aasmcpu,
  55. cgbase,cgutils,
  56. hlcgobj,hlcgcpu,
  57. defutil,pass_2,verbose,
  58. symtype,symdef;
  59. {*****************************************************************************
  60. twasminlinenode
  61. *****************************************************************************}
  62. function twasminlinenode.first_abs_real: tnode;
  63. begin
  64. expectloc:=LOC_FPUREGISTER;
  65. result:=nil;
  66. end;
  67. function twasminlinenode.first_int_real: tnode;
  68. begin
  69. expectloc:=LOC_FPUREGISTER;
  70. result:=nil;
  71. end;
  72. function twasminlinenode.first_sqrt_real: tnode;
  73. begin
  74. expectloc:=LOC_FPUREGISTER;
  75. result:=nil;
  76. end;
  77. function twasminlinenode.first_trunc_real: tnode;
  78. begin
  79. expectloc:=LOC_REGISTER;
  80. result:=nil;
  81. end;
  82. function twasminlinenode.first_round_real: tnode;
  83. begin
  84. expectloc:=LOC_REGISTER;
  85. result:=nil;
  86. end;
  87. procedure twasminlinenode.second_abs_real;
  88. begin
  89. secondpass(left);
  90. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  91. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  92. case left.location.size of
  93. OS_F32:
  94. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f32_abs));
  95. OS_F64:
  96. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f64_abs));
  97. else
  98. internalerror(2021092902);
  99. end;
  100. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  101. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  102. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  103. end;
  104. procedure twasminlinenode.second_int_real;
  105. begin
  106. secondpass(left);
  107. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  108. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  109. case left.location.size of
  110. OS_F32:
  111. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f32_trunc));
  112. OS_F64:
  113. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f64_trunc));
  114. else
  115. internalerror(2021092903);
  116. end;
  117. location_reset(location,LOC_FPUREGISTER,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_sqrt_real;
  122. begin
  123. secondpass(left);
  124. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  125. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  126. case left.location.size of
  127. OS_F32:
  128. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f32_sqrt));
  129. OS_F64:
  130. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f64_sqrt));
  131. else
  132. internalerror(2021092901);
  133. end;
  134. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  135. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  136. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  137. end;
  138. procedure twasminlinenode.second_trunc_real;
  139. begin
  140. secondpass(left);
  141. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  142. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  143. case left.location.size of
  144. OS_F32:
  145. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_i64_trunc_f32_s));
  146. OS_F64:
  147. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_i64_trunc_f64_s));
  148. else
  149. internalerror(2021092904);
  150. end;
  151. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  152. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  153. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  154. end;
  155. procedure twasminlinenode.second_round_real;
  156. begin
  157. secondpass(left);
  158. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  159. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  160. case left.location.size of
  161. OS_F32:
  162. begin
  163. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f32_nearest));
  164. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_i64_trunc_f32_s));
  165. end;
  166. OS_F64:
  167. begin
  168. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f64_nearest));
  169. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_i64_trunc_f64_s));
  170. end
  171. else
  172. internalerror(2021092905);
  173. end;
  174. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  175. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  176. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  177. end;
  178. procedure twasminlinenode.second_high;
  179. var
  180. hightype: TWasmBasicType;
  181. begin
  182. secondpass(left);
  183. if not(is_dynamic_array(left.resultdef)) then
  184. Internalerror(2019122801);
  185. { determine the WasmBasicType of the result }
  186. if is_64bit(resultdef) then
  187. hightype:=wbt_i64
  188. else
  189. hightype:=wbt_i32;
  190. { length in dynamic arrays is at offset -sizeof(pint) }
  191. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  192. { 64-bit pointer values need a <>0 comparison to produce a 32-bit int on the stack (0 or 1) for the 'if' instruction.
  193. 32-bit pointer values don't need it, because 'if' already expects and pops a 32-bit int and checks for <>0. }
  194. if is_64bit(left.resultdef) then
  195. begin
  196. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,left.resultdef,0,R_INTREGISTER);
  197. thlcgwasm(hlcg).a_cmp_stack_stack(current_asmdata.CurrAsmList,left.resultdef,OC_NE);
  198. end;
  199. { if not nil }
  200. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[hightype])));
  201. thlcgwasm(hlcg).incblock;
  202. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  203. { volatility of the dyn. array refers to the volatility of the
  204. string pointer, not of the string data }
  205. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  206. { length in dynamic arrays is at offset -ossinttype.size }
  207. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,left.resultdef,ossinttype.size);
  208. { load length }
  209. if ossinttype.size=8 then
  210. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i64_load,0))
  211. else
  212. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_load,0));
  213. { else }
  214. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_else));
  215. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  216. { high=-1 }
  217. thlcgwasm(hlcg).a_load_const_stack(current_asmdata.CurrAsmList,resultdef,-1,R_INTREGISTER);
  218. { endif }
  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. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  223. if location.size in [OS_64,OS_S64] then
  224. begin
  225. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  226. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  227. end
  228. else
  229. {$endif}
  230. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  231. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  232. end;
  233. procedure twasminlinenode.second_memory_size;
  234. begin
  235. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_memory_size));
  236. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  237. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  238. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  239. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  240. end;
  241. procedure twasminlinenode.second_memory_grow;
  242. begin
  243. secondpass(left);
  244. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  245. thlcgwasm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,left.resultdef,left.location.register);
  246. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_memory_grow));
  247. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  248. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  249. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  250. end;
  251. procedure twasminlinenode.second_unreachable;
  252. begin
  253. location_reset(location,LOC_VOID,OS_NO);
  254. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_unreachable));
  255. end;
  256. procedure twasminlinenode.second_throw_fpcexception;
  257. begin
  258. location_reset(location,LOC_VOID,OS_NO);
  259. current_asmdata.CurrAsmList.Concat(taicpu.op_sym(a_throw,current_asmdata.WeakRefAsmSymbol(FPC_EXCEPTION_TAG_SYM,AT_WASM_EXCEPTION_TAG)));
  260. end;
  261. function twasminlinenode.first_sqr_real: tnode;
  262. begin
  263. expectloc:=LOC_FPUREGISTER;
  264. first_sqr_real:=nil;
  265. end;
  266. function twasminlinenode.pass_typecheck_cpu: tnode;
  267. begin
  268. Result:=nil;
  269. case inlinenumber of
  270. in_wasm32_memory_size:
  271. begin
  272. CheckParameters(0);
  273. resultdef:=u32inttype;
  274. end;
  275. in_wasm32_memory_grow:
  276. begin
  277. CheckParameters(1);
  278. resultdef:=u32inttype;
  279. end;
  280. in_wasm32_unreachable:
  281. begin
  282. CheckParameters(0);
  283. resultdef:=voidtype;
  284. end;
  285. in_wasm32_throw_fpcexception:
  286. begin
  287. CheckParameters(0);
  288. resultdef:=voidtype;
  289. end;
  290. else
  291. Result:=inherited pass_typecheck_cpu;
  292. end;
  293. end;
  294. function twasminlinenode.first_cpu: tnode;
  295. begin
  296. Result:=nil;
  297. case inlinenumber of
  298. in_wasm32_memory_size,
  299. in_wasm32_memory_grow:
  300. expectloc:=LOC_REGISTER;
  301. in_wasm32_unreachable,
  302. in_wasm32_throw_fpcexception:
  303. expectloc:=LOC_VOID;
  304. else
  305. Result:=inherited first_cpu;
  306. end;
  307. end;
  308. procedure twasminlinenode.pass_generate_code_cpu;
  309. begin
  310. case inlinenumber of
  311. in_wasm32_memory_size:
  312. second_memory_size;
  313. in_wasm32_memory_grow:
  314. second_memory_grow;
  315. in_wasm32_unreachable:
  316. second_unreachable;
  317. in_wasm32_throw_fpcexception:
  318. second_throw_fpcexception;
  319. else
  320. inherited pass_generate_code_cpu;
  321. end;
  322. end;
  323. procedure twasminlinenode.second_length;
  324. var
  325. lendef : tdef;
  326. href : treference;
  327. extra_slots: LongInt;
  328. begin
  329. secondpass(left);
  330. if is_shortstring(left.resultdef) then
  331. begin
  332. location_copy(location,left.location);
  333. location.size:=OS_8;
  334. end
  335. else
  336. begin
  337. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  338. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  339. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register);
  340. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  341. thlcgwasm(hlcg).incblock;
  342. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  343. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_const,0));
  344. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  345. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  346. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  347. { the length of a widestring is a 32 bit unsigned int. Since every
  348. character occupies 2 bytes, on a 32 bit platform you can express
  349. the maximum length using 31 bits. On a 64 bit platform, it may be
  350. 32 bits. This means that regardless of the platform, a location
  351. with size OS_SINT/ossinttype can hold the length without
  352. overflowing (this code returns an ossinttype value) }
  353. if is_widestring(left.resultdef) then
  354. lendef:=u32inttype
  355. else
  356. lendef:=ossinttype;
  357. { volatility of the ansistring/widestring refers to the volatility of the
  358. string pointer, not of the string data }
  359. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  360. extra_slots:=thlcgwasm(hlcg).prepare_stack_for_ref(current_asmdata.CurrAsmList,href,false);
  361. thlcgwasm(hlcg).a_load_ref_stack(current_asmdata.CurrAsmList,lendef,href,extra_slots);
  362. if is_widestring(left.resultdef) then
  363. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SHR,resultdef,1);
  364. { Dynamic arrays do not have their length attached but their maximum index }
  365. if is_dynamic_array(left.resultdef) then
  366. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_ADD,resultdef,1);
  367. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  368. thlcgwasm(hlcg).decblock;
  369. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  370. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  371. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  372. end;
  373. end;
  374. procedure twasminlinenode.second_sqr_real;
  375. begin
  376. secondpass(left);
  377. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  378. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  379. thlcgwasm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  380. case left.location.size of
  381. OS_F32:
  382. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f32_mul));
  383. OS_F64:
  384. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_f64_mul));
  385. else
  386. internalerror(2021060102);
  387. end;
  388. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  389. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  390. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  391. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  392. end;
  393. begin
  394. cinlinenode:=twasminlinenode;
  395. end.