@designliquido/llvm-bindings 2.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/build.yml +2 -2
- package/CHANGELOG.md +1 -0
- package/CMakeLists.txt +2 -2
- package/README.md +1 -0
- package/cmake/LLVM.cmake +1 -1
- package/package.json +4 -6
- package/src/IR/Intrinsic.cpp +0 -1
- package/tests/IR/OpaquePointer.spec.ts +39 -0
- package/tests/IR/Type.spec.ts +24 -0
- package/tests/fixtures/add.spec.ts +24 -0
- package/tests/fixtures/attribute.spec.ts +27 -0
- package/tests/fixtures/bitcodeWriter.spec.ts +42 -0
- package/tests/fixtures/class.spec.ts +30 -0
- package/tests/fixtures/debugInfo.spec.ts +73 -0
- package/tests/fixtures/exception.spec.ts +42 -0
- package/tests/fixtures/fibonacci.spec.ts +36 -0
- package/tests/fixtures/gep.spec.ts +27 -0
- package/tests/fixtures/intrinsic.spec.ts +29 -0
- package/tests/fixtures/linker.spec.ts +30 -0
- package/tests/fixtures/str.spec.ts +14 -0
- package/tests/fixtures/switch.spec.ts +45 -0
- package/tests/fixtures/unary.spec.ts +44 -0
- package/tests/fixtures/variable.spec.ts +25 -0
- package/test/add.ts +0 -32
- package/test/attribute.ts +0 -37
- package/test/bitcodeWriter.ts +0 -44
- package/test/class.ts +0 -39
- package/test/debugInfo.ts +0 -181
- package/test/exception.ts +0 -57
- package/test/fibonacci.ts +0 -44
- package/test/gep.ts +0 -42
- package/test/index.ts +0 -33
- package/test/intrinsic.ts +0 -35
- package/test/linker.ts +0 -48
- package/test/str.ts +0 -19
- package/test/switch.ts +0 -58
- package/test/target.ts +0 -14
- package/test/type.ts +0 -21
- package/test/unary.ts +0 -53
- package/test/variable.ts +0 -37
- /package/{test → tests/fixtures}/bitcode/add.bc +0 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import llvm from '../..';
|
|
2
|
+
|
|
3
|
+
describe('Test Variable', () => {
|
|
4
|
+
test('builds and verifies valid IR for alloca/store/load', () => {
|
|
5
|
+
const context = new llvm.LLVMContext();
|
|
6
|
+
const module = new llvm.Module('variable', context);
|
|
7
|
+
const builder = new llvm.IRBuilder(context);
|
|
8
|
+
|
|
9
|
+
const returnType = builder.getVoidTy();
|
|
10
|
+
const functionType = llvm.FunctionType.get(returnType, false);
|
|
11
|
+
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'variable', module);
|
|
12
|
+
|
|
13
|
+
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
14
|
+
builder.SetInsertPoint(entryBB);
|
|
15
|
+
const alloca = builder.CreateAlloca(builder.getInt32Ty(), null, 'a');
|
|
16
|
+
builder.CreateStore(builder.getInt32(11), alloca);
|
|
17
|
+
const value = builder.CreateLoad(builder.getInt32Ty(), alloca);
|
|
18
|
+
const cond = builder.CreateICmpSGT(value, builder.getInt32(10), 'cond');
|
|
19
|
+
builder.CreateSelect(cond, builder.getInt32(10), builder.getInt32(20));
|
|
20
|
+
builder.CreateRetVoid();
|
|
21
|
+
|
|
22
|
+
expect(llvm.verifyFunction(func)).toBe(false);
|
|
23
|
+
expect(llvm.verifyModule(module)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
});
|
package/test/add.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testAdd(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const returnType = builder.getInt32Ty();
|
|
11
|
-
const paramTypes = [builder.getInt32Ty(), builder.getInt32Ty()];
|
|
12
|
-
const functionType = llvm.FunctionType.get(returnType, paramTypes, false);
|
|
13
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'add', module);
|
|
14
|
-
|
|
15
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
16
|
-
builder.SetInsertPoint(entryBB);
|
|
17
|
-
const paramA = func.getArg(0);
|
|
18
|
-
const paramB = func.getArg(1);
|
|
19
|
-
const result = builder.CreateAdd(paramA, paramB);
|
|
20
|
-
builder.CreateRet(result);
|
|
21
|
-
|
|
22
|
-
console.log(module.print());
|
|
23
|
-
|
|
24
|
-
if (llvm.verifyFunction(func)) {
|
|
25
|
-
console.error(`${filename}: verifying the 'add' function failed`);
|
|
26
|
-
process.exit(1);
|
|
27
|
-
}
|
|
28
|
-
if (llvm.verifyModule(module)) {
|
|
29
|
-
console.error(`${filename}: verifying the module failed`);
|
|
30
|
-
process.exit(1);
|
|
31
|
-
}
|
|
32
|
-
}
|
package/test/attribute.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testAttribute(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const returnType = builder.getInt32Ty();
|
|
11
|
-
const paramTypes = [builder.getInt32Ty(), builder.getInt32Ty()];
|
|
12
|
-
const functionType = llvm.FunctionType.get(returnType, paramTypes, false);
|
|
13
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'addWithAttributes', module);
|
|
14
|
-
|
|
15
|
-
const noInlineAttr = llvm.Attribute.get(context, llvm.Attribute.AttrKind.NoInline);
|
|
16
|
-
const inRegAttr = llvm.Attribute.get(context, llvm.Attribute.AttrKind.InReg, builder.getInt32Ty());
|
|
17
|
-
func.addFnAttr(noInlineAttr);
|
|
18
|
-
func.addParamAttr(0, inRegAttr);
|
|
19
|
-
|
|
20
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
21
|
-
builder.SetInsertPoint(entryBB);
|
|
22
|
-
const paramA = func.getArg(0);
|
|
23
|
-
const paramB = func.getArg(1);
|
|
24
|
-
const result = builder.CreateAdd(paramA, paramB);
|
|
25
|
-
builder.CreateRet(result);
|
|
26
|
-
|
|
27
|
-
console.log(module.print());
|
|
28
|
-
|
|
29
|
-
if (llvm.verifyFunction(func)) {
|
|
30
|
-
console.error(`${filename}: verifying the 'addWithAttributes' function failed`);
|
|
31
|
-
process.exit(1);
|
|
32
|
-
}
|
|
33
|
-
if (llvm.verifyModule(module)) {
|
|
34
|
-
console.error(`${filename}: verifying the module failed`);
|
|
35
|
-
process.exit(1);
|
|
36
|
-
}
|
|
37
|
-
}
|
package/test/bitcodeWriter.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import llvm from '..';
|
|
4
|
-
|
|
5
|
-
export default function testBitcodeWriter(): void {
|
|
6
|
-
const filename = path.basename(__filename);
|
|
7
|
-
const context = new llvm.LLVMContext();
|
|
8
|
-
const module = new llvm.Module(filename, context);
|
|
9
|
-
const builder = new llvm.IRBuilder(context);
|
|
10
|
-
|
|
11
|
-
const returnType = builder.getInt32Ty();
|
|
12
|
-
const paramTypes = [builder.getInt32Ty(), builder.getInt32Ty()];
|
|
13
|
-
const functionType = llvm.FunctionType.get(returnType, paramTypes, false);
|
|
14
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'writer', module);
|
|
15
|
-
|
|
16
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry');
|
|
17
|
-
builder.SetInsertPoint(entryBB);
|
|
18
|
-
const paramA = func.getArg(0);
|
|
19
|
-
const paramB = func.getArg(1);
|
|
20
|
-
const result = builder.CreateAdd(paramA, paramB);
|
|
21
|
-
builder.CreateRet(result);
|
|
22
|
-
|
|
23
|
-
console.log(module.print());
|
|
24
|
-
|
|
25
|
-
if (llvm.verifyFunction(func)) {
|
|
26
|
-
console.error(`${filename}: verifying the 'writer' function failed`);
|
|
27
|
-
process.exit(1);
|
|
28
|
-
}
|
|
29
|
-
if (llvm.verifyModule(module)) {
|
|
30
|
-
console.error(`${filename}: verifying the module failed`);
|
|
31
|
-
process.exit(1);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const outputDir = path.join(__dirname, 'temp');
|
|
35
|
-
const outputBitcodePath = path.join(outputDir, 'add.out.bc');
|
|
36
|
-
if (!fs.existsSync(outputDir)) {
|
|
37
|
-
fs.mkdirSync(outputDir);
|
|
38
|
-
}
|
|
39
|
-
if (fs.existsSync(outputBitcodePath)) {
|
|
40
|
-
fs.unlinkSync(outputBitcodePath);
|
|
41
|
-
}
|
|
42
|
-
llvm.WriteBitcodeToFile(module, outputBitcodePath);
|
|
43
|
-
console.log(`${filename}: bitcode was successfully written to the ${outputBitcodePath}`);
|
|
44
|
-
}
|
package/test/class.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testClass(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const elementTypes = [builder.getInt32Ty(), builder.getInt32Ty()];
|
|
11
|
-
const classStructType = llvm.StructType.create(context, elementTypes, 'Person');
|
|
12
|
-
|
|
13
|
-
const returnType = builder.getVoidTy();
|
|
14
|
-
const paramTypes = [llvm.PointerType.getUnqual(classStructType)];
|
|
15
|
-
const functionType = llvm.FunctionType.get(returnType, paramTypes, false);
|
|
16
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'class_Person_constructor', module);
|
|
17
|
-
|
|
18
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
19
|
-
builder.SetInsertPoint(entryBB);
|
|
20
|
-
|
|
21
|
-
const thisPtr = func.getArg(0);
|
|
22
|
-
const propertyPtr = builder.CreateGEP(classStructType, thisPtr, [
|
|
23
|
-
builder.getInt32(0),
|
|
24
|
-
builder.getInt32(1)
|
|
25
|
-
]);
|
|
26
|
-
builder.CreateStore(builder.getInt32(111), propertyPtr);
|
|
27
|
-
builder.CreateRetVoid();
|
|
28
|
-
|
|
29
|
-
console.log(module.print());
|
|
30
|
-
|
|
31
|
-
if (llvm.verifyFunction(func)) {
|
|
32
|
-
console.error(`${filename}: verifying the 'class_Person_constructor' function failed`);
|
|
33
|
-
process.exit(1);
|
|
34
|
-
}
|
|
35
|
-
if (llvm.verifyModule(module)) {
|
|
36
|
-
console.error(`${filename}: verifying the module failed`);
|
|
37
|
-
process.exit(1);
|
|
38
|
-
}
|
|
39
|
-
};
|
package/test/debugInfo.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
function createAddFunc(
|
|
5
|
-
context: llvm.LLVMContext,
|
|
6
|
-
module: llvm.Module,
|
|
7
|
-
builder: llvm.IRBuilder,
|
|
8
|
-
debugInfoBuilder: llvm.DIBuilder,
|
|
9
|
-
debugInfoFile: llvm.DIFile,
|
|
10
|
-
debugInfoUnit: llvm.DICompileUnit,
|
|
11
|
-
debugInfoBasicType: llvm.DIBasicType): llvm.Function {
|
|
12
|
-
|
|
13
|
-
const addFuncReturnType = builder.getInt32Ty();
|
|
14
|
-
const addFuncParamTypes = [builder.getInt32Ty(), builder.getInt32Ty()];
|
|
15
|
-
const addFuncType = llvm.FunctionType.get(addFuncReturnType, addFuncParamTypes, false);
|
|
16
|
-
const addFunc = llvm.Function.Create(addFuncType, llvm.Function.LinkageTypes.ExternalLinkage, 'add', module);
|
|
17
|
-
const debugInfoParamTypes = debugInfoBuilder.getOrCreateTypeArray([debugInfoBasicType, debugInfoBasicType, debugInfoBasicType]);
|
|
18
|
-
const debugInfoSubroutineType = debugInfoBuilder.createSubroutineType(debugInfoParamTypes);
|
|
19
|
-
const debugInfoAddFuncSubprogram = debugInfoBuilder.createFunction(
|
|
20
|
-
debugInfoFile, 'add', '', debugInfoFile, 1,
|
|
21
|
-
debugInfoSubroutineType, 1, llvm.DINode.DIFlags.FlagPrototyped, llvm.DISubprogram.DISPFlags.SPFlagDefinition
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
addFunc.setSubprogram(debugInfoAddFuncSubprogram);
|
|
25
|
-
builder.SetCurrentDebugLocation(new llvm.DebugLoc());
|
|
26
|
-
|
|
27
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', addFunc);
|
|
28
|
-
builder.SetInsertPoint(entryBB);
|
|
29
|
-
|
|
30
|
-
const allocaA = builder.CreateAlloca(builder.getInt32Ty(), null, 'a');
|
|
31
|
-
builder.CreateStore(addFunc.getArg(0), allocaA);
|
|
32
|
-
const diLocalVarA = debugInfoBuilder.createParameterVariable(debugInfoAddFuncSubprogram, 'a', 1, debugInfoFile, 1, debugInfoBasicType);
|
|
33
|
-
debugInfoBuilder.insertDeclare(
|
|
34
|
-
allocaA,
|
|
35
|
-
diLocalVarA,
|
|
36
|
-
debugInfoBuilder.createExpression(),
|
|
37
|
-
llvm.DILocation.get(context, 1, 0, debugInfoAddFuncSubprogram),
|
|
38
|
-
builder.GetInsertBlock()!
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
const allocaB = builder.CreateAlloca(builder.getInt32Ty(), null, 'b');
|
|
42
|
-
builder.CreateStore(addFunc.getArg(1), allocaB);
|
|
43
|
-
const diLocalVarB = debugInfoBuilder.createParameterVariable(debugInfoAddFuncSubprogram, 'b', 2, debugInfoFile, 1, debugInfoBasicType);
|
|
44
|
-
debugInfoBuilder.insertDeclare(
|
|
45
|
-
allocaB,
|
|
46
|
-
diLocalVarB,
|
|
47
|
-
debugInfoBuilder.createExpression(),
|
|
48
|
-
llvm.DILocation.get(context, 1, 0, debugInfoAddFuncSubprogram),
|
|
49
|
-
builder.GetInsertBlock()!
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 2, 12, debugInfoAddFuncSubprogram));
|
|
53
|
-
const loadA = builder.CreateLoad(builder.getInt32Ty(), allocaA);
|
|
54
|
-
|
|
55
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 2, 16, debugInfoAddFuncSubprogram));
|
|
56
|
-
const loadB = builder.CreateLoad(builder.getInt32Ty(), allocaB);
|
|
57
|
-
|
|
58
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 2, 14, debugInfoAddFuncSubprogram));
|
|
59
|
-
const value = builder.CreateAdd(loadA, loadB);
|
|
60
|
-
|
|
61
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 2, 5, debugInfoAddFuncSubprogram));
|
|
62
|
-
builder.CreateRet(value);
|
|
63
|
-
|
|
64
|
-
debugInfoBuilder.finalizeSubprogram(debugInfoAddFuncSubprogram);
|
|
65
|
-
if (llvm.verifyFunction(addFunc)) {
|
|
66
|
-
console.error(`${path.basename(__filename)}: verifying the 'add' function failed`);
|
|
67
|
-
}
|
|
68
|
-
return addFunc;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function createMainFunc(
|
|
72
|
-
context: llvm.LLVMContext,
|
|
73
|
-
module: llvm.Module,
|
|
74
|
-
builder: llvm.IRBuilder,
|
|
75
|
-
debugInfoBuilder: llvm.DIBuilder,
|
|
76
|
-
debugInfoFile: llvm.DIFile,
|
|
77
|
-
debugInfoUnit: llvm.DICompileUnit,
|
|
78
|
-
debugInfoBasicType: llvm.DIBasicType,
|
|
79
|
-
addFunc: llvm.Function): llvm.Function {
|
|
80
|
-
|
|
81
|
-
const mainFuncReturnType = builder.getInt32Ty();
|
|
82
|
-
const mainFuncType = llvm.FunctionType.get(mainFuncReturnType, false);
|
|
83
|
-
const mainFunc = llvm.Function.Create(mainFuncType, llvm.Function.LinkageTypes.ExternalLinkage, 'main', module);
|
|
84
|
-
const debugInfoParamTypes = debugInfoBuilder.getOrCreateTypeArray([debugInfoBasicType]);
|
|
85
|
-
const debugInfoSubroutineType = debugInfoBuilder.createSubroutineType(debugInfoParamTypes);
|
|
86
|
-
const debugInfoMainFuncSubprogram = debugInfoBuilder.createFunction(
|
|
87
|
-
debugInfoFile, 'main', '', debugInfoFile, 5,
|
|
88
|
-
debugInfoSubroutineType, 5, llvm.DINode.DIFlags.FlagPrototyped, llvm.DISubprogram.DISPFlags.SPFlagDefinition
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
mainFunc.setSubprogram(debugInfoMainFuncSubprogram);
|
|
92
|
-
builder.SetCurrentDebugLocation(new llvm.DebugLoc());
|
|
93
|
-
|
|
94
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', mainFunc);
|
|
95
|
-
builder.SetInsertPoint(entryBB);
|
|
96
|
-
|
|
97
|
-
const allocaA = builder.CreateAlloca(builder.getInt32Ty(), null, 'a');
|
|
98
|
-
const allocaB = builder.CreateAlloca(builder.getInt32Ty(), null, 'b');
|
|
99
|
-
const allocaC = builder.CreateAlloca(builder.getInt32Ty(), null, 'c');
|
|
100
|
-
|
|
101
|
-
const diLocalVarA = debugInfoBuilder.createAutoVariable(debugInfoMainFuncSubprogram, 'a', debugInfoFile, 6, debugInfoBasicType);
|
|
102
|
-
debugInfoBuilder.insertDeclare(
|
|
103
|
-
allocaA,
|
|
104
|
-
diLocalVarA,
|
|
105
|
-
debugInfoBuilder.createExpression(),
|
|
106
|
-
llvm.DILocation.get(context, 6, 9, debugInfoMainFuncSubprogram),
|
|
107
|
-
builder.GetInsertBlock()!
|
|
108
|
-
);
|
|
109
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 6, 9, debugInfoMainFuncSubprogram));
|
|
110
|
-
builder.CreateStore(builder.getInt32(1), allocaA);
|
|
111
|
-
|
|
112
|
-
const diLocalVarB = debugInfoBuilder.createAutoVariable(debugInfoMainFuncSubprogram, 'b', debugInfoFile, 7, debugInfoBasicType);
|
|
113
|
-
debugInfoBuilder.insertDeclare(
|
|
114
|
-
allocaB,
|
|
115
|
-
diLocalVarB,
|
|
116
|
-
debugInfoBuilder.createExpression(),
|
|
117
|
-
llvm.DILocation.get(context, 7, 9, debugInfoMainFuncSubprogram),
|
|
118
|
-
builder.GetInsertBlock()!
|
|
119
|
-
);
|
|
120
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 7, 9, debugInfoMainFuncSubprogram));
|
|
121
|
-
builder.CreateStore(builder.getInt32(2), allocaB);
|
|
122
|
-
|
|
123
|
-
const diLocalVarC = debugInfoBuilder.createAutoVariable(debugInfoMainFuncSubprogram, 'c', debugInfoFile, 8, debugInfoBasicType);
|
|
124
|
-
debugInfoBuilder.insertDeclare(
|
|
125
|
-
allocaC,
|
|
126
|
-
diLocalVarC,
|
|
127
|
-
debugInfoBuilder.createExpression(),
|
|
128
|
-
llvm.DILocation.get(context, 8, 9, debugInfoMainFuncSubprogram),
|
|
129
|
-
builder.GetInsertBlock()!
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 8, 17, debugInfoMainFuncSubprogram));
|
|
133
|
-
const loadA = builder.CreateLoad(builder.getInt32Ty(), allocaA);
|
|
134
|
-
|
|
135
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 8, 20, debugInfoMainFuncSubprogram));
|
|
136
|
-
const loadB = builder.CreateLoad(builder.getInt32Ty(), allocaB);
|
|
137
|
-
|
|
138
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 8, 13, debugInfoMainFuncSubprogram));
|
|
139
|
-
const callRet = builder.CreateCall(addFunc, [loadA, loadB]);
|
|
140
|
-
|
|
141
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 8, 9, debugInfoMainFuncSubprogram));
|
|
142
|
-
builder.CreateStore(callRet, allocaC);
|
|
143
|
-
|
|
144
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 9, 12, debugInfoMainFuncSubprogram));
|
|
145
|
-
const loadC = builder.CreateLoad(builder.getInt32Ty(), allocaC);
|
|
146
|
-
|
|
147
|
-
builder.SetCurrentDebugLocation(llvm.DILocation.get(context, 9, 5, debugInfoMainFuncSubprogram));
|
|
148
|
-
builder.CreateRet(loadC);
|
|
149
|
-
|
|
150
|
-
debugInfoBuilder.finalizeSubprogram(debugInfoMainFuncSubprogram);
|
|
151
|
-
if (llvm.verifyFunction(mainFunc)) {
|
|
152
|
-
console.error(`${path.basename(__filename)}: verifying the 'main' function failed`);
|
|
153
|
-
}
|
|
154
|
-
return mainFunc;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export default function testDebugInfo(): void {
|
|
158
|
-
const filename = path.basename(__filename);
|
|
159
|
-
const context = new llvm.LLVMContext();
|
|
160
|
-
const module = new llvm.Module(filename, context);
|
|
161
|
-
const builder = new llvm.IRBuilder(context);
|
|
162
|
-
|
|
163
|
-
module.addModuleFlag(llvm.Module.ModFlagBehavior.Warning, 'Debug Info Version', llvm.LLVMConstants.DEBUG_METADATA_VERSION);
|
|
164
|
-
module.addModuleFlag(llvm.Module.ModFlagBehavior.Warning, 'Dwarf Version', llvm.dwarf.LLVMConstants.DWARF_VERSION);
|
|
165
|
-
const debugInfoBuilder = new llvm.DIBuilder(module);
|
|
166
|
-
const debugInfoFile = debugInfoBuilder.createFile(path.basename(__filename), __dirname);
|
|
167
|
-
const debugInfoUnit = debugInfoBuilder.createCompileUnit(llvm.dwarf.SourceLanguage.DW_LANG_C, debugInfoFile, 'llvm-bindings', false, '', 0);
|
|
168
|
-
const debugInfoBasicType = debugInfoBuilder.createBasicType('int', 32, llvm.dwarf.TypeKind.DW_ATE_float);
|
|
169
|
-
|
|
170
|
-
const addFunc = createAddFunc(context, module, builder, debugInfoBuilder, debugInfoFile, debugInfoUnit, debugInfoBasicType);
|
|
171
|
-
createMainFunc(context, module, builder, debugInfoBuilder, debugInfoFile, debugInfoUnit, debugInfoBasicType, addFunc);
|
|
172
|
-
|
|
173
|
-
debugInfoBuilder.finalize();
|
|
174
|
-
|
|
175
|
-
console.log(module.print());
|
|
176
|
-
|
|
177
|
-
if (llvm.verifyModule(module)) {
|
|
178
|
-
console.error(`${filename}: verifying the module failed`);
|
|
179
|
-
process.exit(1);
|
|
180
|
-
}
|
|
181
|
-
}
|
package/test/exception.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testException() {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const i8PtrType = builder.getInt8PtrTy();
|
|
11
|
-
const voidType = builder.getVoidTy();
|
|
12
|
-
|
|
13
|
-
const functionType = llvm.FunctionType.get(voidType, false);
|
|
14
|
-
const mainFunc = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'exception', module);
|
|
15
|
-
|
|
16
|
-
const allocExceptionFuncType = llvm.FunctionType.get(i8PtrType, [builder.getInt64Ty()], false);
|
|
17
|
-
const allocExceptionFunc = module.getOrInsertFunction('__cxa_allocate_exception', allocExceptionFuncType);
|
|
18
|
-
const throwFuncType = llvm.FunctionType.get(voidType, [i8PtrType, i8PtrType, i8PtrType], false);
|
|
19
|
-
const throwFunc = module.getOrInsertFunction('__cxa_throw', throwFuncType);
|
|
20
|
-
|
|
21
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', mainFunc);
|
|
22
|
-
builder.SetInsertPoint(entryBB);
|
|
23
|
-
|
|
24
|
-
const errMsgStr = builder.CreateGlobalString('error message');
|
|
25
|
-
|
|
26
|
-
const tmp1 = builder.CreateCall(allocExceptionFunc, [builder.getInt64(8)]);
|
|
27
|
-
const tmp2 = builder.CreateBitCast(tmp1, llvm.PointerType.getUnqual(builder.getInt8PtrTy()));
|
|
28
|
-
builder.CreateStore(
|
|
29
|
-
builder.CreateInBoundsGEP(
|
|
30
|
-
errMsgStr.getValueType(),
|
|
31
|
-
errMsgStr,
|
|
32
|
-
[builder.getInt64(0), builder.getInt64(0)]
|
|
33
|
-
),
|
|
34
|
-
tmp2
|
|
35
|
-
);
|
|
36
|
-
const tinfo = new llvm.GlobalVariable(
|
|
37
|
-
module,
|
|
38
|
-
builder.getInt8PtrTy(),
|
|
39
|
-
true,
|
|
40
|
-
llvm.Function.LinkageTypes.ExternalLinkage,
|
|
41
|
-
null
|
|
42
|
-
);
|
|
43
|
-
const tmp3 = builder.CreateBitCast(tinfo, builder.getInt8PtrTy());
|
|
44
|
-
builder.CreateCall(throwFunc, [tmp1, tmp3, llvm.ConstantPointerNull.get(builder.getInt8PtrTy())]);
|
|
45
|
-
builder.CreateUnreachable();
|
|
46
|
-
|
|
47
|
-
console.log(module.print());
|
|
48
|
-
|
|
49
|
-
if (llvm.verifyFunction(mainFunc)) {
|
|
50
|
-
console.error(`${filename}: verifying the 'exception' function failed`);
|
|
51
|
-
process.exit(1);
|
|
52
|
-
}
|
|
53
|
-
if (llvm.verifyModule(module)) {
|
|
54
|
-
console.error(`${filename}: verifying the module failed`);
|
|
55
|
-
process.exit(1);
|
|
56
|
-
}
|
|
57
|
-
}
|
package/test/fibonacci.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testFibonacci(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const returnType = builder.getInt32Ty();
|
|
11
|
-
const paramTypes = [builder.getInt32Ty()];
|
|
12
|
-
const functionType = llvm.FunctionType.get(returnType, paramTypes, false);
|
|
13
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'fibonacci', module);
|
|
14
|
-
|
|
15
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
16
|
-
const thenBB = llvm.BasicBlock.Create(context, 'then', func);
|
|
17
|
-
const elseBB = llvm.BasicBlock.Create(context, 'else', func);
|
|
18
|
-
|
|
19
|
-
builder.SetInsertPoint(entryBB);
|
|
20
|
-
const cond = builder.CreateICmpULE(func.getArg(0), builder.getInt32(1), 'cond');
|
|
21
|
-
builder.CreateCondBr(cond, thenBB, elseBB);
|
|
22
|
-
|
|
23
|
-
builder.SetInsertPoint(thenBB);
|
|
24
|
-
builder.CreateRet(func.getArg(0));
|
|
25
|
-
|
|
26
|
-
builder.SetInsertPoint(elseBB);
|
|
27
|
-
const n_1 = builder.CreateSub(func.getArg(0), builder.getInt32(1), 'n_1');
|
|
28
|
-
const n_2 = builder.CreateSub(func.getArg(0), builder.getInt32(2), 'n_2');
|
|
29
|
-
const ret1 = builder.CreateCall(func, [n_1], 'ret1');
|
|
30
|
-
const ret2 = builder.CreateCall(func, [n_2], 'ret2');
|
|
31
|
-
const result = builder.CreateAdd(ret1, ret2);
|
|
32
|
-
builder.CreateRet(result);
|
|
33
|
-
|
|
34
|
-
console.log(module.print());
|
|
35
|
-
|
|
36
|
-
if (llvm.verifyFunction(func)) {
|
|
37
|
-
console.error(`${filename}: verifying the 'fibonacci' function failed`);
|
|
38
|
-
process.exit(1);
|
|
39
|
-
}
|
|
40
|
-
if (llvm.verifyModule(module)) {
|
|
41
|
-
console.error(`${filename}: verifying the module failed`);
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
}
|
package/test/gep.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testGEP(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const returnType = builder.getVoidTy();
|
|
11
|
-
const functionType = llvm.FunctionType.get(returnType, false);
|
|
12
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'gep', module);
|
|
13
|
-
|
|
14
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
15
|
-
builder.SetInsertPoint(entryBB);
|
|
16
|
-
|
|
17
|
-
const strAlloca = builder.CreateAlloca(builder.getInt8PtrTy(), null, 'a');
|
|
18
|
-
const printConst = builder.CreateGlobalString(
|
|
19
|
-
'string content',
|
|
20
|
-
'.str',
|
|
21
|
-
0,
|
|
22
|
-
module
|
|
23
|
-
);
|
|
24
|
-
const ptr = builder.CreateGEP(printConst.getValueType(), printConst, [
|
|
25
|
-
builder.getInt64(0),
|
|
26
|
-
builder.getInt64(0),
|
|
27
|
-
]);
|
|
28
|
-
builder.CreateStore(ptr, strAlloca);
|
|
29
|
-
|
|
30
|
-
builder.CreateRetVoid();
|
|
31
|
-
|
|
32
|
-
console.log(module.print());
|
|
33
|
-
|
|
34
|
-
if (llvm.verifyFunction(func)) {
|
|
35
|
-
console.error(`${filename}: 'verifying the 'gep' function failed`);
|
|
36
|
-
process.exit(1);
|
|
37
|
-
}
|
|
38
|
-
if (llvm.verifyModule(module)) {
|
|
39
|
-
console.error(`${filename}: verifying the module failed`);
|
|
40
|
-
process.exit(1);
|
|
41
|
-
}
|
|
42
|
-
}
|
package/test/index.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import testAdd from './add';
|
|
2
|
-
import testFibonacci from './fibonacci';
|
|
3
|
-
import testVariable from './variable';
|
|
4
|
-
import testUnary from './unary';
|
|
5
|
-
import testStr from './str';
|
|
6
|
-
import testSwitch from './switch';
|
|
7
|
-
import testClass from './class';
|
|
8
|
-
import testLinker from './linker';
|
|
9
|
-
import testBitcodeWriter from './bitcodeWriter';
|
|
10
|
-
import testIntrinsic from './intrinsic';
|
|
11
|
-
import testException from './exception';
|
|
12
|
-
import testType from './type';
|
|
13
|
-
import testDebugInfo from './debugInfo';
|
|
14
|
-
import testTarget from './target';
|
|
15
|
-
import testGEP from './gep';
|
|
16
|
-
import testAttribute from './attribute';
|
|
17
|
-
|
|
18
|
-
testAdd();
|
|
19
|
-
testFibonacci();
|
|
20
|
-
testVariable();
|
|
21
|
-
testUnary();
|
|
22
|
-
testStr();
|
|
23
|
-
testSwitch();
|
|
24
|
-
testClass();
|
|
25
|
-
testLinker();
|
|
26
|
-
testBitcodeWriter();
|
|
27
|
-
testIntrinsic();
|
|
28
|
-
testException();
|
|
29
|
-
testType();
|
|
30
|
-
testDebugInfo();
|
|
31
|
-
testTarget();
|
|
32
|
-
testGEP();
|
|
33
|
-
testAttribute();
|
package/test/intrinsic.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testIntrinsic(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const returnType = builder.getVoidTy();
|
|
11
|
-
const functionType = llvm.FunctionType.get(returnType, false);
|
|
12
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'intrinsic', module);
|
|
13
|
-
|
|
14
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
15
|
-
builder.SetInsertPoint(entryBB);
|
|
16
|
-
const debugtrapFn = llvm.Intrinsic.getDeclaration(module, llvm.Intrinsic.debugtrap);
|
|
17
|
-
builder.CreateCall(debugtrapFn, []);
|
|
18
|
-
const memmoveFn = llvm.Intrinsic.getDeclaration(module, llvm.Intrinsic.memmove, [
|
|
19
|
-
builder.getInt8PtrTy(),
|
|
20
|
-
builder.getInt8PtrTy(),
|
|
21
|
-
builder.getInt32Ty()
|
|
22
|
-
]);
|
|
23
|
-
builder.CreateRetVoid();
|
|
24
|
-
|
|
25
|
-
console.log(module.print());
|
|
26
|
-
|
|
27
|
-
if (llvm.verifyFunction(func)) {
|
|
28
|
-
console.error(`${filename}: verifying the 'intrinsic' function failed`);
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
if (llvm.verifyModule(module)) {
|
|
32
|
-
console.error(`${filename}: verifying the module failed`);
|
|
33
|
-
process.exit(1);
|
|
34
|
-
}
|
|
35
|
-
}
|
package/test/linker.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testLinker(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
const bitcodePath = path.join(__dirname, 'bitcode/add.bc');
|
|
11
|
-
const err = new llvm.SMDiagnostic();
|
|
12
|
-
const srcModule = llvm.parseIRFile(bitcodePath, err, context);
|
|
13
|
-
if (llvm.verifyModule(srcModule)) {
|
|
14
|
-
console.error(`${filename}: verifying the source module failed`);
|
|
15
|
-
process.exit(1);
|
|
16
|
-
}
|
|
17
|
-
if (llvm.Linker.linkModules(module, srcModule)) {
|
|
18
|
-
console.error(`${filename}: linking the source modules to the destination module failed`);
|
|
19
|
-
process.exit(1);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const returnType = builder.getInt32Ty();
|
|
23
|
-
const functionType = llvm.FunctionType.get(returnType, false);
|
|
24
|
-
const func = llvm.Function.Create(functionType, llvm.Function.LinkageTypes.ExternalLinkage, 'linker', module);
|
|
25
|
-
|
|
26
|
-
const entryBB = llvm.BasicBlock.Create(context, 'entry', func);
|
|
27
|
-
builder.SetInsertPoint(entryBB);
|
|
28
|
-
const addFunc = module.getFunction('add');
|
|
29
|
-
if (!addFunc) {
|
|
30
|
-
console.error(`${filename}: unable to get the 'add' function`);
|
|
31
|
-
process.exit(1);
|
|
32
|
-
}
|
|
33
|
-
const argA = builder.getInt32(1);
|
|
34
|
-
const argB = builder.getInt32(2);
|
|
35
|
-
const retVal = builder.CreateCall(addFunc, [argA, argB]);
|
|
36
|
-
builder.CreateRet(retVal);
|
|
37
|
-
|
|
38
|
-
console.log(module.print());
|
|
39
|
-
|
|
40
|
-
if (llvm.verifyFunction(func)) {
|
|
41
|
-
console.error(`${filename}: verifying the 'linker' function failed`);
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
if (llvm.verifyModule(module)) {
|
|
45
|
-
console.error(`${filename}: verifying the module failed`);
|
|
46
|
-
process.exit(1);
|
|
47
|
-
}
|
|
48
|
-
}
|
package/test/str.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import llvm from '..';
|
|
3
|
-
|
|
4
|
-
export default function testStr(): void {
|
|
5
|
-
const filename = path.basename(__filename);
|
|
6
|
-
const context = new llvm.LLVMContext();
|
|
7
|
-
const module = new llvm.Module(filename, context);
|
|
8
|
-
const builder = new llvm.IRBuilder(context);
|
|
9
|
-
|
|
10
|
-
builder.CreateGlobalString('HelloWorld', 'str', 0, module);
|
|
11
|
-
builder.CreateGlobalStringPtr('Bye Bye', 'str', 0, module);
|
|
12
|
-
|
|
13
|
-
console.log(module.print());
|
|
14
|
-
|
|
15
|
-
if (llvm.verifyModule(module)) {
|
|
16
|
-
console.error(`${filename}: verifying the module failed`);
|
|
17
|
-
process.exit(1);
|
|
18
|
-
}
|
|
19
|
-
}
|