35 Commits

Author SHA1 Message Date
vms
3b1c314c35 fix some comments in FuncContext 2018-10-17 12:36:55 +03:00
vms
f1a2d14463 update outdated urls to WebAssembly specification 2018-09-14 18:48:30 +03:00
vms
ab269fdbd8 fix the issue with absence of bintrayUser and bintrayKey parameters 2018-09-14 18:35:30 +03:00
da70c9fca4 Fix for "lateinit property logger has not been initialized" 2018-08-28 16:50:37 +04:00
a1a5563367 Publishing the fork to binTray 2018-08-14 17:32:10 +04:00
e489e7c889 Publishing the fork to binTray 2018-08-14 17:22:30 +04:00
c1391b2701 Expected but isn't working version 2018-08-14 12:11:48 +04:00
6b28c5a93b merge original-master to master 2018-08-10 13:10:28 +04:00
9dc4112512 Add beginning of CLI for func split issue #19 2018-07-29 01:18:39 -05:00
01d89947e5 Complete main func splitting impl with test 2018-07-28 23:28:50 -05:00
4373676448 Begin test for large split func on issue #19 2018-07-27 17:03:55 -05:00
94953a4ada More work on large func splitter for issue #19 2018-07-27 15:16:37 -05:00
472579020a Early code for WASM function splitting for issue #19 2018-07-27 02:56:30 -05:00
75de1d76e3 Finish stack walker 2018-07-27 01:15:48 -05:00
67e914d683 Begin abstracting stack walking for issue #19 2018-07-26 17:17:43 -05:00
4bc12f4b94 Begin work on Go examples for #14 2018-07-26 17:16:11 -05:00
1990f46743 merge asmble-master to master 2018-07-26 10:49:28 +04:00
559df45f09 Merge pull request #1 from cretz/master
Fetch master changes
2018-07-26 10:34:42 +04:00
73862e9bc9 Chunk data sections even smaller per #18 and update README explanation 2018-07-26 00:27:17 -05:00
1127b61eb5 Update README explanation about string const max 2018-07-26 00:08:40 -05:00
a66c05ad4a Support large data sections. Fixes #18 2018-07-26 00:05:18 -05:00
6786350f53 Fixed to set proper stack diff size for store insns 2018-07-26 00:03:49 -05:00
706da0d486 Removed prefixed dollar sign from sexpr names and add export names to dedupe check for issue #17 2018-07-25 16:30:48 -05:00
1d5c1e527a Emit given names in compiled class. Fixes #17 2018-07-25 15:59:27 -05:00
1430bf48a6 Support names in converters for issue #17 2018-07-25 15:19:25 -05:00
96febbecd5 Beginning of name support for issue #17 2018-07-25 12:57:54 -05:00
80a8a1fbb9 Temporary disable rust-regex example 2018-07-25 10:35:37 +04:00
dd72c7124c Fix Rust Simple and Rust String examples 2018-07-25 10:21:38 +04:00
c04a3c4a9b Add some java docs 2018-07-24 11:00:41 +04:00
51520ac07d Add some java docs 2018-07-23 12:52:30 +04:00
3c25b40c40 Maven fetch instructions 2018-07-20 16:15:26 -05:00
96458bdec7 Maven publishing support. Fixes #15 2018-07-20 15:59:03 -05:00
97660de6ba Add some java docs 2018-07-20 12:52:21 +04:00
cee7a86773 Fix rust-regex example 2018-07-19 17:02:25 +04:00
cfa4a35af1 remove C example 2018-07-19 09:14:01 +04:00
48 changed files with 1505 additions and 250 deletions

2
.gitignore vendored
View File

@ -16,6 +16,8 @@
/annotations/out
/examples/c-simple/bin
/examples/c-simple/build
/examples/go-simple/bin
/examples/go-simple/build
/examples/rust-simple/Cargo.lock
/examples/rust-simple/bin
/examples/rust-simple/build

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017 Chad Retz
Copyright (c) 2018 Chad Retz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -183,9 +183,16 @@ JVM languages.
### Getting
The latest tag can be added to your build script via [JitPack](https://jitpack.io). For example,
[here](https://jitpack.io/#cretz/asmble/0.1.0) are instructions for using the 0.1.0 release and
[here](https://jitpack.io/#cretz/asmble/master-SNAPSHOT) are instructions for the latest master.
The compiler and annotations are deployed to Maven Central. The compiler is written in Kotlin and can be added as a
Gradle dependency with:
compile 'com.github.cretz.asmble:asmble-compiler:0.3.0'
This is only needed to compile of course, the compiled code has no runtime requirement. The compiled code does include
some annotations (but in Java its ok to have annotations that are not found). If you do want to reflect the annotations,
the annotation library can be added as a Gradle dependency with:
compile 'com.github.cretz.asmble:asmble-annotations:0.3.0'
### Building and Testing
@ -366,7 +373,10 @@ and the JVM:
* WebAssembly has a nice data section for byte arrays whereas the JVM does not. Right now we use a single-byte-char
string constant (i.e. ISO-8859 charset). This saves class file size, but this means we call `String::getBytes` on
init to load bytes from the string constant.
init to load bytes from the string constant. Due to the JVM using an unsigned 16-bit int as the string constant
length, the maximum byte length is 65536. Since the string constants are stored as UTF-8 constants, they can be up to
four bytes a character. Therefore, we populate memory in data chunks no larger than 16300 (nice round number to make
sure that even in the worse case of 4 bytes per char in UTF-8 view, we're still under the max).
* The JVM makes no guarantees about trailing bits being preserved on NaN floating point representations like WebAssembly
does. This causes some mismatch on WebAssembly tests depending on how the JVM "feels" (I haven't dug into why some
bit patterns stay and some don't when NaNs are passed through methods).

View File

@ -14,17 +14,30 @@ buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'me.champeau.gradle:jmh-gradle-plugin:0.4.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
}
}
allprojects {
apply plugin: 'java'
group 'com.github.cretz.asmble'
version '0.4.0-fl'
repositories {
mavenCentral()
}
}
project(':annotations') {
javadoc {
options.links 'https://docs.oracle.com/javase/8/docs/api/'
// TODO: change when https://github.com/gradle/gradle/issues/2354 is fixed
options.addStringOption 'Xdoclint:all', '-Xdoclint:-missing'
}
publishSettings(project, 'asmble-annotations', 'Asmble WASM Annotations')
}
project(':compiler') {
apply plugin: 'kotlin'
apply plugin: 'application'
@ -45,6 +58,8 @@ project(':compiler') {
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile "org.ow2.asm:asm-debug-all:$asm_version"
}
publishSettings(project, 'asmble-compiler', 'Asmble WASM Compiler')
}
project(':examples') {
@ -53,35 +68,28 @@ project(':examples') {
compileOnly project(':compiler')
}
// C/C++ example helpers
// Go example helpers
task cToWasm {
task goToWasm {
doFirst {
mkdir 'build'
exec {
def cFileName = fileTree(dir: 'src', includes: ['*.c']).files.iterator().next()
commandLine 'clang', '--target=wasm32-unknown-unknown-wasm', '-O3', cFileName, '-c', '-o', 'build/lib.wasm'
def goFileName = fileTree(dir: '.', includes: ['*.go']).files.iterator().next()
environment 'GOOS': 'js', 'GOARCH': 'wasm'
commandLine 'go', 'build', '-o', 'build/lib.wasm', goFileName
}
}
}
task showCWast(type: JavaExec) {
dependsOn cToWasm
classpath configurations.compileClasspath
main = 'asmble.cli.MainKt'
doFirst {
args 'translate', 'build/lib.wasm'
}
}
task compileCWasm(type: JavaExec) {
dependsOn cToWasm
task compileGoWasm(type: JavaExec) {
dependsOn goToWasm
classpath configurations.compileClasspath
main = 'asmble.cli.MainKt'
doFirst {
// args 'help', 'compile'
def outFile = 'build/wasm-classes/' + wasmCompiledClassName.replace('.', '/') + '.class'
file(outFile).parentFile.mkdirs()
args 'compile', 'build/lib.wasm', wasmCompiledClassName, '-out', outFile
args 'compile', 'build/lib.wasm', wasmCompiledClassName, '-out', outFile, '-log', 'debug'
}
}
@ -134,40 +142,43 @@ project(':examples') {
}
}
project(':examples:c-simple') {
project(':examples:go-simple') {
apply plugin: 'application'
ext.wasmCompiledClassName = 'asmble.generated.CSimple'
ext.wasmCompiledClassName = 'asmble.generated.GoSimple'
dependencies {
compile files('build/wasm-classes')
}
compileJava {
dependsOn compileCWasm
dependsOn compileGoWasm
}
mainClassName = 'asmble.examples.csimple.Main'
mainClassName = 'asmble.examples.gosimple.Main'
}
project(':examples:rust-regex') {
apply plugin: 'application'
apply plugin: 'me.champeau.gradle.jmh'
ext.wasmCompiledClassName = 'asmble.generated.RustRegex'
dependencies {
compile files('build/wasm-classes')
testCompile 'junit:junit:4.12'
}
compileJava {
dependsOn compileRustWasm
}
mainClassName = 'asmble.examples.rustregex.Main'
test {
testLogging.showStandardStreams = true
testLogging.events 'PASSED', 'SKIPPED'
}
jmh {
iterations = 5
warmupIterations = 5
fork = 3
}
}
// todo temporary disable Rust regex, because some strings in wasm code exceed the size in 65353 bytes.
// project(':examples:rust-regex') {
// apply plugin: 'application'
// apply plugin: 'me.champeau.gradle.jmh'
// ext.wasmCompiledClassName = 'asmble.generated.RustRegex'
// dependencies {
// compile files('build/wasm-classes')
// testCompile 'junit:junit:4.12'
// }
// compileJava {
// dependsOn compileRustWasm
// }
// mainClassName = 'asmble.examples.rustregex.Main'
// test {
// testLogging.showStandardStreams = true
// testLogging.events 'PASSED', 'SKIPPED'
// }
// jmh {
// iterations = 5
// warmupIterations = 5
// fork = 3
// }
// }
project(':examples:rust-simple') {
apply plugin: 'application'
@ -191,4 +202,62 @@ project(':examples:rust-string') {
dependsOn compileRustWasm
}
mainClassName = 'asmble.examples.ruststring.Main'
}
}
def publishSettings(project, projectName, projectDescription) {
project.with {
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'
apply plugin: 'maven'
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
classifier = 'sources'
}
publishing {
publications {
MyPublication(MavenPublication) {
from components.java
groupId group
artifactId projectName
artifact sourcesJar
version version
}
}
}
bintray {
if(!hasProperty("bintrayUser") || !hasProperty("bintrayKey")) {
return
}
user = bintrayUser
key = bintrayKey
publications = ['MyPublication']
//[Default: false] Whether to override version artifacts already published
override = false
//[Default: false] Whether version should be auto published after an upload
publish = true
pkg {
repo = 'releases'
name = projectName
userOrg = 'fluencelabs'
licenses = ['MIT']
vcsUrl = 'https://github.com/fluencelabs/asmble'
version {
name = project.version
desc = projectDescription
released = new Date()
vcsTag = project.version
}
}
}
}
}

View File

@ -3,7 +3,19 @@ package asmble.ast
import java.util.*
import kotlin.reflect.KClass
/**
* All WebAssembly AST nodes as static inner classes.
*/
sealed class Node {
/**
* Wasm module definition.
*
* The unit of WebAssembly code is the module. A module collects definitions
* for types, functions, tables, memories, and globals. In addition, it can
* declare imports and exports and provide initialization logic in the form
* of data and element segments or a start function.
*/
data class Module(
val types: List<Type.Func> = emptyList(),
val imports: List<Import> = emptyList(),
@ -15,6 +27,7 @@ sealed class Node {
val elems: List<Elem> = emptyList(),
val funcs: List<Func> = emptyList(),
val data: List<Data> = emptyList(),
val names: NameSection? = null,
val customSections: List<CustomSection> = emptyList()
) : Node()
@ -148,6 +161,12 @@ sealed class Node {
}
}
data class NameSection(
val moduleName: String?,
val funcNames: Map<Int, String>,
val localNames: Map<Int, Map<Int, String>>
) : Node()
sealed class Instr : Node() {
fun op() = InstrOp.classToOpMap[this::class] ?: throw Exception("No op found for ${this::class}")
@ -165,12 +184,15 @@ sealed class Node {
interface Const<out T : Number> : Args { val value: T }
}
// Control flow
// Control instructions [https://www.w3.org/TR/wasm-core-1/#control-instructions]
object Unreachable : Instr(), Args.None
object Nop : Instr(), Args.None
data class Block(override val type: Type.Value?) : Instr(), Args.Type
data class Loop(override val type: Type.Value?) : Instr(), Args.Type
data class If(override val type: Type.Value?) : Instr(), Args.Type
object Else : Instr(), Args.None
object End : Instr(), Args.None
data class Br(override val relativeDepth: Int) : Instr(), Args.RelativeDepth
@ -181,25 +203,27 @@ sealed class Node {
) : Instr(), Args.Table
object Return : Instr()
// Call operators
data class Call(override val index: Int) : Instr(), Args.Index
data class CallIndirect(
override val index: Int,
override val reserved: Boolean
) : Instr(), Args.ReservedIndex
// Parametric operators
// Parametric instructions [https://www.w3.org/TR/wasm-core-1/#parametric-instructions]
object Drop : Instr(), Args.None
object Select : Instr(), Args.None
// Variable access
// Variable instructions [https://www.w3.org/TR/wasm-core-1/#variable-instructions]
data class GetLocal(override val index: Int) : Instr(), Args.Index
data class SetLocal(override val index: Int) : Instr(), Args.Index
data class TeeLocal(override val index: Int) : Instr(), Args.Index
data class GetGlobal(override val index: Int) : Instr(), Args.Index
data class SetGlobal(override val index: Int) : Instr(), Args.Index
// Memory operators
// Memory instructions [https://www.w3.org/TR/wasm-core-1/#memory-instructions]
data class I32Load(override val align: Int, override val offset: Long) : Instr(), Args.AlignOffset
data class I64Load(override val align: Int, override val offset: Long) : Instr(), Args.AlignOffset
data class F32Load(override val align: Int, override val offset: Long) : Instr(), Args.AlignOffset
@ -226,7 +250,9 @@ sealed class Node {
data class MemorySize(override val reserved: Boolean) : Instr(), Args.Reserved
data class MemoryGrow(override val reserved: Boolean) : Instr(), Args.Reserved
// Constants
// Numeric instructions [https://www.w3.org/TR/wasm-core-1/#numeric-instructions]
// Constants operators
data class I32Const(override val value: Int) : Instr(), Args.Const<Int>
data class I64Const(override val value: Long) : Instr(), Args.Const<Long>
data class F32Const(override val value: Float) : Instr(), Args.Const<Float>

View File

@ -2,10 +2,16 @@ package asmble.ast
import asmble.io.SExprToStr
/**
* Ast representation of wasm S-expressions (wast format).
* see [[https://webassembly.github.io/spec/core/text/index.html]]
*/
sealed class SExpr {
data class Multi(val vals: List<SExpr> = emptyList()) : SExpr() {
override fun toString() = SExprToStr.Compact.fromSExpr(this)
}
data class Symbol(
val contents: String = "",
val quoted: Boolean = false,
@ -15,4 +21,5 @@ sealed class SExpr {
// This is basically the same as the deprecated java.lang.String#getBytes
fun rawContentCharsToBytes() = contents.toCharArray().map(Char::toByte)
}
}

View File

@ -1,6 +1,10 @@
package asmble.ast
/**
* Ast representation of wasm script.
*/
data class Script(val commands: List<Cmd>) {
sealed class Cmd {
data class Module(val module: Node.Module, val name: String?): Cmd()
data class Register(val string: String, val name: String?): Cmd()

View File

@ -0,0 +1,250 @@
package asmble.ast
// This is a utility for walking the stack. It can do validation or just walk naively.
data class Stack(
// If some of these values below are null, the pops/pushes may appear "unknown"
val mod: CachedModule? = null,
val func: Node.Func? = null,
// Null if not tracking the current stack and all pops succeed
val current: List<Node.Type.Value>? = null,
val insnApplies: List<InsnApply> = emptyList(),
val strict: Boolean = false,
val unreachableUntilNextEndCount: Int = 0
) {
fun next(v: Node.Instr, callFuncTypeOverride: Node.Type.Func? = null) = insnApply(v) {
// If we're unreachable, and not an end, we skip and move on
if (unreachableUntilNextEndCount > 0 && v !is Node.Instr.End) {
// If it's a block, we increase it because we'll see another end
return@insnApply if (v is Node.Instr.Args.Type) unreachable(unreachableUntilNextEndCount + 1) else nop()
}
when (v) {
is Node.Instr.Nop, is Node.Instr.Block, is Node.Instr.Loop -> nop()
is Node.Instr.If, is Node.Instr.BrIf -> popI32()
is Node.Instr.Return -> (func?.type?.ret?.let { pop(it) } ?: nop()) + unreachable(1)
is Node.Instr.Unreachable -> unreachable(1)
is Node.Instr.End, is Node.Instr.Else -> {
// Put back what was before the last block and add the block's type
// Go backwards to find the starting block
var currDepth = 0
val found = insnApplies.findLast {
when (it.insn) {
is Node.Instr.End -> { currDepth++; false }
is Node.Instr.Args.Type -> if (currDepth > 0) { currDepth--; false } else true
else -> false
}
}?.takeIf {
// When it's else, needs to be if
v !is Node.Instr.Else || it.insn is Node.Instr.If
}
val changes = when {
found != null && found.insn is Node.Instr.Args.Type &&
found.stackAtBeginning != null && this != null -> {
// Pop everything from before the block's start, then push if necessary...
// The If block includes an int at the beginning we must not include when subtracting
var preBlockStackSize = found.stackAtBeginning.size
if (found.insn is Node.Instr.If) preBlockStackSize--
val popped =
if (unreachableUntilNextEndCount > 1) nop()
else (0 until (size - preBlockStackSize)).flatMap { pop() }
// Only push if this is not an else
val pushed =
if (unreachableUntilNextEndCount > 1 || v is Node.Instr.Else) nop()
else (found.insn.type?.let { push(it) } ?: nop())
popped + pushed
}
strict -> error("Unable to find starting block for end")
else -> nop()
}
if (unreachableUntilNextEndCount > 0) changes + unreachable(unreachableUntilNextEndCount - 1)
else changes
}
is Node.Instr.Br -> unreachable(v.relativeDepth + 1)
is Node.Instr.BrTable -> popI32() + unreachable(1)
is Node.Instr.Call -> (callFuncTypeOverride ?: func(v.index)).let {
if (it == null) error("Call func type missing")
it.params.reversed().flatMap { pop(it) } + (it.ret?.let { push(it) } ?: nop())
}
is Node.Instr.CallIndirect -> (callFuncTypeOverride ?: mod?.mod?.types?.getOrNull(v.index)).let {
if (it == null) error("Call func type missing")
// We add one for the table index
popI32() + it.params.reversed().flatMap { pop(it) } + (it.ret?.let { push(it) } ?: nop())
}
is Node.Instr.Drop -> pop()
is Node.Instr.Select -> popI32() + pop().let { it + pop(it.first().type) + push(it.first().type) }
is Node.Instr.GetLocal -> push(local(v.index))
is Node.Instr.SetLocal -> pop(local(v.index))
is Node.Instr.TeeLocal -> local(v.index).let { pop(it) + push(it) }
is Node.Instr.GetGlobal -> push(global(v.index))
is Node.Instr.SetGlobal -> pop(global(v.index))
is Node.Instr.I32Load, is Node.Instr.I32Load8S, is Node.Instr.I32Load8U,
is Node.Instr.I32Load16U, is Node.Instr.I32Load16S -> popI32() + pushI32()
is Node.Instr.I64Load, is Node.Instr.I64Load8S, is Node.Instr.I64Load8U, is Node.Instr.I64Load16U,
is Node.Instr.I64Load16S, is Node.Instr.I64Load32S, is Node.Instr.I64Load32U -> popI32() + pushI64()
is Node.Instr.F32Load -> popI32() + pushF32()
is Node.Instr.F64Load -> popI32() + pushF64()
is Node.Instr.I32Store, is Node.Instr.I32Store8, is Node.Instr.I32Store16 -> popI32() + popI32()
is Node.Instr.I64Store, is Node.Instr.I64Store8,
is Node.Instr.I64Store16, is Node.Instr.I64Store32 -> popI64() + popI32()
is Node.Instr.F32Store -> popF32() + popI32()
is Node.Instr.F64Store -> popF64() + popI32()
is Node.Instr.MemorySize -> pushI32()
is Node.Instr.MemoryGrow -> popI32() + pushI32()
is Node.Instr.I32Const -> pushI32()
is Node.Instr.I64Const -> pushI64()
is Node.Instr.F32Const -> pushF32()
is Node.Instr.F64Const -> pushF64()
is Node.Instr.I32Add, is Node.Instr.I32Sub, is Node.Instr.I32Mul, is Node.Instr.I32DivS,
is Node.Instr.I32DivU, is Node.Instr.I32RemS, is Node.Instr.I32RemU, is Node.Instr.I32And,
is Node.Instr.I32Or, is Node.Instr.I32Xor, is Node.Instr.I32Shl, is Node.Instr.I32ShrS,
is Node.Instr.I32ShrU, is Node.Instr.I32Rotl, is Node.Instr.I32Rotr, is Node.Instr.I32Eq,
is Node.Instr.I32Ne, is Node.Instr.I32LtS, is Node.Instr.I32LeS, is Node.Instr.I32LtU,
is Node.Instr.I32LeU, is Node.Instr.I32GtS, is Node.Instr.I32GeS, is Node.Instr.I32GtU,
is Node.Instr.I32GeU -> popI32() + popI32() + pushI32()
is Node.Instr.I32Clz, is Node.Instr.I32Ctz, is Node.Instr.I32Popcnt,
is Node.Instr.I32Eqz -> popI32() + pushI32()
is Node.Instr.I64Add, is Node.Instr.I64Sub, is Node.Instr.I64Mul, is Node.Instr.I64DivS,
is Node.Instr.I64DivU, is Node.Instr.I64RemS, is Node.Instr.I64RemU, is Node.Instr.I64And,
is Node.Instr.I64Or, is Node.Instr.I64Xor, is Node.Instr.I64Shl, is Node.Instr.I64ShrS,
is Node.Instr.I64ShrU, is Node.Instr.I64Rotl, is Node.Instr.I64Rotr -> popI64() + popI64() + pushI64()
is Node.Instr.I64Eq, is Node.Instr.I64Ne, is Node.Instr.I64LtS, is Node.Instr.I64LeS,
is Node.Instr.I64LtU, is Node.Instr.I64LeU, is Node.Instr.I64GtS,
is Node.Instr.I64GeS, is Node.Instr.I64GtU, is Node.Instr.I64GeU -> popI64() + popI64() + pushI32()
is Node.Instr.I64Clz, is Node.Instr.I64Ctz, is Node.Instr.I64Popcnt -> popI64() + pushI64()
is Node.Instr.I64Eqz -> popI64() + pushI32()
is Node.Instr.F32Add, is Node.Instr.F32Sub, is Node.Instr.F32Mul, is Node.Instr.F32Div,
is Node.Instr.F32Min, is Node.Instr.F32Max, is Node.Instr.F32CopySign -> popF32() + popF32() + pushF32()
is Node.Instr.F32Eq, is Node.Instr.F32Ne, is Node.Instr.F32Lt, is Node.Instr.F32Le,
is Node.Instr.F32Gt, is Node.Instr.F32Ge -> popF32() + popF32() + pushI32()
is Node.Instr.F32Abs, is Node.Instr.F32Neg, is Node.Instr.F32Ceil, is Node.Instr.F32Floor,
is Node.Instr.F32Trunc, is Node.Instr.F32Nearest, is Node.Instr.F32Sqrt -> popF32() + pushF32()
is Node.Instr.F64Add, is Node.Instr.F64Sub, is Node.Instr.F64Mul, is Node.Instr.F64Div,
is Node.Instr.F64Min, is Node.Instr.F64Max, is Node.Instr.F64CopySign -> popF64() + popF64() + pushF64()
is Node.Instr.F64Eq, is Node.Instr.F64Ne, is Node.Instr.F64Lt, is Node.Instr.F64Le,
is Node.Instr.F64Gt, is Node.Instr.F64Ge -> popF64() + popF64() + pushI32()
is Node.Instr.F64Abs, is Node.Instr.F64Neg, is Node.Instr.F64Ceil, is Node.Instr.F64Floor,
is Node.Instr.F64Trunc, is Node.Instr.F64Nearest, is Node.Instr.F64Sqrt -> popF64() + pushF64()
is Node.Instr.I32WrapI64 -> popI64() + pushI32()
is Node.Instr.I32TruncSF32, is Node.Instr.I32TruncUF32,
is Node.Instr.I32ReinterpretF32 -> popF32() + pushI32()
is Node.Instr.I32TruncSF64, is Node.Instr.I32TruncUF64 -> popF64() + pushI32()
is Node.Instr.I64ExtendSI32, is Node.Instr.I64ExtendUI32 -> popI32() + pushI64()
is Node.Instr.I64TruncSF32, is Node.Instr.I64TruncUF32 -> popF32() + pushI64()
is Node.Instr.I64TruncSF64, is Node.Instr.I64TruncUF64,
is Node.Instr.I64ReinterpretF64 -> popF64() + pushI64()
is Node.Instr.F32ConvertSI32, is Node.Instr.F32ConvertUI32,
is Node.Instr.F32ReinterpretI32 -> popI32() + pushF32()
is Node.Instr.F32ConvertSI64, is Node.Instr.F32ConvertUI64 -> popI64() + pushF32()
is Node.Instr.F32DemoteF64 -> popF64() + pushF32()
is Node.Instr.F64ConvertSI32, is Node.Instr.F64ConvertUI32 -> popI32() + pushF64()
is Node.Instr.F64ConvertSI64, is Node.Instr.F64ConvertUI64,
is Node.Instr.F64ReinterpretI64 -> popI64() + pushF64()
is Node.Instr.F64PromoteF32 -> popF32() + pushF64()
}
}
protected fun insnApply(v: Node.Instr, fn: MutableList<Node.Type.Value>?.() -> List<InsnApplyResponse>): Stack {
val mutStack = current?.toMutableList()
val applyResp = mutStack.fn()
val newUnreachable = (applyResp.find { it is Unreachable } as? Unreachable)?.untilEndCount
return copy(
current = mutStack,
insnApplies = insnApplies + InsnApply(
insn = v,
stackAtBeginning = current,
stackChanges = applyResp.mapNotNull { it as? StackChange },
unreachableUntilEndCount = newUnreachable ?: unreachableUntilNextEndCount
),
unreachableUntilNextEndCount = newUnreachable ?: unreachableUntilNextEndCount
)
}
protected fun unreachable(untilEndCount: Int) = listOf(Unreachable(untilEndCount))
protected fun local(index: Int) = func?.let {
it.type.params.getOrNull(index) ?: it.locals.getOrNull(index - it.type.params.size)
}
protected fun global(index: Int) = mod?.let {
it.importGlobals.getOrNull(index)?.type?.contentType ?:
it.mod.globals.getOrNull(index - it.importGlobals.size)?.type?.contentType
}
protected fun func(index: Int) = mod?.let {
it.importFuncs.getOrNull(index)?.typeIndex?.let { i -> it.mod.types.getOrNull(i) } ?:
it.mod.funcs.getOrNull(index - it.importFuncs.size)?.type
}
protected fun nop() = emptyList<StackChange>()
protected fun MutableList<Node.Type.Value>?.popType(expecting: Node.Type.Value? = null) =
this?.takeIf {
it.isNotEmpty().also {
require(!strict || it) { "Expected $expecting got empty" }
}
}?.let {
removeAt(size - 1).takeIf { actual -> (expecting == null || actual == expecting).also {
require(!strict || it) { "Expected $expecting got $actual" }
} }
} ?: expecting
protected fun MutableList<Node.Type.Value>?.pop(expecting: Node.Type.Value? = null) =
listOf(StackChange(popType(expecting), true))
protected fun MutableList<Node.Type.Value>?.popI32() = pop(Node.Type.Value.I32)
protected fun MutableList<Node.Type.Value>?.popI64() = pop(Node.Type.Value.I64)
protected fun MutableList<Node.Type.Value>?.popF32() = pop(Node.Type.Value.F32)
protected fun MutableList<Node.Type.Value>?.popF64() = pop(Node.Type.Value.F64)
protected fun MutableList<Node.Type.Value>?.push(type: Node.Type.Value? = null) =
listOf(StackChange(type, false)).also { if (this != null && type != null) add(type) }
protected fun MutableList<Node.Type.Value>?.pushI32() = push(Node.Type.Value.I32)
protected fun MutableList<Node.Type.Value>?.pushI64() = push(Node.Type.Value.I64)
protected fun MutableList<Node.Type.Value>?.pushF32() = push(Node.Type.Value.F32)
protected fun MutableList<Node.Type.Value>?.pushF64() = push(Node.Type.Value.F64)
data class InsnApply(
val insn: Node.Instr,
val stackAtBeginning: List<Node.Type.Value>?,
val stackChanges: List<StackChange>,
val unreachableUntilEndCount: Int
)
protected interface InsnApplyResponse
data class StackChange(
val type: Node.Type.Value?,
val pop: Boolean
) : InsnApplyResponse
data class Unreachable(
val untilEndCount: Int
) : InsnApplyResponse
class CachedModule(val mod: Node.Module) {
val importFuncs by lazy { mod.imports.mapNotNull { it.kind as? Node.Import.Kind.Func } }
val importGlobals by lazy { mod.imports.mapNotNull { it.kind as? Node.Import.Kind.Global } }
}
companion object {
fun walkStrict(mod: Node.Module, func: Node.Func, afterInsn: ((Stack, Node.Instr) -> Unit)? = null) =
func.instructions.fold(Stack(
mod = CachedModule(mod),
func = func,
current = emptyList(),
strict = true
)) { stack, insn -> stack.next(insn).also { afterInsn?.invoke(it, insn) } }.also { stack ->
// We expect to be in an unreachable state at the end or have the single return value on the stack
if (stack.unreachableUntilNextEndCount == 0) {
val expectedStack = (func.type.ret?.let { listOf(it) } ?: emptyList())
require(expectedStack == stack.current) {
"Expected end to be $expectedStack, got ${stack.current}"
}
}
}
fun stackChanges(v: Node.Instr, callFuncType: Node.Type.Func? = null) =
Stack().next(v, callFuncType).insnApplies.last().stackChanges
fun stackChanges(mod: CachedModule, func: Node.Func, v: Node.Instr) =
Stack(mod, func).next(v).insnApplies.last().stackChanges
fun stackDiff(v: Node.Instr, callFuncType: Node.Type.Func? = null) =
stackChanges(v, callFuncType).sumBy { if (it.pop) -1 else 1 }
fun stackDiff(mod: CachedModule, func: Node.Func, v: Node.Instr) =
stackChanges(mod, func, v).sumBy { if (it.pop) -1 else 1 }
}
}

View File

@ -0,0 +1,183 @@
package asmble.ast.opt
import asmble.ast.Node
import asmble.ast.Stack
// This is a naive implementation that just grabs adjacent sets of restricted insns and breaks the one that will save
// the most instructions off into its own function.
open class SplitLargeFunc(
val minSetLength: Int = 5,
val maxSetLength: Int = 40,
val maxParamCount: Int = 30
) {
// Null if no replacement. Second value is number of instructions saved. fnIndex must map to actual func,
// not imported one.
fun apply(mod: Node.Module, fnIndex: Int): Pair<Node.Module, Int>? {
// Get the func
val importFuncCount = mod.imports.count { it.kind is Node.Import.Kind.Func }
val actualFnIndex = fnIndex - importFuncCount
val func = mod.funcs.getOrElse(actualFnIndex) {
error("Unable to find non-import func at $fnIndex (actual $actualFnIndex)")
}
// Just take the best pattern and apply it
val newFuncIndex = importFuncCount + mod.funcs.size
return commonPatterns(mod, func).firstOrNull()?.let { pattern ->
// Name it as <funcname>$splitN (n is num just to disambiguate) if names are part of the mod
val newName = mod.names?.funcNames?.get(fnIndex)?.let {
"$it\$split".let { it + mod.names.funcNames.count { (_, v) -> v.startsWith(it) } }
}
// Go over every replacement in reverse, changing the instructions to our new set
val newInsns = pattern.replacements.foldRight(func.instructions) { repl, insns ->
insns.take(repl.range.start) +
repl.preCallConsts +
Node.Instr.Call(newFuncIndex) +
insns.drop(repl.range.endInclusive + 1)
}
// Return the module w/ the new function, it's new name, and the insns saved
mod.copy(
funcs = mod.funcs.toMutableList().also {
it[actualFnIndex] = func.copy(instructions = newInsns)
} + pattern.newFunc,
names = mod.names?.copy(funcNames = mod.names.funcNames.toMutableMap().also {
it[newFuncIndex] = newName!!
})
) to pattern.insnsSaved
}
}
// Results are by most insns saved. There can be overlap across patterns but never within a single pattern.
fun commonPatterns(mod: Node.Module, fn: Node.Func): List<CommonPattern> {
// Walk the stack for validation needs
val stack = Stack.walkStrict(mod, fn)
// Let's grab sets of insns that qualify. In this naive impl, in order to qualify the insn set needs to
// only have a certain set of insns that can be broken off. It can also only change the stack by 0 or 1
// value while never dipping below the starting stack. We also store the index they started at.
var insnSets = emptyList<InsnSet>()
// Pair in fold keyed by insn index
fn.instructions.foldIndexed(null as List<Pair<Int, Node.Instr>>?) { index, lastInsns, insn ->
if (!insn.canBeMoved) null else (lastInsns ?: emptyList()).plus(index to insn).also { fullNewInsnSet ->
// Get all final instructions between min and max size and with allowed param count (i.e. const count)
val trailingInsnSet = fullNewInsnSet.takeLast(maxSetLength)
// Get all instructions between the min and max
insnSets += (minSetLength..maxSetLength).
asSequence().
flatMap { trailingInsnSet.asSequence().windowed(it) }.
filter { it.count { it.second is Node.Instr.Args.Const<*> } <= maxParamCount }.
mapNotNull { newIndexedInsnSet ->
// Before adding, make sure it qualifies with the stack
InsnSet(
startIndex = newIndexedInsnSet.first().first,
insns = newIndexedInsnSet.map { it.second },
valueAddedToStack = null
).withStackValueIfValid(stack)
}
}
}
// Sort the insn sets by the ones with the most insns
insnSets = insnSets.sortedByDescending { it.insns.size }
// Now let's create replacements for each, keyed by the extracted func
val patterns = insnSets.fold(emptyMap<Node.Func, List<Replacement>>()) { map, insnSet ->
insnSet.extractCommonFunc().let { (func, replacement) ->
val existingReplacements = map.getOrDefault(func, emptyList())
// Ignore if there is any overlap
if (existingReplacements.any(replacement::overlaps)) map
else map + (func to existingReplacements.plus(replacement))
}
}
// Now sort the patterns by most insns saved and return
return patterns.map { (k, v) ->
CommonPattern(k, v.sortedBy { it.range.first })
}.sortedByDescending { it.insnsSaved }
}
val Node.Instr.canBeMoved get() =
// No blocks
this !is Node.Instr.Block && this !is Node.Instr.Loop && this !is Node.Instr.If &&
this !is Node.Instr.Else && this !is Node.Instr.End &&
// No breaks
this !is Node.Instr.Br && this !is Node.Instr.BrIf && this !is Node.Instr.BrTable &&
// No return
this !is Node.Instr.Return &&
// No local access
this !is Node.Instr.GetLocal && this !is Node.Instr.SetLocal && this !is Node.Instr.TeeLocal
fun InsnSet.withStackValueIfValid(stack: Stack): InsnSet? {
// This makes sure that the stack only changes by at most one item and never dips below its starting val.
// If it is invalid, null is returned. If it qualifies and does change 1 value, it is set.
// First, make sure the stack after the last insn is the same as the first or the same + 1 val
val startingStack = stack.insnApplies[startIndex].stackAtBeginning!!
val endingStack = stack.insnApplies.getOrNull(startIndex + insns.size)?.stackAtBeginning ?: stack.current!!
if (endingStack.size != startingStack.size && endingStack.size != startingStack.size + 1) return null
if (endingStack.take(startingStack.size) != startingStack) return null
// Now, walk the insns and make sure they never pop below the start
var stackCounter = 0
stack.insnApplies.subList(startIndex, startIndex + insns.size).forEach {
it.stackChanges.forEach {
stackCounter += if (it.pop) -1 else 1
if (stackCounter < 0) return null
}
}
// We're good, now only if the ending stack is one over the start do we have a ret val
return copy(
valueAddedToStack = endingStack.lastOrNull()?.takeIf { endingStack.size == startingStack.size + 1 }
)
}
fun InsnSet.extractCommonFunc() =
// This extracts a function with constants changed to parameters
insns.fold(Pair(
Node.Func(Node.Type.Func(params = emptyList(), ret = valueAddedToStack), emptyList(), emptyList()),
Replacement(range = startIndex until startIndex + insns.size, preCallConsts = emptyList()))
) { (func, repl), insn ->
if (insn !is Node.Instr.Args.Const<*>) func.copy(instructions = func.instructions + insn) to repl
else func.copy(
type = func.type.copy(params = func.type.params + insn.constType),
instructions = func.instructions + Node.Instr.GetLocal(func.type.params.size)
) to repl.copy(preCallConsts = repl.preCallConsts + insn)
}
protected val Node.Instr.Args.Const<*>.constType get() = when (this) {
is Node.Instr.I32Const -> Node.Type.Value.I32
is Node.Instr.I64Const -> Node.Type.Value.I64
is Node.Instr.F32Const -> Node.Type.Value.F32
is Node.Instr.F64Const -> Node.Type.Value.F64
else -> error("unreachable")
}
data class InsnSet(
val startIndex: Int,
val insns: List<Node.Instr>,
val valueAddedToStack: Node.Type.Value?
)
data class Replacement(
val range: IntRange,
val preCallConsts: List<Node.Instr>
) {
// Subtract one because there is a call after this
val insnsSaved get() = (range.last + 1) - range.first - 1 - preCallConsts.size
fun overlaps(o: Replacement) = range.contains(o.range.first) || range.contains(o.range.last) ||
o.range.contains(range.first) || o.range.contains(range.last)
}
data class CommonPattern(
val newFunc: Node.Func,
// In order by earliest replacement first
val replacements: List<Replacement>
) {
// Replacement pieces saved (with one added for the invocation) less new func instructions
val insnsSaved get() = replacements.sumBy { it.insnsSaved } - newFunc.instructions.size
}
companion object : SplitLargeFunc()
}

View File

@ -51,7 +51,7 @@ open class Compile : Command<Compile.Args>() {
val inFormat =
if (args.inFormat != "<use file extension>") args.inFormat
else args.inFile.substringAfterLast('.', "<unknown>")
val script = Translate.inToAst(args.inFile, inFormat)
val script = Translate().also { it.logger = logger }.inToAst(args.inFile, inFormat)
val mod = (script.commands.firstOrNull() as? Script.Cmd.Module) ?:
error("Only a single sexpr for (module) allowed")
val outStream = when (args.outFile) {

View File

@ -3,6 +3,9 @@ package asmble.cli
import asmble.compile.jvm.javaIdent
import asmble.run.jvm.Module
/**
* This class provide ''invoke'' WASM code functionality.
*/
open class Invoke : ScriptCommand<Invoke.Args>() {
override val name = "invoke"
@ -34,6 +37,7 @@ open class Invoke : ScriptCommand<Invoke.Args>() {
).also { bld.done() }
override fun run(args: Args) {
// Compiles wasm to bytecode, do registrations and so on.
val ctx = prepareContext(args.scriptArgs)
// Instantiate the module
val module =
@ -41,11 +45,11 @@ open class Invoke : ScriptCommand<Invoke.Args>() {
else ctx.registrations[args.module] as? Module.Instance ?:
error("Unable to find module registered as ${args.module}")
// Just make sure the module is instantiated here...
module.instance(ctx)
val instance = module.instance(ctx)
// If an export is provided, call it
if (args.export != "<start-func>") args.export.javaIdent.let { javaName ->
val method = module.cls.declaredMethods.find { it.name == javaName } ?:
error("Unable to find export '${args.export}'")
// Finds java method(wasm fn) in class(wasm module) by name(declared in <start-func>)
val method = module.cls.declaredMethods.find { it.name == javaName } ?: error("Unable to find export '${args.export}'")
// Map args to params
require(method.parameterTypes.size == args.args.size) {
"Given arg count of ${args.args.size} is invalid for $method"
@ -59,11 +63,20 @@ open class Invoke : ScriptCommand<Invoke.Args>() {
else -> error("Unrecognized type for param ${index + 1}: $paramType")
}
}
val result = method.invoke(module.instance(ctx), *params.toTypedArray())
val result = method.invoke(instance, *params.toTypedArray())
if (args.resultToStdout && method.returnType != Void.TYPE) println(result)
}
}
/**
* Arguments for 'invoke' command.
*
* @param scriptArgs Common arguments for 'invoke' and 'run' ScriptCommands.
* @param module The module name to run. If it's a JVM class, it must have a no-arg constructor
* @param export The specific export function to invoke
* @param args Parameter for the export if export is present
* @param resultToStdout If true result will print to stout
*/
data class Args(
val scriptArgs: ScriptCommand.ScriptArgs,
val module: String,

View File

@ -3,8 +3,11 @@ package asmble.cli
import asmble.util.Logger
import kotlin.system.exitProcess
val commands = listOf(Compile, Help, Invoke, Link, Run, Translate)
val commands = listOf(Compile, Help, Invoke, Link, Run, SplitFunc, Translate)
/**
* Entry point of command line interface.
*/
fun main(args: Array<String>) {
if (args.isEmpty()) return println(
"""
@ -28,6 +31,7 @@ fun main(args: Array<String>) {
val globals = Main.globalArgs(argBuild)
logger = Logger.Print(globals.logLevel)
command.logger = logger
logger.info { "Running the command=${command.name} with args=${argBuild.args}" }
command.runWithArgs(argBuild)
} catch (e: Exception) {
logger.error { "Error ${command?.let { "in command '${it.name}'" } ?: ""}: ${e.message}" }

View File

@ -45,16 +45,18 @@ abstract class ScriptCommand<T> : Command<T>() {
)
fun prepareContext(args: ScriptArgs): ScriptContext {
var ctx = ScriptContext(
var context = ScriptContext(
packageName = "asmble.temp" + UUID.randomUUID().toString().replace("-", ""),
defaultMaxMemPages = args.defaultMaxMemPages
)
// Compile everything
ctx = args.inFiles.foldIndexed(ctx) { index, ctx, inFile ->
context = args.inFiles.foldIndexed(context) { index, ctx, inFile ->
try {
when (inFile.substringAfterLast('.')) {
// if input file is class file
"class" -> ctx.classLoader.addClass(File(inFile).readBytes()).let { ctx }
else -> Translate.inToAst(inFile, inFile.substringAfterLast('.')).let { inAst ->
// if input file is wasm file
else -> Translate.also { it.logger = logger }.inToAst(inFile, inFile.substringAfterLast('.')).let { inAst ->
val (mod, name) = (inAst.commands.singleOrNull() as? Script.Cmd.Module) ?:
error("Input file must only contain a single module")
val className = name?.javaIdent?.capitalize() ?:
@ -67,17 +69,28 @@ abstract class ScriptCommand<T> : Command<T>() {
}
}
}
} catch (e: Exception) { throw Exception("Failed loading $inFile - ${e.message}", e) }
} catch (e: Exception) {
throw Exception("Failed loading $inFile - ${e.message}", e)
}
}
// Do registrations
ctx = args.registrations.fold(ctx) { ctx, (moduleName, className) ->
context = args.registrations.fold(context) { ctx, (moduleName, className) ->
ctx.withModuleRegistered(moduleName,
Module.Native(Class.forName(className, true, ctx.classLoader).newInstance()))
}
if (args.specTestRegister) ctx = ctx.withHarnessRegistered()
return ctx
if (args.specTestRegister) context = context.withHarnessRegistered() // проверить что не так с "Cannot find compatible import for spectest::print"
return context
}
/**
* Common arguments for 'invoke' and 'run' ScriptCommands.
*
* @param inFiles Files to add to classpath. Can be wasm, wast, or class file
* @param registrations Register class name to a module name
* @param disableAutoRegister If set, this will not auto-register modules with names
* @param specTestRegister If true, registers the spec test harness as 'spectest'
* @param defaultMaxMemPages The maximum number of memory pages when a module doesn't say
*/
data class ScriptArgs(
val inFiles: List<String>,
val registrations: List<Pair<String, String>>,

View File

@ -0,0 +1,146 @@
package asmble.cli
import asmble.ast.Node
import asmble.ast.Script
import asmble.ast.opt.SplitLargeFunc
open class SplitFunc : Command<SplitFunc.Args>() {
override val name = "split-func"
override val desc = "Split a WebAssembly function into two"
override fun args(bld: Command.ArgsBuilder) = Args(
inFile = bld.arg(
name = "inFile",
desc = "The wast or wasm WebAssembly file name. Can be '--' to read from stdin."
),
funcName = bld.arg(
name = "funcName",
desc = "The name (or '#' + function space index) of the function to split"
),
inFormat = bld.arg(
name = "inFormat",
opt = "in",
desc = "Either 'wast' or 'wasm' to describe format.",
default = "<use file extension>",
lowPriority = true
),
outFile = bld.arg(
name = "outFile",
opt = "outFile",
desc = "The wast or wasm WebAssembly file name. Can be '--' to write to stdout.",
default = "<inFileSansExt.split.wasm or stdout>",
lowPriority = true
),
outFormat = bld.arg(
name = "outFormat",
opt = "out",
desc = "Either 'wast' or 'wasm' to describe format.",
default = "<use file extension or wast for stdout>",
lowPriority = true
),
compact = bld.flag(
opt = "compact",
desc = "If set for wast out format, will be compacted.",
lowPriority = true
),
minInsnSetLength = bld.arg(
name = "minInsnSetLength",
opt = "minLen",
desc = "The minimum number of instructions allowed for the split off function.",
default = "5",
lowPriority = true
).toInt(),
maxInsnSetLength = bld.arg(
name = "maxInsnSetLength",
opt = "maxLen",
desc = "The maximum number of instructions allowed for the split off function.",
default = "40",
lowPriority = true
).toInt(),
maxNewFuncParamCount = bld.arg(
name = "maxNewFuncParamCount",
opt = "maxParams",
desc = "The maximum number of params allowed for the split off function.",
default = "30",
lowPriority = true
).toInt(),
attempts = bld.arg(
name = "attempts",
opt = "attempts",
desc = "The number of attempts to perform.",
default = "1",
lowPriority = true
).toInt()
).also { bld.done() }
override fun run(args: Args) {
// Load the mod
val translate = Translate().also { it.logger = logger }
val inFormat =
if (args.inFormat != "<use file extension>") args.inFormat
else args.inFile.substringAfterLast('.', "<unknown>")
val script = translate.inToAst(args.inFile, inFormat)
var mod = (script.commands.firstOrNull() as? Script.Cmd.Module)?.module ?: error("Only a single module allowed")
// Do attempts
val splitter = SplitLargeFunc(
minSetLength = args.minInsnSetLength,
maxSetLength = args.maxInsnSetLength,
maxParamCount = args.maxNewFuncParamCount
)
for (attempt in 0 until args.attempts) {
// Find the function
var index = mod.names?.funcNames?.toList()?.find { it.second == args.funcName }?.first
if (index == null && args.funcName.startsWith('#')) index = args.funcName.drop(1).toInt()
val origFunc = index?.let {
mod.funcs.getOrNull(it - mod.imports.count { it.kind is Node.Import.Kind.Func })
} ?: error("Unable to find func")
// Split it
val results = splitter.apply(mod, index)
if (results == null) {
logger.warn { "No instructions after attempt $attempt" }
break
}
val (splitMod, insnsSaved) = results
val newFunc = splitMod.funcs[index - mod.imports.count { it.kind is Node.Import.Kind.Func }]
val splitFunc = splitMod.funcs.last()
logger.warn {
"Split complete, from func with ${origFunc.instructions.size} insns to a func " +
"with ${newFunc.instructions.size} insns + delegated func " +
"with ${splitFunc.instructions.size} insns and ${splitFunc.type.params.size} params, " +
"saved $insnsSaved insns"
}
mod = splitMod
}
// Write it
val outFile = when {
args.outFile != "<inFileSansExt.split.wasm or stdout>" -> args.outFile
args.inFile == "--" -> "--"
else -> args.inFile.replaceAfterLast('.', "split." + args.inFile.substringAfterLast('.'))
}
val outFormat = when {
args.outFormat != "<use file extension or wast for stdout>" -> args.outFormat
outFile == "--" -> "wast"
else -> outFile.substringAfterLast('.', "<unknown>")
}
translate.astToOut(outFile, outFormat, args.compact,
Script(listOf(Script.Cmd.Module(mod, mod.names?.moduleName))))
}
data class Args(
val inFile: String,
val inFormat: String,
val funcName: String,
val outFile: String,
val outFormat: String,
val compact: Boolean,
val minInsnSetLength: Int,
val maxInsnSetLength: Int,
val maxNewFuncParamCount: Int,
val attempts: Int
)
companion object : SplitFunc()
}

View File

@ -52,30 +52,16 @@ open class Translate : Command<Translate.Args>() {
if (args.outFormat != "<use file extension or wast for stdout>") args.outFormat
else if (args.outFile == "--") "wast"
else args.outFile.substringAfterLast('.', "<unknown>")
val outStream =
if (args.outFile == "--") System.out
else FileOutputStream(args.outFile)
outStream.use { outStream ->
when (outFormat) {
"wast" -> {
val sexprToStr = if (args.compact) SExprToStr.Compact else SExprToStr
val sexprs = AstToSExpr.fromScript(script)
outStream.write(sexprToStr.fromSExpr(*sexprs.toTypedArray()).toByteArray())
}
"wasm" -> {
val mod = (script.commands.firstOrNull() as? Script.Cmd.Module)?.module ?:
error("Output to WASM requires input be just a single module")
AstToBinary.fromModule(ByteWriter.OutputStream(outStream), mod)
}
else -> error("Unknown out format '$outFormat'")
}
}
astToOut(args.outFile, outFormat, args.compact, script)
}
fun inToAst(inFile: String, inFormat: String): Script {
val inBytes =
if (inFile == "--") System.`in`.use { it.readBytes() }
else File(inFile).let { f -> FileInputStream(f).use { it.readBytes(f.length().toIntExact()) } }
else File(inFile).let { f ->
// Input file might not fit into the memory
FileInputStream(f).use { it.readBytes(f.length().toIntExact()) }
}
return when (inFormat) {
"wast" -> StrToSExpr.parse(inBytes.toString(Charsets.UTF_8)).let { res ->
when (res) {
@ -84,12 +70,33 @@ open class Translate : Command<Translate.Args>() {
}
}
"wasm" ->
Script(listOf(Script.Cmd.Module(BinaryToAst.toModule(
Script(listOf(Script.Cmd.Module(BinaryToAst(logger = logger).toModule(
ByteReader.InputStream(inBytes.inputStream())), null)))
else -> error("Unknown in format '$inFormat'")
}
}
fun astToOut(outFile: String, outFormat: String, compact: Boolean, script: Script) {
val outStream =
if (outFile == "--") System.out
else FileOutputStream(outFile)
outStream.use { outStream ->
when (outFormat) {
"wast" -> {
val sexprToStr = if (compact) SExprToStr.Compact else SExprToStr
val sexprs = AstToSExpr.fromScript(script)
outStream.write(sexprToStr.fromSExpr(*sexprs.toTypedArray()).toByteArray())
}
"wasm" -> {
val mod = (script.commands.firstOrNull() as? Script.Cmd.Module)?.module ?:
error("Output to WASM requires input be just a single module")
AstToBinary.fromModule(ByteWriter.OutputStream(outStream), mod)
}
else -> error("Unknown out format '$outFormat'")
}
}
}
data class Args(
val inFile: String,
val inFormat: String,

View File

@ -210,3 +210,7 @@ fun ByteArray.asClassNode(): ClassNode {
ClassReader(this).accept(newNode, 0)
return newNode
}
fun ByteArray.chunked(v: Int) = (0 until size step v).asSequence().map {
copyOfRange(it, (it + v).takeIf { it < size } ?: size)
}

View File

@ -30,10 +30,11 @@ open class AstToAsm {
}
fun addFields(ctx: ClsContext) {
// Mem field if present
// Mem field if present, adds `private final field memory` to
if (ctx.hasMemory)
ctx.cls.fields.add(FieldNode(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "memory",
ctx.mem.memType.asmDesc, null, null))
ctx.cls.fields.add(
FieldNode((Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL), "memory", ctx.mem.memType.asmDesc, null, null)
)
// Table field if present...
// Private final for now, but likely won't be final in future versions supporting
// mutable tables, may be not even a table but a list (and final)

View File

@ -46,16 +46,25 @@ open class ByteBufferMem(val direct: Boolean = true) : Mem {
let(buildOffset).popExpecting(Int::class.ref).
addInsns(
forceFnType<ByteBuffer.(Int) -> Buffer>(ByteBuffer::position).invokeVirtual(),
TypeInsnNode(Opcodes.CHECKCAST, memType.asmName),
// We're going to do this as an LDC string in ISO-8859 and read it back at runtime
LdcInsnNode(bytes.toString(Charsets.ISO_8859_1)),
LdcInsnNode("ISO-8859-1"),
// Ug, can't do func refs on native types here...
MethodInsnNode(Opcodes.INVOKEVIRTUAL, String::class.ref.asmName,
"getBytes", "(Ljava/lang/String;)[B", false),
0.const,
bytes.size.const,
forceFnType<ByteBuffer.(ByteArray, Int, Int) -> ByteBuffer>(ByteBuffer::put).invokeVirtual(),
TypeInsnNode(Opcodes.CHECKCAST, memType.asmName)
).addInsns(
// We're going to do this as an LDC string in ISO-8859 and read it back at runtime. However,
// due to JVM limits, we can't have a string > 65536 chars. We chunk into 16300 because when
// converting to UTF8 const it can be up to 4 bytes per char, so this makes sure it doesn't
// overflow.
bytes.chunked(16300).flatMap { bytes ->
sequenceOf(
LdcInsnNode(bytes.toString(Charsets.ISO_8859_1)),
LdcInsnNode("ISO-8859-1"),
// Ug, can't do func refs on native types here...
MethodInsnNode(Opcodes.INVOKEVIRTUAL, String::class.ref.asmName,
"getBytes", "(Ljava/lang/String;)[B", false),
0.const,
bytes.size.const,
forceFnType<ByteBuffer.(ByteArray, Int, Int) -> ByteBuffer>(ByteBuffer::put).invokeVirtual()
)
}.toList()
).addInsns(
InsnNode(Opcodes.POP)
)

View File

@ -39,6 +39,24 @@ data class ClsContext(
val hasTable: Boolean by lazy {
mod.tables.isNotEmpty() || mod.imports.any { it.kind is Node.Import.Kind.Table }
}
val dedupedFuncNames: Map<Int, String>? by lazy {
// Consider all exports as seen
val seen = mod.exports.flatMap { export ->
when {
export.kind == Node.ExternalKind.FUNCTION -> listOf(export.field.javaIdent)
// Just to make it easy, consider all globals as having setters
export.kind == Node.ExternalKind.GLOBAL ->
export.field.javaIdent.capitalize().let { listOf("get$it", "set$it") }
else -> listOf("get" + export.field.javaIdent.capitalize())
}
}.toMutableSet()
mod.names?.funcNames?.toList()?.sortedBy { it.first }?.map { (index, origName) ->
var name = origName.javaIdent
var nameIndex = 0
while (!seen.add(name)) name = origName.javaIdent + (nameIndex++)
index to name
}?.toMap()
}
fun assertHasMemory() { if (!hasMemory) throw CompileErr.UnknownMemory(0) }
@ -71,7 +89,7 @@ data class ClsContext(
fun importGlobalGetterFieldName(index: Int) = "import\$get" + globalName(index)
fun importGlobalSetterFieldName(index: Int) = "import\$set" + globalName(index)
fun globalName(index: Int) = "\$global$index"
fun funcName(index: Int) = "\$func$index"
fun funcName(index: Int) = dedupedFuncNames?.get(index) ?: "\$func$index"
private fun syntheticFunc(
nameSuffix: String,

View File

@ -4,6 +4,24 @@ import asmble.ast.Node
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.*
/**
* Jvm representation of a function.
*
* @param name Name of the fn.
* @param params List of parameters of the fn.
* @param ret Type of the fn returner value.
* @param access The value of the access_flags item is a mask of flags used to
* denote access permissions to and properties of this class or
* interface [https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.1-200-E.1].
* @param insns List of nodes that represents a bytecode instruction.
* @param stack A stack of operand types. Mirror of the operand stack(jvm stack)
* where types of operands instead operands.
* @param blockStack List of blocks of code
* @param ifStack Contains index of [org.objectweb.asm.tree.JumpInsnNode] that
* has a null label initially
* @param lastStackIsMemLeftover If there is the memory on the stack and we need it
* in the future, we mark it as leftover and reuse
*/
data class Func(
val name: String,
val params: List<TypeRef> = emptyList(),
@ -12,7 +30,6 @@ data class Func(
val insns: List<AbstractInsnNode> = emptyList(),
val stack: List<TypeRef> = emptyList(),
val blockStack: List<Block> = emptyList(),
// Contains index of JumpInsnNode that has a null label initially
val ifStack: List<Int> = emptyList(),
val lastStackIsMemLeftover: Boolean = false
) {
@ -110,10 +127,11 @@ data class Func(
}
}
fun pushBlock(insn: Node.Instr, labelType: Node.Type.Value?, endType: Node.Type.Value?) =
/** Creates new block with specified instruction and pushes it into the blockStack.*/
fun pushBlock(insn: Node.Instr, labelType: Node.Type.Value?, endType: Node.Type.Value?): Func =
pushBlock(insn, listOfNotNull(labelType?.typeRef), listOfNotNull(endType?.typeRef))
fun pushBlock(insn: Node.Instr, labelTypes: List<TypeRef>, endTypes: List<TypeRef>) =
fun pushBlock(insn: Node.Instr, labelTypes: List<TypeRef>, endTypes: List<TypeRef>): Func =
copy(blockStack = blockStack + Block(insn, insns.size, stack, labelTypes, endTypes))
fun popBlock() = copy(blockStack = blockStack.dropLast(1)) to blockStack.last()
@ -127,6 +145,22 @@ data class Func(
fun popIf() = copy(ifStack = ifStack.dropLast(1)) to peekIf()
/**
* Representation of code block.
*
* Blocks are composed of matched pairs of `block ... end` instructions, loops
* with matched pairs of `loop ... end` instructions, and ifs with either
* `if ... end` or if ... else ... end sequences. For each of these constructs
* the instructions in the ellipsis are said to be enclosed in the construct.
*
* @param isns Start instruction of this block, might be a 'Block', 'Loop'
* or 'If'
* @param startIndex Index of start instruction of this block in list of all
* instructions
* @param origStack Current block stack of operand types.
* @param labelTypes A type of label for this block
* @param endTypes A type of block return value
*/
class Block(
val insn: Node.Instr,
val startIndex: Int,

View File

@ -14,23 +14,31 @@ import java.lang.invoke.MethodHandle
// TODO: modularize
open class FuncBuilder {
fun fromFunc(ctx: ClsContext, f: Node.Func, index: Int): Func {
/**
* Converts wasm AST [asmble.ast.Node.Func] to Jvm bytecode representation [asmble.compile.jvm.Func].
*
* @param ctx A Global context for converting.
* @param fn AST of wasm fn.
* @param index Fn index, used for generating fn name
*/
fun fromFunc(ctx: ClsContext, fn: Node.Func, index: Int): Func {
ctx.debug { "Building function ${ctx.funcName(index)}" }
ctx.trace { "Function ast:\n${SExprToStr.fromSExpr(AstToSExpr.fromFunc(f))}" }
ctx.trace { "Function ast:\n${SExprToStr.fromSExpr(AstToSExpr.fromFunc(fn))}" }
var func = Func(
access = Opcodes.ACC_PRIVATE,
name = ctx.funcName(index),
params = f.type.params.map(Node.Type.Value::typeRef),
ret = f.type.ret?.let(Node.Type.Value::typeRef) ?: Void::class.ref
params = fn.type.params.map(Node.Type.Value::typeRef),
ret = fn.type.ret?.let(Node.Type.Value::typeRef) ?: Void::class.ref
)
// Rework the instructions
val reworkedInsns = ctx.reworker.rework(ctx, f)
val reworkedInsns = ctx.reworker.rework(ctx, fn)
// Start the implicit block
func = func.pushBlock(Node.Instr.Block(f.type.ret), f.type.ret, f.type.ret)
func = func.pushBlock(Node.Instr.Block(fn.type.ret), fn.type.ret, fn.type.ret)
// Create the context
val funcCtx = FuncContext(
cls = ctx,
node = f,
node = fn,
insns = reworkedInsns,
memIsLocalVar =
ctx.reworker.nonAdjacentMemAccesses(reworkedInsns) >= ctx.nonAdjacentMemAccessesRequiringLocalVar
@ -46,9 +54,9 @@ open class FuncBuilder {
// Add all instructions
ctx.debug { "Applying insns for function ${ctx.funcName(index)}" }
// All functions have an implicit block
func = funcCtx.insns.foldIndexed(func) { index, func, insn ->
func = funcCtx.insns.foldIndexed(func) { idx, f, insn ->
ctx.debug { "Applying insn $insn" }
val ret = applyInsn(funcCtx, func, insn, index)
val ret = applyInsn(funcCtx, f, insn, idx)
ctx.trace { "Resulting stack: ${ret.stack}"}
ret
}
@ -56,11 +64,11 @@ open class FuncBuilder {
// End the implicit block
val implicitBlock = func.currentBlock
func = applyEnd(funcCtx, func)
f.type.ret?.typeRef?.also { func = func.popExpecting(it, implicitBlock) }
fn.type.ret?.typeRef?.also { func = func.popExpecting(it, implicitBlock) }
// If the last instruction does not terminate, add the expected return
if (func.insns.isEmpty() || !func.insns.last().isTerminating) {
func = func.addInsns(InsnNode(when (f.type.ret) {
func = func.addInsns(InsnNode(when (fn.type.ret) {
null -> Opcodes.RETURN
Node.Type.Value.I32 -> Opcodes.IRETURN
Node.Type.Value.I64 -> Opcodes.LRETURN
@ -72,8 +80,10 @@ open class FuncBuilder {
}
fun applyInsn(ctx: FuncContext, fn: Func, i: Insn, index: Int) = when (i) {
is Insn.Node ->
applyNodeInsn(ctx, fn, i.insn, index)
is Insn.ImportFuncRefNeededOnStack ->
// Func refs are method handle fields
fn.addInsns(
@ -81,6 +91,7 @@ open class FuncBuilder {
FieldInsnNode(Opcodes.GETFIELD, ctx.cls.thisRef.asmName,
ctx.cls.funcName(i.index), MethodHandle::class.ref.asmDesc)
).push(MethodHandle::class.ref)
is Insn.ImportGlobalSetRefNeededOnStack ->
// Import setters are method handle fields
fn.addInsns(
@ -88,13 +99,17 @@ open class FuncBuilder {
FieldInsnNode(Opcodes.GETFIELD, ctx.cls.thisRef.asmName,
ctx.cls.importGlobalSetterFieldName(i.index), MethodHandle::class.ref.asmDesc)
).push(MethodHandle::class.ref)
is Insn.ThisNeededOnStack ->
// load a reference onto the stack from a local variable
fn.addInsns(VarInsnNode(Opcodes.ALOAD, 0)).push(ctx.cls.thisRef)
is Insn.MemNeededOnStack ->
putMemoryOnStack(ctx, fn)
}
fun applyNodeInsn(ctx: FuncContext, fn: Func, i: Node.Instr, index: Int) = when (i) {
is Node.Instr.Unreachable ->
fn.addInsns(UnsupportedOperationException::class.athrow("Unreachable")).markUnreachable()
is Node.Instr.Nop ->
@ -127,18 +142,16 @@ open class FuncBuilder {
fn.pop().let { (fn, popped) ->
fn.addInsns(InsnNode(if (popped.stackSize == 2) Opcodes.POP2 else Opcodes.POP))
}
is Node.Instr.Select ->
applySelectInsn(ctx, fn)
is Node.Instr.GetLocal ->
applyGetLocal(ctx, fn, i.index)
is Node.Instr.SetLocal ->
applySetLocal(ctx, fn, i.index)
is Node.Instr.TeeLocal ->
applyTeeLocal(ctx, fn, i.index)
is Node.Instr.GetGlobal ->
applyGetGlobal(ctx, fn, i.index)
is Node.Instr.SetGlobal ->
applySetGlobal(ctx, fn, i.index)
is Node.Instr.Select -> applySelectInsn(ctx, fn)
// Variable access
is Node.Instr.GetLocal -> applyGetLocal(ctx, fn, i.index)
is Node.Instr.SetLocal -> applySetLocal(ctx, fn, i.index)
is Node.Instr.TeeLocal -> applyTeeLocal(ctx, fn, i.index)
is Node.Instr.GetGlobal -> applyGetGlobal(ctx, fn, i.index)
is Node.Instr.SetGlobal -> applySetGlobal(ctx, fn, i.index)
// Memory operators
is Node.Instr.I32Load, is Node.Instr.I64Load, is Node.Instr.F32Load, is Node.Instr.F64Load,
is Node.Instr.I32Load8S, is Node.Instr.I32Load8U, is Node.Instr.I32Load16U, is Node.Instr.I32Load16S,
is Node.Instr.I64Load8S, is Node.Instr.I64Load8U, is Node.Instr.I64Load16U, is Node.Instr.I64Load16S,
@ -461,18 +474,18 @@ open class FuncBuilder {
fun popUntilStackSize(
ctx: FuncContext,
fn: Func,
func: Func,
block: Func.Block,
untilStackSize: Int,
keepLast: Boolean
): Func {
ctx.debug { "For block ${block.insn}, popping until stack size $untilStackSize, keeping last? $keepLast" }
// Just get the latest, don't actually pop...
val type = if (keepLast) fn.pop().second else null
return (0 until Math.max(0, fn.stack.size - untilStackSize)).fold(fn) { fn, _ ->
val type = if (keepLast) func.pop().second else null
return (0 until Math.max(0, func.stack.size - untilStackSize)).fold(func) { fn, _ ->
// Essentially swap and pop if they want to keep the latest
(if (type != null && fn.stack.size > 1) fn.stackSwap(block) else fn).let { fn ->
fn.pop(block).let { (fn, poppedType) ->
(if (type != null && fn.stack.size > 1) fn.stackSwap(block) else fn).let { f ->
f.pop(block).let { (fn, poppedType) ->
fn.addInsns(InsnNode(if (poppedType.stackSize == 2) Opcodes.POP2 else Opcodes.POP))
}
}
@ -1076,10 +1089,12 @@ open class FuncBuilder {
putMemoryOnStack(ctx, fn).let { fn -> ctx.cls.mem.currentMemory(ctx, fn) }
}
/**
* Store is a special case where the memory ref is already pre-injected on
* the stack before this call. But it can have a memory leftover on the stack
* so we pop it if we need to
*/
fun applyStoreOp(ctx: FuncContext, fn: Func, insn: Node.Instr.Args.AlignOffset, insnIndex: Int) =
// Store is a special case where the memory ref is already pre-injected on
// the stack before this call. But it can have a memory leftover on the stack
// so we pop it if we need to
ctx.cls.assertHasMemory().let {
ctx.cls.mem.storeOp(ctx, fn, insn).let { fn ->
// As a special case, if this leaves the mem on the stack
@ -1268,11 +1283,11 @@ open class FuncBuilder {
}
}
fun applyReturnInsn(ctx: FuncContext, fn: Func): Func {
// If the current stakc is unreachable, we consider that our block since it
fun applyReturnInsn(ctx: FuncContext, func: Func): Func {
// If the current stack is unreachable, we consider that our block since it
// will pop properly.
val block = if (fn.currentBlock.unreachable) fn.currentBlock else fn.blockStack.first()
popForBlockEscape(ctx, fn, block).let { fn ->
val block = if (func.currentBlock.unreachable) func.currentBlock else func.blockStack.first()
popForBlockEscape(ctx, func, block).let { fn ->
return when (ctx.node.type.ret) {
null ->
fn.addInsns(InsnNode(Opcodes.RETURN))
@ -1284,9 +1299,9 @@ open class FuncBuilder {
fn.popExpecting(Float::class.ref, block).addInsns(InsnNode(Opcodes.FRETURN))
Node.Type.Value.F64 ->
fn.popExpecting(Double::class.ref, block).addInsns(InsnNode(Opcodes.DRETURN))
}.let { fn ->
if (fn.stack.isNotEmpty()) throw CompileErr.UnusedStackOnReturn(fn.stack)
fn.markUnreachable()
}.let { it ->
if (it.stack.isNotEmpty()) throw CompileErr.UnusedStackOnReturn(it.stack)
it.markUnreachable()
}
}
}

View File

@ -3,6 +3,15 @@ package asmble.compile.jvm
import asmble.ast.Node
import asmble.util.Logger
/**
* Jvm context of the function execution.
*
* @param cls class execution context
* @param node Ast of this function
* @param insns instructions list
* @param memIsLocalVar true if function uses only local variables and doesn't load
* or store to/from memory.
*/
data class FuncContext(
val cls: ClsContext,
val node: Node.Func,

View File

@ -2,6 +2,9 @@ package asmble.compile.jvm
import asmble.ast.Node
/**
* Does some special manipulations with instruction.
*/
open class InsnReworker {
fun rework(ctx: ClsContext, func: Node.Func): List<Insn> {
@ -29,6 +32,7 @@ open class InsnReworker {
// Note, while walking backwards up the insns to find set/tee, we do skip entire
// blocks/loops/if+else combined with "end"
var neededEagerLocalIndices = emptySet<Int>()
fun addEagerSetIfNeeded(getInsnIndex: Int, localIndex: Int) {
// Within the param range? nothing needed
if (localIndex < func.type.params.size) return
@ -71,6 +75,7 @@ open class InsnReworker {
(insn is Insn.Node && insn.insn !is Node.Instr.SetLocal && insn.insn !is Node.Instr.TeeLocal)
if (needsEagerInit) neededEagerLocalIndices += localIndex
}
insns.forEachIndexed { index, insn ->
if (insn is Insn.Node && insn.insn is Node.Instr.GetLocal) addEagerSetIfNeeded(index, insn.insn.index)
}
@ -86,11 +91,18 @@ open class InsnReworker {
} + insns
}
/**
* Puts into instruction list needed instructions for pushing local variables
* into the stack and returns list of resulting instructions.
*
* @param ctx The Execution context
* @param insns The original instructions
*/
fun injectNeededStackVars(ctx: ClsContext, insns: List<Node.Instr>): List<Insn> {
ctx.trace { "Calculating places to inject needed stack variables" }
// How we do this:
// We run over each insn, and keep a running list of stack
// manips. If there is an insn that needs something so far back,
// manips. If there is an ins'n that needs something so far back,
// we calc where it needs to be added and keep a running list of
// insn inserts. Then at the end we settle up.
//
@ -109,6 +121,14 @@ open class InsnReworker {
// guarantee the value will be in the right order if there are
// multiple for the same index
var insnsToInject = emptyMap<Int, List<Insn>>()
/**
* This function inject current instruction in stack.
*
* @param insn The instruction to inject
* @param count Number of step back on the stack that should we do for
* finding injection index.
*/
fun injectBeforeLastStackCount(insn: Insn, count: Int) {
ctx.trace { "Injecting $insn back $count stack values" }
fun inject(index: Int) {
@ -148,9 +168,11 @@ open class InsnReworker {
}
countSoFar += amountChanged
if (!foundUnconditionalJump) foundUnconditionalJump = insns[insnIndex].let { insn ->
insn is Node.Instr.Br || insn is Node.Instr.BrTable ||
insn is Node.Instr.Unreachable || insn is Node.Instr.Return
if (!foundUnconditionalJump) {
foundUnconditionalJump = insns[insnIndex].let { insn ->
insn is Node.Instr.Br || insn is Node.Instr.BrTable ||
insn is Node.Instr.Unreachable || insn is Node.Instr.Return
}
}
if (countSoFar == count) {
ctx.trace { "Found injection point as before insn #$insnIndex" }
@ -161,6 +183,7 @@ open class InsnReworker {
if (!foundUnconditionalJump) throw CompileErr.StackInjectionMismatch(count, insn)
}
var traceStackSize = 0 // Used only for trace
// Go over each insn, determining where to inject
insns.forEachIndexed { index, insn ->
// Handle special injection cases
@ -199,19 +222,32 @@ open class InsnReworker {
else -> { }
}
// Log some trace output
ctx.trace {
insnStackDiff(ctx, insn).let {
traceStackSize += it
"Stack diff is $it for insn #$index $insn, stack size now: $traceStackSize"
}
}
// Add the current diff
ctx.trace { "Stack diff is ${insnStackDiff(ctx, insn)} for insn #$index $insn" }
stackManips += insnStackDiff(ctx, insn) to index
}
// Build resulting list
return insns.foldIndexed(emptyList<Insn>()) { index, ret, insn ->
return insns.foldIndexed(emptyList()) { index, ret, insn ->
val injections = insnsToInject[index] ?: emptyList()
ret + injections + Insn.Node(insn)
}
}
fun insnStackDiff(ctx: ClsContext, insn: Node.Instr) = when (insn) {
/**
* Calculate stack difference after calling instruction current instruction.
* Returns the difference from stack cursor position before instruction and after.
* `N = PUSH_OPS - POP_OPS.` '-n' mean that POP operation will be more than PUSH.
* If '0' then stack won't changed.
*/
fun insnStackDiff(ctx: ClsContext, insn: Node.Instr): Int = when (insn) {
is Node.Instr.Unreachable, is Node.Instr.Nop, is Node.Instr.Block,
is Node.Instr.Loop, is Node.Instr.If, is Node.Instr.Else,
is Node.Instr.End, is Node.Instr.Br, is Node.Instr.BrIf,
@ -238,7 +274,7 @@ open class InsnReworker {
is Node.Instr.I64Load32S, is Node.Instr.I64Load32U -> POP_PARAM + PUSH_RESULT
is Node.Instr.I32Store, is Node.Instr.I64Store, is Node.Instr.F32Store, is Node.Instr.F64Store,
is Node.Instr.I32Store8, is Node.Instr.I32Store16, is Node.Instr.I64Store8, is Node.Instr.I64Store16,
is Node.Instr.I64Store32 -> POP_PARAM
is Node.Instr.I64Store32 -> POP_PARAM + POP_PARAM
is Node.Instr.MemorySize -> PUSH_RESULT
is Node.Instr.MemoryGrow -> POP_PARAM + PUSH_RESULT
is Node.Instr.I32Const, is Node.Instr.I64Const,
@ -284,7 +320,8 @@ open class InsnReworker {
is Node.Instr.F64ReinterpretI64 -> POP_PARAM + PUSH_RESULT
}
fun nonAdjacentMemAccesses(insns: List<Insn>) = insns.fold(0 to false) { (count, lastCouldHaveMem), insn ->
/** Returns number of memory accesses. */
fun nonAdjacentMemAccesses(insns: List<Insn>): Int = insns.fold(0 to false) { (count, lastCouldHaveMem), insn ->
val inc =
if (lastCouldHaveMem) 0
else if (insn == Insn.MemNeededOnStack) 1

View File

@ -2,14 +2,25 @@ package asmble.compile.jvm
import org.objectweb.asm.Type
/**
* A Java field or method type. This class can be used to make it easier to
* manipulate type and method descriptors.
*
* @param asm Wrapped [org.objectweb.asm.Type] from asm library
*/
data class TypeRef(val asm: Type) {
/** The internal name of the class corresponding to this object or array type. */
val asmName: String get() = asm.internalName
/** The descriptor corresponding to this Java type. */
val asmDesc: String get() = asm.descriptor
fun asMethodRetDesc(vararg args: TypeRef) = Type.getMethodDescriptor(asm, *args.map { it.asm }.toTypedArray())
/** Size of this type in stack, either 1 or 2 only allowed, where 1 = 2^32` bits */
val stackSize: Int get() = if (asm == Type.DOUBLE_TYPE || asm == Type.LONG_TYPE) 2 else 1
fun asMethodRetDesc(vararg args: TypeRef) = Type.getMethodDescriptor(asm, *args.map { it.asm }.toTypedArray())
fun equivalentTo(other: TypeRef) = this == other || this == Unknown || other == Unknown
object UnknownType

View File

@ -5,6 +5,7 @@ import asmble.util.toRawIntBits
import asmble.util.toRawLongBits
import asmble.util.toUnsignedBigInt
import asmble.util.toUnsignedLong
import java.io.ByteArrayOutputStream
open class AstToBinary(val version: Long = 1L) {
@ -140,6 +141,9 @@ open class AstToBinary(val version: Long = 1L) {
fromResizableLimits(b, n.limits)
}
fun fromModule(n: Node.Module) =
ByteArrayOutputStream().also { fromModule(ByteWriter.OutputStream(it), n) }.toByteArray()
fun fromModule(b: ByteWriter, n: Node.Module) {
b.writeUInt32(0x6d736100)
b.writeUInt32(version)
@ -160,10 +164,33 @@ open class AstToBinary(val version: Long = 1L) {
wrapListSection(b, n, 9, n.elems, this::fromElem)
wrapListSection(b, n, 10, n.funcs, this::fromFuncBody)
wrapListSection(b, n, 11, n.data, this::fromData)
n.names?.also { fromNames(b, it) }
// All other custom sections after the previous
n.customSections.filter { it.afterSectionId > 11 }.forEach { fromCustomSection(b, it) }
}
fun fromNames(b: ByteWriter, n: Node.NameSection) {
fun <T> indexMap(b: ByteWriter, map: Map<Int, T>, fn: (T) -> Unit) {
b.writeVarUInt32(map.size)
map.forEach { index, v -> b.writeVarUInt32(index).also { fn(v) } }
}
fun nameMap(b: ByteWriter, map: Map<Int, String>) = indexMap(b, map) { b.writeString(it) }
b.writeVarUInt7(0)
b.withVarUInt32PayloadSizePrepended { b ->
b.writeString("name")
n.moduleName?.also { moduleName ->
b.writeVarUInt7(0)
b.withVarUInt32PayloadSizePrepended { b -> b.writeString(moduleName) }
}
if (n.funcNames.isNotEmpty()) b.writeVarUInt7(1).also {
b.withVarUInt32PayloadSizePrepended { b -> nameMap(b, n.funcNames) }
}
if (n.localNames.isNotEmpty()) b.writeVarUInt7(2).also {
b.withVarUInt32PayloadSizePrepended { b -> indexMap(b, n.localNames) { nameMap(b, it) } }
}
}
}
fun fromResizableLimits(b: ByteWriter, n: Node.ResizableLimits) {
b.writeVarUInt1(n.maximum != null)
b.writeVarUInt32(n.initial)

View File

@ -62,13 +62,21 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
Node.ExternalKind.GLOBAL -> newMulti("global") + v.index
}
fun fromFunc(v: Node.Func, name: String? = null, impExp: ImportOrExport? = null) =
newMulti("func", name) + impExp?.let(this::fromImportOrExport) + fromFuncSig(v.type) +
fromLocals(v.locals) + fromInstrs(v.instructions).unwrapInstrs()
fun fromFunc(
v: Node.Func,
name: String? = null,
impExp: ImportOrExport? = null,
localNames: Map<Int, String> = emptyMap()
) =
newMulti("func", name) + impExp?.let(this::fromImportOrExport) + fromFuncSig(v.type, localNames) +
fromLocals(v.locals, v.type.params.size, localNames) + fromInstrs(v.instructions).unwrapInstrs()
fun fromFuncSig(v: Node.Type.Func): List<SExpr> {
fun fromFuncSig(v: Node.Type.Func, localNames: Map<Int, String> = emptyMap()): List<SExpr> {
var ret = emptyList<SExpr>()
if (v.params.isNotEmpty()) ret += newMulti("param") + v.params.map(this::fromType)
if (v.params.isNotEmpty()) {
if (localNames.isEmpty()) ret += newMulti("param") + v.params.map(this::fromType)
else ret += v.params.mapIndexed { index, param -> newMulti("param", localNames[index]) + fromType(param) }
}
v.ret?.also { ret += newMulti("result") + fromType(it) }
return ret
}
@ -80,8 +88,8 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
fun fromGlobalSig(v: Node.Type.Global) =
if (v.mutable) newMulti("mut") + fromType(v.contentType) else fromType(v.contentType)
fun fromImport(v: Node.Import, types: List<Node.Type.Func>) =
(newMulti("import") + v.module.quoted) + v.field.quoted + fromImportKind(v.kind, types)
fun fromImport(v: Node.Import, types: List<Node.Type.Func>, name: String? = null) =
(newMulti("import") + v.module.quoted) + v.field.quoted + fromImportKind(v.kind, types, name)
fun fromImportFunc(v: Node.Import.Kind.Func, types: List<Node.Type.Func>, name: String? = null) =
fromImportFunc(types.getOrElse(v.typeIndex) { throw Exception("No type at ${v.typeIndex}") }, name)
@ -91,11 +99,11 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
fun fromImportGlobal(v: Node.Import.Kind.Global, name: String? = null) =
newMulti("global", name) + fromGlobalSig(v.type)
fun fromImportKind(v: Node.Import.Kind, types: List<Node.Type.Func>) = when(v) {
is Node.Import.Kind.Func -> fromImportFunc(v, types)
is Node.Import.Kind.Table -> fromImportTable(v)
is Node.Import.Kind.Memory -> fromImportMemory(v)
is Node.Import.Kind.Global -> fromImportGlobal(v)
fun fromImportKind(v: Node.Import.Kind, types: List<Node.Type.Func>, name: String? = null) = when(v) {
is Node.Import.Kind.Func -> fromImportFunc(v, types, name)
is Node.Import.Kind.Table -> fromImportTable(v, name)
is Node.Import.Kind.Memory -> fromImportMemory(v, name)
is Node.Import.Kind.Global -> fromImportGlobal(v, name)
}
fun fromImportMemory(v: Node.Import.Kind.Memory, name: String? = null) =
@ -161,8 +169,10 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
return listOf(SExpr.Multi(untilNext().first))
}
fun fromLocals(v: List<Node.Type.Value>) =
if (v.isEmpty()) null else newMulti("local") + v.map(this::fromType)
fun fromLocals(v: List<Node.Type.Value>, paramOffset: Int, localNames: Map<Int, String> = emptyMap()) =
if (v.isEmpty()) emptyList()
else if (localNames.isEmpty()) listOf(newMulti("local") + v.map(this::fromType))
else v.mapIndexed { index, v -> newMulti("local", localNames[paramOffset + index]) + fromType(v) }
fun fromMemory(v: Node.Type.Memory, name: String? = null, impExp: ImportOrExport? = null) =
newMulti("memory", name) + impExp?.let(this::fromImportOrExport) + fromMemorySig(v)
@ -175,7 +185,7 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
is Script.Cmd.Meta.Output -> newMulti("output", v.name) + v.str
}
fun fromModule(v: Node.Module, name: String? = null): SExpr.Multi {
fun fromModule(v: Node.Module, name: String? = v.names?.moduleName): SExpr.Multi {
var ret = newMulti("module", name)
// If there is a call_indirect, then we need to output all types in exact order.
@ -187,8 +197,14 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
v.types.filterIndexed { i, _ -> importIndices.contains(i) } - v.funcs.map { it.type }
}
// Keep track of the current function index for names
var funcIndex = -1
ret += types.map { fromTypeDef(it) }
ret += v.imports.map { fromImport(it, v.types) }
ret += v.imports.map {
if (it.kind is Node.Import.Kind.Func) funcIndex++
fromImport(it, v.types, v.names?.funcNames?.get(funcIndex))
}
ret += v.exports.map(this::fromExport)
ret += v.tables.map { fromTable(it) }
ret += v.memories.map { fromMemory(it) }
@ -196,7 +212,14 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
ret += v.elems.map(this::fromElem)
ret += v.data.map(this::fromData)
ret += v.startFuncIndex?.let(this::fromStart)
ret += v.funcs.map { fromFunc(it) }
ret += v.funcs.map {
funcIndex++
fromFunc(
v = it,
name = v.names?.funcNames?.get(funcIndex),
localNames = v.names?.localNames?.get(funcIndex) ?: emptyMap()
)
}
return ret
}
@ -235,10 +258,8 @@ open class AstToSExpr(val parensInstrs: Boolean = true) {
if (exp == null) this else this.copy(vals = this.vals + exp)
private operator fun SExpr.Multi.plus(exps: List<SExpr>?) =
if (exps == null || exps.isEmpty()) this else this.copy(vals = this.vals + exps)
private fun newMulti(initSymb: String? = null, initName: String? = null): SExpr.Multi {
initName?.also { require(it.startsWith("$")) }
return SExpr.Multi() + initSymb + initName
}
private fun newMulti(initSymb: String? = null, initName: String? = null) =
SExpr.Multi() + initSymb + initName?.let { "$$it" }
private fun List<SExpr.Multi>.unwrapInstrs() =
if (parensInstrs) this else this.single().vals
private val String.quoted get() = fromString(this, true)

View File

@ -2,11 +2,13 @@ package asmble.io
import asmble.ast.Node
import asmble.util.*
import java.io.ByteArrayInputStream
import java.nio.ByteBuffer
open class BinaryToAst(
val version: Long = 1L,
val logger: Logger = Logger.Print(Logger.Level.OFF)
val logger: Logger = Logger.Print(Logger.Level.OFF),
val includeNameSection: Boolean = true
) : Logger by logger {
fun toBlockType(b: ByteReader) = b.readVarInt7().toInt().let {
@ -19,6 +21,23 @@ open class BinaryToAst(
payload = b.readBytes()
)
fun toNameSection(b: ByteReader) = generateSequence {
if (b.isEof) null
else b.readVarUInt7().toInt() to b.read(b.readVarUInt32AsInt())
}.fold(Node.NameSection(null, emptyMap(), emptyMap())) { sect, (type, b) ->
fun <T> indexMap(b: ByteReader, fn: (ByteReader) -> T) =
b.readList { it.readVarUInt32AsInt() to fn(it) }.let { pairs ->
pairs.toMap().also { require(it.size == pairs.size) { "Malformed names: duplicate indices" } }
}
fun nameMap(b: ByteReader) = indexMap(b) { it.readString() }
when (type) {
0 -> sect.copy(moduleName = b.readString())
1 -> sect.copy(funcNames = nameMap(b))
2 -> sect.copy(localNames = indexMap(b, ::nameMap))
else -> error("Malformed names: unrecognized type: $type")
}.also { require(b.isEof) }
}
fun toData(b: ByteReader) = Node.Data(
index = b.readVarUInt32AsInt(),
offset = toInitExpr(b),
@ -151,6 +170,8 @@ open class BinaryToAst(
fun toMemoryType(b: ByteReader) = Node.Type.Memory(toResizableLimits(b))
fun toModule(b: ByteArray) = toModule(ByteReader.InputStream(b.inputStream()))
fun toModule(b: ByteReader): Node.Module {
if (b.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber()
b.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) }
@ -165,14 +186,17 @@ open class BinaryToAst(
require(sectionId > maxSectionId) { "Section ID $sectionId came after $maxSectionId" }.
also { maxSectionId = sectionId }
val sectionLen = b.readVarUInt32AsInt()
// each 'read' invocation creates new InputStream and don't closes it
sections += sectionId to b.read(sectionLen)
}
// Now build the module
fun <T> readSectionList(sectionId: Int, fn: (ByteReader) -> T) =
sections.find { it.first == sectionId }?.second?.readList(fn) ?: emptyList()
val types = readSectionList(1, this::toFuncType)
val funcIndices = readSectionList(3) { it.readVarUInt32AsInt() }
var nameSection: Node.NameSection? = null
return Node.Module(
types = types,
imports = readSectionList(2, this::toImport),
@ -193,10 +217,18 @@ open class BinaryToAst(
val afterSectionId = if (index == 0) 0 else sections[index - 1].let { (prevSectionId, _) ->
if (prevSectionId == 0) customSections.last().afterSectionId else prevSectionId
}
customSections + toCustomSection(b, afterSectionId)
// Try to parse the name section
val section = toCustomSection(b, afterSectionId).takeIf { section ->
val shouldParseNames = includeNameSection && nameSection == null && section.name == "name"
!shouldParseNames || try {
nameSection = toNameSection(ByteReader.InputStream(section.payload.inputStream()))
false
} catch (e: Exception) { warn { "Failed parsing name section: $e" }; true }
}
if (section == null) customSections else customSections + section
}
}
)
).copy(names = nameSection)
}
fun toResizableLimits(b: ByteReader) = b.readVarUInt1().let {

View File

@ -100,6 +100,7 @@ abstract class ByteReader {
return result
}
// todo looks like this InputStream isn't ever closed
class InputStream(val ins: java.io.InputStream) : ByteReader() {
private var nextByte: Byte? = null
private var sawEof = false

View File

@ -9,22 +9,27 @@ import asmble.util.*
import java.io.ByteArrayInputStream
import java.math.BigInteger
typealias NameMap = Map<String, Int>
open class SExprToAst {
open class SExprToAst(
val includeNames: Boolean = true
) {
data class ExprContext(
val nameMap: NameMap,
val blockDepth: Int = 0,
val types: List<Node.Type.Func> = emptyList(),
val callIndirectNeverBeforeSeenFuncTypes: MutableList<Node.Type.Func> = mutableListOf()
)
) {
companion object {
val empty = ExprContext(NameMap(emptyMap(), null, null))
}
}
data class FuncResult(
val name: String?,
val func: Node.Func,
val importOrExport: ImportOrExport?,
// These come from call_indirect insns
val additionalFuncTypesToAdd: List<Node.Type.Func>
val additionalFuncTypesToAdd: List<Node.Type.Func>,
val nameMap: NameMap
)
fun toAction(exp: SExpr.Multi): Script.Cmd.Action {
@ -36,7 +41,7 @@ open class SExprToAst {
return when(exp.vals.first().symbolStr()) {
"invoke" ->
Script.Cmd.Action.Invoke(name, str, exp.vals.drop(index).map {
toExprMaybe(it as SExpr.Multi, ExprContext(emptyMap()))
toExprMaybe(it as SExpr.Multi, ExprContext.empty)
})
"get" ->
Script.Cmd.Action.Get(name, str)
@ -49,7 +54,7 @@ open class SExprToAst {
return when(exp.vals.first().symbolStr()) {
"assert_return" ->
Script.Cmd.Assertion.Return(toAction(mult),
exp.vals.drop(2).map { toExprMaybe(it as SExpr.Multi, ExprContext(emptyMap())) })
exp.vals.drop(2).map { toExprMaybe(it as SExpr.Multi, ExprContext.empty) })
"assert_return_canonical_nan" ->
Script.Cmd.Assertion.ReturnNan(toAction(mult), canonical = true)
"assert_return_arithmetic_nan" ->
@ -176,7 +181,7 @@ open class SExprToAst {
var innerCtx = ctx.copy(blockDepth = ctx.blockDepth + 1)
exp.maybeName(opOffset)?.also {
opOffset++
innerCtx = innerCtx.copy(nameMap = innerCtx.nameMap + ("block:$it" to innerCtx.blockDepth))
innerCtx = innerCtx.copy(nameMap = innerCtx.nameMap.add("block", it, innerCtx.blockDepth))
}
val sigs = toBlockSigMaybe(exp, opOffset)
@ -233,7 +238,7 @@ open class SExprToAst {
var (nameMap, exprsUsed, sig) = toFuncSig(exp, currentIndex, origNameMap, types)
currentIndex += exprsUsed
val locals = exp.repeated("local", currentIndex, { toLocals(it) }).mapIndexed { index, (nameMaybe, vals) ->
nameMaybe?.also { require(vals.size == 1); nameMap += "local:$it" to (index + sig.params.size) }
nameMaybe?.also { require(vals.size == 1); nameMap = nameMap.add("local", it, index + sig.params.size) }
vals
}
currentIndex += locals.size
@ -250,7 +255,8 @@ open class SExprToAst {
name = name,
func = Node.Func(sig, locals.flatten(), instrs),
importOrExport = maybeImpExp,
additionalFuncTypesToAdd = ctx.callIndirectNeverBeforeSeenFuncTypes
additionalFuncTypesToAdd = ctx.callIndirectNeverBeforeSeenFuncTypes,
nameMap = nameMap
)
}
@ -268,7 +274,7 @@ open class SExprToAst {
} else null to offset
var nameMap = origNameMap
val params = exp.repeated("param", offset, { toParams(it) }).mapIndexed { index, (nameMaybe, vals) ->
nameMaybe?.also { require(vals.size == 1); nameMap += "local:$it" to index }
nameMaybe?.also { require(vals.size == 1); nameMap = nameMap.add("local", it, index) }
vals
}
val resultExps = exp.repeated("result", offset + params.size, this::toResult)
@ -395,7 +401,7 @@ open class SExprToAst {
val maybeName = exp.maybeName(offset + opOffset)
if (maybeName != null) {
opOffset++
innerCtx = innerCtx.copy(nameMap = innerCtx.nameMap + ("block:$maybeName" to innerCtx.blockDepth))
innerCtx = innerCtx.copy(nameMap = innerCtx.nameMap.add("block", maybeName, innerCtx.blockDepth))
}
val sigs = toBlockSigMaybe(exp, offset + opOffset)
opOffset += sigs.size
@ -428,7 +434,7 @@ open class SExprToAst {
opOffset++
exp.maybeName(offset + opOffset)?.also {
opOffset++
innerCtx = innerCtx.copy(nameMap = innerCtx.nameMap + ("block:$it" to ctx.blockDepth))
innerCtx = innerCtx.copy(nameMap = innerCtx.nameMap.add("block", it, ctx.blockDepth))
}
toInstrs(exp, offset + opOffset, innerCtx, false).also {
ret += it.first
@ -522,7 +528,7 @@ open class SExprToAst {
val exps = exp.vals.mapNotNull { it as? SExpr.Multi }
// Eagerly build the names (for forward decls)
val (nameMap, eagerTypes) = toModuleForwardNameMapAndTypes(exps)
var (nameMap, eagerTypes) = toModuleForwardNameMapAndTypes(exps)
mod = mod.copy(types = eagerTypes)
fun Node.Module.addTypeIfNotPresent(type: Node.Type.Func): Pair<Node.Module, Int> {
@ -555,6 +561,7 @@ open class SExprToAst {
Node.Import.Kind.Memory(kind) to (memoryCount++ to Node.ExternalKind.MEMORY)
else -> throw Exception("Unrecognized import kind: $kind")
}
mod = mod.copy(
imports = mod.imports + Node.Import(module, field, importKind),
exports = mod.exports + exportFields.map {
@ -579,11 +586,14 @@ open class SExprToAst {
"elem" -> mod = mod.copy(elems = mod.elems + toElem(exp, nameMap))
"data" -> mod = mod.copy(data = mod.data + toData(exp, nameMap))
"start" -> mod = mod.copy(startFuncIndex = toStart(exp, nameMap))
"func" -> toFunc(exp, nameMap, mod.types).also { (_, fn, impExp, additionalFuncTypes) ->
"func" -> toFunc(exp, nameMap, mod.types).also { (_, fn, impExp, additionalFuncTypes, localNameMap) ->
if (impExp is ImportOrExport.Import) {
handleImport(impExp.module, impExp.name, fn.type, impExp.exportFields)
} else {
if (impExp is ImportOrExport.Export) addExport(impExp, Node.ExternalKind.FUNCTION, funcCount)
if (includeNames) nameMap = nameMap.copy(
localNames = nameMap.localNames!! + (funcCount to localNameMap.getAllNamesByIndex("local"))
)
funcCount++
mod = mod.copy(funcs = mod.funcs + fn).addTypeIfNotPresent(fn.type).first
mod = additionalFuncTypes.fold(mod) { mod, typ -> mod.addTypeIfNotPresent(typ).first }
@ -644,6 +654,15 @@ open class SExprToAst {
if (mod.tables.size + mod.imports.count { it.kind is Node.Import.Kind.Table } > 1)
throw IoErr.MultipleTables()
// Set the name map pieces if we're including them
if (includeNames) mod = mod.copy(
names = Node.NameSection(
moduleName = name,
funcNames = nameMap.funcNames!!,
localNames = nameMap.localNames!!
)
)
return name to mod
}
@ -680,10 +699,14 @@ open class SExprToAst {
var globalCount = 0
var tableCount = 0
var memoryCount = 0
var namesToIndices = emptyMap<String, Int>()
var nameMap = NameMap(
names = emptyMap(),
funcNames = if (includeNames) emptyMap() else null,
localNames = if (includeNames) emptyMap() else null
)
var types = emptyList<Node.Type.Func>()
fun maybeAddName(name: String?, index: Int, type: String) {
name?.let { namesToIndices += "$type:$it" to index }
name?.also { nameMap = nameMap.add(type, it, index) }
}
// All imports first
@ -711,12 +734,12 @@ open class SExprToAst {
"memory" -> maybeAddName(kindName, memoryCount++, "memory")
// We go ahead and do the full type def build here eagerly
"type" -> maybeAddName(kindName, types.size, "type").also { _ ->
toTypeDef(it, namesToIndices).also { (_, type) -> types += type }
toTypeDef(it, nameMap).also { (_, type) -> types += type }
}
else -> {}
}
}
return namesToIndices to types
return nameMap to types
}
fun toOpMaybe(exp: SExpr.Multi, offset: Int, ctx: ExprContext): Pair<Node.Instr, Int>? {
@ -753,7 +776,8 @@ open class SExprToAst {
// First lookup the func sig
val (updatedNameMap, expsUsed, funcType) = toFuncSig(exp, offset + 1, ctx.nameMap, ctx.types)
// Make sure there are no changes to the name map
if (ctx.nameMap.size != updatedNameMap.size) throw IoErr.IndirectCallSetParamNames()
if (ctx.nameMap.size != updatedNameMap.size)
throw IoErr.IndirectCallSetParamNames()
// Obtain the func index from the types table, the indirects table, or just add it
var funcTypeIndex = ctx.types.indexOf(funcType)
// If it's not in the type list, check the call indirect list
@ -910,7 +934,7 @@ open class SExprToAst {
fun toVarMaybe(exp: SExpr, nameMap: NameMap, nameType: String): Int? {
return exp.symbolStr()?.let { it ->
if (it.startsWith("$"))
nameMap["$nameType:$it"] ?:
nameMap.get(nameType, it.drop(1)) ?:
throw Exception("Unable to find index for name $it of type $nameType in $nameMap")
else if (it.startsWith("0x")) it.substring(2).toIntOrNull(16)
else it.toIntOrNull()
@ -1005,7 +1029,7 @@ open class SExprToAst {
private fun SExpr.Multi.maybeName(index: Int): String? {
if (this.vals.size > index && this.vals[index] is SExpr.Symbol) {
val sym = this.vals[index] as SExpr.Symbol
if (!sym.quoted && sym.contents[0] == '$') return sym.contents
if (!sym.quoted && sym.contents[0] == '$') return sym.contents.drop(1)
}
return null
}
@ -1028,5 +1052,26 @@ open class SExprToAst {
return this.vals.first().requireSymbol(contents, quotedCheck)
}
data class NameMap(
// Key prefixed with type then colon before actual name
val names: Map<String, Int>,
// Null if not including names
val funcNames: Map<Int, String>?,
val localNames: Map<Int, Map<Int, String>>?
) {
val size get() = names.size
fun add(type: String, name: String, index: Int) = copy(
names = names + ("$type:$name" to index),
funcNames = funcNames?.let { if (type == "func") it + (index to name) else it }
)
fun get(type: String, name: String) = names["$type:$name"]
fun getAllNamesByIndex(type: String) = names.mapNotNull { (k, v) ->
k.takeIf { k.startsWith("$type:") }?.let { v to k.substring(type.length + 1) }
}.toMap()
}
companion object : SExprToAst()
}

View File

@ -18,6 +18,13 @@ open class StrToSExpr {
data class Error(val pos: Pos, val msg: String) : ParseResult()
}
fun parseSingleMulti(str: CharSequence) = parse(str).let {
when (it) {
is ParseResult.Success -> (it.vals.singleOrNull() as? SExpr.Multi) ?: error("Not a single multi-expr")
is ParseResult.Error -> error("Failed parsing at ${it.pos.line}:${it.pos.char} - ${it.msg}")
}
}
fun parse(str: CharSequence): ParseResult {
val state = ParseState(str)
val ret = mutableListOf<SExpr>()

View File

@ -19,6 +19,20 @@ import java.lang.invoke.MethodType
import java.lang.reflect.InvocationTargetException
import java.util.*
/**
* Script context. Contains all the information needed to execute this script.
*
* @param packageName Package name for this script
* @param modules List of all modules to be load for this script
* @param registrations Registered modules, key - module name, value - module instance
* @param logger Logger for this script
* @param adjustContext Fn for tuning context (looks redundant)
* @param classLoader ClassLoader for loading all classes for this script
* @param exceptionTranslator Converts exceptions to error messages
* @param defaultMaxMemPages The maximum number of memory pages when a module doesn't say
* @param includeBinaryInCompiledClass Store binary wasm code to compiled class
* file as annotation [asmble.annotation.WasmModule]
*/
data class ScriptContext(
val packageName: String,
val modules: List<Module.Compiled> = emptyList(),

View File

@ -0,0 +1,33 @@
package asmble.ast
import asmble.SpecTestUnit
import asmble.TestBase
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@RunWith(Parameterized::class)
class StackTest(val unit: SpecTestUnit) : TestBase() {
@Test
fun testStack() {
// If it's not a module expecting an error, we'll try to walk the stack on each function
unit.script.commands.mapNotNull { it as? Script.Cmd.Module }.forEach { mod ->
mod.module.funcs.filter { it.instructions.isNotEmpty() }.forEach { func ->
debug { "Func: ${func.type}" }
var indexCount = 0
Stack.walkStrict(mod.module, func) { stack, insn ->
debug { " After $insn (next: ${func.instructions.getOrNull(++indexCount)}, " +
"unreach depth: ${stack.unreachableUntilNextEndCount})" }
debug { " " + stack.current }
}
}
}
}
companion object {
// Only tests that shouldn't fail
@JvmStatic @Parameterized.Parameters(name = "{0}")
fun data() = SpecTestUnit.allUnits.filterNot { it.shouldFail }//.filter { it.name == "loop" }
}
}

View File

@ -0,0 +1,89 @@
package asmble.ast.opt
import asmble.TestBase
import asmble.ast.Node
import asmble.compile.jvm.AstToAsm
import asmble.compile.jvm.ClsContext
import asmble.run.jvm.ScriptContext
import org.junit.Test
import java.nio.ByteBuffer
import java.util.*
import kotlin.test.assertEquals
class SplitLargeFuncTest : TestBase() {
@Test
fun testSplitLargeFunc() {
// We're going to make a large function that does some addition and then stores in mem
val ctx = ClsContext(
packageName = "test",
className = "Temp" + UUID.randomUUID().toString().replace("-", ""),
logger = logger,
mod = Node.Module(
memories = listOf(Node.Type.Memory(Node.ResizableLimits(initial = 2, maximum = 2))),
funcs = listOf(Node.Func(
type = Node.Type.Func(params = emptyList(), ret = null),
locals = emptyList(),
instructions = (0 until 501).flatMap {
listOf<Node.Instr>(
Node.Instr.I32Const(it * 4),
// Let's to i * (i = 1)
Node.Instr.I32Const(it),
Node.Instr.I32Const(it - 1),
Node.Instr.I32Mul,
Node.Instr.I32Store(0, 0)
)
}
)),
names = Node.NameSection(
moduleName = null,
funcNames = mapOf(0 to "someFunc"),
localNames = emptyMap()
),
exports = listOf(
Node.Export("memory", Node.ExternalKind.MEMORY, 0),
Node.Export("someFunc", Node.ExternalKind.FUNCTION, 0)
)
)
)
// Compile it
AstToAsm.fromModule(ctx)
val cls = ScriptContext.SimpleClassLoader(javaClass.classLoader, logger).fromBuiltContext(ctx)
val inst = cls.newInstance()
// Run someFunc
cls.getMethod("someFunc").invoke(inst)
// Get the memory out
val mem = cls.getMethod("getMemory").invoke(inst) as ByteBuffer
// Read out the mem values
(0 until 501).forEach { assertEquals(it * (it - 1), mem.getInt(it * 4)) }
// Now split it
val (splitMod, insnsSaved) = SplitLargeFunc.apply(ctx.mod, 0) ?: error("Nothing could be split")
// Count insns and confirm it is as expected
val origInsnCount = ctx.mod.funcs.sumBy { it.instructions.size }
val newInsnCount = splitMod.funcs.sumBy { it.instructions.size }
assertEquals(origInsnCount - newInsnCount, insnsSaved)
// Compile it
val splitCtx = ClsContext(
packageName = "test",
className = "Temp" + UUID.randomUUID().toString().replace("-", ""),
logger = logger,
mod = splitMod
)
AstToAsm.fromModule(splitCtx)
val splitCls = ScriptContext.SimpleClassLoader(javaClass.classLoader, logger).fromBuiltContext(splitCtx)
val splitInst = splitCls.newInstance()
// Run someFunc
splitCls.getMethod("someFunc").invoke(splitInst)
// Get the memory out and compare it
val splitMem = splitCls.getMethod("getMemory").invoke(splitInst) as ByteBuffer
assertEquals(mem, splitMem)
// Dump some info
logger.debug {
val orig = ctx.mod.funcs.first()
val (new, split) = splitMod.funcs.let { it.first() to it.last() }
"Split complete, from single func with ${orig.instructions.size} insns to func " +
"with ${new.instructions.size} insns + delegated func " +
"with ${split.instructions.size} insns and ${split.type.params.size} params"
}
}
}

View File

@ -0,0 +1,45 @@
package asmble.compile.jvm
import asmble.TestBase
import asmble.ast.Node
import asmble.run.jvm.ScriptContext
import asmble.util.get
import org.junit.Test
import java.nio.ByteBuffer
import java.util.*
import kotlin.test.assertEquals
class LargeDataTest : TestBase() {
@Test
fun testLargeData() {
// This previously failed because string constants can't be longer than 65536 chars.
// We create a byte array across the whole gambit of bytes to test UTF8 encoding.
val bytesExpected = ByteArray(70000) { ((it % 255) - Byte.MIN_VALUE).toByte() }
val mod = Node.Module(
memories = listOf(Node.Type.Memory(
limits = Node.ResizableLimits(initial = 2, maximum = 2)
)),
data = listOf(Node.Data(
index = 0,
offset = listOf(Node.Instr.I32Const(0)),
data = bytesExpected
))
)
val ctx = ClsContext(
packageName = "test",
className = "Temp" + UUID.randomUUID().toString().replace("-", ""),
mod = mod,
logger = logger
)
AstToAsm.fromModule(ctx)
val cls = ScriptContext.SimpleClassLoader(javaClass.classLoader, logger).fromBuiltContext(ctx)
// Instantiate it, get the memory out, and check it
val field = cls.getDeclaredField("memory").apply { isAccessible = true }
val buf = field[cls.newInstance()] as ByteBuffer
// Grab all + 1 and check values
val bytesActual = ByteArray(70001).also { buf.get(0, it) }
bytesActual.forEachIndexed { index, byte ->
assertEquals(if (index == 70000) 0.toByte() else bytesExpected[index], byte)
}
}
}

View File

@ -0,0 +1,38 @@
package asmble.compile.jvm
import asmble.TestBase
import asmble.io.SExprToAst
import asmble.io.StrToSExpr
import asmble.run.jvm.ScriptContext
import org.junit.Test
import java.util.*
class NamesTest : TestBase() {
@Test
fun testNames() {
// Compile and make sure the names are set right
val (_, mod) = SExprToAst.toModule(StrToSExpr.parseSingleMulti("""
(module ${'$'}mod_name
(import "foo" "bar" (func ${'$'}import_func (param i32)))
(type ${'$'}some_sig (func (param ${'$'}type_param i32)))
(func ${'$'}some_func
(type ${'$'}some_sig)
(param ${'$'}func_param i32)
(local ${'$'}func_local0 i32)
(local ${'$'}func_local1 f64)
)
)
""".trimIndent()))
val ctx = ClsContext(
packageName = "test",
className = "Temp" + UUID.randomUUID().toString().replace("-", ""),
mod = mod,
logger = logger
)
AstToAsm.fromModule(ctx)
val cls = ScriptContext.SimpleClassLoader(javaClass.classLoader, logger).fromBuiltContext(ctx)
// Make sure the import field and the func are present named
cls.getDeclaredField("import_func")
cls.getDeclaredMethod("some_func", Integer.TYPE)
}
}

View File

@ -1,6 +1,7 @@
package asmble.io
import asmble.SpecTestUnit
import asmble.TestBase
import asmble.ast.Node
import asmble.ast.Script
import asmble.util.Logger
@ -13,12 +14,10 @@ import java.io.ByteArrayOutputStream
import kotlin.test.assertEquals
@RunWith(Parameterized::class)
class IoTest(val unit: SpecTestUnit) : Logger by Logger.Print(Logger.Level.INFO) {
class IoTest(val unit: SpecTestUnit) : TestBase() {
@Test
fun testIo() {
// Ignore things that are supposed to fail
if (unit.shouldFail) return
// Go from the AST to binary then back to AST then back to binary and confirm values are as expected
val ast1 = unit.script.commands.mapNotNull { (it as? Script.Cmd.Module)?.module?.also {
trace { "AST from script:\n" + SExprToStr.fromSExpr(AstToSExpr.fromModule(it)) }
@ -46,7 +45,8 @@ class IoTest(val unit: SpecTestUnit) : Logger by Logger.Print(Logger.Level.INFO)
}
companion object {
// Only tests that shouldn't fail
@JvmStatic @Parameterized.Parameters(name = "{0}")
fun data() = SpecTestUnit.allUnits
fun data() = SpecTestUnit.allUnits.filterNot { it.shouldFail }
}
}

View File

@ -0,0 +1,47 @@
package asmble.io
import asmble.ast.Node
import org.junit.Test
import kotlin.test.assertEquals
class NamesTest {
@Test
fun testNames() {
// First, make sure it can parse from sexpr
val (_, mod1) = SExprToAst.toModule(StrToSExpr.parseSingleMulti("""
(module ${'$'}mod_name
(import "foo" "bar" (func ${'$'}import_func (param i32)))
(type ${'$'}some_sig (func (param ${'$'}type_param i32)))
(func ${'$'}some_func
(type ${'$'}some_sig)
(param ${'$'}func_param i32)
(local ${'$'}func_local0 i32)
(local ${'$'}func_local1 f64)
)
)
""".trimIndent()))
val expected = Node.NameSection(
moduleName = "mod_name",
funcNames = mapOf(
0 to "import_func",
1 to "some_func"
),
localNames = mapOf(
1 to mapOf(
0 to "func_param",
1 to "func_local0",
2 to "func_local1"
)
)
)
assertEquals(expected, mod1.names)
// Now back to binary and then back and make sure it's still there
val bytes = AstToBinary.fromModule(mod1)
val mod2 = BinaryToAst.toModule(bytes)
assertEquals(expected, mod2.names)
// Now back to sexpr and then back to make sure the sexpr writer works
val sexpr = AstToSExpr.fromModule(mod2)
val (_, mod3) = SExprToAst.toModule(sexpr)
assertEquals(expected, mod3.names)
}
}

View File

@ -9,9 +9,3 @@ Compile Rust to WASM and then to the JVM. In order of complexity:
* [rust-simple](rust-simple)
* [rust-string](rust-string)
* [rust-regex](rust-regex)
### C/C++
Compile C to WASM and then to the JVM. In order of complexity:
* [c-simple](c-simple)

View File

@ -1,14 +0,0 @@
### Example: C Simple
This shows a simple example of compiling C to WASM and then to the JVM. This is the C version of
[rust-simple](../rust-simple).
In order to run the C or C++ examples, the latest LLVM binaries must be on the `PATH`, built with the experimental
WebAssembly target. This can be built by passing `-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly` to `cmake` when
building WebAssembly. Or it can be downloaded from a nightly build site
([this one](http://gsdview.appspot.com/wasm-llvm/builds/) was used for these examples at the time of writing).
Everything else is basically the same as [rust-simple](../rust-simple) except with C code and using `clang`. To run
execute the following from the root `asmble` dir:
gradlew --no-daemon :examples:c-simple:run

View File

@ -1,3 +0,0 @@
int addOne(int x) {
return x + 1;
}

View File

@ -1,13 +0,0 @@
package asmble.examples.csimple;
import java.lang.invoke.MethodHandle;
import asmble.generated.CSimple;
class Main {
public static void main(String[] args) {
// Doesn't need memory or method table
CSimple simple = new CSimple(0, new MethodHandle[0]);
System.out.println("25 + 1 = " + simple.addOne(25));
}
}

View File

@ -0,0 +1,10 @@
package main
import (
"fmt"
"os"
)
func main() {
fmt.Printf("Args: %v", os.Args)
}

View File

@ -2,8 +2,9 @@
extern crate regex;
use std::ptr::NonNull;
use regex::Regex;
use std::heap::{Alloc, Heap, Layout};
use std::alloc::{Alloc, Global, Layout};
use std::mem;
use std::str;
@ -37,17 +38,17 @@ pub extern "C" fn match_count(r: *mut Regex, str_ptr: *mut u8, len: usize) -> us
}
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *mut u8 {
pub extern "C" fn alloc(size: usize) -> NonNull<u8> {
unsafe {
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
Heap.alloc(layout).unwrap()
Global.alloc(layout).unwrap()
}
}
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut u8, size: usize) {
pub extern "C" fn dealloc(ptr: NonNull<u8>, size: usize) {
unsafe {
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
Heap.dealloc(ptr, layout);
Global.dealloc(ptr, layout);
}
}

View File

@ -3,6 +3,9 @@ package asmble.examples.rustregex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Implementation of {@link RegexLib} based on `java.util.regex`.
*/
public class JavaLib implements RegexLib<String> {
@Override
public JavaPattern compile(String str) {

View File

@ -5,6 +5,10 @@ import asmble.generated.RustRegex;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
/**
* Implementation of {@link RegexLib} based on `asmble.generated.RustRegex` that
* was composed from Rust code (see lib.rs).
*/
public class RustLib implements RegexLib<RustLib.Ptr> {
// 600 pages is enough for our use

View File

@ -1,6 +1,7 @@
#![feature(allocator_api)]
use std::heap::{Alloc, Heap, Layout};
use std::ptr::NonNull;
use std::alloc::{Alloc, Global, Layout};
use std::ffi::{CString};
use std::mem;
use std::os::raw::c_char;
@ -30,17 +31,17 @@ pub extern "C" fn prepend_from_rust(ptr: *mut u8, len: usize) -> *const c_char {
}
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *mut u8 {
pub extern "C" fn alloc(size: usize) -> NonNull<u8> {
unsafe {
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
Heap.alloc(layout).unwrap()
Global.alloc(layout).unwrap()
}
}
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut u8, size: usize) {
pub extern "C" fn dealloc(ptr: NonNull<u8>, size: usize) {
unsafe {
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
Heap.dealloc(ptr, layout);
Global.dealloc(ptr, layout);
}
}

View File

@ -2,6 +2,7 @@ rootProject.name = 'asmble'
include 'annotations',
'compiler',
'examples:c-simple',
'examples:go-simple',
'examples:rust-regex',
'examples:rust-simple',
'examples:rust-string'