Skip to main content

Module lower

Module lower 

Source
Expand description

The selector: an IR function becomes a machine IR function.

Design: spec/10-backend.md sections 10.2 and 10.3.

What the matcher in crate::select does is answer one question about one term. What this does is ask it: walk a function, decide which terms are worth asking about, and build machine instructions out of what comes back. Nothing here decides what an IR term lowers to. That is in rules/x86-64.rules and it is proved before it is used, which is the whole point of the arrangement and the reason this file is short.

§What it does with an instruction

It tries the ways the instruction can be shown to the matcher, in order, and takes the first that a rule fires on. crate::term is what a way of showing one is, and the order is the most specific first: an operand that is a constant is offered as a constant before it is offered as a register, and an operand computed by an instruction of its own is offered as that instruction before it is offered as a register. A rule that wants an immediate too wide for the machine has a guard that turns it down, and the search carries on to the way of showing it that puts the constant in a register, which is the right answer and is one nobody had to write down.

A constant is not lowered where it is written. It is materialized where a register for it is first wanted, which is what keeps a constant that every use folded into an immediate from leaving a dead instruction behind, and it also gives the value the shortest live range it could have. The instruction that materializes it comes from the rule set like everything else.

§What it does not do yet

Anything with an effect, and anything that ends a block. There are no rules for loads, stores, calls, branches or returns, because spec/10-backend.md section 10.2 wants the language for an effect settled before the rules that have one are written, and until they are written a function containing any of them is one this reports it cannot lower. Everything is in the general purpose registers, because every rule in the set is about an integer.

Blocks are walked in the order the function holds them and a value is expected to be defined before it is used. That is true of a straight line and it is what the rules cover.

Structs§

Unsupported
Why a function could not be lowered.

Functions§

func
The x86-64 machine IR for that function.