P6A-8: Source Reference Bundle and harness profile aji_source
- source bundle: a /docs directory with QUICK.md (Quick Reference plus a where-to-read table), language/*.md, diagnostics/errors.md and the AIR bundle only under air/; built from docs/agent/source-bundle.txt (> output files, @manifest includes) by zig build agent-reference - Quick Reference examples are checked: every aji block must be an excerpt of a conformance program in tests/conformance/source/quickref (zig build test fails otherwise); QR fixes found by the check (mut d = data, region not auto-borrowed, statement/borrow blocks that could not verify, io_error match) - frontend: task.*/channel.*/mutex.* are not-yet-implemented, not E7006 - aji lower --stats: mechanical_air_eliminated_raw and _normalized (each byte-string materialization counted as one constant; spec 15 §11) - harness: profile aji_source (doc_groups), per-file and per-group docs metrics on distinctive lines, profile columns (tool calls, lowered AIR raw/normalized, docs files read, IR-form and type-mismatch counts), dev-echo aji_source variant, scripted run and test, source token report - plan: P6A-8 done, P6A-9 criteria; Q-021/Q-022 resolved; unsafe block deferred
This commit is contained in:
1 parent
61be766634
commit
3e2d9b63a7
35 files changed
+1170
-106
No files matched your search
@@ -69,3 +69,58 @@ test {
|
||||
_ = parse;
|
||||
_ = lower_mod;
|
||||
}
|
||||
|
||||
pub const Size = struct { bytes: usize, lines: usize };
|
||||
|
||||
/// AIR size with every byte-string materialization (the instructions from
|
||||
/// its `local.zero` to its final `borrow.end`) replaced by one notional
|
||||
/// line ` const.bytes %<storage>, b"<canonical literal>"` (spec 15 §11).
|
||||
/// Blank lines are not counted, as for the raw size.
|
||||
pub fn normalizedSize(arena: std.mem.Allocator, o: lower_mod.Output) std.mem.Allocator.Error!Size {
|
||||
var size: Size = .{ .bytes = 0, .lines = 0 };
|
||||
var it = std.mem.splitScalar(u8, o.air, '\n');
|
||||
var i: usize = 0;
|
||||
var last: u32 = 0;
|
||||
while (it.next()) |ln| : (i += 1) {
|
||||
const k: u32 = if (i < o.constant_of.len) o.constant_of[i] else 0;
|
||||
if (k != 0) {
|
||||
if (k != last) {
|
||||
const c = o.constants[k - 1];
|
||||
const text = try std.fmt.allocPrint(arena, " const.bytes %{s}, {s}", .{ c.storage, try canonicalBytes(arena, c.bytes) });
|
||||
size.bytes += text.len + 1;
|
||||
size.lines += 1;
|
||||
}
|
||||
last = k;
|
||||
continue;
|
||||
}
|
||||
last = 0;
|
||||
if (std.mem.trim(u8, ln, " \t\r").len == 0) continue;
|
||||
size.bytes += ln.len + 1;
|
||||
size.lines += 1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/// `b"…"` with printable ASCII as is and `\n \r \t \0 \\ \" \xNN` otherwise.
|
||||
pub fn canonicalBytes(arena: std.mem.Allocator, bytes: []const u8) std.mem.Allocator.Error![]const u8 {
|
||||
var out: std.ArrayList(u8) = .empty;
|
||||
try out.appendSlice(arena, "b\"");
|
||||
for (bytes) |c| switch (c) {
|
||||
'\n' => try out.appendSlice(arena, "\\n"),
|
||||
'\r' => try out.appendSlice(arena, "\\r"),
|
||||
'\t' => try out.appendSlice(arena, "\\t"),
|
||||
0 => try out.appendSlice(arena, "\\0"),
|
||||
'\\' => try out.appendSlice(arena, "\\\\"),
|
||||
'"' => try out.appendSlice(arena, "\\\""),
|
||||
0x20...0x21, 0x23...0x5b, 0x5d...0x7e => try out.append(arena, c),
|
||||
else => try out.print(arena, "\\x{x:0>2}", .{c}),
|
||||
};
|
||||
try out.append(arena, '"');
|
||||
return out.items;
|
||||
}
|
||||
|
||||
test canonicalBytes {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
try std.testing.expectEqualStrings("b\"hi\\n\\\"\\x01\"", try canonicalBytes(a.allocator(), "hi\n\"\x01"));
|
||||
}
|
||||
+36
-4
@@ -123,6 +123,13 @@ pub const Output = struct {
|
||||
/// The same AIR with `; line:col-line:col` comments before each
|
||||
/// instruction (not canonical; `aji lower --annotate`).
|
||||
annotated: []const u8,
|
||||
/// Byte-string materializations, for the normalized size metric
|
||||
/// (spec 15 §11): `constant_of[i]` is 0, or `k` when AIR line `i + 1`
|
||||
/// belongs to the materialization of `constants[k - 1]`.
|
||||
constant_of: []const u32,
|
||||
constants: []const Constant,
|
||||
|
||||
pub const Constant = struct { storage: []const u8, bytes: []const u8 };
|
||||
};
|
||||
|
||||
pub const Result = union(enum) {
|
||||
@@ -142,10 +149,12 @@ pub fn lower(arena: Allocator, m: ast.Module) Allocator.Error!Result {
|
||||
error.Unimplemented => return .{ .unimplemented = l.unimpl.? },
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
};
|
||||
return .{ .ok = .{ .air = l.out.items, .lines = l.lines.items, .annotated = l.annotated.items } };
|
||||
return .{ .ok = .{ .air = l.out.items, .lines = l.lines.items, .annotated = l.annotated.items, .constant_of = l.constant_of.items, .constants = l.constants.items } };
|
||||
}
|
||||
|
||||
const Inst = struct { text: []const u8, span: ?Span };
|
||||
/// `constant`: 0, or the 1-based index of the byte-string constant whose
|
||||
/// materialization the instruction belongs to.
|
||||
const Inst = struct { text: []const u8, span: ?Span, constant: u32 = 0 };
|
||||
|
||||
const Block = struct {
|
||||
name: []const u8,
|
||||
@@ -213,6 +222,10 @@ const Lowerer = struct {
|
||||
out: std.ArrayList(u8) = .empty,
|
||||
annotated: std.ArrayList(u8) = .empty,
|
||||
lines: std.ArrayList(?Span) = .empty,
|
||||
constant_of: std.ArrayList(u32) = .empty,
|
||||
constants: std.ArrayList(Output.Constant) = .empty,
|
||||
/// Set while a byte string is materialized: the tag for its instructions.
|
||||
in_constant: u32 = 0,
|
||||
problem: ?Problem = null,
|
||||
unimpl: ?@FieldType(Result, "unimplemented") = null,
|
||||
sigs: std.StringHashMapUnmanaged(FnSig) = .empty,
|
||||
@@ -302,6 +315,11 @@ const Lowerer = struct {
|
||||
// ------------------------------------------------------------ output
|
||||
|
||||
fn line(l: *Lowerer, text: []const u8, span: ?Span) Error!void {
|
||||
try l.lineTagged(text, span, 0);
|
||||
}
|
||||
|
||||
fn lineTagged(l: *Lowerer, text: []const u8, span: ?Span, constant: u32) Error!void {
|
||||
try l.constant_of.append(l.arena, constant);
|
||||
try l.out.appendSlice(l.arena, text);
|
||||
try l.out.append(l.arena, '\n');
|
||||
try l.lines.append(l.arena, span);
|
||||
@@ -539,7 +557,7 @@ const Lowerer = struct {
|
||||
}
|
||||
try head.appendSlice(l.arena, ":");
|
||||
try l.line(head.items, null);
|
||||
for (b.insts.items) |inst| try l.line(try l.fmt(" {s}", .{inst.text}), inst.span);
|
||||
for (b.insts.items) |inst| try l.lineTagged(try l.fmt(" {s}", .{inst.text}), inst.span, inst.constant);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -564,7 +582,7 @@ const Lowerer = struct {
|
||||
|
||||
fn emit(l: *Lowerer, text: []const u8, span: Span) Error!void {
|
||||
const b = &l.blocks.items[l.cur.?];
|
||||
try b.insts.append(l.arena, .{ .text = text, .span = span });
|
||||
try b.insts.append(l.arena, .{ .text = text, .span = span, .constant = l.in_constant });
|
||||
}
|
||||
|
||||
/// Emits `%name = text` and returns the value.
|
||||
@@ -1574,6 +1592,11 @@ const Lowerer = struct {
|
||||
if (bytes.len == 0) return l.fail("E7006", span, "an empty byte string has no array type", &.{});
|
||||
const ty = try l.arrayOf(.{ .int = "u8" }, bytes.len);
|
||||
const st = try l.storageFor("bytes", ty, span);
|
||||
// Everything from here to the final `borrow.end` is the constant's
|
||||
// initialization; the `local.alloc` is real frame storage.
|
||||
try l.constants.append(l.arena, .{ .storage = st, .bytes = bytes });
|
||||
l.in_constant = @intCast(l.constants.items.len);
|
||||
defer l.in_constant = 0;
|
||||
try l.emit(try l.fmt("local.zero %{s}", .{st}), span);
|
||||
const b = try l.def("w", try l.wrap(.mut, ty), try l.fmt("borrow.mut %{s}", .{st}), span);
|
||||
for (bytes, 0..) |byte, i| {
|
||||
@@ -2020,6 +2043,7 @@ const Lowerer = struct {
|
||||
|
||||
fn lowerFallible(l: *Lowerer, e: ast.Expr) Error!Fallible {
|
||||
if (e.kind != .call) return l.fail("E7006", e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
|
||||
if (laterLayer(e.kind.call.callee.*)) return l.notYet(e.span, "this operation (layers 7–8)");
|
||||
const path = pathOf(e.kind.call.callee.*) orelse return l.fail("E7006", e.span, "outcome arms (`ok`, `err`, …) belong to a fallible operation", &.{});
|
||||
const args = e.kind.call.args;
|
||||
const want = struct {
|
||||
@@ -2254,6 +2278,14 @@ fn elemOf(t: Ty) ?Ty {
|
||||
};
|
||||
}
|
||||
|
||||
/// `task.*`, `channel.*`, `mutex.*`: operations of layers 7–8.
|
||||
fn laterLayer(e: ast.Expr) bool {
|
||||
var buf: [64]u8 = undefined;
|
||||
const text = dotted(e, &buf) orelse return false;
|
||||
for ([_][]const u8{ "task.", "channel.", "mutex." }) |p| if (std.mem.startsWith(u8, text, p)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// The built-in operation named by a callee such as `own.new` or
|
||||
/// `cap.narrow.budget`; null for anything else.
|
||||
fn pathOf(e: ast.Expr) ?[]const u8 {
|
||||
|
||||
+15
-3
@@ -117,16 +117,28 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
}.count;
|
||||
const sc = code(source);
|
||||
const ac = code(o.air);
|
||||
const nc = try aji.frontend.normalizedSize(arena, o);
|
||||
const ratio = struct {
|
||||
fn f(a: usize, b: usize) f64 {
|
||||
return @as(f64, @floatFromInt(a)) / @as(f64, @floatFromInt(@max(b, 1)));
|
||||
}
|
||||
}.f;
|
||||
var s: std.json.Stringify = .{ .writer = out };
|
||||
try s.write(.{
|
||||
.source_bytes = sc.bytes,
|
||||
.source_lines = sc.lines,
|
||||
.air_bytes = ac.bytes,
|
||||
.air_lines = ac.lines,
|
||||
.expansion_bytes = @as(f64, @floatFromInt(ac.bytes)) / @as(f64, @floatFromInt(@max(sc.bytes, 1))),
|
||||
.expansion_lines = @as(f64, @floatFromInt(ac.lines)) / @as(f64, @floatFromInt(@max(sc.lines, 1))),
|
||||
.air_bytes_normalized = nc.bytes,
|
||||
.air_lines_normalized = nc.lines,
|
||||
.byte_string_constants = o.constants.len,
|
||||
.expansion_bytes = ratio(ac.bytes, sc.bytes),
|
||||
.expansion_lines = ratio(ac.lines, sc.lines),
|
||||
// 1 − source/AIR: the share of AIR text the frontend writes for the author.
|
||||
.mechanical_air_eliminated = 1.0 - @as(f64, @floatFromInt(sc.bytes)) / @as(f64, @floatFromInt(@max(ac.bytes, 1))),
|
||||
.mechanical_air_eliminated_raw = 1.0 - ratio(sc.bytes, ac.bytes),
|
||||
// The same against AIR with each byte-string materialization counted
|
||||
// as one logical constant operation (spec 15 §11).
|
||||
.mechanical_air_eliminated_normalized = 1.0 - ratio(sc.bytes, nc.bytes),
|
||||
});
|
||||
try out.writeByte('\n');
|
||||
return .accepted;
|
||||
|
||||
Reference in new issue
Block a user