make std/string test ok again

This commit is contained in:
dcode 2019-03-16 14:48:22 +01:00
parent 05a35f42f6
commit 0c388ca4c6
9 changed files with 3929 additions and 4779 deletions

View File

@ -62,11 +62,12 @@ import {
Field, Field,
Global, Global,
DecoratorFlags, DecoratorFlags,
ClassPrototype ClassPrototype,
Local
} from "./program"; } from "./program";
import { import {
FlowFlags FlowFlags, Flow
} from "./flow"; } from "./flow";
import { import {
@ -4214,14 +4215,21 @@ export function compileArraySet(
valueExpression: Expression, valueExpression: Expression,
contextualType: Type contextualType: Type
): ExpressionRef { ): ExpressionRef {
var program = compiler.program;
var type = typedArraySymbolToType(target.internalName); var type = typedArraySymbolToType(target.internalName);
if (type) { if (!type) {
return compileTypedArraySet(compiler, target, type, thisExpression, assert(target.prototype == program.arrayPrototype);
elementExpression, valueExpression, contextualType); type = assert(target.typeArguments)[0];
} }
assert(target.prototype == compiler.program.arrayPrototype); return compileTypedArraySet(
type = assert(target.typeArguments)[0]; compiler,
throw new Error("not implemented"); target,
type,
thisExpression,
elementExpression,
valueExpression,
contextualType
);
} }
function compileTypedArraySet( function compileTypedArraySet(
@ -4233,7 +4241,6 @@ function compileTypedArraySet(
valueExpression: Expression, valueExpression: Expression,
contextualType: Type contextualType: Type
): ExpressionRef { ): ExpressionRef {
var type = typedArraySymbolToType(target.internalName);
var module = compiler.module; var module = compiler.module;
var dataStart = assert(target.lookupInSelf("dataStart")); var dataStart = assert(target.lookupInSelf("dataStart"));
@ -4267,16 +4274,22 @@ function compileTypedArraySet(
} }
} }
var typeIsManaged = type.is(TypeFlags.REFERENCE); // FIXME: .isManaged
var usizeType = compiler.options.usizeType; var usizeType = compiler.options.usizeType;
var nativeSizeType = compiler.options.nativeSizeType; var nativeSizeType = compiler.options.nativeSizeType;
var thisExpr = compiler.compileExpression(
thisExpression,
target.type,
ConversionKind.IMPLICIT,
WrapMode.NONE
);
var tempThis: Local | null = null;
if (typeIsManaged) {
tempThis = compiler.currentFlow.getTempLocal(target.type, false);
thisExpr = module.createTeeLocal(tempThis.index, thisExpr);
}
var dataStartExpr = module.createLoad(usizeType.byteSize, true, var dataStartExpr = module.createLoad(usizeType.byteSize, true,
compiler.compileExpression( thisExpr, nativeSizeType, (<Field>dataStart).memoryOffset
thisExpression,
target.type,
ConversionKind.IMPLICIT,
WrapMode.NONE
),
nativeSizeType, (<Field>dataStart).memoryOffset
); );
var typeAlignLog2 = type.alignLog2; var typeAlignLog2 = type.alignLog2;
@ -4316,10 +4329,33 @@ function compileTypedArraySet(
WrapMode.NONE WrapMode.NONE
); );
// clamp // handle Array<Ref>: value = LINK<T, TArray>(value, this), value
if (target.internalName == BuiltinSymbols.Uint8ClampedArray) { if (typeIsManaged) {
let program = compiler.program;
let linkPrototype = assert(program.linkPrototype);
let linkInstance = compiler.resolver.resolveFunction(linkPrototype, [ type, target.type ]);
if (!linkInstance) return module.createUnreachable();
let previousFlow = compiler.currentFlow;
let tempValue = previousFlow.getTempLocal(type, false);
let flow = Flow.createInline(previousFlow.parentFunction, linkInstance);
compiler.currentFlow = flow;
flow.addScopedAlias(linkInstance.signature.getParameterName(0), type, tempValue.index);
flow.addScopedAlias(linkInstance.signature.getParameterName(1), target.type, assert(tempThis).index);
let body = compiler.compileFunctionBody(linkInstance);
body.unshift(
module.createSetLocal(tempValue.index, valueExpr)
);
body.push(
module.createGetLocal(tempValue.index, nativeSizeType)
);
previousFlow.freeTempLocal(tempValue);
previousFlow.freeTempLocal(tempThis!); tempThis = null;
compiler.currentFlow = previousFlow;
valueExpr = module.createBlock(flow.inlineReturnLabel, body, nativeSizeType);
// handle Uint8ClampedArray: value = ~(value >> 31) & (((255 - value) >> 31) | value)
} else if (target.internalName == BuiltinSymbols.Uint8ClampedArray) {
let tempLocal = compiler.currentFlow.getAndFreeTempLocal(Type.i32, true); let tempLocal = compiler.currentFlow.getAndFreeTempLocal(Type.i32, true);
// ~(value >> 31) & (((255 - value) >> 31) | value)
valueExpr = module.createBinary(BinaryOp.AndI32, valueExpr = module.createBinary(BinaryOp.AndI32,
module.createBinary(BinaryOp.XorI32, module.createBinary(BinaryOp.XorI32,
module.createBinary(BinaryOp.ShrI32, module.createBinary(BinaryOp.ShrI32,
@ -4340,6 +4376,7 @@ function compileTypedArraySet(
) )
); );
} }
assert(!tempThis);
var nativeType = type.toNativeType(); var nativeType = type.toNativeType();

View File

@ -1065,7 +1065,7 @@ export class Compiler extends DiagnosticEmitter {
} }
/** Compiles the body of a function within the specified flow. */ /** Compiles the body of a function within the specified flow. */
private compileFunctionBody(instance: Function): ExpressionRef[] { compileFunctionBody(instance: Function): ExpressionRef[] {
var module = this.module; var module = this.module;
var bodyNode = assert(instance.prototype.bodyNode); var bodyNode = assert(instance.prototype.bodyNode);
var returnType = instance.signature.returnType; var returnType = instance.signature.returnType;
@ -4689,22 +4689,22 @@ export class Compiler extends DiagnosticEmitter {
case ElementKind.CLASS: { case ElementKind.CLASS: {
let elementExpression = resolver.currentElementExpression; let elementExpression = resolver.currentElementExpression;
if (elementExpression) { // indexed access if (elementExpression) { // indexed access
let arrayBufferView = this.program.arrayBufferViewInstance;
if (arrayBufferView) {
if ((<Class>target).prototype.extends(arrayBufferView.prototype)) {
return compileArraySet(
this,
<Class>target,
assert(this.resolver.currentThisExpression),
elementExpression,
valueExpression,
contextualType
);
}
}
let isUnchecked = flow.is(FlowFlags.UNCHECKED_CONTEXT); let isUnchecked = flow.is(FlowFlags.UNCHECKED_CONTEXT);
let indexedSet = (<Class>target).lookupOverload(OperatorKind.INDEXED_SET, isUnchecked); let indexedSet = (<Class>target).lookupOverload(OperatorKind.INDEXED_SET, isUnchecked);
if (!indexedSet) { if (!indexedSet) {
let arrayBufferView = this.program.arrayBufferViewInstance;
if (arrayBufferView) {
if ((<Class>target).prototype.extends(arrayBufferView.prototype)) {
return compileArraySet(
this,
<Class>target,
assert(this.resolver.currentThisExpression),
elementExpression,
valueExpression,
contextualType
);
}
}
let indexedGet = (<Class>target).lookupOverload(OperatorKind.INDEXED_GET, isUnchecked); let indexedGet = (<Class>target).lookupOverload(OperatorKind.INDEXED_GET, isUnchecked);
if (!indexedGet) { if (!indexedGet) {
this.error( this.error(

View File

@ -280,8 +280,8 @@ export abstract class DiagnosticEmitter {
var message = DiagnosticMessage.create(code, category, arg0, arg1, arg2).withRange(range); var message = DiagnosticMessage.create(code, category, arg0, arg1, arg2).withRange(range);
if (relatedRange) message.relatedRange = relatedRange; if (relatedRange) message.relatedRange = relatedRange;
this.diagnostics.push(message); this.diagnostics.push(message);
// console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary
// console.log(<string>new Error("stack").stack); console.log(<string>new Error("stack").stack);
} }
/** Emits an informatory diagnostic message. */ /** Emits an informatory diagnostic message. */

View File

@ -346,12 +346,14 @@ export class Array<T> extends ArrayBufferView {
} }
reverse(): Array<T> { reverse(): Array<T> {
var base = this.dataStart; var front = this.dataStart;
for (let front = 0, back = this.length_ - 1; front < back; ++front, --back) { var back = this.dataEnd - sizeof<T>();
let temp = load<T>(base, front); while (front < back) {
let dest = base + (<usize>back << alignof<T>()); let temp = load<T>(front);
store<T>(base + (<usize>front << alignof<T>()), load<T>(dest)); store<T>(front, load<T>(back));
store<T>(dest, temp); store<T>(back, temp);
front += sizeof<T>();
back -= sizeof<T>();
} }
return this; return this;
} }

View File

@ -192,7 +192,7 @@ function assertUnregistered(ref: usize): void {
/** Asserts that a managed object has already been registered. */ /** Asserts that a managed object has already been registered. */
// @ts-ignore: decorator // @ts-ignore: decorator
function assertRegistered(ref: usize): void { function assertRegistered(ref: usize): void {
assert(ref > HEAP_BASE); // must be a heap object // may be a static string or buffer (not a heap object)
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC); assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
} }

View File

@ -1,4 +1,4 @@
import { ALLOCATE, REGISTER, HEADER, HEADER_SIZE } from "./runtime"; import { ALLOCATE, REGISTER, HEADER, HEADER_SIZE, ArrayBufferView, LINK } from "./runtime";
import { MAX_SIZE_32 } from "./util/allocator"; import { MAX_SIZE_32 } from "./util/allocator";
import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string"; import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string";
@ -359,22 +359,20 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
// split by chars // split by chars
length = min<isize>(length, <isize>limit); length = min<isize>(length, <isize>limit);
let result = new Array<String>(length); let result = new Array<String>(length);
let buffer = unreachable(); // TODO let resultStart = changetype<ArrayBufferView>(result).dataStart;
// let buffer = <ArrayBuffer>result.buffer_;
for (let i: isize = 0; i < length; ++i) { for (let i: isize = 0; i < length; ++i) {
let char = ALLOCATE(2); let charStr = ALLOCATE(2);
store<u16>( store<u16>(
changetype<usize>(char), charStr,
load<u16>( load<u16>(changetype<usize>(this) + (<usize>i << 1))
changetype<usize>(this) + (<usize>i << 1)
)
); );
store<usize>(changetype<usize>(buffer) + (<usize>i << 1), char); store<String>(resultStart + (<usize>i << alignof<String>()), REGISTER<String>(charStr));
LINK(charStr, result);
} }
return result; return result;
} else if (!length) { } else if (!length) {
let result = new Array<String>(1); let result = new Array<String>(1);
unchecked(result[0] = changetype<String>("")); store<string>(changetype<ArrayBufferView>(result).dataStart, ""); // no need to register/link
return result; return result;
} }
var result = new Array<String>(); var result = new Array<String>();

View File

@ -1,12 +1,12 @@
import "allocator/arena"; import "allocator/arena";
import { Array } from "array"; import { Array } from "array";
import { COMPARATOR } from "internal/sort"; import { COMPARATOR } from "util/sort";
// Obtains the internal capacity of an array from its backing buffer. // Obtains the internal capacity of an array from its backing buffer.
function internalCapacity<T>(array: Array<T>): i32 { function internalCapacity<T>(array: Array<T>): i32 {
// the memory region used by the backing buffer might still be larger in that the ArrayBuffer // the memory region used by the backing buffer might still be larger in that the ArrayBuffer
// pre-allocates a power of 2 sized buffer itself and reuses it as long as it isn't exceeded. // pre-allocates a power of 2 sized buffer itself and reuses it as long as it isn't exceeded.
var buffer: ArrayBuffer = array.buffer_; var buffer: ArrayBuffer = array.data;
return buffer.byteLength >> alignof<T>(); return buffer.byteLength >> alignof<T>();
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff