narmset.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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(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(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:=hlcg.getintregister(current_asmdata.CurrAsmList, opdef);
  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. current_asmdata.CurrAsmList.Concat(tai_align.Create(4));
  216. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  217. { generate jump table }
  218. last:=min_;
  219. genitem(current_asmdata.CurrAsmList,hp);
  220. end
  221. else
  222. begin
  223. { adjust index }
  224. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,
  225. min_+ord(not(cs_create_pic in current_settings.moduleswitches)),
  226. indexreg,indexreg);
  227. { create reference and generate jump table }
  228. reference_reset(href,4);
  229. href.base:=NR_PC;
  230. href.index:=indexreg;
  231. href.shiftmode:=SM_LSL;
  232. href.shiftimm:=2;
  233. if cs_create_pic in current_settings.moduleswitches then
  234. begin
  235. picoffset:=-8;
  236. current_asmdata.getaddrlabel(piclabel);
  237. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  238. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,indexreg);
  239. cg.a_label(current_asmdata.CurrAsmList,piclabel);
  240. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,indexreg,NR_PC);
  241. end
  242. else
  243. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  244. { generate jump table }
  245. last:=min_;
  246. genitem(current_asmdata.CurrAsmList,hp);
  247. end;
  248. end;
  249. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  250. var
  251. first : boolean;
  252. lastrange : boolean;
  253. last : TConstExprInt;
  254. cond_lt,cond_le : tresflags;
  255. opcgsize : tcgsize;
  256. procedure genitem(t : pcaselabel);
  257. begin
  258. if assigned(t^.less) then
  259. genitem(t^.less);
  260. { need we to test the first value }
  261. if first and (t^._low>get_min_value(left.resultdef)) then
  262. begin
  263. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  264. end;
  265. if t^._low=t^._high then
  266. begin
  267. if t^._low-last=0 then
  268. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  269. else
  270. begin
  271. tbasecgarm(cg).cgsetflags:=true;
  272. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  273. then genlinearlist wouldn't be used }
  274. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  275. tbasecgarm(cg).cgsetflags:=false;
  276. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  277. end;
  278. last:=t^._low;
  279. lastrange:=false;
  280. end
  281. else
  282. begin
  283. { it begins with the smallest label, if the value }
  284. { is even smaller then jump immediately to the }
  285. { ELSE-label }
  286. if first then
  287. begin
  288. { have we to ajust the first value ? }
  289. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  290. begin
  291. tbasecgarm(cg).cgsetflags:=true;
  292. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  293. then genlinearlist wouldn't be use }
  294. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
  295. tbasecgarm(cg).cgsetflags:=false;
  296. end;
  297. end
  298. else
  299. begin
  300. { if there is no unused label between the last and the }
  301. { present label then the lower limit can be checked }
  302. { immediately. else check the range in between: }
  303. tbasecgarm(cg).cgsetflags:=true;
  304. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  305. then genlinearlist wouldn't be use }
  306. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  307. tbasecgarm(cg).cgsetflags:=false;
  308. { no jump necessary here if the new range starts at }
  309. { at the value following the previous one }
  310. if ((t^._low-last) <> 1) or
  311. (not lastrange) then
  312. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  313. end;
  314. tbasecgarm(cg).cgsetflags:=true;
  315. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  316. then genlinearlist wouldn't be use }
  317. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_32,aint(int64(t^._high-t^._low)),hregister);
  318. tbasecgarm(cg).cgsetflags:=false;
  319. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  320. last:=t^._high;
  321. lastrange:=true;
  322. end;
  323. first:=false;
  324. if assigned(t^.greater) then
  325. genitem(t^.greater);
  326. end;
  327. begin
  328. opcgsize:=def_cgsize(opsize);
  329. if with_sign then
  330. begin
  331. cond_lt:=F_LT;
  332. cond_le:=F_LE;
  333. end
  334. else
  335. begin
  336. cond_lt:=F_CC;
  337. cond_le:=F_LS;
  338. end;
  339. { do we need to generate cmps? }
  340. if (with_sign and (min_label<0)) then
  341. genlinearcmplist(hp)
  342. else
  343. begin
  344. last:=0;
  345. lastrange:=false;
  346. first:=true;
  347. genitem(hp);
  348. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  349. end;
  350. end;
  351. begin
  352. cinnode:=tarminnode;
  353. ccasenode:=tarmcasenode;
  354. end.