nx86ld.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. reference_reset(href,0,[]);
  91. location.reference.index:=current_procinfo.got;
  92. location.reference.scalefactor:=1;
  93. location.reference.refaddr:=addr_tlsgd;
  94. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  95. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_LEA,S_L,location.reference,NR_EAX));
  96. cg.g_call(current_asmdata.CurrAsmList,'___tls_get_addr');
  97. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  98. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  99. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_EAX,hregister);
  100. reference_reset(location.reference,location.reference.alignment,location.reference.volatility);
  101. location.reference.base:=hregister;
  102. end;
  103. else
  104. Internalerror(2018110401);
  105. end;
  106. end;
  107. end;
  108. {$endif i386}
  109. end;
  110. end;
  111. begin
  112. cloadnode:=tx86loadnode;
  113. end.