M0.1: HLSL 2021 parser and syntax tree
- src/front/ast.zig: purely syntactic tree (declarations, statements, expressions, types as written, attributes, semantics/registers). - src/front/parser.zig: recursive descent over the preprocessed tokens, with a scoped type-name table to tell declarations from expressions, casts from parenthesized expressions and template arguments from comparisons. Covers the M0.1 language plus the syntax M0.2 needs (templates incl. partial specialization and variable templates, operator overloading, bitfields, methods, namespaces, enums, typedef/using) and dxc-isms seen in the corpus: payload access qualifiers, typed [[attributes]], legacy annotations / state blocks, `.template` calls, condition declarations, functions returning arrays. - Error recovery at declaration and statement boundaries. - src/front/ast_dump.zig and -zdxc-ast / -zdxc-parse dev flags. - `zig build oracle-parse`: all 1604 corpus files that dxc compiles under -HV 2021 parse without errors; no crashes over all 4764 test files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b8560a5b05
commit
f6b2e9cf36
|
|
@ -47,6 +47,13 @@ pub fn build(b: *std.Build) void {
|
|||
oracle.has_side_effects = true;
|
||||
oracle_step.dependOn(&oracle.step);
|
||||
|
||||
const oracle_parse_step = b.step("oracle-parse", "Parse every dxc-valid corpus file with zdxc");
|
||||
const oracle_parse = b.addSystemCommand(&.{ "sh", "test/oracle/parse.sh" });
|
||||
oracle_parse.addArtifactArg(exe);
|
||||
oracle_parse.setCwd(b.path("."));
|
||||
oracle_parse.has_side_effects = true;
|
||||
oracle_parse_step.dependOn(&oracle_parse.step);
|
||||
|
||||
// Benchmarks always build optimized so numbers are comparable.
|
||||
const bench = b.addExecutable(.{
|
||||
.name = "pp_bench",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,275 @@
|
|||
//! Syntax tree for HLSL 2021. Purely syntactic: names are not resolved and
|
||||
//! types are recorded as written. Nodes live in the parser's arena.
|
||||
|
||||
const std = @import("std");
|
||||
const source = @import("source.zig");
|
||||
pub const Loc = source.Loc;
|
||||
|
||||
/// A possibly qualified name as written: `foo`, `N::foo`, `::foo`.
|
||||
pub const Name = struct {
|
||||
loc: Loc,
|
||||
/// Components without separators; a leading empty component means
|
||||
/// a global-scope `::` prefix.
|
||||
parts: []const []const u8,
|
||||
/// Template arguments per part (`S<T>::value`); empty when no part has
|
||||
/// any, otherwise the same length as `parts`.
|
||||
args: []const []const TemplateArg = &.{},
|
||||
|
||||
pub fn last(n: Name) []const u8 {
|
||||
return n.parts[n.parts.len - 1];
|
||||
}
|
||||
|
||||
pub fn isSimple(n: Name, s: []const u8) bool {
|
||||
return n.parts.len == 1 and std.mem.eql(u8, n.parts[0], s);
|
||||
}
|
||||
};
|
||||
|
||||
pub const TemplateArg = union(enum) {
|
||||
type: *TypeRef,
|
||||
expr: *Expr,
|
||||
};
|
||||
|
||||
/// A type as written: `float4`, `Texture2D<float4>`, `vector<float, 4>`,
|
||||
/// `N::S<T>`. Resolution happens in Sema.
|
||||
pub const TypeRef = struct {
|
||||
loc: Loc,
|
||||
name: Name,
|
||||
args: []const TemplateArg = &.{},
|
||||
/// Array dimensions on type arguments: `StructuredBuffer<float[3]>`.
|
||||
array_dims: []const ?*Expr = &.{},
|
||||
/// `[[...]]` attributes after the type (`__builtin_LinAlgMatrix [[...]]`).
|
||||
attrs: []const Attribute = &.{},
|
||||
/// `unorm` / `snorm` inside resource element types.
|
||||
norm: enum { none, unorm, snorm } = .none,
|
||||
};
|
||||
|
||||
/// Storage classes, type qualifiers and parameter / interpolation
|
||||
/// modifiers that may precede a declaration.
|
||||
pub const Qualifiers = packed struct(u64) {
|
||||
@"const": bool = false,
|
||||
static: bool = false,
|
||||
uniform: bool = false,
|
||||
@"extern": bool = false,
|
||||
@"volatile": bool = false,
|
||||
precise: bool = false,
|
||||
groupshared: bool = false,
|
||||
shared: bool = false,
|
||||
in: bool = false,
|
||||
out: bool = false,
|
||||
row_major: bool = false,
|
||||
column_major: bool = false,
|
||||
nointerpolation: bool = false,
|
||||
linear: bool = false,
|
||||
centroid: bool = false,
|
||||
sample: bool = false,
|
||||
noperspective: bool = false,
|
||||
globallycoherent: bool = false,
|
||||
reordercoherent: bool = false,
|
||||
@"inline": bool = false,
|
||||
@"export": bool = false,
|
||||
// Geometry shader input primitive types.
|
||||
point: bool = false,
|
||||
line: bool = false,
|
||||
triangle: bool = false,
|
||||
lineadj: bool = false,
|
||||
triangleadj: bool = false,
|
||||
// Mesh shader output / payload modifiers.
|
||||
vertices: bool = false,
|
||||
indices: bool = false,
|
||||
primitives: bool = false,
|
||||
payload: bool = false,
|
||||
_: u34 = 0,
|
||||
};
|
||||
|
||||
/// `[name(args)]` or `[[ns::name(args)]]`.
|
||||
pub const Attribute = struct {
|
||||
loc: Loc,
|
||||
/// e.g. `numthreads`, `vk::binding`.
|
||||
name: []const u8,
|
||||
args: []const *Expr,
|
||||
double_bracket: bool,
|
||||
};
|
||||
|
||||
/// Text after `:` on a declarator.
|
||||
pub const Semantic = struct {
|
||||
loc: Loc,
|
||||
/// `access` is a ray payload qualifier: `read(caller)`, `write(miss)`.
|
||||
kind: enum { semantic, register, packoffset, access },
|
||||
/// Semantic name (`SV_Target0`), or the comma-separated arguments of
|
||||
/// register(...) / packoffset(...) with internal tokens joined
|
||||
/// (`t0`, `space1`, `c0.x`).
|
||||
values: []const []const u8,
|
||||
};
|
||||
|
||||
pub const UnaryOp = enum { plus, minus, not, bit_not, pre_inc, pre_dec, post_inc, post_dec };
|
||||
|
||||
pub const BinaryOp = enum {
|
||||
mul,
|
||||
div,
|
||||
rem,
|
||||
add,
|
||||
sub,
|
||||
shl,
|
||||
shr,
|
||||
lt,
|
||||
gt,
|
||||
le,
|
||||
ge,
|
||||
eq,
|
||||
ne,
|
||||
bit_and,
|
||||
bit_xor,
|
||||
bit_or,
|
||||
log_and,
|
||||
log_or,
|
||||
comma,
|
||||
};
|
||||
|
||||
pub const AssignOp = enum { assign, mul, div, rem, add, sub, shl, shr, bit_and, bit_xor, bit_or };
|
||||
|
||||
pub const Expr = struct {
|
||||
loc: Loc,
|
||||
kind: Kind,
|
||||
|
||||
pub const Kind = union(enum) {
|
||||
int_lit: []const u8,
|
||||
float_lit: []const u8,
|
||||
bool_lit: bool,
|
||||
string_lit: []const u8,
|
||||
name: Name,
|
||||
this,
|
||||
unary: struct { op: UnaryOp, operand: *Expr },
|
||||
binary: struct { op: BinaryOp, lhs: *Expr, rhs: *Expr },
|
||||
assign: struct { op: AssignOp, lhs: *Expr, rhs: *Expr },
|
||||
conditional: struct { cond: *Expr, then: *Expr, @"else": *Expr },
|
||||
/// Function or method call; `template_args` for `f<T>(...)`.
|
||||
call: struct { callee: *Expr, args: []const *Expr, template_args: []const TemplateArg = &.{} },
|
||||
/// Type used as a function: `float4(...)`, `vector<float, 2>(...)`.
|
||||
construct: struct { type: *TypeRef, args: []const *Expr },
|
||||
cast: struct { type: *TypeRef, array_dims: []const ?*Expr = &.{}, operand: *Expr },
|
||||
/// `.name` (field, swizzle or method); `arrow` is accepted for
|
||||
/// error reporting.
|
||||
member: struct { base: *Expr, name: []const u8, name_loc: Loc },
|
||||
index: struct { base: *Expr, index: *Expr },
|
||||
init_list: []const *Expr,
|
||||
sizeof_type: *TypeRef,
|
||||
sizeof_expr: *Expr,
|
||||
};
|
||||
};
|
||||
|
||||
pub const Stmt = struct {
|
||||
loc: Loc,
|
||||
attrs: []const Attribute = &.{},
|
||||
kind: Kind,
|
||||
|
||||
pub const Kind = union(enum) {
|
||||
compound: []const *Stmt,
|
||||
/// Local declarations: variables, typedefs, structs.
|
||||
decl: []const *Decl,
|
||||
expr: *Expr,
|
||||
empty,
|
||||
/// `cond_decl` is set for `if (T x = e)`; `cond` then names `x`.
|
||||
@"if": struct { cond_decl: ?*Decl = null, cond: *Expr, then: *Stmt, @"else": ?*Stmt },
|
||||
@"for": struct { init: ?*Stmt, cond: ?*Expr, inc: ?*Expr, body: *Stmt },
|
||||
@"while": struct { cond_decl: ?*Decl = null, cond: *Expr, body: *Stmt },
|
||||
do_while: struct { body: *Stmt, cond: *Expr },
|
||||
@"switch": struct { cond_decl: ?*Decl = null, cond: *Expr, body: *Stmt },
|
||||
case: struct { value: *Expr, stmt: *Stmt },
|
||||
default: *Stmt,
|
||||
@"break",
|
||||
@"continue",
|
||||
@"return": ?*Expr,
|
||||
discard,
|
||||
};
|
||||
};
|
||||
|
||||
pub const VarDecl = struct {
|
||||
attrs: []const Attribute,
|
||||
/// Non-empty for variable templates.
|
||||
template_params: []const TemplateParam = &.{},
|
||||
/// Qualified out-of-line definitions (`int S::x = 1;`) keep the full
|
||||
/// spelled name, e.g. `S::x`.
|
||||
quals: Qualifiers,
|
||||
type: *TypeRef,
|
||||
name: []const u8,
|
||||
name_loc: Loc,
|
||||
array_dims: []const ?*Expr,
|
||||
semantics: []const Semantic,
|
||||
bitfield: ?*Expr = null,
|
||||
init: ?*Expr = null,
|
||||
};
|
||||
|
||||
pub const TemplateParam = struct {
|
||||
loc: Loc,
|
||||
name: []const u8,
|
||||
/// null for `typename T` / `class T`; the type for value parameters.
|
||||
value_type: ?*TypeRef,
|
||||
default: ?TemplateArg,
|
||||
};
|
||||
|
||||
pub const FuncDecl = struct {
|
||||
attrs: []const Attribute,
|
||||
quals: Qualifiers,
|
||||
template_params: []const TemplateParam,
|
||||
ret: *TypeRef,
|
||||
/// Function name; for operators `operator+`, `operator[]`, `operator()`.
|
||||
name: Name,
|
||||
params: []const VarDecl,
|
||||
ret_semantics: []const Semantic,
|
||||
/// Array dimensions after the parameter list (`T f()[N]`).
|
||||
ret_dims: []const ?*Expr = &.{},
|
||||
/// Trailing `const` on methods.
|
||||
is_const: bool,
|
||||
body: ?*Stmt,
|
||||
};
|
||||
|
||||
pub const StructDecl = struct {
|
||||
attrs: []const Attribute,
|
||||
keyword: enum { @"struct", class, interface },
|
||||
template_params: []const TemplateParam,
|
||||
name: ?[]const u8,
|
||||
/// Arguments of a partial / explicit specialization: `S<true, T>`.
|
||||
specialization_args: []const TemplateArg = &.{},
|
||||
bases: []const *TypeRef,
|
||||
/// Null for a forward declaration.
|
||||
members: ?[]const *Decl,
|
||||
};
|
||||
|
||||
pub const CBufferDecl = struct {
|
||||
tbuffer: bool,
|
||||
name: []const u8,
|
||||
semantics: []const Semantic,
|
||||
members: []const *Decl,
|
||||
};
|
||||
|
||||
pub const EnumDecl = struct {
|
||||
name: ?[]const u8,
|
||||
scoped: bool,
|
||||
underlying: ?*TypeRef,
|
||||
values: []const struct { loc: Loc, name: []const u8, value: ?*Expr },
|
||||
};
|
||||
|
||||
pub const Decl = struct {
|
||||
loc: Loc,
|
||||
kind: Kind,
|
||||
|
||||
pub const Kind = union(enum) {
|
||||
@"var": VarDecl,
|
||||
func: FuncDecl,
|
||||
@"struct": StructDecl,
|
||||
cbuffer: CBufferDecl,
|
||||
typedef: struct { name: []const u8, type: *TypeRef, array_dims: []const ?*Expr, template_params: []const TemplateParam = &.{} },
|
||||
@"enum": EnumDecl,
|
||||
namespace: struct { name: []const u8, decls: []const *Decl },
|
||||
/// `using namespace N;`
|
||||
using_namespace: Name,
|
||||
/// `using N::name;`
|
||||
using_decl: Name,
|
||||
pragma: []const u8,
|
||||
static_assert: struct { cond: *Expr, message: ?[]const u8 },
|
||||
};
|
||||
};
|
||||
|
||||
pub const TranslationUnit = struct {
|
||||
decls: []const *Decl,
|
||||
};
|
||||
|
|
@ -0,0 +1,414 @@
|
|||
//! Compact S-expression dump of the AST, for tests and debugging.
|
||||
|
||||
const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
const W = std.Io.Writer;
|
||||
|
||||
pub fn dumpUnit(w: *W, tu: ast.TranslationUnit) W.Error!void {
|
||||
for (tu.decls) |d| {
|
||||
try decl(w, d, 0);
|
||||
try w.writeByte('\n');
|
||||
}
|
||||
}
|
||||
|
||||
fn indent(w: *W, n: usize) W.Error!void {
|
||||
try w.splatByteAll(' ', n * 2);
|
||||
}
|
||||
|
||||
fn name(w: *W, n: ast.Name) W.Error!void {
|
||||
for (n.parts, 0..) |part, i| {
|
||||
if (i > 0) try w.writeAll("::");
|
||||
try w.writeAll(part);
|
||||
if (n.args.len > 0 and n.args[i].len > 0) try templateArgs(w, n.args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
fn templateArgs(w: *W, args: []const ast.TemplateArg) W.Error!void {
|
||||
try w.writeByte('<');
|
||||
for (args, 0..) |a, i| {
|
||||
if (i > 0) try w.writeAll(", ");
|
||||
switch (a) {
|
||||
.type => |ty| try typeRef(w, ty),
|
||||
.expr => |e| try expr(w, e),
|
||||
}
|
||||
}
|
||||
try w.writeByte('>');
|
||||
}
|
||||
|
||||
pub fn typeRef(w: *W, t: *const ast.TypeRef) W.Error!void {
|
||||
if (t.norm != .none) try w.print("{s} ", .{@tagName(t.norm)});
|
||||
try name(w, t.name);
|
||||
if (t.args.len > 0) try templateArgs(w, t.args);
|
||||
try dims(w, t.array_dims);
|
||||
}
|
||||
|
||||
fn quals(w: *W, q: ast.Qualifiers) W.Error!void {
|
||||
const info = @typeInfo(ast.Qualifiers).@"struct";
|
||||
inline for (info.field_names, info.field_types) |fname, ftype| {
|
||||
if (ftype == bool and @field(q, fname)) try w.print("{s} ", .{fname});
|
||||
}
|
||||
}
|
||||
|
||||
fn attrs(w: *W, list: []const ast.Attribute) W.Error!void {
|
||||
for (list) |a| {
|
||||
try w.print("[{s}", .{a.name});
|
||||
if (a.args.len > 0) {
|
||||
try w.writeByte('(');
|
||||
for (a.args, 0..) |e, i| {
|
||||
if (i > 0) try w.writeAll(", ");
|
||||
try expr(w, e);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
}
|
||||
try w.writeAll("] ");
|
||||
}
|
||||
}
|
||||
|
||||
fn semantics(w: *W, list: []const ast.Semantic) W.Error!void {
|
||||
for (list) |s| {
|
||||
switch (s.kind) {
|
||||
.semantic => try w.print(" : {s}", .{s.values[0]}),
|
||||
.register, .packoffset, .access => {
|
||||
try w.print(" : {s}(", .{@tagName(s.kind)});
|
||||
for (s.values, 0..) |v, i| {
|
||||
if (i > 0) try w.writeAll(", ");
|
||||
try w.writeAll(v);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dims(w: *W, list: []const ?*ast.Expr) W.Error!void {
|
||||
for (list) |d| {
|
||||
try w.writeByte('[');
|
||||
if (d) |e| try expr(w, e);
|
||||
try w.writeByte(']');
|
||||
}
|
||||
}
|
||||
|
||||
fn varDecl(w: *W, v: ast.VarDecl) W.Error!void {
|
||||
try templateParams(w, v.template_params);
|
||||
try attrs(w, v.attrs);
|
||||
try quals(w, v.quals);
|
||||
try typeRef(w, v.type);
|
||||
try w.print(" {s}", .{v.name});
|
||||
try dims(w, v.array_dims);
|
||||
if (v.bitfield) |b| {
|
||||
try w.writeAll(" : ");
|
||||
try expr(w, b);
|
||||
}
|
||||
try semantics(w, v.semantics);
|
||||
if (v.init) |e| {
|
||||
try w.writeAll(" = ");
|
||||
try expr(w, e);
|
||||
}
|
||||
}
|
||||
|
||||
fn templateParams(w: *W, params: []const ast.TemplateParam) W.Error!void {
|
||||
if (params.len == 0) return;
|
||||
try w.writeAll("template<");
|
||||
for (params, 0..) |tp, i| {
|
||||
if (i > 0) try w.writeAll(", ");
|
||||
if (tp.value_type) |vt| {
|
||||
try typeRef(w, vt);
|
||||
try w.print(" {s}", .{tp.name});
|
||||
} else try w.print("typename {s}", .{tp.name});
|
||||
}
|
||||
try w.writeAll("> ");
|
||||
}
|
||||
|
||||
pub fn decl(w: *W, d: *const ast.Decl, depth: usize) W.Error!void {
|
||||
try indent(w, depth);
|
||||
switch (d.kind) {
|
||||
.@"var" => |v| {
|
||||
try w.writeAll("(var ");
|
||||
try varDecl(w, v);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.func => |f| {
|
||||
try w.writeAll("(func ");
|
||||
try templateParams(w, f.template_params);
|
||||
try attrs(w, f.attrs);
|
||||
try quals(w, f.quals);
|
||||
try typeRef(w, f.ret);
|
||||
try w.writeByte(' ');
|
||||
try name(w, f.name);
|
||||
try w.writeByte('(');
|
||||
for (f.params, 0..) |prm, i| {
|
||||
if (i > 0) try w.writeAll(", ");
|
||||
try varDecl(w, prm);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
try dims(w, f.ret_dims);
|
||||
if (f.is_const) try w.writeAll(" const");
|
||||
try semantics(w, f.ret_semantics);
|
||||
if (f.body) |b| {
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, b, depth + 1);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.@"struct" => |s| {
|
||||
try w.print("({s} ", .{@tagName(s.keyword)});
|
||||
try templateParams(w, s.template_params);
|
||||
try attrs(w, s.attrs);
|
||||
try w.writeAll(s.name orelse "<anon>");
|
||||
if (s.specialization_args.len > 0) try templateArgs(w, s.specialization_args);
|
||||
for (s.bases) |b| {
|
||||
try w.writeAll(" : ");
|
||||
try typeRef(w, b);
|
||||
}
|
||||
if (s.members) |members| {
|
||||
for (members) |m| {
|
||||
try w.writeByte('\n');
|
||||
try decl(w, m, depth + 1);
|
||||
}
|
||||
} else try w.writeAll(" <fwd>");
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.cbuffer => |c| {
|
||||
try w.print("({s} {s}", .{ if (c.tbuffer) "tbuffer" else "cbuffer", c.name });
|
||||
try semantics(w, c.semantics);
|
||||
for (c.members) |m| {
|
||||
try w.writeByte('\n');
|
||||
try decl(w, m, depth + 1);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.typedef => |t| {
|
||||
try w.writeAll("(typedef ");
|
||||
try templateParams(w, t.template_params);
|
||||
try typeRef(w, t.type);
|
||||
try w.print(" {s}", .{t.name});
|
||||
try dims(w, t.array_dims);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.@"enum" => |e| {
|
||||
try w.print("(enum{s} {s}", .{ if (e.scoped) " class" else "", e.name orelse "<anon>" });
|
||||
for (e.values) |v| {
|
||||
try w.print(" {s}", .{v.name});
|
||||
if (v.value) |x| {
|
||||
try w.writeByte('=');
|
||||
try expr(w, x);
|
||||
}
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.namespace => |n| {
|
||||
try w.print("(namespace {s}", .{n.name});
|
||||
for (n.decls) |m| {
|
||||
try w.writeByte('\n');
|
||||
try decl(w, m, depth + 1);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.using_namespace => |n| {
|
||||
try w.writeAll("(using namespace ");
|
||||
try name(w, n);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.using_decl => |n| {
|
||||
try w.writeAll("(using ");
|
||||
try name(w, n);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.pragma => |p| try w.print("(pragma {s})", .{p}),
|
||||
.static_assert => |s| {
|
||||
try w.writeAll("(static_assert ");
|
||||
try expr(w, s.cond);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stmt(w: *W, s: *const ast.Stmt, depth: usize) W.Error!void {
|
||||
try indent(w, depth);
|
||||
try attrs(w, s.attrs);
|
||||
switch (s.kind) {
|
||||
.compound => |list| {
|
||||
try w.writeAll("{");
|
||||
for (list) |c| {
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, c, depth + 1);
|
||||
}
|
||||
try w.writeAll("}");
|
||||
},
|
||||
.decl => |list| {
|
||||
for (list, 0..) |d, i| {
|
||||
if (i > 0) try w.writeByte('\n');
|
||||
try decl(w, d, if (i == 0) 0 else depth);
|
||||
}
|
||||
},
|
||||
.expr => |e| try expr(w, e),
|
||||
.empty => try w.writeAll("(empty)"),
|
||||
.@"if" => |x| {
|
||||
try w.writeAll("(if ");
|
||||
if (x.cond_decl) |d| {
|
||||
try decl(w, d, 0);
|
||||
try w.writeByte(' ');
|
||||
}
|
||||
try expr(w, x.cond);
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, x.then, depth + 1);
|
||||
if (x.@"else") |e| {
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, e, depth + 1);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.@"for" => |x| {
|
||||
try w.writeAll("(for ");
|
||||
if (x.init) |i| try stmt(w, i, 0) else try w.writeAll("-");
|
||||
try w.writeAll("; ");
|
||||
if (x.cond) |c| try expr(w, c) else try w.writeAll("-");
|
||||
try w.writeAll("; ");
|
||||
if (x.inc) |c| try expr(w, c) else try w.writeAll("-");
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, x.body, depth + 1);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.@"while" => |x| {
|
||||
try w.writeAll("(while ");
|
||||
try expr(w, x.cond);
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, x.body, depth + 1);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.do_while => |x| {
|
||||
try w.writeAll("(do\n");
|
||||
try stmt(w, x.body, depth + 1);
|
||||
try w.writeAll(" while ");
|
||||
try expr(w, x.cond);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.@"switch" => |x| {
|
||||
try w.writeAll("(switch ");
|
||||
try expr(w, x.cond);
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, x.body, depth + 1);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.case => |x| {
|
||||
try w.writeAll("(case ");
|
||||
try expr(w, x.value);
|
||||
try w.writeByte('\n');
|
||||
try stmt(w, x.stmt, depth + 1);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.default => |x| {
|
||||
try w.writeAll("(default\n");
|
||||
try stmt(w, x, depth + 1);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.@"break" => try w.writeAll("(break)"),
|
||||
.@"continue" => try w.writeAll("(continue)"),
|
||||
.discard => try w.writeAll("(discard)"),
|
||||
.@"return" => |v| {
|
||||
try w.writeAll("(return");
|
||||
if (v) |e| {
|
||||
try w.writeByte(' ');
|
||||
try expr(w, e);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expr(w: *W, e: *const ast.Expr) W.Error!void {
|
||||
switch (e.kind) {
|
||||
.int_lit, .float_lit, .string_lit => |t| try w.writeAll(t),
|
||||
.bool_lit => |b| try w.writeAll(if (b) "true" else "false"),
|
||||
.name => |n| try name(w, n),
|
||||
.this => try w.writeAll("this"),
|
||||
.unary => |u| {
|
||||
try w.print("({s} ", .{@tagName(u.op)});
|
||||
try expr(w, u.operand);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.binary => |b| {
|
||||
try w.print("({s} ", .{@tagName(b.op)});
|
||||
try expr(w, b.lhs);
|
||||
try w.writeByte(' ');
|
||||
try expr(w, b.rhs);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.assign => |a| {
|
||||
try w.print("({s}= ", .{if (a.op == .assign) "" else @tagName(a.op)});
|
||||
try expr(w, a.lhs);
|
||||
try w.writeByte(' ');
|
||||
try expr(w, a.rhs);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.conditional => |c| {
|
||||
try w.writeAll("(? ");
|
||||
try expr(w, c.cond);
|
||||
try w.writeByte(' ');
|
||||
try expr(w, c.then);
|
||||
try w.writeByte(' ');
|
||||
try expr(w, c.@"else");
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.call => |c| {
|
||||
try w.writeAll("(call ");
|
||||
try expr(w, c.callee);
|
||||
if (c.template_args.len > 0) {
|
||||
const tmp: ast.TypeRef = .{ .loc = e.loc, .name = .{ .loc = e.loc, .parts = &.{""} }, .args = c.template_args };
|
||||
try typeRef(w, &tmp);
|
||||
}
|
||||
for (c.args) |a| {
|
||||
try w.writeByte(' ');
|
||||
try expr(w, a);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.construct => |c| {
|
||||
try w.writeAll("(construct ");
|
||||
try typeRef(w, c.type);
|
||||
for (c.args) |a| {
|
||||
try w.writeByte(' ');
|
||||
try expr(w, a);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.cast => |c| {
|
||||
try w.writeAll("(cast ");
|
||||
try typeRef(w, c.type);
|
||||
try dims(w, c.array_dims);
|
||||
try w.writeByte(' ');
|
||||
try expr(w, c.operand);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.member => |m| {
|
||||
try w.writeAll("(. ");
|
||||
try expr(w, m.base);
|
||||
try w.print(" {s})", .{m.name});
|
||||
},
|
||||
.index => |x| {
|
||||
try w.writeAll("([] ");
|
||||
try expr(w, x.base);
|
||||
try w.writeByte(' ');
|
||||
try expr(w, x.index);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.init_list => |list| {
|
||||
try w.writeByte('{');
|
||||
for (list, 0..) |a, i| {
|
||||
if (i > 0) try w.writeAll(", ");
|
||||
try expr(w, a);
|
||||
}
|
||||
try w.writeByte('}');
|
||||
},
|
||||
.sizeof_type => |t| {
|
||||
try w.writeAll("(sizeof ");
|
||||
try typeRef(w, t);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
.sizeof_expr => |x| {
|
||||
try w.writeAll("(sizeof ");
|
||||
try expr(w, x);
|
||||
try w.writeByte(')');
|
||||
},
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
19
src/main.zig
19
src/main.zig
|
|
@ -28,6 +28,8 @@ const usage =
|
|||
\\ -zdxc-check-hash Verify the container hash of <input>
|
||||
\\ -zdxc-lex Print the tokens of <input>, one per line (no preprocessing;
|
||||
\\ `#line` lines are skipped)
|
||||
\\ -zdxc-parse Preprocess and parse <input>, reporting syntax errors only
|
||||
\\ -zdxc-ast Preprocess and parse <input> and dump the syntax tree
|
||||
\\
|
||||
;
|
||||
|
||||
|
|
@ -45,6 +47,8 @@ const Options = struct {
|
|||
empty: bool = false,
|
||||
check_hash: bool = false,
|
||||
lex: bool = false,
|
||||
parse: bool = false,
|
||||
dump_ast: bool = false,
|
||||
};
|
||||
|
||||
const Cli = struct {
|
||||
|
|
@ -119,6 +123,11 @@ fn parseArgs(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, std
|
|||
opts.check_hash = true;
|
||||
} else if (std.mem.eql(u8, a, "-zdxc-lex")) {
|
||||
opts.lex = true;
|
||||
} else if (std.mem.eql(u8, a, "-zdxc-parse")) {
|
||||
opts.parse = true;
|
||||
} else if (std.mem.eql(u8, a, "-zdxc-ast")) {
|
||||
opts.parse = true;
|
||||
opts.dump_ast = true;
|
||||
} else if (std.mem.eql(u8, a, "--version")) {
|
||||
try stdout.print("zdxc {s}\n", .{zdxc.version});
|
||||
return null;
|
||||
|
|
@ -173,7 +182,7 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: *
|
|||
else
|
||||
null;
|
||||
|
||||
if (opts.preprocess) {
|
||||
if (opts.preprocess or opts.parse) {
|
||||
const input = opts.input orelse return cli.fail("no input file", .{});
|
||||
var disk: front.source.DiskFs = .{ .io = cli.io };
|
||||
var sm = front.source.SourceManager.init(arena, disk.fileSystem());
|
||||
|
|
@ -188,7 +197,13 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: *
|
|||
.enable_16bit = opts.enable_16bit,
|
||||
});
|
||||
var out: Io.Writer.Allocating = .init(arena);
|
||||
if (try pp.enterMainFile(input)) try front.pp_print.print(&pp, &out.writer);
|
||||
if (try pp.enterMainFile(input)) {
|
||||
if (opts.parse) {
|
||||
var parser = try front.parser.Parser.fromPreprocessor(arena, &diags, &pp);
|
||||
const tu = try parser.parseTranslationUnit();
|
||||
if (opts.dump_ast) try front.ast_dump.dumpUnit(stdout, tu);
|
||||
} else try front.pp_print.print(&pp, &out.writer);
|
||||
}
|
||||
try diags.render(&sm, cli.stderr);
|
||||
try cli.stderr.flush();
|
||||
if (diags.hasErrors()) return error.Fatal;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ pub const front = struct {
|
|||
pub const scan = @import("front/scan.zig");
|
||||
pub const preproc = @import("front/preproc.zig");
|
||||
pub const pp_print = @import("front/pp_print.zig");
|
||||
pub const ast = @import("front/ast.zig");
|
||||
pub const parser = @import("front/parser.zig");
|
||||
pub const ast_dump = @import("front/ast_dump.zig");
|
||||
};
|
||||
|
||||
pub const dxil = struct {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/sh
|
||||
# Print the file name if dxc compiles it with its RUN-line flags (-HV 2021).
|
||||
flags=$(sh "$(dirname "$0")/run_flags.sh" "$1") || exit 0
|
||||
# shellcheck disable=SC2086
|
||||
"$DXC" -HV 2021 $flags "$1" -Fo /dev/null >/dev/null 2>&1 && echo "$1"
|
||||
exit 0
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#!/bin/sh
|
||||
# Parser oracle: every corpus file that dxc compiles successfully (with the
|
||||
# target and flags from its RUN line, under -HV 2021) must parse without
|
||||
# syntax errors in zdxc. Prints the pass rate and the most common errors.
|
||||
#
|
||||
# Usage: test/oracle/parse.sh <path-to-zdxc>
|
||||
# The list of dxc-valid files is cached in zig-out/oracle/parse-valid.txt;
|
||||
# delete it to rebuild (takes a few minutes).
|
||||
set -u
|
||||
|
||||
ZDXC=$(cd "$(dirname "$1")" && pwd)/$(basename "$1")
|
||||
DXC_ROOT=${DXC_ROOT:-..}
|
||||
DXC=$(cd "${DXC_BUILD:-$DXC_ROOT/build}" && pwd)/bin/dxc
|
||||
CACHE=zig-out/oracle/parse-valid.txt
|
||||
JOBS=${JOBS:-8}
|
||||
mkdir -p zig-out/oracle
|
||||
|
||||
if [ ! -s "$CACHE" ]; then
|
||||
echo "building list of dxc-valid corpus files (cached in $CACHE)..."
|
||||
tmp=$(mktemp)
|
||||
find "$DXC_ROOT/tools/clang/test/CodeGenHLSL" "$DXC_ROOT/tools/clang/test/CodeGenDXIL" "$DXC_ROOT/tools/clang/test/CodeGenSPIRV" -name '*.hlsl' | sort > "$tmp"
|
||||
export DXC
|
||||
xargs -P "$JOBS" -n 1 sh test/oracle/dxc_valid_one.sh < "$tmp" | sort > "$CACHE"
|
||||
rm -f "$tmp"
|
||||
fi
|
||||
|
||||
TMP=$(mktemp -d)
|
||||
trap 'rm -rf "$TMP"' EXIT
|
||||
pass=0
|
||||
fail=0
|
||||
while read -r f; do
|
||||
flags=$(sh test/oracle/run_flags.sh "$f") || continue
|
||||
# shellcheck disable=SC2086
|
||||
if "$ZDXC" -zdxc-parse $flags "$f" > "$TMP/out" 2>&1; then
|
||||
pass=$((pass + 1))
|
||||
else
|
||||
fail=$((fail + 1))
|
||||
echo "$f" >> "$TMP/failed"
|
||||
grep -m1 -E 'error' "$TMP/out" | sed -E 's/^.*(error|fatal): //' >> "$TMP/errors"
|
||||
fi
|
||||
done < "$CACHE"
|
||||
|
||||
if [ "$fail" -gt 0 ]; then
|
||||
echo "most common first errors:"
|
||||
sed -E "s/'[^']*'/'X'/g" "$TMP/errors" | sort | uniq -c | sort -rn | head -${SHOW:-15}
|
||||
echo "first failing files:"
|
||||
head -${SHOW_FILES:-10} "$TMP/failed"
|
||||
fi
|
||||
echo "oracle parse: $pass passed, $fail failed (of $(wc -l < "$CACHE" | tr -d ' ') dxc-valid files)"
|
||||
[ "$fail" -eq 0 ]
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#!/bin/sh
|
||||
# Print the first parse error of every dxc-valid corpus file that zdxc
|
||||
# fails to parse (run test/oracle/parse.sh first to build the file list).
|
||||
# Usage: test/oracle/parse_errors.sh <path-to-zdxc>
|
||||
while read -r f; do
|
||||
flags=$(sh test/oracle/run_flags.sh "$f") || continue
|
||||
# shellcheck disable=SC2086
|
||||
"$1" -zdxc-parse $flags "$f" 2>&1 | grep -m1 -E "error|fatal" | sed -E "s#^[^ ]*: (error|fatal): #$f: #"
|
||||
done < zig-out/oracle/parse-valid.txt
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#!/bin/sh
|
||||
# Print the dxc/zdxc flags taken from the first RUN line of an HLSL test:
|
||||
# -T, -E, -D, -spirv and -enable-16bit-types. Exits 1 if there is no -T.
|
||||
run=$(grep -m1 'RUN:' "$1")
|
||||
target=$(printf '%s' "$run" | grep -oE -- '-T *[a-z]+_6_[0-9]+' | head -1 | sed 's/-T *//')
|
||||
[ -n "$target" ] || exit 1
|
||||
entry=$(printf '%s' "$run" | grep -oE -- '-E *[A-Za-z_][A-Za-z0-9_]*' | head -1 | sed 's/-E */-E /')
|
||||
defs=$(printf '%s' "$run" | grep -oE -- '-D *[A-Za-z_][A-Za-z0-9_]*(=[^ ]*)?' | sed 's/-D */-D/' | tr '\n' ' ')
|
||||
extra=""
|
||||
printf '%s' "$run" | grep -q -- '-spirv' && extra="$extra -spirv"
|
||||
printf '%s' "$run" | grep -q -- '-enable-16bit-types' && extra="$extra -enable-16bit-types"
|
||||
echo "-T $target $entry $extra $defs"
|
||||
Loading…
Reference in New Issue