narmset.pas 13 KB

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