1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-31 23:53:02 +00:00

More work on x86 backend.

This commit is contained in:
Calvin Rose
2024-11-26 11:18:46 -06:00
parent 9fa9286fca
commit 046d299d77
4 changed files with 247 additions and 34 deletions

View File

@@ -7,13 +7,17 @@
(use ./frontend)
(setdyn :verbose true)
# Pointer types
(defpointer p32 uint)
(defpointer p16 u16)
(defpointer cursor p32)
(defn-external write:void [fd:int mem:pointer size:uint])
(defn-external exit:void [x:int])
(defn-external malloc:p32 [x:uint])
(defn-external free:void [m:p32])
# Linux syscalls
(defn-syscall brk:p32 12 [amount:uint])
(defn-syscall exit:void 60 [code:int])
(defn-syscall write:void 1 [fd:int data:p32 size:uint])
(defsys w32:void [c:cursor x:uint]
(def p:p32 (load c))
@@ -30,9 +34,11 @@
(defsys makebmp:p32 [w:uint h:uint]
(def size:uint (+ 56 (* w h 4)))
(def mem:p32 (malloc size))
(def cursor_data:p32 mem)
(def c:cursor (address cursor_data))
(def mem:p32 (brk size))
#(def cursor_data:p32 mem)
#(def c:cursor (address cursor_data))
(def c:cursor (cast (brk 4)))
(store c mem)
(w16 c 0x4D42) # ascii "BM"
(w32 c size)
(w32 c 0)
@@ -52,27 +58,32 @@
# Draw
(def red:uint 0xFFFF0000)
(def blue:uint 0xFF0000FF)
(def green:uint 0xFF00FF00)
(var y:uint 0)
(while (< y h)
(var x:uint 0)
(while (< x w)
(if (> y 64)
(w32 c blue)
(def d2:uint (+ (* x x) (* y y)))
(if (> d2 100000)
(if (> d2 200000) (w32 c green) (w32 c blue))
(w32 c red))
(set x (+ 1 x)))
(set y (+ y 1)))
(write 1 mem size)
(return mem))
(defsys main:int []
(def w:uint 128)
(defsys _start:void []
(def w:uint 512)
(def h:uint 512)
(makebmp w h)
(return 0))
# (makebmp w h)
(def size:uint (+ 56 (* w h 4)))
(def mem:p32 (brk size))
(store mem (the uint 0x4d424d42))
(write 1 mem 4)
#(write 1 (cast "hello, world!\n") 14)
(exit 0)
(return))
####
#(dump)
(print "#include <unistd.h>")
(dumpc)
#(dumpx64)
(dumpx64)

View File

@@ -12,6 +12,7 @@
(def slot-types @{})
(def functions @{})
(def type-fields @{})
(def syscalls @{})
(defn get-slot
[&opt new-name]
@@ -317,20 +318,11 @@
(array/push into ;args)
nil)
# Syscall
'syscall
(do
(def slots @[])
(def ret (if no-return nil (get-slot)))
(each arg args
(array/push slots (visit1 arg into)))
(array/push into ~(syscall :default ,ret ,;slots))
ret)
# Assume function call
# Assume function call or syscall
(do
(def slots @[])
(def signature (get functions op))
(def is-syscall (get syscalls op))
(assert signature (string "unknown function " op))
(def ret (if no-return nil (get-slot)))
(when ret
@@ -338,7 +330,9 @@
(assign-type ret (first signature)))
(each [arg-type arg] (map tuple (drop 1 signature) args)
(array/push slots (visit1 arg into false arg-type)))
(array/push into ~(call :default ,ret [pointer ,op] ,;slots))
(if is-syscall
(array/push into ~(syscall :default ,ret (int ,is-syscall) ,;slots))
(array/push into ~(call :default ,ret [pointer ,op] ,;slots)))
ret)))
(errorf "cannot compile %q" code)))
@@ -478,6 +472,20 @@
(array/push signature tp))
(put functions fn-name (freeze signature)))
# External syscall
'defn-syscall
(do
(def [name sysnum args] rest)
(assert (tuple? args))
(def [fn-name fn-tp] (type-extract name 'void))
(def pcount (length args)) #TODO - more complicated signatures
(def signature @[fn-tp])
(each arg args
(def [name tp] (type-extract arg 'int))
(array/push signature tp))
(put syscalls fn-name sysnum)
(put functions fn-name (freeze signature)))
# Top level function definition
'defn
(do
@@ -544,4 +552,5 @@
(defmacro defarray [& args] [compile1 ~',(keep-syntax! (dyn *macro-form*) ~(defarray ,;args))])
(defmacro defpointer [& args] [compile1 ~',(keep-syntax! (dyn *macro-form*) ~(defpointer ,;args))])
(defmacro defn-external [& args] [compile1 ~',(keep-syntax! (dyn *macro-form*) ~(defn-external ,;args))])
(defmacro defn-syscall [& args] [compile1 ~',(keep-syntax! (dyn *macro-form*) ~(defn-syscall ,;args))])
(defmacro defsys [& args] [compile1 ~',(keep-syntax! (dyn *macro-form*) ~(defn ,;args))])

5
examples/sysir/run_drawing2.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
valgrind build/janet examples/sysir/drawing2.janet > temp.nasm
nasm -felf64 temp.nasm -l temp.lst -o temp.o
ld -o temp.bin -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc temp.o
valgrind ./temp.bin