Skip to content

Commit 603a776

Browse files
committed
Feature: Initial support for unint8/16/32/64/128 type
1 parent 672ac12 commit 603a776

5 files changed

Lines changed: 110 additions & 23 deletions

File tree

src/backend/llvm/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {CPPMangler} from "./cpp.mangler";
55
import {Context} from "./context";
66
import {NativeTypeResolver} from "./native-type-resolver";
77
import UnsupportedError from "../error/unsupported.error";
8+
import {NativeType} from "./native-type";
89

910
export function passReturnStatement(parent: ts.ReturnStatement, ctx: Context, builder: llvm.IRBuilder) {
1011
if (!parent.expression) {
@@ -74,13 +75,19 @@ function buildFromNumericLiteral(
7475
value: ts.NumericLiteral,
7576
ctx: Context,
7677
builder: llvm.IRBuilder,
77-
lType?: llvm.Type
78+
nativeType?: NativeType
7879
): llvm.Value {
79-
if (!lType || lType.isDoubleTy()) {
80+
if (!nativeType || nativeType.getType().isDoubleTy()) {
8081
return llvm.ConstantFP.get(ctx.llvmContext, parseFloat(value.text));
8182
}
8283

83-
return llvm.ConstantInt.get(ctx.llvmContext, parseInt(value.text), (<llvm.IntegerType>lType).getBitWidth());
84+
const type = nativeType.getType();
85+
return llvm.ConstantInt.get(
86+
ctx.llvmContext,
87+
parseInt(value.text),
88+
(<llvm.IntegerType>type).getBitWidth(),
89+
nativeType.isSigned()
90+
);
8491
}
8592

8693
function buildFromBinaryExpression(
@@ -195,12 +202,12 @@ function buildFromIdentifier(identifier: ts.Identifier, ctx: Context, builder: l
195202
}
196203

197204

198-
function buildFromExpression(block: ts.Expression, ctx: Context, builder: llvm.IRBuilder, lType?: llvm.Type): llvm.Value {
205+
function buildFromExpression(block: ts.Expression, ctx: Context, builder: llvm.IRBuilder, nativeType?: NativeType): llvm.Value {
199206
switch (block.kind) {
200207
case ts.SyntaxKind.Identifier:
201208
return buildFromIdentifier(<any>block, ctx, builder);
202209
case ts.SyntaxKind.NumericLiteral:
203-
return buildFromNumericLiteral(<any>block, ctx, builder, lType);
210+
return buildFromNumericLiteral(<any>block, ctx, builder, nativeType);
204211
case ts.SyntaxKind.StringLiteral:
205212
return buildFromStringValue(<any>block, ctx, builder);
206213
case ts.SyntaxKind.BinaryExpression:
@@ -224,16 +231,16 @@ export function passVariableDeclaration(block: ts.VariableDeclaration, ctx: Cont
224231
if (block.initializer) {
225232
const type = ctx.typeChecker.getTypeAtLocation(block);
226233

227-
const llvmType = NativeTypeResolver.getType(
234+
const nativeType = NativeTypeResolver.getType(
228235
type,
229236
ctx
230237
);
231238

232-
const defaultValue = buildFromExpression(block.initializer, ctx, builder, llvmType);
239+
const defaultValue = buildFromExpression(block.initializer, ctx, builder, nativeType);
233240

234241
if (block.name.kind == ts.SyntaxKind.Identifier) {
235242
const allocate = builder.createAlloca(
236-
llvmType,
243+
nativeType.getType(),
237244
undefined,
238245
<string>block.name.escapedText
239246
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//
2+
// export interface ManglerInterface {
3+
// getFunctionName(name: string): string;
4+
// }

src/backend/llvm/native-type-resolver.ts

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22
import * as ts from 'typescript';
33
import * as llvm from 'llvm-node';
44
import {Context} from "./context";
5+
import {NativeType} from "./native-type";
56

67
export class NativeTypeResolver {
7-
static getType(type: ts.Type, ctx: Context): llvm.Type {
8+
static getType(type: ts.Type, ctx: Context): NativeType {
89
if (type.isLiteral()) {
910
if (type.isNumberLiteral()) {
10-
return llvm.Type.getDoubleTy(
11-
ctx.llvmContext
11+
return new NativeType(
12+
llvm.Type.getDoubleTy(
13+
ctx.llvmContext
14+
)
1215
);
1316
}
1417

1518
if (type.isStringLiteral()) {
16-
return llvm.Type.getInt8PtrTy(
17-
ctx.llvmContext
19+
return new NativeType(
20+
llvm.Type.getInt8PtrTy(
21+
ctx.llvmContext
22+
)
1823
);
1924
}
2025

@@ -27,24 +32,69 @@ export class NativeTypeResolver {
2732
if (aliasSymbol) {
2833
switch (aliasSymbol.escapedName) {
2934
case 'int8':
30-
return llvm.Type.getInt8Ty(
31-
ctx.llvmContext
35+
return new NativeType(
36+
llvm.Type.getInt8Ty(
37+
ctx.llvmContext
38+
)
39+
);
40+
case 'uint8':
41+
return new NativeType(
42+
llvm.Type.getInt8Ty(
43+
ctx.llvmContext
44+
),
45+
true
3246
);
3347
case 'int16':
34-
return llvm.Type.getInt16Ty(
35-
ctx.llvmContext
48+
return new NativeType(
49+
llvm.Type.getInt16Ty(
50+
ctx.llvmContext
51+
)
52+
);
53+
case 'uint16':
54+
return new NativeType(
55+
llvm.Type.getInt16Ty(
56+
ctx.llvmContext
57+
),
58+
true
3659
);
3760
case 'int32':
38-
return llvm.Type.getInt32Ty(
39-
ctx.llvmContext
61+
return new NativeType(
62+
llvm.Type.getInt32Ty(
63+
ctx.llvmContext
64+
)
65+
);
66+
case 'uint32':
67+
return new NativeType(
68+
llvm.Type.getInt32Ty(
69+
ctx.llvmContext
70+
),
71+
true
4072
);
4173
case 'int64':
42-
return llvm.Type.getInt64Ty(
43-
ctx.llvmContext
74+
return new NativeType(
75+
llvm.Type.getInt64Ty(
76+
ctx.llvmContext
77+
)
78+
);
79+
case 'uint64':
80+
return new NativeType(
81+
llvm.Type.getInt64Ty(
82+
ctx.llvmContext
83+
),
84+
true
4485
);
4586
case 'int128':
46-
return llvm.Type.getInt128Ty(
47-
ctx.llvmContext
87+
return new NativeType(
88+
llvm.Type.getInt128Ty(
89+
ctx.llvmContext
90+
)
91+
);
92+
case 'uint128':
93+
return new NativeType(
94+
llvm.Type.getInt128Ty(
95+
ctx.llvmContext
96+
),
97+
true
4898
);
4999
default:
50100
throw new Error(

src/backend/llvm/native-type.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import * as llvm from 'llvm-node';
3+
4+
export class NativeType {
5+
protected llvmType: llvm.Type;
6+
protected signed: boolean;
7+
8+
public constructor(llvmType: llvm.Type, signed: boolean = false) {
9+
this.llvmType = llvmType;
10+
this.signed = signed;
11+
}
12+
13+
public getType(): llvm.Type {
14+
return this.llvmType;
15+
}
16+
17+
public isSigned(): boolean {
18+
return this.signed;
19+
}
20+
}

staticscript.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ declare type int16 = {};
2222
declare type int32 = {};
2323
declare type int64 = {};
2424
declare type int128 = {};
25+
26+
declare type uint8 = {};
27+
declare type uint16 = {};
28+
declare type uint32 = {};
29+
declare type uint64 = {};
30+
declare type uint128 = {};

0 commit comments

Comments
 (0)