Add darwin target support
authorLuke Lau <luke_lau@icloud.com>
Tue, 23 Jul 2019 22:52:21 +0000 (23:52 +0100)
committerLuke Lau <luke_lau@icloud.com>
Tue, 23 Jul 2019 22:52:21 +0000 (23:52 +0100)
codegen.scm
main.scm
tests.scm

index 5303b4975854be55bfc5d36aedcf5e607ebe8db9..04a03fa7af0efa80e0c7f212e988195d4310eeb4 100644 (file)
@@ -1,6 +1,8 @@
 (load "typecheck.scm")
 (load "ast.scm")
 
+(define target 'darwin)
+
 (define (emit . s)
   (begin
     (apply printf s)
@@ -47,6 +49,8 @@
   (emit "not %rax")
   (emit "andq $1, %rax"))
 
+; 'write file handle addr-string num-bytes
+     
 (define (codegen-print x si env)
   (codegen-expr x si env) ; x should be a static-string, producing a label
 
   (emit "not %rcx")      ; -%rcx = strlen + 1
   (emit "dec %rcx")
 
-  (emit "mov %rcx, %rdx") ; number of bytes
-  (emit "mov %rax, %rsi") ; addr of string
-  (emit "mov $1, %rax") ; file handle 1 (stdout)
-  (emit "mov $1, %rdi") ; syscall 1 (write)
+  (case target
+    ('darwin
+     (emit "movq %rax, %rsi") ; string addr
+     (emit "movq %rcx, %rdx") ; num bytes
+     (emit "movq $1, %rdi")   ; file handle (stdout)
+     (emit "movq $0x2000004, %rax")) ; syscall 4 (write)
+    ('linux
+     (emit "mov %rax, %rsi")  ; string addr
+     (emit "mov %rcx, %rdx")  ; num bytes
+     (emit "mov $1, %rax")    ; file handle (stdout)
+     (emit "mov $1, %rdi"))) ; syscall 1 (write)
   (emit "syscall"))
 
 (define (range s n)
     (emit "pop %rbp") ; restore caller's base pointer
     (emit "ret")))
 
+(define (codegen-string label)
+  (case target
+    ('darwin (emit "movq ~a@GOTPCREL(%rip), %rax" label))
+    ('linux  (emit "movq $~a, %rax" label))))
+
 (define cur-label 0)
 (define (fresh-label)
   (set! cur-label (+ 1 cur-label))
     ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
     ('int-literal (emit "movq $~a, %rax" e))
     
-    ('static-string (emit "movq $~a, %rax" (cadr e))) ; move label
+    ('static-string (codegen-string (cadr e)))
 
     (else (error #f "don't know how to codegen this"))))
 
 
                                        ; exit syscall
     (emit "mov %rax, %rdi")
-    (emit "mov $60, %rax")
+    (case target
+      ('darwin (emit "movq $0x2000001, %rax"))
+      ('linux (emit "mov $60, %rax")))
     (emit "syscall")
 
     (emit "\t.data")
 
     (for-each codegen-string-data strings)))
 
-(define (compile-to-binary program output)
+(define (compile-to-binary program output t)
+  (set! target t)
   (when (not (eq? (typecheck program) 'int)) (error #f "not an int"))
   (let ([tmp-path "/tmp/a.s"])
     (when (file-exists? tmp-path) (delete-file tmp-path))
     (with-output-to-file tmp-path
       (lambda () (codegen program)))
-    (system (format "clang -nostdlib /tmp/a.s -o ~a" output))))
+
+    (case target
+      ('darwin
+       (system "as /tmp/a.s -o /tmp/a.o")
+       (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
+      ('linux
+       (system "as /tmp/a.s -o /tmp/a.o")
+       (system (format "ld /tmp/a.o -o ~a" output))))))
index 53daa7c73b947bd85035303146778e8dfa3543b6..ce9241ac11537abaf0766e6876e138b24645efd3 100644 (file)
--- a/main.scm
+++ b/main.scm
@@ -1,7 +1,34 @@
 (load "codegen.scm")
 
+; returns (os filename)
+(define (parse-args)
+  (define (parse-os x)
+    (if (null? x) 'darwin ; todo: replace this with the os
+                                       ; it was compiled with
+       (if
+        (or (equal? (car x) "-t")
+            (equal? (car x) "--target"))
+        (case (cadr x)
+          ("darwin" 'darwin)
+          ("linux"  'linux)
+          (else (error #f "unknown os")))         
+        (parse-os (cdr x)))))
+  (define (parse-file x)
+    (if (null? x) 'stdin
+       (if
+        (or (equal? (car x) "-t")
+            (equal? (car x) "--target"))
+        (parse-file (cddr x))
+        (car x))))
+  (let ((args (cdr (command-line))))
+    (list (parse-os args) (parse-file args))))
+
+
+(define target (car (parse-args)))
+(define file (cadr (parse-args)))
+
 (compile-to-binary
- (if (> (length (command-line)) 1)
-     (call-with-input-file (cadr (command-line)) read)
-     (read))
- "a.out")
+ (if (eqv? file 'stdin)
+     (read)
+     (call-with-input-file file read))
+ "a.out" target)
index bfd9124510826ea685a814248fd084509510b583..5b1b1bfc769b8540f6e1ef0d0ebc4ee7a52a4ebd 100644 (file)
--- a/tests.scm
+++ b/tests.scm
 (define (test-prog prog exit-code)
   (display prog)
   (newline)
-  (compile-to-binary prog "/tmp/test-prog")
+  (compile-to-binary prog "/tmp/test-prog" 'darwin)
   (test (system "/tmp/test-prog") exit-code))
 
 (define (test-prog-stdout prog output)
   (display prog)
   (newline)
-  (compile-to-binary prog "/tmp/test-prog")
+  (compile-to-binary prog "/tmp/test-prog" 'darwin)
   (system "/tmp/test-prog > /tmp/test-output.txt")
   (let ((str (read-file "/tmp/test-output.txt")))
     (test str output)))