#! @file layouts.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the layouts of types, as the C compiler's `compiler__make_layout` #! makes them: scalars aligned to their size, as C aligns them, so a struct is #! laid out as C lays out one of the same fields, a union as its tag and then #! its largest field, and each with a map of the words that may hold a heap #! object. Also contains the signatures calls pass arguments by, and how many #! words a value of each layout is as a value of the intermediate code. import sakura::analyzer; module sakura::compiler; #! The type a value is laid out as: what its aliases stand for, and what a #! constant settles as where nothing converts it. #! @param this The compiler. #! @param kind The type. #! @returns The type laid out. pub def lowerer::layout_type(&mut this, kind: u32): u32 { var at := this.checker.unalias(kind); if this.checker.types.is_untyped(at) { at = this.checker.unalias(this.checker.settle(at)); } return at; } #! The layout of a type, made where it is new. #! @param this The compiler. #! @param kind The type. #! @returns The layout's index. pub def lowerer::layout_of(&mut this, kind: u32): u32 { val plain := this.layout_type(kind); if this.layout_ids.get(&plain) is some(got) { return got; } return this.make_layout(plain); } #! A copy of a layout, which stays as it is however the layouts grow. #! @param this The compiler. #! @param index The layout's index. #! @returns The layout. pub def lowerer::layout_at(&this, index: u32): layout { return this.program.layouts[index]; } #! The layout of a type's values: what kind it is, its size, and the rest. #! @param this The compiler. #! @param kind The type. #! @returns A copy of the layout. pub def lowerer::shape_of(&mut this, kind: u32): layout { val index := this.layout_of(kind); return this.program.layouts[index]; } #! A field of a layout, by its position. #! @param this The compiler. #! @param index The layout's index. #! @param at The field's position. #! @returns A copy of the field. pub def lowerer::field_at(&this, index: u32, at: u32): slot_field { val first: u32 = this.program.layouts[index].fields; return this.program.fields[first as u64 + at as u64]; } #! Makes the layout of a type not yet laid out. It is registered before its #! parts are laid out, so a type referring to itself through a reference or a #! pointer finds it. #! @param this The compiler. #! @param kind The type, as `layout_type` gives it. #! @returns The layout's index. def lowerer::make_layout(&mut this, kind: u32): u32 { this.laying++; val index := this.program.layouts.length as u32; this.program.layouts.append(layout { type_id = kind, align = 1, how = access::bytes }); this.layout_ids.set(kind, index); var made := layout { type_id = kind, align = 1, how = access::bytes }; var parts := vector::::new(); match this.checker.types.kind_of(kind) { ty::boolean, ty::untyped_bool => { made.kind = lk::boolean; made.bits = 8; made.size = 1; made.how = access::u8; } ty::signed, ty::unsigned => this.integer_layout(&mut made, this.checker.types.kind_of(kind) == ty::signed, this.checker.types.first_of(kind)); ty::floating => { val bits := this.checker.types.first_of(kind); made.kind = lk::floating; made.bits = bits; made.size = bits <= 32 ? 4 : 8; made.align = made.size; made.how = bits <= 32 ? access::f32 : access::f64; } ty::reference, ty::null_type => { val referent := this.checker.types.kind_of(kind) == ty::reference ? this.checker.types.first_of(kind) : 0; val traited := referent != 0 && this.checker.named_kind(this.checker.unalias(referent)) == dk::trait_type; made.kind = traited ? lk::trait_reference : lk::reference; made.size = traited ? 2 * word_size : word_size; made.align = word_size; made.how = traited ? access::bytes : access::i64; } ty::pointer => this.pointer_layout(&mut made); ty::fixed => { val element := this.layout_of(this.checker.types.first_of(kind)); val inner := this.layout_at(element); made.kind = lk::fixed; made.element = element + 1; made.count = this.checker.types.part_count(kind) == 0 ? this.checker.types.second_of(kind) : 0; made.align = inner.align; made.size = inner.size * made.count as u64; } ty::function => { made.kind = lk::function; made.size = 2 * word_size; made.align = word_size; } ty::named => this.named_layout(kind, &mut made, &mut parts); else => {} } # A view is written as the elements it views, and a view of bytes, like the # string that owns them, as the text itself. Both keep where the elements # begin in a pointer field, with how many there are in the field after it. if made.kind == lk::record && parts.length >= 2 { val viewed := this.viewed(kind); val element: u32 = viewed != 0 ? this.layout_of(viewed) + 1 : 0; var bytes := this.is_string(kind); if element != 0 { val inner := this.layout_at(element - 1); bytes = bytes || (inner.size == 1 && inner.kind != lk::record); } for i in 0..parts.length - 1 { break if !bytes && element == 0; continue if this.layout_at(parts[i].layout).kind != lk::pointer; made.texts = bytes; made.views = !bytes; made.viewed = bytes ? 0 : element; made.text = i as u32; break; } } made.size = round_up(made.size, made.align); val scalar := made.kind == lk::signed || made.kind == lk::unsigned || made.kind == lk::floating || made.kind == lk::boolean || made.kind == lk::pointer; made.frame = scalar ? word_size : round_up(made.size, word_size); # Each part is kept in a run of its own, after whatever laying out the parts # made. made.fields = this.program.fields.length as u32; made.count = made.kind == lk::fixed ? made.count : parts.length as u32; for &each in parts { this.program.fields.append(each); } this.note_references(&mut made, parts.view()); # A reference or pointer is laid out without what it refers to, which may be a # type still being laid out, or one holding a type that is, as `node` holds # the `maybe<&node>` whose `&node` reaches it. So that comes once the outermost # layout is made, where every size is known. this.program.layouts[index] = made; val held := this.checker.types.kind_of(kind); if (held == ty::reference && made.kind == lk::reference) || held == ty::pointer { this.referents.append(index); this.referents.append(this.checker.types.first_of(kind)); } this.laying--; if this.laying == 0 { this.laying = 1; var at: u64 = 0; while at < this.referents.length { val reaching: u32 = this.referents[at]; val element := this.layout_of(this.referents[at + 1]); this.program.layouts[reaching].element = element + 1; at += 2; } this.referents.clear(); this.laying = 0; } return index; } #! Lays out an integer of the bits, aligned to its size as C aligns it. #! @param this The compiler. #! @param made The layout being made. #! @param signed Whether it is signed. #! @param bits Its bits. def lowerer::integer_layout(&mut this, made: &mut layout, signed: bool, bits: u32): void { made.kind = signed ? lk::signed : lk::unsigned; made.bits = bits; made.size = bits <= 8 ? 1 : bits <= 16 ? 2 : bits <= 32 ? 4 : 8; made.align = made.size; made.how = match made.size { 1 => signed ? access::i8 : access::u8, 2 => signed ? access::i16 : access::u16, 4 => signed ? access::i32 : access::u32, else => access::i64 }; } #! Lays out a pointer, an address the program follows itself, as the address #! of a C function is too. #! @param this The compiler. #! @param made The layout being made. def lowerer::pointer_layout(&mut this, made: &mut layout): void { made.kind = lk::pointer; made.size = word_size; made.align = word_size; made.how = access::i64; } #! Lays out a named type: a struct, a union, an enum, or the address of a C #! function. #! @param this The compiler. #! @param kind The type. #! @param made The layout being made. #! @param parts Where its fields are written. def lowerer::named_layout(&mut this, kind: u32, made: &mut layout, parts: &mut vector): void { val decl: u32 = this.checker.types.first_of(kind); if this.checker.is_std(kind, word::cfn) { this.pointer_layout(made); return; } match this.checker.decls.kinds.p[decl] { dk::enum_type => { # An enum is laid out as its integer, naming each member as a field whose # offset is its value. val integer := this.layout_at(this.layout_of(this.checker.enum_integer(kind))); made.kind = integer.kind; made.bits = integer.bits; made.size = integer.size; made.align = integer.align; made.how = integer.how; made.name = this.checker.decls.names.p[decl]; val run: u32 = this.checker.decls.children.p[decl]; for i in 0..this.checker.decls.list_length(run) { val member := this.checker.decls.list_at(run, i as u32); var value := constant {}; this.checker.decl_value(member, &mut value); parts.append(slot_field { name = this.checker.decls.names.p[member], offset = value.bits(), decl = member }); } } dk::struct_type, dk::union_type => { # An instance of a generic is named with its arguments, as `pair`. made.name = this.checker.decls.names.p[decl]; if this.checker.types.part_count(kind) != 0 { val named := this.checker.describe(kind); made.name = this.checker.names.intern(&named.view()); } val run: u32 = this.checker.decls.children.p[decl]; for i in 0..this.checker.decls.list_length(run) { val field := this.checker.decls.list_at(run, i as u32); continue if this.checker.decls.kinds.p[field] != dk::field; this.checker.resolve(field); val held := this.checker.instantiated(this.checker.decls.types.p[field], decl, kind); parts.append(slot_field { name = this.checker.decls.names.p[field], layout = this.layout_of(held), type_id = held, decl = field, spread = this.checker.is_spread(field) }); } val is_union := this.checker.decls.kinds.p[decl] == dk::union_type; if is_union && this.checker.decls.has(decl, df::foreign) { # Every field of a C union begins where it does, as an `extern module` lays it out. made.kind = lk::record; for &each in parts { val inner := this.layout_at(each.layout); made.size = inner.size > made.size ? inner.size : made.size; made.align = inner.align > made.align ? inner.align : made.align; } return; } if !is_union { made.kind = lk::record; var offset: u64 = 0; for i in 0..parts.length { val inner := this.layout_at(parts[i].layout); offset = round_up(offset, inner.align); parts[i].offset = offset; offset += inner.size; made.align = inner.align > made.align ? inner.align : made.align; } made.size = offset; return; } # A union of an empty field and a reference, a trait reference, a # function value, or a struct of one of them alone, none of which is ever # null, holds no tag: it holds the empty field where its first word is # null, as `none` of `maybe<&T>` and `maybe>`. made.kind = lk::variant; if parts.length == 2 { val first := this.layout_at(parts[0].layout); val second := this.layout_at(parts[1].layout); var held: u32 = 0; if first.size == 0 && this.never_null(&second) { held = 2; } else if second.size == 0 && this.never_null(&first) { held = 1; } if held != 0 { val payload := this.layout_at(parts[held - 1].layout); made.niche = held; made.size = payload.size; made.align = payload.align; parts[0].offset = 0; parts[1].offset = 0; return; } } # Every field of a union is the payload after the tag, which is its index. made.align = word_size; var payload: u64 = 0; for i in 0..parts.length { val inner := this.layout_at(parts[i].layout); parts[i].offset = word_size; payload = inner.size > payload ? inner.size : payload; made.align = inner.align > made.align ? inner.align : made.align; } made.size = word_size + payload; } else => {} } } #! Notes whether a layout may hold a reference: as a reference, a trait #! reference, or a function value, which holds its environment, or as what #! holds one by value. #! @param this The compiler. #! @param made The layout being made. #! @param parts Its fields. def lowerer::note_references(&mut this, made: &mut layout, parts: span): void { match made.kind { lk::reference, lk::trait_reference, lk::function => made.pointers = true; lk::fixed => { if made.element != 0 { made.pointers = this.layout_at(made.element - 1).pointers; } } lk::record, lk::variant => { for &each in parts { if each.layout != 0 && this.layout_at(each.layout).pointers { made.pointers = true; } } } else => {} } } #! The elements a view of std views, or 0 for anything else: both the read-only #! `span` and the writable `slice` are views, and hold the same two words. #! @param this The compiler. #! @param kind The type. #! @returns The type of the elements, or 0. pub def lowerer::viewed(&mut this, kind: u32): u32 { val plain := this.checker.unalias(kind); if this.checker.is_std(plain, word::span) || this.checker.is_std(plain, word::slice) { return this.checker.types.part_count(plain) != 0 ? this.checker.types.part_at(plain, 0) : 0; } return 0; } #! Whether a type is std's `string`, which owns the text it holds. #! @param this The compiler. #! @param kind The type. #! @returns Whether it is. pub def lowerer::is_string(&mut this, kind: u32): bool { return this.checker.is_std(this.checker.unalias(kind), word::string); } #! The field a union holds, by its index, given the first word of its value: #! its tag, or of a union laid out without one, the field laid out where the #! word is not null, and the empty one where it is. #! @param held The union's layout. #! @param first The first word of its value. #! @returns The field's index. pub def held_field(held: &layout, first: u64): u64 { return first if held.niche == 0; val laid: u64 = held.niche as u64 - 1; return first != 0 ? laid : 1 - laid; } #! Whether a value of the layout is never null in its first word: a reference, a #! trait reference, a function value, or a struct whose one field is one, as #! `box`, which a union holding it beside an empty field takes as its tag. #! @param this The compiler. #! @param held The layout. #! @returns Whether it is. pub def lowerer::never_null(&mut this, held: &layout): bool { if held.kind == lk::record && held.count == 1 { val field: slot_field = this.program.fields[held.fields as u64]; val inner := this.layout_at(field.layout); return field.offset == 0 && inner.size == held.size && this.never_null(&inner); } return held.kind == lk::reference || held.kind == lk::trait_reference || held.kind == lk::function; } #! Whether a value of the layout is held in memory, at an address, rather than #! as words: a fixed array, a struct, or a union. #! @param held The layout. #! @returns Whether it is. pub def in_memory(held: &layout): bool { return held.kind == lk::fixed || held.kind == lk::record || held.kind == lk::variant; } #! Whether a function giving a value of the layout returns it in memory, at an #! address its call gives: one held in memory that has bytes. A struct of no #! fields is returned as nothing. #! @param held The layout. #! @returns Whether it is. pub def returned_in_memory(held: &layout): bool { return in_memory(held) && held.size != 0; } #! How many words a value of the layout is, as a value of the code: none for #! `void` and what is held in memory. #! @param held The layout. #! @returns How many. pub def word_count(held: &layout): u32 { return match held.kind { lk::empty, lk::fixed, lk::record, lk::variant => 0, lk::reference => 1, lk::trait_reference, lk::function => 2, else => 1 }; } #! The type of a word of a value of the layout, as a value of the code. #! @param held The layout. #! @returns A real for a float, and a word for anything else. pub def word_type(held: &layout): vt { return held.kind == lk::floating ? vt::real : vt::word; } #! The signature of calls through a function type: the layout of each of its #! parameters, where each begins, and its result's layout. #! @param this The compiler. #! @param kind The function type. #! @returns The signature's index. pub def lowerer::signature_of(&mut this, kind: u32): u32 { val plain := this.checker.unalias(kind); if this.signature_ids.get(&plain) is some(got) { return got; } var params := vector::::new(); var offsets := vector::::new(); var size: u64 = 0; for i in 0..this.checker.types.part_count(plain) { val each := this.layout_of(this.checker.types.part_at(plain, i as u32)); params.append(each); offsets.append(size); size += this.layout_at(each).frame; } val result := this.layout_of(this.checker.types.first_of(plain)); val index := this.program.signatures.length as u32; this.program.signatures.append(signature { params = move(&mut params), offsets = move(&mut offsets), size = size, result = result }); this.signature_ids.set(plain, index); return index; } #! The types of the values the code of a function of the signature takes: the #! words of each parameter, or the address of one held in memory. #! @param this The compiler. #! @param index The signature. #! @returns The types. pub def lowerer::param_types(&this, index: u32): vector { var made := vector::::new(); val held := &this.program.signatures[index]; for &each in held.params { val shape := this.layout_at(each); if in_memory(&shape) { made.append(vt::word); continue; } for _ in 0..word_count(&shape) { made.append(word_type(&shape)); } } return made; }