]> Witch of Git - ivy/blob - docs/example.s
Add memory layout docs and assembly experiments
[ivy] / docs / example.s
1 // clang -masm=intel -nostdlib docs/example.s -lSystem -Wl,-e,_start -g -o target/example
2
3 SYS_OFFSET = 0x2000000
4 #define SYS(X) (SYS_OFFSET + X)
5 SYS_EXIT = SYS(0x01)
6 SYS_WRITE = SYS(0x04)
7 SYS_MUNMAP = SYS(0x49)
8 SYS_MMAP = SYS(0xC5)
9
10 PROT_READ = 0x1
11 PROT_WRITE = 0x2
12 MAP_ANONYMOUS = 0x1000
13 MAP_SHARED = 0x1
14 MAP_PRIVATE = 0x2
15
16 STDOUT = 1
17 STDERR = 1
18
19 // args: RDI RSI RDX RCX R8 R9
20
21 .text
22 .global _start
23 _start:
24 // 16-byte align the stack
25 sub rsp, 16
26 and spl, 0xf0
27
28 mov rdi, 413
29 call ivy_alloc
30 mov rdi, rax
31 call _free
32 mov rdi, 0
33 call _exit
34
35
36 ivy_alloc:
37 call _malloc
38 ret
39
40 ivy_dealloc:
41 call _free
42 ret
43
44
45 ivy_check_int:
46 test rdi, 1
47 jz ivy_check_ok
48 cmp qword ptr [rdi], 1
49 jne ivy_check_int_fail
50 jmp ivy_check_ok
51 ivy_check_lam:
52 test rdi, 1
53 jnz ivy_check_lam_fail
54 cmp qword ptr [rdi], 0
55 jne ivy_check_lam_fail
56 ivy_check_ok:
57 ret
58 ivy_check_int_fail:
59 mov rsi, [rip + EXPECT_INT_ERROR]
60 mov rdx, [rip + EXPECT_INT_ERROR_LEN]
61 jmp ivy_abort
62 ivy_check_lam_fail:
63 mov rsi, [rip + EXPECT_LAM_ERROR]
64 mov rdx, [rip + EXPECT_LAM_ERROR_LEN]
65 jmp ivy_abort
66
67
68 EXPECT_INT_ERROR: .ascii "Error: typecheck expected an integer type"
69 EXPECT_INT_ERROR_LEN: .quad . - EXPECT_INT_ERROR
70 EXPECT_LAM_ERROR: .ascii "Error: typecheck expected a lambda type"
71 EXPECT_LAM_ERROR_LEN: .quad . - EXPECT_LAM_ERROR
72
73
74 ivy_abort:
75 mov rdi, STDERR
76 call _write
77 mov rdi, 1
78 call _exit
79
80
81 ivy_debug_log:
82 push rax
83 mov rsi, rsp
84 sub rsp, 1
85 mov rdx, '\n'
86 mov [rsp], dl
87
88 mov rdx, 0
89 ivy_debug_log_loop:
90 sub rsp, 1
91 mov rbx, rax
92 and rbx, 0xF
93 add rbx, '0'
94 mov [rsp], bl
95 cmp rbx, '9'
96 jle ivy_debug_skip
97 add rbx, 'A' - '0' - 10
98 mov [rsp], bl
99 ivy_debug_skip:
100 shr rax, 4
101 add rdx, 1
102 cmp rdx, 16
103 jl ivy_debug_log_loop
104
105 mov rdi, STDOUT
106 mov rsi, rsp
107 mov rdx, 17
108 call _write
109 add rsp, 17
110 pop rax
111 ret