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

Changeset 819:446263a8a30d

Show
Ignore:
Timestamp:
12/01/08 19:07:22 (1 month ago)
Author:
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
branch:
default
Message:

Fixed taking address of global static array element as constant expression.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gen/toir.cpp

    r809 r819  
    897897LLConstant* AddrExp::toConstElem(IRState* p) 
    898898{ 
    899     assert(e1->op == TOKvar); 
    900     VarExp* vexp = (VarExp*)e1; 
    901  
    902     if (vexp->var->needThis()) 
    903     { 
    904         error("need 'this' to access %s", vexp->var->toChars()); 
    905         fatal(); 
    906     } 
    907  
    908     // global variable 
    909     if (VarDeclaration* vd = vexp->var->isVarDeclaration()) 
    910     { 
    911         LLConstant* llc = llvm::dyn_cast<LLConstant>(vd->ir.getIrValue()); 
    912         assert(llc); 
    913         return llc; 
    914     } 
    915     // static function 
    916     else if (FuncDeclaration* fd = vexp->var->isFuncDeclaration()) 
    917     { 
    918         IrFunction* irfunc = fd->ir.irFunc; 
    919         assert(irfunc); 
    920         return irfunc->func; 
     899    // FIXME: this should probably be generalized more so we don't 
     900    // need to have a case for each thing we can take the address of 
     901 
     902    // address of global variable 
     903    if (e1->op == TOKvar) 
     904    { 
     905        VarExp* vexp = (VarExp*)e1; 
     906 
     907        // make sure 'this' isn't needed 
     908        if (vexp->var->needThis()) 
     909        { 
     910            error("need 'this' to access %s", vexp->var->toChars()); 
     911            fatal(); 
     912        } 
     913 
     914        // global variable 
     915        if (VarDeclaration* vd = vexp->var->isVarDeclaration()) 
     916        { 
     917            LLConstant* llc = llvm::dyn_cast<LLConstant>(vd->ir.getIrValue()); 
     918            assert(llc); 
     919            return llc; 
     920        } 
     921        // static function 
     922        else if (FuncDeclaration* fd = vexp->var->isFuncDeclaration()) 
     923        { 
     924            IrFunction* irfunc = fd->ir.irFunc; 
     925            assert(irfunc); 
     926            return irfunc->func; 
     927        } 
     928        // something else 
     929        else 
     930        { 
     931            // fail 
     932            goto Lerr; 
     933        } 
     934    } 
     935    // address of indexExp 
     936    else if (e1->op == TOKindex) 
     937    { 
     938        IndexExp* iexp = (IndexExp*)e1; 
     939 
     940        // indexee must be global static array var 
     941        assert(iexp->e1->op == TOKvar); 
     942        VarExp* vexp = (VarExp*)iexp->e1; 
     943        VarDeclaration* vd = vexp->var->isVarDeclaration(); 
     944        assert(vd); 
     945        assert(vd->type->toBasetype()->ty == Tsarray); 
     946        assert(vd->ir.irGlobal); 
     947 
     948        // get index 
     949        LLConstant* index = iexp->e2->toConstElem(p); 
     950        assert(index->getType() == DtoSize_t()); 
     951 
     952        // gep 
     953        LLConstant* idxs[2] = { DtoConstSize_t(0), index }; 
     954        LLConstant* gep = llvm::ConstantExpr::getGetElementPtr(isaConstant(vd->ir.irGlobal->value), idxs, 2); 
     955 
     956        // bitcast to requested type 
     957        assert(type->toBasetype()->ty == Tpointer); 
     958        return DtoBitCast(gep, DtoType(type)); 
    921959    } 
    922960    // not yet supported 
    923961    else 
    924962    { 
     963    Lerr: 
    925964        error("constant expression '%s' not yet implemented", toChars()); 
    926965        fatal(); 
Copyright © 2008, LDC Development Team.