interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tools / sm121dis / sm121dis.om

356 lines · 7375 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note tools/sm121dis/sm121dis.om -- Omega's sm_121 disassembler on the command line (C6 of docs/OMEGA.md).
5note Composed after gpu/nvidia/om/disasm/sm121_dis.om (OM-1, no fn main in decoder).
6note Pure seeded-Omega replacement for tools/sm121dis/sm121dis.c.
7note
8note sm121dis <code.bin> a listing in nvdisasm's layout: the address in a comment, then TEXT ;
9note sm121dis -b SM121 <code.bin> the same, called the way the checks used to call nvdisasm
10note sm121dis --pairs <code.bin> one line per word: <addr> <lo hex> <hi hex> <text> ;
11note sm121dis --version names the tool, for the check logs
12note
13note Exit 0 when every word was a known form, 1 when some were not (printed "?? ..."), 2 for usage/file error.
14
15text cli_t_usage "usage: sm121dis [--pairs | -b SM121] <code.bin> | --version\n"
16text cli_t_verflag "--version"
17text cli_t_verout "sm121dis: Omega's own sm_121 disassembler (gpu/nvidia/om/disasm/sm121_dis.om)\n"
18text cli_t_pairs "--pairs"
19text cli_t_b "-b"
20text cli_t_sm121 "SM121"
21text cli_t_only "sm121dis: only -b SM121\n"
22text cli_t_open ": cannot open file\n"
23text cli_t_oom "sm121dis: out of memory\n"
24text cli_t_mod16 " is not a whole number of 16-byte words\n"
25text cli_t_pfx "sm121dis: "
26text cli_t_long "sm121dis: text too long\n"
27text cli_t_pad8 " /*"
28text cli_t_cend "*/ "
29text cli_t_hex "0123456789abcdef"
30
31global cli_a1 uchar 4096
32global cli_a2 uchar 4096
33global cli_path uchar 4096
34global cli_code uchar *
35global cli_line uchar 512
36global cli_out uchar 65536
37global cli_olen ulong 1
38
39fn cli_wr_all fd int n ulong to bool
40 off = ulong 0
41 while lt off n bound 65536
42 k = write fd cli_out off sub n off
43 if eq k neg long 4
44 continue
45 end
46 if le k 0
47 ret false
48 end
49 off = add off ulong k
50 end
51 ret true
52end
53
54fn cli_flush to void
55 n = get cli_olen 0
56 if gt n 0
57 ok = cli_wr_all 1 n
58 put cli_olen 0 0
59 end
60end
61
62fn cli_oc c uchar to void
63 n = get cli_olen 0
64 if ge n 65536
65 cli_flush
66 n = ulong 0
67 end
68 put cli_out n c
69 put cli_olen 0 add n 1
70end
71
72fn cli_werr_a1 n ulong to void
73 off = ulong 0
74 while lt off n bound 4096
75 k = write 2 cli_path off sub n off
76 if eq k neg long 4
77 continue
78 end
79 if le k 0
80 ret
81 end
82 off = add off ulong k
83 end
84end
85
86fn cli_eq_a1_ver n ulong to bool
87 if ne n len cli_t_verflag
88 ret false
89 end
90 for i ulong len cli_t_verflag
91 if ne get cli_a1 i get cli_t_verflag i
92 ret false
93 end
94 end
95 ret true
96end
97
98fn cli_eq_a1_pairs n ulong to bool
99 if ne n len cli_t_pairs
100 ret false
101 end
102 for i ulong len cli_t_pairs
103 if ne get cli_a1 i get cli_t_pairs i
104 ret false
105 end
106 end
107 ret true
108end
109
110fn cli_eq_a1_b n ulong to bool
111 if ne n len cli_t_b
112 ret false
113 end
114 for i ulong len cli_t_b
115 if ne get cli_a1 i get cli_t_b i
116 ret false
117 end
118 end
119 ret true
120end
121
122fn cli_eq_a2_sm121 n ulong to bool
123 if ne n len cli_t_sm121
124 ret false
125 end
126 for i ulong len cli_t_sm121
127 if ne get cli_a2 i get cli_t_sm121 i
128 ret false
129 end
130 end
131 ret true
132end
133
134fn cli_ohex4 v ulong to void
135 if gt v 65535
136 n = ulong 5
137 for k ulong 11
138 if eq shr v mul n 4 0
139 break
140 end
141 n = add n 1
142 end
143 for k ulong n bound 16
144 sh = mul sub sub n 1 k 4
145 cli_oc get cli_t_hex and shr v sh 15
146 end
147 ret
148 end
149 for k ulong 4
150 sh = mul sub 3 k 4
151 cli_oc get cli_t_hex and shr v sh 15
152 end
153end
154
155fn cli_ohex16 v ulong to void
156 for k ulong 16
157 sh = mul sub 15 k 4
158 cli_oc get cli_t_hex and shr v sh 15
159 end
160end
161
162fn cli_word64 off ulong to ulong
163 v = ulong 0
164 for i ulong 8
165 b = ulong get cli_code add off sub 7 i
166 v = or shl v 8 b
167 end
168 ret v
169end
170
171fn cli_pad_start clen ulong to ulong
172 endp = ulong 0
173 at = ulong 0
174 while le add at 16 clen bound 16777216
175 lo = cli_word64 at
176 if eq and lo 4095 2381
177 endp = add at 16
178 end
179 at = add at 16
180 end
181 ret endp
182end
183
184fn cli_read_file plen ulong to long
185 fd = fopen cli_path 0 plen 0 0
186 if lt fd 0
187 cli_werr_a1 plen
188 k = write 2 cli_t_open 0 len cli_t_open
189 ret neg long 1
190 end
191 cap = ulong 65536
192 eg = enlarge cli_code cap
193 if lt eg 0
194 cl = fclose int fd
195 k = write 2 cli_t_oom 0 len cli_t_oom
196 ret neg long 1
197 end
198 tot = ulong 0
199 while true bound 1048576
200 if eq tot cap
201 ncap = mul cap 2
202 eg2 = enlarge cli_code ncap
203 if lt eg2 0
204 cl = fclose int fd
205 k = write 2 cli_t_oom 0 len cli_t_oom
206 ret neg long 1
207 end
208 cap = ncap
209 end
210 k = read int fd cli_code tot sub cap tot
211 if eq k neg long 4
212 continue
213 end
214 if lt k 0
215 cl = fclose int fd
216 ret neg long 1
217 end
218 if eq k 0
219 break
220 end
221 tot = add tot ulong k
222 end
223 cl = fclose int fd
224 ret long tot
225end
226
227fn cli_emit_word off ulong pad ulong pairs ulong to int
228 tbuf = new uchar 256 0
229 lo = cli_word64 off
230 hi = cli_word64 add off 8
231 r = sm121_dis lo hi off edit tbuf
232 tn = and r 65535
233 ovf = and shr r 18 1
234 if or ne ovf 0 ge tn 256
235 ret neg int 1
236 end
237 isunk = and shr r 16 1
238 if eq pairs 1
239 cli_ohex4 off
240 cli_oc 32
241 cli_ohex16 lo
242 cli_oc 32
243 cli_ohex16 hi
244 cli_oc 32
245 else
246 for i ulong len cli_t_pad8
247 cli_oc get cli_t_pad8 i
248 end
249 cli_ohex4 off
250 for i ulong len cli_t_cend
251 cli_oc get cli_t_cend i
252 end
253 end
254 for i ulong tn bound 256
255 cli_oc get tbuf i
256 end
257 ispad = false
258 if ne pad 0
259 if ge off pad
260 ispad = true
261 end
262 end
263 if not ispad
264 cli_oc 32
265 end
266 cli_oc 59
267 cli_oc 10
268 if ne isunk 0
269 ret 1
270 end
271 ret 0
272end
273
274fn cli_emit_all clen ulong pairs ulong to int
275 pad = cli_pad_start clen
276 unk = ulong 0
277 put cli_olen 0 0
278 off = ulong 0
279 while lt off clen bound 16777216
280 rc = cli_emit_word off pad pairs
281 if lt rc 0
282 cli_flush
283 k = write 2 cli_t_long 0 len cli_t_long
284 ret 2
285 end
286 if gt rc 0
287 unk = add unk 1
288 end
289 off = add off 16
290 end
291 cli_flush
292 if ne unk 0
293 ret 1
294 end
295 ret 0
296end
297
298fn main to int
299 ac = argcnt
300 if eq ac 2
301 n1 = argread 1 cli_a1 0 4095
302 if lt n1 0
303 k = write 2 cli_t_usage 0 len cli_t_usage
304 ret 2
305 end
306 if cli_eq_a1_ver ulong n1
307 k = write 1 cli_t_verout 0 len cli_t_verout
308 ret 0
309 end
310 end
311 pairs = ulong 0
312 arch = ulong 0
313 if eq ac 3
314 n1 = argread 1 cli_a1 0 4095
315 if ge n1 0
316 if cli_eq_a1_pairs ulong n1
317 pairs = 1
318 end
319 end
320 end
321 if eq ac 4
322 n1 = argread 1 cli_a1 0 4095
323 n2 = argread 2 cli_a2 0 4095
324 if and ge n1 0 ge n2 0
325 if cli_eq_a1_b ulong n1
326 if not cli_eq_a2_sm121 ulong n2
327 k = write 2 cli_t_only 0 len cli_t_only
328 ret 2
329 end
330 arch = 1
331 end
332 end
333 end
334 want = add 2 add pairs mul 2 arch
335 if ne ulong ac want
336 k = write 2 cli_t_usage 0 len cli_t_usage
337 ret 2
338 end
339 plen = argread sub ulong ac 1 cli_path 0 4095
340 if le plen 0
341 k = write 2 cli_t_usage 0 len cli_t_usage
342 ret 2
343 end
344 flen = cli_read_file ulong plen
345 if lt flen 0
346 ret 2
347 end
348 clen = ulong flen
349 if ne rem clen 16 0
350 k = write 2 cli_t_pfx 0 len cli_t_pfx
351 cli_werr_a1 ulong plen
352 k = write 2 cli_t_mod16 0 len cli_t_mod16
353 ret 2
354 end
355 ret cli_emit_all clen pairs
356end