initial commit: a mess of assembly code
[fmap.git] / x86_64_sse2_x87 / pbf / features / motorway.s
blob06fda65bd83e089cace39c90a4f462106eb8e70f
1 ; vim: set filetype=fasm foldmethod=marker commentstring=;%s colorcolumn=101 :
2 motorway: namespace motorway
3 ; input:
4 ; rdi = tile_p
5 ; rsi = offset of the way in the tile db
6 ; rdx = node_p of the one provoking this very tile addition, only used if initing required
7 align_nops
8 import:
9 define tile_p rbx
10 define rbx_save rsp + 8 * 0
11 define r12_save rsp + 8 * 1
12 define r13_save rsp + 8 * 2
13 define motorway_of rsp + 8 * 3
14 enter 8 * 4, 0
15 and rsp, not 0xf ; align stack for external calls
16 mov [rbx_save], rbx
18 mov tile_p, rdi
19 mov [motorway_of], rsi
21 bt qword [tile_p + tile_t.flags], tile_t.flags.motorways_inited
22 jnc motorways_init
23 mov rdi, qword [tile_p + tile_t.features.motorways.fd]
24 motorways_inited:
25 ; write the offset into the array of the ways in the tile motorways ------------------------
26 lea rsi, [motorway_of]
27 mov rdx, 8
28 mov rax, linux.write
29 syscall
30 cmp rax, linux.errno.last
31 jae err_write_motorway_of_failed
32 stc
33 epilog:
34 mov rbx, qword [rbx_save]
35 leave
36 ret
37 ;-------------------------------------------------------------------------------------------
38 ; we presume created directories
39 define node_p r12 ; r12 due te re-using tile init code
40 define path_finish r13 ; r13 due to re-using tile init code
41 motorways_init:
42 ; build the path re-using tile init code ---------------------------------------------------
43 mov [r12_save], r12
44 mov [r13_save], r13
45 mov node_p, rdx
46 call tile.init.xy_to_str ; we share r12 and r13
47 call tile.init.path_tile_dir_do ; we share r12 and r13
48 mov r12, qword [r12_save]
49 purge node_p
50 mov rax, '/motorwa'
51 mov qword [path_finish], rax
52 mov eax, 'ys'
53 mov dword [path_finish + 8], eax ; 0 terminator added
54 mov r13, qword [r13_save]
55 purge path_finish
56 ; get a fd ---------------------------------------------------------------------------------
57 mov rax, [ctx]
58 mov rdi, [rax + ctx_t.tile_db_dir.fd]
59 lea rsi, [tile.init.path]
60 mov rdx, linux.O_CREAT or linux.O_TRUNC or linux.O_WRONLY
61 mov r10, 110110110b ; RW for all but umask
62 mov rax, linux.openat
63 syscall
64 cmp rax, linux.errno.last
65 jae err_motorways_openat_failed
66 mov [tile_p + tile_t.features.motorways.fd], rax
67 mov rdi, rax ; caller expect the fd being in rdi as well
69 or qword [tile_p + tile_t.flags], 1 shl tile_t.flags.motorways_inited
70 jmp motorways_inited
71 ;-------------------------------------------------------------------------------------------
72 err_motorways_openat_failed:
73 mov rdi, 2
74 lea rsi, [msg.err_motorways_openat_failed]
75 mov rdx, rax
76 lea rcx, [tile.init.path]
77 call qword [libc.dprintf]
78 clc
79 jmp epilog
80 err_write_motorway_of_failed:
81 mov rdi, 2
82 lea rsi, [msg.err_write_motorway_of_failed]
83 mov rdx, rax
84 call qword [libc.dprintf]
85 clc
86 jmp epilog
87 purge tile_p
88 purge rbx_save
89 purge r12_save
90 purge r13_save
91 purge motorway_of
92 end namespace ; motorways