narmset.pas 18 KB

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