assemblyscript/src/types.ts

253 lines
7.8 KiB
TypeScript
Raw Normal View History

import {
Target
} from "./compiler";
2017-12-24 03:19:47 +01:00
import {
Class,
Function
} from "./program";
import {
sb
} from "./util/sb";
import {
NativeType,
ExpressionRef,
Module
2017-12-24 03:19:47 +01:00
} from "./module";
2017-10-07 14:29:43 +02:00
2017-12-16 17:54:53 +01:00
/** Indicates the kind of a type. */
2017-10-02 12:52:15 +02:00
export const enum TypeKind {
// signed integers
I8,
I16,
I32,
I64,
ISIZE,
// unsigned integers
U8,
U16,
U32,
U64,
USIZE,
BOOL, // sic
// floats
F32,
F64,
2017-12-16 17:54:53 +01:00
// other
2017-10-02 12:52:15 +02:00
VOID
}
2017-12-16 17:54:53 +01:00
/** Represents a resolved type. */
2017-10-02 12:52:15 +02:00
export class Type {
2017-12-16 17:54:53 +01:00
/** Type kind. */
2017-10-02 12:52:15 +02:00
kind: TypeKind;
2017-12-16 17:54:53 +01:00
/** Size in bits. */
2017-10-02 12:52:15 +02:00
size: i32;
2017-12-16 17:54:53 +01:00
/** Size in bytes. */
2017-12-04 02:00:48 +01:00
byteSize: i32;
2017-12-16 17:54:53 +01:00
/** Underlying class type, if a class type. */
2017-11-17 14:33:51 +01:00
classType: Class | null;
2017-12-16 17:54:53 +01:00
/** Underlying function type, if a function type. */
functionType: Function | null;
/** Whether nullable or not. */
isNullable: bool = false;
/** Respective nullable type, if nullable. */
nullableType: Type | null = null;
2017-10-02 12:52:15 +02:00
2017-12-16 17:54:53 +01:00
/** Constructs a new resolved type. */
2017-10-02 12:52:15 +02:00
constructor(kind: TypeKind, size: i32) {
this.kind = kind;
this.size = size;
this.byteSize = <i32>ceil<f64>(<f64>size / 8);
2017-10-02 12:52:15 +02:00
this.classType = null;
}
2017-12-16 17:54:53 +01:00
/** Sign-extending 32-bit shift, if a small signed integer. */
2017-10-02 12:52:15 +02:00
get smallIntegerShift(): i32 { return 32 - this.size; }
2017-12-16 17:54:53 +01:00
/** Truncating 32-bit mask, if a small unsigned integer. */
2017-10-02 12:52:15 +02:00
get smallIntegerMask(): i32 { return -1 >>> (32 - this.size); }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any integer kind. */
2017-10-02 12:52:15 +02:00
get isAnyInteger(): bool { return this.kind >= TypeKind.I8 && this.kind <= TypeKind.BOOL; }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any small integer kind. */
2017-10-02 12:52:15 +02:00
get isSmallInteger(): bool { return this.size != 0 && this.size < 32; }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any long integer kind. */
2017-10-02 12:52:15 +02:00
get isLongInteger(): bool { return this.size == 64 && this.kind != TypeKind.F64; }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any unsigned integer kind. */
2017-10-02 12:52:15 +02:00
get isUnsignedInteger(): bool { return this.kind >= TypeKind.U8 && this.kind <= TypeKind.BOOL; }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any signed integer kind. */
2017-10-02 12:52:15 +02:00
get isSignedInteger(): bool { return this.kind >= TypeKind.I8 && this.kind <= TypeKind.ISIZE; }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any size kind, i.e., `isize` or `usize`. */
2017-10-02 12:52:15 +02:00
get isAnySize(): bool { return this.kind == TypeKind.ISIZE || this.kind == TypeKind.USIZE; }
2017-12-16 17:54:53 +01:00
/** Tests if this type is of any float kind, i.e., `f32` or `f64`. */
2017-10-02 12:52:15 +02:00
get isAnyFloat(): bool { return this.kind == TypeKind.F32 || this.kind == TypeKind.F64; }
/** Tests if this type is a class type. */
get isClass(): bool { return this.classType != null; }
/** Tests if this type is a function type. */
get isFunction(): bool { return this.functionType != null; }
2017-10-02 12:52:15 +02:00
2017-12-16 17:54:53 +01:00
/** Composes a class type from this type and a class. */
2017-11-17 14:33:51 +01:00
asClass(classType: Class): Type {
2017-12-16 17:54:53 +01:00
assert(this.kind == TypeKind.USIZE);
var ret = new Type(this.kind, this.size);
2017-10-02 12:52:15 +02:00
ret.classType = classType;
return ret;
}
2017-12-16 17:54:53 +01:00
/** Composes a function type from this type and a function. */
asFunction(functionType: Function): Type {
assert(this.kind == TypeKind.USIZE);
var ret = new Type(this.kind, this.size);
2017-12-16 17:54:53 +01:00
ret.functionType = functionType;
return ret;
}
/** Composes the respective nullable type of this type. */
2017-12-18 03:46:36 +01:00
asNullable(): Type | null {
2017-12-16 17:54:53 +01:00
assert(this.kind == TypeKind.USIZE);
if (this.isNullable && !this.nullableType)
(this.nullableType = new Type(this.kind, this.size)).isNullable = true;
2017-12-18 03:46:36 +01:00
return this.nullableType;
2017-10-02 12:52:15 +02:00
}
2017-12-16 17:54:53 +01:00
/** Converts this type to its TypeScript representation. */
2017-10-07 14:29:43 +02:00
toString(kindOnly: bool = false): string {
2017-10-02 12:52:15 +02:00
switch (this.kind) {
case TypeKind.I8: return "i8";
case TypeKind.I16: return "i16";
case TypeKind.I32: return "i32";
2017-12-01 02:08:03 +01:00
case TypeKind.I64: return "i64";
2017-10-02 12:52:15 +02:00
case TypeKind.ISIZE: return "isize";
case TypeKind.U8: return "u8";
case TypeKind.U16: return "u16";
case TypeKind.U32: return "u32";
case TypeKind.U64: return "u64";
2017-12-16 17:54:53 +01:00
case TypeKind.USIZE:
if (kindOnly) return "usize";
return this.classType ? this.classType.toString()
: this.functionType ? this.functionType.toTypeString()
: "usize";
2017-10-02 12:52:15 +02:00
case TypeKind.BOOL: return "bool";
case TypeKind.F32: return "f32";
case TypeKind.F64: return "f64";
case TypeKind.VOID: return "void";
2017-12-18 03:46:36 +01:00
default: assert(false); return "";
2017-10-02 12:52:15 +02:00
}
}
2017-12-24 03:19:47 +01:00
// Binaryen specific
/** Converts this type to its respective native type. */
toNativeType(): NativeType {
return this.kind == TypeKind.F32 ? NativeType.F32
: this.kind == TypeKind.F64 ? NativeType.F64
: this.isLongInteger ? NativeType.I64
: this.isAnyInteger ? NativeType.I32
: NativeType.None;
2017-12-24 03:19:47 +01:00
}
/** Converts this type to its native `0` value. */
toNativeZero(module: Module): ExpressionRef {
return this.kind == TypeKind.F32 ? module.createF32(0)
: this.kind == TypeKind.F64 ? module.createF64(0)
: this.isLongInteger ? module.createI64(0, 0)
: module.createI32(0);
}
/** Converts this type to its native `1` value. */
toNativeOne(module: Module): ExpressionRef {
return this.kind == TypeKind.F32 ? module.createF32(1)
: this.kind == TypeKind.F64 ? module.createF64(1)
: this.isLongInteger ? module.createI64(1, 0)
: module.createI32(1);
}
/** Converts this type to its signature string. */
toSignatureString(): string {
switch (this.kind) {
default:
return "i";
case TypeKind.I64:
case TypeKind.U64:
return "I";
case TypeKind.ISIZE:
case TypeKind.USIZE:
return select<string>("I", "i", this.size == 64);
case TypeKind.F32:
return "f";
case TypeKind.F64:
return "F";
case TypeKind.VOID:
return "v";
}
2017-12-24 03:19:47 +01:00
}
// Types
2017-12-16 17:54:53 +01:00
/** An 8-bit signed integer. */
2017-10-02 12:52:15 +02:00
static readonly i8: Type = new Type(TypeKind.I8, 8);
2017-12-16 17:54:53 +01:00
/** A 16-bit signed integer. */
2017-10-02 12:52:15 +02:00
static readonly i16: Type = new Type(TypeKind.I16, 16);
2017-12-16 17:54:53 +01:00
/** A 32-bit signed integer. */
2017-10-02 12:52:15 +02:00
static readonly i32: Type = new Type(TypeKind.I32, 32);
2017-12-16 17:54:53 +01:00
/** A 64-bit signed integer. */
2017-10-02 12:52:15 +02:00
static readonly i64: Type = new Type(TypeKind.I64, 64);
2017-12-16 17:54:53 +01:00
/** A 32-bit signed size. WASM32 only. */
static readonly isize32: Type = new Type(TypeKind.ISIZE, 32);
2017-12-16 17:54:53 +01:00
/** A 64-bit signed size. WASM64 only. */
static readonly isize64: Type = new Type(TypeKind.ISIZE, 64);
2017-12-16 17:54:53 +01:00
/** An 8-bit unsigned integer. */
2017-10-02 12:52:15 +02:00
static readonly u8: Type = new Type(TypeKind.U8, 8);
2017-12-16 17:54:53 +01:00
/** A 16-bit unsigned integer. */
2017-10-02 12:52:15 +02:00
static readonly u16: Type = new Type(TypeKind.U16, 16);
2017-12-16 17:54:53 +01:00
/** A 32-bit unsigned integer. */
2017-10-02 12:52:15 +02:00
static readonly u32: Type = new Type(TypeKind.U32, 32);
2017-12-16 17:54:53 +01:00
/** A 64-bit unsigned integer. */
2017-10-02 12:52:15 +02:00
static readonly u64: Type = new Type(TypeKind.U64, 64);
2017-12-16 17:54:53 +01:00
/** A 32-bit unsigned size. WASM32 only. */
static readonly usize32: Type = new Type(TypeKind.USIZE, 32);
2017-12-16 17:54:53 +01:00
/** A 64-bit unsigned size. WASM64 only. */
static readonly usize64: Type = new Type(TypeKind.USIZE, 64);
2017-12-16 17:54:53 +01:00
/** A 1-bit unsigned integer. */
2017-10-02 12:52:15 +02:00
static readonly bool: Type = new Type(TypeKind.BOOL, 1);
2017-12-16 17:54:53 +01:00
/** A 32-bit float. */
2017-10-02 12:52:15 +02:00
static readonly f32: Type = new Type(TypeKind.F32, 32);
2017-12-16 17:54:53 +01:00
/** A 64-bit float. */
2017-10-02 12:52:15 +02:00
static readonly f64: Type = new Type(TypeKind.F64, 64);
2017-12-16 17:54:53 +01:00
/** No return type. */
2017-10-02 12:52:15 +02:00
static readonly void: Type = new Type(TypeKind.VOID, 0);
}
2017-12-24 03:19:47 +01:00
/** Converts an array of types to an array of native types. */
export function typesToNativeTypes(types: Type[]): NativeType[] {
var k = types.length;
var ret = new Array<NativeType>(k);
for (var i = 0; i < k; ++i)
2017-12-24 03:19:47 +01:00
ret[i] = types[i].toNativeType();
return ret;
}
2017-12-16 17:54:53 +01:00
/** Converts an array of types to its combined string representation. Usually type arguments. */
export function typesToString(types: Type[]): string {
var k = types.length;
2017-10-07 14:29:43 +02:00
if (!k)
return "";
var sb = new Array<string>(k);
for (var i = 0; i < k; ++i)
sb[i] = types[i].toString();
return sb.join(", ");
2017-10-07 14:29:43 +02:00
}