manuel-serrano / bigloo

a practical Scheme compiler
http://www-sop.inria.fr/indes/fp/Bigloo
Other
135 stars 19 forks source link

WebAssembly #44

Open manuel-serrano opened 4 years ago

manuel-serrano commented 4 years ago

Eduardo tried to generate WebAssembly code and failed. Here is an excerpt of his last post:

Now, let us see the problem. It would be great if Bigloo could generate WebAssembly through emsdk and emcc. As far as I know, Mark Feeley had no difficulty in creating an emsdk version for gambit scheme. However, this was not very useful since gambit does not generate executable code, therefore one cannot send a scheme app to a browser. As for Bigloo, I tried to build it through the emcc toolchain, but I failed. Then I am writing to you, in order to request that emsdk and emcc be added to the ports that Bigloo already has.

I'm not familiar with emsdk and emcc. If this is the way to go, probably Marc will be able to help. However, I'm wondering if this really what you want. If you want to generate webassembly, my first impression is that clang/llvm and wasi are better options. Did you try that? A quick check let me think that all the pieces needed to compile and test are already there (and apparently the packages are available from Debian bulleyes). It seems a promising direction. Is there a volunteer to try? I will help as much as I can.

FemtoEmacs commented 4 years ago

The emsdk and emcc are based on gcc/clang and offer gcc libraries and a complete toolchain. To show you how easy is to program in emcc, I wrote a small demo using the sdl library for gcc. It shows the Bigloo logo, which you can move through the keyboard arrows. Here is the link:

http://medicina.tips/big/mvlogo.html

And here is what emcc --version says:

~/em/wasmtests/owl/mvbigloo$ emcc --version
emcc (Emscripten gcc/clang-like replacement) 1.39.15
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.

The instructions for installing Emscripten are given on the following page:

https://emscripten.org/docs/getting_started/downloads.html

It is very simple and fast. I will give you a summary below.

# Get the emsdk repo
~$ git clone https://github.com/emscripten-core/emsdk.git

# Enter that directory
~$ cd emsdk

# Fetch the latest version of the emsdk (not needed the first time you clone)
~$ git pull

# Download and install the latest SDK tools.
~$ ./emsdk install latest

# Make the "latest" SDK "active" for the current user. (writes ~/.emscripten file)
~$ ./emsdk activate latest

# Activate PATH and other environment variables 
~$ source ./emsdk_env.sh

It is advisable to export the environment variables through the .bashrc configuration file. My home directory is /home/scm and thus I added the lines below to the .bashrc file:

# export GUILE_JIT_THRESHOLD=2                                                  
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:/lib
export GUILE_LOAD_PATH=$GUILE_LOAD_PATH:/usr/share/guile/3.0
export PATH=$PATH:/home/scm/racket/bin

# source /home/scm/emsdk/emsdk_env.sh                                           
export PATH=$PATH:/home/scm/emsdk
export PATH=$PATH:/home/scm/emsdk/upstream/emscripten
export PATH=$PATH:/home/scm/emsdk/node/12.9.1_64bit/bin

# Setting environment variables:                                                
export EMSDK=/home/scm/emsdk
export EM_CONFIG=/home/scm/.emscripten
export EMSDK_NODE=/home/scm/emsdk/node/12.9.1_64bit/bin/node

The conclusion it that you will not use more than a couple of hours to port Bigloo to the Emscripten toolchain. However, I have another project that is similar to the WASI project, which you mentioned. WebAssembly uses sexpr syntax, but it requires type declaration/annotation for every constant, every instruction and every variable. I decided to write a clean version, with succinct declarations. Therefore, I am writing a lisp like syntax for WebAssembly. This syntax is called Low Level Lisp, or LLL for short. Together with this syntax, I am writing a tutorial on how to use Bigloo and LLL on the Web. Here is the project:

https://github.com/FemtoEmacs/wasCm

I would like to make LLL more Scheme like. For this, I need to add lexical closures and list processing. Could you help me with that? You will find a prototype list processing on the book, which is included in the project.

FemtoEmacs commented 4 years ago

Dear Manoel.

In order to give you a proof of concept of a Scheme to C compiler that can generate code for running on a browser, I implemented a tiny browser Scheme that outputs C code, which one can easily build through the emcc compiler from the Emscripten toolchain. The browser Scheme is based on a paper published by Matthew Might, which explained how to compile Scheme to C through closure conversion. Unfortunately, Matthew Might did not provide enough details on how his compiler is built. In particular, I could not figure out how to implement tail call optimization, even though the emcc C compiler is based on the gcc, and does have TCO. To make things easier, you don't need to install the Emscripten toolchain. You can use the gcc toolchain, in order to check the behaviour of the browser Scheme. In the link below, you will find a Readme file that explains how to use the browser Scheme.

https://github.com/FemtoEmacs/wasCm/tree/master/s03-to-c

I hope that you can help me with the TCO, even if you decide against porting Bigloo to the Emscripten toolchain.

takikawa commented 1 year ago

Hi folks, I know this is a fairly old issue but it seemed worth mentioning here that I was able to get Bigloo to build with the emscripten toolchain. Unfortunately while I was able to compile Bigloo code to Wasm, there's some difficulty in actually running it and getting a useful result (it hits the unreachable Wasm instruction and throws, possibly somewhere in the runtime initialization).

I made the build reproducible by making a Dockerfile for it, which you can find here: https://github.com/takikawa/bigloo-wasm-dockerfile/

It relies on several patches (uploaded to Gist, linked from the Dockerfile) to make the build work on recent emscripten toolchains. I'm sharing this in case anyone finds it useful to build on (and can figure out how to get past the unreachable crashes).

takikawa commented 1 year ago

It relies on several patches (uploaded to Gist, linked from the Dockerfile) to make the build work on recent emscripten toolchains. I'm sharing this in case anyone finds it useful to build on (and can figure out how to get past the unreachable crashes).

BTW in the latest version, I was able to get simple examples like "hello world" and numeric operations to run without crashing on wasm. I was also able to get some longer examples to run, such as maze.scm from the examples directory.