From 87efdd167043ec529df5342a21d40a645763a2d1 Mon Sep 17 00:00:00 2001 From: Markus Rudolph Date: Mon, 5 Aug 2024 14:37:58 +0200 Subject: [PATCH] Add `Module::getOrInsertGlobal(string, llvm::Type)` --- include/IR/Module.h | 2 ++ include/Util/ErrMsg.h | 1 + llvm-bindings.d.ts | 2 ++ src/IR/Module.cpp | 12 ++++++++++++ tests/IR/Module.spec.ts | 9 +++++++++ 5 files changed, 26 insertions(+) diff --git a/include/IR/Module.h b/include/IR/Module.h index d1182bc..9966130 100644 --- a/include/IR/Module.h +++ b/include/IR/Module.h @@ -48,6 +48,8 @@ class Module : public Napi::ObjectWrap { Napi::Value getGlobalVariable(const Napi::CallbackInfo &info); + Napi::Value getOrInsertGlobal(const Napi::CallbackInfo &info); + void addModuleFlag(const Napi::CallbackInfo &info); Napi::Value empty(const Napi::CallbackInfo &info); diff --git a/include/Util/ErrMsg.h b/include/Util/ErrMsg.h index 886e806..0d5a34a 100644 --- a/include/Util/ErrMsg.h +++ b/include/Util/ErrMsg.h @@ -24,6 +24,7 @@ namespace ErrMsg { constexpr const char *getFunction = "Module.getFunction needs to be called with: (name: string)"; constexpr const char *getOrInsertFunction = "Module.getOrInsertFunction needs to be called with: (name: string, fnType: FunctionType)"; constexpr const char *getGlobalVariable = "Module.getGlobalVariable needs to be called with: (name: string, allowInternal?: boolean)"; + constexpr const char *getOrInsertGlobal = "Module.getOrInsertGlobal needs to be called with: (name: string, type: Type)"; constexpr const char *addModuleFlag = "Module.addModuleFlag needs to be called with (behavior: number, key: string, value: number)" "\n\t - limit: behavior should belong to [1, 7]"; } diff --git a/llvm-bindings.d.ts b/llvm-bindings.d.ts index d1fc84a..7d8d60f 100644 --- a/llvm-bindings.d.ts +++ b/llvm-bindings.d.ts @@ -227,6 +227,8 @@ declare namespace llvm { public getGlobalVariable(name: string, allowInternal?: boolean): GlobalVariable | null; + public getOrInsertGlobal(name: string, varType: Type): FunctionCallee; + public addModuleFlag(behavior: number, key: string, value: number): void; public empty(): boolean; diff --git a/src/IR/Module.cpp b/src/IR/Module.cpp index 8d91a3c..56c1a3b 100644 --- a/src/IR/Module.cpp +++ b/src/IR/Module.cpp @@ -31,6 +31,7 @@ void Module::Init(Napi::Env env, Napi::Object &exports) { InstanceMethod("getFunction", &Module::getFunction), InstanceMethod("getOrInsertFunction", &Module::getOrInsertFunction), InstanceMethod("getGlobalVariable", &Module::getGlobalVariable), + InstanceMethod("getOrInsertGlobal", &Module::getOrInsertGlobal), InstanceMethod("addModuleFlag", &Module::addModuleFlag), InstanceMethod("empty", &Module::empty), InstanceMethod("print", &Module::print) @@ -198,6 +199,17 @@ Napi::Value Module::getGlobalVariable(const Napi::CallbackInfo &info) { throw Napi::TypeError::New(env, ErrMsg::Class::Module::getGlobalVariable); } +Napi::Value Module::getOrInsertGlobal(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + if (info.Length() == 2 && info[0].IsString() && Type::IsClassOf(info[1])) { + std::string functionName = info[0].As(); + llvm::Type *type = Type::Extract(info[1]); + llvm::Constant* glob = module->getOrInsertGlobal(functionName, type); + return GlobalVariable::New(env, dyn_cast_or_null(glob)); + } + throw Napi::TypeError::New(env, ErrMsg::Class::Module::getOrInsertGlobal); +} + void Module::addModuleFlag(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); if (info.Length() == 3 && info[0].IsNumber() && info[1].IsString() && info[2].IsNumber()) { diff --git a/tests/IR/Module.spec.ts b/tests/IR/Module.spec.ts index ec05499..784197e 100644 --- a/tests/IR/Module.spec.ts +++ b/tests/IR/Module.spec.ts @@ -359,4 +359,13 @@ describe('Test Module', () => { expect(module.print()).toMatchSnapshot(); }); }); + + describe('Global variables', () => { + test('Global variable declared', () => { + const context = new llvm.LLVMContext(); + const module = new llvm.Module(FileName, context); + module.getOrInsertGlobal('HomerSimpson', llvm.Type.getInt32Ty(context)); + expect(module.print()).toContain('@HomerSimpson'); + }); + }); });