Skip to content

Commit a2fbb0d

Browse files
committed
Feature(compiler): Initial support for TypedArray(s) inside backend
1 parent 9ba9634 commit a2fbb0d

3 files changed

Lines changed: 79 additions & 8 deletions

File tree

src/backend/llvm/code-generation/array-literal-expression.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@ export class ArrayLiteralExpressionCodeGenerator implements NodeGenerateInterfac
1111
// const elementType = NativeTypeResolver.getType(type, ctx);
1212

1313
// @todo Fix this hardcore...
14-
const elementType = llvm.Type.getDoublePtrTy(ctx.llvmContext);
15-
16-
const structType = llvm.StructType.create(ctx.llvmContext, 'array<int>');
17-
structType.setBody([
18-
elementType,
19-
llvm.Type.getInt32Ty(ctx.llvmContext),
20-
]);
14+
const structType = ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
15+
llvm.Type.getDoubleTy(ctx.llvmContext),
16+
ctx,
17+
'array<double>'
18+
);
2119

2220
const allocate = builder.createAlloca(
2321
structType
2422
);
2523

2624
return new ArrayReference(
27-
elementType,
25+
structType.getElementType(0),
2826
allocate
2927
);
3028
}
29+
30+
static buildTypedArrayStructLLVMType(elementType: llvm.Type, ctx: Context, name: string): llvm.StructType {
31+
const structType = llvm.StructType.create(ctx.llvmContext, name);
32+
structType.setBody([
33+
elementType,
34+
llvm.Type.getInt32Ty(ctx.llvmContext),
35+
]);
36+
37+
return structType;
38+
}
3139
}

src/backend/llvm/context.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as ts from "typescript";
33
import * as llvm from "llvm-node";
44
import {Scope} from "./scope";
5+
import {ArrayLiteralExpressionCodeGenerator} from "./code-generation/array-literal-expression";
56

67
export class SignatureToFunctionTable extends Map<ts.Signature, llvm.Function> {
78

@@ -20,5 +21,53 @@ export class Context {
2021
this.typeChecker = typeChecker;
2122
this.llvmContext = new llvm.LLVMContext();
2223
this.llvmModule = new llvm.Module("test", this.llvmContext);
24+
25+
this.scope.classes.set('Uint8Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
26+
llvm.Type.getInt8Ty(this.llvmContext),
27+
this,
28+
'array<uint8>'
29+
));
30+
31+
this.scope.classes.set('Int8Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
32+
llvm.Type.getInt8Ty(this.llvmContext),
33+
this,
34+
'array<int8>'
35+
));
36+
37+
this.scope.classes.set('Int16Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
38+
llvm.Type.getInt16Ty(this.llvmContext),
39+
this,
40+
'array<int16>'
41+
));
42+
43+
this.scope.classes.set('Uint16Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
44+
llvm.Type.getInt16Ty(this.llvmContext),
45+
this,
46+
'array<uint16>'
47+
));
48+
49+
this.scope.classes.set('Int32Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
50+
llvm.Type.getInt32Ty(this.llvmContext),
51+
this,
52+
'array<int32>'
53+
));
54+
55+
this.scope.classes.set('Uint32Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
56+
llvm.Type.getInt32Ty(this.llvmContext),
57+
this,
58+
'array<uint32>'
59+
));
60+
61+
this.scope.classes.set('Float32Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
62+
llvm.Type.getFloatTy(this.llvmContext),
63+
this,
64+
'array<float32>'
65+
));
66+
67+
this.scope.classes.set('Float64Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
68+
llvm.Type.getDoubleTy(this.llvmContext),
69+
this,
70+
'array<float64>'
71+
));
2372
}
2473
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
{
3+
let int8Array = new Int8Array(5);
4+
let uint8Array = new Uint8Array(5);
5+
6+
let int16Array = new Int16Array(5);
7+
let uint16Array = new Uint16Array(5);
8+
9+
let int32Array = new Int32Array(5);
10+
let uint32Array = new Uint32Array(5);
11+
12+
let float32Array = new Float32Array(5);
13+
let float64Array = new Float64Array(5);
14+
}

0 commit comments

Comments
 (0)