LLVM API Documentation

DebugInfoBuilder.cpp

Go to the documentation of this file.
00001 //===-- llvm/VMCore/DebugInfoBuilder.cpp - ----------------------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the definition of the DebugInfoBuilder class, which is
00011 // a helper class used to construct source level debugging information.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include <llvm/Support/DebugInfoBuilder.h>
00016 #include <llvm/DerivedTypes.h>
00017 #include <llvm/Constants.h>
00018 #include <llvm/GlobalVariable.h>
00019 #include <llvm/Module.h>
00020 #include <llvm/Support/Dwarf.h>
00021 #include <llvm/System/Path.h>
00022 
00023 using namespace llvm;
00024 
00025 namespace {
00026     
00027 //===----------------------------------------------------------------------===//
00028 // Debug version -- copied from MachineModuleInfo (for now), in order to avoid
00029 // creating a dependency on CodeGen. These declarations really should be moved
00030 // to a better place where modules can get at them without being dependent on
00031 // CodeGen.
00032 enum {
00033   LLVMDebugVersion = (6 << 16),         // Current version of debug information.
00034   LLVMDebugVersion5 = (5 << 16),        // Constant for version 5.
00035   LLVMDebugVersion4 = (4 << 16),        // Constant for version 4.
00036   LLVMDebugVersionMask = 0xffff0000     // Mask for version number.
00037 };
00038 
00039 const char ANCHOR_TYPE_NAME[] = "llvm.dbg.anchor.type";
00040 const char COMPILE_UNIT_ANCHOR_NAME[] = "llvm.dbg.compile_units";
00041 const char GLOBAL_VAR_ANCHOR_NAME[] = "llvm.dbg.global_variables";
00042 const char SUBPROGRAM_ANCHOR_NAME[] = "llvm.dbg.subprograms";
00043 const char COMPILE_UNIT_TYPE_NAME[] = "llvm.dbg.compile_unit.type";
00044 const char COMPILE_UNIT_NAME[] = "llvm.dbg.compile_unit";
00045 const char SUBPROGRAM_NAME[] = "llvm.dbg.subprogram";
00046 const char BASICTYPE_NAME[] = "llvm.dbg.basictype";
00047 const char DERIVEDTYPE_NAME[] = "llvm.dbg.derivedtype";
00048 
00049 } // end anonymous namespace
00050 
00051 DebugInfoBuilder::DebugInfoBuilder() {
00052     anyPtrType = PointerType::getUnqual(StructType::get(NULL, NULL));
00053     anchorType = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
00054 }
00055 
00056 GlobalVariable * DebugInfoBuilder::createAnchor(unsigned anchorTag,
00057     const char * anchorName) {
00058 
00059     std::vector<Constant *> values;
00060     values.push_back(ConstantInt::get(Type::Int32Ty, LLVMDebugVersion));
00061     values.push_back(ConstantInt::get(Type::Int32Ty, anchorTag));
00062     
00063     return new GlobalVariable(anchorType, true, GlobalValue::LinkOnceLinkage,
00064         ConstantStruct::get(anchorType, values), anchorName, module);
00065 }
00066 
00067 // Calculate the size of the specified LLVM type.
00068 Constant * DebugInfoBuilder::getSize(const Type * type) {
00069     Constant * one = ConstantInt::get(Type::Int32Ty, 1);
00070     return ConstantExpr::getPtrToInt(
00071         ConstantExpr::getGetElementPtr(
00072             ConstantPointerNull::get(PointerType::getUnqual(type)),
00073             &one, 1), Type::Int32Ty);
00074 }
00075     
00076 Constant * DebugInfoBuilder::getAlignment(const Type * type) {
00077     // Calculates the alignment of T using "sizeof({i8, T}) - sizeof(T)"
00078     return ConstantExpr::getSub(
00079         getSize(StructType::get(Type::Int8Ty, type, NULL)),
00080         getSize(type));
00081 }
00082     
00083 void DebugInfoBuilder::setModule(Module * m) {
00084     module = m;
00085     module->addTypeName(ANCHOR_TYPE_NAME, anchorType);
00086     
00087     compileUnitAnchor = module->getGlobalVariable(COMPILE_UNIT_ANCHOR_NAME);
00088     if (compileUnitAnchor == NULL) {
00089         compileUnitAnchor =
00090             createAnchor(dwarf::DW_TAG_compile_unit, COMPILE_UNIT_ANCHOR_NAME);
00091     }
00092 
00093     globalVariableAnchor = module->getGlobalVariable(GLOBAL_VAR_ANCHOR_NAME);
00094     if (globalVariableAnchor == NULL) {
00095         globalVariableAnchor =
00096             createAnchor(dwarf::DW_TAG_compile_unit, GLOBAL_VAR_ANCHOR_NAME);
00097     }
00098 
00099     subprogramAnchor = module->getGlobalVariable(SUBPROGRAM_ANCHOR_NAME);
00100     if (subprogramAnchor == NULL) {
00101         subprogramAnchor =
00102             createAnchor(dwarf::DW_TAG_compile_unit, SUBPROGRAM_ANCHOR_NAME);
00103     }
00104     
00105     compileUnit = module->getGlobalVariable(COMPILE_UNIT_NAME);
00106     setContext(compileUnit);
00107 }
00108     
00109 GlobalVariable * DebugInfoBuilder::createCompileUnitDescriptor(unsigned langId,
00110     const sys::Path & srcPath, const std::string & producer) {
00111 
00112     if (compileUnit == NULL) {
00113         std::vector<Constant *> values;
00114         values.push_back(ConstantInt::get(
00115             Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_compile_unit));
00116         values.push_back(
00117             ConstantExpr::getBitCast(compileUnitAnchor, anyPtrType));
00118         values.push_back(ConstantInt::get(Type::Int32Ty, langId));
00119         values.push_back(ConstantArray::get(srcPath.getLast()));
00120         values.push_back(ConstantArray::get(srcPath.getDirname() + "/"));
00121         values.push_back(ConstantArray::get(producer));
00122     
00123         Constant * structVal = ConstantStruct::get(values, false);
00124         compileUnit = new GlobalVariable(structVal->getType(), true,
00125             GlobalValue::InternalLinkage, structVal, COMPILE_UNIT_NAME, module);
00126     }
00127 
00128     setContext(compileUnit);
00129     return compileUnit;
00130 }
00131 
00132 GlobalVariable * DebugInfoBuilder::createSubProgramDescriptor(
00133     const std::string & name,
00134     const std::string & qualifiedName,
00135     unsigned line,
00136     GlobalVariable * typeDesc,
00137     bool isInternal,
00138     bool isDefined) {
00139         
00140     assert(compileUnit != NULL);
00141     assert(subprogramAnchor != NULL);
00142         
00143     std::vector<Constant *> values;
00144     values.push_back(ConstantInt::get(
00145         Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_subprogram));
00146     values.push_back(ConstantExpr::getBitCast(subprogramAnchor, anyPtrType));
00147     values.push_back(ConstantExpr::getBitCast(context, anyPtrType));
00148     values.push_back(ConstantArray::get(name));
00149     values.push_back(ConstantArray::get(qualifiedName));
00150     values.push_back(ConstantArray::get(qualifiedName));
00151     values.push_back(ConstantExpr::getBitCast(compileUnit, anyPtrType));
00152     values.push_back(ConstantInt::get(Type::Int32Ty, line));
00153     values.push_back(typeDesc ?
00154         ConstantExpr::getBitCast(typeDesc, anyPtrType) :
00155         ConstantPointerNull::get(anyPtrType));
00156     values.push_back(ConstantInt::get(Type::Int1Ty, isInternal));
00157     values.push_back(ConstantInt::get(Type::Int1Ty, isDefined));
00158     
00159     Constant * structVal = ConstantStruct::get(values, false);
00160     return new GlobalVariable(structVal->getType(), true,
00161         GlobalValue::InternalLinkage, structVal, SUBPROGRAM_NAME, module);
00162 }
00163 
00164 GlobalVariable * DebugInfoBuilder::createBasicTypeDescriptor(
00165     std::string & name,
00166     unsigned line,
00167     unsigned sizeInBits,
00168     unsigned alignmentInBits,
00169     unsigned offsetInBits,
00170     unsigned typeEncoding) {
00171 
00172     std::vector<Constant *> values;
00173     values.push_back(ConstantInt::get(
00174         Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
00175     values.push_back(ConstantExpr::getBitCast(context, anyPtrType));
00176     values.push_back(ConstantArray::get(name));
00177     values.push_back(ConstantExpr::getBitCast(compileUnit, anyPtrType));
00178     values.push_back(ConstantInt::get(Type::Int32Ty, line));
00179     values.push_back(ConstantInt::get(Type::Int32Ty, sizeInBits));
00180     values.push_back(ConstantInt::get(Type::Int32Ty, alignmentInBits));
00181     values.push_back(ConstantInt::get(Type::Int32Ty, offsetInBits));
00182     values.push_back(ConstantInt::get(Type::Int32Ty, typeEncoding));
00183 
00184     Constant * structVal = ConstantStruct::get(values, false);
00185     return new GlobalVariable(structVal->getType(), true,
00186         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
00187 }
00188 
00189 GlobalVariable * DebugInfoBuilder::createIntegerTypeDescriptor(
00190     std::string & name, const IntegerType * type, bool isSigned) {
00191 
00192     std::vector<Constant *> values;
00193     values.push_back(ConstantInt::get(
00194         Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
00195     values.push_back(ConstantPointerNull::get(anyPtrType));
00196     values.push_back(ConstantArray::get(name));
00197     values.push_back(ConstantPointerNull::get(anyPtrType));
00198     values.push_back(ConstantInt::get(Type::Int32Ty, 0));
00199     values.push_back(ConstantInt::get(Type::Int32Ty, type->getBitWidth()));
00200     values.push_back(getAlignment(type));
00201     values.push_back(ConstantInt::get(Type::Int32Ty, 0));
00202     values.push_back(ConstantInt::get(Type::Int32Ty,
00203         isSigned ? dwarf::DW_ATE_signed_char : dwarf::DW_ATE_unsigned_char));
00204 
00205     Constant * structVal = ConstantStruct::get(values, false);
00206     return new GlobalVariable(structVal->getType(), true,
00207         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
00208 }
00209 
00210 GlobalVariable * DebugInfoBuilder::createCharacterTypeDescriptor(
00211     std::string & name, const IntegerType * type, bool isSigned) {
00212 
00213     std::vector<Constant *> values;
00214     values.push_back(ConstantInt::get(
00215         Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
00216     values.push_back(ConstantPointerNull::get(anyPtrType));
00217     values.push_back(ConstantArray::get(name));
00218     values.push_back(ConstantPointerNull::get(anyPtrType));
00219     values.push_back(ConstantInt::get(Type::Int32Ty, 0));
00220     values.push_back(ConstantInt::get(Type::Int32Ty, type->getBitWidth()));
00221     values.push_back(getAlignment(type));
00222     values.push_back(ConstantInt::get(Type::Int32Ty, 0/*offsetInBits*/));
00223     values.push_back(ConstantInt::get(Type::Int32Ty,
00224         isSigned ? dwarf::DW_ATE_signed_char : dwarf::DW_ATE_unsigned_char));
00225 
00226     Constant * structVal = ConstantStruct::get(values, false);
00227     return new GlobalVariable(structVal->getType(), true,
00228         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
00229 }
00230 
00231 GlobalVariable * DebugInfoBuilder::createFloatTypeDescriptor(
00232     std::string & name, const Type * type) {
00233 
00234     std::vector<Constant *> values;
00235     values.push_back(ConstantInt::get(
00236         Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
00237     values.push_back(ConstantPointerNull::get(anyPtrType));
00238     values.push_back(ConstantArray::get(name));
00239     values.push_back(ConstantPointerNull::get(anyPtrType));
00240     values.push_back(ConstantInt::get(Type::Int32Ty, 0));
00241     values.push_back(getSize(type));
00242     values.push_back(getAlignment(type));
00243     values.push_back(ConstantInt::get(Type::Int32Ty, 0/*offsetInBits*/));
00244     values.push_back(ConstantInt::get(Type::Int32Ty, dwarf::DW_ATE_float));
00245 
00246     Constant * structVal = ConstantStruct::get(values, false);
00247     return new GlobalVariable(structVal->getType(), true,
00248         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
00249 }
00250 
00251 GlobalVariable * DebugInfoBuilder::createPointerTypeDescriptor(
00252     std::string & name,
00253     GlobalVariable * referenceType,
00254     const PointerType * type,
00255     unsigned line) {
00256 
00257     std::vector<Constant *> values;
00258     values.push_back(ConstantInt::get(
00259         Type::Int32Ty, dwarf::DW_TAG_pointer_type + LLVMDebugVersion));
00260     values.push_back(
00261         context ? ConstantExpr::getBitCast(context, anyPtrType) : NULL);
00262     values.push_back(ConstantArray::get(name));
00263     values.push_back(
00264         compileUnit ? ConstantExpr::getBitCast(compileUnit, anyPtrType) : NULL);
00265     values.push_back(ConstantInt::get(Type::Int32Ty, line));
00266     values.push_back(getSize(type));
00267     values.push_back(getAlignment(type));
00268     values.push_back(ConstantInt::get(Type::Int32Ty, 0));
00269     values.push_back(referenceType);
00270 
00271     Constant * structVal = ConstantStruct::get(values, false);
00272     return new GlobalVariable(structVal->getType(), true,
00273         GlobalValue::InternalLinkage, structVal, DERIVEDTYPE_NAME, module);
00274 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.