ngppcset.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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,
  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. 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. i:=last+1;
  67. while i<=t^._low-1 do
  68. begin
  69. list.concat(Tai_const.Create_sym(elselabel));
  70. inc(i);
  71. end;
  72. i:=t^._low;
  73. while i<=t^._high do
  74. begin
  75. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  76. inc(i);
  77. end;
  78. last:=t^._high;
  79. if assigned(t^.greater) then
  80. genitem(list,t^.greater);
  81. end;
  82. begin
  83. if not(jumptable_no_range) then
  84. begin
  85. { case expr less than min_ => goto elselabel }
  86. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(min_),hregister,elselabel);
  87. { case expr greater than max_ => goto elselabel }
  88. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,aint(max_),hregister,elselabel);
  89. end;
  90. current_asmdata.getjumplabel(table);
  91. { make it a 32bit register }
  92. // allocate base and index registers register
  93. indexreg:= cg.makeregsize(current_asmdata.CurrAsmList, hregister, OS_INT);
  94. { indexreg := hregister; }
  95. cg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, OS_INT, hregister, indexreg);
  96. { create reference, indexreg := indexreg * sizeof(OS_ADDR) }
  97. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, tcgsize2size[OS_ADDR], indexreg);
  98. reference_reset_symbol(href, table, (-aint(min_)) * tcgsize2size[OS_ADDR]);
  99. href.index := indexreg;
  100. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, href, indexreg);
  101. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_MTCTR, indexreg));
  102. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_BCTR));
  103. { generate jump table }
  104. new_section(current_procinfo.aktlocaldata,sec_rodata,current_procinfo.procdef.mangledname,sizeof(aint));
  105. current_procinfo.aktlocaldata.concat(Tai_label.Create(table));
  106. last:=min_;
  107. genitem(current_procinfo.aktlocaldata,hp);
  108. end;
  109. procedure tgppccasenode.genlinearlist(hp : pcaselabel);
  110. var
  111. first, lastrange : boolean;
  112. last : TConstExprInt;
  113. procedure genitem(t : pcaselabel);
  114. procedure gensub(value:longint);
  115. var
  116. tmpreg: tregister;
  117. begin
  118. value := -value;
  119. if (value >= low(smallint)) and
  120. (value <= high(smallint)) then
  121. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_ADDIC_,hregister,
  122. hregister,value))
  123. else
  124. begin
  125. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  126. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,value,tmpreg);
  127. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD_,hregister,
  128. hregister,tmpreg));
  129. end;
  130. end;
  131. begin
  132. if (get_min_value(left.resultdef) >= low(smallint)) and
  133. (get_max_value(left.resultdef) <= high(word)) then
  134. begin
  135. genlinearcmplist(hp);
  136. exit;
  137. end;
  138. if assigned(t^.less) then
  139. genitem(t^.less);
  140. { need we to test the first value }
  141. if first and (t^._low>get_min_value(left.resultdef)) then
  142. begin
  143. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,jmp_lt,aword(t^._low),hregister,elselabel);
  144. end;
  145. if t^._low=t^._high then
  146. begin
  147. if t^._low-last=0 then
  148. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  149. else
  150. gensub(longint(t^._low-last));
  151. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,blocklabel(t^.blockid));
  152. last:=t^._low;
  153. lastrange := false;
  154. end
  155. else
  156. begin
  157. { it begins with the smallest label, if the value }
  158. { is even smaller then jump immediately to the }
  159. { ELSE-label }
  160. if first then
  161. begin
  162. { have we to ajust the first value ? }
  163. if (t^._low>get_min_value(left.resultdef)) then
  164. gensub(longint(t^._low));
  165. end
  166. else
  167. begin
  168. { if there is no unused label between the last and the }
  169. { present label then the lower limit can be checked }
  170. { immediately. else check the range in between: }
  171. gensub(longint(t^._low-last));
  172. if ((t^._low-last) <> 1) or
  173. (not lastrange) then
  174. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,jmp_lt,elselabel);
  175. end;
  176. gensub(longint(t^._high-t^._low));
  177. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,jmp_le,blocklabel(t^.blockid));
  178. last:=t^._high;
  179. lastrange := true;
  180. end;
  181. first:=false;
  182. if assigned(t^.greater) then
  183. genitem(t^.greater);
  184. end;
  185. begin
  186. { do we need to generate cmps? }
  187. if (with_sign and (min_label<0)) or
  188. (opsize in [OS_32,OS_64,OS_S64]) then
  189. genlinearcmplist(hp)
  190. else
  191. begin
  192. last:=0;
  193. lastrange:=false;
  194. first:=true;
  195. genitem(hp);
  196. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  197. end;
  198. end;
  199. begin
  200. ccasenode:=tgppccasenode;
  201. end.