summaryrefslogtreecommitdiff
path: root/compiler.scm
blob: 919be4cc4bd2ecaf6b364a17cb660cb4b5d8f8e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(use-modules (ice-9 format))

(define (emit template-string . args)
  (apply format #t template-string args)
  (newline))

(define (compile-program x)
  (define fixnum-shift 2)
  (define fixnum-tag 0)
  (define char-shift 8)
  (define char-tag 15)
  (define boolean-shift 7)
  (define boolean-tag 31)
  (define empty-list 47) ;; #b00101111
  (define (immediate-rep x)
    (cond
     ((integer? x)
      (ash x fixnum-shift))
     ((char? x)
      (logior (ash (char->integer x) char-shift) char-tag))
     ((boolean? x)
      (logior (ash (if x 1 0) boolean-shift) boolean-tag))
     ((null? x)
      empty-list)))
  (with-output-to-file "scheme_entry.S"
    (lambda ()
      (display ".text
.p2align 4
.globl	scheme_entry
.type	scheme_entry, @function
scheme_entry:
")
      (emit "movl $~a, %eax" (immediate-rep x))
      (emit "ret"))))