Added source file/line correspondence for dwarf (PowerPC only at this point.)
authorJim Laskey <jlaskey@mac.com>
Fri, 16 Dec 2005 22:45:29 +0000 (22:45 +0000)
committerJim Laskey <jlaskey@mac.com>
Fri, 16 Dec 2005 22:45:29 +0000 (22:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24748 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/MachineDebugInfo.h [new file with mode: 0644]
include/llvm/CodeGen/MachineFunction.h
include/llvm/CodeGen/SelectionDAGNodes.h
lib/CodeGen/MachineFunction.cpp
lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
lib/CodeGen/SelectionDAG/SelectionDAG.cpp
lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
lib/Target/PowerPC/PPCAsmPrinter.cpp
lib/Target/PowerPC/PPCInstrInfo.td
lib/Target/TargetSelectionDAG.td

diff --git a/include/llvm/CodeGen/MachineDebugInfo.h b/include/llvm/CodeGen/MachineDebugInfo.h
new file mode 100644 (file)
index 0000000..0dba586
--- /dev/null
@@ -0,0 +1,80 @@
+//===-- llvm/CodeGen/MachineDebugInfo.h -------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by James M. Laskey and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Collect debug information for a module.  This information should be in a
+// neutral form that can be used by different debugging schemes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_MACHINEDEBUGINFO_H
+#define LLVM_CODEGEN_MACHINEDEBUGINFO_H
+
+#include <string>
+#include <map>
+#include <vector>
+
+namespace llvm {
+//===----------------------------------------------------------------------===//
+/// MachineDebugInfo - This class contains debug information specific to a
+/// module.  Queries can be made by different debugging schemes and reformated
+/// for specific use.
+///
+class MachineDebugInfo {
+private:
+  // convenience types
+  typedef std::map<std::string, unsigned> StrIntMap;
+  typedef StrIntMap::iterator StrIntMapIter;
+  
+  StrIntMap SourceMap;                  // Map of source file path to id
+  unsigned SourceCount;                 // Number of source files (used to
+                                        // generate id)
+
+public:
+  // Ctor.
+  MachineDebugInfo() : SourceMap(), SourceCount(0) {}
+  
+  /// RecordSource - Register a source file with debug info.  Returns an id.
+  ///
+  unsigned RecordSource(std::string fname, std::string dirname) {
+    // Compose a key
+    std::string path = dirname + "/" + fname;
+    // Check if the source file is already recorded
+    StrIntMapIter SMI = SourceMap.find(path);
+    // If already there return existing id
+    if (SMI != SourceMap.end()) return SMI->second;
+    // Bump up the count
+    ++SourceCount;
+    // Record the count
+    SourceMap[path] = SourceCount;
+    // Return id
+    return SourceCount;
+  }
+
+  /// getSourceFiles - Return a vector of files.  Vector index + 1 equals id.
+  ///
+  std::vector<std::string> getSourceFiles() {
+    std::vector<std::string> Sources(SourceCount);
+    
+    for (StrIntMapIter SMI = SourceMap.begin(), E = SourceMap.end(); SMI != E;
+                       SMI++) {
+      unsigned Index = SMI->second - 1;
+      std::string Path = SMI->first;
+      Sources[Index] = Path;
+    }
+    return Sources;
+  }
+  
+}; // End class MachineDebugInfo
+//===----------------------------------------------------------------------===//
+
+
+
+} // End llvm namespace
+
+#endif
index 8727793eec6531f4d78825f6f19502e2874a2904..05077fe5f070d3efee4db20a622ffea5001db58d 100644 (file)
@@ -18,6 +18,7 @@
 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
 #define LLVM_CODEGEN_MACHINEFUNCTION_H
 
+#include "llvm/CodeGen/MachineDebugInfo.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/Support/Annotation.h"
 
@@ -112,6 +113,10 @@ class MachineFunction : private Annotation {
   /// stored in the second element.
   std::vector<std::pair<unsigned, unsigned> > LiveIns;
   std::vector<unsigned> LiveOuts;
+  
+  /// DebugInfo - Keep track of debug information for the function.
+  ///
+  MachineDebugInfo DebugInfo;
 
 public:
   MachineFunction(const Function *Fn, const TargetMachine &TM);
@@ -212,6 +217,11 @@ public:
   const MachineBasicBlock *getLastBlock() const {
     return MBBNumbering.back();
   }
+  
+  /// getDebugInfo - Returns the DebugInfo.
+  MachineDebugInfo &getDebugInfo() {
+    return DebugInfo;
+  }
 
   /// print - Print out the MachineFunction in a format suitable for debugging
   /// to the specified stream.
index 10b9050e268fd6b35fb331e501d0a8b12a741cf0..bf0191031b0721aa3130d8809de7edd64023530e 100644 (file)
@@ -352,6 +352,12 @@ namespace ISD {
     // as output.
     LOCATION,
     
+    // DEBUG_LOC - This node is used to represent source line information
+    // embedded in the code.  It takes token chain as input, then a line number,
+    // then a column then a file id (provided by MachineDebugInfo.  It produces
+    // a token chain as output.
+    DEBUG_LOC,
+    
     // BUILTIN_OP_END - This must be the last enum value in this list.
     BUILTIN_OP_END,
   };
index f0ece6b3dbbec51691d853cc9e31875e03cfbc9b..f51176ff2104bcb60796b85a89ff21db0f4fd43c 100644 (file)
@@ -108,7 +108,7 @@ void ilist_traits<MachineBasicBlock>::transferNodesFromList(
 
 MachineFunction::MachineFunction(const Function *F,
                                  const TargetMachine &TM)
-  : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
+  : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0), DebugInfo() {
   SSARegMapping = new SSARegMap();
   MFInfo = 0;
   FrameInfo = new MachineFrameInfo();
index 15376216a51be4694c4b27c5f951871e069f7cb7..c5b7bf9f5413ad30f3dc0c7adbae634b493de1af 100644 (file)
@@ -611,9 +611,18 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
     switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
     case TargetLowering::Promote:
     default: assert(0 && "This action is not supported yet!");
-    case TargetLowering::Expand:
-      // If the target doesn't support line numbers, ignore this node.
-      Result = Tmp1;
+    case TargetLowering::Expand: {
+        MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
+        std::vector<SDOperand> Ops;
+        Ops.push_back(Tmp1);  // chain
+        Ops.push_back(Node->getOperand(1));  // line #
+        Ops.push_back(Node->getOperand(2));  // col #
+        const std::string &fname = cast<StringSDNode>(Node->getOperand(3))->getValue();
+        const std::string &dirname=cast<StringSDNode>(Node->getOperand(4))->getValue();
+        unsigned id = DebugInfo.RecordSource(fname, dirname);
+        Ops.push_back(DAG.getConstant(id, MVT::i32));  // source file id
+        Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
+      }
       break;
     case TargetLowering::Legal:
       if (Tmp1 != Node->getOperand(0) ||
@@ -635,6 +644,28 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
       break;
     }
     break;
+    
+  case ISD::DEBUG_LOC:
+    assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
+    switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
+    case TargetLowering::Promote:
+    case TargetLowering::Expand:
+    default: assert(0 && "This action is not supported yet!");
+    case TargetLowering::Legal:
+      Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
+      Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the line #.
+      Tmp3 = LegalizeOp(Node->getOperand(2));  // Legalize the col #.
+      Tmp4 = LegalizeOp(Node->getOperand(3));  // Legalize the source file id.
+      
+      if (Tmp1 != Node->getOperand(0) ||
+          Tmp2 != Node->getOperand(1) ||
+          Tmp3 != Node->getOperand(2) ||
+          Tmp4 != Node->getOperand(3)) {
+        Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
+      }
+      break;
+    }
+    break;    
 
   case ISD::Constant:
     // We know we don't need to expand constants here, constants only have one
index 30ebeb2b8e9b063a79229128c523b704ca686f4d..2fe1b0df0de91d6843561ca68b7eb5b8a39083d6 100644 (file)
@@ -1961,6 +1961,7 @@ const char *SDNode::getOperationName(const SelectionDAG *G) const {
 
   // Debug info
   case ISD::LOCATION: return "location";
+  case ISD::DEBUG_LOC: return "debug_loc";
 
   case ISD::CONDCODE:
     switch (cast<CondCodeSDNode>(this)->get()) {
index 20335fbaa47ce26c33c873acf8af0a27e5c79be1..e6a0913a9b5a68b134715301c5e84be0f7b922b4 100644 (file)
@@ -918,8 +918,8 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
       if (ConstantStruct *CS = 
             dyn_cast<ConstantStruct>(cunit->getInitializer())) {
         if (CS->getNumOperands() > 0) {
-          Ops.push_back(DAG.getString(getStringValue(CS->getOperand(4))));
           Ops.push_back(DAG.getString(getStringValue(CS->getOperand(3))));
+          Ops.push_back(DAG.getString(getStringValue(CS->getOperand(4))));
         }
       }
     }
index 83e3b0519a93879341fffbfc6b2959a73ad27bd2..cb27e1f3888672e340b1b5bedd60e7ffde2d8f71 100644 (file)
@@ -25,6 +25,7 @@
 #include "llvm/Module.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/CodeGen/AsmPrinter.h"
+#include "llvm/CodeGen/MachineDebugInfo.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/Support/Mangler.h"
@@ -375,6 +376,13 @@ void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
 bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
   SetupMachineFunction(MF);
   O << "\n\n";
+  
+  // Print out dwarf file info
+  MachineDebugInfo &DebugInfo = MF.getDebugInfo();
+  std::vector<std::string> Sources = DebugInfo.getSourceFiles();
+  for (unsigned i = 0, N = Sources.size(); i < N; i++) {
+    O << "\t; .file\t" << (i + 1) << "," << "\"" << Sources[i]  << "\"" << "\n";
+  }
 
   // Print out constants referenced by the function
   EmitConstantPool(MF.getConstantPool());
index 225017cea787f250d917eb214026c1e048b12b39..af7560b648c93609451ff730caf5c7a8a6278668 100644 (file)
@@ -921,6 +921,15 @@ def V_SET0 : VXForm_setzero<1220, (ops VRRC:$vD),
                       []>;
 
 
+//===----------------------------------------------------------------------===//
+// DWARF Pseudo Instructions
+//
+
+def DWARF_LOC        : Pseudo<(ops i32imm:$line, i32imm:$col, i32imm:$file),
+                              "; .loc $file, $line, $col",
+                      [(dwarf_loc (i32 imm:$line), (i32 imm:$col),
+                                  (i32 imm:$file))]>;
+
 //===----------------------------------------------------------------------===//
 // PowerPC Instruction Patterns
 //
index 36f83c168f036c1af3d7d0a44bddf27dbc70eb74..0da445d5d14ea666c090aae6742ba49dd4131e58 100644 (file)
@@ -415,3 +415,13 @@ class ComplexPattern<ValueType ty, int numops, string fn, list<SDNode> roots = [
   string SelectFunc = fn;
   list<SDNode> RootNodes = roots;
 }
+
+//===----------------------------------------------------------------------===//
+// Dwarf support.
+//
+def SDT_dwarf_loc : SDTypeProfile<0, 3,
+                                    [SDTCisInt<0>, SDTCisInt<1>, SDTCisInt<2>]>;
+def dwarf_loc  : SDNode<"ISD::DEBUG_LOC", SDT_dwarf_loc,[SDNPHasChain]>;
+
+
+