n386set.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i386 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 n386set;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. node,nset,pass_1,nx86set;
  23. type
  24. ti386casenode = class(tx86casenode)
  25. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  26. procedure genlinearlist(hp : pcaselabel);override;
  27. end;
  28. implementation
  29. uses
  30. systems,
  31. verbose,globals,constexp,
  32. symconst,symdef,defutil,
  33. aasmbase,aasmtai,aasmdata,aasmcpu,
  34. cgbase,pass_2,
  35. ncon,
  36. cpubase,cpuinfo,procinfo,
  37. cga,cgutils,cgobj,ncgutil,
  38. cgx86;
  39. {*****************************************************************************
  40. TI386CASENODE
  41. *****************************************************************************}
  42. procedure ti386casenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  43. begin
  44. { a jump table crashes the pipeline! }
  45. if current_settings.optimizecputype=cpu_386 then
  46. inc(max_linear_list,3)
  47. else if current_settings.optimizecputype=cpu_Pentium then
  48. inc(max_linear_list,6)
  49. else if current_settings.optimizecputype in [cpu_Pentium2,cpu_Pentium3] then
  50. inc(max_linear_list,9)
  51. else if current_settings.optimizecputype=cpu_Pentium4 then
  52. inc(max_linear_list,14);
  53. end;
  54. procedure ti386casenode.genlinearlist(hp : pcaselabel);
  55. var
  56. first : boolean;
  57. lastrange : boolean;
  58. last : TConstExprInt;
  59. cond_lt,cond_le : tresflags;
  60. opcgsize: tcgsize;
  61. procedure genitem(t : pcaselabel);
  62. begin
  63. opcgsize:=def_cgsize(opsize);
  64. if assigned(t^.less) then
  65. genitem(t^.less);
  66. { need we to test the first value }
  67. if first and (t^._low>get_min_value(left.resultdef)) then
  68. begin
  69. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  70. end;
  71. if t^._low=t^._high then
  72. begin
  73. if t^._low-last=0 then
  74. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  75. else
  76. begin
  77. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue-last.svalue), hregister);
  78. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,blocklabel(t^.blockid));
  79. end;
  80. last:=t^._low;
  81. lastrange:=false;
  82. end
  83. else
  84. begin
  85. { it begins with the smallest label, if the value }
  86. { is even smaller then jump immediately to the }
  87. { ELSE-label }
  88. if first then
  89. begin
  90. { have we to ajust the first value ? }
  91. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  92. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue), hregister);
  93. end
  94. else
  95. begin
  96. { if there is no unused label between the last and the }
  97. { present label then the lower limit can be checked }
  98. { immediately. else check the range in between: }
  99. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue-last.svalue), hregister);
  100. { no jump necessary here if the new range starts at }
  101. { at the value following the previous one }
  102. if ((t^._low-last) <> 1) or
  103. (not lastrange) then
  104. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  105. end;
  106. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  107. using a_op_const_reg(OP_SUB) is not possible }
  108. emit_const_reg(A_SUB,TCGSize2OpSize[opcgsize],aint(t^._high.svalue-t^._low.svalue),hregister);
  109. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  110. last:=t^._high;
  111. lastrange:=true;
  112. end;
  113. first:=false;
  114. if assigned(t^.greater) then
  115. genitem(t^.greater);
  116. end;
  117. begin
  118. if with_sign then
  119. begin
  120. cond_lt:=F_L;
  121. cond_le:=F_LE;
  122. end
  123. else
  124. begin
  125. cond_lt:=F_B;
  126. cond_le:=F_BE;
  127. end;
  128. { do we need to generate cmps? }
  129. if (with_sign and (min_label<0)) then
  130. genlinearcmplist(hp)
  131. else
  132. begin
  133. last:=0;
  134. lastrange:=false;
  135. first:=true;
  136. genitem(hp);
  137. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  138. end;
  139. end;
  140. begin
  141. ccasenode:=ti386casenode;
  142. end.