nwasmset.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. {
  2. Copyright (c) 1998-2002, 2021 by Florian Klaempfl and Nikolay Nikolov
  3. Generate WebAssembly code for in/case 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 nwasmset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nset,ncgset;
  22. type
  23. { twasminnode }
  24. twasminnode = class(tcginnode)
  25. protected
  26. function checkgenjumps(out setparts: Tsetparts; out numparts: byte; out use_small: boolean): boolean;override;
  27. end;
  28. { twasmcasenode }
  29. twasmcasenode = class(tcgcasenode)
  30. private
  31. ElseBr: Integer;
  32. EndBr: Integer;
  33. function GetBranchBr(Block: TNode; out _Br: Integer): Boolean;
  34. protected
  35. function BlockBr(id:longint):Integer;
  36. procedure genlinearlist(hp : pcaselabel);override;
  37. procedure genlinearcmplist(hp : pcaselabel);override;
  38. public
  39. procedure pass_generate_code;override;
  40. end;
  41. implementation
  42. uses
  43. globtype,globals,
  44. cpubase,
  45. cgbase,cgutils,
  46. aasmdata,aasmcpu,
  47. hlcgobj,hlcgcpu,
  48. nbas,
  49. symtype,
  50. pass_2,defutil,verbose,constexp;
  51. {*****************************************************************************
  52. TWASMINNODE
  53. *****************************************************************************}
  54. function twasminnode.checkgenjumps(out setparts: Tsetparts; out numparts: byte; out use_small: boolean): boolean;
  55. begin
  56. { call inherited to initialize use_small }
  57. inherited;
  58. result:=false;
  59. end;
  60. {*****************************************************************************
  61. TWASMCASENODE
  62. *****************************************************************************}
  63. function twasmcasenode.GetBranchBr(Block: TNode; out _Br: Integer): Boolean;
  64. begin
  65. Result := True;
  66. if not Assigned(Block) then
  67. begin
  68. { Block doesn't exist / is empty }
  69. _Br := EndBr;
  70. Exit;
  71. end;
  72. { These optimisations aren't particularly debugger friendly }
  73. if not (cs_opt_level2 in current_settings.optimizerswitches) then
  74. begin
  75. Result := False;
  76. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  77. thlcgwasm(hlcg).incblock;
  78. _Br:=thlcgwasm(hlcg).br_blocks;
  79. Exit;
  80. end;
  81. while Assigned(Block) do
  82. begin
  83. case Block.nodetype of
  84. nothingn:
  85. begin
  86. _Br := EndBr;
  87. Exit;
  88. end;
  89. goton:
  90. InternalError(2021011801);
  91. blockn:
  92. begin
  93. Block := TBlockNode(Block).Left;
  94. Continue;
  95. end;
  96. statementn:
  97. begin
  98. { If the right node is assigned, then it's a compound block
  99. that can't be simplified, so fall through, set Result to
  100. False and make a new label }
  101. if Assigned(TStatementNode(Block).right) then
  102. Break;
  103. Block := TStatementNode(Block).Left;
  104. Continue;
  105. end;
  106. else
  107. ;
  108. end;
  109. Break;
  110. end;
  111. { Create unique label }
  112. Result := False;
  113. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  114. thlcgwasm(hlcg).incblock;
  115. _Br:=thlcgwasm(hlcg).br_blocks;
  116. end;
  117. function twasmcasenode.BlockBr(id: longint): Integer;
  118. begin
  119. if not assigned(blocks[id]) then
  120. internalerror(200411301);
  121. result:=pcaseblock(blocks[id])^.BlockBr;
  122. end;
  123. procedure twasmcasenode.genlinearlist(hp: pcaselabel);
  124. var
  125. first : boolean;
  126. last : TConstExprInt;
  127. scratch_reg: tregister;
  128. newsize: tcgsize;
  129. newdef: tdef;
  130. procedure gensub(value:tcgint);
  131. begin
  132. { here, since the sub and cmp are separate we need
  133. to move the result before subtract to help
  134. the register allocator
  135. }
  136. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, opsize, hregister, scratch_reg);
  137. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, value, hregister);
  138. end;
  139. procedure genitem(t : pcaselabel);
  140. begin
  141. if assigned(t^.less) then
  142. genitem(t^.less);
  143. { do we need to test the first value? }
  144. if first and (t^._low>get_min_value(left.resultdef)) then
  145. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList,opsize,jmp_lt,tcgint(t^._low.svalue),hregister,thlcgwasm(hlcg).br_blocks-ElseBr);
  146. if t^._low=t^._high then
  147. begin
  148. if t^._low-last=0 then
  149. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList,opsize,OC_EQ,0,hregister,thlcgwasm(hlcg).br_blocks-BlockBr(t^.blockid))
  150. else
  151. begin
  152. gensub(tcgint(t^._low.svalue-last.svalue));
  153. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList,opsize,
  154. OC_EQ,tcgint(t^._low.svalue-last.svalue),scratch_reg,thlcgwasm(hlcg).br_blocks-BlockBr(t^.blockid));
  155. end;
  156. last:=t^._low;
  157. end
  158. else
  159. begin
  160. { it begins with the smallest label, if the value }
  161. { is even smaller then jump immediately to the }
  162. { ELSE-label }
  163. if first then
  164. begin
  165. { have we to ajust the first value ? }
  166. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  167. gensub(tcgint(t^._low.svalue));
  168. end
  169. else
  170. begin
  171. { if there is no unused label between the last and the }
  172. { present label then the lower limit can be checked }
  173. { immediately. else check the range in between: }
  174. gensub(tcgint(t^._low.svalue-last.svalue));
  175. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList, opsize,jmp_lt,tcgint(t^._low.svalue-last.svalue),scratch_reg,thlcgwasm(hlcg).br_blocks-ElseBr);
  176. end;
  177. gensub(tcgint(t^._high.svalue-t^._low.svalue));
  178. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList,opsize,jmp_le,tcgint(t^._high.svalue-t^._low.svalue),scratch_reg,thlcgwasm(hlcg).br_blocks-BlockBr(t^.blockid));
  179. last:=t^._high;
  180. end;
  181. first:=false;
  182. if assigned(t^.greater) then
  183. genitem(t^.greater);
  184. end;
  185. begin
  186. { do we need to generate cmps? }
  187. if (with_sign and (min_label<0)) then
  188. genlinearcmplist(hp)
  189. else
  190. begin
  191. { sign/zero extend the value to a full register before starting to
  192. subtract values, so that on platforms that don't have
  193. subregisters of the same size as the value we don't generate
  194. sign/zero-extensions after every subtraction
  195. make newsize always signed, since we only do this if the size in
  196. bytes of the register is larger than the original opsize, so
  197. the value can always be represented by a larger signed type }
  198. newsize:=tcgsize2signed[reg_cgsize(hregister)];
  199. if tcgsize2size[newsize]>opsize.size then
  200. begin
  201. newdef:=cgsize_orddef(newsize);
  202. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,newdef);
  203. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,newdef,hregister,scratch_reg);
  204. hregister:=scratch_reg;
  205. opsize:=newdef;
  206. end;
  207. if (labelcnt>1) or not(cs_opt_level1 in current_settings.optimizerswitches) then
  208. begin
  209. last:=0;
  210. first:=true;
  211. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  212. genitem(hp);
  213. end
  214. else
  215. begin
  216. { If only one label exists, we can greatly simplify the checks to a simple comparison }
  217. if hp^._low=hp^._high then
  218. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList, opsize, OC_EQ, tcgint(hp^._low.svalue), hregister, thlcgwasm(hlcg).br_blocks-BlockBr(hp^.blockid))
  219. else
  220. begin
  221. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  222. gensub(tcgint(hp^._low.svalue));
  223. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList, opsize, OC_BE, tcgint(hp^._high.svalue-hp^._low.svalue), hregister, thlcgwasm(hlcg).br_blocks-BlockBr(hp^.blockid))
  224. end;
  225. end;
  226. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,thlcgwasm(hlcg).br_blocks-ElseBr));
  227. end;
  228. end;
  229. procedure twasmcasenode.genlinearcmplist(hp : pcaselabel);
  230. var
  231. last : TConstExprInt;
  232. lastwasrange: boolean;
  233. procedure genitem(t : pcaselabel);
  234. begin
  235. if assigned(t^.less) then
  236. genitem(t^.less);
  237. if t^._low=t^._high then
  238. begin
  239. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList, opsize, OC_EQ, tcgint(t^._low.svalue),hregister, thlcgwasm(hlcg).br_blocks-BlockBr(t^.blockid));
  240. { Reset last here, because we've only checked for one value and need to compare
  241. for the next range both the lower and upper bound }
  242. lastwasrange := false;
  243. end
  244. else
  245. begin
  246. { it begins with the smallest label, if the value }
  247. { is even smaller then jump immediately to the }
  248. { ELSE-label }
  249. if not lastwasrange or (t^._low-last>1) then
  250. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList, opsize, jmp_lt, tcgint(t^._low.svalue), hregister, thlcgwasm(hlcg).br_blocks-ElseBr);
  251. thlcgwasm(hlcg).a_cmp_const_reg_br(current_asmdata.CurrAsmList, opsize, jmp_le, tcgint(t^._high.svalue), hregister, thlcgwasm(hlcg).br_blocks-BlockBr(t^.blockid));
  252. last:=t^._high;
  253. lastwasrange := true;
  254. end;
  255. if assigned(t^.greater) then
  256. genitem(t^.greater);
  257. end;
  258. begin
  259. last:=0;
  260. lastwasrange:=false;
  261. genitem(hp);
  262. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,thlcgwasm(hlcg).br_blocks-ElseBr));
  263. end;
  264. procedure twasmcasenode.pass_generate_code;
  265. var
  266. oldflowcontrol: tflowcontrol;
  267. ShortcutElse: Boolean;
  268. i: Integer;
  269. begin
  270. location_reset(location,LOC_VOID,OS_NO);
  271. oldflowcontrol := flowcontrol;
  272. include(flowcontrol,fc_inflowcontrol);
  273. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  274. thlcgwasm(hlcg).incblock;
  275. EndBr:=thlcgwasm(hlcg).br_blocks;
  276. { Do some optimisation to deal with empty else blocks }
  277. ShortcutElse := GetBranchBr(elseblock, ElseBr);
  278. for i:=blocks.count-1 downto 0 do
  279. with pcaseblock(blocks[i])^ do
  280. shortcut := GetBranchBr(statement, BlockBr);
  281. with_sign:=is_signed(left.resultdef);
  282. if with_sign then
  283. begin
  284. jmp_gt:=OC_GT;
  285. jmp_lt:=OC_LT;
  286. jmp_le:=OC_LTE;
  287. end
  288. else
  289. begin
  290. jmp_gt:=OC_A;
  291. jmp_lt:=OC_B;
  292. jmp_le:=OC_BE;
  293. end;
  294. secondpass(left);
  295. if (left.expectloc=LOC_JUMP)<>
  296. (left.location.loc=LOC_JUMP) then
  297. internalerror(2006050501);
  298. { determines the size of the operand }
  299. opsize:=left.resultdef;
  300. { copy the case expression to a register }
  301. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opsize,false);
  302. {$if not defined(cpu64bitalu)}
  303. if def_cgsize(opsize) in [OS_S64,OS_64] then
  304. begin
  305. hregister:=left.location.register64.reglo;
  306. hregister2:=left.location.register64.reghi;
  307. end
  308. else
  309. {$endif not cpu64bitalu and not cpuhighleveltarget}
  310. hregister:=left.location.register;
  311. { we need the min_label always to choose between }
  312. { cmps and subs/decs }
  313. min_label:=case_get_min(labels);
  314. { Generate the jumps }
  315. {$ifdef OLDREGVARS}
  316. load_all_regvars(current_asmdata.CurrAsmList);
  317. {$endif OLDREGVARS}
  318. {$if not defined(cpu64bitalu)}
  319. if def_cgsize(opsize) in [OS_64,OS_S64] then
  320. genlinearcmplist(labels)
  321. else
  322. {$endif not cpu64bitalu and not cpuhighleveltarget}
  323. begin
  324. //if cs_opt_level1 in current_settings.optimizerswitches then
  325. // begin
  326. // { procedures are empirically passed on }
  327. // { consumption can also be calculated }
  328. // { but does it pay on the different }
  329. // { processors? }
  330. // { moreover can the size only be appro- }
  331. // { ximated as it is not known if rel8, }
  332. // { rel16 or rel32 jumps are used }
  333. //
  334. // max_label := case_get_max(labels);
  335. //
  336. // { can we omit the range check of the jump table ? }
  337. // getrange(left.resultdef,lv,hv);
  338. // jumptable_no_range:=(lv=min_label) and (hv=max_label);
  339. //
  340. // distv:=max_label-min_label;
  341. // if distv>=0 then
  342. // dist:=distv.uvalue
  343. // else
  344. // dist:=asizeuint(-distv.svalue);
  345. //
  346. // { optimize for size ? }
  347. // if cs_opt_size in current_settings.optimizerswitches then
  348. // begin
  349. // if has_jumptable and
  350. // (min_label>=int64(low(aint))) and
  351. // (max_label<=high(aint)) and
  352. // not((labelcnt<=2) or
  353. // (distv.svalue<0) or
  354. // (dist>3*labelcnt)) then
  355. // begin
  356. // { if the labels less or more a continuum then }
  357. // genjumptable(labels,min_label.svalue,max_label.svalue);
  358. // end
  359. // else
  360. // begin
  361. // { a linear list is always smaller than a jump tree }
  362. // genlinearlist(labels);
  363. // end;
  364. // end
  365. // else
  366. // begin
  367. // max_dist:=4*labelcoverage;
  368. //
  369. // { Don't allow jump tables to get too large }
  370. // if max_dist>4*labelcnt then
  371. // max_dist:=min(max_dist,2048);
  372. //
  373. // if jumptable_no_range then
  374. // max_linear_list:=4
  375. // else
  376. // max_linear_list:=2;
  377. //
  378. // { allow processor specific values }
  379. // optimizevalues(max_linear_list,max_dist);
  380. //
  381. // if (labelcnt<=max_linear_list) then
  382. // genlinearlist(labels)
  383. // else
  384. // begin
  385. // if (has_jumptable) and
  386. // (dist<max_dist) and
  387. // (min_label>=int64(low(aint))) and
  388. // (max_label<=high(aint)) then
  389. // genjumptable(labels,min_label.svalue,max_label.svalue)
  390. // { value has been determined on an i7-4770 using a random case with random values
  391. // if more values are known, this can be handled depending on the target CPU
  392. //
  393. // Testing on a Core 2 Duo E6850 as well as on a Raspi3 showed also, that 64 is
  394. // a good value }
  395. // else if labelcnt>=64 then
  396. // genjmptree(labels)
  397. // else
  398. // genlinearlist(labels);
  399. // end;
  400. // end;
  401. // end
  402. //else
  403. { it's always not bad }
  404. genlinearlist(labels);
  405. end;
  406. { generate the instruction blocks }
  407. for i:=0 to blocks.count-1 do with pcaseblock(blocks[i])^ do
  408. begin
  409. { If the labels are not equal, then the block label has been shortcut to point elsewhere,
  410. so there's no need to implement it }
  411. if not shortcut then
  412. begin
  413. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  414. thlcgwasm(hlcg).decblock;
  415. secondpass(statement);
  416. { don't come back to case line }
  417. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^;
  418. {$ifdef OLDREGVARS}
  419. load_all_regvars(current_asmdata.CurrAsmList);
  420. {$endif OLDREGVARS}
  421. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,thlcgwasm(hlcg).br_blocks-EndBr));
  422. end;
  423. end;
  424. { ...and the else block }
  425. if not ShortcutElse then
  426. begin
  427. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  428. thlcgwasm(hlcg).decblock;
  429. end;
  430. if Assigned(elseblock) then
  431. begin
  432. secondpass(elseblock);
  433. {$ifdef OLDREGVARS}
  434. load_all_regvars(current_asmdata.CurrAsmList);
  435. {$endif OLDREGVARS}
  436. end;
  437. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  438. thlcgwasm(hlcg).decblock;
  439. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  440. end;
  441. begin
  442. cinnode:=twasminnode;
  443. ccasenode:=twasmcasenode;
  444. end.