narmset.pas 13 KB

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