#! @file intrinsics.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the intrinsics, the functions std declares `@intrinsic` that the #! compiler gives: an operation of their own, or a value worked out while #! compiling, rather than a call. import sakura::analyzer; module sakura::compiler; #! Lowers a call of an intrinsic. #! @param this The emitter. #! @param node The `call` node. #! @param decl The intrinsic's declaration. #! @returns What it gives. pub def emitter::intrinsic(&mut this, node: u32, decl: u32): operand { val name := this.c.checker.names.text(this.c.checker.decls.names.p[decl]); val kind := this.c.checker.type_at(node); val run := this.c.checker.right_of(node); # A type's layout is known while compiling. if name == "size_of" || name == "align_of" { val instance := this.called(node); var size: u64 = 0; if instance != 0 { val args: u32 = this.c.checker.instances[instance].args; val held := this.c.shape_of(this.c.checker.types.part_at(args, 0)); size = name == "size_of" ? held.size : held.align; } return operand::one(kind, this.build.word(size)); } if name == "os" || name == "arch" || name == "backend" { val value := name == "os" ? this.c.checker.target.os : name == "arch" ? this.c.checker.target.arch : this.c.checker.target.backend; return this.text_value(this.c.checker.names.text(value), kind); } if name == "call_site" { return this.call_site(node, kind); } if name == "contracts" { return operand::one(kind, this.build.word(this.c.checker.contracts_off ? 0 : 1)); } return this.compiler_value(kind) if name == "compiler"; # These read the argument as it is written: in a default, the argument the # call wrote for the parameter it names. if name == "source_of" || name == "values_of" { val arg := this.c.checker.run_length(run) != 0 ? this.c.checker.run_at(run, 0) : 0; val written := arg != 0 ? this.written_argument(arg) : 0; val saved := this.c.checker.at; if written != 0 { this.c.checker.use_table(this.defaulting.table); } val chosen := written != 0 ? written : arg; var made := operand {}; if name == "source_of" { var text := string::new(nothing {}); if chosen != 0 { this.source_text(chosen, &mut text); } this.c.checker.at = saved; made = this.text_value(text.span, kind); } else { val outer := this.defaulting; this.defaulting = default_site {}; made = this.values_of(chosen, kind); this.defaulting = outer; this.c.checker.at = saved; } return made; } if name == "name_of" || name == "path_of" { # The checker keeps the type named as the callee's. val named := this.c.checker.type_at(this.c.checker.left_of(node)); var text := string::new(nothing {}); if named != 0 && named != bt::error { text = name == "path_of" ? this.c.checker.describe_path(named) : this.c.checker.describe(named); } return this.text_value(text.span, kind); } if name == "value_of" { if this.c.checker.is_known(node) && this.c.is_scalar_type(kind) { return this.known(&this.c.checker.value_at(node), kind); } val ref := this.c.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::enum_member { var value := constant {}; this.c.checker.decl_value(ref, &mut value); return this.known(&value, kind); } if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::variable { val place := this.global_place(ref); return this.load(&place); } if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::function { return this.function_value(node, ref); } # An attribute is the value it writes, made where it is read. if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::attribute { val saved := this.c.checker.at; this.c.checker.use_table(this.c.checker.units[this.c.checker.decls.files.p[ref]].table); val made := this.construct_of(this.c.checker.decls.types.p[ref], this.c.checker.right_of(this.c.checker.decls.nodes.p[ref])); this.c.checker.at = saved; return made; } this.problem(node, "`value_of` reaches nothing lowering knows."); return this.zero_value(kind); } if name == "attached" { return this.attached(node, kind); } if name == "compile_error" { this.build.unreachable(); return operand::none(kind); } if name == "lanes_have" || name == "lanes_apply" { return this.lanes(node, name == "lanes_have", kind); } # Vectors are values held in registers between operations in an executable # optimized with inlining and promotion, and nowhere else. if name == "lanes_held" { val native := !this.c.options.evaluating && this.c.checker.names.text(this.c.checker.target.backend) == "native"; return operand::one(kind, this.build.word(native && this.c.options.level >= 2 ? 1 : 0)); } if name.starts_with("atomic_") { return this.atomic(node, name, kind); } # `fault` takes its text's two words, wherever the view is. if name == "fault" { val text := this.value(this.c.checker.run_at(run, 0)); var words: u32[2] = [0, 0]; if text.address != 0 { words[0] = this.build.load(access::i64, text.address, 0); words[1] = this.build.load(access::i64, text.address, word_size); } else { words[0] = text.words[0]; words[1] = text.words[1]; } this.build.effect(op::fault, words[0:2], 0, 0); return operand::none(kind); } # The rest are operations, given their arguments' values. var args := vector::::new(); for i in 0..this.c.checker.run_length(run) { val made := this.value(this.c.checker.run_at(run, i as u32)); made.args(&mut args); } # A member of an enum's place among the values of its members, and the member # at a place, by which a range of its members counts. if name == "rank_of" || name == "member_at" { val instance := this.called(node); val members := instance != 0 ? this.c.checker.types.part_at(this.c.checker.instances[instance].args, 0) : bt::error; if members == bt::error || this.c.checker.named_kind(this.c.checker.unalias(members)) != dk::enum_type { this.problem(node, "`rank_of` and `member_at` count the members of an enum."); return this.zero_value(kind); } val made := name == "rank_of" ? this.member_rank(args[0], members) : this.member_of_rank(args[0], members); return operand::one(kind, made); } if name == "raw_allocate" || name == "raw_allocate_zeroed" { val uncleared: u32 = name == "raw_allocate" ? 1 : 0; return operand::one(kind, this.build.value(op::allocate, args.span, vt::word, uncleared, 0)); } if name == "raw_release" { this.build.effect(op::release, args.span, 0, 0); return operand::none(kind); } # `core::close` closes a value where it is, as its scope ending would: its own # `close`, then what it holds that closes. if name == "close_where_it_is" { val instance := this.called(node); if instance != 0 && args.length > 0 { val closed := this.c.checker.types.part_at(this.c.checker.instances[instance].args, 0); val place := location { kind = closed, address = args[0] }; this.close(&place); } return operand::none(kind); } if name == "raw_resize" { return operand::one(kind, this.build.value(op::resize, args.span, vt::word, 0, 0)); } if name == "syscall" { return operand::one(kind, this.build.value(op::syscall, args.span, vt::word, 0, 0)); } if name == "frame" || name == "executable" || name == "environ" || name == "take_signals" || name == "current" { val chosen := name == "frame" ? op::frame : name == "executable" ? op::image : name == "environ" ? op::environ : name == "current" ? op::current : op::signals; return operand::one(kind, this.build.value(chosen, span::::blank(), vt::word, 0, 0)); } # A thread runs the function value, of two words, given the context, a word; # the runtime starts one of the system on a record's stacks. if name == "start" || name == "clone_thread" { return operand::one(kind, this.build.value(name == "start" ? op::start : op::clone, args.span, vt::word, 0, 0)); } if name == "catch_signal" { return operand::one(kind, this.build.value(op::catch, args.span, vt::word, 0, 0)); } if name == "exit" { this.build.effect(op::exit, args.span, 0, 0); return operand::none(kind); } if name == "abort" { this.build.effect(op::abort, span::::blank(), 0, 0); return operand::none(kind); } if name == "trap" { this.build.effect(op::trap, args.span, 0, 0); return operand::none(kind); } # Operations of the machine on a whole real or word, which `std::math` narrows # for the other types. if name == "square_root" { return operand::one(kind, this.build.unary(op::fsqrt, args[0])); } if name == "ones_in" || name == "zeros_leading" || name == "zeros_trailing" || name == "bytes_reversed" { val chosen := name == "ones_in" ? op::ones : name == "zeros_leading" ? op::clz : name == "zeros_trailing" ? op::ctz : op::bswap; return operand::one(kind, this.build.unary(chosen, args[0])); } this.problem(node, "The intrinsic is not lowered."); return this.zero_value(kind); } #! `lanes_have` and `lanes_apply`, of an operation on lanes: in an executable, #! whether the processor it runs on has the vector instructions working it, as #! it found when it started, and the lanes worked with them. The virtual machine #! and constants have none, so never reach the second. #! @param this The emitter. #! @param node The `call` node. #! @param have Whether it is `lanes_have`. #! @param kind What it gives. #! @returns What it gives. def emitter::lanes(&mut this, node: u32, have: bool, kind: u32): operand { # Its type arguments are the type of lane, the operation, and how many lanes. var lane := lane_none; var operation: u32 = 0; var count: u64 = 0; val instance := this.called(node); if instance != 0 { val args: u32 = this.c.checker.instances[instance].args; lane = this.lane_of(this.c.checker.types.part_at(args, 0)); operation = this.constant_argument(this.c.checker.types.part_at(args, 1)) as u32; count = this.constant_argument(this.c.checker.types.part_at(args, 2)); } val native := !this.c.options.evaluating && this.c.checker.names.text(this.c.checker.target.backend) == "native"; val need := native && lane != lane_none ? lanes_needs(operation, lane, count * lane_bits(lane) / 8) : cpu_never; if have { return operand::one(kind, this.build.word(0)) if need == cpu_never; return operand::one(kind, this.build.word(1)) if need == cpu_sse2; val found := this.build.value(op::cpu, span::::blank(), vt::word, 0, 0); val held := this.build.binary(op::band, found, this.build.word(need)); return operand::one(kind, this.build.binary(op::eq, held, this.build.word(need))); } if need == cpu_never { this.build.unreachable(); return operand::none(kind); } # The pointers: where the result goes, then the operands. var pointers := vector::::new(); val run := this.c.checker.right_of(node); for i in 0..this.c.checker.run_length(run) { val made := this.value(this.c.checker.run_at(run, i as u32)); made.args(&mut pointers); } # The operands are read, worked as values, and the result written, so a # vector a cell holds is a value wherever the cell is promoted. val bytes := count * lane_bits(lane) / 8; var args := vector::::new(); for k in 0..lanes_operands(operation) { val how := lanes_operand_access(operation, lane, bytes, k); args.append(this.build.load(how, pointers[1 + k as u64], 0)); } val result := lanes_result_access(operation, lane, bytes); val made := operation == lanes_copy ? args[0] : this.build.value(op::lanes, args.span, result.value_type(), operation, lane as u64 | bytes << 8); this.build.store(result, pointers[0], 0, made); return operand::none(kind); } #! The atomics of `core::atomic`: an operation on the `T` at an address, in one #! step, which is the first type argument, and of `atomic_fetch` the operation #! the second. A compare-exchange gives whether it wrote, and writes what it #! found where a pointer it is given points. #! @param this The emitter. #! @param node The `call` node. #! @param name The intrinsic's name. #! @param kind What it gives. #! @returns What it gives. def emitter::atomic(&mut this, node: u32, name: strview, kind: u32): operand { var operation := name == "atomic_load" ? atomic_load : name == "atomic_store" ? atomic_store : name == "atomic_exchange" ? atomic_exchange : name == "atomic_compare_exchange" ? atomic_compare_exchange : name == "atomic_fence" ? atomic_fence : atomic_add; var how := access::i64; var unsigned: u64 = 0; val instance := this.called(node); if instance != 0 && operation != atomic_fence { val args: u32 = this.c.checker.instances[instance].args; val held := this.c.checker.types.part_at(args, 0); how = this.c.shape_of(held).how; if this.c.checker.types.kind_of(this.c.checker.unalias(held)) == ty::unsigned { unsigned = atomic_unsigned; } if name == "atomic_fetch" { operation = this.constant_argument(this.c.checker.types.part_at(args, 1)) as u32; } } var values := vector::::new(); val run := this.c.checker.right_of(node); for i in 0..this.c.checker.run_length(run) { val made := this.value(this.c.checker.run_at(run, i as u32)); made.args(&mut values); } if operation == atomic_fence { this.build.effect(op::atomic, values.span, operation, 0); return operand::none(kind); } # While compiling, what it reaches is checked as a read through a pointer is. this.reaches(values[0], this.build.word(1), how.width()); if operation == atomic_compare_exchange { this.reaches(values[3], this.build.word(1), how.width()); var args: u32[5] = [values[0], values[1], values[2], values[4], values[5]]; val found := this.build.value(op::atomic, args[0:5], vt::word, operation, how as u64 | unsigned); this.build.store(how, values[3], 0, found); return operand::one(kind, this.build.binary(op::eq, found, values[1])); } if atomic_gives(operation) { return operand::one(kind, this.build.value(op::atomic, values.span, vt::word, operation, how as u64 | unsigned)); } this.build.effect(op::atomic, values.span, operation, how as u64 | unsigned); return operand::none(kind); } #! The type of lane a number is, as vector instructions take it. #! @param this The emitter. #! @param kind The type. #! @returns The type of lane, or `lane_none` where it is no number. def emitter::lane_of(&mut this, kind: u32): u32 { val plain := this.c.checker.unalias(kind); val of := this.c.checker.types.kind_of(plain); val bits := this.c.checker.types.first_of(plain); if of == ty::floating { return bits == 32 ? lane_f32 : lane_f64; } return lane_none if of != ty::signed && of != ty::unsigned; val width: u32 = bits == 8 ? 0 : bits == 16 ? 2 : bits == 32 ? 4 : 6; return width + (of == ty::unsigned ? 1 : 0); } #! The number a constant type argument is. #! @param this The emitter. #! @param arg The type argument. #! @returns The number, or 0 where it is none. def emitter::constant_argument(&mut this, arg: u32): u64 { return 0 if this.c.checker.types.kind_of(arg) != ty::value; return this.c.checker.known_at(this.c.checker.types.second_of(arg)).value.magnitude; } #! Where a call is written, as `core::site`: its file, line, and column, and the #! signature of the function holding it. #! @param this The emitter. #! @param node The `call` node. #! @param kind The `site` type. #! @returns The value. def emitter::call_site(&mut this, node: u32, kind: u32): operand { # In a default, it is where the call leaving the parameter out is. val file := this.defaulting.node != 0 ? this.defaulting.file : this.c.checker.at.file; val site := this.defaulting.node != 0 ? this.defaulting.node : node; val held := &this.c.checker.units[file].source; val offset := this.c.node_offset(file, site); val at := held.lines.place_of(offset); val plain := this.c.checker.unalias(kind); val index := this.c.layout_of(plain); val shape := this.c.layout_at(index); val address := this.temporary_of(kind); this.build.clear(address, shape.size); val whole := location { kind = kind, address = address, framed = true }; var signature := string::new(nothing {}); val decl: u32 = this.c.program.functions[this.function].decl; if decl != 0 { this.c.signature_text(decl, &mut signature); } for i in 0..shape.count { val field := this.c.field_at(index, i as u32); val name := this.c.checker.names.text(field.name); val place := this.within(&whole, field.offset, field.type_id); var value := operand {}; if name == "path" { value = this.text_value(held.path, field.type_id); } else if name == "line" { value = operand::one(field.type_id, this.build.word(at.line as u64)); } else if name == "column" { value = operand::one(field.type_id, this.build.word(at.column as u64)); } else if name == "signature" { value = this.text_value(signature.span, field.type_id); } else { continue; } this.store(&place, &value); } return operand::at(kind, address); } #! The version of the compiler, as `core::version`: its `major`, `minor`, and #! `patch`. #! @param this The emitter. #! @param kind The `version` type. #! @returns The value. def emitter::compiler_value(&mut this, kind: u32): operand { val index := this.c.layout_of(this.c.checker.unalias(kind)); val shape := this.c.layout_at(index); val address := this.temporary_of(kind); this.build.clear(address, shape.size); val whole := location { kind = kind, address = address, framed = true }; for i in 0..shape.count { val field := this.c.field_at(index, i as u32); val part := version_part(this.c.checker.names.text(field.name)); continue if part < 0; val place := this.within(&whole, field.offset, field.type_id); val value := operand::one(field.type_id, this.build.word(part as u64)); this.store(&place, &value); } return operand::at(kind, address); } #! The part of the compiler's version a field of `core::version` holds. #! @param name The field's name. #! @returns The part, or -1 where the field is none of them. def version_part(name: strview): i64 { return compiler_version.major as i64 if name == "major"; return compiler_version.minor as i64 if name == "minor"; return compiler_version.patch as i64 if name == "patch"; return -1; } #! The text an expression is written as, without the spaces around it. #! @param this The emitter. #! @param node The expression. #! @param out Where the text is written. pub def emitter::source_text(&mut this, node: u32, out: &mut string): void { this.c.checker.written_text(node, out); } #! What the operands of an expression held, as `values_of` gives it: both #! operands of a comparison, or the expression itself, each as `text = value`, #! but for literals, which say what they hold already. #! @param this The emitter. #! @param node The expression, or 0. #! @param kind The `string` type. #! @returns The text. def emitter::values_of(&mut this, node: u32, kind: u32): operand { this.build.effect(op::format, span::::blank(), 0, 0); if node != 0 { this.write_values(node, ""); } return this.owned_text(kind); } #! Writes what the operands of an expression held into the text being made, as #! `values_of` gives it, after a lead where there are any. #! @param this The emitter. #! @param node The expression. #! @param lead What comes before the first, as `, with `, or nothing. pub def emitter::write_values(&mut this, node: u32, lead: strview): void { var named := vector::::new(); val shape := this.c.checker.kind_of(node); if shape == nk::binary { val operator := this.c.checker.token_kind(node); if operator.compares() { named.append(this.c.checker.left_of(node)); named.append(this.c.checker.right_of(node)); } } else if shape != nk::unary { named.append(node); } var first := true; for &each in named { val literal := this.c.checker.kind_of(each); continue if literal == nk::number || literal == nk::string || literal == nk::character || literal == nk::boolean || literal == nk::null_lit; if !first { this.build.effect(op::format_text, span::::blank(), this.c.text_datum(", "), 0); } else if lead.length != 0 { this.build.effect(op::format_text, span::::blank(), this.c.text_datum(lead), 0); } first = false; var text := string::new(nothing {}); this.source_text(each, &mut text); this.build.effect(op::format_text, span::::blank(), this.c.text_datum(text.span), 0); this.build.effect(op::format_text, span::::blank(), this.c.text_datum(" = "), 0); val mark := this.cell_mark(); val value := this.value(each); this.format_value(&value); this.release(mark); } } #! `attached::(d)`: a view of the values of the attributes of the type `A` #! written on the declaration `d` describes, in the order they are written, each #! made as the struct literal it is, where the declaration is. They are held as #! long as the function runs, since the view may outlive the statement. #! @param this The emitter. #! @param node The `call` node. #! @param kind The view's type. #! @returns The view. def emitter::attached(&mut this, node: u32, kind: u32): operand { val view := this.temporary_of(kind); this.build.clear(view, 2 * word_size); val run := this.c.checker.right_of(node); val wanted := this.c.viewed(kind); return operand::at(kind, view) if wanted == 0 || this.c.checker.run_length(run) == 0; val one := this.c.checker.reflected_one(this.c.checker.run_at(run, 0)); return operand::at(kind, view) if one == checker::no_reflection; val held: desc = this.c.checker.descs[one]; # The attributes of the type, in the order they are written. val decl := held.decl; var types := this.c.checker.attribute_types(decl, held.param); val saved := this.c.checker.at; this.c.checker.use_table(this.c.checker.units[this.c.checker.decls.files.p[decl]].table); val attrs := this.c.checker.attributes_at(decl, held.param); var chosen := vector::::new(); var at: u64 = 0; for i in 0..this.c.checker.run_length(attrs) { val attr := this.c.checker.run_at(attrs, i as u32); continue if this.c.checker.kind_of(attr) != nk::attribute; if at < types.length && this.c.checker.strip(types[at], 0) == this.c.checker.strip(wanted, 0) { chosen.append(attr); } at++; } if chosen.length == 0 { this.c.checker.at = saved; return operand::at(kind, view); } val shape := this.c.shape_of(wanted); val array := this.c.checker.types.fixed(wanted, chosen.length as u32); val whole := this.c.shape_of(array); val cell := this.build.code.add_cell(round_up(whole.size, word_size) as u32, whole.align < word_size ? word_size as u32 : whole.align as u32, ck::local, 0); val first := this.build.cell_address(cell); for i in 0..chosen.length { val made := this.construct_of(wanted, this.c.checker.right_of(chosen[i])); val place := location { kind = wanted, address = first, offset = i * shape.size, framed = true }; this.store(&place, &made); } this.c.checker.at = saved; this.build.store(access::i64, view, 0, first); this.build.store(access::i64, view, word_size, this.build.word(chosen.length)); return operand::at(kind, view); }