From f8d264f2b18fccdc3b96d8fb66656128a25137f2 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 20 Aug 2017 11:57:02 +0200 Subject: Separate lib into object file --- .gitignore | 1 + CodeGen.hs | 7 +-- Main.hs | 2 +- Makefile | 5 ++- liblang.asm | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ prologue.asm | 134 -------------------------------------------------------- 6 files changed, 151 insertions(+), 139 deletions(-) create mode 100644 liblang.asm delete mode 100644 prologue.asm 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/liblang.asm b/liblang.asm new file mode 100644 index 0000000..52a68cc --- /dev/null +++ b/liblang.asm @@ -0,0 +1,141 @@ +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, putc, putint, getc, _builtin_malloc +default rel + +extern main + +section .text +start: + call main + mov rdi, rax + mov eax, SYS_EXIT + syscall + jmp $ + +putc: + push rax + push rdi + push rsi + push rdx + push rcx + push r11 + mov eax, SYS_WRITE + mov edi, 1 + lea rsi, [rsp+56] + mov edx, edi + syscall + pop r11 + pop rcx + pop rdx + pop rsi + pop rdi + pop rax + ret + +putint: + push rdi + push rsi + push rax + push rbx + push rcx + push rdx + push r11 + mov rax, [rsp+64] + mov ebx, 18 + test rax, rax + jz .numzero + + mov ecx, 10 + +.strlp: + xor edx, edx + div rcx + add dl, '0' + dec rbx + mov [rsp+rbx], dl + test rax, rax + jnz .strlp + jmp .strdone + +.numzero: + dec rbx + mov byte [rsp+rbx], '0' + jmp .strdone + +.strdone: + mov eax, SYS_WRITE + mov edi, 1 + lea rsi, [rsp+rbx] + mov edx, 18 + sub rdx, rbx + syscall + + pop r11 + pop rdx + pop rcx + pop rbx + pop rax + pop rsi + pop rdi + ret + +getc: + push rdi + push rsi + push rdx + push rcx + push r11 + mov eax, SYS_READ + xor edi, edi + mov rsi, rsp + mov edx, 1 + syscall + cmp rax, 1 + jne .fail + mov rax, [rsp] +.finish: + pop r11 + pop rcx + pop rdx + pop rsi + pop rdi + ret +.fail: + mov rax, -1 + jmp .finish + +_builtin_malloc: + push rdi + push rsi + push rdx + push r8 + push r9 + push r10 + push r11 + push rcx + xor edi, edi + mov rsi, [rsp+72] + mov edx, 0x03 + mov r10d, 0x1001 + mov r8d, -1 + xor r9d, r9d + mov eax, SYS_MMAP + syscall + pop rcx + pop r11 + pop r10 + pop r9 + pop r8 + pop rdx + pop rsi + pop rdi + ret + + +section .data +db 0 ; for dyld diff --git a/prologue.asm b/prologue.asm deleted file mode 100644 index 74d9de3..0000000 --- a/prologue.asm +++ /dev/null @@ -1,134 +0,0 @@ -; SYS_EXIT equ 0x2000001 ;code -; SYS_FORK equ 0x2000002 ;-- -; SYS_READ equ 0x2000003 ;fd, buf, len -; SYS_WRITE equ 0x2000004 ;fd, buf, len - -global start -default rel - -section .text -start: - call main - mov rdi, rax - mov eax, 0x2000001 - syscall - jmp $ - -putc: - push rax - push rdi - push rsi - push rdx - push rcx - push r11 - mov eax, 0x2000004 - mov edi, 1 - lea rsi, [rsp+56] - mov edx, edi - syscall - pop r11 - pop rcx - pop rdx - pop rsi - pop rdi - pop rax - ret - -putint: - push rdi - push rsi - push rax - push rbx - push rcx - push rdx - push r11 - mov rax, [rsp+64] - mov ebx, 18 - test rax, rax - jz .numzero - - mov ecx, 10 - -.strlp: - xor edx, edx - div rcx - add dl, '0' - dec rbx - mov [rsp+rbx], dl - test rax, rax - jnz .strlp - jmp .strdone - -.numzero: - dec rbx - mov byte [rsp+rbx], '0' - jmp .strdone - -.strdone: - mov eax, 0x2000004 - mov edi, 1 - lea rsi, [rsp+rbx] - mov edx, 18 - sub rdx, rbx - syscall - - pop r11 - pop rdx - pop rcx - pop rbx - pop rax - pop rsi - pop rdi - ret - -getc: - push rdi - push rsi - push rdx - push rcx - push r11 - mov eax, 0x2000003 - xor edi, edi - mov rsi, rsp - mov edx, 1 - syscall - cmp rax, 1 - jne .fail - mov rax, [rsp] -.finish: - pop r11 - pop rcx - pop rdx - pop rsi - pop rdi - ret -.fail: - mov rax, -1 - jmp .finish - -_builtin_malloc: - push rdi - push rsi - push rdx - push r8 - push r9 - push r10 - push r11 - push rcx - xor edi, edi - mov rsi, [rsp+72] - mov edx, 0x03 - mov r10d, 0x1001 - mov r8d, -1 - xor r9d, r9d - mov eax, 0x20000C5 - syscall - pop rcx - pop r11 - pop r10 - pop r9 - pop r8 - pop rdx - pop rsi - pop rdi - ret -- cgit v1.2.3-70-g09d2