Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

Changeset 946:1714836f2c0b

Show
Ignore:
Timestamp:
02/08/09 10:50:22 (3 years ago)
Author:
Christian Kamm <kamm incasoftware de>
branch:
default
Message:

Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h.
Add getCompilationModule to Dsymbol and fix template compile unit decision code.
Runtime compiles with -g again.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • dmd/dsymbol.c

    r626 r946  
    550550    if (m) 
    551551        return m; 
     552    s = s->parent; 
     553    } 
     554    return NULL; 
     555} 
     556 
     557 
     558/********************************** 
     559 * Determine which Module a Dsymbol will be compiled in. 
     560 * This may be different from getModule for templates. 
     561 */ 
     562 
     563Module *Dsymbol::getCompilationModule() 
     564{ 
     565    Module *m; 
     566    TemplateInstance *ti; 
     567    Dsymbol *s; 
     568 
     569    //printf("Dsymbol::getModule()\n"); 
     570    s = this; 
     571    while (s) 
     572    { 
     573    //printf("\ts = '%s'\n", s->toChars()); 
     574    m = s->isModule(); 
     575    if (m) 
     576        return m; 
     577    ti = s->isTemplateInstance(); 
     578    if (ti && ti->tmodule) 
     579        return ti->tmodule; 
    552580    s = s->parent; 
    553581    } 
  • dmd/dsymbol.h

    r913 r946  
    120120    void error(const char *format, ...); 
    121121    void checkDeprecated(Loc loc, Scope *sc); 
    122     Module *getModule(); 
     122    Module *getModule();        // module where declared 
     123    Module *getCompilationModule(); // possibly different for templates 
    123124    Dsymbol *pastMixin(); 
    124125    Dsymbol *toParent(); 
  • dmd/template.c

    r940 r946  
    29832983    // get the enclosing template instance from the scope tinst 
    29842984    tinst = sc->tinst; 
    2985     tmodule = sc->module; 
     2985 
     2986    // get the module of the outermost enclosing instantiation 
     2987    if (tinst) 
     2988    tmodule = tinst->tmodule; 
     2989    else 
     2990    tmodule = sc->module; 
    29862991 
    29872992#if LOG 
  • gen/functions.cpp

    r945 r946  
    648648    if (global.params.symdebug) { 
    649649        Module* mo = fd->getModule(); 
    650         fd->ir.irFunc->dwarfSubProg = DtoDwarfSubProgram(fd); 
     650        fd->ir.irFunc->diSubprogram = DtoDwarfSubProgram(fd); 
    651651    } 
    652652 
  • gen/irstate.cpp

    r493 r946  
    4848 
    4949////////////////////////////////////////////////////////////////////////////////////////// 
    50 IRState::IRState() 
     50IRState::IRState(llvm::Module* m) 
     51    : module(m), difactory(*m) 
    5152{ 
    5253    interfaceInfoType = NULL; 
     
    5556 
    5657    dmodule = 0; 
    57     module = 0; 
    5858    emitMain = false; 
    5959    mainFunc = 0; 
  • gen/irstate.h

    r945 r946  
    132132struct IRState 
    133133{ 
    134     IRState(); 
     134    IRState(llvm::Module* m); 
    135135 
    136136    // module 
     
    191191    IRBuilderHelper ir; 
    192192 
     193    // debug info helper 
     194    llvm::DIFactory difactory; 
     195 
    193196    typedef std::list<Dsymbol*> DsymbolList; 
    194197    // dsymbols that need to be resolved 
  • gen/llvm.h

    r653 r946  
    1414 
    1515#include "llvm/Target/TargetData.h" 
     16 
     17#include "llvm/Analysis/DebugInfo.h" 
    1618 
    1719#include "llvm/Support/IRBuilder.h" 
  • gen/llvmhelpers.cpp

    r945 r946  
    975975        if (global.params.symdebug) 
    976976        { 
    977             LLGlobalVariable* gv = DtoDwarfGlobalVariable(gvar, vd)
     977            LLGlobalVariable* gv = DtoDwarfGlobalVariable(gvar, vd).getGV()
    978978            // keep a reference so GDCE doesn't delete it ! 
    979979            gIR->usedArray.push_back(llvm::ConstantExpr::getBitCast(gv, getVoidPtrType())); 
     
    15581558bool mustDefineSymbol(Dsymbol* s) 
    15591559{ 
    1560 #if 1 
     1560#if 0 
    15611561    return s->getModule() == gIR->dmodule || DtoIsTemplateInstance(s) != NULL; 
    15621562#else 
     
    15741574bool needsTemplateLinkage(Dsymbol* s) 
    15751575{ 
    1576 #if 1 
     1576#if 0 
    15771577    return DtoIsTemplateInstance(s) != NULL; 
    15781578#else 
  • gen/todebug.cpp

    r904 r946  
    2424 
    2525////////////////////////////////////////////////////////////////////////////////////////////////// 
    26  
    27 /** 
    28  * Emits a global variable, LLVM Dwarf style. 
    29  * @param type Type of variable. 
    30  * @param values Initializers. 
    31  * @param name Name. 
    32  * @return The global variable. 
    33  */ 
    34 static LLGlobalVariable* emitDwarfGlobal(const LLStructType* type, const std::vector<LLConstant*> values, const char* name, bool linkonce=false) 
    35 { 
    36     LLConstant* c = llvm::ConstantStruct::get(type, values); 
    37     LLGlobalValue::LinkageTypes linkage = linkonce ? LLGlobalValue::LinkOnceLinkage : LLGlobalValue::InternalLinkage; 
    38     LLGlobalVariable* gv = new LLGlobalVariable(type, true, linkage, c, name, gIR->module); 
    39     gv->setSection("llvm.metadata"); 
    40     return gv; 
    41 } 
    4226 
    4327/** 
     
    5741////////////////////////////////////////////////////////////////////////////////////////////////// 
    5842 
    59 /** 
    60  * Emits the Dwarf anchors that are used repeatedly by LLVM debug info. 
    61  */ 
    62 static void emitDwarfAnchors() 
    63 
    64     const llvm::StructType* anchorTy = isaStruct(gIR->module->getTypeByName("llvm.dbg.anchor.type")); 
    65     std::vector<LLConstant*> vals(2); 
    66  
    67     vals[0] = DtoConstUint(llvm::LLVMDebugVersion); 
    68     vals[1] = DtoConstUint(DW_TAG_compile_unit); 
    69     gIR->dwarfCUs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.compile_units", true); 
    70  
    71     vals[0] = DtoConstUint(llvm::LLVMDebugVersion); 
    72     vals[1] = DtoConstUint(DW_TAG_variable); 
    73     gIR->dwarfGVs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.global_variables", true); 
    74  
    75     vals[0] = DtoConstUint(llvm::LLVMDebugVersion); 
    76     vals[1] = DtoConstUint(DW_TAG_subprogram); 
    77     gIR->dwarfSPs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.subprograms", true); 
    78 
    79  
    80 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    81  
    82 static LLConstant* getDwarfAnchor(dwarf_constants c) 
    83 
    84     if (!gIR->dwarfCUs) 
    85         emitDwarfAnchors(); 
     43static llvm::DIAnchor getDwarfAnchor(dwarf_constants c) 
     44
    8645    switch (c) 
    8746    { 
    8847    case DW_TAG_compile_unit: 
    89         return gIR->dwarfCUs
     48        return gIR->difactory.GetOrCreateCompileUnitAnchor()
    9049    case DW_TAG_variable: 
    91         return gIR->dwarfGVs
     50        return gIR->difactory.GetOrCreateGlobalVariableAnchor()
    9251    case DW_TAG_subprogram: 
    93         return gIR->dwarfSPs
     52        return gIR->difactory.GetOrCreateSubprogramAnchor()
    9453    default: 
    9554        assert(0); 
     
    12786} 
    12887 
    129 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    130  
    131 static LLGlobalVariable* dwarfCompileUnit(Module* m) 
    132 
    133     std::vector<LLConstant*> vals(6); 
    134     vals[0] = DBG_TAG(DW_TAG_compile_unit); 
    135     vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_compile_unit)); 
    136  
    137     if (global.params.symdebug == 2) 
    138         vals[2] = DtoConstUint(DW_LANG_C); 
    139     else 
    140         vals[2] = DtoConstUint(DW_LANG_D); 
    141     vals[3] = DtoConstStringPtr(FileName::name(m->srcfile->name->toChars()), "llvm.metadata"); 
    142     std::string srcpath(FileName::path(m->srcfile->name->toChars())); 
    143     if (!FileName::absolute(srcpath.c_str())) { 
    144         llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory(); 
    145         tmp.appendComponent(srcpath); 
    146         srcpath = tmp.toString(); 
    147         if (!srcpath.empty() && *srcpath.rbegin() != '/' && *srcpath.rbegin() != '\\') 
    148             srcpath = srcpath + '/'; 
    149     } 
    150     vals[4] = DtoConstStringPtr(srcpath.c_str(), "llvm.metadata"); 
    151     vals[5] = DtoConstStringPtr("LDC (http://www.dsource.org/projects/ldc)", "llvm.metadata"); 
    152  
    153     LLGlobalVariable* gv = emitDwarfGlobal(getDwarfCompileUnitType(), vals, "llvm.dbg.compile_unit"); 
    154     m->ir.irModule->dwarfCompileUnit = gv; 
    155     return gv; 
    156 
    157  
    158 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    159  
    160 static LLGlobalVariable* dwarfSubProgram(llvm::GlobalVariable* emitUnit, llvm::GlobalVariable* defineUnit, const char* prettyname, const char* mangledname, unsigned int linenum, bool isprivate) 
    161 
    162     std::vector<LLConstant*> vals(11); 
    163     vals[0] = DBG_TAG(DW_TAG_subprogram); 
    164     vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_subprogram)); 
    165  
    166     vals[2] = DBG_CAST(emitUnit); 
    167     vals[3] = DtoConstStringPtr(prettyname, "llvm.metadata"); 
    168     vals[4] = vals[3]; 
    169     vals[5] = DtoConstStringPtr(mangledname, "llvm.metadata"); 
    170     vals[6] = DBG_CAST(defineUnit); 
    171     vals[7] = DtoConstUint(linenum); 
    172     vals[8] = DBG_NULL; 
    173     vals[9] = DtoConstBool(isprivate); 
    174     vals[10] = DtoConstBool(emitUnit == defineUnit); 
    175  
    176     Logger::println("emitting subprogram global"); 
    177  
    178     return emitDwarfGlobal(getDwarfSubProgramType(), vals, "llvm.dbg.subprogram"); 
    179 
    180  
    181 /* 
    182 static LLGlobalVariable* dwarfSubProgram(FuncDeclaration* fd, llvm::GlobalVariable* compileUnit) 
    183 
    184     std::vector<LLConstant*> vals(11); 
    185     vals[0] = DBG_TAG(DW_TAG_subprogram); 
    186     vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_subprogram)); 
    187  
    188     vals[2] = DBG_CAST(compileUnit); 
    189     vals[3] = DtoConstStringPtr(fd->toPrettyChars(), "llvm.metadata"); 
    190     vals[4] = vals[3]; 
    191     vals[5] = DtoConstStringPtr(fd->mangle(), "llvm.metadata"); 
    192     vals[6] = DBG_CAST( DtoDwarfCompileUnit(fd->getModule()) ); 
    193     vals[7] = DtoConstUint(fd->loc.linnum); 
    194     vals[8] = DBG_NULL; 
    195     vals[9] = DtoConstBool(fd->protection == PROTprivate); 
    196     vals[10] = DtoConstBool(fd->getModule() == gIR->dmodule); 
    197  
    198     Logger::println("emitting subprogram global"); 
    199  
    200     return emitDwarfGlobal(getDwarfSubProgramType(), vals, "llvm.dbg.subprogram"); 
    201 }*/ 
    202  
    203 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    204  
    205 static LLGlobalVariable* dwarfTypeDescription_impl(Type* type, LLGlobalVariable* cu, const char* c_name); 
    206 static LLGlobalVariable* dwarfTypeDescription(Type* type, LLGlobalVariable* cu, const char* c_name); 
    207  
    208 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    209  
    210 static LLGlobalVariable* dwarfBasicType(Type* type, llvm::GlobalVariable* compileUnit) 
     88 
     89////////////////////////////////////////////////////////////////////////////////////////////////// 
     90 
     91static llvm::DIType dwarfTypeDescription_impl(Type* type, llvm::DICompileUnit cu, const char* c_name); 
     92static llvm::DIType dwarfTypeDescription(Type* type, llvm::DICompileUnit cu, const char* c_name); 
     93 
     94////////////////////////////////////////////////////////////////////////////////////////////////// 
     95 
     96static llvm::DIBasicType dwarfBasicType(Type* type, llvm::DICompileUnit compileUnit) 
    21197{ 
    21298    Type* t = type->toBasetype(); 
    213  
    21499    const LLType* T = DtoType(type); 
    215100 
    216     std::vector<LLConstant*> vals(10); 
    217  
    218     // tag 
    219     vals[0] = DBG_TAG(DW_TAG_base_type); 
    220  
    221     // context 
    222     vals[1] = DBG_CAST(compileUnit); 
    223  
    224     // name 
    225     vals[2] = DtoConstStringPtr(type->toChars(), "llvm.metadata"); 
    226  
    227     // compile unit where defined 
    228     vals[3] = DBG_NULL; 
    229  
    230     // line number where defined 
    231     vals[4] = DtoConstInt(0); 
    232  
    233     // size in bits 
    234     vals[5] = LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false); 
    235  
    236     // alignment in bits 
    237     vals[6] = LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false); 
    238  
    239     // offset in bits 
    240     vals[7] = LLConstantInt::get(LLType::Int64Ty, 0, false); 
    241  
    242     // FIXME: dont know what this is 
    243     vals[8] = DtoConstUint(0); 
    244  
    245     // dwarf type 
     101    // find encoding 
    246102    unsigned id; 
    247103    if (t->isintegral()) 
     
    260116        assert(0 && "unsupported basictype for debug info"); 
    261117    } 
    262     vals[9] = DtoConstUint(id); 
    263  
    264     return emitDwarfGlobal(getDwarfBasicTypeType(), vals, "llvm.dbg.basictype"); 
    265 
    266  
    267 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    268  
    269 static LLGlobalVariable* dwarfDerivedType(Type* type, llvm::GlobalVariable* compileUnit) 
     118 
     119    return gIR->difactory.CreateBasicType( 
     120        compileUnit, // context 
     121        type->toChars(), // name 
     122        llvm::DICompileUnit(NULL), // compile unit 
     123        0, // line number 
     124        getTypeBitSize(T), // size (bits) 
     125        getABITypeAlign(T)*8, // align (bits) 
     126        0, // offset (bits) 
     127//FIXME: need flags? 
     128        0, // flags 
     129        id // encoding 
     130    ); 
     131
     132 
     133////////////////////////////////////////////////////////////////////////////////////////////////// 
     134 
     135static llvm::DIDerivedType dwarfDerivedType(Type* type, llvm::DICompileUnit compileUnit) 
    270136{ 
    271137    const LLType* T = DtoType(type); 
    272138    Type* t = type->toBasetype(); 
    273139 
    274     // defaults 
    275     LLConstant* name = getNullPtr(getVoidPtrType()); 
    276  
    277     // find tag 
    278     unsigned tag; 
    279     if (t->ty == Tpointer) 
    280     { 
    281         tag = DW_TAG_pointer_type; 
    282     } 
    283     else 
    284     { 
    285         assert(0 && "unsupported derivedtype for debug info"); 
    286     } 
    287  
    288     std::vector<LLConstant*> vals(10); 
    289  
    290     // tag 
    291     vals[0] = DBG_TAG(tag); 
    292  
    293     // context 
    294     vals[1] = DBG_CAST(compileUnit); 
    295  
    296     // name 
    297     vals[2] = name; 
    298  
    299     // compile unit where defined 
    300     vals[3] = DBG_NULL; 
    301  
    302     // line number where defined 
    303     vals[4] = DtoConstInt(0); 
    304  
    305     // size in bits 
    306     vals[5] = LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false); 
    307  
    308     // alignment in bits 
    309     vals[6] = LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false); 
    310  
    311     // offset in bits 
    312     vals[7] = LLConstantInt::get(LLType::Int64Ty, 0, false); 
    313  
    314     // FIXME: dont know what this is 
    315     vals[8] = DtoConstUint(0); 
    316  
    317     // base type 
     140    assert(t->ty == Tpointer && "unsupported derivedtype for debug info, only pointers allowed"); 
     141 
     142    // find base type 
     143    llvm::DIType basetype; 
    318144    Type* nt = t->nextOf(); 
    319     LLGlobalVariable* nTD = dwarfTypeDescription_impl(nt, compileUnit, NULL); 
    320     if (nt->ty == Tvoid || !nTD) 
    321         vals[9] = DBG_NULL; 
    322     else 
    323         vals[9] = DBG_CAST(nTD); 
    324  
    325     return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype"); 
    326 
    327  
    328 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    329  
    330 static LLGlobalVariable* dwarfMemberType(unsigned linnum, Type* type, LLGlobalVariable* compileUnit, LLGlobalVariable* definedCU, const char* c_name, unsigned offset) 
     145    basetype = dwarfTypeDescription_impl(nt, compileUnit, NULL); 
     146    if (nt->ty == Tvoid) 
     147        basetype = llvm::DIType(NULL); 
     148 
     149    return gIR->difactory.CreateDerivedType( 
     150        DW_TAG_pointer_type, // tag 
     151        compileUnit, // context 
     152        "", // name 
     153        llvm::DICompileUnit(NULL), // compile unit 
     154        0, // line number 
     155        getTypeBitSize(T), // size (bits) 
     156        getABITypeAlign(T)*8, // align (bits) 
     157        0, // offset (bits) 
     158//FIXME: need flags? 
     159        0, // flags 
     160        basetype // derived from 
     161    ); 
     162
     163 
     164////////////////////////////////////////////////////////////////////////////////////////////////// 
     165 
     166static llvm::DIDerivedType dwarfMemberType(unsigned linnum, Type* type, llvm::DICompileUnit compileUnit, llvm::DICompileUnit definedCU, const char* c_name, unsigned offset) 
    331167{ 
    332168    const LLType* T = DtoType(type); 
    333169    Type* t = type->toBasetype(); 
    334170 
    335     // defaults 
    336     LLConstant* name; 
    337     if (c_name) 
    338         name = DtoConstStringPtr(c_name, "llvm.metadata"); 
    339     else 
    340         name = getNullPtr(getVoidPtrType()); 
    341  
    342     std::vector<LLConstant*> vals(10); 
    343  
    344     // tag 
    345     vals[0] = DBG_TAG(DW_TAG_member); 
    346  
    347     // context 
    348     vals[1] = DBG_CAST(compileUnit); 
    349  
    350     // name 
    351     vals[2] = name; 
    352  
    353     // compile unit where defined 
    354     if (definedCU) 
    355         vals[3] = DBG_CAST(definedCU); 
    356     else 
    357         vals[3] = DBG_NULL; 
    358  
    359     // line number where defined 
    360     vals[4] = DtoConstInt(linnum); 
    361  
    362     // size in bits 
    363     vals[5] = LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false); 
    364  
    365     // alignment in bits 
    366     vals[6] = LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false); 
    367  
    368     // offset in bits 
    369     vals[7] = LLConstantInt::get(LLType::Int64Ty, offset*8, false); 
    370  
    371     // FIXME: dont know what this is 
    372     vals[8] = DtoConstUint(0); 
    373  
    374     // base type 
    375     LLGlobalVariable* nTD = dwarfTypeDescription(t, compileUnit, NULL); 
    376     if (t->ty == Tvoid || !nTD) 
    377         vals[9] = DBG_NULL; 
    378     else 
    379         vals[9] = DBG_CAST(nTD); 
    380  
    381     return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype"); 
    382 
    383  
    384 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    385  
    386 static LLGlobalVariable* dwarfCompositeType(Type* type, llvm::GlobalVariable* compileUnit) 
     171    // find base type 
     172    llvm::DIType basetype; 
     173    basetype = dwarfTypeDescription(t, compileUnit, NULL); 
     174    if (t->ty == Tvoid) 
     175        basetype = llvm::DIType(NULL); 
     176 
     177    return gIR->difactory.CreateDerivedType( 
     178        DW_TAG_member, // tag 
     179        compileUnit, // context 
     180        c_name, // name 
     181        definedCU, // compile unit 
     182        linnum, // line number 
     183        getTypeBitSize(T), // size (bits) 
     184        getABITypeAlign(T)*8, // align (bits) 
     185        offset*8, // offset (bits) 
     186//FIXME: need flags? 
     187        0, // flags 
     188        basetype // derived from 
     189    ); 
     190
     191 
     192////////////////////////////////////////////////////////////////////////////////////////////////// 
     193 
     194//FIXME: This does not use llvm's DIFactory as it can't  
     195//   handle recursive types properly. 
     196static llvm::DICompositeType dwarfCompositeType(Type* type, llvm::DICompileUnit compileUnit) 
    387197{ 
    388198    const LLType* T = DtoType(type); 
     
    393203    LLGlobalVariable* members = NULL; 
    394204    unsigned linnum = 0; 
    395     LLGlobalVariable* definedCU = NULL
     205    llvm::DICompileUnit definedCU
    396206 
    397207    // prepare tag and members 
     
    406216        tag = DW_TAG_structure_type; 
    407217 
    408         LLGlobalVariable* len = dwarfMemberType(0, Type::tsize_t, compileUnit, NULL, "length", 0); 
     218        LLGlobalVariable* len = dwarfMemberType(0, Type::tsize_t, compileUnit, llvm::DICompileUnit(NULL), "length", 0).getGV(); 
    409219        assert(len); 
    410         LLGlobalVariable* ptr = dwarfMemberType(0, t->nextOf()->pointerTo(), compileUnit, NULL, "ptr", global.params.is64bit?8:4); 
     220        LLGlobalVariable* ptr = dwarfMemberType(0, t->nextOf()->pointerTo(), compileUnit, llvm::DICompileUnit(NULL), "ptr", global.params.is64bit?8:4).getGV(); 
    411221        assert(ptr); 
    412222 
     
    443253        // to provide debug info. probably a forward-declared struct? 
    444254        if (sd->sizeok == 0) 
    445             return NULL
     255            return llvm::DICompositeType(NULL)
    446256 
    447257        IrStruct* ir = sd->ir.irStruct; 
    448258        assert(ir); 
    449         if (ir->dwarfComposite
    450             return ir->dwarfComposite; 
     259        if (!ir->diCompositeType.isNull()
     260            return ir->diCompositeType; 
    451261 
    452262        // set to handle recursive types properly 
    453263        gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype"); 
    454         ir->dwarfComposite = gv; 
     264        // set bogus initializer to satisfy asserts in DICompositeType constructor 
     265        gv->setInitializer(LLConstant::getNullValue(getDwarfCompositeTypeType())); 
     266        ir->diCompositeType = llvm::DICompositeType(gv); 
    455267 
    456268        tag = DW_TAG_structure_type; 
     
    458270        name = DtoConstStringPtr(sd->toChars(), "llvm.metadata"); 
    459271        linnum = sd->loc.linnum; 
    460         definedCU = DtoDwarfCompileUnit(sd->getModule()); 
     272        definedCU = DtoDwarfCompileUnit(sd->getCompilationModule()); 
    461273 
    462274        std::vector<LLConstant*> elems; 
     
    471283                assert(vd); 
    472284 
    473                 LLGlobalVariable* ptr = dwarfMemberType(vd->loc.linnum, vd->type, compileUnit, definedCU, vd->toChars(), vd->offset)
     285                LLGlobalVariable* ptr = dwarfMemberType(vd->loc.linnum, vd->type, compileUnit, definedCU, vd->toChars(), vd->offset).getGV()
    474286                elems.push_back(DBG_CAST(ptr)); 
    475287            } 
     
    494306 
    495307    // context 
    496     vals[1] = DBG_CAST(compileUnit); 
     308    vals[1] = DBG_CAST(compileUnit.getGV()); 
    497309 
    498310    // name 
     
    500312 
    501313    // compile unit where defined 
    502     if (definedCU
    503         vals[3] = DBG_CAST(definedCU); 
     314    if (definedCU.getGV()
     315        vals[3] = DBG_CAST(definedCU.getGV()); 
    504316    else 
    505317        vals[3] = DBG_NULL; 
     
    535347    gv->setInitializer(initia); 
    536348 
    537     return gv
    538 } 
    539  
    540 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    541  
    542 static LLGlobalVariable* dwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 
     349    return llvm::DICompositeType(gv)
     350} 
     351 
     352////////////////////////////////////////////////////////////////////////////////////////////////// 
     353 
     354static llvm::DIGlobalVariable dwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 
    543355{ 
    544356    assert(vd->isDataseg()); 
    545     LLGlobalVariable* compileUnit = DtoDwarfCompileUnit(gIR->dmodule); 
    546  
    547     std::vector<LLConstant*> vals(12); 
    548     vals[0] = DBG_TAG(DW_TAG_variable); 
    549     vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_variable)); 
    550  
    551     vals[2] = DBG_CAST(compileUnit); 
    552  
    553     vals[3] = DtoConstStringPtr(vd->mangle(), "llvm.metadata"); 
    554     vals[4] = DtoConstStringPtr(vd->toPrettyChars(), "llvm.metadata"); 
    555     vals[5] = DtoConstStringPtr(vd->toChars(), "llvm.metadata"); 
    556  
    557     vals[6] = DBG_CAST(DtoDwarfCompileUnit(vd->getModule())); 
    558     vals[7] = DtoConstUint(vd->loc.linnum); 
    559  
    560     LLGlobalVariable* TY = dwarfTypeDescription_impl(vd->type, compileUnit, NULL); 
    561     vals[8] = TY ? DBG_CAST(TY) : DBG_NULL; 
    562     vals[9] = DtoConstBool(vd->protection == PROTprivate); 
    563     vals[10] = DtoConstBool(vd->getModule() == gIR->dmodule); 
    564  
    565     vals[11] = DBG_CAST(ll); 
    566  
    567     return emitDwarfGlobal(getDwarfGlobalVariableType(), vals, "llvm.dbg.global_variable"); 
    568 
    569  
    570 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    571  
    572 static LLGlobalVariable* dwarfVariable(VarDeclaration* vd, LLGlobalVariable* typeDescr) 
     357    llvm::DICompileUnit compileUnit = DtoDwarfCompileUnit(gIR->dmodule); 
     358 
     359    return gIR->difactory.CreateGlobalVariable( 
     360        compileUnit, // context 
     361        vd->mangle(), // name 
     362        vd->toPrettyChars(), // displayname 
     363        vd->toChars(), // linkage name 
     364        DtoDwarfCompileUnit(vd->getCompilationModule()), // compile unit 
     365        vd->loc.linnum, // line num 
     366        dwarfTypeDescription_impl(vd->type, compileUnit, NULL), // type 
     367        vd->protection == PROTprivate, // is local to unit 
     368        vd->getCompilationModule() == gIR->dmodule, // is definition 
     369        ll // value 
     370    ); 
     371
     372 
     373////////////////////////////////////////////////////////////////////////////////////////////////// 
     374 
     375static llvm::DIVariable dwarfVariable(VarDeclaration* vd, llvm::DIType type) 
    573376{ 
    574377    assert(!vd->isDataseg() && "static variable"); 
     
    580383        tag = DW_TAG_auto_variable; 
    581384 
    582     std::vector<LLConstant*> vals(6); 
    583     // tag 
    584     vals[0] = DBG_TAG(tag); 
    585     // context 
    586     vals[1] = DBG_CAST(gIR->func()->dwarfSubProg); 
    587     // name 
    588     vals[2] = DtoConstStringPtr(vd->toChars(), "llvm.metadata"); 
    589     // compile unit where defined 
    590     vals[3] = DBG_CAST(DtoDwarfCompileUnit(vd->getModule())); 
    591     // line number where defined 
    592     vals[4] = DtoConstUint(vd->loc.linnum); 
    593     // type descriptor 
    594     vals[5] = DBG_CAST(typeDescr); 
    595  
    596     return emitDwarfGlobal(getDwarfVariableType(), vals, "llvm.dbg.variable"); 
    597 
    598  
    599 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    600  
    601 static void dwarfDeclare(LLValue* var, LLGlobalVariable* varDescr) 
    602 
    603     LLSmallVector<LLValue*,2> args(2); 
    604     args[0] = DtoBitCast(var, DBG_TYPE); 
    605     args[1] = DBG_CAST(varDescr); 
    606     gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.declare"), args.begin(), args.end()); 
    607 
    608  
    609 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    610  
    611 static LLGlobalVariable* dwarfTypeDescription_impl(Type* type, LLGlobalVariable* cu, const char* c_name) 
     385    return gIR->difactory.CreateVariable( 
     386        tag, // tag 
     387        gIR->func()->diSubprogram, // context 
     388        vd->toChars(), // name 
     389        DtoDwarfCompileUnit(vd->getCompilationModule()), // compile unit 
     390        vd->loc.linnum, // line num 
     391        type // type 
     392    ); 
     393
     394 
     395////////////////////////////////////////////////////////////////////////////////////////////////// 
     396 
     397static void dwarfDeclare(LLValue* var, llvm::DIVariable divar) 
     398
     399    gIR->difactory.InsertDeclare(var, divar, gIR->scopebb()); 
     400
     401 
     402////////////////////////////////////////////////////////////////////////////////////////////////// 
     403 
     404static llvm::DIType dwarfTypeDescription_impl(Type* type, llvm::DICompileUnit cu, const char* c_name) 
    612405{ 
    613406    Type* t = type->toBasetype(); 
    614407    if (t->ty == Tvoid) 
    615         return NULL
     408        return llvm::DIType(NULL)
    616409    else if (t->isintegral() || t->isfloating()) 
    617410        return dwarfBasicType(type, cu); 
     
    621414        return dwarfCompositeType(type, cu); 
    622415 
    623     return NULL
    624 } 
    625  
    626 static LLGlobalVariable* dwarfTypeDescription(Type* type, LLGlobalVariable* cu, const char* c_name) 
     416    return llvm::DIType(NULL)
     417} 
     418 
     419static llvm::DIType dwarfTypeDescription(Type* type, llvm::DICompileUnit cu, const char* c_name) 
    627420{ 
    628421    Type* t = type->toBasetype(); 
     
    641434 
    642435    // get compile units 
    643     LLGlobalVariable* thisCU = DtoDwarfCompileUnit(gIR->dmodule); 
    644     LLGlobalVariable* varCU = thisCU; 
    645     if (vd->getModule() != gIR->dmodule) 
    646         varCU = DtoDwarfCompileUnit(vd->getModule()); 
     436    llvm::DICompileUnit thisCU = DtoDwarfCompileUnit(gIR->dmodule); 
     437    llvm::DICompileUnit varCU = thisCU; 
     438    if (vd->getCompilationModule() != gIR->dmodule) 
     439        varCU = DtoDwarfCompileUnit(vd->getCompilationModule()); 
    647440 
    648441    // get type description 
    649     Type* t = vd->type->toBasetype(); 
    650     LLGlobalVariable* TD = dwarfTypeDescription(vd->type, thisCU, NULL); 
    651     if (TD == NULL) 
     442    llvm::DIType TD = dwarfTypeDescription(vd->type, thisCU, NULL); 
     443    if (TD.isNull()) 
    652444        return; // unsupported 
    653445 
    654446    // get variable description 
    655     LLGlobalVariable* VD; 
    656     VD = dwarfVariable(vd, TD); 
     447    llvm::DIVariable VD = dwarfVariable(vd, TD); 
    657448 
    658449    // declare 
     
    662453////////////////////////////////////////////////////////////////////////////////////////////////// 
    663454 
    664 LLGlobalVariable* DtoDwarfCompileUnit(Module* m) 
     455llvm::DICompileUnit DtoDwarfCompileUnit(Module* m) 
    665456{ 
    666457    Logger::println("D to dwarf compile_unit"); 
     
    670461    if (!m->ir.irModule) 
    671462        m->ir.irModule = new IrModule(m, m->srcfile->toChars()); 
    672     else if (m->ir.irModule->dwarfCompileUnit) 
    673     { 
    674         if (m->ir.irModule->dwarfCompileUnit->getParent() == gIR->module) 
    675             return m->ir.irModule->dwarfCompileUnit; 
    676     } 
    677  
    678     LLGlobalVariable* gv = dwarfCompileUnit(m); 
    679     m->ir.irModule->dwarfCompileUnit = gv; 
    680     return gv; 
    681 
    682  
    683 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    684  
    685 LLGlobalVariable* DtoDwarfSubProgram(FuncDeclaration* fd) 
     463    else if (!m->ir.irModule->diCompileUnit.isNull()) 
     464    { 
     465        assert (m->ir.irModule->diCompileUnit.getGV()->getParent() == gIR->module 
     466            && "debug info compile unit belongs to incorrect llvm module!"); 
     467        return m->ir.irModule->diCompileUnit; 
     468    } 
     469 
     470    // prepare srcpath 
     471    std::string srcpath(FileName::path(m->srcfile->name->toChars())); 
     472    if (!FileName::absolute(srcpath.c_str())) { 
     473        llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory(); 
     474        tmp.appendComponent(srcpath); 
     475        srcpath = tmp.toString(); 
     476        if (!srcpath.empty() && *srcpath.rbegin() != '/' && *srcpath.rbegin() != '\\') 
     477            srcpath = srcpath + '/'; 
     478    } 
     479 
     480    // make compile unit 
     481    m->ir.irModule->diCompileUnit = gIR->difactory.CreateCompileUnit( 
     482        global.params.symdebug == 2 ? DW_LANG_C : DW_LANG_D, 
     483        m->srcfile->name->toChars(), 
     484        srcpath, 
     485        "LDC (http://www.dsource.org/projects/ldc)", 
     486//FIXME: What do these two mean? 
     487        false, // isMain, 
     488        false // isOptimized 
     489    ); 
     490 
     491    return m->ir.irModule->diCompileUnit; 
     492
     493 
     494////////////////////////////////////////////////////////////////////////////////////////////////// 
     495 
     496llvm::DISubprogram DtoDwarfSubProgram(FuncDeclaration* fd) 
    686497{ 
    687498    Logger::println("D to dwarf subprogram"); 
    688499    LOG_SCOPE; 
    689500 
     501    llvm::DICompileUnit context = DtoDwarfCompileUnit(gIR->dmodule); 
     502    llvm::DICompileUnit definition = DtoDwarfCompileUnit(fd->getCompilationModule()); 
     503 
    690504    // FIXME: duplicates ? 
    691     return dwarfSubProgram( 
    692         DtoDwarfCompileUnit(gIR->dmodule), 
    693         DtoDwarfCompileUnit(fd->getModule()), 
    694         fd->toPrettyChars(), fd->mangle(), 
    695         fd->loc.linnum, 
    696         fd->protection == PROTprivate); 
    697 
    698  
    699 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    700  
    701 LLGlobalVariable* DtoDwarfSubProgramInternal(const char* prettyname, const char* mangledname) 
     505    return gIR->difactory.CreateSubprogram( 
     506        context, // context 
     507        fd->toPrettyChars(), // name 
     508        fd->toPrettyChars(), // display name 
     509        fd->mangle(), // linkage name 
     510        definition, // compile unit 
     511        fd->loc.linnum, // line no 
     512//FIXME: what's this type for? 
     513        llvm::DIType(NULL), // type 
     514        fd->protection == PROTprivate, // is local to unit 
     515        context.getGV() == definition.getGV() // isdefinition 
     516    ); 
     517
     518 
     519////////////////////////////////////////////////////////////////////////////////////////////////// 
     520 
     521llvm::DISubprogram DtoDwarfSubProgramInternal(const char* prettyname, const char* mangledname) 
    702522{ 
    703523    Logger::println("D to dwarf subprogram"); 
    704524    LOG_SCOPE; 
    705525 
     526    llvm::DICompileUnit context = DtoDwarfCompileUnit(gIR->dmodule); 
     527 
    706528    // FIXME: duplicates ? 
    707     return dwarfSubProgram( 
    708         DtoDwarfCompileUnit(gIR->dmodule), 
    709         DtoDwarfCompileUnit(gIR->dmodule), 
    710         prettyname, mangledname, 
    711         0, 
    712         true); 
    713 
    714  
    715 ////////////////////////////////////////////////////////////////////////////////////////////////// 
    716  
    717 LLGlobalVariable* DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 
     529    return gIR->difactory.CreateSubprogram( 
     530        context, // context 
     531        prettyname, // name 
     532        prettyname, // display name 
     533        mangledname, // linkage name 
     534        context, // compile unit 
     535        0, // line no 
     536//FIXME: what's this type for? 
     537        llvm::DIType(NULL), // type 
     538        true, // is local to unit 
     539        true // isdefinition 
     540    ); 
     541
     542 
     543////////////////////////////////////////////////////////////////////////////////////////////////// 
     544 
     545llvm::DIGlobalVariable DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 
    718546{ 
    719547    Logger::println("D to dwarf global_variable"); 
     
    731559    LOG_SCOPE; 
    732560 
    733     assert(fd->ir.irFunc->dwarfSubProg); 
    734     gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(fd->ir.irFunc->dwarfSubProg)); 
     561    assert(!fd->ir.irFunc->diSubprogram.isNull()); 
     562    gIR->difactory.InsertSubprogramStart(fd->ir.irFunc->diSubprogram, gIR->scopebb()); 
    735563} 
    736564 
     
    742570    LOG_SCOPE; 
    743571 
    744     assert(fd->ir.irFunc->dwarfSubProg); 
    745     gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(fd->ir.irFunc->dwarfSubProg)); 
     572    assert(!fd->ir.irFunc->diSubprogram.isNull()); 
     573    gIR->difactory.InsertRegionEnd(fd->ir.irFunc->diSubprogram, gIR->scopebb()); 
    746574} 
    747575 
     
    753581    LOG_SCOPE; 
    754582 
    755     LLSmallVector<LLValue*,3> args(3); 
    756     args[0] = DtoConstUint(ln); 
    757     args[1] = DtoConstUint(0); 
    758     FuncDeclaration* fd = gIR->func()->decl; 
    759     args[2] = DBG_CAST(DtoDwarfCompileUnit(fd->getModule())); 
    760     gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.stoppoint"), args.begin(), args.end()); 
    761 } 
     583    gIR->difactory.InsertStopPoint( 
     584        DtoDwarfCompileUnit(gIR->func()->decl->getCompilationModule()), // compile unit 
     585        ln, // line no 
     586        0, // col no 
     587        gIR->scopebb() 
     588    ); 
     589} 
  • gen/todebug.h

    r686 r946  
    99 * @return the Dwarf compile_unit. 
    1010 */ 
    11 llvm::GlobalVariable* DtoDwarfCompileUnit(Module* m); 
     11llvm::DICompileUnit DtoDwarfCompileUnit(Module* m); 
    1212 
    1313/** 
     
    1616 * @return the Dwarf subprogram global. 
    1717 */ 
    18 llvm::GlobalVariable* DtoDwarfSubProgram(FuncDeclaration* fd); 
     18llvm::DISubprogram DtoDwarfSubProgram(FuncDeclaration* fd); 
    1919 
    2020/** 
     
    2424 * @return the Dwarf subprogram global. 
    2525 */ 
    26 llvm::GlobalVariable* DtoDwarfSubProgramInternal(const char* prettyname, const char* mangledname); 
     26llvm::DISubprogram DtoDwarfSubProgramInternal(const char* prettyname, const char* mangledname); 
    2727 
    2828void DtoDwarfFuncStart(FuncDeclaration* fd); 
     
    4444 * @return  
    4545 */ 
    46 LLGlobalVariable* DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd); 
     46llvm::DIGlobalVariable DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd); 
    4747 
    4848#endif // LDC_GEN_TODEBUG_H 
  • gen/toobj.cpp

    r945 r946  
    8383    deleteObjFile(); 
    8484 
    85     // create a new ir state 
    86     // TODO look at making the instance static and moving most functionality into IrModule where it belongs 
    87     IRState ir; 
    88     gIR = &ir; 
    89     ir.dmodule = this; 
    90  
    91     // reset all IR data stored in Dsymbols and Types 
    92     IrDsymbol::resetAll(); 
    93     IrType::resetAll(); 
    94  
    9585    // name the module 
    9686    std::string mname(toChars()); 
    9787    if (md != 0) 
    9888        mname = md->toChars(); 
    99     ir.module = new llvm::Module(mname); 
     89 
     90    // create a new ir state 
     91    // TODO look at making the instance static and moving most functionality into IrModule where it belongs 
     92    IRState ir(new llvm::Module(mname)); 
     93    gIR = &ir; 
     94    ir.dmodule = this; 
     95 
     96    // reset all IR data stored in Dsymbols and Types 
     97    IrDsymbol::resetAll(); 
     98    IrType::resetAll(); 
    10099 
    101100    // module ir state 
     
    427426    LLGlobalVariable* subprog; 
    428427    if(global.params.symdebug) { 
    429         subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str())
     428        subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV()
    430429        builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); 
    431430    } 
     
    472471    LLGlobalVariable* subprog; 
    473472    if(global.params.symdebug) { 
    474         subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str())
     473        subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV()
    475474        builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); 
    476475    } 
     
    517516    LLGlobalVariable* subprog; 
    518517    if(global.params.symdebug) { 
    519         subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str())
     518        subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV()
    520519        builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); 
    521520    } 
     
    574573    LLGlobalVariable* subprog; 
    575574    if(global.params.symdebug) { 
    576         subprog = DtoDwarfSubProgramInternal(fname.c_str(), fname.c_str())
     575        subprog = DtoDwarfSubProgramInternal(fname.c_str(), fname.c_str()).getGV()
    577576        builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); 
    578577    } 
  • ir/irfunction.cpp

    r936 r946  
    3232    _argptr = NULL; 
    3333     
    34     dwarfSubProg = NULL; 
    35  
    3634    nextUnique.push(0); 
    3735} 
  • ir/irfunction.h

    r936 r946  
    2929    llvm::Value* _argptr; 
    3030     
    31     llvm::Constant* dwarfSubProg
     31    llvm::DISubprogram diSubprogram
    3232 
    3333    // pushes a unique label scope of the given name 
  • ir/irmodule.cpp

    r683 r946  
    1111    fileName = new llvm::GlobalVariable( 
    1212        slice->getType(), true, LLGlobalValue::InternalLinkage, slice, ".modulefilename", gIR->module); 
    13  
    14     dwarfCompileUnit = NULL; 
    1513} 
    1614 
  • ir/irmodule.h

    r683 r946  
    1313    Module* M; 
    1414 
    15     LLGlobalVariable* dwarfCompileUnit; 
    1615    LLGlobalVariable* fileName; 
     16    llvm::DICompileUnit diCompileUnit; 
    1717}; 
    1818 
  • ir/irstruct.cpp

    r945 r946  
    3434    classInfoOpaque(llvm::OpaqueType::get()), 
    3535    vtblTy(llvm::OpaqueType::get()), 
    36     vtblInitTy(llvm::OpaqueType::get()) 
     36    vtblInitTy(llvm::OpaqueType::get()), 
     37    diCompositeType(NULL) 
    3738{ 
    3839    aggrdecl = aggr; 
     
    5859 
    5960    packed = false; 
    60  
    61     dwarfComposite = NULL; 
    6261} 
    6362 
  • ir/irstruct.h

    r816 r946  
    149149    bool packed; 
    150150 
    151     // dwarf composite global 
    152     LLGlobalVariable* dwarfComposite; 
     151    // composite type debug description 
     152    llvm::DICompositeType diCompositeType; 
    153153}; 
    154154 
Copyright © 2008, LDC Development Team.