在llvm中声明int64全局变量时出错

在llvm中声明int64全局变量时出错,llvm,llvm-ir,llvm-c++-api,Llvm,Llvm Ir,Llvm C++ Api,我正在尝试使用llvm构建我自己的dsl 在这里,我在使用VS2017win32模式时遇到了一个奇怪的问题。 我通过外部链接在一个模块中定义了一个值为3的int64全局变量 然后,当我在另一个模块中声明这个变量并加载它的值时,我得到了12884901891(与0x30000003相同)。当我在Linux上尝试这个方法时,效果很好 希望有人能帮助我 Blow是我的代码。CJITEngine是一个简单的单例软件包 如果我用其他类型而不是int64定义全局变量,比如int32、float、double

我正在尝试使用
llvm
构建我自己的dsl

在这里,我在使用VS2017
win32
模式时遇到了一个奇怪的问题。 我通过外部链接在一个模块中定义了一个值为3的
int64
全局变量

然后,当我在另一个模块中声明这个变量并加载它的值时,我得到了12884901891(与0x30000003相同)。当我在Linux上尝试这个方法时,效果很好

希望有人能帮助我

Blow是我的代码。CJITEngine是一个简单的单例软件包

如果我用其他类型而不是int64定义全局变量,比如int32、float、double,那么这段代码可以很好地运行


    int main()
    {
        CJITEngine::Instance().Init();
        DataLayout &layout = CJITEngine::Instance().GetDataLayout();

        using fpType = void(*)(int64_t*);
        fpType fp = nullptr;

        string name("aaaaaaaa");

        {
            unique_ptr<Module> pModule = CJITEngine::Instance().CreateModule("module1");

            IRBuilder<>& m_oBuilder = CJITEngine::Instance().GetIRBuilder();
            Type* pType = m_oBuilder.getInt64Ty();
            GlobalVariable* pVarValue = new GlobalVariable(*pModule, pType, false, GlobalValue::ExternalLinkage,
                                                           m_oBuilder.getInt64(3), name);
            pVarValue->setAlignment(layout.getABITypeAlignment(pType));
            pVarValue->setDSOLocal(true);
            pModule->addModuleFlag(Module::Error, "NumRegisterParameters", m_oBuilder.getInt32(0));
            pModule->addModuleFlag(Module::Error, "wchar_size", m_oBuilder.getInt32(2));
            pModule->print(outs(), nullptr);
            std::cout << "--------------------------------------------------------" << std::endl;
            CJITEngine::Instance().AddModule(std::move(pModule));
        }

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

        {
            unique_ptr<Module> pModule = CJITEngine::Instance().CreateModule("module2");

            LLVMContext& m_oContext = CJITEngine::Instance().GetContext();
            IRBuilder<>& m_oBuilder = CJITEngine::Instance().GetIRBuilder();
            Type* pType = m_oBuilder.getInt64Ty();
            GlobalVariable* pVarValue = new GlobalVariable(*pModule, pType, false, GlobalValue::ExternalLinkage,
                                                  nullptr, name);
            pVarValue->setAlignment(layout.getABITypeAlignment(pType));
            pVarValue->setDSOLocal(true);
            FunctionType* pFuncType = FunctionType::get(m_oBuilder.getVoidTy(), {Type::getInt64PtrTy(m_oContext)}, false);
            Function* pFunc = Function::Create(pFuncType, Function::ExternalLinkage, "func", pModule.get());

            pFunc->setDSOLocal(true);
            BasicBlock* pEntryBlock = BasicBlock::Create(m_oContext, "entry", pFunc);
            BasicBlock* pExitBlock = BasicBlock::Create(m_oContext, "exit");
            m_oBuilder.SetInsertPoint(pEntryBlock);

            auto agr = pFunc->args().begin();
            AllocaInst* pParam = m_oBuilder.CreateAlloca(agr->getType());
            pParam->setAlignment(layout.getABITypeAlignment(Type::getInt64PtrTy(m_oContext)));
            m_oBuilder.CreateAlignedStore(agr, pParam, layout.getABITypeAlignment(Type::getInt64PtrTy(m_oContext)));
            Value* pStr = m_oBuilder.CreateAlignedLoad(pVarValue, layout.getABITypeAlignment(Type::getInt64Ty(m_oContext)));
            Value* ppp= m_oBuilder.CreateAlignedLoad(pParam, layout.getABITypeAlignment(Type::getInt64PtrTy(m_oContext)));
            m_oBuilder.CreateAlignedStore(pStr, ppp, layout.getABITypeAlignment(Type::getInt64Ty(m_oContext)));

            m_oBuilder.CreateRetVoid();

            pModule->addModuleFlag(Module::Error, "NumRegisterParameters", m_oBuilder.getInt32(0));
            pModule->addModuleFlag(Module::Error, "wchar_size", m_oBuilder.getInt32(2));

            pModule->print(outs(), nullptr);
            CJITEngine::Instance().AddModule(std::move(pModule));

            JITSymbol symbol = CJITEngine::Instance().FindSymbol("func");
            fp = (fpType)static_cast<intptr_t>(cantFail(symbol.getAddress()));

            int64_t x = 10;
            fp(&x);
            std::cout << hex << x << endl; // x is 0x300000003 here
        }
        return 0;
    }


int main()
{
CJITEngine::Instance().Init();
DataLayout&layout=CJITEngine::Instance().GetDataLayout();
使用fpType=void(*)(int64_t*);
fpType fp=nullptr;
字符串名称(“aaaaaa”);
{
uniqueptrpmodule=CJITEngine::Instance().CreateModule(“module1”);
IRBuilder&m_oBuilder=CJITEngine::Instance().GetIRBuilder();
Type*pType=m_oBuilder.getInt64Ty();
GlobalVariable*pVarValue=新的GlobalVariable(*pModule,pType,false,GlobalValue::ExternalLinkage,
m_oBuilder.getInt64(3),名称);
pVarValue->setAlignment(layout.getABITypeAlignment(pType));
pVarValue->setDSOLocal(真);
pModule->addModuleFlag(Module::Error,“NumRegisterParameters”,m_oBuilder.getInt32(0));
pModule->addModuleFlag(Module::Error,“wchar_size”,m_oBuilder.getInt32(2));
pModule->打印(outs(),nullptr);
标准::cout setDSOLocal(真);
FunctionType*pFuncType=FunctionType::get(m_oBuilder.getVoidTy(),{Type::getInt64PtrTy(m_oContext)},false);
Function*pFunc=Function::Create(pFuncType,Function::ExternalLinkage,“func”,pModule.get());
pFunc->setDSOLocal(真);
BasicBlock*pEntryBlock=BasicBlock::Create(m_oContext,“entry”,pFunc);
BasicBlock*pExitBlock=BasicBlock::Create(m_oContext,“exit”);
m_oBuilder.设置插入点(pEntryBlock);
自动agr=pFunc->args().begin();
AllocaInst*pParam=m_oBuilder.CreateAlloca(agr->getType());
pParam->setAlignment(layout.getABITypeAlignment(Type::getint64ptry(m_oContext));
m_oBuilder.CreateAlignedStore(agr、pParam、layout.getABITypeAlignment(Type::getint64ptry(m_oContext));
Value*pStr=m_oBuilder.CreateAlignedLoad(pVarValue,layout.getABITypeAlignment(Type::getInt64Ty(m_oContext));
Value*ppp=m_oBuilder.CreateAlignedLoad(pParam,layout.getAbityAlignment(Type::getInt64PtrTy(m_oContext));
m_oBuilder.CreateAlignedStore(pStr、ppp、layout.getABITypeAlignment(Type::getInt64Ty(m_oContext));
m_oBuilder.CreateRetVoid();
pModule->addModuleFlag(Module::Error,“NumRegisterParameters”,m_oBuilder.getInt32(0));
pModule->addModuleFlag(Module::Error,“wchar_size”,m_oBuilder.getInt32(2));
pModule->打印(outs(),nullptr);
CJITEngine::Instance().AddModule(std::move(pModule));
JITSymbol=CJITEngine::Instance().FindSymbol(“func”);
fp=(fpType)静态转换(cantFail(symbol.getAddress());
int64_t x=10;
fp&x;

std::cout如果我在定义的模块中加载全局值,我可以得到正确的值。全局性和外部链接意味着变量位于全局命名空间中,即,你承诺只有一个具有该名称的变量,LLVM和链接器可能依赖于此。这并不意味着其他模块会神奇地知道哪个模块实际包含该变量。显示IR或用于生成该变量的代码。@arrowd已添加代码