nppcset.pas 7.8 KB

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