ngppcset.pas 8.6 KB

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