#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;
* debugger;
* interoperation with JavaScript libraries together with the set of predefined browser interfaces.
* supports WebAssebly output (experimental).
* supports WebAssembly output (experimental).
Quick start

View File

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

View File

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

View File

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

View File

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

View File

@ -15,11 +15,22 @@
*/
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.URL;
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.tooling.ClassAlias;
import org.teavm.tooling.RuntimeCopyOperation;
@ -315,14 +326,8 @@ public final class TeaVMRunner {
try {
int version = Integer.parseInt(value);
switch (version) {
case 11:
tool.setWasmVersion(WasmBinaryVersion.V_0xB);
break;
case 12:
tool.setWasmVersion(WasmBinaryVersion.V_0xC);
break;
case 13:
tool.setWasmVersion(WasmBinaryVersion.V_0xD);
case 1:
tool.setWasmVersion(WasmBinaryVersion.V_0x1);
break;
default:
System.err.print("Wrong version value");

View File

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

View File

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