narmset.pas 16 KB

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