Skip to content

Commit 0ed47c8

Browse files
committed
Added method 'getTypeAllocSizeInBits' to class 'DataLayout', added additional constructor allowing to instantiate 'DataLayout' based on the existing 'Module'
added corresponding tests
1 parent c768136 commit 0ed47c8

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

include/IR/DataLayout.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ class DataLayout : public Napi::ObjectWrap<DataLayout> {
2525
Napi::Value getStringRepresentation(const Napi::CallbackInfo &info);
2626

2727
Napi::Value getTypeAllocSize(const Napi::CallbackInfo &info);
28+
29+
Napi::Value getTypeAllocSizeInBits(const Napi::CallbackInfo &info);
2830
};

llvm-bindings.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1946,11 +1946,15 @@ declare namespace llvm {
19461946
}
19471947

19481948
class DataLayout {
1949+
public constructor(module: Module);
1950+
19491951
public constructor(desc: string);
19501952

19511953
public getStringRepresentation(): string;
19521954

19531955
public getTypeAllocSize(type: Type): number;
1956+
1957+
public getTypeAllocSizeInBits(type: Type): number;
19541958
}
19551959

19561960
function verifyFunction(func: Function): boolean;

src/IR/DataLayout.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ void DataLayout::Init(Napi::Env env, Napi::Object &exports) {
55
Napi::HandleScope scope(env);
66
Napi::Function func = DefineClass(env, "DataLayout", {
77
InstanceMethod("getStringRepresentation", &DataLayout::getStringRepresentation),
8-
InstanceMethod("getTypeAllocSize", &DataLayout::getTypeAllocSize)
8+
InstanceMethod("getTypeAllocSize", &DataLayout::getTypeAllocSize),
9+
InstanceMethod("getTypeAllocSizeInBits", &DataLayout::getTypeAllocSizeInBits)
910
});
1011
constructor = Napi::Persistent(func);
1112
constructor.SuppressDestruct();
@@ -27,10 +28,13 @@ llvm::DataLayout &DataLayout::Extract(const Napi::Value &value) {
2728
DataLayout::DataLayout(const Napi::CallbackInfo &info) : ObjectWrap(info) {
2829
Napi::Env env = info.Env();
2930
if (!info.IsConstructCall() || info.Length() == 0 ||
30-
!info[0].IsExternal() && !info[0].IsString()) {
31+
!Module::IsClassOf(info[0]) && !info[0].IsExternal() && !info[0].IsString()) {
3132
throw Napi::TypeError::New(env, ErrMsg::Class::DataLayout::constructor);
3233
}
33-
if (info[0].IsExternal()) {
34+
if (Module::IsClassOf(info[0])) {
35+
llvm::Module *module = Module::Extract(info[0]);
36+
dataLayout = new llvm::DataLayout(module);
37+
} else if (info[0].IsExternal()) {
3438
auto external = info[0].As<Napi::External<llvm::DataLayout>>();
3539
dataLayout = external.Data();
3640
} else if (info[0].IsString()) {
@@ -57,3 +61,13 @@ Napi::Value DataLayout::getTypeAllocSize(const Napi::CallbackInfo &info) {
5761
}
5862
throw Napi::TypeError::New(env, ErrMsg::Class::DataLayout::getTypeAllocSize);
5963
}
64+
65+
Napi::Value DataLayout::getTypeAllocSizeInBits(const Napi::CallbackInfo &info) {
66+
Napi::Env env = info.Env();
67+
if (info.Length() == 1 && Type::IsClassOf(info[0])) {
68+
llvm::Type *type = Type::Extract(info[0]);
69+
auto allocSizeInBits = dataLayout->getTypeAllocSizeInBits(type);
70+
return Napi::Number::New(env, double(allocSizeInBits.getFixedSize()));
71+
}
72+
throw Napi::TypeError::New(env, ErrMsg::Class::DataLayout::getTypeAllocSize);
73+
}

tests/IR/Type.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('Test type testers', () => {
3939
test('Test with ArrayType', () => {
4040
const context = new llvm.LLVMContext();
4141
const irBuilder = new llvm.IRBuilder(context);
42+
const module = new llvm.Module('testModule', context);
4243

4344
[
4445
llvm.ArrayType.get(irBuilder.getInt8Ty(), 17),
@@ -57,12 +58,16 @@ describe('Test type testers', () => {
5758
expect(valArrayType.isStructTy()).toBe(false);
5859
expect(valArrayType.isVectorTy()).toBe(false);
5960
expect(valArrayType.isVoidTy()).toBe(false);
61+
62+
expect(module.getDataLayout().getTypeAllocSize(valArrayType)).toBe(17);
63+
expect(module.getDataLayout().getTypeAllocSizeInBits(valArrayType)).toBe(136);
6064
});
6165
});
6266

6367
test('Test with StructType', () => {
6468
const context = new llvm.LLVMContext();
6569
const irBuilder = new llvm.IRBuilder(context);
70+
const module = new llvm.Module('testModule', context);
6671

6772
[
6873
llvm.StructType.create(context, [ irBuilder.getInt8Ty(), irBuilder.getInt8Ty() ], 'myStruct'),
@@ -85,6 +90,12 @@ describe('Test type testers', () => {
8590
expect(valStructType.isStructTy() && valStructType.getNumElements()).toBe(2);
8691
expect(valStructType.isStructTy() && valStructType.getElementType(0).isIntegerTy(8)).toBe(true);
8792
expect(valStructType.isStructTy() && valStructType.getElementType(1).isIntegerTy(8)).toBe(true);
93+
94+
expect(module.getDataLayout().getTypeAllocSize(valStructType)).toBe(2);
95+
expect(module.getDataLayout().getTypeAllocSizeInBits(valStructType)).toBe(16);
96+
97+
expect(new llvm.DataLayout(module).getTypeAllocSize(valStructType)).toBe(2);
98+
expect(new llvm.DataLayout(module.getDataLayoutStr()).getTypeAllocSizeInBits(valStructType)).toBe(16);
8899
});
89100
});
90101

0 commit comments

Comments
 (0)