mirror of
https://github.com/fluencelabs/asmble
synced 2025-04-25 06:42:22 +00:00
Merge pull request #2 from fluencelabs/logger
Fix logger and return C example
This commit is contained in:
commit
b9b45cf997
127
build.gradle
127
build.gradle
@ -21,7 +21,11 @@ buildscript {
|
|||||||
allprojects {
|
allprojects {
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
group 'com.github.cretz.asmble'
|
group 'com.github.cretz.asmble'
|
||||||
version '0.4.0-fl'
|
version '0.4.1-fl'
|
||||||
|
|
||||||
|
// skips building and running for the specified examples
|
||||||
|
ext.skipExamples = ['c-simple', 'go-simple', 'rust-regex']
|
||||||
|
// todo disabling Rust regex is temporary because some strings in wasm code exceed the size in 65353 bytes.
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@ -68,6 +72,38 @@ project(':examples') {
|
|||||||
compileOnly project(':compiler')
|
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
|
// Go example helpers
|
||||||
|
|
||||||
task goToWasm {
|
task goToWasm {
|
||||||
@ -142,8 +178,33 @@ project(':examples') {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project(':examples:c-simple') {
|
||||||
|
if (project.name in skipExamples) {
|
||||||
|
println("[Note!] Building and runnig for ${project.name} was skipped")
|
||||||
|
test.onlyIf { false } // explicit skipping tests
|
||||||
|
compileJava.onlyIf { false } // explicit skipping compile
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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') {
|
project(':examples:go-simple') {
|
||||||
|
if (project.name in skipExamples) {
|
||||||
|
println("[Note!] Building and runnig for ${project.name} was skipped")
|
||||||
|
test.onlyIf { false } // explicit skipping tests
|
||||||
|
compileJava.onlyIf { false } // explicit skipping compile
|
||||||
|
return
|
||||||
|
}
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
ext.wasmCompiledClassName = 'asmble.generated.GoSimple'
|
ext.wasmCompiledClassName = 'asmble.generated.GoSimple'
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -155,32 +216,44 @@ project(':examples:go-simple') {
|
|||||||
mainClassName = 'asmble.examples.gosimple.Main'
|
mainClassName = 'asmble.examples.gosimple.Main'
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo temporary disable Rust regex, because some strings in wasm code exceed the size in 65353 bytes.
|
project(':examples:rust-regex') {
|
||||||
|
if (project.name in skipExamples) {
|
||||||
|
println("[Note!] Building and runnig for ${project.name} was skipped")
|
||||||
|
test.onlyIf { false } // explicit skipping tests
|
||||||
|
compileJava.onlyIf { false } // explicit skipping compile
|
||||||
|
compileTestJava.onlyIf { false } // explicit skipping compile
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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'
|
||||||
|
|
||||||
// project(':examples:rust-regex') {
|
test {
|
||||||
// apply plugin: 'application'
|
testLogging.showStandardStreams = true
|
||||||
// apply plugin: 'me.champeau.gradle.jmh'
|
testLogging.events 'PASSED', 'SKIPPED'
|
||||||
// ext.wasmCompiledClassName = 'asmble.generated.RustRegex'
|
}
|
||||||
// dependencies {
|
jmh {
|
||||||
// compile files('build/wasm-classes')
|
iterations = 5
|
||||||
// testCompile 'junit:junit:4.12'
|
warmupIterations = 5
|
||||||
// }
|
fork = 3
|
||||||
// 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') {
|
project(':examples:rust-simple') {
|
||||||
|
if (project.name in skipExamples) {
|
||||||
|
println("[Note!] Building and runnig for ${project.name} was skipped")
|
||||||
|
test.onlyIf { false } // explicit skipping tests
|
||||||
|
compileJava.onlyIf { false } // explicit skipping compile
|
||||||
|
return
|
||||||
|
}
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
ext.wasmCompiledClassName = 'asmble.generated.RustSimple'
|
ext.wasmCompiledClassName = 'asmble.generated.RustSimple'
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -193,6 +266,12 @@ project(':examples:rust-simple') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
project(':examples:rust-string') {
|
project(':examples:rust-string') {
|
||||||
|
if (project.name in skipExamples) {
|
||||||
|
println("[Note!] Building and runnig for ${project.name} was skipped")
|
||||||
|
test.onlyIf { false } // explicit skipping tests
|
||||||
|
compileJava.onlyIf { false } // explicit skipping compile
|
||||||
|
return
|
||||||
|
}
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
ext.wasmCompiledClassName = 'asmble.generated.RustString'
|
ext.wasmCompiledClassName = 'asmble.generated.RustString'
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -56,7 +56,10 @@ abstract class ScriptCommand<T> : Command<T>() {
|
|||||||
// if input file is class file
|
// if input file is class file
|
||||||
"class" -> ctx.classLoader.addClass(File(inFile).readBytes()).let { ctx }
|
"class" -> ctx.classLoader.addClass(File(inFile).readBytes()).let { ctx }
|
||||||
// if input file is wasm file
|
// if input file is wasm file
|
||||||
else -> Translate.also { it.logger = logger }.inToAst(inFile, inFile.substringAfterLast('.')).let { inAst ->
|
else -> {
|
||||||
|
val translateCmd = Translate
|
||||||
|
translateCmd.logger = this.logger
|
||||||
|
translateCmd.inToAst(inFile, inFile.substringAfterLast('.')).let { inAst ->
|
||||||
val (mod, name) = (inAst.commands.singleOrNull() as? Script.Cmd.Module) ?:
|
val (mod, name) = (inAst.commands.singleOrNull() as? Script.Cmd.Module) ?:
|
||||||
error("Input file must only contain a single module")
|
error("Input file must only contain a single module")
|
||||||
val className = name?.javaIdent?.capitalize() ?:
|
val className = name?.javaIdent?.capitalize() ?:
|
||||||
@ -69,6 +72,7 @@ abstract class ScriptCommand<T> : Command<T>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw Exception("Failed loading $inFile - ${e.message}", e)
|
throw Exception("Failed loading $inFile - ${e.message}", e)
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ open class Translate : Command<Translate.Args>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"wasm" ->
|
"wasm" ->
|
||||||
Script(listOf(Script.Cmd.Module(BinaryToAst(logger = logger).toModule(
|
Script(listOf(Script.Cmd.Module(BinaryToAst(logger = this.logger).toModule(
|
||||||
ByteReader.InputStream(inBytes.inputStream())), null)))
|
ByteReader.InputStream(inBytes.inputStream())), null)))
|
||||||
else -> error("Unknown in format '$inFormat'")
|
else -> error("Unknown in format '$inFormat'")
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,11 @@ package asmble.io
|
|||||||
|
|
||||||
import asmble.ast.Node
|
import asmble.ast.Node
|
||||||
import asmble.util.*
|
import asmble.util.*
|
||||||
import java.io.ByteArrayInputStream
|
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
|
|
||||||
open class BinaryToAst(
|
open class BinaryToAst(
|
||||||
val version: Long = 1L,
|
val version: Long = 1L,
|
||||||
val logger: Logger = Logger.Print(Logger.Level.OFF),
|
val logger: Logger = Logger.Print(Logger.Level.WARN),
|
||||||
val includeNameSection: Boolean = true
|
val includeNameSection: Boolean = true
|
||||||
) : Logger by logger {
|
) : Logger by logger {
|
||||||
|
|
||||||
|
14
examples/c-simple/README.md
Normal file
14
examples/c-simple/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
### 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
|
3
examples/c-simple/src/lib.c
Normal file
3
examples/c-simple/src/lib.c
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
int addOne(int x) {
|
||||||
|
return x + 1;
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user