Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.41.0

0.41.0 / src / back / native / x86_64 / generator.lucb

#==============================================================================================
#
#   x86_64 - The x86_64 code generator
#
#   DESCRIPTION:
#       Emits AT&T assembly for x86-64. Calling-convention classification lives in
#       x86_64/abi; sections and symbols live in native/objects, address loads in x86_64/object; frame
#       unwind directives live in x86_64/unwind. Instruction selection consumes
#       these policies for System V on Linux and Microsoft x64 on Windows.
#
#       The shared frame planner and linear scan allocator assign temporaries and
#       frame slots, integers and floats alike (`registers_of`); Windows' nonvolatile
#       registers are saved when the function's text names them. Half operations use
#       F16C at v3, and conversion routines at lower CPU levels.
#
#       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 x86_64/ are modules of their own.
#
#==============================================================================================
from back.native.x86_64.abi import Convention
from back.native.x86_64.abi import Placement
import back.native.x86_64.object
from back.native.x86_64.unwind import FrameUnwind
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.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 x86-64 generator could not emit the program.
pub let failed: ErrorCode = ErrorCode.package(21)

# mark: The generator =========================================================================

## The x86-64 code generator: the IR to x86-64 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: nothing on Linux (§19.5).
    let symbol_prefix: str
    let object_format: objects.Format
    ## Independent calling-convention and unwind policies.
    let convention: Convention
    var unwinding: FrameUnwind
    ## The instruction-set level (§19.5): which forms the generator may emit.
    pub var level: u32
    var program_unit: ir.Unit*
    var f: ir.Function*
    var index: usize
    ## The instruction being emitted, for an asm block to see the operands around it.
    var at: usize
    ## 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[]
    ## Windows: the nonvolatile registers beyond the allocator's (`windows_nonvolatile`, one
    ## bit each) the function's save area holds, and those its text named.
    var windows_plan: u16 = 0
    var windows_used: u16 = 0
    ## Below x86-64-v3 there is no F16C: halves convert through the unit's own routines,
    ## emitted once at its end when a function needed them.
    var half_routines: bool
    ## Callee-saved registers an asm block names, pushed before it and popped after its
    ## outputs are read; `pending` is how many.
    var saved: str[8]
    var pending: usize
    ## The frame and the callee-saved registers: what every generator decides the same way.
    var fr: frame.Frame
    ## `--debug`: the frame descriptors, gathered while each function's frame is known.
    var descriptors: buffer.Buffer
    ## 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
    ## The bits of the float constants the function's text reads from its literal pool
    ## (`constant`), which follows the function in read-only data (`finish_pool`).
    var pool: list.List[u64]
    ## For an assembly the driver splits into pieces it assembles in parallel (ELF): the
    ## labels functions share (text literals, the sign masks, the half routines) 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 (bit 0, it calls the half routines).
    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[]

    ## Register allocation: for each temporary, the callee-saved register it lives in
    ## The callee-saved register code a promoted frame slot lives in, 0 when it stays in the
    pub static func create(program_unit: ir.Unit*, os: build_target.Os = build_target.Os.linux, symbol_prefix: str = "") -> Generator!:
        return Generator(symbol_prefix = symbol_prefix, object_format = objects.Format(os, symbol_prefix, 4), convention = Convention(program_unit, os), unwinding = FrameUnwind(os == build_target.Os.windows, false), level = 1, writer = assembly.Text(out = try buffer.Buffer.create(262144)), program_unit = program_unit, f = program_unit.functions.span().data, index = 0,
                         at = 0, saved = ["", "", "", "", "", "", "", ""], pending = 0, half_routines = false,
                         fr = try frame.Frame.create(registers_of(os == build_target.Os.windows)), descriptors = try buffer.Buffer.create(4096), cold = try buffer.Buffer.create(1024), pool = try list.List[u64].create(8))

    # -- output ---------------------------------------------------------------------------

    func symbol(name: str) -> !:
        try self.writer.out.put(self.symbol_prefix)
        try self.writer.out.put(name)

    ## `.L<function>_<label>`: local to the object, as the assembler treats `.L` names.
    func label_name(n: u32) -> !:
        try self.writer.out.put(".L")
        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(".L")
        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)

    ## The label of entry `n` of the function's literal pool (`constant`).
    func pool_label(n: u32) -> !:
        try self.writer.out.put(".L")
        try stable_names.put_function_label(&self.writer.out, self.index, self.function_tags, self.split or self.stable)
        try self.writer.out.put("_k")
        try self.writer.out.put_u64((u64)n)

    ## The label of jump table `n` of the function.
    func table_name(n: u32) -> !:
        try self.writer.out.put(".L")
        try stable_names.put_function_label(&self.writer.out, self.index, self.function_tags, self.split or self.stable)
        try self.writer.out.put("_jt")
        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)

    ## A label functions share, as this assembly spells it: local to the file (`.Lname`), or
    ## with `split` the hidden symbol `lb_name` every piece can name.
    func shared(label: str) -> str:
        if not self.split:
            return label
        if label == ".Lvsign32":
            return "lb_vsign32"
        if label == ".Lvsign64":
            return "lb_vsign64"
        if label == ".Llb_half_to_single":
            return "lb_half_to_single"
        if label == ".Llb_single_to_half":
            return "lb_single_to_half"
        return label

    ## Every function's text, each generated on whichever thread takes it by a generator of
    ## its own, in an arena of its own, and written here in order. A function's text depends
    ## on that function alone (its labels carry its index), so it is the text one function
    ## after another would give.
    func functions_in_parallel() -> !:
        let n = self.program_unit.functions.count
        with memory.heap:
            var job = Generating(model = &self, texts = try new str[n] ---, halves = try new bool[n] ---, failures = try parallel.failures(n))
            defer free(job.texts)
            defer free(job.halves)
            defer free(job.failures)
            for k in 0..<n:
                job.texts[k] = ""
                job.halves[k] = false
            defer:
                for k in 0..<n:
                    if job.texts[k].length > 0:
                        free(job.texts[k].bytes)
            let order = try self.program_unit.largest_first(n)
            defer free(order)
            try parallel.each_ordered(n, generate_task, (void*)&job, order)
            try parallel.check(job.failures)
            self.function_flags = try new u8[n] --- in memory.heap
            for k in 0..<n:
                self.starts[k] = self.writer.out.used
                try self.writer.put(job.texts[k])
                self.function_flags[k] = 1 if job.halves[k] else 0
                if job.halves[k]:
                    self.half_routines = true

    ## A generator for one function like `model`: the same target and settings, its own
    ## frame and output, from the current allocator.
    static func like(model: Generator*) -> Generator!:
        var made = Generator(symbol_prefix = model.symbol_prefix, object_format = model.object_format, convention = model.convention, unwinding = model.unwinding, level = model.level, writer = assembly.Text(out = try buffer.Buffer.create(16384)), program_unit = model.program_unit, f = model.f, index = 0,
                             at = 0, saved = ["", "", "", "", "", "", "", ""], pending = 0, half_routines = false,
                             fr = try frame.Frame.create(registers_of(model.convention.windows)), descriptors = try buffer.Buffer.create(256), cold = try buffer.Buffer.create(1024), pool = try list.List[u64].create(8))
        made.dw = none
        made.split = model.split
        made.stable = model.stable
        made.function_tags = model.function_tags
        made.text_tags = model.text_tags
        return made

    ## The assembly emitted so far.
    pub func text() -> str:
        return self.writer.out.view()