mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
We've got a big one here! This commit reworks how we represent pointers in the InternPool, and rewrites the logic for loading and storing from them at comptime. Firstly, the pointer representation. Previously, pointers were represented in a highly structured manner: pointers to fields, array elements, etc, were explicitly represented. This works well for simple cases, but is quite difficult to handle in the cases of unusual reinterpretations, pointer casts, offsets, etc. Therefore, pointers are now represented in a more "flat" manner. For types without well-defined layouts -- such as comptime-only types, automatic-layout aggregates, and so on -- we still use this "hierarchical" structure. However, for types with well-defined layouts, we use a byte offset associated with the pointer. This allows the comptime pointer access logic to deal with reinterpreted pointers far more gracefully, because the "base address" of a pointer -- for instance a `field` -- is a single value which pointer accesses cannot exceed since the parent has undefined layout. This strategy is also more useful to most backends -- see the updated logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do prefer a chain of field and elements accesses for lowering pointer values, such as SPIR-V, there is a helpful function in `Value` which creates a strategy to derive a pointer value using ideally only field and element accesses. This is actually more correct than the previous logic, since it correctly handles pointer casts which, after the dust has settled, end up referring exactly to an aggregate field or array element. In terms of the pointer access code, it has been rewritten from the ground up. The old logic had become rather a mess of special cases being added whenever bugs were hit, and was still riddled with bugs. The new logic was written to handle the "difficult" cases correctly, the most notable of which is restructuring of a comptime-only array (for instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`. Currently, the logic for loading and storing work somewhat differently, but a future change will likely improve the loading logic to bring it more in line with the store strategy. As far as I can tell, the rewrite has fixed all bugs exposed by #19414. As a part of this, the comptime bitcast logic has also been rewritten. Previously, bitcasts simply worked by serializing the entire value into an in-memory buffer, then deserializing it. This strategy has two key weaknesses: pointers, and undefined values. Representations of these values at comptime cannot be easily serialized/deserialized whilst preserving data, which means many bitcasts would become runtime-known if pointers were involved, or would turn `undefined` values into `0xAA`. The new logic works by "flattening" the datastructure to be cast into a sequence of bit-packed atomic values, and then "unflattening" it; using serialization when necessary, but with special handling for `undefined` values and for pointers which align in virtual memory. The resulting code is definitely slower -- more on this later -- but it is correct. The pointer access and bitcast logic required some helper functions and types which are not generally useful elsewhere, so I opted to split them into separate files `Sema/comptime_ptr_access.zig` and `Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small public APIs. Whilst working on this branch, I caught various unrelated bugs with transitive Sema errors, and with the handling of `undefined` values. These bugs have been fixed, and corresponding behavior test added. In terms of performance, I do anticipate that this commit will regress performance somewhat, because the new pointer access and bitcast logic is necessarily more complex. I have not yet taken performance measurements, but will do shortly, and post the results in this PR. If the performance regression is severe, I will do work to to optimize the new logic before merge. Resolves: #19452 Resolves: #19460
195 lines
5.2 KiB
Zig
195 lines
5.2 KiB
Zig
//! An abstract tree representation of a Markdown document.
|
|
|
|
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
const assert = std.debug.assert;
|
|
const Allocator = std.mem.Allocator;
|
|
const Renderer = @import("renderer.zig").Renderer;
|
|
|
|
nodes: Node.List.Slice,
|
|
extra: []u32,
|
|
string_bytes: []u8,
|
|
|
|
const Document = @This();
|
|
|
|
pub const Node = struct {
|
|
tag: Tag,
|
|
data: Data,
|
|
|
|
pub const Index = enum(u32) {
|
|
root = 0,
|
|
_,
|
|
};
|
|
pub const List = std.MultiArrayList(Node);
|
|
|
|
pub const Tag = enum {
|
|
/// Data is `container`.
|
|
root,
|
|
|
|
// Blocks
|
|
/// Data is `list`.
|
|
list,
|
|
/// Data is `list_item`.
|
|
list_item,
|
|
/// Data is `container`.
|
|
table,
|
|
/// Data is `container`.
|
|
table_row,
|
|
/// Data is `table_cell`.
|
|
table_cell,
|
|
/// Data is `heading`.
|
|
heading,
|
|
/// Data is `code_block`.
|
|
code_block,
|
|
/// Data is `container`.
|
|
blockquote,
|
|
/// Data is `container`.
|
|
paragraph,
|
|
/// Data is `none`.
|
|
thematic_break,
|
|
|
|
// Inlines
|
|
/// Data is `link`.
|
|
link,
|
|
/// Data is `text`.
|
|
autolink,
|
|
/// Data is `link`.
|
|
image,
|
|
/// Data is `container`.
|
|
strong,
|
|
/// Data is `container`.
|
|
emphasis,
|
|
/// Data is `text`.
|
|
code_span,
|
|
/// Data is `text`.
|
|
text,
|
|
/// Data is `none`.
|
|
line_break,
|
|
};
|
|
|
|
pub const Data = union {
|
|
none: void,
|
|
container: struct {
|
|
children: ExtraIndex,
|
|
},
|
|
text: struct {
|
|
content: StringIndex,
|
|
},
|
|
list: struct {
|
|
start: ListStart,
|
|
children: ExtraIndex,
|
|
},
|
|
list_item: struct {
|
|
tight: bool,
|
|
children: ExtraIndex,
|
|
},
|
|
table_cell: struct {
|
|
info: packed struct {
|
|
alignment: TableCellAlignment,
|
|
header: bool,
|
|
},
|
|
children: ExtraIndex,
|
|
},
|
|
heading: struct {
|
|
/// Between 1 and 6, inclusive.
|
|
level: u3,
|
|
children: ExtraIndex,
|
|
},
|
|
code_block: struct {
|
|
tag: StringIndex,
|
|
content: StringIndex,
|
|
},
|
|
link: struct {
|
|
target: StringIndex,
|
|
children: ExtraIndex,
|
|
},
|
|
|
|
comptime {
|
|
// In Debug and ReleaseSafe builds, there may be hidden extra fields
|
|
// included for safety checks. Without such safety checks enabled,
|
|
// we always want this union to be 8 bytes.
|
|
if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
|
|
assert(@sizeOf(Data) == 8);
|
|
}
|
|
}
|
|
};
|
|
|
|
/// The starting number of a list. This is either a number between 0 and
|
|
/// 999,999,999, inclusive, or `unordered` to indicate an unordered list.
|
|
pub const ListStart = enum(u30) {
|
|
// When https://github.com/ziglang/zig/issues/104 is implemented, this
|
|
// type can be more naturally expressed as ?u30. As it is, we want
|
|
// values to fit within 4 bytes, so ?u30 does not yet suffice for
|
|
// storage.
|
|
unordered = std.math.maxInt(u30),
|
|
_,
|
|
|
|
pub fn asNumber(start: ListStart) ?u30 {
|
|
if (start == .unordered) return null;
|
|
assert(@intFromEnum(start) <= 999_999_999);
|
|
return @intFromEnum(start);
|
|
}
|
|
};
|
|
|
|
pub const TableCellAlignment = enum(u2) {
|
|
unset,
|
|
left,
|
|
center,
|
|
right,
|
|
};
|
|
|
|
/// Trailing: `len` times `Node.Index`
|
|
pub const Children = struct {
|
|
len: u32,
|
|
};
|
|
};
|
|
|
|
pub const ExtraIndex = enum(u32) { _ };
|
|
|
|
/// The index of a null-terminated string in `string_bytes`.
|
|
pub const StringIndex = enum(u32) {
|
|
empty = 0,
|
|
_,
|
|
};
|
|
|
|
pub fn deinit(doc: *Document, allocator: Allocator) void {
|
|
doc.nodes.deinit(allocator);
|
|
allocator.free(doc.extra);
|
|
allocator.free(doc.string_bytes);
|
|
doc.* = undefined;
|
|
}
|
|
|
|
/// Renders a document directly to a writer using the default renderer.
|
|
pub fn render(doc: Document, writer: anytype) @TypeOf(writer).Error!void {
|
|
const renderer: Renderer(@TypeOf(writer), void) = .{ .context = {} };
|
|
try renderer.render(doc, writer);
|
|
}
|
|
|
|
pub fn ExtraData(comptime T: type) type {
|
|
return struct { data: T, end: usize };
|
|
}
|
|
|
|
pub fn extraData(doc: Document, comptime T: type, index: ExtraIndex) ExtraData(T) {
|
|
const fields = @typeInfo(T).Struct.fields;
|
|
var i: usize = @intFromEnum(index);
|
|
var result: T = undefined;
|
|
inline for (fields) |field| {
|
|
@field(result, field.name) = switch (field.type) {
|
|
u32 => doc.extra[i],
|
|
else => @compileError("bad field type"),
|
|
};
|
|
i += 1;
|
|
}
|
|
return .{ .data = result, .end = i };
|
|
}
|
|
|
|
pub fn extraChildren(doc: Document, index: ExtraIndex) []const Node.Index {
|
|
const children = doc.extraData(Node.Children, index);
|
|
return @ptrCast(doc.extra[children.end..][0..children.data.len]);
|
|
}
|
|
|
|
pub fn string(doc: Document, index: StringIndex) [:0]const u8 {
|
|
const start = @intFromEnum(index);
|
|
return std.mem.span(@as([*:0]u8, @ptrCast(doc.string_bytes[start..].ptr)));
|
|
}
|