dymokomi/luce-base
Source of release 0.41.0
0.41.0 / src / back / native / arm64 / generator.lucb
#==============================================================================================
#
# arm64 - The arm64 code generator
#
# DESCRIPTION:
# Turns an `ir.Unit` into assembly text for the system assembler, for arm64-macos:
# sections and symbols as native/objects spells them for Mach-O, page addressing
# and thread-locals as arm64/object does, and calls under AAPCS64 with Apple's
# variant (base.md §19.5): integers in x0-x7, floats in d0-d7, aggregates
# up to sixteen bytes in registers, larger ones by address, homogeneous float
# aggregates in d registers, results the same way with x8 for the large ones, and
# variadic arguments on the stack under Apple's rule.
#
# The shared frame planner (back.native.frame) and linear-scan allocator
# (back.native.regalloc) give each temporary a register or a frame slot; an
# instruction reads its operands where they live, through scratch registers when
# they are in memory.
#
# This fragment holds the `Generator` and its fields; each concern's methods are
# in a fragment of their own under `extend Generator:`, as `ORDER` lists them
# (§16.1). The other files of arm64/ are modules of their own.
#
#==============================================================================================
import back.native.arm64.object
import back.native.dwarf
import back.native.assembly
import back.native.objects
import back.native.frame
import back.native.regalloc
import back.native.incoming
import back.native.flags
import back.native.levels
import back.native.selection
import back.native.stable_names
import back.ir.ir
import back.target as build_target
import back.parallel
import memory
import support.arena as arenas
import support.buffer
import support.list
## The arm64 generator could not emit the program.
pub let failed: ErrorCode = ErrorCode.package(15)
## One argument's placement for a call.
struct ArgPlacement:
## 0 = integer registers, 1 = float registers, 2 = stack, 3 = by reference in an integer register
var where: u8
var first: u32
var count: u32
var stack_offset: u32
# mark: The generator =========================================================================
## The arm64 code generator: the IR to arm64 assembly, through its object format.
pub struct Generator:
## `--debug` or `--release --debug`: the DWARF emitter, else none.
pub var dw: dwarf.Emitter*?
## The assembly text being written.
var writer: assembly.Text
## The next of the frame's `crossings` not yet passed, in the function being emitted.
var crossing: usize = 0
## What precedes a C symbol in this target's assembler: `_` on macOS (§19.5).
pub var symbol_prefix: str
## How Mach-O spells sections, symbols and labels.
var object_format: objects.Format
var program_unit: ir.Unit*
var f: ir.Function*
var index: usize
## The frame and the callee-saved registers: what every generator decides the same way.
var fr: frame.Frame
var text_count: usize
## `--debug`: the frame descriptors, gathered while each function's frame is known.
var descriptors: buffer.Buffer
## For an assembly the driver splits into pieces it assembles in parallel: text literals
## are hidden global symbols, not labels local to one file, so every piece can name them.
pub var split: bool = false
## Labels and literals named by what they label (`stable_names`), as with `split`, but
## local to the one assembly: a function's text is then the same in every build that
## has it, which `reuse` needs, on a target whose assembly is not split.
pub var stable: bool = false
## Where each function's text begins in the output, and after them, where the rest
## (module asm, literals, tables, globals) begins: `functions.count + 1` offsets.
pub var starts: usize[]
## Where the format's closing directives begin; every piece of a split assembly ends
## with them.
pub var ending: usize = 0
## With `split` or `stable`: each function's tag and each text literal's
## (`stable_names`), which name their labels in place of their indexes.
pub var function_tags: u64[]
## With `split` or `stable`: each text literal's tag.
pub var text_tags: u64[]
## The texts the last build generated, by function, taken instead of generating them
## (`reuse`); empty where a function is generated.
pub var reused: str[]
## The generator's flags for each reused text (none on arm64).
pub var reused_flags: u8[]
## After `generate`: the generator's flags for each function, which `reuse` keeps with
## its text.
pub var function_flags: u8[]
## By temporary, in the function being emitted: a `w` value whose register's upper half
## is zero (`selection.zero_extended`), which an `extuw` in the same register leaves be.
var zero_upper: bool[]
## How far sp is below the frame's bottom while a call sequence has moved it; frame
## operands relative to sp add it back.
var sp_shift: u64
## The index of the instruction being emitted, for a call to find its `word`s after it.
var at: usize = 0
## How far sp is below the fixed frame for the dynamic area's levels reserved at the
## instruction being emitted (`levels`); frame operands relative to sp add it back.
var dynamic_shift: u64 = 0
## In a function taking frame storage (`ir.Op.alloca`) once its prologue has run: the
## fixed frame is reached through x28, which holds sp's place at the frame's bottom,
## since sp moves by amounts only known at run time.
var frame_base: bool = false
## By label, the loops' heads of the function being emitted (`selection.loop_heads`).
var loop_heads: bool[]
## By label, 1 + the index of the comparison a jump to that loop's head makes itself
## (`selection.rotations`); 0 for every other label.
var head_tests: u32[]
## By instruction, its role in a condition kept in the flags (`flags.plan`), in the
## function being emitted; the condition the flags hold after a chain's comparison.
var flag_roles: u8[]
var flag_condition: str = "ne"
## The failure paths of the function's checks (`check_failure`), written after its body;
## how many it has.
var cold: buffer.Buffer
var cold_count: u32 = 0
## A generator for `program_unit`, targeting `os` with the given C symbol prefix.
pub static func create(program_unit: ir.Unit*, os: build_target.Os = build_target.Os.macos, symbol_prefix: str = "_") -> Generator!:
return Generator(symbol_prefix = symbol_prefix, object_format = objects.Format(os, symbol_prefix, 2), writer = assembly.Text(out = try buffer.Buffer.create(262144)), program_unit = program_unit, f = program_unit.functions.span().data, index = 0,
fr = try frame.Frame.create(frame.Registers(ints = 10, floats = 8, keep_ints = 4, keep_floats = 4, scratch_ints = 12, scratch_floats = 13)), text_count = 0, descriptors = try buffer.Buffer.create(4096), cold = try buffer.Buffer.create(1024))
# -- output ---------------------------------------------------------------------------
## `_name`
func symbol(name: str) -> !:
try self.writer.out.put(self.symbol_prefix)
try self.writer.out.put(name)
func label_name(n: u32) -> !:
try self.writer.out.put(self.object_format.local_label)
try stable_names.put_function_label(&self.writer.out, self.index, self.function_tags, self.split or self.stable)
try self.writer.out.put("_")
try self.writer.out.put_u64((u64)n)
## The label of the function's cold path `n` (`check_failure`).
func cold_label(n: u32) -> !:
try self.writer.out.put(self.object_format.local_label)
try stable_names.put_function_label(&self.writer.out, self.index, self.function_tags, self.split or self.stable)
try self.writer.out.put("_c")
try self.writer.out.put_u64((u64)n)
func text_label(n: u32) -> !:
try self.object_format.text_name(&self.writer.out, self.split, self.stable, self.text_tags, n)
## The text literal's label as text, for an address the format spells.
func text_label_name(n: u32) -> str!:
var name = try buffer.Buffer.create(32)
try self.object_format.text_name(&name, self.split, self.stable, self.text_tags, n)
return name.view()
## The assembly emitted so far.
pub func text() -> str:
return self.writer.out.view()