mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-18 17:31:29 +00:00
Type limits
This commit is contained in:
@ -3,7 +3,7 @@ import { DiagnosticCode } from "./diagnostics";
|
||||
import { Node, Expression } from "./ast";
|
||||
import { Type } from "./types";
|
||||
import { Module, ExpressionRef, UnaryOp, BinaryOp, HostOp, NativeType, FunctionTypeRef } from "./module";
|
||||
import { Program, ElementFlags, FunctionPrototype, Local } from "./program";
|
||||
import { Program, ElementFlags, Element, Global, FunctionPrototype, Local } from "./program";
|
||||
|
||||
/** Initializes the specified program with built-in functions. */
|
||||
export function initialize(program: Program): void {
|
||||
@ -35,14 +35,67 @@ export function initialize(program: Program): void {
|
||||
addFunction(program, "assert");
|
||||
addFunction(program, "parseInt");
|
||||
addFunction(program, "parseFloat");
|
||||
|
||||
addFunction(program, "i8").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "i8.MIN_VALUE", null, Type.i8).withConstantIntegerValue(-128, -1) ],
|
||||
[ "MAX_VALUE", new Global(program, "i8.MAX_VALUE", null, Type.i8).withConstantIntegerValue(127, 0) ]
|
||||
]);
|
||||
addFunction(program, "i16").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "i16.MIN_VALUE", null, Type.i16).withConstantIntegerValue(-32768, -1) ],
|
||||
[ "MAX_VALUE", new Global(program, "i16.MAX_VALUE", null, Type.i16).withConstantIntegerValue(32767, 0) ]
|
||||
]);
|
||||
addFunction(program, "i32").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "i32.MIN_VALUE", null, Type.i32).withConstantIntegerValue(-2147483648, -1) ],
|
||||
[ "MAX_VALUE", new Global(program, "i32.MAX_VALUE", null, Type.i32).withConstantIntegerValue(2147483647, 0) ]
|
||||
]);
|
||||
addFunction(program, "i64").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "i64.MIN_VALUE", null, Type.i64).withConstantIntegerValue(0, -2147483648) ],
|
||||
[ "MAX_VALUE", new Global(program, "i64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, 2147483647) ]
|
||||
]);
|
||||
addFunction(program, "u8").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "u8.MIN_VALUE", null, Type.u8).withConstantIntegerValue(0, 0) ],
|
||||
[ "MAX_VALUE", new Global(program, "u8.MAX_VALUE", null, Type.u8).withConstantIntegerValue(255, 0) ]
|
||||
]);
|
||||
addFunction(program, "u16").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "u16.MIN_VALUE", null, Type.u16).withConstantIntegerValue(0, 0) ],
|
||||
[ "MAX_VALUE", new Global(program, "u16.MAX_VALUE", null, Type.u16).withConstantIntegerValue(65535, 0) ]
|
||||
]);
|
||||
addFunction(program, "u32").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "u32.MIN_VALUE", null, Type.u32).withConstantIntegerValue(0, 0) ],
|
||||
[ "MAX_VALUE", new Global(program, "u32.MAX_VALUE", null, Type.u32).withConstantIntegerValue(-1, 0) ]
|
||||
]);
|
||||
addFunction(program, "u64").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "u64.MIN_VALUE", null, Type.u64).withConstantIntegerValue(0, 0) ],
|
||||
[ "MAX_VALUE", new Global(program, "u64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, -1) ]
|
||||
]);
|
||||
addFunction(program, "bool").members = new Map([
|
||||
[ "MIN_VALUE", new Global(program, "bool.MIN_VALUE", null, Type.bool).withConstantIntegerValue(0, 0) ],
|
||||
[ "MAX_VALUE", new Global(program, "bool.MAX_VALUE", null, Type.bool).withConstantIntegerValue(1, 0) ]
|
||||
]);
|
||||
addFunction(program, "f32").members = new Map([
|
||||
[ "MIN_SAFE_INTEGER", new Global(program, "f32.MIN_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(-16777215) ],
|
||||
[ "MAX_SAFE_INTEGER", new Global(program, "f32.MAX_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(16777215) ]
|
||||
]);
|
||||
addFunction(program, "f64").members = new Map([
|
||||
[ "MIN_SAFE_INTEGER", new Global(program, "f64.MIN_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(-9007199254740991) ],
|
||||
[ "MAX_SAFE_INTEGER", new Global(program, "f64.MAX_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(9007199254740991) ]
|
||||
]);
|
||||
if (program.target == Target.WASM64) {
|
||||
program.elements.set("isize", <Element>program.elements.get("i64"));
|
||||
program.elements.set("usize", <Element>program.elements.get("u64"));
|
||||
} else {
|
||||
program.elements.set("isize", <Element>program.elements.get("i32"));
|
||||
program.elements.set("usize", <Element>program.elements.get("u32"));
|
||||
}
|
||||
}
|
||||
|
||||
/** Adds a built-in function to the specified program. */
|
||||
function addFunction(program: Program, name: string, isGeneric: bool = false): void {
|
||||
let prototype: FunctionPrototype = new FunctionPrototype(program, name, null, null);
|
||||
function addFunction(program: Program, name: string, isGeneric: bool = false): FunctionPrototype {
|
||||
let prototype: FunctionPrototype = new FunctionPrototype(program, name, name, null, null);
|
||||
prototype.isBuiltIn = true;
|
||||
if (isGeneric) prototype.isGeneric = true;
|
||||
program.elements.set(name, prototype);
|
||||
return prototype;
|
||||
}
|
||||
|
||||
/** Compiles a call to a built-in function. */
|
||||
|
Reference in New Issue
Block a user