Working with the compiler

This guide shows how to compile, optimize and deploy code in various Scheme implementations. It does not go in depth into implementation internals and how to optimize code for particular tasks.

CHICKEN Scheme

Overview

The CHICKEN compiler translates Scheme to C, and then calls upon the system C compiler to turn that C into executable files, object files or shared object files.

To build an executable called foo from the file foo.scm:

csc foo.scm

To build a shared object foo.so which can be loaded into a running Scheme program using load:

csc -shared foo.scm

To build a shared object for embedding into a C program:

csc -shared -embedded foo.scm

Shell command

The translator is called chicken, but for most normal uses you want to use the csc frontend which will call the C compiler for you.

Help

csc -help shows usage. There is also a manpage: man csc.

The CHICKEN manual has details.

The CHICKEN mailing list and IRC channel are the best places to ask for help with more difficult problems.

Showing optimized Scheme code

This command will show you the expressions after final analysis, in CPS form:

csc -debug 7 foo.scm

Viewing generated C code

By default, the compiler will delete the intermediate C files it generates. To keep generated files, use -k:

csc -k foo.scm

Showing disassembly of compiled Scheme code

csc foo.scm && objdump -d foo

Gambit Scheme

Overview

The Gambit compiler translates Scheme to C, and then calls upon the system C compiler to turn that C into standard object files (ELF, COFF or Mach-O).

Shell command

The compiler is gsc (also accessible by the alias gsc-script — on some operating systems gsc is GhostScript, not Gambit). gsc is the same program as the interpreter gsi but includes extra libraries that implement the compiler.

Help

gsc -help shows usage.

The Gambit manual has details.

The Gambit mailing list is the best place to ask for help with more difficult problems.

Showing optimized Scheme code

gsc -expansion foo.scm

Showing disassembly of compiled Scheme code

rm -f foo.o1 && gsc foo.scm && objdump -d foo.o1

Caveats

  • gsc generates object files foo.o1, foo.o2, foo.o3, etc. These are ordinary object files like foo.o from the C compiler but have a running number at the end of the filename. Beware of commands like objdump -d foo.o1 that refer to a particular number; it may be an old object file left over from a previous gsc run.

  • If a pre-release version of gsc produces a mysterious failure, try removing all object and executable files generated by previous runs in the same directory and compiling your code from scratch. gsc can use existing files as cached build steps, and things sometimes get out of sync.

Contributing

doc.scheme.org is a community subdomain of scheme.org.