User Manual

Introduction

Cyclone is a brand-new Scheme-to-C compiler with the goal of supporting real-world application development using the R7RS Scheme Language standard. We provide modern features and a stable system capable of generating fast native binaries.

A variant of the Cheney on the MTA technique is used to implement full tail recursion, continuations, generational garbage collection, and native threads. This is the same technique proposed by Henry Baker (Cheney on the MTA) and implemented first by CHICKEN Scheme. The difference is that our compiler allows multiple native threads, each with their own stack. A tracing garbage collector is used to manage the second-generation heap and perform major collections without “stopping the world”.

Cyclone is developed by Justin Ethier.

Bug reports and patches are welcome! Please report any issues using the Issues Page.

Requirements

Cyclone has been tested under Linux on the x86, x86-64, and ARM platforms.

The following packages are required:

On a Debian variant such as Ubuntu the necessary packages may be installed via the command:

sudo apt-get install libck-dev make gcc

The following command can be used to install dependencies on Fedora, though libck will also need to be built from source:

sudo yum install gcc make

Installation

Cyclone cannot be built directly on a system that does not have Cyclone binaries installed because the compiler is self-hosting. The easiest way to install Cyclone binaries is to build from source using cyclone-bootstrap:

$ git clone https://github.com/justinethier/cyclone-bootstrap.git
$ cd cyclone-bootstrap
$ ./install.sh

Once Cyclone is installed, it can be rebuilt directly from the cyclone repository:

$ make
$ make test
$ sudo make install

Usage

Compiling Scheme Programs

A Scheme program may be compiled using the cyclone command:

$ cyclone  examples/fac.scm
$ examples/fac
3628800

Compiling Scheme Libraries

Scheme code can be organized into libraries that are compiled separately from programs. Cyclone intends a library to represent a single C module (or file) when compiled.

Each library must be placed into a .sld file that corresponds to the library name. For example, the library

(scheme cyclone util) 

would be defined in its .sld file as:

(define-library (scheme cyclone util)
  ... )

and should be located in the file

scheme/cyclone/util.sld

Cyclone will automatically generate libraries when compiling a program. For example:

$ cd cyclone/examples/game-of-life
$ cyclone life.scm
$ ./life

Command Line Options

cyclone has the following command line options:

Option Notes
-A directory Append directory to the list of directories that are searched in order to locate imported libraries.
-I directory Prepend directory to the list of directories that are searched in order to locate imported libraries.
-CP cc-commands Specify a custom command line for the C compiler to compile a program module. See Makefile.config for an example of how to construct such a command line.
-CE cc-commands Specify a custom command line for the C compiler to compile an executable.
-CL cc-commands Specify a custom command line for the C compiler to compile a library module.
-CS cc-commands Specify a custom command line for the C compiler to compile a shared object module.
-COPT options Specify custom options to provide to the C compiler, EG: "-Imy-directory".
-CLNK option Specify a custom command to provide as a linker option, EG: “-lcurl”.
-Ox Optimization level, higher means more optimizations will be used. Set to 0 to disable optimizations.
-d Only generate intermediate C files, do not compile them. This option will also show the C compiler commands that would have been used to compile the C file.
-t Show intermediate trace output in generated C files
-h, --help Display usage information
-v Display version information
-vn Display version number
-batch Automatically compile local library dependencies (enabled by default).
-no-batch Compile as a single unit, do not attempt to compile local library dependencies.
-use-unsafe-prims Emit unsafe primitives. These primitives are faster but do not perform runtime type checking or bounds checking.
-no-call-history Do not track call history in the compiled code. This allows for a faster runtime at the cost of having no call history in the event of an exception.

Generated Files

The following files are generated during the Cyclone compilation process:

File Extension Install Required Notes
.meta Yes These text files contain the expanded version of any macros exported by a Scheme library, and allow other modules to easily use those macros during compilation. This file is not generated when compiling a program.
.c   C code file generated by Cyclone.
.o Yes Object file generated by the C compiler from the corresponding .c file.
.so Yes Shared Object files generated by the C compiler from the corresponding .c file. These are only generated for Scheme libraries and are used to allow loading a library at runtime.
(None) Yes Final executable file generated by the C compiler when compiling a program.

When installing a library, all of the files indicated above must be installed as well as the corresponding .sld file.

Interpreter

Scheme code can be evaluated interactively using the icyc command:

    $ icyc
    cyclone> (write 'hello-world)
    hello-world

Language Details

Cyclone implements the Scheme language as documented by the R7RS Scheme Specification.

A R7RS Compliance Chart lists differences between the specification and Cyclone’s implementation.

API Documentation is available for the libraries provided by Cyclone.

Macros

Syntax Rules

High-level hygienic macros may be created using syntax-rules. This system is based on a template language specified by R7RS. The specification goes into more detail on how to work with these macros:

(define-syntax when
  (syntax-rules  ()
    ((when test result1 result2 ...)
     (if test
         (begin result1 result2 ...)))))

Explicit Renaming

Alternatively a low-level explicit renaming (ER) system is provided that allows defining macros using Scheme code, in a similar manner as defmacro.

This macro system provides the convenience functions (rename identifier) to hygienically rename an identifier and (compare identifier1 identifier2) to compare two identifiers:

(define-syntax when
  (er-macro-transformer
    (lambda (exp rename compare)
      (if (null? (cdr exp)) (error/loc "empty when" exp))
      (if (null? (cddr exp)) (error/loc "no when body" exp))
      `(if ,(cadr exp)
           ((lambda () ,@(cddr exp)))))))

Debugging

Multithreaded Programming

Overview

The srfi 18 library may be imported to provide support for multithreaded programs. See the SRFI 18 specification for more background information.

Many helper functions are provided by (cyclone concurrent) to make it easier to write multithreaded programs.

Thread Safety

Cyclone uses a generational garbage collector that automatically move objects from the first generation (on the stack) to the second generation (on the heap). This move is performed by the application thread that originally created the object. Without the proper safety measures in place this could cause problems as the address that another thread is using for an object may suddenly change.

To prevent race conditions Cyclone automatically relocates objects to the heap before they can be accessed by more than one thread. It is still necessary for application code to use the appropriate concurrency constructs - such as locks, atomics, etc - to ensure that an object is safely accessed by only one thread at a time.

Foreign Function Interface

Writing a Scheme Function in C

The define-c special form can be used to define a function containing user-defined C code. This code will be carried through from the Scheme file all the way to the compiled C file. For example:

 (define-c Cyc-add-exception-handler
   "(void *data, int argc, closure _, object k, object h)"
   " gc_thread_data *thd = (gc_thread_data *)data;
     make_pair(c, h, thd->exception_handler_stack);
     thd->exception_handler_stack = &c;
     return_closcall1(data, k, &c); ")

The arguments to define-c are:

Functions that may block must call the set_thread_blocked macro to let the system know the thread may block. After the blocking section is finished, the return_thread_runnable macro must be called to recover from the blocking operation and call into the current continuation. For example:

object Cyc_mutex_lock(void *data, object cont, object obj) {
  mutex m = (mutex) obj;
  Cyc_check_mutex(data, obj);
  set_thread_blocked(data, cont);
  if (pthread_mutex_lock(&(m->lock)) != 0) {
    fprintf(stderr, "Error locking mutex\n");
    exit(1);
  }
  return_thread_runnable(data, boolean_t);
}

The Cyclone runtime can be used as a reference for how to write your own C functions. A good starting point would be runtime.c and types.h.

Foreign Library

The (cyclone foreign) provides a convenient interface for integrating with C code, and provides higher-level constructs than define-c. See the API documentation for more information.

Including a C Header File

A C header may be included using the include-c-header special form. This special form may be used either as part of a library definition:

(define-library (example life)
  (include-c-header "../write-png.h")
  (export life)
  ... )

Or as part of a program (add any includes immediately after the import expression, if one is present):

(import (scheme base)
        (example life)
        (example grid))
(include-c-header "stdlib.h")
(include-c-header "<stdio.h>")

By default this will generate an #include preprocessor directive with the name of the header file in double quotes. However, if include-c-header is passed a text string with angle brackets (EG: "<stdio.h>"), the generated C code will use angle brackets instead.

C Compiler Options

A Cyclone library may use the `c-compiler-options expression to pass options directly to the C compiler. For example:

(define-library (my-lib)
  (c-compiler-options "-Imy-dir/include")
  ...

This expression may also be used at the top level of a program, EG:

(c-compiler-options "-Imy-dir/include")

Linking to a C Library

A Cyclone library may use the c-linker-options expression to instruct the compiler to include linker options when building an executable. For example:

(define-library (cyclone curl)
  (include-c-header "<curl/curl.h>")
  (export curl-version)
  (c-linker-options "-lcurl")

This expression may also be used at the top level of a program, EG:

(c-linker-options "-lcurl")

Calling Scheme Functions from C

C functions Cyc_scm_call and Cyc_scm_call_no_gc are provided to allow calling Scheme functions from C.

The best way to get started with these functions is with the code examples in the Cyclone source tree under examples/call-scm-from-c.

Licensing

Cyclone is available under the MIT license.

References and Further Reading