narmset.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate arm assembler for in set/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 narmset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,constexp,
  22. symtype,
  23. cgbase,
  24. node,nset,pass_1,ncgset;
  25. type
  26. { tarminnode }
  27. tarminnode = class(tcginnode)
  28. function pass_1: tnode; override;
  29. procedure in_smallset(opdef: tdef; setbase: aint); override;
  30. end;
  31. tarmcasenode = class(tcgcasenode)
  32. procedure optimizevalues(var max_linear_list:int64;var max_dist:qword);override;
  33. function has_jumptable : boolean;override;
  34. procedure genjumptable(hp : pcaselabel;min_,max_ : int64);override;
  35. procedure genlinearlist(hp : pcaselabel);override;
  36. procedure genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);override;
  37. end;
  38. implementation
  39. uses
  40. verbose,globals,defutil,systems,
  41. aasmbase,aasmtai,aasmdata,aasmcpu,
  42. cpubase,cpuinfo,
  43. cgutils,cgobj,ncgutil,
  44. cgcpu,hlcgobj;
  45. {*****************************************************************************
  46. TARMINNODE
  47. *****************************************************************************}
  48. function tarminnode.pass_1: tnode;
  49. var
  50. setparts: Tsetparts;
  51. numparts: byte;
  52. use_small: boolean;
  53. begin
  54. result:=inherited pass_1;
  55. if not(assigned(result)) then
  56. begin
  57. if not(checkgenjumps(setparts,numparts,use_small)) and
  58. use_small and
  59. (target_info.endian=endian_little) then
  60. expectloc:=LOC_FLAGS;
  61. end;
  62. end;
  63. procedure tarminnode.in_smallset(opdef: tdef; setbase: aint);
  64. var
  65. so : tshifterop;
  66. hregister : tregister;
  67. begin
  68. { the code below needs changes for big endian targets (they start
  69. counting from the most significant bit)
  70. }
  71. if target_info.endian=endian_big then
  72. begin
  73. inherited;
  74. exit;
  75. end;
  76. location_reset(location,LOC_FLAGS,OS_NO);
  77. location.resflags:=F_NE;
  78. if (left.location.loc=LOC_CONSTANT) and not(GenerateThumbCode) then
  79. begin
  80. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
  81. right.resultdef, right.resultdef, true);
  82. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  83. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_TST,right.location.register,1 shl (left.location.value-setbase)));
  84. end
  85. else
  86. begin
  87. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location,
  88. left.resultdef, opdef, true);
  89. register_maybe_adjust_setbase(current_asmdata.CurrAsmList, opdef,
  90. left.location, setbase);
  91. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
  92. right.resultdef, right.resultdef, true);
  93. hregister:=hlcg.getintregister(current_asmdata.CurrAsmList, opdef);
  94. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_MOV,hregister,1));
  95. if GenerateThumbCode or GenerateThumb2Code then
  96. begin
  97. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_LSL,hregister,left.location.register));
  98. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  99. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_TST,right.location.register,hregister));
  100. end
  101. else
  102. begin
  103. shifterop_reset(so);
  104. so.rs:=left.location.register;
  105. so.shiftmode:=SM_LSL;
  106. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  107. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_TST,right.location.register,hregister,so));
  108. end;
  109. end;
  110. end;
  111. {*****************************************************************************
  112. TARMCASENODE
  113. *****************************************************************************}
  114. procedure tarmcasenode.optimizevalues(var max_linear_list:int64;var max_dist:qword);
  115. begin
  116. inc(max_linear_list,2)
  117. end;
  118. function tarmcasenode.has_jumptable : boolean;
  119. begin
  120. has_jumptable:=true;
  121. end;
  122. procedure tarmcasenode.genjumptable(hp : pcaselabel;min_,max_ : int64);
  123. var
  124. last : TConstExprInt;
  125. tmpreg,
  126. basereg,
  127. indexreg : tregister;
  128. href : treference;
  129. tablelabel, piclabel : TAsmLabel;
  130. opcgsize : tcgsize;
  131. picoffset : int64;
  132. procedure genitem(list:TAsmList;t : pcaselabel);
  133. var
  134. i : int64;
  135. begin
  136. if assigned(t^.less) then
  137. genitem(list,t^.less);
  138. { fill possible hole }
  139. i:=last+1;
  140. while i<=t^._low-1 do
  141. begin
  142. if cs_create_pic in current_settings.moduleswitches then
  143. list.concat(Tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,elselabel,picoffset))
  144. else
  145. list.concat(Tai_const.Create_sym(elselabel));
  146. i:=i+1;
  147. end;
  148. i:=t^._low;
  149. while i<=t^._high do
  150. begin
  151. if cs_create_pic in current_settings.moduleswitches then
  152. list.concat(Tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,blocklabel(t^.blockid),picoffset))
  153. else
  154. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  155. i:=i+1;
  156. end;
  157. last:=t^._high;
  158. if assigned(t^.greater) then
  159. genitem(list,t^.greater);
  160. end;
  161. procedure genitem_thumb2(list:TAsmList;t : pcaselabel);
  162. var
  163. i : aint;
  164. begin
  165. if assigned(t^.less) then
  166. genitem_thumb2(list,t^.less);
  167. { fill possible hole }
  168. for i:=last.svalue+1 to t^._low.svalue-1 do
  169. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
  170. for i:=t^._low.svalue to t^._high.svalue do
  171. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
  172. last:=t^._high.svalue;
  173. if assigned(t^.greater) then
  174. genitem_thumb2(list,t^.greater);
  175. end;
  176. begin
  177. opcgsize:=def_cgsize(opsize);
  178. if not(jumptable_no_range) then
  179. begin
  180. { case expr less than min_ => goto elselabel }
  181. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
  182. { case expr greater than max_ => goto elselabel }
  183. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
  184. end;
  185. { make it a 32bit register }
  186. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  187. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);
  188. if GenerateThumb2Code then
  189. begin
  190. if cs_create_pic in current_settings.moduleswitches then
  191. internalerror(2013082101);
  192. { adjust index }
  193. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  194. { create reference and generate jump table }
  195. reference_reset(href,4,[]);
  196. href.base:=NR_PC;
  197. href.index:=indexreg;
  198. href.shiftmode:=SM_LSL;
  199. href.shiftimm:=1;
  200. current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
  201. { generate jump table }
  202. current_asmdata.getjumplabel(tablelabel);
  203. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  204. last:=min_;
  205. genitem_thumb2(current_asmdata.CurrAsmList,hp);
  206. end
  207. else if GenerateThumbCode then
  208. begin
  209. if cs_create_pic in current_settings.moduleswitches then
  210. internalerror(2013082102);
  211. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  212. current_asmdata.getaddrlabel(tablelabel);
  213. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,indexreg);
  214. basereg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
  215. reference_reset_symbol(href,tablelabel,0,4,[]);
  216. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, href, basereg);
  217. reference_reset(href,0,[]);
  218. href.base:=basereg;
  219. href.index:=indexreg;
  220. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
  221. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_ADDR, OS_ADDR, href, tmpreg);
  222. { do not use BX here to avoid switching into arm mode }
  223. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_MOV, NR_PC, tmpreg));
  224. current_asmdata.CurrAsmList.Concat(tai_align.Create(4));
  225. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  226. { generate jump table }
  227. last:=min_;
  228. genitem(current_asmdata.CurrAsmList,hp);
  229. end
  230. else
  231. begin
  232. { adjust index }
  233. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,
  234. min_+ord(not(cs_create_pic in current_settings.moduleswitches)),
  235. indexreg,indexreg);
  236. { create reference and generate jump table }
  237. reference_reset(href,4,[]);
  238. href.base:=NR_PC;
  239. href.index:=indexreg;
  240. href.shiftmode:=SM_LSL;
  241. href.shiftimm:=2;
  242. if cs_create_pic in current_settings.moduleswitches then
  243. begin
  244. picoffset:=-8;
  245. current_asmdata.getaddrlabel(piclabel);
  246. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  247. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,indexreg);
  248. cg.a_label(current_asmdata.CurrAsmList,piclabel);
  249. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,indexreg,NR_PC);
  250. end
  251. else
  252. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  253. { generate jump table }
  254. last:=min_;
  255. genitem(current_asmdata.CurrAsmList,hp);
  256. end;
  257. end;
  258. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  259. var
  260. first : boolean;
  261. lastrange : boolean;
  262. last : TConstExprInt;
  263. cond_lt,cond_le : tresflags;
  264. opcgsize : tcgsize;
  265. procedure genitem(t : pcaselabel);
  266. begin
  267. if assigned(t^.less) then
  268. genitem(t^.less);
  269. { need we to test the first value }
  270. if first and (t^._low>get_min_value(left.resultdef)) then
  271. begin
  272. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  273. end;
  274. if t^._low=t^._high then
  275. begin
  276. if t^._low-last=0 then
  277. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  278. else
  279. begin
  280. tbasecgarm(cg).cgsetflags:=true;
  281. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  282. then genlinearlist wouldn't be used }
  283. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  284. tbasecgarm(cg).cgsetflags:=false;
  285. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  286. end;
  287. last:=t^._low;
  288. lastrange:=false;
  289. end
  290. else
  291. begin
  292. { it begins with the smallest label, if the value }
  293. { is even smaller then jump immediately to the }
  294. { ELSE-label }
  295. if first then
  296. begin
  297. { have we to ajust the first value ? }
  298. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  299. begin
  300. tbasecgarm(cg).cgsetflags:=true;
  301. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  302. then genlinearlist wouldn't be use }
  303. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
  304. tbasecgarm(cg).cgsetflags:=false;
  305. end;
  306. end
  307. else
  308. begin
  309. { if there is no unused label between the last and the }
  310. { present label then the lower limit can be checked }
  311. { immediately. else check the range in between: }
  312. tbasecgarm(cg).cgsetflags:=true;
  313. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  314. then genlinearlist wouldn't be use }
  315. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  316. tbasecgarm(cg).cgsetflags:=false;
  317. { no jump necessary here if the new range starts at }
  318. { at the value following the previous one }
  319. if ((t^._low-last) <> 1) or
  320. (not lastrange) then
  321. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  322. end;
  323. tbasecgarm(cg).cgsetflags:=true;
  324. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  325. then genlinearlist wouldn't be use }
  326. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_32,aint(int64(t^._high-t^._low)),hregister);
  327. tbasecgarm(cg).cgsetflags:=false;
  328. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  329. last:=t^._high;
  330. lastrange:=true;
  331. end;
  332. first:=false;
  333. if assigned(t^.greater) then
  334. genitem(t^.greater);
  335. end;
  336. begin
  337. opcgsize:=def_cgsize(opsize);
  338. if with_sign then
  339. begin
  340. cond_lt:=F_LT;
  341. cond_le:=F_LE;
  342. end
  343. else
  344. begin
  345. cond_lt:=F_CC;
  346. cond_le:=F_LS;
  347. end;
  348. { do we need to generate cmps? }
  349. if (with_sign and (min_label<0)) then
  350. genlinearcmplist(hp)
  351. else
  352. begin
  353. last:=0;
  354. lastrange:=false;
  355. first:=true;
  356. genitem(hp);
  357. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  358. end;
  359. end;
  360. procedure tarmcasenode.genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);
  361. var
  362. lesslabel,greaterlabel : tasmlabel;
  363. cond_gt: TResFlags;
  364. cmplow : Boolean;
  365. begin
  366. if with_sign then
  367. cond_gt:=F_GT
  368. else
  369. cond_gt:=F_HI;
  370. current_asmdata.CurrAsmList.concat(cai_align.Create(current_settings.alignment.jumpalign));
  371. cg.a_label(current_asmdata.CurrAsmList,p^.labellabel);
  372. { calculate labels for left and right }
  373. if p^.less=nil then
  374. lesslabel:=elselabel
  375. else
  376. lesslabel:=p^.less^.labellabel;
  377. if p^.greater=nil then
  378. greaterlabel:=elselabel
  379. else
  380. greaterlabel:=p^.greater^.labellabel;
  381. { calculate labels for left and right }
  382. { no range label: }
  383. if p^._low=p^._high then
  384. begin
  385. if greaterlabel=lesslabel then
  386. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_NE,p^._low,hregister,lesslabel)
  387. else
  388. begin
  389. cmplow:=p^._low-1<>parentvalue;
  390. if cmplow then
  391. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
  392. if p^._high+1<>parentvalue then
  393. begin
  394. if cmplow then
  395. hlcg.a_jmp_flags(current_asmdata.CurrAsmList,cond_gt,greaterlabel)
  396. else
  397. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._low,hregister,greaterlabel);
  398. end;
  399. end;
  400. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  401. end
  402. else
  403. begin
  404. if p^._low-1<>parentvalue then
  405. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
  406. if p^._high+1<>parentvalue then
  407. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._high,hregister,greaterlabel);
  408. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  409. end;
  410. if assigned(p^.less) then
  411. genjmptreeentry(p^.less,p^._low);
  412. if assigned(p^.greater) then
  413. genjmptreeentry(p^.greater,p^._high);
  414. end;
  415. begin
  416. cinnode:=tarminnode;
  417. ccasenode:=tarmcasenode;
  418. end.