pub struct Linker { /* private fields */ }Expand description
The Linker is responsible for analyzing the input modules and libraries provided to the
assembler, and linking them together.
The core conceptual data structure of the linker is the module graph, which is implemented by a vector of module nodes, and a call graph, which is implemented as an adjacency matrix of item nodes and the outgoing edges from those nodes, representing references from that item to another symbol (typically as the result of procedure invocation, hence “call” graph).
Each item/symbol known to the linker is given a global item index, which is actually a pair of indices: a module index (which indexes into the vector of module nodes), and an item index (which indexes into the items defined by a module). These global item indices function as a unique identifier within the linker, to a specific item, and can be resolved to either the original syntax tree of the item, or to metadata about the item retrieved from previously- assembled MAST.
The process of linking involves two phases:
- Setting up the linker context, by providing the set of inputs to link together
- Analyzing and rewriting the symbols known to the linker, as needed, to ensure that all symbol references are resolved to concrete definitions.
The assembler will call Self::link once it has provided all inputs that it wants to link,
which will, when successful, return the set of module indices corresponding to the modules that
comprise the public interface of the assembled artifact. The assembler then constructs the MAST
starting from the exported procedures of those modules, recursively tracing the call graph
based on whether or not the callee is statically or dynamically linked. In the static linking
case, any procedures referenced in a statically-linked library or module will be included in
the assembled artifact. In the dynamic linking case, referenced procedures are instead
referenced in the assembled artifact only by their MAST root.
Implementations§
Source§impl Linker
Constructors
impl Linker
Constructors
Sourcepub fn new(source_manager: Arc<dyn SourceManager>) -> Self
pub fn new(source_manager: Arc<dyn SourceManager>) -> Self
Instantiate a new Linker, using the provided SourceManager to resolve source info.
Sourcepub fn link_library(&mut self, library: LinkLibrary) -> Result<(), LinkerError>
pub fn link_library(&mut self, library: LinkLibrary) -> Result<(), LinkerError>
Registers library and all of its modules with the linker, according to its kind
Sourcepub fn link_assembled_modules(
&mut self,
modules: impl IntoIterator<Item = ModuleInfo>,
) -> Result<(), LinkerError>
pub fn link_assembled_modules( &mut self, modules: impl IntoIterator<Item = ModuleInfo>, ) -> Result<(), LinkerError>
Registers a set of MAST modules with the linker.
If called directly, the modules will default to being dynamically linked. You must use
Self::link_library if you wish to statically link a set of assembled modules.
Sourcepub fn link_assembled_module(
&mut self,
module: ModuleInfo,
) -> Result<ModuleIndex, LinkerError>
pub fn link_assembled_module( &mut self, module: ModuleInfo, ) -> Result<ModuleIndex, LinkerError>
Registers a MAST module with the linker.
If called directly, the module will default to being dynamically linked. You must use
Self::link_library if you wish to statically link module.
Sourcepub fn link_modules(
&mut self,
modules: impl IntoIterator<Item = Box<Module>>,
) -> Result<Vec<ModuleIndex>, LinkerError>
pub fn link_modules( &mut self, modules: impl IntoIterator<Item = Box<Module>>, ) -> Result<Vec<ModuleIndex>, LinkerError>
Registers a set of AST modules with the linker.
See Self::link_module for more details.
Sourcepub fn link_module(
&mut self,
module: &mut Module,
) -> Result<ModuleIndex, LinkerError>
pub fn link_module( &mut self, module: &mut Module, ) -> Result<ModuleIndex, LinkerError>
Registers an AST module with the linker.
A module provided to this method is presumed to be dynamically linked, unless specifically
handled otherwise by the assembler. In particular, the assembler will only statically link
the set of AST modules provided to Self::link, as they are expected to comprise the
public interface of the assembled artifact.
§Errors
This operation can fail for the following reasons:
- Module with same Path is in the graph already
- Too many modules in the graph
§Panics
This function will panic if the number of modules exceeds the maximum representable
ModuleIndex value, u16::MAX.
Source§impl Linker
Kernels
impl Linker
Kernels
pub fn kernel(&self) -> &Kernel
pub fn has_nonempty_kernel(&self) -> bool
Source§impl Linker
Analysis
impl Linker
Analysis
Sourcepub fn link(
&mut self,
modules: impl IntoIterator<Item = Box<Module>>,
) -> Result<Vec<ModuleIndex>, LinkerError>
pub fn link( &mut self, modules: impl IntoIterator<Item = Box<Module>>, ) -> Result<Vec<ModuleIndex>, LinkerError>
Links modules using the current state of the linker.
Returns the module indices corresponding to the provided modules, which are expected to provide the public interface of the final assembled artifact.
Sourcepub fn link_kernel(
&mut self,
kernel: Box<Module>,
) -> Result<Vec<ModuleIndex>, LinkerError>
pub fn link_kernel( &mut self, kernel: Box<Module>, ) -> Result<Vec<ModuleIndex>, LinkerError>
Links kernel using the current state of the linker.
Returns the module index of the kernel module, which is expected to provide the public interface of the final assembled kernel.
This differs from link in that we allow all AST modules in the module graph access to
kernel features, e.g. caller, as if they are defined by the kernel module itself.
Source§impl Linker
Accessors/Queries
impl Linker
Accessors/Queries
Sourcepub fn libraries(&self) -> impl Iterator<Item = &LinkLibrary>
pub fn libraries(&self) -> impl Iterator<Item = &LinkLibrary>
Get an iterator over the external libraries the linker has linked against
Sourcepub fn topological_sort_from_root(
&self,
caller: GlobalItemIndex,
) -> Result<Vec<GlobalItemIndex>, CycleError>
pub fn topological_sort_from_root( &self, caller: GlobalItemIndex, ) -> Result<Vec<GlobalItemIndex>, CycleError>
Compute the topological sort of the callgraph rooted at caller
Sourcepub fn get_procedure_index_by_digest(
&self,
procedure_digest: &Word,
) -> Option<GlobalItemIndex>
pub fn get_procedure_index_by_digest( &self, procedure_digest: &Word, ) -> Option<GlobalItemIndex>
Returns a procedure index which corresponds to the provided procedure digest.
Note that there can be many procedures with the same digest - due to having the same code, and/or using different decorators which don’t affect the MAST root. This method returns an arbitrary one.
Sourcepub fn resolve_invoke_target(
&self,
caller: &SymbolResolutionContext,
target: &InvocationTarget,
) -> Result<SymbolResolution, LinkerError>
pub fn resolve_invoke_target( &self, caller: &SymbolResolutionContext, target: &InvocationTarget, ) -> Result<SymbolResolution, LinkerError>
Resolves target from the perspective of caller.
Sourcepub fn resolve_alias_target(
&self,
caller: &SymbolResolutionContext,
target: &Alias,
) -> Result<SymbolResolution, LinkerError>
pub fn resolve_alias_target( &self, caller: &SymbolResolutionContext, target: &Alias, ) -> Result<SymbolResolution, LinkerError>
Resolves target from the perspective of caller.
Sourcepub fn resolve_path(
&self,
caller: &SymbolResolutionContext,
path: &Path,
) -> Result<SymbolResolution, LinkerError>
pub fn resolve_path( &self, caller: &SymbolResolutionContext, path: &Path, ) -> Result<SymbolResolution, LinkerError>
Resolves path from the perspective of caller.
Sourcepub fn find_module_index(&self, path: &Path) -> Option<ModuleIndex>
pub fn find_module_index(&self, path: &Path) -> Option<ModuleIndex>
Resolve a Path to a ModuleIndex in this graph
Trait Implementations§
Source§impl Index<GlobalItemIndex> for Linker
impl Index<GlobalItemIndex> for Linker
Auto Trait Implementations§
impl Freeze for Linker
impl !RefUnwindSafe for Linker
impl !Send for Linker
impl !Sync for Linker
impl Unpin for Linker
impl UnsafeUnpin for Linker
impl !UnwindSafe for Linker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more