narmset.pas 8.1 KB

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