Skip to main content

rucc_asm/
lib.rs

1//! Instruction encoders, the integrated assembler, inline assembly and relaxation.
2//!
3//! Design: `spec/11-asm-objects-debug.md`. Layer rank 11, see `spec/18-package-layout.md`.
4//!
5//! # Status
6//!
7//! What is written are the two things a compiler does with a machine function: the assembly text
8//! `-S` produces, which is [`print()`], and the bytes of a text section, which is [`assemble`].
9//! Section 11.1 asks for one instruction description behind both, and there is one: the walk over
10//! a function is the same walk in both files, reading the same list out of `rucc-target`, and the
11//! only difference is whether an instruction is written down by name or handed to the encoder. So
12//! the listing and the object file cannot come to disagree about what an instruction is.
13//!
14//! What [`assemble`] hands back with the bytes is what the linker has to be told: where each
15//! function starts and how long it is, and every place in the bytes that names something this
16//! file does not contain. The jumps inside a function are not among them, because by the end of a
17//! function every block has a place and they are filled in here.
18//!
19//! A build that asked for debug information gets one more thing: where each machine instruction
20//! began and which span of the source it came from. Spans rather than files and lines, because this
21//! layer has no source map and the thing that has one is the driver, which is also the only place a
22//! `-ffile-prefix-map` still has paths to rewrite.
23//!
24//! The variables a file defines are here for the same reason and in the same shape. [`globals`] is
25//! the one walk over a module's globals, and what it gives back is a list of pieces that
26//! [`print()`] writes down as directives and [`Globals::image`] writes down as bytes, so a `.long`
27//! in a listing and the four bytes in the object beside it cannot come to disagree either. Where a
28//! variable goes is worked out there rather than named by the front end, and what a section is
29//! called is the object format's business.
30//!
31//! [`read`] is the other direction: a file of assembly that somebody else wrote, turned into the
32//! sections and names an object is written from. The directives and the labels are one half of it
33//! and the instructions are the other, and a mnemonic with no bytes behind it is refused by name
34//! with its line number rather than skipped. Nothing there describes the machine a second time:
35//! the bytes of an instruction come from the one encoder in `rucc-target` that the compiler's own
36//! output goes through, so a file this assembles and a file this compiles cannot disagree about
37//! what an instruction is. Branch relaxation is not here yet, so a jump is four bytes of distance
38//! whether it needs them or not, which is correct and longer than gas would have written.
39//!
40//! Every crate in the workspace is published, and publishing implies a promise. This one is
41//! tier 3: its Rust API is explicitly unstable and will change without a major version bump.
42//! Depend on the `rucc` binary's behaviour, not on this.
43
44#![doc(html_root_url = "https://docs.rs/rucc-asm/0.10.74")]
45
46mod att;
47mod bytes;
48mod data;
49mod format;
50mod instruction;
51mod source;
52mod unwind;
53
54pub use crate::att::print;
55pub use crate::bytes::{Assembled, Row, assemble};
56pub use crate::data::{Globals, Piece, Variable, aliases, globals};
57pub use crate::format::Directives;
58pub use crate::source::{Trouble, read};
59
60use std::fmt;
61
62/// The milestone in `spec/17-milestones.md` that fills this crate in.
63pub const MILESTONE: &str = "M3";
64
65/// A function this compiler could not write out as assembly.
66///
67/// Neither of these is a program's fault and neither should ever reach a user, since a machine
68/// function that reaches here has been through the whole backend and the tests pin both of the
69/// claims below. They are errors rather than assertions because the alternative to reporting one
70/// is writing a listing that is quietly wrong, and a wrong listing is the failure section 11.1 is
71/// written to prevent.
72#[derive(Debug, Clone, PartialEq, Eq)]
73pub enum Error {
74    /// An opcode the target has no description of.
75    Opcode {
76        /// The function it turned up in.
77        func: String,
78        /// The opcode, as the machine IR spells it.
79        opcode: String,
80    },
81    /// A register that is still virtual, which is a function that was never allocated.
82    Virtual {
83        /// The function it turned up in.
84        func: String,
85        /// The opcode the register is an operand of.
86        opcode: String,
87    },
88    /// An instruction the description names and the encoder could not write bytes for.
89    ///
90    /// The two halves of the description are meant to hold the same instructions, and a test
91    /// pins that they do, so this is either a row that was left out of one of them or an
92    /// operand the machine cannot express in the instruction that was chosen for it.
93    Encode {
94        /// The function it turned up in.
95        func: String,
96        /// The opcode, as the machine IR spells it.
97        opcode: String,
98        /// What the encoder said, already formatted.
99        why: String,
100    },
101    /// A jump inside a function to somewhere more than two gigabytes away.
102    ///
103    /// A single function that long is not a program anybody wrote, and the four bytes a jump
104    /// carries are all there are, so this is reported rather than wrapped around into a jump
105    /// somewhere else entirely.
106    Distance {
107        /// The function it turned up in.
108        func: String,
109        /// How far the jump would have had to reach.
110        bytes: i64,
111    },
112    /// A machine this crate cannot write assembly for.
113    Machine {
114        /// The triple that was asked for.
115        triple: String,
116    },
117    /// A thread-local variable on a format that does not spell one the way ELF does.
118    ///
119    /// The only one of these that is about a program rather than about this compiler. ELF says a
120    /// thread-local variable with a section flag and a symbol type, and that is written. Windows
121    /// hands out an index at load time and reaches the variable through a table the index names,
122    /// and Mach-O puts a descriptor in front of every one and reaches it by calling through the
123    /// descriptor, so on those two one is refused rather than written out as an ordinary variable
124    /// that every thread would share.
125    Thread {
126        /// The variable, as the C program spelled it.
127        name: String,
128        /// The object format that has no writing of one here, as its own name.
129        format: &'static str,
130    },
131    /// An ifunc, which is not a mistake and not written yet.
132    ///
133    /// The other thing an alias in the IR can be, and a different job from a second name for
134    /// something: the symbol is resolved once at program start by calling a function in this
135    /// object, which wants a symbol type of its own and a relocation of its own. One is refused
136    /// rather than written as an ordinary alias that would go to the resolver instead of to what
137    /// the resolver picked.
138    IFunc {
139        /// The name it defines, as the C program spelled it.
140        name: String,
141    },
142    /// A prologue the target's unwind table has no way to describe.
143    ///
144    /// ELF carries a little program per function and can say anything an instruction did to the
145    /// frame. Windows carries a fixed list of codes instead, each one of a handful of shapes a
146    /// prologue is allowed to have, and a prologue outside that list has no spelling there. The
147    /// one this compiler writes that does not fit is the frame pointer form, which establishes the
148    /// pointer before it takes the frame, so the table is refused rather than written describing a
149    /// frame of the wrong size. A build that does not want a table at all is the way past it, which
150    /// is `-fno-asynchronous-unwind-tables -fno-unwind-tables`.
151    Frame {
152        /// The function it turned up in.
153        func: String,
154        /// What about its prologue, already formatted.
155        why: String,
156    },
157    /// A piece of an initializer nothing here can write down.
158    Image {
159        /// The variable it is part of.
160        name: String,
161        /// What about it, already formatted.
162        why: String,
163    },
164}
165
166impl fmt::Display for Error {
167    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168        match self {
169            Error::Opcode { func, opcode } => {
170                write!(f, "'{func}' has a '{opcode}' and the target does not say what one is")
171            }
172            Error::Virtual { func, opcode } => {
173                write!(f, "'{func}' reached the assembler with a virtual register in a '{opcode}'")
174            }
175            Error::Encode { func, opcode, why } => {
176                write!(f, "'{func}' has a '{opcode}' the encoder refused: {why}")
177            }
178            Error::Distance { func, bytes } => {
179                write!(f, "'{func}' has a jump reaching {bytes} bytes, which does not fit in four")
180            }
181            Error::Machine { triple } => {
182                write!(f, "there is no assembly writer for {triple} in this compiler yet")
183            }
184            Error::Thread { name, format } => {
185                write!(f, "'{name}' is thread-local, which is not written on {format} yet")
186            }
187            Error::IFunc { name } => {
188                write!(f, "'{name}' is an ifunc, which this compiler does not write yet")
189            }
190            Error::Frame { func, why } => {
191                write!(f, "'{func}' has {why}, which no unwind table here can describe")
192            }
193            Error::Image { name, why } => {
194                write!(f, "the initializer of '{name}' has {why} in it, which cannot be written")
195            }
196        }
197    }
198}
199
200#[cfg(test)]
201mod tests {
202    #[test]
203    fn milestone_is_recorded() {
204        assert!(super::MILESTONE.starts_with('M'));
205    }
206}