nppcset.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  3. Generate PowerPC 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 nppcset;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. node, nset, ncgset, cpubase, cgbase, cgobj, aasmbase, aasmtai, globtype;
  22. type
  23. tppccasenode = 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, cgutils;
  41. {*****************************************************************************
  42. TCGCASENODE
  43. *****************************************************************************}
  44. procedure tppccasenode.optimizevalues(var max_linear_list : aint; var max_dist : aword);
  45. begin
  46. max_linear_list := 10;
  47. end;
  48. function tppccasenode.has_jumptable : boolean;
  49. begin
  50. has_jumptable := true;
  51. end;
  52. procedure tppccasenode.genjumptable(hp : pcaselabel; min_, max_ : aint);
  53. var
  54. table : tasmlabel;
  55. last : TConstExprInt;
  56. indexreg : tregister;
  57. href : treference;
  58. procedure genitem(list:taasmoutput;t : pcaselabel);
  59. var
  60. i : aint;
  61. begin
  62. if assigned(t^.less) then
  63. genitem(list,t^.less);
  64. { fill possible hole }
  65. for i:=last+1 to t^._low-1 do
  66. list.concat(Tai_const.Create_sym(elselabel));
  67. for i:=t^._low to t^._high do
  68. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  69. last:=t^._high;
  70. if assigned(t^.greater) then
  71. genitem(list,t^.greater);
  72. end;
  73. begin
  74. { this is exactly the same code as for 32 bit PowerPC processors. It might be useful to change this
  75. later (with e.g. TOC support) into a method which uses relative values in the jumptable to save space
  76. and memory bandwidth. At the moment this is not a good idea, since these methods involve loading of
  77. one or more 64 bit integer adresses which is slow }
  78. if not(jumptable_no_range) then begin
  79. { case expr less than min_ => goto elselabel }
  80. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(min_),hregister,elselabel);
  81. { case expr greater than max_ => goto elselabel }
  82. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,aint(max_),hregister,elselabel);
  83. end;
  84. objectlibrary.getjumplabel(table);
  85. { allocate base and index registers register }
  86. indexreg:= cg.makeregsize(exprasmlist, hregister, OS_INT);
  87. { indexreg := hregister; }
  88. cg.a_load_reg_reg(exprasmlist, opsize, OS_INT, hregister, indexreg);
  89. { create reference, indexreg := indexreg * sizeof(OS_ADDR) }
  90. cg.a_op_const_reg(exprasmlist, OP_MUL, OS_INT, tcgsize2size[OS_ADDR], indexreg);
  91. reference_reset_symbol(href, table, (-aint(min_)) * tcgsize2size[OS_ADDR]);
  92. href.index := indexreg;
  93. cg.a_load_ref_reg(exprasmlist, OS_INT, OS_INT, href, indexreg);
  94. exprasmlist.concat(taicpu.op_reg(A_MTCTR, indexreg));
  95. exprasmlist.concat(taicpu.op_none(A_BCTR));
  96. { generate jump table }
  97. new_section(current_procinfo.aktlocaldata,sec_data,current_procinfo.procdef.mangledname,sizeof(aint));
  98. current_procinfo.aktlocaldata.concat(Tai_label.Create(table));
  99. last:=min_;
  100. genitem(current_procinfo.aktlocaldata,hp);
  101. end;
  102. procedure tppccasenode.genlinearlist(hp: pcaselabel);
  103. var
  104. first, lastrange: boolean;
  105. last: TConstExprInt;
  106. procedure genitem(t: pcaselabel);
  107. procedure gensub(value: aint);
  108. var
  109. tmpreg: tregister;
  110. begin
  111. value := -value;
  112. if (value >= low(smallint)) and
  113. (value <= high(smallint)) then
  114. exprasmlist.concat(taicpu.op_reg_reg_const(A_ADDIC_, hregister,
  115. hregister, value))
  116. else
  117. begin
  118. tmpreg := cg.getintregister(exprasmlist, OS_INT);
  119. cg.a_load_const_reg(exprasmlist, OS_INT, value, tmpreg);
  120. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ADD_, hregister,
  121. hregister, tmpreg));
  122. end;
  123. end;
  124. begin
  125. if assigned(t^.less) then
  126. genitem(t^.less);
  127. { need we to test the first value }
  128. if first and (t^._low > get_min_value(left.resulttype.def)) then begin
  129. cg.a_cmp_const_reg_label(exprasmlist, OS_INT, jmp_lt, aword(t^._low),
  130. hregister, elselabel);
  131. end;
  132. if t^._low = t^._high then begin
  133. if t^._low - last = 0 then
  134. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ, 0, hregister,
  135. blocklabel(t^.blockid))
  136. else
  137. gensub(aint(t^._low - last));
  138. tcgppc(cg).a_jmp_cond(exprasmlist, OC_EQ, blocklabel(t^.blockid));
  139. last := t^._low;
  140. lastrange := false;
  141. end else begin
  142. { it begins with the smallest label, if the value }
  143. { is even smaller then jump immediately to the }
  144. { ELSE-label }
  145. if first then begin
  146. { have we to ajust the first value ? }
  147. if (t^._low > get_min_value(left.resulttype.def)) then
  148. gensub(aint(t^._low));
  149. end else begin
  150. { if there is no unused label between the last and the }
  151. { present label then the lower limit can be checked }
  152. { immediately. else check the range in between: }
  153. gensub(aint(t^._low - last));
  154. if ((t^._low - last) <> 1) or (not lastrange) then
  155. tcgppc(cg).a_jmp_cond(exprasmlist, jmp_lt, elselabel);
  156. end;
  157. gensub(aint(t^._high - t^._low));
  158. tcgppc(cg).a_jmp_cond(exprasmlist, jmp_le, blocklabel(t^.blockid));
  159. last := t^._high;
  160. lastrange := true;
  161. end;
  162. first := false;
  163. if assigned(t^.greater) then
  164. genitem(t^.greater);
  165. end;
  166. begin
  167. { do we need to generate cmps? }
  168. if (with_sign and (min_label < 0)) or (opsize = OS_32) then
  169. genlinearcmplist(hp)
  170. else begin
  171. last := 0;
  172. lastrange := false;
  173. first := true;
  174. genitem(hp);
  175. cg.a_jmp_always(exprasmlist, elselabel);
  176. end;
  177. end;
  178. begin
  179. ccasenode := tppccasenode;
  180. end.