Skip to content

Compilation model (proposal)

P-E-P edited this page Oct 4, 2024 · 3 revisions

cargo-gccrs invokes gccrs once with -fPIC to get the crate metadata, an object file, an assembly file, ... as necessary/requested. For all emit types except link, the respective file generated by gccrs is directly passed to the user. For link depending on the crate type(s) one of more of the following will be done:

  • bin: Gcc will be invoked with the right arguments to link the final executable.
  • lib/rlib: The crate metadata, object file and if necessary LTO metadata are packaged in a single archive.
  • dylib: Gcc will be invoked with the right arguments to link a dylib with all symbols exported and the crate metadata embedded in the dylib.
  • cdylib: Gcc will be invoked with the right arguments to link a dylib with only #[no_mangle] symbols exported and crate metadata not embedded.
  • staticlib: The object file combined with the object files of all dependencies (rust and native archives) will be bundled in a single archive without crate metadata.
  • proc-macro: Gcc will be invoked with the right arguments to link a proc-macro. (will have to wait until gccrs supports proc macros)

Gccrs will then have to learn how to read crate metadata from rlibs and dylibs.

This proposal should solve the problem of having to invoke gcc multiple times when using multiple crate types or when using --emit obj,link or something like that. It is also somewhat close to what rustc internally does except that some logic is moved from rustc_codegen_ssa::back::link to cargo-gccrs. It may also be possible to implement this logic in the gcc driver so you have for example -frust-crate-type=bin,lib,dylib.

--

@bjorn3

(originally from Rust-GCC/cargo-gccrs#34)

---
title: GCCRS Graph
---
stateDiagram-v2
    Rust --> Tokens: Lexer
    Tokens --> AST: Parser
    state AST {
        [*] --> attr_checking
        attr_checking: Attribute Checker
        cfg_stripping: Cfg stripping
        toplevel_nr: Top level name resolution
        early_nr: Early name resolution
        validation: Validation
        late: Late name resolution
        feature_gating: Feature gating
        attr_checking --> cfg_stripping
        cfg_stripping --> toplevel_nr
        toplevel_nr --> early_nr
        early_nr --> expansion
        expansion --> cfg_stripping
        expansion --> validation
        validation --> feature_gating
        feature_gating --> late
        late --> [*]
    }
    state HIR {
        type_resolve: Type resolver
        privacy: Privacy checker
        safety: Safety checker
        const: Const checker
        dead: Dead code scanner
        [*] --> type_resolve
        type_resolve --> privacy
        privacy --> safety
        safety --> const
        const --> dead
        dead --> [*]
    }
    AST --> HIR: Lowering
    metadata: Crate metadata
    HIR --> metadata
    HIR --> Generic
    state Generic {
        unused: Unused variables checker
        readonly: Read only checker
        [*] --> unused
        unused --> readonly
        readonly --> [*]
    }
    state BIR {
        wrapper: Polonius wrapper
        polonius: Polonius
        wrapper --> polonius
    }
    HIR --> BIR
    Generic --> Gimple
    state RTL {
        CFGC: Control flow graph cleanup
        fwd_prop: Forward propagation
        loop: Loop optimisation
        if: If conversion
        combination: Instruction combination
        register: Register allocation
        bb_reordering: Basic block reordering
        [*] --> CFGC
        CFGC --> fwd_prop
        fwd_prop --> CSE
        CSE --> GCSE
        GCSE --> loop
        loop --> if
        if --> combination
        combination --> scheduling
        scheduling --> register
        register --> bb_reordering
    }
    Gimple --> RTL
    RTL --> Assembly
Loading
Clone this wiki locally