Difference between revisions of "WebAssembly/Compiler"

From Lazarus wiki
m (See Also)
(Embedded target)
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
== Instructions ==
 
== Instructions ==
=== Compiling the compiler ===
+
 
 +
=== Prerequisites ===
 +
 
 +
The ''llvm-mc'' assembler and ''wasm-ld'' linker. These are the assembler and linker from the LLVM project.
 +
 
 +
=== Obtaining the compiler sources ===
  
 
Get the sources:
 
Get the sources:
  
  git clone https://github.com/skalogryz/freepascal fpc-wasm
+
svn checkout https://svn.freepascal.org/svn/fpc/branches/wasm/ fpc-wasm
+
 
switch to correct branch:
+
enter the directory:
  cd fpc-wasm
+
cd fpc-wasm
  git checkout webasm
+
 
 +
=== Supported targets ===
 +
 
 +
==== WASI - The WebAssembly System Interface ====
 +
 
 +
[https://wasi.dev/ WASI] is a modular system interface for WebAssembly. It allows creating portable and secure programs that can run in a sandboxed command-line environment. See the WASI website for more information.
 +
 
 +
To build the compiler for the WASI target:
 +
 
 +
make all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc
 +
 
 +
Currently, it reaches the point, where it fails with this error:
 +
 
 +
globals.pas(40,7) Fatal: Can't find unit sysutils used by globals
 +
Fatal: Compilation aborted
  
compile:
+
This is because the sysutils hasn't been ported to the WASI target, yet. If compilation reaches this point, you can still compile 'Hello, world!'. The following units have been ported for the WASI target:
  cd compiler
 
 
 
====Using Lazarus====
 
  open ppcwasm.lpi
 
hit the "compile" button.
 
  
=====Using command-line=====
+
* system
  fpc -dnoopt -dwasm -Fiwasm -Fuwasm -Fusystems -S2 pp.pas
+
* objpas
 +
* ctypes
 +
* strings
 +
* wasiapi - interface for the WASI API
  
the compiler can be renamed
+
==== Embedded target ====
  mv pp ppwasm
+
 
or, on windows:
+
The [[TARGET_Embedded|Embedded target]] is primarily used for embedded systems, without an operating system. However, it also happens to be a perfect fit for creating WebAssembly modules that don't use any particular operating system-like API.
  mv pp.exe ppwasm.exe
+
 
 +
make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc
  
 
=== Using the compiler ===
 
=== Using the compiler ===
  
You need a system.pas unit. It can be found here:
+
Hint: If your fpc.cfg has
 +
<pre>
 +
#IFDEF FPC_CROSSCOMPILING
 +
#IFDEF NEEDCROSSBINUTILS
 +
  -XP$FPCTARGET-
 +
#ENDIF
 +
#ENDIF
 +
</pre>
 +
 
 +
Enclose it in '''#IFNDEF CPUWASM32''' :
 +
<pre>
 +
#IFDEF FPC_CROSSCOMPILING
 +
#IFDEF NEEDCROSSBINUTILS
 +
#ifndef cpuwasm32
 +
  -XP$FPCTARGET-
 +
#endif
 +
#ENDIF
 +
#ENDIF
 +
</pre>
 +
 
 +
Otherwise, you have to create symlinks wasm-wasm-wasmld to wasmld, and wasm-wasm-wasmtool to wasmtool.
 +
 
 +
==wasm-demo==
 +
Wasm-demo is a Pascal WebAssembly demo project, ported Google's C WebAssembly example (https://codelabs.developers.google.com/codelabs/web-assembly-intro)
 +
 
 +
You follow these steps in order to get the demo project working:
 +
 
 +
1. Get the project sources: https://github.com/skalogryz/wasm-demo
 +
 
 +
===Using in webassembly.studio===
 +
2. Run the wasm compiler against lyff.pas, but request the assembler file to be kept
 +
  pp -a lyff.pas
 +
Webassembly.studio accepts the assembler file and would compile it on the server side.
 +
 
 +
3. open up  https://webassembly.studio and select "Create empty Wat Project". The project will consist of:
 +
:main.html
 +
:main.js
 +
:main.wat
 +
All files of the project can be edited through the web interface.
  
[https://github.com/skalogryz/wasm-demo wasm-demo]
+
4. Copy and paste the contents of the following files:
 +
: (compiled) lyff.wat to main.wat.  
 +
: wasm-demo\webassembly.studio\main.html to main.html
 +
: wasm-demo\webassembly.studio\main.js to main.js
  
You'll also need the wasm tools:
+
Note: every time you copy and paste, don't forget to press "SAVE" button (on the right side of the web interface)
  
[https://github.com/WebAssembly/wabt/releases wasm tools]
+
5. once all files are update, hit "Build and Run"
  
Extract the binaries and put them somewhere in your path.
 
 
==See Also==
 
==See Also==
 
*[[WebAssembly]]
 
*[[WebAssembly]]
 
[[Category:WebAssembly]]
 
[[Category:WebAssembly]]

Latest revision as of 05:42, 31 January 2021

Instructions

Prerequisites

The llvm-mc assembler and wasm-ld linker. These are the assembler and linker from the LLVM project.

Obtaining the compiler sources

Get the sources:

svn checkout https://svn.freepascal.org/svn/fpc/branches/wasm/ fpc-wasm

enter the directory:

cd fpc-wasm

Supported targets

WASI - The WebAssembly System Interface

WASI is a modular system interface for WebAssembly. It allows creating portable and secure programs that can run in a sandboxed command-line environment. See the WASI website for more information.

To build the compiler for the WASI target:

make all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc

Currently, it reaches the point, where it fails with this error:

globals.pas(40,7) Fatal: Can't find unit sysutils used by globals
Fatal: Compilation aborted

This is because the sysutils hasn't been ported to the WASI target, yet. If compilation reaches this point, you can still compile 'Hello, world!'. The following units have been ported for the WASI target:

  • system
  • objpas
  • ctypes
  • strings
  • wasiapi - interface for the WASI API

Embedded target

The Embedded target is primarily used for embedded systems, without an operating system. However, it also happens to be a perfect fit for creating WebAssembly modules that don't use any particular operating system-like API.

make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc

Using the compiler

Hint: If your fpc.cfg has

#IFDEF FPC_CROSSCOMPILING
#IFDEF NEEDCROSSBINUTILS
  -XP$FPCTARGET-
#ENDIF
#ENDIF

Enclose it in #IFNDEF CPUWASM32 :

#IFDEF FPC_CROSSCOMPILING
#IFDEF NEEDCROSSBINUTILS
#ifndef cpuwasm32
  -XP$FPCTARGET-
#endif
#ENDIF
#ENDIF

Otherwise, you have to create symlinks wasm-wasm-wasmld to wasmld, and wasm-wasm-wasmtool to wasmtool.

wasm-demo

Wasm-demo is a Pascal WebAssembly demo project, ported Google's C WebAssembly example (https://codelabs.developers.google.com/codelabs/web-assembly-intro)

You follow these steps in order to get the demo project working:

1. Get the project sources: https://github.com/skalogryz/wasm-demo

Using in webassembly.studio

2. Run the wasm compiler against lyff.pas, but request the assembler file to be kept

 pp -a lyff.pas

Webassembly.studio accepts the assembler file and would compile it on the server side.

3. open up https://webassembly.studio and select "Create empty Wat Project". The project will consist of:

main.html
main.js
main.wat

All files of the project can be edited through the web interface.

4. Copy and paste the contents of the following files:

(compiled) lyff.wat to main.wat.
wasm-demo\webassembly.studio\main.html to main.html
wasm-demo\webassembly.studio\main.js to main.js

Note: every time you copy and paste, don't forget to press "SAVE" button (on the right side of the web interface)

5. once all files are update, hit "Build and Run"

See Also