Skip to main content

rucc_lower/
unit.rs

1//! The module level of the walk: what a translation unit's declarations become.
2//!
3//! Design: `spec/08-ir.md` section 8.9.
4//!
5//! One typed tree becomes one [`Module`]. A file-scope object becomes a global with an image
6//! built from its initializer, a function becomes a [`Func`] whose body is built by
7//! [`body`](mod@crate::body), and a string literal becomes an unnamed constant global that
8//! whatever mentioned it points at.
9//!
10//! # What an image is
11//!
12//! An initializer arrives here already flattened: one entry per scalar that is stored, each
13//! with the byte offset it goes at, with every designator and every nested brace already
14//! resolved. So building the image is a walk over the entries in offset order, filling the gaps
15//! between them with zeros, and the only thing that has to be worked out per entry is whether
16//! the value is a number, a run of bytes from a string literal, or the address of something the
17//! linker has to place.
18//!
19//! # Names
20//!
21//! An object with linkage is known by the name it was written with, and there is nothing to
22//! invent. A `static` inside a function has no linkage and still needs a name in the object
23//! file, so it gets `name.N`, which is what gcc does and is why two functions may each have a
24//! `static int count;` without colliding. A string literal has no name at all and gets
25//! `.Lstr.N`, whose leading dot keeps it out of the symbol table on every target that has the
26//! convention.
27
28use std::cmp::Ordering;
29use std::collections::{BTreeMap, HashMap, HashSet};
30use std::fmt;
31
32use rucc_base::{Interner, Symbol};
33use rucc_diag::{Diagnostic, Span};
34use rucc_ir::{
35    Alias, AliasKind, AttrSet, DataList, Datum, FpContract, Func, Global, Imm,
36    Linkage as IrLinkage, Meta, Module, Reloc, SymbolRef, TlsModel, Type,
37    Visibility as IrVisibility,
38};
39use rucc_sema::{
40    Address, Base, Const, Conversion, DeclFlags, DeclId, DeclKind, Definition, Effects, Emission,
41    Eval, ExprId, ExprKind, InitEntry, InitList, LabelId, Linkage, Priority, StorageDuration,
42    StrId, Tast, Visibility,
43};
44use rucc_target::{ObjectFormat, TargetInfo};
45use rucc_types::{TypeId, TypeKind, Types, compatible, is_complex, is_scalar};
46
47use crate::abi::{self, Plan};
48use crate::aliasing;
49use crate::body;
50use crate::directives;
51use crate::reach;
52use crate::repr;
53
54/// Which functions get a stack protector, which is what the `-fstack-protector` family decides.
55///
56/// The question is about the locals a function has, so it is answered here and not in the back
57/// end: by the time a frame is laid out the types are gone and every local is a size and an
58/// alignment. What the back end then does about the answer is its own business, and it is carried
59/// to it as [`rucc_ir::AttrSet::STACK_PROTECT`] on the function.
60///
61/// The names are gcc's, and so are the rules. A build that has been compiled with one of these for
62/// twenty years is entitled to the same set of protected functions from a compiler claiming to be
63/// compatible, because the ones left out are the ones an exploit goes looking for.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
65pub enum Protector {
66    /// None of them, which is `-fno-stack-protector` and what a command line that says nothing
67    /// gets.
68    #[default]
69    None,
70    /// A function with a local array of at least eight bytes, or one whose stack grows while it
71    /// runs. `-fstack-protector`, which is the original and the narrowest.
72    Buffers,
73    /// Any of those, and any function with a local array at all, a local holding one, or a local
74    /// whose address is taken. `-fstack-protector-strong`, which is what every distribution builds
75    /// its packages with and therefore the one a real build line carries.
76    Strong,
77    /// Every function that has a frame at all. `-fstack-protector-all`.
78    All,
79}
80
81/// What overflows rather than being undefined, which is `-fwrapv` and its relatives.
82///
83/// Every licence the walk grants the optimizer about overflow is one flag on one instruction, and
84/// withdrawing a licence is not setting it. So this is read where the flags are chosen and nowhere
85/// else, and a unit built with either of these is a unit whose IR carries less rather than a unit
86/// the passes are told something extra about. That is also what makes it correct across link time
87/// optimization: a body from a unit that wraps and a body from one that does not keep their own
88/// answers when they end up in the same module.
89///
90/// `-ftrapv` is the exception and is the reason this is not simply two flags. It is the other
91/// answer to the question `-fwrapv` answers, and it is the only one of the three that asks for
92/// something to be generated rather than for something to be left out.
93#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
94pub struct Wrapping {
95    /// Whether signed arithmetic wraps, from `-fwrapv`. Set, and an add, a subtract, a multiply, a
96    /// shift and a negation in a signed type stop saying they do not wrap.
97    pub signed: bool,
98    /// Whether pointer arithmetic wraps, from `-fwrapv-pointer`. Set, and the multiply that turns
99    /// an index into a number of bytes stops saying so.
100    ///
101    /// That multiply is the whole of it here, because the addition itself never claimed anything: a
102    /// `ptradd` carries no flags in this IR and no pass reads one off it.
103    pub pointer: bool,
104    /// Whether a signed overflow stops the program, from `-ftrapv`. Set, and an add, a subtract, a
105    /// multiply and a negation in a signed type become calls to the routine in the runtime that
106    /// does the arithmetic and checks it.
107    ///
108    /// Never set at the same time as [`Wrapping::signed`], because a program cannot both wrap and
109    /// stop. The driver is what keeps that true.
110    pub trap: bool,
111}
112
113/// Everything the walk reads, which is a checked translation unit and the target it is for.
114///
115/// The interner is mutable because the walk invents names the program never wrote: the label a
116/// string literal is emitted under, and the mangled name of a function-scope `static`.
117pub struct Context<'a> {
118    /// The typed tree.
119    pub tast: &'a Tast,
120    /// The types it points into.
121    pub types: &'a Types,
122    /// What is being compiled for, which is where every width and every alignment comes from.
123    pub target: &'a TargetInfo,
124    /// The name table.
125    pub names: &'a mut Interner,
126    /// What a name that no declaration of it said anything about gets, which is `-fvisibility=`.
127    ///
128    /// A fact about the compilation rather than about any declaration, which is why it arrives
129    /// here rather than on the tree: the checker knows what was written and this knows what the
130    /// command line asked for, and the answer is the first of those where there is one.
131    pub visibility: IrVisibility,
132    /// Which functions get a stack protector, which is `-fstack-protector` and its relatives.
133    pub protector: Protector,
134    /// What overflows rather than being undefined, which is `-fwrapv` and its relatives.
135    ///
136    /// A fact about the compilation for the same reason the two above it are: what was written is
137    /// on the tree and what was asked for is on the command line.
138    pub wrapping: Wrapping,
139    /// Whether an access carries the node for the type it goes through, which is
140    /// `-fstrict-aliasing` and is on unless `-fno-strict-aliasing` cleared it.
141    ///
142    /// Clearing it here rather than in the optimizer is what makes the flag one condition in one
143    /// place: an access with no node conflicts with every other access, so a unit built with the
144    /// flag off is a unit whose IR says less rather than a unit the passes are told something
145    /// extra about. That is also what keeps it right across link time optimization, the way
146    /// [`Context::wrapping`] is: a body from a unit that named its types and a body from one that
147    /// did not keep their own answers when they end up in the same module.
148    pub aliasing: bool,
149    /// Whether an access says how far the padding after it reaches, which is
150    /// `-fsafety-init=nopadding` and is what a build with no safety tier gets too, since nothing
151    /// reads the number then.
152    ///
153    /// Here rather than in the safety pass for the reason [`Context::aliasing`] is here: what the
154    /// number is takes a record's layout, and the layout is a thing the walk has in hand and the
155    /// pass over the IR does not. The pass reads it and does not decide anything, which keeps the
156    /// flag one condition in one place and keeps it right across link time optimization.
157    pub padding: bool,
158    /// How far a multiply and an addition may be fused into one rounding, which is
159    /// `-ffp-contract=`.
160    ///
161    /// A fact about the compilation like the ones above it, and the one of them that is written
162    /// down rather than acted on: it goes onto every function with a body as
163    /// [`rucc_ir::Attrs::fp_contract`], because the place that would fuse anything is the code
164    /// generator and by the time it runs the command line is gone and the two operations it might
165    /// fuse may have come from different statements.
166    pub contract: FpContract,
167    /// What every function in the unit is aligned to unless it asked for more itself, which is
168    /// `-falign-functions` and is `None` for the alignment the target gives anyway.
169    ///
170    /// A fact about the compilation like the ones above it, and it meets a fact about a
171    /// declaration here rather than further down: `__attribute__((aligned(N)))` is a statement
172    /// about one function and this is a preference about all of them, so the function takes the
173    /// larger of the two and everything below reads one number.
174    pub align: Option<u32>,
175    /// Whether every function calls the two profiling hooks on the way in and on the way out,
176    /// which is `-finstrument-functions`.
177    ///
178    /// Done here rather than in a pass because gcc does it before it inlines anything, so a body
179    /// that is inlined takes its two calls with it and they still name the function they were
180    /// written for. A pass over the IR would see the body only after that had happened to it.
181    pub instrument: bool,
182    /// Whether an exception may unwind through what the walk builds, which is `-fexceptions`.
183    ///
184    /// What it asks of C is that a `cleanup` handler run when an unwind passes through its scope,
185    /// and that takes a landing pad and a table the personality routine reads, neither of which is
186    /// built yet. So what it does here is turn down the one shape the missing pad would change the
187    /// meaning of, a call made while a handler is owed, rather than build it without the pad.
188    pub exceptions: bool,
189    /// How a file named by a `.incbin` in an `asm` at file scope is read, given the name as the
190    /// template wrote it and handing back either the bytes or what went wrong.
191    ///
192    /// Passed in rather than reached for, because the walk has no business opening files and
193    /// because a caller that put its sources somewhere other than a disk has put this file there
194    /// too. The name is resolved the way an assembler resolves it, which is against the directory
195    /// the compiler was run in and not against the directory the source was found in.
196    pub read: &'a mut dyn FnMut(&str) -> Result<Vec<u8>, String>,
197}
198
199// Written out rather than derived because a closure has no `Debug`, and printing one would say
200// nothing anyway. What is worth reading here is the settings, so those are what this prints.
201impl fmt::Debug for Context<'_> {
202    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203        f.debug_struct("Context")
204            .field("visibility", &self.visibility)
205            .field("protector", &self.protector)
206            .field("wrapping", &self.wrapping)
207            .field("aliasing", &self.aliasing)
208            .field("padding", &self.padding)
209            .field("contract", &self.contract)
210            .field("align", &self.align)
211            .field("instrument", &self.instrument)
212            .field("exceptions", &self.exceptions)
213            .finish_non_exhaustive()
214    }
215}
216
217/// One function that runs without anything calling it, waiting for the section it goes in.
218///
219/// Held back rather than written where the definition is met, because the order they go in is not
220/// always the order the file defined them: a format with one section for all of them is a format
221/// where the only record of the priority is the position in that section, so they have to be
222/// sorted, and sorting means having all of them.
223#[derive(Debug, Clone, Copy)]
224struct Start {
225    /// The function the entry is the address of.
226    func: Symbol,
227    /// Whether it runs in the run-up to `main` rather than in the run-down after it.
228    before: bool,
229    /// Where in the order the attribute asked for it to go.
230    priority: Priority,
231    /// The definition it came from, for the diagnostic a format with no way to say it needs.
232    span: Span,
233}
234
235impl Start {
236    /// Where this goes among the others, which is the order the entries are written in.
237    ///
238    /// A lower number first, and the unnumbered ones after every numbered one, which is the order
239    /// an ELF linker puts the sections in and therefore the order every format has to come out in
240    /// for the three of them to agree. The sort is stable, so two at the same priority stay in the
241    /// order the file defined them, which is all that decides between them.
242    fn order(&self) -> (u8, u16) {
243        match self.priority {
244            Priority::Numbered(number) => (0, number),
245            Priority::Unnumbered => (1, 0),
246        }
247    }
248}
249
250/// What the walk produced.
251#[derive(Debug)]
252pub struct Lowered {
253    /// The module, which is complete even when something was reported: a construct that is not
254    /// supported yet leaves the rest of the function around it intact.
255    pub module: Module,
256    /// What was reported, in the order it was found.
257    pub diagnostics: Vec<Diagnostic>,
258}
259
260/// Walks a checked translation unit and builds the IR for it.
261///
262/// `name` is the module's name, which is the file the tree came from.
263#[must_use]
264pub fn lower(name: &str, cx: Context<'_>) -> Lowered {
265    let Context {
266        tast,
267        types,
268        target,
269        names,
270        visibility,
271        protector,
272        wrapping,
273        aliasing,
274        padding,
275        contract,
276        align,
277        instrument,
278        exceptions,
279        read,
280    } = cx;
281    let module = Module::new(names.intern(name), target);
282    let reachable = reach::reachable(reach::Decide::new(tast, types, target, names));
283    let mut unit = Unit {
284        tast,
285        types,
286        target,
287        names,
288        visibility,
289        protector,
290        wrapping,
291        aliasing,
292        padding,
293        cliques: 0,
294        tree: aliasing::Tree::default(),
295        contract,
296        align,
297        instrument,
298        exceptions,
299        read,
300        module,
301        diagnostics: Vec::new(),
302        strings: HashMap::new(),
303        anonymous: 0,
304        statics: HashMap::new(),
305        labels: HashMap::new(),
306        done: HashSet::new(),
307        aliases: Vec::new(),
308        sets: Vec::new(),
309        aliased: HashSet::new(),
310        starts: Vec::new(),
311        renamed: HashMap::new(),
312        reachable,
313    };
314    unit.run();
315    Lowered { module: unit.module, diagnostics: unit.diagnostics }
316}
317
318/// The walk over one translation unit, and everything it has built so far.
319pub(crate) struct Unit<'a> {
320    pub(crate) tast: &'a Tast,
321    pub(crate) types: &'a Types,
322    pub(crate) target: &'a TargetInfo,
323    pub(crate) names: &'a mut Interner,
324    /// What a name no declaration said anything about gets. See [`Context::visibility`].
325    visibility: IrVisibility,
326    /// Which functions get a stack protector. See [`Context::protector`].
327    pub(crate) protector: Protector,
328    /// What wraps rather than being undefined. See [`Context::wrapping`].
329    pub(crate) wrapping: Wrapping,
330    /// Whether an access names the type it goes through. See [`Context::aliasing`].
331    aliasing: bool,
332    /// Whether an access says how far the padding after it reaches. See [`Context::padding`].
333    pub(crate) padding: bool,
334    /// How many `restrict` scopes have been handed out, which is a number the whole module shares
335    /// so that no two functions promise different things with the same one. See
336    /// [`restrict`](mod@crate::restrict) for why that matters before there is an inliner.
337    pub(crate) cliques: u16,
338    /// The type based aliasing tree built so far, which is one per module.
339    tree: aliasing::Tree,
340    /// How far a multiply and an addition may be fused. See [`Context::contract`].
341    pub(crate) contract: FpContract,
342    /// What every function is aligned to unless it asked for more. See [`Context::align`].
343    align: Option<u32>,
344    /// Whether the profiling hooks are called. See [`Context::instrument`].
345    pub(crate) instrument: bool,
346    /// Whether an exception may unwind through the unit. See [`Context::exceptions`].
347    pub(crate) exceptions: bool,
348    /// How a file a `.incbin` names is read. See [`Context::read`].
349    read: &'a mut dyn FnMut(&str) -> Result<Vec<u8>, String>,
350    pub(crate) module: Module,
351    pub(crate) diagnostics: Vec<Diagnostic>,
352    /// The global each string literal was emitted as, so that two mentions of one literal are
353    /// one object.
354    strings: HashMap<StrId, Symbol>,
355    /// How many runs of bytes written under no label in an `asm` at file scope have been given a
356    /// name, which is what keeps the next one from being given the same one.
357    anonymous: usize,
358    /// The name each object with no linkage was given.
359    statics: HashMap<DeclId, Symbol>,
360    /// The name each label an image holds the address of was given.
361    ///
362    /// A label is a place inside a function and has no name in the object file, because a jump to
363    /// one is a distance the assembler works out and never a symbol. An image is the one thing
364    /// that cannot do that: it is in another section, so what it holds is a relocation, and a
365    /// relocation names a symbol. So a label an image points at gets one, minted here because the
366    /// image is lowered before the body is walked and the block the label starts does not exist
367    /// yet when the name is first asked for.
368    labels: HashMap<LabelId, Symbol>,
369    /// What has been emitted, because a redeclaration is the same declaration seen twice.
370    done: HashSet<DeclId>,
371    /// The declarations that are a second name for something rather than a thing of their own,
372    /// in the order the file made them.
373    ///
374    /// Held back rather than emitted where they are met, because what an alias points at may be
375    /// written below it and whether anything defines it is a question only the whole file
376    /// answers.
377    aliases: Vec<DeclId>,
378    /// The names a `.set` in an `asm` at file scope gave to something else, with the block each
379    /// one was written in, in the order the file wrote them.
380    ///
381    /// Held back for the reason above and written out beside the aliases, since the two are the
382    /// same thing said two ways: a second symbol at an address this object already has.
383    sets: Vec<(directives::Set, Span)>,
384    /// The symbols something in the file is a second name for.
385    ///
386    /// A `static` function nothing calls is not emitted, and being what an alias points at is a
387    /// reason to emit one that no reference in the file says: the string an alias names is not a
388    /// use of anything as far as the walk over the tree is concerned.
389    aliased: HashSet<Symbol>,
390    /// The functions the file asked to have run without anything calling them, in the order it
391    /// defined them.
392    ///
393    /// Held back rather than emitted where they are met, because the entries go in the order the
394    /// priorities put them and a function written at the top of the file may have asked to run
395    /// last. Only the whole file settles that order.
396    starts: Vec<Start>,
397    /// The assembler name the file gave to a name with linkage, kept by the name that was
398    /// written rather than by the declaration that wrote it.
399    ///
400    /// For [`Unit::library_name`], which knows what the C library calls a function and not what
401    /// this file has said about it. The declaration that renames `memcpy` is a different
402    /// declaration from the implicit one the checker made for `__builtin_memcpy`, so the label
403    /// on the first is never reached from the second, and a program that renames a function and
404    /// then calls the builtin means the call to go to the new name.
405    renamed: HashMap<Symbol, Symbol>,
406    /// What something in the file reaches, which is what decides whether a function with
407    /// internal linkage is emitted at all.
408    reachable: HashSet<DeclId>,
409}
410
411// The debug is by hand and short: a translation unit is not something anybody wants printed as
412// a `{:?}`, and the module has a printer of its own for when they do.
413impl fmt::Debug for Unit<'_> {
414    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
415        f.debug_struct("Unit")
416            .field("module", &self.module.counts())
417            .field("diagnostics", &self.diagnostics.len())
418            .finish()
419    }
420}
421
422impl Unit<'_> {
423    /// The aliasing node an access through `ty` carries, and [`None`] when it carries none.
424    ///
425    /// [`None`] is also every answer under `-fno-strict-aliasing`, which is the whole of what that
426    /// flag does here. See [`aliasing`](mod@crate::aliasing) for which types have a node.
427    pub(crate) fn alias_node(&mut self, ty: TypeId) -> Option<Meta> {
428        if !self.aliasing {
429            return None;
430        }
431        self.tree.node(&mut self.module, self.names, self.types, ty)
432    }
433
434    /// The root of the aliasing tree, which is the node an access that may be punned carries.
435    ///
436    /// The root is `char` and it conflicts with everything, so an access carrying it is an access
437    /// nothing may be reordered across and, in the type plane, a byte nothing has settled the type
438    /// of. `crate::body` says which accesses those are.
439    pub(crate) fn alias_root(&mut self) -> Option<Meta> {
440        if !self.aliasing {
441            return None;
442        }
443        Some(self.tree.root(&mut self.module, self.names))
444    }
445
446    /// Every declaration the file made, in the order it made them.
447    fn run(&mut self) {
448        self.file_asms();
449        self.find_aliased();
450        self.find_renamed();
451        for index in 0..self.tast.top_level().len() {
452            let decl = self.tast.top_level()[index];
453            if !self.done.insert(decl) {
454                continue;
455            }
456            match self.tast[decl].kind {
457                DeclKind::Function => self.function(decl),
458                DeclKind::Object => self.object(decl),
459                // A name for a type is only in the tree at block scope and nothing is emitted
460                // for one.
461                DeclKind::Type => {}
462            }
463        }
464        for index in 0..self.aliases.len() {
465            self.alias(self.aliases[index]);
466        }
467        for index in 0..self.sets.len() {
468            let (set, span) = self.sets[index].clone();
469            self.equated(&set, span);
470        }
471        self.startups();
472    }
473
474    /// The `asm` written at file scope, read into the globals they define.
475    ///
476    /// Ahead of the declarations rather than among them. A block usually names more than one
477    /// thing and means them to be next to each other, the object writer lays globals out in the
478    /// order the module holds them, and adding a block's globals together is what makes them a
479    /// run. A declaration of one of those names below the block then finds a definition already
480    /// there and leaves it alone, which is the division the program wrote: the template says what
481    /// the bytes are and the C declaration says what they are to be read as.
482    fn file_asms(&mut self) {
483        for index in 0..self.tast.file_asms().len() {
484            let asm = self.tast.file_asms()[index];
485            let template = self.spelled(asm.template);
486            let read = match directives::assemble(&template, &mut *self.read) {
487                Ok(read) => read,
488                Err(directives::Failed::Unsupported(what)) => {
489                    self.unsupported(&format!("{what} in an `asm` at file scope"), asm.span);
490                    continue;
491                }
492                // An instruction, which the assembler reads rather than this, so the template is
493                // kept as it was written and the unit is assembled from its listing, where the
494                // template goes in the way gcc puts it into its own. Only on ELF, which is the
495                // syntax the listing reader knows.
496                Err(directives::Failed::Instruction(_))
497                    if self.target.object_format == ObjectFormat::Elf =>
498                {
499                    self.module.add_file_asm(template);
500                    continue;
501                }
502                Err(directives::Failed::Instruction(word)) => {
503                    let what = format!("the instruction '{word}' in an `asm` at file scope");
504                    self.unsupported(&what, asm.span);
505                    continue;
506                }
507                Err(directives::Failed::Missing(name, why)) => {
508                    let message = format!("cannot open '{name}' for reading: {why}");
509                    self.diagnostics.push(Diagnostic::error(message, asm.span).with_code("E0702"));
510                    continue;
511                }
512            };
513            // The name of every global of the block first, because a distance one of them writes
514            // is measured to a place in another of them and a relocation names a symbol, so the
515            // name has to be to hand before the bytes that refer to it are built.
516            let symbols: Vec<Symbol> = read
517                .pieces
518                .iter()
519                .map(|piece| match &piece.name {
520                    Some(name) => self.names.intern(name),
521                    None => {
522                        let name = format!(".Lasm.{}", self.anonymous);
523                        self.anonymous += 1;
524                        self.names.intern(&name)
525                    }
526                })
527                .collect();
528            for (index, piece) in read.pieces.into_iter().enumerate() {
529                self.piece(piece, symbols[index], &symbols);
530            }
531            // Held back until the file has been walked, because a name a block equates may be
532            // defined below the block, and remembered as a name something points at, because a
533            // `static` function an equate is the only reference to is one that has to be emitted.
534            for set in read.sets {
535                let target = self.names.intern(&set.target);
536                self.aliased.insert(target);
537                self.sets.push((set, asm.span));
538            }
539        }
540    }
541
542    /// One global an `asm` at file scope defined, under the name minted for it and with the names
543    /// of the whole block to hand.
544    ///
545    /// The bytes a template writes before it writes any label are a global like the rest and a
546    /// global has to have a name, so one is minted for them. Nothing refers to it by that name, so
547    /// the only thing it has to be is one nothing else takes, and the leading dot keeps it out of
548    /// the symbol table the way the name of a string literal does.
549    fn piece(&mut self, piece: directives::Piece, symbol: Symbol, symbols: &[Symbol]) {
550        let mut global = Global::new(symbol, piece.size, piece.align.max(1));
551        global.linkage = piece.linkage;
552        global.visibility = piece.visibility;
553        let bss = matches!(piece.section, directives::Section::Bss);
554        match piece.section {
555            // Which of the sections the object writer has an answer of its own for. Asking for
556            // `.rodata` by name would produce a second section with that spelling and with the
557            // flags of a writable one, so what is said here is what the global is instead.
558            directives::Section::ReadOnly => global.constant = true,
559            directives::Section::Data | directives::Section::Bss => {}
560            directives::Section::Named(name) => global.section = Some(self.names.intern(&name)),
561            // Refused where the template was read, since what goes in that section is
562            // instructions and there is nothing here that makes one.
563            directives::Section::Text => return,
564        }
565        let mut data = Vec::with_capacity(piece.items.len());
566        if piece.items.is_empty() && bss {
567            // A label at the end of the zero filled section, which has nothing under it and
568            // still has to land there rather than in the section of written bytes. An image of
569            // no zeros is what says so, since being all zeros is how a global asks for that
570            // section and an empty image asks for nothing.
571            data.push(Datum::Zero(0));
572        }
573        for item in piece.items {
574            data.push(match item {
575                directives::Item::Bytes(bytes) => Datum::Bytes(self.module.push_bytes(&bytes)),
576                directives::Item::Int { width, value } => {
577                    let ty = Type::int(u32::from(width) * 8);
578                    Datum::Scalar {
579                        ty,
580                        value: self.module.add_imm(Imm::int(i128::from(value), ty)),
581                    }
582                }
583                directives::Item::Zero(bytes) => Datum::Zero(bytes),
584                // Four bytes holding how far that global is from these bytes, which the reader
585                // said in globals of this block rather than in names because the place it
586                // measures to is usually a label the object file holds no name for.
587                directives::Item::Away { piece, addend } => {
588                    let reloc = Reloc { symbol: symbols[piece], addend, size: 4 };
589                    Datum::Away(self.module.add_reloc(reloc))
590                }
591            });
592        }
593        global.init = Some(self.module.push_data(&data));
594        self.place_global(global);
595    }
596
597    /// Which symbols the file gives a second name to, before anything is emitted.
598    ///
599    /// Ahead of the walk rather than during it, because a `static` function is emitted or not on
600    /// the strength of what reaches it and the alias that reaches one may be written below it.
601    fn find_aliased(&mut self) {
602        for index in 0..self.tast.top_level().len() {
603            let decl = self.tast.top_level()[index];
604            let Some(target) = self.tast[decl].alias else { continue };
605            let spelling = self.spelled(target);
606            let symbol = self.names.intern(&spelling);
607            self.aliased.insert(symbol);
608        }
609    }
610
611    /// Which names the file gave an assembler name of their own, before anything is emitted.
612    ///
613    /// Ahead of the walk for the reason [`Unit::find_aliased`] is: the call to
614    /// `__builtin_memcpy` may be written above the declaration of `memcpy` that renames it, and
615    /// the two spellings are one function.
616    fn find_renamed(&mut self) {
617        for index in 0..self.tast.top_level().len() {
618            let decl = self.tast.top_level()[index];
619            let node = &self.tast[decl];
620            let (linkage, name, label) = (node.linkage, node.name, node.asm_label);
621            if linkage == Linkage::None {
622                continue;
623            }
624            let (Some(name), Some(label)) = (name, label) else { continue };
625            let spelling = self.spelled(label);
626            let symbol = self.names.intern(&spelling);
627            self.renamed.insert(name, symbol);
628        }
629    }
630
631    /// The bytes of a string literal as a name, which is what a symbol in an attribute is.
632    fn spelled(&self, id: StrId) -> String {
633        self.tast[id].elements.iter().filter_map(|&unit| char::from_u32(unit)).collect()
634    }
635
636    /// One object with static storage duration.
637    fn object(&mut self, decl: DeclId) {
638        let tast = self.tast;
639        let node = &tast[decl];
640        let (ty, state, init) = (node.ty, node.state, node.init);
641        let (linkage, duration, alignment) = (node.linkage, node.duration, node.alignment);
642        let span = tast.decl_span(decl);
643        if duration == StorageDuration::Automatic {
644            // A block-scope object with automatic storage is a slot or a value in the function
645            // that declares it, and the body is what makes it. Nothing is emitted here.
646            return;
647        }
648        // A second name for something else is not an object of its own, so nothing is laid out
649        // and no image is built. It is held back until the rest of the file has been walked,
650        // because what it points at may be below it.
651        if node.alias.is_some() {
652            self.aliases.push(decl);
653            return;
654        }
655
656        let symbol = self.symbol_of(decl);
657        let size = repr::size_of(self.types, self.target, ty);
658        let align = alignment.unwrap_or_else(|| repr::align_of(self.types, self.target, ty));
659        let mut global = Global::new(symbol, size, align);
660        global.linkage = self.told(decl, linkage);
661        // A tentative definition counts as one, because it is one: `int x;` at file scope puts a
662        // symbol in this object and the linker never has to look anywhere else for it.
663        global.visibility = self.seen(decl, state != Definition::Declared);
664        global.tls = (duration == StorageDuration::Thread).then_some(TlsModel::GlobalDynamic);
665        global.constant = repr::is_read_only(self.types, ty);
666        global.init = match state {
667            // `extern int x;` and nothing else names an object another translation unit
668            // defines. The global is here so that a reference to it has something to resolve
669            // against, and it has no image, which is what makes it a declaration.
670            Definition::Declared => None,
671            Definition::Tentative => Some(self.zeros(size)),
672            Definition::Defined => {
673                let (data, covered) = self.image(init, size, span);
674                // The object is as large as its image when the image is the larger of the two.
675                // A structure whose last member is a flexible array is the only way that
676                // happens: `sizeof` answers without the array and an initializer that fills it
677                // makes an object big enough to hold what was written. C 6.7.2.1p18 leaves the
678                // size to the implementation, gcc grows the object, and this does the same
679                // rather than hand the linker a size the image does not fit in.
680                global.size = size.max(covered);
681                Some(data)
682            }
683        };
684        self.place_global(global);
685    }
686
687    /// One function, with its body when it has one.
688    fn function(&mut self, decl: DeclId) {
689        let tast = self.tast;
690        let node = &tast[decl];
691        let (ty, linkage, body, align) = (node.ty, node.linkage, node.body, node.alignment);
692        let noreturn = node.flags.contains(DeclFlags::NORETURN);
693        let naked = node.flags.contains(DeclFlags::NAKED);
694        let twice = node.flags.contains(DeclFlags::RETURNS_TWICE);
695        let effects = node.effects;
696        let startup = node.startup;
697        let span = tast.decl_span(decl);
698        if node.name.is_none() {
699            return;
700        }
701        // The same as for an object: a second name is not a function of its own, and it is held
702        // back until what it points at has been emitted.
703        if node.alias.is_some() {
704            self.aliases.push(decl);
705            return;
706        }
707        // Which asks the one question the reference to it asks, so that a declaration that
708        // renamed the symbol renames the definition as well and the two still meet.
709        let name = self.symbol_of(decl);
710        if self.is_dropped(decl, name) {
711            return;
712        }
713        let Some(plan) = self.plan(ty, &[], span) else { return };
714
715        let mut func = Func::new(name, plan.signature.clone());
716        // The name the source spelled, where an assembler name says the symbol is not it. A
717        // declaration of `strstr` renamed to `my_strstr` is a declaration of `strstr` still, and
718        // once the symbol is the only name left there is nothing to find that out again from.
719        if node.asm_label.is_some() {
720            func.spelled = node.name.filter(|&spelled| spelled != name);
721        }
722        // Where the body begins, which is the line a debugger names over the prologue. gcc says the
723        // line the opening brace is on rather than the line the declarator is on, and the two
724        // differ in the style that puts the brace underneath. No instruction in a prologue has a
725        // span of its own, so this is the only place the fact can come from. A declaration has no
726        // body and produces no prologue, so it falls back to the declarator and nothing reads it.
727        func.declared = body.map_or(span, |body| tast.stmt_span(body));
728        // And where the name is written in the definition, which is what a report about the
729        // function as a whole points at. Not `span`, which is the first declaration and is a
730        // prototype at the top of the file as often as not.
731        func.named = tast.definition_span(decl);
732        // The larger of what this function asked for and what the command line asked of all of
733        // them, since the attribute is a requirement and the flag is a preference, and a
734        // preference does not get to move a function off a boundary its own source named.
735        func.align = match (align, self.align) {
736            (Some(mine), Some(everyones)) => Some(mine.max(everyones)),
737            (mine, everyones) => mine.or(everyones),
738        };
739        // The one thing a declaration says that nobody downstream can work out for themselves.
740        // What `abort` does belongs to `abort`, and a translation unit that only declares it has
741        // nothing to look at, so the claim has to travel on the declaration or not at all.
742        if noreturn {
743            func.attrs.set |= AttrSet::NORETURN;
744        }
745        // Which is not a claim about what a call to it does but a fact about how the function
746        // itself is written, so unlike the two around it there is nothing here for a declaration
747        // alone to be useful for. It travels the same way because the attribute is written in the
748        // same places. See [`rucc_codegen`] for what reads it, which is the frame.
749        if naked {
750            func.attrs.set |= AttrSet::NAKED;
751        }
752        // A claim about what a call to it does, like `noreturn`, and it has to travel for the same
753        // reason: `sigsetjmp` is only ever declared here, and the frame of whoever calls it is
754        // what the claim changes. See `rucc_codegen::tail::comes_back` for what reads it.
755        if twice {
756            func.attrs.set |= AttrSet::RETURNS_TWICE;
757        }
758        // And the other one, for the same reason. What a call to `strtol` reads belongs to
759        // `strtol`, and the purity analysis answers opaque for everything it cannot see a body
760        // for, so a unit that only declares the function gets nothing out of it unless the
761        // promise arrives here. `const` says the result comes from the arguments alone, which
762        // is `readnone`, and `pure` says it may read memory, which is `readonly`. The two are
763        // an incompatible pair in the IR and only one of them is ever set.
764        func.attrs.set |= match effects {
765            Effects::Any => AttrSet::NONE,
766            Effects::Pure => AttrSet::READONLY,
767            Effects::Const => AttrSet::READNONE,
768        };
769        // Whether to inline it, which `rucc_opt::inline` reads for `always_inline` at every level
770        // and for a plain `inline` from `-O1` up. The IR will not have both of the first two, so a
771        // name that said both gets the one gcc keeps, which is `always_inline` after a warning that
772        // the other was ignored, and either of them says more than the keyword does.
773        if node.flags.contains(DeclFlags::ALWAYS_INLINE) {
774            func.attrs.set |= AttrSet::ALWAYS_INLINE;
775        } else if node.flags.contains(DeclFlags::NOINLINE) {
776            func.attrs.set |= AttrSet::NOINLINE;
777        } else if node.flags.contains(DeclFlags::DECLARED_INLINE) {
778            func.attrs.set |= AttrSet::INLINE_HINT;
779        }
780        // How often it is called, which `rucc_opt::predict` reads off the callee to make the path
781        // to a call of a cold function the unlikely one. The IR will not have both, and a name
782        // that said both gets `cold`.
783        if node.flags.contains(DeclFlags::COLD) {
784            func.attrs.set |= AttrSet::COLD;
785        } else if node.flags.contains(DeclFlags::HOT) {
786            func.attrs.set |= AttrSet::HOT;
787        }
788        // What a `target` attribute said the function is built for, which the inliner compares
789        // against each caller: a body built for SSE4.2 is not copied into one that is not.
790        func.target = tast.target(decl);
791        // An inline definition this unit calls, which this unit puts a copy of out of line for
792        // every call the inliner leaves alone. See [`Self::out_of_line`].
793        let copied = body.is_some() && self.out_of_line(decl, node.inline);
794        // Under GNU's reading the copy is for the inliner and nothing else. See
795        // [`Self::out_of_line`].
796        if copied && node.flags.contains(DeclFlags::GNU_INLINE) {
797            func.attrs.set |= AttrSet::INLINE_ONLY;
798        }
799        func.linkage = if copied { IrLinkage::LinkOnce } else { self.told(decl, linkage) };
800        // The same question as for an object, and the same answer, with one wrinkle: an inline
801        // definition this unit neither emits nor calls is a declaration here, since C 6.7.4p7
802        // sends the calls to whatever unit holds the external definition, so it is not this
803        // file's to describe. That is the condition the body is lowered under, a few lines below.
804        func.visibility = self.seen(decl, body.is_some() && (node.inline.emits() || copied));
805        // An inline definition is not an external definition, so what goes in the module is the
806        // declaration and not the body. C 6.7.4p7 says the calls in this unit go to the definition
807        // some other unit holds, which is what the declaration gives them, and glibc's headers
808        // rely on it: every one of their inline definitions would otherwise be a second definition
809        // of a name the library already defines. Unless this unit is one of the callers, which is
810        // the case [`Self::out_of_line`] is about.
811        if body.is_some() && (node.inline.emits() || copied) {
812            // `optimize ("no-strict-aliasing")` on the function, which has to be kept in the IR
813            // rather than on the command line because the inliner may copy this body into a
814            // function that is under the rule.
815            let strict = self.aliasing;
816            self.aliasing &= !node.flags.contains(DeclFlags::NO_STRICT_ALIASING);
817            body::lower(self, decl, &mut func, &plan);
818            self.aliasing = strict;
819            // Only for a definition, because an entry is an address and a declaration of something
820            // another file defines has none to put there. gcc reads the attribute off whichever
821            // declaration carried it and then waits for the definition in the same way, which is
822            // why writing `__attribute__((constructor)) void f(void);` in a header costs every
823            // file that includes it nothing.
824            if let Some(priority) = startup.before {
825                self.starts.push(Start { func: name, before: true, priority, span });
826            }
827            if let Some(priority) = startup.after {
828                self.starts.push(Start { func: name, before: false, priority, span });
829            }
830        }
831        self.place_func(func);
832    }
833
834    /// Puts a function in the module under a name something may already be under.
835    ///
836    /// Two declarations of one identifier were merged before this, so the only way one name
837    /// arrives twice is an assembler name that renames one identifier onto another: a
838    /// declaration of `f` renamed to `g` beside a definition of `g` is one symbol written two
839    /// ways, which is what the program asked for and what the linker is going to see. The
840    /// definition wins wherever there is one, since what the declaration is here for is to give
841    /// the calls something to resolve against and the definition does that as well.
842    ///
843    /// A name already carrying a definition keeps it. That is the program defining one symbol
844    /// twice, and the assembler says so with the name in front of it, which is a better message
845    /// than anything available here.
846    fn place_func(&mut self, func: Func) {
847        match self.module.lookup(func.name) {
848            None => {
849                self.module.add_func(func);
850            }
851            Some(SymbolRef::Func(id))
852                if self.module[id].is_declaration() && !func.is_declaration() =>
853            {
854                self.module[id] = func;
855            }
856            Some(_) => {}
857        }
858    }
859
860    /// One declaration that is a second name for something the same file defines.
861    ///
862    /// Emitted after everything else, so the target is looked up in a module that already holds
863    /// whatever the file defines whether it was written above the alias or below it.
864    ///
865    /// The target has to be defined here and not merely declared, which is gcc's rule and is
866    /// what the object format can express: an alias is a symbol at another symbol's address, and
867    /// a name this file does not define has no address for one to be at. A program that writes
868    /// an alias of something in another object wants a reference rather than a definition, and
869    /// what it gets from gcc is this same error rather than a name the linker cannot resolve.
870    fn alias(&mut self, decl: DeclId) {
871        let Some(written) = self.tast[decl].alias else { return };
872        let span = self.tast.decl_span(decl);
873        let name = self.symbol_of(decl);
874        let spelling = self.spelled(written);
875        let target = self.names.intern(&spelling);
876        if self.no_address(name, target, span) {
877            return;
878        }
879        // Something already under this name, which is the program defining one symbol twice. The
880        // definition that is there stands, the way it does for a function and for an object.
881        if self.module.lookup(name).is_some() {
882            return;
883        }
884        let mut alias = Alias::new(name, target);
885        alias.linkage = self.told(decl, self.tast[decl].linkage);
886        // Its own answer, because the attribute is written on the alias and an alias is a symbol
887        // of its own. `weak, alias, visibility("hidden")` is a name a library keeps to itself
888        // while the thing it points at stays exported, which is how glibc writes half of them.
889        // Always a definition. An alias is a symbol this object puts at an address in this object,
890        // and one whose target is merely declared was refused a few lines above.
891        alias.visibility = self.seen(decl, true);
892        self.module.add_alias(alias);
893    }
894
895    /// One name a `.set` in an `asm` at file scope gave to something else.
896    ///
897    /// The same thing as the alias above it and written out the same way, with the two answers
898    /// about the name coming from the directives around the `.set` rather than from an attribute:
899    /// `.globl` and `.weak` say how the linker sees it, `.hidden` and `.protected` say how far it
900    /// reaches, and a name no directive spoke about is local, which is what an assembler does with
901    /// one. A name the file also defines keeps its own definition, which is the rule everything
902    /// else here follows and is what gcc's output shows for a `.set` written above a definition of
903    /// the same name.
904    ///
905    /// A name made by `.symver` is bound the way the name it stands for is, which is what gas
906    /// does, since no directive can speak about a name spelled with an `@` in it.
907    fn equated(&mut self, set: &directives::Set, span: Span) {
908        let name = self.names.intern(&set.name);
909        let mut target = self.names.intern(&set.target);
910        // A target that is itself an alias stands for what that one stands for. xz gives a version
911        // to a name it declared with the alias attribute, and every alias the attributes made was
912        // added before the first of these, so the chain is already there to follow. The count
913        // bounds a cycle, which the attributes refused where they were written.
914        for _ in 0..self.module.aliases().count() {
915            match self.module.lookup(target) {
916                Some(SymbolRef::Alias(id)) if self.module[id].kind == AliasKind::Alias => {
917                    target = self.module[id].target;
918                }
919                _ => break,
920            }
921        }
922        if self.no_address(name, target, span) {
923            return;
924        }
925        // A name the file only declared is one the `.set` gives an address to, and tcc's test
926        // calls a function declared `extern` in C and defined by `.set` in a file-scope `asm`.
927        let declared = match self.module.lookup(name) {
928            None => true,
929            Some(SymbolRef::Func(id)) => self.module[id].is_declaration(),
930            Some(SymbolRef::Global(id)) => self.module[id].is_declaration(),
931            Some(SymbolRef::Alias(_)) => false,
932        };
933        if !declared {
934            return;
935        }
936        let mut alias = Alias::new(name, target);
937        alias.linkage = set.linkage;
938        alias.visibility = set.visibility;
939        if set.versioned {
940            (alias.linkage, alias.visibility) = match self.module.lookup(target) {
941                Some(SymbolRef::Func(id)) => (self.module[id].linkage, self.module[id].visibility),
942                Some(SymbolRef::Global(id)) => {
943                    (self.module[id].linkage, self.module[id].visibility)
944                }
945                _ => (set.linkage, set.visibility),
946            };
947        }
948        self.module.add_alias_over(alias);
949    }
950
951    /// Whether there is no address for a second name to be at, reporting why when there is not.
952    ///
953    /// The target has to be defined here and not merely declared, because an alias is a symbol at
954    /// another symbol's address and a name this file does not define has no address in it. A
955    /// program that writes one of these about something in another object wants a reference rather
956    /// than a definition, and gcc turns that down as well.
957    fn no_address(&mut self, name: Symbol, target: Symbol, span: Span) -> bool {
958        let spelled = self.names.resolve(name).to_owned();
959        if name == target {
960            let what = format!("'{spelled}' is aliased to itself");
961            self.diagnostics.push(Diagnostic::error(what, span).with_code("E0697"));
962            return true;
963        }
964        let defined = match self.module.lookup(target) {
965            Some(SymbolRef::Func(id)) => !self.module[id].is_declaration(),
966            Some(SymbolRef::Global(id)) => self.module[id].init.is_some(),
967            // A chain of them is a thing gcc takes and this does not yet, because resolving one
968            // wants the aliases put in an order that the file they were written in need not be
969            // in. It is reported rather than written out as a name pointing at a name.
970            Some(SymbolRef::Alias(_)) | None => false,
971        };
972        if !defined {
973            let spelling = self.names.resolve(target).to_owned();
974            let what = format!("'{spelled}' is aliased to undefined symbol '{spelling}'");
975            let note = "the target of an alias has to be defined in this same file, since an \
976                        alias is a second name for an address and not a reference to one";
977            let refused = Diagnostic::error(what, span).with_code("E0697");
978            self.diagnostics.push(refused.note(note, span));
979            return true;
980        }
981        false
982    }
983
984    /// The list of functions to run around `main`, written out as the entries that run them.
985    ///
986    /// In priority order rather than in the order the file defined them, because two of the three
987    /// formats get their order from the order the entries are in and only ELF sorts anything at
988    /// link time.
989    fn startups(&mut self) {
990        let mut starts = std::mem::take(&mut self.starts);
991        starts.sort_by_key(Start::order);
992        for start in starts {
993            self.start_entry(&start);
994        }
995    }
996
997    /// One entry, which is a pointer wide object in the section the format runs.
998    ///
999    /// A relocation against the function rather than a value, since the address is not known until
1000    /// the link. The object has internal linkage and a name nothing refers to: the only thing that
1001    /// reads it is the CRT walking the section, which finds it by where it is and not by what it is
1002    /// called. gcc emits no symbol at all for one, and a name with a dot in it is the nearest thing
1003    /// to that here, being one no C program can write and therefore one no program collides with.
1004    fn start_entry(&mut self, start: &Start) {
1005        let Some(section) = self.start_section(start) else {
1006            self.no_start(start);
1007            return;
1008        };
1009        let size = u64::from(self.target.pointer_width / 8);
1010        let align = u32::try_from(size).unwrap_or(1);
1011        let called = self.names.resolve(start.func).to_owned();
1012        let which = if start.before { "ctor" } else { "dtor" };
1013        let name = self.names.intern(&format!("__rucc_{which}.{called}"));
1014        let section = self.names.intern(&section);
1015        let mut global = Global::new(name, size, align);
1016        global.linkage = IrLinkage::Internal;
1017        global.section = Some(section);
1018        let size = u32::try_from(size).unwrap_or(0);
1019        let reloc = self.module.add_reloc(Reloc { symbol: start.func, addend: 0, size });
1020        global.init = Some(self.module.push_data(&[Datum::Addr(reloc)]));
1021        self.place_global(global);
1022    }
1023
1024    /// The pointer to the personality routine every landing pad in this unit is run by, which the
1025    /// header of the unwind table reaches through. Made once however many pads there are.
1026    ///
1027    /// gcc makes it a hidden weak object in a group of its own, so that every object of a link
1028    /// shares one. This one is local to the unit, which costs a word per object and needs neither
1029    /// a group nor a weak definition, and what an unwinder reads through it is the same address.
1030    /// It is a pointer rather than the routine itself because the routine is in the C runtime's
1031    /// shared library, and a table in a read only section cannot hold the distance to a name the
1032    /// dynamic linker places: the header holds the distance to this, and this holds the address.
1033    pub(crate) fn personality(&mut self) {
1034        let name = self.names.intern(rucc_ir::PERSONALITY_REF);
1035        if self.module.lookup(name).is_some() {
1036            return;
1037        }
1038        let size = u64::from(self.target.pointer_width / 8);
1039        let align = u32::try_from(size).unwrap_or(1);
1040        let mut global = Global::new(name, size, align);
1041        global.linkage = IrLinkage::Internal;
1042        let routine = self.names.intern(rucc_ir::PERSONALITY);
1043        let size = u32::try_from(size).unwrap_or(0);
1044        let reloc = self.module.add_reloc(Reloc { symbol: routine, addend: 0, size });
1045        global.init = Some(self.module.push_data(&[Datum::Addr(reloc)]));
1046        self.module.add_global(global);
1047    }
1048
1049    /// The section an entry goes in, and [`None`] for a format with no way to ask for one.
1050    ///
1051    /// ELF has both halves and the linker sorts the numbered sections ahead of the plain one, so
1052    /// the number goes in the name and the order comes out right however the files were linked.
1053    ///
1054    /// COFF has the run-up only. The name is sorted by what follows the `$` and the CRT walks
1055    /// everything between the `.CRT$XCA` and `.CRT$XCZ` markers, so a numbered entry goes just
1056    /// after the first marker and an unnumbered one at `U`, which keeps the numbered ones first.
1057    ///
1058    /// Mach-O has the run-up only as well, and it has no sorting at all: the entries run in the
1059    /// order the section holds them, which is the order [`Self::startups`] put them in.
1060    fn start_section(&self, start: &Start) -> Option<String> {
1061        match self.target.object_format {
1062            ObjectFormat::Elf => {
1063                let base = if start.before { ".init_array" } else { ".fini_array" };
1064                Some(match start.priority {
1065                    Priority::Numbered(number) => format!("{base}.{number:05}"),
1066                    Priority::Unnumbered => base.to_owned(),
1067                })
1068            }
1069            ObjectFormat::Coff if start.before => Some(match start.priority {
1070                Priority::Numbered(number) => format!(".CRT$XCA{number:05}"),
1071                Priority::Unnumbered => ".CRT$XCU".to_owned(),
1072            }),
1073            ObjectFormat::MachO if start.before => {
1074                Some("__DATA,__mod_init_func,mod_init_funcs".to_owned())
1075            }
1076            ObjectFormat::Coff | ObjectFormat::MachO | ObjectFormat::Wasm => None,
1077        }
1078    }
1079
1080    /// Reports an attribute this format has nowhere to put.
1081    ///
1082    /// Refused rather than dropped, because the whole point of the attribute is that something
1083    /// else calls the function and a program that quietly does not get its call has no way of
1084    /// noticing until whatever the function set up is missing.
1085    ///
1086    /// The run-down is what is missing on the two formats that have a run-up. Mach-O used to have
1087    /// a terminator list and dyld stopped running it, so clang registers the call with
1088    /// `__cxa_atexit` from a constructor it writes for the purpose, and nothing in the CRT a COFF
1089    /// target links against has been confirmed to walk one either. Doing the same here is a
1090    /// feature rather than a section name, which is why this is a message and not a branch above.
1091    fn no_start(&mut self, start: &Start) {
1092        let which = if start.before { "constructor" } else { "destructor" };
1093        let format = self.target.object_format.as_str();
1094        let what = format!("the '{which}' attribute on a {format} target");
1095        self.unsupported(&what, start.span);
1096    }
1097
1098    /// How far a name reaches outside a shared library, which is what a declaration of it said
1099    /// where one said anything and what the command line asked for where none did.
1100    ///
1101    /// gcc's `-fvisibility=` is written as the default rather than as an override, so the
1102    /// attribute wins wherever it was written, and that is the whole reason a library compiled
1103    /// with `-fvisibility=hidden` can still export the dozen names it means to export.
1104    ///
1105    /// The default reaches what this unit defines and stops there, which is the `defined`
1106    /// argument and is the whole of tamnd/rucc#1234. `-fvisibility=hidden` is a claim about the
1107    /// names this file puts into the library, and a name it only mentions is one it knows nothing
1108    /// about: `stderr` is in libc however the file that reads it was compiled, and calling it
1109    /// hidden tells the linker to resolve it inside this object, which it cannot do. The attribute
1110    /// on a declaration is a different thing and still counts, because a program that writes it
1111    /// has said where the definition is going to come from.
1112    ///
1113    /// Measured against gcc 16.2.0 rather than read off the manual, since the manual says the flag
1114    /// applies to declarations and does not say which ones. For `extern int plain;` beside
1115    /// `__attribute__((visibility("hidden"))) extern int marked;` at `-fPIC -fvisibility=hidden`,
1116    /// gcc writes `plain` as `GLOBAL DEFAULT UND` and reaches it through the global offset table,
1117    /// and writes `marked` as `GLOBAL HIDDEN UND` and reaches it from the instruction pointer.
1118    fn seen(&self, decl: DeclId, defined: bool) -> IrVisibility {
1119        match self.tast[decl].visibility {
1120            Some(Visibility::Default) => IrVisibility::Default,
1121            Some(Visibility::Hidden) => IrVisibility::Hidden,
1122            Some(Visibility::Protected) => IrVisibility::Protected,
1123            None if defined => self.visibility,
1124            None => IrVisibility::Default,
1125        }
1126    }
1127
1128    /// What the linker is told about a name, which is its C linkage unless a declaration of it
1129    /// wrote `weak`.
1130    ///
1131    /// The attribute is refused on internal linkage where it is read, so external is the only
1132    /// thing it can change, and the two things a program means by it are one thing to the linker.
1133    /// On a definition it says another object's definition of the name beats this one, which is
1134    /// how a library ships a default. On a reference to something this file does not define it
1135    /// says the link may leave the name undefined and hand the reference a zero address, which is
1136    /// how a library offers a hook and why zstd's thirty files link at all.
1137    fn told(&self, decl: DeclId, linkage: Linkage) -> IrLinkage {
1138        match linkage {
1139            Linkage::External if self.tast[decl].flags.contains(DeclFlags::WEAK) => IrLinkage::Weak,
1140            Linkage::External => IrLinkage::External,
1141            Linkage::Internal | Linkage::None => IrLinkage::Internal,
1142        }
1143    }
1144
1145    /// Whether a body this unit is not meant to emit has to be emitted anyway, because this unit
1146    /// calls it and has nothing else to send the call to.
1147    ///
1148    /// C 6.7.4p7 says an inline definition is not an external definition, and the bargain it
1149    /// offers is that the call is replaced by the body, so nobody ever has to resolve the name.
1150    /// A compiler that inlines keeps its end of it. This one inlines only a function marked
1151    /// `always_inline`, so any other call left standing is a call to a name no object file
1152    /// defines, and the program fails at the link on a function it can see the body of. micropython is a program that does exactly that:
1153    /// `py/misc.h` writes `MP_COMPRESSED_ROM_TEXT` as `inline __attribute__((always_inline))`,
1154    /// nothing anywhere defines it out of line, and every file that reports an error calls it.
1155    ///
1156    /// So a copy goes out of line, under [`IrLinkage::LinkOnce`]. Every unit that calls one emits
1157    /// its own copy of the same body, the linker keeps one and the rest are discarded, and a unit
1158    /// that holds the real external definition beats all of them because a strong definition
1159    /// beats a weak one. What that costs is object size in the units that call one. What it buys
1160    /// is that the address of the function is the same everywhere and that the program links,
1161    /// which is the whole of what the program was asking for.
1162    ///
1163    /// Only when this unit names it, which is why [`reach`] stopped treating one of these as a
1164    /// root. An unreferenced inline definition is still emitted as nothing at all, which is what
1165    /// keeps a file that includes `stdio.h` from carrying its own `vprintf`, `putchar`, `getchar`
1166    /// and the dozen more glibc writes beside them.
1167    ///
1168    /// Under GNU's reading of `inline` the copy is marked `inline_only`, so the inliner still has
1169    /// the body and nothing emits it. There `extern inline` promises that the external definition
1170    /// is in some other object, and gcc never emits one of these bodies. glibc's `_FORTIFY_SOURCE`
1171    /// wrappers are exactly that: `memcpy` is written `extern inline` under `gnu_inline` with a
1172    /// body that calls `__builtin___memcpy_chk`, which becomes a call to `memcpy` again. That body
1173    /// emitted is a `memcpy` that calls itself, and it wins over the C library's inside the shared
1174    /// object that holds it.
1175    fn out_of_line(&self, decl: DeclId, emission: Emission) -> bool {
1176        !emission.emits() && self.reachable.contains(&decl)
1177    }
1178
1179    /// The same for an object, where a global with no image is the declaration.
1180    fn place_global(&mut self, global: Global) {
1181        match self.module.lookup(global.name) {
1182            None => {
1183                self.module.add_global(global);
1184            }
1185            Some(SymbolRef::Global(id))
1186                if self.module[id].init.is_none() && global.init.is_some() =>
1187            {
1188                self.module[id] = global;
1189            }
1190            Some(_) => {}
1191        }
1192    }
1193
1194    /// Whether this function is one nothing can call, which is the set that is not emitted.
1195    ///
1196    /// A name with internal linkage is not visible to another translation unit, so a definition
1197    /// of one that nothing here refers to is a definition of something that can never run.
1198    /// [`reach`](mod@crate::reach) is what worked out which those are, and an attribute that asks
1199    /// for the definition to be kept has already been read into the answer.
1200    ///
1201    /// A second name for it is the one reason to keep it that the walk over the tree cannot see,
1202    /// since what an alias points at is a string and not a reference to anything. So the symbol
1203    /// is what is asked about here rather than the declaration: an alias names what the linker
1204    /// will look for, which is what a declaration that renamed itself with `__asm__` is under.
1205    ///
1206    /// Nothing is said about it. gcc has `-Wunused-function` for a `static` function nobody
1207    /// wrote a call to, which is a warning about the program, and this is not that: the header
1208    /// that defines six of them is not the file being compiled and its author is not the person
1209    /// reading the output.
1210    fn is_dropped(&self, decl: DeclId, symbol: Symbol) -> bool {
1211        self.tast[decl].linkage != Linkage::External
1212            && !self.reachable.contains(&decl)
1213            && !self.aliased.contains(&symbol)
1214    }
1215
1216    /// How everything a call to this function type hands over travels, and [`None`] for one the
1217    /// walk cannot make.
1218    ///
1219    /// `actual` is the types of the arguments at a call site, which matter only past the end of
1220    /// the prototype: what a variadic argument does is decided from what was written there, and
1221    /// there is no parameter to decide it from. A definition passes nothing for it.
1222    pub(crate) fn plan(&mut self, ty: TypeId, actual: &[TypeId], span: Span) -> Option<Plan> {
1223        self.plan_with(ty, actual, false, span)
1224    }
1225
1226    /// The same, as the call site sees it rather than as the function does.
1227    ///
1228    /// The two differ for a type that is not a prototype. An old style definition is the one of
1229    /// those that knows what its parameters are, and 6.5.2.2p6 checks a call against a prototype
1230    /// and against nothing at all otherwise, so a parameter it disagrees with does not make the
1231    /// call wrong and cannot be what the argument travels as either: the value at the call is
1232    /// the argument's own type and nothing converted it. So a parameter the argument facing it
1233    /// is compatible with is used, which is the usual case and is what makes the call go to the
1234    /// name, and one it is not compatible with gives way to what was actually written. A call
1235    /// like that is undefined behaviour if control reaches it and the file still has to
1236    /// translate, which is the same position [`Body::direct`](crate::body) already takes.
1237    pub(crate) fn call_plan(&mut self, ty: TypeId, actual: &[TypeId], span: Span) -> Option<Plan> {
1238        self.plan_with(ty, actual, true, span)
1239    }
1240
1241    fn plan_with(
1242        &mut self,
1243        ty: TypeId,
1244        actual: &[TypeId],
1245        at_call: bool,
1246        span: Span,
1247    ) -> Option<Plan> {
1248        let canonical = self.types.canonical(ty);
1249        let canonical = match self.types.kind(canonical) {
1250            // A call goes through a pointer to a function, and the type in hand may be either.
1251            TypeKind::Pointer(pointee) => self.types.canonical(pointee),
1252            _ => canonical,
1253        };
1254        let TypeKind::Function(id) = self.types.kind(canonical) else {
1255            self.unsupported("a call through something that is not a function", span);
1256            return None;
1257        };
1258        let signature = self.types.signature(id);
1259        let ret = signature.ret;
1260        // A function declared without a prototype takes what it is given, which is what a
1261        // signature with no parameters and no end to them says. C23 removed these and this is
1262        // what `int f();` means in every dialect before it.
1263        let variadic = signature.variadic || !signature.prototyped;
1264        let params = if at_call && !signature.prototyped {
1265            // An argument past the end of the list has no parameter to travel as, which is what
1266            // a call to an unprototyped function with more arguments than the definition takes
1267            // is, so the list ends where the arguments do.
1268            signature
1269                .params
1270                .iter()
1271                .zip(actual)
1272                .map(|(&param, &arg)| if compatible(self.types, param, arg) { param } else { arg })
1273                .collect()
1274        } else {
1275            signature.params.clone()
1276        };
1277
1278        match abi::plan(self.types, self.target, ret, &params, actual, variadic) {
1279            Ok(plan) => Some(plan),
1280            Err(what) => {
1281                self.unsupported(what, span);
1282                None
1283            }
1284        }
1285    }
1286
1287    /// The image of an initializer: the entries in ascending order, with the gaps zeroed, and
1288    /// how many bytes it covers.
1289    ///
1290    /// The count is the size that was asked for except when a flexible array member was given
1291    /// something to hold, which is the one case where an image is larger than the type it is an
1292    /// image of.
1293    pub(crate) fn image(
1294        &mut self,
1295        init: Option<InitList>,
1296        size: u64,
1297        span: Span,
1298    ) -> (DataList, u64) {
1299        let Some(init) = init else { return (self.zeros(size), size) };
1300        let (data, at) = self.pieces(init, size, span);
1301        (self.module.push_data(&data), at)
1302    }
1303
1304    /// The data an image is made of, before it becomes a [`DataList`].
1305    ///
1306    /// This is apart from [`Self::image`] so that an image can be built inside another one,
1307    /// which is what a compound literal used as a value in an initializer needs.
1308    fn pieces(&mut self, init: InitList, size: u64, span: Span) -> (Vec<Datum>, u64) {
1309        let entries = self.in_image_order(&self.tast[init]);
1310        let mut packed = self.packed(&entries, size);
1311        let mut data: Vec<Datum> = Vec::with_capacity(entries.len());
1312        let mut at = 0;
1313        for entry in entries {
1314            let piece = self.entry(entry, &mut packed, size);
1315            if piece.is_empty() {
1316                continue;
1317            }
1318            let covered: u64 = piece.iter().map(|datum| datum.size(&self.module)).sum();
1319            match entry.offset.cmp(&at) {
1320                Ordering::Greater => data.push(Datum::Zero(entry.offset - at)),
1321                // An entry that begins inside the one before it, which is neither the same
1322                // place nor a later one. A union whose members are initialized through two
1323                // designators is the way to write it. The earlier bytes are already in the
1324                // list and the image cannot take them out again, so this is refused, and
1325                // nothing here is wrong enough to drop the rest of the image.
1326                Ordering::Less => {
1327                    self.unsupported("an initializer that writes over an earlier one", span);
1328                    continue;
1329                }
1330                Ordering::Equal => {}
1331            }
1332            at = entry.offset + covered;
1333            data.extend(piece);
1334        }
1335        if at < size {
1336            // The tail of a partly initialized object, which C says is zero. So is the tail of
1337            // an array the initializer did not fill, and so is every byte of padding.
1338            data.push(Datum::Zero(size - at));
1339            at = size;
1340        }
1341        (data, at)
1342    }
1343
1344    /// The entries an image is written from, which is not the order they were written in.
1345    ///
1346    /// A designator names a place, and the places may be named in any order at all:
1347    /// `{ .b = 2, .a = 1 }` is the same object as `{ .a = 1, .b = 2 }` and C says so in as many
1348    /// words. An image is bytes in ascending order, so the entries are put in that order here.
1349    /// The sort is stable, which is what makes the rest of the rule work: naming one place
1350    /// twice is legal and the last of them is the one that stands, so among the entries at one
1351    /// offset the written order is kept and all but the last are dropped.
1352    ///
1353    /// A bit-field is never dropped, because several of them share one offset without writing
1354    /// over anything. Which bytes they came to is settled by [`Self::packed`] before this runs
1355    /// and the whole run goes in under the first entry that has a bit in it.
1356    fn in_image_order(&self, entries: &[InitEntry]) -> Vec<InitEntry> {
1357        let mut sorted = entries.to_vec();
1358        sorted.sort_by_key(|entry| entry.offset);
1359        let mut kept: Vec<InitEntry> = Vec::with_capacity(sorted.len());
1360        for entry in sorted {
1361            if !entry.is_bit_field() {
1362                let over = |last: &InitEntry| last.offset == entry.offset && !last.is_bit_field();
1363                while kept.last().is_some_and(over) {
1364                    kept.pop();
1365                }
1366            }
1367            kept.push(entry);
1368        }
1369        kept
1370    }
1371
1372    /// What one entry of an initializer puts in the image.
1373    ///
1374    /// A bit-field is not a datum of its own, because two of them can live in one byte and an
1375    /// image is written in bytes. They were put together into their bytes by [`Self::packed`]
1376    /// before this ran, and the whole run of bytes goes in under the first entry that lies in
1377    /// it, which is why a later one in the same run answers with nothing.
1378    ///
1379    /// The zeroes at the end of a run are left off it, and a run that is nothing but zeroes
1380    /// answers with nothing at all. Either way the gap before the next entry covers them, which
1381    /// is the same image and is a smaller one to carry, and it is what keeps an object whose
1382    /// bit-fields are all zero in `.bss`. A zero at the front of a run or inside one stays, since
1383    /// that is where the run starts and what makes it one run. The run comes out of the map
1384    /// whatever is in it, so a later entry lying in it answers with nothing for the usual reason
1385    /// rather than writing the run a second time.
1386    ///
1387    /// An entry is usually one datum and a compound literal read is the reason the answer is a
1388    /// list: that entry is a whole object and puts as many data in as the object it is.
1389    fn entry(&mut self, entry: InitEntry, packed: &mut BTreeMap<u64, u8>, size: u64) -> Vec<Datum> {
1390        if entry.is_bit_field() {
1391            let Some(bytes) = take_run(packed, entry.offset) else { return Vec::new() };
1392            let Some(last) = bytes.iter().rposition(|&byte| byte != 0) else { return Vec::new() };
1393            return vec![Datum::Bytes(self.module.push_bytes(&bytes[..=last]))];
1394        }
1395        if let Some(literal) = self.literal_read(entry.value) {
1396            return self.literal_image(literal, self.tast.expr_span(entry.value));
1397        }
1398        if entry.reverse {
1399            if let Some(reversed) = self.reversed_datum(entry) {
1400                return reversed;
1401            }
1402        }
1403        // How much room is left in the object, which is what a string literal longer than the
1404        // array it initializes is cut down to. An entry that begins where the object ends is the
1405        // initializer of a flexible array member, and there the object grows to hold what was
1406        // written rather than the value being cut to fit, so nothing is taken off it.
1407        let room = if entry.offset < size { size - entry.offset } else { u64::MAX };
1408        if let Some(halves) = self.complex_image(entry.value) {
1409            return halves;
1410        }
1411        self.datum(entry.value, room).into_iter().collect()
1412    }
1413
1414    /// A complex constant as the two data an image holds it in, and [`None`] for anything else.
1415    ///
1416    /// A complex value is two real ones and an image is bytes, so `1.0 + 2.0i` goes in as the two
1417    /// halves one after the other, which is the layout every ABI here already reads it as. It is
1418    /// two data rather than one because a datum is one scalar, and it is here rather than in
1419    /// [`Self::datum`] for the same reason.
1420    fn complex_image(&mut self, value: ExprId) -> Option<Vec<Datum>> {
1421        let ty = self.tast[value].ty;
1422        let part = rucc_types::real_part(self.types, ty)?;
1423        let span = self.tast.expr_span(value);
1424        // Everything below this point answers with something, because the folding reports its own
1425        // failure and asking for the value a second time would report it twice.
1426        let folded = match self.fold(value) {
1427            Some(folded) => folded,
1428            None => return Some(Vec::new()),
1429        };
1430        let Some(ty) = repr::value_type(self.types, self.target, part) else {
1431            self.unsupported("this complex initializer", span);
1432            return Some(Vec::new());
1433        };
1434        // Each half goes in as the half's own type would, which is the bits of a floating value
1435        // and the number of an integer one.
1436        let halves = match folded {
1437            Const::Complex { real, imag } => {
1438                [real, imag].map(|half| Imm::from_bits(half.to_bits()))
1439            }
1440            Const::ComplexInt { real, imag } => [real, imag].map(|half| Imm::int(half, ty)),
1441            _ => {
1442                self.unsupported("this complex initializer", span);
1443                return Some(Vec::new());
1444            }
1445        };
1446        let data = halves
1447            .into_iter()
1448            .map(|half| {
1449                let imm = self.module.add_imm(half);
1450                Datum::Scalar { ty, value: imm }
1451            })
1452            .collect();
1453        Some(data)
1454    }
1455
1456    /// The compound literal an entry reads, if that is what the entry is.
1457    ///
1458    /// Reading an object is a node of its own, so a literal used as a value comes through as a
1459    /// read of a literal. A literal whose address is taken is not a read and is not this: that
1460    /// one folds to an address and goes in as a relocation, with the object it points at emitted
1461    /// on its own.
1462    fn literal_read(&self, value: ExprId) -> Option<DeclId> {
1463        let ExprKind::Convert { kind: Conversion::Lvalue, operand } = self.tast[value].kind else {
1464            return None;
1465        };
1466        match self.tast[operand].kind {
1467            ExprKind::CompoundLiteral(decl) => Some(decl),
1468            _ => None,
1469        }
1470    }
1471
1472    /// The bytes a compound literal contributes where it is read, which are its own image.
1473    ///
1474    /// The literal has static storage duration here, since a file-scope initializer is the only
1475    /// place this is reached from, and C 6.7.11p4 is what lets it stand as a constant element.
1476    /// Its own initializer is built at the offset the entry is at, so the parent image ends up
1477    /// with the literal's bytes laid into it rather than a name pointing at a second object.
1478    fn literal_image(&mut self, literal: DeclId, span: Span) -> Vec<Datum> {
1479        let size = repr::size_of(self.types, self.target, self.tast[literal].ty);
1480        let Some(init) = self.tast[literal].init else {
1481            return if size == 0 { Vec::new() } else { vec![Datum::Zero(size)] };
1482        };
1483        self.pieces(init, size, span).0
1484    }
1485
1486    /// The bit-fields of an initializer, put together into the bytes they lie in.
1487    ///
1488    /// Every byte a field lies in is in the map, whatever the bits it put there are. It is
1489    /// tempting to leave a zero byte out, on the grounds that what an image does not say is zero
1490    /// anyway, and it is wrong: the run a field's bytes make is taken out of the map from the
1491    /// byte the field starts at, so a field whose first byte happens to be zero would have its
1492    /// whole run left behind and `struct { unsigned f : 20; } x = { 0x12300 };` would read as
1493    /// zero. A run that is all zeroes is written as zeroes by [`Self::entry`], so an object that
1494    /// really is zero still costs nothing in the image.
1495    ///
1496    /// A field named twice takes only the bits of the field, so the last of them stands and does
1497    /// not read as the two values together.
1498    fn packed(&mut self, entries: &[InitEntry], size: u64) -> BTreeMap<u64, u8> {
1499        let mut bytes = BTreeMap::new();
1500        for entry in entries.iter().filter(|entry| entry.is_bit_field()) {
1501            let Some(folded) = self.fold(entry.value) else { continue };
1502            let Const::Int(number) = folded else {
1503                let span = self.tast.expr_span(entry.value);
1504                let what = "a bit-field initialized by something that is not an integer";
1505                self.unsupported(what, span);
1506                continue;
1507            };
1508            let width = entry.bit_width;
1509            let ones = if width >= 128 { u128::MAX } else { (1u128 << width) - 1 };
1510            // Which bytes the field lies in and where in them it sits. A reversed field lies in
1511            // the same bytes and is counted from the top of them, and the byte at its address is
1512            // then the most significant of the ones the value is assembled in rather than the
1513            // least, which is why the walk below runs the other way as well.
1514            let span = u64::from((entry.bit_offset + width).div_ceil(8));
1515            let start = if entry.reverse {
1516                u32::try_from(span * 8).unwrap_or(u32::MAX) - entry.bit_offset - width
1517            } else {
1518                entry.bit_offset
1519            };
1520            let mut mask = ones << start;
1521            let mut placed = ((number as u128) & ones) << start;
1522            let mut step = 0;
1523            while mask != 0 && step < span {
1524                let at = if entry.reverse {
1525                    entry.offset + span - 1 - step
1526                } else {
1527                    entry.offset + step
1528                };
1529                if at < size {
1530                    let (bits, keep) = ((placed & 0xff) as u8, !((mask & 0xff) as u8));
1531                    let byte = bytes.entry(at).or_insert(0);
1532                    *byte = (*byte & keep) | bits;
1533                }
1534                mask >>= 8;
1535                placed >>= 8;
1536                step += 1;
1537            }
1538        }
1539        bytes
1540    }
1541
1542    /// What one entry of a record whose scalars are stored the other way round puts in the image.
1543    ///
1544    /// The bytes of the value, written in the order opposite to the target's, which is the whole of
1545    /// what the attribute asks for. It answers with nothing where the ordinary path is already
1546    /// right: a value one byte wide has only one order, and an aggregate is bytes its own members
1547    /// put there in whatever order each of them is stored in.
1548    ///
1549    /// Two things are refused rather than written the wrong way. A complex value is two scalars and
1550    /// this is one, and an address is a number the linker fills in later and there is nowhere to
1551    /// say it goes in backwards. Both are worth an answer one day and neither is worth a wrong one.
1552    fn reversed_datum(&mut self, entry: InitEntry) -> Option<Vec<Datum>> {
1553        let ty = self.tast[entry.value].ty;
1554        let span = self.tast.expr_span(entry.value);
1555        if is_complex(self.types, ty) {
1556            let what = "a complex member of a record whose scalars are stored the other way round";
1557            self.unsupported(what, span);
1558            return Some(Vec::new());
1559        }
1560        let size = repr::size_of(self.types, self.target, ty);
1561        if size < 2 || !is_scalar(self.types, ty) {
1562            return None;
1563        }
1564        let bits = match self.fold(entry.value) {
1565            Some(Const::Int(number)) => number as u128,
1566            Some(Const::Float(number)) => number.to_bits(),
1567            Some(Const::Address(Address { base: Base::Absolute, offset })) => offset as u128,
1568            Some(_) => {
1569                let what = "an address in a record whose scalars are stored the other way round";
1570                self.unsupported(what, span);
1571                return Some(Vec::new());
1572            }
1573            None => return Some(Vec::new()),
1574        };
1575        let take = cap(size).min(16);
1576        let mut bytes = bits.to_le_bytes()[..take].to_vec();
1577        if self.target.little_endian {
1578            bytes.reverse();
1579        }
1580        Some(vec![Datum::Bytes(self.module.push_bytes(&bytes))])
1581    }
1582
1583    /// One entry of an image, given how many bytes are left in the object it goes in.
1584    fn datum(&mut self, value: ExprId, room: u64) -> Option<Datum> {
1585        let tast = self.tast;
1586        let ty = tast[value].ty;
1587        let span = tast.expr_span(value);
1588        if let TypeKind::Array { .. } = self.types.kind(self.types.canonical(ty)) {
1589            // An array in an initializer is a string literal initializing it, because that is
1590            // the only way an array is ever a value. `char s[2] = "hi";` drops the terminator,
1591            // which is the one case where the literal is longer than what it initializes, and
1592            // the front end has already given the value the type of the array it is filling, so
1593            // the type is what says how many of the literal's bytes are part of it. `room` is
1594            // still consulted because a flexible array member is filled by a literal that keeps
1595            // its own type and there is no size in the object for it to be cut to.
1596            let ExprKind::Str(id) = tast[value].kind else {
1597                self.unsupported("this initializer", span);
1598                return None;
1599            };
1600            let bytes = tast[id].bytes(self.target);
1601            let holds = repr::size_of(self.types, self.target, ty);
1602            let take = bytes.len().min(cap(holds)).min(cap(room));
1603            return Some(Datum::Bytes(self.module.push_bytes(&bytes[..take])));
1604        }
1605
1606        let size = repr::size_of(self.types, self.target, ty);
1607        match self.fold(value)? {
1608            Const::Int(number) => {
1609                let ty = repr::value_type(self.types, self.target, ty)?;
1610                // An integer constant of pointer type is a null pointer constant, which is what
1611                // `NULL` is, or an address the program wrote as a number. An image is bytes and
1612                // `ptr` says nothing about how many, so it goes in as the integer it is at the
1613                // width the target's addresses have. An address the linker has to fill in is
1614                // the arm below, and is the only one that stays a pointer.
1615                let ty = if ty.is_ptr() { Type::int(self.target.pointer_width) } else { ty };
1616                let imm = self.module.add_imm(Imm::int(number, ty));
1617                Some(Datum::Scalar { ty, value: imm })
1618            }
1619            Const::Float(number) => {
1620                let ty = repr::value_type(self.types, self.target, ty)?;
1621                let imm = self.module.add_imm(Imm::from_bits(number.to_bits()));
1622                Some(Datum::Scalar { ty, value: imm })
1623            }
1624            // A complex constant is two scalars and this answers with one, so it is not one of
1625            // these. [`Self::complex_image`] puts one in before this is reached.
1626            Const::Complex { .. } | Const::ComplexInt { .. } => None,
1627            // An address into nothing is a number, so it goes into the image as one and there is
1628            // no relocation for the linker to fill in. `static char *p = &((struct S *)0)->f;` is
1629            // a pointer whose value is known here, and the walk that folded it already said so.
1630            Const::Address(Address { base: Base::Absolute, offset }) => {
1631                let ty = repr::value_type(self.types, self.target, ty)?;
1632                let ty = if ty.is_ptr() { Type::int(self.target.pointer_width) } else { ty };
1633                let imm = self.module.add_imm(Imm::int(offset, ty));
1634                Some(Datum::Scalar { ty, value: imm })
1635            }
1636            // Two labels, both named for the image the way one is for `&&l`, and the width is
1637            // the type's since the distance is a number and not an address.
1638            Const::Apart { to, from } => {
1639                let to = self.label_name(to);
1640                let from = self.label_name(from);
1641                let size = u32::try_from(size).unwrap_or(0);
1642                let to = self.module.add_reloc(Reloc { symbol: to, addend: 0, size });
1643                Some(Datum::Apart { to, from })
1644            }
1645            Const::Address(address) => {
1646                let symbol = match address.base {
1647                    Base::Decl(decl) => {
1648                        // A compound literal is an object nothing declares, so the address of
1649                        // one is also the only thing that asks for it to be emitted. Without
1650                        // this the image names a symbol the module never defines and the link
1651                        // is what finds out. Anything with a name of its own is left alone,
1652                        // since the walk over the unit reaches those on its own.
1653                        if self.tast[decl].name.is_none() {
1654                            self.local_static(decl);
1655                        }
1656                        self.symbol_of(decl)
1657                    }
1658                    Base::Str(id) => self.string(id),
1659                    Base::Label(label) => self.label_name(label),
1660                    // Answered above, where it becomes a number rather than a reference.
1661                    Base::Absolute => return None,
1662                };
1663                let addend = i64::try_from(address.offset).unwrap_or(0);
1664                let size = u32::try_from(size).unwrap_or(0);
1665                Some(Datum::Addr(self.module.add_reloc(Reloc { symbol, addend, size })))
1666            }
1667        }
1668    }
1669
1670    /// An image of nothing but zeros, which is what a tentative definition has.
1671    fn zeros(&mut self, size: u64) -> DataList {
1672        if size == 0 {
1673            return DataList::EMPTY;
1674        }
1675        self.module.push_data(&[Datum::Zero(size)])
1676    }
1677
1678    /// The global a string literal is emitted as, making it the first time it is asked for.
1679    pub(crate) fn string(&mut self, id: StrId) -> Symbol {
1680        if let Some(&symbol) = self.strings.get(&id) {
1681            return symbol;
1682        }
1683        let literal = &self.tast[id];
1684        let bytes = literal.bytes(self.target);
1685        let align = literal.encoding.element_width(self.target) / 8;
1686        let symbol = self.names.intern(&format!(".Lstr.{}", self.strings.len()));
1687
1688        let mut global = Global::new(symbol, bytes.len() as u64, align.max(1));
1689        global.linkage = IrLinkage::Internal;
1690        // Not because the type says so, since a literal is an array of `char` and not of
1691        // `const char`, but because writing to one is undefined and every target puts them
1692        // somewhere read-only.
1693        global.constant = true;
1694        let range = self.module.push_bytes(&bytes);
1695        global.init = Some(self.module.push_data(&[Datum::Bytes(range)]));
1696        self.module.add_global(global);
1697        self.strings.insert(id, symbol);
1698        symbol
1699    }
1700
1701    /// The name a label an image holds the address of is known by, minting one the first time.
1702    ///
1703    /// The number is what makes two labels in two functions two names, the same way it does for a
1704    /// `static` inside a function. Nothing but the relocation and the definition the back end
1705    /// writes for it ever reads this, so the spelling only has to be one the object format lets a
1706    /// local symbol have, and the leading dot is what keeps it out of the symbol table on the
1707    /// formats that have the convention.
1708    pub(crate) fn label_name(&mut self, label: LabelId) -> Symbol {
1709        if let Some(&symbol) = self.labels.get(&label) {
1710            return symbol;
1711        }
1712        let symbol = self.names.intern(&format!(".Llbl.{}", self.labels.len()));
1713        self.labels.insert(label, symbol);
1714        symbol
1715    }
1716
1717    /// The name a label was given, or `None` for a label no image points at.
1718    pub(crate) fn named_label(&self, label: LabelId) -> Option<Symbol> {
1719        self.labels.get(&label).copied()
1720    }
1721
1722    /// The name the C library gives a function the program named with the `__builtin_` prefix,
1723    /// and nothing for every other name.
1724    ///
1725    /// `__builtin_abort` is a call to `abort`: the prefix is how a program reaches the function
1726    /// the library promises where a macro or a definition of its own has taken the plain name,
1727    /// so the two spellings are one function and the one the linker will look for is the short
1728    /// one. Which names those are is [`rucc_sema::library_name`]'s to say, since it is the same
1729    /// answer the front end declared them out of.
1730    fn library_name(&mut self, name: Symbol) -> Option<Symbol> {
1731        let library = rucc_sema::library_name(self.names.resolve(name))?;
1732        let symbol = self.names.intern(library);
1733        // And then whatever the file said that name is called in the object file. A program is
1734        // allowed to declare `memcpy` with an assembler name of its own and go on calling
1735        // `__builtin_memcpy`, and what it means by that is the renamed one: the prefix picks the
1736        // function out of the library, it does not ask for a symbol the file has renamed away.
1737        Some(self.renamed.get(&symbol).copied().unwrap_or(symbol))
1738    }
1739
1740    /// The symbol of one of the objects libgcc defines and the program only reads, declared in
1741    /// the module the first time it is asked for.
1742    ///
1743    /// These are `__cpu_model` and `__cpu_features2`, which `__builtin_cpu_supports` and
1744    /// `__builtin_cpu_is` read. The program never declares them, so without this the load would
1745    /// be from a name the module knows nothing about. The declaration is the one gcc makes, an
1746    /// external object with the ordinary visibility and no image, and a declaration the program
1747    /// did write of the same name is left as it is, since it says the same thing.
1748    pub(crate) fn libgcc_object(&mut self, name: &str, size: u64) -> Symbol {
1749        let symbol = self.names.intern(name);
1750        // External, the ordinary visibility and no image, which is what a new global is.
1751        self.place_global(Global::new(symbol, size, 4));
1752        symbol
1753    }
1754
1755    /// The name an object or a function is known by in the object file.
1756    pub(crate) fn symbol_of(&mut self, decl: DeclId) -> Symbol {
1757        let tast = self.tast;
1758        let node = &tast[decl];
1759        // The assembler name a declaration wrote, which is the symbol whatever the identifier
1760        // spells. It stands for a `static` and for a local one as well as for a name the linker
1761        // sees, so it is read before anything else here: a program that renames a name has said
1762        // what the symbol is, and the numbering below is for the ones that have not.
1763        if let Some(label) = node.asm_label {
1764            let spelling: String =
1765                tast[label].elements.iter().filter_map(|&unit| char::from_u32(unit)).collect();
1766            return self.names.intern(&spelling);
1767        }
1768        if node.linkage != Linkage::None {
1769            let Some(name) = node.name else { return self.names.intern(".Lanon") };
1770            return self.library_name(name).unwrap_or(name);
1771        }
1772        if let Some(&symbol) = self.statics.get(&decl) {
1773            return symbol;
1774        }
1775        // A `static` in a function, or a compound literal with static storage duration. The
1776        // number is what makes two of them in two functions two objects.
1777        let base = match node.name {
1778            Some(name) => self.names.resolve(name).to_string(),
1779            None => ".Lanon".to_string(),
1780        };
1781        let symbol = self.names.intern(&format!("{base}.{}", self.statics.len()));
1782        self.statics.insert(decl, symbol);
1783        symbol
1784    }
1785
1786    /// Emits the global for an object with static storage duration declared inside a function.
1787    pub(crate) fn local_static(&mut self, decl: DeclId) {
1788        if !self.done.insert(decl) {
1789            return;
1790        }
1791        match self.tast[decl].kind {
1792            // A function declared inside a body is a declaration of the function, not an
1793            // object with static storage that happens to be one.
1794            DeclKind::Function => self.function(decl),
1795            DeclKind::Object => self.object(decl),
1796            DeclKind::Type => {}
1797        }
1798    }
1799
1800    /// The value of a constant expression, reporting what folding it reported.
1801    ///
1802    /// Everything this is asked about is part of the image of an object that exists before the
1803    /// program runs, which is the one place C23 6.6p10 lets a compiler take more than the rest of
1804    /// 6.6 does, so it asks for the reading the front end already accepted there. Asking the
1805    /// strict way instead would refuse here what was allowed a pass earlier, which is a wrong
1806    /// answer arriving late rather than an extra check.
1807    fn fold(&mut self, expr: ExprId) -> Option<Const> {
1808        let mut eval = Eval::new(self.tast, self.types, self.target, self.names);
1809        let folded = eval.initializer(expr);
1810        let reported = eval.finish();
1811        self.diagnostics.extend(reported);
1812        match folded {
1813            Ok(value) => Some(value),
1814            Err(stop) => {
1815                if !stop.poisoned {
1816                    let span = self.tast.expr_span(stop.at);
1817                    self.unsupported("an initializer this compiler cannot fold", span);
1818                }
1819                None
1820            }
1821        }
1822    }
1823
1824    /// Reports a construct the walk does not build IR for yet.
1825    pub(crate) fn unsupported(&mut self, what: &str, span: Span) {
1826        self.diagnostics.push(
1827            Diagnostic::error(format!("{what} is not supported yet"), span).with_code("E0519"),
1828        );
1829    }
1830
1831    /// Reports a call to a builtin this compiler knows the name of and does nothing with.
1832    ///
1833    /// It is its own message rather than [`Self::unsupported`] because the construct is not the
1834    /// problem: a call is a call, and what is missing is the one function it goes to. The note is
1835    /// what a reader needs, since a builtin is the one name a programmer does not expect to have
1836    /// to provide and the alternative to this message is a linker asking them for it.
1837    pub(crate) fn missing_builtin(&mut self, spelled: &str, span: Span) {
1838        let message = format!("`{spelled}` is not implemented yet");
1839        let note = "a call to it would go to a symbol no object file defines, so this is refused \
1840                    here rather than at the link";
1841        self.diagnostics.push(Diagnostic::error(message, span).with_code("E0686").note(note, span));
1842    }
1843}
1844
1845/// A count of bytes as a length of a slice of them, saturating on a target whose addresses are
1846/// wider than this host's.
1847fn cap(bytes: u64) -> usize {
1848    usize::try_from(bytes).unwrap_or(usize::MAX)
1849}
1850
1851/// The run of bytes a bit-field entry starts, taken out of the map.
1852///
1853/// [`None`] when there is no byte at that offset, which means an earlier entry in the same run
1854/// already took it, since [`Unit::packed`] puts every byte a field lies in into the map.
1855fn take_run(bytes: &mut BTreeMap<u64, u8>, start: u64) -> Option<Vec<u8>> {
1856    let mut run = vec![bytes.remove(&start)?];
1857    let mut at = start + 1;
1858    while let Some(byte) = bytes.remove(&at) {
1859        run.push(byte);
1860        at += 1;
1861    }
1862    Some(run)
1863}