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 10, 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//! The variables a file defines are here for the same reason and in the same shape. [`globals`] is
20//! the one walk over a module's globals, and what it gives back is a list of pieces that
21//! [`print()`] writes down as directives and [`Globals::image`] writes down as bytes, so a `.long`
22//! in a listing and the four bytes in the object beside it cannot come to disagree either. Where a
23//! variable goes is worked out there rather than named by the front end, and what a section is
24//! called is the object format's business.
25//!
26//! The assembler that reads `.s` and `.S`, inline assembly and relaxation are the rest of M3 and
27//! M4 and are not here yet.
28//!
29//! Every crate in the workspace is published, and publishing implies a promise. This one is
30//! tier 3: its Rust API is explicitly unstable and will change without a major version bump.
31//! Depend on the `rucc` binary's behaviour, not on this.
32
33#![doc(html_root_url = "https://docs.rs/rucc-asm/0.3.8")]
34
35mod att;
36mod bytes;
37mod data;
38mod format;
39
40pub use crate::att::print;
41pub use crate::bytes::assemble;
42pub use crate::data::{Globals, Piece, Variable, globals};
43pub use crate::format::Directives;
44
45use std::fmt;
46
47/// The milestone in `spec/17-milestones.md` that fills this crate in.
48pub const MILESTONE: &str = "M3";
49
50/// A function this compiler could not write out as assembly.
51///
52/// Neither of these is a program's fault and neither should ever reach a user, since a machine
53/// function that reaches here has been through the whole backend and the tests pin both of the
54/// claims below. They are errors rather than assertions because the alternative to reporting one
55/// is writing a listing that is quietly wrong, and a wrong listing is the failure section 11.1 is
56/// written to prevent.
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum Error {
59 /// An opcode the target has no description of.
60 Opcode {
61 /// The function it turned up in.
62 func: String,
63 /// The opcode, as the machine IR spells it.
64 opcode: String,
65 },
66 /// A register that is still virtual, which is a function that was never allocated.
67 Virtual {
68 /// The function it turned up in.
69 func: String,
70 /// The opcode the register is an operand of.
71 opcode: String,
72 },
73 /// An instruction the description names and the encoder could not write bytes for.
74 ///
75 /// The two halves of the description are meant to hold the same instructions, and a test
76 /// pins that they do, so this is either a row that was left out of one of them or an
77 /// operand the machine cannot express in the instruction that was chosen for it.
78 Encode {
79 /// The function it turned up in.
80 func: String,
81 /// The opcode, as the machine IR spells it.
82 opcode: String,
83 /// What the encoder said, already formatted.
84 why: String,
85 },
86 /// A jump inside a function to somewhere more than two gigabytes away.
87 ///
88 /// A single function that long is not a program anybody wrote, and the four bytes a jump
89 /// carries are all there are, so this is reported rather than wrapped around into a jump
90 /// somewhere else entirely.
91 Distance {
92 /// The function it turned up in.
93 func: String,
94 /// How far the jump would have had to reach.
95 bytes: i64,
96 },
97 /// A machine this crate cannot write assembly for.
98 Machine {
99 /// The triple that was asked for.
100 triple: String,
101 },
102 /// A thread-local variable, which is not a mistake and not written yet.
103 ///
104 /// The only one of these that is about a program rather than about this compiler. Reaching a
105 /// thread-local variable is a call or a load off the thread pointer depending on the model,
106 /// and none of that is built, so one is refused rather than written out as an ordinary
107 /// variable that every thread would share.
108 Thread {
109 /// The variable, as the C program spelled it.
110 name: String,
111 },
112 /// A piece of an initializer nothing here can write down.
113 Image {
114 /// The variable it is part of.
115 name: String,
116 /// What about it, already formatted.
117 why: String,
118 },
119}
120
121impl fmt::Display for Error {
122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123 match self {
124 Error::Opcode { func, opcode } => {
125 write!(f, "'{func}' has a '{opcode}' and the target does not say what one is")
126 }
127 Error::Virtual { func, opcode } => {
128 write!(f, "'{func}' reached the assembler with a virtual register in a '{opcode}'")
129 }
130 Error::Encode { func, opcode, why } => {
131 write!(f, "'{func}' has a '{opcode}' the encoder refused: {why}")
132 }
133 Error::Distance { func, bytes } => {
134 write!(f, "'{func}' has a jump reaching {bytes} bytes, which does not fit in four")
135 }
136 Error::Machine { triple } => {
137 write!(f, "there is no assembly writer for {triple} in this compiler yet")
138 }
139 Error::Thread { name } => {
140 write!(f, "'{name}' is thread-local, which this compiler does not build yet")
141 }
142 Error::Image { name, why } => {
143 write!(f, "the initializer of '{name}' has {why} in it, which cannot be written")
144 }
145 }
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 #[test]
152 fn milestone_is_recorded() {
153 assert!(super::MILESTONE.starts_with('M'));
154 }
155}