From 4d72864977d93f7db544cdc7a549aedc30574b01 Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Wed, 20 Jun 2018 19:14:02 -0400 Subject: [PATCH 01/10] Improve clang documentation --- clang.md | 65 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/clang.md b/clang.md index ded7333..0895f1c 100644 --- a/clang.md +++ b/clang.md @@ -1,26 +1,50 @@ # Compiling C/C++ to WebAssembly -## Rolling your own compiler +Many high level languages already support compilation to WebAssembly +through the experimental LLVM backend. Unfortunately, it is a tedious +and poorly documented process. This document aims to alleviate some +of this tedium with a step-by-step tutorial to compile some basic C +code to WAST format using LLVM, as well as provide instructions for building +the toolchain. -Clang has a WebAssembly target, though it is not easy to use currently. First, a custom build must be made. +## Dependencies + +- LLVM + Clang: Must be built with the experimental WebAssebmly backend enabled +- Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST + +## Install LLVM and Clang with the WebAssembly backend + +### From the repo + +First, clone the needed repositories: -To build `clang`: ```sh git clone http://llvm.org/git/llvm.git cd llvm/tools git clone http://llvm.org/git/clang.git cd ../projects git clone http://llvm.org/git/compiler-rt.git + +Then initialize CMake: + +```sh mkdir ../build cd ../build cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. +``` + +Lastly, call `make` as usual: + +```sh cmake --build . -cd ../.. ``` -This will take anything from 1 to 5 hours. +At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. + +## Install Binaryen + +This one is much easier. Simply: -To build `binaryen`: ```sh git clone https://github.com/WebAssembly/binaryen.git cd binaryen @@ -28,27 +52,22 @@ mkdir build cd build cmake .. cmake --build . -cd ../.. ``` -## Using this compiler +CMake will also generate an `install` target if you want to actually install Binaryen on your system. -Now everything is set to finally compile *Hello World*! +## Compile C/C++ to WebAssembly -The compilation process has four steps: -- compiling (`clang`) -- linking to LLVM IR (`llc`) -- compiling to WebAssembly S-expressions (`s2wasm`) -- compiling to WebAssembly binary (`wasm-as`) +First we must compile C to LLVM bitcode through the Clang frontend: -Note: the last step can also be accomplished with [wabt](https://github.com/webassembly/wabt) (previously called *sexpr-wasm-prototype*). +`clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` -Cheat sheet: -```sh -clang -emit-llvm --target=wasm32-unknown-unknown-elf -nostdlib -S hello.c -llc -o hello.s hello.ll -s2wasm -o hello.wast hello.s -wasm-as -o hello.wasm hello.wast -``` +Next we can generate linear WASM output from the bitcode: + +`llc -asm-verbose=false -o main.s main.bc` + +The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: + +`s2wasm -o main.wast main.s` -There you go, you have your very first WebAssembly binary. +The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. From c22dc748060324ff14fa895cdb018fd4c63090b5 Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Wed, 20 Jun 2018 19:14:02 -0400 Subject: [PATCH 02/10] Add docs for using LLVM toolchain with Webassembly --- c2wasm.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 c2wasm.md diff --git a/c2wasm.md b/c2wasm.md new file mode 100644 index 0000000..8fe66e0 --- /dev/null +++ b/c2wasm.md @@ -0,0 +1,78 @@ +# Compiling C to WebAssembly +Many high level languages already support compilation to WebAssembly +through the experimental LLVM backend. Unfortunately, it is a tedious +and poorly documented process. This document aims to alleviate some +of this tedium with a step-by-step tutorial to compile some basic C +code to WAST format using LLVM. + +## Dependencies +- LLVM + Clang: Must be built with the experimental WASM backend enabled +- Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST + +## Install LLVM and Clang with the WASM backend +#### Via package manager +Some package managers support package options which ease the process of +installing LLVM with special features. If you have one of these, you are in luck. +##### Portage +The only ebuilds of LLVM and Clang supporting the WASM target are the ones that checkout +the latest sources from subversion. Because they are regarded as unstable by Portage, +we must manually unmask these packages. This is done by telling portage to accept all keywords +for said packages. + +Add the following lines to your `package.accept_keywords` file: +`=sys-devel/llvm-9999 ** +=sys-devel/clang-9999 ** +=sys-devel/clang-runtime-9999 ** +=sys-libs/libomp-9999 ** +=sys-devel/clang-common-6.0.0 ~amd64 +=sys-libs/compiler-rt-9999 ** +=sys-libs/compiler-rt-sanitizers-9999 **` + +Next we add the following USE flags to package.use: +`>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly +>=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` +Note: Specifying all package atoms with a higher version than `5.0.1` just in case a stable version +releases with WASM support. + +Lastly, LLVM and Clang can be emerged. Make sure that the USE flag is enabled: +`sudo emerge -av llvm clang` + +##### Other package managers +install gentoo + +#### From the repo +First, clone the needed repositories: +`git clone http://llvm.org/git/llvm.git +cd llvm/tools +git clone http://llvm.org/git/clang.git +cd ../projects +git clone http://llvm.org/git/compiler-rt.git` + +Then initialize CMake: +`mkdir ../build +cd ../build +cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` + +Lastly, call `make` as usual: +`make -j4 all` + +At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. + +## Install Binaryen +This one is much easier. Simply: +`git clone https://github.com/WebAssembly/binaryen.git +cd binaryen +mkdir build +cd build +cmake .. +make -j4 all` +CMake will also generate an `install` target if you want to actually install Binaryen on your system. + +## Compile C to WASM +First we must compile C to LLVM bitcode through the Clang frontend: +`clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` +Next we can generate WASM output from the bitcode: +`llc -asm-verbose=false -o main.s main.bc` +The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: +`s2wasm -o main.wast main.s` +The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. From c62f155c82ab54e47ef587d2911fcf2e202ab98c Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Wed, 20 Jun 2018 19:24:04 -0400 Subject: [PATCH 03/10] formatting --- c2wasm.md | 82 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/c2wasm.md b/c2wasm.md index 8fe66e0..45b0911 100644 --- a/c2wasm.md +++ b/c2wasm.md @@ -10,69 +10,69 @@ code to WAST format using LLVM. - Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST ## Install LLVM and Clang with the WASM backend -#### Via package manager +### Via package manager Some package managers support package options which ease the process of installing LLVM with special features. If you have one of these, you are in luck. -##### Portage +#### Portage The only ebuilds of LLVM and Clang supporting the WASM target are the ones that checkout the latest sources from subversion. Because they are regarded as unstable by Portage, we must manually unmask these packages. This is done by telling portage to accept all keywords for said packages. -Add the following lines to your `package.accept_keywords` file: -`=sys-devel/llvm-9999 ** -=sys-devel/clang-9999 ** -=sys-devel/clang-runtime-9999 ** -=sys-libs/libomp-9999 ** -=sys-devel/clang-common-6.0.0 ~amd64 -=sys-libs/compiler-rt-9999 ** -=sys-libs/compiler-rt-sanitizers-9999 **` +Add the following lines to your `package.accept_keywords` file: +`=sys-devel/llvm-9999 ** +=sys-devel/clang-9999 ** +=sys-devel/clang-runtime-9999 ** +=sys-libs/libomp-9999 ** +=sys-devel/clang-common-6.0.0 ~amd64 +=sys-libs/compiler-rt-9999 ** +=sys-libs/compiler-rt-sanitizers-9999 **` -Next we add the following USE flags to package.use: -`>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly ->=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` +Next we add the following USE flags to package.use: +`>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly +>=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` Note: Specifying all package atoms with a higher version than `5.0.1` just in case a stable version releases with WASM support. -Lastly, LLVM and Clang can be emerged. Make sure that the USE flag is enabled: +Lastly, LLVM and Clang can be emerged. Make sure that the USE flag is enabled: `sudo emerge -av llvm clang` -##### Other package managers +#### Other package managers install gentoo -#### From the repo -First, clone the needed repositories: -`git clone http://llvm.org/git/llvm.git -cd llvm/tools -git clone http://llvm.org/git/clang.git -cd ../projects -git clone http://llvm.org/git/compiler-rt.git` +### From the repo +First, clone the needed repositories: +`git clone http://llvm.org/git/llvm.git +cd llvm/tools +git clone http://llvm.org/git/clang.git +cd ../projects +git clone http://llvm.org/git/compiler-rt.git` -Then initialize CMake: -`mkdir ../build -cd ../build -cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` +Then initialize CMake: +`mkdir ../build +cd ../build +cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` -Lastly, call `make` as usual: -`make -j4 all` +Lastly, call `make` as usual: +`make -j4 all` At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. ## Install Binaryen This one is much easier. Simply: -`git clone https://github.com/WebAssembly/binaryen.git -cd binaryen -mkdir build -cd build -cmake .. -make -j4 all` +`git clone https://github.com/WebAssembly/binaryen.git +cd binaryen +mkdir build +cd build +cmake .. +make -j4 all` CMake will also generate an `install` target if you want to actually install Binaryen on your system. ## Compile C to WASM -First we must compile C to LLVM bitcode through the Clang frontend: -`clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` -Next we can generate WASM output from the bitcode: -`llc -asm-verbose=false -o main.s main.bc` -The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: -`s2wasm -o main.wast main.s` -The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. +First we must compile C to LLVM bitcode through the Clang frontend: +`clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` +Next we can generate WASM output from the bitcode: +`llc -asm-verbose=false -o main.s main.bc` +The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: +`s2wasm -o main.wast main.s` +The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. From f9e7757f3a87a654d788fb22c1ca95205105be45 Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Wed, 20 Jun 2018 19:27:35 -0400 Subject: [PATCH 04/10] formatting 2 --- c2wasm.md | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/c2wasm.md b/c2wasm.md index 45b0911..eaa958d 100644 --- a/c2wasm.md +++ b/c2wasm.md @@ -20,17 +20,17 @@ we must manually unmask these packages. This is done by telling portage to accep for said packages. Add the following lines to your `package.accept_keywords` file: -`=sys-devel/llvm-9999 ** -=sys-devel/clang-9999 ** -=sys-devel/clang-runtime-9999 ** -=sys-libs/libomp-9999 ** -=sys-devel/clang-common-6.0.0 ~amd64 -=sys-libs/compiler-rt-9999 ** -=sys-libs/compiler-rt-sanitizers-9999 **` +`=sys-devel/llvm-9999 **` +`=sys-devel/clang-9999 **` +`=sys-devel/clang-runtime-9999 **` +`=sys-libs/libomp-9999 **` +`=sys-devel/clang-common-6.0.0 ~amd64` +`=sys-libs/compiler-rt-9999 **` +`=sys-libs/compiler-rt-sanitizers-9999 **` Next we add the following USE flags to package.use: -`>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly ->=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` +`>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly` +`>=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` Note: Specifying all package atoms with a higher version than `5.0.1` just in case a stable version releases with WASM support. @@ -42,16 +42,16 @@ install gentoo ### From the repo First, clone the needed repositories: -`git clone http://llvm.org/git/llvm.git -cd llvm/tools -git clone http://llvm.org/git/clang.git -cd ../projects -git clone http://llvm.org/git/compiler-rt.git` +`git clone http://llvm.org/git/llvm.git` +`cd llvm/tools` +`git clone http://llvm.org/git/clang.git` +`cd ../projects` +`git clone http://llvm.org/git/compiler-rt.git` Then initialize CMake: -`mkdir ../build -cd ../build -cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` +`mkdir ../build` +`cd ../build` +`cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` Lastly, call `make` as usual: `make -j4 all` @@ -60,13 +60,13 @@ At the end of the (long) build the binaries will be in `bin`. Feel free to add t ## Install Binaryen This one is much easier. Simply: -`git clone https://github.com/WebAssembly/binaryen.git -cd binaryen -mkdir build -cd build -cmake .. -make -j4 all` -CMake will also generate an `install` target if you want to actually install Binaryen on your system. +`git clone https://github.com/WebAssembly/binaryen.git` +`cd binaryen` +`mkdir build` +`cd build` +`cmake ..` +`make -j4 all` +`CMake will also generate an `install` target if you want to actually install Binaryen on your system. ## Compile C to WASM First we must compile C to LLVM bitcode through the Clang frontend: From ada878680007ba26fa724691d1d92bfe2e8b891f Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Wed, 20 Jun 2018 19:30:10 -0400 Subject: [PATCH 05/10] moar formatting --- c2wasm.md | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/c2wasm.md b/c2wasm.md index eaa958d..42ddaed 100644 --- a/c2wasm.md +++ b/c2wasm.md @@ -19,18 +19,28 @@ the latest sources from subversion. Because they are regarded as unstable by Por we must manually unmask these packages. This is done by telling portage to accept all keywords for said packages. -Add the following lines to your `package.accept_keywords` file: +Add the following lines to your `package.accept_keywords` file: + `=sys-devel/llvm-9999 **` + `=sys-devel/clang-9999 **` + `=sys-devel/clang-runtime-9999 **` + `=sys-libs/libomp-9999 **` + `=sys-devel/clang-common-6.0.0 ~amd64` + `=sys-libs/compiler-rt-9999 **` + `=sys-libs/compiler-rt-sanitizers-9999 **` Next we add the following USE flags to package.use: + `>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly` + `>=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` + Note: Specifying all package atoms with a higher version than `5.0.1` just in case a stable version releases with WASM support. @@ -42,37 +52,59 @@ install gentoo ### From the repo First, clone the needed repositories: + `git clone http://llvm.org/git/llvm.git` + `cd llvm/tools` + `git clone http://llvm.org/git/clang.git` + `cd ../projects` + `git clone http://llvm.org/git/compiler-rt.git` Then initialize CMake: + `mkdir ../build` + `cd ../build` + `cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` Lastly, call `make` as usual: + `make -j4 all` At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. ## Install Binaryen This one is much easier. Simply: + `git clone https://github.com/WebAssembly/binaryen.git` + `cd binaryen` + `mkdir build` + `cd build` + `cmake ..` + `make -j4 all` -`CMake will also generate an `install` target if you want to actually install Binaryen on your system. + +CMake will also generate an `install` target if you want to actually install Binaryen on your system. ## Compile C to WASM First we must compile C to LLVM bitcode through the Clang frontend: + `clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` + Next we can generate WASM output from the bitcode: + `llc -asm-verbose=false -o main.s main.bc` + The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: + `s2wasm -o main.wast main.s` + The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. From 39cd72ae714edd4f773cdc4ea24c10efecf52bb1 Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Thu, 5 Jul 2018 17:52:08 -0400 Subject: [PATCH 06/10] remove package manager specific docs --- c2wasm.md | 42 ++---------------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/c2wasm.md b/c2wasm.md index 42ddaed..eb94c10 100644 --- a/c2wasm.md +++ b/c2wasm.md @@ -3,52 +3,14 @@ Many high level languages already support compilation to WebAssembly through the experimental LLVM backend. Unfortunately, it is a tedious and poorly documented process. This document aims to alleviate some of this tedium with a step-by-step tutorial to compile some basic C -code to WAST format using LLVM. +code to WAST format using LLVM, as well as provide instructions for building +the toolchain. ## Dependencies - LLVM + Clang: Must be built with the experimental WASM backend enabled - Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST ## Install LLVM and Clang with the WASM backend -### Via package manager -Some package managers support package options which ease the process of -installing LLVM with special features. If you have one of these, you are in luck. -#### Portage -The only ebuilds of LLVM and Clang supporting the WASM target are the ones that checkout -the latest sources from subversion. Because they are regarded as unstable by Portage, -we must manually unmask these packages. This is done by telling portage to accept all keywords -for said packages. - -Add the following lines to your `package.accept_keywords` file: - -`=sys-devel/llvm-9999 **` - -`=sys-devel/clang-9999 **` - -`=sys-devel/clang-runtime-9999 **` - -`=sys-libs/libomp-9999 **` - -`=sys-devel/clang-common-6.0.0 ~amd64` - -`=sys-libs/compiler-rt-9999 **` - -`=sys-libs/compiler-rt-sanitizers-9999 **` - -Next we add the following USE flags to package.use: - -`>=sys-devel/llvm-5.0.1 llvm_targets_WebAssembly` - -`>=sys-devel/clang-5.0.1 llvm_targets_WebAssembly` - -Note: Specifying all package atoms with a higher version than `5.0.1` just in case a stable version -releases with WASM support. - -Lastly, LLVM and Clang can be emerged. Make sure that the USE flag is enabled: -`sudo emerge -av llvm clang` - -#### Other package managers -install gentoo ### From the repo First, clone the needed repositories: From ff66ae7d227e82ae8dc9623d594adac2dc42de73 Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Wed, 5 Dec 2018 15:53:30 -0500 Subject: [PATCH 07/10] f --- c2wasm.md | 72 ------------------------------------------------------- clang.md | 4 ++-- 2 files changed, 2 insertions(+), 74 deletions(-) delete mode 100644 c2wasm.md diff --git a/c2wasm.md b/c2wasm.md deleted file mode 100644 index eb94c10..0000000 --- a/c2wasm.md +++ /dev/null @@ -1,72 +0,0 @@ -# Compiling C to WebAssembly -Many high level languages already support compilation to WebAssembly -through the experimental LLVM backend. Unfortunately, it is a tedious -and poorly documented process. This document aims to alleviate some -of this tedium with a step-by-step tutorial to compile some basic C -code to WAST format using LLVM, as well as provide instructions for building -the toolchain. - -## Dependencies -- LLVM + Clang: Must be built with the experimental WASM backend enabled -- Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST - -## Install LLVM and Clang with the WASM backend - -### From the repo -First, clone the needed repositories: - -`git clone http://llvm.org/git/llvm.git` - -`cd llvm/tools` - -`git clone http://llvm.org/git/clang.git` - -`cd ../projects` - -`git clone http://llvm.org/git/compiler-rt.git` - -Then initialize CMake: - -`mkdir ../build` - -`cd ../build` - -`cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..` - -Lastly, call `make` as usual: - -`make -j4 all` - -At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. - -## Install Binaryen -This one is much easier. Simply: - -`git clone https://github.com/WebAssembly/binaryen.git` - -`cd binaryen` - -`mkdir build` - -`cd build` - -`cmake ..` - -`make -j4 all` - -CMake will also generate an `install` target if you want to actually install Binaryen on your system. - -## Compile C to WASM -First we must compile C to LLVM bitcode through the Clang frontend: - -`clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` - -Next we can generate WASM output from the bitcode: - -`llc -asm-verbose=false -o main.s main.bc` - -The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: - -`s2wasm -o main.wast main.s` - -The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. diff --git a/clang.md b/clang.md index 0895f1c..a3e9468 100644 --- a/clang.md +++ b/clang.md @@ -66,8 +66,8 @@ Next we can generate linear WASM output from the bitcode: `llc -asm-verbose=false -o main.s main.bc` -The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: +The backend output is in linear assembly format, so we must convert this to WAST with binaryen's `s2wasm` tool: `s2wasm -o main.wast main.s` -The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. +The code will now be in WAST format but must be cleaned up with [wasm-chisel](https://github.com/wasmx/wasm-chisel) to be deployed as a contract. From 7742ce13586e386cd12f7d669a0dde294a8b6966 Mon Sep 17 00:00:00 2001 From: Lane Rettig Date: Thu, 27 Dec 2018 06:53:32 -0500 Subject: [PATCH 08/10] Some minor tweaks --- clang.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/clang.md b/clang.md index a3e9468..f66b0c4 100644 --- a/clang.md +++ b/clang.md @@ -12,6 +12,8 @@ the toolchain. - LLVM + Clang: Must be built with the experimental WebAssebmly backend enabled - Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST +Instructions for installing these necessary components follow. + ## Install LLVM and Clang with the WebAssembly backend ### From the repo @@ -24,6 +26,7 @@ cd llvm/tools git clone http://llvm.org/git/clang.git cd ../projects git clone http://llvm.org/git/compiler-rt.git +``` Then initialize CMake: @@ -33,13 +36,13 @@ cd ../build cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. ``` -Lastly, call `make` as usual: +Lastly, call `cmake` as usual: ```sh cmake --build . ``` -At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. +At the end of the build process the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. ## Install Binaryen From cfc05d58f06543413b97fc31c4c58bb328c41f10 Mon Sep 17 00:00:00 2001 From: Lane Rettig Date: Thu, 27 Dec 2018 07:42:50 -0500 Subject: [PATCH 09/10] Add link on linear wasm --- clang.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang.md b/clang.md index f66b0c4..469ca29 100644 --- a/clang.md +++ b/clang.md @@ -65,7 +65,7 @@ First we must compile C to LLVM bitcode through the Clang frontend: `clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` -Next we can generate linear WASM output from the bitcode: +Next we can generate [linear WASM output](https://github.com/WebAssembly/design/blob/master/TextFormat.md#linear-instructions) from the bitcode: `llc -asm-verbose=false -o main.s main.bc` From 835762370a6ce19d51a3ab5b2a680bdf008eeac6 Mon Sep 17 00:00:00 2001 From: Lane Rettig Date: Thu, 27 Dec 2018 07:43:54 -0500 Subject: [PATCH 10/10] Wasm/wast should not be uppercase --- clang.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang.md b/clang.md index 469ca29..8e2a1b3 100644 --- a/clang.md +++ b/clang.md @@ -4,13 +4,13 @@ Many high level languages already support compilation to WebAssembly through the experimental LLVM backend. Unfortunately, it is a tedious and poorly documented process. This document aims to alleviate some of this tedium with a step-by-step tutorial to compile some basic C -code to WAST format using LLVM, as well as provide instructions for building +code to wast format using LLVM, as well as provide instructions for building the toolchain. ## Dependencies - LLVM + Clang: Must be built with the experimental WebAssebmly backend enabled -- Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST +- Binaryen: Needed to convert the `.s` output of LLVM's backend to wast Instructions for installing these necessary components follow. @@ -65,12 +65,12 @@ First we must compile C to LLVM bitcode through the Clang frontend: `clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` -Next we can generate [linear WASM output](https://github.com/WebAssembly/design/blob/master/TextFormat.md#linear-instructions) from the bitcode: +Next we can generate [linear Wasm output](https://github.com/WebAssembly/design/blob/master/TextFormat.md#linear-instructions) from the bitcode: `llc -asm-verbose=false -o main.s main.bc` -The backend output is in linear assembly format, so we must convert this to WAST with binaryen's `s2wasm` tool: +The backend output is in linear assembly format, so we must convert this to wast with binaryen's `s2wasm` tool: `s2wasm -o main.wast main.s` -The code will now be in WAST format but must be cleaned up with [wasm-chisel](https://github.com/wasmx/wasm-chisel) to be deployed as a contract. +The code will now be in wast format but must be cleaned up with [wasm-chisel](https://github.com/wasmx/wasm-chisel) to be deployed as a contract.