nwasmset.pas 19 KB

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