338 lines
18 KiB
HTML
338 lines
18 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en" class="light sidebar-visible" dir="ltr">
|
|
<head>
|
|
<!-- Book generated using mdBook -->
|
|
<meta charset="UTF-8">
|
|
<title>MIR construction - Rust Compiler Development Guide</title>
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
<meta name="description" content="A guide to developing the Rust compiler (rustc)">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
<link rel="icon" href="../favicon.svg">
|
|
<link rel="shortcut icon" href="../favicon.png">
|
|
<link rel="stylesheet" href="../css/variables.css">
|
|
<link rel="stylesheet" href="../css/general.css">
|
|
<link rel="stylesheet" href="../css/chrome.css">
|
|
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
|
|
<!-- Fonts -->
|
|
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
|
|
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
|
|
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
|
|
|
<!-- Provide site root and default themes to javascript -->
|
|
<script>
|
|
const path_to_root = "../";
|
|
const default_light_theme = "light";
|
|
const default_dark_theme = "navy";
|
|
</script>
|
|
<!-- Start loading toc.js asap -->
|
|
<script src="../toc.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="body-container">
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
<script>
|
|
try {
|
|
let theme = localStorage.getItem('mdbook-theme');
|
|
let sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
}
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
}
|
|
} catch (e) { }
|
|
</script>
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
<script>
|
|
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
|
let theme;
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
const html = document.documentElement;
|
|
html.classList.remove('light')
|
|
html.classList.add(theme);
|
|
html.classList.add("js");
|
|
</script>
|
|
|
|
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
<script>
|
|
let sidebar = null;
|
|
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
|
if (document.body.clientWidth >= 1080) {
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
sidebar = sidebar || 'visible';
|
|
} else {
|
|
sidebar = 'hidden';
|
|
}
|
|
sidebar_toggle.checked = sidebar === 'visible';
|
|
html.classList.remove('sidebar-visible');
|
|
html.classList.add("sidebar-" + sidebar);
|
|
</script>
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
<!-- populated by js -->
|
|
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
|
<noscript>
|
|
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
|
|
</noscript>
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
|
<div class="sidebar-resize-indicator"></div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
<div class="page">
|
|
<div id="menu-bar-hover-placeholder"></div>
|
|
<div id="menu-bar" class="menu-bar sticky">
|
|
<div class="left-buttons">
|
|
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
<i class="fa fa-bars"></i>
|
|
</label>
|
|
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
<i class="fa fa-paint-brush"></i>
|
|
</button>
|
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
</ul>
|
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
<i class="fa fa-search"></i>
|
|
</button>
|
|
</div>
|
|
|
|
<h1 class="menu-title">Rust Compiler Development Guide</h1>
|
|
|
|
<div class="right-buttons">
|
|
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
<i id="print-button" class="fa fa-print"></i>
|
|
</a>
|
|
<a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository">
|
|
<i id="git-repository-button" class="fa fa-github"></i>
|
|
</a>
|
|
<a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/mir/construction.md" title="Suggest an edit" aria-label="Suggest an edit">
|
|
<i id="git-edit-button" class="fa fa-edit"></i>
|
|
</a>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="search-wrapper" class="hidden">
|
|
<form id="searchbar-outer" class="searchbar-outer">
|
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
</form>
|
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
<div id="searchresults-header" class="searchresults-header"></div>
|
|
<ul id="searchresults">
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
<script>
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
});
|
|
</script>
|
|
|
|
<div id="content" class="content">
|
|
<main>
|
|
<h1 id="mir-construction"><a class="header" href="#mir-construction">MIR construction</a></h1>
|
|
<ul>
|
|
<li><a href="#unpack-all-the-things"><code>unpack!</code> all the things</a></li>
|
|
<li><a href="#lowering-expressions-into-the-desired-mir">Lowering expressions into the desired MIR</a></li>
|
|
<li><a href="#operator-lowering">Operator lowering</a></li>
|
|
<li><a href="#method-call-lowering">Method call lowering</a></li>
|
|
<li><a href="#conditions">Conditions</a>
|
|
<ul>
|
|
<li><a href="#pattern-matching">Pattern matching</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#aggregate-construction">Aggregate construction</a></li>
|
|
</ul>
|
|
<p>The lowering of <a href="../hir.html">HIR</a> to <a href="./index.html">MIR</a> occurs for the following (probably incomplete)
|
|
list of items:</p>
|
|
<ul>
|
|
<li>Function and closure bodies</li>
|
|
<li>Initializers of <code>static</code> and <code>const</code> items</li>
|
|
<li>Initializers of enum discriminants</li>
|
|
<li>Glue and shims of any kind
|
|
<ul>
|
|
<li>Tuple struct initializer functions</li>
|
|
<li>Drop code (the <code>Drop::drop</code> function is not called directly)</li>
|
|
<li>Drop implementations of types without an explicit <code>Drop</code> implementation</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<p>The lowering is triggered by calling the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir_transform/fn.mir_built.html"><code>mir_built</code></a> query. The MIR builder does
|
|
not actually use the HIR but operates on the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir_build/thir/index.html">THIR</a> instead, processing THIR
|
|
expressions recursively.</p>
|
|
<p>The lowering creates local variables for every argument as specified in the signature.
|
|
Next, it creates local variables for every binding specified (e.g. <code>(a, b): (i32, String)</code>)
|
|
produces 3 bindings, one for the argument, and two for the bindings. Next, it generates
|
|
field accesses that read the fields from the argument and writes the value to the binding
|
|
variable.</p>
|
|
<p>With this initialization out of the way, the lowering triggers a recursive call
|
|
to a function that generates the MIR for the body (a <code>Block</code> expression) and
|
|
writes the result into the <code>RETURN_PLACE</code>.</p>
|
|
<h2 id="unpack-all-the-things"><a class="header" href="#unpack-all-the-things"><code>unpack!</code> all the things</a></h2>
|
|
<p>Functions that generate MIR tend to fall into one of two patterns.
|
|
First, if the function generates only statements, then it will take a
|
|
basic block as argument onto which those statements should be appended.
|
|
It can then return a result as normal:</p>
|
|
<pre><code class="language-rust ignore">fn generate_some_mir(&mut self, block: BasicBlock) -> ResultType {
|
|
...
|
|
}</code></pre>
|
|
<p>But there are other functions that may generate new basic blocks as well.
|
|
For example, lowering an expression like <code>if foo { 22 } else { 44 }</code>
|
|
requires generating a small "diamond-shaped graph".
|
|
In this case, the functions take a basic block where their code starts
|
|
and return a (potentially) new basic block where the code generation ends.
|
|
The <code>BlockAnd</code> type is used to represent this:</p>
|
|
<pre><code class="language-rust ignore">fn generate_more_mir(&mut self, block: BasicBlock) -> BlockAnd<ResultType> {
|
|
...
|
|
}</code></pre>
|
|
<p>When you invoke these functions, it is common to have a local variable <code>block</code>
|
|
that is effectively a "cursor". It represents the point at which we are adding new MIR.
|
|
When you invoke <code>generate_more_mir</code>, you want to update this cursor.
|
|
You can do this manually, but it's tedious:</p>
|
|
<pre><code class="language-rust ignore">let mut block;
|
|
let v = match self.generate_more_mir(..) {
|
|
BlockAnd { block: new_block, value: v } => {
|
|
block = new_block;
|
|
v
|
|
}
|
|
};</code></pre>
|
|
<p>For this reason, we offer a macro that lets you write
|
|
<code>let v = unpack!(block = self.generate_more_mir(...))</code>.
|
|
It simply extracts the new block and overwrites the
|
|
variable <code>block</code> that you named in the <code>unpack!</code>.</p>
|
|
<h2 id="lowering-expressions-into-the-desired-mir"><a class="header" href="#lowering-expressions-into-the-desired-mir">Lowering expressions into the desired MIR</a></h2>
|
|
<p>There are essentially four kinds of representations one might want of an expression:</p>
|
|
<ul>
|
|
<li><code>Place</code> refers to a (or part of a) preexisting memory location (local, static, promoted)</li>
|
|
<li><code>Rvalue</code> is something that can be assigned to a <code>Place</code></li>
|
|
<li><code>Operand</code> is an argument to e.g. a <code>+</code> operation or a function call</li>
|
|
<li>a temporary variable containing a copy of the value</li>
|
|
</ul>
|
|
<p>The following image depicts a general overview of the interactions between the
|
|
representations:</p>
|
|
<img src="mir_overview.svg">
|
|
<p><a href="mir_detailed.svg">Click here for a more detailed view</a></p>
|
|
<p>We start out with lowering the function body to an <code>Rvalue</code> so we can create an
|
|
assignment to <code>RETURN_PLACE</code>, This <code>Rvalue</code> lowering will in turn trigger lowering to
|
|
<code>Operand</code> for its arguments (if any). <code>Operand</code> lowering either produces a <code>const</code>
|
|
operand, or moves/copies out of a <code>Place</code>, thus triggering a <code>Place</code> lowering. An
|
|
expression being lowered to a <code>Place</code> can in turn trigger a temporary to be created
|
|
if the expression being lowered contains operations. This is where the snake bites its
|
|
own tail and we need to trigger an <code>Rvalue</code> lowering for the expression to be written
|
|
into the local.</p>
|
|
<h2 id="operator-lowering"><a class="header" href="#operator-lowering">Operator lowering</a></h2>
|
|
<p>Operators on builtin types are not lowered to function calls (which would end up being
|
|
infinite recursion calls, because the trait impls just contain the operation itself
|
|
again). Instead there are <code>Rvalue</code>s for binary and unary operators and index operations.
|
|
These <code>Rvalue</code>s later get codegened to llvm primitive operations or llvm intrinsics.</p>
|
|
<p>Operators on all other types get lowered to a function call to their <code>impl</code> of the
|
|
operator's corresponding trait.</p>
|
|
<p>Regardless of the lowering kind, the arguments to the operator are lowered to <code>Operand</code>s.
|
|
This means all arguments are either constants, or refer to an already existing value
|
|
somewhere in a local or static.</p>
|
|
<h2 id="method-call-lowering"><a class="header" href="#method-call-lowering">Method call lowering</a></h2>
|
|
<p>Method calls are lowered to the same <code>TerminatorKind</code> that function calls are.
|
|
In <a href="./index.html">MIR</a> there is no difference between method calls and function calls anymore.</p>
|
|
<h2 id="conditions"><a class="header" href="#conditions">Conditions</a></h2>
|
|
<p><code>if</code> conditions and <code>match</code> statements for <code>enum</code>s with variants that have no fields are
|
|
lowered to <code>TerminatorKind::SwitchInt</code>. Each possible value (so <code>0</code> and <code>1</code> for <code>if</code>
|
|
conditions) has a corresponding <code>BasicBlock</code> to which the code continues.
|
|
The argument being branched on is (again) an <code>Operand</code> representing the value of
|
|
the if condition.</p>
|
|
<h3 id="pattern-matching"><a class="header" href="#pattern-matching">Pattern matching</a></h3>
|
|
<p><code>match</code> statements for <code>enum</code>s with variants that have fields are lowered to
|
|
<code>TerminatorKind::SwitchInt</code>, too, but the <code>Operand</code> refers to a <code>Place</code> where the
|
|
discriminant of the value can be found. This often involves reading the discriminant
|
|
to a new temporary variable.</p>
|
|
<h2 id="aggregate-construction"><a class="header" href="#aggregate-construction">Aggregate construction</a></h2>
|
|
<p>Aggregate values of any kind (e.g. structs or tuples) are built via <code>Rvalue::Aggregate</code>.
|
|
All fields are
|
|
lowered to <code>Operator</code>s. This is essentially equivalent to one assignment
|
|
statement per aggregate field plus an assignment to the discriminant in the
|
|
case of <code>enum</code>s.</p>
|
|
|
|
</main>
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
<!-- Mobile navigation buttons -->
|
|
<a rel="prev" href="../mir/index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
<a rel="next prefetch" href="../mir/visitor.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
<div style="clear: both"></div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
<a rel="prev" href="../mir/index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
<a rel="next prefetch" href="../mir/visitor.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
window.playground_copyable = true;
|
|
</script>
|
|
|
|
|
|
<script src="../elasticlunr.min.js"></script>
|
|
<script src="../mark.min.js"></script>
|
|
<script src="../searcher.js"></script>
|
|
|
|
<script src="../clipboard.min.js"></script>
|
|
<script src="../highlight.js"></script>
|
|
<script src="../book.js"></script>
|
|
|
|
<!-- Custom JS scripts -->
|
|
<script src="../mermaid.min.js"></script>
|
|
<script src="../mermaid-init.js"></script>
|
|
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|