Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ typings/
lib/

package-lock.json

# Generated polyfill binaries
packages/proposal-sign-extension-ops/src/polyfills/*.wasm
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"markdown-to-html": "0.0.13",
"mocha": "4.0.1",
"performance-now": "2.1.0",
"prettier": "1.9.2"
"prettier": "1.9.2",
"assemblyscript": "github:AssemblyScript/assemblyscript"
},
"dependencies": {
"babel-plugin-mamacro": "0.0.3",
Expand Down
7 changes: 7 additions & 0 deletions packages/helper-wasm-bytecode/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ const symbolsByByte = {
0xba: createSymbolObject("convert_u/i64", "f64"),
0xbb: createSymbolObject("promote/f32", "f64"),

// Sign extension op proposal
0xc0: createSymbolObject("extend8_s", "i32"),
0xc1: createSymbolObject("extend16_s", "i32"),
0xc2: createSymbolObject("extend8_s", "i64"),
0xc3: createSymbolObject("extend16_s", "i64"),
0xc4: createSymbolObject("extend32_s", "i64"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should change that to support two or more bytes? Atomic operations also have an additional byte.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to the threads proposal? i.e. https://webassembly.github.io/threads/binary/instructions.html#atomic-memory-instructions. Then it shouldn't affect this proposal but something to look out for in the future I think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already added a check for the atomic operations prefix byte. I just saying that we could move the logic here by matching an array of bytes?


0xbc: createSymbolObject("reinterpret/f32", "i32"),
0xbd: createSymbolObject("reinterpret/f64", "i64"),
0xbe: createSymbolObject("reinterpret/i32", "f32"),
Expand Down
4 changes: 4 additions & 0 deletions packages/proposal-sign-extension-ops/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sign extension proposal

<https://github.com/WebAssembly/sign-extension-ops>

34 changes: 34 additions & 0 deletions packages/proposal-sign-extension-ops/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@webassemblyjs/proposal-sign-ops",
"version": "1.5.6",
"description": "",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/xtuc/webassemblyjs.git"
},
"publishConfig": {
"access": "public"
},
"author": "Mauro Bringolf",
"license": "MIT",
"dependencies": {
"@webassemblyjs/ast": "1.5.6",
"@webassemblyjs/helper-buffer": "1.5.6",
"@webassemblyjs/helper-wasm-bytecode": "1.5.6",
"@webassemblyjs/helper-wasm-section": "1.5.6",
"@webassemblyjs/wasm-edit": "1.5.6",
"@webassemblyjs/wasm-gen": "1.5.6",
"@webassemblyjs/wasm-opt": "1.5.6",
"debug": "^3.1.0"
},
"devDependencies": {
"@webassemblyjs/helper-test-framework": "1.5.6",
"@webassemblyjs/wast-parser": "1.5.6",
"@webassemblyjs/wasm-parser": "1.5.6",
"@webassemblyjs/wast-printer": "1.5.6"
}
}
78 changes: 78 additions & 0 deletions packages/proposal-sign-extension-ops/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { traverse, callInstruction, numberLiteral } from "@webassemblyjs/ast";
import i32_extend8_s from "./polyfills/i32_extend8_s.json";
import i32_extend16_s from "./polyfills/i32_extend16_s.json";
import i64_extend8_s from "./polyfills/i64_extend8_s.json";
import i64_extend16_s from "./polyfills/i64_extend16_s.json";
import i64_extend32_s from "./polyfills/i64_extend32_s.json";

class Polyfills {
constructor(startIndex) {
this.startIndex = startIndex;

this.asts = {
i32_extend8_s,
i32_extend16_s,
i64_extend8_s,
i64_extend16_s,
i64_extend32_s
};

this.instructions = Object.keys(this.asts);

this.index = {};

this.instructions.forEach(instrName => {
this.index[instrName] = -1;
});
}

replaceWith(path, instrName) {
if (this.index[instrName] === -1) {
this.index[instrName] = this.startIndex++;
}

const index = this.index[instrName];
path.replaceWith(callInstruction(numberLiteral(index, String(index))));
}

matchesInstruction(instrName) {
return this.instructions.includes(instrName);
}

insertInto(ast) {
const funcsToPush = Object.keys(this.index)
.filter(instrName => this.index[instrName] >= 0)
.sort((x, y) => (this.index[x] > this.index[y] ? 1 : -1))
.map(instrName => this.asts[instrName]);

ast.body[0].fields.push(...funcsToPush);
}
}

export function transformAst(ast) {
let numberOfFuncs = 0;

const countFuncVisitor = {
Func() {
++numberOfFuncs;
}
};

traverse(ast, countFuncVisitor);

const polyfills = new Polyfills(numberOfFuncs);

const signExtendVisitor = {
Instr(path) {
if (polyfills.matchesInstruction(path.node.id)) {
polyfills.replaceWith(path, path.node.id);
}
}
};

traverse(ast, signExtendVisitor);

polyfills.insertInto(ast);

return ast;
}
165 changes: 165 additions & 0 deletions packages/proposal-sign-extension-ops/src/polyfills/i32_extend16_s.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"type": "Func",
"name": {
"type": "Identifier",
"value": "packages/proposal-sign-extension-ops/src/polyfills/i32_extend16_s/i32_extend16_s"
},
"signature": {
"type": "Signature",
"params": [
{
"valtype": "i32"
}
],
"results": [
"i32"
]
},
"body": [
{
"type": "Instr",
"id": "get_local",
"args": [
{
"type": "NumberLiteral",
"value": 0,
"raw": "0"
}
],
"loc": {
"start": {
"line": -1,
"column": 59
},
"end": {
"line": -1,
"column": 61
}
}
},
{
"type": "Instr",
"id": "const",
"args": [
{
"type": "NumberLiteral",
"value": -32768,
"raw": "-32768"
}
],
"object": "i32"
},
{
"type": "Instr",
"id": "or",
"args": [],
"object": "i32"
},
{
"type": "Instr",
"id": "get_local",
"args": [
{
"type": "NumberLiteral",
"value": 0,
"raw": "0"
}
],
"loc": {
"start": {
"line": -1,
"column": 66
},
"end": {
"line": -1,
"column": 68
}
}
},
{
"type": "Instr",
"id": "const",
"args": [
{
"type": "NumberLiteral",
"value": 32767,
"raw": "32767"
}
],
"object": "i32"
},
{
"type": "Instr",
"id": "and",
"args": [],
"object": "i32"
},
{
"type": "Instr",
"id": "get_local",
"args": [
{
"type": "NumberLiteral",
"value": 0,
"raw": "0"
}
],
"loc": {
"start": {
"line": -1,
"column": 73
},
"end": {
"line": -1,
"column": 75
}
}
},
{
"type": "Instr",
"id": "const",
"args": [
{
"type": "NumberLiteral",
"value": 32768,
"raw": "32768"
}
],
"object": "i32"
},
{
"type": "Instr",
"id": "and",
"args": [],
"object": "i32"
},
{
"type": "Instr",
"id": "select",
"args": [],
"loc": {
"start": {
"line": -1,
"column": 80
},
"end": {
"line": -1,
"column": 81
}
}
}
],
"loc": {
"start": {
"line": -1,
"column": 57
},
"end": {
"line": -1,
"column": 82
}
},
"metadata": {
"bodySize": 24
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Polyfill for i32_extend16_s instruction
*/
export function i32_extend16_s(x: i32): i32 {
return (x & 0x00008000) ? (x | 0xffff8000) : (x & 0x00007fff);
}
Loading