#252 WASM binary version was reset to 0x01 for MVP official release

In current browsers and for the official MVP only WASM binary version 0x01 is supported.
All other binary versions are no longer required or supported, hence the generation
logic for them is no longer needed.
This commit is contained in:
sertic 2017-04-04 12:48:00 +02:00
parent 8144464b0e
commit 19b804b5fc
9 changed files with 181 additions and 788 deletions

View File

@ -20,7 +20,7 @@ You can use TeaVM for building applications for the browser, due to the followin
* generation of source maps; * generation of source maps;
* debugger; * debugger;
* interoperation with JavaScript libraries together with the set of predefined browser interfaces. * interoperation with JavaScript libraries together with the set of predefined browser interfaces.
* supports WebAssebly output (experimental). * supports WebAssembly output (experimental).
Quick start Quick start

View File

@ -131,7 +131,7 @@ public class WasmTarget implements TeaVMTarget {
private ClassInitializerEliminator classInitializerEliminator; private ClassInitializerEliminator classInitializerEliminator;
private ClassInitializerTransformer classInitializerTransformer; private ClassInitializerTransformer classInitializerTransformer;
private ShadowStackTransformer shadowStackTransformer; private ShadowStackTransformer shadowStackTransformer;
private WasmBinaryVersion version = WasmBinaryVersion.V_0xC; private WasmBinaryVersion version = WasmBinaryVersion.V_0x1;
@Override @Override
public void setController(TeaVMTargetController controller) { public void setController(TeaVMTargetController controller) {

View File

@ -57,14 +57,8 @@ public class WasmBinaryRenderer {
public void render(WasmModule module) { public void render(WasmModule module) {
output.writeInt32(0x6d736100); output.writeInt32(0x6d736100);
switch (version) { switch (version) {
case V_0xB: case V_0x1:
output.writeInt32(0xB); output.writeInt32(0x01);
break;
case V_0xC:
output.writeInt32(0xC);
break;
case V_0xD:
output.writeInt32(0xD);
break; break;
} }
@ -94,7 +88,7 @@ public class WasmBinaryRenderer {
section.writeLEB(signatures.size()); section.writeLEB(signatures.size());
for (WasmSignature signature : signatures) { for (WasmSignature signature : signatures) {
section.writeByte(version == WasmBinaryVersion.V_0xD ? 0x60 : 0x40); section.writeByte(0x60);
section.writeLEB(signature.types.length - 1); section.writeLEB(signature.types.length - 1);
for (int i = 1; i < signature.types.length; ++i) { for (int i = 1; i < signature.types.length; ++i) {
section.writeType(signature.types[i], version); section.writeType(signature.types[i], version);
@ -117,11 +111,7 @@ public class WasmBinaryRenderer {
if (function.getImportName() == null) { if (function.getImportName() == null) {
continue; continue;
} }
if (version == WasmBinaryVersion.V_0xB) { functionIndexes.put(function.getName(), functions.size());
importIndexes.put(function.getName(), index++);
} else {
functionIndexes.put(function.getName(), functions.size());
}
functions.add(function); functions.add(function);
} }
if (functions.isEmpty()) { if (functions.isEmpty()) {
@ -134,9 +124,6 @@ public class WasmBinaryRenderer {
for (WasmFunction function : functions) { for (WasmFunction function : functions) {
WasmSignature signature = WasmSignature.fromFunction(function); WasmSignature signature = WasmSignature.fromFunction(function);
int signatureIndex = signatureIndexes.get(signature); int signatureIndex = signatureIndexes.get(signature);
if (version == WasmBinaryVersion.V_0xB) {
section.writeLEB(signatureIndex);
}
String moduleName = function.getImportModule(); String moduleName = function.getImportModule();
if (moduleName == null) { if (moduleName == null) {
@ -146,10 +133,8 @@ public class WasmBinaryRenderer {
section.writeAsciiString(function.getImportName()); section.writeAsciiString(function.getImportName());
if (version != WasmBinaryVersion.V_0xB) { section.writeByte(EXTERNAL_KIND_FUNCTION);
section.writeByte(EXTERNAL_KIND_FUNCTION); section.writeLEB(signatureIndex);
section.writeLEB(signatureIndex);
}
} }
writeSection(SECTION_IMPORT, "import", section.getData()); writeSection(SECTION_IMPORT, "import", section.getData());
@ -181,36 +166,21 @@ public class WasmBinaryRenderer {
WasmBinaryWriter section = new WasmBinaryWriter(); WasmBinaryWriter section = new WasmBinaryWriter();
if (version == WasmBinaryVersion.V_0xB) { section.writeByte(1);
section.writeLEB(module.getFunctionTable().size()); section.writeByte(0x70);
for (WasmFunction function : module.getFunctionTable()) { section.writeByte(0);
section.writeLEB(functionIndexes.get(function.getName())); section.writeLEB(functionIndexes.size());
}
} else {
section.writeByte(1);
if (version == WasmBinaryVersion.V_0xD) {
section.writeByte(0x70);
} else {
section.writeByte(0x20);
}
section.writeByte(0);
section.writeLEB(functionIndexes.size());
}
writeSection(SECTION_TABLE, "table", section.getData()); writeSection(SECTION_TABLE, "table", section.getData());
} }
private void renderMemory(WasmModule module) { private void renderMemory(WasmModule module) {
WasmBinaryWriter section = new WasmBinaryWriter(); WasmBinaryWriter section = new WasmBinaryWriter();
if (version != WasmBinaryVersion.V_0xB) { section.writeByte(1);
section.writeByte(1); section.writeByte(1);
section.writeByte(1);
}
section.writeLEB(module.getMemorySize()); section.writeLEB(module.getMemorySize());
section.writeLEB(module.getMemorySize()); section.writeLEB(module.getMemorySize());
if (version == WasmBinaryVersion.V_0xB) {
section.writeByte(1);
}
writeSection(SECTION_MEMORY, "memory", section.getData()); writeSection(SECTION_MEMORY, "memory", section.getData());
} }
@ -228,16 +198,11 @@ public class WasmBinaryRenderer {
section.writeLEB(functions.size()); section.writeLEB(functions.size());
for (WasmFunction function : functions) { for (WasmFunction function : functions) {
int functionIndex = functionIndexes.get(function.getName()); int functionIndex = functionIndexes.get(function.getName());
if (version == WasmBinaryVersion.V_0xB) {
section.writeLEB(functionIndex);
}
section.writeAsciiString(function.getExportName()); section.writeAsciiString(function.getExportName());
if (version != WasmBinaryVersion.V_0xB) { section.writeByte(EXTERNAL_KIND_FUNCTION);
section.writeByte(EXTERNAL_KIND_FUNCTION); section.writeLEB(functionIndex);
section.writeLEB(functionIndex);
}
} }
writeSection(SECTION_EXPORT, "export", section.getData()); writeSection(SECTION_EXPORT, "export", section.getData());
@ -255,7 +220,7 @@ public class WasmBinaryRenderer {
} }
private void renderElement(WasmModule module) { private void renderElement(WasmModule module) {
if (module.getFunctionTable().isEmpty() || version == WasmBinaryVersion.V_0xB) { if (module.getFunctionTable().isEmpty()) {
return; return;
} }
@ -318,34 +283,21 @@ public class WasmBinaryRenderer {
} }
} }
Map<String, Integer> importIndexes = this.importIndexes; Map<String, Integer> importIndexes = this.functionIndexes;
if (version != WasmBinaryVersion.V_0xB) {
importIndexes = this.functionIndexes;
}
WasmBinaryRenderingVisitor visitor = new WasmBinaryRenderingVisitor(code, version, functionIndexes, WasmBinaryRenderingVisitor visitor = new WasmBinaryRenderingVisitor(code, version, functionIndexes,
importIndexes, signatureIndexes); importIndexes, signatureIndexes);
for (WasmExpression part : function.getBody()) { for (WasmExpression part : function.getBody()) {
part.acceptVisitor(visitor); part.acceptVisitor(visitor);
} }
if (version == WasmBinaryVersion.V_0xC) { code.writeByte(0x0B);
code.writeByte(0x0F);
} else if (version == WasmBinaryVersion.V_0xD) {
code.writeByte(0x0B);
}
return code.getData(); return code.getData();
} }
private void renderInitializer(WasmBinaryWriter output, int value) { private void renderInitializer(WasmBinaryWriter output, int value) {
if (version == WasmBinaryVersion.V_0xC) { output.writeByte(0x41);
output.writeByte(0x10); output.writeLEB(value);
output.writeLEB(value); output.writeByte(0x0B);
output.writeByte(0x0F);
} else {
output.writeByte(0x41);
output.writeLEB(value);
output.writeByte(0x0B);
}
} }
private void renderData(WasmModule module) { private void renderData(WasmModule module) {
@ -357,12 +309,8 @@ public class WasmBinaryRenderer {
section.writeLEB(module.getSegments().size()); section.writeLEB(module.getSegments().size());
for (WasmMemorySegment segment : module.getSegments()) { for (WasmMemorySegment segment : module.getSegments()) {
if (version == WasmBinaryVersion.V_0xB) { section.writeByte(0);
section.writeLEB(segment.getOffset()); renderInitializer(section, segment.getOffset());
} else {
section.writeByte(0);
renderInitializer(section, segment.getOffset());
}
section.writeLEB(segment.getLength()); section.writeLEB(segment.getLength());
int chunkSize = 65536; int chunkSize = 65536;
@ -410,19 +358,14 @@ public class WasmBinaryRenderer {
} }
private void writeSection(int id, String name, byte[] data) { private void writeSection(int id, String name, byte[] data) {
if (version != WasmBinaryVersion.V_0xB) { output.writeByte(id);
output.writeByte(id); int length = data.length;
int length = data.length; if (id == 0) {
if (id == 0) { length += name.length() + 1;
length += name.length() + 1; }
} output.writeLEB(length);
output.writeLEB(length); if (id == 0) {
if (id == 0) {
output.writeAsciiString(name);
}
} else {
output.writeAsciiString(name); output.writeAsciiString(name);
output.writeLEB(data.length);
} }
output.writeBytes(data); output.writeBytes(data);

View File

@ -16,7 +16,5 @@
package org.teavm.backend.wasm.render; package org.teavm.backend.wasm.render;
public enum WasmBinaryVersion { public enum WasmBinaryVersion {
V_0xB, V_0x1
V_0xC,
V_0xD
} }

View File

@ -29,21 +29,21 @@ public class WasmBinaryWriter {
public void writeType(WasmType type, WasmBinaryVersion version) { public void writeType(WasmType type, WasmBinaryVersion version) {
if (type == null) { if (type == null) {
writeByte(version == WasmBinaryVersion.V_0xD ? 0x40 : 0); writeByte(0x40);
return; return;
} }
switch (type) { switch (type) {
case INT32: case INT32:
writeByte(version == WasmBinaryVersion.V_0xD ? 0x7F : 1); writeByte(0x7F);
break; break;
case INT64: case INT64:
writeByte(version == WasmBinaryVersion.V_0xD ? 0x7E : 2); writeByte(0x7E);
break; break;
case FLOAT32: case FLOAT32:
writeByte(version == WasmBinaryVersion.V_0xD ? 0x7D : 3); writeByte(0x7D);
break; break;
case FLOAT64: case FLOAT64:
writeByte(version == WasmBinaryVersion.V_0xD ? 0x7C : 4); writeByte(0x7C);
break; break;
} }
} }

View File

@ -15,11 +15,22 @@
*/ */
package org.teavm.cli; package org.teavm.cli;
import java.io.*; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import org.apache.commons.cli.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.teavm.backend.wasm.render.WasmBinaryVersion; import org.teavm.backend.wasm.render.WasmBinaryVersion;
import org.teavm.tooling.ClassAlias; import org.teavm.tooling.ClassAlias;
import org.teavm.tooling.RuntimeCopyOperation; import org.teavm.tooling.RuntimeCopyOperation;
@ -315,14 +326,8 @@ public final class TeaVMRunner {
try { try {
int version = Integer.parseInt(value); int version = Integer.parseInt(value);
switch (version) { switch (version) {
case 11: case 1:
tool.setWasmVersion(WasmBinaryVersion.V_0xB); tool.setWasmVersion(WasmBinaryVersion.V_0x1);
break;
case 12:
tool.setWasmVersion(WasmBinaryVersion.V_0xC);
break;
case 13:
tool.setWasmVersion(WasmBinaryVersion.V_0xD);
break; break;
default: default:
System.err.print("Wrong version value"); System.err.print("Wrong version value");

View File

@ -100,7 +100,7 @@ public class TeaVMTool implements BaseTeaVMTool {
private DebugInformationBuilder debugEmitter; private DebugInformationBuilder debugEmitter;
private JavaScriptTarget javaScriptTarget; private JavaScriptTarget javaScriptTarget;
private WasmTarget webAssemblyTarget; private WasmTarget webAssemblyTarget;
private WasmBinaryVersion wasmVersion = WasmBinaryVersion.V_0xD; private WasmBinaryVersion wasmVersion = WasmBinaryVersion.V_0x1;
public File getTargetDirectory() { public File getTargetDirectory() {
return targetDirectory; return targetDirectory;

View File

@ -78,7 +78,7 @@ public class TeaVMCompileMojo extends AbstractTeaVMMojo {
private TeaVMTool tool = new TeaVMTool(); private TeaVMTool tool = new TeaVMTool();
@Parameter @Parameter
private WasmBinaryVersion wasmVersion = WasmBinaryVersion.V_0xD; private WasmBinaryVersion wasmVersion = WasmBinaryVersion.V_0x1;
@Override @Override
protected File getTargetDirectory() { protected File getTargetDirectory() {