Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Zilliqa/scilla-compiler

Repository files navigation

Scilla Compiler

Build Status License

Introduction

A compiler to translate Scilla to LLVM-IR. Scilla transitions in the generated LLVM-IR can be compiled to a shared library and executed using ScillaRTL. The project is under active development and is not intended for production use yet.

Building

Prerequisites:

  • sudo apt-get update && sudo apt-get install -yq ninja-build libboost-system-dev libboost-test-dev

LLVM

The Scilla compiler requires LLVM-13 to be installed on your system. Refer to https://apt.llvm.org/ on how to install LLVM-13.

  • wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
  • sudo apt-get update
  • sudo apt-get install llvm-13 clang-13

Building LLVM from Source (Optional)

Instructions to build LLVM from source

If you are building LLVM from source, it must be built with OCaml support.

First, you need to install the requirements in your OCaml switch:

opam install ctypes ocamlfind

To install the OCaml bindings to your local opam directory, the following CMake configuration flags must be provided to LLVM:

  • -DLLVM_OCAML_INSTALL_PATH="/your/opam/switch/lib": change the value based on your OCaml switch. The built OCaml bindings will be installed to this path.
  • -DLLVM_ENABLE_RTTI=ON: This is required by ScillaRTL.
  • -G "Ninja" to build LLVM using ninja instead of make. This is not necessary, but is usually faster.

Check if your LLVM bindings are built successfully:

  • ninja check-llvm-bindings-ocaml.

Install the bindings to your opam switch:

  • ninja bindings/ocaml/install

For convenience and CI purposes scripts/build_install_llvm.sh has been provided which downloads and builds LLVM in ${HOME} and installs it to the current opam switch. You can modify and use it as necessary.

Install Scilla_base and other dependencies

The compiler depends on Scilla base, which can be installed by following the instructions here After installing Scilla_base, run the following.

  • opam install ./ --deps-only --with-test --yes

Build and test the Compiler

To build and obtain the executables in the project's bin directory:

  • make
  • make test

If you are using LLVM from a non-system path (because you built it yourself, for example) then the environment variable LIBRARY_PATH must be set to the directory that contains built LLVM libraries.

Installing the compiler to your opam switch

This will install the compiler binaries to your current opam switch. This step is not usually necessary to try out and use the compiler.

  • make install

This will uninstall Scilla compiler installed with the previous command.

  • make uninstall

Compiling Scilla to LLVM-IR

LLVM-IR generated from this compiler can be compiled and linked into a shared library (clang -shared) and executed using ScillaRTL. (If you didn't install the compiler to your opam switch, you can run the binaries directly from the bin directory).

Compile a full Scilla contract:

  • scilla-llvm -libdir src/stdlib tests/codegen/contr/simple-map.scilla -gaslimit 10000

Compile a pure Scilla expression:

  • expr-llvm -libdir src/stdlib tests/codegen/expr/match_assign.scilexp -gaslimit 10000

Testsuite

The testsuite includes both Scilla expressions and full contracts for which LLVM-IR is generated and compared against pre-generated text.

  • make test