mirror of
https://github.com/fluencelabs/asmble
synced 2025-07-04 00:41:34 +00:00
Compare commits
17 Commits
original-m
...
documentat
Author | SHA1 | Date | |
---|---|---|---|
3b1c314c35 | |||
f1a2d14463 | |||
ab269fdbd8 | |||
da70c9fca4 | |||
a1a5563367 | |||
e489e7c889 | |||
c1391b2701 | |||
6b28c5a93b | |||
1990f46743 | |||
559df45f09 | |||
80a8a1fbb9 | |||
dd72c7124c | |||
c04a3c4a9b | |||
51520ac07d | |||
97660de6ba | |||
cee7a86773 | |||
cfa4a35af1 |
209
build.gradle
209
build.gradle
@ -2,8 +2,8 @@ group 'asmble'
|
||||
version '0.2.0'
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.2.61'
|
||||
ext.asm_version = '6.2.1'
|
||||
ext.kotlin_version = '1.2.51'
|
||||
ext.asm_version = '5.2'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@ -14,13 +14,14 @@ 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-SNAPSHOT'
|
||||
version '0.4.0-fl'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@ -34,7 +35,7 @@ project(':annotations') {
|
||||
options.addStringOption 'Xdoclint:all', '-Xdoclint:-missing'
|
||||
}
|
||||
|
||||
publishSettings(project, 'asmble-annotations', 'Asmble WASM Annotations', true)
|
||||
publishSettings(project, 'asmble-annotations', 'Asmble WASM Annotations')
|
||||
}
|
||||
|
||||
project(':compiler') {
|
||||
@ -53,12 +54,12 @@ project(':compiler') {
|
||||
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
|
||||
compile "org.ow2.asm:asm-tree:$asm_version"
|
||||
compile "org.ow2.asm:asm-util:$asm_version"
|
||||
compile "org.ow2.asm:asm-commons:$asm_version"
|
||||
testCompile 'junit:junit:4.12'
|
||||
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', false)
|
||||
publishSettings(project, 'asmble-compiler', 'Asmble WASM Compiler')
|
||||
}
|
||||
|
||||
project(':examples') {
|
||||
@ -67,38 +68,6 @@ project(':examples') {
|
||||
compileOnly project(':compiler')
|
||||
}
|
||||
|
||||
// C/C++ example helpers
|
||||
|
||||
task cToWasm {
|
||||
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'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
classpath configurations.compileClasspath
|
||||
main = 'asmble.cli.MainKt'
|
||||
doFirst {
|
||||
def outFile = 'build/wasm-classes/' + wasmCompiledClassName.replace('.', '/') + '.class'
|
||||
file(outFile).parentFile.mkdirs()
|
||||
args 'compile', 'build/lib.wasm', wasmCompiledClassName, '-out', outFile
|
||||
}
|
||||
}
|
||||
|
||||
// Go example helpers
|
||||
|
||||
task goToWasm {
|
||||
@ -173,17 +142,6 @@ project(':examples') {
|
||||
}
|
||||
}
|
||||
|
||||
project(':examples:c-simple') {
|
||||
apply plugin: 'application'
|
||||
ext.wasmCompiledClassName = 'asmble.generated.CSimple'
|
||||
dependencies {
|
||||
compile files('build/wasm-classes')
|
||||
}
|
||||
compileJava {
|
||||
dependsOn compileCWasm
|
||||
}
|
||||
mainClassName = 'asmble.examples.csimple.Main'
|
||||
}
|
||||
|
||||
project(':examples:go-simple') {
|
||||
apply plugin: 'application'
|
||||
@ -197,28 +155,30 @@ project(':examples:go-simple') {
|
||||
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'
|
||||
@ -244,75 +204,60 @@ project(':examples:rust-string') {
|
||||
mainClassName = 'asmble.examples.ruststring.Main'
|
||||
}
|
||||
|
||||
def publishSettings(project, projectName, projectDescription, includeJavadoc) {
|
||||
def publishSettings(project, projectName, projectDescription) {
|
||||
|
||||
project.with {
|
||||
if (!project.hasProperty('ossrhUsername')) return
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'maven'
|
||||
apply plugin: 'signing'
|
||||
|
||||
archivesBaseName = projectName
|
||||
|
||||
task packageSources(type: Jar) {
|
||||
task sourcesJar(type: Jar) {
|
||||
from sourceSets.main.allJava
|
||||
classifier = 'sources'
|
||||
from sourceSets.main.allSource
|
||||
}
|
||||
|
||||
if (includeJavadoc) {
|
||||
task packageJavadoc(type: Jar, dependsOn: 'javadoc') {
|
||||
from javadoc.destinationDir
|
||||
classifier = 'javadoc'
|
||||
}
|
||||
} else {
|
||||
task packageJavadoc(type: Jar) {
|
||||
// Empty to satisfy Sonatype's javadoc.jar requirement
|
||||
classifier 'javadoc'
|
||||
}
|
||||
}
|
||||
|
||||
artifacts {
|
||||
archives packageSources, packageJavadoc
|
||||
}
|
||||
|
||||
signing {
|
||||
sign configurations.archives
|
||||
}
|
||||
|
||||
uploadArchives {
|
||||
repositories {
|
||||
mavenDeployer {
|
||||
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
|
||||
repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
|
||||
authentication(userName: ossrhUsername, password: ossrhPassword)
|
||||
}
|
||||
snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots/') {
|
||||
authentication(userName: ossrhUsername, password: ossrhPassword)
|
||||
}
|
||||
pom.project {
|
||||
name projectName
|
||||
packaging 'jar'
|
||||
description projectDescription
|
||||
url 'https://github.com/cretz/asmble'
|
||||
scm {
|
||||
connection 'scm:git:git@github.com:cretz/asmble.git'
|
||||
developerConnection 'scm:git:git@github.com:cretz/asmble.git'
|
||||
url 'git@github.com:cretz/asmble.git'
|
||||
}
|
||||
licenses {
|
||||
license {
|
||||
name 'MIT License'
|
||||
url 'https://opensource.org/licenses/MIT'
|
||||
}
|
||||
}
|
||||
developers {
|
||||
developer {
|
||||
id 'cretz'
|
||||
name 'Chad Retz'
|
||||
url 'https://github.com/cretz'
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
Taken from https://github.com/cretz/msplit
|
@ -1,286 +0,0 @@
|
||||
package asmble.compile.jvm.msplit;
|
||||
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static asmble.compile.jvm.msplit.Util.*;
|
||||
|
||||
/** Splits a method into two */
|
||||
public class SplitMethod {
|
||||
|
||||
protected final int api;
|
||||
|
||||
/** @param api Same as for {@link org.objectweb.asm.MethodVisitor#MethodVisitor(int)} or any other ASM class */
|
||||
public SplitMethod(int api) { this.api = api; }
|
||||
|
||||
/**
|
||||
* Calls {@link #split(String, MethodNode, int, int, int)} with minSize as 20% + 1 of the original, maxSize as
|
||||
* 70% + 1 of the original, and firstAtLeast as maxSize. The original method is never modified and the result can
|
||||
* be null if no split points are found.
|
||||
*/
|
||||
public Result split(String owner, MethodNode method) {
|
||||
// Between 20% + 1 and 70% + 1 of size
|
||||
int insnCount = method.instructions.size();
|
||||
int minSize = (int) (insnCount * 0.2) + 1;
|
||||
int maxSize = (int) (insnCount * 0.7) + 1;
|
||||
return split(owner, method, minSize, maxSize, maxSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the given method into two. This uses a {@link Splitter} to consistently create
|
||||
* {@link asmble.compile.jvm.msplit.Splitter.SplitPoint}s until one reaches firstAtLeast or the largest otherwise, and then calls
|
||||
* {@link #fromSplitPoint(String, MethodNode, Splitter.SplitPoint)}.
|
||||
*
|
||||
* @param owner The internal name of the owning class. Needed when splitting to call the split off method.
|
||||
* @param method The method to split, never modified
|
||||
* @param minSize The minimum number of instructions the split off method must have
|
||||
* @param maxSize The maximum number of instructions the split off method can have
|
||||
* @param firstAtLeast The number of instructions that, when first reached, will immediately be used without
|
||||
* continuing. Since split points are streamed, this allows splitting without waiting to
|
||||
* find the largest overall. If this is <= 0, it will not apply and all split points will be
|
||||
* checked to find the largest before doing the split.
|
||||
* @return The resulting split method or null if there were no split points found
|
||||
*/
|
||||
public Result split(String owner, MethodNode method, int minSize, int maxSize, int firstAtLeast) {
|
||||
// Get the largest split point
|
||||
Splitter.SplitPoint largest = null;
|
||||
for (Splitter.SplitPoint point : new Splitter(api, owner, method, minSize, maxSize)) {
|
||||
if (largest == null || point.length > largest.length) {
|
||||
largest = point;
|
||||
// Early exit?
|
||||
if (firstAtLeast > 0 && largest.length >= firstAtLeast) break;
|
||||
}
|
||||
}
|
||||
if (largest == null) return null;
|
||||
return fromSplitPoint(owner, method, largest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the given method at the given split point. Called by {@link #split(String, MethodNode, int, int, int)}. The
|
||||
* original method is never modified.
|
||||
*/
|
||||
public Result fromSplitPoint(String owner, MethodNode orig, Splitter.SplitPoint splitPoint) {
|
||||
MethodNode splitOff = createSplitOffMethod(orig, splitPoint);
|
||||
MethodNode trimmed = createTrimmedMethod(owner, orig, splitOff, splitPoint);
|
||||
return new Result(trimmed, splitOff);
|
||||
}
|
||||
|
||||
protected MethodNode createSplitOffMethod(MethodNode orig, Splitter.SplitPoint splitPoint) {
|
||||
// The new method is a static synthetic method named method.name + "$split" that returns an object array
|
||||
// Key is previous local index, value is new local index
|
||||
Map<Integer, Integer> localsMap = new HashMap<>();
|
||||
// The new method's parameters are all stack items + all read locals
|
||||
List<Type> args = new ArrayList<>(splitPoint.neededFromStackAtStart);
|
||||
splitPoint.localsRead.forEach((index, type) -> {
|
||||
args.add(type);
|
||||
localsMap.put(index, args.size() - 1);
|
||||
});
|
||||
// Create the new method
|
||||
MethodNode newMethod = new MethodNode(api,
|
||||
Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_SYNTHETIC, orig.name + "$split",
|
||||
Type.getMethodDescriptor(Type.getType(Object[].class), args.toArray(new Type[0])), null, null);
|
||||
// Add the written locals to the map that are not already there
|
||||
int newLocalIndex = args.size();
|
||||
for (Integer key : splitPoint.localsWritten.keySet()) {
|
||||
if (!localsMap.containsKey(key)) {
|
||||
localsMap.put(key, newLocalIndex);
|
||||
newLocalIndex++;
|
||||
}
|
||||
}
|
||||
// First set of instructions is pushing the new stack from the params
|
||||
for (int i = 0; i < splitPoint.neededFromStackAtStart.size(); i++) {
|
||||
Type item = splitPoint.neededFromStackAtStart.get(i);
|
||||
newMethod.visitVarInsn(loadOpFromType(item), i);
|
||||
}
|
||||
// Next set of instructions comes verbatim from the original, but we have to change the local indexes
|
||||
Set<Label> seenLabels = new HashSet<>();
|
||||
for (int i = 0; i < splitPoint.length; i++) {
|
||||
AbstractInsnNode insn = orig.instructions.get(i + splitPoint.start);
|
||||
// Skip frames
|
||||
if (insn instanceof FrameNode) continue;
|
||||
// Store the label
|
||||
if (insn instanceof LabelNode) seenLabels.add(((LabelNode) insn).getLabel());
|
||||
// Change the local if needed
|
||||
if (insn instanceof VarInsnNode) {
|
||||
insn = insn.clone(Collections.emptyMap());
|
||||
((VarInsnNode) insn).var = localsMap.get(((VarInsnNode) insn).var);
|
||||
} else if (insn instanceof IincInsnNode) {
|
||||
insn = insn.clone(Collections.emptyMap());
|
||||
((VarInsnNode) insn).var = localsMap.get(((VarInsnNode) insn).var);
|
||||
}
|
||||
insn.accept(newMethod);
|
||||
}
|
||||
// Final set of instructions is an object array of stack to set and then locals written
|
||||
// Create the object array
|
||||
int retArrSize = splitPoint.putOnStackAtEnd.size() + splitPoint.localsWritten.size();
|
||||
intConst(retArrSize).accept(newMethod);
|
||||
newMethod.visitTypeInsn(Opcodes.ANEWARRAY, OBJECT_TYPE.getInternalName());
|
||||
// So, we're going to store the arr in the next avail local
|
||||
int retArrLocalIndex = newLocalIndex;
|
||||
newMethod.visitVarInsn(Opcodes.ASTORE, retArrLocalIndex);
|
||||
// Now go over each stack item and load the arr, swap w/ the stack, add the index, swap with the stack, and store
|
||||
for (int i = splitPoint.putOnStackAtEnd.size() - 1; i >= 0; i--) {
|
||||
Type item = splitPoint.putOnStackAtEnd.get(i);
|
||||
// Box the item on the stack if necessary
|
||||
boxStackIfNecessary(item, newMethod);
|
||||
// Load the array
|
||||
newMethod.visitVarInsn(Opcodes.ALOAD, retArrLocalIndex);
|
||||
// Swap to put stack back on top
|
||||
newMethod.visitInsn(Opcodes.SWAP);
|
||||
// Add the index
|
||||
intConst(i).accept(newMethod);
|
||||
// Swap to put the stack value back on top
|
||||
newMethod.visitInsn(Opcodes.SWAP);
|
||||
// Now that we have arr, index, value, we can store in the array
|
||||
newMethod.visitInsn(Opcodes.AASTORE);
|
||||
}
|
||||
// Do the same with written locals
|
||||
int currIndex = splitPoint.putOnStackAtEnd.size();
|
||||
for (Integer index : splitPoint.localsWritten.keySet()) {
|
||||
Type item = splitPoint.localsWritten.get(index);
|
||||
// Load the array
|
||||
newMethod.visitVarInsn(Opcodes.ALOAD, retArrLocalIndex);
|
||||
// Add the arr index
|
||||
intConst(currIndex).accept(newMethod);
|
||||
currIndex++;
|
||||
// Load the var
|
||||
newMethod.visitVarInsn(loadOpFromType(item), localsMap.get(index));
|
||||
// Box it if necessary
|
||||
boxStackIfNecessary(item, newMethod);
|
||||
// Store in array
|
||||
newMethod.visitInsn(Opcodes.AASTORE);
|
||||
}
|
||||
// Load the array out and return it
|
||||
newMethod.visitVarInsn(Opcodes.ALOAD, retArrLocalIndex);
|
||||
newMethod.visitInsn(Opcodes.ARETURN);
|
||||
// Any try catch blocks that start in here
|
||||
for (TryCatchBlockNode tryCatch : orig.tryCatchBlocks) {
|
||||
if (seenLabels.contains(tryCatch.start.getLabel())) tryCatch.accept(newMethod);
|
||||
}
|
||||
// Reset the labels
|
||||
newMethod.instructions.resetLabels();
|
||||
return newMethod;
|
||||
}
|
||||
|
||||
protected MethodNode createTrimmedMethod(String owner, MethodNode orig,
|
||||
MethodNode splitOff, Splitter.SplitPoint splitPoint) {
|
||||
// The trimmed method is the same as the original, yet the split area is replaced with a call to the split off
|
||||
// portion. Before calling the split-off, we have to add locals to the stack part. Then afterwards, we have to
|
||||
// replace the stack and written locals.
|
||||
// Effectively clone the orig
|
||||
MethodNode newMethod = new MethodNode(api, orig.access, orig.name, orig.desc,
|
||||
orig.signature, orig.exceptions.toArray(new String[0]));
|
||||
orig.accept(newMethod);
|
||||
// Remove all insns, we'll re-add the ones outside the split range
|
||||
newMethod.instructions.clear();
|
||||
// Remove all try catch blocks and keep track of seen labels, we'll re-add them at the end
|
||||
newMethod.tryCatchBlocks.clear();
|
||||
Set<Label> seenLabels = new HashSet<>();
|
||||
// Also keep track of the locals that have been stored, need to know
|
||||
Set<Integer> seenStoredLocals = new HashSet<>();
|
||||
// If this is an instance method, we consider "0" (i.e. "this") as seen
|
||||
if ((orig.access & Opcodes.ACC_STATIC) == 0) seenStoredLocals.add(0);
|
||||
// Add the insns before split
|
||||
for (int i = 0; i < splitPoint.start; i++) {
|
||||
AbstractInsnNode insn = orig.instructions.get(i + splitPoint.start);
|
||||
// Skip frames
|
||||
if (insn instanceof FrameNode) continue;
|
||||
// Record label
|
||||
if (insn instanceof LabelNode) seenLabels.add(((LabelNode) insn).getLabel());
|
||||
// Check a local store has happened
|
||||
if (insn instanceof VarInsnNode && isStoreOp(insn.getOpcode())) seenStoredLocals.add(((VarInsnNode) insn).var);
|
||||
insn.accept(newMethod);
|
||||
}
|
||||
// Push all the read locals on the stack
|
||||
splitPoint.localsRead.forEach((index, type) -> {
|
||||
// We've seen a store for this, so just load it, otherwise use a zero val
|
||||
// TODO: safe? if not, maybe just put at the top of the method a bunch of defaulted locals?
|
||||
if (seenStoredLocals.contains(index)) newMethod.visitVarInsn(loadOpFromType(type), index);
|
||||
else zeroVal(type).accept(newMethod);
|
||||
});
|
||||
// Invoke the split off method
|
||||
newMethod.visitMethodInsn(Opcodes.INVOKESTATIC, owner, splitOff.name, splitOff.desc, false);
|
||||
// Now the object array is on the stack which contains stack pieces + written locals
|
||||
// Take off the locals
|
||||
int localArrIndex = splitPoint.putOnStackAtEnd.size();
|
||||
for (Integer index : splitPoint.localsWritten.keySet()) {
|
||||
// Dupe the array
|
||||
newMethod.visitInsn(Opcodes.DUP);
|
||||
// Put the index on the stack
|
||||
intConst(localArrIndex).accept(newMethod);
|
||||
localArrIndex++;
|
||||
// Load the written local
|
||||
Type item = splitPoint.localsWritten.get(index);
|
||||
newMethod.visitInsn(Opcodes.AALOAD);
|
||||
// Cast to local type
|
||||
if (!item.equals(OBJECT_TYPE)) {
|
||||
newMethod.visitTypeInsn(Opcodes.CHECKCAST, boxedTypeIfNecessary(item).getInternalName());
|
||||
}
|
||||
// Unbox if necessary
|
||||
unboxStackIfNecessary(item, newMethod);
|
||||
// Store in the local
|
||||
newMethod.visitVarInsn(storeOpFromType(item), index);
|
||||
}
|
||||
// Now just load up the stack
|
||||
for (int i = 0; i < splitPoint.putOnStackAtEnd.size(); i++) {
|
||||
boolean last = i == splitPoint.putOnStackAtEnd.size() - 1;
|
||||
// Since the loop started with the array, we only dupe the array every time but the last
|
||||
if (!last) newMethod.visitInsn(Opcodes.DUP);
|
||||
// Put the index on the stack
|
||||
intConst(i).accept(newMethod);
|
||||
// Load the stack item
|
||||
Type item = splitPoint.putOnStackAtEnd.get(i);
|
||||
newMethod.visitInsn(Opcodes.AALOAD);
|
||||
// Cast to local type
|
||||
if (!item.equals(OBJECT_TYPE)) {
|
||||
newMethod.visitTypeInsn(Opcodes.CHECKCAST, boxedTypeIfNecessary(item).getInternalName());
|
||||
}
|
||||
// Unbox if necessary
|
||||
unboxStackIfNecessary(item, newMethod);
|
||||
// For all but the last stack item, we need to swap with the arr ref above.
|
||||
if (!last) {
|
||||
// Note if the stack item takes two slots, we do a form of dup then pop since there's no swap1x2
|
||||
if (item == Type.LONG_TYPE || item == Type.DOUBLE_TYPE) {
|
||||
newMethod.visitInsn(Opcodes.DUP_X2);
|
||||
newMethod.visitInsn(Opcodes.POP);
|
||||
} else {
|
||||
newMethod.visitInsn(Opcodes.SWAP);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now we have restored all locals and all stack...add the rest of the insns after the split
|
||||
for (int i = splitPoint.start + splitPoint.length; i < orig.instructions.size(); i++) {
|
||||
AbstractInsnNode insn = orig.instructions.get(i + splitPoint.start);
|
||||
// Skip frames
|
||||
if (insn instanceof FrameNode) continue;
|
||||
// Record label
|
||||
if (insn instanceof LabelNode) seenLabels.add(((LabelNode) insn).getLabel());
|
||||
insn.accept(newMethod);
|
||||
}
|
||||
// Add any try catch blocks that started in here
|
||||
for (TryCatchBlockNode tryCatch : orig.tryCatchBlocks) {
|
||||
if (seenLabels.contains(tryCatch.start.getLabel())) tryCatch.accept(newMethod);
|
||||
}
|
||||
// Reset the labels
|
||||
newMethod.instructions.resetLabels();
|
||||
return newMethod;
|
||||
}
|
||||
|
||||
/** Result of a split method */
|
||||
public static class Result {
|
||||
/** A copy of the original method, but changed to invoke {@link #splitOffMethod} */
|
||||
public final MethodNode trimmedMethod;
|
||||
/** The new method that was split off the original and is called by {@link #splitOffMethod} */
|
||||
public final MethodNode splitOffMethod;
|
||||
|
||||
public Result(MethodNode trimmedMethod, MethodNode splitOffMethod) {
|
||||
this.trimmedMethod = trimmedMethod;
|
||||
this.splitOffMethod = splitOffMethod;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,392 +0,0 @@
|
||||
package asmble.compile.jvm.msplit;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.commons.AnalyzerAdapter;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static asmble.compile.jvm.msplit.Util.*;
|
||||
|
||||
/** For a given method, iterate over possible split points */
|
||||
public class Splitter implements Iterable<Splitter.SplitPoint> {
|
||||
protected final int api;
|
||||
protected final String owner;
|
||||
protected final MethodNode method;
|
||||
protected final int minSize;
|
||||
protected final int maxSize;
|
||||
|
||||
/**
|
||||
* @param api Same as for {@link org.objectweb.asm.MethodVisitor#MethodVisitor(int)} or any other ASM class
|
||||
* @param owner Internal name of the method's owner
|
||||
* @param method The method to find split points for
|
||||
* @param minSize The minimum number of instructions required for the split point to be valid
|
||||
* @param maxSize The maximum number of instructions that split points cannot exceeed
|
||||
*/
|
||||
public Splitter(int api, String owner, MethodNode method, int minSize, int maxSize) {
|
||||
this.api = api;
|
||||
this.owner = owner;
|
||||
this.method = method;
|
||||
this.minSize = minSize;
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<SplitPoint> iterator() { return new Iter(); }
|
||||
|
||||
// Types are always int, float, long, double, or ref (no other primitives)
|
||||
/** A split point in a method that can be split off into another method */
|
||||
public static class SplitPoint {
|
||||
/**
|
||||
* The locals read in this split area, keyed by index. Value type is always int, float, long, double, or object.
|
||||
*/
|
||||
public final SortedMap<Integer, Type> localsRead;
|
||||
/**
|
||||
* The locals written in this split area, keyed by index. Value type is always int, float, long, double, or object.
|
||||
*/
|
||||
public final SortedMap<Integer, Type> localsWritten;
|
||||
/**
|
||||
* The values of the stack needed at the start of this split area. Type is always int, float, long, double, or
|
||||
* object.
|
||||
*/
|
||||
public final List<Type> neededFromStackAtStart;
|
||||
/**
|
||||
* The values of the stack at the end of this split area that are needed to put back on the original. Type is always
|
||||
* int, float, long, double, or object.
|
||||
*/
|
||||
public final List<Type> putOnStackAtEnd;
|
||||
/**
|
||||
* The instruction index this split area begins at.
|
||||
*/
|
||||
public final int start;
|
||||
/**
|
||||
* The number of instructions this split area has.
|
||||
*/
|
||||
public final int length;
|
||||
|
||||
public SplitPoint(SortedMap<Integer, Type> localsRead, SortedMap<Integer, Type>localsWritten,
|
||||
List<Type> neededFromStackAtStart, List<Type> putOnStackAtEnd, int start, int length) {
|
||||
this.localsRead = localsRead;
|
||||
this.localsWritten = localsWritten;
|
||||
this.neededFromStackAtStart = neededFromStackAtStart;
|
||||
this.putOnStackAtEnd = putOnStackAtEnd;
|
||||
this.start = start;
|
||||
this.length = length;
|
||||
}
|
||||
}
|
||||
|
||||
protected int compareInsnIndexes(AbstractInsnNode o1, AbstractInsnNode o2) {
|
||||
return Integer.compare(method.instructions.indexOf(o1), method.instructions.indexOf(o2));
|
||||
}
|
||||
|
||||
protected class Iter implements Iterator<SplitPoint> {
|
||||
protected final AbstractInsnNode[] insns;
|
||||
protected final List<TryCatchBlockNode> tryCatchBlocks;
|
||||
protected int currIndex = -1;
|
||||
protected boolean peeked;
|
||||
protected SplitPoint peekedValue;
|
||||
|
||||
protected Iter() {
|
||||
insns = method.instructions.toArray();
|
||||
tryCatchBlocks = new ArrayList<>(method.tryCatchBlocks);
|
||||
// Must be sorted by earliest starting index then earliest end index then earliest handler
|
||||
tryCatchBlocks.sort((o1, o2) -> {
|
||||
int cmp = compareInsnIndexes(o1.start, o2.start);
|
||||
if (cmp == 0) compareInsnIndexes(o1.end, o2.end);
|
||||
if (cmp == 0) compareInsnIndexes(o1.handler, o2.handler);
|
||||
return cmp;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (!peeked) {
|
||||
peeked = true;
|
||||
peekedValue = nextOrNull();
|
||||
}
|
||||
return peekedValue != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SplitPoint next() {
|
||||
// If we've peeked in hasNext, use that
|
||||
SplitPoint ret;
|
||||
if (peeked) {
|
||||
peeked = false;
|
||||
ret = peekedValue;
|
||||
} else {
|
||||
ret = nextOrNull();
|
||||
}
|
||||
if (ret == null) throw new NoSuchElementException();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected SplitPoint nextOrNull() {
|
||||
// Try for each index
|
||||
while (++currIndex + minSize <= insns.length) {
|
||||
SplitPoint longest = longestForCurrIndex();
|
||||
if (longest != null) return longest;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected SplitPoint longestForCurrIndex() {
|
||||
// As a special case, if the previous insn was a line number, that was good enough
|
||||
if (currIndex - 1 >- 0 && insns[currIndex - 1] instanceof LineNumberNode) return null;
|
||||
// Build the info object
|
||||
InsnTraverseInfo info = new InsnTraverseInfo();
|
||||
info.startIndex = currIndex;
|
||||
info.endIndex = Math.min(currIndex + maxSize - 1, insns.length - 1);
|
||||
// Reduce the end based on try/catch blocks the start is in or that jump to
|
||||
constrainEndByTryCatchBlocks(info);
|
||||
// Reduce the end based on any jumps within
|
||||
constrainEndByInternalJumps(info);
|
||||
// Reduce the end based on any jumps into
|
||||
constrainEndByExternalJumps(info);
|
||||
// Make sure we didn't reduce the end too far
|
||||
if (info.getSize() < minSize) return null;
|
||||
// Now that we have our largest range from the start index, we can go over each updating the local refs and stack
|
||||
// For the stack, we are going to use the
|
||||
return splitPointFromInfo(info);
|
||||
}
|
||||
|
||||
protected void constrainEndByTryCatchBlocks(InsnTraverseInfo info) {
|
||||
// Go over all the try/catch blocks, sorted by earliest
|
||||
for (TryCatchBlockNode block : tryCatchBlocks) {
|
||||
int handleIndex = method.instructions.indexOf(block.handler);
|
||||
int startIndex = method.instructions.indexOf(block.start);
|
||||
int endIndex = method.instructions.indexOf(block.end) - 1;
|
||||
boolean catchWithinDisallowed;
|
||||
|
||||
if (info.startIndex <= startIndex && info.endIndex >= endIndex) {
|
||||
// The try block is entirely inside the range...
|
||||
catchWithinDisallowed = false;
|
||||
// Since it's entirely within, we need the catch handler within too
|
||||
if (handleIndex < info.startIndex || handleIndex > info.endIndex) {
|
||||
// Well, it's not within, so that means we can't include this try block at all
|
||||
info.endIndex = Math.min(info.endIndex, startIndex - 1);
|
||||
}
|
||||
} else if (info.startIndex > startIndex && info.endIndex > endIndex) {
|
||||
// The try block started before this range, but ends inside of it...
|
||||
// The end has to be changed to the block's end so it doesn't go over the boundary
|
||||
info.endIndex = Math.min(info.endIndex, endIndex);
|
||||
// The catch can't jump in here
|
||||
catchWithinDisallowed = true;
|
||||
} else if (info.startIndex <= startIndex && info.endIndex < endIndex) {
|
||||
// The try block started in this range, but ends outside of it...
|
||||
// Can't have the block then, reduce it to before the start
|
||||
info.endIndex = Math.min(info.endIndex, startIndex - 1);
|
||||
// Since we don't have the block, we can't jump in here either
|
||||
catchWithinDisallowed = true;
|
||||
} else {
|
||||
// The try block is completely outside, just restrict the catch from jumping in
|
||||
catchWithinDisallowed = true;
|
||||
}
|
||||
// If the catch is within and not allowed to be, we have to change the end to before it
|
||||
if (catchWithinDisallowed && info.startIndex <= handleIndex && info.endIndex >= handleIndex) {
|
||||
info.endIndex = Math.min(info.endIndex, handleIndex - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void constrainEndByInternalJumps(InsnTraverseInfo info) {
|
||||
for (int i = info.startIndex; i <= info.endIndex; i++) {
|
||||
AbstractInsnNode node = insns[i];
|
||||
int earliestIndex;
|
||||
int furthestIndex;
|
||||
if (node instanceof JumpInsnNode) {
|
||||
earliestIndex = method.instructions.indexOf(((JumpInsnNode) node).label);
|
||||
furthestIndex = earliestIndex;
|
||||
} else if (node instanceof TableSwitchInsnNode) {
|
||||
earliestIndex = method.instructions.indexOf(((TableSwitchInsnNode) node).dflt);
|
||||
furthestIndex = earliestIndex;
|
||||
for (LabelNode label : ((TableSwitchInsnNode) node).labels) {
|
||||
int index = method.instructions.indexOf(label);
|
||||
earliestIndex = Math.min(earliestIndex, index);
|
||||
furthestIndex = Math.max(furthestIndex, index);
|
||||
}
|
||||
} else if (node instanceof LookupSwitchInsnNode) {
|
||||
earliestIndex = method.instructions.indexOf(((LookupSwitchInsnNode) node).dflt);
|
||||
furthestIndex = earliestIndex;
|
||||
for (LabelNode label : ((LookupSwitchInsnNode) node).labels) {
|
||||
int index = method.instructions.indexOf(label);
|
||||
earliestIndex = Math.min(earliestIndex, index);
|
||||
furthestIndex = Math.max(furthestIndex, index);
|
||||
}
|
||||
} else continue;
|
||||
// Stop here if any indexes are out of range, otherwise, change end
|
||||
if (earliestIndex < info.startIndex || furthestIndex > info.endIndex) {
|
||||
info.endIndex = i - 1;
|
||||
return;
|
||||
}
|
||||
info.endIndex = Math.max(info.endIndex, furthestIndex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void constrainEndByExternalJumps(InsnTraverseInfo info) {
|
||||
// Basically, if any external jumps jump into our range, that can't be included in the range
|
||||
for (int i = 0; i < insns.length; i++) {
|
||||
if (i >= info.startIndex && i <= info.endIndex) continue;
|
||||
AbstractInsnNode node = insns[i];
|
||||
if (node instanceof JumpInsnNode) {
|
||||
int index = method.instructions.indexOf(((JumpInsnNode) node).label);
|
||||
if (index >= info.startIndex) info.endIndex = Math.min(info.endIndex, index - 1);
|
||||
} else if (node instanceof TableSwitchInsnNode) {
|
||||
int index = method.instructions.indexOf(((TableSwitchInsnNode) node).dflt);
|
||||
if (index >= info.startIndex) info.endIndex = Math.min(info.endIndex, index - 1);
|
||||
for (LabelNode label : ((TableSwitchInsnNode) node).labels) {
|
||||
index = method.instructions.indexOf(label);
|
||||
if (index >= info.startIndex) info.endIndex = Math.min(info.endIndex, index - 1);
|
||||
}
|
||||
} else if (node instanceof LookupSwitchInsnNode) {
|
||||
int index = method.instructions.indexOf(((LookupSwitchInsnNode) node).dflt);
|
||||
if (index >= info.startIndex) info.endIndex = Math.min(info.endIndex, index - 1);
|
||||
for (LabelNode label : ((LookupSwitchInsnNode) node).labels) {
|
||||
index = method.instructions.indexOf(label);
|
||||
if (index >= info.startIndex) info.endIndex = Math.min(info.endIndex, index - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected SplitPoint splitPointFromInfo(InsnTraverseInfo info) {
|
||||
// We're going to use the analyzer adapter and run it for the up until the end, a step at a time
|
||||
StackAndLocalTrackingAdapter adapter = new StackAndLocalTrackingAdapter(Splitter.this);
|
||||
// Visit all of the insns up our start.
|
||||
// XXX: I checked the source of AnalyzerAdapter to confirm I don't need any of the surrounding stuff
|
||||
for (int i = 0; i < info.startIndex; i++) insns[i].accept(adapter);
|
||||
// Take the stack at the start and copy it off
|
||||
List<Object> stackAtStart = new ArrayList<>(adapter.stack);
|
||||
// Reset some adapter state
|
||||
adapter.lowestStackSize = stackAtStart.size();
|
||||
adapter.localsRead.clear();
|
||||
adapter.localsWritten.clear();
|
||||
// Now go over the remaining range
|
||||
for (int i = info.startIndex; i <= info.endIndex; i++) insns[i].accept(adapter);
|
||||
// Build the split point
|
||||
return new SplitPoint(
|
||||
localMapFromAdapterLocalMap(adapter.localsRead, adapter.uninitializedTypes),
|
||||
localMapFromAdapterLocalMap(adapter.localsWritten, adapter.uninitializedTypes),
|
||||
typesFromAdapterStackRange(stackAtStart, adapter.lowestStackSize, adapter.uninitializedTypes),
|
||||
typesFromAdapterStackRange(adapter.stack, adapter.lowestStackSize, adapter.uninitializedTypes),
|
||||
info.startIndex,
|
||||
info.getSize()
|
||||
);
|
||||
}
|
||||
|
||||
protected SortedMap<Integer, Type> localMapFromAdapterLocalMap(
|
||||
SortedMap<Integer, Object> map, Map<Object, Object> uninitializedTypes) {
|
||||
SortedMap<Integer, Type> ret = new TreeMap<>();
|
||||
map.forEach((k, v) -> ret.put(k, typeFromAdapterStackItem(v, uninitializedTypes)));
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected List<Type> typesFromAdapterStackRange(
|
||||
List<Object> stack, int start, Map<Object, Object> uninitializedTypes) {
|
||||
List<Type> ret = new ArrayList<>();
|
||||
for (int i = start; i < stack.size(); i++) {
|
||||
Object item = stack.get(i);
|
||||
ret.add(typeFromAdapterStackItem(item, uninitializedTypes));
|
||||
// Jump an extra spot for longs and doubles
|
||||
if (item == Opcodes.LONG || item == Opcodes.DOUBLE) {
|
||||
if (stack.get(++i) != Opcodes.TOP) throw new IllegalStateException("Expected top after long/double");
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected Type typeFromAdapterStackItem(Object item, Map<Object, Object> uninitializedTypes) {
|
||||
if (item == Opcodes.INTEGER) return Type.INT_TYPE;
|
||||
else if (item == Opcodes.FLOAT) return Type.FLOAT_TYPE;
|
||||
else if (item == Opcodes.LONG) return Type.LONG_TYPE;
|
||||
else if (item == Opcodes.DOUBLE) return Type.DOUBLE_TYPE;
|
||||
else if (item == Opcodes.NULL) return OBJECT_TYPE;
|
||||
else if (item == Opcodes.UNINITIALIZED_THIS) return Type.getObjectType(owner);
|
||||
else if (item instanceof Label) return Type.getObjectType((String) uninitializedTypes.get(item));
|
||||
else if (item instanceof String) return Type.getObjectType((String) item);
|
||||
else throw new IllegalStateException("Unrecognized stack item: " + item);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class StackAndLocalTrackingAdapter extends AnalyzerAdapter {
|
||||
public int lowestStackSize;
|
||||
public final SortedMap<Integer, Object> localsRead = new TreeMap<>();
|
||||
public final SortedMap<Integer, Object> localsWritten = new TreeMap<>();
|
||||
|
||||
protected StackAndLocalTrackingAdapter(Splitter splitter) {
|
||||
super(splitter.api, splitter.owner, splitter.method.access, splitter.method.name, splitter.method.desc, null);
|
||||
stack = new SizeChangeNotifyList<Object>() {
|
||||
@Override
|
||||
protected void onSizeChanged() { lowestStackSize = Math.min(lowestStackSize, size()); }
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitVarInsn(int opcode, int var) {
|
||||
switch (opcode) {
|
||||
case Opcodes.ILOAD:
|
||||
case Opcodes.LLOAD:
|
||||
case Opcodes.FLOAD:
|
||||
case Opcodes.DLOAD:
|
||||
case Opcodes.ALOAD:
|
||||
localsRead.put(var, locals.get(var));
|
||||
break;
|
||||
case Opcodes.ISTORE:
|
||||
case Opcodes.FSTORE:
|
||||
case Opcodes.ASTORE:
|
||||
localsWritten.put(var, stack.get(stack.size() - 1));
|
||||
break;
|
||||
case Opcodes.LSTORE:
|
||||
case Opcodes.DSTORE:
|
||||
localsWritten.put(var, stack.get(stack.size() - 2));
|
||||
break;
|
||||
}
|
||||
super.visitVarInsn(opcode, var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitIincInsn(int var, int increment) {
|
||||
localsRead.put(var, Type.INT_TYPE);
|
||||
localsWritten.put(var, Type.INT_TYPE);
|
||||
super.visitIincInsn(var, increment);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class SizeChangeNotifyList<T> extends AbstractList<T> {
|
||||
protected final ArrayList<T> list = new ArrayList<>();
|
||||
|
||||
protected void onSizeChanged() { }
|
||||
|
||||
@Override
|
||||
public T get(int index) { return list.get(index); }
|
||||
|
||||
@Override
|
||||
public int size() { return list.size(); }
|
||||
|
||||
@Override
|
||||
public T set(int index, T element) { return list.set(index, element); }
|
||||
|
||||
@Override
|
||||
public void add(int index, T element) {
|
||||
list.add(index, element);
|
||||
onSizeChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T remove(int index) {
|
||||
T ret = list.remove(index);
|
||||
onSizeChanged();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
protected static class InsnTraverseInfo {
|
||||
public int startIndex;
|
||||
// Can only shrink, never increase in size
|
||||
public int endIndex;
|
||||
|
||||
public int getSize() { return endIndex - startIndex + 1; }
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
package asmble.compile.jvm.msplit;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
class Util {
|
||||
private Util() { }
|
||||
|
||||
static final Type OBJECT_TYPE = Type.getType(Object.class);
|
||||
|
||||
static AbstractInsnNode zeroVal(Type type) {
|
||||
if (type == Type.INT_TYPE) return new InsnNode(Opcodes.ICONST_0);
|
||||
else if (type == Type.LONG_TYPE) return new InsnNode(Opcodes.LCONST_0);
|
||||
else if (type == Type.FLOAT_TYPE) return new InsnNode(Opcodes.FCONST_0);
|
||||
else if (type == Type.DOUBLE_TYPE) return new InsnNode(Opcodes.DCONST_0);
|
||||
else return new InsnNode(Opcodes.ACONST_NULL);
|
||||
}
|
||||
|
||||
static boolean isStoreOp(int opcode) {
|
||||
return opcode == Opcodes.ISTORE || opcode == Opcodes.LSTORE || opcode == Opcodes.FSTORE ||
|
||||
opcode == Opcodes.DSTORE || opcode == Opcodes.ASTORE;
|
||||
}
|
||||
|
||||
static int storeOpFromType(Type type) {
|
||||
if (type == Type.INT_TYPE) return Opcodes.ISTORE;
|
||||
else if (type == Type.LONG_TYPE) return Opcodes.LSTORE;
|
||||
else if (type == Type.FLOAT_TYPE) return Opcodes.FSTORE;
|
||||
else if (type == Type.DOUBLE_TYPE) return Opcodes.DSTORE;
|
||||
else return Opcodes.ASTORE;
|
||||
}
|
||||
|
||||
static int loadOpFromType(Type type) {
|
||||
if (type == Type.INT_TYPE) return Opcodes.ILOAD;
|
||||
else if (type == Type.LONG_TYPE) return Opcodes.LLOAD;
|
||||
else if (type == Type.FLOAT_TYPE) return Opcodes.FLOAD;
|
||||
else if (type == Type.DOUBLE_TYPE) return Opcodes.DLOAD;
|
||||
else return Opcodes.ALOAD;
|
||||
}
|
||||
|
||||
static Type boxedTypeIfNecessary(Type type) {
|
||||
if (type == Type.INT_TYPE) return Type.getType(Integer.class);
|
||||
else if (type == Type.LONG_TYPE) return Type.getType(Long.class);
|
||||
else if (type == Type.FLOAT_TYPE) return Type.getType(Float.class);
|
||||
else if (type == Type.DOUBLE_TYPE) return Type.getType(Double.class);
|
||||
else return type;
|
||||
}
|
||||
|
||||
static void boxStackIfNecessary(Type type, MethodNode method) {
|
||||
if (type == Type.INT_TYPE) boxCall(Integer.class, type).accept(method);
|
||||
else if (type == Type.FLOAT_TYPE) boxCall(Float.class, type).accept(method);
|
||||
else if (type == Type.LONG_TYPE) boxCall(Long.class, type).accept(method);
|
||||
else if (type == Type.DOUBLE_TYPE) boxCall(Double.class, type).accept(method);
|
||||
}
|
||||
|
||||
static void unboxStackIfNecessary(Type type, MethodNode method) {
|
||||
if (type == Type.INT_TYPE) method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||
"java/lang/Integer", "intValue", Type.getMethodDescriptor(Type.INT_TYPE), false);
|
||||
else if (type == Type.FLOAT_TYPE) method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||
"java/lang/Float", "floatValue", Type.getMethodDescriptor(Type.FLOAT_TYPE), false);
|
||||
else if (type == Type.LONG_TYPE) method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||
"java/lang/Long", "longValue", Type.getMethodDescriptor(Type.LONG_TYPE), false);
|
||||
else if (type == Type.DOUBLE_TYPE) method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||
"java/lang/Double", "doubleValue", Type.getMethodDescriptor(Type.DOUBLE_TYPE), false);
|
||||
}
|
||||
|
||||
static AbstractInsnNode intConst(int v) {
|
||||
switch (v) {
|
||||
case -1: return new InsnNode(Opcodes.ICONST_M1);
|
||||
case 0: return new InsnNode(Opcodes.ICONST_0);
|
||||
case 1: return new InsnNode(Opcodes.ICONST_1);
|
||||
case 2: return new InsnNode(Opcodes.ICONST_2);
|
||||
case 3: return new InsnNode(Opcodes.ICONST_3);
|
||||
case 4: return new InsnNode(Opcodes.ICONST_4);
|
||||
case 5: return new InsnNode(Opcodes.ICONST_5);
|
||||
default: return new LdcInsnNode(v);
|
||||
}
|
||||
}
|
||||
|
||||
static MethodInsnNode boxCall(Class<?> boxType, Type primType) {
|
||||
return new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(boxType),
|
||||
"valueOf", Type.getMethodDescriptor(Type.getType(boxType), primType), false);
|
||||
}
|
||||
}
|
@ -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(),
|
||||
@ -172,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
|
||||
@ -188,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
|
||||
@ -233,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>
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
@ -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()
|
||||
|
183
compiler/src/main/kotlin/asmble/ast/opt/SplitLargeFunc.kt
Normal file
183
compiler/src/main/kotlin/asmble/ast/opt/SplitLargeFunc.kt
Normal 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()
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package asmble.cli
|
||||
|
||||
import asmble.ast.Script
|
||||
import asmble.compile.jvm.AsmToBinary
|
||||
import asmble.compile.jvm.AstToAsm
|
||||
import asmble.compile.jvm.ClsContext
|
||||
import asmble.compile.jvm.withComputedFramesAndMaxs
|
||||
import java.io.FileOutputStream
|
||||
|
||||
@Suppress("NAME_SHADOWING")
|
||||
@ -69,7 +69,7 @@ open class Compile : Command<Compile.Args>() {
|
||||
includeBinary = args.includeBinary
|
||||
)
|
||||
AstToAsm.fromModule(ctx)
|
||||
outStream.write(AsmToBinary.fromClassNode(ctx.cls))
|
||||
outStream.write(ctx.cls.withComputedFramesAndMaxs())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -1,7 +1,7 @@
|
||||
package asmble.cli
|
||||
|
||||
import asmble.compile.jvm.AsmToBinary
|
||||
import asmble.compile.jvm.Linker
|
||||
import asmble.compile.jvm.withComputedFramesAndMaxs
|
||||
import java.io.FileOutputStream
|
||||
|
||||
open class Link : Command<Link.Args>() {
|
||||
@ -52,7 +52,7 @@ open class Link : Command<Link.Args>() {
|
||||
defaultMaxMemPages = args.defaultMaxMem
|
||||
)
|
||||
Linker.link(ctx)
|
||||
outStream.write(AsmToBinary.fromClassNode(ctx.cls))
|
||||
outStream.write(ctx.cls.withComputedFramesAndMaxs())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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}" }
|
||||
|
@ -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>>,
|
||||
|
146
compiler/src/main/kotlin/asmble/cli/SplitFunc.kt
Normal file
146
compiler/src/main/kotlin/asmble/cli/SplitFunc.kt
Normal 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()
|
||||
}
|
@ -58,7 +58,10 @@ open class Translate : Command<Translate.Args>() {
|
||||
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) {
|
||||
|
@ -2,6 +2,7 @@ package asmble.compile.jvm
|
||||
|
||||
import asmble.ast.Node
|
||||
import org.objectweb.asm.ClassReader
|
||||
import org.objectweb.asm.ClassWriter
|
||||
import org.objectweb.asm.Opcodes
|
||||
import org.objectweb.asm.Type
|
||||
import org.objectweb.asm.tree.*
|
||||
@ -188,6 +189,16 @@ fun MethodNode.toAsmString(): String {
|
||||
val Node.Type.Func.asmDesc: String get() =
|
||||
(this.ret?.typeRef ?: Void::class.ref).asMethodRetDesc(*this.params.map { it.typeRef }.toTypedArray())
|
||||
|
||||
fun ClassNode.withComputedFramesAndMaxs(
|
||||
cw: ClassWriter = ClassWriter(ClassWriter.COMPUTE_FRAMES + ClassWriter.COMPUTE_MAXS)
|
||||
): ByteArray {
|
||||
// Note, compute maxs adds a bunch of NOPs for unreachable code.
|
||||
// See $func12 of block.wast. I don't believe the extra time over the
|
||||
// instructions to remove the NOPs is worth it.
|
||||
this.accept(cw)
|
||||
return cw.toByteArray()
|
||||
}
|
||||
|
||||
fun ClassNode.toAsmString(): String {
|
||||
val stringWriter = StringWriter()
|
||||
this.accept(TraceClassVisitor(PrintWriter(stringWriter)))
|
||||
|
@ -1,51 +0,0 @@
|
||||
package asmble.compile.jvm
|
||||
|
||||
import asmble.compile.jvm.msplit.SplitMethod
|
||||
import org.objectweb.asm.ClassWriter
|
||||
import org.objectweb.asm.MethodTooLargeException
|
||||
import org.objectweb.asm.Opcodes
|
||||
import org.objectweb.asm.tree.ClassNode
|
||||
|
||||
/**
|
||||
* May mutate given class nodes on [fromClassNode] if [splitMethod] is present (the default). Uses the two-param
|
||||
* [SplitMethod.split] call to try and split overly large methods.
|
||||
*/
|
||||
open class AsmToBinary(val splitMethod: SplitMethod? = SplitMethod(Opcodes.ASM6)) {
|
||||
fun fromClassNode(
|
||||
cn: ClassNode,
|
||||
newClassWriter: () -> ClassWriter = { ClassWriter(ClassWriter.COMPUTE_FRAMES + ClassWriter.COMPUTE_MAXS) }
|
||||
): ByteArray {
|
||||
while (true) {
|
||||
val cw = newClassWriter()
|
||||
// Note, compute maxs adds a bunch of NOPs for unreachable code.
|
||||
// See $func12 of block.wast. I don't believe the extra time over the
|
||||
// instructions to remove the NOPs is worth it.
|
||||
cn.accept(cw)
|
||||
try {
|
||||
return cw.toByteArray()
|
||||
} catch (e: MethodTooLargeException) {
|
||||
if (splitMethod == null) throw e
|
||||
// Split the offending method by removing it and replacing it with the split ones
|
||||
require(cn.name == e.className)
|
||||
val tooLargeIndex = cn.methods.indexOfFirst { it.name == e.methodName && it.desc == e.descriptor }
|
||||
require(tooLargeIndex >= 0)
|
||||
val split = splitMethod.split(cn.name, cn.methods[tooLargeIndex])
|
||||
split ?: throw IllegalStateException("Failed to split", e)
|
||||
// Change the split off method's name if there's already one
|
||||
val origName = split.splitOffMethod.name
|
||||
var foundCount = 0
|
||||
while (cn.methods.any { it.name == split.splitOffMethod.name }) {
|
||||
split.splitOffMethod.name = origName + (++foundCount)
|
||||
}
|
||||
// Replace at the index
|
||||
cn.methods.removeAt(tooLargeIndex)
|
||||
cn.methods.add(tooLargeIndex, split.splitOffMethod)
|
||||
cn.methods.add(tooLargeIndex, split.trimmedMethod)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object : AsmToBinary() {
|
||||
val noSplit = AsmToBinary(null)
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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) {
|
||||
@ -130,19 +150,29 @@ open class InsnReworker {
|
||||
// if we are at 0, add the result of said block if necessary to the count.
|
||||
if (insideOfBlocks > 0) {
|
||||
// If it's not a block, just ignore it
|
||||
(insns[insnIndex] as? Node.Instr.Args.Type)?.let {
|
||||
val blockStackDiff = insns[insnIndex].let {
|
||||
when (it) {
|
||||
is Node.Instr.Block -> if (it.type == null) 0 else 1
|
||||
is Node.Instr.Loop -> 0
|
||||
is Node.Instr.If -> if (it.type == null) -1 else 0
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
if (blockStackDiff != null) {
|
||||
insideOfBlocks--
|
||||
ctx.trace { "Found block begin, number of blocks we're still inside: $insideOfBlocks" }
|
||||
// We're back on our block, change the count if it had a result
|
||||
if (insideOfBlocks == 0 && it.type != null) countSoFar++
|
||||
// We're back on our block, change the count
|
||||
if (insideOfBlocks == 0) countSoFar += blockStackDiff
|
||||
}
|
||||
if (insideOfBlocks > 0) continue
|
||||
}
|
||||
|
||||
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" }
|
||||
@ -205,17 +235,24 @@ open class InsnReworker {
|
||||
}
|
||||
|
||||
// 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.Else, is Node.Instr.End, is Node.Instr.Br,
|
||||
is Node.Instr.Loop, is Node.Instr.If, is Node.Instr.Else,
|
||||
is Node.Instr.End, is Node.Instr.Br, is Node.Instr.BrIf,
|
||||
is Node.Instr.Return -> NOP
|
||||
is Node.Instr.If, is Node.Instr.BrIf, is Node.Instr.BrTable -> POP_PARAM
|
||||
is Node.Instr.BrTable -> POP_PARAM
|
||||
is Node.Instr.Call -> ctx.funcTypeAtIndex(insn.index).let {
|
||||
// All calls pop params and any return is a push
|
||||
(POP_PARAM * it.params.size) + (if (it.ret == null) NOP else PUSH_RESULT)
|
||||
@ -283,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
|
||||
|
@ -201,7 +201,7 @@ open class Linker {
|
||||
"instance" + mod.name.javaIdent.capitalize(), mod.ref.asmDesc),
|
||||
InsnNode(Opcodes.ARETURN)
|
||||
)
|
||||
ctx.cls.methods.plusAssign(func.toMethodNode())
|
||||
ctx.cls.methods.plusAssign(func)
|
||||
}
|
||||
|
||||
class ModuleClass(val cls: Class<*>, overrideName: String? = null) {
|
||||
|
@ -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
|
||||
|
@ -186,12 +186,14 @@ 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
|
||||
|
@ -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
|
||||
|
@ -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(),
|
||||
@ -305,15 +319,10 @@ data class ScriptContext(
|
||||
bindImport(import, true, MethodType.methodType(Array<MethodHandle>::class.java)).
|
||||
invokeWithArguments()!! as Array<MethodHandle>
|
||||
|
||||
open class SimpleClassLoader(
|
||||
parent: ClassLoader,
|
||||
logger: Logger,
|
||||
val splitWhenTooLarge: Boolean = true
|
||||
) : ClassLoader(parent), Logger by logger {
|
||||
open class SimpleClassLoader(parent: ClassLoader, logger: Logger) : ClassLoader(parent), Logger by logger {
|
||||
fun fromBuiltContext(ctx: ClsContext): Class<*> {
|
||||
trace { "Computing frames for ASM class:\n" + ctx.cls.toAsmString() }
|
||||
val writer = if (splitWhenTooLarge) AsmToBinary else AsmToBinary.noSplit
|
||||
return writer.fromClassNode(ctx.cls).let { bytes ->
|
||||
return ctx.cls.withComputedFramesAndMaxs().let { bytes ->
|
||||
debug { "ASM class:\n" + bytes.asClassNode().toAsmString() }
|
||||
defineClass("${ctx.packageName}.${ctx.className}", bytes, 0, bytes.size)
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ class SpecTestUnit(name: String, wast: String, expectedOutput: String?) : BaseTe
|
||||
"nop" -> 20
|
||||
"memory_grow" -> 830
|
||||
"imports" -> 5
|
||||
else -> 2
|
||||
else -> 1
|
||||
}
|
||||
|
||||
override fun warningInsteadOfErrReason(t: Throwable) = when (name) {
|
||||
|
@ -1,31 +1,29 @@
|
||||
package asmble.run.jvm
|
||||
package asmble.ast.opt
|
||||
|
||||
import asmble.TestBase
|
||||
import asmble.ast.Node
|
||||
import asmble.compile.jvm.AstToAsm
|
||||
import asmble.compile.jvm.ClsContext
|
||||
import org.junit.Assert
|
||||
import asmble.run.jvm.ScriptContext
|
||||
import org.junit.Test
|
||||
import org.objectweb.asm.MethodTooLargeException
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LargeFuncTest : TestBase() {
|
||||
class SplitLargeFuncTest : TestBase() {
|
||||
@Test
|
||||
fun testLargeFunc() {
|
||||
val numInsnChunks = 6001
|
||||
// Make large func that does some math
|
||||
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 = 4, maximum = 4))),
|
||||
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 numInsnChunks).flatMap {
|
||||
instructions = (0 until 501).flatMap {
|
||||
listOf<Node.Instr>(
|
||||
Node.Instr.I32Const(it * 4),
|
||||
// Let's to i * (i = 1)
|
||||
@ -49,21 +47,43 @@ class LargeFuncTest : TestBase() {
|
||||
)
|
||||
// Compile it
|
||||
AstToAsm.fromModule(ctx)
|
||||
// Confirm the method size is too large
|
||||
try {
|
||||
ScriptContext.SimpleClassLoader(javaClass.classLoader, logger, splitWhenTooLarge = false).
|
||||
fromBuiltContext(ctx)
|
||||
Assert.fail()
|
||||
} catch (e: MethodTooLargeException) { }
|
||||
// Try again with split
|
||||
val cls = ScriptContext.SimpleClassLoader(javaClass.classLoader, logger).fromBuiltContext(ctx)
|
||||
// Create it and check that it still does what we expect
|
||||
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 numInsnChunks).forEach { assertEquals(it * (it - 1), mem.getInt(it * 4)) }
|
||||
(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"
|
||||
}
|
||||
}
|
||||
}
|
Submodule compiler/src/test/resources/spec updated: fda397a56f...1f00d57d00
@ -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)
|
@ -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
|
@ -1,3 +0,0 @@
|
||||
int addOne(int x) {
|
||||
return x + 1;
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -2,8 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
fmt.Printf("Args: %v", os.Args)
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user