nppcset.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. function has_jumptable : boolean;override;
  26. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);override;
  27. procedure genlinearlist(hp : pcaselabel); override;
  28. end;
  29. implementation
  30. uses
  31. systems,
  32. verbose,globals,
  33. symconst,symdef,defutil,
  34. paramgr,
  35. cpuinfo,
  36. pass_2,cgcpu,
  37. ncon,
  38. tgobj,ncgutil,regvars,rgobj,aasmcpu,
  39. procinfo,
  40. cgutils;
  41. {*****************************************************************************
  42. TCGCASENODE
  43. *****************************************************************************}
  44. function tppccasenode.has_jumptable : boolean;
  45. begin
  46. has_jumptable:=true;
  47. end;
  48. procedure tppccasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  49. var
  50. table : tasmlabel;
  51. last : TConstExprInt;
  52. indexreg : tregister;
  53. href : treference;
  54. jumpsegment : TAAsmOutput;
  55. procedure genitem(t : pcaselabel);
  56. var
  57. i : aint;
  58. begin
  59. if assigned(t^.less) then
  60. genitem(t^.less);
  61. { fill possible hole }
  62. for i:=last+1 to t^._low-1 do
  63. jumpSegment.concat(Tai_const.Create_sym(elselabel));
  64. for i:=t^._low to t^._high do
  65. jumpSegment.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  66. last:=t^._high;
  67. if assigned(t^.greater) then
  68. genitem(t^.greater);
  69. end;
  70. begin
  71. if (cs_create_smart in aktmoduleswitches) or
  72. (af_smartlink_sections in target_asm.flags) then
  73. jumpsegment:=current_procinfo.aktlocaldata
  74. else
  75. jumpsegment:=asmlist[al_data];
  76. if not(jumptable_no_range) then
  77. begin
  78. { case expr less than min_ => goto elselabel }
  79. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(min_),hregister,elselabel);
  80. { case expr greater than max_ => goto elselabel }
  81. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,aint(max_),hregister,elselabel);
  82. end;
  83. objectlibrary.getlabel(table);
  84. { make it a 32bit register }
  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. if not(cs_littlesize in aktglobalswitches) then
  98. jumpSegment.concat(Tai_Align.Create_Op(4, 0));
  99. jumpSegment.concat(Tai_label.Create(table));
  100. last:=min_;
  101. genitem(hp);
  102. end;
  103. procedure tppccasenode.genlinearlist(hp : pcaselabel);
  104. var
  105. first, lastrange : boolean;
  106. last : TConstExprInt;
  107. procedure genitem(t : pcaselabel);
  108. procedure gensub(value:longint);
  109. var
  110. tmpreg: tregister;
  111. begin
  112. value := -value;
  113. if (value >= low(smallint)) and
  114. (value <= high(smallint)) then
  115. exprasmlist.concat(taicpu.op_reg_reg_const(A_ADDIC_,hregister,
  116. hregister,value))
  117. else
  118. begin
  119. tmpreg := cg.getintregister(exprasmlist,OS_INT);
  120. cg.a_load_const_reg(exprasmlist,OS_INT,value,tmpreg);
  121. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ADD_,hregister,
  122. hregister,tmpreg));
  123. end;
  124. end;
  125. begin
  126. if assigned(t^.less) then
  127. genitem(t^.less);
  128. { need we to test the first value }
  129. if first and (t^._low>get_min_value(left.resulttype.def)) then
  130. begin
  131. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,jmp_lt,aword(t^._low),hregister,elselabel);
  132. end;
  133. if t^._low=t^._high then
  134. begin
  135. if t^._low-last=0 then
  136. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  137. else
  138. gensub(longint(t^._low-last));
  139. tcgppc(cg).a_jmp_cond(exprasmlist,OC_EQ,blocklabel(t^.blockid));
  140. last:=t^._low;
  141. lastrange := false;
  142. end
  143. else
  144. begin
  145. { it begins with the smallest label, if the value }
  146. { is even smaller then jump immediately to the }
  147. { ELSE-label }
  148. if first then
  149. begin
  150. { have we to ajust the first value ? }
  151. if (t^._low>get_min_value(left.resulttype.def)) then
  152. gensub(longint(t^._low));
  153. end
  154. else
  155. begin
  156. { if there is no unused label between the last and the }
  157. { present label then the lower limit can be checked }
  158. { immediately. else check the range in between: }
  159. gensub(longint(t^._low-last));
  160. if ((t^._low-last) <> 1) or
  161. (not lastrange) then
  162. tcgppc(cg).a_jmp_cond(exprasmlist,jmp_lt,elselabel);
  163. end;
  164. gensub(longint(t^._high-t^._low));
  165. tcgppc(cg).a_jmp_cond(exprasmlist,jmp_le,blocklabel(t^.blockid));
  166. last:=t^._high;
  167. lastrange := true;
  168. end;
  169. first:=false;
  170. if assigned(t^.greater) then
  171. genitem(t^.greater);
  172. end;
  173. begin
  174. { do we need to generate cmps? }
  175. if (with_sign and (min_label<0)) or
  176. (opsize = OS_32) then
  177. genlinearcmplist(hp)
  178. else
  179. begin
  180. last:=0;
  181. lastrange:=false;
  182. first:=true;
  183. genitem(hp);
  184. cg.a_jmp_always(exprasmlist,elselabel);
  185. end;
  186. end;
  187. begin
  188. ccasenode:=tppccasenode;
  189. end.