view ir/irfunction.cpp @ 1047:6bb04dbee21f

Some calling convention work for x86-64: - Implement x86-64 extern(C), hopefully correctly. - Tried to be a bit smarter about extern(D) while I was there. Interestingly, this code seems to be generating more efficient code than gcc and llvm-gcc in some edge cases, like returning a `{ [7 x i8] }` loaded from a stack slot from an extern(C) function. (gcc generates 7 1-byte loads, while this code generates a 4-byte, a 2-byte and a 1-byte load) I also added some changes to make sure structs being returned from functions or passed in as parameters are stored in memory where the rest of the backend seems to expect them to be. These should be removed when support for first-class aggregates improves.
author Frits van Bommel <fvbommel wxs.nl>
date Fri, 06 Mar 2009 16:00:47 +0100
parents 45af482e3832
children 32ead42679d1
line wrap: on
line source


#include "gen/llvm.h"
#include "gen/tollvm.h"
#include "gen/abi.h"
#include "gen/dvalue.h"
#include "ir/irfunction.h"

#include <sstream>

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

IrFuncTyArg::IrFuncTyArg(Type* t, bool bref, unsigned a)
{
    type = t;
    ltype = bref ? DtoType(t->pointerTo()) : DtoType(t);
    attrs = a;
    byref = bref;
    rewrite = NULL;
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

llvm::Value* IrFuncTy::putRet(Type* dty, DValue* val)
{
    assert(!arg_sret);
    if (ret->rewrite)
        return ret->rewrite->put(dty, val);
    return val->getRVal();
}

llvm::Value* IrFuncTy::getRet(Type* dty, DValue* val)
{
    assert(!arg_sret);
    if (ret->rewrite)
        return ret->rewrite->get(dty, val);
    return val->getRVal();
}

llvm::Value* IrFuncTy::putParam(Type* dty, int idx, DValue* val)
{
    assert(idx >= 0 && idx < args.size() && "invalid putParam");
    if (args[idx]->rewrite)
        return args[idx]->rewrite->put(dty, val);
    return val->getRVal();
}

llvm::Value* IrFuncTy::getParam(Type* dty, int idx, DValue* val)
{
    assert(idx >= 0 && idx < args.size() && "invalid getParam");
    if (args[idx]->rewrite)
        return args[idx]->rewrite->get(dty, val);
    return val->getRVal();
}

void IrFuncTy::getParam(Type* dty, int idx, DValue* val, llvm::Value* lval)
{
    assert(idx >= 0 && idx < args.size() && "invalid getParam");

    if (args[idx]->rewrite)
    {
        args[idx]->rewrite->getL(dty, val, lval);
        return;
    }

    DtoStore(val->getRVal(), lval);
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

IrFunction::IrFunction(FuncDeclaration* fd)
{
    decl = fd;

    Type* t = fd->type->toBasetype();
    assert(t->ty == Tfunction);
    type = (TypeFunction*)t;
    func = NULL;
    allocapoint = NULL;

    queued = false;
    defined = false;

    retArg = NULL;
    thisArg = NULL;
    nestArg = NULL;

    nestedVar = NULL;
    
    _arguments = NULL;
    _argptr = NULL;
    
    nextUnique.push(0);
}

std::string IrFunction::getScopedLabelName(const char* ident)
{
    if(labelScopes.empty())
        return std::string(ident);

    std::string result = "__";
    for(unsigned int i = 0; i < labelScopes.size(); ++i)
        result += labelScopes[i] + "_";
    return result + ident;
}

void IrFunction::pushUniqueLabelScope(const char* name)
{
    std::ostringstream uniquename;
    uniquename << name << nextUnique.top()++;
    nextUnique.push(0);
    labelScopes.push_back(uniquename.str());
}

void IrFunction::popLabelScope()
{
    labelScopes.pop_back();
    nextUnique.pop();
}

void IrFunction::setNeverInline()
{
    assert(!func->hasFnAttr(llvm::Attribute::AlwaysInline) && "function can't be never- and always-inline at the same time");
    func->addFnAttr(llvm::Attribute::NoInline);
}

void IrFunction::setAlwaysInline()
{
    assert(!func->hasFnAttr(llvm::Attribute::NoInline) && "function can't be never- and always-inline at the same time");
    func->addFnAttr(llvm::Attribute::AlwaysInline);
}