-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,359 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies { | ||
compile project(':classpy-common') | ||
} |
23 changes: 23 additions & 0 deletions
23
classpy-wasm/src/main/java/com/github/zxh/classpy/wasm/Vector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.github.zxh.classpy.wasm; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class Vector extends WasmBinComponent { | ||
|
||
private Supplier<? extends WasmBinComponent> supplier; | ||
|
||
public Vector(Supplier<? extends WasmBinComponent> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
@Override | ||
protected void readContent(WasmBinReader reader) { | ||
int length = readU32(reader, "length"); | ||
for (int i = 0; i < length; i++) { | ||
WasmBinComponent element = supplier.get(); | ||
add(null, element); | ||
element.read(reader); | ||
} | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
classpy-wasm/src/main/java/com/github/zxh/classpy/wasm/WasmBinComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.github.zxh.classpy.wasm; | ||
|
||
import com.github.zxh.classpy.common.FileComponent; | ||
import com.github.zxh.classpy.wasm.instructions.Expr; | ||
import com.github.zxh.classpy.wasm.types.ValType; | ||
import com.github.zxh.classpy.wasm.values.Byte; | ||
import com.github.zxh.classpy.wasm.values.Bytes; | ||
import com.github.zxh.classpy.wasm.values.Name; | ||
import com.github.zxh.classpy.wasm.values.U32; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class WasmBinComponent extends FileComponent { | ||
|
||
public final void read(WasmBinReader reader) { | ||
try { | ||
int offset = reader.getPosition(); | ||
readContent(reader); | ||
int length = reader.getPosition() - offset; | ||
super.setOffset(offset); | ||
super.setLength(length); | ||
} catch (Exception e) { | ||
System.out.println("error parsing: " + getClass()); | ||
throw e; | ||
} | ||
} | ||
|
||
protected void readContent(WasmBinReader reader) { | ||
for (FileComponent fc : getComponents()) { | ||
((WasmBinComponent) fc).read(reader); | ||
} | ||
} | ||
|
||
protected void postRead() { | ||
|
||
} | ||
|
||
protected int readU32(WasmBinReader reader, String name) { | ||
U32 u32 = new U32(); | ||
add(name, u32); | ||
u32.read(reader); | ||
return u32.getIntValue(); | ||
} | ||
|
||
protected int readByte(WasmBinReader reader, String name) { | ||
Byte b = new Byte(); | ||
add(name, b); | ||
b.read(reader); | ||
return b.getValue(); | ||
} | ||
|
||
protected int readByte(WasmBinReader reader, String name, byte... expectedValues) { | ||
Byte b = new Byte(expectedValues); | ||
add(name, b); | ||
b.read(reader); | ||
return b.getValue(); | ||
} | ||
|
||
protected byte[] readBytes(WasmBinReader reader, String name, int n) { | ||
Bytes bytes = new Bytes(n); | ||
add(name, bytes); | ||
bytes.read(reader); | ||
return bytes.getBytes(); | ||
} | ||
|
||
protected <T extends WasmBinComponent> T read(WasmBinReader reader, | ||
String name, | ||
Supplier<T> supplier) { | ||
T c = supplier.get(); | ||
add(name, c); | ||
c.read(reader); | ||
return c; | ||
} | ||
|
||
protected void readVector(WasmBinReader reader, String name, | ||
Supplier<? extends WasmBinComponent> supplier) { | ||
Vector vec = new Vector(supplier); | ||
add(name, vec); | ||
vec.read(reader); | ||
} | ||
|
||
protected void _byte(String name, byte... expectedValues) { | ||
add(name, new Byte(expectedValues)); | ||
} | ||
|
||
protected void u32(String name) { | ||
add(name, new U32()); | ||
} | ||
|
||
protected void name(String name) { | ||
add(name, new Name()); | ||
} | ||
|
||
protected void valType(String name) { | ||
add(name, new ValType()); | ||
} | ||
|
||
protected void expr(String name) { | ||
add(name, new Expr()); | ||
} | ||
|
||
protected void vector(String name, | ||
Supplier<? extends WasmBinComponent> supplier) { | ||
add(name, new Vector(supplier)); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
classpy-wasm/src/main/java/com/github/zxh/classpy/wasm/WasmBinFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.zxh.classpy.wasm; | ||
|
||
import com.github.zxh.classpy.wasm.sections.Section; | ||
|
||
public class WasmBinFile extends WasmBinComponent { | ||
|
||
protected void readContent(WasmBinReader reader) { | ||
readBytes(reader, "magic", 4); | ||
readBytes(reader, "version", 4); | ||
readSections(reader); | ||
} | ||
|
||
private void readSections(WasmBinReader reader) { | ||
while (reader.remaining() > 0) { | ||
Section section = new Section(); | ||
add("section", section); | ||
section.read(reader); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
classpy-wasm/src/main/java/com/github/zxh/classpy/wasm/WasmBinParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.zxh.classpy.wasm; | ||
|
||
import com.github.zxh.classpy.common.FileComponent; | ||
import com.github.zxh.classpy.common.FileParser; | ||
|
||
public class WasmBinParser implements FileParser { | ||
|
||
@Override | ||
public WasmBinFile parse(byte[] data) { | ||
WasmBinFile wasm = new WasmBinFile(); | ||
try { | ||
wasm.read(new WasmBinReader(data)); | ||
postRead(wasm); | ||
} catch (Exception e) { | ||
e.printStackTrace(System.err); | ||
} | ||
return wasm; | ||
} | ||
|
||
private static void postRead(WasmBinComponent bc) { | ||
for (FileComponent c : bc.getComponents()) { | ||
postRead((WasmBinComponent) c); | ||
} | ||
bc.postRead(); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
classpy-wasm/src/main/java/com/github/zxh/classpy/wasm/WasmBinReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.github.zxh.classpy.wasm; | ||
|
||
import com.github.zxh.classpy.common.BytesReader; | ||
|
||
import java.nio.ByteOrder; | ||
|
||
public class WasmBinReader extends BytesReader { | ||
|
||
public WasmBinReader(byte[] data) { | ||
super(data, ByteOrder.LITTLE_ENDIAN); // ? | ||
} | ||
|
||
public long readU32() { | ||
return readUnsignedLEB128(32); | ||
} | ||
|
||
public long readU64() { | ||
return readUnsignedLEB128(64); | ||
} | ||
|
||
public long readS32() { | ||
return readSignedLEB128(32); | ||
} | ||
|
||
public long readS64() { | ||
return readSignedLEB128(64); | ||
} | ||
|
||
private long readUnsignedLEB128(int nBits) { | ||
long result = 0; | ||
for (int shift = 0; shift < nBits; shift += 7) { | ||
int b = readByte(); | ||
result |= ((b & 0b0111_1111) << shift); | ||
if ((b & 0b1000_0000) == 0) { | ||
return result; | ||
} | ||
} | ||
|
||
throw new RuntimeException("can not decode unsigned LEB128"); | ||
} | ||
|
||
private long readSignedLEB128(int size) { | ||
long result = 0; | ||
int shift = 0; | ||
//size = number of bits in signed integer; | ||
byte b; | ||
do{ | ||
b = readByte(); | ||
result |= ((b & 0b0111_1111) << shift); | ||
shift += 7; | ||
} while ((b & 0b1000_0000) != 0); | ||
|
||
/* sign bit of byte is second high order bit (0x40) */ | ||
if ((shift < size) && ((b & 0b0100_0000) != 0)) { | ||
/* sign extend */ | ||
result |= (~0 << shift); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
classpy-wasm/src/main/java/com/github/zxh/classpy/wasm/instructions/Expr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.zxh.classpy.wasm.instructions; | ||
|
||
import com.github.zxh.classpy.wasm.WasmBinComponent; | ||
import com.github.zxh.classpy.wasm.WasmBinReader; | ||
|
||
public class Expr extends WasmBinComponent { | ||
|
||
@Override | ||
protected void readContent(WasmBinReader reader) { | ||
while (reader.remaining() > 0) { | ||
Instr instr = read(reader, null, Instr::new); | ||
if (instr.getOpcode() == 0x0B) { // end | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.