Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3cb439e

Browse files
committedDec 15, 2017
First C example for issue #11
1 parent 923946f commit 3cb439e

File tree

7 files changed

+95
-1
lines changed

7 files changed

+95
-1
lines changed
 

‎.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
.classpath
2+
.project
3+
.settings
14
/gradlew
25
/gradlew.bat
36
/.gradle
47
/.idea
58
/build
69
/gradle
10+
/compiler/bin
711
/compiler/build
812
/compiler/out
13+
/emscripten-runtime/bin
914
/emscripten-runtime/build
1015
/emscripten-runtime/out
16+
/annotations/bin
1117
/annotations/build
1218
/annotations/out
19+
/examples/c-simple/bin
20+
/examples/c-simple/build
1321
/examples/rust-simple/Cargo.lock
22+
/examples/rust-simple/bin
1423
/examples/rust-simple/build
1524
/examples/rust-simple/target
1625
/examples/rust-string/Cargo.lock
26+
/examples/rust-string/bin
1727
/examples/rust-string/build
1828
/examples/rust-string/target
1929
/examples/rust-regex/Cargo.lock
30+
/examples/rust-regex/bin
2031
/examples/rust-regex/build
2132
/examples/rust-regex/target

‎build.gradle

+46
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,40 @@ project(':examples') {
6060
compileOnly project(':compiler')
6161
}
6262

63+
// C/C++ example helpers
64+
65+
task cToWasm {
66+
doFirst {
67+
mkdir 'build'
68+
exec {
69+
def cFileName = fileTree(dir: 'src', includes: ['*.c']).files.iterator().next()
70+
commandLine 'clang', '--target=wasm32-unknown-unknown-wasm', '-O3', cFileName, '-c', '-o', 'build/lib.wasm'
71+
}
72+
}
73+
}
74+
75+
task showCWast(type: JavaExec) {
76+
dependsOn cToWasm
77+
classpath configurations.compileClasspath
78+
main = 'asmble.cli.MainKt'
79+
doFirst {
80+
args 'translate', 'build/lib.wasm'
81+
}
82+
}
83+
84+
task compileCWasm(type: JavaExec) {
85+
dependsOn cToWasm
86+
classpath configurations.compileClasspath
87+
main = 'asmble.cli.MainKt'
88+
doFirst {
89+
def outFile = 'build/wasm-classes/' + wasmCompiledClassName.replace('.', '/') + '.class'
90+
file(outFile).parentFile.mkdirs()
91+
args 'compile', 'build/lib.wasm', wasmCompiledClassName, '-out', outFile
92+
}
93+
}
94+
95+
// Rust example helpers
96+
6397
ext.rustBuildRelease = true
6498

6599
task rustToWasm(type: Exec) {
@@ -107,6 +141,18 @@ project(':examples') {
107141
}
108142
}
109143

144+
project(':examples:c-simple') {
145+
apply plugin: 'application'
146+
ext.wasmCompiledClassName = 'asmble.generated.CSimple'
147+
dependencies {
148+
compile files('build/wasm-classes')
149+
}
150+
compileJava {
151+
dependsOn compileCWasm
152+
}
153+
mainClassName = 'asmble.examples.csimple.Main'
154+
}
155+
110156
project(':examples:rust-regex') {
111157
apply plugin: 'application'
112158
apply plugin: 'me.champeau.gradle.jmh'

‎examples/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ Compile Rust to WASM and then to the JVM. In order of complexity:
88

99
* [rust-simple](rust-simple)
1010
* [rust-string](rust-string)
11-
* [rust-regex](rust-regex)
11+
* [rust-regex](rust-regex)
12+
13+
### C/C++
14+
15+
Compile C to WASM and then to the JVM. In order of complexity:
16+
17+
* [c-simple](c-simple)

‎examples/c-simple/README.md

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

‎examples/c-simple/src/lib.c

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int addOne(int x) {
2+
return x + 1;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package asmble.examples.csimple;
2+
3+
import java.lang.invoke.MethodHandle;
4+
5+
import asmble.generated.CSimple;
6+
7+
class Main {
8+
public static void main(String[] args) {
9+
// Doesn't need memory or method table
10+
CSimple simple = new CSimple(0, new MethodHandle[0]);
11+
System.out.println("25 + 1 = " + simple.addOne(25));
12+
}
13+
}

‎settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ rootProject.name = 'asmble'
22
include 'annotations',
33
'compiler',
44
'emscripten-runtime',
5+
'examples:c-simple',
56
'examples:rust-regex',
67
'examples:rust-simple',
78
'examples:rust-string'

0 commit comments

Comments
 (0)
Please sign in to comment.