nx64set.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. sectype: TAsmSectiontype;
  56. jtitemconsttype: taiconst_type;
  57. procedure genitem(list:TAsmList;t : pcaselabel);
  58. var
  59. i : aint;
  60. begin
  61. if assigned(t^.less) then
  62. genitem(list,t^.less);
  63. { fill possible hole }
  64. i:=last.svalue+1;
  65. while i<=t^._low.svalue-1 do
  66. begin
  67. list.concat(Tai_const.Create_rel_sym(jtitemconsttype,tablelabel,elselabel));
  68. inc(i);
  69. end;
  70. i:=t^._low.svalue;
  71. while i<=t^._high.svalue do
  72. begin
  73. list.concat(Tai_const.Create_rel_sym(jtitemconsttype,tablelabel,blocklabel(t^.blockid)));
  74. inc(i);
  75. end;
  76. last:=t^._high;
  77. if assigned(t^.greater) then
  78. genitem(list,t^.greater);
  79. end;
  80. begin
  81. if not(target_info.system in systems_darwin) then
  82. jtitemconsttype:=aitconst_32bit
  83. else
  84. { see https://gmplib.org/list-archives/gmp-bugs/2012-December/002836.html }
  85. jtitemconsttype:=aitconst_darwin_dwarf_delta32;
  86. last:=min_;
  87. opcgsize:=def_cgsize(opsize);
  88. if not(jumptable_no_range) then
  89. begin
  90. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  91. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opcgsize,aint(min_),hregister);
  92. { case expr greater than max_ => goto elselabel }
  93. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,OC_A,aint(max_)-aint(min_),hregister,elselabel);
  94. min_:=0;
  95. end;
  96. { local label in order to avoid using GOT }
  97. current_asmdata.getlabel(tablelabel,alt_data);
  98. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_ADDR);
  99. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_ADDR,hregister,indexreg);
  100. { load table address }
  101. reference_reset_symbol(href,tablelabel,0,4);
  102. basereg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  103. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,basereg);
  104. { load table slot, 32-bit sign extended }
  105. reference_reset_base(href,basereg,-aint(min_)*4,4);
  106. href.index:=indexreg;
  107. href.scalefactor:=4;
  108. jumpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  109. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_S32,OS_ADDR,href,jumpreg);
  110. { add table address }
  111. reference_reset_base(href,basereg,0,sizeof(pint));
  112. href.index:=jumpreg;
  113. href.scalefactor:=1;
  114. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,jumpreg);
  115. { and finally jump }
  116. emit_reg(A_JMP,S_NO,jumpreg);
  117. { generate jump table }
  118. if not(target_info.system in systems_darwin) then
  119. sectype:=sec_rodata
  120. else
  121. { on Mac OS X, dead code stripping ("smart linking") happens based on
  122. global symbols: every global/static symbol (symbols that do not
  123. start with "L") marks the start of a new "subsection" that is
  124. discarded by the linker if there are no references to this symbol.
  125. This means that if you put the jump table in the rodata section, it
  126. will become part of the block of data associated with the previous
  127. non-L-label in the rodata section and stay or be thrown away
  128. depending on whether that block of data is referenced. Therefore,
  129. jump tables must be added in the code section and since aktlocaldata
  130. is inserted right after the routine, it will become part of the
  131. same subsection that contains the routine's code }
  132. sectype:=sec_code;
  133. new_section(current_procinfo.aktlocaldata,sectype,current_procinfo.procdef.mangledname,4);
  134. current_procinfo.aktlocaldata.concat(Tai_label.Create(tablelabel));
  135. genitem(current_procinfo.aktlocaldata,hp);
  136. end;
  137. begin
  138. ccasenode:=tx8664casenode;
  139. end.