Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Module::getOrInsertGlobal(string, llvm::Type) #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, llvm::cast<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');
});
});
});
Loading