Skip to content

Commit 4396f01

Browse files
committed
More work on drawing example and frontend changes.
1 parent 046d299 commit 4396f01

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

examples/sysir/drawing2.janet

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
(defpointer cursor p32)
1616

1717
# Linux syscalls
18-
(defn-syscall brk:p32 12 [amount:uint])
19-
(defn-syscall exit:void 60 [code:int])
20-
(defn-syscall write:void 1 [fd:int data:p32 size:uint])
18+
# (defn-syscall brk:p32 12 [amount:uint])
19+
# (defn-syscall exit:void 60 [code:int])
20+
# (defn-syscall write:void 1 [fd:int data:p32 size:uint])
21+
# (defn-syscall write_string 1 [fd:int data:pointer size:uint])
22+
23+
# External
24+
(defn-external write:void [fd:int mem:pointer size:uint])
25+
(defn-external exit:void [x:int])
26+
(defn-external malloc:p32 [size:uint])
2127

2228
(defsys w32:void [c:cursor x:uint]
2329
(def p:p32 (load c))
@@ -34,10 +40,10 @@
3440

3541
(defsys makebmp:p32 [w:uint h:uint]
3642
(def size:uint (+ 56 (* w h 4)))
37-
(def mem:p32 (brk size))
43+
(def mem:p32 (malloc size))
44+
(def c:cursor (cast (malloc 4)))
3845
#(def cursor_data:p32 mem)
3946
#(def c:cursor (address cursor_data))
40-
(def c:cursor (cast (brk 4)))
4147
(store c mem)
4248
(w16 c 0x4D42) # ascii "BM"
4349
(w32 c size)
@@ -72,18 +78,15 @@
7278
(write 1 mem size)
7379
(return mem))
7480

75-
(defsys _start:void []
81+
(defsys main:int []
7682
(def w:uint 512)
7783
(def h:uint 512)
78-
# (makebmp w h)
79-
(def size:uint (+ 56 (* w h 4)))
80-
(def mem:p32 (brk size))
81-
(store mem (the uint 0x4d424d42))
82-
(write 1 mem 4)
83-
#(write 1 (cast "hello, world!\n") 14)
84-
(exit 0)
85-
(return))
84+
(makebmp w h)
85+
(return 0))
8686

8787
####
8888

89-
(dumpx64)
89+
#(dumpx64)
90+
91+
(print "#include <unistd.h>")
92+
(dumpc)

examples/sysir/frontend.janet

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@
153153
(array/push into ~(pointer-add ,slot ,base-slot ,offset-slot))
154154
slot)
155155

156+
'pointer-sub
157+
(do
158+
(assert (= 2 (length args)))
159+
(def [base offset] args)
160+
(def base-slot (visit1 base into false type-hint))
161+
(def offset-slot (visit1 offset into false 'int))
162+
(def slot (get-slot))
163+
(when type-hint (array/push into ~(bind ,slot ,type-hint)))
164+
(array/push into ~(pointer-subtract ,slot ,base-slot ,offset-slot))
165+
slot)
166+
156167
# Type hinting
157168
'the
158169
(do

examples/sysir/run_drawing.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
valgrind build/janet examples/sysir/drawing.janet > temp.nasm
3-
nasm -felf64 temp.nasm -l temp.lst -o temp.o
4-
ld -o temp.bin -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc temp.o
5-
valgrind ./temp.bin
2+
valgrind build/janet examples/sysir/drawing.janet > temp.c
3+
cc temp.c
4+
./a.out > temp.bmp
5+
feh temp.bmp

src/core/sysir_x86.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ static void sysemit_sysv_call(JanetSysx64Context *ctx, JanetSysInstruction instr
682682
janet_formatb(buffer, "call ");
683683
sysemit_operand(ctx, instruction.call.callee, "\n");
684684
}
685-
if (instruction.call.flags & JANET_SYS_CALLFLAG_HAS_DEST) sysemit_movreg(ctx, RAX, instruction.call.dest);
686685
if (save_r11) sysemit_popreg(ctx, 11);
687686
if (save_r10) sysemit_popreg(ctx, 10);
688687
if (save_r9) sysemit_popreg(ctx, 9);
@@ -691,6 +690,7 @@ static void sysemit_sysv_call(JanetSysx64Context *ctx, JanetSysInstruction instr
691690
if (save_rdx) sysemit_popreg(ctx, RDX);
692691
if (save_rsi) sysemit_popreg(ctx, RSI);
693692
if (save_rdi) sysemit_popreg(ctx, RDI);
693+
if (instruction.call.flags & JANET_SYS_CALLFLAG_HAS_DEST) sysemit_movfromreg(ctx, instruction.call.dest, RAX);
694694
}
695695

696696
static void sysemit_win64_call(JanetSysx64Context *ctx, JanetSysInstruction instruction, uint32_t *args, uint32_t argcount) {
@@ -726,13 +726,13 @@ static void sysemit_win64_call(JanetSysx64Context *ctx, JanetSysInstruction inst
726726
if (argcount > 4) {
727727
janet_formatb(buffer, "add rsp, %u\n", 8 * (argcount - 4));
728728
}
729-
if (instruction.call.flags & JANET_SYS_CALLFLAG_HAS_DEST) sysemit_movreg(ctx, RAX, instruction.call.dest);
730729
if (save_r11) sysemit_popreg(ctx, 11);
731730
if (save_r10) sysemit_popreg(ctx, 10);
732731
if (save_r9) sysemit_popreg(ctx, 9);
733732
if (save_r8) sysemit_popreg(ctx, 8);
734733
if (save_rdx) sysemit_popreg(ctx, RDX);
735734
if (save_rcx) sysemit_popreg(ctx, RCX);
735+
if (instruction.call.flags & JANET_SYS_CALLFLAG_HAS_DEST) sysemit_movfromreg(ctx, instruction.call.dest, RAX);
736736
}
737737

738738
void janet_sys_ir_lower_to_x64(JanetSysIRLinkage *linkage, JanetSysTarget target, JanetBuffer *buffer) {

0 commit comments

Comments
 (0)