nwasmset.pas 18 KB

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