#! @file constants.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the values computed while compiling: the constants, the #! conditions of the `if const` statements the checker left undecided, and the #! text of each `compile_error` it was given text it could not read. #! #! A program evaluating them is lowered first, with a function of its own for #! each, which computes the value into a global. Reading a constant stops that #! program unless the constant is evaluated, so the machine evaluates the one it #! needs first; and it stops reading what only a running program has, such as #! a variable given its value once it runs. #! #! The value each computes is kept as bytes laid out as its type, with the #! memory it points into, each address within them noted rather than written, #! since the memory it was computed in is the compiler's. The program then #! holds each as data, whose addresses are written where it is placed: a #! constant is read where its data is, and a condition decides its branch. import sakura::analyzer; import std::containers; module sakura::compiler; #! What stands for no block, where an address a value holds names a function. pub const no_block: u32 = 0xFFFFFFFF; #! What stands for a block, where an address a value holds points into another #! constant's value, which `decl`, or `table` and `node`, name as for a function. pub const in_constant: u32 = 0xFFFFFFFE; #! Where a value computed while compiling holds an address: of memory it points #! into, kept as a block, of another constant's value, or of a function, as a #! function value holds it. pub struct kept_address { pub var at: u64; #! The byte of the value or block the address is written at. pub var block: u32; #! The block it points into, `in_constant`, or `no_block` for a function. pub var addend: u64; #! How far into the block or the constant's value it points. pub var decl: u32; #! The function's or the global constant's declaration, or 0 for an anonymous function or a local constant. pub var instance: u32; #! One more than the instance of a generic it is, or 0. pub var table: u32; #! Of an anonymous function or a local constant, the result table of its nodes. pub var node: u32; #! Of an anonymous function, its `lambda` node, and of a local constant, its `variable` node. pub var file: u32; #! Of an anonymous function, its file. } #! Memory a value computed while compiling points into. pub struct kept_block { pub var bytes: vector; #! What it holds. pub var addresses: vector; #! Where it holds addresses. } #! A value computed while compiling: its bytes, laid out as its type is, and #! the memory it reaches. pub struct evaluated { pub var bytes: vector; #! The value. pub var addresses: vector; #! Where it holds addresses. pub var blocks: vector; #! The memory it points into, which addresses name by index. } #! What evaluating worked out, by the key of each constant, condition, or text. pub struct outcomes { pub var values: dict; #! The value of each constant. pub var decisions: dict; #! Whether the condition of each `if const` holds. pub var texts: dict; #! What each `compile_error` evaluating reports says. } #! Makes outcomes holding nothing. #! @returns The outcomes. pub def outcomes::new(): outcomes { return outcomes { values = dict::::new(), decisions = dict::::new(), texts = dict::::new() }; } #! The key of a global constant. #! @param decl Its declaration. #! @returns The key. pub def decl_key(decl: u32): u64 { return decl as u64; } #! The key of a local constant, the condition of an `if const`, or the text of #! a `compile_error`: the result table it was checked in, and its node. #! @param table The table. #! @param node The `variable`, `if_const`, or `call` node. #! @returns The key. pub def node_key(table: u32, node: u32): u64 { return (1u << 63) | pair_key(table, node); } #! A value computed while compiling, in the program evaluating them. pub struct evaluation { pub var key: u64; #! Its key. pub var decl: u32; #! The global constant, or 0. pub var table: u32; #! The result table a local constant, condition, or text was checked in. pub var node: u32; #! Its `variable`, `if_const`, or `call` node. pub var condition: bool; #! Whether it is the condition of an `if const`. pub var text: bool; #! Whether it is the text of a `compile_error`, its first argument. pub var kind: u32; #! The type of its value. pub var offset: u64; #! The global holding its value. pub var function: u32; #! The function computing it. } #! Lowers the program evaluating what the checker left to evaluating: every #! global constant whose value is not a number it worked out, and the local #! constants, conditions of `if const`, and text of `compile_error` it noted. #! @param this The compiler. pub def lowerer::lower_evaluations(&mut this): void { for id in 1..this.checker.decls.count { val decl := id as u32; continue if !this.needs_evaluation(decl); this.evaluation_of(decl_key(decl), decl, 0, 0, false); } for i in 0..this.checker.to_evaluate.length { val each: u64 = this.checker.to_evaluate[i]; val table := pair_high(each); val node := pair_low(each); val syntax := &this.checker.units[this.checker.tables.p[table].file].syntax; val condition := syntax.node_kinds[node] == nk::if_const; this.evaluation_of(node_key(table, node), 0, table, node, condition); } } #! Whether a declaration is a global constant whose value is evaluated: one the #! checker did not work out as a number, or one of a struct, a union, or an #! array. #! @param this The compiler. #! @param decl The declaration. #! @returns Whether it is. pub def lowerer::needs_evaluation(&mut this, decl: u32): bool { return false if this.checker.decls.kinds.p[decl] != dk::variable || !this.checker.decls.has(decl, df::constant); return false if this.checker.decls.has(decl, df::failed) || this.checker.decls.nodes.p[decl] == 0; val owner: u32 = this.checker.decls.owners.p[decl]; return false if owner != 0 && this.checker.own_param_count(owner) != 0; val saved := this.checker.at; this.checker.use_table(this.checker.units[this.checker.decls.files.p[decl]].table); val value := this.checker.record(this.checker.decls.nodes.p[decl], variable_slot::value); val known := value != 0 && this.checker.is_known(value) && this.is_scalar_type(this.checker.decls.types.p[decl]); this.checker.at = saved; return value != 0 && !known; } #! Whether values of a type are one word, as a number, `bool`, enum, or pointer. #! @param this The compiler. #! @param kind The type. #! @returns Whether they are. pub def lowerer::is_scalar_type(&mut this, kind: u32): bool { return false if kind == bt::error || kind == bt::void_type || kind == 0; val plain := this.layout_type(kind); return this.checker.types.is_numeric(plain) || this.checker.types.is_boolean(plain) || this.checker.named_kind(plain) == dk::enum_type || this.checker.types.kind_of(plain) == ty::pointer; } #! The evaluation computing a constant, condition, or text, made where it is #! new: a global holding its value, and a function computing it there. #! @param this The compiler. #! @param key Its key. #! @param decl The global constant, or 0. #! @param table The result table of a local constant, condition, or text. #! @param node Its node: of text, the `compile_error` call. #! @param condition Whether it is the condition of an `if const`. #! @returns Its index. pub def lowerer::evaluation_of(&mut this, key: u64, decl: u32, table: u32, node: u32, condition: bool): u32 { if this.evaluation_ids.get(&key) is some(got) { return got; } val saved := this.checker.at; var kind: u32 = bt::boolean; var offset: u64 = 0; var text := false; var name := string::new(nothing {}); if decl != 0 { this.checker.resolve(decl); kind = this.checker.decls.types.p[decl]; offset = this.global_of(decl); name.append(this.checker.names.text(this.checker.decls.names.p[decl])); } else { this.checker.use_table(table); text = !condition && this.checker.kind_of(node) == nk::call; if text { # Text is evaluated as the type it is given as, as a `string` that owns # its bytes, rather than the view `compile_error` takes of them. kind = this.checker.type_at(this.checker.run_at(this.checker.right_of(node), 0)); name.append(""); } else if !condition { kind = this.checker.type_at(node); name.append(this.checker.names.text(this.checker.name_of(node))); } else { name.append(""); } val held := this.shape_of(kind); this.program.globals = round_up(this.program.globals, held.align); offset = this.program.globals; this.program.globals += (held.size == 0 ? word_size : held.size) + 1; } this.checker.at = saved; val void_type := this.checker.types.function(span::::empty(), bt::void_type, false); val function := this.add_function(move(&mut name), this.signature_of(void_type)); val index := this.evaluations.length as u32; this.evaluations.append(evaluation { key = key, decl = decl, table = table, node = node, condition = condition, text = text, kind = kind, offset = offset, function = function }); this.evaluation_ids.set(key, index); this.program.functions[function].evaluation = index + 1; this.want(function); return index; } #! Lowers the function computing an evaluation's value into its global. #! @param this The emitter. #! @param index The evaluation. pub def emitter::lower_evaluation(&mut this, index: u32): void { val held: evaluation = this.c.evaluations[index]; var value: u32 = 0; if held.decl != 0 { this.table = this.c.checker.units[this.c.checker.decls.files.p[held.decl]].table; this.use_own_table(); value = this.c.checker.record(this.c.checker.decls.nodes.p[held.decl], variable_slot::value); this.at_node(this.c.checker.decls.nodes.p[held.decl]); } else { this.table = held.table; this.use_own_table(); if held.condition { value = this.c.checker.left_of(held.node); } else if held.text { value = this.c.checker.run_at(this.c.checker.right_of(held.node), 0); } else { value = this.c.checker.record(held.node, variable_slot::value); } this.at_node(held.node); } this.result = bt::void_type; this.plan_storage(value); val mark := this.begin_statement(); val address := this.build.value(op::global, span::::empty(), vt::word, 0, held.offset); val place := location { kind = held.kind, address = address }; val made := this.value_as(value, held.kind); this.adopt(&made); this.store(&place, &made); this.end_statement(mark); this.build.ret(span::::empty()); } #! The data holding a value computed while compiling, made where it is new: #! the memory it points into first, then the value, each address within them #! written where the data is placed, and each function as it is numbered here. #! @param this The compiler. #! @param key The constant's key. #! @param kind Its type. #! @returns The datum, or `no_block` where nothing was evaluated for it. pub def lowerer::evaluated_datum(&mut this, key: u64, kind: u32): u32 { if this.constant_data.get(&key) is some(got) { return got; } return no_block if !this.evaluated.values.contains(&key); # Each block is placed before any is written, since one may point into another. val count := this.evaluated.values[key].blocks.length; val first := this.program.data.length as u32; for _ in 0..count { this.program.data.append(datum { bytes = vector::::new(), align = 16, relocations = vector::::new() }); } for b in 0..count { var bytes := vector::::copy_of(&this.evaluated.values[key].blocks[b].bytes); var moved := vector::::new(); var addresses := vector::::copy_of(&this.evaluated.values[key].blocks[b].addresses); this.place_addresses(&mut bytes, &mut moved, addresses.view(), first); bytes.append(0); this.program.data[first as u64 + b].bytes = move(&mut bytes); this.program.data[first as u64 + b].relocations = move(&mut moved); } var bytes := vector::::copy_of(&this.evaluated.values[key].bytes); var moved := vector::::new(); var addresses := vector::::copy_of(&this.evaluated.values[key].addresses); this.place_addresses(&mut bytes, &mut moved, addresses.view(), first); bytes.append(0); val shape := this.shape_of(kind); val index := this.program.data.length as u32; this.program.data.append(datum { bytes = move(&mut bytes), align = shape.align < word_size ? word_size : shape.align, relocations = move(&mut moved) }); this.constant_data.set(key, index); return index; } #! Writes the addresses a value computed while compiling holds: each of a #! block as a relocation into the data the block became, each into another #! constant as one into the data that constant's value is, and each function #! as its index here plus one. #! @param this The compiler. #! @param bytes The bytes holding them. #! @param moved Where the relocations are written. #! @param addresses The addresses. #! @param first The datum the first block became. def lowerer::place_addresses(&mut this, bytes: &mut vector, moved: &mut vector, addresses: span, first: u32): void { for &each in addresses { if each.block == in_constant { # The constant was evaluated before the one pointing into it, so its data # never waits on this. moved.append(relocation { at = each.at, target = this.constant_datum(each.decl, each.table, each.node), addend = each.addend }); continue; } if each.block != no_block { moved.append(relocation { at = each.at, target = first + each.block, addend = each.addend }); continue; } var function: u32 = 0; if each.decl != 0 { function = this.function_of(each.decl, each.instance); } else { val saved := this.checker.at; this.checker.use_table(each.table); val kind := this.checker.type_at(each.node); this.checker.at = saved; function = this.lambda_of(each.table, each.node, each.file, kind, false); } this.program.functions[function].taken = true; val word := function as u64 + 1; write_number(bytes, each.at, word_size, word); } } #! The data holding another constant's value, which an address a value holds #! points into, made where it is new. #! @param this The compiler. #! @param decl The global constant, or 0 for a local one. #! @param table Of a local constant, the result table it was checked in. #! @param node Of a local constant, its `variable` node. #! @returns The datum. def lowerer::constant_datum(&mut this, decl: u32, table: u32, node: u32): u32 { if decl != 0 { return this.evaluated_datum(decl_key(decl), this.checker.decls.types.p[decl]); } val saved := this.checker.at; this.checker.use_table(table); val kind := this.checker.type_at(node); this.checker.at = saved; return this.evaluated_datum(node_key(table, node), kind); }