nx64set.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86_64 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 nx64set;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. node,nset,pass_1,nx86set;
  23. type
  24. tx8664casenode = class(tx86casenode)
  25. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  26. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);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. TX8664CASENODE
  41. *****************************************************************************}
  42. procedure tx8664casenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  43. begin
  44. inc(max_linear_list,9);
  45. end;
  46. { Always generate position-independent jump table, it is twice less in size at a price
  47. of two extra instructions (which shouldn't cause more slowdown than pipeline trashing) }
  48. procedure tx8664casenode.genjumptable(hp : pcaselabel; min_,max_ : aint);
  49. var
  50. last: TConstExprInt;
  51. tablelabel: TAsmLabel;
  52. basereg,indexreg,jumpreg: TRegister;
  53. href: TReference;
  54. opcgsize: tcgsize;
  55. procedure genitem(list:TAsmList;t : pcaselabel);
  56. var
  57. i : aint;
  58. begin
  59. if assigned(t^.less) then
  60. genitem(list,t^.less);
  61. { fill possible hole }
  62. i:=last.svalue+1;
  63. while i<=t^._low.svalue-1 do
  64. begin
  65. list.concat(Tai_const.Create_rel_sym(aitconst_32bit,tablelabel,elselabel));
  66. inc(i);
  67. end;
  68. i:=t^._low.svalue;
  69. while i<=t^._high.svalue do
  70. begin
  71. list.concat(Tai_const.Create_rel_sym(aitconst_32bit,tablelabel,blocklabel(t^.blockid)));
  72. inc(i);
  73. end;
  74. last:=t^._high;
  75. if assigned(t^.greater) then
  76. genitem(list,t^.greater);
  77. end;
  78. begin
  79. last:=min_;
  80. opcgsize:=def_cgsize(opsize);
  81. if not(jumptable_no_range) then
  82. begin
  83. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  84. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opcgsize,aint(min_),hregister);
  85. { case expr greater than max_ => goto elselabel }
  86. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,OC_A,aint(max_)-aint(min_),hregister,elselabel);
  87. min_:=0;
  88. end;
  89. { local label in order to avoid using GOT }
  90. current_asmdata.getlabel(tablelabel,alt_data);
  91. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_ADDR);
  92. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_ADDR,hregister,indexreg);
  93. { load table address }
  94. reference_reset_symbol(href,tablelabel,0,4);
  95. basereg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  96. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,basereg);
  97. { load table slot, 32-bit sign extended }
  98. reference_reset_base(href,basereg,-aint(min_)*4,4);
  99. href.index:=indexreg;
  100. href.scalefactor:=4;
  101. jumpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  102. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_S32,OS_ADDR,href,jumpreg);
  103. { add table address }
  104. reference_reset_base(href,basereg,0,sizeof(pint));
  105. href.index:=jumpreg;
  106. href.scalefactor:=1;
  107. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,jumpreg);
  108. { and finally jump }
  109. emit_reg(A_JMP,S_NO,jumpreg);
  110. { generate jump table }
  111. new_section(current_procinfo.aktlocaldata,sec_rodata,current_procinfo.procdef.mangledname,4);
  112. current_procinfo.aktlocaldata.concat(Tai_label.Create(tablelabel));
  113. genitem(current_procinfo.aktlocaldata,hp);
  114. end;
  115. begin
  116. ccasenode:=tx8664casenode;
  117. end.