narmset.pas 7.8 KB

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