narmset.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. 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(current_settings.cputype in cpu_thumb) 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 current_settings.cputype in cpu_thumb 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: TAsmLabel;
  119. opcgsize : tcgsize;
  120. procedure genitem(list:TAsmList;t : pcaselabel);
  121. var
  122. i : aint;
  123. begin
  124. if assigned(t^.less) then
  125. genitem(list,t^.less);
  126. { fill possible hole }
  127. for i:=last.svalue+1 to t^._low.svalue-1 do
  128. list.concat(Tai_const.Create_sym(elselabel));
  129. for i:=t^._low.svalue to t^._high.svalue do
  130. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  131. last:=t^._high.svalue;
  132. if assigned(t^.greater) then
  133. genitem(list,t^.greater);
  134. end;
  135. procedure genitem_thumb2(list:TAsmList;t : pcaselabel);
  136. var
  137. i : aint;
  138. begin
  139. if assigned(t^.less) then
  140. genitem_thumb2(list,t^.less);
  141. { fill possible hole }
  142. for i:=last.svalue+1 to t^._low.svalue-1 do
  143. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
  144. for i:=t^._low.svalue to t^._high.svalue do
  145. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
  146. last:=t^._high.svalue;
  147. if assigned(t^.greater) then
  148. genitem_thumb2(list,t^.greater);
  149. end;
  150. begin
  151. opcgsize:=def_cgsize(opsize);
  152. if not(jumptable_no_range) then
  153. begin
  154. { case expr less than min_ => goto elselabel }
  155. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
  156. { case expr greater than max_ => goto elselabel }
  157. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
  158. end;
  159. { make it a 32bit register }
  160. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  161. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);
  162. if current_settings.cputype in cpu_thumb2 then
  163. begin
  164. { adjust index }
  165. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  166. { create reference and generate jump table }
  167. reference_reset(href,4);
  168. href.base:=NR_PC;
  169. href.index:=indexreg;
  170. href.shiftmode:=SM_LSL;
  171. href.shiftimm:=1;
  172. current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
  173. { generate jump table }
  174. current_asmdata.getjumplabel(tablelabel);
  175. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  176. last:=min_;
  177. genitem_thumb2(current_asmdata.CurrAsmList,hp);
  178. end
  179. else if current_settings.cputype in cpu_thumb then
  180. begin
  181. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_+1,indexreg,indexreg);
  182. current_asmdata.getaddrlabel(tablelabel);
  183. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,indexreg);
  184. basereg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
  185. reference_reset_symbol(href,tablelabel,0,4);
  186. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, href, basereg);
  187. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_ADDr, indexreg, basereg);
  188. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_BX, basereg));
  189. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  190. { generate jump table }
  191. last:=min_;
  192. genitem(current_asmdata.CurrAsmList,hp);
  193. end
  194. else
  195. begin
  196. { adjust index }
  197. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_+1,indexreg,indexreg);
  198. { create reference and generate jump table }
  199. reference_reset(href,4);
  200. href.base:=NR_PC;
  201. href.index:=indexreg;
  202. href.shiftmode:=SM_LSL;
  203. href.shiftimm:=2;
  204. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  205. { generate jump table }
  206. last:=min_;
  207. genitem(current_asmdata.CurrAsmList,hp);
  208. end;
  209. end;
  210. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  211. var
  212. first : boolean;
  213. lastrange : boolean;
  214. last : TConstExprInt;
  215. cond_lt,cond_le : tresflags;
  216. opcgsize : tcgsize;
  217. procedure genitem(t : pcaselabel);
  218. begin
  219. if assigned(t^.less) then
  220. genitem(t^.less);
  221. { need we to test the first value }
  222. if first and (t^._low>get_min_value(left.resultdef)) then
  223. begin
  224. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  225. end;
  226. if t^._low=t^._high then
  227. begin
  228. if t^._low-last=0 then
  229. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  230. else
  231. begin
  232. tbasecgarm(cg).cgsetflags:=true;
  233. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  234. then genlinearlist wouldn't be used }
  235. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  236. tbasecgarm(cg).cgsetflags:=false;
  237. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  238. end;
  239. last:=t^._low;
  240. lastrange:=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 first then
  248. begin
  249. { have we to ajust the first value ? }
  250. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  251. begin
  252. tbasecgarm(cg).cgsetflags:=true;
  253. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  254. then genlinearlist wouldn't be use }
  255. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
  256. tbasecgarm(cg).cgsetflags:=false;
  257. end;
  258. end
  259. else
  260. begin
  261. { if there is no unused label between the last and the }
  262. { present label then the lower limit can be checked }
  263. { immediately. else check the range in between: }
  264. tbasecgarm(cg).cgsetflags:=true;
  265. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  266. then genlinearlist wouldn't be use }
  267. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  268. tbasecgarm(cg).cgsetflags:=false;
  269. { no jump necessary here if the new range starts at }
  270. { at the value following the previous one }
  271. if ((t^._low-last) <> 1) or
  272. (not lastrange) then
  273. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  274. end;
  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^._high-t^._low)),hregister);
  279. tbasecgarm(cg).cgsetflags:=false;
  280. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  281. last:=t^._high;
  282. lastrange:=true;
  283. end;
  284. first:=false;
  285. if assigned(t^.greater) then
  286. genitem(t^.greater);
  287. end;
  288. begin
  289. opcgsize:=def_cgsize(opsize);
  290. if with_sign then
  291. begin
  292. cond_lt:=F_LT;
  293. cond_le:=F_LE;
  294. end
  295. else
  296. begin
  297. cond_lt:=F_CC;
  298. cond_le:=F_LS;
  299. end;
  300. { do we need to generate cmps? }
  301. if (with_sign and (min_label<0)) then
  302. genlinearcmplist(hp)
  303. else
  304. begin
  305. last:=0;
  306. lastrange:=false;
  307. first:=true;
  308. genitem(hp);
  309. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  310. end;
  311. end;
  312. begin
  313. cinnode:=tarminnode;
  314. ccasenode:=tarmcasenode;
  315. end.