nppcset.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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,aasmdata, 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:TAsmList;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. i:=last+1;
  66. while i<=t^._low-1 do
  67. begin
  68. list.concat(Tai_const.Create_sym(elselabel));
  69. inc(i);
  70. end;
  71. i:=t^._low;
  72. while i<=t^._high do
  73. begin
  74. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  75. inc(i);
  76. end;
  77. last:=t^._high;
  78. if assigned(t^.greater) then
  79. genitem(list,t^.greater);
  80. end;
  81. begin
  82. { this is exactly the same code as for 32 bit PowerPC processors. It might be useful to change this
  83. later (with e.g. TOC support) into a method which uses relative values in the jumptable to save space
  84. and memory bandwidth. At the moment this is not a good idea, since these methods involve loading of
  85. one or more 64 bit integer adresses which is slow }
  86. if not(jumptable_no_range) then begin
  87. { case expr less than min_ => goto elselabel }
  88. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(min_),hregister,elselabel);
  89. { case expr greater than max_ => goto elselabel }
  90. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,aint(max_),hregister,elselabel);
  91. end;
  92. current_asmdata.getjumplabel(table);
  93. { allocate base and index registers register }
  94. indexreg:= cg.makeregsize(current_asmdata.CurrAsmList, hregister, OS_INT);
  95. { indexreg := hregister; }
  96. cg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, OS_INT, hregister, indexreg);
  97. { create reference, indexreg := indexreg * sizeof(OS_ADDR) }
  98. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, tcgsize2size[OS_ADDR], indexreg);
  99. reference_reset_symbol(href, table, (-aint(min_)) * tcgsize2size[OS_ADDR]);
  100. href.index := indexreg;
  101. cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, href, indexreg);
  102. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_MTCTR, indexreg));
  103. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_BCTR));
  104. { generate jump table }
  105. new_section(current_procinfo.aktlocaldata,sec_data,current_procinfo.procdef.mangledname,sizeof(aint));
  106. current_procinfo.aktlocaldata.concat(Tai_label.Create(table));
  107. last:=min_;
  108. genitem(current_procinfo.aktlocaldata,hp);
  109. end;
  110. procedure tppccasenode.genlinearlist(hp: pcaselabel);
  111. var
  112. first, lastrange: boolean;
  113. last: TConstExprInt;
  114. procedure genitem(t: pcaselabel);
  115. procedure gensub(value: aint);
  116. var
  117. tmpreg: tregister;
  118. begin
  119. value := -value;
  120. if (value >= low(smallint)) and
  121. (value <= high(smallint)) then
  122. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_ADDIC_, hregister,
  123. hregister, value))
  124. else
  125. begin
  126. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  127. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, value, tmpreg);
  128. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD_, hregister,
  129. hregister, tmpreg));
  130. end;
  131. end;
  132. begin
  133. if assigned(t^.less) then
  134. genitem(t^.less);
  135. { need we to test the first value }
  136. if first and (t^._low > get_min_value(left.resulttype.def)) then begin
  137. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_INT, jmp_lt, aword(t^._low),
  138. hregister, elselabel);
  139. end;
  140. if t^._low = t^._high then begin
  141. if t^._low - last = 0 then
  142. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, 0, hregister,
  143. blocklabel(t^.blockid))
  144. else
  145. gensub(aint(t^._low - last));
  146. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList, OC_EQ, blocklabel(t^.blockid));
  147. last := t^._low;
  148. lastrange := false;
  149. end else begin
  150. { it begins with the smallest label, if the value }
  151. { is even smaller then jump immediately to the }
  152. { ELSE-label }
  153. if first then begin
  154. { have we to ajust the first value ? }
  155. if (t^._low > get_min_value(left.resulttype.def)) then
  156. gensub(aint(t^._low));
  157. end else begin
  158. { if there is no unused label between the last and the }
  159. { present label then the lower limit can be checked }
  160. { immediately. else check the range in between: }
  161. gensub(aint(t^._low - last));
  162. if ((t^._low - last) <> 1) or (not lastrange) then
  163. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList, jmp_lt, elselabel);
  164. end;
  165. gensub(aint(t^._high - t^._low));
  166. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList, jmp_le, blocklabel(t^.blockid));
  167. last := t^._high;
  168. lastrange := true;
  169. end;
  170. first := false;
  171. if assigned(t^.greater) then
  172. genitem(t^.greater);
  173. end;
  174. begin
  175. { do we need to generate cmps? }
  176. if (with_sign and (min_label < 0)) or (opsize = OS_32) then
  177. genlinearcmplist(hp)
  178. else begin
  179. last := 0;
  180. lastrange := false;
  181. first := true;
  182. genitem(hp);
  183. cg.a_jmp_always(current_asmdata.CurrAsmList, elselabel);
  184. end;
  185. end;
  186. begin
  187. ccasenode := tppccasenode;
  188. end.