Skip to content

Commit

Permalink
Add Module::getOrInsertGlobal(string, llvm::Type)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotes committed Aug 5, 2024
1 parent 2993d11 commit 87efdd1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Module : public Napi::ObjectWrap<Module> {

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);
Expand Down
1 change: 1 addition & 0 deletions include/Util/ErrMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
}
Expand Down
2 changes: 2 additions & 0 deletions llvm-bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Napi::String>();
llvm::Type *type = Type::Extract(info[1]);
llvm::Constant* glob = module->getOrInsertGlobal(functionName, type);
return GlobalVariable::New(env, dyn_cast_or_null<llvm::GlobalVariable>(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()) {
Expand Down
9 changes: 9 additions & 0 deletions tests/IR/Module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit 87efdd1

Please sign in to comment.