nwasmset.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. aasmbase;
  23. type
  24. { twasminnode }
  25. twasminnode = class(tcginnode)
  26. protected
  27. function checkgenjumps(out setparts: Tsetparts; out numparts: byte; out use_small: boolean): boolean;override;
  28. end;
  29. { twasmcasenode }
  30. twasmcasenode = class(tcgcasenode)
  31. private
  32. function GetBranchLabel(Block: TNode; out _Label: TAsmLabel): Boolean;
  33. protected
  34. procedure genlinearlist(hp : pcaselabel);override;
  35. procedure genlinearcmplist(hp : pcaselabel);override;
  36. public
  37. procedure pass_generate_code;override;
  38. end;
  39. implementation
  40. uses
  41. globtype,globals,
  42. cpubase,
  43. cgbase,cgutils,
  44. aasmdata,aasmcpu,
  45. hlcgobj,hlcgcpu,
  46. nbas,
  47. symtype,
  48. pass_2,defutil,verbose,constexp;
  49. {*****************************************************************************
  50. TWASMINNODE
  51. *****************************************************************************}
  52. function twasminnode.checkgenjumps(out setparts: Tsetparts; out numparts: byte; out use_small: boolean): boolean;
  53. begin
  54. { call inherited to initialize use_small }
  55. inherited;
  56. result:=false;
  57. end;
  58. {*****************************************************************************
  59. TWASMCASENODE
  60. *****************************************************************************}
  61. function twasmcasenode.GetBranchLabel(Block: TNode; out _Label: TAsmLabel): Boolean;
  62. begin
  63. Result := True;
  64. if not Assigned(Block) then
  65. begin
  66. { Block doesn't exist / is empty }
  67. _Label := endlabel;
  68. Exit;
  69. end;
  70. { These optimisations aren't particularly debugger friendly }
  71. if not (cs_opt_level2 in current_settings.optimizerswitches) then
  72. begin
  73. Result := False;
  74. current_asmdata.getjumplabel(_Label);
  75. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  76. Exit;
  77. end;
  78. while Assigned(Block) do
  79. begin
  80. case Block.nodetype of
  81. nothingn:
  82. begin
  83. _Label := endlabel;
  84. Exit;
  85. end;
  86. goton:
  87. InternalError(2021011801);
  88. blockn:
  89. begin
  90. Block := TBlockNode(Block).Left;
  91. Continue;
  92. end;
  93. statementn:
  94. begin
  95. { If the right node is assigned, then it's a compound block
  96. that can't be simplified, so fall through, set Result to
  97. False and make a new label }
  98. if Assigned(TStatementNode(Block).right) then
  99. Break;
  100. Block := TStatementNode(Block).Left;
  101. Continue;
  102. end;
  103. else
  104. ;
  105. end;
  106. Break;
  107. end;
  108. { Create unique label }
  109. Result := False;
  110. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  111. current_asmdata.getjumplabel(_Label);
  112. end;
  113. procedure twasmcasenode.genlinearlist(hp: pcaselabel);
  114. var
  115. first : boolean;
  116. last : TConstExprInt;
  117. scratch_reg: tregister;
  118. newsize: tcgsize;
  119. newdef: tdef;
  120. procedure gensub(value:tcgint);
  121. begin
  122. { here, since the sub and cmp are separate we need
  123. to move the result before subtract to help
  124. the register allocator
  125. }
  126. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, opsize, hregister, scratch_reg);
  127. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, value, hregister);
  128. end;
  129. procedure genitem(t : pcaselabel);
  130. begin
  131. if assigned(t^.less) then
  132. genitem(t^.less);
  133. { do we need to test the first value? }
  134. if first and (t^._low>get_min_value(left.resultdef)) then
  135. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,tcgint(t^._low.svalue),hregister,elselabel);
  136. if t^._low=t^._high then
  137. begin
  138. if t^._low-last=0 then
  139. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_EQ,0,hregister,blocklabel(t^.blockid))
  140. else
  141. begin
  142. gensub(tcgint(t^._low.svalue-last.svalue));
  143. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,
  144. OC_EQ,tcgint(t^._low.svalue-last.svalue),scratch_reg,blocklabel(t^.blockid));
  145. end;
  146. last:=t^._low;
  147. end
  148. else
  149. begin
  150. { it begins with the smallest label, if the value }
  151. { is even smaller then jump immediately to the }
  152. { ELSE-label }
  153. if first then
  154. begin
  155. { have we to ajust the first value ? }
  156. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  157. gensub(tcgint(t^._low.svalue));
  158. end
  159. else
  160. begin
  161. { if there is no unused label between the last and the }
  162. { present label then the lower limit can be checked }
  163. { immediately. else check the range in between: }
  164. gensub(tcgint(t^._low.svalue-last.svalue));
  165. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize,jmp_lt,tcgint(t^._low.svalue-last.svalue),scratch_reg,elselabel);
  166. end;
  167. gensub(tcgint(t^._high.svalue-t^._low.svalue));
  168. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_le,tcgint(t^._high.svalue-t^._low.svalue),scratch_reg,blocklabel(t^.blockid));
  169. last:=t^._high;
  170. end;
  171. first:=false;
  172. if assigned(t^.greater) then
  173. genitem(t^.greater);
  174. end;
  175. begin
  176. { do we need to generate cmps? }
  177. if (with_sign and (min_label<0)) then
  178. genlinearcmplist(hp)
  179. else
  180. begin
  181. { sign/zero extend the value to a full register before starting to
  182. subtract values, so that on platforms that don't have
  183. subregisters of the same size as the value we don't generate
  184. sign/zero-extensions after every subtraction
  185. make newsize always signed, since we only do this if the size in
  186. bytes of the register is larger than the original opsize, so
  187. the value can always be represented by a larger signed type }
  188. newsize:=tcgsize2signed[reg_cgsize(hregister)];
  189. if tcgsize2size[newsize]>opsize.size then
  190. begin
  191. newdef:=cgsize_orddef(newsize);
  192. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,newdef);
  193. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,newdef,hregister,scratch_reg);
  194. hregister:=scratch_reg;
  195. opsize:=newdef;
  196. end;
  197. if (labelcnt>1) or not(cs_opt_level1 in current_settings.optimizerswitches) then
  198. begin
  199. last:=0;
  200. first:=true;
  201. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  202. genitem(hp);
  203. end
  204. else
  205. begin
  206. { If only one label exists, we can greatly simplify the checks to a simple comparison }
  207. if hp^._low=hp^._high then
  208. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, tcgint(hp^._low.svalue), hregister,blocklabel(hp^.blockid))
  209. else
  210. begin
  211. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  212. gensub(tcgint(hp^._low.svalue));
  213. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_BE, tcgint(hp^._high.svalue-hp^._low.svalue), hregister,blocklabel(hp^.blockid))
  214. end;
  215. end;
  216. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br,elselabel));
  217. end;
  218. end;
  219. procedure twasmcasenode.genlinearcmplist(hp : pcaselabel);
  220. var
  221. last : TConstExprInt;
  222. lastwasrange: boolean;
  223. procedure genitem(t : pcaselabel);
  224. begin
  225. if assigned(t^.less) then
  226. genitem(t^.less);
  227. if t^._low=t^._high then
  228. begin
  229. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, tcgint(t^._low.svalue),hregister, blocklabel(t^.blockid));
  230. { Reset last here, because we've only checked for one value and need to compare
  231. for the next range both the lower and upper bound }
  232. lastwasrange := false;
  233. end
  234. else
  235. begin
  236. { it begins with the smallest label, if the value }
  237. { is even smaller then jump immediately to the }
  238. { ELSE-label }
  239. if not lastwasrange or (t^._low-last>1) then
  240. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_lt, tcgint(t^._low.svalue), hregister, elselabel);
  241. thlcgwasm(hlcg).a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_le, tcgint(t^._high.svalue), hregister, blocklabel(t^.blockid));
  242. last:=t^._high;
  243. lastwasrange := true;
  244. end;
  245. if assigned(t^.greater) then
  246. genitem(t^.greater);
  247. end;
  248. begin
  249. last:=0;
  250. lastwasrange:=false;
  251. genitem(hp);
  252. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br,elselabel));
  253. end;
  254. procedure twasmcasenode.pass_generate_code;
  255. var
  256. oldflowcontrol: tflowcontrol;
  257. ShortcutElse: Boolean;
  258. i: Integer;
  259. begin
  260. location_reset(location,LOC_VOID,OS_NO);
  261. oldflowcontrol := flowcontrol;
  262. include(flowcontrol,fc_inflowcontrol);
  263. current_asmdata.getjumplabel(endlabel);
  264. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  265. { Do some optimisation to deal with empty else blocks }
  266. ShortcutElse := GetBranchLabel(elseblock, elselabel);
  267. for i:=blocks.count-1 downto 0 do
  268. with pcaseblock(blocks[i])^ do
  269. shortcut := GetBranchLabel(statement, blocklabel);
  270. with_sign:=is_signed(left.resultdef);
  271. if with_sign then
  272. begin
  273. jmp_gt:=OC_GT;
  274. jmp_lt:=OC_LT;
  275. jmp_le:=OC_LTE;
  276. end
  277. else
  278. begin
  279. jmp_gt:=OC_A;
  280. jmp_lt:=OC_B;
  281. jmp_le:=OC_BE;
  282. end;
  283. secondpass(left);
  284. if (left.expectloc=LOC_JUMP)<>
  285. (left.location.loc=LOC_JUMP) then
  286. internalerror(2006050501);
  287. { determines the size of the operand }
  288. opsize:=left.resultdef;
  289. { copy the case expression to a register }
  290. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opsize,false);
  291. {$if not defined(cpu64bitalu)}
  292. if def_cgsize(opsize) in [OS_S64,OS_64] then
  293. begin
  294. hregister:=left.location.register64.reglo;
  295. hregister2:=left.location.register64.reghi;
  296. end
  297. else
  298. {$endif not cpu64bitalu and not cpuhighleveltarget}
  299. hregister:=left.location.register;
  300. { we need the min_label always to choose between }
  301. { cmps and subs/decs }
  302. min_label:=case_get_min(labels);
  303. { Generate the jumps }
  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. hlcg.a_label(current_asmdata.CurrAsmList,blocklabel);
  401. secondpass(statement);
  402. { don't come back to case line }
  403. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^;
  404. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_br,endlabel));
  405. end;
  406. end;
  407. { ...and the else block }
  408. if not ShortcutElse then
  409. begin
  410. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  411. hlcg.a_label(current_asmdata.CurrAsmList,elselabel);
  412. end;
  413. if Assigned(elseblock) then
  414. begin
  415. secondpass(elseblock);
  416. end;
  417. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
  418. hlcg.a_label(current_asmdata.CurrAsmList,endlabel);
  419. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  420. end;
  421. begin
  422. cinnode:=twasminnode;
  423. ccasenode:=twasmcasenode;
  424. end.