SECD: A reduced version.
#lang secd | package: secd |
1 Introduction
This is an implementation of a compiler for a very small subset of the SECD language. The programs written in this language will be compiled to ARM Assembler. The resulting program will run the instructions and print the result in the standard output.
As an example. If the source SECD program is:
#lang secd |
( |
(INT_CONST 56) |
(FUN (IF0 ((FUN (INT_CONST 4) (ADD) (RETURN)) (INT_CONST 3) (APPLY)) |
((FUN (INT_CONST 8) (ADD) (RETURN)) (INT_CONST 6) (APPLY))) |
(RETURN)) |
(INT_CONST 0) |
(APPLY) |
(ADD)) |
The resulting ARM Assembler program will be:
.data |
fstr: .asciz "%d\n" |
|
.text |
.global main |
.extern printf |
|
fun0: |
stmfd sp!, {fp, ip, lr} |
mov fp, sp |
|
stmfd sp!, {r0} |
|
ldr r0, =4 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0, r1} |
add r0, r1 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0} |
|
mov sp, fp |
ldmfd sp!, {fp, ip, pc} |
|
fun1: |
stmfd sp!, {fp, ip, lr} |
mov fp, sp |
|
stmfd sp!, {r0} |
|
ldr r0, =8 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0, r1} |
add r0, r1 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0} |
|
mov sp, fp |
ldmfd sp!, {fp, ip, pc} |
|
fun2: |
stmfd sp!, {fp, ip, lr} |
mov fp, sp |
|
stmfd sp!, {r0} |
|
ldmfd sp!, {r0} |
tst r0, r0 |
bne ifnz0 |
|
ldr r0, =fun0 |
stmfd sp!, {r0} |
|
ldr r0, =3 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0, r7} |
blx r7 |
stmfd sp!, {r0} |
|
b endifz0 |
|
ifnz0: |
|
ldr r0, =fun1 |
stmfd sp!, {r0} |
|
ldr r0, =6 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0, r7} |
blx r7 |
stmfd sp!, {r0} |
|
endifz0: |
|
ldmfd sp!, {r0} |
|
mov sp, fp |
ldmfd sp!, {fp, ip, pc} |
|
main: |
|
stmfd sp!, {fp, ip, lr} |
mov fp, sp |
|
ldr r0, =56 |
stmfd sp!, {r0} |
|
ldr r0, =fun2 |
stmfd sp!, {r0} |
|
ldr r0, =0 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0, r7} |
blx r7 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r0, r1} |
add r0, r1 |
stmfd sp!, {r0} |
|
ldmfd sp!, {r1} |
ldr r0, =fstr |
bl printf |
|
mov sp, fp |
ldmfd sp!, {fp, ip, pc} |
$ gcc -c program.s -o program.o |
$ gcc program.o -o program |
$ ./program |
63 |
2 Instruction Set
As explained in the introduction, this implementation provides only a small subset of the instructions available in the SECD language. Moreover, the names of the instructions differ from the real ones. Only the following instructions are available in the current release. This instruction set will be expanded in the near future.
secd-instruction
instruction : secd-instruction? return : secd-return?
secd-instruction
trueInstrs : secd-instruction-list? falseInstrs : secd-instruction-list?