llvm.org GIT mirror llvm / 75b6882
Remove X86SharedAsmPrinter git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52860 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 12 years ago
4 changed file(s) with 487 addition(s) and 529 deletion(s). Raw diff Collapse all Expand all
1919 #include "X86MachineFunctionInfo.h"
2020 #include "X86TargetMachine.h"
2121 #include "X86TargetAsmInfo.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Type.h"
26 #include "llvm/ADT/Statistic.h"
2227 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/CallingConv.h"
2428 #include "llvm/CodeGen/MachineJumpTableInfo.h"
25 #include "llvm/Module.h"
2629 #include "llvm/Support/Mangler.h"
2730 #include "llvm/Target/TargetAsmInfo.h"
2831 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/ADT/Statistic.h"
3032 using namespace llvm;
3133
3234 STATISTIC(EmittedInsts, "Number of machine instrs printed");
4345 assert(0 && "Don't know how to print PIC label!\n");
4446
4547 return label;
48 }
49
50 static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
51 const TargetData *TD) {
52 X86MachineFunctionInfo Info;
53 uint64_t Size = 0;
54
55 switch (F->getCallingConv()) {
56 case CallingConv::X86_StdCall:
57 Info.setDecorationStyle(StdCall);
58 break;
59 case CallingConv::X86_FastCall:
60 Info.setDecorationStyle(FastCall);
61 break;
62 default:
63 return Info;
64 }
65
66 unsigned argNum = 1;
67 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
68 AI != AE; ++AI, ++argNum) {
69 const Type* Ty = AI->getType();
70
71 // 'Dereference' type in case of byval parameter attribute
72 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
73 Ty = cast(Ty)->getElementType();
74
75 // Size should be aligned to DWORD boundary
76 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
77 }
78
79 // We're not supporting tooooo huge arguments :)
80 Info.setBytesToPopOnReturn((unsigned int)Size);
81 return Info;
82 }
83
84 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
85 /// Don't print things like \n or \0.
86 static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
87 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
88 Name != E; ++Name)
89 if (isprint(*Name))
90 OS << *Name;
91 }
92
93 /// decorateName - Query FunctionInfoMap and use this information for various
94 /// name decoration.
95 void X86ATTAsmPrinter::decorateName(std::string &Name,
96 const GlobalValue *GV) {
97 const Function *F = dyn_cast(GV);
98 if (!F) return;
99
100 // We don't want to decorate non-stdcall or non-fastcall functions right now
101 unsigned CC = F->getCallingConv();
102 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
103 return;
104
105 // Decorate names only when we're targeting Cygwin/Mingw32 targets
106 if (!Subtarget->isTargetCygMing())
107 return;
108
109 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
110
111 const X86MachineFunctionInfo *Info;
112 if (info_item == FunctionInfoMap.end()) {
113 // Calculate apropriate function info and populate map
114 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
115 Info = &FunctionInfoMap[F];
116 } else {
117 Info = &info_item->second;
118 }
119
120 const FunctionType *FT = F->getFunctionType();
121 switch (Info->getDecorationStyle()) {
122 case None:
123 break;
124 case StdCall:
125 // "Pure" variadic functions do not receive @0 suffix.
126 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
127 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
128 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
129 break;
130 case FastCall:
131 // "Pure" variadic functions do not receive @0 suffix.
132 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
133 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
134 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
135
136 if (Name[0] == '_') {
137 Name[0] = '@';
138 } else {
139 Name = '@' + Name;
140 }
141 break;
142 default:
143 assert(0 && "Unsupported DecorationStyle");
144 }
46145 }
47146
48147 /// getSectionForFunction - Return the section that we should emit the
93192 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
94193 FunctionInfoMap[F] = *MF.getInfo();
95194
96 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
195 decorateName(CurrentFnName, F);
97196
98197 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
99198
297396 bool isThreadLocal = GVar && GVar->isThreadLocal();
298397
299398 std::string Name = Mang->getValueName(GV);
300 X86SharedAsmPrinter::decorateName(Name, GV);
399 decorateName(Name, GV);
301400
302401 if (!isMemOp && !isCallOp)
303402 O << '$';
648747 printInstruction(MI);
649748 }
650749
750 /// doInitialization
751 bool X86ATTAsmPrinter::doInitialization(Module &M) {
752 if (TAI->doesSupportDebugInformation()) {
753 // Emit initial debug information.
754 DW.BeginModule(&M);
755 }
756
757 bool Result = AsmPrinter::doInitialization(M);
758
759 // Darwin wants symbols to be quoted if they have complex names.
760 if (Subtarget->isTargetDarwin())
761 Mang->setUseQuotes(true);
762
763 return Result;
764 }
765
766
767 bool X86ATTAsmPrinter::doFinalization(Module &M) {
768 // Note: this code is not shared by the Intel printer as it is too different
769 // from how MASM does things. When making changes here don't forget to look
770 // at X86IntelAsmPrinter::doFinalization().
771 const TargetData *TD = TM.getTargetData();
772
773 // Print out module-level global variables here.
774 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
775 I != E; ++I) {
776 if (!I->hasInitializer())
777 continue; // External global require no code
778
779 // Check to see if this is a special global used by LLVM, if so, emit it.
780 if (EmitSpecialLLVMGlobal(I)) {
781 if (Subtarget->isTargetDarwin() &&
782 TM.getRelocationModel() == Reloc::Static) {
783 if (I->getName() == "llvm.global_ctors")
784 O << ".reference .constructors_used\n";
785 else if (I->getName() == "llvm.global_dtors")
786 O << ".reference .destructors_used\n";
787 }
788 continue;
789 }
790
791 std::string name = Mang->getValueName(I);
792 Constant *C = I->getInitializer();
793 const Type *Type = C->getType();
794 unsigned Size = TD->getABITypeSize(Type);
795 unsigned Align = TD->getPreferredAlignmentLog(I);
796
797 if (I->hasHiddenVisibility()) {
798 if (const char *Directive = TAI->getHiddenDirective())
799 O << Directive << name << "\n";
800 } else if (I->hasProtectedVisibility()) {
801 if (const char *Directive = TAI->getProtectedDirective())
802 O << Directive << name << "\n";
803 }
804
805 if (Subtarget->isTargetELF())
806 O << "\t.type\t" << name << ",@object\n";
807
808 if (C->isNullValue() && !I->hasSection()) {
809 if (I->hasExternalLinkage()) {
810 if (const char *Directive = TAI->getZeroFillDirective()) {
811 O << "\t.globl " << name << "\n";
812 O << Directive << "__DATA, __common, " << name << ", "
813 << Size << ", " << Align << "\n";
814 continue;
815 }
816 }
817
818 if (!I->isThreadLocal() &&
819 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
820 I->hasLinkOnceLinkage() || I->hasCommonLinkage())) {
821 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
822 if (!NoZerosInBSS && TAI->getBSSSection())
823 SwitchToDataSection(TAI->getBSSSection(), I);
824 else
825 SwitchToDataSection(TAI->getDataSection(), I);
826 if (TAI->getLCOMMDirective() != NULL) {
827 if (I->hasInternalLinkage()) {
828 O << TAI->getLCOMMDirective() << name << "," << Size;
829 if (Subtarget->isTargetDarwin())
830 O << "," << Align;
831 } else if (Subtarget->isTargetDarwin() && !I->hasCommonLinkage()) {
832 O << "\t.globl " << name << "\n"
833 << TAI->getWeakDefDirective() << name << "\n";
834 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
835 EmitAlignment(Align, I);
836 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
837 PrintUnmangledNameSafely(I, O);
838 O << "\n";
839 EmitGlobalConstant(C);
840 continue;
841 } else {
842 O << TAI->getCOMMDirective() << name << "," << Size;
843
844 // Leopard and above support aligned common symbols.
845 if (Subtarget->getDarwinVers() >= 9)
846 O << "," << Align;
847 }
848 } else {
849 if (!Subtarget->isTargetCygMing()) {
850 if (I->hasInternalLinkage())
851 O << "\t.local\t" << name << "\n";
852 }
853 O << TAI->getCOMMDirective() << name << "," << Size;
854 if (TAI->getCOMMDirectiveTakesAlignment())
855 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
856 }
857 O << "\t\t" << TAI->getCommentString() << " ";
858 PrintUnmangledNameSafely(I, O);
859 O << "\n";
860 continue;
861 }
862 }
863
864 switch (I->getLinkage()) {
865 case GlobalValue::CommonLinkage:
866 case GlobalValue::LinkOnceLinkage:
867 case GlobalValue::WeakLinkage:
868 if (Subtarget->isTargetDarwin()) {
869 O << "\t.globl " << name << "\n"
870 << TAI->getWeakDefDirective() << name << "\n";
871 if (!I->isConstant())
872 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
873 else {
874 const ArrayType *AT = dyn_cast(Type);
875 if (AT && AT->getElementType()==Type::Int8Ty)
876 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", I);
877 else
878 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
879 }
880 } else if (Subtarget->isTargetCygMing()) {
881 std::string SectionName(".section\t.data$linkonce." +
882 name +
883 ",\"aw\"");
884 SwitchToDataSection(SectionName.c_str(), I);
885 O << "\t.globl\t" << name << "\n"
886 << "\t.linkonce same_size\n";
887 } else {
888 std::string SectionName("\t.section\t.llvm.linkonce.d." +
889 name +
890 ",\"aw\",@progbits");
891 SwitchToDataSection(SectionName.c_str(), I);
892 O << "\t.weak\t" << name << "\n";
893 }
894 break;
895 case GlobalValue::DLLExportLinkage:
896 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
897 // FALL THROUGH
898 case GlobalValue::AppendingLinkage:
899 // FIXME: appending linkage variables should go into a section of
900 // their name or something. For now, just emit them as external.
901 case GlobalValue::ExternalLinkage:
902 // If external or appending, declare as a global symbol
903 O << "\t.globl " << name << "\n";
904 // FALL THROUGH
905 case GlobalValue::InternalLinkage: {
906 if (I->isConstant()) {
907 const ConstantArray *CVA = dyn_cast(C);
908 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
909 SwitchToDataSection(TAI->getCStringSection(), I);
910 break;
911 }
912 }
913 // FIXME: special handling for ".ctors" & ".dtors" sections
914 if (I->hasSection() &&
915 (I->getSection() == ".ctors" ||
916 I->getSection() == ".dtors")) {
917 std::string SectionName = ".section " + I->getSection();
918
919 if (Subtarget->isTargetCygMing()) {
920 SectionName += ",\"aw\"";
921 } else {
922 assert(!Subtarget->isTargetDarwin());
923 SectionName += ",\"aw\",@progbits";
924 }
925 SwitchToDataSection(SectionName.c_str());
926 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
927 // Honor all section names on Darwin; ObjC uses this
928 std::string SectionName = ".section " + I->getSection();
929 SwitchToDataSection(SectionName.c_str());
930 } else {
931 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
932 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
933 TAI->getBSSSection(), I);
934 else if (!I->isConstant())
935 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
936 TAI->getDataSection(), I);
937 else if (I->isThreadLocal())
938 SwitchToDataSection(TAI->getTLSDataSection());
939 else {
940 // Read-only data.
941 bool HasReloc = C->ContainsRelocations();
942 if (HasReloc &&
943 Subtarget->isTargetDarwin() &&
944 TM.getRelocationModel() != Reloc::Static)
945 SwitchToDataSection("\t.const_data\n");
946 else if (!HasReloc && Size == 4 &&
947 TAI->getFourByteConstantSection())
948 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
949 else if (!HasReloc && Size == 8 &&
950 TAI->getEightByteConstantSection())
951 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
952 else if (!HasReloc && Size == 16 &&
953 TAI->getSixteenByteConstantSection())
954 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
955 else if (TAI->getReadOnlySection())
956 SwitchToDataSection(TAI->getReadOnlySection(), I);
957 else
958 SwitchToDataSection(TAI->getDataSection(), I);
959 }
960 }
961
962 break;
963 }
964 default:
965 assert(0 && "Unknown linkage type!");
966 }
967
968 EmitAlignment(Align, I);
969 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
970 PrintUnmangledNameSafely(I, O);
971 O << "\n";
972 if (TAI->hasDotTypeDotSizeDirective())
973 O << "\t.size\t" << name << ", " << Size << "\n";
974 // If the initializer is a extern weak symbol, remember to emit the weak
975 // reference!
976 if (const GlobalValue *GV = dyn_cast(C))
977 if (GV->hasExternalWeakLinkage())
978 ExtWeakSymbols.insert(GV);
979
980 EmitGlobalConstant(C);
981 }
982
983 // Output linker support code for dllexported globals
984 if (!DLLExportedGVs.empty()) {
985 SwitchToDataSection(".section .drectve");
986 }
987
988 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
989 e = DLLExportedGVs.end();
990 i != e; ++i) {
991 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
992 }
993
994 if (!DLLExportedFns.empty()) {
995 SwitchToDataSection(".section .drectve");
996 }
997
998 for (StringSet<>::iterator i = DLLExportedFns.begin(),
999 e = DLLExportedFns.end();
1000 i != e; ++i) {
1001 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1002 }
1003
1004 if (Subtarget->isTargetDarwin()) {
1005 SwitchToDataSection("");
1006
1007 // Output stubs for dynamically-linked functions
1008 unsigned j = 1;
1009 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1010 i != e; ++i, ++j) {
1011 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1012 "self_modifying_code+pure_instructions,5", 0);
1013 std::string p = i->getKeyData();
1014 printSuffixedName(p, "$stub");
1015 O << ":\n";
1016 O << "\t.indirect_symbol " << p << "\n";
1017 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1018 }
1019
1020 O << "\n";
1021
1022 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1023 // Add the (possibly multiple) personalities to the set of global values.
1024 // Only referenced functions get into the Personalities list.
1025 const std::vector& Personalities = MMI->getPersonalities();
1026
1027 for (std::vector::const_iterator I = Personalities.begin(),
1028 E = Personalities.end(); I != E; ++I)
1029 if (*I) GVStubs.insert("_" + (*I)->getName());
1030 }
1031
1032 // Output stubs for external and common global variables.
1033 if (!GVStubs.empty())
1034 SwitchToDataSection(
1035 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1036 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1037 i != e; ++i) {
1038 std::string p = i->getKeyData();
1039 printSuffixedName(p, "$non_lazy_ptr");
1040 O << ":\n";
1041 O << "\t.indirect_symbol " << p << "\n";
1042 O << "\t.long\t0\n";
1043 }
1044
1045 // Emit final debug information.
1046 DW.EndModule();
1047
1048 // Funny Darwin hack: This flag tells the linker that no global symbols
1049 // contain code that falls through to other global symbols (e.g. the obvious
1050 // implementation of multiple entry points). If this doesn't occur, the
1051 // linker can safely perform dead code stripping. Since LLVM never
1052 // generates code that does this, it is always safe to set.
1053 O << "\t.subsections_via_symbols\n";
1054 } else if (Subtarget->isTargetCygMing()) {
1055 // Emit type information for external functions
1056 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1057 i != e; ++i) {
1058 O << "\t.def\t " << i->getKeyData()
1059 << ";\t.scl\t" << COFF::C_EXT
1060 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1061 << ";\t.endef\n";
1062 }
1063
1064 // Emit final debug information.
1065 DW.EndModule();
1066 } else if (Subtarget->isTargetELF()) {
1067 // Emit final debug information.
1068 DW.EndModule();
1069 }
1070
1071 return AsmPrinter::doFinalization(M);
1072 }
1073
6511074 // Include the auto-generated portion of the assembly writer.
6521075 #include "X86GenAsmWriter.inc"
1313 #ifndef X86ATTASMPRINTER_H
1414 #define X86ATTASMPRINTER_H
1515
16 #include "X86AsmPrinter.h"
16 #include "X86.h"
17 #include "X86MachineFunctionInfo.h"
18 #include "X86TargetMachine.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/DwarfWriter.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
1723 #include "llvm/CodeGen/ValueTypes.h"
24 #include "llvm/Support/Compiler.h"
1825
1926 namespace llvm {
2027
2128 struct MachineJumpTableInfo;
22
23 struct VISIBILITY_HIDDEN X86ATTAsmPrinter : public X86SharedAsmPrinter {
24 X86ATTAsmPrinter(std::ostream &O, X86TargetMachine &TM, const TargetAsmInfo *T)
25 : X86SharedAsmPrinter(O, TM, T) { }
29
30 struct VISIBILITY_HIDDEN X86ATTAsmPrinter : public AsmPrinter {
31 DwarfWriter DW;
32 MachineModuleInfo *MMI;
33
34 const X86Subtarget *Subtarget;
35
36 X86ATTAsmPrinter(std::ostream &O, X86TargetMachine &TM,
37 const TargetAsmInfo *T)
38 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(0) {
39 Subtarget = &TM.getSubtarget();
40 }
2641
2742 virtual const char *getPassName() const {
2843 return "X86 AT&T-Style Assembly Printer";
2944 }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 if (Subtarget->isTargetDarwin() ||
49 Subtarget->isTargetELF() ||
50 Subtarget->isTargetCygMing()) {
51 AU.addRequired();
52 }
53 AsmPrinter::getAnalysisUsage(AU);
54 }
55
56 bool doInitialization(Module &M);
57 bool doFinalization(Module &M);
3058
3159 /// printInstruction - This method is automatically generated by tablegen
3260 /// from the instruction set description. This method returns true if the
6795 void printlea64_32mem(const MachineInstr *MI, unsigned OpNo) {
6896 printMemReference(MI, OpNo, "subreg64");
6997 }
70
98
7199 bool printAsmMRegister(const MachineOperand &MO, const char Mode);
72100 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73101 unsigned AsmVariant, const char *ExtraCode);
74102 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
75103 unsigned AsmVariant, const char *ExtraCode);
76
104
77105 void printMachineInstruction(const MachineInstr *MI);
78106 void printSSECC(const MachineInstr *MI, unsigned Op);
79107 void printMemReference(const MachineInstr *MI, unsigned Op,
87115 void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
88116 const MachineBasicBlock *MBB,
89117 unsigned uid) const;
90
118
91119 void printPICLabel(const MachineInstr *MI, unsigned Op);
92120 bool runOnMachineFunction(MachineFunction &F);
93
121
94122 /// getSectionForFunction - Return the section that we should emit the
95123 /// specified function body into.
96124 virtual std::string getSectionForFunction(const Function &F) const;
125
126 // Necessary for Darwin to print out the apprioriate types of linker stubs
127 StringSet<> FnStubs, GVStubs, LinkOnceStubs;
128
129 // Necessary for dllexport support
130 StringSet<> DLLExportedFns, DLLExportedGVs;
131
132 // We have to propagate some information about MachineFunction to
133 // AsmPrinter. It's ok, when we're printing the function, since we have
134 // access to MachineFunction and can get the appropriate MachineFunctionInfo.
135 // Unfortunately, this is not possible when we're printing reference to
136 // Function (e.g. calling it and so on). Even more, there is no way to get the
137 // corresponding MachineFunctions: it can even be not created at all. That's
138 // why we should use additional structure, when we're collecting all necessary
139 // information.
140 //
141 // This structure is using e.g. for name decoration for stdcall & fastcall'ed
142 // function, since we have to use arguments' size for decoration.
143 typedef std::map FMFInfoMap;
144 FMFInfoMap FunctionInfoMap;
145
146 void decorateName(std::string& Name, const GlobalValue* GV);
97147 };
98148
99149 } // end namespace llvm
1313 //
1414 //===----------------------------------------------------------------------===//
1515
16 #include "X86AsmPrinter.h"
1716 #include "X86ATTAsmPrinter.h"
18 #include "X86COFF.h"
1917 #include "X86IntelAsmPrinter.h"
20 #include "X86MachineFunctionInfo.h"
2118 #include "X86Subtarget.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Module.h"
26 #include "llvm/DerivedTypes.h"
27 #include "llvm/ParameterAttributes.h"
28 #include "llvm/Type.h"
29 #include "llvm/Assembly/Writer.h"
30 #include "llvm/Support/Mangler.h"
31 #include "llvm/Target/TargetAsmInfo.h"
32 #include "llvm/Target/TargetOptions.h"
3319 using namespace llvm;
34
35 static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
36 const TargetData *TD) {
37 X86MachineFunctionInfo Info;
38 uint64_t Size = 0;
39
40 switch (F->getCallingConv()) {
41 case CallingConv::X86_StdCall:
42 Info.setDecorationStyle(StdCall);
43 break;
44 case CallingConv::X86_FastCall:
45 Info.setDecorationStyle(FastCall);
46 break;
47 default:
48 return Info;
49 }
50
51 unsigned argNum = 1;
52 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
53 AI != AE; ++AI, ++argNum) {
54 const Type* Ty = AI->getType();
55
56 // 'Dereference' type in case of byval parameter attribute
57 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
58 Ty = cast(Ty)->getElementType();
59
60 // Size should be aligned to DWORD boundary
61 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
62 }
63
64 // We're not supporting tooooo huge arguments :)
65 Info.setBytesToPopOnReturn((unsigned int)Size);
66 return Info;
67 }
68
69
70 /// decorateName - Query FunctionInfoMap and use this information for various
71 /// name decoration.
72 void X86SharedAsmPrinter::decorateName(std::string &Name,
73 const GlobalValue *GV) {
74 const Function *F = dyn_cast(GV);
75 if (!F) return;
76
77 // We don't want to decorate non-stdcall or non-fastcall functions right now
78 unsigned CC = F->getCallingConv();
79 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
80 return;
81
82 // Decorate names only when we're targeting Cygwin/Mingw32 targets
83 if (!Subtarget->isTargetCygMing())
84 return;
85
86 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
87
88 const X86MachineFunctionInfo *Info;
89 if (info_item == FunctionInfoMap.end()) {
90 // Calculate apropriate function info and populate map
91 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
92 Info = &FunctionInfoMap[F];
93 } else {
94 Info = &info_item->second;
95 }
96
97 const FunctionType *FT = F->getFunctionType();
98 switch (Info->getDecorationStyle()) {
99 case None:
100 break;
101 case StdCall:
102 // "Pure" variadic functions do not receive @0 suffix.
103 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
104 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
105 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
106 break;
107 case FastCall:
108 // "Pure" variadic functions do not receive @0 suffix.
109 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
110 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
111 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
112
113 if (Name[0] == '_') {
114 Name[0] = '@';
115 } else {
116 Name = '@' + Name;
117 }
118 break;
119 default:
120 assert(0 && "Unsupported DecorationStyle");
121 }
122 }
123
124 /// doInitialization
125 bool X86SharedAsmPrinter::doInitialization(Module &M) {
126 if (TAI->doesSupportDebugInformation()) {
127 // Emit initial debug information.
128 DW.BeginModule(&M);
129 }
130
131 bool Result = AsmPrinter::doInitialization(M);
132
133 // Darwin wants symbols to be quoted if they have complex names.
134 if (Subtarget->isTargetDarwin())
135 Mang->setUseQuotes(true);
136
137 return Result;
138 }
139
140 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
141 /// Don't print things like \n or \0.
142 static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
143 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
144 Name != E; ++Name)
145 if (isprint(*Name))
146 OS << *Name;
147 }
148
149 bool X86SharedAsmPrinter::doFinalization(Module &M) {
150 // Note: this code is not shared by the Intel printer as it is too different
151 // from how MASM does things. When making changes here don't forget to look
152 // at X86IntelAsmPrinter::doFinalization().
153 const TargetData *TD = TM.getTargetData();
154
155 // Print out module-level global variables here.
156 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
157 I != E; ++I) {
158 if (!I->hasInitializer())
159 continue; // External global require no code
160
161 // Check to see if this is a special global used by LLVM, if so, emit it.
162 if (EmitSpecialLLVMGlobal(I)) {
163 if (Subtarget->isTargetDarwin() &&
164 TM.getRelocationModel() == Reloc::Static) {
165 if (I->getName() == "llvm.global_ctors")
166 O << ".reference .constructors_used\n";
167 else if (I->getName() == "llvm.global_dtors")
168 O << ".reference .destructors_used\n";
169 }
170 continue;
171 }
172
173 std::string name = Mang->getValueName(I);
174 Constant *C = I->getInitializer();
175 const Type *Type = C->getType();
176 unsigned Size = TD->getABITypeSize(Type);
177 unsigned Align = TD->getPreferredAlignmentLog(I);
178
179 if (I->hasHiddenVisibility()) {
180 if (const char *Directive = TAI->getHiddenDirective())
181 O << Directive << name << "\n";
182 } else if (I->hasProtectedVisibility()) {
183 if (const char *Directive = TAI->getProtectedDirective())
184 O << Directive << name << "\n";
185 }
186
187 if (Subtarget->isTargetELF())
188 O << "\t.type\t" << name << ",@object\n";
189
190 if (C->isNullValue() && !I->hasSection()) {
191 if (I->hasExternalLinkage()) {
192 if (const char *Directive = TAI->getZeroFillDirective()) {
193 O << "\t.globl " << name << "\n";
194 O << Directive << "__DATA, __common, " << name << ", "
195 << Size << ", " << Align << "\n";
196 continue;
197 }
198 }
199
200 if (!I->isThreadLocal() &&
201 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
202 I->hasLinkOnceLinkage() || I->hasCommonLinkage())) {
203 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
204 if (!NoZerosInBSS && TAI->getBSSSection())
205 SwitchToDataSection(TAI->getBSSSection(), I);
206 else
207 SwitchToDataSection(TAI->getDataSection(), I);
208 if (TAI->getLCOMMDirective() != NULL) {
209 if (I->hasInternalLinkage()) {
210 O << TAI->getLCOMMDirective() << name << "," << Size;
211 if (Subtarget->isTargetDarwin())
212 O << "," << Align;
213 } else if (Subtarget->isTargetDarwin() && !I->hasCommonLinkage()) {
214 O << "\t.globl " << name << "\n"
215 << TAI->getWeakDefDirective() << name << "\n";
216 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
217 EmitAlignment(Align, I);
218 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
219 PrintUnmangledNameSafely(I, O);
220 O << "\n";
221 EmitGlobalConstant(C);
222 continue;
223 } else {
224 O << TAI->getCOMMDirective() << name << "," << Size;
225
226 // Leopard and above support aligned common symbols.
227 if (Subtarget->getDarwinVers() >= 9)
228 O << "," << Align;
229 }
230 } else {
231 if (!Subtarget->isTargetCygMing()) {
232 if (I->hasInternalLinkage())
233 O << "\t.local\t" << name << "\n";
234 }
235 O << TAI->getCOMMDirective() << name << "," << Size;
236 if (TAI->getCOMMDirectiveTakesAlignment())
237 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
238 }
239 O << "\t\t" << TAI->getCommentString() << " ";
240 PrintUnmangledNameSafely(I, O);
241 O << "\n";
242 continue;
243 }
244 }
245
246 switch (I->getLinkage()) {
247 case GlobalValue::CommonLinkage:
248 case GlobalValue::LinkOnceLinkage:
249 case GlobalValue::WeakLinkage:
250 if (Subtarget->isTargetDarwin()) {
251 O << "\t.globl " << name << "\n"
252 << TAI->getWeakDefDirective() << name << "\n";
253 if (!I->isConstant())
254 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
255 else {
256 const ArrayType *AT = dyn_cast(Type);
257 if (AT && AT->getElementType()==Type::Int8Ty)
258 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", I);
259 else
260 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
261 }
262 } else if (Subtarget->isTargetCygMing()) {
263 std::string SectionName(".section\t.data$linkonce." +
264 name +
265 ",\"aw\"");
266 SwitchToDataSection(SectionName.c_str(), I);
267 O << "\t.globl\t" << name << "\n"
268 << "\t.linkonce same_size\n";
269 } else {
270 std::string SectionName("\t.section\t.llvm.linkonce.d." +
271 name +
272 ",\"aw\",@progbits");
273 SwitchToDataSection(SectionName.c_str(), I);
274 O << "\t.weak\t" << name << "\n";
275 }
276 break;
277 case GlobalValue::DLLExportLinkage:
278 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
279 // FALL THROUGH
280 case GlobalValue::AppendingLinkage:
281 // FIXME: appending linkage variables should go into a section of
282 // their name or something. For now, just emit them as external.
283 case GlobalValue::ExternalLinkage:
284 // If external or appending, declare as a global symbol
285 O << "\t.globl " << name << "\n";
286 // FALL THROUGH
287 case GlobalValue::InternalLinkage: {
288 if (I->isConstant()) {
289 const ConstantArray *CVA = dyn_cast(C);
290 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
291 SwitchToDataSection(TAI->getCStringSection(), I);
292 break;
293 }
294 }
295 // FIXME: special handling for ".ctors" & ".dtors" sections
296 if (I->hasSection() &&
297 (I->getSection() == ".ctors" ||
298 I->getSection() == ".dtors")) {
299 std::string SectionName = ".section " + I->getSection();
300
301 if (Subtarget->isTargetCygMing()) {
302 SectionName += ",\"aw\"";
303 } else {
304 assert(!Subtarget->isTargetDarwin());
305 SectionName += ",\"aw\",@progbits";
306 }
307 SwitchToDataSection(SectionName.c_str());
308 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
309 // Honor all section names on Darwin; ObjC uses this
310 std::string SectionName = ".section " + I->getSection();
311 SwitchToDataSection(SectionName.c_str());
312 } else {
313 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
314 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
315 TAI->getBSSSection(), I);
316 else if (!I->isConstant())
317 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
318 TAI->getDataSection(), I);
319 else if (I->isThreadLocal())
320 SwitchToDataSection(TAI->getTLSDataSection());
321 else {
322 // Read-only data.
323 bool HasReloc = C->ContainsRelocations();
324 if (HasReloc &&
325 Subtarget->isTargetDarwin() &&
326 TM.getRelocationModel() != Reloc::Static)
327 SwitchToDataSection("\t.const_data\n");
328 else if (!HasReloc && Size == 4 &&
329 TAI->getFourByteConstantSection())
330 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
331 else if (!HasReloc && Size == 8 &&
332 TAI->getEightByteConstantSection())
333 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
334 else if (!HasReloc && Size == 16 &&
335 TAI->getSixteenByteConstantSection())
336 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
337 else if (TAI->getReadOnlySection())
338 SwitchToDataSection(TAI->getReadOnlySection(), I);
339 else
340 SwitchToDataSection(TAI->getDataSection(), I);
341 }
342 }
343
344 break;
345 }
346 default:
347 assert(0 && "Unknown linkage type!");
348 }
349
350 EmitAlignment(Align, I);
351 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
352 PrintUnmangledNameSafely(I, O);
353 O << "\n";
354 if (TAI->hasDotTypeDotSizeDirective())
355 O << "\t.size\t" << name << ", " << Size << "\n";
356 // If the initializer is a extern weak symbol, remember to emit the weak
357 // reference!
358 if (const GlobalValue *GV = dyn_cast(C))
359 if (GV->hasExternalWeakLinkage())
360 ExtWeakSymbols.insert(GV);
361
362 EmitGlobalConstant(C);
363 }
364
365 // Output linker support code for dllexported globals
366 if (!DLLExportedGVs.empty()) {
367 SwitchToDataSection(".section .drectve");
368 }
369
370 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
371 e = DLLExportedGVs.end();
372 i != e; ++i) {
373 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
374 }
375
376 if (!DLLExportedFns.empty()) {
377 SwitchToDataSection(".section .drectve");
378 }
379
380 for (StringSet<>::iterator i = DLLExportedFns.begin(),
381 e = DLLExportedFns.end();
382 i != e; ++i) {
383 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
384 }
385
386 if (Subtarget->isTargetDarwin()) {
387 SwitchToDataSection("");
388
389 // Output stubs for dynamically-linked functions
390 unsigned j = 1;
391 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
392 i != e; ++i, ++j) {
393 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
394 "self_modifying_code+pure_instructions,5", 0);
395 std::string p = i->getKeyData();
396 printSuffixedName(p, "$stub");
397 O << ":\n";
398 O << "\t.indirect_symbol " << p << "\n";
399 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
400 }
401
402 O << "\n";
403
404 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
405 // Add the (possibly multiple) personalities to the set of global values.
406 // Only referenced functions get into the Personalities list.
407 const std::vector& Personalities = MMI->getPersonalities();
408
409 for (std::vector::const_iterator I = Personalities.begin(),
410 E = Personalities.end(); I != E; ++I)
411 if (*I) GVStubs.insert("_" + (*I)->getName());
412 }
413
414 // Output stubs for external and common global variables.
415 if (!GVStubs.empty())
416 SwitchToDataSection(
417 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
418 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
419 i != e; ++i) {
420 std::string p = i->getKeyData();
421 printSuffixedName(p, "$non_lazy_ptr");
422 O << ":\n";
423 O << "\t.indirect_symbol " << p << "\n";
424 O << "\t.long\t0\n";
425 }
426
427 // Emit final debug information.
428 DW.EndModule();
429
430 // Funny Darwin hack: This flag tells the linker that no global symbols
431 // contain code that falls through to other global symbols (e.g. the obvious
432 // implementation of multiple entry points). If this doesn't occur, the
433 // linker can safely perform dead code stripping. Since LLVM never
434 // generates code that does this, it is always safe to set.
435 O << "\t.subsections_via_symbols\n";
436 } else if (Subtarget->isTargetCygMing()) {
437 // Emit type information for external functions
438 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
439 i != e; ++i) {
440 O << "\t.def\t " << i->getKeyData()
441 << ";\t.scl\t" << COFF::C_EXT
442 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
443 << ";\t.endef\n";
444 }
445
446 // Emit final debug information.
447 DW.EndModule();
448 } else if (Subtarget->isTargetELF()) {
449 // Emit final debug information.
450 DW.EndModule();
451 }
452
453 return AsmPrinter::doFinalization(M);
454 }
45520
45621 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
45722 /// for a MachineFunction to the given output stream, using the given target
None //===-- X86AsmPrinter.h - Convert X86 LLVM code to Intel assembly ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file the shared super class printer that converts from our internal
10 // representation of machine-dependent LLVM code to Intel and AT&T format
11 // assembly language. This printer is the output mechanism used by `llc'.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef X86ASMPRINTER_H
16 #define X86ASMPRINTER_H
17
18 #include "X86.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "X86TargetMachine.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/Support/Compiler.h"
26
27 namespace llvm {
28
29 struct VISIBILITY_HIDDEN X86SharedAsmPrinter : public AsmPrinter {
30 DwarfWriter DW;
31 MachineModuleInfo *MMI;
32
33 X86SharedAsmPrinter(std::ostream &O, X86TargetMachine &TM,
34 const TargetAsmInfo *T)
35 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(0) {
36 Subtarget = &TM.getSubtarget();
37 }
38
39 // We have to propagate some information about MachineFunction to
40 // AsmPrinter. It's ok, when we're printing the function, since we have
41 // access to MachineFunction and can get the appropriate MachineFunctionInfo.
42 // Unfortunately, this is not possible when we're printing reference to
43 // Function (e.g. calling it and so on). Even more, there is no way to get the
44 // corresponding MachineFunctions: it can even be not created at all. That's
45 // why we should use additional structure, when we're collecting all necessary
46 // information.
47 //
48 // This structure is using e.g. for name decoration for stdcall & fastcall'ed
49 // function, since we have to use arguments' size for decoration.
50 typedef std::map FMFInfoMap;
51 FMFInfoMap FunctionInfoMap;
52
53 void decorateName(std::string& Name, const GlobalValue* GV);
54
55 bool doInitialization(Module &M);
56 bool doFinalization(Module &M);
57
58 void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 if (Subtarget->isTargetDarwin() ||
61 Subtarget->isTargetELF() ||
62 Subtarget->isTargetCygMing()) {
63 AU.addRequired();
64 }
65 AsmPrinter::getAnalysisUsage(AU);
66 }
67
68 const X86Subtarget *Subtarget;
69
70 // Necessary for Darwin to print out the apprioriate types of linker stubs
71 StringSet<> FnStubs, GVStubs, LinkOnceStubs;
72
73 // Necessary for dllexport support
74 StringSet<> DLLExportedFns, DLLExportedGVs;
75 };
76
77 } // end namespace llvm
78
79 #endif