forked from ovr/StaticScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
101 lines (83 loc) · 3.4 KB
/
context.ts
File metadata and controls
101 lines (83 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as ts from "typescript";
import * as llvm from "llvm-node";
import {Scope} from "./scope";
import {ArrayLiteralExpressionCodeGenerator} from "./code-generation/array-literal-expression";
export class SignatureToFunctionTable extends Map<ts.Signature, llvm.Function> {
}
export class Context {
public typeChecker: ts.TypeChecker;
public llvmContext: llvm.LLVMContext;
public llvmModule: llvm.Module;
public signature: SignatureToFunctionTable = new SignatureToFunctionTable();
public scope: Scope = new Scope();
public constructor(typeChecker: ts.TypeChecker) {
this.typeChecker = typeChecker;
this.llvmContext = new llvm.LLVMContext();
this.llvmModule = new llvm.Module("test", this.llvmContext);
this.scope.classes.set('Uint8Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getInt8Ty(this.llvmContext),
this,
'array<uint8>'
));
this.scope.classes.set('Int8Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getInt8Ty(this.llvmContext),
this,
'array<int8>'
));
this.scope.classes.set('Int16Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getInt16Ty(this.llvmContext),
this,
'array<int16>'
));
this.scope.classes.set('Uint16Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getInt16Ty(this.llvmContext),
this,
'array<uint16>'
));
this.scope.classes.set('Int32Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getInt32Ty(this.llvmContext),
this,
'array<int32>'
));
this.scope.classes.set('Uint32Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getInt32Ty(this.llvmContext),
this,
'array<uint32>'
));
this.scope.classes.set('Float32Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getFloatTy(this.llvmContext),
this,
'array<float32>'
));
this.scope.classes.set('Float64Array', ArrayLiteralExpressionCodeGenerator.buildTypedArrayStructLLVMType(
llvm.Type.getDoubleTy(this.llvmContext),
this,
'array<float64>'
));
}
getIntrinsic(functionName: string): llvm.Function {
const moduleFn = this.llvmModule.getFunction(functionName);
if (moduleFn) {
return moduleFn;
}
switch (functionName) {
case 'llvm.pow.f64':
const intrinsicType = llvm.FunctionType.get(
llvm.Type.getDoubleTy(this.llvmContext),
[
llvm.Type.getDoubleTy(this.llvmContext),
llvm.Type.getDoubleTy(this.llvmContext),
],
false
);
return llvm.Function.create(
intrinsicType,
llvm.LinkageTypes.ExternalLinkage,
functionName,
this.llvmModule
);
default:
throw new Error(`Unknown intrinsic: "${functionName}"`);
}
}
}