ratasm 0.3.0

A terminal IDE and debugger for x86_64 assembly
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Symbol extraction: the labels, sections and constants a file defines.

use super::buffer::TextBuffer;
use super::position::Position;
use super::syntax::{self, TokenKind};

/// What kind of thing a symbol names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SymbolKind {
    /// A top-level label such as `_start:`.
    Label,
    /// A label scoped to the preceding top-level label, such as `.loop:`.
    LocalLabel,
    /// A `section` or `segment` declaration.
    Section,
    /// A symbol exported with `global`.
    Global,
    /// A symbol imported with `extern`.
    Extern,
    /// A constant defined with `equ`.
    Constant,
    /// A preprocessor macro or define.
    Macro,
}

impl SymbolKind {
    /// A short label for display in the symbol list.
    pub const fn description(self) -> &'static str {
        match self {
            SymbolKind::Label => "label",
            SymbolKind::LocalLabel => "local label",
            SymbolKind::Section => "section",
            SymbolKind::Global => "global",
            SymbolKind::Extern => "extern",
            SymbolKind::Constant => "constant",
            SymbolKind::Macro => "macro",
        }
    }

    /// Whether the symbol is a jump target the user can navigate to.
    pub const fn is_jump_target(self) -> bool {
        matches!(self, SymbolKind::Label | SymbolKind::LocalLabel)
    }

    /// Whether the symbol is defined here rather than merely named here.
    pub const fn is_definition(self) -> bool {
        matches!(
            self,
            SymbolKind::Label | SymbolKind::LocalLabel | SymbolKind::Constant | SymbolKind::Macro
        )
    }
}

/// A symbol defined somewhere in a buffer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Symbol {
    /// The name as written, for example `.loop`.
    pub name: String,
    /// The fully qualified name, for example `_start.loop`.
    pub qualified_name: String,
    /// What the symbol is.
    pub kind: SymbolKind,
    /// Where the definition starts.
    pub position: Position,
    /// Extra context shown next to the name, such as the section it sits in.
    pub detail: String,
}

impl Symbol {
    /// Whether `query` names this symbol, in either spelling.
    pub fn matches_name(&self, query: &str) -> bool {
        self.name == query || self.qualified_name == query
    }
}

/// Extracts every symbol defined in `buffer`, in source order.
pub fn extract(buffer: &TextBuffer) -> Vec<Symbol> {
    let mut symbols = Vec::new();
    let mut current_label: Option<String> = None;
    let mut current_section: Option<String> = None;

    for (line_index, line) in buffer.lines().iter().enumerate() {
        let tokens = syntax::tokenize(line);
        let significant: Vec<_> = tokens
            .iter()
            .filter(|token| !token.kind.is_trivia())
            .collect();
        let Some(first) = significant.first() else {
            continue;
        };

        match first.kind {
            TokenKind::LabelDefinition => {
                let name = first.text(line).to_owned();
                let is_local = name.starts_with('.');
                let qualified_name = match (&current_label, is_local) {
                    (Some(parent), true) => format!("{parent}{name}"),
                    _ => name.clone(),
                };
                if !is_local {
                    current_label = Some(name.clone());
                }

                let kind = match significant
                    .iter()
                    .find(|token| token.kind == TokenKind::Directive)
                {
                    Some(directive) if directive.text(line).eq_ignore_ascii_case("equ") => {
                        SymbolKind::Constant
                    }
                    _ if is_local => SymbolKind::LocalLabel,
                    _ => SymbolKind::Label,
                };

                symbols.push(Symbol {
                    name,
                    qualified_name,
                    kind,
                    position: Position::new(line_index, 0),
                    detail: current_section.clone().unwrap_or_default(),
                });
            }
            TokenKind::Directive => {
                let directive = first.text(line).to_ascii_lowercase();
                match directive.as_str() {
                    "section" | "segment" => {
                        if let Some(operand) = significant.get(1) {
                            let name = operand.text(line).to_owned();
                            current_section = Some(name.clone());
                            symbols.push(Symbol {
                                name: name.clone(),
                                qualified_name: name,
                                kind: SymbolKind::Section,
                                position: Position::new(line_index, 0),
                                detail: String::new(),
                            });
                        }
                    }
                    "global" | "extern" | "common" => {
                        let kind = if directive == "extern" {
                            SymbolKind::Extern
                        } else {
                            SymbolKind::Global
                        };
                        for operand in significant.iter().skip(1) {
                            if operand.kind != TokenKind::Identifier {
                                continue;
                            }
                            let name = operand.text(line).to_owned();
                            symbols.push(Symbol {
                                name: name.clone(),
                                qualified_name: name,
                                kind,
                                position: Position::new(line_index, 0),
                                detail: directive.clone(),
                            });
                        }
                    }
                    _ => {}
                }
            }
            TokenKind::Preprocessor => {
                let directive = first.text(line).to_ascii_lowercase();
                if matches!(
                    directive.as_str(),
                    "%define" | "%xdefine" | "%assign" | "%macro" | "%idefine"
                ) {
                    if let Some(operand) = significant.get(1) {
                        let name = operand.text(line).to_owned();
                        symbols.push(Symbol {
                            name: name.clone(),
                            qualified_name: name,
                            kind: SymbolKind::Macro,
                            position: Position::new(line_index, 0),
                            detail: directive.trim_start_matches('%').to_owned(),
                        });
                    }
                }
            }
            _ => {}
        }
    }

    symbols
}

/// Finds the definition of `name`, resolving local labels against `from`.
pub fn find_definition<'a>(
    symbols: &'a [Symbol],
    buffer: &TextBuffer,
    name: &str,
    from: Position,
) -> Option<&'a Symbol> {
    if name.starts_with('.') {
        if let Some(parent) = enclosing_label(buffer, from) {
            let qualified = format!("{parent}{name}");
            if let Some(found) = symbols
                .iter()
                .find(|symbol| symbol.qualified_name == qualified)
            {
                return Some(found);
            }
        }
    }
    symbols.iter().find(|symbol| symbol.matches_name(name))
}

/// The most recent non-local label at or before `position`.
pub fn enclosing_label(buffer: &TextBuffer, position: Position) -> Option<String> {
    let last = position.line.min(buffer.line_count().saturating_sub(1));
    (0..=last).rev().find_map(|index| {
        let line = buffer.line_or_empty(index);
        syntax::label_definition(line).filter(|label| !label.starts_with('.'))
    })
}

/// Every label name defined in the buffer, for completion.
pub fn label_names(buffer: &TextBuffer) -> Vec<String> {
    let mut names: Vec<String> = extract(buffer)
        .into_iter()
        .filter(|symbol| {
            matches!(
                symbol.kind,
                SymbolKind::Label
                    | SymbolKind::LocalLabel
                    | SymbolKind::Constant
                    | SymbolKind::Extern
                    | SymbolKind::Macro
            )
        })
        .map(|symbol| symbol.name)
        .collect();
    names.sort_unstable();
    names.dedup();
    names
}

#[cfg(test)]
mod tests {
    use super::*;

    const PROGRAM: &str = "\
section .data
message: db `Hello`, 0
length:  equ $ - message

section .text
global _start
extern printf

%define BUFFER 64

_start:
    mov rax, 1
.loop:
    dec rax
    jnz .loop
    ret

main:
.loop:
    ret
";

    fn symbols() -> Vec<Symbol> {
        extract(&TextBuffer::from_text(PROGRAM))
    }

    /// Finds the symbol with this name and kind.
    fn find(name: &str, kind: SymbolKind) -> Symbol {
        symbols()
            .into_iter()
            .find(|symbol| symbol.name == name && symbol.kind == kind)
            .unwrap_or_else(|| panic!("symbol {name} ({kind:?}) not found"))
    }

    #[test]
    fn top_level_labels_are_extracted() {
        for name in ["_start", "main", "message"] {
            let symbol = find(name, SymbolKind::Label);
            assert_eq!(symbol.qualified_name, name);
        }
    }

    #[test]
    fn sections_are_extracted() {
        let sections: Vec<String> = symbols()
            .into_iter()
            .filter(|symbol| symbol.kind == SymbolKind::Section)
            .map(|symbol| symbol.name)
            .collect();
        assert_eq!(sections, vec![".data", ".text"]);
    }

    #[test]
    fn globals_and_externs_are_distinguished() {
        assert_eq!(find("_start", SymbolKind::Global).detail, "global");
        assert_eq!(find("printf", SymbolKind::Extern).detail, "extern");
    }

    #[test]
    fn a_declared_and_defined_symbol_produces_both_entries() {
        let entries: Vec<SymbolKind> = symbols()
            .into_iter()
            .filter(|symbol| symbol.name == "_start")
            .map(|symbol| symbol.kind)
            .collect();
        assert_eq!(entries, vec![SymbolKind::Global, SymbolKind::Label]);
    }

    #[test]
    fn a_constant_defined_with_equ_is_not_a_plain_label() {
        assert_eq!(
            find("length", SymbolKind::Constant).kind,
            SymbolKind::Constant
        );
    }

    #[test]
    fn preprocessor_defines_are_extracted() {
        assert_eq!(find("BUFFER", SymbolKind::Macro).kind, SymbolKind::Macro);
    }

    #[test]
    fn symbols_carry_the_section_they_sit_in() {
        assert_eq!(find("message", SymbolKind::Label).detail, ".data");
        assert_eq!(find("_start", SymbolKind::Label).detail, ".text");
    }

    #[test]
    fn local_labels_are_qualified_by_their_parent() {
        let locals: Vec<(String, String)> = symbols()
            .into_iter()
            .filter(|symbol| symbol.kind == SymbolKind::LocalLabel)
            .map(|symbol| (symbol.name, symbol.qualified_name))
            .collect();
        assert_eq!(
            locals,
            vec![
                (".loop".to_owned(), "_start.loop".to_owned()),
                (".loop".to_owned(), "main.loop".to_owned()),
            ]
        );
    }

    #[test]
    fn a_local_label_resolves_against_the_enclosing_label() {
        let buffer = TextBuffer::from_text(PROGRAM);
        let symbols = extract(&buffer);

        let from_start = find_definition(&symbols, &buffer, ".loop", Position::new(14, 8))
            .expect("resolves under _start");
        assert_eq!(from_start.qualified_name, "_start.loop");

        let from_main = find_definition(&symbols, &buffer, ".loop", Position::new(19, 4))
            .expect("resolves under main");
        assert_eq!(from_main.qualified_name, "main.loop");
    }

    #[test]
    fn a_qualified_name_resolves_directly() {
        let buffer = TextBuffer::from_text(PROGRAM);
        let symbols = extract(&buffer);
        let found = find_definition(&symbols, &buffer, "main.loop", Position::ORIGIN)
            .expect("qualified lookup");
        assert_eq!(found.qualified_name, "main.loop");
    }

    #[test]
    fn an_unknown_name_resolves_to_nothing() {
        let buffer = TextBuffer::from_text(PROGRAM);
        let symbols = extract(&buffer);
        assert!(find_definition(&symbols, &buffer, "nowhere", Position::ORIGIN).is_none());
    }

    #[test]
    fn enclosing_label_walks_backwards() {
        let buffer = TextBuffer::from_text(PROGRAM);
        assert_eq!(
            enclosing_label(&buffer, Position::new(14, 0)),
            Some("_start".to_owned())
        );
        assert_eq!(
            enclosing_label(&buffer, Position::new(19, 0)),
            Some("main".to_owned())
        );
    }

    #[test]
    fn enclosing_label_past_the_end_of_the_buffer_is_clamped() {
        let buffer = TextBuffer::from_text(PROGRAM);
        assert!(enclosing_label(&buffer, Position::new(9999, 0)).is_some());
    }

    #[test]
    fn labels_inside_comments_and_strings_are_ignored() {
        let buffer = TextBuffer::from_text(
            "; fake_label: not real\n    db \"other_label: also not real\", 0\nreal_label:\n",
        );
        let names: Vec<String> = extract(&buffer).into_iter().map(|s| s.name).collect();
        assert_eq!(names, vec!["real_label"]);
    }

    #[test]
    fn an_empty_buffer_yields_no_symbols() {
        assert!(extract(&TextBuffer::new()).is_empty());
    }

    #[test]
    fn label_names_are_sorted_and_deduplicated() {
        let buffer = TextBuffer::from_text(PROGRAM);
        let names = label_names(&buffer);
        let mut sorted = names.clone();
        sorted.sort_unstable();
        assert_eq!(names, sorted);
        assert_eq!(
            names.iter().filter(|name| *name == ".loop").count(),
            1,
            "duplicate short names collapse for completion"
        );
        assert!(names.contains(&"_start".to_owned()));
        assert!(names.contains(&"printf".to_owned()));
    }
}