ngppcset.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  3. Generate PowerPC32/64 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 ngppcset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nset,ncgset,cpubase,cgbase,cgobj,aasmbase,aasmtai,aasmdata,globtype;
  22. type
  23. tgppccasenode = class(tcgcasenode)
  24. protected
  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. paramgr,
  36. cpuinfo,
  37. pass_2,cgcpu,
  38. ncon,
  39. tgobj,ncgutil,regvars,rgobj,aasmcpu,
  40. procinfo,
  41. cgutils;
  42. {*****************************************************************************
  43. TCGCASENODE
  44. *****************************************************************************}
  45. procedure tgppccasenode.optimizevalues(var max_linear_list : aint; var max_dist : aword);
  46. begin
  47. max_linear_list := 10;
  48. end;
  49. function tgppccasenode.has_jumptable : boolean;
  50. begin
  51. has_jumptable:=true;
  52. end;
  53. procedure tgppccasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  54. var
  55. table : tasmlabel;
  56. last : TConstExprInt;
  57. indexreg : tregister;
  58. href : treference;
  59. mulfactor: longint;
  60. procedure genitem(list:TAsmList;t : pcaselabel);
  61. var
  62. i : TConstExprInt;
  63. begin
  64. if assigned(t^.less) then
  65. genitem(list,t^.less);
  66. { fill possible hole }
  67. i:=last+1;
  68. while i<=t^._low-1 do
  69. begin
  70. list.concat(Tai_const.Create_rel_sym(aitconst_32bit,table,elselabel));
  71. i:=i+1;
  72. end;
  73. i:=t^._low;
  74. while i<=t^._high do
  75. begin
  76. list.concat(Tai_const.Create_rel_sym(aitconst_32bit,table,blocklabel(t^.blockid)));
  77. i:=i+1;
  78. end;
  79. last:=t^._high;
  80. if assigned(t^.greater) then
  81. genitem(list,t^.greater);
  82. end;
  83. begin
  84. last:=min_;
  85. { make it a 32bit register }
  86. // allocate base and index registers register
  87. indexreg:= cg.makeregsize(current_asmdata.CurrAsmList, hregister, OS_INT);
  88. { indexreg := hregister; }
  89. cg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, OS_INT, hregister, indexreg);
  90. if not(jumptable_no_range) then
  91. begin
  92. { use aword(value-min)<aword(max-min) instead of two comparisons }
  93. { case expr outside min_ .. max_ => goto elselabel }
  94. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,aint(min_),indexreg);
  95. { this trick requires an unsigned comparison in all cases }
  96. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_A,aint(max_)-aint(min_),indexreg,elselabel);
  97. { already taken into account now }
  98. min_:=0;
  99. end;
  100. current_asmdata.getjumplabel(table);
  101. { create reference, indexreg := indexreg * sizeof(jtentry) (= 4) }
  102. mulfactor:=4;
  103. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, mulfactor, indexreg);
  104. reference_reset_symbol(href, table, (-aint(min_)) * mulfactor, 4);
  105. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  106. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  107. reference_reset_base(href,hregister,0,4);
  108. href.index:=indexreg;
  109. indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  110. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_S32,OS_ADDR,href,indexreg);
  111. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,hregister,indexreg);
  112. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_MTCTR, indexreg));
  113. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_BCTR));
  114. { generate jump table }
  115. current_asmdata.CurrAsmList.concat(Tai_label.Create(table));
  116. genitem(current_asmdata.CurrAsmList,hp);
  117. end;
  118. procedure tgppccasenode.genlinearlist(hp : pcaselabel);
  119. var
  120. first, lastrange : boolean;
  121. last : TConstExprInt;
  122. procedure genitem(t : pcaselabel);
  123. procedure gensub(value:longint);
  124. var
  125. tmpreg: tregister;
  126. begin
  127. value := -value;
  128. if (value >= low(smallint)) and
  129. (value <= high(smallint)) then
  130. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_ADDIC_,hregister,
  131. hregister,value))
  132. else
  133. begin
  134. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  135. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,value,tmpreg);
  136. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD_,hregister,
  137. hregister,tmpreg));
  138. end;
  139. end;
  140. begin
  141. if (get_min_value(left.resultdef) >= int64(low(smallint))) and
  142. (get_max_value(left.resultdef) <= int64(high(word))) then
  143. begin
  144. genlinearcmplist(hp);
  145. exit;
  146. end;
  147. if assigned(t^.less) then
  148. genitem(t^.less);
  149. { need we to test the first value }
  150. if first and (t^._low>get_min_value(left.resultdef)) then
  151. begin
  152. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,jmp_lt,aword(t^._low.svalue),hregister,elselabel);
  153. end;
  154. if t^._low=t^._high then
  155. begin
  156. if t^._low-last=0 then
  157. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  158. else
  159. gensub(longint(int64(t^._low-last)));
  160. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,blocklabel(t^.blockid));
  161. last:=t^._low;
  162. lastrange := false;
  163. end
  164. else
  165. begin
  166. { it begins with the smallest label, if the value }
  167. { is even smaller then jump immediately to the }
  168. { ELSE-label }
  169. if first then
  170. begin
  171. { have we to ajust the first value ? }
  172. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  173. gensub(longint(int64(t^._low)));
  174. end
  175. else
  176. begin
  177. { if there is no unused label between the last and the }
  178. { present label then the lower limit can be checked }
  179. { immediately. else check the range in between: }
  180. gensub(longint(int64(t^._low-last)));
  181. if ((t^._low-last) <> 1) or
  182. (not lastrange) then
  183. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,jmp_lt,elselabel);
  184. end;
  185. gensub(longint(int64(t^._high-t^._low)));
  186. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,jmp_le,blocklabel(t^.blockid));
  187. last:=t^._high;
  188. lastrange := true;
  189. end;
  190. first:=false;
  191. if assigned(t^.greater) then
  192. genitem(t^.greater);
  193. end;
  194. begin
  195. { do we need to generate cmps? }
  196. if (with_sign and (min_label<0)) or
  197. (opsize in [OS_32,OS_64,OS_S64]) then
  198. genlinearcmplist(hp)
  199. else
  200. begin
  201. last:=0;
  202. lastrange:=false;
  203. first:=true;
  204. genitem(hp);
  205. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  206. end;
  207. end;
  208. begin
  209. ccasenode:=tgppccasenode;
  210. end.