aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-08-20 11:57:02 +0200
committertomsmeding <tom.smeding@gmail.com>2017-08-20 11:57:02 +0200
commitf8d264f2b18fccdc3b96d8fb66656128a25137f2 (patch)
treec4da71e1b7bed93b9f2728503212b6fc43cc9e32
parent965f8bf85d7850be074bad735d815b15a85a3de0 (diff)
Separate lib into object file
-rw-r--r--.gitignore1
-rw-r--r--CodeGen.hs7
-rw-r--r--Main.hs2
-rw-r--r--Makefile5
-rw-r--r--liblang.asm (renamed from prologue.asm)27
5 files changed, 27 insertions, 15 deletions
diff --git a/.gitignore b/.gitignore
index f220a21..d197ab1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ main
obj
obsolete
z_output*
+liblang.o
diff --git a/CodeGen.hs b/CodeGen.hs
index 774696f..764a900 100644
--- a/CodeGen.hs
+++ b/CodeGen.hs
@@ -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
diff --git a/Main.hs b/Main.hs
index 4c0f68d..dd07952 100644
--- a/Main.hs
+++ b/Main.hs
@@ -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"
diff --git a/Makefile b/Makefile
index 3ce6d7e..0c27b69 100644
--- a/Makefile
+++ b/Makefile
@@ -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