narmset.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. procedure genitem(list:TAsmList;t : pcaselabel);
  59. var
  60. i : aint;
  61. begin
  62. if assigned(t^.less) then
  63. genitem(list,t^.less);
  64. { fill possible hole }
  65. for i:=last.svalue+1 to t^._low.svalue-1 do
  66. list.concat(Tai_const.Create_sym(elselabel));
  67. for i:=t^._low.svalue to t^._high.svalue do
  68. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  69. last:=t^._high.svalue;
  70. if assigned(t^.greater) then
  71. genitem(list,t^.greater);
  72. end;
  73. procedure genitem_thumb2(list:TAsmList;t : pcaselabel);
  74. var
  75. i : aint;
  76. begin
  77. if assigned(t^.less) then
  78. genitem_thumb2(list,t^.less);
  79. { fill possible hole }
  80. for i:=last.svalue+1 to t^._low.svalue-1 do
  81. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
  82. for i:=t^._low.svalue to t^._high.svalue do
  83. list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
  84. last:=t^._high.svalue;
  85. if assigned(t^.greater) then
  86. genitem_thumb2(list,t^.greater);
  87. end;
  88. begin
  89. if not(jumptable_no_range) then
  90. begin
  91. { case expr less than min_ => goto elselabel }
  92. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(min_),hregister,elselabel);
  93. { case expr greater than max_ => goto elselabel }
  94. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,aint(max_),hregister,elselabel);
  95. end;
  96. { make it a 32bit register }
  97. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  98. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,OS_INT,hregister,indexreg);
  99. if current_settings.cputype in cpu_thumb2 then
  100. begin
  101. { adjust index }
  102. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
  103. { create reference and generate jump table }
  104. reference_reset(href,4);
  105. href.base:=NR_PC;
  106. href.index:=indexreg;
  107. href.shiftmode:=SM_LSL;
  108. href.shiftimm:=1;
  109. current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
  110. { generate jump table }
  111. current_asmdata.getjumplabel(tablelabel);
  112. cg.a_label(current_asmdata.CurrAsmList,tablelabel);
  113. last:=min_;
  114. genitem_thumb2(current_asmdata.CurrAsmList,hp);
  115. end
  116. else
  117. begin
  118. { adjust index }
  119. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_+1,indexreg,indexreg);
  120. { create reference and generate jump table }
  121. reference_reset(href,4);
  122. href.base:=NR_PC;
  123. href.index:=indexreg;
  124. href.shiftmode:=SM_LSL;
  125. href.shiftimm:=2;
  126. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
  127. { generate jump table }
  128. last:=min_;
  129. genitem(current_asmdata.CurrAsmList,hp);
  130. end;
  131. end;
  132. procedure tarmcasenode.genlinearlist(hp : pcaselabel);
  133. var
  134. first : boolean;
  135. lastrange : boolean;
  136. last : TConstExprInt;
  137. cond_lt,cond_le : tresflags;
  138. procedure genitem(t : pcaselabel);
  139. begin
  140. if assigned(t^.less) then
  141. genitem(t^.less);
  142. { need we to test the first value }
  143. if first and (t^._low>get_min_value(left.resultdef)) then
  144. begin
  145. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  146. end;
  147. if t^._low=t^._high then
  148. begin
  149. if t^._low-last=0 then
  150. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  151. else
  152. begin
  153. tcgarm(cg).cgsetflags:=true;
  154. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(int64(t^._low-last)), hregister);
  155. tcgarm(cg).cgsetflags:=false;
  156. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
  157. end;
  158. last:=t^._low;
  159. lastrange:=false;
  160. end
  161. else
  162. begin
  163. { it begins with the smallest label, if the value }
  164. { is even smaller then jump immediately to the }
  165. { ELSE-label }
  166. if first then
  167. begin
  168. { have we to ajust the first value ? }
  169. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  170. begin
  171. tcgarm(cg).cgsetflags:=true;
  172. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(int64(t^._low)), hregister);
  173. tcgarm(cg).cgsetflags:=false;
  174. end;
  175. end
  176. else
  177. begin
  178. { if there is no unused label between the last and the }
  179. { present label then the lower limit can be checked }
  180. { immediately. else check the range in between: }
  181. tcgarm(cg).cgsetflags:=true;
  182. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(int64(t^._low-last)), hregister);
  183. tcgarm(cg).cgsetflags:=false;
  184. { no jump necessary here if the new range starts at }
  185. { at the value following the previous one }
  186. if ((t^._low-last) <> 1) or
  187. (not lastrange) then
  188. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  189. end;
  190. tcgarm(cg).cgsetflags:=true;
  191. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,aint(int64(t^._high-t^._low)),hregister);
  192. tcgarm(cg).cgsetflags:=false;
  193. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  194. last:=t^._high;
  195. lastrange:=true;
  196. end;
  197. first:=false;
  198. if assigned(t^.greater) then
  199. genitem(t^.greater);
  200. end;
  201. begin
  202. if with_sign then
  203. begin
  204. cond_lt:=F_LT;
  205. cond_le:=F_LE;
  206. end
  207. else
  208. begin
  209. cond_lt:=F_CC;
  210. cond_le:=F_LS;
  211. end;
  212. { do we need to generate cmps? }
  213. if (with_sign and (min_label<0)) then
  214. genlinearcmplist(hp)
  215. else
  216. begin
  217. last:=0;
  218. lastrange:=false;
  219. first:=true;
  220. genitem(hp);
  221. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  222. end;
  223. end;
  224. begin
  225. ccasenode:=tarmcasenode;
  226. end.