llvm.org GIT mirror llvm / 7f70559
* PIC codegen for X86/Linux has been implemented * PIC-aware internal structures in X86 Codegen have been refactored * Visibility (default/weak) has been added * Docs fixes (external weak linkage, visibility, formatting) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33136 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 13 years ago
31 changed file(s) with 604 addition(s) and 175 deletion(s). Raw diff Collapse all Expand all
742742
  • #3: LLVM 1.3
  • 743743
  • #4: LLVM 1.3.x (not released)
  • 744744
  • #5: LLVM 1.4 and newer
  • 745
    746745
    747746

    Note that we plan to eventually expand the target description

    748747 capabilities
    10811080 If this bit is set, a SectionID follows this vbr.
    10821081
    10831082
    1084 bit(10-31)
    1083 bit(10-12)
    1084 Visibility style: 0=Default, 1=Hidden.
    1085
    1086
    1087 bit(13-31)
    10851088 Currently unassigned.
    10861089
    10871090
    13361339
    13371340
    13381341
    1339
    Undef Entriesa>div>
    1342
    Undef Entriesdiv>
    13401343
    13411344
    13421345

    When the number of operands to the constant is one, we have an 'undef' value

    13441347
    13451348
    13461349
    1347
    Inline Assembler Entriesa>div>
    1350
    Inline Assembler Entriesdiv>
    13481351
    13491352
    13501353

    Inline Assembler entries are stored in the constant pool, though they are not

    13681371
    13691372
    13701373 uint32_vbr
    1371 Flagssup>td>
    1374 Flagstd>
    13721375
    13731376
    13741377
    13791382
    13801383 13811384 1382
    Constant Expression Entriesa>div> 1385
    Constant Expression Entriesdiv> 13831386 13841387
    13851388 14351438 14361439 14371440 uint32_vbr 1438 The linkage type of the function: 0=External, 1=Weak, 1439 2=Appending, 3=Internal, 4=LinkOnce, 5=DllImport, 6=DllExport1 1441 The linkage and 1442 visibility style field 14401443 14411444 14421445 block 14691472 be written. Compaction tables are only written if they will actually 14701473 save bytecode space. If not, then a regular constant pool is written. 14711474 1472
    1475 1476 1477
    Linkage and 1478 visibility word 1479
    1480
    1481 1482
    1483
    1484
    1485 Type
    1486 Field Description
    1487
    1488
    1489 bit(0-15)
    1490 The linkage type of the function: 0=External, 1=Weak,
    1491 2=Appending, 3=Internal, 4=LinkOnce, 5=DllImport, 6=DllExport1
    1492
    1493
    1494 bit(16-31)
    1495 Visibility style: 0=Default, 1=Hidden.
    1496
    1497
    1498
    1499 1500
    1501
    1502 14731503 14741504
    274274 For example, %foo, %DivisionByZero, %a.really.long.identifier. The actual
    275275 regular expression used is '%[a-zA-Z$._][a-zA-Z$._0-9]*'.
    276276 Identifiers which require other characters in their names can be surrounded
    277 with quotes. In this way, anything except a " character can be used
    277 with quotes. In this way, anything except a " character can be used
    278278 in a name.
    279279
    280280
  • Unnamed values are represented as an unsigned numeric value with a '%'
  • 451451 "sections" with identical names when .o files are linked.
    452452
    453453
    454
    extern_weak:
    455
    The semantics of this linkage follow the ELF model: the symbol is weak
    456 until linked, if not linked, the symbol becomes null instead of being an
    457 undefined reference.
    458
    459
    460
    454461
    externally visible:
    455462
    456463
    If none of the above identifiers are used, the global is externally
    457464 visible, meaning that it participates in linkage and can be used to resolve
    458465 external symbol references.
    459
    460
    461
    extern_weak:
    462
    463
    "extern_weak" TBD
    464466
    465467
    466468

    469471 DLLs.
    470472

    471473
    474
    472475
    dllimport:
    473476
    474477
    "dllimport" linkage causes the compiler to reference a function
    488491
    489492
    490493
    491

    For example, since the ".LC0"

    494

    For example, since the ".LC0"

    492495 variable is defined to be internal, if another module defined a ".LC0"
    493496 variable and was linked with this one, one of the two would be renamed,
    494497 preventing a collision. Since "main" and "puts" are
    496499 outside of the current module.

    497500

    It is illegal for a function declaration

    498501 to have any linkage type other than "externally visible", dllimport,
    499 or extern_weak.a>p>
    502 or extern_weak.p>
    500503
    501504
    502505
    675678
    676679

    Parameter attributes consist of an at sign (@) followed by either a single

    677680 keyword or a comma separate list of keywords enclosed in parentheses. For
    678 example:<pre>
    681 example:</p><pre>
    679682 %someFunc = i16 @zext (i8 @(sext) %someParam)
    680683 %someFunc = i16 @zext (i8 @zext %someParam)
    681 Note that the two function types above are unique because the parameter
    682 has a different attribute (@sext in the first one, @zext in the second).

    683
    684

    Currently, only the following parameter attributes are defined:

    684

    Note that the two function types above are unique because the parameter has

    685 a different attribute (@sext in the first one, @zext in the second).

    686
    687

    Currently, only the following parameter attributes are defined:

    685688
    686689
    @zext
    687690
    This indicates that the parameter should be zero extended just before
    689692
    @sext
    690693
    This indicates that the parameter should be sign extended just before
    691694 a call to this function.
    692

    695
    693696
    694697

    The current motivation for parameter attributes is to enable the sign and

    695698 zero extend information necessary for the C calling convention to be passed
    30523055
    Arguments:
    30533056

    The 'inttoptr' instruction takes an integer

    30543057 value to cast, and a type to cast it to, which must be a
    3055 pointer type.
    3058 pointer type.
    30563059
    30573060
    Semantics:
    30583061

    The 'inttoptr' instruction converts value to type

    31713174 true if var1 is less than var2.
    31723175
  • sle: interprets the operands as signed values and yields
  • 31733176 true if var1 is less than or equal to var2.
    3174
    31753177
    31763178

    If the operands are pointer typed, the pointer

    31773179 values are treated as integers and then compared.

    111111 const Type* ElemType, ///< The type of the global variable
    112112 bool isConstant, ///< Whether the GV is constant or not
    113113 GlobalValue::LinkageTypes,///< The linkage type of the GV
    114 GlobalValue::VisibilityTypes,///< The visibility style of the GV
    114115 unsigned SlotNum, ///< Slot number of GV
    115116 unsigned initSlot ///< Slot number of GV's initializer (0 if none)
    116117 ) {}
    3737 ExternalWeakLinkage, /// ExternalWeak linkage description
    3838 GhostLinkage /// Stand-in functions for streaming fns from BC files
    3939 };
    40 enum VisibilityTypes {
    41 DefaultVisibility,
    42 HiddenVisibility
    43 };
    4044 protected:
    4145 GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
    4246 LinkageTypes linkage, const std::string &name = "")
    43 : Constant(Ty, vty, Ops, NumOps, name),
    44 Parent(0), Linkage(linkage), Alignment(0) { }
    47 : Constant(Ty, vty, Ops, NumOps, name), Parent(0),
    48 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) { }
    4549
    4650 Module *Parent;
    4751 LinkageTypes Linkage; // The linkage of this global
    52 VisibilityTypes Visibility; // The visibility style of this global
    4853 unsigned Alignment; // Alignment of this symbol, must be power of two
    4954 std::string Section; // Section to emit this into, empty mean default
    5055 public:
    5762 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
    5863 Alignment = Align;
    5964 }
    65
    66 VisibilityTypes getVisibility() const { return Visibility; }
    67 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
    68 void setVisibility(VisibilityTypes V) { Visibility = V; }
    6069
    6170 bool hasSection() const { return !Section.empty(); }
    6271 const std::string &getSection() const { return Section; }
    202202 appending { return APPENDING; }
    203203 dllimport { return DLLIMPORT; }
    204204 dllexport { return DLLEXPORT; }
    205 hidden { return HIDDEN; }
    205206 extern_weak { return EXTERN_WEAK; }
    206207 external { return EXTERNAL; }
    207208 implementation { return IMPLEMENTATION; }
    202202 appending { return APPENDING; }
    203203 dllimport { return DLLIMPORT; }
    204204 dllexport { return DLLEXPORT; }
    205 hidden { return HIDDEN; }
    205206 extern_weak { return EXTERN_WEAK; }
    206207 external { return EXTERNAL; }
    207208 implementation { return IMPLEMENTATION; }
    209209 std::map LateResolveValues;
    210210 bool isDeclare; // Is this function a forward declararation?
    211211 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
    212 GlobalValue::VisibilityTypes Visibility;
    212213
    213214 /// BBForwardRefs - When we see forward references to basic blocks, keep
    214215 /// track of them here.
    219220 inline PerFunctionInfo() {
    220221 CurrentFunction = 0;
    221222 isDeclare = false;
    222 Linkage = GlobalValue::ExternalLinkage;
    223 Linkage = GlobalValue::ExternalLinkage;
    224 Visibility = GlobalValue::DefaultVisibility;
    223225 }
    224226
    225227 inline void FunctionStart(Function *M) {
    244246 CurrentFunction = 0;
    245247 isDeclare = false;
    246248 Linkage = GlobalValue::ExternalLinkage;
    249 Visibility = GlobalValue::DefaultVisibility;
    247250 }
    248251 } CurFun; // Info for the current function...
    249252
    647650 /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
    648651 /// this is a declaration, otherwise it is a definition.
    649652 static GlobalVariable *
    650 ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
    653 ParseGlobalVariable(char *NameStr,
    654 GlobalValue::LinkageTypes Linkage,
    655 GlobalValue::VisibilityTypes Visibility,
    651656 bool isConstantGlobal, const Type *Ty,
    652657 Constant *Initializer) {
    653658 if (isa(Ty)) {
    680685 CurModule.CurrentModule->getGlobalList().push_back(GV);
    681686 GV->setInitializer(Initializer);
    682687 GV->setLinkage(Linkage);
    688 GV->setVisibility(Visibility);
    683689 GV->setConstant(isConstantGlobal);
    684690 InsertValue(GV, CurModule.Values);
    685691 return GV;
    701707 GlobalVariable *GV =
    702708 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
    703709 CurModule.CurrentModule);
    710 GV->setVisibility(Visibility);
    704711 InsertValue(GV, CurModule.Values);
    705712 return GV;
    706713 }
    897904 std::vector *ConstVector;
    898905
    899906 llvm::GlobalValue::LinkageTypes Linkage;
    907 llvm::GlobalValue::VisibilityTypes Visibility;
    900908 llvm::FunctionType::ParameterAttributes ParamAttrs;
    901909 int64_t SInt64Val;
    902910 uint64_t UInt64Val;
    939947 %type OptSideEffect // 'sideeffect' or not.
    940948 %type GVInternalLinkage GVExternalLinkage
    941949 %type FunctionDefineLinkage FunctionDeclareLinkage
    950 %type GVVisibilityStyle
    942951 %type BigOrLittle
    943952
    944953 // ValueRef - Unresolved reference to a definition or BB
    10091018
    10101019 // Function Attributes
    10111020 %token NORETURN
    1021
    1022 // Visibility Styles
    1023 %token DEFAULT HIDDEN
    10121024
    10131025 %start Module
    10141026 %%
    10781090 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
    10791091 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
    10801092 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
    1093 ;
    1094
    1095 GVVisibilityStyle
    1096 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
    1097 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
    10811098 ;
    10821099
    10831100 FunctionDeclareLinkage
    12241241 if (isVarArg) Params.pop_back();
    12251242
    12261243 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
    1227 delete $3; // Delete the argument list
    1244 delete $3; // Delete the argument list
    12281245 delete $1; // Delete the return type handle
    12291246 $$ = new PATypeHolder(HandleUpRefs(FT));
    12301247 CHECK_FOR_ERROR
    18821899 }
    18831900 CHECK_FOR_ERROR
    18841901 }
    1885 | OptAssign GlobalType ConstVal { /* "Externally Visible" Linkage */
    1886 if ($3 == 0)
    1902 | OptAssign GVVisibilityStyle GlobalType ConstVal { /* "Externally Visible" Linkage */
    1903 if ($4 == 0)
    18871904 GEN_ERROR("Global value initializer is not a constant!");
    1888 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage, $2,
    1889 $3->getType(), $3);
    1905 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
    1906 $2, $3, $4->getType(), $4);
    18901907 CHECK_FOR_ERROR
    18911908 } GlobalVarAttributes {
    18921909 CurGV = 0;
    18931910 }
    1894 | OptAssign GVInternalLinkage GlobalType ConstVal {
    1895 if ($4 == 0)
    1911 | OptAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
    1912 if ($5 == 0)
    18961913 GEN_ERROR("Global value initializer is not a constant!");
    1897 CurGV = ParseGlobalVariable($1, $2, $3, $4->getType(), $4);
    1914 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
    18981915 CHECK_FOR_ERROR
    18991916 } GlobalVarAttributes {
    19001917 CurGV = 0;
    19011918 }
    1902 | OptAssign GVExternalLinkage GlobalType Types {
    1903 if (!UpRefs.empty())
    1904 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
    1905 CurGV = ParseGlobalVariable($1, $2, $3, *$4, 0);
    1906 CHECK_FOR_ERROR
    1907 delete $4;
    1919 | OptAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
    1920 if (!UpRefs.empty())
    1921 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
    1922 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
    1923 CHECK_FOR_ERROR
    1924 delete $5;
    19081925 } GlobalVarAttributes {
    19091926 CurGV = 0;
    19101927 CHECK_FOR_ERROR
    21022119 // correctly handle cases, when pointer to function is passed as argument to
    21032120 // another function.
    21042121 Fn->setLinkage(CurFun.Linkage);
    2122 Fn->setVisibility(CurFun.Visibility);
    21052123 }
    21062124 Fn->setCallingConv($1);
    21072125 Fn->setAlignment($9);
    21352153
    21362154 BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
    21372155
    2138 FunctionHeader : FunctionDefineLinkage FunctionHeaderH BEGIN {
    2156 FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
    21392157 $$ = CurFun.CurrentFunction;
    21402158
    21412159 // Make sure that we keep track of the linkage type even if there was a
    21422160 // previous "declare".
    21432161 $$->setLinkage($1);
    2162 $$->setVisibility($2);
    21442163 };
    21452164
    21462165 END : ENDTOK | '}'; // Allow end of '}' to end a function
    21502169 CHECK_FOR_ERROR
    21512170 };
    21522171
    2153 FunctionProto : FunctionDeclareLinkage FunctionHeaderH {
    2172 FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
    21542173 CurFun.CurrentFunction->setLinkage($1);
    2174 CurFun.CurrentFunction->setVisibility($2);
    21552175 $$ = CurFun.CurrentFunction;
    21562176 CurFun.FunctionDone();
    21572177 CHECK_FOR_ERROR
    209209 std::map LateResolveValues;
    210210 bool isDeclare; // Is this function a forward declararation?
    211211 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
    212 GlobalValue::VisibilityTypes Visibility;
    212213
    213214 /// BBForwardRefs - When we see forward references to basic blocks, keep
    214215 /// track of them here.
    219220 inline PerFunctionInfo() {
    220221 CurrentFunction = 0;
    221222 isDeclare = false;
    222 Linkage = GlobalValue::ExternalLinkage;
    223 Linkage = GlobalValue::ExternalLinkage;
    224 Visibility = GlobalValue::DefaultVisibility;
    223225 }
    224226
    225227 inline void FunctionStart(Function *M) {
    244246 CurrentFunction = 0;
    245247 isDeclare = false;
    246248 Linkage = GlobalValue::ExternalLinkage;
    249 Visibility = GlobalValue::DefaultVisibility;
    247250 }
    248251 } CurFun; // Info for the current function...
    249252
    647650 /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
    648651 /// this is a declaration, otherwise it is a definition.
    649652 static GlobalVariable *
    650 ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
    653 ParseGlobalVariable(char *NameStr,
    654 GlobalValue::LinkageTypes Linkage,
    655 GlobalValue::VisibilityTypes Visibility,
    651656 bool isConstantGlobal, const Type *Ty,
    652657 Constant *Initializer) {
    653658 if (isa(Ty)) {
    680685 CurModule.CurrentModule->getGlobalList().push_back(GV);
    681686 GV->setInitializer(Initializer);
    682687 GV->setLinkage(Linkage);
    688 GV->setVisibility(Visibility);
    683689 GV->setConstant(isConstantGlobal);
    684690 InsertValue(GV, CurModule.Values);
    685691 return GV;
    701707 GlobalVariable *GV =
    702708 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
    703709 CurModule.CurrentModule);
    710 GV->setVisibility(Visibility);
    704711 InsertValue(GV, CurModule.Values);
    705712 return GV;
    706713 }
    897904 std::vector *ConstVector;
    898905
    899906 llvm::GlobalValue::LinkageTypes Linkage;
    907 llvm::GlobalValue::VisibilityTypes Visibility;
    900908 llvm::FunctionType::ParameterAttributes ParamAttrs;
    901909 int64_t SInt64Val;
    902910 uint64_t UInt64Val;
    939947 %type OptSideEffect // 'sideeffect' or not.
    940948 %type GVInternalLinkage GVExternalLinkage
    941949 %type FunctionDefineLinkage FunctionDeclareLinkage
    950 %type GVVisibilityStyle
    942951 %type BigOrLittle
    943952
    944953 // ValueRef - Unresolved reference to a definition or BB
    10091018
    10101019 // Function Attributes
    10111020 %token NORETURN
    1021
    1022 // Visibility Styles
    1023 %token DEFAULT HIDDEN
    10121024
    10131025 %start Module
    10141026 %%
    10781090 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
    10791091 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
    10801092 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
    1093 ;
    1094
    1095 GVVisibilityStyle
    1096 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
    1097 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
    10811098 ;
    10821099
    10831100 FunctionDeclareLinkage
    12241241 if (isVarArg) Params.pop_back();
    12251242
    12261243 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
    1227 delete $3; // Delete the argument list
    1244 delete $3; // Delete the argument list
    12281245 delete $1; // Delete the return type handle
    12291246 $$ = new PATypeHolder(HandleUpRefs(FT));
    12301247 CHECK_FOR_ERROR
    18821899 }
    18831900 CHECK_FOR_ERROR
    18841901 }
    1885 | OptAssign GlobalType ConstVal { /* "Externally Visible" Linkage */
    1886 if ($3 == 0)
    1902 | OptAssign GVVisibilityStyle GlobalType ConstVal { /* "Externally Visible" Linkage */
    1903 if ($4 == 0)
    18871904 GEN_ERROR("Global value initializer is not a constant!");
    1888 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage, $2,
    1889 $3->getType(), $3);
    1905 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
    1906 $2, $3, $4->getType(), $4);
    18901907 CHECK_FOR_ERROR
    18911908 } GlobalVarAttributes {
    18921909 CurGV = 0;
    18931910 }
    1894 | OptAssign GVInternalLinkage GlobalType ConstVal {
    1895 if ($4 == 0)
    1911 | OptAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
    1912 if ($5 == 0)
    18961913 GEN_ERROR("Global value initializer is not a constant!");
    1897 CurGV = ParseGlobalVariable($1, $2, $3, $4->getType(), $4);
    1914 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
    18981915 CHECK_FOR_ERROR
    18991916 } GlobalVarAttributes {
    19001917 CurGV = 0;
    19011918 }
    1902 | OptAssign GVExternalLinkage GlobalType Types {
    1903 if (!UpRefs.empty())
    1904 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
    1905 CurGV = ParseGlobalVariable($1, $2, $3, *$4, 0);
    1906 CHECK_FOR_ERROR
    1907 delete $4;
    1919 | OptAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
    1920 if (!UpRefs.empty())
    1921 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
    1922 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
    1923 CHECK_FOR_ERROR
    1924 delete $5;
    19081925 } GlobalVarAttributes {
    19091926 CurGV = 0;
    19101927 CHECK_FOR_ERROR
    21022119 // correctly handle cases, when pointer to function is passed as argument to
    21032120 // another function.
    21042121 Fn->setLinkage(CurFun.Linkage);
    2122 Fn->setVisibility(CurFun.Visibility);
    21052123 }
    21062124 Fn->setCallingConv($1);
    21072125 Fn->setAlignment($9);
    21352153
    21362154 BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
    21372155
    2138 FunctionHeader : FunctionDefineLinkage FunctionHeaderH BEGIN {
    2156 FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
    21392157 $$ = CurFun.CurrentFunction;
    21402158
    21412159 // Make sure that we keep track of the linkage type even if there was a
    21422160 // previous "declare".
    21432161 $$->setLinkage($1);
    2162 $$->setVisibility($2);
    21442163 };
    21452164
    21462165 END : ENDTOK | '}'; // Allow end of '}' to end a function
    21502169 CHECK_FOR_ERROR
    21512170 };
    21522171
    2153 FunctionProto : FunctionDeclareLinkage FunctionHeaderH {
    2172 FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
    21542173 CurFun.CurrentFunction->setLinkage($1);
    2174 CurFun.CurrentFunction->setVisibility($2);
    21552175 $$ = CurFun.CurrentFunction;
    21562176 CurFun.FunctionDone();
    21572177 CHECK_FOR_ERROR
    161161 const Type* ElemType,
    162162 bool isConstant,
    163163 GlobalValue::LinkageTypes Linkage,
    164 GlobalValue::VisibilityTypes Visibility,
    164165 unsigned SlotNum,
    165166 unsigned initSlot
    166167 ) {
    168169 *os << " GV: "
    169170 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
    170171 << ( isConstant? "Constant, " : "Variable, ")
    171 << " Linkage=" << Linkage << " Type=";
    172 << " Linkage=" << Linkage
    173 << " Visibility="<< Visibility
    174 << " Type=";
    172175 WriteTypeSymbolic(*os, ElemType, M);
    173176 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
    174177 << "\n";
    205208 *os << " Function Decl: ";
    206209 WriteTypeSymbolic(*os,Func->getType(),M);
    207210 *os <<", Linkage=" << Func->getLinkage();
    211 *os <<", Visibility=" << Func->getVisibility();
    208212 *os << "\n";
    209213 }
    210214 }
    310314 if (os) {
    311315 *os << " BLOCK: Function {\n"
    312316 << " Linkage: " << Func->getLinkage() << "\n"
    317 << " Visibility: " << Func->getVisibility() << "\n"
    313318 << " Type: ";
    314319 WriteTypeSymbolic(*os,Func->getType(),M);
    315320 *os << "\n";
    16271627
    16281628 unsigned FuncSize = BlockEnd - At;
    16291629 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
    1630
    1631 unsigned LinkageType = read_vbr_uint();
    1632 switch (LinkageType) {
    1630 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
    1631
    1632 unsigned rWord = read_vbr_uint();
    1633 unsigned LinkageID = rWord & 65535;
    1634 unsigned VisibilityID = rWord >> 16;
    1635 switch (LinkageID) {
    16331636 case 0: Linkage = GlobalValue::ExternalLinkage; break;
    16341637 case 1: Linkage = GlobalValue::WeakLinkage; break;
    16351638 case 2: Linkage = GlobalValue::AppendingLinkage; break;
    16431646 Linkage = GlobalValue::InternalLinkage;
    16441647 break;
    16451648 }
    1649 switch (VisibilityID) {
    1650 case 0: Visibility = GlobalValue::DefaultVisibility; break;
    1651 case 1: Visibility = GlobalValue::HiddenVisibility; break;
    1652 default:
    1653 error("Unknown visibility type: " + utostr(VisibilityID));
    1654 Visibility = GlobalValue::DefaultVisibility;
    1655 break;
    1656 }
    16461657
    16471658 F->setLinkage(Linkage);
    1659 F->setVisibility(Visibility);
    16481660 if (Handler) Handler->handleFunctionBegin(F,FuncSize);
    16491661
    16501662 // Keep track of how many basic blocks we have read in...
    18431855 // Linkage, bit4+ = slot#
    18441856 unsigned SlotNo = VarType >> 5;
    18451857 unsigned LinkageID = (VarType >> 2) & 7;
    1858 unsigned VisibilityID = 0;
    18461859 bool isConstant = VarType & 1;
    18471860 bool hasInitializer = (VarType & 2) != 0;
    18481861 unsigned Alignment = 0;
    18521865 if (LinkageID == 3 && !hasInitializer) {
    18531866 unsigned ExtWord = read_vbr_uint();
    18541867 // The extension word has this format: bit 0 = has initializer, bit 1-3 =
    1855 // linkage, bit 4-8 = alignment (log2), bits 10+ = future use.
    1868 // linkage, bit 4-8 = alignment (log2), bit 9 = has section,
    1869 // bits 10-12 = visibility, bits 13+ = future use.
    18561870 hasInitializer = ExtWord & 1;
    18571871 LinkageID = (ExtWord >> 1) & 7;
    18581872 Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
    1873 VisibilityID = (ExtWord >> 10) & 7;
    18591874
    18601875 if (ExtWord & (1 << 9)) // Has a section ID.
    18611876 GlobalSectionID = read_vbr_uint();
    18761891 Linkage = GlobalValue::InternalLinkage;
    18771892 break;
    18781893 }
    1879
    1894 GlobalValue::VisibilityTypes Visibility;
    1895 switch (VisibilityID) {
    1896 case 0: Visibility = GlobalValue::DefaultVisibility; break;
    1897 case 1: Visibility = GlobalValue::HiddenVisibility; break;
    1898 default:
    1899 error("Unknown visibility type: " + utostr(VisibilityID));
    1900 Visibility = GlobalValue::DefaultVisibility;
    1901 break;
    1902 }
    1903
    18801904 const Type *Ty = getType(SlotNo);
    18811905 if (!Ty)
    18821906 error("Global has no type! SlotNo=" + utostr(SlotNo));
    18901914 GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
    18911915 0, "", TheModule);
    18921916 GV->setAlignment(Alignment);
    1917 GV->setVisibility(Visibility);
    18931918 insertValue(GV, SlotNo, ModuleValues);
    18941919
    18951920 if (GlobalSectionID != 0)
    19031928
    19041929 // Notify handler about the global value.
    19051930 if (Handler)
    1906 Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo,initSlot);
    1931 Handler->handleGlobalVariable(ElTy, isConstant, Linkage, Visibility,
    1932 SlotNo, initSlot);
    19071933
    19081934 // Get next item
    19091935 VarType = read_vbr_uint();
    959959 }
    960960 }
    961961
    962 static unsigned getEncodedVisibility(const GlobalValue *GV) {
    963 switch (GV->getVisibility()) {
    964 default: assert(0 && "Invalid visibility!");
    965 case GlobalValue::DefaultVisibility: return 0;
    966 case GlobalValue::HiddenVisibility: return 1;
    967 }
    968 }
    969
    962970 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
    963971 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
    964972
    978986
    979987 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
    980988 // bit5+ = Slot # for type.
    981 bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection();
    989 bool HasExtensionWord = (I->getAlignment() != 0) ||
    990 I->hasSection() ||
    991 (I->getVisibility() != GlobalValue::DefaultVisibility);
    982992
    983993 // If we need to use the extension byte, set linkage=3(internal) and
    984994 // initializer = 0 (impossible!).
    9921002 output_vbr(oSlot);
    9931003
    9941004 // The extension word has this format: bit 0 = has initializer, bit 1-3 =
    995 // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID,
    996 // bits 10+ = future use.
    1005 // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID,
    1006 // bits 10-12 = visibility, bits 13+ = future use.
    9971007 unsigned ExtWord = (unsigned)I->hasInitializer() |
    9981008 (getEncodedLinkage(I) << 1) |
    9991009 ((Log2_32(I->getAlignment())+1) << 4) |
    1000 ((unsigned)I->hasSection() << 9);
    1010 ((unsigned)I->hasSection() << 9) |
    1011 (getEncodedVisibility(I) << 10);
    10011012 output_vbr(ExtWord);
    10021013 if (I->hasSection()) {
    10031014 // Give section names unique ID's.
    11011112 if (F->isExternal()) return;
    11021113
    11031114 BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this);
    1104 output_vbr(getEncodedLinkage(F));
    1115 unsigned rWord = (getEncodedVisibility(F) << 16) | getEncodedLinkage(F);
    1116 output_vbr(rWord);
    11051117
    11061118 // Get slot information about the function...
    11071119 Table.incorporateFunction(F);
    12891289 << "#define __ATTRIBUTE_WEAK__\n"
    12901290 << "#endif\n\n";
    12911291
    1292 // Add hidden visibility support. FIXME: APPLE_CC?
    1293 Out << "#if defined(__GNUC__)\n"
    1294 << "#define __HIDDEN__ __attribute__((visibility(\"hidden\")))\n"
    1295 << "#endif\n\n";
    1296
    12921297 // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise
    12931298 // From the GCC documentation:
    12941299 //
    14991504 Out << " __ATTRIBUTE_CTOR__";
    15001505 if (StaticDtors.count(I))
    15011506 Out << " __ATTRIBUTE_DTOR__";
    1507 if (I->hasHiddenVisibility())
    1508 Out << " __HIDDEN__";
    15021509
    15031510 if (I->hasName() && I->getName()[0] == 1)
    15041511 Out << " LLVM_ASM(\"" << I->getName().c_str()+1 << "\")";
    15301537 Out << " __ATTRIBUTE_WEAK__";
    15311538 else if (I->hasExternalWeakLinkage())
    15321539 Out << " __EXTERNAL_WEAK__";
    1540 if (I->hasHiddenVisibility())
    1541 Out << " __HIDDEN__";
    15331542 Out << ";\n";
    15341543 }
    15351544 }
    15581567 else if (I->hasWeakLinkage())
    15591568 Out << " __ATTRIBUTE_WEAK__";
    15601569
    1570 if (I->hasHiddenVisibility())
    1571 Out << " __HIDDEN__";
    1572
    15611573 // If the initializer is not null, emit the initializer. If it is null,
    15621574 // we try to avoid emitting large amounts of zeros. The problem with
    15631575 // this, however, occurs when the variable has weak linkage. In this
    533533
    534534 //===---------------------------------------------------------------------===//
    535535
    536 We should handle __attribute__ ((__visibility__ ("hidden"))).
    537
    538 //===---------------------------------------------------------------------===//
    539
    540536 int %foo(int* %a, int %t) {
    541537 entry:
    542538 br label %cond_true
    1818 #include "X86MachineFunctionInfo.h"
    1919 #include "X86TargetMachine.h"
    2020 #include "X86TargetAsmInfo.h"
    21 #include "llvm/ADT/StringExtras.h"
    2122 #include "llvm/CallingConv.h"
    2223 #include "llvm/Module.h"
    2324 #include "llvm/Support/Mangler.h"
    2728 using namespace llvm;
    2829
    2930 STATISTIC(EmittedInsts, "Number of machine instrs printed");
    31
    32 static std::string computePICLabel(unsigned fnNumber,
    33 const X86Subtarget* Subtarget)
    34 {
    35 std::string label;
    36
    37 if (Subtarget->isTargetDarwin()) {
    38 label = "\"L" + utostr_32(fnNumber) + "$pb\"";
    39 } else if (Subtarget->isTargetELF()) {
    40 label = ".Lllvm$" + utostr_32(fnNumber) + "$piclabel";
    41 } else
    42 assert(0 && "Don't know how to print PIC label!\n");
    43
    44 return label;
    45 }
    3046
    3147 /// getSectionForFunction - Return the section that we should emit the
    3248 /// specified function body into.
    108124 }
    109125 break;
    110126 }
    127 if (F->hasHiddenVisibility())
    128 O << "\t.hidden " << CurrentFnName << "\n";
    129
    111130 O << CurrentFnName << ":\n";
    112131 // Add some workaround for linkonce linkage on Cygwin\MinGW
    113132 if (Subtarget->isTargetCygMing() &&
    114133 (F->getLinkage() == Function::LinkOnceLinkage ||
    115134 F->getLinkage() == Function::WeakLinkage))
    116 O << "_llvm$workaround$fake$stub_" << CurrentFnName << ":\n";
    135 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
    117136
    118137 if (Subtarget->isTargetDarwin() ||
    119138 Subtarget->isTargetELF() ||
    192211 if (!isMemOp) O << '$';
    193212 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
    194213 << MO.getJumpTableIndex();
    195 if (X86PICStyle == PICStyle::Stub &&
    196 TM.getRelocationModel() == Reloc::PIC_)
    197 O << "-\"L" << getFunctionNumber() << "$pb\"";
    214
    215 if (TM.getRelocationModel() == Reloc::PIC_) {
    216 if (Subtarget->isPICStyleStub())
    217 O << "-\"L" << getFunctionNumber() << "$pb\"";
    218 else if (Subtarget->isPICStyleGOT())
    219 O << "@GOTOFF";
    220 }
    221
    198222 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
    199223 O << "(%rip)";
    200224 return;
    204228 if (!isMemOp) O << '$';
    205229 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
    206230 << MO.getConstantPoolIndex();
    207 if (X86PICStyle == PICStyle::Stub &&
    208 TM.getRelocationModel() == Reloc::PIC_)
    209 O << "-\"L" << getFunctionNumber() << "$pb\"";
    231
    232 if (TM.getRelocationModel() == Reloc::PIC_) {
    233 if (Subtarget->isPICStyleStub())
    234 O << "-\"L" << getFunctionNumber() << "$pb\"";
    235 if (Subtarget->isPICStyleGOT())
    236 O << "@GOTOFF";
    237 }
    238
    210239 int Offset = MO.getOffset();
    211240 if (Offset > 0)
    212241 O << "+" << Offset;
    227256
    228257 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
    229258 GV->hasLinkOnceLinkage());
    259 bool isHidden = GV->hasHiddenVisibility();
    230260
    231261 X86SharedAsmPrinter::decorateName(Name, GV);
    232262
    233 if (X86PICStyle == PICStyle::Stub &&
    234 TM.getRelocationModel() != Reloc::Static) {
    263 if (Subtarget->isPICStyleStub()) {
    235264 // Link-once, External, or Weakly-linked global variables need
    236265 // non-lazily-resolved stubs
    237266 if (isExt) {
    257286 O << "__imp_";
    258287 }
    259288 O << Name;
    289
    290 if (Subtarget->isPICStyleGOT() && isCallOp && isa(GV)) {
    291 // Assemble call via PLT for non-local symbols
    292 if (!isHidden || isExt)
    293 O << "@PLT";
    294 }
    260295 }
    261296
    262297 if (GV->hasExternalWeakLinkage())
    268303 else if (Offset < 0)
    269304 O << Offset;
    270305
    271 if (isMemOp && Subtarget->is64Bit()) {
    272 if (isExt && TM.getRelocationModel() != Reloc::Static)
    273 O << "@GOTPCREL(%rip)";
    274 else if (!NotRIPRel)
    275 // Use rip when possible to reduce code size, except when index or
    276 // base register are also part of the address. e.g.
    277 // foo(%rip)(%rcx,%rax,4) is not legal
    278 O << "(%rip)";
    306 if (isMemOp) {
    307 if (isExt) {
    308 if (Subtarget->isPICStyleGOT()) {
    309 O << "@GOT";
    310 } else if (Subtarget->isPICStyleRIPRel()) {
    311 O << "@GOTPCREL(%rip)";
    312 } if (Subtarget->is64Bit() && !NotRIPRel)
    313 // Use rip when possible to reduce code size, except when
    314 // index or base register are also part of the address. e.g.
    315 // foo(%rip)(%rcx,%rax,4) is not legal
    316 O << "(%rip)";
    317 } else {
    318 if (Subtarget->is64Bit() && !NotRIPRel)
    319 O << "(%rip)";
    320 else if (Subtarget->isPICStyleGOT())
    321 O << "@GOTOFF";
    322 }
    279323 }
    280324
    281325 return;
    282326 }
    283327 case MachineOperand::MO_ExternalSymbol: {
    284328 bool isCallOp = Modifier && !strcmp(Modifier, "call");
    285 if (isCallOp &&
    286 X86PICStyle == PICStyle::Stub &&
    287 TM.getRelocationModel() != Reloc::Static) {
    288 std::string Name(TAI->getGlobalPrefix());
    289 Name += MO.getSymbolName();
    329 std::string Name(TAI->getGlobalPrefix());
    330 Name += MO.getSymbolName();
    331 if (isCallOp && Subtarget->isPICStyleStub()) {
    290332 FnStubs.insert(Name);
    291333 O << "L" << Name << "$stub";
    292334 return;
    293335 }
    294336 if (!isCallOp) O << '$';
    295 O << TAI->getGlobalPrefix() << MO.getSymbolName();
    337 O << Name;
    338
    339 if (Subtarget->isPICStyleGOT()) {
    340 std::string GOTName(TAI->getGlobalPrefix());
    341 GOTName+="_GLOBAL_OFFSET_TABLE_";
    342 if (Name == GOTName)
    343 // Really hack! Emit extra offset to PC during printing GOT offset to
    344 // compensate size of popl instruction. The resulting code should look
    345 // like:
    346 // call .piclabel
    347 // piclabel:
    348 // popl %some_register
    349 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
    350 O << " + [.-" << computePICLabel(getFunctionNumber(), Subtarget) << "]";
    351 }
    352
    353 if (isCallOp && Subtarget->isPICStyleGOT())
    354 O << "@PLT";
    296355
    297356 if (!isCallOp && Subtarget->is64Bit())
    298357 O << "(%rip)";
    365424 }
    366425
    367426 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
    368 O << "\"L" << getFunctionNumber() << "$pb\"\n";
    369 O << "\"L" << getFunctionNumber() << "$pb\":";
    427 std::string label = computePICLabel(getFunctionNumber(), Subtarget);
    428 O << label << "\n" << label << ":";
    370429 }
    371430
    372431
    104104
    105105 /// doInitialization
    106106 bool X86SharedAsmPrinter::doInitialization(Module &M) {
    107 if (Subtarget->isTargetDarwin()) {
    108 if (!Subtarget->is64Bit())
    109 X86PICStyle = PICStyle::Stub;
    110
    111 // Emit initial debug information.
    112 DW.BeginModule(&M);
    113 } else if (Subtarget->isTargetELF() || Subtarget->isTargetCygMing()) {
    107 if (Subtarget->isTargetELF() ||
    108 Subtarget->isTargetCygMing() ||
    109 Subtarget->isTargetDarwin()) {
    114110 // Emit initial debug information.
    115111 DW.BeginModule(&M);
    116112 }
    240236 << "\n";
    241237 if (TAI->hasDotTypeDotSizeDirective())
    242238 O << "\t.size " << name << ", " << Size << "\n";
    243
    244239 // If the initializer is a extern weak symbol, remember to emit the weak
    245240 // reference!
    246241 if (const GlobalValue *GV = dyn_cast(C))
    250245 EmitGlobalConstant(C);
    251246 O << '\n';
    252247 }
    248 if (I->hasHiddenVisibility())
    249 O << "\t.hidden " << name << "\n";
    253250 }
    254251
    255252 // Output linker support code for dllexported globals
    2727
    2828 namespace llvm {
    2929
    30 // FIXME: Move this to CodeGen/AsmPrinter.h
    31 namespace PICStyle {
    32 enum X86AsmPICStyle {
    33 Stub, GOT
    34 };
    35 }
    36
    3730 struct VISIBILITY_HIDDEN X86SharedAsmPrinter : public AsmPrinter {
    3831 DwarfWriter DW;
    3932
    4033 X86SharedAsmPrinter(std::ostream &O, X86TargetMachine &TM,
    4134 const TargetAsmInfo *T)
    42 : AsmPrinter(O, TM, T), DW(O, this, T), X86PICStyle(PICStyle::GOT) {
    35 : AsmPrinter(O, TM, T), DW(O, this, T) {
    4336 Subtarget = &TM.getSubtarget();
    4437 }
    4538
    7265 MachineFunctionPass::getAnalysisUsage(AU);
    7366 }
    7467
    75 PICStyle::X86AsmPICStyle X86PICStyle;
    76
    7768 const X86Subtarget *Subtarget;
    7869
    7970 // Necessary for Darwin to print out the apprioriate types of linker stubs
    937937 MachineBasicBlock &FirstMBB = BB->getParent()->front();
    938938 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
    939939 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
    940 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
    940 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
    941
    941942 const TargetInstrInfo *TII = TM.getInstrInfo();
    942943 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
    943 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), GlobalBaseReg);
    944 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
    945
    946 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
    947 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
    948 if (Subtarget->isPICStyleGOT()) {
    949 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
    950 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
    951 addReg(PC).
    952 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
    953 } else {
    954 GlobalBaseReg = PC;
    955 }
    956
    944957 }
    945958 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
    946959 }
    663663 InFlag = Chain.getValue(1);
    664664 }
    665665
    666 if (Subtarget->isPICStyleGOT()) {
    667 Chain = DAG.getCopyToReg(Chain, X86::EBX,
    668 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    669 InFlag);
    670 InFlag = Chain.getValue(1);
    671 }
    672
    666673 // If the callee is a GlobalAddress node (quite common, every direct call is)
    667674 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
    668675 if (GlobalAddressSDNode *G = dyn_cast(Callee)) {
    686693 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
    687694 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
    688695 RegsToPass[i].second.getValueType()));
    689
    696
    690697 if (InFlag.Val)
    691698 Ops.push_back(InFlag);
    692699
    38553862 getPointerTy(),
    38563863 CP->getAlignment());
    38573864 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
    3858 if (Subtarget->isTargetDarwin()) {
    3859 // With PIC, the address is actually $g + Offset.
    3860 if (!Subtarget->is64Bit() &&
    3861 getTargetMachine().getRelocationModel() == Reloc::PIC_)
    3862 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3863 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
    3865 // With PIC, the address is actually $g + Offset.
    3866 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
    3867 !Subtarget->isPICStyleRIPRel()) {
    3868 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3869 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    3870 Result);
    38643871 }
    38653872
    38663873 return Result;
    38713878 GlobalValue *GV = cast(Op)->getGlobal();
    38723879 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
    38733880 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
    3874 if (Subtarget->isTargetDarwin()) {
    3875 // With PIC, the address is actually $g + Offset.
    3876 if (!Subtarget->is64Bit() &&
    3877 getTargetMachine().getRelocationModel() == Reloc::PIC_)
    3878 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3879 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    3880 Result);
    3881 // With PIC, the address is actually $g + Offset.
    3882 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
    3883 !Subtarget->isPICStyleRIPRel()) {
    3884 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3885 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    3886 Result);
    38813887 }
    38823888
    38833889 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
    38843890 // load the value at address GV, not the value of GV itself. This means that
    38853891 // the GlobalAddress must be in the base or index register of the address, not
    38863892 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
    3893 // The same applies for external symbols during PIC codegen
    38873894 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
    38883895 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
    38893896
    38953902 const char *Sym = cast(Op)->getSymbol();
    38963903 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
    38973904 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
    3898 if (Subtarget->isTargetDarwin()) {
    3899 // With PIC, the address is actually $g + Offset.
    3900 if (!Subtarget->is64Bit() &&
    3901 getTargetMachine().getRelocationModel() == Reloc::PIC_)
    3902 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3903 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    3904 Result);
    3905 // With PIC, the address is actually $g + Offset.
    3906 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
    3907 !Subtarget->isPICStyleRIPRel()) {
    3908 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3909 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    3910 Result);
    3911 }
    3912
    3913 return Result;
    3914 }
    3915
    3916 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
    3917 JumpTableSDNode *JT = cast(Op);
    3918 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
    3919 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
    3920 // With PIC, the address is actually $g + Offset.
    3921 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
    3922 !Subtarget->isPICStyleRIPRel()) {
    3923 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    3924 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    3925 Result);
    39053926 }
    39063927
    39073928 return Result;
    43314352 }
    43324353 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
    43334354 Cond, Op.getOperand(2), CC, Cond.getValue(1));
    4334 }
    4335
    4336 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
    4337 JumpTableSDNode *JT = cast(Op);
    4338 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
    4339 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
    4340 if (Subtarget->isTargetDarwin()) {
    4341 // With PIC, the address is actually $g + Offset.
    4342 if (!Subtarget->is64Bit() &&
    4343 getTargetMachine().getRelocationModel() == Reloc::PIC_)
    4344 Result = DAG.getNode(ISD::ADD, getPointerTy(),
    4345 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
    4346 Result);
    4347 }
    4348
    4349 return Result;
    43504355 }
    43514356
    43524357 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
    177177 MIB = MIB.addGlobalAddress(MO.getGlobal(), MO.getOffset());
    178178 else if (MO.isJumpTableIndex())
    179179 MIB = MIB.addJumpTableIndex(MO.getJumpTableIndex());
    180 else if (MO.isExternalSymbol())
    181 MIB = MIB.addExternalSymbol(MO.getSymbolName());
    180182 else
    181183 assert(0 && "Unknown operand type!");
    182184 }
    201203 MIB = MIB.addGlobalAddress(MO.getGlobal(), MO.getOffset());
    202204 else if (MO.isJumpTableIndex())
    203205 MIB = MIB.addJumpTableIndex(MO.getJumpTableIndex());
    206 else if (MO.isExternalSymbol())
    207 MIB = MIB.addExternalSymbol(MO.getSymbolName());
    204208 else
    205209 assert(0 && "Unknown operand for FuseInst!");
    206210 }
    1818 using namespace llvm;
    1919
    2020 cl::opt
    21 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::unset),
    21 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
    2222 cl::desc("Choose style of code to emit from X86 backend:"),
    2323 cl::values(
    24 clEnumValN(X86Subtarget::att, "att", " Emit AT&T-style assembly"),
    25 clEnumValN(X86Subtarget::intel, "intel", " Emit Intel-style assembly"),
    24 clEnumValN(X86Subtarget::ATT, "att", " Emit AT&T-style assembly"),
    25 clEnumValN(X86Subtarget::Intel, "intel", " Emit Intel-style assembly"),
    2626 clEnumValEnd));
    2727
    2828
    3535 bool isDirectCall) const
    3636 {
    3737 if (TM.getRelocationModel() != Reloc::Static)
    38 if (isTargetDarwin()) {
    38 if (isTargetDarwin() || isPICStyleGOT()) {
    3939 return (!isDirectCall &&
    4040 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
    4141 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())));
    211211
    212212 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
    213213 : AsmFlavor(AsmWriterFlavor)
    214 , PICStyle(PICStyle::None)
    214215 , X86SSELevel(NoMMXSSE)
    215216 , HasX86_64(false)
    216217 , stackAlignment(8)
    269270
    270271 // If the asm syntax hasn't been overridden on the command line, use whatever
    271272 // the target wants.
    272 if (AsmFlavor == X86Subtarget::unset) {
    273 if (AsmFlavor == X86Subtarget::Unset) {
    273274 if (TargetType == isWindows) {
    274 AsmFlavor = X86Subtarget::intel;
    275 AsmFlavor = X86Subtarget::Intel;
    275276 } else {
    276 AsmFlavor = X86Subtarget::att;
    277 AsmFlavor = X86Subtarget::ATT;
    277278 }
    278279 }
    279280
    2121 class Module;
    2222 class GlobalValue;
    2323 class TargetMachine;
    24
    25 namespace PICStyle {
    26 enum Style {
    27 Stub, GOT, RIPRel, WinPIC, None
    28 };
    29 }
    2430
    2531 class X86Subtarget : public TargetSubtarget {
    2632 public:
    2733 enum AsmWriterFlavorTy {
    28 att, intel, unset
    34 ATT, Intel, Unset
    2935 };
    30
    3136 protected:
    3237 enum X86SSEEnum {
    3338 NoMMXSSE, MMX, SSE1, SSE2, SSE3
    4045 /// AsmFlavor - Which x86 asm dialect to use.
    4146 AsmWriterFlavorTy AsmFlavor;
    4247
    48 /// PICStyle - Which PIC style to use
    49 PICStyle::Style PICStyle;
    50
    4351 /// X86SSELevel - MMX, SSE1, SSE2, SSE3, or none supported.
    4452 X86SSEEnum X86SSELevel;
    4553
    92100
    93101 bool is64Bit() const { return Is64Bit; }
    94102
    103 PICStyle::Style getPICStyle() const { return PICStyle; }
    104 void setPICStyle(PICStyle::Style Style) { PICStyle = Style; }
    105
    95106 bool hasMMX() const { return X86SSELevel >= MMX; }
    96107 bool hasSSE1() const { return X86SSELevel >= SSE1; }
    97108 bool hasSSE2() const { return X86SSELevel >= SSE2; }
    99110 bool has3DNow() const { return X863DNowLevel >= ThreeDNow; }
    100111 bool has3DNowA() const { return X863DNowLevel >= ThreeDNowA; }
    101112
    102 bool isFlavorAtt() const { return AsmFlavor == att; }
    103 bool isFlavorIntel() const { return AsmFlavor == intel; }
    113 bool isFlavorAtt() const { return AsmFlavor == ATT; }
    114 bool isFlavorIntel() const { return AsmFlavor == Intel; }
    104115
    105116 bool isTargetDarwin() const { return TargetType == isDarwin; }
    106117 bool isTargetELF() const { return TargetType == isELF; }
    110121 TargetType == isCygwin); }
    111122 bool isTargetCygwin() const { return TargetType == isCygwin; }
    112123
    124 bool isPICStyleSet() const { return PICStyle != PICStyle::None; }
    125 bool isPICStyleGOT() const { return PICStyle == PICStyle::GOT; }
    126 bool isPICStyleStub() const { return PICStyle == PICStyle::Stub; }
    127 bool isPICStyleRIPRel() const { return PICStyle == PICStyle::RIPRel; }
    128 bool isPICStyleWinPIC() const { return PICStyle == PICStyle:: WinPIC; }
    129
    113130 /// True if accessing the GV requires an extra load. For Windows, dllimported
    114131 /// symbols are indirect, loading the value at address GV rather then the
    115132 /// value of GV itself. This means that the GlobalAddress must be in the base
    126126 if (getCodeModel() == CodeModel::Default)
    127127 setCodeModel(CodeModel::Small);
    128128 }
    129
    130 if (getRelocationModel() == Reloc::PIC_) {
    131 if (Subtarget.isTargetDarwin()) {
    132 if (Subtarget.is64Bit())
    133 Subtarget.setPICStyle(PICStyle::RIPRel);
    134 else
    135 Subtarget.setPICStyle(PICStyle::Stub);
    136 } else if (Subtarget.isTargetELF())
    137 Subtarget.setPICStyle(PICStyle::GOT);
    138 else
    139 assert(0 && "Don't know how to generate PIC code for this target!");
    140 } else if (getRelocationModel() == Reloc::DynamicNoPIC) {
    141 if (Subtarget.isTargetDarwin())
    142 Subtarget.setPICStyle(PICStyle::Stub);
    143 else if (Subtarget.isTargetCygMing())
    144 Subtarget.setPICStyle(PICStyle::WinPIC);
    145 else
    146 assert(0 && "Don't know how to generate PIC code for this target!");
    147 }
    129148 }
    130149
    131150 //===----------------------------------------------------------------------===//
    162181 MachineCodeEmitter &MCE) {
    163182 // FIXME: Move this to TargetJITInfo!
    164183 setRelocationModel(Reloc::Static);
    184 Subtarget.setPICStyle(PICStyle::None);
    185
    165186 // JIT cannot ensure globals are placed in the lower 4G of address.
    166187 if (Subtarget.is64Bit())
    167188 setCodeModel(CodeModel::Large);
    857857 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
    858858 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
    859859 default: Out << "external "; break;
    860 }
    861 else
    860 } else {
    862861 switch (GV->getLinkage()) {
    863862 case GlobalValue::InternalLinkage: Out << "internal "; break;
    864863 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
    872871 cerr << "GhostLinkage not allowed in AsmWriter!\n";
    873872 abort();
    874873 }
    875
    874 switch (GV->getVisibility()) {
    875 case GlobalValue::DefaultVisibility: break;
    876 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
    877 default:
    878 cerr << "Invalid visibility style!\n";
    879 abort();
    880 }
    881 }
    882
    876883 Out << (GV->isConstant() ? "constant " : "global ");
    877884 printType(GV->getType()->getElementType());
    878885
    972979 case GlobalValue::GhostLinkage:
    973980 cerr << "GhostLinkage not allowed in AsmWriter!\n";
    974981 abort();
    982 }
    983 switch (F->getVisibility()) {
    984 case GlobalValue::DefaultVisibility: break;
    985 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
    986 default:
    987 cerr << "Invalid visibility style!\n";
    988 abort();
    975989 }
    976990 }
    977991
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu | grep ".hidden" | wc -l | grep 2
    2
    3 %struct.Person = type { i32 }
    4 %a = hidden global i32 0
    5 %b = external global i32
    6
    7 implementation ; Functions:
    8
    9 define weak hidden void %_ZN6Person13privateMethodEv(%struct.Person* %this) {
    10 ret void
    11 }
    12
    13 declare void %function(i32)
    14
    15 define weak void %_ZN6PersonC1Ei(%struct.Person* %this, i32 %_c) {
    16 ret void
    17 }
    18
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep GOT | wc -l | grep 3 &&
    4 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep GOTOFF | wc -l | grep 0
    5
    6 %ptr = external global i32*
    7 %dst = external global i32
    8 %src = external global i32
    9
    10 define void %foo() {
    11 entry:
    12 store i32* %dst, i32** %ptr
    13 %tmp.s = load i32* %src
    14 store i32 %tmp.s, i32* %dst
    15 ret void
    16 }
    17
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep GOTOFF | wc -l | grep 4
    4
    5 %ptr = internal global i32* null
    6 %dst = internal global i32 0
    7 %src = internal global i32 0
    8
    9 define void %foo() {
    10 entry:
    11 store i32* %dst, i32** %ptr
    12 %tmp.s = load i32* %src
    13 store i32 %tmp.s, i32* %dst
    14 ret void
    15 }
    16
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep PLT | wc -l | grep 1
    4
    5 define void %bar() {
    6 entry:
    7 call void(...)* %foo()
    8 br label %return
    9 return:
    10 ret void
    11 }
    12
    13 declare void %foo(...)
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep PLT | wc -l | grep 1 &&
    4 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep "GOT" | wc -l | grep 1 &&
    5 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep "GOTOFF" | wc -l | grep 0
    6
    7 %pfoo = external global void(...)*
    8
    9 define void %bar() {
    10 entry:
    11 %tmp = call void(...)*(...)* %afoo()
    12 store void(...)* %tmp, void(...)** %pfoo
    13 %tmp1 = load void(...)** %pfoo
    14 call void(...)* %tmp1()
    15 br label %return
    16 return:
    17 ret void
    18 }
    19
    20 declare void(...)* %afoo(...)
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep PLT | wc -l | grep 1
    4
    5 %ptr = external global i32*
    6
    7 define void %foo() {
    8 entry:
    9 %ptr = malloc i32, i32 10
    10 ret void
    11 }
    12
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep GOTOFF | wc -l | grep 2 &&
    4 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep CPI | wc -l | grep 4
    5
    6 define double %foo(i32 %a.u) {
    7 entry:
    8 %tmp = icmp eq i32 %a.u,0
    9 %retval = select bool %tmp, double 4.561230e+02, double 1.234560e+02
    10 ret double %retval
    11 }
    12
    0 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic &&
    1 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep _GLOBAL_OFFSET_TABLE_ &&
    2 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep piclabel | wc -l | grep 3 &&
    3 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep PLT | wc -l | grep 13 &&
    4 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep GOTOFF | wc -l | grep 1 &&
    5 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic | grep JTI | wc -l | grep 15
    6
    7 define void %bar(i32 %n.u) {
    8 entry:
    9 switch i32 %n.u, label %bb12 [i32 1, label %bb i32 2, label %bb6 i32 4, label %bb7 i32 5, label %bb8 i32 6, label %bb10 i32 7, label %bb1 i32 8, label %bb3 i32 9, label %bb4 i32 10, label %bb9 i32 11, label %bb2 i32 12, label %bb5 i32 13, label %bb11 ]
    10 bb:
    11 tail call void(...)* %foo1()
    12 ret void
    13 bb1:
    14 tail call void(...)* %foo2()
    15 ret void
    16 bb2:
    17 tail call void(...)* %foo6()
    18 ret void
    19 bb3:
    20 tail call void(...)* %foo3()
    21 ret void
    22 bb4:
    23 tail call void(...)* %foo4()
    24 ret void
    25 bb5:
    26 tail call void(...)* %foo5()
    27 ret void
    28 bb6:
    29 tail call void(...)* %foo1()
    30 ret void
    31 bb7:
    32 tail call void(...)* %foo2()
    33 ret void
    34 bb8:
    35 tail call void(...)* %foo6()
    36 ret void
    37 bb9:
    38 tail call void(...)* %foo3()
    39 ret void
    40 bb10:
    41 tail call void(...)* %foo4()
    42 ret void
    43 bb11:
    44 tail call void(...)* %foo5()
    45 ret void
    46 bb12:
    47 tail call void(...)* %foo6()
    48 ret void
    49 }
    50
    51 declare void %foo1(...)
    52 declare void %foo2(...)
    53 declare void %foo6(...)
    54 declare void %foo3(...)
    55 declare void %foo4(...)
    56 declare void %foo5(...)