Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.45.0

0.45.0 / src / back / native / arm64 / unit.lucb

#==============================================================================================
#
#   arm64/unit - The unit
#
#   DESCRIPTION:
#       The whole program's text: sections, data, text literals, and every function.
#
#==============================================================================================
extend Generator:
    # mark: The unit ===========================================================================
    pub func generate() -> !:
        if let dw = self.dw:
            try dw.begin(&self.writer.out)
        try self.object_format.text_section(&self.writer.out)
        self.starts = try new usize[self.program_unit.functions.count + 1] ---
        # the reasons generated checks trap with, in the unit's texts before any function is
        # generated: the functions only read the texts, whichever thread generates them
        for reason in trap_reasons:
            _ = try self.program_unit.text(reason)
        if self.split or self.stable:
            self.function_tags = try stable_names.function_tags(self.program_unit)
            self.text_tags = try stable_names.text_tags(self.program_unit)
        var i: usize = 0
        if self.dw == none:
            try self.functions_in_parallel()
        else:
            # the DWARF emitter gathers every function's lines and frames in order
            while i < self.program_unit.functions.count:
                self.starts[i] = self.writer.out.used
                try self.function(i)
                i += 1
        self.starts[self.program_unit.functions.count] = self.writer.out.used
        try objects.module_asm(&self.writer, self.program_unit)
        try self.object_format.initializer(&self.writer.out, self.program_unit.init_function)
        # text literals
        try self.object_format.read_only(&self.writer.out)
        try self.object_format.texts(&self.writer, self.program_unit, self.split, self.stable, self.text_tags)
        try self.object_format.witness_tables(&self.writer, self.program_unit)
        # frame descriptors, when `--debug` asked for them
        if self.descriptors.used > 0:
            try self.writer.put(self.descriptors.view())
        try self.object_format.globals(&self.writer.out, self.program_unit)
        try self.writer.put("\n")
        if let dw = self.dw:
            try dw.finish(&self.writer.out)
        self.ending = self.writer.out.used
        try self.object_format.finish(&self.writer.out)

    ## 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] ---, failures = try parallel.failures(n))
            defer free(job.texts)
            defer free(job.failures)
            for k in 0..<n:
                job.texts[k] = ""
            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)
            # no flags on arm64: a function's text is all `reuse` keeps of it
            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] = 0

    ## 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, writer = assembly.Text(out = try buffer.Buffer.create(16384)), program_unit = model.program_unit, f = model.f, 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, formed_constants = true)), text_count = 0, 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