Skip to content

Commit 87efdd1

Browse files
committed
Add Module::getOrInsertGlobal(string, llvm::Type)
1 parent 2993d11 commit 87efdd1

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

include/IR/Module.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Module : public Napi::ObjectWrap<Module> {
4848

4949
Napi::Value getGlobalVariable(const Napi::CallbackInfo &info);
5050

51+
Napi::Value getOrInsertGlobal(const Napi::CallbackInfo &info);
52+
5153
void addModuleFlag(const Napi::CallbackInfo &info);
5254

5355
Napi::Value empty(const Napi::CallbackInfo &info);

include/Util/ErrMsg.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace ErrMsg {
2424
constexpr const char *getFunction = "Module.getFunction needs to be called with: (name: string)";
2525
constexpr const char *getOrInsertFunction = "Module.getOrInsertFunction needs to be called with: (name: string, fnType: FunctionType)";
2626
constexpr const char *getGlobalVariable = "Module.getGlobalVariable needs to be called with: (name: string, allowInternal?: boolean)";
27+
constexpr const char *getOrInsertGlobal = "Module.getOrInsertGlobal needs to be called with: (name: string, type: Type)";
2728
constexpr const char *addModuleFlag = "Module.addModuleFlag needs to be called with (behavior: number, key: string, value: number)"
2829
"\n\t - limit: behavior should belong to [1, 7]";
2930
}

llvm-bindings.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ declare namespace llvm {
227227

228228
public getGlobalVariable(name: string, allowInternal?: boolean): GlobalVariable | null;
229229

230+
public getOrInsertGlobal(name: string, varType: Type): FunctionCallee;
231+
230232
public addModuleFlag(behavior: number, key: string, value: number): void;
231233

232234
public empty(): boolean;

src/IR/Module.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void Module::Init(Napi::Env env, Napi::Object &exports) {
3131
InstanceMethod("getFunction", &Module::getFunction),
3232
InstanceMethod("getOrInsertFunction", &Module::getOrInsertFunction),
3333
InstanceMethod("getGlobalVariable", &Module::getGlobalVariable),
34+
InstanceMethod("getOrInsertGlobal", &Module::getOrInsertGlobal),
3435
InstanceMethod("addModuleFlag", &Module::addModuleFlag),
3536
InstanceMethod("empty", &Module::empty),
3637
InstanceMethod("print", &Module::print)
@@ -198,6 +199,17 @@ Napi::Value Module::getGlobalVariable(const Napi::CallbackInfo &info) {
198199
throw Napi::TypeError::New(env, ErrMsg::Class::Module::getGlobalVariable);
199200
}
200201

202+
Napi::Value Module::getOrInsertGlobal(const Napi::CallbackInfo &info) {
203+
Napi::Env env = info.Env();
204+
if (info.Length() == 2 && info[0].IsString() && Type::IsClassOf(info[1])) {
205+
std::string functionName = info[0].As<Napi::String>();
206+
llvm::Type *type = Type::Extract(info[1]);
207+
llvm::Constant* glob = module->getOrInsertGlobal(functionName, type);
208+
return GlobalVariable::New(env, dyn_cast_or_null<llvm::GlobalVariable>(glob));
209+
}
210+
throw Napi::TypeError::New(env, ErrMsg::Class::Module::getOrInsertGlobal);
211+
}
212+
201213
void Module::addModuleFlag(const Napi::CallbackInfo &info) {
202214
Napi::Env env = info.Env();
203215
if (info.Length() == 3 && info[0].IsNumber() && info[1].IsString() && info[2].IsNumber()) {

tests/IR/Module.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,13 @@ describe('Test Module', () => {
359359
expect(module.print()).toMatchSnapshot();
360360
});
361361
});
362+
363+
describe('Global variables', () => {
364+
test('Global variable declared', () => {
365+
const context = new llvm.LLVMContext();
366+
const module = new llvm.Module(FileName, context);
367+
module.getOrInsertGlobal('HomerSimpson', llvm.Type.getInt32Ty(context));
368+
expect(module.print()).toContain('@HomerSimpson');
369+
});
370+
});
362371
});

0 commit comments

Comments
 (0)