Selaa lähdekoodia

Compile something

It's just an integer, but it actually is a number, and it exports and
builds and runs properly...  That's... more than I knew before!

!!!!
Lucas Stadler 8 vuotta sitten
vanhempi
commit
9888bcefc8
5 muutettua tiedostoa jossa 43 lisäystä ja 0 poistoa
  1. 2 0
      scm/inc/.gitignore
  2. 9 0
      scm/inc/Makefile
  3. 17 0
      scm/inc/compiler.scm
  4. 10 0
      scm/inc/driver.c
  5. 5 0
      scm/inc/scheme.s

+ 2 - 0
scm/inc/.gitignore

@ -0,0 +1,2 @@
1
scheme
2
integers.s

+ 9 - 0
scm/inc/Makefile

@ -1,2 +1,11 @@
1
run-scheme: scheme
2
	./scheme
3
4
scheme: scheme.s driver.c
5
	gcc -O3 scheme.s driver.c -o $@
6
7
scheme.s: compiler.scm
8
	petite --script compiler.scm > $@
9
1 10
integers.s: integers.c
2 11
	gcc -O3 --omit-frame-pointer -S $<

+ 17 - 0
scm/inc/compiler.scm

@ -0,0 +1,17 @@
1
(define (emit1 instr arg)
2
  (display "\t")
3
  (display (format instr arg))
4
  (display "\n"))
5
6
(define (emit0 instr)
7
  (display "\t")
8
  (display instr)
9
  (display "\n"))
10
11
(define (compile-program x)
12
  (display "\t.globl scheme_entry\n\n")
13
  (display "scheme_entry:\n")
14
  (emit1 "movl $~a, %eax" x)
15
  (emit0 "ret"))
16
17
(compile-program 42)

+ 10 - 0
scm/inc/driver.c

@ -0,0 +1,10 @@
1
/* a simple driver for scheme_entry */
2
3
#include <stdio.h>
4
5
int scheme_entry();
6
7
int main(int argc, char **argv) {
8
	printf("%d\n", scheme_entry());
9
	return 0;
10
}

+ 5 - 0
scm/inc/scheme.s

@ -0,0 +1,5 @@
1
	.globl scheme_entry
2
3
scheme_entry:
4
	movl $42, %eax
5
	ret