narmset.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. node,nset,pass_1,ncgset;
  23. type
  24. tarmcasenode = class(tcgcasenode)
  25. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  26. function has_jumptable : boolean;override;
  27. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);override;
  28. procedure genlinearlist(hp : pcaselabel);override;
  29. end;
  30. implementation
  31. uses
  32. systems,
  33. verbose,globals,constexp,
  34. symconst,symdef,defutil,
  35. aasmbase,aasmtai,aasmdata,aasmcpu,
  36. cgbase,pass_2,
  37. ncon,
  38. cpubase,cpuinfo,procinfo,
  39. cgutils,cgobj,ncgutil,
  40. cgcpu;
  41. {*****************************************************************************
  42. TARMCASENODE
  43. *****************************************************************************}
  44. procedure tarmcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  45. begin
  46. inc(max_linear_list,2)
  47. end;
  48. function tarmcasenode.has_jumptable : boolean;
  49. begin
  50. has_jumptable:=true;
  51. end;
  52. procedure tarmcasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  53. var
  54. last : TConstExprInt;
  55. indexreg : tregister;
  56. href : treference;
  57. tablelabel: TAsmLabel;
  58. opcgsize : tcgsize;
  59. procedure genitem(list:TAsmList;t : pcaselabel);
  60. var
  61. i : aint;
  62. begin
  63. if assigned(t^.less) then
  64. genitem(list,t^.less);
  65. { fill possible hole }
  66. for i:=last.svalue+1 to t^._low.svalue-1 do
  67. list.concat(Tai_const.Create_sym(elselabel));
  68. for i:=t^._low.svalue to t^._high.svalue do
  69. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  70. last:=t^._high.svalue;
  71. if assigned(t^.greater) then
  72. genitem(list,t^.greater);
  73. end;
  74. procedure genitem_thumb2(list:TAsmList;t : pcaselabel);
  75. var
  76. i : aint;
  77. begin
  78. if assigned(t^.less) then
  79. genitem_thumb2(list,t^.less);
  80. { fill possible hole }
  81. for i:=last.svalue+1 to t^._low.svalue-1 do
  82. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
  83. for i:=t^._low.svalue to t^._high.svalue do
  84. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
  85. last:=t^._high.svalue;
  86. if assigned(t^.greater) then
  87. genitem_thumb2(list,t^.greater);
  88. end;
  89. begin
  90. opcgsize:=def_cgsize(opsize);
  91. if not(jumptable_no_range) then
  92. begin
  93. { case expr less than min_ => goto elselabel }
  94. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
  95. { case expr greater than max_ => goto elselabel }
  96. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
  97. end;
  98. { make it a 32bit register }
  99. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  100. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);
  101. if current_settings.cputype in cpu_thumb2 then
  102. begin
  103. { adjust index }
  104. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  105. { create reference and generate jump table }
  106. reference_reset(href,4);
  107. href.base:=NR_PC;
  108. href.index:=indexreg;
  109. href.shiftmode:=SM_LSL;
  110. href.shiftimm:=1;
  111. current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
  112. { generate jump table }
  113. current_asmdata.getjumplabel(tablelabel);
  114. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  115. last:=min_;
  116. genitem_thumb2(current_asmdata.CurrAsmList,hp);
  117. end
  118. else
  119. begin
  120. { adjust index }
  121. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_+1,indexreg,indexreg);
  122. { create reference and generate jump table }
  123. reference_reset(href,4);
  124. href.base:=NR_PC;
  125. href.index:=indexreg;
  126. href.shiftmode:=SM_LSL;
  127. href.shiftimm:=2;
  128. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  129. { generate jump table }
  130. last:=min_;
  131. genitem(current_asmdata.CurrAsmList,hp);
  132. end;
  133. end;
  134. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  135. var
  136. first : boolean;
  137. lastrange : boolean;
  138. last : TConstExprInt;
  139. cond_lt,cond_le : tresflags;
  140. opcgsize : tcgsize;
  141. procedure genitem(t : pcaselabel);
  142. begin
  143. if assigned(t^.less) then
  144. genitem(t^.less);
  145. { need we to test the first value }
  146. if first and (t^._low>get_min_value(left.resultdef)) then
  147. begin
  148. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  149. end;
  150. if t^._low=t^._high then
  151. begin
  152. if t^._low-last=0 then
  153. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  154. else
  155. begin
  156. tcgarm(cg).cgsetflags:=true;
  157. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  158. then genlinearlist wouldn't be used }
  159. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  160. tcgarm(cg).cgsetflags:=false;
  161. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  162. end;
  163. last:=t^._low;
  164. lastrange:=false;
  165. end
  166. else
  167. begin
  168. { it begins with the smallest label, if the value }
  169. { is even smaller then jump immediately to the }
  170. { ELSE-label }
  171. if first then
  172. begin
  173. { have we to ajust the first value ? }
  174. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  175. begin
  176. tcgarm(cg).cgsetflags:=true;
  177. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  178. then genlinearlist wouldn't be use }
  179. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
  180. tcgarm(cg).cgsetflags:=false;
  181. end;
  182. end
  183. else
  184. begin
  185. { if there is no unused label between the last and the }
  186. { present label then the lower limit can be checked }
  187. { immediately. else check the range in between: }
  188. tcgarm(cg).cgsetflags:=true;
  189. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  190. then genlinearlist wouldn't be use }
  191. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
  192. tcgarm(cg).cgsetflags:=false;
  193. { no jump necessary here if the new range starts at }
  194. { at the value following the previous one }
  195. if ((t^._low-last) <> 1) or
  196. (not lastrange) then
  197. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  198. end;
  199. tcgarm(cg).cgsetflags:=true;
  200. { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
  201. then genlinearlist wouldn't be use }
  202. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_32,aint(int64(t^._high-t^._low)),hregister);
  203. tcgarm(cg).cgsetflags:=false;
  204. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  205. last:=t^._high;
  206. lastrange:=true;
  207. end;
  208. first:=false;
  209. if assigned(t^.greater) then
  210. genitem(t^.greater);
  211. end;
  212. begin
  213. opcgsize:=def_cgsize(opsize);
  214. if with_sign then
  215. begin
  216. cond_lt:=F_LT;
  217. cond_le:=F_LE;
  218. end
  219. else
  220. begin
  221. cond_lt:=F_CC;
  222. cond_le:=F_LS;
  223. end;
  224. { do we need to generate cmps? }
  225. if (with_sign and (min_label<0)) then
  226. genlinearcmplist(hp)
  227. else
  228. begin
  229. last:=0;
  230. lastrange:=false;
  231. first:=true;
  232. genitem(hp);
  233. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  234. end;
  235. end;
  236. begin
  237. ccasenode:=tarmcasenode;
  238. end.