[llvm-commits] [llvm] r52858 - in /llvm/trunk/lib/Target/X86: X86AsmPrinter.h X86InstrInfo.h X86IntelAsmPrinter.cpp X86IntelAsmPrinter.h

Anton Korobeynikov asl at math.spbu.ru
Sat Jun 28 04:07:54 PDT 2008


Author: asl
Date: Sat Jun 28 06:07:54 2008
New Revision: 52858

URL: http://llvm.org/viewvc/llvm-project?rev=52858&view=rev
Log:
Make intel asmprinter child of generic asmprinter, not x86 shared asm printer. This leads to some code duplication, which will be resolved later.

Modified:
    llvm/trunk/lib/Target/X86/X86AsmPrinter.h
    llvm/trunk/lib/Target/X86/X86InstrInfo.h
    llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.cpp
    llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.h

Modified: llvm/trunk/lib/Target/X86/X86AsmPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86AsmPrinter.h?rev=52858&r1=52857&r2=52858&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86AsmPrinter.h (original)
+++ llvm/trunk/lib/Target/X86/X86AsmPrinter.h Sat Jun 28 06:07:54 2008
@@ -73,23 +73,6 @@
 
   // 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

Modified: llvm/trunk/lib/Target/X86/X86InstrInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86InstrInfo.h?rev=52858&r1=52857&r2=52858&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86InstrInfo.h (original)
+++ llvm/trunk/lib/Target/X86/X86InstrInfo.h Sat Jun 28 06:07:54 2008
@@ -227,6 +227,23 @@
   };
 }
 
+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;

Modified: llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.cpp?rev=52858&r1=52857&r2=52858&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.cpp Sat Jun 28 06:07:54 2008
@@ -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<PointerType>(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<Function>(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 @@
   if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
     FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
 
-  X86SharedAsmPrinter::decorateName(CurrentFnName, F);
+  decorateName(CurrentFnName, F);
 
   SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
 
@@ -162,7 +251,7 @@
     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 @@
   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()) {

Modified: llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.h?rev=52858&r1=52857&r2=52858&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.h (original)
+++ llvm/trunk/lib/Target/X86/X86IntelAsmPrinter.h Sat Jun 28 06:07:54 2008
@@ -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 @@
   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<const Function*, X86MachineFunctionInfo> 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





More information about the llvm-commits mailing list