From 1c4b5eaa4628935c4db3e422aa10f2031e6d1679 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Sat, 28 Jun 2008 11:07:54 +0000 Subject: [PATCH] Make intel asmprinter child of generic asmprinter, not x86 shared asm printer. This leads to some code duplication, which will be resolved later. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52858 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/X86/X86AsmPrinter.h | 17 ----- lib/Target/X86/X86InstrInfo.h | 17 +++++ lib/Target/X86/X86IntelAsmPrinter.cpp | 97 +++++++++++++++++++++++++-- lib/Target/X86/X86IntelAsmPrinter.h | 34 ++++++++-- 4 files changed, 138 insertions(+), 27 deletions(-) diff --git a/lib/Target/X86/X86AsmPrinter.h b/lib/Target/X86/X86AsmPrinter.h index 95e24605505..b9c79fee4b6 100644 --- a/lib/Target/X86/X86AsmPrinter.h +++ b/lib/Target/X86/X86AsmPrinter.h @@ -73,23 +73,6 @@ struct VISIBILITY_HIDDEN X86SharedAsmPrinter : public AsmPrinter { // Necessary for dllexport support StringSet<> DLLExportedFns, DLLExportedGVs; - - inline static bool isScale(const MachineOperand &MO) { - return MO.isImmediate() && - (MO.getImm() == 1 || MO.getImm() == 2 || - MO.getImm() == 4 || MO.getImm() == 8); - } - - inline static bool isMem(const MachineInstr *MI, unsigned Op) { - if (MI->getOperand(Op).isFrameIndex()) return true; - return Op+4 <= MI->getNumOperands() && - MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) && - MI->getOperand(Op+2).isRegister() && - (MI->getOperand(Op+3).isImmediate() || - MI->getOperand(Op+3).isGlobalAddress() || - MI->getOperand(Op+3).isConstantPoolIndex() || - MI->getOperand(Op+3).isJumpTableIndex()); - } }; } // end namespace llvm diff --git a/lib/Target/X86/X86InstrInfo.h b/lib/Target/X86/X86InstrInfo.h index 8bbe64d1a5d..d24156531e4 100644 --- a/lib/Target/X86/X86InstrInfo.h +++ b/lib/Target/X86/X86InstrInfo.h @@ -227,6 +227,23 @@ namespace X86II { }; } +inline static bool isScale(const MachineOperand &MO) { + return MO.isImmediate() && + (MO.getImm() == 1 || MO.getImm() == 2 || + MO.getImm() == 4 || MO.getImm() == 8); +} + +inline static bool isMem(const MachineInstr *MI, unsigned Op) { + if (MI->getOperand(Op).isFrameIndex()) return true; + return Op+4 <= MI->getNumOperands() && + MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) && + MI->getOperand(Op+2).isRegister() && + (MI->getOperand(Op+3).isImmediate() || + MI->getOperand(Op+3).isGlobalAddress() || + MI->getOperand(Op+3).isConstantPoolIndex() || + MI->getOperand(Op+3).isJumpTableIndex()); +} + class X86InstrInfo : public TargetInstrInfoImpl { X86TargetMachine &TM; const X86RegisterInfo RI; diff --git a/lib/Target/X86/X86IntelAsmPrinter.cpp b/lib/Target/X86/X86IntelAsmPrinter.cpp index aa64668d97b..c46daaed9dd 100644 --- a/lib/Target/X86/X86IntelAsmPrinter.cpp +++ b/lib/Target/X86/X86IntelAsmPrinter.cpp @@ -15,20 +15,109 @@ #define DEBUG_TYPE "asm-printer" #include "X86IntelAsmPrinter.h" +#include "X86InstrInfo.h" #include "X86TargetAsmInfo.h" #include "X86.h" #include "llvm/CallingConv.h" #include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" #include "llvm/Module.h" +#include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Assembly/Writer.h" #include "llvm/Support/Mangler.h" #include "llvm/Target/TargetAsmInfo.h" #include "llvm/Target/TargetOptions.h" -#include "llvm/ADT/Statistic.h" using namespace llvm; STATISTIC(EmittedInsts, "Number of machine instrs printed"); +static X86MachineFunctionInfo calculateFunctionInfo(const Function *F, + const TargetData *TD) { + X86MachineFunctionInfo Info; + uint64_t Size = 0; + + switch (F->getCallingConv()) { + case CallingConv::X86_StdCall: + Info.setDecorationStyle(StdCall); + break; + case CallingConv::X86_FastCall: + Info.setDecorationStyle(FastCall); + break; + default: + return Info; + } + + unsigned argNum = 1; + for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); + AI != AE; ++AI, ++argNum) { + const Type* Ty = AI->getType(); + + // 'Dereference' type in case of byval parameter attribute + if (F->paramHasAttr(argNum, ParamAttr::ByVal)) + Ty = cast(Ty)->getElementType(); + + // Size should be aligned to DWORD boundary + Size += ((TD->getABITypeSize(Ty) + 3)/4)*4; + } + + // We're not supporting tooooo huge arguments :) + Info.setBytesToPopOnReturn((unsigned int)Size); + return Info; +} + + +/// decorateName - Query FunctionInfoMap and use this information for various +/// name decoration. +void X86IntelAsmPrinter::decorateName(std::string &Name, + const GlobalValue *GV) { + const Function *F = dyn_cast(GV); + if (!F) return; + + // We don't want to decorate non-stdcall or non-fastcall functions right now + unsigned CC = F->getCallingConv(); + if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) + return; + + FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F); + + const X86MachineFunctionInfo *Info; + if (info_item == FunctionInfoMap.end()) { + // Calculate apropriate function info and populate map + FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData()); + Info = &FunctionInfoMap[F]; + } else { + Info = &info_item->second; + } + + const FunctionType *FT = F->getFunctionType(); + switch (Info->getDecorationStyle()) { + case None: + break; + case StdCall: + // "Pure" variadic functions do not receive @0 suffix. + if (!FT->isVarArg() || (FT->getNumParams() == 0) || + (FT->getNumParams() == 1 && F->hasStructRetAttr())) + Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); + break; + case FastCall: + // "Pure" variadic functions do not receive @0 suffix. + if (!FT->isVarArg() || (FT->getNumParams() == 0) || + (FT->getNumParams() == 1 && F->hasStructRetAttr())) + Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); + + if (Name[0] == '_') + Name[0] = '@'; + else + Name = '@' + Name; + + break; + default: + assert(0 && "Unsupported DecorationStyle"); + } +} + + std::string X86IntelAsmPrinter::getSectionForFunction(const Function &F) const { // Intel asm always emits functions to _text. return "_text"; @@ -53,7 +142,7 @@ bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) { if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) FunctionInfoMap[F] = *MF.getInfo(); - X86SharedAsmPrinter::decorateName(CurrentFnName, F); + decorateName(CurrentFnName, F); SwitchToTextSection(getSectionForFunction(*F).c_str(), F); @@ -162,7 +251,7 @@ void X86IntelAsmPrinter::printOp(const MachineOperand &MO, GlobalValue *GV = MO.getGlobal(); std::string Name = Mang->getValueName(GV); - X86SharedAsmPrinter::decorateName(Name, GV); + decorateName(Name, GV); if (!isMemOp && !isCallOp) O << "OFFSET "; if (GV->hasDLLImportLinkage()) { @@ -328,7 +417,7 @@ bool X86IntelAsmPrinter::doInitialization(Module &M) { for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) if (I->isDeclaration()) { std::string Name = Mang->getValueName(I); - X86SharedAsmPrinter::decorateName(Name, I); + decorateName(Name, I); O << "\textern " ; if (I->hasDLLImportLinkage()) { diff --git a/lib/Target/X86/X86IntelAsmPrinter.h b/lib/Target/X86/X86IntelAsmPrinter.h index 2a10e1c9b71..ea1e9a76056 100644 --- a/lib/Target/X86/X86IntelAsmPrinter.h +++ b/lib/Target/X86/X86IntelAsmPrinter.h @@ -14,16 +14,19 @@ #ifndef X86INTELASMPRINTER_H #define X86INTELASMPRINTER_H -#include "X86AsmPrinter.h" -#include "llvm/CodeGen/ValueTypes.h" -#include "llvm/Target/TargetRegisterInfo.h" +#include "X86.h" +#include "X86MachineFunctionInfo.h" +#include "X86TargetMachine.h" +#include "llvm/CodeGen/AsmPrinter.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/Support/Compiler.h" namespace llvm { -struct VISIBILITY_HIDDEN X86IntelAsmPrinter : public X86SharedAsmPrinter { +struct VISIBILITY_HIDDEN X86IntelAsmPrinter : public AsmPrinter { X86IntelAsmPrinter(std::ostream &O, X86TargetMachine &TM, const TargetAsmInfo *T) - : X86SharedAsmPrinter(O, TM, T) { + : AsmPrinter(O, TM, T) { } virtual const char *getPassName() const { @@ -110,12 +113,31 @@ struct VISIBILITY_HIDDEN X86IntelAsmPrinter : public X86SharedAsmPrinter { bool runOnMachineFunction(MachineFunction &F); bool doInitialization(Module &M); bool doFinalization(Module &M); - + + // We have to propagate some information about MachineFunction to + // AsmPrinter. It's ok, when we're printing the function, since we have + // access to MachineFunction and can get the appropriate MachineFunctionInfo. + // Unfortunately, this is not possible when we're printing reference to + // Function (e.g. calling it and so on). Even more, there is no way to get the + // corresponding MachineFunctions: it can even be not created at all. That's + // why we should use additional structure, when we're collecting all necessary + // information. + // + // This structure is using e.g. for name decoration for stdcall & fastcall'ed + // function, since we have to use arguments' size for decoration. + typedef std::map FMFInfoMap; + FMFInfoMap FunctionInfoMap; + + void decorateName(std::string& Name, const GlobalValue* GV); + /// getSectionForFunction - Return the section that we should emit the /// specified function body into. virtual std::string getSectionForFunction(const Function &F) const; virtual void EmitString(const ConstantArray *CVA) const; + + // Necessary for dllexport support + StringSet<> DLLExportedFns, DLLExportedGVs; }; } // end namespace llvm -- 2.34.1