Less prose; Update i64 example; More options for asc

This commit is contained in:
dcodeIO 2017-12-25 12:08:51 +01:00
parent 4baff99125
commit 5c4bf1af76
14 changed files with 322 additions and 82 deletions

View File

@ -3,34 +3,31 @@
[![Build Status](https://travis-ci.org/AssemblyScript/assemblyscript.svg?branch=master)](https://travis-ci.org/AssemblyScript/assemblyscript)
**AssemblyScript** is a new compiler targeting [WebAssembly](http://webassembly.org) while utilizing [TypeScript](http://www.typescriptlang.org)'s syntax and [node](https://nodejs.org)'s vibrant ecosystem. Instead of requiring complex toolchains to set up, you can simply `npm install` it - or run it in a browser.
Note, though, that this version of the compiler (0.5.0, NEXT) is relatively new and does not yet support some features a TypeScript programmer might expect, e.g., strings, arrays and classes.
See [the AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) for additional information and documentation.
How does it work?
-----------------
By compiling syntactially (not necessarily semantically) valid TypeScript to [Binaryen](https://github.com/WebAssembly/binaryen) IR, the resulting module can be validated, optimized, emitted in WebAssembly text or binary format and converted to [asm.js](http://asmjs.org) as a polyfill.
**AssemblyScript** compiles strictly typed [TypeScript](http://www.typescriptlang.org) to [WebAssembly](http://webassembly.org) using [Binaryen](https://github.com/WebAssembly/binaryen). Unlike more complex toolchains, `asc` generates minimal WebAssembly modules while being just an `npm install` away.
Examples
--------
A few early examples to get an idea:
* **[Conway's Game of Life](./examples/game-of-life)** as seen on dcode.io<br />
* **[i64 polyfill](./examples/i64-polyfill)** using 32-bit integers<br />
* **[Conway's Game of Life](./examples/game-of-life)**<br />
Continuously updates the cellular automaton and visualizes its state on a canvas.
* **[i64 polyfill](./examples/i64-polyfill)**<br />
Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).
Or browse the [compiler tests](./tests/compiler) for a more in-depth overview of what's supported already.
Getting started
---------------
This version of the compiler is not on [npm](https://www.npmjs.com/package/assemblyscript), yet, but if you'd like to try it today or even plan to contribute, this is how you do it:
Note that this version of the compiler is relatively new and does not yet support some features a TypeScript programmer might expect, e.g., strings, arrays and classes. It is not on [npm](https://www.npmjs.com/package/assemblyscript), yet, but you can already try it out today:
```
$> git clone https://github.com/AssemblyScript/assemblyscript.git
$> cd assemblyscript
$> npm install
$> npm link
```
Author your module using either
@ -41,9 +38,11 @@ Author your module using either
and run:
```
$> node bin/asc yourModule.ts
$> asc yourModule.ts
```
See [the AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) for additional documentation.
Building
--------

View File

@ -143,6 +143,7 @@ var options = assemblyscript.createOptions();
assemblyscript.setTarget(options, 0);
assemblyscript.setNoTreeShaking(options, args.noTreeShaking);
assemblyscript.setNoAssert(options, args.noAssert);
assemblyscript.setNoMemory(options, args.noMemory);
// TODO: noDebug binaryen feature, removing names the debug section
var module = assemblyscript.compile(parser, options);

View File

@ -51,6 +51,10 @@
"desc": "Does not include the standard library.",
"type": "boolean"
},
"noMemory": {
"desc": "Does not set up a memory.",
"type": "boolean"
},
"trapMode": {
"desc": [
"Sets the trap mode to use.",

View File

@ -1,15 +1,126 @@
i64 polyfill
============
@assemblyscript/i64
===================
An [AssemblyScript](http://assemblyscript.org) example. Exposes i64 operations to JS using 32-bit integers (low and high bits).
Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).
Instructions
------------
Usage
-----
```
$> npm install @assemblyscript/i64
```
```ts
import * as i64 from "@assemblyscript/i64";
i64.div(10, 0, 2, 0);
console.log("result: lo=" + i64.getLo() + ", hi=" + i64.getHi());
```
API
---
**Note** that `u32` is just an alias of `number` in JavaScript with values in 32-bit integer range.
* **getLo**(): `u32`<br />
Gets the low 32 bits of the computed 64-bit value.
* **getHi**(): `u32`<br />
Gets the high 32 bits of the computed 64-bit value.
* **clz**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic count leading zero bits operation. All zero bits are considered leading if the value is zero.
* **ctz**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic count tailing zero bits operation. All zero bits are considered trailing if the value is zero.
* **popcnt**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic count number of one bits operation.
* **eqz**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic equals-zero operation.
* **add**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic addition operation.
* **sub**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic subtraction operation.
* **mul**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic multiplication operation.
* **div_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed division operation.
* **div_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned division operation.
* **rem_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed remainder operation.
* **rem_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned remainder operation.
* **and**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise and operation.
* **or**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise or operation.
* **xor**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise xor operation.
* **shl**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise shift left operation.
* **shr_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed bitwise shift right operation.
* **shr_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned bitwise shift right operation.
* **rotl**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic rotate left operation.
* **rotr**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic rotate right operation.
* **eq**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic compare equal operation.
* **ne**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic compare unequal operation.
* **lt_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed less than operation.
* **lt_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned less than operation.
* **le_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed less than or equal operation.
* **le_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned less than or equal operation.
* **gt_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed greater than operation.
* **gt_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned greater than operation.
* **ge_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed greater than or equal operation.
* **ge_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned greater than or equal operation.
Building
--------
To build [assembly/i64.ts](./assembly/i64.ts) to an untouched and an optimized `.wasm` including their respective `.wast` representations, run:
```
$> npm run build
```
Afterwards, `require` the node module as usual (CommonJS entry point is [index.js](./index.js)).

63
examples/i64-polyfill/index.d.ts vendored Normal file
View File

@ -0,0 +1,63 @@
type u32 = number;
/** Gets the low 32 bits of the computed 64-bit value. */
export function getLo(): u32;
/** Gets the high 32 bits of the computed 64-bit value. */
export function getHi(): u32;
/** Performs the sign-agnostic count leading zero bits operation. All zero bits are considered leading if the value is zero. */
export function clz(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic count tailing zero bits operation. All zero bits are considered trailing if the value is zero. */
export function ctz(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic count number of one bits operation. */
export function popcnt(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic equals-zero operation. */
export function eqz(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic addition operation. */
export function add(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic subtraction operation. */
export function sub(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic multiplication operation. */
export function mul(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed division operation. */
export function div_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned division operation. */
export function div_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed remainder operation. */
export function rem_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned remainder operation. */
export function rem_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise and operation. */
export function and(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise or operation. */
export function or(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise xor operation. */
export function xor(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise shift left operation. */
export function shl(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed bitwise shift right operation. */
export function shr_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned bitwise shift right operation. */
export function shr_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic rotate left operation. */
export function rotl(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic rotate right operation. */
export function rotr(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic compare equal operation. */
export function eq(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic compare unequal operation. */
export function ne(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed less than operation. */
export function lt_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned less than operation. */
export function lt_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed less than or equal operation. */
export function le_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned less than or equal operation. */
export function le_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed greater than operation.*/
export function gt_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned greater than operation.*/
export function gt_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed greater than or equal operation. */
export function ge_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned greater than or equal operation. */
export function ge_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;

View File

@ -1,10 +1,28 @@
{
"name": "@assemblyscript/i64-polyfill-example",
"name": "@assemblyscript/i64",
"version": "1.0.0",
"private": true,
"author": "Daniel Wirtz <dcode+assemblyscript@dcode.io>",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/AssemblyScript/assemblyscript.git"
},
"bugs": {
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
},
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"build": "npm run build:untouched && npm run build:optimized",
"build:untouched": "asc assembly/i64.ts -t i64.untouched.wast -b i64.untouched.wasm --validate",
"build:optimized": "asc -O assembly/i64.ts -b i64.optimized.wasm -t i64.optimized.wast --validate"
}
"build:untouched": "asc assembly/i64.ts -t i64.untouched.wast -b i64.untouched.wasm --noMemory --validate",
"build:optimized": "asc -O assembly/i64.ts -b i64.optimized.wasm -t i64.optimized.wast --noMemory --validate",
"test": "node tests"
},
"files": [
"assembly/",
"i64.optimized.wasm",
"index.d.ts",
"index.js",
"README.md"
]
}

View File

@ -0,0 +1,40 @@
var assert = require("assert");
function assertUnary(op, loLeft, hiLeft, loResult, hiResult) {
op(loLeft, hiLeft);
assert.strictEqual(i64.getLo(), loResult);
assert.strictEqual(i64.getHi(), hiResult);
}
function assertBinary(op, loLeft, hiLeft, loRight, hiRight, loResult, hiResult) {
op(loLeft, hiLeft, loLeft, loRight);
assert.strictEqual(i64.getLo(), loResult);
assert.strictEqual(i64.getHi(), hiResult);
}
var i64 = require("..");
assertUnary(i64.clz, 1, 0, 63, 0);
assertUnary(i64.clz, 0, 1, 31, 0);
assertUnary(i64.clz, 1, 1, 31, 0);
assertUnary(i64.clz, 0, 0, 64, 0);
assertUnary(i64.ctz, 0, 0x80000000, 63, 0);
assertUnary(i64.ctz, 0x80000000, 0x80000000, 31, 0);
assertUnary(i64.ctz, 0, 1, 32, 0);
assertUnary(i64.ctz, 1, 0, 0, 0);
assertUnary(i64.ctz, 0, 0, 64, 0);
assertUnary(i64.popcnt, 0x55555555, 0x55555555, 32, 0);
assertUnary(i64.popcnt, -1, -1, 64, 0);
assertUnary(i64.popcnt, 0, 0, 0, 0);
assertUnary(i64.popcnt, 0x55, 0, 4, 0);
assertUnary(i64.popcnt, 0, 0x55, 4, 0);
assertUnary(i64.popcnt, 0x55, 0x55, 8, 0);
assertUnary(i64.eqz, 0, 0, 1, 0);
assertUnary(i64.eqz, 0, 1, 0, 0);
assertUnary(i64.eqz, 1, 0, 0, 0);
assertUnary(i64.eqz, 1, 1, 0, 0);
// TODO...

View File

@ -5,10 +5,10 @@
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/AssemblyScript/next.git"
"url": "https://github.com/AssemblyScript/assemblyscript.git"
},
"bugs": {
"url": "https://github.com/AssemblyScript/next/issues"
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
},
"dependencies": {
"binaryen": "40.0.0-nightly.20171209",

View File

@ -134,6 +134,8 @@ export class Options {
noTreeShaking: bool = false;
/** If true, replaces assertions with nops. */
noAssert: bool = false;
/** If true, does not set up a memory. */
noMemory: bool = false;
}
/** Indicates the desired kind of a conversion. */
@ -227,22 +229,23 @@ export class Compiler extends DiagnosticEmitter {
}
// set up memory
const initial: U64 = this.memoryOffset.clone();
if (this.options.target == Target.WASM64)
this.module.addGlobal("HEAP_BASE", NativeType.I64, false, this.module.createI64(initial.lo, initial.hi));
else
this.module.addGlobal("HEAP_BASE", NativeType.I32, false, this.module.createI32(initial.lo));
if (!this.options.noMemory) {
const initial: U64 = this.memoryOffset.clone();
if (this.options.target == Target.WASM64)
this.module.addGlobal("HEAP_BASE", NativeType.I64, false, this.module.createI64(initial.lo, initial.hi));
else
this.module.addGlobal("HEAP_BASE", NativeType.I32, false, this.module.createI32(initial.lo));
// determine initial page size
const initialOverlaps: U64 = initial.clone();
initialOverlaps.and32(0xffff);
if (!initialOverlaps.isZero) {
initial.or32(0xffff);
initial.add32(1);
// determine initial page size
const initialOverlaps: U64 = initial.clone();
initialOverlaps.and32(0xffff);
if (!initialOverlaps.isZero) {
initial.or32(0xffff);
initial.add32(1);
}
initial.shru32(16); // now is initial size in 64k pages
this.module.setMemory(initial.toI32(), Module.MAX_MEMORY_WASM32 /* TODO: not WASM64 compatible yet */, this.memorySegments, this.options.target, "memory");
}
initial.shru32(16); // now is initial size in 64k pages
this.module.setMemory(initial.toI32(), Module.MAX_MEMORY_WASM32 /* TODO: not WASM64 compatible yet */, this.memorySegments, this.options.target, "memory");
return this.module;
}

View File

@ -113,6 +113,11 @@ export function setNoAssert(options: Options, noAssert: bool): void {
options.noAssert = noAssert;
}
/** Sets the `noMemory` option. */
export function setNoMemory(options: Options, noMemory: bool): void {
options.noMemory = noMemory;
}
/** Compiles the sources computed by the parser to a module. */
export function compile(parser: Parser, options: Options | null = null): Module {
const program: Program = parser.finish();

View File

@ -137,15 +137,11 @@ export class Type {
/** 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 || this.kind == TypeKind.BOOL
? NativeType.I32
: NativeType.None;
return this.kind == TypeKind.F32 ? NativeType.F32
: this.kind == TypeKind.F64 ? NativeType.F64
: this.isLongInteger ? NativeType.I64
: this.isAnyInteger ? NativeType.I32
: NativeType.None;
}
/** Converts this type to its native `0` value. */

View File

@ -1300,35 +1300,35 @@
../../examples/i64-polyfill/assembly/i64/gt_u
../../examples/i64-polyfill/assembly/i64/ge_s
../../examples/i64-polyfill/assembly/i64/ge_u
i64/getHi
i64/getLo
i64/clz
i64/ctz
i64/popcnt
i64/eqz
i64/add
i64/sub
i64/mul
i64/div_s
i64/div_u
i64/rem_s
i64/rem_u
i64/and
i64/or
i64/xor
i64/shl
i64/shr_s
i64/shr_u
i64/rotl
i64/rotr
i64/eq
i64/ne
i64/lt_s
i64/lt_u
i64/le_s
i64/le_u
i64/gt_s
i64/gt_u
i64/ge_s
i64/ge_u
i64-polyfill/getHi
i64-polyfill/getLo
i64-polyfill/clz
i64-polyfill/ctz
i64-polyfill/popcnt
i64-polyfill/eqz
i64-polyfill/add
i64-polyfill/sub
i64-polyfill/mul
i64-polyfill/div_s
i64-polyfill/div_u
i64-polyfill/rem_s
i64-polyfill/rem_u
i64-polyfill/and
i64-polyfill/or
i64-polyfill/xor
i64-polyfill/shl
i64-polyfill/shr_s
i64-polyfill/shr_u
i64-polyfill/rotl
i64-polyfill/rotr
i64-polyfill/eq
i64-polyfill/ne
i64-polyfill/lt_s
i64-polyfill/lt_u
i64-polyfill/le_s
i64-polyfill/le_u
i64-polyfill/gt_s
i64-polyfill/gt_u
i64-polyfill/ge_s
i64-polyfill/ge_u
;)