narmset.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,opdef,1,hregister);
  95. if GenerateThumbCode or GenerateThumb2Code then
  96. begin
  97. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_SHL,opdef,left.location.register,hregister);
  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 : int64;
  164. begin
  165. if assigned(t^.less) then
  166. genitem_thumb2(list,t^.less);
  167. { fill possible hole }
  168. i:=last.svalue+1;
  169. while i<=t^._low.svalue-1 do
  170. begin
  171. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
  172. i:=i+1;
  173. end;
  174. i:=t^._low.svalue;
  175. while i<=t^._high.svalue do
  176. begin
  177. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
  178. i:=i+1;
  179. end;
  180. last:=t^._high.svalue;
  181. if assigned(t^.greater) then
  182. genitem_thumb2(list,t^.greater);
  183. end;
  184. begin
  185. opcgsize:=def_cgsize(opsize);
  186. if not(jumptable_no_range) then
  187. begin
  188. { case expr less than min_ => goto elselabel }
  189. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
  190. { case expr greater than max_ => goto elselabel }
  191. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
  192. end;
  193. { make it a 32bit register }
  194. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  195. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);
  196. if GenerateThumb2Code then
  197. begin
  198. if cs_create_pic in current_settings.moduleswitches then
  199. internalerror(2013082101);
  200. { adjust index }
  201. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  202. { create reference and generate jump table }
  203. reference_reset(href,4,[]);
  204. href.base:=NR_PC;
  205. href.index:=indexreg;
  206. href.shiftmode:=SM_LSL;
  207. href.shiftimm:=1;
  208. current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
  209. { generate jump table }
  210. current_asmdata.getjumplabel(tablelabel);
  211. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  212. last:=min_;
  213. genitem_thumb2(current_asmdata.CurrAsmList,hp);
  214. end
  215. else if GenerateThumbCode then
  216. begin
  217. if cs_create_pic in current_settings.moduleswitches then
  218. internalerror(2013082102);
  219. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  220. current_asmdata.getaddrlabel(tablelabel);
  221. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,indexreg);
  222. basereg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
  223. reference_reset_symbol(href,tablelabel,0,4,[]);
  224. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, href, basereg);
  225. reference_reset(href,0,[]);
  226. href.base:=basereg;
  227. href.index:=indexreg;
  228. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
  229. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_ADDR, OS_ADDR, href, tmpreg);
  230. { do not use BX here to avoid switching into arm mode }
  231. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_MOV, NR_PC, tmpreg));
  232. current_asmdata.CurrAsmList.Concat(tai_align.Create(4));
  233. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  234. { generate jump table }
  235. last:=min_;
  236. genitem(current_asmdata.CurrAsmList,hp);
  237. end
  238. else
  239. begin
  240. { adjust index }
  241. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,
  242. min_+ord(not(cs_create_pic in current_settings.moduleswitches)),
  243. indexreg,indexreg);
  244. { create reference and generate jump table }
  245. reference_reset(href,4,[]);
  246. href.base:=NR_PC;
  247. href.index:=indexreg;
  248. href.shiftmode:=SM_LSL;
  249. href.shiftimm:=2;
  250. if cs_create_pic in current_settings.moduleswitches then
  251. begin
  252. picoffset:=-8;
  253. current_asmdata.getaddrlabel(piclabel);
  254. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  255. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,indexreg);
  256. cg.a_label(current_asmdata.CurrAsmList,piclabel);
  257. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,indexreg,NR_PC);
  258. end
  259. else
  260. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  261. { generate jump table }
  262. last:=min_;
  263. genitem(current_asmdata.CurrAsmList,hp);
  264. end;
  265. end;
  266. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  267. var
  268. first : boolean;
  269. lastrange : boolean;
  270. last : TConstExprInt;
  271. cond_lt,cond_le : tresflags;
  272. opcgsize : tcgsize;
  273. procedure genitem(t : pcaselabel);
  274. begin
  275. if assigned(t^.less) then
  276. genitem(t^.less);
  277. { need we to test the first value }
  278. if first and (t^._low>get_min_value(left.resultdef)) then
  279. begin
  280. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  281. end;
  282. if t^._low=t^._high then
  283. begin
  284. if t^._low-last=0 then
  285. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  286. else
  287. begin
  288. tbasecgarm(cg).cgsetflags:=true;
  289. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  290. then genlinearlist wouldn't be used }
  291. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  292. tbasecgarm(cg).cgsetflags:=false;
  293. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  294. end;
  295. last:=t^._low;
  296. lastrange:=false;
  297. end
  298. else
  299. begin
  300. { it begins with the smallest label, if the value }
  301. { is even smaller then jump immediately to the }
  302. { ELSE-label }
  303. if first then
  304. begin
  305. { have we to ajust the first value ? }
  306. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  307. begin
  308. tbasecgarm(cg).cgsetflags:=true;
  309. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  310. then genlinearlist wouldn't be use }
  311. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
  312. tbasecgarm(cg).cgsetflags:=false;
  313. end;
  314. end
  315. else
  316. begin
  317. { if there is no unused label between the last and the }
  318. { present label then the lower limit can be checked }
  319. { immediately. else check the range in between: }
  320. tbasecgarm(cg).cgsetflags:=true;
  321. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  322. then genlinearlist wouldn't be use }
  323. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  324. tbasecgarm(cg).cgsetflags:=false;
  325. { no jump necessary here if the new range starts at }
  326. { at the value following the previous one }
  327. if ((t^._low-last) <> 1) or
  328. (not lastrange) then
  329. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  330. end;
  331. tbasecgarm(cg).cgsetflags:=true;
  332. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  333. then genlinearlist wouldn't be use }
  334. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_32,aint(int64(t^._high-t^._low)),hregister);
  335. tbasecgarm(cg).cgsetflags:=false;
  336. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  337. last:=t^._high;
  338. lastrange:=true;
  339. end;
  340. first:=false;
  341. if assigned(t^.greater) then
  342. genitem(t^.greater);
  343. end;
  344. begin
  345. opcgsize:=def_cgsize(opsize);
  346. if with_sign then
  347. begin
  348. cond_lt:=F_LT;
  349. cond_le:=F_LE;
  350. end
  351. else
  352. begin
  353. cond_lt:=F_CC;
  354. cond_le:=F_LS;
  355. end;
  356. { do we need to generate cmps? }
  357. if (with_sign and (min_label<0)) then
  358. genlinearcmplist(hp)
  359. else
  360. begin
  361. last:=0;
  362. lastrange:=false;
  363. first:=true;
  364. genitem(hp);
  365. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  366. end;
  367. end;
  368. procedure tarmcasenode.genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);
  369. var
  370. lesslabel,greaterlabel : tasmlabel;
  371. cond_gt: TResFlags;
  372. cmplow : Boolean;
  373. begin
  374. if with_sign then
  375. cond_gt:=F_GT
  376. else
  377. cond_gt:=F_HI;
  378. current_asmdata.CurrAsmList.concat(cai_align.Create(current_settings.alignment.jumpalign));
  379. cg.a_label(current_asmdata.CurrAsmList,p^.labellabel);
  380. { calculate labels for left and right }
  381. if p^.less=nil then
  382. lesslabel:=elselabel
  383. else
  384. lesslabel:=p^.less^.labellabel;
  385. if p^.greater=nil then
  386. greaterlabel:=elselabel
  387. else
  388. greaterlabel:=p^.greater^.labellabel;
  389. { calculate labels for left and right }
  390. { no range label: }
  391. if p^._low=p^._high then
  392. begin
  393. if greaterlabel=lesslabel then
  394. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_NE,p^._low,hregister,lesslabel)
  395. else
  396. begin
  397. cmplow:=p^._low-1<>parentvalue;
  398. if cmplow then
  399. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
  400. if p^._high+1<>parentvalue then
  401. begin
  402. if cmplow then
  403. hlcg.a_jmp_flags(current_asmdata.CurrAsmList,cond_gt,greaterlabel)
  404. else
  405. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._low,hregister,greaterlabel);
  406. end;
  407. end;
  408. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  409. end
  410. else
  411. begin
  412. if p^._low-1<>parentvalue then
  413. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
  414. if p^._high+1<>parentvalue then
  415. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._high,hregister,greaterlabel);
  416. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  417. end;
  418. if assigned(p^.less) then
  419. genjmptreeentry(p^.less,p^._low);
  420. if assigned(p^.greater) then
  421. genjmptreeentry(p^.greater,p^._high);
  422. end;
  423. begin
  424. cinnode:=tarminnode;
  425. ccasenode:=tarmcasenode;
  426. end.