diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CodeGen.hs | 7 | ||||
-rw-r--r-- | Main.hs | 2 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | liblang.asm (renamed from prologue.asm) | 27 |
5 files changed, 27 insertions, 15 deletions
@@ -2,3 +2,4 @@ main obj obsolete z_output* +liblang.o @@ -9,7 +9,6 @@ import Data.List import Data.Maybe import Data.Map.Strict ((!)) import qualified Data.Map.Strict as Map -import Text.Heredoc import Debug.Trace import AST @@ -66,8 +65,10 @@ codegen (IRProgram vars funcs) = do X64.verify x64 varcg <- liftM unlines $ mapM codegenVar vars x64opt <- x64Optimise x64 - return $ [there|prologue.asm|] ++ "\n" ++ X64.stringify x64opt ++ - "\nsection .data\n" ++ (if length vars > 0 then varcg else "db 0 ; keep dyld happy\n") + return $ "extern putc, putint, getc, _builtin_malloc\n" ++ + "global main\ndefault rel\nsection .text\n" ++ + X64.stringify x64opt ++ + "\nsection .data\n" ++ varcg codegenVar :: DVar -> Error String @@ -52,4 +52,4 @@ main = do callCommand "yasm -w+all -fmacho64 z_output.asm -o z_output.o" hPutStrLn stderr "Linking with ld..." - callCommand "ld z_output.o -o z_output" + callCommand "ld z_output.o liblang.o -o z_output" @@ -21,6 +21,9 @@ run: $(TARGET) ./$(TARGET) $(RUNFLAGS) -$(TARGET): $(wildcard *.hs) +$(TARGET): $(wildcard *.hs) liblang.o @mkdir -p obj ghc $(GHCFLAGS) Main.hs -o $@ + +liblang.o: liblang.asm + yasm -w+all -fmacho64 $< -o $@ diff --git a/prologue.asm b/liblang.asm index 74d9de3..52a68cc 100644 --- a/prologue.asm +++ b/liblang.asm @@ -1,16 +1,19 @@ -; SYS_EXIT equ 0x2000001 ;code -; SYS_FORK equ 0x2000002 ;-- -; SYS_READ equ 0x2000003 ;fd, buf, len -; SYS_WRITE equ 0x2000004 ;fd, buf, len +SYS_EXIT equ 0x2000001 ;code +SYS_FORK equ 0x2000002 ;-- +SYS_READ equ 0x2000003 ;fd, buf, len +SYS_WRITE equ 0x2000004 ;fd, buf, len +SYS_MMAP equ 0x20000C5 ;addr, len, prot, flags, fd, offset -global start +global start, putc, putint, getc, _builtin_malloc default rel +extern main + section .text start: call main mov rdi, rax - mov eax, 0x2000001 + mov eax, SYS_EXIT syscall jmp $ @@ -21,7 +24,7 @@ putc: push rdx push rcx push r11 - mov eax, 0x2000004 + mov eax, SYS_WRITE mov edi, 1 lea rsi, [rsp+56] mov edx, edi @@ -65,7 +68,7 @@ putint: jmp .strdone .strdone: - mov eax, 0x2000004 + mov eax, SYS_WRITE mov edi, 1 lea rsi, [rsp+rbx] mov edx, 18 @@ -87,7 +90,7 @@ getc: push rdx push rcx push r11 - mov eax, 0x2000003 + mov eax, SYS_READ xor edi, edi mov rsi, rsp mov edx, 1 @@ -121,7 +124,7 @@ _builtin_malloc: mov r10d, 0x1001 mov r8d, -1 xor r9d, r9d - mov eax, 0x20000C5 + mov eax, SYS_MMAP syscall pop rcx pop r11 @@ -132,3 +135,7 @@ _builtin_malloc: pop rsi pop rdi ret + + +section .data +db 0 ; for dyld |