简洁实现类似 Lisp 的语言,适用于低端和嵌入式设备
-- indented-text --
Introduction
============Hedgehog is a very concise implementation of a LISP-like language for low-end and embedded devices, primarily for machine-to-machine systems. It consists of a compiler to byte code and a corresponding interpreter. The byte code interpreter is written in standard conforming C, is efficient and easily portable, and can be compiled to a very small executable or library of only some 20-30 kilobytes.
The Hedgehog LISP dialect has proper support for local and lambda functions, lexical scoping, variable argument functions, garbage collection, exceptions, macros, and over a hundred predefined functions or special forms. The built-in types are lists, symbols, strings, 32-bit integers, AVL-trees, and tuples up to 16 elements wide. Proper 32-bit wide integers are necessary for various bit-level operations in embedded systems.
The Hedgehog compiler and byte code interpreter, essentially all code written in C, is distributed under the "Lesser" GNU General Public License (GPL). In layman's terms, you have to ship the source code of the Hedgehog compiler and interpreter whenever you ship the corresponding binary executable. However, all files written in Hedgehog Lisp itself, including the standard library in the prelude.d directory, are distributed the revised BSD license, which allows them to be included also in programs that are distributed without full source code. See the files LICENSE.LGPL and LICENSE.BSD.
This public version of the lisp compiler is routinely exercised on Linux and Cygwin, it has been briefly tested on FreeBSD and SunOS, and should be easily portable to any Posix system. The interpreter part has additionally been ported to several embedded operating systems. Support, alternative licenses, and ports can be discussed with Oliotalo Ltd. The ports have support for various protocols and devices specific for that platform.
For further information or to download, see http://hedgehog.oliotalo.fi/ or mail [email protected].
Authors: Kenneth Oksanen Lars Wirzenius
Version-specific Note
=====================This series of versions, hedgehog-2.*, has been extracted and cleaned up from a significantly more featureful hedgehog-1.6, which is used by Oliotalo in several embedded machine-to-machine solutions. While many parts of the code are mature and well-tested, this version undoubtedly contains also immature and incorrect pieces of code. Also some parts are missing, such as a few good demo applications and various features planned for the state machine library (which hedgehog-1.6 has but we wish to improve for 2.0). Therefore we consider this as an beta-quality at best. But we will gradually improve.
Running HedgeHog
================To compile and install Hedgehog on a typical Linux installation,
unpack the sources and change to the source directory and then do the
following:
mkdir o
cd o
../configure linux /usr/local
make
where make' refers to GNU make. This builds to executables the lisp compiler hhc' and the byte code interpreter hhi', and some lisp library files in the directory prelude.d'. Hedgehog has been
compiled and tested on a number of other platforms, including FreeBSD
and SunOS, but you will need GNU make and in many cases, especially if
you wish to edit the develop the code further, also the GNU awk and
lex replacements gawk and flex.
Assuming the above completed successfully, you should now be able to
compile tests in the tests'-directory, for example by issuing ./hhc -g -p prelude.d ../tests/fib.hl in order to compile the ubiquitous Fibonacci-test. This produces two output files, fib.hlo' and fib.hls'. The latter is a symbolic assembler file and used mostly for debugging after a possible error reported by the interpreter. It is particularly useful if you didn't pass hhc' the flag -g' telling it to add a little debugging information to the fib.hlo'.
The former is the actual byte code program, augmented with a small header and the program's constant data. You should now be able to run the compiled Fibonacci-test in `fib.hlo' by by issuing ./hhi fib.hlo Depending on your machine's speed this will last a few seconds, but eventually you should see the result: fib(30) = 1346269 or fib(22) = 28657 depending on which distribution you have.
The flag -p prelude.d' you passed to hhc' tells it to use the lisp
library in the subdirectory prelude.d'. Conventionally you should make install with sufficient priviledges. This places hhc' and hhi' in /usr/local/bin' and the standard library /usr/local/lib/hh', as you had instructed configure' to do. Now that the prelude is in its
predefined place, you can refrain from passing -p prelude.d' to hhc'.
Pass hhc' and hhc' the flag --help' or -h' to see their full
command line interface. Note that the heap and stack sizes are fixed
during each execution, but are adjustable from the command line.
The prelude
===========The Hedgehog standard library, or prelude, consists of all the files
in the prelude.d' directory that have a .hl' suffix. The filenames
are expected to be sorted according to ASCII character codes (in the
"C" locale, that is: some other locales have weird rules for sorting,
e.g., the fi_FI one). The naming convention is, therefore, to begin
each filename with three digits and a dash. This will allow ordering
between the files.
XXX Here comes a little tour of the prelude.
Porting and Adaptation
======================XXX Design and write a guide.
If memcmp performs signed comparison of bytes on your platform, you must define HH_MEMCMP to some function which performs unsigned comparison. This is necessary for Lisp-level string comparisons and compression to work correctly.
The macros HH_UNIX, HH_SUNOS, HH_BSD, HH_LINUX etc. refer to the target platform where we run hhi, but not necessarily to hhc.
But while nothing is written yet, a few words of advice:
If your target looks like unix:
make' (cross-)compiles and (cross-)executes a small C program which digs out various flag values and struct layouts of the system call interface into the prelude file 300-unix.hl'.
Ensure the cross-execution succeeded.If your target does not look like unix:
You have to write a new main program file. You can take `hh_interp_unix.c' as your starting point.
Search for occurrences of HH_UNIX in the code (including the compiler), and adapt the code for your target in similar respects.
The byte code instruction set is designed to be concise, allow a small implementation of the interpreter, but still allow a reasonably efficient implementation.
The instructions are of variable instruction length. The uppermost two bits in the first byte of the instruction tell the byte code type. Bits 00 indicate an instruction without an immediate field, bits 01 indicate that the second byte of the instruction is a one-byte signed immediate field. Bits 10 indicate a two-byte immediate value and bits 11 indicate a four-byte immediate value. A handful of instructions use more than a single immediate field.
The lowermost six bits of the first byte of each instruction indicate
the mnemonic, such as push' or load_imm'. The byte code
instructions with an immediate field and the ones without are
separate. This means there are at most 64 instructions with an
immediate field and 64 instructions without. The latter is often too
few, and therefore rarely used non-immediate instructions are encoded
as "ext insns" a special immediate instruction plus an immediate field
that specifies the instructions. All instructions are defined in the
file `hh_insn.def', which with some C macro trickery is included in
several places for various purposes.
Some instructions, such load_imm' or pick', have variants which
first push the value of accu to stack before actual computation. This
both reduces byte code program lengths and improves performance
because of reduced instruction dispatching. For some other
instructions, for example for jumps and many non-atomic expression,
such variants would be rarely used, and are therefore omitted in order
to save instruction space.
Virtual Machine
---------------The virtual machine is a stack machine but so that the least recently
computed value cached in a special "register" accu'. The benefit of accu' is twofold: Firstly, if the result is not used, there is no
need to pop it off the stack. Secondly, `accu' reduces the traffic to
and from the stack, thereby somewhat improving the performance.
The stack pointer `sp' points to the position in stack where the next pushed value goes to. The stack grows upward.
A function is a tuple whose first word refers to the beginning of the function's byte code and the second word refers to the function's environment, or HH_NIL (i.e., the Lisp nil) if there is only the global environment. The first byte code instruction of each function is actually not an instruction at all, but a byte of information of the arity of the function. The seven lowermost bits tell the number of arguments the function expects, and if the uppermost bit is set, then the function is a variable argument function.
The same stack is used to store arguments to both byte code
instructions, builtin functions and user-defined functions.
Let's follow through a proper function call. The caller evaluates all
subexpressions in the s-expression from left (the called function) to
right, pushing all but the last one which is left in accu'. Now it issues the byte code instruction call' with the immediate value
giving the number of arguments in the call. This instruction does all
of the following:
return n' which pops n' values off the stack to find and resume the
caller's stored program counter.Dynamic extent is achieved by creating small tuples, called
"environments" or "closures", which contain exactly those variables
which do not by their nature have infinite lifetime (such as global
variables) and are referred to from local "lambda" functions created
by fn's or def's. For example
(def (mkf2 x)
(fn (y) (+ y x)))
is automatically turned into something like
(def (mkf1 x)
(.make_new_env 2)
(.put_new_env 0 x)
(.bind_env $.0))
(def ($.0 y)
(+ y (.get_env 0)))
The $.0' is an automatically generated symbol for the local fn'
lifted to the top-level. .bind_env' leaves to accu' the function
$.0' bound to the created environment, which contains the value of variable x', which the local function refers to.
A callable function is a cons-cell, whose CAR-slot contains a program counter and CDR-slot contains the environment tuple, or nil if the function does not refer to local variables defined outside of it. Calling a function always reads the environment bound to that function, but otherwise environment pointers are pushed and restored from the sta
暂无开放 Issues,或尚未同步最近议题。