Skip to content

Commit

Permalink
Added method 'getTypeAllocSizeInBits' to class 'DataLayout', added ad…
Browse files Browse the repository at this point in the history
…ditional constructor allowing to instantiate 'DataLayout' based on the existing 'Module'

added corresponding tests
  • Loading branch information
sailingKieler committed Jul 8, 2024
1 parent 29c386b commit 792ac2e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ class DataLayout : public Napi::ObjectWrap<DataLayout> {
Napi::Value getStringRepresentation(const Napi::CallbackInfo &info);

Napi::Value getTypeAllocSize(const Napi::CallbackInfo &info);

Napi::Value getTypeAllocSizeInBits(const Napi::CallbackInfo &info);
};
4 changes: 4 additions & 0 deletions llvm-bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1946,11 +1946,15 @@ declare namespace llvm {
}

class DataLayout {
public constructor(module: Module);

public constructor(desc: string);

public getStringRepresentation(): string;

public getTypeAllocSize(type: Type): number;

public getTypeAllocSizeInBits(type: Type): number;
}

function verifyFunction(func: Function): boolean;
Expand Down
20 changes: 17 additions & 3 deletions src/IR/DataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ void DataLayout::Init(Napi::Env env, Napi::Object &exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "DataLayout", {
InstanceMethod("getStringRepresentation", &DataLayout::getStringRepresentation),
InstanceMethod("getTypeAllocSize", &DataLayout::getTypeAllocSize)
InstanceMethod("getTypeAllocSize", &DataLayout::getTypeAllocSize),
InstanceMethod("getTypeAllocSizeInBits", &DataLayout::getTypeAllocSizeInBits)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand All @@ -27,10 +28,13 @@ llvm::DataLayout &DataLayout::Extract(const Napi::Value &value) {
DataLayout::DataLayout(const Napi::CallbackInfo &info) : ObjectWrap(info) {
Napi::Env env = info.Env();
if (!info.IsConstructCall() || info.Length() == 0 ||
!info[0].IsExternal() && !info[0].IsString()) {
!Module::IsClassOf(info[0]) && !info[0].IsExternal() && !info[0].IsString()) {
throw Napi::TypeError::New(env, ErrMsg::Class::DataLayout::constructor);
}
if (info[0].IsExternal()) {
if (Module::IsClassOf(info[0])) {
llvm::Module *module = Module::Extract(info[0]);
dataLayout = new llvm::DataLayout(module);
} else if (info[0].IsExternal()) {
auto external = info[0].As<Napi::External<llvm::DataLayout>>();
dataLayout = external.Data();
} else if (info[0].IsString()) {
Expand All @@ -57,3 +61,13 @@ Napi::Value DataLayout::getTypeAllocSize(const Napi::CallbackInfo &info) {
}
throw Napi::TypeError::New(env, ErrMsg::Class::DataLayout::getTypeAllocSize);
}

Napi::Value DataLayout::getTypeAllocSizeInBits(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() == 1 && Type::IsClassOf(info[0])) {
llvm::Type *type = Type::Extract(info[0]);
auto allocSizeInBits = dataLayout->getTypeAllocSizeInBits(type);
return Napi::Number::New(env, double(allocSizeInBits.getFixedSize()));
}
throw Napi::TypeError::New(env, ErrMsg::Class::DataLayout::getTypeAllocSize);
}
11 changes: 11 additions & 0 deletions tests/IR/Type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Test type testers', () => {
test('Test with ArrayType', () => {
const context = new llvm.LLVMContext();
const irBuilder = new llvm.IRBuilder(context);
const module = new llvm.Module('testModule', context);

[
llvm.ArrayType.get(irBuilder.getInt8Ty(), 17),
Expand All @@ -57,12 +58,16 @@ describe('Test type testers', () => {
expect(valArrayType.isStructTy()).toBe(false);
expect(valArrayType.isVectorTy()).toBe(false);
expect(valArrayType.isVoidTy()).toBe(false);

expect(module.getDataLayout().getTypeAllocSize(valArrayType)).toBe(17);
expect(module.getDataLayout().getTypeAllocSizeInBits(valArrayType)).toBe(136);
});
});

test('Test with StructType', () => {
const context = new llvm.LLVMContext();
const irBuilder = new llvm.IRBuilder(context);
const module = new llvm.Module('testModule', context);

[
llvm.StructType.create(context, [ irBuilder.getInt8Ty(), irBuilder.getInt8Ty() ], 'myStruct'),
Expand All @@ -85,6 +90,12 @@ describe('Test type testers', () => {
expect(valStructType.isStructTy() && valStructType.getNumElements()).toBe(2);
expect(valStructType.isStructTy() && valStructType.getElementType(0).isIntegerTy(8)).toBe(true);
expect(valStructType.isStructTy() && valStructType.getElementType(1).isIntegerTy(8)).toBe(true);

expect(module.getDataLayout().getTypeAllocSize(valStructType)).toBe(2);
expect(module.getDataLayout().getTypeAllocSizeInBits(valStructType)).toBe(16);

expect(new llvm.DataLayout(module).getTypeAllocSize(valStructType)).toBe(2);
expect(new llvm.DataLayout(module.getDataLayoutStr()).getTypeAllocSizeInBits(valStructType)).toBe(16);
});
});

Expand Down

0 comments on commit 792ac2e

Please sign in to comment.