nx86ld.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {
  2. Copyright (c) 1998-2002,2015 by Florian Klaempfl
  3. Generate x86 assembler for in load 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 nx86ld;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,symsym,
  23. node,nld,ncgld;
  24. type
  25. tx86loadnode = class(tcgloadnode)
  26. protected
  27. procedure generate_threadvar_access(gvs: tstaticvarsym); override;
  28. end;
  29. implementation
  30. uses
  31. globals,
  32. cutils,verbose,systems,
  33. aasmbase,aasmtai,aasmdata,aasmcpu,
  34. cgutils,cgobj,
  35. symconst,symdef,symtable,
  36. cgbase,cpubase,parabase,paramgr,
  37. procinfo;
  38. {*****************************************************************************
  39. TX86LOADNODE
  40. *****************************************************************************}
  41. procedure tx86loadnode.generate_threadvar_access(gvs: tstaticvarsym);
  42. var
  43. paraloc1 : tcgpara;
  44. pd: tprocdef;
  45. href: treference;
  46. hregister : tregister;
  47. handled: boolean;
  48. begin
  49. handled:=false;
  50. if (tf_section_threadvars in target_info.flags) then
  51. begin
  52. if target_info.system in [system_i386_win32,system_x86_64_win64] then
  53. begin
  54. paraloc1.init;
  55. pd:=search_system_proc('fpc_tls_add');
  56. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  57. if not(vo_is_weak_external in gvs.varoptions) then
  58. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(gvs.mangledname,AT_DATA,use_indirect_symbol(gvs)),0,sizeof(pint),[])
  59. else
  60. reference_reset_symbol(href,current_asmdata.WeakRefAsmSymbol(gvs.mangledname,AT_DATA),0,sizeof(pint),[]);
  61. cg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,href,paraloc1);
  62. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  63. paraloc1.done;
  64. cg.g_call(current_asmdata.CurrAsmList,'FPC_TLS_ADD');
  65. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  66. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  67. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  68. location.reference.base:=hregister;
  69. handled:=true;
  70. end;
  71. end;
  72. if not handled then
  73. inherited;
  74. if (tf_section_threadvars in target_info.flags) then
  75. begin
  76. {$ifdef i386}
  77. case target_info.system of
  78. system_i386_linux,system_i386_android:
  79. begin
  80. case current_settings.tlsmodel of
  81. tlsm_local:
  82. begin
  83. location.reference.segment:=NR_GS;
  84. location.reference.refaddr:=addr_ntpoff;
  85. end;
  86. tlsm_general:
  87. begin
  88. if not(cs_create_pic in current_settings.moduleswitches) then
  89. Internalerror(2018110701);
  90. include(current_procinfo.flags,pi_needs_got);
  91. reference_reset(href,0,[]);
  92. location.reference.index:=current_procinfo.got;
  93. location.reference.scalefactor:=1;
  94. location.reference.refaddr:=addr_tlsgd;
  95. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  96. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_LEA,S_L,location.reference,NR_EAX));
  97. cg.g_call(current_asmdata.CurrAsmList,'___tls_get_addr');
  98. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  99. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  100. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_EAX,hregister);
  101. reference_reset(location.reference,location.reference.alignment,location.reference.volatility);
  102. location.reference.base:=hregister;
  103. end;
  104. else
  105. Internalerror(2018110401);
  106. end;
  107. end;
  108. end;
  109. {$endif i386}
  110. {$ifdef x86_64}
  111. case target_info.system of
  112. system_x86_64_linux:
  113. begin
  114. case current_settings.tlsmodel of
  115. tlsm_local:
  116. begin
  117. location.reference.segment:=NR_FS;
  118. location.reference.refaddr:=addr_tpoff;
  119. end;
  120. tlsm_general:
  121. begin
  122. if not(cs_create_pic in current_settings.moduleswitches) then
  123. Internalerror(2019012001);
  124. current_asmdata.CurrAsmList.concat(tai_const.Create_8bit($66));
  125. reference_reset(href,0,[]);
  126. location.reference.base:=NR_RIP;
  127. location.reference.scalefactor:=1;
  128. location.reference.refaddr:=addr_tlsgd;
  129. cg.getcpuregister(current_asmdata.CurrAsmList,NR_RDI);
  130. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_LEA,S_Q,location.reference,NR_RDI));
  131. current_asmdata.CurrAsmList.concat(tai_const.Create_8bit($66));
  132. current_asmdata.CurrAsmList.concat(tai_const.Create_8bit($66));
  133. current_asmdata.CurrAsmList.concat(tai_const.Create_8bit($48));
  134. cg.g_call(current_asmdata.CurrAsmList,'__tls_get_addr');
  135. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RDI);
  136. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  137. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  138. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_RAX,hregister);
  139. reference_reset(location.reference,location.reference.alignment,location.reference.volatility);
  140. location.reference.base:=hregister;
  141. end;
  142. else
  143. Internalerror(2019012002);
  144. end;
  145. end;
  146. end;
  147. {$endif x86_64}
  148. end;
  149. end;
  150. begin
  151. cloadnode:=tx86loadnode;
  152. end.