narmset.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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,
  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(uopsize: tcgsize; 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. end;
  37. implementation
  38. uses
  39. verbose,globals,constexp,defutil,
  40. aasmbase,aasmtai,aasmdata,aasmcpu,
  41. cpubase,cpuinfo,
  42. cgutils,cgobj,ncgutil,
  43. cgcpu,hlcgobj;
  44. {*****************************************************************************
  45. TARMINNODE
  46. *****************************************************************************}
  47. function tarminnode.pass_1: tnode;
  48. var
  49. setparts: Tsetparts;
  50. numparts: byte;
  51. use_small: boolean;
  52. begin
  53. result:=inherited pass_1;
  54. if not(assigned(result)) then
  55. begin
  56. if not(checkgenjumps(setparts,numparts,use_small)) and
  57. use_small then
  58. expectloc:=LOC_FLAGS;
  59. end;
  60. end;
  61. procedure tarminnode.in_smallset(uopsize: tcgsize; opdef: tdef; setbase: aint);
  62. var
  63. so : tshifterop;
  64. hregister : tregister;
  65. begin
  66. location_reset(location,LOC_FLAGS,OS_NO);
  67. location.resflags:=F_NE;
  68. if (left.location.loc=LOC_CONSTANT) and not(GenerateThumbCode) then
  69. begin
  70. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
  71. right.resultdef, right.resultdef, true);
  72. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  73. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_TST,right.location.register,1 shl (left.location.value-setbase)));
  74. end
  75. else
  76. begin
  77. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location,
  78. left.resultdef, opdef, true);
  79. register_maybe_adjust_setbase(current_asmdata.CurrAsmList, left.location,
  80. setbase);
  81. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
  82. right.resultdef, right.resultdef, true);
  83. hregister:=cg.getintregister(current_asmdata.CurrAsmList, uopsize);
  84. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_MOV,hregister,1));
  85. if GenerateThumbCode or GenerateThumb2Code then
  86. begin
  87. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_LSL,hregister,left.location.register));
  88. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  89. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_TST,right.location.register,hregister));
  90. end
  91. else
  92. begin
  93. shifterop_reset(so);
  94. so.rs:=left.location.register;
  95. so.shiftmode:=SM_LSL;
  96. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  97. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_TST,right.location.register,hregister,so));
  98. end;
  99. end;
  100. end;
  101. {*****************************************************************************
  102. TARMCASENODE
  103. *****************************************************************************}
  104. procedure tarmcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  105. begin
  106. inc(max_linear_list,2)
  107. end;
  108. function tarmcasenode.has_jumptable : boolean;
  109. begin
  110. has_jumptable:=true;
  111. end;
  112. procedure tarmcasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  113. var
  114. last : TConstExprInt;
  115. basereg,
  116. indexreg : tregister;
  117. href : treference;
  118. tablelabel, piclabel : TAsmLabel;
  119. opcgsize : tcgsize;
  120. picoffset : int64;
  121. procedure genitem(list:TAsmList;t : pcaselabel);
  122. var
  123. i : aint;
  124. begin
  125. if assigned(t^.less) then
  126. genitem(list,t^.less);
  127. { fill possible hole }
  128. for i:=last.svalue+1 to t^._low.svalue-1 do
  129. if cs_create_pic in current_settings.moduleswitches then
  130. list.concat(Tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,elselabel,picoffset))
  131. else
  132. list.concat(Tai_const.Create_sym(elselabel));
  133. for i:=t^._low.svalue to t^._high.svalue do
  134. if cs_create_pic in current_settings.moduleswitches then
  135. list.concat(Tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,blocklabel(t^.blockid),picoffset))
  136. else
  137. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  138. last:=t^._high.svalue;
  139. if assigned(t^.greater) then
  140. genitem(list,t^.greater);
  141. end;
  142. procedure genitem_thumb2(list:TAsmList;t : pcaselabel);
  143. var
  144. i : aint;
  145. begin
  146. if assigned(t^.less) then
  147. genitem_thumb2(list,t^.less);
  148. { fill possible hole }
  149. for i:=last.svalue+1 to t^._low.svalue-1 do
  150. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
  151. for i:=t^._low.svalue to t^._high.svalue do
  152. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
  153. last:=t^._high.svalue;
  154. if assigned(t^.greater) then
  155. genitem_thumb2(list,t^.greater);
  156. end;
  157. begin
  158. opcgsize:=def_cgsize(opsize);
  159. if not(jumptable_no_range) then
  160. begin
  161. { case expr less than min_ => goto elselabel }
  162. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
  163. { case expr greater than max_ => goto elselabel }
  164. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
  165. end;
  166. { make it a 32bit register }
  167. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  168. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);
  169. if GenerateThumb2Code then
  170. begin
  171. if cs_create_pic in current_settings.moduleswitches then
  172. internalerror(2013082101);
  173. { adjust index }
  174. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  175. { create reference and generate jump table }
  176. reference_reset(href,4);
  177. href.base:=NR_PC;
  178. href.index:=indexreg;
  179. href.shiftmode:=SM_LSL;
  180. href.shiftimm:=1;
  181. current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
  182. { generate jump table }
  183. current_asmdata.getjumplabel(tablelabel);
  184. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  185. last:=min_;
  186. genitem_thumb2(current_asmdata.CurrAsmList,hp);
  187. end
  188. else if GenerateThumbCode then
  189. begin
  190. if cs_create_pic in current_settings.moduleswitches then
  191. internalerror(2013082102);
  192. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_+1,indexreg,indexreg);
  193. current_asmdata.getaddrlabel(tablelabel);
  194. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,indexreg);
  195. basereg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
  196. reference_reset_symbol(href,tablelabel,0,4);
  197. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, href, basereg);
  198. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_ADDr, indexreg, basereg);
  199. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_BX, basereg));
  200. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  201. { generate jump table }
  202. last:=min_;
  203. genitem(current_asmdata.CurrAsmList,hp);
  204. end
  205. else
  206. begin
  207. { adjust index }
  208. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,
  209. min_+ord(not(cs_create_pic in current_settings.moduleswitches)),
  210. indexreg,indexreg);
  211. { create reference and generate jump table }
  212. reference_reset(href,4);
  213. href.base:=NR_PC;
  214. href.index:=indexreg;
  215. href.shiftmode:=SM_LSL;
  216. href.shiftimm:=2;
  217. if cs_create_pic in current_settings.moduleswitches then
  218. begin
  219. picoffset:=-8;
  220. current_asmdata.getaddrlabel(piclabel);
  221. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  222. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,indexreg);
  223. cg.a_label(current_asmdata.CurrAsmList,piclabel);
  224. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,indexreg,NR_PC);
  225. end
  226. else
  227. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  228. { generate jump table }
  229. last:=min_;
  230. genitem(current_asmdata.CurrAsmList,hp);
  231. end;
  232. end;
  233. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  234. var
  235. first : boolean;
  236. lastrange : boolean;
  237. last : TConstExprInt;
  238. cond_lt,cond_le : tresflags;
  239. opcgsize : tcgsize;
  240. procedure genitem(t : pcaselabel);
  241. begin
  242. if assigned(t^.less) then
  243. genitem(t^.less);
  244. { need we to test the first value }
  245. if first and (t^._low>get_min_value(left.resultdef)) then
  246. begin
  247. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  248. end;
  249. if t^._low=t^._high then
  250. begin
  251. if t^._low-last=0 then
  252. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  253. else
  254. begin
  255. tbasecgarm(cg).cgsetflags:=true;
  256. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  257. then genlinearlist wouldn't be used }
  258. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  259. tbasecgarm(cg).cgsetflags:=false;
  260. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  261. end;
  262. last:=t^._low;
  263. lastrange:=false;
  264. end
  265. else
  266. begin
  267. { it begins with the smallest label, if the value }
  268. { is even smaller then jump immediately to the }
  269. { ELSE-label }
  270. if first then
  271. begin
  272. { have we to ajust the first value ? }
  273. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  274. begin
  275. tbasecgarm(cg).cgsetflags:=true;
  276. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  277. then genlinearlist wouldn't be use }
  278. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
  279. tbasecgarm(cg).cgsetflags:=false;
  280. end;
  281. end
  282. else
  283. begin
  284. { if there is no unused label between the last and the }
  285. { present label then the lower limit can be checked }
  286. { immediately. else check the range in between: }
  287. tbasecgarm(cg).cgsetflags:=true;
  288. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  289. then genlinearlist wouldn't be use }
  290. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  291. tbasecgarm(cg).cgsetflags:=false;
  292. { no jump necessary here if the new range starts at }
  293. { at the value following the previous one }
  294. if ((t^._low-last) <> 1) or
  295. (not lastrange) then
  296. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  297. end;
  298. tbasecgarm(cg).cgsetflags:=true;
  299. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  300. then genlinearlist wouldn't be use }
  301. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_32,aint(int64(t^._high-t^._low)),hregister);
  302. tbasecgarm(cg).cgsetflags:=false;
  303. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  304. last:=t^._high;
  305. lastrange:=true;
  306. end;
  307. first:=false;
  308. if assigned(t^.greater) then
  309. genitem(t^.greater);
  310. end;
  311. begin
  312. opcgsize:=def_cgsize(opsize);
  313. if with_sign then
  314. begin
  315. cond_lt:=F_LT;
  316. cond_le:=F_LE;
  317. end
  318. else
  319. begin
  320. cond_lt:=F_CC;
  321. cond_le:=F_LS;
  322. end;
  323. { do we need to generate cmps? }
  324. if (with_sign and (min_label<0)) then
  325. genlinearcmplist(hp)
  326. else
  327. begin
  328. last:=0;
  329. lastrange:=false;
  330. first:=true;
  331. genitem(hp);
  332. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  333. end;
  334. end;
  335. begin
  336. cinnode:=tarminnode;
  337. ccasenode:=tarmcasenode;
  338. end.