Skip to content

Commit 02d9e3c

Browse files
committed
spirv: snake-case the spec
1 parent e837765 commit 02d9e3c

File tree

12 files changed

+13413
-11568
lines changed

12 files changed

+13413
-11568
lines changed

src/codegen/spirv.zig

Lines changed: 427 additions & 471 deletions
Large diffs are not rendered by default.

src/codegen/spirv/Assembler.zig

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const assert = std.debug.assert;
77
const spec = @import("spec.zig");
88
const Opcode = spec.Opcode;
99
const Word = spec.Word;
10-
const IdRef = spec.IdRef;
11-
const IdResult = spec.IdResult;
10+
const Id = spec.Id;
1211
const StorageClass = spec.StorageClass;
1312

1413
const SpvModule = @import("Module.zig");
@@ -127,10 +126,10 @@ const AsmValue = union(enum) {
127126
unresolved_forward_reference,
128127

129128
/// This result-value is a normal result produced by a different instruction.
130-
value: IdRef,
129+
value: Id,
131130

132131
/// This result-value represents a type registered into the module's type system.
133-
ty: IdRef,
132+
ty: Id,
134133

135134
/// This is a pre-supplied constant integer value.
136135
constant: u32,
@@ -141,7 +140,7 @@ const AsmValue = union(enum) {
141140
/// Retrieve the result-id of this AsmValue. Asserts that this AsmValue
142141
/// is of a variant that allows the result to be obtained (not an unresolved
143142
/// forward declaration, not in the process of being declared, etc).
144-
pub fn resultId(self: AsmValue) IdRef {
143+
pub fn resultId(self: AsmValue) Id {
145144
return switch (self) {
146145
.just_declared,
147146
.unresolved_forward_reference,
@@ -275,28 +274,28 @@ fn todo(self: *Assembler, comptime fmt: []const u8, args: anytype) Error {
275274
/// error message has already been emitted into `self.errors`.
276275
fn processInstruction(self: *Assembler) !void {
277276
const result: AsmValue = switch (self.inst.opcode) {
278-
.OpEntryPoint => {
277+
.entry_point => {
279278
return self.fail(0, "cannot export entry points via OpEntryPoint, export the kernel using callconv(.kernel)", .{});
280279
},
281-
.OpCapability => {
280+
.capability => {
282281
try self.spv.addCapability(@enumFromInt(self.inst.operands.items[0].value));
283282
return;
284283
},
285-
.OpExtension => {
284+
.extension => {
286285
const ext_name_offset = self.inst.operands.items[0].string;
287286
const ext_name = std.mem.sliceTo(self.inst.string_bytes.items[ext_name_offset..], 0);
288287
try self.spv.addExtension(ext_name);
289288
return;
290289
},
291-
.OpExtInstImport => blk: {
290+
.ext_inst_import => blk: {
292291
const set_name_offset = self.inst.operands.items[1].string;
293292
const set_name = std.mem.sliceTo(self.inst.string_bytes.items[set_name_offset..], 0);
294293
const set_tag = std.meta.stringToEnum(spec.InstructionSet, set_name) orelse {
295294
return self.fail(set_name_offset, "unknown instruction set: {s}", .{set_name});
296295
};
297296
break :blk .{ .value = try self.spv.importInstructionSet(set_tag) };
298297
},
299-
.OpExecutionMode, .OpExecutionModeId => {
298+
.execution_mode, .execution_mode_id => {
300299
assert(try self.processGenericInstruction() == null);
301300
const entry_point_id = try self.resolveRefId(self.inst.operands.items[0].ref_id);
302301
const exec_mode: spec.ExecutionMode = @enumFromInt(self.inst.operands.items[1].value);
@@ -314,7 +313,7 @@ fn processInstruction(self: *Assembler) !void {
314313
return;
315314
},
316315
else => switch (self.inst.opcode.class()) {
317-
.TypeDeclaration => try self.processTypeInstruction(),
316+
.type_declaration => try self.processTypeInstruction(),
318317
else => (try self.processGenericInstruction()) orelse return,
319318
},
320319
};
@@ -336,9 +335,9 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
336335
const operands = self.inst.operands.items;
337336
const section = &self.spv.sections.types_globals_constants;
338337
const id = switch (self.inst.opcode) {
339-
.OpTypeVoid => try self.spv.voidType(),
340-
.OpTypeBool => try self.spv.boolType(),
341-
.OpTypeInt => blk: {
338+
.type_void => try self.spv.voidType(),
339+
.type_bool => try self.spv.boolType(),
340+
.type_int => blk: {
342341
const signedness: std.builtin.Signedness = switch (operands[2].literal32) {
343342
0 => .unsigned,
344343
1 => .signed,
@@ -352,7 +351,7 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
352351
};
353352
break :blk try self.spv.intType(signedness, width);
354353
},
355-
.OpTypeFloat => blk: {
354+
.type_float => blk: {
356355
const bits = operands[1].literal32;
357356
switch (bits) {
358357
16, 32, 64 => {},
@@ -362,47 +361,47 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
362361
}
363362
break :blk try self.spv.floatType(@intCast(bits));
364363
},
365-
.OpTypeVector => blk: {
364+
.type_vector => blk: {
366365
const child_type = try self.resolveRefId(operands[1].ref_id);
367366
break :blk try self.spv.vectorType(operands[2].literal32, child_type);
368367
},
369-
.OpTypeArray => {
368+
.type_array => {
370369
// TODO: The length of an OpTypeArray is determined by a constant (which may be a spec constant),
371370
// and so some consideration must be taken when entering this in the type system.
372371
return self.todo("process OpTypeArray", .{});
373372
},
374-
.OpTypeRuntimeArray => blk: {
373+
.type_runtime_array => blk: {
375374
const element_type = try self.resolveRefId(operands[1].ref_id);
376375
const result_id = self.spv.allocId();
377-
try section.emit(self.spv.gpa, .OpTypeRuntimeArray, .{
376+
try section.emit(self.spv.gpa, .type_runtime_array, .{
378377
.id_result = result_id,
379378
.element_type = element_type,
380379
});
381380
break :blk result_id;
382381
},
383-
.OpTypePointer => blk: {
382+
.type_pointer => blk: {
384383
const storage_class: StorageClass = @enumFromInt(operands[1].value);
385384
const child_type = try self.resolveRefId(operands[2].ref_id);
386385
const result_id = self.spv.allocId();
387-
try section.emit(self.spv.gpa, .OpTypePointer, .{
386+
try section.emit(self.spv.gpa, .type_pointer, .{
388387
.id_result = result_id,
389388
.storage_class = storage_class,
390389
.type = child_type,
391390
});
392391
break :blk result_id;
393392
},
394-
.OpTypeStruct => blk: {
395-
const ids = try self.gpa.alloc(IdRef, operands[1..].len);
393+
.type_struct => blk: {
394+
const ids = try self.gpa.alloc(Id, operands[1..].len);
396395
defer self.gpa.free(ids);
397396
for (operands[1..], ids) |op, *id| id.* = try self.resolveRefId(op.ref_id);
398397
const result_id = self.spv.allocId();
399398
try self.spv.structType(result_id, ids, null);
400399
break :blk result_id;
401400
},
402-
.OpTypeImage => blk: {
401+
.type_image => blk: {
403402
const sampled_type = try self.resolveRefId(operands[1].ref_id);
404403
const result_id = self.spv.allocId();
405-
try section.emit(self.gpa, .OpTypeImage, .{
404+
try section.emit(self.gpa, .type_image, .{
406405
.id_result = result_id,
407406
.sampled_type = sampled_type,
408407
.dim = @enumFromInt(operands[2].value),
@@ -414,28 +413,28 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
414413
});
415414
break :blk result_id;
416415
},
417-
.OpTypeSampler => blk: {
416+
.type_sampler => blk: {
418417
const result_id = self.spv.allocId();
419-
try section.emit(self.gpa, .OpTypeSampler, .{ .id_result = result_id });
418+
try section.emit(self.gpa, .type_sampler, .{ .id_result = result_id });
420419
break :blk result_id;
421420
},
422-
.OpTypeSampledImage => blk: {
421+
.type_sampled_image => blk: {
423422
const image_type = try self.resolveRefId(operands[1].ref_id);
424423
const result_id = self.spv.allocId();
425-
try section.emit(self.gpa, .OpTypeSampledImage, .{ .id_result = result_id, .image_type = image_type });
424+
try section.emit(self.gpa, .type_sampled_image, .{ .id_result = result_id, .image_type = image_type });
426425
break :blk result_id;
427426
},
428-
.OpTypeFunction => blk: {
427+
.type_function => blk: {
429428
const param_operands = operands[2..];
430429
const return_type = try self.resolveRefId(operands[1].ref_id);
431430

432-
const param_types = try self.spv.gpa.alloc(IdRef, param_operands.len);
431+
const param_types = try self.spv.gpa.alloc(Id, param_operands.len);
433432
defer self.spv.gpa.free(param_types);
434433
for (param_types, param_operands) |*param, operand| {
435434
param.* = try self.resolveRefId(operand.ref_id);
436435
}
437436
const result_id = self.spv.allocId();
438-
try section.emit(self.spv.gpa, .OpTypeFunction, .{
437+
try section.emit(self.spv.gpa, .type_function, .{
439438
.id_result = result_id,
440439
.return_type = return_type,
441440
.id_ref_2 = param_types,
@@ -457,17 +456,17 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
457456
const operands = self.inst.operands.items;
458457
var maybe_spv_decl_index: ?SpvModule.Decl.Index = null;
459458
const section = switch (self.inst.opcode.class()) {
460-
.ConstantCreation => &self.spv.sections.types_globals_constants,
461-
.Annotation => &self.spv.sections.annotations,
462-
.TypeDeclaration => unreachable, // Handled elsewhere.
459+
.constant_creation => &self.spv.sections.types_globals_constants,
460+
.annotation => &self.spv.sections.annotations,
461+
.type_declaration => unreachable, // Handled elsewhere.
463462
else => switch (self.inst.opcode) {
464-
.OpEntryPoint => unreachable,
465-
.OpExecutionMode, .OpExecutionModeId => &self.spv.sections.execution_modes,
466-
.OpVariable => section: {
463+
.entry_point => unreachable,
464+
.execution_mode, .execution_mode_id => &self.spv.sections.execution_modes,
465+
.variable => section: {
467466
const storage_class: spec.StorageClass = @enumFromInt(operands[2].value);
468-
if (storage_class == .Function) break :section &self.func.prologue;
467+
if (storage_class == .function) break :section &self.func.prologue;
469468
maybe_spv_decl_index = try self.spv.allocDecl(.global);
470-
if (self.spv.version.minor < 4 and storage_class != .Input and storage_class != .Output) {
469+
if (self.spv.version.minor < 4 and storage_class != .input and storage_class != .output) {
471470
// Before version 1.4, the interface’s storage classes are limited to the Input and Output
472471
break :section &self.spv.sections.types_globals_constants;
473472
}
@@ -481,7 +480,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
481480
},
482481
};
483482

484-
var maybe_result_id: ?IdResult = null;
483+
var maybe_result_id: ?Id = null;
485484
const first_word = section.instructions.items.len;
486485
// At this point we're not quite sure how many operands this instruction is going to have,
487486
// so insert 0 and patch up the actual opcode word later.
@@ -504,12 +503,12 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
504503
else
505504
self.spv.allocId();
506505
try section.ensureUnusedCapacity(self.spv.gpa, 1);
507-
section.writeOperand(IdResult, maybe_result_id.?);
506+
section.writeOperand(Id, maybe_result_id.?);
508507
},
509508
.ref_id => |index| {
510509
const result = try self.resolveRef(index);
511510
try section.ensureUnusedCapacity(self.spv.gpa, 1);
512-
section.writeOperand(spec.IdRef, result.resultId());
511+
section.writeOperand(spec.Id, result.resultId());
513512
},
514513
.string => |offset| {
515514
const text = std.mem.sliceTo(self.inst.string_bytes.items[offset..], 0);
@@ -558,7 +557,7 @@ fn resolveRef(self: *Assembler, ref: AsmValue.Ref) !AsmValue {
558557
}
559558
}
560559

561-
fn resolveRefId(self: *Assembler, ref: AsmValue.Ref) !IdRef {
560+
fn resolveRefId(self: *Assembler, ref: AsmValue.Ref) !Id {
562561
const value = try self.resolveRef(ref);
563562
return value.resultId();
564563
}
@@ -600,7 +599,7 @@ fn parseInstruction(self: *Assembler) !void {
600599
const expected_operands = inst.operands;
601600
// This is a loop because the result-id is not always the first operand.
602601
const requires_lhs_result = for (expected_operands) |op| {
603-
if (op.kind == .IdResult) break true;
602+
if (op.kind == .id_result) break true;
604603
} else false;
605604

606605
if (requires_lhs_result and maybe_lhs_result == null) {
@@ -614,7 +613,7 @@ fn parseInstruction(self: *Assembler) !void {
614613
}
615614

616615
for (expected_operands) |operand| {
617-
if (operand.kind == .IdResult) {
616+
if (operand.kind == .id_result) {
618617
try self.inst.operands.append(self.gpa, .{ .result_id = maybe_lhs_result.? });
619618
continue;
620619
}
@@ -646,11 +645,11 @@ fn parseOperand(self: *Assembler, kind: spec.OperandKind) Error!void {
646645
.value_enum => try self.parseValueEnum(kind),
647646
.id => try self.parseRefId(),
648647
else => switch (kind) {
649-
.LiteralInteger => try self.parseLiteralInteger(),
650-
.LiteralString => try self.parseString(),
651-
.LiteralContextDependentNumber => try self.parseContextDependentNumber(),
652-
.LiteralExtInstInteger => try self.parseLiteralExtInstInteger(),
653-
.PairIdRefIdRef => try self.parsePhiSource(),
648+
.literal_integer => try self.parseLiteralInteger(),
649+
.literal_string => try self.parseString(),
650+
.literal_context_dependent_number => try self.parseContextDependentNumber(),
651+
.literal_ext_inst_integer => try self.parseLiteralExtInstInteger(),
652+
.pair_id_ref_id_ref => try self.parsePhiSource(),
654653
else => return self.todo("parse operand of type {s}", .{@tagName(kind)}),
655654
},
656655
}
@@ -850,7 +849,7 @@ fn parseContextDependentNumber(self: *Assembler) !void {
850849
// Currently, this operand appears in OpConstant and OpSpecConstant, where the too-be-parsed type
851850
// is determined by the result type. That means that in this instructions we have to resolve the
852851
// operand type early and look at the result to see how we need to proceed.
853-
assert(self.inst.opcode == .OpConstant or self.inst.opcode == .OpSpecConstant);
852+
assert(self.inst.opcode == .constant or self.inst.opcode == .spec_constant);
854853

855854
const tok = self.currentToken();
856855
const result = try self.resolveRef(self.inst.operands.items[0].ref_id);

0 commit comments

Comments
 (0)