Add compiler hints

This commit is contained in:
dcodeIO
2018-06-29 00:14:42 +02:00
parent 3ed4f3331e
commit 82da2d1f6d
15 changed files with 189 additions and 57 deletions

View File

@ -177,15 +177,20 @@ export class Options {
importMemory: bool = false;
/** If true, imports the function table provided by the embedder. */
importTable: bool = false;
/** Static memory start offset. */
memoryBase: u32 = 0;
/** If true, generates information necessary for source maps. */
sourceMap: bool = false;
/** Static memory start offset. */
memoryBase: i32 = 0;
/** Global aliases. */
globalAliases: Map<string,string> | null = null;
/** Additional features to activate. */
features: Feature = Feature.NONE;
/** Hinted optimize level. Not applied by the compiler itself. */
optimizeLevelHint: i32 = 0;
/** Hinted shrink level. Not applied by the compiler itself. */
shrinkLevelHint: i32 = 0;
/** Tests if the target is WASM64 or, otherwise, WASM32. */
get isWasm64(): bool {
return this.target == Target.WASM64;
@ -251,8 +256,8 @@ export class Compiler extends DiagnosticEmitter {
module: Module;
/** Current function in compilation. */
currentFunction: Function;
/** Outer function in compilation, if compiling a function expression. */
outerFunction: Function | null = null;
/** Current outer function in compilation, if compiling a function expression. */
currentOuterFunction: Function | null = null;
/** Current enum in compilation. */
currentEnum: Enum | null = null;
/** Current type in compilation. */
@ -2045,13 +2050,20 @@ export class Compiler extends DiagnosticEmitter {
let local = new Local(program, name, -1, type);
switch (getExpressionType(initExpr)) {
case NativeType.I32: {
local = local.withConstantIntegerValue(getConstValueI32(initExpr), 0);
local = local.withConstantIntegerValue(
i64_new(
getConstValueI32(initExpr),
0
)
);
break;
}
case NativeType.I64: {
local = local.withConstantIntegerValue(
getConstValueI64Low(initExpr),
getConstValueI64High(initExpr)
i64_new(
getConstValueI64Low(initExpr),
getConstValueI64High(initExpr)
)
);
break;
}

View File

@ -134,6 +134,12 @@ export function enableFeature(options: Options, feature: Feature): void {
options.features |= feature;
}
/** Gives the compiler a hint at the optimize levels that will be used later on. */
export function setOptimizeLevelHints(options: Options, optimizeLevel: i32, shrinkLevel: i32): void {
options.optimizeLevelHint = optimizeLevel;
options.shrinkLevelHint = shrinkLevel;
}
/** Finishes parsing. */
export function finishParsing(parser: Parser): Program {
return parser.finish();

View File

@ -15,7 +15,8 @@ import {
} from "./common";
import {
Options
Options,
Feature
} from "./compiler";
import {
@ -371,6 +372,8 @@ export class Program extends DiagnosticEmitter {
/** Initializes the program and its elements prior to compilation. */
initialize(options: Options): void {
this.options = options;
// add built-in types
this.typesLookup = new Map([
["i8", Type.i8],
["i16", Type.i16],
@ -390,6 +393,25 @@ export class Program extends DiagnosticEmitter {
["boolean", Type.bool]
]);
// add compiler hints
this.setConstantInteger("ASC_TARGET", Type.i32,
i64_new(options.isWasm64 ? 2 : 1));
this.setConstantInteger("ASC_NO_TREESHAKING", Type.bool,
i64_new(options.noTreeShaking ? 1 : 0, 0));
this.setConstantInteger("ASC_NO_ASSERT", Type.bool,
i64_new(options.noAssert ? 1 : 0, 0));
this.setConstantInteger("ASC_MEMORY_BASE", Type.i32,
i64_new(options.memoryBase, 0));
this.setConstantInteger("ASC_OPTIMIZE_LEVEL", Type.i32,
i64_new(options.optimizeLevelHint, 0));
this.setConstantInteger("ASC_SHRINK_LEVEL", Type.i32,
i64_new(options.shrinkLevelHint, 0));
this.setConstantInteger("ASC_FEATURE_MUTABLE_GLOBAL", Type.bool,
i64_new(options.hasFeature(Feature.MUTABLE_GLOBAL) ? 1 : 0, 0));
this.setConstantInteger("ASC_FEATURE_SIGN_EXTENSION", Type.bool,
i64_new(options.hasFeature(Feature.SIGN_EXTENSION) ? 1 : 0, 0));
// remember deferred elements
var queuedImports = new Array<QueuedImport>();
var queuedExports = new Map<string,QueuedExport>();
var queuedExtends = new Array<ClassPrototype>();
@ -617,6 +639,24 @@ export class Program extends DiagnosticEmitter {
}
}
/** Sets a constant integer value. */
setConstantInteger(globalName: string, type: Type, value: I64): void {
assert(type.is(TypeFlags.INTEGER));
this.elementsLookup.set(globalName,
new Global(this, globalName, globalName, type, null, DecoratorFlags.NONE)
.withConstantIntegerValue(value)
);
}
/** Sets a constant float value. */
setConstantFloat(globalName: string, type: Type, value: f64): void {
assert(type.is(TypeFlags.FLOAT));
this.elementsLookup.set(globalName,
new Global(this, globalName, globalName, type, null, DecoratorFlags.NONE)
.withConstantFloatValue(value)
);
}
/** Tries to resolve an import by traversing exports and queued exports. */
private tryResolveImport(
externalName: string,
@ -2670,9 +2710,9 @@ export class VariableLikeElement extends Element {
this.declaration = declaration;
}
withConstantIntegerValue(lo: i32, hi: i32): this {
withConstantIntegerValue(value: I64): this {
this.constantValueKind = ConstantValueKind.INTEGER;
this.constantIntegerValue = i64_new(lo, hi);
this.constantIntegerValue = value;
this.set(CommonFlags.CONST | CommonFlags.INLINED);
return this;
}