text
stringlengths
12
867k
//! test for importing publically pub const foo = @import("imported1.zig")
// // Passing integer pointers around is generally not something you're going // to do. Integers are cheap to copy. // // But you know what IS useful? Pointers to structs: // // const Vertex = struct{ x: u32, y: u32, z: u32 }; // // var v1 = Vertex{ .x=3, .y=2, .z=5 }; // // var pv: *Vertex = &v1; // <-- a pointer to our struct // // Note that you don't need to dereference the "pv" pointer to access // the struct's fields: // // YES: pv.x // NO: pv.*.x // // We can write functions that take pointer arguments: // // fn foo(v: *Vertex) void { // v.x += 2; // v.y += 3; // v.z += 7; // } // // And pass references to them: // // foo(&v1); // // // Let's revisit our RPG example and make a printCharacter() function // that takes a Character pointer. // const std = @import("std"); const Class = enum{ wizard, thief, bard, warrior, }; const Character = struct{ class: Class, gold: u32, health: u8 = 100, // <--- You can also fields a default value! experience: u32, }; pub fn main() void { var glorp = Character{ .class = Class.wizard, .gold = 10, .experience = 20, }; // FIX ME! // Please pass our Character "glorp" to printCharacter(): printCharacter( ??? ); } // Note how this function's "c" parameter is a pointer to a Character struct. fn printCharacter(c: *Character) void { // Here's something you haven't seen before: when switching an enum, you // don't have to write the full enum name. Zig understands that ".wizard" // means "Class.wizard" when we switch on a Class enum value: const class_name = switch (c.class) { .wizard => "Wizard", .thief => "Thief", .bard => "Bard", .warrior => "Warrior", }; std.debug.print("{s} (G:{} H:{} XP:{})", .{ class_name, c.gold, c.health, c.experience, }); }
const std = @import("std"); pub const c_flecs = @import("c_flecs.zig"); pub const Entity = c_flecs.ecs_entity_t; pub const System = Entity; pub const Phase = enum(@TagType(c_flecs.EcsSystemKind)) { OnUpdate = @enumToInt(c_flecs.EcsSystemKind.EcsOnUpdate), }; pub const Rows = struct { type_map: std.StringHashMap(u32), pub inner: *c_flecs.ecs_rows_t, fn init(rows: *c_flecs.ecs_rows_t, type_map: std.StringHashMap(u32)) @This() { return @This() { .inner = rows, .type_map = type_map, }; } pub fn getSystem(self: @This()) System { return self.inner.system; } pub fn getDeltaTime(self: @This()) f32 { return self.inner.delta_time; } pub fn getWorldTime(self: @This()) f32 { return self.inner.world_time; } pub fn getFrameOffset(self: @This()) u32 { return self.inner.frame_offset; } pub fn getOffset(self: @This()) u32 { return self.inner.offset; } pub fn count(self: @This()) u32 { return self.inner.count; } pub fn getInterruptedBy(self: @This()) u32 { return self.inner.count; } /// Return the column at the given ix. ty should be the type of entities in /// that column (see the entity signature). /// If the column is shared, there will be only one data item. /// Otherwise, the length should be equal to self.count(). /// Prefer comp() to this, unless the extra performance is needed (one less hash table lookup) pub fn col_ix(self: @This(), comptime T: type, ix: u32) []T { if (c_flecs.ecs_is_shared(self.inner, ix)) { return @ptrCast([*]T, @alignCast(@alignOf(T), c_flecs._ecs_column(self.inner, @sizeOf(T), 1)))[0..1]; } else { return @ptrCast([*]T, @alignCast(@alignOf(T), c_flecs._ecs_column(self.inner, @sizeOf(T), 1)))[0..self.count()]; } } /// Get a component with a type. This is a convenience, function, and uses a /// hash map to lookup the column of the type name. For maximum performance (but /// slightly uglier code), access by the column index with col() pub fn col(self: @This(), comptime T: type) []T { const ix_opt = self.type_map.get(@typeName(T)); if (ix_opt) |ix| { return self.col_ix(T, ix.value); } else { @panic("Can't find component " ++ @typeName(T) ++ " in system."); } } }; const ColModifier = enum { Self, }; const ColDesc = struct { T: type, modifier: ColModifier, }; pub fn SystemSignature(comptime n: i32, comptime cols: [n]ColDesc) type { // Compute signature as a string comptime const string_sig = c"Pos"; return struct { pub fn count(self: @This()) i32 { return n; } pub fn asStr(self: @This()) [*c]const u8 { return string_sig; } pub fn getCols(self: @This()) comptime [n]ColDesc { return cols; } pub fn getColIndex(comptime col: type) i32 { comptime { for (cols) |c, ix| { if (c.T == col) { return ix; } } } @panic("Can't find col of type " ++ @typeName(col)); } fn ThisPlusOneColumn(comptime T: type) type { comptime const desc = ColDesc {.T = T, .modifier = ColModifier.Self}; return SystemSignature(n + 1, cols ++ [1]ColDesc{desc}); } pub fn col(self: @This(), comptime T: type) ThisPlusOneColumn(T) { return ThisPlusOneColumn(T) {}; } }; } pub fn buildSig() SystemSignature(0, [0]ColDesc {}) { return SystemSignature(0, [0]ColDesc {}) {}; } /// Get static memory with the given id. ID must be supplied so zig caches the /// result for the same T and same id. fn getStaticMemoryInit(comptime id: var, comptime val: var) *@typeOf(val) { return &struct { pub var buf: @typeOf(val) = val; }.buf; } /// Get static memory with the given id. ID must be supplied so zig caches the /// result for the same T and same id. fn getStaticMemory(comptime id: var, comptime T: type) *T { return &struct { pub var buf: T = undefined; }.buf; } /// Get a type map, mapping type names to column indexes fn getTypeMapForSystem(comptime system_id: var, comptime sig: var) std.StringHashMap(u32) { return struct { pub fn get() std.StringHashMap(u32) { const TypeMapAndInitialised = struct { init: bool = false, type_map: std.StringHashMap(u32) = undefined, }; // Initialise type map, to map type names to columns. Allocate // enough for the columns we have, assuming each column takes 40 bytes // for an entry in the hash map. const bytes_per_entry = @sizeOf(std.StringHashMap(u32).Entry); var type_map_memory = getStaticMemory(system_id, [bytes_per_entry * sig.getCols().len]u8); var type_map_and_initialised = getStaticMemoryInit(system_id, TypeMapAndInitialised {}); if (!type_map_and_initialised.init) { type_map_and_initialised.init = true; const type_map = &type_map_and_initialised.type_map; var fixed_alloc = std.heap.FixedBufferAllocator.init(type_map_memory[0..]); var allocator = &fixed_alloc.allocator; type_map.* = std.StringHashMap(u32).init(allocator); type_map.ensureCapacityExact(sig.getCols().len) catch @panic("Error, not enough memory allocated"); // Insert types inline for (sig.getCols()[0..]) |col_desc, ix| { _ = type_map.putAssumeCapacity(@typeName(col_desc.T), ix); } } return type_map_and_initialised.type_map; } }.get(); } /// Wrap a zig function in a C compatible ABI fn initSystemDispatcher(comptime function: fn (rows: Rows) void, comptime system_id: var, comptime sig: var) extern fn (rows: [*c]c_flecs.ecs_rows_t) void { return struct { pub extern "c" fn function(rows: [*c]c_flecs.ecs_rows_t) void { function(Rows.init(rows, getTypeMapForSystem(system_id, sig))); } }.function; } fn toCStringLit(comptime string: []const u8) [*c]const u8 { return (string ++ "\x00")[0..].ptr; } extern "c" fn systemDispatcher(rows: [*c]c_flecs.ecs_rows_t) void { // Wrap the rows const wrapped = Rows.init(rows); // Figure out which system this should be, dispatch that var res_fn = system_map.?.get(rows.*.system); if (res_fn) |function| { function.value(wrapped); } else { std.debug.warn("Error: can't find system {}\n", rows.*.system); } } /// Gets a pointer to some static data for the given type. Useful for storing flecs component handles. fn getComponentHandle(comptime T : type) *u64 { return &(struct { pub var handle : u64 = undefined; }.handle); } pub const World = struct { inner: *c_flecs.ecs_world, allocator: *std.mem.Allocator, frame_delta: f32 = 1.0 / 60.0, pub fn init(allocator: *std.mem.Allocator) @This() { return @This() { .inner = c_flecs.ecs_init().?, .allocator = allocator, }; } pub fn deinit(self: @This()) void { _ = c_flecs.ecs_fini(self.inner); } pub fn setTargetFps(self: *@This(), fps: f32) void { self.frame_delta = 1.0 / fps; c_flecs.ecs_set_target_fps(self.inner, fps); } pub fn registerComponent(self: @This(), comptime T: type) void { comptime var c_string_lit : [@typeName(T).len + 1]u8 = undefined; comptime { inline for (@typeName(T)) |c, ix| { c_string_lit[ix] = c; } c_string_lit[c_string_lit.len - 1] = 0; } getComponentHandle(T).* = c_flecs.ecs_new_component(self.inner, &c_string_lit, @sizeOf(T)); } /// Given a component, get the internal flecs component handle. If T hasn't /// been registered, the result of this function is undefined. pub fn getFlecsComponentHandle(comptime T : type) u64 { return getComponentHandle(T).*; } pub fn progress(self: @This()) void { _ = c_flecs.ecs_progress(self.inner, self.frame_delta); } /// Register a system with the given signature: https://github.com/SanderMertens/flecs/blob/master/Manual.md#system-signatures /// Need to provide a name for the system for better debug info. /// @param sig a SystemSignature pub fn registerSystem(self: @This(), comptime name: []const u8, phase: Phase, comptime function: fn(Rows) void, comptime sig: var) void { const sys = c_flecs.ecs_new_system(self.inner, toCStringLit(name), @bitCast(c_flecs.EcsSystemKind, phase), sig.asStr(), initSystemDispatcher(function, name, sig)); } /// Create a new entity with the given components. Add components later on with set(). pub fn new(self: @This(), component_vals: ...) Entity { const e = c_flecs._ecs_new(self.inner, null); comptime var ii : usize = 0; inline while (ii < component_vals.len) { self.set(e, component_vals[ii]); ii += 1; } return e; } /// Create a prefab and return it. Set values in this prefab, then use newInstance(). pub fn newPrefab(self: @This(), component_vals: ...) Entity { const e = self.new(component_vals); c_flecs._ecs_add(self.inner, e, c_flecs.ecs_type_from_entity(self.inner, c_flecs.EEcsPrefab)); return e; } pub fn newInstance(self: @This(), super: Entity) Entity { return c_flecs._ecs_new_instance(self.inner, super, null); } pub fn set(self: @This(), entity: Entity, val: var) void { comptime const T = @typeOf(val); const handle = getComponentHandle(T).*; var val_copied = val; _ = c_flecs._ecs_set_ptr(self.inner, entity, handle, @sizeOf(@typeOf(val)), &val_copied); } };
const std = @import("std"); const Allocator = std.mem.Allocator; const List = std.ArrayList; const Map = std.AutoHashMap; const StrMap = std.StringHashMap; const BitSet = std.DynamicBitSet; const Str = []const u8; const util = @import("util.zig"); const gpa = util.gpa; const data = @embedFile("../data/day09.txt"); pub fn main() !void { } // Useful stdlib functions const tokenize = std.mem.tokenize; const split = std.mem.split; const indexOf = std.mem.indexOfScalar; const indexOfAny = std.mem.indexOfAny; const indexOfStr = std.mem.indexOfPosLinear; const lastIndexOf = std.mem.lastIndexOfScalar; const lastIndexOfAny = std.mem.lastIndexOfAny; const lastIndexOfStr = std.mem.lastIndexOfLinear; const trim = std.mem.trim; const sliceMin = std.mem.min; const sliceMax = std.mem.max; const parseInt = std.fmt.parseInt; const parseFloat = std.fmt.parseFloat; const min = std.math.min; const min3 = std.math.min3; const max = std.math.max; const max3 = std.math.max3; const print = std.debug.print; const assert = std.debug.assert; const sort = std.sort.sort; const asc = std.sort.asc; const desc = std.sort.desc;
pub fn testTrickyDefer() !void { defer canFail() catch {}; defer try canFail(); const a = maybeInt() orelse return; } fn canFail() anyerror!void { } pub fn maybeInt() ?i32 { return 0; } export fn entry() usize { return @sizeOf(@TypeOf(testTrickyDefer)); } // error // backend=stage1 // target=native // // tmp.zig:4:11: error: 'try' not allowed inside defer expression
pub const Channel = @import("event/channel.zig").Channel; pub const Future = @import("event/future.zig").Future; pub const Group = @import("event/group.zig").Group; pub const Batch = @import("event/batch.zig").Batch; pub const Lock = @import("event/lock.zig").Lock; pub const Locked = @import("event/locked.zig").Locked; pub const RwLock = @import("event/rwlock.zig").RwLock; pub const RwLocked = @import("event/rwlocked.zig").RwLocked; pub const Loop = @import("event/loop.zig").Loop; test "import event tests" { _ = @import("event/channel.zig"); _ = @import("event/future.zig"); _ = @import("event/group.zig"); _ = @import("event/batch.zig"); _ = @import("event/lock.zig"); _ = @import("event/locked.zig"); _ = @import("event/rwlock.zig"); _ = @import("event/rwlocked.zig"); _ = @import("event/loop.zig"); }
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2021 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. pub const ArrayHashMap = array_hash_map.ArrayHashMap; pub const ArrayHashMapUnmanaged = array_hash_map.ArrayHashMapUnmanaged; pub const ArrayList = @import("array_list.zig").ArrayList; pub const ArrayListAligned = @import("array_list.zig").ArrayListAligned; pub const ArrayListAlignedUnmanaged = @import("array_list.zig").ArrayListAlignedUnmanaged; pub const ArrayListUnmanaged = @import("array_list.zig").ArrayListUnmanaged; pub const AutoArrayHashMap = array_hash_map.AutoArrayHashMap; pub const AutoArrayHashMapUnmanaged = array_hash_map.AutoArrayHashMapUnmanaged; pub const AutoHashMap = hash_map.AutoHashMap; pub const AutoHashMapUnmanaged = hash_map.AutoHashMapUnmanaged; pub const BufMap = @import("buf_map.zig").BufMap; pub const BufSet = @import("buf_set.zig").BufSet; pub const ChildProcess = @import("child_process.zig").ChildProcess; pub const ComptimeStringMap = @import("comptime_string_map.zig").ComptimeStringMap; pub const DynLib = @import("dynamic_library.zig").DynLib; pub const HashMap = hash_map.HashMap; pub const HashMapUnmanaged = hash_map.HashMapUnmanaged; pub const PackedIntArray = @import("packed_int_array.zig").PackedIntArray; pub const PackedIntArrayEndian = @import("packed_int_array.zig").PackedIntArrayEndian; pub const PackedIntSlice = @import("packed_int_array.zig").PackedIntSlice; pub const PackedIntSliceEndian = @import("packed_int_array.zig").PackedIntSliceEndian; pub const PriorityQueue = @import("priority_queue.zig").PriorityQueue; pub const Progress = @import("Progress.zig"); pub const SemanticVersion = @import("SemanticVersion.zig"); pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList; pub const StringHashMap = hash_map.StringHashMap; pub const StringHashMapUnmanaged = hash_map.StringHashMapUnmanaged; pub const StringArrayHashMap = array_hash_map.StringArrayHashMap; pub const StringArrayHashMapUnmanaged = array_hash_map.StringArrayHashMapUnmanaged; pub const TailQueue = @import("linked_list.zig").TailQueue; pub const Target = @import("target.zig").Target; pub const Thread = @import("Thread.zig"); pub const array_hash_map = @import("array_hash_map.zig"); pub const atomic = @import("atomic.zig"); pub const base64 = @import("base64.zig"); pub const build = @import("build.zig"); pub const builtin = @import("builtin.zig"); pub const c = @import("c.zig"); pub const coff = @import("coff.zig"); pub const compress = @import("compress.zig"); pub const crypto = @import("crypto.zig"); pub const cstr = @import("cstr.zig"); pub const debug = @import("debug.zig"); pub const dwarf = @import("dwarf.zig"); pub const elf = @import("elf.zig"); pub const event = @import("event.zig"); pub const fifo = @import("fifo.zig"); pub const fmt = @import("fmt.zig"); pub const fs = @import("fs.zig"); pub const hash = @import("hash.zig"); pub const hash_map = @import("hash_map.zig"); pub const heap = @import("heap.zig"); pub const io = @import("io.zig"); pub const json = @import("json.zig"); pub const leb = @import("leb128.zig"); pub const log = @import("log.zig"); pub const macho = @import("macho.zig"); pub const math = @import("math.zig"); pub const mem = @import("mem.zig"); pub const meta = @import("meta.zig"); pub const net = @import("net.zig"); pub const os = @import("os.zig"); pub const once = @import("once.zig").once; pub const packed_int_array = @import("packed_int_array.zig"); pub const pdb = @import("pdb.zig"); pub const process = @import("process.zig"); pub const rand = @import("rand.zig"); pub const sort = @import("sort.zig"); pub const ascii = @import("ascii.zig"); pub const testing = @import("testing.zig"); pub const time = @import("time.zig"); pub const unicode = @import("unicode.zig"); pub const valgrind = @import("valgrind.zig"); pub const zig = @import("zig.zig"); pub const start = @import("start.zig"); // This forces the start.zig file to be imported, and the comptime logic inside that // file decides whether to export any appropriate start symbols. comptime { _ = start; } test "" { if (builtin.os.tag == .windows) { // We only test the Windows-relevant stuff to save memory because the CI // server is hitting OOM. TODO revert this after stage2 arrives. _ = ChildProcess; _ = DynLib; _ = Progress; _ = Target; _ = Thread; _ = atomic; _ = build; _ = builtin; _ = debug; _ = event; _ = fs; _ = heap; _ = io; _ = log; _ = macho; _ = net; _ = os; _ = once; _ = pdb; _ = process; _ = testing; _ = time; _ = unicode; _ = zig; _ = start; } else { testing.refAllDecls(@This()); } }
const sabaton = @import("root").sabaton; comptime { asm ( // We pack the CPU boot trampolines into the stack slots, because reasons. \\ .section .data \\ .balign 8 \\ .global smp_tag \\smp_tag: \\ .8byte 0x34d1d96339647025 // smp identifier \\ .8byte 0 // next \\ .8byte 0 // flags \\ .4byte 0 // boot cpu id \\ .4byte 0 // pad \\ .8byte 4 // cpu_count \\ \\ // CPU 0 \\ .4byte 0 // ACPI UID \\ .4byte 0 // CPU ID \\ .8byte 0 // target stack \\ .8byte 0 // goto address \\ .8byte 0 // extra argument \\ \\ // CPU 1 \\ .4byte 1 // ACPI UID \\ .4byte 1 // CPU ID \\ .8byte __boot_stack + 0x1000 // target stack \\ .8byte 0 // goto address \\ .8byte 0 // extra argument \\ \\ // CPU 2 \\ .4byte 2 // ACPI UID \\ .4byte 2 // CPU ID \\ .8byte __boot_stack + 0x2000 // target stack \\ .8byte 0 // goto address \\ .8byte 0 // extra argument \\ \\ // CPU 3 \\ .4byte 3 // ACPI UID \\ .4byte 3 // CPU ID \\ .8byte __boot_stack + 0x3000 // target stack \\ .8byte 0 // goto address \\ .8byte 0 // extra argument \\ \\ .section .text.smp_stub \\ .global smp_stub \\smp_stub: \\ BL el2_to_el1 \\ MSR SPSel, #0 \\ LDR X1, [X0, #8] // Load stack \\ MOV SP, X1 \\ // Fall through to smp_entry ); } export fn smp_entry(context: u64) linksection(".text.smp_entry") noreturn { @call(.{ .modifier = .always_inline }, sabaton.stivale2_smp_ready, .{context}); } fn cpucfg(offset: u16) *volatile u32 { return @intToPtr(*volatile u32, @as(usize, 0x0170_0C00) + offset); } fn ccu(offset: u64) *volatile u32 { return @intToPtr(*volatile u32, @as(usize, 0x01C2_0000) + offset); } pub fn init() void { const smp_tag = sabaton.near("smp_tag").addr(sabaton.Stivale2tag); sabaton.add_tag(&smp_tag[0]); var core: u64 = 1; while (core < 4) : (core += 1) { const ap_x0 = @ptrToInt(smp_tag) + 40 + core * 32; _ = sabaton.psci.wake_cpu(@ptrToInt(sabaton.near("smp_stub").addr(u32)), core, ap_x0, .SMC); } }
const std = @import("std"); const upaya = @import("upaya.zig"); usingnamespace upaya.sokol; pub const RenderTexture = extern struct { img: sg_image = undefined, width: i32 = 0, height: i32 = 0, pub const Filter = enum { linear, nearest }; pub fn init(width: i32, height: i32, filter: Filter) RenderTexture { var img_desc = std.mem.zeroes(sg_image_desc); img_desc.render_target = true; img_desc.width = width; img_desc.height = height; img_desc.pixel_format = .SG_PIXELFORMAT_RGBA8; img_desc.min_filter = if (filter == .linear) .SG_FILTER_LINEAR else .SG_FILTER_NEAREST; img_desc.mag_filter = if (filter == .linear) .SG_FILTER_LINEAR else .SG_FILTER_NEAREST; return .{ .width = width, .height = height, .img = sg_make_image(&img_desc) }; } pub fn deinit(self: RenderTexture) void { sg_destroy_image(self.img); } pub fn imTextureID(self: RenderTexture) upaya.imgui.ImTextureID { return @intToPtr(*c_void, self.img.id); } };
const MenuEffect = enum {}; fn func(effect: MenuEffect) void { _ = effect; } export fn entry() void { func(MenuEffect.ThisDoesNotExist); } // using invalid types in function call raises an error // // tmp.zig:1:20: error: enum declarations must have at least one tag
//! Analyzed Intermediate Representation. //! This data is produced by Sema and consumed by codegen. //! Unlike ZIR where there is one instance for an entire source file, each function //! gets its own `Air` instance. const std = @import("std"); const builtin = @import("builtin"); const Value = @import("value.zig").Value; const Type = @import("type.zig").Type; const Module = @import("Module.zig"); const assert = std.debug.assert; const Air = @This(); instructions: std.MultiArrayList(Inst).Slice, /// The meaning of this data is determined by `Inst.Tag` value. /// The first few indexes are reserved. See `ExtraIndex` for the values. extra: []const u32, values: []const Value, pub const ExtraIndex = enum(u32) { /// Payload index of the main `Block` in the `extra` array. main_block, _, }; pub const Inst = struct { tag: Tag, data: Data, pub const Tag = enum(u8) { /// The first N instructions in the main block must be one arg instruction per /// function parameter. This makes function parameters participate in /// liveness analysis without any special handling. /// Uses the `ty` field. arg, /// Float or integer addition. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. add, /// Integer addition. Wrapping is defined to be twos complement wrapping. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. addwrap, /// Saturating integer addition. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. add_sat, /// Float or integer subtraction. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. sub, /// Integer subtraction. Wrapping is defined to be twos complement wrapping. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. subwrap, /// Saturating integer subtraction. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. sub_sat, /// Float or integer multiplication. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mul, /// Integer multiplication. Wrapping is defined to be twos complement wrapping. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mulwrap, /// Saturating integer multiplication. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mul_sat, /// Float division. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_float, /// Truncating integer or float division. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_trunc, /// Flooring integer or float division. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_floor, /// Integer or float division. Guaranteed no remainder. /// For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_exact, /// Integer or float remainder division. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. rem, /// Integer or float modulus division. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mod, /// Add an offset to a pointer, returning a new pointer. /// The offset is in element type units, not bytes. /// Wrapping is undefined behavior. /// The lhs is the pointer, rhs is the offset. Result type is the same as lhs. /// Uses the `bin_op` field. ptr_add, /// Subtract an offset from a pointer, returning a new pointer. /// The offset is in element type units, not bytes. /// Wrapping is undefined behavior. /// The lhs is the pointer, rhs is the offset. Result type is the same as lhs. /// Uses the `bin_op` field. ptr_sub, /// Given two operands which can be floats, integers, or vectors, returns the /// greater of the operands. For vectors it operates element-wise. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. max, /// Given two operands which can be floats, integers, or vectors, returns the /// lesser of the operands. For vectors it operates element-wise. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. min, /// Integer addition with overflow. Both operands are guaranteed to be the same type, /// and the result is bool. The wrapped value is written to the pointer given by the in /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types /// of the operation. /// Uses the `pl_op` field with payload `Bin`. add_with_overflow, /// Integer subtraction with overflow. Both operands are guaranteed to be the same type, /// and the result is bool. The wrapped value is written to the pointer given by the in /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types /// of the operation. /// Uses the `pl_op` field with payload `Bin`. sub_with_overflow, /// Integer multiplication with overflow. Both operands are guaranteed to be the same type, /// and the result is bool. The wrapped value is written to the pointer given by the in /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types /// of the operation. /// Uses the `pl_op` field with payload `Bin`. mul_with_overflow, /// Integer left-shift with overflow. Both operands are guaranteed to be the same type, /// and the result is bool. The wrapped value is written to the pointer given by the in /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types /// of the operation. /// Uses the `pl_op` field with payload `Bin`. shl_with_overflow, /// Allocates stack local memory. /// Uses the `ty` field. alloc, /// If the function will pass the result by-ref, this instruction returns the /// result pointer. Otherwise it is equivalent to `alloc`. /// Uses the `ty` field. ret_ptr, /// Inline assembly. Uses the `ty_pl` field. Payload is `Asm`. assembly, /// Bitwise AND. `&`. /// Result type is the same as both operands. /// Uses the `bin_op` field. bit_and, /// Bitwise OR. `|`. /// Result type is the same as both operands. /// Uses the `bin_op` field. bit_or, /// Shift right. `>>` /// Uses the `bin_op` field. shr, /// Shift right. The shift produces a poison value if it shifts out any non-zero bits. /// Uses the `bin_op` field. shr_exact, /// Shift left. `<<` /// Uses the `bin_op` field. shl, /// Shift left; For unsigned integers, the shift produces a poison value if it shifts /// out any non-zero bits. For signed integers, the shift produces a poison value if /// it shifts out any bits that disagree with the resultant sign bit. /// Uses the `bin_op` field. shl_exact, /// Saturating integer shift left. `<<|` /// Uses the `bin_op` field. shl_sat, /// Bitwise XOR. `^` /// Uses the `bin_op` field. xor, /// Boolean or binary NOT. /// Uses the `ty_op` field. not, /// Reinterpret the memory representation of a value as a different type. /// Uses the `ty_op` field. bitcast, /// Uses the `ty_pl` field with payload `Block`. block, /// A labeled block of code that loops forever. At the end of the body it is implied /// to repeat; no explicit "repeat" instruction terminates loop bodies. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `ty_pl` field. Payload is `Block`. loop, /// Return from a block with a result. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `br` field. br, /// Lowers to a hardware trap instruction, or the next best thing. /// Result type is always void. breakpoint, /// Yields the return address of the current function. /// Uses the `no_op` field. ret_addr, /// Function call. /// Result type is the return type of the function being called. /// Uses the `pl_op` field with the `Call` payload. operand is the callee. /// Triggers `resolveTypeLayout` on the return type of the callee. call, /// Count leading zeroes of an integer according to its representation in twos complement. /// Result type will always be an unsigned integer big enough to fit the answer. /// Uses the `ty_op` field. clz, /// Count trailing zeroes of an integer according to its representation in twos complement. /// Result type will always be an unsigned integer big enough to fit the answer. /// Uses the `ty_op` field. ctz, /// Count number of 1 bits in an integer according to its representation in twos complement. /// Result type will always be an unsigned integer big enough to fit the answer. /// Uses the `ty_op` field. popcount, /// Reverse the bytes in an integer according to its representation in twos complement. /// Uses the `ty_op` field. byte_swap, /// Reverse the bits in an integer according to its representation in twos complement. /// Uses the `ty_op` field. bit_reverse, /// Square root of a floating point number. /// Uses the `un_op` field. sqrt, /// Sine a floating point number. /// Uses the `un_op` field. sin, /// Cosine a floating point number. /// Uses the `un_op` field. cos, /// Base e exponential of a floating point number. /// Uses the `un_op` field. exp, /// Base 2 exponential of a floating point number. /// Uses the `un_op` field. exp2, /// Natural (base e) logarithm of a floating point number. /// Uses the `un_op` field. log, /// Base 2 logarithm of a floating point number. /// Uses the `un_op` field. log2, /// Base 10 logarithm of a floating point number. /// Uses the `un_op` field. log10, /// Aboslute value of a floating point number. /// Uses the `un_op` field. fabs, /// Floor: rounds a floating pointer number down to the nearest integer. /// Uses the `un_op` field. floor, /// Ceiling: rounds a floating pointer number up to the nearest integer. /// Uses the `un_op` field. ceil, /// Rounds a floating pointer number to the nearest integer. /// Uses the `un_op` field. round, /// Rounds a floating pointer number to the nearest integer towards zero. /// Uses the `un_op` field. trunc_float, /// `<`. Result type is always bool. /// Uses the `bin_op` field. cmp_lt, /// `<=`. Result type is always bool. /// Uses the `bin_op` field. cmp_lte, /// `==`. Result type is always bool. /// Uses the `bin_op` field. cmp_eq, /// `>=`. Result type is always bool. /// Uses the `bin_op` field. cmp_gte, /// `>`. Result type is always bool. /// Uses the `bin_op` field. cmp_gt, /// `!=`. Result type is always bool. /// Uses the `bin_op` field. cmp_neq, /// Conditional branch. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `pl_op` field. Operand is the condition. Payload is `CondBr`. cond_br, /// Switch branch. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `pl_op` field. Operand is the condition. Payload is `SwitchBr`. switch_br, /// A comptime-known value. Uses the `ty_pl` field, payload is index of /// `values` array. constant, /// A comptime-known type. Uses the `ty` field. const_ty, /// Notes the beginning of a source code statement and marks the line and column. /// Result type is always void. /// Uses the `dbg_stmt` field. dbg_stmt, /// ?T => bool /// Result type is always bool. /// Uses the `un_op` field. is_null, /// ?T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_null, /// *?T => bool /// Result type is always bool. /// Uses the `un_op` field. is_null_ptr, /// *?T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_null_ptr, /// E!T => bool /// Result type is always bool. /// Uses the `un_op` field. is_err, /// E!T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_err, /// *E!T => bool /// Result type is always bool. /// Uses the `un_op` field. is_err_ptr, /// *E!T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_err_ptr, /// Result type is always bool. /// Uses the `bin_op` field. bool_and, /// Result type is always bool. /// Uses the `bin_op` field. bool_or, /// Read a value from a pointer. /// Uses the `ty_op` field. load, /// Converts a pointer to its address. Result type is always `usize`. /// Uses the `un_op` field. ptrtoint, /// Given a boolean, returns 0 or 1. /// Result type is always `u1`. /// Uses the `un_op` field. bool_to_int, /// Return a value from a function. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `un_op` field. /// Triggers `resolveTypeLayout` on the return type. ret, /// This instruction communicates that the function's result value is pointed to by /// the operand. If the function will pass the result by-ref, the operand is a /// `ret_ptr` instruction. Otherwise, this instruction is equivalent to a `load` /// on the operand, followed by a `ret` on the loaded value. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `un_op` field. /// Triggers `resolveTypeLayout` on the return type. ret_load, /// Write a value to a pointer. LHS is pointer, RHS is value. /// Result type is always void. /// Uses the `bin_op` field. store, /// Indicates the program counter will never get to this instruction. /// Result type is always noreturn; no instructions in a block follow this one. unreach, /// Convert from a float type to a smaller one. /// Uses the `ty_op` field. fptrunc, /// Convert from a float type to a wider one. /// Uses the `ty_op` field. fpext, /// Returns an integer with a different type than the operand. The new type may have /// fewer, the same, or more bits than the operand type. The new type may also /// differ in signedness from the operand type. However, the instruction /// guarantees that the same integer value fits in both types. /// The new type may also be an enum type, in which case the integer cast operates on /// the integer tag type of the enum. /// See `trunc` for integer truncation. /// Uses the `ty_op` field. intcast, /// Truncate higher bits from an integer, resulting in an integer with the same /// sign but an equal or smaller number of bits. /// Uses the `ty_op` field. trunc, /// ?T => T. If the value is null, undefined behavior. /// Uses the `ty_op` field. optional_payload, /// *?T => *T. If the value is null, undefined behavior. /// Uses the `ty_op` field. optional_payload_ptr, /// *?T => *T. Sets the value to non-null with an undefined payload value. /// Uses the `ty_op` field. optional_payload_ptr_set, /// Given a payload value, wraps it in an optional type. /// Uses the `ty_op` field. wrap_optional, /// E!T -> T. If the value is an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_payload, /// E!T -> E. If the value is not an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_err, /// *(E!T) -> *T. If the value is an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_payload_ptr, /// *(E!T) -> E. If the value is not an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_err_ptr, /// *(E!T) => *T. Sets the value to non-error with an undefined payload value. /// Uses the `ty_op` field. errunion_payload_ptr_set, /// wrap from T to E!T /// Uses the `ty_op` field. wrap_errunion_payload, /// wrap from E to E!T /// Uses the `ty_op` field. wrap_errunion_err, /// Given a pointer to a struct or union and a field index, returns a pointer to the field. /// Uses the `ty_pl` field, payload is `StructField`. /// TODO rename to `agg_field_ptr`. struct_field_ptr, /// Given a pointer to a struct or union, returns a pointer to the field. /// The field index is the number at the end of the name. /// Uses `ty_op` field. /// TODO rename to `agg_field_ptr_index_X` struct_field_ptr_index_0, struct_field_ptr_index_1, struct_field_ptr_index_2, struct_field_ptr_index_3, /// Given a byval struct or union and a field index, returns the field byval. /// Uses the `ty_pl` field, payload is `StructField`. /// TODO rename to `agg_field_val` struct_field_val, /// Given a pointer to a tagged union, set its tag to the provided value. /// Result type is always void. /// Uses the `bin_op` field. LHS is union pointer, RHS is new tag value. set_union_tag, /// Given a tagged union value, get its tag value. /// Uses the `ty_op` field. get_union_tag, /// Constructs a slice from a pointer and a length. /// Uses the `ty_pl` field, payload is `Bin`. lhs is ptr, rhs is len. slice, /// Given a slice value, return the length. /// Result type is always usize. /// Uses the `ty_op` field. slice_len, /// Given a slice value, return the pointer. /// Uses the `ty_op` field. slice_ptr, /// Given a pointer to a slice, return a pointer to the length of the slice. /// Uses the `ty_op` field. ptr_slice_len_ptr, /// Given a pointer to a slice, return a pointer to the pointer of the slice. /// Uses the `ty_op` field. ptr_slice_ptr_ptr, /// Given an (array value or vector value) and element index, /// return the element value at that index. /// Result type is the element type of the array operand. /// Uses the `bin_op` field. array_elem_val, /// Given a slice value, and element index, return the element value at that index. /// Result type is the element type of the slice operand. /// Uses the `bin_op` field. slice_elem_val, /// Given a slice value and element index, return a pointer to the element value at that index. /// Result type is a pointer to the element type of the slice operand. /// Uses the `ty_pl` field with payload `Bin`. slice_elem_ptr, /// Given a pointer value, and element index, return the element value at that index. /// Result type is the element type of the pointer operand. /// Uses the `bin_op` field. ptr_elem_val, /// Given a pointer value, and element index, return the element pointer at that index. /// Result type is pointer to the element type of the pointer operand. /// Uses the `ty_pl` field with payload `Bin`. ptr_elem_ptr, /// Given a pointer to an array, return a slice. /// Uses the `ty_op` field. array_to_slice, /// Given a float operand, return the integer with the closest mathematical meaning. /// Uses the `ty_op` field. float_to_int, /// Given an integer operand, return the float with the closest mathematical meaning. /// Uses the `ty_op` field. int_to_float, /// Given an integer, bool, float, or pointer operand, return a vector with all elements /// equal to the scalar value. /// Uses the `ty_op` field. splat, /// Given dest ptr, value, and len, set all elements at dest to value. /// Result type is always void. /// Uses the `pl_op` field. Operand is the dest ptr. Payload is `Bin`. `lhs` is the /// value, `rhs` is the length. /// The element type may be any type, not just u8. memset, /// Given dest ptr, src ptr, and len, copy len elements from src to dest. /// Result type is always void. /// Uses the `pl_op` field. Operand is the dest ptr. Payload is `Bin`. `lhs` is the /// src ptr, `rhs` is the length. /// The element type may be any type, not just u8. memcpy, /// Uses the `ty_pl` field with payload `Cmpxchg`. cmpxchg_weak, /// Uses the `ty_pl` field with payload `Cmpxchg`. cmpxchg_strong, /// Lowers to a memory fence instruction. /// Result type is always void. /// Uses the `fence` field. fence, /// Atomically load from a pointer. /// Result type is the element type of the pointer. /// Uses the `atomic_load` field. atomic_load, /// Atomically store through a pointer. /// Result type is always `void`. /// Uses the `bin_op` field. LHS is pointer, RHS is element. atomic_store_unordered, /// Same as `atomic_store_unordered` but with `AtomicOrder.Monotonic`. atomic_store_monotonic, /// Same as `atomic_store_unordered` but with `AtomicOrder.Release`. atomic_store_release, /// Same as `atomic_store_unordered` but with `AtomicOrder.SeqCst`. atomic_store_seq_cst, /// Atomically read-modify-write via a pointer. /// Result type is the element type of the pointer. /// Uses the `pl_op` field with payload `AtomicRmw`. Operand is `ptr`. atomic_rmw, /// Given an enum tag value, returns the tag name. The enum type may be non-exhaustive. /// Result type is always `[:0]const u8`. /// Uses the `un_op` field. tag_name, /// Given an error value, return the error name. Result type is always `[:0] const u8`. /// Uses the `un_op` field. error_name, /// Constructs a vector, tuple, or array value out of runtime-known elements. /// Some of the elements may be comptime-known. /// Uses the `ty_pl` field, payload is index of an array of elements, each of which /// is a `Ref`. Length of the array is given by the vector type. /// TODO rename this to `aggregate_init` and make it support array values and /// struct values too. vector_init, /// Communicates an intent to load memory. /// Result is always unused. /// Uses the `prefetch` field. prefetch, /// Implements @fieldParentPtr builtin. /// Uses the `ty_pl` field. field_parent_ptr, pub fn fromCmpOp(op: std.math.CompareOperator) Tag { return switch (op) { .lt => .cmp_lt, .lte => .cmp_lte, .eq => .cmp_eq, .gte => .cmp_gte, .gt => .cmp_gt, .neq => .cmp_neq, }; } pub fn toCmpOp(tag: Tag) ?std.math.CompareOperator { return switch (tag) { .cmp_lt => .lt, .cmp_lte => .lte, .cmp_eq => .eq, .cmp_gte => .gte, .cmp_gt => .gt, .cmp_neq => .neq, else => null, }; } }; /// The position of an AIR instruction within the `Air` instructions array. pub const Index = u32; pub const Ref = @import("Zir.zig").Inst.Ref; /// All instructions have an 8-byte payload, which is contained within /// this union. `Tag` determines which union field is active, as well as /// how to interpret the data within. pub const Data = union { no_op: void, un_op: Ref, bin_op: struct { lhs: Ref, rhs: Ref, }, ty: Type, ty_op: struct { ty: Ref, operand: Ref, }, ty_pl: struct { ty: Ref, // Index into a different array. payload: u32, }, br: struct { block_inst: Index, operand: Ref, }, pl_op: struct { operand: Ref, payload: u32, }, dbg_stmt: struct { line: u32, column: u32, }, fence: std.builtin.AtomicOrder, atomic_load: struct { ptr: Ref, order: std.builtin.AtomicOrder, }, prefetch: struct { ptr: Ref, rw: std.builtin.PrefetchOptions.Rw, locality: u2, cache: std.builtin.PrefetchOptions.Cache, }, // Make sure we don't accidentally add a field to make this union // bigger than expected. Note that in Debug builds, Zig is allowed // to insert a secret field for safety checks. comptime { if (builtin.mode != .Debug) { assert(@sizeOf(Data) == 8); } } }; }; /// Trailing is a list of instruction indexes for every `body_len`. pub const Block = struct { body_len: u32, }; /// Trailing is a list of `Inst.Ref` for every `args_len`. pub const Call = struct { args_len: u32, }; /// This data is stored inside extra, with two sets of trailing `Inst.Ref`: /// * 0. the then body, according to `then_body_len`. /// * 1. the else body, according to `else_body_len`. pub const CondBr = struct { then_body_len: u32, else_body_len: u32, }; /// Trailing: /// * 0. `Case` for each `cases_len` /// * 1. the else body, according to `else_body_len`. pub const SwitchBr = struct { cases_len: u32, else_body_len: u32, /// Trailing: /// * item: Inst.Ref // for each `items_len`. /// * instruction index for each `body_len`. pub const Case = struct { items_len: u32, body_len: u32, }; }; pub const StructField = struct { /// Whether this is a pointer or byval is determined by the AIR tag. struct_operand: Inst.Ref, field_index: u32, }; pub const Bin = struct { lhs: Inst.Ref, rhs: Inst.Ref, }; pub const FieldParentPtr = struct { field_ptr: Inst.Ref, field_index: u32, }; /// Trailing: /// 0. `Inst.Ref` for every outputs_len /// 1. `Inst.Ref` for every inputs_len /// 2. for every outputs_len /// - constraint: memory at this position is reinterpreted as a null /// terminated string. pad to the next u32 after the null byte. /// 3. for every inputs_len /// - constraint: memory at this position is reinterpreted as a null /// terminated string. pad to the next u32 after the null byte. /// 4. for every clobbers_len /// - clobber_name: memory at this position is reinterpreted as a null /// terminated string. pad to the next u32 after the null byte. /// 5. A number of u32 elements follow according to the equation `(source_len + 3) / 4`. /// Memory starting at this position is reinterpreted as the source bytes. pub const Asm = struct { /// Length of the assembly source in bytes. source_len: u32, outputs_len: u32, inputs_len: u32, /// The MSB is `is_volatile`. /// The rest of the bits are `clobbers_len`. flags: u32, }; pub const Cmpxchg = struct { ptr: Inst.Ref, expected_value: Inst.Ref, new_value: Inst.Ref, /// 0b00000000000000000000000000000XXX - success_order /// 0b00000000000000000000000000XXX000 - failure_order flags: u32, pub fn successOrder(self: Cmpxchg) std.builtin.AtomicOrder { return @intToEnum(std.builtin.AtomicOrder, @truncate(u3, self.flags)); } pub fn failureOrder(self: Cmpxchg) std.builtin.AtomicOrder { return @intToEnum(std.builtin.AtomicOrder, @truncate(u3, self.flags >> 3)); } }; pub const AtomicRmw = struct { operand: Inst.Ref, /// 0b00000000000000000000000000000XXX - ordering /// 0b0000000000000000000000000XXXX000 - op flags: u32, pub fn ordering(self: AtomicRmw) std.builtin.AtomicOrder { return @intToEnum(std.builtin.AtomicOrder, @truncate(u3, self.flags)); } pub fn op(self: AtomicRmw) std.builtin.AtomicRmwOp { return @intToEnum(std.builtin.AtomicRmwOp, @truncate(u4, self.flags >> 3)); } }; pub fn getMainBody(air: Air) []const Air.Inst.Index { const body_index = air.extra[@enumToInt(ExtraIndex.main_block)]; const extra = air.extraData(Block, body_index); return air.extra[extra.end..][0..extra.data.body_len]; } pub fn typeOf(air: Air, inst: Air.Inst.Ref) Type { const ref_int = @enumToInt(inst); if (ref_int < Air.Inst.Ref.typed_value_map.len) { return Air.Inst.Ref.typed_value_map[ref_int].ty; } return air.typeOfIndex(@intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len)); } pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type { const datas = air.instructions.items(.data); switch (air.instructions.items(.tag)[inst]) { .add, .addwrap, .add_sat, .sub, .subwrap, .sub_sat, .mul, .mulwrap, .mul_sat, .div_float, .div_trunc, .div_floor, .div_exact, .rem, .mod, .bit_and, .bit_or, .xor, .ptr_add, .ptr_sub, .shr, .shr_exact, .shl, .shl_exact, .shl_sat, .min, .max, => return air.typeOf(datas[inst].bin_op.lhs), .sqrt, .sin, .cos, .exp, .exp2, .log, .log2, .log10, .fabs, .floor, .ceil, .round, .trunc_float, => return air.typeOf(datas[inst].un_op), .cmp_lt, .cmp_lte, .cmp_eq, .cmp_gte, .cmp_gt, .cmp_neq, .is_null, .is_non_null, .is_null_ptr, .is_non_null_ptr, .is_err, .is_non_err, .is_err_ptr, .is_non_err_ptr, .bool_and, .bool_or, => return Type.initTag(.bool), .const_ty => return Type.initTag(.type), .alloc, .ret_ptr, .arg, => return datas[inst].ty, .assembly, .block, .constant, .struct_field_ptr, .struct_field_val, .slice_elem_ptr, .ptr_elem_ptr, .cmpxchg_weak, .cmpxchg_strong, .slice, .vector_init, .field_parent_ptr, => return air.getRefType(datas[inst].ty_pl.ty), .not, .bitcast, .load, .fpext, .fptrunc, .intcast, .trunc, .optional_payload, .optional_payload_ptr, .optional_payload_ptr_set, .errunion_payload_ptr_set, .wrap_optional, .unwrap_errunion_payload, .unwrap_errunion_err, .unwrap_errunion_payload_ptr, .unwrap_errunion_err_ptr, .wrap_errunion_payload, .wrap_errunion_err, .slice_ptr, .ptr_slice_len_ptr, .ptr_slice_ptr_ptr, .struct_field_ptr_index_0, .struct_field_ptr_index_1, .struct_field_ptr_index_2, .struct_field_ptr_index_3, .array_to_slice, .float_to_int, .int_to_float, .splat, .get_union_tag, .clz, .ctz, .popcount, .byte_swap, .bit_reverse, => return air.getRefType(datas[inst].ty_op.ty), .loop, .br, .cond_br, .switch_br, .ret, .ret_load, .unreach, => return Type.initTag(.noreturn), .breakpoint, .dbg_stmt, .store, .fence, .atomic_store_unordered, .atomic_store_monotonic, .atomic_store_release, .atomic_store_seq_cst, .memset, .memcpy, .set_union_tag, .prefetch, => return Type.initTag(.void), .ptrtoint, .slice_len, .ret_addr, => return Type.initTag(.usize), .bool_to_int => return Type.initTag(.u1), .tag_name, .error_name => return Type.initTag(.const_slice_u8_sentinel_0), .call => { const callee_ty = air.typeOf(datas[inst].pl_op.operand); switch (callee_ty.zigTypeTag()) { .Fn => return callee_ty.fnReturnType(), .Pointer => return callee_ty.childType().fnReturnType(), else => unreachable, } }, .slice_elem_val, .ptr_elem_val, .array_elem_val => { const ptr_ty = air.typeOf(datas[inst].bin_op.lhs); return ptr_ty.elemType(); }, .atomic_load => { const ptr_ty = air.typeOf(datas[inst].atomic_load.ptr); return ptr_ty.elemType(); }, .atomic_rmw => { const ptr_ty = air.typeOf(datas[inst].pl_op.operand); return ptr_ty.elemType(); }, .add_with_overflow, .sub_with_overflow, .mul_with_overflow, .shl_with_overflow, => return Type.initTag(.bool), } } pub fn getRefType(air: Air, ref: Air.Inst.Ref) Type { const ref_int = @enumToInt(ref); if (ref_int < Air.Inst.Ref.typed_value_map.len) { var buffer: Value.ToTypeBuffer = undefined; return Air.Inst.Ref.typed_value_map[ref_int].val.toType(&buffer); } const inst_index = ref_int - Air.Inst.Ref.typed_value_map.len; const air_tags = air.instructions.items(.tag); const air_datas = air.instructions.items(.data); assert(air_tags[inst_index] == .const_ty); return air_datas[inst_index].ty; } /// Returns the requested data, as well as the new index which is at the start of the /// trailers for the object. pub fn extraData(air: Air, comptime T: type, index: usize) struct { data: T, end: usize } { const fields = std.meta.fields(T); var i: usize = index; var result: T = undefined; inline for (fields) |field| { @field(result, field.name) = switch (field.field_type) { u32 => air.extra[i], Inst.Ref => @intToEnum(Inst.Ref, air.extra[i]), i32 => @bitCast(i32, air.extra[i]), else => @compileError("bad field type"), }; i += 1; } return .{ .data = result, .end = i, }; } pub fn deinit(air: *Air, gpa: std.mem.Allocator) void { air.instructions.deinit(gpa); gpa.free(air.extra); gpa.free(air.values); air.* = undefined; } const ref_start_index: u32 = Air.Inst.Ref.typed_value_map.len; pub fn indexToRef(inst: Air.Inst.Index) Air.Inst.Ref { return @intToEnum(Air.Inst.Ref, ref_start_index + inst); } pub fn refToIndex(inst: Air.Inst.Ref) ?Air.Inst.Index { const ref_int = @enumToInt(inst); if (ref_int >= ref_start_index) { return ref_int - ref_start_index; } else { return null; } } /// Returns `null` if runtime-known. pub fn value(air: Air, inst: Air.Inst.Ref) ?Value { const ref_int = @enumToInt(inst); if (ref_int < Air.Inst.Ref.typed_value_map.len) { return Air.Inst.Ref.typed_value_map[ref_int].val; } const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len); const air_datas = air.instructions.items(.data); switch (air.instructions.items(.tag)[inst_index]) { .constant => return air.values[air_datas[inst_index].ty_pl.payload], .const_ty => unreachable, else => return air.typeOfIndex(inst_index).onePossibleValue(), } }
const std = @import("std"); const time = std.time; const math = @import("./math.zig"); fn print_row(stdout: anytype, f: []const u8, v: f32, t: f64) void { stdout.print("{s: >8} {d: >5.2} {d: >5.2}s {d: >5.3}s\n", .{ f, v, t, t }) catch unreachable; } pub fn main() !void { const stdout = std.io.getStdOut().writer(); var prng = std.rand.DefaultPrng.init(@intCast(u64, time.milliTimestamp())); const M = 2 << 16; const N = 1024; var start: i128 = undefined; var end: i128 = undefined; var acc: i128 = undefined; var i: usize = undefined; var j: usize = undefined; var X: [N]f32 = undefined; var Y: [N]f32 = undefined; // tests rewritten until results confirmed hypothesis i = 0; while (i < N) : (i += 1) { X[i] = prng.random.float(f32); } stdout.print("{}\n", .{X[0]}) catch unreachable; stdout.print("{s: <8} {s: <5} {s: <7}\n", .{ "function", "value", "time" }) catch unreachable; i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = @sqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "@sqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = std.math.sqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "sqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = math.a_sqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "a_sqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = math.f_sqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "f_sqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = 1 / @sqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "1/@sqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = 1 / std.math.sqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "1/sqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = math.a_isqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "a_isqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); i = 0; start = time.nanoTimestamp(); while (i < M) : (i += 1) { j = 0; while (j < N) : (j += 1) { Y[j] = math.f_isqrt(X[j]); } } end = time.nanoTimestamp(); print_row(stdout, "f_isqrt", Y[0], (@intToFloat(f64, end - start) / 1_000_000_000.0)); }
const std = @import("std"); const builtin = @import("builtin"); const backend = if (@hasDecl(@import("root"), "zgtBackend")) @import("root").zgtBackend else switch (builtin.os.tag) { .windows => @import("backends/win32/backend.zig"), .linux => @import("backends/gtk/backend.zig"), .freestanding => blk: { if (builtin.cpu.arch == .wasm32) { break :blk @import("backends/wasm/backend.zig"); } else { @compileError("Unsupported OS: freestanding"); } }, else => @compileError(std.fmt.comptimePrint("Unsupported OS: {}", .{builtin.os.tag})), }; pub usingnamespace backend; test "backend: create window" { try backend.init(); var window = try backend.Window.create(); window.show(); { var i: usize = 0; while (i < 30) : (i += 1) { if (i == 15) { window.close(); } try std.testing.expectEqual(i < 15, backend.runStep(.Asynchronous)); } } } test "backend: text field" { try backend.init(); var field = try backend.TextField.create(); defer field.deinit(); field.setText("Hello, World!"); try std.testing.expectEqualStrings("Hello, World!", field.getText()); const str = "שָׁלוֹםUnicode 👩‍👦‍👦 नमस्ते"; field.setText(str); try std.testing.expectEqualStrings(str, field.getText()); } test "backend: scrollable" { try backend.init(); var scrollable = try backend.Scrollable.create(); defer scrollable.deinit(); // TODO: more tests }
// DNS RDATA understanding (parsing etc) const std = @import("std"); const io = std.io; const fmt = std.fmt; const dns = @import("../dns.zig"); const InError = io.SliceInStream.Error; const OutError = io.SliceOutStream.Error; const Type = dns.Type; pub const SOAData = struct { mname: dns.Name, rname: dns.Name, serial: u32, refresh: u32, retry: u32, expire: u32, minimum: u32, }; pub const MXData = struct { preference: u16, exchange: dns.Name, }; pub const SRVData = struct { priority: u16, weight: u16, port: u16, target: dns.Name, }; /// DNS RDATA representation to a "native-r" type for nicer usage. pub const DNSRData = union(Type) { A: std.net.Address, AAAA: std.net.Address, NS: dns.Name, MD: dns.Name, MF: dns.Name, CNAME: dns.Name, SOA: SOAData, MB: dns.Name, MG: dns.Name, MR: dns.Name, // ???? NULL: void, // TODO WKS bit map WKS: struct { addr: u32, proto: u8, // how to define bit map? align(8)? }, PTR: dns.Name, // TODO replace by Name? HINFO: struct { cpu: []const u8, os: []const u8, }, MINFO: struct { rmailbx: dns.Name, emailbx: dns.Name, }, MX: MXData, TXT: [][]const u8, SRV: SRVData, pub fn size(self: @This()) usize { return switch (self) { .A => 4, .AAAA => 16, .NS, .MD, .MF, .MB, .MG, .MR, .CNAME, .PTR => |name| name.size(), else => @panic("TODO"), }; } /// Format the RData into a prettier version of it. /// For example, A rdata would be formatted to its ipv4 address. pub fn format(self: @This(), comptime f: []const u8, options: fmt.FormatOptions, writer: anytype) !void { if (f.len != 0) { @compileError("Unknown format character: '" ++ f ++ "'"); } switch (self) { .A, .AAAA => |addr| return fmt.format(writer, "{}", .{addr}), .NS, .MD, .MF, .MB, .MG, .MR, .CNAME, .PTR => |name| return fmt.format(writer, "{}", .{name}), .SOA => |soa| return fmt.format(writer, "{} {} {} {} {} {} {}", .{ soa.mname, soa.rname, soa.serial, soa.refresh, soa.retry, soa.expire, soa.minimum, }), .MX => |mx| return fmt.format(writer, "{} {}", .{ mx.preference, mx.exchange }), .SRV => |srv| return fmt.format(writer, "{} {} {} {}", .{ srv.priority, srv.weight, srv.port, srv.target, }), else => return fmt.format(writer, "TODO support {}", .{@tagName(self)}), } return fmt.format(writer, "{}"); } }; /// Deserialize a given Resource's RDATA information. Requires the original /// Packet for allocation for memory ownership. pub fn deserializeRData( pkt_const: dns.Packet, resource: dns.Resource, ) !DNSRData { var pkt = pkt_const; var in = dns.FixedStream{ .buffer = resource.opaque_rdata, .pos = 0 }; var deserializer = dns.DNSDeserializer.init(in.reader()); var rdata = switch (resource.rr_type) { .A => blk: { var ip4addr: [4]u8 = undefined; for (ip4addr) |_, i| { ip4addr[i] = try deserializer.deserialize(u8); } break :blk DNSRData{ .A = std.net.Address.initIp4(ip4addr, 0), }; }, .AAAA => blk: { var ip6_addr: [16]u8 = undefined; for (ip6_addr) |byte, i| { ip6_addr[i] = try deserializer.deserialize(u8); } break :blk DNSRData{ .AAAA = std.net.Address.initIp6(ip6_addr, 0, 0, 0), }; }, .NS => DNSRData{ .NS = try pkt.deserializeName(&deserializer) }, .CNAME => DNSRData{ .CNAME = try pkt.deserializeName(&deserializer) }, .PTR => DNSRData{ .PTR = try pkt.deserializeName(&deserializer) }, .MX => blk: { break :blk DNSRData{ .MX = MXData{ .preference = try deserializer.deserialize(u16), .exchange = try pkt.deserializeName(&deserializer), }, }; }, .MD => DNSRData{ .MD = try pkt.deserializeName(&deserializer) }, .MF => DNSRData{ .MF = try pkt.deserializeName(&deserializer) }, .SOA => blk: { var mname = try pkt.deserializeName(&deserializer); var rname = try pkt.deserializeName(&deserializer); var serial = try deserializer.deserialize(u32); var refresh = try deserializer.deserialize(u32); var retry = try deserializer.deserialize(u32); var expire = try deserializer.deserialize(u32); var minimum = try deserializer.deserialize(u32); break :blk DNSRData{ .SOA = SOAData{ .mname = mname, .rname = rname, .serial = serial, .refresh = refresh, .retry = retry, .expire = expire, .minimum = minimum, }, }; }, .SRV => blk: { const priority = try deserializer.deserialize(u16); const weight = try deserializer.deserialize(u16); const port = try deserializer.deserialize(u16); var target = try pkt.deserializeName(&deserializer); break :blk DNSRData{ .SRV = .{ .priority = priority, .weight = weight, .port = port, .target = target, }, }; }, else => { return error.InvalidRData; }, }; return rdata; } /// Serialize a given DNSRData into []u8 pub fn serializeRData( rdata: DNSRData, serializer: anytype, ) !void { switch (rdata) { .A => |addr| try serializer.serialize(addr.in.addr), .AAAA => |addr| try serializer.serialize(addr.in6.addr), .NS, .MD, .MF, .MB, .MG, .MR, .CNAME, .PTR => |name| try serializer.serialize(name), .SOA => |soa_data| blk: { try serializer.serialize(soa_data.mname); try serializer.serialize(soa_data.rname); try serializer.serialize(soa_data.serial); try serializer.serialize(soa_data.refresh); try serializer.serialize(soa_data.retry); try serializer.serialize(soa_data.expire); try serializer.serialize(soa_data.minimum); }, .MX => |mxdata| blk: { try serializer.serialize(mxdata.preference); try serializer.serialize(mxdata.exchange); }, .SRV => |srv| blk: { try serializer.serialize(srv.priority); try serializer.serialize(srv.weight); try serializer.serialize(srv.port); try serializer.serialize(srv.target); }, else => return error.NotImplemented, } }
const std = @import("std"); const c = @import("../c.zig"); const gui = @import("../gui.zig"); pub const Color = struct { const Self = @This(); r: f32, g: f32, b: f32, fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { try buf.append(.{ .x = self.r, .y = self.g, .z = self.b, .w = 0 }); } }; pub const Diffuse = struct { const Self = @This(); color: Color, fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { return self.color.encode(buf); } fn draw_gui(self: *Self) bool { return c.igColorEdit3("", @ptrCast([*c]f32, &self.color), 0); } }; pub const Light = struct { const Self = @This(); // The GUI clamps colors to 0-1, so we include a secondary multiplier // to adjust brightness beyond that range. color: Color, intensity: f32, fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { return buf.append(.{ .x = self.color.r * self.intensity, .y = self.color.g * self.intensity, .z = self.color.b * self.intensity, .w = 0, }); } fn draw_gui(self: *Self) bool { const a = c.igColorEdit3("", @ptrCast([*c]f32, &self.color), 0); c.igPushItemWidth(c.igGetWindowWidth() * 0.4); const b = c.igDragFloat("intensity", &self.intensity, 0.05, 1, 10, "%.2f", 0); c.igPopItemWidth(); return a or b; } }; pub const Metal = struct { const Self = @This(); color: Color, fuzz: f32, fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { try buf.append(.{ .x = self.color.r, .y = self.color.g, .z = self.color.b, .w = self.fuzz, }); } fn draw_gui(self: *Self) bool { const a = c.igColorEdit3("", @ptrCast([*c]f32, &self.color), 0); c.igPushItemWidth(c.igGetWindowWidth() * 0.4); const b = c.igDragFloat("fuzz", &self.fuzz, 0.01, 0, 10, "%.2f", 0); c.igPopItemWidth(); return a or b; } }; pub const Glass = struct { const Self = @This(); eta: f32, slope: f32, fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { try buf.append(.{ .x = self.eta, .y = self.slope, .z = 0, .w = 0, }); } fn draw_gui(self: *Self) bool { c.igPushItemWidth(c.igGetWindowWidth() * 0.4); const a = c.igDragFloat("eta", &self.eta, 0.01, 1, 2, "%.2f", 0); const b = c.igDragFloat("slope", &self.slope, 0.0001, 0, 0.01, "%.4f", 0); c.igPopItemWidth(); return a or b; } }; pub const Laser = struct { const Self = @This(); // The GUI clamps colors to 0-1, so we include a secondary multiplier // to adjust brightness beyond that range. color: Color, intensity: f32, focus: f32, fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { return buf.append(.{ .x = self.color.r * self.intensity, .y = self.color.g * self.intensity, .z = self.color.b * self.intensity, .w = self.focus, }); } fn draw_gui(self: *Self) bool { const a = c.igColorEdit3("", @ptrCast([*c]f32, &self.color), 0); c.igPushItemWidth(c.igGetWindowWidth() * 0.4); const b = c.igDragFloat("intensity", &self.intensity, 0.05, 1, 500, "%.2f", 0); const f = c.igDragFloat("focus", &self.focus, 0.01, 0, 1, "%.2f", 0); c.igPopItemWidth(); return a or b or f; } }; pub const Metaflat = struct { // Nothing in the struct }; pub const Material = union(enum) { const Self = @This(); Diffuse: Diffuse, Light: Light, Metal: Metal, Glass: Glass, Laser: Laser, Metaflat: Metaflat, pub fn tag(self: Self) u32 { return switch (self) { .Diffuse => c.MAT_DIFFUSE, .Light => c.MAT_LIGHT, .Metal => c.MAT_METAL, .Glass => c.MAT_GLASS, .Laser => c.MAT_LASER, .Metaflat => c.MAT_METAFLAT, }; } pub fn new_diffuse(r: f32, g: f32, b: f32) Self { return .{ .Diffuse = .{ .color = .{ .r = r, .g = g, .b = b } }, }; } pub fn new_light(r: f32, g: f32, b: f32, intensity: f32) Self { return .{ .Light = .{ .color = .{ .r = r, .g = g, .b = b }, .intensity = intensity, }, }; } pub fn new_metal(r: f32, g: f32, b: f32, fuzz: f32) Self { return .{ .Metal = .{ .color = .{ .r = r, .g = g, .b = b }, .fuzz = fuzz }, }; } pub fn new_glass(eta: f32, slope: f32) Self { return .{ .Glass = .{ .eta = eta, .slope = slope }, }; } pub fn new_laser(r: f32, g: f32, b: f32, intensity: f32, focus: f32) Self { return .{ .Laser = .{ .color = .{ .r = r, .g = g, .b = b }, .intensity = intensity, .focus = focus, }, }; } pub fn new_metaflat() Self { return .{ .Metaflat = .{}, }; } pub fn encode(self: Self, buf: *std.ArrayList(c.vec4)) !void { // We don't encode the tag here, because we can put it into the Shape // header to save space. return switch (self) { .Diffuse => |m| m.encode(buf), .Light => |m| m.encode(buf), .Metal => |m| m.encode(buf), .Glass => |m| m.encode(buf), .Laser => |m| m.encode(buf), .Metaflat => {}, }; } fn color(self: *const Self) Color { return switch (self.*) { .Diffuse => |m| m.color, .Light => |m| m.color, .Metal => |m| m.color, .Glass => Color{ .r = 1, .g = 0.5, .b = 0.5 }, .Laser => |m| m.color, .Metaflat => Color{ .r = 1, .g = 0.5, .b = 0.5 }, }; } pub fn draw_gui(self: *Self) bool { comptime var widgets = @import("../gui/widgets.zig"); var changed = false; if (widgets.draw_enum_combo(Self, self.*)) |e| { // Swap the material type if the combo box returns a new tag changed = true; switch (e) { .Diffuse => self.* = .{ .Diffuse = .{ .color = self.color(), }, }, .Light => self.* = .{ .Light = .{ .color = self.color(), .intensity = 1, }, }, .Metal => self.* = .{ .Metal = .{ .color = self.color(), .fuzz = 0.1, }, }, .Glass => self.* = .{ .Glass = .{ .eta = 0.15, .slope = 0.013, }, }, .Laser => self.* = .{ .Laser = .{ .color = self.color(), .intensity = 1, .focus = 0.5, }, }, .Metaflat => self.* = .{ .Metaflat = .{}, }, } } changed = switch (self.*) { .Diffuse => |*d| d.draw_gui(), .Light => |*d| d.draw_gui(), .Metal => |*d| d.draw_gui(), .Glass => |*d| d.draw_gui(), .Laser => |*d| d.draw_gui(), .Metaflat => false, } or changed; return changed; } };
pub usingnamespace @cImport({ @cDefine("GLFW_INCLUDE_GLCOREARB", ""); @cInclude("GLFW/glfw3.h"); @cInclude("stb_image_write.h"); }); pub var glCreateTextures: @typeInfo(PFNGLCREATETEXTURESPROC).Optional.child = undefined; pub var glTextureStorage2D: @typeInfo(PFNGLTEXTURESTORAGE2DPROC).Optional.child = undefined; pub var glTextureStorage2DMultisample: @typeInfo(PFNGLTEXTURESTORAGE2DMULTISAMPLEPROC).Optional.child = undefined; pub var glTextureParameteri: @typeInfo(PFNGLTEXTUREPARAMETERIPROC).Optional.child = undefined; pub var glTextureParameterf: @typeInfo(PFNGLTEXTUREPARAMETERFPROC).Optional.child = undefined; pub var glTextureParameterfv: @typeInfo(PFNGLTEXTUREPARAMETERFVPROC).Optional.child = undefined; pub var glTextureSubImage2D: @typeInfo(PFNGLTEXTURESUBIMAGE2DPROC).Optional.child = undefined; pub var glCreateFramebuffers: @typeInfo(PFNGLCREATEFRAMEBUFFERSPROC).Optional.child = undefined; pub var glDeleteFramebuffers: @typeInfo(PFNGLDELETEFRAMEBUFFERSPROC).Optional.child = undefined; pub var glBindFramebuffer: @typeInfo(PFNGLBINDFRAMEBUFFERPROC).Optional.child = undefined; pub var glNamedFramebufferTexture: @typeInfo(PFNGLNAMEDFRAMEBUFFERTEXTUREPROC).Optional.child = undefined; pub var glBlitNamedFramebuffer: @typeInfo(PFNGLBLITNAMEDFRAMEBUFFERPROC).Optional.child = undefined; pub var glClearNamedFramebufferfv: @typeInfo(PFNGLCLEARNAMEDFRAMEBUFFERFVPROC).Optional.child = undefined; pub var glClearNamedFramebufferfi: @typeInfo(PFNGLCLEARNAMEDFRAMEBUFFERFIPROC).Optional.child = undefined; pub var glDebugMessageCallback: @typeInfo(PFNGLDEBUGMESSAGECALLBACKPROC).Optional.child = undefined; pub var glUseProgram: @typeInfo(PFNGLUSEPROGRAMPROC).Optional.child = undefined; pub var glUseProgramStages: @typeInfo(PFNGLUSEPROGRAMSTAGESPROC).Optional.child = undefined; pub var glCreateShaderProgramv: @typeInfo(PFNGLCREATESHADERPROGRAMVPROC).Optional.child = undefined; pub var glDeleteProgram: @typeInfo(PFNGLDELETEPROGRAMPROC).Optional.child = undefined; pub var glCreateProgramPipelines: @typeInfo(PFNGLCREATEPROGRAMPIPELINESPROC).Optional.child = undefined; pub var glBindProgramPipeline: @typeInfo(PFNGLBINDPROGRAMPIPELINEPROC).Optional.child = undefined; pub var glProgramUniform1f: @typeInfo(PFNGLPROGRAMUNIFORM1FPROC).Optional.child = undefined; pub var glProgramUniform2f: @typeInfo(PFNGLPROGRAMUNIFORM2FPROC).Optional.child = undefined; pub var glProgramUniform4fv: @typeInfo(PFNGLPROGRAMUNIFORM4FVPROC).Optional.child = undefined; pub var glProgramUniform1i: @typeInfo(PFNGLPROGRAMUNIFORM1IPROC).Optional.child = undefined; pub var glProgramUniform2i: @typeInfo(PFNGLPROGRAMUNIFORM2IPROC).Optional.child = undefined; pub var glMemoryBarrier: @typeInfo(PFNGLMEMORYBARRIERPROC).Optional.child = undefined; pub var glDispatchCompute: @typeInfo(PFNGLDISPATCHCOMPUTEPROC).Optional.child = undefined; pub var glGetProgramiv: @typeInfo(PFNGLGETPROGRAMIVPROC).Optional.child = undefined; pub var glGetProgramInfoLog: @typeInfo(PFNGLGETPROGRAMINFOLOGPROC).Optional.child = undefined; pub var glBindImageTexture: @typeInfo(PFNGLBINDIMAGETEXTUREPROC).Optional.child = undefined; pub var glTextureBarrier: @typeInfo(PFNGLTEXTUREBARRIERPROC).Optional.child = undefined; pub var glClearTexImage: @typeInfo(PFNGLCLEARTEXIMAGEPROC).Optional.child = undefined; pub var glEnable: @typeInfo(PFNGLENABLEPROC).Optional.child = undefined; pub var glGetError: @typeInfo(PFNGLGETERRORPROC).Optional.child = undefined; pub var glDeleteTextures: @typeInfo(PFNGLDELETETEXTURESPROC).Optional.child = undefined; pub var glBindTextureUnit: @typeInfo(PFNGLBINDTEXTUREUNITPROC).Optional.child = undefined; pub var glDrawArrays: @typeInfo(PFNGLDRAWARRAYSPROC).Optional.child = undefined; pub var glCreateVertexArrays: @typeInfo(PFNGLCREATEVERTEXARRAYSPROC).Optional.child = undefined; pub var glDeleteVertexArrays: @typeInfo(PFNGLDELETEVERTEXARRAYSPROC).Optional.child = undefined; pub var glBindVertexArray: @typeInfo(PFNGLBINDVERTEXARRAYPROC).Optional.child = undefined; pub var glReadPixels: @typeInfo(PFNGLREADPIXELSPROC).Optional.child = undefined; pub var glFinish: @typeInfo(PFNGLFINISHPROC).Optional.child = undefined; pub fn initOpenGlEntryPoints() void { glCreateTextures = @ptrCast(@TypeOf(glCreateTextures), glfwGetProcAddress("glCreateTextures").?); glTextureStorage2D = @ptrCast(@TypeOf(glTextureStorage2D), glfwGetProcAddress("glTextureStorage2D").?); glTextureStorage2DMultisample = @ptrCast(@TypeOf(glTextureStorage2DMultisample), glfwGetProcAddress("glTextureStorage2DMultisample").?); glTextureParameteri = @ptrCast(@TypeOf(glTextureParameteri), glfwGetProcAddress("glTextureParameteri").?); glTextureParameterf = @ptrCast(@TypeOf(glTextureParameterf), glfwGetProcAddress("glTextureParameterf").?); glTextureParameterfv = @ptrCast(@TypeOf(glTextureParameterfv), glfwGetProcAddress("glTextureParameterfv").?); glTextureSubImage2D = @ptrCast(@TypeOf(glTextureSubImage2D), glfwGetProcAddress("glTextureSubImage2D").?); glCreateFramebuffers = @ptrCast(@TypeOf(glCreateFramebuffers), glfwGetProcAddress("glCreateFramebuffers").?); glDeleteFramebuffers = @ptrCast(@TypeOf(glDeleteFramebuffers), glfwGetProcAddress("glDeleteFramebuffers").?); glBindFramebuffer = @ptrCast(@TypeOf(glBindFramebuffer), glfwGetProcAddress("glBindFramebuffer").?); glNamedFramebufferTexture = @ptrCast(@TypeOf(glNamedFramebufferTexture), glfwGetProcAddress("glNamedFramebufferTexture").?); glBlitNamedFramebuffer = @ptrCast(@TypeOf(glBlitNamedFramebuffer), glfwGetProcAddress("glBlitNamedFramebuffer").?); glClearNamedFramebufferfv = @ptrCast(@TypeOf(glClearNamedFramebufferfv), glfwGetProcAddress("glClearNamedFramebufferfv").?); glClearNamedFramebufferfi = @ptrCast(@TypeOf(glClearNamedFramebufferfi), glfwGetProcAddress("glClearNamedFramebufferfi").?); glDebugMessageCallback = @ptrCast(@TypeOf(glDebugMessageCallback), glfwGetProcAddress("glDebugMessageCallback").?); glUseProgram = @ptrCast(@TypeOf(glUseProgram), glfwGetProcAddress("glUseProgram").?); glUseProgramStages = @ptrCast(@TypeOf(glUseProgramStages), glfwGetProcAddress("glUseProgramStages").?); glCreateShaderProgramv = @ptrCast(@TypeOf(glCreateShaderProgramv), glfwGetProcAddress("glCreateShaderProgramv").?); glDeleteProgram = @ptrCast(@TypeOf(glDeleteProgram), glfwGetProcAddress("glDeleteProgram").?); glCreateProgramPipelines = @ptrCast(@TypeOf(glCreateProgramPipelines), glfwGetProcAddress("glCreateProgramPipelines").?); glBindProgramPipeline = @ptrCast(@TypeOf(glBindProgramPipeline), glfwGetProcAddress("glBindProgramPipeline").?); glProgramUniform1f = @ptrCast(@TypeOf(glProgramUniform1f), glfwGetProcAddress("glProgramUniform1f").?); glProgramUniform2f = @ptrCast(@TypeOf(glProgramUniform2f), glfwGetProcAddress("glProgramUniform2f").?); glProgramUniform4fv = @ptrCast(@TypeOf(glProgramUniform4fv), glfwGetProcAddress("glProgramUniform4fv").?); glProgramUniform1i = @ptrCast(@TypeOf(glProgramUniform1i), glfwGetProcAddress("glProgramUniform1i").?); glProgramUniform2i = @ptrCast(@TypeOf(glProgramUniform2i), glfwGetProcAddress("glProgramUniform2i").?); glMemoryBarrier = @ptrCast(@TypeOf(glMemoryBarrier), glfwGetProcAddress("glMemoryBarrier").?); glDispatchCompute = @ptrCast(@TypeOf(glDispatchCompute), glfwGetProcAddress("glDispatchCompute").?); glGetProgramiv = @ptrCast(@TypeOf(glGetProgramiv), glfwGetProcAddress("glGetProgramiv").?); glGetProgramInfoLog = @ptrCast(@TypeOf(glGetProgramInfoLog), glfwGetProcAddress("glGetProgramInfoLog").?); glBindImageTexture = @ptrCast(@TypeOf(glBindImageTexture), glfwGetProcAddress("glBindImageTexture").?); glTextureBarrier = @ptrCast(@TypeOf(glTextureBarrier), glfwGetProcAddress("glTextureBarrier").?); glClearTexImage = @ptrCast(@TypeOf(glClearTexImage), glfwGetProcAddress("glClearTexImage").?); glEnable = @ptrCast(@TypeOf(glEnable), glfwGetProcAddress("glEnable").?); glGetError = @ptrCast(@TypeOf(glGetError), glfwGetProcAddress("glGetError").?); glDeleteTextures = @ptrCast(@TypeOf(glDeleteTextures), glfwGetProcAddress("glDeleteTextures").?); glBindTextureUnit = @ptrCast(@TypeOf(glBindTextureUnit), glfwGetProcAddress("glBindTextureUnit").?); glDrawArrays = @ptrCast(@TypeOf(glDrawArrays), glfwGetProcAddress("glDrawArrays").?); glCreateVertexArrays = @ptrCast(@TypeOf(glCreateVertexArrays), glfwGetProcAddress("glCreateVertexArrays").?); glDeleteVertexArrays = @ptrCast(@TypeOf(glDeleteVertexArrays), glfwGetProcAddress("glDeleteVertexArrays").?); glBindVertexArray = @ptrCast(@TypeOf(glBindVertexArray), glfwGetProcAddress("glBindVertexArray").?); glReadPixels = @ptrCast(@TypeOf(glReadPixels), glfwGetProcAddress("glReadPixels").?); glFinish = @ptrCast(@TypeOf(glFinish), glfwGetProcAddress("glFinish").?); }
const expect = @import("std").testing.expect; test "simple generic fn" { expect(max(i32, 3, -1) == 3); expect(max(f32, 0.123, 0.456) == 0.456); expect(add(2, 3) == 5); } fn max(comptime T: type, a: T, b: T) T { return if (a > b) a else b; } fn add(comptime a: i32, b: i32) i32 { return (comptime a) + b; } const the_max = max(u32, 1234, 5678); test "compile time generic eval" { expect(the_max == 5678); } fn gimmeTheBigOne(a: u32, b: u32) u32 { return max(u32, a, b); } fn shouldCallSameInstance(a: u32, b: u32) u32 { return max(u32, a, b); } fn sameButWithFloats(a: f64, b: f64) f64 { return max(f64, a, b); } test "fn with comptime args" { expect(gimmeTheBigOne(1234, 5678) == 5678); expect(shouldCallSameInstance(34, 12) == 34); expect(sameButWithFloats(0.43, 0.49) == 0.49); } test "var params" { expect(max_i32(12, 34) == 34); expect(max_f64(1.2, 3.4) == 3.4); } comptime { expect(max_i32(12, 34) == 34); expect(max_f64(1.2, 3.4) == 3.4); } fn max_var(a: var, b: var) @typeOf(a + b) { return if (a > b) a else b; } fn max_i32(a: i32, b: i32) i32 { return max_var(a, b); } fn max_f64(a: f64, b: f64) f64 { return max_var(a, b); } pub fn List(comptime T: type) type { return SmallList(T, 8); } pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) type { return struct { items: []T, length: usize, prealloc_items: [STATIC_SIZE]T, }; } test "function with return type type" { var list: List(i32) = undefined; var list2: List(i32) = undefined; list.length = 10; list2.length = 10; expect(list.prealloc_items.len == 8); expect(list2.prealloc_items.len == 8); } test "generic struct" { var a1 = GenNode(i32){ .value = 13, .next = null, }; var b1 = GenNode(bool){ .value = true, .next = null, }; expect(a1.value == 13); expect(a1.value == a1.getVal()); expect(b1.getVal()); } fn GenNode(comptime T: type) type { return struct { value: T, next: ?*GenNode(T), fn getVal(n: *const GenNode(T)) T { return n.value; } }; } test "const decls in struct" { expect(GenericDataThing(3).count_plus_one == 4); } fn GenericDataThing(comptime count: isize) type { return struct { const count_plus_one = count + 1; }; } test "use generic param in generic param" { expect(aGenericFn(i32, 3, 4) == 7); } fn aGenericFn(comptime T: type, comptime a: T, b: T) T { return a + b; } test "generic fn with implicit cast" { expect(getFirstByte(u8, &[_]u8{13}) == 13); expect(getFirstByte(u16, &[_]u16{ 0, 13, }) == 0); } fn getByte(ptr: ?*const u8) u8 { return ptr.?.*; } fn getFirstByte(comptime T: type, mem: []const T) u8 { return getByte(@ptrCast(*const u8, &mem[0])); } const foos = [_]fn (var) bool{ foo1, foo2, }; fn foo1(arg: var) bool { return arg; } fn foo2(arg: var) bool { return !arg; } test "array of generic fns" { expect(foos[0](true)); expect(!foos[1](true)); }
const std = @import("std.zig"); const assert = std.debug.assert; const testing = std.testing; const mem = std.mem; const math = std.math; const builtin = @import("builtin"); /// Stable in-place sort. O(n) best case, O(pow(n, 2)) worst case. O(1) memory (no allocator required). pub fn insertionSort(comptime T: type, items: []T, lessThan: fn (lhs: T, rhs: T) bool) void { { var i: usize = 1; while (i < items.len) : (i += 1) { const x = items[i]; var j: usize = i; while (j > 0 and lessThan(x, items[j - 1])) : (j -= 1) { items[j] = items[j - 1]; } items[j] = x; } } } const Range = struct { start: usize, end: usize, fn init(start: usize, end: usize) Range { return Range{ .start = start, .end = end, }; } fn length(self: Range) usize { return self.end - self.start; } }; const Iterator = struct { size: usize, power_of_two: usize, numerator: usize, decimal: usize, denominator: usize, decimal_step: usize, numerator_step: usize, fn init(size2: usize, min_level: usize) Iterator { const power_of_two = math.floorPowerOfTwo(usize, size2); const denominator = power_of_two / min_level; return Iterator{ .numerator = 0, .decimal = 0, .size = size2, .power_of_two = power_of_two, .denominator = denominator, .decimal_step = size2 / denominator, .numerator_step = size2 % denominator, }; } fn begin(self: *Iterator) void { self.numerator = 0; self.decimal = 0; } fn nextRange(self: *Iterator) Range { const start = self.decimal; self.decimal += self.decimal_step; self.numerator += self.numerator_step; if (self.numerator >= self.denominator) { self.numerator -= self.denominator; self.decimal += 1; } return Range{ .start = start, .end = self.decimal, }; } fn finished(self: *Iterator) bool { return self.decimal >= self.size; } fn nextLevel(self: *Iterator) bool { self.decimal_step += self.decimal_step; self.numerator_step += self.numerator_step; if (self.numerator_step >= self.denominator) { self.numerator_step -= self.denominator; self.decimal_step += 1; } return (self.decimal_step < self.size); } fn length(self: *Iterator) usize { return self.decimal_step; } }; const Pull = struct { from: usize, to: usize, count: usize, range: Range, }; /// Stable in-place sort. O(n) best case, O(n*log(n)) worst case and average case. O(1) memory (no allocator required). /// Currently implemented as block sort. pub fn sort(comptime T: type, items: []T, lessThan: fn (lhs: T, rhs: T) bool) void { // Implementation ported from https://github.com/BonzaiThePenguin/WikiSort/blob/master/WikiSort.c var cache: [512]T = undefined; if (items.len < 4) { if (items.len == 3) { // hard coded insertion sort if (lessThan(items[1], items[0])) mem.swap(T, &items[0], &items[1]); if (lessThan(items[2], items[1])) { mem.swap(T, &items[1], &items[2]); if (lessThan(items[1], items[0])) mem.swap(T, &items[0], &items[1]); } } else if (items.len == 2) { if (lessThan(items[1], items[0])) mem.swap(T, &items[0], &items[1]); } return; } // sort groups of 4-8 items at a time using an unstable sorting network, // but keep track of the original item orders to force it to be stable // http://pages.ripco.net/~jgamble/nw.html var iterator = Iterator.init(items.len, 4); while (!iterator.finished()) { var order = [_]u8{ 0, 1, 2, 3, 4, 5, 6, 7 }; const range = iterator.nextRange(); const sliced_items = items[range.start..]; switch (range.length()) { 8 => { swap(T, sliced_items, lessThan, &order, 0, 1); swap(T, sliced_items, lessThan, &order, 2, 3); swap(T, sliced_items, lessThan, &order, 4, 5); swap(T, sliced_items, lessThan, &order, 6, 7); swap(T, sliced_items, lessThan, &order, 0, 2); swap(T, sliced_items, lessThan, &order, 1, 3); swap(T, sliced_items, lessThan, &order, 4, 6); swap(T, sliced_items, lessThan, &order, 5, 7); swap(T, sliced_items, lessThan, &order, 1, 2); swap(T, sliced_items, lessThan, &order, 5, 6); swap(T, sliced_items, lessThan, &order, 0, 4); swap(T, sliced_items, lessThan, &order, 3, 7); swap(T, sliced_items, lessThan, &order, 1, 5); swap(T, sliced_items, lessThan, &order, 2, 6); swap(T, sliced_items, lessThan, &order, 1, 4); swap(T, sliced_items, lessThan, &order, 3, 6); swap(T, sliced_items, lessThan, &order, 2, 4); swap(T, sliced_items, lessThan, &order, 3, 5); swap(T, sliced_items, lessThan, &order, 3, 4); }, 7 => { swap(T, sliced_items, lessThan, &order, 1, 2); swap(T, sliced_items, lessThan, &order, 3, 4); swap(T, sliced_items, lessThan, &order, 5, 6); swap(T, sliced_items, lessThan, &order, 0, 2); swap(T, sliced_items, lessThan, &order, 3, 5); swap(T, sliced_items, lessThan, &order, 4, 6); swap(T, sliced_items, lessThan, &order, 0, 1); swap(T, sliced_items, lessThan, &order, 4, 5); swap(T, sliced_items, lessThan, &order, 2, 6); swap(T, sliced_items, lessThan, &order, 0, 4); swap(T, sliced_items, lessThan, &order, 1, 5); swap(T, sliced_items, lessThan, &order, 0, 3); swap(T, sliced_items, lessThan, &order, 2, 5); swap(T, sliced_items, lessThan, &order, 1, 3); swap(T, sliced_items, lessThan, &order, 2, 4); swap(T, sliced_items, lessThan, &order, 2, 3); }, 6 => { swap(T, sliced_items, lessThan, &order, 1, 2); swap(T, sliced_items, lessThan, &order, 4, 5); swap(T, sliced_items, lessThan, &order, 0, 2); swap(T, sliced_items, lessThan, &order, 3, 5); swap(T, sliced_items, lessThan, &order, 0, 1); swap(T, sliced_items, lessThan, &order, 3, 4); swap(T, sliced_items, lessThan, &order, 2, 5); swap(T, sliced_items, lessThan, &order, 0, 3); swap(T, sliced_items, lessThan, &order, 1, 4); swap(T, sliced_items, lessThan, &order, 2, 4); swap(T, sliced_items, lessThan, &order, 1, 3); swap(T, sliced_items, lessThan, &order, 2, 3); }, 5 => { swap(T, sliced_items, lessThan, &order, 0, 1); swap(T, sliced_items, lessThan, &order, 3, 4); swap(T, sliced_items, lessThan, &order, 2, 4); swap(T, sliced_items, lessThan, &order, 2, 3); swap(T, sliced_items, lessThan, &order, 1, 4); swap(T, sliced_items, lessThan, &order, 0, 3); swap(T, sliced_items, lessThan, &order, 0, 2); swap(T, sliced_items, lessThan, &order, 1, 3); swap(T, sliced_items, lessThan, &order, 1, 2); }, 4 => { swap(T, sliced_items, lessThan, &order, 0, 1); swap(T, sliced_items, lessThan, &order, 2, 3); swap(T, sliced_items, lessThan, &order, 0, 2); swap(T, sliced_items, lessThan, &order, 1, 3); swap(T, sliced_items, lessThan, &order, 1, 2); }, else => {}, } } if (items.len < 8) return; // then merge sort the higher levels, which can be 8-15, 16-31, 32-63, 64-127, etc. while (true) { // if every A and B block will fit into the cache, use a special branch specifically for merging with the cache // (we use < rather than <= since the block size might be one more than iterator.length()) if (iterator.length() < cache.len) { // if four subarrays fit into the cache, it's faster to merge both pairs of subarrays into the cache, // then merge the two merged subarrays from the cache back into the original array if ((iterator.length() + 1) * 4 <= cache.len and iterator.length() * 4 <= items.len) { iterator.begin(); while (!iterator.finished()) { // merge A1 and B1 into the cache var A1 = iterator.nextRange(); var B1 = iterator.nextRange(); var A2 = iterator.nextRange(); var B2 = iterator.nextRange(); if (lessThan(items[B1.end - 1], items[A1.start])) { // the two ranges are in reverse order, so copy them in reverse order into the cache mem.copy(T, cache[B1.length()..], items[A1.start..A1.end]); mem.copy(T, cache[0..], items[B1.start..B1.end]); } else if (lessThan(items[B1.start], items[A1.end - 1])) { // these two ranges weren't already in order, so merge them into the cache mergeInto(T, items, A1, B1, lessThan, cache[0..]); } else { // if A1, B1, A2, and B2 are all in order, skip doing anything else if (!lessThan(items[B2.start], items[A2.end - 1]) and !lessThan(items[A2.start], items[B1.end - 1])) continue; // copy A1 and B1 into the cache in the same order mem.copy(T, cache[0..], items[A1.start..A1.end]); mem.copy(T, cache[A1.length()..], items[B1.start..B1.end]); } A1 = Range.init(A1.start, B1.end); // merge A2 and B2 into the cache if (lessThan(items[B2.end - 1], items[A2.start])) { // the two ranges are in reverse order, so copy them in reverse order into the cache mem.copy(T, cache[A1.length() + B2.length() ..], items[A2.start..A2.end]); mem.copy(T, cache[A1.length()..], items[B2.start..B2.end]); } else if (lessThan(items[B2.start], items[A2.end - 1])) { // these two ranges weren't already in order, so merge them into the cache mergeInto(T, items, A2, B2, lessThan, cache[A1.length()..]); } else { // copy A2 and B2 into the cache in the same order mem.copy(T, cache[A1.length()..], items[A2.start..A2.end]); mem.copy(T, cache[A1.length() + A2.length() ..], items[B2.start..B2.end]); } A2 = Range.init(A2.start, B2.end); // merge A1 and A2 from the cache into the items const A3 = Range.init(0, A1.length()); const B3 = Range.init(A1.length(), A1.length() + A2.length()); if (lessThan(cache[B3.end - 1], cache[A3.start])) { // the two ranges are in reverse order, so copy them in reverse order into the items mem.copy(T, items[A1.start + A2.length() ..], cache[A3.start..A3.end]); mem.copy(T, items[A1.start..], cache[B3.start..B3.end]); } else if (lessThan(cache[B3.start], cache[A3.end - 1])) { // these two ranges weren't already in order, so merge them back into the items mergeInto(T, cache[0..], A3, B3, lessThan, items[A1.start..]); } else { // copy A3 and B3 into the items in the same order mem.copy(T, items[A1.start..], cache[A3.start..A3.end]); mem.copy(T, items[A1.start + A1.length() ..], cache[B3.start..B3.end]); } } // we merged two levels at the same time, so we're done with this level already // (iterator.nextLevel() is called again at the bottom of this outer merge loop) _ = iterator.nextLevel(); } else { iterator.begin(); while (!iterator.finished()) { var A = iterator.nextRange(); var B = iterator.nextRange(); if (lessThan(items[B.end - 1], items[A.start])) { // the two ranges are in reverse order, so a simple rotation should fix it mem.rotate(T, items[A.start..B.end], A.length()); } else if (lessThan(items[B.start], items[A.end - 1])) { // these two ranges weren't already in order, so we'll need to merge them! mem.copy(T, cache[0..], items[A.start..A.end]); mergeExternal(T, items, A, B, lessThan, cache[0..]); } } } } else { // this is where the in-place merge logic starts! // 1. pull out two internal buffers each containing √A unique values // 1a. adjust block_size and buffer_size if we couldn't find enough unique values // 2. loop over the A and B subarrays within this level of the merge sort // 3. break A and B into blocks of size 'block_size' // 4. "tag" each of the A blocks with values from the first internal buffer // 5. roll the A blocks through the B blocks and drop/rotate them where they belong // 6. merge each A block with any B values that follow, using the cache or the second internal buffer // 7. sort the second internal buffer if it exists // 8. redistribute the two internal buffers back into the items var block_size: usize = math.sqrt(iterator.length()); var buffer_size = iterator.length() / block_size + 1; // as an optimization, we really only need to pull out the internal buffers once for each level of merges // after that we can reuse the same buffers over and over, then redistribute it when we're finished with this level var A: Range = undefined; var B: Range = undefined; var index: usize = 0; var last: usize = 0; var count: usize = 0; var find: usize = 0; var start: usize = 0; var pull_index: usize = 0; var pull = [_]Pull{ Pull{ .from = 0, .to = 0, .count = 0, .range = Range.init(0, 0), }, Pull{ .from = 0, .to = 0, .count = 0, .range = Range.init(0, 0), }, }; var buffer1 = Range.init(0, 0); var buffer2 = Range.init(0, 0); // find two internal buffers of size 'buffer_size' each find = buffer_size + buffer_size; var find_separately = false; if (block_size <= cache.len) { // if every A block fits into the cache then we won't need the second internal buffer, // so we really only need to find 'buffer_size' unique values find = buffer_size; } else if (find > iterator.length()) { // we can't fit both buffers into the same A or B subarray, so find two buffers separately find = buffer_size; find_separately = true; } // we need to find either a single contiguous space containing 2√A unique values (which will be split up into two buffers of size √A each), // or we need to find one buffer of < 2√A unique values, and a second buffer of √A unique values, // OR if we couldn't find that many unique values, we need the largest possible buffer we can get // in the case where it couldn't find a single buffer of at least √A unique values, // all of the Merge steps must be replaced by a different merge algorithm (MergeInPlace) iterator.begin(); while (!iterator.finished()) { A = iterator.nextRange(); B = iterator.nextRange(); // just store information about where the values will be pulled from and to, // as well as how many values there are, to create the two internal buffers // check A for the number of unique values we need to fill an internal buffer // these values will be pulled out to the start of A last = A.start; count = 1; while (count < find) : ({ last = index; count += 1; }) { index = findLastForward(T, items, items[last], Range.init(last + 1, A.end), lessThan, find - count); if (index == A.end) break; } index = last; if (count >= buffer_size) { // keep track of the range within the items where we'll need to "pull out" these values to create the internal buffer pull[pull_index] = Pull{ .range = Range.init(A.start, B.end), .count = count, .from = index, .to = A.start, }; pull_index = 1; if (count == buffer_size + buffer_size) { // we were able to find a single contiguous section containing 2√A unique values, // so this section can be used to contain both of the internal buffers we'll need buffer1 = Range.init(A.start, A.start + buffer_size); buffer2 = Range.init(A.start + buffer_size, A.start + count); break; } else if (find == buffer_size + buffer_size) { // we found a buffer that contains at least √A unique values, but did not contain the full 2√A unique values, // so we still need to find a second separate buffer of at least √A unique values buffer1 = Range.init(A.start, A.start + count); find = buffer_size; } else if (block_size <= cache.len) { // we found the first and only internal buffer that we need, so we're done! buffer1 = Range.init(A.start, A.start + count); break; } else if (find_separately) { // found one buffer, but now find the other one buffer1 = Range.init(A.start, A.start + count); find_separately = false; } else { // we found a second buffer in an 'A' subarray containing √A unique values, so we're done! buffer2 = Range.init(A.start, A.start + count); break; } } else if (pull_index == 0 and count > buffer1.length()) { // keep track of the largest buffer we were able to find buffer1 = Range.init(A.start, A.start + count); pull[pull_index] = Pull{ .range = Range.init(A.start, B.end), .count = count, .from = index, .to = A.start, }; } // check B for the number of unique values we need to fill an internal buffer // these values will be pulled out to the end of B last = B.end - 1; count = 1; while (count < find) : ({ last = index - 1; count += 1; }) { index = findFirstBackward(T, items, items[last], Range.init(B.start, last), lessThan, find - count); if (index == B.start) break; } index = last; if (count >= buffer_size) { // keep track of the range within the items where we'll need to "pull out" these values to create the internal buffe pull[pull_index] = Pull{ .range = Range.init(A.start, B.end), .count = count, .from = index, .to = B.end, }; pull_index = 1; if (count == buffer_size + buffer_size) { // we were able to find a single contiguous section containing 2√A unique values, // so this section can be used to contain both of the internal buffers we'll need buffer1 = Range.init(B.end - count, B.end - buffer_size); buffer2 = Range.init(B.end - buffer_size, B.end); break; } else if (find == buffer_size + buffer_size) { // we found a buffer that contains at least √A unique values, but did not contain the full 2√A unique values, // so we still need to find a second separate buffer of at least √A unique values buffer1 = Range.init(B.end - count, B.end); find = buffer_size; } else if (block_size <= cache.len) { // we found the first and only internal buffer that we need, so we're done! buffer1 = Range.init(B.end - count, B.end); break; } else if (find_separately) { // found one buffer, but now find the other one buffer1 = Range.init(B.end - count, B.end); find_separately = false; } else { // buffer2 will be pulled out from a 'B' subarray, so if the first buffer was pulled out from the corresponding 'A' subarray, // we need to adjust the end point for that A subarray so it knows to stop redistributing its values before reaching buffer2 if (pull[0].range.start == A.start) pull[0].range.end -= pull[1].count; // we found a second buffer in an 'B' subarray containing √A unique values, so we're done! buffer2 = Range.init(B.end - count, B.end); break; } } else if (pull_index == 0 and count > buffer1.length()) { // keep track of the largest buffer we were able to find buffer1 = Range.init(B.end - count, B.end); pull[pull_index] = Pull{ .range = Range.init(A.start, B.end), .count = count, .from = index, .to = B.end, }; } } // pull out the two ranges so we can use them as internal buffers pull_index = 0; while (pull_index < 2) : (pull_index += 1) { const length = pull[pull_index].count; if (pull[pull_index].to < pull[pull_index].from) { // we're pulling the values out to the left, which means the start of an A subarray index = pull[pull_index].from; count = 1; while (count < length) : (count += 1) { index = findFirstBackward(T, items, items[index - 1], Range.init(pull[pull_index].to, pull[pull_index].from - (count - 1)), lessThan, length - count); const range = Range.init(index + 1, pull[pull_index].from + 1); mem.rotate(T, items[range.start..range.end], range.length() - count); pull[pull_index].from = index + count; } } else if (pull[pull_index].to > pull[pull_index].from) { // we're pulling values out to the right, which means the end of a B subarray index = pull[pull_index].from + 1; count = 1; while (count < length) : (count += 1) { index = findLastForward(T, items, items[index], Range.init(index, pull[pull_index].to), lessThan, length - count); const range = Range.init(pull[pull_index].from, index - 1); mem.rotate(T, items[range.start..range.end], count); pull[pull_index].from = index - 1 - count; } } } // adjust block_size and buffer_size based on the values we were able to pull out buffer_size = buffer1.length(); block_size = iterator.length() / buffer_size + 1; // the first buffer NEEDS to be large enough to tag each of the evenly sized A blocks, // so this was originally here to test the math for adjusting block_size above // assert((iterator.length() + 1)/block_size <= buffer_size); // now that the two internal buffers have been created, it's time to merge each A+B combination at this level of the merge sort! iterator.begin(); while (!iterator.finished()) { A = iterator.nextRange(); B = iterator.nextRange(); // remove any parts of A or B that are being used by the internal buffers start = A.start; if (start == pull[0].range.start) { if (pull[0].from > pull[0].to) { A.start += pull[0].count; // if the internal buffer takes up the entire A or B subarray, then there's nothing to merge // this only happens for very small subarrays, like √4 = 2, 2 * (2 internal buffers) = 4, // which also only happens when cache.len is small or 0 since it'd otherwise use MergeExternal if (A.length() == 0) continue; } else if (pull[0].from < pull[0].to) { B.end -= pull[0].count; if (B.length() == 0) continue; } } if (start == pull[1].range.start) { if (pull[1].from > pull[1].to) { A.start += pull[1].count; if (A.length() == 0) continue; } else if (pull[1].from < pull[1].to) { B.end -= pull[1].count; if (B.length() == 0) continue; } } if (lessThan(items[B.end - 1], items[A.start])) { // the two ranges are in reverse order, so a simple rotation should fix it mem.rotate(T, items[A.start..B.end], A.length()); } else if (lessThan(items[A.end], items[A.end - 1])) { // these two ranges weren't already in order, so we'll need to merge them! var findA: usize = undefined; // break the remainder of A into blocks. firstA is the uneven-sized first A block var blockA = Range.init(A.start, A.end); var firstA = Range.init(A.start, A.start + blockA.length() % block_size); // swap the first value of each A block with the value in buffer1 var indexA = buffer1.start; index = firstA.end; while (index < blockA.end) : ({ indexA += 1; index += block_size; }) { mem.swap(T, &items[indexA], &items[index]); } // start rolling the A blocks through the B blocks! // whenever we leave an A block behind, we'll need to merge the previous A block with any B blocks that follow it, so track that information as well var lastA = firstA; var lastB = Range.init(0, 0); var blockB = Range.init(B.start, B.start + math.min(block_size, B.length())); blockA.start += firstA.length(); indexA = buffer1.start; // if the first unevenly sized A block fits into the cache, copy it there for when we go to Merge it // otherwise, if the second buffer is available, block swap the contents into that if (lastA.length() <= cache.len) { mem.copy(T, cache[0..], items[lastA.start..lastA.end]); } else if (buffer2.length() > 0) { blockSwap(T, items, lastA.start, buffer2.start, lastA.length()); } if (blockA.length() > 0) { while (true) { // if there's a previous B block and the first value of the minimum A block is <= the last value of the previous B block, // then drop that minimum A block behind. or if there are no B blocks left then keep dropping the remaining A blocks. if ((lastB.length() > 0 and !lessThan(items[lastB.end - 1], items[indexA])) or blockB.length() == 0) { // figure out where to split the previous B block, and rotate it at the split const B_split = binaryFirst(T, items, items[indexA], lastB, lessThan); const B_remaining = lastB.end - B_split; // swap the minimum A block to the beginning of the rolling A blocks var minA = blockA.start; findA = minA + block_size; while (findA < blockA.end) : (findA += block_size) { if (lessThan(items[findA], items[minA])) { minA = findA; } } blockSwap(T, items, blockA.start, minA, block_size); // swap the first item of the previous A block back with its original value, which is stored in buffer1 mem.swap(T, &items[blockA.start], &items[indexA]); indexA += 1; // locally merge the previous A block with the B values that follow it // if lastA fits into the external cache we'll use that (with MergeExternal), // or if the second internal buffer exists we'll use that (with MergeInternal), // or failing that we'll use a strictly in-place merge algorithm (MergeInPlace) if (lastA.length() <= cache.len) { mergeExternal(T, items, lastA, Range.init(lastA.end, B_split), lessThan, cache[0..]); } else if (buffer2.length() > 0) { mergeInternal(T, items, lastA, Range.init(lastA.end, B_split), lessThan, buffer2); } else { mergeInPlace(T, items, lastA, Range.init(lastA.end, B_split), lessThan); } if (buffer2.length() > 0 or block_size <= cache.len) { // copy the previous A block into the cache or buffer2, since that's where we need it to be when we go to merge it anyway if (block_size <= cache.len) { mem.copy(T, cache[0..], items[blockA.start .. blockA.start + block_size]); } else { blockSwap(T, items, blockA.start, buffer2.start, block_size); } // this is equivalent to rotating, but faster // the area normally taken up by the A block is either the contents of buffer2, or data we don't need anymore since we memcopied it // either way, we don't need to retain the order of those items, so instead of rotating we can just block swap B to where it belongs blockSwap(T, items, B_split, blockA.start + block_size - B_remaining, B_remaining); } else { // we are unable to use the 'buffer2' trick to speed up the rotation operation since buffer2 doesn't exist, so perform a normal rotation mem.rotate(T, items[B_split .. blockA.start + block_size], blockA.start - B_split); } // update the range for the remaining A blocks, and the range remaining from the B block after it was split lastA = Range.init(blockA.start - B_remaining, blockA.start - B_remaining + block_size); lastB = Range.init(lastA.end, lastA.end + B_remaining); // if there are no more A blocks remaining, this step is finished! blockA.start += block_size; if (blockA.length() == 0) break; } else if (blockB.length() < block_size) { // move the last B block, which is unevenly sized, to before the remaining A blocks, by using a rotation // the cache is disabled here since it might contain the contents of the previous A block mem.rotate(T, items[blockA.start..blockB.end], blockB.start - blockA.start); lastB = Range.init(blockA.start, blockA.start + blockB.length()); blockA.start += blockB.length(); blockA.end += blockB.length(); blockB.end = blockB.start; } else { // roll the leftmost A block to the end by swapping it with the next B block blockSwap(T, items, blockA.start, blockB.start, block_size); lastB = Range.init(blockA.start, blockA.start + block_size); blockA.start += block_size; blockA.end += block_size; blockB.start += block_size; if (blockB.end > B.end - block_size) { blockB.end = B.end; } else { blockB.end += block_size; } } } } // merge the last A block with the remaining B values if (lastA.length() <= cache.len) { mergeExternal(T, items, lastA, Range.init(lastA.end, B.end), lessThan, cache[0..]); } else if (buffer2.length() > 0) { mergeInternal(T, items, lastA, Range.init(lastA.end, B.end), lessThan, buffer2); } else { mergeInPlace(T, items, lastA, Range.init(lastA.end, B.end), lessThan); } } } // when we're finished with this merge step we should have the one or two internal buffers left over, where the second buffer is all jumbled up // insertion sort the second buffer, then redistribute the buffers back into the items using the opposite process used for creating the buffer // while an unstable sort like quicksort could be applied here, in benchmarks it was consistently slightly slower than a simple insertion sort, // even for tens of millions of items. this may be because insertion sort is quite fast when the data is already somewhat sorted, like it is here insertionSort(T, items[buffer2.start..buffer2.end], lessThan); pull_index = 0; while (pull_index < 2) : (pull_index += 1) { var unique = pull[pull_index].count * 2; if (pull[pull_index].from > pull[pull_index].to) { // the values were pulled out to the left, so redistribute them back to the right var buffer = Range.init(pull[pull_index].range.start, pull[pull_index].range.start + pull[pull_index].count); while (buffer.length() > 0) { index = findFirstForward(T, items, items[buffer.start], Range.init(buffer.end, pull[pull_index].range.end), lessThan, unique); const amount = index - buffer.end; mem.rotate(T, items[buffer.start..index], buffer.length()); buffer.start += (amount + 1); buffer.end += amount; unique -= 2; } } else if (pull[pull_index].from < pull[pull_index].to) { // the values were pulled out to the right, so redistribute them back to the left var buffer = Range.init(pull[pull_index].range.end - pull[pull_index].count, pull[pull_index].range.end); while (buffer.length() > 0) { index = findLastBackward(T, items, items[buffer.end - 1], Range.init(pull[pull_index].range.start, buffer.start), lessThan, unique); const amount = buffer.start - index; mem.rotate(T, items[index..buffer.end], amount); buffer.start -= amount; buffer.end -= (amount + 1); unique -= 2; } } } } // double the size of each A and B subarray that will be merged in the next level if (!iterator.nextLevel()) break; } } // merge operation without a buffer fn mergeInPlace(comptime T: type, items: []T, A_arg: Range, B_arg: Range, lessThan: fn (T, T) bool) void { if (A_arg.length() == 0 or B_arg.length() == 0) return; // this just repeatedly binary searches into B and rotates A into position. // the paper suggests using the 'rotation-based Hwang and Lin algorithm' here, // but I decided to stick with this because it had better situational performance // // (Hwang and Lin is designed for merging subarrays of very different sizes, // but WikiSort almost always uses subarrays that are roughly the same size) // // normally this is incredibly suboptimal, but this function is only called // when none of the A or B blocks in any subarray contained 2√A unique values, // which places a hard limit on the number of times this will ACTUALLY need // to binary search and rotate. // // according to my analysis the worst case is √A rotations performed on √A items // once the constant factors are removed, which ends up being O(n) // // again, this is NOT a general-purpose solution – it only works well in this case! // kind of like how the O(n^2) insertion sort is used in some places var A = A_arg; var B = B_arg; while (true) { // find the first place in B where the first item in A needs to be inserted const mid = binaryFirst(T, items, items[A.start], B, lessThan); // rotate A into place const amount = mid - A.end; mem.rotate(T, items[A.start..mid], A.length()); if (B.end == mid) break; // calculate the new A and B ranges B.start = mid; A = Range.init(A.start + amount, B.start); A.start = binaryLast(T, items, items[A.start], A, lessThan); if (A.length() == 0) break; } } // merge operation using an internal buffer fn mergeInternal(comptime T: type, items: []T, A: Range, B: Range, lessThan: fn (T, T) bool, buffer: Range) void { // whenever we find a value to add to the final array, swap it with the value that's already in that spot // when this algorithm is finished, 'buffer' will contain its original contents, but in a different order var A_count: usize = 0; var B_count: usize = 0; var insert: usize = 0; if (B.length() > 0 and A.length() > 0) { while (true) { if (!lessThan(items[B.start + B_count], items[buffer.start + A_count])) { mem.swap(T, &items[A.start + insert], &items[buffer.start + A_count]); A_count += 1; insert += 1; if (A_count >= A.length()) break; } else { mem.swap(T, &items[A.start + insert], &items[B.start + B_count]); B_count += 1; insert += 1; if (B_count >= B.length()) break; } } } // swap the remainder of A into the final array blockSwap(T, items, buffer.start + A_count, A.start + insert, A.length() - A_count); } fn blockSwap(comptime T: type, items: []T, start1: usize, start2: usize, block_size: usize) void { var index: usize = 0; while (index < block_size) : (index += 1) { mem.swap(T, &items[start1 + index], &items[start2 + index]); } } // combine a linear search with a binary search to reduce the number of comparisons in situations // where have some idea as to how many unique values there are and where the next value might be fn findFirstForward(comptime T: type, items: []T, value: T, range: Range, lessThan: fn (T, T) bool, unique: usize) usize { if (range.length() == 0) return range.start; const skip = math.max(range.length() / unique, @as(usize, 1)); var index = range.start + skip; while (lessThan(items[index - 1], value)) : (index += skip) { if (index >= range.end - skip) { return binaryFirst(T, items, value, Range.init(index, range.end), lessThan); } } return binaryFirst(T, items, value, Range.init(index - skip, index), lessThan); } fn findFirstBackward(comptime T: type, items: []T, value: T, range: Range, lessThan: fn (T, T) bool, unique: usize) usize { if (range.length() == 0) return range.start; const skip = math.max(range.length() / unique, @as(usize, 1)); var index = range.end - skip; while (index > range.start and !lessThan(items[index - 1], value)) : (index -= skip) { if (index < range.start + skip) { return binaryFirst(T, items, value, Range.init(range.start, index), lessThan); } } return binaryFirst(T, items, value, Range.init(index, index + skip), lessThan); } fn findLastForward(comptime T: type, items: []T, value: T, range: Range, lessThan: fn (T, T) bool, unique: usize) usize { if (range.length() == 0) return range.start; const skip = math.max(range.length() / unique, @as(usize, 1)); var index = range.start + skip; while (!lessThan(value, items[index - 1])) : (index += skip) { if (index >= range.end - skip) { return binaryLast(T, items, value, Range.init(index, range.end), lessThan); } } return binaryLast(T, items, value, Range.init(index - skip, index), lessThan); } fn findLastBackward(comptime T: type, items: []T, value: T, range: Range, lessThan: fn (T, T) bool, unique: usize) usize { if (range.length() == 0) return range.start; const skip = math.max(range.length() / unique, @as(usize, 1)); var index = range.end - skip; while (index > range.start and lessThan(value, items[index - 1])) : (index -= skip) { if (index < range.start + skip) { return binaryLast(T, items, value, Range.init(range.start, index), lessThan); } } return binaryLast(T, items, value, Range.init(index, index + skip), lessThan); } fn binaryFirst(comptime T: type, items: []T, value: T, range: Range, lessThan: fn (T, T) bool) usize { var start = range.start; var end = range.end - 1; if (range.start >= range.end) return range.end; while (start < end) { const mid = start + (end - start) / 2; if (lessThan(items[mid], value)) { start = mid + 1; } else { end = mid; } } if (start == range.end - 1 and lessThan(items[start], value)) { start += 1; } return start; } fn binaryLast(comptime T: type, items: []T, value: T, range: Range, lessThan: fn (T, T) bool) usize { var start = range.start; var end = range.end - 1; if (range.start >= range.end) return range.end; while (start < end) { const mid = start + (end - start) / 2; if (!lessThan(value, items[mid])) { start = mid + 1; } else { end = mid; } } if (start == range.end - 1 and !lessThan(value, items[start])) { start += 1; } return start; } fn mergeInto(comptime T: type, from: []T, A: Range, B: Range, lessThan: fn (T, T) bool, into: []T) void { var A_index: usize = A.start; var B_index: usize = B.start; const A_last = A.end; const B_last = B.end; var insert_index: usize = 0; while (true) { if (!lessThan(from[B_index], from[A_index])) { into[insert_index] = from[A_index]; A_index += 1; insert_index += 1; if (A_index == A_last) { // copy the remainder of B into the final array mem.copy(T, into[insert_index..], from[B_index..B_last]); break; } } else { into[insert_index] = from[B_index]; B_index += 1; insert_index += 1; if (B_index == B_last) { // copy the remainder of A into the final array mem.copy(T, into[insert_index..], from[A_index..A_last]); break; } } } } fn mergeExternal(comptime T: type, items: []T, A: Range, B: Range, lessThan: fn (T, T) bool, cache: []T) void { // A fits into the cache, so use that instead of the internal buffer var A_index: usize = 0; var B_index: usize = B.start; var insert_index: usize = A.start; const A_last = A.length(); const B_last = B.end; if (B.length() > 0 and A.length() > 0) { while (true) { if (!lessThan(items[B_index], cache[A_index])) { items[insert_index] = cache[A_index]; A_index += 1; insert_index += 1; if (A_index == A_last) break; } else { items[insert_index] = items[B_index]; B_index += 1; insert_index += 1; if (B_index == B_last) break; } } } // copy the remainder of A into the final array mem.copy(T, items[insert_index..], cache[A_index..A_last]); } fn swap(comptime T: type, items: []T, lessThan: fn (lhs: T, rhs: T) bool, order: *[8]u8, x: usize, y: usize) void { if (lessThan(items[y], items[x]) or ((order.*)[x] > (order.*)[y] and !lessThan(items[x], items[y]))) { mem.swap(T, &items[x], &items[y]); mem.swap(u8, &(order.*)[x], &(order.*)[y]); } } // Use these to generate a comparator function for a given type. e.g. `sort(u8, slice, asc(u8))`. pub fn asc(comptime T: type) fn (T, T) bool { const impl = struct { fn inner(a: T, b: T) bool { return a < b; } }; return impl.inner; } pub fn desc(comptime T: type) fn (T, T) bool { const impl = struct { fn inner(a: T, b: T) bool { return a > b; } }; return impl.inner; } test "stable sort" { testStableSort(); comptime testStableSort(); } fn testStableSort() void { var expected = [_]IdAndValue{ IdAndValue{ .id = 0, .value = 0 }, IdAndValue{ .id = 1, .value = 0 }, IdAndValue{ .id = 2, .value = 0 }, IdAndValue{ .id = 0, .value = 1 }, IdAndValue{ .id = 1, .value = 1 }, IdAndValue{ .id = 2, .value = 1 }, IdAndValue{ .id = 0, .value = 2 }, IdAndValue{ .id = 1, .value = 2 }, IdAndValue{ .id = 2, .value = 2 }, }; var cases = [_][9]IdAndValue{ [_]IdAndValue{ IdAndValue{ .id = 0, .value = 0 }, IdAndValue{ .id = 0, .value = 1 }, IdAndValue{ .id = 0, .value = 2 }, IdAndValue{ .id = 1, .value = 0 }, IdAndValue{ .id = 1, .value = 1 }, IdAndValue{ .id = 1, .value = 2 }, IdAndValue{ .id = 2, .value = 0 }, IdAndValue{ .id = 2, .value = 1 }, IdAndValue{ .id = 2, .value = 2 }, }, [_]IdAndValue{ IdAndValue{ .id = 0, .value = 2 }, IdAndValue{ .id = 0, .value = 1 }, IdAndValue{ .id = 0, .value = 0 }, IdAndValue{ .id = 1, .value = 2 }, IdAndValue{ .id = 1, .value = 1 }, IdAndValue{ .id = 1, .value = 0 }, IdAndValue{ .id = 2, .value = 2 }, IdAndValue{ .id = 2, .value = 1 }, IdAndValue{ .id = 2, .value = 0 }, }, }; for (cases) |*case| { insertionSort(IdAndValue, (case.*)[0..], cmpByValue); for (case.*) |item, i| { testing.expect(item.id == expected[i].id); testing.expect(item.value == expected[i].value); } } } const IdAndValue = struct { id: usize, value: i32, }; fn cmpByValue(a: IdAndValue, b: IdAndValue) bool { return asc(i32)(a.value, b.value); } test "std.sort" { const u8cases = [_][]const []const u8{ [_][]const u8{ "", "", }, [_][]const u8{ "a", "a", }, [_][]const u8{ "az", "az", }, [_][]const u8{ "za", "az", }, [_][]const u8{ "asdf", "adfs", }, [_][]const u8{ "one", "eno", }, }; for (u8cases) |case| { var buf: [8]u8 = undefined; const slice = buf[0..case[0].len]; mem.copy(u8, slice, case[0]); sort(u8, slice, asc(u8)); testing.expect(mem.eql(u8, slice, case[1])); } const i32cases = [_][]const []const i32{ [_][]const i32{ [_]i32{}, [_]i32{}, }, [_][]const i32{ [_]i32{1}, [_]i32{1}, }, [_][]const i32{ [_]i32{ 0, 1 }, [_]i32{ 0, 1 }, }, [_][]const i32{ [_]i32{ 1, 0 }, [_]i32{ 0, 1 }, }, [_][]const i32{ [_]i32{ 1, -1, 0 }, [_]i32{ -1, 0, 1 }, }, [_][]const i32{ [_]i32{ 2, 1, 3 }, [_]i32{ 1, 2, 3 }, }, }; for (i32cases) |case| { var buf: [8]i32 = undefined; const slice = buf[0..case[0].len]; mem.copy(i32, slice, case[0]); sort(i32, slice, asc(i32)); testing.expect(mem.eql(i32, slice, case[1])); } } test "std.sort descending" { const rev_cases = [_][]const []const i32{ [_][]const i32{ [_]i32{}, [_]i32{}, }, [_][]const i32{ [_]i32{1}, [_]i32{1}, }, [_][]const i32{ [_]i32{ 0, 1 }, [_]i32{ 1, 0 }, }, [_][]const i32{ [_]i32{ 1, 0 }, [_]i32{ 1, 0 }, }, [_][]const i32{ [_]i32{ 1, -1, 0 }, [_]i32{ 1, 0, -1 }, }, [_][]const i32{ [_]i32{ 2, 1, 3 }, [_]i32{ 3, 2, 1 }, }, }; for (rev_cases) |case| { var buf: [8]i32 = undefined; const slice = buf[0..case[0].len]; mem.copy(i32, slice, case[0]); sort(i32, slice, desc(i32)); testing.expect(mem.eql(i32, slice, case[1])); } } test "another sort case" { var arr = [_]i32{ 5, 3, 1, 2, 4 }; sort(i32, arr[0..], asc(i32)); testing.expect(mem.eql(i32, arr, [_]i32{ 1, 2, 3, 4, 5 })); } test "sort fuzz testing" { var prng = std.rand.DefaultPrng.init(0x12345678); const test_case_count = 10; var i: usize = 0; while (i < test_case_count) : (i += 1) { fuzzTest(&prng.random); } } var fixed_buffer_mem: [100 * 1024]u8 = undefined; fn fuzzTest(rng: *std.rand.Random) void { const array_size = rng.range(usize, 0, 1000); var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable; // populate with random data for (array) |*item, index| { item.id = index; item.value = rng.range(i32, 0, 100); } sort(IdAndValue, array, cmpByValue); var index: usize = 1; while (index < array.len) : (index += 1) { if (array[index].value == array[index - 1].value) { testing.expect(array[index].id > array[index - 1].id); } else { testing.expect(array[index].value > array[index - 1].value); } } } pub fn min(comptime T: type, items: []T, lessThan: fn (lhs: T, rhs: T) bool) T { var i: usize = 0; var smallest = items[0]; for (items[1..]) |item| { if (lessThan(item, smallest)) { smallest = item; } } return smallest; } pub fn max(comptime T: type, items: []T, lessThan: fn (lhs: T, rhs: T) bool) T { var i: usize = 0; var biggest = items[0]; for (items[1..]) |item| { if (lessThan(biggest, item)) { biggest = item; } } return biggest; }
const foo = @compileError("aoeu",); const bar = baz + foo; const baz = 1; export fn entry() i32 { return bar; } // error // backend=stage1 // target=native // // tmp.zig:1:13: error: aoeu
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2020 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const __divti3 = @import("divti3.zig").__divti3; const testing = @import("std").testing; fn test__divti3(a: i128, b: i128, expected: i128) void { const x = __divti3(a, b); testing.expect(x == expected); } test "divti3" { test__divti3(0, 1, 0); test__divti3(0, -1, 0); test__divti3(2, 1, 2); test__divti3(2, -1, -2); test__divti3(-2, 1, -2); test__divti3(-2, -1, 2); test__divti3(@bitCast(i128, @as(u128, 0x8 << 124)), 1, @bitCast(i128, @as(u128, 0x8 << 124))); test__divti3(@bitCast(i128, @as(u128, 0x8 << 124)), -1, @bitCast(i128, @as(u128, 0x8 << 124))); test__divti3(@bitCast(i128, @as(u128, 0x8 << 124)), -2, @bitCast(i128, @as(u128, 0x4 << 124))); test__divti3(@bitCast(i128, @as(u128, 0x8 << 124)), 2, @bitCast(i128, @as(u128, 0xc << 124))); }
fn main() void { var bad: f128 = 0x0.0_p1; _ = bad; } // invalid underscore placement in float literal - 14 // // tmp.zig:2:21: error: expected expression, found 'invalid bytes' // tmp.zig:2:27: note: invalid byte: 'p'
const std = @import("std"); const mem = std.mem; const Poly1305 = std.crypto.onetimeauth.Poly1305; const Chacha20IETF = std.crypto.stream.chacha.ChaCha20IETF; // TODO See stdlib, this is a modified non vectorized implementation pub const ChaCha20Stream = struct { const math = std.math; pub const BlockVec = [16]u32; pub fn initContext(key: [8]u32, d: [4]u32) BlockVec { const c = "expand 32-byte k"; const constant_le = comptime [4]u32{ mem.readIntLittle(u32, c[0..4]), mem.readIntLittle(u32, c[4..8]), mem.readIntLittle(u32, c[8..12]), mem.readIntLittle(u32, c[12..16]), }; return BlockVec{ constant_le[0], constant_le[1], constant_le[2], constant_le[3], key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7], d[0], d[1], d[2], d[3], }; } const QuarterRound = struct { a: usize, b: usize, c: usize, d: usize, }; fn Rp(a: usize, b: usize, c: usize, d: usize) QuarterRound { return QuarterRound{ .a = a, .b = b, .c = c, .d = d, }; } inline fn chacha20Core(x: *BlockVec, input: BlockVec) void { x.* = input; const rounds = comptime [_]QuarterRound{ Rp(0, 4, 8, 12), Rp(1, 5, 9, 13), Rp(2, 6, 10, 14), Rp(3, 7, 11, 15), Rp(0, 5, 10, 15), Rp(1, 6, 11, 12), Rp(2, 7, 8, 13), Rp(3, 4, 9, 14), }; comptime var j: usize = 0; inline while (j < 20) : (j += 2) { inline for (rounds) |r| { x[r.a] +%= x[r.b]; x[r.d] = math.rotl(u32, x[r.d] ^ x[r.a], @as(u32, 16)); x[r.c] +%= x[r.d]; x[r.b] = math.rotl(u32, x[r.b] ^ x[r.c], @as(u32, 12)); x[r.a] +%= x[r.b]; x[r.d] = math.rotl(u32, x[r.d] ^ x[r.a], @as(u32, 8)); x[r.c] +%= x[r.d]; x[r.b] = math.rotl(u32, x[r.b] ^ x[r.c], @as(u32, 7)); } } } inline fn hashToBytes(out: *[64]u8, x: BlockVec) void { var i: usize = 0; while (i < 4) : (i += 1) { mem.writeIntLittle(u32, out[16 * i + 0 ..][0..4], x[i * 4 + 0]); mem.writeIntLittle(u32, out[16 * i + 4 ..][0..4], x[i * 4 + 1]); mem.writeIntLittle(u32, out[16 * i + 8 ..][0..4], x[i * 4 + 2]); mem.writeIntLittle(u32, out[16 * i + 12 ..][0..4], x[i * 4 + 3]); } } inline fn contextFeedback(x: *BlockVec, ctx: BlockVec) void { var i: usize = 0; while (i < 16) : (i += 1) { x[i] +%= ctx[i]; } } pub fn initPoly1305(key: [32]u8, nonce: [12]u8, ad: [13]u8) Poly1305 { var polyKey = [_]u8{0} ** 32; Chacha20IETF.xor(&polyKey, &polyKey, 0, key, nonce); var mac = Poly1305.init(&polyKey); mac.update(&ad); // Pad to 16 bytes from ad mac.update(&.{ 0, 0, 0 }); return mac; } /// Call after `mac` has been updated with the whole message pub fn checkPoly1305(mac: *Poly1305, len: usize, tag: [16]u8) !void { if (len % 16 != 0) { const zeros = [_]u8{0} ** 16; const padding = 16 - (len % 16); mac.update(zeros[0..padding]); } var lens: [16]u8 = undefined; mem.writeIntLittle(u64, lens[0..8], 13); mem.writeIntLittle(u64, lens[8..16], len); mac.update(lens[0..]); var computedTag: [16]u8 = undefined; mac.final(computedTag[0..]); var acc: u8 = 0; for (computedTag) |_, i| { acc |= computedTag[i] ^ tag[i]; } if (acc != 0) { return error.AuthenticationFailed; } } // TODO: Optimize this pub fn chacha20Xor(out: []u8, in: []const u8, key: [8]u32, ctx: *BlockVec, idx: *usize, buf: *[64]u8) void { _ = key; var x: BlockVec = undefined; var i: usize = 0; while (i < in.len) { if (idx.* % 64 == 0) { if (idx.* != 0) { ctx.*[12] += 1; } chacha20Core(x[0..], ctx.*); contextFeedback(&x, ctx.*); hashToBytes(buf, x); } out[i] = in[i] ^ buf[idx.* % 64]; i += 1; idx.* += 1; } } }; pub fn keyToWords(key: [32]u8) [8]u32 { var k: [8]u32 = undefined; var i: usize = 0; while (i < 8) : (i += 1) { k[i] = mem.readIntLittle(u32, key[i * 4 ..][0..4]); } return k; } // See std.crypto.core.modes.ctr /// This mode creates a key stream by encrypting an incrementing counter using a block cipher, and adding it to the source material. pub fn ctr( comptime BlockCipher: anytype, block_cipher: BlockCipher, dst: []u8, src: []const u8, counterInt: *u128, idx: *usize, endian: std.builtin.Endian, ) void { std.debug.assert(dst.len >= src.len); const block_length = BlockCipher.block_length; var cur_idx: usize = 0; const offset = idx.* % block_length; if (offset != 0) { const part_len = std.math.min(block_length - offset, src.len); var counter: [BlockCipher.block_length]u8 = undefined; mem.writeInt(u128, &counter, counterInt.*, endian); var pad = [_]u8{0} ** block_length; mem.copy(u8, pad[offset..], src[0..part_len]); block_cipher.xor(&pad, &pad, counter); mem.copy(u8, dst[0..part_len], pad[offset..][0..part_len]); cur_idx += part_len; idx.* += part_len; if (idx.* % block_length == 0) counterInt.* += 1; } const start_idx = cur_idx; const remaining = src.len - cur_idx; cur_idx = 0; const parallel_count = BlockCipher.block.parallel.optimal_parallel_blocks; const wide_block_length = parallel_count * 16; if (remaining >= wide_block_length) { var counters: [parallel_count * 16]u8 = undefined; while (cur_idx + wide_block_length <= remaining) : (cur_idx += wide_block_length) { comptime var j = 0; inline while (j < parallel_count) : (j += 1) { mem.writeInt(u128, counters[j * 16 .. j * 16 + 16], counterInt.*, endian); counterInt.* +%= 1; } block_cipher.xorWide(parallel_count, dst[start_idx..][cur_idx .. cur_idx + wide_block_length][0..wide_block_length], src[start_idx..][cur_idx .. cur_idx + wide_block_length][0..wide_block_length], counters); idx.* += wide_block_length; } } while (cur_idx + block_length <= remaining) : (cur_idx += block_length) { var counter: [BlockCipher.block_length]u8 = undefined; mem.writeInt(u128, &counter, counterInt.*, endian); counterInt.* +%= 1; block_cipher.xor(dst[start_idx..][cur_idx .. cur_idx + block_length][0..block_length], src[start_idx..][cur_idx .. cur_idx + block_length][0..block_length], counter); idx.* += block_length; } if (cur_idx < remaining) { std.debug.assert(idx.* % block_length == 0); var counter: [BlockCipher.block_length]u8 = undefined; mem.writeInt(u128, &counter, counterInt.*, endian); var pad = [_]u8{0} ** block_length; mem.copy(u8, &pad, src[start_idx..][cur_idx..]); block_cipher.xor(&pad, &pad, counter); mem.copy(u8, dst[start_idx..][cur_idx..], pad[0 .. remaining - cur_idx]); idx.* += remaining - cur_idx; if (idx.* % block_length == 0) counterInt.* +%= 1; } } // Ported from BearSSL's ec_prime_i31 engine pub const ecc = struct { pub const SECP384R1 = struct { pub const point_len = 96; const order = [point_len / 2]u8{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC7, 0x63, 0x4D, 0x81, 0xF4, 0x37, 0x2D, 0xDF, 0x58, 0x1A, 0x0D, 0xB2, 0x48, 0xB0, 0xA7, 0x7A, 0xEC, 0xEC, 0x19, 0x6A, 0xCC, 0xC5, 0x29, 0x73, }; const P = [_]u32{ 0x0000018C, 0x7FFFFFFF, 0x00000001, 0x00000000, 0x7FFFFFF8, 0x7FFFFFEF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x00000FFF, }; const R2 = [_]u32{ 0x0000018C, 0x00000000, 0x00000080, 0x7FFFFE00, 0x000001FF, 0x00000800, 0x00000000, 0x7FFFE000, 0x00001FFF, 0x00008000, 0x00008000, 0x00000000, 0x00000000, 0x00000000, }; const B = [_]u32{ 0x0000018C, 0x6E666840, 0x070D0392, 0x5D810231, 0x7651D50C, 0x17E218D6, 0x1B192002, 0x44EFE441, 0x3A524E2B, 0x2719BA5F, 0x41F02209, 0x36C5643E, 0x5813EFFE, 0x000008A5, }; const base_point = [point_len]u8{ 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E, 0xF3, 0x20, 0xAD, 0x74, 0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B, 0x98, 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A, 0x38, 0x55, 0x02, 0xF2, 0x5D, 0xBF, 0x55, 0x29, 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A, 0xB7, 0x36, 0x17, 0xDE, 0x4A, 0x96, 0x26, 0x2C, 0x6F, 0x5D, 0x9E, 0x98, 0xBF, 0x92, 0x92, 0xDC, 0x29, 0xF8, 0xF4, 0x1D, 0xBD, 0x28, 0x9A, 0x14, 0x7C, 0xE9, 0xDA, 0x31, 0x13, 0xB5, 0xF0, 0xB8, 0xC0, 0x0A, 0x60, 0xB1, 0xCE, 0x1D, 0x7E, 0x81, 0x9D, 0x7A, 0x43, 0x1D, 0x7C, 0x90, 0xEA, 0x0E, 0x5F, }; comptime { std.debug.assert((P[0] - (P[0] >> 5) + 7) >> 2 == point_len + 1); } }; pub const SECP256R1 = struct { pub const point_len = 64; const order = [point_len / 2]u8{ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, }; const P = [_]u32{ 0x00000108, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x00000007, 0x00000000, 0x00000000, 0x00000040, 0x7FFFFF80, 0x000000FF, }; const R2 = [_]u32{ 0x00000108, 0x00014000, 0x00018000, 0x00000000, 0x7FF40000, 0x7FEFFFFF, 0x7FF7FFFF, 0x7FAFFFFF, 0x005FFFFF, 0x00000000, }; const B = [_]u32{ 0x00000108, 0x6FEE1803, 0x6229C4BD, 0x21B139BE, 0x327150AA, 0x3567802E, 0x3F7212ED, 0x012E4355, 0x782DD38D, 0x0000000E, }; const base_point = [point_len]u8{ 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5, }; comptime { std.debug.assert((P[0] - (P[0] >> 5) + 7) >> 2 == point_len + 1); } }; fn jacobian_len(comptime Curve: type) usize { return @divTrunc(Curve.order.len * 8 + 61, 31); } fn Jacobian(comptime Curve: type) type { return [3][jacobian_len(Curve)]u32; } fn zero_jacobian(comptime Curve: type) Jacobian(Curve) { var result = std.mem.zeroes(Jacobian(Curve)); result[0][0] = Curve.P[0]; result[1][0] = Curve.P[0]; result[2][0] = Curve.P[0]; return result; } pub fn scalarmult( comptime Curve: type, point: [Curve.point_len]u8, k: []const u8, ) ![Curve.point_len]u8 { var P: Jacobian(Curve) = undefined; var res: u32 = decode_to_jacobian(Curve, &P, point); point_mul(Curve, &P, k); var out: [Curve.point_len]u8 = undefined; encode_from_jacobian(Curve, &out, P); if (res == 0) return error.MultiplicationFailed; return out; } pub fn KeyPair(comptime Curve: type) type { return struct { public_key: [Curve.point_len]u8, secret_key: [Curve.point_len / 2]u8, }; } pub fn make_key_pair(comptime Curve: type, rand_bytes: [Curve.point_len / 2]u8) KeyPair(Curve) { var key_bytes = rand_bytes; comptime var mask: u8 = 0xFF; comptime { while (mask >= Curve.order[0]) { mask >>= 1; } } key_bytes[0] &= mask; key_bytes[Curve.point_len / 2 - 1] |= 0x01; return .{ .secret_key = key_bytes, .public_key = scalarmult(Curve, Curve.base_point, &key_bytes) catch unreachable, }; } fn jacobian_with_one_set(comptime Curve: type, comptime fields: [2][jacobian_len(Curve)]u32) Jacobian(Curve) { const plen = comptime (Curve.P[0] + 63) >> 5; return fields ++ [1][jacobian_len(Curve)]u32{ [2]u32{ Curve.P[0], 1 } ++ ([1]u32{0} ** (plen - 2)), }; } fn encode_from_jacobian(comptime Curve: type, point: *[Curve.point_len]u8, P: Jacobian(Curve)) void { var Q = P; const T = comptime jacobian_with_one_set(Curve, [2][jacobian_len(Curve)]u32{ undefined, undefined }); _ = run_code(Curve, &Q, T, &code.affine); encode_jacobian_part(point[0 .. Curve.point_len / 2], &Q[0]); encode_jacobian_part(point[Curve.point_len / 2 ..], &Q[1]); } fn point_mul(comptime Curve: type, P: *Jacobian(Curve), x: []const u8) void { var P2 = P.*; point_double(Curve, &P2); var P3 = P.*; point_add(Curve, &P3, P2); var Q = zero_jacobian(Curve); var qz: u32 = 1; var xlen = x.len; var xidx: usize = 0; while (xlen > 0) : ({ xlen -= 1; xidx += 1; }) { var k: u3 = 6; while (true) : (k -= 2) { point_double(Curve, &Q); point_double(Curve, &Q); var T = P.*; var U = Q; const bits = @as(u32, x[xidx] >> k) & 3; const bnz = NEQ(bits, 0); CCOPY(EQ(bits, 2), mem.asBytes(&T), mem.asBytes(&P2)); CCOPY(EQ(bits, 3), mem.asBytes(&T), mem.asBytes(&P3)); point_add(Curve, &U, T); CCOPY(bnz & qz, mem.asBytes(&Q), mem.asBytes(&T)); CCOPY(bnz & ~qz, mem.asBytes(&Q), mem.asBytes(&U)); qz &= ~bnz; if (k == 0) break; } } P.* = Q; } inline fn point_double(comptime Curve: type, P: *Jacobian(Curve)) void { _ = run_code(Curve, P, P.*, &code.double); } inline fn point_add(comptime Curve: type, P1: *Jacobian(Curve), P2: Jacobian(Curve)) void { _ = run_code(Curve, P1, P2, &code._add); } fn decode_to_jacobian( comptime Curve: type, out: *Jacobian(Curve), point: [Curve.point_len]u8, ) u32 { out.* = zero_jacobian(Curve); var result = decode_mod(Curve, &out.*[0], point[0 .. Curve.point_len / 2].*); result &= decode_mod(Curve, &out.*[1], point[Curve.point_len / 2 ..].*); const zlen = comptime ((Curve.P[0] + 63) >> 5); comptime std.debug.assert(zlen == @typeInfo(@TypeOf(Curve.R2)).Array.len); comptime std.debug.assert(zlen == @typeInfo(@TypeOf(Curve.B)).Array.len); const Q = comptime jacobian_with_one_set(Curve, [2][jacobian_len(Curve)]u32{ Curve.R2, Curve.B }); result &= ~run_code(Curve, out, Q, &code.check); return result; } const code = struct { const P1x = 0; const P1y = 1; const P1z = 2; const P2x = 3; const P2y = 4; const P2z = 5; const Px = 0; const Py = 1; const Pz = 2; const t1 = 6; const t2 = 7; const t3 = 8; const t4 = 9; const t5 = 10; const t6 = 11; const t7 = 12; const t8 = 3; const t9 = 4; const t10 = 5; fn MSET(comptime d: u16, comptime a: u16) u16 { return 0x0000 + (d << 8) + (a << 4); } fn MADD(comptime d: u16, comptime a: u16) u16 { return 0x1000 + (d << 8) + (a << 4); } fn MSUB(comptime d: u16, comptime a: u16) u16 { return 0x2000 + (d << 8) + (a << 4); } fn MMUL(comptime d: u16, comptime a: u16, comptime b: u16) u16 { return 0x3000 + (d << 8) + (a << 4) + b; } fn MINV(comptime d: u16, comptime a: u16, comptime b: u16) u16 { return 0x4000 + (d << 8) + (a << 4) + b; } fn MTZ(comptime d: u16) u16 { return 0x5000 + (d << 8); } const ENDCODE = 0; const check = [_]u16{ // Convert x and y to Montgomery representation. MMUL(t1, P1x, P2x), MMUL(t2, P1y, P2x), MSET(P1x, t1), MSET(P1y, t2), // Compute x^3 in t1. MMUL(t2, P1x, P1x), MMUL(t1, P1x, t2), // Subtract 3*x from t1. MSUB(t1, P1x), MSUB(t1, P1x), MSUB(t1, P1x), // Add b. MADD(t1, P2y), // Compute y^2 in t2. MMUL(t2, P1y, P1y), // Compare y^2 with x^3 - 3*x + b; they must match. MSUB(t1, t2), MTZ(t1), // Set z to 1 (in Montgomery representation). MMUL(P1z, P2x, P2z), ENDCODE, }; const double = [_]u16{ // Compute z^2 (in t1). MMUL(t1, Pz, Pz), // Compute x-z^2 (in t2) and then x+z^2 (in t1). MSET(t2, Px), MSUB(t2, t1), MADD(t1, Px), // Compute m = 3*(x+z^2)*(x-z^2) (in t1). MMUL(t3, t1, t2), MSET(t1, t3), MADD(t1, t3), MADD(t1, t3), // Compute s = 4*x*y^2 (in t2) and 2*y^2 (in t3). MMUL(t3, Py, Py), MADD(t3, t3), MMUL(t2, Px, t3), MADD(t2, t2), // Compute x' = m^2 - 2*s. MMUL(Px, t1, t1), MSUB(Px, t2), MSUB(Px, t2), // Compute z' = 2*y*z. MMUL(t4, Py, Pz), MSET(Pz, t4), MADD(Pz, t4), // Compute y' = m*(s - x') - 8*y^4. Note that we already have // 2*y^2 in t3. MSUB(t2, Px), MMUL(Py, t1, t2), MMUL(t4, t3, t3), MSUB(Py, t4), MSUB(Py, t4), ENDCODE, }; const _add = [_]u16{ // Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3). MMUL(t3, P2z, P2z), MMUL(t1, P1x, t3), MMUL(t4, P2z, t3), MMUL(t3, P1y, t4), // Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4). MMUL(t4, P1z, P1z), MMUL(t2, P2x, t4), MMUL(t5, P1z, t4), MMUL(t4, P2y, t5), //Compute h = u2 - u1 (in t2) and r = s2 - s1 (in t4). MSUB(t2, t1), MSUB(t4, t3), // Report cases where r = 0 through the returned flag. MTZ(t4), // Compute u1*h^2 (in t6) and h^3 (in t5). MMUL(t7, t2, t2), MMUL(t6, t1, t7), MMUL(t5, t7, t2), // Compute x3 = r^2 - h^3 - 2*u1*h^2. // t1 and t7 can be used as scratch registers. MMUL(P1x, t4, t4), MSUB(P1x, t5), MSUB(P1x, t6), MSUB(P1x, t6), //Compute y3 = r*(u1*h^2 - x3) - s1*h^3. MSUB(t6, P1x), MMUL(P1y, t4, t6), MMUL(t1, t5, t3), MSUB(P1y, t1), //Compute z3 = h*z1*z2. MMUL(t1, P1z, P2z), MMUL(P1z, t1, t2), ENDCODE, }; const affine = [_]u16{ // Save z*R in t1. MSET(t1, P1z), // Compute z^3 in t2. MMUL(t2, P1z, P1z), MMUL(t3, P1z, t2), MMUL(t2, t3, P2z), // Invert to (1/z^3) in t2. MINV(t2, t3, t4), // Compute y. MSET(t3, P1y), MMUL(P1y, t2, t3), // Compute (1/z^2) in t3. MMUL(t3, t2, t1), // Compute x. MSET(t2, P1x), MMUL(P1x, t2, t3), ENDCODE, }; }; fn decode_mod( comptime Curve: type, x: *[jacobian_len(Curve)]u32, src: [Curve.point_len / 2]u8, ) u32 { const mlen = comptime ((Curve.P[0] + 31) >> 5); const tlen = comptime std.math.max(mlen << 2, Curve.point_len / 2) + 4; var r: u32 = 0; var pass: usize = 0; while (pass < 2) : (pass += 1) { var v: usize = 1; var acc: u32 = 0; var acc_len: u32 = 0; var u: usize = 0; while (u < tlen) : (u += 1) { const b = if (u < Curve.point_len / 2) @as(u32, src[Curve.point_len / 2 - 1 - u]) else 0; acc |= b << @truncate(u5, acc_len); acc_len += 8; if (acc_len >= 31) { const xw = acc & 0x7FFFFFFF; acc_len -= 31; acc = b >> @truncate(u5, 8 - acc_len); if (v <= mlen) { if (pass != 0) { x[v] = r & xw; } else { const cc = @bitCast(u32, CMP(xw, Curve.P[v])); r = MUX(EQ(cc, 0), r, cc); } } else if (pass == 0) { r = MUX(EQ(xw, 0), r, 1); } v += 1; } } r >>= 1; r |= (r << 1); } x[0] = Curve.P[0]; return r & 1; } fn run_code( comptime Curve: type, P1: *Jacobian(Curve), P2: Jacobian(Curve), comptime Code: []const u16, ) u32 { const jaclen = comptime jacobian_len(Curve); var t: [13][jaclen]u32 = undefined; var result: u32 = 1; t[0..3].* = P1.*; t[3..6].* = P2; comptime var u: usize = 0; inline while (true) : (u += 1) { comptime var op = Code[u]; if (op == 0) break; const d = comptime (op >> 8) & 0x0F; const a = comptime (op >> 4) & 0x0F; const b = comptime op & 0x0F; op >>= 12; switch (op) { 0 => t[d] = t[a], 1 => { var ctl = add(&t[d], &t[a], 1); ctl |= NOT(sub(&t[d], &Curve.P, 0)); _ = sub(&t[d], &Curve.P, ctl); }, 2 => _ = add(&t[d], &Curve.P, sub(&t[d], &t[a], 1)), 3 => montymul(&t[d], &t[a], &t[b], &Curve.P, 1), 4 => { var tp: [Curve.point_len / 2]u8 = undefined; encode_jacobian_part(&tp, &Curve.P); tp[Curve.point_len / 2 - 1] -= 2; modpow(Curve, &t[d], tp, 1, &t[a], &t[b]); }, else => result &= ~iszero(&t[d]), } } P1.* = t[0..3].*; return result; } inline fn MUL31(x: u32, y: u32) u64 { return @as(u64, x) * @as(u64, y); } inline fn MUL31_lo(x: u32, y: u32) u32 { return (x *% y) & 0x7FFFFFFF; } inline fn MUX(ctl: u32, x: u32, y: u32) u32 { return y ^ (@bitCast(u32, -@bitCast(i32, ctl)) & (x ^ y)); } inline fn NOT(ctl: u32) u32 { return ctl ^ 1; } inline fn NEQ(x: u32, y: u32) u32 { const q = x ^ y; return (q | @bitCast(u32, -@bitCast(i32, q))) >> 31; } inline fn EQ(x: u32, y: u32) u32 { const q = x ^ y; return NOT((q | @bitCast(u32, -@bitCast(i32, q))) >> 31); } inline fn CMP(x: u32, y: u32) i32 { return @bitCast(i32, GT(x, y)) | -@bitCast(i32, GT(y, x)); } inline fn GT(x: u32, y: u32) u32 { const z = y -% x; return (z ^ ((x ^ y) & (x ^ z))) >> 31; } inline fn LT(x: u32, y: u32) u32 { return GT(y, x); } inline fn GE(x: u32, y: u32) u32 { return NOT(GT(y, x)); } fn CCOPY(ctl: u32, dst: []u8, src: []const u8) void { for (src) |s, i| { dst[i] = @truncate(u8, MUX(ctl, s, dst[i])); } } inline fn set_zero(out: [*]u32, bit_len: u32) void { out[0] = bit_len; mem.set(u32, (out + 1)[0 .. (bit_len + 31) >> 5], 0); } fn divrem(_hi: u32, _lo: u32, d: u32, r: *u32) u32 { var hi = _hi; var lo = _lo; var q: u32 = 0; const ch = EQ(hi, d); hi = MUX(ch, 0, hi); var k: u5 = 31; while (k > 0) : (k -= 1) { const j = @truncate(u5, 32 - @as(u6, k)); const w = (hi << j) | (lo >> k); const ctl = GE(w, d) | (hi >> k); const hi2 = (w -% d) >> j; const lo2 = lo -% (d << k); hi = MUX(ctl, hi2, hi); lo = MUX(ctl, lo2, lo); q |= ctl << k; } const cf = GE(lo, d) | hi; q |= cf; r.* = MUX(cf, lo -% d, lo); return q; } inline fn div(hi: u32, lo: u32, d: u32) u32 { var r: u32 = undefined; return divrem(hi, lo, d, &r); } fn muladd_small(x: [*]u32, z: u32, m: [*]const u32) void { var a0: u32 = undefined; var a1: u32 = undefined; var b0: u32 = undefined; const mblr = @intCast(u5, m[0] & 31); const mlen = (m[0] + 31) >> 5; const hi = x[mlen]; if (mblr == 0) { a0 = x[mlen]; mem.copyBackwards(u32, (x + 2)[0 .. mlen - 1], (x + 1)[0 .. mlen - 1]); x[1] = z; a1 = x[mlen]; b0 = m[mlen]; } else { a0 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr)) & 0x7FFFFFFF; mem.copyBackwards(u32, (x + 2)[0 .. mlen - 1], (x + 1)[0 .. mlen - 1]); x[1] = z; a1 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr)) & 0x7FFFFFFF; b0 = ((m[mlen] << (31 - mblr)) | (m[mlen - 1] >> mblr)) & 0x7FFFFFFF; } const g = div(a0 >> 1, a1 | (a0 << 31), b0); const q = MUX(EQ(a0, b0), 0x7FFFFFFF, MUX(EQ(g, 0), 0, g -% 1)); var cc: u32 = 0; var tb: u32 = 1; var u: usize = 1; while (u <= mlen) : (u += 1) { const mw = m[u]; const zl = MUL31(mw, q) + cc; cc = @truncate(u32, zl >> 31); const zw = @truncate(u32, zl) & 0x7FFFFFFF; const xw = x[u]; var nxw = xw -% zw; cc += nxw >> 31; nxw &= 0x7FFFFFFF; x[u] = nxw; tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw)); } const over = GT(cc, hi); const under = ~over & (tb | LT(cc, hi)); _ = add(x, m, over); _ = sub(x, m, under); } fn to_monty(x: [*]u32, m: [*]const u32) void { const mlen = (m[0] + 31) >> 5; var k = mlen; while (k > 0) : (k -= 1) { muladd_small(x, 0, m); } } fn modpow( comptime Curve: type, x: *[jacobian_len(Curve)]u32, e: [Curve.point_len / 2]u8, m0i: u32, t1: *[jacobian_len(Curve)]u32, t2: *[jacobian_len(Curve)]u32, ) void { t1.* = x.*; to_monty(t1, &Curve.P); set_zero(x, Curve.P[0]); x[1] = 1; const bitlen = comptime (Curve.point_len / 2) << 3; var k: usize = 0; while (k < bitlen) : (k += 1) { const ctl = (e[Curve.point_len / 2 - 1 - (k >> 3)] >> (@truncate(u3, k & 7))) & 1; montymul(t2, x, t1, &Curve.P, m0i); CCOPY(ctl, mem.asBytes(x), mem.asBytes(t2)); montymul(t2, t1, t1, &Curve.P, m0i); t1.* = t2.*; } } fn encode_jacobian_part(dst: []u8, x: [*]const u32) void { const xlen = (x[0] + 31) >> 5; var buf = @ptrToInt(dst.ptr) + dst.len; var len: usize = dst.len; var k: usize = 1; var acc: u32 = 0; var acc_len: u5 = 0; while (len != 0) { const w = if (k <= xlen) x[k] else 0; k += 1; if (acc_len == 0) { acc = w; acc_len = 31; } else { const z = acc | (w << acc_len); acc_len -= 1; acc = w >> (31 - acc_len); if (len >= 4) { buf -= 4; len -= 4; mem.writeIntBig(u32, @intToPtr([*]u8, buf)[0..4], z); } else { switch (len) { 3 => { @intToPtr(*u8, buf - 3).* = @truncate(u8, z >> 16); @intToPtr(*u8, buf - 2).* = @truncate(u8, z >> 8); }, 2 => @intToPtr(*u8, buf - 2).* = @truncate(u8, z >> 8), 1 => {}, else => unreachable, } @intToPtr(*u8, buf - 1).* = @truncate(u8, z); return; } } } } fn montymul( out: [*]u32, x: [*]const u32, y: [*]const u32, m: [*]const u32, m0i: u32, ) void { const len = (m[0] + 31) >> 5; const len4 = len & ~@as(usize, 3); set_zero(out, m[0]); var dh: u32 = 0; var u: usize = 0; while (u < len) : (u += 1) { const xu = x[u + 1]; const f = MUL31_lo(out[1] + MUL31_lo(x[u + 1], y[1]), m0i); var r: u64 = 0; var v: usize = 0; while (v < len4) : (v += 4) { comptime var j = 1; inline while (j <= 4) : (j += 1) { const z = out[v + j] +% MUL31(xu, y[v + j]) +% MUL31(f, m[v + j]) +% r; r = z >> 31; out[v + j - 1] = @truncate(u32, z) & 0x7FFFFFFF; } } while (v < len) : (v += 1) { const z = out[v + 1] +% MUL31(xu, y[v + 1]) +% MUL31(f, m[v + 1]) +% r; r = z >> 31; out[v] = @truncate(u32, z) & 0x7FFFFFFF; } dh += @truncate(u32, r); out[len] = dh & 0x7FFFFFFF; dh >>= 31; } out[0] = m[0]; const ctl = NEQ(dh, 0) | NOT(sub(out, m, 0)); _ = sub(out, m, ctl); } fn add(a: [*]u32, b: [*]const u32, ctl: u32) u32 { var u: usize = 1; var cc: u32 = 0; const m = (a[0] + 63) >> 5; while (u < m) : (u += 1) { const aw = a[u]; const bw = b[u]; const naw = aw +% bw +% cc; cc = naw >> 31; a[u] = MUX(ctl, naw & 0x7FFFFFFF, aw); } return cc; } fn sub(a: [*]u32, b: [*]const u32, ctl: u32) u32 { var cc: u32 = 0; const m = (a[0] + 63) >> 5; var u: usize = 1; while (u < m) : (u += 1) { const aw = a[u]; const bw = b[u]; const naw = aw -% bw -% cc; cc = naw >> 31; a[u] = MUX(ctl, naw & 0x7FFFFFFF, aw); } return cc; } fn iszero(arr: [*]const u32) u32 { const mlen = (arr[0] + 63) >> 5; var z: u32 = 0; var u: usize = mlen - 1; while (u > 0) : (u -= 1) { z |= arr[u]; } return ~(z | @bitCast(u32, -@bitCast(i32, z))) >> 31; } }; test "elliptic curve functions with secp384r1 curve" { { // Decode to Jacobian then encode again with no operations var P: ecc.Jacobian(ecc.SECP384R1) = undefined; _ = ecc.decode_to_jacobian(ecc.SECP384R1, &P, ecc.SECP384R1.base_point); var out: [96]u8 = undefined; ecc.encode_from_jacobian(ecc.SECP384R1, &out, P); try std.testing.expectEqual(ecc.SECP384R1.base_point, out); // Multiply by one, check that the result is still the base point mem.set(u8, &out, 0); ecc.point_mul(ecc.SECP384R1, &P, &[1]u8{1}); ecc.encode_from_jacobian(ecc.SECP384R1, &out, P); try std.testing.expectEqual(ecc.SECP384R1.base_point, out); } { // @TODO Remove this once std.crypto.rand works in .evented mode var rand = blk: { var seed: [std.rand.DefaultCsprng.secret_seed_length]u8 = undefined; try std.os.getrandom(&seed); break :blk &std.rand.DefaultCsprng.init(seed).random(); }; // Derive a shared secret from a Diffie-Hellman key exchange var seed: [48]u8 = undefined; rand.bytes(&seed); const kp1 = ecc.make_key_pair(ecc.SECP384R1, seed); rand.bytes(&seed); const kp2 = ecc.make_key_pair(ecc.SECP384R1, seed); const shared1 = try ecc.scalarmult(ecc.SECP384R1, kp1.public_key, &kp2.secret_key); const shared2 = try ecc.scalarmult(ecc.SECP384R1, kp2.public_key, &kp1.secret_key); try std.testing.expectEqual(shared1, shared2); } // @TODO Add tests with known points. }
//! The primary goal of this projeoct is to provide a Zig implementation of the //! Idlspec language, its compiler, vm, and other auxiliary bits and pieces seen //! throughout the proposed ecosystem (such as the markup language and spec language) const std = @import("std"); const mem = std.mem; const token = @import("./lang/token.zig"); const builtin = @import("builtin"); const Cmd = @import("./cli.zig").Cmd; const match = @import("./cli.zig").match; const process = std.process; const fs = std.fs; const ChildProcess = std.ChildProcess; pub const io_mode = .evented; const Parser = @import("./lang/parser.zig").Parser; pub fn main() anyerror!void { var a = std.heap.ArenaAllocator.init(std.heap.page_allocator); var alo = a.allocator(); var args = std.process.args(); var mcmd: Cmd = Cmd.help; _ = args.skip(); while (try args.next(alo)) |arg| { if (Cmd.isCmd(arg)) |cm| { std.debug.print("Cmd: {s}\n", .{arg}); mcmd = cm; } else if (std.mem.startsWith(u8, arg, "--")) { std.debug.print("Opt: {s}\n", .{arg[2..]}); } else if (std.mem.startsWith(u8, arg, "-")) { std.debug.print("Opt: {s}", .{arg[1..]}); } else { continue; } } try mcmd.exe(); // _ = try token.tokFile(alo, "../../res/test.is"); } //// Process system args to get command, subcommand(s) (if applicable), and args/flags/opts /// Provides the global command, subcommand, opts, and flags. pub fn processCmd(a: std.mem.Allocator) !void { const args = std.process.argsAlloc(a); defer std.process.argsFree(a, args); var cmd: ?Cmd = null; var arg_i: usize = 1; const arg = args[arg_i]; // ARG POSITION 1 corresponds to base command // Iterates through each possible pair/quad of command values and breaks if there's a match std.debug.print("\x1b[37;1m CMD \x1b[32;1m]#:{d}\x1b[0m is:\x1b[33;1m {s}\x1b[0m", .{ arg_i, cmd }); const main_cmd = cb: { if (match(arg, "list", "ls")) break :cb Cmd{ .list = null }; if (match(arg, "about", "A")) break :cb Cmd{ .about = null }; if (match(arg, "build", "B")) break :cb Cmd{ .base = null }; if (match(arg, "base", "b")) break :cb Cmd{ .build = null }; if (match(arg, "space", "S")) break :cb Cmd{ .id = null }; if (match(arg, "page", "p")) break :cb Cmd{ .build = null }; if (match(arg, "repl", "R")) break :cb Cmd{ .repl = null }; if (match(arg, "shell", "sh")) break :cb Cmd{ .shell = null }; if (match(arg, "init", "i")) break :cb Cmd{ .init = null }; if (match(arg, "id", "I")) break :cb Cmd{ .id = null }; if (match(arg, "guide", "G")) break :cb Cmd{ .build = null }; if (match(arg, "query", "q")) break :cb Cmd{ .build = null }; if (match(arg, "help", "h") or match(arg, "--help", "-h")) break :cb Cmd{ .build = null }; }; try main_cmd.exe(); } // var o: ?Linke([]String u8) = null; // defer opts.deinit(); // For command two, then get subcommands of chosen command and loop thru those // to find a match. In timee . // while (arg_i < args.len) : (arg_i += 2) { // // First we'll parse boolean valued flags // if (match(a, "--debug", "-d")) { // flags.prepend("verbose"); // } else if (match(arg, "--version", "-v")) { // flags.prepend("version"); // } else if (match(arg, "--private", "-p")) { // flags.prepend("private"); // } else if (match(arg, "--all-bases", "-A")) { // flags.prepend("all-bases"); // } else continue; // Next we'll look through the possible gloobal opt keys // if (match(a, "--base", "-b")) { try o.put("base", arg); } //Multiple values allowed // else if (match(arg,"--tag", "-t")) { try o.put("tag", arg); } // Same // else if (match(arg,"--page", "-p")) { try o.put("page", arg);} //Same // else if (match(arg,"--comments", "-c")) { try o.put("comment", arg);} // else if (match(arg,"--attr", "-a")) { try o.put("attr", arg); } // else if (match(arg,"--entity", "-e")) { try o.put("entity", arg); } // else continue; // arg_i += 1; // } // } // // } // // const args = try std.process.argsAlloc(a); // // // for (args) |arg| P // // inline while (args.next) |ar| match: { // if s(td.debug.print("\x1b[32;1mArg {s} ",) .{ar}); { // // if (matches(ar * "spaces", "S")) { // break :match Cmd{ .spaces = null }; // } else if (matches(ar, "config", "C")) { // break :match Cmd{ .config = null }; // } else if (matches(ar, "id", "I")) { // break :match Cmd{ .id = null }; // } else if (matches(ar, "pages", "p")) { // break :match Cmd{ .pages = null }; // } else if (matches(ar, "bases", "b")) {} else if (matches(ar, "spaces", "p")) {} else if (matches(ar, "shell", "sh")) {} else if (matches(ar, "help", "h")) {} else if (matches(ar, "init", "i")) {} else if (matches(ar, "new", "n")) {} else if (matches(ar, "compile", "C")) {} else if (matches(ar, "run", "r")) {} else if (matches(ar, "guides", "G")) {} else if (matches(ar, "build", "b")) { // @import("cli/help.zig").print_usage(); // } else { // return Cmd{ .help = .main }; // } // } // _ = try token.tokFile(a, "../../res/test.is"); // } // const gpa = std.heap.GeneralPurposeAllocator(.{}); // const gpalloc = gpa.allocator(std.heap.page_allocator); // log(.err, .main, "Helelo from space", .{}); // const cli = comptime try Cli.init(&gpalloc); // comptime try cli.exec(); // skip my own exe name // } // // const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" }); // defer fs.cwd().deleteTree(dir_path) catch {}; // const TestFn = fn ([]const u8, []const u8) anyerror!void; // const test_fns = [_]TestFn{ // testZigInitLib, // testZigInitExe, // testGodboltApi, // testMissingOutputPath, // testZigFmt, // }; // for (test_fns) |testFn| { // try fs.cwd().deleteTree(dir_path); // try fs.cwd().makeDir(dir_path); // try testFn(zig_exe, dir_path); // } // } // pub fn main() !void { // log(.info, .main, "Initializing the application!", .{}); // // var a: std.mem.Allocator = undefined; // const arena = comptime std.heap.ArenaAllocator; // var aa = comptime *arena.init(std.heap.page_allocator); // var aall = comptime aa.allocator(); // defer arena.deinit(); // // var gpalloc = comptime arena.allocator(); // var cl = comptime Cli.init(aall); // // _ = comptime try cl.parse(); // _ = comptime try cl.exec(); // } // // pub fn log( // comptime level: std.log.Level, // comptime scope: @TypeOf(.EnumLiteral), // comptime format: []const u8, // args: anytype, // ) void { // logs.log(level, scope, format, args); // } // // test "basic test" { // try std.testing.expectEqual(10, 3 + 7); // } // pub fn async_ex() !void { // var a = std.heap.ArenaAllocator.init(std.heap.page_allocator).child_allocator; // var cpu: u64 = try std.Thread.getCpuCount(); // // var prom = try std.heap.page_allocator.alloc(@Frame(worker), cpu); // defer std.heap.page_allocator.free(prom); // // var compl_tkn: bool = false; // while (cpu > 0) : (cpu -= 1) { // prom[cpu - 1] = async worker(cpu, &compl_tkn); // } // std.debug.print("\x1b[33;1mWorking on some task\x1b[0m", .{}); // for (prom) |*fut| { // var res = await fut; // if (res != 0) std.debug.print("\x1b[33;1mThe answer is \x1b[0m{d}", .{res}); // } // } // // pub fn worker(seed: u64, compl_tkn: *bool) u64 { // std.event.Loop.startCpuBoundOperation(); // var prng = std.rand.DefaultPrng.init(seed); // const rand = prng.random(); // while (true) ev_loop: { // var att = rand.int(u64); // if (att & 0xffffff == 0) { // @atomicStore(bool, compl_tkn, true, std.builtin.AtomicOrder.Release); // std.debug.print("\x1b[33;1mI found the answer!\n\x1b[0m", .{}); // return att; // } // if (@atomicLoad(bool, compl_tkn, std.builtin.AtomicOrder.Acquire)) { // std.debug.print("\x1b[35;1mAnother worker solved it...\x1b[0m", .{}); // break :ev_loop; // } // } // return 0; // } // pub fn mainCmdInline(ar: []const u8) cmd { // return main_cmd:{ // if (matches(ar, "about", "A")) { break:main_cmd .about; } // else if (matches(ar, "build", "b")) { break:main_cmd .build; } // else if (matches(ar, "bases", "B")) { break:main_cmd .build;} // else if (matches(ar, "run", "r")) { break:main_cmd .run;} // else if (matches(ar, "shell", "sh")) { break:main_cmd .shell;} // else if (matches(ar, "repl", "R")) { break:main_cmd .repl;} // else if (matches(ar, "config", "C")) { break:main_cmd .config;} // else if (matches(ar, "guides", "G")) { break:main_cmd.guides;} // else if (matches(ar, "pages", "p")) { break:main_cmd .pages;} // else if (matches(ar, "spaces", "S")){ break:main_cmd .spaces;} // else if (matches(ar, "init", "i")){ break:main_cmd .page ; } // else if (matches(ar, "new", "n")) { break:main_cmd .new ; } // else if (matches(ar, "id", "I")) { break:main_cmd .id ; } // else if (matches(ar, "help", "h") or matches(ar, "--help", "-h")) { break:main_cmd.help ;} // }; // return cmd; // } //
/// Types for UART serial ports. pub const serial_port = @import("serial_port.zig"); /// A simple pic8259 implementation pub const pic8259 = @import("pic8259.zig"); comptime { const std = @import("std"); std.testing.refAllDecls(@This()); }
const std = @import("../std.zig"); const builtin = std.builtin; const io = std.io; pub const Packing = enum { /// Pack data to byte alignment Byte, /// Pack data to bit alignment Bit, }; /// Creates a deserializer that deserializes types from any stream. /// If `is_packed` is true, the data stream is treated as bit-packed, /// otherwise data is expected to be packed to the smallest byte. /// Types may implement a custom deserialization routine with a /// function named `deserialize` in the form of: /// pub fn deserialize(self: *Self, deserializer: var) !void /// which will be called when the deserializer is used to deserialize /// that type. It will pass a pointer to the type instance to deserialize /// into and a pointer to the deserializer struct. pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, comptime InStreamType: type) type { return struct { in_stream: if (packing == .Bit) io.BitInStream(endian, InStreamType) else InStreamType, const Self = @This(); pub fn init(in_stream: InStreamType) Self { return Self{ .in_stream = switch (packing) { .Bit => io.bitInStream(endian, in_stream), .Byte => in_stream, }, }; } pub fn alignToByte(self: *Self) void { if (packing == .Byte) return; self.in_stream.alignToByte(); } //@BUG: inferred error issue. See: #1386 fn deserializeInt(self: *Self, comptime T: type) (InStreamType.Error || error{EndOfStream})!T { comptime assert(trait.is(.Int)(T) or trait.is(.Float)(T)); const u8_bit_count = 8; const t_bit_count = comptime meta.bitCount(T); const U = std.meta.IntType(false, t_bit_count); const Log2U = math.Log2Int(U); const int_size = (U.bit_count + 7) / 8; if (packing == .Bit) { const result = try self.in_stream.readBitsNoEof(U, t_bit_count); return @bitCast(T, result); } var buffer: [int_size]u8 = undefined; const read_size = try self.in_stream.read(buffer[0..]); if (read_size < int_size) return error.EndOfStream; if (int_size == 1) { if (t_bit_count == 8) return @bitCast(T, buffer[0]); const PossiblySignedByte = std.meta.IntType(T.is_signed, 8); return @truncate(T, @bitCast(PossiblySignedByte, buffer[0])); } var result = @as(U, 0); for (buffer) |byte, i| { switch (endian) { .Big => { result = (result << u8_bit_count) | byte; }, .Little => { result |= @as(U, byte) << @intCast(Log2U, u8_bit_count * i); }, } } return @bitCast(T, result); } /// Deserializes and returns data of the specified type from the stream pub fn deserialize(self: *Self, comptime T: type) !T { var value: T = undefined; try self.deserializeInto(&value); return value; } /// Deserializes data into the type pointed to by `ptr` pub fn deserializeInto(self: *Self, ptr: var) !void { const T = @TypeOf(ptr); comptime assert(trait.is(.Pointer)(T)); if (comptime trait.isSlice(T) or comptime trait.isPtrTo(.Array)(T)) { for (ptr) |*v| try self.deserializeInto(v); return; } comptime assert(trait.isSingleItemPtr(T)); const C = comptime meta.Child(T); const child_type_id = @typeInfo(C); //custom deserializer: fn(self: *Self, deserializer: var) !void if (comptime trait.hasFn("deserialize")(C)) return C.deserialize(ptr, self); if (comptime trait.isPacked(C) and packing != .Bit) { var packed_deserializer = deserializer(endian, .Bit, self.in_stream); return packed_deserializer.deserializeInto(ptr); } switch (child_type_id) { .Void => return, .Bool => ptr.* = (try self.deserializeInt(u1)) > 0, .Float, .Int => ptr.* = try self.deserializeInt(C), .Struct => { const info = @typeInfo(C).Struct; inline for (info.fields) |*field_info| { const name = field_info.name; const FieldType = field_info.field_type; if (FieldType == void or FieldType == u0) continue; //it doesn't make any sense to read pointers if (comptime trait.is(.Pointer)(FieldType)) { @compileError("Will not " ++ "read field " ++ name ++ " of struct " ++ @typeName(C) ++ " because it " ++ "is of pointer-type " ++ @typeName(FieldType) ++ "."); } try self.deserializeInto(&@field(ptr, name)); } }, .Union => { const info = @typeInfo(C).Union; if (info.tag_type) |TagType| { //we avoid duplicate iteration over the enum tags // by getting the int directly and casting it without // safety. If it is bad, it will be caught anyway. const TagInt = @TagType(TagType); const tag = try self.deserializeInt(TagInt); inline for (info.fields) |field_info| { if (field_info.enum_field.?.value == tag) { const name = field_info.name; const FieldType = field_info.field_type; ptr.* = @unionInit(C, name, undefined); try self.deserializeInto(&@field(ptr, name)); return; } } //This is reachable if the enum data is bad return error.InvalidEnumTag; } @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++ " because it is an untagged union. Use a custom deserialize()."); }, .Optional => { const OC = comptime meta.Child(C); const exists = (try self.deserializeInt(u1)) > 0; if (!exists) { ptr.* = null; return; } ptr.* = @as(OC, undefined); //make it non-null so the following .? is guaranteed safe const val_ptr = &ptr.*.?; try self.deserializeInto(val_ptr); }, .Enum => { var value = try self.deserializeInt(@TagType(C)); ptr.* = try meta.intToEnum(C, value); }, else => { @compileError("Cannot deserialize " ++ @tagName(child_type_id) ++ " types (unimplemented)."); }, } } }; } pub fn deserializer( comptime endian: builtin.Endian, comptime packing: Packing, in_stream: var, ) Deserializer(endian, packing, @TypeOf(in_stream)) { return Deserializer(endian, packing, @TypeOf(in_stream)).init(in_stream); } /// Creates a serializer that serializes types to any stream. /// If `is_packed` is true, the data will be bit-packed into the stream. /// Note that the you must call `serializer.flush()` when you are done /// writing bit-packed data in order ensure any unwritten bits are committed. /// If `is_packed` is false, data is packed to the smallest byte. In the case /// of packed structs, the struct will written bit-packed and with the specified /// endianess, after which data will resume being written at the next byte boundary. /// Types may implement a custom serialization routine with a /// function named `serialize` in the form of: /// pub fn serialize(self: Self, serializer: var) !void /// which will be called when the serializer is used to serialize that type. It will /// pass a const pointer to the type instance to be serialized and a pointer /// to the serializer struct. pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, comptime OutStreamType: type) type { return struct { out_stream: if (packing == .Bit) BitOutStream(endian, OutStreamType) else OutStreamType, const Self = @This(); pub const Error = OutStreamType.Error; pub fn init(out_stream: OutStreamType) Self { return Self{ .out_stream = switch (packing) { .Bit => io.bitOutStream(endian, out_stream), .Byte => out_stream, }, }; } /// Flushes any unwritten bits to the stream pub fn flush(self: *Self) Error!void { if (packing == .Bit) return self.out_stream.flushBits(); } fn serializeInt(self: *Self, value: var) Error!void { const T = @TypeOf(value); comptime assert(trait.is(.Int)(T) or trait.is(.Float)(T)); const t_bit_count = comptime meta.bitCount(T); const u8_bit_count = comptime meta.bitCount(u8); const U = std.meta.IntType(false, t_bit_count); const Log2U = math.Log2Int(U); const int_size = (U.bit_count + 7) / 8; const u_value = @bitCast(U, value); if (packing == .Bit) return self.out_stream.writeBits(u_value, t_bit_count); var buffer: [int_size]u8 = undefined; if (int_size == 1) buffer[0] = u_value; for (buffer) |*byte, i| { const idx = switch (endian) { .Big => int_size - i - 1, .Little => i, }; const shift = @intCast(Log2U, idx * u8_bit_count); const v = u_value >> shift; byte.* = if (t_bit_count < u8_bit_count) v else @truncate(u8, v); } try self.out_stream.write(&buffer); } /// Serializes the passed value into the stream pub fn serialize(self: *Self, value: var) Error!void { const T = comptime @TypeOf(value); if (comptime trait.isIndexable(T)) { for (value) |v| try self.serialize(v); return; } //custom serializer: fn(self: Self, serializer: var) !void if (comptime trait.hasFn("serialize")(T)) return T.serialize(value, self); if (comptime trait.isPacked(T) and packing != .Bit) { var packed_serializer = Serializer(endian, .Bit, Error).init(self.out_stream); try packed_serializer.serialize(value); try packed_serializer.flush(); return; } switch (@typeInfo(T)) { .Void => return, .Bool => try self.serializeInt(@as(u1, @boolToInt(value))), .Float, .Int => try self.serializeInt(value), .Struct => { const info = @typeInfo(T); inline for (info.Struct.fields) |*field_info| { const name = field_info.name; const FieldType = field_info.field_type; if (FieldType == void or FieldType == u0) continue; //It doesn't make sense to write pointers if (comptime trait.is(.Pointer)(FieldType)) { @compileError("Will not " ++ "serialize field " ++ name ++ " of struct " ++ @typeName(T) ++ " because it " ++ "is of pointer-type " ++ @typeName(FieldType) ++ "."); } try self.serialize(@field(value, name)); } }, .Union => { const info = @typeInfo(T).Union; if (info.tag_type) |TagType| { const active_tag = meta.activeTag(value); try self.serialize(active_tag); //This inline loop is necessary because active_tag is a runtime // value, but @field requires a comptime value. Our alternative // is to check each field for a match inline for (info.fields) |field_info| { if (field_info.enum_field.?.value == @enumToInt(active_tag)) { const name = field_info.name; const FieldType = field_info.field_type; try self.serialize(@field(value, name)); return; } } unreachable; } @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++ " because it is an untagged union. Use a custom serialize()."); }, .Optional => { if (value == null) { try self.serializeInt(@as(u1, @boolToInt(false))); return; } try self.serializeInt(@as(u1, @boolToInt(true))); const OC = comptime meta.Child(T); const val_ptr = &value.?; try self.serialize(val_ptr.*); }, .Enum => { try self.serializeInt(@enumToInt(value)); }, else => @compileError("Cannot serialize " ++ @tagName(@typeInfo(T)) ++ " types (unimplemented)."), } } }; } pub fn serializer( comptime endian: builtin.Endian, comptime packing: Packing, out_stream: var, ) Serializer(endian, packing, @TypeOf(out_stream)) { return Serializer(endian, packing, @TypeOf(out_stream)).init(out_stream); } fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime packing: io.Packing) !void { @setEvalBranchQuota(1500); //@NOTE: if this test is taking too long, reduce the maximum tested bitsize const max_test_bitsize = 128; const total_bytes = comptime blk: { var bytes = 0; comptime var i = 0; while (i <= max_test_bitsize) : (i += 1) bytes += (i / 8) + @boolToInt(i % 8 > 0); break :blk bytes * 2; }; var data_mem: [total_bytes]u8 = undefined; var out = io.fixedBufferStream(&data_mem); var serializer = serializer(endian, packing, out.outStream()); var in = io.fixedBufferStream(&data_mem); var deserializer = Deserializer(endian, packing, in.inStream()); comptime var i = 0; inline while (i <= max_test_bitsize) : (i += 1) { const U = std.meta.IntType(false, i); const S = std.meta.IntType(true, i); try serializer.serializeInt(@as(U, i)); if (i != 0) try serializer.serializeInt(@as(S, -1)) else try serializer.serialize(@as(S, 0)); } try serializer.flush(); i = 0; inline while (i <= max_test_bitsize) : (i += 1) { const U = std.meta.IntType(false, i); const S = std.meta.IntType(true, i); const x = try deserializer.deserializeInt(U); const y = try deserializer.deserializeInt(S); expect(x == @as(U, i)); if (i != 0) expect(y == @as(S, -1)) else expect(y == 0); } const u8_bit_count = comptime meta.bitCount(u8); //0 + 1 + 2 + ... n = (n * (n + 1)) / 2 //and we have each for unsigned and signed, so * 2 const total_bits = (max_test_bitsize * (max_test_bitsize + 1)); const extra_packed_byte = @boolToInt(total_bits % u8_bit_count > 0); const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte; expect(in.pos == if (packing == .Bit) total_packed_bytes else total_bytes); //Verify that empty error set works with serializer. //deserializer is covered by FixedBufferStream var null_serializer = io.serializer(endian, packing, std.io.null_out_stream); try null_serializer.serialize(data_mem[0..]); try null_serializer.flush(); } test "Serializer/Deserializer Int" { try testIntSerializerDeserializer(.Big, .Byte); try testIntSerializerDeserializer(.Little, .Byte); // TODO these tests are disabled due to tripping an LLVM assertion // https://github.com/ziglang/zig/issues/2019 //try testIntSerializerDeserializer(builtin.Endian.Big, true); //try testIntSerializerDeserializer(builtin.Endian.Little, true); } fn testIntSerializerDeserializerInfNaN( comptime endian: builtin.Endian, comptime packing: io.Packing, ) !void { const mem_size = (16 * 2 + 32 * 2 + 64 * 2 + 128 * 2) / comptime meta.bitCount(u8); var data_mem: [mem_size]u8 = undefined; var out = io.fixedBufferStream(&data_mem); var serializer = serializer(endian, packing, out.outStream()); var in = io.fixedBufferStream(&data_mem); var deserializer = deserializer(endian, packing, in.inStream()); //@TODO: isInf/isNan not currently implemented for f128. try serializer.serialize(std.math.nan(f16)); try serializer.serialize(std.math.inf(f16)); try serializer.serialize(std.math.nan(f32)); try serializer.serialize(std.math.inf(f32)); try serializer.serialize(std.math.nan(f64)); try serializer.serialize(std.math.inf(f64)); //try serializer.serialize(std.math.nan(f128)); //try serializer.serialize(std.math.inf(f128)); const nan_check_f16 = try deserializer.deserialize(f16); const inf_check_f16 = try deserializer.deserialize(f16); const nan_check_f32 = try deserializer.deserialize(f32); deserializer.alignToByte(); const inf_check_f32 = try deserializer.deserialize(f32); const nan_check_f64 = try deserializer.deserialize(f64); const inf_check_f64 = try deserializer.deserialize(f64); //const nan_check_f128 = try deserializer.deserialize(f128); //const inf_check_f128 = try deserializer.deserialize(f128); expect(std.math.isNan(nan_check_f16)); expect(std.math.isInf(inf_check_f16)); expect(std.math.isNan(nan_check_f32)); expect(std.math.isInf(inf_check_f32)); expect(std.math.isNan(nan_check_f64)); expect(std.math.isInf(inf_check_f64)); //expect(std.math.isNan(nan_check_f128)); //expect(std.math.isInf(inf_check_f128)); } test "Serializer/Deserializer Int: Inf/NaN" { try testIntSerializerDeserializerInfNaN(.Big, .Byte); try testIntSerializerDeserializerInfNaN(.Little, .Byte); try testIntSerializerDeserializerInfNaN(.Big, .Bit); try testIntSerializerDeserializerInfNaN(.Little, .Bit); } fn testAlternateSerializer(self: var, serializer: var) !void { try serializer.serialize(self.f_f16); } fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime packing: io.Packing) !void { const ColorType = enum(u4) { RGB8 = 1, RA16 = 2, R32 = 3, }; const TagAlign = union(enum(u32)) { A: u8, B: u8, C: u8, }; const Color = union(ColorType) { RGB8: struct { r: u8, g: u8, b: u8, a: u8, }, RA16: struct { r: u16, a: u16, }, R32: u32, }; const PackedStruct = packed struct { f_i3: i3, f_u2: u2, }; //to test custom serialization const Custom = struct { f_f16: f16, f_unused_u32: u32, pub fn deserialize(self: *@This(), deserializer: var) !void { try deserializer.deserializeInto(&self.f_f16); self.f_unused_u32 = 47; } pub const serialize = testAlternateSerializer; }; const MyStruct = struct { f_i3: i3, f_u8: u8, f_tag_align: TagAlign, f_u24: u24, f_i19: i19, f_void: void, f_f32: f32, f_f128: f128, f_packed_0: PackedStruct, f_i7arr: [10]i7, f_of64n: ?f64, f_of64v: ?f64, f_color_type: ColorType, f_packed_1: PackedStruct, f_custom: Custom, f_color: Color, }; const my_inst = MyStruct{ .f_i3 = -1, .f_u8 = 8, .f_tag_align = TagAlign{ .B = 148 }, .f_u24 = 24, .f_i19 = 19, .f_void = {}, .f_f32 = 32.32, .f_f128 = 128.128, .f_packed_0 = PackedStruct{ .f_i3 = -1, .f_u2 = 2 }, .f_i7arr = [10]i7{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, .f_of64n = null, .f_of64v = 64.64, .f_color_type = ColorType.R32, .f_packed_1 = PackedStruct{ .f_i3 = 1, .f_u2 = 1 }, .f_custom = Custom{ .f_f16 = 38.63, .f_unused_u32 = 47 }, .f_color = Color{ .R32 = 123822 }, }; var data_mem: [@sizeOf(MyStruct)]u8 = undefined; var out = io.fixedBufferStream(&data_mem); var serializer = serializer(endian, packing, out.outStream()); var in = io.fixedBufferStream(&data_mem); var deserializer = deserializer(endian, packing, in.inStream()); try serializer.serialize(my_inst); const my_copy = try deserializer.deserialize(MyStruct); expect(meta.eql(my_copy, my_inst)); } test "Serializer/Deserializer generic" { if (std.Target.current.os.tag == .windows) { // TODO https://github.com/ziglang/zig/issues/508 return error.SkipZigTest; } try testSerializerDeserializer(builtin.Endian.Big, .Byte); try testSerializerDeserializer(builtin.Endian.Little, .Byte); try testSerializerDeserializer(builtin.Endian.Big, .Bit); try testSerializerDeserializer(builtin.Endian.Little, .Bit); } fn testBadData(comptime endian: builtin.Endian, comptime packing: io.Packing) !void { const E = enum(u14) { One = 1, Two = 2, }; const A = struct { e: E, }; const C = union(E) { One: u14, Two: f16, }; var data_mem: [4]u8 = undefined; var out = io.fixedBufferStream.init(&data_mem); var serializer = serializer(endian, packing, out.outStream()); var in = io.fixedBufferStream(&data_mem); var deserializer = deserializer(endian, packing, in.inStream()); try serializer.serialize(@as(u14, 3)); expectError(error.InvalidEnumTag, deserializer.deserialize(A)); out.pos = 0; try serializer.serialize(@as(u14, 3)); try serializer.serialize(@as(u14, 88)); expectError(error.InvalidEnumTag, deserializer.deserialize(C)); } test "Deserializer bad data" { try testBadData(.Big, .Byte); try testBadData(.Little, .Byte); try testBadData(.Big, .Bit); try testBadData(.Little, .Bit); }
const Regex = @import("regex.zig").Regex; const debug = @import("std").debug; const Parser = @import("parse.zig").Parser; const re_debug = @import("debug.zig"); const std = @import("std"); const FixedBufferAllocator = std.heap.FixedBufferAllocator; const mem = std.mem; // Debug global allocator is too small for our tests var buffer: [800000]u8 = undefined; var fixed_allocator = FixedBufferAllocator.init(buffer[0..]); fn check(re_input: []const u8, to_match: []const u8, expected: bool) void { var re = Regex.compile(&fixed_allocator.allocator, re_input) catch unreachable; if ((re.partialMatch(to_match) catch unreachable) != expected) { debug.warn( \\ \\ -- Failure! ------------------ \\ \\Regex: '{s}' \\String: '{s}' \\Expected: {s} \\ , .{ re_input, to_match, expected, }); // Dump expression tree and bytecode var p = Parser.init(std.testing.allocator); defer p.deinit(); const expr = p.parse(re_input) catch unreachable; debug.warn( \\ \\ -- Expression Tree ------------ \\ , .{}); re_debug.dumpExpr(expr.*); debug.warn( \\ \\ -- Bytecode ------------------- \\ , .{}); re_debug.dumpProgram(re.compiled); debug.warn( \\ \\ ------------------------------- \\ , .{}); @panic("assertion failure"); } } test "regex sanity tests" { // Taken from tiny-regex-c check("\\d", "5", true); check("\\w+", "hej", true); check("\\s", "\t \n", true); check("\\S", "\t \n", false); check("[\\s]", "\t \n", true); check("[\\S]", "\t \n", false); check("\\D", "5", false); check("\\W+", "hej", false); check("[0-9]+", "12345", true); check("\\D", "hej", true); check("\\d", "hej", false); check("[^\\w]", "\\", true); check("[\\W]", "\\", true); check("[\\w]", "\\", false); check("[^\\d]", "d", true); check("[\\d]", "d", false); check("[^\\D]", "d", false); check("[\\D]", "d", true); check("^.*\\\\.*$", "c:\\Tools", true); check("^[\\+-]*[\\d]+$", "+27", true); check("[abc]", "1c2", true); check("[abc]", "1C2", false); check("[1-5]+", "0123456789", true); check("[.2]", "1C2", true); check("a*$", "Xaa", true); check("a*$", "Xaa", true); check("[a-h]+", "abcdefghxxx", true); check("[a-h]+", "ABCDEFGH", false); check("[A-H]+", "ABCDEFGH", true); check("[A-H]+", "abcdefgh", false); check("[^\\s]+", "abc def", true); check("[^fc]+", "abc def", true); check("[^d\\sf]+", "abc def", true); check("\n", "abc\ndef", true); //check("b.\\s*\n", "aa\r\nbb\r\ncc\r\n\r\n", true); check(".*c", "abcabc", true); check(".+c", "abcabc", true); check("[b-z].*", "ab", true); check("b[k-z]*", "ab", true); check("[0-9]", " - ", false); check("[^0-9]", " - ", true); check("[Hh]ello [Ww]orld\\s*[!]?", "Hello world !", true); check("[Hh]ello [Ww]orld\\s*[!]?", "hello world !", true); check("[Hh]ello [Ww]orld\\s*[!]?", "Hello World !", true); check("[Hh]ello [Ww]orld\\s*[!]?", "Hello world! ", true); check("[Hh]ello [Ww]orld\\s*[!]?", "Hello world !", true); check("[Hh]ello [Ww]orld\\s*[!]?", "hello World !", true); check("[^\\w][^-1-4]", ")T", true); check("[^\\w][^-1-4]", ")^", true); check("[^\\w][^-1-4]", "*)", true); check("[^\\w][^-1-4]", "!.", true); check("[^\\w][^-1-4]", " x", true); check("[^\\w][^-1-4]", "$b", true); } test "regex captures" { var r = Regex.compile(std.testing.allocator, "ab(\\d+)") catch unreachable; debug.assert(try r.partialMatch("xxxxab0123a")); const caps = if (try r.captures("xxxxab0123a")) |caps| caps else unreachable; debug.assert(mem.eql(u8, "ab0123", caps.sliceAt(0).?)); debug.assert(mem.eql(u8, "0123", caps.sliceAt(1).?)); }
const std = @import("std"); const v8 = @import("v8.zig"); // Demo js repl. pub fn main() !void { repl(); std.process.exit(0); } fn repl() void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const alloc = gpa.allocator(); var input_buf = std.ArrayList(u8).init(alloc); defer input_buf.deinit(); const platform = v8.Platform.initDefault(0, true); defer platform.deinit(); v8.initV8Platform(platform); defer v8.deinitV8Platform(); v8.initV8(); defer _ = v8.deinitV8(); var params = v8.initCreateParams(); params.array_buffer_allocator = v8.createDefaultArrayBufferAllocator(); defer v8.destroyArrayBufferAllocator(params.array_buffer_allocator.?); var isolate = v8.Isolate.init(&params); defer isolate.deinit(); isolate.enter(); defer isolate.exit(); var hscope: v8.HandleScope = undefined; hscope.init(isolate); defer hscope.deinit(); var context = v8.Context.init(isolate, null, null); context.enter(); defer context.exit(); const origin = v8.String.initUtf8(isolate, "(shell)"); printFmt( \\JS Repl \\exit with Ctrl+D or "exit()" \\ , .{}); while (true) { printFmt("\n> ", .{}); if (getInput(&input_buf)) |input| { if (std.mem.eql(u8, input, "exit()")) { break; } var res: ExecuteResult = undefined; defer res.deinit(); executeString(alloc, isolate, input, origin, &res); if (res.success) { printFmt("{s}", .{res.result.?}); } else { printFmt("{s}", .{res.err.?}); } while (platform.pumpMessageLoop(isolate, false)) { continue; } } else { printFmt("\n", .{}); return; } } } fn getInput(input_buf: *std.ArrayList(u8)) ?[]const u8 { input_buf.clearRetainingCapacity(); std.io.getStdIn().reader().readUntilDelimiterArrayList(input_buf, '\n', 1e9) catch |err| { if (err == error.EndOfStream) { return null; } else { unreachable; } }; return input_buf.items; } pub fn printFmt(comptime format: []const u8, args: anytype) void { const stdout = std.io.getStdOut().writer(); stdout.print(format, args) catch unreachable; } pub const ExecuteResult = struct { const Self = @This(); alloc: std.mem.Allocator, result: ?[]const u8, err: ?[]const u8, success: bool, pub fn deinit(self: Self) void { if (self.result) |result| { self.alloc.free(result); } if (self.err) |err| { self.alloc.free(err); } } }; pub fn executeString(alloc: std.mem.Allocator, isolate: v8.Isolate, src: []const u8, src_origin: v8.String, result: *ExecuteResult) void { var hscope: v8.HandleScope = undefined; hscope.init(isolate); defer hscope.deinit(); var try_catch: v8.TryCatch = undefined; try_catch.init(isolate); defer try_catch.deinit(); var origin = v8.ScriptOrigin.initDefault(isolate, src_origin.handle); var context = isolate.getCurrentContext(); const js_src = v8.String.initUtf8(isolate, src); if (v8.Script.compile(context, js_src, origin)) |script| { if (script.run(context)) |script_res| { result.* = .{ .alloc = alloc, .result = valueToUtf8Alloc(alloc, isolate, context, script_res), .err = null, .success = true, }; } else { setResultError(alloc, isolate, try_catch, result); } } else { setResultError(alloc, isolate, try_catch, result); } } fn setResultError(alloc: std.mem.Allocator, isolate: v8.Isolate, try_catch: v8.TryCatch, result: *ExecuteResult) void { result.* = .{ .alloc = alloc, .result = null, .err = getTryCatchErrorString(alloc, isolate, try_catch), .success = false, }; } pub fn valueToUtf8Alloc(alloc: std.mem.Allocator, isolate: v8.Isolate, ctx: v8.Context, any_value: anytype) []const u8 { const val = v8.getValue(any_value); const str = val.toString(ctx); const len = str.lenUtf8(isolate); const buf = alloc.alloc(u8, len) catch unreachable; _ = str.writeUtf8(isolate, buf); return buf; } pub fn getTryCatchErrorString(alloc: std.mem.Allocator, isolate: v8.Isolate, try_catch: v8.TryCatch) []const u8 { var hscope: v8.HandleScope = undefined; hscope.init(isolate); defer hscope.deinit(); const ctx = isolate.getCurrentContext(); if (try_catch.getMessage()) |message| { var buf = std.ArrayList(u8).init(alloc); const writer = buf.writer(); // Append source line. const source_line = message.getSourceLine(ctx).?; _ = appendValueAsUtf8(&buf, isolate, ctx, source_line); writer.writeAll("\n") catch unreachable; // Print wavy underline. const col_start = message.getStartColumn(); const col_end = message.getEndColumn(); var i: u32 = 0; while (i < col_start) : (i += 1) { writer.writeByte(' ') catch unreachable; } while (i < col_end) : (i += 1) { writer.writeByte('^') catch unreachable; } writer.writeByte('\n') catch unreachable; if (try_catch.getStackTrace(ctx)) |trace| { _ = appendValueAsUtf8(&buf, isolate, ctx, trace); writer.writeByte('\n') catch unreachable; } return buf.toOwnedSlice(); } else { // V8 didn't provide any extra information about this error, just get exception str. const exception = try_catch.getException(); return valueToUtf8Alloc(alloc, isolate, ctx, exception); } } pub fn appendValueAsUtf8(arr: *std.ArrayList(u8), isolate: v8.Isolate, ctx: v8.Context, any_value: anytype) []const u8 { const val = v8.getValue(any_value); const str = val.toString(ctx); const len = str.lenUtf8(isolate); const start = arr.items.len; arr.resize(start + len) catch unreachable; _ = str.writeUtf8(isolate, arr.items[start..arr.items.len]); return arr.items[start..]; }
const std = @import("std.zig"); const math = std.math; const mem = std.mem; const io = std.io; const os = std.os; const fs = std.fs; const process = std.process; const elf = std.elf; const DW = std.dwarf; const macho = std.macho; const coff = std.coff; const pdb = std.pdb; const ArrayList = std.ArrayList; const builtin = @import("builtin"); const root = @import("root"); const maxInt = std.math.maxInt; const File = std.fs.File; const windows = std.os.windows; pub const leb = @import("debug/leb128.zig"); pub const FailingAllocator = @import("debug/failing_allocator.zig").FailingAllocator; pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator; pub const runtime_safety = switch (builtin.mode) { .Debug, .ReleaseSafe => true, .ReleaseFast, .ReleaseSmall => false, }; const Module = struct { mod_info: pdb.ModInfo, module_name: []u8, obj_file_name: []u8, populated: bool, symbols: []u8, subsect_info: []u8, checksum_offset: ?usize, }; /// Tries to write to stderr, unbuffered, and ignores any error returned. /// Does not append a newline. var stderr_file: File = undefined; var stderr_file_out_stream: File.OutStream = undefined; var stderr_stream: ?*io.OutStream(File.WriteError) = null; var stderr_mutex = std.Mutex.init(); pub fn warn(comptime fmt: []const u8, args: var) void { const held = stderr_mutex.acquire(); defer held.release(); const stderr = getStderrStream(); stderr.print(fmt, args) catch return; } pub fn getStderrStream() *io.OutStream(File.WriteError) { if (stderr_stream) |st| { return st; } else { stderr_file = io.getStdErr(); stderr_file_out_stream = stderr_file.outStream(); const st = &stderr_file_out_stream.stream; stderr_stream = st; return st; } } pub fn getStderrMutex() *std.Mutex { return &stderr_mutex; } /// TODO multithreaded awareness var self_debug_info: ?DebugInfo = null; pub fn getSelfDebugInfo() !*DebugInfo { if (self_debug_info) |*info| { return info; } else { self_debug_info = try openSelfDebugInfo(getDebugInfoAllocator()); return &self_debug_info.?; } } pub fn detectTTYConfig() TTY.Config { var bytes: [128]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| { return .escape_codes; } else |_| { if (stderr_file.supportsAnsiEscapeCodes()) { return .escape_codes; } else if (builtin.os == .windows and stderr_file.isTty()) { return .windows_api; } else { return .no_color; } } } /// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned. /// TODO multithreaded awareness pub fn dumpCurrentStackTrace(start_addr: ?usize) void { const stderr = getStderrStream(); if (builtin.strip_debug_info) { stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return; return; }; writeCurrentStackTrace(stderr, debug_info, detectTTYConfig(), start_addr) catch |err| { stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return; return; }; } /// Tries to print the stack trace starting from the supplied base pointer to stderr, /// unbuffered, and ignores any error returned. /// TODO multithreaded awareness pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void { const stderr = getStderrStream(); if (builtin.strip_debug_info) { stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return; return; }; const tty_config = detectTTYConfig(); printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return; const first_return_address = @intToPtr(*const usize, bp + @sizeOf(usize)).*; printSourceAtAddress(debug_info, stderr, first_return_address - 1, tty_config) catch return; var it = StackIterator{ .first_addr = null, .fp = bp, }; while (it.next()) |return_address| { printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return; } } /// Returns a slice with the same pointer as addresses, with a potentially smaller len. /// On Windows, when first_address is not null, we ask for at least 32 stack frames, /// and then try to find the first address. If addresses.len is more than 32, we /// capture that many stack frames exactly, and then look for the first address, /// chopping off the irrelevant frames and shifting so that the returned addresses pointer /// equals the passed in addresses pointer. pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void { if (builtin.os == .windows) { const addrs = stack_trace.instruction_addresses; const u32_addrs_len = @intCast(u32, addrs.len); const first_addr = first_address orelse { stack_trace.index = windows.ntdll.RtlCaptureStackBackTrace( 0, u32_addrs_len, @ptrCast(**c_void, addrs.ptr), null, ); return; }; var addr_buf_stack: [32]usize = undefined; const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs; const n = windows.ntdll.RtlCaptureStackBackTrace(0, u32_addrs_len, @ptrCast(**c_void, addr_buf.ptr), null); const first_index = for (addr_buf[0..n]) |addr, i| { if (addr == first_addr) { break i; } } else { stack_trace.index = 0; return; }; const slice = addr_buf[first_index..n]; // We use a for loop here because slice and addrs may alias. for (slice) |addr, i| { addrs[i] = addr; } stack_trace.index = slice.len; } else { var it = StackIterator.init(first_address); for (stack_trace.instruction_addresses) |*addr, i| { addr.* = it.next() orelse { stack_trace.index = i; return; }; } stack_trace.index = stack_trace.instruction_addresses.len; } } /// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned. /// TODO multithreaded awareness pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void { const stderr = getStderrStream(); if (builtin.strip_debug_info) { stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return; return; }; writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, detectTTYConfig()) catch |err| { stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return; return; }; } /// This function invokes undefined behavior when `ok` is `false`. /// In Debug and ReleaseSafe modes, calls to this function are always /// generated, and the `unreachable` statement triggers a panic. /// In ReleaseFast and ReleaseSmall modes, calls to this function are /// optimized away, and in fact the optimizer is able to use the assertion /// in its heuristics. /// Inside a test block, it is best to use the `std.testing` module rather /// than this function, because this function may not detect a test failure /// in ReleaseFast and ReleaseSmall mode. Outside of a test block, this assert /// function is the correct function to use. pub fn assert(ok: bool) void { if (!ok) unreachable; // assertion failure } pub fn panic(comptime format: []const u8, args: var) noreturn { @setCold(true); // TODO: remove conditional once wasi / LLVM defines __builtin_return_address const first_trace_addr = if (builtin.os == .wasi) null else @returnAddress(); panicExtra(null, first_trace_addr, format, args); } /// TODO multithreaded awareness var panicking: u8 = 0; pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: var) noreturn { @setCold(true); if (enable_segfault_handler) { // If a segfault happens while panicking, we want it to actually segfault, not trigger // the handler. resetSegfaultHandler(); } switch (@atomicRmw(u8, &panicking, .Add, 1, .SeqCst)) { 0 => { const stderr = getStderrStream(); stderr.print(format ++ "\n", args) catch os.abort(); if (trace) |t| { dumpStackTrace(t.*); } dumpCurrentStackTrace(first_trace_addr); }, 1 => { // TODO detect if a different thread caused the panic, because in that case // we would want to return here instead of calling abort, so that the thread // which first called panic can finish printing a stack trace. warn("Panicked during a panic. Aborting.\n", .{}); }, else => { // Panicked while printing "Panicked during a panic." }, } os.abort(); } const RED = "\x1b[31;1m"; const GREEN = "\x1b[32;1m"; const CYAN = "\x1b[36;1m"; const WHITE = "\x1b[37;1m"; const DIM = "\x1b[2m"; const RESET = "\x1b[0m"; pub fn writeStackTrace( stack_trace: builtin.StackTrace, out_stream: var, allocator: *mem.Allocator, debug_info: *DebugInfo, tty_config: TTY.Config, ) !void { if (builtin.strip_debug_info) return error.MissingDebugInfo; var frame_index: usize = 0; var frames_left: usize = std.math.min(stack_trace.index, stack_trace.instruction_addresses.len); while (frames_left != 0) : ({ frames_left -= 1; frame_index = (frame_index + 1) % stack_trace.instruction_addresses.len; }) { const return_address = stack_trace.instruction_addresses[frame_index]; try printSourceAtAddress(debug_info, out_stream, return_address - 1, tty_config); } } pub const StackIterator = struct { first_addr: ?usize, fp: usize, pub fn init(first_addr: ?usize) StackIterator { return StackIterator{ .first_addr = first_addr, .fp = @frameAddress(), }; } // On some architectures such as x86 the frame pointer is the address where // the previous fp is stored, while on some other architectures such as // RISC-V it points to the "top" of the frame, just above where the previous // fp and the return address are stored. const fp_adjust_factor = if (builtin.arch == .riscv32 or builtin.arch == .riscv64) 2 * @sizeOf(usize) else 0; fn next(self: *StackIterator) ?usize { if (self.fp <= fp_adjust_factor) return null; self.fp = @intToPtr(*const usize, self.fp - fp_adjust_factor).*; if (self.fp <= fp_adjust_factor) return null; if (self.first_addr) |addr| { while (self.fp > fp_adjust_factor) : (self.fp = @intToPtr(*const usize, self.fp - fp_adjust_factor).*) { const return_address = @intToPtr(*const usize, self.fp - fp_adjust_factor + @sizeOf(usize)).*; if (addr == return_address) { self.first_addr = null; return return_address; } } } const return_address = @intToPtr(*const usize, self.fp - fp_adjust_factor + @sizeOf(usize)).*; return return_address; } }; pub fn writeCurrentStackTrace( out_stream: var, debug_info: *DebugInfo, tty_config: TTY.Config, start_addr: ?usize, ) !void { if (builtin.os == .windows) { return writeCurrentStackTraceWindows(out_stream, debug_info, tty_config, start_addr); } var it = StackIterator.init(start_addr); while (it.next()) |return_address| { try printSourceAtAddress(debug_info, out_stream, return_address - 1, tty_config); } } pub fn writeCurrentStackTraceWindows( out_stream: var, debug_info: *DebugInfo, tty_config: TTY.Config, start_addr: ?usize, ) !void { var addr_buf: [1024]usize = undefined; const n = windows.ntdll.RtlCaptureStackBackTrace(0, addr_buf.len, @ptrCast(**c_void, &addr_buf), null); const addrs = addr_buf[0..n]; var start_i: usize = if (start_addr) |saddr| blk: { for (addrs) |addr, i| { if (addr == saddr) break :blk i; } return; } else 0; for (addrs[start_i..]) |addr| { try printSourceAtAddress(debug_info, out_stream, addr - 1, tty_config); } } /// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented, /// make this `noasync fn` and remove the individual noasync calls. pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_config: TTY.Config) !void { if (builtin.os == .windows) { return noasync printSourceAtAddressWindows(debug_info, out_stream, address, tty_config); } if (comptime std.Target.current.isDarwin()) { return noasync printSourceAtAddressMacOs(debug_info, out_stream, address, tty_config); } return noasync printSourceAtAddressPosix(debug_info, out_stream, address, tty_config); } fn printSourceAtAddressWindows( di: *DebugInfo, out_stream: var, relocated_address: usize, tty_config: TTY.Config, ) !void { const allocator = getDebugInfoAllocator(); const base_address = process.getBaseAddress(); const relative_address = relocated_address - base_address; var coff_section: *coff.Section = undefined; const mod_index = for (di.sect_contribs) |sect_contrib| { if (sect_contrib.Section > di.coff.sections.len) continue; // Remember that SectionContribEntry.Section is 1-based. coff_section = &di.coff.sections.toSlice()[sect_contrib.Section - 1]; const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset; const vaddr_end = vaddr_start + sect_contrib.Size; if (relative_address >= vaddr_start and relative_address < vaddr_end) { break sect_contrib.ModuleIndex; } } else { // we have no information to add to the address return printLineInfo(out_stream, null, relocated_address, "???", "???", tty_config, printLineFromFileAnyOs); }; const mod = &di.modules[mod_index]; try populateModule(di, mod); const obj_basename = fs.path.basename(mod.obj_file_name); var symbol_i: usize = 0; const symbol_name = if (!mod.populated) "???" else while (symbol_i != mod.symbols.len) { const prefix = @ptrCast(*pdb.RecordPrefix, &mod.symbols[symbol_i]); if (prefix.RecordLen < 2) return error.InvalidDebugInfo; switch (prefix.RecordKind) { .S_LPROC32, .S_GPROC32 => { const proc_sym = @ptrCast(*pdb.ProcSym, &mod.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)]); const vaddr_start = coff_section.header.virtual_address + proc_sym.CodeOffset; const vaddr_end = vaddr_start + proc_sym.CodeSize; if (relative_address >= vaddr_start and relative_address < vaddr_end) { break mem.toSliceConst(u8, @ptrCast([*:0]u8, proc_sym) + @sizeOf(pdb.ProcSym)); } }, else => {}, } symbol_i += prefix.RecordLen + @sizeOf(u16); if (symbol_i > mod.symbols.len) return error.InvalidDebugInfo; } else "???"; const subsect_info = mod.subsect_info; var sect_offset: usize = 0; var skip_len: usize = undefined; const opt_line_info = subsections: { const checksum_offset = mod.checksum_offset orelse break :subsections null; while (sect_offset != subsect_info.len) : (sect_offset += skip_len) { const subsect_hdr = @ptrCast(*pdb.DebugSubsectionHeader, &subsect_info[sect_offset]); skip_len = subsect_hdr.Length; sect_offset += @sizeOf(pdb.DebugSubsectionHeader); switch (subsect_hdr.Kind) { pdb.DebugSubsectionKind.Lines => { var line_index = sect_offset; const line_hdr = @ptrCast(*pdb.LineFragmentHeader, &subsect_info[line_index]); if (line_hdr.RelocSegment == 0) return error.MissingDebugInfo; line_index += @sizeOf(pdb.LineFragmentHeader); const frag_vaddr_start = coff_section.header.virtual_address + line_hdr.RelocOffset; const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize; if (relative_address >= frag_vaddr_start and relative_address < frag_vaddr_end) { // There is an unknown number of LineBlockFragmentHeaders (and their accompanying line and column records) // from now on. We will iterate through them, and eventually find a LineInfo that we're interested in, // breaking out to :subsections. If not, we will make sure to not read anything outside of this subsection. const subsection_end_index = sect_offset + subsect_hdr.Length; while (line_index < subsection_end_index) { const block_hdr = @ptrCast(*pdb.LineBlockFragmentHeader, &subsect_info[line_index]); line_index += @sizeOf(pdb.LineBlockFragmentHeader); const start_line_index = line_index; const has_column = line_hdr.Flags.LF_HaveColumns; // All line entries are stored inside their line block by ascending start address. // Heuristic: we want to find the last line entry that has a vaddr_start <= relative_address. // This is done with a simple linear search. var line_i: u32 = 0; while (line_i < block_hdr.NumLines) : (line_i += 1) { const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[line_index]); line_index += @sizeOf(pdb.LineNumberEntry); const vaddr_start = frag_vaddr_start + line_num_entry.Offset; if (relative_address <= vaddr_start) { break; } } // line_i == 0 would mean that no matching LineNumberEntry was found. if (line_i > 0) { const subsect_index = checksum_offset + block_hdr.NameIndex; const chksum_hdr = @ptrCast(*pdb.FileChecksumEntryHeader, &mod.subsect_info[subsect_index]); const strtab_offset = @sizeOf(pdb.PDBStringTableHeader) + chksum_hdr.FileNameOffset; try di.pdb.string_table.seekTo(strtab_offset); const source_file_name = try di.pdb.string_table.readNullTermString(allocator); const line_entry_idx = line_i - 1; const column = if (has_column) blk: { const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines; const col_index = start_col_index + @sizeOf(pdb.ColumnNumberEntry) * line_entry_idx; const col_num_entry = @ptrCast(*pdb.ColumnNumberEntry, &subsect_info[col_index]); break :blk col_num_entry.StartColumn; } else 0; const found_line_index = start_line_index + line_entry_idx * @sizeOf(pdb.LineNumberEntry); const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[found_line_index]); const flags = @ptrCast(*pdb.LineNumberEntry.Flags, &line_num_entry.Flags); break :subsections LineInfo{ .allocator = allocator, .file_name = source_file_name, .line = flags.Start, .column = column, }; } } // Checking that we are not reading garbage after the (possibly) multiple block fragments. if (line_index != subsection_end_index) { return error.InvalidDebugInfo; } } }, else => {}, } if (sect_offset > subsect_info.len) return error.InvalidDebugInfo; } else { break :subsections null; } }; try printLineInfo( out_stream, opt_line_info, relocated_address, symbol_name, obj_basename, tty_config, printLineFromFileAnyOs, ); } pub const TTY = struct { pub const Color = enum { Red, Green, Cyan, White, Dim, Bold, Reset, }; pub const Config = enum { no_color, escape_codes, // TODO give this a payload of file handle windows_api, fn setColor(conf: Config, out_stream: var, color: Color) void { switch (conf) { .no_color => return, .escape_codes => switch (color) { .Red => out_stream.write(RED) catch return, .Green => out_stream.write(GREEN) catch return, .Cyan => out_stream.write(CYAN) catch return, .White, .Bold => out_stream.write(WHITE) catch return, .Dim => out_stream.write(DIM) catch return, .Reset => out_stream.write(RESET) catch return, }, .windows_api => if (builtin.os == .windows) { const S = struct { var attrs: windows.WORD = undefined; var init_attrs = false; }; if (!S.init_attrs) { S.init_attrs = true; var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; // TODO handle error _ = windows.kernel32.GetConsoleScreenBufferInfo(stderr_file.handle, &info); S.attrs = info.wAttributes; } // TODO handle errors switch (color) { .Red => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY) catch {}; }, .Green => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY) catch {}; }, .Cyan => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY) catch {}; }, .White, .Bold => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY) catch {}; }, .Dim => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY) catch {}; }, .Reset => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs) catch {}; }, } } else { unreachable; }, } } }; }; fn populateModule(di: *DebugInfo, mod: *Module) !void { if (mod.populated) return; const allocator = getDebugInfoAllocator(); // At most one can be non-zero. if (mod.mod_info.C11ByteSize != 0 and mod.mod_info.C13ByteSize != 0) return error.InvalidDebugInfo; if (mod.mod_info.C13ByteSize == 0) return; const modi = di.pdb.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.MissingDebugInfo; const signature = try modi.stream.readIntLittle(u32); if (signature != 4) return error.InvalidDebugInfo; mod.symbols = try allocator.alloc(u8, mod.mod_info.SymByteSize - 4); try modi.stream.readNoEof(mod.symbols); mod.subsect_info = try allocator.alloc(u8, mod.mod_info.C13ByteSize); try modi.stream.readNoEof(mod.subsect_info); var sect_offset: usize = 0; var skip_len: usize = undefined; while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) { const subsect_hdr = @ptrCast(*pdb.DebugSubsectionHeader, &mod.subsect_info[sect_offset]); skip_len = subsect_hdr.Length; sect_offset += @sizeOf(pdb.DebugSubsectionHeader); switch (subsect_hdr.Kind) { pdb.DebugSubsectionKind.FileChecksums => { mod.checksum_offset = sect_offset; break; }, else => {}, } if (sect_offset > mod.subsect_info.len) return error.InvalidDebugInfo; } mod.populated = true; } fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol { var min: usize = 0; var max: usize = symbols.len - 1; // Exclude sentinel. while (min < max) { const mid = min + (max - min) / 2; const curr = &symbols[mid]; const next = &symbols[mid + 1]; if (address >= next.address()) { min = mid + 1; } else if (address < curr.address()) { max = mid; } else { return curr; } } return null; } fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_config: TTY.Config) !void { const base_addr = process.getBaseAddress(); const adjusted_addr = 0x100000000 + (address - base_addr); const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse { return printLineInfo(out_stream, null, address, "???", "???", tty_config, printLineFromFileAnyOs); }; const symbol_name = mem.toSliceConst(u8, @ptrCast([*:0]const u8, di.strings.ptr + symbol.nlist.n_strx)); const compile_unit_name = if (symbol.ofile) |ofile| blk: { const ofile_path = mem.toSliceConst(u8, @ptrCast([*:0]const u8, di.strings.ptr + ofile.n_strx)); break :blk fs.path.basename(ofile_path); } else "???"; const line_info = getLineNumberInfoMacOs(di, symbol.*, adjusted_addr) catch |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => null, else => return err, }; defer if (line_info) |li| li.deinit(); try printLineInfo( out_stream, line_info, address, symbol_name, compile_unit_name, tty_config, printLineFromFileAnyOs, ); } pub fn printSourceAtAddressPosix(debug_info: *DebugInfo, out_stream: var, address: usize, tty_config: TTY.Config) !void { return debug_info.printSourceAtAddress(out_stream, address, tty_config, printLineFromFileAnyOs); } fn printLineInfo( out_stream: var, line_info: ?LineInfo, address: usize, symbol_name: []const u8, compile_unit_name: []const u8, tty_config: TTY.Config, comptime printLineFromFile: var, ) !void { tty_config.setColor(out_stream, .White); if (line_info) |*li| { try out_stream.print("{}:{}:{}", .{ li.file_name, li.line, li.column }); } else { try out_stream.print("???:?:?", .{}); } tty_config.setColor(out_stream, .Reset); try out_stream.write(": "); tty_config.setColor(out_stream, .Dim); try out_stream.print("0x{x} in {} ({})", .{ address, symbol_name, compile_unit_name }); tty_config.setColor(out_stream, .Reset); try out_stream.write("\n"); // Show the matching source code line if possible if (line_info) |li| { if (noasync printLineFromFile(out_stream, li)) { if (li.column > 0) { // The caret already takes one char const space_needed = @intCast(usize, li.column - 1); try out_stream.writeByteNTimes(' ', space_needed); tty_config.setColor(out_stream, .Green); try out_stream.write("^"); tty_config.setColor(out_stream, .Reset); } try out_stream.write("\n"); } else |err| switch (err) { error.EndOfFile, error.FileNotFound => {}, error.BadPathName => {}, else => return err, } } } // TODO use this pub const OpenSelfDebugInfoError = error{ MissingDebugInfo, OutOfMemory, UnsupportedOperatingSystem, }; /// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented, /// make this `noasync fn` and remove the individual noasync calls. pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { if (builtin.strip_debug_info) return error.MissingDebugInfo; if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) { return noasync root.os.debug.openSelfDebugInfo(allocator); } if (builtin.os == .windows) { return noasync openSelfDebugInfoWindows(allocator); } if (comptime std.Target.current.isDarwin()) { return noasync openSelfDebugInfoMacOs(allocator); } return noasync openSelfDebugInfoPosix(allocator); } fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { const self_file = try fs.openSelfExe(); defer self_file.close(); const coff_obj = try allocator.create(coff.Coff); coff_obj.* = coff.Coff.init(allocator, self_file); var di = DebugInfo{ .coff = coff_obj, .pdb = undefined, .sect_contribs = undefined, .modules = undefined, }; try di.coff.loadHeader(); var path_buf: [windows.MAX_PATH]u8 = undefined; const len = try di.coff.getPdbPath(path_buf[0..]); const raw_path = path_buf[0..len]; const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path}); try di.pdb.openFile(di.coff, path); var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo; const version = try pdb_stream.stream.readIntLittle(u32); const signature = try pdb_stream.stream.readIntLittle(u32); const age = try pdb_stream.stream.readIntLittle(u32); var guid: [16]u8 = undefined; try pdb_stream.stream.readNoEof(&guid); if (version != 20000404) // VC70, only value observed by LLVM team return error.UnknownPDBVersion; if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age) return error.PDBMismatch; // We validated the executable and pdb match. const string_table_index = str_tab_index: { const name_bytes_len = try pdb_stream.stream.readIntLittle(u32); const name_bytes = try allocator.alloc(u8, name_bytes_len); try pdb_stream.stream.readNoEof(name_bytes); const HashTableHeader = packed struct { Size: u32, Capacity: u32, fn maxLoad(cap: u32) u32 { return cap * 2 / 3 + 1; } }; const hash_tbl_hdr = try pdb_stream.stream.readStruct(HashTableHeader); if (hash_tbl_hdr.Capacity == 0) return error.InvalidDebugInfo; if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity)) return error.InvalidDebugInfo; const present = try readSparseBitVector(&pdb_stream.stream, allocator); if (present.len != hash_tbl_hdr.Size) return error.InvalidDebugInfo; const deleted = try readSparseBitVector(&pdb_stream.stream, allocator); const Bucket = struct { first: u32, second: u32, }; const bucket_list = try allocator.alloc(Bucket, present.len); for (present) |_| { const name_offset = try pdb_stream.stream.readIntLittle(u32); const name_index = try pdb_stream.stream.readIntLittle(u32); const name = mem.toSlice(u8, @ptrCast([*:0]u8, name_bytes.ptr + name_offset)); if (mem.eql(u8, name, "/names")) { break :str_tab_index name_index; } } return error.MissingDebugInfo; }; di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo; di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo; const dbi = di.pdb.dbi; // Dbi Header const dbi_stream_header = try dbi.stream.readStruct(pdb.DbiStreamHeader); if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team return error.UnknownPDBVersion; if (dbi_stream_header.Age != age) return error.UnmatchingPDB; const mod_info_size = dbi_stream_header.ModInfoSize; const section_contrib_size = dbi_stream_header.SectionContributionSize; var modules = ArrayList(Module).init(allocator); // Module Info Substream var mod_info_offset: usize = 0; while (mod_info_offset != mod_info_size) { const mod_info = try dbi.stream.readStruct(pdb.ModInfo); var this_record_len: usize = @sizeOf(pdb.ModInfo); const module_name = try dbi.readNullTermString(allocator); this_record_len += module_name.len + 1; const obj_file_name = try dbi.readNullTermString(allocator); this_record_len += obj_file_name.len + 1; if (this_record_len % 4 != 0) { const round_to_next_4 = (this_record_len | 0x3) + 1; const march_forward_bytes = round_to_next_4 - this_record_len; try dbi.seekBy(@intCast(isize, march_forward_bytes)); this_record_len += march_forward_bytes; } try modules.append(Module{ .mod_info = mod_info, .module_name = module_name, .obj_file_name = obj_file_name, .populated = false, .symbols = undefined, .subsect_info = undefined, .checksum_offset = null, }); mod_info_offset += this_record_len; if (mod_info_offset > mod_info_size) return error.InvalidDebugInfo; } di.modules = modules.toOwnedSlice(); // Section Contribution Substream var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator); var sect_cont_offset: usize = 0; if (section_contrib_size != 0) { const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.stream.readIntLittle(u32)); if (ver != pdb.SectionContrSubstreamVersion.Ver60) return error.InvalidDebugInfo; sect_cont_offset += @sizeOf(u32); } while (sect_cont_offset != section_contrib_size) { const entry = try sect_contribs.addOne(); entry.* = try dbi.stream.readStruct(pdb.SectionContribEntry); sect_cont_offset += @sizeOf(pdb.SectionContribEntry); if (sect_cont_offset > section_contrib_size) return error.InvalidDebugInfo; } di.sect_contribs = sect_contribs.toOwnedSlice(); return di; } fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize { const num_words = try stream.readIntLittle(u32); var word_i: usize = 0; var list = ArrayList(usize).init(allocator); while (word_i != num_words) : (word_i += 1) { const word = try stream.readIntLittle(u32); var bit_i: u5 = 0; while (true) : (bit_i += 1) { if (word & (@as(u32, 1) << bit_i) != 0) { try list.append(word_i * 32 + bit_i); } if (bit_i == maxInt(u5)) break; } } return list.toOwnedSlice(); } fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DwarfInfo.Section { const elf_header = (try elf_file.findSection(name)) orelse return null; return DwarfInfo.Section{ .offset = elf_header.offset, .size = elf_header.size, }; } /// Initialize DWARF info. The caller has the responsibility to initialize most /// the DwarfInfo fields before calling. These fields can be left undefined: /// * abbrev_table_list /// * compile_unit_list pub fn openDwarfDebugInfo(di: *DwarfInfo, allocator: *mem.Allocator) !void { di.abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator); di.compile_unit_list = ArrayList(CompileUnit).init(allocator); di.func_list = ArrayList(Func).init(allocator); try di.scanAllFunctions(); try di.scanAllCompileUnits(); } pub fn openElfDebugInfo( allocator: *mem.Allocator, data: []u8, ) !DwarfInfo { var seekable_stream = io.SliceSeekableInStream.init(data); var efile = try elf.Elf.openStream( allocator, @ptrCast(*DwarfSeekableStream, &seekable_stream.seekable_stream), @ptrCast(*DwarfInStream, &seekable_stream.stream), ); defer efile.close(); const debug_info = (try efile.findSection(".debug_info")) orelse return error.MissingDebugInfo; const debug_abbrev = (try efile.findSection(".debug_abbrev")) orelse return error.MissingDebugInfo; const debug_str = (try efile.findSection(".debug_str")) orelse return error.MissingDebugInfo; const debug_line = (try efile.findSection(".debug_line")) orelse return error.MissingDebugInfo; const opt_debug_ranges = try efile.findSection(".debug_ranges"); var di = DwarfInfo{ .endian = efile.endian, .debug_info = (data[@intCast(usize, debug_info.offset)..@intCast(usize, debug_info.offset + debug_info.size)]), .debug_abbrev = (data[@intCast(usize, debug_abbrev.offset)..@intCast(usize, debug_abbrev.offset + debug_abbrev.size)]), .debug_str = (data[@intCast(usize, debug_str.offset)..@intCast(usize, debug_str.offset + debug_str.size)]), .debug_line = (data[@intCast(usize, debug_line.offset)..@intCast(usize, debug_line.offset + debug_line.size)]), .debug_ranges = if (opt_debug_ranges) |debug_ranges| data[@intCast(usize, debug_ranges.offset)..@intCast(usize, debug_ranges.offset + debug_ranges.size)] else null, }; efile.close(); try openDwarfDebugInfo(&di, allocator); return di; } fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { var exe_file = try fs.openSelfExe(); errdefer exe_file.close(); const exe_len = math.cast(usize, try exe_file.getEndPos()) catch return error.DebugInfoTooLarge; const exe_mmap = try os.mmap( null, exe_len, os.PROT_READ, os.MAP_SHARED, exe_file.handle, 0, ); errdefer os.munmap(exe_mmap); return openElfDebugInfo(allocator, exe_mmap); } fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo { const hdr = &std.c._mh_execute_header; assert(hdr.magic == std.macho.MH_MAGIC_64); const hdr_base = @ptrCast([*]u8, hdr); var ptr = hdr_base + @sizeOf(macho.mach_header_64); var ncmd: u32 = hdr.ncmds; const symtab = while (ncmd != 0) : (ncmd -= 1) { const lc = @ptrCast(*std.macho.load_command, ptr); switch (lc.cmd) { std.macho.LC_SYMTAB => break @ptrCast(*std.macho.symtab_command, ptr), else => {}, } ptr = @alignCast(@alignOf(std.macho.load_command), ptr + lc.cmdsize); } else { return error.MissingDebugInfo; }; const syms = @ptrCast([*]macho.nlist_64, @alignCast(@alignOf(macho.nlist_64), hdr_base + symtab.symoff))[0..symtab.nsyms]; const strings = @ptrCast([*]u8, hdr_base + symtab.stroff)[0..symtab.strsize]; const symbols_buf = try allocator.alloc(MachoSymbol, syms.len); var ofile: ?*macho.nlist_64 = null; var reloc: u64 = 0; var symbol_index: usize = 0; var last_len: u64 = 0; for (syms) |*sym| { if (sym.n_type & std.macho.N_STAB != 0) { switch (sym.n_type) { std.macho.N_OSO => { ofile = sym; reloc = 0; }, std.macho.N_FUN => { if (sym.n_sect == 0) { last_len = sym.n_value; } else { symbols_buf[symbol_index] = MachoSymbol{ .nlist = sym, .ofile = ofile, .reloc = reloc, }; symbol_index += 1; } }, std.macho.N_BNSYM => { if (reloc == 0) { reloc = sym.n_value; } }, else => continue, } } } const sentinel = try allocator.create(macho.nlist_64); sentinel.* = macho.nlist_64{ .n_strx = 0, .n_type = 36, .n_sect = 0, .n_desc = 0, .n_value = symbols_buf[symbol_index - 1].nlist.n_value + last_len, }; const symbols = allocator.shrink(symbols_buf, symbol_index); // Even though lld emits symbols in ascending order, this debug code // should work for programs linked in any valid way. // This sort is so that we can binary search later. std.sort.sort(MachoSymbol, symbols, MachoSymbol.addressLessThan); return DebugInfo{ .ofiles = DebugInfo.OFileTable.init(allocator), .symbols = symbols, .strings = strings, }; } fn printLineFromFileAnyOs(out_stream: var, line_info: LineInfo) !void { var f = try fs.cwd().openFile(line_info.file_name, .{}); defer f.close(); // TODO fstat and make sure that the file has the correct size var buf: [mem.page_size]u8 = undefined; var line: usize = 1; var column: usize = 1; var abs_index: usize = 0; while (true) { const amt_read = try f.read(buf[0..]); const slice = buf[0..amt_read]; for (slice) |byte| { if (line == line_info.line) { try out_stream.writeByte(byte); if (byte == '\n') { return; } } if (byte == '\n') { line += 1; column = 1; } else { column += 1; } } if (amt_read < buf.len) return error.EndOfFile; } } const MachoSymbol = struct { nlist: *macho.nlist_64, ofile: ?*macho.nlist_64, reloc: u64, /// Returns the address from the macho file fn address(self: MachoSymbol) u64 { return self.nlist.n_value; } fn addressLessThan(lhs: MachoSymbol, rhs: MachoSymbol) bool { return lhs.address() < rhs.address(); } }; pub const DwarfSeekableStream = io.SeekableStream(anyerror, anyerror); pub const DwarfInStream = io.InStream(anyerror); pub const DwarfInfo = struct { endian: builtin.Endian, // No memory is owned by the DwarfInfo debug_info: []u8, debug_abbrev: []u8, debug_str: []u8, debug_line: []u8, debug_ranges: ?[]u8, // Filled later by the initializer abbrev_table_list: ArrayList(AbbrevTableHeader) = undefined, compile_unit_list: ArrayList(CompileUnit) = undefined, func_list: ArrayList(Func) = undefined, pub fn allocator(self: DwarfInfo) *mem.Allocator { return self.abbrev_table_list.allocator; } /// This function works in freestanding mode. /// fn printLineFromFile(out_stream: var, line_info: LineInfo) !void pub fn printSourceAtAddress( self: *DwarfInfo, out_stream: var, address: usize, tty_config: TTY.Config, comptime printLineFromFile: var, ) !void { const compile_unit = self.findCompileUnit(address) catch { return printLineInfo(out_stream, null, address, "???", "???", tty_config, printLineFromFile); }; const compile_unit_name = try compile_unit.die.getAttrString(self, DW.AT_name); const symbol_name = self.getSymbolName(address) orelse "???"; const line_info = self.getLineNumberInfo(compile_unit.*, address) catch |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => null, else => return err, }; defer if (line_info) |li| li.deinit(); try printLineInfo( out_stream, line_info, address, symbol_name, compile_unit_name, tty_config, printLineFromFile, ); } fn getSymbolName(di: *DwarfInfo, address: u64) ?[]const u8 { for (di.func_list.toSliceConst()) |*func| { if (func.pc_range) |range| { if (address >= range.start and address < range.end) { return func.name; } } } return null; } fn scanAllFunctions(di: *DwarfInfo) !void { var s = io.SliceSeekableInStream.init(di.debug_info); var this_unit_offset: u64 = 0; while (true) { s.seekable_stream.seekTo(this_unit_offset) catch |err| switch (err) { error.EndOfStream => return, else => return err, }; var is_64: bool = undefined; const unit_length = try readInitialLength(@TypeOf(s.stream.readFn).ReturnType.ErrorSet, &s.stream, &is_64); if (unit_length == 0) return; const next_offset = unit_length + (if (is_64) @as(usize, 12) else @as(usize, 4)); const version = try s.stream.readInt(u16, di.endian); if (version < 2 or version > 5) return error.InvalidDebugInfo; const debug_abbrev_offset = if (is_64) try s.stream.readInt(u64, di.endian) else try s.stream.readInt(u32, di.endian); const address_size = try s.stream.readByte(); if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; const compile_unit_pos = try s.seekable_stream.getPos(); const abbrev_table = try di.getAbbrevTable(debug_abbrev_offset); try s.seekable_stream.seekTo(compile_unit_pos); const next_unit_pos = this_unit_offset + next_offset; while ((try s.seekable_stream.getPos()) < next_unit_pos) { const die_obj = (try di.parseDie(&s.stream, abbrev_table, is_64)) orelse continue; const after_die_offset = try s.seekable_stream.getPos(); switch (die_obj.tag_id) { DW.TAG_subprogram, DW.TAG_inlined_subroutine, DW.TAG_subroutine, DW.TAG_entry_point => { const fn_name = x: { var depth: i32 = 3; var this_die_obj = die_obj; // Prenvent endless loops while (depth > 0) : (depth -= 1) { if (this_die_obj.getAttr(DW.AT_name)) |_| { const name = try this_die_obj.getAttrString(di, DW.AT_name); break :x name; } else if (this_die_obj.getAttr(DW.AT_abstract_origin)) |ref| { // Follow the DIE it points to and repeat const ref_offset = try this_die_obj.getAttrRef(DW.AT_abstract_origin); if (ref_offset > next_offset) return error.InvalidDebugInfo; try s.seekable_stream.seekTo(this_unit_offset + ref_offset); this_die_obj = (try di.parseDie(&s.stream, abbrev_table, is_64)) orelse return error.InvalidDebugInfo; } else if (this_die_obj.getAttr(DW.AT_specification)) |ref| { // Follow the DIE it points to and repeat const ref_offset = try this_die_obj.getAttrRef(DW.AT_specification); if (ref_offset > next_offset) return error.InvalidDebugInfo; try s.seekable_stream.seekTo(this_unit_offset + ref_offset); this_die_obj = (try di.parseDie(&s.stream, abbrev_table, is_64)) orelse return error.InvalidDebugInfo; } else { break :x null; } } break :x null; }; const pc_range = x: { if (die_obj.getAttrAddr(DW.AT_low_pc)) |low_pc| { if (die_obj.getAttr(DW.AT_high_pc)) |high_pc_value| { const pc_end = switch (high_pc_value.*) { FormValue.Address => |value| value, FormValue.Const => |value| b: { const offset = try value.asUnsignedLe(); break :b (low_pc + offset); }, else => return error.InvalidDebugInfo, }; break :x PcRange{ .start = low_pc, .end = pc_end, }; } else { break :x null; } } else |err| { if (err != error.MissingDebugInfo) return err; break :x null; } }; try di.func_list.append(Func{ .name = fn_name, .pc_range = pc_range, }); }, else => {}, } try s.seekable_stream.seekTo(after_die_offset); } this_unit_offset += next_offset; } } fn scanAllCompileUnits(di: *DwarfInfo) !void { var s = io.SliceSeekableInStream.init(di.debug_info); var this_unit_offset: u64 = 0; while (true) { s.seekable_stream.seekTo(this_unit_offset) catch |err| switch (err) { error.EndOfStream => return, else => return err, }; var is_64: bool = undefined; const unit_length = try readInitialLength(@TypeOf(s.stream.readFn).ReturnType.ErrorSet, &s.stream, &is_64); if (unit_length == 0) return; const next_offset = unit_length + (if (is_64) @as(usize, 12) else @as(usize, 4)); const version = try s.stream.readInt(u16, di.endian); if (version < 2 or version > 5) return error.InvalidDebugInfo; const debug_abbrev_offset = if (is_64) try s.stream.readInt(u64, di.endian) else try s.stream.readInt(u32, di.endian); const address_size = try s.stream.readByte(); if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; const compile_unit_pos = try s.seekable_stream.getPos(); const abbrev_table = try di.getAbbrevTable(debug_abbrev_offset); try s.seekable_stream.seekTo(compile_unit_pos); const compile_unit_die = try di.allocator().create(Die); compile_unit_die.* = (try di.parseDie(&s.stream, abbrev_table, is_64)) orelse return error.InvalidDebugInfo; if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo; const pc_range = x: { if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| { if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| { const pc_end = switch (high_pc_value.*) { FormValue.Address => |value| value, FormValue.Const => |value| b: { const offset = try value.asUnsignedLe(); break :b (low_pc + offset); }, else => return error.InvalidDebugInfo, }; break :x PcRange{ .start = low_pc, .end = pc_end, }; } else { break :x null; } } else |err| { if (err != error.MissingDebugInfo) return err; break :x null; } }; try di.compile_unit_list.append(CompileUnit{ .version = version, .is_64 = is_64, .pc_range = pc_range, .die = compile_unit_die, }); this_unit_offset += next_offset; } } fn findCompileUnit(di: *DwarfInfo, target_address: u64) !*const CompileUnit { for (di.compile_unit_list.toSlice()) |*compile_unit| { if (compile_unit.pc_range) |range| { if (target_address >= range.start and target_address < range.end) return compile_unit; } if (di.debug_ranges) |debug_ranges| { if (compile_unit.die.getAttrSecOffset(DW.AT_ranges)) |ranges_offset| { var s = io.SliceSeekableInStream.init(debug_ranges); // All the addresses in the list are relative to the value // specified by DW_AT_low_pc or to some other value encoded // in the list itself var base_address = try compile_unit.die.getAttrAddr(DW.AT_low_pc); try s.seekable_stream.seekTo(ranges_offset); while (true) { const begin_addr = try s.stream.readIntLittle(usize); const end_addr = try s.stream.readIntLittle(usize); if (begin_addr == 0 and end_addr == 0) { break; } // This entry selects a new value for the base address if (begin_addr == maxInt(usize)) { base_address = end_addr; continue; } if (target_address >= base_address + begin_addr and target_address < base_address + end_addr) { return compile_unit; } } return error.InvalidDebugInfo; } else |err| { if (err != error.MissingDebugInfo) return err; continue; } } } return error.MissingDebugInfo; } /// Gets an already existing AbbrevTable given the abbrev_offset, or if not found, /// seeks in the stream and parses it. fn getAbbrevTable(di: *DwarfInfo, abbrev_offset: u64) !*const AbbrevTable { for (di.abbrev_table_list.toSlice()) |*header| { if (header.offset == abbrev_offset) { return &header.table; } } try di.abbrev_table_list.append(AbbrevTableHeader{ .offset = abbrev_offset, .table = try di.parseAbbrevTable(abbrev_offset), }); return &di.abbrev_table_list.items[di.abbrev_table_list.len - 1].table; } fn parseAbbrevTable(di: *DwarfInfo, offset: u64) !AbbrevTable { var s = io.SliceSeekableInStream.init(di.debug_abbrev); try s.seekable_stream.seekTo(offset); var result = AbbrevTable.init(di.allocator()); errdefer result.deinit(); while (true) { const abbrev_code = try leb.readULEB128(u64, &s.stream); if (abbrev_code == 0) return result; try result.append(AbbrevTableEntry{ .abbrev_code = abbrev_code, .tag_id = try leb.readULEB128(u64, &s.stream), .has_children = (try s.stream.readByte()) == DW.CHILDREN_yes, .attrs = ArrayList(AbbrevAttr).init(di.allocator()), }); const attrs = &result.items[result.len - 1].attrs; while (true) { const attr_id = try leb.readULEB128(u64, &s.stream); const form_id = try leb.readULEB128(u64, &s.stream); if (attr_id == 0 and form_id == 0) break; try attrs.append(AbbrevAttr{ .attr_id = attr_id, .form_id = form_id, }); } } } fn parseDie(di: *DwarfInfo, in_stream: var, abbrev_table: *const AbbrevTable, is_64: bool) !?Die { const abbrev_code = try leb.readULEB128(u64, in_stream); if (abbrev_code == 0) return null; const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) orelse return error.InvalidDebugInfo; var result = Die{ .tag_id = table_entry.tag_id, .has_children = table_entry.has_children, .attrs = ArrayList(Die.Attr).init(di.allocator()), }; try result.attrs.resize(table_entry.attrs.len); for (table_entry.attrs.toSliceConst()) |attr, i| { result.attrs.items[i] = Die.Attr{ .id = attr.attr_id, .value = try parseFormValue(di.allocator(), in_stream, attr.form_id, is_64), }; } return result; } fn getLineNumberInfo(di: *DwarfInfo, compile_unit: CompileUnit, target_address: usize) !LineInfo { var s = io.SliceSeekableInStream.init(di.debug_line); const compile_unit_cwd = try compile_unit.die.getAttrString(di, DW.AT_comp_dir); const line_info_offset = try compile_unit.die.getAttrSecOffset(DW.AT_stmt_list); try s.seekable_stream.seekTo(line_info_offset); var is_64: bool = undefined; const unit_length = try readInitialLength(@TypeOf(s.stream.readFn).ReturnType.ErrorSet, &s.stream, &is_64); if (unit_length == 0) { return error.MissingDebugInfo; } const next_offset = unit_length + (if (is_64) @as(usize, 12) else @as(usize, 4)); const version = try s.stream.readInt(u16, di.endian); // TODO support 3 and 5 if (version != 2 and version != 4) return error.InvalidDebugInfo; const prologue_length = if (is_64) try s.stream.readInt(u64, di.endian) else try s.stream.readInt(u32, di.endian); const prog_start_offset = (try s.seekable_stream.getPos()) + prologue_length; const minimum_instruction_length = try s.stream.readByte(); if (minimum_instruction_length == 0) return error.InvalidDebugInfo; if (version >= 4) { // maximum_operations_per_instruction _ = try s.stream.readByte(); } const default_is_stmt = (try s.stream.readByte()) != 0; const line_base = try s.stream.readByteSigned(); const line_range = try s.stream.readByte(); if (line_range == 0) return error.InvalidDebugInfo; const opcode_base = try s.stream.readByte(); const standard_opcode_lengths = try di.allocator().alloc(u8, opcode_base - 1); { var i: usize = 0; while (i < opcode_base - 1) : (i += 1) { standard_opcode_lengths[i] = try s.stream.readByte(); } } var include_directories = ArrayList([]u8).init(di.allocator()); try include_directories.append(compile_unit_cwd); while (true) { const dir = try readStringRaw(di.allocator(), &s.stream); if (dir.len == 0) break; try include_directories.append(dir); } var file_entries = ArrayList(FileEntry).init(di.allocator()); var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(), &file_entries, target_address); while (true) { const file_name = try readStringRaw(di.allocator(), &s.stream); if (file_name.len == 0) break; const dir_index = try leb.readULEB128(usize, &s.stream); const mtime = try leb.readULEB128(usize, &s.stream); const len_bytes = try leb.readULEB128(usize, &s.stream); try file_entries.append(FileEntry{ .file_name = file_name, .dir_index = dir_index, .mtime = mtime, .len_bytes = len_bytes, }); } try s.seekable_stream.seekTo(prog_start_offset); const next_unit_pos = line_info_offset + next_offset; while ((try s.seekable_stream.getPos()) < next_unit_pos) { const opcode = try s.stream.readByte(); if (opcode == DW.LNS_extended_op) { const op_size = try leb.readULEB128(u64, &s.stream); if (op_size < 1) return error.InvalidDebugInfo; var sub_op = try s.stream.readByte(); switch (sub_op) { DW.LNE_end_sequence => { prog.end_sequence = true; if (try prog.checkLineMatch()) |info| return info; prog.reset(); }, DW.LNE_set_address => { const addr = try s.stream.readInt(usize, di.endian); prog.address = addr; }, DW.LNE_define_file => { const file_name = try readStringRaw(di.allocator(), &s.stream); const dir_index = try leb.readULEB128(usize, &s.stream); const mtime = try leb.readULEB128(usize, &s.stream); const len_bytes = try leb.readULEB128(usize, &s.stream); try file_entries.append(FileEntry{ .file_name = file_name, .dir_index = dir_index, .mtime = mtime, .len_bytes = len_bytes, }); }, else => { const fwd_amt = math.cast(isize, op_size - 1) catch return error.InvalidDebugInfo; try s.seekable_stream.seekBy(fwd_amt); }, } } else if (opcode >= opcode_base) { // special opcodes const adjusted_opcode = opcode - opcode_base; const inc_addr = minimum_instruction_length * (adjusted_opcode / line_range); const inc_line = @as(i32, line_base) + @as(i32, adjusted_opcode % line_range); prog.line += inc_line; prog.address += inc_addr; if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; } else { switch (opcode) { DW.LNS_copy => { if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; }, DW.LNS_advance_pc => { const arg = try leb.readULEB128(usize, &s.stream); prog.address += arg * minimum_instruction_length; }, DW.LNS_advance_line => { const arg = try leb.readILEB128(i64, &s.stream); prog.line += arg; }, DW.LNS_set_file => { const arg = try leb.readULEB128(usize, &s.stream); prog.file = arg; }, DW.LNS_set_column => { const arg = try leb.readULEB128(u64, &s.stream); prog.column = arg; }, DW.LNS_negate_stmt => { prog.is_stmt = !prog.is_stmt; }, DW.LNS_set_basic_block => { prog.basic_block = true; }, DW.LNS_const_add_pc => { const inc_addr = minimum_instruction_length * ((255 - opcode_base) / line_range); prog.address += inc_addr; }, DW.LNS_fixed_advance_pc => { const arg = try s.stream.readInt(u16, di.endian); prog.address += arg; }, DW.LNS_set_prologue_end => {}, else => { if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo; const len_bytes = standard_opcode_lengths[opcode - 1]; try s.seekable_stream.seekBy(len_bytes); }, } } } return error.MissingDebugInfo; } fn getString(di: *DwarfInfo, offset: u64) ![]u8 { if (offset > di.debug_str.len) return error.InvalidDebugInfo; const casted_offset = math.cast(usize, offset) catch return error.InvalidDebugInfo; // Valid strings always have a terminating zero byte if (mem.indexOfScalarPos(u8, di.debug_str, casted_offset, 0)) |last| { return di.debug_str[casted_offset..last]; } return error.InvalidDebugInfo; } }; pub const DebugInfo = switch (builtin.os) { .macosx, .ios, .watchos, .tvos => struct { symbols: []const MachoSymbol, strings: []const u8, ofiles: OFileTable, const OFileTable = std.HashMap( *macho.nlist_64, DwarfInfo, std.hash_map.getHashPtrAddrFn(*macho.nlist_64), std.hash_map.getTrivialEqlFn(*macho.nlist_64), ); pub fn allocator(self: DebugInfo) *mem.Allocator { return self.ofiles.allocator; } }, .uefi, .windows => struct { pdb: pdb.Pdb, coff: *coff.Coff, sect_contribs: []pdb.SectionContribEntry, modules: []Module, }, else => DwarfInfo, }; const PcRange = struct { start: u64, end: u64, }; const CompileUnit = struct { version: u16, is_64: bool, die: *Die, pc_range: ?PcRange, }; const AbbrevTable = ArrayList(AbbrevTableEntry); const AbbrevTableHeader = struct { // offset from .debug_abbrev offset: u64, table: AbbrevTable, }; const AbbrevTableEntry = struct { has_children: bool, abbrev_code: u64, tag_id: u64, attrs: ArrayList(AbbrevAttr), }; const AbbrevAttr = struct { attr_id: u64, form_id: u64, }; const FormValue = union(enum) { Address: u64, Block: []u8, Const: Constant, ExprLoc: []u8, Flag: bool, SecOffset: u64, Ref: u64, RefAddr: u64, String: []u8, StrPtr: u64, }; const Constant = struct { payload: u64, signed: bool, fn asUnsignedLe(self: *const Constant) !u64 { if (self.signed) return error.InvalidDebugInfo; return self.payload; } }; const Die = struct { tag_id: u64, has_children: bool, attrs: ArrayList(Attr), const Attr = struct { id: u64, value: FormValue, }; fn getAttr(self: *const Die, id: u64) ?*const FormValue { for (self.attrs.toSliceConst()) |*attr| { if (attr.id == id) return &attr.value; } return null; } fn getAttrAddr(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Address => |value| value, else => error.InvalidDebugInfo, }; } fn getAttrSecOffset(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Const => |value| value.asUnsignedLe(), FormValue.SecOffset => |value| value, else => error.InvalidDebugInfo, }; } fn getAttrUnsignedLe(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Const => |value| value.asUnsignedLe(), else => error.InvalidDebugInfo, }; } fn getAttrRef(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Ref => |value| value, else => error.InvalidDebugInfo, }; } fn getAttrString(self: *const Die, di: *DwarfInfo, id: u64) ![]u8 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.String => |value| value, FormValue.StrPtr => |offset| di.getString(offset), else => error.InvalidDebugInfo, }; } }; const FileEntry = struct { file_name: []const u8, dir_index: usize, mtime: usize, len_bytes: usize, }; pub const LineInfo = struct { line: u64, column: u64, file_name: []const u8, allocator: ?*mem.Allocator, fn deinit(self: LineInfo) void { const allocator = self.allocator orelse return; allocator.free(self.file_name); } }; const LineNumberProgram = struct { address: usize, file: usize, line: i64, column: u64, is_stmt: bool, basic_block: bool, end_sequence: bool, default_is_stmt: bool, target_address: usize, include_dirs: []const []const u8, file_entries: *ArrayList(FileEntry), prev_address: usize, prev_file: usize, prev_line: i64, prev_column: u64, prev_is_stmt: bool, prev_basic_block: bool, prev_end_sequence: bool, // Reset the state machine following the DWARF specification pub fn reset(self: *LineNumberProgram) void { self.address = 0; self.file = 1; self.line = 1; self.column = 0; self.is_stmt = self.default_is_stmt; self.basic_block = false; self.end_sequence = false; // Invalidate all the remaining fields self.prev_address = 0; self.prev_file = undefined; self.prev_line = undefined; self.prev_column = undefined; self.prev_is_stmt = undefined; self.prev_basic_block = undefined; self.prev_end_sequence = undefined; } pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: *ArrayList(FileEntry), target_address: usize) LineNumberProgram { return LineNumberProgram{ .address = 0, .file = 1, .line = 1, .column = 0, .is_stmt = is_stmt, .basic_block = false, .end_sequence = false, .include_dirs = include_dirs, .file_entries = file_entries, .default_is_stmt = is_stmt, .target_address = target_address, .prev_address = 0, .prev_file = undefined, .prev_line = undefined, .prev_column = undefined, .prev_is_stmt = undefined, .prev_basic_block = undefined, .prev_end_sequence = undefined, }; } pub fn checkLineMatch(self: *LineNumberProgram) !?LineInfo { if (self.target_address >= self.prev_address and self.target_address < self.address) { const file_entry = if (self.prev_file == 0) { return error.MissingDebugInfo; } else if (self.prev_file - 1 >= self.file_entries.len) { return error.InvalidDebugInfo; } else &self.file_entries.items[self.prev_file - 1]; const dir_name = if (file_entry.dir_index >= self.include_dirs.len) { return error.InvalidDebugInfo; } else self.include_dirs[file_entry.dir_index]; const file_name = try fs.path.join(self.file_entries.allocator, &[_][]const u8{ dir_name, file_entry.file_name }); errdefer self.file_entries.allocator.free(file_name); return LineInfo{ .line = if (self.prev_line >= 0) @intCast(u64, self.prev_line) else 0, .column = self.prev_column, .file_name = file_name, .allocator = self.file_entries.allocator, }; } self.prev_address = self.address; self.prev_file = self.file; self.prev_line = self.line; self.prev_column = self.column; self.prev_is_stmt = self.is_stmt; self.prev_basic_block = self.basic_block; self.prev_end_sequence = self.end_sequence; return null; } }; // TODO the noasyncs here are workarounds fn readStringRaw(allocator: *mem.Allocator, in_stream: var) ![]u8 { var buf = ArrayList(u8).init(allocator); while (true) { const byte = try noasync in_stream.readByte(); if (byte == 0) break; try buf.append(byte); } return buf.toSlice(); } // TODO the noasyncs here are workarounds fn readAllocBytes(allocator: *mem.Allocator, in_stream: var, size: usize) ![]u8 { const buf = try allocator.alloc(u8, size); errdefer allocator.free(buf); if ((try noasync in_stream.read(buf)) < size) return error.EndOfFile; return buf; } fn parseFormValueBlockLen(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue { const buf = try readAllocBytes(allocator, in_stream, size); return FormValue{ .Block = buf }; } // TODO the noasyncs here are workarounds fn parseFormValueBlock(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue { const block_len = try noasync in_stream.readVarInt(usize, builtin.Endian.Little, size); return parseFormValueBlockLen(allocator, in_stream, block_len); } fn parseFormValueConstant(allocator: *mem.Allocator, in_stream: var, signed: bool, comptime size: i32) !FormValue { // TODO: Please forgive me, I've worked around zig not properly spilling some intermediate values here. // `noasync` should be removed from all the function calls once it is fixed. return FormValue{ .Const = Constant{ .signed = signed, .payload = switch (size) { 1 => try noasync in_stream.readIntLittle(u8), 2 => try noasync in_stream.readIntLittle(u16), 4 => try noasync in_stream.readIntLittle(u32), 8 => try noasync in_stream.readIntLittle(u64), -1 => blk: { if (signed) { const x = try noasync leb.readILEB128(i64, in_stream); break :blk @bitCast(u64, x); } else { const x = try noasync leb.readULEB128(u64, in_stream); break :blk x; } }, else => @compileError("Invalid size"), }, }, }; } // TODO the noasyncs here are workarounds fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 { return if (is_64) try noasync in_stream.readIntLittle(u64) else @as(u64, try noasync in_stream.readIntLittle(u32)); } // TODO the noasyncs here are workarounds fn parseFormValueTargetAddrSize(in_stream: var) !u64 { if (@sizeOf(usize) == 4) { // TODO this cast should not be needed return @as(u64, try noasync in_stream.readIntLittle(u32)); } else if (@sizeOf(usize) == 8) { return noasync in_stream.readIntLittle(u64); } else { unreachable; } } // TODO the noasyncs here are workarounds fn parseFormValueRef(allocator: *mem.Allocator, in_stream: var, size: i32) !FormValue { return FormValue{ .Ref = switch (size) { 1 => try noasync in_stream.readIntLittle(u8), 2 => try noasync in_stream.readIntLittle(u16), 4 => try noasync in_stream.readIntLittle(u32), 8 => try noasync in_stream.readIntLittle(u64), -1 => try noasync leb.readULEB128(u64, in_stream), else => unreachable, }, }; } // TODO the noasyncs here are workarounds fn parseFormValue(allocator: *mem.Allocator, in_stream: var, form_id: u64, is_64: bool) anyerror!FormValue { return switch (form_id) { DW.FORM_addr => FormValue{ .Address = try parseFormValueTargetAddrSize(in_stream) }, DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1), DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2), DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4), DW.FORM_block => x: { const block_len = try noasync leb.readULEB128(usize, in_stream); return parseFormValueBlockLen(allocator, in_stream, block_len); }, DW.FORM_data1 => parseFormValueConstant(allocator, in_stream, false, 1), DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2), DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4), DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8), DW.FORM_udata, DW.FORM_sdata => { const signed = form_id == DW.FORM_sdata; return parseFormValueConstant(allocator, in_stream, signed, -1); }, DW.FORM_exprloc => { const size = try noasync leb.readULEB128(usize, in_stream); const buf = try readAllocBytes(allocator, in_stream, size); return FormValue{ .ExprLoc = buf }; }, DW.FORM_flag => FormValue{ .Flag = (try noasync in_stream.readByte()) != 0 }, DW.FORM_flag_present => FormValue{ .Flag = true }, DW.FORM_sec_offset => FormValue{ .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, 1), DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, 2), DW.FORM_ref4 => parseFormValueRef(allocator, in_stream, 4), DW.FORM_ref8 => parseFormValueRef(allocator, in_stream, 8), DW.FORM_ref_udata => parseFormValueRef(allocator, in_stream, -1), DW.FORM_ref_addr => FormValue{ .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_ref_sig8 => FormValue{ .Ref = try noasync in_stream.readIntLittle(u64) }, DW.FORM_string => FormValue{ .String = try readStringRaw(allocator, in_stream) }, DW.FORM_strp => FormValue{ .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_indirect => { const child_form_id = try noasync leb.readULEB128(u64, in_stream); const F = @TypeOf(async parseFormValue(allocator, in_stream, child_form_id, is_64)); var frame = try allocator.create(F); defer allocator.destroy(frame); return await @asyncCall(frame, {}, parseFormValue, allocator, in_stream, child_form_id, is_64); }, else => error.InvalidDebugInfo, }; } fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*const AbbrevTableEntry { for (abbrev_table.toSliceConst()) |*table_entry| { if (table_entry.abbrev_code == abbrev_code) return table_entry; } return null; } fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, address: usize) !LineInfo { const ofile = symbol.ofile orelse return error.MissingDebugInfo; const gop = try di.ofiles.getOrPut(ofile); const dwarf_info = if (gop.found_existing) &gop.kv.value else blk: { errdefer _ = di.ofiles.remove(ofile); const ofile_path = mem.toSliceConst(u8, @ptrCast([*:0]const u8, di.strings.ptr + ofile.n_strx)); var exe_file = try std.fs.openFileAbsoluteC(ofile_path, .{}); errdefer exe_file.close(); const exe_len = math.cast(usize, try exe_file.getEndPos()) catch return error.DebugInfoTooLarge; const exe_mmap = try os.mmap( null, exe_len, os.PROT_READ, os.MAP_SHARED, exe_file.handle, 0, ); errdefer os.munmap(exe_mmap); const hdr = @ptrCast( *const macho.mach_header_64, @alignCast(@alignOf(macho.mach_header_64), exe_mmap.ptr), ); if (hdr.magic != std.macho.MH_MAGIC_64) return error.InvalidDebugInfo; const hdr_base = @ptrCast([*]const u8, hdr); var ptr = hdr_base + @sizeOf(macho.mach_header_64); var ncmd: u32 = hdr.ncmds; const segcmd = while (ncmd != 0) : (ncmd -= 1) { const lc = @ptrCast(*const std.macho.load_command, ptr); switch (lc.cmd) { std.macho.LC_SEGMENT_64 => { break @ptrCast( *const std.macho.segment_command_64, @alignCast(@alignOf(std.macho.segment_command_64), ptr), ); }, else => {}, } ptr = @alignCast(@alignOf(std.macho.load_command), ptr + lc.cmdsize); } else { return error.MissingDebugInfo; }; var opt_debug_line: ?*const macho.section_64 = null; var opt_debug_info: ?*const macho.section_64 = null; var opt_debug_abbrev: ?*const macho.section_64 = null; var opt_debug_str: ?*const macho.section_64 = null; var opt_debug_ranges: ?*const macho.section_64 = null; const sections = @ptrCast([*]const macho.section_64, @alignCast(@alignOf(macho.section_64), ptr + @sizeOf(std.macho.segment_command_64)))[0..segcmd.nsects]; for (sections) |*sect| { // The section name may not exceed 16 chars and a trailing null may // not be present const name = if (mem.indexOfScalar(u8, sect.sectname[0..], 0)) |last| sect.sectname[0..last] else sect.sectname[0..]; if (mem.eql(u8, name, "__debug_line")) { opt_debug_line = sect; } else if (mem.eql(u8, name, "__debug_info")) { opt_debug_info = sect; } else if (mem.eql(u8, name, "__debug_abbrev")) { opt_debug_abbrev = sect; } else if (mem.eql(u8, name, "__debug_str")) { opt_debug_str = sect; } else if (mem.eql(u8, name, "__debug_ranges")) { opt_debug_ranges = sect; } } var debug_line = opt_debug_line orelse return error.MissingDebugInfo; var debug_info = opt_debug_info orelse return error.MissingDebugInfo; var debug_str = opt_debug_str orelse return error.MissingDebugInfo; var debug_abbrev = opt_debug_abbrev orelse return error.MissingDebugInfo; gop.kv.value = DwarfInfo{ .endian = .Little, .debug_info = exe_mmap[@intCast(usize, debug_info.offset)..@intCast(usize, debug_info.offset + debug_info.size)], .debug_abbrev = exe_mmap[@intCast(usize, debug_abbrev.offset)..@intCast(usize, debug_abbrev.offset + debug_abbrev.size)], .debug_str = exe_mmap[@intCast(usize, debug_str.offset)..@intCast(usize, debug_str.offset + debug_str.size)], .debug_line = exe_mmap[@intCast(usize, debug_line.offset)..@intCast(usize, debug_line.offset + debug_line.size)], .debug_ranges = if (opt_debug_ranges) |debug_ranges| exe_mmap[@intCast(usize, debug_ranges.offset)..@intCast(usize, debug_ranges.offset + debug_ranges.size)] else null, }; try openDwarfDebugInfo(&gop.kv.value, di.allocator()); break :blk &gop.kv.value; }; const o_file_address = address - symbol.reloc; const compile_unit = try dwarf_info.findCompileUnit(o_file_address); return dwarf_info.getLineNumberInfo(compile_unit.*, o_file_address); } const Func = struct { pc_range: ?PcRange, name: ?[]u8, }; fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool) !u64 { const first_32_bits = try in_stream.readIntLittle(u32); is_64.* = (first_32_bits == 0xffffffff); if (is_64.*) { return in_stream.readIntLittle(u64); } else { if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo; // TODO this cast should not be needed return @as(u64, first_32_bits); } } /// This should only be used in temporary test programs. pub const global_allocator = &global_fixed_allocator.allocator; var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]); var global_allocator_mem: [100 * 1024]u8 = undefined; /// TODO multithreaded awareness var debug_info_allocator: ?*mem.Allocator = null; var debug_info_arena_allocator: std.heap.ArenaAllocator = undefined; fn getDebugInfoAllocator() *mem.Allocator { if (debug_info_allocator) |a| return a; debug_info_arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator); debug_info_allocator = &debug_info_arena_allocator.allocator; return &debug_info_arena_allocator.allocator; } /// Whether or not the current target can print useful debug information when a segfault occurs. pub const have_segfault_handling_support = builtin.os == .linux or builtin.os == .windows; pub const enable_segfault_handler: bool = if (@hasDecl(root, "enable_segfault_handler")) root.enable_segfault_handler else runtime_safety and have_segfault_handling_support; pub fn maybeEnableSegfaultHandler() void { if (enable_segfault_handler) { std.debug.attachSegfaultHandler(); } } var windows_segfault_handle: ?windows.HANDLE = null; /// Attaches a global SIGSEGV handler which calls @panic("segmentation fault"); pub fn attachSegfaultHandler() void { if (!have_segfault_handling_support) { @compileError("segfault handler not supported for this target"); } if (builtin.os == .windows) { windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows); return; } var act = os.Sigaction{ .sigaction = handleSegfaultLinux, .mask = os.empty_sigset, .flags = (os.SA_SIGINFO | os.SA_RESTART | os.SA_RESETHAND), }; os.sigaction(os.SIGSEGV, &act, null); os.sigaction(os.SIGILL, &act, null); } fn resetSegfaultHandler() void { if (builtin.os == .windows) { if (windows_segfault_handle) |handle| { assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0); windows_segfault_handle = null; } return; } var act = os.Sigaction{ .sigaction = os.SIG_DFL, .mask = os.empty_sigset, .flags = 0, }; os.sigaction(os.SIGSEGV, &act, null); os.sigaction(os.SIGILL, &act, null); } fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: *const c_void) callconv(.C) noreturn { // Reset to the default handler so that if a segfault happens in this handler it will crash // the process. Also when this handler returns, the original instruction will be repeated // and the resulting segfault will crash the process rather than continually dump stack traces. resetSegfaultHandler(); const addr = @ptrToInt(info.fields.sigfault.addr); switch (sig) { os.SIGSEGV => std.debug.warn("Segmentation fault at address 0x{x}\n", .{addr}), os.SIGILL => std.debug.warn("Illegal instruction at address 0x{x}\n", .{addr}), else => unreachable, } switch (builtin.arch) { .i386 => { const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); const ip = @intCast(usize, ctx.mcontext.gregs[os.REG_EIP]); const bp = @intCast(usize, ctx.mcontext.gregs[os.REG_EBP]); dumpStackTraceFromBase(bp, ip); }, .x86_64 => { const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); const ip = @intCast(usize, ctx.mcontext.gregs[os.REG_RIP]); const bp = @intCast(usize, ctx.mcontext.gregs[os.REG_RBP]); dumpStackTraceFromBase(bp, ip); }, .arm => { const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); const ip = @intCast(usize, ctx.mcontext.arm_pc); const bp = @intCast(usize, ctx.mcontext.arm_fp); dumpStackTraceFromBase(bp, ip); }, .aarch64 => { const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr)); const ip = @intCast(usize, ctx.mcontext.pc); // x29 is the ABI-designated frame pointer const bp = @intCast(usize, ctx.mcontext.regs[29]); dumpStackTraceFromBase(bp, ip); }, else => {}, } // We cannot allow the signal handler to return because when it runs the original instruction // again, the memory may be mapped and undefined behavior would occur rather than repeating // the segfault. So we simply abort here. os.abort(); } fn handleSegfaultWindows(info: *windows.EXCEPTION_POINTERS) callconv(.Stdcall) c_long { const exception_address = @ptrToInt(info.ExceptionRecord.ExceptionAddress); switch (info.ExceptionRecord.ExceptionCode) { windows.EXCEPTION_DATATYPE_MISALIGNMENT => panicExtra(null, exception_address, "Unaligned Memory Access", .{}), windows.EXCEPTION_ACCESS_VIOLATION => panicExtra(null, exception_address, "Segmentation fault at address 0x{x}", .{info.ExceptionRecord.ExceptionInformation[1]}), windows.EXCEPTION_ILLEGAL_INSTRUCTION => panicExtra(null, exception_address, "Illegal Instruction", .{}), windows.EXCEPTION_STACK_OVERFLOW => panicExtra(null, exception_address, "Stack Overflow", .{}), else => return windows.EXCEPTION_CONTINUE_SEARCH, } } pub fn dumpStackPointerAddr(prefix: []const u8) void { const sp = asm ("" : [argc] "={rsp}" (-> usize) ); std.debug.warn("{} sp = 0x{x}\n", .{ prefix, sp }); } // Reference everything so it gets tested. test "" { _ = leb; }
const std = @import("std"); const concepts = @import("../../lib.zig").concepts; pub fn Seed( comptime Context: type, comptime Value: type, comptime deserialize: @TypeOf(struct { fn f(_: Context, _: ?std.mem.Allocator, deserializer: anytype) @TypeOf(deserializer).Error!Value { unreachable; } }.f), ) type { return struct { pub const @"getty.de.Seed" = struct { context: Context, const Self = @This(); pub const Value = Value; pub fn deserialize(self: Self, allocator: ?std.mem.Allocator, deserializer: anytype) Return(@TypeOf(deserializer)) { return try deserialize(self.context, allocator, deserializer); } }; pub fn seed(self: Context) @"getty.de.Seed" { return .{ .context = self }; } fn Return(comptime Deserializer: type) type { comptime concepts.@"getty.Deserializer"(Deserializer); return Deserializer.Error!Value; } }; }
const macho = @import("../macho.zig"); extern "c" fn __error() *c_int; pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int; pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header; pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize; pub extern "c" fn mach_absolute_time() u64; pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void; pub extern "c" fn kqueue() c_int; pub extern "c" fn kevent( kq: c_int, changelist: [*]const Kevent, nchanges: c_int, eventlist: [*]Kevent, nevents: c_int, timeout: ?*const timespec, ) c_int; pub extern "c" fn kevent64( kq: c_int, changelist: [*]const kevent64_s, nchanges: c_int, eventlist: [*]kevent64_s, nevents: c_int, flags: c_uint, timeout: ?*const timespec, ) c_int; pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; pub extern "c" fn bind(socket: c_int, address: ?*const sockaddr, address_len: socklen_t) c_int; pub extern "c" fn socket(domain: c_int, type: c_int, protocol: c_int) c_int; const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header; /// The value of the link editor defined symbol _MH_EXECUTE_SYM is the address /// of the mach header in a Mach-O executable file type. It does not appear in /// any file type other than a MH_EXECUTE file type. The type of the symbol is /// absolute as the header is not part of any section. /// This symbol is populated when linking the system's libc, which is guaranteed /// on this operating system. However when building object files or libraries, /// the system libc won't be linked until the final executable. So we /// export a weak symbol here, to be overridden by the real one. pub extern "c" var _mh_execute_header: mach_hdr = undefined; comptime { @export("__mh_execute_header", _mh_execute_header, @import("builtin").GlobalLinkage.Weak); } pub const mach_header_64 = macho.mach_header_64; pub const mach_header = macho.mach_header; pub use @import("../os/darwin/errno.zig"); pub const _errno = __error; pub const in_port_t = u16; pub const sa_family_t = u8; pub const socklen_t = u32; pub const sockaddr = extern union { in: sockaddr_in, in6: sockaddr_in6, }; pub const sockaddr_in = extern struct { len: u8, family: sa_family_t, port: in_port_t, addr: u32, zero: [8]u8, }; pub const sockaddr_in6 = extern struct { len: u8, family: sa_family_t, port: in_port_t, flowinfo: u32, addr: [16]u8, scope_id: u32, }; pub const timeval = extern struct { tv_sec: c_long, tv_usec: i32, }; pub const timezone = extern struct { tz_minuteswest: i32, tz_dsttime: i32, }; pub const mach_timebase_info_data = extern struct { numer: u32, denom: u32, }; /// Renamed to Stat to not conflict with the stat function. pub const Stat = extern struct { dev: i32, mode: u16, nlink: u16, ino: u64, uid: u32, gid: u32, rdev: i32, atime: usize, atimensec: usize, mtime: usize, mtimensec: usize, ctime: usize, ctimensec: usize, birthtime: usize, birthtimensec: usize, size: i64, blocks: i64, blksize: i32, flags: u32, gen: u32, lspare: i32, qspare: [2]i64, }; pub const timespec = extern struct { tv_sec: isize, tv_nsec: isize, }; pub const sigset_t = u32; /// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name. pub const Sigaction = extern struct { handler: extern fn (c_int) void, sa_mask: sigset_t, sa_flags: c_int, }; pub const dirent = extern struct { d_ino: usize, d_seekoff: usize, d_reclen: u16, d_namlen: u16, d_type: u8, d_name: u8, // field address is address of first byte of name }; pub const pthread_attr_t = extern struct { __sig: c_long, __opaque: [56]u8, }; /// Renamed from `kevent` to `Kevent` to avoid conflict with function name. pub const Kevent = extern struct { ident: usize, filter: i16, flags: u16, fflags: u32, data: isize, udata: usize, }; // sys/types.h on macos uses #pragma pack(4) so these checks are // to make sure the struct is laid out the same. These values were // produced from C code using the offsetof macro. const std = @import("../std.zig"); const assert = std.debug.assert; comptime { assert(@byteOffsetOf(Kevent, "ident") == 0); assert(@byteOffsetOf(Kevent, "filter") == 8); assert(@byteOffsetOf(Kevent, "flags") == 10); assert(@byteOffsetOf(Kevent, "fflags") == 12); assert(@byteOffsetOf(Kevent, "data") == 16); assert(@byteOffsetOf(Kevent, "udata") == 24); } pub const kevent64_s = extern struct { ident: u64, filter: i16, flags: u16, fflags: u32, data: i64, udata: u64, ext: [2]u64, }; pub const mach_port_t = c_uint; pub const clock_serv_t = mach_port_t; pub const clock_res_t = c_int; pub const mach_port_name_t = natural_t; pub const natural_t = c_uint; pub const mach_timespec_t = extern struct { tv_sec: c_uint, tv_nsec: clock_res_t, }; pub const kern_return_t = c_int; pub const host_t = mach_port_t; pub const CALENDAR_CLOCK = 1; pub extern fn mach_host_self() mach_port_t; pub extern fn clock_get_time(clock_serv: clock_serv_t, cur_time: *mach_timespec_t) kern_return_t; pub extern fn host_get_clock_service(host: host_t, clock_id: clock_id_t, clock_serv: ?[*]clock_serv_t) kern_return_t; pub extern fn mach_port_deallocate(task: ipc_space_t, name: mach_port_name_t) kern_return_t; // sys/types.h on macos uses #pragma pack() so these checks are // to make sure the struct is laid out the same. These values were // produced from C code using the offsetof macro. comptime { assert(@byteOffsetOf(kevent64_s, "ident") == 0); assert(@byteOffsetOf(kevent64_s, "filter") == 8); assert(@byteOffsetOf(kevent64_s, "flags") == 10); assert(@byteOffsetOf(kevent64_s, "fflags") == 12); assert(@byteOffsetOf(kevent64_s, "data") == 16); assert(@byteOffsetOf(kevent64_s, "udata") == 24); assert(@byteOffsetOf(kevent64_s, "ext") == 32); }
const __extenddftf2 = @import("extendXfYf2.zig").__extenddftf2; const __extendhfsf2 = @import("extendXfYf2.zig").__extendhfsf2; const __extendsftf2 = @import("extendXfYf2.zig").__extendsftf2; const assert = @import("std").debug.assert; fn test__extenddftf2(a: f64, expectedHi: u64, expectedLo: u64) void { const x = __extenddftf2(a); const rep = @bitCast(u128, x); const hi = @intCast(u64, rep >> 64); const lo = @truncate(u64, rep); if (hi == expectedHi and lo == expectedLo) return; // test other possible NaN representation(signal NaN) if (expectedHi == 0x7fff800000000000 and expectedLo == 0x0) { if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and ((hi & 0xffffffffffff) > 0 or lo > 0)) { return; } } @panic("__extenddftf2 test failure"); } fn test__extendhfsf2(a: u16, expected: u32) void { const x = __extendhfsf2(a); const rep = @bitCast(u32, x); if (rep == expected) { if (rep & 0x7fffffff > 0x7f800000) { return; // NaN is always unequal. } if (x == @bitCast(f32, expected)) { return; } } @panic("__extendhfsf2 test failure"); } fn test__extendsftf2(a: f32, expectedHi: u64, expectedLo: u64) void { const x = __extendsftf2(a); const rep = @bitCast(u128, x); const hi = @intCast(u64, rep >> 64); const lo = @truncate(u64, rep); if (hi == expectedHi and lo == expectedLo) return; // test other possible NaN representation(signal NaN) if (expectedHi == 0x7fff800000000000 and expectedLo == 0x0) { if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and ((hi & 0xffffffffffff) > 0 or lo > 0)) { return; } } @panic("__extendsftf2 test failure"); } test "extenddftf2" { // qNaN test__extenddftf2(makeQNaN64(), 0x7fff800000000000, 0x0); // NaN test__extenddftf2(makeNaN64(0x7100000000000), 0x7fff710000000000, 0x0); // inf test__extenddftf2(makeInf64(), 0x7fff000000000000, 0x0); // zero test__extenddftf2(0.0, 0x0, 0x0); test__extenddftf2(0x1.23456789abcdefp+5, 0x400423456789abcd, 0xf000000000000000); test__extenddftf2(0x1.edcba987654321fp-9, 0x3ff6edcba9876543, 0x2000000000000000); test__extenddftf2(0x1.23456789abcdefp+45, 0x402c23456789abcd, 0xf000000000000000); test__extenddftf2(0x1.edcba987654321fp-45, 0x3fd2edcba9876543, 0x2000000000000000); } test "extendhfsf2" { test__extendhfsf2(0x7e00, 0x7fc00000); // qNaN test__extendhfsf2(0x7f00, 0x7fe00000); // sNaN test__extendhfsf2(0x7c01, 0x7f802000); // sNaN test__extendhfsf2(0, 0); // 0 test__extendhfsf2(0x8000, 0x80000000); // -0 test__extendhfsf2(0x7c00, 0x7f800000); // inf test__extendhfsf2(0xfc00, 0xff800000); // -inf test__extendhfsf2(0x0001, 0x33800000); // denormal (min), 2**-24 test__extendhfsf2(0x8001, 0xb3800000); // denormal (min), -2**-24 test__extendhfsf2(0x03ff, 0x387fc000); // denormal (max), 2**-14 - 2**-24 test__extendhfsf2(0x83ff, 0xb87fc000); // denormal (max), -2**-14 + 2**-24 test__extendhfsf2(0x0400, 0x38800000); // normal (min), 2**-14 test__extendhfsf2(0x8400, 0xb8800000); // normal (min), -2**-14 test__extendhfsf2(0x7bff, 0x477fe000); // normal (max), 65504 test__extendhfsf2(0xfbff, 0xc77fe000); // normal (max), -65504 test__extendhfsf2(0x3c01, 0x3f802000); // normal, 1 + 2**-10 test__extendhfsf2(0xbc01, 0xbf802000); // normal, -1 - 2**-10 test__extendhfsf2(0x3555, 0x3eaaa000); // normal, approx. 1/3 test__extendhfsf2(0xb555, 0xbeaaa000); // normal, approx. -1/3 } test "extendsftf2" { // qNaN test__extendsftf2(makeQNaN32(), 0x7fff800000000000, 0x0); // NaN test__extendsftf2(makeNaN32(0x410000), 0x7fff820000000000, 0x0); // inf test__extendsftf2(makeInf32(), 0x7fff000000000000, 0x0); // zero test__extendsftf2(0.0, 0x0, 0x0); test__extendsftf2(0x1.23456p+5, 0x4004234560000000, 0x0); test__extendsftf2(0x1.edcbap-9, 0x3ff6edcba0000000, 0x0); test__extendsftf2(0x1.23456p+45, 0x402c234560000000, 0x0); test__extendsftf2(0x1.edcbap-45, 0x3fd2edcba0000000, 0x0); } fn makeQNaN64() f64 { return @bitCast(f64, u64(0x7ff8000000000000)); } fn makeInf64() f64 { return @bitCast(f64, u64(0x7ff0000000000000)); } fn makeNaN64(rand: u64) f64 { return @bitCast(f64, 0x7ff0000000000000 | (rand & 0xfffffffffffff)); } fn makeQNaN32() f32 { return @bitCast(f32, u32(0x7fc00000)); } fn makeNaN32(rand: u32) f32 { return @bitCast(f32, 0x7f800000 | (rand & 0x7fffff)); } fn makeInf32() f32 { return @bitCast(f32, u32(0x7f800000)); }
//! Analyzed Intermediate Representation. //! This data is produced by Sema and consumed by codegen. //! Unlike ZIR where there is one instance for an entire source file, each function //! gets its own `Air` instance. const std = @import("std"); const builtin = @import("builtin"); const Value = @import("value.zig").Value; const Type = @import("type.zig").Type; const Module = @import("Module.zig"); const assert = std.debug.assert; const Air = @This(); instructions: std.MultiArrayList(Inst).Slice, /// The meaning of this data is determined by `Inst.Tag` value. /// The first few indexes are reserved. See `ExtraIndex` for the values. extra: []const u32, values: []const Value, pub const ExtraIndex = enum(u32) { /// Payload index of the main `Block` in the `extra` array. main_block, _, }; pub const Inst = struct { tag: Tag, data: Data, pub const Tag = enum(u8) { /// The first N instructions in the main block must be one arg instruction per /// function parameter. This makes function parameters participate in /// liveness analysis without any special handling. /// Uses the `ty_str` field. /// The string is the parameter name. arg, /// Float or integer addition. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. add, /// Integer addition. Wrapping is defined to be twos complement wrapping. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. addwrap, /// Saturating integer addition. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. add_sat, /// Float or integer subtraction. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. sub, /// Integer subtraction. Wrapping is defined to be twos complement wrapping. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. subwrap, /// Saturating integer subtraction. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. sub_sat, /// Float or integer multiplication. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mul, /// Integer multiplication. Wrapping is defined to be twos complement wrapping. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mulwrap, /// Saturating integer multiplication. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mul_sat, /// Float division. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_float, /// Truncating integer or float division. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_trunc, /// Flooring integer or float division. For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_floor, /// Integer or float division. Guaranteed no remainder. /// For integers, wrapping is undefined behavior. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. div_exact, /// Integer or float remainder division. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. rem, /// Integer or float modulus division. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. mod, /// Add an offset to a pointer, returning a new pointer. /// The offset is in element type units, not bytes. /// Wrapping is undefined behavior. /// The lhs is the pointer, rhs is the offset. Result type is the same as lhs. /// Uses the `bin_op` field. ptr_add, /// Subtract an offset from a pointer, returning a new pointer. /// The offset is in element type units, not bytes. /// Wrapping is undefined behavior. /// The lhs is the pointer, rhs is the offset. Result type is the same as lhs. /// Uses the `bin_op` field. ptr_sub, /// Given two operands which can be floats, integers, or vectors, returns the /// greater of the operands. For vectors it operates element-wise. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. max, /// Given two operands which can be floats, integers, or vectors, returns the /// lesser of the operands. For vectors it operates element-wise. /// Both operands are guaranteed to be the same type, and the result type /// is the same as both operands. /// Uses the `bin_op` field. min, /// Allocates stack local memory. /// Uses the `ty` field. alloc, /// If the function will pass the result by-ref, this instruction returns the /// result pointer. Otherwise it is equivalent to `alloc`. /// Uses the `ty` field. ret_ptr, /// Inline assembly. Uses the `ty_pl` field. Payload is `Asm`. assembly, /// Bitwise AND. `&`. /// Result type is the same as both operands. /// Uses the `bin_op` field. bit_and, /// Bitwise OR. `|`. /// Result type is the same as both operands. /// Uses the `bin_op` field. bit_or, /// Shift right. `>>` /// Uses the `bin_op` field. shr, /// Shift left. `<<` /// Uses the `bin_op` field. shl, /// Shift left; For unsigned integers, the shift produces a poison value if it shifts /// out any non-zero bits. For signed integers, the shift produces a poison value if /// it shifts out any bits that disagree with the resultant sign bit. /// Uses the `bin_op` field. shl_exact, /// Saturating integer shift left. `<<|` /// Uses the `bin_op` field. shl_sat, /// Bitwise XOR. `^` /// Uses the `bin_op` field. xor, /// Boolean or binary NOT. /// Uses the `ty_op` field. not, /// Reinterpret the memory representation of a value as a different type. /// Uses the `ty_op` field. bitcast, /// Uses the `ty_pl` field with payload `Block`. block, /// A labeled block of code that loops forever. At the end of the body it is implied /// to repeat; no explicit "repeat" instruction terminates loop bodies. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `ty_pl` field. Payload is `Block`. loop, /// Return from a block with a result. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `br` field. br, /// Lowers to a hardware trap instruction, or the next best thing. /// Result type is always void. breakpoint, /// Function call. /// Result type is the return type of the function being called. /// Uses the `pl_op` field with the `Call` payload. operand is the callee. /// Triggers `resolveTypeLayout` on the return type of the callee. call, /// Count leading zeroes of an integer according to its representation in twos complement. /// Result type will always be an unsigned integer big enough to fit the answer. /// Uses the `ty_op` field. clz, /// Count trailing zeroes of an integer according to its representation in twos complement. /// Result type will always be an unsigned integer big enough to fit the answer. /// Uses the `ty_op` field. ctz, /// Count number of 1 bits in an integer according to its representation in twos complement. /// Result type will always be an unsigned integer big enough to fit the answer. /// Uses the `ty_op` field. popcount, /// `<`. Result type is always bool. /// Uses the `bin_op` field. cmp_lt, /// `<=`. Result type is always bool. /// Uses the `bin_op` field. cmp_lte, /// `==`. Result type is always bool. /// Uses the `bin_op` field. cmp_eq, /// `>=`. Result type is always bool. /// Uses the `bin_op` field. cmp_gte, /// `>`. Result type is always bool. /// Uses the `bin_op` field. cmp_gt, /// `!=`. Result type is always bool. /// Uses the `bin_op` field. cmp_neq, /// Conditional branch. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `pl_op` field. Operand is the condition. Payload is `CondBr`. cond_br, /// Switch branch. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `pl_op` field. Operand is the condition. Payload is `SwitchBr`. switch_br, /// A comptime-known value. Uses the `ty_pl` field, payload is index of /// `values` array. constant, /// A comptime-known type. Uses the `ty` field. const_ty, /// Notes the beginning of a source code statement and marks the line and column. /// Result type is always void. /// Uses the `dbg_stmt` field. dbg_stmt, /// ?T => bool /// Result type is always bool. /// Uses the `un_op` field. is_null, /// ?T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_null, /// *?T => bool /// Result type is always bool. /// Uses the `un_op` field. is_null_ptr, /// *?T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_null_ptr, /// E!T => bool /// Result type is always bool. /// Uses the `un_op` field. is_err, /// E!T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_err, /// *E!T => bool /// Result type is always bool. /// Uses the `un_op` field. is_err_ptr, /// *E!T => bool (inverted logic) /// Result type is always bool. /// Uses the `un_op` field. is_non_err_ptr, /// Result type is always bool. /// Uses the `bin_op` field. bool_and, /// Result type is always bool. /// Uses the `bin_op` field. bool_or, /// Read a value from a pointer. /// Uses the `ty_op` field. load, /// Converts a pointer to its address. Result type is always `usize`. /// Uses the `un_op` field. ptrtoint, /// Given a boolean, returns 0 or 1. /// Result type is always `u1`. /// Uses the `un_op` field. bool_to_int, /// Return a value from a function. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `un_op` field. /// Triggers `resolveTypeLayout` on the return type. ret, /// This instruction communicates that the function's result value is pointed to by /// the operand. If the function will pass the result by-ref, the operand is a /// `ret_ptr` instruction. Otherwise, this instruction is equivalent to a `load` /// on the operand, followed by a `ret` on the loaded value. /// Result type is always noreturn; no instructions in a block follow this one. /// Uses the `un_op` field. /// Triggers `resolveTypeLayout` on the return type. ret_load, /// Write a value to a pointer. LHS is pointer, RHS is value. /// Result type is always void. /// Uses the `bin_op` field. store, /// Indicates the program counter will never get to this instruction. /// Result type is always noreturn; no instructions in a block follow this one. unreach, /// Convert from a float type to a smaller one. /// Uses the `ty_op` field. fptrunc, /// Convert from a float type to a wider one. /// Uses the `ty_op` field. fpext, /// Returns an integer with a different type than the operand. The new type may have /// fewer, the same, or more bits than the operand type. The new type may also /// differ in signedness from the operand type. However, the instruction /// guarantees that the same integer value fits in both types. /// The new type may also be an enum type, in which case the integer cast operates on /// the integer tag type of the enum. /// See `trunc` for integer truncation. /// Uses the `ty_op` field. intcast, /// Truncate higher bits from an integer, resulting in an integer with the same /// sign but an equal or smaller number of bits. /// Uses the `ty_op` field. trunc, /// ?T => T. If the value is null, undefined behavior. /// Uses the `ty_op` field. optional_payload, /// *?T => *T. If the value is null, undefined behavior. /// Uses the `ty_op` field. optional_payload_ptr, /// *?T => *T. Sets the value to non-null with an undefined payload value. /// Uses the `ty_op` field. optional_payload_ptr_set, /// Given a payload value, wraps it in an optional type. /// Uses the `ty_op` field. wrap_optional, /// E!T -> T. If the value is an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_payload, /// E!T -> E. If the value is not an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_err, /// *(E!T) -> *T. If the value is an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_payload_ptr, /// *(E!T) -> E. If the value is not an error, undefined behavior. /// Uses the `ty_op` field. unwrap_errunion_err_ptr, /// wrap from T to E!T /// Uses the `ty_op` field. wrap_errunion_payload, /// wrap from E to E!T /// Uses the `ty_op` field. wrap_errunion_err, /// Given a pointer to a struct or union and a field index, returns a pointer to the field. /// Uses the `ty_pl` field, payload is `StructField`. /// TODO rename to `agg_field_ptr`. struct_field_ptr, /// Given a pointer to a struct or union, returns a pointer to the field. /// The field index is the number at the end of the name. /// Uses `ty_op` field. /// TODO rename to `agg_field_ptr_index_X` struct_field_ptr_index_0, struct_field_ptr_index_1, struct_field_ptr_index_2, struct_field_ptr_index_3, /// Given a byval struct or union and a field index, returns the field byval. /// Uses the `ty_pl` field, payload is `StructField`. /// TODO rename to `agg_field_val` struct_field_val, /// Given a pointer to a tagged union, set its tag to the provided value. /// Result type is always void. /// Uses the `bin_op` field. LHS is union pointer, RHS is new tag value. set_union_tag, /// Given a tagged union value, get its tag value. /// Uses the `ty_op` field. get_union_tag, /// Constructs a slice from a pointer and a length. /// Uses the `ty_pl` field, payload is `Bin`. lhs is ptr, rhs is len. slice, /// Given a slice value, return the length. /// Result type is always usize. /// Uses the `ty_op` field. slice_len, /// Given a slice value, return the pointer. /// Uses the `ty_op` field. slice_ptr, /// Given a pointer to a slice, return a pointer to the length of the slice. /// Uses the `ty_op` field. ptr_slice_len_ptr, /// Given a pointer to a slice, return a pointer to the pointer of the slice. /// Uses the `ty_op` field. ptr_slice_ptr_ptr, /// Given an array value and element index, return the element value at that index. /// Result type is the element type of the array operand. /// Uses the `bin_op` field. array_elem_val, /// Given a slice value, and element index, return the element value at that index. /// Result type is the element type of the slice operand. /// Uses the `bin_op` field. slice_elem_val, /// Given a slice value and element index, return a pointer to the element value at that index. /// Result type is a pointer to the element type of the slice operand. /// Uses the `ty_pl` field with payload `Bin`. slice_elem_ptr, /// Given a pointer value, and element index, return the element value at that index. /// Result type is the element type of the pointer operand. /// Uses the `bin_op` field. ptr_elem_val, /// Given a pointer value, and element index, return the element pointer at that index. /// Result type is pointer to the element type of the pointer operand. /// Uses the `ty_pl` field with payload `Bin`. ptr_elem_ptr, /// Given a pointer to an array, return a slice. /// Uses the `ty_op` field. array_to_slice, /// Given a float operand, return the integer with the closest mathematical meaning. /// Uses the `ty_op` field. float_to_int, /// Given an integer operand, return the float with the closest mathematical meaning. /// Uses the `ty_op` field. int_to_float, /// Given dest ptr, value, and len, set all elements at dest to value. /// Result type is always void. /// Uses the `pl_op` field. Operand is the dest ptr. Payload is `Bin`. `lhs` is the /// value, `rhs` is the length. /// The element type may be any type, not just u8. memset, /// Given dest ptr, src ptr, and len, copy len elements from src to dest. /// Result type is always void. /// Uses the `pl_op` field. Operand is the dest ptr. Payload is `Bin`. `lhs` is the /// src ptr, `rhs` is the length. /// The element type may be any type, not just u8. memcpy, /// Uses the `ty_pl` field with payload `Cmpxchg`. cmpxchg_weak, /// Uses the `ty_pl` field with payload `Cmpxchg`. cmpxchg_strong, /// Lowers to a memory fence instruction. /// Result type is always void. /// Uses the `fence` field. fence, /// Atomically load from a pointer. /// Result type is the element type of the pointer. /// Uses the `atomic_load` field. atomic_load, /// Atomically store through a pointer. /// Result type is always `void`. /// Uses the `bin_op` field. LHS is pointer, RHS is element. atomic_store_unordered, /// Same as `atomic_store_unordered` but with `AtomicOrder.Monotonic`. atomic_store_monotonic, /// Same as `atomic_store_unordered` but with `AtomicOrder.Release`. atomic_store_release, /// Same as `atomic_store_unordered` but with `AtomicOrder.SeqCst`. atomic_store_seq_cst, /// Atomically read-modify-write via a pointer. /// Result type is the element type of the pointer. /// Uses the `pl_op` field with payload `AtomicRmw`. Operand is `ptr`. atomic_rmw, pub fn fromCmpOp(op: std.math.CompareOperator) Tag { return switch (op) { .lt => .cmp_lt, .lte => .cmp_lte, .eq => .cmp_eq, .gte => .cmp_gte, .gt => .cmp_gt, .neq => .cmp_neq, }; } pub fn toCmpOp(tag: Tag) ?std.math.CompareOperator { return switch (tag) { .cmp_lt => .lt, .cmp_lte => .lte, .cmp_eq => .eq, .cmp_gte => .gte, .cmp_gt => .gt, .cmp_neq => .neq, else => null, }; } }; /// The position of an AIR instruction within the `Air` instructions array. pub const Index = u32; pub const Ref = @import("Zir.zig").Inst.Ref; /// All instructions have an 8-byte payload, which is contained within /// this union. `Tag` determines which union field is active, as well as /// how to interpret the data within. pub const Data = union { no_op: void, un_op: Ref, bin_op: struct { lhs: Ref, rhs: Ref, }, ty: Type, ty_op: struct { ty: Ref, operand: Ref, }, ty_pl: struct { ty: Ref, // Index into a different array. payload: u32, }, ty_str: struct { ty: Ref, // ZIR string table index. str: u32, }, br: struct { block_inst: Index, operand: Ref, }, pl_op: struct { operand: Ref, payload: u32, }, dbg_stmt: struct { line: u32, column: u32, }, fence: std.builtin.AtomicOrder, atomic_load: struct { ptr: Ref, order: std.builtin.AtomicOrder, }, // Make sure we don't accidentally add a field to make this union // bigger than expected. Note that in Debug builds, Zig is allowed // to insert a secret field for safety checks. comptime { if (builtin.mode != .Debug) { assert(@sizeOf(Data) == 8); } } }; }; /// Trailing is a list of instruction indexes for every `body_len`. pub const Block = struct { body_len: u32, }; /// Trailing is a list of `Inst.Ref` for every `args_len`. pub const Call = struct { args_len: u32, }; /// This data is stored inside extra, with two sets of trailing `Inst.Ref`: /// * 0. the then body, according to `then_body_len`. /// * 1. the else body, according to `else_body_len`. pub const CondBr = struct { then_body_len: u32, else_body_len: u32, }; /// Trailing: /// * 0. `Case` for each `cases_len` /// * 1. the else body, according to `else_body_len`. pub const SwitchBr = struct { cases_len: u32, else_body_len: u32, /// Trailing: /// * item: Inst.Ref // for each `items_len`. /// * instruction index for each `body_len`. pub const Case = struct { items_len: u32, body_len: u32, }; }; pub const StructField = struct { /// Whether this is a pointer or byval is determined by the AIR tag. struct_operand: Inst.Ref, field_index: u32, }; pub const Bin = struct { lhs: Inst.Ref, rhs: Inst.Ref, }; /// Trailing: /// 0. `Inst.Ref` for every outputs_len /// 1. `Inst.Ref` for every inputs_len pub const Asm = struct { /// Index to the corresponding ZIR instruction. /// `asm_source`, `outputs_len`, `inputs_len`, `clobbers_len`, `is_volatile`, and /// clobbers are found via here. zir_index: u32, }; pub const Cmpxchg = struct { ptr: Inst.Ref, expected_value: Inst.Ref, new_value: Inst.Ref, /// 0b00000000000000000000000000000XXX - success_order /// 0b00000000000000000000000000XXX000 - failure_order flags: u32, pub fn successOrder(self: Cmpxchg) std.builtin.AtomicOrder { return @intToEnum(std.builtin.AtomicOrder, @truncate(u3, self.flags)); } pub fn failureOrder(self: Cmpxchg) std.builtin.AtomicOrder { return @intToEnum(std.builtin.AtomicOrder, @truncate(u3, self.flags >> 3)); } }; pub const AtomicRmw = struct { operand: Inst.Ref, /// 0b00000000000000000000000000000XXX - ordering /// 0b0000000000000000000000000XXXX000 - op flags: u32, pub fn ordering(self: AtomicRmw) std.builtin.AtomicOrder { return @intToEnum(std.builtin.AtomicOrder, @truncate(u3, self.flags)); } pub fn op(self: AtomicRmw) std.builtin.AtomicRmwOp { return @intToEnum(std.builtin.AtomicRmwOp, @truncate(u4, self.flags >> 3)); } }; pub fn getMainBody(air: Air) []const Air.Inst.Index { const body_index = air.extra[@enumToInt(ExtraIndex.main_block)]; const extra = air.extraData(Block, body_index); return air.extra[extra.end..][0..extra.data.body_len]; } pub fn typeOf(air: Air, inst: Air.Inst.Ref) Type { const ref_int = @enumToInt(inst); if (ref_int < Air.Inst.Ref.typed_value_map.len) { return Air.Inst.Ref.typed_value_map[ref_int].ty; } return air.typeOfIndex(@intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len)); } pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type { const datas = air.instructions.items(.data); switch (air.instructions.items(.tag)[inst]) { .arg => return air.getRefType(datas[inst].ty_str.ty), .add, .addwrap, .add_sat, .sub, .subwrap, .sub_sat, .mul, .mulwrap, .mul_sat, .div_float, .div_trunc, .div_floor, .div_exact, .rem, .mod, .bit_and, .bit_or, .xor, .ptr_add, .ptr_sub, .shr, .shl, .shl_exact, .shl_sat, .min, .max, => return air.typeOf(datas[inst].bin_op.lhs), .cmp_lt, .cmp_lte, .cmp_eq, .cmp_gte, .cmp_gt, .cmp_neq, .is_null, .is_non_null, .is_null_ptr, .is_non_null_ptr, .is_err, .is_non_err, .is_err_ptr, .is_non_err_ptr, .bool_and, .bool_or, => return Type.initTag(.bool), .const_ty => return Type.initTag(.type), .alloc, .ret_ptr, => return datas[inst].ty, .assembly, .block, .constant, .struct_field_ptr, .struct_field_val, .slice_elem_ptr, .ptr_elem_ptr, .cmpxchg_weak, .cmpxchg_strong, .slice, => return air.getRefType(datas[inst].ty_pl.ty), .not, .bitcast, .load, .fpext, .fptrunc, .intcast, .trunc, .optional_payload, .optional_payload_ptr, .optional_payload_ptr_set, .wrap_optional, .unwrap_errunion_payload, .unwrap_errunion_err, .unwrap_errunion_payload_ptr, .unwrap_errunion_err_ptr, .wrap_errunion_payload, .wrap_errunion_err, .slice_ptr, .ptr_slice_len_ptr, .ptr_slice_ptr_ptr, .struct_field_ptr_index_0, .struct_field_ptr_index_1, .struct_field_ptr_index_2, .struct_field_ptr_index_3, .array_to_slice, .float_to_int, .int_to_float, .get_union_tag, .clz, .ctz, .popcount, => return air.getRefType(datas[inst].ty_op.ty), .loop, .br, .cond_br, .switch_br, .ret, .ret_load, .unreach, => return Type.initTag(.noreturn), .breakpoint, .dbg_stmt, .store, .fence, .atomic_store_unordered, .atomic_store_monotonic, .atomic_store_release, .atomic_store_seq_cst, .memset, .memcpy, .set_union_tag, => return Type.initTag(.void), .ptrtoint, .slice_len, => return Type.initTag(.usize), .bool_to_int => return Type.initTag(.u1), .call => { const callee_ty = air.typeOf(datas[inst].pl_op.operand); switch (callee_ty.zigTypeTag()) { .Fn => return callee_ty.fnReturnType(), .Pointer => return callee_ty.childType().fnReturnType(), else => unreachable, } }, .slice_elem_val, .ptr_elem_val, .array_elem_val => { const ptr_ty = air.typeOf(datas[inst].bin_op.lhs); return ptr_ty.elemType(); }, .atomic_load => { const ptr_ty = air.typeOf(datas[inst].atomic_load.ptr); return ptr_ty.elemType(); }, .atomic_rmw => { const ptr_ty = air.typeOf(datas[inst].pl_op.operand); return ptr_ty.elemType(); }, } } pub fn getRefType(air: Air, ref: Air.Inst.Ref) Type { const ref_int = @enumToInt(ref); if (ref_int < Air.Inst.Ref.typed_value_map.len) { var buffer: Value.ToTypeBuffer = undefined; return Air.Inst.Ref.typed_value_map[ref_int].val.toType(&buffer); } const inst_index = ref_int - Air.Inst.Ref.typed_value_map.len; const air_tags = air.instructions.items(.tag); const air_datas = air.instructions.items(.data); assert(air_tags[inst_index] == .const_ty); return air_datas[inst_index].ty; } /// Returns the requested data, as well as the new index which is at the start of the /// trailers for the object. pub fn extraData(air: Air, comptime T: type, index: usize) struct { data: T, end: usize } { const fields = std.meta.fields(T); var i: usize = index; var result: T = undefined; inline for (fields) |field| { @field(result, field.name) = switch (field.field_type) { u32 => air.extra[i], Inst.Ref => @intToEnum(Inst.Ref, air.extra[i]), i32 => @bitCast(i32, air.extra[i]), else => @compileError("bad field type"), }; i += 1; } return .{ .data = result, .end = i, }; } pub fn deinit(air: *Air, gpa: std.mem.Allocator) void { air.instructions.deinit(gpa); gpa.free(air.extra); gpa.free(air.values); air.* = undefined; } const ref_start_index: u32 = Air.Inst.Ref.typed_value_map.len; pub fn indexToRef(inst: Air.Inst.Index) Air.Inst.Ref { return @intToEnum(Air.Inst.Ref, ref_start_index + inst); } pub fn refToIndex(inst: Air.Inst.Ref) ?Air.Inst.Index { const ref_int = @enumToInt(inst); if (ref_int >= ref_start_index) { return ref_int - ref_start_index; } else { return null; } } /// Returns `null` if runtime-known. pub fn value(air: Air, inst: Air.Inst.Ref) ?Value { const ref_int = @enumToInt(inst); if (ref_int < Air.Inst.Ref.typed_value_map.len) { return Air.Inst.Ref.typed_value_map[ref_int].val; } const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len); const air_datas = air.instructions.items(.data); switch (air.instructions.items(.tag)[inst_index]) { .constant => return air.values[air_datas[inst_index].ty_pl.payload], .const_ty => unreachable, else => return air.typeOfIndex(inst_index).onePossibleValue(), } }
const std = @import("std"); const builtin = @import("builtin"); const Builder = std.build.Builder; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); const tests = b.addTest("src/okredis.zig"); tests.setBuildMode(mode); tests.setNamePrefix("debug test"); const test_step = b.step("test", "Run all tests in debug mode."); test_step.dependOn(&tests.step); const build_docs = b.addSystemCommand(&[_][]const u8{ b.zig_exe, "test", "src/okredis.zig", // "-target", // "x86_64-linux", "-femit-docs", "-fno-emit-bin", "--output-dir", ".", }); const all_step = b.step("all", "Builds docs and runs all tests"); const docs = b.step("docs", "Builds docs"); docs.dependOn(&build_docs.step); all_step.dependOn(test_step); all_step.dependOn(docs); b.default_step.dependOn(docs); }
const std = @import("std"); const expect = std.testing.expect; fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type { expect(Key == @IntType(false, Key.bit_count)); expect(Key.bit_count >= mask_bit_count); const ShardKey = @IntType(false, mask_bit_count); const shift_amount = Key.bit_count - ShardKey.bit_count; return struct { const Self = @This(); shards: [1 << ShardKey.bit_count]?*Node, pub fn create() Self { return Self{ .shards = []?*Node{null} ** (1 << ShardKey.bit_count) }; } fn getShardKey(key: Key) ShardKey { // https://github.com/ziglang/zig/issues/1544 // this special case is needed because you can't u32 >> 32. if (ShardKey == u0) return 0; // this can be u1 >> u0 const shard_key = key >> shift_amount; // TODO: https://github.com/ziglang/zig/issues/1544 // This cast could be implicit if we teach the compiler that // u32 >> 30 -> u2 return @intCast(ShardKey, shard_key); } pub fn put(self: *Self, node: *Node) void { const shard_key = Self.getShardKey(node.key); node.next = self.shards[shard_key]; self.shards[shard_key] = node; } pub fn get(self: *Self, key: Key) ?*Node { const shard_key = Self.getShardKey(key); var maybe_node = self.shards[shard_key]; while (maybe_node) |node| : (maybe_node = node.next) { if (node.key == key) return node; } return null; } pub const Node = struct { key: Key, value: V, next: ?*Node, pub fn init(self: *Node, key: Key, value: V) void { self.key = key; self.value = value; self.next = null; } }; }; } test "sharded table" { // realistic 16-way sharding testShardedTable(u32, 4, 8); testShardedTable(u5, 0, 32); // ShardKey == u0 testShardedTable(u5, 2, 32); testShardedTable(u5, 5, 32); testShardedTable(u1, 0, 2); testShardedTable(u1, 1, 2); // this does u1 >> u0 testShardedTable(u0, 0, 1); } fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime node_count: comptime_int) void { const Table = ShardedTable(Key, mask_bit_count, void); var table = Table.create(); var node_buffer: [node_count]Table.Node = undefined; for (node_buffer) |*node, i| { const key = @intCast(Key, i); expect(table.get(key) == null); node.init(key, {}); table.put(node); } for (node_buffer) |*node, i| { expect(table.get(@intCast(Key, i)) == node); } } // #2225 test "comptime shr of BigInt" { comptime { var n0 = 0xdeadbeef0000000000000000; std.debug.assert(n0 >> 64 == 0xdeadbeef); var n1 = 17908056155735594659; std.debug.assert(n1 >> 64 == 0); } }
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2021 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const std = @import("../../std.zig"); const builtin = std.builtin; const maxInt = std.math.maxInt; pub const blkcnt_t = i64; pub const blksize_t = i32; pub const clock_t = u32; pub const dev_t = u64; pub const fd_t = i32; pub const gid_t = u32; pub const ino_t = u64; pub const mode_t = u32; pub const nlink_t = u32; pub const off_t = i64; pub const pid_t = i32; pub const socklen_t = u32; pub const time_t = i64; pub const uid_t = u32; pub const lwpid_t = i32; pub const suseconds_t = c_int; /// Renamed from `kevent` to `Kevent` to avoid conflict with function name. pub const Kevent = extern struct { ident: usize, filter: i32, flags: u32, fflags: u32, data: i64, udata: usize, }; pub const RTLD_LAZY = 1; pub const RTLD_NOW = 2; pub const RTLD_GLOBAL = 0x100; pub const RTLD_LOCAL = 0x200; pub const RTLD_NODELETE = 0x01000; pub const RTLD_NOLOAD = 0x02000; pub const RTLD_NEXT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))); pub const RTLD_DEFAULT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -2))); pub const RTLD_SELF = @intToPtr(*c_void, @bitCast(usize, @as(isize, -3))); pub const dl_phdr_info = extern struct { dlpi_addr: usize, dlpi_name: ?[*:0]const u8, dlpi_phdr: [*]std.elf.Phdr, dlpi_phnum: u16, }; pub const Flock = extern struct { l_start: off_t, l_len: off_t, l_pid: pid_t, l_type: i16, l_whence: i16, }; pub const addrinfo = extern struct { flags: i32, family: i32, socktype: i32, protocol: i32, addrlen: socklen_t, canonname: ?[*:0]u8, addr: ?*sockaddr, next: ?*addrinfo, }; pub const EAI = enum(c_int) { /// address family for hostname not supported ADDRFAMILY = 1, /// name could not be resolved at this time AGAIN = 2, /// flags parameter had an invalid value BADFLAGS = 3, /// non-recoverable failure in name resolution FAIL = 4, /// address family not recognized FAMILY = 5, /// memory allocation failure MEMORY = 6, /// no address associated with hostname NODATA = 7, /// name does not resolve NONAME = 8, /// service not recognized for socket type SERVICE = 9, /// intended socket type was not recognized SOCKTYPE = 10, /// system error returned in errno SYSTEM = 11, /// invalid value for hints BADHINTS = 12, /// resolved protocol is unknown PROTOCOL = 13, /// argument buffer overflow OVERFLOW = 14, _, }; pub const EAI_MAX = 15; pub const msghdr = extern struct { /// optional address msg_name: ?*sockaddr, /// size of address msg_namelen: socklen_t, /// scatter/gather array msg_iov: [*]iovec, /// # elements in msg_iov msg_iovlen: i32, /// ancillary data msg_control: ?*c_void, /// ancillary data buffer len msg_controllen: socklen_t, /// flags on received message msg_flags: i32, }; pub const msghdr_const = extern struct { /// optional address msg_name: ?*const sockaddr, /// size of address msg_namelen: socklen_t, /// scatter/gather array msg_iov: [*]iovec_const, /// # elements in msg_iov msg_iovlen: i32, /// ancillary data msg_control: ?*c_void, /// ancillary data buffer len msg_controllen: socklen_t, /// flags on received message msg_flags: i32, }; pub const libc_stat = extern struct { dev: dev_t, mode: mode_t, ino: ino_t, nlink: nlink_t, uid: uid_t, gid: gid_t, rdev: dev_t, atim: timespec, mtim: timespec, ctim: timespec, birthtim: timespec, size: off_t, blocks: blkcnt_t, blksize: blksize_t, flags: u32, gen: u32, __spare: [2]u32, pub fn atime(self: @This()) timespec { return self.atim; } pub fn mtime(self: @This()) timespec { return self.mtim; } pub fn ctime(self: @This()) timespec { return self.ctim; } }; pub const timespec = extern struct { tv_sec: i64, tv_nsec: isize, }; pub const timeval = extern struct { /// seconds tv_sec: time_t, /// microseconds tv_usec: suseconds_t, }; pub const MAXNAMLEN = 511; pub const dirent = extern struct { d_fileno: ino_t, d_reclen: u16, d_namlen: u16, d_type: u8, d_name: [MAXNAMLEN:0]u8, pub fn reclen(self: dirent) u16 { return self.d_reclen; } }; pub const SOCK_STREAM = 1; pub const SOCK_DGRAM = 2; pub const SOCK_RAW = 3; pub const SOCK_RDM = 4; pub const SOCK_SEQPACKET = 5; pub const SOCK_CONN_DGRAM = 6; pub const SOCK_DCCP = SOCK_CONN_DGRAM; pub const SOCK_CLOEXEC = 0x10000000; pub const SOCK_NONBLOCK = 0x20000000; pub const SOCK_NOSIGPIPE = 0x40000000; pub const SOCK_FLAGS_MASK = 0xf0000000; pub const SO_DEBUG = 0x0001; pub const SO_ACCEPTCONN = 0x0002; pub const SO_REUSEADDR = 0x0004; pub const SO_KEEPALIVE = 0x0008; pub const SO_DONTROUTE = 0x0010; pub const SO_BROADCAST = 0x0020; pub const SO_USELOOPBACK = 0x0040; pub const SO_LINGER = 0x0080; pub const SO_OOBINLINE = 0x0100; pub const SO_REUSEPORT = 0x0200; pub const SO_NOSIGPIPE = 0x0800; pub const SO_ACCEPTFILTER = 0x1000; pub const SO_TIMESTAMP = 0x2000; pub const SO_RERROR = 0x4000; pub const SO_SNDBUF = 0x1001; pub const SO_RCVBUF = 0x1002; pub const SO_SNDLOWAT = 0x1003; pub const SO_RCVLOWAT = 0x1004; pub const SO_ERROR = 0x1007; pub const SO_TYPE = 0x1008; pub const SO_OVERFLOWED = 0x1009; pub const SO_NOHEADER = 0x100a; pub const SO_SNDTIMEO = 0x100b; pub const SO_RCVTIMEO = 0x100c; pub const SOL_SOCKET = 0xffff; pub const PF_UNSPEC = AF_UNSPEC; pub const PF_LOCAL = AF_LOCAL; pub const PF_UNIX = PF_LOCAL; pub const PF_INET = AF_INET; pub const PF_IMPLINK = AF_IMPLINK; pub const PF_PUP = AF_PUP; pub const PF_CHAOS = AF_CHAOS; pub const PF_NS = AF_NS; pub const PF_ISO = AF_ISO; pub const PF_OSI = AF_ISO; pub const PF_ECMA = AF_ECMA; pub const PF_DATAKIT = AF_DATAKIT; pub const PF_CCITT = AF_CCITT; pub const PF_SNA = AF_SNA; pub const PF_DECnet = AF_DECnet; pub const PF_DLI = AF_DLI; pub const PF_LAT = AF_LAT; pub const PF_HYLINK = AF_HYLINK; pub const PF_APPLETALK = AF_APPLETALK; pub const PF_OROUTE = AF_OROUTE; pub const PF_LINK = AF_LINK; pub const PF_COIP = AF_COIP; pub const PF_CNT = AF_CNT; pub const PF_INET6 = AF_INET6; pub const PF_IPX = AF_IPX; pub const PF_ISDN = AF_ISDN; pub const PF_E164 = AF_E164; pub const PF_NATM = AF_NATM; pub const PF_ARP = AF_ARP; pub const PF_BLUETOOTH = AF_BLUETOOTH; pub const PF_MPLS = AF_MPLS; pub const PF_ROUTE = AF_ROUTE; pub const PF_CAN = AF_CAN; pub const PF_ETHER = AF_ETHER; pub const PF_MAX = AF_MAX; pub const AF_UNSPEC = 0; pub const AF_LOCAL = 1; pub const AF_UNIX = AF_LOCAL; pub const AF_INET = 2; pub const AF_IMPLINK = 3; pub const AF_PUP = 4; pub const AF_CHAOS = 5; pub const AF_NS = 6; pub const AF_ISO = 7; pub const AF_OSI = AF_ISO; pub const AF_ECMA = 8; pub const AF_DATAKIT = 9; pub const AF_CCITT = 10; pub const AF_SNA = 11; pub const AF_DECnet = 12; pub const AF_DLI = 13; pub const AF_LAT = 14; pub const AF_HYLINK = 15; pub const AF_APPLETALK = 16; pub const AF_OROUTE = 17; pub const AF_LINK = 18; pub const AF_COIP = 20; pub const AF_CNT = 21; pub const AF_IPX = 23; pub const AF_INET6 = 24; pub const AF_ISDN = 26; pub const AF_E164 = AF_ISDN; pub const AF_NATM = 27; pub const AF_ARP = 28; pub const AF_BLUETOOTH = 31; pub const AF_IEEE80211 = 32; pub const AF_MPLS = 33; pub const AF_ROUTE = 34; pub const AF_CAN = 35; pub const AF_ETHER = 36; pub const AF_MAX = 37; pub const in_port_t = u16; pub const sa_family_t = u8; pub const sockaddr = extern struct { /// total length len: u8, /// address family family: sa_family_t, /// actually longer; address value data: [14]u8, }; pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; pub const sockaddr_in = extern struct { len: u8 = @sizeOf(sockaddr_in), family: sa_family_t = AF_INET, port: in_port_t, addr: u32, zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, }; pub const sockaddr_in6 = extern struct { len: u8 = @sizeOf(sockaddr_in6), family: sa_family_t = AF_INET6, port: in_port_t, flowinfo: u32, addr: [16]u8, scope_id: u32, }; /// Definitions for UNIX IPC domain. pub const sockaddr_un = extern struct { /// total sockaddr length len: u8 = @sizeOf(sockaddr_un), /// AF_LOCAL family: sa_family_t = AF_LOCAL, /// path name path: [104]u8, }; /// get address to use bind() pub const AI_PASSIVE = 0x00000001; /// fill ai_canonname pub const AI_CANONNAME = 0x00000002; /// prevent host name resolution pub const AI_NUMERICHOST = 0x00000004; /// prevent service name resolution pub const AI_NUMERICSERV = 0x00000008; /// only if any address is assigned pub const AI_ADDRCONFIG = 0x00000400; pub const CTL_KERN = 1; pub const CTL_DEBUG = 5; pub const KERN_PROC_ARGS = 48; // struct: process argv/env pub const KERN_PROC_PATHNAME = 5; // path to executable pub const PATH_MAX = 1024; pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; pub const PROT_NONE = 0; pub const PROT_READ = 1; pub const PROT_WRITE = 2; pub const PROT_EXEC = 4; pub const CLOCK_REALTIME = 0; pub const CLOCK_VIRTUAL = 1; pub const CLOCK_PROF = 2; pub const CLOCK_MONOTONIC = 3; pub const CLOCK_THREAD_CPUTIME_ID = 0x20000000; pub const CLOCK_PROCESS_CPUTIME_ID = 0x40000000; pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); pub const MAP_SHARED = 0x0001; pub const MAP_PRIVATE = 0x0002; pub const MAP_REMAPDUP = 0x0004; pub const MAP_FIXED = 0x0010; pub const MAP_RENAME = 0x0020; pub const MAP_NORESERVE = 0x0040; pub const MAP_INHERIT = 0x0080; pub const MAP_HASSEMAPHORE = 0x0200; pub const MAP_TRYFIXED = 0x0400; pub const MAP_WIRED = 0x0800; pub const MAP_FILE = 0x0000; pub const MAP_NOSYNC = 0x0800; pub const MAP_ANON = 0x1000; pub const MAP_ANONYMOUS = MAP_ANON; pub const MAP_STACK = 0x2000; pub const WNOHANG = 0x00000001; pub const WUNTRACED = 0x00000002; pub const WSTOPPED = WUNTRACED; pub const WCONTINUED = 0x00000010; pub const WNOWAIT = 0x00010000; pub const WEXITED = 0x00000020; pub const WTRAPPED = 0x00000040; pub const SA_ONSTACK = 0x0001; pub const SA_RESTART = 0x0002; pub const SA_RESETHAND = 0x0004; pub const SA_NOCLDSTOP = 0x0008; pub const SA_NODEFER = 0x0010; pub const SA_NOCLDWAIT = 0x0020; pub const SA_SIGINFO = 0x0040; pub const SIGHUP = 1; pub const SIGINT = 2; pub const SIGQUIT = 3; pub const SIGILL = 4; pub const SIGTRAP = 5; pub const SIGABRT = 6; pub const SIGIOT = SIGABRT; pub const SIGEMT = 7; pub const SIGFPE = 8; pub const SIGKILL = 9; pub const SIGBUS = 10; pub const SIGSEGV = 11; pub const SIGSYS = 12; pub const SIGPIPE = 13; pub const SIGALRM = 14; pub const SIGTERM = 15; pub const SIGURG = 16; pub const SIGSTOP = 17; pub const SIGTSTP = 18; pub const SIGCONT = 19; pub const SIGCHLD = 20; pub const SIGTTIN = 21; pub const SIGTTOU = 22; pub const SIGIO = 23; pub const SIGXCPU = 24; pub const SIGXFSZ = 25; pub const SIGVTALRM = 26; pub const SIGPROF = 27; pub const SIGWINCH = 28; pub const SIGINFO = 29; pub const SIGUSR1 = 30; pub const SIGUSR2 = 31; pub const SIGPWR = 32; pub const SIGRTMIN = 33; pub const SIGRTMAX = 63; // access function pub const F_OK = 0; // test for existence of file pub const X_OK = 1; // test for execute or search permission pub const W_OK = 2; // test for write permission pub const R_OK = 4; // test for read permission /// open for reading only pub const O_RDONLY = 0x00000000; /// open for writing only pub const O_WRONLY = 0x00000001; /// open for reading and writing pub const O_RDWR = 0x00000002; /// mask for above modes pub const O_ACCMODE = 0x00000003; /// no delay pub const O_NONBLOCK = 0x00000004; /// set append mode pub const O_APPEND = 0x00000008; /// open with shared file lock pub const O_SHLOCK = 0x00000010; /// open with exclusive file lock pub const O_EXLOCK = 0x00000020; /// signal pgrp when data ready pub const O_ASYNC = 0x00000040; /// synchronous writes pub const O_SYNC = 0x00000080; /// don't follow symlinks on the last pub const O_NOFOLLOW = 0x00000100; /// create if nonexistent pub const O_CREAT = 0x00000200; /// truncate to zero length pub const O_TRUNC = 0x00000400; /// error if already exists pub const O_EXCL = 0x00000800; /// don't assign controlling terminal pub const O_NOCTTY = 0x00008000; /// write: I/O data completion pub const O_DSYNC = 0x00010000; /// read: I/O completion as for write pub const O_RSYNC = 0x00020000; /// use alternate i/o semantics pub const O_ALT_IO = 0x00040000; /// direct I/O hint pub const O_DIRECT = 0x00080000; /// fail if not a directory pub const O_DIRECTORY = 0x00200000; /// set close on exec pub const O_CLOEXEC = 0x00400000; /// skip search permission checks pub const O_SEARCH = 0x00800000; pub const F_DUPFD = 0; pub const F_GETFD = 1; pub const F_SETFD = 2; pub const F_GETFL = 3; pub const F_SETFL = 4; pub const F_GETOWN = 5; pub const F_SETOWN = 6; pub const F_GETLK = 7; pub const F_SETLK = 8; pub const F_SETLKW = 9; pub const F_RDLCK = 1; pub const F_WRLCK = 3; pub const F_UNLCK = 2; pub const LOCK_SH = 1; pub const LOCK_EX = 2; pub const LOCK_UN = 8; pub const LOCK_NB = 4; pub const FD_CLOEXEC = 1; pub const SEEK_SET = 0; pub const SEEK_CUR = 1; pub const SEEK_END = 2; pub const SIG_BLOCK = 1; pub const SIG_UNBLOCK = 2; pub const SIG_SETMASK = 3; pub const DT_UNKNOWN = 0; pub const DT_FIFO = 1; pub const DT_CHR = 2; pub const DT_DIR = 4; pub const DT_BLK = 6; pub const DT_REG = 8; pub const DT_LNK = 10; pub const DT_SOCK = 12; pub const DT_WHT = 14; /// add event to kq (implies enable) pub const EV_ADD = 0x0001; /// delete event from kq pub const EV_DELETE = 0x0002; /// enable event pub const EV_ENABLE = 0x0004; /// disable event (not reported) pub const EV_DISABLE = 0x0008; /// only report one occurrence pub const EV_ONESHOT = 0x0010; /// clear event state after reporting pub const EV_CLEAR = 0x0020; /// force immediate event output /// ... with or without EV_ERROR /// ... use KEVENT_FLAG_ERROR_EVENTS /// on syscalls supporting flags pub const EV_RECEIPT = 0x0040; /// disable event after reporting pub const EV_DISPATCH = 0x0080; pub const EVFILT_READ = 0; pub const EVFILT_WRITE = 1; /// attached to aio requests pub const EVFILT_AIO = 2; /// attached to vnodes pub const EVFILT_VNODE = 3; /// attached to struct proc pub const EVFILT_PROC = 4; /// attached to struct proc pub const EVFILT_SIGNAL = 5; /// timers pub const EVFILT_TIMER = 6; /// Filesystem events pub const EVFILT_FS = 7; /// On input, NOTE_TRIGGER causes the event to be triggered for output. pub const NOTE_TRIGGER = 0x08000000; /// low water mark pub const NOTE_LOWAT = 0x00000001; /// vnode was removed pub const NOTE_DELETE = 0x00000001; /// data contents changed pub const NOTE_WRITE = 0x00000002; /// size increased pub const NOTE_EXTEND = 0x00000004; /// attributes changed pub const NOTE_ATTRIB = 0x00000008; /// link count changed pub const NOTE_LINK = 0x00000010; /// vnode was renamed pub const NOTE_RENAME = 0x00000020; /// vnode access was revoked pub const NOTE_REVOKE = 0x00000040; /// process exited pub const NOTE_EXIT = 0x80000000; /// process forked pub const NOTE_FORK = 0x40000000; /// process exec'd pub const NOTE_EXEC = 0x20000000; /// mask for signal & exit status pub const NOTE_PDATAMASK = 0x000fffff; pub const NOTE_PCTRLMASK = 0xf0000000; pub const TIOCCBRK = 0x2000747a; pub const TIOCCDTR = 0x20007478; pub const TIOCCONS = 0x80047462; pub const TIOCDCDTIMESTAMP = 0x40107458; pub const TIOCDRAIN = 0x2000745e; pub const TIOCEXCL = 0x2000740d; pub const TIOCEXT = 0x80047460; pub const TIOCFLAG_CDTRCTS = 0x10; pub const TIOCFLAG_CLOCAL = 0x2; pub const TIOCFLAG_CRTSCTS = 0x4; pub const TIOCFLAG_MDMBUF = 0x8; pub const TIOCFLAG_SOFTCAR = 0x1; pub const TIOCFLUSH = 0x80047410; pub const TIOCGETA = 0x402c7413; pub const TIOCGETD = 0x4004741a; pub const TIOCGFLAGS = 0x4004745d; pub const TIOCGLINED = 0x40207442; pub const TIOCGPGRP = 0x40047477; pub const TIOCGQSIZE = 0x40047481; pub const TIOCGRANTPT = 0x20007447; pub const TIOCGSID = 0x40047463; pub const TIOCGSIZE = 0x40087468; pub const TIOCGWINSZ = 0x40087468; pub const TIOCMBIC = 0x8004746b; pub const TIOCMBIS = 0x8004746c; pub const TIOCMGET = 0x4004746a; pub const TIOCMSET = 0x8004746d; pub const TIOCM_CAR = 0x40; pub const TIOCM_CD = 0x40; pub const TIOCM_CTS = 0x20; pub const TIOCM_DSR = 0x100; pub const TIOCM_DTR = 0x2; pub const TIOCM_LE = 0x1; pub const TIOCM_RI = 0x80; pub const TIOCM_RNG = 0x80; pub const TIOCM_RTS = 0x4; pub const TIOCM_SR = 0x10; pub const TIOCM_ST = 0x8; pub const TIOCNOTTY = 0x20007471; pub const TIOCNXCL = 0x2000740e; pub const TIOCOUTQ = 0x40047473; pub const TIOCPKT = 0x80047470; pub const TIOCPKT_DATA = 0x0; pub const TIOCPKT_DOSTOP = 0x20; pub const TIOCPKT_FLUSHREAD = 0x1; pub const TIOCPKT_FLUSHWRITE = 0x2; pub const TIOCPKT_IOCTL = 0x40; pub const TIOCPKT_NOSTOP = 0x10; pub const TIOCPKT_START = 0x8; pub const TIOCPKT_STOP = 0x4; pub const TIOCPTMGET = 0x40287446; pub const TIOCPTSNAME = 0x40287448; pub const TIOCRCVFRAME = 0x80087445; pub const TIOCREMOTE = 0x80047469; pub const TIOCSBRK = 0x2000747b; pub const TIOCSCTTY = 0x20007461; pub const TIOCSDTR = 0x20007479; pub const TIOCSETA = 0x802c7414; pub const TIOCSETAF = 0x802c7416; pub const TIOCSETAW = 0x802c7415; pub const TIOCSETD = 0x8004741b; pub const TIOCSFLAGS = 0x8004745c; pub const TIOCSIG = 0x2000745f; pub const TIOCSLINED = 0x80207443; pub const TIOCSPGRP = 0x80047476; pub const TIOCSQSIZE = 0x80047480; pub const TIOCSSIZE = 0x80087467; pub const TIOCSTART = 0x2000746e; pub const TIOCSTAT = 0x80047465; pub const TIOCSTI = 0x80017472; pub const TIOCSTOP = 0x2000746f; pub const TIOCSWINSZ = 0x80087467; pub const TIOCUCNTL = 0x80047466; pub const TIOCXMTFRAME = 0x80087444; pub fn WEXITSTATUS(s: u32) u8 { return @intCast(u8, (s >> 8) & 0xff); } pub fn WTERMSIG(s: u32) u32 { return s & 0x7f; } pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } pub fn WIFCONTINUED(s: u32) bool { return ((s & 0x7f) == 0xffff); } pub fn WIFSTOPPED(s: u32) bool { return ((s & 0x7f != 0x7f) and !WIFCONTINUED(s)); } pub fn WIFSIGNALED(s: u32) bool { return !WIFSTOPPED(s) and !WIFCONTINUED(s) and !WIFEXITED(s); } pub const winsize = extern struct { ws_row: u16, ws_col: u16, ws_xpixel: u16, ws_ypixel: u16, }; const NSIG = 32; pub const SIG_DFL = @intToPtr(?Sigaction.sigaction_fn, 0); pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1); pub const SIG_ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); /// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. pub const Sigaction = extern struct { pub const handler_fn = fn (c_int) callconv(.C) void; pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; /// signal handler handler: extern union { handler: ?handler_fn, sigaction: ?sigaction_fn, }, /// signal mask to apply mask: sigset_t, /// signal options flags: c_uint, }; pub const sigval_t = extern union { int: i32, ptr: ?*c_void, }; pub const siginfo_t = extern union { pad: [128]u8, info: _ksiginfo, }; pub const _ksiginfo = extern struct { signo: i32, code: i32, errno: i32, // 64bit architectures insert 4bytes of padding here, this is done by // correctly aligning the reason field reason: extern union { rt: extern struct { pid: pid_t, uid: uid_t, value: sigval_t, }, child: extern struct { pid: pid_t, uid: uid_t, status: i32, utime: clock_t, stime: clock_t, }, fault: extern struct { addr: ?*c_void, trap: i32, trap2: i32, trap3: i32, }, poll: extern struct { band: i32, fd: i32, }, syscall: extern struct { sysnum: i32, retval: [2]i32, @"error": i32, args: [8]u64, }, ptrace_state: extern struct { pe_report_event: i32, option: extern union { pe_other_pid: pid_t, pe_lwp: lwpid_t, }, }, } align(@sizeOf(usize)), }; pub const _SIG_WORDS = 4; pub const _SIG_MAXSIG = 128; pub inline fn _SIG_IDX(sig: usize) usize { return sig - 1; } pub inline fn _SIG_WORD(sig: usize) usize { return_SIG_IDX(sig) >> 5; } pub inline fn _SIG_BIT(sig: usize) usize { return 1 << (_SIG_IDX(sig) & 31); } pub inline fn _SIG_VALID(sig: usize) usize { return sig <= _SIG_MAXSIG and sig > 0; } pub const sigset_t = extern struct { __bits: [_SIG_WORDS]u32, }; pub const empty_sigset = sigset_t{ .__bits = [_]u32{0} ** _SIG_WORDS }; // XXX x86_64 specific pub const mcontext_t = extern struct { gregs: [26]u64, mc_tlsbase: u64, fpregs: [512]u8 align(8), }; pub const REG_RBP = 12; pub const REG_RIP = 21; pub const REG_RSP = 24; pub const ucontext_t = extern struct { flags: u32, link: ?*ucontext_t, sigmask: sigset_t, stack: stack_t, mcontext: mcontext_t, __pad: [ switch (builtin.cpu.arch) { .i386 => 4, .mips, .mipsel, .mips64, .mips64el => 14, .arm, .armeb, .thumb, .thumbeb => 1, .sparc, .sparcel, .sparcv9 => if (@sizeOf(usize) == 4) 43 else 8, else => 0, } ]u32, }; pub const EPERM = 1; // Operation not permitted pub const ENOENT = 2; // No such file or directory pub const ESRCH = 3; // No such process pub const EINTR = 4; // Interrupted system call pub const EIO = 5; // Input/output error pub const ENXIO = 6; // Device not configured pub const E2BIG = 7; // Argument list too long pub const ENOEXEC = 8; // Exec format error pub const EBADF = 9; // Bad file descriptor pub const ECHILD = 10; // No child processes pub const EDEADLK = 11; // Resource deadlock avoided // 11 was EAGAIN pub const ENOMEM = 12; // Cannot allocate memory pub const EACCES = 13; // Permission denied pub const EFAULT = 14; // Bad address pub const ENOTBLK = 15; // Block device required pub const EBUSY = 16; // Device busy pub const EEXIST = 17; // File exists pub const EXDEV = 18; // Cross-device link pub const ENODEV = 19; // Operation not supported by device pub const ENOTDIR = 20; // Not a directory pub const EISDIR = 21; // Is a directory pub const EINVAL = 22; // Invalid argument pub const ENFILE = 23; // Too many open files in system pub const EMFILE = 24; // Too many open files pub const ENOTTY = 25; // Inappropriate ioctl for device pub const ETXTBSY = 26; // Text file busy pub const EFBIG = 27; // File too large pub const ENOSPC = 28; // No space left on device pub const ESPIPE = 29; // Illegal seek pub const EROFS = 30; // Read-only file system pub const EMLINK = 31; // Too many links pub const EPIPE = 32; // Broken pipe // math software pub const EDOM = 33; // Numerical argument out of domain pub const ERANGE = 34; // Result too large or too small // non-blocking and interrupt i/o pub const EAGAIN = 35; // Resource temporarily unavailable pub const EWOULDBLOCK = EAGAIN; // Operation would block pub const EINPROGRESS = 36; // Operation now in progress pub const EALREADY = 37; // Operation already in progress // ipc/network software -- argument errors pub const ENOTSOCK = 38; // Socket operation on non-socket pub const EDESTADDRREQ = 39; // Destination address required pub const EMSGSIZE = 40; // Message too long pub const EPROTOTYPE = 41; // Protocol wrong type for socket pub const ENOPROTOOPT = 42; // Protocol option not available pub const EPROTONOSUPPORT = 43; // Protocol not supported pub const ESOCKTNOSUPPORT = 44; // Socket type not supported pub const EOPNOTSUPP = 45; // Operation not supported pub const EPFNOSUPPORT = 46; // Protocol family not supported pub const EAFNOSUPPORT = 47; // Address family not supported by protocol family pub const EADDRINUSE = 48; // Address already in use pub const EADDRNOTAVAIL = 49; // Can't assign requested address // ipc/network software -- operational errors pub const ENETDOWN = 50; // Network is down pub const ENETUNREACH = 51; // Network is unreachable pub const ENETRESET = 52; // Network dropped connection on reset pub const ECONNABORTED = 53; // Software caused connection abort pub const ECONNRESET = 54; // Connection reset by peer pub const ENOBUFS = 55; // No buffer space available pub const EISCONN = 56; // Socket is already connected pub const ENOTCONN = 57; // Socket is not connected pub const ESHUTDOWN = 58; // Can't send after socket shutdown pub const ETOOMANYREFS = 59; // Too many references: can't splice pub const ETIMEDOUT = 60; // Operation timed out pub const ECONNREFUSED = 61; // Connection refused pub const ELOOP = 62; // Too many levels of symbolic links pub const ENAMETOOLONG = 63; // File name too long // should be rearranged pub const EHOSTDOWN = 64; // Host is down pub const EHOSTUNREACH = 65; // No route to host pub const ENOTEMPTY = 66; // Directory not empty // quotas & mush pub const EPROCLIM = 67; // Too many processes pub const EUSERS = 68; // Too many users pub const EDQUOT = 69; // Disc quota exceeded // Network File System pub const ESTALE = 70; // Stale NFS file handle pub const EREMOTE = 71; // Too many levels of remote in path pub const EBADRPC = 72; // RPC struct is bad pub const ERPCMISMATCH = 73; // RPC version wrong pub const EPROGUNAVAIL = 74; // RPC prog. not avail pub const EPROGMISMATCH = 75; // Program version wrong pub const EPROCUNAVAIL = 76; // Bad procedure for program pub const ENOLCK = 77; // No locks available pub const ENOSYS = 78; // Function not implemented pub const EFTYPE = 79; // Inappropriate file type or format pub const EAUTH = 80; // Authentication error pub const ENEEDAUTH = 81; // Need authenticator // SystemV IPC pub const EIDRM = 82; // Identifier removed pub const ENOMSG = 83; // No message of desired type pub const EOVERFLOW = 84; // Value too large to be stored in data type // Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 pub const EILSEQ = 85; // Illegal byte sequence // From IEEE Std 1003.1-2001 // Base, Realtime, Threads or Thread Priority Scheduling option errors pub const ENOTSUP = 86; // Not supported // Realtime option errors pub const ECANCELED = 87; // Operation canceled // Realtime, XSI STREAMS option errors pub const EBADMSG = 88; // Bad or Corrupt message // XSI STREAMS option errors pub const ENODATA = 89; // No message available pub const ENOSR = 90; // No STREAM resources pub const ENOSTR = 91; // Not a STREAM pub const ETIME = 92; // STREAM ioctl timeout // File system extended attribute errors pub const ENOATTR = 93; // Attribute not found // Realtime, XSI STREAMS option errors pub const EMULTIHOP = 94; // Multihop attempted pub const ENOLINK = 95; // Link has been severed pub const EPROTO = 96; // Protocol error pub const ELAST = 96; // Must equal largest errno pub const MINSIGSTKSZ = 8192; pub const SIGSTKSZ = MINSIGSTKSZ + 32768; pub const SS_ONSTACK = 1; pub const SS_DISABLE = 4; pub const stack_t = extern struct { ss_sp: [*]u8, ss_size: isize, ss_flags: i32, }; pub const S_IFMT = 0o170000; pub const S_IFIFO = 0o010000; pub const S_IFCHR = 0o020000; pub const S_IFDIR = 0o040000; pub const S_IFBLK = 0o060000; pub const S_IFREG = 0o100000; pub const S_IFLNK = 0o120000; pub const S_IFSOCK = 0o140000; pub const S_IFWHT = 0o160000; pub const S_ISUID = 0o4000; pub const S_ISGID = 0o2000; pub const S_ISVTX = 0o1000; pub const S_IRWXU = 0o700; pub const S_IRUSR = 0o400; pub const S_IWUSR = 0o200; pub const S_IXUSR = 0o100; pub const S_IRWXG = 0o070; pub const S_IRGRP = 0o040; pub const S_IWGRP = 0o020; pub const S_IXGRP = 0o010; pub const S_IRWXO = 0o007; pub const S_IROTH = 0o004; pub const S_IWOTH = 0o002; pub const S_IXOTH = 0o001; pub fn S_ISFIFO(m: u32) bool { return m & S_IFMT == S_IFIFO; } pub fn S_ISCHR(m: u32) bool { return m & S_IFMT == S_IFCHR; } pub fn S_ISDIR(m: u32) bool { return m & S_IFMT == S_IFDIR; } pub fn S_ISBLK(m: u32) bool { return m & S_IFMT == S_IFBLK; } pub fn S_ISREG(m: u32) bool { return m & S_IFMT == S_IFREG; } pub fn S_ISLNK(m: u32) bool { return m & S_IFMT == S_IFLNK; } pub fn S_ISSOCK(m: u32) bool { return m & S_IFMT == S_IFSOCK; } pub fn S_IWHT(m: u32) bool { return m & S_IFMT == S_IFWHT; } /// Magic value that specify the use of the current working directory /// to determine the target of relative file paths in the openat() and /// similar syscalls. pub const AT_FDCWD = -100; /// Check access using effective user and group ID pub const AT_EACCESS = 0x0100; /// Do not follow symbolic links pub const AT_SYMLINK_NOFOLLOW = 0x0200; /// Follow symbolic link pub const AT_SYMLINK_FOLLOW = 0x0400; /// Remove directory instead of file pub const AT_REMOVEDIR = 0x0800; pub const HOST_NAME_MAX = 255; /// dummy for IP pub const IPPROTO_IP = 0; /// IP6 hop-by-hop options pub const IPPROTO_HOPOPTS = 0; /// control message protocol pub const IPPROTO_ICMP = 1; /// group mgmt protocol pub const IPPROTO_IGMP = 2; /// gateway^2 (deprecated) pub const IPPROTO_GGP = 3; /// IP header pub const IPPROTO_IPV4 = 4; /// IP inside IP pub const IPPROTO_IPIP = 4; /// tcp pub const IPPROTO_TCP = 6; /// exterior gateway protocol pub const IPPROTO_EGP = 8; /// pup pub const IPPROTO_PUP = 12; /// user datagram protocol pub const IPPROTO_UDP = 17; /// xns idp pub const IPPROTO_IDP = 22; /// tp-4 w/ class negotiation pub const IPPROTO_TP = 29; /// DCCP pub const IPPROTO_DCCP = 33; /// IP6 header pub const IPPROTO_IPV6 = 41; /// IP6 routing header pub const IPPROTO_ROUTING = 43; /// IP6 fragmentation header pub const IPPROTO_FRAGMENT = 44; /// resource reservation pub const IPPROTO_RSVP = 46; /// GRE encaps RFC 1701 pub const IPPROTO_GRE = 47; /// encap. security payload pub const IPPROTO_ESP = 50; /// authentication header pub const IPPROTO_AH = 51; /// IP Mobility RFC 2004 pub const IPPROTO_MOBILE = 55; /// IPv6 ICMP pub const IPPROTO_IPV6_ICMP = 58; /// ICMP6 pub const IPPROTO_ICMPV6 = 58; /// IP6 no next header pub const IPPROTO_NONE = 59; /// IP6 destination option pub const IPPROTO_DSTOPTS = 60; /// ISO cnlp pub const IPPROTO_EON = 80; /// Ethernet-in-IP pub const IPPROTO_ETHERIP = 97; /// encapsulation header pub const IPPROTO_ENCAP = 98; /// Protocol indep. multicast pub const IPPROTO_PIM = 103; /// IP Payload Comp. Protocol pub const IPPROTO_IPCOMP = 108; /// VRRP RFC 2338 pub const IPPROTO_VRRP = 112; /// Common Address Resolution Protocol pub const IPPROTO_CARP = 112; /// L2TPv3 pub const IPPROTO_L2TP = 115; /// SCTP pub const IPPROTO_SCTP = 132; /// PFSYNC pub const IPPROTO_PFSYNC = 240; /// raw IP packet pub const IPPROTO_RAW = 255; pub const rlimit_resource = enum(c_int) { CPU = 0, FSIZE = 1, DATA = 2, STACK = 3, CORE = 4, RSS = 5, MEMLOCK = 6, NPROC = 7, NOFILE = 8, SBSIZE = 9, VMEM = 10, NTHR = 11, _, pub const AS: rlimit_resource = .VMEM; }; pub const rlim_t = u64; /// No limit pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1; pub const RLIM_SAVED_MAX = RLIM_INFINITY; pub const RLIM_SAVED_CUR = RLIM_INFINITY; pub const rlimit = extern struct { /// Soft limit cur: rlim_t, /// Hard limit max: rlim_t, }; pub const SHUT_RD = 0; pub const SHUT_WR = 1; pub const SHUT_RDWR = 2; pub const nfds_t = u32; pub const pollfd = extern struct { fd: fd_t, events: i16, revents: i16, }; /// Testable events (may be specified in events field). pub const POLLIN = 0x0001; pub const POLLPRI = 0x0002; pub const POLLOUT = 0x0004; pub const POLLRDNORM = 0x0040; pub const POLLWRNORM = POLLOUT; pub const POLLRDBAND = 0x0080; pub const POLLWRBAND = 0x0100; /// Non-testable events (may not be specified in events field). pub const POLLERR = 0x0008; pub const POLLHUP = 0x0010; pub const POLLNVAL = 0x0020;
// TODO: Remove condition after deprecating 'typeOf'. See https://github.com/ziglang/zig/issues/1348 test "zig fmt: change @typeOf to @TypeOf" { try testTransform( \\const a = @typeOf(@as(usize, 10)); \\ , \\const a = @TypeOf(@as(usize, 10)); \\ ); } // TODO: Remove nakedcc/stdcallcc once zig 0.6.0 is released. See https://github.com/ziglang/zig/pull/3977 test "zig fmt: convert extern/nakedcc/stdcallcc into callconv(...)" { try testTransform( \\nakedcc fn foo1() void {} \\stdcallcc fn foo2() void {} \\extern fn foo3() void {} , \\fn foo1() callconv(.Naked) void {} \\fn foo2() callconv(.Stdcall) void {} \\fn foo3() callconv(.C) void {} \\ ); } test "zig fmt: comptime struct field" { try testCanonical( \\const Foo = struct { \\ a: i32, \\ comptime b: i32 = 1234, \\}; \\ ); } test "zig fmt: c pointer type" { try testCanonical( \\pub extern fn repro() [*c]const u8; \\ ); } test "zig fmt: builtin call with trailing comma" { try testCanonical( \\pub fn main() void { \\ @breakpoint(); \\ _ = @boolToInt(a); \\ _ = @call( \\ a, \\ b, \\ c, \\ ); \\} \\ ); } test "zig fmt: asm expression with comptime content" { try testCanonical( \\comptime { \\ asm ("foo" ++ "bar"); \\} \\pub fn main() void { \\ asm volatile ("foo" ++ "bar"); \\ asm volatile ("foo" ++ "bar" \\ : [_] "" (x) \\ ); \\ asm volatile ("foo" ++ "bar" \\ : [_] "" (x) \\ : [_] "" (y) \\ ); \\ asm volatile ("foo" ++ "bar" \\ : [_] "" (x) \\ : [_] "" (y) \\ : "h", "e", "l", "l", "o" \\ ); \\} \\ ); } test "zig fmt: var struct field" { try testCanonical( \\pub const Pointer = struct { \\ sentinel: var, \\}; \\ ); } test "zig fmt: sentinel-terminated array type" { try testCanonical( \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 { \\ return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); \\} \\ ); } test "zig fmt: sentinel-terminated slice type" { try testCanonical( \\pub fn toSlice(self: Buffer) [:0]u8 { \\ return self.list.toSlice()[0..self.len()]; \\} \\ ); } test "zig fmt: anon literal in array" { try testCanonical( \\var arr: [2]Foo = .{ \\ .{ .a = 2 }, \\ .{ .b = 3 }, \\}; \\ ); } test "zig fmt: anon struct literal syntax" { try testCanonical( \\const x = .{ \\ .a = b, \\ .c = d, \\}; \\ ); } test "zig fmt: anon list literal syntax" { try testCanonical( \\const x = .{ a, b, c }; \\ ); } test "zig fmt: async function" { try testCanonical( \\pub const Server = struct { \\ handleRequestFn: async fn (*Server, *const std.net.Address, File) void, \\}; \\test "hi" { \\ var ptr = @ptrCast(async fn (i32) void, other); \\} \\ ); } test "zig fmt: whitespace fixes" { try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}\n// zig fmt: off\ntest \"\"{\r\n\tconst a = b;}\r\n", \\test "" { \\ const hi = x; \\} \\// zig fmt: off \\test ""{ \\ const a = b;} \\ ); } test "zig fmt: while else err prong with no block" { try testCanonical( \\test "" { \\ const result = while (returnError()) |value| { \\ break value; \\ } else |err| @as(i32, 2); \\ expect(result == 2); \\} \\ ); } test "zig fmt: tagged union with enum values" { try testCanonical( \\const MultipleChoice2 = union(enum(u32)) { \\ Unspecified1: i32, \\ A: f32 = 20, \\ Unspecified2: void, \\ B: bool = 40, \\ Unspecified3: i32, \\ C: i8 = 60, \\ Unspecified4: void, \\ D: void = 1000, \\ Unspecified5: i32, \\}; \\ ); } test "zig fmt: allowzero pointer" { try testCanonical( \\const T = [*]allowzero const u8; \\ ); } test "zig fmt: enum literal" { try testCanonical( \\const x = .hi; \\ ); } test "zig fmt: enum literal inside array literal" { try testCanonical( \\test "enums in arrays" { \\ var colors = []Color{.Green}; \\ colors = []Colors{ .Green, .Cyan }; \\ colors = []Colors{ \\ .Grey, \\ .Green, \\ .Cyan, \\ }; \\} \\ ); } test "zig fmt: character literal larger than u8" { try testCanonical( \\const x = '\u{01f4a9}'; \\ ); } test "zig fmt: infix operator and then multiline string literal" { try testCanonical( \\const x = "" ++ \\ \\ hi \\; \\ ); } test "zig fmt: C pointers" { try testCanonical( \\const Ptr = [*c]i32; \\ ); } test "zig fmt: threadlocal" { try testCanonical( \\threadlocal var x: i32 = 1234; \\ ); } test "zig fmt: linksection" { try testCanonical( \\export var aoeu: u64 linksection(".text.derp") = 1234; \\export fn _start() linksection(".text.boot") callconv(.Naked) noreturn {} \\ ); } test "zig fmt: correctly move doc comments on struct fields" { try testTransform( \\pub const section_64 = extern struct { \\ sectname: [16]u8, /// name of this section \\ segname: [16]u8, /// segment this section goes in \\}; , \\pub const section_64 = extern struct { \\ /// name of this section \\ sectname: [16]u8, \\ /// segment this section goes in \\ segname: [16]u8, \\}; \\ ); } test "zig fmt: doc comments on param decl" { try testCanonical( \\pub const Allocator = struct { \\ shrinkFn: fn ( \\ self: *Allocator, \\ /// Guaranteed to be the same as what was returned from most recent call to \\ /// `allocFn`, `reallocFn`, or `shrinkFn`. \\ old_mem: []u8, \\ /// Guaranteed to be the same as what was returned from most recent call to \\ /// `allocFn`, `reallocFn`, or `shrinkFn`. \\ old_alignment: u29, \\ /// Guaranteed to be less than or equal to `old_mem.len`. \\ new_byte_count: usize, \\ /// Guaranteed to be less than or equal to `old_alignment`. \\ new_alignment: u29, \\ ) []u8, \\}; \\ ); } test "zig fmt: aligned struct field" { try testCanonical( \\pub const S = struct { \\ f: i32 align(32), \\}; \\ ); try testCanonical( \\pub const S = struct { \\ f: i32 align(32) = 1, \\}; \\ ); } test "zig fmt: preserve space between async fn definitions" { try testCanonical( \\async fn a() void {} \\ \\async fn b() void {} \\ ); } test "zig fmt: comment to disable/enable zig fmt first" { try testCanonical( \\// Test trailing comma syntax \\// zig fmt: off \\ \\const struct_trailing_comma = struct { x: i32, y: i32, }; ); } test "zig fmt: comment to disable/enable zig fmt" { try testTransform( \\const a = b; \\// zig fmt: off \\const c = d; \\// zig fmt: on \\const e = f; , \\const a = b; \\// zig fmt: off \\const c = d; \\// zig fmt: on \\const e = f; \\ ); } test "zig fmt: line comment following 'zig fmt: off'" { try testCanonical( \\// zig fmt: off \\// Test \\const e = f; ); } test "zig fmt: doc comment following 'zig fmt: off'" { try testCanonical( \\// zig fmt: off \\/// test \\const e = f; ); } test "zig fmt: line and doc comment following 'zig fmt: off'" { try testCanonical( \\// zig fmt: off \\// test 1 \\/// test 2 \\const e = f; ); } test "zig fmt: doc and line comment following 'zig fmt: off'" { try testCanonical( \\// zig fmt: off \\/// test 1 \\// test 2 \\const e = f; ); } test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" { try testCanonical( \\// zig fmt: off \\// zig fmt: on \\// zig fmt: off \\const e = f; \\// zig fmt: off \\// zig fmt: on \\// zig fmt: off \\const a = b; \\// zig fmt: on \\const c = d; \\// zig fmt: on \\ ); } test "zig fmt: line comment following 'zig fmt: on'" { try testCanonical( \\// zig fmt: off \\const e = f; \\// zig fmt: on \\// test \\const e = f; \\ ); } test "zig fmt: doc comment following 'zig fmt: on'" { try testCanonical( \\// zig fmt: off \\const e = f; \\// zig fmt: on \\/// test \\const e = f; \\ ); } test "zig fmt: line and doc comment following 'zig fmt: on'" { try testCanonical( \\// zig fmt: off \\const e = f; \\// zig fmt: on \\// test1 \\/// test2 \\const e = f; \\ ); } test "zig fmt: doc and line comment following 'zig fmt: on'" { try testCanonical( \\// zig fmt: off \\const e = f; \\// zig fmt: on \\/// test1 \\// test2 \\const e = f; \\ ); } test "zig fmt: pointer of unknown length" { try testCanonical( \\fn foo(ptr: [*]u8) void {} \\ ); } test "zig fmt: spaces around slice operator" { try testCanonical( \\var a = b[c..d]; \\var a = b[c..d :0]; \\var a = b[c + 1 .. d]; \\var a = b[c + 1 ..]; \\var a = b[c .. d + 1]; \\var a = b[c .. d + 1 :0]; \\var a = b[c.a..d.e]; \\var a = b[c.a..d.e :0]; \\ ); } test "zig fmt: async call in if condition" { try testCanonical( \\comptime { \\ if (async b()) { \\ a(); \\ } \\} \\ ); } test "zig fmt: 2nd arg multiline string" { try testCanonical( \\comptime { \\ cases.addAsm("hello world linux x86_64", \\ \\.text \\ , "Hello, world!\n"); \\} \\ ); } test "zig fmt: if condition wraps" { try testTransform( \\comptime { \\ if (cond and \\ cond) { \\ return x; \\ } \\ while (cond and \\ cond) { \\ return x; \\ } \\ if (a == b and \\ c) { \\ a = b; \\ } \\ while (a == b and \\ c) { \\ a = b; \\ } \\ if ((cond and \\ cond)) { \\ return x; \\ } \\ while ((cond and \\ cond)) { \\ return x; \\ } \\ var a = if (a) |*f| x: { \\ break :x &a.b; \\ } else |err| err; \\} , \\comptime { \\ if (cond and \\ cond) \\ { \\ return x; \\ } \\ while (cond and \\ cond) \\ { \\ return x; \\ } \\ if (a == b and \\ c) \\ { \\ a = b; \\ } \\ while (a == b and \\ c) \\ { \\ a = b; \\ } \\ if ((cond and \\ cond)) \\ { \\ return x; \\ } \\ while ((cond and \\ cond)) \\ { \\ return x; \\ } \\ var a = if (a) |*f| x: { \\ break :x &a.b; \\ } else |err| err; \\} \\ ); } test "zig fmt: if condition has line break but must not wrap" { try testCanonical( \\comptime { \\ if (self.user_input_options.put(name, UserInputOption{ \\ .name = name, \\ .used = false, \\ }) catch unreachable) |*prev_value| { \\ foo(); \\ bar(); \\ } \\ if (put( \\ a, \\ b, \\ )) { \\ foo(); \\ } \\} \\ ); } test "zig fmt: same-line doc comment on variable declaration" { try testTransform( \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space \\pub const MAP_FILE = 0x0000; /// map from file (default) \\ \\pub const EMEDIUMTYPE = 124; /// Wrong medium type \\ \\// nameserver query return codes \\pub const ENSROK = 0; /// DNS server returned answer with no data , \\/// allocated from memory, swap space \\pub const MAP_ANONYMOUS = 0x1000; \\/// map from file (default) \\pub const MAP_FILE = 0x0000; \\ \\/// Wrong medium type \\pub const EMEDIUMTYPE = 124; \\ \\// nameserver query return codes \\/// DNS server returned answer with no data \\pub const ENSROK = 0; \\ ); } test "zig fmt: if-else with comment before else" { try testCanonical( \\comptime { \\ // cexp(finite|nan +- i inf|nan) = nan + i nan \\ if ((hx & 0x7fffffff) != 0x7f800000) { \\ return Complex(f32).new(y - y, y - y); \\ } // cexp(-inf +- i inf|nan) = 0 + i0 \\ else if (hx & 0x80000000 != 0) { \\ return Complex(f32).new(0, 0); \\ } // cexp(+inf +- i inf|nan) = inf + i nan \\ else { \\ return Complex(f32).new(x, y - y); \\ } \\} \\ ); } test "zig fmt: if nested" { try testCanonical( \\pub fn foo() void { \\ return if ((aInt & bInt) >= 0) \\ if (aInt < bInt) \\ GE_LESS \\ else if (aInt == bInt) \\ GE_EQUAL \\ else \\ GE_GREATER \\ else if (aInt > bInt) \\ GE_LESS \\ else if (aInt == bInt) \\ GE_EQUAL \\ else \\ GE_GREATER; \\} \\ ); } test "zig fmt: respect line breaks in if-else" { try testCanonical( \\comptime { \\ return if (cond) a else b; \\ return if (cond) \\ a \\ else \\ b; \\ return if (cond) \\ a \\ else if (cond) \\ b \\ else \\ c; \\} \\ ); } test "zig fmt: respect line breaks after infix operators" { try testCanonical( \\comptime { \\ self.crc = \\ lookup_tables[0][p[7]] ^ \\ lookup_tables[1][p[6]] ^ \\ lookup_tables[2][p[5]] ^ \\ lookup_tables[3][p[4]] ^ \\ lookup_tables[4][@truncate(u8, self.crc >> 24)] ^ \\ lookup_tables[5][@truncate(u8, self.crc >> 16)] ^ \\ lookup_tables[6][@truncate(u8, self.crc >> 8)] ^ \\ lookup_tables[7][@truncate(u8, self.crc >> 0)]; \\} \\ ); } test "zig fmt: fn decl with trailing comma" { try testTransform( \\fn foo(a: i32, b: i32,) void {} , \\fn foo( \\ a: i32, \\ b: i32, \\) void {} \\ ); } test "zig fmt: enum decl with no trailing comma" { try testTransform( \\const StrLitKind = enum {Normal, C}; , \\const StrLitKind = enum { \\ Normal, \\ C, \\}; \\ ); } test "zig fmt: switch comment before prong" { try testCanonical( \\comptime { \\ switch (a) { \\ // hi \\ 0 => {}, \\ } \\} \\ ); } test "zig fmt: struct literal no trailing comma" { try testTransform( \\const a = foo{ .x = 1, .y = 2 }; \\const a = foo{ .x = 1, \\ .y = 2 }; , \\const a = foo{ .x = 1, .y = 2 }; \\const a = foo{ \\ .x = 1, \\ .y = 2, \\}; \\ ); } test "zig fmt: struct literal containing a multiline expression" { try testTransform( \\const a = A{ .x = if (f1()) 10 else 20 }; \\const a = A{ .x = if (f1()) 10 else 20, }; \\const a = A{ .x = if (f1()) \\ 10 else 20 }; \\const a = A{ .x = if (f1()) 10 else 20, .y = f2() + 100 }; \\const a = A{ .x = if (f1()) 10 else 20, .y = f2() + 100, }; \\const a = A{ .x = if (f1()) \\ 10 else 20}; \\const a = A{ .x = switch(g) {0 => "ok", else => "no"} }; \\ , \\const a = A{ .x = if (f1()) 10 else 20 }; \\const a = A{ \\ .x = if (f1()) 10 else 20, \\}; \\const a = A{ \\ .x = if (f1()) \\ 10 \\ else \\ 20, \\}; \\const a = A{ .x = if (f1()) 10 else 20, .y = f2() + 100 }; \\const a = A{ \\ .x = if (f1()) 10 else 20, \\ .y = f2() + 100, \\}; \\const a = A{ \\ .x = if (f1()) \\ 10 \\ else \\ 20, \\}; \\const a = A{ \\ .x = switch (g) { \\ 0 => "ok", \\ else => "no", \\ }, \\}; \\ ); } test "zig fmt: array literal with hint" { try testTransform( \\const a = []u8{ \\ 1, 2, // \\ 3, \\ 4, \\ 5, \\ 6, \\ 7 }; \\const a = []u8{ \\ 1, 2, // \\ 3, \\ 4, \\ 5, \\ 6, \\ 7, 8 }; \\const a = []u8{ \\ 1, 2, // \\ 3, \\ 4, \\ 5, \\ 6, // blah \\ 7, 8 }; \\const a = []u8{ \\ 1, 2, // \\ 3, // \\ 4, \\ 5, \\ 6, \\ 7 }; \\const a = []u8{ \\ 1, \\ 2, \\ 3, 4, // \\ 5, 6, // \\ 7, 8, // \\}; , \\const a = []u8{ \\ 1, 2, \\ 3, 4, \\ 5, 6, \\ 7, \\}; \\const a = []u8{ \\ 1, 2, \\ 3, 4, \\ 5, 6, \\ 7, 8, \\}; \\const a = []u8{ \\ 1, 2, \\ 3, 4, \\ 5, 6, // blah \\ 7, 8, \\}; \\const a = []u8{ \\ 1, 2, \\ 3, // \\ 4, \\ 5, 6, \\ 7, \\}; \\const a = []u8{ \\ 1, \\ 2, \\ 3, \\ 4, \\ 5, \\ 6, \\ 7, \\ 8, \\}; \\ ); } test "zig fmt: array literal veritical column alignment" { try testTransform( \\const a = []u8{ \\ 1000, 200, \\ 30, 4, \\ 50000, 60 \\}; \\const a = []u8{0, 1, 2, 3, 40, \\ 4,5,600,7, \\ 80, \\ 9, 10, 11, 0, 13, 14, 15}; \\ , \\const a = []u8{ \\ 1000, 200, \\ 30, 4, \\ 50000, 60, \\}; \\const a = []u8{ \\ 0, 1, 2, 3, 40, \\ 4, 5, 600, 7, 80, \\ 9, 10, 11, 0, 13, \\ 14, 15, \\}; \\ ); } test "zig fmt: multiline string with backslash at end of line" { try testCanonical( \\comptime { \\ err( \\ \\\ \\ ); \\} \\ ); } test "zig fmt: multiline string parameter in fn call with trailing comma" { try testCanonical( \\fn foo() void { \\ try stdout.print( \\ \\ZIG_CMAKE_BINARY_DIR {} \\ \\ZIG_C_HEADER_FILES {} \\ \\ZIG_DIA_GUIDS_LIB {} \\ \\ \\ , \\ std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR), \\ std.cstr.toSliceConst(c.ZIG_CXX_COMPILER), \\ std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB), \\ ); \\} \\ ); } test "zig fmt: trailing comma on fn call" { try testCanonical( \\comptime { \\ var module = try Module.create( \\ allocator, \\ zig_lib_dir, \\ full_cache_dir, \\ ); \\} \\ ); } test "zig fmt: multi line arguments without last comma" { try testTransform( \\pub fn foo( \\ a: usize, \\ b: usize, \\ c: usize, \\ d: usize \\) usize { \\ return a + b + c + d; \\} \\ , \\pub fn foo(a: usize, b: usize, c: usize, d: usize) usize { \\ return a + b + c + d; \\} \\ ); } test "zig fmt: empty block with only comment" { try testCanonical( \\comptime { \\ { \\ // comment \\ } \\} \\ ); } test "zig fmt: no trailing comma on struct decl" { try testTransform( \\const RoundParam = struct { \\ k: usize, s: u32, t: u32 \\}; , \\const RoundParam = struct { \\ k: usize, \\ s: u32, \\ t: u32, \\}; \\ ); } test "zig fmt: extra newlines at the end" { try testTransform( \\const a = b; \\ \\ \\ , \\const a = b; \\ ); } test "zig fmt: simple asm" { try testTransform( \\comptime { \\ asm volatile ( \\ \\.globl aoeu; \\ \\.type aoeu, @function; \\ \\.set aoeu, derp; \\ ); \\ \\ asm ("not real assembly" \\ :[a] "x" (x),); \\ asm ("not real assembly" \\ :[a] "x" (->i32),:[a] "x" (1),); \\ asm ("still not real assembly" \\ :::"a","b",); \\} , \\comptime { \\ asm volatile ( \\ \\.globl aoeu; \\ \\.type aoeu, @function; \\ \\.set aoeu, derp; \\ ); \\ \\ asm ("not real assembly" \\ : [a] "x" (x) \\ ); \\ asm ("not real assembly" \\ : [a] "x" (-> i32) \\ : [a] "x" (1) \\ ); \\ asm ("still not real assembly" \\ : \\ : \\ : "a", "b" \\ ); \\} \\ ); } test "zig fmt: nested struct literal with one item" { try testCanonical( \\const a = foo{ \\ .item = bar{ .a = b }, \\}; \\ ); } test "zig fmt: switch cases trailing comma" { try testTransform( \\fn switch_cases(x: i32) void { \\ switch (x) { \\ 1,2,3 => {}, \\ 4,5, => {}, \\ 6... 8, => {}, \\ else => {}, \\ } \\} , \\fn switch_cases(x: i32) void { \\ switch (x) { \\ 1, 2, 3 => {}, \\ 4, \\ 5, \\ => {}, \\ 6...8 => {}, \\ else => {}, \\ } \\} \\ ); } test "zig fmt: slice align" { try testCanonical( \\const A = struct { \\ items: []align(A) T, \\}; \\ ); } test "zig fmt: add trailing comma to array literal" { try testTransform( \\comptime { \\ return []u16{'m', 's', 'y', 's', '-' // hi \\ }; \\ return []u16{'m', 's', 'y', 's', \\ '-'}; \\ return []u16{'m', 's', 'y', 's', '-'}; \\} , \\comptime { \\ return []u16{ \\ 'm', 's', 'y', 's', '-', // hi \\ }; \\ return []u16{ \\ 'm', 's', 'y', 's', \\ '-', \\ }; \\ return []u16{ 'm', 's', 'y', 's', '-' }; \\} \\ ); } test "zig fmt: first thing in file is line comment" { try testCanonical( \\// Introspection and determination of system libraries needed by zig. \\ \\// Introspection and determination of system libraries needed by zig. \\ \\const std = @import("std"); \\ ); } test "zig fmt: line comment after doc comment" { try testCanonical( \\/// doc comment \\// line comment \\fn foo() void {} \\ ); } test "zig fmt: float literal with exponent" { try testCanonical( \\test "bit field alignment" { \\ assert(@TypeOf(&blah.b) == *align(1:3:6) const u3); \\} \\ ); } test "zig fmt: float literal with exponent" { try testCanonical( \\test "aoeu" { \\ switch (state) { \\ TermState.Start => switch (c) { \\ '\x1b' => state = TermState.Escape, \\ else => try out.writeByte(c), \\ }, \\ } \\} \\ ); } test "zig fmt: float literal with exponent" { try testCanonical( \\pub const f64_true_min = 4.94065645841246544177e-324; \\const threshold = 0x1.a827999fcef32p+1022; \\ ); } test "zig fmt: if-else end of comptime" { try testCanonical( \\comptime { \\ if (a) { \\ b(); \\ } else { \\ b(); \\ } \\} \\ ); } test "zig fmt: nested blocks" { try testCanonical( \\comptime { \\ { \\ { \\ { \\ a(); \\ } \\ } \\ } \\} \\ ); } test "zig fmt: block with same line comment after end brace" { try testCanonical( \\comptime { \\ { \\ b(); \\ } // comment \\} \\ ); } test "zig fmt: statements with comment between" { try testCanonical( \\comptime { \\ a = b; \\ // comment \\ a = b; \\} \\ ); } test "zig fmt: statements with empty line between" { try testCanonical( \\comptime { \\ a = b; \\ \\ a = b; \\} \\ ); } test "zig fmt: ptr deref operator and unwrap optional operator" { try testCanonical( \\const a = b.*; \\const a = b.?; \\ ); } test "zig fmt: comment after if before another if" { try testCanonical( \\test "aoeu" { \\ // comment \\ if (x) { \\ bar(); \\ } \\} \\ \\test "aoeu" { \\ if (x) { \\ foo(); \\ } \\ // comment \\ if (x) { \\ bar(); \\ } \\} \\ ); } test "zig fmt: line comment between if block and else keyword" { try testCanonical( \\test "aoeu" { \\ // cexp(finite|nan +- i inf|nan) = nan + i nan \\ if ((hx & 0x7fffffff) != 0x7f800000) { \\ return Complex(f32).new(y - y, y - y); \\ } \\ // cexp(-inf +- i inf|nan) = 0 + i0 \\ else if (hx & 0x80000000 != 0) { \\ return Complex(f32).new(0, 0); \\ } \\ // cexp(+inf +- i inf|nan) = inf + i nan \\ // another comment \\ else { \\ return Complex(f32).new(x, y - y); \\ } \\} \\ ); } test "zig fmt: same line comments in expression" { try testCanonical( \\test "aoeu" { \\ const x = ( // a \\ 0 // b \\ ); // c \\} \\ ); } test "zig fmt: add comma on last switch prong" { try testTransform( \\test "aoeu" { \\switch (self.init_arg_expr) { \\ InitArg.Type => |t| { }, \\ InitArg.None, \\ InitArg.Enum => { } \\} \\ switch (self.init_arg_expr) { \\ InitArg.Type => |t| { }, \\ InitArg.None, \\ InitArg.Enum => { }//line comment \\ } \\} , \\test "aoeu" { \\ switch (self.init_arg_expr) { \\ InitArg.Type => |t| {}, \\ InitArg.None, InitArg.Enum => {}, \\ } \\ switch (self.init_arg_expr) { \\ InitArg.Type => |t| {}, \\ InitArg.None, InitArg.Enum => {}, //line comment \\ } \\} \\ ); } test "zig fmt: same-line comment after a statement" { try testCanonical( \\test "" { \\ a = b; \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption \\ a = b; \\} \\ ); } test "zig fmt: same-line comment after var decl in struct" { try testCanonical( \\pub const vfs_cap_data = extern struct { \\ const Data = struct {}; // when on disk. \\}; \\ ); } test "zig fmt: same-line comment after field decl" { try testCanonical( \\pub const dirent = extern struct { \\ d_name: u8, \\ d_name: u8, // comment 1 \\ d_name: u8, \\ d_name: u8, // comment 2 \\ d_name: u8, \\}; \\ ); } test "zig fmt: same-line comment after switch prong" { try testCanonical( \\test "" { \\ switch (err) { \\ error.PathAlreadyExists => {}, // comment 2 \\ else => return err, // comment 1 \\ } \\} \\ ); } test "zig fmt: same-line comment after non-block if expression" { try testCanonical( \\comptime { \\ if (sr > n_uword_bits - 1) // d > r \\ return 0; \\} \\ ); } test "zig fmt: same-line comment on comptime expression" { try testCanonical( \\test "" { \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt \\} \\ ); } test "zig fmt: switch with empty body" { try testCanonical( \\test "" { \\ foo() catch |err| switch (err) {}; \\} \\ ); } test "zig fmt: line comments in struct initializer" { try testCanonical( \\fn foo() void { \\ return Self{ \\ .a = b, \\ \\ // Initialize these two fields to buffer_size so that \\ // in `readFn` we treat the state as being able to read \\ .start_index = buffer_size, \\ .end_index = buffer_size, \\ \\ // middle \\ \\ .a = b, \\ \\ // end \\ }; \\} \\ ); } test "zig fmt: first line comment in struct initializer" { try testCanonical( \\pub async fn acquire(self: *Self) HeldLock { \\ return HeldLock{ \\ // guaranteed allocation elision \\ .held = self.lock.acquire(), \\ .value = &self.private_data, \\ }; \\} \\ ); } test "zig fmt: doc comments before struct field" { try testCanonical( \\pub const Allocator = struct { \\ /// Allocate byte_count bytes and return them in a slice, with the \\ /// slice's pointer aligned at least to alignment bytes. \\ allocFn: fn () void, \\}; \\ ); } test "zig fmt: error set declaration" { try testCanonical( \\const E = error{ \\ A, \\ B, \\ \\ C, \\}; \\ \\const Error = error{ \\ /// no more memory \\ OutOfMemory, \\}; \\ \\const Error = error{ \\ /// no more memory \\ OutOfMemory, \\ \\ /// another \\ Another, \\ \\ // end \\}; \\ \\const Error = error{OutOfMemory}; \\const Error = error{}; \\ ); } test "zig fmt: union(enum(u32)) with assigned enum values" { try testCanonical( \\const MultipleChoice = union(enum(u32)) { \\ A = 20, \\ B = 40, \\ C = 60, \\ D = 1000, \\}; \\ ); } test "zig fmt: resume from suspend block" { try testCanonical( \\fn foo() void { \\ suspend { \\ resume @frame(); \\ } \\} \\ ); } test "zig fmt: comments before error set decl" { try testCanonical( \\const UnexpectedError = error{ \\ /// The Operating System returned an undocumented error code. \\ Unexpected, \\ // another \\ Another, \\ \\ // in between \\ \\ // at end \\}; \\ ); } test "zig fmt: comments before switch prong" { try testCanonical( \\test "" { \\ switch (err) { \\ error.PathAlreadyExists => continue, \\ \\ // comment 1 \\ \\ // comment 2 \\ else => return err, \\ // at end \\ } \\} \\ ); } test "zig fmt: comments before var decl in struct" { try testCanonical( \\pub const vfs_cap_data = extern struct { \\ // All of these are mandated as little endian \\ // when on disk. \\ const Data = struct { \\ permitted: u32, \\ inheritable: u32, \\ }; \\ \\ // in between \\ \\ /// All of these are mandated as little endian \\ /// when on disk. \\ const Data = struct { \\ permitted: u32, \\ inheritable: u32, \\ }; \\ \\ // at end \\}; \\ ); } test "zig fmt: array literal with 1 item on 1 line" { try testCanonical( \\var s = []const u64{0} ** 25; \\ ); } test "zig fmt: comments before global variables" { try testCanonical( \\/// Foo copies keys and values before they go into the map, and \\/// frees them when they get removed. \\pub const Foo = struct {}; \\ ); } test "zig fmt: comments in statements" { try testCanonical( \\test "std" { \\ // statement comment \\ _ = @import("foo/bar.zig"); \\ \\ // middle \\ // middle2 \\ \\ // end \\} \\ ); } test "zig fmt: comments before test decl" { try testCanonical( \\/// top level doc comment \\test "hi" {} \\ \\// top level normal comment \\test "hi" {} \\ \\// middle \\ \\// end \\ ); } test "zig fmt: preserve spacing" { try testCanonical( \\const std = @import("std"); \\ \\pub fn main() !void { \\ var stdout_file = std.io.getStdOut; \\ var stdout_file = std.io.getStdOut; \\ \\ var stdout_file = std.io.getStdOut; \\ var stdout_file = std.io.getStdOut; \\} \\ ); } test "zig fmt: return types" { try testCanonical( \\pub fn main() !void {} \\pub fn main() var {} \\pub fn main() i32 {} \\ ); } test "zig fmt: imports" { try testCanonical( \\const std = @import("std"); \\const std = @import(); \\ ); } test "zig fmt: global declarations" { try testCanonical( \\const a = b; \\pub const a = b; \\var a = b; \\pub var a = b; \\const a: i32 = b; \\pub const a: i32 = b; \\var a: i32 = b; \\pub var a: i32 = b; \\extern const a: i32 = b; \\pub extern const a: i32 = b; \\extern var a: i32 = b; \\pub extern var a: i32 = b; \\extern "a" const a: i32 = b; \\pub extern "a" const a: i32 = b; \\extern "a" var a: i32 = b; \\pub extern "a" var a: i32 = b; \\ ); } test "zig fmt: extern declaration" { try testCanonical( \\extern var foo: c_int; \\ ); } test "zig fmt: alignment" { try testCanonical( \\var foo: c_int align(1); \\ ); } test "zig fmt: C main" { try testCanonical( \\fn main(argc: c_int, argv: **u8) c_int { \\ const a = b; \\} \\ ); } test "zig fmt: return" { try testCanonical( \\fn foo(argc: c_int, argv: **u8) c_int { \\ return 0; \\} \\ \\fn bar() void { \\ return; \\} \\ ); } test "zig fmt: pointer attributes" { try testCanonical( \\extern fn f1(s: *align(*u8) u8) c_int; \\extern fn f2(s: **align(1) *const *volatile u8) c_int; \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int; \\extern fn f4(s: *align(1) const volatile u8) c_int; \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int; \\ ); } test "zig fmt: slice attributes" { try testCanonical( \\extern fn f1(s: *align(*u8) u8) c_int; \\extern fn f2(s: **align(1) *const *volatile u8) c_int; \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int; \\extern fn f4(s: *align(1) const volatile u8) c_int; \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int; \\ ); } test "zig fmt: test declaration" { try testCanonical( \\test "test name" { \\ const a = 1; \\ var b = 1; \\} \\ ); } test "zig fmt: infix operators" { try testCanonical( \\test "infix operators" { \\ var i = undefined; \\ i = 2; \\ i *= 2; \\ i |= 2; \\ i ^= 2; \\ i <<= 2; \\ i >>= 2; \\ i &= 2; \\ i *= 2; \\ i *%= 2; \\ i -= 2; \\ i -%= 2; \\ i += 2; \\ i +%= 2; \\ i /= 2; \\ i %= 2; \\ _ = i == i; \\ _ = i != i; \\ _ = i != i; \\ _ = i.i; \\ _ = i || i; \\ _ = i!i; \\ _ = i ** i; \\ _ = i ++ i; \\ _ = i orelse i; \\ _ = i % i; \\ _ = i / i; \\ _ = i *% i; \\ _ = i * i; \\ _ = i -% i; \\ _ = i - i; \\ _ = i +% i; \\ _ = i + i; \\ _ = i << i; \\ _ = i >> i; \\ _ = i & i; \\ _ = i ^ i; \\ _ = i | i; \\ _ = i >= i; \\ _ = i <= i; \\ _ = i > i; \\ _ = i < i; \\ _ = i and i; \\ _ = i or i; \\} \\ ); } test "zig fmt: precedence" { try testCanonical( \\test "precedence" { \\ a!b(); \\ (a!b)(); \\ !a!b; \\ !(a!b); \\ !a{}; \\ !(a{}); \\ a + b{}; \\ (a + b){}; \\ a << b + c; \\ (a << b) + c; \\ a & b << c; \\ (a & b) << c; \\ a ^ b & c; \\ (a ^ b) & c; \\ a | b ^ c; \\ (a | b) ^ c; \\ a == b | c; \\ (a == b) | c; \\ a and b == c; \\ (a and b) == c; \\ a or b and c; \\ (a or b) and c; \\ (a or b) and c; \\} \\ ); } test "zig fmt: prefix operators" { try testCanonical( \\test "prefix operators" { \\ try return --%~!&0; \\} \\ ); } test "zig fmt: call expression" { try testCanonical( \\test "test calls" { \\ a(); \\ a(1); \\ a(1, 2); \\ a(1, 2) + a(1, 2); \\} \\ ); } test "zig fmt: var type" { try testCanonical( \\fn print(args: var) var {} \\ ); } test "zig fmt: functions" { try testCanonical( \\extern fn puts(s: *const u8) c_int; \\extern "c" fn puts(s: *const u8) c_int; \\export fn puts(s: *const u8) c_int; \\inline fn puts(s: *const u8) c_int; \\noinline fn puts(s: *const u8) c_int; \\pub extern fn puts(s: *const u8) c_int; \\pub extern "c" fn puts(s: *const u8) c_int; \\pub export fn puts(s: *const u8) c_int; \\pub inline fn puts(s: *const u8) c_int; \\pub noinline fn puts(s: *const u8) c_int; \\pub extern fn puts(s: *const u8) align(2 + 2) c_int; \\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int; \\pub export fn puts(s: *const u8) align(2 + 2) c_int; \\pub inline fn puts(s: *const u8) align(2 + 2) c_int; \\pub noinline fn puts(s: *const u8) align(2 + 2) c_int; \\ ); } test "zig fmt: multiline string" { try testCanonical( \\test "" { \\ const s1 = \\ \\one \\ \\two) \\ \\three \\ ; \\ const s3 = // hi \\ \\one \\ \\two) \\ \\three \\ ; \\} \\ ); } test "zig fmt: values" { try testCanonical( \\test "values" { \\ 1; \\ 1.0; \\ "string"; \\ 'c'; \\ true; \\ false; \\ null; \\ undefined; \\ anyerror; \\ this; \\ unreachable; \\} \\ ); } test "zig fmt: indexing" { try testCanonical( \\test "test index" { \\ a[0]; \\ a[0 + 5]; \\ a[0..]; \\ a[0..5]; \\ a[a[0]]; \\ a[a[0..]]; \\ a[a[0..5]]; \\ a[a[0]..]; \\ a[a[0..5]..]; \\ a[a[0]..a[0]]; \\ a[a[0..5]..a[0]]; \\ a[a[0..5]..a[0..5]]; \\} \\ ); } test "zig fmt: struct declaration" { try testCanonical( \\const S = struct { \\ const Self = @This(); \\ f1: u8, \\ f3: u8, \\ \\ fn method(self: *Self) Self { \\ return self.*; \\ } \\ \\ f2: u8, \\}; \\ \\const Ps = packed struct { \\ a: u8, \\ b: u8, \\ \\ c: u8, \\}; \\ \\const Es = extern struct { \\ a: u8, \\ b: u8, \\ \\ c: u8, \\}; \\ ); } test "zig fmt: enum declaration" { try testCanonical( \\const E = enum { \\ Ok, \\ SomethingElse = 0, \\}; \\ \\const E2 = enum(u8) { \\ Ok, \\ SomethingElse = 255, \\ SomethingThird, \\}; \\ \\const Ee = extern enum { \\ Ok, \\ SomethingElse, \\ SomethingThird, \\}; \\ \\const Ep = packed enum { \\ Ok, \\ SomethingElse, \\ SomethingThird, \\}; \\ ); } test "zig fmt: union declaration" { try testCanonical( \\const U = union { \\ Int: u8, \\ Float: f32, \\ None, \\ Bool: bool, \\}; \\ \\const Ue = union(enum) { \\ Int: u8, \\ Float: f32, \\ None, \\ Bool: bool, \\}; \\ \\const E = enum { \\ Int, \\ Float, \\ None, \\ Bool, \\}; \\ \\const Ue2 = union(E) { \\ Int: u8, \\ Float: f32, \\ None, \\ Bool: bool, \\}; \\ \\const Eu = extern union { \\ Int: u8, \\ Float: f32, \\ None, \\ Bool: bool, \\}; \\ ); } test "zig fmt: arrays" { try testCanonical( \\test "test array" { \\ const a: [2]u8 = [2]u8{ \\ 1, \\ 2, \\ }; \\ const a: [2]u8 = []u8{ \\ 1, \\ 2, \\ }; \\ const a: [0]u8 = []u8{}; \\ const x: [4:0]u8 = undefined; \\} \\ ); } test "zig fmt: container initializers" { try testCanonical( \\const a0 = []u8{}; \\const a1 = []u8{1}; \\const a2 = []u8{ \\ 1, \\ 2, \\ 3, \\ 4, \\}; \\const s0 = S{}; \\const s1 = S{ .a = 1 }; \\const s2 = S{ \\ .a = 1, \\ .b = 2, \\}; \\ ); } test "zig fmt: catch" { try testCanonical( \\test "catch" { \\ const a: anyerror!u8 = 0; \\ _ = a catch return; \\ _ = a catch |err| return; \\} \\ ); } test "zig fmt: blocks" { try testCanonical( \\test "blocks" { \\ { \\ const a = 0; \\ const b = 0; \\ } \\ \\ blk: { \\ const a = 0; \\ const b = 0; \\ } \\ \\ const r = blk: { \\ const a = 0; \\ const b = 0; \\ }; \\} \\ ); } test "zig fmt: switch" { try testCanonical( \\test "switch" { \\ switch (0) { \\ 0 => {}, \\ 1 => unreachable, \\ 2, 3 => {}, \\ 4...7 => {}, \\ 1 + 4 * 3 + 22 => {}, \\ else => { \\ const a = 1; \\ const b = a; \\ }, \\ } \\ \\ const res = switch (0) { \\ 0 => 0, \\ 1 => 2, \\ 1 => a = 4, \\ else => 4, \\ }; \\ \\ const Union = union(enum) { \\ Int: i64, \\ Float: f64, \\ }; \\ \\ switch (u) { \\ Union.Int => |int| {}, \\ Union.Float => |*float| unreachable, \\ } \\} \\ ); } test "zig fmt: while" { try testCanonical( \\test "while" { \\ while (10 < 1) unreachable; \\ \\ while (10 < 1) unreachable else unreachable; \\ \\ while (10 < 1) { \\ unreachable; \\ } \\ \\ while (10 < 1) \\ unreachable; \\ \\ var i: usize = 0; \\ while (i < 10) : (i += 1) { \\ continue; \\ } \\ \\ i = 0; \\ while (i < 10) : (i += 1) \\ continue; \\ \\ i = 0; \\ var j: usize = 0; \\ while (i < 10) : ({ \\ i += 1; \\ j += 1; \\ }) { \\ continue; \\ } \\ \\ var a: ?u8 = 2; \\ while (a) |v| : (a = null) { \\ continue; \\ } \\ \\ while (a) |v| : (a = null) \\ unreachable; \\ \\ label: while (10 < 0) { \\ unreachable; \\ } \\ \\ const res = while (0 < 10) { \\ break 7; \\ } else { \\ unreachable; \\ }; \\ \\ const res = while (0 < 10) \\ break 7 \\ else \\ unreachable; \\ \\ var a: anyerror!u8 = 0; \\ while (a) |v| { \\ a = error.Err; \\ } else |err| { \\ i = 1; \\ } \\ \\ comptime var k: usize = 0; \\ inline while (i < 10) : (i += 1) \\ j += 2; \\} \\ ); } test "zig fmt: for" { try testCanonical( \\test "for" { \\ for (a) |v| { \\ continue; \\ } \\ \\ for (a) |v| continue; \\ \\ for (a) |v| continue else return; \\ \\ for (a) |v| { \\ continue; \\ } else return; \\ \\ for (a) |v| continue else { \\ return; \\ } \\ \\ for (a) |v| \\ continue \\ else \\ return; \\ \\ for (a) |v| \\ continue; \\ \\ for (a) |*v| \\ continue; \\ \\ for (a) |v, i| { \\ continue; \\ } \\ \\ for (a) |v, i| \\ continue; \\ \\ for (a) |b| switch (b) { \\ c => {}, \\ d => {}, \\ }; \\ \\ for (a) |b| \\ switch (b) { \\ c => {}, \\ d => {}, \\ }; \\ \\ const res = for (a) |v, i| { \\ break v; \\ } else { \\ unreachable; \\ }; \\ \\ var num: usize = 0; \\ inline for (a) |v, i| { \\ num += v; \\ num += i; \\ } \\} \\ ); try testTransform( \\test "fix for" { \\ for (a) |x| \\ f(x) else continue; \\} \\ , \\test "fix for" { \\ for (a) |x| \\ f(x) \\ else continue; \\} \\ ); } test "zig fmt: if" { try testCanonical( \\test "if" { \\ if (10 < 0) { \\ unreachable; \\ } \\ \\ if (10 < 0) unreachable; \\ \\ if (10 < 0) { \\ unreachable; \\ } else { \\ const a = 20; \\ } \\ \\ if (10 < 0) { \\ unreachable; \\ } else if (5 < 0) { \\ unreachable; \\ } else { \\ const a = 20; \\ } \\ \\ const is_world_broken = if (10 < 0) true else false; \\ const some_number = 1 + if (10 < 0) 2 else 3; \\ \\ const a: ?u8 = 10; \\ const b: ?u8 = null; \\ if (a) |v| { \\ const some = v; \\ } else if (b) |*v| { \\ unreachable; \\ } else { \\ const some = 10; \\ } \\ \\ const non_null_a = if (a) |v| v else 0; \\ \\ const a_err: anyerror!u8 = 0; \\ if (a_err) |v| { \\ const p = v; \\ } else |err| { \\ unreachable; \\ } \\} \\ ); } test "zig fmt: defer" { try testCanonical( \\test "defer" { \\ var i: usize = 0; \\ defer i = 1; \\ defer { \\ i += 2; \\ i *= i; \\ } \\ \\ errdefer i += 3; \\ errdefer { \\ i += 2; \\ i /= i; \\ } \\} \\ ); } test "zig fmt: comptime" { try testCanonical( \\fn a() u8 { \\ return 5; \\} \\ \\fn b(comptime i: u8) u8 { \\ return i; \\} \\ \\const av = comptime a(); \\const av2 = comptime blk: { \\ var res = a(); \\ res *= b(2); \\ break :blk res; \\}; \\ \\comptime { \\ _ = a(); \\} \\ \\test "comptime" { \\ const av3 = comptime a(); \\ const av4 = comptime blk: { \\ var res = a(); \\ res *= a(); \\ break :blk res; \\ }; \\ \\ comptime var i = 0; \\ comptime { \\ i = a(); \\ i += b(i); \\ } \\} \\ ); } test "zig fmt: fn type" { try testCanonical( \\fn a(i: u8) u8 { \\ return i + 1; \\} \\ \\const a: fn (u8) u8 = undefined; \\const b: extern fn (u8) u8 = undefined; \\const c: fn (u8) callconv(.Naked) u8 = undefined; \\const ap: fn (u8) u8 = a; \\ ); } test "zig fmt: inline asm" { try testCanonical( \\pub fn syscall1(number: usize, arg1: usize) usize { \\ return asm volatile ("syscall" \\ : [ret] "={rax}" (-> usize) \\ : [number] "{rax}" (number), \\ [arg1] "{rdi}" (arg1) \\ : "rcx", "r11" \\ ); \\} \\ ); } test "zig fmt: async functions" { try testCanonical( \\async fn simpleAsyncFn() void { \\ const a = async a.b(); \\ x += 1; \\ suspend; \\ x += 1; \\ suspend; \\ const p: anyframe->void = async simpleAsyncFn() catch unreachable; \\ await p; \\} \\ \\test "suspend, resume, await" { \\ const p: anyframe = async testAsyncSeq(); \\ resume p; \\ await p; \\} \\ ); } test "zig fmt: noasync" { try testCanonical( \\const a = noasync foo(); \\ ); } test "zig fmt: Block after if" { try testCanonical( \\test "Block after if" { \\ if (true) { \\ const a = 0; \\ } \\ \\ { \\ const a = 0; \\ } \\} \\ ); } test "zig fmt: use" { try testCanonical( \\usingnamespace @import("std"); \\pub usingnamespace @import("std"); \\ ); } test "zig fmt: string identifier" { try testCanonical( \\const @"a b" = @"c d".@"e f"; \\fn @"g h"() void {} \\ ); } test "zig fmt: error return" { try testCanonical( \\fn err() anyerror { \\ call(); \\ return error.InvalidArgs; \\} \\ ); } test "zig fmt: comptime block in container" { try testCanonical( \\pub fn container() type { \\ return struct { \\ comptime { \\ if (false) { \\ unreachable; \\ } \\ } \\ }; \\} \\ ); } test "zig fmt: inline asm parameter alignment" { try testCanonical( \\pub fn main() void { \\ asm volatile ( \\ \\ foo \\ \\ bar \\ ); \\ asm volatile ( \\ \\ foo \\ \\ bar \\ : [_] "" (-> usize), \\ [_] "" (-> usize) \\ ); \\ asm volatile ( \\ \\ foo \\ \\ bar \\ : \\ : [_] "" (0), \\ [_] "" (0) \\ ); \\ asm volatile ( \\ \\ foo \\ \\ bar \\ : \\ : \\ : "", "" \\ ); \\ asm volatile ( \\ \\ foo \\ \\ bar \\ : [_] "" (-> usize), \\ [_] "" (-> usize) \\ : [_] "" (0), \\ [_] "" (0) \\ : "", "" \\ ); \\} \\ ); } test "zig fmt: multiline string in array" { try testCanonical( \\const Foo = [][]const u8{ \\ \\aaa \\, \\ \\bbb \\}; \\ \\fn bar() void { \\ const Foo = [][]const u8{ \\ \\aaa \\ , \\ \\bbb \\ }; \\ const Bar = [][]const u8{ // comment here \\ \\aaa \\ \\ \\ , // and another comment can go here \\ \\bbb \\ }; \\} \\ ); } test "zig fmt: if type expr" { try testCanonical( \\const mycond = true; \\pub fn foo() if (mycond) i32 else void { \\ if (mycond) { \\ return 42; \\ } \\} \\ ); } test "zig fmt: file ends with struct field" { try testTransform( \\a: bool , \\a: bool, \\ ); } test "zig fmt: comment after empty comment" { try testTransform( \\const x = true; // \\// \\// \\//a \\ , \\const x = true; \\//a \\ ); } test "zig fmt: line comment in array" { try testTransform( \\test "a" { \\ var arr = [_]u32{ \\ 0 \\ // 1, \\ // 2, \\ }; \\} \\ , \\test "a" { \\ var arr = [_]u32{ \\ 0, // 1, \\ // 2, \\ }; \\} \\ ); try testCanonical( \\test "a" { \\ var arr = [_]u32{ \\ 0, \\ // 1, \\ // 2, \\ }; \\} \\ ); } test "zig fmt: comment after params" { try testTransform( \\fn a( \\ b: u32 \\ // c: u32, \\ // d: u32, \\) void {} \\ , \\fn a( \\ b: u32, // c: u32, \\ // d: u32, \\) void {} \\ ); try testCanonical( \\fn a( \\ b: u32, \\ // c: u32, \\ // d: u32, \\) void {} \\ ); } test "zig fmt: comment in array initializer/access" { try testCanonical( \\test "a" { \\ var a = x{ //aa \\ //bb \\ }; \\ var a = []x{ //aa \\ //bb \\ }; \\ var b = [ //aa \\ _ \\ ]x{ //aa \\ //bb \\ 9, \\ }; \\ var c = b[ //aa \\ 0 \\ ]; \\ var d = [_ \\ //aa \\ ]x{ //aa \\ //bb \\ 9, \\ }; \\ var e = d[0 \\ //aa \\ ]; \\} \\ ); } test "zig fmt: comments at several places in struct init" { try testTransform( \\var bar = Bar{ \\ .x = 10, // test \\ .y = "test" \\ // test \\}; \\ , \\var bar = Bar{ \\ .x = 10, // test \\ .y = "test", // test \\}; \\ ); try testCanonical( \\var bar = Bar{ // test \\ .x = 10, // test \\ .y = "test", \\ // test \\}; \\ ); } test "zig fmt: top level doc comments" { try testCanonical( \\//! tld 1 \\//! tld 2 \\//! tld 3 \\ \\// comment \\ \\/// A doc \\const A = struct { \\ //! A tld 1 \\ //! A tld 2 \\ //! A tld 3 \\}; \\ \\/// B doc \\const B = struct { \\ //! B tld 1 \\ //! B tld 2 \\ //! B tld 3 \\ \\ /// b doc \\ b: u32, \\}; \\ \\/// C doc \\const C = struct { \\ //! C tld 1 \\ //! C tld 2 \\ //! C tld 3 \\ \\ /// c1 doc \\ c1: u32, \\ \\ //! C tld 4 \\ //! C tld 5 \\ //! C tld 6 \\ \\ /// c2 doc \\ c2: u32, \\}; \\ ); try testCanonical( \\//! Top-level documentation. \\ \\/// This is A \\pub const A = usize; \\ ); try testCanonical( \\//! Nothing here \\ ); } test "zig fmt: extern without container keyword returns error" { try testError( \\const container = extern {}; \\ ); } const std = @import("std"); const mem = std.mem; const warn = std.debug.warn; const io = std.io; const maxInt = std.math.maxInt; var fixed_buffer_mem: [100 * 1024]u8 = undefined; fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *bool) ![]u8 { const stderr = &io.getStdErr().outStream().stream; const tree = try std.zig.parse(allocator, source); defer tree.deinit(); var error_it = tree.errors.iterator(0); while (error_it.next()) |parse_error| { const token = tree.tokens.at(parse_error.loc()); const loc = tree.tokenLocation(0, parse_error.loc()); try stderr.print("(memory buffer):{}:{}: error: ", .{ loc.line + 1, loc.column + 1 }); try tree.renderError(parse_error, stderr); try stderr.print("\n{}\n", .{source[loc.line_start..loc.line_end]}); { var i: usize = 0; while (i < loc.column) : (i += 1) { try stderr.write(" "); } } { const caret_count = token.end - token.start; var i: usize = 0; while (i < caret_count) : (i += 1) { try stderr.write("~"); } } try stderr.write("\n"); } if (tree.errors.len != 0) { return error.ParseError; } var buffer = try std.Buffer.initSize(allocator, 0); errdefer buffer.deinit(); var buffer_out_stream = io.BufferOutStream.init(&buffer); anything_changed.* = try std.zig.render(allocator, &buffer_out_stream.stream, tree); return buffer.toOwnedSlice(); } fn testTransform(source: []const u8, expected_source: []const u8) !void { const needed_alloc_count = x: { // Try it once with unlimited memory, make sure it works var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize)); var anything_changed: bool = undefined; const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed); if (!mem.eql(u8, result_source, expected_source)) { warn("\n====== expected this output: =========\n", .{}); warn("{}", .{expected_source}); warn("\n======== instead found this: =========\n", .{}); warn("{}", .{result_source}); warn("\n======================================\n", .{}); return error.TestFailed; } const changes_expected = source.ptr != expected_source.ptr; if (anything_changed != changes_expected) { warn("std.zig.render returned {} instead of {}\n", .{ anything_changed, changes_expected }); return error.TestFailed; } std.testing.expect(anything_changed == changes_expected); failing_allocator.allocator.free(result_source); break :x failing_allocator.index; }; var fail_index: usize = 0; while (fail_index < needed_alloc_count) : (fail_index += 1) { var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index); var anything_changed: bool = undefined; if (testParse(source, &failing_allocator.allocator, &anything_changed)) |_| { return error.NondeterministicMemoryUsage; } else |err| switch (err) { error.OutOfMemory => { if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) { warn( "\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n", .{ fail_index, needed_alloc_count, failing_allocator.allocated_bytes, failing_allocator.freed_bytes, failing_allocator.allocations, failing_allocator.deallocations, }, ); return error.MemoryLeakDetected; } }, error.ParseError => @panic("test failed"), else => @panic("test failed"), } } } fn testCanonical(source: []const u8) !void { return testTransform(source, source); } fn testError(source: []const u8) !void { var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); const tree = try std.zig.parse(&fixed_allocator.allocator, source); defer tree.deinit(); std.testing.expect(tree.errors.len != 0); }
//! A condition provides a way for a kernel thread to block until it is signaled //! to wake up. Spurious wakeups are possible. //! This API supports static initialization and does not require deinitialization. impl: Impl = .{}, const std = @import("../std.zig"); const Condition = @This(); const windows = std.os.windows; const linux = std.os.linux; const Mutex = std.Thread.Mutex; const assert = std.debug.assert; pub fn wait(cond: *Condition, mutex: *Mutex) void { cond.impl.wait(mutex); } pub fn signal(cond: *Condition) void { cond.impl.signal(); } pub fn broadcast(cond: *Condition) void { cond.impl.broadcast(); } const Impl = if (std.builtin.single_threaded) SingleThreadedCondition else if (std.Target.current.os.tag == .windows) WindowsCondition else if (std.Thread.use_pthreads) PthreadCondition else AtomicCondition; pub const SingleThreadedCondition = struct { pub fn wait(cond: *SingleThreadedCondition, mutex: *Mutex) void { _ = cond; _ = mutex; unreachable; // deadlock detected } pub fn signal(cond: *SingleThreadedCondition) void { _ = cond; } pub fn broadcast(cond: *SingleThreadedCondition) void { _ = cond; } }; pub const WindowsCondition = struct { cond: windows.CONDITION_VARIABLE = windows.CONDITION_VARIABLE_INIT, pub fn wait(cond: *WindowsCondition, mutex: *Mutex) void { const rc = windows.kernel32.SleepConditionVariableSRW( &cond.cond, &mutex.impl.srwlock, windows.INFINITE, @as(windows.ULONG, 0), ); assert(rc != windows.FALSE); } pub fn signal(cond: *WindowsCondition) void { windows.kernel32.WakeConditionVariable(&cond.cond); } pub fn broadcast(cond: *WindowsCondition) void { windows.kernel32.WakeAllConditionVariable(&cond.cond); } }; pub const PthreadCondition = struct { cond: std.c.pthread_cond_t = .{}, pub fn wait(cond: *PthreadCondition, mutex: *Mutex) void { const rc = std.c.pthread_cond_wait(&cond.cond, &mutex.impl.pthread_mutex); assert(rc == .SUCCESS); } pub fn signal(cond: *PthreadCondition) void { const rc = std.c.pthread_cond_signal(&cond.cond); assert(rc == .SUCCESS); } pub fn broadcast(cond: *PthreadCondition) void { const rc = std.c.pthread_cond_broadcast(&cond.cond); assert(rc == .SUCCESS); } }; pub const AtomicCondition = struct { pending: bool = false, queue_mutex: Mutex = .{}, queue_list: QueueList = .{}, pub const QueueList = std.SinglyLinkedList(QueueItem); pub const QueueItem = struct { futex: i32 = 0, fn wait(cond: *@This()) void { while (@atomicLoad(i32, &cond.futex, .Acquire) == 0) { switch (std.Target.current.os.tag) { .linux => { switch (linux.getErrno(linux.futex_wait( &cond.futex, linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAIT, 0, null, ))) { .SUCCESS => {}, .INTR => {}, .AGAIN => {}, else => unreachable, } }, else => std.atomic.spinLoopHint(), } } } fn notify(cond: *@This()) void { @atomicStore(i32, &cond.futex, 1, .Release); switch (std.Target.current.os.tag) { .linux => { switch (linux.getErrno(linux.futex_wake( &cond.futex, linux.FUTEX_PRIVATE_FLAG | linux.FUTEX_WAKE, 1, ))) { .SUCCESS => {}, .FAULT => {}, else => unreachable, } }, else => {}, } } }; pub fn wait(cond: *AtomicCondition, mutex: *Mutex) void { var waiter = QueueList.Node{ .data = .{} }; { const held = cond.queue_mutex.acquire(); defer held.release(); cond.queue_list.prepend(&waiter); @atomicStore(bool, &cond.pending, true, .SeqCst); } mutex.unlock(); waiter.data.wait(); mutex.lock(); } pub fn signal(cond: *AtomicCondition) void { if (@atomicLoad(bool, &cond.pending, .SeqCst) == false) return; const maybe_waiter = blk: { const held = cond.queue_mutex.acquire(); defer held.release(); const maybe_waiter = cond.queue_list.popFirst(); @atomicStore(bool, &cond.pending, cond.queue_list.first != null, .SeqCst); break :blk maybe_waiter; }; if (maybe_waiter) |waiter| waiter.data.notify(); } pub fn broadcast(cond: *AtomicCondition) void { if (@atomicLoad(bool, &cond.pending, .SeqCst) == false) return; @atomicStore(bool, &cond.pending, false, .SeqCst); var waiters = blk: { const held = cond.queue_mutex.acquire(); defer held.release(); const waiters = cond.queue_list; cond.queue_list = .{}; break :blk waiters; }; while (waiters.popFirst()) |waiter| waiter.data.notify(); } };
const std = @import("std"); const mem = std.mem; const testing = std.testing; const Allocator = mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const ArrayList = std.ArrayList; pub const Attribute = struct { name: []const u8, value: []const u8, }; pub const Content = union(enum) { CharData: []const u8, Comment: []const u8, Element: *Element, }; pub const Element = struct { pub const AttributeList = ArrayList(*Attribute); pub const ContentList = ArrayList(Content); tag: []const u8, attributes: AttributeList, children: ContentList, fn init(tag: []const u8, alloc: Allocator) Element { return .{ .tag = tag, .attributes = AttributeList.init(alloc), .children = ContentList.init(alloc), }; } pub fn getAttribute(self: *Element, attrib_name: []const u8) ?[]const u8 { for (self.attributes.items) |child| { if (mem.eql(u8, child.name, attrib_name)) { return child.value; } } return null; } pub fn getCharData(self: *Element, child_tag: []const u8) ?[]const u8 { const child = self.findChildByTag(child_tag) orelse return null; if (child.children.items.len != 1) { return null; } return switch (child.children.items[0]) { .CharData => |char_data| char_data, else => null, }; } pub fn iterator(self: *Element) ChildIterator { return .{ .items = self.children.items, .i = 0, }; } pub fn elements(self: *Element) ChildElementIterator { return .{ .inner = self.iterator(), }; } pub fn findChildByTag(self: *Element, tag: []const u8) ?*Element { return self.findChildrenByTag(tag).next(); } pub fn findChildrenByTag(self: *Element, tag: []const u8) FindChildrenByTagIterator { return .{ .inner = self.elements(), .tag = tag, }; } pub const ChildIterator = struct { items: []Content, i: usize, pub fn next(self: *ChildIterator) ?*Content { if (self.i < self.items.len) { self.i += 1; return &self.items[self.i - 1]; } return null; } }; pub const ChildElementIterator = struct { inner: ChildIterator, pub fn next(self: *ChildElementIterator) ?*Element { while (self.inner.next()) |child| { if (child.* != .Element) { continue; } return child.*.Element; } return null; } }; pub const FindChildrenByTagIterator = struct { inner: ChildElementIterator, tag: []const u8, pub fn next(self: *FindChildrenByTagIterator) ?*Element { while (self.inner.next()) |child| { if (!mem.eql(u8, child.tag, self.tag)) { continue; } return child; } return null; } }; }; pub const XmlDecl = struct { version: []const u8, encoding: ?[]const u8, standalone: ?bool, }; pub const Document = struct { arena: ArenaAllocator, xml_decl: ?*XmlDecl, root: *Element, pub fn deinit(self: Document) void { var arena = self.arena; // Copy to stack so self can be taken by value. arena.deinit(); } }; const ParseContext = struct { source: []const u8, offset: usize, line: usize, column: usize, fn init(source: []const u8) ParseContext { return .{ .source = source, .offset = 0, .line = 0, .column = 0, }; } fn peek(self: *ParseContext) ?u8 { return if (self.offset < self.source.len) self.source[self.offset] else null; } fn consume(self: *ParseContext) !u8 { if (self.offset < self.source.len) { return self.consumeNoEof(); } return error.UnexpectedEof; } fn consumeNoEof(self: *ParseContext) u8 { std.debug.assert(self.offset < self.source.len); const c = self.source[self.offset]; self.offset += 1; if (c == '\n') { self.line += 1; self.column = 0; } else { self.column += 1; } return c; } fn eat(self: *ParseContext, char: u8) bool { self.expect(char) catch return false; return true; } fn expect(self: *ParseContext, expected: u8) !void { if (self.peek()) |actual| { if (expected != actual) { return error.UnexpectedCharacter; } _ = self.consumeNoEof(); return; } return error.UnexpectedEof; } fn eatStr(self: *ParseContext, text: []const u8) bool { self.expectStr(text) catch return false; return true; } fn expectStr(self: *ParseContext, text: []const u8) !void { if (self.source.len < self.offset + text.len) { return error.UnexpectedEof; } else if (std.mem.startsWith(u8, self.source[self.offset..], text)) { var i: usize = 0; while (i < text.len) : (i += 1) { _ = self.consumeNoEof(); } return; } return error.UnexpectedCharacter; } fn eatWs(self: *ParseContext) bool { var ws = false; while (self.peek()) |ch| { switch (ch) { ' ', '\t', '\n', '\r' => { ws = true; _ = self.consumeNoEof(); }, else => break, } } return ws; } fn expectWs(self: *ParseContext) !void { if (!self.eatWs()) return error.UnexpectedCharacter; } fn currentLine(self: ParseContext) []const u8 { var begin: usize = 0; if (mem.lastIndexOfScalar(u8, self.source[0..self.offset], '\n')) |prev_nl| { begin = prev_nl + 1; } var end = mem.indexOfScalarPos(u8, self.source, self.offset, '\n') orelse self.source.len; return self.source[begin..end]; } }; test "ParseContext" { { var ctx = ParseContext.init("I like pythons"); try testing.expectEqual(@as(?u8, 'I'), ctx.peek()); try testing.expectEqual(@as(u8, 'I'), ctx.consumeNoEof()); try testing.expectEqual(@as(?u8, ' '), ctx.peek()); try testing.expectEqual(@as(u8, ' '), try ctx.consume()); try testing.expect(ctx.eat('l')); try testing.expectEqual(@as(?u8, 'i'), ctx.peek()); try testing.expectEqual(false, ctx.eat('a')); try testing.expectEqual(@as(?u8, 'i'), ctx.peek()); try ctx.expect('i'); try testing.expectEqual(@as(?u8, 'k'), ctx.peek()); try testing.expectError(error.UnexpectedCharacter, ctx.expect('a')); try testing.expectEqual(@as(?u8, 'k'), ctx.peek()); try testing.expect(ctx.eatStr("ke")); try testing.expectEqual(@as(?u8, ' '), ctx.peek()); try testing.expect(ctx.eatWs()); try testing.expectEqual(@as(?u8, 'p'), ctx.peek()); try testing.expectEqual(false, ctx.eatWs()); try testing.expectEqual(@as(?u8, 'p'), ctx.peek()); try testing.expectEqual(false, ctx.eatStr("aaaaaaaaa")); try testing.expectEqual(@as(?u8, 'p'), ctx.peek()); try testing.expectError(error.UnexpectedEof, ctx.expectStr("aaaaaaaaa")); try testing.expectEqual(@as(?u8, 'p'), ctx.peek()); try testing.expectError(error.UnexpectedCharacter, ctx.expectStr("pytn")); try testing.expectEqual(@as(?u8, 'p'), ctx.peek()); try ctx.expectStr("python"); try testing.expectEqual(@as(?u8, 's'), ctx.peek()); } { var ctx = ParseContext.init(""); try testing.expectEqual(ctx.peek(), null); try testing.expectError(error.UnexpectedEof, ctx.consume()); try testing.expectEqual(ctx.eat('p'), false); try testing.expectError(error.UnexpectedEof, ctx.expect('p')); } } pub const ParseError = error{ IllegalCharacter, UnexpectedEof, UnexpectedCharacter, UnclosedValue, UnclosedComment, InvalidName, InvalidEntity, InvalidStandaloneValue, NonMatchingClosingTag, InvalidDocument, OutOfMemory, }; pub fn parse(backing_allocator: Allocator, source: []const u8) !Document { var ctx = ParseContext.init(source); return try parseDocument(&ctx, backing_allocator); } fn parseDocument(ctx: *ParseContext, backing_allocator: Allocator) !Document { var doc = Document{ .arena = ArenaAllocator.init(backing_allocator), .xml_decl = null, .root = undefined, }; errdefer doc.deinit(); const allocator = doc.arena.allocator(); try trySkipComments(ctx, allocator); doc.xml_decl = try tryParseProlog(ctx, allocator); _ = ctx.eatWs(); try trySkipComments(ctx, allocator); doc.root = (try tryParseElement(ctx, allocator)) orelse return error.InvalidDocument; _ = ctx.eatWs(); try trySkipComments(ctx, allocator); if (ctx.peek() != null) return error.InvalidDocument; return doc; } fn parseAttrValue(ctx: *ParseContext, alloc: Allocator) ![]const u8 { const quote = try ctx.consume(); if (quote != '"' and quote != '\'') return error.UnexpectedCharacter; const begin = ctx.offset; while (true) { const c = ctx.consume() catch return error.UnclosedValue; if (c == quote) break; } const end = ctx.offset - 1; return try dupeAndUnescape(alloc, ctx.source[begin..end]); } fn parseEqAttrValue(ctx: *ParseContext, alloc: Allocator) ![]const u8 { _ = ctx.eatWs(); try ctx.expect('='); _ = ctx.eatWs(); return try parseAttrValue(ctx, alloc); } fn parseNameNoDupe(ctx: *ParseContext) ![]const u8 { // XML's spec on names is very long, so to make this easier // we just take any character that is not special and not whitespace const begin = ctx.offset; while (ctx.peek()) |ch| { switch (ch) { ' ', '\t', '\n', '\r' => break, '&', '"', '\'', '<', '>', '?', '=', '/' => break, else => _ = ctx.consumeNoEof(), } } const end = ctx.offset; if (begin == end) return error.InvalidName; return ctx.source[begin..end]; } fn tryParseCharData(ctx: *ParseContext, alloc: Allocator) !?[]const u8 { const begin = ctx.offset; while (ctx.peek()) |ch| { switch (ch) { '<' => break, else => _ = ctx.consumeNoEof(), } } const end = ctx.offset; if (begin == end) return null; return try dupeAndUnescape(alloc, ctx.source[begin..end]); } fn parseContent(ctx: *ParseContext, alloc: Allocator) ParseError!Content { if (try tryParseCharData(ctx, alloc)) |cd| { return Content{ .CharData = cd }; } else if (try tryParseComment(ctx, alloc)) |comment| { return Content{ .Comment = comment }; } else if (try tryParseElement(ctx, alloc)) |elem| { return Content{ .Element = elem }; } else { return error.UnexpectedCharacter; } } fn tryParseAttr(ctx: *ParseContext, alloc: Allocator) !?*Attribute { const name = parseNameNoDupe(ctx) catch return null; _ = ctx.eatWs(); try ctx.expect('='); _ = ctx.eatWs(); const value = try parseAttrValue(ctx, alloc); const attr = try alloc.create(Attribute); attr.name = try alloc.dupe(u8, name); attr.value = value; return attr; } fn tryParseElement(ctx: *ParseContext, alloc: Allocator) !?*Element { const start = ctx.offset; if (!ctx.eat('<')) return null; const tag = parseNameNoDupe(ctx) catch { ctx.offset = start; return null; }; const element = try alloc.create(Element); element.* = Element.init(try alloc.dupe(u8, tag), alloc); while (ctx.eatWs()) { const attr = (try tryParseAttr(ctx, alloc)) orelse break; try element.attributes.append(attr); } if (ctx.eatStr("/>")) { return element; } try ctx.expect('>'); while (true) { if (ctx.peek() == null) { return error.UnexpectedEof; } else if (ctx.eatStr("</")) { break; } const content = try parseContent(ctx, alloc); try element.children.append(content); } const closing_tag = try parseNameNoDupe(ctx); if (!std.mem.eql(u8, tag, closing_tag)) { return error.NonMatchingClosingTag; } _ = ctx.eatWs(); try ctx.expect('>'); return element; } test "tryParseElement" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); { var ctx = ParseContext.init("<= a='b'/>"); try testing.expectEqual(@as(?*Element, null), try tryParseElement(&ctx, alloc)); try testing.expectEqual(@as(?u8, '<'), ctx.peek()); } { var ctx = ParseContext.init("<python size='15' color = \"green\"/>"); const elem = try tryParseElement(&ctx, alloc); try testing.expectEqualSlices(u8, elem.?.tag, "python"); const size_attr = elem.?.attributes.items[0]; try testing.expectEqualSlices(u8, size_attr.name, "size"); try testing.expectEqualSlices(u8, size_attr.value, "15"); const color_attr = elem.?.attributes.items[1]; try testing.expectEqualSlices(u8, color_attr.name, "color"); try testing.expectEqualSlices(u8, color_attr.value, "green"); } { var ctx = ParseContext.init("<python>test</python>"); const elem = try tryParseElement(&ctx, alloc); try testing.expectEqualSlices(u8, elem.?.tag, "python"); try testing.expectEqualSlices(u8, elem.?.children.items[0].CharData, "test"); } { var ctx = ParseContext.init("<a>b<c/>d<e/>f<!--g--></a>"); const elem = try tryParseElement(&ctx, alloc); try testing.expectEqualSlices(u8, elem.?.tag, "a"); try testing.expectEqualSlices(u8, elem.?.children.items[0].CharData, "b"); try testing.expectEqualSlices(u8, elem.?.children.items[1].Element.tag, "c"); try testing.expectEqualSlices(u8, elem.?.children.items[2].CharData, "d"); try testing.expectEqualSlices(u8, elem.?.children.items[3].Element.tag, "e"); try testing.expectEqualSlices(u8, elem.?.children.items[4].CharData, "f"); try testing.expectEqualSlices(u8, elem.?.children.items[5].Comment, "g"); } } fn tryParseProlog(ctx: *ParseContext, alloc: Allocator) !?*XmlDecl { const start = ctx.offset; if (!ctx.eatStr("<?") or !mem.eql(u8, try parseNameNoDupe(ctx), "xml")) { ctx.offset = start; return null; } const decl = try alloc.create(XmlDecl); decl.encoding = null; decl.standalone = null; // Version info is mandatory try ctx.expectWs(); try ctx.expectStr("version"); decl.version = try parseEqAttrValue(ctx, alloc); if (ctx.eatWs()) { // Optional encoding and standalone info var require_ws = false; if (ctx.eatStr("encoding")) { decl.encoding = try parseEqAttrValue(ctx, alloc); require_ws = true; } if (require_ws == ctx.eatWs() and ctx.eatStr("standalone")) { const standalone = try parseEqAttrValue(ctx, alloc); if (std.mem.eql(u8, standalone, "yes")) { decl.standalone = true; } else if (std.mem.eql(u8, standalone, "no")) { decl.standalone = false; } else { return error.InvalidStandaloneValue; } } _ = ctx.eatWs(); } try ctx.expectStr("?>"); return decl; } test "tryParseProlog" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); { var ctx = ParseContext.init("<?xmla version='aa'?>"); try testing.expectEqual(@as(?*XmlDecl, null), try tryParseProlog(&ctx, alloc)); try testing.expectEqual(@as(?u8, '<'), ctx.peek()); } { var ctx = ParseContext.init("<?xml version='aa'?>"); const decl = try tryParseProlog(&ctx, alloc); try testing.expectEqualSlices(u8, "aa", decl.?.version); try testing.expectEqual(@as(?[]const u8, null), decl.?.encoding); try testing.expectEqual(@as(?bool, null), decl.?.standalone); } { var ctx = ParseContext.init("<?xml version=\"aa\" encoding = 'bbb' standalone \t = 'yes'?>"); const decl = try tryParseProlog(&ctx, alloc); try testing.expectEqualSlices(u8, "aa", decl.?.version); try testing.expectEqualSlices(u8, "bbb", decl.?.encoding.?); try testing.expectEqual(@as(?bool, true), decl.?.standalone.?); } } fn trySkipComments(ctx: *ParseContext, alloc: Allocator) !void { while (try tryParseComment(ctx, alloc)) |_| { _ = ctx.eatWs(); } } fn tryParseComment(ctx: *ParseContext, alloc: Allocator) !?[]const u8 { if (!ctx.eatStr("<!--")) return null; const begin = ctx.offset; while (!ctx.eatStr("-->")) { _ = ctx.consume() catch return error.UnclosedComment; } const end = ctx.offset - "-->".len; return try alloc.dupe(u8, ctx.source[begin..end]); } fn unescapeEntity(text: []const u8) !u8 { const EntitySubstition = struct { text: []const u8, replacement: u8 }; const entities = [_]EntitySubstition{ .{ .text = "&lt;", .replacement = '<' }, .{ .text = "&gt;", .replacement = '>' }, .{ .text = "&amp;", .replacement = '&' }, .{ .text = "&apos;", .replacement = '\'' }, .{ .text = "&quot;", .replacement = '"' }, }; for (entities) |entity| { if (std.mem.eql(u8, text, entity.text)) return entity.replacement; } return error.InvalidEntity; } fn dupeAndUnescape(alloc: Allocator, text: []const u8) ![]const u8 { const str = try alloc.alloc(u8, text.len); var j: usize = 0; var i: usize = 0; while (i < text.len) : (j += 1) { if (text[i] == '&') { const entity_end = 1 + (mem.indexOfScalarPos(u8, text, i, ';') orelse return error.InvalidEntity); str[j] = try unescapeEntity(text[i..entity_end]); i = entity_end; } else { str[j] = text[i]; i += 1; } } return alloc.shrink(str, j); } test "dupeAndUnescape" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); try testing.expectEqualSlices(u8, "test", try dupeAndUnescape(alloc, "test")); try testing.expectEqualSlices(u8, "a<b&c>d\"e'f<", try dupeAndUnescape(alloc, "a&lt;b&amp;c&gt;d&quot;e&apos;f&lt;")); try testing.expectError(error.InvalidEntity, dupeAndUnescape(alloc, "python&")); try testing.expectError(error.InvalidEntity, dupeAndUnescape(alloc, "python&&")); try testing.expectError(error.InvalidEntity, dupeAndUnescape(alloc, "python&test;")); try testing.expectError(error.InvalidEntity, dupeAndUnescape(alloc, "python&boa")); } test "Top level comments" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); const doc = try parse(alloc, "<?xml version='aa'?><!--comment--><python color='green'/><!--another comment-->"); try testing.expectEqualSlices(u8, "python", doc.root.tag); }
// This file contains thin wrappers around OS-specific APIs, with these // specific goals in mind: // * Convert "errno"-style error codes into Zig errors. // * When null-terminated byte buffers are required, provide APIs which accept // slices as well as APIs which accept null-terminated byte buffers. Same goes // for UTF-16LE encoding. // * Where operating systems share APIs, e.g. POSIX, these thin wrappers provide // cross platform abstracting. // * When there exists a corresponding libc function and linking libc, the libc // implementation is used. Exceptions are made for known buggy areas of libc. // On Linux libc can be side-stepped by using `std.os.linux` directly. // * For Windows, this file represents the API that libc would provide for // Windows. For thin wrappers around Windows-specific APIs, see `std.os.windows`. // Note: The Zig standard library does not support POSIX thread cancellation, and // in general EINTR is handled by trying again. const std = @import("std.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; const math = std.math; const mem = std.mem; const elf = std.elf; const dl = @import("dynamic_library.zig"); const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES; comptime { assert(@import("std") == std); // std lib tests require --override-lib-dir } pub const darwin = @import("os/darwin.zig"); pub const freebsd = @import("os/freebsd.zig"); pub const linux = @import("os/linux.zig"); pub const netbsd = @import("os/netbsd.zig"); pub const uefi = @import("os/uefi.zig"); pub const wasi = @import("os/wasi.zig"); pub const windows = @import("os/windows.zig"); pub const zen = @import("os/zen.zig"); /// When linking libc, this is the C API. Otherwise, it is the OS-specific system interface. pub const system = if (builtin.link_libc) std.c else switch (builtin.os) { .macosx, .ios, .watchos, .tvos => darwin, .freebsd => freebsd, .linux => linux, .netbsd => netbsd, .wasi => wasi, .windows => windows, .zen => zen, else => struct {}, }; pub usingnamespace @import("os/bits.zig"); /// See also `getenv`. Populated by startup code before main(). pub var environ: [][*]u8 = undefined; /// Populated by startup code before main(). /// Not available on Windows. See `std.process.args` /// for obtaining the process arguments. pub var argv: [][*]u8 = undefined; /// To obtain errno, call this function with the return value of the /// system function call. For some systems this will obtain the value directly /// from the return code; for others it will use a thread-local errno variable. /// Therefore, this function only returns a well-defined value when it is called /// directly after the system function call which one wants to learn the errno /// value of. pub const errno = system.getErrno; /// Closes the file descriptor. /// This function is not capable of returning any indication of failure. An /// application which wants to ensure writes have succeeded before closing /// must call `fsync` before `close`. /// Note: The Zig standard library does not support POSIX thread cancellation. pub fn close(fd: fd_t) void { if (windows.is_the_target) { return windows.CloseHandle(fd); } if (wasi.is_the_target) { _ = wasi.fd_close(fd); } if (darwin.is_the_target) { // This avoids the EINTR problem. switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) { EBADF => unreachable, // Always a race condition. else => return, } } switch (errno(system.close(fd))) { EBADF => unreachable, // Always a race condition. EINTR => return, // This is still a success. See https://github.com/ziglang/zig/issues/2425 else => return, } } pub const GetRandomError = OpenError; /// Obtain a series of random bytes. These bytes can be used to seed user-space /// random number generators or for cryptographic purposes. /// When linking against libc, this calls the /// appropriate OS-specific library call. Otherwise it uses the zig standard /// library implementation. pub fn getrandom(buffer: []u8) GetRandomError!void { if (windows.is_the_target) { return windows.RtlGenRandom(buffer); } if (linux.is_the_target or freebsd.is_the_target) { var buf = buffer; const use_c = !linux.is_the_target or std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok; while (buf.len != 0) { var err: u16 = undefined; const num_read = if (use_c) blk: { const rc = std.c.getrandom(buf.ptr, buf.len, 0); err = std.c.getErrno(rc); break :blk @bitCast(usize, rc); } else blk: { const rc = linux.getrandom(buf.ptr, buf.len, 0); err = linux.getErrno(rc); break :blk rc; }; switch (err) { 0 => buf = buf[num_read..], EINVAL => unreachable, EFAULT => unreachable, EINTR => continue, ENOSYS => return getRandomBytesDevURandom(buf), else => return unexpectedErrno(err), } } return; } if (wasi.is_the_target) { switch (wasi.random_get(buffer.ptr, buffer.len)) { 0 => return, else => |err| return unexpectedErrno(err), } } return getRandomBytesDevURandom(buffer); } fn getRandomBytesDevURandom(buf: []u8) !void { const fd = try openC(c"/dev/urandom", O_RDONLY | O_CLOEXEC, 0); defer close(fd); const st = try fstat(fd); if (!S_ISCHR(st.mode)) { return error.NoDevice; } const stream = &std.fs.File.openHandle(fd).inStream().stream; stream.readNoEof(buf) catch return error.Unexpected; } /// Causes abnormal process termination. /// If linking against libc, this calls the abort() libc function. Otherwise /// it raises SIGABRT followed by SIGKILL and finally lo pub fn abort() noreturn { @setCold(true); // MSVCRT abort() sometimes opens a popup window which is undesirable, so // even when linking libc on Windows we use our own abort implementation. // See https://github.com/ziglang/zig/issues/2071 for more details. if (windows.is_the_target) { if (builtin.mode == .Debug) { @breakpoint(); } windows.kernel32.ExitProcess(3); } if (builtin.link_libc) { system.abort(); } if (builtin.os == .uefi) { exit(0); // TODO choose appropriate exit code } raise(SIGABRT) catch {}; // TODO the rest of the implementation of abort() from musl libc here raise(SIGKILL) catch {}; exit(127); } pub const RaiseError = error{Unexpected}; pub fn raise(sig: u8) RaiseError!void { if (builtin.link_libc) { switch (errno(system.raise(sig))) { 0 => return, else => |err| return unexpectedErrno(err), } } if (wasi.is_the_target) { switch (wasi.proc_raise(SIGABRT)) { 0 => return, else => |err| return unexpectedErrno(err), } } if (linux.is_the_target) { var set: linux.sigset_t = undefined; linux.blockAppSignals(&set); const tid = linux.syscall0(linux.SYS_gettid); const rc = linux.syscall2(linux.SYS_tkill, tid, sig); linux.restoreSignals(&set); switch (errno(rc)) { 0 => return, else => |err| return unexpectedErrno(err), } } @compileError("std.os.raise unimplemented for this target"); } pub const KillError = error{ PermissionDenied, Unexpected, }; pub fn kill(pid: pid_t, sig: u8) KillError!void { switch (errno(system.kill(pid, sig))) { 0 => return, EINVAL => unreachable, // invalid signal EPERM => return error.PermissionDenied, ESRCH => unreachable, // always a race condition else => |err| return unexpectedErrno(err), } } /// Exits the program cleanly with the specified status code. pub fn exit(status: u8) noreturn { if (builtin.link_libc) { system.exit(status); } if (windows.is_the_target) { windows.kernel32.ExitProcess(status); } if (wasi.is_the_target) { wasi.proc_exit(status); } if (linux.is_the_target and !builtin.single_threaded) { linux.exit_group(status); } if (uefi.is_the_target) { // exit() is only avaliable if exitBootServices() has not been called yet. // This call to exit should not fail, so we don't care about its return value. if (uefi.system_table.boot_services) |bs| { _ = bs.exit(uefi.handle, status, 0, null); } // If we can't exit, reboot the system instead. uefi.system_table.runtime_services.resetSystem(uefi.tables.ResetType.ResetCold, status, 0, null); } system.exit(status); } pub const ReadError = error{ InputOutput, SystemResources, IsDir, OperationAborted, BrokenPipe, /// This error occurs when no global event loop is configured, /// and reading from the file descriptor would block. WouldBlock, Unexpected, }; /// Returns the number of bytes that were read, which can be less than /// buf.len. If 0 bytes were read, that means EOF. /// If the application has a global event loop enabled, EAGAIN is handled /// via the event loop. Otherwise EAGAIN results in error.WouldBlock. pub fn read(fd: fd_t, buf: []u8) ReadError!usize { if (windows.is_the_target) { return windows.ReadFile(fd, buf); } if (wasi.is_the_target and !builtin.link_libc) { const iovs = [1]iovec{iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }}; var nread: usize = undefined; switch (wasi.fd_read(fd, &iovs, iovs.len, &nread)) { 0 => return nread, else => |err| return unexpectedErrno(err), } } while (true) { const rc = system.read(fd, buf.ptr, buf.len); switch (errno(rc)) { 0 => return @intCast(usize, rc), EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, EAGAIN => if (std.event.Loop.instance) |loop| { loop.waitUntilFdReadable(fd) catch return error.WouldBlock; continue; } else { return error.WouldBlock; }, EBADF => unreachable, // Always a race condition. EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } return index; } /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// This function is for blocking file descriptors only. pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize { if (darwin.is_the_target) { // Darwin does not have preadv but it does have pread. var off: usize = 0; var iov_i: usize = 0; var inner_off: usize = 0; while (true) { const v = iov[iov_i]; const rc = darwin.pread(fd, v.iov_base + inner_off, v.iov_len - inner_off, offset + off); const err = darwin.getErrno(rc); switch (err) { 0 => { const amt_read = @bitCast(usize, rc); off += amt_read; inner_off += amt_read; if (inner_off == v.iov_len) { iov_i += 1; inner_off = 0; if (iov_i == iov.len) { return off; } } if (rc == 0) return off; // EOF continue; }, EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, ESPIPE => unreachable, // fd is not seekable EAGAIN => unreachable, // This function is for blocking reads. EBADF => unreachable, // always a race condition EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, ENOMEM => return error.SystemResources, else => return unexpectedErrno(err), } } } while (true) { // TODO handle the case when iov_len is too large and get rid of this @intCast const rc = system.preadv(fd, iov.ptr, @intCast(u32, iov.len), offset); switch (errno(rc)) { 0 => return @bitCast(usize, rc), EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, EAGAIN => unreachable, // This function is for blocking reads. EBADF => unreachable, // always a race condition EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } } pub const WriteError = error{ DiskQuota, FileTooBig, InputOutput, NoSpaceLeft, AccessDenied, BrokenPipe, SystemResources, OperationAborted, Unexpected, }; /// Write to a file descriptor. Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { if (windows.is_the_target) { return windows.WriteFile(fd, bytes); } if (wasi.is_the_target and !builtin.link_libc) { const ciovs = [1]iovec_const{iovec_const{ .iov_base = bytes.ptr, .iov_len = bytes.len, }}; var nwritten: usize = undefined; switch (wasi.fd_write(fd, &ciovs, ciovs.len, &nwritten)) { 0 => return, else => |err| return unexpectedErrno(err), } } // Linux can return EINVAL when write amount is > 0x7ffff000 // See https://github.com/ziglang/zig/pull/743#issuecomment-363165856 // TODO audit this. Shawn Landden says that this is not actually true. // if this logic should stay, move it to std.os.linux const max_bytes_len = 0x7ffff000; var index: usize = 0; while (index < bytes.len) { const amt_to_write = math.min(bytes.len - index, usize(max_bytes_len)); const rc = system.write(fd, bytes.ptr + index, amt_to_write); switch (errno(rc)) { 0 => { index += @intCast(usize, rc); continue; }, EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, EAGAIN => unreachable, // This function is for blocking writes. EBADF => unreachable, // Always a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, ENOSPC => return error.NoSpaceLeft, EPERM => return error.AccessDenied, EPIPE => return error.BrokenPipe, else => |err| return unexpectedErrno(err), } } } /// Write multiple buffers to a file descriptor. Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. For non-blocking, see /// `writevAsync`. pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!void { while (true) { // TODO handle the case when iov_len is too large and get rid of this @intCast const rc = system.writev(fd, iov.ptr, @intCast(u32, iov.len)); switch (errno(rc)) { 0 => return, EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, EAGAIN => unreachable, // This function is for blocking writes. EBADF => unreachable, // Always a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, ENOSPC => return error.NoSpaceLeft, EPERM => return error.AccessDenied, EPIPE => return error.BrokenPipe, else => |err| return unexpectedErrno(err), } } } /// Write multiple buffers to a file descriptor, with a position offset. /// Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. For non-blocking, see /// `pwritevAsync`. pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void { if (darwin.is_the_target) { // Darwin does not have pwritev but it does have pwrite. var off: usize = 0; var iov_i: usize = 0; var inner_off: usize = 0; while (true) { const v = iov[iov_i]; const rc = darwin.pwrite(fd, v.iov_base + inner_off, v.iov_len - inner_off, offset + off); const err = darwin.getErrno(rc); switch (err) { 0 => { const amt_written = @bitCast(usize, rc); off += amt_written; inner_off += amt_written; if (inner_off == v.iov_len) { iov_i += 1; inner_off = 0; if (iov_i == iov.len) { return; } } continue; }, EINTR => continue, ESPIPE => unreachable, // `fd` is not seekable. EINVAL => unreachable, EFAULT => unreachable, EAGAIN => unreachable, // This function is for blocking writes. EBADF => unreachable, // Always a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, ENOSPC => return error.NoSpaceLeft, EPERM => return error.AccessDenied, EPIPE => return error.BrokenPipe, else => return unexpectedErrno(err), } } } while (true) { // TODO handle the case when iov_len is too large and get rid of this @intCast const rc = system.pwritev(fd, iov.ptr, @intCast(u32, iov.len), offset); switch (errno(rc)) { 0 => return, EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, EAGAIN => unreachable, // This function is for blocking writes. EBADF => unreachable, // Always a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, ENOSPC => return error.NoSpaceLeft, EPERM => return error.AccessDenied, EPIPE => return error.BrokenPipe, else => |err| return unexpectedErrno(err), } } } pub const OpenError = error{ AccessDenied, FileTooBig, IsDir, SymLinkLoop, ProcessFdQuotaExceeded, NameTooLong, SystemFdQuotaExceeded, NoDevice, FileNotFound, SystemResources, NoSpaceLeft, NotDir, PathAlreadyExists, DeviceBusy, Unexpected, }; /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `openC`. pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!fd_t { const file_path_c = try toPosixPath(file_path); return openC(&file_path_c, flags, perm); } /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `open`. /// TODO https://github.com/ziglang/zig/issues/265 pub fn openC(file_path: [*]const u8, flags: u32, perm: usize) OpenError!fd_t { while (true) { const rc = system.open(file_path, flags, perm); switch (errno(rc)) { 0 => return @intCast(fd_t, rc), EINTR => continue, EFAULT => unreachable, EINVAL => unreachable, EACCES => return error.AccessDenied, EFBIG => return error.FileTooBig, EOVERFLOW => return error.FileTooBig, EISDIR => return error.IsDir, ELOOP => return error.SymLinkLoop, EMFILE => return error.ProcessFdQuotaExceeded, ENAMETOOLONG => return error.NameTooLong, ENFILE => return error.SystemFdQuotaExceeded, ENODEV => return error.NoDevice, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, ENOTDIR => return error.NotDir, EPERM => return error.AccessDenied, EEXIST => return error.PathAlreadyExists, EBUSY => return error.DeviceBusy, else => |err| return unexpectedErrno(err), } } } /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` is relative to the open directory handle `dir_fd`. /// See also `openatC`. pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: usize) OpenError!fd_t { const file_path_c = try toPosixPath(file_path); return openatC(dir_fd, &file_path_c, flags, mode); } /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` is relative to the open directory handle `dir_fd`. /// See also `openat`. pub fn openatC(dir_fd: fd_t, file_path: [*]const u8, flags: u32, mode: usize) OpenError!fd_t { while (true) { const rc = system.openat(dir_fd, file_path, flags, mode); switch (errno(rc)) { 0 => return @intCast(fd_t, rc), EINTR => continue, EFAULT => unreachable, EINVAL => unreachable, EACCES => return error.AccessDenied, EFBIG => return error.FileTooBig, EOVERFLOW => return error.FileTooBig, EISDIR => return error.IsDir, ELOOP => return error.SymLinkLoop, EMFILE => return error.ProcessFdQuotaExceeded, ENAMETOOLONG => return error.NameTooLong, ENFILE => return error.SystemFdQuotaExceeded, ENODEV => return error.NoDevice, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, ENOTDIR => return error.NotDir, EPERM => return error.AccessDenied, EEXIST => return error.PathAlreadyExists, EBUSY => return error.DeviceBusy, else => |err| return unexpectedErrno(err), } } } pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { while (true) { switch (errno(system.dup2(old_fd, new_fd))) { 0 => return, EBUSY, EINTR => continue, EMFILE => return error.ProcessFdQuotaExceeded, EINVAL => unreachable, else => |err| return unexpectedErrno(err), } } } /// This function must allocate memory to add a null terminating bytes on path and each arg. /// It must also convert to KEY=VALUE\0 format for environment variables, and include null /// pointers after the args and after the environment variables. /// `argv[0]` is the executable path. /// This function also uses the PATH environment variable to get the full path to the executable. /// TODO provide execveC which does not take an allocator pub fn execve(allocator: *mem.Allocator, argv_slice: []const []const u8, env_map: *const std.BufMap) !void { const argv_buf = try allocator.alloc(?[*]u8, argv_slice.len + 1); mem.set(?[*]u8, argv_buf, null); defer { for (argv_buf) |arg| { const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break; allocator.free(arg_buf); } allocator.free(argv_buf); } for (argv_slice) |arg, i| { const arg_buf = try allocator.alloc(u8, arg.len + 1); @memcpy(arg_buf.ptr, arg.ptr, arg.len); arg_buf[arg.len] = 0; argv_buf[i] = arg_buf.ptr; } argv_buf[argv_slice.len] = null; const envp_buf = try createNullDelimitedEnvMap(allocator, env_map); defer freeNullDelimitedEnvMap(allocator, envp_buf); const exe_path = argv_slice[0]; if (mem.indexOfScalar(u8, exe_path, '/') != null) { return execveErrnoToErr(errno(system.execve(argv_buf[0].?, argv_buf.ptr, envp_buf.ptr))); } const PATH = getenv("PATH") orelse "/usr/local/bin:/bin/:/usr/bin"; // PATH.len because it is >= the largest search_path // +1 for the / to join the search path and exe_path // +1 for the null terminating byte const path_buf = try allocator.alloc(u8, PATH.len + exe_path.len + 2); defer allocator.free(path_buf); var it = mem.tokenize(PATH, ":"); var seen_eacces = false; var err: usize = undefined; while (it.next()) |search_path| { mem.copy(u8, path_buf, search_path); path_buf[search_path.len] = '/'; mem.copy(u8, path_buf[search_path.len + 1 ..], exe_path); path_buf[search_path.len + exe_path.len + 1] = 0; err = errno(system.execve(path_buf.ptr, argv_buf.ptr, envp_buf.ptr)); assert(err > 0); if (err == EACCES) { seen_eacces = true; } else if (err != ENOENT) { return execveErrnoToErr(err); } } if (seen_eacces) { err = EACCES; } return execveErrnoToErr(err); } pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*]u8 { const envp_count = env_map.count(); const envp_buf = try allocator.alloc(?[*]u8, envp_count + 1); mem.set(?[*]u8, envp_buf, null); errdefer freeNullDelimitedEnvMap(allocator, envp_buf); { var it = env_map.iterator(); var i: usize = 0; while (it.next()) |pair| : (i += 1) { const env_buf = try allocator.alloc(u8, pair.key.len + pair.value.len + 2); @memcpy(env_buf.ptr, pair.key.ptr, pair.key.len); env_buf[pair.key.len] = '='; @memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len); env_buf[env_buf.len - 1] = 0; envp_buf[i] = env_buf.ptr; } assert(i == envp_count); } assert(envp_buf[envp_count] == null); return envp_buf; } pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*]u8) void { for (envp_buf) |env| { const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break; allocator.free(env_buf); } allocator.free(envp_buf); } pub const ExecveError = error{ SystemResources, AccessDenied, InvalidExe, FileSystem, IsDir, FileNotFound, NotDir, FileBusy, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, NameTooLong, Unexpected, }; fn execveErrnoToErr(err: usize) ExecveError { assert(err > 0); switch (err) { EFAULT => unreachable, E2BIG => return error.SystemResources, EMFILE => return error.ProcessFdQuotaExceeded, ENAMETOOLONG => return error.NameTooLong, ENFILE => return error.SystemFdQuotaExceeded, ENOMEM => return error.SystemResources, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EINVAL => return error.InvalidExe, ENOEXEC => return error.InvalidExe, EIO => return error.FileSystem, ELOOP => return error.FileSystem, EISDIR => return error.IsDir, ENOENT => return error.FileNotFound, ENOTDIR => return error.NotDir, ETXTBSY => return error.FileBusy, else => return unexpectedErrno(err), } } /// Get an environment variable. /// See also `getenvC`. /// TODO make this go through libc when we have it pub fn getenv(key: []const u8) ?[]const u8 { for (environ) |ptr| { var line_i: usize = 0; while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const this_key = ptr[0..line_i]; if (!mem.eql(u8, key, this_key)) continue; var end_i: usize = line_i; while (ptr[end_i] != 0) : (end_i += 1) {} const this_value = ptr[line_i + 1 .. end_i]; return this_value; } return null; } /// Get an environment variable with a null-terminated name. /// See also `getenv`. /// TODO https://github.com/ziglang/zig/issues/265 pub fn getenvC(key: [*]const u8) ?[]const u8 { if (builtin.link_libc) { const value = system.getenv(key) orelse return null; return mem.toSliceConst(u8, value); } return getenv(mem.toSliceConst(u8, key)); } pub const GetCwdError = error{ NameTooLong, CurrentWorkingDirectoryUnlinked, Unexpected, }; /// The result is a slice of out_buffer, indexed from 0. pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { if (windows.is_the_target) { return windows.GetCurrentDirectory(out_buffer); } const err = if (builtin.link_libc) blk: { break :blk if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; } else blk: { break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); }; switch (err) { 0 => return mem.toSlice(u8, out_buffer.ptr), EFAULT => unreachable, EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, ERANGE => return error.NameTooLong, else => return unexpectedErrno(@intCast(usize, err)), } } pub const SymLinkError = error{ AccessDenied, DiskQuota, PathAlreadyExists, FileSystem, SymLinkLoop, FileNotFound, SystemResources, NoSpaceLeft, ReadOnlyFileSystem, NotDir, NameTooLong, InvalidUtf8, BadPathName, Unexpected, }; /// Creates a symbolic link named `sym_link_path` which contains the string `target_path`. /// A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent /// one; the latter case is known as a dangling link. /// If `sym_link_path` exists, it will not be overwritten. /// See also `symlinkC` and `symlinkW`. pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void { if (windows.is_the_target) { const target_path_w = try windows.sliceToPrefixedFileW(target_path); const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path); return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0); } else { const target_path_c = try toPosixPath(target_path); const sym_link_path_c = try toPosixPath(sym_link_path); return symlinkC(&target_path_c, &sym_link_path_c); } } /// This is the same as `symlink` except the parameters are null-terminated pointers. /// See also `symlink`. pub fn symlinkC(target_path: [*]const u8, sym_link_path: [*]const u8) SymLinkError!void { if (windows.is_the_target) { const target_path_w = try windows.cStrToPrefixedFileW(target_path); const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path); return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0); } switch (errno(system.symlink(target_path, sym_link_path))) { 0 => return, EFAULT => unreachable, EINVAL => unreachable, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EDQUOT => return error.DiskQuota, EEXIST => return error.PathAlreadyExists, EIO => return error.FileSystem, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOTDIR => return error.NotDir, ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub fn symlinkat(target_path: []const u8, newdirfd: fd_t, sym_link_path: []const u8) SymLinkError!void { const target_path_c = try toPosixPath(target_path); const sym_link_path_c = try toPosixPath(sym_link_path); return symlinkatC(target_path_c, newdirfd, sym_link_path_c); } pub fn symlinkatC(target_path: [*]const u8, newdirfd: fd_t, sym_link_path: [*]const u8) SymLinkError!void { switch (errno(system.symlinkat(target_path, newdirfd, sym_link_path))) { 0 => return, EFAULT => unreachable, EINVAL => unreachable, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EDQUOT => return error.DiskQuota, EEXIST => return error.PathAlreadyExists, EIO => return error.FileSystem, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOTDIR => return error.NotDir, ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub const UnlinkError = error{ FileNotFound, AccessDenied, FileBusy, FileSystem, IsDir, SymLinkLoop, NameTooLong, NotDir, SystemResources, ReadOnlyFileSystem, Unexpected, /// On Windows, file paths must be valid Unicode. InvalidUtf8, /// On Windows, file paths cannot contain these characters: /// '/', '*', '?', '"', '<', '>', '|' BadPathName, }; /// Delete a name and possibly the file it refers to. /// See also `unlinkC`. pub fn unlink(file_path: []const u8) UnlinkError!void { if (windows.is_the_target) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return windows.DeleteFileW(&file_path_w); } else { const file_path_c = try toPosixPath(file_path); return unlinkC(&file_path_c); } } /// Same as `unlink` except the parameter is a null terminated UTF8-encoded string. pub fn unlinkC(file_path: [*]const u8) UnlinkError!void { if (windows.is_the_target) { const file_path_w = try windows.cStrToPrefixedFileW(file_path); return windows.DeleteFileW(&file_path_w); } switch (errno(system.unlink(file_path))) { 0 => return, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EBUSY => return error.FileBusy, EFAULT => unreachable, EINVAL => unreachable, EIO => return error.FileSystem, EISDIR => return error.IsDir, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOTDIR => return error.NotDir, ENOMEM => return error.SystemResources, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } const RenameError = error{ AccessDenied, FileBusy, DiskQuota, IsDir, SymLinkLoop, LinkQuotaExceeded, NameTooLong, FileNotFound, NotDir, SystemResources, NoSpaceLeft, PathAlreadyExists, ReadOnlyFileSystem, RenameAcrossMountPoints, InvalidUtf8, BadPathName, Unexpected, }; /// Change the name or location of a file. pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void { if (windows.is_the_target) { const old_path_w = try windows.sliceToPrefixedFileW(old_path); const new_path_w = try windows.sliceToPrefixedFileW(new_path); return renameW(&old_path_w, &new_path_w); } else { const old_path_c = try toPosixPath(old_path); const new_path_c = try toPosixPath(new_path); return renameC(&old_path_c, &new_path_c); } } /// Same as `rename` except the parameters are null-terminated byte arrays. pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void { if (windows.is_the_target) { const old_path_w = try windows.cStrToPrefixedFileW(old_path); const new_path_w = try windows.cStrToPrefixedFileW(new_path); return renameW(&old_path_w, &new_path_w); } switch (errno(system.rename(old_path, new_path))) { 0 => return, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EBUSY => return error.FileBusy, EDQUOT => return error.DiskQuota, EFAULT => unreachable, EINVAL => unreachable, EISDIR => return error.IsDir, ELOOP => return error.SymLinkLoop, EMLINK => return error.LinkQuotaExceeded, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOTDIR => return error.NotDir, ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, EEXIST => return error.PathAlreadyExists, ENOTEMPTY => return error.PathAlreadyExists, EROFS => return error.ReadOnlyFileSystem, EXDEV => return error.RenameAcrossMountPoints, else => |err| return unexpectedErrno(err), } } /// Same as `rename` except the parameters are null-terminated UTF16LE encoded byte arrays. /// Assumes target is Windows. pub fn renameW(old_path: [*]const u16, new_path: [*]const u16) RenameError!void { const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH; return windows.MoveFileExW(old_path, new_path, flags); } pub const MakeDirError = error{ AccessDenied, DiskQuota, PathAlreadyExists, SymLinkLoop, LinkQuotaExceeded, NameTooLong, FileNotFound, SystemResources, NoSpaceLeft, NotDir, ReadOnlyFileSystem, InvalidUtf8, BadPathName, Unexpected, }; /// Create a directory. /// `mode` is ignored on Windows. pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void { if (windows.is_the_target) { const dir_path_w = try windows.sliceToPrefixedFileW(dir_path); return windows.CreateDirectoryW(&dir_path_w, null); } else { const dir_path_c = try toPosixPath(dir_path); return mkdirC(&dir_path_c, mode); } } /// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string. pub fn mkdirC(dir_path: [*]const u8, mode: u32) MakeDirError!void { if (windows.is_the_target) { const dir_path_w = try windows.cStrToPrefixedFileW(dir_path); return windows.CreateDirectoryW(&dir_path_w, null); } switch (errno(system.mkdir(dir_path, mode))) { 0 => return, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EDQUOT => return error.DiskQuota, EEXIST => return error.PathAlreadyExists, EFAULT => unreachable, ELOOP => return error.SymLinkLoop, EMLINK => return error.LinkQuotaExceeded, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOSPC => return error.NoSpaceLeft, ENOTDIR => return error.NotDir, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub const DeleteDirError = error{ AccessDenied, FileBusy, SymLinkLoop, NameTooLong, FileNotFound, SystemResources, NotDir, DirNotEmpty, ReadOnlyFileSystem, InvalidUtf8, BadPathName, Unexpected, }; /// Deletes an empty directory. pub fn rmdir(dir_path: []const u8) DeleteDirError!void { if (windows.is_the_target) { const dir_path_w = try windows.sliceToPrefixedFileW(dir_path); return windows.RemoveDirectoryW(&dir_path_w); } else { const dir_path_c = try toPosixPath(dir_path); return rmdirC(&dir_path_c); } } /// Same as `rmdir` except the parameter is null-terminated. pub fn rmdirC(dir_path: [*]const u8) DeleteDirError!void { if (windows.is_the_target) { const dir_path_w = try windows.cStrToPrefixedFileW(dir_path); return windows.RemoveDirectoryW(&dir_path_w); } switch (errno(system.rmdir(dir_path))) { 0 => return, EACCES => return error.AccessDenied, EPERM => return error.AccessDenied, EBUSY => return error.FileBusy, EFAULT => unreachable, EINVAL => unreachable, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOTDIR => return error.NotDir, EEXIST => return error.DirNotEmpty, ENOTEMPTY => return error.DirNotEmpty, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub const ChangeCurDirError = error{ AccessDenied, FileSystem, SymLinkLoop, NameTooLong, FileNotFound, SystemResources, NotDir, Unexpected, }; /// Changes the current working directory of the calling process. /// `dir_path` is recommended to be a UTF-8 encoded string. pub fn chdir(dir_path: []const u8) ChangeCurDirError!void { if (windows.is_the_target) { const dir_path_w = try windows.sliceToPrefixedFileW(dir_path); @compileError("TODO implement chdir for Windows"); } else { const dir_path_c = try toPosixPath(dir_path); return chdirC(&dir_path_c); } } /// Same as `chdir` except the parameter is null-terminated. pub fn chdirC(dir_path: [*]const u8) ChangeCurDirError!void { if (windows.is_the_target) { const dir_path_w = try windows.cStrToPrefixedFileW(dir_path); @compileError("TODO implement chdir for Windows"); } switch (errno(system.chdir(dir_path))) { 0 => return, EACCES => return error.AccessDenied, EFAULT => unreachable, EIO => return error.FileSystem, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOTDIR => return error.NotDir, else => |err| return unexpectedErrno(err), } } pub const ReadLinkError = error{ AccessDenied, FileSystem, SymLinkLoop, NameTooLong, FileNotFound, SystemResources, NotDir, Unexpected, }; /// Read value of a symbolic link. /// The return value is a slice of `out_buffer` from index 0. pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 { if (windows.is_the_target) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); @compileError("TODO implement readlink for Windows"); } else { const file_path_c = try toPosixPath(file_path); return readlinkC(&file_path_c, out_buffer); } } /// Same as `readlink` except `file_path` is null-terminated. pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 { if (windows.is_the_target) { const file_path_w = try windows.cStrToPrefixedFileW(file_path); @compileError("TODO implement readlink for Windows"); } const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len); switch (errno(rc)) { 0 => return out_buffer[0..@bitCast(usize, rc)], EACCES => return error.AccessDenied, EFAULT => unreachable, EINVAL => unreachable, EIO => return error.FileSystem, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOTDIR => return error.NotDir, else => |err| return unexpectedErrno(err), } } pub const SetIdError = error{ ResourceLimitReached, InvalidUserId, PermissionDenied, Unexpected, }; pub fn setuid(uid: u32) SetIdError!void { switch (errno(system.setuid(uid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setreuid(ruid: u32, euid: u32) SetIdError!void { switch (errno(system.setreuid(ruid, euid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setgid(gid: u32) SetIdError!void { switch (errno(system.setgid(gid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setregid(rgid: u32, egid: u32) SetIdError!void { switch (errno(system.setregid(rgid, egid))) { 0 => return, EAGAIN => return error.ResourceLimitReached, EINVAL => return error.InvalidUserId, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } /// Test whether a file descriptor refers to a terminal. pub fn isatty(handle: fd_t) bool { if (windows.is_the_target) { if (isCygwinPty(handle)) return true; var out: windows.DWORD = undefined; return windows.kernel32.GetConsoleMode(handle, &out) != 0; } if (builtin.link_libc) { return system.isatty(handle) != 0; } if (wasi.is_the_target) { @compileError("TODO implement std.os.isatty for WASI"); } if (linux.is_the_target) { var wsz: linux.winsize = undefined; return linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(handle)), linux.TIOCGWINSZ, @ptrToInt(&wsz)) == 0; } unreachable; } pub fn isCygwinPty(handle: fd_t) bool { if (!windows.is_the_target) return false; const size = @sizeOf(windows.FILE_NAME_INFO); var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (size + windows.MAX_PATH); if (windows.kernel32.GetFileInformationByHandleEx( handle, windows.FileNameInfo, @ptrCast(*c_void, &name_info_bytes), name_info_bytes.len, ) == 0) { return false; } const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]); const name_bytes = name_info_bytes[size .. size + usize(name_info.FileNameLength)]; const name_wide = @bytesToSlice(u16, name_bytes); return mem.indexOf(u16, name_wide, [_]u16{ 'm', 's', 'y', 's', '-' }) != null or mem.indexOf(u16, name_wide, [_]u16{ '-', 'p', 't', 'y' }) != null; } pub const SocketError = error{ /// Permission to create a socket of the specified type and/or /// pro‐tocol is denied. PermissionDenied, /// The implementation does not support the specified address family. AddressFamilyNotSupported, /// Unknown protocol, or protocol family not available. ProtocolFamilyNotAvailable, /// The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, /// Insufficient memory is available. The socket cannot be created until sufficient /// resources are freed. SystemResources, /// The protocol type or the specified protocol is not supported within this domain. ProtocolNotSupported, Unexpected, }; pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!i32 { const rc = system.socket(domain, socket_type, protocol); switch (errno(rc)) { 0 => return @intCast(i32, rc), EACCES => return error.PermissionDenied, EAFNOSUPPORT => return error.AddressFamilyNotSupported, EINVAL => return error.ProtocolFamilyNotAvailable, EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, ENOBUFS, ENOMEM => return error.SystemResources, EPROTONOSUPPORT => return error.ProtocolNotSupported, else => |err| return unexpectedErrno(err), } } pub const BindError = error{ /// The address is protected, and the user is not the superuser. /// For UNIX domain sockets: Search permission is denied on a component /// of the path prefix. AccessDenied, /// The given address is already in use, or in the case of Internet domain sockets, /// The port number was specified as zero in the socket /// address structure, but, upon attempting to bind to an ephemeral port, it was /// determined that all port numbers in the ephemeral port range are currently in /// use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range ip(7). AddressInUse, /// A nonexistent interface was requested or the requested address was not local. AddressNotAvailable, /// Too many symbolic links were encountered in resolving addr. SymLinkLoop, /// addr is too long. NameTooLong, /// A component in the directory prefix of the socket pathname does not exist. FileNotFound, /// Insufficient kernel memory was available. SystemResources, /// A component of the path prefix is not a directory. NotDir, /// The socket inode would reside on a read-only filesystem. ReadOnlyFileSystem, Unexpected, }; /// addr is `*const T` where T is one of the sockaddr pub fn bind(fd: i32, addr: *const sockaddr) BindError!void { const rc = system.bind(fd, addr, @sizeOf(sockaddr)); switch (errno(rc)) { 0 => return, EACCES => return error.AccessDenied, EADDRINUSE => return error.AddressInUse, EBADF => unreachable, // always a race condition if this error is returned EINVAL => unreachable, ENOTSOCK => unreachable, EADDRNOTAVAIL => return error.AddressNotAvailable, EFAULT => unreachable, ELOOP => return error.SymLinkLoop, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOTDIR => return error.NotDir, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } const ListenError = error{ /// Another socket is already listening on the same port. /// For Internet domain sockets, the socket referred to by sockfd had not previously /// been bound to an address and, upon attempting to bind it to an ephemeral port, it /// was determined that all port numbers in the ephemeral port range are currently in /// use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7). AddressInUse, /// The file descriptor sockfd does not refer to a socket. FileDescriptorNotASocket, /// The socket is not of a type that supports the listen() operation. OperationNotSupported, Unexpected, }; pub fn listen(sockfd: i32, backlog: u32) ListenError!void { const rc = system.listen(sockfd, backlog); switch (errno(rc)) { 0 => return, EADDRINUSE => return error.AddressInUse, EBADF => unreachable, ENOTSOCK => return error.FileDescriptorNotASocket, EOPNOTSUPP => return error.OperationNotSupported, else => |err| return unexpectedErrno(err), } } pub const AcceptError = error{ ConnectionAborted, /// The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, /// Not enough free memory. This often means that the memory allocation is limited /// by the socket buffer limits, not by the system memory. SystemResources, /// The file descriptor sockfd does not refer to a socket. FileDescriptorNotASocket, /// The referenced socket is not of type SOCK_STREAM. OperationNotSupported, ProtocolFailure, /// Firewall rules forbid connection. BlockedByFirewall, Unexpected, }; /// Accept a connection on a socket. `fd` must be opened in blocking mode. /// See also `accept4_async`. pub fn accept4(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 { while (true) { var sockaddr_size = u32(@sizeOf(sockaddr)); const rc = system.accept4(fd, addr, &sockaddr_size, flags); switch (errno(rc)) { 0 => return @intCast(i32, rc), EINTR => continue, else => |err| return unexpectedErrno(err), EAGAIN => unreachable, // This function is for blocking only. EBADF => unreachable, // always a race condition ECONNABORTED => return error.ConnectionAborted, EFAULT => unreachable, EINVAL => unreachable, EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, ENOBUFS => return error.SystemResources, ENOMEM => return error.SystemResources, ENOTSOCK => return error.FileDescriptorNotASocket, EOPNOTSUPP => return error.OperationNotSupported, EPROTO => return error.ProtocolFailure, EPERM => return error.BlockedByFirewall, } } } /// This is the same as `accept4` except `fd` is expected to be non-blocking. /// Returns -1 if would block. pub fn accept4_async(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 { while (true) { var sockaddr_size = u32(@sizeOf(sockaddr)); const rc = system.accept4(fd, addr, &sockaddr_size, flags); switch (errno(rc)) { 0 => return @intCast(i32, rc), EINTR => continue, else => |err| return unexpectedErrno(err), EAGAIN => return -1, EBADF => unreachable, // always a race condition ECONNABORTED => return error.ConnectionAborted, EFAULT => unreachable, EINVAL => unreachable, EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, ENOBUFS => return error.SystemResources, ENOMEM => return error.SystemResources, ENOTSOCK => return error.FileDescriptorNotASocket, EOPNOTSUPP => return error.OperationNotSupported, EPROTO => return error.ProtocolFailure, EPERM => return error.BlockedByFirewall, } } } pub const EpollCreateError = error{ /// The per-user limit on the number of epoll instances imposed by /// /proc/sys/fs/epoll/max_user_instances was encountered. See epoll(7) for further /// details. /// Or, The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, /// There was insufficient memory to create the kernel object. SystemResources, Unexpected, }; pub fn epoll_create1(flags: u32) EpollCreateError!i32 { const rc = system.epoll_create1(flags); switch (errno(rc)) { 0 => return @intCast(i32, rc), else => |err| return unexpectedErrno(err), EINVAL => unreachable, EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, ENOMEM => return error.SystemResources, } } pub const EpollCtlError = error{ /// op was EPOLL_CTL_ADD, and the supplied file descriptor fd is already registered /// with this epoll instance. FileDescriptorAlreadyPresentInSet, /// fd refers to an epoll instance and this EPOLL_CTL_ADD operation would result in a /// circular loop of epoll instances monitoring one another. OperationCausesCircularLoop, /// op was EPOLL_CTL_MOD or EPOLL_CTL_DEL, and fd is not registered with this epoll /// instance. FileDescriptorNotRegistered, /// There was insufficient memory to handle the requested op control operation. SystemResources, /// The limit imposed by /proc/sys/fs/epoll/max_user_watches was encountered while /// trying to register (EPOLL_CTL_ADD) a new file descriptor on an epoll instance. /// See epoll(7) for further details. UserResourceLimitReached, /// The target file fd does not support epoll. This error can occur if fd refers to, /// for example, a regular file or a directory. FileDescriptorIncompatibleWithEpoll, Unexpected, }; pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*epoll_event) EpollCtlError!void { const rc = system.epoll_ctl(epfd, op, fd, event); switch (errno(rc)) { 0 => return, else => |err| return unexpectedErrno(err), EBADF => unreachable, // always a race condition if this happens EEXIST => return error.FileDescriptorAlreadyPresentInSet, EINVAL => unreachable, ELOOP => return error.OperationCausesCircularLoop, ENOENT => return error.FileDescriptorNotRegistered, ENOMEM => return error.SystemResources, ENOSPC => return error.UserResourceLimitReached, EPERM => return error.FileDescriptorIncompatibleWithEpoll, } } /// Waits for an I/O event on an epoll file descriptor. /// Returns the number of file descriptors ready for the requested I/O, /// or zero if no file descriptor became ready during the requested timeout milliseconds. pub fn epoll_wait(epfd: i32, events: []epoll_event, timeout: i32) usize { while (true) { // TODO get rid of the @intCast const rc = system.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout); switch (errno(rc)) { 0 => return @intCast(usize, rc), EINTR => continue, EBADF => unreachable, EFAULT => unreachable, EINVAL => unreachable, else => unreachable, } } } pub const EventFdError = error{ SystemResources, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, Unexpected, }; pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 { const rc = system.eventfd(initval, flags); switch (errno(rc)) { 0 => return @intCast(i32, rc), else => |err| return unexpectedErrno(err), EINVAL => unreachable, // invalid parameters EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, ENODEV => return error.SystemResources, ENOMEM => return error.SystemResources, } } pub const GetSockNameError = error{ /// Insufficient resources were available in the system to perform the operation. SystemResources, Unexpected, }; pub fn getsockname(sockfd: i32) GetSockNameError!sockaddr { var addr: sockaddr = undefined; var addrlen: socklen_t = @sizeOf(sockaddr); switch (errno(system.getsockname(sockfd, &addr, &addrlen))) { 0 => return addr, else => |err| return unexpectedErrno(err), EBADF => unreachable, // always a race condition EFAULT => unreachable, EINVAL => unreachable, // invalid parameters ENOTSOCK => unreachable, ENOBUFS => return error.SystemResources, } } pub const ConnectError = error{ /// For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket /// file, or search permission is denied for one of the directories in the path prefix. /// or /// The user tried to connect to a broadcast address without having the socket broadcast flag enabled or /// the connection request failed because of a local firewall rule. PermissionDenied, /// Local address is already in use. AddressInUse, /// (Internet domain sockets) The socket referred to by sockfd had not previously been bound to an /// address and, upon attempting to bind it to an ephemeral port, it was determined that all port numbers /// in the ephemeral port range are currently in use. See the discussion of /// /proc/sys/net/ipv4/ip_local_port_range in ip(7). AddressNotAvailable, /// The passed address didn't have the correct address family in its sa_family field. AddressFamilyNotSupported, /// Insufficient entries in the routing cache. SystemResources, /// A connect() on a stream socket found no one listening on the remote address. ConnectionRefused, /// Network is unreachable. NetworkUnreachable, /// Timeout while attempting connection. The server may be too busy to accept new connections. Note /// that for IP sockets the timeout may be very long when syncookies are enabled on the server. ConnectionTimedOut, Unexpected, }; /// Initiate a connection on a socket. /// This is for blocking file descriptors only. /// For non-blocking, see `connect_async`. pub fn connect(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void { while (true) { switch (errno(system.connect(sockfd, sock_addr, len))) { 0 => return, EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, EADDRNOTAVAIL => return error.AddressNotAvailable, EAFNOSUPPORT => return error.AddressFamilyNotSupported, EAGAIN => return error.SystemResources, EALREADY => unreachable, // The socket is nonblocking and a previous connection attempt has not yet been completed. EBADF => unreachable, // sockfd is not a valid open file descriptor. ECONNREFUSED => return error.ConnectionRefused, EFAULT => unreachable, // The socket structure address is outside the user's address space. EINPROGRESS => unreachable, // The socket is nonblocking and the connection cannot be completed immediately. EINTR => continue, EISCONN => unreachable, // The socket is already connected. ENETUNREACH => return error.NetworkUnreachable, ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, else => |err| return unexpectedErrno(err), } } } /// Same as `connect` except it is for blocking socket file descriptors. /// It expects to receive EINPROGRESS`. pub fn connect_async(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void { while (true) { switch (errno(system.connect(sockfd, sock_addr, len))) { EINVAL => unreachable, EINTR => continue, 0, EINPROGRESS => return, EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, EADDRNOTAVAIL => return error.AddressNotAvailable, EAFNOSUPPORT => return error.AddressFamilyNotSupported, EAGAIN => return error.SystemResources, EALREADY => unreachable, // The socket is nonblocking and a previous connection attempt has not yet been completed. EBADF => unreachable, // sockfd is not a valid open file descriptor. ECONNREFUSED => return error.ConnectionRefused, EFAULT => unreachable, // The socket structure address is outside the user's address space. EISCONN => unreachable, // The socket is already connected. ENETUNREACH => return error.NetworkUnreachable, ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, else => |err| return unexpectedErrno(err), } } } pub fn getsockoptError(sockfd: i32) ConnectError!void { var err_code: u32 = undefined; var size: u32 = @sizeOf(u32); const rc = system.getsockopt(sockfd, SOL_SOCKET, SO_ERROR, @ptrCast([*]u8, &err_code), &size); assert(size == 4); switch (errno(rc)) { 0 => switch (err_code) { 0 => return, EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, EADDRNOTAVAIL => return error.AddressNotAvailable, EAFNOSUPPORT => return error.AddressFamilyNotSupported, EAGAIN => return error.SystemResources, EALREADY => unreachable, // The socket is nonblocking and a previous connection attempt has not yet been completed. EBADF => unreachable, // sockfd is not a valid open file descriptor. ECONNREFUSED => return error.ConnectionRefused, EFAULT => unreachable, // The socket structure address is outside the user's address space. EISCONN => unreachable, // The socket is already connected. ENETUNREACH => return error.NetworkUnreachable, ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, else => |err| return unexpectedErrno(err), }, EBADF => unreachable, // The argument sockfd is not a valid file descriptor. EFAULT => unreachable, // The address pointed to by optval or optlen is not in a valid part of the process address space. EINVAL => unreachable, ENOPROTOOPT => unreachable, // The option is unknown at the level indicated. ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. else => |err| return unexpectedErrno(err), } } pub fn waitpid(pid: i32, flags: u32) u32 { // TODO allow implicit pointer cast from *u32 to *c_uint ? const Status = if (builtin.link_libc) c_uint else u32; var status: Status = undefined; while (true) { switch (errno(system.waitpid(pid, &status, flags))) { 0 => return @bitCast(u32, status), EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. EINVAL => unreachable, // The options argument was invalid else => unreachable, } } } pub const FStatError = error{ SystemResources, Unexpected, }; pub fn fstat(fd: fd_t) FStatError!Stat { var stat: Stat = undefined; if (darwin.is_the_target) { switch (darwin.getErrno(darwin.@"fstat$INODE64"(fd, &stat))) { 0 => return stat, EINVAL => unreachable, EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } switch (errno(system.fstat(fd, &stat))) { 0 => return stat, EINVAL => unreachable, EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub const KQueueError = error{ /// The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, Unexpected, }; pub fn kqueue() KQueueError!i32 { const rc = system.kqueue(); switch (errno(rc)) { 0 => return @intCast(i32, rc), EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, else => |err| return unexpectedErrno(err), } } pub const KEventError = error{ /// The process does not have permission to register a filter. AccessDenied, /// The event could not be found to be modified or deleted. EventNotFound, /// No memory was available to register the event. SystemResources, /// The specified process to attach to does not exist. ProcessNotFound, /// changelist or eventlist had too many items on it. /// TODO remove this possibility Overflow, }; pub fn kevent( kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec, ) KEventError!usize { while (true) { const rc = system.kevent( kq, changelist.ptr, try math.cast(c_int, changelist.len), eventlist.ptr, try math.cast(c_int, eventlist.len), timeout, ); switch (errno(rc)) { 0 => return @intCast(usize, rc), EACCES => return error.AccessDenied, EFAULT => unreachable, EBADF => unreachable, // Always a race condition. EINTR => continue, EINVAL => unreachable, ENOENT => return error.EventNotFound, ENOMEM => return error.SystemResources, ESRCH => return error.ProcessNotFound, else => unreachable, } } } pub const INotifyInitError = error{ ProcessFdQuotaExceeded, SystemFdQuotaExceeded, SystemResources, Unexpected, }; /// initialize an inotify instance pub fn inotify_init1(flags: u32) INotifyInitError!i32 { const rc = system.inotify_init1(flags); switch (errno(rc)) { 0 => return @intCast(i32, rc), EINVAL => unreachable, EMFILE => return error.ProcessFdQuotaExceeded, ENFILE => return error.SystemFdQuotaExceeded, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub const INotifyAddWatchError = error{ AccessDenied, NameTooLong, FileNotFound, SystemResources, UserResourceLimitReached, Unexpected, }; /// add a watch to an initialized inotify instance pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INotifyAddWatchError!i32 { const pathname_c = try toPosixPath(pathname); return inotify_add_watchC(inotify_fd, &pathname_c, mask); } /// Same as `inotify_add_watch` except pathname is null-terminated. pub fn inotify_add_watchC(inotify_fd: i32, pathname: [*]const u8, mask: u32) INotifyAddWatchError!i32 { const rc = system.inotify_add_watch(inotify_fd, pathname, mask); switch (errno(rc)) { 0 => return @intCast(i32, rc), EACCES => return error.AccessDenied, EBADF => unreachable, EFAULT => unreachable, EINVAL => unreachable, ENAMETOOLONG => return error.NameTooLong, ENOENT => return error.FileNotFound, ENOMEM => return error.SystemResources, ENOSPC => return error.UserResourceLimitReached, else => |err| return unexpectedErrno(err), } } /// remove an existing watch from an inotify instance pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void { switch (errno(system.inotify_rm_watch(inotify_fd, wd))) { 0 => return, EBADF => unreachable, EINVAL => unreachable, else => unreachable, } } pub const MProtectError = error{ /// The memory cannot be given the specified access. This can happen, for example, if you /// mmap(2) a file to which you have read-only access, then ask mprotect() to mark it /// PROT_WRITE. AccessDenied, /// Changing the protection of a memory region would result in the total number of map‐ /// pings with distinct attributes (e.g., read versus read/write protection) exceeding the /// allowed maximum. (For example, making the protection of a range PROT_READ in the mid‐ /// dle of a region currently protected as PROT_READ|PROT_WRITE would result in three map‐ /// pings: two read/write mappings at each end and a read-only mapping in the middle.) OutOfMemory, Unexpected, }; /// `memory.len` must be page-aligned. pub fn mprotect(memory: []align(mem.page_size) u8, protection: u32) MProtectError!void { assert(mem.isAligned(memory.len, mem.page_size)); switch (errno(system.mprotect(memory.ptr, memory.len, protection))) { 0 => return, EINVAL => unreachable, EACCES => return error.AccessDenied, ENOMEM => return error.OutOfMemory, else => |err| return unexpectedErrno(err), } } pub const ForkError = error{ SystemResources, Unexpected, }; pub fn fork() ForkError!pid_t { const rc = system.fork(); switch (errno(rc)) { 0 => return @intCast(pid_t, rc), EAGAIN => return error.SystemResources, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub const MMapError = error{ /// The underlying filesystem of the specified file does not support memory mapping. MemoryMappingNotSupported, /// A file descriptor refers to a non-regular file. Or a file mapping was requested, /// but the file descriptor is not open for reading. Or `MAP_SHARED` was requested /// and `PROT_WRITE` is set, but the file descriptor is not open in `O_RDWR` mode. /// Or `PROT_WRITE` is set, but the file is append-only. AccessDenied, /// The `prot` argument asks for `PROT_EXEC` but the mapped area belongs to a file on /// a filesystem that was mounted no-exec. PermissionDenied, LockedMemoryLimitExceeded, OutOfMemory, Unexpected, }; /// Map files or devices into memory. /// Use of a mapped region can result in these signals: /// * SIGSEGV - Attempted write into a region mapped as read-only. /// * SIGBUS - Attempted access to a portion of the buffer that does not correspond to the file pub fn mmap( ptr: ?[*]align(mem.page_size) u8, length: usize, prot: u32, flags: u32, fd: fd_t, offset: u64, ) MMapError![]align(mem.page_size) u8 { const err = if (builtin.link_libc) blk: { const rc = std.c.mmap(ptr, length, prot, flags, fd, offset); if (rc != std.c.MAP_FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length]; break :blk @intCast(usize, system._errno().*); } else blk: { const rc = system.mmap(ptr, length, prot, flags, fd, offset); const err = errno(rc); if (err == 0) return @intToPtr([*]align(mem.page_size) u8, rc)[0..length]; break :blk err; }; switch (err) { ETXTBSY => return error.AccessDenied, EACCES => return error.AccessDenied, EPERM => return error.PermissionDenied, EAGAIN => return error.LockedMemoryLimitExceeded, EBADF => unreachable, // Always a race condition. EOVERFLOW => unreachable, // The number of pages used for length + offset would overflow. ENODEV => return error.MemoryMappingNotSupported, EINVAL => unreachable, // Invalid parameters to mmap() ENOMEM => return error.OutOfMemory, else => return unexpectedErrno(err), } } /// Deletes the mappings for the specified address range, causing /// further references to addresses within the range to generate invalid memory references. /// Note that while POSIX allows unmapping a region in the middle of an existing mapping, /// Zig's munmap function does not, for two reasons: /// * It violates the Zig principle that resource deallocation must succeed. /// * The Windows function, VirtualFree, has this restriction. pub fn munmap(memory: []align(mem.page_size) u8) void { switch (errno(system.munmap(memory.ptr, memory.len))) { 0 => return, EINVAL => unreachable, // Invalid parameters. ENOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping. else => unreachable, } } pub const AccessError = error{ PermissionDenied, FileNotFound, NameTooLong, InputOutput, SystemResources, BadPathName, /// On Windows, file paths must be valid Unicode. InvalidUtf8, Unexpected, }; /// check user's permissions for a file /// TODO currently this assumes `mode` is `F_OK` on Windows. pub fn access(path: []const u8, mode: u32) AccessError!void { if (windows.is_the_target) { const path_w = try windows.sliceToPrefixedFileW(path); _ = try windows.GetFileAttributesW(&path_w); return; } const path_c = try toPosixPath(path); return accessC(&path_c, mode); } /// Same as `access` except `path` is null-terminated. pub fn accessC(path: [*]const u8, mode: u32) AccessError!void { if (windows.is_the_target) { const path_w = try windows.cStrToPrefixedFileW(path); _ = try windows.GetFileAttributesW(&path_w); return; } switch (errno(system.access(path, mode))) { 0 => return, EACCES => return error.PermissionDenied, EROFS => return error.PermissionDenied, ELOOP => return error.PermissionDenied, ETXTBSY => return error.PermissionDenied, ENOTDIR => return error.FileNotFound, ENOENT => return error.FileNotFound, ENAMETOOLONG => return error.NameTooLong, EINVAL => unreachable, EFAULT => unreachable, EIO => return error.InputOutput, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. /// Otherwise use `access` or `accessC`. /// TODO currently this ignores `mode`. pub fn accessW(path: [*]const u16, mode: u32) windows.GetFileAttributesError!void { const ret = try windows.GetFileAttributesW(path); if (ret != windows.INVALID_FILE_ATTRIBUTES) { return; } switch (windows.kernel32.GetLastError()) { windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, windows.ERROR.ACCESS_DENIED => return error.PermissionDenied, else => |err| return windows.unexpectedError(err), } } pub const PipeError = error{ SystemFdQuotaExceeded, ProcessFdQuotaExceeded, Unexpected, }; /// Creates a unidirectional data channel that can be used for interprocess communication. pub fn pipe() PipeError![2]fd_t { var fds: [2]fd_t = undefined; switch (errno(system.pipe(&fds))) { 0 => return fds, EINVAL => unreachable, // Invalid parameters to pipe() EFAULT => unreachable, // Invalid fds pointer ENFILE => return error.SystemFdQuotaExceeded, EMFILE => return error.ProcessFdQuotaExceeded, else => |err| return unexpectedErrno(err), } } pub fn pipe2(flags: u32) PipeError![2]fd_t { var fds: [2]fd_t = undefined; switch (errno(system.pipe2(&fds, flags))) { 0 => return fds, EINVAL => unreachable, // Invalid flags EFAULT => unreachable, // Invalid fds pointer ENFILE => return error.SystemFdQuotaExceeded, EMFILE => return error.ProcessFdQuotaExceeded, else => |err| return unexpectedErrno(err), } } pub const SysCtlError = error{ PermissionDenied, SystemResources, NameTooLong, Unexpected, }; pub fn sysctl( name: []const c_int, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize, ) SysCtlError!void { const name_len = math.cast(c_uint, name.len) catch return error.NameTooLong; switch (errno(system.sysctl(name.ptr, name_len, oldp, oldlenp, newp, newlen))) { 0 => return, EFAULT => unreachable, EPERM => return error.PermissionDenied, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub fn sysctlbynameC( name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize, ) SysCtlError!void { switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) { 0 => return, EFAULT => unreachable, EPERM => return error.PermissionDenied, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { switch (errno(system.gettimeofday(tv, tz))) { 0 => return, EINVAL => unreachable, else => unreachable, } } pub const SeekError = error{ Unseekable, Unexpected, }; /// Repositions read/write file offset relative to the beginning. pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void { if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, offset, &result, SEEK_SET))) { 0 => return, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (windows.is_the_target) { return windows.SetFilePointerEx_BEGIN(fd, offset); } const ipos = @bitCast(i64, offset); // the OS treats this as unsigned switch (errno(system.lseek(fd, ipos, SEEK_SET))) { 0 => return, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } /// Repositions read/write file offset relative to the current offset. pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void { if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_CUR))) { 0 => return, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (windows.is_the_target) { return windows.SetFilePointerEx_CURRENT(fd, offset); } switch (errno(system.lseek(fd, offset, SEEK_CUR))) { 0 => return, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } /// Repositions read/write file offset relative to the end. pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void { if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_END))) { 0 => return, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (windows.is_the_target) { return windows.SetFilePointerEx_END(fd, offset); } switch (errno(system.lseek(fd, offset, SEEK_END))) { 0 => return, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } /// Returns the read/write file offset relative to the beginning. pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 { if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, 0, &result, SEEK_CUR))) { 0 => return result, EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (windows.is_the_target) { return windows.SetFilePointerEx_CURRENT_get(fd); } const rc = system.lseek(fd, 0, SEEK_CUR); switch (errno(rc)) { 0 => return @bitCast(u64, rc), EBADF => unreachable, // always a race condition EINVAL => return error.Unseekable, EOVERFLOW => return error.Unseekable, ESPIPE => return error.Unseekable, ENXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } pub const RealPathError = error{ FileNotFound, AccessDenied, NameTooLong, NotSupported, NotDir, SymLinkLoop, InputOutput, FileTooBig, IsDir, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, NoDevice, SystemResources, NoSpaceLeft, FileSystem, BadPathName, DeviceBusy, SharingViolation, PipeBusy, /// On Windows, file paths must be valid Unicode. InvalidUtf8, PathAlreadyExists, Unexpected, }; /// Return the canonicalized absolute pathname. /// Expands all symbolic links and resolves references to `.`, `..`, and /// extra `/` characters in `pathname`. /// The return value is a slice of `out_buffer`, but not necessarily from the beginning. /// See also `realpathC` and `realpathW`. pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { if (windows.is_the_target) { const pathname_w = try windows.sliceToPrefixedFileW(pathname); return realpathW(&pathname_w, out_buffer); } const pathname_c = try toPosixPath(pathname); return realpathC(&pathname_c, out_buffer); } /// Same as `realpath` except `pathname` is null-terminated. pub fn realpathC(pathname: [*]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { if (windows.is_the_target) { const pathname_w = try windows.cStrToPrefixedFileW(pathname); return realpathW(&pathname_w, out_buffer); } if (linux.is_the_target and !builtin.link_libc) { const fd = try openC(pathname, linux.O_PATH | linux.O_NONBLOCK | linux.O_CLOEXEC, 0); defer close(fd); var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined; const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{}\x00", fd) catch unreachable; return readlinkC(proc_path.ptr, out_buffer); } const result_path = std.c.realpath(pathname, out_buffer) orelse switch (std.c._errno().*) { EINVAL => unreachable, EBADF => unreachable, EFAULT => unreachable, EACCES => return error.AccessDenied, ENOENT => return error.FileNotFound, ENOTSUP => return error.NotSupported, ENOTDIR => return error.NotDir, ENAMETOOLONG => return error.NameTooLong, ELOOP => return error.SymLinkLoop, EIO => return error.InputOutput, else => |err| return unexpectedErrno(@intCast(usize, err)), }; return mem.toSlice(u8, result_path); } /// Same as `realpath` except `pathname` is null-terminated and UTF16LE-encoded. pub fn realpathW(pathname: [*]const u16, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { const h_file = try windows.CreateFileW( pathname, windows.GENERIC_READ, windows.FILE_SHARE_READ, null, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null, ); defer windows.CloseHandle(h_file); var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined; const wide_len = try windows.GetFinalPathNameByHandleW(h_file, &wide_buf, wide_buf.len, windows.VOLUME_NAME_DOS); assert(wide_len <= wide_buf.len); const wide_slice = wide_buf[0..wide_len]; // Windows returns \\?\ prepended to the path. // We strip it to make this function consistent across platforms. const prefix = [_]u16{ '\\', '\\', '?', '\\' }; const start_index = if (mem.startsWith(u16, wide_slice, prefix)) prefix.len else 0; // Trust that Windows gives us valid UTF-16LE. const end_index = std.unicode.utf16leToUtf8(out_buffer, wide_slice[start_index..]) catch unreachable; return out_buffer[0..end_index]; } /// Spurious wakeups are possible and no precision of timing is guaranteed. pub fn nanosleep(seconds: u64, nanoseconds: u64) void { var req = timespec{ .tv_sec = math.cast(isize, seconds) catch math.maxInt(isize), .tv_nsec = math.cast(isize, nanoseconds) catch math.maxInt(isize), }; var rem: timespec = undefined; while (true) { switch (errno(system.nanosleep(&req, &rem))) { EFAULT => unreachable, EINVAL => { // Sometimes Darwin returns EINVAL for no reason. // We treat it as a spurious wakeup. return; }, EINTR => { req = rem; continue; }, // This prong handles success as well as unexpected errors. else => return, } } } pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { // This is implemented only for systems using ELF executables if (windows.is_the_target or builtin.os == .uefi or wasi.is_the_target or darwin.is_the_target) @compileError("dl_iterate_phdr is not available for this target"); if (builtin.link_libc) { return system.dl_iterate_phdr( @ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data), ); } const elf_base = std.process.getBaseAddress(); const ehdr = @intToPtr(*elf.Ehdr, elf_base); // Make sure the base address points to an ELF image assert(mem.eql(u8, ehdr.e_ident[0..4], "\x7fELF")); const n_phdr = ehdr.e_phnum; const phdrs = (@intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff))[0..n_phdr]; var it = dl.linkmap_iterator(phdrs) catch unreachable; // The executable has no dynamic link segment, create a single entry for // the whole ELF image if (it.end()) { var info = dl_phdr_info{ .dlpi_addr = elf_base, .dlpi_name = c"/proc/self/exe", .dlpi_phdr = phdrs.ptr, .dlpi_phnum = ehdr.e_phnum, }; return callback(&info, @sizeOf(dl_phdr_info), data); } // Last return value from the callback function var last_r: isize = 0; while (it.next()) |entry| { var dlpi_phdr: [*]elf.Phdr = undefined; var dlpi_phnum: u16 = undefined; if (entry.l_addr != 0) { const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); dlpi_phdr = @intToPtr([*]elf.Phdr, entry.l_addr + elf_header.e_phoff); dlpi_phnum = elf_header.e_phnum; } else { // This is the running ELF image dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff); dlpi_phnum = ehdr.e_phnum; } var info = dl_phdr_info{ .dlpi_addr = entry.l_addr, .dlpi_name = entry.l_name, .dlpi_phdr = dlpi_phdr, .dlpi_phnum = dlpi_phnum, }; last_r = callback(&info, @sizeOf(dl_phdr_info), data); if (last_r != 0) break; } return last_r; } pub const ClockGetTimeError = error{ UnsupportedClock, Unexpected, }; pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { switch (errno(system.clock_gettime(clk_id, tp))) { 0 => return, EFAULT => unreachable, EINVAL => return error.UnsupportedClock, else => |err| return unexpectedErrno(err), } } pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { switch (errno(system.clock_getres(clk_id, res))) { 0 => return, EFAULT => unreachable, EINVAL => return error.UnsupportedClock, else => |err| return unexpectedErrno(err), } } pub const SchedGetAffinityError = error{ PermissionDenied, Unexpected, }; pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { var set: cpu_set_t = undefined; switch (errno(system.sched_getaffinity(pid, @sizeOf(cpu_set_t), &set))) { 0 => return set, EFAULT => unreachable, EINVAL => unreachable, ESRCH => unreachable, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 { var path_with_null: [PATH_MAX]u8 = undefined; // >= rather than > to make room for the null byte if (file_path.len >= PATH_MAX) return error.NameTooLong; mem.copy(u8, &path_with_null, file_path); path_with_null[file_path.len] = 0; return path_with_null; } /// Whether or not error.Unexpected will print its value and a stack trace. /// if this happens the fix is to add the error code to the corresponding /// switch expression, possibly introduce a new error in the error set, and /// send a patch to Zig. pub const unexpected_error_tracing = builtin.mode == .Debug; pub const UnexpectedError = error{ /// The Operating System returned an undocumented error code. /// This error is in theory not possible, but it would be better /// to handle this error than to invoke undefined behavior. Unexpected, }; /// Call this when you made a syscall or something that sets errno /// and you get an unexpected error. pub fn unexpectedErrno(err: usize) UnexpectedError { if (unexpected_error_tracing) { std.debug.warn("unexpected errno: {}\n", err); std.debug.dumpCurrentStackTrace(null); } return error.Unexpected; } pub const SigaltstackError = error{ /// The supplied stack size was less than MINSIGSTKSZ. SizeTooSmall, /// Attempted to change the signal stack while it was active. PermissionDenied, Unexpected, }; pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void { if (windows.is_the_target or uefi.is_the_target or wasi.is_the_target) @compileError("std.os.sigaltstack not available for this target"); switch (errno(system.sigaltstack(ss, old_ss))) { 0 => return, EFAULT => unreachable, EINVAL => unreachable, ENOMEM => return error.SizeTooSmall, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } /// Examine and change a signal action. pub fn sigaction(sig: u6, act: *const Sigaction, oact: ?*Sigaction) void { switch (errno(system.sigaction(sig, act, oact))) { 0 => return, EFAULT => unreachable, EINVAL => unreachable, else => unreachable, } } pub const FutimensError = error{ /// times is NULL, or both tv_nsec values are UTIME_NOW, and either: /// * the effective user ID of the caller does not match the owner /// of the file, the caller does not have write access to the /// file, and the caller is not privileged (Linux: does not have /// either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability); /// or, /// * the file is marked immutable (see chattr(1)). AccessDenied, /// The caller attempted to change one or both timestamps to a value /// other than the current time, or to change one of the timestamps /// to the current time while leaving the other timestamp unchanged, /// (i.e., times is not NULL, neither tv_nsec field is UTIME_NOW, /// and neither tv_nsec field is UTIME_OMIT) and either: /// * the caller's effective user ID does not match the owner of /// file, and the caller is not privileged (Linux: does not have /// the CAP_FOWNER capability); or, /// * the file is marked append-only or immutable (see chattr(1)). PermissionDenied, ReadOnlyFileSystem, Unexpected, }; pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void { switch (errno(system.futimens(fd, times))) { 0 => return, EACCES => return error.AccessDenied, EPERM => return error.PermissionDenied, EBADF => unreachable, // always a race condition EFAULT => unreachable, EINVAL => unreachable, EROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub const GetHostNameError = error{ PermissionDenied, Unexpected, }; pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { if (builtin.link_libc) { switch (errno(system.gethostname(name_buffer, name_buffer.len))) { 0 => return mem.toSlice(u8, name_buffer), EFAULT => unreachable, ENAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } if (linux.is_the_target) { var uts: utsname = undefined; switch (errno(system.uname(&uts))) { 0 => { const hostname = mem.toSlice(u8, &uts.nodename); mem.copy(u8, name_buffer, hostname); return name_buffer[0..hostname.len]; }, EFAULT => unreachable, EPERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } @compileError("TODO implement gethostname for this OS"); } test "" { _ = @import("os/darwin.zig"); _ = @import("os/freebsd.zig"); _ = @import("os/linux.zig"); _ = @import("os/netbsd.zig"); _ = @import("os/uefi.zig"); _ = @import("os/wasi.zig"); _ = @import("os/windows.zig"); _ = @import("os/zen.zig"); _ = @import("os/test.zig"); }
const assertOrPanic = @import("std").debug.assertOrPanic; test "switch with numbers" { testSwitchWithNumbers(13); } fn testSwitchWithNumbers(x: u32) void { const result = switch (x) { 1, 2, 3, 4...8 => false, 13 => true, else => false, }; assertOrPanic(result); } test "switch with all ranges" { assertOrPanic(testSwitchWithAllRanges(50, 3) == 1); assertOrPanic(testSwitchWithAllRanges(101, 0) == 2); assertOrPanic(testSwitchWithAllRanges(300, 5) == 3); assertOrPanic(testSwitchWithAllRanges(301, 6) == 6); } fn testSwitchWithAllRanges(x: u32, y: u32) u32 { return switch (x) { 0...100 => 1, 101...200 => 2, 201...300 => 3, else => y, }; } test "implicit comptime switch" { const x = 3 + 4; const result = switch (x) { 3 => 10, 4 => 11, 5, 6 => 12, 7, 8 => 13, else => 14, }; comptime { assertOrPanic(result + 1 == 14); } } test "switch on enum" { const fruit = Fruit.Orange; nonConstSwitchOnEnum(fruit); } const Fruit = enum { Apple, Orange, Banana, }; fn nonConstSwitchOnEnum(fruit: Fruit) void { switch (fruit) { Fruit.Apple => unreachable, Fruit.Orange => {}, Fruit.Banana => unreachable, } } test "switch statement" { nonConstSwitch(SwitchStatmentFoo.C); } fn nonConstSwitch(foo: SwitchStatmentFoo) void { const val = switch (foo) { SwitchStatmentFoo.A => i32(1), SwitchStatmentFoo.B => 2, SwitchStatmentFoo.C => 3, SwitchStatmentFoo.D => 4, }; assertOrPanic(val == 3); } const SwitchStatmentFoo = enum { A, B, C, D, }; test "switch prong with variable" { switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 }); switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 }); switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} }); } const SwitchProngWithVarEnum = union(enum) { One: i32, Two: f32, Meh: void, }; fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void { switch (a) { SwitchProngWithVarEnum.One => |x| { assertOrPanic(x == 13); }, SwitchProngWithVarEnum.Two => |x| { assertOrPanic(x == 13.0); }, SwitchProngWithVarEnum.Meh => |x| { const v: void = x; }, } } test "switch on enum using pointer capture" { testSwitchEnumPtrCapture(); comptime testSwitchEnumPtrCapture(); } fn testSwitchEnumPtrCapture() void { var value = SwitchProngWithVarEnum{ .One = 1234 }; switch (value) { SwitchProngWithVarEnum.One => |*x| x.* += 1, else => unreachable, } switch (value) { SwitchProngWithVarEnum.One => |x| assertOrPanic(x == 1235), else => unreachable, } } test "switch with multiple expressions" { const x = switch (returnsFive()) { 1, 2, 3 => 1, 4, 5, 6 => 2, else => i32(3), }; assertOrPanic(x == 2); } fn returnsFive() i32 { return 5; } const Number = union(enum) { One: u64, Two: u8, Three: f32, }; const number = Number{ .Three = 1.23 }; fn returnsFalse() bool { switch (number) { Number.One => |x| return x > 1234, Number.Two => |x| return x == 'a', Number.Three => |x| return x > 12.34, } } test "switch on const enum with var" { assertOrPanic(!returnsFalse()); } test "switch on type" { assertOrPanic(trueIfBoolFalseOtherwise(bool)); assertOrPanic(!trueIfBoolFalseOtherwise(i32)); } fn trueIfBoolFalseOtherwise(comptime T: type) bool { return switch (T) { bool => true, else => false, }; } test "switch handles all cases of number" { testSwitchHandleAllCases(); comptime testSwitchHandleAllCases(); } fn testSwitchHandleAllCases() void { assertOrPanic(testSwitchHandleAllCasesExhaustive(0) == 3); assertOrPanic(testSwitchHandleAllCasesExhaustive(1) == 2); assertOrPanic(testSwitchHandleAllCasesExhaustive(2) == 1); assertOrPanic(testSwitchHandleAllCasesExhaustive(3) == 0); assertOrPanic(testSwitchHandleAllCasesRange(100) == 0); assertOrPanic(testSwitchHandleAllCasesRange(200) == 1); assertOrPanic(testSwitchHandleAllCasesRange(201) == 2); assertOrPanic(testSwitchHandleAllCasesRange(202) == 4); assertOrPanic(testSwitchHandleAllCasesRange(230) == 3); } fn testSwitchHandleAllCasesExhaustive(x: u2) u2 { return switch (x) { 0 => u2(3), 1 => 2, 2 => 1, 3 => 0, }; } fn testSwitchHandleAllCasesRange(x: u8) u8 { return switch (x) { 0...100 => u8(0), 101...200 => 1, 201, 203 => 2, 202 => 4, 204...255 => 3, }; } test "switch all prongs unreachable" { testAllProngsUnreachable(); comptime testAllProngsUnreachable(); } fn testAllProngsUnreachable() void { assertOrPanic(switchWithUnreachable(1) == 2); assertOrPanic(switchWithUnreachable(2) == 10); } fn switchWithUnreachable(x: i32) i32 { while (true) { switch (x) { 1 => return 2, 2 => break, else => continue, } } return 10; } fn return_a_number() anyerror!i32 { return 1; } test "capture value of switch with all unreachable prongs" { const x = return_a_number() catch |err| switch (err) { else => unreachable, }; assertOrPanic(x == 1); } test "switching on booleans" { testSwitchOnBools(); comptime testSwitchOnBools(); } fn testSwitchOnBools() void { assertOrPanic(testSwitchOnBoolsTrueAndFalse(true) == false); assertOrPanic(testSwitchOnBoolsTrueAndFalse(false) == true); assertOrPanic(testSwitchOnBoolsTrueWithElse(true) == false); assertOrPanic(testSwitchOnBoolsTrueWithElse(false) == true); assertOrPanic(testSwitchOnBoolsFalseWithElse(true) == false); assertOrPanic(testSwitchOnBoolsFalseWithElse(false) == true); } fn testSwitchOnBoolsTrueAndFalse(x: bool) bool { return switch (x) { true => false, false => true, }; } fn testSwitchOnBoolsTrueWithElse(x: bool) bool { return switch (x) { true => false, else => true, }; } fn testSwitchOnBoolsFalseWithElse(x: bool) bool { return switch (x) { false => true, else => false, }; }
const builtin = @import("builtin"); const std = @import("std"); const debug = std.debug; const testing = std.testing; const TypeInfo = builtin.TypeInfo; const TypeId = builtin.TypeId; pub fn Reify(comptime info: TypeInfo) type { switch (info) { .Type => return type, .Void => return void, .Bool => return bool, .Null => return @TypeOf(null), .Undefined => return @TypeOf(undefined), .NoReturn => return noreturn, .ComptimeInt => return comptime_int, .ComptimeFloat => return comptime_float, // TODO: Implement without using @Type .Int => |int| unreachable, .Float => |float| switch (float.bits) { 16 => return f16, 32 => return f32, 64 => return f64, 128 => return f128, else => @compileError("Float cannot be Reified with {TODO bits in error} bits"), }, .Pointer => |ptr| switch (ptr.size) { .One => { if (ptr.is_const and ptr.is_volatile) return *align(ptr.alignment) const volatile ptr.child; if (ptr.is_const) return *align(ptr.alignment) const ptr.child; if (ptr.is_volatile) return *align(ptr.alignment) volatile ptr.child; return *align(ptr.alignment) ptr.child; }, .Many => { if (ptr.is_const and ptr.is_volatile) return [*]align(ptr.alignment) const volatile ptr.child; if (ptr.is_const) return [*]align(ptr.alignment) const ptr.child; if (ptr.is_volatile) return [*]align(ptr.alignment) volatile ptr.child; return [*]align(ptr.alignment) ptr.child; }, .Slice => { if (ptr.is_const and ptr.is_volatile) return []align(ptr.alignment) const volatile ptr.child; if (ptr.is_const) return []align(ptr.alignment) const ptr.child; if (ptr.is_volatile) return []align(ptr.alignment) volatile ptr.child; return []align(ptr.alignment) ptr.child; }, .C => { if (ptr.is_const and ptr.is_volatile) return [*c]align(ptr.alignment) const volatile ptr.child; if (ptr.is_const) return [*c]align(ptr.alignment) const ptr.child; if (ptr.is_volatile) return [*c]align(ptr.alignment) volatile ptr.child; return [*c]align(ptr.alignment) ptr.child; }, }, .Array => |arr| return [arr.len]arr.child, .Struct => |str| @compileError("TODO"), .Optional => |opt| return ?opt.child, .ErrorUnion => |err_union| return err_union.error_set!err_union.payload, .ErrorSet => |err_set| { var Res = error{}; inline for (err_set.errors) |err| { Res = Res || @field(anyerror, err.name); } return Res; }, .Opaque => return @OpaqueType(), .AnyFrame => return anyframe, .Enum => |enu| @compileError("TODO"), .Union => |unio| @compileError("TODO"), .Fn => |func| @compileError("TODO"), .BoundFn => |func| @compileError("TODO"), .Frame => @compileError("TODO"), .Vector => @compileError("TODO"), .EnumLiteral => @compileError("TODO"), } } test "reify: type" { const T = Reify(@typeInfo(type)); testing.expectEqual(T, type); } test "reify: void" { const T = Reify(@typeInfo(void)); testing.expectEqual(T, void); } test "reify: bool" { const T = Reify(@typeInfo(bool)); testing.expectEqual(T, bool); } test "reify: noreturn" { const T = Reify(@typeInfo(noreturn)); testing.expectEqual(T, noreturn); } test "reify: ix/ux" { //@setEvalBranchQuota(10000); //inline for ([_]bool{ true, false }) |signed| { // comptime var i = 0; // inline while (i < 256) : (i += 1) { // const T1 = @IntType(signed, i); // const T2 = Reify(@typeInfo(T1)); // comptime testing.expectEqual(T1, T2); // } //} } test "reify: fx" { testing.expectEqual(Reify(@typeInfo(f16)), f16); // TODO: All these fail for some reason //testing.expectEqual(Reify(@typeInfo(f32)), f32); //testing.expectEqual(Reify(@typeInfo(f64)), f64); //testing.expectEqual(Reify(@typeInfo(f128)), f128); } test "reify: *X" { const types = [_]type{ *u8, *const u8, *volatile u8, *align(4) u8, *const volatile u8, *align(4) volatile u8, *align(4) const u8, *align(4) const volatile u8, }; inline for (types) |P| { const T = Reify(@typeInfo(P)); testing.expectEqual(T, P); } } test "reify: [*]X" { const types = [_]type{ [*]u8, [*]const u8, [*]volatile u8, [*]align(4) u8, [*]const volatile u8, [*]align(4) volatile u8, [*]align(4) const u8, [*]align(4) const volatile u8, }; inline for (types) |P| { const T = Reify(@typeInfo(P)); testing.expectEqual(T, P); } } test "reify: []X" { const types = [_]type{ []u8, []const u8, []volatile u8, []align(4) u8, []const volatile u8, []align(4) volatile u8, []align(4) const u8, []align(4) const volatile u8, }; inline for (types) |P| { const T = comptime Reify(@typeInfo(P)); testing.expectEqual(T, P); } } test "reify: [n]X" { testing.expectEqual([1]u8, Reify(@typeInfo([1]u8))); // TODO: This fails for some reason //testing.expectEqual([10]u8, Reify(@typeInfo([10]u8))); } test "reify: struct" { return error.SkipZigTest; } test "reify: ?X" { const T = Reify(@typeInfo(?u8)); testing.expectEqual(T, ?u8); } test "reify: X!Y" { const Set = error{}; const T = Reify(@typeInfo(Set!u8)); testing.expectEqual(T, Set!u8); } test "reify: error sets" { return error.SkipZigTest; } test "reify: enum" { return error.SkipZigTest; } test "reify: union" { return error.SkipZigTest; } test "reify: fn" { return error.SkipZigTest; } test "reify: boundfn" { return error.SkipZigTest; } test "reify: ..." { return error.SkipZigTest; } test "reify: @OpaqueType()" { const T = Reify(@typeInfo(@OpaqueType())); testing.expectEqual(@as(TypeId, @typeInfo(T)), .Opaque); } test "reify: anyframe" { const T = Reify(@typeInfo(anyframe)); testing.expectEqual(T, anyframe); } test "reify: @Frame" { return error.SkipZigTest; }
pub usingnamespace @import("pyxeledit.zig");
const std = @import("std"); const builtin = @import("builtin"); const testing = std.testing; const Allocator = std.mem.Allocator; // ustar tar implementation pub const Header = extern struct { name: [100]u8, mode: [7:0]u8, uid: [7:0]u8, gid: [7:0]u8, size: [11:0]u8, mtime: [11:0]u8, checksum: [7:0]u8, typeflag: FileType, linkname: [100]u8, magic: [5:0]u8, version: [2]u8, uname: [31:0]u8, gname: [31:0]u8, devmajor: [7:0]u8, devminor: [7:0]u8, prefix: [155]u8, pad: [12]u8, const Self = @This(); const FileType = enum(u8) { regular = '0', hard_link = '1', symbolic_link = '2', character = '3', block = '4', directory = '5', fifo = '6', reserved = '7', pax_global = 'g', extended = 'x', _, }; fn init() Self { var ret = std.mem.zeroes(Self); ret.magic = [_:0]u8{ 'u', 's', 't', 'a', 'r' }; ret.version = [_:0]u8{ '0', '0' }; return ret; } fn setPath(self: *Self, path: []const u8) !void { if (path.len > 100) { var i: usize = 0; while (i < path.len and path.len - i > 100) { while (path[i] != '/') : (i += 1) {} } _ = try std.fmt.bufPrint(&self.prefix, "{s}", .{path[0..i]}); _ = try std.fmt.bufPrint(&self.name, "{s}", .{path[i + 1 ..]}); } else { _ = try std.fmt.bufPrint(&self.name, "{s}", .{path}); } } fn setSize(self: *Self, size: u64) !void { _ = try std.fmt.bufPrint(&self.size, "{o:0>11}", .{size}); } fn setMtime(self: *Self, mtime: u32) !void { _ = try std.fmt.bufPrint(&self.mtime, "{o:0>11}", .{mtime}); } fn setMode(self: *Self, filetype: FileType, perm: u9) !void { switch (filetype) { .regular => _ = try std.fmt.bufPrint(&self.mode, "0100{o:0>3}", .{perm}), .directory => _ = try std.fmt.bufPrint(&self.mode, "0040{o:0>3}", .{perm}), else => return error.Unsupported, } } fn setUid(self: *Self, uid: u32) !void { _ = try std.fmt.bufPrint(&self.uid, "{o:0>7}", .{uid}); } fn setGid(self: *Self, gid: u32) !void { _ = try std.fmt.bufPrint(&self.gid, "{o:0>7}", .{gid}); } fn updateChecksum(self: *Self) !void { const offset = @offsetOf(Self, "checksum"); var checksum: usize = 0; for (std.mem.asBytes(self)) |val, i| { checksum += if (i >= offset and i < offset + @sizeOf(@TypeOf(self.checksum))) ' ' else val; } _ = try std.fmt.bufPrint(&self.checksum, "{o:0>7}", .{checksum}); } fn fromStat(stat: std.fs.File.Stat, path: []const u8) !Header { if (std.mem.indexOf(u8, path, "\\") != null) return error.NeedPosixPath; if (std.fs.path.isAbsolute(path)) return error.NeedRelPath; var ret = Self.init(); ret.typeflag = switch (stat.kind) { .File => .regular, .Directory => .directory, else => return error.UnsupportedType, }; try ret.setPath(path); try ret.setSize(stat.size); try ret.setMtime(@truncate(u32, @bitCast(u128, @divTrunc(stat.mtime, std.time.ns_per_s)))); try ret.setMode(ret.typeflag, @truncate(u9, stat.mode)); try ret.setUid(0); try ret.setGid(0); std.mem.copy(u8, &ret.uname, "root"); std.mem.copy(u8, &ret.gname, "root"); try ret.updateChecksum(); return ret; } pub fn isBlank(self: *const Header) bool { const block = std.mem.asBytes(self); return for (block) |elem| { if (elem != 0) break false; } else true; } }; test "Header size" { try testing.expectEqual(512, @sizeOf(Header)); } pub fn instantiate( allocator: Allocator, dir: std.fs.Dir, reader: anytype, skip_depth: usize, ) !void { var count: usize = 0; while (true) { const header = reader.readStruct(Header) catch |err| { return if (err == error.EndOfStream) if (count < 2) error.AbrubtEnd else break else err; }; if (header.isBlank()) { count += 1; continue; } else if (count > 0) { return error.Format; } var size = try std.fmt.parseUnsigned(usize, &header.size, 8); const block_size = ((size + 511) / 512) * 512; var components = std.ArrayList([]const u8).init(allocator); defer components.deinit(); var path_it = std.mem.tokenize(u8, &header.prefix, "/\x00"); if (header.prefix[0] != 0) { while (path_it.next()) |component| { try components.append(component); } } path_it = std.mem.tokenize(u8, &header.name, "/\x00"); while (path_it.next()) |component| { try components.append(component); } const tmp_path = try std.fs.path.join(allocator, components.items); defer allocator.free(tmp_path); if (skip_depth >= components.items.len) { try reader.skipBytes(block_size, .{}); continue; } var i: usize = 0; while (i < skip_depth) : (i += 1) { _ = components.orderedRemove(0); } const file_path = try std.fs.path.join(allocator, components.items); defer allocator.free(file_path); switch (header.typeflag) { .directory => try dir.makePath(file_path), .pax_global => try reader.skipBytes(512, .{}), .regular => { const file = try dir.createFile(file_path, .{ .read = true, .truncate = true }); defer file.close(); const skip_size = block_size - size; var buf: [std.mem.page_size]u8 = undefined; while (size > 0) { const buffered = try reader.read(buf[0..std.math.min(size, 512)]); try file.writeAll(buf[0..buffered]); size -= buffered; } try reader.skipBytes(skip_size, .{}); }, else => {}, } } } pub fn builder(allocator: Allocator, writer: anytype) Builder(@TypeOf(writer)) { return Builder(@TypeOf(writer)).init(allocator, writer); } pub fn Builder(comptime Writer: type) type { return struct { writer: Writer, arena: std.heap.ArenaAllocator, directories: std.StringHashMap(void), const Self = @This(); pub fn init(allocator: Allocator, writer: Writer) Self { return Self{ .arena = std.heap.ArenaAllocator.init(allocator), .writer = writer, .directories = std.StringHashMap(void).init(allocator), }; } pub fn deinit(self: *Self) void { self.directories.deinit(); self.arena.deinit(); } pub fn finish(self: *Self) !void { try self.writer.writeByteNTimes(0, 1024); } fn maybeAddDirectories( self: *Self, path: []const u8, ) !void { var i: usize = 0; while (i < path.len) : (i += 1) { while (path[i] != '/' and i < path.len) i += 1; if (i >= path.len) break; const dirpath = try self.arena.allocator().dupe(u8, path[0..i]); if (self.directories.contains(dirpath)) continue else try self.directories.put(dirpath, {}); const stat = std.fs.File.Stat{ .inode = undefined, .size = 0, .mode = switch (builtin.os.tag) { .windows => 0, else => 0o755, }, .kind = .Directory, .atime = undefined, .mtime = std.time.nanoTimestamp(), .ctime = undefined, }; const allocator = self.arena.child_allocator; const posix_dirpath = try std.mem.replaceOwned(u8, allocator, dirpath, std.fs.path.sep_str_windows, std.fs.path.sep_str_posix); defer allocator.free(posix_dirpath); const header = try Header.fromStat(stat, posix_dirpath); try self.writer.writeAll(std.mem.asBytes(&header)); } } /// prefix is a path to prepend subpath with pub fn addFile( self: *Self, root: std.fs.Dir, prefix: ?[]const u8, subpath: []const u8, ) !void { const allocator = self.arena.child_allocator; const path = if (prefix) |prefix_path| try std.fs.path.join(allocator, &[_][]const u8{ prefix_path, subpath }) else subpath; defer if (prefix != null) allocator.free(path); const posix_path = try std.mem.replaceOwned(u8, allocator, path, std.fs.path.sep_str_windows, std.fs.path.sep_str_posix); defer allocator.free(posix_path); if (std.fs.path.dirname(posix_path)) |dirname| try self.maybeAddDirectories(posix_path[0 .. dirname.len + 1]); const subfile = try root.openFile(subpath, .{ .read = true, .write = true }); defer subfile.close(); const stat = try subfile.stat(); const header = try Header.fromStat(stat, posix_path); var buf: [std.mem.page_size]u8 = undefined; try self.writer.writeAll(std.mem.asBytes(&header)); var counter = std.io.countingWriter(self.writer); while (true) { const n = try subfile.reader().read(&buf); if (n == 0) break; try counter.writer().writeAll(buf[0..n]); } const padding = blk: { const mod = counter.bytes_written % 512; break :blk if (mod > 0) 512 - mod else 0; }; try self.writer.writeByteNTimes(0, @intCast(usize, padding)); } /// add slice of bytes as file `path` pub fn addSlice(self: *Self, slice: []const u8, path: []const u8) !void { const allocator = self.arena.child_allocator; const posix_path = try std.mem.replaceOwned(u8, allocator, path, std.fs.path.sep_str_windows, std.fs.path.sep_str_posix); defer allocator.free(posix_path); const stat = std.fs.File.Stat{ .inode = undefined, .size = slice.len, .mode = switch (builtin.os.tag) { .windows => 0, else => 0o644, }, .kind = .File, .atime = undefined, .mtime = std.time.nanoTimestamp(), .ctime = undefined, }; var header = try Header.fromStat(stat, posix_path); const padding = blk: { const mod = slice.len % 512; break :blk if (mod > 0) 512 - mod else 0; }; try self.writer.writeAll(std.mem.asBytes(&header)); try self.writer.writeAll(slice); try self.writer.writeByteNTimes(0, padding); } }; } pub const PaxHeaderMap = struct { text: []const u8, map: std.StringHashMap([]const u8), const Self = @This(); pub fn init(allocator: Allocator, reader: anytype) !Self { // TODO: header verification const header = try reader.readStruct(Header); if (header.typeflag != .pax_global) return error.NotPaxGlobalHeader; const size = try std.fmt.parseInt(usize, &header.size, 8); const text = try allocator.alloc(u8, size); errdefer allocator.free(text); var i: usize = 0; while (i < size) : (i = try reader.read(text[i..])) {} var map = std.StringHashMap([]const u8).init(allocator); errdefer map.deinit(); var it = std.mem.tokenize(u8, text, "\n"); while (it.next()) |line| { const begin = (std.mem.indexOf(u8, line, " ") orelse return error.BadMapEntry) + 1; const eql = std.mem.indexOf(u8, line[begin..], "=") orelse return error.BadMapEntry; try map.put(line[begin .. begin + eql], line[begin + eql + 1 ..]); } return Self{ .text = text, .map = map, }; } pub fn get(self: Self, key: []const u8) ?[]const u8 { return self.map.get(key); } pub fn deinit(self: *Self) void { self.map.allocator.free(self.text); self.map.deinit(); } }; pub fn fileExtractor(path: []const u8, reader: anytype) FileExtractor(@TypeOf(reader)) { return FileExtractor(@TypeOf(reader)).init(path, reader); } pub fn FileExtractor(comptime ReaderType: type) type { return struct { path: []const u8, internal: ReaderType, len: ?usize, const Self = @This(); pub fn init(path: []const u8, internal: ReaderType) Self { return Self{ .path = path, .internal = internal, .len = null, }; } pub const Error = ReaderType.Error || error{ FileNotFound, EndOfStream } || std.fmt.ParseIntError; pub const Reader = std.io.Reader(*Self, Error, read); pub fn read(self: *Self, buf: []u8) Error!usize { if (self.len == null) { while (true) { const header = try self.internal.readStruct(Header); for (std.mem.asBytes(&header)) |c| { if (c != 0) break; } else return error.FileNotFound; const size = try std.fmt.parseInt(usize, &header.size, 8); const name = header.name[0 .. std.mem.indexOf(u8, &header.name, "\x00") orelse header.name.len]; if (std.mem.eql(u8, name, self.path)) { self.len = size; break; } else if (size > 0) { try self.internal.skipBytes(size + (512 - (size % 512)), .{}); } } } const n = try self.internal.read(buf[0..std.math.min(self.len.?, buf.len)]); self.len.? -= n; return n; } pub fn reader(self: *Self) Reader { return .{ .context = self }; } }; }
// This is the userland implementation of translate-c which will be used by both stage1 // and stage2. Currently the only way it is used is with `zig translate-c-2`. const std = @import("std"); const assert = std.debug.assert; const ast = std.zig.ast; const Token = std.zig.Token; usingnamespace @import("clang.zig"); const ctok = @import("c_tokenizer.zig"); const CToken = ctok.CToken; const mem = std.mem; const math = std.math; const CallingConvention = std.builtin.TypeInfo.CallingConvention; pub const ClangErrMsg = Stage2ErrorMsg; pub const Error = error{OutOfMemory}; const TypeError = Error || error{UnsupportedType}; const TransError = TypeError || error{UnsupportedTranslation}; const DeclTable = std.HashMap(usize, []const u8, addrHash, addrEql); fn addrHash(x: usize) u32 { switch (@typeInfo(usize).Int.bits) { 32 => return x, // pointers are usually aligned so we ignore the bits that are probably all 0 anyway // usually the larger bits of addr space are unused so we just chop em off 64 => return @truncate(u32, x >> 4), else => @compileError("unreachable"), } } fn addrEql(a: usize, b: usize) bool { return a == b; } const SymbolTable = std.StringHashMap(*ast.Node); const AliasList = std.SegmentedList(struct { alias: []const u8, name: []const u8, }, 4); const Scope = struct { id: Id, parent: ?*Scope, const Id = enum { Switch, Block, Root, Condition, Loop, }; const Switch = struct { base: Scope, pending_block: *ast.Node.Block, cases: *ast.Node.Switch.CaseList, has_default: bool = false, }; const Block = struct { base: Scope, block_node: *ast.Node.Block, variables: AliasList, label: ?[]const u8, mangle_count: u32 = 0, /// Don't forget to set rbrace token and block_node later fn init(c: *Context, parent: *Scope, label: ?[]const u8) !*Block { const block = try c.a().create(Block); block.* = .{ .base = .{ .id = .Block, .parent = parent, }, .block_node = undefined, .variables = AliasList.init(c.a()), .label = label, }; return block; } /// Given the desired name, return a name that does not shadow anything from outer scopes. /// Inserts the returned name into the scope. fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { var proposed_name = name; while (scope.contains(proposed_name)) { scope.mangle_count += 1; proposed_name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ name, scope.mangle_count }); } try scope.variables.push(.{ .name = name, .alias = proposed_name }); return proposed_name; } fn getAlias(scope: *Block, name: []const u8) []const u8 { var it = scope.variables.iterator(0); while (it.next()) |p| { if (mem.eql(u8, p.name, name)) return p.alias; } return scope.base.parent.?.getAlias(name); } fn localContains(scope: *Block, name: []const u8) bool { var it = scope.variables.iterator(0); while (it.next()) |p| { if (mem.eql(u8, p.name, name)) return true; } return false; } fn contains(scope: *Block, name: []const u8) bool { if (scope.localContains(name)) return true; return scope.base.parent.?.contains(name); } }; const Root = struct { base: Scope, sym_table: SymbolTable, macro_table: SymbolTable, context: *Context, fn init(c: *Context) Root { return .{ .base = .{ .id = .Root, .parent = null, }, .sym_table = SymbolTable.init(c.a()), .macro_table = SymbolTable.init(c.a()), .context = c, }; } /// Check if the global scope contains this name, without looking into the "future", e.g. /// ignore the preprocessed decl and macro names. fn containsNow(scope: *Root, name: []const u8) bool { return isZigPrimitiveType(name) or scope.sym_table.contains(name) or scope.macro_table.contains(name); } /// Check if the global scope contains the name, includes all decls that haven't been translated yet. fn contains(scope: *Root, name: []const u8) bool { return scope.containsNow(name) or scope.context.global_names.contains(name); } }; fn findBlockScope(inner: *Scope, c: *Context) !*Scope.Block { var scope = inner; while (true) { switch (scope.id) { .Root => unreachable, .Block => return @fieldParentPtr(Block, "base", scope), .Condition => { // comma operator used return try Block.init(c, scope, "blk"); }, else => scope = scope.parent.?, } } } fn getAlias(scope: *Scope, name: []const u8) []const u8 { return switch (scope.id) { .Root => return name, .Block => @fieldParentPtr(Block, "base", scope).getAlias(name), .Switch, .Loop, .Condition => scope.parent.?.getAlias(name), }; } fn contains(scope: *Scope, name: []const u8) bool { return switch (scope.id) { .Root => @fieldParentPtr(Root, "base", scope).contains(name), .Block => @fieldParentPtr(Block, "base", scope).contains(name), .Switch, .Loop, .Condition => scope.parent.?.contains(name), }; } fn getBreakableScope(inner: *Scope) *Scope { var scope = inner; while (true) { switch (scope.id) { .Root => unreachable, .Switch => return scope, .Loop => return scope, else => scope = scope.parent.?, } } } fn getSwitch(inner: *Scope) *Scope.Switch { var scope = inner; while (true) { switch (scope.id) { .Root => unreachable, .Switch => return @fieldParentPtr(Switch, "base", scope), else => scope = scope.parent.?, } } } }; pub const Context = struct { tree: *ast.Tree, source_buffer: *std.Buffer, err: Error, source_manager: *ZigClangSourceManager, decl_table: DeclTable, alias_list: AliasList, global_scope: *Scope.Root, clang_context: *ZigClangASTContext, mangle_count: u32 = 0, /// This one is different than the root scope's name table. This contains /// a list of names that we found by visiting all the top level decls without /// translating them. The other maps are updated as we translate; this one is updated /// up front in a pre-processing step. global_names: std.StringHashMap(void), fn getMangle(c: *Context) u32 { c.mangle_count += 1; return c.mangle_count; } fn a(c: *Context) *mem.Allocator { return &c.tree.arena_allocator.allocator; } /// Convert a null-terminated C string to a slice allocated in the arena fn str(c: *Context, s: [*:0]const u8) ![]u8 { return mem.dupe(c.a(), u8, mem.toSliceConst(u8, s)); } /// Convert a clang source location to a file:line:column string fn locStr(c: *Context, loc: ZigClangSourceLocation) ![]u8 { const spelling_loc = ZigClangSourceManager_getSpellingLoc(c.source_manager, loc); const filename_c = ZigClangSourceManager_getFilename(c.source_manager, spelling_loc); const filename = if (filename_c) |s| try c.str(s) else @as([]const u8, "(no file)"); const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc); const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc); return std.fmt.allocPrint(c.a(), "{}:{}:{}", .{ filename, line, column }); } }; pub fn translate( backing_allocator: *mem.Allocator, args_begin: [*]?[*]const u8, args_end: [*]?[*]const u8, errors: *[]ClangErrMsg, resources_path: [*:0]const u8, ) !*ast.Tree { const ast_unit = ZigClangLoadFromCommandLine( args_begin, args_end, &errors.ptr, &errors.len, resources_path, ) orelse { if (errors.len == 0) return error.OutOfMemory; return error.SemanticAnalyzeFail; }; defer ZigClangASTUnit_delete(ast_unit); const tree = blk: { var tree_arena = std.heap.ArenaAllocator.init(backing_allocator); errdefer tree_arena.deinit(); const tree = try tree_arena.allocator.create(ast.Tree); tree.* = ast.Tree{ .source = undefined, // need to use Buffer.toOwnedSlice later .root_node = undefined, .arena_allocator = tree_arena, .tokens = undefined, // can't reference the allocator yet .errors = undefined, // can't reference the allocator yet .generated = true, }; break :blk tree; }; const arena = &tree.arena_allocator.allocator; // now we can reference the allocator errdefer tree.arena_allocator.deinit(); tree.tokens = ast.Tree.TokenList.init(arena); tree.errors = ast.Tree.ErrorList.init(arena); tree.root_node = try arena.create(ast.Node.Root); tree.root_node.* = ast.Node.Root{ .base = ast.Node{ .id = ast.Node.Id.Root }, .decls = ast.Node.Root.DeclList.init(arena), // initialized with the eof token at the end .eof_token = undefined, }; var source_buffer = try std.Buffer.initSize(arena, 0); var context = Context{ .tree = tree, .source_buffer = &source_buffer, .source_manager = ZigClangASTUnit_getSourceManager(ast_unit), .err = undefined, .decl_table = DeclTable.init(arena), .alias_list = AliasList.init(arena), .global_scope = try arena.create(Scope.Root), .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?, .global_names = std.StringHashMap(void).init(arena), }; context.global_scope.* = Scope.Root.init(&context); try prepopulateGlobalNameTable(ast_unit, &context); if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, &context, declVisitorC)) { return context.err; } try transPreprocessorEntities(&context, ast_unit); try addMacros(&context); var it = context.alias_list.iterator(0); while (it.next()) |alias| { if (!context.global_scope.sym_table.contains(alias.alias)) { try createAlias(&context, alias); } } tree.root_node.eof_token = try appendToken(&context, .Eof, ""); tree.source = source_buffer.toOwnedSlice(); if (false) { std.debug.warn("debug source:\n{}\n==EOF==\ntokens:\n", tree.source); var i: usize = 0; while (i < tree.tokens.len) : (i += 1) { const token = tree.tokens.at(i); std.debug.warn("{}\n", token); } } return tree; } fn prepopulateGlobalNameTable(ast_unit: *ZigClangASTUnit, c: *Context) !void { if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, c, declVisitorNamesOnlyC)) { return c.err; } // TODO if we see #undef, delete it from the table var it = ZigClangASTUnit_getLocalPreprocessingEntities_begin(ast_unit); const it_end = ZigClangASTUnit_getLocalPreprocessingEntities_end(ast_unit); while (it.I != it_end.I) : (it.I += 1) { const entity = ZigClangPreprocessingRecord_iterator_deref(it); switch (ZigClangPreprocessedEntity_getKind(entity)) { .MacroDefinitionKind => { const macro = @ptrCast(*ZigClangMacroDefinitionRecord, entity); const raw_name = ZigClangMacroDefinitionRecord_getName_getNameStart(macro); const name = try c.str(raw_name); _ = try c.global_names.put(name, {}); }, else => {}, } } } extern fn declVisitorNamesOnlyC(context: ?*c_void, decl: *const ZigClangDecl) bool { const c = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); declVisitorNamesOnly(c, decl) catch |err| { c.err = err; return false; }; return true; } extern fn declVisitorC(context: ?*c_void, decl: *const ZigClangDecl) bool { const c = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); declVisitor(c, decl) catch |err| { c.err = err; return false; }; return true; } fn declVisitorNamesOnly(c: *Context, decl: *const ZigClangDecl) Error!void { const decl_name = try c.str(ZigClangDecl_getName_bytes_begin(decl)); _ = try c.global_names.put(decl_name, {}); } fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { switch (ZigClangDecl_getKind(decl)) { .Function => { return visitFnDecl(c, @ptrCast(*const ZigClangFunctionDecl, decl)); }, .Typedef => { _ = try transTypeDef(c, @ptrCast(*const ZigClangTypedefNameDecl, decl)); }, .Enum => { _ = try transEnumDecl(c, @ptrCast(*const ZigClangEnumDecl, decl)); }, .Record => { _ = try transRecordDecl(c, @ptrCast(*const ZigClangRecordDecl, decl)); }, .Var => { return visitVarDecl(c, @ptrCast(*const ZigClangVarDecl, decl)); }, else => { const decl_name = try c.str(ZigClangDecl_getDeclKindName(decl)); try emitWarning(c, ZigClangDecl_getLocation(decl), "ignoring {} declaration", .{decl_name}); }, } } fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl))); if (c.global_scope.sym_table.contains(fn_name)) return; // Avoid processing this decl twice const rp = makeRestorePoint(c); const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl); const has_body = ZigClangFunctionDecl_hasBody(fn_decl); const storage_class = ZigClangFunctionDecl_getStorageClass(fn_decl); const decl_ctx = FnDeclContext{ .fn_name = fn_name, .has_body = has_body, .storage_class = storage_class, .is_export = switch (storage_class) { .None => has_body and !ZigClangFunctionDecl_isInlineSpecified(fn_decl), .Extern, .Static => false, .PrivateExtern => return failDecl(c, fn_decl_loc, fn_name, "unsupported storage class: private extern", .{}), .Auto => unreachable, // Not legal on functions .Register => unreachable, // Not legal on functions else => unreachable, }, }; var fn_qt = ZigClangFunctionDecl_getType(fn_decl); var fn_type = ZigClangQualType_getTypePtr(fn_qt); if (ZigClangType_getTypeClass(fn_type) == .Attributed) { const attr_type = @ptrCast(*const ZigClangAttributedType, fn_type); fn_qt = ZigClangAttributedType_getEquivalentType(attr_type); fn_type = ZigClangQualType_getTypePtr(fn_qt); } const proto_node = switch (ZigClangType_getTypeClass(fn_type)) { .FunctionProto => blk: { const fn_proto_type = @ptrCast(*const ZigClangFunctionProtoType, fn_type); break :blk transFnProto(rp, fn_decl, fn_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{}); }, error.OutOfMemory => |e| return e, }; }, .FunctionNoProto => blk: { const fn_no_proto_type = @ptrCast(*const ZigClangFunctionType, fn_type); break :blk transFnNoProto(rp, fn_no_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{}); }, error.OutOfMemory => |e| return e, }; }, else => return failDecl(c, fn_decl_loc, fn_name, "unable to resolve function type {}", .{ZigClangType_getTypeClass(fn_type)}), }; if (!decl_ctx.has_body) { const semi_tok = try appendToken(c, .Semicolon, ";"); return addTopLevelDecl(c, fn_name, &proto_node.base); } // actual function definition with body const body_stmt = ZigClangFunctionDecl_getBody(fn_decl); const block_scope = try Scope.Block.init(rp.c, &c.global_scope.base, null); var scope = &block_scope.base; const block_node = try transCreateNodeBlock(rp.c, null); block_scope.block_node = block_node; var it = proto_node.params.iterator(0); while (it.next()) |p| { const param = @fieldParentPtr(ast.Node.ParamDecl, "base", p.*); const param_name = tokenSlice(c, param.name_token orelse return failDecl(c, fn_decl_loc, fn_name, "function {} parameter has no name", .{fn_name})); const mangled_param_name = try block_scope.makeMangledName(c, param_name); const arg_name = blk: { const bare_arg_name = try std.fmt.allocPrint(c.a(), "arg_{}", .{mangled_param_name}); break :blk try block_scope.makeMangledName(c, bare_arg_name); }; const node = try transCreateNodeVarDecl(c, false, false, mangled_param_name); node.eq_token = try appendToken(c, .Equal, "="); node.init_node = try transCreateNodeIdentifier(c, arg_name); node.semicolon_token = try appendToken(c, .Semicolon, ";"); try block_node.statements.push(&node.base); param.name_token = try appendIdentifier(c, arg_name); _ = try appendToken(c, .Colon, ":"); } transCompoundStmtInline(rp, &block_scope.base, @ptrCast(*const ZigClangCompoundStmt, body_stmt), block_node) catch |err| switch (err) { error.OutOfMemory => |e| return e, error.UnsupportedTranslation, error.UnsupportedType, => return failDecl(c, fn_decl_loc, fn_name, "unable to translate function", .{}), }; block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); proto_node.body_node = &block_node.base; return addTopLevelDecl(c, fn_name, &proto_node.base); } fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { const var_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, var_decl))); if (c.global_scope.sym_table.contains(var_name)) return; // Avoid processing this decl twice const rp = makeRestorePoint(c); const visib_tok = try appendToken(c, .Keyword_pub, "pub"); const thread_local_token = if (ZigClangVarDecl_getTLSKind(var_decl) == .None) null else try appendToken(c, .Keyword_threadlocal, "threadlocal"); const scope = &c.global_scope.base; // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "_{}", .{var_name}) else var_name; const var_decl_loc = ZigClangVarDecl_getLocation(var_decl); const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); const storage_class = ZigClangVarDecl_getStorageClass(var_decl); const is_const = ZigClangQualType_isConstQualified(qual_type); const extern_tok = if (storage_class == .Extern) try appendToken(c, .Keyword_extern, "extern") else if (storage_class != .Static) try appendToken(c, .Keyword_export, "export") else null; const mut_tok = if (is_const) try appendToken(c, .Keyword_const, "const") else try appendToken(c, .Keyword_var, "var"); const name_tok = try appendIdentifier(c, checked_name); _ = try appendToken(c, .Colon, ":"); const type_node = transQualType(rp, qual_type, var_decl_loc) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, var_decl_loc, checked_name, "unable to resolve variable type", .{}); }, error.OutOfMemory => |e| return e, }; var eq_tok: ast.TokenIndex = undefined; var init_node: ?*ast.Node = null; // If the initialization expression is not present, initialize with undefined. // If it is an integer literal, we can skip the @as since it will be redundant // with the variable type. if (ZigClangVarDecl_hasInit(var_decl)) { eq_tok = try appendToken(c, .Equal, "="); init_node = if (ZigClangVarDecl_getInit(var_decl)) |expr| transExprCoercing(rp, &c.global_scope.base, expr, .used, .r_value) catch |err| switch (err) { error.UnsupportedTranslation, error.UnsupportedType, => { return failDecl(c, var_decl_loc, checked_name, "unable to translate initializer", .{}); }, error.OutOfMemory => |e| return e, } else try transCreateNodeUndefinedLiteral(c); } else if (storage_class != .Extern) { return failDecl(c, var_decl_loc, checked_name, "non-extern variable has no initializer", .{}); } const node = try c.a().create(ast.Node.VarDecl); node.* = ast.Node.VarDecl{ .doc_comments = null, .visib_token = visib_tok, .thread_local_token = thread_local_token, .name_token = name_tok, .eq_token = eq_tok, .mut_token = mut_tok, .comptime_token = null, .extern_export_token = extern_tok, .lib_name = null, .type_node = type_node, .align_node = null, .section_node = null, .init_node = init_node, .semicolon_token = try appendToken(c, .Semicolon, ";"), }; return addTopLevelDecl(c, checked_name, &node.base); } fn transTypeDefAsBuiltin(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, builtin_name: []const u8) !*ast.Node { _ = try c.decl_table.put(@ptrToInt(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl)), builtin_name); return transCreateNodeIdentifier(c, builtin_name); } fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl) Error!?*ast.Node { if (c.decl_table.get(@ptrToInt(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl)))) |kv| return try transCreateNodeIdentifier(c, kv.value); // Avoid processing this decl twice const rp = makeRestorePoint(c); const typedef_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, typedef_decl))); // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.a(), "_{}", .{typedef_name}) else typedef_name; if (mem.eql(u8, checked_name, "uint8_t")) return transTypeDefAsBuiltin(c, typedef_decl, "u8") else if (mem.eql(u8, checked_name, "int8_t")) return transTypeDefAsBuiltin(c, typedef_decl, "i8") else if (mem.eql(u8, checked_name, "uint16_t")) return transTypeDefAsBuiltin(c, typedef_decl, "u16") else if (mem.eql(u8, checked_name, "int16_t")) return transTypeDefAsBuiltin(c, typedef_decl, "i16") else if (mem.eql(u8, checked_name, "uint32_t")) return transTypeDefAsBuiltin(c, typedef_decl, "u32") else if (mem.eql(u8, checked_name, "int32_t")) return transTypeDefAsBuiltin(c, typedef_decl, "i32") else if (mem.eql(u8, checked_name, "uint64_t")) return transTypeDefAsBuiltin(c, typedef_decl, "u64") else if (mem.eql(u8, checked_name, "int64_t")) return transTypeDefAsBuiltin(c, typedef_decl, "i64") else if (mem.eql(u8, checked_name, "intptr_t")) return transTypeDefAsBuiltin(c, typedef_decl, "isize") else if (mem.eql(u8, checked_name, "uintptr_t")) return transTypeDefAsBuiltin(c, typedef_decl, "usize") else if (mem.eql(u8, checked_name, "ssize_t")) return transTypeDefAsBuiltin(c, typedef_decl, "isize") else if (mem.eql(u8, checked_name, "size_t")) return transTypeDefAsBuiltin(c, typedef_decl, "usize"); _ = try c.decl_table.put(@ptrToInt(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl)), checked_name); const visib_tok = try appendToken(c, .Keyword_pub, "pub"); const const_tok = try appendToken(c, .Keyword_const, "const"); const node = try transCreateNodeVarDecl(c, true, true, checked_name); node.eq_token = try appendToken(c, .Equal, "="); const child_qt = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl); const typedef_loc = ZigClangTypedefNameDecl_getLocation(typedef_decl); node.init_node = transQualType(rp, child_qt, typedef_loc) catch |err| switch (err) { error.UnsupportedType => { try failDecl(c, typedef_loc, checked_name, "unable to resolve typedef child type", .{}); return null; }, error.OutOfMemory => |e| return e, }; node.semicolon_token = try appendToken(c, .Semicolon, ";"); try addTopLevelDecl(c, checked_name, &node.base); return transCreateNodeIdentifier(c, checked_name); } fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*ast.Node { if (c.decl_table.get(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)))) |kv| return try transCreateNodeIdentifier(c, kv.value); // Avoid processing this decl twice const record_loc = ZigClangRecordDecl_getLocation(record_decl); var bare_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, record_decl))); var is_unnamed = false; if (ZigClangRecordDecl_isAnonymousStructOrUnion(record_decl) or bare_name.len == 0) { bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()}); is_unnamed = true; } var container_kind_name: []const u8 = undefined; var container_kind: std.zig.Token.Id = undefined; if (ZigClangRecordDecl_isUnion(record_decl)) { container_kind_name = "union"; container_kind = .Keyword_union; } else if (ZigClangRecordDecl_isStruct(record_decl)) { container_kind_name = "struct"; container_kind = .Keyword_struct; } else { try emitWarning(c, record_loc, "record {} is not a struct or union", .{bare_name}); return null; } const name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name }); _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name); const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name); node.eq_token = try appendToken(c, .Equal, "="); var semicolon: ast.TokenIndex = undefined; node.init_node = blk: { const rp = makeRestorePoint(c); const record_def = ZigClangRecordDecl_getDefinition(record_decl) orelse { const opaque = try transCreateNodeOpaqueType(c); semicolon = try appendToken(c, .Semicolon, ";"); break :blk opaque; }; const extern_tok = try appendToken(c, .Keyword_extern, "extern"); const container_tok = try appendToken(c, container_kind, container_kind_name); const lbrace_token = try appendToken(c, .LBrace, "{"); const container_node = try c.a().create(ast.Node.ContainerDecl); container_node.* = .{ .layout_token = extern_tok, .kind_token = container_tok, .init_arg_expr = .None, .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()), .lbrace_token = lbrace_token, .rbrace_token = undefined, }; var it = ZigClangRecordDecl_field_begin(record_def); const end_it = ZigClangRecordDecl_field_end(record_def); while (ZigClangRecordDecl_field_iterator_neq(it, end_it)) : (it = ZigClangRecordDecl_field_iterator_next(it)) { const field_decl = ZigClangRecordDecl_field_iterator_deref(it); const field_loc = ZigClangFieldDecl_getLocation(field_decl); if (ZigClangFieldDecl_isBitField(field_decl)) { const opaque = try transCreateNodeOpaqueType(c); semicolon = try appendToken(c, .Semicolon, ";"); try emitWarning(c, field_loc, "{} demoted to opaque type - has bitfield", .{container_kind_name}); break :blk opaque; } const raw_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, field_decl))); if (raw_name.len < 1) continue; // fix weird windows bug? const field_name = try appendIdentifier(c, raw_name); _ = try appendToken(c, .Colon, ":"); const field_type = transQualType(rp, ZigClangFieldDecl_getType(field_decl), field_loc) catch |err| switch (err) { error.UnsupportedType => { try failDecl(c, record_loc, name, "unable to translate {} member type", .{container_kind_name}); return null; }, else => |e| return e, }; const field_node = try c.a().create(ast.Node.ContainerField); field_node.* = .{ .doc_comments = null, .comptime_token = null, .name_token = field_name, .type_expr = field_type, .value_expr = null, .align_expr = null, }; try container_node.fields_and_decls.push(&field_node.base); _ = try appendToken(c, .Comma, ","); } container_node.rbrace_token = try appendToken(c, .RBrace, "}"); semicolon = try appendToken(c, .Semicolon, ";"); break :blk &container_node.base; }; node.semicolon_token = semicolon; try addTopLevelDecl(c, name, &node.base); if (!is_unnamed) try c.alias_list.push(.{ .alias = bare_name, .name = name }); return transCreateNodeIdentifier(c, name); } fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.Node { if (c.decl_table.get(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)))) |name| return try transCreateNodeIdentifier(c, name.value); // Avoid processing this decl twice const rp = makeRestorePoint(c); const enum_loc = ZigClangEnumDecl_getLocation(enum_decl); var bare_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, enum_decl))); var is_unnamed = false; if (bare_name.len == 0) { bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()}); is_unnamed = true; } const name = try std.fmt.allocPrint(c.a(), "enum_{}", .{bare_name}); _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name); const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name); node.eq_token = try appendToken(c, .Equal, "="); node.init_node = if (ZigClangEnumDecl_getDefinition(enum_decl)) |enum_def| blk: { var pure_enum = true; var it = ZigClangEnumDecl_enumerator_begin(enum_def); var end_it = ZigClangEnumDecl_enumerator_end(enum_def); while (ZigClangEnumDecl_enumerator_iterator_neq(it, end_it)) : (it = ZigClangEnumDecl_enumerator_iterator_next(it)) { const enum_const = ZigClangEnumDecl_enumerator_iterator_deref(it); if (ZigClangEnumConstantDecl_getInitExpr(enum_const)) |_| { pure_enum = false; break; } } const extern_tok = try appendToken(c, .Keyword_extern, "extern"); const container_tok = try appendToken(c, .Keyword_enum, "enum"); const container_node = try c.a().create(ast.Node.ContainerDecl); container_node.* = .{ .layout_token = extern_tok, .kind_token = container_tok, .init_arg_expr = .None, .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()), .lbrace_token = undefined, .rbrace_token = undefined, }; const int_type = ZigClangEnumDecl_getIntegerType(enum_decl); // The underlying type may be null in case of forward-declared enum // types, while that's not ISO-C compliant many compilers allow this and // default to the usual integer type used for all the enums. // TODO only emit this tag type if the enum tag type is not the default. // I don't know what the default is, need to figure out how clang is deciding. // it appears to at least be different across gcc/msvc if (int_type.ptr != null and !isCBuiltinType(int_type, .UInt) and !isCBuiltinType(int_type, .Int)) { _ = try appendToken(c, .LParen, "("); container_node.init_arg_expr = .{ .Type = transQualType(rp, int_type, enum_loc) catch |err| switch (err) { error.UnsupportedType => { try failDecl(c, enum_loc, name, "unable to translate enum tag type", .{}); return null; }, else => |e| return e, }, }; _ = try appendToken(c, .RParen, ")"); } container_node.lbrace_token = try appendToken(c, .LBrace, "{"); it = ZigClangEnumDecl_enumerator_begin(enum_def); end_it = ZigClangEnumDecl_enumerator_end(enum_def); while (ZigClangEnumDecl_enumerator_iterator_neq(it, end_it)) : (it = ZigClangEnumDecl_enumerator_iterator_next(it)) { const enum_const = ZigClangEnumDecl_enumerator_iterator_deref(it); const enum_val_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, enum_const))); const field_name = if (!is_unnamed and mem.startsWith(u8, enum_val_name, bare_name)) enum_val_name[bare_name.len..] else enum_val_name; const field_name_tok = try appendIdentifier(c, field_name); const int_node = if (!pure_enum) blk: { _ = try appendToken(c, .Colon, "="); break :blk try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const)); } else null; const field_node = try c.a().create(ast.Node.ContainerField); field_node.* = .{ .doc_comments = null, .comptime_token = null, .name_token = field_name_tok, .type_expr = null, .value_expr = int_node, .align_expr = null, }; try container_node.fields_and_decls.push(&field_node.base); _ = try appendToken(c, .Comma, ","); // In C each enum value is in the global namespace. So we put them there too. // At this point we can rely on the enum emitting successfully. const tld_node = try transCreateNodeVarDecl(c, true, true, enum_val_name); tld_node.eq_token = try appendToken(c, .Equal, "="); tld_node.init_node = try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const)); tld_node.semicolon_token = try appendToken(c, .Semicolon, ";"); try addTopLevelDecl(c, field_name, &tld_node.base); } container_node.rbrace_token = try appendToken(c, .RBrace, "}"); break :blk &container_node.base; } else try transCreateNodeOpaqueType(c); node.semicolon_token = try appendToken(c, .Semicolon, ";"); try addTopLevelDecl(c, name, &node.base); if (!is_unnamed) try c.alias_list.push(.{ .alias = bare_name, .name = name }); return transCreateNodeIdentifier(c, name); } fn createAlias(c: *Context, alias: var) !void { const node = try transCreateNodeVarDecl(c, true, true, alias.alias); node.eq_token = try appendToken(c, .Equal, "="); node.init_node = try transCreateNodeIdentifier(c, alias.name); node.semicolon_token = try appendToken(c, .Semicolon, ";"); return addTopLevelDecl(c, alias.alias, &node.base); } const ResultUsed = enum { used, unused, }; const LRValue = enum { l_value, r_value, }; fn transStmt( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmt, result_used: ResultUsed, lrvalue: LRValue, ) TransError!*ast.Node { const sc = ZigClangStmt_getStmtClass(stmt); switch (sc) { .BinaryOperatorClass => return transBinaryOperator(rp, scope, @ptrCast(*const ZigClangBinaryOperator, stmt), result_used), .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), .CStyleCastExprClass => return transCStyleCastExprClass(rp, scope, @ptrCast(*const ZigClangCStyleCastExpr, stmt), result_used, lrvalue), .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), .DeclRefExprClass => return transDeclRefExpr(rp, scope, @ptrCast(*const ZigClangDeclRefExpr, stmt), lrvalue), .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt), result_used), .IntegerLiteralClass => return transIntegerLiteral(rp, scope, @ptrCast(*const ZigClangIntegerLiteral, stmt), result_used, .with_as), .ReturnStmtClass => return transReturnStmt(rp, scope, @ptrCast(*const ZigClangReturnStmt, stmt)), .StringLiteralClass => return transStringLiteral(rp, scope, @ptrCast(*const ZigClangStringLiteral, stmt), result_used), .ParenExprClass => { const expr = try transExpr(rp, scope, ZigClangParenExpr_getSubExpr(@ptrCast(*const ZigClangParenExpr, stmt)), .used, lrvalue); if (expr.id == .GroupedExpression) return maybeSuppressResult(rp, scope, result_used, expr); const node = try rp.c.a().create(ast.Node.GroupedExpression); node.* = .{ .lparen = try appendToken(rp.c, .LParen, "("), .expr = expr, .rparen = try appendToken(rp.c, .RParen, ")"), }; return maybeSuppressResult(rp, scope, result_used, &node.base); }, .InitListExprClass => return transInitListExpr(rp, scope, @ptrCast(*const ZigClangInitListExpr, stmt), result_used), .ImplicitValueInitExprClass => return transImplicitValueInitExpr(rp, scope, @ptrCast(*const ZigClangExpr, stmt), result_used), .IfStmtClass => return transIfStmt(rp, scope, @ptrCast(*const ZigClangIfStmt, stmt)), .WhileStmtClass => return transWhileLoop(rp, scope, @ptrCast(*const ZigClangWhileStmt, stmt)), .DoStmtClass => return transDoWhileLoop(rp, scope, @ptrCast(*const ZigClangDoStmt, stmt)), .NullStmtClass => { const block = try transCreateNodeBlock(rp.c, null); block.rbrace = try appendToken(rp.c, .RBrace, "}"); return &block.base; }, .ContinueStmtClass => return try transCreateNodeContinue(rp.c), .BreakStmtClass => return transBreak(rp, scope), .ForStmtClass => return transForLoop(rp, scope, @ptrCast(*const ZigClangForStmt, stmt)), .FloatingLiteralClass => return transFloatingLiteral(rp, scope, @ptrCast(*const ZigClangFloatingLiteral, stmt), result_used), .ConditionalOperatorClass => return transConditionalOperator(rp, scope, @ptrCast(*const ZigClangConditionalOperator, stmt), result_used), .SwitchStmtClass => return transSwitch(rp, scope, @ptrCast(*const ZigClangSwitchStmt, stmt)), .CaseStmtClass => return transCase(rp, scope, @ptrCast(*const ZigClangCaseStmt, stmt)), .DefaultStmtClass => return transDefault(rp, scope, @ptrCast(*const ZigClangDefaultStmt, stmt)), .ConstantExprClass => return transConstantExpr(rp, scope, @ptrCast(*const ZigClangExpr, stmt), result_used), .PredefinedExprClass => return transPredefinedExpr(rp, scope, @ptrCast(*const ZigClangPredefinedExpr, stmt), result_used), .CharacterLiteralClass => return transCharLiteral(rp, scope, @ptrCast(*const ZigClangCharacterLiteral, stmt), result_used, .with_as), .StmtExprClass => return transStmtExpr(rp, scope, @ptrCast(*const ZigClangStmtExpr, stmt), result_used), .MemberExprClass => return transMemberExpr(rp, scope, @ptrCast(*const ZigClangMemberExpr, stmt), result_used), .ArraySubscriptExprClass => return transArrayAccess(rp, scope, @ptrCast(*const ZigClangArraySubscriptExpr, stmt), result_used), .CallExprClass => return transCallExpr(rp, scope, @ptrCast(*const ZigClangCallExpr, stmt), result_used), .UnaryExprOrTypeTraitExprClass => return transUnaryExprOrTypeTraitExpr(rp, scope, @ptrCast(*const ZigClangUnaryExprOrTypeTraitExpr, stmt), result_used), .UnaryOperatorClass => return transUnaryOperator(rp, scope, @ptrCast(*const ZigClangUnaryOperator, stmt), result_used), .CompoundAssignOperatorClass => return transCompoundAssignOperator(rp, scope, @ptrCast(*const ZigClangCompoundAssignOperator, stmt), result_used), else => { return revertAndWarn( rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(stmt), "TODO implement translation of stmt class {}", .{@tagName(sc)}, ); }, } } fn transBinaryOperator( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangBinaryOperator, result_used: ResultUsed, ) TransError!*ast.Node { const op = ZigClangBinaryOperator_getOpcode(stmt); const qt = ZigClangBinaryOperator_getType(stmt); var op_token: ast.TokenIndex = undefined; var op_id: ast.Node.InfixOp.Op = undefined; switch (op) { .Assign => return transCreateNodeAssign(rp, scope, result_used, ZigClangBinaryOperator_getLHS(stmt), ZigClangBinaryOperator_getRHS(stmt)), .Comma => { const block_scope = try scope.findBlockScope(rp.c); const expr = block_scope.base.parent == scope; const lparen = if (expr) blk: { const l = try appendToken(rp.c, .LParen, "("); block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label); break :blk l; } else undefined; const lhs = try transExpr(rp, &block_scope.base, ZigClangBinaryOperator_getLHS(stmt), .unused, .r_value); try block_scope.block_node.statements.push(lhs); const rhs = try transExpr(rp, &block_scope.base, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); if (expr) { _ = try appendToken(rp.c, .Semicolon, ";"); const break_node = try transCreateNodeBreak(rp.c, block_scope.label); break_node.rhs = rhs; try block_scope.block_node.statements.push(&break_node.base); block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); const rparen = try appendToken(rp.c, .RParen, ")"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = lparen, .expr = &block_scope.block_node.base, .rparen = rparen, }; return maybeSuppressResult(rp, scope, result_used, &grouped_expr.base); } else { return maybeSuppressResult(rp, scope, result_used, rhs); } }, .Div => { if (!cIsUnsignedInteger(qt)) { // signed integer division uses @divTrunc const div_trunc_node = try transCreateNodeBuiltinFnCall(rp.c, "@divTrunc"); try div_trunc_node.params.push(try transExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value)); _ = try appendToken(rp.c, .Comma, ","); const rhs = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); try div_trunc_node.params.push(rhs); div_trunc_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return maybeSuppressResult(rp, scope, result_used, &div_trunc_node.base); } }, .Rem => { if (!cIsUnsignedInteger(qt)) { // signed integer division uses @rem const rem_node = try transCreateNodeBuiltinFnCall(rp.c, "@rem"); try rem_node.params.push(try transExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value)); _ = try appendToken(rp.c, .Comma, ","); const rhs = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); try rem_node.params.push(rhs); rem_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return maybeSuppressResult(rp, scope, result_used, &rem_node.base); } }, .Shl => { const node = try transCreateNodeShiftOp(rp, scope, stmt, .BitShiftLeft, .AngleBracketAngleBracketLeft, "<<"); return maybeSuppressResult(rp, scope, result_used, node); }, .Shr => { const node = try transCreateNodeShiftOp(rp, scope, stmt, .BitShiftRight, .AngleBracketAngleBracketRight, ">>"); return maybeSuppressResult(rp, scope, result_used, node); }, .LAnd => { const node = try transCreateNodeBoolInfixOp(rp, scope, stmt, .BoolAnd, result_used, true); return maybeSuppressResult(rp, scope, result_used, node); }, .LOr => { const node = try transCreateNodeBoolInfixOp(rp, scope, stmt, .BoolOr, result_used, true); return maybeSuppressResult(rp, scope, result_used, node); }, else => {}, } const lhs_node = try transExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value); switch (op) { .Add => { if (cIsUnsignedInteger(qt)) { op_token = try appendToken(rp.c, .PlusPercent, "+%"); op_id = .AddWrap; } else { op_token = try appendToken(rp.c, .Plus, "+"); op_id = .Add; } }, .Sub => { if (cIsUnsignedInteger(qt)) { op_token = try appendToken(rp.c, .MinusPercent, "-%"); op_id = .SubWrap; } else { op_token = try appendToken(rp.c, .Minus, "-"); op_id = .Sub; } }, .Mul => { if (cIsUnsignedInteger(qt)) { op_token = try appendToken(rp.c, .AsteriskPercent, "*%"); op_id = .MulWrap; } else { op_token = try appendToken(rp.c, .Asterisk, "*"); op_id = .Mul; } }, .Div => { // unsigned/float division uses the operator op_id = .Div; op_token = try appendToken(rp.c, .Slash, "/"); }, .Rem => { // unsigned/float division uses the operator op_id = .Mod; op_token = try appendToken(rp.c, .Percent, "%"); }, .LT => { op_id = .LessThan; op_token = try appendToken(rp.c, .AngleBracketLeft, "<"); }, .GT => { op_id = .GreaterThan; op_token = try appendToken(rp.c, .AngleBracketRight, ">"); }, .LE => { op_id = .LessOrEqual; op_token = try appendToken(rp.c, .AngleBracketLeftEqual, "<="); }, .GE => { op_id = .GreaterOrEqual; op_token = try appendToken(rp.c, .AngleBracketRightEqual, ">="); }, .EQ => { op_id = .EqualEqual; op_token = try appendToken(rp.c, .EqualEqual, "=="); }, .NE => { op_id = .BangEqual; op_token = try appendToken(rp.c, .BangEqual, "!="); }, .And => { op_id = .BitAnd; op_token = try appendToken(rp.c, .Ampersand, "&"); }, .Xor => { op_id = .BitXor; op_token = try appendToken(rp.c, .Caret, "^"); }, .Or => { op_id = .BitOr; op_token = try appendToken(rp.c, .Pipe, "|"); }, .Assign, .MulAssign, .DivAssign, .RemAssign, .AddAssign, .SubAssign, .ShlAssign, .ShrAssign, .AndAssign, .XorAssign, .OrAssign, => unreachable, else => unreachable, } const rhs_node = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); return transCreateNodeInfixOp(rp, scope, lhs_node, op_id, op_token, rhs_node, result_used, true); } fn transCompoundStmtInline( rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangCompoundStmt, block_node: *ast.Node.Block, ) TransError!void { var it = ZigClangCompoundStmt_body_begin(stmt); const end_it = ZigClangCompoundStmt_body_end(stmt); while (it != end_it) : (it += 1) { const result = try transStmt(rp, parent_scope, it[0], .unused, .r_value); if (result != &block_node.base) try block_node.statements.push(result); } } fn transCompoundStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundStmt) TransError!*ast.Node { const block_scope = try Scope.Block.init(rp.c, scope, null); block_scope.block_node = try transCreateNodeBlock(rp.c, null); try transCompoundStmtInline(rp, &block_scope.base, stmt, block_scope.block_node); block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); return &block_scope.block_node.base; } fn transCStyleCastExprClass( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCStyleCastExpr, result_used: ResultUsed, lrvalue: LRValue, ) TransError!*ast.Node { const sub_expr = ZigClangCStyleCastExpr_getSubExpr(stmt); const cast_node = (try transCCast( rp, scope, ZigClangCStyleCastExpr_getBeginLoc(stmt), ZigClangCStyleCastExpr_getType(stmt), ZigClangExpr_getType(sub_expr), try transExpr(rp, scope, sub_expr, .used, lrvalue), )); return maybeSuppressResult(rp, scope, result_used, cast_node); } fn transDeclStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangDeclStmt) TransError!*ast.Node { const c = rp.c; const block_scope = scope.findBlockScope(c) catch unreachable; var it = ZigClangDeclStmt_decl_begin(stmt); const end_it = ZigClangDeclStmt_decl_end(stmt); while (it != end_it) : (it += 1) { switch (ZigClangDecl_getKind(it[0])) { .Var => { const var_decl = @ptrCast(*const ZigClangVarDecl, it[0]); const thread_local_token = if (ZigClangVarDecl_getTLSKind(var_decl) == .None) null else try appendToken(c, .Keyword_threadlocal, "threadlocal"); const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); const name = try c.str(ZigClangDecl_getName_bytes_begin( @ptrCast(*const ZigClangDecl, var_decl), )); const mangled_name = try block_scope.makeMangledName(c, name); const node = try transCreateNodeVarDecl(c, false, ZigClangQualType_isConstQualified(qual_type), mangled_name); _ = try appendToken(c, .Colon, ":"); const loc = ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)); node.type_node = try transQualType(rp, qual_type, loc); node.eq_token = try appendToken(c, .Equal, "="); var init_node = if (ZigClangVarDecl_getInit(var_decl)) |expr| try transExprCoercing(rp, scope, expr, .used, .r_value) else try transCreateNodeUndefinedLiteral(c); if (isBoolRes(init_node)) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@boolToInt"); try builtin_node.params.push(init_node); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); init_node = &builtin_node.base; } node.init_node = init_node; node.semicolon_token = try appendToken(c, .Semicolon, ";"); try block_scope.block_node.statements.push(&node.base); }, else => |kind| return revertAndWarn( rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO implement translation of DeclStmt kind {}", .{@tagName(kind)}, ), } } return &block_scope.block_node.base; } fn transDeclRefExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangDeclRefExpr, lrvalue: LRValue, ) TransError!*ast.Node { const value_decl = ZigClangDeclRefExpr_getDecl(expr); const name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, value_decl))); const mangled_name = scope.getAlias(name); return transCreateNodeIdentifier(rp.c, mangled_name); } fn transImplicitCastExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangImplicitCastExpr, result_used: ResultUsed, ) TransError!*ast.Node { const c = rp.c; const sub_expr = ZigClangImplicitCastExpr_getSubExpr(expr); const sub_expr_node = try transExpr(rp, scope, @ptrCast(*const ZigClangExpr, sub_expr), .used, .r_value); const dest_type = getExprQualType(c, @ptrCast(*const ZigClangExpr, expr)); const src_type = getExprQualType(c, sub_expr); switch (ZigClangImplicitCastExpr_getCastKind(expr)) { .BitCast, .FloatingCast, .FloatingToIntegral, .IntegralToFloating, .IntegralCast, .PointerToIntegral, .IntegralToPointer => { return transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, sub_expr_node); }, .LValueToRValue, .NoOp, .FunctionToPointerDecay, .ArrayToPointerDecay => { return maybeSuppressResult(rp, scope, result_used, sub_expr_node); }, .NullToPointer => { return try transCreateNodeNullLiteral(rp.c); }, .PointerToBoolean => { // @ptrToInt(val) != 0 const ptr_to_int = try transCreateNodeBuiltinFnCall(rp.c, "@ptrToInt"); try ptr_to_int.params.push(try transExpr(rp, scope, sub_expr, .used, .r_value)); ptr_to_int.rparen_token = try appendToken(rp.c, .RParen, ")"); const op_token = try appendToken(rp.c, .BangEqual, "!="); const rhs_node = try transCreateNodeInt(rp.c, 0); return transCreateNodeInfixOp(rp, scope, &ptr_to_int.base, .BangEqual, op_token, rhs_node, result_used, false); }, .IntegralToBoolean => { // val != 0 const node = try transExpr(rp, scope, sub_expr, .used, .r_value); const op_token = try appendToken(rp.c, .BangEqual, "!="); const rhs_node = try transCreateNodeInt(rp.c, 0); return transCreateNodeInfixOp(rp, scope, node, .BangEqual, op_token, rhs_node, result_used, false); }, else => |kind| return revertAndWarn( rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, expr)), "TODO implement translation of CastKind {}", .{@tagName(kind)}, ), } } fn transBoolExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangExpr, used: ResultUsed, lrvalue: LRValue, grouped: bool, ) TransError!*ast.Node { const lparen = if (grouped) try appendToken(rp.c, .LParen, "(") else undefined; var res = try transExpr(rp, scope, expr, used, lrvalue); if (isBoolRes(res)) { if (!grouped and res.id == .GroupedExpression) { const group = @fieldParentPtr(ast.Node.GroupedExpression, "base", res); res = group.expr; // get zig fmt to work properly tokenSlice(rp.c, group.lparen)[0] = ')'; } return res; } const ty = ZigClangQualType_getTypePtr(getExprQualTypeBeforeImplicitCast(rp.c, expr)); const node = try finishBoolExpr(rp, scope, ZigClangExpr_getBeginLoc(expr), ty, res, used); if (grouped) { const rparen = try appendToken(rp.c, .RParen, ")"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = lparen, .expr = node, .rparen = rparen, }; return maybeSuppressResult(rp, scope, used, &grouped_expr.base); } else { return maybeSuppressResult(rp, scope, used, node); } } fn isBoolRes(res: *ast.Node) bool { switch (res.id) { .InfixOp => switch (@fieldParentPtr(ast.Node.InfixOp, "base", res).op) { .BoolOr, .BoolAnd, .EqualEqual, .BangEqual, .LessThan, .GreaterThan, .LessOrEqual, .GreaterOrEqual, => return true, else => {}, }, .PrefixOp => switch (@fieldParentPtr(ast.Node.PrefixOp, "base", res).op) { .BoolNot => return true, else => {}, }, .BoolLiteral => return true, .GroupedExpression => return isBoolRes(@fieldParentPtr(ast.Node.GroupedExpression, "base", res).expr), else => {}, } return false; } fn finishBoolExpr( rp: RestorePoint, scope: *Scope, loc: ZigClangSourceLocation, ty: *const ZigClangType, node: *ast.Node, used: ResultUsed, ) TransError!*ast.Node { switch (ZigClangType_getTypeClass(ty)) { .Builtin => { const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Bool => return node, .Char_U, .UChar, .Char_S, .SChar, .UShort, .UInt, .ULong, .ULongLong, .Short, .Int, .Long, .LongLong, .UInt128, .Int128, .Float, .Double, .Float128, .LongDouble, .WChar_U, .Char8, .Char16, .Char32, .WChar_S, .Float16, => { const op_token = try appendToken(rp.c, .BangEqual, "!="); const rhs_node = try transCreateNodeInt(rp.c, 0); return transCreateNodeInfixOp(rp, scope, node, .BangEqual, op_token, rhs_node, used, false); }, .NullPtr => { const op_token = try appendToken(rp.c, .EqualEqual, "=="); const rhs_node = try transCreateNodeNullLiteral(rp.c); return transCreateNodeInfixOp(rp, scope, node, .EqualEqual, op_token, rhs_node, used, false); }, else => {}, } }, .Pointer => { const op_token = try appendToken(rp.c, .BangEqual, "!="); const rhs_node = try transCreateNodeNullLiteral(rp.c); return transCreateNodeInfixOp(rp, scope, node, .BangEqual, op_token, rhs_node, used, false); }, .Typedef => { const typedef_ty = @ptrCast(*const ZigClangTypedefType, ty); const typedef_decl = ZigClangTypedefType_getDecl(typedef_ty); const underlying_type = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl); return finishBoolExpr(rp, scope, loc, ZigClangQualType_getTypePtr(underlying_type), node, used); }, .Enum => { const op_token = try appendToken(rp.c, .BangEqual, "!="); const rhs_node = try transCreateNodeInt(rp.c, 0); return transCreateNodeInfixOp(rp, scope, node, .BangEqual, op_token, rhs_node, used, false); }, .Elaborated => { const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ty); const named_type = ZigClangElaboratedType_getNamedType(elaborated_ty); return finishBoolExpr(rp, scope, loc, ZigClangQualType_getTypePtr(named_type), node, used); }, else => {}, } return revertAndWarn(rp, error.UnsupportedType, loc, "unsupported bool expression type", .{}); } const SuppressCast = enum { with_as, no_as, }; fn transIntegerLiteral( rp: RestorePoint, scope: *Scope, expr: *const ZigClangIntegerLiteral, result_used: ResultUsed, suppress_as: SuppressCast, ) TransError!*ast.Node { var eval_result: ZigClangExprEvalResult = undefined; if (!ZigClangIntegerLiteral_EvaluateAsInt(expr, &eval_result, rp.c.clang_context)) { const loc = ZigClangIntegerLiteral_getBeginLoc(expr); return revertAndWarn(rp, error.UnsupportedTranslation, loc, "invalid integer literal", .{}); } if (suppress_as == .no_as) { const int_lit_node = try transCreateNodeAPInt(rp.c, ZigClangAPValue_getInt(&eval_result.Val)); return maybeSuppressResult(rp, scope, result_used, int_lit_node); } // Integer literals in C have types, and this can matter for several reasons. // For example, this is valid C: // unsigned char y = 256; // How this gets evaluated is the 256 is an integer, which gets truncated to signed char, then bit-casted // to unsigned char, resulting in 0. In order for this to work, we have to emit this zig code: // var y = @bitCast(u8, @truncate(i8, @as(c_int, 256))); // Ideally in translate-c we could flatten this out to simply: // var y: u8 = 0; // But the first step is to be correct, and the next step is to make the output more elegant. // @as(T, x) const expr_base = @ptrCast(*const ZigClangExpr, expr); const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); const ty_node = try transQualType(rp, ZigClangExpr_getType(expr_base), ZigClangExpr_getBeginLoc(expr_base)); try as_node.params.push(ty_node); _ = try appendToken(rp.c, .Comma, ","); const int_lit_node = try transCreateNodeAPInt(rp.c, ZigClangAPValue_getInt(&eval_result.Val)); try as_node.params.push(int_lit_node); as_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return maybeSuppressResult(rp, scope, result_used, &as_node.base); } fn transReturnStmt( rp: RestorePoint, scope: *Scope, expr: *const ZigClangReturnStmt, ) TransError!*ast.Node { const node = try transCreateNodeReturnExpr(rp.c); if (ZigClangReturnStmt_getRetValue(expr)) |val_expr| { node.rhs = try transExprCoercing(rp, scope, val_expr, .used, .r_value); } _ = try appendToken(rp.c, .Semicolon, ";"); return &node.base; } fn transStringLiteral( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStringLiteral, result_used: ResultUsed, ) TransError!*ast.Node { const kind = ZigClangStringLiteral_getKind(stmt); switch (kind) { .Ascii, .UTF8 => { var len: usize = undefined; const bytes_ptr = ZigClangStringLiteral_getString_bytes_begin_size(stmt, &len); const str = bytes_ptr[0..len]; var char_buf: [4]u8 = undefined; len = 0; for (str) |c| len += escapeChar(c, &char_buf).len; const buf = try rp.c.a().alloc(u8, len + "\"\"".len); buf[0] = '"'; writeEscapedString(buf[1..], str); buf[buf.len - 1] = '"'; const token = try appendToken(rp.c, .StringLiteral, buf); const node = try rp.c.a().create(ast.Node.StringLiteral); node.* = .{ .token = token, }; return maybeSuppressResult(rp, scope, result_used, &node.base); }, .UTF16, .UTF32, .Wide => return revertAndWarn( rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO: support string literal kind {}", .{kind}, ), else => unreachable, } } fn escapedStringLen(s: []const u8) usize { var len: usize = 0; var char_buf: [4]u8 = undefined; for (s) |c| len += escapeChar(c, &char_buf).len; return len; } fn writeEscapedString(buf: []u8, s: []const u8) void { var char_buf: [4]u8 = undefined; var i: usize = 0; for (s) |c| { const escaped = escapeChar(c, &char_buf); mem.copy(u8, buf[i..], escaped); i += escaped.len; } } // Returns either a string literal or a slice of `buf`. fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 { return switch (c) { '\"' => "\\\""[0..], '\'' => "\\'"[0..], '\\' => "\\\\"[0..], '\n' => "\\n"[0..], '\r' => "\\r"[0..], '\t' => "\\t"[0..], else => std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable, }; } fn transCCast( rp: RestorePoint, scope: *Scope, loc: ZigClangSourceLocation, dst_type: ZigClangQualType, src_type: ZigClangQualType, expr: *ast.Node, ) !*ast.Node { if (ZigClangType_isVoidType(qualTypeCanon(dst_type))) return expr; if (ZigClangQualType_eq(dst_type, src_type)) return expr; if (qualTypeIsPtr(dst_type) and qualTypeIsPtr(src_type)) return transCPtrCast(rp, loc, dst_type, src_type, expr); if (cIsInteger(dst_type) and cIsInteger(src_type)) { // 1. Extend or truncate without changing signed-ness. // 2. Bit-cast to correct signed-ness // @bitCast(dest_type, intermediate_value) const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@bitCast"); try cast_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); switch (cIntTypeCmp(dst_type, src_type)) { .lt => { // @truncate(SameSignSmallerInt, src_type) const trunc_node = try transCreateNodeBuiltinFnCall(rp.c, "@truncate"); const ty_node = try transQualTypeIntWidthOf(rp.c, dst_type, cIsSignedInteger(src_type)); try trunc_node.params.push(ty_node); _ = try appendToken(rp.c, .Comma, ","); try trunc_node.params.push(expr); trunc_node.rparen_token = try appendToken(rp.c, .RParen, ")"); try cast_node.params.push(&trunc_node.base); }, .gt => { // @as(SameSignBiggerInt, src_type) const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); const ty_node = try transQualTypeIntWidthOf(rp.c, dst_type, cIsSignedInteger(src_type)); try as_node.params.push(ty_node); _ = try appendToken(rp.c, .Comma, ","); try as_node.params.push(expr); as_node.rparen_token = try appendToken(rp.c, .RParen, ")"); try cast_node.params.push(&as_node.base); }, .eq => { try cast_node.params.push(expr); }, } cast_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &cast_node.base; } if (cIsInteger(dst_type) and qualTypeIsPtr(src_type)) { // @intCast(dest_type, @ptrToInt(val)) const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast"); try cast_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@ptrToInt"); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); try cast_node.params.push(&builtin_node.base); cast_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &cast_node.base; } if (cIsInteger(src_type) and qualTypeIsPtr(dst_type)) { // @intToPtr(dest_type, val) const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@intToPtr"); try builtin_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &builtin_node.base; } if (cIsFloating(src_type) and cIsFloating(dst_type)) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@floatCast"); try builtin_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &builtin_node.base; } if (cIsFloating(src_type) and !cIsFloating(dst_type)) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@floatToInt"); try builtin_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &builtin_node.base; } if (!cIsFloating(src_type) and cIsFloating(dst_type)) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@intToFloat"); try builtin_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &builtin_node.base; } if (ZigClangQualType_getTypeClass(src_type) == .Elaborated) { const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(src_type)); return transCCast(rp, scope, loc, dst_type, ZigClangElaboratedType_getNamedType(elaborated_ty), expr); } if (ZigClangQualType_getTypeClass(dst_type) == .Elaborated) { const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(dst_type)); return transCCast(rp, scope, loc, ZigClangElaboratedType_getNamedType(elaborated_ty), src_type, expr); } if (ZigClangQualType_getTypeClass(dst_type) == .Enum) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@intToEnum"); try builtin_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &builtin_node.base; } if (ZigClangQualType_getTypeClass(src_type) == .Enum and ZigClangQualType_getTypeClass(dst_type) != .Enum) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@enumToInt"); try builtin_node.params.push(expr); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &builtin_node.base; } const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); try cast_node.params.push(try transQualType(rp, dst_type, loc)); _ = try appendToken(rp.c, .Comma, ","); try cast_node.params.push(expr); cast_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &cast_node.base; } fn transExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangExpr, used: ResultUsed, lrvalue: LRValue, ) TransError!*ast.Node { return transStmt(rp, scope, @ptrCast(*const ZigClangStmt, expr), used, lrvalue); } /// Same as `transExpr` but with the knowledge that the operand will be type coerced, and therefore /// an `@as` would be redundant. This is used to prevent redundant `@as` in integer literals. fn transExprCoercing( rp: RestorePoint, scope: *Scope, expr: *const ZigClangExpr, used: ResultUsed, lrvalue: LRValue, ) TransError!*ast.Node { switch (ZigClangStmt_getStmtClass(@ptrCast(*const ZigClangStmt, expr))) { .IntegerLiteralClass => { return transIntegerLiteral(rp, scope, @ptrCast(*const ZigClangIntegerLiteral, expr), .used, .no_as); }, .CharacterLiteralClass => { return transCharLiteral(rp, scope, @ptrCast(*const ZigClangCharacterLiteral, expr), .used, .no_as); }, .UnaryOperatorClass => { const un_expr = @ptrCast(*const ZigClangUnaryOperator, expr); if (ZigClangUnaryOperator_getOpcode(un_expr) == .Extension) { return transExprCoercing(rp, scope, ZigClangUnaryOperator_getSubExpr(un_expr), used, lrvalue); } }, else => {}, } return transExpr(rp, scope, expr, .used, .r_value); } fn transInitListExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangInitListExpr, used: ResultUsed, ) TransError!*ast.Node { const qt = getExprQualType(rp.c, @ptrCast(*const ZigClangExpr, expr)); const qual_type = ZigClangQualType_getTypePtr(qt); const source_loc = ZigClangExpr_getBeginLoc(@ptrCast(*const ZigClangExpr, expr)); switch (ZigClangType_getTypeClass(qual_type)) { .ConstantArray => {}, .Record, .Elaborated => { return revertAndWarn(rp, error.UnsupportedType, source_loc, "TODO initListExpr for structs", .{}); }, else => { const type_name = rp.c.str(ZigClangType_getTypeClassName(qual_type)); return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported initlist type: '{}'", .{type_name}); }, } const arr_type = ZigClangType_getAsArrayTypeUnsafe(qual_type); const child_qt = ZigClangArrayType_getElementType(arr_type); const init_count = ZigClangInitListExpr_getNumInits(expr); const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, qual_type); const size_ap_int = ZigClangConstantArrayType_getSize(const_arr_ty); const all_count = ZigClangAPInt_getLimitedValue(size_ap_int, math.maxInt(usize)); const leftover_count = all_count - init_count; var init_node: *ast.Node.SuffixOp = undefined; var cat_tok: ast.TokenIndex = undefined; if (init_count != 0) { const dot_tok = try appendToken(rp.c, .Period, "."); init_node = try transCreateNodeContainerInitializer(rp.c, dot_tok); var i: c_uint = 0; while (i < init_count) : (i += 1) { const elem_expr = ZigClangInitListExpr_getInit(expr, i); try init_node.op.ArrayInitializer.push(try transExpr(rp, scope, elem_expr, .used, .r_value)); _ = try appendToken(rp.c, .Comma, ","); } init_node.rtoken = try appendToken(rp.c, .RBrace, "}"); if (leftover_count == 0) { return &init_node.base; } cat_tok = try appendToken(rp.c, .PlusPlus, "++"); } const dot_tok = try appendToken(rp.c, .Period, "."); var filler_init_node = try transCreateNodeContainerInitializer(rp.c, dot_tok); const filler_val_expr = ZigClangInitListExpr_getArrayFiller(expr); try filler_init_node.op.ArrayInitializer.push(try transExpr(rp, scope, filler_val_expr, .used, .r_value)); filler_init_node.rtoken = try appendToken(rp.c, .RBrace, "}"); const rhs_node = if (leftover_count == 1) &filler_init_node.base else blk: { const mul_tok = try appendToken(rp.c, .AsteriskAsterisk, "**"); const mul_node = try rp.c.a().create(ast.Node.InfixOp); mul_node.* = .{ .op_token = mul_tok, .lhs = &filler_init_node.base, .op = .ArrayMult, .rhs = try transCreateNodeInt(rp.c, leftover_count), }; break :blk &mul_node.base; }; if (init_count == 0) { return rhs_node; } const cat_node = try rp.c.a().create(ast.Node.InfixOp); cat_node.* = .{ .op_token = cat_tok, .lhs = &init_node.base, .op = .ArrayCat, .rhs = rhs_node, }; return &cat_node.base; } fn transImplicitValueInitExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangExpr, used: ResultUsed, ) TransError!*ast.Node { const source_loc = ZigClangExpr_getBeginLoc(expr); const qt = getExprQualType(rp.c, expr); const ty = ZigClangQualType_getTypePtr(qt); const node = switch (ZigClangType_getTypeClass(ty)) { .Builtin => blk: { const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Bool => return try transCreateNodeBoolLiteral(rp.c, false), .Char_U, .UChar, .Char_S, .Char8, .SChar, .UShort, .UInt, .ULong, .ULongLong, .Short, .Int, .Long, .LongLong, .UInt128, .Int128, .Float, .Double, .Float128, .Float16, .LongDouble, => return transCreateNodeInt(rp.c, 0), else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type", .{}), } }, .Pointer => return transCreateNodeNullLiteral(rp.c), else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "type does not have an implicit init value", .{}), }; } fn transIfStmt( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangIfStmt, ) TransError!*ast.Node { // if (c) t // if (c) t else e const if_node = try transCreateNodeIf(rp.c); var cond_scope = Scope{ .parent = scope, .id = .Condition, }; if_node.condition = try transBoolExpr(rp, &cond_scope, @ptrCast(*const ZigClangExpr, ZigClangIfStmt_getCond(stmt)), .used, .r_value, false); _ = try appendToken(rp.c, .RParen, ")"); if_node.body = try transStmt(rp, scope, ZigClangIfStmt_getThen(stmt), .unused, .r_value); if (ZigClangIfStmt_getElse(stmt)) |expr| { if_node.@"else" = try transCreateNodeElse(rp.c); if_node.@"else".?.body = try transStmt(rp, scope, expr, .unused, .r_value); } _ = try appendToken(rp.c, .Semicolon, ";"); return &if_node.base; } fn transWhileLoop( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangWhileStmt, ) TransError!*ast.Node { const while_node = try transCreateNodeWhile(rp.c); var cond_scope = Scope{ .parent = scope, .id = .Condition, }; while_node.condition = try transBoolExpr(rp, &cond_scope, @ptrCast(*const ZigClangExpr, ZigClangWhileStmt_getCond(stmt)), .used, .r_value, false); _ = try appendToken(rp.c, .RParen, ")"); var loop_scope = Scope{ .parent = scope, .id = .Loop, }; while_node.body = try transStmt(rp, &loop_scope, ZigClangWhileStmt_getBody(stmt), .unused, .r_value); return &while_node.base; } fn transDoWhileLoop( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangDoStmt, ) TransError!*ast.Node { const while_node = try transCreateNodeWhile(rp.c); while_node.condition = try transCreateNodeBoolLiteral(rp.c, true); _ = try appendToken(rp.c, .RParen, ")"); var new = false; var loop_scope = Scope{ .parent = scope, .id = .Loop, }; const body_node = if (ZigClangStmt_getStmtClass(ZigClangDoStmt_getBody(stmt)) == .CompoundStmtClass) blk: { // there's already a block in C, so we'll append our condition to it. // c: do { // c: a; // c: b; // c: } while(c); // zig: while (true) { // zig: a; // zig: b; // zig: if (!cond) break; // zig: } const body = (try transStmt(rp, &loop_scope, ZigClangDoStmt_getBody(stmt), .unused, .r_value)).cast(ast.Node.Block).?; // if this is used as an expression in Zig it needs to be immediately followed by a semicolon _ = try appendToken(rp.c, .Semicolon, ";"); break :blk body; } else blk: { // the C statement is without a block, so we need to create a block to contain it. // c: do // c: a; // c: while(c); // zig: while (true) { // zig: a; // zig: if (!cond) break; // zig: } new = true; const block = try transCreateNodeBlock(rp.c, null); try block.statements.push(try transStmt(rp, &loop_scope, ZigClangDoStmt_getBody(stmt), .unused, .r_value)); break :blk block; }; // if (!cond) break; const if_node = try transCreateNodeIf(rp.c); var cond_scope = Scope{ .parent = scope, .id = .Condition, }; const prefix_op = try transCreateNodePrefixOp(rp.c, .BoolNot, .Bang, "!"); prefix_op.rhs = try transBoolExpr(rp, &cond_scope, @ptrCast(*const ZigClangExpr, ZigClangDoStmt_getCond(stmt)), .used, .r_value, true); _ = try appendToken(rp.c, .RParen, ")"); if_node.condition = &prefix_op.base; if_node.body = &(try transCreateNodeBreak(rp.c, null)).base; _ = try appendToken(rp.c, .Semicolon, ";"); try body_node.statements.push(&if_node.base); if (new) body_node.rbrace = try appendToken(rp.c, .RBrace, "}"); while_node.body = &body_node.base; return &while_node.base; } fn transForLoop( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangForStmt, ) TransError!*ast.Node { var loop_scope = Scope{ .parent = scope, .id = .Loop, }; var block = false; var block_scope: ?*Scope.Block = null; if (ZigClangForStmt_getInit(stmt)) |init| { block_scope = try Scope.Block.init(rp.c, scope, null); block_scope.?.block_node = try transCreateNodeBlock(rp.c, null); loop_scope.parent = &block_scope.?.base; _ = try transStmt(rp, &loop_scope, init, .unused, .r_value); } var cond_scope = Scope{ .parent = scope, .id = .Condition, }; const while_node = try transCreateNodeWhile(rp.c); while_node.condition = if (ZigClangForStmt_getCond(stmt)) |cond| try transBoolExpr(rp, &cond_scope, cond, .used, .r_value, false) else try transCreateNodeBoolLiteral(rp.c, true); _ = try appendToken(rp.c, .RParen, ")"); if (ZigClangForStmt_getInc(stmt)) |incr| { _ = try appendToken(rp.c, .Colon, ":"); _ = try appendToken(rp.c, .LParen, "("); while_node.continue_expr = try transExpr(rp, &cond_scope, incr, .unused, .r_value); _ = try appendToken(rp.c, .RParen, ")"); } while_node.body = try transStmt(rp, &loop_scope, ZigClangForStmt_getBody(stmt), .unused, .r_value); if (block_scope != null) { try block_scope.?.block_node.statements.push(&while_node.base); block_scope.?.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); return &block_scope.?.block_node.base; } else return &while_node.base; } fn transSwitch( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangSwitchStmt, ) TransError!*ast.Node { const switch_node = try transCreateNodeSwitch(rp.c); var switch_scope = Scope.Switch{ .base = .{ .id = .Switch, .parent = scope, }, .cases = &switch_node.cases, .pending_block = undefined, }; var cond_scope = Scope{ .parent = scope, .id = .Condition, }; switch_node.expr = try transExpr(rp, &cond_scope, ZigClangSwitchStmt_getCond(stmt), .used, .r_value); _ = try appendToken(rp.c, .RParen, ")"); _ = try appendToken(rp.c, .LBrace, "{"); switch_node.rbrace = try appendToken(rp.c, .RBrace, "}"); const block_scope = try Scope.Block.init(rp.c, &switch_scope.base, null); // tmp block that all statements will go before being picked up by a case or default const block = try transCreateNodeBlock(rp.c, null); block_scope.block_node = block; const switch_block = try transCreateNodeBlock(rp.c, null); try switch_block.statements.push(&switch_node.base); switch_scope.pending_block = switch_block; const last = try transStmt(rp, &block_scope.base, ZigClangSwitchStmt_getBody(stmt), .unused, .r_value); _ = try appendToken(rp.c, .Semicolon, ";"); // take all pending statements var it = last.cast(ast.Node.Block).?.statements.iterator(0); while (it.next()) |n| { try switch_scope.pending_block.statements.push(n.*); } switch_scope.pending_block.label = try appendIdentifier(rp.c, "__switch"); _ = try appendToken(rp.c, .Colon, ":"); if (!switch_scope.has_default) { const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c)); else_prong.expr = &(try transCreateNodeBreak(rp.c, "__switch")).base; _ = try appendToken(rp.c, .Comma, ","); try switch_node.cases.push(&else_prong.base); } switch_scope.pending_block.rbrace = try appendToken(rp.c, .RBrace, "}"); return &switch_scope.pending_block.base; } fn transCase( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCaseStmt, ) TransError!*ast.Node { const block_scope = scope.findBlockScope(rp.c) catch unreachable; const switch_scope = scope.getSwitch(); const label = try std.fmt.allocPrint(rp.c.a(), "__case_{}", .{switch_scope.cases.len - @boolToInt(switch_scope.has_default)}); _ = try appendToken(rp.c, .Semicolon, ";"); const expr = if (ZigClangCaseStmt_getRHS(stmt)) |rhs| blk: { const lhs_node = try transExpr(rp, scope, ZigClangCaseStmt_getLHS(stmt), .used, .r_value); const ellips = try appendToken(rp.c, .Ellipsis3, "..."); const rhs_node = try transExpr(rp, scope, rhs, .used, .r_value); const node = try rp.c.a().create(ast.Node.InfixOp); node.* = .{ .op_token = ellips, .lhs = lhs_node, .op = .Range, .rhs = rhs_node, }; break :blk &node.base; } else try transExpr(rp, scope, ZigClangCaseStmt_getLHS(stmt), .used, .r_value); const switch_prong = try transCreateNodeSwitchCase(rp.c, expr); switch_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base; _ = try appendToken(rp.c, .Comma, ","); try switch_scope.cases.push(&switch_prong.base); const block = try transCreateNodeBlock(rp.c, null); switch_scope.pending_block.label = try appendIdentifier(rp.c, label); _ = try appendToken(rp.c, .Colon, ":"); switch_scope.pending_block.rbrace = try appendToken(rp.c, .RBrace, "}"); try block.statements.push(&switch_scope.pending_block.base); // take all pending statements var it = block_scope.block_node.statements.iterator(0); while (it.next()) |n| { try switch_scope.pending_block.statements.push(n.*); } block_scope.block_node.statements.shrink(0); switch_scope.pending_block = block; return transStmt(rp, scope, ZigClangCaseStmt_getSubStmt(stmt), .unused, .r_value); } fn transDefault( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangDefaultStmt, ) TransError!*ast.Node { const block_scope = scope.findBlockScope(rp.c) catch unreachable; const switch_scope = scope.getSwitch(); const label = "__default"; switch_scope.has_default = true; _ = try appendToken(rp.c, .Semicolon, ";"); const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c)); else_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base; _ = try appendToken(rp.c, .Comma, ","); try switch_scope.cases.push(&else_prong.base); const block = try transCreateNodeBlock(rp.c, null); switch_scope.pending_block.label = try appendIdentifier(rp.c, label); _ = try appendToken(rp.c, .Colon, ":"); switch_scope.pending_block.rbrace = try appendToken(rp.c, .RBrace, "}"); try block.statements.push(&switch_scope.pending_block.base); // take all pending statements var it = block_scope.block_node.statements.iterator(0); while (it.next()) |n| { try switch_scope.pending_block.statements.push(n.*); } block_scope.block_node.statements.shrink(0); switch_scope.pending_block = block; return transStmt(rp, scope, ZigClangDefaultStmt_getSubStmt(stmt), .unused, .r_value); } fn transConstantExpr(rp: RestorePoint, scope: *Scope, expr: *const ZigClangExpr, used: ResultUsed) TransError!*ast.Node { var result: ZigClangExprEvalResult = undefined; if (!ZigClangExpr_EvaluateAsConstantExpr(expr, &result, .EvaluateForCodeGen, rp.c.clang_context)) return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangExpr_getBeginLoc(expr), "invalid constant expression", .{}); return maybeSuppressResult(rp, scope, used, try transCreateNodeAPInt(rp.c, ZigClangAPValue_getInt(&result.Val))); } fn transPredefinedExpr(rp: RestorePoint, scope: *Scope, expr: *const ZigClangPredefinedExpr, used: ResultUsed) TransError!*ast.Node { return transStringLiteral(rp, scope, ZigClangPredefinedExpr_getFunctionName(expr), used); } fn transCharLiteral( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCharacterLiteral, result_used: ResultUsed, suppress_as: SuppressCast, ) TransError!*ast.Node { const kind = ZigClangCharacterLiteral_getKind(stmt); const int_lit_node = switch (kind) { .Ascii, .UTF8 => blk: { const val = ZigClangCharacterLiteral_getValue(stmt); if (kind == .Ascii) { // C has a somewhat obscure feature called multi-character character // constant if (val > 255) break :blk try transCreateNodeInt(rp.c, val); } var char_buf: [4]u8 = undefined; const token = try appendTokenFmt(rp.c, .CharLiteral, "'{}'", .{escapeChar(@intCast(u8, val), &char_buf)}); const node = try rp.c.a().create(ast.Node.CharLiteral); node.* = .{ .token = token, }; break :blk &node.base; }, .UTF16, .UTF32, .Wide => return revertAndWarn( rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO: support character literal kind {}", .{kind}, ), else => unreachable, }; if (suppress_as == .no_as) { return maybeSuppressResult(rp, scope, result_used, int_lit_node); } // See comment in `transIntegerLiteral` for why this code is here. // @as(T, x) const expr_base = @ptrCast(*const ZigClangExpr, stmt); const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); const ty_node = try transQualType(rp, ZigClangExpr_getType(expr_base), ZigClangExpr_getBeginLoc(expr_base)); try as_node.params.push(ty_node); _ = try appendToken(rp.c, .Comma, ","); try as_node.params.push(int_lit_node); as_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return maybeSuppressResult(rp, scope, result_used, &as_node.base); } fn transStmtExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmtExpr, used: ResultUsed) TransError!*ast.Node { const comp = ZigClangStmtExpr_getSubStmt(stmt); if (used == .unused) { return transCompoundStmt(rp, scope, comp); } const lparen = try appendToken(rp.c, .LParen, "("); const block_scope = try Scope.Block.init(rp.c, scope, "blk"); const block = try transCreateNodeBlock(rp.c, "blk"); block_scope.block_node = block; var it = ZigClangCompoundStmt_body_begin(comp); const end_it = ZigClangCompoundStmt_body_end(comp); while (it != end_it - 1) : (it += 1) { const result = try transStmt(rp, &block_scope.base, it[0], .unused, .r_value); if (result != &block.base) try block.statements.push(result); } const break_node = try transCreateNodeBreak(rp.c, "blk"); break_node.rhs = try transStmt(rp, &block_scope.base, it[0], .used, .r_value); _ = try appendToken(rp.c, .Semicolon, ";"); try block.statements.push(&break_node.base); block.rbrace = try appendToken(rp.c, .RBrace, "}"); const rparen = try appendToken(rp.c, .RParen, ")"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = lparen, .expr = &block.base, .rparen = rparen, }; return maybeSuppressResult(rp, scope, used, &grouped_expr.base); } fn transMemberExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangMemberExpr, result_used: ResultUsed) TransError!*ast.Node { var container_node = try transExpr(rp, scope, ZigClangMemberExpr_getBase(stmt), .used, .r_value); if (ZigClangMemberExpr_isArrow(stmt)) { container_node = try transCreateNodePtrDeref(rp.c, container_node); } const name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, ZigClangMemberExpr_getMemberDecl(stmt)))); if (name.len == 0) { return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO access of anonymous field", .{}); } const node = try transCreateNodeFieldAccess(rp.c, container_node, name); return maybeSuppressResult(rp, scope, result_used, node); } fn transArrayAccess(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangArraySubscriptExpr, result_used: ResultUsed) TransError!*ast.Node { const container_node = try transExpr(rp, scope, ZigClangArraySubscriptExpr_getBase(stmt), .used, .r_value); const node = try transCreateNodeArrayAccess(rp.c, container_node); node.op.ArrayAccess = try transExpr(rp, scope, ZigClangArraySubscriptExpr_getIdx(stmt), .used, .r_value); node.rtoken = try appendToken(rp.c, .RBrace, "]"); return maybeSuppressResult(rp, scope, result_used, &node.base); } fn transCallExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCallExpr, result_used: ResultUsed) TransError!*ast.Node { const callee = ZigClangCallExpr_getCallee(stmt); var raw_fn_expr = try transExpr(rp, scope, callee, .used, .r_value); var is_ptr = false; const fn_ty = qualTypeGetFnProto(ZigClangExpr_getType(callee), &is_ptr); const fn_expr = if (is_ptr and fn_ty != null) blk: { if (ZigClangExpr_getStmtClass(callee) == .ImplicitCastExprClass) { const implicit_cast = @ptrCast(*const ZigClangImplicitCastExpr, callee); if (ZigClangImplicitCastExpr_getCastKind(implicit_cast) == .FunctionToPointerDecay) { const subexpr = ZigClangImplicitCastExpr_getSubExpr(implicit_cast); if (ZigClangExpr_getStmtClass(subexpr) == .DeclRefExprClass) { const decl_ref = @ptrCast(*const ZigClangDeclRefExpr, subexpr); const named_decl = ZigClangDeclRefExpr_getFoundDecl(decl_ref); if (ZigClangDecl_getKind(@ptrCast(*const ZigClangDecl, named_decl)) == .Function) { break :blk raw_fn_expr; } } } } break :blk try transCreateNodeUnwrapNull(rp.c, raw_fn_expr); } else raw_fn_expr; const node = try transCreateNodeFnCall(rp.c, fn_expr); const num_args = ZigClangCallExpr_getNumArgs(stmt); const args = ZigClangCallExpr_getArgs(stmt); var i: usize = 0; while (i < num_args) : (i += 1) { if (i != 0) { _ = try appendToken(rp.c, .Comma, ","); } const arg = try transExpr(rp, scope, args[i], .used, .r_value); try node.op.Call.params.push(arg); } node.rtoken = try appendToken(rp.c, .RParen, ")"); if (fn_ty) |ty| { const canon = ZigClangQualType_getCanonicalType(ty.getReturnType()); const ret_ty = ZigClangQualType_getTypePtr(canon); if (ZigClangType_isVoidType(ret_ty)) { _ = try appendToken(rp.c, .Semicolon, ";"); return &node.base; } } return maybeSuppressResult(rp, scope, result_used, &node.base); } const ClangFunctionType = union(enum) { Proto: *const ZigClangFunctionProtoType, NoProto: *const ZigClangFunctionType, fn getReturnType(self: @This()) ZigClangQualType { switch (@as(@TagType(@This()), self)) { .Proto => return ZigClangFunctionProtoType_getReturnType(self.Proto), .NoProto => return ZigClangFunctionType_getReturnType(self.NoProto), } } }; fn qualTypeGetFnProto(qt: ZigClangQualType, is_ptr: *bool) ?ClangFunctionType { const canon = ZigClangQualType_getCanonicalType(qt); var ty = ZigClangQualType_getTypePtr(canon); is_ptr.* = false; if (ZigClangType_getTypeClass(ty) == .Pointer) { is_ptr.* = true; const child_qt = ZigClangType_getPointeeType(ty); ty = ZigClangQualType_getTypePtr(child_qt); } if (ZigClangType_getTypeClass(ty) == .FunctionProto) { return ClangFunctionType{ .Proto = @ptrCast(*const ZigClangFunctionProtoType, ty) }; } if (ZigClangType_getTypeClass(ty) == .FunctionNoProto) { return ClangFunctionType{ .NoProto = @ptrCast(*const ZigClangFunctionType, ty) }; } return null; } fn transUnaryExprOrTypeTraitExpr( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangUnaryExprOrTypeTraitExpr, result_used: ResultUsed, ) TransError!*ast.Node { const type_node = try transQualType( rp, ZigClangUnaryExprOrTypeTraitExpr_getTypeOfArgument(stmt), ZigClangUnaryExprOrTypeTraitExpr_getBeginLoc(stmt), ); const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@sizeOf"); try builtin_node.params.push(type_node); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return maybeSuppressResult(rp, scope, result_used, &builtin_node.base); } fn qualTypeHasWrappingOverflow(qt: ZigClangQualType) bool { if (cIsUnsignedInteger(qt)) { // unsigned integer overflow wraps around. return true; } else { // float, signed integer, and pointer overflow is undefined behavior. return false; } } fn transUnaryOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangUnaryOperator, used: ResultUsed) TransError!*ast.Node { const op_expr = ZigClangUnaryOperator_getSubExpr(stmt); switch (ZigClangUnaryOperator_getOpcode(stmt)) { .PostInc => if (qualTypeHasWrappingOverflow(ZigClangUnaryOperator_getType(stmt))) return transCreatePostCrement(rp, scope, stmt, .AssignAddWrap, .PlusPercentEqual, "+%=", used) else return transCreatePostCrement(rp, scope, stmt, .AssignAdd, .PlusEqual, "+=", used), .PostDec => if (qualTypeHasWrappingOverflow(ZigClangUnaryOperator_getType(stmt))) return transCreatePostCrement(rp, scope, stmt, .AssignSubWrap, .MinusPercentEqual, "-%=", used) else return transCreatePostCrement(rp, scope, stmt, .AssignSub, .MinusEqual, "-=", used), .PreInc => if (qualTypeHasWrappingOverflow(ZigClangUnaryOperator_getType(stmt))) return transCreatePreCrement(rp, scope, stmt, .AssignAddWrap, .PlusPercentEqual, "+%=", used) else return transCreatePreCrement(rp, scope, stmt, .AssignAdd, .PlusEqual, "+=", used), .PreDec => if (qualTypeHasWrappingOverflow(ZigClangUnaryOperator_getType(stmt))) return transCreatePreCrement(rp, scope, stmt, .AssignSubWrap, .MinusPercentEqual, "-%=", used) else return transCreatePreCrement(rp, scope, stmt, .AssignSub, .MinusEqual, "-=", used), .AddrOf => { const op_node = try transCreateNodePrefixOp(rp.c, .AddressOf, .Ampersand, "&"); op_node.rhs = try transExpr(rp, scope, op_expr, used, .r_value); return &op_node.base; }, .Deref => { const value_node = try transExpr(rp, scope, op_expr, used, .r_value); var is_ptr = false; const fn_ty = qualTypeGetFnProto(ZigClangExpr_getType(op_expr), &is_ptr); if (fn_ty != null and is_ptr) return value_node; const unwrapped = try transCreateNodeUnwrapNull(rp.c, value_node); return transCreateNodePtrDeref(rp.c, unwrapped); }, .Plus => return transExpr(rp, scope, op_expr, used, .r_value), .Minus => { if (!qualTypeHasWrappingOverflow(ZigClangExpr_getType(op_expr))) { const op_node = try transCreateNodePrefixOp(rp.c, .Negation, .Minus, "-"); op_node.rhs = try transExpr(rp, scope, op_expr, .used, .r_value); return &op_node.base; } else if (cIsUnsignedInteger(ZigClangExpr_getType(op_expr))) { // we gotta emit 0 -% x const zero = try transCreateNodeInt(rp.c, 0); const token = try appendToken(rp.c, .MinusPercent, "-%"); const expr = try transExpr(rp, scope, op_expr, .used, .r_value); return transCreateNodeInfixOp(rp, scope, zero, .SubWrap, token, expr, used, true); } else return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangUnaryOperator_getBeginLoc(stmt), "C negation with non float non integer", .{}); }, .Not => { const op_node = try transCreateNodePrefixOp(rp.c, .BitNot, .Tilde, "~"); op_node.rhs = try transExpr(rp, scope, op_expr, .used, .r_value); return &op_node.base; }, .LNot => { const op_node = try transCreateNodePrefixOp(rp.c, .BoolNot, .Bang, "!"); op_node.rhs = try transBoolExpr(rp, scope, op_expr, .used, .r_value, true); return &op_node.base; }, .Extension => { return transExpr(rp, scope, ZigClangUnaryOperator_getSubExpr(stmt), used, .l_value); }, else => return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangUnaryOperator_getBeginLoc(stmt), "unsupported C translation {}", .{ZigClangUnaryOperator_getOpcode(stmt)}), } } fn transCreatePreCrement( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangUnaryOperator, op: ast.Node.InfixOp.Op, op_tok_id: std.zig.Token.Id, bytes: []const u8, used: ResultUsed, ) TransError!*ast.Node { const op_expr = ZigClangUnaryOperator_getSubExpr(stmt); if (used == .unused) { // common case // c: ++expr // zig: expr += 1 const expr = try transExpr(rp, scope, op_expr, .used, .r_value); const token = try appendToken(rp.c, op_tok_id, bytes); const one = try transCreateNodeInt(rp.c, 1); _ = try appendToken(rp.c, .Semicolon, ";"); return transCreateNodeInfixOp(rp, scope, expr, op, token, one, .used, false); } // worst case // c: ++expr // zig: (blk: { // zig: const _ref = &expr; // zig: _ref.* += 1; // zig: break :blk _ref.* // zig: }) const block_scope = try Scope.Block.init(rp.c, scope, "blk"); block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label); const ref = try block_scope.makeMangledName(rp.c, "ref"); const node = try transCreateNodeVarDecl(rp.c, false, true, ref); node.eq_token = try appendToken(rp.c, .Equal, "="); const rhs_node = try transCreateNodePrefixOp(rp.c, .AddressOf, .Ampersand, "&"); rhs_node.rhs = try transExpr(rp, scope, op_expr, .used, .r_value); node.init_node = &rhs_node.base; node.semicolon_token = try appendToken(rp.c, .Semicolon, ";"); try block_scope.block_node.statements.push(&node.base); const lhs_node = try transCreateNodeIdentifier(rp.c, ref); const ref_node = try transCreateNodePtrDeref(rp.c, lhs_node); _ = try appendToken(rp.c, .Semicolon, ";"); const token = try appendToken(rp.c, op_tok_id, bytes); const one = try transCreateNodeInt(rp.c, 1); _ = try appendToken(rp.c, .Semicolon, ";"); const assign = try transCreateNodeInfixOp(rp, scope, ref_node, op, token, one, .used, false); try block_scope.block_node.statements.push(assign); const break_node = try transCreateNodeBreak(rp.c, block_scope.label); break_node.rhs = ref_node; try block_scope.block_node.statements.push(&break_node.base); block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); // semicolon must immediately follow rbrace because it is the last token in a block _ = try appendToken(rp.c, .Semicolon, ";"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = try appendToken(rp.c, .LParen, "("), .expr = &block_scope.block_node.base, .rparen = try appendToken(rp.c, .RParen, ")"), }; return &grouped_expr.base; } fn transCreatePostCrement( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangUnaryOperator, op: ast.Node.InfixOp.Op, op_tok_id: std.zig.Token.Id, bytes: []const u8, used: ResultUsed, ) TransError!*ast.Node { const op_expr = ZigClangUnaryOperator_getSubExpr(stmt); if (used == .unused) { // common case // c: ++expr // zig: expr += 1 const expr = try transExpr(rp, scope, op_expr, .used, .r_value); const token = try appendToken(rp.c, op_tok_id, bytes); const one = try transCreateNodeInt(rp.c, 1); _ = try appendToken(rp.c, .Semicolon, ";"); return transCreateNodeInfixOp(rp, scope, expr, op, token, one, .used, false); } // worst case // c: expr++ // zig: (blk: { // zig: const _ref = &expr; // zig: const _tmp = _ref.*; // zig: _ref.* += 1; // zig: break :blk _tmp // zig: }) const block_scope = try Scope.Block.init(rp.c, scope, "blk"); block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label); const ref = try block_scope.makeMangledName(rp.c, "ref"); const node = try transCreateNodeVarDecl(rp.c, false, true, ref); node.eq_token = try appendToken(rp.c, .Equal, "="); const rhs_node = try transCreateNodePrefixOp(rp.c, .AddressOf, .Ampersand, "&"); rhs_node.rhs = try transExpr(rp, scope, op_expr, .used, .r_value); node.init_node = &rhs_node.base; node.semicolon_token = try appendToken(rp.c, .Semicolon, ";"); try block_scope.block_node.statements.push(&node.base); const lhs_node = try transCreateNodeIdentifier(rp.c, ref); const ref_node = try transCreateNodePtrDeref(rp.c, lhs_node); _ = try appendToken(rp.c, .Semicolon, ";"); const tmp = try block_scope.makeMangledName(rp.c, "tmp"); const tmp_node = try transCreateNodeVarDecl(rp.c, false, true, tmp); tmp_node.eq_token = try appendToken(rp.c, .Equal, "="); tmp_node.init_node = ref_node; tmp_node.semicolon_token = try appendToken(rp.c, .Semicolon, ";"); try block_scope.block_node.statements.push(&tmp_node.base); const token = try appendToken(rp.c, op_tok_id, bytes); const one = try transCreateNodeInt(rp.c, 1); _ = try appendToken(rp.c, .Semicolon, ";"); const assign = try transCreateNodeInfixOp(rp, scope, ref_node, op, token, one, .used, false); try block_scope.block_node.statements.push(assign); const break_node = try transCreateNodeBreak(rp.c, block_scope.label); break_node.rhs = try transCreateNodeIdentifier(rp.c, tmp); try block_scope.block_node.statements.push(&break_node.base); _ = try appendToken(rp.c, .Semicolon, ";"); block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = try appendToken(rp.c, .LParen, "("), .expr = &block_scope.block_node.base, .rparen = try appendToken(rp.c, .RParen, ")"), }; return &grouped_expr.base; } fn transCompoundAssignOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundAssignOperator, used: ResultUsed) TransError!*ast.Node { switch (ZigClangCompoundAssignOperator_getOpcode(stmt)) { .MulAssign => if (qualTypeHasWrappingOverflow(ZigClangCompoundAssignOperator_getType(stmt))) return transCreateCompoundAssign(rp, scope, stmt, .AssignMulWrap, .AsteriskPercentEqual, "*%=", .MulWrap, .AsteriskPercent, "*%", used) else return transCreateCompoundAssign(rp, scope, stmt, .AssignMul, .AsteriskEqual, "*=", .Mul, .Asterisk, "*", used), .AddAssign => if (qualTypeHasWrappingOverflow(ZigClangCompoundAssignOperator_getType(stmt))) return transCreateCompoundAssign(rp, scope, stmt, .AssignAddWrap, .PlusPercentEqual, "+%=", .AddWrap, .PlusPercent, "+%", used) else return transCreateCompoundAssign(rp, scope, stmt, .AssignAdd, .PlusEqual, "+=", .Add, .Plus, "+", used), .SubAssign => if (qualTypeHasWrappingOverflow(ZigClangCompoundAssignOperator_getType(stmt))) return transCreateCompoundAssign(rp, scope, stmt, .AssignSubWrap, .MinusPercentEqual, "-%=", .SubWrap, .MinusPercent, "-%", used) else return transCreateCompoundAssign(rp, scope, stmt, .AssignSub, .MinusPercentEqual, "-=", .Sub, .Minus, "-", used), .ShlAssign => return transCreateCompoundAssign(rp, scope, stmt, .AssignBitShiftLeft, .AngleBracketAngleBracketLeftEqual, "<<=", .BitShiftLeft, .AngleBracketAngleBracketLeft, "<<", used), .ShrAssign => return transCreateCompoundAssign(rp, scope, stmt, .AssignBitShiftRight, .AngleBracketAngleBracketRightEqual, ">>=", .BitShiftRight, .AngleBracketAngleBracketRight, ">>", used), .AndAssign => return transCreateCompoundAssign(rp, scope, stmt, .AssignBitAnd, .AmpersandEqual, "&=", .BitAnd, .Ampersand, "&", used), .XorAssign => return transCreateCompoundAssign(rp, scope, stmt, .AssignBitXor, .CaretEqual, "^=", .BitXor, .Caret, "^", used), .OrAssign => return transCreateCompoundAssign(rp, scope, stmt, .AssignBitOr, .PipeEqual, "|=", .BitOr, .Pipe, "|", used), else => return revertAndWarn( rp, error.UnsupportedTranslation, ZigClangCompoundAssignOperator_getBeginLoc(stmt), "unsupported C translation {}", .{ZigClangCompoundAssignOperator_getOpcode(stmt)}, ), } } fn transCreateCompoundAssign( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundAssignOperator, assign_op: ast.Node.InfixOp.Op, assign_tok_id: std.zig.Token.Id, assign_bytes: []const u8, bin_op: ast.Node.InfixOp.Op, bin_tok_id: std.zig.Token.Id, bin_bytes: []const u8, used: ResultUsed, ) TransError!*ast.Node { const is_shift = bin_op == .BitShiftLeft or bin_op == .BitShiftRight; const lhs = ZigClangCompoundAssignOperator_getLHS(stmt); const rhs = ZigClangCompoundAssignOperator_getRHS(stmt); const loc = ZigClangCompoundAssignOperator_getBeginLoc(stmt); if (used == .unused) { // common case // c: lhs += rhs // zig: lhs += rhs const lhs_node = try transExpr(rp, scope, lhs, .used, .l_value); const eq_token = try appendToken(rp.c, assign_tok_id, assign_bytes); var rhs_node = if (is_shift) try transExprCoercing(rp, scope, rhs, .used, .r_value) else try transExpr(rp, scope, rhs, .used, .r_value); if (is_shift) { const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc); try as_node.params.push(rhs_type); _ = try appendToken(rp.c, .Comma, ","); try as_node.params.push(rhs_node); as_node.rparen_token = try appendToken(rp.c, .RParen, ")"); rhs_node = &as_node.base; } if (scope.id != .Condition) _ = try appendToken(rp.c, .Semicolon, ";"); return transCreateNodeInfixOp(rp, scope, lhs_node, assign_op, eq_token, rhs_node, .used, false); } // worst case // c: lhs += rhs // zig: (blk: { // zig: const _ref = &lhs; // zig: _ref.* = _ref.* + rhs; // zig: break :blk _ref.* // zig: }) const block_scope = try Scope.Block.init(rp.c, scope, "blk"); block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label); const ref = try block_scope.makeMangledName(rp.c, "ref"); const node = try transCreateNodeVarDecl(rp.c, false, true, ref); node.eq_token = try appendToken(rp.c, .Equal, "="); const addr_node = try transCreateNodePrefixOp(rp.c, .AddressOf, .Ampersand, "&"); addr_node.rhs = try transExpr(rp, scope, lhs, .used, .l_value); node.init_node = &addr_node.base; node.semicolon_token = try appendToken(rp.c, .Semicolon, ";"); try block_scope.block_node.statements.push(&node.base); const lhs_node = try transCreateNodeIdentifier(rp.c, ref); const ref_node = try transCreateNodePtrDeref(rp.c, lhs_node); _ = try appendToken(rp.c, .Semicolon, ";"); const bin_token = try appendToken(rp.c, bin_tok_id, bin_bytes); var rhs_node = try transExpr(rp, scope, rhs, .used, .r_value); if (is_shift) { const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc); try as_node.params.push(rhs_type); _ = try appendToken(rp.c, .Comma, ","); try as_node.params.push(rhs_node); as_node.rparen_token = try appendToken(rp.c, .RParen, ")"); rhs_node = &as_node.base; } const rhs_bin = try transCreateNodeInfixOp(rp, scope, ref_node, bin_op, bin_token, rhs_node, .used, false); _ = try appendToken(rp.c, .Semicolon, ";"); const eq_token = try appendToken(rp.c, .Equal, "="); const assign = try transCreateNodeInfixOp(rp, scope, ref_node, .Assign, eq_token, rhs_bin, .used, false); try block_scope.block_node.statements.push(assign); const break_node = try transCreateNodeBreak(rp.c, block_scope.label); break_node.rhs = ref_node; try block_scope.block_node.statements.push(&break_node.base); block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); // semicolon must immediately follow rbrace because it is the last token in a block _ = try appendToken(rp.c, .Semicolon, ";"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = try appendToken(rp.c, .LParen, "("), .expr = &block_scope.block_node.base, .rparen = try appendToken(rp.c, .RParen, ")"), }; return &grouped_expr.base; } fn transCPtrCast( rp: RestorePoint, loc: ZigClangSourceLocation, dst_type: ZigClangQualType, src_type: ZigClangQualType, expr: *ast.Node, ) !*ast.Node { const ty = ZigClangQualType_getTypePtr(dst_type); const child_type = ZigClangType_getPointeeType(ty); // Implicit downcasting from higher to lower alignment values is forbidden, // use @alignCast to side-step this problem const ptrcast_node = try transCreateNodeBuiltinFnCall(rp.c, "@ptrCast"); const dst_type_node = try transType(rp, ty, loc); try ptrcast_node.params.push(dst_type_node); _ = try appendToken(rp.c, .Comma, ","); if (ZigClangType_isVoidType(qualTypeCanon(child_type))) { // void has 1-byte alignment, so @alignCast is not needed try ptrcast_node.params.push(expr); } else { const aligncast_node = try transCreateNodeBuiltinFnCall(rp.c, "@alignCast"); const alignof_node = try transCreateNodeBuiltinFnCall(rp.c, "@alignOf"); const child_type_node = try transQualType(rp, child_type, loc); try alignof_node.params.push(child_type_node); alignof_node.rparen_token = try appendToken(rp.c, .RParen, ")"); try aligncast_node.params.push(&alignof_node.base); _ = try appendToken(rp.c, .Comma, ","); try aligncast_node.params.push(expr); aligncast_node.rparen_token = try appendToken(rp.c, .RParen, ")"); try ptrcast_node.params.push(&aligncast_node.base); } ptrcast_node.rparen_token = try appendToken(rp.c, .RParen, ")"); return &ptrcast_node.base; } fn transBreak(rp: RestorePoint, scope: *Scope) TransError!*ast.Node { const break_scope = scope.getBreakableScope(); const br = try transCreateNodeBreak(rp.c, if (break_scope.id == .Switch) "__switch" else null); _ = try appendToken(rp.c, .Semicolon, ";"); return &br.base; } fn transFloatingLiteral(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangFloatingLiteral, used: ResultUsed) TransError!*ast.Node { // TODO use something more accurate const dbl = ZigClangAPFloat_getValueAsApproximateDouble(stmt); const node = try rp.c.a().create(ast.Node.FloatLiteral); node.* = .{ .token = try appendTokenFmt(rp.c, .FloatLiteral, "{d}", .{dbl}), }; return maybeSuppressResult(rp, scope, used, &node.base); } fn transConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangConditionalOperator, used: ResultUsed) TransError!*ast.Node { const grouped = scope.id == .Condition; const lparen = if (grouped) try appendToken(rp.c, .LParen, "(") else undefined; const if_node = try transCreateNodeIf(rp.c); var cond_scope = Scope{ .parent = scope, .id = .Condition, }; const cond_expr = ZigClangConditionalOperator_getCond(stmt); const true_expr = ZigClangConditionalOperator_getTrueExpr(stmt); const false_expr = ZigClangConditionalOperator_getFalseExpr(stmt); if_node.condition = try transBoolExpr(rp, &cond_scope, cond_expr, .used, .r_value, false); _ = try appendToken(rp.c, .RParen, ")"); if_node.body = try transExpr(rp, scope, true_expr, .used, .r_value); if_node.@"else" = try transCreateNodeElse(rp.c); if_node.@"else".?.body = try transExpr(rp, scope, false_expr, .used, .r_value); if (grouped) { const rparen = try appendToken(rp.c, .RParen, ")"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = lparen, .expr = &if_node.base, .rparen = rparen, }; return maybeSuppressResult(rp, scope, used, &grouped_expr.base); } else { return maybeSuppressResult(rp, scope, used, &if_node.base); } } fn maybeSuppressResult( rp: RestorePoint, scope: *Scope, used: ResultUsed, result: *ast.Node, ) TransError!*ast.Node { if (used == .used) return result; if (scope.id != .Condition) { // NOTE: This is backwards, but the semicolon must immediately follow the node. _ = try appendToken(rp.c, .Semicolon, ";"); } else { // TODO is there a way to avoid this hack? // this parenthesis must come immediately following the node _ = try appendToken(rp.c, .RParen, ")"); // these need to come before _ _ = try appendToken(rp.c, .Colon, ":"); _ = try appendToken(rp.c, .LParen, "("); } const lhs = try transCreateNodeIdentifier(rp.c, "_"); const op_token = try appendToken(rp.c, .Equal, "="); const op_node = try rp.c.a().create(ast.Node.InfixOp); op_node.* = .{ .op_token = op_token, .lhs = lhs, .op = .Assign, .rhs = result, }; return &op_node.base; } fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: *ast.Node) !void { try c.tree.root_node.decls.push(decl_node); _ = try c.global_scope.sym_table.put(name, decl_node); } fn transQualType(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigClangSourceLocation) TypeError!*ast.Node { return transType(rp, ZigClangQualType_getTypePtr(qt), source_loc); } /// Produces a Zig AST node by translating a Clang QualType, respecting the width, but modifying the signed-ness. /// Asserts the type is an integer. fn transQualTypeIntWidthOf(c: *Context, ty: ZigClangQualType, is_signed: bool) TypeError!*ast.Node { return transTypeIntWidthOf(c, qualTypeCanon(ty), is_signed); } /// Produces a Zig AST node by translating a Clang Type, respecting the width, but modifying the signed-ness. /// Asserts the type is an integer. fn transTypeIntWidthOf(c: *Context, ty: *const ZigClangType, is_signed: bool) TypeError!*ast.Node { assert(ZigClangType_getTypeClass(ty) == .Builtin); const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); return transCreateNodeIdentifier(c, switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Char_U, .Char_S, .UChar, .SChar, .Char8 => if (is_signed) "i8" else "u8", .UShort, .Short => if (is_signed) "c_short" else "c_ushort", .UInt, .Int => if (is_signed) "c_int" else "c_uint", .ULong, .Long => if (is_signed) "c_long" else "c_ulong", .ULongLong, .LongLong => if (is_signed) "c_longlong" else "c_ulonglong", .UInt128, .Int128 => if (is_signed) "i128" else "u128", .Char16 => if (is_signed) "i16" else "u16", .Char32 => if (is_signed) "i32" else "u32", else => unreachable, // only call this function when it has already been determined the type is int }); } fn isCBuiltinType(qt: ZigClangQualType, kind: ZigClangBuiltinTypeKind) bool { const c_type = qualTypeCanon(qt); if (ZigClangType_getTypeClass(c_type) != .Builtin) return false; const builtin_ty = @ptrCast(*const ZigClangBuiltinType, c_type); return ZigClangBuiltinType_getKind(builtin_ty) == kind; } fn qualTypeIsPtr(qt: ZigClangQualType) bool { return ZigClangType_getTypeClass(qualTypeCanon(qt)) == .Pointer; } fn qualTypeIntBitWidth(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigClangSourceLocation) !u32 { const ty = ZigClangQualType_getTypePtr(qt); switch (ZigClangType_getTypeClass(ty)) { .Builtin => { const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Char_U, .UChar, .Char_S, .SChar, => return 8, .UInt128, .Int128, => return 128, else => return 0, } unreachable; }, .Typedef => { const typedef_ty = @ptrCast(*const ZigClangTypedefType, ty); const typedef_decl = ZigClangTypedefType_getDecl(typedef_ty); const type_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, typedef_decl))); if (mem.eql(u8, type_name, "uint8_t") or mem.eql(u8, type_name, "int8_t")) { return 8; } else if (mem.eql(u8, type_name, "uint16_t") or mem.eql(u8, type_name, "int16_t")) { return 16; } else if (mem.eql(u8, type_name, "uint32_t") or mem.eql(u8, type_name, "int32_t")) { return 32; } else if (mem.eql(u8, type_name, "uint64_t") or mem.eql(u8, type_name, "int64_t")) { return 64; } else { return 0; } }, else => return 0, } unreachable; } fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigClangSourceLocation) !*ast.Node { const int_bit_width = try qualTypeIntBitWidth(rp, qt, source_loc); if (int_bit_width != 0) { // we can perform the log2 now. const cast_bit_width = math.log2_int(u64, int_bit_width); const node = try rp.c.a().create(ast.Node.IntegerLiteral); node.* = ast.Node.IntegerLiteral{ .token = try appendTokenFmt(rp.c, .Identifier, "u{}", .{cast_bit_width}), }; return &node.base; } const zig_type_node = try transQualType(rp, qt, source_loc); // @import("std").math.Log2Int(c_long); // // FnCall // FieldAccess // FieldAccess // FnCall (.builtin = true) // Symbol "import" // StringLiteral "std" // Symbol "math" // Symbol "Log2Int" // Symbol <zig_type_node> (var from above) const import_fn_call = try transCreateNodeBuiltinFnCall(rp.c, "@import"); const std_token = try appendToken(rp.c, .StringLiteral, "\"std\""); const std_node = try rp.c.a().create(ast.Node.StringLiteral); std_node.* = ast.Node.StringLiteral{ .token = std_token, }; try import_fn_call.params.push(&std_node.base); import_fn_call.rparen_token = try appendToken(rp.c, .RParen, ")"); const inner_field_access = try transCreateNodeFieldAccess(rp.c, &import_fn_call.base, "math"); const outer_field_access = try transCreateNodeFieldAccess(rp.c, inner_field_access, "Log2Int"); const log2int_fn_call = try transCreateNodeFnCall(rp.c, outer_field_access); try @fieldParentPtr(ast.Node.SuffixOp, "base", &log2int_fn_call.base).op.Call.params.push(zig_type_node); log2int_fn_call.rtoken = try appendToken(rp.c, .RParen, ")"); return &log2int_fn_call.base; } fn qualTypeChildIsFnProto(qt: ZigClangQualType) bool { const ty = ZigClangQualType_getTypePtr(qt); switch (ZigClangType_getTypeClass(ty)) { .FunctionProto, .FunctionNoProto => return true, .Elaborated => { const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ty); return qualTypeChildIsFnProto(ZigClangElaboratedType_getNamedType(elaborated_ty)); }, .Typedef => { const typedef_ty = @ptrCast(*const ZigClangTypedefType, ty); const typedef_decl = ZigClangTypedefType_getDecl(typedef_ty); return qualTypeChildIsFnProto(ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl)); }, .Paren => { const paren_type = @ptrCast(*const ZigClangParenType, ty); const inner_type = ZigClangParenType_getInnerType(paren_type); switch (ZigClangQualType_getTypeClass(inner_type)) { .FunctionProto, .FunctionNoProto => return true, else => return false, } }, .Attributed => { const attr_type = @ptrCast(*const ZigClangAttributedType, ty); return qualTypeChildIsFnProto(ZigClangAttributedType_getEquivalentType(attr_type)); }, else => return false, } } fn qualTypeCanon(qt: ZigClangQualType) *const ZigClangType { const canon = ZigClangQualType_getCanonicalType(qt); return ZigClangQualType_getTypePtr(canon); } fn getExprQualType(c: *Context, expr: *const ZigClangExpr) ZigClangQualType { blk: { // If this is a C `char *`, turn it into a `const char *` if (ZigClangExpr_getStmtClass(expr) != .ImplicitCastExprClass) break :blk; const cast_expr = @ptrCast(*const ZigClangImplicitCastExpr, expr); if (ZigClangImplicitCastExpr_getCastKind(cast_expr) != .ArrayToPointerDecay) break :blk; const sub_expr = ZigClangImplicitCastExpr_getSubExpr(cast_expr); if (ZigClangExpr_getStmtClass(sub_expr) != .StringLiteralClass) break :blk; const array_qt = ZigClangExpr_getType(sub_expr); const array_type = @ptrCast(*const ZigClangArrayType, ZigClangQualType_getTypePtr(array_qt)); var pointee_qt = ZigClangArrayType_getElementType(array_type); ZigClangQualType_addConst(&pointee_qt); return ZigClangASTContext_getPointerType(c.clang_context, pointee_qt); } return ZigClangExpr_getType(expr); } fn getExprQualTypeBeforeImplicitCast(c: *Context, expr: *const ZigClangExpr) ZigClangQualType { if (ZigClangExpr_getStmtClass(expr) == .ImplicitCastExprClass) { const cast_expr = @ptrCast(*const ZigClangImplicitCastExpr, expr); return getExprQualType(c, ZigClangImplicitCastExpr_getSubExpr(cast_expr)); } return ZigClangExpr_getType(expr); } fn typeIsOpaque(c: *Context, ty: *const ZigClangType, loc: ZigClangSourceLocation) bool { switch (ZigClangType_getTypeClass(ty)) { .Builtin => { const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); return ZigClangBuiltinType_getKind(builtin_ty) == .Void; }, .Record => { const record_ty = @ptrCast(*const ZigClangRecordType, ty); const record_decl = ZigClangRecordType_getDecl(record_ty); const record_def = ZigClangRecordDecl_getDefinition(record_decl) orelse return true; var it = ZigClangRecordDecl_field_begin(record_def); const end_it = ZigClangRecordDecl_field_end(record_def); while (ZigClangRecordDecl_field_iterator_neq(it, end_it)) : (it = ZigClangRecordDecl_field_iterator_next(it)) { const field_decl = ZigClangRecordDecl_field_iterator_deref(it); if (ZigClangFieldDecl_isBitField(field_decl)) { return true; } } return false; }, .Elaborated => { const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ty); const qt = ZigClangElaboratedType_getNamedType(elaborated_ty); return typeIsOpaque(c, ZigClangQualType_getTypePtr(qt), loc); }, .Typedef => { const typedef_ty = @ptrCast(*const ZigClangTypedefType, ty); const typedef_decl = ZigClangTypedefType_getDecl(typedef_ty); const underlying_type = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl); return typeIsOpaque(c, ZigClangQualType_getTypePtr(underlying_type), loc); }, else => return false, } } fn cIsInteger(qt: ZigClangQualType) bool { return cIsSignedInteger(qt) or cIsUnsignedInteger(qt); } fn cIsUnsignedInteger(qt: ZigClangQualType) bool { const c_type = qualTypeCanon(qt); if (ZigClangType_getTypeClass(c_type) != .Builtin) return false; const builtin_ty = @ptrCast(*const ZigClangBuiltinType, c_type); return switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Char_U, .UChar, .Char_S, .UShort, .UInt, .ULong, .ULongLong, .UInt128, .WChar_U, => true, else => false, }; } fn cIntTypeToIndex(qt: ZigClangQualType) u8 { const c_type = qualTypeCanon(qt); assert(ZigClangType_getTypeClass(c_type) == .Builtin); const builtin_ty = @ptrCast(*const ZigClangBuiltinType, c_type); return switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Bool, .Char_U, .Char_S, .UChar, .SChar, .Char8 => 1, .WChar_U, .WChar_S => 2, .UShort, .Short, .Char16 => 3, .UInt, .Int, .Char32 => 4, .ULong, .Long => 5, .ULongLong, .LongLong => 6, .UInt128, .Int128 => 7, else => unreachable, }; } fn cIntTypeCmp(a: ZigClangQualType, b: ZigClangQualType) math.Order { const a_index = cIntTypeToIndex(a); const b_index = cIntTypeToIndex(b); return math.order(a_index, b_index); } fn cIsSignedInteger(qt: ZigClangQualType) bool { const c_type = qualTypeCanon(qt); if (ZigClangType_getTypeClass(c_type) != .Builtin) return false; const builtin_ty = @ptrCast(*const ZigClangBuiltinType, c_type); return switch (ZigClangBuiltinType_getKind(builtin_ty)) { .SChar, .Short, .Int, .Long, .LongLong, .Int128, .WChar_S, => true, else => false, }; } fn cIsFloating(qt: ZigClangQualType) bool { const c_type = qualTypeCanon(qt); if (ZigClangType_getTypeClass(c_type) != .Builtin) return false; const builtin_ty = @ptrCast(*const ZigClangBuiltinType, c_type); return switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Float, .Double, .Float128, .LongDouble, => true, else => false, }; } fn transCreateNodeAssign( rp: RestorePoint, scope: *Scope, result_used: ResultUsed, lhs: *const ZigClangExpr, rhs: *const ZigClangExpr, ) !*ast.Node { // common case // c: lhs = rhs // zig: lhs = rhs if (result_used == .unused) { const lhs_node = try transExpr(rp, scope, lhs, .used, .l_value); const eq_token = try appendToken(rp.c, .Equal, "="); var rhs_node = try transExprCoercing(rp, scope, rhs, .used, .r_value); if (isBoolRes(rhs_node)) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@boolToInt"); try builtin_node.params.push(rhs_node); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); rhs_node = &builtin_node.base; } if (scope.id != .Condition) _ = try appendToken(rp.c, .Semicolon, ";"); return transCreateNodeInfixOp(rp, scope, lhs_node, .Assign, eq_token, rhs_node, .used, false); } // worst case // c: lhs = rhs // zig: (blk: { // zig: const _tmp = rhs; // zig: lhs = _tmp; // zig: break :blk _tmp // zig: }) const block_scope = try Scope.Block.init(rp.c, scope, "blk"); block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label); const tmp = try block_scope.makeMangledName(rp.c, "tmp"); const node = try transCreateNodeVarDecl(rp.c, false, true, tmp); node.eq_token = try appendToken(rp.c, .Equal, "="); var rhs_node = try transExpr(rp, &block_scope.base, rhs, .used, .r_value); if (isBoolRes(rhs_node)) { const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@boolToInt"); try builtin_node.params.push(rhs_node); builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); rhs_node = &builtin_node.base; } node.init_node = rhs_node; node.semicolon_token = try appendToken(rp.c, .Semicolon, ";"); try block_scope.block_node.statements.push(&node.base); const lhs_node = try transExpr(rp, &block_scope.base, lhs, .used, .l_value); const eq_token = try appendToken(rp.c, .Equal, "="); const ident = try transCreateNodeIdentifier(rp.c, tmp); _ = try appendToken(rp.c, .Semicolon, ";"); const assign = try transCreateNodeInfixOp(rp, &block_scope.base, lhs_node, .Assign, eq_token, ident, .used, false); try block_scope.block_node.statements.push(assign); const break_node = try transCreateNodeBreak(rp.c, block_scope.label); break_node.rhs = try transCreateNodeIdentifier(rp.c, tmp); _ = try appendToken(rp.c, .Semicolon, ";"); try block_scope.block_node.statements.push(&break_node.base); block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); // semicolon must immediately follow rbrace because it is the last token in a block _ = try appendToken(rp.c, .Semicolon, ";"); return &block_scope.block_node.base; } fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.BuiltinCall { const builtin_token = try appendToken(c, .Builtin, name); _ = try appendToken(c, .LParen, "("); const node = try c.a().create(ast.Node.BuiltinCall); node.* = .{ .builtin_token = builtin_token, .params = ast.Node.BuiltinCall.ParamList.init(c.a()), .rparen_token = undefined, // set after appending args }; return node; } fn transCreateNodeFnCall(c: *Context, fn_expr: *ast.Node) !*ast.Node.SuffixOp { _ = try appendToken(c, .LParen, "("); const node = try c.a().create(ast.Node.SuffixOp); node.* = .{ .lhs = .{ .node = fn_expr }, .op = .{ .Call = .{ .params = ast.Node.SuffixOp.Op.Call.ParamList.init(c.a()), .async_token = null, }, }, .rtoken = undefined, // set after appending args }; return node; } fn transCreateNodeFieldAccess(c: *Context, container: *ast.Node, field_name: []const u8) !*ast.Node { const field_access_node = try c.a().create(ast.Node.InfixOp); field_access_node.* = .{ .op_token = try appendToken(c, .Period, "."), .lhs = container, .op = .Period, .rhs = try transCreateNodeIdentifier(c, field_name), }; return &field_access_node.base; } fn transCreateNodePrefixOp( c: *Context, op: ast.Node.PrefixOp.Op, op_tok_id: std.zig.Token.Id, bytes: []const u8, ) !*ast.Node.PrefixOp { const node = try c.a().create(ast.Node.PrefixOp); node.* = ast.Node.PrefixOp{ .op_token = try appendToken(c, op_tok_id, bytes), .op = op, .rhs = undefined, // translate and set afterward }; return node; } fn transCreateNodeInfixOp( rp: RestorePoint, scope: *Scope, lhs_node: *ast.Node, op: ast.Node.InfixOp.Op, op_token: ast.TokenIndex, rhs_node: *ast.Node, used: ResultUsed, grouped: bool, ) !*ast.Node { var lparen = if (grouped) try appendToken(rp.c, .LParen, "(") else null; const node = try rp.c.a().create(ast.Node.InfixOp); node.* = .{ .op_token = op_token, .lhs = lhs_node, .op = op, .rhs = rhs_node, }; if (!grouped) return maybeSuppressResult(rp, scope, used, &node.base); const rparen = try appendToken(rp.c, .RParen, ")"); const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); grouped_expr.* = .{ .lparen = lparen.?, .expr = &node.base, .rparen = rparen, }; return maybeSuppressResult(rp, scope, used, &grouped_expr.base); } fn transCreateNodeBoolInfixOp( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangBinaryOperator, op: ast.Node.InfixOp.Op, used: ResultUsed, grouped: bool, ) !*ast.Node { std.debug.assert(op == .BoolAnd or op == .BoolOr); const lhs_hode = try transBoolExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value, true); const op_token = if (op == .BoolAnd) try appendToken(rp.c, .Keyword_and, "and") else try appendToken(rp.c, .Keyword_or, "or"); const rhs = try transBoolExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value, true); return transCreateNodeInfixOp( rp, scope, lhs_hode, op, op_token, rhs, used, grouped, ); } fn transCreateNodePtrType( c: *Context, is_const: bool, is_volatile: bool, op_tok_id: std.zig.Token.Id, ) !*ast.Node.PrefixOp { const node = try c.a().create(ast.Node.PrefixOp); const op_token = switch (op_tok_id) { .LBracket => blk: { const lbracket = try appendToken(c, .LBracket, "["); _ = try appendToken(c, .Asterisk, "*"); _ = try appendToken(c, .RBracket, "]"); break :blk lbracket; }, .Identifier => blk: { const lbracket = try appendToken(c, .LBracket, "["); // Rendering checks if this token + 2 == .Identifier, so needs to return this token _ = try appendToken(c, .Asterisk, "*"); _ = try appendIdentifier(c, "c"); _ = try appendToken(c, .RBracket, "]"); break :blk lbracket; }, .Asterisk => try appendToken(c, .Asterisk, "*"), else => unreachable, }; node.* = .{ .op_token = op_token, .op = .{ .PtrType = .{ .const_token = if (is_const) try appendToken(c, .Keyword_const, "const") else null, .volatile_token = if (is_volatile) try appendToken(c, .Keyword_volatile, "volatile") else null, }, }, .rhs = undefined, // translate and set afterward }; return node; } fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node { const num_limbs = ZigClangAPSInt_getNumWords(int); var aps_int = int; const is_negative = ZigClangAPSInt_isSigned(int) and ZigClangAPSInt_isNegative(int); if (is_negative) aps_int = ZigClangAPSInt_negate(aps_int); var big = try math.big.Int.initCapacity(c.a(), num_limbs); if (is_negative) big.negate(); defer big.deinit(); const data = ZigClangAPSInt_getRawData(aps_int); var i: @TypeOf(num_limbs) = 0; while (i < num_limbs) : (i += 1) big.limbs[i] = data[i]; const str = big.toString(c.a(), 10) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => unreachable, }; const token = try appendToken(c, .IntegerLiteral, str); const node = try c.a().create(ast.Node.IntegerLiteral); node.* = .{ .token = token, }; if (is_negative) ZigClangAPSInt_free(aps_int); return &node.base; } fn transCreateNodeReturnExpr(c: *Context) !*ast.Node.ControlFlowExpression { const ltoken = try appendToken(c, .Keyword_return, "return"); const node = try c.a().create(ast.Node.ControlFlowExpression); node.* = .{ .ltoken = ltoken, .kind = .Return, .rhs = null, }; return node; } fn transCreateNodeUndefinedLiteral(c: *Context) !*ast.Node { const token = try appendToken(c, .Keyword_undefined, "undefined"); const node = try c.a().create(ast.Node.UndefinedLiteral); node.* = .{ .token = token, }; return &node.base; } fn transCreateNodeNullLiteral(c: *Context) !*ast.Node { const token = try appendToken(c, .Keyword_null, "null"); const node = try c.a().create(ast.Node.NullLiteral); node.* = .{ .token = token, }; return &node.base; } fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node { const token = if (value) try appendToken(c, .Keyword_true, "true") else try appendToken(c, .Keyword_false, "false"); const node = try c.a().create(ast.Node.BoolLiteral); node.* = .{ .token = token, }; return &node.base; } fn transCreateNodeContainerInitializer(c: *Context, dot_tok: ast.TokenIndex) !*ast.Node.SuffixOp { _ = try appendToken(c, .LBrace, "{"); const node = try c.a().create(ast.Node.SuffixOp); node.* = ast.Node.SuffixOp{ .lhs = .{ .dot = dot_tok }, .op = .{ .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()), }, .rtoken = undefined, // set after appending values }; return node; } fn transCreateNodeInt(c: *Context, int: var) !*ast.Node { const token = try appendTokenFmt(c, .IntegerLiteral, "{}", .{int}); const node = try c.a().create(ast.Node.IntegerLiteral); node.* = .{ .token = token, }; return &node.base; } fn transCreateNodeFloat(c: *Context, int: var) !*ast.Node { const token = try appendTokenFmt(c, .FloatLiteral, "{}", .{int}); const node = try c.a().create(ast.Node.FloatLiteral); node.* = .{ .token = token, }; return &node.base; } fn transCreateNodeOpaqueType(c: *Context) !*ast.Node { const call_node = try transCreateNodeBuiltinFnCall(c, "@OpaqueType"); call_node.rparen_token = try appendToken(c, .RParen, ")"); return &call_node.base; } fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_alias: *ast.Node.FnProto) !*ast.Node { const scope = &c.global_scope.base; const pub_tok = try appendToken(c, .Keyword_pub, "pub"); const inline_tok = try appendToken(c, .Keyword_inline, "inline"); const fn_tok = try appendToken(c, .Keyword_fn, "fn"); const name_tok = try appendIdentifier(c, name); _ = try appendToken(c, .LParen, "("); var fn_params = ast.Node.FnProto.ParamList.init(c.a()); var it = proto_alias.params.iterator(0); while (it.next()) |pn| { if (it.index != 0) { _ = try appendToken(c, .Comma, ","); } const param = pn.*.cast(ast.Node.ParamDecl).?; const param_name_tok = param.name_token orelse try appendTokenFmt(c, .Identifier, "arg_{}", .{c.getMangle()}); _ = try appendToken(c, .Colon, ":"); const param_node = try c.a().create(ast.Node.ParamDecl); param_node.* = .{ .doc_comments = null, .comptime_token = null, .noalias_token = param.noalias_token, .name_token = param_name_tok, .type_node = param.type_node, .var_args_token = null, }; try fn_params.push(&param_node.base); } _ = try appendToken(c, .RParen, ")"); const fn_proto = try c.a().create(ast.Node.FnProto); fn_proto.* = .{ .doc_comments = null, .visib_token = pub_tok, .fn_token = fn_tok, .name_token = name_tok, .params = fn_params, .return_type = proto_alias.return_type, .var_args_token = null, .extern_export_inline_token = inline_tok, .cc_token = null, .body_node = null, .lib_name = null, .align_expr = null, .section_expr = null, }; const block = try transCreateNodeBlock(c, null); const return_expr = try transCreateNodeReturnExpr(c); const unwrap_expr = try transCreateNodeUnwrapNull(c, ref.cast(ast.Node.VarDecl).?.init_node.?); const call_expr = try transCreateNodeFnCall(c, unwrap_expr); it = fn_params.iterator(0); while (it.next()) |pn| { if (it.index != 0) { _ = try appendToken(c, .Comma, ","); } const param = pn.*.cast(ast.Node.ParamDecl).?; try call_expr.op.Call.params.push(try transCreateNodeIdentifier(c, tokenSlice(c, param.name_token.?))); } call_expr.rtoken = try appendToken(c, .RParen, ")"); return_expr.rhs = &call_expr.base; _ = try appendToken(c, .Semicolon, ";"); block.rbrace = try appendToken(c, .RBrace, "}"); try block.statements.push(&return_expr.base); fn_proto.body_node = &block.base; return &fn_proto.base; } fn transCreateNodeUnwrapNull(c: *Context, wrapped: *ast.Node) !*ast.Node { _ = try appendToken(c, .Period, "."); const qm = try appendToken(c, .QuestionMark, "?"); const node = try c.a().create(ast.Node.SuffixOp); node.* = .{ .op = .UnwrapOptional, .lhs = .{ .node = wrapped }, .rtoken = qm, }; return &node.base; } fn transCreateNodeEnumLiteral(c: *Context, name: []const u8) !*ast.Node { const node = try c.a().create(ast.Node.EnumLiteral); node.* = .{ .dot = try appendToken(c, .Period, "."), .name = try appendIdentifier(c, name), }; return &node.base; } fn transCreateNodeIf(c: *Context) !*ast.Node.If { const if_tok = try appendToken(c, .Keyword_if, "if"); _ = try appendToken(c, .LParen, "("); const node = try c.a().create(ast.Node.If); node.* = .{ .if_token = if_tok, .condition = undefined, .payload = null, .body = undefined, .@"else" = null, }; return node; } fn transCreateNodeElse(c: *Context) !*ast.Node.Else { const node = try c.a().create(ast.Node.Else); node.* = .{ .else_token = try appendToken(c, .Keyword_else, "else"), .payload = null, .body = undefined, }; return node; } fn transCreateNodeBlock(c: *Context, label: ?[]const u8) !*ast.Node.Block { const label_node = if (label) |l| blk: { const ll = try appendIdentifier(c, l); _ = try appendToken(c, .Colon, ":"); break :blk ll; } else null; const block_node = try c.a().create(ast.Node.Block); block_node.* = .{ .label = label_node, .lbrace = try appendToken(c, .LBrace, "{"), .statements = ast.Node.Block.StatementList.init(c.a()), .rbrace = undefined, }; return block_node; } fn transCreateNodeBreak(c: *Context, label: ?[]const u8) !*ast.Node.ControlFlowExpression { const ltoken = try appendToken(c, .Keyword_break, "break"); const label_node = if (label) |l| blk: { _ = try appendToken(c, .Colon, ":"); break :blk try transCreateNodeIdentifier(c, l); } else null; const node = try c.a().create(ast.Node.ControlFlowExpression); node.* = .{ .ltoken = ltoken, .kind = .{ .Break = label_node }, .rhs = null, }; return node; } fn transCreateNodeVarDecl(c: *Context, is_pub: bool, is_const: bool, name: []const u8) !*ast.Node.VarDecl { const visib_tok = if (is_pub) try appendToken(c, .Keyword_pub, "pub") else null; const mut_tok = if (is_const) try appendToken(c, .Keyword_const, "const") else try appendToken(c, .Keyword_var, "var"); const name_tok = try appendIdentifier(c, name); const node = try c.a().create(ast.Node.VarDecl); node.* = .{ .doc_comments = null, .visib_token = visib_tok, .thread_local_token = null, .name_token = name_tok, .eq_token = undefined, .mut_token = mut_tok, .comptime_token = null, .extern_export_token = null, .lib_name = null, .type_node = null, .align_node = null, .section_node = null, .init_node = null, .semicolon_token = undefined, }; return node; } fn transCreateNodeWhile(c: *Context) !*ast.Node.While { const while_tok = try appendToken(c, .Keyword_while, "while"); _ = try appendToken(c, .LParen, "("); const node = try c.a().create(ast.Node.While); node.* = .{ .label = null, .inline_token = null, .while_token = while_tok, .condition = undefined, .payload = null, .continue_expr = null, .body = undefined, .@"else" = null, }; return node; } fn transCreateNodeContinue(c: *Context) !*ast.Node { const ltoken = try appendToken(c, .Keyword_continue, "continue"); const node = try c.a().create(ast.Node.ControlFlowExpression); node.* = .{ .ltoken = ltoken, .kind = .{ .Continue = null }, .rhs = null, }; _ = try appendToken(c, .Semicolon, ";"); return &node.base; } fn transCreateNodeSwitch(c: *Context) !*ast.Node.Switch { const switch_tok = try appendToken(c, .Keyword_switch, "switch"); _ = try appendToken(c, .LParen, "("); const node = try c.a().create(ast.Node.Switch); node.* = .{ .switch_token = switch_tok, .expr = undefined, .cases = ast.Node.Switch.CaseList.init(c.a()), .rbrace = undefined, }; return node; } fn transCreateNodeSwitchCase(c: *Context, lhs: *ast.Node) !*ast.Node.SwitchCase { const arrow_tok = try appendToken(c, .EqualAngleBracketRight, "=>"); const node = try c.a().create(ast.Node.SwitchCase); node.* = .{ .items = ast.Node.SwitchCase.ItemList.init(c.a()), .arrow_token = arrow_tok, .payload = null, .expr = undefined, }; try node.items.push(lhs); return node; } fn transCreateNodeSwitchElse(c: *Context) !*ast.Node { const node = try c.a().create(ast.Node.SwitchElse); node.* = .{ .token = try appendToken(c, .Keyword_else, "else"), }; return &node.base; } fn transCreateNodeShiftOp( rp: RestorePoint, scope: *Scope, stmt: *const ZigClangBinaryOperator, op: ast.Node.InfixOp.Op, op_tok_id: std.zig.Token.Id, bytes: []const u8, ) !*ast.Node { std.debug.assert(op == .BitShiftLeft or op == .BitShiftRight); const lhs_expr = ZigClangBinaryOperator_getLHS(stmt); const rhs_expr = ZigClangBinaryOperator_getRHS(stmt); const rhs_location = ZigClangExpr_getBeginLoc(rhs_expr); // lhs >> @as(u5, rh) const lhs = try transExpr(rp, scope, lhs_expr, .used, .l_value); const op_token = try appendToken(rp.c, op_tok_id, bytes); const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as"); const rhs_type = try qualTypeToLog2IntRef(rp, ZigClangBinaryOperator_getType(stmt), rhs_location); try as_node.params.push(rhs_type); _ = try appendToken(rp.c, .Comma, ","); const rhs = try transExprCoercing(rp, scope, rhs_expr, .used, .r_value); try as_node.params.push(rhs); as_node.rparen_token = try appendToken(rp.c, .RParen, ")"); const node = try rp.c.a().create(ast.Node.InfixOp); node.* = ast.Node.InfixOp{ .op_token = op_token, .lhs = lhs, .op = op, .rhs = &as_node.base, }; return &node.base; } fn transCreateNodePtrDeref(c: *Context, lhs: *ast.Node) !*ast.Node { const node = try c.a().create(ast.Node.SuffixOp); node.* = .{ .lhs = .{ .node = lhs }, .op = .Deref, .rtoken = try appendToken(c, .PeriodAsterisk, ".*"), }; return &node.base; } fn transCreateNodeArrayAccess(c: *Context, lhs: *ast.Node) !*ast.Node.SuffixOp { _ = try appendToken(c, .LBrace, "["); const node = try c.a().create(ast.Node.SuffixOp); node.* = .{ .lhs = .{ .node = lhs }, .op = .{ .ArrayAccess = undefined, }, .rtoken = undefined, }; return node; } const RestorePoint = struct { c: *Context, token_index: ast.TokenIndex, src_buf_index: usize, fn activate(self: RestorePoint) void { self.c.tree.tokens.shrink(self.token_index); self.c.source_buffer.shrink(self.src_buf_index); } }; fn makeRestorePoint(c: *Context) RestorePoint { return RestorePoint{ .c = c, .token_index = c.tree.tokens.len, .src_buf_index = c.source_buffer.len(), }; } fn transType(rp: RestorePoint, ty: *const ZigClangType, source_loc: ZigClangSourceLocation) TypeError!*ast.Node { switch (ZigClangType_getTypeClass(ty)) { .Builtin => { const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); return transCreateNodeIdentifier(rp.c, switch (ZigClangBuiltinType_getKind(builtin_ty)) { .Void => "c_void", .Bool => "bool", .Char_U, .UChar, .Char_S, .Char8 => "u8", .SChar => "i8", .UShort => "c_ushort", .UInt => "c_uint", .ULong => "c_ulong", .ULongLong => "c_ulonglong", .Short => "c_short", .Int => "c_int", .Long => "c_long", .LongLong => "c_longlong", .UInt128 => "u128", .Int128 => "i128", .Float => "f32", .Double => "f64", .Float128 => "f128", .Float16 => "f16", .LongDouble => "c_longdouble", else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type", .{}), }); }, .FunctionProto => { const fn_proto_ty = @ptrCast(*const ZigClangFunctionProtoType, ty); const fn_proto = try transFnProto(rp, null, fn_proto_ty, source_loc, null, false); return &fn_proto.base; }, .FunctionNoProto => { const fn_no_proto_ty = @ptrCast(*const ZigClangFunctionType, ty); const fn_proto = try transFnNoProto(rp, fn_no_proto_ty, source_loc, null, false); return &fn_proto.base; }, .Paren => { const paren_ty = @ptrCast(*const ZigClangParenType, ty); return transQualType(rp, ZigClangParenType_getInnerType(paren_ty), source_loc); }, .Pointer => { const child_qt = ZigClangType_getPointeeType(ty); if (qualTypeChildIsFnProto(child_qt)) { const optional_node = try transCreateNodePrefixOp(rp.c, .OptionalType, .QuestionMark, "?"); optional_node.rhs = try transQualType(rp, child_qt, source_loc); return &optional_node.base; } if (typeIsOpaque(rp.c, ZigClangQualType_getTypePtr(child_qt), source_loc)) { const optional_node = try transCreateNodePrefixOp(rp.c, .OptionalType, .QuestionMark, "?"); const pointer_node = try transCreateNodePtrType( rp.c, ZigClangQualType_isConstQualified(child_qt), ZigClangQualType_isVolatileQualified(child_qt), .Asterisk, ); optional_node.rhs = &pointer_node.base; pointer_node.rhs = try transQualType(rp, child_qt, source_loc); return &optional_node.base; } const pointer_node = try transCreateNodePtrType( rp.c, ZigClangQualType_isConstQualified(child_qt), ZigClangQualType_isVolatileQualified(child_qt), .Identifier, ); pointer_node.rhs = try transQualType(rp, child_qt, source_loc); return &pointer_node.base; }, .ConstantArray => { const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, ty); const size_ap_int = ZigClangConstantArrayType_getSize(const_arr_ty); const size = ZigClangAPInt_getLimitedValue(size_ap_int, math.maxInt(usize)); var node = try transCreateNodePrefixOp( rp.c, .{ .ArrayType = .{ .len_expr = undefined, .sentinel = null, }, }, .LBracket, "[", ); node.op.ArrayType.len_expr = try transCreateNodeInt(rp.c, size); _ = try appendToken(rp.c, .RBracket, "]"); node.rhs = try transQualType(rp, ZigClangConstantArrayType_getElementType(const_arr_ty), source_loc); return &node.base; }, .IncompleteArray => { const incomplete_array_ty = @ptrCast(*const ZigClangIncompleteArrayType, ty); const child_qt = ZigClangIncompleteArrayType_getElementType(incomplete_array_ty); var node = try transCreateNodePtrType( rp.c, ZigClangQualType_isConstQualified(child_qt), ZigClangQualType_isVolatileQualified(child_qt), .Identifier, ); node.rhs = try transQualType(rp, child_qt, source_loc); return &node.base; }, .Typedef => { const typedef_ty = @ptrCast(*const ZigClangTypedefType, ty); const typedef_decl = ZigClangTypedefType_getDecl(typedef_ty); return (try transTypeDef(rp.c, typedef_decl)) orelse revertAndWarn(rp, error.UnsupportedType, source_loc, "unable to translate typedef declaration", .{}); }, .Record => { const record_ty = @ptrCast(*const ZigClangRecordType, ty); const record_decl = ZigClangRecordType_getDecl(record_ty); return (try transRecordDecl(rp.c, record_decl)) orelse revertAndWarn(rp, error.UnsupportedType, source_loc, "unable to resolve record declaration", .{}); }, .Enum => { const enum_ty = @ptrCast(*const ZigClangEnumType, ty); const enum_decl = ZigClangEnumType_getDecl(enum_ty); return (try transEnumDecl(rp.c, enum_decl)) orelse revertAndWarn(rp, error.UnsupportedType, source_loc, "unable to translate enum declaration", .{}); }, .Elaborated => { const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ty); return transQualType(rp, ZigClangElaboratedType_getNamedType(elaborated_ty), source_loc); }, .Decayed => { const decayed_ty = @ptrCast(*const ZigClangDecayedType, ty); return transQualType(rp, ZigClangDecayedType_getDecayedType(decayed_ty), source_loc); }, .Attributed => { const attributed_ty = @ptrCast(*const ZigClangAttributedType, ty); return transQualType(rp, ZigClangAttributedType_getEquivalentType(attributed_ty), source_loc); }, .MacroQualified => { const macroqualified_ty = @ptrCast(*const ZigClangMacroQualifiedType, ty); return transQualType(rp, ZigClangMacroQualifiedType_getModifiedType(macroqualified_ty), source_loc); }, else => { const type_name = rp.c.str(ZigClangType_getTypeClassName(ty)); return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported type: '{}'", .{type_name}); }, } } fn isCVoid(qt: ZigClangQualType) bool { const ty = ZigClangQualType_getTypePtr(qt); if (ZigClangType_getTypeClass(ty) == .Builtin) { const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); return ZigClangBuiltinType_getKind(builtin_ty) == .Void; } return false; } const FnDeclContext = struct { fn_name: []const u8, has_body: bool, storage_class: ZigClangStorageClass, is_export: bool, }; fn transCC( rp: RestorePoint, fn_ty: *const ZigClangFunctionType, source_loc: ZigClangSourceLocation, ) !CallingConvention { const clang_cc = ZigClangFunctionType_getCallConv(fn_ty); switch (clang_cc) { .C => return CallingConvention.C, .X86StdCall => return CallingConvention.Stdcall, else => return revertAndWarn( rp, error.UnsupportedType, source_loc, "unsupported calling convention: {}", .{@tagName(clang_cc)}, ), } } fn transFnProto( rp: RestorePoint, fn_decl: ?*const ZigClangFunctionDecl, fn_proto_ty: *const ZigClangFunctionProtoType, source_loc: ZigClangSourceLocation, fn_decl_context: ?FnDeclContext, is_pub: bool, ) !*ast.Node.FnProto { const fn_ty = @ptrCast(*const ZigClangFunctionType, fn_proto_ty); const cc = try transCC(rp, fn_ty, source_loc); const is_var_args = ZigClangFunctionProtoType_isVariadic(fn_proto_ty); return finishTransFnProto(rp, fn_decl, fn_proto_ty, fn_ty, source_loc, fn_decl_context, is_var_args, cc, is_pub); } fn transFnNoProto( rp: RestorePoint, fn_ty: *const ZigClangFunctionType, source_loc: ZigClangSourceLocation, fn_decl_context: ?FnDeclContext, is_pub: bool, ) !*ast.Node.FnProto { const cc = try transCC(rp, fn_ty, source_loc); const is_var_args = if (fn_decl_context) |ctx| !ctx.is_export else true; return finishTransFnProto(rp, null, null, fn_ty, source_loc, fn_decl_context, is_var_args, cc, is_pub); } fn finishTransFnProto( rp: RestorePoint, fn_decl: ?*const ZigClangFunctionDecl, fn_proto_ty: ?*const ZigClangFunctionProtoType, fn_ty: *const ZigClangFunctionType, source_loc: ZigClangSourceLocation, fn_decl_context: ?FnDeclContext, is_var_args: bool, cc: CallingConvention, is_pub: bool, ) !*ast.Node.FnProto { const is_export = if (fn_decl_context) |ctx| ctx.is_export else false; const is_extern = if (fn_decl_context) |ctx| !ctx.has_body else true; // TODO check for always_inline attribute // TODO check for align attribute // pub extern fn name(...) T const pub_tok = if (is_pub) try appendToken(rp.c, .Keyword_pub, "pub") else null; const cc_tok = if (cc == .Stdcall) try appendToken(rp.c, .Keyword_stdcallcc, "stdcallcc") else null; const extern_export_inline_tok = if (is_export) try appendToken(rp.c, .Keyword_export, "export") else if (cc == .C and is_extern) try appendToken(rp.c, .Keyword_extern, "extern") else null; const fn_tok = try appendToken(rp.c, .Keyword_fn, "fn"); const name_tok = if (fn_decl_context) |ctx| try appendIdentifier(rp.c, ctx.fn_name) else null; const lparen_tok = try appendToken(rp.c, .LParen, "("); var fn_params = ast.Node.FnProto.ParamList.init(rp.c.a()); const param_count: usize = if (fn_proto_ty != null) ZigClangFunctionProtoType_getNumParams(fn_proto_ty.?) else 0; var i: usize = 0; while (i < param_count) : (i += 1) { const param_qt = ZigClangFunctionProtoType_getParamType(fn_proto_ty.?, @intCast(c_uint, i)); const noalias_tok = if (ZigClangQualType_isRestrictQualified(param_qt)) try appendToken(rp.c, .Keyword_noalias, "noalias") else null; const param_name_tok: ?ast.TokenIndex = blk: { if (fn_decl != null) { const param = ZigClangFunctionDecl_getParamDecl(fn_decl.?, @intCast(c_uint, i)); const param_name: []const u8 = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, param))); if (param_name.len < 1) break :blk null; const result = try appendIdentifier(rp.c, param_name); _ = try appendToken(rp.c, .Colon, ":"); break :blk result; } break :blk null; }; const type_node = try transQualType(rp, param_qt, source_loc); const param_node = try rp.c.a().create(ast.Node.ParamDecl); param_node.* = ast.Node.ParamDecl{ .base = ast.Node{ .id = ast.Node.Id.ParamDecl }, .doc_comments = null, .comptime_token = null, .noalias_token = noalias_tok, .name_token = param_name_tok, .type_node = type_node, .var_args_token = null, }; try fn_params.push(&param_node.base); if (i + 1 < param_count) { _ = try appendToken(rp.c, .Comma, ","); } } if (is_var_args) { if (param_count > 0) { _ = try appendToken(rp.c, .Comma, ","); } const var_arg_node = try rp.c.a().create(ast.Node.ParamDecl); var_arg_node.* = ast.Node.ParamDecl{ .base = ast.Node{ .id = ast.Node.Id.ParamDecl }, .doc_comments = null, .comptime_token = null, .noalias_token = null, .name_token = null, .type_node = undefined, // Note: Accessing this causes an access violation. Need to check .var_args_token first before trying this field .var_args_token = try appendToken(rp.c, .Ellipsis3, "..."), }; try fn_params.push(&var_arg_node.base); } const rparen_tok = try appendToken(rp.c, .RParen, ")"); const return_type_node = blk: { if (ZigClangFunctionType_getNoReturnAttr(fn_ty)) { break :blk try transCreateNodeIdentifier(rp.c, "noreturn"); } else { const return_qt = ZigClangFunctionType_getReturnType(fn_ty); if (isCVoid(return_qt)) { // convert primitive c_void to actual void (only for return type) break :blk try transCreateNodeIdentifier(rp.c, "void"); } else { break :blk transQualType(rp, return_qt, source_loc) catch |err| switch (err) { error.UnsupportedType => { try emitWarning(rp.c, source_loc, "unsupported function proto return type", .{}); return err; }, error.OutOfMemory => |e| return e, }; } } }; const fn_proto = try rp.c.a().create(ast.Node.FnProto); fn_proto.* = .{ .doc_comments = null, .visib_token = pub_tok, .fn_token = fn_tok, .name_token = name_tok, .params = fn_params, .return_type = .{ .Explicit = return_type_node }, .var_args_token = null, // TODO this field is broken in the AST data model .extern_export_inline_token = extern_export_inline_tok, .cc_token = cc_tok, .body_node = null, .lib_name = null, .align_expr = null, .section_expr = null, }; return fn_proto; } fn revertAndWarn( rp: RestorePoint, err: var, source_loc: ZigClangSourceLocation, comptime format: []const u8, args: var, ) (@TypeOf(err) || error{OutOfMemory}) { rp.activate(); try emitWarning(rp.c, source_loc, format, args); return err; } fn emitWarning(c: *Context, loc: ZigClangSourceLocation, comptime format: []const u8, args: var) !void { const args_prefix = .{c.locStr(loc)}; _ = try appendTokenFmt(c, .LineComment, "// {}: warning: " ++ format, args_prefix ++ args); } pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comptime format: []const u8, args: var) !void { // pub const name = @compileError(msg); const pub_tok = try appendToken(c, .Keyword_pub, "pub"); const const_tok = try appendToken(c, .Keyword_const, "const"); const name_tok = try appendIdentifier(c, name); const eq_tok = try appendToken(c, .Equal, "="); const builtin_tok = try appendToken(c, .Builtin, "@compileError"); const lparen_tok = try appendToken(c, .LParen, "("); const msg_tok = try appendTokenFmt(c, .StringLiteral, "\"" ++ format ++ "\"", args); const rparen_tok = try appendToken(c, .RParen, ")"); const semi_tok = try appendToken(c, .Semicolon, ";"); const msg_node = try c.a().create(ast.Node.StringLiteral); msg_node.* = ast.Node.StringLiteral{ .base = ast.Node{ .id = ast.Node.Id.StringLiteral }, .token = msg_tok, }; const call_node = try c.a().create(ast.Node.BuiltinCall); call_node.* = ast.Node.BuiltinCall{ .base = ast.Node{ .id = ast.Node.Id.BuiltinCall }, .builtin_token = builtin_tok, .params = ast.Node.BuiltinCall.ParamList.init(c.a()), .rparen_token = rparen_tok, }; try call_node.params.push(&msg_node.base); const var_decl_node = try c.a().create(ast.Node.VarDecl); var_decl_node.* = ast.Node.VarDecl{ .base = ast.Node{ .id = ast.Node.Id.VarDecl }, .doc_comments = null, .visib_token = pub_tok, .thread_local_token = null, .name_token = name_tok, .eq_token = eq_tok, .mut_token = const_tok, .comptime_token = null, .extern_export_token = null, .lib_name = null, .type_node = null, .align_node = null, .section_node = null, .init_node = &call_node.base, .semicolon_token = semi_tok, }; try addTopLevelDecl(c, name, &var_decl_node.base); } fn appendToken(c: *Context, token_id: Token.Id, bytes: []const u8) !ast.TokenIndex { std.debug.assert(token_id != .Identifier); // use appendIdentifier return appendTokenFmt(c, token_id, "{}", .{bytes}); } fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8, args: var) !ast.TokenIndex { const S = struct { fn callback(context: *Context, bytes: []const u8) error{OutOfMemory}!void { return context.source_buffer.append(bytes); } }; const start_index = c.source_buffer.len(); errdefer c.source_buffer.shrink(start_index); try std.fmt.format(c, error{OutOfMemory}, S.callback, format, args); const end_index = c.source_buffer.len(); const token_index = c.tree.tokens.len; const new_token = try c.tree.tokens.addOne(); errdefer c.tree.tokens.shrink(token_index); new_token.* = Token{ .id = token_id, .start = start_index, .end = end_index, }; try c.source_buffer.appendByte(' '); return token_index; } // TODO hook up with codegen fn isZigPrimitiveType(name: []const u8) bool { if (name.len > 1 and (name[0] == 'u' or name[0] == 'i')) { for (name[1..]) |c| { switch (c) { '0'...'9' => {}, else => return false, } } return true; } // void is invalid in c so it doesn't need to be checked. return mem.eql(u8, name, "comptime_float") or mem.eql(u8, name, "comptime_int") or mem.eql(u8, name, "bool") or mem.eql(u8, name, "isize") or mem.eql(u8, name, "usize") or mem.eql(u8, name, "f16") or mem.eql(u8, name, "f32") or mem.eql(u8, name, "f64") or mem.eql(u8, name, "f128") or mem.eql(u8, name, "c_longdouble") or mem.eql(u8, name, "noreturn") or mem.eql(u8, name, "type") or mem.eql(u8, name, "anyerror") or mem.eql(u8, name, "c_short") or mem.eql(u8, name, "c_ushort") or mem.eql(u8, name, "c_int") or mem.eql(u8, name, "c_uint") or mem.eql(u8, name, "c_long") or mem.eql(u8, name, "c_ulong") or mem.eql(u8, name, "c_longlong") or mem.eql(u8, name, "c_ulonglong"); } fn isValidZigIdentifier(name: []const u8) bool { for (name) |c, i| { switch (c) { '_', 'a'...'z', 'A'...'Z' => {}, '0'...'9' => if (i == 0) return false, else => return false, } } return true; } fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex { if (!isValidZigIdentifier(name) or std.zig.Token.getKeyword(name) != null) { return appendTokenFmt(c, .Identifier, "@\"{}\"", .{name}); } else { return appendTokenFmt(c, .Identifier, "{}", .{name}); } } fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { const token_index = try appendIdentifier(c, name); const identifier = try c.a().create(ast.Node.Identifier); identifier.* = ast.Node.Identifier{ .base = ast.Node{ .id = ast.Node.Id.Identifier }, .token = token_index, }; return &identifier.base; } pub fn freeErrors(errors: []ClangErrMsg) void { ZigClangErrorMsg_delete(errors.ptr, errors.len); } fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void { // TODO if we see #undef, delete it from the table var it = ZigClangASTUnit_getLocalPreprocessingEntities_begin(unit); const it_end = ZigClangASTUnit_getLocalPreprocessingEntities_end(unit); var tok_list = ctok.TokenList.init(c.a()); const scope = c.global_scope; while (it.I != it_end.I) : (it.I += 1) { const entity = ZigClangPreprocessingRecord_iterator_deref(it); tok_list.shrink(0); switch (ZigClangPreprocessedEntity_getKind(entity)) { .MacroDefinitionKind => { const macro = @ptrCast(*ZigClangMacroDefinitionRecord, entity); const raw_name = ZigClangMacroDefinitionRecord_getName_getNameStart(macro); const begin_loc = ZigClangMacroDefinitionRecord_getSourceRange_getBegin(macro); const name = try c.str(raw_name); // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "_{}", .{name}) else name; if (scope.containsNow(mangled_name)) { continue; } const begin_c = ZigClangSourceManager_getCharacterData(c.source_manager, begin_loc); ctok.tokenizeCMacro(c, begin_loc, mangled_name, &tok_list, begin_c) catch |err| switch (err) { error.OutOfMemory => |e| return e, else => { continue; }, }; var tok_it = tok_list.iterator(0); const first_tok = tok_it.next().?; assert(first_tok.id == .Identifier and mem.eql(u8, first_tok.bytes, name)); const next = tok_it.peek().?; switch (next.id) { .Identifier => { // if it equals itself, ignore. for example, from stdio.h: // #define stdin stdin if (mem.eql(u8, name, next.bytes)) { continue; } }, .Eof => { // this means it is a macro without a value // we don't care about such things continue; }, else => {}, } const macro_fn = if (tok_it.peek().?.id == .Fn) blk: { _ = tok_it.next(); break :blk true; } else false; (if (macro_fn) transMacroFnDefine(c, &tok_it, mangled_name, begin_loc) else transMacroDefine(c, &tok_it, mangled_name, begin_loc)) catch |err| switch (err) { error.ParseError => continue, error.OutOfMemory => |e| return e, }; }, else => {}, } } } fn transMacroDefine(c: *Context, it: *ctok.TokenList.Iterator, name: []const u8, source_loc: ZigClangSourceLocation) ParseError!void { const scope = &c.global_scope.base; const node = try transCreateNodeVarDecl(c, true, true, name); node.eq_token = try appendToken(c, .Equal, "="); node.init_node = try parseCExpr(c, it, source_loc, scope); const last = it.next().?; if (last.id != .Eof) return failDecl( c, source_loc, name, "unable to translate C expr: unexpected token {}", .{last.id}, ); node.semicolon_token = try appendToken(c, .Semicolon, ";"); _ = try c.global_scope.macro_table.put(name, &node.base); } fn transMacroFnDefine(c: *Context, it: *ctok.TokenList.Iterator, name: []const u8, source_loc: ZigClangSourceLocation) ParseError!void { const block_scope = try Scope.Block.init(c, &c.global_scope.base, null); const scope = &block_scope.base; const pub_tok = try appendToken(c, .Keyword_pub, "pub"); const inline_tok = try appendToken(c, .Keyword_inline, "inline"); const fn_tok = try appendToken(c, .Keyword_fn, "fn"); const name_tok = try appendIdentifier(c, name); _ = try appendToken(c, .LParen, "("); if (it.next().?.id != .LParen) { return failDecl( c, source_loc, name, "unable to translate C expr: expected '('", .{}, ); } var fn_params = ast.Node.FnProto.ParamList.init(c.a()); while (true) { const param_tok = it.next().?; if (param_tok.id != .Identifier) { return failDecl( c, source_loc, name, "unable to translate C expr: expected identifier", .{}, ); } const mangled_name = try block_scope.makeMangledName(c, param_tok.bytes); const param_name_tok = try appendIdentifier(c, mangled_name); _ = try appendToken(c, .Colon, ":"); const token_index = try appendToken(c, .Keyword_var, "var"); const identifier = try c.a().create(ast.Node.Identifier); identifier.* = ast.Node.Identifier{ .base = ast.Node{ .id = ast.Node.Id.Identifier }, .token = token_index, }; const param_node = try c.a().create(ast.Node.ParamDecl); param_node.* = .{ .doc_comments = null, .comptime_token = null, .noalias_token = null, .name_token = param_name_tok, .type_node = &identifier.base, .var_args_token = null, }; try fn_params.push(&param_node.base); if (it.peek().?.id != .Comma) break; _ = it.next(); _ = try appendToken(c, .Comma, ","); } if (it.next().?.id != .RParen) { return failDecl( c, source_loc, name, "unable to translate C expr: expected ')'", .{}, ); } _ = try appendToken(c, .RParen, ")"); const type_of = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); type_of.rparen_token = try appendToken(c, .RParen, ")"); const fn_proto = try c.a().create(ast.Node.FnProto); fn_proto.* = .{ .visib_token = pub_tok, .extern_export_inline_token = inline_tok, .fn_token = fn_tok, .name_token = name_tok, .params = fn_params, .return_type = .{ .Explicit = &type_of.base }, .doc_comments = null, .var_args_token = null, .cc_token = null, .body_node = null, .lib_name = null, .align_expr = null, .section_expr = null, }; const block = try transCreateNodeBlock(c, null); const return_expr = try transCreateNodeReturnExpr(c); const expr = try parseCExpr(c, it, source_loc, scope); const last = it.next().?; if (last.id != .Eof) return failDecl( c, source_loc, name, "unable to translate C expr: unexpected token {}", .{last.id}, ); _ = try appendToken(c, .Semicolon, ";"); try type_of.params.push(expr); return_expr.rhs = expr; block.rbrace = try appendToken(c, .RBrace, "}"); try block.statements.push(&return_expr.base); fn_proto.body_node = &block.base; _ = try c.global_scope.macro_table.put(name, &fn_proto.base); } const ParseError = Error || error{ParseError}; fn parseCExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSourceLocation, scope: *Scope) ParseError!*ast.Node { const node = try parseCPrefixOpExpr(c, it, source_loc, scope); switch (it.next().?.id) { .QuestionMark => { // must come immediately after expr _ = try appendToken(c, .RParen, ")"); const if_node = try transCreateNodeIf(c); if_node.condition = node; if_node.body = try parseCPrimaryExpr(c, it, source_loc, scope); if (it.next().?.id != .Colon) { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: expected ':'", .{}, ); return error.ParseError; } if_node.@"else" = try transCreateNodeElse(c); if_node.@"else".?.body = try parseCPrimaryExpr(c, it, source_loc, scope); return &if_node.base; }, else => { _ = it.prev(); return node; }, } } fn parseCNumLit(c: *Context, tok: *CToken, source_loc: ZigClangSourceLocation) ParseError!*ast.Node { if (tok.id == .NumLitInt) { if (tok.num_lit_suffix == .None) { if (tok.bytes.len > 2 and tok.bytes[0] == '0') { switch (tok.bytes[1]) { '0'...'7' => { // octal return transCreateNodeInt(c, try std.fmt.allocPrint(c.a(), "0o{}", .{tok.bytes})); }, else => {}, } } return transCreateNodeInt(c, tok.bytes); } const cast_node = try transCreateNodeBuiltinFnCall(c, "@as"); try cast_node.params.push(try transCreateNodeIdentifier(c, switch (tok.num_lit_suffix) { .U => "c_uint", .L => "c_long", .LU => "c_ulong", .LL => "c_longlong", .LLU => "c_ulonglong", else => unreachable, })); _ = try appendToken(c, .Comma, ","); try cast_node.params.push(try transCreateNodeInt(c, tok.bytes)); cast_node.rparen_token = try appendToken(c, .RParen, ")"); return &cast_node.base; } else if (tok.id == .NumLitFloat) { if (tok.num_lit_suffix == .None) { return transCreateNodeFloat(c, tok.bytes); } const cast_node = try transCreateNodeBuiltinFnCall(c, "@as"); try cast_node.params.push(try transCreateNodeIdentifier(c, switch (tok.num_lit_suffix) { .F => "f32", .L => "f64", else => unreachable, })); _ = try appendToken(c, .Comma, ","); try cast_node.params.push(try transCreateNodeFloat(c, tok.bytes)); cast_node.rparen_token = try appendToken(c, .RParen, ")"); return &cast_node.base; } else unreachable; } fn parseCPrimaryExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSourceLocation, scope: *Scope) ParseError!*ast.Node { const tok = it.next().?; switch (tok.id) { .CharLit => { const token = try appendToken(c, .CharLiteral, tok.bytes); const node = try c.a().create(ast.Node.CharLiteral); node.* = ast.Node.CharLiteral{ .token = token, }; return &node.base; }, .StrLit => { const token = try appendToken(c, .StringLiteral, tok.bytes); const node = try c.a().create(ast.Node.StringLiteral); node.* = ast.Node.StringLiteral{ .token = token, }; return &node.base; }, .NumLitInt, .NumLitFloat => { return parseCNumLit(c, tok, source_loc); }, .Identifier => { const mangled_name = scope.getAlias(tok.bytes); return transCreateNodeIdentifier(c, mangled_name); }, .LParen => { const inner_node = try parseCExpr(c, it, source_loc, scope); if (it.peek().?.id == .RParen) { _ = it.next(); if (it.peek().?.id != .LParen) { return inner_node; } _ = it.next(); } // hack to get zig fmt to render a comma in builtin calls _ = try appendToken(c, .Comma, ","); const node_to_cast = try parseCExpr(c, it, source_loc, scope); if (it.next().?.id != .RParen) { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: expected ')''", .{}, ); return error.ParseError; } //if (@typeId(@TypeOf(x)) == .Pointer) // @ptrCast(dest, x) //else if (@typeId(@TypeOf(x)) == .Integer) // @intToPtr(dest, x) //else // @as(dest, x) const if_1 = try transCreateNodeIf(c); const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeId"); const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); try type_id_1.params.push(&type_of_1.base); try type_of_1.params.push(node_to_cast); type_of_1.rparen_token = try appendToken(c, .RParen, ")"); type_id_1.rparen_token = try appendToken(c, .RParen, ")"); const cmp_1 = try c.a().create(ast.Node.InfixOp); cmp_1.* = .{ .op_token = try appendToken(c, .EqualEqual, "=="), .lhs = &type_id_1.base, .op = .EqualEqual, .rhs = try transCreateNodeEnumLiteral(c, "Pointer"), }; if_1.condition = &cmp_1.base; _ = try appendToken(c, .RParen, ")"); const ptr_cast = try transCreateNodeBuiltinFnCall(c, "@ptrCast"); try ptr_cast.params.push(inner_node); try ptr_cast.params.push(node_to_cast); ptr_cast.rparen_token = try appendToken(c, .RParen, ")"); if_1.body = &ptr_cast.base; const else_1 = try transCreateNodeElse(c); if_1.@"else" = else_1; const if_2 = try transCreateNodeIf(c); const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeId"); const type_of_2 = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); try type_id_2.params.push(&type_of_2.base); try type_of_2.params.push(node_to_cast); type_of_2.rparen_token = try appendToken(c, .RParen, ")"); type_id_2.rparen_token = try appendToken(c, .RParen, ")"); const cmp_2 = try c.a().create(ast.Node.InfixOp); cmp_2.* = .{ .op_token = try appendToken(c, .EqualEqual, "=="), .lhs = &type_id_2.base, .op = .EqualEqual, .rhs = try transCreateNodeEnumLiteral(c, "Int"), }; if_2.condition = &cmp_2.base; else_1.body = &if_2.base; _ = try appendToken(c, .RParen, ")"); const int_to_ptr = try transCreateNodeBuiltinFnCall(c, "@intToPtr"); try int_to_ptr.params.push(inner_node); try int_to_ptr.params.push(node_to_cast); int_to_ptr.rparen_token = try appendToken(c, .RParen, ")"); if_2.body = &int_to_ptr.base; const else_2 = try transCreateNodeElse(c); if_2.@"else" = else_2; const as = try transCreateNodeBuiltinFnCall(c, "@as"); try as.params.push(inner_node); try as.params.push(node_to_cast); as.rparen_token = try appendToken(c, .RParen, ")"); else_2.body = &as.base; return &if_1.base; }, else => { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: unexpected token {}", .{tok.id}, ); return error.ParseError; }, } } fn parseCSuffixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSourceLocation, scope: *Scope) ParseError!*ast.Node { var node = try parseCPrimaryExpr(c, it, source_loc, scope); while (true) { const tok = it.next().?; switch (tok.id) { .Dot => { const name_tok = it.next().?; if (name_tok.id != .Identifier) { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: expected identifier", .{}, ); return error.ParseError; } node = try transCreateNodeFieldAccess(c, node, name_tok.bytes); }, .Arrow => { const name_tok = it.next().?; if (name_tok.id != .Identifier) { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: expected identifier", .{}, ); return error.ParseError; } const deref = try transCreateNodePtrDeref(c, node); node = try transCreateNodeFieldAccess(c, deref, name_tok.bytes); }, .Asterisk => { if (it.peek().?.id == .RParen) { // type *) // hack to get zig fmt to render a comma in builtin calls _ = try appendToken(c, .Comma, ","); const ptr = try transCreateNodePtrType(c, false, false, .Identifier); ptr.rhs = node; return &ptr.base; } else { // expr * expr const op_token = try appendToken(c, .Asterisk, "*"); const rhs = try parseCPrimaryExpr(c, it, source_loc, scope); const mul_node = try c.a().create(ast.Node.InfixOp); mul_node.* = .{ .op_token = op_token, .lhs = node, .op = .BitShiftLeft, .rhs = rhs, }; node = &mul_node.base; } }, .Shl => { const op_token = try appendToken(c, .AngleBracketAngleBracketLeft, "<<"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const bitshift_node = try c.a().create(ast.Node.InfixOp); bitshift_node.* = .{ .op_token = op_token, .lhs = node, .op = .BitShiftLeft, .rhs = rhs, }; node = &bitshift_node.base; }, .Shr => { const op_token = try appendToken(c, .AngleBracketAngleBracketRight, ">>"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const bitshift_node = try c.a().create(ast.Node.InfixOp); bitshift_node.* = .{ .op_token = op_token, .lhs = node, .op = .BitShiftRight, .rhs = rhs, }; node = &bitshift_node.base; }, .Pipe => { const op_token = try appendToken(c, .Pipe, "|"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const or_node = try c.a().create(ast.Node.InfixOp); or_node.* = .{ .op_token = op_token, .lhs = node, .op = .BitOr, .rhs = rhs, }; node = &or_node.base; }, .Ampersand => { const op_token = try appendToken(c, .Ampersand, "&"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const bitand_node = try c.a().create(ast.Node.InfixOp); bitand_node.* = .{ .op_token = op_token, .lhs = node, .op = .BitAnd, .rhs = rhs, }; node = &bitand_node.base; }, .Plus => { const op_token = try appendToken(c, .Plus, "+"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const add_node = try c.a().create(ast.Node.InfixOp); add_node.* = .{ .op_token = op_token, .lhs = node, .op = .Add, .rhs = rhs, }; node = &add_node.base; }, .Minus => { const op_token = try appendToken(c, .Minus, "-"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const sub_node = try c.a().create(ast.Node.InfixOp); sub_node.* = .{ .op_token = op_token, .lhs = node, .op = .Sub, .rhs = rhs, }; node = &sub_node.base; }, .And => { const op_token = try appendToken(c, .Keyword_and, "and"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const and_node = try c.a().create(ast.Node.InfixOp); and_node.* = .{ .op_token = op_token, .lhs = node, .op = .BoolAnd, .rhs = rhs, }; node = &and_node.base; }, .Or => { const op_token = try appendToken(c, .Keyword_or, "or"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const or_node = try c.a().create(ast.Node.InfixOp); or_node.* = .{ .op_token = op_token, .lhs = node, .op = .BoolOr, .rhs = rhs, }; node = &or_node.base; }, .Gt => { const op_token = try appendToken(c, .AngleBracketRight, ">"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const and_node = try c.a().create(ast.Node.InfixOp); and_node.* = .{ .op_token = op_token, .lhs = node, .op = .GreaterThan, .rhs = rhs, }; node = &and_node.base; }, .Gte => { const op_token = try appendToken(c, .AngleBracketRightEqual, ">="); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const and_node = try c.a().create(ast.Node.InfixOp); and_node.* = .{ .op_token = op_token, .lhs = node, .op = .GreaterOrEqual, .rhs = rhs, }; node = &and_node.base; }, .Lt => { const op_token = try appendToken(c, .AngleBracketLeft, "<"); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const and_node = try c.a().create(ast.Node.InfixOp); and_node.* = .{ .op_token = op_token, .lhs = node, .op = .LessThan, .rhs = rhs, }; node = &and_node.base; }, .Lte => { const op_token = try appendToken(c, .AngleBracketLeftEqual, "<="); const rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); const and_node = try c.a().create(ast.Node.InfixOp); and_node.* = .{ .op_token = op_token, .lhs = node, .op = .LessOrEqual, .rhs = rhs, }; node = &and_node.base; }, .LBrace => { const arr_node = try transCreateNodeArrayAccess(c, node); arr_node.op.ArrayAccess = try parseCPrefixOpExpr(c, it, source_loc, scope); arr_node.rtoken = try appendToken(c, .RBrace, "]"); node = &arr_node.base; if (it.next().?.id != .RBrace) { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: expected ']'", .{}, ); return error.ParseError; } }, .LParen => { const call_node = try transCreateNodeFnCall(c, node); while (true) { const arg = try parseCPrefixOpExpr(c, it, source_loc, scope); try call_node.op.Call.params.push(arg); const next = it.next().?; if (next.id == .Comma) _ = try appendToken(c, .Comma, ",") else if (next.id == .RParen) break else { try failDecl( c, source_loc, it.list.at(0).*.bytes, "unable to translate C expr: expected ',' or ')'", .{}, ); return error.ParseError; } } call_node.rtoken = try appendToken(c, .RParen, ")"); node = &call_node.base; }, else => { _ = it.prev(); return node; }, } } } fn parseCPrefixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: ZigClangSourceLocation, scope: *Scope) ParseError!*ast.Node { const op_tok = it.next().?; switch (op_tok.id) { .Bang => { const node = try transCreateNodePrefixOp(c, .BoolNot, .Bang, "!"); node.rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); return &node.base; }, .Minus => { const node = try transCreateNodePrefixOp(c, .Negation, .Minus, "-"); node.rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); return &node.base; }, .Tilde => { const node = try transCreateNodePrefixOp(c, .BitNot, .Tilde, "~"); node.rhs = try parseCPrefixOpExpr(c, it, source_loc, scope); return &node.base; }, .Asterisk => { const prefix_op_expr = try parseCPrefixOpExpr(c, it, source_loc, scope); return try transCreateNodePtrDeref(c, prefix_op_expr); }, else => { _ = it.prev(); return try parseCSuffixOpExpr(c, it, source_loc, scope); }, } } fn tokenSlice(c: *Context, token: ast.TokenIndex) []u8 { const tok = c.tree.tokens.at(token); const slice = c.source_buffer.toSlice()[tok.start..tok.end]; return if (mem.startsWith(u8, slice, "@\"")) slice[2 .. slice.len - 1] else slice; } fn getContainer(c: *Context, node: *ast.Node) ?*ast.Node { if (node.id == .ContainerDecl) { return node; } else if (node.id == .PrefixOp) { return node; } else if (node.cast(ast.Node.Identifier)) |ident| { if (c.global_scope.sym_table.get(tokenSlice(c, ident.token))) |kv| { if (kv.value.cast(ast.Node.VarDecl)) |var_decl| return getContainer(c, var_decl.init_node.?); } } else if (node.cast(ast.Node.InfixOp)) |infix| { if (infix.op != .Period) return null; if (getContainerTypeOf(c, infix.lhs)) |ty_node| { if (ty_node.cast(ast.Node.ContainerDecl)) |container| { var it = container.fields_and_decls.iterator(0); while (it.next()) |field_ref| { const field = field_ref.*.cast(ast.Node.ContainerField).?; const ident = infix.rhs.cast(ast.Node.Identifier).?; if (mem.eql(u8, tokenSlice(c, field.name_token), tokenSlice(c, ident.token))) { return getContainer(c, field.type_expr.?); } } } } } return null; } fn getContainerTypeOf(c: *Context, ref: *ast.Node) ?*ast.Node { if (ref.cast(ast.Node.Identifier)) |ident| { if (c.global_scope.sym_table.get(tokenSlice(c, ident.token))) |kv| { if (kv.value.cast(ast.Node.VarDecl)) |var_decl| { if (var_decl.type_node) |ty| return getContainer(c, ty); } } } else if (ref.cast(ast.Node.InfixOp)) |infix| { if (infix.op != .Period) return null; if (getContainerTypeOf(c, infix.lhs)) |ty_node| { if (ty_node.cast(ast.Node.ContainerDecl)) |container| { var it = container.fields_and_decls.iterator(0); while (it.next()) |field_ref| { const field = field_ref.*.cast(ast.Node.ContainerField).?; const ident = infix.rhs.cast(ast.Node.Identifier).?; if (mem.eql(u8, tokenSlice(c, field.name_token), tokenSlice(c, ident.token))) { return getContainer(c, field.type_expr.?); } } } else return ty_node; } } return null; } fn getFnProto(c: *Context, ref: *ast.Node) ?*ast.Node.FnProto { const init = if (ref.cast(ast.Node.VarDecl)) |v| v.init_node.? else return null; if (getContainerTypeOf(c, init)) |ty_node| { if (ty_node.cast(ast.Node.PrefixOp)) |prefix| { if (prefix.op == .OptionalType) { if (prefix.rhs.cast(ast.Node.FnProto)) |fn_proto| { return fn_proto; } } } } return null; } fn addMacros(c: *Context) !void { var macro_it = c.global_scope.macro_table.iterator(); while (macro_it.next()) |kv| { if (getFnProto(c, kv.value)) |proto_node| { // If a macro aliases a global variable which is a function pointer, we conclude that // the macro is intended to represent a function that assumes the function pointer // variable is non-null and calls it. try addTopLevelDecl(c, kv.key, try transCreateNodeMacroFn(c, kv.key, kv.value, proto_node)); } else { try addTopLevelDecl(c, kv.key, kv.value); } } }
const Builder = @import("std").build.Builder; pub fn build(b: *Builder) void { const main = b.addTest("main.zig"); main.setBuildMode(b.standardReleaseOptions()); main.pie = true; const test_step = b.step("test", "Test the program"); test_step.dependOn(&main.step); b.default_step.dependOn(test_step); }
const std = @import("std"); const Builder = std.build.Builder; pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); const tests = b.addTest("test.zig"); tests.setBuildMode(mode); tests.addPackagePath("zuri", "zuri/src/zuri.zig"); const test_step = b.step("test", "Run library tests"); test_step.dependOn(&tests.step); var basic = b.addExecutable("basic", "examples/basic.zig"); basic.setBuildMode(mode); basic.addPackage(.{ .name = "routez", .path = "src/routez.zig", .dependencies = &[_]std.build.Pkg{.{ .name = "zuri", .path = "zuri/src/zuri.zig", }}, }); basic.setOutputDir("zig-cache"); basic.install(); const basic_step = b.step("basic", "Basic example"); basic_step.dependOn(&basic.run().step); }
// // Using `catch` to replace an error with a default value is a bit // of a blunt instrument since it doesn't matter what the error is. // // Catch lets us capture the error value and perform additional // actions with this form: // // canFail() catch |err| { // if (err == FishError.TunaMalfunction) { // ... // } // }; // const std = @import("std"); const MyNumberError = error{ TooSmall, TooBig, }; pub fn main() void { // The "catch 0" below is just our way of dealing with the fact // that makeJustRight() returns a error union (for now). var a: u32 = makeJustRight(44) catch 0; var b: u32 = makeJustRight(14) catch 0; var c: u32 = makeJustRight(4) catch 0; std.debug.print("a={}, b={}, c={}\n", .{ a, b, c }); } // In this silly example we've split the responsibility of making // a number just right into four (!) functions: // // makeJustRight() Calls fixTooBig(), cannot fix any errors. // fixTooBig() Calls fixTooSmall(), fixes TooBig errors. // fixTooSmall() Calls detectProblems(), fixes TooSmall errors. // detectProblems() Returns the number or an error. // fn makeJustRight(n: u32) MyNumberError!u32 { return fixTooBig(n) catch |err| { return err; }; } fn fixTooBig(n: u32) MyNumberError!u32 { return fixTooSmall(n) catch |err| { if (err == MyNumberError.TooBig) { return 20; } return err; }; } fn fixTooSmall(n: u32) MyNumberError!u32 { // Oh dear, this is missing a lot! But don't worry, it's nearly // identical to fixTooBig() above. // // If we get a TooSmall error, we should return 10. // If we get any other error, we should return that error. // Otherwise, we return the u32 number. return detectProblems(n) catch |err| { if (err == MyNumberError.TooSmall) { return 10; } return err; }; } fn detectProblems(n: u32) MyNumberError!u32 { if (n < 10) return MyNumberError.TooSmall; if (n > 20) return MyNumberError.TooBig; return n; }
const std = @import("std"); const llvm = @import("codegen/llvm/bindings.zig"); pub const ArchOsAbi = struct { arch: std.Target.Cpu.Arch, os: std.Target.Os.Tag, abi: std.Target.Abi, os_ver: ?std.builtin.Version = null, }; pub const available_libcs = [_]ArchOsAbi{ .{ .arch = .aarch64_be, .os = .linux, .abi = .gnu }, .{ .arch = .aarch64_be, .os = .linux, .abi = .musl }, .{ .arch = .aarch64_be, .os = .windows, .abi = .gnu }, .{ .arch = .aarch64, .os = .linux, .abi = .gnu }, .{ .arch = .aarch64, .os = .linux, .abi = .musl }, .{ .arch = .aarch64, .os = .windows, .abi = .gnu }, .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 11, .minor = 0 } }, .{ .arch = .aarch64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 12, .minor = 0 } }, .{ .arch = .armeb, .os = .linux, .abi = .gnueabi }, .{ .arch = .armeb, .os = .linux, .abi = .gnueabihf }, .{ .arch = .armeb, .os = .linux, .abi = .musleabi }, .{ .arch = .armeb, .os = .linux, .abi = .musleabihf }, .{ .arch = .armeb, .os = .windows, .abi = .gnu }, .{ .arch = .arm, .os = .linux, .abi = .gnueabi }, .{ .arch = .arm, .os = .linux, .abi = .gnueabihf }, .{ .arch = .arm, .os = .linux, .abi = .musleabi }, .{ .arch = .arm, .os = .linux, .abi = .musleabihf }, .{ .arch = .thumb, .os = .linux, .abi = .gnueabi }, .{ .arch = .thumb, .os = .linux, .abi = .gnueabihf }, .{ .arch = .thumb, .os = .linux, .abi = .musleabi }, .{ .arch = .thumb, .os = .linux, .abi = .musleabihf }, .{ .arch = .arm, .os = .windows, .abi = .gnu }, .{ .arch = .csky, .os = .linux, .abi = .gnueabi }, .{ .arch = .csky, .os = .linux, .abi = .gnueabihf }, .{ .arch = .i386, .os = .linux, .abi = .gnu }, .{ .arch = .i386, .os = .linux, .abi = .musl }, .{ .arch = .i386, .os = .windows, .abi = .gnu }, .{ .arch = .m68k, .os = .linux, .abi = .gnu }, .{ .arch = .m68k, .os = .linux, .abi = .musl }, .{ .arch = .mips64el, .os = .linux, .abi = .gnuabi64 }, .{ .arch = .mips64el, .os = .linux, .abi = .gnuabin32 }, .{ .arch = .mips64el, .os = .linux, .abi = .musl }, .{ .arch = .mips64, .os = .linux, .abi = .gnuabi64 }, .{ .arch = .mips64, .os = .linux, .abi = .gnuabin32 }, .{ .arch = .mips64, .os = .linux, .abi = .musl }, .{ .arch = .mipsel, .os = .linux, .abi = .gnueabi }, .{ .arch = .mipsel, .os = .linux, .abi = .gnueabihf }, .{ .arch = .mipsel, .os = .linux, .abi = .musl }, .{ .arch = .mips, .os = .linux, .abi = .gnueabi }, .{ .arch = .mips, .os = .linux, .abi = .gnueabihf }, .{ .arch = .mips, .os = .linux, .abi = .musl }, .{ .arch = .powerpc64le, .os = .linux, .abi = .gnu }, .{ .arch = .powerpc64le, .os = .linux, .abi = .musl }, .{ .arch = .powerpc64, .os = .linux, .abi = .gnu }, .{ .arch = .powerpc64, .os = .linux, .abi = .musl }, .{ .arch = .powerpc, .os = .linux, .abi = .gnueabi }, .{ .arch = .powerpc, .os = .linux, .abi = .gnueabihf }, .{ .arch = .powerpc, .os = .linux, .abi = .musl }, .{ .arch = .riscv64, .os = .linux, .abi = .gnu }, .{ .arch = .riscv64, .os = .linux, .abi = .musl }, .{ .arch = .s390x, .os = .linux, .abi = .gnu }, .{ .arch = .s390x, .os = .linux, .abi = .musl }, .{ .arch = .sparc, .os = .linux, .abi = .gnu }, .{ .arch = .sparcv9, .os = .linux, .abi = .gnu }, .{ .arch = .wasm32, .os = .freestanding, .abi = .musl }, .{ .arch = .wasm32, .os = .wasi, .abi = .musl }, .{ .arch = .x86_64, .os = .linux, .abi = .gnu }, .{ .arch = .x86_64, .os = .linux, .abi = .gnux32 }, .{ .arch = .x86_64, .os = .linux, .abi = .musl }, .{ .arch = .x86_64, .os = .windows, .abi = .gnu }, .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 10, .minor = 0 } }, .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 11, .minor = 0 } }, .{ .arch = .x86_64, .os = .macos, .abi = .gnu, .os_ver = .{ .major = 12, .minor = 0 } }, }; pub fn libCGenericName(target: std.Target) [:0]const u8 { switch (target.os.tag) { .windows => return "mingw", .macos, .ios, .tvos, .watchos => return "darwin", else => {}, } switch (target.abi) { .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32, .gnuilp32, => return "glibc", .musl, .musleabi, .musleabihf, .muslx32, .none, => return "musl", .code16, .eabi, .eabihf, .android, .msvc, .itanium, .cygnus, .coreclr, .simulator, .macabi, => unreachable, } } pub fn osArchName(target: std.Target) [:0]const u8 { return switch (target.os.tag) { .linux => switch (target.cpu.arch) { .arm, .armeb, .thumb, .thumbeb => "arm", .aarch64, .aarch64_be, .aarch64_32 => "arm64", .mips, .mipsel, .mips64, .mips64el => "mips", .powerpc, .powerpcle, .powerpc64, .powerpc64le => "powerpc", .riscv32, .riscv64 => "riscv", .sparc, .sparcel, .sparcv9 => "sparc", .i386, .x86_64 => "x86", else => @tagName(target.cpu.arch), }, else => @tagName(target.cpu.arch), }; } pub fn canBuildLibC(target: std.Target) bool { for (available_libcs) |libc| { if (target.cpu.arch == libc.arch and target.os.tag == libc.os and target.abi == libc.abi) { if (target.os.tag == .macos) { const ver = target.os.version_range.semver; if (ver.min.major != libc.os_ver.?.major) continue; // no match, keep going } return true; } } return false; } pub fn cannotDynamicLink(target: std.Target) bool { return switch (target.os.tag) { .freestanding, .other => true, else => false, }; } /// On Darwin, we always link libSystem which contains libc. /// Similarly on FreeBSD and NetBSD we always link system libc /// since this is the stable syscall interface. pub fn osRequiresLibC(target: std.Target) bool { return target.os.requiresLibC(); } pub fn libcNeedsLibUnwind(target: std.Target) bool { return switch (target.os.tag) { .macos, .ios, .watchos, .tvos, .freestanding, .wasi, // Wasm/WASI currently doesn't offer support for libunwind, so don't link it. => false, .windows => target.abi != .msvc, else => true, }; } pub fn requiresPIE(target: std.Target) bool { return target.isAndroid() or target.isDarwin() or target.os.tag == .openbsd; } /// This function returns whether non-pic code is completely invalid on the given target. pub fn requiresPIC(target: std.Target, linking_libc: bool) bool { return target.isAndroid() or target.os.tag == .windows or target.os.tag == .uefi or osRequiresLibC(target) or (linking_libc and target.isGnuLibC()); } /// This is not whether the target supports Position Independent Code, but whether the -fPIC /// C compiler argument is valid to Clang. pub fn supports_fpic(target: std.Target) bool { return target.os.tag != .windows; } pub fn isSingleThreaded(target: std.Target) bool { return target.isWasm(); } /// Valgrind supports more, but Zig does not support them yet. pub fn hasValgrindSupport(target: std.Target) bool { switch (target.cpu.arch) { .x86_64 => { return target.os.tag == .linux or target.os.tag == .solaris or (target.os.tag == .windows and target.abi != .msvc); }, else => return false, } } /// The set of targets that LLVM has non-experimental support for. /// Used to select between LLVM backend and self-hosted backend when compiling in /// release modes. pub fn hasLlvmSupport(target: std.Target) bool { return switch (target.cpu.arch) { .arm, .armeb, .aarch64, .aarch64_be, .aarch64_32, .arc, .avr, .bpfel, .bpfeb, .csky, .hexagon, .m68k, .mips, .mipsel, .mips64, .mips64el, .msp430, .powerpc, .powerpcle, .powerpc64, .powerpc64le, .r600, .amdgcn, .riscv32, .riscv64, .sparc, .sparcv9, .sparcel, .s390x, .tce, .tcele, .thumb, .thumbeb, .i386, .x86_64, .xcore, .nvptx, .nvptx64, .le32, .le64, .amdil, .amdil64, .hsail, .hsail64, .spir, .spir64, .kalimba, .shave, .lanai, .wasm32, .wasm64, .renderscript32, .renderscript64, .ve, => true, .spu_2, .spirv32, .spirv64, => false, }; } pub fn supportsStackProbing(target: std.Target) bool { return target.os.tag != .windows and target.os.tag != .uefi and (target.cpu.arch == .i386 or target.cpu.arch == .x86_64); } pub fn osToLLVM(os_tag: std.Target.Os.Tag) llvm.OSType { return switch (os_tag) { .freestanding, .other, .opencl, .glsl450, .vulkan, .plan9 => .UnknownOS, .windows, .uefi => .Win32, .ananas => .Ananas, .cloudabi => .CloudABI, .dragonfly => .DragonFly, .freebsd => .FreeBSD, .fuchsia => .Fuchsia, .ios => .IOS, .kfreebsd => .KFreeBSD, .linux => .Linux, .lv2 => .Lv2, .macos => .MacOSX, .netbsd => .NetBSD, .openbsd => .OpenBSD, .solaris => .Solaris, .zos => .ZOS, .haiku => .Haiku, .minix => .Minix, .rtems => .RTEMS, .nacl => .NaCl, .aix => .AIX, .cuda => .CUDA, .nvcl => .NVCL, .amdhsa => .AMDHSA, .ps4 => .PS4, .elfiamcu => .ELFIAMCU, .tvos => .TvOS, .watchos => .WatchOS, .mesa3d => .Mesa3D, .contiki => .Contiki, .amdpal => .AMDPAL, .hermit => .HermitCore, .hurd => .Hurd, .wasi => .WASI, .emscripten => .Emscripten, }; } pub fn archToLLVM(arch_tag: std.Target.Cpu.Arch) llvm.ArchType { return switch (arch_tag) { .arm => .arm, .armeb => .armeb, .aarch64 => .aarch64, .aarch64_be => .aarch64_be, .aarch64_32 => .aarch64_32, .arc => .arc, .avr => .avr, .bpfel => .bpfel, .bpfeb => .bpfeb, .csky => .csky, .hexagon => .hexagon, .m68k => .m68k, .mips => .mips, .mipsel => .mipsel, .mips64 => .mips64, .mips64el => .mips64el, .msp430 => .msp430, .powerpc => .ppc, .powerpcle => .ppcle, .powerpc64 => .ppc64, .powerpc64le => .ppc64le, .r600 => .r600, .amdgcn => .amdgcn, .riscv32 => .riscv32, .riscv64 => .riscv64, .sparc => .sparc, .sparcv9 => .sparcv9, .sparcel => .sparcel, .s390x => .systemz, .tce => .tce, .tcele => .tcele, .thumb => .thumb, .thumbeb => .thumbeb, .i386 => .x86, .x86_64 => .x86_64, .xcore => .xcore, .nvptx => .nvptx, .nvptx64 => .nvptx64, .le32 => .le32, .le64 => .le64, .amdil => .amdil, .amdil64 => .amdil64, .hsail => .hsail, .hsail64 => .hsail64, .spir => .spir, .spir64 => .spir64, .kalimba => .kalimba, .shave => .shave, .lanai => .lanai, .wasm32 => .wasm32, .wasm64 => .wasm64, .renderscript32 => .renderscript32, .renderscript64 => .renderscript64, .ve => .ve, .spu_2, .spirv32, .spirv64 => .UnknownArch, }; } fn eqlIgnoreCase(ignore_case: bool, a: []const u8, b: []const u8) bool { if (ignore_case) { return std.ascii.eqlIgnoreCase(a, b); } else { return std.mem.eql(u8, a, b); } } pub fn is_libc_lib_name(target: std.Target, name: []const u8) bool { const ignore_case = target.os.tag.isDarwin() or target.os.tag == .windows; if (eqlIgnoreCase(ignore_case, name, "c")) return true; if (target.isMinGW()) { if (eqlIgnoreCase(ignore_case, name, "m")) return true; return false; } if (target.abi.isGnu() or target.abi.isMusl() or target.os.tag.isDarwin()) { if (eqlIgnoreCase(ignore_case, name, "m")) return true; if (eqlIgnoreCase(ignore_case, name, "rt")) return true; if (eqlIgnoreCase(ignore_case, name, "pthread")) return true; if (eqlIgnoreCase(ignore_case, name, "crypt")) return true; if (eqlIgnoreCase(ignore_case, name, "util")) return true; if (eqlIgnoreCase(ignore_case, name, "xnet")) return true; if (eqlIgnoreCase(ignore_case, name, "resolv")) return true; if (eqlIgnoreCase(ignore_case, name, "dl")) return true; } if (target.os.tag.isDarwin() and eqlIgnoreCase(ignore_case, name, "System")) return true; return false; } pub fn is_libcpp_lib_name(target: std.Target, name: []const u8) bool { const ignore_case = target.os.tag.isDarwin() or target.os.tag == .windows; return eqlIgnoreCase(ignore_case, name, "c++") or eqlIgnoreCase(ignore_case, name, "stdc++") or eqlIgnoreCase(ignore_case, name, "c++abi"); } pub fn hasDebugInfo(target: std.Target) bool { return !target.cpu.arch.isWasm(); } pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode { if (target.cpu.arch.isWasm() and target.os.tag == .freestanding) { return .ReleaseSmall; } else { return .ReleaseFast; } } pub fn hasRedZone(target: std.Target) bool { return switch (target.cpu.arch) { .x86_64, .i386, .powerpc, .powerpc64, .powerpc64le, .aarch64, .aarch64_be, .aarch64_32, => true, else => false, }; } pub fn libcFullLinkFlags(target: std.Target) []const []const u8 { // The linking order of these is significant and should match the order other // c compilers such as gcc or clang use. return switch (target.os.tag) { .netbsd, .openbsd => &[_][]const u8{ "-lm", "-lpthread", "-lc", "-lutil", }, .solaris => &[_][]const u8{ "-lm", "-lsocket", "-lnsl", // Solaris releases after 10 merged the threading libraries into libc. "-lc", }, .haiku => &[_][]const u8{ "-lm", "-lroot", "-lpthread", "-lc", }, else => switch (target.abi) { .android => &[_][]const u8{ "-lm", "-lc", "-ldl", }, else => &[_][]const u8{ "-lm", "-lpthread", "-lc", "-ldl", "-lrt", "-lutil", }, }, }; } pub fn clangMightShellOutForAssembly(target: std.Target) bool { // Clang defaults to using the system assembler over the internal one // when targeting a non-BSD OS. return target.cpu.arch.isSPARC(); } /// Each backend architecture in Clang has a different codepath which may or may not /// support an -mcpu flag. pub fn clangAssemblerSupportsMcpuArg(target: std.Target) bool { return switch (target.cpu.arch) { .arm, .armeb, .thumb, .thumbeb => true, else => false, }; } pub fn needUnwindTables(target: std.Target) bool { return target.os.tag == .windows; } /// TODO this was ported from stage1 but it does not take into account CPU features, /// which can affect this value. Audit this! pub fn largestAtomicBits(target: std.Target) u32 { return switch (target.cpu.arch) { .avr, .msp430, .spu_2, => 16, .arc, .arm, .armeb, .hexagon, .m68k, .le32, .mips, .mipsel, .nvptx, .powerpc, .powerpcle, .r600, .riscv32, .sparc, .sparcel, .tce, .tcele, .thumb, .thumbeb, .i386, .xcore, .amdil, .hsail, .spir, .kalimba, .lanai, .shave, .wasm32, .renderscript32, .csky, .spirv32, => 32, .aarch64, .aarch64_be, .aarch64_32, .amdgcn, .bpfel, .bpfeb, .le64, .mips64, .mips64el, .nvptx64, .powerpc64, .powerpc64le, .riscv64, .sparcv9, .s390x, .amdil64, .hsail64, .spir64, .wasm64, .renderscript64, .ve, .spirv64, => 64, .x86_64 => 128, }; } pub fn defaultAddressSpace( target: std.Target, context: enum { /// Query the default address space for global constant values. global_constant, /// Query the default address space for global mutable values. global_mutable, /// Query the default address space for function-local values. local, /// Query the default address space for functions themselves. function, }, ) std.builtin.AddressSpace { _ = target; _ = context; return .generic; } pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 { const have_float = switch (target.abi) { .gnuilp32 => return "ilp32", .gnueabihf, .musleabihf, .eabihf => true, else => false, }; switch (target.cpu.arch) { .riscv64 => { const featureSetHas = std.Target.riscv.featureSetHas; if (featureSetHas(target.cpu.features, .d)) { return "lp64d"; } else if (have_float) { return "lp64f"; } else { return "lp64"; } }, .riscv32 => { const featureSetHas = std.Target.riscv.featureSetHas; if (featureSetHas(target.cpu.features, .d)) { return "ilp32d"; } else if (have_float) { return "ilp32f"; } else if (featureSetHas(target.cpu.features, .e)) { return "ilp32e"; } else { return "ilp32"; } }, //TODO add ARM, Mips, and PowerPC else => return null, } } pub fn defaultFunctionAlignment(target: std.Target) u32 { return switch (target.cpu.arch) { .arm, .armeb => 4, .aarch64, .aarch64_32, .aarch64_be => 4, .riscv64 => 2, else => 1, }; }
const std = @import("std"); const Options = @import("../../build.zig").Options; const content_dir = "directml_convolution_test_content/"; pub fn build(b: *std.build.Builder, options: Options) *std.build.LibExeObjStep { const exe_options = b.addOptions(); exe_options.addOption(bool, "enable_pix", options.enable_pix); exe_options.addOption(bool, "enable_dx_debug", options.enable_dx_debug); exe_options.addOption(bool, "enable_dx_gpu_debug", options.enable_dx_gpu_debug); exe_options.addOption(bool, "enable_tracy", options.tracy != null); exe_options.addOption(bool, "enable_d2d", false); exe_options.addOption([]const u8, "content_dir", content_dir); const exe = b.addExecutable("directml_convolution_test", thisDir() ++ "/src/directml_convolution_test.zig"); exe.setBuildMode(options.build_mode); exe.setTarget(options.target); exe.addOptions("build_options", exe_options); const dxc_step = buildShaders(b); const install_content_step = b.addInstallDirectory(.{ .source_dir = thisDir() ++ "/" ++ content_dir, .install_dir = .{ .custom = "" }, .install_subdir = "bin/" ++ content_dir, }); install_content_step.step.dependOn(dxc_step); exe.step.dependOn(&install_content_step.step); exe.step.dependOn( &b.addInstallFile( .{ .path = thisDir() ++ "/../../libs/zwin32/bin/x64/DirectML.dll" }, "bin/DirectML.dll", ).step, ); exe.step.dependOn( &b.addInstallFile( .{ .path = thisDir() ++ "/../../libs/zwin32/bin/x64/DirectML.pdb" }, "bin/DirectML.pdb", ).step, ); exe.step.dependOn( &b.addInstallFile( .{ .path = thisDir() ++ "/../../libs/zwin32/bin/x64/DirectML.Debug.dll" }, "bin/DirectML.Debug.dll", ).step, ); exe.step.dependOn( &b.addInstallFile( .{ .path = thisDir() ++ "/../../libs/zwin32/bin/x64/DirectML.Debug.pdb" }, "bin/DirectML.Debug.pdb", ).step, ); // This is needed to export symbols from an .exe file. // We export D3D12SDKVersion and D3D12SDKPath symbols which // is required by DirectX 12 Agility SDK. exe.rdynamic = true; exe.want_lto = false; const options_pkg = std.build.Pkg{ .name = "build_options", .path = exe_options.getSource(), }; const zwin32_pkg = std.build.Pkg{ .name = "zwin32", .path = .{ .path = thisDir() ++ "/../../libs/zwin32/zwin32.zig" }, }; exe.addPackage(zwin32_pkg); const ztracy_pkg = std.build.Pkg{ .name = "ztracy", .path = .{ .path = thisDir() ++ "/../../libs/ztracy/src/ztracy.zig" }, .dependencies = &[_]std.build.Pkg{options_pkg}, }; exe.addPackage(ztracy_pkg); @import("../../libs/ztracy/build.zig").link(b, exe, .{ .tracy_path = options.tracy }); const zd3d12_pkg = std.build.Pkg{ .name = "zd3d12", .path = .{ .path = thisDir() ++ "/../../libs/zd3d12/src/zd3d12.zig" }, .dependencies = &[_]std.build.Pkg{ zwin32_pkg, ztracy_pkg, options_pkg, }, }; exe.addPackage(zd3d12_pkg); @import("../../libs/zd3d12/build.zig").link(b, exe); const common_pkg = std.build.Pkg{ .name = "common", .path = .{ .path = thisDir() ++ "/../../libs/common/src/common.zig" }, .dependencies = &[_]std.build.Pkg{ zwin32_pkg, zd3d12_pkg, ztracy_pkg, options_pkg, }, }; exe.addPackage(common_pkg); @import("../../libs/common/build.zig").link(b, exe); return exe; } fn buildShaders(b: *std.build.Builder) *std.build.Step { const dxc_step = b.step("directml_convolution_test-dxc", "Build shaders for 'directml_convolution_test' demo"); var dxc_command = makeDxcCmd( "../../libs/common/src/hlsl/common.hlsl", "vsImGui", "imgui.vs.cso", "vs", "PSO__IMGUI", ); dxc_step.dependOn(&b.addSystemCommand(&dxc_command).step); dxc_command = makeDxcCmd( "../../libs/common/src/hlsl/common.hlsl", "psImGui", "imgui.ps.cso", "ps", "PSO__IMGUI", ); dxc_step.dependOn(&b.addSystemCommand(&dxc_command).step); dxc_command = makeDxcCmd( "src/directml_convolution_test.hlsl", "vsDrawTexture", "draw_texture.vs.cso", "vs", "PSO__DRAW_TEXTURE", ); dxc_step.dependOn(&b.addSystemCommand(&dxc_command).step); dxc_command = makeDxcCmd( "src/directml_convolution_test.hlsl", "psDrawTexture", "draw_texture.ps.cso", "ps", "PSO__DRAW_TEXTURE", ); dxc_step.dependOn(&b.addSystemCommand(&dxc_command).step); dxc_command = makeDxcCmd( "src/directml_convolution_test.hlsl", "csTextureToBuffer", "texture_to_buffer.cs.cso", "cs", "PSO__TEXTURE_TO_BUFFER", ); dxc_step.dependOn(&b.addSystemCommand(&dxc_command).step); dxc_command = makeDxcCmd( "src/directml_convolution_test.hlsl", "csBufferToTexture", "buffer_to_texture.cs.cso", "cs", "PSO__BUFFER_TO_TEXTURE", ); dxc_step.dependOn(&b.addSystemCommand(&dxc_command).step); return dxc_step; } fn makeDxcCmd( comptime input_path: []const u8, comptime entry_point: []const u8, comptime output_filename: []const u8, comptime profile: []const u8, comptime define: []const u8, ) [9][]const u8 { const shader_ver = "6_6"; const shader_dir = thisDir() ++ "/" ++ content_dir ++ "shaders/"; return [9][]const u8{ thisDir() ++ "/../../libs/zwin32/bin/x64/dxc.exe", thisDir() ++ "/" ++ input_path, "/E " ++ entry_point, "/Fo " ++ shader_dir ++ output_filename, "/T " ++ profile ++ "_" ++ shader_ver, if (define.len == 0) "" else "/D " ++ define, "/WX", "/Ges", "/O3", }; } fn thisDir() []const u8 { return std.fs.path.dirname(@src().file) orelse "."; }
const Coff = @This(); const std = @import("std"); const builtin = @import("builtin"); const log = std.log.scoped(.link); const Allocator = std.mem.Allocator; const assert = std.debug.assert; const fs = std.fs; const allocPrint = std.fmt.allocPrint; const mem = std.mem; const trace = @import("../tracy.zig").trace; const Module = @import("../Module.zig"); const Compilation = @import("../Compilation.zig"); const codegen = @import("../codegen.zig"); const link = @import("../link.zig"); const build_options = @import("build_options"); const Cache = @import("../Cache.zig"); const mingw = @import("../mingw.zig"); const Air = @import("../Air.zig"); const Liveness = @import("../Liveness.zig"); const LlvmObject = @import("../codegen/llvm.zig").Object; const allocation_padding = 4 / 3; const minimum_text_block_size = 64 * allocation_padding; const section_alignment = 4096; const file_alignment = 512; const default_image_base = 0x400_000; const section_table_size = 2 * 40; comptime { assert(mem.isAligned(default_image_base, section_alignment)); } pub const base_tag: link.File.Tag = .coff; const msdos_stub = @embedFile("msdos-stub.bin"); /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. llvm_object: ?*LlvmObject = null, base: link.File, ptr_width: PtrWidth, error_flags: link.File.ErrorFlags = .{}, text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{}, last_text_block: ?*TextBlock = null, /// Section table file pointer. section_table_offset: u32 = 0, /// Section data file pointer. section_data_offset: u32 = 0, /// Optional header file pointer. optional_header_offset: u32 = 0, /// Absolute virtual address of the offset table when the executable is loaded in memory. offset_table_virtual_address: u32 = 0, /// Current size of the offset table on disk, must be a multiple of `file_alignment` offset_table_size: u32 = 0, /// Contains absolute virtual addresses offset_table: std.ArrayListUnmanaged(u64) = .{}, /// Free list of offset table indices offset_table_free_list: std.ArrayListUnmanaged(u32) = .{}, /// Virtual address of the entry point procedure relative to image base. entry_addr: ?u32 = null, /// Absolute virtual address of the text section when the executable is loaded in memory. text_section_virtual_address: u32 = 0, /// Current size of the `.text` section on disk, must be a multiple of `file_alignment` text_section_size: u32 = 0, offset_table_size_dirty: bool = false, text_section_size_dirty: bool = false, /// This flag is set when the virtual size of the whole image file when loaded in memory has changed /// and needs to be updated in the optional header. size_of_image_dirty: bool = false, pub const PtrWidth = enum { p32, p64 }; pub const TextBlock = struct { /// Offset of the code relative to the start of the text section text_offset: u32, /// Used size of the text block size: u32, /// This field is undefined for symbols with size = 0. offset_table_index: u32, /// Points to the previous and next neighbors, based on the `text_offset`. /// This can be used to find, for example, the capacity of this `TextBlock`. prev: ?*TextBlock, next: ?*TextBlock, pub const empty = TextBlock{ .text_offset = 0, .size = 0, .offset_table_index = undefined, .prev = null, .next = null, }; /// Returns how much room there is to grow in virtual address space. fn capacity(self: TextBlock) u64 { if (self.next) |next| { return next.text_offset - self.text_offset; } // This is the last block, the capacity is only limited by the address space. return std.math.maxInt(u32) - self.text_offset; } fn freeListEligible(self: TextBlock) bool { // No need to keep a free list node for the last block. const next = self.next orelse return false; const cap = next.text_offset - self.text_offset; const ideal_cap = self.size * allocation_padding; if (cap <= ideal_cap) return false; const surplus = cap - ideal_cap; return surplus >= minimum_text_block_size; } /// Absolute virtual address of the text block when the file is loaded in memory. fn getVAddr(self: TextBlock, coff: Coff) u32 { return coff.text_section_virtual_address + self.text_offset; } }; pub const SrcFn = void; pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Options) !*Coff { assert(options.object_format == .coff); if (build_options.have_llvm and options.use_llvm) { const self = try createEmpty(allocator, options); errdefer self.base.destroy(); self.llvm_object = try LlvmObject.create(allocator, sub_path, options); return self; } const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options), }); errdefer file.close(); const self = try createEmpty(allocator, options); errdefer self.base.destroy(); self.base.file = file; // TODO Write object specific relocations, COFF symbol table, then enable object file output. switch (options.output_mode) { .Exe => {}, .Obj => return error.TODOImplementWritingObjFiles, .Lib => return error.TODOImplementWritingLibFiles, } var coff_file_header_offset: u32 = 0; if (options.output_mode == .Exe) { // Write the MS-DOS stub and the PE signature try self.base.file.?.pwriteAll(msdos_stub ++ "PE\x00\x00", 0); coff_file_header_offset = msdos_stub.len + 4; } // COFF file header const data_directory_count = 0; var hdr_data: [112 + data_directory_count * 8 + section_table_size]u8 = undefined; var index: usize = 0; const machine = self.base.options.target.cpu.arch.toCoffMachine(); if (machine == .Unknown) { return error.UnsupportedCOFFArchitecture; } mem.writeIntLittle(u16, hdr_data[0..2], @enumToInt(machine)); index += 2; // Number of sections (we only use .got, .text) mem.writeIntLittle(u16, hdr_data[index..][0..2], 2); index += 2; // TimeDateStamp (u32), PointerToSymbolTable (u32), NumberOfSymbols (u32) mem.set(u8, hdr_data[index..][0..12], 0); index += 12; const optional_header_size = switch (options.output_mode) { .Exe => data_directory_count * 8 + switch (self.ptr_width) { .p32 => @as(u16, 96), .p64 => 112, }, else => 0, }; const section_table_offset = coff_file_header_offset + 20 + optional_header_size; const default_offset_table_size = file_alignment; const default_size_of_code = 0; self.section_data_offset = mem.alignForwardGeneric(u32, self.section_table_offset + section_table_size, file_alignment); const section_data_relative_virtual_address = mem.alignForwardGeneric(u32, self.section_table_offset + section_table_size, section_alignment); self.offset_table_virtual_address = default_image_base + section_data_relative_virtual_address; self.offset_table_size = default_offset_table_size; self.section_table_offset = section_table_offset; self.text_section_virtual_address = default_image_base + section_data_relative_virtual_address + section_alignment; self.text_section_size = default_size_of_code; // Size of file when loaded in memory const size_of_image = mem.alignForwardGeneric(u32, self.text_section_virtual_address - default_image_base + default_size_of_code, section_alignment); mem.writeIntLittle(u16, hdr_data[index..][0..2], optional_header_size); index += 2; // Characteristics var characteristics: u16 = std.coff.IMAGE_FILE_DEBUG_STRIPPED | std.coff.IMAGE_FILE_RELOCS_STRIPPED; // TODO Remove debug info stripped flag when necessary if (options.output_mode == .Exe) { characteristics |= std.coff.IMAGE_FILE_EXECUTABLE_IMAGE; } switch (self.ptr_width) { .p32 => characteristics |= std.coff.IMAGE_FILE_32BIT_MACHINE, .p64 => characteristics |= std.coff.IMAGE_FILE_LARGE_ADDRESS_AWARE, } mem.writeIntLittle(u16, hdr_data[index..][0..2], characteristics); index += 2; assert(index == 20); try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset); if (options.output_mode == .Exe) { self.optional_header_offset = coff_file_header_offset + 20; // Optional header index = 0; mem.writeIntLittle(u16, hdr_data[0..2], switch (self.ptr_width) { .p32 => @as(u16, 0x10b), .p64 => 0x20b, }); index += 2; // Linker version (u8 + u8) mem.set(u8, hdr_data[index..][0..2], 0); index += 2; // SizeOfCode (UNUSED, u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32), BaseOfCode (UNUSED, u32) mem.set(u8, hdr_data[index..][0..20], 0); index += 20; if (self.ptr_width == .p32) { // Base of data relative to the image base (UNUSED) mem.set(u8, hdr_data[index..][0..4], 0); index += 4; // Image base address mem.writeIntLittle(u32, hdr_data[index..][0..4], default_image_base); index += 4; } else { // Image base address mem.writeIntLittle(u64, hdr_data[index..][0..8], default_image_base); index += 8; } // Section alignment mem.writeIntLittle(u32, hdr_data[index..][0..4], section_alignment); index += 4; // File alignment mem.writeIntLittle(u32, hdr_data[index..][0..4], file_alignment); index += 4; // Required OS version, 6.0 is vista mem.writeIntLittle(u16, hdr_data[index..][0..2], 6); index += 2; mem.writeIntLittle(u16, hdr_data[index..][0..2], 0); index += 2; // Image version mem.set(u8, hdr_data[index..][0..4], 0); index += 4; // Required subsystem version, same as OS version mem.writeIntLittle(u16, hdr_data[index..][0..2], 6); index += 2; mem.writeIntLittle(u16, hdr_data[index..][0..2], 0); index += 2; // Reserved zeroes (u32) mem.set(u8, hdr_data[index..][0..4], 0); index += 4; mem.writeIntLittle(u32, hdr_data[index..][0..4], size_of_image); index += 4; mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset); index += 4; // CheckSum (u32) mem.set(u8, hdr_data[index..][0..4], 0); index += 4; // Subsystem, TODO: Let users specify the subsystem, always CUI for now mem.writeIntLittle(u16, hdr_data[index..][0..2], 3); index += 2; // DLL characteristics mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x0); index += 2; switch (self.ptr_width) { .p32 => { // Size of stack reserve + commit mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000_000); index += 4; mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000); index += 4; // Size of heap reserve + commit mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x100_000); index += 4; mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000); index += 4; }, .p64 => { // Size of stack reserve + commit mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000_000); index += 8; mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000); index += 8; // Size of heap reserve + commit mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x100_000); index += 8; mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000); index += 8; }, } // Reserved zeroes mem.set(u8, hdr_data[index..][0..4], 0); index += 4; // Number of data directories mem.writeIntLittle(u32, hdr_data[index..][0..4], data_directory_count); index += 4; // Initialize data directories to zero mem.set(u8, hdr_data[index..][0 .. data_directory_count * 8], 0); index += data_directory_count * 8; assert(index == optional_header_size); } // Write section table. // First, the .got section hdr_data[index..][0..8].* = ".got\x00\x00\x00\x00".*; index += 8; if (options.output_mode == .Exe) { // Virtual size (u32) mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size); index += 4; // Virtual address (u32) mem.writeIntLittle(u32, hdr_data[index..][0..4], self.offset_table_virtual_address - default_image_base); index += 4; } else { mem.set(u8, hdr_data[index..][0..8], 0); index += 8; } // Size of raw data (u32) mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size); index += 4; // File pointer to the start of the section mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset); index += 4; // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16) mem.set(u8, hdr_data[index..][0..12], 0); index += 12; // Section flags mem.writeIntLittle(u32, hdr_data[index..][0..4], std.coff.IMAGE_SCN_CNT_INITIALIZED_DATA | std.coff.IMAGE_SCN_MEM_READ); index += 4; // Then, the .text section hdr_data[index..][0..8].* = ".text\x00\x00\x00".*; index += 8; if (options.output_mode == .Exe) { // Virtual size (u32) mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code); index += 4; // Virtual address (u32) mem.writeIntLittle(u32, hdr_data[index..][0..4], self.text_section_virtual_address - default_image_base); index += 4; } else { mem.set(u8, hdr_data[index..][0..8], 0); index += 8; } // Size of raw data (u32) mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code); index += 4; // File pointer to the start of the section mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset + default_offset_table_size); index += 4; // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16) mem.set(u8, hdr_data[index..][0..12], 0); index += 12; // Section flags mem.writeIntLittle( u32, hdr_data[index..][0..4], std.coff.IMAGE_SCN_CNT_CODE | std.coff.IMAGE_SCN_MEM_EXECUTE | std.coff.IMAGE_SCN_MEM_READ | std.coff.IMAGE_SCN_MEM_WRITE, ); index += 4; assert(index == optional_header_size + section_table_size); try self.base.file.?.pwriteAll(hdr_data[0..index], self.optional_header_offset); try self.base.file.?.setEndPos(self.section_data_offset + default_offset_table_size + default_size_of_code); return self; } pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Coff { const ptr_width: PtrWidth = switch (options.target.cpu.arch.ptrBitWidth()) { 0...32 => .p32, 33...64 => .p64, else => return error.UnsupportedCOFFArchitecture, }; const self = try gpa.create(Coff); self.* = .{ .base = .{ .tag = .coff, .options = options, .allocator = gpa, .file = null, }, .ptr_width = ptr_width, }; return self; } pub fn allocateDeclIndexes(self: *Coff, decl: *Module.Decl) !void { if (self.llvm_object) |_| return; try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1); if (self.offset_table_free_list.popOrNull()) |i| { decl.link.coff.offset_table_index = i; } else { decl.link.coff.offset_table_index = @intCast(u32, self.offset_table.items.len); _ = self.offset_table.addOneAssumeCapacity(); const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8; if (self.offset_table.items.len > self.offset_table_size / entry_size) { self.offset_table_size_dirty = true; } } self.offset_table.items[decl.link.coff.offset_table_index] = 0; } fn allocateTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 { const new_block_min_capacity = new_block_size * allocation_padding; // We use these to indicate our intention to update metadata, placing the new block, // and possibly removing a free list node. // It would be simpler to do it inside the for loop below, but that would cause a // problem if an error was returned later in the function. So this action // is actually carried out at the end of the function, when errors are no longer possible. var block_placement: ?*TextBlock = null; var free_list_removal: ?usize = null; const vaddr = blk: { var i: usize = 0; while (i < self.text_block_free_list.items.len) { const free_block = self.text_block_free_list.items[i]; const next_block_text_offset = free_block.text_offset + free_block.capacity(); const new_block_text_offset = mem.alignForwardGeneric(u64, free_block.getVAddr(self.*) + free_block.size, alignment) - self.text_section_virtual_address; if (new_block_text_offset < next_block_text_offset and next_block_text_offset - new_block_text_offset >= new_block_min_capacity) { block_placement = free_block; const remaining_capacity = next_block_text_offset - new_block_text_offset - new_block_min_capacity; if (remaining_capacity < minimum_text_block_size) { free_list_removal = i; } break :blk new_block_text_offset + self.text_section_virtual_address; } else { if (!free_block.freeListEligible()) { _ = self.text_block_free_list.swapRemove(i); } else { i += 1; } continue; } } else if (self.last_text_block) |last| { const new_block_vaddr = mem.alignForwardGeneric(u64, last.getVAddr(self.*) + last.size, alignment); block_placement = last; break :blk new_block_vaddr; } else { break :blk self.text_section_virtual_address; } }; const expand_text_section = block_placement == null or block_placement.?.next == null; if (expand_text_section) { const needed_size = @intCast(u32, mem.alignForwardGeneric(u64, vaddr + new_block_size - self.text_section_virtual_address, file_alignment)); if (needed_size > self.text_section_size) { const current_text_section_virtual_size = mem.alignForwardGeneric(u32, self.text_section_size, section_alignment); const new_text_section_virtual_size = mem.alignForwardGeneric(u32, needed_size, section_alignment); if (current_text_section_virtual_size != new_text_section_virtual_size) { self.size_of_image_dirty = true; // Write new virtual size var buf: [4]u8 = undefined; mem.writeIntLittle(u32, &buf, new_text_section_virtual_size); try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 8); } self.text_section_size = needed_size; self.text_section_size_dirty = true; } self.last_text_block = text_block; } text_block.text_offset = @intCast(u32, vaddr - self.text_section_virtual_address); text_block.size = @intCast(u32, new_block_size); // This function can also reallocate a text block. // In this case we need to "unplug" it from its previous location before // plugging it in to its new location. if (text_block.prev) |prev| { prev.next = text_block.next; } if (text_block.next) |next| { next.prev = text_block.prev; } if (block_placement) |big_block| { text_block.prev = big_block; text_block.next = big_block.next; big_block.next = text_block; } else { text_block.prev = null; text_block.next = null; } if (free_list_removal) |i| { _ = self.text_block_free_list.swapRemove(i); } return vaddr; } fn growTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 { const block_vaddr = text_block.getVAddr(self.*); const align_ok = mem.alignBackwardGeneric(u64, block_vaddr, alignment) == block_vaddr; const need_realloc = !align_ok or new_block_size > text_block.capacity(); if (!need_realloc) return @as(u64, block_vaddr); return self.allocateTextBlock(text_block, new_block_size, alignment); } fn shrinkTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64) void { text_block.size = @intCast(u32, new_block_size); if (text_block.capacity() - text_block.size >= minimum_text_block_size) { self.text_block_free_list.append(self.base.allocator, text_block) catch {}; } } fn freeTextBlock(self: *Coff, text_block: *TextBlock) void { var already_have_free_list_node = false; { var i: usize = 0; // TODO turn text_block_free_list into a hash map while (i < self.text_block_free_list.items.len) { if (self.text_block_free_list.items[i] == text_block) { _ = self.text_block_free_list.swapRemove(i); continue; } if (self.text_block_free_list.items[i] == text_block.prev) { already_have_free_list_node = true; } i += 1; } } if (self.last_text_block == text_block) { self.last_text_block = text_block.prev; } if (text_block.prev) |prev| { prev.next = text_block.next; if (!already_have_free_list_node and prev.freeListEligible()) { // The free list is heuristics, it doesn't have to be perfect, so we can // ignore the OOM here. self.text_block_free_list.append(self.base.allocator, prev) catch {}; } } if (text_block.next) |next| { next.prev = text_block.prev; } } fn writeOffsetTableEntry(self: *Coff, index: usize) !void { const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8; const endian = self.base.options.target.cpu.arch.endian(); const offset_table_start = self.section_data_offset; if (self.offset_table_size_dirty) { const current_raw_size = self.offset_table_size; const new_raw_size = self.offset_table_size * 2; log.debug("growing offset table from raw size {} to {}\n", .{ current_raw_size, new_raw_size }); // Move the text section to a new place in the executable const current_text_section_start = self.section_data_offset + current_raw_size; const new_text_section_start = self.section_data_offset + new_raw_size; const amt = try self.base.file.?.copyRangeAll(current_text_section_start, self.base.file.?, new_text_section_start, self.text_section_size); if (amt != self.text_section_size) return error.InputOutput; // Write the new raw size in the .got header var buf: [8]u8 = undefined; mem.writeIntLittle(u32, buf[0..4], new_raw_size); try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 16); // Write the new .text section file offset in the .text section header mem.writeIntLittle(u32, buf[0..4], new_text_section_start); try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 20); const current_virtual_size = mem.alignForwardGeneric(u32, self.offset_table_size, section_alignment); const new_virtual_size = mem.alignForwardGeneric(u32, new_raw_size, section_alignment); // If we had to move in the virtual address space, we need to fix the VAs in the offset table, as well as the virtual address of the `.text` section // and the virtual size of the `.got` section if (new_virtual_size != current_virtual_size) { log.debug("growing offset table from virtual size {} to {}\n", .{ current_virtual_size, new_virtual_size }); self.size_of_image_dirty = true; const va_offset = new_virtual_size - current_virtual_size; // Write .got virtual size mem.writeIntLittle(u32, buf[0..4], new_virtual_size); try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 8); // Write .text new virtual address self.text_section_virtual_address = self.text_section_virtual_address + va_offset; mem.writeIntLittle(u32, buf[0..4], self.text_section_virtual_address - default_image_base); try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 12); // Fix the VAs in the offset table for (self.offset_table.items) |*va, idx| { if (va.* != 0) { va.* += va_offset; switch (entry_size) { 4 => { mem.writeInt(u32, buf[0..4], @intCast(u32, va.*), endian); try self.base.file.?.pwriteAll(buf[0..4], offset_table_start + idx * entry_size); }, 8 => { mem.writeInt(u64, &buf, va.*, endian); try self.base.file.?.pwriteAll(&buf, offset_table_start + idx * entry_size); }, else => unreachable, } } } } self.offset_table_size = new_raw_size; self.offset_table_size_dirty = false; } // Write the new entry switch (entry_size) { 4 => { var buf: [4]u8 = undefined; mem.writeInt(u32, &buf, @intCast(u32, self.offset_table.items[index]), endian); try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size); }, 8 => { var buf: [8]u8 = undefined; mem.writeInt(u64, &buf, self.offset_table.items[index], endian); try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size); }, else => unreachable, } } pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void { if (build_options.skip_non_native and builtin.object_format != .coff) { @panic("Attempted to compile for object format that was disabled by build configuration"); } if (build_options.have_llvm) { if (self.llvm_object) |llvm_object| { return llvm_object.updateFunc(module, func, air, liveness); } } const tracy = trace(@src()); defer tracy.end(); var code_buffer = std.ArrayList(u8).init(self.base.allocator); defer code_buffer.deinit(); const decl = func.owner_decl; const res = try codegen.generateFunction( &self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none, ); const code = switch (res) { .appended => code_buffer.items, .fail => |em| { decl.analysis = .codegen_failure; try module.failed_decls.put(module.gpa, decl, em); return; }, }; return self.finishUpdateDecl(module, func.owner_decl, code); } pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void { if (build_options.skip_non_native and builtin.object_format != .coff) { @panic("Attempted to compile for object format that was disabled by build configuration"); } if (build_options.have_llvm) { if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(module, decl); } const tracy = trace(@src()); defer tracy.end(); if (decl.val.tag() == .extern_fn) { return; // TODO Should we do more when front-end analyzed extern decl? } // TODO COFF/PE debug information // TODO Implement exports var code_buffer = std.ArrayList(u8).init(self.base.allocator); defer code_buffer.deinit(); const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ .ty = decl.ty, .val = decl.val, }, &code_buffer, .none); const code = switch (res) { .externally_managed => |x| x, .appended => code_buffer.items, .fail => |em| { decl.analysis = .codegen_failure; try module.failed_decls.put(module.gpa, decl, em); return; }, }; return self.finishUpdateDecl(module, decl, code); } fn finishUpdateDecl(self: *Coff, module: *Module, decl: *Module.Decl, code: []const u8) !void { const required_alignment = decl.ty.abiAlignment(self.base.options.target); const curr_size = decl.link.coff.size; if (curr_size != 0) { const capacity = decl.link.coff.capacity(); const need_realloc = code.len > capacity or !mem.isAlignedGeneric(u32, decl.link.coff.text_offset, required_alignment); if (need_realloc) { const curr_vaddr = self.getDeclVAddr(decl); const vaddr = try self.growTextBlock(&decl.link.coff, code.len, required_alignment); log.debug("growing {s} from 0x{x} to 0x{x}\n", .{ decl.name, curr_vaddr, vaddr }); if (vaddr != curr_vaddr) { log.debug(" (writing new offset table entry)\n", .{}); self.offset_table.items[decl.link.coff.offset_table_index] = vaddr; try self.writeOffsetTableEntry(decl.link.coff.offset_table_index); } } else if (code.len < curr_size) { self.shrinkTextBlock(&decl.link.coff, code.len); } } else { const vaddr = try self.allocateTextBlock(&decl.link.coff, code.len, required_alignment); log.debug("allocated text block for {s} at 0x{x} (size: {Bi})\n", .{ mem.sliceTo(decl.name, 0), vaddr, std.fmt.fmtIntSizeDec(code.len), }); errdefer self.freeTextBlock(&decl.link.coff); self.offset_table.items[decl.link.coff.offset_table_index] = vaddr; try self.writeOffsetTableEntry(decl.link.coff.offset_table_index); } // Write the code into the file try self.base.file.?.pwriteAll(code, self.section_data_offset + self.offset_table_size + decl.link.coff.text_offset); // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated. const decl_exports = module.decl_exports.get(decl) orelse &[0]*Module.Export{}; return self.updateDeclExports(module, decl, decl_exports); } pub fn freeDecl(self: *Coff, decl: *Module.Decl) void { if (build_options.have_llvm) { if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl); } // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. self.freeTextBlock(&decl.link.coff); self.offset_table_free_list.append(self.base.allocator, decl.link.coff.offset_table_index) catch {}; } pub fn updateDeclExports( self: *Coff, module: *Module, decl: *Module.Decl, exports: []const *Module.Export, ) !void { if (build_options.skip_non_native and builtin.object_format != .coff) { @panic("Attempted to compile for object format that was disabled by build configuration"); } if (build_options.have_llvm) { if (self.llvm_object) |llvm_object| return llvm_object.updateDeclExports(module, decl, exports); } for (exports) |exp| { if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, ".text")) { try module.failed_exports.ensureUnusedCapacity(module.gpa, 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), ); continue; } } if (mem.eql(u8, exp.options.name, "_start")) { self.entry_addr = decl.link.coff.getVAddr(self.*) - default_image_base; } else { try module.failed_exports.ensureUnusedCapacity(module.gpa, 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: Exports other than '_start'", .{}), ); continue; } } } pub fn flush(self: *Coff, comp: *Compilation) !void { if (build_options.have_llvm and self.base.options.use_lld) { return self.linkWithLLD(comp); } else { switch (self.base.options.effectiveOutputMode()) { .Exe, .Obj => {}, .Lib => return error.TODOImplementWritingLibFiles, } return self.flushModule(comp); } } pub fn flushModule(self: *Coff, comp: *Compilation) !void { const tracy = trace(@src()); defer tracy.end(); if (build_options.have_llvm) { if (self.llvm_object) |llvm_object| { return try llvm_object.flushModule(comp); } } if (self.text_section_size_dirty) { // Write the new raw size in the .text header var buf: [4]u8 = undefined; mem.writeIntLittle(u32, &buf, self.text_section_size); try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 16); try self.base.file.?.setEndPos(self.section_data_offset + self.offset_table_size + self.text_section_size); self.text_section_size_dirty = false; } if (self.base.options.output_mode == .Exe and self.size_of_image_dirty) { const new_size_of_image = mem.alignForwardGeneric(u32, self.text_section_virtual_address - default_image_base + self.text_section_size, section_alignment); var buf: [4]u8 = undefined; mem.writeIntLittle(u32, &buf, new_size_of_image); try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 56); self.size_of_image_dirty = false; } if (self.entry_addr == null and self.base.options.output_mode == .Exe) { log.debug("flushing. no_entry_point_found = true\n", .{}); self.error_flags.no_entry_point_found = true; } else { log.debug("flushing. no_entry_point_found = false\n", .{}); self.error_flags.no_entry_point_found = false; if (self.base.options.output_mode == .Exe) { // Write AddressOfEntryPoint var buf: [4]u8 = undefined; mem.writeIntLittle(u32, &buf, self.entry_addr.?); try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 16); } } } fn linkWithLLD(self: *Coff, comp: *Compilation) !void { const tracy = trace(@src()); defer tracy.end(); var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator); defer arena_allocator.deinit(); const arena = &arena_allocator.allocator; const directory = self.base.options.emit.?.directory; // Just an alias to make it shorter to type. // If there is no Zig code to compile, then we should skip flushing the output file because it // will not be part of the linker line anyway. const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: { const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1; if (use_stage1) { const obj_basename = try std.zig.binNameAlloc(arena, .{ .root_name = self.base.options.root_name, .target = self.base.options.target, .output_mode = .Obj, }); const o_directory = module.zig_cache_artifact_directory; const full_obj_path = try o_directory.join(arena, &[_][]const u8{obj_basename}); break :blk full_obj_path; } try self.flushModule(comp); const obj_basename = self.base.intermediary_basename.?; const full_obj_path = try directory.join(arena, &[_][]const u8{obj_basename}); break :blk full_obj_path; } else null; const is_lib = self.base.options.output_mode == .Lib; const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib; const is_exe_or_dyn_lib = is_dyn_lib or self.base.options.output_mode == .Exe; const link_in_crt = self.base.options.link_libc and is_exe_or_dyn_lib; const target = self.base.options.target; // See link/Elf.zig for comments on how this mechanism works. const id_symlink_basename = "lld.id"; var man: Cache.Manifest = undefined; defer if (!self.base.options.disable_lld_caching) man.deinit(); var digest: [Cache.hex_digest_len]u8 = undefined; if (!self.base.options.disable_lld_caching) { man = comp.cache_parent.obtain(); self.base.releaseLock(); try man.addListOfFiles(self.base.options.objects); for (comp.c_object_table.keys()) |key| { _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); man.hash.addOptional(self.base.options.stack_size_override); man.hash.addOptional(self.base.options.image_base_override); man.hash.addListOfBytes(self.base.options.lib_dirs); man.hash.add(self.base.options.skip_linker_dependencies); if (self.base.options.link_libc) { man.hash.add(self.base.options.libc_installation != null); if (self.base.options.libc_installation) |libc_installation| { man.hash.addBytes(libc_installation.crt_dir.?); if (target.abi == .msvc) { man.hash.addBytes(libc_installation.msvc_lib_dir.?); man.hash.addBytes(libc_installation.kernel32_lib_dir.?); } } } link.hashAddSystemLibs(&man.hash, self.base.options.system_libs); man.hash.addOptional(self.base.options.subsystem); man.hash.add(self.base.options.is_test); man.hash.add(self.base.options.tsaware); man.hash.add(self.base.options.nxcompat); man.hash.add(self.base.options.dynamicbase); man.hash.addOptional(self.base.options.major_subsystem_version); man.hash.addOptional(self.base.options.minor_subsystem_version); // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. _ = try man.hit(); digest = man.final(); var prev_digest_buf: [digest.len]u8 = undefined; const prev_digest: []u8 = Cache.readSmallFile( directory.handle, id_symlink_basename, &prev_digest_buf, ) catch |err| blk: { log.debug("COFF LLD new_digest={s} error: {s}", .{ std.fmt.fmtSliceHexLower(&digest), @errorName(err) }); // Handle this as a cache miss. break :blk prev_digest_buf[0..0]; }; if (mem.eql(u8, prev_digest, &digest)) { log.debug("COFF LLD digest={s} match - skipping invocation", .{std.fmt.fmtSliceHexLower(&digest)}); // Hot diggity dog! The output binary is already there. self.base.lock = man.toOwnedLock(); return; } log.debug("COFF LLD prev_digest={s} new_digest={s}", .{ std.fmt.fmtSliceHexLower(prev_digest), std.fmt.fmtSliceHexLower(&digest) }); // We are about to change the output file to be different, so we invalidate the build hash now. directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) { error.FileNotFound => {}, else => |e| return e, }; } const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path}); if (self.base.options.output_mode == .Obj) { // LLD's COFF driver does not support the equivalent of `-r` so we do a simple file copy // here. TODO: think carefully about how we can avoid this redundant operation when doing // build-obj. See also the corresponding TODO in linkAsArchive. const the_object_path = blk: { if (self.base.options.objects.len != 0) break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; // TODO I think this is unreachable. Audit this situation when solving the above TODO // regarding eliding redundant object -> object transformations. return error.NoObjectsToLink; }; // This can happen when using --enable-cache and using the stage1 backend. In this case // we can skip the file copy. if (!mem.eql(u8, the_object_path, full_out_path)) { try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{}); } } else { // Create an LLD command line and invoke it. var argv = std.ArrayList([]const u8).init(self.base.allocator); defer argv.deinit(); // We will invoke ourselves as a child process to gain access to LLD. // This is necessary because LLD does not behave properly as a library - // it calls exit() and does not reset all global data between invocations. try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "lld-link" }); try argv.append("-ERRORLIMIT:0"); try argv.append("-NOLOGO"); if (!self.base.options.strip) { try argv.append("-DEBUG"); } if (self.base.options.lto) { switch (self.base.options.optimize_mode) { .Debug => {}, .ReleaseSmall => try argv.append("-OPT:lldlto=2"), .ReleaseFast, .ReleaseSafe => try argv.append("-OPT:lldlto=3"), } } if (self.base.options.output_mode == .Exe) { const stack_size = self.base.options.stack_size_override orelse 16777216; try argv.append(try allocPrint(arena, "-STACK:{d}", .{stack_size})); } if (self.base.options.image_base_override) |image_base| { try argv.append(try std.fmt.allocPrint(arena, "-BASE:{d}", .{image_base})); } if (target.cpu.arch == .i386) { try argv.append("-MACHINE:X86"); } else if (target.cpu.arch == .x86_64) { try argv.append("-MACHINE:X64"); } else if (target.cpu.arch.isARM()) { if (target.cpu.arch.ptrBitWidth() == 32) { try argv.append("-MACHINE:ARM"); } else { try argv.append("-MACHINE:ARM64"); } } if (is_dyn_lib) { try argv.append("-DLL"); } if (self.base.options.tsaware) { try argv.append("-tsaware"); } if (self.base.options.nxcompat) { try argv.append("-nxcompat"); } if (self.base.options.dynamicbase) { try argv.append("-dynamicbase"); } const subsystem_suffix = ss: { if (self.base.options.major_subsystem_version) |major| { if (self.base.options.minor_subsystem_version) |minor| { break :ss try allocPrint(arena, ",{d}.{d}", .{ major, minor }); } else { break :ss try allocPrint(arena, ",{d}", .{major}); } } break :ss ""; }; try argv.append(try allocPrint(arena, "-OUT:{s}", .{full_out_path})); if (self.base.options.implib_emit) |emit| { const implib_out_path = try emit.directory.join(arena, &[_][]const u8{emit.sub_path}); try argv.append(try allocPrint(arena, "-IMPLIB:{s}", .{implib_out_path})); } if (self.base.options.link_libc) { if (self.base.options.libc_installation) |libc_installation| { try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.crt_dir.?})); if (target.abi == .msvc) { try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.msvc_lib_dir.?})); try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.kernel32_lib_dir.?})); } } } for (self.base.options.lib_dirs) |lib_dir| { try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir})); } try argv.appendSlice(self.base.options.objects); for (comp.c_object_table.keys()) |key| { try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { try argv.append(p); } const resolved_subsystem: ?std.Target.SubSystem = blk: { if (self.base.options.subsystem) |explicit| break :blk explicit; switch (target.os.tag) { .windows => { if (self.base.options.module) |module| { if (module.stage1_flags.have_dllmain_crt_startup or is_dyn_lib) break :blk null; if (module.stage1_flags.have_c_main or self.base.options.is_test or module.stage1_flags.have_winmain_crt_startup or module.stage1_flags.have_wwinmain_crt_startup) { break :blk .Console; } if (module.stage1_flags.have_winmain or module.stage1_flags.have_wwinmain) break :blk .Windows; } }, .uefi => break :blk .EfiApplication, else => {}, } break :blk null; }; const Mode = enum { uefi, win32 }; const mode: Mode = mode: { if (resolved_subsystem) |subsystem| switch (subsystem) { .Console => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:console{s}", .{ subsystem_suffix, })); break :mode .win32; }, .EfiApplication => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_application{s}", .{ subsystem_suffix, })); break :mode .uefi; }, .EfiBootServiceDriver => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_boot_service_driver{s}", .{ subsystem_suffix, })); break :mode .uefi; }, .EfiRom => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_rom{s}", .{ subsystem_suffix, })); break :mode .uefi; }, .EfiRuntimeDriver => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_runtime_driver{s}", .{ subsystem_suffix, })); break :mode .uefi; }, .Native => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:native{s}", .{ subsystem_suffix, })); break :mode .win32; }, .Posix => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:posix{s}", .{ subsystem_suffix, })); break :mode .win32; }, .Windows => { try argv.append(try allocPrint(arena, "-SUBSYSTEM:windows{s}", .{ subsystem_suffix, })); break :mode .win32; }, } else if (target.os.tag == .uefi) { break :mode .uefi; } else { break :mode .win32; } }; switch (mode) { .uefi => try argv.appendSlice(&[_][]const u8{ "-BASE:0", "-ENTRY:EfiMain", "-OPT:REF", "-SAFESEH:NO", "-MERGE:.rdata=.data", "-ALIGN:32", "-NODEFAULTLIB", "-SECTION:.xdata,D", }), .win32 => { if (link_in_crt) { if (target.abi.isGnu()) { try argv.append("-lldmingw"); if (target.cpu.arch == .i386) { try argv.append("-ALTERNATENAME:__image_base__=___ImageBase"); } else { try argv.append("-ALTERNATENAME:__image_base__=__ImageBase"); } if (is_dyn_lib) { try argv.append(try comp.get_libc_crt_file(arena, "dllcrt2.obj")); if (target.cpu.arch == .i386) { try argv.append("-ALTERNATENAME:__DllMainCRTStartup@12=_DllMainCRTStartup@12"); } else { try argv.append("-ALTERNATENAME:_DllMainCRTStartup=DllMainCRTStartup"); } } else { try argv.append(try comp.get_libc_crt_file(arena, "crt2.obj")); } try argv.append(try comp.get_libc_crt_file(arena, "mingw32.lib")); try argv.append(try comp.get_libc_crt_file(arena, "mingwex.lib")); try argv.append(try comp.get_libc_crt_file(arena, "msvcrt-os.lib")); for (mingw.always_link_libs) |name| { if (!self.base.options.system_libs.contains(name)) { const lib_basename = try allocPrint(arena, "{s}.lib", .{name}); try argv.append(try comp.get_libc_crt_file(arena, lib_basename)); } } } else { const lib_str = switch (self.base.options.link_mode) { .Dynamic => "", .Static => "lib", }; const d_str = switch (self.base.options.optimize_mode) { .Debug => "d", else => "", }; switch (self.base.options.link_mode) { .Static => try argv.append(try allocPrint(arena, "libcmt{s}.lib", .{d_str})), .Dynamic => try argv.append(try allocPrint(arena, "msvcrt{s}.lib", .{d_str})), } try argv.append(try allocPrint(arena, "{s}vcruntime{s}.lib", .{ lib_str, d_str })); try argv.append(try allocPrint(arena, "{s}ucrt{s}.lib", .{ lib_str, d_str })); //Visual C++ 2015 Conformance Changes //https://msdn.microsoft.com/en-us/library/bb531344.aspx try argv.append("legacy_stdio_definitions.lib"); // msvcrt depends on kernel32 and ntdll try argv.append("kernel32.lib"); try argv.append("ntdll.lib"); } } else { try argv.append("-NODEFAULTLIB"); if (!is_lib) { if (self.base.options.module) |module| { if (module.stage1_flags.have_winmain_crt_startup) { try argv.append("-ENTRY:WinMainCRTStartup"); } else { try argv.append("-ENTRY:wWinMainCRTStartup"); } } else { try argv.append("-ENTRY:wWinMainCRTStartup"); } } } }, } // libc++ dep if (self.base.options.link_libcpp) { try argv.append(comp.libcxxabi_static_lib.?.full_object_path); try argv.append(comp.libcxx_static_lib.?.full_object_path); } // libunwind dep if (self.base.options.link_libunwind) { try argv.append(comp.libunwind_static_lib.?.full_object_path); } if (is_exe_or_dyn_lib and !self.base.options.skip_linker_dependencies) { if (!self.base.options.link_libc) { if (comp.libc_static_lib) |lib| { try argv.append(lib.full_object_path); } } // MinGW doesn't provide libssp symbols if (target.abi.isGnu()) { if (comp.libssp_static_lib) |lib| { try argv.append(lib.full_object_path); } } // MSVC compiler_rt is missing some stuff, so we build it unconditionally but // and rely on weak linkage to allow MSVC compiler_rt functions to override ours. if (comp.compiler_rt_static_lib) |lib| { try argv.append(lib.full_object_path); } } try argv.ensureUnusedCapacity(self.base.options.system_libs.count()); for (self.base.options.system_libs.keys()) |key| { const lib_basename = try allocPrint(arena, "{s}.lib", .{key}); if (comp.crt_files.get(lib_basename)) |crt_file| { argv.appendAssumeCapacity(crt_file.full_object_path); continue; } if (try self.findLib(arena, lib_basename)) |full_path| { argv.appendAssumeCapacity(full_path); continue; } if (target.abi.isGnu()) { const fallback_name = try allocPrint(arena, "lib{s}.dll.a", .{key}); if (try self.findLib(arena, fallback_name)) |full_path| { argv.appendAssumeCapacity(full_path); continue; } } log.err("DLL import library for -l{s} not found", .{key}); return error.DllImportLibraryNotFound; } if (self.base.options.verbose_link) { // Skip over our own name so that the LLD linker name is the first argv item. Compilation.dump_argv(argv.items[1..]); } // Sadly, we must run LLD as a child process because it does not behave // properly as a library. const child = try std.ChildProcess.init(argv.items, arena); defer child.deinit(); if (comp.clang_passthrough_mode) { child.stdin_behavior = .Inherit; child.stdout_behavior = .Inherit; child.stderr_behavior = .Inherit; const term = child.spawnAndWait() catch |err| { log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) }); return error.UnableToSpawnSelf; }; switch (term) { .Exited => |code| { if (code != 0) { // TODO https://github.com/ziglang/zig/issues/6342 std.process.exit(1); } }, else => std.process.abort(), } } else { child.stdin_behavior = .Ignore; child.stdout_behavior = .Ignore; child.stderr_behavior = .Pipe; try child.spawn(); const stderr = try child.stderr.?.reader().readAllAlloc(arena, 10 * 1024 * 1024); const term = child.wait() catch |err| { log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) }); return error.UnableToSpawnSelf; }; switch (term) { .Exited => |code| { if (code != 0) { // TODO parse this output and surface with the Compilation API rather than // directly outputting to stderr here. std.debug.print("{s}", .{stderr}); return error.LLDReportedFailure; } }, else => { log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr }); return error.LLDCrashed; }, } if (stderr.len != 0) { log.warn("unexpected LLD stderr:\n{s}", .{stderr}); } } } if (!self.base.options.disable_lld_caching) { // Update the file with the digest. If it fails we can continue; it only // means that the next invocation will have an unnecessary cache miss. Cache.writeSmallFile(directory.handle, id_symlink_basename, &digest) catch |err| { log.warn("failed to save linking hash digest file: {s}", .{@errorName(err)}); }; // Again failure here only means an unnecessary cache miss. man.writeManifest() catch |err| { log.warn("failed to write cache manifest when linking: {s}", .{@errorName(err)}); }; // We hang on to this lock so that the output file path can be used without // other processes clobbering it. self.base.lock = man.toOwnedLock(); } } fn findLib(self: *Coff, arena: *Allocator, name: []const u8) !?[]const u8 { for (self.base.options.lib_dirs) |lib_dir| { const full_path = try fs.path.join(arena, &.{ lib_dir, name }); fs.cwd().access(full_path, .{}) catch |err| switch (err) { error.FileNotFound => continue, else => |e| return e, }; return full_path; } return null; } pub fn getDeclVAddr(self: *Coff, decl: *const Module.Decl) u64 { assert(self.llvm_object == null); return self.text_section_virtual_address + decl.link.coff.text_offset; } pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void { _ = self; _ = module; _ = decl; // TODO Implement this } pub fn deinit(self: *Coff) void { if (build_options.have_llvm) { if (self.llvm_object) |llvm_object| llvm_object.destroy(self.base.allocator); } self.text_block_free_list.deinit(self.base.allocator); self.offset_table.deinit(self.base.allocator); self.offset_table_free_list.deinit(self.base.allocator); }
//! This is the userland implementation of translate-c which is used by both stage1 //! and stage2. const std = @import("std"); const assert = std.debug.assert; const clang = @import("clang.zig"); const ctok = std.c.tokenizer; const CToken = std.c.Token; const mem = std.mem; const math = std.math; const meta = std.meta; const ast = @import("translate_c/ast.zig"); const Node = ast.Node; const Tag = Node.Tag; const c_builtins = std.c.builtins; const CallingConvention = std.builtin.CallingConvention; pub const ClangErrMsg = clang.Stage2ErrorMsg; pub const Error = std.mem.Allocator.Error; const TypeError = Error || error{UnsupportedType}; const TransError = TypeError || error{UnsupportedTranslation}; const SymbolTable = std.StringArrayHashMap(Node); const AliasList = std.ArrayList(struct { alias: []const u8, name: []const u8, }); const Scope = struct { id: Id, parent: ?*Scope, const Id = enum { block, root, condition, loop, do_loop, }; /// Used for the scope of condition expressions, for example `if (cond)`. /// The block is lazily initialised because it is only needed for rare /// cases of comma operators being used. const Condition = struct { base: Scope, block: ?Block = null, fn getBlockScope(self: *Condition, c: *Context) !*Block { if (self.block) |*b| return b; self.block = try Block.init(c, &self.base, true); return &self.block.?; } fn deinit(self: *Condition) void { if (self.block) |*b| b.deinit(); } }; /// Represents an in-progress Node.Block. This struct is stack-allocated. /// When it is deinitialized, it produces an Node.Block which is allocated /// into the main arena. const Block = struct { base: Scope, statements: std.ArrayList(Node), variables: AliasList, mangle_count: u32 = 0, label: ?[]const u8 = null, /// When the block corresponds to a function, keep track of the return type /// so that the return expression can be cast, if necessary return_type: ?clang.QualType = null, /// C static local variables are wrapped in a block-local struct. The struct /// is named after the (mangled) variable name, the Zig variable within the /// struct itself is given this name. const StaticInnerName = "static"; fn init(c: *Context, parent: *Scope, labeled: bool) !Block { var blk = Block{ .base = .{ .id = .block, .parent = parent, }, .statements = std.ArrayList(Node).init(c.gpa), .variables = AliasList.init(c.gpa), }; if (labeled) { blk.label = try blk.makeMangledName(c, "blk"); } return blk; } fn deinit(self: *Block) void { self.statements.deinit(); self.variables.deinit(); self.* = undefined; } fn complete(self: *Block, c: *Context) !Node { if (self.base.parent.?.id == .do_loop) { // We reserve 1 extra statement if the parent is a do_loop. This is in case of // do while, we want to put `if (cond) break;` at the end. const alloc_len = self.statements.items.len + @boolToInt(self.base.parent.?.id == .do_loop); var stmts = try c.arena.alloc(Node, alloc_len); stmts.len = self.statements.items.len; mem.copy(Node, stmts, self.statements.items); return Tag.block.create(c.arena, .{ .label = self.label, .stmts = stmts, }); } if (self.statements.items.len == 0) return Tag.empty_block.init(); return Tag.block.create(c.arena, .{ .label = self.label, .stmts = try c.arena.dupe(Node, self.statements.items), }); } /// Given the desired name, return a name that does not shadow anything from outer scopes. /// Inserts the returned name into the scope. fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { const name_copy = try c.arena.dupe(u8, name); var proposed_name = name_copy; while (scope.contains(proposed_name)) { scope.mangle_count += 1; proposed_name = try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ name, scope.mangle_count }); } try scope.variables.append(.{ .name = name_copy, .alias = proposed_name }); return proposed_name; } fn getAlias(scope: *Block, name: []const u8) []const u8 { for (scope.variables.items) |p| { if (mem.eql(u8, p.name, name)) return p.alias; } return scope.base.parent.?.getAlias(name); } fn localContains(scope: *Block, name: []const u8) bool { for (scope.variables.items) |p| { if (mem.eql(u8, p.alias, name)) return true; } return false; } fn contains(scope: *Block, name: []const u8) bool { if (scope.localContains(name)) return true; return scope.base.parent.?.contains(name); } }; const Root = struct { base: Scope, sym_table: SymbolTable, macro_table: SymbolTable, context: *Context, nodes: std.ArrayList(Node), fn init(c: *Context) Root { return .{ .base = .{ .id = .root, .parent = null, }, .sym_table = SymbolTable.init(c.gpa), .macro_table = SymbolTable.init(c.gpa), .context = c, .nodes = std.ArrayList(Node).init(c.gpa), }; } fn deinit(scope: *Root) void { scope.sym_table.deinit(); scope.macro_table.deinit(); scope.nodes.deinit(); } /// Check if the global scope contains this name, without looking into the "future", e.g. /// ignore the preprocessed decl and macro names. fn containsNow(scope: *Root, name: []const u8) bool { return isZigPrimitiveType(name) or scope.sym_table.contains(name) or scope.macro_table.contains(name); } /// Check if the global scope contains the name, includes all decls that haven't been translated yet. fn contains(scope: *Root, name: []const u8) bool { return scope.containsNow(name) or scope.context.global_names.contains(name); } }; fn findBlockScope(inner: *Scope, c: *Context) !*Scope.Block { var scope = inner; while (true) { switch (scope.id) { .root => unreachable, .block => return @fieldParentPtr(Block, "base", scope), .condition => return @fieldParentPtr(Condition, "base", scope).getBlockScope(c), else => scope = scope.parent.?, } } } fn findBlockReturnType(inner: *Scope, c: *Context) clang.QualType { var scope = inner; while (true) { switch (scope.id) { .root => unreachable, .block => { const block = @fieldParentPtr(Block, "base", scope); if (block.return_type) |qt| return qt; scope = scope.parent.?; }, else => scope = scope.parent.?, } } } fn getAlias(scope: *Scope, name: []const u8) []const u8 { return switch (scope.id) { .root => return name, .block => @fieldParentPtr(Block, "base", scope).getAlias(name), .loop, .do_loop, .condition => scope.parent.?.getAlias(name), }; } fn contains(scope: *Scope, name: []const u8) bool { return switch (scope.id) { .root => @fieldParentPtr(Root, "base", scope).contains(name), .block => @fieldParentPtr(Block, "base", scope).contains(name), .loop, .do_loop, .condition => scope.parent.?.contains(name), }; } fn getBreakableScope(inner: *Scope) *Scope { var scope = inner; while (true) { switch (scope.id) { .root => unreachable, .loop, .do_loop => return scope, else => scope = scope.parent.?, } } } /// Appends a node to the first block scope if inside a function, or to the root tree if not. fn appendNode(inner: *Scope, node: Node) !void { var scope = inner; while (true) { switch (scope.id) { .root => { const root = @fieldParentPtr(Root, "base", scope); return root.nodes.append(node); }, .block => { const block = @fieldParentPtr(Block, "base", scope); return block.statements.append(node); }, else => scope = scope.parent.?, } } } }; pub const Context = struct { gpa: *mem.Allocator, arena: *mem.Allocator, source_manager: *clang.SourceManager, decl_table: std.AutoArrayHashMapUnmanaged(usize, []const u8) = .{}, alias_list: AliasList, global_scope: *Scope.Root, clang_context: *clang.ASTContext, mangle_count: u32 = 0, /// Table of record decls that have been demoted to opaques. opaque_demotes: std.AutoHashMapUnmanaged(usize, void) = .{}, /// Table of unnamed enums and records that are child types of typedefs. unnamed_typedefs: std.AutoHashMapUnmanaged(usize, []const u8) = .{}, /// This one is different than the root scope's name table. This contains /// a list of names that we found by visiting all the top level decls without /// translating them. The other maps are updated as we translate; this one is updated /// up front in a pre-processing step. global_names: std.StringArrayHashMapUnmanaged(void) = .{}, fn getMangle(c: *Context) u32 { c.mangle_count += 1; return c.mangle_count; } /// Convert a null-terminated C string to a slice allocated in the arena fn str(c: *Context, s: [*:0]const u8) ![]u8 { return mem.dupe(c.arena, u8, mem.spanZ(s)); } /// Convert a clang source location to a file:line:column string fn locStr(c: *Context, loc: clang.SourceLocation) ![]u8 { const spelling_loc = c.source_manager.getSpellingLoc(loc); const filename_c = c.source_manager.getFilename(spelling_loc); const filename = if (filename_c) |s| try c.str(s) else @as([]const u8, "(no file)"); const line = c.source_manager.getSpellingLineNumber(spelling_loc); const column = c.source_manager.getSpellingColumnNumber(spelling_loc); return std.fmt.allocPrint(c.arena, "{s}:{d}:{d}", .{ filename, line, column }); } }; pub fn translate( gpa: *mem.Allocator, args_begin: [*]?[*]const u8, args_end: [*]?[*]const u8, errors: *[]ClangErrMsg, resources_path: [*:0]const u8, ) !std.zig.ast.Tree { const ast_unit = clang.LoadFromCommandLine( args_begin, args_end, &errors.ptr, &errors.len, resources_path, ) orelse { if (errors.len == 0) return error.ASTUnitFailure; return error.SemanticAnalyzeFail; }; defer ast_unit.delete(); // For memory that has the same lifetime as the Tree that we return // from this function. var arena = std.heap.ArenaAllocator.init(gpa); errdefer arena.deinit(); var context = Context{ .gpa = gpa, .arena = &arena.allocator, .source_manager = ast_unit.getSourceManager(), .alias_list = AliasList.init(gpa), .global_scope = try arena.allocator.create(Scope.Root), .clang_context = ast_unit.getASTContext(), }; context.global_scope.* = Scope.Root.init(&context); defer { context.decl_table.deinit(gpa); context.alias_list.deinit(); context.global_names.deinit(gpa); context.opaque_demotes.deinit(gpa); context.unnamed_typedefs.deinit(gpa); context.global_scope.deinit(); } try context.global_scope.nodes.append(Tag.usingnamespace_builtins.init()); try prepopulateGlobalNameTable(ast_unit, &context); if (!ast_unit.visitLocalTopLevelDecls(&context, declVisitorC)) { return error.OutOfMemory; } try transPreprocessorEntities(&context, ast_unit); try addMacros(&context); for (context.alias_list.items) |alias| { if (!context.global_scope.sym_table.contains(alias.alias)) { const node = try Tag.alias.create(context.arena, .{ .actual = alias.alias, .mangled = alias.name }); try addTopLevelDecl(&context, alias.alias, node); } } return ast.render(gpa, context.global_scope.nodes.items); } fn prepopulateGlobalNameTable(ast_unit: *clang.ASTUnit, c: *Context) !void { if (!ast_unit.visitLocalTopLevelDecls(c, declVisitorNamesOnlyC)) { return error.OutOfMemory; } // TODO if we see #undef, delete it from the table var it = ast_unit.getLocalPreprocessingEntities_begin(); const it_end = ast_unit.getLocalPreprocessingEntities_end(); while (it.I != it_end.I) : (it.I += 1) { const entity = it.deref(); switch (entity.getKind()) { .MacroDefinitionKind => { const macro = @ptrCast(*clang.MacroDefinitionRecord, entity); const raw_name = macro.getName_getNameStart(); const name = try c.str(raw_name); try c.global_names.put(c.gpa, name, {}); }, else => {}, } } } fn declVisitorNamesOnlyC(context: ?*c_void, decl: *const clang.Decl) callconv(.C) bool { const c = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); declVisitorNamesOnly(c, decl) catch return false; return true; } fn declVisitorC(context: ?*c_void, decl: *const clang.Decl) callconv(.C) bool { const c = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); declVisitor(c, decl) catch return false; return true; } fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void { if (decl.castToNamedDecl()) |named_decl| { const decl_name = try c.str(named_decl.getName_bytes_begin()); try c.global_names.put(c.gpa, decl_name, {}); // Check for typedefs with unnamed enum/record child types. if (decl.getKind() == .Typedef) { const typedef_decl = @ptrCast(*const clang.TypedefNameDecl, decl); var child_ty = typedef_decl.getUnderlyingType().getTypePtr(); const addr: usize = while (true) switch (child_ty.getTypeClass()) { .Enum => { const enum_ty = @ptrCast(*const clang.EnumType, child_ty); const enum_decl = enum_ty.getDecl(); // check if this decl is unnamed if (@ptrCast(*const clang.NamedDecl, enum_decl).getName_bytes_begin()[0] != 0) return; break @ptrToInt(enum_decl.getCanonicalDecl()); }, .Record => { const record_ty = @ptrCast(*const clang.RecordType, child_ty); const record_decl = record_ty.getDecl(); // check if this decl is unnamed if (@ptrCast(*const clang.NamedDecl, record_decl).getName_bytes_begin()[0] != 0) return; break @ptrToInt(record_decl.getCanonicalDecl()); }, .Elaborated => { const elaborated_ty = @ptrCast(*const clang.ElaboratedType, child_ty); child_ty = elaborated_ty.getNamedType().getTypePtr(); }, .Decayed => { const decayed_ty = @ptrCast(*const clang.DecayedType, child_ty); child_ty = decayed_ty.getDecayedType().getTypePtr(); }, .Attributed => { const attributed_ty = @ptrCast(*const clang.AttributedType, child_ty); child_ty = attributed_ty.getEquivalentType().getTypePtr(); }, .MacroQualified => { const macroqualified_ty = @ptrCast(*const clang.MacroQualifiedType, child_ty); child_ty = macroqualified_ty.getModifiedType().getTypePtr(); }, else => return, } else unreachable; // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 const name = if (isZigPrimitiveType(decl_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ decl_name, c.getMangle() }) else decl_name; const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr); if (result.found_existing) { // One typedef can declare multiple names. // Don't put this one in `decl_table` so it's processed later. return; } result.value_ptr.* = name; // Put this typedef in the decl_table to avoid redefinitions. try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), name); } } } fn declVisitor(c: *Context, decl: *const clang.Decl) Error!void { switch (decl.getKind()) { .Function => { return visitFnDecl(c, @ptrCast(*const clang.FunctionDecl, decl)); }, .Typedef => { try transTypeDef(c, &c.global_scope.base, @ptrCast(*const clang.TypedefNameDecl, decl)); }, .Enum => { try transEnumDecl(c, &c.global_scope.base, @ptrCast(*const clang.EnumDecl, decl)); }, .Record => { try transRecordDecl(c, &c.global_scope.base, @ptrCast(*const clang.RecordDecl, decl)); }, .Var => { return visitVarDecl(c, @ptrCast(*const clang.VarDecl, decl), null); }, .Empty => { // Do nothing }, .FileScopeAsm => { try transFileScopeAsm(c, &c.global_scope.base, @ptrCast(*const clang.FileScopeAsmDecl, decl)); }, else => { const decl_name = try c.str(decl.getDeclKindName()); try warn(c, &c.global_scope.base, decl.getLocation(), "ignoring {s} declaration", .{decl_name}); }, } } fn transFileScopeAsm(c: *Context, scope: *Scope, file_scope_asm: *const clang.FileScopeAsmDecl) Error!void { const asm_string = file_scope_asm.getAsmString(); var len: usize = undefined; const bytes_ptr = asm_string.getString_bytes_begin_size(&len); const str = try std.fmt.allocPrint(c.arena, "\"{}\"", .{std.zig.fmtEscapes(bytes_ptr[0..len])}); const str_node = try Tag.string_literal.create(c.arena, str); const asm_node = try Tag.asm_simple.create(c.arena, str_node); const block = try Tag.block_single.create(c.arena, asm_node); const comptime_node = try Tag.@"comptime".create(c.arena, block); try scope.appendNode(comptime_node); } fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void { const fn_name = try c.str(@ptrCast(*const clang.NamedDecl, fn_decl).getName_bytes_begin()); if (c.global_scope.sym_table.contains(fn_name)) return; // Avoid processing this decl twice // Skip this declaration if a proper definition exists if (!fn_decl.isThisDeclarationADefinition()) { if (fn_decl.getDefinition()) |def| return visitFnDecl(c, def); } const fn_decl_loc = fn_decl.getLocation(); const has_body = fn_decl.hasBody(); const storage_class = fn_decl.getStorageClass(); var decl_ctx = FnDeclContext{ .fn_name = fn_name, .has_body = has_body, .storage_class = storage_class, .is_export = switch (storage_class) { .None => has_body and !fn_decl.isInlineSpecified(), .Extern, .Static => false, .PrivateExtern => return failDecl(c, fn_decl_loc, fn_name, "unsupported storage class: private extern", .{}), .Auto => unreachable, // Not legal on functions .Register => unreachable, // Not legal on functions }, }; var fn_qt = fn_decl.getType(); const fn_type = while (true) { const fn_type = fn_qt.getTypePtr(); switch (fn_type.getTypeClass()) { .Attributed => { const attr_type = @ptrCast(*const clang.AttributedType, fn_type); fn_qt = attr_type.getEquivalentType(); }, .Paren => { const paren_type = @ptrCast(*const clang.ParenType, fn_type); fn_qt = paren_type.getInnerType(); }, else => break fn_type, } } else unreachable; const fn_ty = @ptrCast(*const clang.FunctionType, fn_type); const return_qt = fn_ty.getReturnType(); const proto_node = switch (fn_type.getTypeClass()) { .FunctionProto => blk: { const fn_proto_type = @ptrCast(*const clang.FunctionProtoType, fn_type); if (has_body and fn_proto_type.isVariadic()) { decl_ctx.has_body = false; decl_ctx.storage_class = .Extern; decl_ctx.is_export = false; try warn(c, &c.global_scope.base, fn_decl_loc, "TODO unable to translate variadic function, demoted to extern", .{}); } break :blk transFnProto(c, fn_decl, fn_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{}); }, error.OutOfMemory => |e| return e, }; }, .FunctionNoProto => blk: { const fn_no_proto_type = @ptrCast(*const clang.FunctionType, fn_type); break :blk transFnNoProto(c, fn_no_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{}); }, error.OutOfMemory => |e| return e, }; }, else => return failDecl(c, fn_decl_loc, fn_name, "unable to resolve function type {}", .{fn_type.getTypeClass()}), }; if (!decl_ctx.has_body) { return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base)); } // actual function definition with body const body_stmt = fn_decl.getBody(); var block_scope = try Scope.Block.init(c, &c.global_scope.base, false); block_scope.return_type = return_qt; defer block_scope.deinit(); var scope = &block_scope.base; var param_id: c_uint = 0; for (proto_node.data.params) |*param, i| { const param_name = param.name orelse { proto_node.data.is_extern = true; proto_node.data.is_export = false; try warn(c, &c.global_scope.base, fn_decl_loc, "function {s} parameter has no name, demoted to extern", .{fn_name}); return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base)); }; const c_param = fn_decl.getParamDecl(param_id); const qual_type = c_param.getOriginalType(); const is_const = qual_type.isConstQualified(); const mangled_param_name = try block_scope.makeMangledName(c, param_name); param.name = mangled_param_name; if (!is_const) { const bare_arg_name = try std.fmt.allocPrint(c.arena, "arg_{s}", .{mangled_param_name}); const arg_name = try block_scope.makeMangledName(c, bare_arg_name); param.name = arg_name; const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name }); try block_scope.statements.append(redecl_node); } param_id += 1; } const casted_body = @ptrCast(*const clang.CompoundStmt, body_stmt); transCompoundStmtInline(c, casted_body, &block_scope) catch |err| switch (err) { error.OutOfMemory => |e| return e, error.UnsupportedTranslation, error.UnsupportedType, => { proto_node.data.is_extern = true; proto_node.data.is_export = false; try warn(c, &c.global_scope.base, fn_decl_loc, "unable to translate function, demoted to extern", .{}); return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base)); }, }; // add return statement if the function didn't have one blk: { const maybe_body = try block_scope.complete(c); if (fn_ty.getNoReturnAttr() or isCVoid(return_qt) or maybe_body.isNoreturn(false)) { proto_node.data.body = maybe_body; break :blk; } const rhs = transZeroInitExpr(c, scope, fn_decl_loc, return_qt.getTypePtr()) catch |err| switch (err) { error.OutOfMemory => |e| return e, error.UnsupportedTranslation, error.UnsupportedType, => { proto_node.data.is_extern = true; proto_node.data.is_export = false; try warn(c, &c.global_scope.base, fn_decl_loc, "unable to create a return value for function, demoted to extern", .{}); return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base)); }, }; const ret = try Tag.@"return".create(c.arena, rhs); try block_scope.statements.append(ret); proto_node.data.body = try block_scope.complete(c); } return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base)); } fn transQualTypeMaybeInitialized(c: *Context, scope: *Scope, qt: clang.QualType, decl_init: ?*const clang.Expr, loc: clang.SourceLocation) TransError!Node { return if (decl_init) |init_expr| transQualTypeInitialized(c, scope, qt, init_expr, loc) else transQualType(c, scope, qt, loc); } /// if mangled_name is not null, this var decl was declared in a block scope. fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]const u8) Error!void { const var_name = mangled_name orelse try c.str(@ptrCast(*const clang.NamedDecl, var_decl).getName_bytes_begin()); if (c.global_scope.sym_table.contains(var_name)) return; // Avoid processing this decl twice const is_pub = mangled_name == null; const is_threadlocal = var_decl.getTLSKind() != .None; const scope = &c.global_scope.base; // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ var_name, c.getMangle() }) else var_name; const var_decl_loc = var_decl.getLocation(); const qual_type = var_decl.getTypeSourceInfo_getType(); const storage_class = var_decl.getStorageClass(); const is_const = qual_type.isConstQualified(); const has_init = var_decl.hasInit(); const decl_init = var_decl.getInit(); // In C extern variables with initializers behave like Zig exports. // extern int foo = 2; // does the same as: // extern int foo; // int foo = 2; var is_extern = storage_class == .Extern and !has_init; var is_export = !is_extern and storage_class != .Static; const type_node = transQualTypeMaybeInitialized(c, scope, qual_type, decl_init, var_decl_loc) catch |err| switch (err) { error.UnsupportedTranslation, error.UnsupportedType => { return failDecl(c, var_decl_loc, checked_name, "unable to resolve variable type", .{}); }, error.OutOfMemory => |e| return e, }; var init_node: ?Node = null; // If the initialization expression is not present, initialize with undefined. // If it is an integer literal, we can skip the @as since it will be redundant // with the variable type. if (has_init) trans_init: { if (decl_init) |expr| { const node_or_error = if (expr.getStmtClass() == .StringLiteralClass) transStringLiteralInitializer(c, scope, @ptrCast(*const clang.StringLiteral, expr), type_node) else transExprCoercing(c, scope, expr, .used); init_node = node_or_error catch |err| switch (err) { error.UnsupportedTranslation, error.UnsupportedType, => { is_extern = true; is_export = false; try warn(c, scope, var_decl_loc, "unable to translate variable initializer, demoted to extern", .{}); break :trans_init; }, error.OutOfMemory => |e| return e, }; if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node.?)) { init_node = try Tag.bool_to_int.create(c.arena, init_node.?); } } else { init_node = Tag.undefined_literal.init(); } } else if (storage_class != .Extern) { // The C language specification states that variables with static or threadlocal // storage without an initializer are initialized to a zero value. // @import("std").mem.zeroes(T) init_node = try Tag.std_mem_zeroes.create(c.arena, type_node); } const linksection_string = blk: { var str_len: usize = undefined; if (var_decl.getSectionAttribute(&str_len)) |str_ptr| { break :blk str_ptr[0..str_len]; } break :blk null; }; const node = try Tag.var_decl.create(c.arena, .{ .is_pub = is_pub, .is_const = is_const, .is_extern = is_extern, .is_export = is_export, .is_threadlocal = is_threadlocal, .linksection_string = linksection_string, .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)), .name = checked_name, .type = type_node, .init = init_node, }); return addTopLevelDecl(c, checked_name, node); } const builtin_typedef_map = std.ComptimeStringMap([]const u8, .{ .{ "uint8_t", "u8" }, .{ "int8_t", "i8" }, .{ "uint16_t", "u16" }, .{ "int16_t", "i16" }, .{ "uint32_t", "u32" }, .{ "int32_t", "i32" }, .{ "uint64_t", "u64" }, .{ "int64_t", "i64" }, .{ "intptr_t", "isize" }, .{ "uintptr_t", "usize" }, .{ "ssize_t", "isize" }, .{ "size_t", "usize" }, }); fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNameDecl) Error!void { if (c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl()))) |name| return; // Avoid processing this decl twice const toplevel = scope.id == .root; const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined; const bare_name = try c.str(@ptrCast(*const clang.NamedDecl, typedef_decl).getName_bytes_begin()); // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 var name: []const u8 = if (isZigPrimitiveType(bare_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ bare_name, c.getMangle() }) else bare_name; if (builtin_typedef_map.get(name)) |builtin| { return c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), builtin); } if (!toplevel) name = try bs.makeMangledName(c, name); try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), name); const child_qt = typedef_decl.getUnderlyingType(); const typedef_loc = typedef_decl.getLocation(); const init_node = transQualType(c, scope, child_qt, typedef_loc) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, typedef_loc, name, "unable to resolve typedef child type", .{}); }, error.OutOfMemory => |e| return e, }; const payload = try c.arena.create(ast.Payload.SimpleVarDecl); payload.* = .{ .base = .{ .tag = ([2]Tag{ .var_simple, .pub_var_simple })[@boolToInt(toplevel)] }, .data = .{ .name = name, .init = init_node, }, }; const node = Node.initPayload(&payload.base); if (toplevel) { try addTopLevelDecl(c, name, node); } else { try scope.appendNode(node); } } /// Build a getter function for a flexible array member at the end of a C struct /// e.g. `T items[]` or `T items[0]`. The generated function returns a [*c] pointer /// to the flexible array with the correct const and volatile qualifiers fn buildFlexibleArrayFn( c: *Context, scope: *Scope, layout: *const clang.ASTRecordLayout, field_name: []const u8, field_decl: *const clang.FieldDecl, ) TypeError!Node { const field_qt = field_decl.getType(); const u8_type = try Tag.type.create(c.arena, "u8"); const self_param_name = "self"; const self_param = try Tag.identifier.create(c.arena, self_param_name); const self_type = try Tag.typeof.create(c.arena, self_param); const fn_params = try c.arena.alloc(ast.Payload.Param, 1); fn_params[0] = .{ .name = self_param_name, .type = Tag.@"anytype".init(), .is_noalias = false, }; const array_type = @ptrCast(*const clang.ArrayType, field_qt.getTypePtr()); const element_qt = array_type.getElementType(); const element_type = try transQualType(c, scope, element_qt, field_decl.getLocation()); var block_scope = try Scope.Block.init(c, scope, false); defer block_scope.deinit(); const intermediate_type_name = try block_scope.makeMangledName(c, "Intermediate"); const intermediate_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type }); const intermediate_type_decl = try Tag.var_simple.create(c.arena, .{ .name = intermediate_type_name, .init = intermediate_type, }); try block_scope.statements.append(intermediate_type_decl); const intermediate_type_ident = try Tag.identifier.create(c.arena, intermediate_type_name); const return_type_name = try block_scope.makeMangledName(c, "ReturnType"); const return_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type }); const return_type_decl = try Tag.var_simple.create(c.arena, .{ .name = return_type_name, .init = return_type, }); try block_scope.statements.append(return_type_decl); const return_type_ident = try Tag.identifier.create(c.arena, return_type_name); const field_index = field_decl.getFieldIndex(); const bit_offset = layout.getFieldOffset(field_index); // this is a target-specific constant based on the struct layout const byte_offset = bit_offset / 8; const casted_self = try Tag.ptr_cast.create(c.arena, .{ .lhs = intermediate_type_ident, .rhs = self_param, }); const field_offset = try transCreateNodeNumber(c, byte_offset, .int); const field_ptr = try Tag.add.create(c.arena, .{ .lhs = casted_self, .rhs = field_offset }); const alignment = try Tag.alignof.create(c.arena, element_type); const ptr_val = try Tag.align_cast.create(c.arena, .{ .lhs = alignment, .rhs = field_ptr }); const ptr_cast = try Tag.ptr_cast.create(c.arena, .{ .lhs = return_type_ident, .rhs = ptr_val }); const return_stmt = try Tag.@"return".create(c.arena, ptr_cast); try block_scope.statements.append(return_stmt); const payload = try c.arena.create(ast.Payload.Func); payload.* = .{ .base = .{ .tag = .func }, .data = .{ .is_pub = true, .is_extern = false, .is_export = false, .is_var_args = false, .name = field_name, .linksection_string = null, .explicit_callconv = null, .params = fn_params, .return_type = return_type, .body = try block_scope.complete(c), .alignment = null, }, }; return Node.initPayload(&payload.base); } fn isFlexibleArrayFieldDecl(c: *Context, field_decl: *const clang.FieldDecl) bool { return qualTypeCanon(field_decl.getType()).isIncompleteOrZeroLengthArrayType(c.clang_context); } /// clang's RecordDecl::hasFlexibleArrayMember is not suitable for determining /// this because it returns false for a record that ends with a zero-length /// array, but we consider those to be flexible arrays fn hasFlexibleArrayField(c: *Context, record_def: *const clang.RecordDecl) bool { var it = record_def.field_begin(); const end_it = record_def.field_end(); while (it.neq(end_it)) : (it = it.next()) { const field_decl = it.deref(); if (isFlexibleArrayFieldDecl(c, field_decl)) return true; } return false; } fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordDecl) Error!void { if (c.decl_table.get(@ptrToInt(record_decl.getCanonicalDecl()))) |name| return; // Avoid processing this decl twice const record_loc = record_decl.getLocation(); const toplevel = scope.id == .root; const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined; var is_union = false; var container_kind_name: []const u8 = undefined; var bare_name: []const u8 = try c.str(@ptrCast(*const clang.NamedDecl, record_decl).getName_bytes_begin()); if (record_decl.isUnion()) { container_kind_name = "union"; is_union = true; } else if (record_decl.isStruct()) { container_kind_name = "struct"; } else { try c.decl_table.putNoClobber(c.gpa, @ptrToInt(record_decl.getCanonicalDecl()), bare_name); return failDecl(c, record_loc, bare_name, "record {s} is not a struct or union", .{bare_name}); } var is_unnamed = false; var name = bare_name; if (c.unnamed_typedefs.get(@ptrToInt(record_decl.getCanonicalDecl()))) |typedef_name| { bare_name = typedef_name; name = typedef_name; } else { // Record declarations such as `struct {...} x` have no name but they're not // anonymous hence here isAnonymousStructOrUnion is not needed if (bare_name.len == 0) { bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{d}", .{c.getMangle()}); is_unnamed = true; } name = try std.fmt.allocPrint(c.arena, "{s}_{s}", .{ container_kind_name, bare_name }); } if (!toplevel) name = try bs.makeMangledName(c, name); try c.decl_table.putNoClobber(c.gpa, @ptrToInt(record_decl.getCanonicalDecl()), name); const is_pub = toplevel and !is_unnamed; const init_node = blk: { const record_def = record_decl.getDefinition() orelse { try c.opaque_demotes.put(c.gpa, @ptrToInt(record_decl.getCanonicalDecl()), {}); break :blk Tag.opaque_literal.init(); }; const is_packed = record_decl.getPackedAttribute(); var fields = std.ArrayList(ast.Payload.Record.Field).init(c.gpa); defer fields.deinit(); var functions = std.ArrayList(Node).init(c.gpa); defer functions.deinit(); const has_flexible_array = hasFlexibleArrayField(c, record_def); var unnamed_field_count: u32 = 0; var it = record_def.field_begin(); const end_it = record_def.field_end(); const layout = record_def.getASTRecordLayout(c.clang_context); const record_alignment = layout.getAlignment(); while (it.neq(end_it)) : (it = it.next()) { const field_decl = it.deref(); const field_loc = field_decl.getLocation(); const field_qt = field_decl.getType(); if (field_decl.isBitField()) { try c.opaque_demotes.put(c.gpa, @ptrToInt(record_decl.getCanonicalDecl()), {}); try warn(c, scope, field_loc, "{s} demoted to opaque type - has bitfield", .{container_kind_name}); break :blk Tag.opaque_literal.init(); } var is_anon = false; var field_name = try c.str(@ptrCast(*const clang.NamedDecl, field_decl).getName_bytes_begin()); if (field_decl.isAnonymousStructOrUnion() or field_name.len == 0) { // Context.getMangle() is not used here because doing so causes unpredictable field names for anonymous fields. field_name = try std.fmt.allocPrint(c.arena, "unnamed_{d}", .{unnamed_field_count}); unnamed_field_count += 1; is_anon = true; } if (isFlexibleArrayFieldDecl(c, field_decl)) { const flexible_array_fn = buildFlexibleArrayFn(c, scope, layout, field_name, field_decl) catch |err| switch (err) { error.UnsupportedType => { try c.opaque_demotes.put(c.gpa, @ptrToInt(record_decl.getCanonicalDecl()), {}); try warn(c, scope, record_loc, "{s} demoted to opaque type - unable to translate type of flexible array field {s}", .{ container_kind_name, field_name }); break :blk Tag.opaque_literal.init(); }, else => |e| return e, }; try functions.append(flexible_array_fn); continue; } const field_type = transQualType(c, scope, field_qt, field_loc) catch |err| switch (err) { error.UnsupportedType => { try c.opaque_demotes.put(c.gpa, @ptrToInt(record_decl.getCanonicalDecl()), {}); try warn(c, scope, record_loc, "{s} demoted to opaque type - unable to translate type of field {s}", .{ container_kind_name, field_name }); break :blk Tag.opaque_literal.init(); }, else => |e| return e, }; const alignment = if (has_flexible_array and field_decl.getFieldIndex() == 0) @intCast(c_uint, record_alignment) else zigAlignment(field_decl.getAlignedAttribute(c.clang_context)); if (is_anon) { try c.decl_table.putNoClobber(c.gpa, @ptrToInt(field_decl.getCanonicalDecl()), field_name); } try fields.append(.{ .name = field_name, .type = field_type, .alignment = alignment, }); } const record_payload = try c.arena.create(ast.Payload.Record); record_payload.* = .{ .base = .{ .tag = ([2]Tag{ .@"struct", .@"union" })[@boolToInt(is_union)] }, .data = .{ .is_packed = is_packed, .fields = try c.arena.dupe(ast.Payload.Record.Field, fields.items), .functions = try c.arena.dupe(Node, functions.items), }, }; break :blk Node.initPayload(&record_payload.base); }; const payload = try c.arena.create(ast.Payload.SimpleVarDecl); payload.* = .{ .base = .{ .tag = ([2]Tag{ .var_simple, .pub_var_simple })[@boolToInt(is_pub)] }, .data = .{ .name = name, .init = init_node, }, }; if (toplevel) { try addTopLevelDecl(c, name, Node.initPayload(&payload.base)); if (!is_unnamed) try c.alias_list.append(.{ .alias = bare_name, .name = name }); } else { try scope.appendNode(Node.initPayload(&payload.base)); } } fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const clang.EnumDecl) Error!void { if (c.decl_table.get(@ptrToInt(enum_decl.getCanonicalDecl()))) |name| return; // Avoid processing this decl twice const enum_loc = enum_decl.getLocation(); const toplevel = scope.id == .root; const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined; var is_unnamed = false; var bare_name: []const u8 = try c.str(@ptrCast(*const clang.NamedDecl, enum_decl).getName_bytes_begin()); var name = bare_name; if (c.unnamed_typedefs.get(@ptrToInt(enum_decl.getCanonicalDecl()))) |typedef_name| { bare_name = typedef_name; name = typedef_name; } else { if (bare_name.len == 0) { bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{d}", .{c.getMangle()}); is_unnamed = true; } name = try std.fmt.allocPrint(c.arena, "enum_{s}", .{bare_name}); } if (!toplevel) _ = try bs.makeMangledName(c, name); try c.decl_table.putNoClobber(c.gpa, @ptrToInt(enum_decl.getCanonicalDecl()), name); const is_pub = toplevel and !is_unnamed; var redecls = std.ArrayList(Tag.enum_redecl.Data()).init(c.gpa); defer redecls.deinit(); const init_node = if (enum_decl.getDefinition()) |enum_def| blk: { var pure_enum = true; var it = enum_def.enumerator_begin(); var end_it = enum_def.enumerator_end(); while (it.neq(end_it)) : (it = it.next()) { const enum_const = it.deref(); if (enum_const.getInitExpr()) |_| { pure_enum = false; break; } } var fields = std.ArrayList(ast.Payload.Enum.Field).init(c.gpa); defer fields.deinit(); const int_type = enum_decl.getIntegerType(); // The underlying type may be null in case of forward-declared enum // types, while that's not ISO-C compliant many compilers allow this and // default to the usual integer type used for all the enums. // default to c_int since msvc and gcc default to different types const init_arg_expr = if (int_type.ptr != null) transQualType(c, scope, int_type, enum_loc) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, enum_loc, name, "unable to translate enum tag type", .{}); }, else => |e| return e, } else try Tag.type.create(c.arena, "c_int"); it = enum_def.enumerator_begin(); end_it = enum_def.enumerator_end(); while (it.neq(end_it)) : (it = it.next()) { const enum_const = it.deref(); const enum_val_name = try c.str(@ptrCast(*const clang.NamedDecl, enum_const).getName_bytes_begin()); const field_name = if (!is_unnamed and mem.startsWith(u8, enum_val_name, bare_name)) enum_val_name[bare_name.len..] else enum_val_name; const int_node = if (!pure_enum) try transCreateNodeAPInt(c, enum_const.getInitVal()) else null; try fields.append(.{ .name = field_name, .value = int_node, }); // In C each enum value is in the global namespace. So we put them there too. // At this point we can rely on the enum emitting successfully. try redecls.append(.{ .enum_val_name = enum_val_name, .field_name = field_name, .enum_name = name, }); } break :blk try Tag.@"enum".create(c.arena, .{ .int_type = init_arg_expr, .fields = try c.arena.dupe(ast.Payload.Enum.Field, fields.items), }); } else blk: { try c.opaque_demotes.put(c.gpa, @ptrToInt(enum_decl.getCanonicalDecl()), {}); break :blk Tag.opaque_literal.init(); }; const payload = try c.arena.create(ast.Payload.SimpleVarDecl); payload.* = .{ .base = .{ .tag = ([2]Tag{ .var_simple, .pub_var_simple })[@boolToInt(is_pub)] }, .data = .{ .name = name, .init = init_node, }, }; if (toplevel) { try addTopLevelDecl(c, name, Node.initPayload(&payload.base)); if (!is_unnamed) try c.alias_list.append(.{ .alias = bare_name, .name = name }); } else { try scope.appendNode(Node.initPayload(&payload.base)); } for (redecls.items) |redecl| { if (toplevel) { try addTopLevelDecl(c, redecl.field_name, try Tag.pub_enum_redecl.create(c.arena, redecl)); } else { try scope.appendNode(try Tag.enum_redecl.create(c.arena, .{ .enum_val_name = try bs.makeMangledName(c, redecl.enum_val_name), .field_name = redecl.field_name, .enum_name = redecl.enum_name, })); } } } const ResultUsed = enum { used, unused, }; fn transStmt( c: *Context, scope: *Scope, stmt: *const clang.Stmt, result_used: ResultUsed, ) TransError!Node { const sc = stmt.getStmtClass(); switch (sc) { .BinaryOperatorClass => return transBinaryOperator(c, scope, @ptrCast(*const clang.BinaryOperator, stmt), result_used), .CompoundStmtClass => return transCompoundStmt(c, scope, @ptrCast(*const clang.CompoundStmt, stmt)), .CStyleCastExprClass => return transCStyleCastExprClass(c, scope, @ptrCast(*const clang.CStyleCastExpr, stmt), result_used), .DeclStmtClass => return transDeclStmt(c, scope, @ptrCast(*const clang.DeclStmt, stmt)), .DeclRefExprClass => return transDeclRefExpr(c, scope, @ptrCast(*const clang.DeclRefExpr, stmt)), .ImplicitCastExprClass => return transImplicitCastExpr(c, scope, @ptrCast(*const clang.ImplicitCastExpr, stmt), result_used), .IntegerLiteralClass => return transIntegerLiteral(c, scope, @ptrCast(*const clang.IntegerLiteral, stmt), result_used, .with_as), .ReturnStmtClass => return transReturnStmt(c, scope, @ptrCast(*const clang.ReturnStmt, stmt)), .StringLiteralClass => return transStringLiteral(c, scope, @ptrCast(*const clang.StringLiteral, stmt), result_used), .ParenExprClass => { const expr = try transExpr(c, scope, @ptrCast(*const clang.ParenExpr, stmt).getSubExpr(), .used); return maybeSuppressResult(c, scope, result_used, expr); }, .InitListExprClass => return transInitListExpr(c, scope, @ptrCast(*const clang.InitListExpr, stmt), result_used), .ImplicitValueInitExprClass => return transImplicitValueInitExpr(c, scope, @ptrCast(*const clang.Expr, stmt), result_used), .IfStmtClass => return transIfStmt(c, scope, @ptrCast(*const clang.IfStmt, stmt)), .WhileStmtClass => return transWhileLoop(c, scope, @ptrCast(*const clang.WhileStmt, stmt)), .DoStmtClass => return transDoWhileLoop(c, scope, @ptrCast(*const clang.DoStmt, stmt)), .NullStmtClass => { return Tag.empty_block.init(); }, .ContinueStmtClass => return Tag.@"continue".init(), .BreakStmtClass => return Tag.@"break".init(), .ForStmtClass => return transForLoop(c, scope, @ptrCast(*const clang.ForStmt, stmt)), .FloatingLiteralClass => return transFloatingLiteral(c, scope, @ptrCast(*const clang.FloatingLiteral, stmt), result_used), .ConditionalOperatorClass => { return transConditionalOperator(c, scope, @ptrCast(*const clang.ConditionalOperator, stmt), result_used); }, .BinaryConditionalOperatorClass => { return transBinaryConditionalOperator(c, scope, @ptrCast(*const clang.BinaryConditionalOperator, stmt), result_used); }, .SwitchStmtClass => return transSwitch(c, scope, @ptrCast(*const clang.SwitchStmt, stmt)), .CaseStmtClass, .DefaultStmtClass => { return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO complex switch", .{}); }, .ConstantExprClass => return transConstantExpr(c, scope, @ptrCast(*const clang.Expr, stmt), result_used), .PredefinedExprClass => return transPredefinedExpr(c, scope, @ptrCast(*const clang.PredefinedExpr, stmt), result_used), .CharacterLiteralClass => return transCharLiteral(c, scope, @ptrCast(*const clang.CharacterLiteral, stmt), result_used, .with_as), .StmtExprClass => return transStmtExpr(c, scope, @ptrCast(*const clang.StmtExpr, stmt), result_used), .MemberExprClass => return transMemberExpr(c, scope, @ptrCast(*const clang.MemberExpr, stmt), result_used), .ArraySubscriptExprClass => return transArrayAccess(c, scope, @ptrCast(*const clang.ArraySubscriptExpr, stmt), result_used), .CallExprClass => return transCallExpr(c, scope, @ptrCast(*const clang.CallExpr, stmt), result_used), .UnaryExprOrTypeTraitExprClass => return transUnaryExprOrTypeTraitExpr(c, scope, @ptrCast(*const clang.UnaryExprOrTypeTraitExpr, stmt), result_used), .UnaryOperatorClass => return transUnaryOperator(c, scope, @ptrCast(*const clang.UnaryOperator, stmt), result_used), .CompoundAssignOperatorClass => return transCompoundAssignOperator(c, scope, @ptrCast(*const clang.CompoundAssignOperator, stmt), result_used), .OpaqueValueExprClass => { const source_expr = @ptrCast(*const clang.OpaqueValueExpr, stmt).getSourceExpr().?; const expr = try transExpr(c, scope, source_expr, .used); return maybeSuppressResult(c, scope, result_used, expr); }, .OffsetOfExprClass => return transOffsetOfExpr(c, scope, @ptrCast(*const clang.OffsetOfExpr, stmt), result_used), .CompoundLiteralExprClass => { const compound_literal = @ptrCast(*const clang.CompoundLiteralExpr, stmt); return transExpr(c, scope, compound_literal.getInitializer(), result_used); }, .GenericSelectionExprClass => { const gen_sel = @ptrCast(*const clang.GenericSelectionExpr, stmt); return transExpr(c, scope, gen_sel.getResultExpr(), result_used); }, .ConvertVectorExprClass => { const conv_vec = @ptrCast(*const clang.ConvertVectorExpr, stmt); const conv_vec_node = try transConvertVectorExpr(c, scope, stmt.getBeginLoc(), conv_vec); return maybeSuppressResult(c, scope, result_used, conv_vec_node); }, .ShuffleVectorExprClass => { const shuffle_vec_expr = @ptrCast(*const clang.ShuffleVectorExpr, stmt); const shuffle_vec_node = try transShuffleVectorExpr(c, scope, shuffle_vec_expr); return maybeSuppressResult(c, scope, result_used, shuffle_vec_node); }, // When adding new cases here, see comment for maybeBlockify() else => { return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)}); }, } } /// See https://clang.llvm.org/docs/LanguageExtensions.html#langext-builtin-convertvector fn transConvertVectorExpr( c: *Context, scope: *Scope, source_loc: clang.SourceLocation, expr: *const clang.ConvertVectorExpr, ) TransError!Node { const base_stmt = @ptrCast(*const clang.Stmt, expr); var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const src_expr = expr.getSrcExpr(); const src_type = qualTypeCanon(src_expr.getType()); const src_vector_ty = @ptrCast(*const clang.VectorType, src_type); const src_element_qt = src_vector_ty.getElementType(); const src_element_type_node = try transQualType(c, &block_scope.base, src_element_qt, base_stmt.getBeginLoc()); const src_expr_node = try transExpr(c, &block_scope.base, src_expr, .used); const dst_qt = expr.getTypeSourceInfo_getType(); const dst_type_node = try transQualType(c, &block_scope.base, dst_qt, base_stmt.getBeginLoc()); const dst_vector_ty = @ptrCast(*const clang.VectorType, qualTypeCanon(dst_qt)); const num_elements = dst_vector_ty.getNumElements(); const dst_element_qt = dst_vector_ty.getElementType(); // workaround for https://github.com/ziglang/zig/issues/8322 // we store the casted results into temp variables and use those // to initialize the vector. Eventually we can just directly // construct the init_list from casted source members var i: usize = 0; while (i < num_elements) : (i += 1) { const mangled_name = try block_scope.makeMangledName(c, "tmp"); const value = try Tag.array_access.create(c.arena, .{ .lhs = src_expr_node, .rhs = try transCreateNodeNumber(c, i, .int), }); const tmp_decl_node = try Tag.var_simple.create(c.arena, .{ .name = mangled_name, .init = try transCCast(c, &block_scope.base, base_stmt.getBeginLoc(), dst_element_qt, src_element_qt, value), }); try block_scope.statements.append(tmp_decl_node); } const init_list = try c.arena.alloc(Node, num_elements); for (init_list) |*init, init_index| { const tmp_decl = block_scope.statements.items[init_index]; const name = tmp_decl.castTag(.var_simple).?.data.name; init.* = try Tag.identifier.create(c.arena, name); } const vec_init = try Tag.array_init.create(c.arena, .{ .cond = dst_type_node, .cases = init_list, }); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = vec_init, }); try block_scope.statements.append(break_node); return block_scope.complete(c); } fn makeShuffleMask(c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorExpr, vector_len: Node) TransError!Node { const num_subexprs = expr.getNumSubExprs(); assert(num_subexprs >= 3); // two source vectors + at least 1 index expression const mask_len = num_subexprs - 2; const mask_type = try Tag.std_meta_vector.create(c.arena, .{ .lhs = try transCreateNodeNumber(c, mask_len, .int), .rhs = try Tag.type.create(c.arena, "i32"), }); const init_list = try c.arena.alloc(Node, mask_len); for (init_list) |*init, i| { const index_expr = try transExprCoercing(c, scope, expr.getExpr(@intCast(c_uint, i + 2)), .used); const converted_index = try Tag.std_meta_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len }); init.* = converted_index; } const mask_init = try Tag.array_init.create(c.arena, .{ .cond = mask_type, .cases = init_list, }); return Tag.@"comptime".create(c.arena, mask_init); } /// @typeInfo(@TypeOf(vec_node)).Vector.<field> fn vectorTypeInfo(arena: *mem.Allocator, vec_node: Node, field: []const u8) TransError!Node { const typeof_call = try Tag.typeof.create(arena, vec_node); const typeinfo_call = try Tag.typeinfo.create(arena, typeof_call); const vector_type_info = try Tag.field_access.create(arena, .{ .lhs = typeinfo_call, .field_name = "Vector" }); return Tag.field_access.create(arena, .{ .lhs = vector_type_info, .field_name = field }); } fn transShuffleVectorExpr( c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorExpr, ) TransError!Node { const base_expr = @ptrCast(*const clang.Expr, expr); const num_subexprs = expr.getNumSubExprs(); if (num_subexprs < 3) return fail(c, error.UnsupportedTranslation, base_expr.getBeginLoc(), "ShuffleVector needs at least 1 index", .{}); const a = try transExpr(c, scope, expr.getExpr(0), .used); const b = try transExpr(c, scope, expr.getExpr(1), .used); // clang requires first two arguments to __builtin_shufflevector to be same type const vector_child_type = try vectorTypeInfo(c.arena, a, "child"); const vector_len = try vectorTypeInfo(c.arena, a, "len"); const shuffle_mask = try makeShuffleMask(c, scope, expr, vector_len); return Tag.shuffle.create(c.arena, .{ .element_type = vector_child_type, .a = a, .b = b, .mask_vector = shuffle_mask, }); } /// Translate a "simple" offsetof expression containing exactly one component, /// when that component is of kind .Field - e.g. offsetof(mytype, myfield) fn transSimpleOffsetOfExpr( c: *Context, scope: *Scope, expr: *const clang.OffsetOfExpr, ) TransError!Node { assert(expr.getNumComponents() == 1); const component = expr.getComponent(0); if (component.getKind() == .Field) { const field_decl = component.getField(); if (field_decl.getParent()) |record_decl| { if (c.decl_table.get(@ptrToInt(record_decl.getCanonicalDecl()))) |type_name| { const type_node = try Tag.type.create(c.arena, type_name); var raw_field_name = try c.str(@ptrCast(*const clang.NamedDecl, field_decl).getName_bytes_begin()); const quoted_field_name = try std.fmt.allocPrint(c.arena, "\"{s}\"", .{raw_field_name}); const field_name_node = try Tag.string_literal.create(c.arena, quoted_field_name); return Tag.offset_of.create(c.arena, .{ .lhs = type_node, .rhs = field_name_node, }); } } } return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "Failed to translate simple OffsetOfExpr", .{}); } fn transOffsetOfExpr( c: *Context, scope: *Scope, expr: *const clang.OffsetOfExpr, result_used: ResultUsed, ) TransError!Node { if (expr.getNumComponents() == 1) { const offsetof_expr = try transSimpleOffsetOfExpr(c, scope, expr); return maybeSuppressResult(c, scope, result_used, offsetof_expr); } // TODO implement OffsetOfExpr with more than 1 component // OffsetOfExpr API: // call expr.getComponent(idx) while idx < expr.getNumComponents() // component.getKind() will be either .Array or .Field (other kinds are C++-only) // if .Field, use component.getField() to retrieve *clang.FieldDecl // if .Array, use component.getArrayExprIndex() to get a c_uint which // can be passed to expr.getIndexExpr(expr_index) to get the *clang.Expr for the array index return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "TODO: implement complex OffsetOfExpr translation", .{}); } /// Cast a signed integer node to a usize, for use in pointer arithmetic. Negative numbers /// will become very large positive numbers but that is ok since we only use this in /// pointer arithmetic expressions, where wraparound will ensure we get the correct value. /// node -> @bitCast(usize, @intCast(isize, node)) fn usizeCastForWrappingPtrArithmetic(gpa: *mem.Allocator, node: Node) TransError!Node { const intcast_node = try Tag.int_cast.create(gpa, .{ .lhs = try Tag.identifier.create(gpa, "isize"), .rhs = node, }); return Tag.bit_cast.create(gpa, .{ .lhs = try Tag.identifier.create(gpa, "usize"), .rhs = intcast_node, }); } /// Translate an arithmetic expression with a pointer operand and a signed-integer operand. /// Zig requires a usize argument for pointer arithmetic, so we intCast to isize and then /// bitcast to usize; pointer wraparound make the math work. /// Zig pointer addition is not commutative (unlike C); the pointer operand needs to be on the left. /// The + operator in C is not a sequence point so it should be safe to switch the order if necessary. fn transCreatePointerArithmeticSignedOp( c: *Context, scope: *Scope, stmt: *const clang.BinaryOperator, result_used: ResultUsed, ) TransError!Node { const is_add = stmt.getOpcode() == .Add; const lhs = stmt.getLHS(); const rhs = stmt.getRHS(); const swap_operands = is_add and cIsSignedInteger(getExprQualType(c, lhs)); const swizzled_lhs = if (swap_operands) rhs else lhs; const swizzled_rhs = if (swap_operands) lhs else rhs; const lhs_node = try transExpr(c, scope, swizzled_lhs, .used); const rhs_node = try transExpr(c, scope, swizzled_rhs, .used); const bitcast_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node); return transCreateNodeInfixOp( c, scope, if (is_add) .add else .sub, lhs_node, bitcast_node, result_used, ); } fn transBinaryOperator( c: *Context, scope: *Scope, stmt: *const clang.BinaryOperator, result_used: ResultUsed, ) TransError!Node { const op = stmt.getOpcode(); const qt = stmt.getType(); const isPointerDiffExpr = cIsPointerDiffExpr(c, stmt); switch (op) { .Assign => return try transCreateNodeAssign(c, scope, result_used, stmt.getLHS(), stmt.getRHS()), .Comma => { var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const lhs = try transExpr(c, &block_scope.base, stmt.getLHS(), .unused); try block_scope.statements.append(lhs); const rhs = try transExpr(c, &block_scope.base, stmt.getRHS(), .used); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = rhs, }); try block_scope.statements.append(break_node); const block_node = try block_scope.complete(c); return maybeSuppressResult(c, scope, result_used, block_node); }, .Div => { if (cIsSignedInteger(qt)) { // signed integer division uses @divTrunc const lhs = try transExpr(c, scope, stmt.getLHS(), .used); const rhs = try transExpr(c, scope, stmt.getRHS(), .used); const div_trunc = try Tag.div_trunc.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); return maybeSuppressResult(c, scope, result_used, div_trunc); } }, .Rem => { if (cIsSignedInteger(qt)) { // signed integer division uses @rem const lhs = try transExpr(c, scope, stmt.getLHS(), .used); const rhs = try transExpr(c, scope, stmt.getRHS(), .used); const rem = try Tag.rem.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); return maybeSuppressResult(c, scope, result_used, rem); } }, .Shl => { return transCreateNodeShiftOp(c, scope, stmt, .shl, result_used); }, .Shr => { return transCreateNodeShiftOp(c, scope, stmt, .shr, result_used); }, .LAnd => { return transCreateNodeBoolInfixOp(c, scope, stmt, .@"and", result_used); }, .LOr => { return transCreateNodeBoolInfixOp(c, scope, stmt, .@"or", result_used); }, .Add, .Sub => { // `ptr + idx` and `idx + ptr` -> ptr + @bitCast(usize, @intCast(isize, idx)) // `ptr - idx` -> ptr - @bitCast(usize, @intCast(isize, idx)) if (qualTypeIsPtr(qt) and (cIsSignedInteger(getExprQualType(c, stmt.getLHS())) or cIsSignedInteger(getExprQualType(c, stmt.getRHS())))) return transCreatePointerArithmeticSignedOp(c, scope, stmt, result_used); }, else => {}, } var op_id: Tag = undefined; switch (op) { .Add => { if (cIsUnsignedInteger(qt)) { op_id = .add_wrap; } else { op_id = .add; } }, .Sub => { if (cIsUnsignedInteger(qt) or isPointerDiffExpr) { op_id = .sub_wrap; } else { op_id = .sub; } }, .Mul => { if (cIsUnsignedInteger(qt)) { op_id = .mul_wrap; } else { op_id = .mul; } }, .Div => { // unsigned/float division uses the operator op_id = .div; }, .Rem => { // unsigned/float division uses the operator op_id = .mod; }, .LT => { op_id = .less_than; }, .GT => { op_id = .greater_than; }, .LE => { op_id = .less_than_equal; }, .GE => { op_id = .greater_than_equal; }, .EQ => { op_id = .equal; }, .NE => { op_id = .not_equal; }, .And => { op_id = .bit_and; }, .Xor => { op_id = .bit_xor; }, .Or => { op_id = .bit_or; }, else => unreachable, } const lhs_uncasted = try transExpr(c, scope, stmt.getLHS(), .used); const rhs_uncasted = try transExpr(c, scope, stmt.getRHS(), .used); const lhs = if (isBoolRes(lhs_uncasted)) try Tag.bool_to_int.create(c.arena, lhs_uncasted) else if (isPointerDiffExpr) try Tag.ptr_to_int.create(c.arena, lhs_uncasted) else lhs_uncasted; const rhs = if (isBoolRes(rhs_uncasted)) try Tag.bool_to_int.create(c.arena, rhs_uncasted) else if (isPointerDiffExpr) try Tag.ptr_to_int.create(c.arena, rhs_uncasted) else rhs_uncasted; const infixOpNode = try transCreateNodeInfixOp(c, scope, op_id, lhs, rhs, result_used); if (isPointerDiffExpr) { // @divExact(@bitCast(<platform-ptrdiff_t>, @ptrToInt(lhs) -% @ptrToInt(rhs)), @sizeOf(<lhs target type>)) const ptrdiff_type = try transQualTypeIntWidthOf(c, qt, true); // C standard requires that pointer subtraction operands are of the same type, // otherwise it is undefined behavior. So we can assume the left and right // sides are the same QualType and arbitrarily choose left. const lhs_expr = stmt.getLHS(); const lhs_qt = getExprQualType(c, lhs_expr); const lhs_qt_translated = try transQualType(c, scope, lhs_qt, lhs_expr.getBeginLoc()); const elem_type = lhs_qt_translated.castTag(.c_pointer).?.data.elem_type; const sizeof = try Tag.sizeof.create(c.arena, elem_type); const bitcast = try Tag.bit_cast.create(c.arena, .{ .lhs = ptrdiff_type, .rhs = infixOpNode }); return Tag.div_exact.create(c.arena, .{ .lhs = bitcast, .rhs = sizeof, }); } return infixOpNode; } fn transCompoundStmtInline( c: *Context, stmt: *const clang.CompoundStmt, block: *Scope.Block, ) TransError!void { var it = stmt.body_begin(); const end_it = stmt.body_end(); while (it != end_it) : (it += 1) { const result = try transStmt(c, &block.base, it[0], .unused); switch (result.tag()) { .declaration, .empty_block => {}, else => try block.statements.append(result), } } } fn transCompoundStmt(c: *Context, scope: *Scope, stmt: *const clang.CompoundStmt) TransError!Node { var block_scope = try Scope.Block.init(c, scope, false); defer block_scope.deinit(); try transCompoundStmtInline(c, stmt, &block_scope); return try block_scope.complete(c); } fn transCStyleCastExprClass( c: *Context, scope: *Scope, stmt: *const clang.CStyleCastExpr, result_used: ResultUsed, ) TransError!Node { const sub_expr = stmt.getSubExpr(); const cast_node = (try transCCast( c, scope, stmt.getBeginLoc(), stmt.getType(), sub_expr.getType(), try transExpr(c, scope, sub_expr, .used), )); return maybeSuppressResult(c, scope, result_used, cast_node); } /// Clang reports the alignment in bits, we use bytes /// Clang uses 0 for "no alignment specified", we use null fn zigAlignment(bit_alignment: c_uint) ?c_uint { if (bit_alignment == 0) return null; return bit_alignment / 8; } fn transDeclStmtOne( c: *Context, scope: *Scope, decl: *const clang.Decl, block_scope: *Scope.Block, ) TransError!void { switch (decl.getKind()) { .Var => { const var_decl = @ptrCast(*const clang.VarDecl, decl); const decl_init = var_decl.getInit(); const qual_type = var_decl.getTypeSourceInfo_getType(); const name = try c.str(@ptrCast(*const clang.NamedDecl, var_decl).getName_bytes_begin()); const mangled_name = try block_scope.makeMangledName(c, name); if (var_decl.getStorageClass() == .Extern) { // This is actually a global variable, put it in the global scope and reference it. // `_ = mangled_name;` return visitVarDecl(c, var_decl, mangled_name); } const is_static_local = var_decl.isStaticLocal(); const is_const = qual_type.isConstQualified(); const loc = decl.getLocation(); const type_node = try transQualTypeMaybeInitialized(c, scope, qual_type, decl_init, loc); var init_node = if (decl_init) |expr| if (expr.getStmtClass() == .StringLiteralClass) try transStringLiteralInitializer(c, scope, @ptrCast(*const clang.StringLiteral, expr), type_node) else try transExprCoercing(c, scope, expr, .used) else if (is_static_local) try Tag.std_mem_zeroes.create(c.arena, type_node) else Tag.undefined_literal.init(); if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node)) { init_node = try Tag.bool_to_int.create(c.arena, init_node); } const var_name: []const u8 = if (is_static_local) Scope.Block.StaticInnerName else mangled_name; var node = try Tag.var_decl.create(c.arena, .{ .is_pub = false, .is_const = is_const, .is_extern = false, .is_export = false, .is_threadlocal = var_decl.getTLSKind() != .None, .linksection_string = null, .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)), .name = var_name, .type = type_node, .init = init_node, }); if (is_static_local) { node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node }); } try block_scope.statements.append(node); const cleanup_attr = var_decl.getCleanupAttribute(); if (cleanup_attr) |fn_decl| { const cleanup_fn_name = try c.str(@ptrCast(*const clang.NamedDecl, fn_decl).getName_bytes_begin()); const fn_id = try Tag.identifier.create(c.arena, cleanup_fn_name); const varname = try Tag.identifier.create(c.arena, mangled_name); const args = try c.arena.alloc(Node, 1); args[0] = try Tag.address_of.create(c.arena, varname); const cleanup_call = try Tag.call.create(c.arena, .{ .lhs = fn_id, .args = args }); const discard = try Tag.discard.create(c.arena, cleanup_call); const deferred_cleanup = try Tag.@"defer".create(c.arena, discard); try block_scope.statements.append(deferred_cleanup); } }, .Typedef => { try transTypeDef(c, scope, @ptrCast(*const clang.TypedefNameDecl, decl)); }, .Record => { try transRecordDecl(c, scope, @ptrCast(*const clang.RecordDecl, decl)); }, .Enum => { try transEnumDecl(c, scope, @ptrCast(*const clang.EnumDecl, decl)); }, .Function => { try visitFnDecl(c, @ptrCast(*const clang.FunctionDecl, decl)); }, else => |kind| return fail( c, error.UnsupportedTranslation, decl.getLocation(), "TODO implement translation of DeclStmt kind {s}", .{@tagName(kind)}, ), } } fn transDeclStmt(c: *Context, scope: *Scope, stmt: *const clang.DeclStmt) TransError!Node { const block_scope = try scope.findBlockScope(c); var it = stmt.decl_begin(); const end_it = stmt.decl_end(); while (it != end_it) : (it += 1) { try transDeclStmtOne(c, scope, it[0], block_scope); } return Tag.declaration.init(); } fn transDeclRefExpr( c: *Context, scope: *Scope, expr: *const clang.DeclRefExpr, ) TransError!Node { const value_decl = expr.getDecl(); const name = try c.str(@ptrCast(*const clang.NamedDecl, value_decl).getName_bytes_begin()); const mangled_name = scope.getAlias(name); var ref_expr = try Tag.identifier.create(c.arena, mangled_name); if (@ptrCast(*const clang.Decl, value_decl).getKind() == .Var) { const var_decl = @ptrCast(*const clang.VarDecl, value_decl); if (var_decl.isStaticLocal()) { ref_expr = try Tag.field_access.create(c.arena, .{ .lhs = ref_expr, .field_name = Scope.Block.StaticInnerName, }); } } return ref_expr; } fn transImplicitCastExpr( c: *Context, scope: *Scope, expr: *const clang.ImplicitCastExpr, result_used: ResultUsed, ) TransError!Node { const sub_expr = expr.getSubExpr(); const dest_type = getExprQualType(c, @ptrCast(*const clang.Expr, expr)); const src_type = getExprQualType(c, sub_expr); switch (expr.getCastKind()) { .BitCast, .FloatingCast, .FloatingToIntegral, .IntegralToFloating, .IntegralCast, .PointerToIntegral, .IntegralToPointer => { const sub_expr_node = try transExpr(c, scope, sub_expr, .used); const casted = try transCCast(c, scope, expr.getBeginLoc(), dest_type, src_type, sub_expr_node); return maybeSuppressResult(c, scope, result_used, casted); }, .LValueToRValue, .NoOp, .FunctionToPointerDecay => { const sub_expr_node = try transExpr(c, scope, sub_expr, .used); return maybeSuppressResult(c, scope, result_used, sub_expr_node); }, .ArrayToPointerDecay => { const sub_expr_node = try transExpr(c, scope, sub_expr, .used); if (exprIsNarrowStringLiteral(sub_expr) or exprIsFlexibleArrayRef(c, sub_expr)) { return maybeSuppressResult(c, scope, result_used, sub_expr_node); } const addr = try Tag.address_of.create(c.arena, sub_expr_node); const casted = try transCPtrCast(c, scope, expr.getBeginLoc(), dest_type, src_type, addr); return maybeSuppressResult(c, scope, result_used, casted); }, .NullToPointer => { return Tag.null_literal.init(); }, .PointerToBoolean => { // @ptrToInt(val) != 0 const ptr_to_int = try Tag.ptr_to_int.create(c.arena, try transExpr(c, scope, sub_expr, .used)); const ne = try Tag.not_equal.create(c.arena, .{ .lhs = ptr_to_int, .rhs = Tag.zero_literal.init() }); return maybeSuppressResult(c, scope, result_used, ne); }, .IntegralToBoolean => { const sub_expr_node = try transExpr(c, scope, sub_expr, .used); // The expression is already a boolean one, return it as-is if (isBoolRes(sub_expr_node)) return maybeSuppressResult(c, scope, result_used, sub_expr_node); // val != 0 const ne = try Tag.not_equal.create(c.arena, .{ .lhs = sub_expr_node, .rhs = Tag.zero_literal.init() }); return maybeSuppressResult(c, scope, result_used, ne); }, .BuiltinFnToFnPtr => { return transBuiltinFnExpr(c, scope, sub_expr, result_used); }, .ToVoid => { // Should only appear in the rhs and lhs of a ConditionalOperator return transExpr(c, scope, sub_expr, .unused); }, else => |kind| return fail( c, error.UnsupportedTranslation, @ptrCast(*const clang.Stmt, expr).getBeginLoc(), "TODO implement translation of CastKind {s}", .{@tagName(kind)}, ), } } fn isBuiltinDefined(name: []const u8) bool { inline for (meta.declarations(c_builtins)) |decl| { if (std.mem.eql(u8, name, decl.name)) return true; } return false; } fn transBuiltinFnExpr(c: *Context, scope: *Scope, expr: *const clang.Expr, used: ResultUsed) TransError!Node { const node = try transExpr(c, scope, expr, used); if (node.castTag(.identifier)) |ident| { const name = ident.data; if (!isBuiltinDefined(name)) return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "TODO implement function '{s}' in std.c.builtins", .{name}); } return node; } fn transBoolExpr( c: *Context, scope: *Scope, expr: *const clang.Expr, used: ResultUsed, ) TransError!Node { if (@ptrCast(*const clang.Stmt, expr).getStmtClass() == .IntegerLiteralClass) { var is_zero: bool = undefined; if (!(@ptrCast(*const clang.IntegerLiteral, expr).isZero(&is_zero, c.clang_context))) { return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "invalid integer literal", .{}); } return Node{ .tag_if_small_enough = @enumToInt(([2]Tag{ .true_literal, .false_literal })[@boolToInt(is_zero)]) }; } var res = try transExpr(c, scope, expr, used); if (isBoolRes(res)) { return maybeSuppressResult(c, scope, used, res); } const ty = getExprQualType(c, expr).getTypePtr(); const node = try finishBoolExpr(c, scope, expr.getBeginLoc(), ty, res, used); return maybeSuppressResult(c, scope, used, node); } fn exprIsBooleanType(expr: *const clang.Expr) bool { return qualTypeIsBoolean(expr.getType()); } fn exprIsNarrowStringLiteral(expr: *const clang.Expr) bool { switch (expr.getStmtClass()) { .StringLiteralClass => { const string_lit = @ptrCast(*const clang.StringLiteral, expr); return string_lit.getCharByteWidth() == 1; }, .PredefinedExprClass => return true, .UnaryOperatorClass => { const op_expr = @ptrCast(*const clang.UnaryOperator, expr).getSubExpr(); return exprIsNarrowStringLiteral(op_expr); }, .ParenExprClass => { const op_expr = @ptrCast(*const clang.ParenExpr, expr).getSubExpr(); return exprIsNarrowStringLiteral(op_expr); }, .GenericSelectionExprClass => { const gen_sel = @ptrCast(*const clang.GenericSelectionExpr, expr); return exprIsNarrowStringLiteral(gen_sel.getResultExpr()); }, else => return false, } } fn exprIsFlexibleArrayRef(c: *Context, expr: *const clang.Expr) bool { if (expr.getStmtClass() == .MemberExprClass) { const member_expr = @ptrCast(*const clang.MemberExpr, expr); const member_decl = member_expr.getMemberDecl(); const decl_kind = @ptrCast(*const clang.Decl, member_decl).getKind(); if (decl_kind == .Field) { const field_decl = @ptrCast(*const clang.FieldDecl, member_decl); return isFlexibleArrayFieldDecl(c, field_decl); } } return false; } fn isBoolRes(res: Node) bool { switch (res.tag()) { .@"or", .@"and", .equal, .not_equal, .less_than, .less_than_equal, .greater_than, .greater_than_equal, .not, .false_literal, .true_literal, => return true, else => return false, } } fn finishBoolExpr( c: *Context, scope: *Scope, loc: clang.SourceLocation, ty: *const clang.Type, node: Node, used: ResultUsed, ) TransError!Node { switch (ty.getTypeClass()) { .Builtin => { const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); switch (builtin_ty.getKind()) { .Bool => return node, .Char_U, .UChar, .Char_S, .SChar, .UShort, .UInt, .ULong, .ULongLong, .Short, .Int, .Long, .LongLong, .UInt128, .Int128, .Float, .Double, .Float128, .LongDouble, .WChar_U, .Char8, .Char16, .Char32, .WChar_S, .Float16, => { // node != 0 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() }); }, .NullPtr => { // node == null return Tag.equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() }); }, else => {}, } }, .Pointer => { // node != null return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() }); }, .Typedef => { const typedef_ty = @ptrCast(*const clang.TypedefType, ty); const typedef_decl = typedef_ty.getDecl(); const underlying_type = typedef_decl.getUnderlyingType(); return finishBoolExpr(c, scope, loc, underlying_type.getTypePtr(), node, used); }, .Enum => { // node != 0 const int_val = try Tag.enum_to_int.create(c.arena, node); return Tag.not_equal.create(c.arena, .{ .lhs = int_val, .rhs = Tag.zero_literal.init() }); }, .Elaborated => { const elaborated_ty = @ptrCast(*const clang.ElaboratedType, ty); const named_type = elaborated_ty.getNamedType(); return finishBoolExpr(c, scope, loc, named_type.getTypePtr(), node, used); }, else => {}, } return fail(c, error.UnsupportedType, loc, "unsupported bool expression type", .{}); } const SuppressCast = enum { with_as, no_as, }; fn transIntegerLiteral( c: *Context, scope: *Scope, expr: *const clang.IntegerLiteral, result_used: ResultUsed, suppress_as: SuppressCast, ) TransError!Node { var eval_result: clang.ExprEvalResult = undefined; if (!expr.EvaluateAsInt(&eval_result, c.clang_context)) { const loc = expr.getBeginLoc(); return fail(c, error.UnsupportedTranslation, loc, "invalid integer literal", .{}); } if (suppress_as == .no_as) { const int_lit_node = try transCreateNodeAPInt(c, eval_result.Val.getInt()); return maybeSuppressResult(c, scope, result_used, int_lit_node); } // Integer literals in C have types, and this can matter for several reasons. // For example, this is valid C: // unsigned char y = 256; // How this gets evaluated is the 256 is an integer, which gets truncated to signed char, then bit-casted // to unsigned char, resulting in 0. In order for this to work, we have to emit this zig code: // var y = @bitCast(u8, @truncate(i8, @as(c_int, 256))); // Ideally in translate-c we could flatten this out to simply: // var y: u8 = 0; // But the first step is to be correct, and the next step is to make the output more elegant. // @as(T, x) const expr_base = @ptrCast(*const clang.Expr, expr); const ty_node = try transQualType(c, scope, expr_base.getType(), expr_base.getBeginLoc()); const rhs = try transCreateNodeAPInt(c, eval_result.Val.getInt()); const as = try Tag.as.create(c.arena, .{ .lhs = ty_node, .rhs = rhs }); return maybeSuppressResult(c, scope, result_used, as); } fn transReturnStmt( c: *Context, scope: *Scope, expr: *const clang.ReturnStmt, ) TransError!Node { const val_expr = expr.getRetValue() orelse return Tag.return_void.init(); var rhs = try transExprCoercing(c, scope, val_expr, .used); const return_qt = scope.findBlockReturnType(c); if (isBoolRes(rhs) and !qualTypeIsBoolean(return_qt)) { rhs = try Tag.bool_to_int.create(c.arena, rhs); } return Tag.@"return".create(c.arena, rhs); } fn transNarrowStringLiteral( c: *Context, scope: *Scope, stmt: *const clang.StringLiteral, result_used: ResultUsed, ) TransError!Node { var len: usize = undefined; const bytes_ptr = stmt.getString_bytes_begin_size(&len); const str = try std.fmt.allocPrint(c.arena, "\"{}\"", .{std.zig.fmtEscapes(bytes_ptr[0..len])}); const node = try Tag.string_literal.create(c.arena, str); return maybeSuppressResult(c, scope, result_used, node); } fn transStringLiteral( c: *Context, scope: *Scope, stmt: *const clang.StringLiteral, result_used: ResultUsed, ) TransError!Node { const kind = stmt.getKind(); switch (kind) { .Ascii, .UTF8 => return transNarrowStringLiteral(c, scope, stmt, result_used), .UTF16, .UTF32, .Wide => { const str_type = @tagName(stmt.getKind()); const name = try std.fmt.allocPrint(c.arena, "zig.{s}_string_{d}", .{ str_type, c.getMangle() }); const expr_base = @ptrCast(*const clang.Expr, stmt); const array_type = try transQualTypeInitialized(c, scope, expr_base.getType(), expr_base, expr_base.getBeginLoc()); const lit_array = try transStringLiteralInitializer(c, scope, stmt, array_type); const decl = try Tag.var_simple.create(c.arena, .{ .name = name, .init = lit_array }); try scope.appendNode(decl); const node = try Tag.identifier.create(c.arena, name); return maybeSuppressResult(c, scope, result_used, node); }, } } fn getArrayPayload(array_type: Node) ast.Payload.Array.ArrayTypeInfo { return (array_type.castTag(.array_type) orelse array_type.castTag(.null_sentinel_array_type).?).data; } /// Translate a string literal that is initializing an array. In general narrow string /// literals become `"<string>".*` or `"<string>"[0..<size>].*` if they need truncation. /// Wide string literals become an array of integers. zero-fillers pad out the array to /// the appropriate length, if necessary. fn transStringLiteralInitializer( c: *Context, scope: *Scope, stmt: *const clang.StringLiteral, array_type: Node, ) TransError!Node { assert(array_type.tag() == .array_type or array_type.tag() == .null_sentinel_array_type); const is_narrow = stmt.getKind() == .Ascii or stmt.getKind() == .UTF8; const str_length = stmt.getLength(); const payload = getArrayPayload(array_type); const array_size = payload.len; const elem_type = payload.elem_type; if (array_size == 0) return Tag.empty_array.create(c.arena, elem_type); const num_inits = math.min(str_length, array_size); const init_node = if (num_inits > 0) blk: { if (is_narrow) { // "string literal".* or string literal"[0..num_inits].* var str = try transNarrowStringLiteral(c, scope, stmt, .used); if (str_length != array_size) str = try Tag.string_slice.create(c.arena, .{ .string = str, .end = num_inits }); break :blk try Tag.deref.create(c.arena, str); } else { const init_list = try c.arena.alloc(Node, num_inits); var i: c_uint = 0; while (i < num_inits) : (i += 1) { init_list[i] = try transCreateCharLitNode(c, false, stmt.getCodeUnit(i)); } const init_args = .{ .len = num_inits, .elem_type = elem_type }; const init_array_type = try if (array_type.tag() == .array_type) Tag.array_type.create(c.arena, init_args) else Tag.null_sentinel_array_type.create(c.arena, init_args); break :blk try Tag.array_init.create(c.arena, .{ .cond = init_array_type, .cases = init_list, }); } } else null; if (num_inits == array_size) return init_node.?; // init_node is only null if num_inits == 0; but if num_inits == array_size == 0 we've already returned assert(array_size > str_length); // If array_size <= str_length, `num_inits == array_size` and we've already returned. const filler_node = try Tag.array_filler.create(c.arena, .{ .type = elem_type, .filler = Tag.zero_literal.init(), .count = array_size - str_length, }); if (init_node) |some| { return Tag.array_cat.create(c.arena, .{ .lhs = some, .rhs = filler_node }); } else { return filler_node; } } /// determine whether `stmt` is a "pointer subtraction expression" - a subtraction where /// both operands resolve to addresses. The C standard requires that both operands /// point to elements of the same array object, but we do not verify that here. fn cIsPointerDiffExpr(c: *Context, stmt: *const clang.BinaryOperator) bool { const lhs = @ptrCast(*const clang.Stmt, stmt.getLHS()); const rhs = @ptrCast(*const clang.Stmt, stmt.getRHS()); return stmt.getOpcode() == .Sub and qualTypeIsPtr(@ptrCast(*const clang.Expr, lhs).getType()) and qualTypeIsPtr(@ptrCast(*const clang.Expr, rhs).getType()); } fn cIsEnum(qt: clang.QualType) bool { return qt.getCanonicalType().getTypeClass() == .Enum; } fn cIsVector(qt: clang.QualType) bool { return qt.getCanonicalType().getTypeClass() == .Vector; } /// Get the underlying int type of an enum. The C compiler chooses a signed int /// type that is large enough to hold all of the enum's values. It is not required /// to be the smallest possible type that can hold all the values. fn cIntTypeForEnum(enum_qt: clang.QualType) clang.QualType { assert(cIsEnum(enum_qt)); const ty = enum_qt.getCanonicalType().getTypePtr(); const enum_ty = @ptrCast(*const clang.EnumType, ty); const enum_decl = enum_ty.getDecl(); return enum_decl.getIntegerType(); } // when modifying this function, make sure to also update std.meta.cast fn transCCast( c: *Context, scope: *Scope, loc: clang.SourceLocation, dst_type: clang.QualType, src_type: clang.QualType, expr: Node, ) !Node { if (qualTypeCanon(dst_type).isVoidType()) return expr; if (dst_type.eq(src_type)) return expr; if (qualTypeIsPtr(dst_type) and qualTypeIsPtr(src_type)) return transCPtrCast(c, scope, loc, dst_type, src_type, expr); const dst_node = try transQualType(c, scope, dst_type, loc); if (cIsInteger(dst_type) and (cIsInteger(src_type) or cIsEnum(src_type))) { // 1. If src_type is an enum, determine the underlying signed int type // 2. Extend or truncate without changing signed-ness. // 3. Bit-cast to correct signed-ness const src_int_type = if (cIsInteger(src_type)) src_type else cIntTypeForEnum(src_type); const src_type_is_signed = cIsSignedInteger(src_int_type); var src_int_expr = if (cIsInteger(src_type)) expr else try Tag.enum_to_int.create(c.arena, expr); if (isBoolRes(src_int_expr)) { src_int_expr = try Tag.bool_to_int.create(c.arena, src_int_expr); } switch (cIntTypeCmp(dst_type, src_int_type)) { .lt => { // @truncate(SameSignSmallerInt, src_int_expr) const ty_node = try transQualTypeIntWidthOf(c, dst_type, src_type_is_signed); src_int_expr = try Tag.truncate.create(c.arena, .{ .lhs = ty_node, .rhs = src_int_expr }); }, .gt => { // @as(SameSignBiggerInt, src_int_expr) const ty_node = try transQualTypeIntWidthOf(c, dst_type, src_type_is_signed); src_int_expr = try Tag.as.create(c.arena, .{ .lhs = ty_node, .rhs = src_int_expr }); }, .eq => { // src_int_expr = src_int_expr }, } // @bitCast(dest_type, intermediate_value) return Tag.bit_cast.create(c.arena, .{ .lhs = dst_node, .rhs = src_int_expr }); } if (cIsVector(src_type) or cIsVector(dst_type)) { // C cast where at least 1 operand is a vector requires them to be same size // @bitCast(dest_type, val) return Tag.bit_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (cIsInteger(dst_type) and qualTypeIsPtr(src_type)) { // @intCast(dest_type, @ptrToInt(val)) const ptr_to_int = try Tag.ptr_to_int.create(c.arena, expr); return Tag.int_cast.create(c.arena, .{ .lhs = dst_node, .rhs = ptr_to_int }); } if (cIsInteger(src_type) and qualTypeIsPtr(dst_type)) { // @intToPtr(dest_type, val) return Tag.int_to_ptr.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (cIsFloating(src_type) and cIsFloating(dst_type)) { // @floatCast(dest_type, val) return Tag.float_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (cIsFloating(src_type) and !cIsFloating(dst_type)) { // @floatToInt(dest_type, val) return Tag.float_to_int.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (!cIsFloating(src_type) and cIsFloating(dst_type)) { // @intToFloat(dest_type, val) return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (qualTypeIsBoolean(src_type) and !qualTypeIsBoolean(dst_type)) { // @boolToInt returns either a comptime_int or a u1 // TODO: if dst_type is 1 bit & signed (bitfield) we need @bitCast // instead of @as const bool_to_int = try Tag.bool_to_int.create(c.arena, expr); return Tag.as.create(c.arena, .{ .lhs = dst_node, .rhs = bool_to_int }); } if (cIsEnum(dst_type)) { // import("std").meta.cast(dest_type, val) return Tag.std_meta_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (cIsEnum(src_type) and !cIsEnum(dst_type)) { // @enumToInt(val) return Tag.enum_to_int.create(c.arena, expr); } // @as(dest_type, val) return Tag.as.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } fn transExpr(c: *Context, scope: *Scope, expr: *const clang.Expr, used: ResultUsed) TransError!Node { return transStmt(c, scope, @ptrCast(*const clang.Stmt, expr), used); } /// Same as `transExpr` but with the knowledge that the operand will be type coerced, and therefore /// an `@as` would be redundant. This is used to prevent redundant `@as` in integer literals. fn transExprCoercing(c: *Context, scope: *Scope, expr: *const clang.Expr, used: ResultUsed) TransError!Node { switch (@ptrCast(*const clang.Stmt, expr).getStmtClass()) { .IntegerLiteralClass => { return transIntegerLiteral(c, scope, @ptrCast(*const clang.IntegerLiteral, expr), .used, .no_as); }, .CharacterLiteralClass => { return transCharLiteral(c, scope, @ptrCast(*const clang.CharacterLiteral, expr), .used, .no_as); }, .UnaryOperatorClass => { const un_expr = @ptrCast(*const clang.UnaryOperator, expr); if (un_expr.getOpcode() == .Extension) { return transExprCoercing(c, scope, un_expr.getSubExpr(), used); } }, .ImplicitCastExprClass => { const cast_expr = @ptrCast(*const clang.ImplicitCastExpr, expr); const sub_expr = cast_expr.getSubExpr(); switch (@ptrCast(*const clang.Stmt, sub_expr).getStmtClass()) { .IntegerLiteralClass, .CharacterLiteralClass => switch (cast_expr.getCastKind()) { .IntegralToFloating => return transExprCoercing(c, scope, sub_expr, used), .IntegralCast => { const dest_type = getExprQualType(c, expr); if (literalFitsInType(c, sub_expr, dest_type)) return transExprCoercing(c, scope, sub_expr, used); }, else => {}, }, else => {}, } }, else => {}, } return transExpr(c, scope, expr, .used); } fn literalFitsInType(c: *Context, expr: *const clang.Expr, qt: clang.QualType) bool { var width = qualTypeIntBitWidth(c, qt) catch 8; if (width == 0) width = 8; // Byte is the smallest type. const is_signed = cIsSignedInteger(qt); const width_max_int = (@as(u64, 1) << math.lossyCast(u6, width - @boolToInt(is_signed))) - 1; switch (@ptrCast(*const clang.Stmt, expr).getStmtClass()) { .CharacterLiteralClass => { const char_lit = @ptrCast(*const clang.CharacterLiteral, expr); const val = char_lit.getValue(); // If the val is less than the max int then it fits. return val <= width_max_int; }, .IntegerLiteralClass => { const int_lit = @ptrCast(*const clang.IntegerLiteral, expr); var eval_result: clang.ExprEvalResult = undefined; if (!int_lit.EvaluateAsInt(&eval_result, c.clang_context)) { return false; } const int = eval_result.Val.getInt(); return int.lessThanEqual(width_max_int); }, else => unreachable, } } fn transInitListExprRecord( c: *Context, scope: *Scope, loc: clang.SourceLocation, expr: *const clang.InitListExpr, ty: *const clang.Type, ) TransError!Node { var is_union_type = false; // Unions and Structs are both represented as RecordDecl const record_ty = ty.getAsRecordType() orelse blk: { is_union_type = true; break :blk ty.getAsUnionType(); } orelse unreachable; const record_decl = record_ty.getDecl(); const record_def = record_decl.getDefinition() orelse unreachable; const ty_node = try transType(c, scope, ty, loc); const init_count = expr.getNumInits(); var field_inits = std.ArrayList(ast.Payload.ContainerInit.Initializer).init(c.gpa); defer field_inits.deinit(); var init_i: c_uint = 0; var it = record_def.field_begin(); const end_it = record_def.field_end(); while (it.neq(end_it)) : (it = it.next()) { const field_decl = it.deref(); // The initializer for a union type has a single entry only if (is_union_type and field_decl != expr.getInitializedFieldInUnion()) { continue; } assert(init_i < init_count); const elem_expr = expr.getInit(init_i); init_i += 1; // Generate the field assignment expression: // .field_name = expr var raw_name = try c.str(@ptrCast(*const clang.NamedDecl, field_decl).getName_bytes_begin()); if (field_decl.isAnonymousStructOrUnion()) { const name = c.decl_table.get(@ptrToInt(field_decl.getCanonicalDecl())).?; raw_name = try mem.dupe(c.arena, u8, name); } try field_inits.append(.{ .name = raw_name, .value = try transExpr(c, scope, elem_expr, .used), }); } return Tag.container_init.create(c.arena, .{ .lhs = ty_node, .inits = try c.arena.dupe(ast.Payload.ContainerInit.Initializer, field_inits.items), }); } fn transInitListExprArray( c: *Context, scope: *Scope, loc: clang.SourceLocation, expr: *const clang.InitListExpr, ty: *const clang.Type, ) TransError!Node { const arr_type = ty.getAsArrayTypeUnsafe(); const child_qt = arr_type.getElementType(); const child_type = try transQualType(c, scope, child_qt, loc); const init_count = expr.getNumInits(); assert(@ptrCast(*const clang.Type, arr_type).isConstantArrayType()); const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, arr_type); const size_ap_int = const_arr_ty.getSize(); const all_count = size_ap_int.getLimitedValue(usize); const leftover_count = all_count - init_count; if (all_count == 0) { return Tag.empty_array.create(c.arena, child_type); } const init_node = if (init_count != 0) blk: { const init_list = try c.arena.alloc(Node, init_count); for (init_list) |*init, i| { const elem_expr = expr.getInit(@intCast(c_uint, i)); init.* = try transExprCoercing(c, scope, elem_expr, .used); } const init_node = try Tag.array_init.create(c.arena, .{ .cond = try Tag.array_type.create(c.arena, .{ .len = init_count, .elem_type = child_type }), .cases = init_list, }); if (leftover_count == 0) { return init_node; } break :blk init_node; } else null; const filler_val_expr = expr.getArrayFiller(); const filler_node = try Tag.array_filler.create(c.arena, .{ .type = child_type, .filler = try transExprCoercing(c, scope, filler_val_expr, .used), .count = leftover_count, }); if (init_node) |some| { return Tag.array_cat.create(c.arena, .{ .lhs = some, .rhs = filler_node }); } else { return filler_node; } } fn transInitListExprVector( c: *Context, scope: *Scope, loc: clang.SourceLocation, expr: *const clang.InitListExpr, ty: *const clang.Type, ) TransError!Node { const qt = getExprQualType(c, @ptrCast(*const clang.Expr, expr)); const vector_type = try transQualType(c, scope, qt, loc); const init_count = expr.getNumInits(); if (init_count == 0) { return Tag.container_init.create(c.arena, .{ .lhs = vector_type, .inits = try c.arena.alloc(ast.Payload.ContainerInit.Initializer, 0), }); } var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); // workaround for https://github.com/ziglang/zig/issues/8322 // we store the initializers in temp variables and use those // to initialize the vector. Eventually we can just directly // construct the init_list from casted source members var i: usize = 0; while (i < init_count) : (i += 1) { const mangled_name = try block_scope.makeMangledName(c, "tmp"); const init_expr = expr.getInit(@intCast(c_uint, i)); const tmp_decl_node = try Tag.var_simple.create(c.arena, .{ .name = mangled_name, .init = try transExpr(c, &block_scope.base, init_expr, .used), }); try block_scope.statements.append(tmp_decl_node); } const init_list = try c.arena.alloc(Node, init_count); for (init_list) |*init, init_index| { const tmp_decl = block_scope.statements.items[init_index]; const name = tmp_decl.castTag(.var_simple).?.data.name; init.* = try Tag.identifier.create(c.arena, name); } const array_init = try Tag.array_init.create(c.arena, .{ .cond = vector_type, .cases = init_list, }); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = array_init, }); try block_scope.statements.append(break_node); return block_scope.complete(c); } fn transInitListExpr( c: *Context, scope: *Scope, expr: *const clang.InitListExpr, used: ResultUsed, ) TransError!Node { const qt = getExprQualType(c, @ptrCast(*const clang.Expr, expr)); var qual_type = qt.getTypePtr(); const source_loc = @ptrCast(*const clang.Expr, expr).getBeginLoc(); if (qualTypeWasDemotedToOpaque(c, qt)) { return fail(c, error.UnsupportedTranslation, source_loc, "Cannot initialize opaque type", .{}); } if (qual_type.isRecordType()) { return maybeSuppressResult(c, scope, used, try transInitListExprRecord( c, scope, source_loc, expr, qual_type, )); } else if (qual_type.isArrayType()) { return maybeSuppressResult(c, scope, used, try transInitListExprArray( c, scope, source_loc, expr, qual_type, )); } else if (qual_type.isVectorType()) { return maybeSuppressResult(c, scope, used, try transInitListExprVector( c, scope, source_loc, expr, qual_type, )); } else { const type_name = c.str(qual_type.getTypeClassName()); return fail(c, error.UnsupportedType, source_loc, "unsupported initlist type: '{s}'", .{type_name}); } } fn transZeroInitExpr( c: *Context, scope: *Scope, source_loc: clang.SourceLocation, ty: *const clang.Type, ) TransError!Node { switch (ty.getTypeClass()) { .Builtin => { const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); switch (builtin_ty.getKind()) { .Bool => return Tag.false_literal.init(), .Char_U, .UChar, .Char_S, .Char8, .SChar, .UShort, .UInt, .ULong, .ULongLong, .Short, .Int, .Long, .LongLong, .UInt128, .Int128, .Float, .Double, .Float128, .Float16, .LongDouble, => return Tag.zero_literal.init(), else => return fail(c, error.UnsupportedType, source_loc, "unsupported builtin type", .{}), } }, .Pointer => return Tag.null_literal.init(), .Typedef => { const typedef_ty = @ptrCast(*const clang.TypedefType, ty); const typedef_decl = typedef_ty.getDecl(); return transZeroInitExpr( c, scope, source_loc, typedef_decl.getUnderlyingType().getTypePtr(), ); }, else => return Tag.std_mem_zeroes.create(c.arena, try transType(c, scope, ty, source_loc)), } } fn transImplicitValueInitExpr( c: *Context, scope: *Scope, expr: *const clang.Expr, used: ResultUsed, ) TransError!Node { const source_loc = expr.getBeginLoc(); const qt = getExprQualType(c, expr); const ty = qt.getTypePtr(); return transZeroInitExpr(c, scope, source_loc, ty); } /// If a statement can possibly translate to a Zig assignment (either directly because it's /// an assignment in C or indirectly via result assignment to `_`) AND it's the sole statement /// in the body of an if statement or loop, then we need to put the statement into its own block. /// The `else` case here corresponds to statements that could result in an assignment. If a statement /// class never needs a block, add its enum to the top prong. fn maybeBlockify(c: *Context, scope: *Scope, stmt: *const clang.Stmt) TransError!Node { switch (stmt.getStmtClass()) { .BreakStmtClass, .CompoundStmtClass, .ContinueStmtClass, .DeclRefExprClass, .DeclStmtClass, .DoStmtClass, .ForStmtClass, .IfStmtClass, .ReturnStmtClass, .NullStmtClass, .WhileStmtClass, => return transStmt(c, scope, stmt, .unused), else => { var block_scope = try Scope.Block.init(c, scope, false); defer block_scope.deinit(); const result = try transStmt(c, &block_scope.base, stmt, .unused); try block_scope.statements.append(result); return block_scope.complete(c); }, } } fn transIfStmt( c: *Context, scope: *Scope, stmt: *const clang.IfStmt, ) TransError!Node { // if (c) t // if (c) t else e var cond_scope = Scope.Condition{ .base = .{ .parent = scope, .id = .condition, }, }; defer cond_scope.deinit(); const cond_expr = @ptrCast(*const clang.Expr, stmt.getCond()); const cond = try transBoolExpr(c, &cond_scope.base, cond_expr, .used); const then_body = try maybeBlockify(c, scope, stmt.getThen()); const else_body = if (stmt.getElse()) |expr| try maybeBlockify(c, scope, expr) else null; return Tag.@"if".create(c.arena, .{ .cond = cond, .then = then_body, .@"else" = else_body }); } fn transWhileLoop( c: *Context, scope: *Scope, stmt: *const clang.WhileStmt, ) TransError!Node { var cond_scope = Scope.Condition{ .base = .{ .parent = scope, .id = .condition, }, }; defer cond_scope.deinit(); const cond_expr = @ptrCast(*const clang.Expr, stmt.getCond()); const cond = try transBoolExpr(c, &cond_scope.base, cond_expr, .used); var loop_scope = Scope{ .parent = scope, .id = .loop, }; const body = try maybeBlockify(c, &loop_scope, stmt.getBody()); return Tag.@"while".create(c.arena, .{ .cond = cond, .body = body, .cont_expr = null }); } fn transDoWhileLoop( c: *Context, scope: *Scope, stmt: *const clang.DoStmt, ) TransError!Node { var loop_scope = Scope{ .parent = scope, .id = .do_loop, }; // if (!cond) break; var cond_scope = Scope.Condition{ .base = .{ .parent = scope, .id = .condition, }, }; defer cond_scope.deinit(); const cond = try transBoolExpr(c, &cond_scope.base, @ptrCast(*const clang.Expr, stmt.getCond()), .used); const if_not_break = switch (cond.tag()) { .false_literal => return transStmt(c, scope, stmt.getBody(), .unused), .true_literal => { const body_node = try maybeBlockify(c, scope, stmt.getBody()); return Tag.while_true.create(c.arena, body_node); }, else => try Tag.if_not_break.create(c.arena, cond), }; const body_node = if (stmt.getBody().getStmtClass() == .CompoundStmtClass) blk: { // there's already a block in C, so we'll append our condition to it. // c: do { // c: a; // c: b; // c: } while(c); // zig: while (true) { // zig: a; // zig: b; // zig: if (!cond) break; // zig: } const node = try transStmt(c, &loop_scope, stmt.getBody(), .unused); const block = node.castTag(.block).?; block.data.stmts.len += 1; // This is safe since we reserve one extra space in Scope.Block.complete. block.data.stmts[block.data.stmts.len - 1] = if_not_break; break :blk node; } else blk: { // the C statement is without a block, so we need to create a block to contain it. // c: do // c: a; // c: while(c); // zig: while (true) { // zig: a; // zig: if (!cond) break; // zig: } const statements = try c.arena.alloc(Node, 2); statements[0] = try transStmt(c, &loop_scope, stmt.getBody(), .unused); statements[1] = if_not_break; break :blk try Tag.block.create(c.arena, .{ .label = null, .stmts = statements }); }; return Tag.while_true.create(c.arena, body_node); } fn transForLoop( c: *Context, scope: *Scope, stmt: *const clang.ForStmt, ) TransError!Node { var loop_scope = Scope{ .parent = scope, .id = .loop, }; var block_scope: ?Scope.Block = null; defer if (block_scope) |*bs| bs.deinit(); if (stmt.getInit()) |init| { block_scope = try Scope.Block.init(c, scope, false); loop_scope.parent = &block_scope.?.base; const init_node = try transStmt(c, &block_scope.?.base, init, .unused); if (init_node.tag() != .declaration) try block_scope.?.statements.append(init_node); } var cond_scope = Scope.Condition{ .base = .{ .parent = &loop_scope, .id = .condition, }, }; defer cond_scope.deinit(); const cond = if (stmt.getCond()) |cond| try transBoolExpr(c, &cond_scope.base, cond, .used) else Tag.true_literal.init(); const cont_expr = if (stmt.getInc()) |incr| try transExpr(c, &cond_scope.base, incr, .unused) else null; const body = try maybeBlockify(c, &loop_scope, stmt.getBody()); const while_node = try Tag.@"while".create(c.arena, .{ .cond = cond, .body = body, .cont_expr = cont_expr }); if (block_scope) |*bs| { try bs.statements.append(while_node); return try bs.complete(c); } else { return while_node; } } fn transSwitch( c: *Context, scope: *Scope, stmt: *const clang.SwitchStmt, ) TransError!Node { var loop_scope = Scope{ .parent = scope, .id = .loop, }; var block_scope = try Scope.Block.init(c, &loop_scope, false); defer block_scope.deinit(); const base_scope = &block_scope.base; var cond_scope = Scope.Condition{ .base = .{ .parent = base_scope, .id = .condition, }, }; defer cond_scope.deinit(); const switch_expr = try transExpr(c, &cond_scope.base, stmt.getCond(), .used); var cases = std.ArrayList(Node).init(c.gpa); defer cases.deinit(); var has_default = false; const body = stmt.getBody(); assert(body.getStmtClass() == .CompoundStmtClass); const compound_stmt = @ptrCast(*const clang.CompoundStmt, body); var it = compound_stmt.body_begin(); const end_it = compound_stmt.body_end(); // Iterate over switch body and collect all cases. // Fallthrough is handled by duplicating statements. while (it != end_it) : (it += 1) { switch (it[0].getStmtClass()) { .CaseStmtClass => { var items = std.ArrayList(Node).init(c.gpa); defer items.deinit(); const sub = try transCaseStmt(c, base_scope, it[0], &items); const res = try transSwitchProngStmt(c, base_scope, sub, it, end_it); if (items.items.len == 0) { has_default = true; const switch_else = try Tag.switch_else.create(c.arena, res); try cases.append(switch_else); } else { const switch_prong = try Tag.switch_prong.create(c.arena, .{ .cases = try c.arena.dupe(Node, items.items), .cond = res, }); try cases.append(switch_prong); } }, .DefaultStmtClass => { has_default = true; const default_stmt = @ptrCast(*const clang.DefaultStmt, it[0]); var sub = default_stmt.getSubStmt(); while (true) switch (sub.getStmtClass()) { .CaseStmtClass => sub = @ptrCast(*const clang.CaseStmt, sub).getSubStmt(), .DefaultStmtClass => sub = @ptrCast(*const clang.DefaultStmt, sub).getSubStmt(), else => break, }; const res = try transSwitchProngStmt(c, base_scope, sub, it, end_it); const switch_else = try Tag.switch_else.create(c.arena, res); try cases.append(switch_else); }, else => {}, // collected in transSwitchProngStmt } } if (!has_default) { const else_prong = try Tag.switch_else.create(c.arena, Tag.empty_block.init()); try cases.append(else_prong); } const switch_node = try Tag.@"switch".create(c.arena, .{ .cond = switch_expr, .cases = try c.arena.dupe(Node, cases.items), }); try block_scope.statements.append(switch_node); try block_scope.statements.append(Tag.@"break".init()); const while_body = try block_scope.complete(c); return Tag.while_true.create(c.arena, while_body); } /// Collects all items for this case, returns the first statement after the labels. /// If items ends up empty, the prong should be translated as an else. fn transCaseStmt(c: *Context, scope: *Scope, stmt: *const clang.Stmt, items: *std.ArrayList(Node)) TransError!*const clang.Stmt { var sub = stmt; var seen_default = false; while (true) { switch (sub.getStmtClass()) { .DefaultStmtClass => { seen_default = true; items.items.len = 0; const default_stmt = @ptrCast(*const clang.DefaultStmt, sub); sub = default_stmt.getSubStmt(); }, .CaseStmtClass => { const case_stmt = @ptrCast(*const clang.CaseStmt, sub); if (seen_default) { items.items.len = 0; sub = case_stmt.getSubStmt(); continue; } const expr = if (case_stmt.getRHS()) |rhs| blk: { const lhs_node = try transExprCoercing(c, scope, case_stmt.getLHS(), .used); const rhs_node = try transExprCoercing(c, scope, rhs, .used); break :blk try Tag.ellipsis3.create(c.arena, .{ .lhs = lhs_node, .rhs = rhs_node }); } else try transExprCoercing(c, scope, case_stmt.getLHS(), .used); try items.append(expr); sub = case_stmt.getSubStmt(); }, else => return sub, } } } /// Collects all statements seen by this case into a block. /// Avoids creating a block if the first statement is a break or return. fn transSwitchProngStmt( c: *Context, scope: *Scope, stmt: *const clang.Stmt, parent_it: clang.CompoundStmt.ConstBodyIterator, parent_end_it: clang.CompoundStmt.ConstBodyIterator, ) TransError!Node { switch (stmt.getStmtClass()) { .BreakStmtClass => return Tag.@"break".init(), .ReturnStmtClass => return transStmt(c, scope, stmt, .unused), .CaseStmtClass, .DefaultStmtClass => unreachable, else => { var block_scope = try Scope.Block.init(c, scope, false); defer block_scope.deinit(); // we do not need to translate `stmt` since it is the first stmt of `parent_it` try transSwitchProngStmtInline(c, &block_scope, parent_it, parent_end_it); return try block_scope.complete(c); }, } } /// Collects all statements seen by this case into a block. fn transSwitchProngStmtInline( c: *Context, block: *Scope.Block, start_it: clang.CompoundStmt.ConstBodyIterator, end_it: clang.CompoundStmt.ConstBodyIterator, ) TransError!void { var it = start_it; while (it != end_it) : (it += 1) { switch (it[0].getStmtClass()) { .ReturnStmtClass => { const result = try transStmt(c, &block.base, it[0], .unused); try block.statements.append(result); return; }, .BreakStmtClass => { try block.statements.append(Tag.@"break".init()); return; }, .CaseStmtClass => { var sub = @ptrCast(*const clang.CaseStmt, it[0]).getSubStmt(); while (true) switch (sub.getStmtClass()) { .CaseStmtClass => sub = @ptrCast(*const clang.CaseStmt, sub).getSubStmt(), .DefaultStmtClass => sub = @ptrCast(*const clang.DefaultStmt, sub).getSubStmt(), else => break, }; const result = try transStmt(c, &block.base, sub, .unused); assert(result.tag() != .declaration); try block.statements.append(result); if (result.isNoreturn(true)) { return; } }, .DefaultStmtClass => { var sub = @ptrCast(*const clang.DefaultStmt, it[0]).getSubStmt(); while (true) switch (sub.getStmtClass()) { .CaseStmtClass => sub = @ptrCast(*const clang.CaseStmt, sub).getSubStmt(), .DefaultStmtClass => sub = @ptrCast(*const clang.DefaultStmt, sub).getSubStmt(), else => break, }; const result = try transStmt(c, &block.base, sub, .unused); assert(result.tag() != .declaration); try block.statements.append(result); if (result.isNoreturn(true)) { return; } }, .CompoundStmtClass => { const result = try transCompoundStmt(c, &block.base, @ptrCast(*const clang.CompoundStmt, it[0])); try block.statements.append(result); if (result.isNoreturn(true)) { return; } }, else => { const result = try transStmt(c, &block.base, it[0], .unused); switch (result.tag()) { .declaration, .empty_block => {}, else => try block.statements.append(result), } }, } } return; } fn transConstantExpr(c: *Context, scope: *Scope, expr: *const clang.Expr, used: ResultUsed) TransError!Node { var result: clang.ExprEvalResult = undefined; if (!expr.evaluateAsConstantExpr(&result, .Normal, c.clang_context)) return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "invalid constant expression", .{}); switch (result.Val.getKind()) { .Int => { // See comment in `transIntegerLiteral` for why this code is here. // @as(T, x) const expr_base = @ptrCast(*const clang.Expr, expr); const as_node = try Tag.as.create(c.arena, .{ .lhs = try transQualType(c, scope, expr_base.getType(), expr_base.getBeginLoc()), .rhs = try transCreateNodeAPInt(c, result.Val.getInt()), }); return maybeSuppressResult(c, scope, used, as_node); }, else => |kind| { return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "unsupported constant expression kind '{s}'", .{kind}); }, } } fn transPredefinedExpr(c: *Context, scope: *Scope, expr: *const clang.PredefinedExpr, used: ResultUsed) TransError!Node { return transStringLiteral(c, scope, expr.getFunctionName(), used); } fn transCreateCharLitNode(c: *Context, narrow: bool, val: u32) TransError!Node { return Tag.char_literal.create(c.arena, if (narrow) try std.fmt.allocPrint(c.arena, "'{'}'", .{std.zig.fmtEscapes(&.{@intCast(u8, val)})}) else try std.fmt.allocPrint(c.arena, "'\\u{{{x}}}'", .{val})); } fn transCharLiteral( c: *Context, scope: *Scope, stmt: *const clang.CharacterLiteral, result_used: ResultUsed, suppress_as: SuppressCast, ) TransError!Node { const kind = stmt.getKind(); const val = stmt.getValue(); const narrow = kind == .Ascii or kind == .UTF8; // C has a somewhat obscure feature called multi-character character constant // e.g. 'abcd' const int_lit_node = if (kind == .Ascii and val > 255) try transCreateNodeNumber(c, val, .int) else try transCreateCharLitNode(c, narrow, val); if (suppress_as == .no_as) { return maybeSuppressResult(c, scope, result_used, int_lit_node); } // See comment in `transIntegerLiteral` for why this code is here. // @as(T, x) const expr_base = @ptrCast(*const clang.Expr, stmt); const as_node = try Tag.as.create(c.arena, .{ .lhs = try transQualType(c, scope, expr_base.getType(), expr_base.getBeginLoc()), .rhs = int_lit_node, }); return maybeSuppressResult(c, scope, result_used, as_node); } fn transStmtExpr(c: *Context, scope: *Scope, stmt: *const clang.StmtExpr, used: ResultUsed) TransError!Node { const comp = stmt.getSubStmt(); if (used == .unused) { return transCompoundStmt(c, scope, comp); } var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); var it = comp.body_begin(); const end_it = comp.body_end(); while (it != end_it - 1) : (it += 1) { const result = try transStmt(c, &block_scope.base, it[0], .unused); switch (result.tag()) { .declaration, .empty_block => {}, else => try block_scope.statements.append(result), } } const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = try transStmt(c, &block_scope.base, it[0], .used), }); try block_scope.statements.append(break_node); const res = try block_scope.complete(c); return maybeSuppressResult(c, scope, used, res); } fn transMemberExpr(c: *Context, scope: *Scope, stmt: *const clang.MemberExpr, result_used: ResultUsed) TransError!Node { var container_node = try transExpr(c, scope, stmt.getBase(), .used); if (stmt.isArrow()) { container_node = try Tag.deref.create(c.arena, container_node); } const member_decl = stmt.getMemberDecl(); const name = blk: { const decl_kind = @ptrCast(*const clang.Decl, member_decl).getKind(); // If we're referring to a anonymous struct/enum find the bogus name // we've assigned to it during the RecordDecl translation if (decl_kind == .Field) { const field_decl = @ptrCast(*const clang.FieldDecl, member_decl); if (field_decl.isAnonymousStructOrUnion()) { const name = c.decl_table.get(@ptrToInt(field_decl.getCanonicalDecl())).?; break :blk try mem.dupe(c.arena, u8, name); } } const decl = @ptrCast(*const clang.NamedDecl, member_decl); break :blk try c.str(decl.getName_bytes_begin()); }; var node = try Tag.field_access.create(c.arena, .{ .lhs = container_node, .field_name = name }); if (exprIsFlexibleArrayRef(c, @ptrCast(*const clang.Expr, stmt))) { node = try Tag.call.create(c.arena, .{ .lhs = node, .args = &.{} }); } return maybeSuppressResult(c, scope, result_used, node); } fn transArrayAccess(c: *Context, scope: *Scope, stmt: *const clang.ArraySubscriptExpr, result_used: ResultUsed) TransError!Node { var base_stmt = stmt.getBase(); // Unwrap the base statement if it's an array decayed to a bare pointer type // so that we index the array itself if (@ptrCast(*const clang.Stmt, base_stmt).getStmtClass() == .ImplicitCastExprClass) { const implicit_cast = @ptrCast(*const clang.ImplicitCastExpr, base_stmt); if (implicit_cast.getCastKind() == .ArrayToPointerDecay) { base_stmt = implicit_cast.getSubExpr(); } } const container_node = try transExpr(c, scope, base_stmt, .used); // cast if the index is long long or signed const subscr_expr = stmt.getIdx(); const qt = getExprQualType(c, subscr_expr); const is_longlong = cIsLongLongInteger(qt); const is_signed = cIsSignedInteger(qt); const rhs = if (is_longlong or is_signed) blk: { // check if long long first so that signed long long doesn't just become unsigned long long var typeid_node = if (is_longlong) try Tag.identifier.create(c.arena, "usize") else try transQualTypeIntWidthOf(c, qt, false); break :blk try Tag.int_cast.create(c.arena, .{ .lhs = typeid_node, .rhs = try transExpr(c, scope, subscr_expr, .used) }); } else try transExpr(c, scope, subscr_expr, .used); const node = try Tag.array_access.create(c.arena, .{ .lhs = container_node, .rhs = rhs, }); return maybeSuppressResult(c, scope, result_used, node); } /// Check if an expression is ultimately a reference to a function declaration /// (which means it should not be unwrapped with `.?` in translated code) fn cIsFunctionDeclRef(expr: *const clang.Expr) bool { switch (expr.getStmtClass()) { .ParenExprClass => { const op_expr = @ptrCast(*const clang.ParenExpr, expr).getSubExpr(); return cIsFunctionDeclRef(op_expr); }, .DeclRefExprClass => { const decl_ref = @ptrCast(*const clang.DeclRefExpr, expr); const value_decl = decl_ref.getDecl(); const qt = value_decl.getType(); return qualTypeChildIsFnProto(qt); }, .ImplicitCastExprClass => { const implicit_cast = @ptrCast(*const clang.ImplicitCastExpr, expr); const cast_kind = implicit_cast.getCastKind(); if (cast_kind == .BuiltinFnToFnPtr) return true; if (cast_kind == .FunctionToPointerDecay) { return cIsFunctionDeclRef(implicit_cast.getSubExpr()); } return false; }, .UnaryOperatorClass => { const un_op = @ptrCast(*const clang.UnaryOperator, expr); const opcode = un_op.getOpcode(); return (opcode == .AddrOf or opcode == .Deref) and cIsFunctionDeclRef(un_op.getSubExpr()); }, .GenericSelectionExprClass => { const gen_sel = @ptrCast(*const clang.GenericSelectionExpr, expr); return cIsFunctionDeclRef(gen_sel.getResultExpr()); }, else => return false, } } fn transCallExpr(c: *Context, scope: *Scope, stmt: *const clang.CallExpr, result_used: ResultUsed) TransError!Node { const callee = stmt.getCallee(); var raw_fn_expr = try transExpr(c, scope, callee, .used); var is_ptr = false; const fn_ty = qualTypeGetFnProto(callee.getType(), &is_ptr); const fn_expr = if (is_ptr and fn_ty != null and !cIsFunctionDeclRef(callee)) try Tag.unwrap.create(c.arena, raw_fn_expr) else raw_fn_expr; const num_args = stmt.getNumArgs(); const args = try c.arena.alloc(Node, num_args); const c_args = stmt.getArgs(); var i: usize = 0; while (i < num_args) : (i += 1) { var arg = try transExpr(c, scope, c_args[i], .used); // In C the result type of a boolean expression is int. If this result is passed as // an argument to a function whose parameter is also int, there is no cast. Therefore // in Zig we'll need to cast it from bool to u1 (which will safely coerce to c_int). if (fn_ty) |ty| { switch (ty) { .Proto => |fn_proto| { const param_count = fn_proto.getNumParams(); if (i < param_count) { const param_qt = fn_proto.getParamType(@intCast(c_uint, i)); if (isBoolRes(arg) and cIsNativeInt(param_qt)) { arg = try Tag.bool_to_int.create(c.arena, arg); } } }, else => {}, } } args[i] = arg; } const node = try Tag.call.create(c.arena, .{ .lhs = fn_expr, .args = args }); if (fn_ty) |ty| { const canon = ty.getReturnType().getCanonicalType(); const ret_ty = canon.getTypePtr(); if (ret_ty.isVoidType()) { return node; } } return maybeSuppressResult(c, scope, result_used, node); } const ClangFunctionType = union(enum) { Proto: *const clang.FunctionProtoType, NoProto: *const clang.FunctionType, fn getReturnType(self: @This()) clang.QualType { switch (@as(meta.Tag(@This()), self)) { .Proto => return self.Proto.getReturnType(), .NoProto => return self.NoProto.getReturnType(), } } }; fn qualTypeGetFnProto(qt: clang.QualType, is_ptr: *bool) ?ClangFunctionType { const canon = qt.getCanonicalType(); var ty = canon.getTypePtr(); is_ptr.* = false; if (ty.getTypeClass() == .Pointer) { is_ptr.* = true; const child_qt = ty.getPointeeType(); ty = child_qt.getTypePtr(); } if (ty.getTypeClass() == .FunctionProto) { return ClangFunctionType{ .Proto = @ptrCast(*const clang.FunctionProtoType, ty) }; } if (ty.getTypeClass() == .FunctionNoProto) { return ClangFunctionType{ .NoProto = @ptrCast(*const clang.FunctionType, ty) }; } return null; } fn transUnaryExprOrTypeTraitExpr( c: *Context, scope: *Scope, stmt: *const clang.UnaryExprOrTypeTraitExpr, result_used: ResultUsed, ) TransError!Node { const loc = stmt.getBeginLoc(); const type_node = try transQualType(c, scope, stmt.getTypeOfArgument(), loc); const kind = stmt.getKind(); switch (kind) { .SizeOf => return Tag.sizeof.create(c.arena, type_node), .AlignOf => return Tag.alignof.create(c.arena, type_node), .PreferredAlignOf, .VecStep, .OpenMPRequiredSimdAlign, => return fail( c, error.UnsupportedTranslation, loc, "Unsupported type trait kind {}", .{kind}, ), } } fn qualTypeHasWrappingOverflow(qt: clang.QualType) bool { if (cIsUnsignedInteger(qt)) { // unsigned integer overflow wraps around. return true; } else { // float, signed integer, and pointer overflow is undefined behavior. return false; } } fn transUnaryOperator(c: *Context, scope: *Scope, stmt: *const clang.UnaryOperator, used: ResultUsed) TransError!Node { const op_expr = stmt.getSubExpr(); switch (stmt.getOpcode()) { .PostInc => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreatePostCrement(c, scope, stmt, .add_wrap_assign, used) else return transCreatePostCrement(c, scope, stmt, .add_assign, used), .PostDec => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreatePostCrement(c, scope, stmt, .sub_wrap_assign, used) else return transCreatePostCrement(c, scope, stmt, .sub_assign, used), .PreInc => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreatePreCrement(c, scope, stmt, .add_wrap_assign, used) else return transCreatePreCrement(c, scope, stmt, .add_assign, used), .PreDec => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreatePreCrement(c, scope, stmt, .sub_wrap_assign, used) else return transCreatePreCrement(c, scope, stmt, .sub_assign, used), .AddrOf => { if (cIsFunctionDeclRef(op_expr)) { return transExpr(c, scope, op_expr, used); } return Tag.address_of.create(c.arena, try transExpr(c, scope, op_expr, used)); }, .Deref => { const node = try transExpr(c, scope, op_expr, used); var is_ptr = false; const fn_ty = qualTypeGetFnProto(op_expr.getType(), &is_ptr); if (fn_ty != null and is_ptr) return node; const unwrapped = try Tag.unwrap.create(c.arena, node); return Tag.deref.create(c.arena, unwrapped); }, .Plus => return transExpr(c, scope, op_expr, used), .Minus => { if (!qualTypeHasWrappingOverflow(op_expr.getType())) { return Tag.negate.create(c.arena, try transExpr(c, scope, op_expr, .used)); } else if (cIsUnsignedInteger(op_expr.getType())) { // use -% x for unsigned integers return Tag.negate_wrap.create(c.arena, try transExpr(c, scope, op_expr, .used)); } else return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "C negation with non float non integer", .{}); }, .Not => { return Tag.bit_not.create(c.arena, try transExpr(c, scope, op_expr, .used)); }, .LNot => { return Tag.not.create(c.arena, try transBoolExpr(c, scope, op_expr, .used)); }, .Extension => { return transExpr(c, scope, stmt.getSubExpr(), used); }, else => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "unsupported C translation {}", .{stmt.getOpcode()}), } } fn transCreatePreCrement( c: *Context, scope: *Scope, stmt: *const clang.UnaryOperator, op: Tag, used: ResultUsed, ) TransError!Node { const op_expr = stmt.getSubExpr(); if (used == .unused) { // common case // c: ++expr // zig: expr += 1 const lhs = try transExpr(c, scope, op_expr, .used); const rhs = Tag.one_literal.init(); return transCreateNodeInfixOp(c, scope, op, lhs, rhs, .used); } // worst case // c: ++expr // zig: (blk: { // zig: const _ref = &expr; // zig: _ref.* += 1; // zig: break :blk _ref.* // zig: }) var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const ref = try block_scope.makeMangledName(c, "ref"); const expr = try transExpr(c, &block_scope.base, op_expr, .used); const addr_of = try Tag.address_of.create(c.arena, expr); const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = ref, .init = addr_of }); try block_scope.statements.append(ref_decl); const lhs_node = try Tag.identifier.create(c.arena, ref); const ref_node = try Tag.deref.create(c.arena, lhs_node); const node = try transCreateNodeInfixOp(c, &block_scope.base, op, ref_node, Tag.one_literal.init(), .used); try block_scope.statements.append(node); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = ref_node, }); try block_scope.statements.append(break_node); return block_scope.complete(c); } fn transCreatePostCrement( c: *Context, scope: *Scope, stmt: *const clang.UnaryOperator, op: Tag, used: ResultUsed, ) TransError!Node { const op_expr = stmt.getSubExpr(); if (used == .unused) { // common case // c: expr++ // zig: expr += 1 const lhs = try transExpr(c, scope, op_expr, .used); const rhs = Tag.one_literal.init(); return transCreateNodeInfixOp(c, scope, op, lhs, rhs, .used); } // worst case // c: expr++ // zig: (blk: { // zig: const _ref = &expr; // zig: const _tmp = _ref.*; // zig: _ref.* += 1; // zig: break :blk _tmp // zig: }) var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const ref = try block_scope.makeMangledName(c, "ref"); const expr = try transExpr(c, &block_scope.base, op_expr, .used); const addr_of = try Tag.address_of.create(c.arena, expr); const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = ref, .init = addr_of }); try block_scope.statements.append(ref_decl); const lhs_node = try Tag.identifier.create(c.arena, ref); const ref_node = try Tag.deref.create(c.arena, lhs_node); const tmp = try block_scope.makeMangledName(c, "tmp"); const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = ref_node }); try block_scope.statements.append(tmp_decl); const node = try transCreateNodeInfixOp(c, &block_scope.base, op, ref_node, Tag.one_literal.init(), .used); try block_scope.statements.append(node); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = try Tag.identifier.create(c.arena, tmp), }); try block_scope.statements.append(break_node); return block_scope.complete(c); } fn transCompoundAssignOperator(c: *Context, scope: *Scope, stmt: *const clang.CompoundAssignOperator, used: ResultUsed) TransError!Node { switch (stmt.getOpcode()) { .MulAssign => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreateCompoundAssign(c, scope, stmt, .mul_wrap_assign, used) else return transCreateCompoundAssign(c, scope, stmt, .mul_assign, used), .AddAssign => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreateCompoundAssign(c, scope, stmt, .add_wrap_assign, used) else return transCreateCompoundAssign(c, scope, stmt, .add_assign, used), .SubAssign => if (qualTypeHasWrappingOverflow(stmt.getType())) return transCreateCompoundAssign(c, scope, stmt, .sub_wrap_assign, used) else return transCreateCompoundAssign(c, scope, stmt, .sub_assign, used), .DivAssign => return transCreateCompoundAssign(c, scope, stmt, .div_assign, used), .RemAssign => return transCreateCompoundAssign(c, scope, stmt, .mod_assign, used), .ShlAssign => return transCreateCompoundAssign(c, scope, stmt, .shl_assign, used), .ShrAssign => return transCreateCompoundAssign(c, scope, stmt, .shr_assign, used), .AndAssign => return transCreateCompoundAssign(c, scope, stmt, .bit_and_assign, used), .XorAssign => return transCreateCompoundAssign(c, scope, stmt, .bit_xor_assign, used), .OrAssign => return transCreateCompoundAssign(c, scope, stmt, .bit_or_assign, used), else => return fail( c, error.UnsupportedTranslation, stmt.getBeginLoc(), "unsupported C translation {}", .{stmt.getOpcode()}, ), } } fn transCreateCompoundAssign( c: *Context, scope: *Scope, stmt: *const clang.CompoundAssignOperator, op: Tag, used: ResultUsed, ) TransError!Node { const is_shift = op == .shl_assign or op == .shr_assign; const is_div = op == .div_assign; const is_mod = op == .mod_assign; const lhs = stmt.getLHS(); const rhs = stmt.getRHS(); const loc = stmt.getBeginLoc(); const lhs_qt = getExprQualType(c, lhs); const rhs_qt = getExprQualType(c, rhs); const is_signed = cIsSignedInteger(lhs_qt); const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt); const requires_int_cast = blk: { const are_integers = cIsInteger(lhs_qt) and cIsInteger(rhs_qt); const are_same_sign = cIsSignedInteger(lhs_qt) == cIsSignedInteger(rhs_qt); break :blk are_integers and !(are_same_sign and cIntTypeCmp(lhs_qt, rhs_qt) == .eq); }; if (used == .unused) { // common case // c: lhs += rhs // zig: lhs += rhs const lhs_node = try transExpr(c, scope, lhs, .used); var rhs_node = try transExpr(c, scope, rhs, .used); if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node); if ((is_mod or is_div) and is_signed) { if (requires_int_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); const operands = .{ .lhs = lhs_node, .rhs = rhs_node }; const builtin = if (is_mod) try Tag.rem.create(c.arena, operands) else try Tag.div_trunc.create(c.arena, operands); return transCreateNodeInfixOp(c, scope, .assign, lhs_node, builtin, .used); } if (is_shift) { const cast_to_type = try qualTypeToLog2IntRef(c, scope, rhs_qt, loc); rhs_node = try Tag.int_cast.create(c.arena, .{ .lhs = cast_to_type, .rhs = rhs_node }); } else if (requires_int_cast) { rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); } return transCreateNodeInfixOp(c, scope, op, lhs_node, rhs_node, .used); } // worst case // c: lhs += rhs // zig: (blk: { // zig: const _ref = &lhs; // zig: _ref.* += rhs; // zig: break :blk _ref.* // zig: }) var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const ref = try block_scope.makeMangledName(c, "ref"); const expr = try transExpr(c, &block_scope.base, lhs, .used); const addr_of = try Tag.address_of.create(c.arena, expr); const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = ref, .init = addr_of }); try block_scope.statements.append(ref_decl); const lhs_node = try Tag.identifier.create(c.arena, ref); const ref_node = try Tag.deref.create(c.arena, lhs_node); var rhs_node = try transExpr(c, &block_scope.base, rhs, .used); if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node); if ((is_mod or is_div) and is_signed) { if (requires_int_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); const operands = .{ .lhs = ref_node, .rhs = rhs_node }; const builtin = if (is_mod) try Tag.rem.create(c.arena, operands) else try Tag.div_trunc.create(c.arena, operands); const assign = try transCreateNodeInfixOp(c, &block_scope.base, .assign, ref_node, builtin, .used); try block_scope.statements.append(assign); } else { if (is_shift) { const cast_to_type = try qualTypeToLog2IntRef(c, &block_scope.base, rhs_qt, loc); rhs_node = try Tag.int_cast.create(c.arena, .{ .lhs = cast_to_type, .rhs = rhs_node }); } else if (requires_int_cast) { rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node); } const assign = try transCreateNodeInfixOp(c, &block_scope.base, op, ref_node, rhs_node, .used); try block_scope.statements.append(assign); } const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = ref_node, }); try block_scope.statements.append(break_node); return block_scope.complete(c); } fn transCPtrCast( c: *Context, scope: *Scope, loc: clang.SourceLocation, dst_type: clang.QualType, src_type: clang.QualType, expr: Node, ) !Node { const ty = dst_type.getTypePtr(); const child_type = ty.getPointeeType(); const src_ty = src_type.getTypePtr(); const src_child_type = src_ty.getPointeeType(); const dst_type_node = try transType(c, scope, ty, loc); if (!src_ty.isArrayType() and ((src_child_type.isConstQualified() and !child_type.isConstQualified()) or (src_child_type.isVolatileQualified() and !child_type.isVolatileQualified()))) { // Casting away const or volatile requires us to use @intToPtr const ptr_to_int = try Tag.ptr_to_int.create(c.arena, expr); const int_to_ptr = try Tag.int_to_ptr.create(c.arena, .{ .lhs = dst_type_node, .rhs = ptr_to_int }); return int_to_ptr; } else { // Implicit downcasting from higher to lower alignment values is forbidden, // use @alignCast to side-step this problem const rhs = if (qualTypeCanon(child_type).isVoidType()) // void has 1-byte alignment, so @alignCast is not needed expr else if (typeIsOpaque(c, qualTypeCanon(child_type), loc)) // For opaque types a ptrCast is enough expr else blk: { const child_type_node = try transQualType(c, scope, child_type, loc); const alignof = try Tag.std_meta_alignment.create(c.arena, child_type_node); const align_cast = try Tag.align_cast.create(c.arena, .{ .lhs = alignof, .rhs = expr }); break :blk align_cast; }; return Tag.ptr_cast.create(c.arena, .{ .lhs = dst_type_node, .rhs = rhs }); } } fn transFloatingLiteral(c: *Context, scope: *Scope, expr: *const clang.FloatingLiteral, used: ResultUsed) TransError!Node { switch (expr.getRawSemantics()) { .IEEEhalf, // f16 .IEEEsingle, // f32 .IEEEdouble, // f64 => {}, else => |format| return fail( c, error.UnsupportedTranslation, expr.getBeginLoc(), "unsupported floating point constant format {}", .{format}, ), } // TODO use something more accurate var dbl = expr.getValueAsApproximateDouble(); const is_negative = dbl < 0; if (is_negative) dbl = -dbl; const str = if (dbl == std.math.floor(dbl)) try std.fmt.allocPrint(c.arena, "{d}.0", .{dbl}) else try std.fmt.allocPrint(c.arena, "{d}", .{dbl}); var node = try Tag.float_literal.create(c.arena, str); if (is_negative) node = try Tag.negate.create(c.arena, node); return maybeSuppressResult(c, scope, used, node); } fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.BinaryConditionalOperator, used: ResultUsed) TransError!Node { // GNU extension of the ternary operator where the middle expression is // omitted, the conditition itself is returned if it evaluates to true const qt = @ptrCast(*const clang.Expr, stmt).getType(); const res_is_bool = qualTypeIsBoolean(qt); const casted_stmt = @ptrCast(*const clang.AbstractConditionalOperator, stmt); const cond_expr = casted_stmt.getCond(); const true_expr = casted_stmt.getTrueExpr(); const false_expr = casted_stmt.getFalseExpr(); // c: (cond_expr)?:(false_expr) // zig: (blk: { // const _cond_temp = (cond_expr); // break :blk if (_cond_temp) _cond_temp else (false_expr); // }) var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const mangled_name = try block_scope.makeMangledName(c, "cond_temp"); const init_node = try transExpr(c, &block_scope.base, cond_expr, .used); const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = mangled_name, .init = init_node }); try block_scope.statements.append(ref_decl); var cond_scope = Scope.Condition{ .base = .{ .parent = &block_scope.base, .id = .condition, }, }; defer cond_scope.deinit(); const cond_ident = try Tag.identifier.create(c.arena, mangled_name); const ty = getExprQualType(c, cond_expr).getTypePtr(); const cond_node = try finishBoolExpr(c, &cond_scope.base, cond_expr.getBeginLoc(), ty, cond_ident, .used); var then_body = cond_ident; if (!res_is_bool and isBoolRes(init_node)) { then_body = try Tag.bool_to_int.create(c.arena, then_body); } var else_body = try transExpr(c, &block_scope.base, false_expr, .used); if (!res_is_bool and isBoolRes(else_body)) { else_body = try Tag.bool_to_int.create(c.arena, else_body); } const if_node = try Tag.@"if".create(c.arena, .{ .cond = cond_node, .then = then_body, .@"else" = else_body, }); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = if_node, }); try block_scope.statements.append(break_node); const res = try block_scope.complete(c); return maybeSuppressResult(c, scope, used, res); } fn transConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang.ConditionalOperator, used: ResultUsed) TransError!Node { var cond_scope = Scope.Condition{ .base = .{ .parent = scope, .id = .condition, }, }; defer cond_scope.deinit(); const qt = @ptrCast(*const clang.Expr, stmt).getType(); const res_is_bool = qualTypeIsBoolean(qt); const casted_stmt = @ptrCast(*const clang.AbstractConditionalOperator, stmt); const cond_expr = casted_stmt.getCond(); const true_expr = casted_stmt.getTrueExpr(); const false_expr = casted_stmt.getFalseExpr(); const cond = try transBoolExpr(c, &cond_scope.base, cond_expr, .used); var then_body = try transExpr(c, scope, true_expr, used); if (!res_is_bool and isBoolRes(then_body)) { then_body = try Tag.bool_to_int.create(c.arena, then_body); } var else_body = try transExpr(c, scope, false_expr, used); if (!res_is_bool and isBoolRes(else_body)) { else_body = try Tag.bool_to_int.create(c.arena, else_body); } const if_node = try Tag.@"if".create(c.arena, .{ .cond = cond, .then = then_body, .@"else" = else_body, }); // Clang inserts ImplicitCast(ToVoid)'s to both rhs and lhs so we don't need to supress the result here. return if_node; } fn maybeSuppressResult( c: *Context, scope: *Scope, used: ResultUsed, result: Node, ) TransError!Node { if (used == .used) return result; return Tag.discard.create(c.arena, result); } fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: Node) !void { try c.global_scope.sym_table.put(name, decl_node); try c.global_scope.nodes.append(decl_node); } /// Translate a qualtype for a variable with an initializer. This only matters /// for incomplete arrays, since the initializer determines the size of the array. fn transQualTypeInitialized( c: *Context, scope: *Scope, qt: clang.QualType, decl_init: *const clang.Expr, source_loc: clang.SourceLocation, ) TypeError!Node { const ty = qt.getTypePtr(); if (ty.getTypeClass() == .IncompleteArray) { const incomplete_array_ty = @ptrCast(*const clang.IncompleteArrayType, ty); const elem_ty = try transType(c, scope, incomplete_array_ty.getElementType().getTypePtr(), source_loc); switch (decl_init.getStmtClass()) { .StringLiteralClass => { const string_lit = @ptrCast(*const clang.StringLiteral, decl_init); const string_lit_size = string_lit.getLength(); const array_size = @intCast(usize, string_lit_size); // incomplete array initialized with empty string, will be translated as [1]T{0} // see https://github.com/ziglang/zig/issues/8256 if (array_size == 0) return Tag.array_type.create(c.arena, .{ .len = 1, .elem_type = elem_ty }); return Tag.null_sentinel_array_type.create(c.arena, .{ .len = array_size, .elem_type = elem_ty }); }, .InitListExprClass => { const init_expr = @ptrCast(*const clang.InitListExpr, decl_init); const size = init_expr.getNumInits(); return Tag.array_type.create(c.arena, .{ .len = size, .elem_type = elem_ty }); }, else => {}, } } return transQualType(c, scope, qt, source_loc); } fn transQualType(c: *Context, scope: *Scope, qt: clang.QualType, source_loc: clang.SourceLocation) TypeError!Node { return transType(c, scope, qt.getTypePtr(), source_loc); } /// Produces a Zig AST node by translating a Clang QualType, respecting the width, but modifying the signed-ness. /// Asserts the type is an integer. fn transQualTypeIntWidthOf(c: *Context, ty: clang.QualType, is_signed: bool) TypeError!Node { return transTypeIntWidthOf(c, qualTypeCanon(ty), is_signed); } /// Produces a Zig AST node by translating a Clang Type, respecting the width, but modifying the signed-ness. /// Asserts the type is an integer. fn transTypeIntWidthOf(c: *Context, ty: *const clang.Type, is_signed: bool) TypeError!Node { assert(ty.getTypeClass() == .Builtin); const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); return Tag.type.create(c.arena, switch (builtin_ty.getKind()) { .Char_U, .Char_S, .UChar, .SChar, .Char8 => if (is_signed) "i8" else "u8", .UShort, .Short => if (is_signed) "c_short" else "c_ushort", .UInt, .Int => if (is_signed) "c_int" else "c_uint", .ULong, .Long => if (is_signed) "c_long" else "c_ulong", .ULongLong, .LongLong => if (is_signed) "c_longlong" else "c_ulonglong", .UInt128, .Int128 => if (is_signed) "i128" else "u128", .Char16 => if (is_signed) "i16" else "u16", .Char32 => if (is_signed) "i32" else "u32", else => unreachable, // only call this function when it has already been determined the type is int }); } fn isCBuiltinType(qt: clang.QualType, kind: clang.BuiltinTypeKind) bool { const c_type = qualTypeCanon(qt); if (c_type.getTypeClass() != .Builtin) return false; const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return builtin_ty.getKind() == kind; } fn qualTypeIsPtr(qt: clang.QualType) bool { return qualTypeCanon(qt).getTypeClass() == .Pointer; } fn qualTypeIsBoolean(qt: clang.QualType) bool { return qualTypeCanon(qt).isBooleanType(); } fn qualTypeIntBitWidth(c: *Context, qt: clang.QualType) !u32 { const ty = qt.getTypePtr(); switch (ty.getTypeClass()) { .Builtin => { const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); switch (builtin_ty.getKind()) { .Char_U, .UChar, .Char_S, .SChar, => return 8, .UInt128, .Int128, => return 128, else => return 0, } unreachable; }, .Typedef => { const typedef_ty = @ptrCast(*const clang.TypedefType, ty); const typedef_decl = typedef_ty.getDecl(); const type_name = try c.str(@ptrCast(*const clang.NamedDecl, typedef_decl).getName_bytes_begin()); if (mem.eql(u8, type_name, "uint8_t") or mem.eql(u8, type_name, "int8_t")) { return 8; } else if (mem.eql(u8, type_name, "uint16_t") or mem.eql(u8, type_name, "int16_t")) { return 16; } else if (mem.eql(u8, type_name, "uint32_t") or mem.eql(u8, type_name, "int32_t")) { return 32; } else if (mem.eql(u8, type_name, "uint64_t") or mem.eql(u8, type_name, "int64_t")) { return 64; } else { return 0; } }, else => return 0, } } fn qualTypeToLog2IntRef(c: *Context, scope: *Scope, qt: clang.QualType, source_loc: clang.SourceLocation) !Node { const int_bit_width = try qualTypeIntBitWidth(c, qt); if (int_bit_width != 0) { // we can perform the log2 now. const cast_bit_width = math.log2_int(u64, int_bit_width); return Tag.log2_int_type.create(c.arena, cast_bit_width); } const zig_type = try transQualType(c, scope, qt, source_loc); return Tag.std_math_Log2Int.create(c.arena, zig_type); } fn qualTypeChildIsFnProto(qt: clang.QualType) bool { const ty = qualTypeCanon(qt); switch (ty.getTypeClass()) { .FunctionProto, .FunctionNoProto => return true, else => return false, } } fn qualTypeCanon(qt: clang.QualType) *const clang.Type { const canon = qt.getCanonicalType(); return canon.getTypePtr(); } fn getExprQualType(c: *Context, expr: *const clang.Expr) clang.QualType { blk: { // If this is a C `char *`, turn it into a `const char *` if (expr.getStmtClass() != .ImplicitCastExprClass) break :blk; const cast_expr = @ptrCast(*const clang.ImplicitCastExpr, expr); if (cast_expr.getCastKind() != .ArrayToPointerDecay) break :blk; const sub_expr = cast_expr.getSubExpr(); if (sub_expr.getStmtClass() != .StringLiteralClass) break :blk; const array_qt = sub_expr.getType(); const array_type = @ptrCast(*const clang.ArrayType, array_qt.getTypePtr()); var pointee_qt = array_type.getElementType(); pointee_qt.addConst(); return c.clang_context.getPointerType(pointee_qt); } return expr.getType(); } fn typeIsOpaque(c: *Context, ty: *const clang.Type, loc: clang.SourceLocation) bool { switch (ty.getTypeClass()) { .Builtin => { const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); return builtin_ty.getKind() == .Void; }, .Record => { const record_ty = @ptrCast(*const clang.RecordType, ty); const record_decl = record_ty.getDecl(); const record_def = record_decl.getDefinition() orelse return true; var it = record_def.field_begin(); const end_it = record_def.field_end(); while (it.neq(end_it)) : (it = it.next()) { const field_decl = it.deref(); if (field_decl.isBitField()) { return true; } } return false; }, .Elaborated => { const elaborated_ty = @ptrCast(*const clang.ElaboratedType, ty); const qt = elaborated_ty.getNamedType(); return typeIsOpaque(c, qt.getTypePtr(), loc); }, .Typedef => { const typedef_ty = @ptrCast(*const clang.TypedefType, ty); const typedef_decl = typedef_ty.getDecl(); const underlying_type = typedef_decl.getUnderlyingType(); return typeIsOpaque(c, underlying_type.getTypePtr(), loc); }, else => return false, } } fn cIsInteger(qt: clang.QualType) bool { return cIsSignedInteger(qt) or cIsUnsignedInteger(qt); } fn cIsUnsignedInteger(qt: clang.QualType) bool { const c_type = qualTypeCanon(qt); if (c_type.getTypeClass() != .Builtin) return false; const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return switch (builtin_ty.getKind()) { .Char_U, .UChar, .Char_S, .UShort, .UInt, .ULong, .ULongLong, .UInt128, .WChar_U, => true, else => false, }; } fn cIntTypeToIndex(qt: clang.QualType) u8 { const c_type = qualTypeCanon(qt); assert(c_type.getTypeClass() == .Builtin); const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return switch (builtin_ty.getKind()) { .Bool, .Char_U, .Char_S, .UChar, .SChar, .Char8 => 1, .WChar_U, .WChar_S => 2, .UShort, .Short, .Char16 => 3, .UInt, .Int, .Char32 => 4, .ULong, .Long => 5, .ULongLong, .LongLong => 6, .UInt128, .Int128 => 7, else => unreachable, }; } fn cIntTypeCmp(a: clang.QualType, b: clang.QualType) math.Order { const a_index = cIntTypeToIndex(a); const b_index = cIntTypeToIndex(b); return math.order(a_index, b_index); } fn cIsSignedInteger(qt: clang.QualType) bool { const c_type = qualTypeCanon(qt); if (c_type.getTypeClass() != .Builtin) return false; const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return switch (builtin_ty.getKind()) { .SChar, .Short, .Int, .Long, .LongLong, .Int128, .WChar_S, => true, else => false, }; } fn cIsNativeInt(qt: clang.QualType) bool { const c_type = qualTypeCanon(qt); if (c_type.getTypeClass() != .Builtin) return false; const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return builtin_ty.getKind() == .Int; } fn cIsFloating(qt: clang.QualType) bool { const c_type = qualTypeCanon(qt); if (c_type.getTypeClass() != .Builtin) return false; const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return switch (builtin_ty.getKind()) { .Float, .Double, .Float128, .LongDouble, => true, else => false, }; } fn cIsLongLongInteger(qt: clang.QualType) bool { const c_type = qualTypeCanon(qt); if (c_type.getTypeClass() != .Builtin) return false; const builtin_ty = @ptrCast(*const clang.BuiltinType, c_type); return switch (builtin_ty.getKind()) { .LongLong, .ULongLong, .Int128, .UInt128 => true, else => false, }; } fn transCreateNodeAssign( c: *Context, scope: *Scope, result_used: ResultUsed, lhs: *const clang.Expr, rhs: *const clang.Expr, ) !Node { // common case // c: lhs = rhs // zig: lhs = rhs if (result_used == .unused) { const lhs_node = try transExpr(c, scope, lhs, .used); var rhs_node = try transExprCoercing(c, scope, rhs, .used); if (!exprIsBooleanType(lhs) and isBoolRes(rhs_node)) { rhs_node = try Tag.bool_to_int.create(c.arena, rhs_node); } return transCreateNodeInfixOp(c, scope, .assign, lhs_node, rhs_node, .used); } // worst case // c: lhs = rhs // zig: (blk: { // zig: const _tmp = rhs; // zig: lhs = _tmp; // zig: break :blk _tmp // zig: }) var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); const tmp = try block_scope.makeMangledName(c, "tmp"); const rhs_node = try transExpr(c, &block_scope.base, rhs, .used); const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = rhs_node }); try block_scope.statements.append(tmp_decl); const lhs_node = try transExpr(c, &block_scope.base, lhs, .used); const tmp_ident = try Tag.identifier.create(c.arena, tmp); const assign = try transCreateNodeInfixOp(c, &block_scope.base, .assign, lhs_node, tmp_ident, .used); try block_scope.statements.append(assign); const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = tmp_ident, }); try block_scope.statements.append(break_node); return block_scope.complete(c); } fn transCreateNodeInfixOp( c: *Context, scope: *Scope, op: Tag, lhs: Node, rhs: Node, used: ResultUsed, ) !Node { const payload = try c.arena.create(ast.Payload.BinOp); payload.* = .{ .base = .{ .tag = op }, .data = .{ .lhs = lhs, .rhs = rhs, }, }; return maybeSuppressResult(c, scope, used, Node.initPayload(&payload.base)); } fn transCreateNodeBoolInfixOp( c: *Context, scope: *Scope, stmt: *const clang.BinaryOperator, op: Tag, used: ResultUsed, ) !Node { std.debug.assert(op == .@"and" or op == .@"or"); const lhs = try transBoolExpr(c, scope, stmt.getLHS(), .used); const rhs = try transBoolExpr(c, scope, stmt.getRHS(), .used); return transCreateNodeInfixOp(c, scope, op, lhs, rhs, used); } fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node { const num_limbs = math.cast(usize, int.getNumWords()) catch |err| switch (err) { error.Overflow => return error.OutOfMemory, }; var aps_int = int; const is_negative = int.isSigned() and int.isNegative(); if (is_negative) aps_int = aps_int.negate(); defer if (is_negative) { aps_int.free(); }; const limbs = try c.arena.alloc(math.big.Limb, num_limbs); defer c.arena.free(limbs); const data = aps_int.getRawData(); switch (@sizeOf(math.big.Limb)) { 8 => { var i: usize = 0; while (i < num_limbs) : (i += 1) { limbs[i] = data[i]; } }, 4 => { var limb_i: usize = 0; var data_i: usize = 0; while (limb_i < num_limbs) : ({ limb_i += 2; data_i += 1; }) { limbs[limb_i] = @truncate(u32, data[data_i]); limbs[limb_i + 1] = @truncate(u32, data[data_i] >> 32); } }, else => @compileError("unimplemented"), } const big: math.big.int.Const = .{ .limbs = limbs, .positive = true }; const str = big.toStringAlloc(c.arena, 10, .lower) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, }; const res = try Tag.integer_literal.create(c.arena, str); if (is_negative) return Tag.negate.create(c.arena, res); return res; } fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node { const fmt_s = if (comptime meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}"; const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num}); if (num_kind == .float) return Tag.float_literal.create(c.arena, str) else return Tag.integer_literal.create(c.arena, str); } fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: Node, proto_alias: *ast.Payload.Func) !Node { const scope = &c.global_scope.base; var fn_params = std.ArrayList(ast.Payload.Param).init(c.gpa); defer fn_params.deinit(); for (proto_alias.data.params) |param, i| { const param_name = param.name orelse try std.fmt.allocPrint(c.arena, "arg_{d}", .{c.getMangle()}); try fn_params.append(.{ .name = param_name, .type = param.type, .is_noalias = param.is_noalias, }); } const init = if (ref.castTag(.var_decl)) |v| v.data.init.? else if (ref.castTag(.var_simple) orelse ref.castTag(.pub_var_simple)) |v| v.data.init else unreachable; const unwrap_expr = try Tag.unwrap.create(c.arena, init); const args = try c.arena.alloc(Node, fn_params.items.len); for (fn_params.items) |param, i| { args[i] = try Tag.identifier.create(c.arena, param.name.?); } const call_expr = try Tag.call.create(c.arena, .{ .lhs = unwrap_expr, .args = args, }); const return_expr = try Tag.@"return".create(c.arena, call_expr); const block = try Tag.block_single.create(c.arena, return_expr); return Tag.pub_inline_fn.create(c.arena, .{ .name = name, .params = try c.arena.dupe(ast.Payload.Param, fn_params.items), .return_type = proto_alias.data.return_type, .body = block, }); } fn transCreateNodeShiftOp( c: *Context, scope: *Scope, stmt: *const clang.BinaryOperator, op: Tag, used: ResultUsed, ) !Node { std.debug.assert(op == .shl or op == .shr); const lhs_expr = stmt.getLHS(); const rhs_expr = stmt.getRHS(); const rhs_location = rhs_expr.getBeginLoc(); // lhs >> @as(u5, rh) const lhs = try transExpr(c, scope, lhs_expr, .used); const rhs_type = try qualTypeToLog2IntRef(c, scope, stmt.getType(), rhs_location); const rhs = try transExprCoercing(c, scope, rhs_expr, .used); const rhs_casted = try Tag.int_cast.create(c.arena, .{ .lhs = rhs_type, .rhs = rhs }); return transCreateNodeInfixOp(c, scope, op, lhs, rhs_casted, used); } fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clang.SourceLocation) TypeError!Node { switch (ty.getTypeClass()) { .Builtin => { const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); return Tag.type.create(c.arena, switch (builtin_ty.getKind()) { .Void => "c_void", .Bool => "bool", .Char_U, .UChar, .Char_S, .Char8 => "u8", .SChar => "i8", .UShort => "c_ushort", .UInt => "c_uint", .ULong => "c_ulong", .ULongLong => "c_ulonglong", .Short => "c_short", .Int => "c_int", .Long => "c_long", .LongLong => "c_longlong", .UInt128 => "u128", .Int128 => "i128", .Float => "f32", .Double => "f64", .Float128 => "f128", .Float16 => "f16", .LongDouble => "c_longdouble", else => return fail(c, error.UnsupportedType, source_loc, "unsupported builtin type", .{}), }); }, .FunctionProto => { const fn_proto_ty = @ptrCast(*const clang.FunctionProtoType, ty); const fn_proto = try transFnProto(c, null, fn_proto_ty, source_loc, null, false); return Node.initPayload(&fn_proto.base); }, .FunctionNoProto => { const fn_no_proto_ty = @ptrCast(*const clang.FunctionType, ty); const fn_proto = try transFnNoProto(c, fn_no_proto_ty, source_loc, null, false); return Node.initPayload(&fn_proto.base); }, .Paren => { const paren_ty = @ptrCast(*const clang.ParenType, ty); return transQualType(c, scope, paren_ty.getInnerType(), source_loc); }, .Pointer => { const child_qt = ty.getPointeeType(); if (qualTypeChildIsFnProto(child_qt)) { return Tag.optional_type.create(c.arena, try transQualType(c, scope, child_qt, source_loc)); } const is_const = child_qt.isConstQualified(); const is_volatile = child_qt.isVolatileQualified(); const elem_type = try transQualType(c, scope, child_qt, source_loc); if (typeIsOpaque(c, child_qt.getTypePtr(), source_loc) or qualTypeWasDemotedToOpaque(c, child_qt)) { const ptr = try Tag.single_pointer.create(c.arena, .{ .is_const = is_const, .is_volatile = is_volatile, .elem_type = elem_type }); return Tag.optional_type.create(c.arena, ptr); } return Tag.c_pointer.create(c.arena, .{ .is_const = is_const, .is_volatile = is_volatile, .elem_type = elem_type }); }, .ConstantArray => { const const_arr_ty = @ptrCast(*const clang.ConstantArrayType, ty); const size_ap_int = const_arr_ty.getSize(); const size = size_ap_int.getLimitedValue(usize); const elem_type = try transType(c, scope, const_arr_ty.getElementType().getTypePtr(), source_loc); return Tag.array_type.create(c.arena, .{ .len = size, .elem_type = elem_type }); }, .IncompleteArray => { const incomplete_array_ty = @ptrCast(*const clang.IncompleteArrayType, ty); const child_qt = incomplete_array_ty.getElementType(); const is_const = child_qt.isConstQualified(); const is_volatile = child_qt.isVolatileQualified(); const elem_type = try transQualType(c, scope, child_qt, source_loc); return Tag.c_pointer.create(c.arena, .{ .is_const = is_const, .is_volatile = is_volatile, .elem_type = elem_type }); }, .Typedef => { const typedef_ty = @ptrCast(*const clang.TypedefType, ty); const typedef_decl = typedef_ty.getDecl(); var trans_scope = scope; if (@ptrCast(*const clang.Decl, typedef_decl).castToNamedDecl()) |named_decl| { const decl_name = try c.str(named_decl.getName_bytes_begin()); if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base; } try transTypeDef(c, trans_scope, typedef_decl); const name = c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl())).?; return Tag.identifier.create(c.arena, name); }, .Record => { const record_ty = @ptrCast(*const clang.RecordType, ty); const record_decl = record_ty.getDecl(); var trans_scope = scope; if (@ptrCast(*const clang.Decl, record_decl).castToNamedDecl()) |named_decl| { const decl_name = try c.str(named_decl.getName_bytes_begin()); if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base; } try transRecordDecl(c, trans_scope, record_decl); const name = c.decl_table.get(@ptrToInt(record_decl.getCanonicalDecl())).?; return Tag.identifier.create(c.arena, name); }, .Enum => { const enum_ty = @ptrCast(*const clang.EnumType, ty); const enum_decl = enum_ty.getDecl(); var trans_scope = scope; if (@ptrCast(*const clang.Decl, enum_decl).castToNamedDecl()) |named_decl| { const decl_name = try c.str(named_decl.getName_bytes_begin()); if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base; } try transEnumDecl(c, trans_scope, enum_decl); const name = c.decl_table.get(@ptrToInt(enum_decl.getCanonicalDecl())).?; return Tag.identifier.create(c.arena, name); }, .Elaborated => { const elaborated_ty = @ptrCast(*const clang.ElaboratedType, ty); return transQualType(c, scope, elaborated_ty.getNamedType(), source_loc); }, .Decayed => { const decayed_ty = @ptrCast(*const clang.DecayedType, ty); return transQualType(c, scope, decayed_ty.getDecayedType(), source_loc); }, .Attributed => { const attributed_ty = @ptrCast(*const clang.AttributedType, ty); return transQualType(c, scope, attributed_ty.getEquivalentType(), source_loc); }, .MacroQualified => { const macroqualified_ty = @ptrCast(*const clang.MacroQualifiedType, ty); return transQualType(c, scope, macroqualified_ty.getModifiedType(), source_loc); }, .TypeOf => { const typeof_ty = @ptrCast(*const clang.TypeOfType, ty); return transQualType(c, scope, typeof_ty.getUnderlyingType(), source_loc); }, .TypeOfExpr => { const typeofexpr_ty = @ptrCast(*const clang.TypeOfExprType, ty); const underlying_expr = transExpr(c, scope, typeofexpr_ty.getUnderlyingExpr(), .used) catch |err| switch (err) { error.UnsupportedTranslation => { return fail(c, error.UnsupportedType, source_loc, "unsupported underlying expression for TypeOfExpr", .{}); }, else => |e| return e, }; return Tag.typeof.create(c.arena, underlying_expr); }, .Vector => { const vector_ty = @ptrCast(*const clang.VectorType, ty); const num_elements = vector_ty.getNumElements(); const element_qt = vector_ty.getElementType(); return Tag.std_meta_vector.create(c.arena, .{ .lhs = try transCreateNodeNumber(c, num_elements, .int), .rhs = try transQualType(c, scope, element_qt, source_loc), }); }, else => { const type_name = c.str(ty.getTypeClassName()); return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{s}'", .{type_name}); }, } } fn qualTypeWasDemotedToOpaque(c: *Context, qt: clang.QualType) bool { const ty = qt.getTypePtr(); switch (qt.getTypeClass()) { .Typedef => { const typedef_ty = @ptrCast(*const clang.TypedefType, ty); const typedef_decl = typedef_ty.getDecl(); const underlying_type = typedef_decl.getUnderlyingType(); return qualTypeWasDemotedToOpaque(c, underlying_type); }, .Record => { const record_ty = @ptrCast(*const clang.RecordType, ty); const record_decl = record_ty.getDecl(); const canonical = @ptrToInt(record_decl.getCanonicalDecl()); return c.opaque_demotes.contains(canonical); }, .Enum => { const enum_ty = @ptrCast(*const clang.EnumType, ty); const enum_decl = enum_ty.getDecl(); const canonical = @ptrToInt(enum_decl.getCanonicalDecl()); return c.opaque_demotes.contains(canonical); }, .Elaborated => { const elaborated_ty = @ptrCast(*const clang.ElaboratedType, ty); return qualTypeWasDemotedToOpaque(c, elaborated_ty.getNamedType()); }, .Decayed => { const decayed_ty = @ptrCast(*const clang.DecayedType, ty); return qualTypeWasDemotedToOpaque(c, decayed_ty.getDecayedType()); }, .Attributed => { const attributed_ty = @ptrCast(*const clang.AttributedType, ty); return qualTypeWasDemotedToOpaque(c, attributed_ty.getEquivalentType()); }, .MacroQualified => { const macroqualified_ty = @ptrCast(*const clang.MacroQualifiedType, ty); return qualTypeWasDemotedToOpaque(c, macroqualified_ty.getModifiedType()); }, else => return false, } } fn isCVoid(qt: clang.QualType) bool { const ty = qt.getTypePtr(); if (ty.getTypeClass() == .Builtin) { const builtin_ty = @ptrCast(*const clang.BuiltinType, ty); return builtin_ty.getKind() == .Void; } return false; } const FnDeclContext = struct { fn_name: []const u8, has_body: bool, storage_class: clang.StorageClass, is_export: bool, }; fn transCC( c: *Context, fn_ty: *const clang.FunctionType, source_loc: clang.SourceLocation, ) !CallingConvention { const clang_cc = fn_ty.getCallConv(); switch (clang_cc) { .C => return CallingConvention.C, .X86StdCall => return CallingConvention.Stdcall, .X86FastCall => return CallingConvention.Fastcall, .X86VectorCall, .AArch64VectorCall => return CallingConvention.Vectorcall, .X86ThisCall => return CallingConvention.Thiscall, .AAPCS => return CallingConvention.AAPCS, .AAPCS_VFP => return CallingConvention.AAPCSVFP, .X86_64SysV => return CallingConvention.SysV, else => return fail( c, error.UnsupportedType, source_loc, "unsupported calling convention: {s}", .{@tagName(clang_cc)}, ), } } fn transFnProto( c: *Context, fn_decl: ?*const clang.FunctionDecl, fn_proto_ty: *const clang.FunctionProtoType, source_loc: clang.SourceLocation, fn_decl_context: ?FnDeclContext, is_pub: bool, ) !*ast.Payload.Func { const fn_ty = @ptrCast(*const clang.FunctionType, fn_proto_ty); const cc = try transCC(c, fn_ty, source_loc); const is_var_args = fn_proto_ty.isVariadic(); return finishTransFnProto(c, fn_decl, fn_proto_ty, fn_ty, source_loc, fn_decl_context, is_var_args, cc, is_pub); } fn transFnNoProto( c: *Context, fn_ty: *const clang.FunctionType, source_loc: clang.SourceLocation, fn_decl_context: ?FnDeclContext, is_pub: bool, ) !*ast.Payload.Func { const cc = try transCC(c, fn_ty, source_loc); const is_var_args = if (fn_decl_context) |ctx| (!ctx.is_export and ctx.storage_class != .Static) else true; return finishTransFnProto(c, null, null, fn_ty, source_loc, fn_decl_context, is_var_args, cc, is_pub); } fn finishTransFnProto( c: *Context, fn_decl: ?*const clang.FunctionDecl, fn_proto_ty: ?*const clang.FunctionProtoType, fn_ty: *const clang.FunctionType, source_loc: clang.SourceLocation, fn_decl_context: ?FnDeclContext, is_var_args: bool, cc: CallingConvention, is_pub: bool, ) !*ast.Payload.Func { const is_export = if (fn_decl_context) |ctx| ctx.is_export else false; const is_extern = if (fn_decl_context) |ctx| !ctx.has_body else false; const scope = &c.global_scope.base; // TODO check for always_inline attribute // TODO check for align attribute var fn_params = std.ArrayList(ast.Payload.Param).init(c.gpa); defer fn_params.deinit(); const param_count: usize = if (fn_proto_ty != null) fn_proto_ty.?.getNumParams() else 0; try fn_params.ensureCapacity(param_count); var i: usize = 0; while (i < param_count) : (i += 1) { const param_qt = fn_proto_ty.?.getParamType(@intCast(c_uint, i)); const is_noalias = param_qt.isRestrictQualified(); const param_name: ?[]const u8 = if (fn_decl) |decl| blk: { const param = decl.getParamDecl(@intCast(c_uint, i)); const param_name: []const u8 = try c.str(@ptrCast(*const clang.NamedDecl, param).getName_bytes_begin()); if (param_name.len < 1) break :blk null; break :blk param_name; } else null; const type_node = try transQualType(c, scope, param_qt, source_loc); fn_params.addOneAssumeCapacity().* = .{ .is_noalias = is_noalias, .name = param_name, .type = type_node, }; } const linksection_string = blk: { if (fn_decl) |decl| { var str_len: usize = undefined; if (decl.getSectionAttribute(&str_len)) |str_ptr| { break :blk str_ptr[0..str_len]; } } break :blk null; }; const alignment = if (fn_decl) |decl| zigAlignment(decl.getAlignedAttribute(c.clang_context)) else null; const explicit_callconv = if ((is_export or is_extern) and cc == .C) null else cc; const return_type_node = blk: { if (fn_ty.getNoReturnAttr()) { break :blk Tag.noreturn_type.init(); } else { const return_qt = fn_ty.getReturnType(); if (isCVoid(return_qt)) { // convert primitive c_void to actual void (only for return type) break :blk Tag.void_type.init(); } else { break :blk transQualType(c, scope, return_qt, source_loc) catch |err| switch (err) { error.UnsupportedType => { try warn(c, scope, source_loc, "unsupported function proto return type", .{}); return err; }, error.OutOfMemory => |e| return e, }; } } }; const name: ?[]const u8 = if (fn_decl_context) |ctx| ctx.fn_name else null; const payload = try c.arena.create(ast.Payload.Func); payload.* = .{ .base = .{ .tag = .func }, .data = .{ .is_pub = is_pub, .is_extern = is_extern, .is_export = is_export, .is_var_args = is_var_args, .name = name, .linksection_string = linksection_string, .explicit_callconv = explicit_callconv, .params = try c.arena.dupe(ast.Payload.Param, fn_params.items), .return_type = return_type_node, .body = null, .alignment = alignment, }, }; return payload; } fn warn(c: *Context, scope: *Scope, loc: clang.SourceLocation, comptime format: []const u8, args: anytype) !void { const args_prefix = .{c.locStr(loc)}; const value = try std.fmt.allocPrint(c.arena, "// {s}: warning: " ++ format, args_prefix ++ args); try scope.appendNode(try Tag.warning.create(c.arena, value)); } fn fail( c: *Context, err: anytype, source_loc: clang.SourceLocation, comptime format: []const u8, args: anytype, ) (@TypeOf(err) || error{OutOfMemory}) { try warn(c, &c.global_scope.base, source_loc, format, args); return err; } pub fn failDecl(c: *Context, loc: clang.SourceLocation, name: []const u8, comptime format: []const u8, args: anytype) Error!void { // location // pub const name = @compileError(msg); const fail_msg = try std.fmt.allocPrint(c.arena, format, args); try addTopLevelDecl(c, name, try Tag.fail_decl.create(c.arena, .{ .actual = name, .mangled = fail_msg })); const location_comment = try std.fmt.allocPrint(c.arena, "// {s}", .{c.locStr(loc)}); try c.global_scope.nodes.append(try Tag.warning.create(c.arena, location_comment)); } pub fn freeErrors(errors: []ClangErrMsg) void { errors.ptr.delete(errors.len); } fn isZigPrimitiveType(name: []const u8) bool { if (name.len > 1 and (name[0] == 'u' or name[0] == 'i')) { for (name[1..]) |c| { switch (c) { '0'...'9' => {}, else => return false, } } return true; } return @import("AstGen.zig").simple_types.has(name); } const MacroCtx = struct { source: []const u8, list: []const CToken, i: usize = 0, loc: clang.SourceLocation, name: []const u8, fn peek(self: *MacroCtx) ?CToken.Id { if (self.i >= self.list.len) return null; return self.list[self.i + 1].id; } fn next(self: *MacroCtx) ?CToken.Id { if (self.i >= self.list.len) return null; self.i += 1; return self.list[self.i].id; } fn slice(self: *MacroCtx) []const u8 { const tok = self.list[self.i]; return self.source[tok.start..tok.end]; } fn fail(self: *MacroCtx, c: *Context, comptime fmt: []const u8, args: anytype) !void { return failDecl(c, self.loc, self.name, fmt, args); } }; fn transPreprocessorEntities(c: *Context, unit: *clang.ASTUnit) Error!void { // TODO if we see #undef, delete it from the table var it = unit.getLocalPreprocessingEntities_begin(); const it_end = unit.getLocalPreprocessingEntities_end(); var tok_list = std.ArrayList(CToken).init(c.gpa); defer tok_list.deinit(); const scope = c.global_scope; while (it.I != it_end.I) : (it.I += 1) { const entity = it.deref(); tok_list.items.len = 0; switch (entity.getKind()) { .MacroDefinitionKind => { const macro = @ptrCast(*clang.MacroDefinitionRecord, entity); const raw_name = macro.getName_getNameStart(); const begin_loc = macro.getSourceRange_getBegin(); const end_loc = clang.Lexer.getLocForEndOfToken(macro.getSourceRange_getEnd(), c.source_manager, unit); const name = try c.str(raw_name); // TODO https://github.com/ziglang/zig/issues/3756 // TODO https://github.com/ziglang/zig/issues/1802 const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ name, c.getMangle() }) else name; if (scope.containsNow(mangled_name)) { continue; } const begin_c = c.source_manager.getCharacterData(begin_loc); const end_c = c.source_manager.getCharacterData(end_loc); const slice_len = @ptrToInt(end_c) - @ptrToInt(begin_c); const slice = begin_c[0..slice_len]; var tokenizer = std.c.Tokenizer{ .buffer = slice, }; while (true) { const tok = tokenizer.next(); switch (tok.id) { .Nl, .Eof => { try tok_list.append(tok); break; }, .LineComment, .MultiLineComment => continue, else => {}, } try tok_list.append(tok); } var macro_ctx = MacroCtx{ .source = slice, .list = tok_list.items, .name = mangled_name, .loc = begin_loc, }; assert(mem.eql(u8, macro_ctx.slice(), name)); var macro_fn = false; switch (macro_ctx.peek().?) { .Identifier => { // if it equals itself, ignore. for example, from stdio.h: // #define stdin stdin const tok = macro_ctx.list[1]; if (mem.eql(u8, name, slice[tok.start..tok.end])) { continue; } }, .Nl, .Eof => { // this means it is a macro without a value // we don't care about such things continue; }, .LParen => { // if the name is immediately followed by a '(' then it is a function macro_fn = macro_ctx.list[0].end == macro_ctx.list[1].start; }, else => {}, } (if (macro_fn) transMacroFnDefine(c, &macro_ctx) else transMacroDefine(c, &macro_ctx)) catch |err| switch (err) { error.ParseError => continue, error.OutOfMemory => |e| return e, }; }, else => {}, } } } fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void { const scope = &c.global_scope.base; const init_node = try parseCExpr(c, m, scope); const last = m.next().?; if (last != .Eof and last != .Nl) return m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(last)}); const var_decl = try Tag.pub_var_simple.create(c.arena, .{ .name = m.name, .init = init_node }); try c.global_scope.macro_table.put(m.name, var_decl); } fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void { var block_scope = try Scope.Block.init(c, &c.global_scope.base, false); defer block_scope.deinit(); const scope = &block_scope.base; if (m.next().? != .LParen) { return m.fail(c, "unable to translate C expr: expected '('", .{}); } var fn_params = std.ArrayList(ast.Payload.Param).init(c.gpa); defer fn_params.deinit(); while (true) { if (m.peek().? != .Identifier) break; _ = m.next(); const mangled_name = try block_scope.makeMangledName(c, m.slice()); try fn_params.append(.{ .is_noalias = false, .name = mangled_name, .type = Tag.@"anytype".init(), }); if (m.peek().? != .Comma) break; _ = m.next(); } if (m.next().? != .RParen) { return m.fail(c, "unable to translate C expr: expected ')'", .{}); } const expr = try parseCExpr(c, m, scope); const last = m.next().?; if (last != .Eof and last != .Nl) return m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(last)}); const typeof_arg = if (expr.castTag(.block)) |some| blk: { const stmts = some.data.stmts; const blk_last = stmts[stmts.len - 1]; const br = blk_last.castTag(.break_val).?; break :blk br.data.val; } else expr; const return_type = if (typeof_arg.castTag(.std_meta_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some| some.data.lhs else if (typeof_arg.castTag(.std_mem_zeroes)) |some| some.data else try Tag.typeof.create(c.arena, typeof_arg); const return_expr = try Tag.@"return".create(c.arena, expr); try block_scope.statements.append(return_expr); const fn_decl = try Tag.pub_inline_fn.create(c.arena, .{ .name = m.name, .params = try c.arena.dupe(ast.Payload.Param, fn_params.items), .return_type = return_type, .body = try block_scope.complete(c), }); try c.global_scope.macro_table.put(m.name, fn_decl); } const ParseError = Error || error{ParseError}; fn parseCExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { // TODO parseCAssignExpr here const node = try parseCCondExpr(c, m, scope); if (m.next().? != .Comma) { m.i -= 1; return node; } var block_scope = try Scope.Block.init(c, scope, true); defer block_scope.deinit(); var last = node; while (true) { // suppress result const ignore = try Tag.discard.create(c.arena, last); try block_scope.statements.append(ignore); last = try parseCCondExpr(c, m, scope); if (m.next().? != .Comma) { m.i -= 1; break; } } const break_node = try Tag.break_val.create(c.arena, .{ .label = block_scope.label, .val = last, }); try block_scope.statements.append(break_node); return try block_scope.complete(c); } fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { var lit_bytes = m.slice(); switch (m.list[m.i].id) { .IntegerLiteral => |suffix| { var radix: []const u8 = "decimal"; if (lit_bytes.len > 2 and lit_bytes[0] == '0') { switch (lit_bytes[1]) { '0'...'7' => { // Octal lit_bytes = try std.fmt.allocPrint(c.arena, "0o{s}", .{lit_bytes[1..]}); radix = "octal"; }, 'X' => { // Hexadecimal with capital X, valid in C but not in Zig lit_bytes = try std.fmt.allocPrint(c.arena, "0x{s}", .{lit_bytes[2..]}); radix = "hexadecimal"; }, 'x' => { radix = "hexadecimal"; }, else => {}, } } const type_node = try Tag.type.create(c.arena, switch (suffix) { .none => "c_int", .u => "c_uint", .l => "c_long", .lu => "c_ulong", .ll => "c_longlong", .llu => "c_ulonglong", .f => unreachable, }); lit_bytes = lit_bytes[0 .. lit_bytes.len - switch (suffix) { .none => @as(u8, 0), .u, .l => 1, .lu, .ll => 2, .llu => 3, .f => unreachable, }]; const value = std.fmt.parseInt(i128, lit_bytes, 0) catch math.maxInt(i128); // make the output less noisy by skipping promoteIntLiteral where // it's guaranteed to not be required because of C standard type constraints const guaranteed_to_fit = switch (suffix) { .none => !meta.isError(math.cast(i16, value)), .u => !meta.isError(math.cast(u16, value)), .l => !meta.isError(math.cast(i32, value)), .lu => !meta.isError(math.cast(u32, value)), .ll => !meta.isError(math.cast(i64, value)), .llu => !meta.isError(math.cast(u64, value)), .f => unreachable, }; const literal_node = try transCreateNodeNumber(c, lit_bytes, .int); if (guaranteed_to_fit) { return Tag.as.create(c.arena, .{ .lhs = type_node, .rhs = literal_node }); } else { return Tag.std_meta_promoteIntLiteral.create(c.arena, .{ .type = type_node, .value = literal_node, .radix = try Tag.enum_literal.create(c.arena, radix), }); } }, .FloatLiteral => |suffix| { if (suffix != .none) lit_bytes = lit_bytes[0 .. lit_bytes.len - 1]; const dot_index = mem.indexOfScalar(u8, lit_bytes, '.').?; if (dot_index == 0) { lit_bytes = try std.fmt.allocPrint(c.arena, "0{s}", .{lit_bytes}); } else if (dot_index + 1 == lit_bytes.len or !std.ascii.isDigit(lit_bytes[dot_index + 1])) { // If the literal lacks a digit after the `.`, we need to // add one since `1.` or `1.e10` would be invalid syntax in Zig. lit_bytes = try std.fmt.allocPrint(c.arena, "{s}0{s}", .{ lit_bytes[0 .. dot_index + 1], lit_bytes[dot_index + 1 ..], }); } if (suffix == .none) return transCreateNodeNumber(c, lit_bytes, .float); const type_node = try Tag.type.create(c.arena, switch (suffix) { .f => "f32", .l => "c_longdouble", else => unreachable, }); const rhs = try transCreateNodeNumber(c, lit_bytes, .float); return Tag.as.create(c.arena, .{ .lhs = type_node, .rhs = rhs }); }, else => unreachable, } } fn zigifyEscapeSequences(ctx: *Context, m: *MacroCtx) ![]const u8 { var source = m.slice(); for (source) |c, i| { if (c == '\"' or c == '\'') { source = source[i..]; break; } } for (source) |c| { if (c == '\\') { break; } } else return source; var bytes = try ctx.arena.alloc(u8, source.len * 2); var state: enum { Start, Escape, Hex, Octal, } = .Start; var i: usize = 0; var count: u8 = 0; var num: u8 = 0; for (source) |c| { switch (state) { .Escape => { switch (c) { 'n', 'r', 't', '\\', '\'', '\"' => { bytes[i] = c; }, '0'...'7' => { count += 1; num += c - '0'; state = .Octal; bytes[i] = 'x'; }, 'x' => { state = .Hex; bytes[i] = 'x'; }, 'a' => { bytes[i] = 'x'; i += 1; bytes[i] = '0'; i += 1; bytes[i] = '7'; }, 'b' => { bytes[i] = 'x'; i += 1; bytes[i] = '0'; i += 1; bytes[i] = '8'; }, 'f' => { bytes[i] = 'x'; i += 1; bytes[i] = '0'; i += 1; bytes[i] = 'C'; }, 'v' => { bytes[i] = 'x'; i += 1; bytes[i] = '0'; i += 1; bytes[i] = 'B'; }, '?' => { i -= 1; bytes[i] = '?'; }, 'u', 'U' => { try m.fail(ctx, "macro tokenizing failed: TODO unicode escape sequences", .{}); return error.ParseError; }, else => { try m.fail(ctx, "macro tokenizing failed: unknown escape sequence", .{}); return error.ParseError; }, } i += 1; if (state == .Escape) state = .Start; }, .Start => { if (c == '\\') { state = .Escape; } bytes[i] = c; i += 1; }, .Hex => { switch (c) { '0'...'9' => { num = std.math.mul(u8, num, 16) catch { try m.fail(ctx, "macro tokenizing failed: hex literal overflowed", .{}); return error.ParseError; }; num += c - '0'; }, 'a'...'f' => { num = std.math.mul(u8, num, 16) catch { try m.fail(ctx, "macro tokenizing failed: hex literal overflowed", .{}); return error.ParseError; }; num += c - 'a' + 10; }, 'A'...'F' => { num = std.math.mul(u8, num, 16) catch { try m.fail(ctx, "macro tokenizing failed: hex literal overflowed", .{}); return error.ParseError; }; num += c - 'A' + 10; }, else => { i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); num = 0; if (c == '\\') state = .Escape else state = .Start; bytes[i] = c; i += 1; }, } }, .Octal => { const accept_digit = switch (c) { // The maximum length of a octal literal is 3 digits '0'...'7' => count < 3, else => false, }; if (accept_digit) { count += 1; num = std.math.mul(u8, num, 8) catch { try m.fail(ctx, "macro tokenizing failed: octal literal overflowed", .{}); return error.ParseError; }; num += c - '0'; } else { i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); num = 0; count = 0; if (c == '\\') state = .Escape else state = .Start; bytes[i] = c; i += 1; } }, } } if (state == .Hex or state == .Octal) i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); return bytes[0..i]; } fn parseCPrimaryExprInner(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { const tok = m.next().?; const slice = m.slice(); switch (tok) { .CharLiteral => { if (slice[0] != '\'' or slice[1] == '\\' or slice.len == 3) { return Tag.char_literal.create(c.arena, try zigifyEscapeSequences(c, m)); } else { const str = try std.fmt.allocPrint(c.arena, "0x{s}", .{std.fmt.fmtSliceHexLower(slice[1 .. slice.len - 1])}); return Tag.integer_literal.create(c.arena, str); } }, .StringLiteral => { return Tag.string_literal.create(c.arena, try zigifyEscapeSequences(c, m)); }, .IntegerLiteral, .FloatLiteral => { return parseCNumLit(c, m); }, // eventually this will be replaced by std.c.parse which will handle these correctly .Keyword_void => return Tag.type.create(c.arena, "c_void"), .Keyword_bool => return Tag.type.create(c.arena, "bool"), .Keyword_double => return Tag.type.create(c.arena, "f64"), .Keyword_long => return Tag.type.create(c.arena, "c_long"), .Keyword_int => return Tag.type.create(c.arena, "c_int"), .Keyword_float => return Tag.type.create(c.arena, "f32"), .Keyword_short => return Tag.type.create(c.arena, "c_short"), .Keyword_char => return Tag.type.create(c.arena, "u8"), .Keyword_unsigned => if (m.next()) |t| switch (t) { .Keyword_char => return Tag.type.create(c.arena, "u8"), .Keyword_short => return Tag.type.create(c.arena, "c_ushort"), .Keyword_int => return Tag.type.create(c.arena, "c_uint"), .Keyword_long => if (m.peek() != null and m.peek().? == .Keyword_long) { _ = m.next(); return Tag.type.create(c.arena, "c_ulonglong"); } else return Tag.type.create(c.arena, "c_ulong"), else => { m.i -= 1; return Tag.type.create(c.arena, "c_uint"); }, } else { return Tag.type.create(c.arena, "c_uint"); }, .Keyword_signed => if (m.next()) |t| switch (t) { .Keyword_char => return Tag.type.create(c.arena, "i8"), .Keyword_short => return Tag.type.create(c.arena, "c_short"), .Keyword_int => return Tag.type.create(c.arena, "c_int"), .Keyword_long => if (m.peek() != null and m.peek().? == .Keyword_long) { _ = m.next(); return Tag.type.create(c.arena, "c_longlong"); } else return Tag.type.create(c.arena, "c_long"), else => { m.i -= 1; return Tag.type.create(c.arena, "c_int"); }, } else { return Tag.type.create(c.arena, "c_int"); }, .Keyword_enum, .Keyword_struct, .Keyword_union => { // struct Foo will be declared as struct_Foo by transRecordDecl const next_id = m.next().?; if (next_id != .Identifier) { try m.fail(c, "unable to translate C expr: expected Identifier instead got: {s}", .{@tagName(next_id)}); return error.ParseError; } const name = try std.fmt.allocPrint(c.arena, "{s}_{s}", .{ slice, m.slice() }); return Tag.identifier.create(c.arena, name); }, .Identifier => { const mangled_name = scope.getAlias(slice); if (mem.startsWith(u8, mangled_name, "__builtin_") and !isBuiltinDefined(mangled_name)) { try m.fail(c, "TODO implement function '{s}' in std.c.builtins", .{mangled_name}); return error.ParseError; } return Tag.identifier.create(c.arena, builtin_typedef_map.get(mangled_name) orelse mangled_name); }, .LParen => { const inner_node = try parseCExpr(c, m, scope); const next_id = m.next().?; if (next_id != .RParen) { try m.fail(c, "unable to translate C expr: expected ')' instead got: {s}", .{@tagName(next_id)}); return error.ParseError; } var saw_l_paren = false; var saw_integer_literal = false; switch (m.peek().?) { // (type)(to_cast) .LParen => { saw_l_paren = true; _ = m.next(); }, // (type)sizeof(x) .Keyword_sizeof, // (type)alignof(x) .Keyword_alignof, // (type)identifier .Identifier, => {}, // (type)integer .IntegerLiteral => { saw_integer_literal = true; }, else => return inner_node, } const node_to_cast = try parseCExpr(c, m, scope); if (saw_l_paren and m.next().? != .RParen) { try m.fail(c, "unable to translate C expr: expected ')'", .{}); return error.ParseError; } return Tag.std_meta_cast.create(c.arena, .{ .lhs = inner_node, .rhs = node_to_cast }); }, else => { try m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(tok)}); return error.ParseError; }, } } fn parseCPrimaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCPrimaryExprInner(c, m, scope); // In C the preprocessor would handle concatting strings while expanding macros. // This should do approximately the same by concatting any strings and identifiers // after a primary expression. while (true) { switch (m.peek().?) { .StringLiteral, .Identifier => {}, else => break, } node = try Tag.array_cat.create(c.arena, .{ .lhs = node, .rhs = try parseCPrimaryExprInner(c, m, scope) }); } return node; } fn macroBoolToInt(c: *Context, node: Node) !Node { if (!isBoolRes(node)) { return node; } return Tag.bool_to_int.create(c.arena, node); } fn macroIntToBool(c: *Context, node: Node) !Node { if (isBoolRes(node)) { return node; } return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() }); } fn parseCCondExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { const node = try parseCOrExpr(c, m, scope); if (m.peek().? != .QuestionMark) { return node; } _ = m.next(); const then_body = try parseCOrExpr(c, m, scope); if (m.next().? != .Colon) { try m.fail(c, "unable to translate C expr: expected ':'", .{}); return error.ParseError; } const else_body = try parseCCondExpr(c, m, scope); return Tag.@"if".create(c.arena, .{ .cond = node, .then = then_body, .@"else" = else_body }); } fn parseCOrExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCAndExpr(c, m, scope); while (m.next().? == .PipePipe) { const lhs = try macroIntToBool(c, node); const rhs = try macroIntToBool(c, try parseCAndExpr(c, m, scope)); node = try Tag.@"or".create(c.arena, .{ .lhs = lhs, .rhs = rhs }); } m.i -= 1; return node; } fn parseCAndExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCBitOrExpr(c, m, scope); while (m.next().? == .AmpersandAmpersand) { const lhs = try macroIntToBool(c, node); const rhs = try macroIntToBool(c, try parseCBitOrExpr(c, m, scope)); node = try Tag.@"and".create(c.arena, .{ .lhs = lhs, .rhs = rhs }); } m.i -= 1; return node; } fn parseCBitOrExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCBitXorExpr(c, m, scope); while (m.next().? == .Pipe) { const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCBitXorExpr(c, m, scope)); node = try Tag.bit_or.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); } m.i -= 1; return node; } fn parseCBitXorExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCBitAndExpr(c, m, scope); while (m.next().? == .Caret) { const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCBitAndExpr(c, m, scope)); node = try Tag.bit_xor.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); } m.i -= 1; return node; } fn parseCBitAndExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCEqExpr(c, m, scope); while (m.next().? == .Ampersand) { const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCEqExpr(c, m, scope)); node = try Tag.bit_and.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); } m.i -= 1; return node; } fn parseCEqExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCRelExpr(c, m, scope); while (true) { switch (m.peek().?) { .BangEqual => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCRelExpr(c, m, scope)); node = try Tag.not_equal.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .EqualEqual => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCRelExpr(c, m, scope)); node = try Tag.equal.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, else => return node, } } } fn parseCRelExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCShiftExpr(c, m, scope); while (true) { switch (m.peek().?) { .AngleBracketRight => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCShiftExpr(c, m, scope)); node = try Tag.greater_than.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .AngleBracketRightEqual => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCShiftExpr(c, m, scope)); node = try Tag.greater_than_equal.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .AngleBracketLeft => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCShiftExpr(c, m, scope)); node = try Tag.less_than.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .AngleBracketLeftEqual => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCShiftExpr(c, m, scope)); node = try Tag.less_than_equal.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, else => return node, } } } fn parseCShiftExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCAddSubExpr(c, m, scope); while (true) { switch (m.peek().?) { .AngleBracketAngleBracketLeft => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCAddSubExpr(c, m, scope)); node = try Tag.shl.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .AngleBracketAngleBracketRight => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCAddSubExpr(c, m, scope)); node = try Tag.shr.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, else => return node, } } } fn parseCAddSubExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCMulExpr(c, m, scope); while (true) { switch (m.peek().?) { .Plus => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCMulExpr(c, m, scope)); node = try Tag.add.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .Minus => { _ = m.next(); const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCMulExpr(c, m, scope)); node = try Tag.sub.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, else => return node, } } } fn parseCMulExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCUnaryExpr(c, m, scope); while (true) { switch (m.next().?) { .Asterisk => { const next = m.peek().?; if (next == .RParen or next == .Nl or next == .Eof) { // type *) // last token of `node` const prev_id = m.list[m.i - 1].id; if (prev_id == .Keyword_void) { const ptr = try Tag.single_pointer.create(c.arena, .{ .is_const = false, .is_volatile = false, .elem_type = node, }); return Tag.optional_type.create(c.arena, ptr); } else { return Tag.c_pointer.create(c.arena, .{ .is_const = false, .is_volatile = false, .elem_type = node, }); } } else { // expr * expr const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCUnaryExpr(c, m, scope)); node = try Tag.mul.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); } }, .Slash => { const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCUnaryExpr(c, m, scope)); node = try Tag.div.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .Percent => { const lhs = try macroBoolToInt(c, node); const rhs = try macroBoolToInt(c, try parseCUnaryExpr(c, m, scope)); node = try Tag.mod.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, else => { m.i -= 1; return node; }, } } } fn parseCPostfixExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { var node = try parseCPrimaryExpr(c, m, scope); while (true) { switch (m.next().?) { .Period => { if (m.next().? != .Identifier) { try m.fail(c, "unable to translate C expr: expected identifier", .{}); return error.ParseError; } node = try Tag.field_access.create(c.arena, .{ .lhs = node, .field_name = m.slice() }); }, .Arrow => { if (m.next().? != .Identifier) { try m.fail(c, "unable to translate C expr: expected identifier", .{}); return error.ParseError; } const deref = try Tag.deref.create(c.arena, node); node = try Tag.field_access.create(c.arena, .{ .lhs = deref, .field_name = m.slice() }); }, .LBracket => { const index = try macroBoolToInt(c, try parseCExpr(c, m, scope)); node = try Tag.array_access.create(c.arena, .{ .lhs = node, .rhs = index }); if (m.next().? != .RBracket) { try m.fail(c, "unable to translate C expr: expected ']'", .{}); return error.ParseError; } }, .LParen => { if (m.peek().? == .RParen) { m.i += 1; node = try Tag.call.create(c.arena, .{ .lhs = node, .args = &[0]Node{} }); } else { var args = std.ArrayList(Node).init(c.gpa); defer args.deinit(); while (true) { const arg = try parseCCondExpr(c, m, scope); try args.append(arg); switch (m.next().?) { .Comma => {}, .RParen => break, else => { try m.fail(c, "unable to translate C expr: expected ',' or ')'", .{}); return error.ParseError; }, } } node = try Tag.call.create(c.arena, .{ .lhs = node, .args = try c.arena.dupe(Node, args.items) }); } }, .LBrace => { // Check for designated field initializers if (m.peek().? == .Period) { var init_vals = std.ArrayList(ast.Payload.ContainerInitDot.Initializer).init(c.gpa); defer init_vals.deinit(); while (true) { if (m.next().? != .Period) { try m.fail(c, "unable to translate C expr: expected '.'", .{}); return error.ParseError; } if (m.next().? != .Identifier) { try m.fail(c, "unable to translate C expr: expected identifier", .{}); return error.ParseError; } const name = m.slice(); if (m.next().? != .Equal) { try m.fail(c, "unable to translate C expr: expected '='", .{}); return error.ParseError; } const val = try parseCCondExpr(c, m, scope); try init_vals.append(.{ .name = name, .value = val }); switch (m.next().?) { .Comma => {}, .RBrace => break, else => { try m.fail(c, "unable to translate C expr: expected ',' or '}}'", .{}); return error.ParseError; }, } } const tuple_node = try Tag.container_init_dot.create(c.arena, try c.arena.dupe(ast.Payload.ContainerInitDot.Initializer, init_vals.items)); node = try Tag.std_mem_zeroinit.create(c.arena, .{ .lhs = node, .rhs = tuple_node }); continue; } var init_vals = std.ArrayList(Node).init(c.gpa); defer init_vals.deinit(); while (true) { const val = try parseCCondExpr(c, m, scope); try init_vals.append(val); switch (m.next().?) { .Comma => {}, .RBrace => break, else => { try m.fail(c, "unable to translate C expr: expected ',' or '}}'", .{}); return error.ParseError; }, } } const tuple_node = try Tag.tuple.create(c.arena, try c.arena.dupe(Node, init_vals.items)); node = try Tag.std_mem_zeroinit.create(c.arena, .{ .lhs = node, .rhs = tuple_node }); }, .PlusPlus, .MinusMinus => { try m.fail(c, "TODO postfix inc/dec expr", .{}); return error.ParseError; }, else => { m.i -= 1; return node; }, } } } fn parseCUnaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { switch (m.next().?) { .Bang => { const operand = try macroIntToBool(c, try parseCUnaryExpr(c, m, scope)); return Tag.not.create(c.arena, operand); }, .Minus => { const operand = try macroBoolToInt(c, try parseCUnaryExpr(c, m, scope)); return Tag.negate.create(c.arena, operand); }, .Plus => return try parseCUnaryExpr(c, m, scope), .Tilde => { const operand = try macroBoolToInt(c, try parseCUnaryExpr(c, m, scope)); return Tag.bit_not.create(c.arena, operand); }, .Asterisk => { const operand = try parseCUnaryExpr(c, m, scope); return Tag.deref.create(c.arena, operand); }, .Ampersand => { const operand = try parseCUnaryExpr(c, m, scope); return Tag.address_of.create(c.arena, operand); }, .Keyword_sizeof => { const operand = if (m.peek().? == .LParen) blk: { _ = m.next(); // C grammar says this should be 'type-name' but we have to // use parseCMulExpr to correctly handle pointer types. const inner = try parseCMulExpr(c, m, scope); if (m.next().? != .RParen) { try m.fail(c, "unable to translate C expr: expected ')'", .{}); return error.ParseError; } break :blk inner; } else try parseCUnaryExpr(c, m, scope); return Tag.std_meta_sizeof.create(c.arena, operand); }, .Keyword_alignof => { // TODO this won't work if using <stdalign.h>'s // #define alignof _Alignof if (m.next().? != .LParen) { try m.fail(c, "unable to translate C expr: expected '('", .{}); return error.ParseError; } // C grammar says this should be 'type-name' but we have to // use parseCMulExpr to correctly handle pointer types. const operand = try parseCMulExpr(c, m, scope); if (m.next().? != .RParen) { try m.fail(c, "unable to translate C expr: expected ')'", .{}); return error.ParseError; } return Tag.alignof.create(c.arena, operand); }, .PlusPlus, .MinusMinus => { try m.fail(c, "TODO unary inc/dec expr", .{}); return error.ParseError; }, else => { m.i -= 1; return try parseCPostfixExpr(c, m, scope); }, } } fn getContainer(c: *Context, node: Node) ?Node { switch (node.tag()) { .@"union", .@"struct", .@"enum", .address_of, .bit_not, .not, .optional_type, .negate, .negate_wrap, .array_type, .c_pointer, .single_pointer, => return node, .identifier => { const ident = node.castTag(.identifier).?; if (c.global_scope.sym_table.get(ident.data)) |value| { if (value.castTag(.var_decl)) |var_decl| return getContainer(c, var_decl.data.init.?); if (value.castTag(.var_simple) orelse value.castTag(.pub_var_simple)) |var_decl| return getContainer(c, var_decl.data.init); } }, .field_access => { const field_access = node.castTag(.field_access).?; if (getContainerTypeOf(c, field_access.data.lhs)) |ty_node| { if (ty_node.castTag(.@"struct") orelse ty_node.castTag(.@"union")) |container| { for (container.data.fields) |field| { if (mem.eql(u8, field.name, field_access.data.field_name)) { return getContainer(c, field.type); } } } } }, else => {}, } return null; } fn getContainerTypeOf(c: *Context, ref: Node) ?Node { if (ref.castTag(.identifier)) |ident| { if (c.global_scope.sym_table.get(ident.data)) |value| { if (value.castTag(.var_decl)) |var_decl| { return getContainer(c, var_decl.data.type); } } } else if (ref.castTag(.field_access)) |field_access| { if (getContainerTypeOf(c, field_access.data.lhs)) |ty_node| { if (ty_node.castTag(.@"struct") orelse ty_node.castTag(.@"union")) |container| { for (container.data.fields) |field| { if (mem.eql(u8, field.name, field_access.data.field_name)) { return getContainer(c, field.type); } } } else return ty_node; } } return null; } fn getFnProto(c: *Context, ref: Node) ?*ast.Payload.Func { const init = if (ref.castTag(.var_decl)) |v| v.data.init orelse return null else if (ref.castTag(.var_simple) orelse ref.castTag(.pub_var_simple)) |v| v.data.init else return null; if (getContainerTypeOf(c, init)) |ty_node| { if (ty_node.castTag(.optional_type)) |prefix| { if (prefix.data.castTag(.func)) |fn_proto| { return fn_proto; } } } return null; } fn addMacros(c: *Context) !void { var it = c.global_scope.macro_table.iterator(); while (it.next()) |entry| { if (getFnProto(c, entry.value_ptr.*)) |proto_node| { // If a macro aliases a global variable which is a function pointer, we conclude that // the macro is intended to represent a function that assumes the function pointer // variable is non-null and calls it. try addTopLevelDecl(c, entry.key_ptr.*, try transCreateNodeMacroFn(c, entry.key_ptr.*, entry.value_ptr.*, proto_node)); } else { try addTopLevelDecl(c, entry.key_ptr.*, entry.value_ptr.*); } } }
const image = @import("image.zig"); const color = @import("color.zig"); const Allocator = @import("std").mem.Allocator; // mlarouche: Because this is a interface, I use Zig function naming convention instead of the variable naming convention pub const FormatInterface = struct { format: FormatFn, formatDetect: FormatDetectFn, readForImage: ReadForImageFn, writeForImage: WriteForImageFn, pub const FormatFn = fn () image.ImageFormat; pub const FormatDetectFn = fn (reader: image.ImageReader, seekStream: image.ImageSeekStream) anyerror!bool; pub const ReadForImageFn = fn (allocator: *Allocator, reader: image.ImageReader, seekStream: image.ImageSeekStream, pixels: *?color.ColorStorage) anyerror!image.ImageInfo; pub const WriteForImageFn = fn (allocator: *Allocator, write_stream: image.ImageWriterStream, seek_stream: image.ImageSeekStream, pixels: color.ColorStorage, save_info: image.ImageSaveInfo) anyerror!void; };
const std = @import("std"); const expect = std.testing.expect; test "@byteSwap integers" { const ByteSwapIntTest = struct { fn run() !void { try t(u0, 0, 0); try t(u8, 0x12, 0x12); try t(u16, 0x1234, 0x3412); try t(u24, 0x123456, 0x563412); try t(u32, 0x12345678, 0x78563412); try t(u40, 0x123456789a, 0x9a78563412); try t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412))); try t(u56, 0x123456789abcde, 0xdebc9a78563412); try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412); try t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412); try t(u0, @as(u0, 0), 0); try t(i8, @as(i8, -50), -50); try t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412))); try t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412))); try t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412))); try t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412)); try t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412))); try t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412))); try t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412))); try t( i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)), @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)), ); } fn t(comptime I: type, input: I, expected_output: I) !void { try std.testing.expectEqual(expected_output, @byteSwap(I, input)); } }; comptime try ByteSwapIntTest.run(); try ByteSwapIntTest.run(); } test "@byteSwap vectors" { const ByteSwapVectorTest = struct { fn run() !void { try t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 }); try t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 }); try t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 }); } fn t( comptime I: type, comptime n: comptime_int, input: std.meta.Vector(n, I), expected_vector: std.meta.Vector(n, I), ) !void { const actual_output: [n]I = @byteSwap(I, input); const expected_output: [n]I = expected_vector; try std.testing.expectEqual(expected_output, actual_output); } }; comptime try ByteSwapVectorTest.run(); try ByteSwapVectorTest.run(); }
const std = @import("std"); const Builder = @import("../src/sql_builder.zig").Builder; const testing = std.testing; test "builder" { var temp_memory = std.heap.ArenaAllocator.init(std.heap.page_allocator); const allocator = &temp_memory.allocator; defer temp_memory.deinit(); var builder = Builder.new(.Insert, allocator).table("test"); try builder.addColumn("id"); try builder.addColumn("name"); try builder.addColumn("age"); try builder.addValue("5"); try builder.addValue("Test"); try builder.addValue("3"); try builder.end(); try testing.expectEqualStrings("INSERT INTO test (id,name,age) VALUES (5,'Test',3);", builder.command()); builder.deinit(); var builder2 = Builder.new(.Insert, allocator).table("test"); try builder2.addColumn("id"); try builder2.addColumn("name"); try builder2.addColumn("age"); try builder2.addValue("5"); try builder2.addValue("Test"); try builder2.addValue("3"); try builder2.addValue("1"); try builder2.addValue("Test2"); try builder2.addValue("53"); try builder2.addValue("3"); try builder2.addValue("Test3"); try builder2.addValue("53"); try builder2.end(); try testing.expectEqualStrings("INSERT INTO test (id,name,age) VALUES (5,'Test',3),(1,'Test2',53),(3,'Test3',53);", builder2.command()); builder2.deinit(); var builder3 = Builder.new(.Update, allocator).table("test").where(try Builder.buildQuery("WHERE NAME = {s};", .{"Steve"}, allocator)); try builder3.addColumn("id"); try builder3.addColumn("name"); try builder3.addColumn("age"); try builder3.addValue("5"); try builder3.addValue("Test"); try builder3.addValue("3"); try builder3.end(); try testing.expectEqualStrings("UPDATE test SET id=5,name='Test',age=3 WHERE NAME = 'Steve';", builder3.command()); builder3.deinit(); }
// // A sentinel value indicates the end of data. Let's imagine a // sequence of lowercase letters where uppercase 'S' is the // sentinel, indicating the end of the sequence: // // abcdefS // // If our sequence also allows for uppercase letters, 'S' would // make a terrible sentinel since it could no longer be a regular // value in the sequence: // // abcdQRST // ^-- Oops! The last letter in the sequence is R! // // A popular choice for indicating the end of a string is the // value 0. ASCII and Unicode call this the "Null Character". // // Zig supports sentinel-terminated arrays, slices, and pointers: // // const a: [4:0]u32 = [4:0]u32{1, 2, 3, 4}; // const b: [:0]const u32 = &[4:0]u32{1, 2, 3, 4}; // const c: [*:0]const u32 = &[4:0]u32{1, 2, 3, 4}; // // Array 'a' stores 5 u32 values, the last of which is 0. // However the compiler takes care of this housekeeping detail // for you. You can treat 'a' as a normal array with just 4 // items. // // Slice 'b' is only allowed to point to zero-terminated arrays // but otherwise works just like a normal slice. // // Pointer 'c' is exactly like the many-item pointers we learned // about in exercise 054, but it is guaranteed to end in 0. // Because of this guarantee, we can safely find the end of this // many-item pointer without knowing its length. (We CAN'T do // that with regular many-item pointers!). // // Important: the sentinel value must be of the same type as the // data being terminated! // const print = @import("std").debug.print; pub fn main() void { // Here's a zero-terminated array of u32 values: var nums = [_:0]u32{ 1, 2, 3, 4, 5, 6 }; // And here's a zero-terminated many-item pointer: var ptr: [*:0]u32 = &nums; // For fun, let's replace the value at position 3 with the // sentinel value 0. This seems kind of naughty. nums[3] = 0; // So now we have a zero-terminated array and a many-item // pointer that reference the same data: a sequence of // numbers that both ends in and CONTAINS the sentinel value. // // Attempting to loop through and print both of these should // demonstrate how they are similar and different. // // (It turns out that the array prints completely, including // the sentinel 0 in the middle. The many-item pointer stops // at the first sentinel value.) printSequence(nums); printSequence(ptr); print("\n", .{}); } // Here's our generic sequence printing function. It's nearly // complete, but there are a couple missing bits. Please fix // them! fn printSequence(my_seq: anytype) void { const my_type = @typeInfo(@TypeOf(my_seq)); // The TypeInfo contained in my_type is a union. We use a // switch to handle printing the Array or Pointer fields, // depending on which type of my_seq was passed in: switch (my_type) { .Array => { print("Array:", .{}); // Loop through the items in my_seq. for (my_type) |s| { print("{}", .{s}); } }, .Pointer => { // Check this out - it's pretty cool: const my_sentinel = my_type.Pointer.sentinel; print("Many-item pointer:", .{}); // Loop through the items in my_seq until we hit the // sentinel value. var i: usize = 0; while (i != my_sentinel) { print("{}", .{my_seq[i]}); i += 1; } }, else => unreachable, } print(". ", .{}); }
const std = @import("std"); const mem = std.mem; const os = std.os; const Token = std.zig.Token; const ast = std.zig.ast; const TokenIndex = std.zig.ast.TokenIndex; const Compilation = @import("compilation.zig").Compilation; const Scope = @import("scope.zig").Scope; pub const Color = enum { Auto, Off, On, }; pub const Span = struct { first: ast.TokenIndex, last: ast.TokenIndex, pub fn token(i: TokenIndex) Span { return Span{ .first = i, .last = i, }; } pub fn node(n: *ast.Node) Span { return Span{ .first = n.firstToken(), .last = n.lastToken(), }; } }; pub const Msg = struct { text: []u8, realpath: []u8, data: Data, const Data = union(enum) { Cli: Cli, PathAndTree: PathAndTree, ScopeAndComp: ScopeAndComp, }; const PathAndTree = struct { span: Span, tree: *ast.Tree, allocator: *mem.Allocator, }; const ScopeAndComp = struct { span: Span, tree_scope: *Scope.AstTree, compilation: *Compilation, }; const Cli = struct { allocator: *mem.Allocator, }; pub fn destroy(self: *Msg) void { switch (self.data) { Data.Cli => |cli| { cli.allocator.free(self.text); cli.allocator.free(self.realpath); cli.allocator.destroy(self); }, Data.PathAndTree => |path_and_tree| { path_and_tree.allocator.free(self.text); path_and_tree.allocator.free(self.realpath); path_and_tree.allocator.destroy(self); }, Data.ScopeAndComp => |scope_and_comp| { scope_and_comp.tree_scope.base.deref(scope_and_comp.compilation); scope_and_comp.compilation.gpa().free(self.text); scope_and_comp.compilation.gpa().free(self.realpath); scope_and_comp.compilation.gpa().destroy(self); }, } } fn getAllocator(self: *const Msg) *mem.Allocator { switch (self.data) { Data.Cli => |cli| return cli.allocator, Data.PathAndTree => |path_and_tree| { return path_and_tree.allocator; }, Data.ScopeAndComp => |scope_and_comp| { return scope_and_comp.compilation.gpa(); }, } } pub fn getTree(self: *const Msg) *ast.Tree { switch (self.data) { Data.Cli => unreachable, Data.PathAndTree => |path_and_tree| { return path_and_tree.tree; }, Data.ScopeAndComp => |scope_and_comp| { return scope_and_comp.tree_scope.tree; }, } } pub fn getSpan(self: *const Msg) Span { return switch (self.data) { Data.Cli => unreachable, Data.PathAndTree => |path_and_tree| path_and_tree.span, Data.ScopeAndComp => |scope_and_comp| scope_and_comp.span, }; } /// Takes ownership of text /// References tree_scope, and derefs when the msg is freed pub fn createFromScope(comp: *Compilation, tree_scope: *Scope.AstTree, span: Span, text: []u8) !*Msg { const realpath = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath); errdefer comp.gpa().free(realpath); const msg = try comp.gpa().create(Msg{ .text = text, .realpath = realpath, .data = Data{ .ScopeAndComp = ScopeAndComp{ .tree_scope = tree_scope, .compilation = comp, .span = span, }, }, }); tree_scope.base.ref(); return msg; } /// Caller owns returned Msg and must free with `allocator` /// allocator will additionally be used for printing messages later. pub fn createFromCli(comp: *Compilation, realpath: []const u8, text: []u8) !*Msg { const realpath_copy = try mem.dupe(comp.gpa(), u8, realpath); errdefer comp.gpa().free(realpath_copy); const msg = try comp.gpa().create(Msg{ .text = text, .realpath = realpath_copy, .data = Data{ .Cli = Cli{ .allocator = comp.gpa() }, }, }); return msg; } pub fn createFromParseErrorAndScope( comp: *Compilation, tree_scope: *Scope.AstTree, parse_error: *const ast.Error, ) !*Msg { const loc_token = parse_error.loc(); var text_buf = try std.Buffer.initSize(comp.gpa(), 0); defer text_buf.deinit(); const realpath_copy = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath); errdefer comp.gpa().free(realpath_copy); var out_stream = &std.io.BufferOutStream.init(&text_buf).stream; try parse_error.render(&tree_scope.tree.tokens, out_stream); const msg = try comp.gpa().create(Msg{ .text = undefined, .realpath = realpath_copy, .data = Data{ .ScopeAndComp = ScopeAndComp{ .tree_scope = tree_scope, .compilation = comp, .span = Span{ .first = loc_token, .last = loc_token, }, }, }, }); tree_scope.base.ref(); msg.text = text_buf.toOwnedSlice(); return msg; } /// `realpath` must outlive the returned Msg /// `tree` must outlive the returned Msg /// Caller owns returned Msg and must free with `allocator` /// allocator will additionally be used for printing messages later. pub fn createFromParseError( allocator: *mem.Allocator, parse_error: *const ast.Error, tree: *ast.Tree, realpath: []const u8, ) !*Msg { const loc_token = parse_error.loc(); var text_buf = try std.Buffer.initSize(allocator, 0); defer text_buf.deinit(); const realpath_copy = try mem.dupe(allocator, u8, realpath); errdefer allocator.free(realpath_copy); var out_stream = &std.io.BufferOutStream.init(&text_buf).stream; try parse_error.render(&tree.tokens, out_stream); const msg = try allocator.create(Msg{ .text = undefined, .realpath = realpath_copy, .data = Data{ .PathAndTree = PathAndTree{ .allocator = allocator, .tree = tree, .span = Span{ .first = loc_token, .last = loc_token, }, }, }, }); msg.text = text_buf.toOwnedSlice(); errdefer allocator.destroy(msg); return msg; } pub fn printToStream(msg: *const Msg, stream: var, color_on: bool) !void { switch (msg.data) { Data.Cli => { try stream.print("{}:-:-: error: {}\n", msg.realpath, msg.text); return; }, else => {}, } const allocator = msg.getAllocator(); const tree = msg.getTree(); const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); const relpath = try os.path.relative(allocator, cwd, msg.realpath); defer allocator.free(relpath); const path = if (relpath.len < msg.realpath.len) relpath else msg.realpath; const span = msg.getSpan(); const first_token = tree.tokens.at(span.first); const last_token = tree.tokens.at(span.last); const start_loc = tree.tokenLocationPtr(0, first_token); const end_loc = tree.tokenLocationPtr(first_token.end, last_token); if (!color_on) { try stream.print( "{}:{}:{}: error: {}\n", path, start_loc.line + 1, start_loc.column + 1, msg.text, ); return; } try stream.print( "{}:{}:{}: error: {}\n{}\n", path, start_loc.line + 1, start_loc.column + 1, msg.text, tree.source[start_loc.line_start..start_loc.line_end], ); try stream.writeByteNTimes(' ', start_loc.column); try stream.writeByteNTimes('~', last_token.end - first_token.start); try stream.write("\n"); } pub fn printToFile(msg: *const Msg, file: os.File, color: Color) !void { const color_on = switch (color) { Color.Auto => file.isTty(), Color.On => true, Color.Off => false, }; var stream = &file.outStream().stream; return msg.printToStream(stream, color_on); } };
const uart = @import("uart"); const std = @import("std"); usingnamespace @import("gpio"); //https://lastminuteengineers.com/a4988-stepper-motor-driver-arduino-tutorial/ const DIR = 1; const STEP = 2; pub fn main() void { pinMode(DIR, .output); pinMode(STEP, .output); digitalWrite(DIR, .low); var x: u32 = 0; while (true) { digitalWrite(STEP, .high); delay(200); // digitalWrite(STEP, .high); // delay(200); // digitalWrite(STEP, .low); // delay(500); } }
const std = @import("std.zig"); const builtin = @import("builtin"); const debug = std.debug; const assert = debug.assert; const math = std.math; const mem = @This(); const meta = std.meta; const trait = meta.trait; const testing = std.testing; const Endian = std.builtin.Endian; const native_endian = builtin.cpu.arch.endian(); /// Compile time known minimum page size. /// https://github.com/ziglang/zig/issues/4082 pub const page_size = switch (builtin.cpu.arch) { .wasm32, .wasm64 => 64 * 1024, .aarch64 => switch (builtin.os.tag) { .macos, .ios, .watchos, .tvos => 16 * 1024, else => 4 * 1024, }, .sparcv9 => 8 * 1024, else => 4 * 1024, }; /// The standard library currently thoroughly depends on byte size /// being 8 bits. (see the use of u8 throughout allocation code as /// the "byte" type.) Code which depends on this can reference this /// declaration. If we ever try to port the standard library to a /// non-8-bit-byte platform, this will allow us to search for things /// which need to be updated. pub const byte_size_in_bits = 8; pub const Allocator = @import("mem/Allocator.zig"); /// Detects and asserts if the std.mem.Allocator interface is violated by the caller /// or the allocator. pub fn ValidationAllocator(comptime T: type) type { return struct { const Self = @This(); underlying_allocator: T, pub fn init(underlying_allocator: T) @This() { return .{ .underlying_allocator = underlying_allocator, }; } pub fn allocator(self: *Self) Allocator { return Allocator.init(self, alloc, resize, free); } fn getUnderlyingAllocatorPtr(self: *Self) Allocator { if (T == Allocator) return self.underlying_allocator; return self.underlying_allocator.allocator(); } pub fn alloc( self: *Self, n: usize, ptr_align: u29, len_align: u29, ret_addr: usize, ) Allocator.Error![]u8 { assert(n > 0); assert(mem.isValidAlign(ptr_align)); if (len_align != 0) { assert(mem.isAlignedAnyAlign(n, len_align)); assert(n >= len_align); } const underlying = self.getUnderlyingAllocatorPtr(); const result = try underlying.rawAlloc(n, ptr_align, len_align, ret_addr); assert(mem.isAligned(@ptrToInt(result.ptr), ptr_align)); if (len_align == 0) { assert(result.len == n); } else { assert(result.len >= n); assert(mem.isAlignedAnyAlign(result.len, len_align)); } return result; } pub fn resize( self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize, ) ?usize { assert(buf.len > 0); if (len_align != 0) { assert(mem.isAlignedAnyAlign(new_len, len_align)); assert(new_len >= len_align); } const underlying = self.getUnderlyingAllocatorPtr(); const result = underlying.rawResize(buf, buf_align, new_len, len_align, ret_addr) orelse return null; if (len_align == 0) { assert(result == new_len); } else { assert(result >= new_len); assert(mem.isAlignedAnyAlign(result, len_align)); } return result; } pub fn free( self: *Self, buf: []u8, buf_align: u29, ret_addr: usize, ) void { _ = self; _ = buf_align; _ = ret_addr; assert(buf.len > 0); } pub usingnamespace if (T == Allocator or !@hasDecl(T, "reset")) struct {} else struct { pub fn reset(self: *Self) void { self.underlying_allocator.reset(); } }; }; } pub fn validationWrap(allocator: anytype) ValidationAllocator(@TypeOf(allocator)) { return ValidationAllocator(@TypeOf(allocator)).init(allocator); } /// An allocator helper function. Adjusts an allocation length satisfy `len_align`. /// `full_len` should be the full capacity of the allocation which may be greater /// than the `len` that was requsted. This function should only be used by allocators /// that are unaffected by `len_align`. pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize { assert(alloc_len > 0); assert(alloc_len >= len_align); assert(full_len >= alloc_len); if (len_align == 0) return alloc_len; const adjusted = alignBackwardAnyAlign(full_len, len_align); assert(adjusted >= alloc_len); return adjusted; } const fail_allocator = Allocator{ .ptr = undefined, .vtable = &failAllocator_vtable, }; const failAllocator_vtable = Allocator.VTable{ .alloc = failAllocatorAlloc, .resize = Allocator.NoResize(anyopaque).noResize, .free = Allocator.NoOpFree(anyopaque).noOpFree, }; fn failAllocatorAlloc(_: *anyopaque, n: usize, alignment: u29, len_align: u29, ra: usize) Allocator.Error![]u8 { _ = n; _ = alignment; _ = len_align; _ = ra; return error.OutOfMemory; } test "mem.Allocator basics" { try testing.expectError(error.OutOfMemory, fail_allocator.alloc(u8, 1)); try testing.expectError(error.OutOfMemory, fail_allocator.allocSentinel(u8, 1, 0)); } test "Allocator.resize" { const primitiveIntTypes = .{ i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, }; inline for (primitiveIntTypes) |T| { var values = try testing.allocator.alloc(T, 100); defer testing.allocator.free(values); for (values) |*v, i| v.* = @intCast(T, i); values = testing.allocator.resize(values, values.len + 10) orelse return error.OutOfMemory; try testing.expect(values.len == 110); } const primitiveFloatTypes = .{ f16, f32, f64, f128, }; inline for (primitiveFloatTypes) |T| { var values = try testing.allocator.alloc(T, 100); defer testing.allocator.free(values); for (values) |*v, i| v.* = @intToFloat(T, i); values = testing.allocator.resize(values, values.len + 10) orelse return error.OutOfMemory; try testing.expect(values.len == 110); } } /// Copy all of source into dest at position 0. /// dest.len must be >= source.len. /// If the slices overlap, dest.ptr must be <= src.ptr. pub fn copy(comptime T: type, dest: []T, source: []const T) void { // TODO instead of manually doing this check for the whole array // and turning off runtime safety, the compiler should detect loops like // this and automatically omit safety checks for loops @setRuntimeSafety(false); assert(dest.len >= source.len); for (source) |s, i| dest[i] = s; } /// Copy all of source into dest at position 0. /// dest.len must be >= source.len. /// If the slices overlap, dest.ptr must be >= src.ptr. pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void { // TODO instead of manually doing this check for the whole array // and turning off runtime safety, the compiler should detect loops like // this and automatically omit safety checks for loops @setRuntimeSafety(false); assert(dest.len >= source.len); var i = source.len; while (i > 0) { i -= 1; dest[i] = source[i]; } } /// Sets all elements of `dest` to `value`. pub fn set(comptime T: type, dest: []T, value: T) void { for (dest) |*d| d.* = value; } /// Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function. /// However, it is recognized that there are sometimes use cases for initializing all fields to a "zero" value. For example, when /// interfacing with a C API where this practice is more common and relied upon. If you are performing code review and see this /// function used, examine closely - it may be a code smell. /// Zero initializes the type. /// This can be used to zero initialize a any type for which it makes sense. Structs will be initialized recursively. pub fn zeroes(comptime T: type) T { switch (@typeInfo(T)) { .ComptimeInt, .Int, .ComptimeFloat, .Float => { return @as(T, 0); }, .Enum, .EnumLiteral => { return @intToEnum(T, 0); }, .Void => { return {}; }, .Bool => { return false; }, .Optional, .Null => { return null; }, .Struct => |struct_info| { if (@sizeOf(T) == 0) return T{}; if (comptime meta.containerLayout(T) == .Extern) { var item: T = undefined; set(u8, asBytes(&item), 0); return item; } else { var structure: T = undefined; inline for (struct_info.fields) |field| { @field(structure, field.name) = zeroes(@TypeOf(@field(structure, field.name))); } return structure; } }, .Pointer => |ptr_info| { switch (ptr_info.size) { .Slice => { return &[_]ptr_info.child{}; }, .C => { return null; }, .One, .Many => { @compileError("Can't set a non nullable pointer to zero."); }, } }, .Array => |info| { if (info.sentinel) |sentinel| { return [_:sentinel]info.child{zeroes(info.child)} ** info.len; } return [_]info.child{zeroes(info.child)} ** info.len; }, .Vector => |info| { return @splat(info.len, zeroes(info.child)); }, .Union => |info| { if (comptime meta.containerLayout(T) == .Extern) { // The C language specification states that (global) unions // should be zero initialized to the first named member. var item: T = undefined; @field(item, info.fields[0].name) = zeroes(@TypeOf(@field(item, info.fields[0].name))); return item; } @compileError("Can't set a " ++ @typeName(T) ++ " to zero."); }, .ErrorUnion, .ErrorSet, .Fn, .BoundFn, .Type, .NoReturn, .Undefined, .Opaque, .Frame, .AnyFrame, => { @compileError("Can't set a " ++ @typeName(T) ++ " to zero."); }, } } test "mem.zeroes" { const C_struct = extern struct { x: u32, y: u32, }; var a = zeroes(C_struct); a.y += 10; try testing.expect(a.x == 0); try testing.expect(a.y == 10); const ZigStruct = struct { integral_types: struct { integer_0: i0, integer_8: i8, integer_16: i16, integer_32: i32, integer_64: i64, integer_128: i128, unsigned_0: u0, unsigned_8: u8, unsigned_16: u16, unsigned_32: u32, unsigned_64: u64, unsigned_128: u128, float_32: f32, float_64: f64, }, pointers: struct { optional: ?*u8, c_pointer: [*c]u8, slice: []u8, }, array: [2]u32, vector_u32: meta.Vector(2, u32), vector_f32: meta.Vector(2, f32), vector_bool: meta.Vector(2, bool), optional_int: ?u8, empty: void, sentinel: [3:0]u8, }; const b = zeroes(ZigStruct); try testing.expectEqual(@as(i8, 0), b.integral_types.integer_0); try testing.expectEqual(@as(i8, 0), b.integral_types.integer_8); try testing.expectEqual(@as(i16, 0), b.integral_types.integer_16); try testing.expectEqual(@as(i32, 0), b.integral_types.integer_32); try testing.expectEqual(@as(i64, 0), b.integral_types.integer_64); try testing.expectEqual(@as(i128, 0), b.integral_types.integer_128); try testing.expectEqual(@as(u8, 0), b.integral_types.unsigned_0); try testing.expectEqual(@as(u8, 0), b.integral_types.unsigned_8); try testing.expectEqual(@as(u16, 0), b.integral_types.unsigned_16); try testing.expectEqual(@as(u32, 0), b.integral_types.unsigned_32); try testing.expectEqual(@as(u64, 0), b.integral_types.unsigned_64); try testing.expectEqual(@as(u128, 0), b.integral_types.unsigned_128); try testing.expectEqual(@as(f32, 0), b.integral_types.float_32); try testing.expectEqual(@as(f64, 0), b.integral_types.float_64); try testing.expectEqual(@as(?*u8, null), b.pointers.optional); try testing.expectEqual(@as([*c]u8, null), b.pointers.c_pointer); try testing.expectEqual(@as([]u8, &[_]u8{}), b.pointers.slice); for (b.array) |e| { try testing.expectEqual(@as(u32, 0), e); } try testing.expectEqual(@splat(2, @as(u32, 0)), b.vector_u32); try testing.expectEqual(@splat(2, @as(f32, 0.0)), b.vector_f32); try testing.expectEqual(@splat(2, @as(bool, false)), b.vector_bool); try testing.expectEqual(@as(?u8, null), b.optional_int); for (b.sentinel) |e| { try testing.expectEqual(@as(u8, 0), e); } const C_union = extern union { a: u8, b: u32, }; var c = zeroes(C_union); try testing.expectEqual(@as(u8, 0), c.a); } /// Initializes all fields of the struct with their default value, or zero values if no default value is present. /// If the field is present in the provided initial values, it will have that value instead. /// Structs are initialized recursively. pub fn zeroInit(comptime T: type, init: anytype) T { const Init = @TypeOf(init); switch (@typeInfo(T)) { .Struct => |struct_info| { switch (@typeInfo(Init)) { .Struct => |init_info| { var value = std.mem.zeroes(T); if (init_info.is_tuple) { inline for (init_info.fields) |field, i| { @field(value, struct_info.fields[i].name) = @field(init, field.name); } return value; } inline for (init_info.fields) |field| { if (!@hasField(T, field.name)) { @compileError("Encountered an initializer for `" ++ field.name ++ "`, but it is not a field of " ++ @typeName(T)); } } inline for (struct_info.fields) |field| { if (@hasField(Init, field.name)) { switch (@typeInfo(field.field_type)) { .Struct => { @field(value, field.name) = zeroInit(field.field_type, @field(init, field.name)); }, else => { @field(value, field.name) = @field(init, field.name); }, } } else if (field.default_value) |default_value| { @field(value, field.name) = default_value; } } return value; }, else => { @compileError("The initializer must be a struct"); }, } }, else => { @compileError("Can't default init a " ++ @typeName(T)); }, } } test "zeroInit" { const I = struct { d: f64, }; const S = struct { a: u32, b: ?bool, c: I, e: [3]u8, f: i64 = -1, }; const s = zeroInit(S, .{ .a = 42, }); try testing.expectEqual(S{ .a = 42, .b = null, .c = .{ .d = 0, }, .e = [3]u8{ 0, 0, 0 }, .f = -1, }, s); const Color = struct { r: u8, g: u8, b: u8, a: u8, }; const c = zeroInit(Color, .{ 255, 255 }); try testing.expectEqual(Color{ .r = 255, .g = 255, .b = 0, .a = 0, }, c); } /// Compares two slices of numbers lexicographically. O(n). pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { const n = math.min(lhs.len, rhs.len); var i: usize = 0; while (i < n) : (i += 1) { switch (math.order(lhs[i], rhs[i])) { .eq => continue, .lt => return .lt, .gt => return .gt, } } return math.order(lhs.len, rhs.len); } test "order" { try testing.expect(order(u8, "abcd", "bee") == .lt); try testing.expect(order(u8, "abc", "abc") == .eq); try testing.expect(order(u8, "abc", "abc0") == .lt); try testing.expect(order(u8, "", "") == .eq); try testing.expect(order(u8, "", "a") == .lt); } /// Returns true if lhs < rhs, false otherwise pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool { return order(T, lhs, rhs) == .lt; } test "mem.lessThan" { try testing.expect(lessThan(u8, "abcd", "bee")); try testing.expect(!lessThan(u8, "abc", "abc")); try testing.expect(lessThan(u8, "abc", "abc0")); try testing.expect(!lessThan(u8, "", "")); try testing.expect(lessThan(u8, "", "a")); } /// Compares two slices and returns whether they are equal. pub fn eql(comptime T: type, a: []const T, b: []const T) bool { if (a.len != b.len) return false; if (a.ptr == b.ptr) return true; for (a) |item, index| { if (b[index] != item) return false; } return true; } /// Compares two slices and returns the index of the first inequality. /// Returns null if the slices are equal. pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize { const shortest = math.min(a.len, b.len); if (a.ptr == b.ptr) return if (a.len == b.len) null else shortest; var index: usize = 0; while (index < shortest) : (index += 1) if (a[index] != b[index]) return index; return if (a.len == b.len) null else shortest; } test "indexOfDiff" { try testing.expectEqual(indexOfDiff(u8, "one", "one"), null); try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3); try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3); try testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6); try testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0); } /// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and /// returns a slice. If there is a sentinel on the input type, there will be a /// sentinel on the output type. The constness of the output type matches /// the constness of the input type. `[*c]` pointers are assumed to be 0-terminated, /// and assumed to not allow null. pub fn Span(comptime T: type) type { switch (@typeInfo(T)) { .Optional => |optional_info| { return ?Span(optional_info.child); }, .Pointer => |ptr_info| { var new_ptr_info = ptr_info; switch (ptr_info.size) { .One => switch (@typeInfo(ptr_info.child)) { .Array => |info| { new_ptr_info.child = info.child; new_ptr_info.sentinel = info.sentinel; }, else => @compileError("invalid type given to std.mem.Span"), }, .C => { new_ptr_info.sentinel = 0; new_ptr_info.is_allowzero = false; }, .Many, .Slice => {}, } new_ptr_info.size = .Slice; return @Type(std.builtin.TypeInfo{ .Pointer = new_ptr_info }); }, else => @compileError("invalid type given to std.mem.Span"), } } test "Span" { try testing.expect(Span(*[5]u16) == []u16); try testing.expect(Span(?*[5]u16) == ?[]u16); try testing.expect(Span(*const [5]u16) == []const u16); try testing.expect(Span(?*const [5]u16) == ?[]const u16); try testing.expect(Span([]u16) == []u16); try testing.expect(Span(?[]u16) == ?[]u16); try testing.expect(Span([]const u8) == []const u8); try testing.expect(Span(?[]const u8) == ?[]const u8); try testing.expect(Span([:1]u16) == [:1]u16); try testing.expect(Span(?[:1]u16) == ?[:1]u16); try testing.expect(Span([:1]const u8) == [:1]const u8); try testing.expect(Span(?[:1]const u8) == ?[:1]const u8); try testing.expect(Span([*:1]u16) == [:1]u16); try testing.expect(Span(?[*:1]u16) == ?[:1]u16); try testing.expect(Span([*:1]const u8) == [:1]const u8); try testing.expect(Span(?[*:1]const u8) == ?[:1]const u8); try testing.expect(Span([*c]u16) == [:0]u16); try testing.expect(Span(?[*c]u16) == ?[:0]u16); try testing.expect(Span([*c]const u8) == [:0]const u8); try testing.expect(Span(?[*c]const u8) == ?[:0]const u8); } /// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and /// returns a slice. If there is a sentinel on the input type, there will be a /// sentinel on the output type. The constness of the output type matches /// the constness of the input type. /// /// When there is both a sentinel and an array length or slice length, the /// length value is used instead of the sentinel. pub fn span(ptr: anytype) Span(@TypeOf(ptr)) { if (@typeInfo(@TypeOf(ptr)) == .Optional) { if (ptr) |non_null| { return span(non_null); } else { return null; } } const Result = Span(@TypeOf(ptr)); const l = len(ptr); if (@typeInfo(Result).Pointer.sentinel) |s| { return ptr[0..l :s]; } else { return ptr[0..l]; } } test "span" { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; const ptr = @as([*:3]u16, array[0..2 :3]); try testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 })); try testing.expect(eql(u16, span(&array), &[_]u16{ 1, 2, 3, 4, 5 })); try testing.expectEqual(@as(?[:0]u16, null), span(@as(?[*:0]u16, null))); } pub const spanZ = @compileError("deprecated; use use std.mem.span() or std.mem.sliceTo()"); /// Helper for the return type of sliceTo() fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type { switch (@typeInfo(T)) { .Optional => |optional_info| { return ?SliceTo(optional_info.child, end); }, .Pointer => |ptr_info| { var new_ptr_info = ptr_info; new_ptr_info.size = .Slice; switch (ptr_info.size) { .One => switch (@typeInfo(ptr_info.child)) { .Array => |array_info| { new_ptr_info.child = array_info.child; // The return type must only be sentinel terminated if we are guaranteed // to find the value searched for, which is only the case if it matches // the sentinel of the type passed. if (array_info.sentinel) |sentinel| { if (end == sentinel) { new_ptr_info.sentinel = end; } else { new_ptr_info.sentinel = null; } } }, else => {}, }, .Many, .Slice => { // The return type must only be sentinel terminated if we are guaranteed // to find the value searched for, which is only the case if it matches // the sentinel of the type passed. if (ptr_info.sentinel) |sentinel| { if (end == sentinel) { new_ptr_info.sentinel = end; } else { new_ptr_info.sentinel = null; } } }, .C => { new_ptr_info.sentinel = end; // C pointers are always allowzero, but we don't want the return type to be. assert(new_ptr_info.is_allowzero); new_ptr_info.is_allowzero = false; }, } return @Type(std.builtin.TypeInfo{ .Pointer = new_ptr_info }); }, else => {}, } @compileError("invalid type given to std.mem.sliceTo: " ++ @typeName(T)); } /// Takes a pointer to an array, an array, a sentinel-terminated pointer, or a slice and /// iterates searching for the first occurrence of `end`, returning the scanned slice. /// If `end` is not found, the full length of the array/slice/sentinel terminated pointer is returned. /// If the pointer type is sentinel terminated and `end` matches that terminator, the /// resulting slice is also sentinel terminated. /// Pointer properties such as mutability and alignment are preserved. /// C pointers are assumed to be non-null. pub fn sliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) SliceTo(@TypeOf(ptr), end) { if (@typeInfo(@TypeOf(ptr)) == .Optional) { const non_null = ptr orelse return null; return sliceTo(non_null, end); } const Result = SliceTo(@TypeOf(ptr), end); const length = lenSliceTo(ptr, end); if (@typeInfo(Result).Pointer.sentinel) |s| { return ptr[0..length :s]; } else { return ptr[0..length]; } } test "sliceTo" { try testing.expectEqualSlices(u8, "aoeu", sliceTo("aoeu", 0)); { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; try testing.expectEqualSlices(u16, &array, sliceTo(&array, 0)); try testing.expectEqualSlices(u16, array[0..3], sliceTo(array[0..3], 0)); try testing.expectEqualSlices(u16, array[0..2], sliceTo(&array, 3)); try testing.expectEqualSlices(u16, array[0..2], sliceTo(array[0..3], 3)); const sentinel_ptr = @ptrCast([*:5]u16, &array); try testing.expectEqualSlices(u16, array[0..2], sliceTo(sentinel_ptr, 3)); try testing.expectEqualSlices(u16, array[0..4], sliceTo(sentinel_ptr, 99)); const optional_sentinel_ptr = @ptrCast(?[*:5]u16, &array); try testing.expectEqualSlices(u16, array[0..2], sliceTo(optional_sentinel_ptr, 3).?); try testing.expectEqualSlices(u16, array[0..4], sliceTo(optional_sentinel_ptr, 99).?); const c_ptr = @as([*c]u16, &array); try testing.expectEqualSlices(u16, array[0..2], sliceTo(c_ptr, 3)); const slice: []u16 = &array; try testing.expectEqualSlices(u16, array[0..2], sliceTo(slice, 3)); try testing.expectEqualSlices(u16, &array, sliceTo(slice, 99)); const sentinel_slice: [:5]u16 = array[0..4 :5]; try testing.expectEqualSlices(u16, array[0..2], sliceTo(sentinel_slice, 3)); try testing.expectEqualSlices(u16, array[0..4], sliceTo(sentinel_slice, 99)); } { var sentinel_array: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 }; try testing.expectEqualSlices(u16, sentinel_array[0..2], sliceTo(&sentinel_array, 3)); try testing.expectEqualSlices(u16, &sentinel_array, sliceTo(&sentinel_array, 0)); try testing.expectEqualSlices(u16, &sentinel_array, sliceTo(&sentinel_array, 99)); } try testing.expectEqual(@as(?[]u8, null), sliceTo(@as(?[]u8, null), 0)); } /// Private helper for sliceTo(). If you want the length, use sliceTo(foo, x).len fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize { switch (@typeInfo(@TypeOf(ptr))) { .Pointer => |ptr_info| switch (ptr_info.size) { .One => switch (@typeInfo(ptr_info.child)) { .Array => |array_info| { if (array_info.sentinel) |sentinel| { if (sentinel == end) { return indexOfSentinel(array_info.child, end, ptr); } } return indexOfScalar(array_info.child, ptr, end) orelse array_info.len; }, else => {}, }, .Many => if (ptr_info.sentinel) |sentinel| { // We may be looking for something other than the sentinel, // but iterating past the sentinel would be a bug so we need // to check for both. var i: usize = 0; while (ptr[i] != end and ptr[i] != sentinel) i += 1; return i; }, .C => { assert(ptr != null); return indexOfSentinel(ptr_info.child, end, ptr); }, .Slice => { if (ptr_info.sentinel) |sentinel| { if (sentinel == end) { return indexOfSentinel(ptr_info.child, sentinel, ptr); } } return indexOfScalar(ptr_info.child, ptr, end) orelse ptr.len; }, }, else => {}, } @compileError("invalid type given to std.mem.sliceTo: " ++ @typeName(@TypeOf(ptr))); } test "lenSliceTo" { try testing.expect(lenSliceTo("aoeu", 0) == 4); { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; try testing.expectEqual(@as(usize, 5), lenSliceTo(&array, 0)); try testing.expectEqual(@as(usize, 3), lenSliceTo(array[0..3], 0)); try testing.expectEqual(@as(usize, 2), lenSliceTo(&array, 3)); try testing.expectEqual(@as(usize, 2), lenSliceTo(array[0..3], 3)); const sentinel_ptr = @ptrCast([*:5]u16, &array); try testing.expectEqual(@as(usize, 2), lenSliceTo(sentinel_ptr, 3)); try testing.expectEqual(@as(usize, 4), lenSliceTo(sentinel_ptr, 99)); const c_ptr = @as([*c]u16, &array); try testing.expectEqual(@as(usize, 2), lenSliceTo(c_ptr, 3)); const slice: []u16 = &array; try testing.expectEqual(@as(usize, 2), lenSliceTo(slice, 3)); try testing.expectEqual(@as(usize, 5), lenSliceTo(slice, 99)); const sentinel_slice: [:5]u16 = array[0..4 :5]; try testing.expectEqual(@as(usize, 2), lenSliceTo(sentinel_slice, 3)); try testing.expectEqual(@as(usize, 4), lenSliceTo(sentinel_slice, 99)); } { var sentinel_array: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 }; try testing.expectEqual(@as(usize, 2), lenSliceTo(&sentinel_array, 3)); try testing.expectEqual(@as(usize, 5), lenSliceTo(&sentinel_array, 0)); try testing.expectEqual(@as(usize, 5), lenSliceTo(&sentinel_array, 99)); } } /// Takes a pointer to an array, an array, a vector, a sentinel-terminated pointer, /// a slice or a tuple, and returns the length. /// In the case of a sentinel-terminated array, it uses the array length. /// For C pointers it assumes it is a pointer-to-many with a 0 sentinel. pub fn len(value: anytype) usize { return switch (@typeInfo(@TypeOf(value))) { .Array => |info| info.len, .Vector => |info| info.len, .Pointer => |info| switch (info.size) { .One => switch (@typeInfo(info.child)) { .Array => value.len, else => @compileError("invalid type given to std.mem.len"), }, .Many => if (info.sentinel) |sentinel| indexOfSentinel(info.child, sentinel, value) else @compileError("length of pointer with no sentinel"), .C => { assert(value != null); return indexOfSentinel(info.child, 0, value); }, .Slice => value.len, }, .Struct => |info| if (info.is_tuple) { return info.fields.len; } else @compileError("invalid type given to std.mem.len"), else => @compileError("invalid type given to std.mem.len"), }; } test "len" { try testing.expect(len("aoeu") == 4); { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; try testing.expect(len(&array) == 5); try testing.expect(len(array[0..3]) == 3); array[2] = 0; const ptr = @as([*:0]u16, array[0..2 :0]); try testing.expect(len(ptr) == 2); } { var array: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 }; try testing.expect(len(&array) == 5); array[2] = 0; try testing.expect(len(&array) == 5); } { const vector: meta.Vector(2, u32) = [2]u32{ 1, 2 }; try testing.expect(len(vector) == 2); } { const tuple = .{ 1, 2 }; try testing.expect(len(tuple) == 2); try testing.expect(tuple[0] == 1); } } pub const lenZ = @compileError("deprecated; use std.mem.len() or std.mem.sliceTo().len"); pub fn indexOfSentinel(comptime Elem: type, comptime sentinel: Elem, ptr: [*:sentinel]const Elem) usize { var i: usize = 0; while (ptr[i] != sentinel) { i += 1; } return i; } /// Returns true if all elements in a slice are equal to the scalar value provided pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool { for (slice) |item| { if (item != scalar) return false; } return true; } pub const dupe = @compileError("deprecated; use `Allocator.dupe`"); pub const dupeZ = @compileError("deprecated; use `Allocator.dupeZ`"); /// Remove values from the beginning of a slice. pub fn trimLeft(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { var begin: usize = 0; while (begin < slice.len and indexOfScalar(T, values_to_strip, slice[begin]) != null) : (begin += 1) {} return slice[begin..]; } /// Remove values from the end of a slice. pub fn trimRight(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { var end: usize = slice.len; while (end > 0 and indexOfScalar(T, values_to_strip, slice[end - 1]) != null) : (end -= 1) {} return slice[0..end]; } /// Remove values from the beginning and end of a slice. pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { var begin: usize = 0; var end: usize = slice.len; while (begin < end and indexOfScalar(T, values_to_strip, slice[begin]) != null) : (begin += 1) {} while (end > begin and indexOfScalar(T, values_to_strip, slice[end - 1]) != null) : (end -= 1) {} return slice[begin..end]; } test "mem.trim" { try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n")); try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n")); try testing.expectEqualSlices(u8, "foo", trim(u8, " foo\n ", " \n")); try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n")); } /// Linear search for the index of a scalar value inside a slice. pub fn indexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { return indexOfScalarPos(T, slice, 0, value); } /// Linear search for the last index of a scalar value inside a slice. pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { var i: usize = slice.len; while (i != 0) { i -= 1; if (slice[i] == value) return i; } return null; } pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize { var i: usize = start_index; while (i < slice.len) : (i += 1) { if (slice[i] == value) return i; } return null; } pub fn indexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize { return indexOfAnyPos(T, slice, 0, values); } pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize { var i: usize = slice.len; while (i != 0) { i -= 1; for (values) |value| { if (slice[i] == value) return i; } } return null; } pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { var i: usize = start_index; while (i < slice.len) : (i += 1) { for (values) |value| { if (slice[i] == value) return i; } } return null; } pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { return indexOfPos(T, haystack, 0, needle); } /// Find the index in a slice of a sub-slice, searching from the end backwards. /// To start looking at a different index, slice the haystack first. /// Consider using `lastIndexOf` instead of this, which will automatically use a /// more sophisticated algorithm on larger inputs. pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize { var i: usize = haystack.len - needle.len; while (true) : (i -= 1) { if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i; if (i == 0) return null; } } /// Consider using `indexOfPos` instead of this, which will automatically use a /// more sophisticated algorithm on larger inputs. pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { var i: usize = start_index; const end = haystack.len - needle.len; while (i <= end) : (i += 1) { if (eql(T, haystack[i .. i + needle.len], needle)) return i; } return null; } fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void { for (table) |*c| { c.* = pattern.len; } var i: usize = pattern.len - 1; // The first item is intentionally ignored and the skip size will be pattern.len. // This is the standard way boyer-moore-horspool is implemented. while (i > 0) : (i -= 1) { table[pattern[i]] = i; } } fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { for (table) |*c| { c.* = pattern.len; } var i: usize = 0; // The last item is intentionally ignored and the skip size will be pattern.len. // This is the standard way boyer-moore-horspool is implemented. while (i < pattern.len - 1) : (i += 1) { table[pattern[i]] = pattern.len - 1 - i; } } /// Find the index in a slice of a sub-slice, searching from the end backwards. /// To start looking at a different index, slice the haystack first. /// Uses the Reverse boyer-moore-horspool algorithm on large inputs; /// `lastIndexOfLinear` on small inputs. pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { if (needle.len > haystack.len) return null; if (needle.len == 0) return haystack.len; if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 52 or needle.len <= 4) return lastIndexOfLinear(T, haystack, needle); const haystack_bytes = sliceAsBytes(haystack); const needle_bytes = sliceAsBytes(needle); var skip_table: [256]usize = undefined; boyerMooreHorspoolPreprocessReverse(needle_bytes, skip_table[0..]); var i: usize = haystack_bytes.len - needle_bytes.len; while (true) { if (i % @sizeOf(T) == 0 and mem.eql(u8, haystack_bytes[i .. i + needle_bytes.len], needle_bytes)) { return @divExact(i, @sizeOf(T)); } const skip = skip_table[haystack_bytes[i]]; if (skip > i) break; i -= skip; } return null; } /// Uses Boyer-moore-horspool algorithm on large inputs; `indexOfPosLinear` on small inputs. pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { if (needle.len > haystack.len) return null; if (needle.len == 0) return start_index; if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 52 or needle.len <= 4) return indexOfPosLinear(T, haystack, start_index, needle); const haystack_bytes = sliceAsBytes(haystack); const needle_bytes = sliceAsBytes(needle); var skip_table: [256]usize = undefined; boyerMooreHorspoolPreprocess(needle_bytes, skip_table[0..]); var i: usize = start_index * @sizeOf(T); while (i <= haystack_bytes.len - needle_bytes.len) { if (i % @sizeOf(T) == 0 and mem.eql(u8, haystack_bytes[i .. i + needle_bytes.len], needle_bytes)) { return @divExact(i, @sizeOf(T)); } i += skip_table[haystack_bytes[i + needle_bytes.len - 1]]; } return null; } test "mem.indexOf" { try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8); try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8); try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null); try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null); try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0); try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48); try testing.expect(indexOf(u8, "one two three four", "four").? == 14); try testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14); try testing.expect(indexOf(u8, "one two three four", "gour") == null); try testing.expect(lastIndexOf(u8, "one two three four", "gour") == null); try testing.expect(indexOf(u8, "foo", "foo").? == 0); try testing.expect(lastIndexOf(u8, "foo", "foo").? == 0); try testing.expect(indexOf(u8, "foo", "fool") == null); try testing.expect(lastIndexOf(u8, "foo", "lfoo") == null); try testing.expect(lastIndexOf(u8, "foo", "fool") == null); try testing.expect(indexOf(u8, "foo foo", "foo").? == 0); try testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4); try testing.expect(lastIndexOfAny(u8, "boo, cat", "abo").? == 6); try testing.expect(lastIndexOfScalar(u8, "boo", 'o').? == 2); } test "mem.indexOf multibyte" { { // make haystack and needle long enough to trigger boyer-moore-horspool algorithm const haystack = [1]u16{0} ** 100 ++ [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff }; const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needle), 100); // check for misaligned false positives (little and big endian) const needleLE = [_]u16{ 0xbbbb, 0xcccc, 0xdddd, 0xeeee, 0xffff }; try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needleLE), null); const needleBE = [_]u16{ 0xaacc, 0xbbdd, 0xccee, 0xddff, 0xee00 }; try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needleBE), null); } { // make haystack and needle long enough to trigger boyer-moore-horspool algorithm const haystack = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100; const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; try testing.expectEqual(lastIndexOf(u16, &haystack, &needle), 0); // check for misaligned false positives (little and big endian) const needleLE = [_]u16{ 0xbbbb, 0xcccc, 0xdddd, 0xeeee, 0xffff }; try testing.expectEqual(lastIndexOf(u16, &haystack, &needleLE), null); const needleBE = [_]u16{ 0xaacc, 0xbbdd, 0xccee, 0xddff, 0xee00 }; try testing.expectEqual(lastIndexOf(u16, &haystack, &needleBE), null); } } test "mem.indexOfPos empty needle" { try testing.expectEqual(indexOfPos(u8, "abracadabra", 5, ""), 5); } /// Returns the number of needles inside the haystack /// needle.len must be > 0 /// does not count overlapping needles pub fn count(comptime T: type, haystack: []const T, needle: []const T) usize { assert(needle.len > 0); var i: usize = 0; var found: usize = 0; while (indexOfPos(T, haystack, i, needle)) |idx| { i = idx + needle.len; found += 1; } return found; } test "mem.count" { try testing.expect(count(u8, "", "h") == 0); try testing.expect(count(u8, "h", "h") == 1); try testing.expect(count(u8, "hh", "h") == 2); try testing.expect(count(u8, "world!", "hello") == 0); try testing.expect(count(u8, "hello world!", "hello") == 1); try testing.expect(count(u8, " abcabc abc", "abc") == 3); try testing.expect(count(u8, "udexdcbvbruhasdrw", "bruh") == 1); try testing.expect(count(u8, "foo bar", "o bar") == 1); try testing.expect(count(u8, "foofoofoo", "foo") == 3); try testing.expect(count(u8, "fffffff", "ff") == 3); try testing.expect(count(u8, "owowowu", "owowu") == 1); } /// Returns true if the haystack contains expected_count or more needles /// needle.len must be > 0 /// does not count overlapping needles pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: usize, needle: []const T) bool { assert(needle.len > 0); if (expected_count == 0) return true; var i: usize = 0; var found: usize = 0; while (indexOfPos(T, haystack, i, needle)) |idx| { i = idx + needle.len; found += 1; if (found == expected_count) return true; } return false; } test "mem.containsAtLeast" { try testing.expect(containsAtLeast(u8, "aa", 0, "a")); try testing.expect(containsAtLeast(u8, "aa", 1, "a")); try testing.expect(containsAtLeast(u8, "aa", 2, "a")); try testing.expect(!containsAtLeast(u8, "aa", 3, "a")); try testing.expect(containsAtLeast(u8, "radaradar", 1, "radar")); try testing.expect(!containsAtLeast(u8, "radaradar", 2, "radar")); try testing.expect(containsAtLeast(u8, "radarradaradarradar", 3, "radar")); try testing.expect(!containsAtLeast(u8, "radarradaradarradar", 4, "radar")); try testing.expect(containsAtLeast(u8, " radar radar ", 2, "radar")); try testing.expect(!containsAtLeast(u8, " radar radar ", 3, "radar")); } /// Reads an integer from memory with size equal to bytes.len. /// T specifies the return type, which must be large enough to store /// the result. pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: Endian) ReturnType { var result: ReturnType = 0; switch (endian) { .Big => { for (bytes) |b| { result = (result << 8) | b; } }, .Little => { const ShiftType = math.Log2Int(ReturnType); for (bytes) |b, index| { result = result | (@as(ReturnType, b) << @intCast(ShiftType, index * 8)); } }, } return result; } /// Reads an integer from memory with bit count specified by T. /// The bit count of T must be evenly divisible by 8. /// This function cannot fail and cannot cause undefined behavior. /// Assumes the endianness of memory is native. This means the function can /// simply pointer cast memory. pub fn readIntNative(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8) T { return @ptrCast(*align(1) const T, bytes).*; } /// Reads an integer from memory with bit count specified by T. /// The bit count of T must be evenly divisible by 8. /// This function cannot fail and cannot cause undefined behavior. /// Assumes the endianness of memory is foreign, so it must byte-swap. pub fn readIntForeign(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8) T { return @byteSwap(T, readIntNative(T, bytes)); } pub const readIntLittle = switch (native_endian) { .Little => readIntNative, .Big => readIntForeign, }; pub const readIntBig = switch (native_endian) { .Little => readIntForeign, .Big => readIntNative, }; /// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0 /// and ignores extra bytes. /// The bit count of T must be evenly divisible by 8. /// Assumes the endianness of memory is native. This means the function can /// simply pointer cast memory. pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T { const n = @divExact(@typeInfo(T).Int.bits, 8); assert(bytes.len >= n); return readIntNative(T, bytes[0..n]); } /// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0 /// and ignores extra bytes. /// The bit count of T must be evenly divisible by 8. /// Assumes the endianness of memory is foreign, so it must byte-swap. pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T { return @byteSwap(T, readIntSliceNative(T, bytes)); } pub const readIntSliceLittle = switch (native_endian) { .Little => readIntSliceNative, .Big => readIntSliceForeign, }; pub const readIntSliceBig = switch (native_endian) { .Little => readIntSliceForeign, .Big => readIntSliceNative, }; /// Reads an integer from memory with bit count specified by T. /// The bit count of T must be evenly divisible by 8. /// This function cannot fail and cannot cause undefined behavior. pub fn readInt(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8, endian: Endian) T { if (endian == native_endian) { return readIntNative(T, bytes); } else { return readIntForeign(T, bytes); } } /// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0 /// and ignores extra bytes. /// The bit count of T must be evenly divisible by 8. pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: Endian) T { const n = @divExact(@typeInfo(T).Int.bits, 8); assert(bytes.len >= n); return readInt(T, bytes[0..n], endian); } test "comptime read/write int" { comptime { var bytes: [2]u8 = undefined; writeIntLittle(u16, &bytes, 0x1234); const result = readIntBig(u16, &bytes); try testing.expect(result == 0x3412); } comptime { var bytes: [2]u8 = undefined; writeIntBig(u16, &bytes, 0x1234); const result = readIntLittle(u16, &bytes); try testing.expect(result == 0x3412); } } test "readIntBig and readIntLittle" { try testing.expect(readIntSliceBig(u0, &[_]u8{}) == 0x0); try testing.expect(readIntSliceLittle(u0, &[_]u8{}) == 0x0); try testing.expect(readIntSliceBig(u8, &[_]u8{0x32}) == 0x32); try testing.expect(readIntSliceLittle(u8, &[_]u8{0x12}) == 0x12); try testing.expect(readIntSliceBig(u16, &[_]u8{ 0x12, 0x34 }) == 0x1234); try testing.expect(readIntSliceLittle(u16, &[_]u8{ 0x12, 0x34 }) == 0x3412); try testing.expect(readIntSliceBig(u72, &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024); try testing.expect(readIntSliceLittle(u72, &[_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec); try testing.expect(readIntSliceBig(i8, &[_]u8{0xff}) == -1); try testing.expect(readIntSliceLittle(i8, &[_]u8{0xfe}) == -2); try testing.expect(readIntSliceBig(i16, &[_]u8{ 0xff, 0xfd }) == -3); try testing.expect(readIntSliceLittle(i16, &[_]u8{ 0xfc, 0xff }) == -4); } /// Writes an integer to memory, storing it in twos-complement. /// This function always succeeds, has defined behavior for all inputs, and /// accepts any integer bit width. /// This function stores in native endian, which means it is implemented as a simple /// memory store. pub fn writeIntNative(comptime T: type, buf: *[(@typeInfo(T).Int.bits + 7) / 8]u8, value: T) void { @ptrCast(*align(1) T, buf).* = value; } /// Writes an integer to memory, storing it in twos-complement. /// This function always succeeds, has defined behavior for all inputs, but /// the integer bit width must be divisible by 8. /// This function stores in foreign endian, which means it does a @byteSwap first. pub fn writeIntForeign(comptime T: type, buf: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T) void { writeIntNative(T, buf, @byteSwap(T, value)); } pub const writeIntLittle = switch (native_endian) { .Little => writeIntNative, .Big => writeIntForeign, }; pub const writeIntBig = switch (native_endian) { .Little => writeIntForeign, .Big => writeIntNative, }; /// Writes an integer to memory, storing it in twos-complement. /// This function always succeeds, has defined behavior for all inputs, but /// the integer bit width must be divisible by 8. pub fn writeInt(comptime T: type, buffer: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T, endian: Endian) void { if (endian == native_endian) { return writeIntNative(T, buffer, value); } else { return writeIntForeign(T, buffer, value); } } /// Writes a twos-complement little-endian integer to memory. /// Asserts that buf.len >= @typeInfo(T).Int.bits / 8. /// The bit count of T must be divisible by 8. /// Any extra bytes in buffer after writing the integer are set to zero. To /// avoid the branch to check for extra buffer bytes, use writeIntLittle /// instead. pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void { assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8)); if (@typeInfo(T).Int.bits == 0) { return set(u8, buffer, 0); } else if (@typeInfo(T).Int.bits == 8) { set(u8, buffer, 0); buffer[0] = @bitCast(u8, value); return; } // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); var bits = @bitCast(uint, value); for (buffer) |*b| { b.* = @truncate(u8, bits); bits >>= 8; } } /// Writes a twos-complement big-endian integer to memory. /// Asserts that buffer.len >= @typeInfo(T).Int.bits / 8. /// The bit count of T must be divisible by 8. /// Any extra bytes in buffer before writing the integer are set to zero. To /// avoid the branch to check for extra buffer bytes, use writeIntBig instead. pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void { assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8)); if (@typeInfo(T).Int.bits == 0) { return set(u8, buffer, 0); } else if (@typeInfo(T).Int.bits == 8) { set(u8, buffer, 0); buffer[buffer.len - 1] = @bitCast(u8, value); return; } // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); var bits = @bitCast(uint, value); var index: usize = buffer.len; while (index != 0) { index -= 1; buffer[index] = @truncate(u8, bits); bits >>= 8; } } pub const writeIntSliceNative = switch (native_endian) { .Little => writeIntSliceLittle, .Big => writeIntSliceBig, }; pub const writeIntSliceForeign = switch (native_endian) { .Little => writeIntSliceBig, .Big => writeIntSliceLittle, }; /// Writes a twos-complement integer to memory, with the specified endianness. /// Asserts that buf.len >= @typeInfo(T).Int.bits / 8. /// The bit count of T must be evenly divisible by 8. /// Any extra bytes in buffer not part of the integer are set to zero, with /// respect to endianness. To avoid the branch to check for extra buffer bytes, /// use writeInt instead. pub fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: Endian) void { comptime assert(@typeInfo(T).Int.bits % 8 == 0); return switch (endian) { .Little => writeIntSliceLittle(T, buffer, value), .Big => writeIntSliceBig(T, buffer, value), }; } test "writeIntBig and writeIntLittle" { var buf0: [0]u8 = undefined; var buf1: [1]u8 = undefined; var buf2: [2]u8 = undefined; var buf9: [9]u8 = undefined; writeIntBig(u0, &buf0, 0x0); try testing.expect(eql(u8, buf0[0..], &[_]u8{})); writeIntLittle(u0, &buf0, 0x0); try testing.expect(eql(u8, buf0[0..], &[_]u8{})); writeIntBig(u8, &buf1, 0x12); try testing.expect(eql(u8, buf1[0..], &[_]u8{0x12})); writeIntLittle(u8, &buf1, 0x34); try testing.expect(eql(u8, buf1[0..], &[_]u8{0x34})); writeIntBig(u16, &buf2, 0x1234); try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0x12, 0x34 })); writeIntLittle(u16, &buf2, 0x5678); try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0x78, 0x56 })); writeIntBig(u72, &buf9, 0x123456789abcdef024); try testing.expect(eql(u8, buf9[0..], &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); writeIntLittle(u72, &buf9, 0xfedcba9876543210ec); try testing.expect(eql(u8, buf9[0..], &[_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); writeIntBig(i8, &buf1, -1); try testing.expect(eql(u8, buf1[0..], &[_]u8{0xff})); writeIntLittle(i8, &buf1, -2); try testing.expect(eql(u8, buf1[0..], &[_]u8{0xfe})); writeIntBig(i16, &buf2, -3); try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xff, 0xfd })); writeIntLittle(i16, &buf2, -4); try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xfc, 0xff })); } /// Swap the byte order of all the members of the fields of a struct /// (Changing their endianess) pub fn bswapAllFields(comptime S: type, ptr: *S) void { if (@typeInfo(S) != .Struct) @compileError("bswapAllFields expects a struct as the first argument"); inline for (std.meta.fields(S)) |f| { @field(ptr, f.name) = @byteSwap(f.field_type, @field(ptr, f.name)); } } test "bswapAllFields" { const T = extern struct { f0: u8, f1: u16, f2: u32, }; var s = T{ .f0 = 0x12, .f1 = 0x1234, .f2 = 0x12345678, }; bswapAllFields(T, &s); try std.testing.expectEqual(T{ .f0 = 0x12, .f1 = 0x3412, .f2 = 0x78563412, }, s); } /// Returns an iterator that iterates over the slices of `buffer` that are not /// any of the bytes in `delimiter_bytes`. /// tokenize(u8, " abc def ghi ", " ") /// Will return slices for "abc", "def", "ghi", null, in that order. /// If `buffer` is empty, the iterator will return null. /// If `delimiter_bytes` does not exist in buffer, /// the iterator will return `buffer`, null, in that order. /// See also the related function `split`. pub fn tokenize(comptime T: type, buffer: []const T, delimiter_bytes: []const T) TokenIterator(T) { return .{ .index = 0, .buffer = buffer, .delimiter_bytes = delimiter_bytes, }; } test "mem.tokenize" { var it = tokenize(u8, " abc def ghi ", " "); try testing.expect(eql(u8, it.next().?, "abc")); try testing.expect(eql(u8, it.next().?, "def")); try testing.expect(eql(u8, it.next().?, "ghi")); try testing.expect(it.next() == null); it = tokenize(u8, "..\\bob", "\\"); try testing.expect(eql(u8, it.next().?, "..")); try testing.expect(eql(u8, "..", "..\\bob"[0..it.index])); try testing.expect(eql(u8, it.next().?, "bob")); try testing.expect(it.next() == null); it = tokenize(u8, "//a/b", "/"); try testing.expect(eql(u8, it.next().?, "a")); try testing.expect(eql(u8, it.next().?, "b")); try testing.expect(eql(u8, "//a/b", "//a/b"[0..it.index])); try testing.expect(it.next() == null); it = tokenize(u8, "|", "|"); try testing.expect(it.next() == null); it = tokenize(u8, "", "|"); try testing.expect(it.next() == null); it = tokenize(u8, "hello", ""); try testing.expect(eql(u8, it.next().?, "hello")); try testing.expect(it.next() == null); it = tokenize(u8, "hello", " "); try testing.expect(eql(u8, it.next().?, "hello")); try testing.expect(it.next() == null); var it16 = tokenize( u16, std.unicode.utf8ToUtf16LeStringLiteral("hello"), std.unicode.utf8ToUtf16LeStringLiteral(" "), ); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("hello"))); try testing.expect(it16.next() == null); } test "mem.tokenize (multibyte)" { var it = tokenize(u8, "a|b,c/d e", " /,|"); try testing.expect(eql(u8, it.next().?, "a")); try testing.expect(eql(u8, it.next().?, "b")); try testing.expect(eql(u8, it.next().?, "c")); try testing.expect(eql(u8, it.next().?, "d")); try testing.expect(eql(u8, it.next().?, "e")); try testing.expect(it.next() == null); var it16 = tokenize( u16, std.unicode.utf8ToUtf16LeStringLiteral("a|b,c/d e"), std.unicode.utf8ToUtf16LeStringLiteral(" /,|"), ); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("a"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("b"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("c"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("d"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("e"))); try testing.expect(it16.next() == null); } test "mem.tokenize (reset)" { var it = tokenize(u8, " abc def ghi ", " "); try testing.expect(eql(u8, it.next().?, "abc")); try testing.expect(eql(u8, it.next().?, "def")); try testing.expect(eql(u8, it.next().?, "ghi")); it.reset(); try testing.expect(eql(u8, it.next().?, "abc")); try testing.expect(eql(u8, it.next().?, "def")); try testing.expect(eql(u8, it.next().?, "ghi")); try testing.expect(it.next() == null); } /// Returns an iterator that iterates over the slices of `buffer` that /// are separated by bytes in `delimiter`. /// split(u8, "abc|def||ghi", "|") /// will return slices for "abc", "def", "", "ghi", null, in that order. /// If `delimiter` does not exist in buffer, /// the iterator will return `buffer`, null, in that order. /// The delimiter length must not be zero. /// See also the related function `tokenize`. pub fn split(comptime T: type, buffer: []const T, delimiter: []const T) SplitIterator(T) { assert(delimiter.len != 0); return .{ .index = 0, .buffer = buffer, .delimiter = delimiter, }; } test "mem.split" { var it = split(u8, "abc|def||ghi", "|"); try testing.expect(eql(u8, it.next().?, "abc")); try testing.expect(eql(u8, it.next().?, "def")); try testing.expect(eql(u8, it.next().?, "")); try testing.expect(eql(u8, it.next().?, "ghi")); try testing.expect(it.next() == null); it = split(u8, "", "|"); try testing.expect(eql(u8, it.next().?, "")); try testing.expect(it.next() == null); it = split(u8, "|", "|"); try testing.expect(eql(u8, it.next().?, "")); try testing.expect(eql(u8, it.next().?, "")); try testing.expect(it.next() == null); it = split(u8, "hello", " "); try testing.expect(eql(u8, it.next().?, "hello")); try testing.expect(it.next() == null); var it16 = split( u16, std.unicode.utf8ToUtf16LeStringLiteral("hello"), std.unicode.utf8ToUtf16LeStringLiteral(" "), ); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("hello"))); try testing.expect(it16.next() == null); } test "mem.split (multibyte)" { var it = split(u8, "a, b ,, c, d, e", ", "); try testing.expect(eql(u8, it.next().?, "a")); try testing.expect(eql(u8, it.next().?, "b ,")); try testing.expect(eql(u8, it.next().?, "c")); try testing.expect(eql(u8, it.next().?, "d")); try testing.expect(eql(u8, it.next().?, "e")); try testing.expect(it.next() == null); var it16 = split( u16, std.unicode.utf8ToUtf16LeStringLiteral("a, b ,, c, d, e"), std.unicode.utf8ToUtf16LeStringLiteral(", "), ); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("a"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("b ,"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("c"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("d"))); try testing.expect(eql(u16, it16.next().?, std.unicode.utf8ToUtf16LeStringLiteral("e"))); try testing.expect(it16.next() == null); } pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool { return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle); } test "mem.startsWith" { try testing.expect(startsWith(u8, "Bob", "Bo")); try testing.expect(!startsWith(u8, "Needle in haystack", "haystack")); } pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool { return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle); } test "mem.endsWith" { try testing.expect(endsWith(u8, "Needle in haystack", "haystack")); try testing.expect(!endsWith(u8, "Bob", "Bo")); } pub fn TokenIterator(comptime T: type) type { return struct { buffer: []const T, delimiter_bytes: []const T, index: usize, const Self = @This(); /// Returns a slice of the next token, or null if tokenization is complete. pub fn next(self: *Self) ?[]const T { // move to beginning of token while (self.index < self.buffer.len and self.isSplitByte(self.buffer[self.index])) : (self.index += 1) {} const start = self.index; if (start == self.buffer.len) { return null; } // move to end of token while (self.index < self.buffer.len and !self.isSplitByte(self.buffer[self.index])) : (self.index += 1) {} const end = self.index; return self.buffer[start..end]; } /// Returns a slice of the remaining bytes. Does not affect iterator state. pub fn rest(self: Self) []const T { // move to beginning of token var index: usize = self.index; while (index < self.buffer.len and self.isSplitByte(self.buffer[index])) : (index += 1) {} return self.buffer[index..]; } /// Resets the iterator to the initial token. pub fn reset(self: *Self) void { self.index = 0; } fn isSplitByte(self: Self, byte: T) bool { for (self.delimiter_bytes) |delimiter_byte| { if (byte == delimiter_byte) { return true; } } return false; } }; } pub fn SplitIterator(comptime T: type) type { return struct { buffer: []const T, index: ?usize, delimiter: []const T, const Self = @This(); /// Returns a slice of the next field, or null if splitting is complete. pub fn next(self: *Self) ?[]const T { const start = self.index orelse return null; const end = if (indexOfPos(T, self.buffer, start, self.delimiter)) |delim_start| blk: { self.index = delim_start + self.delimiter.len; break :blk delim_start; } else blk: { self.index = null; break :blk self.buffer.len; }; return self.buffer[start..end]; } /// Returns a slice of the remaining bytes. Does not affect iterator state. pub fn rest(self: Self) []const T { const end = self.buffer.len; const start = self.index orelse end; return self.buffer[start..end]; } }; } /// Naively combines a series of slices with a separator. /// Allocates memory for the result, which must be freed by the caller. pub fn join(allocator: Allocator, separator: []const u8, slices: []const []const u8) ![]u8 { return joinMaybeZ(allocator, separator, slices, false); } /// Naively combines a series of slices with a separator and null terminator. /// Allocates memory for the result, which must be freed by the caller. pub fn joinZ(allocator: Allocator, separator: []const u8, slices: []const []const u8) ![:0]u8 { const out = try joinMaybeZ(allocator, separator, slices, true); return out[0 .. out.len - 1 :0]; } fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []const u8, zero: bool) ![]u8 { if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{}; const total_len = blk: { var sum: usize = separator.len * (slices.len - 1); for (slices) |slice| sum += slice.len; if (zero) sum += 1; break :blk sum; }; const buf = try allocator.alloc(u8, total_len); errdefer allocator.free(buf); copy(u8, buf, slices[0]); var buf_index: usize = slices[0].len; for (slices[1..]) |slice| { copy(u8, buf[buf_index..], separator); buf_index += separator.len; copy(u8, buf[buf_index..], slice); buf_index += slice.len; } if (zero) buf[buf.len - 1] = 0; // No need for shrink since buf is exactly the correct size. return buf; } test "mem.join" { { const str = try join(testing.allocator, ",", &[_][]const u8{}); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "")); } { const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" }); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "a,b,c")); } { const str = try join(testing.allocator, ",", &[_][]const u8{"a"}); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "a")); } { const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "", "b", "", "c" }); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "a,,b,,c")); } } test "mem.joinZ" { { const str = try joinZ(testing.allocator, ",", &[_][]const u8{}); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "")); try testing.expectEqual(str[str.len], 0); } { const str = try joinZ(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" }); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "a,b,c")); try testing.expectEqual(str[str.len], 0); } { const str = try joinZ(testing.allocator, ",", &[_][]const u8{"a"}); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "a")); try testing.expectEqual(str[str.len], 0); } { const str = try joinZ(testing.allocator, ",", &[_][]const u8{ "a", "", "b", "", "c" }); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "a,,b,,c")); try testing.expectEqual(str[str.len], 0); } } /// Copies each T from slices into a new slice that exactly holds all the elements. pub fn concat(allocator: Allocator, comptime T: type, slices: []const []const T) ![]T { if (slices.len == 0) return &[0]T{}; const total_len = blk: { var sum: usize = 0; for (slices) |slice| { sum += slice.len; } break :blk sum; }; const buf = try allocator.alloc(T, total_len); errdefer allocator.free(buf); var buf_index: usize = 0; for (slices) |slice| { copy(T, buf[buf_index..], slice); buf_index += slice.len; } // No need for shrink since buf is exactly the correct size. return buf; } test "concat" { { const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" }); defer testing.allocator.free(str); try testing.expect(eql(u8, str, "abcdefghi")); } { const str = try concat(testing.allocator, u32, &[_][]const u32{ &[_]u32{ 0, 1 }, &[_]u32{ 2, 3, 4 }, &[_]u32{}, &[_]u32{5}, }); defer testing.allocator.free(str); try testing.expect(eql(u32, str, &[_]u32{ 0, 1, 2, 3, 4, 5 })); } } test "testStringEquality" { try testing.expect(eql(u8, "abcd", "abcd")); try testing.expect(!eql(u8, "abcdef", "abZdef")); try testing.expect(!eql(u8, "abcdefg", "abcdef")); } test "testReadInt" { try testReadIntImpl(); comptime try testReadIntImpl(); } fn testReadIntImpl() !void { { const bytes = [_]u8{ 0x12, 0x34, 0x56, 0x78, }; try testing.expect(readInt(u32, &bytes, Endian.Big) == 0x12345678); try testing.expect(readIntBig(u32, &bytes) == 0x12345678); try testing.expect(readIntBig(i32, &bytes) == 0x12345678); try testing.expect(readInt(u32, &bytes, Endian.Little) == 0x78563412); try testing.expect(readIntLittle(u32, &bytes) == 0x78563412); try testing.expect(readIntLittle(i32, &bytes) == 0x78563412); } { const buf = [_]u8{ 0x00, 0x00, 0x12, 0x34, }; const answer = readInt(u32, &buf, Endian.Big); try testing.expect(answer == 0x00001234); } { const buf = [_]u8{ 0x12, 0x34, 0x00, 0x00, }; const answer = readInt(u32, &buf, Endian.Little); try testing.expect(answer == 0x00003412); } { const bytes = [_]u8{ 0xff, 0xfe, }; try testing.expect(readIntBig(u16, &bytes) == 0xfffe); try testing.expect(readIntBig(i16, &bytes) == -0x0002); try testing.expect(readIntLittle(u16, &bytes) == 0xfeff); try testing.expect(readIntLittle(i16, &bytes) == -0x0101); } } test "writeIntSlice" { try testWriteIntImpl(); comptime try testWriteIntImpl(); } fn testWriteIntImpl() !void { var bytes: [8]u8 = undefined; writeIntSlice(u0, bytes[0..], 0, Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(u0, bytes[0..], 0, Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(u64, bytes[0..], 0x12345678CAFEBABE, Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0xCA, 0xFE, 0xBA, 0xBE, })); writeIntSlice(u64, bytes[0..], 0xBEBAFECA78563412, Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0xCA, 0xFE, 0xBA, 0xBE, })); writeIntSlice(u32, bytes[0..], 0x12345678, Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, })); writeIntSlice(u32, bytes[0..], 0x78563412, Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(u16, bytes[0..], 0x1234, Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, })); writeIntSlice(u16, bytes[0..], 0x1234, Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0xCD, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAB, 0xCD, })); writeIntSlice(u8, bytes[0..], 0x12, Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, })); writeIntSlice(u8, bytes[0..], 0x12, Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(i8, bytes[0..], -1, Endian.Big); try testing.expect(eql(u8, &bytes, &[_]u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, })); writeIntSlice(i8, bytes[0..], -1, Endian.Little); try testing.expect(eql(u8, &bytes, &[_]u8{ 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); } /// Returns the smallest number in a slice. O(n). /// `slice` must not be empty. pub fn min(comptime T: type, slice: []const T) T { var best = slice[0]; for (slice[1..]) |item| { best = math.min(best, item); } return best; } test "mem.min" { try testing.expect(min(u8, "abcdefg") == 'a'); } /// Returns the largest number in a slice. O(n). /// `slice` must not be empty. pub fn max(comptime T: type, slice: []const T) T { var best = slice[0]; for (slice[1..]) |item| { best = math.max(best, item); } return best; } test "mem.max" { try testing.expect(max(u8, "abcdefg") == 'g'); } pub fn swap(comptime T: type, a: *T, b: *T) void { const tmp = a.*; a.* = b.*; b.* = tmp; } /// In-place order reversal of a slice pub fn reverse(comptime T: type, items: []T) void { var i: usize = 0; const end = items.len / 2; while (i < end) : (i += 1) { swap(T, &items[i], &items[items.len - i - 1]); } } test "reverse" { var arr = [_]i32{ 5, 3, 1, 2, 4 }; reverse(i32, arr[0..]); try testing.expect(eql(i32, &arr, &[_]i32{ 4, 2, 1, 3, 5 })); } /// In-place rotation of the values in an array ([0 1 2 3] becomes [1 2 3 0] if we rotate by 1) /// Assumes 0 <= amount <= items.len pub fn rotate(comptime T: type, items: []T, amount: usize) void { reverse(T, items[0..amount]); reverse(T, items[amount..]); reverse(T, items); } test "rotate" { var arr = [_]i32{ 5, 3, 1, 2, 4 }; rotate(i32, arr[0..], 2); try testing.expect(eql(i32, &arr, &[_]i32{ 1, 2, 4, 5, 3 })); } /// Replace needle with replacement as many times as possible, writing to an output buffer which is assumed to be of /// appropriate size. Use replacementSize to calculate an appropriate buffer size. /// The needle must not be empty. pub fn replace(comptime T: type, input: []const T, needle: []const T, replacement: []const T, output: []T) usize { // Empty needle will loop until output buffer overflows. assert(needle.len > 0); var i: usize = 0; var slide: usize = 0; var replacements: usize = 0; while (slide < input.len) { if (mem.startsWith(T, input[slide..], needle)) { mem.copy(T, output[i .. i + replacement.len], replacement); i += replacement.len; slide += needle.len; replacements += 1; } else { output[i] = input[slide]; i += 1; slide += 1; } } return replacements; } test "replace" { var output: [29]u8 = undefined; var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]); var expected: []const u8 = "All your Zig are belong to us"; try testing.expect(replacements == 1); try testing.expectEqualStrings(expected, output[0..expected.len]); replacements = replace(u8, "Favor reading code over writing code.", "code", "", output[0..]); expected = "Favor reading over writing ."; try testing.expect(replacements == 2); try testing.expectEqualStrings(expected, output[0..expected.len]); // Empty needle is not allowed but input may be empty. replacements = replace(u8, "", "x", "y", output[0..0]); expected = ""; try testing.expect(replacements == 0); try testing.expectEqualStrings(expected, output[0..expected.len]); // Adjacent replacements. replacements = replace(u8, "\\n\\n", "\\n", "\n", output[0..]); expected = "\n\n"; try testing.expect(replacements == 2); try testing.expectEqualStrings(expected, output[0..expected.len]); replacements = replace(u8, "abbba", "b", "cd", output[0..]); expected = "acdcdcda"; try testing.expect(replacements == 3); try testing.expectEqualStrings(expected, output[0..expected.len]); } /// Replace all occurences of `needle` with `replacement`. pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) void { for (slice) |e, i| { if (e == needle) { slice[i] = replacement; } } } /// Collapse consecutive duplicate elements into one entry. pub fn collapseRepeatsLen(comptime T: type, slice: []T, elem: T) usize { if (slice.len == 0) return 0; var write_idx: usize = 1; var read_idx: usize = 1; while (read_idx < slice.len) : (read_idx += 1) { if (slice[read_idx - 1] != elem or slice[read_idx] != elem) { slice[write_idx] = slice[read_idx]; write_idx += 1; } } return write_idx; } /// Collapse consecutive duplicate elements into one entry. pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) []T { return slice[0..collapseRepeatsLen(T, slice, elem)]; } fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void { const mutable = try std.testing.allocator.dupe(u8, str); defer std.testing.allocator.free(mutable); try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected)); } test "collapseRepeats" { try testCollapseRepeats("", '/', ""); try testCollapseRepeats("a", '/', "a"); try testCollapseRepeats("/", '/', "/"); try testCollapseRepeats("//", '/', "/"); try testCollapseRepeats("/a", '/', "/a"); try testCollapseRepeats("//a", '/', "/a"); try testCollapseRepeats("a/", '/', "a/"); try testCollapseRepeats("a//", '/', "a/"); try testCollapseRepeats("a/a", '/', "a/a"); try testCollapseRepeats("a//a", '/', "a/a"); try testCollapseRepeats("//a///a////", '/', "/a/a/"); } /// Calculate the size needed in an output buffer to perform a replacement. /// The needle must not be empty. pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, replacement: []const T) usize { // Empty needle will loop forever. assert(needle.len > 0); var i: usize = 0; var size: usize = input.len; while (i < input.len) { if (mem.startsWith(T, input[i..], needle)) { size = size - needle.len + replacement.len; i += needle.len; } else { i += 1; } } return size; } test "replacementSize" { try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29); try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29); try testing.expect(replacementSize(u8, "Only one obvious way to do things.", "things.", "things in Zig.") == 41); // Empty needle is not allowed but input may be empty. try testing.expect(replacementSize(u8, "", "x", "y") == 0); // Adjacent replacements. try testing.expect(replacementSize(u8, "\\n\\n", "\\n", "\n") == 2); try testing.expect(replacementSize(u8, "abbba", "b", "cd") == 8); } /// Perform a replacement on an allocated buffer of pre-determined size. Caller must free returned memory. pub fn replaceOwned(comptime T: type, allocator: Allocator, input: []const T, needle: []const T, replacement: []const T) Allocator.Error![]T { var output = try allocator.alloc(T, replacementSize(T, input, needle, replacement)); _ = replace(T, input, needle, replacement, output); return output; } test "replaceOwned" { const gpa = std.testing.allocator; const base_replace = replaceOwned(u8, gpa, "All your base are belong to us", "base", "Zig") catch @panic("out of memory"); defer gpa.free(base_replace); try testing.expect(eql(u8, base_replace, "All your Zig are belong to us")); const zen_replace = replaceOwned(u8, gpa, "Favor reading code over writing code.", " code", "") catch @panic("out of memory"); defer gpa.free(zen_replace); try testing.expect(eql(u8, zen_replace, "Favor reading over writing.")); } /// Converts a little-endian integer to host endianness. pub fn littleToNative(comptime T: type, x: T) T { return switch (native_endian) { .Little => x, .Big => @byteSwap(T, x), }; } /// Converts a big-endian integer to host endianness. pub fn bigToNative(comptime T: type, x: T) T { return switch (native_endian) { .Little => @byteSwap(T, x), .Big => x, }; } /// Converts an integer from specified endianness to host endianness. pub fn toNative(comptime T: type, x: T, endianness_of_x: Endian) T { return switch (endianness_of_x) { .Little => littleToNative(T, x), .Big => bigToNative(T, x), }; } /// Converts an integer which has host endianness to the desired endianness. pub fn nativeTo(comptime T: type, x: T, desired_endianness: Endian) T { return switch (desired_endianness) { .Little => nativeToLittle(T, x), .Big => nativeToBig(T, x), }; } /// Converts an integer which has host endianness to little endian. pub fn nativeToLittle(comptime T: type, x: T) T { return switch (native_endian) { .Little => x, .Big => @byteSwap(T, x), }; } /// Converts an integer which has host endianness to big endian. pub fn nativeToBig(comptime T: type, x: T) T { return switch (native_endian) { .Little => @byteSwap(T, x), .Big => x, }; } /// Returns the number of elements that, if added to the given pointer, align it /// to a multiple of the given quantity, or `null` if one of the following /// conditions is met: /// - The aligned pointer would not fit the address space, /// - The delta required to align the pointer is not a multiple of the pointee's /// type. pub fn alignPointerOffset(ptr: anytype, align_to: u29) ?usize { assert(align_to != 0 and @popCount(u29, align_to) == 1); const T = @TypeOf(ptr); const info = @typeInfo(T); if (info != .Pointer or info.Pointer.size != .Many) @compileError("expected many item pointer, got " ++ @typeName(T)); // Do nothing if the pointer is already well-aligned. if (align_to <= info.Pointer.alignment) return 0; // Calculate the aligned base address with an eye out for overflow. const addr = @ptrToInt(ptr); var new_addr: usize = undefined; if (@addWithOverflow(usize, addr, align_to - 1, &new_addr)) return null; new_addr &= ~@as(usize, align_to - 1); // The delta is expressed in terms of bytes, turn it into a number of child // type elements. const delta = new_addr - addr; const pointee_size = @sizeOf(info.Pointer.child); if (delta % pointee_size != 0) return null; return delta / pointee_size; } /// Aligns a given pointer value to a specified alignment factor. /// Returns an aligned pointer or null if one of the following conditions is /// met: /// - The aligned pointer would not fit the address space, /// - The delta required to align the pointer is not a multiple of the pointee's /// type. pub fn alignPointer(ptr: anytype, align_to: u29) ?@TypeOf(ptr) { const adjust_off = alignPointerOffset(ptr, align_to) orelse return null; const T = @TypeOf(ptr); // Avoid the use of intToPtr to avoid losing the pointer provenance info. return @alignCast(@typeInfo(T).Pointer.alignment, ptr + adjust_off); } test "alignPointer" { const S = struct { fn checkAlign(comptime T: type, base: usize, align_to: u29, expected: usize) !void { var ptr = @intToPtr(T, base); var aligned = alignPointer(ptr, align_to); try testing.expectEqual(expected, @ptrToInt(aligned)); } }; try S.checkAlign([*]u8, 0x123, 0x200, 0x200); try S.checkAlign([*]align(4) u8, 0x10, 2, 0x10); try S.checkAlign([*]u32, 0x10, 2, 0x10); try S.checkAlign([*]u32, 0x4, 16, 0x10); // Misaligned. try S.checkAlign([*]align(1) u32, 0x3, 2, 0); // Overflow. try S.checkAlign([*]u32, math.maxInt(usize) - 3, 8, 0); } fn CopyPtrAttrs(comptime source: type, comptime size: std.builtin.TypeInfo.Pointer.Size, comptime child: type) type { const info = @typeInfo(source).Pointer; return @Type(.{ .Pointer = .{ .size = size, .is_const = info.is_const, .is_volatile = info.is_volatile, .is_allowzero = info.is_allowzero, .alignment = info.alignment, .address_space = info.address_space, .child = child, .sentinel = null, }, }); } fn AsBytesReturnType(comptime P: type) type { if (!trait.isSingleItemPtr(P)) @compileError("expected single item pointer, passed " ++ @typeName(P)); const size = @sizeOf(meta.Child(P)); return CopyPtrAttrs(P, .One, [size]u8); } /// Given a pointer to a single item, returns a slice of the underlying bytes, preserving pointer attributes. pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) { const P = @TypeOf(ptr); return @ptrCast(AsBytesReturnType(P), ptr); } test "asBytes" { const deadbeef = @as(u32, 0xDEADBEEF); const deadbeef_bytes = switch (native_endian) { .Big => "\xDE\xAD\xBE\xEF", .Little => "\xEF\xBE\xAD\xDE", }; try testing.expect(eql(u8, asBytes(&deadbeef), deadbeef_bytes)); var codeface = @as(u32, 0xC0DEFACE); for (asBytes(&codeface).*) |*b| b.* = 0; try testing.expect(codeface == 0); const S = packed struct { a: u8, b: u8, c: u8, d: u8, }; const inst = S{ .a = 0xBE, .b = 0xEF, .c = 0xDE, .d = 0xA1, }; try testing.expect(eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); const ZST = struct {}; const zero = ZST{}; try testing.expect(eql(u8, asBytes(&zero), "")); } test "asBytes preserves pointer attributes" { const inArr: u32 align(16) = 0xDEADBEEF; const inPtr = @ptrCast(*align(16) const volatile u32, &inArr); const outSlice = asBytes(inPtr); const in = @typeInfo(@TypeOf(inPtr)).Pointer; const out = @typeInfo(@TypeOf(outSlice)).Pointer; try testing.expectEqual(in.is_const, out.is_const); try testing.expectEqual(in.is_volatile, out.is_volatile); try testing.expectEqual(in.is_allowzero, out.is_allowzero); try testing.expectEqual(in.alignment, out.alignment); } /// Given any value, returns a copy of its bytes in an array. pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 { return asBytes(&value).*; } test "toBytes" { var my_bytes = toBytes(@as(u32, 0x12345678)); switch (native_endian) { .Big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")), .Little => try testing.expect(eql(u8, &my_bytes, "\x78\x56\x34\x12")), } my_bytes[0] = '\x99'; switch (native_endian) { .Big => try testing.expect(eql(u8, &my_bytes, "\x99\x34\x56\x78")), .Little => try testing.expect(eql(u8, &my_bytes, "\x99\x56\x34\x12")), } } fn BytesAsValueReturnType(comptime T: type, comptime B: type) type { const size = @as(usize, @sizeOf(T)); if (comptime !trait.is(.Pointer)(B) or (meta.Child(B) != [size]u8 and meta.Child(B) != [size:0]u8)) { comptime var buf: [100]u8 = undefined; @compileError(std.fmt.bufPrint(&buf, "expected *[{}]u8, passed " ++ @typeName(B), .{size}) catch unreachable); } return CopyPtrAttrs(B, .One, T); } /// Given a pointer to an array of bytes, returns a pointer to a value of the specified type /// backed by those bytes, preserving pointer attributes. pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) { return @ptrCast(BytesAsValueReturnType(T, @TypeOf(bytes)), bytes); } test "bytesAsValue" { const deadbeef = @as(u32, 0xDEADBEEF); const deadbeef_bytes = switch (native_endian) { .Big => "\xDE\xAD\xBE\xEF", .Little => "\xEF\xBE\xAD\xDE", }; try testing.expect(deadbeef == bytesAsValue(u32, deadbeef_bytes).*); var codeface_bytes: [4]u8 = switch (native_endian) { .Big => "\xC0\xDE\xFA\xCE", .Little => "\xCE\xFA\xDE\xC0", }.*; var codeface = bytesAsValue(u32, &codeface_bytes); try testing.expect(codeface.* == 0xC0DEFACE); codeface.* = 0; for (codeface_bytes) |b| try testing.expect(b == 0); const S = packed struct { a: u8, b: u8, c: u8, d: u8, }; const inst = S{ .a = 0xBE, .b = 0xEF, .c = 0xDE, .d = 0xA1, }; const inst_bytes = "\xBE\xEF\xDE\xA1"; const inst2 = bytesAsValue(S, inst_bytes); try testing.expect(meta.eql(inst, inst2.*)); } test "bytesAsValue preserves pointer attributes" { const inArr align(16) = [4]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; const inSlice = @ptrCast(*align(16) const volatile [4]u8, &inArr)[0..]; const outPtr = bytesAsValue(u32, inSlice); const in = @typeInfo(@TypeOf(inSlice)).Pointer; const out = @typeInfo(@TypeOf(outPtr)).Pointer; try testing.expectEqual(in.is_const, out.is_const); try testing.expectEqual(in.is_volatile, out.is_volatile); try testing.expectEqual(in.is_allowzero, out.is_allowzero); try testing.expectEqual(in.alignment, out.alignment); } /// Given a pointer to an array of bytes, returns a value of the specified type backed by a /// copy of those bytes. pub fn bytesToValue(comptime T: type, bytes: anytype) T { return bytesAsValue(T, bytes).*; } test "bytesToValue" { const deadbeef_bytes = switch (native_endian) { .Big => "\xDE\xAD\xBE\xEF", .Little => "\xEF\xBE\xAD\xDE", }; const deadbeef = bytesToValue(u32, deadbeef_bytes); try testing.expect(deadbeef == @as(u32, 0xDEADBEEF)); } fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type { if (!(trait.isSlice(bytesType) or trait.isPtrTo(.Array)(bytesType)) or meta.Elem(bytesType) != u8) { @compileError("expected []u8 or *[_]u8, passed " ++ @typeName(bytesType)); } if (trait.isPtrTo(.Array)(bytesType) and @typeInfo(meta.Child(bytesType)).Array.len % @sizeOf(T) != 0) { @compileError("number of bytes in " ++ @typeName(bytesType) ++ " is not divisible by size of " ++ @typeName(T)); } return CopyPtrAttrs(bytesType, .Slice, T); } /// Given a slice of bytes, returns a slice of the specified type /// backed by those bytes, preserving pointer attributes. pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T, @TypeOf(bytes)) { // let's not give an undefined pointer to @ptrCast // it may be equal to zero and fail a null check if (bytes.len == 0) { return &[0]T{}; } const cast_target = CopyPtrAttrs(@TypeOf(bytes), .Many, T); return @ptrCast(cast_target, bytes)[0..@divExact(bytes.len, @sizeOf(T))]; } test "bytesAsSlice" { { const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; const slice = bytesAsSlice(u16, bytes[0..]); try testing.expect(slice.len == 2); try testing.expect(bigToNative(u16, slice[0]) == 0xDEAD); try testing.expect(bigToNative(u16, slice[1]) == 0xBEEF); } { const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; var runtime_zero: usize = 0; const slice = bytesAsSlice(u16, bytes[runtime_zero..]); try testing.expect(slice.len == 2); try testing.expect(bigToNative(u16, slice[0]) == 0xDEAD); try testing.expect(bigToNative(u16, slice[1]) == 0xBEEF); } } test "bytesAsSlice keeps pointer alignment" { { var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 }; const numbers = bytesAsSlice(u32, bytes[0..]); comptime try testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32); } { var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 }; var runtime_zero: usize = 0; const numbers = bytesAsSlice(u32, bytes[runtime_zero..]); comptime try testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32); } } test "bytesAsSlice on a packed struct" { const F = packed struct { a: u8, }; var b = [1]u8{9}; var f = bytesAsSlice(F, &b); try testing.expect(f[0].a == 9); } test "bytesAsSlice with specified alignment" { var bytes align(4) = [_]u8{ 0x33, 0x33, 0x33, 0x33, }; const slice: []u32 = std.mem.bytesAsSlice(u32, bytes[0..]); try testing.expect(slice[0] == 0x33333333); } test "bytesAsSlice preserves pointer attributes" { const inArr align(16) = [4]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; const inSlice = @ptrCast(*align(16) const volatile [4]u8, &inArr)[0..]; const outSlice = bytesAsSlice(u16, inSlice); const in = @typeInfo(@TypeOf(inSlice)).Pointer; const out = @typeInfo(@TypeOf(outSlice)).Pointer; try testing.expectEqual(in.is_const, out.is_const); try testing.expectEqual(in.is_volatile, out.is_volatile); try testing.expectEqual(in.is_allowzero, out.is_allowzero); try testing.expectEqual(in.alignment, out.alignment); } fn SliceAsBytesReturnType(comptime sliceType: type) type { if (!trait.isSlice(sliceType) and !trait.isPtrTo(.Array)(sliceType)) { @compileError("expected []T or *[_]T, passed " ++ @typeName(sliceType)); } return CopyPtrAttrs(sliceType, .Slice, u8); } /// Given a slice, returns a slice of the underlying bytes, preserving pointer attributes. pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) { const Slice = @TypeOf(slice); // let's not give an undefined pointer to @ptrCast // it may be equal to zero and fail a null check if (slice.len == 0 and comptime meta.sentinel(Slice) == null) { return &[0]u8{}; } const cast_target = CopyPtrAttrs(Slice, .Many, u8); return @ptrCast(cast_target, slice)[0 .. slice.len * @sizeOf(meta.Elem(Slice))]; } test "sliceAsBytes" { const bytes = [_]u16{ 0xDEAD, 0xBEEF }; const slice = sliceAsBytes(bytes[0..]); try testing.expect(slice.len == 4); try testing.expect(eql(u8, slice, switch (native_endian) { .Big => "\xDE\xAD\xBE\xEF", .Little => "\xAD\xDE\xEF\xBE", })); } test "sliceAsBytes with sentinel slice" { const empty_string: [:0]const u8 = ""; const bytes = sliceAsBytes(empty_string); try testing.expect(bytes.len == 0); } test "sliceAsBytes packed struct at runtime and comptime" { const Foo = packed struct { a: u4, b: u4, }; const S = struct { fn doTheTest() !void { var foo: Foo = undefined; var slice = sliceAsBytes(@as(*[1]Foo, &foo)[0..1]); slice[0] = 0x13; switch (native_endian) { .Big => { try testing.expect(foo.a == 0x1); try testing.expect(foo.b == 0x3); }, .Little => { try testing.expect(foo.a == 0x3); try testing.expect(foo.b == 0x1); }, } } }; try S.doTheTest(); comptime try S.doTheTest(); } test "sliceAsBytes and bytesAsSlice back" { try testing.expect(@sizeOf(i32) == 4); var big_thing_array = [_]i32{ 1, 2, 3, 4 }; const big_thing_slice: []i32 = big_thing_array[0..]; const bytes = sliceAsBytes(big_thing_slice); try testing.expect(bytes.len == 4 * 4); bytes[4] = 0; bytes[5] = 0; bytes[6] = 0; bytes[7] = 0; try testing.expect(big_thing_slice[1] == 0); const big_thing_again = bytesAsSlice(i32, bytes); try testing.expect(big_thing_again[2] == 3); big_thing_again[2] = -1; try testing.expect(bytes[8] == math.maxInt(u8)); try testing.expect(bytes[9] == math.maxInt(u8)); try testing.expect(bytes[10] == math.maxInt(u8)); try testing.expect(bytes[11] == math.maxInt(u8)); } test "sliceAsBytes preserves pointer attributes" { const inArr align(16) = [2]u16{ 0xDEAD, 0xBEEF }; const inSlice = @ptrCast(*align(16) const volatile [2]u16, &inArr)[0..]; const outSlice = sliceAsBytes(inSlice); const in = @typeInfo(@TypeOf(inSlice)).Pointer; const out = @typeInfo(@TypeOf(outSlice)).Pointer; try testing.expectEqual(in.is_const, out.is_const); try testing.expectEqual(in.is_volatile, out.is_volatile); try testing.expectEqual(in.is_allowzero, out.is_allowzero); try testing.expectEqual(in.alignment, out.alignment); } /// Round an address up to the nearest aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignForward(addr: usize, alignment: usize) usize { return alignForwardGeneric(usize, addr, alignment); } /// Round an address up to the nearest aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T { return alignBackwardGeneric(T, addr + (alignment - 1), alignment); } /// Force an evaluation of the expression; this tries to prevent /// the compiler from optimizing the computation away even if the /// result eventually gets discarded. pub fn doNotOptimizeAway(val: anytype) void { asm volatile ("" : : [val] "rm" (val), : "memory" ); } test "alignForward" { try testing.expect(alignForward(1, 1) == 1); try testing.expect(alignForward(2, 1) == 2); try testing.expect(alignForward(1, 2) == 2); try testing.expect(alignForward(2, 2) == 2); try testing.expect(alignForward(3, 2) == 4); try testing.expect(alignForward(4, 2) == 4); try testing.expect(alignForward(7, 8) == 8); try testing.expect(alignForward(8, 8) == 8); try testing.expect(alignForward(9, 8) == 16); try testing.expect(alignForward(15, 8) == 16); try testing.expect(alignForward(16, 8) == 16); try testing.expect(alignForward(17, 8) == 24); } /// Round an address up to the previous aligned address /// Unlike `alignBackward`, `alignment` can be any positive number, not just a power of 2. pub fn alignBackwardAnyAlign(i: usize, alignment: usize) usize { if (@popCount(usize, alignment) == 1) return alignBackward(i, alignment); assert(alignment != 0); return i - @mod(i, alignment); } /// Round an address up to the previous aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignBackward(addr: usize, alignment: usize) usize { return alignBackwardGeneric(usize, addr, alignment); } /// Round an address up to the previous aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T { assert(@popCount(T, alignment) == 1); // 000010000 // example alignment // 000001111 // subtract 1 // 111110000 // binary not return addr & ~(alignment - 1); } /// Returns whether `alignment` is a valid alignment, meaning it is /// a positive power of 2. pub fn isValidAlign(alignment: u29) bool { return @popCount(u29, alignment) == 1; } pub fn isAlignedAnyAlign(i: usize, alignment: usize) bool { if (@popCount(usize, alignment) == 1) return isAligned(i, alignment); assert(alignment != 0); return 0 == @mod(i, alignment); } /// Given an address and an alignment, return true if the address is a multiple of the alignment /// The alignment must be a power of 2 and greater than 0. pub fn isAligned(addr: usize, alignment: usize) bool { return isAlignedGeneric(u64, addr, alignment); } pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool { return alignBackwardGeneric(T, addr, alignment) == addr; } test "isAligned" { try testing.expect(isAligned(0, 4)); try testing.expect(isAligned(1, 1)); try testing.expect(isAligned(2, 1)); try testing.expect(isAligned(2, 2)); try testing.expect(!isAligned(2, 4)); try testing.expect(isAligned(3, 1)); try testing.expect(!isAligned(3, 2)); try testing.expect(!isAligned(3, 4)); try testing.expect(isAligned(4, 4)); try testing.expect(isAligned(4, 2)); try testing.expect(isAligned(4, 1)); try testing.expect(!isAligned(4, 8)); try testing.expect(!isAligned(4, 16)); } test "freeing empty string with null-terminated sentinel" { const empty_string = try testing.allocator.dupeZ(u8, ""); testing.allocator.free(empty_string); } /// Returns a slice with the given new alignment, /// all other pointer attributes copied from `AttributeSource`. fn AlignedSlice(comptime AttributeSource: type, comptime new_alignment: u29) type { const info = @typeInfo(AttributeSource).Pointer; return @Type(.{ .Pointer = .{ .size = .Slice, .is_const = info.is_const, .is_volatile = info.is_volatile, .is_allowzero = info.is_allowzero, .alignment = new_alignment, .address_space = info.address_space, .child = info.child, .sentinel = null, }, }); } /// Returns the largest slice in the given bytes that conforms to the new alignment, /// or `null` if the given bytes contain no conforming address. pub fn alignInBytes(bytes: []u8, comptime new_alignment: usize) ?[]align(new_alignment) u8 { const begin_address = @ptrToInt(bytes.ptr); const end_address = begin_address + bytes.len; const begin_address_aligned = mem.alignForward(begin_address, new_alignment); const new_length = std.math.sub(usize, end_address, begin_address_aligned) catch |e| switch (e) { error.Overflow => return null, }; const alignment_offset = begin_address_aligned - begin_address; return @alignCast(new_alignment, bytes[alignment_offset .. alignment_offset + new_length]); } /// Returns the largest sub-slice within the given slice that conforms to the new alignment, /// or `null` if the given slice contains no conforming address. pub fn alignInSlice(slice: anytype, comptime new_alignment: usize) ?AlignedSlice(@TypeOf(slice), new_alignment) { const bytes = sliceAsBytes(slice); const aligned_bytes = alignInBytes(bytes, new_alignment) orelse return null; const Element = @TypeOf(slice[0]); const slice_length_bytes = aligned_bytes.len - (aligned_bytes.len % @sizeOf(Element)); const aligned_slice = bytesAsSlice(Element, aligned_bytes[0..slice_length_bytes]); return @alignCast(new_alignment, aligned_slice); }
const std = @import("../std.zig"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; const ast = std.zig.ast; const Node = ast.Node; const Tree = ast.Tree; const AstError = ast.Error; const TokenIndex = ast.TokenIndex; const NodeIndex = ast.NodeIndex; const Token = std.zig.Token; pub const Error = error{ParseError} || Allocator.Error; /// Result should be freed with tree.deinit() when there are /// no more references to any of the tokens or nodes. pub fn parse(gpa: *Allocator, source: []const u8) Allocator.Error!*Tree { var token_ids = std.ArrayList(Token.Id).init(gpa); defer token_ids.deinit(); var token_locs = std.ArrayList(Token.Loc).init(gpa); defer token_locs.deinit(); // Empirically, the zig std lib has an 8:1 ratio of source bytes to token count. const estimated_token_count = source.len / 8; try token_ids.ensureCapacity(estimated_token_count); try token_locs.ensureCapacity(estimated_token_count); var tokenizer = std.zig.Tokenizer.init(source); while (true) { const token = tokenizer.next(); try token_ids.append(token.id); try token_locs.append(token.loc); if (token.id == .Eof) break; } var parser: Parser = .{ .source = source, .arena = std.heap.ArenaAllocator.init(gpa), .gpa = gpa, .token_ids = token_ids.items, .token_locs = token_locs.items, .errors = .{}, .tok_i = 0, }; defer parser.errors.deinit(gpa); errdefer parser.arena.deinit(); while (token_ids.items[parser.tok_i] == .LineComment) parser.tok_i += 1; const root_node = try parser.parseRoot(); const tree = try parser.arena.allocator.create(Tree); tree.* = .{ .gpa = gpa, .source = source, .token_ids = token_ids.toOwnedSlice(), .token_locs = token_locs.toOwnedSlice(), .errors = parser.errors.toOwnedSlice(gpa), .root_node = root_node, .arena = parser.arena.state, }; return tree; } /// Represents in-progress parsing, will be converted to an ast.Tree after completion. const Parser = struct { arena: std.heap.ArenaAllocator, gpa: *Allocator, source: []const u8, token_ids: []const Token.Id, token_locs: []const Token.Loc, tok_i: TokenIndex, errors: std.ArrayListUnmanaged(AstError), /// Root <- skip ContainerMembers eof fn parseRoot(p: *Parser) Allocator.Error!*Node.Root { const decls = try parseContainerMembers(p, true); defer p.gpa.free(decls); // parseContainerMembers will try to skip as much // invalid tokens as it can so this can only be the EOF const eof_token = p.eatToken(.Eof).?; const decls_len = @intCast(NodeIndex, decls.len); const node = try Node.Root.create(&p.arena.allocator, decls_len, eof_token); std.mem.copy(*Node, node.decls(), decls); return node; } /// ContainerMembers /// <- TestDecl ContainerMembers /// / TopLevelComptime ContainerMembers /// / KEYWORD_pub? TopLevelDecl ContainerMembers /// / ContainerField COMMA ContainerMembers /// / ContainerField /// / fn parseContainerMembers(p: *Parser, top_level: bool) ![]*Node { var list = std.ArrayList(*Node).init(p.gpa); defer list.deinit(); var field_state: union(enum) { /// no fields have been seen none, /// currently parsing fields seen, /// saw fields and then a declaration after them. /// payload is first token of previous declaration. end: TokenIndex, /// ther was a declaration between fields, don't report more errors err, } = .none; while (true) { if (try p.parseContainerDocComments()) |node| { try list.append(node); continue; } const doc_comments = try p.parseDocComment(); if (p.parseTestDecl() catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ParseError => { p.findNextContainerMember(); continue; }, }) |node| { if (field_state == .seen) { field_state = .{ .end = node.firstToken() }; } node.cast(Node.TestDecl).?.doc_comments = doc_comments; try list.append(node); continue; } if (p.parseTopLevelComptime() catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ParseError => { p.findNextContainerMember(); continue; }, }) |node| { if (field_state == .seen) { field_state = .{ .end = node.firstToken() }; } node.cast(Node.Comptime).?.doc_comments = doc_comments; try list.append(node); continue; } const visib_token = p.eatToken(.Keyword_pub); if (p.parseTopLevelDecl(doc_comments, visib_token) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ParseError => { p.findNextContainerMember(); continue; }, }) |node| { if (field_state == .seen) { field_state = .{ .end = visib_token orelse node.firstToken() }; } try list.append(node); continue; } if (visib_token != null) { try p.errors.append(p.gpa, .{ .ExpectedPubItem = .{ .token = p.tok_i }, }); // ignore this pub continue; } if (p.parseContainerField() catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ParseError => { // attempt to recover p.findNextContainerMember(); continue; }, }) |node| { switch (field_state) { .none => field_state = .seen, .err, .seen => {}, .end => |tok| { try p.errors.append(p.gpa, .{ .DeclBetweenFields = .{ .token = tok }, }); // continue parsing, error will be reported later field_state = .err; }, } const field = node.cast(Node.ContainerField).?; field.doc_comments = doc_comments; try list.append(node); const comma = p.eatToken(.Comma) orelse { // try to continue parsing const index = p.tok_i; p.findNextContainerMember(); const next = p.token_ids[p.tok_i]; switch (next) { .Eof => break, else => { if (next == .RBrace) { if (!top_level) break; _ = p.nextToken(); } // add error and continue try p.errors.append(p.gpa, .{ .ExpectedToken = .{ .token = index, .expected_id = .Comma }, }); continue; }, } }; if (try p.parseAppendedDocComment(comma)) |appended_comment| field.doc_comments = appended_comment; continue; } // Dangling doc comment if (doc_comments != null) { try p.errors.append(p.gpa, .{ .UnattachedDocComment = .{ .token = doc_comments.?.firstToken() }, }); } const next = p.token_ids[p.tok_i]; switch (next) { .Eof => break, .Keyword_comptime => { _ = p.nextToken(); try p.errors.append(p.gpa, .{ .ExpectedBlockOrField = .{ .token = p.tok_i }, }); }, else => { const index = p.tok_i; if (next == .RBrace) { if (!top_level) break; _ = p.nextToken(); } // this was likely not supposed to end yet, // try to find the next declaration p.findNextContainerMember(); try p.errors.append(p.gpa, .{ .ExpectedContainerMembers = .{ .token = index }, }); }, } } return list.toOwnedSlice(); } /// Attempts to find next container member by searching for certain tokens fn findNextContainerMember(p: *Parser) void { var level: u32 = 0; while (true) { const tok = p.nextToken(); switch (p.token_ids[tok]) { // any of these can start a new top level declaration .Keyword_test, .Keyword_comptime, .Keyword_pub, .Keyword_export, .Keyword_extern, .Keyword_inline, .Keyword_noinline, .Keyword_usingnamespace, .Keyword_threadlocal, .Keyword_const, .Keyword_var, .Keyword_fn, .Identifier, => { if (level == 0) { p.putBackToken(tok); return; } }, .Comma, .Semicolon => { // this decl was likely meant to end here if (level == 0) { return; } }, .LParen, .LBracket, .LBrace => level += 1, .RParen, .RBracket => { if (level != 0) level -= 1; }, .RBrace => { if (level == 0) { // end of container, exit p.putBackToken(tok); return; } level -= 1; }, .Eof => { p.putBackToken(tok); return; }, else => {}, } } } /// Attempts to find the next statement by searching for a semicolon fn findNextStmt(p: *Parser) void { var level: u32 = 0; while (true) { const tok = p.nextToken(); switch (p.token_ids[tok]) { .LBrace => level += 1, .RBrace => { if (level == 0) { p.putBackToken(tok); return; } level -= 1; }, .Semicolon => { if (level == 0) { return; } }, .Eof => { p.putBackToken(tok); return; }, else => {}, } } } /// Eat a multiline container doc comment fn parseContainerDocComments(p: *Parser) !?*Node { if (p.eatToken(.ContainerDocComment)) |first_line| { while (p.eatToken(.ContainerDocComment)) |_| {} const node = try p.arena.allocator.create(Node.DocComment); node.* = .{ .first_line = first_line }; return &node.base; } return null; } /// TestDecl <- KEYWORD_test STRINGLITERALSINGLE Block fn parseTestDecl(p: *Parser) !?*Node { const test_token = p.eatToken(.Keyword_test) orelse return null; const name_node = try p.expectNode(parseStringLiteralSingle, .{ .ExpectedStringLiteral = .{ .token = p.tok_i }, }); const block_node = try p.expectNode(parseBlock, .{ .ExpectedLBrace = .{ .token = p.tok_i }, }); const test_node = try p.arena.allocator.create(Node.TestDecl); test_node.* = .{ .doc_comments = null, .test_token = test_token, .name = name_node, .body_node = block_node, }; return &test_node.base; } /// TopLevelComptime <- KEYWORD_comptime BlockExpr fn parseTopLevelComptime(p: *Parser) !?*Node { const tok = p.eatToken(.Keyword_comptime) orelse return null; const lbrace = p.eatToken(.LBrace) orelse { p.putBackToken(tok); return null; }; p.putBackToken(lbrace); const block_node = try p.expectNode(parseBlockExpr, .{ .ExpectedLabelOrLBrace = .{ .token = p.tok_i }, }); const comptime_node = try p.arena.allocator.create(Node.Comptime); comptime_node.* = .{ .doc_comments = null, .comptime_token = tok, .expr = block_node, }; return &comptime_node.base; } /// TopLevelDecl /// <- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / (KEYWORD_inline / KEYWORD_noinline))? FnProto (SEMICOLON / Block) /// / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl /// / KEYWORD_usingnamespace Expr SEMICOLON fn parseTopLevelDecl(p: *Parser, doc_comments: ?*Node.DocComment, visib_token: ?TokenIndex) !?*Node { var lib_name: ?*Node = null; const extern_export_inline_token = blk: { if (p.eatToken(.Keyword_export)) |token| break :blk token; if (p.eatToken(.Keyword_extern)) |token| { lib_name = try p.parseStringLiteralSingle(); break :blk token; } if (p.eatToken(.Keyword_inline)) |token| break :blk token; if (p.eatToken(.Keyword_noinline)) |token| break :blk token; break :blk null; }; if (try p.parseFnProto(.top_level, .{ .doc_comments = doc_comments, .visib_token = visib_token, .extern_export_inline_token = extern_export_inline_token, .lib_name = lib_name, })) |node| { return node; } if (extern_export_inline_token) |token| { if (p.token_ids[token] == .Keyword_inline or p.token_ids[token] == .Keyword_noinline) { try p.errors.append(p.gpa, .{ .ExpectedFn = .{ .token = p.tok_i }, }); return error.ParseError; } } const thread_local_token = p.eatToken(.Keyword_threadlocal); if (try p.parseVarDecl(.{ .doc_comments = doc_comments, .visib_token = visib_token, .thread_local_token = thread_local_token, .extern_export_token = extern_export_inline_token, .lib_name = lib_name, })) |node| { return node; } if (thread_local_token != null) { try p.errors.append(p.gpa, .{ .ExpectedVarDecl = .{ .token = p.tok_i }, }); // ignore this and try again; return error.ParseError; } if (extern_export_inline_token) |token| { try p.errors.append(p.gpa, .{ .ExpectedVarDeclOrFn = .{ .token = p.tok_i }, }); // ignore this and try again; return error.ParseError; } const use_token = p.eatToken(.Keyword_usingnamespace) orelse return null; const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const semicolon_token = try p.expectToken(.Semicolon); const node = try p.arena.allocator.create(Node.Use); node.* = .{ .doc_comments = doc_comments orelse try p.parseAppendedDocComment(semicolon_token), .visib_token = visib_token, .use_token = use_token, .expr = expr, .semicolon_token = semicolon_token, }; return &node.base; } /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (Keyword_anytype / TypeExpr) fn parseFnProto(p: *Parser, level: enum { top_level, as_type }, fields: struct { doc_comments: ?*Node.DocComment = null, visib_token: ?TokenIndex = null, extern_export_inline_token: ?TokenIndex = null, lib_name: ?*Node = null, }) !?*Node { // TODO: Remove once extern/async fn rewriting is var is_async: ?void = null; var is_extern_prototype: ?void = null; const cc_token: ?TokenIndex = blk: { if (p.eatToken(.Keyword_extern)) |token| { is_extern_prototype = {}; break :blk token; } if (p.eatToken(.Keyword_async)) |token| { is_async = {}; break :blk token; } break :blk null; }; const fn_token = p.eatToken(.Keyword_fn) orelse { if (cc_token) |token| p.putBackToken(token); return null; }; const name_token = p.eatToken(.Identifier); const lparen = try p.expectToken(.LParen); const params = try p.parseParamDeclList(); defer p.gpa.free(params); const var_args_token = p.eatToken(.Ellipsis3); const rparen = try p.expectToken(.RParen); const align_expr = try p.parseByteAlign(); const section_expr = try p.parseLinkSection(); const callconv_expr = try p.parseCallconv(); const exclamation_token = p.eatToken(.Bang); const return_type_expr = (try p.parseAnyType()) orelse try p.expectNodeRecoverable(parseTypeExpr, .{ // most likely the user forgot to specify the return type. // Mark return type as invalid and try to continue. .ExpectedReturnType = .{ .token = p.tok_i }, }); // TODO https://github.com/ziglang/zig/issues/3750 const R = Node.FnProto.ReturnType; const return_type = if (return_type_expr == null) R{ .Invalid = rparen } else if (exclamation_token != null) R{ .InferErrorSet = return_type_expr.? } else R{ .Explicit = return_type_expr.? }; const body_node: ?*Node = switch (level) { .top_level => blk: { if (p.eatToken(.Semicolon)) |_| { break :blk null; } break :blk try p.expectNodeRecoverable(parseBlock, .{ // Since parseBlock only return error.ParseError on // a missing '}' we can assume this function was // supposed to end here. .ExpectedSemiOrLBrace = .{ .token = p.tok_i }, }); }, .as_type => null, }; const fn_proto_node = try Node.FnProto.create(&p.arena.allocator, .{ .params_len = params.len, .fn_token = fn_token, .return_type = return_type, }, .{ .doc_comments = fields.doc_comments, .visib_token = fields.visib_token, .name_token = name_token, .var_args_token = var_args_token, .extern_export_inline_token = fields.extern_export_inline_token, .body_node = body_node, .lib_name = fields.lib_name, .align_expr = align_expr, .section_expr = section_expr, .callconv_expr = callconv_expr, .is_extern_prototype = is_extern_prototype, .is_async = is_async, }); std.mem.copy(Node.FnProto.ParamDecl, fn_proto_node.params(), params); return &fn_proto_node.base; } /// VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? LinkSection? (EQUAL Expr)? SEMICOLON fn parseVarDecl(p: *Parser, fields: struct { doc_comments: ?*Node.DocComment = null, visib_token: ?TokenIndex = null, thread_local_token: ?TokenIndex = null, extern_export_token: ?TokenIndex = null, lib_name: ?*Node = null, comptime_token: ?TokenIndex = null, }) !?*Node { const mut_token = p.eatToken(.Keyword_const) orelse p.eatToken(.Keyword_var) orelse return null; const name_token = try p.expectToken(.Identifier); const type_node = if (p.eatToken(.Colon) != null) try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }) else null; const align_node = try p.parseByteAlign(); const section_node = try p.parseLinkSection(); const eq_token = p.eatToken(.Equal); const init_node = if (eq_token != null) blk: { break :blk try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); } else null; const semicolon_token = try p.expectToken(.Semicolon); const doc_comments = fields.doc_comments orelse try p.parseAppendedDocComment(semicolon_token); const node = try Node.VarDecl.create(&p.arena.allocator, .{ .mut_token = mut_token, .name_token = name_token, .semicolon_token = semicolon_token, }, .{ .doc_comments = doc_comments, .visib_token = fields.visib_token, .thread_local_token = fields.thread_local_token, .eq_token = eq_token, .comptime_token = fields.comptime_token, .extern_export_token = fields.extern_export_token, .lib_name = fields.lib_name, .type_node = type_node, .align_node = align_node, .section_node = section_node, .init_node = init_node, }); return &node.base; } /// ContainerField <- KEYWORD_comptime? IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)? fn parseContainerField(p: *Parser) !?*Node { const comptime_token = p.eatToken(.Keyword_comptime); const name_token = p.eatToken(.Identifier) orelse { if (comptime_token) |t| p.putBackToken(t); return null; }; var align_expr: ?*Node = null; var type_expr: ?*Node = null; if (p.eatToken(.Colon)) |_| { if (p.eatToken(.Keyword_anytype) orelse p.eatToken(.Keyword_var)) |anytype_tok| { const node = try p.arena.allocator.create(Node.AnyType); node.* = .{ .token = anytype_tok }; type_expr = &node.base; } else { type_expr = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); align_expr = try p.parseByteAlign(); } } const value_expr = if (p.eatToken(.Equal)) |_| try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }) else null; const node = try p.arena.allocator.create(Node.ContainerField); node.* = .{ .doc_comments = null, .comptime_token = comptime_token, .name_token = name_token, .type_expr = type_expr, .value_expr = value_expr, .align_expr = align_expr, }; return &node.base; } /// Statement /// <- KEYWORD_comptime? VarDecl /// / KEYWORD_comptime BlockExprStatement /// / KEYWORD_nosuspend BlockExprStatement /// / KEYWORD_suspend (SEMICOLON / BlockExprStatement) /// / KEYWORD_defer BlockExprStatement /// / KEYWORD_errdefer Payload? BlockExprStatement /// / IfStatement /// / LabeledStatement /// / SwitchExpr /// / AssignExpr SEMICOLON fn parseStatement(p: *Parser) Error!?*Node { const comptime_token = p.eatToken(.Keyword_comptime); if (try p.parseVarDecl(.{ .comptime_token = comptime_token, })) |node| { return node; } if (comptime_token) |token| { const block_expr = try p.expectNode(parseBlockExprStatement, .{ .ExpectedBlockOrAssignment = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.Comptime); node.* = .{ .doc_comments = null, .comptime_token = token, .expr = block_expr, }; return &node.base; } if (p.eatToken(.Keyword_nosuspend)) |nosuspend_token| { const block_expr = try p.expectNode(parseBlockExprStatement, .{ .ExpectedBlockOrAssignment = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.Nosuspend); node.* = .{ .nosuspend_token = nosuspend_token, .expr = block_expr, }; return &node.base; } if (p.eatToken(.Keyword_suspend)) |suspend_token| { const semicolon = p.eatToken(.Semicolon); const body_node = if (semicolon == null) blk: { break :blk try p.expectNode(parseBlockExprStatement, .{ .ExpectedBlockOrExpression = .{ .token = p.tok_i }, }); } else null; const node = try p.arena.allocator.create(Node.Suspend); node.* = .{ .suspend_token = suspend_token, .body = body_node, }; return &node.base; } const defer_token = p.eatToken(.Keyword_defer) orelse p.eatToken(.Keyword_errdefer); if (defer_token) |token| { const payload = if (p.token_ids[token] == .Keyword_errdefer) try p.parsePayload() else null; const expr_node = try p.expectNode(parseBlockExprStatement, .{ .ExpectedBlockOrExpression = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.Defer); node.* = .{ .defer_token = token, .expr = expr_node, .payload = payload, }; return &node.base; } if (try p.parseIfStatement()) |node| return node; if (try p.parseLabeledStatement()) |node| return node; if (try p.parseSwitchExpr()) |node| return node; if (try p.parseAssignExpr()) |node| { _ = try p.expectTokenRecoverable(.Semicolon); return node; } return null; } /// IfStatement /// <- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )? /// / IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) fn parseIfStatement(p: *Parser) !?*Node { const if_node = (try p.parseIfPrefix()) orelse return null; const if_prefix = if_node.cast(Node.If).?; const block_expr = (try p.parseBlockExpr()); const assign_expr = if (block_expr == null) try p.expectNode(parseAssignExpr, .{ .ExpectedBlockOrAssignment = .{ .token = p.tok_i }, }) else null; const semicolon = if (assign_expr != null) p.eatToken(.Semicolon) else null; const else_node = if (semicolon == null) blk: { const else_token = p.eatToken(.Keyword_else) orelse break :blk null; const payload = try p.parsePayload(); const else_body = try p.expectNode(parseStatement, .{ .InvalidToken = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.Else); node.* = .{ .else_token = else_token, .payload = payload, .body = else_body, }; break :blk node; } else null; if (block_expr) |body| { if_prefix.body = body; if_prefix.@"else" = else_node; return if_node; } if (assign_expr) |body| { if_prefix.body = body; if (semicolon != null) return if_node; if (else_node != null) { if_prefix.@"else" = else_node; return if_node; } try p.errors.append(p.gpa, .{ .ExpectedSemiOrElse = .{ .token = p.tok_i }, }); } return if_node; } /// LabeledStatement <- BlockLabel? (Block / LoopStatement) fn parseLabeledStatement(p: *Parser) !?*Node { var colon: TokenIndex = undefined; const label_token = p.parseBlockLabel(&colon); if (try p.parseBlock()) |node| { node.cast(Node.Block).?.label = label_token; return node; } if (try p.parseLoopStatement()) |node| { if (node.cast(Node.For)) |for_node| { for_node.label = label_token; } else if (node.cast(Node.While)) |while_node| { while_node.label = label_token; } else unreachable; return node; } if (label_token != null) { try p.errors.append(p.gpa, .{ .ExpectedLabelable = .{ .token = p.tok_i }, }); return error.ParseError; } return null; } /// LoopStatement <- KEYWORD_inline? (ForStatement / WhileStatement) fn parseLoopStatement(p: *Parser) !?*Node { const inline_token = p.eatToken(.Keyword_inline); if (try p.parseForStatement()) |node| { node.cast(Node.For).?.inline_token = inline_token; return node; } if (try p.parseWhileStatement()) |node| { node.cast(Node.While).?.inline_token = inline_token; return node; } if (inline_token == null) return null; // If we've seen "inline", there should have been a "for" or "while" try p.errors.append(p.gpa, .{ .ExpectedInlinable = .{ .token = p.tok_i }, }); return error.ParseError; } /// ForStatement /// <- ForPrefix BlockExpr ( KEYWORD_else Statement )? /// / ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement ) fn parseForStatement(p: *Parser) !?*Node { const node = (try p.parseForPrefix()) orelse return null; const for_prefix = node.cast(Node.For).?; if (try p.parseBlockExpr()) |block_expr_node| { for_prefix.body = block_expr_node; if (p.eatToken(.Keyword_else)) |else_token| { const statement_node = try p.expectNode(parseStatement, .{ .InvalidToken = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = null, .body = statement_node, }; for_prefix.@"else" = else_node; return node; } return node; } for_prefix.body = try p.expectNode(parseAssignExpr, .{ .ExpectedBlockOrAssignment = .{ .token = p.tok_i }, }); if (p.eatToken(.Semicolon) != null) return node; if (p.eatToken(.Keyword_else)) |else_token| { const statement_node = try p.expectNode(parseStatement, .{ .ExpectedStatement = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = null, .body = statement_node, }; for_prefix.@"else" = else_node; return node; } try p.errors.append(p.gpa, .{ .ExpectedSemiOrElse = .{ .token = p.tok_i }, }); return node; } /// WhileStatement /// <- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )? /// / WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) fn parseWhileStatement(p: *Parser) !?*Node { const node = (try p.parseWhilePrefix()) orelse return null; const while_prefix = node.cast(Node.While).?; if (try p.parseBlockExpr()) |block_expr_node| { while_prefix.body = block_expr_node; if (p.eatToken(.Keyword_else)) |else_token| { const payload = try p.parsePayload(); const statement_node = try p.expectNode(parseStatement, .{ .InvalidToken = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = payload, .body = statement_node, }; while_prefix.@"else" = else_node; return node; } return node; } while_prefix.body = try p.expectNode(parseAssignExpr, .{ .ExpectedBlockOrAssignment = .{ .token = p.tok_i }, }); if (p.eatToken(.Semicolon) != null) return node; if (p.eatToken(.Keyword_else)) |else_token| { const payload = try p.parsePayload(); const statement_node = try p.expectNode(parseStatement, .{ .ExpectedStatement = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = payload, .body = statement_node, }; while_prefix.@"else" = else_node; return node; } try p.errors.append(p.gpa, .{ .ExpectedSemiOrElse = .{ .token = p.tok_i }, }); return node; } /// BlockExprStatement /// <- BlockExpr /// / AssignExpr SEMICOLON fn parseBlockExprStatement(p: *Parser) !?*Node { if (try p.parseBlockExpr()) |node| return node; if (try p.parseAssignExpr()) |node| { _ = try p.expectTokenRecoverable(.Semicolon); return node; } return null; } /// BlockExpr <- BlockLabel? Block fn parseBlockExpr(p: *Parser) Error!?*Node { var colon: TokenIndex = undefined; const label_token = p.parseBlockLabel(&colon); const block_node = (try p.parseBlock()) orelse { if (label_token) |label| { p.putBackToken(label + 1); // ":" p.putBackToken(label); // IDENTIFIER } return null; }; block_node.cast(Node.Block).?.label = label_token; return block_node; } /// AssignExpr <- Expr (AssignOp Expr)? fn parseAssignExpr(p: *Parser) !?*Node { return p.parseBinOpExpr(parseAssignOp, parseExpr, .Once); } /// Expr <- KEYWORD_try* BoolOrExpr fn parseExpr(p: *Parser) Error!?*Node { return p.parsePrefixOpExpr(parseTry, parseBoolOrExpr); } /// BoolOrExpr <- BoolAndExpr (KEYWORD_or BoolAndExpr)* fn parseBoolOrExpr(p: *Parser) !?*Node { return p.parseBinOpExpr( SimpleBinOpParseFn(.Keyword_or, .BoolOr), parseBoolAndExpr, .Infinitely, ); } /// BoolAndExpr <- CompareExpr (KEYWORD_and CompareExpr)* fn parseBoolAndExpr(p: *Parser) !?*Node { return p.parseBinOpExpr( SimpleBinOpParseFn(.Keyword_and, .BoolAnd), parseCompareExpr, .Infinitely, ); } /// CompareExpr <- BitwiseExpr (CompareOp BitwiseExpr)? fn parseCompareExpr(p: *Parser) !?*Node { return p.parseBinOpExpr(parseCompareOp, parseBitwiseExpr, .Once); } /// BitwiseExpr <- BitShiftExpr (BitwiseOp BitShiftExpr)* fn parseBitwiseExpr(p: *Parser) !?*Node { return p.parseBinOpExpr(parseBitwiseOp, parseBitShiftExpr, .Infinitely); } /// BitShiftExpr <- AdditionExpr (BitShiftOp AdditionExpr)* fn parseBitShiftExpr(p: *Parser) !?*Node { return p.parseBinOpExpr(parseBitShiftOp, parseAdditionExpr, .Infinitely); } /// AdditionExpr <- MultiplyExpr (AdditionOp MultiplyExpr)* fn parseAdditionExpr(p: *Parser) !?*Node { return p.parseBinOpExpr(parseAdditionOp, parseMultiplyExpr, .Infinitely); } /// MultiplyExpr <- PrefixExpr (MultiplyOp PrefixExpr)* fn parseMultiplyExpr(p: *Parser) !?*Node { return p.parseBinOpExpr(parseMultiplyOp, parsePrefixExpr, .Infinitely); } /// PrefixExpr <- PrefixOp* PrimaryExpr fn parsePrefixExpr(p: *Parser) !?*Node { return p.parsePrefixOpExpr(parsePrefixOp, parsePrimaryExpr); } /// PrimaryExpr /// <- AsmExpr /// / IfExpr /// / KEYWORD_break BreakLabel? Expr? /// / KEYWORD_comptime Expr /// / KEYWORD_nosuspend Expr /// / KEYWORD_continue BreakLabel? /// / KEYWORD_resume Expr /// / KEYWORD_return Expr? /// / BlockLabel? LoopExpr /// / Block /// / CurlySuffixExpr fn parsePrimaryExpr(p: *Parser) !?*Node { if (try p.parseAsmExpr()) |node| return node; if (try p.parseIfExpr()) |node| return node; if (p.eatToken(.Keyword_break)) |token| { const label = try p.parseBreakLabel(); const expr_node = try p.parseExpr(); const node = try p.arena.allocator.create(Node.ControlFlowExpression); node.* = .{ .ltoken = token, .kind = .{ .Break = label }, .rhs = expr_node, }; return &node.base; } if (p.eatToken(.Keyword_comptime)) |token| { const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.Comptime); node.* = .{ .doc_comments = null, .comptime_token = token, .expr = expr_node, }; return &node.base; } if (p.eatToken(.Keyword_nosuspend)) |token| { const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.Nosuspend); node.* = .{ .nosuspend_token = token, .expr = expr_node, }; return &node.base; } if (p.eatToken(.Keyword_continue)) |token| { const label = try p.parseBreakLabel(); const node = try p.arena.allocator.create(Node.ControlFlowExpression); node.* = .{ .ltoken = token, .kind = .{ .Continue = label }, .rhs = null, }; return &node.base; } if (p.eatToken(.Keyword_resume)) |token| { const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.SimplePrefixOp); node.* = .{ .base = .{ .tag = .Resume }, .op_token = token, .rhs = expr_node, }; return &node.base; } if (p.eatToken(.Keyword_return)) |token| { const expr_node = try p.parseExpr(); const node = try p.arena.allocator.create(Node.ControlFlowExpression); node.* = .{ .ltoken = token, .kind = .Return, .rhs = expr_node, }; return &node.base; } var colon: TokenIndex = undefined; const label = p.parseBlockLabel(&colon); if (try p.parseLoopExpr()) |node| { if (node.cast(Node.For)) |for_node| { for_node.label = label; } else if (node.cast(Node.While)) |while_node| { while_node.label = label; } else unreachable; return node; } if (label) |token| { p.putBackToken(token + 1); // ":" p.putBackToken(token); // IDENTIFIER } if (try p.parseBlock()) |node| return node; if (try p.parseCurlySuffixExpr()) |node| return node; return null; } /// IfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)? fn parseIfExpr(p: *Parser) !?*Node { return p.parseIf(parseExpr); } /// Block <- LBRACE Statement* RBRACE fn parseBlock(p: *Parser) !?*Node { const lbrace = p.eatToken(.LBrace) orelse return null; var statements = std.ArrayList(*Node).init(p.gpa); defer statements.deinit(); while (true) { const statement = (p.parseStatement() catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ParseError => { // try to skip to the next statement p.findNextStmt(); continue; }, }) orelse break; try statements.append(statement); } const rbrace = try p.expectToken(.RBrace); const statements_len = @intCast(NodeIndex, statements.items.len); const block_node = try Node.Block.alloc(&p.arena.allocator, statements_len); block_node.* = .{ .label = null, .lbrace = lbrace, .statements_len = statements_len, .rbrace = rbrace, }; std.mem.copy(*Node, block_node.statements(), statements.items); return &block_node.base; } /// LoopExpr <- KEYWORD_inline? (ForExpr / WhileExpr) fn parseLoopExpr(p: *Parser) !?*Node { const inline_token = p.eatToken(.Keyword_inline); if (try p.parseForExpr()) |node| { node.cast(Node.For).?.inline_token = inline_token; return node; } if (try p.parseWhileExpr()) |node| { node.cast(Node.While).?.inline_token = inline_token; return node; } if (inline_token == null) return null; // If we've seen "inline", there should have been a "for" or "while" try p.errors.append(p.gpa, .{ .ExpectedInlinable = .{ .token = p.tok_i }, }); return error.ParseError; } /// ForExpr <- ForPrefix Expr (KEYWORD_else Expr)? fn parseForExpr(p: *Parser) !?*Node { const node = (try p.parseForPrefix()) orelse return null; const for_prefix = node.cast(Node.For).?; const body_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); for_prefix.body = body_node; if (p.eatToken(.Keyword_else)) |else_token| { const body = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = null, .body = body, }; for_prefix.@"else" = else_node; } return node; } /// WhileExpr <- WhilePrefix Expr (KEYWORD_else Payload? Expr)? fn parseWhileExpr(p: *Parser) !?*Node { const node = (try p.parseWhilePrefix()) orelse return null; const while_prefix = node.cast(Node.While).?; const body_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); while_prefix.body = body_node; if (p.eatToken(.Keyword_else)) |else_token| { const payload = try p.parsePayload(); const body = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = payload, .body = body, }; while_prefix.@"else" = else_node; } return node; } /// CurlySuffixExpr <- TypeExpr InitList? fn parseCurlySuffixExpr(p: *Parser) !?*Node { const lhs = (try p.parseTypeExpr()) orelse return null; const suffix_op = (try p.parseInitList(lhs)) orelse return lhs; return suffix_op; } /// InitList /// <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE /// / LBRACE Expr (COMMA Expr)* COMMA? RBRACE /// / LBRACE RBRACE fn parseInitList(p: *Parser, lhs: *Node) !?*Node { const lbrace = p.eatToken(.LBrace) orelse return null; var init_list = std.ArrayList(*Node).init(p.gpa); defer init_list.deinit(); if (try p.parseFieldInit()) |field_init| { try init_list.append(field_init); while (p.eatToken(.Comma)) |_| { const next = (try p.parseFieldInit()) orelse break; try init_list.append(next); } const node = try Node.StructInitializer.alloc(&p.arena.allocator, init_list.items.len); node.* = .{ .lhs = lhs, .rtoken = try p.expectToken(.RBrace), .list_len = init_list.items.len, }; std.mem.copy(*Node, node.list(), init_list.items); return &node.base; } if (try p.parseExpr()) |expr| { try init_list.append(expr); while (p.eatToken(.Comma)) |_| { const next = (try p.parseExpr()) orelse break; try init_list.append(next); } const node = try Node.ArrayInitializer.alloc(&p.arena.allocator, init_list.items.len); node.* = .{ .lhs = lhs, .rtoken = try p.expectToken(.RBrace), .list_len = init_list.items.len, }; std.mem.copy(*Node, node.list(), init_list.items); return &node.base; } const node = try p.arena.allocator.create(Node.StructInitializer); node.* = .{ .lhs = lhs, .rtoken = try p.expectToken(.RBrace), .list_len = 0, }; return &node.base; } /// InitList /// <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE /// / LBRACE Expr (COMMA Expr)* COMMA? RBRACE /// / LBRACE RBRACE fn parseAnonInitList(p: *Parser, dot: TokenIndex) !?*Node { const lbrace = p.eatToken(.LBrace) orelse return null; var init_list = std.ArrayList(*Node).init(p.gpa); defer init_list.deinit(); if (try p.parseFieldInit()) |field_init| { try init_list.append(field_init); while (p.eatToken(.Comma)) |_| { const next = (try p.parseFieldInit()) orelse break; try init_list.append(next); } const node = try Node.StructInitializerDot.alloc(&p.arena.allocator, init_list.items.len); node.* = .{ .dot = dot, .rtoken = try p.expectToken(.RBrace), .list_len = init_list.items.len, }; std.mem.copy(*Node, node.list(), init_list.items); return &node.base; } if (try p.parseExpr()) |expr| { try init_list.append(expr); while (p.eatToken(.Comma)) |_| { const next = (try p.parseExpr()) orelse break; try init_list.append(next); } const node = try Node.ArrayInitializerDot.alloc(&p.arena.allocator, init_list.items.len); node.* = .{ .dot = dot, .rtoken = try p.expectToken(.RBrace), .list_len = init_list.items.len, }; std.mem.copy(*Node, node.list(), init_list.items); return &node.base; } const node = try p.arena.allocator.create(Node.StructInitializerDot); node.* = .{ .dot = dot, .rtoken = try p.expectToken(.RBrace), .list_len = 0, }; return &node.base; } /// TypeExpr <- PrefixTypeOp* ErrorUnionExpr fn parseTypeExpr(p: *Parser) Error!?*Node { return p.parsePrefixOpExpr(parsePrefixTypeOp, parseErrorUnionExpr); } /// ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK TypeExpr)? fn parseErrorUnionExpr(p: *Parser) !?*Node { const suffix_expr = (try p.parseSuffixExpr()) orelse return null; if (try SimpleBinOpParseFn(.Bang, .ErrorUnion)(p)) |node| { const error_union = node.castTag(.ErrorUnion).?; const type_expr = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); error_union.lhs = suffix_expr; error_union.rhs = type_expr; return node; } return suffix_expr; } /// SuffixExpr /// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments /// / PrimaryTypeExpr (SuffixOp / FnCallArguments)* fn parseSuffixExpr(p: *Parser) !?*Node { const maybe_async = p.eatToken(.Keyword_async); if (maybe_async) |async_token| { const token_fn = p.eatToken(.Keyword_fn); if (token_fn != null) { // TODO: remove this hack when async fn rewriting is // HACK: If we see the keyword `fn`, then we assume that // we are parsing an async fn proto, and not a call. // We therefore put back all tokens consumed by the async // prefix... p.putBackToken(token_fn.?); p.putBackToken(async_token); return p.parsePrimaryTypeExpr(); } var res = try p.expectNode(parsePrimaryTypeExpr, .{ .ExpectedPrimaryTypeExpr = .{ .token = p.tok_i }, }); while (try p.parseSuffixOp(res)) |node| { res = node; } const params = (try p.parseFnCallArguments()) orelse { try p.errors.append(p.gpa, .{ .ExpectedParamList = .{ .token = p.tok_i }, }); // ignore this, continue parsing return res; }; defer p.gpa.free(params.list); const node = try Node.Call.alloc(&p.arena.allocator, params.list.len); node.* = .{ .lhs = res, .params_len = params.list.len, .async_token = async_token, .rtoken = params.rparen, }; std.mem.copy(*Node, node.params(), params.list); return &node.base; } if (try p.parsePrimaryTypeExpr()) |expr| { var res = expr; while (true) { if (try p.parseSuffixOp(res)) |node| { res = node; continue; } if (try p.parseFnCallArguments()) |params| { defer p.gpa.free(params.list); const call = try Node.Call.alloc(&p.arena.allocator, params.list.len); call.* = .{ .lhs = res, .params_len = params.list.len, .async_token = null, .rtoken = params.rparen, }; std.mem.copy(*Node, call.params(), params.list); res = &call.base; continue; } break; } return res; } return null; } /// PrimaryTypeExpr /// <- BUILTINIDENTIFIER FnCallArguments /// / CHAR_LITERAL /// / ContainerDecl /// / DOT IDENTIFIER /// / ErrorSetDecl /// / FLOAT /// / FnProto /// / GroupedExpr /// / LabeledTypeExpr /// / IDENTIFIER /// / IfTypeExpr /// / INTEGER /// / KEYWORD_comptime TypeExpr /// / KEYWORD_error DOT IDENTIFIER /// / KEYWORD_false /// / KEYWORD_null /// / KEYWORD_anyframe /// / KEYWORD_true /// / KEYWORD_undefined /// / KEYWORD_unreachable /// / STRINGLITERAL /// / SwitchExpr fn parsePrimaryTypeExpr(p: *Parser) !?*Node { if (try p.parseBuiltinCall()) |node| return node; if (p.eatToken(.CharLiteral)) |token| { const node = try p.arena.allocator.create(Node.CharLiteral); node.* = .{ .token = token, }; return &node.base; } if (try p.parseContainerDecl()) |node| return node; if (try p.parseAnonLiteral()) |node| return node; if (try p.parseErrorSetDecl()) |node| return node; if (try p.parseFloatLiteral()) |node| return node; if (try p.parseFnProto(.as_type, .{})) |node| return node; if (try p.parseGroupedExpr()) |node| return node; if (try p.parseLabeledTypeExpr()) |node| return node; if (try p.parseIdentifier()) |node| return node; if (try p.parseIfTypeExpr()) |node| return node; if (try p.parseIntegerLiteral()) |node| return node; if (p.eatToken(.Keyword_comptime)) |token| { const expr = (try p.parseTypeExpr()) orelse return null; const node = try p.arena.allocator.create(Node.Comptime); node.* = .{ .doc_comments = null, .comptime_token = token, .expr = expr, }; return &node.base; } if (p.eatToken(.Keyword_error)) |token| { const period = try p.expectTokenRecoverable(.Period); const identifier = try p.expectNodeRecoverable(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); const global_error_set = try p.createLiteral(Node.ErrorType, token); if (period == null or identifier == null) return global_error_set; const node = try p.arena.allocator.create(Node.SimpleInfixOp); node.* = .{ .base = Node{ .tag = .Period }, .op_token = period.?, .lhs = global_error_set, .rhs = identifier.?, }; return &node.base; } if (p.eatToken(.Keyword_false)) |token| return p.createLiteral(Node.BoolLiteral, token); if (p.eatToken(.Keyword_null)) |token| return p.createLiteral(Node.NullLiteral, token); if (p.eatToken(.Keyword_anyframe)) |token| { const node = try p.arena.allocator.create(Node.AnyFrameType); node.* = .{ .anyframe_token = token, .result = null, }; return &node.base; } if (p.eatToken(.Keyword_true)) |token| return p.createLiteral(Node.BoolLiteral, token); if (p.eatToken(.Keyword_undefined)) |token| return p.createLiteral(Node.UndefinedLiteral, token); if (p.eatToken(.Keyword_unreachable)) |token| return p.createLiteral(Node.Unreachable, token); if (try p.parseStringLiteral()) |node| return node; if (try p.parseSwitchExpr()) |node| return node; return null; } /// ContainerDecl <- (KEYWORD_extern / KEYWORD_packed)? ContainerDeclAuto fn parseContainerDecl(p: *Parser) !?*Node { const layout_token = p.eatToken(.Keyword_extern) orelse p.eatToken(.Keyword_packed); const node = (try p.parseContainerDeclAuto()) orelse { if (layout_token) |token| p.putBackToken(token); return null; }; node.cast(Node.ContainerDecl).?.*.layout_token = layout_token; return node; } /// ErrorSetDecl <- KEYWORD_error LBRACE IdentifierList RBRACE fn parseErrorSetDecl(p: *Parser) !?*Node { const error_token = p.eatToken(.Keyword_error) orelse return null; if (p.eatToken(.LBrace) == null) { // Might parse as `KEYWORD_error DOT IDENTIFIER` later in PrimaryTypeExpr, so don't error p.putBackToken(error_token); return null; } const decls = try p.parseErrorTagList(); defer p.gpa.free(decls); const rbrace = try p.expectToken(.RBrace); const node = try Node.ErrorSetDecl.alloc(&p.arena.allocator, decls.len); node.* = .{ .error_token = error_token, .decls_len = decls.len, .rbrace_token = rbrace, }; std.mem.copy(*Node, node.decls(), decls); return &node.base; } /// GroupedExpr <- LPAREN Expr RPAREN fn parseGroupedExpr(p: *Parser) !?*Node { const lparen = p.eatToken(.LParen) orelse return null; const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const rparen = try p.expectToken(.RParen); const node = try p.arena.allocator.create(Node.GroupedExpression); node.* = .{ .lparen = lparen, .expr = expr, .rparen = rparen, }; return &node.base; } /// IfTypeExpr <- IfPrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? fn parseIfTypeExpr(p: *Parser) !?*Node { return p.parseIf(parseTypeExpr); } /// LabeledTypeExpr /// <- BlockLabel Block /// / BlockLabel? LoopTypeExpr fn parseLabeledTypeExpr(p: *Parser) !?*Node { var colon: TokenIndex = undefined; const label = p.parseBlockLabel(&colon); if (label) |token| { if (try p.parseBlock()) |node| { node.cast(Node.Block).?.label = token; return node; } } if (try p.parseLoopTypeExpr()) |node| { switch (node.tag) { .For => node.cast(Node.For).?.label = label, .While => node.cast(Node.While).?.label = label, else => unreachable, } return node; } if (label) |token| { p.putBackToken(colon); p.putBackToken(token); } return null; } /// LoopTypeExpr <- KEYWORD_inline? (ForTypeExpr / WhileTypeExpr) fn parseLoopTypeExpr(p: *Parser) !?*Node { const inline_token = p.eatToken(.Keyword_inline); if (try p.parseForTypeExpr()) |node| { node.cast(Node.For).?.inline_token = inline_token; return node; } if (try p.parseWhileTypeExpr()) |node| { node.cast(Node.While).?.inline_token = inline_token; return node; } if (inline_token == null) return null; // If we've seen "inline", there should have been a "for" or "while" try p.errors.append(p.gpa, .{ .ExpectedInlinable = .{ .token = p.tok_i }, }); return error.ParseError; } /// ForTypeExpr <- ForPrefix TypeExpr (KEYWORD_else TypeExpr)? fn parseForTypeExpr(p: *Parser) !?*Node { const node = (try p.parseForPrefix()) orelse return null; const for_prefix = node.cast(Node.For).?; const type_expr = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); for_prefix.body = type_expr; if (p.eatToken(.Keyword_else)) |else_token| { const else_expr = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = null, .body = else_expr, }; for_prefix.@"else" = else_node; } return node; } /// WhileTypeExpr <- WhilePrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? fn parseWhileTypeExpr(p: *Parser) !?*Node { const node = (try p.parseWhilePrefix()) orelse return null; const while_prefix = node.cast(Node.While).?; const type_expr = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); while_prefix.body = type_expr; if (p.eatToken(.Keyword_else)) |else_token| { const payload = try p.parsePayload(); const else_expr = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = null, .body = else_expr, }; while_prefix.@"else" = else_node; } return node; } /// SwitchExpr <- KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE fn parseSwitchExpr(p: *Parser) !?*Node { const switch_token = p.eatToken(.Keyword_switch) orelse return null; _ = try p.expectToken(.LParen); const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); _ = try p.expectToken(.LBrace); const cases = try p.parseSwitchProngList(); defer p.gpa.free(cases); const rbrace = try p.expectToken(.RBrace); const node = try Node.Switch.alloc(&p.arena.allocator, cases.len); node.* = .{ .switch_token = switch_token, .expr = expr_node, .cases_len = cases.len, .rbrace = rbrace, }; std.mem.copy(*Node, node.cases(), cases); return &node.base; } /// AsmExpr <- KEYWORD_asm KEYWORD_volatile? LPAREN Expr AsmOutput? RPAREN /// AsmOutput <- COLON AsmOutputList AsmInput? /// AsmInput <- COLON AsmInputList AsmClobbers? /// AsmClobbers <- COLON StringList /// StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL? fn parseAsmExpr(p: *Parser) !?*Node { const asm_token = p.eatToken(.Keyword_asm) orelse return null; const volatile_token = p.eatToken(.Keyword_volatile); _ = try p.expectToken(.LParen); const template = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); var arena_outputs: []Node.Asm.Output = &[0]Node.Asm.Output{}; var arena_inputs: []Node.Asm.Input = &[0]Node.Asm.Input{}; var arena_clobbers: []*Node = &[0]*Node{}; if (p.eatToken(.Colon) != null) { const outputs = try p.parseAsmOutputList(); defer p.gpa.free(outputs); arena_outputs = try p.arena.allocator.dupe(Node.Asm.Output, outputs); if (p.eatToken(.Colon) != null) { const inputs = try p.parseAsmInputList(); defer p.gpa.free(inputs); arena_inputs = try p.arena.allocator.dupe(Node.Asm.Input, inputs); if (p.eatToken(.Colon) != null) { const clobbers = try ListParseFn(*Node, parseStringLiteral)(p); defer p.gpa.free(clobbers); arena_clobbers = try p.arena.allocator.dupe(*Node, clobbers); } } } const node = try p.arena.allocator.create(Node.Asm); node.* = .{ .asm_token = asm_token, .volatile_token = volatile_token, .template = template, .outputs = arena_outputs, .inputs = arena_inputs, .clobbers = arena_clobbers, .rparen = try p.expectToken(.RParen), }; return &node.base; } /// DOT IDENTIFIER fn parseAnonLiteral(p: *Parser) !?*Node { const dot = p.eatToken(.Period) orelse return null; // anon enum literal if (p.eatToken(.Identifier)) |name| { const node = try p.arena.allocator.create(Node.EnumLiteral); node.* = .{ .dot = dot, .name = name, }; return &node.base; } if (try p.parseAnonInitList(dot)) |node| { return node; } p.putBackToken(dot); return null; } /// AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPAREN fn parseAsmOutputItem(p: *Parser) !?Node.Asm.Output { const lbracket = p.eatToken(.LBracket) orelse return null; const name = try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RBracket); const constraint = try p.expectNode(parseStringLiteral, .{ .ExpectedStringLiteral = .{ .token = p.tok_i }, }); _ = try p.expectToken(.LParen); const kind: Node.Asm.Output.Kind = blk: { if (p.eatToken(.Arrow) != null) { const return_ident = try p.expectNode(parseTypeExpr, .{ .ExpectedTypeExpr = .{ .token = p.tok_i }, }); break :blk .{ .Return = return_ident }; } const variable = try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); break :blk .{ .Variable = variable.cast(Node.Identifier).? }; }; const rparen = try p.expectToken(.RParen); return Node.Asm.Output{ .lbracket = lbracket, .symbolic_name = name, .constraint = constraint, .kind = kind, .rparen = rparen, }; } /// AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN fn parseAsmInputItem(p: *Parser) !?Node.Asm.Input { const lbracket = p.eatToken(.LBracket) orelse return null; const name = try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RBracket); const constraint = try p.expectNode(parseStringLiteral, .{ .ExpectedStringLiteral = .{ .token = p.tok_i }, }); _ = try p.expectToken(.LParen); const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const rparen = try p.expectToken(.RParen); return Node.Asm.Input{ .lbracket = lbracket, .symbolic_name = name, .constraint = constraint, .expr = expr, .rparen = rparen, }; } /// BreakLabel <- COLON IDENTIFIER fn parseBreakLabel(p: *Parser) !?*Node { _ = p.eatToken(.Colon) orelse return null; return try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); } /// BlockLabel <- IDENTIFIER COLON fn parseBlockLabel(p: *Parser, colon_token: *TokenIndex) ?TokenIndex { const identifier = p.eatToken(.Identifier) orelse return null; if (p.eatToken(.Colon)) |colon| { colon_token.* = colon; return identifier; } p.putBackToken(identifier); return null; } /// FieldInit <- DOT IDENTIFIER EQUAL Expr fn parseFieldInit(p: *Parser) !?*Node { const period_token = p.eatToken(.Period) orelse return null; const name_token = p.eatToken(.Identifier) orelse { // Because of anon literals `.{` is also valid. p.putBackToken(period_token); return null; }; const eq_token = p.eatToken(.Equal) orelse { // `.Name` may also be an enum literal, which is a later rule. p.putBackToken(name_token); p.putBackToken(period_token); return null; }; const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.FieldInitializer); node.* = .{ .period_token = period_token, .name_token = name_token, .expr = expr_node, }; return &node.base; } /// WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN fn parseWhileContinueExpr(p: *Parser) !?*Node { _ = p.eatToken(.Colon) orelse return null; _ = try p.expectToken(.LParen); const node = try p.expectNode(parseAssignExpr, .{ .ExpectedExprOrAssignment = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); return node; } /// LinkSection <- KEYWORD_linksection LPAREN Expr RPAREN fn parseLinkSection(p: *Parser) !?*Node { _ = p.eatToken(.Keyword_linksection) orelse return null; _ = try p.expectToken(.LParen); const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); return expr_node; } /// CallConv <- KEYWORD_callconv LPAREN Expr RPAREN fn parseCallconv(p: *Parser) !?*Node { _ = p.eatToken(.Keyword_callconv) orelse return null; _ = try p.expectToken(.LParen); const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); return expr_node; } /// ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType fn parseParamDecl(p: *Parser) !?Node.FnProto.ParamDecl { const doc_comments = try p.parseDocComment(); const noalias_token = p.eatToken(.Keyword_noalias); const comptime_token = if (noalias_token == null) p.eatToken(.Keyword_comptime) else null; const name_token = blk: { const identifier = p.eatToken(.Identifier) orelse break :blk null; if (p.eatToken(.Colon) != null) break :blk identifier; p.putBackToken(identifier); // ParamType may also be an identifier break :blk null; }; const param_type = (try p.parseParamType()) orelse { // Only return cleanly if no keyword, identifier, or doc comment was found if (noalias_token == null and comptime_token == null and name_token == null and doc_comments == null) { return null; } try p.errors.append(p.gpa, .{ .ExpectedParamType = .{ .token = p.tok_i }, }); return error.ParseError; }; return Node.FnProto.ParamDecl{ .doc_comments = doc_comments, .comptime_token = comptime_token, .noalias_token = noalias_token, .name_token = name_token, .param_type = param_type, }; } /// ParamType /// <- Keyword_anytype /// / DOT3 /// / TypeExpr fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType { // TODO cast from tuple to error union is broken const P = Node.FnProto.ParamDecl.ParamType; if (try p.parseAnyType()) |node| return P{ .any_type = node }; if (try p.parseTypeExpr()) |node| return P{ .type_expr = node }; return null; } /// IfPrefix <- KEYWORD_if LPAREN Expr RPAREN PtrPayload? fn parseIfPrefix(p: *Parser) !?*Node { const if_token = p.eatToken(.Keyword_if) orelse return null; _ = try p.expectToken(.LParen); const condition = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); const payload = try p.parsePtrPayload(); const node = try p.arena.allocator.create(Node.If); node.* = .{ .if_token = if_token, .condition = condition, .payload = payload, .body = undefined, // set by caller .@"else" = null, }; return &node.base; } /// WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? fn parseWhilePrefix(p: *Parser) !?*Node { const while_token = p.eatToken(.Keyword_while) orelse return null; _ = try p.expectToken(.LParen); const condition = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); const payload = try p.parsePtrPayload(); const continue_expr = try p.parseWhileContinueExpr(); const node = try p.arena.allocator.create(Node.While); node.* = .{ .label = null, .inline_token = null, .while_token = while_token, .condition = condition, .payload = payload, .continue_expr = continue_expr, .body = undefined, // set by caller .@"else" = null, }; return &node.base; } /// ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload fn parseForPrefix(p: *Parser) !?*Node { const for_token = p.eatToken(.Keyword_for) orelse return null; _ = try p.expectToken(.LParen); const array_expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); const payload = try p.expectNode(parsePtrIndexPayload, .{ .ExpectedPayload = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.For); node.* = .{ .label = null, .inline_token = null, .for_token = for_token, .array_expr = array_expr, .payload = payload, .body = undefined, // set by caller .@"else" = null, }; return &node.base; } /// Payload <- PIPE IDENTIFIER PIPE fn parsePayload(p: *Parser) !?*Node { const lpipe = p.eatToken(.Pipe) orelse return null; const identifier = try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); const rpipe = try p.expectToken(.Pipe); const node = try p.arena.allocator.create(Node.Payload); node.* = .{ .lpipe = lpipe, .error_symbol = identifier, .rpipe = rpipe, }; return &node.base; } /// PtrPayload <- PIPE ASTERISK? IDENTIFIER PIPE fn parsePtrPayload(p: *Parser) !?*Node { const lpipe = p.eatToken(.Pipe) orelse return null; const asterisk = p.eatToken(.Asterisk); const identifier = try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); const rpipe = try p.expectToken(.Pipe); const node = try p.arena.allocator.create(Node.PointerPayload); node.* = .{ .lpipe = lpipe, .ptr_token = asterisk, .value_symbol = identifier, .rpipe = rpipe, }; return &node.base; } /// PtrIndexPayload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE fn parsePtrIndexPayload(p: *Parser) !?*Node { const lpipe = p.eatToken(.Pipe) orelse return null; const asterisk = p.eatToken(.Asterisk); const identifier = try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); const index = if (p.eatToken(.Comma) == null) null else try p.expectNode(parseIdentifier, .{ .ExpectedIdentifier = .{ .token = p.tok_i }, }); const rpipe = try p.expectToken(.Pipe); const node = try p.arena.allocator.create(Node.PointerIndexPayload); node.* = .{ .lpipe = lpipe, .ptr_token = asterisk, .value_symbol = identifier, .index_symbol = index, .rpipe = rpipe, }; return &node.base; } /// SwitchProng <- SwitchCase EQUALRARROW PtrPayload? AssignExpr fn parseSwitchProng(p: *Parser) !?*Node { const node = (try p.parseSwitchCase()) orelse return null; const arrow = try p.expectToken(.EqualAngleBracketRight); const payload = try p.parsePtrPayload(); const expr = try p.expectNode(parseAssignExpr, .{ .ExpectedExprOrAssignment = .{ .token = p.tok_i }, }); const switch_case = node.cast(Node.SwitchCase).?; switch_case.arrow_token = arrow; switch_case.payload = payload; switch_case.expr = expr; return node; } /// SwitchCase /// <- SwitchItem (COMMA SwitchItem)* COMMA? /// / KEYWORD_else fn parseSwitchCase(p: *Parser) !?*Node { var list = std.ArrayList(*Node).init(p.gpa); defer list.deinit(); if (try p.parseSwitchItem()) |first_item| { try list.append(first_item); while (p.eatToken(.Comma) != null) { const next_item = (try p.parseSwitchItem()) orelse break; try list.append(next_item); } } else if (p.eatToken(.Keyword_else)) |else_token| { const else_node = try p.arena.allocator.create(Node.SwitchElse); else_node.* = .{ .token = else_token, }; try list.append(&else_node.base); } else return null; const node = try Node.SwitchCase.alloc(&p.arena.allocator, list.items.len); node.* = .{ .items_len = list.items.len, .arrow_token = undefined, // set by caller .payload = null, .expr = undefined, // set by caller }; std.mem.copy(*Node, node.items(), list.items); return &node.base; } /// SwitchItem <- Expr (DOT3 Expr)? fn parseSwitchItem(p: *Parser) !?*Node { const expr = (try p.parseExpr()) orelse return null; if (p.eatToken(.Ellipsis3)) |token| { const range_end = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); const node = try p.arena.allocator.create(Node.SimpleInfixOp); node.* = .{ .base = Node{ .tag = .Range }, .op_token = token, .lhs = expr, .rhs = range_end, }; return &node.base; } return expr; } /// AssignOp /// <- ASTERISKEQUAL /// / SLASHEQUAL /// / PERCENTEQUAL /// / PLUSEQUAL /// / MINUSEQUAL /// / LARROW2EQUAL /// / RARROW2EQUAL /// / AMPERSANDEQUAL /// / CARETEQUAL /// / PIPEEQUAL /// / ASTERISKPERCENTEQUAL /// / PLUSPERCENTEQUAL /// / MINUSPERCENTEQUAL /// / EQUAL fn parseAssignOp(p: *Parser) !?*Node { const token = p.nextToken(); const op: Node.Tag = switch (p.token_ids[token]) { .AsteriskEqual => .AssignMul, .SlashEqual => .AssignDiv, .PercentEqual => .AssignMod, .PlusEqual => .AssignAdd, .MinusEqual => .AssignSub, .AngleBracketAngleBracketLeftEqual => .AssignBitShiftLeft, .AngleBracketAngleBracketRightEqual => .AssignBitShiftRight, .AmpersandEqual => .AssignBitAnd, .CaretEqual => .AssignBitXor, .PipeEqual => .AssignBitOr, .AsteriskPercentEqual => .AssignMulWrap, .PlusPercentEqual => .AssignAddWrap, .MinusPercentEqual => .AssignSubWrap, .Equal => .Assign, else => { p.putBackToken(token); return null; }, }; const node = try p.arena.allocator.create(Node.SimpleInfixOp); node.* = .{ .base = .{ .tag = op }, .op_token = token, .lhs = undefined, // set by caller .rhs = undefined, // set by caller }; return &node.base; } /// CompareOp /// <- EQUALEQUAL /// / EXCLAMATIONMARKEQUAL /// / LARROW /// / RARROW /// / LARROWEQUAL /// / RARROWEQUAL fn parseCompareOp(p: *Parser) !?*Node { const token = p.nextToken(); const op: Node.Tag = switch (p.token_ids[token]) { .EqualEqual => .EqualEqual, .BangEqual => .BangEqual, .AngleBracketLeft => .LessThan, .AngleBracketRight => .GreaterThan, .AngleBracketLeftEqual => .LessOrEqual, .AngleBracketRightEqual => .GreaterOrEqual, else => { p.putBackToken(token); return null; }, }; return p.createInfixOp(token, op); } /// BitwiseOp /// <- AMPERSAND /// / CARET /// / PIPE /// / KEYWORD_orelse /// / KEYWORD_catch Payload? fn parseBitwiseOp(p: *Parser) !?*Node { const token = p.nextToken(); const op: Node.Tag = switch (p.token_ids[token]) { .Ampersand => .BitAnd, .Caret => .BitXor, .Pipe => .BitOr, .Keyword_orelse => .OrElse, .Keyword_catch => { const payload = try p.parsePayload(); const node = try p.arena.allocator.create(Node.Catch); node.* = .{ .op_token = token, .lhs = undefined, // set by caller .rhs = undefined, // set by caller .payload = payload, }; return &node.base; }, else => { p.putBackToken(token); return null; }, }; return p.createInfixOp(token, op); } /// BitShiftOp /// <- LARROW2 /// / RARROW2 fn parseBitShiftOp(p: *Parser) !?*Node { const token = p.nextToken(); const op: Node.Tag = switch (p.token_ids[token]) { .AngleBracketAngleBracketLeft => .BitShiftLeft, .AngleBracketAngleBracketRight => .BitShiftRight, else => { p.putBackToken(token); return null; }, }; return p.createInfixOp(token, op); } /// AdditionOp /// <- PLUS /// / MINUS /// / PLUS2 /// / PLUSPERCENT /// / MINUSPERCENT fn parseAdditionOp(p: *Parser) !?*Node { const token = p.nextToken(); const op: Node.Tag = switch (p.token_ids[token]) { .Plus => .Add, .Minus => .Sub, .PlusPlus => .ArrayCat, .PlusPercent => .AddWrap, .MinusPercent => .SubWrap, else => { p.putBackToken(token); return null; }, }; return p.createInfixOp(token, op); } /// MultiplyOp /// <- PIPE2 /// / ASTERISK /// / SLASH /// / PERCENT /// / ASTERISK2 /// / ASTERISKPERCENT fn parseMultiplyOp(p: *Parser) !?*Node { const token = p.nextToken(); const op: Node.Tag = switch (p.token_ids[token]) { .PipePipe => .MergeErrorSets, .Asterisk => .Mul, .Slash => .Div, .Percent => .Mod, .AsteriskAsterisk => .ArrayMult, .AsteriskPercent => .MulWrap, else => { p.putBackToken(token); return null; }, }; return p.createInfixOp(token, op); } /// PrefixOp /// <- EXCLAMATIONMARK /// / MINUS /// / TILDE /// / MINUSPERCENT /// / AMPERSAND /// / KEYWORD_try /// / KEYWORD_await fn parsePrefixOp(p: *Parser) !?*Node { const token = p.nextToken(); switch (p.token_ids[token]) { .Bang => return p.allocSimplePrefixOp(.BoolNot, token), .Minus => return p.allocSimplePrefixOp(.Negation, token), .Tilde => return p.allocSimplePrefixOp(.BitNot, token), .MinusPercent => return p.allocSimplePrefixOp(.NegationWrap, token), .Ampersand => return p.allocSimplePrefixOp(.AddressOf, token), .Keyword_try => return p.allocSimplePrefixOp(.Try, token), .Keyword_await => return p.allocSimplePrefixOp(.Await, token), else => { p.putBackToken(token); return null; }, } } fn allocSimplePrefixOp(p: *Parser, comptime tag: Node.Tag, token: TokenIndex) !?*Node { const node = try p.arena.allocator.create(Node.SimplePrefixOp); node.* = .{ .base = .{ .tag = tag }, .op_token = token, .rhs = undefined, // set by caller }; return &node.base; } // TODO: ArrayTypeStart is either an array or a slice, but const/allowzero only work on // pointers. Consider updating this rule: // ... // / ArrayTypeStart // / SliceTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* // / PtrTypeStart ... /// PrefixTypeOp /// <- QUESTIONMARK /// / KEYWORD_anyframe MINUSRARROW /// / ArrayTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* /// / PtrTypeStart (KEYWORD_align LPAREN Expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* fn parsePrefixTypeOp(p: *Parser) !?*Node { if (p.eatToken(.QuestionMark)) |token| { const node = try p.arena.allocator.create(Node.SimplePrefixOp); node.* = .{ .base = .{ .tag = .OptionalType }, .op_token = token, .rhs = undefined, // set by caller }; return &node.base; } if (p.eatToken(.Keyword_anyframe)) |token| { const arrow = p.eatToken(.Arrow) orelse { p.putBackToken(token); return null; }; const node = try p.arena.allocator.create(Node.AnyFrameType); node.* = .{ .anyframe_token = token, .result = .{ .arrow_token = arrow, .return_type = undefined, // set by caller }, }; return &node.base; } if (try p.parsePtrTypeStart()) |node| { // If the token encountered was **, there will be two nodes instead of one. // The attributes should be applied to the rightmost operator. var ptr_info = if (node.cast(Node.PtrType)) |ptr_type| if (p.token_ids[ptr_type.op_token] == .AsteriskAsterisk) &ptr_type.rhs.cast(Node.PtrType).?.ptr_info else &ptr_type.ptr_info else if (node.cast(Node.SliceType)) |slice_type| &slice_type.ptr_info else unreachable; while (true) { if (p.eatToken(.Keyword_align)) |align_token| { const lparen = try p.expectToken(.LParen); const expr_node = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); // Optional bit range const bit_range = if (p.eatToken(.Colon)) |_| bit_range_value: { const range_start = try p.expectNode(parseIntegerLiteral, .{ .ExpectedIntegerLiteral = .{ .token = p.tok_i }, }); _ = try p.expectToken(.Colon); const range_end = try p.expectNode(parseIntegerLiteral, .{ .ExpectedIntegerLiteral = .{ .token = p.tok_i }, }); break :bit_range_value ast.PtrInfo.Align.BitRange{ .start = range_start, .end = range_end, }; } else null; _ = try p.expectToken(.RParen); if (ptr_info.align_info != null) { try p.errors.append(p.gpa, .{ .ExtraAlignQualifier = .{ .token = p.tok_i - 1 }, }); continue; } ptr_info.align_info = ast.PtrInfo.Align{ .node = expr_node, .bit_range = bit_range, }; continue; } if (p.eatToken(.Keyword_const)) |const_token| { if (ptr_info.const_token != null) { try p.errors.append(p.gpa, .{ .ExtraConstQualifier = .{ .token = p.tok_i - 1 }, }); continue; } ptr_info.const_token = const_token; continue; } if (p.eatToken(.Keyword_volatile)) |volatile_token| { if (ptr_info.volatile_token != null) { try p.errors.append(p.gpa, .{ .ExtraVolatileQualifier = .{ .token = p.tok_i - 1 }, }); continue; } ptr_info.volatile_token = volatile_token; continue; } if (p.eatToken(.Keyword_allowzero)) |allowzero_token| { if (ptr_info.allowzero_token != null) { try p.errors.append(p.gpa, .{ .ExtraAllowZeroQualifier = .{ .token = p.tok_i - 1 }, }); continue; } ptr_info.allowzero_token = allowzero_token; continue; } break; } return node; } if (try p.parseArrayTypeStart()) |node| { if (node.cast(Node.SliceType)) |slice_type| { // Collect pointer qualifiers in any order, but disallow duplicates while (true) { if (try p.parseByteAlign()) |align_expr| { if (slice_type.ptr_info.align_info != null) { try p.errors.append(p.gpa, .{ .ExtraAlignQualifier = .{ .token = p.tok_i - 1 }, }); continue; } slice_type.ptr_info.align_info = ast.PtrInfo.Align{ .node = align_expr, .bit_range = null, }; continue; } if (p.eatToken(.Keyword_const)) |const_token| { if (slice_type.ptr_info.const_token != null) { try p.errors.append(p.gpa, .{ .ExtraConstQualifier = .{ .token = p.tok_i - 1 }, }); continue; } slice_type.ptr_info.const_token = const_token; continue; } if (p.eatToken(.Keyword_volatile)) |volatile_token| { if (slice_type.ptr_info.volatile_token != null) { try p.errors.append(p.gpa, .{ .ExtraVolatileQualifier = .{ .token = p.tok_i - 1 }, }); continue; } slice_type.ptr_info.volatile_token = volatile_token; continue; } if (p.eatToken(.Keyword_allowzero)) |allowzero_token| { if (slice_type.ptr_info.allowzero_token != null) { try p.errors.append(p.gpa, .{ .ExtraAllowZeroQualifier = .{ .token = p.tok_i - 1 }, }); continue; } slice_type.ptr_info.allowzero_token = allowzero_token; continue; } break; } } return node; } return null; } /// SuffixOp /// <- LBRACKET Expr (DOT2 (Expr (COLON Expr)?)?)? RBRACKET /// / DOT IDENTIFIER /// / DOTASTERISK /// / DOTQUESTIONMARK fn parseSuffixOp(p: *Parser, lhs: *Node) !?*Node { if (p.eatToken(.LBracket)) |_| { const index_expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); if (p.eatToken(.Ellipsis2) != null) { const end_expr = try p.parseExpr(); const sentinel: ?*Node = if (p.eatToken(.Colon) != null) try p.parseExpr() else null; const rtoken = try p.expectToken(.RBracket); const node = try p.arena.allocator.create(Node.Slice); node.* = .{ .lhs = lhs, .rtoken = rtoken, .start = index_expr, .end = end_expr, .sentinel = sentinel, }; return &node.base; } const rtoken = try p.expectToken(.RBracket); const node = try p.arena.allocator.create(Node.ArrayAccess); node.* = .{ .lhs = lhs, .rtoken = rtoken, .index_expr = index_expr, }; return &node.base; } if (p.eatToken(.PeriodAsterisk)) |period_asterisk| { const node = try p.arena.allocator.create(Node.SimpleSuffixOp); node.* = .{ .base = .{ .tag = .Deref }, .lhs = lhs, .rtoken = period_asterisk, }; return &node.base; } if (p.eatToken(.Period)) |period| { if (try p.parseIdentifier()) |identifier| { const node = try p.arena.allocator.create(Node.SimpleInfixOp); node.* = .{ .base = Node{ .tag = .Period }, .op_token = period, .lhs = lhs, .rhs = identifier, }; return &node.base; } if (p.eatToken(.QuestionMark)) |question_mark| { const node = try p.arena.allocator.create(Node.SimpleSuffixOp); node.* = .{ .base = .{ .tag = .UnwrapOptional }, .lhs = lhs, .rtoken = question_mark, }; return &node.base; } try p.errors.append(p.gpa, .{ .ExpectedSuffixOp = .{ .token = p.tok_i }, }); return null; } return null; } /// FnCallArguments <- LPAREN ExprList RPAREN /// ExprList <- (Expr COMMA)* Expr? fn parseFnCallArguments(p: *Parser) !?AnnotatedParamList { if (p.eatToken(.LParen) == null) return null; const list = try ListParseFn(*Node, parseExpr)(p); errdefer p.gpa.free(list); const rparen = try p.expectToken(.RParen); return AnnotatedParamList{ .list = list, .rparen = rparen }; } const AnnotatedParamList = struct { list: []*Node, rparen: TokenIndex, }; /// ArrayTypeStart <- LBRACKET Expr? RBRACKET fn parseArrayTypeStart(p: *Parser) !?*Node { const lbracket = p.eatToken(.LBracket) orelse return null; const expr = try p.parseExpr(); const sentinel = if (p.eatToken(.Colon)) |_| try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }) else null; const rbracket = try p.expectToken(.RBracket); if (expr) |len_expr| { if (sentinel) |s| { const node = try p.arena.allocator.create(Node.ArrayTypeSentinel); node.* = .{ .op_token = lbracket, .rhs = undefined, // set by caller .len_expr = len_expr, .sentinel = s, }; return &node.base; } else { const node = try p.arena.allocator.create(Node.ArrayType); node.* = .{ .op_token = lbracket, .rhs = undefined, // set by caller .len_expr = len_expr, }; return &node.base; } } const node = try p.arena.allocator.create(Node.SliceType); node.* = .{ .op_token = lbracket, .rhs = undefined, // set by caller .ptr_info = .{ .sentinel = sentinel }, }; return &node.base; } /// PtrTypeStart /// <- ASTERISK /// / ASTERISK2 /// / PTRUNKNOWN /// / PTRC fn parsePtrTypeStart(p: *Parser) !?*Node { if (p.eatToken(.Asterisk)) |asterisk| { const sentinel = if (p.eatToken(.Colon)) |_| try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }) else null; const node = try p.arena.allocator.create(Node.PtrType); node.* = .{ .op_token = asterisk, .rhs = undefined, // set by caller .ptr_info = .{ .sentinel = sentinel }, }; return &node.base; } if (p.eatToken(.AsteriskAsterisk)) |double_asterisk| { const node = try p.arena.allocator.create(Node.PtrType); node.* = .{ .op_token = double_asterisk, .rhs = undefined, // set by caller }; // Special case for **, which is its own token const child = try p.arena.allocator.create(Node.PtrType); child.* = .{ .op_token = double_asterisk, .rhs = undefined, // set by caller }; node.rhs = &child.base; return &node.base; } if (p.eatToken(.LBracket)) |lbracket| { const asterisk = p.eatToken(.Asterisk) orelse { p.putBackToken(lbracket); return null; }; if (p.eatToken(.Identifier)) |ident| { const token_loc = p.token_locs[ident]; const token_slice = p.source[token_loc.start..token_loc.end]; if (!std.mem.eql(u8, token_slice, "c")) { p.putBackToken(ident); } else { _ = try p.expectToken(.RBracket); const node = try p.arena.allocator.create(Node.PtrType); node.* = .{ .op_token = lbracket, .rhs = undefined, // set by caller }; return &node.base; } } const sentinel = if (p.eatToken(.Colon)) |_| try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }) else null; _ = try p.expectToken(.RBracket); const node = try p.arena.allocator.create(Node.PtrType); node.* = .{ .op_token = lbracket, .rhs = undefined, // set by caller .ptr_info = .{ .sentinel = sentinel }, }; return &node.base; } return null; } /// ContainerDeclAuto <- ContainerDeclType LBRACE ContainerMembers RBRACE fn parseContainerDeclAuto(p: *Parser) !?*Node { const container_decl_type = (try p.parseContainerDeclType()) orelse return null; const lbrace = try p.expectToken(.LBrace); const members = try p.parseContainerMembers(false); defer p.gpa.free(members); const rbrace = try p.expectToken(.RBrace); const members_len = @intCast(NodeIndex, members.len); const node = try Node.ContainerDecl.alloc(&p.arena.allocator, members_len); node.* = .{ .layout_token = null, .kind_token = container_decl_type.kind_token, .init_arg_expr = container_decl_type.init_arg_expr, .fields_and_decls_len = members_len, .lbrace_token = lbrace, .rbrace_token = rbrace, }; std.mem.copy(*Node, node.fieldsAndDecls(), members); return &node.base; } /// Holds temporary data until we are ready to construct the full ContainerDecl AST node. const ContainerDeclType = struct { kind_token: TokenIndex, init_arg_expr: Node.ContainerDecl.InitArg, }; /// ContainerDeclType /// <- KEYWORD_struct /// / KEYWORD_enum (LPAREN Expr RPAREN)? /// / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)? fn parseContainerDeclType(p: *Parser) !?ContainerDeclType { const kind_token = p.nextToken(); const init_arg_expr = switch (p.token_ids[kind_token]) { .Keyword_struct => Node.ContainerDecl.InitArg{ .None = {} }, .Keyword_enum => blk: { if (p.eatToken(.LParen) != null) { const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); break :blk Node.ContainerDecl.InitArg{ .Type = expr }; } break :blk Node.ContainerDecl.InitArg{ .None = {} }; }, .Keyword_union => blk: { if (p.eatToken(.LParen) != null) { if (p.eatToken(.Keyword_enum) != null) { if (p.eatToken(.LParen) != null) { const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); _ = try p.expectToken(.RParen); break :blk Node.ContainerDecl.InitArg{ .Enum = expr }; } _ = try p.expectToken(.RParen); break :blk Node.ContainerDecl.InitArg{ .Enum = null }; } const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); break :blk Node.ContainerDecl.InitArg{ .Type = expr }; } break :blk Node.ContainerDecl.InitArg{ .None = {} }; }, else => { p.putBackToken(kind_token); return null; }, }; return ContainerDeclType{ .kind_token = kind_token, .init_arg_expr = init_arg_expr, }; } /// ByteAlign <- KEYWORD_align LPAREN Expr RPAREN fn parseByteAlign(p: *Parser) !?*Node { _ = p.eatToken(.Keyword_align) orelse return null; _ = try p.expectToken(.LParen); const expr = try p.expectNode(parseExpr, .{ .ExpectedExpr = .{ .token = p.tok_i }, }); _ = try p.expectToken(.RParen); return expr; } /// IdentifierList <- (IDENTIFIER COMMA)* IDENTIFIER? /// Only ErrorSetDecl parses an IdentifierList fn parseErrorTagList(p: *Parser) ![]*Node { return ListParseFn(*Node, parseErrorTag)(p); } /// SwitchProngList <- (SwitchProng COMMA)* SwitchProng? fn parseSwitchProngList(p: *Parser) ![]*Node { return ListParseFn(*Node, parseSwitchProng)(p); } /// AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem? fn parseAsmOutputList(p: *Parser) Error![]Node.Asm.Output { return ListParseFn(Node.Asm.Output, parseAsmOutputItem)(p); } /// AsmInputList <- (AsmInputItem COMMA)* AsmInputItem? fn parseAsmInputList(p: *Parser) Error![]Node.Asm.Input { return ListParseFn(Node.Asm.Input, parseAsmInputItem)(p); } /// ParamDeclList <- (ParamDecl COMMA)* ParamDecl? fn parseParamDeclList(p: *Parser) ![]Node.FnProto.ParamDecl { return ListParseFn(Node.FnProto.ParamDecl, parseParamDecl)(p); } const NodeParseFn = fn (p: *Parser) Error!?*Node; fn ListParseFn(comptime E: type, comptime nodeParseFn: anytype) ParseFn([]E) { return struct { pub fn parse(p: *Parser) ![]E { var list = std.ArrayList(E).init(p.gpa); defer list.deinit(); while (try nodeParseFn(p)) |item| { try list.append(item); switch (p.token_ids[p.tok_i]) { .Comma => _ = p.nextToken(), // all possible delimiters .Colon, .RParen, .RBrace, .RBracket => break, else => { // this is likely just a missing comma, // continue parsing this list and give an error try p.errors.append(p.gpa, .{ .ExpectedToken = .{ .token = p.tok_i, .expected_id = .Comma }, }); }, } } return list.toOwnedSlice(); } }.parse; } fn SimpleBinOpParseFn(comptime token: Token.Id, comptime op: Node.Tag) NodeParseFn { return struct { pub fn parse(p: *Parser) Error!?*Node { const op_token = if (token == .Keyword_and) switch (p.token_ids[p.tok_i]) { .Keyword_and => p.nextToken(), .Invalid_ampersands => blk: { try p.errors.append(p.gpa, .{ .InvalidAnd = .{ .token = p.tok_i }, }); break :blk p.nextToken(); }, else => return null, } else p.eatToken(token) orelse return null; const node = try p.arena.allocator.create(Node.SimpleInfixOp); node.* = .{ .base = .{ .tag = op }, .op_token = op_token, .lhs = undefined, // set by caller .rhs = undefined, // set by caller }; return &node.base; } }.parse; } // Helper parsers not included in the grammar fn parseBuiltinCall(p: *Parser) !?*Node { const token = p.eatToken(.Builtin) orelse return null; const params = (try p.parseFnCallArguments()) orelse { try p.errors.append(p.gpa, .{ .ExpectedParamList = .{ .token = p.tok_i }, }); // lets pretend this was an identifier so we can continue parsing const node = try p.arena.allocator.create(Node.Identifier); node.* = .{ .token = token, }; return &node.base; }; defer p.gpa.free(params.list); const node = try Node.BuiltinCall.alloc(&p.arena.allocator, params.list.len); node.* = .{ .builtin_token = token, .params_len = params.list.len, .rparen_token = params.rparen, }; std.mem.copy(*Node, node.params(), params.list); return &node.base; } fn parseErrorTag(p: *Parser) !?*Node { const doc_comments = try p.parseDocComment(); // no need to rewind on failure const token = p.eatToken(.Identifier) orelse return null; const node = try p.arena.allocator.create(Node.ErrorTag); node.* = .{ .doc_comments = doc_comments, .name_token = token, }; return &node.base; } fn parseIdentifier(p: *Parser) !?*Node { const token = p.eatToken(.Identifier) orelse return null; const node = try p.arena.allocator.create(Node.Identifier); node.* = .{ .token = token, }; return &node.base; } fn parseAnyType(p: *Parser) !?*Node { const token = p.eatToken(.Keyword_anytype) orelse p.eatToken(.Keyword_var) orelse return null; // TODO remove in next release cycle const node = try p.arena.allocator.create(Node.AnyType); node.* = .{ .token = token, }; return &node.base; } fn createLiteral(p: *Parser, comptime T: type, token: TokenIndex) !*Node { const result = try p.arena.allocator.create(T); result.* = T{ .token = token, }; return &result.base; } fn parseStringLiteralSingle(p: *Parser) !?*Node { if (p.eatToken(.StringLiteral)) |token| { const node = try p.arena.allocator.create(Node.StringLiteral); node.* = .{ .token = token, }; return &node.base; } return null; } // string literal or multiline string literal fn parseStringLiteral(p: *Parser) !?*Node { if (try p.parseStringLiteralSingle()) |node| return node; if (p.eatToken(.MultilineStringLiteralLine)) |first_line| { const start_tok_i = p.tok_i; var tok_i = start_tok_i; var count: usize = 1; // including first_line while (true) : (tok_i += 1) { switch (p.token_ids[tok_i]) { .LineComment => continue, .MultilineStringLiteralLine => count += 1, else => break, } } const node = try Node.MultilineStringLiteral.alloc(&p.arena.allocator, count); node.* = .{ .lines_len = count }; const lines = node.lines(); tok_i = start_tok_i; lines[0] = first_line; count = 1; while (true) : (tok_i += 1) { switch (p.token_ids[tok_i]) { .LineComment => continue, .MultilineStringLiteralLine => { lines[count] = tok_i; count += 1; }, else => break, } } p.tok_i = tok_i; return &node.base; } return null; } fn parseIntegerLiteral(p: *Parser) !?*Node { const token = p.eatToken(.IntegerLiteral) orelse return null; const node = try p.arena.allocator.create(Node.IntegerLiteral); node.* = .{ .token = token, }; return &node.base; } fn parseFloatLiteral(p: *Parser) !?*Node { const token = p.eatToken(.FloatLiteral) orelse return null; const node = try p.arena.allocator.create(Node.FloatLiteral); node.* = .{ .token = token, }; return &node.base; } fn parseTry(p: *Parser) !?*Node { const token = p.eatToken(.Keyword_try) orelse return null; const node = try p.arena.allocator.create(Node.SimplePrefixOp); node.* = .{ .base = .{ .tag = .Try }, .op_token = token, .rhs = undefined, // set by caller }; return &node.base; } /// IfPrefix Body (KEYWORD_else Payload? Body)? fn parseIf(p: *Parser, bodyParseFn: NodeParseFn) !?*Node { const node = (try p.parseIfPrefix()) orelse return null; const if_prefix = node.cast(Node.If).?; if_prefix.body = try p.expectNode(bodyParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); const else_token = p.eatToken(.Keyword_else) orelse return node; const payload = try p.parsePayload(); const else_expr = try p.expectNode(bodyParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); const else_node = try p.arena.allocator.create(Node.Else); else_node.* = .{ .else_token = else_token, .payload = payload, .body = else_expr, }; if_prefix.@"else" = else_node; return node; } /// Eat a multiline doc comment fn parseDocComment(p: *Parser) !?*Node.DocComment { if (p.eatToken(.DocComment)) |first_line| { while (p.eatToken(.DocComment)) |_| {} const node = try p.arena.allocator.create(Node.DocComment); node.* = .{ .first_line = first_line }; return node; } return null; } fn tokensOnSameLine(p: *Parser, token1: TokenIndex, token2: TokenIndex) bool { return std.mem.indexOfScalar(u8, p.source[p.token_locs[token1].end..p.token_locs[token2].start], '\n') == null; } /// Eat a single-line doc comment on the same line as another node fn parseAppendedDocComment(p: *Parser, after_token: TokenIndex) !?*Node.DocComment { const comment_token = p.eatToken(.DocComment) orelse return null; if (p.tokensOnSameLine(after_token, comment_token)) { const node = try p.arena.allocator.create(Node.DocComment); node.* = .{ .first_line = comment_token }; return node; } p.putBackToken(comment_token); return null; } /// Op* Child fn parsePrefixOpExpr(p: *Parser, comptime opParseFn: NodeParseFn, comptime childParseFn: NodeParseFn) Error!?*Node { if (try opParseFn(p)) |first_op| { var rightmost_op = first_op; while (true) { switch (rightmost_op.tag) { .AddressOf, .Await, .BitNot, .BoolNot, .OptionalType, .Negation, .NegationWrap, .Resume, .Try, => { if (try opParseFn(p)) |rhs| { rightmost_op.cast(Node.SimplePrefixOp).?.rhs = rhs; rightmost_op = rhs; } else break; }, .ArrayType => { if (try opParseFn(p)) |rhs| { rightmost_op.cast(Node.ArrayType).?.rhs = rhs; rightmost_op = rhs; } else break; }, .ArrayTypeSentinel => { if (try opParseFn(p)) |rhs| { rightmost_op.cast(Node.ArrayTypeSentinel).?.rhs = rhs; rightmost_op = rhs; } else break; }, .SliceType => { if (try opParseFn(p)) |rhs| { rightmost_op.cast(Node.SliceType).?.rhs = rhs; rightmost_op = rhs; } else break; }, .PtrType => { var ptr_type = rightmost_op.cast(Node.PtrType).?; // If the token encountered was **, there will be two nodes if (p.token_ids[ptr_type.op_token] == .AsteriskAsterisk) { rightmost_op = ptr_type.rhs; ptr_type = rightmost_op.cast(Node.PtrType).?; } if (try opParseFn(p)) |rhs| { ptr_type.rhs = rhs; rightmost_op = rhs; } else break; }, .AnyFrameType => { const prom = rightmost_op.cast(Node.AnyFrameType).?; if (try opParseFn(p)) |rhs| { prom.result.?.return_type = rhs; rightmost_op = rhs; } else break; }, else => unreachable, } } // If any prefix op existed, a child node on the RHS is required switch (rightmost_op.tag) { .AddressOf, .Await, .BitNot, .BoolNot, .OptionalType, .Negation, .NegationWrap, .Resume, .Try, => { const prefix_op = rightmost_op.cast(Node.SimplePrefixOp).?; prefix_op.rhs = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); }, .ArrayType => { const prefix_op = rightmost_op.cast(Node.ArrayType).?; prefix_op.rhs = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); }, .ArrayTypeSentinel => { const prefix_op = rightmost_op.cast(Node.ArrayTypeSentinel).?; prefix_op.rhs = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); }, .PtrType => { const prefix_op = rightmost_op.cast(Node.PtrType).?; prefix_op.rhs = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); }, .SliceType => { const prefix_op = rightmost_op.cast(Node.SliceType).?; prefix_op.rhs = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); }, .AnyFrameType => { const prom = rightmost_op.cast(Node.AnyFrameType).?; prom.result.?.return_type = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); }, else => unreachable, } return first_op; } // Otherwise, the child node is optional return childParseFn(p); } /// Child (Op Child)* /// Child (Op Child)? fn parseBinOpExpr( p: *Parser, opParseFn: NodeParseFn, childParseFn: NodeParseFn, chain: enum { Once, Infinitely, }, ) Error!?*Node { var res = (try childParseFn(p)) orelse return null; while (try opParseFn(p)) |node| { const right = try p.expectNode(childParseFn, .{ .InvalidToken = .{ .token = p.tok_i }, }); const left = res; res = node; if (node.castTag(.Catch)) |op| { op.lhs = left; op.rhs = right; } else if (node.cast(Node.SimpleInfixOp)) |op| { op.lhs = left; op.rhs = right; } switch (chain) { .Once => break, .Infinitely => continue, } } return res; } fn createInfixOp(p: *Parser, op_token: TokenIndex, tag: Node.Tag) !*Node { const node = try p.arena.allocator.create(Node.SimpleInfixOp); node.* = .{ .base = Node{ .tag = tag }, .op_token = op_token, .lhs = undefined, // set by caller .rhs = undefined, // set by caller }; return &node.base; } fn eatToken(p: *Parser, id: Token.Id) ?TokenIndex { return if (p.token_ids[p.tok_i] == id) p.nextToken() else null; } fn expectToken(p: *Parser, id: Token.Id) Error!TokenIndex { return (try p.expectTokenRecoverable(id)) orelse error.ParseError; } fn expectTokenRecoverable(p: *Parser, id: Token.Id) !?TokenIndex { const token = p.nextToken(); if (p.token_ids[token] != id) { try p.errors.append(p.gpa, .{ .ExpectedToken = .{ .token = token, .expected_id = id }, }); // go back so that we can recover properly p.putBackToken(token); return null; } return token; } fn nextToken(p: *Parser) TokenIndex { const result = p.tok_i; p.tok_i += 1; assert(p.token_ids[result] != .LineComment); if (p.tok_i >= p.token_ids.len) return result; while (true) { if (p.token_ids[p.tok_i] != .LineComment) return result; p.tok_i += 1; } } fn putBackToken(p: *Parser, putting_back: TokenIndex) void { while (p.tok_i > 0) { p.tok_i -= 1; if (p.token_ids[p.tok_i] == .LineComment) continue; assert(putting_back == p.tok_i); return; } } fn expectNode( p: *Parser, parseFn: NodeParseFn, /// if parsing fails err: AstError, ) Error!*Node { return (try p.expectNodeRecoverable(parseFn, err)) orelse return error.ParseError; } fn expectNodeRecoverable( p: *Parser, parseFn: NodeParseFn, /// if parsing fails err: AstError, ) !?*Node { return (try parseFn(p)) orelse { try p.errors.append(p.gpa, err); return null; }; } }; fn ParseFn(comptime T: type) type { return fn (p: *Parser) Error!T; } test "std.zig.parser" { _ = @import("parser_test.zig"); }
const stroo = extern struct { moo: ?[*c]u8, }; export fn testf(fluff: *stroo) void { _ = fluff; } // error // backend=stage1 // target=native // // tmp.zig:2:5: error: extern structs cannot contain fields of type '?[*c]u8'
//! This file contains thin wrappers around OS-specific APIs, with these //! specific goals in mind: //! * Convert "errno"-style error codes into Zig errors. //! * When null-terminated byte buffers are required, provide APIs which accept //! slices as well as APIs which accept null-terminated byte buffers. Same goes //! for UTF-16LE encoding. //! * Where operating systems share APIs, e.g. POSIX, these thin wrappers provide //! cross platform abstracting. //! * When there exists a corresponding libc function and linking libc, the libc //! implementation is used. Exceptions are made for known buggy areas of libc. //! On Linux libc can be side-stepped by using `std.os.linux` directly. //! * For Windows, this file represents the API that libc would provide for //! Windows. For thin wrappers around Windows-specific APIs, see `std.os.windows`. //! Note: The Zig standard library does not support POSIX thread cancellation, and //! in general EINTR is handled by trying again. const root = @import("root"); const std = @import("std.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; const math = std.math; const mem = std.mem; const elf = std.elf; const fs = std.fs; const dl = @import("dynamic_library.zig"); const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES; const is_windows = builtin.os.tag == .windows; const Allocator = std.mem.Allocator; const Preopen = std.fs.wasi.Preopen; const PreopenList = std.fs.wasi.PreopenList; pub const darwin = @import("os/darwin.zig"); pub const dragonfly = std.c; pub const freebsd = std.c; pub const haiku = std.c; pub const netbsd = std.c; pub const openbsd = std.c; pub const solaris = std.c; pub const linux = @import("os/linux.zig"); pub const plan9 = @import("os/plan9.zig"); pub const uefi = @import("os/uefi.zig"); pub const wasi = @import("os/wasi.zig"); pub const windows = @import("os/windows.zig"); pub const posix_spawn = @import("os/posix_spawn.zig"); comptime { assert(@import("std") == std); // std lib tests require --zig-lib-dir } test { _ = darwin; _ = linux; _ = uefi; _ = wasi; _ = windows; _ = posix_spawn; _ = @import("os/test.zig"); } /// Applications can override the `system` API layer in their root source file. /// Otherwise, when linking libc, this is the C API. /// When not linking libc, it is the OS-specific system interface. pub const system = if (@hasDecl(root, "os") and root.os != @This()) root.os.system else if (builtin.link_libc or is_windows) std.c else switch (builtin.os.tag) { .linux => linux, .wasi => wasi, .uefi => uefi, else => struct {}, }; pub const AF = system.AF; pub const AF_SUN = system.AF_SUN; pub const ARCH = system.ARCH; pub const AT = system.AT; pub const AT_SUN = system.AT_SUN; pub const CLOCK = system.CLOCK; pub const CPU_COUNT = system.CPU_COUNT; pub const CTL = system.CTL; pub const DT = system.DT; pub const E = system.E; pub const Elf_Symndx = system.Elf_Symndx; pub const F = system.F; pub const FD_CLOEXEC = system.FD_CLOEXEC; pub const Flock = system.Flock; pub const HOST_NAME_MAX = system.HOST_NAME_MAX; pub const IFNAMESIZE = system.IFNAMESIZE; pub const IOV_MAX = system.IOV_MAX; pub const IPPROTO = system.IPPROTO; pub const KERN = system.KERN; pub const Kevent = system.Kevent; pub const LOCK = system.LOCK; pub const MADV = system.MADV; pub const MAP = system.MAP; pub const MSF = system.MSF; pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN; pub const MFD = system.MFD; pub const MMAP2_UNIT = system.MMAP2_UNIT; pub const MSG = system.MSG; pub const NAME_MAX = system.NAME_MAX; pub const O = switch (builtin.os.tag) { // We want to expose the POSIX-like OFLAGS, so we use std.c.wasi.O instead // of std.os.wasi.O, which is for non-POSIX-like `wasi.path_open`, etc. .wasi => std.c.O, else => system.O, }; pub const PATH_MAX = system.PATH_MAX; pub const POLL = system.POLL; pub const POSIX_FADV = system.POSIX_FADV; pub const PR = system.PR; pub const PROT = system.PROT; pub const REG = system.REG; pub const RIGHT = system.RIGHT; pub const RLIM = system.RLIM; pub const RR = system.RR; pub const S = system.S; pub const SA = system.SA; pub const SC = system.SC; pub const _SC = system._SC; pub const SEEK = system.SEEK; pub const SHUT = system.SHUT; pub const SIG = system.SIG; pub const SIOCGIFINDEX = system.SIOCGIFINDEX; pub const SO = system.SO; pub const SOCK = system.SOCK; pub const SOL = system.SOL; pub const STDERR_FILENO = system.STDERR_FILENO; pub const STDIN_FILENO = system.STDIN_FILENO; pub const STDOUT_FILENO = system.STDOUT_FILENO; pub const SYS = system.SYS; pub const Sigaction = system.Sigaction; pub const Stat = system.Stat; pub const TCSA = system.TCSA; pub const TCP = system.TCP; pub const VDSO = system.VDSO; pub const W = system.W; pub const addrinfo = system.addrinfo; pub const blkcnt_t = system.blkcnt_t; pub const blksize_t = system.blksize_t; pub const clock_t = system.clock_t; pub const cpu_set_t = system.cpu_set_t; pub const dev_t = system.dev_t; pub const dl_phdr_info = system.dl_phdr_info; pub const empty_sigset = system.empty_sigset; pub const fd_t = system.fd_t; pub const fdflags_t = system.fdflags_t; pub const fdstat_t = system.fdstat_t; pub const gid_t = system.gid_t; pub const ifreq = system.ifreq; pub const ino_t = system.ino_t; pub const lookupflags_t = system.lookupflags_t; pub const mcontext_t = system.mcontext_t; pub const mode_t = system.mode_t; pub const msghdr = system.msghdr; pub const msghdr_const = system.msghdr_const; pub const nfds_t = system.nfds_t; pub const nlink_t = system.nlink_t; pub const off_t = system.off_t; pub const oflags_t = system.oflags_t; pub const pid_t = system.pid_t; pub const pollfd = system.pollfd; pub const port_t = system.port_t; pub const port_event = system.port_event; pub const port_notify = system.port_notify; pub const file_obj = system.file_obj; pub const rights_t = system.rights_t; pub const rlim_t = system.rlim_t; pub const rlimit = system.rlimit; pub const rlimit_resource = system.rlimit_resource; pub const rusage = system.rusage; pub const sa_family_t = system.sa_family_t; pub const siginfo_t = system.siginfo_t; pub const sigset_t = system.sigset_t; pub const sockaddr = system.sockaddr; pub const socklen_t = system.socklen_t; pub const stack_t = system.stack_t; pub const tcflag_t = system.tcflag_t; pub const termios = system.termios; pub const time_t = system.time_t; pub const timespec = system.timespec; pub const timestamp_t = system.timestamp_t; pub const timeval = system.timeval; pub const timezone = system.timezone; pub const ucontext_t = system.ucontext_t; pub const uid_t = system.uid_t; pub const user_desc = system.user_desc; pub const utsname = system.utsname; pub const F_OK = system.F_OK; pub const R_OK = system.R_OK; pub const W_OK = system.W_OK; pub const X_OK = system.X_OK; pub const iovec = extern struct { iov_base: [*]u8, iov_len: usize, }; pub const iovec_const = extern struct { iov_base: [*]const u8, iov_len: usize, }; pub const LOG = struct { /// system is unusable pub const EMERG = 0; /// action must be taken immediately pub const ALERT = 1; /// critical conditions pub const CRIT = 2; /// error conditions pub const ERR = 3; /// warning conditions pub const WARNING = 4; /// normal but significant condition pub const NOTICE = 5; /// informational pub const INFO = 6; /// debug-level messages pub const DEBUG = 7; }; /// An fd-relative file path /// /// This is currently only used for WASI-specific functionality, but the concept /// is the same as the dirfd/pathname pairs in the `*at(...)` POSIX functions. pub const RelativePathWasi = struct { /// Handle to directory dir_fd: fd_t, /// Path to resource within `dir_fd`. relative_path: []const u8, }; pub const socket_t = if (builtin.os.tag == .windows) windows.ws2_32.SOCKET else fd_t; /// See also `getenv`. Populated by startup code before main(). /// TODO this is a footgun because the value will be undefined when using `zig build-lib`. /// https://github.com/ziglang/zig/issues/4524 pub var environ: [][*:0]u8 = undefined; /// Populated by startup code before main(). /// Not available on WASI or Windows without libc. See `std.process.argsAlloc` /// or `std.process.argsWithAllocator` for a cross-platform alternative. pub var argv: [][*:0]u8 = if (builtin.link_libc) undefined else switch (builtin.os.tag) { .windows => @compileError("argv isn't supported on Windows: use std.process.argsAlloc instead"), .wasi => @compileError("argv isn't supported on WASI: use std.process.argsAlloc instead"), else => undefined, }; /// To obtain errno, call this function with the return value of the /// system function call. For some systems this will obtain the value directly /// from the return code; for others it will use a thread-local errno variable. /// Therefore, this function only returns a well-defined value when it is called /// directly after the system function call which one wants to learn the errno /// value of. pub const errno = system.getErrno; /// Closes the file descriptor. /// This function is not capable of returning any indication of failure. An /// application which wants to ensure writes have succeeded before closing /// must call `fsync` before `close`. /// Note: The Zig standard library does not support POSIX thread cancellation. pub fn close(fd: fd_t) void { if (builtin.os.tag == .windows) { return windows.CloseHandle(fd); } if (builtin.os.tag == .wasi and !builtin.link_libc) { _ = wasi.fd_close(fd); return; } if (comptime builtin.target.isDarwin()) { // This avoids the EINTR problem. switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) { .BADF => unreachable, // Always a race condition. else => return, } } switch (errno(system.close(fd))) { .BADF => unreachable, // Always a race condition. .INTR => return, // This is still a success. See https://github.com/ziglang/zig/issues/2425 else => return, } } pub const FChmodError = error{ AccessDenied, InputOutput, SymLinkLoop, FileNotFound, SystemResources, ReadOnlyFileSystem, } || UnexpectedError; /// Changes the mode of the file referred to by the file descriptor. /// The process must have the correct privileges in order to do this /// successfully, or must have the effective user ID matching the owner /// of the file. pub fn fchmod(fd: fd_t, mode: mode_t) FChmodError!void { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) @compileError("Unsupported OS"); while (true) { const res = system.fchmod(fd, mode); switch (system.getErrno(res)) { .SUCCESS => return, .INTR => continue, .BADF => unreachable, // Can be reached if the fd refers to a directory opened without `OpenDirOptions{ .iterate = true }` .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .IO => return error.InputOutput, .LOOP => return error.SymLinkLoop, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.FileNotFound, .PERM => return error.AccessDenied, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } } pub const FChownError = error{ AccessDenied, InputOutput, SymLinkLoop, FileNotFound, SystemResources, ReadOnlyFileSystem, } || UnexpectedError; /// Changes the owner and group of the file referred to by the file descriptor. /// The process must have the correct privileges in order to do this /// successfully. The group may be changed by the owner of the directory to /// any group of which the owner is a member. If the owner or group is /// specified as `null`, the ID is not changed. pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) @compileError("Unsupported OS"); while (true) { const res = system.fchown(fd, owner orelse @as(u32, 0) -% 1, group orelse @as(u32, 0) -% 1); switch (system.getErrno(res)) { .SUCCESS => return, .INTR => continue, .BADF => unreachable, // Can be reached if the fd refers to a directory opened without `OpenDirOptions{ .iterate = true }` .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .IO => return error.InputOutput, .LOOP => return error.SymLinkLoop, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.FileNotFound, .PERM => return error.AccessDenied, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } } pub const GetRandomError = OpenError; /// Obtain a series of random bytes. These bytes can be used to seed user-space /// random number generators or for cryptographic purposes. /// When linking against libc, this calls the /// appropriate OS-specific library call. Otherwise it uses the zig standard /// library implementation. pub fn getrandom(buffer: []u8) GetRandomError!void { if (builtin.os.tag == .windows) { return windows.RtlGenRandom(buffer); } if (builtin.os.tag == .linux or builtin.os.tag == .freebsd) { var buf = buffer; const use_c = builtin.os.tag != .linux or std.c.versionCheck(std.builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok; while (buf.len != 0) { const res = if (use_c) blk: { const rc = std.c.getrandom(buf.ptr, buf.len, 0); break :blk .{ .num_read = @bitCast(usize, rc), .err = std.c.getErrno(rc), }; } else blk: { const rc = linux.getrandom(buf.ptr, buf.len, 0); break :blk .{ .num_read = rc, .err = linux.getErrno(rc), }; }; switch (res.err) { .SUCCESS => buf = buf[res.num_read..], .INVAL => unreachable, .FAULT => unreachable, .INTR => continue, .NOSYS => return getRandomBytesDevURandom(buf), else => return unexpectedErrno(res.err), } } return; } switch (builtin.os.tag) { .netbsd, .openbsd, .macos, .ios, .tvos, .watchos => { system.arc4random_buf(buffer.ptr, buffer.len); return; }, .wasi => switch (wasi.random_get(buffer.ptr, buffer.len)) { .SUCCESS => return, else => |err| return unexpectedErrno(err), }, else => return getRandomBytesDevURandom(buffer), } } fn getRandomBytesDevURandom(buf: []u8) !void { const fd = try openZ("/dev/urandom", O.RDONLY | O.CLOEXEC, 0); defer close(fd); const st = try fstat(fd); if (!S.ISCHR(st.mode)) { return error.NoDevice; } const file = std.fs.File{ .handle = fd, .capable_io_mode = .blocking, .intended_io_mode = .blocking, }; const stream = file.reader(); stream.readNoEof(buf) catch return error.Unexpected; } /// Causes abnormal process termination. /// If linking against libc, this calls the abort() libc function. Otherwise /// it raises SIGABRT followed by SIGKILL and finally lo pub fn abort() noreturn { @setCold(true); // MSVCRT abort() sometimes opens a popup window which is undesirable, so // even when linking libc on Windows we use our own abort implementation. // See https://github.com/ziglang/zig/issues/2071 for more details. if (builtin.os.tag == .windows) { if (builtin.mode == .Debug) { @breakpoint(); } windows.kernel32.ExitProcess(3); } if (!builtin.link_libc and builtin.os.tag == .linux) { raise(SIG.ABRT) catch {}; // TODO the rest of the implementation of abort() from musl libc here raise(SIG.KILL) catch {}; exit(127); } if (builtin.os.tag == .uefi) { exit(0); // TODO choose appropriate exit code } if (builtin.os.tag == .wasi) { @breakpoint(); exit(1); } system.abort(); } pub const RaiseError = UnexpectedError; pub fn raise(sig: u8) RaiseError!void { if (builtin.link_libc) { switch (errno(system.raise(sig))) { .SUCCESS => return, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .linux) { var set: sigset_t = undefined; // block application signals _ = linux.sigprocmask(SIG.BLOCK, &linux.app_mask, &set); const tid = linux.gettid(); const rc = linux.tkill(tid, sig); // restore signal mask _ = linux.sigprocmask(SIG.SETMASK, &set, null); switch (errno(rc)) { .SUCCESS => return, else => |err| return unexpectedErrno(err), } } @compileError("std.os.raise unimplemented for this target"); } pub const KillError = error{PermissionDenied} || UnexpectedError; pub fn kill(pid: pid_t, sig: u8) KillError!void { switch (errno(system.kill(pid, sig))) { .SUCCESS => return, .INVAL => unreachable, // invalid signal .PERM => return error.PermissionDenied, .SRCH => unreachable, // always a race condition else => |err| return unexpectedErrno(err), } } /// Exits the program cleanly with the specified status code. pub fn exit(status: u8) noreturn { if (builtin.link_libc) { system.exit(status); } if (builtin.os.tag == .windows) { windows.kernel32.ExitProcess(status); } if (builtin.os.tag == .wasi) { wasi.proc_exit(status); } if (builtin.os.tag == .linux and !builtin.single_threaded) { linux.exit_group(status); } if (builtin.os.tag == .uefi) { // exit() is only avaliable if exitBootServices() has not been called yet. // This call to exit should not fail, so we don't care about its return value. if (uefi.system_table.boot_services) |bs| { _ = bs.exit(uefi.handle, @intToEnum(uefi.Status, status), 0, null); } // If we can't exit, reboot the system instead. uefi.system_table.runtime_services.resetSystem(uefi.tables.ResetType.ResetCold, @intToEnum(uefi.Status, status), 0, null); } system.exit(status); } pub const ReadError = error{ InputOutput, SystemResources, IsDir, OperationAborted, BrokenPipe, ConnectionResetByPeer, ConnectionTimedOut, NotOpenForReading, /// This error occurs when no global event loop is configured, /// and reading from the file descriptor would block. WouldBlock, /// In WASI, this error occurs when the file descriptor does /// not hold the required rights to read from it. AccessDenied, } || UnexpectedError; /// Returns the number of bytes that were read, which can be less than /// buf.len. If 0 bytes were read, that means EOF. /// If `fd` is opened in non blocking mode, the function will return error.WouldBlock /// when EAGAIN is received. /// /// Linux has a limit on how many bytes may be transferred in one `read` call, which is `0x7ffff000` /// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as /// well as stuffing the errno codes into the last `4096` values. This is noted on the `read` man page. /// The limit on Darwin is `0x7fffffff`, trying to read more than that returns EINVAL. /// The corresponding POSIX limit is `math.maxInt(isize)`. pub fn read(fd: fd_t, buf: []u8) ReadError!usize { if (builtin.os.tag == .windows) { return windows.ReadFile(fd, buf, null, std.io.default_mode); } if (builtin.os.tag == .wasi and !builtin.link_libc) { const iovs = [1]iovec{iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }}; var nread: usize = undefined; switch (wasi.fd_read(fd, &iovs, iovs.len, &nread)) { .SUCCESS => return nread, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForReading, // Can be a race condition. .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .CONNRESET => return error.ConnectionResetByPeer, .TIMEDOUT => return error.ConnectionTimedOut, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } // Prevents EINVAL. const max_count = switch (builtin.os.tag) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = @minimum(max_count, buf.len); while (true) { const rc = system.read(fd, buf.ptr, adjusted_len); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // Can be a race condition. .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .CONNRESET => return error.ConnectionResetByPeer, .TIMEDOUT => return error.ConnectionTimedOut, else => |err| return unexpectedErrno(err), } } } /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// /// For POSIX systems, if `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// On Windows, if the application has a global event loop enabled, I/O Completion Ports are /// used to perform the I/O. `error.WouldBlock` is not possible on Windows. /// /// This operation is non-atomic on the following systems: /// * Windows /// On these systems, the read races with concurrent writes to the same file descriptor. pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { if (builtin.os.tag == .windows) { // TODO improve this to use ReadFileScatter if (iov.len == 0) return @as(usize, 0); const first = iov[0]; return read(fd, first.iov_base[0..first.iov_len]); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var nread: usize = undefined; switch (wasi.fd_read(fd, iov.ptr, iov.len, &nread)) { .SUCCESS => return nread, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, // currently not support in WASI .BADF => return error.NotOpenForReading, // can be a race condition .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31); while (true) { // TODO handle the case when iov_len is too large and get rid of this @intCast const rc = system.readv(fd, iov.ptr, iov_count); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // can be a race condition .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } } pub const PReadError = ReadError || error{Unseekable}; /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// /// Retries when interrupted by a signal. /// /// For POSIX systems, if `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// On Windows, if the application has a global event loop enabled, I/O Completion Ports are /// used to perform the I/O. `error.WouldBlock` is not possible on Windows. /// /// Linux has a limit on how many bytes may be transferred in one `pread` call, which is `0x7ffff000` /// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as /// well as stuffing the errno codes into the last `4096` values. This is noted on the `read` man page. /// The limit on Darwin is `0x7fffffff`, trying to read more than that returns EINVAL. /// The corresponding POSIX limit is `math.maxInt(isize)`. pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize { if (builtin.os.tag == .windows) { return windows.ReadFile(fd, buf, offset, std.io.default_mode); } if (builtin.os.tag == .wasi and !builtin.link_libc) { const iovs = [1]iovec{iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }}; var nread: usize = undefined; switch (wasi.fd_pread(fd, &iovs, iovs.len, offset, &nread)) { .SUCCESS => return nread, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForReading, // Can be a race condition. .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .CONNRESET => return error.ConnectionResetByPeer, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } // Prevent EINVAL. const max_count = switch (builtin.os.tag) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = @minimum(max_count, buf.len); const pread_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.pread64 else system.pread; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned while (true) { const rc = pread_sym(fd, buf.ptr, adjusted_len, ioffset); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // Can be a race condition. .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .CONNRESET => return error.ConnectionResetByPeer, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, else => |err| return unexpectedErrno(err), } } } pub const TruncateError = error{ FileTooBig, InputOutput, FileBusy, /// In WASI, this error occurs when the file descriptor does /// not hold the required rights to call `ftruncate` on it. AccessDenied, } || UnexpectedError; pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { if (builtin.os.tag == .windows) { var io_status_block: windows.IO_STATUS_BLOCK = undefined; var eof_info = windows.FILE_END_OF_FILE_INFORMATION{ .EndOfFile = @bitCast(windows.LARGE_INTEGER, length), }; const rc = windows.ntdll.NtSetInformationFile( fd, &io_status_block, &eof_info, @sizeOf(windows.FILE_END_OF_FILE_INFORMATION), .FileEndOfFileInformation, ); switch (rc) { .SUCCESS => return, .INVALID_HANDLE => unreachable, // Handle not open for writing .ACCESS_DENIED => return error.AccessDenied, else => return windows.unexpectedStatus(rc), } } if (builtin.os.tag == .wasi and !builtin.link_libc) { switch (wasi.fd_filestat_set_size(fd, length)) { .SUCCESS => return, .INTR => unreachable, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .PERM => return error.AccessDenied, .TXTBSY => return error.FileBusy, .BADF => unreachable, // Handle not open for writing .INVAL => unreachable, // Handle not open for writing .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } while (true) { const ftruncate_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.ftruncate64 else system.ftruncate; const ilen = @bitCast(i64, length); // the OS treats this as unsigned switch (errno(ftruncate_sym(fd, ilen))) { .SUCCESS => return, .INTR => continue, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .PERM => return error.AccessDenied, .TXTBSY => return error.FileBusy, .BADF => unreachable, // Handle not open for writing .INVAL => unreachable, // Handle not open for writing else => |err| return unexpectedErrno(err), } } } /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// /// Retries when interrupted by a signal. /// /// For POSIX systems, if `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// On Windows, if the application has a global event loop enabled, I/O Completion Ports are /// used to perform the I/O. `error.WouldBlock` is not possible on Windows. /// /// This operation is non-atomic on the following systems: /// * Darwin /// * Windows /// On these systems, the read races with concurrent writes to the same file descriptor. pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { const have_pread_but_not_preadv = switch (builtin.os.tag) { .windows, .macos, .ios, .watchos, .tvos, .haiku => true, else => false, }; if (have_pread_but_not_preadv) { // We could loop here; but proper usage of `preadv` must handle partial reads anyway. // So we simply read into the first vector only. if (iov.len == 0) return @as(usize, 0); const first = iov[0]; return pread(fd, first.iov_base[0..first.iov_len], offset); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var nread: usize = undefined; switch (wasi.fd_pread(fd, iov.ptr, iov.len, offset, &nread)) { .SUCCESS => return nread, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForReading, // can be a race condition .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31); const preadv_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.preadv64 else system.preadv; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned while (true) { const rc = preadv_sym(fd, iov.ptr, iov_count, ioffset); switch (errno(rc)) { .SUCCESS => return @bitCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForReading, // can be a race condition .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, else => |err| return unexpectedErrno(err), } } } pub const WriteError = error{ DiskQuota, FileTooBig, InputOutput, NoSpaceLeft, /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to write to it. AccessDenied, BrokenPipe, SystemResources, OperationAborted, NotOpenForWriting, /// This error occurs when no global event loop is configured, /// and reading from the file descriptor would block. WouldBlock, /// Connection reset by peer. ConnectionResetByPeer, } || UnexpectedError; /// Write to a file descriptor. /// Retries when interrupted by a signal. /// Returns the number of bytes written. If nonzero bytes were supplied, this will be nonzero. /// /// Note that a successful write() may transfer fewer than count bytes. Such partial writes can /// occur for various reasons; for example, because there was insufficient space on the disk /// device to write all of the requested bytes, or because a blocked write() to a socket, pipe, or /// similar was interrupted by a signal handler after it had transferred some, but before it had /// transferred all of the requested bytes. In the event of a partial write, the caller can make /// another write() call to transfer the remaining bytes. The subsequent call will either /// transfer further bytes or may result in an error (e.g., if the disk is now full). /// /// For POSIX systems, if `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// On Windows, if the application has a global event loop enabled, I/O Completion Ports are /// used to perform the I/O. `error.WouldBlock` is not possible on Windows. /// /// Linux has a limit on how many bytes may be transferred in one `write` call, which is `0x7ffff000` /// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as /// well as stuffing the errno codes into the last `4096` values. This is noted on the `write` man page. /// The limit on Darwin is `0x7fffffff`, trying to read more than that returns EINVAL. /// The corresponding POSIX limit is `math.maxInt(isize)`. pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { if (builtin.os.tag == .windows) { return windows.WriteFile(fd, bytes, null, std.io.default_mode); } if (builtin.os.tag == .wasi and !builtin.link_libc) { const ciovs = [_]iovec_const{iovec_const{ .iov_base = bytes.ptr, .iov_len = bytes.len, }}; var nwritten: usize = undefined; switch (wasi.fd_write(fd, &ciovs, ciovs.len, &nwritten)) { .SUCCESS => return nwritten, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForWriting, // can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const max_count = switch (builtin.os.tag) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = @minimum(max_count, bytes.len); while (true) { const rc = system.write(fd, bytes.ptr, adjusted_len); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .CONNRESET => return error.ConnectionResetByPeer, else => |err| return unexpectedErrno(err), } } } /// Write multiple buffers to a file descriptor. /// Retries when interrupted by a signal. /// Returns the number of bytes written. If nonzero bytes were supplied, this will be nonzero. /// /// Note that a successful write() may transfer fewer bytes than supplied. Such partial writes can /// occur for various reasons; for example, because there was insufficient space on the disk /// device to write all of the requested bytes, or because a blocked write() to a socket, pipe, or /// similar was interrupted by a signal handler after it had transferred some, but before it had /// transferred all of the requested bytes. In the event of a partial write, the caller can make /// another write() call to transfer the remaining bytes. The subsequent call will either /// transfer further bytes or may result in an error (e.g., if the disk is now full). /// /// For POSIX systems, if `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// On Windows, if the application has a global event loop enabled, I/O Completion Ports are /// used to perform the I/O. `error.WouldBlock` is not possible on Windows. /// /// If `iov.len` is larger than `IOV_MAX`, a partial write will occur. pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { if (builtin.os.tag == .windows) { // TODO improve this to use WriteFileScatter if (iov.len == 0) return @as(usize, 0); const first = iov[0]; return write(fd, first.iov_base[0..first.iov_len]); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var nwritten: usize = undefined; switch (wasi.fd_write(fd, iov.ptr, iov.len, &nwritten)) { .SUCCESS => return nwritten, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForWriting, // can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @intCast(u31, iov.len); while (true) { const rc = system.writev(fd, iov.ptr, iov_count); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .CONNRESET => return error.ConnectionResetByPeer, else => |err| return unexpectedErrno(err), } } } pub const PWriteError = WriteError || error{Unseekable}; /// Write to a file descriptor, with a position offset. /// Retries when interrupted by a signal. /// Returns the number of bytes written. If nonzero bytes were supplied, this will be nonzero. /// /// Note that a successful write() may transfer fewer bytes than supplied. Such partial writes can /// occur for various reasons; for example, because there was insufficient space on the disk /// device to write all of the requested bytes, or because a blocked write() to a socket, pipe, or /// similar was interrupted by a signal handler after it had transferred some, but before it had /// transferred all of the requested bytes. In the event of a partial write, the caller can make /// another write() call to transfer the remaining bytes. The subsequent call will either /// transfer further bytes or may result in an error (e.g., if the disk is now full). /// /// For POSIX systems, if `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// On Windows, if the application has a global event loop enabled, I/O Completion Ports are /// used to perform the I/O. `error.WouldBlock` is not possible on Windows. /// /// Linux has a limit on how many bytes may be transferred in one `pwrite` call, which is `0x7ffff000` /// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as /// well as stuffing the errno codes into the last `4096` values. This is noted on the `write` man page. /// The limit on Darwin is `0x7fffffff`, trying to write more than that returns EINVAL. /// The corresponding POSIX limit is `math.maxInt(isize)`. pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { if (builtin.os.tag == .windows) { return windows.WriteFile(fd, bytes, offset, std.io.default_mode); } if (builtin.os.tag == .wasi and !builtin.link_libc) { const ciovs = [1]iovec_const{iovec_const{ .iov_base = bytes.ptr, .iov_len = bytes.len, }}; var nwritten: usize = undefined; switch (wasi.fd_pwrite(fd, &ciovs, ciovs.len, offset, &nwritten)) { .SUCCESS => return nwritten, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForWriting, // can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } // Prevent EINVAL. const max_count = switch (builtin.os.tag) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = @minimum(max_count, bytes.len); const pwrite_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.pwrite64 else system.pwrite; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned while (true) { const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, ioffset); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, else => |err| return unexpectedErrno(err), } } } /// Write multiple buffers to a file descriptor, with a position offset. /// Retries when interrupted by a signal. /// Returns the number of bytes written. If nonzero bytes were supplied, this will be nonzero. /// /// Note that a successful write() may transfer fewer than count bytes. Such partial writes can /// occur for various reasons; for example, because there was insufficient space on the disk /// device to write all of the requested bytes, or because a blocked write() to a socket, pipe, or /// similar was interrupted by a signal handler after it had transferred some, but before it had /// transferred all of the requested bytes. In the event of a partial write, the caller can make /// another write() call to transfer the remaining bytes. The subsequent call will either /// transfer further bytes or may result in an error (e.g., if the disk is now full). /// /// If `fd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. /// /// The following systems do not have this syscall, and will return partial writes if more than one /// vector is provided: /// * Darwin /// * Windows /// /// If `iov.len` is larger than `IOV_MAX`, a partial write will occur. pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usize { const have_pwrite_but_not_pwritev = switch (builtin.os.tag) { .windows, .macos, .ios, .watchos, .tvos, .haiku => true, else => false, }; if (have_pwrite_but_not_pwritev) { // We could loop here; but proper usage of `pwritev` must handle partial writes anyway. // So we simply write the first vector only. if (iov.len == 0) return @as(usize, 0); const first = iov[0]; return pwrite(fd, first.iov_base[0..first.iov_len], offset); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var nwritten: usize = undefined; switch (wasi.fd_pwrite(fd, iov.ptr, iov.len, offset, &nwritten)) { .SUCCESS => return nwritten, .INTR => unreachable, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => unreachable, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const pwritev_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.pwritev64 else system.pwritev; const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @intCast(u31, iov.len); const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned while (true) { const rc = pwritev_sym(fd, iov.ptr, iov_count, ioffset); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .PIPE => return error.BrokenPipe, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .OVERFLOW => return error.Unseekable, else => |err| return unexpectedErrno(err), } } } pub const OpenError = error{ /// In WASI, this error may occur when the provided file handle is invalid. InvalidHandle, /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to open a new resource relative to it. AccessDenied, SymLinkLoop, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, NoDevice, FileNotFound, /// The path exceeded `MAX_PATH_BYTES` bytes. NameTooLong, /// Insufficient kernel memory was available, or /// the named file is a FIFO and per-user hard limit on /// memory allocation for pipes has been reached. SystemResources, /// The file is too large to be opened. This error is unreachable /// for 64-bit targets, as well as when opening directories. FileTooBig, /// The path refers to directory but the `O.DIRECTORY` flag was not provided. IsDir, /// A new path cannot be created because the device has no room for the new file. /// This error is only reachable when the `O.CREAT` flag is provided. NoSpaceLeft, /// A component used as a directory in the path was not, in fact, a directory, or /// `O.DIRECTORY` was specified and the path was not a directory. NotDir, /// The path already exists and the `O.CREAT` and `O.EXCL` flags were provided. PathAlreadyExists, DeviceBusy, /// The underlying filesystem does not support file locks FileLocksNotSupported, BadPathName, InvalidUtf8, /// One of these three things: /// * pathname refers to an executable image which is currently being /// executed and write access was requested. /// * pathname refers to a file that is currently in use as a swap /// file, and the O_TRUNC flag was specified. /// * pathname refers to a file that is currently being read by the /// kernel (e.g., for module/firmware loading), and write access was /// requested. FileBusy, WouldBlock, } || UnexpectedError; /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `openZ`. pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t { if (builtin.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return openW(file_path_w.span(), flags, perm); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return openat(wasi.AT.FDCWD, file_path, flags, perm); } const file_path_c = try toPosixPath(file_path); return openZ(&file_path_c, flags, perm); } /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `open`. pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t { if (builtin.os.tag == .windows) { const file_path_w = try windows.cStrToPrefixedFileW(file_path); return openW(file_path_w.span(), flags, perm); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return open(mem.sliceTo(file_path, 0), flags, perm); } const open_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.open64 else system.open; while (true) { const rc = open_sym(file_path, flags, perm); switch (errno(rc)) { .SUCCESS => return @intCast(fd_t, rc), .INTR => continue, .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .FBIG => return error.FileTooBig, .OVERFLOW => return error.FileTooBig, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .MFILE => return error.ProcessFdQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NFILE => return error.SystemFdQuotaExceeded, .NODEV => return error.NoDevice, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .PERM => return error.AccessDenied, .EXIST => return error.PathAlreadyExists, .BUSY => return error.DeviceBusy, else => |err| return unexpectedErrno(err), } } } fn openOptionsFromFlagsWindows(flags: u32) windows.OpenFileOptions { const w = windows; var access_mask: w.ULONG = w.READ_CONTROL | w.FILE_WRITE_ATTRIBUTES | w.SYNCHRONIZE; if (flags & O.RDWR != 0) { access_mask |= w.GENERIC_READ | w.GENERIC_WRITE; } else if (flags & O.WRONLY != 0) { access_mask |= w.GENERIC_WRITE; } else { access_mask |= w.GENERIC_READ | w.GENERIC_WRITE; } const filter: windows.OpenFileOptions.Filter = if (flags & O.DIRECTORY != 0) .dir_only else .file_only; const follow_symlinks: bool = flags & O.NOFOLLOW == 0; const creation: w.ULONG = blk: { if (flags & O.CREAT != 0) { if (flags & O.EXCL != 0) { break :blk w.FILE_CREATE; } } break :blk w.FILE_OPEN; }; return .{ .access_mask = access_mask, .io_mode = .blocking, .creation = creation, .filter = filter, .follow_symlinks = follow_symlinks, }; } /// Windows-only. The path parameter is /// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded. /// Translates the POSIX open API call to a Windows API call. /// TODO currently, this function does not handle all flag combinations /// or makes use of perm argument. pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t { _ = perm; var options = openOptionsFromFlagsWindows(flags); options.dir = std.fs.cwd().fd; return windows.OpenFile(file_path_w, options) catch |err| switch (err) { error.WouldBlock => unreachable, error.PipeBusy => unreachable, else => |e| return e, }; } var wasi_cwd = if (builtin.os.tag == .wasi and !builtin.link_libc) struct { // List of available Preopens preopens: ?PreopenList = null, // Memory buffer for storing the relative portion of the CWD path_buffer: [MAX_PATH_BYTES]u8 = undefined, // The absolute path associated with the current working directory cwd: []const u8 = "/", }{} else undefined; /// Initialize the available Preopen list on WASI and set the CWD to `cwd_init`. /// Note that `cwd_init` corresponds to a Preopen directory, not necessarily /// a POSIX path. For example, "." matches a Preopen provided with `--dir=.` /// /// This must be called before using any relative or absolute paths with `std.os` /// functions, if you are on WASI without linking libc. /// /// The current working directory is initialized to `cwd_root`, and `cwd_root` /// is inserted as a prefix for any Preopens whose dir begins with "." /// For example: /// "./foo/bar" - canonicalizes to -> "{cwd_root}/foo/bar" /// "foo/bar" - canonicalizes to -> "/foo/bar" /// "/foo/bar" - canonicalizes to -> "/foo/bar" /// /// `cwd_root` must be an absolute path. For initialization behavior similar to /// wasi-libc, use "/" as the `cwd_root` /// /// `alloc` must not be a temporary or leak-detecting allocator, since `std.os` /// retains ownership of allocations internally and may never call free(). pub fn initPreopensWasi(alloc: Allocator, cwd_root: []const u8) !void { if (builtin.os.tag == .wasi) { if (!builtin.link_libc) { var preopen_list = PreopenList.init(alloc); errdefer preopen_list.deinit(); try preopen_list.populate(cwd_root); var path_alloc = std.heap.FixedBufferAllocator.init(&wasi_cwd.path_buffer); wasi_cwd.cwd = try path_alloc.allocator().dupe(u8, cwd_root); if (wasi_cwd.preopens) |preopens| preopens.deinit(); wasi_cwd.preopens = preopen_list; } else { // wasi-libc defaults to an effective CWD root of "/" if (!mem.eql(u8, cwd_root, "/")) return error.UnsupportedDirectory; } } } /// Resolve a relative or absolute path to an handle (`fd_t`) and a relative subpath. /// /// For absolute paths, this automatically searches among available Preopens to find /// a match. For relative paths, it uses the "emulated" CWD. /// Automatically looks up the correct Preopen corresponding to the provided path. pub fn resolvePathWasi(path: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) !RelativePathWasi { var allocator = std.heap.FixedBufferAllocator.init(out_buffer); var alloc = allocator.allocator(); const abs_path = fs.path.resolve(alloc, &.{ wasi_cwd.cwd, path }) catch return error.NameTooLong; const preopen_uri = wasi_cwd.preopens.?.findContaining(.{ .Dir = abs_path }); if (preopen_uri) |po| { return RelativePathWasi{ .dir_fd = po.base.fd, .relative_path = po.relative_path, }; } else { // No matching preopen found return error.AccessDenied; } } /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` is relative to the open directory handle `dir_fd`. /// See also `openatZ`. pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t { if (builtin.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return openatW(dir_fd, file_path_w.span(), flags, mode); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { // `mode` is ignored on WASI, which does not support unix-style file permissions const fd = if (dir_fd == wasi.AT.FDCWD or fs.path.isAbsolute(file_path)) blk: { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; const path = try resolvePathWasi(file_path, &path_buf); const opts = try openOptionsFromFlagsWasi(path.dir_fd, flags); break :blk try openatWasi(path.dir_fd, path.relative_path, opts.lookup_flags, opts.oflags, opts.fs_flags, opts.fs_rights_base, opts.fs_rights_inheriting); } else blk: { const opts = try openOptionsFromFlagsWasi(dir_fd, flags); break :blk try openatWasi(dir_fd, file_path, opts.lookup_flags, opts.oflags, opts.fs_flags, opts.fs_rights_base, opts.fs_rights_inheriting); }; errdefer close(fd); const info = try fstat(fd); if (flags & O.WRONLY != 0 and info.filetype == .DIRECTORY) return error.IsDir; return fd; } const file_path_c = try toPosixPath(file_path); return openatZ(dir_fd, &file_path_c, flags, mode); } /// A struct to contain all lookup/rights flags accepted by `wasi.path_open` const WasiOpenOptions = struct { oflags: wasi.oflags_t, lookup_flags: wasi.lookupflags_t, fs_rights_base: wasi.rights_t, fs_rights_inheriting: wasi.rights_t, fs_flags: wasi.fdflags_t, }; /// Compute rights + flags corresponding to the provided POSIX access mode. fn openOptionsFromFlagsWasi(fd: fd_t, oflag: u32) OpenError!WasiOpenOptions { const w = std.os.wasi; // First, discover the rights that we can derive from `fd` var fsb_cur: wasi.fdstat_t = undefined; _ = switch (w.fd_fdstat_get(fd, &fsb_cur)) { .SUCCESS => .{}, .BADF => return error.InvalidHandle, else => |err| return unexpectedErrno(err), }; // Next, calculate the read/write rights to request, depending on the // provided POSIX access mode var rights: w.rights_t = 0; if (oflag & O.RDONLY != 0) { rights |= w.RIGHT.FD_READ | w.RIGHT.FD_READDIR; } if (oflag & O.WRONLY != 0) { rights |= w.RIGHT.FD_DATASYNC | w.RIGHT.FD_WRITE | w.RIGHT.FD_ALLOCATE | w.RIGHT.FD_FILESTAT_SET_SIZE; } // Request all other rights unconditionally rights |= ~(w.RIGHT.FD_DATASYNC | w.RIGHT.FD_READ | w.RIGHT.FD_WRITE | w.RIGHT.FD_ALLOCATE | w.RIGHT.FD_READDIR | w.RIGHT.FD_FILESTAT_SET_SIZE); // But only take rights that we can actually inherit rights &= fsb_cur.fs_rights_inheriting; return WasiOpenOptions{ .oflags = @truncate(w.oflags_t, (oflag >> 12)) & 0xfff, .lookup_flags = if (oflag & O.NOFOLLOW == 0) w.LOOKUP_SYMLINK_FOLLOW else 0, .fs_rights_base = rights, .fs_rights_inheriting = fsb_cur.fs_rights_inheriting, .fs_flags = @truncate(w.fdflags_t, oflag & 0xfff), }; } /// Open and possibly create a file in WASI. pub fn openatWasi(dir_fd: fd_t, file_path: []const u8, lookup_flags: lookupflags_t, oflags: oflags_t, fdflags: fdflags_t, base: rights_t, inheriting: rights_t) OpenError!fd_t { while (true) { var fd: fd_t = undefined; switch (wasi.path_open(dir_fd, lookup_flags, file_path.ptr, file_path.len, oflags, base, inheriting, fdflags, &fd)) { .SUCCESS => return fd, .INTR => continue, .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .FBIG => return error.FileTooBig, .OVERFLOW => return error.FileTooBig, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .MFILE => return error.ProcessFdQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NFILE => return error.SystemFdQuotaExceeded, .NODEV => return error.NoDevice, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .PERM => return error.AccessDenied, .EXIST => return error.PathAlreadyExists, .BUSY => return error.DeviceBusy, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } } /// Open and possibly create a file. Keeps trying if it gets interrupted. /// `file_path` is relative to the open directory handle `dir_fd`. /// See also `openat`. pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: u32, mode: mode_t) OpenError!fd_t { if (builtin.os.tag == .windows) { const file_path_w = try windows.cStrToPrefixedFileW(file_path); return openatW(dir_fd, file_path_w.span(), flags, mode); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return openat(dir_fd, mem.sliceTo(file_path, 0), flags, mode); } const openat_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.openat64 else system.openat; while (true) { const rc = openat_sym(dir_fd, file_path, flags, mode); switch (errno(rc)) { .SUCCESS => return @intCast(fd_t, rc), .INTR => continue, .FAULT => unreachable, .INVAL => unreachable, .BADF => unreachable, .ACCES => return error.AccessDenied, .FBIG => return error.FileTooBig, .OVERFLOW => return error.FileTooBig, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .MFILE => return error.ProcessFdQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NFILE => return error.SystemFdQuotaExceeded, .NODEV => return error.NoDevice, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .PERM => return error.AccessDenied, .EXIST => return error.PathAlreadyExists, .BUSY => return error.DeviceBusy, .OPNOTSUPP => return error.FileLocksNotSupported, .AGAIN => return error.WouldBlock, .TXTBSY => return error.FileBusy, else => |err| return unexpectedErrno(err), } } } /// Windows-only. Similar to `openat` but with pathname argument null-terminated /// WTF16 encoded. /// TODO currently, this function does not handle all flag combinations /// or makes use of perm argument. pub fn openatW(dir_fd: fd_t, file_path_w: []const u16, flags: u32, mode: mode_t) OpenError!fd_t { _ = mode; var options = openOptionsFromFlagsWindows(flags); options.dir = dir_fd; return windows.OpenFile(file_path_w, options) catch |err| switch (err) { error.WouldBlock => unreachable, error.PipeBusy => unreachable, else => |e| return e, }; } pub fn dup(old_fd: fd_t) !fd_t { const rc = system.dup(old_fd); return switch (errno(rc)) { .SUCCESS => return @intCast(fd_t, rc), .MFILE => error.ProcessFdQuotaExceeded, .BADF => unreachable, // invalid file descriptor else => |err| return unexpectedErrno(err), }; } pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { while (true) { switch (errno(system.dup2(old_fd, new_fd))) { .SUCCESS => return, .BUSY, .INTR => continue, .MFILE => return error.ProcessFdQuotaExceeded, .INVAL => unreachable, // invalid parameters passed to dup2 .BADF => unreachable, // invalid file descriptor else => |err| return unexpectedErrno(err), } } } pub const ExecveError = error{ SystemResources, AccessDenied, InvalidExe, FileSystem, IsDir, FileNotFound, NotDir, FileBusy, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, NameTooLong, } || UnexpectedError; /// Like `execve` except the parameters are null-terminated, /// matching the syscall API on all targets. This removes the need for an allocator. /// This function ignores PATH environment variable. See `execvpeZ` for that. pub fn execveZ( path: [*:0]const u8, child_argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8, ) ExecveError { switch (errno(system.execve(path, child_argv, envp))) { .SUCCESS => unreachable, .FAULT => unreachable, .@"2BIG" => return error.SystemResources, .MFILE => return error.ProcessFdQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NFILE => return error.SystemFdQuotaExceeded, .NOMEM => return error.SystemResources, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .INVAL => return error.InvalidExe, .NOEXEC => return error.InvalidExe, .IO => return error.FileSystem, .LOOP => return error.FileSystem, .ISDIR => return error.IsDir, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .TXTBSY => return error.FileBusy, else => |err| switch (builtin.os.tag) { .macos, .ios, .tvos, .watchos => switch (err) { .BADEXEC => return error.InvalidExe, .BADARCH => return error.InvalidExe, else => return unexpectedErrno(err), }, .linux, .solaris => switch (err) { .LIBBAD => return error.InvalidExe, else => return unexpectedErrno(err), }, else => return unexpectedErrno(err), }, } } pub const Arg0Expand = enum { expand, no_expand, }; /// Like `execvpeZ` except if `arg0_expand` is `.expand`, then `argv` is mutable, /// and `argv[0]` is expanded to be the same absolute path that is passed to the execve syscall. /// If this function returns with an error, `argv[0]` will be restored to the value it was when it was passed in. pub fn execvpeZ_expandArg0( comptime arg0_expand: Arg0Expand, file: [*:0]const u8, child_argv: switch (arg0_expand) { .expand => [*:null]?[*:0]const u8, .no_expand => [*:null]const ?[*:0]const u8, }, envp: [*:null]const ?[*:0]const u8, ) ExecveError { const file_slice = mem.sliceTo(file, 0); if (mem.indexOfScalar(u8, file_slice, '/') != null) return execveZ(file, child_argv, envp); const PATH = getenvZ("PATH") orelse "/usr/local/bin:/bin/:/usr/bin"; // Use of MAX_PATH_BYTES here is valid as the path_buf will be passed // directly to the operating system in execveZ. var path_buf: [MAX_PATH_BYTES]u8 = undefined; var it = mem.tokenize(u8, PATH, ":"); var seen_eacces = false; var err: ExecveError = undefined; // In case of expanding arg0 we must put it back if we return with an error. const prev_arg0 = child_argv[0]; defer switch (arg0_expand) { .expand => child_argv[0] = prev_arg0, .no_expand => {}, }; while (it.next()) |search_path| { if (path_buf.len < search_path.len + file_slice.len + 1) return error.NameTooLong; mem.copy(u8, &path_buf, search_path); path_buf[search_path.len] = '/'; mem.copy(u8, path_buf[search_path.len + 1 ..], file_slice); const path_len = search_path.len + file_slice.len + 1; path_buf[path_len] = 0; const full_path = path_buf[0..path_len :0].ptr; switch (arg0_expand) { .expand => child_argv[0] = full_path, .no_expand => {}, } err = execveZ(full_path, child_argv, envp); switch (err) { error.AccessDenied => seen_eacces = true, error.FileNotFound, error.NotDir => {}, else => |e| return e, } } if (seen_eacces) return error.AccessDenied; return err; } /// Like `execvpe` except the parameters are null-terminated, /// matching the syscall API on all targets. This removes the need for an allocator. /// This function also uses the PATH environment variable to get the full path to the executable. /// If `file` is an absolute path, this is the same as `execveZ`. pub fn execvpeZ( file: [*:0]const u8, argv_ptr: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8, ) ExecveError { return execvpeZ_expandArg0(.no_expand, file, argv_ptr, envp); } /// Get an environment variable. /// See also `getenvZ`. pub fn getenv(key: []const u8) ?[]const u8 { if (builtin.link_libc) { var small_key_buf: [64]u8 = undefined; if (key.len < small_key_buf.len) { mem.copy(u8, &small_key_buf, key); small_key_buf[key.len] = 0; const key0 = small_key_buf[0..key.len :0]; return getenvZ(key0); } // Search the entire `environ` because we don't have a null terminated pointer. var ptr = std.c.environ; while (ptr.*) |line| : (ptr += 1) { var line_i: usize = 0; while (line[line_i] != 0 and line[line_i] != '=') : (line_i += 1) {} const this_key = line[0..line_i]; if (!mem.eql(u8, this_key, key)) continue; var end_i: usize = line_i; while (line[end_i] != 0) : (end_i += 1) {} const value = line[line_i + 1 .. end_i]; return value; } return null; } if (builtin.os.tag == .windows) { @compileError("std.os.getenv is unavailable for Windows because environment string is in WTF-16 format. See std.process.getEnvVarOwned for cross-platform API or std.os.getenvW for Windows-specific API."); } // TODO see https://github.com/ziglang/zig/issues/4524 for (environ) |ptr| { var line_i: usize = 0; while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const this_key = ptr[0..line_i]; if (!mem.eql(u8, key, this_key)) continue; var end_i: usize = line_i; while (ptr[end_i] != 0) : (end_i += 1) {} const this_value = ptr[line_i + 1 .. end_i]; return this_value; } return null; } /// Get an environment variable with a null-terminated name. /// See also `getenv`. pub fn getenvZ(key: [*:0]const u8) ?[]const u8 { if (builtin.link_libc) { const value = system.getenv(key) orelse return null; return mem.sliceTo(value, 0); } if (builtin.os.tag == .windows) { @compileError("std.os.getenvZ is unavailable for Windows because environment string is in WTF-16 format. See std.process.getEnvVarOwned for cross-platform API or std.os.getenvW for Windows-specific API."); } return getenv(mem.sliceTo(key, 0)); } /// Windows-only. Get an environment variable with a null-terminated, WTF-16 encoded name. /// See also `getenv`. /// This function performs a Unicode-aware case-insensitive lookup using RtlEqualUnicodeString. pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 { if (builtin.os.tag != .windows) { @compileError("std.os.getenvW is a Windows-only API"); } const key_slice = mem.sliceTo(key, 0); const ptr = windows.peb().ProcessParameters.Environment; var i: usize = 0; while (ptr[i] != 0) { const key_start = i; // There are some special environment variables that start with =, // so we need a special case to not treat = as a key/value separator // if it's the first character. // https://devblogs.microsoft.com/oldnewthing/20100506-00/?p=14133 if (ptr[key_start] == '=') i += 1; while (ptr[i] != 0 and ptr[i] != '=') : (i += 1) {} const this_key = ptr[key_start..i]; if (ptr[i] == '=') i += 1; const value_start = i; while (ptr[i] != 0) : (i += 1) {} const this_value = ptr[value_start..i :0]; const key_string_bytes = @intCast(u16, key_slice.len * 2); const key_string = windows.UNICODE_STRING{ .Length = key_string_bytes, .MaximumLength = key_string_bytes, .Buffer = @intToPtr([*]u16, @ptrToInt(key)), }; const this_key_string_bytes = @intCast(u16, this_key.len * 2); const this_key_string = windows.UNICODE_STRING{ .Length = this_key_string_bytes, .MaximumLength = this_key_string_bytes, .Buffer = this_key.ptr, }; if (windows.ntdll.RtlEqualUnicodeString(&key_string, &this_key_string, windows.TRUE) == windows.TRUE) { return this_value; } i += 1; // skip over null byte } return null; } pub const GetCwdError = error{ NameTooLong, CurrentWorkingDirectoryUnlinked, } || UnexpectedError; /// The result is a slice of out_buffer, indexed from 0. pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { if (builtin.os.tag == .windows) { return windows.GetCurrentDirectory(out_buffer); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { const path = wasi_cwd.cwd; if (out_buffer.len < path.len) return error.NameTooLong; std.mem.copy(u8, out_buffer, path); return out_buffer[0..path.len]; } const err = if (builtin.link_libc) blk: { const c_err = if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; break :blk @intToEnum(E, c_err); } else blk: { break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); }; switch (err) { .SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0), .FAULT => unreachable, .INVAL => unreachable, .NOENT => return error.CurrentWorkingDirectoryUnlinked, .RANGE => return error.NameTooLong, else => return unexpectedErrno(err), } } pub const SymLinkError = error{ /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to create a new symbolic link relative to it. AccessDenied, DiskQuota, PathAlreadyExists, FileSystem, SymLinkLoop, FileNotFound, SystemResources, NoSpaceLeft, ReadOnlyFileSystem, NotDir, NameTooLong, InvalidUtf8, BadPathName, } || UnexpectedError; /// Creates a symbolic link named `sym_link_path` which contains the string `target_path`. /// A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent /// one; the latter case is known as a dangling link. /// If `sym_link_path` exists, it will not be overwritten. /// See also `symlinkZ. pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void { if (builtin.os.tag == .windows) { @compileError("symlink is not supported on Windows; use std.os.windows.CreateSymbolicLink instead"); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return symlinkat(target_path, wasi.AT.FDCWD, sym_link_path); } const target_path_c = try toPosixPath(target_path); const sym_link_path_c = try toPosixPath(sym_link_path); return symlinkZ(&target_path_c, &sym_link_path_c); } /// This is the same as `symlink` except the parameters are null-terminated pointers. /// See also `symlink`. pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLinkError!void { if (builtin.os.tag == .windows) { @compileError("symlink is not supported on Windows; use std.os.windows.CreateSymbolicLink instead"); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return symlink(mem.sliceTo(target_path, 0), mem.sliceTo(sym_link_path, 0)); } switch (errno(system.symlink(target_path, sym_link_path))) { .SUCCESS => return, .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } /// Similar to `symlink`, however, creates a symbolic link named `sym_link_path` which contains the string /// `target_path` **relative** to `newdirfd` directory handle. /// A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent /// one; the latter case is known as a dangling link. /// If `sym_link_path` exists, it will not be overwritten. /// See also `symlinkatWasi`, `symlinkatZ` and `symlinkatW`. pub fn symlinkat(target_path: []const u8, newdirfd: fd_t, sym_link_path: []const u8) SymLinkError!void { if (builtin.os.tag == .windows) { @compileError("symlinkat is not supported on Windows; use std.os.windows.CreateSymbolicLink instead"); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { if (newdirfd == wasi.AT.FDCWD or fs.path.isAbsolute(target_path)) { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; const path = try resolvePathWasi(sym_link_path, &path_buf); return symlinkatWasi(target_path, path.dir_fd, path.relative_path); } return symlinkatWasi(target_path, newdirfd, sym_link_path); } const target_path_c = try toPosixPath(target_path); const sym_link_path_c = try toPosixPath(sym_link_path); return symlinkatZ(&target_path_c, newdirfd, &sym_link_path_c); } /// WASI-only. The same as `symlinkat` but targeting WASI. /// See also `symlinkat`. pub fn symlinkatWasi(target_path: []const u8, newdirfd: fd_t, sym_link_path: []const u8) SymLinkError!void { switch (wasi.path_symlink(target_path.ptr, target_path.len, newdirfd, sym_link_path.ptr, sym_link_path.len)) { .SUCCESS => {}, .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .ROFS => return error.ReadOnlyFileSystem, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } /// The same as `symlinkat` except the parameters are null-terminated pointers. /// See also `symlinkat`. pub fn symlinkatZ(target_path: [*:0]const u8, newdirfd: fd_t, sym_link_path: [*:0]const u8) SymLinkError!void { if (builtin.os.tag == .windows) { @compileError("symlinkat is not supported on Windows; use std.os.windows.CreateSymbolicLink instead"); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return symlinkat(mem.sliceTo(target_path, 0), newdirfd, mem.sliceTo(sym_link_path, 0)); } switch (errno(system.symlinkat(target_path, newdirfd, sym_link_path))) { .SUCCESS => return, .FAULT => unreachable, .INVAL => unreachable, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub const LinkError = UnexpectedError || error{ AccessDenied, DiskQuota, PathAlreadyExists, FileSystem, SymLinkLoop, LinkQuotaExceeded, NameTooLong, FileNotFound, SystemResources, NoSpaceLeft, ReadOnlyFileSystem, NotSameFileSystem, }; pub fn linkZ(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: i32) LinkError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return link(mem.sliceTo(oldpath, 0), mem.sliceTo(newpath, 0), flags); } switch (errno(system.link(oldpath, newpath, flags))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .FAULT => unreachable, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .PERM => return error.AccessDenied, .ROFS => return error.ReadOnlyFileSystem, .XDEV => return error.NotSameFileSystem, .INVAL => unreachable, else => |err| return unexpectedErrno(err), } } pub fn link(oldpath: []const u8, newpath: []const u8, flags: i32) LinkError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return linkat(wasi.AT.FDCWD, oldpath, wasi.AT.FDCWD, newpath, flags) catch |err| switch (err) { error.NotDir => unreachable, // link() does not support directories else => |e| return e, }; } const old = try toPosixPath(oldpath); const new = try toPosixPath(newpath); return try linkZ(&old, &new, flags); } pub const LinkatError = LinkError || error{NotDir}; pub fn linkatZ( olddir: fd_t, oldpath: [*:0]const u8, newdir: fd_t, newpath: [*:0]const u8, flags: i32, ) LinkatError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return linkat(olddir, mem.sliceTo(oldpath, 0), newdir, mem.sliceTo(newpath, 0), flags); } switch (errno(system.linkat(olddir, oldpath, newdir, newpath, flags))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .FAULT => unreachable, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .PERM => return error.AccessDenied, .ROFS => return error.ReadOnlyFileSystem, .XDEV => return error.NotSameFileSystem, .INVAL => unreachable, else => |err| return unexpectedErrno(err), } } pub fn linkat( olddir: fd_t, oldpath: []const u8, newdir: fd_t, newpath: []const u8, flags: i32, ) LinkatError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { var resolve_olddir: bool = (olddir == wasi.AT.FDCWD or fs.path.isAbsolute(oldpath)); var resolve_newdir: bool = (newdir == wasi.AT.FDCWD or fs.path.isAbsolute(newpath)); var old: RelativePathWasi = .{ .dir_fd = olddir, .relative_path = oldpath }; var new: RelativePathWasi = .{ .dir_fd = newdir, .relative_path = newpath }; // Resolve absolute or CWD-relative paths to a path within a Preopen if (resolve_olddir or resolve_newdir) { var buf_old: [MAX_PATH_BYTES]u8 = undefined; var buf_new: [MAX_PATH_BYTES]u8 = undefined; if (resolve_olddir) old = try resolvePathWasi(oldpath, &buf_old); if (resolve_newdir) new = try resolvePathWasi(newpath, &buf_new); return linkatWasi(old, new, flags); } return linkatWasi(old, new, flags); } const old = try toPosixPath(oldpath); const new = try toPosixPath(newpath); return try linkatZ(olddir, &old, newdir, &new, flags); } /// WASI-only. The same as `linkat` but targeting WASI. /// See also `linkat`. pub fn linkatWasi(old: RelativePathWasi, new: RelativePathWasi, flags: i32) LinkatError!void { var old_flags: wasi.lookupflags_t = 0; // TODO: Why is this not defined in wasi-libc? if (flags & linux.AT.SYMLINK_FOLLOW != 0) old_flags |= wasi.LOOKUP_SYMLINK_FOLLOW; switch (wasi.path_link(old.dir_fd, old_flags, old.relative_path.ptr, old.relative_path.len, new.dir_fd, new.relative_path.ptr, new.relative_path.len)) { .SUCCESS => return, .ACCES => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .FAULT => unreachable, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .PERM => return error.AccessDenied, .ROFS => return error.ReadOnlyFileSystem, .XDEV => return error.NotSameFileSystem, .INVAL => unreachable, else => |err| return unexpectedErrno(err), } } pub const UnlinkError = error{ FileNotFound, /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to unlink a resource by path relative to it. AccessDenied, FileBusy, FileSystem, IsDir, SymLinkLoop, NameTooLong, NotDir, SystemResources, ReadOnlyFileSystem, /// On Windows, file paths must be valid Unicode. InvalidUtf8, /// On Windows, file paths cannot contain these characters: /// '/', '*', '?', '"', '<', '>', '|' BadPathName, } || UnexpectedError; /// Delete a name and possibly the file it refers to. /// See also `unlinkZ`. pub fn unlink(file_path: []const u8) UnlinkError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return unlinkat(wasi.AT.FDCWD, file_path, 0) catch |err| switch (err) { error.DirNotEmpty => unreachable, // only occurs when targeting directories else => |e| return e, }; } else if (builtin.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return unlinkW(file_path_w.span()); } else { const file_path_c = try toPosixPath(file_path); return unlinkZ(&file_path_c); } } /// Same as `unlink` except the parameter is a null terminated UTF8-encoded string. pub fn unlinkZ(file_path: [*:0]const u8) UnlinkError!void { if (builtin.os.tag == .windows) { const file_path_w = try windows.cStrToPrefixedFileW(file_path); return unlinkW(file_path_w.span()); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return unlink(mem.sliceTo(file_path, 0)); } switch (errno(system.unlink(file_path))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .FAULT => unreachable, .INVAL => unreachable, .IO => return error.FileSystem, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } /// Windows-only. Same as `unlink` except the parameter is null-terminated, WTF16 encoded. pub fn unlinkW(file_path_w: []const u16) UnlinkError!void { return windows.DeleteFile(file_path_w, .{ .dir = std.fs.cwd().fd }); } pub const UnlinkatError = UnlinkError || error{ /// When passing `AT.REMOVEDIR`, this error occurs when the named directory is not empty. DirNotEmpty, }; /// Delete a file name and possibly the file it refers to, based on an open directory handle. /// Asserts that the path parameter has no null bytes. pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void { if (builtin.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return unlinkatW(dirfd, file_path_w.span(), flags); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { if (dirfd == wasi.AT.FDCWD or fs.path.isAbsolute(file_path)) { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; const path = try resolvePathWasi(file_path, &path_buf); return unlinkatWasi(path.dir_fd, path.relative_path, flags); } return unlinkatWasi(dirfd, file_path, flags); } else { const file_path_c = try toPosixPath(file_path); return unlinkatZ(dirfd, &file_path_c, flags); } } /// WASI-only. Same as `unlinkat` but targeting WASI. /// See also `unlinkat`. pub fn unlinkatWasi(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void { const remove_dir = (flags & AT.REMOVEDIR) != 0; const res = if (remove_dir) wasi.path_remove_directory(dirfd, file_path.ptr, file_path.len) else wasi.path_unlink_file(dirfd, file_path.ptr, file_path.len); switch (res) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .FAULT => unreachable, .IO => return error.FileSystem, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .ROFS => return error.ReadOnlyFileSystem, .NOTEMPTY => return error.DirNotEmpty, .NOTCAPABLE => return error.AccessDenied, .INVAL => unreachable, // invalid flags, or pathname has . as last component .BADF => unreachable, // always a race condition else => |err| return unexpectedErrno(err), } } /// Same as `unlinkat` but `file_path` is a null-terminated string. pub fn unlinkatZ(dirfd: fd_t, file_path_c: [*:0]const u8, flags: u32) UnlinkatError!void { if (builtin.os.tag == .windows) { const file_path_w = try windows.cStrToPrefixedFileW(file_path_c); return unlinkatW(dirfd, file_path_w.span(), flags); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return unlinkat(dirfd, mem.sliceTo(file_path_c, 0), flags); } switch (errno(system.unlinkat(dirfd, file_path_c, flags))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .FAULT => unreachable, .IO => return error.FileSystem, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .ROFS => return error.ReadOnlyFileSystem, .EXIST => return error.DirNotEmpty, .NOTEMPTY => return error.DirNotEmpty, .INVAL => unreachable, // invalid flags, or pathname has . as last component .BADF => unreachable, // always a race condition else => |err| return unexpectedErrno(err), } } /// Same as `unlinkat` but `sub_path_w` is UTF16LE, NT prefixed. Windows only. pub fn unlinkatW(dirfd: fd_t, sub_path_w: []const u16, flags: u32) UnlinkatError!void { const remove_dir = (flags & AT.REMOVEDIR) != 0; return windows.DeleteFile(sub_path_w, .{ .dir = dirfd, .remove_dir = remove_dir }); } pub const RenameError = error{ /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to rename a resource by path relative to it. AccessDenied, FileBusy, DiskQuota, IsDir, SymLinkLoop, LinkQuotaExceeded, NameTooLong, FileNotFound, NotDir, SystemResources, NoSpaceLeft, PathAlreadyExists, ReadOnlyFileSystem, RenameAcrossMountPoints, InvalidUtf8, BadPathName, NoDevice, SharingViolation, PipeBusy, } || UnexpectedError; /// Change the name or location of a file. pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return renameat(wasi.AT.FDCWD, old_path, wasi.AT.FDCWD, new_path); } else if (builtin.os.tag == .windows) { const old_path_w = try windows.sliceToPrefixedFileW(old_path); const new_path_w = try windows.sliceToPrefixedFileW(new_path); return renameW(old_path_w.span().ptr, new_path_w.span().ptr); } else { const old_path_c = try toPosixPath(old_path); const new_path_c = try toPosixPath(new_path); return renameZ(&old_path_c, &new_path_c); } } /// Same as `rename` except the parameters are null-terminated byte arrays. pub fn renameZ(old_path: [*:0]const u8, new_path: [*:0]const u8) RenameError!void { if (builtin.os.tag == .windows) { const old_path_w = try windows.cStrToPrefixedFileW(old_path); const new_path_w = try windows.cStrToPrefixedFileW(new_path); return renameW(old_path_w.span().ptr, new_path_w.span().ptr); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return rename(mem.sliceTo(old_path, 0), mem.sliceTo(new_path, 0)); } switch (errno(system.rename(old_path, new_path))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .DQUOT => return error.DiskQuota, .FAULT => unreachable, .INVAL => unreachable, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .EXIST => return error.PathAlreadyExists, .NOTEMPTY => return error.PathAlreadyExists, .ROFS => return error.ReadOnlyFileSystem, .XDEV => return error.RenameAcrossMountPoints, else => |err| return unexpectedErrno(err), } } /// Same as `rename` except the parameters are null-terminated UTF16LE encoded byte arrays. /// Assumes target is Windows. pub fn renameW(old_path: [*:0]const u16, new_path: [*:0]const u16) RenameError!void { const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH; return windows.MoveFileExW(old_path, new_path, flags); } /// Change the name or location of a file based on an open directory handle. pub fn renameat( old_dir_fd: fd_t, old_path: []const u8, new_dir_fd: fd_t, new_path: []const u8, ) RenameError!void { if (builtin.os.tag == .windows) { const old_path_w = try windows.sliceToPrefixedFileW(old_path); const new_path_w = try windows.sliceToPrefixedFileW(new_path); return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { var resolve_old: bool = (old_dir_fd == wasi.AT.FDCWD or fs.path.isAbsolute(old_path)); var resolve_new: bool = (new_dir_fd == wasi.AT.FDCWD or fs.path.isAbsolute(new_path)); var old: RelativePathWasi = .{ .dir_fd = old_dir_fd, .relative_path = old_path }; var new: RelativePathWasi = .{ .dir_fd = new_dir_fd, .relative_path = new_path }; // Resolve absolute or CWD-relative paths to a path within a Preopen if (resolve_old or resolve_new) { var buf_old: [MAX_PATH_BYTES]u8 = undefined; var buf_new: [MAX_PATH_BYTES]u8 = undefined; if (resolve_old) old = try resolvePathWasi(old_path, &buf_old); if (resolve_new) new = try resolvePathWasi(new_path, &buf_new); return renameatWasi(old, new); } return renameatWasi(old, new); } else { const old_path_c = try toPosixPath(old_path); const new_path_c = try toPosixPath(new_path); return renameatZ(old_dir_fd, &old_path_c, new_dir_fd, &new_path_c); } } /// WASI-only. Same as `renameat` expect targeting WASI. /// See also `renameat`. pub fn renameatWasi(old: RelativePathWasi, new: RelativePathWasi) RenameError!void { switch (wasi.path_rename(old.dir_fd, old.relative_path.ptr, old.relative_path.len, new.dir_fd, new.relative_path.ptr, new.relative_path.len)) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .DQUOT => return error.DiskQuota, .FAULT => unreachable, .INVAL => unreachable, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .EXIST => return error.PathAlreadyExists, .NOTEMPTY => return error.PathAlreadyExists, .ROFS => return error.ReadOnlyFileSystem, .XDEV => return error.RenameAcrossMountPoints, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } /// Same as `renameat` except the parameters are null-terminated byte arrays. pub fn renameatZ( old_dir_fd: fd_t, old_path: [*:0]const u8, new_dir_fd: fd_t, new_path: [*:0]const u8, ) RenameError!void { if (builtin.os.tag == .windows) { const old_path_w = try windows.cStrToPrefixedFileW(old_path); const new_path_w = try windows.cStrToPrefixedFileW(new_path); return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return renameat(old_dir_fd, mem.sliceTo(old_path, 0), new_dir_fd, mem.sliceTo(new_path, 0)); } switch (errno(system.renameat(old_dir_fd, old_path, new_dir_fd, new_path))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .DQUOT => return error.DiskQuota, .FAULT => unreachable, .INVAL => unreachable, .ISDIR => return error.IsDir, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .EXIST => return error.PathAlreadyExists, .NOTEMPTY => return error.PathAlreadyExists, .ROFS => return error.ReadOnlyFileSystem, .XDEV => return error.RenameAcrossMountPoints, else => |err| return unexpectedErrno(err), } } /// Same as `renameat` but Windows-only and the path parameters are /// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded. pub fn renameatW( old_dir_fd: fd_t, old_path_w: []const u16, new_dir_fd: fd_t, new_path_w: []const u16, ReplaceIfExists: windows.BOOLEAN, ) RenameError!void { const src_fd = windows.OpenFile(old_path_w, .{ .dir = old_dir_fd, .access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE, .creation = windows.FILE_OPEN, .io_mode = .blocking, .filter = .any, // This function is supposed to rename both files and directories. }) catch |err| switch (err) { error.WouldBlock => unreachable, // Not possible without `.share_access_nonblocking = true`. else => |e| return e, }; defer windows.CloseHandle(src_fd); const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (MAX_PATH_BYTES - 1); var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION)) = undefined; const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2; if (struct_len > struct_buf_len) return error.NameTooLong; const rename_info = @ptrCast(*windows.FILE_RENAME_INFORMATION, &rename_info_buf); rename_info.* = .{ .ReplaceIfExists = ReplaceIfExists, .RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd, .FileNameLength = @intCast(u32, new_path_w.len * 2), // already checked error.NameTooLong .FileName = undefined, }; std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); var io_status_block: windows.IO_STATUS_BLOCK = undefined; const rc = windows.ntdll.NtSetInformationFile( src_fd, &io_status_block, rename_info, @intCast(u32, struct_len), // already checked for error.NameTooLong .FileRenameInformation, ); switch (rc) { .SUCCESS => return, .INVALID_HANDLE => unreachable, .INVALID_PARAMETER => unreachable, .OBJECT_PATH_SYNTAX_BAD => unreachable, .ACCESS_DENIED => return error.AccessDenied, .OBJECT_NAME_NOT_FOUND => return error.FileNotFound, .OBJECT_PATH_NOT_FOUND => return error.FileNotFound, .NOT_SAME_DEVICE => return error.RenameAcrossMountPoints, else => return windows.unexpectedStatus(rc), } } pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!void { if (builtin.os.tag == .windows) { const sub_dir_path_w = try windows.sliceToPrefixedFileW(sub_dir_path); return mkdiratW(dir_fd, sub_dir_path_w.span(), mode); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { if (dir_fd == wasi.AT.FDCWD or fs.path.isAbsolute(sub_dir_path)) { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; const path = try resolvePathWasi(sub_dir_path, &path_buf); return mkdiratWasi(path.dir_fd, path.relative_path, mode); } return mkdiratWasi(dir_fd, sub_dir_path, mode); } else { const sub_dir_path_c = try toPosixPath(sub_dir_path); return mkdiratZ(dir_fd, &sub_dir_path_c, mode); } } pub fn mkdiratWasi(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!void { _ = mode; switch (wasi.path_create_directory(dir_fd, sub_dir_path.ptr, sub_dir_path.len)) { .SUCCESS => return, .ACCES => return error.AccessDenied, .BADF => unreachable, .PERM => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .FAULT => unreachable, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .ROFS => return error.ReadOnlyFileSystem, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: u32) MakeDirError!void { if (builtin.os.tag == .windows) { const sub_dir_path_w = try windows.cStrToPrefixedFileW(sub_dir_path); return mkdiratW(dir_fd, sub_dir_path_w.span().ptr, mode); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return mkdirat(dir_fd, mem.sliceTo(sub_dir_path, 0), mode); } switch (errno(system.mkdirat(dir_fd, sub_dir_path, mode))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .BADF => unreachable, .PERM => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .FAULT => unreachable, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub fn mkdiratW(dir_fd: fd_t, sub_path_w: []const u16, mode: u32) MakeDirError!void { _ = mode; const sub_dir_handle = windows.OpenFile(sub_path_w, .{ .dir = dir_fd, .access_mask = windows.GENERIC_READ | windows.SYNCHRONIZE, .creation = windows.FILE_CREATE, .io_mode = .blocking, .filter = .dir_only, }) catch |err| switch (err) { error.IsDir => unreachable, error.PipeBusy => unreachable, error.WouldBlock => unreachable, else => |e| return e, }; windows.CloseHandle(sub_dir_handle); } pub const MakeDirError = error{ /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to create a new directory relative to it. AccessDenied, DiskQuota, PathAlreadyExists, SymLinkLoop, LinkQuotaExceeded, NameTooLong, FileNotFound, SystemResources, NoSpaceLeft, NotDir, ReadOnlyFileSystem, InvalidUtf8, BadPathName, NoDevice, } || UnexpectedError; /// Create a directory. /// `mode` is ignored on Windows and WASI. pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return mkdirat(wasi.AT.FDCWD, dir_path, mode); } else if (builtin.os.tag == .windows) { const dir_path_w = try windows.sliceToPrefixedFileW(dir_path); return mkdirW(dir_path_w.span(), mode); } else { const dir_path_c = try toPosixPath(dir_path); return mkdirZ(&dir_path_c, mode); } } /// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string. pub fn mkdirZ(dir_path: [*:0]const u8, mode: u32) MakeDirError!void { if (builtin.os.tag == .windows) { const dir_path_w = try windows.cStrToPrefixedFileW(dir_path); return mkdirW(dir_path_w.span(), mode); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return mkdir(mem.sliceTo(dir_path, 0), mode); } switch (errno(system.mkdir(dir_path, mode))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .DQUOT => return error.DiskQuota, .EXIST => return error.PathAlreadyExists, .FAULT => unreachable, .LOOP => return error.SymLinkLoop, .MLINK => return error.LinkQuotaExceeded, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, .NOTDIR => return error.NotDir, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } /// Windows-only. Same as `mkdir` but the parameters is WTF16 encoded. pub fn mkdirW(dir_path_w: []const u16, mode: u32) MakeDirError!void { _ = mode; const sub_dir_handle = windows.OpenFile(dir_path_w, .{ .dir = std.fs.cwd().fd, .access_mask = windows.GENERIC_READ | windows.SYNCHRONIZE, .creation = windows.FILE_CREATE, .io_mode = .blocking, .filter = .dir_only, }) catch |err| switch (err) { error.IsDir => unreachable, error.PipeBusy => unreachable, error.WouldBlock => unreachable, else => |e| return e, }; windows.CloseHandle(sub_dir_handle); } pub const DeleteDirError = error{ AccessDenied, FileBusy, SymLinkLoop, NameTooLong, FileNotFound, SystemResources, NotDir, DirNotEmpty, ReadOnlyFileSystem, InvalidUtf8, BadPathName, } || UnexpectedError; /// Deletes an empty directory. pub fn rmdir(dir_path: []const u8) DeleteDirError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { return unlinkat(wasi.AT.FDCWD, dir_path, AT.REMOVEDIR) catch |err| switch (err) { error.FileSystem => unreachable, // only occurs when targeting files error.IsDir => unreachable, // only occurs when targeting files else => |e| return e, }; } else if (builtin.os.tag == .windows) { const dir_path_w = try windows.sliceToPrefixedFileW(dir_path); return rmdirW(dir_path_w.span()); } else { const dir_path_c = try toPosixPath(dir_path); return rmdirZ(&dir_path_c); } } /// Same as `rmdir` except the parameter is null-terminated. pub fn rmdirZ(dir_path: [*:0]const u8) DeleteDirError!void { if (builtin.os.tag == .windows) { const dir_path_w = try windows.cStrToPrefixedFileW(dir_path); return rmdirW(dir_path_w.span()); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return rmdir(mem.sliceTo(dir_path, 0)); } switch (errno(system.rmdir(dir_path))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .BUSY => return error.FileBusy, .FAULT => unreachable, .INVAL => return error.BadPathName, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, .EXIST => return error.DirNotEmpty, .NOTEMPTY => return error.DirNotEmpty, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } /// Windows-only. Same as `rmdir` except the parameter is WTF16 encoded. pub fn rmdirW(dir_path_w: []const u16) DeleteDirError!void { return windows.DeleteFile(dir_path_w, .{ .dir = std.fs.cwd().fd, .remove_dir = true }) catch |err| switch (err) { error.IsDir => unreachable, else => |e| return e, }; } pub const ChangeCurDirError = error{ AccessDenied, FileSystem, SymLinkLoop, NameTooLong, FileNotFound, SystemResources, NotDir, BadPathName, /// On Windows, file paths must be valid Unicode. InvalidUtf8, } || UnexpectedError; /// Changes the current working directory of the calling process. /// `dir_path` is recommended to be a UTF-8 encoded string. pub fn chdir(dir_path: []const u8) ChangeCurDirError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { var buf: [MAX_PATH_BYTES]u8 = undefined; var alloc = std.heap.FixedBufferAllocator.init(&buf); const path = try fs.resolve(alloc.allocator(), &.{ wasi_cwd.cwd, dir_path }); const dirinfo = try fstatat(AT.FDCWD, path, 0); if (dirinfo.filetype != .DIRECTORY) { return error.NotDir; } var cwd_alloc = std.heap.FixedBufferAllocator.init(&wasi_cwd.path_buffer); wasi_cwd.cwd = try cwd_alloc.allocator().dupe(u8, path); return; } else if (builtin.os.tag == .windows) { var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined; const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path); if (len > utf16_dir_path.len) return error.NameTooLong; return chdirW(utf16_dir_path[0..len]); } else { const dir_path_c = try toPosixPath(dir_path); return chdirZ(&dir_path_c); } } /// Same as `chdir` except the parameter is null-terminated. pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void { if (builtin.os.tag == .windows) { var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined; const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path); if (len > utf16_dir_path.len) return error.NameTooLong; return chdirW(utf16_dir_path[0..len]); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return chdir(mem.sliceTo(dir_path, 0)); } switch (errno(system.chdir(dir_path))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .FAULT => unreachable, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, else => |err| return unexpectedErrno(err), } } /// Windows-only. Same as `chdir` except the paramter is WTF16 encoded. pub fn chdirW(dir_path: []const u16) ChangeCurDirError!void { windows.SetCurrentDirectory(dir_path) catch |err| switch (err) { error.NoDevice => return error.FileSystem, else => |e| return e, }; } pub const FchdirError = error{ AccessDenied, NotDir, FileSystem, } || UnexpectedError; pub fn fchdir(dirfd: fd_t) FchdirError!void { while (true) { switch (errno(system.fchdir(dirfd))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .BADF => unreachable, .NOTDIR => return error.NotDir, .INTR => continue, .IO => return error.FileSystem, else => |err| return unexpectedErrno(err), } } } pub const ReadLinkError = error{ /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to read value of a symbolic link relative to it. AccessDenied, FileSystem, SymLinkLoop, NameTooLong, FileNotFound, SystemResources, NotLink, NotDir, InvalidUtf8, BadPathName, /// Windows-only. This error may occur if the opened reparse point is /// of unsupported type. UnsupportedReparsePointType, } || UnexpectedError; /// Read value of a symbolic link. /// The return value is a slice of `out_buffer` from index 0. pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 { if (builtin.os.tag == .wasi and !builtin.link_libc) { return readlinkat(wasi.AT.FDCWD, file_path, out_buffer); } else if (builtin.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return readlinkW(file_path_w.span(), out_buffer); } else { const file_path_c = try toPosixPath(file_path); return readlinkZ(&file_path_c, out_buffer); } } /// Windows-only. Same as `readlink` except `file_path` is WTF16 encoded. /// See also `readlinkZ`. pub fn readlinkW(file_path: []const u16, out_buffer: []u8) ReadLinkError![]u8 { return windows.ReadLink(std.fs.cwd().fd, file_path, out_buffer); } /// Same as `readlink` except `file_path` is null-terminated. pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 { if (builtin.os.tag == .windows) { const file_path_w = try windows.cStrToWin32PrefixedFileW(file_path); return readlinkW(file_path_w.span(), out_buffer); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return readlink(mem.sliceTo(file_path, 0), out_buffer); } const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len); switch (errno(rc)) { .SUCCESS => return out_buffer[0..@bitCast(usize, rc)], .ACCES => return error.AccessDenied, .FAULT => unreachable, .INVAL => return error.NotLink, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, else => |err| return unexpectedErrno(err), } } /// Similar to `readlink` except reads value of a symbolink link **relative** to `dirfd` directory handle. /// The return value is a slice of `out_buffer` from index 0. /// See also `readlinkatWasi`, `realinkatZ` and `realinkatW`. pub fn readlinkat(dirfd: fd_t, file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 { if (builtin.os.tag == .wasi and !builtin.link_libc) { if (dirfd == wasi.AT.FDCWD or fs.path.isAbsolute(file_path)) { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; var path = try resolvePathWasi(file_path, &path_buf); return readlinkatWasi(path.dir_fd, path.relative_path, out_buffer); } return readlinkatWasi(dirfd, file_path, out_buffer); } if (builtin.os.tag == .windows) { const file_path_w = try windows.sliceToPrefixedFileW(file_path); return readlinkatW(dirfd, file_path_w.span(), out_buffer); } const file_path_c = try toPosixPath(file_path); return readlinkatZ(dirfd, &file_path_c, out_buffer); } /// WASI-only. Same as `readlinkat` but targets WASI. /// See also `readlinkat`. pub fn readlinkatWasi(dirfd: fd_t, file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 { var bufused: usize = undefined; switch (wasi.path_readlink(dirfd, file_path.ptr, file_path.len, out_buffer.ptr, out_buffer.len, &bufused)) { .SUCCESS => return out_buffer[0..bufused], .ACCES => return error.AccessDenied, .FAULT => unreachable, .INVAL => return error.NotLink, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } /// Windows-only. Same as `readlinkat` except `file_path` is null-terminated, WTF16 encoded. /// See also `readlinkat`. pub fn readlinkatW(dirfd: fd_t, file_path: []const u16, out_buffer: []u8) ReadLinkError![]u8 { return windows.ReadLink(dirfd, file_path, out_buffer); } /// Same as `readlinkat` except `file_path` is null-terminated. /// See also `readlinkat`. pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 { if (builtin.os.tag == .windows) { const file_path_w = try windows.cStrToPrefixedFileW(file_path); return readlinkatW(dirfd, file_path_w.span(), out_buffer); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return readlinkat(dirfd, mem.sliceTo(file_path, 0), out_buffer); } const rc = system.readlinkat(dirfd, file_path, out_buffer.ptr, out_buffer.len); switch (errno(rc)) { .SUCCESS => return out_buffer[0..@bitCast(usize, rc)], .ACCES => return error.AccessDenied, .FAULT => unreachable, .INVAL => return error.NotLink, .IO => return error.FileSystem, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, else => |err| return unexpectedErrno(err), } } pub const SetEidError = error{ InvalidUserId, PermissionDenied, } || UnexpectedError; pub const SetIdError = error{ResourceLimitReached} || SetEidError; pub fn setuid(uid: uid_t) SetIdError!void { switch (errno(system.setuid(uid))) { .SUCCESS => return, .AGAIN => return error.ResourceLimitReached, .INVAL => return error.InvalidUserId, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn seteuid(uid: uid_t) SetEidError!void { switch (errno(system.seteuid(uid))) { .SUCCESS => return, .INVAL => return error.InvalidUserId, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setreuid(ruid: uid_t, euid: uid_t) SetIdError!void { switch (errno(system.setreuid(ruid, euid))) { .SUCCESS => return, .AGAIN => return error.ResourceLimitReached, .INVAL => return error.InvalidUserId, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setgid(gid: gid_t) SetIdError!void { switch (errno(system.setgid(gid))) { .SUCCESS => return, .AGAIN => return error.ResourceLimitReached, .INVAL => return error.InvalidUserId, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setegid(uid: uid_t) SetEidError!void { switch (errno(system.setegid(uid))) { .SUCCESS => return, .INVAL => return error.InvalidUserId, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub fn setregid(rgid: gid_t, egid: gid_t) SetIdError!void { switch (errno(system.setregid(rgid, egid))) { .SUCCESS => return, .AGAIN => return error.ResourceLimitReached, .INVAL => return error.InvalidUserId, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } /// Test whether a file descriptor refers to a terminal. pub fn isatty(handle: fd_t) bool { if (builtin.os.tag == .windows) { if (isCygwinPty(handle)) return true; var out: windows.DWORD = undefined; return windows.kernel32.GetConsoleMode(handle, &out) != 0; } if (builtin.link_libc) { return system.isatty(handle) != 0; } if (builtin.os.tag == .wasi) { var statbuf: fdstat_t = undefined; const err = system.fd_fdstat_get(handle, &statbuf); if (err != 0) { // errno = err; return false; } // A tty is a character device that we can't seek or tell on. if (statbuf.fs_filetype != .CHARACTER_DEVICE or (statbuf.fs_rights_base & (RIGHT.FD_SEEK | RIGHT.FD_TELL)) != 0) { // errno = ENOTTY; return false; } return true; } if (builtin.os.tag == .linux) { while (true) { var wsz: linux.winsize = undefined; const fd = @bitCast(usize, @as(isize, handle)); const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @ptrToInt(&wsz)); switch (linux.getErrno(rc)) { .SUCCESS => return true, .INTR => continue, else => return false, } } } return system.isatty(handle) != 0; } pub fn isCygwinPty(handle: fd_t) bool { if (builtin.os.tag != .windows) return false; const size = @sizeOf(windows.FILE_NAME_INFO); var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (size + windows.MAX_PATH); if (windows.kernel32.GetFileInformationByHandleEx( handle, windows.FileNameInfo, @ptrCast(*anyopaque, &name_info_bytes), name_info_bytes.len, ) == 0) { return false; } const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]); const name_bytes = name_info_bytes[size .. size + @as(usize, name_info.FileNameLength)]; const name_wide = mem.bytesAsSlice(u16, name_bytes); return mem.indexOf(u16, name_wide, &[_]u16{ 'm', 's', 'y', 's', '-' }) != null or mem.indexOf(u16, name_wide, &[_]u16{ '-', 'p', 't', 'y' }) != null; } pub const SocketError = error{ /// Permission to create a socket of the specified type and/or /// pro‐tocol is denied. PermissionDenied, /// The implementation does not support the specified address family. AddressFamilyNotSupported, /// Unknown protocol, or protocol family not available. ProtocolFamilyNotAvailable, /// The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, /// Insufficient memory is available. The socket cannot be created until sufficient /// resources are freed. SystemResources, /// The protocol type or the specified protocol is not supported within this domain. ProtocolNotSupported, /// The socket type is not supported by the protocol. SocketTypeNotSupported, } || UnexpectedError; pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t { if (builtin.os.tag == .windows) { // NOTE: windows translates the SOCK.NONBLOCK/SOCK.CLOEXEC flags into // windows-analagous operations const filtered_sock_type = socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC); const flags: u32 = if ((socket_type & SOCK.CLOEXEC) != 0) windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT else 0; const rc = try windows.WSASocketW( @bitCast(i32, domain), @bitCast(i32, filtered_sock_type), @bitCast(i32, protocol), null, 0, flags, ); errdefer windows.closesocket(rc) catch unreachable; if ((socket_type & SOCK.NONBLOCK) != 0) { var mode: c_ulong = 1; // nonblocking if (windows.ws2_32.SOCKET_ERROR == windows.ws2_32.ioctlsocket(rc, windows.ws2_32.FIONBIO, &mode)) { switch (windows.ws2_32.WSAGetLastError()) { // have not identified any error codes that should be handled yet else => unreachable, } } } return rc; } const have_sock_flags = comptime !builtin.target.isDarwin(); const filtered_sock_type = if (!have_sock_flags) socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC) else socket_type; const rc = system.socket(domain, filtered_sock_type, protocol); switch (errno(rc)) { .SUCCESS => { const fd = @intCast(fd_t, rc); if (!have_sock_flags) { try setSockFlags(fd, socket_type); } return fd; }, .ACCES => return error.PermissionDenied, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .INVAL => return error.ProtocolFamilyNotAvailable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .PROTONOSUPPORT => return error.ProtocolNotSupported, .PROTOTYPE => return error.SocketTypeNotSupported, else => |err| return unexpectedErrno(err), } } pub const ShutdownError = error{ ConnectionAborted, /// Connection was reset by peer, application should close socket as it is no longer usable. ConnectionResetByPeer, BlockingOperationInProgress, /// The network subsystem has failed. NetworkSubsystemFailed, /// The socket is not connected (connection-oriented sockets only). SocketNotConnected, SystemResources, } || UnexpectedError; pub const ShutdownHow = enum { recv, send, both }; /// Shutdown socket send/receive operations pub fn shutdown(sock: socket_t, how: ShutdownHow) ShutdownError!void { if (builtin.os.tag == .windows) { const result = windows.ws2_32.shutdown(sock, switch (how) { .recv => windows.ws2_32.SD_RECEIVE, .send => windows.ws2_32.SD_SEND, .both => windows.ws2_32.SD_BOTH, }); if (0 != result) switch (windows.ws2_32.WSAGetLastError()) { .WSAECONNABORTED => return error.ConnectionAborted, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEINPROGRESS => return error.BlockingOperationInProgress, .WSAEINVAL => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOTCONN => return error.SocketNotConnected, .WSAENOTSOCK => unreachable, .WSANOTINITIALISED => unreachable, else => |err| return windows.unexpectedWSAError(err), }; } else { const rc = system.shutdown(sock, switch (how) { .recv => SHUT.RD, .send => SHUT.WR, .both => SHUT.RDWR, }); switch (errno(rc)) { .SUCCESS => return, .BADF => unreachable, .INVAL => unreachable, .NOTCONN => return error.SocketNotConnected, .NOTSOCK => unreachable, .NOBUFS => return error.SystemResources, else => |err| return unexpectedErrno(err), } } } pub fn closeSocket(sock: socket_t) void { if (builtin.os.tag == .windows) { windows.closesocket(sock) catch unreachable; } else { close(sock); } } pub const BindError = error{ /// The address is protected, and the user is not the superuser. /// For UNIX domain sockets: Search permission is denied on a component /// of the path prefix. AccessDenied, /// The given address is already in use, or in the case of Internet domain sockets, /// The port number was specified as zero in the socket /// address structure, but, upon attempting to bind to an ephemeral port, it was /// determined that all port numbers in the ephemeral port range are currently in /// use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range ip(7). AddressInUse, /// A nonexistent interface was requested or the requested address was not local. AddressNotAvailable, /// Too many symbolic links were encountered in resolving addr. SymLinkLoop, /// addr is too long. NameTooLong, /// A component in the directory prefix of the socket pathname does not exist. FileNotFound, /// Insufficient kernel memory was available. SystemResources, /// A component of the path prefix is not a directory. NotDir, /// The socket inode would reside on a read-only filesystem. ReadOnlyFileSystem, /// The network subsystem has failed. NetworkSubsystemFailed, FileDescriptorNotASocket, AlreadyBound, } || UnexpectedError; /// addr is `*const T` where T is one of the sockaddr pub fn bind(sock: socket_t, addr: *const sockaddr, len: socklen_t) BindError!void { if (builtin.os.tag == .windows) { const rc = windows.bind(sock, addr, len); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, // not initialized WSA .WSAEACCES => return error.AccessDenied, .WSAEADDRINUSE => return error.AddressInUse, .WSAEADDRNOTAVAIL => return error.AddressNotAvailable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEFAULT => unreachable, // invalid pointers .WSAEINVAL => return error.AlreadyBound, .WSAENOBUFS => return error.SystemResources, .WSAENETDOWN => return error.NetworkSubsystemFailed, else => |err| return windows.unexpectedWSAError(err), } unreachable; } return; } else { const rc = system.bind(sock, addr, len); switch (errno(rc)) { .SUCCESS => return, .ACCES => return error.AccessDenied, .ADDRINUSE => return error.AddressInUse, .BADF => unreachable, // always a race condition if this error is returned .INVAL => unreachable, // invalid parameters .NOTSOCK => unreachable, // invalid `sockfd` .ADDRNOTAVAIL => return error.AddressNotAvailable, .FAULT => unreachable, // invalid `addr` pointer .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } unreachable; } pub const ListenError = error{ /// Another socket is already listening on the same port. /// For Internet domain sockets, the socket referred to by sockfd had not previously /// been bound to an address and, upon attempting to bind it to an ephemeral port, it /// was determined that all port numbers in the ephemeral port range are currently in /// use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7). AddressInUse, /// The file descriptor sockfd does not refer to a socket. FileDescriptorNotASocket, /// The socket is not of a type that supports the listen() operation. OperationNotSupported, /// The network subsystem has failed. NetworkSubsystemFailed, /// Ran out of system resources /// On Windows it can either run out of socket descriptors or buffer space SystemResources, /// Already connected AlreadyConnected, /// Socket has not been bound yet SocketNotBound, } || UnexpectedError; pub fn listen(sock: socket_t, backlog: u31) ListenError!void { if (builtin.os.tag == .windows) { const rc = windows.listen(sock, backlog); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, // not initialized WSA .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEADDRINUSE => return error.AddressInUse, .WSAEISCONN => return error.AlreadyConnected, .WSAEINVAL => return error.SocketNotBound, .WSAEMFILE, .WSAENOBUFS => return error.SystemResources, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => return error.OperationNotSupported, .WSAEINPROGRESS => unreachable, else => |err| return windows.unexpectedWSAError(err), } } return; } else { const rc = system.listen(sock, backlog); switch (errno(rc)) { .SUCCESS => return, .ADDRINUSE => return error.AddressInUse, .BADF => unreachable, .NOTSOCK => return error.FileDescriptorNotASocket, .OPNOTSUPP => return error.OperationNotSupported, else => |err| return unexpectedErrno(err), } } } pub const AcceptError = error{ ConnectionAborted, /// The file descriptor sockfd does not refer to a socket. FileDescriptorNotASocket, /// The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, /// Not enough free memory. This often means that the memory allocation is limited /// by the socket buffer limits, not by the system memory. SystemResources, /// Socket is not listening for new connections. SocketNotListening, ProtocolFailure, /// Firewall rules forbid connection. BlockedByFirewall, /// This error occurs when no global event loop is configured, /// and accepting from the socket would block. WouldBlock, /// An incoming connection was indicated, but was subsequently terminated by the /// remote peer prior to accepting the call. ConnectionResetByPeer, /// The network subsystem has failed. NetworkSubsystemFailed, /// The referenced socket is not a type that supports connection-oriented service. OperationNotSupported, } || UnexpectedError; /// Accept a connection on a socket. /// If `sockfd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. pub fn accept( /// This argument is a socket that has been created with `socket`, bound to a local address /// with `bind`, and is listening for connections after a `listen`. sock: socket_t, /// This argument is a pointer to a sockaddr structure. This structure is filled in with the /// address of the peer socket, as known to the communications layer. The exact format of the /// address returned addr is determined by the socket's address family (see `socket` and the /// respective protocol man pages). addr: ?*sockaddr, /// This argument is a value-result argument: the caller must initialize it to contain the /// size (in bytes) of the structure pointed to by addr; on return it will contain the actual size /// of the peer address. /// /// The returned address is truncated if the buffer provided is too small; in this case, `addr_size` /// will return a value greater than was supplied to the call. addr_size: ?*socklen_t, /// The following values can be bitwise ORed in flags to obtain different behavior: /// * `SOCK.NONBLOCK` - Set the `O.NONBLOCK` file status flag on the open file description (see `open`) /// referred to by the new file descriptor. Using this flag saves extra calls to `fcntl` to achieve /// the same result. /// * `SOCK.CLOEXEC` - Set the close-on-exec (`FD_CLOEXEC`) flag on the new file descriptor. See the /// description of the `O.CLOEXEC` flag in `open` for reasons why this may be useful. flags: u32, ) AcceptError!socket_t { const have_accept4 = comptime !(builtin.target.isDarwin() or builtin.os.tag == .windows); assert(0 == (flags & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC))); // Unsupported flag(s) const accepted_sock = while (true) { const rc = if (have_accept4) system.accept4(sock, addr, addr_size, flags) else if (builtin.os.tag == .windows) windows.accept(sock, addr, addr_size) else system.accept(sock, addr, addr_size); if (builtin.os.tag == .windows) { if (rc == windows.ws2_32.INVALID_SOCKET) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, // not initialized WSA .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEFAULT => unreachable, .WSAEINVAL => return error.SocketNotListening, .WSAEMFILE => return error.ProcessFdQuotaExceeded, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOBUFS => return error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => return error.OperationNotSupported, .WSAEWOULDBLOCK => return error.WouldBlock, else => |err| return windows.unexpectedWSAError(err), } } else { break rc; } } else { switch (errno(rc)) { .SUCCESS => { break @intCast(socket_t, rc); }, .INTR => continue, .AGAIN => return error.WouldBlock, .BADF => unreachable, // always a race condition .CONNABORTED => return error.ConnectionAborted, .FAULT => unreachable, .INVAL => return error.SocketNotListening, .NOTSOCK => unreachable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .OPNOTSUPP => unreachable, .PROTO => return error.ProtocolFailure, .PERM => return error.BlockedByFirewall, else => |err| return unexpectedErrno(err), } } } else unreachable; if (!have_accept4) { try setSockFlags(accepted_sock, flags); } return accepted_sock; } pub const EpollCreateError = error{ /// The per-user limit on the number of epoll instances imposed by /// /proc/sys/fs/epoll/max_user_instances was encountered. See epoll(7) for further /// details. /// Or, The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, /// There was insufficient memory to create the kernel object. SystemResources, } || UnexpectedError; pub fn epoll_create1(flags: u32) EpollCreateError!i32 { const rc = system.epoll_create1(flags); switch (errno(rc)) { .SUCCESS => return @intCast(i32, rc), else => |err| return unexpectedErrno(err), .INVAL => unreachable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOMEM => return error.SystemResources, } } pub const EpollCtlError = error{ /// op was EPOLL_CTL_ADD, and the supplied file descriptor fd is already registered /// with this epoll instance. FileDescriptorAlreadyPresentInSet, /// fd refers to an epoll instance and this EPOLL_CTL_ADD operation would result in a /// circular loop of epoll instances monitoring one another. OperationCausesCircularLoop, /// op was EPOLL_CTL_MOD or EPOLL_CTL_DEL, and fd is not registered with this epoll /// instance. FileDescriptorNotRegistered, /// There was insufficient memory to handle the requested op control operation. SystemResources, /// The limit imposed by /proc/sys/fs/epoll/max_user_watches was encountered while /// trying to register (EPOLL_CTL_ADD) a new file descriptor on an epoll instance. /// See epoll(7) for further details. UserResourceLimitReached, /// The target file fd does not support epoll. This error can occur if fd refers to, /// for example, a regular file or a directory. FileDescriptorIncompatibleWithEpoll, } || UnexpectedError; pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*linux.epoll_event) EpollCtlError!void { const rc = system.epoll_ctl(epfd, op, fd, event); switch (errno(rc)) { .SUCCESS => return, else => |err| return unexpectedErrno(err), .BADF => unreachable, // always a race condition if this happens .EXIST => return error.FileDescriptorAlreadyPresentInSet, .INVAL => unreachable, .LOOP => return error.OperationCausesCircularLoop, .NOENT => return error.FileDescriptorNotRegistered, .NOMEM => return error.SystemResources, .NOSPC => return error.UserResourceLimitReached, .PERM => return error.FileDescriptorIncompatibleWithEpoll, } } /// Waits for an I/O event on an epoll file descriptor. /// Returns the number of file descriptors ready for the requested I/O, /// or zero if no file descriptor became ready during the requested timeout milliseconds. pub fn epoll_wait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize { while (true) { // TODO get rid of the @intCast const rc = system.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .BADF => unreachable, .FAULT => unreachable, .INVAL => unreachable, else => unreachable, } } } pub const EventFdError = error{ SystemResources, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, } || UnexpectedError; pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 { const rc = system.eventfd(initval, flags); switch (errno(rc)) { .SUCCESS => return @intCast(i32, rc), else => |err| return unexpectedErrno(err), .INVAL => unreachable, // invalid parameters .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NODEV => return error.SystemResources, .NOMEM => return error.SystemResources, } } pub const GetSockNameError = error{ /// Insufficient resources were available in the system to perform the operation. SystemResources, /// The network subsystem has failed. NetworkSubsystemFailed, /// Socket hasn't been bound yet SocketNotBound, FileDescriptorNotASocket, } || UnexpectedError; pub fn getsockname(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSockNameError!void { if (builtin.os.tag == .windows) { const rc = windows.getsockname(sock, addr, addrlen); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEFAULT => unreachable, // addr or addrlen have invalid pointers or addrlen points to an incorrect value .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotBound, else => |err| return windows.unexpectedWSAError(err), } } return; } else { const rc = system.getsockname(sock, addr, addrlen); switch (errno(rc)) { .SUCCESS => return, else => |err| return unexpectedErrno(err), .BADF => unreachable, // always a race condition .FAULT => unreachable, .INVAL => unreachable, // invalid parameters .NOTSOCK => return error.FileDescriptorNotASocket, .NOBUFS => return error.SystemResources, } } } pub fn getpeername(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSockNameError!void { if (builtin.os.tag == .windows) { const rc = windows.getpeername(sock, addr, addrlen); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEFAULT => unreachable, // addr or addrlen have invalid pointers or addrlen points to an incorrect value .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotBound, else => |err| return windows.unexpectedWSAError(err), } } return; } else { const rc = system.getpeername(sock, addr, addrlen); switch (errno(rc)) { .SUCCESS => return, else => |err| return unexpectedErrno(err), .BADF => unreachable, // always a race condition .FAULT => unreachable, .INVAL => unreachable, // invalid parameters .NOTSOCK => return error.FileDescriptorNotASocket, .NOBUFS => return error.SystemResources, } } } pub const ConnectError = error{ /// For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket /// file, or search permission is denied for one of the directories in the path prefix. /// or /// The user tried to connect to a broadcast address without having the socket broadcast flag enabled or /// the connection request failed because of a local firewall rule. PermissionDenied, /// Local address is already in use. AddressInUse, /// (Internet domain sockets) The socket referred to by sockfd had not previously been bound to an /// address and, upon attempting to bind it to an ephemeral port, it was determined that all port numbers /// in the ephemeral port range are currently in use. See the discussion of /// /proc/sys/net/ipv4/ip_local_port_range in ip(7). AddressNotAvailable, /// The passed address didn't have the correct address family in its sa_family field. AddressFamilyNotSupported, /// Insufficient entries in the routing cache. SystemResources, /// A connect() on a stream socket found no one listening on the remote address. ConnectionRefused, /// Network is unreachable. NetworkUnreachable, /// Timeout while attempting connection. The server may be too busy to accept new connections. Note /// that for IP sockets the timeout may be very long when syncookies are enabled on the server. ConnectionTimedOut, /// This error occurs when no global event loop is configured, /// and connecting to the socket would block. WouldBlock, /// The given path for the unix socket does not exist. FileNotFound, /// Connection was reset by peer before connect could complete. ConnectionResetByPeer, /// Socket is non-blocking and already has a pending connection in progress. ConnectionPending, } || UnexpectedError; /// Initiate a connection on a socket. /// If `sockfd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN or EINPROGRESS is received. pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void { if (builtin.os.tag == .windows) { const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(i32, len)); if (rc == 0) return; switch (windows.ws2_32.WSAGetLastError()) { .WSAEADDRINUSE => return error.AddressInUse, .WSAEADDRNOTAVAIL => return error.AddressNotAvailable, .WSAECONNREFUSED => return error.ConnectionRefused, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAETIMEDOUT => return error.ConnectionTimedOut, .WSAEHOSTUNREACH, // TODO: should we return NetworkUnreachable in this case as well? .WSAENETUNREACH, => return error.NetworkUnreachable, .WSAEFAULT => unreachable, .WSAEINVAL => unreachable, .WSAEISCONN => unreachable, .WSAENOTSOCK => unreachable, .WSAEWOULDBLOCK => unreachable, .WSAEACCES => unreachable, .WSAENOBUFS => return error.SystemResources, .WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported, else => |err| return windows.unexpectedWSAError(err), } return; } while (true) { switch (errno(system.connect(sock, sock_addr, len))) { .SUCCESS => return, .ACCES => return error.PermissionDenied, .PERM => return error.PermissionDenied, .ADDRINUSE => return error.AddressInUse, .ADDRNOTAVAIL => return error.AddressNotAvailable, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .AGAIN, .INPROGRESS => return error.WouldBlock, .ALREADY => return error.ConnectionPending, .BADF => unreachable, // sockfd is not a valid open file descriptor. .CONNREFUSED => return error.ConnectionRefused, .CONNRESET => return error.ConnectionResetByPeer, .FAULT => unreachable, // The socket structure address is outside the user's address space. .INTR => continue, .ISCONN => unreachable, // The socket is already connected. .NETUNREACH => return error.NetworkUnreachable, .NOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. .PROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. .TIMEDOUT => return error.ConnectionTimedOut, .NOENT => return error.FileNotFound, // Returned when socket is AF.UNIX and the given path does not exist. else => |err| return unexpectedErrno(err), } } } pub fn getsockoptError(sockfd: fd_t) ConnectError!void { var err_code: i32 = undefined; var size: u32 = @sizeOf(u32); const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @ptrCast([*]u8, &err_code), &size); assert(size == 4); switch (errno(rc)) { .SUCCESS => switch (@intToEnum(E, err_code)) { .SUCCESS => return, .ACCES => return error.PermissionDenied, .PERM => return error.PermissionDenied, .ADDRINUSE => return error.AddressInUse, .ADDRNOTAVAIL => return error.AddressNotAvailable, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .AGAIN => return error.SystemResources, .ALREADY => return error.ConnectionPending, .BADF => unreachable, // sockfd is not a valid open file descriptor. .CONNREFUSED => return error.ConnectionRefused, .FAULT => unreachable, // The socket structure address is outside the user's address space. .ISCONN => unreachable, // The socket is already connected. .NETUNREACH => return error.NetworkUnreachable, .NOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. .PROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. .TIMEDOUT => return error.ConnectionTimedOut, .CONNRESET => return error.ConnectionResetByPeer, else => |err| return unexpectedErrno(err), }, .BADF => unreachable, // The argument sockfd is not a valid file descriptor. .FAULT => unreachable, // The address pointed to by optval or optlen is not in a valid part of the process address space. .INVAL => unreachable, .NOPROTOOPT => unreachable, // The option is unknown at the level indicated. .NOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. else => |err| return unexpectedErrno(err), } } pub const WaitPidResult = struct { pid: pid_t, status: u32, }; /// Use this version of the `waitpid` wrapper if you spawned your child process using explicit /// `fork` and `execve` method. If you spawned your child process using `posix_spawn` method, /// use `std.os.posix_spawn.waitpid` instead. pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult { const Status = if (builtin.link_libc) c_int else u32; var status: Status = undefined; while (true) { const rc = system.waitpid(pid, &status, if (builtin.link_libc) @intCast(c_int, flags) else flags); switch (errno(rc)) { .SUCCESS => return .{ .pid = @intCast(pid_t, rc), .status = @bitCast(u32, status), }, .INTR => continue, .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. .INVAL => unreachable, // Invalid flags. else => unreachable, } } } pub const FStatError = error{ SystemResources, /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to get its filestat information. AccessDenied, } || UnexpectedError; /// Return information about a file descriptor. pub fn fstat(fd: fd_t) FStatError!Stat { if (builtin.os.tag == .wasi and !builtin.link_libc) { var stat: wasi.filestat_t = undefined; switch (wasi.fd_filestat_get(fd, &stat)) { .SUCCESS => return Stat.fromFilestat(stat), .INVAL => unreachable, .BADF => unreachable, // Always a race condition. .NOMEM => return error.SystemResources, .ACCES => return error.AccessDenied, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .windows) { @compileError("fstat is not yet implemented on Windows"); } const fstat_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.fstat64 else system.fstat; var stat = mem.zeroes(Stat); switch (errno(fstat_sym(fd, &stat))) { .SUCCESS => return stat, .INVAL => unreachable, .BADF => unreachable, // Always a race condition. .NOMEM => return error.SystemResources, .ACCES => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound, SymLinkLoop }; /// Similar to `fstat`, but returns stat of a resource pointed to by `pathname` /// which is relative to `dirfd` handle. /// See also `fstatatZ` and `fstatatWasi`. pub fn fstatat(dirfd: fd_t, pathname: []const u8, flags: u32) FStatAtError!Stat { if (builtin.os.tag == .wasi and !builtin.link_libc) { const wasi_flags = if (flags & linux.AT.SYMLINK_NOFOLLOW == 0) wasi.LOOKUP_SYMLINK_FOLLOW else 0; if (dirfd == wasi.AT.FDCWD or fs.path.isAbsolute(pathname)) { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; const path = try resolvePathWasi(pathname, &path_buf); return fstatatWasi(path.dir_fd, path.relative_path, wasi_flags); } return fstatatWasi(dirfd, pathname, wasi_flags); } else if (builtin.os.tag == .windows) { @compileError("fstatat is not yet implemented on Windows"); } else { const pathname_c = try toPosixPath(pathname); return fstatatZ(dirfd, &pathname_c, flags); } } /// WASI-only. Same as `fstatat` but targeting WASI. /// See also `fstatat`. pub fn fstatatWasi(dirfd: fd_t, pathname: []const u8, flags: u32) FStatAtError!Stat { var stat: wasi.filestat_t = undefined; switch (wasi.path_filestat_get(dirfd, flags, pathname.ptr, pathname.len, &stat)) { .SUCCESS => return Stat.fromFilestat(stat), .INVAL => unreachable, .BADF => unreachable, // Always a race condition. .NOMEM => return error.SystemResources, .ACCES => return error.AccessDenied, .FAULT => unreachable, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.FileNotFound, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } /// Same as `fstatat` but `pathname` is null-terminated. /// See also `fstatat`. pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!Stat { if (builtin.os.tag == .wasi and !builtin.link_libc) { return fstatatWasi(dirfd, mem.sliceTo(pathname), flags); } const fstatat_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.fstatat64 else system.fstatat; var stat = mem.zeroes(Stat); switch (errno(fstatat_sym(dirfd, pathname, &stat, flags))) { .SUCCESS => return stat, .INVAL => unreachable, .BADF => unreachable, // Always a race condition. .NOMEM => return error.SystemResources, .ACCES => return error.AccessDenied, .PERM => return error.AccessDenied, .FAULT => unreachable, .NAMETOOLONG => return error.NameTooLong, .LOOP => return error.SymLinkLoop, .NOENT => return error.FileNotFound, .NOTDIR => return error.FileNotFound, else => |err| return unexpectedErrno(err), } } pub const KQueueError = error{ /// The per-process limit on the number of open file descriptors has been reached. ProcessFdQuotaExceeded, /// The system-wide limit on the total number of open files has been reached. SystemFdQuotaExceeded, } || UnexpectedError; pub fn kqueue() KQueueError!i32 { const rc = system.kqueue(); switch (errno(rc)) { .SUCCESS => return @intCast(i32, rc), .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, else => |err| return unexpectedErrno(err), } } pub const KEventError = error{ /// The process does not have permission to register a filter. AccessDenied, /// The event could not be found to be modified or deleted. EventNotFound, /// No memory was available to register the event. SystemResources, /// The specified process to attach to does not exist. ProcessNotFound, /// changelist or eventlist had too many items on it. /// TODO remove this possibility Overflow, }; pub fn kevent( kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec, ) KEventError!usize { while (true) { const rc = system.kevent( kq, changelist.ptr, math.cast(c_int, changelist.len) orelse return error.Overflow, eventlist.ptr, math.cast(c_int, eventlist.len) orelse return error.Overflow, timeout, ); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .ACCES => return error.AccessDenied, .FAULT => unreachable, .BADF => unreachable, // Always a race condition. .INTR => continue, .INVAL => unreachable, .NOENT => return error.EventNotFound, .NOMEM => return error.SystemResources, .SRCH => return error.ProcessNotFound, else => unreachable, } } } pub const INotifyInitError = error{ ProcessFdQuotaExceeded, SystemFdQuotaExceeded, SystemResources, } || UnexpectedError; /// initialize an inotify instance pub fn inotify_init1(flags: u32) INotifyInitError!i32 { const rc = system.inotify_init1(flags); switch (errno(rc)) { .SUCCESS => return @intCast(i32, rc), .INVAL => unreachable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub const INotifyAddWatchError = error{ AccessDenied, NameTooLong, FileNotFound, SystemResources, UserResourceLimitReached, NotDir, } || UnexpectedError; /// add a watch to an initialized inotify instance pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INotifyAddWatchError!i32 { const pathname_c = try toPosixPath(pathname); return inotify_add_watchZ(inotify_fd, &pathname_c, mask); } /// Same as `inotify_add_watch` except pathname is null-terminated. pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 { const rc = system.inotify_add_watch(inotify_fd, pathname, mask); switch (errno(rc)) { .SUCCESS => return @intCast(i32, rc), .ACCES => return error.AccessDenied, .BADF => unreachable, .FAULT => unreachable, .INVAL => unreachable, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOSPC => return error.UserResourceLimitReached, .NOTDIR => return error.NotDir, else => |err| return unexpectedErrno(err), } } /// remove an existing watch from an inotify instance pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void { switch (errno(system.inotify_rm_watch(inotify_fd, wd))) { .SUCCESS => return, .BADF => unreachable, .INVAL => unreachable, else => unreachable, } } pub const MProtectError = error{ /// The memory cannot be given the specified access. This can happen, for example, if you /// mmap(2) a file to which you have read-only access, then ask mprotect() to mark it /// PROT_WRITE. AccessDenied, /// Changing the protection of a memory region would result in the total number of map‐ /// pings with distinct attributes (e.g., read versus read/write protection) exceeding the /// allowed maximum. (For example, making the protection of a range PROT_READ in the mid‐ /// dle of a region currently protected as PROT_READ|PROT_WRITE would result in three map‐ /// pings: two read/write mappings at each end and a read-only mapping in the middle.) OutOfMemory, } || UnexpectedError; /// `memory.len` must be page-aligned. pub fn mprotect(memory: []align(mem.page_size) u8, protection: u32) MProtectError!void { assert(mem.isAligned(memory.len, mem.page_size)); switch (errno(system.mprotect(memory.ptr, memory.len, protection))) { .SUCCESS => return, .INVAL => unreachable, .ACCES => return error.AccessDenied, .NOMEM => return error.OutOfMemory, else => |err| return unexpectedErrno(err), } } pub const ForkError = error{SystemResources} || UnexpectedError; pub fn fork() ForkError!pid_t { const rc = system.fork(); switch (errno(rc)) { .SUCCESS => return @intCast(pid_t, rc), .AGAIN => return error.SystemResources, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub const MMapError = error{ /// The underlying filesystem of the specified file does not support memory mapping. MemoryMappingNotSupported, /// A file descriptor refers to a non-regular file. Or a file mapping was requested, /// but the file descriptor is not open for reading. Or `MAP.SHARED` was requested /// and `PROT_WRITE` is set, but the file descriptor is not open in `O.RDWR` mode. /// Or `PROT_WRITE` is set, but the file is append-only. AccessDenied, /// The `prot` argument asks for `PROT_EXEC` but the mapped area belongs to a file on /// a filesystem that was mounted no-exec. PermissionDenied, LockedMemoryLimitExceeded, OutOfMemory, } || UnexpectedError; /// Map files or devices into memory. /// `length` does not need to be aligned. /// Use of a mapped region can result in these signals: /// * SIGSEGV - Attempted write into a region mapped as read-only. /// * SIGBUS - Attempted access to a portion of the buffer that does not correspond to the file pub fn mmap( ptr: ?[*]align(mem.page_size) u8, length: usize, prot: u32, flags: u32, fd: fd_t, offset: u64, ) MMapError![]align(mem.page_size) u8 { const mmap_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.mmap64 else system.mmap; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned const rc = mmap_sym(ptr, length, prot, flags, fd, ioffset); const err = if (builtin.link_libc) blk: { if (rc != std.c.MAP.FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length]; break :blk @intToEnum(E, system._errno().*); } else blk: { const err = errno(rc); if (err == .SUCCESS) return @intToPtr([*]align(mem.page_size) u8, rc)[0..length]; break :blk err; }; switch (err) { .SUCCESS => unreachable, .TXTBSY => return error.AccessDenied, .ACCES => return error.AccessDenied, .PERM => return error.PermissionDenied, .AGAIN => return error.LockedMemoryLimitExceeded, .BADF => unreachable, // Always a race condition. .OVERFLOW => unreachable, // The number of pages used for length + offset would overflow. .NODEV => return error.MemoryMappingNotSupported, .INVAL => unreachable, // Invalid parameters to mmap() .NOMEM => return error.OutOfMemory, else => return unexpectedErrno(err), } } /// Deletes the mappings for the specified address range, causing /// further references to addresses within the range to generate invalid memory references. /// Note that while POSIX allows unmapping a region in the middle of an existing mapping, /// Zig's munmap function does not, for two reasons: /// * It violates the Zig principle that resource deallocation must succeed. /// * The Windows function, VirtualFree, has this restriction. pub fn munmap(memory: []align(mem.page_size) const u8) void { switch (errno(system.munmap(memory.ptr, memory.len))) { .SUCCESS => return, .INVAL => unreachable, // Invalid parameters. .NOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping. else => unreachable, } } pub const MSyncError = error{ UnmappedMemory, } || UnexpectedError; pub fn msync(memory: []align(mem.page_size) u8, flags: i32) MSyncError!void { switch (errno(system.msync(memory.ptr, memory.len, flags))) { .SUCCESS => return, .NOMEM => return error.UnmappedMemory, // Unsuccessful, provided pointer does not point mapped memory .INVAL => unreachable, // Invalid parameters. else => unreachable, } } pub const AccessError = error{ PermissionDenied, FileNotFound, NameTooLong, InputOutput, SystemResources, BadPathName, FileBusy, SymLinkLoop, ReadOnlyFileSystem, /// On Windows, file paths must be valid Unicode. InvalidUtf8, } || UnexpectedError; /// check user's permissions for a file /// TODO currently this assumes `mode` is `F.OK` on Windows. pub fn access(path: []const u8, mode: u32) AccessError!void { if (builtin.os.tag == .windows) { const path_w = try windows.sliceToPrefixedFileW(path); _ = try windows.GetFileAttributesW(path_w.span().ptr); return; } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return faccessat(wasi.AT.FDCWD, path, mode, 0); } const path_c = try toPosixPath(path); return accessZ(&path_c, mode); } /// Same as `access` except `path` is null-terminated. pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void { if (builtin.os.tag == .windows) { const path_w = try windows.cStrToPrefixedFileW(path); _ = try windows.GetFileAttributesW(path_w.span().ptr); return; } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return access(mem.sliceTo(path, 0), mode); } switch (errno(system.access(path, mode))) { .SUCCESS => return, .ACCES => return error.PermissionDenied, .ROFS => return error.ReadOnlyFileSystem, .LOOP => return error.SymLinkLoop, .TXTBSY => return error.FileBusy, .NOTDIR => return error.FileNotFound, .NOENT => return error.FileNotFound, .NAMETOOLONG => return error.NameTooLong, .INVAL => unreachable, .FAULT => unreachable, .IO => return error.InputOutput, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. /// Otherwise use `access` or `accessC`. /// TODO currently this ignores `mode`. pub fn accessW(path: [*:0]const u16, mode: u32) windows.GetFileAttributesError!void { _ = mode; const ret = try windows.GetFileAttributesW(path); if (ret != windows.INVALID_FILE_ATTRIBUTES) { return; } switch (windows.kernel32.GetLastError()) { .FILE_NOT_FOUND => return error.FileNotFound, .PATH_NOT_FOUND => return error.FileNotFound, .ACCESS_DENIED => return error.PermissionDenied, else => |err| return windows.unexpectedError(err), } } /// Check user's permissions for a file, based on an open directory handle. /// TODO currently this ignores `mode` and `flags` on Windows. pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessError!void { if (builtin.os.tag == .windows) { const path_w = try windows.sliceToPrefixedFileW(path); return faccessatW(dirfd, path_w.span().ptr, mode, flags); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { var resolved = RelativePathWasi{ .dir_fd = dirfd, .relative_path = path }; const file = blk: { if (dirfd == wasi.AT.FDCWD or fs.path.isAbsolute(path)) { // Resolve absolute or CWD-relative paths to a path within a Preopen var path_buf: [MAX_PATH_BYTES]u8 = undefined; resolved = resolvePathWasi(path, &path_buf) catch |err| break :blk @as(FStatAtError!Stat, err); break :blk fstatat(resolved.dir_fd, resolved.relative_path, flags); } break :blk fstatat(dirfd, path, flags); } catch |err| switch (err) { error.AccessDenied => return error.PermissionDenied, else => |e| return e, }; if (mode != F_OK) { var directory: wasi.fdstat_t = undefined; if (wasi.fd_fdstat_get(resolved.dir_fd, &directory) != .SUCCESS) { return error.PermissionDenied; } var rights: wasi.rights_t = 0; if (mode & R_OK != 0) { rights |= if (file.filetype == .DIRECTORY) wasi.RIGHT.FD_READDIR else wasi.RIGHT.FD_READ; } if (mode & W_OK != 0) { rights |= wasi.RIGHT.FD_WRITE; } // No validation for X_OK if ((rights & directory.fs_rights_inheriting) != rights) { return error.PermissionDenied; } } return; } const path_c = try toPosixPath(path); return faccessatZ(dirfd, &path_c, mode, flags); } /// Same as `faccessat` except the path parameter is null-terminated. pub fn faccessatZ(dirfd: fd_t, path: [*:0]const u8, mode: u32, flags: u32) AccessError!void { if (builtin.os.tag == .windows) { const path_w = try windows.cStrToPrefixedFileW(path); return faccessatW(dirfd, path_w.span().ptr, mode, flags); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return faccessat(dirfd, mem.sliceTo(path, 0), mode, flags); } switch (errno(system.faccessat(dirfd, path, mode, flags))) { .SUCCESS => return, .ACCES => return error.PermissionDenied, .ROFS => return error.ReadOnlyFileSystem, .LOOP => return error.SymLinkLoop, .TXTBSY => return error.FileBusy, .NOTDIR => return error.FileNotFound, .NOENT => return error.FileNotFound, .NAMETOOLONG => return error.NameTooLong, .INVAL => unreachable, .FAULT => unreachable, .IO => return error.InputOutput, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } /// Same as `faccessat` except asserts the target is Windows and the path parameter /// is NtDll-prefixed, null-terminated, WTF-16 encoded. /// TODO currently this ignores `mode` and `flags` pub fn faccessatW(dirfd: fd_t, sub_path_w: [*:0]const u16, mode: u32, flags: u32) AccessError!void { _ = mode; _ = flags; if (sub_path_w[0] == '.' and sub_path_w[1] == 0) { return; } if (sub_path_w[0] == '.' and sub_path_w[1] == '.' and sub_path_w[2] == 0) { return; } const path_len_bytes = math.cast(u16, mem.sliceTo(sub_path_w, 0).len * 2) orelse return error.NameTooLong; var nt_name = windows.UNICODE_STRING{ .Length = path_len_bytes, .MaximumLength = path_len_bytes, .Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w)), }; var attr = windows.OBJECT_ATTRIBUTES{ .Length = @sizeOf(windows.OBJECT_ATTRIBUTES), .RootDirectory = if (std.fs.path.isAbsoluteWindowsW(sub_path_w)) null else dirfd, .Attributes = 0, // Note we do not use OBJ_CASE_INSENSITIVE here. .ObjectName = &nt_name, .SecurityDescriptor = null, .SecurityQualityOfService = null, }; var basic_info: windows.FILE_BASIC_INFORMATION = undefined; switch (windows.ntdll.NtQueryAttributesFile(&attr, &basic_info)) { .SUCCESS => return, .OBJECT_NAME_NOT_FOUND => return error.FileNotFound, .OBJECT_PATH_NOT_FOUND => return error.FileNotFound, .OBJECT_NAME_INVALID => unreachable, .INVALID_PARAMETER => unreachable, .ACCESS_DENIED => return error.PermissionDenied, .OBJECT_PATH_SYNTAX_BAD => unreachable, else => |rc| return windows.unexpectedStatus(rc), } } pub const PipeError = error{ SystemFdQuotaExceeded, ProcessFdQuotaExceeded, } || UnexpectedError; /// Creates a unidirectional data channel that can be used for interprocess communication. pub fn pipe() PipeError![2]fd_t { var fds: [2]fd_t = undefined; switch (errno(system.pipe(&fds))) { .SUCCESS => return fds, .INVAL => unreachable, // Invalid parameters to pipe() .FAULT => unreachable, // Invalid fds pointer .NFILE => return error.SystemFdQuotaExceeded, .MFILE => return error.ProcessFdQuotaExceeded, else => |err| return unexpectedErrno(err), } } pub fn pipe2(flags: u32) PipeError![2]fd_t { if (@hasDecl(system, "pipe2")) { var fds: [2]fd_t = undefined; switch (errno(system.pipe2(&fds, flags))) { .SUCCESS => return fds, .INVAL => unreachable, // Invalid flags .FAULT => unreachable, // Invalid fds pointer .NFILE => return error.SystemFdQuotaExceeded, .MFILE => return error.ProcessFdQuotaExceeded, else => |err| return unexpectedErrno(err), } } var fds: [2]fd_t = try pipe(); errdefer { close(fds[0]); close(fds[1]); } if (flags == 0) return fds; // O.CLOEXEC is special, it's a file descriptor flag and must be set using // F.SETFD. if (flags & O.CLOEXEC != 0) { for (fds) |fd| { switch (errno(system.fcntl(fd, F.SETFD, @as(u32, FD_CLOEXEC)))) { .SUCCESS => {}, .INVAL => unreachable, // Invalid flags .BADF => unreachable, // Always a race condition else => |err| return unexpectedErrno(err), } } } const new_flags = flags & ~@as(u32, O.CLOEXEC); // Set every other flag affecting the file status using F.SETFL. if (new_flags != 0) { for (fds) |fd| { switch (errno(system.fcntl(fd, F.SETFL, new_flags))) { .SUCCESS => {}, .INVAL => unreachable, // Invalid flags .BADF => unreachable, // Always a race condition else => |err| return unexpectedErrno(err), } } } return fds; } pub const SysCtlError = error{ PermissionDenied, SystemResources, NameTooLong, UnknownName, } || UnexpectedError; pub fn sysctl( name: []const c_int, oldp: ?*anyopaque, oldlenp: ?*usize, newp: ?*anyopaque, newlen: usize, ) SysCtlError!void { if (builtin.os.tag == .wasi) { @panic("unsupported"); // TODO should be compile error, not panic } if (builtin.os.tag == .haiku) { @panic("unsupported"); // TODO should be compile error, not panic } const name_len = math.cast(c_uint, name.len) orelse return error.NameTooLong; switch (errno(system.sysctl(name.ptr, name_len, oldp, oldlenp, newp, newlen))) { .SUCCESS => return, .FAULT => unreachable, .PERM => return error.PermissionDenied, .NOMEM => return error.SystemResources, .NOENT => return error.UnknownName, else => |err| return unexpectedErrno(err), } } pub fn sysctlbynameZ( name: [*:0]const u8, oldp: ?*anyopaque, oldlenp: ?*usize, newp: ?*anyopaque, newlen: usize, ) SysCtlError!void { if (builtin.os.tag == .wasi) { @panic("unsupported"); // TODO should be compile error, not panic } if (builtin.os.tag == .haiku) { @panic("unsupported"); // TODO should be compile error, not panic } switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) { .SUCCESS => return, .FAULT => unreachable, .PERM => return error.PermissionDenied, .NOMEM => return error.SystemResources, .NOENT => return error.UnknownName, else => |err| return unexpectedErrno(err), } } pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { switch (errno(system.gettimeofday(tv, tz))) { .SUCCESS => return, .INVAL => unreachable, else => unreachable, } } pub const SeekError = error{ Unseekable, /// In WASI, this error may occur when the file descriptor does /// not hold the required rights to seek on it. AccessDenied, } || UnexpectedError; /// Repositions read/write file offset relative to the beginning. pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void { if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, offset, &result, SEEK.SET))) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .windows) { return windows.SetFilePointerEx_BEGIN(fd, offset); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var new_offset: wasi.filesize_t = undefined; switch (wasi.fd_seek(fd, @bitCast(wasi.filedelta_t, offset), .SET, &new_offset)) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const lseek_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.lseek64 else system.lseek; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned switch (errno(lseek_sym(fd, ioffset, SEEK.SET))) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } /// Repositions read/write file offset relative to the current offset. pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void { if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK.CUR))) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .windows) { return windows.SetFilePointerEx_CURRENT(fd, offset); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var new_offset: wasi.filesize_t = undefined; switch (wasi.fd_seek(fd, offset, .CUR, &new_offset)) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const lseek_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.lseek64 else system.lseek; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned switch (errno(lseek_sym(fd, ioffset, SEEK.CUR))) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } /// Repositions read/write file offset relative to the end. pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void { if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK.END))) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .windows) { return windows.SetFilePointerEx_END(fd, offset); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var new_offset: wasi.filesize_t = undefined; switch (wasi.fd_seek(fd, offset, .END, &new_offset)) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const lseek_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.lseek64 else system.lseek; const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned switch (errno(lseek_sym(fd, ioffset, SEEK.END))) { .SUCCESS => return, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } /// Returns the read/write file offset relative to the beginning. pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 { if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) { var result: u64 = undefined; switch (errno(system.llseek(fd, 0, &result, SEEK.CUR))) { .SUCCESS => return result, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .windows) { return windows.SetFilePointerEx_CURRENT_get(fd); } if (builtin.os.tag == .wasi and !builtin.link_libc) { var new_offset: wasi.filesize_t = undefined; switch (wasi.fd_seek(fd, 0, .CUR, &new_offset)) { .SUCCESS => return new_offset, .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, .NOTCAPABLE => return error.AccessDenied, else => |err| return unexpectedErrno(err), } } const lseek_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.lseek64 else system.lseek; const rc = lseek_sym(fd, 0, SEEK.CUR); switch (errno(rc)) { .SUCCESS => return @bitCast(u64, rc), .BADF => unreachable, // always a race condition .INVAL => return error.Unseekable, .OVERFLOW => return error.Unseekable, .SPIPE => return error.Unseekable, .NXIO => return error.Unseekable, else => |err| return unexpectedErrno(err), } } pub const FcntlError = error{ PermissionDenied, FileBusy, ProcessFdQuotaExceeded, Locked, DeadLock, LockedRegionLimitExceeded, } || UnexpectedError; pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize { while (true) { const rc = system.fcntl(fd, cmd, arg); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .INTR => continue, .AGAIN, .ACCES => return error.Locked, .BADF => unreachable, .BUSY => return error.FileBusy, .INVAL => unreachable, // invalid parameters .PERM => return error.PermissionDenied, .MFILE => return error.ProcessFdQuotaExceeded, .NOTDIR => unreachable, // invalid parameter .DEADLK => return error.DeadLock, .NOLCK => return error.LockedRegionLimitExceeded, else => |err| return unexpectedErrno(err), } } } fn setSockFlags(sock: socket_t, flags: u32) !void { if ((flags & SOCK.CLOEXEC) != 0) { if (builtin.os.tag == .windows) { // TODO: Find out if this is supported for sockets } else { var fd_flags = fcntl(sock, F.GETFD, 0) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; fd_flags |= FD_CLOEXEC; _ = fcntl(sock, F.SETFD, fd_flags) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; } } if ((flags & SOCK.NONBLOCK) != 0) { if (builtin.os.tag == .windows) { var mode: c_ulong = 1; if (windows.ws2_32.ioctlsocket(sock, windows.ws2_32.FIONBIO, &mode) == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOTSOCK => return error.FileDescriptorNotASocket, // TODO: handle more errors else => |err| return windows.unexpectedWSAError(err), } } } else { var fl_flags = fcntl(sock, F.GETFL, 0) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; fl_flags |= O.NONBLOCK; _ = fcntl(sock, F.SETFL, fl_flags) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; } } } pub const FlockError = error{ WouldBlock, /// The kernel ran out of memory for allocating file locks SystemResources, /// The underlying filesystem does not support file locks FileLocksNotSupported, } || UnexpectedError; /// Depending on the operating system `flock` may or may not interact with /// `fcntl` locks made by other processes. pub fn flock(fd: fd_t, operation: i32) FlockError!void { while (true) { const rc = system.flock(fd, operation); switch (errno(rc)) { .SUCCESS => return, .BADF => unreachable, .INTR => continue, .INVAL => unreachable, // invalid parameters .NOLCK => return error.SystemResources, .AGAIN => return error.WouldBlock, // TODO: integrate with async instead of just returning an error .OPNOTSUPP => return error.FileLocksNotSupported, else => |err| return unexpectedErrno(err), } } } pub const RealPathError = error{ FileNotFound, AccessDenied, NameTooLong, NotSupported, NotDir, SymLinkLoop, InputOutput, FileTooBig, IsDir, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, NoDevice, SystemResources, NoSpaceLeft, FileSystem, BadPathName, DeviceBusy, SharingViolation, PipeBusy, /// On WASI, the current CWD may not be associated with an absolute path. InvalidHandle, /// On Windows, file paths must be valid Unicode. InvalidUtf8, PathAlreadyExists, } || UnexpectedError; /// Return the canonicalized absolute pathname. /// Expands all symbolic links and resolves references to `.`, `..`, and /// extra `/` characters in `pathname`. /// The return value is a slice of `out_buffer`, but not necessarily from the beginning. /// See also `realpathZ` and `realpathW`. pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { if (builtin.os.tag == .windows) { const pathname_w = try windows.sliceToPrefixedFileW(pathname); return realpathW(pathname_w.span(), out_buffer); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { var alloc = std.heap.FixedBufferAllocator.init(out_buffer); // NOTE: This emulation is incomplete. Symbolic links are not // currently expanded during path canonicalization. const paths = &.{ wasi_cwd.cwd, pathname }; return fs.path.resolve(alloc.allocator(), paths) catch error.NameTooLong; } const pathname_c = try toPosixPath(pathname); return realpathZ(&pathname_c, out_buffer); } /// Same as `realpath` except `pathname` is null-terminated. pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { if (builtin.os.tag == .windows) { const pathname_w = try windows.cStrToPrefixedFileW(pathname); return realpathW(pathname_w.span(), out_buffer); } else if (builtin.os.tag == .wasi and !builtin.link_libc) { return realpath(mem.sliceTo(pathname, 0), out_buffer); } if (!builtin.link_libc) { const flags = if (builtin.os.tag == .linux) O.PATH | O.NONBLOCK | O.CLOEXEC else O.NONBLOCK | O.CLOEXEC; const fd = openZ(pathname, flags, 0) catch |err| switch (err) { error.FileLocksNotSupported => unreachable, error.WouldBlock => unreachable, error.FileBusy => unreachable, // not asking for write permissions error.InvalidHandle => unreachable, // WASI-only else => |e| return e, }; defer close(fd); return getFdPath(fd, out_buffer); } const result_path = std.c.realpath(pathname, out_buffer) orelse switch (@intToEnum(E, std.c._errno().*)) { .SUCCESS => unreachable, .INVAL => unreachable, .BADF => unreachable, .FAULT => unreachable, .ACCES => return error.AccessDenied, .NOENT => return error.FileNotFound, .OPNOTSUPP => return error.NotSupported, .NOTDIR => return error.NotDir, .NAMETOOLONG => return error.NameTooLong, .LOOP => return error.SymLinkLoop, .IO => return error.InputOutput, else => |err| return unexpectedErrno(err), }; return mem.sliceTo(result_path, 0); } /// Same as `realpath` except `pathname` is UTF16LE-encoded. pub fn realpathW(pathname: []const u16, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { const w = windows; const dir = std.fs.cwd().fd; const access_mask = w.GENERIC_READ | w.SYNCHRONIZE; const share_access = w.FILE_SHARE_READ; const creation = w.FILE_OPEN; const h_file = blk: { const res = w.OpenFile(pathname, .{ .dir = dir, .access_mask = access_mask, .share_access = share_access, .creation = creation, .io_mode = .blocking, }) catch |err| switch (err) { error.IsDir => break :blk w.OpenFile(pathname, .{ .dir = dir, .access_mask = access_mask, .share_access = share_access, .creation = creation, .io_mode = .blocking, .filter = .dir_only, }) catch |er| switch (er) { error.WouldBlock => unreachable, else => |e2| return e2, }, error.WouldBlock => unreachable, else => |e| return e, }; break :blk res; }; defer w.CloseHandle(h_file); return getFdPath(h_file, out_buffer); } /// Return canonical path of handle `fd`. /// This function is very host-specific and is not universally supported by all hosts. /// For example, while it generally works on Linux, macOS or Windows, it is unsupported /// on FreeBSD, or WASI. pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { switch (builtin.os.tag) { .windows => { var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined; const wide_slice = try windows.GetFinalPathNameByHandle(fd, .{}, wide_buf[0..]); // Trust that Windows gives us valid UTF-16LE. const end_index = std.unicode.utf16leToUtf8(out_buffer, wide_slice) catch unreachable; return out_buffer[0..end_index]; }, .macos, .ios, .watchos, .tvos => { // On macOS, we can use F.GETPATH fcntl command to query the OS for // the path to the file descriptor. @memset(out_buffer, 0, MAX_PATH_BYTES); switch (errno(system.fcntl(fd, F.GETPATH, out_buffer))) { .SUCCESS => {}, .BADF => return error.FileNotFound, // TODO man pages for fcntl on macOS don't really tell you what // errno values to expect when command is F.GETPATH... else => |err| return unexpectedErrno(err), } const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES; return out_buffer[0..len]; }, .linux => { var procfs_buf: ["/proc/self/fd/-2147483648".len:0]u8 = undefined; const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{d}\x00", .{fd}) catch unreachable; const target = readlinkZ(std.meta.assumeSentinel(proc_path.ptr, 0), out_buffer) catch |err| { switch (err) { error.UnsupportedReparsePointType => unreachable, // Windows only, error.NotLink => unreachable, else => |e| return e, } }; return target; }, .solaris => { var procfs_buf: ["/proc/self/path/-2147483648".len:0]u8 = undefined; const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable; const target = readlinkZ(proc_path, out_buffer) catch |err| switch (err) { error.UnsupportedReparsePointType => unreachable, error.NotLink => unreachable, else => |e| return e, }; return target; }, else => @compileError("querying for canonical path of a handle is unsupported on this host"), } } /// Spurious wakeups are possible and no precision of timing is guaranteed. pub fn nanosleep(seconds: u64, nanoseconds: u64) void { var req = timespec{ .tv_sec = math.cast(isize, seconds) orelse math.maxInt(isize), .tv_nsec = math.cast(isize, nanoseconds) orelse math.maxInt(isize), }; var rem: timespec = undefined; while (true) { switch (errno(system.nanosleep(&req, &rem))) { .FAULT => unreachable, .INVAL => { // Sometimes Darwin returns EINVAL for no reason. // We treat it as a spurious wakeup. return; }, .INTR => { req = rem; continue; }, // This prong handles success as well as unexpected errors. else => return, } } } pub fn dl_iterate_phdr( context: anytype, comptime Error: type, comptime callback: fn (info: *dl_phdr_info, size: usize, context: @TypeOf(context)) Error!void, ) Error!void { const Context = @TypeOf(context); if (builtin.object_format != .elf) @compileError("dl_iterate_phdr is not available for this target"); if (builtin.link_libc) { switch (system.dl_iterate_phdr(struct { fn callbackC(info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.C) c_int { const context_ptr = @ptrCast(*const Context, @alignCast(@alignOf(*const Context), data)); callback(info, size, context_ptr.*) catch |err| return @errorToInt(err); return 0; } }.callbackC, @intToPtr(?*anyopaque, @ptrToInt(&context)))) { 0 => return, else => |err| return @errSetCast(Error, @intToError(@intCast(u16, err))), // TODO don't hardcode u16 } } const elf_base = std.process.getBaseAddress(); const ehdr = @intToPtr(*elf.Ehdr, elf_base); // Make sure the base address points to an ELF image. assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC)); const n_phdr = ehdr.e_phnum; const phdrs = (@intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff))[0..n_phdr]; var it = dl.linkmap_iterator(phdrs) catch unreachable; // The executable has no dynamic link segment, create a single entry for // the whole ELF image. if (it.end()) { // Find the base address for the ELF image, if this is a PIE the value // is non-zero. const base_address = for (phdrs) |*phdr| { if (phdr.p_type == elf.PT_PHDR) { break @ptrToInt(phdrs.ptr) - phdr.p_vaddr; // We could try computing the difference between _DYNAMIC and // the p_vaddr of the PT_DYNAMIC section, but using the phdr is // good enough (Is it?). } } else unreachable; var info = dl_phdr_info{ .dlpi_addr = base_address, .dlpi_name = "/proc/self/exe", .dlpi_phdr = phdrs.ptr, .dlpi_phnum = ehdr.e_phnum, }; return callback(&info, @sizeOf(dl_phdr_info), context); } // Last return value from the callback function. while (it.next()) |entry| { var dlpi_phdr: [*]elf.Phdr = undefined; var dlpi_phnum: u16 = undefined; if (entry.l_addr != 0) { const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); dlpi_phdr = @intToPtr([*]elf.Phdr, entry.l_addr + elf_header.e_phoff); dlpi_phnum = elf_header.e_phnum; } else { // This is the running ELF image dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff); dlpi_phnum = ehdr.e_phnum; } var info = dl_phdr_info{ .dlpi_addr = entry.l_addr, .dlpi_name = entry.l_name, .dlpi_phdr = dlpi_phdr, .dlpi_phnum = dlpi_phnum, }; try callback(&info, @sizeOf(dl_phdr_info), context); } } pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError; /// TODO: change this to return the timespec as a return value /// TODO: look into making clk_id an enum pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { var ts: timestamp_t = undefined; switch (system.clock_time_get(@bitCast(u32, clk_id), 1, &ts)) { .SUCCESS => { tp.* = .{ .tv_sec = @intCast(i64, ts / std.time.ns_per_s), .tv_nsec = @intCast(isize, ts % std.time.ns_per_s), }; }, .INVAL => return error.UnsupportedClock, else => |err| return unexpectedErrno(err), } return; } if (builtin.os.tag == .windows) { if (clk_id == CLOCK.REALTIME) { var ft: windows.FILETIME = undefined; windows.kernel32.GetSystemTimeAsFileTime(&ft); // FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch. const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime; const ft_per_s = std.time.ns_per_s / 100; tp.* = .{ .tv_sec = @intCast(i64, ft64 / ft_per_s) + std.time.epoch.windows, .tv_nsec = @intCast(c_long, ft64 % ft_per_s) * 100, }; return; } else { // TODO POSIX implementation of CLOCK.MONOTONIC on Windows. return error.UnsupportedClock; } } switch (errno(system.clock_gettime(clk_id, tp))) { .SUCCESS => return, .FAULT => unreachable, .INVAL => return error.UnsupportedClock, else => |err| return unexpectedErrno(err), } } pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { var ts: timestamp_t = undefined; switch (system.clock_res_get(@bitCast(u32, clk_id), &ts)) { .SUCCESS => res.* = .{ .tv_sec = @intCast(i64, ts / std.time.ns_per_s), .tv_nsec = @intCast(isize, ts % std.time.ns_per_s), }, .INVAL => return error.UnsupportedClock, else => |err| return unexpectedErrno(err), } return; } switch (errno(system.clock_getres(clk_id, res))) { .SUCCESS => return, .FAULT => unreachable, .INVAL => return error.UnsupportedClock, else => |err| return unexpectedErrno(err), } } pub const SchedGetAffinityError = error{PermissionDenied} || UnexpectedError; pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { var set: cpu_set_t = undefined; switch (errno(system.sched_getaffinity(pid, @sizeOf(cpu_set_t), &set))) { .SUCCESS => return set, .FAULT => unreachable, .INVAL => unreachable, .SRCH => unreachable, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![MAX_PATH_BYTES - 1:0]u8 { if (std.debug.runtime_safety) assert(std.mem.indexOfScalar(u8, file_path, 0) == null); var path_with_null: [MAX_PATH_BYTES - 1:0]u8 = undefined; // >= rather than > to make room for the null byte if (file_path.len >= MAX_PATH_BYTES) return error.NameTooLong; mem.copy(u8, &path_with_null, file_path); path_with_null[file_path.len] = 0; return path_with_null; } /// Whether or not error.Unexpected will print its value and a stack trace. /// if this happens the fix is to add the error code to the corresponding /// switch expression, possibly introduce a new error in the error set, and /// send a patch to Zig. /// The self-hosted compiler is not fully capable of handle the related code. /// Until then, unexpected error tracing is disabled for the self-hosted compiler. /// TODO remove this once self-hosted is capable enough to handle printing and /// stack trace dumping. pub const unexpected_error_tracing = builtin.zig_backend == .stage1 and builtin.mode == .Debug; pub const UnexpectedError = error{ /// The Operating System returned an undocumented error code. /// This error is in theory not possible, but it would be better /// to handle this error than to invoke undefined behavior. Unexpected, }; /// Call this when you made a syscall or something that sets errno /// and you get an unexpected error. pub fn unexpectedErrno(err: E) UnexpectedError { if (unexpected_error_tracing) { std.debug.print("unexpected errno: {d}\n", .{@enumToInt(err)}); std.debug.dumpCurrentStackTrace(null); } return error.Unexpected; } pub const SigaltstackError = error{ /// The supplied stack size was less than MINSIGSTKSZ. SizeTooSmall, /// Attempted to change the signal stack while it was active. PermissionDenied, } || UnexpectedError; pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void { switch (errno(system.sigaltstack(ss, old_ss))) { .SUCCESS => return, .FAULT => unreachable, .INVAL => unreachable, .NOMEM => return error.SizeTooSmall, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } /// Examine and change a signal action. pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) error{OperationNotSupported}!void { switch (errno(system.sigaction(sig, act, oact))) { .SUCCESS => return, .INVAL, .NOSYS => return error.OperationNotSupported, else => unreachable, } } pub const FutimensError = error{ /// times is NULL, or both tv_nsec values are UTIME_NOW, and either: /// * the effective user ID of the caller does not match the owner /// of the file, the caller does not have write access to the /// file, and the caller is not privileged (Linux: does not have /// either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability); /// or, /// * the file is marked immutable (see chattr(1)). AccessDenied, /// The caller attempted to change one or both timestamps to a value /// other than the current time, or to change one of the timestamps /// to the current time while leaving the other timestamp unchanged, /// (i.e., times is not NULL, neither tv_nsec field is UTIME_NOW, /// and neither tv_nsec field is UTIME_OMIT) and either: /// * the caller's effective user ID does not match the owner of /// file, and the caller is not privileged (Linux: does not have /// the CAP_FOWNER capability); or, /// * the file is marked append-only or immutable (see chattr(1)). PermissionDenied, ReadOnlyFileSystem, } || UnexpectedError; pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void { if (builtin.os.tag == .wasi and !builtin.link_libc) { // TODO WASI encodes `wasi.fstflags` to signify magic values // similar to UTIME_NOW and UTIME_OMIT. Currently, we ignore // this here, but we should really handle it somehow. const atim = times[0].toTimestamp(); const mtim = times[1].toTimestamp(); switch (wasi.fd_filestat_set_times(fd, atim, mtim, wasi.FILESTAT_SET_ATIM | wasi.FILESTAT_SET_MTIM)) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.PermissionDenied, .BADF => unreachable, // always a race condition .FAULT => unreachable, .INVAL => unreachable, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } switch (errno(system.futimens(fd, times))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.PermissionDenied, .BADF => unreachable, // always a race condition .FAULT => unreachable, .INVAL => unreachable, .ROFS => return error.ReadOnlyFileSystem, else => |err| return unexpectedErrno(err), } } pub const GetHostNameError = error{PermissionDenied} || UnexpectedError; pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { if (builtin.link_libc) { switch (errno(system.gethostname(name_buffer, name_buffer.len))) { .SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(name_buffer, 0), 0), .FAULT => unreachable, .NAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } if (builtin.os.tag == .linux) { const uts = uname(); const hostname = mem.sliceTo(std.meta.assumeSentinel(&uts.nodename, 0), 0); mem.copy(u8, name_buffer, hostname); return name_buffer[0..hostname.len]; } @compileError("TODO implement gethostname for this OS"); } pub fn uname() utsname { var uts: utsname = undefined; switch (errno(system.uname(&uts))) { .SUCCESS => return uts, .FAULT => unreachable, else => unreachable, } } pub fn res_mkquery( op: u4, dname: []const u8, class: u8, ty: u8, data: []const u8, newrr: ?[*]const u8, buf: []u8, ) usize { _ = data; _ = newrr; // This implementation is ported from musl libc. // A more idiomatic "ziggy" implementation would be welcome. var name = dname; if (mem.endsWith(u8, name, ".")) name.len -= 1; assert(name.len <= 253); const n = 17 + name.len + @boolToInt(name.len != 0); // Construct query template - ID will be filled later var q: [280]u8 = undefined; @memset(&q, 0, n); q[2] = @as(u8, op) * 8 + 1; q[5] = 1; mem.copy(u8, q[13..], name); var i: usize = 13; var j: usize = undefined; while (q[i] != 0) : (i = j + 1) { j = i; while (q[j] != 0 and q[j] != '.') : (j += 1) {} // TODO determine the circumstances for this and whether or // not this should be an error. if (j - i - 1 > 62) unreachable; q[i - 1] = @intCast(u8, j - i); } q[i + 1] = ty; q[i + 3] = class; // Make a reasonably unpredictable id var ts: timespec = undefined; clock_gettime(CLOCK.REALTIME, &ts) catch {}; const UInt = std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(ts.tv_nsec))); const unsec = @bitCast(UInt, ts.tv_nsec); const id = @truncate(u32, unsec + unsec / 65536); q[0] = @truncate(u8, id / 256); q[1] = @truncate(u8, id); mem.copy(u8, buf, q[0..n]); return n; } pub const SendError = error{ /// (For UNIX domain sockets, which are identified by pathname) Write permission is denied /// on the destination socket file, or search permission is denied for one of the /// directories the path prefix. (See path_resolution(7).) /// (For UDP sockets) An attempt was made to send to a network/broadcast address as though /// it was a unicast address. AccessDenied, /// The socket is marked nonblocking and the requested operation would block, and /// there is no global event loop configured. /// It's also possible to get this error under the following condition: /// (Internet domain datagram sockets) The socket referred to by sockfd had not previously /// been bound to an address and, upon attempting to bind it to an ephemeral port, it was /// determined that all port numbers in the ephemeral port range are currently in use. See /// the discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7). WouldBlock, /// Another Fast Open is already in progress. FastOpenAlreadyInProgress, /// Connection reset by peer. ConnectionResetByPeer, /// The socket type requires that message be sent atomically, and the size of the message /// to be sent made this impossible. The message is not transmitted. MessageTooBig, /// The output queue for a network interface was full. This generally indicates that the /// interface has stopped sending, but may be caused by transient congestion. (Normally, /// this does not occur in Linux. Packets are just silently dropped when a device queue /// overflows.) /// This is also caused when there is not enough kernel memory available. SystemResources, /// The local end has been shut down on a connection oriented socket. In this case, the /// process will also receive a SIGPIPE unless MSG.NOSIGNAL is set. BrokenPipe, FileDescriptorNotASocket, /// Network is unreachable. NetworkUnreachable, /// The local network interface used to reach the destination is down. NetworkSubsystemFailed, } || UnexpectedError; pub const SendMsgError = SendError || error{ /// The passed address didn't have the correct address family in its sa_family field. AddressFamilyNotSupported, /// Returned when socket is AF.UNIX and the given path has a symlink loop. SymLinkLoop, /// Returned when socket is AF.UNIX and the given path length exceeds `MAX_PATH_BYTES` bytes. NameTooLong, /// Returned when socket is AF.UNIX and the given path does not point to an existing file. FileNotFound, NotDir, /// The socket is not connected (connection-oriented sockets only). SocketNotConnected, AddressNotAvailable, }; pub fn sendmsg( /// The file descriptor of the sending socket. sockfd: socket_t, /// Message header and iovecs msg: msghdr_const, flags: u32, ) SendMsgError!usize { while (true) { const rc = system.sendmsg(sockfd, @ptrCast(*const std.x.os.Socket.Message, &msg), @intCast(c_int, flags)); if (builtin.os.tag == .windows) { if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSAEACCES => return error.AccessDenied, .WSAEADDRNOTAVAIL => return error.AddressNotAvailable, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEMSGSIZE => return error.MessageTooBig, .WSAENOBUFS => return error.SystemResources, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported, .WSAEDESTADDRREQ => unreachable, // A destination address is required. .WSAEFAULT => unreachable, // The lpBuffers, lpTo, lpOverlapped, lpNumberOfBytesSent, or lpCompletionRoutine parameters are not part of the user address space, or the lpTo parameter is too small. .WSAEHOSTUNREACH => return error.NetworkUnreachable, // TODO: WSAEINPROGRESS, WSAEINTR .WSAEINVAL => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENETRESET => return error.ConnectionResetByPeer, .WSAENETUNREACH => return error.NetworkUnreachable, .WSAENOTCONN => return error.SocketNotConnected, .WSAESHUTDOWN => unreachable, // The socket has been shut down; it is not possible to WSASendTo on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH. .WSAEWOULDBLOCK => return error.WouldBlock, .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function. else => |err| return windows.unexpectedWSAError(err), } } else { return @intCast(usize, rc); } } else { switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .ACCES => return error.AccessDenied, .AGAIN => return error.WouldBlock, .ALREADY => return error.FastOpenAlreadyInProgress, .BADF => unreachable, // always a race condition .CONNRESET => return error.ConnectionResetByPeer, .DESTADDRREQ => unreachable, // The socket is not connection-mode, and no peer address is set. .FAULT => unreachable, // An invalid user space address was specified for an argument. .INTR => continue, .INVAL => unreachable, // Invalid argument passed. .ISCONN => unreachable, // connection-mode socket was connected already but a recipient was specified .MSGSIZE => return error.MessageTooBig, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .NOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. .OPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type. .PIPE => return error.BrokenPipe, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .HOSTUNREACH => return error.NetworkUnreachable, .NETUNREACH => return error.NetworkUnreachable, .NOTCONN => return error.SocketNotConnected, .NETDOWN => return error.NetworkSubsystemFailed, else => |err| return unexpectedErrno(err), } } } } pub const SendToError = SendMsgError; /// Transmit a message to another socket. /// /// The `sendto` call may be used only when the socket is in a connected state (so that the intended /// recipient is known). The following call /// /// send(sockfd, buf, len, flags); /// /// is equivalent to /// /// sendto(sockfd, buf, len, flags, NULL, 0); /// /// If sendto() is used on a connection-mode (`SOCK.STREAM`, `SOCK.SEQPACKET`) socket, the arguments /// `dest_addr` and `addrlen` are asserted to be `null` and `0` respectively, and asserted /// that the socket was actually connected. /// Otherwise, the address of the target is given by `dest_addr` with `addrlen` specifying its size. /// /// If the message is too long to pass atomically through the underlying protocol, /// `SendError.MessageTooBig` is returned, and the message is not transmitted. /// /// There is no indication of failure to deliver. /// /// When the message does not fit into the send buffer of the socket, `sendto` normally blocks, /// unless the socket has been placed in nonblocking I/O mode. In nonblocking mode it would fail /// with `SendError.WouldBlock`. The `select` call may be used to determine when it is /// possible to send more data. pub fn sendto( /// The file descriptor of the sending socket. sockfd: socket_t, /// Message to send. buf: []const u8, flags: u32, dest_addr: ?*const sockaddr, addrlen: socklen_t, ) SendToError!usize { while (true) { const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen); if (builtin.os.tag == .windows) { if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSAEACCES => return error.AccessDenied, .WSAEADDRNOTAVAIL => return error.AddressNotAvailable, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEMSGSIZE => return error.MessageTooBig, .WSAENOBUFS => return error.SystemResources, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported, .WSAEDESTADDRREQ => unreachable, // A destination address is required. .WSAEFAULT => unreachable, // The lpBuffers, lpTo, lpOverlapped, lpNumberOfBytesSent, or lpCompletionRoutine parameters are not part of the user address space, or the lpTo parameter is too small. .WSAEHOSTUNREACH => return error.NetworkUnreachable, // TODO: WSAEINPROGRESS, WSAEINTR .WSAEINVAL => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENETRESET => return error.ConnectionResetByPeer, .WSAENETUNREACH => return error.NetworkUnreachable, .WSAENOTCONN => return error.SocketNotConnected, .WSAESHUTDOWN => unreachable, // The socket has been shut down; it is not possible to WSASendTo on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH. .WSAEWOULDBLOCK => return error.WouldBlock, .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function. else => |err| return windows.unexpectedWSAError(err), } } else { return @intCast(usize, rc); } } else { switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .ACCES => return error.AccessDenied, .AGAIN => return error.WouldBlock, .ALREADY => return error.FastOpenAlreadyInProgress, .BADF => unreachable, // always a race condition .CONNRESET => return error.ConnectionResetByPeer, .DESTADDRREQ => unreachable, // The socket is not connection-mode, and no peer address is set. .FAULT => unreachable, // An invalid user space address was specified for an argument. .INTR => continue, .INVAL => unreachable, // Invalid argument passed. .ISCONN => unreachable, // connection-mode socket was connected already but a recipient was specified .MSGSIZE => return error.MessageTooBig, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .NOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. .OPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type. .PIPE => return error.BrokenPipe, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOTDIR => return error.NotDir, .HOSTUNREACH => return error.NetworkUnreachable, .NETUNREACH => return error.NetworkUnreachable, .NOTCONN => return error.SocketNotConnected, .NETDOWN => return error.NetworkSubsystemFailed, else => |err| return unexpectedErrno(err), } } } } /// Transmit a message to another socket. /// /// The `send` call may be used only when the socket is in a connected state (so that the intended /// recipient is known). The only difference between `send` and `write` is the presence of /// flags. With a zero flags argument, `send` is equivalent to `write`. Also, the following /// call /// /// send(sockfd, buf, len, flags); /// /// is equivalent to /// /// sendto(sockfd, buf, len, flags, NULL, 0); /// /// There is no indication of failure to deliver. /// /// When the message does not fit into the send buffer of the socket, `send` normally blocks, /// unless the socket has been placed in nonblocking I/O mode. In nonblocking mode it would fail /// with `SendError.WouldBlock`. The `select` call may be used to determine when it is /// possible to send more data. pub fn send( /// The file descriptor of the sending socket. sockfd: socket_t, buf: []const u8, flags: u32, ) SendError!usize { return sendto(sockfd, buf, flags, null, 0) catch |err| switch (err) { error.AddressFamilyNotSupported => unreachable, error.SymLinkLoop => unreachable, error.NameTooLong => unreachable, error.FileNotFound => unreachable, error.NotDir => unreachable, error.NetworkUnreachable => unreachable, error.AddressNotAvailable => unreachable, error.SocketNotConnected => unreachable, else => |e| return e, }; } pub const SendFileError = PReadError || WriteError || SendError; fn count_iovec_bytes(iovs: []const iovec_const) usize { var count: usize = 0; for (iovs) |iov| { count += iov.iov_len; } return count; } /// Transfer data between file descriptors, with optional headers and trailers. /// Returns the number of bytes written, which can be zero. /// /// The `sendfile` call copies `in_len` bytes from one file descriptor to another. When possible, /// this is done within the operating system kernel, which can provide better performance /// characteristics than transferring data from kernel to user space and back, such as with /// `read` and `write` calls. When `in_len` is `0`, it means to copy until the end of the input file has been /// reached. Note, however, that partial writes are still possible in this case. /// /// `in_fd` must be a file descriptor opened for reading, and `out_fd` must be a file descriptor /// opened for writing. They may be any kind of file descriptor; however, if `in_fd` is not a regular /// file system file, it may cause this function to fall back to calling `read` and `write`, in which case /// atomicity guarantees no longer apply. /// /// Copying begins reading at `in_offset`. The input file descriptor seek position is ignored and not updated. /// If the output file descriptor has a seek position, it is updated as bytes are written. When /// `in_offset` is past the end of the input file, it successfully reads 0 bytes. /// /// `flags` has different meanings per operating system; refer to the respective man pages. /// /// These systems support atomically sending everything, including headers and trailers: /// * macOS /// * FreeBSD /// /// These systems support in-kernel data copying, but headers and trailers are not sent atomically: /// * Linux /// /// Other systems fall back to calling `read` / `write`. /// /// Linux has a limit on how many bytes may be transferred in one `sendfile` call, which is `0x7ffff000` /// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as /// well as stuffing the errno codes into the last `4096` values. This is noted on the `sendfile` man page. /// The limit on Darwin is `0x7fffffff`, trying to write more than that returns EINVAL. /// The corresponding POSIX limit on this is `math.maxInt(isize)`. pub fn sendfile( out_fd: fd_t, in_fd: fd_t, in_offset: u64, in_len: u64, headers: []const iovec_const, trailers: []const iovec_const, flags: u32, ) SendFileError!usize { var header_done = false; var total_written: usize = 0; // Prevents EOVERFLOW. const size_t = std.meta.Int(.unsigned, @typeInfo(usize).Int.bits - 1); const max_count = switch (builtin.os.tag) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(size_t), }; switch (builtin.os.tag) { .linux => sf: { // sendfile() first appeared in Linux 2.2, glibc 2.1. const call_sf = comptime if (builtin.link_libc) std.c.versionCheck(.{ .major = 2, .minor = 1 }).ok else builtin.os.version_range.linux.range.max.order(.{ .major = 2, .minor = 2 }) != .lt; if (!call_sf) break :sf; if (headers.len != 0) { const amt = try writev(out_fd, headers); total_written += amt; if (amt < count_iovec_bytes(headers)) return total_written; header_done = true; } // Here we match BSD behavior, making a zero count value send as many bytes as possible. const adjusted_count_tmp = if (in_len == 0) max_count else @minimum(in_len, @as(size_t, max_count)); // TODO we should not need this cast; improve return type of @minimum const adjusted_count = @intCast(usize, adjusted_count_tmp); const sendfile_sym = if (builtin.link_libc) system.sendfile64 else system.sendfile; while (true) { var offset: off_t = @bitCast(off_t, in_offset); const rc = sendfile_sym(out_fd, in_fd, &offset, adjusted_count); switch (errno(rc)) { .SUCCESS => { const amt = @bitCast(usize, rc); total_written += amt; if (in_len == 0 and amt == 0) { // We have detected EOF from `in_fd`. break; } else if (amt < in_len) { return total_written; } else { break; } }, .BADF => unreachable, // Always a race condition. .FAULT => unreachable, // Segmentation fault. .OVERFLOW => unreachable, // We avoid passing too large of a `count`. .NOTCONN => unreachable, // `out_fd` is an unconnected socket. .INVAL, .NOSYS => { // EINVAL could be any of the following situations: // * Descriptor is not valid or locked // * an mmap(2)-like operation is not available for in_fd // * count is negative // * out_fd has the O.APPEND flag set // Because of the "mmap(2)-like operation" possibility, we fall back to doing read/write // manually, the same as ENOSYS. break :sf; }, .AGAIN => if (std.event.Loop.instance) |loop| { loop.waitUntilFdWritable(out_fd); continue; } else { return error.WouldBlock; }, .IO => return error.InputOutput, .PIPE => return error.BrokenPipe, .NOMEM => return error.SystemResources, .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, else => |err| { unexpectedErrno(err) catch {}; break :sf; }, } } if (trailers.len != 0) { total_written += try writev(out_fd, trailers); } return total_written; }, .freebsd => sf: { var hdtr_data: std.c.sf_hdtr = undefined; var hdtr: ?*std.c.sf_hdtr = null; if (headers.len != 0 or trailers.len != 0) { // Here we carefully avoid `@intCast` by returning partial writes when // too many io vectors are provided. const hdr_cnt = math.cast(u31, headers.len) orelse math.maxInt(u31); if (headers.len > hdr_cnt) return writev(out_fd, headers); const trl_cnt = math.cast(u31, trailers.len) orelse math.maxInt(u31); hdtr_data = std.c.sf_hdtr{ .headers = headers.ptr, .hdr_cnt = hdr_cnt, .trailers = trailers.ptr, .trl_cnt = trl_cnt, }; hdtr = &hdtr_data; } const adjusted_count = @minimum(in_len, max_count); while (true) { var sbytes: off_t = undefined; const offset = @bitCast(off_t, in_offset); const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags)); const amt = @bitCast(usize, sbytes); switch (err) { .SUCCESS => return amt, .BADF => unreachable, // Always a race condition. .FAULT => unreachable, // Segmentation fault. .NOTCONN => unreachable, // `out_fd` is an unconnected socket. .INVAL, .OPNOTSUPP, .NOTSOCK, .NOSYS => { // EINVAL could be any of the following situations: // * The fd argument is not a regular file. // * The s argument is not a SOCK.STREAM type socket. // * The offset argument is negative. // Because of some of these possibilities, we fall back to doing read/write // manually, the same as ENOSYS. break :sf; }, .INTR => if (amt != 0) return amt else continue, .AGAIN => if (amt != 0) { return amt; } else if (std.event.Loop.instance) |loop| { loop.waitUntilFdWritable(out_fd); continue; } else { return error.WouldBlock; }, .BUSY => if (amt != 0) { return amt; } else if (std.event.Loop.instance) |loop| { loop.waitUntilFdReadable(in_fd); continue; } else { return error.WouldBlock; }, .IO => return error.InputOutput, .NOBUFS => return error.SystemResources, .PIPE => return error.BrokenPipe, else => { unexpectedErrno(err) catch {}; if (amt != 0) { return amt; } else { break :sf; } }, } } }, .macos, .ios, .tvos, .watchos => sf: { var hdtr_data: std.c.sf_hdtr = undefined; var hdtr: ?*std.c.sf_hdtr = null; if (headers.len != 0 or trailers.len != 0) { // Here we carefully avoid `@intCast` by returning partial writes when // too many io vectors are provided. const hdr_cnt = math.cast(u31, headers.len) orelse math.maxInt(u31); if (headers.len > hdr_cnt) return writev(out_fd, headers); const trl_cnt = math.cast(u31, trailers.len) orelse math.maxInt(u31); hdtr_data = std.c.sf_hdtr{ .headers = headers.ptr, .hdr_cnt = hdr_cnt, .trailers = trailers.ptr, .trl_cnt = trl_cnt, }; hdtr = &hdtr_data; } const adjusted_count_temporary = @minimum(in_len, @as(u63, max_count)); // TODO we should not need this int cast; improve the return type of `@minimum` const adjusted_count = @intCast(u63, adjusted_count_temporary); while (true) { var sbytes: off_t = adjusted_count; const signed_offset = @bitCast(i64, in_offset); const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags)); const amt = @bitCast(usize, sbytes); switch (err) { .SUCCESS => return amt, .BADF => unreachable, // Always a race condition. .FAULT => unreachable, // Segmentation fault. .INVAL => unreachable, .NOTCONN => unreachable, // `out_fd` is an unconnected socket. .OPNOTSUPP, .NOTSOCK, .NOSYS => break :sf, .INTR => if (amt != 0) return amt else continue, .AGAIN => if (amt != 0) { return amt; } else if (std.event.Loop.instance) |loop| { loop.waitUntilFdWritable(out_fd); continue; } else { return error.WouldBlock; }, .IO => return error.InputOutput, .PIPE => return error.BrokenPipe, else => { unexpectedErrno(err) catch {}; if (amt != 0) { return amt; } else { break :sf; } }, } } }, else => {}, // fall back to read/write } if (headers.len != 0 and !header_done) { const amt = try writev(out_fd, headers); total_written += amt; if (amt < count_iovec_bytes(headers)) return total_written; } rw: { var buf: [8 * 4096]u8 = undefined; // Here we match BSD behavior, making a zero count value send as many bytes as possible. const adjusted_count_tmp = if (in_len == 0) buf.len else @minimum(buf.len, in_len); // TODO we should not need this cast; improve return type of @minimum const adjusted_count = @intCast(usize, adjusted_count_tmp); const amt_read = try pread(in_fd, buf[0..adjusted_count], in_offset); if (amt_read == 0) { if (in_len == 0) { // We have detected EOF from `in_fd`. break :rw; } else { return total_written; } } const amt_written = try write(out_fd, buf[0..amt_read]); total_written += amt_written; if (amt_written < in_len or in_len == 0) return total_written; } if (trailers.len != 0) { total_written += try writev(out_fd, trailers); } return total_written; } pub const CopyFileRangeError = error{ FileTooBig, InputOutput, /// `fd_in` is not open for reading; or `fd_out` is not open for writing; /// or the `O.APPEND` flag is set for `fd_out`. FilesOpenedWithWrongFlags, IsDir, OutOfMemory, NoSpaceLeft, Unseekable, PermissionDenied, FileBusy, } || PReadError || PWriteError || UnexpectedError; var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true); /// Transfer data between file descriptors at specified offsets. /// Returns the number of bytes written, which can less than requested. /// /// The `copy_file_range` call copies `len` bytes from one file descriptor to another. When possible, /// this is done within the operating system kernel, which can provide better performance /// characteristics than transferring data from kernel to user space and back, such as with /// `pread` and `pwrite` calls. /// /// `fd_in` must be a file descriptor opened for reading, and `fd_out` must be a file descriptor /// opened for writing. They may be any kind of file descriptor; however, if `fd_in` is not a regular /// file system file, it may cause this function to fall back to calling `pread` and `pwrite`, in which case /// atomicity guarantees no longer apply. /// /// If `fd_in` and `fd_out` are the same, source and target ranges must not overlap. /// The file descriptor seek positions are ignored and not updated. /// When `off_in` is past the end of the input file, it successfully reads 0 bytes. /// /// `flags` has different meanings per operating system; refer to the respective man pages. /// /// These systems support in-kernel data copying: /// * Linux 4.5 (cross-filesystem 5.3) /// /// Other systems fall back to calling `pread` / `pwrite`. /// /// Maximum offsets on Linux are `math.maxInt(i64)`. pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize { const call_cfr = comptime if (builtin.os.tag == .wasi) // WASI-libc doesn't have copy_file_range. false else if (builtin.link_libc) std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok else builtin.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true; if (call_cfr and has_copy_file_range_syscall.load(.Monotonic)) { var off_in_copy = @bitCast(i64, off_in); var off_out_copy = @bitCast(i64, off_out); const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags); switch (system.getErrno(rc)) { .SUCCESS => return @intCast(usize, rc), .BADF => return error.FilesOpenedWithWrongFlags, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOMEM => return error.OutOfMemory, .NOSPC => return error.NoSpaceLeft, .OVERFLOW => return error.Unseekable, .PERM => return error.PermissionDenied, .TXTBSY => return error.FileBusy, // these may not be regular files, try fallback .INVAL => {}, // support for cross-filesystem copy added in Linux 5.3, use fallback .XDEV => {}, // syscall added in Linux 4.5, use fallback .NOSYS => { has_copy_file_range_syscall.store(false, .Monotonic); }, else => |err| return unexpectedErrno(err), } } var buf: [8 * 4096]u8 = undefined; const adjusted_count = @minimum(buf.len, len); const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in); // TODO without @as the line below fails to compile for wasm32-wasi: // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize' if (amt_read == 0) return @as(usize, 0); return pwrite(fd_out, buf[0..amt_read], off_out); } pub const PollError = error{ /// The network subsystem has failed. NetworkSubsystemFailed, /// The kernel had no space to allocate file descriptor tables. SystemResources, } || UnexpectedError; pub fn poll(fds: []pollfd, timeout: i32) PollError!usize { while (true) { const fds_count = math.cast(nfds_t, fds.len) orelse return error.SystemResources; const rc = system.poll(fds.ptr, fds_count, timeout); if (builtin.os.tag == .windows) { if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOBUFS => return error.SystemResources, // TODO: handle more errors else => |err| return windows.unexpectedWSAError(err), } } else { return @intCast(usize, rc); } } else { switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .FAULT => unreachable, .INTR => continue, .INVAL => unreachable, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } unreachable; } } pub const PPollError = error{ /// The operation was interrupted by a delivery of a signal before it could complete. SignalInterrupt, /// The kernel had no space to allocate file descriptor tables. SystemResources, } || UnexpectedError; pub fn ppoll(fds: []pollfd, timeout: ?*const timespec, mask: ?*const sigset_t) PPollError!usize { var ts: timespec = undefined; var ts_ptr: ?*timespec = null; if (timeout) |timeout_ns| { ts_ptr = &ts; ts = timeout_ns.*; } const fds_count = math.cast(nfds_t, fds.len) orelse return error.SystemResources; const rc = system.ppoll(fds.ptr, fds_count, ts_ptr, mask); switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .FAULT => unreachable, .INTR => return error.SignalInterrupt, .INVAL => unreachable, .NOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), } } pub const RecvFromError = error{ /// The socket is marked nonblocking and the requested operation would block, and /// there is no global event loop configured. WouldBlock, /// A remote host refused to allow the network connection, typically because it is not /// running the requested service. ConnectionRefused, /// Could not allocate kernel memory. SystemResources, ConnectionResetByPeer, /// The socket has not been bound. SocketNotBound, /// The UDP message was too big for the buffer and part of it has been discarded MessageTooBig, /// The network subsystem has failed. NetworkSubsystemFailed, /// The socket is not connected (connection-oriented sockets only). SocketNotConnected, } || UnexpectedError; pub fn recv(sock: socket_t, buf: []u8, flags: u32) RecvFromError!usize { return recvfrom(sock, buf, flags, null, null); } /// If `sockfd` is opened in non blocking mode, the function will /// return error.WouldBlock when EAGAIN is received. pub fn recvfrom( sockfd: socket_t, buf: []u8, flags: u32, src_addr: ?*sockaddr, addrlen: ?*socklen_t, ) RecvFromError!usize { while (true) { const rc = system.recvfrom(sockfd, buf.ptr, buf.len, flags, src_addr, addrlen); if (builtin.os.tag == .windows) { if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEINVAL => return error.SocketNotBound, .WSAEMSGSIZE => return error.MessageTooBig, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOTCONN => return error.SocketNotConnected, .WSAEWOULDBLOCK => return error.WouldBlock, // TODO: handle more errors else => |err| return windows.unexpectedWSAError(err), } } else { return @intCast(usize, rc); } } else { switch (errno(rc)) { .SUCCESS => return @intCast(usize, rc), .BADF => unreachable, // always a race condition .FAULT => unreachable, .INVAL => unreachable, .NOTCONN => unreachable, .NOTSOCK => unreachable, .INTR => continue, .AGAIN => return error.WouldBlock, .NOMEM => return error.SystemResources, .CONNREFUSED => return error.ConnectionRefused, .CONNRESET => return error.ConnectionResetByPeer, else => |err| return unexpectedErrno(err), } } } } pub const DnExpandError = error{InvalidDnsPacket}; pub fn dn_expand( msg: []const u8, comp_dn: []const u8, exp_dn: []u8, ) DnExpandError!usize { // This implementation is ported from musl libc. // A more idiomatic "ziggy" implementation would be welcome. var p = comp_dn.ptr; var len: usize = std.math.maxInt(usize); const end = msg.ptr + msg.len; if (p == end or exp_dn.len == 0) return error.InvalidDnsPacket; var dest = exp_dn.ptr; const dend = dest + @minimum(exp_dn.len, 254); // detect reference loop using an iteration counter var i: usize = 0; while (i < msg.len) : (i += 2) { // loop invariants: p<end, dest<dend if ((p[0] & 0xc0) != 0) { if (p + 1 == end) return error.InvalidDnsPacket; var j = ((p[0] & @as(usize, 0x3f)) << 8) | p[1]; if (len == std.math.maxInt(usize)) len = @ptrToInt(p) + 2 - @ptrToInt(comp_dn.ptr); if (j >= msg.len) return error.InvalidDnsPacket; p = msg.ptr + j; } else if (p[0] != 0) { if (dest != exp_dn.ptr) { dest.* = '.'; dest += 1; } var j = p[0]; p += 1; if (j >= @ptrToInt(end) - @ptrToInt(p) or j >= @ptrToInt(dend) - @ptrToInt(dest)) { return error.InvalidDnsPacket; } while (j != 0) { j -= 1; dest.* = p[0]; dest += 1; p += 1; } } else { dest.* = 0; if (len == std.math.maxInt(usize)) len = @ptrToInt(p) + 1 - @ptrToInt(comp_dn.ptr); return len; } } return error.InvalidDnsPacket; } pub const SetSockOptError = error{ /// The socket is already connected, and a specified option cannot be set while the socket is connected. AlreadyConnected, /// The option is not supported by the protocol. InvalidProtocolOption, /// The send and receive timeout values are too big to fit into the timeout fields in the socket structure. TimeoutTooBig, /// Insufficient resources are available in the system to complete the call. SystemResources, // Setting the socket option requires more elevated permissions. PermissionDenied, NetworkSubsystemFailed, FileDescriptorNotASocket, SocketNotBound, } || UnexpectedError; /// Set a socket's options. pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void { if (builtin.os.tag == .windows) { const rc = windows.ws2_32.setsockopt(fd, @intCast(i32, level), @intCast(i32, optname), opt.ptr, @intCast(i32, opt.len)); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEFAULT => unreachable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotBound, else => |err| return windows.unexpectedWSAError(err), } } return; } else { switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(socklen_t, opt.len)))) { .SUCCESS => {}, .BADF => unreachable, // always a race condition .NOTSOCK => unreachable, // always a race condition .INVAL => unreachable, .FAULT => unreachable, .DOM => return error.TimeoutTooBig, .ISCONN => return error.AlreadyConnected, .NOPROTOOPT => return error.InvalidProtocolOption, .NOMEM => return error.SystemResources, .NOBUFS => return error.SystemResources, .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } } pub const MemFdCreateError = error{ SystemFdQuotaExceeded, ProcessFdQuotaExceeded, OutOfMemory, /// memfd_create is available in Linux 3.17 and later. This error is returned /// for older kernel versions. SystemOutdated, } || UnexpectedError; pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t { switch (builtin.os.tag) { .linux => { // memfd_create is available only in glibc versions starting with 2.27. const use_c = std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok; const sys = if (use_c) std.c else linux; const getErrno = if (use_c) std.c.getErrno else linux.getErrno; const rc = sys.memfd_create(name, flags); switch (getErrno(rc)) { .SUCCESS => return @intCast(fd_t, rc), .FAULT => unreachable, // name has invalid memory .INVAL => unreachable, // name/flags are faulty .NFILE => return error.SystemFdQuotaExceeded, .MFILE => return error.ProcessFdQuotaExceeded, .NOMEM => return error.OutOfMemory, .NOSYS => return error.SystemOutdated, else => |err| return unexpectedErrno(err), } }, .freebsd => { const rc = system.memfd_create(name, flags); switch (errno(rc)) { .SUCCESS => return rc, .BADF => unreachable, // name argument NULL .INVAL => unreachable, // name too long or invalid/unsupported flags. .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOSYS => return error.SystemOutdated, else => |err| return unexpectedErrno(err), } }, else => @compileError("target OS does not support memfd_create()"), } } pub const MFD_NAME_PREFIX = "memfd:"; pub const MFD_MAX_NAME_LEN = NAME_MAX - MFD_NAME_PREFIX.len; fn toMemFdPath(name: []const u8) ![MFD_MAX_NAME_LEN:0]u8 { var path_with_null: [MFD_MAX_NAME_LEN:0]u8 = undefined; // >= rather than > to make room for the null byte if (name.len >= MFD_MAX_NAME_LEN) return error.NameTooLong; mem.copy(u8, &path_with_null, name); path_with_null[name.len] = 0; return path_with_null; } pub fn memfd_create(name: []const u8, flags: u32) !fd_t { const name_t = try toMemFdPath(name); return memfd_createZ(&name_t, flags); } pub fn getrusage(who: i32) rusage { var result: rusage = undefined; const rc = system.getrusage(who, &result); switch (errno(rc)) { .SUCCESS => return result, .INVAL => unreachable, .FAULT => unreachable, else => unreachable, } } pub const TermiosGetError = error{NotATerminal} || UnexpectedError; pub fn tcgetattr(handle: fd_t) TermiosGetError!termios { while (true) { var term: termios = undefined; switch (errno(system.tcgetattr(handle, &term))) { .SUCCESS => return term, .INTR => continue, .BADF => unreachable, .NOTTY => return error.NotATerminal, else => |err| return unexpectedErrno(err), } } } pub const TermiosSetError = TermiosGetError || error{ProcessOrphaned}; pub fn tcsetattr(handle: fd_t, optional_action: TCSA, termios_p: termios) TermiosSetError!void { while (true) { switch (errno(system.tcsetattr(handle, optional_action, &termios_p))) { .SUCCESS => return, .BADF => unreachable, .INTR => continue, .INVAL => unreachable, .NOTTY => return error.NotATerminal, .IO => return error.ProcessOrphaned, else => |err| return unexpectedErrno(err), } } } pub const IoCtl_SIOCGIFINDEX_Error = error{ FileSystem, InterfaceNotFound, } || UnexpectedError; pub fn ioctl_SIOCGIFINDEX(fd: fd_t, ifr: *ifreq) IoCtl_SIOCGIFINDEX_Error!void { while (true) { switch (errno(system.ioctl(fd, SIOCGIFINDEX, @ptrToInt(ifr)))) { .SUCCESS => return, .INVAL => unreachable, // Bad parameters. .NOTTY => unreachable, .NXIO => unreachable, .BADF => unreachable, // Always a race condition. .FAULT => unreachable, // Bad pointer parameter. .INTR => continue, .IO => return error.FileSystem, .NODEV => return error.InterfaceNotFound, else => |err| return unexpectedErrno(err), } } } pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t { const rc = system.signalfd(fd, mask, flags); switch (errno(rc)) { .SUCCESS => return @intCast(fd_t, rc), .BADF, .INVAL => unreachable, .NFILE => return error.SystemFdQuotaExceeded, .NOMEM => return error.SystemResources, .MFILE => return error.ProcessResources, .NODEV => return error.InodeMountFail, .NOSYS => return error.SystemOutdated, else => |err| return unexpectedErrno(err), } } pub const SyncError = error{ InputOutput, NoSpaceLeft, DiskQuota, AccessDenied, } || UnexpectedError; /// Write all pending file contents and metadata modifications to all filesystems. pub fn sync() void { system.sync(); } /// Write all pending file contents and metadata modifications to the filesystem which contains the specified file. pub fn syncfs(fd: fd_t) SyncError!void { const rc = system.syncfs(fd); switch (errno(rc)) { .SUCCESS => return, .BADF, .INVAL, .ROFS => unreachable, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .DQUOT => return error.DiskQuota, else => |err| return unexpectedErrno(err), } } /// Write all pending file contents and metadata modifications for the specified file descriptor to the underlying filesystem. pub fn fsync(fd: fd_t) SyncError!void { if (builtin.os.tag == .windows) { if (windows.kernel32.FlushFileBuffers(fd) != 0) return; switch (windows.kernel32.GetLastError()) { .SUCCESS => return, .INVALID_HANDLE => unreachable, .ACCESS_DENIED => return error.AccessDenied, // a sync was performed but the system couldn't update the access time .UNEXP_NET_ERR => return error.InputOutput, else => return error.InputOutput, } } const rc = system.fsync(fd); switch (errno(rc)) { .SUCCESS => return, .BADF, .INVAL, .ROFS => unreachable, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .DQUOT => return error.DiskQuota, else => |err| return unexpectedErrno(err), } } /// Write all pending file contents for the specified file descriptor to the underlying filesystem, but not necessarily the metadata. pub fn fdatasync(fd: fd_t) SyncError!void { if (builtin.os.tag == .windows) { return fsync(fd) catch |err| switch (err) { SyncError.AccessDenied => return, // fdatasync doesn't promise that the access time was synced else => return err, }; } const rc = system.fdatasync(fd); switch (errno(rc)) { .SUCCESS => return, .BADF, .INVAL, .ROFS => unreachable, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .DQUOT => return error.DiskQuota, else => |err| return unexpectedErrno(err), } } pub const PrctlError = error{ /// Can only occur with PR_SET_SECCOMP/SECCOMP_MODE_FILTER or /// PR_SET_MM/PR_SET_MM_EXE_FILE AccessDenied, /// Can only occur with PR_SET_MM/PR_SET_MM_EXE_FILE InvalidFileDescriptor, InvalidAddress, /// Can only occur with PR_SET_SPECULATION_CTRL, PR_MPX_ENABLE_MANAGEMENT, /// or PR_MPX_DISABLE_MANAGEMENT UnsupportedFeature, /// Can only occur wih PR_SET_FP_MODE OperationNotSupported, PermissionDenied, } || UnexpectedError; pub fn prctl(option: PR, args: anytype) PrctlError!u31 { if (@typeInfo(@TypeOf(args)) != .Struct) @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args))); if (args.len > 4) @compileError("prctl takes a maximum of 4 optional arguments"); var buf: [4]usize = undefined; { comptime var i = 0; inline while (i < args.len) : (i += 1) buf[i] = args[i]; } const rc = system.prctl(@enumToInt(option), buf[0], buf[1], buf[2], buf[3]); switch (errno(rc)) { .SUCCESS => return @intCast(u31, rc), .ACCES => return error.AccessDenied, .BADF => return error.InvalidFileDescriptor, .FAULT => return error.InvalidAddress, .INVAL => unreachable, .NODEV, .NXIO => return error.UnsupportedFeature, .OPNOTSUPP => return error.OperationNotSupported, .PERM, .BUSY => return error.PermissionDenied, .RANGE => unreachable, else => |err| return unexpectedErrno(err), } } pub const GetrlimitError = UnexpectedError; pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit { const getrlimit_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.getrlimit64 else system.getrlimit; var limits: rlimit = undefined; switch (errno(getrlimit_sym(resource, &limits))) { .SUCCESS => return limits, .FAULT => unreachable, // bogus pointer .INVAL => unreachable, else => |err| return unexpectedErrno(err), } } pub const SetrlimitError = error{ PermissionDenied, LimitTooBig } || UnexpectedError; pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void { const setrlimit_sym = if (builtin.os.tag == .linux and builtin.link_libc) system.setrlimit64 else system.setrlimit; switch (errno(setrlimit_sym(resource, &limits))) { .SUCCESS => return, .FAULT => unreachable, // bogus pointer .INVAL => return error.LimitTooBig, // this could also mean "invalid resource", but that would be unreachable .PERM => return error.PermissionDenied, else => |err| return unexpectedErrno(err), } } pub const MadviseError = error{ /// advice is MADV.REMOVE, but the specified address range is not a shared writable mapping. AccessDenied, /// advice is MADV.HWPOISON, but the caller does not have the CAP_SYS_ADMIN capability. PermissionDenied, /// A kernel resource was temporarily unavailable. SystemResources, /// One of the following: /// * addr is not page-aligned or length is negative /// * advice is not valid /// * advice is MADV.DONTNEED or MADV.REMOVE and the specified address range /// includes locked, Huge TLB pages, or VM_PFNMAP pages. /// * advice is MADV.MERGEABLE or MADV.UNMERGEABLE, but the kernel was not /// configured with CONFIG_KSM. /// * advice is MADV.FREE or MADV.WIPEONFORK but the specified address range /// includes file, Huge TLB, MAP.SHARED, or VM_PFNMAP ranges. InvalidSyscall, /// (for MADV.WILLNEED) Paging in this area would exceed the process's /// maximum resident set size. WouldExceedMaximumResidentSetSize, /// One of the following: /// * (for MADV.WILLNEED) Not enough memory: paging in failed. /// * Addresses in the specified range are not currently mapped, or /// are outside the address space of the process. OutOfMemory, /// The madvise syscall is not available on this version and configuration /// of the Linux kernel. MadviseUnavailable, /// The operating system returned an undocumented error code. Unexpected, }; /// Give advice about use of memory. /// This syscall is optional and is sometimes configured to be disabled. pub fn madvise(ptr: [*]align(mem.page_size) u8, length: usize, advice: u32) MadviseError!void { switch (errno(system.madvise(ptr, length, advice))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .AGAIN => return error.SystemResources, .BADF => unreachable, // The map exists, but the area maps something that isn't a file. .INVAL => return error.InvalidSyscall, .IO => return error.WouldExceedMaximumResidentSetSize, .NOMEM => return error.OutOfMemory, .NOSYS => return error.MadviseUnavailable, else => |err| return unexpectedErrno(err), } } pub const PerfEventOpenError = error{ /// Returned if the perf_event_attr size value is too small (smaller /// than PERF_ATTR_SIZE_VER0), too big (larger than the page size), /// or larger than the kernel supports and the extra bytes are not /// zero. When E2BIG is returned, the perf_event_attr size field is /// overwritten by the kernel to be the size of the structure it was /// expecting. TooBig, /// Returned when the requested event requires CAP_SYS_ADMIN permis‐ /// sions (or a more permissive perf_event paranoid setting). Some /// common cases where an unprivileged process may encounter this /// error: attaching to a process owned by a different user; moni‐ /// toring all processes on a given CPU (i.e., specifying the pid /// argument as -1); and not setting exclude_kernel when the para‐ /// noid setting requires it. /// Also: /// Returned on many (but not all) architectures when an unsupported /// exclude_hv, exclude_idle, exclude_user, or exclude_kernel set‐ /// ting is specified. /// It can also happen, as with EACCES, when the requested event re‐ /// quires CAP_SYS_ADMIN permissions (or a more permissive /// perf_event paranoid setting). This includes setting a break‐ /// point on a kernel address, and (since Linux 3.13) setting a ker‐ /// nel function-trace tracepoint. PermissionDenied, /// Returned if another event already has exclusive access to the /// PMU. DeviceBusy, /// Each opened event uses one file descriptor. If a large number /// of events are opened, the per-process limit on the number of /// open file descriptors will be reached, and no more events can be /// created. ProcessResources, EventRequiresUnsupportedCpuFeature, /// Returned if you try to add more breakpoint /// events than supported by the hardware. TooManyBreakpoints, /// Returned if PERF_SAMPLE_STACK_USER is set in sample_type and it /// is not supported by hardware. SampleStackNotSupported, /// Returned if an event requiring a specific hardware feature is /// requested but there is no hardware support. This includes re‐ /// questing low-skid events if not supported, branch tracing if it /// is not available, sampling if no PMU interrupt is available, and /// branch stacks for software events. EventNotSupported, /// Returned if PERF_SAMPLE_CALLCHAIN is requested and sam‐ /// ple_max_stack is larger than the maximum specified in /// /proc/sys/kernel/perf_event_max_stack. SampleMaxStackOverflow, /// Returned if attempting to attach to a process that does not exist. ProcessNotFound, } || UnexpectedError; pub fn perf_event_open( attr: *linux.perf_event_attr, pid: pid_t, cpu: i32, group_fd: fd_t, flags: usize, ) PerfEventOpenError!fd_t { const rc = system.perf_event_open(attr, pid, cpu, group_fd, flags); switch (errno(rc)) { .SUCCESS => return @intCast(fd_t, rc), .@"2BIG" => return error.TooBig, .ACCES => return error.PermissionDenied, .BADF => unreachable, // group_fd file descriptor is not valid. .BUSY => return error.DeviceBusy, .FAULT => unreachable, // Segmentation fault. .INVAL => unreachable, // Bad attr settings. .INTR => unreachable, // Mixed perf and ftrace handling for a uprobe. .MFILE => return error.ProcessResources, .NODEV => return error.EventRequiresUnsupportedCpuFeature, .NOENT => unreachable, // Invalid type setting. .NOSPC => return error.TooManyBreakpoints, .NOSYS => return error.SampleStackNotSupported, .OPNOTSUPP => return error.EventNotSupported, .OVERFLOW => return error.SampleMaxStackOverflow, .PERM => return error.PermissionDenied, .SRCH => return error.ProcessNotFound, else => |err| return unexpectedErrno(err), } } pub const TimerFdCreateError = error{ AccessDenied, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, NoDevice, SystemResources, } || UnexpectedError; pub const TimerFdGetError = error{InvalidHandle} || UnexpectedError; pub const TimerFdSetError = TimerFdGetError || error{Canceled}; pub fn timerfd_create(clokid: i32, flags: u32) TimerFdCreateError!fd_t { var rc = linux.timerfd_create(clokid, flags); return switch (errno(rc)) { .SUCCESS => @intCast(fd_t, rc), .INVAL => unreachable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NODEV => return error.NoDevice, .NOMEM => return error.SystemResources, .PERM => return error.AccessDenied, else => |err| return unexpectedErrno(err), }; } pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const linux.itimerspec, old_value: ?*linux.itimerspec) TimerFdSetError!void { var rc = linux.timerfd_settime(fd, flags, new_value, old_value); return switch (errno(rc)) { .SUCCESS => {}, .BADF => error.InvalidHandle, .FAULT => unreachable, .INVAL => unreachable, .CANCELED => error.Canceled, else => |err| return unexpectedErrno(err), }; } pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec { var curr_value: linux.itimerspec = undefined; var rc = linux.timerfd_gettime(fd, &curr_value); return switch (errno(rc)) { .SUCCESS => return curr_value, .BADF => error.InvalidHandle, .FAULT => unreachable, .INVAL => unreachable, else => |err| return unexpectedErrno(err), }; }
const std = @import("../../std.zig"); const net = @import("net.zig"); const os = std.os; const mem = std.mem; const windows = std.os.windows; const ws2_32 = windows.ws2_32; pub fn Mixin(comptime Socket: type) type { return struct { /// Open a new socket. pub fn init(domain: u32, socket_type: u32, protocol: u32, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket { var raw_flags: u32 = ws2_32.WSA_FLAG_OVERLAPPED; const set = std.EnumSet(Socket.InitFlags).init(flags); if (set.contains(.close_on_exec)) raw_flags |= ws2_32.WSA_FLAG_NO_HANDLE_INHERIT; const fd = ws2_32.WSASocketW( @intCast(i32, domain), @intCast(i32, socket_type), @intCast(i32, protocol), null, 0, raw_flags, ); if (fd == ws2_32.INVALID_SOCKET) { return switch (ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => { _ = try windows.WSAStartup(2, 2); return Socket.init(domain, socket_type, protocol, flags); }, .WSAEAFNOSUPPORT => error.AddressFamilyNotSupported, .WSAEMFILE => error.ProcessFdQuotaExceeded, .WSAENOBUFS => error.SystemResources, .WSAEPROTONOSUPPORT => error.ProtocolNotSupported, else => |err| windows.unexpectedWSAError(err), }; } if (set.contains(.nonblocking)) { var enabled: c_ulong = 1; const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled); if (rc == ws2_32.SOCKET_ERROR) { return windows.unexpectedWSAError(ws2_32.WSAGetLastError()); } } return Socket{ .fd = fd }; } /// Closes the socket. pub fn deinit(self: Socket) void { _ = ws2_32.closesocket(self.fd); } /// Shutdown either the read side, write side, or all side of the socket. pub fn shutdown(self: Socket, how: os.ShutdownHow) !void { const rc = ws2_32.shutdown(self.fd, switch (how) { .recv => ws2_32.SD_RECEIVE, .send => ws2_32.SD_SEND, .both => ws2_32.SD_BOTH, }); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAECONNABORTED => return error.ConnectionAborted, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEINPROGRESS => return error.BlockingOperationInProgress, .WSAEINVAL => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOTCONN => return error.SocketNotConnected, .WSAENOTSOCK => unreachable, .WSANOTINITIALISED => unreachable, else => |err| return windows.unexpectedWSAError(err), }; } } /// Binds the socket to an address. pub fn bind(self: Socket, address: Socket.Address) !void { const rc = ws2_32.bind(self.fd, @ptrCast(*const ws2_32.sockaddr, &address.toNative()), @intCast(c_int, address.getNativeSize())); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAEACCES => error.AccessDenied, .WSAEADDRINUSE => error.AddressInUse, .WSAEADDRNOTAVAIL => error.AddressNotAvailable, .WSAEFAULT => error.BadAddress, .WSAEINPROGRESS => error.WouldBlock, .WSAEINVAL => error.AlreadyBound, .WSAENOBUFS => error.NoEphemeralPortsAvailable, .WSAENOTSOCK => error.NotASocket, else => |err| windows.unexpectedWSAError(err), }; } } /// Start listening for incoming connections on the socket. pub fn listen(self: Socket, max_backlog_size: u31) !void { const rc = ws2_32.listen(self.fd, max_backlog_size); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAEADDRINUSE => error.AddressInUse, .WSAEISCONN => error.AlreadyConnected, .WSAEINVAL => error.SocketNotBound, .WSAEMFILE, .WSAENOBUFS => error.SystemResources, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => error.OperationNotSupported, .WSAEINPROGRESS => error.WouldBlock, else => |err| windows.unexpectedWSAError(err), }; } } /// Have the socket attempt to the connect to an address. pub fn connect(self: Socket, address: Socket.Address) !void { const rc = ws2_32.connect(self.fd, @ptrCast(*const ws2_32.sockaddr, &address.toNative()), @intCast(c_int, address.getNativeSize())); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAEADDRINUSE => error.AddressInUse, .WSAEADDRNOTAVAIL => error.AddressNotAvailable, .WSAECONNREFUSED => error.ConnectionRefused, .WSAETIMEDOUT => error.ConnectionTimedOut, .WSAEFAULT => error.BadAddress, .WSAEINVAL => error.ListeningSocket, .WSAEISCONN => error.AlreadyConnected, .WSAENOTSOCK => error.NotASocket, .WSAEACCES => error.BroadcastNotEnabled, .WSAENOBUFS => error.SystemResources, .WSAEAFNOSUPPORT => error.AddressFamilyNotSupported, .WSAEINPROGRESS, .WSAEWOULDBLOCK => error.WouldBlock, .WSAEHOSTUNREACH, .WSAENETUNREACH => error.NetworkUnreachable, else => |err| windows.unexpectedWSAError(err), }; } } /// Accept a pending incoming connection queued to the kernel backlog /// of the socket. pub fn accept(self: Socket, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket.Connection { var address: Socket.Address.Native.Storage = undefined; var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); const fd = ws2_32.accept(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); if (fd == ws2_32.INVALID_SOCKET) { return switch (ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAECONNRESET => error.ConnectionResetByPeer, .WSAEFAULT => unreachable, .WSAEINVAL => error.SocketNotListening, .WSAEMFILE => error.ProcessFdQuotaExceeded, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENOBUFS => error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => error.OperationNotSupported, .WSAEWOULDBLOCK => error.WouldBlock, else => |err| windows.unexpectedWSAError(err), }; } const socket = Socket.from(fd); errdefer socket.deinit(); const socket_address = Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address)); const set = std.EnumSet(Socket.InitFlags).init(flags); if (set.contains(.nonblocking)) { var enabled: c_ulong = 1; const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled); if (rc == ws2_32.SOCKET_ERROR) { return windows.unexpectedWSAError(ws2_32.WSAGetLastError()); } } return Socket.Connection.from(socket, socket_address); } /// Read data from the socket into the buffer provided with a set of flags /// specified. It returns the number of bytes read into the buffer provided. pub fn read(self: Socket, buf: []u8, flags: u32) !usize { var bufs = &[_]ws2_32.WSABUF{.{ .len = @intCast(u32, buf.len), .buf = buf.ptr }}; var num_bytes: u32 = undefined; var flags_ = flags; const rc = ws2_32.WSARecv(self.fd, bufs, 1, &num_bytes, &flags_, null, null); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAECONNABORTED => error.ConnectionAborted, .WSAECONNRESET => error.ConnectionResetByPeer, .WSAEDISCON => error.ConnectionClosedByPeer, .WSAEFAULT => error.BadBuffer, .WSAEINPROGRESS, .WSAEWOULDBLOCK, .WSA_IO_PENDING, .WSAETIMEDOUT, => error.WouldBlock, .WSAEINTR => error.Cancelled, .WSAEINVAL => error.SocketNotBound, .WSAEMSGSIZE => error.MessageTooLarge, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENETRESET => error.NetworkReset, .WSAENOTCONN => error.SocketNotConnected, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => error.OperationNotSupported, .WSAESHUTDOWN => error.AlreadyShutdown, .WSA_OPERATION_ABORTED => error.OperationAborted, else => |err| windows.unexpectedWSAError(err), }; } return @intCast(usize, num_bytes); } /// Write a buffer of data provided to the socket with a set of flags specified. /// It returns the number of bytes that are written to the socket. pub fn write(self: Socket, buf: []const u8, flags: u32) !usize { var bufs = &[_]ws2_32.WSABUF{.{ .len = @intCast(u32, buf.len), .buf = @intToPtr([*]u8, @ptrToInt(buf.ptr)) }}; var num_bytes: u32 = undefined; const rc = ws2_32.WSASend(self.fd, bufs, 1, &num_bytes, flags, null, null); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAECONNABORTED => error.ConnectionAborted, .WSAECONNRESET => error.ConnectionResetByPeer, .WSAEFAULT => error.BadBuffer, .WSAEINPROGRESS, .WSAEWOULDBLOCK, .WSA_IO_PENDING, .WSAETIMEDOUT, => error.WouldBlock, .WSAEINTR => error.Cancelled, .WSAEINVAL => error.SocketNotBound, .WSAEMSGSIZE => error.MessageTooLarge, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENETRESET => error.NetworkReset, .WSAENOBUFS => error.BufferDeadlock, .WSAENOTCONN => error.SocketNotConnected, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => error.OperationNotSupported, .WSAESHUTDOWN => error.AlreadyShutdown, .WSA_OPERATION_ABORTED => error.OperationAborted, else => |err| windows.unexpectedWSAError(err), }; } return @intCast(usize, num_bytes); } /// Writes multiple I/O vectors with a prepended message header to the socket /// with a set of flags specified. It returns the number of bytes that are /// written to the socket. pub fn writeMessage(self: Socket, msg: Socket.Message, flags: u32) !usize { const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSASENDMSG, self.fd, ws2_32.WSAID_WSASENDMSG); var num_bytes: u32 = undefined; const rc = call(self.fd, &msg, flags, &num_bytes, null, null); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAECONNABORTED => error.ConnectionAborted, .WSAECONNRESET => error.ConnectionResetByPeer, .WSAEFAULT => error.BadBuffer, .WSAEINPROGRESS, .WSAEWOULDBLOCK, .WSA_IO_PENDING, .WSAETIMEDOUT, => error.WouldBlock, .WSAEINTR => error.Cancelled, .WSAEINVAL => error.SocketNotBound, .WSAEMSGSIZE => error.MessageTooLarge, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENETRESET => error.NetworkReset, .WSAENOBUFS => error.BufferDeadlock, .WSAENOTCONN => error.SocketNotConnected, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => error.OperationNotSupported, .WSAESHUTDOWN => error.AlreadyShutdown, .WSA_OPERATION_ABORTED => error.OperationAborted, else => |err| windows.unexpectedWSAError(err), }; } return @intCast(usize, num_bytes); } /// Read multiple I/O vectors with a prepended message header from the socket /// with a set of flags specified. It returns the number of bytes that were /// read into the buffer provided. pub fn readMessage(self: Socket, msg: *Socket.Message, flags: u32) !usize { _ = flags; const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSARECVMSG, self.fd, ws2_32.WSAID_WSARECVMSG); var num_bytes: u32 = undefined; const rc = call(self.fd, msg, &num_bytes, null, null); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSAECONNABORTED => error.ConnectionAborted, .WSAECONNRESET => error.ConnectionResetByPeer, .WSAEDISCON => error.ConnectionClosedByPeer, .WSAEFAULT => error.BadBuffer, .WSAEINPROGRESS, .WSAEWOULDBLOCK, .WSA_IO_PENDING, .WSAETIMEDOUT, => error.WouldBlock, .WSAEINTR => error.Cancelled, .WSAEINVAL => error.SocketNotBound, .WSAEMSGSIZE => error.MessageTooLarge, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENETRESET => error.NetworkReset, .WSAENOTCONN => error.SocketNotConnected, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => error.OperationNotSupported, .WSAESHUTDOWN => error.AlreadyShutdown, .WSA_OPERATION_ABORTED => error.OperationAborted, else => |err| windows.unexpectedWSAError(err), }; } return @intCast(usize, num_bytes); } /// Query the address that the socket is locally bounded to. pub fn getLocalAddress(self: Socket) !Socket.Address { var address: Socket.Address.Native.Storage = undefined; var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); const rc = ws2_32.getsockname(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAEFAULT => unreachable, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEINVAL => error.SocketNotBound, else => |err| windows.unexpectedWSAError(err), }; } return Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address)); } /// Query the address that the socket is connected to. pub fn getRemoteAddress(self: Socket) !Socket.Address { var address: Socket.Address.Native.Storage = undefined; var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); const rc = ws2_32.getpeername(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAEFAULT => unreachable, .WSAENETDOWN => error.NetworkSubsystemFailed, .WSAENOTSOCK => error.FileDescriptorNotASocket, .WSAEINVAL => error.SocketNotBound, else => |err| windows.unexpectedWSAError(err), }; } return Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address)); } /// Query and return the latest cached error on the socket. pub fn getError(self: Socket) !void { _ = self; return {}; } /// Query the read buffer size of the socket. pub fn getReadBufferSize(self: Socket) !u32 { _ = self; return 0; } /// Query the write buffer size of the socket. pub fn getWriteBufferSize(self: Socket) !u32 { _ = self; return 0; } /// Set a socket option. pub fn setOption(self: Socket, level: u32, code: u32, value: []const u8) !void { const rc = ws2_32.setsockopt(self.fd, @intCast(i32, level), @intCast(i32, code), value.ptr, @intCast(i32, value.len)); if (rc == ws2_32.SOCKET_ERROR) { return switch (ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEFAULT => unreachable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotBound, else => |err| windows.unexpectedWSAError(err), }; } } /// Have close() or shutdown() syscalls block until all queued messages in the socket have been successfully /// sent, or if the timeout specified in seconds has been reached. It returns `error.UnsupportedSocketOption` /// if the host does not support the option for a socket to linger around up until a timeout specified in /// seconds. pub fn setLinger(self: Socket, timeout_seconds: ?u16) !void { const settings = Socket.Linger.init(timeout_seconds); return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.LINGER, mem.asBytes(&settings)); } /// On connection-oriented sockets, have keep-alive messages be sent periodically. The timing in which keep-alive /// messages are sent are dependant on operating system settings. It returns `error.UnsupportedSocketOption` if /// the host does not support periodically sending keep-alive messages on connection-oriented sockets. pub fn setKeepAlive(self: Socket, enabled: bool) !void { return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.KEEPALIVE, mem.asBytes(&@as(u32, @boolToInt(enabled)))); } /// Allow multiple sockets on the same host to listen on the same address. It returns `error.UnsupportedSocketOption` if /// the host does not support sockets listening the same address. pub fn setReuseAddress(self: Socket, enabled: bool) !void { return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.REUSEADDR, mem.asBytes(&@as(u32, @boolToInt(enabled)))); } /// Allow multiple sockets on the same host to listen on the same port. It returns `error.UnsupportedSocketOption` if /// the host does not supports sockets listening on the same port. /// /// TODO: verify if this truly mimicks SO.REUSEPORT behavior, or if SO.REUSE_UNICASTPORT provides the correct behavior pub fn setReusePort(self: Socket, enabled: bool) !void { try self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.BROADCAST, mem.asBytes(&@as(u32, @boolToInt(enabled)))); try self.setReuseAddress(enabled); } /// Set the write buffer size of the socket. pub fn setWriteBufferSize(self: Socket, size: u32) !void { return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.SNDBUF, mem.asBytes(&size)); } /// Set the read buffer size of the socket. pub fn setReadBufferSize(self: Socket, size: u32) !void { return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.RCVBUF, mem.asBytes(&size)); } /// WARNING: Timeouts only affect blocking sockets. It is undefined behavior if a timeout is /// set on a non-blocking socket. /// /// Set a timeout on the socket that is to occur if no messages are successfully written /// to its bound destination after a specified number of milliseconds. A subsequent write /// to the socket will thereafter return `error.WouldBlock` should the timeout be exceeded. pub fn setWriteTimeout(self: Socket, milliseconds: u32) !void { return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.SNDTIMEO, mem.asBytes(&milliseconds)); } /// WARNING: Timeouts only affect blocking sockets. It is undefined behavior if a timeout is /// set on a non-blocking socket. /// /// Set a timeout on the socket that is to occur if no messages are successfully read /// from its bound destination after a specified number of milliseconds. A subsequent /// read from the socket will thereafter return `error.WouldBlock` should the timeout be /// exceeded. pub fn setReadTimeout(self: Socket, milliseconds: u32) !void { return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.RCVTIMEO, mem.asBytes(&milliseconds)); } }; }
const std = @import("../std.zig"); const os = std.os; const testing = std.testing; const expect = testing.expect; const expectEqual = testing.expectEqual; const expectError = testing.expectError; const io = std.io; const fs = std.fs; const mem = std.mem; const elf = std.elf; const File = std.fs.File; const Thread = std.Thread; const a = std.testing.allocator; const builtin = @import("builtin"); const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; const tmpDir = std.testing.tmpDir; const Dir = std.fs.Dir; const ArenaAllocator = std.heap.ArenaAllocator; test "open smoke test" { if (builtin.os.tag == .wasi) return error.SkipZigTest; // TODO verify file attributes using `fstat` var tmp = tmpDir(.{}); defer tmp.cleanup(); // Get base abs path var arena = ArenaAllocator.init(testing.allocator); defer arena.deinit(); const base_path = blk: { const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); break :blk try fs.realpathAlloc(&arena.allocator, relative_path); }; var file_path: []u8 = undefined; var fd: os.fd_t = undefined; const mode: os.mode_t = if (builtin.os.tag == .windows) 0 else 0o666; // Create some file using `open`. file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); fd = try os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, mode); os.close(fd); // Try this again with the same flags. This op should fail with error.PathAlreadyExists. file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); expectError(error.PathAlreadyExists, os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, mode)); // Try opening without `O_EXCL` flag. file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); fd = try os.open(file_path, os.O_RDWR | os.O_CREAT, mode); os.close(fd); // Try opening as a directory which should fail. file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" }); expectError(error.NotDir, os.open(file_path, os.O_RDWR | os.O_DIRECTORY, mode)); // Create some directory file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" }); try os.mkdir(file_path, mode); // Open dir using `open` file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" }); fd = try os.open(file_path, os.O_RDONLY | os.O_DIRECTORY, mode); os.close(fd); // Try opening as file which should fail. file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" }); expectError(error.IsDir, os.open(file_path, os.O_RDWR, mode)); } test "openat smoke test" { if (builtin.os.tag == .wasi) return error.SkipZigTest; // TODO verify file attributes using `fstatat` var tmp = tmpDir(.{}); defer tmp.cleanup(); var fd: os.fd_t = undefined; const mode: os.mode_t = if (builtin.os.tag == .windows) 0 else 0o666; // Create some file using `openat`. fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, mode); os.close(fd); // Try this again with the same flags. This op should fail with error.PathAlreadyExists. expectError(error.PathAlreadyExists, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, mode)); // Try opening without `O_EXCL` flag. fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT, mode); os.close(fd); // Try opening as a directory which should fail. expectError(error.NotDir, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_DIRECTORY, mode)); // Create some directory try os.mkdirat(tmp.dir.fd, "some_dir", mode); // Open dir using `open` fd = try os.openat(tmp.dir.fd, "some_dir", os.O_RDONLY | os.O_DIRECTORY, mode); os.close(fd); // Try opening as file which should fail. expectError(error.IsDir, os.openat(tmp.dir.fd, "some_dir", os.O_RDWR, mode)); } test "symlink with relative paths" { if (builtin.os.tag == .wasi) return error.SkipZigTest; const cwd = fs.cwd(); cwd.deleteFile("file.txt") catch {}; cwd.deleteFile("symlinked") catch {}; // First, try relative paths in cwd try cwd.writeFile("file.txt", "nonsense"); if (builtin.os.tag == .windows) { try os.windows.CreateSymbolicLink(cwd.fd, &[_]u16{ 's', 'y', 'm', 'l', 'i', 'n', 'k', 'e', 'd' }, &[_]u16{ 'f', 'i', 'l', 'e', '.', 't', 'x', 't' }, false); } else { try os.symlink("file.txt", "symlinked"); } var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; const given = try os.readlink("symlinked", buffer[0..]); expect(mem.eql(u8, "file.txt", given)); try cwd.deleteFile("file.txt"); try cwd.deleteFile("symlinked"); } test "readlink on Windows" { if (builtin.os.tag != .windows) return error.SkipZigTest; try testReadlink("C:\\ProgramData", "C:\\Users\\All Users"); try testReadlink("C:\\Users\\Default", "C:\\Users\\Default User"); try testReadlink("C:\\Users", "C:\\Documents and Settings"); } fn testReadlink(target_path: []const u8, symlink_path: []const u8) !void { var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; const given = try os.readlink(symlink_path, buffer[0..]); expect(mem.eql(u8, target_path, given)); } test "fstatat" { // enable when `fstat` and `fstatat` are implemented on Windows if (builtin.os.tag == .windows) return error.SkipZigTest; var tmp = tmpDir(.{}); defer tmp.cleanup(); // create dummy file const contents = "nonsense"; try tmp.dir.writeFile("file.txt", contents); // fetch file's info on the opened fd directly const file = try tmp.dir.openFile("file.txt", .{}); const stat = try os.fstat(file.handle); defer file.close(); // now repeat but using `fstatat` instead const flags = if (builtin.os.tag == .wasi) 0x0 else os.AT_SYMLINK_NOFOLLOW; const statat = try os.fstatat(tmp.dir.fd, "file.txt", flags); expectEqual(stat, statat); } test "readlinkat" { var tmp = tmpDir(.{}); defer tmp.cleanup(); // create file try tmp.dir.writeFile("file.txt", "nonsense"); // create a symbolic link if (builtin.os.tag == .windows) { try os.windows.CreateSymbolicLink(tmp.dir.fd, &[_]u16{ 'l', 'i', 'n', 'k' }, &[_]u16{ 'f', 'i', 'l', 'e', '.', 't', 'x', 't' }, false); } else { try os.symlinkat("file.txt", tmp.dir.fd, "link"); } // read the link var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; const read_link = try os.readlinkat(tmp.dir.fd, "link", buffer[0..]); expect(mem.eql(u8, "file.txt", read_link)); } fn testThreadIdFn(thread_id: *Thread.Id) void { thread_id.* = Thread.getCurrentId(); } test "std.Thread.getCurrentId" { if (builtin.single_threaded) return error.SkipZigTest; var thread_current_id: Thread.Id = undefined; const thread = try Thread.spawn(&thread_current_id, testThreadIdFn); const thread_id = thread.handle(); thread.wait(); if (Thread.use_pthreads) { expect(thread_current_id == thread_id); } else if (builtin.os.tag == .windows) { expect(Thread.getCurrentId() != thread_current_id); } else { // If the thread completes very quickly, then thread_id can be 0. See the // documentation comments for `std.Thread.handle`. expect(thread_id == 0 or thread_current_id == thread_id); } } test "spawn threads" { if (builtin.single_threaded) return error.SkipZigTest; var shared_ctx: i32 = 1; const thread1 = try Thread.spawn({}, start1); const thread2 = try Thread.spawn(&shared_ctx, start2); const thread3 = try Thread.spawn(&shared_ctx, start2); const thread4 = try Thread.spawn(&shared_ctx, start2); thread1.wait(); thread2.wait(); thread3.wait(); thread4.wait(); expect(shared_ctx == 4); } fn start1(ctx: void) u8 { return 0; } fn start2(ctx: *i32) u8 { _ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst); return 0; } test "cpu count" { if (builtin.os.tag == .wasi) return error.SkipZigTest; const cpu_count = try Thread.cpuCount(); expect(cpu_count >= 1); } test "thread local storage" { if (builtin.single_threaded) return error.SkipZigTest; const thread1 = try Thread.spawn({}, testTls); const thread2 = try Thread.spawn({}, testTls); testTls({}); thread1.wait(); thread2.wait(); } threadlocal var x: i32 = 1234; fn testTls(context: void) void { if (x != 1234) @panic("bad start value"); x += 1; if (x != 1235) @panic("bad end value"); } test "getrandom" { var buf_a: [50]u8 = undefined; var buf_b: [50]u8 = undefined; try os.getrandom(&buf_a); try os.getrandom(&buf_b); // If this test fails the chance is significantly higher that there is a bug than // that two sets of 50 bytes were equal. expect(!mem.eql(u8, &buf_a, &buf_b)); } test "getcwd" { if (builtin.os.tag == .wasi) return error.SkipZigTest; // at least call it so it gets compiled var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; _ = os.getcwd(&buf) catch undefined; } test "sigaltstack" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest; var st: os.stack_t = undefined; try os.sigaltstack(null, &st); // Setting a stack size less than MINSIGSTKSZ returns ENOMEM st.ss_flags = 0; st.ss_size = 1; testing.expectError(error.SizeTooSmall, os.sigaltstack(&st, null)); } // If the type is not available use void to avoid erroring out when `iter_fn` is // analyzed const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void; const IterFnError = error{ MissingPtLoadSegment, MissingLoad, BadElfMagic, FailedConsistencyCheck, }; fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void { // Count how many libraries are loaded counter.* += @as(usize, 1); // The image should contain at least a PT_LOAD segment if (info.dlpi_phnum < 1) return error.MissingPtLoadSegment; // Quick & dirty validation of the phdr pointers, make sure we're not // pointing to some random gibberish var i: usize = 0; var found_load = false; while (i < info.dlpi_phnum) : (i += 1) { const phdr = info.dlpi_phdr[i]; if (phdr.p_type != elf.PT_LOAD) continue; const reloc_addr = info.dlpi_addr + phdr.p_vaddr; // Find the ELF header const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset); // Validate the magic if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return error.BadElfMagic; // Consistency check if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck; found_load = true; break; } if (!found_load) return error.MissingLoad; } test "dl_iterate_phdr" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi or builtin.os.tag == .macosx) return error.SkipZigTest; var counter: usize = 0; try os.dl_iterate_phdr(&counter, IterFnError, iter_fn); expect(counter != 0); } test "gethostname" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest; var buf: [os.HOST_NAME_MAX]u8 = undefined; const hostname = try os.gethostname(&buf); expect(hostname.len != 0); } test "pipe" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest; var fds = try os.pipe(); expect((try os.write(fds[1], "hello")) == 5); var buf: [16]u8 = undefined; expect((try os.read(fds[0], buf[0..])) == 5); testing.expectEqualSlices(u8, buf[0..5], "hello"); os.close(fds[1]); os.close(fds[0]); } test "argsAlloc" { var args = try std.process.argsAlloc(std.testing.allocator); std.process.argsFree(std.testing.allocator, args); } test "memfd_create" { // memfd_create is linux specific. if (builtin.os.tag != .linux) return error.SkipZigTest; const fd = std.os.memfd_create("test", 0) catch |err| switch (err) { // Related: https://github.com/ziglang/zig/issues/4019 error.SystemOutdated => return error.SkipZigTest, else => |e| return e, }; defer std.os.close(fd); expect((try std.os.write(fd, "test")) == 4); try std.os.lseek_SET(fd, 0); var buf: [10]u8 = undefined; const bytes_read = try std.os.read(fd, &buf); expect(bytes_read == 4); expect(mem.eql(u8, buf[0..4], "test")); } test "mmap" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest; var tmp = tmpDir(.{}); defer tmp.cleanup(); // Simple mmap() call with non page-aligned size { const data = try os.mmap( null, 1234, os.PROT_READ | os.PROT_WRITE, os.MAP_ANONYMOUS | os.MAP_PRIVATE, -1, 0, ); defer os.munmap(data); testing.expectEqual(@as(usize, 1234), data.len); // By definition the data returned by mmap is zero-filled testing.expect(mem.eql(u8, data, &[_]u8{0x00} ** 1234)); // Make sure the memory is writeable as requested std.mem.set(u8, data, 0x55); testing.expect(mem.eql(u8, data, &[_]u8{0x55} ** 1234)); } const test_out_file = "os_tmp_test"; // Must be a multiple of 4096 so that the test works with mmap2 const alloc_size = 8 * 4096; // Create a file used for testing mmap() calls with a file descriptor { const file = try tmp.dir.createFile(test_out_file, .{}); defer file.close(); const stream = file.outStream(); var i: u32 = 0; while (i < alloc_size / @sizeOf(u32)) : (i += 1) { try stream.writeIntNative(u32, i); } } // Map the whole file { const file = try tmp.dir.openFile(test_out_file, .{}); defer file.close(); const data = try os.mmap( null, alloc_size, os.PROT_READ, os.MAP_PRIVATE, file.handle, 0, ); defer os.munmap(data); var mem_stream = io.fixedBufferStream(data); const stream = mem_stream.inStream(); var i: u32 = 0; while (i < alloc_size / @sizeOf(u32)) : (i += 1) { testing.expectEqual(i, try stream.readIntNative(u32)); } } // Map the upper half of the file { const file = try tmp.dir.openFile(test_out_file, .{}); defer file.close(); const data = try os.mmap( null, alloc_size / 2, os.PROT_READ, os.MAP_PRIVATE, file.handle, alloc_size / 2, ); defer os.munmap(data); var mem_stream = io.fixedBufferStream(data); const stream = mem_stream.inStream(); var i: u32 = alloc_size / 2 / @sizeOf(u32); while (i < alloc_size / @sizeOf(u32)) : (i += 1) { testing.expectEqual(i, try stream.readIntNative(u32)); } } try tmp.dir.deleteFile(test_out_file); } test "getenv" { if (builtin.os.tag == .windows) { expect(os.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null); } else { expect(os.getenvZ("BOGUSDOESNOTEXISTENVVAR") == null); } } test "fcntl" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest; var tmp = tmpDir(.{}); defer tmp.cleanup(); const test_out_file = "os_tmp_test"; const file = try tmp.dir.createFile(test_out_file, .{}); defer { file.close(); tmp.dir.deleteFile(test_out_file) catch {}; } // Note: The test assumes createFile opens the file with O_CLOEXEC { const flags = try os.fcntl(file.handle, os.F_GETFD, 0); expect((flags & os.FD_CLOEXEC) != 0); } { _ = try os.fcntl(file.handle, os.F_SETFD, 0); const flags = try os.fcntl(file.handle, os.F_GETFD, 0); expect((flags & os.FD_CLOEXEC) == 0); } { _ = try os.fcntl(file.handle, os.F_SETFD, os.FD_CLOEXEC); const flags = try os.fcntl(file.handle, os.F_GETFD, 0); expect((flags & os.FD_CLOEXEC) != 0); } } test "signalfd" { if (builtin.os.tag != .linux) return error.SkipZigTest; _ = std.os.signalfd; }
const std = @import("../index.zig"); const math = std.math; const mem = std.mem; const io = std.io; const os = std.os; const elf = std.elf; const DW = std.dwarf; const macho = std.macho; const coff = std.coff; const pdb = std.pdb; const windows = os.windows; const ArrayList = std.ArrayList; const builtin = @import("builtin"); const maxInt = std.math.maxInt; pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator; pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator; pub const runtime_safety = switch (builtin.mode) { builtin.Mode.Debug, builtin.Mode.ReleaseSafe => true, builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => false, }; const Module = struct { mod_info: pdb.ModInfo, module_name: []u8, obj_file_name: []u8, populated: bool, symbols: []u8, subsect_info: []u8, checksum_offset: ?usize, }; /// Tries to write to stderr, unbuffered, and ignores any error returned. /// Does not append a newline. var stderr_file: os.File = undefined; var stderr_file_out_stream: os.File.OutStream = undefined; /// TODO multithreaded awareness var stderr_stream: ?*io.OutStream(os.File.WriteError) = null; var stderr_mutex = std.Mutex.init(); pub fn warn(comptime fmt: []const u8, args: ...) void { const held = stderr_mutex.acquire(); defer held.release(); const stderr = getStderrStream() catch return; stderr.print(fmt, args) catch return; } pub fn getStderrStream() !*io.OutStream(os.File.WriteError) { if (stderr_stream) |st| { return st; } else { stderr_file = try io.getStdErr(); stderr_file_out_stream = stderr_file.outStream(); const st = &stderr_file_out_stream.stream; stderr_stream = st; return st; } } /// TODO multithreaded awareness var self_debug_info: ?DebugInfo = null; pub fn getSelfDebugInfo() !*DebugInfo { if (self_debug_info) |*info| { return info; } else { self_debug_info = try openSelfDebugInfo(getDebugInfoAllocator()); return &self_debug_info.?; } } fn wantTtyColor() bool { var bytes: [128]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; return if (std.os.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| true else |_| stderr_file.isTty(); } /// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned. /// TODO multithreaded awareness pub fn dumpCurrentStackTrace(start_addr: ?usize) void { const stderr = getStderrStream() catch return; const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; writeCurrentStackTrace(stderr, debug_info, wantTtyColor(), start_addr) catch |err| { stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; return; }; } /// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned. /// TODO multithreaded awareness pub fn dumpStackTrace(stack_trace: *const builtin.StackTrace) void { const stderr = getStderrStream() catch return; const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, wantTtyColor()) catch |err| { stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; return; }; } /// This function invokes undefined behavior when `ok` is `false`. /// In Debug and ReleaseSafe modes, calls to this function are always /// generated, and the `unreachable` statement triggers a panic. /// In ReleaseFast and ReleaseSmall modes, calls to this function can be /// optimized away. pub fn assert(ok: bool) void { if (!ok) { // In ReleaseFast test mode, we still want assert(false) to crash, so // we insert an explicit call to @panic instead of unreachable. // TODO we should use `assertOrPanic` in tests and remove this logic. if (builtin.is_test) { @panic("assertion failure"); } else { unreachable; // assertion failure } } } /// TODO: add `==` operator for `error_union == error_set`, and then /// remove this function pub fn assertError(value: var, expected_error: anyerror) void { if (value) { @panic("expected error"); } else |actual_error| { assert(actual_error == expected_error); } } /// Call this function when you want to panic if the condition is not true. /// If `ok` is `false`, this function will panic in every release mode. pub fn assertOrPanic(ok: bool) void { if (!ok) { @panic("assertion failure"); } } pub fn panic(comptime format: []const u8, args: ...) noreturn { @setCold(true); const first_trace_addr = @ptrToInt(@returnAddress()); panicExtra(null, first_trace_addr, format, args); } /// TODO multithreaded awareness var panicking: u8 = 0; // TODO make this a bool pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: ...) noreturn { @setCold(true); if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) { // Panicked during a panic. // TODO detect if a different thread caused the panic, because in that case // we would want to return here instead of calling abort, so that the thread // which first called panic can finish printing a stack trace. os.abort(); } const stderr = getStderrStream() catch os.abort(); stderr.print(format ++ "\n", args) catch os.abort(); if (trace) |t| { dumpStackTrace(t); } dumpCurrentStackTrace(first_trace_addr); os.abort(); } const RED = "\x1b[31;1m"; const GREEN = "\x1b[32;1m"; const CYAN = "\x1b[36;1m"; const WHITE = "\x1b[37;1m"; const DIM = "\x1b[2m"; const RESET = "\x1b[0m"; pub fn writeStackTrace(stack_trace: *const builtin.StackTrace, out_stream: var, allocator: *mem.Allocator, debug_info: *DebugInfo, tty_color: bool) !void { var frame_index: usize = undefined; var frames_left: usize = undefined; if (stack_trace.index < stack_trace.instruction_addresses.len) { frame_index = 0; frames_left = stack_trace.index; } else { frame_index = (stack_trace.index + 1) % stack_trace.instruction_addresses.len; frames_left = stack_trace.instruction_addresses.len; } while (frames_left != 0) : ({ frames_left -= 1; frame_index = (frame_index + 1) % stack_trace.instruction_addresses.len; }) { const return_address = stack_trace.instruction_addresses[frame_index]; try printSourceAtAddress(debug_info, out_stream, return_address, tty_color); } } pub const StackIterator = struct { first_addr: ?usize, fp: usize, pub fn init(first_addr: ?usize) StackIterator { return StackIterator{ .first_addr = first_addr, .fp = @ptrToInt(@frameAddress()), }; } fn next(self: *StackIterator) ?usize { if (self.fp == 0) return null; self.fp = @intToPtr(*const usize, self.fp).*; if (self.fp == 0) return null; if (self.first_addr) |addr| { while (self.fp != 0) : (self.fp = @intToPtr(*const usize, self.fp).*) { const return_address = @intToPtr(*const usize, self.fp + @sizeOf(usize)).*; if (addr == return_address) { self.first_addr = null; return return_address; } } } const return_address = @intToPtr(*const usize, self.fp + @sizeOf(usize)).*; return return_address; } }; pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void { switch (builtin.os) { builtin.Os.windows => return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr), else => {}, } var it = StackIterator.init(start_addr); while (it.next()) |return_address| { try printSourceAtAddress(debug_info, out_stream, return_address, tty_color); } } pub fn writeCurrentStackTraceWindows( out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize, ) !void { var addr_buf: [1024]usize = undefined; const casted_len = @intCast(u32, addr_buf.len); // TODO shouldn't need this cast const n = windows.RtlCaptureStackBackTrace(0, casted_len, @ptrCast(**c_void, &addr_buf), null); const addrs = addr_buf[0..n]; var start_i: usize = if (start_addr) |saddr| blk: { for (addrs) |addr, i| { if (addr == saddr) break :blk i; } return; } else 0; for (addrs[start_i..]) |addr| { try printSourceAtAddress(debug_info, out_stream, addr, tty_color); } } pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { switch (builtin.os) { builtin.Os.macosx => return printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color), builtin.Os.linux => return printSourceAtAddressLinux(debug_info, out_stream, address, tty_color), builtin.Os.windows => return printSourceAtAddressWindows(debug_info, out_stream, address, tty_color), else => return error.UnsupportedOperatingSystem, } } fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_address: usize, tty_color: bool) !void { const allocator = getDebugInfoAllocator(); const base_address = os.getBaseAddress(); const relative_address = relocated_address - base_address; var coff_section: *coff.Section = undefined; const mod_index = for (di.sect_contribs) |sect_contrib| { if (sect_contrib.Section > di.coff.sections.len) continue; // Remember that SectionContribEntry.Section is 1-based. coff_section = &di.coff.sections.toSlice()[sect_contrib.Section - 1]; const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset; const vaddr_end = vaddr_start + sect_contrib.Size; if (relative_address >= vaddr_start and relative_address < vaddr_end) { break sect_contrib.ModuleIndex; } } else { // we have no information to add to the address if (tty_color) { try out_stream.print("???:?:?: "); setTtyColor(TtyColor.Dim); try out_stream.print("0x{x} in ??? (???)", relocated_address); setTtyColor(TtyColor.Reset); try out_stream.print("\n\n\n"); } else { try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", relocated_address); } return; }; const mod = &di.modules[mod_index]; try populateModule(di, mod); const obj_basename = os.path.basename(mod.obj_file_name); var symbol_i: usize = 0; const symbol_name = while (symbol_i != mod.symbols.len) { const prefix = @ptrCast(*pdb.RecordPrefix, &mod.symbols[symbol_i]); if (prefix.RecordLen < 2) return error.InvalidDebugInfo; switch (prefix.RecordKind) { pdb.SymbolKind.S_LPROC32 => { const proc_sym = @ptrCast(*pdb.ProcSym, &mod.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)]); const vaddr_start = coff_section.header.virtual_address + proc_sym.CodeOffset; const vaddr_end = vaddr_start + proc_sym.CodeSize; if (relative_address >= vaddr_start and relative_address < vaddr_end) { break mem.toSliceConst(u8, @ptrCast([*]u8, proc_sym) + @sizeOf(pdb.ProcSym)); } }, else => {}, } symbol_i += prefix.RecordLen + @sizeOf(u16); if (symbol_i > mod.symbols.len) return error.InvalidDebugInfo; } else "???"; const subsect_info = mod.subsect_info; var sect_offset: usize = 0; var skip_len: usize = undefined; const opt_line_info = subsections: { const checksum_offset = mod.checksum_offset orelse break :subsections null; while (sect_offset != subsect_info.len) : (sect_offset += skip_len) { const subsect_hdr = @ptrCast(*pdb.DebugSubsectionHeader, &subsect_info[sect_offset]); skip_len = subsect_hdr.Length; sect_offset += @sizeOf(pdb.DebugSubsectionHeader); switch (subsect_hdr.Kind) { pdb.DebugSubsectionKind.Lines => { var line_index: usize = sect_offset; const line_hdr = @ptrCast(*pdb.LineFragmentHeader, &subsect_info[line_index]); if (line_hdr.RelocSegment == 0) return error.MissingDebugInfo; line_index += @sizeOf(pdb.LineFragmentHeader); const block_hdr = @ptrCast(*pdb.LineBlockFragmentHeader, &subsect_info[line_index]); line_index += @sizeOf(pdb.LineBlockFragmentHeader); const has_column = line_hdr.Flags.LF_HaveColumns; const frag_vaddr_start = coff_section.header.virtual_address + line_hdr.RelocOffset; const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize; if (relative_address >= frag_vaddr_start and relative_address < frag_vaddr_end) { var line_i: usize = 0; const start_line_index = line_index; while (line_i < block_hdr.NumLines) : (line_i += 1) { const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[line_index]); line_index += @sizeOf(pdb.LineNumberEntry); const flags = @ptrCast(*pdb.LineNumberEntry.Flags, &line_num_entry.Flags); const vaddr_start = frag_vaddr_start + line_num_entry.Offset; const vaddr_end = if (flags.End == 0) frag_vaddr_end else vaddr_start + flags.End; if (relative_address >= vaddr_start and relative_address < vaddr_end) { const subsect_index = checksum_offset + block_hdr.NameIndex; const chksum_hdr = @ptrCast(*pdb.FileChecksumEntryHeader, &mod.subsect_info[subsect_index]); const strtab_offset = @sizeOf(pdb.PDBStringTableHeader) + chksum_hdr.FileNameOffset; try di.pdb.string_table.seekTo(strtab_offset); const source_file_name = try di.pdb.string_table.readNullTermString(allocator); const line = flags.Start; const column = if (has_column) blk: { line_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines; line_index += @sizeOf(pdb.ColumnNumberEntry) * line_i; const col_num_entry = @ptrCast(*pdb.ColumnNumberEntry, &subsect_info[line_index]); break :blk col_num_entry.StartColumn; } else 0; break :subsections LineInfo{ .allocator = allocator, .file_name = source_file_name, .line = line, .column = column, }; } } break :subsections null; } }, else => {}, } if (sect_offset > subsect_info.len) return error.InvalidDebugInfo; } else { break :subsections null; } }; if (tty_color) { setTtyColor(TtyColor.White); if (opt_line_info) |li| { try out_stream.print("{}:{}:{}", li.file_name, li.line, li.column); } else { try out_stream.print("???:?:?"); } setTtyColor(TtyColor.Reset); try out_stream.print(": "); setTtyColor(TtyColor.Dim); try out_stream.print("0x{x} in {} ({})", relocated_address, symbol_name, obj_basename); setTtyColor(TtyColor.Reset); if (opt_line_info) |line_info| { try out_stream.print("\n"); if (printLineFromFileAnyOs(out_stream, line_info)) { if (line_info.column == 0) { try out_stream.write("\n"); } else { { var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) { try out_stream.writeByte(' '); } } setTtyColor(TtyColor.Green); try out_stream.write("^"); setTtyColor(TtyColor.Reset); try out_stream.write("\n"); } } else |err| switch (err) { error.EndOfFile => {}, else => return err, } } else { try out_stream.print("\n\n\n"); } } else { if (opt_line_info) |li| { try out_stream.print("{}:{}:{}: 0x{x} in {} ({})\n\n\n", li.file_name, li.line, li.column, relocated_address, symbol_name, obj_basename); } else { try out_stream.print("???:?:?: 0x{x} in {} ({})\n\n\n", relocated_address, symbol_name, obj_basename); } } } const TtyColor = enum { Red, Green, Cyan, White, Dim, Bold, Reset, }; /// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt fn setTtyColor(tty_color: TtyColor) void { if (os.supportsAnsiEscapeCodes(stderr_file.handle)) { switch (tty_color) { TtyColor.Red => { stderr_file.write(RED) catch return; }, TtyColor.Green => { stderr_file.write(GREEN) catch return; }, TtyColor.Cyan => { stderr_file.write(CYAN) catch return; }, TtyColor.White, TtyColor.Bold => { stderr_file.write(WHITE) catch return; }, TtyColor.Dim => { stderr_file.write(DIM) catch return; }, TtyColor.Reset => { stderr_file.write(RESET) catch return; }, } } else { const S = struct { var attrs: windows.WORD = undefined; var init_attrs = false; }; if (!S.init_attrs) { S.init_attrs = true; var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; // TODO handle error _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); S.attrs = info.wAttributes; } // TODO handle errors switch (tty_color) { TtyColor.Red => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); }, TtyColor.Green => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); }, TtyColor.Cyan => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); }, TtyColor.White, TtyColor.Bold => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); }, TtyColor.Dim => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); }, TtyColor.Reset => { _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); }, } } } fn populateModule(di: *DebugInfo, mod: *Module) !void { if (mod.populated) return; const allocator = getDebugInfoAllocator(); if (mod.mod_info.C11ByteSize != 0) return error.InvalidDebugInfo; if (mod.mod_info.C13ByteSize == 0) return error.MissingDebugInfo; const modi = di.pdb.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.MissingDebugInfo; const signature = try modi.stream.readIntLe(u32); if (signature != 4) return error.InvalidDebugInfo; mod.symbols = try allocator.alloc(u8, mod.mod_info.SymByteSize - 4); try modi.stream.readNoEof(mod.symbols); mod.subsect_info = try allocator.alloc(u8, mod.mod_info.C13ByteSize); try modi.stream.readNoEof(mod.subsect_info); var sect_offset: usize = 0; var skip_len: usize = undefined; while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) { const subsect_hdr = @ptrCast(*pdb.DebugSubsectionHeader, &mod.subsect_info[sect_offset]); skip_len = subsect_hdr.Length; sect_offset += @sizeOf(pdb.DebugSubsectionHeader); switch (subsect_hdr.Kind) { pdb.DebugSubsectionKind.FileChecksums => { mod.checksum_offset = sect_offset; break; }, else => {}, } if (sect_offset > mod.subsect_info.len) return error.InvalidDebugInfo; } mod.populated = true; } fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const MachoSymbol { var min: usize = 0; var max: usize = symbols.len - 1; // Exclude sentinel. while (min < max) { const mid = min + (max - min) / 2; const curr = &symbols[mid]; const next = &symbols[mid + 1]; if (address >= next.address()) { min = mid + 1; } else if (address < curr.address()) { max = mid; } else { return curr; } } return null; } fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { const base_addr = @ptrToInt(&std.c._mh_execute_header); const adjusted_addr = 0x100000000 + (address - base_addr); const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse { if (tty_color) { try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", address); } else { try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", address); } return; }; const symbol_name = mem.toSliceConst(u8, di.strings.ptr + symbol.nlist.n_strx); const compile_unit_name = if (symbol.ofile) |ofile| blk: { const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx); break :blk os.path.basename(ofile_path); } else "???"; if (getLineNumberInfoMacOs(di, symbol.*, adjusted_addr)) |line_info| { defer line_info.deinit(); try printLineInfo( out_stream, line_info, address, symbol_name, compile_unit_name, tty_color, printLineFromFileAnyOs, ); } else |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => { if (tty_color) { try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in {} ({})" ++ RESET ++ "\n\n\n", address, symbol_name, compile_unit_name); } else { try out_stream.print("???:?:?: 0x{x} in {} ({})\n\n\n", address, symbol_name, compile_unit_name); } }, else => return err, } } /// This function works in freestanding mode. /// fn printLineFromFile(out_stream: var, line_info: LineInfo) !void pub fn printSourceAtAddressDwarf( debug_info: *DwarfInfo, out_stream: var, address: usize, tty_color: bool, comptime printLineFromFile: var, ) !void { const compile_unit = findCompileUnit(debug_info, address) catch { if (tty_color) { try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n\n\n", address); } else { try out_stream.print("???:?:?: 0x{x} in ??? (???)\n\n\n", address); } return; }; const compile_unit_name = try compile_unit.die.getAttrString(debug_info, DW.AT_name); if (getLineNumberInfoDwarf(debug_info, compile_unit.*, address - 1)) |line_info| { defer line_info.deinit(); const symbol_name = "???"; try printLineInfo( out_stream, line_info, address, symbol_name, compile_unit_name, tty_color, printLineFromFile, ); } else |err| switch (err) { error.MissingDebugInfo, error.InvalidDebugInfo => { if (tty_color) { try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? ({})" ++ RESET ++ "\n\n\n", address, compile_unit_name); } else { try out_stream.print("???:?:?: 0x{x} in ??? ({})\n\n\n", address, compile_unit_name); } }, else => return err, } } pub fn printSourceAtAddressLinux(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { return printSourceAtAddressDwarf(debug_info, out_stream, address, tty_color, printLineFromFileAnyOs); } fn printLineInfo( out_stream: var, line_info: LineInfo, address: usize, symbol_name: []const u8, compile_unit_name: []const u8, tty_color: bool, comptime printLineFromFile: var, ) !void { if (tty_color) { try out_stream.print( WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ "0x{x} in {} ({})" ++ RESET ++ "\n", line_info.file_name, line_info.line, line_info.column, address, symbol_name, compile_unit_name, ); if (printLineFromFile(out_stream, line_info)) { if (line_info.column == 0) { try out_stream.write("\n"); } else { { var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) { try out_stream.writeByte(' '); } } try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); } } else |err| switch (err) { error.EndOfFile => {}, else => return err, } } else { try out_stream.print( "{}:{}:{}: 0x{x} in {} ({})\n", line_info.file_name, line_info.line, line_info.column, address, symbol_name, compile_unit_name, ); } } // TODO use this pub const OpenSelfDebugInfoError = error{ MissingDebugInfo, OutOfMemory, UnsupportedOperatingSystem, }; pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { switch (builtin.os) { builtin.Os.linux => return openSelfDebugInfoLinux(allocator), builtin.Os.macosx, builtin.Os.ios => return openSelfDebugInfoMacOs(allocator), builtin.Os.windows => return openSelfDebugInfoWindows(allocator), else => return error.UnsupportedOperatingSystem, } } fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { const self_file = try os.openSelfExe(); defer self_file.close(); const coff_obj = try allocator.createOne(coff.Coff); coff_obj.* = coff.Coff{ .in_file = self_file, .allocator = allocator, .coff_header = undefined, .pe_header = undefined, .sections = undefined, .guid = undefined, .age = undefined, }; var di = DebugInfo{ .coff = coff_obj, .pdb = undefined, .sect_contribs = undefined, .modules = undefined, }; try di.coff.loadHeader(); var path_buf: [windows.MAX_PATH]u8 = undefined; const len = try di.coff.getPdbPath(path_buf[0..]); const raw_path = path_buf[0..len]; const path = try os.path.resolve(allocator, raw_path); try di.pdb.openFile(di.coff, path); var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo; const version = try pdb_stream.stream.readIntLe(u32); const signature = try pdb_stream.stream.readIntLe(u32); const age = try pdb_stream.stream.readIntLe(u32); var guid: [16]u8 = undefined; try pdb_stream.stream.readNoEof(guid[0..]); if (!mem.eql(u8, di.coff.guid, guid) or di.coff.age != age) return error.InvalidDebugInfo; // We validated the executable and pdb match. const string_table_index = str_tab_index: { const name_bytes_len = try pdb_stream.stream.readIntLe(u32); const name_bytes = try allocator.alloc(u8, name_bytes_len); try pdb_stream.stream.readNoEof(name_bytes); const HashTableHeader = packed struct { Size: u32, Capacity: u32, fn maxLoad(cap: u32) u32 { return cap * 2 / 3 + 1; } }; const hash_tbl_hdr = try pdb_stream.stream.readStruct(HashTableHeader); if (hash_tbl_hdr.Capacity == 0) return error.InvalidDebugInfo; if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity)) return error.InvalidDebugInfo; const present = try readSparseBitVector(&pdb_stream.stream, allocator); if (present.len != hash_tbl_hdr.Size) return error.InvalidDebugInfo; const deleted = try readSparseBitVector(&pdb_stream.stream, allocator); const Bucket = struct { first: u32, second: u32, }; const bucket_list = try allocator.alloc(Bucket, present.len); for (present) |_| { const name_offset = try pdb_stream.stream.readIntLe(u32); const name_index = try pdb_stream.stream.readIntLe(u32); const name = mem.toSlice(u8, name_bytes.ptr + name_offset); if (mem.eql(u8, name, "/names")) { break :str_tab_index name_index; } } return error.MissingDebugInfo; }; di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.InvalidDebugInfo; di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo; const dbi = di.pdb.dbi; // Dbi Header const dbi_stream_header = try dbi.stream.readStruct(pdb.DbiStreamHeader); const mod_info_size = dbi_stream_header.ModInfoSize; const section_contrib_size = dbi_stream_header.SectionContributionSize; var modules = ArrayList(Module).init(allocator); // Module Info Substream var mod_info_offset: usize = 0; while (mod_info_offset != mod_info_size) { const mod_info = try dbi.stream.readStruct(pdb.ModInfo); var this_record_len: usize = @sizeOf(pdb.ModInfo); const module_name = try dbi.readNullTermString(allocator); this_record_len += module_name.len + 1; const obj_file_name = try dbi.readNullTermString(allocator); this_record_len += obj_file_name.len + 1; const march_forward_bytes = this_record_len % 4; if (march_forward_bytes != 0) { try dbi.seekForward(march_forward_bytes); this_record_len += march_forward_bytes; } try modules.append(Module{ .mod_info = mod_info, .module_name = module_name, .obj_file_name = obj_file_name, .populated = false, .symbols = undefined, .subsect_info = undefined, .checksum_offset = null, }); mod_info_offset += this_record_len; if (mod_info_offset > mod_info_size) return error.InvalidDebugInfo; } di.modules = modules.toOwnedSlice(); // Section Contribution Substream var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator); var sect_cont_offset: usize = 0; if (section_contrib_size != 0) { const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.stream.readIntLe(u32)); if (ver != pdb.SectionContrSubstreamVersion.Ver60) return error.InvalidDebugInfo; sect_cont_offset += @sizeOf(u32); } while (sect_cont_offset != section_contrib_size) { const entry = try sect_contribs.addOne(); entry.* = try dbi.stream.readStruct(pdb.SectionContribEntry); sect_cont_offset += @sizeOf(pdb.SectionContribEntry); if (sect_cont_offset > section_contrib_size) return error.InvalidDebugInfo; } di.sect_contribs = sect_contribs.toOwnedSlice(); return di; } fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize { const num_words = try stream.readIntLe(u32); var word_i: usize = 0; var list = ArrayList(usize).init(allocator); while (word_i != num_words) : (word_i += 1) { const word = try stream.readIntLe(u32); var bit_i: u5 = 0; while (true) : (bit_i += 1) { if (word & (u32(1) << bit_i) != 0) { try list.append(word_i * 32 + bit_i); } if (bit_i == maxInt(u5)) break; } } return list.toOwnedSlice(); } fn findDwarfSectionFromElf(elf_file: *elf.Elf, name: []const u8) !?DwarfInfo.Section { const elf_header = (try elf_file.findSection(name)) orelse return null; return DwarfInfo.Section{ .offset = elf_header.offset, .size = elf_header.size, }; } /// Initialize DWARF info. The caller has the responsibility to initialize most /// the DwarfInfo fields before calling. These fields can be left undefined: /// * abbrev_table_list /// * compile_unit_list pub fn openDwarfDebugInfo(di: *DwarfInfo, allocator: *mem.Allocator) !void { di.abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator); di.compile_unit_list = ArrayList(CompileUnit).init(allocator); try scanAllCompileUnits(di); } pub fn openElfDebugInfo( allocator: *mem.Allocator, elf_seekable_stream: *DwarfSeekableStream, elf_in_stream: *DwarfInStream, ) !DwarfInfo { var efile: elf.Elf = undefined; try efile.openStream(allocator, elf_seekable_stream, elf_in_stream); errdefer efile.close(); var di = DwarfInfo{ .dwarf_seekable_stream = elf_seekable_stream, .dwarf_in_stream = elf_in_stream, .endian = efile.endian, .debug_info = (try findDwarfSectionFromElf(&efile, ".debug_info")) orelse return error.MissingDebugInfo, .debug_abbrev = (try findDwarfSectionFromElf(&efile, ".debug_abbrev")) orelse return error.MissingDebugInfo, .debug_str = (try findDwarfSectionFromElf(&efile, ".debug_str")) orelse return error.MissingDebugInfo, .debug_line = (try findDwarfSectionFromElf(&efile, ".debug_line")) orelse return error.MissingDebugInfo, .debug_ranges = (try findDwarfSectionFromElf(&efile, ".debug_ranges")), .abbrev_table_list = undefined, .compile_unit_list = undefined, }; try openDwarfDebugInfo(&di, allocator); return di; } fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { const S = struct { var self_exe_file: os.File = undefined; var self_exe_seekable_stream: os.File.SeekableStream = undefined; var self_exe_in_stream: os.File.InStream = undefined; }; S.self_exe_file = try os.openSelfExe(); errdefer S.self_exe_file.close(); S.self_exe_seekable_stream = S.self_exe_file.seekableStream(); S.self_exe_in_stream = S.self_exe_file.inStream(); return openElfDebugInfo( allocator, // TODO https://github.com/ziglang/zig/issues/764 @ptrCast(*DwarfSeekableStream, &S.self_exe_seekable_stream.stream), // TODO https://github.com/ziglang/zig/issues/764 @ptrCast(*DwarfInStream, &S.self_exe_in_stream.stream), ); } fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo { const hdr = &std.c._mh_execute_header; assert(hdr.magic == std.macho.MH_MAGIC_64); const hdr_base = @ptrCast([*]u8, hdr); var ptr = hdr_base + @sizeOf(macho.mach_header_64); var ncmd: u32 = hdr.ncmds; const symtab = while (ncmd != 0) : (ncmd -= 1) { const lc = @ptrCast(*std.macho.load_command, ptr); switch (lc.cmd) { std.macho.LC_SYMTAB => break @ptrCast(*std.macho.symtab_command, ptr), else => {}, } ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/1403 } else { return error.MissingDebugInfo; }; const syms = @ptrCast([*]macho.nlist_64, @alignCast(@alignOf(macho.nlist_64), hdr_base + symtab.symoff))[0..symtab.nsyms]; const strings = @ptrCast([*]u8, hdr_base + symtab.stroff)[0..symtab.strsize]; const symbols_buf = try allocator.alloc(MachoSymbol, syms.len); var ofile: ?*macho.nlist_64 = null; var reloc: u64 = 0; var symbol_index: usize = 0; var last_len: u64 = 0; for (syms) |*sym| { if (sym.n_type & std.macho.N_STAB != 0) { switch (sym.n_type) { std.macho.N_OSO => { ofile = sym; reloc = 0; }, std.macho.N_FUN => { if (sym.n_sect == 0) { last_len = sym.n_value; } else { symbols_buf[symbol_index] = MachoSymbol{ .nlist = sym, .ofile = ofile, .reloc = reloc, }; symbol_index += 1; } }, std.macho.N_BNSYM => { if (reloc == 0) { reloc = sym.n_value; } }, else => continue, } } } const sentinel = try allocator.createOne(macho.nlist_64); sentinel.* = macho.nlist_64{ .n_strx = 0, .n_type = 36, .n_sect = 0, .n_desc = 0, .n_value = symbols_buf[symbol_index - 1].nlist.n_value + last_len, }; const symbols = allocator.shrink(MachoSymbol, symbols_buf, symbol_index); // Even though lld emits symbols in ascending order, this debug code // should work for programs linked in any valid way. // This sort is so that we can binary search later. std.sort.sort(MachoSymbol, symbols, MachoSymbol.addressLessThan); return DebugInfo{ .ofiles = DebugInfo.OFileTable.init(allocator), .symbols = symbols, .strings = strings, }; } fn printLineFromFileAnyOs(out_stream: var, line_info: LineInfo) !void { var f = try os.File.openRead(line_info.file_name); defer f.close(); // TODO fstat and make sure that the file has the correct size var buf: [os.page_size]u8 = undefined; var line: usize = 1; var column: usize = 1; var abs_index: usize = 0; while (true) { const amt_read = try f.read(buf[0..]); const slice = buf[0..amt_read]; for (slice) |byte| { if (line == line_info.line) { try out_stream.writeByte(byte); if (byte == '\n') { return; } } if (byte == '\n') { line += 1; column = 1; } else { column += 1; } } if (amt_read < buf.len) return error.EndOfFile; } } const MachoSymbol = struct { nlist: *macho.nlist_64, ofile: ?*macho.nlist_64, reloc: u64, /// Returns the address from the macho file fn address(self: MachoSymbol) u64 { return self.nlist.n_value; } fn addressLessThan(lhs: MachoSymbol, rhs: MachoSymbol) bool { return lhs.address() < rhs.address(); } }; const MachOFile = struct { bytes: []align(@alignOf(macho.mach_header_64)) const u8, sect_debug_info: ?*const macho.section_64, sect_debug_line: ?*const macho.section_64, }; pub const DwarfSeekableStream = io.SeekableStream(anyerror, anyerror); pub const DwarfInStream = io.InStream(anyerror); pub const DwarfInfo = struct { dwarf_seekable_stream: *DwarfSeekableStream, dwarf_in_stream: *DwarfInStream, endian: builtin.Endian, debug_info: Section, debug_abbrev: Section, debug_str: Section, debug_line: Section, debug_ranges: ?Section, abbrev_table_list: ArrayList(AbbrevTableHeader), compile_unit_list: ArrayList(CompileUnit), pub const Section = struct { offset: usize, size: usize, }; pub fn allocator(self: DwarfInfo) *mem.Allocator { return self.abbrev_table_list.allocator; } pub fn readString(self: *DwarfInfo) ![]u8 { return readStringRaw(self.allocator(), self.dwarf_in_stream); } }; pub const DebugInfo = switch (builtin.os) { builtin.Os.macosx => struct { symbols: []const MachoSymbol, strings: []const u8, ofiles: OFileTable, const OFileTable = std.HashMap( *macho.nlist_64, MachOFile, std.hash_map.getHashPtrAddrFn(*macho.nlist_64), std.hash_map.getTrivialEqlFn(*macho.nlist_64), ); pub fn allocator(self: DebugInfo) *mem.Allocator { return self.ofiles.allocator; } }, builtin.Os.windows => struct { pdb: pdb.Pdb, coff: *coff.Coff, sect_contribs: []pdb.SectionContribEntry, modules: []Module, }, builtin.Os.linux => DwarfInfo, builtin.Os.freebsd => struct {}, else => @compileError("Unsupported OS"), }; const PcRange = struct { start: u64, end: u64, }; const CompileUnit = struct { version: u16, is_64: bool, die: *Die, index: usize, pc_range: ?PcRange, }; const AbbrevTable = ArrayList(AbbrevTableEntry); const AbbrevTableHeader = struct { // offset from .debug_abbrev offset: u64, table: AbbrevTable, }; const AbbrevTableEntry = struct { has_children: bool, abbrev_code: u64, tag_id: u64, attrs: ArrayList(AbbrevAttr), }; const AbbrevAttr = struct { attr_id: u64, form_id: u64, }; const FormValue = union(enum) { Address: u64, Block: []u8, Const: Constant, ExprLoc: []u8, Flag: bool, SecOffset: u64, Ref: []u8, RefAddr: u64, RefSig8: u64, String: []u8, StrPtr: u64, }; const Constant = struct { payload: []u8, signed: bool, fn asUnsignedLe(self: *const Constant) !u64 { if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo; if (self.signed) return error.InvalidDebugInfo; return mem.readInt(self.payload, u64, builtin.Endian.Little); } }; const Die = struct { tag_id: u64, has_children: bool, attrs: ArrayList(Attr), const Attr = struct { id: u64, value: FormValue, }; fn getAttr(self: *const Die, id: u64) ?*const FormValue { for (self.attrs.toSliceConst()) |*attr| { if (attr.id == id) return &attr.value; } return null; } fn getAttrAddr(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Address => |value| value, else => error.InvalidDebugInfo, }; } fn getAttrSecOffset(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Const => |value| value.asUnsignedLe(), FormValue.SecOffset => |value| value, else => error.InvalidDebugInfo, }; } fn getAttrUnsignedLe(self: *const Die, id: u64) !u64 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.Const => |value| value.asUnsignedLe(), else => error.InvalidDebugInfo, }; } fn getAttrString(self: *const Die, di: *DwarfInfo, id: u64) ![]u8 { const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; return switch (form_value.*) { FormValue.String => |value| value, FormValue.StrPtr => |offset| getString(di, offset), else => error.InvalidDebugInfo, }; } }; const FileEntry = struct { file_name: []const u8, dir_index: usize, mtime: usize, len_bytes: usize, }; pub const LineInfo = struct { line: usize, column: usize, file_name: []const u8, allocator: ?*mem.Allocator, fn deinit(self: LineInfo) void { const allocator = self.allocator orelse return; allocator.free(self.file_name); } }; const LineNumberProgram = struct { address: usize, file: usize, line: isize, column: usize, is_stmt: bool, basic_block: bool, end_sequence: bool, target_address: usize, include_dirs: []const []const u8, file_entries: *ArrayList(FileEntry), prev_address: usize, prev_file: usize, prev_line: isize, prev_column: usize, prev_is_stmt: bool, prev_basic_block: bool, prev_end_sequence: bool, pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: *ArrayList(FileEntry), target_address: usize) LineNumberProgram { return LineNumberProgram{ .address = 0, .file = 1, .line = 1, .column = 0, .is_stmt = is_stmt, .basic_block = false, .end_sequence = false, .include_dirs = include_dirs, .file_entries = file_entries, .target_address = target_address, .prev_address = 0, .prev_file = undefined, .prev_line = undefined, .prev_column = undefined, .prev_is_stmt = undefined, .prev_basic_block = undefined, .prev_end_sequence = undefined, }; } pub fn checkLineMatch(self: *LineNumberProgram) !?LineInfo { if (self.target_address >= self.prev_address and self.target_address < self.address) { const file_entry = if (self.prev_file == 0) { return error.MissingDebugInfo; } else if (self.prev_file - 1 >= self.file_entries.len) { return error.InvalidDebugInfo; } else &self.file_entries.items[self.prev_file - 1]; const dir_name = if (file_entry.dir_index >= self.include_dirs.len) { return error.InvalidDebugInfo; } else self.include_dirs[file_entry.dir_index]; const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); errdefer self.file_entries.allocator.free(file_name); return LineInfo{ .line = if (self.prev_line >= 0) @intCast(usize, self.prev_line) else 0, .column = self.prev_column, .file_name = file_name, .allocator = self.file_entries.allocator, }; } self.prev_address = self.address; self.prev_file = self.file; self.prev_line = self.line; self.prev_column = self.column; self.prev_is_stmt = self.is_stmt; self.prev_basic_block = self.basic_block; self.prev_end_sequence = self.end_sequence; return null; } }; fn readStringRaw(allocator: *mem.Allocator, in_stream: var) ![]u8 { var buf = ArrayList(u8).init(allocator); while (true) { const byte = try in_stream.readByte(); if (byte == 0) break; try buf.append(byte); } return buf.toSlice(); } fn getString(di: *DwarfInfo, offset: u64) ![]u8 { const pos = di.debug_str.offset + offset; try di.dwarf_seekable_stream.seekTo(pos); return di.readString(); } fn readAllocBytes(allocator: *mem.Allocator, in_stream: var, size: usize) ![]u8 { const buf = try allocator.alloc(u8, size); errdefer allocator.free(buf); if ((try in_stream.read(buf)) < size) return error.EndOfFile; return buf; } fn parseFormValueBlockLen(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue { const buf = try readAllocBytes(allocator, in_stream, size); return FormValue{ .Block = buf }; } fn parseFormValueBlock(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue { const block_len = try in_stream.readVarInt(builtin.Endian.Little, usize, size); return parseFormValueBlockLen(allocator, in_stream, block_len); } fn parseFormValueConstant(allocator: *mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue { return FormValue{ .Const = Constant{ .signed = signed, .payload = try readAllocBytes(allocator, in_stream, size), }, }; } fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 { return if (is_64) try in_stream.readIntLe(u64) else u64(try in_stream.readIntLe(u32)); } fn parseFormValueTargetAddrSize(in_stream: var) !u64 { return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) else unreachable; } fn parseFormValueRefLen(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue { const buf = try readAllocBytes(allocator, in_stream, size); return FormValue{ .Ref = buf }; } fn parseFormValueRef(allocator: *mem.Allocator, in_stream: var, comptime T: type) !FormValue { const block_len = try in_stream.readIntLe(T); return parseFormValueRefLen(allocator, in_stream, block_len); } fn parseFormValue(allocator: *mem.Allocator, in_stream: var, form_id: u64, is_64: bool) anyerror!FormValue { return switch (form_id) { DW.FORM_addr => FormValue{ .Address = try parseFormValueTargetAddrSize(in_stream) }, DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1), DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2), DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4), DW.FORM_block => x: { const block_len = try readULeb128(in_stream); return parseFormValueBlockLen(allocator, in_stream, block_len); }, DW.FORM_data1 => parseFormValueConstant(allocator, in_stream, false, 1), DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2), DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4), DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8), DW.FORM_udata, DW.FORM_sdata => { const block_len = try readULeb128(in_stream); const signed = form_id == DW.FORM_sdata; return parseFormValueConstant(allocator, in_stream, signed, block_len); }, DW.FORM_exprloc => { const size = try readULeb128(in_stream); const buf = try readAllocBytes(allocator, in_stream, size); return FormValue{ .ExprLoc = buf }; }, DW.FORM_flag => FormValue{ .Flag = (try in_stream.readByte()) != 0 }, DW.FORM_flag_present => FormValue{ .Flag = true }, DW.FORM_sec_offset => FormValue{ .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, u8), DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, u16), DW.FORM_ref4 => parseFormValueRef(allocator, in_stream, u32), DW.FORM_ref8 => parseFormValueRef(allocator, in_stream, u64), DW.FORM_ref_udata => { const ref_len = try readULeb128(in_stream); return parseFormValueRefLen(allocator, in_stream, ref_len); }, DW.FORM_ref_addr => FormValue{ .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_ref_sig8 => FormValue{ .RefSig8 = try in_stream.readIntLe(u64) }, DW.FORM_string => FormValue{ .String = try readStringRaw(allocator, in_stream) }, DW.FORM_strp => FormValue{ .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, DW.FORM_indirect => { const child_form_id = try readULeb128(in_stream); return parseFormValue(allocator, in_stream, child_form_id, is_64); }, else => error.InvalidDebugInfo, }; } fn parseAbbrevTable(di: *DwarfInfo) !AbbrevTable { var result = AbbrevTable.init(di.allocator()); while (true) { const abbrev_code = try readULeb128(di.dwarf_in_stream); if (abbrev_code == 0) return result; try result.append(AbbrevTableEntry{ .abbrev_code = abbrev_code, .tag_id = try readULeb128(di.dwarf_in_stream), .has_children = (try di.dwarf_in_stream.readByte()) == DW.CHILDREN_yes, .attrs = ArrayList(AbbrevAttr).init(di.allocator()), }); const attrs = &result.items[result.len - 1].attrs; while (true) { const attr_id = try readULeb128(di.dwarf_in_stream); const form_id = try readULeb128(di.dwarf_in_stream); if (attr_id == 0 and form_id == 0) break; try attrs.append(AbbrevAttr{ .attr_id = attr_id, .form_id = form_id, }); } } } /// Gets an already existing AbbrevTable given the abbrev_offset, or if not found, /// seeks in the stream and parses it. fn getAbbrevTable(di: *DwarfInfo, abbrev_offset: u64) !*const AbbrevTable { for (di.abbrev_table_list.toSlice()) |*header| { if (header.offset == abbrev_offset) { return &header.table; } } try di.dwarf_seekable_stream.seekTo(di.debug_abbrev.offset + abbrev_offset); try di.abbrev_table_list.append(AbbrevTableHeader{ .offset = abbrev_offset, .table = try parseAbbrevTable(di), }); return &di.abbrev_table_list.items[di.abbrev_table_list.len - 1].table; } fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*const AbbrevTableEntry { for (abbrev_table.toSliceConst()) |*table_entry| { if (table_entry.abbrev_code == abbrev_code) return table_entry; } return null; } fn parseDie(di: *DwarfInfo, abbrev_table: *const AbbrevTable, is_64: bool) !Die { const abbrev_code = try readULeb128(di.dwarf_in_stream); const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) orelse return error.InvalidDebugInfo; var result = Die{ .tag_id = table_entry.tag_id, .has_children = table_entry.has_children, .attrs = ArrayList(Die.Attr).init(di.allocator()), }; try result.attrs.resize(table_entry.attrs.len); for (table_entry.attrs.toSliceConst()) |attr, i| { result.attrs.items[i] = Die.Attr{ .id = attr.attr_id, .value = try parseFormValue(di.allocator(), di.dwarf_in_stream, attr.form_id, is_64), }; } return result; } fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: usize) !LineInfo { const ofile = symbol.ofile orelse return error.MissingDebugInfo; const gop = try di.ofiles.getOrPut(ofile); const mach_o_file = if (gop.found_existing) &gop.kv.value else blk: { errdefer _ = di.ofiles.remove(ofile); const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx); gop.kv.value = MachOFile{ .bytes = try std.io.readFileAllocAligned(di.ofiles.allocator, ofile_path, @alignOf(macho.mach_header_64)), .sect_debug_info = null, .sect_debug_line = null, }; const hdr = @ptrCast(*const macho.mach_header_64, gop.kv.value.bytes.ptr); if (hdr.magic != std.macho.MH_MAGIC_64) return error.InvalidDebugInfo; const hdr_base = @ptrCast([*]const u8, hdr); var ptr = hdr_base + @sizeOf(macho.mach_header_64); var ncmd: u32 = hdr.ncmds; const segcmd = while (ncmd != 0) : (ncmd -= 1) { const lc = @ptrCast(*const std.macho.load_command, ptr); switch (lc.cmd) { std.macho.LC_SEGMENT_64 => break @ptrCast(*const std.macho.segment_command_64, @alignCast(@alignOf(std.macho.segment_command_64), ptr)), else => {}, } ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/1403 } else { return error.MissingDebugInfo; }; const sections = @ptrCast([*]const macho.section_64, @alignCast(@alignOf(macho.section_64), ptr + @sizeOf(std.macho.segment_command_64)))[0..segcmd.nsects]; for (sections) |*sect| { if (sect.flags & macho.SECTION_TYPE == macho.S_REGULAR and (sect.flags & macho.SECTION_ATTRIBUTES) & macho.S_ATTR_DEBUG == macho.S_ATTR_DEBUG) { const sect_name = mem.toSliceConst(u8, &sect.sectname); if (mem.eql(u8, sect_name, "__debug_line")) { gop.kv.value.sect_debug_line = sect; } else if (mem.eql(u8, sect_name, "__debug_info")) { gop.kv.value.sect_debug_info = sect; } } } break :blk &gop.kv.value; }; const sect_debug_line = mach_o_file.sect_debug_line orelse return error.MissingDebugInfo; var ptr = mach_o_file.bytes.ptr + sect_debug_line.offset; var is_64: bool = undefined; const unit_length = try readInitialLengthMem(&ptr, &is_64); if (unit_length == 0) return error.MissingDebugInfo; const version = readIntMem(&ptr, u16, builtin.Endian.Little); // TODO support 3 and 5 if (version != 2 and version != 4) return error.InvalidDebugInfo; const prologue_length = if (is_64) readIntMem(&ptr, u64, builtin.Endian.Little) else readIntMem(&ptr, u32, builtin.Endian.Little); const prog_start = ptr + prologue_length; const minimum_instruction_length = readByteMem(&ptr); if (minimum_instruction_length == 0) return error.InvalidDebugInfo; if (version >= 4) { // maximum_operations_per_instruction ptr += 1; } const default_is_stmt = readByteMem(&ptr) != 0; const line_base = readByteSignedMem(&ptr); const line_range = readByteMem(&ptr); if (line_range == 0) return error.InvalidDebugInfo; const opcode_base = readByteMem(&ptr); const standard_opcode_lengths = ptr[0 .. opcode_base - 1]; ptr += opcode_base - 1; var include_directories = ArrayList([]const u8).init(di.allocator()); try include_directories.append(""); while (true) { const dir = readStringMem(&ptr); if (dir.len == 0) break; try include_directories.append(dir); } var file_entries = ArrayList(FileEntry).init(di.allocator()); var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(), &file_entries, target_address); while (true) { const file_name = readStringMem(&ptr); if (file_name.len == 0) break; const dir_index = try readULeb128Mem(&ptr); const mtime = try readULeb128Mem(&ptr); const len_bytes = try readULeb128Mem(&ptr); try file_entries.append(FileEntry{ .file_name = file_name, .dir_index = dir_index, .mtime = mtime, .len_bytes = len_bytes, }); } ptr = prog_start; while (true) { const opcode = readByteMem(&ptr); if (opcode == DW.LNS_extended_op) { const op_size = try readULeb128Mem(&ptr); if (op_size < 1) return error.InvalidDebugInfo; var sub_op = readByteMem(&ptr); switch (sub_op) { DW.LNE_end_sequence => { prog.end_sequence = true; if (try prog.checkLineMatch()) |info| return info; return error.MissingDebugInfo; }, DW.LNE_set_address => { const addr = readIntMem(&ptr, usize, builtin.Endian.Little); prog.address = symbol.reloc + addr; }, DW.LNE_define_file => { const file_name = readStringMem(&ptr); const dir_index = try readULeb128Mem(&ptr); const mtime = try readULeb128Mem(&ptr); const len_bytes = try readULeb128Mem(&ptr); try file_entries.append(FileEntry{ .file_name = file_name, .dir_index = dir_index, .mtime = mtime, .len_bytes = len_bytes, }); }, else => { ptr += op_size - 1; }, } } else if (opcode >= opcode_base) { // special opcodes const adjusted_opcode = opcode - opcode_base; const inc_addr = minimum_instruction_length * (adjusted_opcode / line_range); const inc_line = i32(line_base) + i32(adjusted_opcode % line_range); prog.line += inc_line; prog.address += inc_addr; if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; } else { switch (opcode) { DW.LNS_copy => { if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; }, DW.LNS_advance_pc => { const arg = try readULeb128Mem(&ptr); prog.address += arg * minimum_instruction_length; }, DW.LNS_advance_line => { const arg = try readILeb128Mem(&ptr); prog.line += arg; }, DW.LNS_set_file => { const arg = try readULeb128Mem(&ptr); prog.file = arg; }, DW.LNS_set_column => { const arg = try readULeb128Mem(&ptr); prog.column = arg; }, DW.LNS_negate_stmt => { prog.is_stmt = !prog.is_stmt; }, DW.LNS_set_basic_block => { prog.basic_block = true; }, DW.LNS_const_add_pc => { const inc_addr = minimum_instruction_length * ((255 - opcode_base) / line_range); prog.address += inc_addr; }, DW.LNS_fixed_advance_pc => { const arg = readIntMem(&ptr, u16, builtin.Endian.Little); prog.address += arg; }, DW.LNS_set_prologue_end => {}, else => { if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo; const len_bytes = standard_opcode_lengths[opcode - 1]; ptr += len_bytes; }, } } } return error.MissingDebugInfo; } fn getLineNumberInfoDwarf(di: *DwarfInfo, compile_unit: CompileUnit, target_address: usize) !LineInfo { const compile_unit_cwd = try compile_unit.die.getAttrString(di, DW.AT_comp_dir); const debug_line_end = di.debug_line.offset + di.debug_line.size; var this_offset = di.debug_line.offset; var this_index: usize = 0; while (this_offset < debug_line_end) : (this_index += 1) { try di.dwarf_seekable_stream.seekTo(this_offset); var is_64: bool = undefined; const unit_length = try readInitialLength(@typeOf(di.dwarf_in_stream.readFn).ReturnType.ErrorSet, di.dwarf_in_stream, &is_64); if (unit_length == 0) return error.MissingDebugInfo; const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); if (compile_unit.index != this_index) { this_offset += next_offset; continue; } const version = try di.dwarf_in_stream.readInt(di.endian, u16); // TODO support 3 and 5 if (version != 2 and version != 4) return error.InvalidDebugInfo; const prologue_length = if (is_64) try di.dwarf_in_stream.readInt(di.endian, u64) else try di.dwarf_in_stream.readInt(di.endian, u32); const prog_start_offset = (try di.dwarf_seekable_stream.getPos()) + prologue_length; const minimum_instruction_length = try di.dwarf_in_stream.readByte(); if (minimum_instruction_length == 0) return error.InvalidDebugInfo; if (version >= 4) { // maximum_operations_per_instruction _ = try di.dwarf_in_stream.readByte(); } const default_is_stmt = (try di.dwarf_in_stream.readByte()) != 0; const line_base = try di.dwarf_in_stream.readByteSigned(); const line_range = try di.dwarf_in_stream.readByte(); if (line_range == 0) return error.InvalidDebugInfo; const opcode_base = try di.dwarf_in_stream.readByte(); const standard_opcode_lengths = try di.allocator().alloc(u8, opcode_base - 1); { var i: usize = 0; while (i < opcode_base - 1) : (i += 1) { standard_opcode_lengths[i] = try di.dwarf_in_stream.readByte(); } } var include_directories = ArrayList([]u8).init(di.allocator()); try include_directories.append(compile_unit_cwd); while (true) { const dir = try di.readString(); if (dir.len == 0) break; try include_directories.append(dir); } var file_entries = ArrayList(FileEntry).init(di.allocator()); var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(), &file_entries, target_address); while (true) { const file_name = try di.readString(); if (file_name.len == 0) break; const dir_index = try readULeb128(di.dwarf_in_stream); const mtime = try readULeb128(di.dwarf_in_stream); const len_bytes = try readULeb128(di.dwarf_in_stream); try file_entries.append(FileEntry{ .file_name = file_name, .dir_index = dir_index, .mtime = mtime, .len_bytes = len_bytes, }); } try di.dwarf_seekable_stream.seekTo(prog_start_offset); while (true) { const opcode = try di.dwarf_in_stream.readByte(); if (opcode == DW.LNS_extended_op) { const op_size = try readULeb128(di.dwarf_in_stream); if (op_size < 1) return error.InvalidDebugInfo; var sub_op = try di.dwarf_in_stream.readByte(); switch (sub_op) { DW.LNE_end_sequence => { prog.end_sequence = true; if (try prog.checkLineMatch()) |info| return info; return error.MissingDebugInfo; }, DW.LNE_set_address => { const addr = try di.dwarf_in_stream.readInt(di.endian, usize); prog.address = addr; }, DW.LNE_define_file => { const file_name = try di.readString(); const dir_index = try readULeb128(di.dwarf_in_stream); const mtime = try readULeb128(di.dwarf_in_stream); const len_bytes = try readULeb128(di.dwarf_in_stream); try file_entries.append(FileEntry{ .file_name = file_name, .dir_index = dir_index, .mtime = mtime, .len_bytes = len_bytes, }); }, else => { const fwd_amt = math.cast(isize, op_size - 1) catch return error.InvalidDebugInfo; try di.dwarf_seekable_stream.seekForward(fwd_amt); }, } } else if (opcode >= opcode_base) { // special opcodes const adjusted_opcode = opcode - opcode_base; const inc_addr = minimum_instruction_length * (adjusted_opcode / line_range); const inc_line = i32(line_base) + i32(adjusted_opcode % line_range); prog.line += inc_line; prog.address += inc_addr; if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; } else { switch (opcode) { DW.LNS_copy => { if (try prog.checkLineMatch()) |info| return info; prog.basic_block = false; }, DW.LNS_advance_pc => { const arg = try readULeb128(di.dwarf_in_stream); prog.address += arg * minimum_instruction_length; }, DW.LNS_advance_line => { const arg = try readILeb128(di.dwarf_in_stream); prog.line += arg; }, DW.LNS_set_file => { const arg = try readULeb128(di.dwarf_in_stream); prog.file = arg; }, DW.LNS_set_column => { const arg = try readULeb128(di.dwarf_in_stream); prog.column = arg; }, DW.LNS_negate_stmt => { prog.is_stmt = !prog.is_stmt; }, DW.LNS_set_basic_block => { prog.basic_block = true; }, DW.LNS_const_add_pc => { const inc_addr = minimum_instruction_length * ((255 - opcode_base) / line_range); prog.address += inc_addr; }, DW.LNS_fixed_advance_pc => { const arg = try di.dwarf_in_stream.readInt(di.endian, u16); prog.address += arg; }, DW.LNS_set_prologue_end => {}, else => { if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo; const len_bytes = standard_opcode_lengths[opcode - 1]; try di.dwarf_seekable_stream.seekForward(len_bytes); }, } } } this_offset += next_offset; } return error.MissingDebugInfo; } fn scanAllCompileUnits(di: *DwarfInfo) !void { const debug_info_end = di.debug_info.offset + di.debug_info.size; var this_unit_offset = di.debug_info.offset; var cu_index: usize = 0; while (this_unit_offset < debug_info_end) { try di.dwarf_seekable_stream.seekTo(this_unit_offset); var is_64: bool = undefined; const unit_length = try readInitialLength(@typeOf(di.dwarf_in_stream.readFn).ReturnType.ErrorSet, di.dwarf_in_stream, &is_64); if (unit_length == 0) return; const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); const version = try di.dwarf_in_stream.readInt(di.endian, u16); if (version < 2 or version > 5) return error.InvalidDebugInfo; const debug_abbrev_offset = if (is_64) try di.dwarf_in_stream.readInt(di.endian, u64) else try di.dwarf_in_stream.readInt(di.endian, u32); const address_size = try di.dwarf_in_stream.readByte(); if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; const compile_unit_pos = try di.dwarf_seekable_stream.getPos(); const abbrev_table = try getAbbrevTable(di, debug_abbrev_offset); try di.dwarf_seekable_stream.seekTo(compile_unit_pos); const compile_unit_die = try di.allocator().create(try parseDie(di, abbrev_table, is_64)); if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo; const pc_range = x: { if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| { if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| { const pc_end = switch (high_pc_value.*) { FormValue.Address => |value| value, FormValue.Const => |value| b: { const offset = try value.asUnsignedLe(); break :b (low_pc + offset); }, else => return error.InvalidDebugInfo, }; break :x PcRange{ .start = low_pc, .end = pc_end, }; } else { break :x null; } } else |err| { if (err != error.MissingDebugInfo) return err; break :x null; } }; try di.compile_unit_list.append(CompileUnit{ .version = version, .is_64 = is_64, .pc_range = pc_range, .die = compile_unit_die, .index = cu_index, }); this_unit_offset += next_offset; cu_index += 1; } } fn findCompileUnit(di: *DwarfInfo, target_address: u64) !*const CompileUnit { for (di.compile_unit_list.toSlice()) |*compile_unit| { if (compile_unit.pc_range) |range| { if (target_address >= range.start and target_address < range.end) return compile_unit; } if (compile_unit.die.getAttrSecOffset(DW.AT_ranges)) |ranges_offset| { var base_address: usize = 0; if (di.debug_ranges) |debug_ranges| { try di.dwarf_seekable_stream.seekTo(debug_ranges.offset + ranges_offset); while (true) { const begin_addr = try di.dwarf_in_stream.readIntLe(usize); const end_addr = try di.dwarf_in_stream.readIntLe(usize); if (begin_addr == 0 and end_addr == 0) { break; } if (begin_addr == maxInt(usize)) { base_address = begin_addr; continue; } if (target_address >= begin_addr and target_address < end_addr) { return compile_unit; } } } } else |err| { if (err != error.MissingDebugInfo) return err; continue; } } return error.MissingDebugInfo; } fn readIntMem(ptr: *[*]const u8, comptime T: type, endian: builtin.Endian) T { const result = mem.readInt(ptr.*[0..@sizeOf(T)], T, endian); ptr.* += @sizeOf(T); return result; } fn readByteMem(ptr: *[*]const u8) u8 { const result = ptr.*[0]; ptr.* += 1; return result; } fn readByteSignedMem(ptr: *[*]const u8) i8 { return @bitCast(i8, readByteMem(ptr)); } fn readInitialLengthMem(ptr: *[*]const u8, is_64: *bool) !u64 { const first_32_bits = mem.readIntLE(u32, ptr.*[0..4]); is_64.* = (first_32_bits == 0xffffffff); if (is_64.*) { ptr.* += 4; const result = mem.readIntLE(u64, ptr.*[0..8]); ptr.* += 8; return result; } else { if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo; ptr.* += 4; return u64(first_32_bits); } } fn readStringMem(ptr: *[*]const u8) []const u8 { const result = mem.toSliceConst(u8, ptr.*); ptr.* += result.len + 1; return result; } fn readULeb128Mem(ptr: *[*]const u8) !u64 { var result: u64 = 0; var shift: usize = 0; var i: usize = 0; while (true) { const byte = ptr.*[i]; i += 1; var operand: u64 = undefined; if (@shlWithOverflow(u64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo; result |= operand; if ((byte & 0b10000000) == 0) { ptr.* += i; return result; } shift += 7; } } fn readILeb128Mem(ptr: *[*]const u8) !i64 { var result: i64 = 0; var shift: usize = 0; var i: usize = 0; while (true) { const byte = ptr.*[i]; i += 1; var operand: i64 = undefined; if (@shlWithOverflow(i64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo; result |= operand; shift += 7; if ((byte & 0b10000000) == 0) { if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << @intCast(u6, shift)); ptr.* += i; return result; } } } fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool) !u64 { const first_32_bits = try in_stream.readIntLe(u32); is_64.* = (first_32_bits == 0xffffffff); if (is_64.*) { return in_stream.readIntLe(u64); } else { if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo; return u64(first_32_bits); } } fn readULeb128(in_stream: var) !u64 { var result: u64 = 0; var shift: usize = 0; while (true) { const byte = try in_stream.readByte(); var operand: u64 = undefined; if (@shlWithOverflow(u64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo; result |= operand; if ((byte & 0b10000000) == 0) return result; shift += 7; } } fn readILeb128(in_stream: var) !i64 { var result: i64 = 0; var shift: usize = 0; while (true) { const byte = try in_stream.readByte(); var operand: i64 = undefined; if (@shlWithOverflow(i64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo; result |= operand; shift += 7; if ((byte & 0b10000000) == 0) { if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << @intCast(u6, shift)); return result; } } } /// This should only be used in temporary test programs. pub const global_allocator = &global_fixed_allocator.allocator; var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]); var global_allocator_mem: [100 * 1024]u8 = undefined; /// TODO multithreaded awareness var debug_info_allocator: ?*mem.Allocator = null; var debug_info_direct_allocator: std.heap.DirectAllocator = undefined; var debug_info_arena_allocator: std.heap.ArenaAllocator = undefined; fn getDebugInfoAllocator() *mem.Allocator { if (debug_info_allocator) |a| return a; debug_info_direct_allocator = std.heap.DirectAllocator.init(); debug_info_arena_allocator = std.heap.ArenaAllocator.init(&debug_info_direct_allocator.allocator); debug_info_allocator = &debug_info_arena_allocator.allocator; return &debug_info_arena_allocator.allocator; }
const std = @import("std"); const Cpu = @import("cpu.zig").Cpu; const Precision = @import("console.zig").Precision; pub fn Addressing(comptime precision: Precision) type { return switch (precision) { .fast => enum { accumulator, absolute, absoluteX, absoluteY, immediate, implied, indirect, indirectX, indirectY, relative, zeroPage, zeroPageX, zeroPageY, pub fn op_size(self: @This()) u2 { return switch (self) { .accumulator => 1, .absolute => 3, .absoluteX => 3, .absoluteY => 3, .immediate => 2, .implied => 1, .indirect => 3, .indirectX => 2, .indirectY => 2, .relative => 2, .zeroPage => 2, .zeroPageX => 2, .zeroPageY => 2, }; } }, .accurate => enum { special, absolute, absoluteX, absoluteY, immediate, implied, indirectX, indirectY, relative, zeroPage, zeroPageX, zeroPageY, }, }; } pub fn Instruction(comptime precision: Precision) type { switch (precision) { .fast => return struct { op: Op(.fast), addressing: Addressing(.fast), cycles: u3, var_cycles: bool = false, pub const decode = decodeFast; }, .accurate => return struct { op: Op(.accurate), addressing: Addressing(.accurate), access: Access, pub const decode = decodeAccurate; }, } } // TODO: lut? fn decodeFast(opcode: u8) Instruction(.fast) { const OpFast = Op(.fast); const AddrFast = Addressing(.fast); return switch (opcode) { 0x69 => .{ .op = OpFast.op_adc, .addressing = AddrFast.immediate, .cycles = 2 }, 0x65 => .{ .op = OpFast.op_adc, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x75 => .{ .op = OpFast.op_adc, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0x6d => .{ .op = OpFast.op_adc, .addressing = AddrFast.absolute, .cycles = 4 }, 0x7d => .{ .op = OpFast.op_adc, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0x79 => .{ .op = OpFast.op_adc, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0x61 => .{ .op = OpFast.op_adc, .addressing = AddrFast.indirectX, .cycles = 6 }, 0x71 => .{ .op = OpFast.op_adc, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0x29 => .{ .op = OpFast.op_and, .addressing = AddrFast.immediate, .cycles = 2 }, 0x25 => .{ .op = OpFast.op_and, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x35 => .{ .op = OpFast.op_and, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0x2d => .{ .op = OpFast.op_and, .addressing = AddrFast.absolute, .cycles = 4 }, 0x3d => .{ .op = OpFast.op_and, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0x39 => .{ .op = OpFast.op_and, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0x21 => .{ .op = OpFast.op_and, .addressing = AddrFast.indirectX, .cycles = 6 }, 0x31 => .{ .op = OpFast.op_and, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0x0a => .{ .op = OpFast.op_asl, .addressing = AddrFast.accumulator, .cycles = 2 }, 0x06 => .{ .op = OpFast.op_asl, .addressing = AddrFast.zeroPage, .cycles = 5 }, 0x16 => .{ .op = OpFast.op_asl, .addressing = AddrFast.zeroPageX, .cycles = 6 }, 0x0e => .{ .op = OpFast.op_asl, .addressing = AddrFast.absolute, .cycles = 6 }, 0x1e => .{ .op = OpFast.op_asl, .addressing = AddrFast.absoluteX, .cycles = 7 }, 0x10 => .{ .op = OpFast.op_bpl, .addressing = AddrFast.relative, .cycles = 2 }, 0x30 => .{ .op = OpFast.op_bmi, .addressing = AddrFast.relative, .cycles = 2 }, 0x50 => .{ .op = OpFast.op_bvc, .addressing = AddrFast.relative, .cycles = 2 }, 0x70 => .{ .op = OpFast.op_bvs, .addressing = AddrFast.relative, .cycles = 2 }, 0x90 => .{ .op = OpFast.op_bcc, .addressing = AddrFast.relative, .cycles = 2 }, 0xb0 => .{ .op = OpFast.op_bcs, .addressing = AddrFast.relative, .cycles = 2 }, 0xd0 => .{ .op = OpFast.op_bne, .addressing = AddrFast.relative, .cycles = 2 }, 0xf0 => .{ .op = OpFast.op_beq, .addressing = AddrFast.relative, .cycles = 2 }, 0x24 => .{ .op = OpFast.op_bit, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x2c => .{ .op = OpFast.op_bit, .addressing = AddrFast.absolute, .cycles = 4 }, 0x00 => .{ .op = OpFast.op_brk, .addressing = AddrFast.implied, .cycles = 7 }, 0x18 => .{ .op = OpFast.op_clc, .addressing = AddrFast.implied, .cycles = 2 }, 0xd8 => .{ .op = OpFast.op_cld, .addressing = AddrFast.implied, .cycles = 2 }, 0x58 => .{ .op = OpFast.op_cli, .addressing = AddrFast.implied, .cycles = 2 }, 0xb8 => .{ .op = OpFast.op_clv, .addressing = AddrFast.implied, .cycles = 2 }, 0xc9 => .{ .op = OpFast.op_cmp, .addressing = AddrFast.immediate, .cycles = 2 }, 0xc5 => .{ .op = OpFast.op_cmp, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xd5 => .{ .op = OpFast.op_cmp, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0xcd => .{ .op = OpFast.op_cmp, .addressing = AddrFast.absolute, .cycles = 4 }, 0xdd => .{ .op = OpFast.op_cmp, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0xd9 => .{ .op = OpFast.op_cmp, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0xc1 => .{ .op = OpFast.op_cmp, .addressing = AddrFast.indirectX, .cycles = 6 }, 0xd1 => .{ .op = OpFast.op_cmp, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0xe0 => .{ .op = OpFast.op_cpx, .addressing = AddrFast.immediate, .cycles = 2 }, 0xe4 => .{ .op = OpFast.op_cpx, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xec => .{ .op = OpFast.op_cpx, .addressing = AddrFast.absolute, .cycles = 4 }, 0xc0 => .{ .op = OpFast.op_cpy, .addressing = AddrFast.immediate, .cycles = 2 }, 0xc4 => .{ .op = OpFast.op_cpy, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xcc => .{ .op = OpFast.op_cpy, .addressing = AddrFast.absolute, .cycles = 4 }, 0xc6 => .{ .op = OpFast.op_dec, .addressing = AddrFast.zeroPage, .cycles = 5 }, 0xd6 => .{ .op = OpFast.op_dec, .addressing = AddrFast.zeroPageX, .cycles = 6 }, 0xce => .{ .op = OpFast.op_dec, .addressing = AddrFast.absolute, .cycles = 6 }, 0xde => .{ .op = OpFast.op_dec, .addressing = AddrFast.absoluteX, .cycles = 7 }, 0xca => .{ .op = OpFast.op_dex, .addressing = AddrFast.implied, .cycles = 2 }, 0x88 => .{ .op = OpFast.op_dey, .addressing = AddrFast.implied, .cycles = 2 }, 0x49 => .{ .op = OpFast.op_eor, .addressing = AddrFast.immediate, .cycles = 2 }, 0x45 => .{ .op = OpFast.op_eor, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x55 => .{ .op = OpFast.op_eor, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0x4d => .{ .op = OpFast.op_eor, .addressing = AddrFast.absolute, .cycles = 4 }, 0x5d => .{ .op = OpFast.op_eor, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0x59 => .{ .op = OpFast.op_eor, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0x41 => .{ .op = OpFast.op_eor, .addressing = AddrFast.indirectX, .cycles = 6 }, 0x51 => .{ .op = OpFast.op_eor, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0xe6 => .{ .op = OpFast.op_inc, .addressing = AddrFast.zeroPage, .cycles = 5 }, 0xf6 => .{ .op = OpFast.op_inc, .addressing = AddrFast.zeroPageX, .cycles = 6 }, 0xee => .{ .op = OpFast.op_inc, .addressing = AddrFast.absolute, .cycles = 6 }, 0xfe => .{ .op = OpFast.op_inc, .addressing = AddrFast.absoluteX, .cycles = 7 }, 0xe8 => .{ .op = OpFast.op_inx, .addressing = AddrFast.implied, .cycles = 2 }, 0xc8 => .{ .op = OpFast.op_iny, .addressing = AddrFast.implied, .cycles = 2 }, 0x4c => .{ .op = OpFast.op_jmp, .addressing = AddrFast.absolute, .cycles = 3 }, 0x6c => .{ .op = OpFast.op_jmp, .addressing = AddrFast.indirect, .cycles = 5 }, 0x20 => .{ .op = OpFast.op_jsr, .addressing = AddrFast.absolute, .cycles = 6 }, 0xa9 => .{ .op = OpFast.op_lda, .addressing = AddrFast.immediate, .cycles = 2 }, 0xa5 => .{ .op = OpFast.op_lda, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xb5 => .{ .op = OpFast.op_lda, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0xad => .{ .op = OpFast.op_lda, .addressing = AddrFast.absolute, .cycles = 4 }, 0xbd => .{ .op = OpFast.op_lda, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0xb9 => .{ .op = OpFast.op_lda, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0xa1 => .{ .op = OpFast.op_lda, .addressing = AddrFast.indirectX, .cycles = 6 }, 0xb1 => .{ .op = OpFast.op_lda, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0xa2 => .{ .op = OpFast.op_ldx, .addressing = AddrFast.immediate, .cycles = 2 }, 0xa6 => .{ .op = OpFast.op_ldx, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xb6 => .{ .op = OpFast.op_ldx, .addressing = AddrFast.zeroPageY, .cycles = 4 }, 0xae => .{ .op = OpFast.op_ldx, .addressing = AddrFast.absolute, .cycles = 4 }, 0xbe => .{ .op = OpFast.op_ldx, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0xa0 => .{ .op = OpFast.op_ldy, .addressing = AddrFast.immediate, .cycles = 2 }, 0xa4 => .{ .op = OpFast.op_ldy, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xb4 => .{ .op = OpFast.op_ldy, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0xac => .{ .op = OpFast.op_ldy, .addressing = AddrFast.absolute, .cycles = 4 }, 0xbc => .{ .op = OpFast.op_ldy, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0x4a => .{ .op = OpFast.op_lsr, .addressing = AddrFast.accumulator, .cycles = 2 }, 0x46 => .{ .op = OpFast.op_lsr, .addressing = AddrFast.zeroPage, .cycles = 5 }, 0x56 => .{ .op = OpFast.op_lsr, .addressing = AddrFast.zeroPageX, .cycles = 6 }, 0x4e => .{ .op = OpFast.op_lsr, .addressing = AddrFast.absolute, .cycles = 6 }, 0x5e => .{ .op = OpFast.op_lsr, .addressing = AddrFast.absoluteX, .cycles = 7 }, 0xea => .{ .op = OpFast.op_nop, .addressing = AddrFast.implied, .cycles = 2 }, 0x09 => .{ .op = OpFast.op_ora, .addressing = AddrFast.immediate, .cycles = 2 }, 0x05 => .{ .op = OpFast.op_ora, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x15 => .{ .op = OpFast.op_ora, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0x0d => .{ .op = OpFast.op_ora, .addressing = AddrFast.absolute, .cycles = 4 }, 0x1d => .{ .op = OpFast.op_ora, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0x19 => .{ .op = OpFast.op_ora, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0x01 => .{ .op = OpFast.op_ora, .addressing = AddrFast.indirectX, .cycles = 6 }, 0x11 => .{ .op = OpFast.op_ora, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0x48 => .{ .op = OpFast.op_pha, .addressing = AddrFast.implied, .cycles = 3 }, 0x08 => .{ .op = OpFast.op_php, .addressing = AddrFast.implied, .cycles = 3 }, 0x68 => .{ .op = OpFast.op_pla, .addressing = AddrFast.implied, .cycles = 4 }, 0x28 => .{ .op = OpFast.op_plp, .addressing = AddrFast.implied, .cycles = 4 }, 0x2a => .{ .op = OpFast.op_rol, .addressing = AddrFast.accumulator, .cycles = 2 }, 0x26 => .{ .op = OpFast.op_rol, .addressing = AddrFast.zeroPage, .cycles = 5 }, 0x36 => .{ .op = OpFast.op_rol, .addressing = AddrFast.zeroPageX, .cycles = 6 }, 0x2e => .{ .op = OpFast.op_rol, .addressing = AddrFast.absolute, .cycles = 6 }, 0x3e => .{ .op = OpFast.op_rol, .addressing = AddrFast.absoluteX, .cycles = 7 }, 0x6a => .{ .op = OpFast.op_ror, .addressing = AddrFast.accumulator, .cycles = 2 }, 0x66 => .{ .op = OpFast.op_ror, .addressing = AddrFast.zeroPage, .cycles = 5 }, 0x76 => .{ .op = OpFast.op_ror, .addressing = AddrFast.zeroPageX, .cycles = 6 }, 0x6e => .{ .op = OpFast.op_ror, .addressing = AddrFast.absolute, .cycles = 6 }, 0x7e => .{ .op = OpFast.op_ror, .addressing = AddrFast.absoluteX, .cycles = 7 }, 0x40 => .{ .op = OpFast.op_rti, .addressing = AddrFast.implied, .cycles = 6 }, 0x60 => .{ .op = OpFast.op_rts, .addressing = AddrFast.implied, .cycles = 6 }, 0xe9 => .{ .op = OpFast.op_sbc, .addressing = AddrFast.immediate, .cycles = 2 }, 0xe5 => .{ .op = OpFast.op_sbc, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0xf5 => .{ .op = OpFast.op_sbc, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0xed => .{ .op = OpFast.op_sbc, .addressing = AddrFast.absolute, .cycles = 4 }, 0xfd => .{ .op = OpFast.op_sbc, .addressing = AddrFast.absoluteX, .cycles = 4, .var_cycles = true }, 0xf9 => .{ .op = OpFast.op_sbc, .addressing = AddrFast.absoluteY, .cycles = 4, .var_cycles = true }, 0xe1 => .{ .op = OpFast.op_sbc, .addressing = AddrFast.indirectX, .cycles = 6 }, 0xf1 => .{ .op = OpFast.op_sbc, .addressing = AddrFast.indirectY, .cycles = 5, .var_cycles = true }, 0x38 => .{ .op = OpFast.op_sec, .addressing = AddrFast.implied, .cycles = 2 }, 0xf8 => .{ .op = OpFast.op_sed, .addressing = AddrFast.implied, .cycles = 2 }, 0x78 => .{ .op = OpFast.op_sei, .addressing = AddrFast.implied, .cycles = 2 }, 0x85 => .{ .op = OpFast.op_sta, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x95 => .{ .op = OpFast.op_sta, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0x8d => .{ .op = OpFast.op_sta, .addressing = AddrFast.absolute, .cycles = 4 }, 0x9d => .{ .op = OpFast.op_sta, .addressing = AddrFast.absoluteX, .cycles = 5 }, 0x99 => .{ .op = OpFast.op_sta, .addressing = AddrFast.absoluteY, .cycles = 5 }, 0x81 => .{ .op = OpFast.op_sta, .addressing = AddrFast.indirectX, .cycles = 6 }, 0x91 => .{ .op = OpFast.op_sta, .addressing = AddrFast.indirectY, .cycles = 6 }, 0x86 => .{ .op = OpFast.op_stx, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x96 => .{ .op = OpFast.op_stx, .addressing = AddrFast.zeroPageY, .cycles = 4 }, 0x8e => .{ .op = OpFast.op_stx, .addressing = AddrFast.absolute, .cycles = 4 }, 0x84 => .{ .op = OpFast.op_sty, .addressing = AddrFast.zeroPage, .cycles = 3 }, 0x94 => .{ .op = OpFast.op_sty, .addressing = AddrFast.zeroPageX, .cycles = 4 }, 0x8c => .{ .op = OpFast.op_sty, .addressing = AddrFast.absolute, .cycles = 4 }, 0xaa => .{ .op = OpFast.op_tax, .addressing = AddrFast.implied, .cycles = 2 }, 0xa8 => .{ .op = OpFast.op_tay, .addressing = AddrFast.implied, .cycles = 2 }, 0xba => .{ .op = OpFast.op_tsx, .addressing = AddrFast.implied, .cycles = 2 }, 0x8a => .{ .op = OpFast.op_txa, .addressing = AddrFast.implied, .cycles = 2 }, 0x9a => .{ .op = OpFast.op_txs, .addressing = AddrFast.implied, .cycles = 2 }, 0x98 => .{ .op = OpFast.op_tya, .addressing = AddrFast.implied, .cycles = 2 }, else => .{ .op = OpFast.op_ill, .addressing = AddrFast.implied, .cycles = 0 }, }; } const op_lut = [256]Op(.accurate){ .op_brk, .op_ora, .op_kil, .op_slo, .op_nop, .op_ora, .op_asl, .op_slo, .op_php, .op_ora, .op_asl, .op_anc, .op_nop, .op_ora, .op_asl, .op_slo, .op_bpl, .op_ora, .op_kil, .op_slo, .op_nop, .op_ora, .op_asl, .op_slo, .op_clc, .op_ora, .op_nop, .op_slo, .op_nop, .op_ora, .op_asl, .op_slo, .op_jsr, .op_and, .op_kil, .op_rla, .op_bit, .op_and, .op_rol, .op_rla, .op_plp, .op_and, .op_rol, .op_anc, .op_bit, .op_and, .op_rol, .op_rla, .op_bmi, .op_and, .op_kil, .op_rla, .op_nop, .op_and, .op_rol, .op_rla, .op_sec, .op_and, .op_nop, .op_rla, .op_nop, .op_and, .op_rol, .op_rla, .op_rti, .op_eor, .op_kil, .op_sre, .op_nop, .op_eor, .op_lsr, .op_sre, .op_pha, .op_eor, .op_lsr, .op_alr, .op_jmp, .op_eor, .op_lsr, .op_sre, .op_bvc, .op_eor, .op_kil, .op_sre, .op_nop, .op_eor, .op_lsr, .op_sre, .op_cli, .op_eor, .op_nop, .op_sre, .op_nop, .op_eor, .op_lsr, .op_sre, .op_rts, .op_adc, .op_kil, .op_rra, .op_nop, .op_adc, .op_ror, .op_rra, .op_pla, .op_adc, .op_ror, .op_arr, .op_jmp, .op_adc, .op_ror, .op_rra, .op_bvs, .op_adc, .op_kil, .op_rra, .op_nop, .op_adc, .op_ror, .op_rra, .op_sei, .op_adc, .op_nop, .op_rra, .op_nop, .op_adc, .op_ror, .op_rra, .op_nop, .op_sta, .op_nop, .op_sax, .op_sty, .op_sta, .op_stx, .op_sax, .op_dey, .op_nop, .op_txa, .op_xaa, .op_sty, .op_sta, .op_stx, .op_sax, .op_bcc, .op_sta, .op_kil, .op_ahx, .op_sty, .op_sta, .op_stx, .op_sax, .op_tya, .op_sta, .op_txs, .op_tas, .op_shy, .op_sta, .op_shx, .op_ahx, .op_ldy, .op_lda, .op_ldx, .op_lax, .op_ldy, .op_lda, .op_ldx, .op_lax, .op_tay, .op_lda, .op_tax, .op_lax, .op_ldy, .op_lda, .op_ldx, .op_lax, .op_bcs, .op_lda, .op_kil, .op_lax, .op_ldy, .op_lda, .op_ldx, .op_lax, .op_clv, .op_lda, .op_tsx, .op_las, .op_ldy, .op_lda, .op_ldx, .op_lax, .op_cpy, .op_cmp, .op_nop, .op_dcp, .op_cpy, .op_cmp, .op_dec, .op_dcp, .op_iny, .op_cmp, .op_dex, .op_axs, .op_cpy, .op_cmp, .op_dec, .op_dcp, .op_bne, .op_cmp, .op_kil, .op_dcp, .op_nop, .op_cmp, .op_dec, .op_dcp, .op_cld, .op_cmp, .op_nop, .op_dcp, .op_nop, .op_cmp, .op_dec, .op_dcp, .op_cpx, .op_sbc, .op_nop, .op_isc, .op_cpx, .op_sbc, .op_inc, .op_isc, .op_inx, .op_sbc, .op_nop, .op_sbc, .op_cpx, .op_sbc, .op_inc, .op_isc, .op_beq, .op_sbc, .op_kil, .op_isc, .op_nop, .op_sbc, .op_inc, .op_isc, .op_sed, .op_sbc, .op_nop, .op_isc, .op_nop, .op_sbc, .op_inc, .op_isc, }; const addr_lut = blk: { const intToEnum = [12]Addressing(.accurate){ .special, // = 0 .absolute, // = 1 .absoluteX, // = 2 .absoluteY, // = 3 .immediate, // = 4 .implied, // = 5 .indirectX, // = 6 .indirectY, // = 7 .relative, // = 8 .zeroPage, // = 9 .zeroPageX, // = 10 .zeroPageY, // = 11 }; const numbered = [256]comptime_int{ 0, 6, 5, 6, 9, 9, 9, 9, 0, 4, 5, 4, 1, 1, 1, 1, 8, 7, 5, 7, 10, 10, 10, 10, 5, 3, 5, 3, 2, 2, 2, 2, 0, 6, 5, 6, 9, 9, 9, 9, 0, 4, 5, 4, 1, 1, 1, 1, 8, 7, 5, 7, 10, 10, 10, 10, 5, 3, 5, 3, 2, 2, 2, 2, 0, 6, 5, 6, 9, 9, 9, 9, 0, 4, 5, 4, 0, 1, 1, 1, 8, 7, 5, 7, 10, 10, 10, 10, 5, 3, 5, 3, 2, 2, 2, 2, 0, 6, 5, 6, 9, 9, 9, 9, 0, 4, 5, 4, 0, 1, 1, 1, 8, 7, 5, 7, 10, 10, 10, 10, 5, 3, 5, 3, 2, 2, 2, 2, 4, 6, 4, 6, 9, 9, 9, 9, 5, 4, 5, 4, 1, 1, 1, 1, 8, 7, 5, 7, 10, 10, 11, 11, 5, 3, 5, 3, 2, 2, 3, 3, 4, 6, 4, 6, 9, 9, 9, 9, 5, 4, 5, 4, 1, 1, 1, 1, 8, 7, 5, 7, 10, 10, 11, 11, 5, 3, 5, 3, 2, 2, 3, 3, 4, 6, 4, 6, 9, 9, 9, 9, 5, 4, 5, 4, 1, 1, 1, 1, 8, 7, 5, 7, 10, 10, 10, 10, 5, 3, 5, 3, 2, 2, 2, 2, 4, 6, 4, 6, 9, 9, 9, 9, 5, 4, 5, 4, 1, 1, 1, 1, 8, 7, 5, 7, 10, 10, 10, 10, 5, 3, 5, 3, 2, 2, 2, 2, }; var result = [1]Addressing(.accurate){undefined} ** 256; for (numbered) |n, i| { result[i] = intToEnum[n]; } break :blk result; }; /// Most instructions fall into the category of /// read only, read-modify-write, or read-write pub const Access = enum { special, read, rmw, write, }; const access_lut = blk: { @setEvalBranchQuota(2000); var result = [1]Access{undefined} ** 256; for (result) |*r, i| { const access = switch (addr_lut[i]) { .special => .special, .implied, .immediate, .relative => .read, else => switch (op_lut[i]) { .op_jmp => .special, .op_lda, .op_ldx, .op_ldy, .op_eor, .op_and, .op_ora, .op_adc => .read, .op_sbc, .op_cmp, .op_cpx, .op_cpy, .op_bit, .op_lax, .op_nop => .read, .op_las, .op_tas => .read, .op_asl, .op_lsr, .op_rol, .op_ror, .op_inc, .op_dec, .op_slo => .rmw, .op_sre, .op_rla, .op_rra, .op_isc, .op_dcp => .rmw, .op_sta, .op_stx, .op_sty, .op_sax, .op_ahx, .op_shx, .op_shy => .write, else => @compileError("Haven't implemented op " ++ opToString(op_lut[i])), }, }; r.* = access; } break :blk result; }; fn decodeAccurate(opcode: u8) Instruction(.accurate) { return Instruction(.accurate){ .op = op_lut[opcode], .addressing = addr_lut[opcode], .access = access_lut[opcode], }; } /// Merge T2 fields into T1 /// Surely there must be a stdlib function for this? fn MergedEnum(comptime T1: type, comptime T2: type) type { const type_info1 = @typeInfo(T1); const type_info2 = @typeInfo(T2); const total_fields = type_info1.Enum.fields.len + type_info2.Enum.fields.len; var fields = [1]std.builtin.TypeInfo.EnumField{undefined} ** total_fields; var i: usize = 0; for (type_info1.Enum.fields) |field| { fields[i] = .{ .name = field.name, .value = i }; i += 1; } for (type_info2.Enum.fields) |field| { fields[i] = .{ .name = field.name, .value = i }; i += 1; } const bits = std.math.log2_int_ceil(usize, total_fields); return @Type(.{ .Enum = .{ .layout = .Auto, .tag_type = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = bits } }), .fields = fields[0..], .decls = ([0]std.builtin.TypeInfo.Declaration{})[0..], .is_exhaustive = true, } }); } pub fn opToString(op: anytype) []const u8 { const enum_strs = comptime blk: { const enum_fields = @typeInfo(@TypeOf(op)).Enum.fields; var arr = [_]([3]u8){undefined} ** enum_fields.len; for (arr) |*idx, i| { _ = std.ascii.upperString(idx[0..], enum_fields[i].name[3..]); } break :blk arr; }; return enum_strs[@enumToInt(op)][0..]; } pub fn Op(comptime precision: Precision) type { const Ill = enum { op_ill, }; // zig fmt: off const Documented = enum { op_adc, op_and, op_asl, op_bpl, op_bmi, op_bvc, op_bvs, op_bcc, op_bcs, op_bne, op_beq, op_bit, op_brk, op_clc, op_cld, op_cli, op_clv, op_cmp, op_cpx, op_cpy, op_dec, op_dex, op_dey, op_eor, op_inc, op_inx, op_iny, op_jmp, op_jsr, op_lda, op_ldx, op_ldy, op_lsr, op_nop, op_ora, op_pha, op_php, op_pla, op_plp, op_rol, op_ror, op_rti, op_rts, op_sbc, op_sec, op_sed, op_sei, op_sta, op_stx, op_sty, op_tax, op_tay, op_tsx, op_txa, op_txs, op_tya, }; // zig fmt: on const Undocumented = enum { /// SAH, SHA op_ahx, /// ASR op_alr, /// AAC op_anc, op_arr, /// SBX op_axs, /// DCM op_dcp, /// INS, ISB op_isc, op_kil, /// LAE, LAR, AST op_las, op_lax, op_rla, op_rra, /// AAX, AXS op_sax, /// SXA, SXH, XAS op_shx, op_shy, /// ASO op_slo, /// LSE op_sre, /// SHS, SSH, XAS op_tas, /// ANE, AXA op_xaa, }; switch (precision) { .fast => return MergedEnum(Ill, Documented), .accurate => return MergedEnum(Documented, Undocumented), } }
const Atom = @This(); const std = @import("std"); const build_options = @import("build_options"); const aarch64 = @import("../../arch/aarch64/bits.zig"); const assert = std.debug.assert; const log = std.log.scoped(.link); const macho = std.macho; const math = std.math; const mem = std.mem; const meta = std.meta; const trace = @import("../../tracy.zig").trace; const Allocator = mem.Allocator; const Arch = std.Target.Cpu.Arch; const MachO = @import("../MachO.zig"); const Object = @import("Object.zig"); const StringIndexAdapter = std.hash_map.StringIndexAdapter; /// Each decl always gets a local symbol with the fully qualified name. /// The vaddr and size are found here directly. /// The file offset is found by computing the vaddr offset from the section vaddr /// the symbol references, and adding that to the file offset of the section. /// If this field is 0, it means the codegen size = 0 and there is no symbol or /// offset table entry. local_sym_index: u32, /// List of symbol aliases pointing to the same atom via different nlists aliases: std.ArrayListUnmanaged(u32) = .{}, /// List of symbols contained within this atom contained: std.ArrayListUnmanaged(SymbolAtOffset) = .{}, /// Code (may be non-relocated) this atom represents code: std.ArrayListUnmanaged(u8) = .{}, /// Size and alignment of this atom /// Unlike in Elf, we need to store the size of this symbol as part of /// the atom since macho.nlist_64 lacks this information. size: u64, /// Alignment of this atom as a power of 2. /// For instance, alignment of 0 should be read as 2^0 = 1 byte aligned. alignment: u32, /// List of relocations belonging to this atom. relocs: std.ArrayListUnmanaged(Relocation) = .{}, /// List of offsets contained within this atom that need rebasing by the dynamic /// loader in presence of ASLR. rebases: std.ArrayListUnmanaged(u64) = .{}, /// List of offsets contained within this atom that will be dynamically bound /// by the dynamic loader and contain pointers to resolved (at load time) extern /// symbols (aka proxies aka imports) bindings: std.ArrayListUnmanaged(Binding) = .{}, /// List of lazy bindings lazy_bindings: std.ArrayListUnmanaged(Binding) = .{}, /// List of data-in-code entries. This is currently specific to x86_64 only. dices: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{}, /// Stab entry for this atom. This is currently specific to a binary created /// by linking object files in a traditional sense - in incremental sense, we /// bypass stabs altogether to produce dSYM bundle directly with fully relocated /// DWARF sections. stab: ?Stab = null, /// Points to the previous and next neighbours next: ?*Atom, prev: ?*Atom, /// Previous/next linked list pointers. /// This is the linked list node for this Decl's corresponding .debug_info tag. dbg_info_prev: ?*Atom, dbg_info_next: ?*Atom, /// Offset into .debug_info pointing to the tag for this Decl. dbg_info_off: u32, /// Size of the .debug_info tag for this Decl, not including padding. dbg_info_len: u32, dirty: bool = true, pub const Binding = struct { n_strx: u32, offset: u64, }; pub const SymbolAtOffset = struct { local_sym_index: u32, offset: u64, stab: ?Stab = null, }; pub const Stab = union(enum) { function: u64, static, global, pub fn asNlists(stab: Stab, local_sym_index: u32, macho_file: anytype) ![]macho.nlist_64 { var nlists = std.ArrayList(macho.nlist_64).init(macho_file.base.allocator); defer nlists.deinit(); const sym = macho_file.locals.items[local_sym_index]; switch (stab) { .function => |size| { try nlists.ensureUnusedCapacity(4); nlists.appendAssumeCapacity(.{ .n_strx = 0, .n_type = macho.N_BNSYM, .n_sect = sym.n_sect, .n_desc = 0, .n_value = sym.n_value, }); nlists.appendAssumeCapacity(.{ .n_strx = sym.n_strx, .n_type = macho.N_FUN, .n_sect = sym.n_sect, .n_desc = 0, .n_value = sym.n_value, }); nlists.appendAssumeCapacity(.{ .n_strx = 0, .n_type = macho.N_FUN, .n_sect = 0, .n_desc = 0, .n_value = size, }); nlists.appendAssumeCapacity(.{ .n_strx = 0, .n_type = macho.N_ENSYM, .n_sect = sym.n_sect, .n_desc = 0, .n_value = size, }); }, .global => { try nlists.append(.{ .n_strx = sym.n_strx, .n_type = macho.N_GSYM, .n_sect = 0, .n_desc = 0, .n_value = 0, }); }, .static => { try nlists.append(.{ .n_strx = sym.n_strx, .n_type = macho.N_STSYM, .n_sect = sym.n_sect, .n_desc = 0, .n_value = sym.n_value, }); }, } return nlists.toOwnedSlice(); } }; pub const Relocation = struct { pub const Target = union(enum) { local: u32, global: u32, }; /// Offset within the atom's code buffer. /// Note relocation size can be inferred by relocation's kind. offset: u32, target: Target, addend: i64, subtractor: ?u32, pcrel: bool, length: u2, @"type": u4, }; pub const empty = Atom{ .local_sym_index = 0, .size = 0, .alignment = 0, .prev = null, .next = null, .dbg_info_prev = null, .dbg_info_next = null, .dbg_info_off = undefined, .dbg_info_len = undefined, }; pub fn deinit(self: *Atom, allocator: Allocator) void { self.dices.deinit(allocator); self.lazy_bindings.deinit(allocator); self.bindings.deinit(allocator); self.rebases.deinit(allocator); self.relocs.deinit(allocator); self.contained.deinit(allocator); self.aliases.deinit(allocator); self.code.deinit(allocator); } pub fn clearRetainingCapacity(self: *Atom) void { self.dices.clearRetainingCapacity(); self.lazy_bindings.clearRetainingCapacity(); self.bindings.clearRetainingCapacity(); self.rebases.clearRetainingCapacity(); self.relocs.clearRetainingCapacity(); self.contained.clearRetainingCapacity(); self.aliases.clearRetainingCapacity(); self.code.clearRetainingCapacity(); } /// Returns how much room there is to grow in virtual address space. /// File offset relocation happens transparently, so it is not included in /// this calculation. pub fn capacity(self: Atom, macho_file: MachO) u64 { const self_sym = macho_file.locals.items[self.local_sym_index]; if (self.next) |next| { const next_sym = macho_file.locals.items[next.local_sym_index]; return next_sym.n_value - self_sym.n_value; } else { // We are the last atom. // The capacity is limited only by virtual address space. return std.math.maxInt(u64) - self_sym.n_value; } } pub fn freeListEligible(self: Atom, macho_file: MachO) bool { // No need to keep a free list node for the last atom. const next = self.next orelse return false; const self_sym = macho_file.locals.items[self.local_sym_index]; const next_sym = macho_file.locals.items[next.local_sym_index]; const cap = next_sym.n_value - self_sym.n_value; const ideal_cap = MachO.padToIdeal(self.size); if (cap <= ideal_cap) return false; const surplus = cap - ideal_cap; return surplus >= MachO.min_text_capacity; } const RelocContext = struct { base_addr: u64 = 0, allocator: Allocator, object: *Object, macho_file: *MachO, }; pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocContext) !void { const tracy = trace(@src()); defer tracy.end(); const arch = context.macho_file.base.options.target.cpu.arch; var addend: i64 = 0; var subtractor: ?u32 = null; for (relocs) |rel, i| { blk: { switch (arch) { .aarch64 => switch (@intToEnum(macho.reloc_type_arm64, rel.r_type)) { .ARM64_RELOC_ADDEND => { assert(addend == 0); addend = rel.r_symbolnum; // Verify that it's followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12. if (relocs.len <= i + 1) { log.err("no relocation after ARM64_RELOC_ADDEND", .{}); return error.UnexpectedRelocationType; } const next = @intToEnum(macho.reloc_type_arm64, relocs[i + 1].r_type); switch (next) { .ARM64_RELOC_PAGE21, .ARM64_RELOC_PAGEOFF12 => {}, else => { log.err("unexpected relocation type after ARM64_RELOC_ADDEND", .{}); log.err(" expected ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12", .{}); log.err(" found {s}", .{next}); return error.UnexpectedRelocationType; }, } continue; }, .ARM64_RELOC_SUBTRACTOR => {}, else => break :blk, }, .x86_64 => switch (@intToEnum(macho.reloc_type_x86_64, rel.r_type)) { .X86_64_RELOC_SUBTRACTOR => {}, else => break :blk, }, else => unreachable, } assert(subtractor == null); const sym = context.object.symtab.items[rel.r_symbolnum]; if (sym.sect() and !sym.ext()) { subtractor = context.object.symbol_mapping.get(rel.r_symbolnum).?; } else { const sym_name = context.object.getString(sym.n_strx); const n_strx = context.macho_file.strtab_dir.getKeyAdapted( @as([]const u8, sym_name), StringIndexAdapter{ .bytes = &context.macho_file.strtab, }, ).?; const resolv = context.macho_file.symbol_resolver.get(n_strx).?; assert(resolv.where == .global); subtractor = resolv.local_sym_index; } // Verify that *_SUBTRACTOR is followed by *_UNSIGNED. if (relocs.len <= i + 1) { log.err("no relocation after *_RELOC_SUBTRACTOR", .{}); return error.UnexpectedRelocationType; } switch (arch) { .aarch64 => switch (@intToEnum(macho.reloc_type_arm64, relocs[i + 1].r_type)) { .ARM64_RELOC_UNSIGNED => {}, else => { log.err("unexpected relocation type after ARM64_RELOC_ADDEND", .{}); log.err(" expected ARM64_RELOC_UNSIGNED", .{}); log.err(" found {s}", .{@intToEnum(macho.reloc_type_arm64, relocs[i + 1].r_type)}); return error.UnexpectedRelocationType; }, }, .x86_64 => switch (@intToEnum(macho.reloc_type_x86_64, relocs[i + 1].r_type)) { .X86_64_RELOC_UNSIGNED => {}, else => { log.err("unexpected relocation type after X86_64_RELOC_ADDEND", .{}); log.err(" expected X86_64_RELOC_UNSIGNED", .{}); log.err(" found {s}", .{@intToEnum(macho.reloc_type_x86_64, relocs[i + 1].r_type)}); return error.UnexpectedRelocationType; }, }, else => unreachable, } continue; } const target = target: { if (rel.r_extern == 0) { const sect_id = @intCast(u16, rel.r_symbolnum - 1); const local_sym_index = context.object.sections_as_symbols.get(sect_id) orelse blk: { const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment; const sect = seg.sections.items[sect_id]; const match = (try context.macho_file.getMatchingSection(sect)) orelse unreachable; const local_sym_index = @intCast(u32, context.macho_file.locals.items.len); try context.macho_file.locals.append(context.allocator, .{ .n_strx = 0, .n_type = macho.N_SECT, .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1), .n_desc = 0, .n_value = 0, }); try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index); break :blk local_sym_index; }; break :target Relocation.Target{ .local = local_sym_index }; } const sym = context.object.symtab.items[rel.r_symbolnum]; const sym_name = context.object.getString(sym.n_strx); if (sym.sect() and !sym.ext()) { const sym_index = context.object.symbol_mapping.get(rel.r_symbolnum) orelse unreachable; break :target Relocation.Target{ .local = sym_index }; } const n_strx = context.macho_file.strtab_dir.getKeyAdapted( @as([]const u8, sym_name), StringIndexAdapter{ .bytes = &context.macho_file.strtab, }, ) orelse unreachable; break :target Relocation.Target{ .global = n_strx }; }; const offset = @intCast(u32, rel.r_address); switch (arch) { .aarch64 => { switch (@intToEnum(macho.reloc_type_arm64, rel.r_type)) { .ARM64_RELOC_BRANCH26 => { // TODO rewrite relocation try addStub(target, context); }, .ARM64_RELOC_GOT_LOAD_PAGE21, .ARM64_RELOC_GOT_LOAD_PAGEOFF12, .ARM64_RELOC_POINTER_TO_GOT, => { // TODO rewrite relocation try addGotEntry(target, context); }, .ARM64_RELOC_UNSIGNED => { addend = if (rel.r_length == 3) mem.readIntLittle(i64, self.code.items[offset..][0..8]) else mem.readIntLittle(i32, self.code.items[offset..][0..4]); if (rel.r_extern == 0) { const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment; const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; addend -= @intCast(i64, target_sect_base_addr); } try self.addPtrBindingOrRebase(rel, target, context); }, .ARM64_RELOC_TLVP_LOAD_PAGE21, .ARM64_RELOC_TLVP_LOAD_PAGEOFF12, => { if (target == .global) { try addTlvPtrEntry(target, context); } }, else => {}, } }, .x86_64 => { const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); switch (rel_type) { .X86_64_RELOC_BRANCH => { // TODO rewrite relocation try addStub(target, context); addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]); }, .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => { // TODO rewrite relocation try addGotEntry(target, context); addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]); }, .X86_64_RELOC_UNSIGNED => { addend = if (rel.r_length == 3) mem.readIntLittle(i64, self.code.items[offset..][0..8]) else mem.readIntLittle(i32, self.code.items[offset..][0..4]); if (rel.r_extern == 0) { const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment; const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; addend -= @intCast(i64, target_sect_base_addr); } try self.addPtrBindingOrRebase(rel, target, context); }, .X86_64_RELOC_SIGNED, .X86_64_RELOC_SIGNED_1, .X86_64_RELOC_SIGNED_2, .X86_64_RELOC_SIGNED_4, => { const correction: u3 = switch (rel_type) { .X86_64_RELOC_SIGNED => 0, .X86_64_RELOC_SIGNED_1 => 1, .X86_64_RELOC_SIGNED_2 => 2, .X86_64_RELOC_SIGNED_4 => 4, else => unreachable, }; addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]) + correction; if (rel.r_extern == 0) { // Note for the future self: when r_extern == 0, we should subtract correction from the // addend. const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment; const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr; addend += @intCast(i64, context.base_addr + offset + 4) - @intCast(i64, target_sect_base_addr); } }, .X86_64_RELOC_TLV => { if (target == .global) { try addTlvPtrEntry(target, context); } }, else => {}, } }, else => unreachable, } try self.relocs.append(context.allocator, .{ .offset = offset, .target = target, .addend = addend, .subtractor = subtractor, .pcrel = rel.r_pcrel == 1, .length = rel.r_length, .@"type" = rel.r_type, }); addend = 0; subtractor = null; } } fn addPtrBindingOrRebase( self: *Atom, rel: macho.relocation_info, target: Relocation.Target, context: RelocContext, ) !void { switch (target) { .global => |n_strx| { try self.bindings.append(context.allocator, .{ .n_strx = n_strx, .offset = @intCast(u32, rel.r_address), }); }, .local => { const source_sym = context.macho_file.locals.items[self.local_sym_index]; const match = context.macho_file.section_ordinals.keys()[source_sym.n_sect - 1]; const seg = context.macho_file.load_commands.items[match.seg].segment; const sect = seg.sections.items[match.sect]; const sect_type = sect.type_(); const should_rebase = rebase: { if (rel.r_length != 3) break :rebase false; // TODO actually, a check similar to what dyld is doing, that is, verifying // that the segment is writable should be enough here. const is_right_segment = blk: { if (context.macho_file.data_segment_cmd_index) |idx| { if (match.seg == idx) { break :blk true; } } if (context.macho_file.data_const_segment_cmd_index) |idx| { if (match.seg == idx) { break :blk true; } } break :blk false; }; if (!is_right_segment) break :rebase false; if (sect_type != macho.S_LITERAL_POINTERS and sect_type != macho.S_REGULAR and sect_type != macho.S_MOD_INIT_FUNC_POINTERS and sect_type != macho.S_MOD_TERM_FUNC_POINTERS) { break :rebase false; } break :rebase true; }; if (should_rebase) { try self.rebases.append(context.allocator, @intCast(u32, rel.r_address)); } }, } } fn addTlvPtrEntry(target: Relocation.Target, context: RelocContext) !void { if (context.macho_file.tlv_ptr_entries_table.contains(target)) return; const index = try context.macho_file.allocateTlvPtrEntry(target); const atom = try context.macho_file.createTlvPtrAtom(target); context.macho_file.tlv_ptr_entries.items[index].atom = atom; const match = (try context.macho_file.getMatchingSection(.{ .segname = MachO.makeStaticString("__DATA"), .sectname = MachO.makeStaticString("__thread_ptrs"), .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS, })).?; if (!context.object.start_atoms.contains(match)) { try context.object.start_atoms.putNoClobber(context.allocator, match, atom); } if (context.object.end_atoms.getPtr(match)) |last| { last.*.next = atom; atom.prev = last.*; last.* = atom; } else { try context.object.end_atoms.putNoClobber(context.allocator, match, atom); } } fn addGotEntry(target: Relocation.Target, context: RelocContext) !void { if (context.macho_file.got_entries_table.contains(target)) return; const index = try context.macho_file.allocateGotEntry(target); const atom = try context.macho_file.createGotAtom(target); context.macho_file.got_entries.items[index].atom = atom; const match = MachO.MatchingSection{ .seg = context.macho_file.data_const_segment_cmd_index.?, .sect = context.macho_file.got_section_index.?, }; if (!context.object.start_atoms.contains(match)) { try context.object.start_atoms.putNoClobber(context.allocator, match, atom); } if (context.object.end_atoms.getPtr(match)) |last| { last.*.next = atom; atom.prev = last.*; last.* = atom; } else { try context.object.end_atoms.putNoClobber(context.allocator, match, atom); } } fn addStub(target: Relocation.Target, context: RelocContext) !void { if (target != .global) return; if (context.macho_file.stubs_table.contains(target.global)) return; // If the symbol has been resolved as defined globally elsewhere (in a different translation unit), // then skip creating stub entry. // TODO Is this the correct for the incremental? if (context.macho_file.symbol_resolver.get(target.global).?.where == .global) return; const stub_index = try context.macho_file.allocateStubEntry(target.global); // TODO clean this up! const stub_helper_atom = atom: { const atom = try context.macho_file.createStubHelperAtom(); const match = MachO.MatchingSection{ .seg = context.macho_file.text_segment_cmd_index.?, .sect = context.macho_file.stub_helper_section_index.?, }; if (!context.object.start_atoms.contains(match)) { try context.object.start_atoms.putNoClobber(context.allocator, match, atom); } if (context.object.end_atoms.getPtr(match)) |last| { last.*.next = atom; atom.prev = last.*; last.* = atom; } else { try context.object.end_atoms.putNoClobber(context.allocator, match, atom); } break :atom atom; }; const laptr_atom = atom: { const atom = try context.macho_file.createLazyPointerAtom( stub_helper_atom.local_sym_index, target.global, ); const match = MachO.MatchingSection{ .seg = context.macho_file.data_segment_cmd_index.?, .sect = context.macho_file.la_symbol_ptr_section_index.?, }; if (!context.object.start_atoms.contains(match)) { try context.object.start_atoms.putNoClobber(context.allocator, match, atom); } if (context.object.end_atoms.getPtr(match)) |last| { last.*.next = atom; atom.prev = last.*; last.* = atom; } else { try context.object.end_atoms.putNoClobber(context.allocator, match, atom); } break :atom atom; }; const atom = try context.macho_file.createStubAtom(laptr_atom.local_sym_index); const match = MachO.MatchingSection{ .seg = context.macho_file.text_segment_cmd_index.?, .sect = context.macho_file.stubs_section_index.?, }; if (!context.object.start_atoms.contains(match)) { try context.object.start_atoms.putNoClobber(context.allocator, match, atom); } if (context.object.end_atoms.getPtr(match)) |last| { last.*.next = atom; atom.prev = last.*; last.* = atom; } else { try context.object.end_atoms.putNoClobber(context.allocator, match, atom); } context.macho_file.stubs.items[stub_index] = atom; } pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void { const tracy = trace(@src()); defer tracy.end(); for (self.relocs.items) |rel| { log.debug("relocating {}", .{rel}); const arch = macho_file.base.options.target.cpu.arch; const source_addr = blk: { const sym = macho_file.locals.items[self.local_sym_index]; break :blk sym.n_value + rel.offset; }; var is_via_thread_ptrs: bool = false; const target_addr = blk: { const is_via_got = got: { switch (arch) { .aarch64 => break :got switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) { .ARM64_RELOC_GOT_LOAD_PAGE21, .ARM64_RELOC_GOT_LOAD_PAGEOFF12, .ARM64_RELOC_POINTER_TO_GOT, => true, else => false, }, .x86_64 => break :got switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) { .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => true, else => false, }, else => unreachable, } }; if (is_via_got) { const got_index = macho_file.got_entries_table.get(rel.target) orelse { log.err("expected GOT entry for symbol", .{}); switch (rel.target) { .local => |sym_index| log.err(" local @{d}", .{sym_index}), .global => |n_strx| log.err(" global @'{s}'", .{macho_file.getString(n_strx)}), } log.err(" this is an internal linker error", .{}); return error.FailedToResolveRelocationTarget; }; const atom = macho_file.got_entries.items[got_index].atom; break :blk macho_file.locals.items[atom.local_sym_index].n_value; } switch (rel.target) { .local => |sym_index| { const sym = macho_file.locals.items[sym_index]; const is_tlv = is_tlv: { const source_sym = macho_file.locals.items[self.local_sym_index]; const match = macho_file.section_ordinals.keys()[source_sym.n_sect - 1]; const seg = macho_file.load_commands.items[match.seg].segment; const sect = seg.sections.items[match.sect]; break :is_tlv sect.type_() == macho.S_THREAD_LOCAL_VARIABLES; }; if (is_tlv) { // For TLV relocations, the value specified as a relocation is the displacement from the // TLV initializer (either value in __thread_data or zero-init in __thread_bss) to the first // defined TLV template init section in the following order: // * wrt to __thread_data if defined, then // * wrt to __thread_bss const seg = macho_file.load_commands.items[macho_file.data_segment_cmd_index.?].segment; const base_address = inner: { if (macho_file.tlv_data_section_index) |i| { break :inner seg.sections.items[i].addr; } else if (macho_file.tlv_bss_section_index) |i| { break :inner seg.sections.items[i].addr; } else { log.err("threadlocal variables present but no initializer sections found", .{}); log.err(" __thread_data not found", .{}); log.err(" __thread_bss not found", .{}); return error.FailedToResolveRelocationTarget; } }; break :blk sym.n_value - base_address; } break :blk sym.n_value; }, .global => |n_strx| { // TODO Still trying to figure out how to possibly use stubs for local symbol indirection with // branching instructions. If it is not possible, then the best course of action is to // resurrect the former approach of defering creating synthethic atoms in __got and __la_symbol_ptr // sections until we resolve the relocations. const resolv = macho_file.symbol_resolver.get(n_strx).?; switch (resolv.where) { .global => break :blk macho_file.globals.items[resolv.where_index].n_value, .undef => { if (macho_file.stubs_table.get(n_strx)) |stub_index| { const atom = macho_file.stubs.items[stub_index]; break :blk macho_file.locals.items[atom.local_sym_index].n_value; } else { if (macho_file.tlv_ptr_entries_table.get(rel.target)) |tlv_ptr_index| { is_via_thread_ptrs = true; const atom = macho_file.tlv_ptr_entries.items[tlv_ptr_index].atom; break :blk macho_file.locals.items[atom.local_sym_index].n_value; } break :blk 0; } }, } }, } }; log.debug(" | source_addr = 0x{x}", .{source_addr}); log.debug(" | target_addr = 0x{x}", .{target_addr}); switch (arch) { .aarch64 => { switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) { .ARM64_RELOC_BRANCH26 => { const displacement = math.cast( i28, @intCast(i64, target_addr) - @intCast(i64, source_addr), ) catch |err| switch (err) { error.Overflow => { log.err("jump too big to encode as i28 displacement value", .{}); log.err(" (target - source) = displacement => 0x{x} - 0x{x} = 0x{x}", .{ target_addr, source_addr, @intCast(i64, target_addr) - @intCast(i64, source_addr), }); log.err(" TODO implement branch islands to extend jump distance for arm64", .{}); return error.TODOImplementBranchIslands; }, }; const code = self.code.items[rel.offset..][0..4]; var inst = aarch64.Instruction{ .unconditional_branch_immediate = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.unconditional_branch_immediate, ), code), }; inst.unconditional_branch_immediate.imm26 = @truncate(u26, @bitCast(u28, displacement >> 2)); mem.writeIntLittle(u32, code, inst.toU32()); }, .ARM64_RELOC_PAGE21, .ARM64_RELOC_GOT_LOAD_PAGE21, .ARM64_RELOC_TLVP_LOAD_PAGE21, => { const actual_target_addr = @intCast(i64, target_addr) + rel.addend; const source_page = @intCast(i32, source_addr >> 12); const target_page = @intCast(i32, actual_target_addr >> 12); const pages = @bitCast(u21, @intCast(i21, target_page - source_page)); const code = self.code.items[rel.offset..][0..4]; var inst = aarch64.Instruction{ .pc_relative_address = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.pc_relative_address, ), code), }; inst.pc_relative_address.immhi = @truncate(u19, pages >> 2); inst.pc_relative_address.immlo = @truncate(u2, pages); mem.writeIntLittle(u32, code, inst.toU32()); }, .ARM64_RELOC_PAGEOFF12 => { const code = self.code.items[rel.offset..][0..4]; const actual_target_addr = @intCast(i64, target_addr) + rel.addend; const narrowed = @truncate(u12, @intCast(u64, actual_target_addr)); if (isArithmeticOp(self.code.items[rel.offset..][0..4])) { var inst = aarch64.Instruction{ .add_subtract_immediate = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.add_subtract_immediate, ), code), }; inst.add_subtract_immediate.imm12 = narrowed; mem.writeIntLittle(u32, code, inst.toU32()); } else { var inst = aarch64.Instruction{ .load_store_register = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.load_store_register, ), code), }; const offset: u12 = blk: { if (inst.load_store_register.size == 0) { if (inst.load_store_register.v == 1) { // 128-bit SIMD is scaled by 16. break :blk try math.divExact(u12, narrowed, 16); } // Otherwise, 8-bit SIMD or ldrb. break :blk narrowed; } else { const denom: u4 = try math.powi(u4, 2, inst.load_store_register.size); break :blk try math.divExact(u12, narrowed, denom); } }; inst.load_store_register.offset = offset; mem.writeIntLittle(u32, code, inst.toU32()); } }, .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => { const code = self.code.items[rel.offset..][0..4]; const actual_target_addr = @intCast(i64, target_addr) + rel.addend; const narrowed = @truncate(u12, @intCast(u64, actual_target_addr)); var inst: aarch64.Instruction = .{ .load_store_register = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.load_store_register, ), code), }; const offset = try math.divExact(u12, narrowed, 8); inst.load_store_register.offset = offset; mem.writeIntLittle(u32, code, inst.toU32()); }, .ARM64_RELOC_TLVP_LOAD_PAGEOFF12 => { const code = self.code.items[rel.offset..][0..4]; const actual_target_addr = @intCast(i64, target_addr) + rel.addend; const RegInfo = struct { rd: u5, rn: u5, size: u2, }; const reg_info: RegInfo = blk: { if (isArithmeticOp(code)) { const inst = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.add_subtract_immediate, ), code); break :blk .{ .rd = inst.rd, .rn = inst.rn, .size = inst.sf, }; } else { const inst = mem.bytesToValue(meta.TagPayload( aarch64.Instruction, aarch64.Instruction.load_store_register, ), code); break :blk .{ .rd = inst.rt, .rn = inst.rn, .size = inst.size, }; } }; const narrowed = @truncate(u12, @intCast(u64, actual_target_addr)); var inst = if (is_via_thread_ptrs) blk: { const offset = try math.divExact(u12, narrowed, 8); break :blk aarch64.Instruction{ .load_store_register = .{ .rt = reg_info.rd, .rn = reg_info.rn, .offset = offset, .opc = 0b01, .op1 = 0b01, .v = 0, .size = reg_info.size, }, }; } else aarch64.Instruction{ .add_subtract_immediate = .{ .rd = reg_info.rd, .rn = reg_info.rn, .imm12 = narrowed, .sh = 0, .s = 0, .op = 0, .sf = @truncate(u1, reg_info.size), }, }; mem.writeIntLittle(u32, code, inst.toU32()); }, .ARM64_RELOC_POINTER_TO_GOT => { const result = try math.cast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr)); mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, result)); }, .ARM64_RELOC_UNSIGNED => { const result = blk: { if (rel.subtractor) |subtractor| { const sym = macho_file.locals.items[subtractor]; break :blk @intCast(i64, target_addr) - @intCast(i64, sym.n_value) + rel.addend; } else { break :blk @intCast(i64, target_addr) + rel.addend; } }; if (rel.length == 3) { mem.writeIntLittle(u64, self.code.items[rel.offset..][0..8], @bitCast(u64, result)); } else { mem.writeIntLittle( u32, self.code.items[rel.offset..][0..4], @truncate(u32, @bitCast(u64, result)), ); } }, .ARM64_RELOC_SUBTRACTOR => unreachable, .ARM64_RELOC_ADDEND => unreachable, } }, .x86_64 => { switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) { .X86_64_RELOC_BRANCH => { const displacement = try math.cast( i32, @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend, ); mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); }, .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => { const displacement = try math.cast( i32, @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend, ); mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); }, .X86_64_RELOC_TLV => { if (!is_via_thread_ptrs) { // We need to rewrite the opcode from movq to leaq. self.code.items[rel.offset - 2] = 0x8d; } const displacement = try math.cast( i32, @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend, ); mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); }, .X86_64_RELOC_SIGNED, .X86_64_RELOC_SIGNED_1, .X86_64_RELOC_SIGNED_2, .X86_64_RELOC_SIGNED_4, => { const correction: u3 = switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) { .X86_64_RELOC_SIGNED => 0, .X86_64_RELOC_SIGNED_1 => 1, .X86_64_RELOC_SIGNED_2 => 2, .X86_64_RELOC_SIGNED_4 => 4, else => unreachable, }; const actual_target_addr = @intCast(i64, target_addr) + rel.addend; const displacement = try math.cast( i32, actual_target_addr - @intCast(i64, source_addr + correction + 4), ); mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement)); }, .X86_64_RELOC_UNSIGNED => { const result = blk: { if (rel.subtractor) |subtractor| { const sym = macho_file.locals.items[subtractor]; break :blk @intCast(i64, target_addr) - @intCast(i64, sym.n_value) + rel.addend; } else { break :blk @intCast(i64, target_addr) + rel.addend; } }; if (rel.length == 3) { mem.writeIntLittle(u64, self.code.items[rel.offset..][0..8], @bitCast(u64, result)); } else { mem.writeIntLittle( u32, self.code.items[rel.offset..][0..4], @truncate(u32, @bitCast(u64, result)), ); } }, .X86_64_RELOC_SUBTRACTOR => unreachable, } }, else => unreachable, } } } inline fn isArithmeticOp(inst: *const [4]u8) bool { const group_decode = @truncate(u5, inst[3]); return ((group_decode >> 2) == 4); }
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2021 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. // The reference for these types and values is Microsoft Windows's ucrt (Universal C RunTime). usingnamespace @import("../windows/bits.zig"); const ws2_32 = @import("../windows/ws2_32.zig"); pub const fd_t = HANDLE; pub const ino_t = LARGE_INTEGER; pub const pid_t = HANDLE; pub const mode_t = u0; pub const PATH_MAX = 260; pub const time_t = c_longlong; pub const timespec = extern struct { tv_sec: time_t, tv_nsec: c_long, }; pub const timeval = extern struct { tv_sec: c_long, tv_usec: c_long, }; pub const sig_atomic_t = c_int; /// maximum signal number + 1 pub const NSIG = 23; // Signal types /// interrupt pub const SIGINT = 2; /// illegal instruction - invalid function image pub const SIGILL = 4; /// floating point exception pub const SIGFPE = 8; /// segment violation pub const SIGSEGV = 11; /// Software termination signal from kill pub const SIGTERM = 15; /// Ctrl-Break sequence pub const SIGBREAK = 21; /// abnormal termination triggered by abort call pub const SIGABRT = 22; /// SIGABRT compatible with other platforms, same as SIGABRT pub const SIGABRT_COMPAT = 6; // Signal action codes /// default signal action pub const SIG_DFL = 0; /// ignore signal pub const SIG_IGN = 1; /// return current value pub const SIG_GET = 2; /// signal gets error pub const SIG_SGE = 3; /// acknowledge pub const SIG_ACK = 4; /// Signal error value (returned by signal call on error) pub const SIG_ERR = -1; pub const SEEK_SET = 0; pub const SEEK_CUR = 1; pub const SEEK_END = 2; pub const EPERM = 1; pub const ENOENT = 2; pub const ESRCH = 3; pub const EINTR = 4; pub const EIO = 5; pub const ENXIO = 6; pub const E2BIG = 7; pub const ENOEXEC = 8; pub const EBADF = 9; pub const ECHILD = 10; pub const EAGAIN = 11; pub const ENOMEM = 12; pub const EACCES = 13; pub const EFAULT = 14; pub const EBUSY = 16; pub const EEXIST = 17; pub const EXDEV = 18; pub const ENODEV = 19; pub const ENOTDIR = 20; pub const EISDIR = 21; pub const ENFILE = 23; pub const EMFILE = 24; pub const ENOTTY = 25; pub const EFBIG = 27; pub const ENOSPC = 28; pub const ESPIPE = 29; pub const EROFS = 30; pub const EMLINK = 31; pub const EPIPE = 32; pub const EDOM = 33; pub const EDEADLK = 36; pub const ENAMETOOLONG = 38; pub const ENOLCK = 39; pub const ENOSYS = 40; pub const ENOTEMPTY = 41; pub const EINVAL = 22; pub const ERANGE = 34; pub const EILSEQ = 42; pub const STRUNCATE = 80; // Support EDEADLOCK for compatibility with older Microsoft C versions pub const EDEADLOCK = EDEADLK; // POSIX Supplement pub const EADDRINUSE = 100; pub const EADDRNOTAVAIL = 101; pub const EAFNOSUPPORT = 102; pub const EALREADY = 103; pub const EBADMSG = 104; pub const ECANCELED = 105; pub const ECONNABORTED = 106; pub const ECONNREFUSED = 107; pub const ECONNRESET = 108; pub const EDESTADDRREQ = 109; pub const EHOSTUNREACH = 110; pub const EIDRM = 111; pub const EINPROGRESS = 112; pub const EISCONN = 113; pub const ELOOP = 114; pub const EMSGSIZE = 115; pub const ENETDOWN = 116; pub const ENETRESET = 117; pub const ENETUNREACH = 118; pub const ENOBUFS = 119; pub const ENODATA = 120; pub const ENOLINK = 121; pub const ENOMSG = 122; pub const ENOPROTOOPT = 123; pub const ENOSR = 124; pub const ENOSTR = 125; pub const ENOTCONN = 126; pub const ENOTRECOVERABLE = 127; pub const ENOTSOCK = 128; pub const ENOTSUP = 129; pub const EOPNOTSUPP = 130; pub const EOTHER = 131; pub const EOVERFLOW = 132; pub const EOWNERDEAD = 133; pub const EPROTO = 134; pub const EPROTONOSUPPORT = 135; pub const EPROTOTYPE = 136; pub const ETIME = 137; pub const ETIMEDOUT = 138; pub const ETXTBSY = 139; pub const EWOULDBLOCK = 140; pub const EDQUOT = 10069; pub const F_OK = 0; /// Remove directory instead of unlinking file pub const AT_REMOVEDIR = 0x200; pub const in_port_t = u16; pub const sa_family_t = ws2_32.ADDRESS_FAMILY; pub const socklen_t = ws2_32.socklen_t; pub const sockaddr = ws2_32.sockaddr; pub const sockaddr_in = ws2_32.sockaddr_in; pub const sockaddr_in6 = ws2_32.sockaddr_in6; pub const sockaddr_un = ws2_32.sockaddr_un; pub const in6_addr = [16]u8; pub const in_addr = u32; pub const addrinfo = ws2_32.addrinfo; pub const AF_UNSPEC = ws2_32.AF_UNSPEC; pub const AF_UNIX = ws2_32.AF_UNIX; pub const AF_INET = ws2_32.AF_INET; pub const AF_IMPLINK = ws2_32.AF_IMPLINK; pub const AF_PUP = ws2_32.AF_PUP; pub const AF_CHAOS = ws2_32.AF_CHAOS; pub const AF_NS = ws2_32.AF_NS; pub const AF_IPX = ws2_32.AF_IPX; pub const AF_ISO = ws2_32.AF_ISO; pub const AF_OSI = ws2_32.AF_OSI; pub const AF_ECMA = ws2_32.AF_ECMA; pub const AF_DATAKIT = ws2_32.AF_DATAKIT; pub const AF_CCITT = ws2_32.AF_CCITT; pub const AF_SNA = ws2_32.AF_SNA; pub const AF_DECnet = ws2_32.AF_DECnet; pub const AF_DLI = ws2_32.AF_DLI; pub const AF_LAT = ws2_32.AF_LAT; pub const AF_HYLINK = ws2_32.AF_HYLINK; pub const AF_APPLETALK = ws2_32.AF_APPLETALK; pub const AF_NETBIOS = ws2_32.AF_NETBIOS; pub const AF_VOICEVIEW = ws2_32.AF_VOICEVIEW; pub const AF_FIREFOX = ws2_32.AF_FIREFOX; pub const AF_UNKNOWN1 = ws2_32.AF_UNKNOWN1; pub const AF_BAN = ws2_32.AF_BAN; pub const AF_ATM = ws2_32.AF_ATM; pub const AF_INET6 = ws2_32.AF_INET6; pub const AF_CLUSTER = ws2_32.AF_CLUSTER; pub const AF_12844 = ws2_32.AF_12844; pub const AF_IRDA = ws2_32.AF_IRDA; pub const AF_NETDES = ws2_32.AF_NETDES; pub const AF_TCNPROCESS = ws2_32.AF_TCNPROCESS; pub const AF_TCNMESSAGE = ws2_32.AF_TCNMESSAGE; pub const AF_ICLFXBM = ws2_32.AF_ICLFXBM; pub const AF_BTH = ws2_32.AF_BTH; pub const AF_MAX = ws2_32.AF_MAX; pub const SOCK_STREAM = ws2_32.SOCK_STREAM; pub const SOCK_DGRAM = ws2_32.SOCK_DGRAM; pub const SOCK_RAW = ws2_32.SOCK_RAW; pub const SOCK_RDM = ws2_32.SOCK_RDM; pub const SOCK_SEQPACKET = ws2_32.SOCK_SEQPACKET; /// WARNING: this flag is not supported by windows socket functions directly, /// it is only supported by std.os.socket. Be sure that this value does /// not share any bits with any of the SOCK_* values. pub const SOCK_CLOEXEC = 0x10000; /// WARNING: this flag is not supported by windows socket functions directly, /// it is only supported by std.os.socket. Be sure that this value does /// not share any bits with any of the SOCK_* values. pub const SOCK_NONBLOCK = 0x20000; pub const IPPROTO_ICMP = ws2_32.IPPROTO_ICMP; pub const IPPROTO_IGMP = ws2_32.IPPROTO_IGMP; pub const BTHPROTO_RFCOMM = ws2_32.BTHPROTO_RFCOMM; pub const IPPROTO_TCP = ws2_32.IPPROTO_TCP; pub const IPPROTO_UDP = ws2_32.IPPROTO_UDP; pub const IPPROTO_ICMPV6 = ws2_32.IPPROTO_ICMPV6; pub const IPPROTO_RM = ws2_32.IPPROTO_RM; pub const nfds_t = c_ulong; pub const pollfd = ws2_32.pollfd; pub const POLLRDNORM = ws2_32.POLLRDNORM; pub const POLLRDBAND = ws2_32.POLLRDBAND; pub const POLLIN = ws2_32.POLLIN; pub const POLLPRI = ws2_32.POLLPRI; pub const POLLWRNORM = ws2_32.POLLWRNORM; pub const POLLOUT = ws2_32.POLLOUT; pub const POLLWRBAND = ws2_32.POLLWRBAND; pub const POLLERR = ws2_32.POLLERR; pub const POLLHUP = ws2_32.POLLHUP; pub const POLLNVAL = ws2_32.POLLNVAL; pub const SOL_SOCKET = ws2_32.SOL_SOCKET; pub const SO_DEBUG = ws2_32.SO_DEBUG; pub const SO_ACCEPTCONN = ws2_32.SO_ACCEPTCONN; pub const SO_REUSEADDR = ws2_32.SO_REUSEADDR; pub const SO_KEEPALIVE = ws2_32.SO_KEEPALIVE; pub const SO_DONTROUTE = ws2_32.SO_DONTROUTE; pub const SO_BROADCAST = ws2_32.SO_BROADCAST; pub const SO_USELOOPBACK = ws2_32.SO_USELOOPBACK; pub const SO_LINGER = ws2_32.SO_LINGER; pub const SO_OOBINLINE = ws2_32.SO_OOBINLINE; pub const SO_DONTLINGER = ws2_32.SO_DONTLINGER; pub const SO_EXCLUSIVEADDRUSE = ws2_32.SO_EXCLUSIVEADDRUSE; pub const SO_SNDBUF = ws2_32.SO_SNDBUF; pub const SO_RCVBUF = ws2_32.SO_RCVBUF; pub const SO_SNDLOWAT = ws2_32.SO_SNDLOWAT; pub const SO_RCVLOWAT = ws2_32.SO_RCVLOWAT; pub const SO_SNDTIMEO = ws2_32.SO_SNDTIMEO; pub const SO_RCVTIMEO = ws2_32.SO_RCVTIMEO; pub const SO_ERROR = ws2_32.SO_ERROR; pub const SO_TYPE = ws2_32.SO_TYPE; pub const SO_GROUP_ID = ws2_32.SO_GROUP_ID; pub const SO_GROUP_PRIORITY = ws2_32.SO_GROUP_PRIORITY; pub const SO_MAX_MSG_SIZE = ws2_32.SO_MAX_MSG_SIZE; pub const SO_PROTOCOL_INFOA = ws2_32.SO_PROTOCOL_INFOA; pub const SO_PROTOCOL_INFOW = ws2_32.SO_PROTOCOL_INFOW; pub const PVD_CONFIG = ws2_32.PVD_CONFIG; pub const SO_CONDITIONAL_ACCEPT = ws2_32.SO_CONDITIONAL_ACCEPT; pub const TCP_NODELAY = ws2_32.TCP_NODELAY; pub const O_RDONLY = 0o0; pub const O_WRONLY = 0o1; pub const O_RDWR = 0o2; pub const O_CREAT = 0o100; pub const O_EXCL = 0o200; pub const O_NOCTTY = 0o400; pub const O_TRUNC = 0o1000; pub const O_APPEND = 0o2000; pub const O_NONBLOCK = 0o4000; pub const O_DSYNC = 0o10000; pub const O_SYNC = 0o4010000; pub const O_RSYNC = 0o4010000; pub const O_DIRECTORY = 0o200000; pub const O_NOFOLLOW = 0o400000; pub const O_CLOEXEC = 0o2000000; pub const O_ASYNC = 0o20000; pub const O_DIRECT = 0o40000; pub const O_LARGEFILE = 0; pub const O_NOATIME = 0o1000000; pub const O_PATH = 0o10000000; pub const O_TMPFILE = 0o20200000; pub const O_NDELAY = O_NONBLOCK; pub const IFNAMESIZE = 30;
const main = @import("main.zig"); const vectors = @import("vectors.zig"); const uart = @import("uart.zig"); pub export fn _start() callconv(.Naked) noreturn { // At startup the stack pointer is at the end of RAM // so, no need to set it manually! // Reference this such that the file is analyzed and the vectors // are added. _ = vectors; copy_data_to_ram(); clear_bss(); main.main(); while (true) {} } fn copy_data_to_ram() void { asm volatile ( \\ ; load Z register with the address of the data in flash \\ ldi r30, lo8(__data_load_start) \\ ldi r31, hi8(__data_load_start) \\ ; load X register with address of the data in ram \\ ldi r26, lo8(__data_start) \\ ldi r27, hi8(__data_start) \\ ; load address of end of the data in ram \\ ldi r24, lo8(__data_end) \\ ldi r25, hi8(__data_end) \\ rjmp .L2 \\ \\.L1: \\ lpm r18, Z+ ; copy from Z into r18 and increment Z \\ st X+, r18 ; store r18 at location X and increment X \\ \\.L2: \\ cp r26, r24 \\ cpc r27, r25 ; check and branch if we are at the end of data \\ brne .L1 ); // Probably a good idea to add clobbers here, but compiler doesn't seem to care } fn clear_bss() void { asm volatile ( \\ ; load X register with the beginning of bss section \\ ldi r26, lo8(__bss_start) \\ ldi r27, hi8(__bss_start) \\ ; load end of the bss in registers \\ ldi r24, lo8(__bss_end) \\ ldi r25, hi8(__bss_end) \\ ldi r18, 0x00 \\ rjmp .L4 \\ \\.L3: \\ st X+, r18 \\ \\.L4: \\ cp r26, r24 \\ cpc r27, r25 ; check and branch if we are at the end of bss \\ brne .L3 ); // Probably a good idea to add clobbers here, but compiler doesn't seem to care } pub fn panic(msg: []const u8, error_return_trace: ?*@import("std").builtin.StackTrace) noreturn { // Currently assumes that the uart is initialized in main(). uart.write("PANIC: "); uart.write(msg); // TODO: print stack trace (addresses), which can than be turned into actual source line // numbers on the connected machine. while (true) {} }
use @import("index.zig"); pub const PROV_RSA_FULL = 1; pub const REGSAM = ACCESS_MASK; pub const ACCESS_MASK = DWORD; pub const PHKEY = *HKEY; pub const HKEY = *HKEY__; pub const HKEY__ = extern struct { unused: c_int, }; pub const LSTATUS = LONG; pub extern "advapi32" stdcallcc fn RegOpenKeyExW( hKey: HKEY, lpSubKey: LPCWSTR, ulOptions: DWORD, samDesired: REGSAM, phkResult: *HKEY, ) LSTATUS; pub extern "advapi32" stdcallcc fn RegQueryValueExW( hKey: HKEY, lpValueName: LPCWSTR, lpReserved: LPDWORD, lpType: LPDWORD, lpData: LPBYTE, lpcbData: LPDWORD, ) LSTATUS; // RtlGenRandom is known as SystemFunction036 under advapi32 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */ pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL; pub const RtlGenRandom = SystemFunction036;
const std = @import("std"); const build_options = @import("build_options"); const builtin = @import("builtin"); const assert = std.debug.assert; const leb128 = std.leb; const link = @import("../../link.zig"); const log = std.log.scoped(.codegen); const math = std.math; const mem = std.mem; const trace = @import("../../tracy.zig").trace; const Air = @import("../../Air.zig"); const Allocator = mem.Allocator; const Compilation = @import("../../Compilation.zig"); const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput; const DW = std.dwarf; const ErrorMsg = Module.ErrorMsg; const FnResult = @import("../../codegen.zig").FnResult; const GenerateSymbolError = @import("../../codegen.zig").GenerateSymbolError; const Isel = @import("Isel.zig"); const Liveness = @import("../../Liveness.zig"); const Mir = @import("Mir.zig"); const Module = @import("../../Module.zig"); const RegisterManager = @import("../../register_manager.zig").RegisterManager; const Target = std.Target; const Type = @import("../../type.zig").Type; const TypedValue = @import("../../TypedValue.zig"); const Value = @import("../../value.zig").Value; const Zir = @import("../../Zir.zig"); const InnerError = error{ OutOfMemory, CodegenFail, }; gpa: Allocator, air: Air, liveness: Liveness, bin_file: *link.File, target: *const std.Target, mod_fn: *const Module.Fn, err_msg: ?*ErrorMsg, args: []MCValue, ret_mcv: MCValue, fn_type: Type, arg_index: usize, src_loc: Module.SrcLoc, stack_align: u32, /// MIR Instructions mir_instructions: std.MultiArrayList(Mir.Inst) = .{}, /// MIR extra data mir_extra: std.ArrayListUnmanaged(u32) = .{}, /// Byte offset within the source file of the ending curly. end_di_line: u32, end_di_column: u32, /// The value is an offset into the `Function` `code` from the beginning. /// To perform the reloc, write 32-bit signed little-endian integer /// which is a relative jump, based on the address following the reloc. exitlude_jump_relocs: std.ArrayListUnmanaged(Mir.Inst.Index) = .{}, /// Whenever there is a runtime branch, we push a Branch onto this stack, /// and pop it off when the runtime branch joins. This provides an "overlay" /// of the table of mappings from instructions to `MCValue` from within the branch. /// This way we can modify the `MCValue` for an instruction in different ways /// within different branches. Special consideration is needed when a branch /// joins with its parent, to make sure all instructions have the same MCValue /// across each runtime branch upon joining. branch_stack: *std.ArrayList(Branch), // Key is the block instruction blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .{}, register_manager: RegisterManager(Self, Register, &callee_preserved_regs) = .{}, /// Maps offset to what is stored there. stack: std.AutoHashMapUnmanaged(u32, StackAllocation) = .{}, /// Offset from the stack base, representing the end of the stack frame. max_end_stack: u32 = 0, /// Represents the current end stack offset. If there is no existing slot /// to place a new stack allocation, it goes here, and then bumps `max_end_stack`. next_stack_offset: u32 = 0, /// Debug field, used to find bugs in the compiler. air_bookkeeping: @TypeOf(air_bookkeeping_init) = air_bookkeeping_init, /// For mir debug info, maps a mir index to a air index mir_to_air_map: if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index) else void, const air_bookkeeping_init = if (std.debug.runtime_safety) @as(usize, 0) else {}; pub const MCValue = union(enum) { /// No runtime bits. `void` types, empty structs, u0, enums with 1 tag, etc. /// TODO Look into deleting this tag and using `dead` instead, since every use /// of MCValue.none should be instead looking at the type and noticing it is 0 bits. none, /// Control flow will not allow this value to be observed. unreach, /// No more references to this value remain. dead, /// The value is undefined. undef, /// A pointer-sized integer that fits in a register. /// If the type is a pointer, this is the pointer address in virtual address space. immediate: u64, /// The constant was emitted into the code, at this offset. /// If the type is a pointer, it means the pointer address is embedded in the code. embedded_in_code: usize, /// The value is a pointer to a constant which was emitted into the code, at this offset. ptr_embedded_in_code: usize, /// The value is in a target-specific register. register: Register, /// The value is in memory at a hard-coded address. /// If the type is a pointer, it means the pointer address is at this memory location. memory: u64, /// The value is one of the stack variables. /// If the type is a pointer, it means the pointer address is in the stack at this offset. stack_offset: u32, /// The value is a pointer to one of the stack variables (payload is stack offset). ptr_stack_offset: u32, /// The value is in the compare flags assuming an unsigned operation, /// with this operator applied on top of it. compare_flags_unsigned: math.CompareOperator, /// The value is in the compare flags assuming a signed operation, /// with this operator applied on top of it. compare_flags_signed: math.CompareOperator, fn isMemory(mcv: MCValue) bool { return switch (mcv) { .embedded_in_code, .memory, .stack_offset => true, else => false, }; } fn isImmediate(mcv: MCValue) bool { return switch (mcv) { .immediate => true, else => false, }; } fn isMutable(mcv: MCValue) bool { return switch (mcv) { .none => unreachable, .unreach => unreachable, .dead => unreachable, .immediate, .embedded_in_code, .memory, .compare_flags_unsigned, .compare_flags_signed, .ptr_stack_offset, .ptr_embedded_in_code, .undef, => false, .register, .stack_offset, => true, }; } fn isRegister(mcv: MCValue) bool { return switch (mcv) { .register => true, else => false, }; } }; const Branch = struct { inst_table: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, MCValue) = .{}, fn deinit(self: *Branch, gpa: Allocator) void { self.inst_table.deinit(gpa); self.* = undefined; } }; const StackAllocation = struct { inst: Air.Inst.Index, /// TODO do we need size? should be determined by inst.ty.abiSize() size: u32, }; const BlockData = struct { relocs: std.ArrayListUnmanaged(Mir.Inst.Index), /// The first break instruction encounters `null` here and chooses a /// machine code value for the block result, populating this field. /// Following break instructions encounter that value and use it for /// the location to store their block results. mcv: MCValue, }; const BigTomb = struct { function: *Self, inst: Air.Inst.Index, tomb_bits: Liveness.Bpi, big_tomb_bits: u32, bit_index: usize, fn feed(bt: *BigTomb, op_ref: Air.Inst.Ref) void { const this_bit_index = bt.bit_index; bt.bit_index += 1; const op_int = @enumToInt(op_ref); if (op_int < Air.Inst.Ref.typed_value_map.len) return; const op_index = @intCast(Air.Inst.Index, op_int - Air.Inst.Ref.typed_value_map.len); if (this_bit_index < Liveness.bpi - 1) { const dies = @truncate(u1, bt.tomb_bits >> @intCast(Liveness.OperandInt, this_bit_index)) != 0; if (!dies) return; } else { const big_bit_index = @intCast(u5, this_bit_index - (Liveness.bpi - 1)); const dies = @truncate(u1, bt.big_tomb_bits >> big_bit_index) != 0; if (!dies) return; } bt.function.processDeath(op_index); } fn finishAir(bt: *BigTomb, result: MCValue) void { const is_used = !bt.function.liveness.isUnused(bt.inst); if (is_used) { log.debug("%{d} => {}", .{ bt.inst, result }); const branch = &bt.function.branch_stack.items[bt.function.branch_stack.items.len - 1]; branch.inst_table.putAssumeCapacityNoClobber(bt.inst, result); } bt.function.finishAirBookkeeping(); } }; const Self = @This(); pub fn generate( bin_file: *link.File, src_loc: Module.SrcLoc, module_fn: *Module.Fn, air: Air, liveness: Liveness, code: *std.ArrayList(u8), debug_output: DebugInfoOutput, ) GenerateSymbolError!FnResult { if (build_options.skip_non_native and builtin.cpu.arch != bin_file.options.target.cpu.arch) { @panic("Attempted to compile for architecture that was disabled by build configuration"); } assert(module_fn.owner_decl.has_tv); const fn_type = module_fn.owner_decl.ty; var branch_stack = std.ArrayList(Branch).init(bin_file.allocator); defer { assert(branch_stack.items.len == 1); branch_stack.items[0].deinit(bin_file.allocator); branch_stack.deinit(); } try branch_stack.append(.{}); var function = Self{ .gpa = bin_file.allocator, .air = air, .liveness = liveness, .target = &bin_file.options.target, .bin_file = bin_file, .mod_fn = module_fn, .err_msg = null, .args = undefined, // populated after `resolveCallingConventionValues` .ret_mcv = undefined, // populated after `resolveCallingConventionValues` .fn_type = fn_type, .arg_index = 0, .branch_stack = &branch_stack, .src_loc = src_loc, .stack_align = undefined, .end_di_line = module_fn.rbrace_line, .end_di_column = module_fn.rbrace_column, .mir_to_air_map = if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index).init(bin_file.allocator) else {}, }; defer function.stack.deinit(bin_file.allocator); defer function.blocks.deinit(bin_file.allocator); defer function.exitlude_jump_relocs.deinit(bin_file.allocator); defer function.mir_instructions.deinit(bin_file.allocator); defer function.mir_extra.deinit(bin_file.allocator); defer if (builtin.mode == .Debug) function.mir_to_air_map.deinit(); var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) { error.CodegenFail => return FnResult{ .fail = function.err_msg.? }, else => |e| return e, }; defer call_info.deinit(&function); function.args = call_info.args; function.ret_mcv = call_info.return_value; function.stack_align = call_info.stack_align; function.max_end_stack = call_info.stack_byte_count; function.gen() catch |err| switch (err) { error.CodegenFail => return FnResult{ .fail = function.err_msg.? }, else => |e| return e, }; var mir = Mir{ .function = &function, .instructions = function.mir_instructions.toOwnedSlice(), .extra = function.mir_extra.toOwnedSlice(bin_file.allocator), }; defer mir.deinit(bin_file.allocator); var isel = Isel{ .mir = mir, .bin_file = bin_file, .debug_output = debug_output, .target = &bin_file.options.target, .src_loc = src_loc, .code = code, .prev_di_pc = 0, .prev_di_line = module_fn.lbrace_line, .prev_di_column = module_fn.lbrace_column, }; defer isel.deinit(); isel.lowerMir() catch |err| switch (err) { error.IselFail => return FnResult{ .fail = isel.err_msg.? }, else => |e| return e, }; if (builtin.mode == .Debug and bin_file.options.module.?.comp.verbose_mir) { const w = std.io.getStdErr().writer(); w.print("# Begin Function MIR: {s}:\n", .{module_fn.owner_decl.name}) catch {}; const PrintMir = @import("PrintMir.zig"); const print = PrintMir{ .mir = mir, .bin_file = bin_file, }; print.printMir(w, function.mir_to_air_map, air) catch {}; // we don't care if the debug printing fails w.print("# End Function MIR: {s}\n\n", .{module_fn.owner_decl.name}) catch {}; } if (function.err_msg) |em| { return FnResult{ .fail = em }; } else { return FnResult{ .appended = {} }; } } fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { const gpa = self.gpa; try self.mir_instructions.ensureUnusedCapacity(gpa, 1); const result_index = @intCast(Air.Inst.Index, self.mir_instructions.len); self.mir_instructions.appendAssumeCapacity(inst); return result_index; } pub fn addExtra(self: *Self, extra: anytype) Allocator.Error!u32 { const fields = std.meta.fields(@TypeOf(extra)); try self.mir_extra.ensureUnusedCapacity(self.gpa, fields.len); return self.addExtraAssumeCapacity(extra); } pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 { const fields = std.meta.fields(@TypeOf(extra)); const result = @intCast(u32, self.mir_extra.items.len); inline for (fields) |field| { self.mir_extra.appendAssumeCapacity(switch (field.field_type) { u32 => @field(extra, field.name), i32 => @bitCast(u32, @field(extra, field.name)), else => @compileError("bad field type"), }); } return result; } fn gen(self: *Self) InnerError!void { const cc = self.fn_type.fnCallingConvention(); if (cc != .Naked) { // push the callee_preserved_regs that were used const backpatch_push_callee_preserved_regs_i = try self.addInst(.{ .tag = .push_regs_from_callee_preserved_regs, .ops = undefined, .data = .{ .regs_to_push_or_pop = undefined }, // to be backpatched }); _ = try self.addInst(.{ .tag = .push, .ops = (Mir.Ops{ .reg1 = .rbp, }).encode(), .data = undefined, // unused for push reg, }); _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = .rbp, .reg2 = .rsp, }).encode(), .data = undefined, }); // We want to subtract the aligned stack frame size from rsp here, but we don't // yet know how big it will be, so we leave room for a 4-byte stack size. // TODO During semantic analysis, check if there are no function calls. If there // are none, here we can omit the part where we subtract and then add rsp. const backpatch_stack_sub = try self.addInst(.{ .tag = .nop, .ops = undefined, .data = undefined, }); _ = try self.addInst(.{ .tag = .dbg_prologue_end, .ops = undefined, .data = undefined, }); try self.genBody(self.air.getMainBody()); if (self.exitlude_jump_relocs.items.len == 1) { self.mir_instructions.len -= 1; } else for (self.exitlude_jump_relocs.items) |jmp_reloc| { self.mir_instructions.items(.data)[jmp_reloc].inst = @intCast(u32, self.mir_instructions.len); } _ = try self.addInst(.{ .tag = .dbg_epilogue_begin, .ops = undefined, .data = undefined, }); // Maybe add rsp, x if required. This is backpatched later. const backpatch_stack_add = try self.addInst(.{ .tag = .nop, .ops = undefined, .data = undefined, }); _ = try self.addInst(.{ .tag = .pop, .ops = (Mir.Ops{ .reg1 = .rbp, }).encode(), .data = undefined, }); // calculate the data for callee_preserved_regs to be pushed and popped var callee_preserved_regs_push_data: u32 = 0x0; // TODO this is required on macOS since macOS actively checks for stack alignment // at every extern call site. As far as I can tell, macOS accounts for the typical // function prologue first 2 instructions of: // ... // push rbp // mov rsp, rbp // ... // Thus we don't need to adjust the stack for the first push instruction. However, // any subsequent push of values on the stack such as when preserving registers, // needs to be taken into account here. var stack_adjustment: i32 = 0; inline for (callee_preserved_regs) |reg, i| { if (self.register_manager.isRegAllocated(reg)) { callee_preserved_regs_push_data |= 1 << @intCast(u5, i); if (self.target.isDarwin()) { stack_adjustment += @divExact(reg.size(), 8); } } } const data = self.mir_instructions.items(.data); // backpatch the push instruction data[backpatch_push_callee_preserved_regs_i].regs_to_push_or_pop = callee_preserved_regs_push_data; // pop the callee_preserved_regs _ = try self.addInst(.{ .tag = .pop_regs_from_callee_preserved_regs, .ops = undefined, .data = .{ .regs_to_push_or_pop = callee_preserved_regs_push_data }, }); _ = try self.addInst(.{ .tag = .ret, .ops = (Mir.Ops{ .flags = 0b11, }).encode(), .data = undefined, }); // Adjust the stack const stack_end = self.max_end_stack; if (stack_end > math.maxInt(i32) - stack_adjustment) { return self.failSymbol("too much stack used in call parameters", .{}); } const aligned_stack_end = mem.alignForward(stack_end, self.stack_align); if (aligned_stack_end > 0 or stack_adjustment > 0) { self.mir_instructions.set(backpatch_stack_sub, .{ .tag = .sub, .ops = (Mir.Ops{ .reg1 = .rsp, }).encode(), .data = .{ .imm = @intCast(i32, aligned_stack_end) + stack_adjustment }, }); self.mir_instructions.set(backpatch_stack_add, .{ .tag = .add, .ops = (Mir.Ops{ .reg1 = .rsp, }).encode(), .data = .{ .imm = @intCast(i32, aligned_stack_end) + stack_adjustment }, }); } } else { _ = try self.addInst(.{ .tag = .dbg_prologue_end, .ops = undefined, .data = undefined, }); try self.genBody(self.air.getMainBody()); _ = try self.addInst(.{ .tag = .dbg_epilogue_begin, .ops = undefined, .data = undefined, }); } // Drop them off at the rbrace. const payload = try self.addExtra(Mir.DbgLineColumn{ .line = self.end_di_line, .column = self.end_di_column, }); _ = try self.addInst(.{ .tag = .dbg_line, .ops = undefined, .data = .{ .payload = payload }, }); } fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { const air_tags = self.air.instructions.items(.tag); for (body) |inst| { const old_air_bookkeeping = self.air_bookkeeping; try self.ensureProcessDeathCapacity(Liveness.bpi); if (builtin.mode == .Debug) { try self.mir_to_air_map.put(@intCast(u32, self.mir_instructions.len), inst); } switch (air_tags[inst]) { // zig fmt: off .add, .ptr_add => try self.airAdd(inst), .addwrap => try self.airAddWrap(inst), .add_sat => try self.airAddSat(inst), .sub, .ptr_sub => try self.airSub(inst), .subwrap => try self.airSubWrap(inst), .sub_sat => try self.airSubSat(inst), .mul => try self.airMul(inst), .mulwrap => try self.airMulWrap(inst), .mul_sat => try self.airMulSat(inst), .rem => try self.airRem(inst), .mod => try self.airMod(inst), .shl, .shl_exact => try self.airShl(inst), .shl_sat => try self.airShlSat(inst), .min => try self.airMin(inst), .max => try self.airMax(inst), .slice => try self.airSlice(inst), .add_with_overflow => try self.airAddWithOverflow(inst), .sub_with_overflow => try self.airSubWithOverflow(inst), .mul_with_overflow => try self.airMulWithOverflow(inst), .shl_with_overflow => try self.airShlWithOverflow(inst), .div_float, .div_trunc, .div_floor, .div_exact => try self.airDiv(inst), .cmp_lt => try self.airCmp(inst, .lt), .cmp_lte => try self.airCmp(inst, .lte), .cmp_eq => try self.airCmp(inst, .eq), .cmp_gte => try self.airCmp(inst, .gte), .cmp_gt => try self.airCmp(inst, .gt), .cmp_neq => try self.airCmp(inst, .neq), .bool_and => try self.airBoolOp(inst), .bool_or => try self.airBoolOp(inst), .bit_and => try self.airBitAnd(inst), .bit_or => try self.airBitOr(inst), .xor => try self.airXor(inst), .shr => try self.airShr(inst), .alloc => try self.airAlloc(inst), .ret_ptr => try self.airRetPtr(inst), .arg => try self.airArg(inst), .assembly => try self.airAsm(inst), .bitcast => try self.airBitCast(inst), .block => try self.airBlock(inst), .br => try self.airBr(inst), .breakpoint => try self.airBreakpoint(), .ret_addr => try self.airRetAddr(), .fence => try self.airFence(), .call => try self.airCall(inst), .cond_br => try self.airCondBr(inst), .dbg_stmt => try self.airDbgStmt(inst), .fptrunc => try self.airFptrunc(inst), .fpext => try self.airFpext(inst), .intcast => try self.airIntCast(inst), .trunc => try self.airTrunc(inst), .bool_to_int => try self.airBoolToInt(inst), .is_non_null => try self.airIsNonNull(inst), .is_non_null_ptr => try self.airIsNonNullPtr(inst), .is_null => try self.airIsNull(inst), .is_null_ptr => try self.airIsNullPtr(inst), .is_non_err => try self.airIsNonErr(inst), .is_non_err_ptr => try self.airIsNonErrPtr(inst), .is_err => try self.airIsErr(inst), .is_err_ptr => try self.airIsErrPtr(inst), .load => try self.airLoad(inst), .loop => try self.airLoop(inst), .not => try self.airNot(inst), .ptrtoint => try self.airPtrToInt(inst), .ret => try self.airRet(inst), .ret_load => try self.airRetLoad(inst), .store => try self.airStore(inst), .struct_field_ptr=> try self.airStructFieldPtr(inst), .struct_field_val=> try self.airStructFieldVal(inst), .array_to_slice => try self.airArrayToSlice(inst), .int_to_float => try self.airIntToFloat(inst), .float_to_int => try self.airFloatToInt(inst), .cmpxchg_strong => try self.airCmpxchg(inst), .cmpxchg_weak => try self.airCmpxchg(inst), .atomic_rmw => try self.airAtomicRmw(inst), .atomic_load => try self.airAtomicLoad(inst), .memcpy => try self.airMemcpy(inst), .memset => try self.airMemset(inst), .set_union_tag => try self.airSetUnionTag(inst), .get_union_tag => try self.airGetUnionTag(inst), .clz => try self.airClz(inst), .ctz => try self.airCtz(inst), .popcount => try self.airPopcount(inst), .tag_name => try self.airTagName(inst), .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered), .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic), .atomic_store_release => try self.airAtomicStore(inst, .Release), .atomic_store_seq_cst => try self.airAtomicStore(inst, .SeqCst), .struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0), .struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1), .struct_field_ptr_index_2 => try self.airStructFieldPtrIndex(inst, 2), .struct_field_ptr_index_3 => try self.airStructFieldPtrIndex(inst, 3), .switch_br => try self.airSwitch(inst), .slice_ptr => try self.airSlicePtr(inst), .slice_len => try self.airSliceLen(inst), .ptr_slice_len_ptr => try self.airPtrSliceLenPtr(inst), .ptr_slice_ptr_ptr => try self.airPtrSlicePtrPtr(inst), .array_elem_val => try self.airArrayElemVal(inst), .slice_elem_val => try self.airSliceElemVal(inst), .slice_elem_ptr => try self.airSliceElemPtr(inst), .ptr_elem_val => try self.airPtrElemVal(inst), .ptr_elem_ptr => try self.airPtrElemPtr(inst), .constant => unreachable, // excluded from function bodies .const_ty => unreachable, // excluded from function bodies .unreach => self.finishAirBookkeeping(), .optional_payload => try self.airOptionalPayload(inst), .optional_payload_ptr => try self.airOptionalPayloadPtr(inst), .optional_payload_ptr_set => try self.airOptionalPayloadPtrSet(inst), .unwrap_errunion_err => try self.airUnwrapErrErr(inst), .unwrap_errunion_payload => try self.airUnwrapErrPayload(inst), .unwrap_errunion_err_ptr => try self.airUnwrapErrErrPtr(inst), .unwrap_errunion_payload_ptr=> try self.airUnwrapErrPayloadPtr(inst), .wrap_optional => try self.airWrapOptional(inst), .wrap_errunion_payload => try self.airWrapErrUnionPayload(inst), .wrap_errunion_err => try self.airWrapErrUnionErr(inst), // zig fmt: on } if (std.debug.runtime_safety) { if (self.air_bookkeeping < old_air_bookkeeping + 1) { std.debug.panic("in codegen.zig, handling of AIR instruction %{d} ('{}') did not do proper bookkeeping. Look for a missing call to finishAir.", .{ inst, air_tags[inst] }); } } } } /// Asserts there is already capacity to insert into top branch inst_table. fn processDeath(self: *Self, inst: Air.Inst.Index) void { const air_tags = self.air.instructions.items(.tag); if (air_tags[inst] == .constant) return; // Constants are immortal. // When editing this function, note that the logic must synchronize with `reuseOperand`. const prev_value = self.getResolvedInstValue(inst); const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; branch.inst_table.putAssumeCapacity(inst, .dead); switch (prev_value) { .register => |reg| { const canon_reg = reg.to64(); self.register_manager.freeReg(canon_reg); }, else => {}, // TODO process stack allocation death } } /// Called when there are no operands, and the instruction is always unreferenced. fn finishAirBookkeeping(self: *Self) void { if (std.debug.runtime_safety) { self.air_bookkeeping += 1; } } fn finishAir(self: *Self, inst: Air.Inst.Index, result: MCValue, operands: [Liveness.bpi - 1]Air.Inst.Ref) void { var tomb_bits = self.liveness.getTombBits(inst); for (operands) |op| { const dies = @truncate(u1, tomb_bits) != 0; tomb_bits >>= 1; if (!dies) continue; const op_int = @enumToInt(op); if (op_int < Air.Inst.Ref.typed_value_map.len) continue; const op_index = @intCast(Air.Inst.Index, op_int - Air.Inst.Ref.typed_value_map.len); self.processDeath(op_index); } const is_used = @truncate(u1, tomb_bits) == 0; if (is_used) { log.debug("%{d} => {}", .{ inst, result }); const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; branch.inst_table.putAssumeCapacityNoClobber(inst, result); switch (result) { .register => |reg| { // In some cases (such as bitcast), an operand // may be the same MCValue as the result. If // that operand died and was a register, it // was freed by processDeath. We have to // "re-allocate" the register. if (self.register_manager.isRegFree(reg)) { self.register_manager.getRegAssumeFree(reg, inst); } }, else => {}, } } self.finishAirBookkeeping(); } fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { const table = &self.branch_stack.items[self.branch_stack.items.len - 1].inst_table; try table.ensureUnusedCapacity(self.gpa, additional_count); } fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: u32) !u32 { if (abi_align > self.stack_align) self.stack_align = abi_align; // TODO find a free slot instead of always appending const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align); self.next_stack_offset = offset + abi_size; if (self.next_stack_offset > self.max_end_stack) self.max_end_stack = self.next_stack_offset; try self.stack.putNoClobber(self.gpa, offset, .{ .inst = inst, .size = abi_size, }); return offset; } /// Use a pointer instruction as the basis for allocating stack memory. fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { const elem_ty = self.air.typeOfIndex(inst).elemType(); const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); }; // TODO swap this for inst.ty.ptrAlign const abi_align = elem_ty.abiAlignment(self.target.*); return self.allocMem(inst, abi_size, abi_align); } fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { const elem_ty = self.air.typeOfIndex(inst); const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); }; const abi_align = elem_ty.abiAlignment(self.target.*); if (abi_align > self.stack_align) self.stack_align = abi_align; if (reg_ok) { // Make sure the type can fit in a register before we try to allocate one. const ptr_bits = self.target.cpu.arch.ptrBitWidth(); const ptr_bytes: u64 = @divExact(ptr_bits, 8); if (abi_size <= ptr_bytes) { if (self.register_manager.tryAllocReg(inst, &.{})) |reg| { return MCValue{ .register = registerAlias(reg, abi_size) }; } } } const stack_offset = try self.allocMem(inst, abi_size, abi_align); return MCValue{ .stack_offset = stack_offset }; } pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void { const stack_mcv = try self.allocRegOrMem(inst, false); log.debug("spilling {d} to stack mcv {any}", .{ inst, stack_mcv }); const reg_mcv = self.getResolvedInstValue(inst); assert(reg == reg_mcv.register.to64()); const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; try branch.inst_table.put(self.gpa, inst, stack_mcv); try self.genSetStack(self.air.typeOfIndex(inst), stack_mcv.stack_offset, reg_mcv); } /// Copies a value to a register without tracking the register. The register is not considered /// allocated. A second call to `copyToTmpRegister` may return the same register. /// This can have a side effect of spilling instructions to the stack to free up a register. fn copyToTmpRegister(self: *Self, ty: Type, mcv: MCValue) !Register { const reg = try self.register_manager.allocReg(null, &.{}); try self.genSetReg(ty, reg, mcv); return reg; } /// Allocates a new register and copies `mcv` into it. /// `reg_owner` is the instruction that gets associated with the register in the register table. /// This can have a side effect of spilling instructions to the stack to free up a register. fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue { const reg = try self.register_manager.allocReg(reg_owner, &.{}); try self.genSetReg(self.air.typeOfIndex(reg_owner), reg, mcv); return MCValue{ .register = reg }; } /// Like `copyToNewRegister` but allows to specify a list of excluded registers which /// will not be selected for allocation. This can be done via `exceptions` slice. fn copyToNewRegisterWithExceptions( self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue, exceptions: []const Register, ) !MCValue { const reg = try self.register_manager.allocReg(reg_owner, exceptions); try self.genSetReg(self.air.typeOfIndex(reg_owner), reg, mcv); return MCValue{ .register = reg }; } fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { const stack_offset = try self.allocMemPtr(inst); return self.finishAir(inst, .{ .ptr_stack_offset = stack_offset }, .{ .none, .none, .none }); } fn airRetPtr(self: *Self, inst: Air.Inst.Index) !void { const stack_offset = try self.allocMemPtr(inst); return self.finishAir(inst, .{ .ptr_stack_offset = stack_offset }, .{ .none, .none, .none }); } fn airFptrunc(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; _ = ty_op; return self.fail("TODO implement airFptrunc for {}", .{self.target.cpu.arch}); // return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airFpext(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; _ = ty_op; return self.fail("TODO implement airFpext for {}", .{self.target.cpu.arch}); // return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); const operand_ty = self.air.typeOf(ty_op.operand); const operand = try self.resolveInst(ty_op.operand); const info_a = operand_ty.intInfo(self.target.*); const info_b = self.air.typeOfIndex(inst).intInfo(self.target.*); if (info_a.signedness != info_b.signedness) return self.fail("TODO gen intcast sign safety in semantic analysis", .{}); if (info_a.bits == info_b.bits) return self.finishAir(inst, operand, .{ ty_op.operand, .none, .none }); return self.fail("TODO implement intCast for {}", .{self.target.cpu.arch}); } fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); const operand = try self.resolveInst(ty_op.operand); _ = operand; return self.fail("TODO implement trunc for {}", .{self.target.cpu.arch}); } fn airBoolToInt(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const operand = try self.resolveInst(un_op); const result: MCValue = if (self.liveness.isUnused(inst)) .dead else operand; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airNot(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand = try self.resolveInst(ty_op.operand); switch (operand) { .dead => unreachable, .unreach => unreachable, .compare_flags_unsigned => |op| { const r = MCValue{ .compare_flags_unsigned = switch (op) { .gte => .lt, .gt => .lte, .neq => .eq, .lt => .gte, .lte => .gt, .eq => .neq, }, }; break :result r; }, .compare_flags_signed => |op| { const r = MCValue{ .compare_flags_signed = switch (op) { .gte => .lt, .gt => .lte, .neq => .eq, .lt => .gte, .lte => .gt, .eq => .neq, }, }; break :result r; }, else => {}, } break :result try self.genBinMathOp(inst, ty_op.operand, .bool_true); }; return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airMin(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement min for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airMax(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement max for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airSlice(self: *Self, inst: Air.Inst.Index) !void { const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement slice for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airAdd(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airAddWrap(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement addwrap for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement add_sat for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airSub(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airSubWrap(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement subwrap for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airSubSat(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement sub_sat for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airMul(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airMulWrap(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement mulwrap for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement mul_sat for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airAddWithOverflow for {}", .{self.target.cpu.arch}); } fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airSubWithOverflow for {}", .{self.target.cpu.arch}); } fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airMulWithOverflow for {}", .{self.target.cpu.arch}); } fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airShlWithOverflow for {}", .{self.target.cpu.arch}); } fn airDiv(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement div for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airRem(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement rem for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airMod(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement mod for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airBitAnd(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airBitOr(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airXor(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement xor for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airShl(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement shl for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airShlSat(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement shl_sat for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airShr(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement shr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand = try self.resolveInst(ty_op.operand); if (self.wantSafety()) { // TODO check for null return self.fail("TODO implement check for null in .optional_payload", .{}); } const dst_mcv: MCValue = blk: { if (self.reuseOperand(inst, ty_op.operand, 0, operand)) { break :blk operand; } else { break :blk try self.allocRegOrMem(inst, true); } }; const ty = self.air.typeOf(ty_op.operand); var buf: Type.Payload.ElemType = undefined; try self.load(dst_mcv, operand, ty.optionalChild(&buf)); break :result dst_mcv; }; return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement .optional_payload_ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement .optional_payload_ptr_set for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement unwrap error union error for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement unwrap error union payload for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } // *(E!T) -> E fn airUnwrapErrErrPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement unwrap error union error ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } // *(E!T) -> *T fn airUnwrapErrPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement unwrap error union payload ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const optional_ty = self.air.typeOfIndex(inst); // Optional with a zero-bit payload type is just a boolean true if (optional_ty.abiSize(self.target.*) == 1) break :result MCValue{ .immediate = 1 }; return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch}); }; return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } /// T to E!T fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement wrap errunion payload for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } /// E to E!T fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement wrap errunion error for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airSlicePtr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement slice_ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement slice_len for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement ptr_slice_len_ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airPtrSlicePtrPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement ptr_slice_ptr_ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { const is_volatile = false; // TODO const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (!is_volatile and self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement slice_elem_val for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement slice_elem_ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); } fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement array_elem_val for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { const is_volatile = false; // TODO const bin_op = self.air.instructions.items(.data)[inst].bin_op; const result: MCValue = if (!is_volatile and self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement ptr_elem_val for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement ptr_elem_ptr for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); } fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; _ = bin_op; return self.fail("TODO implement airSetUnionTag for {}", .{self.target.cpu.arch}); } fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airGetUnionTag for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airClz(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airClz for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airCtz(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airCtz for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airPopcount(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airPopcount for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn reuseOperand(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, op_index: Liveness.OperandInt, mcv: MCValue) bool { if (!self.liveness.operandDies(inst, op_index)) return false; switch (mcv) { .register => |reg| { // If it's in the registers table, need to associate the register with the // new instruction. if (reg.allocIndex()) |index| { if (!self.register_manager.isRegFree(reg)) { self.register_manager.registers[index] = inst; } } log.debug("%{d} => {} (reused)", .{ inst, reg }); }, .stack_offset => |off| { log.debug("%{d} => stack offset {d} (reused)", .{ inst, off }); }, else => return false, } // Prevent the operand deaths processing code from deallocating it. self.liveness.clearOperandDeath(inst, op_index); // That makes us responsible for doing the rest of the stuff that processDeath would have done. const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; branch.inst_table.putAssumeCapacity(Air.refToIndex(operand).?, .dead); return true; } fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!void { const elem_ty = ptr_ty.elemType(); switch (ptr) { .none => unreachable, .undef => unreachable, .unreach => unreachable, .dead => unreachable, .compare_flags_unsigned => unreachable, .compare_flags_signed => unreachable, .immediate => |imm| try self.setRegOrMem(elem_ty, dst_mcv, .{ .memory = imm }), .ptr_stack_offset => |off| { try self.setRegOrMem(elem_ty, dst_mcv, .{ .stack_offset = off }); }, .ptr_embedded_in_code => |off| { try self.setRegOrMem(elem_ty, dst_mcv, .{ .embedded_in_code = off }); }, .embedded_in_code => { return self.fail("TODO implement loading from MCValue.embedded_in_code", .{}); }, .register => |reg| try self.setRegOrMem(elem_ty, dst_mcv, .{ .register = reg }), .memory => |addr| { const reg = try self.register_manager.allocReg(null, &.{}); try self.genSetReg(ptr_ty, reg, .{ .memory = addr }); try self.load(dst_mcv, .{ .register = reg }, ptr_ty); }, .stack_offset => { return self.fail("TODO implement loading from MCValue.stack_offset", .{}); }, } } fn airLoad(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const elem_ty = self.air.typeOfIndex(inst); const result: MCValue = result: { if (!elem_ty.hasCodeGenBits()) break :result MCValue.none; const ptr = try self.resolveInst(ty_op.operand); const is_volatile = self.air.typeOf(ty_op.operand).isVolatilePtr(); if (self.liveness.isUnused(inst) and !is_volatile) break :result MCValue.dead; const dst_mcv: MCValue = blk: { if (self.reuseOperand(inst, ty_op.operand, 0, ptr)) { // The MCValue that holds the pointer can be re-used as the value. break :blk ptr; } else { break :blk try self.allocRegOrMem(inst, true); } }; try self.load(dst_mcv, ptr, self.air.typeOf(ty_op.operand)); break :result dst_mcv; }; return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airStore(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const ptr = try self.resolveInst(bin_op.lhs); const value = try self.resolveInst(bin_op.rhs); const elem_ty = self.air.typeOf(bin_op.rhs); switch (ptr) { .none => unreachable, .undef => unreachable, .unreach => unreachable, .dead => unreachable, .compare_flags_unsigned => unreachable, .compare_flags_signed => unreachable, .immediate => |imm| { try self.setRegOrMem(elem_ty, .{ .memory = imm }, value); }, .ptr_stack_offset => |off| { try self.genSetStack(elem_ty, off, value); }, .ptr_embedded_in_code => |off| { try self.setRegOrMem(elem_ty, .{ .embedded_in_code = off }, value); }, .embedded_in_code => { return self.fail("TODO implement storing to MCValue.embedded_in_code", .{}); }, .register => |reg| { try self.genSetPtrReg(elem_ty, reg, value); }, .memory => { return self.fail("TODO implement storing to MCValue.memory", .{}); }, .stack_offset => { return self.fail("TODO implement storing to MCValue.stack_offset", .{}); }, } return self.finishAir(inst, .dead, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) !void { const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; return self.structFieldPtr(extra.struct_operand, ty_pl.ty, extra.field_index); } fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; return self.structFieldPtr(ty_op.operand, ty_op.ty, index); } fn structFieldPtr(self: *Self, operand: Air.Inst.Ref, ty: Air.Inst.Ref, index: u32) !void { _ = self; _ = operand; _ = ty; _ = index; return self.fail("TODO implement codegen struct_field_ptr", .{}); //return self.finishAir(inst, result, .{ extra.struct_ptr, .none, .none }); } fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; _ = extra; return self.fail("TODO implement codegen struct_field_val", .{}); //return self.finishAir(inst, result, .{ extra.struct_ptr, .none, .none }); } /// Perform "binary" operators, excluding comparisons. /// Currently, the following ops are supported: /// ADD, SUB, XOR, OR, AND fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs: Air.Inst.Ref) !MCValue { // We'll handle these ops in two steps. // 1) Prepare an output location (register or memory) // This location will be the location of the operand that dies (if one exists) // or just a temporary register (if one doesn't exist) // 2) Perform the op with the other argument // 3) Sometimes, the output location is memory but the op doesn't support it. // In this case, copy that location to a register, then perform the op to that register instead. // // TODO: make this algorithm less bad const lhs = try self.resolveInst(op_lhs); const rhs = try self.resolveInst(op_rhs); // There are 2 operands, destination and source. // Either one, but not both, can be a memory operand. // Source operand can be an immediate, 8 bits or 32 bits. // So, if either one of the operands dies with this instruction, we can use it // as the result MCValue. var dst_mcv: MCValue = undefined; var src_mcv: MCValue = undefined; if (self.reuseOperand(inst, op_lhs, 0, lhs)) { // LHS dies; use it as the destination. // Both operands cannot be memory. if (lhs.isMemory() and rhs.isMemory()) { dst_mcv = try self.copyToNewRegister(inst, lhs); src_mcv = rhs; } else { dst_mcv = lhs; src_mcv = rhs; } } else if (self.reuseOperand(inst, op_rhs, 1, rhs)) { // RHS dies; use it as the destination. // Both operands cannot be memory. if (lhs.isMemory() and rhs.isMemory()) { dst_mcv = try self.copyToNewRegister(inst, rhs); src_mcv = lhs; } else { dst_mcv = rhs; src_mcv = lhs; } } else { if (lhs.isMemory()) { dst_mcv = if (rhs.isRegister()) // If the allocated register is the same as the rhs register, don't allocate that one // and instead spill a subsequent one. Otherwise, this can result in a miscompilation // in the presence of several binary operations performed in a single block. try self.copyToNewRegisterWithExceptions(inst, lhs, &.{rhs.register}) else try self.copyToNewRegister(inst, lhs); src_mcv = rhs; } else { dst_mcv = if (lhs.isRegister()) // If the allocated register is the same as the rhs register, don't allocate that one // and instead spill a subsequent one. Otherwise, this can result in a miscompilation // in the presence of several binary operations performed in a single block. try self.copyToNewRegisterWithExceptions(inst, rhs, &.{lhs.register}) else try self.copyToNewRegister(inst, rhs); src_mcv = lhs; } } // This instruction supports only signed 32-bit immediates at most. If the immediate // value is larger than this, we put it in a register. // A potential opportunity for future optimization here would be keeping track // of the fact that the instruction is available both as an immediate // and as a register. switch (src_mcv) { .immediate => |imm| { if (imm > math.maxInt(u31)) { src_mcv = MCValue{ .register = try self.copyToTmpRegister(Type.initTag(.u64), src_mcv) }; } }, else => {}, } // Now for step 2, we assing an MIR instruction const dst_ty = self.air.typeOfIndex(inst); const air_tags = self.air.instructions.items(.tag); switch (air_tags[inst]) { .add, .addwrap => try self.genBinMathOpMir(.add, dst_ty, dst_mcv, src_mcv), .bool_or, .bit_or => try self.genBinMathOpMir(.@"or", dst_ty, dst_mcv, src_mcv), .bool_and, .bit_and => try self.genBinMathOpMir(.@"and", dst_ty, dst_mcv, src_mcv), .sub, .subwrap => try self.genBinMathOpMir(.sub, dst_ty, dst_mcv, src_mcv), .xor, .not => try self.genBinMathOpMir(.xor, dst_ty, dst_mcv, src_mcv), .mul, .mulwrap => try self.genIMulOpMir(dst_ty, dst_mcv, src_mcv), else => unreachable, } return dst_mcv; } fn genBinMathOpMir( self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue, ) !void { switch (dst_mcv) { .none => unreachable, .undef => unreachable, .dead, .unreach, .immediate => unreachable, .compare_flags_unsigned => unreachable, .compare_flags_signed => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .register => |dst_reg| { switch (src_mcv) { .none => unreachable, .undef => try self.genSetReg(dst_ty, dst_reg, .undef), .dead, .unreach => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .register => |src_reg| { _ = try self.addInst(.{ .tag = mir_tag, .ops = (Mir.Ops{ .reg1 = registerAlias(dst_reg, @divExact(src_reg.size(), 8)), .reg2 = src_reg, }).encode(), .data = undefined, }); }, .immediate => |imm| { const abi_size = dst_ty.abiSize(self.target.*); _ = try self.addInst(.{ .tag = mir_tag, .ops = (Mir.Ops{ .reg1 = registerAlias(dst_reg, @intCast(u32, abi_size)), }).encode(), .data = .{ .imm = @intCast(i32, imm) }, }); }, .embedded_in_code, .memory => { return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{}); }, .stack_offset => |off| { if (off > math.maxInt(i32)) { return self.fail("stack offset too large", .{}); } const abi_size = dst_ty.abiSize(self.target.*); const adj_off = off + abi_size; _ = try self.addInst(.{ .tag = mir_tag, .ops = (Mir.Ops{ .reg1 = registerAlias(dst_reg, @intCast(u32, abi_size)), .reg2 = .rbp, .flags = 0b01, }).encode(), .data = .{ .imm = -@intCast(i32, adj_off) }, }); }, .compare_flags_unsigned => { return self.fail("TODO implement x86 ADD/SUB/CMP source compare flag (unsigned)", .{}); }, .compare_flags_signed => { return self.fail("TODO implement x86 ADD/SUB/CMP source compare flag (signed)", .{}); }, } }, .stack_offset => |off| { switch (src_mcv) { .none => unreachable, .undef => return self.genSetStack(dst_ty, off, .undef), .dead, .unreach => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .register => |src_reg| { if (off > math.maxInt(i32)) { return self.fail("stack offset too large", .{}); } const abi_size = dst_ty.abiSize(self.target.*); const adj_off = off + abi_size; _ = try self.addInst(.{ .tag = mir_tag, .ops = (Mir.Ops{ .reg1 = .rbp, .reg2 = registerAlias(src_reg, @intCast(u32, abi_size)), .flags = 0b10, }).encode(), .data = .{ .imm = -@intCast(i32, adj_off) }, }); }, .immediate => |imm| { _ = imm; return self.fail("TODO implement x86 ADD/SUB/CMP source immediate", .{}); }, .embedded_in_code, .memory, .stack_offset => { return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{}); }, .compare_flags_unsigned => { return self.fail("TODO implement x86 ADD/SUB/CMP source compare flag (unsigned)", .{}); }, .compare_flags_signed => { return self.fail("TODO implement x86 ADD/SUB/CMP source compare flag (signed)", .{}); }, } }, .embedded_in_code, .memory => { return self.fail("TODO implement x86 ADD/SUB/CMP destination memory", .{}); }, } } // Performs integer multiplication between dst_mcv and src_mcv, storing the result in dst_mcv. fn genIMulOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { switch (dst_mcv) { .none => unreachable, .undef => unreachable, .dead, .unreach, .immediate => unreachable, .compare_flags_unsigned => unreachable, .compare_flags_signed => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .register => |dst_reg| { switch (src_mcv) { .none => unreachable, .undef => try self.genSetReg(dst_ty, dst_reg, .undef), .dead, .unreach => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .register => |src_reg| { // register, register _ = try self.addInst(.{ .tag = .imul_complex, .ops = (Mir.Ops{ .reg1 = registerAlias(dst_reg, @divExact(src_reg.size(), 8)), .reg2 = src_reg, }).encode(), .data = undefined, }); }, .immediate => |imm| { // TODO take into account the type's ABI size when selecting the register alias // register, immediate if (imm <= math.maxInt(i32)) { _ = try self.addInst(.{ .tag = .imul_complex, .ops = (Mir.Ops{ .reg1 = dst_reg.to32(), .reg2 = dst_reg.to32(), .flags = 0b10, }).encode(), .data = .{ .imm = @intCast(i32, imm) }, }); } else { const src_reg = try self.copyToTmpRegister(dst_ty, src_mcv); return self.genIMulOpMir(dst_ty, dst_mcv, MCValue{ .register = src_reg }); } }, .embedded_in_code, .memory, .stack_offset => { return self.fail("TODO implement x86 multiply source memory", .{}); }, .compare_flags_unsigned => { return self.fail("TODO implement x86 multiply source compare flag (unsigned)", .{}); }, .compare_flags_signed => { return self.fail("TODO implement x86 multiply source compare flag (signed)", .{}); }, } }, .stack_offset => |off| { switch (src_mcv) { .none => unreachable, .undef => return self.genSetStack(dst_ty, off, .undef), .dead, .unreach => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .register => |src_reg| { // copy dst to a register const dst_reg = try self.copyToTmpRegister(dst_ty, dst_mcv); // multiply into dst_reg // register, register _ = try self.addInst(.{ .tag = .imul_complex, .ops = (Mir.Ops{ .reg1 = registerAlias(dst_reg, @divExact(src_reg.size(), 8)), .reg2 = src_reg, }).encode(), .data = undefined, }); // copy dst_reg back out return self.genSetStack(dst_ty, off, MCValue{ .register = dst_reg }); }, .immediate => |imm| { _ = imm; return self.fail("TODO implement x86 multiply source immediate", .{}); }, .embedded_in_code, .memory, .stack_offset => { return self.fail("TODO implement x86 multiply source memory", .{}); }, .compare_flags_unsigned => { return self.fail("TODO implement x86 multiply source compare flag (unsigned)", .{}); }, .compare_flags_signed => { return self.fail("TODO implement x86 multiply source compare flag (signed)", .{}); }, } }, .embedded_in_code, .memory => { return self.fail("TODO implement x86 multiply destination memory", .{}); }, } } fn airArg(self: *Self, inst: Air.Inst.Index) !void { const arg_index = self.arg_index; self.arg_index += 1; const ty = self.air.typeOfIndex(inst); _ = ty; const mcv = self.args[arg_index]; const payload = try self.addExtra(Mir.ArgDbgInfo{ .air_inst = inst, .arg_index = @intCast(u32, arg_index), // TODO can arg_index: u32? }); _ = try self.addInst(.{ .tag = .arg_dbg_info, .ops = undefined, .data = .{ .payload = payload }, }); if (self.liveness.isUnused(inst)) return self.finishAirBookkeeping(); switch (mcv) { .register => |reg| { self.register_manager.getRegAssumeFree(reg.to64(), inst); }, else => {}, } return self.finishAir(inst, mcv, .{ .none, .none, .none }); } fn airBreakpoint(self: *Self) !void { _ = try self.addInst(.{ .tag = .brk, .ops = undefined, .data = undefined, }); return self.finishAirBookkeeping(); } fn airRetAddr(self: *Self) !void { return self.fail("TODO implement airRetAddr for {}", .{self.target.cpu.arch}); } fn airFence(self: *Self) !void { return self.fail("TODO implement fence() for {}", .{self.target.cpu.arch}); //return self.finishAirBookkeeping(); } fn airCall(self: *Self, inst: Air.Inst.Index) !void { const pl_op = self.air.instructions.items(.data)[inst].pl_op; const fn_ty = self.air.typeOf(pl_op.operand); const callee = pl_op.operand; const extra = self.air.extraData(Air.Call, pl_op.payload); const args = @bitCast([]const Air.Inst.Ref, self.air.extra[extra.end..][0..extra.data.args_len]); var info = try self.resolveCallingConventionValues(fn_ty); defer info.deinit(self); // Due to incremental compilation, how function calls are generated depends // on linking. if (self.bin_file.tag == link.File.Elf.base_tag or self.bin_file.tag == link.File.Coff.base_tag) { for (info.args) |mc_arg, arg_i| { const arg = args[arg_i]; const arg_ty = self.air.typeOf(arg); const arg_mcv = try self.resolveInst(args[arg_i]); // Here we do not use setRegOrMem even though the logic is similar, because // the function call will move the stack pointer, so the offsets are different. switch (mc_arg) { .none => continue, .register => |reg| { try self.register_manager.getReg(reg, null); try self.genSetReg(arg_ty, reg, arg_mcv); }, .stack_offset => |off| { // Here we need to emit instructions like this: // mov qword ptr [rsp + stack_offset], x try self.genSetStack(arg_ty, off, arg_mcv); }, .ptr_stack_offset => { return self.fail("TODO implement calling with MCValue.ptr_stack_offset arg", .{}); }, .ptr_embedded_in_code => { return self.fail("TODO implement calling with MCValue.ptr_embedded_in_code arg", .{}); }, .undef => unreachable, .immediate => unreachable, .unreach => unreachable, .dead => unreachable, .embedded_in_code => unreachable, .memory => unreachable, .compare_flags_signed => unreachable, .compare_flags_unsigned => unreachable, } } if (self.air.value(callee)) |func_value| { if (func_value.castTag(.function)) |func_payload| { const func = func_payload.data; const ptr_bits = self.target.cpu.arch.ptrBitWidth(); const ptr_bytes: u64 = @divExact(ptr_bits, 8); const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: { const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?]; break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes); } else if (self.bin_file.cast(link.File.Coff)) |coff_file| @intCast(u32, coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes) else unreachable; _ = try self.addInst(.{ .tag = .call, .ops = (Mir.Ops{ .flags = 0b01, }).encode(), .data = .{ .imm = @bitCast(i32, got_addr) }, }); } else if (func_value.castTag(.extern_fn)) |_| { return self.fail("TODO implement calling extern functions", .{}); } else { return self.fail("TODO implement calling bitcasted functions", .{}); } } else { return self.fail("TODO implement calling runtime known function pointer", .{}); } } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { for (info.args) |mc_arg, arg_i| { const arg = args[arg_i]; const arg_ty = self.air.typeOf(arg); const arg_mcv = try self.resolveInst(args[arg_i]); // Here we do not use setRegOrMem even though the logic is similar, because // the function call will move the stack pointer, so the offsets are different. switch (mc_arg) { .none => continue, .register => |reg| { // TODO prevent this macho if block to be generated for all archs try self.register_manager.getReg(reg, null); try self.genSetReg(arg_ty, reg, arg_mcv); }, .stack_offset => |off| { // Here we need to emit instructions like this: // mov qword ptr [rsp + stack_offset], x try self.genSetStack(arg_ty, off, arg_mcv); }, .ptr_stack_offset => { return self.fail("TODO implement calling with MCValue.ptr_stack_offset arg", .{}); }, .ptr_embedded_in_code => { return self.fail("TODO implement calling with MCValue.ptr_embedded_in_code arg", .{}); }, .undef => unreachable, .immediate => unreachable, .unreach => unreachable, .dead => unreachable, .embedded_in_code => unreachable, .memory => unreachable, .compare_flags_signed => unreachable, .compare_flags_unsigned => unreachable, } } if (self.air.value(callee)) |func_value| { if (func_value.castTag(.function)) |func_payload| { const func = func_payload.data; // TODO I'm hacking my way through here by repurposing .memory for storing // index to the GOT target symbol index. try self.genSetReg(Type.initTag(.u64), .rax, .{ .memory = func.owner_decl.link.macho.local_sym_index, }); // callq *%rax _ = try self.addInst(.{ .tag = .call, .ops = (Mir.Ops{ .reg1 = .rax, .flags = 0b01, }).encode(), .data = undefined, }); } else if (func_value.castTag(.extern_fn)) |func_payload| { const decl = func_payload.data; const n_strx = try macho_file.addExternFn(mem.sliceTo(decl.name, 0)); _ = try self.addInst(.{ .tag = .call_extern, .ops = undefined, .data = .{ .extern_fn = n_strx }, }); } else { return self.fail("TODO implement calling bitcasted functions", .{}); } } else { return self.fail("TODO implement calling runtime known function pointer", .{}); } } else if (self.bin_file.cast(link.File.Plan9)) |p9| { for (info.args) |mc_arg, arg_i| { const arg = args[arg_i]; const arg_ty = self.air.typeOf(arg); const arg_mcv = try self.resolveInst(args[arg_i]); // Here we do not use setRegOrMem even though the logic is similar, because // the function call will move the stack pointer, so the offsets are different. switch (mc_arg) { .none => continue, .register => |reg| { try self.register_manager.getReg(reg, null); try self.genSetReg(arg_ty, reg, arg_mcv); }, .stack_offset => |off| { // Here we need to emit instructions like this: // mov qword ptr [rsp + stack_offset], x try self.genSetStack(arg_ty, off, arg_mcv); }, .ptr_stack_offset => { return self.fail("TODO implement calling with MCValue.ptr_stack_offset arg", .{}); }, .ptr_embedded_in_code => { return self.fail("TODO implement calling with MCValue.ptr_embedded_in_code arg", .{}); }, .undef => unreachable, .immediate => unreachable, .unreach => unreachable, .dead => unreachable, .embedded_in_code => unreachable, .memory => unreachable, .compare_flags_signed => unreachable, .compare_flags_unsigned => unreachable, } } if (self.air.value(callee)) |func_value| { if (func_value.castTag(.function)) |func_payload| { try p9.seeDecl(func_payload.data.owner_decl); const ptr_bits = self.target.cpu.arch.ptrBitWidth(); const ptr_bytes: u64 = @divExact(ptr_bits, 8); const got_addr = p9.bases.data; const got_index = func_payload.data.owner_decl.link.plan9.got_index.?; const fn_got_addr = got_addr + got_index * ptr_bytes; _ = try self.addInst(.{ .tag = .call, .ops = (Mir.Ops{ .flags = 0b01, }).encode(), .data = .{ .imm = @bitCast(i32, @intCast(u32, fn_got_addr)) }, }); } else return self.fail("TODO implement calling extern fn on plan9", .{}); } else { return self.fail("TODO implement calling runtime known function pointer", .{}); } } else unreachable; const result: MCValue = result: { switch (info.return_value) { .register => |reg| { if (Register.allocIndex(reg) == null) { // Save function return value in a callee saved register break :result try self.copyToNewRegister(inst, info.return_value); } }, else => {}, } break :result info.return_value; }; if (args.len <= Liveness.bpi - 2) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); buf[0] = callee; std.mem.copy(Air.Inst.Ref, buf[1..], args); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, 1 + args.len); bt.feed(callee); for (args) |arg| { bt.feed(arg); } return bt.finishAir(result); } fn ret(self: *Self, mcv: MCValue) !void { const ret_ty = self.fn_type.fnReturnType(); try self.setRegOrMem(ret_ty, self.ret_mcv, mcv); // TODO when implementing defer, this will need to jump to the appropriate defer expression. // TODO optimization opportunity: figure out when we can emit this as a 2 byte instruction // which is available if the jump is 127 bytes or less forward. const jmp_reloc = try self.addInst(.{ .tag = .jmp, .ops = (Mir.Ops{ .flags = 0b00, }).encode(), .data = .{ .inst = undefined }, }); try self.exitlude_jump_relocs.append(self.gpa, jmp_reloc); } fn airRet(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const operand = try self.resolveInst(un_op); try self.ret(operand); return self.finishAir(inst, .dead, .{ un_op, .none, .none }); } fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const ptr = try self.resolveInst(un_op); // we can reuse self.ret_mcv because it just gets returned try self.load(self.ret_mcv, ptr, self.air.typeOf(un_op)); try self.ret(self.ret_mcv); return self.finishAir(inst, .dead, .{ un_op, .none, .none }); } fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ bin_op.lhs, bin_op.rhs, .none }); const ty = self.air.typeOf(bin_op.lhs); assert(ty.eql(self.air.typeOf(bin_op.rhs))); if (ty.zigTypeTag() == .ErrorSet) return self.fail("TODO implement cmp for errors", .{}); const lhs = try self.resolveInst(bin_op.lhs); const rhs = try self.resolveInst(bin_op.rhs); const result: MCValue = result: { // There are 2 operands, destination and source. // Either one, but not both, can be a memory operand. // Source operand can be an immediate, 8 bits or 32 bits. const dst_mcv = if (lhs.isImmediate() or (lhs.isMemory() and rhs.isMemory())) try self.copyToNewRegister(inst, lhs) else lhs; // This instruction supports only signed 32-bit immediates at most. const src_mcv = try self.limitImmediateType(bin_op.rhs, i32); try self.genBinMathOpMir(.cmp, ty, dst_mcv, src_mcv); break :result switch (ty.isSignedInt()) { true => MCValue{ .compare_flags_signed = op }, false => MCValue{ .compare_flags_unsigned = op }, }; }; return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; const payload = try self.addExtra(Mir.DbgLineColumn{ .line = dbg_stmt.line, .column = dbg_stmt.column, }); _ = try self.addInst(.{ .tag = .dbg_line, .ops = undefined, .data = .{ .payload = payload }, }); return self.finishAirBookkeeping(); } fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { const pl_op = self.air.instructions.items(.data)[inst].pl_op; const cond = try self.resolveInst(pl_op.operand); const extra = self.air.extraData(Air.CondBr, pl_op.payload); const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; const liveness_condbr = self.liveness.getCondBr(inst); const reloc: Mir.Inst.Index = reloc: { switch (cond) { .compare_flags_signed => |cmp_op| { // Here we map the opposites since the jump is to the false branch. const flags: u2 = switch (cmp_op) { .gte => 0b10, .gt => 0b11, .neq => 0b01, .lt => 0b00, .lte => 0b01, .eq => 0b00, }; const tag: Mir.Inst.Tag = if (cmp_op == .neq or cmp_op == .eq) .cond_jmp_eq_ne else .cond_jmp_greater_less; const reloc = try self.addInst(.{ .tag = tag, .ops = (Mir.Ops{ .flags = flags, }).encode(), .data = .{ .inst = undefined }, }); break :reloc reloc; }, .compare_flags_unsigned => |cmp_op| { // Here we map the opposites since the jump is to the false branch. const flags: u2 = switch (cmp_op) { .gte => 0b10, .gt => 0b11, .neq => 0b01, .lt => 0b00, .lte => 0b01, .eq => 0b00, }; const tag: Mir.Inst.Tag = if (cmp_op == .neq or cmp_op == .eq) .cond_jmp_eq_ne else .cond_jmp_above_below; const reloc = try self.addInst(.{ .tag = tag, .ops = (Mir.Ops{ .flags = flags, }).encode(), .data = .{ .inst = undefined }, }); break :reloc reloc; }, .register => |reg| { _ = try self.addInst(.{ .tag = .@"test", .ops = (Mir.Ops{ .reg1 = reg, .flags = 0b00, }).encode(), .data = .{ .imm = 1 }, }); const reloc = try self.addInst(.{ .tag = .cond_jmp_eq_ne, .ops = (Mir.Ops{ .flags = 0b01, }).encode(), .data = .{ .inst = undefined }, }); break :reloc reloc; }, else => return self.fail("TODO implement condbr {s} when condition is {s}", .{ self.target.cpu.arch, @tagName(cond), }), } }; // Capture the state of register and stack allocation state so that we can revert to it. const parent_next_stack_offset = self.next_stack_offset; const parent_free_registers = self.register_manager.free_registers; var parent_stack = try self.stack.clone(self.gpa); defer parent_stack.deinit(self.gpa); const parent_registers = self.register_manager.registers; try self.branch_stack.append(.{}); try self.ensureProcessDeathCapacity(liveness_condbr.then_deaths.len); for (liveness_condbr.then_deaths) |operand| { self.processDeath(operand); } try self.genBody(then_body); // Revert to the previous register and stack allocation state. var saved_then_branch = self.branch_stack.pop(); defer saved_then_branch.deinit(self.gpa); self.register_manager.registers = parent_registers; self.stack.deinit(self.gpa); self.stack = parent_stack; parent_stack = .{}; self.next_stack_offset = parent_next_stack_offset; self.register_manager.free_registers = parent_free_registers; try self.performReloc(reloc); const else_branch = self.branch_stack.addOneAssumeCapacity(); else_branch.* = .{}; try self.ensureProcessDeathCapacity(liveness_condbr.else_deaths.len); for (liveness_condbr.else_deaths) |operand| { self.processDeath(operand); } try self.genBody(else_body); // At this point, each branch will possibly have conflicting values for where // each instruction is stored. They agree, however, on which instructions are alive/dead. // We use the first ("then") branch as canonical, and here emit // instructions into the second ("else") branch to make it conform. // We continue respect the data structure semantic guarantees of the else_branch so // that we can use all the code emitting abstractions. This is why at the bottom we // assert that parent_branch.free_registers equals the saved_then_branch.free_registers // rather than assigning it. const parent_branch = &self.branch_stack.items[self.branch_stack.items.len - 2]; try parent_branch.inst_table.ensureUnusedCapacity(self.gpa, else_branch.inst_table.count()); const else_slice = else_branch.inst_table.entries.slice(); const else_keys = else_slice.items(.key); const else_values = else_slice.items(.value); for (else_keys) |else_key, else_idx| { const else_value = else_values[else_idx]; const canon_mcv = if (saved_then_branch.inst_table.fetchSwapRemove(else_key)) |then_entry| blk: { // The instruction's MCValue is overridden in both branches. parent_branch.inst_table.putAssumeCapacity(else_key, then_entry.value); if (else_value == .dead) { assert(then_entry.value == .dead); continue; } break :blk then_entry.value; } else blk: { if (else_value == .dead) continue; // The instruction is only overridden in the else branch. var i: usize = self.branch_stack.items.len - 2; while (true) { i -= 1; // If this overflows, the question is: why wasn't the instruction marked dead? if (self.branch_stack.items[i].inst_table.get(else_key)) |mcv| { assert(mcv != .dead); break :blk mcv; } } }; log.debug("consolidating else_entry {d} {}=>{}", .{ else_key, else_value, canon_mcv }); // TODO make sure the destination stack offset / register does not already have something // going on there. try self.setRegOrMem(self.air.typeOfIndex(else_key), canon_mcv, else_value); // TODO track the new register / stack allocation } try parent_branch.inst_table.ensureUnusedCapacity(self.gpa, saved_then_branch.inst_table.count()); const then_slice = saved_then_branch.inst_table.entries.slice(); const then_keys = then_slice.items(.key); const then_values = then_slice.items(.value); for (then_keys) |then_key, then_idx| { const then_value = then_values[then_idx]; // We already deleted the items from this table that matched the else_branch. // So these are all instructions that are only overridden in the then branch. parent_branch.inst_table.putAssumeCapacity(then_key, then_value); if (then_value == .dead) continue; const parent_mcv = blk: { var i: usize = self.branch_stack.items.len - 2; while (true) { i -= 1; if (self.branch_stack.items[i].inst_table.get(then_key)) |mcv| { assert(mcv != .dead); break :blk mcv; } } }; log.debug("consolidating then_entry {d} {}=>{}", .{ then_key, parent_mcv, then_value }); // TODO make sure the destination stack offset / register does not already have something // going on there. try self.setRegOrMem(self.air.typeOfIndex(then_key), parent_mcv, then_value); // TODO track the new register / stack allocation } self.branch_stack.pop().deinit(self.gpa); return self.finishAir(inst, .unreach, .{ pl_op.operand, .none, .none }); } fn isNull(self: *Self, ty: Type, operand: MCValue) !MCValue { try self.genBinMathOpMir(.cmp, ty, operand, MCValue{ .immediate = 0 }); return MCValue{ .compare_flags_unsigned = .eq }; } fn isNonNull(self: *Self, ty: Type, operand: MCValue) !MCValue { const is_null_res = try self.isNull(ty, operand); assert(is_null_res.compare_flags_unsigned == .eq); return MCValue{ .compare_flags_unsigned = .neq }; } fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue { _ = ty; _ = operand; // Here you can specialize this instruction if it makes sense to, otherwise the default // will call isNonErr and invert the result. return self.fail("TODO call isNonErr and invert the result", .{}); } fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue { _ = ty; _ = operand; // Here you can specialize this instruction if it makes sense to, otherwise the default // will call isErr and invert the result. return self.fail("TODO call isErr and invert the result", .{}); } fn airIsNull(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand = try self.resolveInst(un_op); const ty = self.air.typeOf(un_op); break :result try self.isNull(ty, operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand_ptr = try self.resolveInst(un_op); const operand: MCValue = blk: { if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { // The MCValue that holds the pointer can be re-used as the value. break :blk operand_ptr; } else { break :blk try self.allocRegOrMem(inst, true); } }; const ptr_ty = self.air.typeOf(un_op); try self.load(operand, operand_ptr, ptr_ty); break :result try self.isNull(ptr_ty.elemType(), operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand = try self.resolveInst(un_op); const ty = self.air.typeOf(un_op); break :result try self.isNonNull(ty, operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand_ptr = try self.resolveInst(un_op); const operand: MCValue = blk: { if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { // The MCValue that holds the pointer can be re-used as the value. break :blk operand_ptr; } else { break :blk try self.allocRegOrMem(inst, true); } }; const ptr_ty = self.air.typeOf(un_op); try self.load(operand, operand_ptr, ptr_ty); break :result try self.isNonNull(ptr_ty.elemType(), operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand = try self.resolveInst(un_op); const ty = self.air.typeOf(un_op); break :result try self.isErr(ty, operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand_ptr = try self.resolveInst(un_op); const operand: MCValue = blk: { if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { // The MCValue that holds the pointer can be re-used as the value. break :blk operand_ptr; } else { break :blk try self.allocRegOrMem(inst, true); } }; const ptr_ty = self.air.typeOf(un_op); try self.load(operand, operand_ptr, ptr_ty); break :result try self.isErr(ptr_ty.elemType(), operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand = try self.resolveInst(un_op); const ty = self.air.typeOf(un_op); break :result try self.isNonErr(ty, operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { const operand_ptr = try self.resolveInst(un_op); const operand: MCValue = blk: { if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { // The MCValue that holds the pointer can be re-used as the value. break :blk operand_ptr; } else { break :blk try self.allocRegOrMem(inst, true); } }; const ptr_ty = self.air.typeOf(un_op); try self.load(operand, operand_ptr, ptr_ty); break :result try self.isNonErr(ptr_ty.elemType(), operand); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airLoop(self: *Self, inst: Air.Inst.Index) !void { // A loop is a setup to be able to jump back to the beginning. const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const loop = self.air.extraData(Air.Block, ty_pl.payload); const body = self.air.extra[loop.end..][0..loop.data.body_len]; const jmp_target = @intCast(u32, self.mir_instructions.len); try self.genBody(body); _ = try self.addInst(.{ .tag = .jmp, .ops = (Mir.Ops{ .flags = 0b00, }).encode(), .data = .{ .inst = jmp_target }, }); return self.finishAirBookkeeping(); } fn airBlock(self: *Self, inst: Air.Inst.Index) !void { try self.blocks.putNoClobber(self.gpa, inst, .{ // A block is a setup to be able to jump to the end. .relocs = .{}, // It also acts as a receptacle for break operands. // Here we use `MCValue.none` to represent a null value so that the first // break instruction will choose a MCValue for the block result and overwrite // this field. Following break instructions will use that MCValue to put their // block results. .mcv = MCValue{ .none = {} }, }); const block_data = self.blocks.getPtr(inst).?; defer block_data.relocs.deinit(self.gpa); const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const extra = self.air.extraData(Air.Block, ty_pl.payload); const body = self.air.extra[extra.end..][0..extra.data.body_len]; try self.genBody(body); for (block_data.relocs.items) |reloc| try self.performReloc(reloc); const result = @bitCast(MCValue, block_data.mcv); return self.finishAir(inst, result, .{ .none, .none, .none }); } fn airSwitch(self: *Self, inst: Air.Inst.Index) !void { const pl_op = self.air.instructions.items(.data)[inst].pl_op; const condition = pl_op.operand; _ = condition; return self.fail("TODO airSwitch for {}", .{self.target.cpu.arch}); // return self.finishAir(inst, .dead, .{ condition, .none, .none }); } fn performReloc(self: *Self, reloc: Mir.Inst.Index) !void { const next_inst = @intCast(u32, self.mir_instructions.len); self.mir_instructions.items(.data)[reloc].inst = next_inst; } fn airBr(self: *Self, inst: Air.Inst.Index) !void { const branch = self.air.instructions.items(.data)[inst].br; try self.br(branch.block_inst, branch.operand); return self.finishAir(inst, .dead, .{ branch.operand, .none, .none }); } fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const air_tags = self.air.instructions.items(.tag); const result: MCValue = if (self.liveness.isUnused(inst)) .dead else switch (air_tags[inst]) { // lhs AND rhs .bool_and => try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs), // lhs OR rhs .bool_or => try self.genBinMathOp(inst, bin_op.lhs, bin_op.rhs), else => unreachable, // Not a boolean operation }; return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); } fn br(self: *Self, block: Air.Inst.Index, operand: Air.Inst.Ref) !void { const block_data = self.blocks.getPtr(block).?; if (self.air.typeOf(operand).hasCodeGenBits()) { const operand_mcv = try self.resolveInst(operand); const block_mcv = block_data.mcv; if (block_mcv == .none) { block_data.mcv = operand_mcv; } else { try self.setRegOrMem(self.air.typeOfIndex(block), block_mcv, operand_mcv); } } return self.brVoid(block); } fn brVoid(self: *Self, block: Air.Inst.Index) !void { const block_data = self.blocks.getPtr(block).?; // Emit a jump with a relocation. It will be patched up after the block ends. try block_data.relocs.ensureUnusedCapacity(self.gpa, 1); // Leave the jump offset undefined const jmp_reloc = try self.addInst(.{ .tag = .jmp, .ops = (Mir.Ops{ .flags = 0b00, }).encode(), .data = .{ .inst = undefined }, }); block_data.relocs.appendAssumeCapacity(jmp_reloc); } fn airAsm(self: *Self, inst: Air.Inst.Index) !void { const air_datas = self.air.instructions.items(.data); const air_extra = self.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload); const zir = self.mod_fn.owner_decl.getFileScope().zir; const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended; const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand); const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source); const outputs_len = @truncate(u5, extended.small); const args_len = @truncate(u5, extended.small >> 5); const clobbers_len = @truncate(u5, extended.small >> 10); _ = clobbers_len; // TODO honor these const is_volatile = @truncate(u1, extended.small >> 15) != 0; const args = @bitCast([]const Air.Inst.Ref, self.air.extra[air_extra.end..][0..args_len]); if (outputs_len > 1) { return self.fail("TODO implement codegen for asm with more than 1 output", .{}); } var extra_i: usize = zir_extra.end; const output_constraint: ?[]const u8 = out: { var i: usize = 0; while (i < outputs_len) : (i += 1) { const output = zir.extraData(Zir.Inst.Asm.Output, extra_i); extra_i = output.end; break :out zir.nullTerminatedString(output.data.constraint); } break :out null; }; const dead = !is_volatile and self.liveness.isUnused(inst); const result: MCValue = if (dead) .dead else result: { for (args) |arg| { const input = zir.extraData(Zir.Inst.Asm.Input, extra_i); extra_i = input.end; const constraint = zir.nullTerminatedString(input.data.constraint); if (constraint.len < 3 or constraint[0] != '{' or constraint[constraint.len - 1] != '}') { return self.fail("unrecognized asm input constraint: '{s}'", .{constraint}); } const reg_name = constraint[1 .. constraint.len - 1]; const reg = parseRegName(reg_name) orelse return self.fail("unrecognized register: '{s}'", .{reg_name}); const arg_mcv = try self.resolveInst(arg); try self.register_manager.getReg(reg, null); try self.genSetReg(self.air.typeOf(arg), reg, arg_mcv); } { var iter = std.mem.tokenize(u8, asm_source, "\n\r"); while (iter.next()) |ins| { if (mem.eql(u8, ins, "syscall")) { _ = try self.addInst(.{ .tag = .syscall, .ops = undefined, .data = undefined, }); } else if (mem.indexOf(u8, ins, "push")) |_| { const arg = ins[4..]; if (mem.indexOf(u8, arg, "$")) |l| { const n = std.fmt.parseInt(u8, ins[4 + l + 1 ..], 10) catch { return self.fail("TODO implement more inline asm int parsing", .{}); }; _ = try self.addInst(.{ .tag = .push, .ops = (Mir.Ops{ .flags = 0b10, }).encode(), .data = .{ .imm = n }, }); } else if (mem.indexOf(u8, arg, "%%")) |l| { const reg_name = ins[4 + l + 2 ..]; const reg = parseRegName(reg_name) orelse return self.fail("unrecognized register: '{s}'", .{reg_name}); _ = try self.addInst(.{ .tag = .push, .ops = (Mir.Ops{ .reg1 = reg, }).encode(), .data = undefined, }); } else return self.fail("TODO more push operands", .{}); } else if (mem.indexOf(u8, ins, "pop")) |_| { const arg = ins[3..]; if (mem.indexOf(u8, arg, "%%")) |l| { const reg_name = ins[3 + l + 2 ..]; const reg = parseRegName(reg_name) orelse return self.fail("unrecognized register: '{s}'", .{reg_name}); _ = try self.addInst(.{ .tag = .pop, .ops = (Mir.Ops{ .reg1 = reg, }).encode(), .data = undefined, }); } else return self.fail("TODO more pop operands", .{}); } else { return self.fail("TODO implement support for more x86 assembly instructions", .{}); } } } if (output_constraint) |output| { if (output.len < 4 or output[0] != '=' or output[1] != '{' or output[output.len - 1] != '}') { return self.fail("unrecognized asm output constraint: '{s}'", .{output}); } const reg_name = output[2 .. output.len - 1]; const reg = parseRegName(reg_name) orelse return self.fail("unrecognized register: '{s}'", .{reg_name}); break :result MCValue{ .register = reg }; } else { break :result MCValue{ .none = {} }; } }; if (args.len <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); std.mem.copy(Air.Inst.Ref, &buf, args); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, args.len); for (args) |arg| { bt.feed(arg); } return bt.finishAir(result); } fn iterateBigTomb(self: *Self, inst: Air.Inst.Index, operand_count: usize) !BigTomb { try self.ensureProcessDeathCapacity(operand_count + 1); return BigTomb{ .function = self, .inst = inst, .tomb_bits = self.liveness.getTombBits(inst), .big_tomb_bits = self.liveness.special.get(inst) orelse 0, .bit_index = 0, }; } /// Sets the value without any modifications to register allocation metadata or stack allocation metadata. fn setRegOrMem(self: *Self, ty: Type, loc: MCValue, val: MCValue) !void { switch (loc) { .none => return, .register => |reg| return self.genSetReg(ty, reg, val), .stack_offset => |off| return self.genSetStack(ty, off, val), .memory => { return self.fail("TODO implement setRegOrMem for memory", .{}); }, else => unreachable, } } fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void { switch (mcv) { .dead => unreachable, .ptr_stack_offset => unreachable, .ptr_embedded_in_code => unreachable, .unreach, .none => return, // Nothing to do. .undef => { if (!self.wantSafety()) return; // The already existing value will do just fine. // TODO Upgrade this to a memset call when we have that available. switch (ty.abiSize(self.target.*)) { 1 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaa }), 2 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaa }), 4 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaaaaaa }), 8 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaaaaaaaaaaaaaa }), else => return self.fail("TODO implement memset", .{}), } }, .compare_flags_unsigned => |op| { _ = op; return self.fail("TODO implement set stack variable with compare flags value (unsigned)", .{}); }, .compare_flags_signed => |op| { _ = op; return self.fail("TODO implement set stack variable with compare flags value (signed)", .{}); }, .immediate => |x_big| { const abi_size = ty.abiSize(self.target.*); const adj_off = stack_offset + abi_size; if (adj_off > 128) { return self.fail("TODO implement set stack variable with large stack offset", .{}); } switch (abi_size) { 1, 2, 4 => { // We have a positive stack offset value but we want a twos complement negative // offset from rbp, which is at the top of the stack frame. // mov [rbp+offset], immediate const payload = try self.addExtra(Mir.ImmPair{ .dest_off = -@intCast(i32, adj_off), .operand = @bitCast(i32, @intCast(u32, x_big)), }); _ = try self.addInst(.{ .tag = .mov_mem_imm, .ops = (Mir.Ops{ .reg1 = .rbp, .flags = switch (abi_size) { 1 => 0b00, 2 => 0b01, 4 => 0b10, else => unreachable, }, }).encode(), .data = .{ .payload = payload }, }); }, 8 => { // We have a positive stack offset value but we want a twos complement negative // offset from rbp, which is at the top of the stack frame. const negative_offset = -@intCast(i32, adj_off); // 64 bit write to memory would take two mov's anyways so we // insted just use two 32 bit writes to avoid register allocation { const payload = try self.addExtra(Mir.ImmPair{ .dest_off = negative_offset + 4, .operand = @bitCast(i32, @truncate(u32, x_big >> 32)), }); _ = try self.addInst(.{ .tag = .mov_mem_imm, .ops = (Mir.Ops{ .reg1 = .rbp, .flags = 0b10, }).encode(), .data = .{ .payload = payload }, }); } { const payload = try self.addExtra(Mir.ImmPair{ .dest_off = negative_offset, .operand = @bitCast(i32, @truncate(u32, x_big)), }); _ = try self.addInst(.{ .tag = .mov_mem_imm, .ops = (Mir.Ops{ .reg1 = .rbp, .flags = 0b10, }).encode(), .data = .{ .payload = payload }, }); } }, else => { return self.fail("TODO implement set abi_size=large stack variable with immediate", .{}); }, } }, .embedded_in_code => { // TODO this and `.stack_offset` below need to get improved to support types greater than // register size, and do general memcpy const reg = try self.copyToTmpRegister(ty, mcv); return self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); }, .register => |reg| { if (stack_offset > math.maxInt(i32)) { return self.fail("stack offset too large", .{}); } const abi_size = ty.abiSize(self.target.*); const adj_off = stack_offset + abi_size; _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = .rbp, .reg2 = registerAlias(reg, @intCast(u32, abi_size)), .flags = 0b10, }).encode(), .data = .{ .imm = -@intCast(i32, adj_off) }, }); }, .memory => |vaddr| { _ = vaddr; return self.fail("TODO implement set stack variable from memory vaddr", .{}); }, .stack_offset => |off| { // TODO this and `.embedded_in_code` above need to get improved to support types greater than // register size, and do general memcpy if (stack_offset == off) return; // Copy stack variable to itself; nothing to do. const reg = try self.copyToTmpRegister(ty, mcv); return self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); }, } } /// Set pointee via pointer stored in a register. /// mov [reg], value fn genSetPtrReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void { switch (mcv) { .dead => unreachable, .unreach, .none => return, // Nothing to do. .immediate => |imm| { const abi_size = ty.abiSize(self.target.*); switch (abi_size) { 1, 2, 4 => { // TODO this is wasteful! // introduce new MIR tag specifically for mov [reg + 0], imm const payload = try self.addExtra(Mir.ImmPair{ .dest_off = 0, .operand = @bitCast(i32, @intCast(u32, imm)), }); _ = try self.addInst(.{ .tag = .mov_mem_imm, .ops = (Mir.Ops{ .reg1 = reg.to64(), .flags = switch (abi_size) { 1 => 0b00, 2 => 0b01, 4 => 0b10, else => unreachable, }, }).encode(), .data = .{ .payload = payload }, }); }, else => { return self.fail("TODO implement set pointee with immediate of ABI size {d}", .{abi_size}); }, } }, else => |other| { return self.fail("TODO implement set pointee with {}", .{other}); }, } } fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void { switch (mcv) { .dead => unreachable, .ptr_stack_offset => |unadjusted_off| { const ptr_abi_size = ty.abiSize(self.target.*); const elem_ty = ty.childType(); const elem_abi_size = elem_ty.abiSize(self.target.*); const off = unadjusted_off + elem_abi_size; if (off < std.math.minInt(i32) or off > std.math.maxInt(i32)) { return self.fail("stack offset too large", .{}); } _ = try self.addInst(.{ .tag = .lea, .ops = (Mir.Ops{ .reg1 = registerAlias(reg, @intCast(u32, ptr_abi_size)), .reg2 = .rbp, }).encode(), .data = .{ .imm = -@intCast(i32, off) }, }); }, .ptr_embedded_in_code => unreachable, .unreach, .none => return, // Nothing to do. .undef => { if (!self.wantSafety()) return; // The already existing value will do just fine. // Write the debug undefined value. switch (reg.size()) { 8 => return self.genSetReg(ty, reg, .{ .immediate = 0xaa }), 16 => return self.genSetReg(ty, reg, .{ .immediate = 0xaaaa }), 32 => return self.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaa }), 64 => return self.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa }), else => unreachable, } }, .compare_flags_unsigned => |op| { const tag: Mir.Inst.Tag = switch (op) { .gte, .gt, .lt, .lte => .cond_set_byte_above_below, .eq, .neq => .cond_set_byte_eq_ne, }; const flags: u2 = switch (op) { .gte => 0b00, .gt => 0b01, .lt => 0b10, .lte => 0b11, .eq => 0b01, .neq => 0b00, }; _ = try self.addInst(.{ .tag = tag, .ops = (Mir.Ops{ .reg1 = reg.to8(), .flags = flags, }).encode(), .data = undefined, }); }, .compare_flags_signed => |op| { _ = op; return self.fail("TODO set register with compare flags value (signed)", .{}); }, .immediate => |x| { // 32-bit moves zero-extend to 64-bit, so xoring the 32-bit // register is the fastest way to zero a register. if (x == 0) { _ = try self.addInst(.{ .tag = .xor, .ops = (Mir.Ops{ .reg1 = reg, .reg2 = reg, }).encode(), .data = undefined, }); return; } if (x <= math.maxInt(i32)) { const abi_size = ty.abiSize(self.target.*); // Next best case: if we set the lower four bytes, the upper four will be zeroed. _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = registerAlias(reg, @intCast(u32, abi_size)), }).encode(), .data = .{ .imm = @intCast(i32, x) }, }); return; } // Worst case: we need to load the 64-bit register with the IMM. GNU's assemblers calls // this `movabs`, though this is officially just a different variant of the plain `mov` // instruction. // // This encoding is, in fact, the *same* as the one used for 32-bit loads. The only // difference is that we set REX.W before the instruction, which extends the load to // 64-bit and uses the full bit-width of the register. const payload = try self.addExtra(Mir.Imm64.encode(x)); _ = try self.addInst(.{ .tag = .movabs, .ops = (Mir.Ops{ .reg1 = reg, }).encode(), .data = .{ .payload = payload }, }); }, .embedded_in_code => |code_offset| { // We need the offset from RIP in a signed i32 twos complement. const payload = try self.addExtra(Mir.Imm64.encode(code_offset)); _ = try self.addInst(.{ .tag = .lea, .ops = (Mir.Ops{ .reg1 = reg, .flags = 0b01, }).encode(), .data = .{ .payload = payload }, }); }, .register => |src_reg| { // If the registers are the same, nothing to do. if (src_reg.id() == reg.id()) return; _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = registerAlias(reg, @divExact(src_reg.size(), 8)), .reg2 = src_reg, }).encode(), .data = undefined, }); }, .memory => |x| { // TODO can we move this entire logic into Emit.zig like with aarch64? if (self.bin_file.options.pie) { // TODO we should flag up `x` as GOT symbol entry explicitly rather than as a hack. _ = try self.addInst(.{ .tag = .lea, .ops = (Mir.Ops{ .reg1 = reg, .flags = 0b10, }).encode(), .data = .{ .got_entry = @intCast(u32, x) }, }); // MOV reg, [reg] _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = reg, .reg2 = reg, .flags = 0b01, }).encode(), .data = .{ .imm = 0 }, }); } else if (x <= math.maxInt(i32)) { // mov reg, [ds:imm32] _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = reg, .flags = 0b01, }).encode(), .data = .{ .imm = @intCast(i32, x) }, }); } else { // If this is RAX, we can use a direct load. // Otherwise, we need to load the address, then indirectly load the value. if (reg.id() == 0) { // movabs rax, ds:moffs64 const payload = try self.addExtra(Mir.Imm64.encode(x)); _ = try self.addInst(.{ .tag = .movabs, .ops = (Mir.Ops{ .reg1 = .rax, .flags = 0b01, // imm64 will become moffs64 }).encode(), .data = .{ .payload = payload }, }); } else { // This requires two instructions; a move imm as used above, followed by an indirect load using the register // as the address and the register as the destination. // // This cannot be used if the lower three bits of the id are equal to four or five, as there // is no way to possibly encode it. This means that RSP, RBP, R12, and R13 cannot be used with // this instruction. const id3 = @truncate(u3, reg.id()); assert(id3 != 4 and id3 != 5); // Rather than duplicate the logic used for the move, we just use a self-call with a new MCValue. try self.genSetReg(ty, reg, MCValue{ .immediate = x }); // Now, the register contains the address of the value to load into it // Currently, we're only allowing 64-bit registers, so we need the `REX.W 8B /r` variant. // TODO: determine whether to allow other sized registers, and if so, handle them properly. // mov reg, [reg + 0x0] _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = reg, .reg2 = reg, .flags = 0b01, }).encode(), .data = .{ .imm = 0 }, }); } } }, .stack_offset => |unadjusted_off| { const abi_size = ty.abiSize(self.target.*); const off = unadjusted_off + abi_size; if (off < std.math.minInt(i32) or off > std.math.maxInt(i32)) { return self.fail("stack offset too large", .{}); } _ = try self.addInst(.{ .tag = .mov, .ops = (Mir.Ops{ .reg1 = registerAlias(reg, @intCast(u32, abi_size)), .reg2 = .rbp, .flags = 0b01, }).encode(), .data = .{ .imm = -@intCast(i32, off) }, }); }, } } fn airPtrToInt(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const result = try self.resolveInst(un_op); return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result = try self.resolveInst(ty_op.operand); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airArrayToSlice for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airIntToFloat(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airIntToFloat for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airFloatToInt(self: *Self, inst: Air.Inst.Index) !void { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement airFloatToInt for {}", .{self.target.cpu.arch}); return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } fn airCmpxchg(self: *Self, inst: Air.Inst.Index) !void { const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; const extra = self.air.extraData(Air.Block, ty_pl.payload); _ = ty_pl; _ = extra; return self.fail("TODO implement airCmpxchg for {}", .{self.target.cpu.arch}); // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); } fn airAtomicRmw(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airCmpxchg for {}", .{self.target.cpu.arch}); } fn airAtomicLoad(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airAtomicLoad for {}", .{self.target.cpu.arch}); } fn airAtomicStore(self: *Self, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void { _ = inst; _ = order; return self.fail("TODO implement airAtomicStore for {}", .{self.target.cpu.arch}); } fn airMemset(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airMemset for {}", .{self.target.cpu.arch}); } fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void { _ = inst; return self.fail("TODO implement airMemcpy for {}", .{self.target.cpu.arch}); } fn airTagName(self: *Self, inst: Air.Inst.Index) !void { const un_op = self.air.instructions.items(.data)[inst].un_op; const operand = try self.resolveInst(un_op); const result: MCValue = if (self.liveness.isUnused(inst)) .dead else { _ = operand; return self.fail("TODO implement airTagName for x86_64", .{}); }; return self.finishAir(inst, result, .{ un_op, .none, .none }); } fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { // First section of indexes correspond to a set number of constant values. const ref_int = @enumToInt(inst); if (ref_int < Air.Inst.Ref.typed_value_map.len) { const tv = Air.Inst.Ref.typed_value_map[ref_int]; if (!tv.ty.hasCodeGenBits()) { return MCValue{ .none = {} }; } return self.genTypedValue(tv); } // If the type has no codegen bits, no need to store it. const inst_ty = self.air.typeOf(inst); if (!inst_ty.hasCodeGenBits()) return MCValue{ .none = {} }; const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len); switch (self.air.instructions.items(.tag)[inst_index]) { .constant => { // Constants have static lifetimes, so they are always memoized in the outer most table. const branch = &self.branch_stack.items[0]; const gop = try branch.inst_table.getOrPut(self.gpa, inst_index); if (!gop.found_existing) { const ty_pl = self.air.instructions.items(.data)[inst_index].ty_pl; gop.value_ptr.* = try self.genTypedValue(.{ .ty = inst_ty, .val = self.air.values[ty_pl.payload], }); } return gop.value_ptr.*; }, .const_ty => unreachable, else => return self.getResolvedInstValue(inst_index), } } fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue { // Treat each stack item as a "layer" on top of the previous one. var i: usize = self.branch_stack.items.len; while (true) { i -= 1; if (self.branch_stack.items[i].inst_table.get(inst)) |mcv| { assert(mcv != .dead); return mcv; } } } /// If the MCValue is an immediate, and it does not fit within this type, /// we put it in a register. /// A potential opportunity for future optimization here would be keeping track /// of the fact that the instruction is available both as an immediate /// and as a register. fn limitImmediateType(self: *Self, operand: Air.Inst.Ref, comptime T: type) !MCValue { const mcv = try self.resolveInst(operand); const ti = @typeInfo(T).Int; switch (mcv) { .immediate => |imm| { // This immediate is unsigned. const U = std.meta.Int(.unsigned, ti.bits - @boolToInt(ti.signedness == .signed)); if (imm >= math.maxInt(U)) { return MCValue{ .register = try self.copyToTmpRegister(Type.initTag(.usize), mcv) }; } }, else => {}, } return mcv; } fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { if (typed_value.val.isUndef()) return MCValue{ .undef = {} }; const ptr_bits = self.target.cpu.arch.ptrBitWidth(); const ptr_bytes: u64 = @divExact(ptr_bits, 8); switch (typed_value.ty.zigTypeTag()) { .Pointer => switch (typed_value.ty.ptrSize()) { .Slice => { var buf: Type.SlicePtrFieldTypeBuffer = undefined; const ptr_type = typed_value.ty.slicePtrFieldType(&buf); const ptr_mcv = try self.genTypedValue(.{ .ty = ptr_type, .val = typed_value.val }); const slice_len = typed_value.val.sliceLen(); // Codegen can't handle some kinds of indirection. If the wrong union field is accessed here it may mean // the Sema code needs to use anonymous Decls or alloca instructions to store data. const ptr_imm = ptr_mcv.memory; _ = slice_len; _ = ptr_imm; // We need more general support for const data being stored in memory to make this work. return self.fail("TODO codegen for const slices", .{}); }, else => { if (typed_value.val.castTag(.decl_ref)) |payload| { const decl = payload.data; decl.alive = true; if (self.bin_file.cast(link.File.Elf)) |elf_file| { const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?]; const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes; return MCValue{ .memory = got_addr }; } else if (self.bin_file.cast(link.File.MachO)) |_| { // TODO I'm hacking my way through here by repurposing .memory for storing // index to the GOT target symbol index. return MCValue{ .memory = decl.link.macho.local_sym_index }; } else if (self.bin_file.cast(link.File.Coff)) |coff_file| { const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes; return MCValue{ .memory = got_addr }; } else if (self.bin_file.cast(link.File.Plan9)) |p9| { try p9.seeDecl(decl); const got_addr = p9.bases.data + decl.link.plan9.got_index.? * ptr_bytes; return MCValue{ .memory = got_addr }; } else { return self.fail("TODO codegen non-ELF const Decl pointer", .{}); } } if (typed_value.val.tag() == .int_u64) { return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; } return self.fail("TODO codegen more kinds of const pointers", .{}); }, }, .Int => { const info = typed_value.ty.intInfo(self.target.*); if (info.bits <= ptr_bits and info.signedness == .signed) { return MCValue{ .immediate = @bitCast(u64, typed_value.val.toSignedInt()) }; } if (info.bits > ptr_bits or info.signedness == .signed) { return self.fail("TODO const int bigger than ptr and signed int", .{}); } return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; }, .Bool => { return MCValue{ .immediate = @boolToInt(typed_value.val.toBool()) }; }, .ComptimeInt => unreachable, // semantic analysis prevents this .ComptimeFloat => unreachable, // semantic analysis prevents this .Optional => { if (typed_value.ty.isPtrLikeOptional()) { if (typed_value.val.isNull()) return MCValue{ .immediate = 0 }; var buf: Type.Payload.ElemType = undefined; return self.genTypedValue(.{ .ty = typed_value.ty.optionalChild(&buf), .val = typed_value.val, }); } else if (typed_value.ty.abiSize(self.target.*) == 1) { return MCValue{ .immediate = @boolToInt(typed_value.val.isNull()) }; } return self.fail("TODO non pointer optionals", .{}); }, .Enum => { if (typed_value.val.castTag(.enum_field_index)) |field_index| { switch (typed_value.ty.tag()) { .enum_simple => { return MCValue{ .immediate = field_index.data }; }, .enum_full, .enum_nonexhaustive => { const enum_full = typed_value.ty.cast(Type.Payload.EnumFull).?.data; if (enum_full.values.count() != 0) { const tag_val = enum_full.values.keys()[field_index.data]; return self.genTypedValue(.{ .ty = enum_full.tag_ty, .val = tag_val }); } else { return MCValue{ .immediate = field_index.data }; } }, else => unreachable, } } else { var int_tag_buffer: Type.Payload.Bits = undefined; const int_tag_ty = typed_value.ty.intTagType(&int_tag_buffer); return self.genTypedValue(.{ .ty = int_tag_ty, .val = typed_value.val }); } }, .ErrorSet => { switch (typed_value.val.tag()) { .@"error" => { const err_name = typed_value.val.castTag(.@"error").?.data.name; const module = self.bin_file.options.module.?; const global_error_set = module.global_error_set; const error_index = global_error_set.get(err_name).?; return MCValue{ .immediate = error_index }; }, else => { // In this case we are rendering an error union which has a 0 bits payload. return MCValue{ .immediate = 0 }; }, } }, .ErrorUnion => { const error_type = typed_value.ty.errorUnionSet(); const payload_type = typed_value.ty.errorUnionPayload(); const sub_val = typed_value.val.castTag(.eu_payload).?.data; if (!payload_type.hasCodeGenBits()) { // We use the error type directly as the type. return self.genTypedValue(.{ .ty = error_type, .val = sub_val }); } return self.fail("TODO implement error union const of type '{}'", .{typed_value.ty}); }, else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty}), } } const CallMCValues = struct { args: []MCValue, return_value: MCValue, stack_byte_count: u32, stack_align: u32, fn deinit(self: *CallMCValues, func: *Self) void { func.gpa.free(self.args); self.* = undefined; } }; /// Caller must call `CallMCValues.deinit`. fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { const cc = fn_ty.fnCallingConvention(); const param_types = try self.gpa.alloc(Type, fn_ty.fnParamLen()); defer self.gpa.free(param_types); fn_ty.fnParamTypes(param_types); var result: CallMCValues = .{ .args = try self.gpa.alloc(MCValue, param_types.len), // These undefined values must be populated before returning from this function. .return_value = undefined, .stack_byte_count = undefined, .stack_align = undefined, }; errdefer self.gpa.free(result.args); const ret_ty = fn_ty.fnReturnType(); switch (cc) { .Naked => { assert(result.args.len == 0); result.return_value = .{ .unreach = {} }; result.stack_byte_count = 0; result.stack_align = 1; return result; }, .Unspecified, .C => { var next_int_reg: usize = 0; var next_stack_offset: u32 = 0; for (param_types) |ty, i| { if (!ty.hasCodeGenBits()) { assert(cc != .C); result.args[i] = .{ .none = {} }; continue; } const param_size = @intCast(u32, ty.abiSize(self.target.*)); const pass_in_reg = switch (ty.zigTypeTag()) { .Bool => true, .Int, .Enum => param_size <= 8, .Pointer => ty.ptrSize() != .Slice, .Optional => ty.isPtrLikeOptional(), else => false, }; if (pass_in_reg) { if (next_int_reg >= c_abi_int_param_regs.len) { result.args[i] = .{ .stack_offset = next_stack_offset }; next_stack_offset += param_size; } else { const aliased_reg = registerAlias( c_abi_int_param_regs[next_int_reg], param_size, ); result.args[i] = .{ .register = aliased_reg }; next_int_reg += 1; } } else { // For simplicity of codegen, slices and other types are always pushed onto the stack. // TODO: look into optimizing this by passing things as registers sometimes, // such as ptr and len of slices as separate registers. // TODO: also we need to honor the C ABI for relevant types rather than passing on // the stack here. result.args[i] = .{ .stack_offset = next_stack_offset }; next_stack_offset += param_size; } } result.stack_byte_count = next_stack_offset; result.stack_align = 16; }, else => return self.fail("TODO implement function parameters for {} on x86_64", .{cc}), } if (ret_ty.zigTypeTag() == .NoReturn) { result.return_value = .{ .unreach = {} }; } else if (!ret_ty.hasCodeGenBits()) { result.return_value = .{ .none = {} }; } else switch (cc) { .Naked => unreachable, .Unspecified, .C => { const ret_ty_size = @intCast(u32, ret_ty.abiSize(self.target.*)); const aliased_reg = registerAlias(c_abi_int_return_regs[0], ret_ty_size); result.return_value = .{ .register = aliased_reg }; }, else => return self.fail("TODO implement function return values for {}", .{cc}), } return result; } /// TODO support scope overrides. Also note this logic is duplicated with `Module.wantSafety`. fn wantSafety(self: *Self) bool { return switch (self.bin_file.options.optimize_mode) { .Debug => true, .ReleaseSafe => true, .ReleaseFast => false, .ReleaseSmall => false, }; } fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { @setCold(true); assert(self.err_msg == null); self.err_msg = try ErrorMsg.create(self.bin_file.allocator, self.src_loc, format, args); return error.CodegenFail; } fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { @setCold(true); assert(self.err_msg == null); self.err_msg = try ErrorMsg.create(self.bin_file.allocator, self.src_loc, format, args); return error.CodegenFail; } const Register = @import("bits.zig").Register; const Instruction = void; const Condition = void; const callee_preserved_regs = @import("bits.zig").callee_preserved_regs; const c_abi_int_param_regs = @import("bits.zig").c_abi_int_param_regs; const c_abi_int_return_regs = @import("bits.zig").c_abi_int_return_regs; fn parseRegName(name: []const u8) ?Register { if (@hasDecl(Register, "parseRegName")) { return Register.parseRegName(name); } return std.meta.stringToEnum(Register, name); } fn registerAlias(reg: Register, size_bytes: u32) Register { // For x86_64 we have to pick a smaller register alias depending on abi size. switch (size_bytes) { 1 => return reg.to8(), 2 => return reg.to16(), 4 => return reg.to32(), 8 => return reg.to64(), else => unreachable, } }
const std = @import("std"); const io = std.io; const File = std.fs.File; const Reader = File.Reader; pub fn main() anyerror!void { var buf: [4000]u8 = undefined; var stream = io.bufferedReader(io.getStdIn().reader()); var rd = stream.reader(); var checks = [_]Counter{ .{ .stride = 1, .skip = 0 }, .{ .stride = 3, .skip = 0 }, .{ .stride = 5, .skip = 0 }, .{ .stride = 7, .skip = 0 }, .{ .stride = 1, .skip = 1 }, }; while (try rd.readUntilDelimiterOrEof(&buf, '\n')) |line| { for (checks) |*c| c.consume(line); } var hits: usize = 1; for (checks) |*c| { std.log.notice("Right = {} Down = {} Hits = {}", .{ c.stride, c.skip, c.hits }); hits *= c.hits; } std.log.notice("Product = {}", .{hits}); } const Counter = struct { const Self = @This(); skip: usize = 0, stride: usize = 1, x: usize = 0, y: usize = 0, hits: usize = 0, pub fn consume(self: *Self, line: []const u8) void { const y = self.y; self.y += 1; if (y % (self.skip + 1) != 0) return; if (line[self.x] == '#') self.hits += 1; self.x += self.stride; if (self.x >= line.len) self.x -= line.len; } };
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2020 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const std = @import("std"); const Target = std.Target; const CrossTarget = std.zig.CrossTarget; const XCR0_XMM = 0x02; const XCR0_YMM = 0x04; const XCR0_MASKREG = 0x20; const XCR0_ZMM0_15 = 0x40; const XCR0_ZMM16_31 = 0x80; fn setFeature(cpu: *Target.Cpu, feature: Target.x86.Feature, enabled: bool) void { const idx = @as(Target.Cpu.Feature.Set.Index, @enumToInt(feature)); if (enabled) cpu.features.addFeature(idx) else cpu.features.removeFeature(idx); } inline fn bit(input: u32, offset: u5) bool { return (input >> offset) & 1 != 0; } inline fn hasMask(input: u32, mask: u32) bool { return (input & mask) == mask; } pub fn detectNativeCpuAndFeatures(arch: Target.Cpu.Arch, os: Target.Os, cross_target: CrossTarget) Target.Cpu { var cpu = Target.Cpu{ .arch = arch, .model = Target.Cpu.Model.generic(arch), .features = Target.Cpu.Feature.Set.empty, }; // First we detect features, to use as hints when detecting CPU Model. detectNativeFeatures(&cpu, os.tag); var leaf = cpuid(0, 0); const max_leaf = leaf.eax; const vendor = leaf.ebx; if (max_leaf > 0) { leaf = cpuid(0x1, 0); const brand_id = leaf.ebx & 0xff; // Detect model and family var family = (leaf.eax >> 8) & 0xf; var model = (leaf.eax >> 4) & 0xf; if (family == 6 or family == 0xf) { if (family == 0xf) { family += (leaf.eax >> 20) & 0xff; } model += ((leaf.eax >> 16) & 0xf) << 4; } // Now we detect the model. switch (vendor) { 0x756e6547 => { detectIntelProcessor(&cpu, family, model, brand_id); }, 0x68747541 => { detectAMDProcessor(&cpu, family, model); }, else => {}, } } // Add the CPU model's feature set into the working set, but then // override with actual detected features again. cpu.features.addFeatureSet(cpu.model.features); detectNativeFeatures(&cpu, os.tag); cpu.features.populateDependencies(cpu.arch.allFeaturesList()); return cpu; } fn detectIntelProcessor(cpu: *Target.Cpu, family: u32, model: u32, brand_id: u32) void { if (brand_id != 0) { return; } switch (family) { 3 => { cpu.model = &Target.x86.cpu._i386; return; }, 4 => { cpu.model = &Target.x86.cpu._i486; return; }, 5 => { if (Target.x86.featureSetHas(cpu.features, .mmx)) { cpu.model = &Target.x86.cpu.pentium_mmx; return; } cpu.model = &Target.x86.cpu.pentium; return; }, 6 => { switch (model) { 0x01 => { cpu.model = &Target.x86.cpu.pentiumpro; return; }, 0x03, 0x05, 0x06 => { cpu.model = &Target.x86.cpu.pentium2; return; }, 0x07, 0x08, 0x0a, 0x0b => { cpu.model = &Target.x86.cpu.pentium3; return; }, 0x09, 0x0d, 0x15 => { cpu.model = &Target.x86.cpu.pentium_m; return; }, 0x0e => { cpu.model = &Target.x86.cpu.yonah; return; }, 0x0f, 0x16 => { cpu.model = &Target.x86.cpu.core2; return; }, 0x17, 0x1d => { cpu.model = &Target.x86.cpu.penryn; return; }, 0x1a, 0x1e, 0x1f, 0x2e => { cpu.model = &Target.x86.cpu.nehalem; return; }, 0x25, 0x2c, 0x2f => { cpu.model = &Target.x86.cpu.westmere; return; }, 0x2a, 0x2d => { cpu.model = &Target.x86.cpu.sandybridge; return; }, 0x3a, 0x3e => { cpu.model = &Target.x86.cpu.ivybridge; return; }, 0x3c, 0x3f, 0x45, 0x46 => { cpu.model = &Target.x86.cpu.haswell; return; }, 0x3d, 0x47, 0x4f, 0x56 => { cpu.model = &Target.x86.cpu.broadwell; return; }, 0x4e, 0x5e, 0x8e, 0x9e => { cpu.model = &Target.x86.cpu.skylake; return; }, 0x55 => { if (Target.x86.featureSetHas(cpu.features, .avx512bf16)) { cpu.model = &Target.x86.cpu.cooperlake; return; } else if (Target.x86.featureSetHas(cpu.features, .avx512vnni)) { cpu.model = &Target.x86.cpu.cascadelake; return; } else { cpu.model = &Target.x86.cpu.skylake_avx512; return; } }, 0x66 => { cpu.model = &Target.x86.cpu.cannonlake; return; }, 0x7d, 0x7e => { cpu.model = &Target.x86.cpu.icelake_client; return; }, 0x6a, 0x6c => { cpu.model = &Target.x86.cpu.icelake_server; return; }, 0x1c, 0x26, 0x27, 0x35, 0x36 => { cpu.model = &Target.x86.cpu.bonnell; return; }, 0x37, 0x4a, 0x4d, 0x5a, 0x5d, 0x4c => { cpu.model = &Target.x86.cpu.silvermont; return; }, 0x5c, 0x5f => { cpu.model = &Target.x86.cpu.goldmont; return; }, 0x7a => { cpu.model = &Target.x86.cpu.goldmont_plus; return; }, 0x86 => { cpu.model = &Target.x86.cpu.tremont; return; }, 0x57 => { cpu.model = &Target.x86.cpu.knl; return; }, 0x85 => { cpu.model = &Target.x86.cpu.knm; return; }, else => return, // Unknown CPU Model } }, 15 => { if (Target.x86.featureSetHas(cpu.features, .@"64bit")) { cpu.model = &Target.x86.cpu.nocona; return; } if (Target.x86.featureSetHas(cpu.features, .sse3)) { cpu.model = &Target.x86.cpu.prescott; return; } cpu.model = &Target.x86.cpu.pentium4; return; }, else => return, // Unknown CPU Model } } fn detectAMDProcessor(cpu: *Target.Cpu, family: u32, model: u32) void { // AMD's cpuid information is less than optimal for determining a CPU model. // This is very unscientific, and not necessarily correct. switch (family) { 4 => { cpu.model = &Target.x86.cpu._i486; return; }, 5 => { cpu.model = &Target.x86.cpu.pentium; switch (model) { 6, 7 => { cpu.model = &Target.x86.cpu.k6; return; }, 8 => { cpu.model = &Target.x86.cpu.k6_2; return; }, 9, 13 => { cpu.model = &Target.x86.cpu.k6_3; return; }, 10 => { cpu.model = &Target.x86.cpu.geode; return; }, else => {}, } return; }, 6 => { if (Target.x86.featureSetHas(cpu.features, .sse)) { cpu.model = &Target.x86.cpu.athlon_xp; return; } cpu.model = &Target.x86.cpu.athlon; return; }, 15 => { if (Target.x86.featureSetHas(cpu.features, .sse3)) { cpu.model = &Target.x86.cpu.k8_sse3; return; } cpu.model = &Target.x86.cpu.k8; return; }, 16 => { cpu.model = &Target.x86.cpu.amdfam10; return; }, 20 => { cpu.model = &Target.x86.cpu.btver1; return; }, 21 => { cpu.model = &Target.x86.cpu.bdver1; if (model >= 0x60 and model <= 0x7f) { cpu.model = &Target.x86.cpu.bdver4; return; } if (model >= 0x30 and model <= 0x3f) { cpu.model = &Target.x86.cpu.bdver3; return; } if ((model >= 0x10 and model <= 0x1f) or model == 0x02) { cpu.model = &Target.x86.cpu.bdver2; return; } return; }, 22 => { cpu.model = &Target.x86.cpu.btver2; return; }, 23 => { cpu.model = &Target.x86.cpu.znver1; if ((model >= 0x30 and model <= 0x3f) or model == 0x71) { cpu.model = &Target.x86.cpu.znver2; return; } return; }, else => { return; }, } } fn detectNativeFeatures(cpu: *Target.Cpu, os_tag: Target.Os.Tag) void { var leaf = cpuid(0, 0); const max_level = leaf.eax; leaf = cpuid(1, 0); setFeature(cpu, .cx8, bit(leaf.edx, 8)); setFeature(cpu, .cmov, bit(leaf.edx, 15)); setFeature(cpu, .mmx, bit(leaf.edx, 23)); setFeature(cpu, .fxsr, bit(leaf.edx, 24)); setFeature(cpu, .sse, bit(leaf.edx, 25)); setFeature(cpu, .sse2, bit(leaf.edx, 26)); setFeature(cpu, .sse3, bit(leaf.ecx, 0)); setFeature(cpu, .pclmul, bit(leaf.ecx, 1)); setFeature(cpu, .ssse3, bit(leaf.ecx, 9)); setFeature(cpu, .cx16, bit(leaf.ecx, 13)); setFeature(cpu, .sse4_1, bit(leaf.ecx, 19)); setFeature(cpu, .sse4_2, bit(leaf.ecx, 20)); setFeature(cpu, .movbe, bit(leaf.ecx, 22)); setFeature(cpu, .popcnt, bit(leaf.ecx, 23)); setFeature(cpu, .aes, bit(leaf.ecx, 25)); setFeature(cpu, .rdrnd, bit(leaf.ecx, 30)); const has_xsave = bit(leaf.ecx, 27); const has_avx = bit(leaf.ecx, 28); // Make sure not to call xgetbv if xsave is not supported const xcr0_eax = if (has_xsave and has_avx) getXCR0() else 0; const has_avx_save = hasMask(xcr0_eax, XCR0_XMM | XCR0_YMM); // LLVM approaches avx512_save by hardcoding it to true on Darwin, // because the kernel saves the context even if the bit is not set. // https://github.com/llvm/llvm-project/blob/bca373f73fc82728a8335e7d6cd164e8747139ec/llvm/lib/Support/Host.cpp#L1378 // // Google approaches this by using a different series of checks and flags, // and this may report the feature more accurately on a technically correct // but ultimately less useful level. // https://github.com/google/cpu_features/blob/b5c271c53759b2b15ff91df19bd0b32f2966e275/src/cpuinfo_x86.c#L113 // (called from https://github.com/google/cpu_features/blob/b5c271c53759b2b15ff91df19bd0b32f2966e275/src/cpuinfo_x86.c#L1052) // // Right now, we use LLVM's approach, because even if the target doesn't support // the feature, the kernel should provide the same functionality transparently, // so the implementation details don't make a difference. // That said, this flag impacts other CPU features' availability, // so until we can verify that this doesn't come with side affects, // we'll say TODO verify this. // Darwin lazily saves the AVX512 context on first use: trust that the OS will // save the AVX512 context if we use AVX512 instructions, even if the bit is not // set right now. const has_avx512_save = switch (os_tag.isDarwin()) { true => true, false => hasMask(xcr0_eax, XCR0_MASKREG | XCR0_ZMM0_15 | XCR0_ZMM16_31), }; setFeature(cpu, .avx, has_avx_save); setFeature(cpu, .fma, has_avx_save and bit(leaf.ecx, 12)); // Only enable XSAVE if OS has enabled support for saving YMM state. setFeature(cpu, .xsave, has_avx_save and bit(leaf.ecx, 26)); setFeature(cpu, .f16c, has_avx_save and bit(leaf.ecx, 29)); leaf = cpuid(0x80000000, 0); const max_ext_level = leaf.eax; if (max_ext_level >= 0x80000001) { leaf = cpuid(0x80000001, 0); setFeature(cpu, .sahf, bit(leaf.ecx, 0)); setFeature(cpu, .lzcnt, bit(leaf.ecx, 5)); setFeature(cpu, .sse4a, bit(leaf.ecx, 6)); setFeature(cpu, .prfchw, bit(leaf.ecx, 8)); setFeature(cpu, .xop, bit(leaf.ecx, 11) and has_avx_save); setFeature(cpu, .lwp, bit(leaf.ecx, 15)); setFeature(cpu, .fma4, bit(leaf.ecx, 16) and has_avx_save); setFeature(cpu, .tbm, bit(leaf.ecx, 21)); setFeature(cpu, .mwaitx, bit(leaf.ecx, 29)); setFeature(cpu, .@"64bit", bit(leaf.edx, 29)); } else { for ([_]Target.x86.Feature{ .sahf, .lzcnt, .sse4a, .prfchw, .xop, .lwp, .fma4, .tbm, .mwaitx, .@"64bit", }) |feat| { setFeature(cpu, feat, false); } } // Misc. memory-related features. if (max_ext_level >= 0x80000008) { leaf = cpuid(0x80000008, 0); setFeature(cpu, .clzero, bit(leaf.ebx, 0)); setFeature(cpu, .wbnoinvd, bit(leaf.ebx, 9)); } else { for ([_]Target.x86.Feature{ .clzero, .wbnoinvd }) |feat| { setFeature(cpu, feat, false); } } if (max_level >= 0x7) { leaf = cpuid(0x7, 0); setFeature(cpu, .fsgsbase, bit(leaf.ebx, 0)); setFeature(cpu, .sgx, bit(leaf.ebx, 2)); setFeature(cpu, .bmi, bit(leaf.ebx, 3)); // AVX2 is only supported if we have the OS save support from AVX. setFeature(cpu, .avx2, bit(leaf.ebx, 5) and has_avx_save); setFeature(cpu, .bmi2, bit(leaf.ebx, 8)); setFeature(cpu, .invpcid, bit(leaf.ebx, 10)); setFeature(cpu, .rtm, bit(leaf.ebx, 11)); // AVX512 is only supported if the OS supports the context save for it. setFeature(cpu, .avx512f, bit(leaf.ebx, 16) and has_avx512_save); setFeature(cpu, .avx512dq, bit(leaf.ebx, 17) and has_avx512_save); setFeature(cpu, .rdseed, bit(leaf.ebx, 18)); setFeature(cpu, .adx, bit(leaf.ebx, 19)); setFeature(cpu, .avx512ifma, bit(leaf.ebx, 21) and has_avx512_save); setFeature(cpu, .clflushopt, bit(leaf.ebx, 23)); setFeature(cpu, .clwb, bit(leaf.ebx, 24)); setFeature(cpu, .avx512pf, bit(leaf.ebx, 26) and has_avx512_save); setFeature(cpu, .avx512er, bit(leaf.ebx, 27) and has_avx512_save); setFeature(cpu, .avx512cd, bit(leaf.ebx, 28) and has_avx512_save); setFeature(cpu, .sha, bit(leaf.ebx, 29)); setFeature(cpu, .avx512bw, bit(leaf.ebx, 30) and has_avx512_save); setFeature(cpu, .avx512vl, bit(leaf.ebx, 31) and has_avx512_save); setFeature(cpu, .prefetchwt1, bit(leaf.ecx, 0)); setFeature(cpu, .avx512vbmi, bit(leaf.ecx, 1) and has_avx512_save); setFeature(cpu, .pku, bit(leaf.ecx, 4)); setFeature(cpu, .waitpkg, bit(leaf.ecx, 5)); setFeature(cpu, .avx512vbmi2, bit(leaf.ecx, 6) and has_avx512_save); setFeature(cpu, .shstk, bit(leaf.ecx, 7)); setFeature(cpu, .gfni, bit(leaf.ecx, 8)); setFeature(cpu, .vaes, bit(leaf.ecx, 9) and has_avx_save); setFeature(cpu, .vpclmulqdq, bit(leaf.ecx, 10) and has_avx_save); setFeature(cpu, .avx512vnni, bit(leaf.ecx, 11) and has_avx512_save); setFeature(cpu, .avx512bitalg, bit(leaf.ecx, 12) and has_avx512_save); setFeature(cpu, .avx512vpopcntdq, bit(leaf.ecx, 14) and has_avx512_save); setFeature(cpu, .avx512vp2intersect, bit(leaf.edx, 8) and has_avx512_save); setFeature(cpu, .rdpid, bit(leaf.ecx, 22)); setFeature(cpu, .cldemote, bit(leaf.ecx, 25)); setFeature(cpu, .movdiri, bit(leaf.ecx, 27)); setFeature(cpu, .movdir64b, bit(leaf.ecx, 28)); setFeature(cpu, .enqcmd, bit(leaf.ecx, 29)); // There are two CPUID leafs which information associated with the pconfig // instruction: // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th // bit of EDX), while the EAX=0x1b leaf returns information on the // availability of specific pconfig leafs. // The target feature here only refers to the the first of these two. // Users might need to check for the availability of specific pconfig // leaves using cpuid, since that information is ignored while // detecting features using the "-march=native" flag. // For more info, see X86 ISA docs. setFeature(cpu, .pconfig, bit(leaf.edx, 18)); // TODO I feel unsure about this check. // It doesn't really seem to check for 7.1, just for 7. // Is this a sound assumption to make? // Note that this is what other implementations do, so I kind of trust it. const has_leaf_7_1 = max_level >= 7; if (has_leaf_7_1) { leaf = cpuid(0x7, 0x1); setFeature(cpu, .avx512bf16, bit(leaf.eax, 5) and has_avx512_save); } else { setFeature(cpu, .avx512bf16, false); } } else { for ([_]Target.x86.Feature{ .fsgsbase, .sgx, .bmi, .avx2, .bmi2, .invpcid, .rtm, .avx512f, .avx512dq, .rdseed, .adx, .avx512ifma, .clflushopt, .clwb, .avx512pf, .avx512er, .avx512cd, .sha, .avx512bw, .avx512vl, .prefetchwt1, .avx512vbmi, .pku, .waitpkg, .avx512vbmi2, .shstk, .gfni, .vaes, .vpclmulqdq, .avx512vnni, .avx512bitalg, .avx512vpopcntdq, .avx512vp2intersect, .rdpid, .cldemote, .movdiri, .movdir64b, .enqcmd, .pconfig, .avx512bf16, }) |feat| { setFeature(cpu, feat, false); } } if (max_level >= 0xD and has_avx_save) { leaf = cpuid(0xD, 0x1); // Only enable XSAVE if OS has enabled support for saving YMM state. setFeature(cpu, .xsaveopt, bit(leaf.eax, 0)); setFeature(cpu, .xsavec, bit(leaf.eax, 1)); setFeature(cpu, .xsaves, bit(leaf.eax, 3)); } else { for ([_]Target.x86.Feature{ .xsaveopt, .xsavec, .xsaves }) |feat| { setFeature(cpu, feat, false); } } if (max_level >= 0x14) { leaf = cpuid(0x14, 0); setFeature(cpu, .ptwrite, bit(leaf.ebx, 4)); } else { setFeature(cpu, .ptwrite, false); } } const CpuidLeaf = packed struct { eax: u32, ebx: u32, ecx: u32, edx: u32, }; fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf { // Workaround for https://github.com/ziglang/zig/issues/215 // Inline assembly in zig only supports one output, // so we pass a pointer to the struct. var cpuid_leaf: CpuidLeaf = undefined; // valid for both x86 and x86_64 asm volatile ( \\ cpuid \\ movl %%eax, 0(%[leaf_ptr]) \\ movl %%ebx, 4(%[leaf_ptr]) \\ movl %%ecx, 8(%[leaf_ptr]) \\ movl %%edx, 12(%[leaf_ptr]) : : [leaf_id] "{eax}" (leaf_id), [subid] "{ecx}" (subid), [leaf_ptr] "r" (&cpuid_leaf) : "eax", "ebx", "ecx", "edx" ); return cpuid_leaf; } // Read control register 0 (XCR0). Used to detect features such as AVX. fn getXCR0() u32 { return asm volatile ( \\ xor %%ecx, %%ecx \\ xgetbv : [ret] "={eax}" (-> u32) : : "eax", "edx", "ecx" ); }
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2020 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const std = @import("../std.zig"); const builtin = std.builtin; usingnamespace std.c; extern "c" fn __errno() *c_int; pub const _errno = __errno; pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int; pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int; pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int; pub extern "c" fn __clock_gettime50(clk_id: c_int, tp: *timespec) c_int; pub extern "c" fn __clock_getres50(clk_id: c_int, tp: *timespec) c_int; pub extern "c" fn __getdents30(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int; pub extern "c" fn __sigaltstack14(ss: ?*stack_t, old_ss: ?*stack_t) c_int; pub extern "c" fn __nanosleep50(rqtp: *const timespec, rmtp: ?*timespec) c_int; pub extern "c" fn __sigaction14(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int; pub extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int; pub extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int; pub extern "c" fn __gettimeofday50(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int; pub extern "c" fn __getrusage50(who: c_int, usage: *rusage) c_int; // libc aliases this as sched_yield pub extern "c" fn __libc_thr_yield() c_int; pub const pthread_mutex_t = extern struct { ptm_magic: u32 = 0x33330003, ptm_errorcheck: padded_pthread_spin_t = 0, ptm_ceiling: padded_pthread_spin_t = 0, ptm_owner: usize = 0, ptm_waiters: ?*u8 = null, ptm_recursed: u32 = 0, ptm_spare2: ?*c_void = null, }; pub const pthread_cond_t = extern struct { ptc_magic: u32 = 0x55550005, ptc_lock: pthread_spin_t = 0, ptc_waiters_first: ?*u8 = null, ptc_waiters_last: ?*u8 = null, ptc_mutex: ?*pthread_mutex_t = null, ptc_private: ?*c_void = null, }; const pthread_spin_t = switch (builtin.arch) { .aarch64, .aarch64_be, .aarch64_32 => u8, .mips, .mipsel, .mips64, .mips64el => u32, .powerpc, .powerpc64, .powerpc64le => i32, .i386, .x86_64 => u8, .arm, .armeb, .thumb, .thumbeb => i32, .sparc, .sparcel, .sparcv9 => u8, .riscv32, .riscv64 => u32, else => @compileError("undefined pthread_spin_t for this arch"), }; const padded_pthread_spin_t = switch (builtin.arch) { .i386, .x86_64 => u32, .sparc, .sparcel, .sparcv9 => u32, else => pthread_spin_t, }; pub const pthread_attr_t = extern struct { pta_magic: u32, pta_flags: i32, pta_private: ?*c_void, };
const std = @import("std"); const os = std.os; const mibu = @import("mibu"); const cursor = mibu.cursor; const clear = mibu.clear; const RawTerm = mibu.term.RawTerm; const Buffer = @import("buffer.zig").Buffer; const Widget = @import("./widget.zig").Widget; const Queue = @import("./mpsc.zig").Queue; const Cell = @import("buffer.zig").Cell; const events = @import("./events.zig"); /// Main structure pub const Terminal = struct { allocator: std.mem.Allocator, raw_term: RawTerm, queue: Queue(mibu.events.Event), buffers: [2]Buffer, // one buffer is previous state current: usize = 0, // current buffer needs_clean: bool = false, const Self = @This(); pub fn init(allocator: std.mem.Allocator, handle: std.os.system.fd_t) !Self { var self = Self{ .allocator = allocator, .buffers = .{ Buffer.init(allocator), Buffer.init(allocator) }, .raw_term = try RawTerm.enableRawMode(handle, .blocking), .queue = Queue(mibu.events.Event).init(allocator), }; return self; } pub fn deinit(self: *Self) void { self.raw_term.disableRawMode() catch {}; self.queue.deinit(); self.buffers[0].deinit(); self.buffers[1].deinit(); } /// Resize screen buffer, useful when terminal size changes pub fn resize(self: *Self) !void { // clear all screen for (self.buffers) |*buffer| { _ = try buffer.resize(); } self.needs_clean = true; } /// Spawns event queue thread pub fn startEvents(self: *Self, in: anytype) !void { try events.spawnEventsThread(in, &self.queue); // Resize event (SIGWINCH) const gen = struct { pub threadlocal var _self: *Self = undefined; fn handleSigWinch(_: c_int) callconv(.C) void { _self.queue.push(.resize); } }; gen._self = self; os.sigaction(os.SIG.WINCH, &os.Sigaction{ .handler = .{ .handler = gen.handleSigWinch }, .mask = os.empty_sigset, .flags = 0, }, null); } /// Blocks thread until next event pub fn nextEvent(self: *Self) mibu.events.Event { return self.queue.pop(); } /// Blocks thread until next event or timeout reach pub fn nextEventTimeout(self: *Self, timeout: u64) ?mibu.events.Event { return self.queue.popTimeout(timeout); } /// Draws a widget on the screen. pub fn drawWidget(self: *Self, widget: *Widget) void { var update_buffer = &self.buffers[1 - self.current]; widget.draw(update_buffer); } /// Flush the buffer to the screen, it should be called /// every time you want to update. pub fn flush(self: *Self, out: anytype) !void { // clear screen is only needed when buffer is resized if (self.needs_clean) { try out.print("{s}", .{clear.all}); self.needs_clean = false; } const current_buffer = &self.buffers[self.current]; const update_buffer = &self.buffers[1 - self.current]; const updates = try current_buffer.diff(update_buffer); defer updates.deinit(); // hide cursor before rendering try out.print("{s}", .{cursor.hide()}); defer out.print("{s}", .{cursor.show()}) catch {}; for (updates.items) |update| { try out.print("{s}{s}{u}", .{ cursor.goTo(update.x + 1, update.y + 1), update.c.*.style.s(), update.c.*.value }); } // after draw change current buffers self.buffers[self.current].reset(); self.current = 1 - self.current; } }; // TODO: change location pub const Rect = struct { row: usize, col: usize, w: usize, h: usize, }; test "refAll" { std.testing.refAllDecls(@This()); }
const std = @import("std.zig"); const math = std.math; const assert = std.debug.assert; const mem = std.mem; const builtin = @import("builtin"); const errol = @import("fmt/errol.zig"); const lossyCast = std.math.lossyCast; pub const default_max_depth = 3; pub const Alignment = enum { Left, Center, Right, }; pub const FormatOptions = struct { precision: ?usize = null, width: ?usize = null, alignment: ?Alignment = null, fill: u8 = ' ', }; fn peekIsAlign(comptime fmt: []const u8) bool { // Should only be called during a state transition to the format segment. comptime assert(fmt[0] == ':'); inline for (([_]u8{ 1, 2 })[0..]) |i| { if (fmt.len > i and (fmt[i] == '<' or fmt[i] == '^' or fmt[i] == '>')) { return true; } } return false; } /// Renders fmt string with args, calling output with slices of bytes. /// If `output` returns an error, the error is returned from `format` and /// `output` is not called again. /// /// The format string must be comptime known and may contain placeholders following /// this format: /// `{[position][specifier]:[fill][alignment][width].[precision]}` /// /// Each word between `[` and `]` is a parameter you have to replace with something: /// /// - *position* is the index of the argument that should be inserted /// - *specifier* is a type-dependent formatting option that determines how a type should formatted (see below) /// - *fill* is a single character which is used to pad the formatted text /// - *alignment* is one of the three characters `<`, `^` or `>`. they define if the text is *left*, *center*, or *right* aligned /// - *width* is the total width of the field in characters /// - *precision* specifies how many decimals a formatted number should have /// /// Note that most of the parameters are optional and may be omitted. Also you can leave out separators like `:` and `.` when /// all parameters after the separator are omitted. /// Only exception is the *fill* parameter. If *fill* is required, one has to specify *alignment* as well, as otherwise /// the digits after `:` is interpreted as *width*, not *fill*. /// /// The *specifier* has several options for types: /// - `x` and `X`: /// - format the non-numeric value as a string of bytes in hexadecimal notation ("binary dump") in either lower case or upper case /// - output numeric value in hexadecimal notation /// - `s`: print a pointer-to-many as a c-string, use zero-termination /// - `B` and `Bi`: output a memory size in either metric (1000) or power-of-two (1024) based notation. works for both float and integer values. /// - `e`: output floating point value in scientific notation /// - `d`: output numeric value in decimal notation /// - `b`: output integer value in binary notation /// - `c`: output integer as an ASCII character. Integer type must have 8 bits at max. /// - `*`: output the address of the value instead of the value itself. /// /// If a formatted user type contains a function of the type /// ``` /// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: var) !void /// ``` /// with `?` being the type formatted, this function will be called instead of the default implementation. /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. /// /// A user type may be a `struct`, `vector`, `union` or `enum` type. pub fn format( out_stream: var, comptime fmt: []const u8, args: var, ) !void { const ArgSetType = u32; if (@typeInfo(@TypeOf(args)) != .Struct) { @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args))); } if (args.len > ArgSetType.bit_count) { @compileError("32 arguments max are supported per format call"); } const State = enum { Start, Positional, CloseBrace, Specifier, FormatFillAndAlign, FormatWidth, FormatPrecision, }; comptime var start_index = 0; comptime var state = State.Start; comptime var maybe_pos_arg: ?comptime_int = null; comptime var specifier_start = 0; comptime var specifier_end = 0; comptime var options = FormatOptions{}; comptime var arg_state: struct { next_arg: usize = 0, used_args: ArgSetType = 0, args_len: usize = args.len, fn hasUnusedArgs(comptime self: *@This()) bool { return (@popCount(ArgSetType, self.used_args) != self.args_len); } fn nextArg(comptime self: *@This(), comptime pos_arg: ?comptime_int) comptime_int { const next_idx = pos_arg orelse blk: { const arg = self.next_arg; self.next_arg += 1; break :blk arg; }; if (next_idx >= self.args_len) { @compileError("Too few arguments"); } // Mark this argument as used self.used_args |= 1 << next_idx; return next_idx; } } = .{}; inline for (fmt) |c, i| { switch (state) { .Start => switch (c) { '{' => { if (start_index < i) { try out_stream.writeAll(fmt[start_index..i]); } start_index = i; specifier_start = i + 1; specifier_end = i + 1; maybe_pos_arg = null; state = .Positional; options = FormatOptions{}; }, '}' => { if (start_index < i) { try out_stream.writeAll(fmt[start_index..i]); } state = .CloseBrace; }, else => {}, }, .Positional => switch (c) { '{' => { state = .Start; start_index = i; }, ':' => { state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth; specifier_end = i; }, '0'...'9' => { if (maybe_pos_arg == null) { maybe_pos_arg = 0; } maybe_pos_arg.? *= 10; maybe_pos_arg.? += c - '0'; specifier_start = i + 1; if (maybe_pos_arg.? >= args.len) { @compileError("Positional value refers to non-existent argument"); } }, '}' => { const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); try formatType( args[arg_to_print], fmt[0..0], options, out_stream, default_max_depth, ); state = .Start; start_index = i + 1; }, else => { state = .Specifier; specifier_start = i; }, }, .CloseBrace => switch (c) { '}' => { state = .Start; start_index = i; }, else => @compileError("Single '}' encountered in format string"), }, .Specifier => switch (c) { ':' => { specifier_end = i; state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth; }, '}' => { const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); try formatType( args[arg_to_print], fmt[specifier_start..i], options, out_stream, default_max_depth, ); state = .Start; start_index = i + 1; }, else => {}, }, // Only entered if the format string contains a fill/align segment. .FormatFillAndAlign => switch (c) { '<' => { options.alignment = Alignment.Left; state = .FormatWidth; }, '^' => { options.alignment = Alignment.Center; state = .FormatWidth; }, '>' => { options.alignment = Alignment.Right; state = .FormatWidth; }, else => { options.fill = c; }, }, .FormatWidth => switch (c) { '0'...'9' => { if (options.width == null) { options.width = 0; } options.width.? *= 10; options.width.? += c - '0'; }, '.' => { state = .FormatPrecision; }, '}' => { const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); try formatType( args[arg_to_print], fmt[specifier_start..specifier_end], options, out_stream, default_max_depth, ); state = .Start; start_index = i + 1; }, else => { @compileError("Unexpected character in width value: " ++ [_]u8{c}); }, }, .FormatPrecision => switch (c) { '0'...'9' => { if (options.precision == null) { options.precision = 0; } options.precision.? *= 10; options.precision.? += c - '0'; }, '}' => { const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); try formatType( args[arg_to_print], fmt[specifier_start..specifier_end], options, out_stream, default_max_depth, ); state = .Start; start_index = i + 1; }, else => { @compileError("Unexpected character in precision value: " ++ [_]u8{c}); }, }, } } comptime { if (comptime arg_state.hasUnusedArgs()) { @compileError("Unused arguments"); } if (state != State.Start) { @compileError("Incomplete format string: " ++ fmt); } } if (start_index < fmt.len) { try out_stream.writeAll(fmt[start_index..]); } } pub fn formatType( value: var, comptime fmt: []const u8, options: FormatOptions, out_stream: var, max_depth: usize, ) @TypeOf(out_stream).Error!void { if (comptime std.mem.eql(u8, fmt, "*")) { try out_stream.writeAll(@typeName(@TypeOf(value).Child)); try out_stream.writeAll("@"); try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, out_stream); return; } const T = @TypeOf(value); if (comptime std.meta.trait.hasFn("format")(T)) { return try value.format(fmt, options, out_stream); } switch (@typeInfo(T)) { .ComptimeInt, .Int, .Float => { return formatValue(value, fmt, options, out_stream); }, .Void => { return out_stream.writeAll("void"); }, .Bool => { return out_stream.writeAll(if (value) "true" else "false"); }, .Optional => { if (value) |payload| { return formatType(payload, fmt, options, out_stream, max_depth); } else { return out_stream.writeAll("null"); } }, .ErrorUnion => { if (value) |payload| { return formatType(payload, fmt, options, out_stream, max_depth); } else |err| { return formatType(err, fmt, options, out_stream, max_depth); } }, .ErrorSet => { try out_stream.writeAll("error."); return out_stream.writeAll(@errorName(value)); }, .Enum => |enumInfo| { try out_stream.writeAll(@typeName(T)); if (enumInfo.is_exhaustive) { try out_stream.writeAll("."); try out_stream.writeAll(@tagName(value)); } else { // TODO: when @tagName works on exhaustive enums print known enum strings try out_stream.writeAll("("); try formatType(@enumToInt(value), fmt, options, out_stream, max_depth); try out_stream.writeAll(")"); } }, .Union => { try out_stream.writeAll(@typeName(T)); if (max_depth == 0) { return out_stream.writeAll("{ ... }"); } const info = @typeInfo(T).Union; if (info.tag_type) |UnionTagType| { try out_stream.writeAll("{ ."); try out_stream.writeAll(@tagName(@as(UnionTagType, value))); try out_stream.writeAll(" = "); inline for (info.fields) |u_field| { if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) { try formatType(@field(value, u_field.name), fmt, options, out_stream, max_depth - 1); } } try out_stream.writeAll(" }"); } else { try format(out_stream, "@{x}", .{@ptrToInt(&value)}); } }, .Struct => |StructT| { try out_stream.writeAll(@typeName(T)); if (max_depth == 0) { return out_stream.writeAll("{ ... }"); } try out_stream.writeAll("{"); inline for (StructT.fields) |f, i| { if (i == 0) { try out_stream.writeAll(" ."); } else { try out_stream.writeAll(", ."); } try out_stream.writeAll(f.name); try out_stream.writeAll(" = "); try formatType(@field(value, f.name), fmt, options, out_stream, max_depth - 1); } try out_stream.writeAll(" }"); }, .Pointer => |ptr_info| switch (ptr_info.size) { .One => switch (@typeInfo(ptr_info.child)) { .Array => |info| { if (info.child == u8) { return formatText(value, fmt, options, out_stream); } return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); }, .Enum, .Union, .Struct => { return formatType(value.*, fmt, options, out_stream, max_depth); }, else => return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }), }, .Many, .C => { if (ptr_info.sentinel) |sentinel| { return formatType(mem.span(value), fmt, options, out_stream, max_depth); } if (ptr_info.child == u8) { if (fmt.len > 0 and fmt[0] == 's') { return formatText(mem.span(value), fmt, options, out_stream); } } return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); }, .Slice => { if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) { return formatText(value, fmt, options, out_stream); } if (ptr_info.child == u8) { return formatText(value, fmt, options, out_stream); } return format(out_stream, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) }); }, }, .Array => |info| { const Slice = @Type(builtin.TypeInfo{ .Pointer = .{ .size = .Slice, .is_const = true, .is_volatile = false, .is_allowzero = false, .alignment = @alignOf(info.child), .child = info.child, .sentinel = null, }, }); return formatType(@as(Slice, &value), fmt, options, out_stream, max_depth); }, .Vector => { const len = @typeInfo(T).Vector.len; try out_stream.writeAll("{ "); var i: usize = 0; while (i < len) : (i += 1) { try formatValue(value[i], fmt, options, out_stream); if (i < len - 1) { try out_stream.writeAll(", "); } } try out_stream.writeAll(" }"); }, .Fn => { return format(out_stream, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); }, .Type => return out_stream.writeAll(@typeName(T)), .EnumLiteral => { const buffer = [_]u8{'.'} ++ @tagName(value); return formatType(buffer, fmt, options, out_stream, max_depth); }, else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"), } } fn formatValue( value: var, comptime fmt: []const u8, options: FormatOptions, out_stream: var, ) !void { if (comptime std.mem.eql(u8, fmt, "B")) { return formatBytes(value, options, 1000, out_stream); } else if (comptime std.mem.eql(u8, fmt, "Bi")) { return formatBytes(value, options, 1024, out_stream); } const T = @TypeOf(value); switch (@typeInfo(T)) { .Float => return formatFloatValue(value, fmt, options, out_stream), .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream), .Bool => return out_stream.writeAll(if (value) "true" else "false"), else => comptime unreachable, } } pub fn formatIntValue( value: var, comptime fmt: []const u8, options: FormatOptions, out_stream: var, ) !void { comptime var radix = 10; comptime var uppercase = false; const int_value = if (@TypeOf(value) == comptime_int) blk: { const Int = math.IntFittingRange(value, value); break :blk @as(Int, value); } else value; if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) { radix = 10; uppercase = false; } else if (comptime std.mem.eql(u8, fmt, "c")) { if (@TypeOf(int_value).bit_count <= 8) { return formatAsciiChar(@as(u8, int_value), options, out_stream); } else { @compileError("Cannot print integer that is larger than 8 bits as a ascii"); } } else if (comptime std.mem.eql(u8, fmt, "b")) { radix = 2; uppercase = false; } else if (comptime std.mem.eql(u8, fmt, "x")) { radix = 16; uppercase = false; } else if (comptime std.mem.eql(u8, fmt, "X")) { radix = 16; uppercase = true; } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } return formatInt(int_value, radix, uppercase, options, out_stream); } fn formatFloatValue( value: var, comptime fmt: []const u8, options: FormatOptions, out_stream: var, ) !void { if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { return formatFloatScientific(value, options, out_stream); } else if (comptime std.mem.eql(u8, fmt, "d")) { return formatFloatDecimal(value, options, out_stream); } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } } pub fn formatText( bytes: []const u8, comptime fmt: []const u8, options: FormatOptions, out_stream: var, ) !void { if (fmt.len == 0) { return out_stream.writeAll(bytes); } else if (comptime std.mem.eql(u8, fmt, "s")) { return formatBuf(bytes, options, out_stream); } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { for (bytes) |c| { try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, out_stream); } return; } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } } pub fn formatAsciiChar( c: u8, options: FormatOptions, out_stream: var, ) !void { return out_stream.writeAll(@as(*const [1]u8, &c)); } pub fn formatBuf( buf: []const u8, options: FormatOptions, out_stream: var, ) !void { try out_stream.writeAll(buf); const width = options.width orelse 0; var leftover_padding = if (width > buf.len) (width - buf.len) else return; const pad_byte = [1]u8{options.fill}; while (leftover_padding > 0) : (leftover_padding -= 1) { try out_stream.writeAll(&pad_byte); } } // Print a float in scientific notation to the specified precision. Null uses full precision. // It should be the case that every full precision, printed value can be re-parsed back to the // same type unambiguously. pub fn formatFloatScientific( value: var, options: FormatOptions, out_stream: var, ) !void { var x = @floatCast(f64, value); // Errol doesn't handle these special cases. if (math.signbit(x)) { try out_stream.writeAll("-"); x = -x; } if (math.isNan(x)) { return out_stream.writeAll("nan"); } if (math.isPositiveInf(x)) { return out_stream.writeAll("inf"); } if (x == 0.0) { try out_stream.writeAll("0"); if (options.precision) |precision| { if (precision != 0) { try out_stream.writeAll("."); var i: usize = 0; while (i < precision) : (i += 1) { try out_stream.writeAll("0"); } } } else { try out_stream.writeAll(".0"); } try out_stream.writeAll("e+00"); return; } var buffer: [32]u8 = undefined; var float_decimal = errol.errol3(x, buffer[0..]); if (options.precision) |precision| { errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); try out_stream.writeAll(float_decimal.digits[0..1]); // {e0} case prints no `.` if (precision != 0) { try out_stream.writeAll("."); var printed: usize = 0; if (float_decimal.digits.len > 1) { const num_digits = math.min(float_decimal.digits.len, precision + 1); try out_stream.writeAll(float_decimal.digits[1..num_digits]); printed += num_digits - 1; } while (printed < precision) : (printed += 1) { try out_stream.writeAll("0"); } } } else { try out_stream.writeAll(float_decimal.digits[0..1]); try out_stream.writeAll("."); if (float_decimal.digits.len > 1) { const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len; try out_stream.writeAll(float_decimal.digits[1..num_digits]); } else { try out_stream.writeAll("0"); } } try out_stream.writeAll("e"); const exp = float_decimal.exp - 1; if (exp >= 0) { try out_stream.writeAll("+"); if (exp > -10 and exp < 10) { try out_stream.writeAll("0"); } try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, out_stream); } else { try out_stream.writeAll("-"); if (exp > -10 and exp < 10) { try out_stream.writeAll("0"); } try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, out_stream); } } // Print a float of the format x.yyyyy where the number of y is specified by the precision argument. // By default floats are printed at full precision (no rounding). pub fn formatFloatDecimal( value: var, options: FormatOptions, out_stream: var, ) !void { var x = @as(f64, value); // Errol doesn't handle these special cases. if (math.signbit(x)) { try out_stream.writeAll("-"); x = -x; } if (math.isNan(x)) { return out_stream.writeAll("nan"); } if (math.isPositiveInf(x)) { return out_stream.writeAll("inf"); } if (x == 0.0) { try out_stream.writeAll("0"); if (options.precision) |precision| { if (precision != 0) { try out_stream.writeAll("."); var i: usize = 0; while (i < precision) : (i += 1) { try out_stream.writeAll("0"); } } else { try out_stream.writeAll(".0"); } } return; } // non-special case, use errol3 var buffer: [32]u8 = undefined; var float_decimal = errol.errol3(x, buffer[0..]); if (options.precision) |precision| { errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal); // exp < 0 means the leading is always 0 as errol result is normalized. var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0; // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this. var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len); if (num_digits_whole > 0) { // We may have to zero pad, for instance 1e4 requires zero padding. try out_stream.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]); var i = num_digits_whole_no_pad; while (i < num_digits_whole) : (i += 1) { try out_stream.writeAll("0"); } } else { try out_stream.writeAll("0"); } // {.0} special case doesn't want a trailing '.' if (precision == 0) { return; } try out_stream.writeAll("."); // Keep track of fractional count printed for case where we pre-pad then post-pad with 0's. var printed: usize = 0; // Zero-fill until we reach significant digits or run out of precision. if (float_decimal.exp <= 0) { const zero_digit_count = @intCast(usize, -float_decimal.exp); const zeros_to_print = math.min(zero_digit_count, precision); var i: usize = 0; while (i < zeros_to_print) : (i += 1) { try out_stream.writeAll("0"); printed += 1; } if (printed >= precision) { return; } } // Remaining fractional portion, zero-padding if insufficient. assert(precision >= printed); if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) { try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]); return; } else { try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad..]); printed += float_decimal.digits.len - num_digits_whole_no_pad; while (printed < precision) : (printed += 1) { try out_stream.writeAll("0"); } } } else { // exp < 0 means the leading is always 0 as errol result is normalized. var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0; // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this. var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len); if (num_digits_whole > 0) { // We may have to zero pad, for instance 1e4 requires zero padding. try out_stream.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]); var i = num_digits_whole_no_pad; while (i < num_digits_whole) : (i += 1) { try out_stream.writeAll("0"); } } else { try out_stream.writeAll("0"); } // Omit `.` if no fractional portion if (float_decimal.exp >= 0 and num_digits_whole_no_pad == float_decimal.digits.len) { return; } try out_stream.writeAll("."); // Zero-fill until we reach significant digits or run out of precision. if (float_decimal.exp < 0) { const zero_digit_count = @intCast(usize, -float_decimal.exp); var i: usize = 0; while (i < zero_digit_count) : (i += 1) { try out_stream.writeAll("0"); } } try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad..]); } } pub fn formatBytes( value: var, options: FormatOptions, comptime radix: usize, out_stream: var, ) !void { if (value == 0) { return out_stream.writeAll("0B"); } const mags_si = " kMGTPEZY"; const mags_iec = " KMGTPEZY"; const magnitude = switch (radix) { 1000 => math.min(math.log2(value) / comptime math.log2(1000), mags_si.len - 1), 1024 => math.min(math.log2(value) / 10, mags_iec.len - 1), else => unreachable, }; const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude)); const suffix = switch (radix) { 1000 => mags_si[magnitude], 1024 => mags_iec[magnitude], else => unreachable, }; try formatFloatDecimal(new_value, options, out_stream); if (suffix == ' ') { return out_stream.writeAll("B"); } const buf = switch (radix) { 1000 => &[_]u8{ suffix, 'B' }, 1024 => &[_]u8{ suffix, 'i', 'B' }, else => unreachable, }; return out_stream.writeAll(buf); } pub fn formatInt( value: var, base: u8, uppercase: bool, options: FormatOptions, out_stream: var, ) !void { const int_value = if (@TypeOf(value) == comptime_int) blk: { const Int = math.IntFittingRange(value, value); break :blk @as(Int, value); } else value; if (@TypeOf(int_value).is_signed) { return formatIntSigned(int_value, base, uppercase, options, out_stream); } else { return formatIntUnsigned(int_value, base, uppercase, options, out_stream); } } fn formatIntSigned( value: var, base: u8, uppercase: bool, options: FormatOptions, out_stream: var, ) !void { const new_options = FormatOptions{ .width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null, .precision = options.precision, .fill = options.fill, }; const bit_count = @typeInfo(@TypeOf(value)).Int.bits; const Uint = std.meta.IntType(false, bit_count); if (value < 0) { try out_stream.writeAll("-"); const new_value = math.absCast(value); return formatIntUnsigned(new_value, base, uppercase, new_options, out_stream); } else if (options.width == null or options.width.? == 0) { return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, out_stream); } else { try out_stream.writeAll("+"); const new_value = @intCast(Uint, value); return formatIntUnsigned(new_value, base, uppercase, new_options, out_stream); } } fn formatIntUnsigned( value: var, base: u8, uppercase: bool, options: FormatOptions, out_stream: var, ) !void { assert(base >= 2); var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined; const min_int_bits = comptime math.max(@TypeOf(value).bit_count, @TypeOf(base).bit_count); const MinInt = std.meta.IntType(@TypeOf(value).is_signed, min_int_bits); var a: MinInt = value; var index: usize = buf.len; while (true) { const digit = a % base; index -= 1; buf[index] = digitToChar(@intCast(u8, digit), uppercase); a /= base; if (a == 0) break; } const digits_buf = buf[index..]; const width = options.width orelse 0; const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0; if (padding > index) { const zero_byte: u8 = options.fill; var leftover_padding = padding - index; while (true) { try out_stream.writeAll(@as(*const [1]u8, &zero_byte)[0..]); leftover_padding -= 1; if (leftover_padding == 0) break; } mem.set(u8, buf[0..index], options.fill); return out_stream.writeAll(&buf); } else { const padded_buf = buf[index - padding ..]; mem.set(u8, padded_buf[0..padding], options.fill); return out_stream.writeAll(padded_buf); } } pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) usize { var fbs = std.io.fixedBufferStream(out_buf); formatInt(value, base, uppercase, options, fbs.outStream()) catch unreachable; return fbs.pos; } pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T { if (!T.is_signed) return parseUnsigned(T, buf, radix); if (buf.len == 0) return @as(T, 0); if (buf[0] == '-') { return math.negate(try parseUnsigned(T, buf[1..], radix)); } else if (buf[0] == '+') { return parseUnsigned(T, buf[1..], radix); } else { return parseUnsigned(T, buf, radix); } } test "parseInt" { std.testing.expect((parseInt(i32, "-10", 10) catch unreachable) == -10); std.testing.expect((parseInt(i32, "+10", 10) catch unreachable) == 10); std.testing.expect(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter); std.testing.expect(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter); std.testing.expect(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter); std.testing.expect((parseInt(u8, "255", 10) catch unreachable) == 255); std.testing.expect(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow); } pub const ParseUnsignedError = error{ /// The result cannot fit in the type specified Overflow, /// The input had a byte that was not a digit InvalidCharacter, }; pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseUnsignedError!T { var x: T = 0; for (buf) |c| { const digit = try charToDigit(c, radix); if (x != 0) x = try math.mul(T, x, try math.cast(T, radix)); x = try math.add(T, x, try math.cast(T, digit)); } return x; } test "parseUnsigned" { std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124); std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535); std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10)); std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff); std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16)); std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF); std.testing.expect((try parseUnsigned(u7, "1", 10)) == 1); std.testing.expect((try parseUnsigned(u7, "1000", 2)) == 8); std.testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10)); std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8)); std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747); // these numbers should fit even though the radix itself doesn't fit in the destination type std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0); std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1); std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10)); std.testing.expect((try parseUnsigned(u1, "001", 16)) == 1); std.testing.expect((try parseUnsigned(u2, "3", 16)) == 3); std.testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16)); } pub const parseFloat = @import("fmt/parse_float.zig").parseFloat; test "parseFloat" { _ = @import("fmt/parse_float.zig"); } pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { const value = switch (c) { '0'...'9' => c - '0', 'A'...'Z' => c - 'A' + 10, 'a'...'z' => c - 'a' + 10, else => return error.InvalidCharacter, }; if (value >= radix) return error.InvalidCharacter; return value; } fn digitToChar(digit: u8, uppercase: bool) u8 { return switch (digit) { 0...9 => digit + '0', 10...35 => digit + ((if (uppercase) @as(u8, 'A') else @as(u8, 'a')) - 10), else => unreachable, }; } pub const BufPrintError = error{ /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes. NoSpaceLeft, }; pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 { var fbs = std.io.fixedBufferStream(buf); try format(fbs.outStream(), fmt, args); return fbs.getWritten(); } // Count the characters needed for format. Useful for preallocating memory pub fn count(comptime fmt: []const u8, args: var) u64 { var counting_stream = std.io.countingOutStream(std.io.null_out_stream); format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {}; return counting_stream.bytes_written; } pub const AllocPrintError = error{OutOfMemory}; pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: var) AllocPrintError![]u8 { const size = math.cast(usize, count(fmt, args)) catch |err| switch (err) { // Output too long. Can't possibly allocate enough memory to display it. error.Overflow => return error.OutOfMemory, }; const buf = try allocator.alloc(u8, size); return bufPrint(buf, fmt, args) catch |err| switch (err) { error.NoSpaceLeft => unreachable, // we just counted the size above }; } pub fn allocPrint0(allocator: *mem.Allocator, comptime fmt: []const u8, args: var) AllocPrintError![:0]u8 { const result = try allocPrint(allocator, fmt ++ "\x00", args); return result[0 .. result.len - 1 :0]; } test "bufPrintInt" { var buffer: [100]u8 = undefined; const buf = buffer[0..]; std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, false, FormatOptions{})); std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{})); std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{})); std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{})); std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{})); std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{})); std.testing.expectEqualSlices(u8, " 666", bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 })); std.testing.expectEqualSlices(u8, " 1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 })); std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 })); std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 })); std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 })); } fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) []u8 { return buf[0..formatIntBuf(buf, value, base, uppercase, options)]; } test "parse u64 digit too big" { _ = parseUnsigned(u64, "123a", 10) catch |err| { if (err == error.InvalidCharacter) return; unreachable; }; unreachable; } test "parse unsigned comptime" { comptime { std.testing.expect((try parseUnsigned(usize, "2", 10)) == 2); } } test "optional" { { const value: ?i32 = 1234; try testFmt("optional: 1234\n", "optional: {}\n", .{value}); } { const value: ?i32 = null; try testFmt("optional: null\n", "optional: {}\n", .{value}); } } test "error" { { const value: anyerror!i32 = 1234; try testFmt("error union: 1234\n", "error union: {}\n", .{value}); } { const value: anyerror!i32 = error.InvalidChar; try testFmt("error union: error.InvalidChar\n", "error union: {}\n", .{value}); } } test "int.small" { { const value: u3 = 0b101; try testFmt("u3: 5\n", "u3: {}\n", .{value}); } } test "int.specifier" { { const value: u8 = 'a'; try testFmt("u8: a\n", "u8: {c}\n", .{value}); } { const value: u8 = 0b1100; try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", .{value}); } } test "int.padded" { try testFmt("u8: ' 1'", "u8: '{:4}'", .{@as(u8, 1)}); try testFmt("u8: 'xxx1'", "u8: '{:x<4}'", .{@as(u8, 1)}); } test "buffer" { { var buf1: [32]u8 = undefined; var fbs = std.io.fixedBufferStream(&buf1); try formatType(1234, "", FormatOptions{}, fbs.outStream(), default_max_depth); std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234")); fbs.reset(); try formatType('a', "c", FormatOptions{}, fbs.outStream(), default_max_depth); std.testing.expect(mem.eql(u8, fbs.getWritten(), "a")); fbs.reset(); try formatType(0b1100, "b", FormatOptions{}, fbs.outStream(), default_max_depth); std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100")); } } test "array" { { const value: [3]u8 = "abc".*; try testFmt("array: abc\n", "array: {}\n", .{value}); try testFmt("array: abc\n", "array: {}\n", .{&value}); var buf: [100]u8 = undefined; try testFmt( try bufPrint(buf[0..], "array: [3]u8@{x}\n", .{@ptrToInt(&value)}), "array: {*}\n", .{&value}, ); } } test "slice" { { const value: []const u8 = "abc"; try testFmt("slice: abc\n", "slice: {}\n", .{value}); } { var runtime_zero: usize = 0; const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero]; try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value}); } try testFmt("buf: Test \n", "buf: {s:5}\n", .{"Test"}); try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"}); } test "pointer" { { const value = @intToPtr(*align(1) i32, 0xdeadbeef); try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", .{value}); try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", .{value}); } { const value = @intToPtr(fn () void, 0xdeadbeef); try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", .{value}); } { const value = @intToPtr(fn () void, 0xdeadbeef); try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", .{value}); } } test "cstr" { try testFmt( "cstr: Test C\n", "cstr: {s}\n", .{@ptrCast([*c]const u8, "Test C")}, ); try testFmt( "cstr: Test C \n", "cstr: {s:10}\n", .{@ptrCast([*c]const u8, "Test C")}, ); } test "filesize" { try testFmt("file size: 63MiB\n", "file size: {Bi}\n", .{@as(usize, 63 * 1024 * 1024)}); try testFmt("file size: 66.06MB\n", "file size: {B:.2}\n", .{@as(usize, 63 * 1024 * 1024)}); } test "struct" { { const Struct = struct { field: u8, }; const value = Struct{ .field = 42 }; try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{value}); try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{&value}); } { const Struct = struct { a: u0, b: u1, }; const value = Struct{ .a = 0, .b = 1 }; try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value}); } } test "enum" { const Enum = enum { One, Two, }; const value = Enum.Two; try testFmt("enum: Enum.Two\n", "enum: {}\n", .{value}); try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value}); } test "non-exhaustive enum" { const Enum = enum(u16) { One = 0x000f, Two = 0xbeef, _, }; try testFmt("enum: Enum(15)\n", "enum: {}\n", .{Enum.One}); try testFmt("enum: Enum(48879)\n", "enum: {}\n", .{Enum.Two}); try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)}); try testFmt("enum: Enum(f)\n", "enum: {x}\n", .{Enum.One}); try testFmt("enum: Enum(beef)\n", "enum: {x}\n", .{Enum.Two}); try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)}); } test "float.scientific" { try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)}); try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)}); try testFmt("f64: -1.234e+11", "f64: {e}", .{@as(f64, -12.34e10)}); try testFmt("f64: 9.99996e-40", "f64: {e}", .{@as(f64, 9.999960e-40)}); } test "float.scientific.precision" { try testFmt("f64: 1.40971e-42", "f64: {e:.5}", .{@as(f64, 1.409706e-42)}); try testFmt("f64: 1.00000e-09", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 814313563)))}); try testFmt("f64: 7.81250e-03", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1006632960)))}); // libc rounds 1.000005e+05 to 1.00000e+05 but zig does 1.00001e+05. // In fact, libc doesn't round a lot of 5 cases up when one past the precision point. try testFmt("f64: 1.00001e+05", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1203982400)))}); } test "float.special" { try testFmt("f64: nan", "f64: {}", .{math.nan_f64}); // negative nan is not defined by IEE 754, // and ARM thus normalizes it to positive nan if (builtin.arch != builtin.Arch.arm) { try testFmt("f64: -nan", "f64: {}", .{-math.nan_f64}); } try testFmt("f64: inf", "f64: {}", .{math.inf_f64}); try testFmt("f64: -inf", "f64: {}", .{-math.inf_f64}); } test "float.decimal" { try testFmt("f64: 152314000000000000000000000000", "f64: {d}", .{@as(f64, 1.52314e+29)}); try testFmt("f32: 0", "f32: {d}", .{@as(f32, 0.0)}); try testFmt("f32: 1.1", "f32: {d:.1}", .{@as(f32, 1.1234)}); try testFmt("f32: 1234.57", "f32: {d:.2}", .{@as(f32, 1234.567)}); // -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64). // -11.12339... is rounded back up to -11.1234 try testFmt("f32: -11.1234", "f32: {d:.4}", .{@as(f32, -11.1234)}); try testFmt("f32: 91.12345", "f32: {d:.5}", .{@as(f32, 91.12345)}); try testFmt("f64: 91.1234567890", "f64: {d:.10}", .{@as(f64, 91.12345678901235)}); try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 0.0)}); try testFmt("f64: 6", "f64: {d:.0}", .{@as(f64, 5.700)}); try testFmt("f64: 10.0", "f64: {d:.1}", .{@as(f64, 9.999)}); try testFmt("f64: 1.000", "f64: {d:.3}", .{@as(f64, 1.0)}); try testFmt("f64: 0.00030000", "f64: {d:.8}", .{@as(f64, 0.0003)}); try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 1.40130e-45)}); try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 9.999960e-40)}); } test "float.libc.sanity" { try testFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 916964781)))}); try testFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 925353389)))}); try testFmt("f64: 0.10000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1036831278)))}); try testFmt("f64: 1.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1065353133)))}); try testFmt("f64: 10.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1092616192)))}); // libc differences // // This is 0.015625 exactly according to gdb. We thus round down, // however glibc rounds up for some reason. This occurs for all // floats of the form x.yyyy25 on a precision point. try testFmt("f64: 0.01563", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1015021568)))}); // errol3 rounds to ... 630 but libc rounds to ...632. Grisu3 // also rounds to 630 so I'm inclined to believe libc is not // optimal here. try testFmt("f64: 18014400656965630.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1518338049)))}); } test "custom" { const Vec2 = struct { const SelfType = @This(); x: f32, y: f32, pub fn format( self: SelfType, comptime fmt: []const u8, options: FormatOptions, out_stream: var, ) !void { if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) { return std.fmt.format(out_stream, "({d:.3},{d:.3})", .{ self.x, self.y }); } else if (comptime std.mem.eql(u8, fmt, "d")) { return std.fmt.format(out_stream, "{d:.3}x{d:.3}", .{ self.x, self.y }); } else { @compileError("Unknown format character: '" ++ fmt ++ "'"); } } }; var buf1: [32]u8 = undefined; var value = Vec2{ .x = 10.2, .y = 2.22, }; try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{&value}); try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{&value}); // same thing but not passing a pointer try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{value}); try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{value}); } test "struct" { const S = struct { a: u32, b: anyerror, }; const inst = S{ .a = 456, .b = error.Unused, }; try testFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst}); } test "union" { const TU = union(enum) { float: f32, int: u32, }; const UU = union { float: f32, int: u32, }; const EU = extern union { float: f32, int: u32, }; const tu_inst = TU{ .int = 123 }; const uu_inst = UU{ .int = 456 }; const eu_inst = EU{ .float = 321.123 }; try testFmt("TU{ .int = 123 }", "{}", .{tu_inst}); var buf: [100]u8 = undefined; const uu_result = try bufPrint(buf[0..], "{}", .{uu_inst}); std.testing.expect(mem.eql(u8, uu_result[0..3], "UU@")); const eu_result = try bufPrint(buf[0..], "{}", .{eu_inst}); std.testing.expect(mem.eql(u8, uu_result[0..3], "EU@")); } test "enum" { const E = enum { One, Two, Three, }; const inst = E.Two; try testFmt("E.Two", "{}", .{inst}); } test "struct.self-referential" { const S = struct { const SelfType = @This(); a: ?*SelfType, }; var inst = S{ .a = null, }; inst.a = &inst; try testFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", .{inst}); } test "struct.zero-size" { const A = struct { fn foo() void {} }; const B = struct { a: A, c: i32, }; const a = A{}; const b = B{ .a = a, .c = 0 }; try testFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b}); } test "bytes.hex" { const some_bytes = "\xCA\xFE\xBA\xBE"; try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes}); try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes}); //Test Slices try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]}); try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]}); const bytes_with_zeros = "\x00\x0E\xBA\xBE"; try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros}); } fn testFmt(expected: []const u8, comptime template: []const u8, args: var) !void { var buf: [100]u8 = undefined; const result = try bufPrint(buf[0..], template, args); if (mem.eql(u8, result, expected)) return; std.debug.warn("\n====== expected this output: =========\n", .{}); std.debug.warn("{}", .{expected}); std.debug.warn("\n======== instead found this: =========\n", .{}); std.debug.warn("{}", .{result}); std.debug.warn("\n======================================\n", .{}); return error.TestFailed; } pub fn trim(buf: []const u8) []const u8 { var start: usize = 0; while (start < buf.len and isWhiteSpace(buf[start])) : (start += 1) {} var end: usize = buf.len; while (true) { if (end > start) { const new_end = end - 1; if (isWhiteSpace(buf[new_end])) { end = new_end; continue; } } break; } return buf[start..end]; } test "trim" { std.testing.expect(mem.eql(u8, "abc", trim("\n abc \t"))); std.testing.expect(mem.eql(u8, "", trim(" "))); std.testing.expect(mem.eql(u8, "", trim(""))); std.testing.expect(mem.eql(u8, "abc", trim(" abc"))); std.testing.expect(mem.eql(u8, "abc", trim("abc "))); } pub fn isWhiteSpace(byte: u8) bool { return switch (byte) { ' ', '\t', '\n', '\r' => true, else => false, }; } pub fn hexToBytes(out: []u8, input: []const u8) !void { if (out.len * 2 < input.len) return error.InvalidLength; var in_i: usize = 0; while (in_i != input.len) : (in_i += 2) { const hi = try charToDigit(input[in_i], 16); const lo = try charToDigit(input[in_i + 1], 16); out[in_i / 2] = (hi << 4) | lo; } } test "hexToBytes" { const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706"; var pb: [32]u8 = undefined; try hexToBytes(pb[0..], test_hex_str); try testFmt(test_hex_str, "{X}", .{pb}); } test "formatIntValue with comptime_int" { const value: comptime_int = 123456789123456789; var buf: [20]u8 = undefined; var fbs = std.io.fixedBufferStream(&buf); try formatIntValue(value, "", FormatOptions{}, fbs.outStream()); std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789")); } test "formatType max_depth" { const Vec2 = struct { const SelfType = @This(); x: f32, y: f32, pub fn format( self: SelfType, comptime fmt: []const u8, options: FormatOptions, out_stream: var, ) !void { if (fmt.len == 0) { return std.fmt.format(out_stream, "({d:.3},{d:.3})", .{ self.x, self.y }); } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } } }; const E = enum { One, Two, Three, }; const TU = union(enum) { const SelfType = @This(); float: f32, int: u32, ptr: ?*SelfType, }; const S = struct { const SelfType = @This(); a: ?*SelfType, tu: TU, e: E, vec: Vec2, }; var inst = S{ .a = null, .tu = TU{ .ptr = null }, .e = E.Two, .vec = Vec2{ .x = 10.2, .y = 2.22 }, }; inst.a = &inst; inst.tu.ptr = &inst.tu; var buf: [1000]u8 = undefined; var fbs = std.io.fixedBufferStream(&buf); try formatType(inst, "", FormatOptions{}, fbs.outStream(), 0); std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ ... }")); fbs.reset(); try formatType(inst, "", FormatOptions{}, fbs.outStream(), 1); std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }")); fbs.reset(); try formatType(inst, "", FormatOptions{}, fbs.outStream(), 2); std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }")); fbs.reset(); try formatType(inst, "", FormatOptions{}, fbs.outStream(), 3); std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }")); } test "positional" { try testFmt("2 1 0", "{2} {1} {0}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) }); try testFmt("2 1 0", "{2} {1} {}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) }); try testFmt("0 0", "{0} {0}", .{@as(usize, 0)}); try testFmt("0 1", "{} {1}", .{ @as(usize, 0), @as(usize, 1) }); try testFmt("1 0 0 1", "{1} {} {0} {}", .{ @as(usize, 0), @as(usize, 1) }); } test "positional with specifier" { try testFmt("10.0", "{0d:.1}", .{@as(f64, 9.999)}); } test "positional/alignment/width/precision" { try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)}); } test "vector" { if (builtin.arch == .mipsel) { // https://github.com/ziglang/zig/issues/3317 return error.SkipZigTest; } if (builtin.arch == .riscv64) { // https://github.com/ziglang/zig/issues/4486 return error.SkipZigTest; } const vbool: @Vector(4, bool) = [_]bool{ true, false, true, false }; const vi64: @Vector(4, i64) = [_]i64{ -2, -1, 0, 1 }; const vu64: @Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 }; try testFmt("{ true, false, true, false }", "{}", .{vbool}); try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64}); try testFmt("{ - 2, - 1, + 0, + 1 }", "{d:5}", .{vi64}); try testFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64}); try testFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64}); try testFmt("{ 1kB, 2kB, 3kB, 4kB }", "{B}", .{vu64}); try testFmt("{ 1000B, 1.953125KiB, 2.9296875KiB, 3.90625KiB }", "{Bi}", .{vu64}); } test "enum-literal" { try testFmt(".hello_world", "{}", .{.hello_world}); }
const std = @import("std"); const mem = std.mem; const Allocator = std.mem.Allocator; const fs = std.fs; const log = std.log.scoped(.link); const assert = std.debug.assert; const Compilation = @import("Compilation.zig"); const Module = @import("Module.zig"); const trace = @import("tracy.zig").trace; const Package = @import("Package.zig"); const Type = @import("type.zig").Type; const Cache = @import("Cache.zig"); const build_options = @import("build_options"); const LibCInstallation = @import("libc_installation.zig").LibCInstallation; pub const producer_string = if (std.builtin.is_test) "zig test" else "zig " ++ build_options.version; pub const Emit = struct { /// Where the output will go. directory: Compilation.Directory, /// Path to the output file, relative to `directory`. sub_path: []const u8, }; pub const Options = struct { /// This is `null` when -fno-emit-bin is used. When `openPath` or `flush` is called, /// it will have already been null-checked. emit: ?Emit, target: std.Target, output_mode: std.builtin.OutputMode, link_mode: std.builtin.LinkMode, object_format: std.builtin.ObjectFormat, optimize_mode: std.builtin.Mode, machine_code_model: std.builtin.CodeModel, root_name: []const u8, /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`. module: ?*Module, dynamic_linker: ?[]const u8, /// Used for calculating how much space to reserve for symbols in case the binary file /// does not already have a symbol table. symbol_count_hint: u64 = 32, /// Used for calculating how much space to reserve for executable program code in case /// the binary file does not already have such a section. program_code_size_hint: u64 = 256 * 1024, entry_addr: ?u64 = null, stack_size_override: ?u64, image_base_override: ?u64, include_compiler_rt: bool, /// Set to `true` to omit debug info. strip: bool, /// If this is true then this link code is responsible for outputting an object /// file and then using LLD to link it together with the link options and other objects. /// Otherwise (depending on `use_llvm`) this link code directly outputs and updates the final binary. use_lld: bool, /// If this is true then this link code is responsible for making an LLVM IR Module, /// outputting it to an object file, and then linking that together with link options and /// other objects. /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary. use_llvm: bool, /// Darwin-only. If this is true, `use_llvm` is true, and `is_native_os` is true, this link code will /// use system linker `ld` instead of the LLD. system_linker_hack: bool, link_libc: bool, link_libcpp: bool, function_sections: bool, eh_frame_hdr: bool, emit_relocs: bool, rdynamic: bool, z_nodelete: bool, z_defs: bool, bind_global_refs_locally: bool, is_native_os: bool, is_native_abi: bool, pic: bool, pie: bool, valgrind: bool, tsan: bool, stack_check: bool, single_threaded: bool, verbose_link: bool, dll_export_fns: bool, error_return_tracing: bool, skip_linker_dependencies: bool, parent_compilation_link_libc: bool, each_lib_rpath: bool, disable_lld_caching: bool, is_test: bool, gc_sections: ?bool = null, allow_shlib_undefined: ?bool, subsystem: ?std.Target.SubSystem, linker_script: ?[]const u8, version_script: ?[]const u8, soname: ?[]const u8, llvm_cpu_features: ?[*:0]const u8, /// Extra args passed directly to LLD. Ignored when not linking with LLD. extra_lld_args: []const []const u8, /// Darwin-only. Set the root path to the system libraries and frameworks. syslibroot: ?[]const u8, objects: []const []const u8, framework_dirs: []const []const u8, frameworks: []const []const u8, system_libs: std.StringArrayHashMapUnmanaged(void), lib_dirs: []const []const u8, rpath_list: []const []const u8, version: ?std.builtin.Version, libc_installation: ?*const LibCInstallation, pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode { return if (options.use_lld) .Obj else options.output_mode; } }; pub const File = struct { tag: Tag, options: Options, file: ?fs.File, allocator: *Allocator, /// When linking with LLD, this linker code will output an object file only at /// this location, and then this path can be placed on the LLD linker line. intermediary_basename: ?[]const u8 = null, /// Prevents other processes from clobbering files in the output directory /// of this linking operation. lock: ?Cache.Lock = null, pub const LinkBlock = union { elf: Elf.TextBlock, coff: Coff.TextBlock, macho: MachO.TextBlock, c: C.DeclBlock, wasm: void, }; pub const LinkFn = union { elf: Elf.SrcFn, coff: Coff.SrcFn, macho: MachO.SrcFn, c: C.FnBlock, wasm: ?Wasm.FnData, }; pub const Export = union { elf: Elf.Export, coff: void, macho: MachO.Export, c: void, wasm: void, }; /// For DWARF .debug_info. pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.hash, Type.eql, std.hash_map.DefaultMaxLoadPercentage); /// For DWARF .debug_info. pub const DbgInfoTypeReloc = struct { /// Offset from `TextBlock.dbg_info_off` (the buffer that is local to a Decl). /// This is where the .debug_info tag for the type is. off: u32, /// Offset from `TextBlock.dbg_info_off` (the buffer that is local to a Decl). /// List of DW.AT_type / DW.FORM_ref4 that points to the type. relocs: std.ArrayListUnmanaged(u32), }; /// Attempts incremental linking, if the file already exists. If /// incremental linking fails, falls back to truncating the file and /// rewriting it. A malicious file is detected as incremental link failure /// and does not cause Illegal Behavior. This operation is not atomic. pub fn openPath(allocator: *Allocator, options: Options) !*File { const use_stage1 = build_options.is_stage1 and options.use_llvm; if (use_stage1 or options.emit == null) { return switch (options.object_format) { .coff, .pe => &(try Coff.createEmpty(allocator, options)).base, .elf => &(try Elf.createEmpty(allocator, options)).base, .macho => &(try MachO.createEmpty(allocator, options)).base, .wasm => &(try Wasm.createEmpty(allocator, options)).base, .c => unreachable, // Reported error earlier. .hex => return error.HexObjectFormatUnimplemented, .raw => return error.RawObjectFormatUnimplemented, }; } const emit = options.emit.?; const use_lld = build_options.have_llvm and options.use_lld; // comptime known false when !have_llvm const sub_path = if (use_lld) blk: { if (options.module == null) { // No point in opening a file, we would not write anything to it. Initialize with empty. return switch (options.object_format) { .coff, .pe => &(try Coff.createEmpty(allocator, options)).base, .elf => &(try Elf.createEmpty(allocator, options)).base, .macho => &(try MachO.createEmpty(allocator, options)).base, .wasm => &(try Wasm.createEmpty(allocator, options)).base, .c => unreachable, // Reported error earlier. .hex => return error.HexObjectFormatUnimplemented, .raw => return error.RawObjectFormatUnimplemented, }; } // Open a temporary object file, not the final output file because we want to link with LLD. break :blk try std.fmt.allocPrint(allocator, "{s}{s}", .{ emit.sub_path, options.target.oFileExt() }); } else emit.sub_path; errdefer if (use_lld) allocator.free(sub_path); const file: *File = switch (options.object_format) { .coff, .pe => &(try Coff.openPath(allocator, sub_path, options)).base, .elf => &(try Elf.openPath(allocator, sub_path, options)).base, .macho => &(try MachO.openPath(allocator, sub_path, options)).base, .wasm => &(try Wasm.openPath(allocator, sub_path, options)).base, .c => &(try C.openPath(allocator, sub_path, options)).base, .hex => return error.HexObjectFormatUnimplemented, .raw => return error.RawObjectFormatUnimplemented, }; if (use_lld) { file.intermediary_basename = sub_path; } return file; } pub fn cast(base: *File, comptime T: type) ?*T { if (base.tag != T.base_tag) return null; return @fieldParentPtr(T, "base", base); } pub fn makeWritable(base: *File) !void { switch (base.tag) { .coff, .elf, .macho => { if (base.file != null) return; const emit = base.options.emit orelse return; base.file = try emit.directory.handle.createFile(emit.sub_path, .{ .truncate = false, .read = true, .mode = determineMode(base.options), }); }, .c, .wasm => {}, } } pub fn makeExecutable(base: *File) !void { switch (base.options.output_mode) { .Obj => return, .Lib => switch (base.options.link_mode) { .Static => return, .Dynamic => {}, }, .Exe => {}, } switch (base.tag) { .macho => if (base.file) |f| { if (base.intermediary_basename != null) { // The file we have open is not the final file that we want to // make executable, so we don't have to close it. return; } if (comptime std.Target.current.isDarwin() and std.Target.current.cpu.arch == .aarch64) { if (base.options.target.cpu.arch != .aarch64) return; // If we're not targeting aarch64, nothing to do. // XNU starting with Big Sur running on arm64 is caching inodes of running binaries. // Any change to the binary will effectively invalidate the kernel's cache // resulting in a SIGKILL on each subsequent run. Since when doing incremental // linking we're modifying a binary in-place, this will end up with the kernel // killing it on every subsequent run. To circumvent it, we will copy the file // into a new inode, remove the original file, and rename the copy to match // the original file. This is super messy, but there doesn't seem any other // way to please the XNU. const emit = base.options.emit orelse return; try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, emit.sub_path, .{}); } f.close(); base.file = null; }, .coff, .elf => if (base.file) |f| { if (base.intermediary_basename != null) { // The file we have open is not the final file that we want to // make executable, so we don't have to close it. return; } f.close(); base.file = null; }, .c, .wasm => {}, } } /// May be called before or after updateDeclExports but must be called /// after allocateDeclIndexes for any given Decl. pub fn updateDecl(base: *File, module: *Module, decl: *Module.Decl) !void { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).updateDecl(module, decl), .elf => return @fieldParentPtr(Elf, "base", base).updateDecl(module, decl), .macho => return @fieldParentPtr(MachO, "base", base).updateDecl(module, decl), .c => return @fieldParentPtr(C, "base", base).updateDecl(module, decl), .wasm => return @fieldParentPtr(Wasm, "base", base).updateDecl(module, decl), } } pub fn updateDeclLineNumber(base: *File, module: *Module, decl: *Module.Decl) !void { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).updateDeclLineNumber(module, decl), .elf => return @fieldParentPtr(Elf, "base", base).updateDeclLineNumber(module, decl), .macho => return @fieldParentPtr(MachO, "base", base).updateDeclLineNumber(module, decl), .c => return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl), .wasm => {}, } } /// Must be called before any call to updateDecl or updateDeclExports for /// any given Decl. pub fn allocateDeclIndexes(base: *File, decl: *Module.Decl) !void { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl), .elf => return @fieldParentPtr(Elf, "base", base).allocateDeclIndexes(decl), .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl), .c => return @fieldParentPtr(C, "base", base).allocateDeclIndexes(decl), .wasm => {}, } } pub fn releaseLock(self: *File) void { if (self.lock) |*lock| { lock.release(); self.lock = null; } } pub fn toOwnedLock(self: *File) Cache.Lock { const lock = self.lock.?; self.lock = null; return lock; } pub fn destroy(base: *File) void { base.releaseLock(); if (base.file) |f| f.close(); if (base.intermediary_basename) |sub_path| base.allocator.free(sub_path); switch (base.tag) { .coff => { const parent = @fieldParentPtr(Coff, "base", base); parent.deinit(); base.allocator.destroy(parent); }, .elf => { const parent = @fieldParentPtr(Elf, "base", base); parent.deinit(); base.allocator.destroy(parent); }, .macho => { const parent = @fieldParentPtr(MachO, "base", base); parent.deinit(); base.allocator.destroy(parent); }, .c => { const parent = @fieldParentPtr(C, "base", base); parent.deinit(); base.allocator.destroy(parent); }, .wasm => { const parent = @fieldParentPtr(Wasm, "base", base); parent.deinit(); base.allocator.destroy(parent); }, } } /// Commit pending changes and write headers. Takes into account final output mode /// and `use_lld`, not only `effectiveOutputMode`. pub fn flush(base: *File, comp: *Compilation) !void { const emit = base.options.emit orelse return; // -fno-emit-bin if (comp.clang_preprocessor_mode == .yes) { // TODO: avoid extra link step when it's just 1 object file (the `zig cc -c` case) // Until then, we do `lld -r -o output.o input.o` even though the output is the same // as the input. For the preprocessing case (`zig cc -E -o foo`) we copy the file // to the final location. See also the corresponding TODO in Coff linking. const full_out_path = try emit.directory.join(comp.gpa, &[_][]const u8{emit.sub_path}); defer comp.gpa.free(full_out_path); assert(comp.c_object_table.count() == 1); const the_entry = comp.c_object_table.items()[0]; const cached_pp_file_path = the_entry.key.status.success.object_path; try fs.cwd().copyFile(cached_pp_file_path, fs.cwd(), full_out_path, .{}); return; } const use_lld = build_options.have_llvm and base.options.use_lld; if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static and !base.options.target.isWasm()) { return base.linkAsArchive(comp); } switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).flush(comp), .elf => return @fieldParentPtr(Elf, "base", base).flush(comp), .macho => return @fieldParentPtr(MachO, "base", base).flush(comp), .c => return @fieldParentPtr(C, "base", base).flush(comp), .wasm => return @fieldParentPtr(Wasm, "base", base).flush(comp), } } /// Commit pending changes and write headers. Works based on `effectiveOutputMode` /// rather than final output mode. pub fn flushModule(base: *File, comp: *Compilation) !void { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).flushModule(comp), .elf => return @fieldParentPtr(Elf, "base", base).flushModule(comp), .macho => return @fieldParentPtr(MachO, "base", base).flushModule(comp), .c => return @fieldParentPtr(C, "base", base).flushModule(comp), .wasm => return @fieldParentPtr(Wasm, "base", base).flushModule(comp), } } /// Called when a Decl is deleted from the Module. pub fn freeDecl(base: *File, decl: *Module.Decl) void { switch (base.tag) { .coff => @fieldParentPtr(Coff, "base", base).freeDecl(decl), .elf => @fieldParentPtr(Elf, "base", base).freeDecl(decl), .macho => @fieldParentPtr(MachO, "base", base).freeDecl(decl), .c => @fieldParentPtr(C, "base", base).freeDecl(decl), .wasm => @fieldParentPtr(Wasm, "base", base).freeDecl(decl), } } pub fn errorFlags(base: *File) ErrorFlags { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).error_flags, .elf => return @fieldParentPtr(Elf, "base", base).error_flags, .macho => return @fieldParentPtr(MachO, "base", base).error_flags, .c => return .{ .no_entry_point_found = false }, .wasm => return ErrorFlags{}, } } /// May be called before or after updateDecl, but must be called after /// allocateDeclIndexes for any given Decl. pub fn updateDeclExports( base: *File, module: *Module, decl: *Module.Decl, exports: []const *Module.Export, ) !void { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).updateDeclExports(module, decl, exports), .elf => return @fieldParentPtr(Elf, "base", base).updateDeclExports(module, decl, exports), .macho => return @fieldParentPtr(MachO, "base", base).updateDeclExports(module, decl, exports), .c => return @fieldParentPtr(C, "base", base).updateDeclExports(module, decl, exports), .wasm => return @fieldParentPtr(Wasm, "base", base).updateDeclExports(module, decl, exports), } } pub fn getDeclVAddr(base: *File, decl: *const Module.Decl) u64 { switch (base.tag) { .coff => return @fieldParentPtr(Coff, "base", base).getDeclVAddr(decl), .elf => return @fieldParentPtr(Elf, "base", base).getDeclVAddr(decl), .macho => return @fieldParentPtr(MachO, "base", base).getDeclVAddr(decl), .c => unreachable, .wasm => unreachable, } } fn linkAsArchive(base: *File, comp: *Compilation) !void { const tracy = trace(@src()); defer tracy.end(); var arena_allocator = std.heap.ArenaAllocator.init(base.allocator); defer arena_allocator.deinit(); const arena = &arena_allocator.allocator; const directory = base.options.emit.?.directory; // Just an alias to make it shorter to type. // If there is no Zig code to compile, then we should skip flushing the output file because it // will not be part of the linker line anyway. const module_obj_path: ?[]const u8 = if (base.options.module) |module| blk: { const use_stage1 = build_options.is_stage1 and base.options.use_llvm; if (use_stage1) { const obj_basename = try std.zig.binNameAlloc(arena, .{ .root_name = base.options.root_name, .target = base.options.target, .output_mode = .Obj, }); const o_directory = base.options.module.?.zig_cache_artifact_directory; const full_obj_path = try o_directory.join(arena, &[_][]const u8{obj_basename}); break :blk full_obj_path; } try base.flushModule(comp); const obj_basename = base.intermediary_basename.?; const full_obj_path = try directory.join(arena, &[_][]const u8{obj_basename}); break :blk full_obj_path; } else null; const compiler_rt_path: ?[]const u8 = if (base.options.include_compiler_rt) comp.compiler_rt_obj.?.full_object_path else null; // This function follows the same pattern as link.Elf.linkWithLLD so if you want some // insight as to what's going on here you can read that function body which is more // well-commented. const id_symlink_basename = "llvm-ar.id"; var man: Cache.Manifest = undefined; defer if (!base.options.disable_lld_caching) man.deinit(); var digest: [Cache.hex_digest_len]u8 = undefined; if (!base.options.disable_lld_caching) { man = comp.cache_parent.obtain(); // We are about to obtain this lock, so here we give other processes a chance first. base.releaseLock(); try man.addListOfFiles(base.options.objects); for (comp.c_object_table.items()) |entry| { _ = try man.addFile(entry.key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); try man.addOptionalFile(compiler_rt_path); // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. _ = try man.hit(); digest = man.final(); var prev_digest_buf: [digest.len]u8 = undefined; const prev_digest: []u8 = Cache.readSmallFile( directory.handle, id_symlink_basename, &prev_digest_buf, ) catch |err| b: { log.debug("archive new_digest={} readFile error: {s}", .{ digest, @errorName(err) }); break :b prev_digest_buf[0..0]; }; if (mem.eql(u8, prev_digest, &digest)) { log.debug("archive digest={} match - skipping invocation", .{digest}); base.lock = man.toOwnedLock(); return; } // We are about to change the output file to be different, so we invalidate the build hash now. directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) { error.FileNotFound => {}, else => |e| return e, }; } var object_files = std.ArrayList([*:0]const u8).init(base.allocator); defer object_files.deinit(); try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len + 2); for (base.options.objects) |obj_path| { object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path)); } for (comp.c_object_table.items()) |entry| { object_files.appendAssumeCapacity(try arena.dupeZ(u8, entry.key.status.success.object_path)); } if (module_obj_path) |p| { object_files.appendAssumeCapacity(try arena.dupeZ(u8, p)); } if (compiler_rt_path) |p| { object_files.appendAssumeCapacity(try arena.dupeZ(u8, p)); } const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path}); const full_out_path_z = try arena.dupeZ(u8, full_out_path); if (base.options.verbose_link) { std.debug.print("ar rcs {s}", .{full_out_path_z}); for (object_files.items) |arg| { std.debug.print(" {s}", .{arg}); } std.debug.print("\n", .{}); } const llvm = @import("llvm_bindings.zig"); const os_type = @import("target.zig").osToLLVM(base.options.target.os.tag); const bad = llvm.WriteArchive(full_out_path_z, object_files.items.ptr, object_files.items.len, os_type); if (bad) return error.UnableToWriteArchive; if (!base.options.disable_lld_caching) { Cache.writeSmallFile(directory.handle, id_symlink_basename, &digest) catch |err| { log.warn("failed to save archive hash digest file: {s}", .{@errorName(err)}); }; man.writeManifest() catch |err| { log.warn("failed to write cache manifest when archiving: {s}", .{@errorName(err)}); }; base.lock = man.toOwnedLock(); } } pub const Tag = enum { coff, elf, macho, c, wasm, }; pub const ErrorFlags = struct { no_entry_point_found: bool = false, }; pub const C = @import("link/C.zig"); pub const Coff = @import("link/Coff.zig"); pub const Elf = @import("link/Elf.zig"); pub const MachO = @import("link/MachO.zig"); pub const Wasm = @import("link/Wasm.zig"); }; pub fn determineMode(options: Options) fs.File.Mode { // On common systems with a 0o022 umask, 0o777 will still result in a file created // with 0o755 permissions, but it works appropriately if the system is configured // more leniently. As another data point, C's fopen seems to open files with the // 666 mode. const executable_mode = if (std.Target.current.os.tag == .windows) 0 else 0o777; switch (options.effectiveOutputMode()) { .Lib => return switch (options.link_mode) { .Dynamic => executable_mode, .Static => fs.File.default_mode, }, .Exe => return executable_mode, .Obj => return fs.File.default_mode, } }
// // You can also make pointers to multiple items without using a slice. // // var foo: [4]u8 = [4]u8{ 1, 2, 3, 4 }; // var foo_slice: []u8 = foo[0..]; // var foo_ptr: [*]u8 = &foo; // // The difference between foo_slice and foo_ptr is that the slice has // a known length. The pointer doesn't. It is up to YOU to keep track // of the number of u8s foo_ptr points to! // const std = @import("std"); pub fn main() void { // Take a good look at the array type to which we're coercing // the zen12 string (the REAL nature of strings will be // revealed when we've learned some additional features): const zen12: *const [21]u8 = "Memory is a resource."; // // It would also have been valid to coerce to a slice: // const zen12: []const u8 = "..."; // // Now let's turn this into a "many-item pointer": const zen_manyptr: [*]const u8 = zen12; // It's okay to access zen_manyptr just like an array or slice as // long as you keep track of the length yourself! // // A "string" in Zig is a pointer to an array of const u8 values // or a slice of const u8 values, into one, as we saw above). So, // we could treat a "many-item pointer" of const u8 a string as long // as we can CONVERT IT TO A SLICE. (Hint: we do know the length!) // // Please fix this line so the print below statement can print it: const zen12_string: []const u8 = zen_manyptr; // Here's the moment of truth! std.debug.print("{s}\n", .{zen12_string}); } // // Are all of these pointer types starting to get confusing? // // FREE ZIG POINTER CHEATSHEET! (Using u8 as the example type.) // +---------------+----------------------------------------------+ // | u8 | one u8 | // | *u8 | pointer to one u8 | // | [2]u8 | two u8s | // | [*]u8 | pointer to unknown number of u8s | // | [2]const u8 | two immutable u8s | // | [*]const u8 | pointer to unknown number of immutable u8s | // | *[2]u8 | pointer to an array of 2 u8s | // | *const [2]u8 | pointer to an immutable array of 2 u8s | // | []u8 | slice of u8s | // | []const u8 | slice of immutable u8s | // +---------------+----------------------------------------------+
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2021 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const std = @import("../std.zig"); const builtin = @import("builtin"); const unicode = std.unicode; const mem = std.mem; const fs = std.fs; const os = std.os; pub const GetAppDataDirError = error{ OutOfMemory, AppDataDirUnavailable, }; /// Caller owns returned memory. /// TODO determine if we can remove the allocator requirement pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataDirError![]u8 { switch (builtin.os.tag) { .windows => { var dir_path_ptr: [*:0]u16 = undefined; switch (os.windows.shell32.SHGetKnownFolderPath( &os.windows.FOLDERID_LocalAppData, os.windows.KF_FLAG_CREATE, null, &dir_path_ptr, )) { os.windows.S_OK => { defer os.windows.ole32.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr)); const global_dir = unicode.utf16leToUtf8Alloc(allocator, mem.spanZ(dir_path_ptr)) catch |err| switch (err) { error.UnexpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, error.ExpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, error.DanglingSurrogateHalf => return error.AppDataDirUnavailable, error.OutOfMemory => return error.OutOfMemory, }; defer allocator.free(global_dir); return fs.path.join(allocator, &[_][]const u8{ global_dir, appname }); }, os.windows.E_OUTOFMEMORY => return error.OutOfMemory, else => return error.AppDataDirUnavailable, } }, .macos => { const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; }; return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname }); }, .linux, .freebsd, .netbsd, .dragonfly, .openbsd => { const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; }; return fs.path.join(allocator, &[_][]const u8{ home_dir, ".local", "share", appname }); }, .haiku => { var dir_path_ptr: [*:0]u8 = undefined; // TODO look into directory_which const be_user_settings = 0xbbe; const rc = os.system.find_directory(be_user_settings, -1, true, dir_path_ptr, 1) ; const settings_dir = try allocator.dupeZ(u8, mem.spanZ(dir_path_ptr)); defer allocator.free(settings_dir); switch (rc) { 0 => return fs.path.join(allocator, &[_][]const u8{ settings_dir, appname }), else => return error.AppDataDirUnavailable, } }, else => @compileError("Unsupported OS"), } } test "getAppDataDir" { if (builtin.os.tag == .wasi) return error.SkipZigTest; // We can't actually validate the result const dir = getAppDataDir(std.testing.allocator, "zig") catch return; defer std.testing.allocator.free(dir); }
//! NOTE: this file is autogenerated, DO NOT MODIFY //-------------------------------------------------------------------------------- // Section: Constants (1009) //-------------------------------------------------------------------------------- pub const IOCTL_STORAGE_BASE = @as(u32, 45); pub const IOCTL_SCMBUS_BASE = @as(u32, 89); pub const IOCTL_DISK_BASE = @as(u32, 7); pub const IOCTL_CHANGER_BASE = @as(u32, 48); pub const FILE_SPECIAL_ACCESS = @as(u32, 0); pub const FILE_DEVICE_UNKNOWN = @as(u32, 34); pub const GUID_DEVINTERFACE_DISK = Guid.initString("53f56307-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_CDROM = Guid.initString("53f56308-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_PARTITION = Guid.initString("53f5630a-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_TAPE = Guid.initString("53f5630b-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_WRITEONCEDISK = Guid.initString("53f5630c-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_VOLUME = Guid.initString("53f5630d-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_MEDIUMCHANGER = Guid.initString("53f56310-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_FLOPPY = Guid.initString("53f56311-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_CDCHANGER = Guid.initString("53f56312-b6bf-11d0-94f2-00a0c91efb8b"); pub const GUID_DEVINTERFACE_STORAGEPORT = Guid.initString("2accfe60-c130-11d2-b082-00a0c91efb8b"); pub const GUID_DEVINTERFACE_VMLUN = Guid.initString("6f416619-9f29-42a5-b20b-37e219ca02b0"); pub const GUID_DEVINTERFACE_SES = Guid.initString("1790c9ec-47d5-4df3-b5af-9adf3cf23e48"); pub const GUID_DEVINTERFACE_ZNSDISK = Guid.initString("b87941c5-ffdb-43c7-b6b1-20b632f0b109"); pub const GUID_DEVINTERFACE_SERVICE_VOLUME = Guid.initString("6ead3d82-25ec-46bc-b7fd-c1f0df8f5037"); pub const GUID_DEVINTERFACE_HIDDEN_VOLUME = Guid.initString("7f108a28-9833-4b3b-b780-2c6b5fa5c062"); pub const GUID_DEVINTERFACE_UNIFIED_ACCESS_RPMB = Guid.initString("27447c21-bcc3-4d07-a05b-a3395bb4eee7"); pub const GUID_DEVICEDUMP_STORAGE_DEVICE = Guid.initString("d8e2592f-1aab-4d56-a746-1f7585df40f4"); pub const GUID_DEVICEDUMP_DRIVER_STORAGE_PORT = Guid.initString("da82441d-7142-4bc1-b844-0807c5a4b67f"); pub const DEVPKEY_Storage_Portable = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 2 }; pub const DEVPKEY_Storage_Removable_Media = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 3 }; pub const DEVPKEY_Storage_System_Critical = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 4 }; pub const DEVPKEY_Storage_Disk_Number = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 5 }; pub const DEVPKEY_Storage_Partition_Number = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 6 }; pub const DEVPKEY_Storage_Mbr_Type = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 7 }; pub const DEVPKEY_Storage_Gpt_Type = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 8 }; pub const DEVPKEY_Storage_Gpt_Name = PROPERTYKEY { .fmtid = Guid.initString("4d1ebee8-0803-4774-9842-b77db50265e9"), .pid = 9 }; pub const IOCTL_STORAGE_CHECK_VERIFY = @as(u32, 2967552); pub const IOCTL_STORAGE_CHECK_VERIFY2 = @as(u32, 2951168); pub const IOCTL_STORAGE_MEDIA_REMOVAL = @as(u32, 2967556); pub const IOCTL_STORAGE_EJECT_MEDIA = @as(u32, 2967560); pub const IOCTL_STORAGE_LOAD_MEDIA = @as(u32, 2967564); pub const IOCTL_STORAGE_LOAD_MEDIA2 = @as(u32, 2951180); pub const IOCTL_STORAGE_RESERVE = @as(u32, 2967568); pub const IOCTL_STORAGE_RELEASE = @as(u32, 2967572); pub const IOCTL_STORAGE_FIND_NEW_DEVICES = @as(u32, 2967576); pub const IOCTL_STORAGE_MANAGE_BYPASS_IO = @as(u32, 2951360); pub const IOCTL_STORAGE_EJECTION_CONTROL = @as(u32, 2951488); pub const IOCTL_STORAGE_MCN_CONTROL = @as(u32, 2951492); pub const IOCTL_STORAGE_GET_MEDIA_TYPES = @as(u32, 2952192); pub const IOCTL_STORAGE_GET_MEDIA_TYPES_EX = @as(u32, 2952196); pub const IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER = @as(u32, 2952208); pub const IOCTL_STORAGE_GET_HOTPLUG_INFO = @as(u32, 2952212); pub const IOCTL_STORAGE_SET_HOTPLUG_INFO = @as(u32, 3001368); pub const IOCTL_STORAGE_RESET_BUS = @as(u32, 2969600); pub const IOCTL_STORAGE_RESET_DEVICE = @as(u32, 2969604); pub const IOCTL_STORAGE_BREAK_RESERVATION = @as(u32, 2969620); pub const IOCTL_STORAGE_PERSISTENT_RESERVE_IN = @as(u32, 2969624); pub const IOCTL_STORAGE_PERSISTENT_RESERVE_OUT = @as(u32, 3002396); pub const IOCTL_STORAGE_GET_DEVICE_NUMBER = @as(u32, 2953344); pub const IOCTL_STORAGE_GET_DEVICE_NUMBER_EX = @as(u32, 2953348); pub const IOCTL_STORAGE_PREDICT_FAILURE = @as(u32, 2953472); pub const IOCTL_STORAGE_FAILURE_PREDICTION_CONFIG = @as(u32, 2953476); pub const IOCTL_STORAGE_GET_COUNTERS = @as(u32, 2953480); pub const IOCTL_STORAGE_READ_CAPACITY = @as(u32, 2969920); pub const IOCTL_STORAGE_GET_DEVICE_TELEMETRY = @as(u32, 3002816); pub const IOCTL_STORAGE_DEVICE_TELEMETRY_NOTIFY = @as(u32, 3002820); pub const IOCTL_STORAGE_DEVICE_TELEMETRY_QUERY_CAPS = @as(u32, 3002824); pub const IOCTL_STORAGE_GET_DEVICE_TELEMETRY_RAW = @as(u32, 3002828); pub const IOCTL_STORAGE_SET_TEMPERATURE_THRESHOLD = @as(u32, 3002880); pub const IOCTL_STORAGE_PROTOCOL_COMMAND = @as(u32, 3003328); pub const IOCTL_STORAGE_SET_PROPERTY = @as(u32, 2987004); pub const IOCTL_STORAGE_QUERY_PROPERTY = @as(u32, 2954240); pub const IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES = @as(u32, 2987012); pub const IOCTL_STORAGE_GET_LB_PROVISIONING_MAP_RESOURCES = @as(u32, 2970632); pub const IOCTL_STORAGE_REINITIALIZE_MEDIA = @as(u32, 2987584); pub const IOCTL_STORAGE_GET_BC_PROPERTIES = @as(u32, 2971648); pub const IOCTL_STORAGE_ALLOCATE_BC_STREAM = @as(u32, 3004420); pub const IOCTL_STORAGE_FREE_BC_STREAM = @as(u32, 3004424); pub const IOCTL_STORAGE_CHECK_PRIORITY_HINT_SUPPORT = @as(u32, 2955392); pub const IOCTL_STORAGE_START_DATA_INTEGRITY_CHECK = @as(u32, 3004548); pub const IOCTL_STORAGE_STOP_DATA_INTEGRITY_CHECK = @as(u32, 3004552); pub const OBSOLETE_IOCTL_STORAGE_RESET_BUS = @as(u32, 3002368); pub const OBSOLETE_IOCTL_STORAGE_RESET_DEVICE = @as(u32, 3002372); pub const IOCTL_STORAGE_FIRMWARE_GET_INFO = @as(u32, 2956288); pub const IOCTL_STORAGE_FIRMWARE_DOWNLOAD = @as(u32, 3005444); pub const IOCTL_STORAGE_FIRMWARE_ACTIVATE = @as(u32, 3005448); pub const IOCTL_STORAGE_ENABLE_IDLE_POWER = @as(u32, 2956416); pub const IOCTL_STORAGE_GET_IDLE_POWERUP_REASON = @as(u32, 2956420); pub const IOCTL_STORAGE_POWER_ACTIVE = @as(u32, 2956424); pub const IOCTL_STORAGE_POWER_IDLE = @as(u32, 2956428); pub const IOCTL_STORAGE_EVENT_NOTIFICATION = @as(u32, 2956432); pub const IOCTL_STORAGE_DEVICE_POWER_CAP = @as(u32, 2956436); pub const IOCTL_STORAGE_RPMB_COMMAND = @as(u32, 2956440); pub const IOCTL_STORAGE_ATTRIBUTE_MANAGEMENT = @as(u32, 3005596); pub const IOCTL_STORAGE_DIAGNOSTIC = @as(u32, 2956448); pub const IOCTL_STORAGE_GET_PHYSICAL_ELEMENT_STATUS = @as(u32, 2956452); pub const IOCTL_STORAGE_REMOVE_ELEMENT_AND_TRUNCATE = @as(u32, 2956480); pub const IOCTL_STORAGE_GET_DEVICE_INTERNAL_LOG = @as(u32, 2956484); pub const STORAGE_DEVICE_FLAGS_RANDOM_DEVICEGUID_REASON_CONFLICT = @as(u32, 1); pub const STORAGE_DEVICE_FLAGS_RANDOM_DEVICEGUID_REASON_NOHWID = @as(u32, 2); pub const STORAGE_DEVICE_FLAGS_PAGE_83_DEVICEGUID = @as(u32, 4); pub const RECOVERED_WRITES_VALID = @as(u32, 1); pub const UNRECOVERED_WRITES_VALID = @as(u32, 2); pub const RECOVERED_READS_VALID = @as(u32, 4); pub const UNRECOVERED_READS_VALID = @as(u32, 8); pub const WRITE_COMPRESSION_INFO_VALID = @as(u32, 16); pub const READ_COMPRESSION_INFO_VALID = @as(u32, 32); pub const TAPE_RETURN_STATISTICS = @as(i32, 0); pub const TAPE_RETURN_ENV_INFO = @as(i32, 1); pub const TAPE_RESET_STATISTICS = @as(i32, 2); pub const MEDIA_ERASEABLE = @as(u32, 1); pub const MEDIA_WRITE_ONCE = @as(u32, 2); pub const MEDIA_READ_ONLY = @as(u32, 4); pub const MEDIA_READ_WRITE = @as(u32, 8); pub const MEDIA_WRITE_PROTECTED = @as(u32, 256); pub const MEDIA_CURRENTLY_MOUNTED = @as(u32, 2147483648); pub const STORAGE_FAILURE_PREDICTION_CONFIG_V1 = @as(u32, 1); pub const SRB_TYPE_SCSI_REQUEST_BLOCK = @as(u32, 0); pub const SRB_TYPE_STORAGE_REQUEST_BLOCK = @as(u32, 1); pub const STORAGE_ADDRESS_TYPE_BTL8 = @as(u32, 0); pub const STORAGE_RPMB_DESCRIPTOR_VERSION_1 = @as(u32, 1); pub const STORAGE_RPMB_MINIMUM_RELIABLE_WRITE_SIZE = @as(u32, 512); pub const STORAGE_CRYPTO_CAPABILITY_VERSION_1 = @as(u32, 1); pub const STORAGE_CRYPTO_DESCRIPTOR_VERSION_1 = @as(u32, 1); pub const STORAGE_TIER_NAME_LENGTH = @as(u32, 256); pub const STORAGE_TIER_DESCRIPTION_LENGTH = @as(u32, 512); pub const STORAGE_TIER_FLAG_NO_SEEK_PENALTY = @as(u32, 131072); pub const STORAGE_TIER_FLAG_WRITE_BACK_CACHE = @as(u32, 2097152); pub const STORAGE_TIER_FLAG_READ_CACHE = @as(u32, 4194304); pub const STORAGE_TIER_FLAG_PARITY = @as(u32, 8388608); pub const STORAGE_TIER_FLAG_SMR = @as(u32, 16777216); pub const STORAGE_TEMPERATURE_VALUE_NOT_REPORTED = @as(u32, 32768); pub const STORAGE_TEMPERATURE_THRESHOLD_FLAG_ADAPTER_REQUEST = @as(u32, 1); pub const STORAGE_COMPONENT_ROLE_CACHE = @as(u32, 1); pub const STORAGE_COMPONENT_ROLE_TIERING = @as(u32, 2); pub const STORAGE_COMPONENT_ROLE_DATA = @as(u32, 4); pub const STORAGE_ATTRIBUTE_BYTE_ADDRESSABLE_IO = @as(u32, 1); pub const STORAGE_ATTRIBUTE_BLOCK_IO = @as(u32, 2); pub const STORAGE_ATTRIBUTE_DYNAMIC_PERSISTENCE = @as(u32, 4); pub const STORAGE_ATTRIBUTE_VOLATILE = @as(u32, 8); pub const STORAGE_ATTRIBUTE_ASYNC_EVENT_NOTIFICATION = @as(u32, 16); pub const STORAGE_ATTRIBUTE_PERF_SIZE_INDEPENDENT = @as(u32, 32); pub const STORAGE_DEVICE_MAX_OPERATIONAL_STATUS = @as(u32, 16); pub const STORAGE_ADAPTER_SERIAL_NUMBER_V1_MAX_LENGTH = @as(u32, 128); pub const DeviceDsmActionFlag_NonDestructive = @as(u32, 2147483648); pub const DEVICE_DSM_FLAG_ENTIRE_DATA_SET_RANGE = @as(u32, 1); pub const DEVICE_DSM_FLAG_TRIM_NOT_FS_ALLOCATED = @as(u32, 2147483648); pub const DEVICE_DSM_FLAG_TRIM_BYPASS_RZAT = @as(u32, 1073741824); pub const DEVICE_DSM_NOTIFY_FLAG_BEGIN = @as(u32, 1); pub const DEVICE_DSM_NOTIFY_FLAG_END = @as(u32, 2); pub const STORAGE_OFFLOAD_MAX_TOKEN_LENGTH = @as(u32, 512); pub const STORAGE_OFFLOAD_TOKEN_ID_LENGTH = @as(u32, 504); pub const STORAGE_OFFLOAD_TOKEN_TYPE_ZERO_DATA = @as(u32, 4294901761); pub const STORAGE_OFFLOAD_READ_RANGE_TRUNCATED = @as(u32, 1); pub const STORAGE_OFFLOAD_WRITE_RANGE_TRUNCATED = @as(u32, 1); pub const STORAGE_OFFLOAD_TOKEN_INVALID = @as(u32, 2); pub const DEVICE_DSM_FLAG_ALLOCATION_CONSOLIDATEABLE_ONLY = @as(u32, 1073741824); pub const DEVICE_DSM_PARAMETERS_V1 = @as(u32, 1); pub const DEVICE_DATA_SET_LBP_STATE_PARAMETERS_VERSION_V1 = @as(u32, 1); pub const DEVICE_DSM_FLAG_REPAIR_INPUT_TOPOLOGY_ID_PRESENT = @as(u32, 1073741824); pub const DEVICE_DSM_FLAG_REPAIR_OUTPUT_PARITY_EXTENT = @as(u32, 536870912); pub const DEVICE_DSM_FLAG_SCRUB_SKIP_IN_SYNC = @as(u32, 268435456); pub const DEVICE_DSM_FLAG_SCRUB_OUTPUT_PARITY_EXTENT = @as(u32, 536870912); pub const DEVICE_DSM_FLAG_PHYSICAL_ADDRESSES_OMIT_TOTAL_RANGES = @as(u32, 268435456); pub const DEVICE_DSM_PHYSICAL_ADDRESSES_OUTPUT_V1 = @as(u32, 1); pub const DEVICE_DSM_PHYSICAL_ADDRESSES_OUTPUT_VERSION_V1 = @as(u32, 1); pub const DEVICE_STORAGE_NO_ERRORS = @as(u32, 1); pub const DEVICE_DSM_RANGE_ERROR_OUTPUT_V1 = @as(u32, 1); pub const DEVICE_DSM_RANGE_ERROR_INFO_VERSION_V1 = @as(u32, 1); pub const IOCTL_STORAGE_BC_VERSION = @as(u32, 1); pub const STORAGE_PRIORITY_HINT_SUPPORTED = @as(u32, 1); pub const STORAGE_DIAGNOSTIC_FLAG_ADAPTER_REQUEST = @as(u32, 1); pub const ERROR_HISTORY_DIRECTORY_ENTRY_DEFAULT_COUNT = @as(u32, 8); pub const DEVICEDUMP_STRUCTURE_VERSION_V1 = @as(u32, 1); pub const DEVICEDUMP_MAX_IDSTRING = @as(u32, 32); pub const MAX_FW_BUCKET_ID_LENGTH = @as(u32, 132); pub const DDUMP_FLAG_DATA_READ_FROM_DEVICE = @as(u32, 1); pub const FW_ISSUEID_NO_ISSUE = @as(u32, 0); pub const FW_ISSUEID_UNKNOWN = @as(u32, 4294967295); pub const TC_PUBLIC_DEVICEDUMP_CONTENT_SMART = @as(u32, 1); pub const TC_PUBLIC_DEVICEDUMP_CONTENT_GPLOG = @as(u32, 2); pub const TC_PUBLIC_DEVICEDUMP_CONTENT_GPLOG_MAX = @as(u32, 16); pub const TC_DEVICEDUMP_SUBSECTION_DESC_LENGTH = @as(u32, 16); pub const CDB_SIZE = @as(u32, 16); pub const TELEMETRY_COMMAND_SIZE = @as(u32, 16); pub const DEVICEDUMP_CAP_PRIVATE_SECTION = @as(u32, 1); pub const DEVICEDUMP_CAP_RESTRICTED_SECTION = @as(u32, 2); pub const STORAGE_IDLE_POWERUP_REASON_VERSION_V1 = @as(u32, 1); pub const STORAGE_DEVICE_POWER_CAP_VERSION_V1 = @as(u32, 1); pub const STORAGE_EVENT_NOTIFICATION_VERSION_V1 = @as(u32, 1); pub const STORAGE_EVENT_MEDIA_STATUS = @as(u64, 1); pub const STORAGE_EVENT_DEVICE_STATUS = @as(u64, 2); pub const STORAGE_EVENT_DEVICE_OPERATION = @as(u64, 4); pub const READ_COPY_NUMBER_KEY = @as(u32, 1380142592); pub const READ_COPY_NUMBER_BYPASS_CACHE_FLAG = @as(u32, 256); pub const STORAGE_HW_FIRMWARE_REQUEST_FLAG_CONTROLLER = @as(u32, 1); pub const STORAGE_HW_FIRMWARE_REQUEST_FLAG_LAST_SEGMENT = @as(u32, 2); pub const STORAGE_HW_FIRMWARE_REQUEST_FLAG_FIRST_SEGMENT = @as(u32, 4); pub const STORAGE_HW_FIRMWARE_REQUEST_FLAG_SWITCH_TO_EXISTING_FIRMWARE = @as(u32, 2147483648); pub const STORAGE_HW_FIRMWARE_INVALID_SLOT = @as(u32, 255); pub const STORAGE_HW_FIRMWARE_REVISION_LENGTH = @as(u32, 16); pub const STORAGE_PROTOCOL_STRUCTURE_VERSION = @as(u32, 1); pub const STORAGE_PROTOCOL_COMMAND_FLAG_ADAPTER_REQUEST = @as(u32, 2147483648); pub const STORAGE_PROTOCOL_STATUS_PENDING = @as(u32, 0); pub const STORAGE_PROTOCOL_STATUS_SUCCESS = @as(u32, 1); pub const STORAGE_PROTOCOL_STATUS_ERROR = @as(u32, 2); pub const STORAGE_PROTOCOL_STATUS_INVALID_REQUEST = @as(u32, 3); pub const STORAGE_PROTOCOL_STATUS_NO_DEVICE = @as(u32, 4); pub const STORAGE_PROTOCOL_STATUS_BUSY = @as(u32, 5); pub const STORAGE_PROTOCOL_STATUS_DATA_OVERRUN = @as(u32, 6); pub const STORAGE_PROTOCOL_STATUS_INSUFFICIENT_RESOURCES = @as(u32, 7); pub const STORAGE_PROTOCOL_STATUS_THROTTLED_REQUEST = @as(u32, 8); pub const STORAGE_PROTOCOL_STATUS_NOT_SUPPORTED = @as(u32, 255); pub const STORAGE_PROTOCOL_COMMAND_LENGTH_NVME = @as(u32, 64); pub const STORAGE_PROTOCOL_SPECIFIC_NVME_ADMIN_COMMAND = @as(u32, 1); pub const STORAGE_PROTOCOL_SPECIFIC_NVME_NVM_COMMAND = @as(u32, 2); pub const STORATTRIBUTE_NONE = @as(u32, 0); pub const STORATTRIBUTE_MANAGEMENT_STATE = @as(u32, 1); pub const STORAGE_SUPPORTED_FEATURES_BYPASS_IO = @as(u32, 1); pub const STORAGE_SUPPORTED_FEATURES_MASK = @as(u32, 1); pub const GUID_DEVINTERFACE_SCM_PHYSICAL_DEVICE = Guid.initString("4283609d-4dc2-43be-bbb4-4f15dfce2c61"); pub const GUID_SCM_PD_HEALTH_NOTIFICATION = Guid.initString("9da2d386-72f5-4ee3-8155-eca0678e3b06"); pub const GUID_SCM_PD_PASSTHROUGH_INVDIMM = Guid.initString("4309ac30-0d11-11e4-9191-0800200c9a66"); pub const GUID_DEVINTERFACE_COMPORT = Guid.initString("86e0d1e0-8089-11d0-9ce4-08003e301f73"); pub const GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR = Guid.initString("4d36e978-e325-11ce-bfc1-08002be10318"); pub const FILE_DEVICE_BEEP = @as(u32, 1); pub const FILE_DEVICE_CD_ROM_FILE_SYSTEM = @as(u32, 3); pub const FILE_DEVICE_CONTROLLER = @as(u32, 4); pub const FILE_DEVICE_DATALINK = @as(u32, 5); pub const FILE_DEVICE_DFS = @as(u32, 6); pub const FILE_DEVICE_DISK_FILE_SYSTEM = @as(u32, 8); pub const FILE_DEVICE_FILE_SYSTEM = @as(u32, 9); pub const FILE_DEVICE_INPORT_PORT = @as(u32, 10); pub const FILE_DEVICE_KEYBOARD = @as(u32, 11); pub const FILE_DEVICE_MAILSLOT = @as(u32, 12); pub const FILE_DEVICE_MIDI_IN = @as(u32, 13); pub const FILE_DEVICE_MIDI_OUT = @as(u32, 14); pub const FILE_DEVICE_MOUSE = @as(u32, 15); pub const FILE_DEVICE_MULTI_UNC_PROVIDER = @as(u32, 16); pub const FILE_DEVICE_NAMED_PIPE = @as(u32, 17); pub const FILE_DEVICE_NETWORK = @as(u32, 18); pub const FILE_DEVICE_NETWORK_BROWSER = @as(u32, 19); pub const FILE_DEVICE_NETWORK_FILE_SYSTEM = @as(u32, 20); pub const FILE_DEVICE_NULL = @as(u32, 21); pub const FILE_DEVICE_PARALLEL_PORT = @as(u32, 22); pub const FILE_DEVICE_PHYSICAL_NETCARD = @as(u32, 23); pub const FILE_DEVICE_PRINTER = @as(u32, 24); pub const FILE_DEVICE_SCANNER = @as(u32, 25); pub const FILE_DEVICE_SERIAL_MOUSE_PORT = @as(u32, 26); pub const FILE_DEVICE_SERIAL_PORT = @as(u32, 27); pub const FILE_DEVICE_SCREEN = @as(u32, 28); pub const FILE_DEVICE_SOUND = @as(u32, 29); pub const FILE_DEVICE_STREAMS = @as(u32, 30); pub const FILE_DEVICE_TAPE_FILE_SYSTEM = @as(u32, 32); pub const FILE_DEVICE_TRANSPORT = @as(u32, 33); pub const FILE_DEVICE_VIDEO = @as(u32, 35); pub const FILE_DEVICE_VIRTUAL_DISK = @as(u32, 36); pub const FILE_DEVICE_WAVE_IN = @as(u32, 37); pub const FILE_DEVICE_WAVE_OUT = @as(u32, 38); pub const FILE_DEVICE_8042_PORT = @as(u32, 39); pub const FILE_DEVICE_NETWORK_REDIRECTOR = @as(u32, 40); pub const FILE_DEVICE_BATTERY = @as(u32, 41); pub const FILE_DEVICE_BUS_EXTENDER = @as(u32, 42); pub const FILE_DEVICE_MODEM = @as(u32, 43); pub const FILE_DEVICE_VDM = @as(u32, 44); pub const FILE_DEVICE_MASS_STORAGE = @as(u32, 45); pub const FILE_DEVICE_SMB = @as(u32, 46); pub const FILE_DEVICE_KS = @as(u32, 47); pub const FILE_DEVICE_CHANGER = @as(u32, 48); pub const FILE_DEVICE_ACPI = @as(u32, 50); pub const FILE_DEVICE_FULLSCREEN_VIDEO = @as(u32, 52); pub const FILE_DEVICE_DFS_FILE_SYSTEM = @as(u32, 53); pub const FILE_DEVICE_DFS_VOLUME = @as(u32, 54); pub const FILE_DEVICE_SERENUM = @as(u32, 55); pub const FILE_DEVICE_TERMSRV = @as(u32, 56); pub const FILE_DEVICE_KSEC = @as(u32, 57); pub const FILE_DEVICE_FIPS = @as(u32, 58); pub const FILE_DEVICE_INFINIBAND = @as(u32, 59); pub const FILE_DEVICE_VMBUS = @as(u32, 62); pub const FILE_DEVICE_CRYPT_PROVIDER = @as(u32, 63); pub const FILE_DEVICE_WPD = @as(u32, 64); pub const FILE_DEVICE_BLUETOOTH = @as(u32, 65); pub const FILE_DEVICE_MT_COMPOSITE = @as(u32, 66); pub const FILE_DEVICE_MT_TRANSPORT = @as(u32, 67); pub const FILE_DEVICE_BIOMETRIC = @as(u32, 68); pub const FILE_DEVICE_PMI = @as(u32, 69); pub const FILE_DEVICE_EHSTOR = @as(u32, 70); pub const FILE_DEVICE_DEVAPI = @as(u32, 71); pub const FILE_DEVICE_GPIO = @as(u32, 72); pub const FILE_DEVICE_USBEX = @as(u32, 73); pub const FILE_DEVICE_CONSOLE = @as(u32, 80); pub const FILE_DEVICE_NFP = @as(u32, 81); pub const FILE_DEVICE_SYSENV = @as(u32, 82); pub const FILE_DEVICE_VIRTUAL_BLOCK = @as(u32, 83); pub const FILE_DEVICE_POINT_OF_SERVICE = @as(u32, 84); pub const FILE_DEVICE_STORAGE_REPLICATION = @as(u32, 85); pub const FILE_DEVICE_TRUST_ENV = @as(u32, 86); pub const FILE_DEVICE_UCM = @as(u32, 87); pub const FILE_DEVICE_UCMTCPCI = @as(u32, 88); pub const FILE_DEVICE_PERSISTENT_MEMORY = @as(u32, 89); pub const FILE_DEVICE_NVDIMM = @as(u32, 90); pub const FILE_DEVICE_HOLOGRAPHIC = @as(u32, 91); pub const FILE_DEVICE_SDFXHCI = @as(u32, 92); pub const FILE_DEVICE_UCMUCSI = @as(u32, 93); pub const FILE_DEVICE_PRM = @as(u32, 94); pub const FILE_DEVICE_EVENT_COLLECTOR = @as(u32, 95); pub const FILE_DEVICE_USB4 = @as(u32, 96); pub const FILE_DEVICE_SOUNDWIRE = @as(u32, 97); pub const METHOD_BUFFERED = @as(u32, 0); pub const METHOD_IN_DIRECT = @as(u32, 1); pub const METHOD_OUT_DIRECT = @as(u32, 2); pub const METHOD_NEITHER = @as(u32, 3); pub const METHOD_DIRECT_TO_HARDWARE = @as(u32, 1); pub const METHOD_DIRECT_FROM_HARDWARE = @as(u32, 2); pub const FILE_ANY_ACCESS = @as(u32, 0); pub const FILE_READ_ACCESS = @as(u32, 1); pub const FILE_WRITE_ACCESS = @as(u32, 2); pub const STORAGE_DEVICE_NUMA_NODE_UNKNOWN = @as(u32, 4294967295); pub const IOCTL_SCMBUS_DEVICE_FUNCTION_BASE = @as(u32, 0); pub const IOCTL_SCM_LOGICAL_DEVICE_FUNCTION_BASE = @as(u32, 768); pub const IOCTL_SCM_PHYSICAL_DEVICE_FUNCTION_BASE = @as(u32, 1536); pub const IOCTL_SCM_BUS_GET_LOGICAL_DEVICES = @as(u32, 5832704); pub const IOCTL_SCM_BUS_GET_PHYSICAL_DEVICES = @as(u32, 5832708); pub const IOCTL_SCM_BUS_GET_REGIONS = @as(u32, 5832712); pub const IOCTL_SCM_BUS_QUERY_PROPERTY = @as(u32, 5832716); pub const IOCTL_SCM_BUS_SET_PROPERTY = @as(u32, 5865492); pub const IOCTL_SCM_BUS_RUNTIME_FW_ACTIVATE = @as(u32, 5865488); pub const IOCTL_SCM_LD_GET_INTERLEAVE_SET = @as(u32, 5835776); pub const IOCTL_SCM_PD_QUERY_PROPERTY = @as(u32, 5838848); pub const IOCTL_SCM_PD_FIRMWARE_DOWNLOAD = @as(u32, 5871620); pub const IOCTL_SCM_PD_FIRMWARE_ACTIVATE = @as(u32, 5871624); pub const IOCTL_SCM_PD_PASSTHROUGH = @as(u32, 5888012); pub const IOCTL_SCM_PD_UPDATE_MANAGEMENT_STATUS = @as(u32, 5838864); pub const IOCTL_SCM_PD_REINITIALIZE_MEDIA = @as(u32, 5871636); pub const IOCTL_SCM_PD_SET_PROPERTY = @as(u32, 5871640); pub const SCM_MAX_SYMLINK_LEN_IN_CHARS = @as(u32, 256); pub const MAX_INTERFACE_CODES = @as(u32, 8); pub const SCM_PD_FIRMWARE_REVISION_LENGTH_BYTES = @as(u32, 32); pub const SCM_PD_PROPERTY_NAME_LENGTH_IN_CHARS = @as(u32, 128); pub const SCM_PD_MAX_OPERATIONAL_STATUS = @as(u32, 16); pub const SCM_PD_FIRMWARE_LAST_DOWNLOAD = @as(u32, 1); pub const IOCTL_DISK_GET_DRIVE_GEOMETRY = @as(u32, 458752); pub const IOCTL_DISK_GET_PARTITION_INFO = @as(u32, 475140); pub const IOCTL_DISK_SET_PARTITION_INFO = @as(u32, 507912); pub const IOCTL_DISK_GET_DRIVE_LAYOUT = @as(u32, 475148); pub const IOCTL_DISK_SET_DRIVE_LAYOUT = @as(u32, 507920); pub const IOCTL_DISK_VERIFY = @as(u32, 458772); pub const IOCTL_DISK_FORMAT_TRACKS = @as(u32, 507928); pub const IOCTL_DISK_REASSIGN_BLOCKS = @as(u32, 507932); pub const IOCTL_DISK_PERFORMANCE = @as(u32, 458784); pub const IOCTL_DISK_IS_WRITABLE = @as(u32, 458788); pub const IOCTL_DISK_LOGGING = @as(u32, 458792); pub const IOCTL_DISK_FORMAT_TRACKS_EX = @as(u32, 507948); pub const IOCTL_DISK_HISTOGRAM_STRUCTURE = @as(u32, 458800); pub const IOCTL_DISK_HISTOGRAM_DATA = @as(u32, 458804); pub const IOCTL_DISK_HISTOGRAM_RESET = @as(u32, 458808); pub const IOCTL_DISK_REQUEST_STRUCTURE = @as(u32, 458812); pub const IOCTL_DISK_REQUEST_DATA = @as(u32, 458816); pub const IOCTL_DISK_PERFORMANCE_OFF = @as(u32, 458848); pub const IOCTL_DISK_CONTROLLER_NUMBER = @as(u32, 458820); pub const SMART_GET_VERSION = @as(u32, 475264); pub const SMART_SEND_DRIVE_COMMAND = @as(u32, 508036); pub const SMART_RCV_DRIVE_DATA = @as(u32, 508040); pub const SMART_RCV_DRIVE_DATA_EX = @as(u32, 458892); pub const IOCTL_DISK_GET_PARTITION_INFO_EX = @as(u32, 458824); pub const IOCTL_DISK_SET_PARTITION_INFO_EX = @as(u32, 507980); pub const IOCTL_DISK_GET_DRIVE_LAYOUT_EX = @as(u32, 458832); pub const IOCTL_DISK_SET_DRIVE_LAYOUT_EX = @as(u32, 507988); pub const IOCTL_DISK_CREATE_DISK = @as(u32, 507992); pub const IOCTL_DISK_GET_LENGTH_INFO = @as(u32, 475228); pub const IOCTL_DISK_GET_DRIVE_GEOMETRY_EX = @as(u32, 458912); pub const IOCTL_DISK_REASSIGN_BLOCKS_EX = @as(u32, 508068); pub const IOCTL_DISK_UPDATE_DRIVE_SIZE = @as(u32, 508104); pub const IOCTL_DISK_GROW_PARTITION = @as(u32, 508112); pub const IOCTL_DISK_GET_CACHE_INFORMATION = @as(u32, 475348); pub const IOCTL_DISK_SET_CACHE_INFORMATION = @as(u32, 508120); pub const IOCTL_DISK_GET_WRITE_CACHE_STATE = @as(u32, 475356); pub const OBSOLETE_DISK_GET_WRITE_CACHE_STATE = @as(u32, 475356); pub const IOCTL_DISK_DELETE_DRIVE_LAYOUT = @as(u32, 508160); pub const IOCTL_DISK_UPDATE_PROPERTIES = @as(u32, 459072); pub const IOCTL_DISK_FORMAT_DRIVE = @as(u32, 508876); pub const IOCTL_DISK_SENSE_DEVICE = @as(u32, 459744); pub const IOCTL_DISK_CHECK_VERIFY = @as(u32, 477184); pub const IOCTL_DISK_MEDIA_REMOVAL = @as(u32, 477188); pub const IOCTL_DISK_EJECT_MEDIA = @as(u32, 477192); pub const IOCTL_DISK_LOAD_MEDIA = @as(u32, 477196); pub const IOCTL_DISK_RESERVE = @as(u32, 477200); pub const IOCTL_DISK_RELEASE = @as(u32, 477204); pub const IOCTL_DISK_FIND_NEW_DEVICES = @as(u32, 477208); pub const IOCTL_DISK_GET_MEDIA_TYPES = @as(u32, 461824); pub const PARTITION_ENTRY_UNUSED = @as(u32, 0); pub const PARTITION_FAT_12 = @as(u32, 1); pub const PARTITION_XENIX_1 = @as(u32, 2); pub const PARTITION_XENIX_2 = @as(u32, 3); pub const PARTITION_FAT_16 = @as(u32, 4); pub const PARTITION_EXTENDED = @as(u32, 5); pub const PARTITION_HUGE = @as(u32, 6); pub const PARTITION_IFS = @as(u32, 7); pub const PARTITION_OS2BOOTMGR = @as(u32, 10); pub const PARTITION_FAT32 = @as(u32, 11); pub const PARTITION_FAT32_XINT13 = @as(u32, 12); pub const PARTITION_XINT13 = @as(u32, 14); pub const PARTITION_XINT13_EXTENDED = @as(u32, 15); pub const PARTITION_MSFT_RECOVERY = @as(u32, 39); pub const PARTITION_MAIN_OS = @as(u32, 40); pub const PARTIITON_OS_DATA = @as(u32, 41); pub const PARTITION_PRE_INSTALLED = @as(u32, 42); pub const PARTITION_BSP = @as(u32, 43); pub const PARTITION_DPP = @as(u32, 44); pub const PARTITION_WINDOWS_SYSTEM = @as(u32, 45); pub const PARTITION_PREP = @as(u32, 65); pub const PARTITION_LDM = @as(u32, 66); pub const PARTITION_DM = @as(u32, 84); pub const PARTITION_EZDRIVE = @as(u32, 85); pub const PARTITION_UNIX = @as(u32, 99); pub const PARTITION_SPACES_DATA = @as(u32, 215); pub const PARTITION_SPACES = @as(u32, 231); pub const PARTITION_GPT = @as(u32, 238); pub const PARTITION_SYSTEM = @as(u32, 239); pub const VALID_NTFT = @as(u32, 192); pub const PARTITION_NTFT = @as(u32, 128); pub const GPT_ATTRIBUTE_NO_BLOCK_IO_PROTOCOL = @as(u64, 2); pub const GPT_ATTRIBUTE_LEGACY_BIOS_BOOTABLE = @as(u64, 4); pub const GPT_BASIC_DATA_ATTRIBUTE_OFFLINE = @as(u64, 576460752303423488); pub const GPT_BASIC_DATA_ATTRIBUTE_DAX = @as(u64, 288230376151711744); pub const GPT_BASIC_DATA_ATTRIBUTE_SERVICE = @as(u64, 144115188075855872); pub const GPT_SPACES_ATTRIBUTE_NO_METADATA = @as(u64, 9223372036854775808); pub const HIST_NO_OF_BUCKETS = @as(u32, 24); pub const DISK_LOGGING_START = @as(u32, 0); pub const DISK_LOGGING_STOP = @as(u32, 1); pub const DISK_LOGGING_DUMP = @as(u32, 2); pub const DISK_BINNING = @as(u32, 3); pub const CAP_ATA_ID_CMD = @as(u32, 1); pub const CAP_ATAPI_ID_CMD = @as(u32, 2); pub const CAP_SMART_CMD = @as(u32, 4); pub const ATAPI_ID_CMD = @as(u32, 161); pub const ID_CMD = @as(u32, 236); pub const SMART_CMD = @as(u32, 176); pub const SMART_CYL_LOW = @as(u32, 79); pub const SMART_CYL_HI = @as(u32, 194); pub const SMART_NO_ERROR = @as(u32, 0); pub const SMART_IDE_ERROR = @as(u32, 1); pub const SMART_INVALID_FLAG = @as(u32, 2); pub const SMART_INVALID_COMMAND = @as(u32, 3); pub const SMART_INVALID_BUFFER = @as(u32, 4); pub const SMART_INVALID_DRIVE = @as(u32, 5); pub const SMART_INVALID_IOCTL = @as(u32, 6); pub const SMART_ERROR_NO_MEM = @as(u32, 7); pub const SMART_INVALID_REGISTER = @as(u32, 8); pub const SMART_NOT_SUPPORTED = @as(u32, 9); pub const SMART_NO_IDE_DEVICE = @as(u32, 10); pub const SMART_OFFLINE_ROUTINE_OFFLINE = @as(u32, 0); pub const SMART_SHORT_SELFTEST_OFFLINE = @as(u32, 1); pub const SMART_EXTENDED_SELFTEST_OFFLINE = @as(u32, 2); pub const SMART_ABORT_OFFLINE_SELFTEST = @as(u32, 127); pub const SMART_SHORT_SELFTEST_CAPTIVE = @as(u32, 129); pub const SMART_EXTENDED_SELFTEST_CAPTIVE = @as(u32, 130); pub const READ_ATTRIBUTE_BUFFER_SIZE = @as(u32, 512); pub const IDENTIFY_BUFFER_SIZE = @as(u32, 512); pub const READ_THRESHOLD_BUFFER_SIZE = @as(u32, 512); pub const SMART_LOG_SECTOR_SIZE = @as(u32, 512); pub const READ_ATTRIBUTES = @as(u32, 208); pub const READ_THRESHOLDS = @as(u32, 209); pub const ENABLE_DISABLE_AUTOSAVE = @as(u32, 210); pub const SAVE_ATTRIBUTE_VALUES = @as(u32, 211); pub const EXECUTE_OFFLINE_DIAGS = @as(u32, 212); pub const SMART_READ_LOG = @as(u32, 213); pub const SMART_WRITE_LOG = @as(u32, 214); pub const ENABLE_SMART = @as(u32, 216); pub const DISABLE_SMART = @as(u32, 217); pub const RETURN_SMART_STATUS = @as(u32, 218); pub const ENABLE_DISABLE_AUTO_OFFLINE = @as(u32, 219); pub const IOCTL_DISK_GET_DISK_ATTRIBUTES = @as(u32, 458992); pub const IOCTL_DISK_SET_DISK_ATTRIBUTES = @as(u32, 508148); pub const DISK_ATTRIBUTE_OFFLINE = @as(u64, 1); pub const DISK_ATTRIBUTE_READ_ONLY = @as(u64, 2); pub const IOCTL_DISK_RESET_SNAPSHOT_INFO = @as(u32, 508432); pub const IOCTL_CHANGER_GET_PARAMETERS = @as(u32, 3162112); pub const IOCTL_CHANGER_GET_STATUS = @as(u32, 3162116); pub const IOCTL_CHANGER_GET_PRODUCT_DATA = @as(u32, 3162120); pub const IOCTL_CHANGER_SET_ACCESS = @as(u32, 3194896); pub const IOCTL_CHANGER_GET_ELEMENT_STATUS = @as(u32, 3194900); pub const IOCTL_CHANGER_INITIALIZE_ELEMENT_STATUS = @as(u32, 3162136); pub const IOCTL_CHANGER_SET_POSITION = @as(u32, 3162140); pub const IOCTL_CHANGER_EXCHANGE_MEDIUM = @as(u32, 3162144); pub const IOCTL_CHANGER_MOVE_MEDIUM = @as(u32, 3162148); pub const IOCTL_CHANGER_REINITIALIZE_TRANSPORT = @as(u32, 3162152); pub const IOCTL_CHANGER_QUERY_VOLUME_TAGS = @as(u32, 3194924); pub const MAX_VOLUME_ID_SIZE = @as(u32, 36); pub const MAX_VOLUME_TEMPLATE_SIZE = @as(u32, 40); pub const VENDOR_ID_LENGTH = @as(u32, 8); pub const PRODUCT_ID_LENGTH = @as(u32, 16); pub const REVISION_LENGTH = @as(u32, 4); pub const SERIAL_NUMBER_LENGTH = @as(u32, 32); pub const CHANGER_RESERVED_BIT = @as(u32, 2147483648); pub const CHANGER_TO_TRANSPORT = @as(u32, 1); pub const CHANGER_TO_SLOT = @as(u32, 2); pub const CHANGER_TO_IEPORT = @as(u32, 4); pub const CHANGER_TO_DRIVE = @as(u32, 8); pub const LOCK_UNLOCK_IEPORT = @as(u32, 1); pub const LOCK_UNLOCK_DOOR = @as(u32, 2); pub const LOCK_UNLOCK_KEYPAD = @as(u32, 4); pub const LOCK_ELEMENT = @as(u32, 0); pub const UNLOCK_ELEMENT = @as(u32, 1); pub const EXTEND_IEPORT = @as(u32, 2); pub const RETRACT_IEPORT = @as(u32, 3); pub const ERROR_LABEL_UNREADABLE = @as(u32, 1); pub const ERROR_LABEL_QUESTIONABLE = @as(u32, 2); pub const ERROR_SLOT_NOT_PRESENT = @as(u32, 4); pub const ERROR_DRIVE_NOT_INSTALLED = @as(u32, 8); pub const ERROR_TRAY_MALFUNCTION = @as(u32, 16); pub const ERROR_INIT_STATUS_NEEDED = @as(u32, 17); pub const ERROR_UNHANDLED_ERROR = @as(u32, 4294967295); pub const SEARCH_ALL = @as(u32, 0); pub const SEARCH_PRIMARY = @as(u32, 1); pub const SEARCH_ALTERNATE = @as(u32, 2); pub const SEARCH_ALL_NO_SEQ = @as(u32, 4); pub const SEARCH_PRI_NO_SEQ = @as(u32, 5); pub const SEARCH_ALT_NO_SEQ = @as(u32, 6); pub const ASSERT_PRIMARY = @as(u32, 8); pub const ASSERT_ALTERNATE = @as(u32, 9); pub const REPLACE_PRIMARY = @as(u32, 10); pub const REPLACE_ALTERNATE = @as(u32, 11); pub const UNDEFINE_PRIMARY = @as(u32, 12); pub const UNDEFINE_ALTERNATE = @as(u32, 13); pub const IOCTL_SERIAL_LSRMST_INSERT = @as(u32, 1769596); pub const IOCTL_SERENUM_EXPOSE_HARDWARE = @as(u32, 3604992); pub const IOCTL_SERENUM_REMOVE_HARDWARE = @as(u32, 3604996); pub const IOCTL_SERENUM_PORT_DESC = @as(u32, 3605000); pub const IOCTL_SERENUM_GET_PORT_NAME = @as(u32, 3605004); pub const FSCTL_REQUEST_OPLOCK_LEVEL_1 = @as(u32, 589824); pub const FSCTL_REQUEST_OPLOCK_LEVEL_2 = @as(u32, 589828); pub const FSCTL_REQUEST_BATCH_OPLOCK = @as(u32, 589832); pub const FSCTL_OPLOCK_BREAK_ACKNOWLEDGE = @as(u32, 589836); pub const FSCTL_OPBATCH_ACK_CLOSE_PENDING = @as(u32, 589840); pub const FSCTL_OPLOCK_BREAK_NOTIFY = @as(u32, 589844); pub const FSCTL_LOCK_VOLUME = @as(u32, 589848); pub const FSCTL_UNLOCK_VOLUME = @as(u32, 589852); pub const FSCTL_DISMOUNT_VOLUME = @as(u32, 589856); pub const FSCTL_IS_VOLUME_MOUNTED = @as(u32, 589864); pub const FSCTL_IS_PATHNAME_VALID = @as(u32, 589868); pub const FSCTL_MARK_VOLUME_DIRTY = @as(u32, 589872); pub const FSCTL_QUERY_RETRIEVAL_POINTERS = @as(u32, 589883); pub const FSCTL_GET_COMPRESSION = @as(u32, 589884); pub const FSCTL_SET_COMPRESSION = @as(u32, 639040); pub const FSCTL_SET_BOOTLOADER_ACCESSED = @as(u32, 589903); pub const FSCTL_MARK_AS_SYSTEM_HIVE = @as(u32, 589903); pub const FSCTL_OPLOCK_BREAK_ACK_NO_2 = @as(u32, 589904); pub const FSCTL_INVALIDATE_VOLUMES = @as(u32, 589908); pub const FSCTL_QUERY_FAT_BPB = @as(u32, 589912); pub const FSCTL_REQUEST_FILTER_OPLOCK = @as(u32, 589916); pub const FSCTL_FILESYSTEM_GET_STATISTICS = @as(u32, 589920); pub const FSCTL_GET_NTFS_VOLUME_DATA = @as(u32, 589924); pub const FSCTL_GET_NTFS_FILE_RECORD = @as(u32, 589928); pub const FSCTL_GET_VOLUME_BITMAP = @as(u32, 589935); pub const FSCTL_GET_RETRIEVAL_POINTERS = @as(u32, 589939); pub const FSCTL_MOVE_FILE = @as(u32, 589940); pub const FSCTL_IS_VOLUME_DIRTY = @as(u32, 589944); pub const FSCTL_ALLOW_EXTENDED_DASD_IO = @as(u32, 589955); pub const FSCTL_FIND_FILES_BY_SID = @as(u32, 589967); pub const FSCTL_SET_OBJECT_ID = @as(u32, 589976); pub const FSCTL_GET_OBJECT_ID = @as(u32, 589980); pub const FSCTL_DELETE_OBJECT_ID = @as(u32, 589984); pub const FSCTL_SET_REPARSE_POINT = @as(u32, 589988); pub const FSCTL_GET_REPARSE_POINT = @as(u32, 589992); pub const FSCTL_DELETE_REPARSE_POINT = @as(u32, 589996); pub const FSCTL_ENUM_USN_DATA = @as(u32, 590003); pub const FSCTL_SECURITY_ID_CHECK = @as(u32, 606391); pub const FSCTL_READ_USN_JOURNAL = @as(u32, 590011); pub const FSCTL_SET_OBJECT_ID_EXTENDED = @as(u32, 590012); pub const FSCTL_CREATE_OR_GET_OBJECT_ID = @as(u32, 590016); pub const FSCTL_SET_SPARSE = @as(u32, 590020); pub const FSCTL_SET_ZERO_DATA = @as(u32, 622792); pub const FSCTL_QUERY_ALLOCATED_RANGES = @as(u32, 606415); pub const FSCTL_ENABLE_UPGRADE = @as(u32, 622800); pub const FSCTL_SET_ENCRYPTION = @as(u32, 590039); pub const FSCTL_ENCRYPTION_FSCTL_IO = @as(u32, 590043); pub const FSCTL_WRITE_RAW_ENCRYPTED = @as(u32, 590047); pub const FSCTL_READ_RAW_ENCRYPTED = @as(u32, 590051); pub const FSCTL_CREATE_USN_JOURNAL = @as(u32, 590055); pub const FSCTL_READ_FILE_USN_DATA = @as(u32, 590059); pub const FSCTL_WRITE_USN_CLOSE_RECORD = @as(u32, 590063); pub const FSCTL_EXTEND_VOLUME = @as(u32, 590064); pub const FSCTL_QUERY_USN_JOURNAL = @as(u32, 590068); pub const FSCTL_DELETE_USN_JOURNAL = @as(u32, 590072); pub const FSCTL_MARK_HANDLE = @as(u32, 590076); pub const FSCTL_SIS_COPYFILE = @as(u32, 590080); pub const FSCTL_SIS_LINK_FILES = @as(u32, 639236); pub const FSCTL_RECALL_FILE = @as(u32, 590103); pub const FSCTL_READ_FROM_PLEX = @as(u32, 606494); pub const FSCTL_FILE_PREFETCH = @as(u32, 590112); pub const FSCTL_MAKE_MEDIA_COMPATIBLE = @as(u32, 622896); pub const FSCTL_SET_DEFECT_MANAGEMENT = @as(u32, 622900); pub const FSCTL_QUERY_SPARING_INFO = @as(u32, 590136); pub const FSCTL_QUERY_ON_DISK_VOLUME_INFO = @as(u32, 590140); pub const FSCTL_SET_VOLUME_COMPRESSION_STATE = @as(u32, 590144); pub const FSCTL_TXFS_MODIFY_RM = @as(u32, 622916); pub const FSCTL_TXFS_QUERY_RM_INFORMATION = @as(u32, 606536); pub const FSCTL_TXFS_ROLLFORWARD_REDO = @as(u32, 622928); pub const FSCTL_TXFS_ROLLFORWARD_UNDO = @as(u32, 622932); pub const FSCTL_TXFS_START_RM = @as(u32, 622936); pub const FSCTL_TXFS_SHUTDOWN_RM = @as(u32, 622940); pub const FSCTL_TXFS_READ_BACKUP_INFORMATION = @as(u32, 606560); pub const FSCTL_TXFS_WRITE_BACKUP_INFORMATION = @as(u32, 622948); pub const FSCTL_TXFS_CREATE_SECONDARY_RM = @as(u32, 622952); pub const FSCTL_TXFS_GET_METADATA_INFO = @as(u32, 606572); pub const FSCTL_TXFS_GET_TRANSACTED_VERSION = @as(u32, 606576); pub const FSCTL_TXFS_SAVEPOINT_INFORMATION = @as(u32, 622968); pub const FSCTL_TXFS_CREATE_MINIVERSION = @as(u32, 622972); pub const FSCTL_TXFS_TRANSACTION_ACTIVE = @as(u32, 606604); pub const FSCTL_SET_ZERO_ON_DEALLOCATION = @as(u32, 590228); pub const FSCTL_SET_REPAIR = @as(u32, 590232); pub const FSCTL_GET_REPAIR = @as(u32, 590236); pub const FSCTL_WAIT_FOR_REPAIR = @as(u32, 590240); pub const FSCTL_INITIATE_REPAIR = @as(u32, 590248); pub const FSCTL_CSC_INTERNAL = @as(u32, 590255); pub const FSCTL_SHRINK_VOLUME = @as(u32, 590256); pub const FSCTL_SET_SHORT_NAME_BEHAVIOR = @as(u32, 590260); pub const FSCTL_DFSR_SET_GHOST_HANDLE_STATE = @as(u32, 590264); pub const FSCTL_TXFS_LIST_TRANSACTION_LOCKED_FILES = @as(u32, 606688); pub const FSCTL_TXFS_LIST_TRANSACTIONS = @as(u32, 606692); pub const FSCTL_QUERY_PAGEFILE_ENCRYPTION = @as(u32, 590312); pub const FSCTL_RESET_VOLUME_ALLOCATION_HINTS = @as(u32, 590316); pub const FSCTL_QUERY_DEPENDENT_VOLUME = @as(u32, 590320); pub const FSCTL_SD_GLOBAL_CHANGE = @as(u32, 590324); pub const FSCTL_TXFS_READ_BACKUP_INFORMATION2 = @as(u32, 590328); pub const FSCTL_LOOKUP_STREAM_FROM_CLUSTER = @as(u32, 590332); pub const FSCTL_TXFS_WRITE_BACKUP_INFORMATION2 = @as(u32, 590336); pub const FSCTL_FILE_TYPE_NOTIFICATION = @as(u32, 590340); pub const FSCTL_FILE_LEVEL_TRIM = @as(u32, 623112); pub const FSCTL_GET_BOOT_AREA_INFO = @as(u32, 590384); pub const FSCTL_GET_RETRIEVAL_POINTER_BASE = @as(u32, 590388); pub const FSCTL_SET_PERSISTENT_VOLUME_STATE = @as(u32, 590392); pub const FSCTL_QUERY_PERSISTENT_VOLUME_STATE = @as(u32, 590396); pub const FSCTL_REQUEST_OPLOCK = @as(u32, 590400); pub const FSCTL_CSV_TUNNEL_REQUEST = @as(u32, 590404); pub const FSCTL_IS_CSV_FILE = @as(u32, 590408); pub const FSCTL_QUERY_FILE_SYSTEM_RECOGNITION = @as(u32, 590412); pub const FSCTL_CSV_GET_VOLUME_PATH_NAME = @as(u32, 590416); pub const FSCTL_CSV_GET_VOLUME_NAME_FOR_VOLUME_MOUNT_POINT = @as(u32, 590420); pub const FSCTL_CSV_GET_VOLUME_PATH_NAMES_FOR_VOLUME_NAME = @as(u32, 590424); pub const FSCTL_IS_FILE_ON_CSV_VOLUME = @as(u32, 590428); pub const FSCTL_CORRUPTION_HANDLING = @as(u32, 590432); pub const FSCTL_OFFLOAD_READ = @as(u32, 606820); pub const FSCTL_OFFLOAD_WRITE = @as(u32, 623208); pub const FSCTL_CSV_INTERNAL = @as(u32, 590444); pub const FSCTL_SET_PURGE_FAILURE_MODE = @as(u32, 590448); pub const FSCTL_QUERY_FILE_LAYOUT = @as(u32, 590455); pub const FSCTL_IS_VOLUME_OWNED_BYCSVFS = @as(u32, 590456); pub const FSCTL_GET_INTEGRITY_INFORMATION = @as(u32, 590460); pub const FSCTL_SET_INTEGRITY_INFORMATION = @as(u32, 639616); pub const FSCTL_QUERY_FILE_REGIONS = @as(u32, 590468); pub const FSCTL_RKF_INTERNAL = @as(u32, 590511); pub const FSCTL_SCRUB_DATA = @as(u32, 590512); pub const FSCTL_REPAIR_COPIES = @as(u32, 639668); pub const FSCTL_DISABLE_LOCAL_BUFFERING = @as(u32, 590520); pub const FSCTL_CSV_MGMT_LOCK = @as(u32, 590524); pub const FSCTL_CSV_QUERY_DOWN_LEVEL_FILE_SYSTEM_CHARACTERISTICS = @as(u32, 590528); pub const FSCTL_ADVANCE_FILE_ID = @as(u32, 590532); pub const FSCTL_CSV_SYNC_TUNNEL_REQUEST = @as(u32, 590536); pub const FSCTL_CSV_QUERY_VETO_FILE_DIRECT_IO = @as(u32, 590540); pub const FSCTL_WRITE_USN_REASON = @as(u32, 590544); pub const FSCTL_CSV_CONTROL = @as(u32, 590548); pub const FSCTL_GET_REFS_VOLUME_DATA = @as(u32, 590552); pub const FSCTL_CSV_H_BREAKING_SYNC_TUNNEL_REQUEST = @as(u32, 590564); pub const FSCTL_QUERY_STORAGE_CLASSES = @as(u32, 590572); pub const FSCTL_QUERY_REGION_INFO = @as(u32, 590576); pub const FSCTL_USN_TRACK_MODIFIED_RANGES = @as(u32, 590580); pub const FSCTL_QUERY_SHARED_VIRTUAL_DISK_SUPPORT = @as(u32, 590592); pub const FSCTL_SVHDX_SYNC_TUNNEL_REQUEST = @as(u32, 590596); pub const FSCTL_SVHDX_SET_INITIATOR_INFORMATION = @as(u32, 590600); pub const FSCTL_SET_EXTERNAL_BACKING = @as(u32, 590604); pub const FSCTL_GET_EXTERNAL_BACKING = @as(u32, 590608); pub const FSCTL_DELETE_EXTERNAL_BACKING = @as(u32, 590612); pub const FSCTL_ENUM_EXTERNAL_BACKING = @as(u32, 590616); pub const FSCTL_ENUM_OVERLAY = @as(u32, 590623); pub const FSCTL_ADD_OVERLAY = @as(u32, 623408); pub const FSCTL_REMOVE_OVERLAY = @as(u32, 623412); pub const FSCTL_UPDATE_OVERLAY = @as(u32, 623416); pub const FSCTL_SHUFFLE_FILE = @as(u32, 639808); pub const FSCTL_DUPLICATE_EXTENTS_TO_FILE = @as(u32, 623428); pub const FSCTL_SPARSE_OVERALLOCATE = @as(u32, 590668); pub const FSCTL_STORAGE_QOS_CONTROL = @as(u32, 590672); pub const FSCTL_INITIATE_FILE_METADATA_OPTIMIZATION = @as(u32, 590684); pub const FSCTL_QUERY_FILE_METADATA_OPTIMIZATION = @as(u32, 590688); pub const FSCTL_SVHDX_ASYNC_TUNNEL_REQUEST = @as(u32, 590692); pub const FSCTL_GET_WOF_VERSION = @as(u32, 590696); pub const FSCTL_HCS_SYNC_TUNNEL_REQUEST = @as(u32, 590700); pub const FSCTL_HCS_ASYNC_TUNNEL_REQUEST = @as(u32, 590704); pub const FSCTL_QUERY_EXTENT_READ_CACHE_INFO = @as(u32, 590711); pub const FSCTL_QUERY_REFS_VOLUME_COUNTER_INFO = @as(u32, 590715); pub const FSCTL_CLEAN_VOLUME_METADATA = @as(u32, 590716); pub const FSCTL_SET_INTEGRITY_INFORMATION_EX = @as(u32, 590720); pub const FSCTL_SUSPEND_OVERLAY = @as(u32, 590724); pub const FSCTL_VIRTUAL_STORAGE_QUERY_PROPERTY = @as(u32, 590728); pub const FSCTL_FILESYSTEM_GET_STATISTICS_EX = @as(u32, 590732); pub const FSCTL_QUERY_VOLUME_CONTAINER_STATE = @as(u32, 590736); pub const FSCTL_SET_LAYER_ROOT = @as(u32, 590740); pub const FSCTL_QUERY_DIRECT_ACCESS_EXTENTS = @as(u32, 590747); pub const FSCTL_NOTIFY_STORAGE_SPACE_ALLOCATION = @as(u32, 590748); pub const FSCTL_SSDI_STORAGE_REQUEST = @as(u32, 590752); pub const FSCTL_QUERY_DIRECT_IMAGE_ORIGINAL_BASE = @as(u32, 590756); pub const FSCTL_READ_UNPRIVILEGED_USN_JOURNAL = @as(u32, 590763); pub const FSCTL_GHOST_FILE_EXTENTS = @as(u32, 623532); pub const FSCTL_QUERY_GHOSTED_FILE_EXTENTS = @as(u32, 590768); pub const FSCTL_UNMAP_SPACE = @as(u32, 590772); pub const FSCTL_HCS_SYNC_NO_WRITE_TUNNEL_REQUEST = @as(u32, 590776); pub const FSCTL_START_VIRTUALIZATION_INSTANCE = @as(u32, 590784); pub const FSCTL_GET_FILTER_FILE_IDENTIFIER = @as(u32, 590788); pub const FSCTL_STREAMS_QUERY_PARAMETERS = @as(u32, 590788); pub const FSCTL_STREAMS_ASSOCIATE_ID = @as(u32, 590792); pub const FSCTL_STREAMS_QUERY_ID = @as(u32, 590796); pub const FSCTL_GET_RETRIEVAL_POINTERS_AND_REFCOUNT = @as(u32, 590803); pub const FSCTL_QUERY_VOLUME_NUMA_INFO = @as(u32, 590804); pub const FSCTL_REFS_DEALLOCATE_RANGES = @as(u32, 590808); pub const FSCTL_QUERY_REFS_SMR_VOLUME_INFO = @as(u32, 590812); pub const FSCTL_SET_REFS_SMR_VOLUME_GC_PARAMETERS = @as(u32, 590816); pub const FSCTL_SET_REFS_FILE_STRICTLY_SEQUENTIAL = @as(u32, 590820); pub const FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX = @as(u32, 623592); pub const FSCTL_QUERY_BAD_RANGES = @as(u32, 590828); pub const FSCTL_SET_DAX_ALLOC_ALIGNMENT_HINT = @as(u32, 590832); pub const FSCTL_DELETE_CORRUPTED_REFS_CONTAINER = @as(u32, 590836); pub const FSCTL_SCRUB_UNDISCOVERABLE_ID = @as(u32, 590840); pub const FSCTL_NOTIFY_DATA_CHANGE = @as(u32, 590844); pub const FSCTL_START_VIRTUALIZATION_INSTANCE_EX = @as(u32, 590848); pub const FSCTL_ENCRYPTION_KEY_CONTROL = @as(u32, 590852); pub const FSCTL_VIRTUAL_STORAGE_SET_BEHAVIOR = @as(u32, 590856); pub const FSCTL_SET_REPARSE_POINT_EX = @as(u32, 590860); pub const FSCTL_REARRANGE_FILE = @as(u32, 640032); pub const FSCTL_VIRTUAL_STORAGE_PASSTHROUGH = @as(u32, 590884); pub const FSCTL_GET_RETRIEVAL_POINTER_COUNT = @as(u32, 590891); pub const FSCTL_ENABLE_PER_IO_FLAGS = @as(u32, 590892); pub const FSCTL_QUERY_ASYNC_DUPLICATE_EXTENTS_STATUS = @as(u32, 590896); pub const FSCTL_SMB_SHARE_FLUSH_AND_PURGE = @as(u32, 590908); pub const FSCTL_REFS_STREAM_SNAPSHOT_MANAGEMENT = @as(u32, 590912); pub const FSCTL_MANAGE_BYPASS_IO = @as(u32, 590920); pub const GET_VOLUME_BITMAP_FLAG_MASK_METADATA = @as(u32, 1); pub const FLAG_USN_TRACK_MODIFIED_RANGES_ENABLE = @as(u32, 1); pub const USN_PAGE_SIZE = @as(u32, 4096); pub const USN_REASON_DATA_OVERWRITE = @as(u32, 1); pub const USN_REASON_DATA_EXTEND = @as(u32, 2); pub const USN_REASON_DATA_TRUNCATION = @as(u32, 4); pub const USN_REASON_NAMED_DATA_OVERWRITE = @as(u32, 16); pub const USN_REASON_NAMED_DATA_EXTEND = @as(u32, 32); pub const USN_REASON_NAMED_DATA_TRUNCATION = @as(u32, 64); pub const USN_REASON_FILE_CREATE = @as(u32, 256); pub const USN_REASON_FILE_DELETE = @as(u32, 512); pub const USN_REASON_EA_CHANGE = @as(u32, 1024); pub const USN_REASON_SECURITY_CHANGE = @as(u32, 2048); pub const USN_REASON_RENAME_OLD_NAME = @as(u32, 4096); pub const USN_REASON_RENAME_NEW_NAME = @as(u32, 8192); pub const USN_REASON_INDEXABLE_CHANGE = @as(u32, 16384); pub const USN_REASON_BASIC_INFO_CHANGE = @as(u32, 32768); pub const USN_REASON_HARD_LINK_CHANGE = @as(u32, 65536); pub const USN_REASON_COMPRESSION_CHANGE = @as(u32, 131072); pub const USN_REASON_ENCRYPTION_CHANGE = @as(u32, 262144); pub const USN_REASON_OBJECT_ID_CHANGE = @as(u32, 524288); pub const USN_REASON_REPARSE_POINT_CHANGE = @as(u32, 1048576); pub const USN_REASON_STREAM_CHANGE = @as(u32, 2097152); pub const USN_REASON_TRANSACTED_CHANGE = @as(u32, 4194304); pub const USN_REASON_INTEGRITY_CHANGE = @as(u32, 8388608); pub const USN_REASON_DESIRED_STORAGE_CLASS_CHANGE = @as(u32, 16777216); pub const USN_REASON_CLOSE = @as(u32, 2147483648); pub const USN_DELETE_VALID_FLAGS = @as(u32, 3); pub const MARK_HANDLE_PROTECT_CLUSTERS = @as(u32, 1); pub const MARK_HANDLE_TXF_SYSTEM_LOG = @as(u32, 4); pub const MARK_HANDLE_NOT_TXF_SYSTEM_LOG = @as(u32, 8); pub const MARK_HANDLE_REALTIME = @as(u32, 32); pub const MARK_HANDLE_NOT_REALTIME = @as(u32, 64); pub const MARK_HANDLE_CLOUD_SYNC = @as(u32, 2048); pub const MARK_HANDLE_READ_COPY = @as(u32, 128); pub const MARK_HANDLE_NOT_READ_COPY = @as(u32, 256); pub const MARK_HANDLE_FILTER_METADATA = @as(u32, 512); pub const MARK_HANDLE_RETURN_PURGE_FAILURE = @as(u32, 1024); pub const MARK_HANDLE_DISABLE_FILE_METADATA_OPTIMIZATION = @as(u32, 4096); pub const MARK_HANDLE_ENABLE_USN_SOURCE_ON_PAGING_IO = @as(u32, 8192); pub const MARK_HANDLE_SKIP_COHERENCY_SYNC_DISALLOW_WRITES = @as(u32, 16384); pub const MARK_HANDLE_SUPPRESS_VOLUME_OPEN_FLUSH = @as(u32, 32768); pub const MARK_HANDLE_ENABLE_CPU_CACHE = @as(u32, 268435456); pub const VOLUME_IS_DIRTY = @as(u32, 1); pub const VOLUME_UPGRADE_SCHEDULED = @as(u32, 2); pub const VOLUME_SESSION_OPEN = @as(u32, 4); pub const FILE_PREFETCH_TYPE_FOR_CREATE = @as(u32, 1); pub const FILE_PREFETCH_TYPE_FOR_DIRENUM = @as(u32, 2); pub const FILE_PREFETCH_TYPE_FOR_CREATE_EX = @as(u32, 3); pub const FILE_PREFETCH_TYPE_FOR_DIRENUM_EX = @as(u32, 4); pub const FILE_PREFETCH_TYPE_MAX = @as(u32, 4); pub const FILESYSTEM_STATISTICS_TYPE_REFS = @as(u32, 4); pub const FILE_ZERO_DATA_INFORMATION_FLAG_PRESERVE_CACHED_DATA = @as(u32, 1); pub const FILE_SET_ENCRYPTION = @as(u32, 1); pub const FILE_CLEAR_ENCRYPTION = @as(u32, 2); pub const STREAM_SET_ENCRYPTION = @as(u32, 3); pub const STREAM_CLEAR_ENCRYPTION = @as(u32, 4); pub const MAXIMUM_ENCRYPTION_VALUE = @as(u32, 4); pub const ENCRYPTION_FORMAT_DEFAULT = @as(u32, 1); pub const ENCRYPTED_DATA_INFO_SPARSE_FILE = @as(u32, 1); pub const COPYFILE_SIS_LINK = @as(u32, 1); pub const COPYFILE_SIS_REPLACE = @as(u32, 2); pub const COPYFILE_SIS_FLAGS = @as(u32, 3); pub const SET_REPAIR_ENABLED = @as(u32, 1); pub const SET_REPAIR_WARN_ABOUT_DATA_LOSS = @as(u32, 8); pub const SET_REPAIR_DISABLED_AND_BUGCHECK_ON_CORRUPT = @as(u32, 16); pub const SET_REPAIR_VALID_MASK = @as(u32, 25); pub const FILE_INITIATE_REPAIR_HINT1_FILE_RECORD_NOT_IN_USE = @as(u64, 1); pub const FILE_INITIATE_REPAIR_HINT1_FILE_RECORD_REUSED = @as(u64, 2); pub const FILE_INITIATE_REPAIR_HINT1_FILE_RECORD_NOT_EXIST = @as(u64, 4); pub const FILE_INITIATE_REPAIR_HINT1_FILE_RECORD_NOT_BASE_RECORD = @as(u64, 8); pub const FILE_INITIATE_REPAIR_HINT1_SYSTEM_FILE = @as(u64, 16); pub const FILE_INITIATE_REPAIR_HINT1_NOT_IMPLEMENTED = @as(u64, 32); pub const FILE_INITIATE_REPAIR_HINT1_UNABLE_TO_REPAIR = @as(u64, 64); pub const FILE_INITIATE_REPAIR_HINT1_REPAIR_DISABLED = @as(u64, 128); pub const FILE_INITIATE_REPAIR_HINT1_RECURSIVELY_CORRUPTED = @as(u64, 256); pub const FILE_INITIATE_REPAIR_HINT1_ORPHAN_GENERATED = @as(u64, 512); pub const FILE_INITIATE_REPAIR_HINT1_REPAIRED = @as(u64, 1024); pub const FILE_INITIATE_REPAIR_HINT1_NOTHING_WRONG = @as(u64, 2048); pub const FILE_INITIATE_REPAIR_HINT1_ATTRIBUTE_NOT_FOUND = @as(u64, 4096); pub const FILE_INITIATE_REPAIR_HINT1_POTENTIAL_CROSSLINK = @as(u64, 8192); pub const FILE_INITIATE_REPAIR_HINT1_STALE_INFORMATION = @as(u64, 16384); pub const FILE_INITIATE_REPAIR_HINT1_CLUSTERS_ALREADY_IN_USE = @as(u64, 32768); pub const FILE_INITIATE_REPAIR_HINT1_LCN_NOT_EXIST = @as(u64, 65536); pub const FILE_INITIATE_REPAIR_HINT1_INVALID_RUN_LENGTH = @as(u64, 131072); pub const FILE_INITIATE_REPAIR_HINT1_FILE_RECORD_NOT_ORPHAN = @as(u64, 262144); pub const FILE_INITIATE_REPAIR_HINT1_FILE_RECORD_IS_BASE_RECORD = @as(u64, 524288); pub const FILE_INITIATE_REPAIR_HINT1_INVALID_ARRAY_LENGTH_COUNT = @as(u64, 1048576); pub const FILE_INITIATE_REPAIR_HINT1_SID_VALID = @as(u64, 2097152); pub const FILE_INITIATE_REPAIR_HINT1_SID_MISMATCH = @as(u64, 4194304); pub const FILE_INITIATE_REPAIR_HINT1_INVALID_PARENT = @as(u64, 8388608); pub const FILE_INITIATE_REPAIR_HINT1_PARENT_FILE_RECORD_NOT_IN_USE = @as(u64, 16777216); pub const FILE_INITIATE_REPAIR_HINT1_PARENT_FILE_RECORD_REUSED = @as(u64, 33554432); pub const FILE_INITIATE_REPAIR_HINT1_PARENT_FILE_RECORD_NOT_EXIST = @as(u64, 67108864); pub const FILE_INITIATE_REPAIR_HINT1_PARENT_FILE_RECORD_NOT_BASE_RECORD = @as(u64, 134217728); pub const FILE_INITIATE_REPAIR_HINT1_PARENT_FILE_RECORD_NOT_INDEX = @as(u64, 268435456); pub const FILE_INITIATE_REPAIR_HINT1_VALID_INDEX_ENTRY = @as(u64, 536870912); pub const FILE_INITIATE_REPAIR_HINT1_OUT_OF_GENERIC_NAMES = @as(u64, 1073741824); pub const FILE_INITIATE_REPAIR_HINT1_OUT_OF_RESOURCE = @as(u64, 2147483648); pub const FILE_INITIATE_REPAIR_HINT1_INVALID_LCN = @as(u64, 4294967296); pub const FILE_INITIATE_REPAIR_HINT1_INVALID_VCN = @as(u64, 8589934592); pub const FILE_INITIATE_REPAIR_HINT1_NAME_CONFLICT = @as(u64, 17179869184); pub const FILE_INITIATE_REPAIR_HINT1_ORPHAN = @as(u64, 34359738368); pub const FILE_INITIATE_REPAIR_HINT1_ATTRIBUTE_TOO_SMALL = @as(u64, 68719476736); pub const FILE_INITIATE_REPAIR_HINT1_ATTRIBUTE_NON_RESIDENT = @as(u64, 137438953472); pub const FILE_INITIATE_REPAIR_HINT1_DENY_DEFRAG = @as(u64, 274877906944); pub const FILE_INITIATE_REPAIR_HINT1_PREVIOUS_PARENT_STILL_VALID = @as(u64, 549755813888); pub const FILE_INITIATE_REPAIR_HINT1_INDEX_ENTRY_MISMATCH = @as(u64, 1099511627776); pub const FILE_INITIATE_REPAIR_HINT1_INVALID_ORPHAN_RECOVERY_NAME = @as(u64, 2199023255552); pub const FILE_INITIATE_REPAIR_HINT1_MULTIPLE_FILE_NAME_ATTRIBUTES = @as(u64, 4398046511104); pub const TXFS_LOGGING_MODE_SIMPLE = @as(u32, 1); pub const TXFS_LOGGING_MODE_FULL = @as(u32, 2); pub const TXFS_TRANSACTION_STATE_NONE = @as(u32, 0); pub const TXFS_TRANSACTION_STATE_ACTIVE = @as(u32, 1); pub const TXFS_TRANSACTION_STATE_PREPARED = @as(u32, 2); pub const TXFS_TRANSACTION_STATE_NOTACTIVE = @as(u32, 3); pub const TXFS_RM_STATE_NOT_STARTED = @as(u32, 0); pub const TXFS_RM_STATE_STARTING = @as(u32, 1); pub const TXFS_RM_STATE_ACTIVE = @as(u32, 2); pub const TXFS_RM_STATE_SHUTTING_DOWN = @as(u32, 3); pub const TXFS_ROLLFORWARD_REDO_FLAG_USE_LAST_REDO_LSN = @as(u32, 1); pub const TXFS_ROLLFORWARD_REDO_FLAG_USE_LAST_VIRTUAL_CLOCK = @as(u32, 2); pub const TXFS_START_RM_FLAG_LOG_CONTAINER_COUNT_MAX = @as(u32, 1); pub const TXFS_START_RM_FLAG_LOG_CONTAINER_COUNT_MIN = @as(u32, 2); pub const TXFS_START_RM_FLAG_LOG_CONTAINER_SIZE = @as(u32, 4); pub const TXFS_START_RM_FLAG_LOG_GROWTH_INCREMENT_NUM_CONTAINERS = @as(u32, 8); pub const TXFS_START_RM_FLAG_LOG_GROWTH_INCREMENT_PERCENT = @as(u32, 16); pub const TXFS_START_RM_FLAG_LOG_AUTO_SHRINK_PERCENTAGE = @as(u32, 32); pub const TXFS_START_RM_FLAG_LOG_NO_CONTAINER_COUNT_MAX = @as(u32, 64); pub const TXFS_START_RM_FLAG_LOG_NO_CONTAINER_COUNT_MIN = @as(u32, 128); pub const TXFS_START_RM_FLAG_RECOVER_BEST_EFFORT = @as(u32, 512); pub const TXFS_START_RM_FLAG_LOGGING_MODE = @as(u32, 1024); pub const TXFS_START_RM_FLAG_PRESERVE_CHANGES = @as(u32, 2048); pub const TXFS_START_RM_FLAG_PREFER_CONSISTENCY = @as(u32, 4096); pub const TXFS_START_RM_FLAG_PREFER_AVAILABILITY = @as(u32, 8192); pub const TXFS_LIST_TRANSACTION_LOCKED_FILES_ENTRY_FLAG_CREATED = @as(u32, 1); pub const TXFS_LIST_TRANSACTION_LOCKED_FILES_ENTRY_FLAG_DELETED = @as(u32, 2); pub const TXFS_TRANSACTED_VERSION_NONTRANSACTED = @as(u32, 4294967294); pub const TXFS_TRANSACTED_VERSION_UNCOMMITTED = @as(u32, 4294967295); pub const TXFS_SAVEPOINT_SET = @as(u32, 1); pub const TXFS_SAVEPOINT_ROLLBACK = @as(u32, 2); pub const TXFS_SAVEPOINT_CLEAR = @as(u32, 4); pub const TXFS_SAVEPOINT_CLEAR_ALL = @as(u32, 16); pub const PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED = @as(u32, 1); pub const PERSISTENT_VOLUME_STATE_VOLUME_SCRUB_DISABLED = @as(u32, 2); pub const PERSISTENT_VOLUME_STATE_GLOBAL_METADATA_NO_SEEK_PENALTY = @as(u32, 4); pub const PERSISTENT_VOLUME_STATE_LOCAL_METADATA_NO_SEEK_PENALTY = @as(u32, 8); pub const PERSISTENT_VOLUME_STATE_NO_HEAT_GATHERING = @as(u32, 16); pub const PERSISTENT_VOLUME_STATE_CONTAINS_BACKING_WIM = @as(u32, 32); pub const PERSISTENT_VOLUME_STATE_BACKED_BY_WIM = @as(u32, 64); pub const PERSISTENT_VOLUME_STATE_NO_WRITE_AUTO_TIERING = @as(u32, 128); pub const PERSISTENT_VOLUME_STATE_TXF_DISABLED = @as(u32, 256); pub const PERSISTENT_VOLUME_STATE_REALLOCATE_ALL_DATA_WRITES = @as(u32, 512); pub const PERSISTENT_VOLUME_STATE_CHKDSK_RAN_ONCE = @as(u32, 1024); pub const PERSISTENT_VOLUME_STATE_MODIFIED_BY_CHKDSK = @as(u32, 2048); pub const PERSISTENT_VOLUME_STATE_DAX_FORMATTED = @as(u32, 4096); pub const OPLOCK_LEVEL_CACHE_READ = @as(u32, 1); pub const OPLOCK_LEVEL_CACHE_HANDLE = @as(u32, 2); pub const OPLOCK_LEVEL_CACHE_WRITE = @as(u32, 4); pub const REQUEST_OPLOCK_INPUT_FLAG_REQUEST = @as(u32, 1); pub const REQUEST_OPLOCK_INPUT_FLAG_ACK = @as(u32, 2); pub const REQUEST_OPLOCK_INPUT_FLAG_COMPLETE_ACK_ON_CLOSE = @as(u32, 4); pub const REQUEST_OPLOCK_CURRENT_VERSION = @as(u32, 1); pub const REQUEST_OPLOCK_OUTPUT_FLAG_ACK_REQUIRED = @as(u32, 1); pub const REQUEST_OPLOCK_OUTPUT_FLAG_MODES_PROVIDED = @as(u32, 2); pub const QUERY_DEPENDENT_VOLUME_REQUEST_FLAG_HOST_VOLUMES = @as(u32, 1); pub const QUERY_DEPENDENT_VOLUME_REQUEST_FLAG_GUEST_VOLUMES = @as(u32, 2); pub const SD_GLOBAL_CHANGE_TYPE_MACHINE_SID = @as(u32, 1); pub const SD_GLOBAL_CHANGE_TYPE_QUERY_STATS = @as(u32, 65536); pub const SD_GLOBAL_CHANGE_TYPE_ENUM_SDS = @as(u32, 131072); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_FLAG_PAGE_FILE = @as(u32, 1); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_FLAG_DENY_DEFRAG_SET = @as(u32, 2); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_FLAG_FS_SYSTEM_FILE = @as(u32, 4); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_FLAG_TXF_SYSTEM_FILE = @as(u32, 8); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_ATTRIBUTE_MASK = @as(u32, 4278190080); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_ATTRIBUTE_DATA = @as(u32, 16777216); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_ATTRIBUTE_INDEX = @as(u32, 33554432); pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY_ATTRIBUTE_SYSTEM = @as(u32, 50331648); pub const FILE_TYPE_NOTIFICATION_FLAG_USAGE_BEGIN = @as(u32, 1); pub const FILE_TYPE_NOTIFICATION_FLAG_USAGE_END = @as(u32, 2); pub const FILE_TYPE_NOTIFICATION_GUID_PAGE_FILE = Guid.initString("0d0a64a1-38fc-4db8-9fe7-3f4352cd7c5c"); pub const FILE_TYPE_NOTIFICATION_GUID_HIBERNATION_FILE = Guid.initString("b7624d64-b9a3-4cf8-8011-5b86c940e7b7"); pub const FILE_TYPE_NOTIFICATION_GUID_CRASHDUMP_FILE = Guid.initString("9d453eb7-d2a6-4dbd-a2e3-fbd0ed9109a9"); pub const CSV_MGMTLOCK_CHECK_VOLUME_REDIRECTED = @as(u32, 1); pub const CSV_INVALID_DEVICE_NUMBER = @as(u32, 4294967295); pub const CSV_QUERY_MDS_PATH_V2_VERSION_1 = @as(u32, 1); pub const CSV_QUERY_MDS_PATH_FLAG_STORAGE_ON_THIS_NODE_IS_CONNECTED = @as(u32, 1); pub const CSV_QUERY_MDS_PATH_FLAG_CSV_DIRECT_IO_ENABLED = @as(u32, 2); pub const CSV_QUERY_MDS_PATH_FLAG_SMB_BYPASS_CSV_ENABLED = @as(u32, 4); pub const QUERY_FILE_LAYOUT_RESTART = @as(u32, 1); pub const QUERY_FILE_LAYOUT_INCLUDE_NAMES = @as(u32, 2); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAMS = @as(u32, 4); pub const QUERY_FILE_LAYOUT_INCLUDE_EXTENTS = @as(u32, 8); pub const QUERY_FILE_LAYOUT_INCLUDE_EXTRA_INFO = @as(u32, 16); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAMS_WITH_NO_CLUSTERS_ALLOCATED = @as(u32, 32); pub const QUERY_FILE_LAYOUT_INCLUDE_FULL_PATH_IN_NAMES = @as(u32, 64); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION = @as(u32, 128); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION_FOR_DSC_ATTRIBUTE = @as(u32, 256); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION_FOR_TXF_ATTRIBUTE = @as(u32, 512); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION_FOR_EFS_ATTRIBUTE = @as(u32, 1024); pub const QUERY_FILE_LAYOUT_INCLUDE_ONLY_FILES_WITH_SPECIFIC_ATTRIBUTES = @as(u32, 2048); pub const QUERY_FILE_LAYOUT_INCLUDE_FILES_WITH_DSC_ATTRIBUTE = @as(u32, 4096); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION_FOR_DATA_ATTRIBUTE = @as(u32, 8192); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION_FOR_REPARSE_ATTRIBUTE = @as(u32, 16384); pub const QUERY_FILE_LAYOUT_INCLUDE_STREAM_INFORMATION_FOR_EA_ATTRIBUTE = @as(u32, 32768); pub const QUERY_FILE_LAYOUT_SINGLE_INSTANCED = @as(u32, 1); pub const FILE_LAYOUT_NAME_ENTRY_PRIMARY = @as(u32, 1); pub const FILE_LAYOUT_NAME_ENTRY_DOS = @as(u32, 2); pub const STREAM_LAYOUT_ENTRY_IMMOVABLE = @as(u32, 1); pub const STREAM_LAYOUT_ENTRY_PINNED = @as(u32, 2); pub const STREAM_LAYOUT_ENTRY_RESIDENT = @as(u32, 4); pub const STREAM_LAYOUT_ENTRY_NO_CLUSTERS_ALLOCATED = @as(u32, 8); pub const STREAM_LAYOUT_ENTRY_HAS_INFORMATION = @as(u32, 16); pub const STREAM_EXTENT_ENTRY_AS_RETRIEVAL_POINTERS = @as(u32, 1); pub const STREAM_EXTENT_ENTRY_ALL_EXTENTS = @as(u32, 2); pub const CHECKSUM_TYPE_UNCHANGED = @as(i32, -1); pub const CHECKSUM_TYPE_NONE = @as(u32, 0); pub const CHECKSUM_TYPE_CRC32 = @as(u32, 1); pub const CHECKSUM_TYPE_CRC64 = @as(u32, 2); pub const CHECKSUM_TYPE_ECC = @as(u32, 3); pub const CHECKSUM_TYPE_FIRST_UNUSED_TYPE = @as(u32, 4); pub const FSCTL_INTEGRITY_FLAG_CHECKSUM_ENFORCEMENT_OFF = @as(u32, 1); pub const OFFLOAD_READ_FLAG_ALL_ZERO_BEYOND_CURRENT_RANGE = @as(u32, 1); pub const SET_PURGE_FAILURE_MODE_ENABLED = @as(u32, 1); pub const SET_PURGE_FAILURE_MODE_DISABLED = @as(u32, 2); pub const FILE_REGION_USAGE_VALID_CACHED_DATA = @as(u32, 1); pub const FILE_REGION_USAGE_VALID_NONCACHED_DATA = @as(u32, 2); pub const FILE_REGION_USAGE_OTHER_PAGE_ALIGNMENT = @as(u32, 4); pub const FILE_REGION_USAGE_LARGE_PAGE_ALIGNMENT = @as(u32, 8); pub const FILE_REGION_USAGE_HUGE_PAGE_ALIGNMENT = @as(u32, 16); pub const FILE_REGION_USAGE_QUERY_ALIGNMENT = @as(u32, 8); pub const FILE_STORAGE_TIER_NAME_LENGTH = @as(u32, 256); pub const FILE_STORAGE_TIER_DESCRIPTION_LENGTH = @as(u32, 512); pub const FILE_STORAGE_TIER_FLAG_WRITE_BACK_CACHE = @as(u32, 2097152); pub const FILE_STORAGE_TIER_FLAG_READ_CACHE = @as(u32, 4194304); pub const FILE_STORAGE_TIER_FLAG_PARITY = @as(u32, 8388608); pub const FILE_STORAGE_TIER_FLAG_SMR = @as(u32, 16777216); pub const QUERY_STORAGE_CLASSES_FLAGS_MEASURE_WRITE = @as(u32, 2147483648); pub const QUERY_STORAGE_CLASSES_FLAGS_MEASURE_READ = @as(u32, 1073741824); pub const QUERY_STORAGE_CLASSES_FLAGS_NO_DEFRAG_VOLUME = @as(u32, 536870912); pub const QUERY_FILE_LAYOUT_REPARSE_DATA_INVALID = @as(u32, 1); pub const QUERY_FILE_LAYOUT_REPARSE_TAG_INVALID = @as(u32, 2); pub const DUPLICATE_EXTENTS_DATA_EX_SOURCE_ATOMIC = @as(u32, 1); pub const DUPLICATE_EXTENTS_DATA_EX_ASYNC = @as(u32, 2); pub const REFS_SMR_VOLUME_INFO_OUTPUT_VERSION_V0 = @as(u32, 0); pub const REFS_SMR_VOLUME_INFO_OUTPUT_VERSION_V1 = @as(u32, 1); pub const REFS_SMR_VOLUME_GC_PARAMETERS_VERSION_V1 = @as(u32, 1); pub const STREAMS_INVALID_ID = @as(u32, 0); pub const STREAMS_MAX_ID = @as(u32, 65535); pub const STREAMS_ASSOCIATE_ID_CLEAR = @as(u32, 1); pub const STREAMS_ASSOCIATE_ID_SET = @as(u32, 2); pub const DAX_ALLOC_ALIGNMENT_FLAG_MANDATORY = @as(u32, 1); pub const DAX_ALLOC_ALIGNMENT_FLAG_FALLBACK_SPECIFIED = @as(u32, 2); pub const WOF_CURRENT_VERSION = @as(u32, 1); pub const WOF_PROVIDER_CLOUD = @as(u32, 3); pub const WIM_PROVIDER_CURRENT_VERSION = @as(u32, 1); pub const WIM_PROVIDER_EXTERNAL_FLAG_NOT_ACTIVE = @as(u32, 1); pub const WIM_PROVIDER_EXTERNAL_FLAG_SUSPENDED = @as(u32, 2); pub const FILE_PROVIDER_CURRENT_VERSION = @as(u32, 1); pub const FILE_PROVIDER_SINGLE_FILE = @as(u32, 1); pub const FILE_PROVIDER_COMPRESSION_MAXIMUM = @as(u32, 4); pub const FILE_PROVIDER_FLAG_COMPRESS_ON_WRITE = @as(u32, 1); pub const CONTAINER_VOLUME_STATE_HOSTING_CONTAINER = @as(u32, 1); pub const CONTAINER_ROOT_INFO_FLAG_SCRATCH_ROOT = @as(u32, 1); pub const CONTAINER_ROOT_INFO_FLAG_LAYER_ROOT = @as(u32, 2); pub const CONTAINER_ROOT_INFO_FLAG_VIRTUALIZATION_ROOT = @as(u32, 4); pub const CONTAINER_ROOT_INFO_FLAG_VIRTUALIZATION_TARGET_ROOT = @as(u32, 8); pub const CONTAINER_ROOT_INFO_FLAG_VIRTUALIZATION_EXCEPTION_ROOT = @as(u32, 16); pub const CONTAINER_ROOT_INFO_FLAG_BIND_ROOT = @as(u32, 32); pub const CONTAINER_ROOT_INFO_FLAG_BIND_TARGET_ROOT = @as(u32, 64); pub const CONTAINER_ROOT_INFO_FLAG_BIND_EXCEPTION_ROOT = @as(u32, 128); pub const CONTAINER_ROOT_INFO_FLAG_BIND_DO_NOT_MAP_NAME = @as(u32, 256); pub const CONTAINER_ROOT_INFO_FLAG_UNION_LAYER_ROOT = @as(u32, 512); pub const CONTAINER_ROOT_INFO_VALID_FLAGS = @as(u32, 1023); pub const PROJFS_PROTOCOL_VERSION = @as(u32, 3); pub const EFS_TRACKED_OFFSET_HEADER_FLAG = @as(u32, 1); pub const SPACES_TRACKED_OFFSET_HEADER_FLAG = @as(u32, 2); //-------------------------------------------------------------------------------- // Section: Types (521) //-------------------------------------------------------------------------------- pub const GPT_ATTRIBUTES = enum(u64) { ATTRIBUTE_PLATFORM_REQUIRED = 1, BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER = 9223372036854775808, BASIC_DATA_ATTRIBUTE_HIDDEN = 4611686018427387904, BASIC_DATA_ATTRIBUTE_SHADOW_COPY = 2305843009213693952, BASIC_DATA_ATTRIBUTE_READ_ONLY = 1152921504606846976, _, pub fn initFlags(o: struct { ATTRIBUTE_PLATFORM_REQUIRED: u1 = 0, BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER: u1 = 0, BASIC_DATA_ATTRIBUTE_HIDDEN: u1 = 0, BASIC_DATA_ATTRIBUTE_SHADOW_COPY: u1 = 0, BASIC_DATA_ATTRIBUTE_READ_ONLY: u1 = 0, }) GPT_ATTRIBUTES { return @intToEnum(GPT_ATTRIBUTES, (if (o.ATTRIBUTE_PLATFORM_REQUIRED == 1) @enumToInt(GPT_ATTRIBUTES.ATTRIBUTE_PLATFORM_REQUIRED) else 0) | (if (o.BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER == 1) @enumToInt(GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER) else 0) | (if (o.BASIC_DATA_ATTRIBUTE_HIDDEN == 1) @enumToInt(GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_HIDDEN) else 0) | (if (o.BASIC_DATA_ATTRIBUTE_SHADOW_COPY == 1) @enumToInt(GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_SHADOW_COPY) else 0) | (if (o.BASIC_DATA_ATTRIBUTE_READ_ONLY == 1) @enumToInt(GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_READ_ONLY) else 0) ); } }; pub const GPT_ATTRIBUTE_PLATFORM_REQUIRED = GPT_ATTRIBUTES.ATTRIBUTE_PLATFORM_REQUIRED; pub const GPT_BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER = GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_NO_DRIVE_LETTER; pub const GPT_BASIC_DATA_ATTRIBUTE_HIDDEN = GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_HIDDEN; pub const GPT_BASIC_DATA_ATTRIBUTE_SHADOW_COPY = GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_SHADOW_COPY; pub const GPT_BASIC_DATA_ATTRIBUTE_READ_ONLY = GPT_ATTRIBUTES.BASIC_DATA_ATTRIBUTE_READ_ONLY; pub const USN_DELETE_FLAGS = enum(u32) { DELETE = 1, NOTIFY = 2, _, pub fn initFlags(o: struct { DELETE: u1 = 0, NOTIFY: u1 = 0, }) USN_DELETE_FLAGS { return @intToEnum(USN_DELETE_FLAGS, (if (o.DELETE == 1) @enumToInt(USN_DELETE_FLAGS.DELETE) else 0) | (if (o.NOTIFY == 1) @enumToInt(USN_DELETE_FLAGS.NOTIFY) else 0) ); } }; pub const USN_DELETE_FLAG_DELETE = USN_DELETE_FLAGS.DELETE; pub const USN_DELETE_FLAG_NOTIFY = USN_DELETE_FLAGS.NOTIFY; pub const CHANGER_FEATURES = enum(u32) { BAR_CODE_SCANNER_INSTALLED = 1, CARTRIDGE_MAGAZINE = 256, CLEANER_ACCESS_NOT_VALID = 262144, CLEANER_SLOT = 64, CLOSE_IEPORT = 4, DEVICE_REINITIALIZE_CAPABLE = 134217728, DRIVE_CLEANING_REQUIRED = 65536, DRIVE_EMPTY_ON_DOOR_ACCESS = 536870912, EXCHANGE_MEDIA = 32, INIT_ELEM_STAT_WITH_RANGE = 2, KEYPAD_ENABLE_DISABLE = 268435456, LOCK_UNLOCK = 128, MEDIUM_FLIP = 512, OPEN_IEPORT = 8, POSITION_TO_ELEMENT = 1024, PREDISMOUNT_EJECT_REQUIRED = 131072, PREMOUNT_EJECT_REQUIRED = 524288, REPORT_IEPORT_STATE = 2048, SERIAL_NUMBER_VALID = 67108864, STATUS_NON_VOLATILE = 16, STORAGE_DRIVE = 4096, STORAGE_IEPORT = 8192, STORAGE_SLOT = 16384, STORAGE_TRANSPORT = 32768, VOLUME_ASSERT = 4194304, VOLUME_IDENTIFICATION = 1048576, VOLUME_REPLACE = 8388608, VOLUME_SEARCH = 2097152, VOLUME_UNDEFINE = 16777216, _, pub fn initFlags(o: struct { BAR_CODE_SCANNER_INSTALLED: u1 = 0, CARTRIDGE_MAGAZINE: u1 = 0, CLEANER_ACCESS_NOT_VALID: u1 = 0, CLEANER_SLOT: u1 = 0, CLOSE_IEPORT: u1 = 0, DEVICE_REINITIALIZE_CAPABLE: u1 = 0, DRIVE_CLEANING_REQUIRED: u1 = 0, DRIVE_EMPTY_ON_DOOR_ACCESS: u1 = 0, EXCHANGE_MEDIA: u1 = 0, INIT_ELEM_STAT_WITH_RANGE: u1 = 0, KEYPAD_ENABLE_DISABLE: u1 = 0, LOCK_UNLOCK: u1 = 0, MEDIUM_FLIP: u1 = 0, OPEN_IEPORT: u1 = 0, POSITION_TO_ELEMENT: u1 = 0, PREDISMOUNT_EJECT_REQUIRED: u1 = 0, PREMOUNT_EJECT_REQUIRED: u1 = 0, REPORT_IEPORT_STATE: u1 = 0, SERIAL_NUMBER_VALID: u1 = 0, STATUS_NON_VOLATILE: u1 = 0, STORAGE_DRIVE: u1 = 0, STORAGE_IEPORT: u1 = 0, STORAGE_SLOT: u1 = 0, STORAGE_TRANSPORT: u1 = 0, VOLUME_ASSERT: u1 = 0, VOLUME_IDENTIFICATION: u1 = 0, VOLUME_REPLACE: u1 = 0, VOLUME_SEARCH: u1 = 0, VOLUME_UNDEFINE: u1 = 0, }) CHANGER_FEATURES { return @intToEnum(CHANGER_FEATURES, (if (o.BAR_CODE_SCANNER_INSTALLED == 1) @enumToInt(CHANGER_FEATURES.BAR_CODE_SCANNER_INSTALLED) else 0) | (if (o.CARTRIDGE_MAGAZINE == 1) @enumToInt(CHANGER_FEATURES.CARTRIDGE_MAGAZINE) else 0) | (if (o.CLEANER_ACCESS_NOT_VALID == 1) @enumToInt(CHANGER_FEATURES.CLEANER_ACCESS_NOT_VALID) else 0) | (if (o.CLEANER_SLOT == 1) @enumToInt(CHANGER_FEATURES.CLEANER_SLOT) else 0) | (if (o.CLOSE_IEPORT == 1) @enumToInt(CHANGER_FEATURES.CLOSE_IEPORT) else 0) | (if (o.DEVICE_REINITIALIZE_CAPABLE == 1) @enumToInt(CHANGER_FEATURES.DEVICE_REINITIALIZE_CAPABLE) else 0) | (if (o.DRIVE_CLEANING_REQUIRED == 1) @enumToInt(CHANGER_FEATURES.DRIVE_CLEANING_REQUIRED) else 0) | (if (o.DRIVE_EMPTY_ON_DOOR_ACCESS == 1) @enumToInt(CHANGER_FEATURES.DRIVE_EMPTY_ON_DOOR_ACCESS) else 0) | (if (o.EXCHANGE_MEDIA == 1) @enumToInt(CHANGER_FEATURES.EXCHANGE_MEDIA) else 0) | (if (o.INIT_ELEM_STAT_WITH_RANGE == 1) @enumToInt(CHANGER_FEATURES.INIT_ELEM_STAT_WITH_RANGE) else 0) | (if (o.KEYPAD_ENABLE_DISABLE == 1) @enumToInt(CHANGER_FEATURES.KEYPAD_ENABLE_DISABLE) else 0) | (if (o.LOCK_UNLOCK == 1) @enumToInt(CHANGER_FEATURES.LOCK_UNLOCK) else 0) | (if (o.MEDIUM_FLIP == 1) @enumToInt(CHANGER_FEATURES.MEDIUM_FLIP) else 0) | (if (o.OPEN_IEPORT == 1) @enumToInt(CHANGER_FEATURES.OPEN_IEPORT) else 0) | (if (o.POSITION_TO_ELEMENT == 1) @enumToInt(CHANGER_FEATURES.POSITION_TO_ELEMENT) else 0) | (if (o.PREDISMOUNT_EJECT_REQUIRED == 1) @enumToInt(CHANGER_FEATURES.PREDISMOUNT_EJECT_REQUIRED) else 0) | (if (o.PREMOUNT_EJECT_REQUIRED == 1) @enumToInt(CHANGER_FEATURES.PREMOUNT_EJECT_REQUIRED) else 0) | (if (o.REPORT_IEPORT_STATE == 1) @enumToInt(CHANGER_FEATURES.REPORT_IEPORT_STATE) else 0) | (if (o.SERIAL_NUMBER_VALID == 1) @enumToInt(CHANGER_FEATURES.SERIAL_NUMBER_VALID) else 0) | (if (o.STATUS_NON_VOLATILE == 1) @enumToInt(CHANGER_FEATURES.STATUS_NON_VOLATILE) else 0) | (if (o.STORAGE_DRIVE == 1) @enumToInt(CHANGER_FEATURES.STORAGE_DRIVE) else 0) | (if (o.STORAGE_IEPORT == 1) @enumToInt(CHANGER_FEATURES.STORAGE_IEPORT) else 0) | (if (o.STORAGE_SLOT == 1) @enumToInt(CHANGER_FEATURES.STORAGE_SLOT) else 0) | (if (o.STORAGE_TRANSPORT == 1) @enumToInt(CHANGER_FEATURES.STORAGE_TRANSPORT) else 0) | (if (o.VOLUME_ASSERT == 1) @enumToInt(CHANGER_FEATURES.VOLUME_ASSERT) else 0) | (if (o.VOLUME_IDENTIFICATION == 1) @enumToInt(CHANGER_FEATURES.VOLUME_IDENTIFICATION) else 0) | (if (o.VOLUME_REPLACE == 1) @enumToInt(CHANGER_FEATURES.VOLUME_REPLACE) else 0) | (if (o.VOLUME_SEARCH == 1) @enumToInt(CHANGER_FEATURES.VOLUME_SEARCH) else 0) | (if (o.VOLUME_UNDEFINE == 1) @enumToInt(CHANGER_FEATURES.VOLUME_UNDEFINE) else 0) ); } }; pub const CHANGER_BAR_CODE_SCANNER_INSTALLED = CHANGER_FEATURES.BAR_CODE_SCANNER_INSTALLED; pub const CHANGER_CARTRIDGE_MAGAZINE = CHANGER_FEATURES.CARTRIDGE_MAGAZINE; pub const CHANGER_CLEANER_ACCESS_NOT_VALID = CHANGER_FEATURES.CLEANER_ACCESS_NOT_VALID; pub const CHANGER_CLEANER_SLOT = CHANGER_FEATURES.CLEANER_SLOT; pub const CHANGER_CLOSE_IEPORT = CHANGER_FEATURES.CLOSE_IEPORT; pub const CHANGER_DEVICE_REINITIALIZE_CAPABLE = CHANGER_FEATURES.DEVICE_REINITIALIZE_CAPABLE; pub const CHANGER_DRIVE_CLEANING_REQUIRED = CHANGER_FEATURES.DRIVE_CLEANING_REQUIRED; pub const CHANGER_DRIVE_EMPTY_ON_DOOR_ACCESS = CHANGER_FEATURES.DRIVE_EMPTY_ON_DOOR_ACCESS; pub const CHANGER_EXCHANGE_MEDIA = CHANGER_FEATURES.EXCHANGE_MEDIA; pub const CHANGER_INIT_ELEM_STAT_WITH_RANGE = CHANGER_FEATURES.INIT_ELEM_STAT_WITH_RANGE; pub const CHANGER_KEYPAD_ENABLE_DISABLE = CHANGER_FEATURES.KEYPAD_ENABLE_DISABLE; pub const CHANGER_LOCK_UNLOCK = CHANGER_FEATURES.LOCK_UNLOCK; pub const CHANGER_MEDIUM_FLIP = CHANGER_FEATURES.MEDIUM_FLIP; pub const CHANGER_OPEN_IEPORT = CHANGER_FEATURES.OPEN_IEPORT; pub const CHANGER_POSITION_TO_ELEMENT = CHANGER_FEATURES.POSITION_TO_ELEMENT; pub const CHANGER_PREDISMOUNT_EJECT_REQUIRED = CHANGER_FEATURES.PREDISMOUNT_EJECT_REQUIRED; pub const CHANGER_PREMOUNT_EJECT_REQUIRED = CHANGER_FEATURES.PREMOUNT_EJECT_REQUIRED; pub const CHANGER_REPORT_IEPORT_STATE = CHANGER_FEATURES.REPORT_IEPORT_STATE; pub const CHANGER_SERIAL_NUMBER_VALID = CHANGER_FEATURES.SERIAL_NUMBER_VALID; pub const CHANGER_STATUS_NON_VOLATILE = CHANGER_FEATURES.STATUS_NON_VOLATILE; pub const CHANGER_STORAGE_DRIVE = CHANGER_FEATURES.STORAGE_DRIVE; pub const CHANGER_STORAGE_IEPORT = CHANGER_FEATURES.STORAGE_IEPORT; pub const CHANGER_STORAGE_SLOT = CHANGER_FEATURES.STORAGE_SLOT; pub const CHANGER_STORAGE_TRANSPORT = CHANGER_FEATURES.STORAGE_TRANSPORT; pub const CHANGER_VOLUME_ASSERT = CHANGER_FEATURES.VOLUME_ASSERT; pub const CHANGER_VOLUME_IDENTIFICATION = CHANGER_FEATURES.VOLUME_IDENTIFICATION; pub const CHANGER_VOLUME_REPLACE = CHANGER_FEATURES.VOLUME_REPLACE; pub const CHANGER_VOLUME_SEARCH = CHANGER_FEATURES.VOLUME_SEARCH; pub const CHANGER_VOLUME_UNDEFINE = CHANGER_FEATURES.VOLUME_UNDEFINE; pub const TXFS_RMF_LAGS = enum(u32) { LOGGING_MODE = 1, RENAME_RM = 2, LOG_CONTAINER_COUNT_MAX = 4, LOG_CONTAINER_COUNT_MIN = 8, LOG_GROWTH_INCREMENT_NUM_CONTAINERS = 16, LOG_GROWTH_INCREMENT_PERCENT = 32, LOG_AUTO_SHRINK_PERCENTAGE = 64, LOG_NO_CONTAINER_COUNT_MAX = 128, LOG_NO_CONTAINER_COUNT_MIN = 256, GROW_LOG = 1024, SHRINK_LOG = 2048, ENFORCE_MINIMUM_SIZE = 4096, PRESERVE_CHANGES = 8192, RESET_RM_AT_NEXT_START = 16384, DO_NOT_RESET_RM_AT_NEXT_START = 32768, PREFER_CONSISTENCY = 65536, PREFER_AVAILABILITY = 131072, _, pub fn initFlags(o: struct { LOGGING_MODE: u1 = 0, RENAME_RM: u1 = 0, LOG_CONTAINER_COUNT_MAX: u1 = 0, LOG_CONTAINER_COUNT_MIN: u1 = 0, LOG_GROWTH_INCREMENT_NUM_CONTAINERS: u1 = 0, LOG_GROWTH_INCREMENT_PERCENT: u1 = 0, LOG_AUTO_SHRINK_PERCENTAGE: u1 = 0, LOG_NO_CONTAINER_COUNT_MAX: u1 = 0, LOG_NO_CONTAINER_COUNT_MIN: u1 = 0, GROW_LOG: u1 = 0, SHRINK_LOG: u1 = 0, ENFORCE_MINIMUM_SIZE: u1 = 0, PRESERVE_CHANGES: u1 = 0, RESET_RM_AT_NEXT_START: u1 = 0, DO_NOT_RESET_RM_AT_NEXT_START: u1 = 0, PREFER_CONSISTENCY: u1 = 0, PREFER_AVAILABILITY: u1 = 0, }) TXFS_RMF_LAGS { return @intToEnum(TXFS_RMF_LAGS, (if (o.LOGGING_MODE == 1) @enumToInt(TXFS_RMF_LAGS.LOGGING_MODE) else 0) | (if (o.RENAME_RM == 1) @enumToInt(TXFS_RMF_LAGS.RENAME_RM) else 0) | (if (o.LOG_CONTAINER_COUNT_MAX == 1) @enumToInt(TXFS_RMF_LAGS.LOG_CONTAINER_COUNT_MAX) else 0) | (if (o.LOG_CONTAINER_COUNT_MIN == 1) @enumToInt(TXFS_RMF_LAGS.LOG_CONTAINER_COUNT_MIN) else 0) | (if (o.LOG_GROWTH_INCREMENT_NUM_CONTAINERS == 1) @enumToInt(TXFS_RMF_LAGS.LOG_GROWTH_INCREMENT_NUM_CONTAINERS) else 0) | (if (o.LOG_GROWTH_INCREMENT_PERCENT == 1) @enumToInt(TXFS_RMF_LAGS.LOG_GROWTH_INCREMENT_PERCENT) else 0) | (if (o.LOG_AUTO_SHRINK_PERCENTAGE == 1) @enumToInt(TXFS_RMF_LAGS.LOG_AUTO_SHRINK_PERCENTAGE) else 0) | (if (o.LOG_NO_CONTAINER_COUNT_MAX == 1) @enumToInt(TXFS_RMF_LAGS.LOG_NO_CONTAINER_COUNT_MAX) else 0) | (if (o.LOG_NO_CONTAINER_COUNT_MIN == 1) @enumToInt(TXFS_RMF_LAGS.LOG_NO_CONTAINER_COUNT_MIN) else 0) | (if (o.GROW_LOG == 1) @enumToInt(TXFS_RMF_LAGS.GROW_LOG) else 0) | (if (o.SHRINK_LOG == 1) @enumToInt(TXFS_RMF_LAGS.SHRINK_LOG) else 0) | (if (o.ENFORCE_MINIMUM_SIZE == 1) @enumToInt(TXFS_RMF_LAGS.ENFORCE_MINIMUM_SIZE) else 0) | (if (o.PRESERVE_CHANGES == 1) @enumToInt(TXFS_RMF_LAGS.PRESERVE_CHANGES) else 0) | (if (o.RESET_RM_AT_NEXT_START == 1) @enumToInt(TXFS_RMF_LAGS.RESET_RM_AT_NEXT_START) else 0) | (if (o.DO_NOT_RESET_RM_AT_NEXT_START == 1) @enumToInt(TXFS_RMF_LAGS.DO_NOT_RESET_RM_AT_NEXT_START) else 0) | (if (o.PREFER_CONSISTENCY == 1) @enumToInt(TXFS_RMF_LAGS.PREFER_CONSISTENCY) else 0) | (if (o.PREFER_AVAILABILITY == 1) @enumToInt(TXFS_RMF_LAGS.PREFER_AVAILABILITY) else 0) ); } }; pub const TXFS_RM_FLAG_LOGGING_MODE = TXFS_RMF_LAGS.LOGGING_MODE; pub const TXFS_RM_FLAG_RENAME_RM = TXFS_RMF_LAGS.RENAME_RM; pub const TXFS_RM_FLAG_LOG_CONTAINER_COUNT_MAX = TXFS_RMF_LAGS.LOG_CONTAINER_COUNT_MAX; pub const TXFS_RM_FLAG_LOG_CONTAINER_COUNT_MIN = TXFS_RMF_LAGS.LOG_CONTAINER_COUNT_MIN; pub const TXFS_RM_FLAG_LOG_GROWTH_INCREMENT_NUM_CONTAINERS = TXFS_RMF_LAGS.LOG_GROWTH_INCREMENT_NUM_CONTAINERS; pub const TXFS_RM_FLAG_LOG_GROWTH_INCREMENT_PERCENT = TXFS_RMF_LAGS.LOG_GROWTH_INCREMENT_PERCENT; pub const TXFS_RM_FLAG_LOG_AUTO_SHRINK_PERCENTAGE = TXFS_RMF_LAGS.LOG_AUTO_SHRINK_PERCENTAGE; pub const TXFS_RM_FLAG_LOG_NO_CONTAINER_COUNT_MAX = TXFS_RMF_LAGS.LOG_NO_CONTAINER_COUNT_MAX; pub const TXFS_RM_FLAG_LOG_NO_CONTAINER_COUNT_MIN = TXFS_RMF_LAGS.LOG_NO_CONTAINER_COUNT_MIN; pub const TXFS_RM_FLAG_GROW_LOG = TXFS_RMF_LAGS.GROW_LOG; pub const TXFS_RM_FLAG_SHRINK_LOG = TXFS_RMF_LAGS.SHRINK_LOG; pub const TXFS_RM_FLAG_ENFORCE_MINIMUM_SIZE = TXFS_RMF_LAGS.ENFORCE_MINIMUM_SIZE; pub const TXFS_RM_FLAG_PRESERVE_CHANGES = TXFS_RMF_LAGS.PRESERVE_CHANGES; pub const TXFS_RM_FLAG_RESET_RM_AT_NEXT_START = TXFS_RMF_LAGS.RESET_RM_AT_NEXT_START; pub const TXFS_RM_FLAG_DO_NOT_RESET_RM_AT_NEXT_START = TXFS_RMF_LAGS.DO_NOT_RESET_RM_AT_NEXT_START; pub const TXFS_RM_FLAG_PREFER_CONSISTENCY = TXFS_RMF_LAGS.PREFER_CONSISTENCY; pub const TXFS_RM_FLAG_PREFER_AVAILABILITY = TXFS_RMF_LAGS.PREFER_AVAILABILITY; pub const FILESYSTEM_STATISTICS_TYPE = enum(u16) { EXFAT = 3, FAT = 2, NTFS = 1, }; pub const FILESYSTEM_STATISTICS_TYPE_EXFAT = FILESYSTEM_STATISTICS_TYPE.EXFAT; pub const FILESYSTEM_STATISTICS_TYPE_FAT = FILESYSTEM_STATISTICS_TYPE.FAT; pub const FILESYSTEM_STATISTICS_TYPE_NTFS = FILESYSTEM_STATISTICS_TYPE.NTFS; pub const USN_SOURCE_INFO_ID = enum(u32) { AUXILIARY_DATA = 2, DATA_MANAGEMENT = 1, REPLICATION_MANAGEMENT = 4, CLIENT_REPLICATION_MANAGEMENT = 8, }; pub const USN_SOURCE_AUXILIARY_DATA = USN_SOURCE_INFO_ID.AUXILIARY_DATA; pub const USN_SOURCE_DATA_MANAGEMENT = USN_SOURCE_INFO_ID.DATA_MANAGEMENT; pub const USN_SOURCE_REPLICATION_MANAGEMENT = USN_SOURCE_INFO_ID.REPLICATION_MANAGEMENT; pub const USN_SOURCE_CLIENT_REPLICATION_MANAGEMENT = USN_SOURCE_INFO_ID.CLIENT_REPLICATION_MANAGEMENT; pub const FILE_STORAGE_TIER_FLAGS = enum(u32) { Y = 131072, _, pub fn initFlags(o: struct { Y: u1 = 0, }) FILE_STORAGE_TIER_FLAGS { return @intToEnum(FILE_STORAGE_TIER_FLAGS, (if (o.Y == 1) @enumToInt(FILE_STORAGE_TIER_FLAGS.Y) else 0) ); } }; pub const FILE_STORAGE_TIER_FLAG_NO_SEEK_PENALTY = FILE_STORAGE_TIER_FLAGS.Y; pub const CHANGER_ELEMENT_STATUS_FLAGS = enum(u32) { ACCESS = 8, AVOLTAG = 536870912, EXCEPT = 4, EXENAB = 16, FULL = 1, ID_VALID = 8192, IMPEXP = 2, INENAB = 32, INVERT = 4194304, LUN_VALID = 4096, NOT_BUS = 32768, PVOLTAG = 268435456, SVALID = 8388608, PRODUCT_DATA = 64, _, pub fn initFlags(o: struct { ACCESS: u1 = 0, AVOLTAG: u1 = 0, EXCEPT: u1 = 0, EXENAB: u1 = 0, FULL: u1 = 0, ID_VALID: u1 = 0, IMPEXP: u1 = 0, INENAB: u1 = 0, INVERT: u1 = 0, LUN_VALID: u1 = 0, NOT_BUS: u1 = 0, PVOLTAG: u1 = 0, SVALID: u1 = 0, PRODUCT_DATA: u1 = 0, }) CHANGER_ELEMENT_STATUS_FLAGS { return @intToEnum(CHANGER_ELEMENT_STATUS_FLAGS, (if (o.ACCESS == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.ACCESS) else 0) | (if (o.AVOLTAG == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.AVOLTAG) else 0) | (if (o.EXCEPT == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.EXCEPT) else 0) | (if (o.EXENAB == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.EXENAB) else 0) | (if (o.FULL == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.FULL) else 0) | (if (o.ID_VALID == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.ID_VALID) else 0) | (if (o.IMPEXP == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.IMPEXP) else 0) | (if (o.INENAB == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.INENAB) else 0) | (if (o.INVERT == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.INVERT) else 0) | (if (o.LUN_VALID == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.LUN_VALID) else 0) | (if (o.NOT_BUS == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.NOT_BUS) else 0) | (if (o.PVOLTAG == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.PVOLTAG) else 0) | (if (o.SVALID == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.SVALID) else 0) | (if (o.PRODUCT_DATA == 1) @enumToInt(CHANGER_ELEMENT_STATUS_FLAGS.PRODUCT_DATA) else 0) ); } }; pub const ELEMENT_STATUS_ACCESS = CHANGER_ELEMENT_STATUS_FLAGS.ACCESS; pub const ELEMENT_STATUS_AVOLTAG = CHANGER_ELEMENT_STATUS_FLAGS.AVOLTAG; pub const ELEMENT_STATUS_EXCEPT = CHANGER_ELEMENT_STATUS_FLAGS.EXCEPT; pub const ELEMENT_STATUS_EXENAB = CHANGER_ELEMENT_STATUS_FLAGS.EXENAB; pub const ELEMENT_STATUS_FULL = CHANGER_ELEMENT_STATUS_FLAGS.FULL; pub const ELEMENT_STATUS_ID_VALID = CHANGER_ELEMENT_STATUS_FLAGS.ID_VALID; pub const ELEMENT_STATUS_IMPEXP = CHANGER_ELEMENT_STATUS_FLAGS.IMPEXP; pub const ELEMENT_STATUS_INENAB = CHANGER_ELEMENT_STATUS_FLAGS.INENAB; pub const ELEMENT_STATUS_INVERT = CHANGER_ELEMENT_STATUS_FLAGS.INVERT; pub const ELEMENT_STATUS_LUN_VALID = CHANGER_ELEMENT_STATUS_FLAGS.LUN_VALID; pub const ELEMENT_STATUS_NOT_BUS = CHANGER_ELEMENT_STATUS_FLAGS.NOT_BUS; pub const ELEMENT_STATUS_PVOLTAG = CHANGER_ELEMENT_STATUS_FLAGS.PVOLTAG; pub const ELEMENT_STATUS_SVALID = CHANGER_ELEMENT_STATUS_FLAGS.SVALID; pub const ELEMENT_STATUS_PRODUCT_DATA = CHANGER_ELEMENT_STATUS_FLAGS.PRODUCT_DATA; pub const GET_CHANGER_PARAMETERS_FEATURES1 = enum(u32) { CLEANER_AUTODISMOUNT = 2147483652, CLEANER_OPS_NOT_SUPPORTED = 2147483712, IEPORT_USER_CONTROL_CLOSE = 2147483904, IEPORT_USER_CONTROL_OPEN = 2147483776, MOVE_EXTENDS_IEPORT = 2147484160, MOVE_RETRACTS_IEPORT = 2147484672, PREDISMOUNT_ALIGN_TO_DRIVE = 2147483650, PREDISMOUNT_ALIGN_TO_SLOT = 2147483649, RTN_MEDIA_TO_ORIGINAL_ADDR = 2147483680, SLOTS_USE_TRAYS = 2147483664, TRUE_EXCHANGE_CAPABLE = 2147483656, _, pub fn initFlags(o: struct { CLEANER_AUTODISMOUNT: u1 = 0, CLEANER_OPS_NOT_SUPPORTED: u1 = 0, IEPORT_USER_CONTROL_CLOSE: u1 = 0, IEPORT_USER_CONTROL_OPEN: u1 = 0, MOVE_EXTENDS_IEPORT: u1 = 0, MOVE_RETRACTS_IEPORT: u1 = 0, PREDISMOUNT_ALIGN_TO_DRIVE: u1 = 0, PREDISMOUNT_ALIGN_TO_SLOT: u1 = 0, RTN_MEDIA_TO_ORIGINAL_ADDR: u1 = 0, SLOTS_USE_TRAYS: u1 = 0, TRUE_EXCHANGE_CAPABLE: u1 = 0, }) GET_CHANGER_PARAMETERS_FEATURES1 { return @intToEnum(GET_CHANGER_PARAMETERS_FEATURES1, (if (o.CLEANER_AUTODISMOUNT == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.CLEANER_AUTODISMOUNT) else 0) | (if (o.CLEANER_OPS_NOT_SUPPORTED == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.CLEANER_OPS_NOT_SUPPORTED) else 0) | (if (o.IEPORT_USER_CONTROL_CLOSE == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.IEPORT_USER_CONTROL_CLOSE) else 0) | (if (o.IEPORT_USER_CONTROL_OPEN == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.IEPORT_USER_CONTROL_OPEN) else 0) | (if (o.MOVE_EXTENDS_IEPORT == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.MOVE_EXTENDS_IEPORT) else 0) | (if (o.MOVE_RETRACTS_IEPORT == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.MOVE_RETRACTS_IEPORT) else 0) | (if (o.PREDISMOUNT_ALIGN_TO_DRIVE == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.PREDISMOUNT_ALIGN_TO_DRIVE) else 0) | (if (o.PREDISMOUNT_ALIGN_TO_SLOT == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.PREDISMOUNT_ALIGN_TO_SLOT) else 0) | (if (o.RTN_MEDIA_TO_ORIGINAL_ADDR == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.RTN_MEDIA_TO_ORIGINAL_ADDR) else 0) | (if (o.SLOTS_USE_TRAYS == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.SLOTS_USE_TRAYS) else 0) | (if (o.TRUE_EXCHANGE_CAPABLE == 1) @enumToInt(GET_CHANGER_PARAMETERS_FEATURES1.TRUE_EXCHANGE_CAPABLE) else 0) ); } }; pub const CHANGER_CLEANER_AUTODISMOUNT = GET_CHANGER_PARAMETERS_FEATURES1.CLEANER_AUTODISMOUNT; pub const CHANGER_CLEANER_OPS_NOT_SUPPORTED = GET_CHANGER_PARAMETERS_FEATURES1.CLEANER_OPS_NOT_SUPPORTED; pub const CHANGER_IEPORT_USER_CONTROL_CLOSE = GET_CHANGER_PARAMETERS_FEATURES1.IEPORT_USER_CONTROL_CLOSE; pub const CHANGER_IEPORT_USER_CONTROL_OPEN = GET_CHANGER_PARAMETERS_FEATURES1.IEPORT_USER_CONTROL_OPEN; pub const CHANGER_MOVE_EXTENDS_IEPORT = GET_CHANGER_PARAMETERS_FEATURES1.MOVE_EXTENDS_IEPORT; pub const CHANGER_MOVE_RETRACTS_IEPORT = GET_CHANGER_PARAMETERS_FEATURES1.MOVE_RETRACTS_IEPORT; pub const CHANGER_PREDISMOUNT_ALIGN_TO_DRIVE = GET_CHANGER_PARAMETERS_FEATURES1.PREDISMOUNT_ALIGN_TO_DRIVE; pub const CHANGER_PREDISMOUNT_ALIGN_TO_SLOT = GET_CHANGER_PARAMETERS_FEATURES1.PREDISMOUNT_ALIGN_TO_SLOT; pub const CHANGER_RTN_MEDIA_TO_ORIGINAL_ADDR = GET_CHANGER_PARAMETERS_FEATURES1.RTN_MEDIA_TO_ORIGINAL_ADDR; pub const CHANGER_SLOTS_USE_TRAYS = GET_CHANGER_PARAMETERS_FEATURES1.SLOTS_USE_TRAYS; pub const CHANGER_TRUE_EXCHANGE_CAPABLE = GET_CHANGER_PARAMETERS_FEATURES1.TRUE_EXCHANGE_CAPABLE; pub const STORAGE_HOTPLUG_INFO = extern struct { Size: u32, MediaRemovable: BOOLEAN, MediaHotplug: BOOLEAN, DeviceHotplug: BOOLEAN, WriteCacheEnableOverride: BOOLEAN, }; pub const STORAGE_DEVICE_NUMBER = extern struct { DeviceType: u32, DeviceNumber: u32, PartitionNumber: u32, }; pub const STORAGE_DEVICE_NUMBERS = extern struct { Version: u32, Size: u32, NumberOfDevices: u32, Devices: [1]STORAGE_DEVICE_NUMBER, }; pub const STORAGE_DEVICE_NUMBER_EX = extern struct { Version: u32, Size: u32, Flags: u32, DeviceType: u32, DeviceNumber: u32, DeviceGuid: Guid, PartitionNumber: u32, }; pub const STORAGE_BUS_RESET_REQUEST = extern struct { PathId: u8, }; pub const STORAGE_BREAK_RESERVATION_REQUEST = extern struct { Length: u32, _unused: u8, PathId: u8, TargetId: u8, Lun: u8, }; pub const PREVENT_MEDIA_REMOVAL = extern struct { PreventMediaRemoval: BOOLEAN, }; pub const CLASS_MEDIA_CHANGE_CONTEXT = extern struct { MediaChangeCount: u32, NewState: u32, }; pub const TAPE_STATISTICS = extern struct { Version: u32, Flags: u32, RecoveredWrites: LARGE_INTEGER, UnrecoveredWrites: LARGE_INTEGER, RecoveredReads: LARGE_INTEGER, UnrecoveredReads: LARGE_INTEGER, CompressionRatioReads: u8, CompressionRatioWrites: u8, }; pub const TAPE_GET_STATISTICS = extern struct { Operation: u32, }; pub const STORAGE_MEDIA_TYPE = enum(i32) { DDS_4mm = 32, MiniQic = 33, Travan = 34, QIC = 35, MP_8mm = 36, AME_8mm = 37, AIT1_8mm = 38, DLT = 39, NCTP = 40, IBM_3480 = 41, IBM_3490E = 42, IBM_Magstar_3590 = 43, IBM_Magstar_MP = 44, STK_DATA_D3 = 45, SONY_DTF = 46, DV_6mm = 47, DMI = 48, SONY_D2 = 49, CLEANER_CARTRIDGE = 50, CD_ROM = 51, CD_R = 52, CD_RW = 53, DVD_ROM = 54, DVD_R = 55, DVD_RW = 56, MO_3_RW = 57, MO_5_WO = 58, MO_5_RW = 59, MO_5_LIMDOW = 60, PC_5_WO = 61, PC_5_RW = 62, PD_5_RW = 63, ABL_5_WO = 64, PINNACLE_APEX_5_RW = 65, SONY_12_WO = 66, PHILIPS_12_WO = 67, HITACHI_12_WO = 68, CYGNET_12_WO = 69, KODAK_14_WO = 70, MO_NFR_525 = 71, NIKON_12_RW = 72, IOMEGA_ZIP = 73, IOMEGA_JAZ = 74, SYQUEST_EZ135 = 75, SYQUEST_EZFLYER = 76, SYQUEST_SYJET = 77, AVATAR_F2 = 78, MP2_8mm = 79, DST_S = 80, DST_M = 81, DST_L = 82, VXATape_1 = 83, VXATape_2 = 84, STK_9840 = 85, LTO_Ultrium = 86, LTO_Accelis = 87, DVD_RAM = 88, AIT_8mm = 89, ADR_1 = 90, ADR_2 = 91, STK_9940 = 92, SAIT = 93, VXATape = 94, }; pub const DDS_4mm = STORAGE_MEDIA_TYPE.DDS_4mm; pub const MiniQic = STORAGE_MEDIA_TYPE.MiniQic; pub const Travan = STORAGE_MEDIA_TYPE.Travan; pub const QIC = STORAGE_MEDIA_TYPE.QIC; pub const MP_8mm = STORAGE_MEDIA_TYPE.MP_8mm; pub const AME_8mm = STORAGE_MEDIA_TYPE.AME_8mm; pub const AIT1_8mm = STORAGE_MEDIA_TYPE.AIT1_8mm; pub const DLT = STORAGE_MEDIA_TYPE.DLT; pub const NCTP = STORAGE_MEDIA_TYPE.NCTP; pub const IBM_3480 = STORAGE_MEDIA_TYPE.IBM_3480; pub const IBM_3490E = STORAGE_MEDIA_TYPE.IBM_3490E; pub const IBM_Magstar_3590 = STORAGE_MEDIA_TYPE.IBM_Magstar_3590; pub const IBM_Magstar_MP = STORAGE_MEDIA_TYPE.IBM_Magstar_MP; pub const STK_DATA_D3 = STORAGE_MEDIA_TYPE.STK_DATA_D3; pub const SONY_DTF = STORAGE_MEDIA_TYPE.SONY_DTF; pub const DV_6mm = STORAGE_MEDIA_TYPE.DV_6mm; pub const DMI = STORAGE_MEDIA_TYPE.DMI; pub const SONY_D2 = STORAGE_MEDIA_TYPE.SONY_D2; pub const CLEANER_CARTRIDGE = STORAGE_MEDIA_TYPE.CLEANER_CARTRIDGE; pub const CD_ROM = STORAGE_MEDIA_TYPE.CD_ROM; pub const CD_R = STORAGE_MEDIA_TYPE.CD_R; pub const CD_RW = STORAGE_MEDIA_TYPE.CD_RW; pub const DVD_ROM = STORAGE_MEDIA_TYPE.DVD_ROM; pub const DVD_R = STORAGE_MEDIA_TYPE.DVD_R; pub const DVD_RW = STORAGE_MEDIA_TYPE.DVD_RW; pub const MO_3_RW = STORAGE_MEDIA_TYPE.MO_3_RW; pub const MO_5_WO = STORAGE_MEDIA_TYPE.MO_5_WO; pub const MO_5_RW = STORAGE_MEDIA_TYPE.MO_5_RW; pub const MO_5_LIMDOW = STORAGE_MEDIA_TYPE.MO_5_LIMDOW; pub const PC_5_WO = STORAGE_MEDIA_TYPE.PC_5_WO; pub const PC_5_RW = STORAGE_MEDIA_TYPE.PC_5_RW; pub const PD_5_RW = STORAGE_MEDIA_TYPE.PD_5_RW; pub const ABL_5_WO = STORAGE_MEDIA_TYPE.ABL_5_WO; pub const PINNACLE_APEX_5_RW = STORAGE_MEDIA_TYPE.PINNACLE_APEX_5_RW; pub const SONY_12_WO = STORAGE_MEDIA_TYPE.SONY_12_WO; pub const PHILIPS_12_WO = STORAGE_MEDIA_TYPE.PHILIPS_12_WO; pub const HITACHI_12_WO = STORAGE_MEDIA_TYPE.HITACHI_12_WO; pub const CYGNET_12_WO = STORAGE_MEDIA_TYPE.CYGNET_12_WO; pub const KODAK_14_WO = STORAGE_MEDIA_TYPE.KODAK_14_WO; pub const MO_NFR_525 = STORAGE_MEDIA_TYPE.MO_NFR_525; pub const NIKON_12_RW = STORAGE_MEDIA_TYPE.NIKON_12_RW; pub const IOMEGA_ZIP = STORAGE_MEDIA_TYPE.IOMEGA_ZIP; pub const IOMEGA_JAZ = STORAGE_MEDIA_TYPE.IOMEGA_JAZ; pub const SYQUEST_EZ135 = STORAGE_MEDIA_TYPE.SYQUEST_EZ135; pub const SYQUEST_EZFLYER = STORAGE_MEDIA_TYPE.SYQUEST_EZFLYER; pub const SYQUEST_SYJET = STORAGE_MEDIA_TYPE.SYQUEST_SYJET; pub const AVATAR_F2 = STORAGE_MEDIA_TYPE.AVATAR_F2; pub const MP2_8mm = STORAGE_MEDIA_TYPE.MP2_8mm; pub const DST_S = STORAGE_MEDIA_TYPE.DST_S; pub const DST_M = STORAGE_MEDIA_TYPE.DST_M; pub const DST_L = STORAGE_MEDIA_TYPE.DST_L; pub const VXATape_1 = STORAGE_MEDIA_TYPE.VXATape_1; pub const VXATape_2 = STORAGE_MEDIA_TYPE.VXATape_2; pub const STK_9840 = STORAGE_MEDIA_TYPE.STK_9840; pub const LTO_Ultrium = STORAGE_MEDIA_TYPE.LTO_Ultrium; pub const LTO_Accelis = STORAGE_MEDIA_TYPE.LTO_Accelis; pub const DVD_RAM = STORAGE_MEDIA_TYPE.DVD_RAM; pub const AIT_8mm = STORAGE_MEDIA_TYPE.AIT_8mm; pub const ADR_1 = STORAGE_MEDIA_TYPE.ADR_1; pub const ADR_2 = STORAGE_MEDIA_TYPE.ADR_2; pub const STK_9940 = STORAGE_MEDIA_TYPE.STK_9940; pub const SAIT = STORAGE_MEDIA_TYPE.SAIT; pub const VXATape = STORAGE_MEDIA_TYPE.VXATape; pub const DEVICE_MEDIA_INFO = extern struct { DeviceSpecific: extern union { DiskInfo: extern struct { Cylinders: LARGE_INTEGER, MediaType: STORAGE_MEDIA_TYPE, TracksPerCylinder: u32, SectorsPerTrack: u32, BytesPerSector: u32, NumberMediaSides: u32, MediaCharacteristics: u32, }, RemovableDiskInfo: extern struct { Cylinders: LARGE_INTEGER, MediaType: STORAGE_MEDIA_TYPE, TracksPerCylinder: u32, SectorsPerTrack: u32, BytesPerSector: u32, NumberMediaSides: u32, MediaCharacteristics: u32, }, TapeInfo: extern struct { MediaType: STORAGE_MEDIA_TYPE, MediaCharacteristics: u32, CurrentBlockSize: u32, BusType: STORAGE_BUS_TYPE, BusSpecificData: extern union { ScsiInformation: extern struct { MediumType: u8, DensityCode: u8, }, }, }, }, }; pub const GET_MEDIA_TYPES = extern struct { DeviceType: u32, MediaInfoCount: u32, MediaInfo: [1]DEVICE_MEDIA_INFO, }; pub const STORAGE_PREDICT_FAILURE = extern struct { PredictFailure: u32, VendorSpecific: [512]u8, }; pub const STORAGE_FAILURE_PREDICTION_CONFIG = extern struct { Version: u32, Size: u32, Set: BOOLEAN, Enabled: BOOLEAN, Reserved: u16, }; pub const STORAGE_QUERY_TYPE = enum(i32) { StandardQuery = 0, ExistsQuery = 1, MaskQuery = 2, QueryMaxDefined = 3, }; pub const PropertyStandardQuery = STORAGE_QUERY_TYPE.StandardQuery; pub const PropertyExistsQuery = STORAGE_QUERY_TYPE.ExistsQuery; pub const PropertyMaskQuery = STORAGE_QUERY_TYPE.MaskQuery; pub const PropertyQueryMaxDefined = STORAGE_QUERY_TYPE.QueryMaxDefined; pub const STORAGE_SET_TYPE = enum(i32) { StandardSet = 0, ExistsSet = 1, SetMaxDefined = 2, }; pub const PropertyStandardSet = STORAGE_SET_TYPE.StandardSet; pub const PropertyExistsSet = STORAGE_SET_TYPE.ExistsSet; pub const PropertySetMaxDefined = STORAGE_SET_TYPE.SetMaxDefined; pub const STORAGE_PROPERTY_ID = enum(i32) { DeviceProperty = 0, AdapterProperty = 1, DeviceIdProperty = 2, DeviceUniqueIdProperty = 3, DeviceWriteCacheProperty = 4, MiniportProperty = 5, AccessAlignmentProperty = 6, DeviceSeekPenaltyProperty = 7, DeviceTrimProperty = 8, DeviceWriteAggregationProperty = 9, DeviceDeviceTelemetryProperty = 10, DeviceLBProvisioningProperty = 11, DevicePowerProperty = 12, DeviceCopyOffloadProperty = 13, DeviceResiliencyProperty = 14, DeviceMediumProductType = 15, AdapterRpmbProperty = 16, AdapterCryptoProperty = 17, DeviceIoCapabilityProperty = 48, AdapterProtocolSpecificProperty = 49, DeviceProtocolSpecificProperty = 50, AdapterTemperatureProperty = 51, DeviceTemperatureProperty = 52, AdapterPhysicalTopologyProperty = 53, DevicePhysicalTopologyProperty = 54, DeviceAttributesProperty = 55, DeviceManagementStatus = 56, AdapterSerialNumberProperty = 57, DeviceLocationProperty = 58, DeviceNumaProperty = 59, DeviceZonedDeviceProperty = 60, DeviceUnsafeShutdownCount = 61, DeviceEnduranceProperty = 62, DeviceLedStateProperty = 63, DeviceSelfEncryptionProperty = 64, FruIdProperty = 65, }; pub const StorageDeviceProperty = STORAGE_PROPERTY_ID.DeviceProperty; pub const StorageAdapterProperty = STORAGE_PROPERTY_ID.AdapterProperty; pub const StorageDeviceIdProperty = STORAGE_PROPERTY_ID.DeviceIdProperty; pub const StorageDeviceUniqueIdProperty = STORAGE_PROPERTY_ID.DeviceUniqueIdProperty; pub const StorageDeviceWriteCacheProperty = STORAGE_PROPERTY_ID.DeviceWriteCacheProperty; pub const StorageMiniportProperty = STORAGE_PROPERTY_ID.MiniportProperty; pub const StorageAccessAlignmentProperty = STORAGE_PROPERTY_ID.AccessAlignmentProperty; pub const StorageDeviceSeekPenaltyProperty = STORAGE_PROPERTY_ID.DeviceSeekPenaltyProperty; pub const StorageDeviceTrimProperty = STORAGE_PROPERTY_ID.DeviceTrimProperty; pub const StorageDeviceWriteAggregationProperty = STORAGE_PROPERTY_ID.DeviceWriteAggregationProperty; pub const StorageDeviceDeviceTelemetryProperty = STORAGE_PROPERTY_ID.DeviceDeviceTelemetryProperty; pub const StorageDeviceLBProvisioningProperty = STORAGE_PROPERTY_ID.DeviceLBProvisioningProperty; pub const StorageDevicePowerProperty = STORAGE_PROPERTY_ID.DevicePowerProperty; pub const StorageDeviceCopyOffloadProperty = STORAGE_PROPERTY_ID.DeviceCopyOffloadProperty; pub const StorageDeviceResiliencyProperty = STORAGE_PROPERTY_ID.DeviceResiliencyProperty; pub const StorageDeviceMediumProductType = STORAGE_PROPERTY_ID.DeviceMediumProductType; pub const StorageAdapterRpmbProperty = STORAGE_PROPERTY_ID.AdapterRpmbProperty; pub const StorageAdapterCryptoProperty = STORAGE_PROPERTY_ID.AdapterCryptoProperty; pub const StorageDeviceIoCapabilityProperty = STORAGE_PROPERTY_ID.DeviceIoCapabilityProperty; pub const StorageAdapterProtocolSpecificProperty = STORAGE_PROPERTY_ID.AdapterProtocolSpecificProperty; pub const StorageDeviceProtocolSpecificProperty = STORAGE_PROPERTY_ID.DeviceProtocolSpecificProperty; pub const StorageAdapterTemperatureProperty = STORAGE_PROPERTY_ID.AdapterTemperatureProperty; pub const StorageDeviceTemperatureProperty = STORAGE_PROPERTY_ID.DeviceTemperatureProperty; pub const StorageAdapterPhysicalTopologyProperty = STORAGE_PROPERTY_ID.AdapterPhysicalTopologyProperty; pub const StorageDevicePhysicalTopologyProperty = STORAGE_PROPERTY_ID.DevicePhysicalTopologyProperty; pub const StorageDeviceAttributesProperty = STORAGE_PROPERTY_ID.DeviceAttributesProperty; pub const StorageDeviceManagementStatus = STORAGE_PROPERTY_ID.DeviceManagementStatus; pub const StorageAdapterSerialNumberProperty = STORAGE_PROPERTY_ID.AdapterSerialNumberProperty; pub const StorageDeviceLocationProperty = STORAGE_PROPERTY_ID.DeviceLocationProperty; pub const StorageDeviceNumaProperty = STORAGE_PROPERTY_ID.DeviceNumaProperty; pub const StorageDeviceZonedDeviceProperty = STORAGE_PROPERTY_ID.DeviceZonedDeviceProperty; pub const StorageDeviceUnsafeShutdownCount = STORAGE_PROPERTY_ID.DeviceUnsafeShutdownCount; pub const StorageDeviceEnduranceProperty = STORAGE_PROPERTY_ID.DeviceEnduranceProperty; pub const StorageDeviceLedStateProperty = STORAGE_PROPERTY_ID.DeviceLedStateProperty; pub const StorageDeviceSelfEncryptionProperty = STORAGE_PROPERTY_ID.DeviceSelfEncryptionProperty; pub const StorageFruIdProperty = STORAGE_PROPERTY_ID.FruIdProperty; pub const STORAGE_PROPERTY_QUERY = extern struct { PropertyId: STORAGE_PROPERTY_ID, QueryType: STORAGE_QUERY_TYPE, AdditionalParameters: [1]u8, }; pub const STORAGE_PROPERTY_SET = extern struct { PropertyId: STORAGE_PROPERTY_ID, SetType: STORAGE_SET_TYPE, AdditionalParameters: [1]u8, }; pub const STORAGE_DESCRIPTOR_HEADER = extern struct { Version: u32, Size: u32, }; pub const STORAGE_DEVICE_DESCRIPTOR = extern struct { Version: u32, Size: u32, DeviceType: u8, DeviceTypeModifier: u8, RemovableMedia: BOOLEAN, CommandQueueing: BOOLEAN, VendorIdOffset: u32, ProductIdOffset: u32, ProductRevisionOffset: u32, SerialNumberOffset: u32, BusType: STORAGE_BUS_TYPE, RawPropertiesLength: u32, RawDeviceProperties: [1]u8, }; pub const STORAGE_ADAPTER_DESCRIPTOR = extern struct { Version: u32, Size: u32, MaximumTransferLength: u32, MaximumPhysicalPages: u32, AlignmentMask: u32, AdapterUsesPio: BOOLEAN, AdapterScansDown: BOOLEAN, CommandQueueing: BOOLEAN, AcceleratedTransfer: BOOLEAN, BusType: u8, BusMajorVersion: u16, BusMinorVersion: u16, SrbType: u8, AddressType: u8, }; pub const STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR = extern struct { Version: u32, Size: u32, BytesPerCacheLine: u32, BytesOffsetForCacheAlignment: u32, BytesPerLogicalSector: u32, BytesPerPhysicalSector: u32, BytesOffsetForSectorAlignment: u32, }; pub const STORAGE_MEDIUM_PRODUCT_TYPE_DESCRIPTOR = extern struct { Version: u32, Size: u32, MediumProductType: u32, }; pub const STORAGE_PORT_CODE_SET = enum(i32) { Reserved = 0, Storport = 1, SCSIport = 2, Spaceport = 3, ATAport = 4, USBport = 5, SBP2port = 6, SDport = 7, }; pub const StoragePortCodeSetReserved = STORAGE_PORT_CODE_SET.Reserved; pub const StoragePortCodeSetStorport = STORAGE_PORT_CODE_SET.Storport; pub const StoragePortCodeSetSCSIport = STORAGE_PORT_CODE_SET.SCSIport; pub const StoragePortCodeSetSpaceport = STORAGE_PORT_CODE_SET.Spaceport; pub const StoragePortCodeSetATAport = STORAGE_PORT_CODE_SET.ATAport; pub const StoragePortCodeSetUSBport = STORAGE_PORT_CODE_SET.USBport; pub const StoragePortCodeSetSBP2port = STORAGE_PORT_CODE_SET.SBP2port; pub const StoragePortCodeSetSDport = STORAGE_PORT_CODE_SET.SDport; pub const STORAGE_MINIPORT_DESCRIPTOR = extern struct { Version: u32, Size: u32, Portdriver: STORAGE_PORT_CODE_SET, LUNResetSupported: BOOLEAN, TargetResetSupported: BOOLEAN, IoTimeoutValue: u16, ExtraIoInfoSupported: BOOLEAN, Flags: extern union { Anonymous: extern struct { _bitfield: u8, }, AsBYTE: u8, }, Reserved0: [2]u8, Reserved1: u32, }; pub const STORAGE_IDENTIFIER_CODE_SET = enum(i32) { Reserved = 0, Binary = 1, Ascii = 2, Utf8 = 3, }; pub const StorageIdCodeSetReserved = STORAGE_IDENTIFIER_CODE_SET.Reserved; pub const StorageIdCodeSetBinary = STORAGE_IDENTIFIER_CODE_SET.Binary; pub const StorageIdCodeSetAscii = STORAGE_IDENTIFIER_CODE_SET.Ascii; pub const StorageIdCodeSetUtf8 = STORAGE_IDENTIFIER_CODE_SET.Utf8; pub const STORAGE_IDENTIFIER_TYPE = enum(i32) { VendorSpecific = 0, VendorId = 1, EUI64 = 2, FCPHName = 3, PortRelative = 4, TargetPortGroup = 5, LogicalUnitGroup = 6, MD5LogicalUnitIdentifier = 7, ScsiNameString = 8, }; pub const StorageIdTypeVendorSpecific = STORAGE_IDENTIFIER_TYPE.VendorSpecific; pub const StorageIdTypeVendorId = STORAGE_IDENTIFIER_TYPE.VendorId; pub const StorageIdTypeEUI64 = STORAGE_IDENTIFIER_TYPE.EUI64; pub const StorageIdTypeFCPHName = STORAGE_IDENTIFIER_TYPE.FCPHName; pub const StorageIdTypePortRelative = STORAGE_IDENTIFIER_TYPE.PortRelative; pub const StorageIdTypeTargetPortGroup = STORAGE_IDENTIFIER_TYPE.TargetPortGroup; pub const StorageIdTypeLogicalUnitGroup = STORAGE_IDENTIFIER_TYPE.LogicalUnitGroup; pub const StorageIdTypeMD5LogicalUnitIdentifier = STORAGE_IDENTIFIER_TYPE.MD5LogicalUnitIdentifier; pub const StorageIdTypeScsiNameString = STORAGE_IDENTIFIER_TYPE.ScsiNameString; pub const STORAGE_ID_NAA_FORMAT = enum(i32) { Extended = 2, Registered = 3, ERegisteredExtended = 5, }; pub const StorageIdNAAFormatIEEEExtended = STORAGE_ID_NAA_FORMAT.Extended; pub const StorageIdNAAFormatIEEERegistered = STORAGE_ID_NAA_FORMAT.Registered; pub const StorageIdNAAFormatIEEEERegisteredExtended = STORAGE_ID_NAA_FORMAT.ERegisteredExtended; pub const STORAGE_ASSOCIATION_TYPE = enum(i32) { Device = 0, Port = 1, Target = 2, }; pub const StorageIdAssocDevice = STORAGE_ASSOCIATION_TYPE.Device; pub const StorageIdAssocPort = STORAGE_ASSOCIATION_TYPE.Port; pub const StorageIdAssocTarget = STORAGE_ASSOCIATION_TYPE.Target; pub const STORAGE_IDENTIFIER = extern struct { CodeSet: STORAGE_IDENTIFIER_CODE_SET, Type: STORAGE_IDENTIFIER_TYPE, IdentifierSize: u16, NextOffset: u16, Association: STORAGE_ASSOCIATION_TYPE, Identifier: [1]u8, }; pub const STORAGE_DEVICE_ID_DESCRIPTOR = extern struct { Version: u32, Size: u32, NumberOfIdentifiers: u32, Identifiers: [1]u8, }; pub const DEVICE_SEEK_PENALTY_DESCRIPTOR = extern struct { Version: u32, Size: u32, IncursSeekPenalty: BOOLEAN, }; pub const DEVICE_WRITE_AGGREGATION_DESCRIPTOR = extern struct { Version: u32, Size: u32, BenefitsFromWriteAggregation: BOOLEAN, }; pub const DEVICE_TRIM_DESCRIPTOR = extern struct { Version: u32, Size: u32, TrimEnabled: BOOLEAN, }; pub const DEVICE_LB_PROVISIONING_DESCRIPTOR = extern struct { Version: u32, Size: u32, _bitfield: u8, Reserved1: [7]u8, OptimalUnmapGranularity: u64, UnmapGranularityAlignment: u64, MaxUnmapLbaCount: u32, MaxUnmapBlockDescriptorCount: u32, }; pub const STORAGE_LB_PROVISIONING_MAP_RESOURCES = extern struct { Size: u32, Version: u32, _bitfield1: u8, Reserved1: [3]u8, _bitfield2: u8, Reserved3: [3]u8, AvailableMappingResources: u64, UsedMappingResources: u64, }; pub const DEVICE_POWER_DESCRIPTOR = extern struct { Version: u32, Size: u32, DeviceAttentionSupported: BOOLEAN, AsynchronousNotificationSupported: BOOLEAN, IdlePowerManagementEnabled: BOOLEAN, D3ColdEnabled: BOOLEAN, D3ColdSupported: BOOLEAN, NoVerifyDuringIdlePower: BOOLEAN, Reserved: [2]u8, IdleTimeoutInMS: u32, }; pub const DEVICE_COPY_OFFLOAD_DESCRIPTOR = extern struct { Version: u32, Size: u32, MaximumTokenLifetime: u32, DefaultTokenLifetime: u32, MaximumTransferSize: u64, OptimalTransferCount: u64, MaximumDataDescriptors: u32, MaximumTransferLengthPerDescriptor: u32, OptimalTransferLengthPerDescriptor: u32, OptimalTransferLengthGranularity: u16, Reserved: [2]u8, }; pub const STORAGE_DEVICE_RESILIENCY_DESCRIPTOR = extern struct { Version: u32, Size: u32, NameOffset: u32, NumberOfLogicalCopies: u32, NumberOfPhysicalCopies: u32, PhysicalDiskRedundancy: u32, NumberOfColumns: u32, Interleave: u32, }; pub const STORAGE_RPMB_FRAME_TYPE = enum(i32) { Unknown = 0, Standard = 1, Max = 2, }; pub const StorageRpmbFrameTypeUnknown = STORAGE_RPMB_FRAME_TYPE.Unknown; pub const StorageRpmbFrameTypeStandard = STORAGE_RPMB_FRAME_TYPE.Standard; pub const StorageRpmbFrameTypeMax = STORAGE_RPMB_FRAME_TYPE.Max; pub const STORAGE_RPMB_DESCRIPTOR = extern struct { Version: u32, Size: u32, SizeInBytes: u32, MaxReliableWriteSizeInBytes: u32, FrameFormat: STORAGE_RPMB_FRAME_TYPE, }; pub const STORAGE_CRYPTO_ALGORITHM_ID = enum(i32) { Unknown = 0, XTSAES = 1, BitlockerAESCBC = 2, AESECB = 3, ESSIVAESCBC = 4, Max = 5, }; pub const StorageCryptoAlgorithmUnknown = STORAGE_CRYPTO_ALGORITHM_ID.Unknown; pub const StorageCryptoAlgorithmXTSAES = STORAGE_CRYPTO_ALGORITHM_ID.XTSAES; pub const StorageCryptoAlgorithmBitlockerAESCBC = STORAGE_CRYPTO_ALGORITHM_ID.BitlockerAESCBC; pub const StorageCryptoAlgorithmAESECB = STORAGE_CRYPTO_ALGORITHM_ID.AESECB; pub const StorageCryptoAlgorithmESSIVAESCBC = STORAGE_CRYPTO_ALGORITHM_ID.ESSIVAESCBC; pub const StorageCryptoAlgorithmMax = STORAGE_CRYPTO_ALGORITHM_ID.Max; pub const STORAGE_CRYPTO_KEY_SIZE = enum(i32) { Unknown = 0, @"128Bits" = 1, @"192Bits" = 2, @"256Bits" = 3, @"512Bits" = 4, }; pub const StorageCryptoKeySizeUnknown = STORAGE_CRYPTO_KEY_SIZE.Unknown; pub const StorageCryptoKeySize128Bits = STORAGE_CRYPTO_KEY_SIZE.@"128Bits"; pub const StorageCryptoKeySize192Bits = STORAGE_CRYPTO_KEY_SIZE.@"192Bits"; pub const StorageCryptoKeySize256Bits = STORAGE_CRYPTO_KEY_SIZE.@"256Bits"; pub const StorageCryptoKeySize512Bits = STORAGE_CRYPTO_KEY_SIZE.@"512Bits"; pub const STORAGE_CRYPTO_CAPABILITY = extern struct { Version: u32, Size: u32, CryptoCapabilityIndex: u32, AlgorithmId: STORAGE_CRYPTO_ALGORITHM_ID, KeySize: STORAGE_CRYPTO_KEY_SIZE, DataUnitSizeBitmask: u32, }; pub const STORAGE_CRYPTO_DESCRIPTOR = extern struct { Version: u32, Size: u32, NumKeysSupported: u32, NumCryptoCapabilities: u32, CryptoCapabilities: [1]STORAGE_CRYPTO_CAPABILITY, }; pub const STORAGE_TIER_MEDIA_TYPE = enum(i32) { Unspecified = 0, Disk = 1, Ssd = 2, Scm = 4, Max = 5, }; pub const StorageTierMediaTypeUnspecified = STORAGE_TIER_MEDIA_TYPE.Unspecified; pub const StorageTierMediaTypeDisk = STORAGE_TIER_MEDIA_TYPE.Disk; pub const StorageTierMediaTypeSsd = STORAGE_TIER_MEDIA_TYPE.Ssd; pub const StorageTierMediaTypeScm = STORAGE_TIER_MEDIA_TYPE.Scm; pub const StorageTierMediaTypeMax = STORAGE_TIER_MEDIA_TYPE.Max; pub const STORAGE_TIER_CLASS = enum(i32) { Unspecified = 0, Capacity = 1, Performance = 2, Max = 3, }; pub const StorageTierClassUnspecified = STORAGE_TIER_CLASS.Unspecified; pub const StorageTierClassCapacity = STORAGE_TIER_CLASS.Capacity; pub const StorageTierClassPerformance = STORAGE_TIER_CLASS.Performance; pub const StorageTierClassMax = STORAGE_TIER_CLASS.Max; pub const STORAGE_TIER = extern struct { Id: Guid, Name: [256]u16, Description: [256]u16, Flags: u64, ProvisionedCapacity: u64, MediaType: STORAGE_TIER_MEDIA_TYPE, Class: STORAGE_TIER_CLASS, }; pub const STORAGE_DEVICE_TIERING_DESCRIPTOR = extern struct { Version: u32, Size: u32, Flags: u32, TotalNumberOfTiers: u32, NumberOfTiersReturned: u32, Tiers: [1]STORAGE_TIER, }; pub const STORAGE_DEVICE_FAULT_DOMAIN_DESCRIPTOR = extern struct { Version: u32, Size: u32, NumberOfFaultDomains: u32, FaultDomainIds: [1]Guid, }; pub const STORAGE_PROTOCOL_TYPE = enum(i32) { Unknown = 0, Scsi = 1, Ata = 2, Nvme = 3, Sd = 4, Ufs = 5, Proprietary = 126, MaxReserved = 127, }; pub const ProtocolTypeUnknown = STORAGE_PROTOCOL_TYPE.Unknown; pub const ProtocolTypeScsi = STORAGE_PROTOCOL_TYPE.Scsi; pub const ProtocolTypeAta = STORAGE_PROTOCOL_TYPE.Ata; pub const ProtocolTypeNvme = STORAGE_PROTOCOL_TYPE.Nvme; pub const ProtocolTypeSd = STORAGE_PROTOCOL_TYPE.Sd; pub const ProtocolTypeUfs = STORAGE_PROTOCOL_TYPE.Ufs; pub const ProtocolTypeProprietary = STORAGE_PROTOCOL_TYPE.Proprietary; pub const ProtocolTypeMaxReserved = STORAGE_PROTOCOL_TYPE.MaxReserved; pub const STORAGE_PROTOCOL_NVME_DATA_TYPE = enum(i32) { Unknown = 0, Identify = 1, LogPage = 2, Feature = 3, }; pub const NVMeDataTypeUnknown = STORAGE_PROTOCOL_NVME_DATA_TYPE.Unknown; pub const NVMeDataTypeIdentify = STORAGE_PROTOCOL_NVME_DATA_TYPE.Identify; pub const NVMeDataTypeLogPage = STORAGE_PROTOCOL_NVME_DATA_TYPE.LogPage; pub const NVMeDataTypeFeature = STORAGE_PROTOCOL_NVME_DATA_TYPE.Feature; pub const STORAGE_PROTOCOL_ATA_DATA_TYPE = enum(i32) { Unknown = 0, Identify = 1, LogPage = 2, }; pub const AtaDataTypeUnknown = STORAGE_PROTOCOL_ATA_DATA_TYPE.Unknown; pub const AtaDataTypeIdentify = STORAGE_PROTOCOL_ATA_DATA_TYPE.Identify; pub const AtaDataTypeLogPage = STORAGE_PROTOCOL_ATA_DATA_TYPE.LogPage; pub const STORAGE_PROTOCOL_UFS_DATA_TYPE = enum(i32) { Unknown = 0, QueryDescriptor = 1, QueryAttribute = 2, QueryFlag = 3, QueryDmeAttribute = 4, QueryDmePeerAttribute = 5, Max = 6, }; pub const UfsDataTypeUnknown = STORAGE_PROTOCOL_UFS_DATA_TYPE.Unknown; pub const UfsDataTypeQueryDescriptor = STORAGE_PROTOCOL_UFS_DATA_TYPE.QueryDescriptor; pub const UfsDataTypeQueryAttribute = STORAGE_PROTOCOL_UFS_DATA_TYPE.QueryAttribute; pub const UfsDataTypeQueryFlag = STORAGE_PROTOCOL_UFS_DATA_TYPE.QueryFlag; pub const UfsDataTypeQueryDmeAttribute = STORAGE_PROTOCOL_UFS_DATA_TYPE.QueryDmeAttribute; pub const UfsDataTypeQueryDmePeerAttribute = STORAGE_PROTOCOL_UFS_DATA_TYPE.QueryDmePeerAttribute; pub const UfsDataTypeMax = STORAGE_PROTOCOL_UFS_DATA_TYPE.Max; pub const STORAGE_PROTOCOL_DATA_SUBVALUE_GET_LOG_PAGE = extern union { Anonymous: extern struct { _bitfield: u32, }, AsUlong: u32, }; pub const STORAGE_PROTOCOL_SPECIFIC_DATA = extern struct { ProtocolType: STORAGE_PROTOCOL_TYPE, DataType: u32, ProtocolDataRequestValue: u32, ProtocolDataRequestSubValue: u32, ProtocolDataOffset: u32, ProtocolDataLength: u32, FixedProtocolReturnData: u32, ProtocolDataRequestSubValue2: u32, ProtocolDataRequestSubValue3: u32, ProtocolDataRequestSubValue4: u32, }; pub const STORAGE_PROTOCOL_SPECIFIC_DATA_EXT = extern struct { ProtocolType: STORAGE_PROTOCOL_TYPE, DataType: u32, ProtocolDataValue: u32, ProtocolDataSubValue: u32, ProtocolDataOffset: u32, ProtocolDataLength: u32, FixedProtocolReturnData: u32, ProtocolDataSubValue2: u32, ProtocolDataSubValue3: u32, ProtocolDataSubValue4: u32, ProtocolDataSubValue5: u32, Reserved: [5]u32, }; pub const STORAGE_PROTOCOL_DATA_DESCRIPTOR = extern struct { Version: u32, Size: u32, ProtocolSpecificData: STORAGE_PROTOCOL_SPECIFIC_DATA, }; pub const STORAGE_PROTOCOL_DATA_DESCRIPTOR_EXT = extern struct { Version: u32, Size: u32, ProtocolSpecificData: STORAGE_PROTOCOL_SPECIFIC_DATA_EXT, }; pub const STORAGE_TEMPERATURE_INFO = extern struct { Index: u16, Temperature: i16, OverThreshold: i16, UnderThreshold: i16, OverThresholdChangable: BOOLEAN, UnderThresholdChangable: BOOLEAN, EventGenerated: BOOLEAN, Reserved0: u8, Reserved1: u32, }; pub const STORAGE_TEMPERATURE_DATA_DESCRIPTOR = extern struct { Version: u32, Size: u32, CriticalTemperature: i16, WarningTemperature: i16, InfoCount: u16, Reserved0: [2]u8, Reserved1: [2]u32, TemperatureInfo: [1]STORAGE_TEMPERATURE_INFO, }; pub const STORAGE_TEMPERATURE_THRESHOLD = extern struct { Version: u32, Size: u32, Flags: u16, Index: u16, Threshold: i16, OverThreshold: BOOLEAN, Reserved: u8, }; pub const STORAGE_DEVICE_FORM_FACTOR = enum(i32) { Unknown = 0, @"3_5" = 1, @"2_5" = 2, @"1_8" = 3, @"1_8Less" = 4, Embedded = 5, MemoryCard = 6, mSata = 7, M_2 = 8, PCIeBoard = 9, Dimm = 10, }; pub const FormFactorUnknown = STORAGE_DEVICE_FORM_FACTOR.Unknown; pub const FormFactor3_5 = STORAGE_DEVICE_FORM_FACTOR.@"3_5"; pub const FormFactor2_5 = STORAGE_DEVICE_FORM_FACTOR.@"2_5"; pub const FormFactor1_8 = STORAGE_DEVICE_FORM_FACTOR.@"1_8"; pub const FormFactor1_8Less = STORAGE_DEVICE_FORM_FACTOR.@"1_8Less"; pub const FormFactorEmbedded = STORAGE_DEVICE_FORM_FACTOR.Embedded; pub const FormFactorMemoryCard = STORAGE_DEVICE_FORM_FACTOR.MemoryCard; pub const FormFactormSata = STORAGE_DEVICE_FORM_FACTOR.mSata; pub const FormFactorM_2 = STORAGE_DEVICE_FORM_FACTOR.M_2; pub const FormFactorPCIeBoard = STORAGE_DEVICE_FORM_FACTOR.PCIeBoard; pub const FormFactorDimm = STORAGE_DEVICE_FORM_FACTOR.Dimm; pub const STORAGE_COMPONENT_HEALTH_STATUS = enum(i32) { Unknown = 0, Normal = 1, Throttled = 2, Warning = 3, Disabled = 4, Failed = 5, }; pub const HealthStatusUnknown = STORAGE_COMPONENT_HEALTH_STATUS.Unknown; pub const HealthStatusNormal = STORAGE_COMPONENT_HEALTH_STATUS.Normal; pub const HealthStatusThrottled = STORAGE_COMPONENT_HEALTH_STATUS.Throttled; pub const HealthStatusWarning = STORAGE_COMPONENT_HEALTH_STATUS.Warning; pub const HealthStatusDisabled = STORAGE_COMPONENT_HEALTH_STATUS.Disabled; pub const HealthStatusFailed = STORAGE_COMPONENT_HEALTH_STATUS.Failed; pub const STORAGE_SPEC_VERSION = extern union { Anonymous: extern struct { MinorVersion: extern union { Anonymous: extern struct { SubMinor: u8, Minor: u8, }, AsUshort: u16, }, MajorVersion: u16, }, AsUlong: u32, }; pub const STORAGE_PHYSICAL_DEVICE_DATA = extern struct { DeviceId: u32, Role: u32, HealthStatus: STORAGE_COMPONENT_HEALTH_STATUS, CommandProtocol: STORAGE_PROTOCOL_TYPE, SpecVersion: STORAGE_SPEC_VERSION, FormFactor: STORAGE_DEVICE_FORM_FACTOR, Vendor: [8]u8, Model: [40]u8, FirmwareRevision: [16]u8, Capacity: u64, PhysicalLocation: [32]u8, Reserved: [2]u32, }; pub const STORAGE_PHYSICAL_ADAPTER_DATA = extern struct { AdapterId: u32, HealthStatus: STORAGE_COMPONENT_HEALTH_STATUS, CommandProtocol: STORAGE_PROTOCOL_TYPE, SpecVersion: STORAGE_SPEC_VERSION, Vendor: [8]u8, Model: [40]u8, FirmwareRevision: [16]u8, PhysicalLocation: [32]u8, ExpanderConnected: BOOLEAN, Reserved0: [3]u8, Reserved1: [3]u32, }; pub const STORAGE_PHYSICAL_NODE_DATA = extern struct { NodeId: u32, AdapterCount: u32, AdapterDataLength: u32, AdapterDataOffset: u32, DeviceCount: u32, DeviceDataLength: u32, DeviceDataOffset: u32, Reserved: [3]u32, }; pub const STORAGE_PHYSICAL_TOPOLOGY_DESCRIPTOR = extern struct { Version: u32, Size: u32, NodeCount: u32, Reserved: u32, Node: [1]STORAGE_PHYSICAL_NODE_DATA, }; pub const STORAGE_DEVICE_IO_CAPABILITY_DESCRIPTOR = extern struct { Version: u32, Size: u32, LunMaxIoCount: u32, AdapterMaxIoCount: u32, }; pub const STORAGE_DEVICE_ATTRIBUTES_DESCRIPTOR = extern struct { Version: u32, Size: u32, Attributes: u64, }; pub const STORAGE_DISK_HEALTH_STATUS = enum(i32) { Unknown = 0, Unhealthy = 1, Warning = 2, Healthy = 3, Max = 4, }; pub const DiskHealthUnknown = STORAGE_DISK_HEALTH_STATUS.Unknown; pub const DiskHealthUnhealthy = STORAGE_DISK_HEALTH_STATUS.Unhealthy; pub const DiskHealthWarning = STORAGE_DISK_HEALTH_STATUS.Warning; pub const DiskHealthHealthy = STORAGE_DISK_HEALTH_STATUS.Healthy; pub const DiskHealthMax = STORAGE_DISK_HEALTH_STATUS.Max; pub const STORAGE_DISK_OPERATIONAL_STATUS = enum(i32) { None = 0, Unknown = 1, Ok = 2, PredictingFailure = 3, InService = 4, HardwareError = 5, NotUsable = 6, TransientError = 7, Missing = 8, }; pub const DiskOpStatusNone = STORAGE_DISK_OPERATIONAL_STATUS.None; pub const DiskOpStatusUnknown = STORAGE_DISK_OPERATIONAL_STATUS.Unknown; pub const DiskOpStatusOk = STORAGE_DISK_OPERATIONAL_STATUS.Ok; pub const DiskOpStatusPredictingFailure = STORAGE_DISK_OPERATIONAL_STATUS.PredictingFailure; pub const DiskOpStatusInService = STORAGE_DISK_OPERATIONAL_STATUS.InService; pub const DiskOpStatusHardwareError = STORAGE_DISK_OPERATIONAL_STATUS.HardwareError; pub const DiskOpStatusNotUsable = STORAGE_DISK_OPERATIONAL_STATUS.NotUsable; pub const DiskOpStatusTransientError = STORAGE_DISK_OPERATIONAL_STATUS.TransientError; pub const DiskOpStatusMissing = STORAGE_DISK_OPERATIONAL_STATUS.Missing; pub const STORAGE_OPERATIONAL_STATUS_REASON = enum(i32) { Unknown = 0, ScsiSenseCode = 1, Media = 2, Io = 3, ThresholdExceeded = 4, LostData = 5, EnergySource = 6, Configuration = 7, DeviceController = 8, MediaController = 9, Component = 10, NVDIMM_N = 11, BackgroundOperation = 12, InvalidFirmware = 13, HealthCheck = 14, LostDataPersistence = 15, DisabledByPlatform = 16, LostWritePersistence = 17, DataPersistenceLossImminent = 18, WritePersistenceLossImminent = 19, Max = 20, }; pub const DiskOpReasonUnknown = STORAGE_OPERATIONAL_STATUS_REASON.Unknown; pub const DiskOpReasonScsiSenseCode = STORAGE_OPERATIONAL_STATUS_REASON.ScsiSenseCode; pub const DiskOpReasonMedia = STORAGE_OPERATIONAL_STATUS_REASON.Media; pub const DiskOpReasonIo = STORAGE_OPERATIONAL_STATUS_REASON.Io; pub const DiskOpReasonThresholdExceeded = STORAGE_OPERATIONAL_STATUS_REASON.ThresholdExceeded; pub const DiskOpReasonLostData = STORAGE_OPERATIONAL_STATUS_REASON.LostData; pub const DiskOpReasonEnergySource = STORAGE_OPERATIONAL_STATUS_REASON.EnergySource; pub const DiskOpReasonConfiguration = STORAGE_OPERATIONAL_STATUS_REASON.Configuration; pub const DiskOpReasonDeviceController = STORAGE_OPERATIONAL_STATUS_REASON.DeviceController; pub const DiskOpReasonMediaController = STORAGE_OPERATIONAL_STATUS_REASON.MediaController; pub const DiskOpReasonComponent = STORAGE_OPERATIONAL_STATUS_REASON.Component; pub const DiskOpReasonNVDIMM_N = STORAGE_OPERATIONAL_STATUS_REASON.NVDIMM_N; pub const DiskOpReasonBackgroundOperation = STORAGE_OPERATIONAL_STATUS_REASON.BackgroundOperation; pub const DiskOpReasonInvalidFirmware = STORAGE_OPERATIONAL_STATUS_REASON.InvalidFirmware; pub const DiskOpReasonHealthCheck = STORAGE_OPERATIONAL_STATUS_REASON.HealthCheck; pub const DiskOpReasonLostDataPersistence = STORAGE_OPERATIONAL_STATUS_REASON.LostDataPersistence; pub const DiskOpReasonDisabledByPlatform = STORAGE_OPERATIONAL_STATUS_REASON.DisabledByPlatform; pub const DiskOpReasonLostWritePersistence = STORAGE_OPERATIONAL_STATUS_REASON.LostWritePersistence; pub const DiskOpReasonDataPersistenceLossImminent = STORAGE_OPERATIONAL_STATUS_REASON.DataPersistenceLossImminent; pub const DiskOpReasonWritePersistenceLossImminent = STORAGE_OPERATIONAL_STATUS_REASON.WritePersistenceLossImminent; pub const DiskOpReasonMax = STORAGE_OPERATIONAL_STATUS_REASON.Max; pub const STORAGE_OPERATIONAL_REASON = extern struct { Version: u32, Size: u32, Reason: STORAGE_OPERATIONAL_STATUS_REASON, RawBytes: extern union { ScsiSenseKey: extern struct { SenseKey: u8, ASC: u8, ASCQ: u8, Reserved: u8, }, NVDIMM_N: extern struct { CriticalHealth: u8, ModuleHealth: [2]u8, ErrorThresholdStatus: u8, }, AsUlong: u32, }, }; pub const STORAGE_DEVICE_MANAGEMENT_STATUS = extern struct { Version: u32, Size: u32, Health: STORAGE_DISK_HEALTH_STATUS, NumberOfOperationalStatus: u32, NumberOfAdditionalReasons: u32, OperationalStatus: [16]STORAGE_DISK_OPERATIONAL_STATUS, AdditionalReasons: [1]STORAGE_OPERATIONAL_REASON, }; pub const STORAGE_ADAPTER_SERIAL_NUMBER = extern struct { Version: u32, Size: u32, SerialNumber: [128]u16, }; pub const STORAGE_ZONED_DEVICE_TYPES = enum(i32) { Unknown = 0, HostManaged = 1, HostAware = 2, DeviceManaged = 3, }; pub const ZonedDeviceTypeUnknown = STORAGE_ZONED_DEVICE_TYPES.Unknown; pub const ZonedDeviceTypeHostManaged = STORAGE_ZONED_DEVICE_TYPES.HostManaged; pub const ZonedDeviceTypeHostAware = STORAGE_ZONED_DEVICE_TYPES.HostAware; pub const ZonedDeviceTypeDeviceManaged = STORAGE_ZONED_DEVICE_TYPES.DeviceManaged; pub const STORAGE_ZONE_TYPES = enum(i32) { Unknown = 0, Conventional = 1, SequentialWriteRequired = 2, SequentialWritePreferred = 3, Max = 4, }; pub const ZoneTypeUnknown = STORAGE_ZONE_TYPES.Unknown; pub const ZoneTypeConventional = STORAGE_ZONE_TYPES.Conventional; pub const ZoneTypeSequentialWriteRequired = STORAGE_ZONE_TYPES.SequentialWriteRequired; pub const ZoneTypeSequentialWritePreferred = STORAGE_ZONE_TYPES.SequentialWritePreferred; pub const ZoneTypeMax = STORAGE_ZONE_TYPES.Max; pub const STORAGE_ZONE_GROUP = extern struct { ZoneCount: u32, ZoneType: STORAGE_ZONE_TYPES, ZoneSize: u64, }; pub const STORAGE_ZONED_DEVICE_DESCRIPTOR = extern struct { Version: u32, Size: u32, DeviceType: STORAGE_ZONED_DEVICE_TYPES, ZoneCount: u32, ZoneAttributes: extern union { SequentialRequiredZone: extern struct { MaxOpenZoneCount: u32, UnrestrictedRead: BOOLEAN, Reserved: [3]u8, }, SequentialPreferredZone: extern struct { OptimalOpenZoneCount: u32, Reserved: u32, }, }, ZoneGroupCount: u32, ZoneGroup: [1]STORAGE_ZONE_GROUP, }; pub const DEVICE_LOCATION = extern struct { Socket: u32, Slot: u32, Adapter: u32, Port: u32, Anonymous: extern union { Anonymous1: extern struct { Channel: u32, Device: u32, }, Anonymous2: extern struct { Target: u32, Lun: u32, }, }, }; pub const STORAGE_DEVICE_LOCATION_DESCRIPTOR = extern struct { Version: u32, Size: u32, Location: DEVICE_LOCATION, StringOffset: u32, }; pub const STORAGE_DEVICE_NUMA_PROPERTY = extern struct { Version: u32, Size: u32, NumaNode: u32, }; pub const STORAGE_DEVICE_UNSAFE_SHUTDOWN_COUNT = extern struct { Version: u32, Size: u32, UnsafeShutdownCount: u32, }; pub const STORAGE_HW_ENDURANCE_INFO = extern struct { ValidFields: u32, GroupId: u32, Flags: extern struct { _bitfield: u32, }, LifePercentage: u32, BytesReadCount: [16]u8, ByteWriteCount: [16]u8, }; pub const STORAGE_HW_ENDURANCE_DATA_DESCRIPTOR = extern struct { Version: u32, Size: u32, EnduranceInfo: STORAGE_HW_ENDURANCE_INFO, }; pub const STORAGE_DEVICE_LED_STATE_DESCRIPTOR = extern struct { Version: u32, Size: u32, State: u64, }; pub const STORAGE_DEVICE_SELF_ENCRYPTION_PROPERTY = extern struct { Version: u32, Size: u32, SupportsSelfEncryption: BOOLEAN, }; pub const STORAGE_FRU_ID_DESCRIPTOR = extern struct { Version: u32, Size: u32, IdentifierSize: u32, Identifier: [1]u8, }; pub const DEVICE_DATA_SET_RANGE = extern struct { StartingOffset: i64, LengthInBytes: u64, }; pub const DEVICE_MANAGE_DATA_SET_ATTRIBUTES = extern struct { Size: u32, Action: u32, Flags: u32, ParameterBlockOffset: u32, ParameterBlockLength: u32, DataSetRangesOffset: u32, DataSetRangesLength: u32, }; pub const DEVICE_MANAGE_DATA_SET_ATTRIBUTES_OUTPUT = extern struct { Size: u32, Action: u32, Flags: u32, OperationStatus: u32, ExtendedError: u32, TargetDetailedError: u32, ReservedStatus: u32, OutputBlockOffset: u32, OutputBlockLength: u32, }; pub const DEVICE_DSM_DEFINITION = extern struct { Action: u32, SingleRange: BOOLEAN, ParameterBlockAlignment: u32, ParameterBlockLength: u32, HasOutput: BOOLEAN, OutputBlockAlignment: u32, OutputBlockLength: u32, }; pub const DEVICE_DSM_NOTIFICATION_PARAMETERS = extern struct { Size: u32, Flags: u32, NumFileTypeIDs: u32, FileTypeID: [1]Guid, }; pub const STORAGE_OFFLOAD_TOKEN = extern struct { TokenType: [4]u8, Reserved: [2]u8, TokenIdLength: [2]u8, Anonymous: extern union { StorageOffloadZeroDataToken: extern struct { Reserved2: [504]u8, }, Token: [504]u8, }, }; pub const DEVICE_DSM_OFFLOAD_READ_PARAMETERS = extern struct { Flags: u32, TimeToLive: u32, Reserved: [2]u32, }; pub const STORAGE_OFFLOAD_READ_OUTPUT = extern struct { OffloadReadFlags: u32, Reserved: u32, LengthProtected: u64, TokenLength: u32, Token: STORAGE_OFFLOAD_TOKEN, }; pub const DEVICE_DSM_OFFLOAD_WRITE_PARAMETERS = extern struct { Flags: u32, Reserved: u32, TokenOffset: u64, Token: STORAGE_OFFLOAD_TOKEN, }; pub const STORAGE_OFFLOAD_WRITE_OUTPUT = extern struct { OffloadWriteFlags: u32, Reserved: u32, LengthCopied: u64, }; pub const DEVICE_DATA_SET_LBP_STATE_PARAMETERS = extern struct { Version: u32, Size: u32, Flags: u32, OutputVersion: u32, }; pub const DEVICE_DATA_SET_LB_PROVISIONING_STATE = extern struct { Size: u32, Version: u32, SlabSizeInBytes: u64, SlabOffsetDeltaInBytes: u32, SlabAllocationBitMapBitCount: u32, SlabAllocationBitMapLength: u32, SlabAllocationBitMap: [1]u32, }; pub const DEVICE_DATA_SET_LB_PROVISIONING_STATE_V2 = extern struct { Size: u32, Version: u32, SlabSizeInBytes: u64, SlabOffsetDeltaInBytes: u64, SlabAllocationBitMapBitCount: u32, SlabAllocationBitMapLength: u32, SlabAllocationBitMap: [1]u32, }; pub const DEVICE_DATA_SET_REPAIR_PARAMETERS = extern struct { NumberOfRepairCopies: u32, SourceCopy: u32, RepairCopies: [1]u32, }; pub const DEVICE_DATA_SET_REPAIR_OUTPUT = extern struct { ParityExtent: DEVICE_DATA_SET_RANGE, }; pub const DEVICE_DATA_SET_SCRUB_OUTPUT = extern struct { BytesProcessed: u64, BytesRepaired: u64, BytesFailed: u64, }; pub const DEVICE_DATA_SET_SCRUB_EX_OUTPUT = extern struct { BytesProcessed: u64, BytesRepaired: u64, BytesFailed: u64, ParityExtent: DEVICE_DATA_SET_RANGE, BytesScrubbed: u64, }; pub const DEVICE_DSM_TIERING_QUERY_INPUT = extern struct { Version: u32, Size: u32, Flags: u32, NumberOfTierIds: u32, TierIds: [1]Guid, }; pub const STORAGE_TIER_REGION = extern struct { TierId: Guid, Offset: u64, Length: u64, }; pub const DEVICE_DSM_TIERING_QUERY_OUTPUT = extern struct { Version: u32, Size: u32, Flags: u32, Reserved: u32, Alignment: u64, TotalNumberOfRegions: u32, NumberOfRegionsReturned: u32, Regions: [1]STORAGE_TIER_REGION, }; pub const DEVICE_DSM_NVCACHE_CHANGE_PRIORITY_PARAMETERS = extern struct { Size: u32, TargetPriority: u8, Reserved: [3]u8, }; pub const DEVICE_DATA_SET_TOPOLOGY_ID_QUERY_OUTPUT = extern struct { TopologyRangeBytes: u64, TopologyId: [16]u8, }; pub const DEVICE_STORAGE_ADDRESS_RANGE = extern struct { StartAddress: i64, LengthInBytes: u64, }; pub const DEVICE_DSM_PHYSICAL_ADDRESSES_OUTPUT = extern struct { Version: u32, Flags: u32, TotalNumberOfRanges: u32, NumberOfRangesReturned: u32, Ranges: [1]DEVICE_STORAGE_ADDRESS_RANGE, }; pub const DEVICE_DSM_REPORT_ZONES_PARAMETERS = extern struct { Size: u32, ReportOption: u8, Partial: u8, Reserved: [2]u8, }; pub const STORAGE_ZONES_ATTRIBUTES = enum(i32) { AndLengthMayDifferent = 0, SameLengthSame = 1, SameLastZoneLengthDifferent = 2, MayDifferentLengthSame = 3, }; pub const ZonesAttributeTypeAndLengthMayDifferent = STORAGE_ZONES_ATTRIBUTES.AndLengthMayDifferent; pub const ZonesAttributeTypeSameLengthSame = STORAGE_ZONES_ATTRIBUTES.SameLengthSame; pub const ZonesAttributeTypeSameLastZoneLengthDifferent = STORAGE_ZONES_ATTRIBUTES.SameLastZoneLengthDifferent; pub const ZonesAttributeTypeMayDifferentLengthSame = STORAGE_ZONES_ATTRIBUTES.MayDifferentLengthSame; pub const STORAGE_ZONE_CONDITION = enum(i32) { Conventional = 0, Empty = 1, ImplicitlyOpened = 2, ExplicitlyOpened = 3, Closed = 4, ReadOnly = 13, Full = 14, Offline = 15, }; pub const ZoneConditionConventional = STORAGE_ZONE_CONDITION.Conventional; pub const ZoneConditionEmpty = STORAGE_ZONE_CONDITION.Empty; pub const ZoneConditionImplicitlyOpened = STORAGE_ZONE_CONDITION.ImplicitlyOpened; pub const ZoneConditionExplicitlyOpened = STORAGE_ZONE_CONDITION.ExplicitlyOpened; pub const ZoneConditionClosed = STORAGE_ZONE_CONDITION.Closed; pub const ZoneConditionReadOnly = STORAGE_ZONE_CONDITION.ReadOnly; pub const ZoneConditionFull = STORAGE_ZONE_CONDITION.Full; pub const ZoneConditionOffline = STORAGE_ZONE_CONDITION.Offline; pub const STORAGE_ZONE_DESCRIPTOR = extern struct { Size: u32, ZoneType: STORAGE_ZONE_TYPES, ZoneCondition: STORAGE_ZONE_CONDITION, ResetWritePointerRecommend: BOOLEAN, Reserved0: [3]u8, ZoneSize: u64, WritePointerOffset: u64, }; pub const DEVICE_DSM_REPORT_ZONES_DATA = extern struct { Size: u32, ZoneCount: u32, Attributes: STORAGE_ZONES_ATTRIBUTES, Reserved0: u32, ZoneDescriptors: [1]STORAGE_ZONE_DESCRIPTOR, }; pub const DEVICE_STORAGE_RANGE_ATTRIBUTES = extern struct { LengthInBytes: u64, Anonymous: extern union { AllFlags: u32, Anonymous: extern struct { _bitfield: u32, }, }, Reserved: u32, }; pub const DEVICE_DSM_RANGE_ERROR_INFO = extern struct { Version: u32, Flags: u32, TotalNumberOfRanges: u32, NumberOfRangesReturned: u32, Ranges: [1]DEVICE_STORAGE_RANGE_ATTRIBUTES, }; pub const DEVICE_DSM_LOST_QUERY_PARAMETERS = extern struct { Version: u32, Granularity: u64, }; pub const DEVICE_DSM_LOST_QUERY_OUTPUT = extern struct { Version: u32, Size: u32, Alignment: u64, NumberOfBits: u32, BitMap: [1]u32, }; pub const DEVICE_DSM_FREE_SPACE_OUTPUT = extern struct { Version: u32, FreeSpace: u64, }; pub const DEVICE_DSM_CONVERSION_OUTPUT = extern struct { Version: u32, Source: Guid, }; pub const STORAGE_GET_BC_PROPERTIES_OUTPUT = extern struct { MaximumRequestsPerPeriod: u32, MinimumPeriod: u32, MaximumRequestSize: u64, EstimatedTimePerRequest: u32, NumOutStandingRequests: u32, RequestSize: u64, }; pub const STORAGE_ALLOCATE_BC_STREAM_INPUT = extern struct { Version: u32, RequestsPerPeriod: u32, Period: u32, RetryFailures: BOOLEAN, Discardable: BOOLEAN, Reserved1: [2]BOOLEAN, AccessType: u32, AccessMode: u32, }; pub const STORAGE_ALLOCATE_BC_STREAM_OUTPUT = extern struct { RequestSize: u64, NumOutStandingRequests: u32, }; pub const STORAGE_PRIORITY_HINT_SUPPORT = extern struct { SupportFlags: u32, }; pub const STORAGE_DIAGNOSTIC_LEVEL = enum(i32) { Default = 0, Max = 1, }; pub const StorageDiagnosticLevelDefault = STORAGE_DIAGNOSTIC_LEVEL.Default; pub const StorageDiagnosticLevelMax = STORAGE_DIAGNOSTIC_LEVEL.Max; pub const STORAGE_DIAGNOSTIC_TARGET_TYPE = enum(i32) { Undefined = 0, Port = 1, Miniport = 2, HbaFirmware = 3, Max = 4, }; pub const StorageDiagnosticTargetTypeUndefined = STORAGE_DIAGNOSTIC_TARGET_TYPE.Undefined; pub const StorageDiagnosticTargetTypePort = STORAGE_DIAGNOSTIC_TARGET_TYPE.Port; pub const StorageDiagnosticTargetTypeMiniport = STORAGE_DIAGNOSTIC_TARGET_TYPE.Miniport; pub const StorageDiagnosticTargetTypeHbaFirmware = STORAGE_DIAGNOSTIC_TARGET_TYPE.HbaFirmware; pub const StorageDiagnosticTargetTypeMax = STORAGE_DIAGNOSTIC_TARGET_TYPE.Max; pub const STORAGE_DIAGNOSTIC_REQUEST = extern struct { Version: u32, Size: u32, Flags: u32, TargetType: STORAGE_DIAGNOSTIC_TARGET_TYPE, Level: STORAGE_DIAGNOSTIC_LEVEL, }; pub const STORAGE_DIAGNOSTIC_DATA = extern struct { Version: u32, Size: u32, ProviderId: Guid, BufferSize: u32, Reserved: u32, DiagnosticDataBuffer: [1]u8, }; pub const PHYSICAL_ELEMENT_STATUS_REQUEST = extern struct { Version: u32, Size: u32, StartingElement: u32, Filter: u8, ReportType: u8, Reserved: [2]u8, }; pub const PHYSICAL_ELEMENT_STATUS_DESCRIPTOR = extern struct { Version: u32, Size: u32, ElementIdentifier: u32, PhysicalElementType: u8, PhysicalElementHealth: u8, Reserved1: [2]u8, AssociatedCapacity: u64, Reserved2: [4]u32, }; pub const PHYSICAL_ELEMENT_STATUS = extern struct { Version: u32, Size: u32, DescriptorCount: u32, ReturnedDescriptorCount: u32, ElementIdentifierBeingDepoped: u32, Reserved: u32, Descriptors: [1]PHYSICAL_ELEMENT_STATUS_DESCRIPTOR, }; pub const REMOVE_ELEMENT_AND_TRUNCATE_REQUEST = extern struct { Version: u32, Size: u32, RequestCapacity: u64, ElementIdentifier: u32, Reserved: u32, }; pub const DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE = enum(i32) { InternalStatusDataRequestTypeUndefined = 0, CurrentInternalStatusDataHeader = 1, CurrentInternalStatusData = 2, SavedInternalStatusDataHeader = 3, SavedInternalStatusData = 4, }; pub const DeviceInternalStatusDataRequestTypeUndefined = DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE.InternalStatusDataRequestTypeUndefined; pub const DeviceCurrentInternalStatusDataHeader = DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE.CurrentInternalStatusDataHeader; pub const DeviceCurrentInternalStatusData = DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE.CurrentInternalStatusData; pub const DeviceSavedInternalStatusDataHeader = DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE.SavedInternalStatusDataHeader; pub const DeviceSavedInternalStatusData = DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE.SavedInternalStatusData; pub const DEVICE_INTERNAL_STATUS_DATA_SET = enum(i32) { Undefined = 0, @"1" = 1, @"2" = 2, @"3" = 3, @"4" = 4, Max = 5, }; pub const DeviceStatusDataSetUndefined = DEVICE_INTERNAL_STATUS_DATA_SET.Undefined; pub const DeviceStatusDataSet1 = DEVICE_INTERNAL_STATUS_DATA_SET.@"1"; pub const DeviceStatusDataSet2 = DEVICE_INTERNAL_STATUS_DATA_SET.@"2"; pub const DeviceStatusDataSet3 = DEVICE_INTERNAL_STATUS_DATA_SET.@"3"; pub const DeviceStatusDataSet4 = DEVICE_INTERNAL_STATUS_DATA_SET.@"4"; pub const DeviceStatusDataSetMax = DEVICE_INTERNAL_STATUS_DATA_SET.Max; pub const GET_DEVICE_INTERNAL_STATUS_DATA_REQUEST = extern struct { Version: u32, Size: u32, RequestDataType: DEVICE_INTERNAL_STATUS_DATA_REQUEST_TYPE, RequestDataSet: DEVICE_INTERNAL_STATUS_DATA_SET, }; pub const DEVICE_INTERNAL_STATUS_DATA = extern struct { Version: u32, Size: u32, T10VendorId: u64, DataSet1Length: u32, DataSet2Length: u32, DataSet3Length: u32, DataSet4Length: u32, StatusDataVersion: u8, Reserved: [3]u8, ReasonIdentifier: [128]u8, StatusDataLength: u32, StatusData: [1]u8, }; pub const STORAGE_SANITIZE_METHOD = enum(i32) { Default = 0, BlockErase = 1, CryptoErase = 2, }; pub const StorageSanitizeMethodDefault = STORAGE_SANITIZE_METHOD.Default; pub const StorageSanitizeMethodBlockErase = STORAGE_SANITIZE_METHOD.BlockErase; pub const StorageSanitizeMethodCryptoErase = STORAGE_SANITIZE_METHOD.CryptoErase; pub const STORAGE_REINITIALIZE_MEDIA = extern struct { Version: u32, Size: u32, TimeoutInSeconds: u32, SanitizeOption: extern struct { _bitfield: u32, }, }; pub const STORAGE_MEDIA_SERIAL_NUMBER_DATA = extern struct { Reserved: u16, SerialNumberLength: u16, SerialNumber: [1]u8, }; pub const STORAGE_READ_CAPACITY = extern struct { Version: u32, Size: u32, BlockLength: u32, NumberOfBlocks: LARGE_INTEGER, DiskLength: LARGE_INTEGER, }; pub const WRITE_CACHE_TYPE = enum(i32) { Unknown = 0, None = 1, WriteBack = 2, WriteThrough = 3, }; pub const WriteCacheTypeUnknown = WRITE_CACHE_TYPE.Unknown; pub const WriteCacheTypeNone = WRITE_CACHE_TYPE.None; pub const WriteCacheTypeWriteBack = WRITE_CACHE_TYPE.WriteBack; pub const WriteCacheTypeWriteThrough = WRITE_CACHE_TYPE.WriteThrough; pub const WRITE_CACHE_ENABLE = enum(i32) { EnableUnknown = 0, Disabled = 1, Enabled = 2, }; pub const WriteCacheEnableUnknown = WRITE_CACHE_ENABLE.EnableUnknown; pub const WriteCacheDisabled = WRITE_CACHE_ENABLE.Disabled; pub const WriteCacheEnabled = WRITE_CACHE_ENABLE.Enabled; pub const WRITE_CACHE_CHANGE = enum(i32) { ChangeUnknown = 0, NotChangeable = 1, Changeable = 2, }; pub const WriteCacheChangeUnknown = WRITE_CACHE_CHANGE.ChangeUnknown; pub const WriteCacheNotChangeable = WRITE_CACHE_CHANGE.NotChangeable; pub const WriteCacheChangeable = WRITE_CACHE_CHANGE.Changeable; pub const WRITE_THROUGH = enum(i32) { Unknown = 0, NotSupported = 1, Supported = 2, }; pub const WriteThroughUnknown = WRITE_THROUGH.Unknown; pub const WriteThroughNotSupported = WRITE_THROUGH.NotSupported; pub const WriteThroughSupported = WRITE_THROUGH.Supported; pub const STORAGE_WRITE_CACHE_PROPERTY = extern struct { Version: u32, Size: u32, WriteCacheType: WRITE_CACHE_TYPE, WriteCacheEnabled: WRITE_CACHE_ENABLE, WriteCacheChangeable: WRITE_CACHE_CHANGE, WriteThroughSupported: WRITE_THROUGH, FlushCacheSupported: BOOLEAN, UserDefinedPowerProtection: BOOLEAN, NVCacheEnabled: BOOLEAN, }; pub const PERSISTENT_RESERVE_COMMAND = extern struct { Version: u32, Size: u32, Anonymous: extern union { PR_IN: extern struct { _bitfield: u8, AllocationLength: u16, }, PR_OUT: extern struct { _bitfield1: u8, _bitfield2: u8, ParameterList: [1]u8, }, }, }; pub const _DEVICEDUMP_COLLECTION_TYPE = enum(i32) { BugCheck = 1, ApplicationRequested = 2, DeviceRequested = 3, }; pub const TCCollectionBugCheck = _DEVICEDUMP_COLLECTION_TYPE.BugCheck; pub const TCCollectionApplicationRequested = _DEVICEDUMP_COLLECTION_TYPE.ApplicationRequested; pub const TCCollectionDeviceRequested = _DEVICEDUMP_COLLECTION_TYPE.DeviceRequested; pub const DEVICEDUMP_SUBSECTION_POINTER = packed struct { dwSize: u32, dwFlags: u32, dwOffset: u32, }; pub const DEVICEDUMP_STRUCTURE_VERSION = packed struct { dwSignature: u32, dwVersion: u32, dwSize: u32, }; pub const DEVICEDUMP_SECTION_HEADER = packed struct { guidDeviceDataId: Guid, sOrganizationID: [16]u8, dwFirmwareRevision: u32, sModelNumber: [32]u8, szDeviceManufacturingID: [32]u8, dwFlags: u32, bRestrictedPrivateDataVersion: u32, dwFirmwareIssueId: u32, szIssueDescriptionString: [132]u8, }; pub const GP_LOG_PAGE_DESCRIPTOR = packed struct { LogAddress: u16, LogSectors: u16, }; pub const DEVICEDUMP_PUBLIC_SUBSECTION = packed struct { dwFlags: u32, GPLogTable: [16]GP_LOG_PAGE_DESCRIPTOR, szDescription: [16]CHAR, bData: [1]u8, }; pub const DEVICEDUMP_RESTRICTED_SUBSECTION = extern struct { bData: [1]u8, }; pub const DEVICEDUMP_PRIVATE_SUBSECTION = packed struct { dwFlags: u32, GPLogId: GP_LOG_PAGE_DESCRIPTOR, bData: [1]u8, }; pub const DEVICEDUMP_STORAGEDEVICE_DATA = packed struct { Descriptor: DEVICEDUMP_STRUCTURE_VERSION, SectionHeader: DEVICEDUMP_SECTION_HEADER, dwBufferSize: u32, dwReasonForCollection: u32, PublicData: DEVICEDUMP_SUBSECTION_POINTER, RestrictedData: DEVICEDUMP_SUBSECTION_POINTER, PrivateData: DEVICEDUMP_SUBSECTION_POINTER, }; pub const DEVICEDUMP_STORAGESTACK_PUBLIC_STATE_RECORD = packed struct { Cdb: [16]u8, Command: [16]u8, StartTime: u64, EndTime: u64, OperationStatus: u32, OperationError: u32, StackSpecific: extern union { ExternalStack: packed struct { dwReserved: u32, }, AtaPort: packed struct { dwAtaPortSpecific: u32, }, StorPort: packed struct { SrbTag: u32, }, }, }; pub const DEVICEDUMP_STORAGESTACK_PUBLIC_DUMP = packed struct { Descriptor: DEVICEDUMP_STRUCTURE_VERSION, dwReasonForCollection: u32, cDriverName: [16]u8, uiNumRecords: u32, RecordArray: [1]DEVICEDUMP_STORAGESTACK_PUBLIC_STATE_RECORD, }; pub const STORAGE_IDLE_POWER = extern struct { Version: u32, Size: u32, _bitfield: u32, D3IdleTimeout: u32, }; pub const STORAGE_POWERUP_REASON_TYPE = enum(i32) { Unknown = 0, IO = 1, DeviceAttention = 2, }; pub const StoragePowerupUnknown = STORAGE_POWERUP_REASON_TYPE.Unknown; pub const StoragePowerupIO = STORAGE_POWERUP_REASON_TYPE.IO; pub const StoragePowerupDeviceAttention = STORAGE_POWERUP_REASON_TYPE.DeviceAttention; pub const STORAGE_IDLE_POWERUP_REASON = extern struct { Version: u32, Size: u32, PowerupReason: STORAGE_POWERUP_REASON_TYPE, }; pub const STORAGE_DEVICE_POWER_CAP_UNITS = enum(i32) { Percent = 0, Milliwatts = 1, }; pub const StorageDevicePowerCapUnitsPercent = STORAGE_DEVICE_POWER_CAP_UNITS.Percent; pub const StorageDevicePowerCapUnitsMilliwatts = STORAGE_DEVICE_POWER_CAP_UNITS.Milliwatts; pub const STORAGE_DEVICE_POWER_CAP = extern struct { Version: u32, Size: u32, Units: STORAGE_DEVICE_POWER_CAP_UNITS, MaxPower: u64, }; pub const STORAGE_RPMB_DATA_FRAME = extern struct { Stuff: [196]u8, KeyOrMAC: [32]u8, Data: [256]u8, Nonce: [16]u8, WriteCounter: [4]u8, Address: [2]u8, BlockCount: [2]u8, OperationResult: [2]u8, RequestOrResponseType: [2]u8, }; pub const STORAGE_RPMB_COMMAND_TYPE = enum(i32) { ProgramAuthKey = 1, QueryWriteCounter = 2, AuthenticatedWrite = 3, AuthenticatedRead = 4, ReadResultRequest = 5, AuthenticatedDeviceConfigWrite = 6, AuthenticatedDeviceConfigRead = 7, }; pub const StorRpmbProgramAuthKey = STORAGE_RPMB_COMMAND_TYPE.ProgramAuthKey; pub const StorRpmbQueryWriteCounter = STORAGE_RPMB_COMMAND_TYPE.QueryWriteCounter; pub const StorRpmbAuthenticatedWrite = STORAGE_RPMB_COMMAND_TYPE.AuthenticatedWrite; pub const StorRpmbAuthenticatedRead = STORAGE_RPMB_COMMAND_TYPE.AuthenticatedRead; pub const StorRpmbReadResultRequest = STORAGE_RPMB_COMMAND_TYPE.ReadResultRequest; pub const StorRpmbAuthenticatedDeviceConfigWrite = STORAGE_RPMB_COMMAND_TYPE.AuthenticatedDeviceConfigWrite; pub const StorRpmbAuthenticatedDeviceConfigRead = STORAGE_RPMB_COMMAND_TYPE.AuthenticatedDeviceConfigRead; pub const STORAGE_EVENT_NOTIFICATION = extern struct { Version: u32, Size: u32, Events: u64, }; pub const STORAGE_COUNTER_TYPE = enum(i32) { Unknown = 0, TemperatureCelsius = 1, TemperatureCelsiusMax = 2, ReadErrorsTotal = 3, ReadErrorsCorrected = 4, ReadErrorsUncorrected = 5, WriteErrorsTotal = 6, WriteErrorsCorrected = 7, WriteErrorsUncorrected = 8, ManufactureDate = 9, StartStopCycleCount = 10, StartStopCycleCountMax = 11, LoadUnloadCycleCount = 12, LoadUnloadCycleCountMax = 13, WearPercentage = 14, WearPercentageWarning = 15, WearPercentageMax = 16, PowerOnHours = 17, ReadLatency100NSMax = 18, WriteLatency100NSMax = 19, FlushLatency100NSMax = 20, Max = 21, }; pub const StorageCounterTypeUnknown = STORAGE_COUNTER_TYPE.Unknown; pub const StorageCounterTypeTemperatureCelsius = STORAGE_COUNTER_TYPE.TemperatureCelsius; pub const StorageCounterTypeTemperatureCelsiusMax = STORAGE_COUNTER_TYPE.TemperatureCelsiusMax; pub const StorageCounterTypeReadErrorsTotal = STORAGE_COUNTER_TYPE.ReadErrorsTotal; pub const StorageCounterTypeReadErrorsCorrected = STORAGE_COUNTER_TYPE.ReadErrorsCorrected; pub const StorageCounterTypeReadErrorsUncorrected = STORAGE_COUNTER_TYPE.ReadErrorsUncorrected; pub const StorageCounterTypeWriteErrorsTotal = STORAGE_COUNTER_TYPE.WriteErrorsTotal; pub const StorageCounterTypeWriteErrorsCorrected = STORAGE_COUNTER_TYPE.WriteErrorsCorrected; pub const StorageCounterTypeWriteErrorsUncorrected = STORAGE_COUNTER_TYPE.WriteErrorsUncorrected; pub const StorageCounterTypeManufactureDate = STORAGE_COUNTER_TYPE.ManufactureDate; pub const StorageCounterTypeStartStopCycleCount = STORAGE_COUNTER_TYPE.StartStopCycleCount; pub const StorageCounterTypeStartStopCycleCountMax = STORAGE_COUNTER_TYPE.StartStopCycleCountMax; pub const StorageCounterTypeLoadUnloadCycleCount = STORAGE_COUNTER_TYPE.LoadUnloadCycleCount; pub const StorageCounterTypeLoadUnloadCycleCountMax = STORAGE_COUNTER_TYPE.LoadUnloadCycleCountMax; pub const StorageCounterTypeWearPercentage = STORAGE_COUNTER_TYPE.WearPercentage; pub const StorageCounterTypeWearPercentageWarning = STORAGE_COUNTER_TYPE.WearPercentageWarning; pub const StorageCounterTypeWearPercentageMax = STORAGE_COUNTER_TYPE.WearPercentageMax; pub const StorageCounterTypePowerOnHours = STORAGE_COUNTER_TYPE.PowerOnHours; pub const StorageCounterTypeReadLatency100NSMax = STORAGE_COUNTER_TYPE.ReadLatency100NSMax; pub const StorageCounterTypeWriteLatency100NSMax = STORAGE_COUNTER_TYPE.WriteLatency100NSMax; pub const StorageCounterTypeFlushLatency100NSMax = STORAGE_COUNTER_TYPE.FlushLatency100NSMax; pub const StorageCounterTypeMax = STORAGE_COUNTER_TYPE.Max; pub const STORAGE_COUNTER = extern struct { Type: STORAGE_COUNTER_TYPE, Value: extern union { ManufactureDate: extern struct { Week: u32, Year: u32, }, AsUlonglong: u64, }, }; pub const STORAGE_COUNTERS = extern struct { Version: u32, Size: u32, NumberOfCounters: u32, Counters: [1]STORAGE_COUNTER, }; pub const STORAGE_HW_FIRMWARE_INFO_QUERY = extern struct { Version: u32, Size: u32, Flags: u32, Reserved: u32, }; pub const STORAGE_HW_FIRMWARE_SLOT_INFO = extern struct { Version: u32, Size: u32, SlotNumber: u8, _bitfield: u8, Reserved1: [6]u8, Revision: [16]u8, }; pub const STORAGE_HW_FIRMWARE_INFO = extern struct { Version: u32, Size: u32, _bitfield: u8, SlotCount: u8, ActiveSlot: u8, PendingActivateSlot: u8, FirmwareShared: BOOLEAN, Reserved: [3]u8, ImagePayloadAlignment: u32, ImagePayloadMaxSize: u32, Slot: [1]STORAGE_HW_FIRMWARE_SLOT_INFO, }; pub const STORAGE_HW_FIRMWARE_DOWNLOAD = extern struct { Version: u32, Size: u32, Flags: u32, Slot: u8, Reserved: [3]u8, Offset: u64, BufferSize: u64, ImageBuffer: [1]u8, }; pub const STORAGE_HW_FIRMWARE_DOWNLOAD_V2 = extern struct { Version: u32, Size: u32, Flags: u32, Slot: u8, Reserved: [3]u8, Offset: u64, BufferSize: u64, ImageSize: u32, Reserved2: u32, ImageBuffer: [1]u8, }; pub const STORAGE_HW_FIRMWARE_ACTIVATE = extern struct { Version: u32, Size: u32, Flags: u32, Slot: u8, Reserved0: [3]u8, }; pub const STORAGE_PROTOCOL_COMMAND = extern struct { Version: u32, Length: u32, ProtocolType: STORAGE_PROTOCOL_TYPE, Flags: u32, ReturnStatus: u32, ErrorCode: u32, CommandLength: u32, ErrorInfoLength: u32, DataToDeviceTransferLength: u32, DataFromDeviceTransferLength: u32, TimeOutValue: u32, ErrorInfoOffset: u32, DataToDeviceBufferOffset: u32, DataFromDeviceBufferOffset: u32, CommandSpecific: u32, Reserved0: u32, FixedProtocolReturnData: u32, Reserved1: [3]u32, Command: [1]u8, }; pub const STORAGE_ATTRIBUTE_MGMT_ACTION = enum(i32) { ClearAttribute = 0, SetAttribute = 1, ResetAttribute = 2, }; pub const StorAttributeMgmt_ClearAttribute = STORAGE_ATTRIBUTE_MGMT_ACTION.ClearAttribute; pub const StorAttributeMgmt_SetAttribute = STORAGE_ATTRIBUTE_MGMT_ACTION.SetAttribute; pub const StorAttributeMgmt_ResetAttribute = STORAGE_ATTRIBUTE_MGMT_ACTION.ResetAttribute; pub const STORAGE_ATTRIBUTE_MGMT = extern struct { Version: u32, Size: u32, Action: STORAGE_ATTRIBUTE_MGMT_ACTION, Attribute: u32, }; pub const SCM_PD_HEALTH_NOTIFICATION_DATA = extern struct { DeviceGuid: Guid, }; pub const SCM_LOGICAL_DEVICE_INSTANCE = extern struct { Version: u32, Size: u32, DeviceGuid: Guid, SymbolicLink: [256]u16, }; pub const SCM_LOGICAL_DEVICES = extern struct { Version: u32, Size: u32, DeviceCount: u32, Devices: [1]SCM_LOGICAL_DEVICE_INSTANCE, }; pub const SCM_PHYSICAL_DEVICE_INSTANCE = extern struct { Version: u32, Size: u32, NfitHandle: u32, SymbolicLink: [256]u16, }; pub const SCM_PHYSICAL_DEVICES = extern struct { Version: u32, Size: u32, DeviceCount: u32, Devices: [1]SCM_PHYSICAL_DEVICE_INSTANCE, }; pub const SCM_REGION_FLAG = enum(i32) { None = 0, Label = 1, }; pub const ScmRegionFlagNone = SCM_REGION_FLAG.None; pub const ScmRegionFlagLabel = SCM_REGION_FLAG.Label; pub const SCM_REGION = extern struct { Version: u32, Size: u32, Flags: u32, NfitHandle: u32, LogicalDeviceGuid: Guid, AddressRangeType: Guid, AssociatedId: u32, Length: u64, StartingDPA: u64, BaseSPA: u64, SPAOffset: u64, RegionOffset: u64, }; pub const SCM_REGIONS = extern struct { Version: u32, Size: u32, RegionCount: u32, Regions: [1]SCM_REGION, }; pub const SCM_BUS_QUERY_TYPE = enum(i32) { Descriptor = 0, IsSupported = 1, Max = 2, }; pub const ScmBusQuery_Descriptor = SCM_BUS_QUERY_TYPE.Descriptor; pub const ScmBusQuery_IsSupported = SCM_BUS_QUERY_TYPE.IsSupported; pub const ScmBusQuery_Max = SCM_BUS_QUERY_TYPE.Max; pub const SCM_BUS_SET_TYPE = enum(i32) { Descriptor = 0, IsSupported = 1, Max = 2, }; pub const ScmBusSet_Descriptor = SCM_BUS_SET_TYPE.Descriptor; pub const ScmBusSet_IsSupported = SCM_BUS_SET_TYPE.IsSupported; pub const ScmBusSet_Max = SCM_BUS_SET_TYPE.Max; pub const SCM_BUS_PROPERTY_ID = enum(i32) { RuntimeFwActivationInfo = 0, DedicatedMemoryInfo = 1, DedicatedMemoryState = 2, Max = 3, }; pub const ScmBusProperty_RuntimeFwActivationInfo = SCM_BUS_PROPERTY_ID.RuntimeFwActivationInfo; pub const ScmBusProperty_DedicatedMemoryInfo = SCM_BUS_PROPERTY_ID.DedicatedMemoryInfo; pub const ScmBusProperty_DedicatedMemoryState = SCM_BUS_PROPERTY_ID.DedicatedMemoryState; pub const ScmBusProperty_Max = SCM_BUS_PROPERTY_ID.Max; pub const SCM_BUS_PROPERTY_QUERY = extern struct { Version: u32, Size: u32, PropertyId: SCM_BUS_PROPERTY_ID, QueryType: SCM_BUS_QUERY_TYPE, AdditionalParameters: [1]u8, }; pub const SCM_BUS_FIRMWARE_ACTIVATION_STATE = enum(i32) { Idle = 0, Armed = 1, Busy = 2, }; pub const ScmBusFirmwareActivationState_Idle = SCM_BUS_FIRMWARE_ACTIVATION_STATE.Idle; pub const ScmBusFirmwareActivationState_Armed = SCM_BUS_FIRMWARE_ACTIVATION_STATE.Armed; pub const ScmBusFirmwareActivationState_Busy = SCM_BUS_FIRMWARE_ACTIVATION_STATE.Busy; pub const SCM_BUS_RUNTIME_FW_ACTIVATION_INFO = extern struct { Version: u32, Size: u32, RuntimeFwActivationSupported: BOOLEAN, FirmwareActivationState: SCM_BUS_FIRMWARE_ACTIVATION_STATE, FirmwareActivationCapability: extern struct { _bitfield: u32, }, EstimatedFirmwareActivationTimeInUSecs: u64, EstimatedProcessorAccessQuiesceTimeInUSecs: u64, EstimatedIOAccessQuiesceTimeInUSecs: u64, PlatformSupportedMaxIOAccessQuiesceTimeInUSecs: u64, }; pub const SCM_BUS_DEDICATED_MEMORY_DEVICE_INFO = extern struct { DeviceGuid: Guid, DeviceNumber: u32, Flags: extern struct { _bitfield: u32, }, DeviceSize: u64, }; pub const SCM_BUS_DEDICATED_MEMORY_DEVICES_INFO = extern struct { Version: u32, Size: u32, DeviceCount: u32, Devices: [1]SCM_BUS_DEDICATED_MEMORY_DEVICE_INFO, }; pub const SCM_BUS_PROPERTY_SET = extern struct { Version: u32, Size: u32, PropertyId: SCM_BUS_PROPERTY_ID, SetType: SCM_BUS_SET_TYPE, AdditionalParameters: [1]u8, }; pub const SCM_BUS_DEDICATED_MEMORY_STATE = extern struct { ActivateState: BOOLEAN, }; pub const SCM_INTERLEAVED_PD_INFO = extern struct { DeviceHandle: u32, DeviceGuid: Guid, }; pub const SCM_LD_INTERLEAVE_SET_INFO = extern struct { Version: u32, Size: u32, InterleaveSetSize: u32, InterleaveSet: [1]SCM_INTERLEAVED_PD_INFO, }; pub const SCM_PD_QUERY_TYPE = enum(i32) { Descriptor = 0, IsSupported = 1, Max = 2, }; pub const ScmPhysicalDeviceQuery_Descriptor = SCM_PD_QUERY_TYPE.Descriptor; pub const ScmPhysicalDeviceQuery_IsSupported = SCM_PD_QUERY_TYPE.IsSupported; pub const ScmPhysicalDeviceQuery_Max = SCM_PD_QUERY_TYPE.Max; pub const SCM_PD_SET_TYPE = enum(i32) { Descriptor = 0, IsSupported = 1, Max = 2, }; pub const ScmPhysicalDeviceSet_Descriptor = SCM_PD_SET_TYPE.Descriptor; pub const ScmPhysicalDeviceSet_IsSupported = SCM_PD_SET_TYPE.IsSupported; pub const ScmPhysicalDeviceSet_Max = SCM_PD_SET_TYPE.Max; pub const SCM_PD_PROPERTY_ID = enum(i32) { DeviceInfo = 0, ManagementStatus = 1, FirmwareInfo = 2, LocationString = 3, DeviceSpecificInfo = 4, DeviceHandle = 5, FruIdString = 6, RuntimeFwActivationInfo = 7, RuntimeFwActivationArmState = 8, Max = 9, }; pub const ScmPhysicalDeviceProperty_DeviceInfo = SCM_PD_PROPERTY_ID.DeviceInfo; pub const ScmPhysicalDeviceProperty_ManagementStatus = SCM_PD_PROPERTY_ID.ManagementStatus; pub const ScmPhysicalDeviceProperty_FirmwareInfo = SCM_PD_PROPERTY_ID.FirmwareInfo; pub const ScmPhysicalDeviceProperty_LocationString = SCM_PD_PROPERTY_ID.LocationString; pub const ScmPhysicalDeviceProperty_DeviceSpecificInfo = SCM_PD_PROPERTY_ID.DeviceSpecificInfo; pub const ScmPhysicalDeviceProperty_DeviceHandle = SCM_PD_PROPERTY_ID.DeviceHandle; pub const ScmPhysicalDeviceProperty_FruIdString = SCM_PD_PROPERTY_ID.FruIdString; pub const ScmPhysicalDeviceProperty_RuntimeFwActivationInfo = SCM_PD_PROPERTY_ID.RuntimeFwActivationInfo; pub const ScmPhysicalDeviceProperty_RuntimeFwActivationArmState = SCM_PD_PROPERTY_ID.RuntimeFwActivationArmState; pub const ScmPhysicalDeviceProperty_Max = SCM_PD_PROPERTY_ID.Max; pub const SCM_PD_PROPERTY_QUERY = extern struct { Version: u32, Size: u32, PropertyId: SCM_PD_PROPERTY_ID, QueryType: SCM_PD_QUERY_TYPE, AdditionalParameters: [1]u8, }; pub const SCM_PD_PROPERTY_SET = extern struct { Version: u32, Size: u32, PropertyId: SCM_PD_PROPERTY_ID, SetType: SCM_PD_SET_TYPE, AdditionalParameters: [1]u8, }; pub const SCM_PD_RUNTIME_FW_ACTIVATION_ARM_STATE = extern struct { ArmState: BOOLEAN, }; pub const SCM_PD_DESCRIPTOR_HEADER = extern struct { Version: u32, Size: u32, }; pub const SCM_PD_DEVICE_HANDLE = extern struct { Version: u32, Size: u32, DeviceGuid: Guid, DeviceHandle: u32, }; pub const SCM_PD_DEVICE_INFO = extern struct { Version: u32, Size: u32, DeviceGuid: Guid, UnsafeShutdownCount: u32, PersistentMemorySizeInBytes: u64, VolatileMemorySizeInBytes: u64, TotalMemorySizeInBytes: u64, SlotNumber: u32, DeviceHandle: u32, PhysicalId: u16, NumberOfFormatInterfaceCodes: u8, FormatInterfaceCodes: [8]u16, VendorId: u32, ProductId: u32, SubsystemDeviceId: u32, SubsystemVendorId: u32, ManufacturingLocation: u8, ManufacturingWeek: u8, ManufacturingYear: u8, SerialNumber4Byte: u32, SerialNumberLengthInChars: u32, SerialNumber: [1]CHAR, }; pub const SCM_PD_DEVICE_SPECIFIC_PROPERTY = extern struct { Name: [128]u16, Value: i64, }; pub const SCM_PD_DEVICE_SPECIFIC_INFO = extern struct { Version: u32, Size: u32, NumberOfProperties: u32, DeviceSpecificProperties: [1]SCM_PD_DEVICE_SPECIFIC_PROPERTY, }; pub const SCM_PD_FIRMWARE_SLOT_INFO = extern struct { Version: u32, Size: u32, SlotNumber: u8, _bitfield: u8, Reserved1: [6]u8, Revision: [32]u8, }; pub const SCM_PD_FIRMWARE_INFO = extern struct { Version: u32, Size: u32, ActiveSlot: u8, NextActiveSlot: u8, SlotCount: u8, Slots: [1]SCM_PD_FIRMWARE_SLOT_INFO, }; pub const SCM_PD_HEALTH_STATUS = enum(i32) { Unknown = 0, Unhealthy = 1, Warning = 2, Healthy = 3, Max = 4, }; pub const ScmPhysicalDeviceHealth_Unknown = SCM_PD_HEALTH_STATUS.Unknown; pub const ScmPhysicalDeviceHealth_Unhealthy = SCM_PD_HEALTH_STATUS.Unhealthy; pub const ScmPhysicalDeviceHealth_Warning = SCM_PD_HEALTH_STATUS.Warning; pub const ScmPhysicalDeviceHealth_Healthy = SCM_PD_HEALTH_STATUS.Healthy; pub const ScmPhysicalDeviceHealth_Max = SCM_PD_HEALTH_STATUS.Max; pub const SCM_PD_OPERATIONAL_STATUS = enum(i32) { Unknown = 0, Ok = 1, PredictingFailure = 2, InService = 3, HardwareError = 4, NotUsable = 5, TransientError = 6, Missing = 7, Max = 8, }; pub const ScmPhysicalDeviceOpStatus_Unknown = SCM_PD_OPERATIONAL_STATUS.Unknown; pub const ScmPhysicalDeviceOpStatus_Ok = SCM_PD_OPERATIONAL_STATUS.Ok; pub const ScmPhysicalDeviceOpStatus_PredictingFailure = SCM_PD_OPERATIONAL_STATUS.PredictingFailure; pub const ScmPhysicalDeviceOpStatus_InService = SCM_PD_OPERATIONAL_STATUS.InService; pub const ScmPhysicalDeviceOpStatus_HardwareError = SCM_PD_OPERATIONAL_STATUS.HardwareError; pub const ScmPhysicalDeviceOpStatus_NotUsable = SCM_PD_OPERATIONAL_STATUS.NotUsable; pub const ScmPhysicalDeviceOpStatus_TransientError = SCM_PD_OPERATIONAL_STATUS.TransientError; pub const ScmPhysicalDeviceOpStatus_Missing = SCM_PD_OPERATIONAL_STATUS.Missing; pub const ScmPhysicalDeviceOpStatus_Max = SCM_PD_OPERATIONAL_STATUS.Max; pub const SCM_PD_OPERATIONAL_STATUS_REASON = enum(i32) { Unknown = 0, Media = 1, ThresholdExceeded = 2, LostData = 3, EnergySource = 4, Configuration = 5, DeviceController = 6, MediaController = 7, Component = 8, BackgroundOperation = 9, InvalidFirmware = 10, HealthCheck = 11, LostDataPersistence = 12, DisabledByPlatform = 13, PermanentError = 14, LostWritePersistence = 15, FatalError = 16, DataPersistenceLossImminent = 17, WritePersistenceLossImminent = 18, MediaRemainingSpareBlock = 19, PerformanceDegradation = 20, ExcessiveTemperature = 21, InternalFailure = 22, Max = 23, }; pub const ScmPhysicalDeviceOpReason_Unknown = SCM_PD_OPERATIONAL_STATUS_REASON.Unknown; pub const ScmPhysicalDeviceOpReason_Media = SCM_PD_OPERATIONAL_STATUS_REASON.Media; pub const ScmPhysicalDeviceOpReason_ThresholdExceeded = SCM_PD_OPERATIONAL_STATUS_REASON.ThresholdExceeded; pub const ScmPhysicalDeviceOpReason_LostData = SCM_PD_OPERATIONAL_STATUS_REASON.LostData; pub const ScmPhysicalDeviceOpReason_EnergySource = SCM_PD_OPERATIONAL_STATUS_REASON.EnergySource; pub const ScmPhysicalDeviceOpReason_Configuration = SCM_PD_OPERATIONAL_STATUS_REASON.Configuration; pub const ScmPhysicalDeviceOpReason_DeviceController = SCM_PD_OPERATIONAL_STATUS_REASON.DeviceController; pub const ScmPhysicalDeviceOpReason_MediaController = SCM_PD_OPERATIONAL_STATUS_REASON.MediaController; pub const ScmPhysicalDeviceOpReason_Component = SCM_PD_OPERATIONAL_STATUS_REASON.Component; pub const ScmPhysicalDeviceOpReason_BackgroundOperation = SCM_PD_OPERATIONAL_STATUS_REASON.BackgroundOperation; pub const ScmPhysicalDeviceOpReason_InvalidFirmware = SCM_PD_OPERATIONAL_STATUS_REASON.InvalidFirmware; pub const ScmPhysicalDeviceOpReason_HealthCheck = SCM_PD_OPERATIONAL_STATUS_REASON.HealthCheck; pub const ScmPhysicalDeviceOpReason_LostDataPersistence = SCM_PD_OPERATIONAL_STATUS_REASON.LostDataPersistence; pub const ScmPhysicalDeviceOpReason_DisabledByPlatform = SCM_PD_OPERATIONAL_STATUS_REASON.DisabledByPlatform; pub const ScmPhysicalDeviceOpReason_PermanentError = SCM_PD_OPERATIONAL_STATUS_REASON.PermanentError; pub const ScmPhysicalDeviceOpReason_LostWritePersistence = SCM_PD_OPERATIONAL_STATUS_REASON.LostWritePersistence; pub const ScmPhysicalDeviceOpReason_FatalError = SCM_PD_OPERATIONAL_STATUS_REASON.FatalError; pub const ScmPhysicalDeviceOpReason_DataPersistenceLossImminent = SCM_PD_OPERATIONAL_STATUS_REASON.DataPersistenceLossImminent; pub const ScmPhysicalDeviceOpReason_WritePersistenceLossImminent = SCM_PD_OPERATIONAL_STATUS_REASON.WritePersistenceLossImminent; pub const ScmPhysicalDeviceOpReason_MediaRemainingSpareBlock = SCM_PD_OPERATIONAL_STATUS_REASON.MediaRemainingSpareBlock; pub const ScmPhysicalDeviceOpReason_PerformanceDegradation = SCM_PD_OPERATIONAL_STATUS_REASON.PerformanceDegradation; pub const ScmPhysicalDeviceOpReason_ExcessiveTemperature = SCM_PD_OPERATIONAL_STATUS_REASON.ExcessiveTemperature; pub const ScmPhysicalDeviceOpReason_InternalFailure = SCM_PD_OPERATIONAL_STATUS_REASON.InternalFailure; pub const ScmPhysicalDeviceOpReason_Max = SCM_PD_OPERATIONAL_STATUS_REASON.Max; pub const SCM_PD_MANAGEMENT_STATUS = extern struct { Version: u32, Size: u32, Health: SCM_PD_HEALTH_STATUS, NumberOfOperationalStatus: u32, NumberOfAdditionalReasons: u32, OperationalStatus: [16]SCM_PD_OPERATIONAL_STATUS, AdditionalReasons: [1]SCM_PD_OPERATIONAL_STATUS_REASON, }; pub const SCM_PD_LOCATION_STRING = extern struct { Version: u32, Size: u32, Location: [1]u16, }; pub const SCM_PD_FRU_ID_STRING = extern struct { Version: u32, Size: u32, IdentifierSize: u32, Identifier: [1]u8, }; pub const SCM_PD_FIRMWARE_DOWNLOAD = extern struct { Version: u32, Size: u32, Flags: u32, Slot: u8, Reserved: [3]u8, Offset: u64, FirmwareImageSizeInBytes: u32, FirmwareImage: [1]u8, }; pub const SCM_PD_FIRMWARE_ACTIVATE = extern struct { Version: u32, Size: u32, Flags: u32, Slot: u8, }; pub const SCM_PD_LAST_FW_ACTIVATION_STATUS = enum(i32) { tionStatus_None = 0, tionStatus_Success = 1, tionStatus_FwNotFound = 2, tionStatus_ColdRebootRequired = 3, itonStatus_ActivationInProgress = 4, itonStatus_Retry = 5, itonStatus_FwUnsupported = 6, itonStatus_UnknownError = 7, }; pub const ScmPdLastFwActivationStatus_None = SCM_PD_LAST_FW_ACTIVATION_STATUS.tionStatus_None; pub const ScmPdLastFwActivationStatus_Success = SCM_PD_LAST_FW_ACTIVATION_STATUS.tionStatus_Success; pub const ScmPdLastFwActivationStatus_FwNotFound = SCM_PD_LAST_FW_ACTIVATION_STATUS.tionStatus_FwNotFound; pub const ScmPdLastFwActivationStatus_ColdRebootRequired = SCM_PD_LAST_FW_ACTIVATION_STATUS.tionStatus_ColdRebootRequired; pub const ScmPdLastFwActivaitonStatus_ActivationInProgress = SCM_PD_LAST_FW_ACTIVATION_STATUS.itonStatus_ActivationInProgress; pub const ScmPdLastFwActivaitonStatus_Retry = SCM_PD_LAST_FW_ACTIVATION_STATUS.itonStatus_Retry; pub const ScmPdLastFwActivaitonStatus_FwUnsupported = SCM_PD_LAST_FW_ACTIVATION_STATUS.itonStatus_FwUnsupported; pub const ScmPdLastFwActivaitonStatus_UnknownError = SCM_PD_LAST_FW_ACTIVATION_STATUS.itonStatus_UnknownError; pub const SCM_PD_FIRMWARE_ACTIVATION_STATE = enum(i32) { Idle = 0, Armed = 1, Busy = 2, }; pub const ScmPdFirmwareActivationState_Idle = SCM_PD_FIRMWARE_ACTIVATION_STATE.Idle; pub const ScmPdFirmwareActivationState_Armed = SCM_PD_FIRMWARE_ACTIVATION_STATE.Armed; pub const ScmPdFirmwareActivationState_Busy = SCM_PD_FIRMWARE_ACTIVATION_STATE.Busy; pub const SCM_PD_RUNTIME_FW_ACTIVATION_INFO = extern struct { Version: u32, Size: u32, LastFirmwareActivationStatus: SCM_PD_LAST_FW_ACTIVATION_STATUS, FirmwareActivationState: SCM_PD_FIRMWARE_ACTIVATION_STATE, }; pub const SCM_PD_PASSTHROUGH_INPUT = extern struct { Version: u32, Size: u32, ProtocolGuid: Guid, DataSize: u32, Data: [1]u8, }; pub const SCM_PD_PASSTHROUGH_OUTPUT = extern struct { Version: u32, Size: u32, ProtocolGuid: Guid, DataSize: u32, Data: [1]u8, }; pub const SCM_PD_PASSTHROUGH_INVDIMM_INPUT = extern struct { Opcode: u32, OpcodeParametersLength: u32, OpcodeParameters: [1]u8, }; pub const SCM_PD_PASSTHROUGH_INVDIMM_OUTPUT = extern struct { GeneralStatus: u16, ExtendedStatus: u16, OutputDataLength: u32, OutputData: [1]u8, }; pub const SCM_PD_REINITIALIZE_MEDIA_INPUT = extern struct { Version: u32, Size: u32, Options: extern struct { _bitfield: u32, }, }; pub const SCM_PD_MEDIA_REINITIALIZATION_STATUS = enum(i32) { Success = 0, RebootNeeded = 1, ColdBootNeeded = 2, Max = 3, }; pub const ScmPhysicalDeviceReinit_Success = SCM_PD_MEDIA_REINITIALIZATION_STATUS.Success; pub const ScmPhysicalDeviceReinit_RebootNeeded = SCM_PD_MEDIA_REINITIALIZATION_STATUS.RebootNeeded; pub const ScmPhysicalDeviceReinit_ColdBootNeeded = SCM_PD_MEDIA_REINITIALIZATION_STATUS.ColdBootNeeded; pub const ScmPhysicalDeviceReinit_Max = SCM_PD_MEDIA_REINITIALIZATION_STATUS.Max; pub const SCM_PD_REINITIALIZE_MEDIA_OUTPUT = extern struct { Version: u32, Size: u32, Status: SCM_PD_MEDIA_REINITIALIZATION_STATUS, }; pub const MEDIA_TYPE = enum(i32) { Unknown = 0, F5_1Pt2_512 = 1, F3_1Pt44_512 = 2, F3_2Pt88_512 = 3, F3_20Pt8_512 = 4, F3_720_512 = 5, F5_360_512 = 6, F5_320_512 = 7, F5_320_1024 = 8, F5_180_512 = 9, F5_160_512 = 10, RemovableMedia = 11, FixedMedia = 12, F3_120M_512 = 13, F3_640_512 = 14, F5_640_512 = 15, F5_720_512 = 16, F3_1Pt2_512 = 17, F3_1Pt23_1024 = 18, F5_1Pt23_1024 = 19, F3_128Mb_512 = 20, F3_230Mb_512 = 21, F8_256_128 = 22, F3_200Mb_512 = 23, F3_240M_512 = 24, F3_32M_512 = 25, }; pub const Unknown = MEDIA_TYPE.Unknown; pub const F5_1Pt2_512 = MEDIA_TYPE.F5_1Pt2_512; pub const F3_1Pt44_512 = MEDIA_TYPE.F3_1Pt44_512; pub const F3_2Pt88_512 = MEDIA_TYPE.F3_2Pt88_512; pub const F3_20Pt8_512 = MEDIA_TYPE.F3_20Pt8_512; pub const F3_720_512 = MEDIA_TYPE.F3_720_512; pub const F5_360_512 = MEDIA_TYPE.F5_360_512; pub const F5_320_512 = MEDIA_TYPE.F5_320_512; pub const F5_320_1024 = MEDIA_TYPE.F5_320_1024; pub const F5_180_512 = MEDIA_TYPE.F5_180_512; pub const F5_160_512 = MEDIA_TYPE.F5_160_512; pub const RemovableMedia = MEDIA_TYPE.RemovableMedia; pub const FixedMedia = MEDIA_TYPE.FixedMedia; pub const F3_120M_512 = MEDIA_TYPE.F3_120M_512; pub const F3_640_512 = MEDIA_TYPE.F3_640_512; pub const F5_640_512 = MEDIA_TYPE.F5_640_512; pub const F5_720_512 = MEDIA_TYPE.F5_720_512; pub const F3_1Pt2_512 = MEDIA_TYPE.F3_1Pt2_512; pub const F3_1Pt23_1024 = MEDIA_TYPE.F3_1Pt23_1024; pub const F5_1Pt23_1024 = MEDIA_TYPE.F5_1Pt23_1024; pub const F3_128Mb_512 = MEDIA_TYPE.F3_128Mb_512; pub const F3_230Mb_512 = MEDIA_TYPE.F3_230Mb_512; pub const F8_256_128 = MEDIA_TYPE.F8_256_128; pub const F3_200Mb_512 = MEDIA_TYPE.F3_200Mb_512; pub const F3_240M_512 = MEDIA_TYPE.F3_240M_512; pub const F3_32M_512 = MEDIA_TYPE.F3_32M_512; pub const FORMAT_PARAMETERS = extern struct { MediaType: MEDIA_TYPE, StartCylinderNumber: u32, EndCylinderNumber: u32, StartHeadNumber: u32, EndHeadNumber: u32, }; pub const FORMAT_EX_PARAMETERS = extern struct { MediaType: MEDIA_TYPE, StartCylinderNumber: u32, EndCylinderNumber: u32, StartHeadNumber: u32, EndHeadNumber: u32, FormatGapLength: u16, SectorsPerTrack: u16, SectorNumber: [1]u16, }; pub const DISK_GEOMETRY = extern struct { Cylinders: LARGE_INTEGER, MediaType: MEDIA_TYPE, TracksPerCylinder: u32, SectorsPerTrack: u32, BytesPerSector: u32, }; pub const PARTITION_INFORMATION = extern struct { StartingOffset: LARGE_INTEGER, PartitionLength: LARGE_INTEGER, HiddenSectors: u32, PartitionNumber: u32, PartitionType: u8, BootIndicator: BOOLEAN, RecognizedPartition: BOOLEAN, RewritePartition: BOOLEAN, }; pub const SET_PARTITION_INFORMATION = extern struct { PartitionType: u8, }; pub const DRIVE_LAYOUT_INFORMATION = extern struct { PartitionCount: u32, Signature: u32, PartitionEntry: [1]PARTITION_INFORMATION, }; pub const VERIFY_INFORMATION = extern struct { StartingOffset: LARGE_INTEGER, Length: u32, }; pub const REASSIGN_BLOCKS = extern struct { Reserved: u16, Count: u16, BlockNumber: [1]u32, }; pub const REASSIGN_BLOCKS_EX = packed struct { Reserved: u16, Count: u16, BlockNumber: [1]LARGE_INTEGER, }; pub const PARTITION_STYLE = enum(i32) { MBR = 0, GPT = 1, RAW = 2, }; pub const PARTITION_STYLE_MBR = PARTITION_STYLE.MBR; pub const PARTITION_STYLE_GPT = PARTITION_STYLE.GPT; pub const PARTITION_STYLE_RAW = PARTITION_STYLE.RAW; pub const PARTITION_INFORMATION_GPT = extern struct { PartitionType: Guid, PartitionId: Guid, Attributes: GPT_ATTRIBUTES, Name: [36]u16, }; pub const PARTITION_INFORMATION_MBR = extern struct { PartitionType: u8, BootIndicator: BOOLEAN, RecognizedPartition: BOOLEAN, HiddenSectors: u32, PartitionId: Guid, }; pub const SET_PARTITION_INFORMATION_EX = extern struct { PartitionStyle: PARTITION_STYLE, Anonymous: extern union { Mbr: SET_PARTITION_INFORMATION, Gpt: PARTITION_INFORMATION_GPT, }, }; pub const CREATE_DISK_GPT = extern struct { DiskId: Guid, MaxPartitionCount: u32, }; pub const CREATE_DISK_MBR = extern struct { Signature: u32, }; pub const CREATE_DISK = extern struct { PartitionStyle: PARTITION_STYLE, Anonymous: extern union { Mbr: CREATE_DISK_MBR, Gpt: CREATE_DISK_GPT, }, }; pub const GET_LENGTH_INFORMATION = extern struct { Length: LARGE_INTEGER, }; pub const PARTITION_INFORMATION_EX = extern struct { PartitionStyle: PARTITION_STYLE, StartingOffset: LARGE_INTEGER, PartitionLength: LARGE_INTEGER, PartitionNumber: u32, RewritePartition: BOOLEAN, IsServicePartition: BOOLEAN, Anonymous: extern union { Mbr: PARTITION_INFORMATION_MBR, Gpt: PARTITION_INFORMATION_GPT, }, }; pub const DRIVE_LAYOUT_INFORMATION_GPT = extern struct { DiskId: Guid, StartingUsableOffset: LARGE_INTEGER, UsableLength: LARGE_INTEGER, MaxPartitionCount: u32, }; pub const DRIVE_LAYOUT_INFORMATION_MBR = extern struct { Signature: u32, CheckSum: u32, }; pub const DRIVE_LAYOUT_INFORMATION_EX = extern struct { PartitionStyle: u32, PartitionCount: u32, Anonymous: extern union { Mbr: DRIVE_LAYOUT_INFORMATION_MBR, Gpt: DRIVE_LAYOUT_INFORMATION_GPT, }, PartitionEntry: [1]PARTITION_INFORMATION_EX, }; pub const DETECTION_TYPE = enum(i32) { None = 0, Int13 = 1, ExInt13 = 2, }; pub const DetectNone = DETECTION_TYPE.None; pub const DetectInt13 = DETECTION_TYPE.Int13; pub const DetectExInt13 = DETECTION_TYPE.ExInt13; pub const DISK_INT13_INFO = extern struct { DriveSelect: u16, MaxCylinders: u32, SectorsPerTrack: u16, MaxHeads: u16, NumberDrives: u16, }; pub const DISK_EX_INT13_INFO = extern struct { ExBufferSize: u16, ExFlags: u16, ExCylinders: u32, ExHeads: u32, ExSectorsPerTrack: u32, ExSectorsPerDrive: u64, ExSectorSize: u16, ExReserved: u16, }; pub const DISK_DETECTION_INFO = extern struct { SizeOfDetectInfo: u32, DetectionType: DETECTION_TYPE, Anonymous: extern union { Anonymous: extern struct { Int13: DISK_INT13_INFO, ExInt13: DISK_EX_INT13_INFO, }, }, }; pub const DISK_PARTITION_INFO = extern struct { SizeOfPartitionInfo: u32, PartitionStyle: PARTITION_STYLE, Anonymous: extern union { Mbr: extern struct { Signature: u32, CheckSum: u32, }, Gpt: extern struct { DiskId: Guid, }, }, }; pub const DISK_GEOMETRY_EX = extern struct { Geometry: DISK_GEOMETRY, DiskSize: LARGE_INTEGER, Data: [1]u8, }; pub const DISK_CONTROLLER_NUMBER = extern struct { ControllerNumber: u32, DiskNumber: u32, }; pub const DISK_CACHE_RETENTION_PRIORITY = enum(i32) { EqualPriority = 0, KeepPrefetchedData = 1, KeepReadData = 2, }; pub const EqualPriority = DISK_CACHE_RETENTION_PRIORITY.EqualPriority; pub const KeepPrefetchedData = DISK_CACHE_RETENTION_PRIORITY.KeepPrefetchedData; pub const KeepReadData = DISK_CACHE_RETENTION_PRIORITY.KeepReadData; pub const DISK_CACHE_INFORMATION = extern struct { ParametersSavable: BOOLEAN, ReadCacheEnabled: BOOLEAN, WriteCacheEnabled: BOOLEAN, ReadRetentionPriority: DISK_CACHE_RETENTION_PRIORITY, WriteRetentionPriority: DISK_CACHE_RETENTION_PRIORITY, DisablePrefetchTransferLength: u16, PrefetchScalar: BOOLEAN, Anonymous: extern union { ScalarPrefetch: extern struct { Minimum: u16, Maximum: u16, MaximumBlocks: u16, }, BlockPrefetch: extern struct { Minimum: u16, Maximum: u16, }, }, }; pub const DISK_GROW_PARTITION = extern struct { PartitionNumber: u32, BytesToGrow: LARGE_INTEGER, }; pub const HISTOGRAM_BUCKET = extern struct { Reads: u32, Writes: u32, }; pub const DISK_HISTOGRAM = extern struct { DiskSize: LARGE_INTEGER, Start: LARGE_INTEGER, End: LARGE_INTEGER, Average: LARGE_INTEGER, AverageRead: LARGE_INTEGER, AverageWrite: LARGE_INTEGER, Granularity: u32, Size: u32, ReadCount: u32, WriteCount: u32, Histogram: ?*HISTOGRAM_BUCKET, }; pub const DISK_PERFORMANCE = extern struct { BytesRead: LARGE_INTEGER, BytesWritten: LARGE_INTEGER, ReadTime: LARGE_INTEGER, WriteTime: LARGE_INTEGER, IdleTime: LARGE_INTEGER, ReadCount: u32, WriteCount: u32, QueueDepth: u32, SplitCount: u32, QueryTime: LARGE_INTEGER, StorageDeviceNumber: u32, StorageManagerName: [8]u16, }; pub const DISK_RECORD = extern struct { ByteOffset: LARGE_INTEGER, StartTime: LARGE_INTEGER, EndTime: LARGE_INTEGER, VirtualAddress: ?*anyopaque, NumberOfBytes: u32, DeviceNumber: u8, ReadRequest: BOOLEAN, }; pub const DISK_LOGGING = extern struct { Function: u8, BufferAddress: ?*anyopaque, BufferSize: u32, }; pub const BIN_TYPES = enum(i32) { Size = 0, Location = 1, }; pub const RequestSize = BIN_TYPES.Size; pub const RequestLocation = BIN_TYPES.Location; pub const BIN_RANGE = extern struct { StartValue: LARGE_INTEGER, Length: LARGE_INTEGER, }; pub const PERF_BIN = extern struct { NumberOfBins: u32, TypeOfBin: u32, BinsRanges: [1]BIN_RANGE, }; pub const BIN_COUNT = extern struct { BinRange: BIN_RANGE, BinCount: u32, }; pub const BIN_RESULTS = extern struct { NumberOfBins: u32, BinCounts: [1]BIN_COUNT, }; pub const GETVERSIONINPARAMS = packed struct { bVersion: u8, bRevision: u8, bReserved: u8, bIDEDeviceMap: u8, fCapabilities: u32, dwReserved: [4]u32, }; pub const IDEREGS = extern struct { bFeaturesReg: u8, bSectorCountReg: u8, bSectorNumberReg: u8, bCylLowReg: u8, bCylHighReg: u8, bDriveHeadReg: u8, bCommandReg: u8, bReserved: u8, }; pub const SENDCMDINPARAMS = packed struct { cBufferSize: u32, irDriveRegs: IDEREGS, bDriveNumber: u8, bReserved: [3]u8, dwReserved: [4]u32, bBuffer: [1]u8, }; pub const DRIVERSTATUS = packed struct { bDriverError: u8, bIDEError: u8, bReserved: [2]u8, dwReserved: [2]u32, }; pub const SENDCMDOUTPARAMS = packed struct { cBufferSize: u32, DriverStatus: DRIVERSTATUS, bBuffer: [1]u8, }; pub const GET_DISK_ATTRIBUTES = extern struct { Version: u32, Reserved1: u32, Attributes: u64, }; pub const SET_DISK_ATTRIBUTES = extern struct { Version: u32, Persist: BOOLEAN, Reserved1: [3]u8, Attributes: u64, AttributesMask: u64, Reserved2: [4]u32, }; pub const ELEMENT_TYPE = enum(i32) { AllElements = 0, ChangerTransport = 1, ChangerSlot = 2, ChangerIEPort = 3, ChangerDrive = 4, ChangerDoor = 5, ChangerKeypad = 6, ChangerMaxElement = 7, }; pub const AllElements = ELEMENT_TYPE.AllElements; pub const ChangerTransport = ELEMENT_TYPE.ChangerTransport; pub const ChangerSlot = ELEMENT_TYPE.ChangerSlot; pub const ChangerIEPort = ELEMENT_TYPE.ChangerIEPort; pub const ChangerDrive = ELEMENT_TYPE.ChangerDrive; pub const ChangerDoor = ELEMENT_TYPE.ChangerDoor; pub const ChangerKeypad = ELEMENT_TYPE.ChangerKeypad; pub const ChangerMaxElement = ELEMENT_TYPE.ChangerMaxElement; pub const CHANGER_ELEMENT = extern struct { ElementType: ELEMENT_TYPE, ElementAddress: u32, }; pub const CHANGER_ELEMENT_LIST = extern struct { Element: CHANGER_ELEMENT, NumberOfElements: u32, }; pub const GET_CHANGER_PARAMETERS = extern struct { Size: u32, NumberTransportElements: u16, NumberStorageElements: u16, NumberCleanerSlots: u16, NumberIEElements: u16, NumberDataTransferElements: u16, NumberOfDoors: u16, FirstSlotNumber: u16, FirstDriveNumber: u16, FirstTransportNumber: u16, FirstIEPortNumber: u16, FirstCleanerSlotAddress: u16, MagazineSize: u16, DriveCleanTimeout: u32, Features0: CHANGER_FEATURES, Features1: GET_CHANGER_PARAMETERS_FEATURES1, MoveFromTransport: u8, MoveFromSlot: u8, MoveFromIePort: u8, MoveFromDrive: u8, ExchangeFromTransport: u8, ExchangeFromSlot: u8, ExchangeFromIePort: u8, ExchangeFromDrive: u8, LockUnlockCapabilities: u8, PositionCapabilities: u8, Reserved1: [2]u8, Reserved2: [2]u32, }; pub const CHANGER_PRODUCT_DATA = extern struct { VendorId: [8]u8, ProductId: [16]u8, Revision: [4]u8, SerialNumber: [32]u8, DeviceType: u8, }; pub const CHANGER_SET_ACCESS = extern struct { Element: CHANGER_ELEMENT, Control: u32, }; pub const CHANGER_READ_ELEMENT_STATUS = extern struct { ElementList: CHANGER_ELEMENT_LIST, VolumeTagInfo: BOOLEAN, }; pub const CHANGER_ELEMENT_STATUS = extern struct { Element: CHANGER_ELEMENT, SrcElementAddress: CHANGER_ELEMENT, Flags: CHANGER_ELEMENT_STATUS_FLAGS, ExceptionCode: u32, TargetId: u8, Lun: u8, Reserved: u16, PrimaryVolumeID: [36]u8, AlternateVolumeID: [36]u8, }; pub const CHANGER_ELEMENT_STATUS_EX = extern struct { Element: CHANGER_ELEMENT, SrcElementAddress: CHANGER_ELEMENT, Flags: CHANGER_ELEMENT_STATUS_FLAGS, ExceptionCode: u32, TargetId: u8, Lun: u8, Reserved: u16, PrimaryVolumeID: [36]u8, AlternateVolumeID: [36]u8, VendorIdentification: [8]u8, ProductIdentification: [16]u8, SerialNumber: [32]u8, }; pub const CHANGER_INITIALIZE_ELEMENT_STATUS = extern struct { ElementList: CHANGER_ELEMENT_LIST, BarCodeScan: BOOLEAN, }; pub const CHANGER_SET_POSITION = extern struct { Transport: CHANGER_ELEMENT, Destination: CHANGER_ELEMENT, Flip: BOOLEAN, }; pub const CHANGER_EXCHANGE_MEDIUM = extern struct { Transport: CHANGER_ELEMENT, Source: CHANGER_ELEMENT, Destination1: CHANGER_ELEMENT, Destination2: CHANGER_ELEMENT, Flip1: BOOLEAN, Flip2: BOOLEAN, }; pub const CHANGER_MOVE_MEDIUM = extern struct { Transport: CHANGER_ELEMENT, Source: CHANGER_ELEMENT, Destination: CHANGER_ELEMENT, Flip: BOOLEAN, }; pub const CHANGER_SEND_VOLUME_TAG_INFORMATION = extern struct { StartingElement: CHANGER_ELEMENT, ActionCode: u32, VolumeIDTemplate: [40]u8, }; pub const READ_ELEMENT_ADDRESS_INFO = extern struct { NumberOfElements: u32, ElementStatus: [1]CHANGER_ELEMENT_STATUS, }; pub const CHANGER_DEVICE_PROBLEM_TYPE = enum(i32) { None = 0, Hardware = 1, CHMError = 2, DoorOpen = 3, CalibrationError = 4, TargetFailure = 5, CHMMoveError = 6, CHMZeroError = 7, CartridgeInsertError = 8, PositionError = 9, SensorError = 10, CartridgeEjectError = 11, GripperError = 12, DriveError = 13, }; pub const DeviceProblemNone = CHANGER_DEVICE_PROBLEM_TYPE.None; pub const DeviceProblemHardware = CHANGER_DEVICE_PROBLEM_TYPE.Hardware; pub const DeviceProblemCHMError = CHANGER_DEVICE_PROBLEM_TYPE.CHMError; pub const DeviceProblemDoorOpen = CHANGER_DEVICE_PROBLEM_TYPE.DoorOpen; pub const DeviceProblemCalibrationError = CHANGER_DEVICE_PROBLEM_TYPE.CalibrationError; pub const DeviceProblemTargetFailure = CHANGER_DEVICE_PROBLEM_TYPE.TargetFailure; pub const DeviceProblemCHMMoveError = CHANGER_DEVICE_PROBLEM_TYPE.CHMMoveError; pub const DeviceProblemCHMZeroError = CHANGER_DEVICE_PROBLEM_TYPE.CHMZeroError; pub const DeviceProblemCartridgeInsertError = CHANGER_DEVICE_PROBLEM_TYPE.CartridgeInsertError; pub const DeviceProblemPositionError = CHANGER_DEVICE_PROBLEM_TYPE.PositionError; pub const DeviceProblemSensorError = CHANGER_DEVICE_PROBLEM_TYPE.SensorError; pub const DeviceProblemCartridgeEjectError = CHANGER_DEVICE_PROBLEM_TYPE.CartridgeEjectError; pub const DeviceProblemGripperError = CHANGER_DEVICE_PROBLEM_TYPE.GripperError; pub const DeviceProblemDriveError = CHANGER_DEVICE_PROBLEM_TYPE.DriveError; pub const PATHNAME_BUFFER = extern struct { PathNameLength: u32, Name: [1]u16, }; pub const FSCTL_QUERY_FAT_BPB_BUFFER = extern struct { First0x24BytesOfBootSector: [36]u8, }; pub const NTFS_VOLUME_DATA_BUFFER = extern struct { VolumeSerialNumber: LARGE_INTEGER, NumberSectors: LARGE_INTEGER, TotalClusters: LARGE_INTEGER, FreeClusters: LARGE_INTEGER, TotalReserved: LARGE_INTEGER, BytesPerSector: u32, BytesPerCluster: u32, BytesPerFileRecordSegment: u32, ClustersPerFileRecordSegment: u32, MftValidDataLength: LARGE_INTEGER, MftStartLcn: LARGE_INTEGER, Mft2StartLcn: LARGE_INTEGER, MftZoneStart: LARGE_INTEGER, MftZoneEnd: LARGE_INTEGER, }; pub const NTFS_EXTENDED_VOLUME_DATA = extern struct { ByteCount: u32, MajorVersion: u16, MinorVersion: u16, BytesPerPhysicalSector: u32, LfsMajorVersion: u16, LfsMinorVersion: u16, MaxDeviceTrimExtentCount: u32, MaxDeviceTrimByteCount: u32, MaxVolumeTrimExtentCount: u32, MaxVolumeTrimByteCount: u32, }; pub const REFS_VOLUME_DATA_BUFFER = extern struct { ByteCount: u32, MajorVersion: u32, MinorVersion: u32, BytesPerPhysicalSector: u32, VolumeSerialNumber: LARGE_INTEGER, NumberSectors: LARGE_INTEGER, TotalClusters: LARGE_INTEGER, FreeClusters: LARGE_INTEGER, TotalReserved: LARGE_INTEGER, BytesPerSector: u32, BytesPerCluster: u32, MaximumSizeOfResidentFile: LARGE_INTEGER, FastTierDataFillRatio: u16, SlowTierDataFillRatio: u16, DestagesFastTierToSlowTierRate: u32, Reserved: [9]LARGE_INTEGER, }; pub const STARTING_LCN_INPUT_BUFFER = extern struct { StartingLcn: LARGE_INTEGER, }; pub const STARTING_LCN_INPUT_BUFFER_EX = extern struct { StartingLcn: LARGE_INTEGER, Flags: u32, }; pub const VOLUME_BITMAP_BUFFER = extern struct { StartingLcn: LARGE_INTEGER, BitmapSize: LARGE_INTEGER, Buffer: [1]u8, }; pub const STARTING_VCN_INPUT_BUFFER = extern struct { StartingVcn: LARGE_INTEGER, }; pub const RETRIEVAL_POINTERS_BUFFER = extern struct { ExtentCount: u32, StartingVcn: LARGE_INTEGER, Extents: [1]extern struct { NextVcn: LARGE_INTEGER, Lcn: LARGE_INTEGER, }, }; pub const RETRIEVAL_POINTERS_AND_REFCOUNT_BUFFER = extern struct { ExtentCount: u32, StartingVcn: LARGE_INTEGER, Extents: [1]extern struct { NextVcn: LARGE_INTEGER, Lcn: LARGE_INTEGER, ReferenceCount: u32, }, }; pub const RETRIEVAL_POINTER_COUNT = extern struct { ExtentCount: u32, }; pub const NTFS_FILE_RECORD_INPUT_BUFFER = extern struct { FileReferenceNumber: LARGE_INTEGER, }; pub const NTFS_FILE_RECORD_OUTPUT_BUFFER = extern struct { FileReferenceNumber: LARGE_INTEGER, FileRecordLength: u32, FileRecordBuffer: [1]u8, }; pub const MOVE_FILE_DATA = extern struct { FileHandle: ?HANDLE, StartingVcn: LARGE_INTEGER, StartingLcn: LARGE_INTEGER, ClusterCount: u32, }; pub const MOVE_FILE_RECORD_DATA = extern struct { FileHandle: ?HANDLE, SourceFileRecord: LARGE_INTEGER, TargetFileRecord: LARGE_INTEGER, }; pub const FIND_BY_SID_DATA = extern struct { Restart: u32, Sid: SID, }; pub const FIND_BY_SID_OUTPUT = extern struct { NextEntryOffset: u32, FileIndex: u32, FileNameLength: u32, FileName: [1]u16, }; pub const MFT_ENUM_DATA_V0 = extern struct { StartFileReferenceNumber: u64, LowUsn: i64, HighUsn: i64, }; pub const MFT_ENUM_DATA_V1 = extern struct { StartFileReferenceNumber: u64, LowUsn: i64, HighUsn: i64, MinMajorVersion: u16, MaxMajorVersion: u16, }; pub const CREATE_USN_JOURNAL_DATA = extern struct { MaximumSize: u64, AllocationDelta: u64, }; pub const READ_FILE_USN_DATA = extern struct { MinMajorVersion: u16, MaxMajorVersion: u16, }; pub const READ_USN_JOURNAL_DATA_V0 = extern struct { StartUsn: i64, ReasonMask: u32, ReturnOnlyOnClose: u32, Timeout: u64, BytesToWaitFor: u64, UsnJournalID: u64, }; pub const READ_USN_JOURNAL_DATA_V1 = extern struct { StartUsn: i64, ReasonMask: u32, ReturnOnlyOnClose: u32, Timeout: u64, BytesToWaitFor: u64, UsnJournalID: u64, MinMajorVersion: u16, MaxMajorVersion: u16, }; pub const USN_TRACK_MODIFIED_RANGES = extern struct { Flags: u32, Unused: u32, ChunkSize: u64, FileSizeThreshold: i64, }; pub const USN_RANGE_TRACK_OUTPUT = extern struct { Usn: i64, }; pub const USN_RECORD_V2 = extern struct { RecordLength: u32, MajorVersion: u16, MinorVersion: u16, FileReferenceNumber: u64, ParentFileReferenceNumber: u64, Usn: i64, TimeStamp: LARGE_INTEGER, Reason: u32, SourceInfo: u32, SecurityId: u32, FileAttributes: u32, FileNameLength: u16, FileNameOffset: u16, FileName: [1]u16, }; pub const USN_RECORD_V3 = extern struct { RecordLength: u32, MajorVersion: u16, MinorVersion: u16, FileReferenceNumber: FILE_ID_128, ParentFileReferenceNumber: FILE_ID_128, Usn: i64, TimeStamp: LARGE_INTEGER, Reason: u32, SourceInfo: u32, SecurityId: u32, FileAttributes: u32, FileNameLength: u16, FileNameOffset: u16, FileName: [1]u16, }; pub const USN_RECORD_COMMON_HEADER = extern struct { RecordLength: u32, MajorVersion: u16, MinorVersion: u16, }; pub const USN_RECORD_EXTENT = extern struct { Offset: i64, Length: i64, }; pub const USN_RECORD_V4 = extern struct { Header: USN_RECORD_COMMON_HEADER, FileReferenceNumber: FILE_ID_128, ParentFileReferenceNumber: FILE_ID_128, Usn: i64, Reason: u32, SourceInfo: USN_SOURCE_INFO_ID, RemainingExtents: u32, NumberOfExtents: u16, ExtentSize: u16, Extents: [1]USN_RECORD_EXTENT, }; pub const USN_RECORD_UNION = extern union { Header: USN_RECORD_COMMON_HEADER, V2: USN_RECORD_V2, V3: USN_RECORD_V3, V4: USN_RECORD_V4, }; pub const USN_JOURNAL_DATA_V0 = extern struct { UsnJournalID: u64, FirstUsn: i64, NextUsn: i64, LowestValidUsn: i64, MaxUsn: i64, MaximumSize: u64, AllocationDelta: u64, }; pub const USN_JOURNAL_DATA_V1 = extern struct { UsnJournalID: u64, FirstUsn: i64, NextUsn: i64, LowestValidUsn: i64, MaxUsn: i64, MaximumSize: u64, AllocationDelta: u64, MinSupportedMajorVersion: u16, MaxSupportedMajorVersion: u16, }; pub const USN_JOURNAL_DATA_V2 = extern struct { UsnJournalID: u64, FirstUsn: i64, NextUsn: i64, LowestValidUsn: i64, MaxUsn: i64, MaximumSize: u64, AllocationDelta: u64, MinSupportedMajorVersion: u16, MaxSupportedMajorVersion: u16, Flags: u32, RangeTrackChunkSize: u64, RangeTrackFileSizeThreshold: i64, }; pub const DELETE_USN_JOURNAL_DATA = extern struct { UsnJournalID: u64, DeleteFlags: USN_DELETE_FLAGS, }; pub const MARK_HANDLE_INFO = extern struct { Anonymous: extern union { UsnSourceInfo: u32, CopyNumber: u32, }, VolumeHandle: ?HANDLE, HandleInfo: u32, }; pub const BULK_SECURITY_TEST_DATA = extern struct { DesiredAccess: u32, SecurityIds: [1]u32, }; pub const FILE_PREFETCH = extern struct { Type: u32, Count: u32, Prefetch: [1]u64, }; pub const FILE_PREFETCH_EX = extern struct { Type: u32, Count: u32, Context: ?*anyopaque, Prefetch: [1]u64, }; pub const FILESYSTEM_STATISTICS = extern struct { FileSystemType: FILESYSTEM_STATISTICS_TYPE, Version: u16, SizeOfCompleteStructure: u32, UserFileReads: u32, UserFileReadBytes: u32, UserDiskReads: u32, UserFileWrites: u32, UserFileWriteBytes: u32, UserDiskWrites: u32, MetaDataReads: u32, MetaDataReadBytes: u32, MetaDataDiskReads: u32, MetaDataWrites: u32, MetaDataWriteBytes: u32, MetaDataDiskWrites: u32, }; pub const FAT_STATISTICS = extern struct { CreateHits: u32, SuccessfulCreates: u32, FailedCreates: u32, NonCachedReads: u32, NonCachedReadBytes: u32, NonCachedWrites: u32, NonCachedWriteBytes: u32, NonCachedDiskReads: u32, NonCachedDiskWrites: u32, }; pub const EXFAT_STATISTICS = extern struct { CreateHits: u32, SuccessfulCreates: u32, FailedCreates: u32, NonCachedReads: u32, NonCachedReadBytes: u32, NonCachedWrites: u32, NonCachedWriteBytes: u32, NonCachedDiskReads: u32, NonCachedDiskWrites: u32, }; pub const NTFS_STATISTICS = extern struct { LogFileFullExceptions: u32, OtherExceptions: u32, MftReads: u32, MftReadBytes: u32, MftWrites: u32, MftWriteBytes: u32, MftWritesUserLevel: extern struct { Write: u16, Create: u16, SetInfo: u16, Flush: u16, }, MftWritesFlushForLogFileFull: u16, MftWritesLazyWriter: u16, MftWritesUserRequest: u16, Mft2Writes: u32, Mft2WriteBytes: u32, Mft2WritesUserLevel: extern struct { Write: u16, Create: u16, SetInfo: u16, Flush: u16, }, Mft2WritesFlushForLogFileFull: u16, Mft2WritesLazyWriter: u16, Mft2WritesUserRequest: u16, RootIndexReads: u32, RootIndexReadBytes: u32, RootIndexWrites: u32, RootIndexWriteBytes: u32, BitmapReads: u32, BitmapReadBytes: u32, BitmapWrites: u32, BitmapWriteBytes: u32, BitmapWritesFlushForLogFileFull: u16, BitmapWritesLazyWriter: u16, BitmapWritesUserRequest: u16, BitmapWritesUserLevel: extern struct { Write: u16, Create: u16, SetInfo: u16, }, MftBitmapReads: u32, MftBitmapReadBytes: u32, MftBitmapWrites: u32, MftBitmapWriteBytes: u32, MftBitmapWritesFlushForLogFileFull: u16, MftBitmapWritesLazyWriter: u16, MftBitmapWritesUserRequest: u16, MftBitmapWritesUserLevel: extern struct { Write: u16, Create: u16, SetInfo: u16, Flush: u16, }, UserIndexReads: u32, UserIndexReadBytes: u32, UserIndexWrites: u32, UserIndexWriteBytes: u32, LogFileReads: u32, LogFileReadBytes: u32, LogFileWrites: u32, LogFileWriteBytes: u32, Allocate: extern struct { Calls: u32, Clusters: u32, Hints: u32, RunsReturned: u32, HintsHonored: u32, HintsClusters: u32, Cache: u32, CacheClusters: u32, CacheMiss: u32, CacheMissClusters: u32, }, DiskResourcesExhausted: u32, }; pub const FILESYSTEM_STATISTICS_EX = extern struct { FileSystemType: FILESYSTEM_STATISTICS_TYPE, Version: u16, SizeOfCompleteStructure: u32, UserFileReads: u64, UserFileReadBytes: u64, UserDiskReads: u64, UserFileWrites: u64, UserFileWriteBytes: u64, UserDiskWrites: u64, MetaDataReads: u64, MetaDataReadBytes: u64, MetaDataDiskReads: u64, MetaDataWrites: u64, MetaDataWriteBytes: u64, MetaDataDiskWrites: u64, }; pub const NTFS_STATISTICS_EX = extern struct { LogFileFullExceptions: u32, OtherExceptions: u32, MftReads: u64, MftReadBytes: u64, MftWrites: u64, MftWriteBytes: u64, MftWritesUserLevel: extern struct { Write: u32, Create: u32, SetInfo: u32, Flush: u32, }, MftWritesFlushForLogFileFull: u32, MftWritesLazyWriter: u32, MftWritesUserRequest: u32, Mft2Writes: u64, Mft2WriteBytes: u64, Mft2WritesUserLevel: extern struct { Write: u32, Create: u32, SetInfo: u32, Flush: u32, }, Mft2WritesFlushForLogFileFull: u32, Mft2WritesLazyWriter: u32, Mft2WritesUserRequest: u32, RootIndexReads: u64, RootIndexReadBytes: u64, RootIndexWrites: u64, RootIndexWriteBytes: u64, BitmapReads: u64, BitmapReadBytes: u64, BitmapWrites: u64, BitmapWriteBytes: u64, BitmapWritesFlushForLogFileFull: u32, BitmapWritesLazyWriter: u32, BitmapWritesUserRequest: u32, BitmapWritesUserLevel: extern struct { Write: u32, Create: u32, SetInfo: u32, Flush: u32, }, MftBitmapReads: u64, MftBitmapReadBytes: u64, MftBitmapWrites: u64, MftBitmapWriteBytes: u64, MftBitmapWritesFlushForLogFileFull: u32, MftBitmapWritesLazyWriter: u32, MftBitmapWritesUserRequest: u32, MftBitmapWritesUserLevel: extern struct { Write: u32, Create: u32, SetInfo: u32, Flush: u32, }, UserIndexReads: u64, UserIndexReadBytes: u64, UserIndexWrites: u64, UserIndexWriteBytes: u64, LogFileReads: u64, LogFileReadBytes: u64, LogFileWrites: u64, LogFileWriteBytes: u64, Allocate: extern struct { Calls: u32, RunsReturned: u32, Hints: u32, HintsHonored: u32, Cache: u32, CacheMiss: u32, Clusters: u64, HintsClusters: u64, CacheClusters: u64, CacheMissClusters: u64, }, DiskResourcesExhausted: u32, VolumeTrimCount: u64, VolumeTrimTime: u64, VolumeTrimByteCount: u64, FileLevelTrimCount: u64, FileLevelTrimTime: u64, FileLevelTrimByteCount: u64, VolumeTrimSkippedCount: u64, VolumeTrimSkippedByteCount: u64, NtfsFillStatInfoFromMftRecordCalledCount: u64, NtfsFillStatInfoFromMftRecordBailedBecauseOfAttributeListCount: u64, NtfsFillStatInfoFromMftRecordBailedBecauseOfNonResReparsePointCount: u64, }; pub const FILE_OBJECTID_BUFFER = extern struct { ObjectId: [16]u8, Anonymous: extern union { Anonymous: extern struct { BirthVolumeId: [16]u8, BirthObjectId: [16]u8, DomainId: [16]u8, }, ExtendedInfo: [48]u8, }, }; pub const FILE_SET_SPARSE_BUFFER = extern struct { SetSparse: BOOLEAN, }; pub const FILE_ZERO_DATA_INFORMATION = extern struct { FileOffset: LARGE_INTEGER, BeyondFinalZero: LARGE_INTEGER, }; pub const FILE_ZERO_DATA_INFORMATION_EX = extern struct { FileOffset: LARGE_INTEGER, BeyondFinalZero: LARGE_INTEGER, Flags: u32, }; pub const FILE_ALLOCATED_RANGE_BUFFER = extern struct { FileOffset: LARGE_INTEGER, Length: LARGE_INTEGER, }; pub const ENCRYPTION_BUFFER = extern struct { EncryptionOperation: u32, Private: [1]u8, }; pub const DECRYPTION_STATUS_BUFFER = extern struct { NoEncryptedStreams: BOOLEAN, }; pub const REQUEST_RAW_ENCRYPTED_DATA = extern struct { FileOffset: i64, Length: u32, }; pub const ENCRYPTED_DATA_INFO = extern struct { StartingFileOffset: u64, OutputBufferOffset: u32, BytesWithinFileSize: u32, BytesWithinValidDataLength: u32, CompressionFormat: u16, DataUnitShift: u8, ChunkShift: u8, ClusterShift: u8, EncryptionFormat: u8, NumberOfDataBlocks: u16, DataBlockSize: [1]u32, }; pub const EXTENDED_ENCRYPTED_DATA_INFO = extern struct { ExtendedCode: u32, Length: u32, Flags: u32, Reserved: u32, }; pub const PLEX_READ_DATA_REQUEST = extern struct { ByteOffset: LARGE_INTEGER, ByteLength: u32, PlexNumber: u32, }; pub const SI_COPYFILE = extern struct { SourceFileNameLength: u32, DestinationFileNameLength: u32, Flags: u32, FileNameBuffer: [1]u16, }; pub const FILE_MAKE_COMPATIBLE_BUFFER = extern struct { CloseDisc: BOOLEAN, }; pub const FILE_SET_DEFECT_MGMT_BUFFER = extern struct { Disable: BOOLEAN, }; pub const FILE_QUERY_SPARING_BUFFER = extern struct { SparingUnitBytes: u32, SoftwareSparing: BOOLEAN, TotalSpareBlocks: u32, FreeSpareBlocks: u32, }; pub const FILE_QUERY_ON_DISK_VOL_INFO_BUFFER = extern struct { DirectoryCount: LARGE_INTEGER, FileCount: LARGE_INTEGER, FsFormatMajVersion: u16, FsFormatMinVersion: u16, FsFormatName: [12]u16, FormatTime: LARGE_INTEGER, LastUpdateTime: LARGE_INTEGER, CopyrightInfo: [34]u16, AbstractInfo: [34]u16, FormattingImplementationInfo: [34]u16, LastModifyingImplementationInfo: [34]u16, }; pub const FILE_INITIATE_REPAIR_OUTPUT_BUFFER = extern struct { Hint1: u64, Hint2: u64, Clsn: u64, Status: u32, }; pub const SHRINK_VOLUME_REQUEST_TYPES = enum(i32) { Prepare = 1, Commit = 2, Abort = 3, }; pub const ShrinkPrepare = SHRINK_VOLUME_REQUEST_TYPES.Prepare; pub const ShrinkCommit = SHRINK_VOLUME_REQUEST_TYPES.Commit; pub const ShrinkAbort = SHRINK_VOLUME_REQUEST_TYPES.Abort; pub const SHRINK_VOLUME_INFORMATION = extern struct { ShrinkRequestType: SHRINK_VOLUME_REQUEST_TYPES, Flags: u64, NewNumberOfSectors: i64, }; pub const TXFS_MODIFY_RM = extern struct { Flags: TXFS_RMF_LAGS, LogContainerCountMax: u32, LogContainerCountMin: u32, LogContainerCount: u32, LogGrowthIncrement: u32, LogAutoShrinkPercentage: u32, Reserved: u64, LoggingMode: u16, }; pub const TXFS_QUERY_RM_INFORMATION = extern struct { BytesRequired: u32, TailLsn: u64, CurrentLsn: u64, ArchiveTailLsn: u64, LogContainerSize: u64, HighestVirtualClock: LARGE_INTEGER, LogContainerCount: u32, LogContainerCountMax: u32, LogContainerCountMin: u32, LogGrowthIncrement: u32, LogAutoShrinkPercentage: u32, Flags: TXFS_RMF_LAGS, LoggingMode: u16, Reserved: u16, RmState: u32, LogCapacity: u64, LogFree: u64, TopsSize: u64, TopsUsed: u64, TransactionCount: u64, OnePCCount: u64, TwoPCCount: u64, NumberLogFileFull: u64, OldestTransactionAge: u64, RMName: Guid, TmLogPathOffset: u32, }; pub const TXFS_ROLLFORWARD_REDO_INFORMATION = extern struct { LastVirtualClock: LARGE_INTEGER, LastRedoLsn: u64, HighestRecoveryLsn: u64, Flags: u32, }; pub const TXFS_START_RM_INFORMATION = extern struct { Flags: u32, LogContainerSize: u64, LogContainerCountMin: u32, LogContainerCountMax: u32, LogGrowthIncrement: u32, LogAutoShrinkPercentage: u32, TmLogPathOffset: u32, TmLogPathLength: u16, LoggingMode: u16, LogPathLength: u16, Reserved: u16, LogPath: [1]u16, }; pub const TXFS_GET_METADATA_INFO_OUT = extern struct { TxfFileId: extern struct { LowPart: i64, HighPart: i64, }, LockingTransaction: Guid, LastLsn: u64, TransactionState: u32, }; pub const TXFS_LIST_TRANSACTION_LOCKED_FILES_ENTRY = extern struct { Offset: u64, NameFlags: u32, FileId: i64, Reserved1: u32, Reserved2: u32, Reserved3: i64, FileName: [1]u16, }; pub const TXFS_LIST_TRANSACTION_LOCKED_FILES = extern struct { KtmTransaction: Guid, NumberOfFiles: u64, BufferSizeRequired: u64, Offset: u64, }; pub const TXFS_LIST_TRANSACTIONS_ENTRY = extern struct { TransactionId: Guid, TransactionState: u32, Reserved1: u32, Reserved2: u32, Reserved3: i64, }; pub const TXFS_LIST_TRANSACTIONS = extern struct { NumberOfTransactions: u64, BufferSizeRequired: u64, }; pub const TXFS_READ_BACKUP_INFORMATION_OUT = extern struct { Anonymous: extern union { BufferLength: u32, Buffer: [1]u8, }, }; pub const TXFS_WRITE_BACKUP_INFORMATION = extern struct { Buffer: [1]u8, }; pub const TXFS_GET_TRANSACTED_VERSION = extern struct { ThisBaseVersion: u32, LatestVersion: u32, ThisMiniVersion: u16, FirstMiniVersion: u16, LatestMiniVersion: u16, }; pub const TXFS_SAVEPOINT_INFORMATION = extern struct { KtmTransaction: ?HANDLE, ActionCode: u32, SavepointId: u32, }; pub const TXFS_CREATE_MINIVERSION_INFO = extern struct { StructureVersion: u16, StructureLength: u16, BaseVersion: u32, MiniVersion: u16, }; pub const TXFS_TRANSACTION_ACTIVE_INFO = extern struct { TransactionsActiveAtSnapshot: BOOLEAN, }; pub const BOOT_AREA_INFO = extern struct { BootSectorCount: u32, BootSectors: [2]extern struct { Offset: LARGE_INTEGER, }, }; pub const RETRIEVAL_POINTER_BASE = extern struct { FileAreaOffset: LARGE_INTEGER, }; pub const FILE_FS_PERSISTENT_VOLUME_INFORMATION = extern struct { VolumeFlags: u32, FlagMask: u32, Version: u32, Reserved: u32, }; pub const FILE_SYSTEM_RECOGNITION_INFORMATION = extern struct { FileSystem: [9]CHAR, }; pub const REQUEST_OPLOCK_INPUT_BUFFER = extern struct { StructureVersion: u16, StructureLength: u16, RequestedOplockLevel: u32, Flags: u32, }; pub const REQUEST_OPLOCK_OUTPUT_BUFFER = extern struct { StructureVersion: u16, StructureLength: u16, OriginalOplockLevel: u32, NewOplockLevel: u32, Flags: u32, AccessMode: u32, ShareMode: u16, }; pub const STORAGE_QUERY_DEPENDENT_VOLUME_REQUEST = extern struct { RequestLevel: u32, RequestFlags: u32, }; pub const STORAGE_QUERY_DEPENDENT_VOLUME_LEV1_ENTRY = extern struct { EntryLength: u32, DependencyTypeFlags: u32, ProviderSpecificFlags: u32, VirtualStorageType: VIRTUAL_STORAGE_TYPE, }; pub const STORAGE_QUERY_DEPENDENT_VOLUME_LEV2_ENTRY = extern struct { EntryLength: u32, DependencyTypeFlags: u32, ProviderSpecificFlags: u32, VirtualStorageType: VIRTUAL_STORAGE_TYPE, AncestorLevel: u32, HostVolumeNameOffset: u32, HostVolumeNameSize: u32, DependentVolumeNameOffset: u32, DependentVolumeNameSize: u32, RelativePathOffset: u32, RelativePathSize: u32, DependentDeviceNameOffset: u32, DependentDeviceNameSize: u32, }; pub const STORAGE_QUERY_DEPENDENT_VOLUME_RESPONSE = extern struct { ResponseLevel: u32, NumberEntries: u32, Anonymous: extern union { Lev1Depends: [1]STORAGE_QUERY_DEPENDENT_VOLUME_LEV1_ENTRY, Lev2Depends: [1]STORAGE_QUERY_DEPENDENT_VOLUME_LEV2_ENTRY, }, }; pub const SD_CHANGE_MACHINE_SID_INPUT = extern struct { CurrentMachineSIDOffset: u16, CurrentMachineSIDLength: u16, NewMachineSIDOffset: u16, NewMachineSIDLength: u16, }; pub const SD_CHANGE_MACHINE_SID_OUTPUT = extern struct { NumSDChangedSuccess: u64, NumSDChangedFail: u64, NumSDUnused: u64, NumSDTotal: u64, NumMftSDChangedSuccess: u64, NumMftSDChangedFail: u64, NumMftSDTotal: u64, }; pub const SD_QUERY_STATS_INPUT = extern struct { Reserved: u32, }; pub const SD_QUERY_STATS_OUTPUT = extern struct { SdsStreamSize: u64, SdsAllocationSize: u64, SiiStreamSize: u64, SiiAllocationSize: u64, SdhStreamSize: u64, SdhAllocationSize: u64, NumSDTotal: u64, NumSDUnused: u64, }; pub const SD_ENUM_SDS_INPUT = extern struct { StartingOffset: u64, MaxSDEntriesToReturn: u64, }; pub const SD_ENUM_SDS_ENTRY = extern struct { Hash: u32, SecurityId: u32, Offset: u64, Length: u32, Descriptor: [1]u8, }; pub const SD_ENUM_SDS_OUTPUT = extern struct { NextOffset: u64, NumSDEntriesReturned: u64, NumSDBytesReturned: u64, SDEntry: [1]SD_ENUM_SDS_ENTRY, }; pub const SD_GLOBAL_CHANGE_INPUT = extern struct { Flags: u32, ChangeType: u32, Anonymous: extern union { SdChange: SD_CHANGE_MACHINE_SID_INPUT, SdQueryStats: SD_QUERY_STATS_INPUT, SdEnumSds: SD_ENUM_SDS_INPUT, }, }; pub const SD_GLOBAL_CHANGE_OUTPUT = extern struct { Flags: u32, ChangeType: u32, Anonymous: extern union { SdChange: SD_CHANGE_MACHINE_SID_OUTPUT, SdQueryStats: SD_QUERY_STATS_OUTPUT, SdEnumSds: SD_ENUM_SDS_OUTPUT, }, }; pub const LOOKUP_STREAM_FROM_CLUSTER_INPUT = extern struct { Flags: u32, NumberOfClusters: u32, Cluster: [1]LARGE_INTEGER, }; pub const LOOKUP_STREAM_FROM_CLUSTER_OUTPUT = extern struct { Offset: u32, NumberOfMatches: u32, BufferSizeRequired: u32, }; pub const LOOKUP_STREAM_FROM_CLUSTER_ENTRY = extern struct { OffsetToNext: u32, Flags: u32, Reserved: LARGE_INTEGER, Cluster: LARGE_INTEGER, FileName: [1]u16, }; pub const FILE_TYPE_NOTIFICATION_INPUT = extern struct { Flags: u32, NumFileTypeIDs: u32, FileTypeID: [1]Guid, }; pub const CSV_MGMT_LOCK = extern struct { Flags: u32, }; pub const CSV_NAMESPACE_INFO = extern struct { Version: u32, DeviceNumber: u32, StartingOffset: LARGE_INTEGER, SectorSize: u32, }; pub const CSV_CONTROL_OP = enum(i32) { StartRedirectFile = 2, StopRedirectFile = 3, QueryRedirectState = 4, QueryFileRevision = 6, QueryMdsPath = 8, QueryFileRevisionFileId128 = 9, QueryVolumeRedirectState = 10, EnableUSNRangeModificationTracking = 13, MarkHandleLocalVolumeMount = 14, UnmarkHandleLocalVolumeMount = 15, GetCsvFsMdsPathV2 = 18, DisableCaching = 19, EnableCaching = 20, StartForceDFO = 21, StopForceDFO = 22, QueryMdsPathNoPause = 23, SetVolumeId = 24, QueryVolumeId = 25, }; pub const CsvControlStartRedirectFile = CSV_CONTROL_OP.StartRedirectFile; pub const CsvControlStopRedirectFile = CSV_CONTROL_OP.StopRedirectFile; pub const CsvControlQueryRedirectState = CSV_CONTROL_OP.QueryRedirectState; pub const CsvControlQueryFileRevision = CSV_CONTROL_OP.QueryFileRevision; pub const CsvControlQueryMdsPath = CSV_CONTROL_OP.QueryMdsPath; pub const CsvControlQueryFileRevisionFileId128 = CSV_CONTROL_OP.QueryFileRevisionFileId128; pub const CsvControlQueryVolumeRedirectState = CSV_CONTROL_OP.QueryVolumeRedirectState; pub const CsvControlEnableUSNRangeModificationTracking = CSV_CONTROL_OP.EnableUSNRangeModificationTracking; pub const CsvControlMarkHandleLocalVolumeMount = CSV_CONTROL_OP.MarkHandleLocalVolumeMount; pub const CsvControlUnmarkHandleLocalVolumeMount = CSV_CONTROL_OP.UnmarkHandleLocalVolumeMount; pub const CsvControlGetCsvFsMdsPathV2 = CSV_CONTROL_OP.GetCsvFsMdsPathV2; pub const CsvControlDisableCaching = CSV_CONTROL_OP.DisableCaching; pub const CsvControlEnableCaching = CSV_CONTROL_OP.EnableCaching; pub const CsvControlStartForceDFO = CSV_CONTROL_OP.StartForceDFO; pub const CsvControlStopForceDFO = CSV_CONTROL_OP.StopForceDFO; pub const CsvControlQueryMdsPathNoPause = CSV_CONTROL_OP.QueryMdsPathNoPause; pub const CsvControlSetVolumeId = CSV_CONTROL_OP.SetVolumeId; pub const CsvControlQueryVolumeId = CSV_CONTROL_OP.QueryVolumeId; pub const CSV_CONTROL_PARAM = extern struct { Operation: CSV_CONTROL_OP, Unused: i64, }; pub const CSV_QUERY_REDIRECT_STATE = extern struct { MdsNodeId: u32, DsNodeId: u32, FileRedirected: BOOLEAN, }; pub const CSV_QUERY_FILE_REVISION = extern struct { FileId: i64, FileRevision: [3]i64, }; pub const CSV_QUERY_FILE_REVISION_FILE_ID_128 = extern struct { FileId: FILE_ID_128, FileRevision: [3]i64, }; pub const CSV_QUERY_MDS_PATH = extern struct { MdsNodeId: u32, DsNodeId: u32, PathLength: u32, Path: [1]u16, }; pub const CSVFS_DISK_CONNECTIVITY = enum(i32) { None = 0, MdsNodeOnly = 1, SubsetOfNodes = 2, AllNodes = 3, }; pub const CsvFsDiskConnectivityNone = CSVFS_DISK_CONNECTIVITY.None; pub const CsvFsDiskConnectivityMdsNodeOnly = CSVFS_DISK_CONNECTIVITY.MdsNodeOnly; pub const CsvFsDiskConnectivitySubsetOfNodes = CSVFS_DISK_CONNECTIVITY.SubsetOfNodes; pub const CsvFsDiskConnectivityAllNodes = CSVFS_DISK_CONNECTIVITY.AllNodes; pub const CSV_QUERY_VOLUME_REDIRECT_STATE = extern struct { MdsNodeId: u32, DsNodeId: u32, IsDiskConnected: BOOLEAN, ClusterEnableDirectIo: BOOLEAN, DiskConnectivity: CSVFS_DISK_CONNECTIVITY, }; pub const CSV_QUERY_MDS_PATH_V2 = extern struct { Version: i64, RequiredSize: u32, MdsNodeId: u32, DsNodeId: u32, Flags: u32, DiskConnectivity: CSVFS_DISK_CONNECTIVITY, VolumeId: Guid, IpAddressOffset: u32, IpAddressLength: u32, PathOffset: u32, PathLength: u32, }; pub const CSV_SET_VOLUME_ID = extern struct { VolumeId: Guid, }; pub const CSV_QUERY_VOLUME_ID = extern struct { VolumeId: Guid, }; pub const CSV_QUERY_VETO_FILE_DIRECT_IO_OUTPUT = extern struct { VetoedFromAltitudeIntegral: u64, VetoedFromAltitudeDecimal: u64, Reason: [256]u16, }; pub const STORAGE_RESERVE_ID = enum(i32) { None = 0, Hard = 1, Soft = 2, UpdateScratch = 3, Max = 4, }; pub const StorageReserveIdNone = STORAGE_RESERVE_ID.None; pub const StorageReserveIdHard = STORAGE_RESERVE_ID.Hard; pub const StorageReserveIdSoft = STORAGE_RESERVE_ID.Soft; pub const StorageReserveIdUpdateScratch = STORAGE_RESERVE_ID.UpdateScratch; pub const StorageReserveIdMax = STORAGE_RESERVE_ID.Max; pub const CSV_IS_OWNED_BY_CSVFS = extern struct { OwnedByCSVFS: BOOLEAN, }; pub const FILE_LEVEL_TRIM_RANGE = extern struct { Offset: u64, Length: u64, }; pub const FILE_LEVEL_TRIM = extern struct { Key: u32, NumRanges: u32, Ranges: [1]FILE_LEVEL_TRIM_RANGE, }; pub const FILE_LEVEL_TRIM_OUTPUT = extern struct { NumRangesProcessed: u32, }; pub const QUERY_FILE_LAYOUT_FILTER_TYPE = enum(i32) { FILTER_TYPE_NONE = 0, FILTER_TYPE_CLUSTERS = 1, FILTER_TYPE_FILEID = 2, FILTER_TYPE_STORAGE_RESERVE_ID = 3, NUM_FILTER_TYPES = 4, }; pub const QUERY_FILE_LAYOUT_FILTER_TYPE_NONE = QUERY_FILE_LAYOUT_FILTER_TYPE.FILTER_TYPE_NONE; pub const QUERY_FILE_LAYOUT_FILTER_TYPE_CLUSTERS = QUERY_FILE_LAYOUT_FILTER_TYPE.FILTER_TYPE_CLUSTERS; pub const QUERY_FILE_LAYOUT_FILTER_TYPE_FILEID = QUERY_FILE_LAYOUT_FILTER_TYPE.FILTER_TYPE_FILEID; pub const QUERY_FILE_LAYOUT_FILTER_TYPE_STORAGE_RESERVE_ID = QUERY_FILE_LAYOUT_FILTER_TYPE.FILTER_TYPE_STORAGE_RESERVE_ID; pub const QUERY_FILE_LAYOUT_NUM_FILTER_TYPES = QUERY_FILE_LAYOUT_FILTER_TYPE.NUM_FILTER_TYPES; pub const CLUSTER_RANGE = extern struct { StartingCluster: LARGE_INTEGER, ClusterCount: LARGE_INTEGER, }; pub const FILE_REFERENCE_RANGE = extern struct { StartingFileReferenceNumber: u64, EndingFileReferenceNumber: u64, }; pub const QUERY_FILE_LAYOUT_INPUT = extern struct { Anonymous: extern union { FilterEntryCount: u32, NumberOfPairs: u32, }, Flags: u32, FilterType: QUERY_FILE_LAYOUT_FILTER_TYPE, Reserved: u32, Filter: extern union { ClusterRanges: [1]CLUSTER_RANGE, FileReferenceRanges: [1]FILE_REFERENCE_RANGE, StorageReserveIds: [1]STORAGE_RESERVE_ID, }, }; pub const QUERY_FILE_LAYOUT_OUTPUT = extern struct { FileEntryCount: u32, FirstFileOffset: u32, Flags: u32, Reserved: u32, }; pub const FILE_LAYOUT_ENTRY = extern struct { Version: u32, NextFileOffset: u32, Flags: u32, FileAttributes: u32, FileReferenceNumber: u64, FirstNameOffset: u32, FirstStreamOffset: u32, ExtraInfoOffset: u32, ExtraInfoLength: u32, }; pub const FILE_LAYOUT_NAME_ENTRY = extern struct { NextNameOffset: u32, Flags: u32, ParentFileReferenceNumber: u64, FileNameLength: u32, Reserved: u32, FileName: [1]u16, }; pub const FILE_LAYOUT_INFO_ENTRY = extern struct { BasicInformation: extern struct { CreationTime: LARGE_INTEGER, LastAccessTime: LARGE_INTEGER, LastWriteTime: LARGE_INTEGER, ChangeTime: LARGE_INTEGER, FileAttributes: u32, }, OwnerId: u32, SecurityId: u32, Usn: i64, StorageReserveId: STORAGE_RESERVE_ID, }; pub const STREAM_LAYOUT_ENTRY = extern struct { Version: u32, NextStreamOffset: u32, Flags: u32, ExtentInformationOffset: u32, AllocationSize: LARGE_INTEGER, EndOfFile: LARGE_INTEGER, StreamInformationOffset: u32, AttributeTypeCode: u32, AttributeFlags: u32, StreamIdentifierLength: u32, StreamIdentifier: [1]u16, }; pub const STREAM_EXTENT_ENTRY = extern struct { Flags: u32, ExtentInformation: extern union { RetrievalPointers: RETRIEVAL_POINTERS_BUFFER, }, }; pub const FSCTL_GET_INTEGRITY_INFORMATION_BUFFER = extern struct { ChecksumAlgorithm: u16, Reserved: u16, Flags: u32, ChecksumChunkSizeInBytes: u32, ClusterSizeInBytes: u32, }; pub const FSCTL_SET_INTEGRITY_INFORMATION_BUFFER = extern struct { ChecksumAlgorithm: u16, Reserved: u16, Flags: u32, }; pub const FSCTL_SET_INTEGRITY_INFORMATION_BUFFER_EX = extern struct { EnableIntegrity: u8, KeepIntegrityStateUnchanged: u8, Reserved: u16, Flags: u32, Version: u8, Reserved2: [7]u8, }; pub const FSCTL_OFFLOAD_READ_INPUT = extern struct { Size: u32, Flags: u32, TokenTimeToLive: u32, Reserved: u32, FileOffset: u64, CopyLength: u64, }; pub const FSCTL_OFFLOAD_READ_OUTPUT = extern struct { Size: u32, Flags: u32, TransferLength: u64, Token: [512]u8, }; pub const FSCTL_OFFLOAD_WRITE_INPUT = extern struct { Size: u32, Flags: u32, FileOffset: u64, CopyLength: u64, TransferOffset: u64, Token: [512]u8, }; pub const FSCTL_OFFLOAD_WRITE_OUTPUT = extern struct { Size: u32, Flags: u32, LengthWritten: u64, }; pub const SET_PURGE_FAILURE_MODE_INPUT = extern struct { Flags: u32, }; pub const REPAIR_COPIES_INPUT = extern struct { Size: u32, Flags: u32, FileOffset: LARGE_INTEGER, Length: u32, SourceCopy: u32, NumberOfRepairCopies: u32, RepairCopies: [1]u32, }; pub const REPAIR_COPIES_OUTPUT = extern struct { Size: u32, Status: u32, ResumeFileOffset: LARGE_INTEGER, }; pub const FILE_REGION_INFO = extern struct { FileOffset: i64, Length: i64, Usage: u32, Reserved: u32, }; pub const FILE_REGION_OUTPUT = extern struct { Flags: u32, TotalRegionEntryCount: u32, RegionEntryCount: u32, Reserved: u32, Region: [1]FILE_REGION_INFO, }; pub const FILE_REGION_INPUT = extern struct { FileOffset: i64, Length: i64, DesiredUsage: u32, }; pub const WRITE_USN_REASON_INPUT = extern struct { Flags: u32, UsnReasonToWrite: u32, }; pub const FILE_STORAGE_TIER_MEDIA_TYPE = enum(i32) { Unspecified = 0, Disk = 1, Ssd = 2, Scm = 4, Max = 5, }; pub const FileStorageTierMediaTypeUnspecified = FILE_STORAGE_TIER_MEDIA_TYPE.Unspecified; pub const FileStorageTierMediaTypeDisk = FILE_STORAGE_TIER_MEDIA_TYPE.Disk; pub const FileStorageTierMediaTypeSsd = FILE_STORAGE_TIER_MEDIA_TYPE.Ssd; pub const FileStorageTierMediaTypeScm = FILE_STORAGE_TIER_MEDIA_TYPE.Scm; pub const FileStorageTierMediaTypeMax = FILE_STORAGE_TIER_MEDIA_TYPE.Max; pub const FILE_STORAGE_TIER_CLASS = enum(i32) { Unspecified = 0, Capacity = 1, Performance = 2, Max = 3, }; pub const FileStorageTierClassUnspecified = FILE_STORAGE_TIER_CLASS.Unspecified; pub const FileStorageTierClassCapacity = FILE_STORAGE_TIER_CLASS.Capacity; pub const FileStorageTierClassPerformance = FILE_STORAGE_TIER_CLASS.Performance; pub const FileStorageTierClassMax = FILE_STORAGE_TIER_CLASS.Max; pub const FILE_STORAGE_TIER = extern struct { Id: Guid, Name: [256]u16, Description: [256]u16, Flags: FILE_STORAGE_TIER_FLAGS, ProvisionedCapacity: u64, MediaType: FILE_STORAGE_TIER_MEDIA_TYPE, Class: FILE_STORAGE_TIER_CLASS, }; pub const FSCTL_QUERY_STORAGE_CLASSES_OUTPUT = extern struct { Version: u32, Size: u32, Flags: FILE_STORAGE_TIER_FLAGS, TotalNumberOfTiers: u32, NumberOfTiersReturned: u32, Tiers: [1]FILE_STORAGE_TIER, }; pub const STREAM_INFORMATION_ENTRY = extern struct { pub const _StreamInformation = extern union { pub const _Reparse = extern struct { Length: u16, Flags: u16, ReparseDataSize: u32, ReparseDataOffset: u32, }; pub const _DesiredStorageClass = extern struct { Class: FILE_STORAGE_TIER_CLASS, Flags: u32, }; pub const _DataStream = extern struct { Length: u16, Flags: u16, Reserved: u32, Vdl: u64, }; pub const _Ea = extern struct { Length: u16, Flags: u16, EaSize: u32, EaInformationOffset: u32, }; DesiredStorageClass: _DesiredStorageClass, DataStream: _DataStream, Reparse: _Reparse, Ea: _Ea, }; Version: u32, Flags: u32, StreamInformation: _StreamInformation, }; pub const FSCTL_QUERY_REGION_INFO_INPUT = extern struct { Version: u32, Size: u32, Flags: u32, NumberOfTierIds: u32, TierIds: [1]Guid, }; pub const FILE_STORAGE_TIER_REGION = extern struct { TierId: Guid, Offset: u64, Length: u64, }; pub const FSCTL_QUERY_REGION_INFO_OUTPUT = extern struct { Version: u32, Size: u32, Flags: u32, Reserved: u32, Alignment: u64, TotalNumberOfRegions: u32, NumberOfRegionsReturned: u32, Regions: [1]FILE_STORAGE_TIER_REGION, }; pub const FILE_DESIRED_STORAGE_CLASS_INFORMATION = extern struct { Class: FILE_STORAGE_TIER_CLASS, Flags: u32, }; pub const DUPLICATE_EXTENTS_DATA = extern struct { FileHandle: ?HANDLE, SourceFileOffset: LARGE_INTEGER, TargetFileOffset: LARGE_INTEGER, ByteCount: LARGE_INTEGER, }; pub const DUPLICATE_EXTENTS_DATA_EX = extern struct { Size: usize, FileHandle: ?HANDLE, SourceFileOffset: LARGE_INTEGER, TargetFileOffset: LARGE_INTEGER, ByteCount: LARGE_INTEGER, Flags: u32, }; pub const DUPLICATE_EXTENTS_STATE = enum(i32) { Inactive = 0, Source = 1, Target = 2, }; pub const FileSnapStateInactive = DUPLICATE_EXTENTS_STATE.Inactive; pub const FileSnapStateSource = DUPLICATE_EXTENTS_STATE.Source; pub const FileSnapStateTarget = DUPLICATE_EXTENTS_STATE.Target; pub const ASYNC_DUPLICATE_EXTENTS_STATUS = extern struct { Version: u32, State: DUPLICATE_EXTENTS_STATE, SourceFileOffset: u64, TargetFileOffset: u64, ByteCount: u64, BytesDuplicated: u64, }; pub const REFS_SMR_VOLUME_GC_STATE = enum(i32) { Inactive = 0, Paused = 1, Active = 2, ActiveFullSpeed = 3, }; pub const SmrGcStateInactive = REFS_SMR_VOLUME_GC_STATE.Inactive; pub const SmrGcStatePaused = REFS_SMR_VOLUME_GC_STATE.Paused; pub const SmrGcStateActive = REFS_SMR_VOLUME_GC_STATE.Active; pub const SmrGcStateActiveFullSpeed = REFS_SMR_VOLUME_GC_STATE.ActiveFullSpeed; pub const REFS_SMR_VOLUME_INFO_OUTPUT = extern struct { Version: u32, Flags: u32, SizeOfRandomlyWritableTier: LARGE_INTEGER, FreeSpaceInRandomlyWritableTier: LARGE_INTEGER, SizeofSMRTier: LARGE_INTEGER, FreeSpaceInSMRTier: LARGE_INTEGER, UsableFreeSpaceInSMRTier: LARGE_INTEGER, VolumeGcState: REFS_SMR_VOLUME_GC_STATE, VolumeGcLastStatus: u32, CurrentGcBandFillPercentage: u32, Unused: [6]u64, }; pub const REFS_SMR_VOLUME_GC_ACTION = enum(i32) { Start = 1, StartFullSpeed = 2, Pause = 3, Stop = 4, }; pub const SmrGcActionStart = REFS_SMR_VOLUME_GC_ACTION.Start; pub const SmrGcActionStartFullSpeed = REFS_SMR_VOLUME_GC_ACTION.StartFullSpeed; pub const SmrGcActionPause = REFS_SMR_VOLUME_GC_ACTION.Pause; pub const SmrGcActionStop = REFS_SMR_VOLUME_GC_ACTION.Stop; pub const REFS_SMR_VOLUME_GC_METHOD = enum(i32) { Compaction = 1, Compression = 2, Rotation = 3, }; pub const SmrGcMethodCompaction = REFS_SMR_VOLUME_GC_METHOD.Compaction; pub const SmrGcMethodCompression = REFS_SMR_VOLUME_GC_METHOD.Compression; pub const SmrGcMethodRotation = REFS_SMR_VOLUME_GC_METHOD.Rotation; pub const REFS_SMR_VOLUME_GC_PARAMETERS = extern struct { Version: u32, Flags: u32, Action: REFS_SMR_VOLUME_GC_ACTION, Method: REFS_SMR_VOLUME_GC_METHOD, IoGranularity: u32, CompressionFormat: u32, Unused: [8]u64, }; pub const STREAMS_QUERY_PARAMETERS_OUTPUT_BUFFER = extern struct { OptimalWriteSize: u32, StreamGranularitySize: u32, StreamIdMin: u32, StreamIdMax: u32, }; pub const STREAMS_ASSOCIATE_ID_INPUT_BUFFER = extern struct { Flags: u32, StreamId: u32, }; pub const STREAMS_QUERY_ID_OUTPUT_BUFFER = extern struct { StreamId: u32, }; pub const QUERY_BAD_RANGES_INPUT_RANGE = extern struct { StartOffset: u64, LengthInBytes: u64, }; pub const QUERY_BAD_RANGES_INPUT = extern struct { Flags: u32, NumRanges: u32, Ranges: [1]QUERY_BAD_RANGES_INPUT_RANGE, }; pub const QUERY_BAD_RANGES_OUTPUT_RANGE = extern struct { Flags: u32, Reserved: u32, StartOffset: u64, LengthInBytes: u64, }; pub const QUERY_BAD_RANGES_OUTPUT = extern struct { Flags: u32, NumBadRanges: u32, NextOffsetToLookUp: u64, BadRanges: [1]QUERY_BAD_RANGES_OUTPUT_RANGE, }; pub const SET_DAX_ALLOC_ALIGNMENT_HINT_INPUT = extern struct { Flags: u32, AlignmentShift: u32, FileOffsetToAlign: u64, FallbackAlignmentShift: u32, }; pub const VIRTUAL_STORAGE_BEHAVIOR_CODE = enum(i32) { Undefined = 0, CacheWriteThrough = 1, CacheWriteBack = 2, StopIoProcessing = 3, RestartIoProcessing = 4, }; pub const VirtualStorageBehaviorUndefined = VIRTUAL_STORAGE_BEHAVIOR_CODE.Undefined; pub const VirtualStorageBehaviorCacheWriteThrough = VIRTUAL_STORAGE_BEHAVIOR_CODE.CacheWriteThrough; pub const VirtualStorageBehaviorCacheWriteBack = VIRTUAL_STORAGE_BEHAVIOR_CODE.CacheWriteBack; pub const VirtualStorageBehaviorStopIoProcessing = VIRTUAL_STORAGE_BEHAVIOR_CODE.StopIoProcessing; pub const VirtualStorageBehaviorRestartIoProcessing = VIRTUAL_STORAGE_BEHAVIOR_CODE.RestartIoProcessing; pub const VIRTUAL_STORAGE_SET_BEHAVIOR_INPUT = extern struct { Size: u32, BehaviorCode: VIRTUAL_STORAGE_BEHAVIOR_CODE, }; pub const ENCRYPTION_KEY_CTRL_INPUT = extern struct { HeaderSize: u32, StructureSize: u32, KeyOffset: u16, KeySize: u16, DplLock: u32, DplUserId: u64, DplCredentialId: u64, }; pub const WOF_EXTERNAL_INFO = extern struct { Version: u32, Provider: u32, }; pub const WOF_EXTERNAL_FILE_ID = extern struct { FileId: FILE_ID_128, }; pub const WOF_VERSION_INFO = extern struct { WofVersion: u32, }; pub const WIM_PROVIDER_EXTERNAL_INFO = extern struct { Version: u32, Flags: u32, DataSourceId: LARGE_INTEGER, ResourceHash: [20]u8, }; pub const WIM_PROVIDER_ADD_OVERLAY_INPUT = extern struct { WimType: u32, WimIndex: u32, WimFileNameOffset: u32, WimFileNameLength: u32, }; pub const WIM_PROVIDER_UPDATE_OVERLAY_INPUT = extern struct { DataSourceId: LARGE_INTEGER, WimFileNameOffset: u32, WimFileNameLength: u32, }; pub const WIM_PROVIDER_REMOVE_OVERLAY_INPUT = extern struct { DataSourceId: LARGE_INTEGER, }; pub const WIM_PROVIDER_SUSPEND_OVERLAY_INPUT = extern struct { DataSourceId: LARGE_INTEGER, }; pub const WIM_PROVIDER_OVERLAY_ENTRY = extern struct { NextEntryOffset: u32, DataSourceId: LARGE_INTEGER, WimGuid: Guid, WimFileNameOffset: u32, WimType: u32, WimIndex: u32, Flags: u32, }; pub const FILE_PROVIDER_EXTERNAL_INFO_V0 = extern struct { Version: u32, Algorithm: u32, }; pub const FILE_PROVIDER_EXTERNAL_INFO_V1 = extern struct { Version: u32, Algorithm: u32, Flags: u32, }; pub const CONTAINER_VOLUME_STATE = extern struct { Flags: u32, }; pub const CONTAINER_ROOT_INFO_INPUT = extern struct { Flags: u32, }; pub const CONTAINER_ROOT_INFO_OUTPUT = extern struct { ContainerRootIdLength: u16, ContainerRootId: [1]u8, }; pub const VIRTUALIZATION_INSTANCE_INFO_INPUT = extern struct { NumberOfWorkerThreads: u32, Flags: u32, }; pub const VIRTUALIZATION_INSTANCE_INFO_INPUT_EX = extern struct { HeaderSize: u16, Flags: u32, NotificationInfoSize: u32, NotificationInfoOffset: u16, ProviderMajorVersion: u16, }; pub const VIRTUALIZATION_INSTANCE_INFO_OUTPUT = extern struct { VirtualizationInstanceID: Guid, }; pub const GET_FILTER_FILE_IDENTIFIER_INPUT = extern struct { AltitudeLength: u16, Altitude: [1]u16, }; pub const GET_FILTER_FILE_IDENTIFIER_OUTPUT = extern struct { FilterFileIdentifierLength: u16, FilterFileIdentifier: [1]u8, }; pub const FS_BPIO_OPERATIONS = enum(i32) { ENABLE = 1, DISABLE = 2, QUERY = 3, VOLUME_STACK_PAUSE = 4, VOLUME_STACK_RESUME = 5, STREAM_PAUSE = 6, STREAM_RESUME = 7, GET_INFO = 8, MAX_OPERATION = 9, }; pub const FS_BPIO_OP_ENABLE = FS_BPIO_OPERATIONS.ENABLE; pub const FS_BPIO_OP_DISABLE = FS_BPIO_OPERATIONS.DISABLE; pub const FS_BPIO_OP_QUERY = FS_BPIO_OPERATIONS.QUERY; pub const FS_BPIO_OP_VOLUME_STACK_PAUSE = FS_BPIO_OPERATIONS.VOLUME_STACK_PAUSE; pub const FS_BPIO_OP_VOLUME_STACK_RESUME = FS_BPIO_OPERATIONS.VOLUME_STACK_RESUME; pub const FS_BPIO_OP_STREAM_PAUSE = FS_BPIO_OPERATIONS.STREAM_PAUSE; pub const FS_BPIO_OP_STREAM_RESUME = FS_BPIO_OPERATIONS.STREAM_RESUME; pub const FS_BPIO_OP_GET_INFO = FS_BPIO_OPERATIONS.GET_INFO; pub const FS_BPIO_OP_MAX_OPERATION = FS_BPIO_OPERATIONS.MAX_OPERATION; pub const FS_BPIO_INFLAGS = enum(i32) { None = 0, SKIP_STORAGE_STACK_QUERY = 1, }; pub const FSBPIO_INFL_None = FS_BPIO_INFLAGS.None; pub const FSBPIO_INFL_SKIP_STORAGE_STACK_QUERY = FS_BPIO_INFLAGS.SKIP_STORAGE_STACK_QUERY; pub const FS_BPIO_INPUT = extern struct { Operation: FS_BPIO_OPERATIONS, InFlags: FS_BPIO_INFLAGS, Reserved1: u64, Reserved2: u64, }; pub const FS_BPIO_OUTFLAGS = enum(i32) { None = 0, VOLUME_STACK_BYPASS_PAUSED = 1, STREAM_BYPASS_PAUSED = 2, FILTER_ATTACH_BLOCKED = 4, COMPATIBLE_STORAGE_DRIVER = 8, }; pub const FSBPIO_OUTFL_None = FS_BPIO_OUTFLAGS.None; pub const FSBPIO_OUTFL_VOLUME_STACK_BYPASS_PAUSED = FS_BPIO_OUTFLAGS.VOLUME_STACK_BYPASS_PAUSED; pub const FSBPIO_OUTFL_STREAM_BYPASS_PAUSED = FS_BPIO_OUTFLAGS.STREAM_BYPASS_PAUSED; pub const FSBPIO_OUTFL_FILTER_ATTACH_BLOCKED = FS_BPIO_OUTFLAGS.FILTER_ATTACH_BLOCKED; pub const FSBPIO_OUTFL_COMPATIBLE_STORAGE_DRIVER = FS_BPIO_OUTFLAGS.COMPATIBLE_STORAGE_DRIVER; pub const FS_BPIO_RESULTS = extern struct { OpStatus: u32, FailingDriverNameLen: u16, FailingDriverName: [32]u16, FailureReasonLen: u16, FailureReason: [128]u16, }; pub const FS_BPIO_INFO = extern struct { ActiveBypassIoCount: u32, StorageDriverNameLen: u16, StorageDriverName: [32]u16, }; pub const FS_BPIO_OUTPUT = extern struct { Operation: FS_BPIO_OPERATIONS, OutFlags: FS_BPIO_OUTFLAGS, Reserved1: u64, Reserved2: u64, Anonymous: extern union { Enable: FS_BPIO_RESULTS, Query: FS_BPIO_RESULTS, VolumeStackResume: FS_BPIO_RESULTS, StreamResume: FS_BPIO_RESULTS, GetInfo: FS_BPIO_INFO, }, }; pub const SMB_SHARE_FLUSH_AND_PURGE_INPUT = extern struct { Version: u16, }; pub const SMB_SHARE_FLUSH_AND_PURGE_OUTPUT = extern struct { cEntriesPurged: u32, }; pub const DISK_EXTENT = extern struct { DiskNumber: u32, StartingOffset: LARGE_INTEGER, ExtentLength: LARGE_INTEGER, }; pub const VOLUME_DISK_EXTENTS = extern struct { NumberOfDiskExtents: u32, Extents: [1]DISK_EXTENT, }; pub const VOLUME_GET_GPT_ATTRIBUTES_INFORMATION = extern struct { GptAttributes: u64, }; pub const PIO_IRP_EXT_PROCESS_TRACKED_OFFSET_CALLBACK = fn( SourceContext: ?*IO_IRP_EXT_TRACK_OFFSET_HEADER, TargetContext: ?*IO_IRP_EXT_TRACK_OFFSET_HEADER, RelativeOffset: i64, ) callconv(@import("std").os.windows.WINAPI) void; pub const IO_IRP_EXT_TRACK_OFFSET_HEADER = extern struct { Validation: u16, Flags: u16, TrackedOffsetCallback: ?PIO_IRP_EXT_PROCESS_TRACKED_OFFSET_CALLBACK, }; pub const MOVE_FILE_DATA32 = switch(@import("../zig.zig").arch) { .X64, .Arm64 => extern struct { FileHandle: u32, StartingVcn: LARGE_INTEGER, StartingLcn: LARGE_INTEGER, ClusterCount: u32, }, else => usize, // NOTE: this should be a @compileError but can't because of https://github.com/ziglang/zig/issues/9682 }; pub const MARK_HANDLE_INFO32 = switch(@import("../zig.zig").arch) { .X64, .Arm64 => extern struct { Anonymous: extern union { UsnSourceInfo: u32, CopyNumber: u32, }, VolumeHandle: u32, HandleInfo: u32, }, else => usize, // NOTE: this should be a @compileError but can't because of https://github.com/ziglang/zig/issues/9682 }; pub const DUPLICATE_EXTENTS_DATA32 = switch(@import("../zig.zig").arch) { .X64, .Arm64 => extern struct { FileHandle: u32, SourceFileOffset: LARGE_INTEGER, TargetFileOffset: LARGE_INTEGER, ByteCount: LARGE_INTEGER, }, else => usize, // NOTE: this should be a @compileError but can't because of https://github.com/ziglang/zig/issues/9682 }; pub const DUPLICATE_EXTENTS_DATA_EX32 = switch(@import("../zig.zig").arch) { .X64, .Arm64 => extern struct { Size: u32, FileHandle: u32, SourceFileOffset: LARGE_INTEGER, TargetFileOffset: LARGE_INTEGER, ByteCount: LARGE_INTEGER, Flags: u32, }, else => usize, // NOTE: this should be a @compileError but can't because of https://github.com/ziglang/zig/issues/9682 }; //-------------------------------------------------------------------------------- // Section: Functions (0) //-------------------------------------------------------------------------------- //-------------------------------------------------------------------------------- // Section: Unicode Aliases (0) //-------------------------------------------------------------------------------- const thismodule = @This(); pub usingnamespace switch (@import("../zig.zig").unicode_mode) { .ansi => struct { }, .wide => struct { }, .unspecified => if (@import("builtin").is_test) struct { } else struct { }, }; //-------------------------------------------------------------------------------- // Section: Imports (10) //-------------------------------------------------------------------------------- const Guid = @import("../zig.zig").Guid; const BOOLEAN = @import("../foundation.zig").BOOLEAN; const CHAR = @import("../foundation.zig").CHAR; const FILE_ID_128 = @import("../storage/file_system.zig").FILE_ID_128; const HANDLE = @import("../foundation.zig").HANDLE; const LARGE_INTEGER = @import("../foundation.zig").LARGE_INTEGER; const PROPERTYKEY = @import("../ui/shell/properties_system.zig").PROPERTYKEY; const SID = @import("../security.zig").SID; const STORAGE_BUS_TYPE = @import("../storage/file_system.zig").STORAGE_BUS_TYPE; const VIRTUAL_STORAGE_TYPE = @import("../storage/vhd.zig").VIRTUAL_STORAGE_TYPE; test { // The following '_ = <FuncPtrType>' lines are a workaround for https://github.com/ziglang/zig/issues/4476 if (@hasDecl(@This(), "PIO_IRP_EXT_PROCESS_TRACKED_OFFSET_CALLBACK")) { _ = PIO_IRP_EXT_PROCESS_TRACKED_OFFSET_CALLBACK; } @setEvalBranchQuota( @import("std").meta.declarations(@This()).len * 3 ); // reference all the pub declarations if (!@import("builtin").is_test) return; inline for (@import("std").meta.declarations(@This())) |decl| { if (decl.is_pub) { _ = decl; } } }
const Self = @This(); const std = @import("std"); const testing = std.testing; // Number is the runway number such as "25" number: u16, // Position is the relative position of the runway, if any, such as // L, R, C. This is a byte so that it can be any ASCII character, but position: ?Position = null, /// Position is the potential position of runways with matching numbers. pub const Position = enum { L, R, C, }; /// Runway string such as "15L", "15", etc. The buffer must be at least /// 3 characters large. If the buffer isn't large enough you'll get an error. pub fn toString(self: Self, buf: []u8) ![:0]u8 { var posString: [:0]const u8 = ""; if (self.position) |pos| { posString = @tagName(pos); } return try std.fmt.bufPrintZ(buf, "{d:0>2}{s}", .{ self.number, posString }); } test "string" { var buf: [6]u8 = undefined; { const rwy = Self{ .number = 25 }; try testing.expectEqualStrings(try rwy.toString(&buf), "25"); } { const rwy = Self{ .number = 25, .position = .L }; try testing.expectEqualStrings(try rwy.toString(&buf), "25L"); } { const rwy = Self{ .number = 1, .position = .C }; try testing.expectEqualStrings(try rwy.toString(&buf), "01C"); } // Stupid but should work { const rwy = Self{ .number = 679 }; try testing.expectEqualStrings(try rwy.toString(&buf), "679"); } }
const Builder = @import("std").build.Builder; const psp = @import("src/Zig-PSP/build-psp.zig"); pub fn build(b: *Builder) void { psp.build_psp(b, psp.PSPBuildInfo{ .path_to_sdk = "src/Zig-PSP/", .src_file = "src/main.zig", .title = "Zig PSP Test", }) catch unreachable; }
const std = @import("std"); const os = std.os; const vt = @import("vt-term.zig"); const assertOrPanic = std.debug.assertOrPanic; const EZError = error{ NotImplemented, NoUserInput, }; const EditMode = enum { Normal, Insert, Visual, }; const EditorState = struct { index: usize, // index within row slice cpos: vt.CursorPos, // location of terminal cursor i_cpos: vt.CursorPos, // initial location of terminal cursor (after prompt) max_cpos: vt.CursorPos, // Farthest cursor position mode: EditMode, // current editor mode seq_timer: usize, // timeout for multi-key sequences termd: vt.TerminalDimensions, // last queried terminal dimensions done: bool, // editor is ready to return to user in_buf: []u8, // buffer used to store input in_len: usize, // current user input size const Self = @This(); fn init(prompt: []const u8, buf: []u8) !Self { var state = EditorState{ .cpos = undefined, .i_cpos = undefined, .max_cpos = undefined, .termd = undefined, .index = 0, .mode = EditMode.Normal, .seq_timer = 0, .done = false, .in_buf = buf, .in_len = 0, }; try std_out.write(prompt); try state.updateCursorPos(); state.i_cpos = state.cpos; state.max_cpos = state.cpos; state.updateTerminalSize(); return state; } fn setCursorPos(state: *Self, pos: vt.CursorPos) !void { try vt.setCursorPos(pos); } fn getCursorPos(state: *Self) !vt.CursorPos { return try vt.getCursorPos(); } fn updateCursorPos(state: *Self) !void { state.cpos = try vt.getCursorPos(); } fn updateTerminalSize(state: *Self) void { state.termd = vt.getTerminalSize(); } fn setEditMode(state: *Self, mode: EditMode) void { state.mode = mode; } fn getEditorDone(state: *Self) bool { return state.done; } fn getCurrentUserInput(state: *Self) []u8 { return state.in_buf[0..state.in_len]; } fn moveCursorUp(state: *Self) void {} fn moveCursorDown(state: *Self) void {} fn moveCursorRight(state: *Self) !void { if (state.index < state.in_len) { vt.cursorForward(1) catch return; state.index += 1; try state.updateCursorPos(); } } fn moveCursorLeft(state: *Self) !void { if (state.index > 0) { vt.cursorBackward(1) catch return; state.index -= 1; try state.updateCursorPos(); } } fn copyRight(state: *Self, num: usize) void { //TODO: check that cursor won't go past screen if (state.in_len < state.in_buf.len - num) { std.mem.copy(u8, state.in_buf[state.index + num .. state.in_len + num], state.in_buf[state.index..state.in_len]); } } fn refreshScreen(state: *Self) !void { try state.setCursorPos(state.i_cpos); try vt.eraseCursorToEndOfDisplay(); try std_out.write(state.in_buf[0..state.in_len]); state.max_cpos = try state.getCursorPos(); try state.setCursorPos(state.cpos); } fn insertCharacter(state: *Self, key: u8) void { state.copyRight(1); state.in_buf[state.index] = key; state.in_len += 1; state.index += 1; if (state.cpos.col < state.termd.width - 1) { state.cpos.col += 1; } else { state.cpos.col = 0; if (state.cpos.row < state.termd.height - 1) { state.cpos.row += 1; // else at bottom of screen already } } } fn registerKey(state: *Self, key: u8) !void { const kmem = [1]u8{key}; const kslice = kmem[0..]; state.updateTerminalSize(); switch (state.mode) { EditMode.Insert => switch (key) { CTRL('c') => { state.done = true; }, CTRL('d') => { state.setEditMode(EditMode.Normal); }, else => { state.insertCharacter(key); }, }, EditMode.Normal => switch (key) { 'l' => { try state.moveCursorRight(); }, 'k' => { state.moveCursorUp(); }, 'j' => { state.moveCursorDown(); }, 'h' => { try state.moveCursorLeft(); }, 'i' => { state.setEditMode(EditMode.Insert); }, CTRL('c') => { state.done = true; }, else => {}, }, EditMode.Visual => switch (key) { CTRL('c') => { state.done = true; }, else => { unreachable; }, }, } try state.refreshScreen(); return; } }; const std_in = std.io.getStdIn(); const std_out = std.io.getStdOut(); const std_err = std.io.getStdErr(); const default_max_line_len = 4096; var runtime_allocator: ?*std.mem.Allocator = null; //***************************************************************************** // Description: Displays prompt, returns user input // Parameters: []u8 - Prompt to display, slice // Return: ![]u8 - User input or error if not successful //***************************************************************************** pub fn eazyInputSlice(prompt: []const u8) ![]u8 { if (!os.isTty(0)) { _ = handleNotTty(); } else if (vt.isUnsupportedTerm()) { _ = handleUnsupportedTerm(); } else { const ret_slice_null_terminated = try getEazyInput(prompt); // TODO: how much memory does the length portion of the slice take up? return ret_slice_null_terminated[0 .. ret_slice_null_terminated.len - 1]; } return error.eazyInputNoUserInput; } //***************************************************************************** // Description: Frees memory previously returned by eazyInputSliceAlloc // Parameters: []u8 - slice of memory to free // Return: !void - error if unsuccessful or allocator not initialized //***************************************************************************** pub fn eazyInputSliceFree(user_input: []const u8) !void { if (runtime_allocator) |allocator| { allocator.free(user_input); return; } else { return error.ez_allocator_uninitialized; } } //***************************************************************************** // Description: Allocates memory for the user input // Parameters: []u8 - Prompt to display, slice // Return: ![]u8 - User input or error if not successful //***************************************************************************** fn eazyInputSliceAlloc(comptime T: type, n: usize) ![]T { if (runtime_allocator == null) { runtime_allocator = std.heap.page_allocator; } if (runtime_allocator) |allocator| { var buf = try allocator.alloc(T, n); std.mem.set(u8, buf, 0); return buf; } else { unreachable; } } fn handleNotTty() ![]u8 { return EZError.NotImplemented; } fn handleUnsupportedTerm() ![]u8 { return EZError.NotImplemented; } fn getEazyInput(prompt: []const u8) ![]u8 { var fbuf: [default_max_line_len]u8 = undefined; var orig_term = try vt.enableRawTerminalMode(); defer vt.setTerminalMode(&orig_term) catch {}; // best effort var state = try EditorState.init(prompt, fbuf[0..]); while (!state.getEditorDone()) { if (getKeypress()) |key| { try state.registerKey(key); } else |err| return err; } var ret_input = state.getCurrentUserInput(); if (ret_input.len > 0) { var buf = try eazyInputSliceAlloc(u8, ret_input.len); errdefer eazyInputSliceFree(buf) catch {}; std.mem.copy(u8, ret_input, buf); return buf; } return EZError.NoUserInput; } fn getKeypress() !u8 { var c = [_]u8{0}; var count = try std_in.read(c[0..1]); if (count == 1) return c[0] else return error.noKeypress; } inline fn CTRL(c: u8) u8 { return c & @as(u8, 0x1F); } fn strnslice(c_str: ?[*]const u8, n: usize) []const u8 { // TODO: how to return const slice only if input was const? // check for null pointer input, convert to zero length slice var slice: []const u8 = undefined; if (c_str) |p| { for (p[0..n]) |c, i| { if (c == 0) { slice = p[0..i]; break; } } else { slice = p[0..n]; } } else { slice = ([_]u8{0})[0..0]; } return slice; } test "eazyinput.zig: strnslice" { const cstr_null: ?[*]const u8 = null; const cstr_0: ?[*]const u8 = ""; const cstr_1: ?[*]const u8 = "123456"; // string is null pointer std.debug.assert(std.mem.eql(u8, strnslice(cstr_null, 10), ""[0..0])); // null terminator is first byte std.debug.assert(std.mem.eql(u8, strnslice(cstr_0, 10), ""[0..0])); // null terminator is at "n" index std.debug.assert(std.mem.eql(u8, strnslice(cstr_1, 6), "123456"[0..6])); // null terminator is beyond "n" index std.debug.assert(std.mem.eql(u8, strnslice(cstr_1, 5), "123456"[0..5])); // null terminator is before "n" index std.debug.assert(std.mem.eql(u8, strnslice(cstr_1, 7), "123456"[0..6])); } test "eazyinput.zig: allocations and frees" { var buf = try eazyInputSliceAlloc(u8, default_max_line_len); try eazyInputSliceFree(buf); } test "eazyinput.zig: top level call" { var ret = try getEazyInput("prompt"); defer eazyInputSliceFree(ret) catch {}; }
const std = @import("std"); const zline = @import("zline"); pub fn main() anyerror!void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.debug.assert(!gpa.deinit()); var line: zline = try zline.init(&gpa.allocator, std.io.getStdOut()); defer line.destroy(); { while (true) { const x = line.prompt("> ") catch |err| { std.debug.print("Prompt failed with error {}\n", .{err}); return; }; std.debug.print("Received: {s}\n", .{x}); if (std.mem.eql(u8, "exit", x)) { break; } } } }
const assert = @import("std").debug.assert; test "try on error union" { tryOnErrorUnionImpl(); comptime tryOnErrorUnionImpl(); } fn tryOnErrorUnionImpl() void { const x = if (returnsTen()) |val| val + 1 else |err| switch (err) { error.ItBroke, error.NoMem => 1, error.CrappedOut => i32(2), else => unreachable, }; assert(x == 11); } fn returnsTen() error!i32 { return 10; } test "try without vars" { const result1 = if (failIfTrue(true)) 1 else |_| i32(2); assert(result1 == 2); const result2 = if (failIfTrue(false)) 1 else |_| i32(2); assert(result2 == 1); } fn failIfTrue(ok: bool) error!void { if (ok) { return error.ItBroke; } else { return; } } test "try then not executed with assignment" { if (failIfTrue(true)) { unreachable; } else |err| { assert(err == error.ItBroke); } }
const std = @import("std"); const builtin = @import("builtin"); const ArrayList = std.ArrayList; const Allocator = std.mem.Allocator; const AutoHashMap = std.AutoHashMap; const min = std.math.min; const warn = std.debug.warn; /// Top Level pub const Device = struct { name: ArrayList(u8), version: ArrayList(u8), description: ArrayList(u8), cpu: ?Cpu, /// Bus Interface Properties /// Smallest addressable unit in bits address_unit_bits: ?u32, /// The Maximum data bit width accessible within a single transfer max_bit_width: ?u32, /// Start register default properties reg_default_size: ?u32, reg_default_reset_value: ?u32, reg_default_reset_mask: ?u32, peripherals: Peripherals, interrupts: Interrupts, const Self = @This(); pub fn init(allocator: *Allocator) !Self { var name = ArrayList(u8).init(allocator); errdefer name.deinit(); var version = ArrayList(u8).init(allocator); errdefer version.deinit(); var description = ArrayList(u8).init(allocator); errdefer description.deinit(); var peripherals = Peripherals.init(allocator); errdefer peripherals.deinit(); var interrupts = Interrupts.init(allocator); errdefer interrupts.deinit(); return Self{ .name = name, .version = version, .description = description, .cpu = null, .address_unit_bits = null, .max_bit_width = null, .reg_default_size = null, .reg_default_reset_value = null, .reg_default_reset_mask = null, .peripherals = peripherals, .interrupts = interrupts, }; } pub fn deinit(self: *Self) void { self.name.deinit(); self.version.deinit(); self.description.deinit(); self.peripherals.deinit(); self.interrupts.deinit(); } pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void { const name = if (self.name.items.len == 0) "unknown" else self.name.items; const version = if (self.version.items.len == 0) "unknown" else self.version.items; const description = if (self.description.items.len == 0) "unknown" else self.description.items; try out_stream.print( \\pub const device_name = "{s}"; \\pub const device_revision = "{s}"; \\pub const device_description = "{s}"; \\ , .{ name, version, description }); if (self.cpu) |the_cpu| { try out_stream.print("{}\n", .{the_cpu}); } // now print peripherals for (self.peripherals.items) |peripheral| { try out_stream.print("{}\n", .{peripheral}); } // now print interrupt table try out_stream.writeAll("pub const interrupts = struct {\n"); var iter = self.interrupts.iterator(); while (iter.next()) |entry| { var interrupt = entry.value_ptr.*; if (interrupt.value) |int_value| { try out_stream.print( "pub const {s} = {};\n", .{ interrupt.name.items, int_value }, ); } } try out_stream.writeAll("};"); return; } }; pub const Cpu = struct { name: ArrayList(u8), revision: ArrayList(u8), endian: ArrayList(u8), mpu_present: ?bool, fpu_present: ?bool, nvic_prio_bits: ?u32, vendor_systick_config: ?bool, const Self = @This(); pub fn init(allocator: *Allocator) !Self { var name = ArrayList(u8).init(allocator); errdefer name.deinit(); var revision = ArrayList(u8).init(allocator); errdefer revision.deinit(); var endian = ArrayList(u8).init(allocator); errdefer endian.deinit(); return Self{ .name = name, .revision = revision, .endian = endian, .mpu_present = null, .fpu_present = null, .nvic_prio_bits = null, .vendor_systick_config = null, }; } pub fn deinit(self: *Self) void { self.name.deinit(); self.revision.deinit(); self.endian.deinit(); } pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void { try out_stream.writeAll("\n"); const name = if (self.name.items.len == 0) "unknown" else self.name.items; const revision = if (self.revision.items.len == 0) "unknown" else self.revision.items; const endian = if (self.endian.items.len == 0) "unknown" else self.endian.items; const mpu_present = self.mpu_present orelse false; const fpu_present = self.mpu_present orelse false; const vendor_systick_config = self.vendor_systick_config orelse false; try out_stream.print( \\pub const cpu = struct {{ \\ pub const name = "{s}"; \\ pub const revision = "{s}"; \\ pub const endian = "{s}"; \\ pub const mpu_present = {}; \\ pub const fpu_present = {}; \\ pub const vendor_systick_config = {}; \\ , .{ name, revision, endian, mpu_present, fpu_present, vendor_systick_config }); if (self.nvic_prio_bits) |prio_bits| { try out_stream.print( \\ pub const nvic_prio_bits = {}; \\ , .{prio_bits}); } try out_stream.writeAll("};"); return; } }; pub const Peripherals = ArrayList(Peripheral); pub const Peripheral = struct { name: ArrayList(u8), group_name: ArrayList(u8), description: ArrayList(u8), base_address: ?u32, address_block: ?AddressBlock, registers: Registers, const Self = @This(); pub fn init(allocator: *Allocator) !Self { var name = ArrayList(u8).init(allocator); errdefer name.deinit(); var group_name = ArrayList(u8).init(allocator); errdefer group_name.deinit(); var description = ArrayList(u8).init(allocator); errdefer description.deinit(); var registers = Registers.init(allocator); errdefer registers.deinit(); return Self{ .name = name, .group_name = group_name, .description = description, .base_address = null, .address_block = null, .registers = registers, }; } pub fn copy(self: Self, allocator: *Allocator) !Self { var the_copy = try Self.init(allocator); errdefer the_copy.deinit(); try the_copy.name.appendSlice(self.name.items); try the_copy.group_name.appendSlice(self.group_name.items); try the_copy.description.appendSlice(self.description.items); the_copy.base_address = self.base_address; the_copy.address_block = self.address_block; for (self.registers.items) |self_register| { try the_copy.registers.append(try self_register.copy(allocator)); } return the_copy; } pub fn deinit(self: *Self) void { self.name.deinit(); self.group_name.deinit(); self.description.deinit(); self.registers.deinit(); } pub fn isValid(self: Self) bool { if (self.name.items.len == 0) { return false; } _ = self.base_address orelse return false; return true; } pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void { try out_stream.writeAll("\n"); if (!self.isValid()) { try out_stream.writeAll("// Not enough info to print peripheral value\n"); return; } const name = self.name.items; const description = if (self.description.items.len == 0) "No description" else self.description.items; const base_address = self.base_address.?; try out_stream.print( \\/// {s} \\pub const {s} = struct {{ \\ \\const base_address = 0x{x}; , .{ description, name, base_address }); // now print registers for (self.registers.items) |register| { try out_stream.print("{}\n", .{register}); } // and close the peripheral try out_stream.print("}};", .{}); return; } }; pub const AddressBlock = struct { offset: ?u32, size: ?u32, usage: ArrayList(u8), const Self = @This(); pub fn init(allocator: *Allocator) !Self { var usage = ArrayList(u8).init(allocator); errdefer usage.deinit(); return Self{ .offset = null, .size = null, .usage = usage, }; } pub fn deinit(self: *Self) void { self.usage.deinit(); } }; pub const Interrupts = AutoHashMap(u32, Interrupt); pub const Interrupt = struct { name: ArrayList(u8), description: ArrayList(u8), value: ?u32, const Self = @This(); pub fn init(allocator: *Allocator) !Self { var name = ArrayList(u8).init(allocator); errdefer name.deinit(); var description = ArrayList(u8).init(allocator); errdefer description.deinit(); return Self{ .name = name, .description = description, .value = null, }; } pub fn copy(self: Self, allocator: *Allocator) !Self { var the_copy = try Self.init(allocator); try the_copy.name.append(self.name.items); try the_copy.description.append(self.description.items); the_copy.value = self.value; return the_copy; } pub fn deinit(self: *Self) void { self.name.deinit(); self.description.deinit(); } pub fn isValid(self: Self) bool { if (self.name.items.len == 0) { return false; } _ = self.value orelse return false; return true; } pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void { try out_stream.writeAll("\n"); if (!self.isValid()) { try output(context, "// Not enough info to print interrupt value\n"); return; } const name = self.name.items; const description = if (self.description.items.len == 0) "No description" else self.description.items; try out_stream.print( \\/// {s} \\pub const {s} = {s}; \\ , .{ description, name, value.? }); } }; const Registers = ArrayList(Register); pub const Register = struct { periph_containing: ArrayList(u8), name: ArrayList(u8), display_name: ArrayList(u8), description: ArrayList(u8), address_offset: ?u32, size: u32, reset_value: u32, fields: Fields, access: Access = .ReadWrite, const Self = @This(); pub fn init(allocator: *Allocator, periph: []const u8, reset_value: u32, size: u32) !Self { var prefix = ArrayList(u8).init(allocator); errdefer prefix.deinit(); try prefix.appendSlice(periph); var name = ArrayList(u8).init(allocator); errdefer name.deinit(); var display_name = ArrayList(u8).init(allocator); errdefer display_name.deinit(); var description = ArrayList(u8).init(allocator); errdefer description.deinit(); var fields = Fields.init(allocator); errdefer fields.deinit(); return Self{ .periph_containing = prefix, .name = name, .display_name = display_name, .description = description, .address_offset = null, .size = size, .reset_value = reset_value, .fields = fields, }; } pub fn copy(self: Self, allocator: *Allocator) !Self { var the_copy = try Self.init(allocator, self.periph_containing.items, self.reset_value, self.size); try the_copy.name.appendSlice(self.name.items); try the_copy.display_name.appendSlice(self.display_name.items); try the_copy.description.appendSlice(self.description.items); the_copy.address_offset = self.address_offset; the_copy.access = self.access; for (self.fields.items) |self_field| { try the_copy.fields.append(try self_field.copy(allocator)); } return the_copy; } pub fn deinit(self: *Self) void { self.periph_containing.deinit(); self.name.deinit(); self.display_name.deinit(); self.description.deinit(); self.fields.deinit(); } pub fn isValid(self: Self) bool { if (self.name.items.len == 0) { return false; } _ = self.address_offset orelse return false; return true; } fn fieldsSortCompare(context: void, left: Field, right: Field) bool { if (left.bit_offset != null and right.bit_offset != null) { if (left.bit_offset.? < right.bit_offset.?) { return true; } if (left.bit_offset.? > right.bit_offset.?) { return false; } } else if (left.bit_offset == null) { return true; } return false; } fn alignedEndOfUnusedChunk(chunk_start: u32, last_unused: u32) u32 { // Next multiple of 8 from chunk_start + 1 const next_multiple = (chunk_start + 8) & ~@as(u32, 7); return min(next_multiple, last_unused); } fn writeUnusedField(first_unused: u32, last_unused: u32, reg_reset_value: u32, out_stream: anytype) !void { // Fill unused bits between two fields // TODO: right now we have to manually chunk unused bits to 8-bit boundaries as a workaround // to this bug https://github.com/ziglang/zig/issues/2627 var chunk_start = first_unused; var chunk_end = alignedEndOfUnusedChunk(chunk_start, last_unused); try out_stream.print("\n/// unused [{}:{}]", .{ first_unused, last_unused - 1 }); while (chunk_start < last_unused) : ({ chunk_start = chunk_end; chunk_end = alignedEndOfUnusedChunk(chunk_start, last_unused); }) { try out_stream.writeAll("\n"); const chunk_width = chunk_end - chunk_start; const unused_value = Field.fieldResetValue(chunk_start, chunk_width, reg_reset_value); try out_stream.print("_unused{}: u{} = {},", .{ chunk_start, chunk_width, unused_value }); } } pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void { try out_stream.writeAll("\n"); if (!self.isValid()) { try out_stream.writeAll("// Not enough info to print register value\n"); return; } const name = self.name.items; const periph = self.periph_containing.items; const description = if (self.description.items.len == 0) "No description" else self.description.items; // print packed struct containing fields try out_stream.print( \\/// {s} \\const {s}_val = packed struct {{ , .{ name, name }); // Sort fields from LSB to MSB for next step std.sort.sort(Field, self.fields.items, {}, fieldsSortCompare); var last_uncovered_bit: u32 = 0; for (self.fields.items) |field| { if ((field.bit_offset == null) or (field.bit_width == null)) { try out_stream.writeAll("// Not enough info to print register\n"); return; } const bit_offset = field.bit_offset.?; const bit_width = field.bit_width.?; if (last_uncovered_bit != bit_offset) { try writeUnusedField(last_uncovered_bit, bit_offset, self.reset_value, out_stream); } try out_stream.print("{}", .{field}); last_uncovered_bit = bit_offset + bit_width; } // Check if we need padding at the end if (last_uncovered_bit != 32) { try writeUnusedField(last_uncovered_bit, 32, self.reset_value, out_stream); } // close the struct and init the register try out_stream.print( \\ \\}}; \\/// {s} \\pub const {s} = Register({s}_val).init(base_address + 0x{x}); , .{ description, name, name, self.address_offset.? }); return; } }; pub const Access = enum { ReadOnly, WriteOnly, ReadWrite, }; pub const Fields = ArrayList(Field); pub const Field = struct { periph: ArrayList(u8), register: ArrayList(u8), register_reset_value: u32, name: ArrayList(u8), description: ArrayList(u8), bit_offset: ?u32, bit_width: ?u32, access: Access = .ReadWrite, const Self = @This(); pub fn init(allocator: *Allocator, periph_containing: []const u8, register_containing: []const u8, register_reset_value: u32) !Self { var periph = ArrayList(u8).init(allocator); try periph.appendSlice(periph_containing); errdefer periph.deinit(); var register = ArrayList(u8).init(allocator); try register.appendSlice(register_containing); errdefer register.deinit(); var name = ArrayList(u8).init(allocator); errdefer name.deinit(); var description = ArrayList(u8).init(allocator); errdefer description.deinit(); return Self{ .periph = periph, .register = register, .register_reset_value = register_reset_value, .name = name, .description = description, .bit_offset = null, .bit_width = null, }; } pub fn copy(self: Self, allocator: *Allocator) !Self { var the_copy = try Self.init(allocator, self.periph.items, self.register.items, self.register_reset_value); try the_copy.name.appendSlice(self.name.items); try the_copy.description.appendSlice(self.description.items); the_copy.bit_offset = self.bit_offset; the_copy.bit_width = self.bit_width; the_copy.access = self.access; return the_copy; } pub fn deinit(self: *Self) void { self.periph.deinit(); self.register.deinit(); self.name.deinit(); self.description.deinit(); } pub fn fieldResetValue(bit_start: u32, bit_width: u32, reg_reset_value: u32) u32 { const shifted_reset_value = reg_reset_value >> @intCast(u5, bit_start); const reset_value_mask = @intCast(u32, (@as(u33, 1) << @intCast(u6, bit_width)) - 1); return shifted_reset_value & reset_value_mask; } pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void { try out_stream.writeAll("\n"); if (self.name.items.len == 0) { try out_stream.writeAll("// No name to print field value\n"); return; } if ((self.bit_offset == null) or (self.bit_width == null)) { try out_stream.writeAll("// Not enough info to print field\n"); return; } const name = self.name.items; const description = if (self.description.items.len == 0) "No description" else self.description.items; const start_bit = self.bit_offset.?; const end_bit = (start_bit + self.bit_width.? - 1); const bit_width = self.bit_width.?; const reg_reset_value = self.register_reset_value; const reset_value = fieldResetValue(start_bit, bit_width, reg_reset_value); try out_stream.print( \\/// {s} [{}:{}] \\/// {s} \\{s}: u{} = {}, , .{ name, start_bit, end_bit, // description description, // val name, bit_width, reset_value, }); return; } }; test "Field print" { var allocator = std.testing.allocator; const fieldDesiredPrint = \\ \\/// RNGEN [2:2] \\/// RNGEN comment \\RNGEN: u1 = 1, \\ ; var output_buffer = ArrayList(u8).init(allocator); defer output_buffer.deinit(); var buf_stream = output_buffer.writer(); var field = try Field.init(allocator, "PERIPH", "RND", 0b101); defer field.deinit(); try field.name.appendSlice("RNGEN"); try field.description.appendSlice("RNGEN comment"); field.bit_offset = 2; field.bit_width = 1; try buf_stream.print("{}\n", .{field}); std.testing.expect(std.mem.eql(u8, output_buffer.items, fieldDesiredPrint)); } test "Register Print" { var allocator = std.testing.allocator; const registerDesiredPrint = \\ \\/// RND \\const RND_val = packed struct { \\/// unused [0:1] \\_unused0: u2 = 1, \\/// RNGEN [2:2] \\/// RNGEN comment \\RNGEN: u1 = 1, \\/// unused [3:9] \\_unused3: u5 = 0, \\_unused8: u2 = 0, \\/// SEED [10:12] \\/// SEED comment \\SEED: u3 = 0, \\/// unused [13:31] \\_unused13: u3 = 0, \\_unused16: u8 = 0, \\_unused24: u8 = 0, \\}; \\/// RND comment \\pub const RND = Register(RND_val).init(base_address + 0x100); \\ ; var output_buffer = ArrayList(u8).init(allocator); defer output_buffer.deinit(); var buf_stream = output_buffer.writer(); var register = try Register.init(allocator, "PERIPH", 0b101, 0x20); defer register.deinit(); try register.name.appendSlice("RND"); try register.description.appendSlice("RND comment"); register.address_offset = 0x100; register.size = 0x20; var field = try Field.init(allocator, "PERIPH", "RND", 0b101); defer field.deinit(); try field.name.appendSlice("RNGEN"); try field.description.appendSlice("RNGEN comment"); field.bit_offset = 2; field.bit_width = 1; field.access = .ReadWrite; // write field will exist var field2 = try Field.init(allocator, "PERIPH", "RND", 0b101); defer field2.deinit(); try field2.name.appendSlice("SEED"); try field2.description.appendSlice("SEED comment"); field2.bit_offset = 10; field2.bit_width = 3; field2.access = .ReadWrite; try register.fields.append(field); try register.fields.append(field2); try buf_stream.print("{}\n", .{register}); std.testing.expectEqualSlices(u8, output_buffer.items, registerDesiredPrint); } test "Peripheral Print" { var allocator = std.testing.allocator; const peripheralDesiredPrint = \\ \\/// PERIPH comment \\pub const PERIPH = struct { \\ \\const base_address = 0x24000; \\/// RND \\const RND_val = packed struct { \\/// unused [0:1] \\_unused0: u2 = 1, \\/// RNGEN [2:2] \\/// RNGEN comment \\RNGEN: u1 = 1, \\/// unused [3:9] \\_unused3: u5 = 0, \\_unused8: u2 = 0, \\/// SEED [10:12] \\/// SEED comment \\SEED: u3 = 0, \\/// unused [13:31] \\_unused13: u3 = 0, \\_unused16: u8 = 0, \\_unused24: u8 = 0, \\}; \\/// RND comment \\pub const RND = Register(RND_val).init(base_address + 0x100); \\}; \\ ; var output_buffer = ArrayList(u8).init(allocator); defer output_buffer.deinit(); var buf_stream = output_buffer.writer(); var peripheral = try Peripheral.init(allocator); defer peripheral.deinit(); try peripheral.name.appendSlice("PERIPH"); try peripheral.description.appendSlice("PERIPH comment"); peripheral.base_address = 0x24000; var register = try Register.init(allocator, "PERIPH", 0b101, 0x20); defer register.deinit(); try register.name.appendSlice("RND"); try register.description.appendSlice("RND comment"); register.address_offset = 0x100; register.size = 0x20; var field = try Field.init(allocator, "PERIPH", "RND", 0b101); defer field.deinit(); try field.name.appendSlice("RNGEN"); try field.description.appendSlice("RNGEN comment"); field.bit_offset = 2; field.bit_width = 1; field.access = .ReadOnly; // since only register, write field will not exist var field2 = try Field.init(allocator, "PERIPH", "RND", 0b101); defer field2.deinit(); try field2.name.appendSlice("SEED"); try field2.description.appendSlice("SEED comment"); field2.bit_offset = 10; field2.bit_width = 3; field2.access = .ReadWrite; try register.fields.append(field); try register.fields.append(field2); try peripheral.registers.append(register); try buf_stream.print("{}\n", .{peripheral}); std.testing.expectEqualSlices(u8, peripheralDesiredPrint, output_buffer.items); } fn bitWidthToMask(width: u32) u32 { const max_supported_bits = 32; const width_to_mask = blk: { comptime var mask_array: [max_supported_bits + 1]u32 = undefined; inline for (mask_array) |*item, i| { const i_use = if (i == 0) max_supported_bits else i; // This is needed to support both Zig 0.7 and 0.8 const int_type_info = if (@hasField(builtin.TypeInfo.Int, "signedness")) .{ .signedness = .unsigned, .bits = i_use } else .{ .is_signed = false, .bits = i_use }; item.* = std.math.maxInt(@Type(builtin.TypeInfo{ .Int = int_type_info })); } break :blk mask_array; }; const width_to_mask_slice = width_to_mask[0..]; return width_to_mask_slice[if (width > max_supported_bits) 0 else width]; }
// Ported from musl, which is licensed under the MIT license: // https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT // // https://git.musl-libc.org/cgit/musl/tree/src/math/lnf.c // https://git.musl-libc.org/cgit/musl/tree/src/math/ln.c const std = @import("../std.zig"); const math = std.math; const expect = std.testing.expect; const builtin = @import("builtin"); const TypeId = builtin.TypeId; /// Returns the natural logarithm of x. /// /// Special Cases: /// - ln(+inf) = +inf /// - ln(0) = -inf /// - ln(x) = nan if x < 0 /// - ln(nan) = nan pub fn ln(x: var) @typeOf(x) { const T = @typeOf(x); switch (@typeId(T)) { TypeId.ComptimeFloat => { return @typeOf(1.0)(ln_64(x)); }, TypeId.Float => { return switch (T) { f32 => ln_32(x), f64 => ln_64(x), else => @compileError("ln not implemented for " ++ @typeName(T)), }; }, TypeId.ComptimeInt => { return @typeOf(1)(math.floor(ln_64(f64(x)))); }, TypeId.Int => { return T(math.floor(ln_64(f64(x)))); }, else => @compileError("ln not implemented for " ++ @typeName(T)), } } pub fn ln_32(x_: f32) f32 { const ln2_hi: f32 = 6.9313812256e-01; const ln2_lo: f32 = 9.0580006145e-06; const Lg1: f32 = 0xaaaaaa.0p-24; const Lg2: f32 = 0xccce13.0p-25; const Lg3: f32 = 0x91e9ee.0p-25; const Lg4: f32 = 0xf89e26.0p-26; var x = x_; var ix = @bitCast(u32, x); var k: i32 = 0; // x < 2^(-126) if (ix < 0x00800000 or ix >> 31 != 0) { // log(+-0) = -inf if (ix << 1 == 0) { return -math.inf(f32); } // log(-#) = nan if (ix >> 31 != 0) { return math.nan(f32); } // subnormal, scale x k -= 25; x *= 0x1.0p25; ix = @bitCast(u32, x); } else if (ix >= 0x7F800000) { return x; } else if (ix == 0x3F800000) { return 0; } // x into [sqrt(2) / 2, sqrt(2)] ix += 0x3F800000 - 0x3F3504F3; k += @intCast(i32, ix >> 23) - 0x7F; ix = (ix & 0x007FFFFF) + 0x3F3504F3; x = @bitCast(f32, ix); const f = x - 1.0; const s = f / (2.0 + f); const z = s * s; const w = z * z; const t1 = w * (Lg2 + w * Lg4); const t2 = z * (Lg1 + w * Lg3); const R = t2 + t1; const hfsq = 0.5 * f * f; const dk = @intToFloat(f32, k); return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi; } pub fn ln_64(x_: f64) f64 { const ln2_hi: f64 = 6.93147180369123816490e-01; const ln2_lo: f64 = 1.90821492927058770002e-10; const Lg1: f64 = 6.666666666666735130e-01; const Lg2: f64 = 3.999999999940941908e-01; const Lg3: f64 = 2.857142874366239149e-01; const Lg4: f64 = 2.222219843214978396e-01; const Lg5: f64 = 1.818357216161805012e-01; const Lg6: f64 = 1.531383769920937332e-01; const Lg7: f64 = 1.479819860511658591e-01; var x = x_; var ix = @bitCast(u64, x); var hx = @intCast(u32, ix >> 32); var k: i32 = 0; if (hx < 0x00100000 or hx >> 31 != 0) { // log(+-0) = -inf if (ix << 1 == 0) { return -math.inf(f64); } // log(-#) = nan if (hx >> 31 != 0) { return math.nan(f64); } // subnormal, scale x k -= 54; x *= 0x1.0p54; hx = @intCast(u32, @bitCast(u64, ix) >> 32); } else if (hx >= 0x7FF00000) { return x; } else if (hx == 0x3FF00000 and ix << 32 == 0) { return 0; } // x into [sqrt(2) / 2, sqrt(2)] hx += 0x3FF00000 - 0x3FE6A09E; k += @intCast(i32, hx >> 20) - 0x3FF; hx = (hx & 0x000FFFFF) + 0x3FE6A09E; ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF); x = @bitCast(f64, ix); const f = x - 1.0; const hfsq = 0.5 * f * f; const s = f / (2.0 + f); const z = s * s; const w = z * z; const t1 = w * (Lg2 + w * (Lg4 + w * Lg6)); const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); const R = t2 + t1; const dk = @intToFloat(f64, k); return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi; } test "math.ln" { expect(ln(f32(0.2)) == ln_32(0.2)); expect(ln(f64(0.2)) == ln_64(0.2)); } test "math.ln32" { const epsilon = 0.000001; expect(math.approxEq(f32, ln_32(0.2), -1.609438, epsilon)); expect(math.approxEq(f32, ln_32(0.8923), -0.113953, epsilon)); expect(math.approxEq(f32, ln_32(1.5), 0.405465, epsilon)); expect(math.approxEq(f32, ln_32(37.45), 3.623007, epsilon)); expect(math.approxEq(f32, ln_32(89.123), 4.490017, epsilon)); expect(math.approxEq(f32, ln_32(123123.234375), 11.720941, epsilon)); } test "math.ln64" { const epsilon = 0.000001; expect(math.approxEq(f64, ln_64(0.2), -1.609438, epsilon)); expect(math.approxEq(f64, ln_64(0.8923), -0.113953, epsilon)); expect(math.approxEq(f64, ln_64(1.5), 0.405465, epsilon)); expect(math.approxEq(f64, ln_64(37.45), 3.623007, epsilon)); expect(math.approxEq(f64, ln_64(89.123), 4.490017, epsilon)); expect(math.approxEq(f64, ln_64(123123.234375), 11.720941, epsilon)); } test "math.ln32.special" { expect(math.isPositiveInf(ln_32(math.inf(f32)))); expect(math.isNegativeInf(ln_32(0.0))); expect(math.isNan(ln_32(-1.0))); expect(math.isNan(ln_32(math.nan(f32)))); } test "math.ln64.special" { expect(math.isPositiveInf(ln_64(math.inf(f64)))); expect(math.isNegativeInf(ln_64(0.0))); expect(math.isNan(ln_64(-1.0))); expect(math.isNan(ln_64(math.nan(f64)))); }
// Test trailing comma syntax // zig fmt: off const struct_trailing_comma = struct.{ x: i32, y: i32, }; const struct_no_comma = struct.{ x: i32, y: i32 }; const struct_fn_no_comma = struct.{ fn m() void {} y: i32 }; const enum_no_comma = enum.{ A, B }; fn container_init() void { const S = struct.{ x: i32, y: i32 }; _ = S.{ .x = 1, .y = 2 }; _ = S.{ .x = 1, .y = 2, }; } fn switch_cases(x: i32) void { switch (x) { 1,2,3 => {}, 4,5, => {}, 6...8, => {}, else => {}, } } fn switch_prongs(x: i32) void { switch (x) { 0 => {}, else => {}, } switch (x) { 0 => {}, else => {} } } const fn_no_comma = fn(i32, i32)void; const fn_trailing_comma = fn(i32, i32,)void; fn fn_calls() void { fn add(x: i32, y: i32,) i32.{ x + y }; _ = add(1, 2); _ = add(1, 2,); } fn asm_lists() void { if (false) { // Build AST but don't analyze asm ("not real assembly" :[a] "x" (x),); asm ("not real assembly" :[a] "x" (->i32),:[a] "x" (1),); asm ("still not real assembly" :::"a","b",); } }
const std = @import("std.zig"); const root = @import("root"); const debug = std.debug; const assert = debug.assert; const testing = std.testing; const mem = std.mem; const os = std.os; const builtin = @import("builtin"); const c = std.c; const maxInt = std.math.maxInt; pub const LoggingAllocator = @import("heap/logging_allocator.zig").LoggingAllocator; pub const loggingAllocator = @import("heap/logging_allocator.zig").loggingAllocator; pub const ArenaAllocator = @import("heap/arena_allocator.zig").ArenaAllocator; pub const GeneralPurposeAllocator = @import("heap/general_purpose_allocator.zig").GeneralPurposeAllocator; const Allocator = mem.Allocator; usingnamespace if (comptime @hasDecl(c, "malloc_size")) struct { pub const supports_malloc_size = true; pub const malloc_size = c.malloc_size; } else if (comptime @hasDecl(c, "malloc_usable_size")) struct { pub const supports_malloc_size = true; pub const malloc_size = c.malloc_usable_size; } else struct { pub const supports_malloc_size = false; }; pub const c_allocator = &c_allocator_state; var c_allocator_state = Allocator{ .allocFn = cAlloc, .resizeFn = cResize, }; fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Allocator.Error![]u8 { assert(ptr_align <= @alignOf(c_longdouble)); const ptr = @ptrCast([*]u8, c.malloc(len) orelse return error.OutOfMemory); if (len_align == 0) { return ptr[0..len]; } const full_len = init: { if (supports_malloc_size) { const s = malloc_size(ptr); assert(s >= len); break :init s; } break :init len; }; return ptr[0..mem.alignBackwardAnyAlign(full_len, len_align)]; } fn cResize( self: *Allocator, buf: []u8, old_align: u29, new_len: usize, len_align: u29, ret_addr: usize, ) Allocator.Error!usize { if (new_len == 0) { c.free(buf.ptr); return 0; } if (new_len <= buf.len) { return mem.alignAllocLen(buf.len, new_len, len_align); } if (supports_malloc_size) { const full_len = malloc_size(buf.ptr); if (new_len <= full_len) { return mem.alignAllocLen(full_len, new_len, len_align); } } return error.OutOfMemory; } /// This allocator makes a syscall directly for every allocation and free. /// Thread-safe and lock-free. pub const page_allocator = if (std.Target.current.isWasm()) &wasm_page_allocator_state else if (std.Target.current.os.tag == .freestanding) root.os.heap.page_allocator else &page_allocator_state; var page_allocator_state = Allocator{ .allocFn = PageAllocator.alloc, .resizeFn = PageAllocator.resize, }; var wasm_page_allocator_state = Allocator{ .allocFn = WasmPageAllocator.alloc, .resizeFn = WasmPageAllocator.resize, }; /// Verifies that the adjusted length will still map to the full length pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize { const aligned_len = mem.alignAllocLen(full_len, len, len_align); assert(mem.alignForward(aligned_len, mem.page_size) == full_len); return aligned_len; } /// TODO Utilize this on Windows. pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null; const PageAllocator = struct { fn alloc(allocator: *Allocator, n: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 { assert(n > 0); const aligned_len = mem.alignForward(n, mem.page_size); if (builtin.os.tag == .windows) { const w = os.windows; // Although officially it's at least aligned to page boundary, // Windows is known to reserve pages on a 64K boundary. It's // even more likely that the requested alignment is <= 64K than // 4K, so we're just allocating blindly and hoping for the best. // see https://devblogs.microsoft.com/oldnewthing/?p=42223 const addr = w.VirtualAlloc( null, aligned_len, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, ) catch return error.OutOfMemory; // If the allocation is sufficiently aligned, use it. if (@ptrToInt(addr) & (alignment - 1) == 0) { return @ptrCast([*]u8, addr)[0..alignPageAllocLen(aligned_len, n, len_align)]; } // If it wasn't, actually do an explicitely aligned allocation. w.VirtualFree(addr, 0, w.MEM_RELEASE); const alloc_size = n + alignment - mem.page_size; while (true) { // Reserve a range of memory large enough to find a sufficiently // aligned address. const reserved_addr = w.VirtualAlloc( null, alloc_size, w.MEM_RESERVE, w.PAGE_NOACCESS, ) catch return error.OutOfMemory; const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); // Release the reserved pages (not actually used). w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE); // At this point, it is possible that another thread has // obtained some memory space that will cause the next // VirtualAlloc call to fail. To handle this, we will retry // until it succeeds. const ptr = w.VirtualAlloc( @intToPtr(*c_void, aligned_addr), aligned_len, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, ) catch continue; return @ptrCast([*]u8, ptr)[0..alignPageAllocLen(aligned_len, n, len_align)]; } } const max_drop_len = alignment - std.math.min(alignment, mem.page_size); const alloc_len = if (max_drop_len <= aligned_len - n) aligned_len else mem.alignForward(aligned_len + max_drop_len, mem.page_size); const hint = @atomicLoad(@TypeOf(next_mmap_addr_hint), &next_mmap_addr_hint, .Unordered); const slice = os.mmap( hint, alloc_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS, -1, 0, ) catch return error.OutOfMemory; assert(mem.isAligned(@ptrToInt(slice.ptr), mem.page_size)); const aligned_addr = mem.alignForward(@ptrToInt(slice.ptr), alignment); const result_ptr = @alignCast(mem.page_size, @intToPtr([*]u8, aligned_addr)); // Unmap the extra bytes that were only requested in order to guarantee // that the range of memory we were provided had a proper alignment in // it somewhere. The extra bytes could be at the beginning, or end, or both. const drop_len = aligned_addr - @ptrToInt(slice.ptr); if (drop_len != 0) { os.munmap(slice[0..drop_len]); } // Unmap extra pages const aligned_buffer_len = alloc_len - drop_len; if (aligned_buffer_len > aligned_len) { os.munmap(result_ptr[aligned_len..aligned_buffer_len]); } const new_hint = @alignCast(mem.page_size, result_ptr + aligned_len); _ = @cmpxchgStrong(@TypeOf(next_mmap_addr_hint), &next_mmap_addr_hint, hint, new_hint, .Monotonic, .Monotonic); return result_ptr[0..alignPageAllocLen(aligned_len, n, len_align)]; } fn resize( allocator: *Allocator, buf_unaligned: []u8, buf_align: u29, new_size: usize, len_align: u29, return_address: usize, ) Allocator.Error!usize { const new_size_aligned = mem.alignForward(new_size, mem.page_size); if (builtin.os.tag == .windows) { const w = os.windows; if (new_size == 0) { // From the docs: // "If the dwFreeType parameter is MEM_RELEASE, this parameter // must be 0 (zero). The function frees the entire region that // is reserved in the initial allocation call to VirtualAlloc." // So we can only use MEM_RELEASE when actually releasing the // whole allocation. w.VirtualFree(buf_unaligned.ptr, 0, w.MEM_RELEASE); return 0; } if (new_size <= buf_unaligned.len) { const base_addr = @ptrToInt(buf_unaligned.ptr); const old_addr_end = base_addr + buf_unaligned.len; const new_addr_end = mem.alignForward(base_addr + new_size, mem.page_size); if (old_addr_end > new_addr_end) { // For shrinking that is not releasing, we will only // decommit the pages not needed anymore. w.VirtualFree( @intToPtr(*c_void, new_addr_end), old_addr_end - new_addr_end, w.MEM_DECOMMIT, ); } return alignPageAllocLen(new_size_aligned, new_size, len_align); } const old_size_aligned = mem.alignForward(buf_unaligned.len, mem.page_size); if (new_size_aligned <= old_size_aligned) { return alignPageAllocLen(new_size_aligned, new_size, len_align); } return error.OutOfMemory; } const buf_aligned_len = mem.alignForward(buf_unaligned.len, mem.page_size); if (new_size_aligned == buf_aligned_len) return alignPageAllocLen(new_size_aligned, new_size, len_align); if (new_size_aligned < buf_aligned_len) { const ptr = @intToPtr([*]align(mem.page_size) u8, @ptrToInt(buf_unaligned.ptr) + new_size_aligned); // TODO: if the next_mmap_addr_hint is within the unmapped range, update it os.munmap(ptr[0 .. buf_aligned_len - new_size_aligned]); if (new_size_aligned == 0) return 0; return alignPageAllocLen(new_size_aligned, new_size, len_align); } // TODO: call mremap // TODO: if the next_mmap_addr_hint is within the remapped range, update it return error.OutOfMemory; } }; const WasmPageAllocator = struct { comptime { if (!std.Target.current.isWasm()) { @compileError("WasmPageAllocator is only available for wasm32 arch"); } } const PageStatus = enum(u1) { used = 0, free = 1, pub const none_free: u8 = 0; }; const FreeBlock = struct { data: []u128, const Io = std.packed_int_array.PackedIntIo(u1, .Little); fn totalPages(self: FreeBlock) usize { return self.data.len * 128; } fn isInitialized(self: FreeBlock) bool { return self.data.len > 0; } fn getBit(self: FreeBlock, idx: usize) PageStatus { const bit_offset = 0; return @intToEnum(PageStatus, Io.get(mem.sliceAsBytes(self.data), idx, bit_offset)); } fn setBits(self: FreeBlock, start_idx: usize, len: usize, val: PageStatus) void { const bit_offset = 0; var i: usize = 0; while (i < len) : (i += 1) { Io.set(mem.sliceAsBytes(self.data), start_idx + i, bit_offset, @enumToInt(val)); } } // Use '0xFFFFFFFF' as a _missing_ sentinel // This saves ~50 bytes compared to returning a nullable // We can guarantee that conventional memory never gets this big, // and wasm32 would not be able to address this memory (32 GB > usize). // Revisit if this is settled: https://github.com/ziglang/zig/issues/3806 const not_found = std.math.maxInt(usize); fn useRecycled(self: FreeBlock, num_pages: usize, alignment: u29) usize { @setCold(true); for (self.data) |segment, i| { const spills_into_next = @bitCast(i128, segment) < 0; const has_enough_bits = @popCount(u128, segment) >= num_pages; if (!spills_into_next and !has_enough_bits) continue; var j: usize = i * 128; while (j < (i + 1) * 128) : (j += 1) { var count: usize = 0; while (j + count < self.totalPages() and self.getBit(j + count) == .free) { count += 1; const addr = j * mem.page_size; if (count >= num_pages and mem.isAligned(addr, alignment)) { self.setBits(j, num_pages, .used); return j; } } j += count; } } return not_found; } fn recycle(self: FreeBlock, start_idx: usize, len: usize) void { self.setBits(start_idx, len, .free); } }; var _conventional_data = [_]u128{0} ** 16; // Marking `conventional` as const saves ~40 bytes const conventional = FreeBlock{ .data = &_conventional_data }; var extended = FreeBlock{ .data = &[_]u128{} }; fn extendedOffset() usize { return conventional.totalPages(); } fn nPages(memsize: usize) usize { return mem.alignForward(memsize, mem.page_size) / mem.page_size; } fn alloc(allocator: *Allocator, len: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 { const page_count = nPages(len); const page_idx = try allocPages(page_count, alignment); return @intToPtr([*]u8, page_idx * mem.page_size)[0..alignPageAllocLen(page_count * mem.page_size, len, len_align)]; } fn allocPages(page_count: usize, alignment: u29) !usize { { const idx = conventional.useRecycled(page_count, alignment); if (idx != FreeBlock.not_found) { return idx; } } const idx = extended.useRecycled(page_count, alignment); if (idx != FreeBlock.not_found) { return idx + extendedOffset(); } const next_page_idx = @wasmMemorySize(0); const next_page_addr = next_page_idx * mem.page_size; const aligned_addr = mem.alignForward(next_page_addr, alignment); const drop_page_count = @divExact(aligned_addr - next_page_addr, mem.page_size); const result = @wasmMemoryGrow(0, @intCast(u32, drop_page_count + page_count)); if (result <= 0) return error.OutOfMemory; assert(result == next_page_idx); const aligned_page_idx = next_page_idx + drop_page_count; if (drop_page_count > 0) { freePages(next_page_idx, aligned_page_idx); } return @intCast(usize, aligned_page_idx); } fn freePages(start: usize, end: usize) void { if (start < extendedOffset()) { conventional.recycle(start, std.math.min(extendedOffset(), end) - start); } if (end > extendedOffset()) { var new_end = end; if (!extended.isInitialized()) { // Steal the last page from the memory currently being recycled // TODO: would it be better if we use the first page instead? new_end -= 1; extended.data = @intToPtr([*]u128, new_end * mem.page_size)[0 .. mem.page_size / @sizeOf(u128)]; // Since this is the first page being freed and we consume it, assume *nothing* is free. mem.set(u128, extended.data, PageStatus.none_free); } const clamped_start = std.math.max(extendedOffset(), start); extended.recycle(clamped_start - extendedOffset(), new_end - clamped_start); } } fn resize( allocator: *Allocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, return_address: usize, ) error{OutOfMemory}!usize { const aligned_len = mem.alignForward(buf.len, mem.page_size); if (new_len > aligned_len) return error.OutOfMemory; const current_n = nPages(aligned_len); const new_n = nPages(new_len); if (new_n != current_n) { const base = nPages(@ptrToInt(buf.ptr)); freePages(base + new_n, base + current_n); } return if (new_len == 0) 0 else alignPageAllocLen(new_n * mem.page_size, new_len, len_align); } }; pub const HeapAllocator = switch (builtin.os.tag) { .windows => struct { allocator: Allocator, heap_handle: ?HeapHandle, const HeapHandle = os.windows.HANDLE; pub fn init() HeapAllocator { return HeapAllocator{ .allocator = Allocator{ .allocFn = alloc, .resizeFn = resize, }, .heap_handle = null, }; } pub fn deinit(self: *HeapAllocator) void { if (self.heap_handle) |heap_handle| { os.windows.HeapDestroy(heap_handle); } } fn getRecordPtr(buf: []u8) *align(1) usize { return @intToPtr(*align(1) usize, @ptrToInt(buf.ptr) + buf.len); } fn alloc( allocator: *Allocator, n: usize, ptr_align: u29, len_align: u29, return_address: usize, ) error{OutOfMemory}![]u8 { const self = @fieldParentPtr(HeapAllocator, "allocator", allocator); const amt = n + ptr_align - 1 + @sizeOf(usize); const optional_heap_handle = @atomicLoad(?HeapHandle, &self.heap_handle, builtin.AtomicOrder.SeqCst); const heap_handle = optional_heap_handle orelse blk: { const options = if (builtin.single_threaded) os.windows.HEAP_NO_SERIALIZE else 0; const hh = os.windows.kernel32.HeapCreate(options, amt, 0) orelse return error.OutOfMemory; const other_hh = @cmpxchgStrong(?HeapHandle, &self.heap_handle, null, hh, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) orelse break :blk hh; os.windows.HeapDestroy(hh); break :blk other_hh.?; // can't be null because of the cmpxchg }; const ptr = os.windows.kernel32.HeapAlloc(heap_handle, 0, amt) orelse return error.OutOfMemory; const root_addr = @ptrToInt(ptr); const aligned_addr = mem.alignForward(root_addr, ptr_align); const return_len = init: { if (len_align == 0) break :init n; const full_len = os.windows.kernel32.HeapSize(heap_handle, 0, ptr); assert(full_len != std.math.maxInt(usize)); assert(full_len >= amt); break :init mem.alignBackwardAnyAlign(full_len - (aligned_addr - root_addr), len_align); }; const buf = @intToPtr([*]u8, aligned_addr)[0..return_len]; getRecordPtr(buf).* = root_addr; return buf; } fn resize( allocator: *Allocator, buf: []u8, buf_align: u29, new_size: usize, len_align: u29, return_address: usize, ) error{OutOfMemory}!usize { const self = @fieldParentPtr(HeapAllocator, "allocator", allocator); if (new_size == 0) { os.windows.HeapFree(self.heap_handle.?, 0, @intToPtr(*c_void, getRecordPtr(buf).*)); return 0; } const root_addr = getRecordPtr(buf).*; const align_offset = @ptrToInt(buf.ptr) - root_addr; const amt = align_offset + new_size + @sizeOf(usize); const new_ptr = os.windows.kernel32.HeapReAlloc( self.heap_handle.?, os.windows.HEAP_REALLOC_IN_PLACE_ONLY, @intToPtr(*c_void, root_addr), amt, ) orelse return error.OutOfMemory; assert(new_ptr == @intToPtr(*c_void, root_addr)); const return_len = init: { if (len_align == 0) break :init new_size; const full_len = os.windows.kernel32.HeapSize(self.heap_handle.?, 0, new_ptr); assert(full_len != std.math.maxInt(usize)); assert(full_len >= amt); break :init mem.alignBackwardAnyAlign(full_len - align_offset, len_align); }; getRecordPtr(buf.ptr[0..return_len]).* = root_addr; return return_len; } }, else => @compileError("Unsupported OS"), }; fn sliceContainsPtr(container: []u8, ptr: [*]u8) bool { return @ptrToInt(ptr) >= @ptrToInt(container.ptr) and @ptrToInt(ptr) < (@ptrToInt(container.ptr) + container.len); } fn sliceContainsSlice(container: []u8, slice: []u8) bool { return @ptrToInt(slice.ptr) >= @ptrToInt(container.ptr) and (@ptrToInt(slice.ptr) + slice.len) <= (@ptrToInt(container.ptr) + container.len); } pub const FixedBufferAllocator = struct { allocator: Allocator, end_index: usize, buffer: []u8, pub fn init(buffer: []u8) FixedBufferAllocator { return FixedBufferAllocator{ .allocator = Allocator{ .allocFn = alloc, .resizeFn = resize, }, .buffer = buffer, .end_index = 0, }; } pub fn ownsPtr(self: *FixedBufferAllocator, ptr: [*]u8) bool { return sliceContainsPtr(self.buffer, ptr); } pub fn ownsSlice(self: *FixedBufferAllocator, slice: []u8) bool { return sliceContainsSlice(self.buffer, slice); } /// NOTE: this will not work in all cases, if the last allocation had an adjusted_index /// then we won't be able to determine what the last allocation was. This is because /// the alignForward operation done in alloc is not reverisible. pub fn isLastAllocation(self: *FixedBufferAllocator, buf: []u8) bool { return buf.ptr + buf.len == self.buffer.ptr + self.end_index; } fn alloc(allocator: *Allocator, n: usize, ptr_align: u29, len_align: u29, ra: usize) ![]u8 { const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator); const aligned_addr = mem.alignForward(@ptrToInt(self.buffer.ptr) + self.end_index, ptr_align); const adjusted_index = aligned_addr - @ptrToInt(self.buffer.ptr); const new_end_index = adjusted_index + n; if (new_end_index > self.buffer.len) { return error.OutOfMemory; } const result = self.buffer[adjusted_index..new_end_index]; self.end_index = new_end_index; return result; } fn resize( allocator: *Allocator, buf: []u8, buf_align: u29, new_size: usize, len_align: u29, return_address: usize, ) Allocator.Error!usize { const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator); assert(self.ownsSlice(buf)); // sanity check if (!self.isLastAllocation(buf)) { if (new_size > buf.len) return error.OutOfMemory; return if (new_size == 0) 0 else mem.alignAllocLen(buf.len, new_size, len_align); } if (new_size <= buf.len) { const sub = buf.len - new_size; self.end_index -= sub; return if (new_size == 0) 0 else mem.alignAllocLen(buf.len - sub, new_size, len_align); } const add = new_size - buf.len; if (add + self.end_index > self.buffer.len) { return error.OutOfMemory; } self.end_index += add; return new_size; } pub fn reset(self: *FixedBufferAllocator) void { self.end_index = 0; } }; pub const ThreadSafeFixedBufferAllocator = blk: { if (builtin.single_threaded) { break :blk FixedBufferAllocator; } else { // lock free break :blk struct { allocator: Allocator, end_index: usize, buffer: []u8, pub fn init(buffer: []u8) ThreadSafeFixedBufferAllocator { return ThreadSafeFixedBufferAllocator{ .allocator = Allocator{ .allocFn = alloc, .resizeFn = Allocator.noResize, }, .buffer = buffer, .end_index = 0, }; } fn alloc(allocator: *Allocator, n: usize, ptr_align: u29, len_align: u29, ra: usize) ![]u8 { const self = @fieldParentPtr(ThreadSafeFixedBufferAllocator, "allocator", allocator); var end_index = @atomicLoad(usize, &self.end_index, builtin.AtomicOrder.SeqCst); while (true) { const addr = @ptrToInt(self.buffer.ptr) + end_index; const adjusted_addr = mem.alignForward(addr, ptr_align); const adjusted_index = end_index + (adjusted_addr - addr); const new_end_index = adjusted_index + n; if (new_end_index > self.buffer.len) { return error.OutOfMemory; } end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) orelse return self.buffer[adjusted_index..new_end_index]; } } pub fn reset(self: *ThreadSafeFixedBufferAllocator) void { self.end_index = 0; } }; } }; pub fn stackFallback(comptime size: usize, fallback_allocator: *Allocator) StackFallbackAllocator(size) { return StackFallbackAllocator(size){ .buffer = undefined, .fallback_allocator = fallback_allocator, .fixed_buffer_allocator = undefined, .allocator = Allocator{ .allocFn = StackFallbackAllocator(size).realloc, .resizeFn = StackFallbackAllocator(size).resize, }, }; } pub fn StackFallbackAllocator(comptime size: usize) type { return struct { const Self = @This(); buffer: [size]u8, allocator: Allocator, fallback_allocator: *Allocator, fixed_buffer_allocator: FixedBufferAllocator, pub fn get(self: *Self) *Allocator { self.fixed_buffer_allocator = FixedBufferAllocator.init(self.buffer[0..]); return &self.allocator; } fn alloc( allocator: *Allocator, len: usize, ptr_align: u29, len_align: u29, return_address: usize, ) error{OutOfMemory}![*]u8 { const self = @fieldParentPtr(Self, "allocator", allocator); return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, ptr_align) catch return fallback_allocator.alloc(len, ptr_align); } fn resize( self: *Allocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, return_address: usize, ) error{OutOfMemory}!void { const self = @fieldParentPtr(Self, "allocator", allocator); if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) { try self.fixed_buffer_allocator.resize(buf, new_len); } else { try self.fallback_allocator.resize(buf, new_len); } } }; } test "c_allocator" { if (builtin.link_libc) { var slice = try c_allocator.alloc(u8, 50); defer c_allocator.free(slice); slice = try c_allocator.realloc(slice, 100); } } test "WasmPageAllocator internals" { if (comptime std.Target.current.isWasm()) { const conventional_memsize = WasmPageAllocator.conventional.totalPages() * mem.page_size; const initial = try page_allocator.alloc(u8, mem.page_size); std.debug.assert(@ptrToInt(initial.ptr) < conventional_memsize); // If this isn't conventional, the rest of these tests don't make sense. Also we have a serious memory leak in the test suite. var inplace = try page_allocator.realloc(initial, 1); testing.expectEqual(initial.ptr, inplace.ptr); inplace = try page_allocator.realloc(inplace, 4); testing.expectEqual(initial.ptr, inplace.ptr); page_allocator.free(inplace); const reuse = try page_allocator.alloc(u8, 1); testing.expectEqual(initial.ptr, reuse.ptr); page_allocator.free(reuse); // This segment may span conventional and extended which has really complex rules so we're just ignoring it for now. const padding = try page_allocator.alloc(u8, conventional_memsize); page_allocator.free(padding); const extended = try page_allocator.alloc(u8, conventional_memsize); testing.expect(@ptrToInt(extended.ptr) >= conventional_memsize); const use_small = try page_allocator.alloc(u8, 1); testing.expectEqual(initial.ptr, use_small.ptr); page_allocator.free(use_small); inplace = try page_allocator.realloc(extended, 1); testing.expectEqual(extended.ptr, inplace.ptr); page_allocator.free(inplace); const reuse_extended = try page_allocator.alloc(u8, conventional_memsize); testing.expectEqual(extended.ptr, reuse_extended.ptr); page_allocator.free(reuse_extended); } } test "PageAllocator" { const allocator = page_allocator; try testAllocator(allocator); try testAllocatorAligned(allocator, 16); if (!std.Target.current.isWasm()) { try testAllocatorLargeAlignment(allocator); try testAllocatorAlignedShrink(allocator); } if (builtin.os.tag == .windows) { // Trying really large alignment. As mentionned in the implementation, // VirtualAlloc returns 64K aligned addresses. We want to make sure // PageAllocator works beyond that, as it's not tested by // `testAllocatorLargeAlignment`. const slice = try allocator.alignedAlloc(u8, 1 << 20, 128); slice[0] = 0x12; slice[127] = 0x34; allocator.free(slice); } { var buf = try allocator.alloc(u8, mem.page_size + 1); defer allocator.free(buf); buf = try allocator.realloc(buf, 1); // shrink past the page boundary } } test "HeapAllocator" { if (builtin.os.tag == .windows) { var heap_allocator = HeapAllocator.init(); defer heap_allocator.deinit(); const allocator = &heap_allocator.allocator; try testAllocator(allocator); try testAllocatorAligned(allocator, 16); try testAllocatorLargeAlignment(allocator); try testAllocatorAlignedShrink(allocator); } } test "ArenaAllocator" { var arena_allocator = ArenaAllocator.init(page_allocator); defer arena_allocator.deinit(); try testAllocator(&arena_allocator.allocator); try testAllocatorAligned(&arena_allocator.allocator, 16); try testAllocatorLargeAlignment(&arena_allocator.allocator); try testAllocatorAlignedShrink(&arena_allocator.allocator); } var test_fixed_buffer_allocator_memory: [800000 * @sizeOf(u64)]u8 = undefined; test "FixedBufferAllocator" { var fixed_buffer_allocator = mem.validationWrap(FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..])); try testAllocator(&fixed_buffer_allocator.allocator); try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16); try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); try testAllocatorAlignedShrink(&fixed_buffer_allocator.allocator); } test "FixedBufferAllocator.reset" { var buf: [8]u8 align(@alignOf(u64)) = undefined; var fba = FixedBufferAllocator.init(buf[0..]); const X = 0xeeeeeeeeeeeeeeee; const Y = 0xffffffffffffffff; var x = try fba.allocator.create(u64); x.* = X; testing.expectError(error.OutOfMemory, fba.allocator.create(u64)); fba.reset(); var y = try fba.allocator.create(u64); y.* = Y; // we expect Y to have overwritten X. testing.expect(x.* == y.*); testing.expect(y.* == Y); } test "FixedBufferAllocator Reuse memory on realloc" { var small_fixed_buffer: [10]u8 = undefined; // check if we re-use the memory { var fixed_buffer_allocator = FixedBufferAllocator.init(small_fixed_buffer[0..]); var slice0 = try fixed_buffer_allocator.allocator.alloc(u8, 5); testing.expect(slice0.len == 5); var slice1 = try fixed_buffer_allocator.allocator.realloc(slice0, 10); testing.expect(slice1.ptr == slice0.ptr); testing.expect(slice1.len == 10); testing.expectError(error.OutOfMemory, fixed_buffer_allocator.allocator.realloc(slice1, 11)); } // check that we don't re-use the memory if it's not the most recent block { var fixed_buffer_allocator = FixedBufferAllocator.init(small_fixed_buffer[0..]); var slice0 = try fixed_buffer_allocator.allocator.alloc(u8, 2); slice0[0] = 1; slice0[1] = 2; var slice1 = try fixed_buffer_allocator.allocator.alloc(u8, 2); var slice2 = try fixed_buffer_allocator.allocator.realloc(slice0, 4); testing.expect(slice0.ptr != slice2.ptr); testing.expect(slice1.ptr != slice2.ptr); testing.expect(slice2[0] == 1); testing.expect(slice2[1] == 2); } } test "ThreadSafeFixedBufferAllocator" { var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); try testAllocator(&fixed_buffer_allocator.allocator); try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16); try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); try testAllocatorAlignedShrink(&fixed_buffer_allocator.allocator); } pub fn testAllocator(base_allocator: *mem.Allocator) !void { var validationAllocator = mem.validationWrap(base_allocator); const allocator = &validationAllocator.allocator; var slice = try allocator.alloc(*i32, 100); testing.expect(slice.len == 100); for (slice) |*item, i| { item.* = try allocator.create(i32); item.*.* = @intCast(i32, i); } slice = try allocator.realloc(slice, 20000); testing.expect(slice.len == 20000); for (slice[0..100]) |item, i| { testing.expect(item.* == @intCast(i32, i)); allocator.destroy(item); } slice = allocator.shrink(slice, 50); testing.expect(slice.len == 50); slice = allocator.shrink(slice, 25); testing.expect(slice.len == 25); slice = allocator.shrink(slice, 0); testing.expect(slice.len == 0); slice = try allocator.realloc(slice, 10); testing.expect(slice.len == 10); allocator.free(slice); } pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void { var validationAllocator = mem.validationWrap(base_allocator); const allocator = &validationAllocator.allocator; // initial var slice = try allocator.alignedAlloc(u8, alignment, 10); testing.expect(slice.len == 10); // grow slice = try allocator.realloc(slice, 100); testing.expect(slice.len == 100); // shrink slice = allocator.shrink(slice, 10); testing.expect(slice.len == 10); // go to zero slice = allocator.shrink(slice, 0); testing.expect(slice.len == 0); // realloc from zero slice = try allocator.realloc(slice, 100); testing.expect(slice.len == 100); // shrink with shrink slice = allocator.shrink(slice, 10); testing.expect(slice.len == 10); // shrink to zero slice = allocator.shrink(slice, 0); testing.expect(slice.len == 0); } pub fn testAllocatorLargeAlignment(base_allocator: *mem.Allocator) mem.Allocator.Error!void { var validationAllocator = mem.validationWrap(base_allocator); const allocator = &validationAllocator.allocator; //Maybe a platform's page_size is actually the same as or // very near usize? if (mem.page_size << 2 > maxInt(usize)) return; const USizeShift = std.meta.Int(false, std.math.log2(usize.bit_count)); const large_align = @as(u29, mem.page_size << 2); var align_mask: usize = undefined; _ = @shlWithOverflow(usize, ~@as(usize, 0), @as(USizeShift, @ctz(u29, large_align)), &align_mask); var slice = try allocator.alignedAlloc(u8, large_align, 500); testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = allocator.shrink(slice, 100); testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = try allocator.realloc(slice, 5000); testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = allocator.shrink(slice, 10); testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = try allocator.realloc(slice, 20000); testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); allocator.free(slice); } pub fn testAllocatorAlignedShrink(base_allocator: *mem.Allocator) mem.Allocator.Error!void { var validationAllocator = mem.validationWrap(base_allocator); const allocator = &validationAllocator.allocator; var debug_buffer: [1000]u8 = undefined; const debug_allocator = &FixedBufferAllocator.init(&debug_buffer).allocator; const alloc_size = mem.page_size * 2 + 50; var slice = try allocator.alignedAlloc(u8, 16, alloc_size); defer allocator.free(slice); var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator); // On Windows, VirtualAlloc returns addresses aligned to a 64K boundary, // which is 16 pages, hence the 32. This test may require to increase // the size of the allocations feeding the `allocator` parameter if they // fail, because of this high over-alignment we want to have. while (@ptrToInt(slice.ptr) == mem.alignForward(@ptrToInt(slice.ptr), mem.page_size * 32)) { try stuff_to_free.append(slice); slice = try allocator.alignedAlloc(u8, 16, alloc_size); } while (stuff_to_free.popOrNull()) |item| { allocator.free(item); } slice[0] = 0x12; slice[60] = 0x34; // realloc to a smaller size but with a larger alignment slice = try allocator.reallocAdvanced(slice, mem.page_size * 32, alloc_size / 2, .exact); testing.expect(slice[0] == 0x12); testing.expect(slice[60] == 0x34); } test "heap" { _ = @import("heap/logging_allocator.zig"); }
const expectEqual = @import("std").testing.expectEqual; const std = @import("std"); const mystruct = struct { pending: ?listofstructs, }; pub fn TailQueue(comptime T: type) type { return struct { const Self = @This(); pub const Node = struct { prev: ?*Node, next: ?*Node, data: T, }; first: ?*Node, last: ?*Node, len: usize, pub fn init() Self { return Self{ .first = null, .last = null, .len = 0, }; } }; } const listofstructs = TailQueue(mystruct); const a = struct { const Self = @This(); foo: listofstructs, pub fn init() Self { return Self{ .foo = listofstructs.init(), }; } }; test "intialization" { var t = a.init(); try std.testing.expectEqual(t.foo.len, 0); }
// Copyright (c) 2018 emekoi // // This library is free software; you can redistribute it and/or modify it // under the terms of the MIT license. See LICENSE for details. // const std = @import("std"); const debug = std.debug; const mem = std.mem; const Allocator = mem.Allocator; const assert = debug.assert; const ArrayList = std.ArrayList; /// A buffer that allocates memory and maintains a null byte at the end. pub fn Buffer(comptime T: type) type { return struct { const Self = @This(); list: ArrayList(T), /// Must deinitialize with deinit. pub fn init(allocator: *Allocator, m: []const T) !Self { var self = try initSize(allocator, m.len); mem.copy(T, self.list.items, m); return self; } /// Must deinitialize with deinit. pub fn initSize(allocator: *Allocator, size: usize) !Self { var self = initNull(allocator); try self.resize(size); return self; } /// Must deinitialize with deinit. /// None of the other operations are valid until you do one of these: /// * ::replaceContents /// * ::resize pub fn initNull(allocator: *Allocator) Self { return Self{ .list = ArrayList(T).init(allocator) }; } /// Must deinitialize with deinit. pub fn initFromSelf(buffer: *const Self) !Self { return Self.init(buffer.list.allocator, buffer.toSliceConst()); } /// Self takes ownership of the passed in slice. The slice must have been /// allocated with `allocator`. /// Must deinitialize with deinit. pub fn fromOwnedSlice(allocator: *Allocator, slice: []T) !Self { var self = Self{ .list = ArrayList(T).fromOwnedSlice(allocator, slice) }; try self.list.append(0); return self; } /// The caller owns the returned memory. The Self becomes null and /// is safe to `deinit`. pub fn toOwnedSlice(self: *Self) []T { const allocator = self.list.allocator; const result = allocator.shrink(T, self.list.items, self.len()); self.* = initNull(allocator); return result; } pub fn allocPrint(allocator: *Allocator, comptime format: []const T, args: ...) !Self { const countSize = struct { fn countSize(size: *usize, bytes: []const T) (error{}!void) { size.* += bytes.len; } }.countSize; var size: usize = 0; std.fmt.format(&size, error{}, countSize, format, args) catch |err| switch (err) {}; var self = try Self.initSize(allocator, size); assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size); return self; } pub fn deinit(self: *Self) void { self.list.deinit(); } pub fn toSlice(self: *const Self) []T { return self.list.toSlice()[0..self.len()]; } pub fn toSliceConst(self: *const Self) []const T { return self.list.toSliceConst()[0..self.len()]; } pub fn shrink(self: *Self, new_len: usize) void { assert(new_len <= self.len()); self.list.shrink(new_len + 1); self.list.items[self.len()] = 0; } pub fn resize(self: *Self, new_len: usize) !void { try self.list.resize(new_len + 1); self.list.items[self.len()] = 0; } pub fn isNull(self: *const Self) bool { return self.list.len == 0; } pub fn len(self: *const Self) usize { return self.list.len - 1; } pub fn append(self: *Self, m: []const T) !void { const old_len = self.len(); try self.resize(old_len + m.len); mem.copy(T, self.list.toSlice()[old_len..], m); } pub fn appendByte(self: *Self, byte: T) !void { const old_len = self.len(); try self.resize(old_len + 1); self.list.toSlice()[old_len] = byte; } pub fn eql(self: *const Self, m: []const T) bool { return mem.eql(T, self.toSliceConst(), m); } pub fn startsWith(self: *const Self, m: []const T) bool { if (self.len() < m.len) return false; return mem.eql(T, self.list.items[0..m.len], m); } pub fn endsWith(self: *const Self, m: []const T) bool { const l = self.len(); if (l < m.len) return false; const start = l - m.len; return mem.eql(T, self.list.items[start..l], m); } pub fn replaceContents(self: *Self, m: []const T) !void { try self.resize(m.len); mem.copy(T, self.list.toSlice(), m); } /// For passing to C functions. pub fn ptr(self: *const Self) [*]T { return self.list.items.ptr; } }; }
const std = @import("std"); const ast = std.zig.ast; const util = @import("utils.zig"); const source_url = "https://github.com/ziglang/zig/blob/master/lib/std/"; const out_fname_html = "index.html"; const in_fname_head = "html_head.html"; const in_fname_entries = "entries.json"; const max_read_filesize = 20 * 1024 * 1024; // 20Mb I mean, really. const JsonEntry = struct { name: []const u8 = undefined, description: ?[]const u8 = null, hide: bool = false, compact: bool = false, }; var tree: ast.Tree = undefined; const DeclInfoLists = struct { name: []const u8 = undefined, fields: std.ArrayList(DeclInfo), types: std.ArrayList(DeclInfo), funcs: std.ArrayList(DeclInfo), values: std.ArrayList(DeclInfo), json_entry: JsonEntry = undefined, pub fn init(alloc: *std.mem.Allocator) !@This() { return @This(){ .fields = std.ArrayList(DeclInfo).init(alloc), .types = std.ArrayList(DeclInfo).init(alloc), .funcs = std.ArrayList(DeclInfo).init(alloc), .values = std.ArrayList(DeclInfo).init(alloc), }; } pub fn htmlStringify(self: DeclInfoLists, writer: anytype) std.fs.File.WriteError!void { if (self.json_entry.hide) { // Ignore hidden items. I tried minimizing or explaining hidden items, but there // is already so much stuff, they were just adding to the clutter. return; } // On one hand, this won't work for Windows paths. On the other // hand, the '/' slash works for the source URLs as-is, so... const in_subdir = std.mem.containsAtLeast(u8, self.name, 1, "/"); const subdir_class = if (in_subdir) "subdir" else ""; const hidden_class = if (self.json_entry.hide) "hidden" else ""; try writer.print( \\ <div class="module {s} {s}"> \\ <h2><a class="decl" target="_blank" href="{s}{s}">{s}</a></h2> \\ <div class="module-inner"> , .{ subdir_class, hidden_class, source_url, self.name, self.name }); if (self.json_entry.description) |desc| { try writer.print("<p>{s}</p>", .{self.json_entry.description}); } inline for (.{"fields", "funcs", "types", "values"}) |n| { try writer.print("<span class=\"kind-{s}\">", .{n}); const this_list = @field(self, n).items; for (this_list) |decl, count| { try decl.htmlStringify(writer, self.json_entry.compact); if (self.json_entry.compact and count > 5) { try writer.print(" {} more...", .{this_list.len-5}); break; } } try writer.writeAll("</span>"); } try writer.writeAll("</div></div>"); // end of module } }; const DeclKind = enum { field, func, type, value }; const DeclInfo = struct { kind: DeclKind, name: []const u8, tag: std.zig.ast.Node.Tag, sub_cont_ty: ?[]const u8 = null, decl_info_lists: ?DeclInfoLists, src_line: usize, pub fn htmlStringify(self: DeclInfo, writer: anytype, compact: bool) std.fs.File.WriteError!void { // If this decl contains inner decls, enclose the lot in a container if (self.decl_info_lists != null) try writer.writeAll("<div class=\"inner-container\">"); var fn_parens = if (self.kind == .func) "()" else ""; var kind_str = switch (self.kind) { .field => "field", .func => "func", .type => "type", .value => "value", }; try writer.print("<a class=\"decl kind-{s}\" target=\"_blank\" href=\"{s}{s}#L{}\">{s}{s}</a> ", .{ kind_str, source_url, cur_file, self.src_line+1, // always one off - is it 0 indexed? self.name, fn_parens, }); if (self.decl_info_lists != null) { inline for (.{"fields", "funcs", "types", "values"}) |n| { const this_list = @field(self.decl_info_lists.?, n).items; for (this_list) |decl, count| { try decl.htmlStringify(writer, compact); if (compact and count > 5) { try writer.print(" {} more...", .{this_list.len}); break; } } } try writer.writeAll("</div>"); // End container started way up at the top of fn } } }; var cur_file: []const u8 = undefined; fn removeTrailingSlash(n: [:0]u8) []u8 { if (std.mem.endsWith(u8, n, "/")) return n[0 .. n.len - 1]; return n; } pub fn main() !void { // var general_pa = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = 8 }){}; // const alloc = &general_pa.allocator; var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const alloc = &arena.allocator; const args = try std.process.argsAlloc(alloc); if (args.len < 2) fatal("The first argument must be a directory path."); const dirname = removeTrailingSlash(args[1]); var walker = std.fs.walkPath(alloc, dirname) catch |e| fatalArgs("Could not read directory: {s}: {}", .{ dirname, e }); // defer walker.deinit(); // Store the decl lists for each file mapped by filename var decls_by_filename = std.StringHashMap(DeclInfoLists).init(alloc); // Read json data const info_json = std.fs.cwd().readFileAlloc(alloc, in_fname_entries, max_read_filesize) catch |e| { fatalArgs("Could not read file {s}: {}", .{in_fname_entries, e}); }; // Now parse those tasty JSON potatoes. // Used ziglearn to understand how to parse into a data type: // https://ziglearn.org/chapter-2/#json // Used laddercraft to understand how to loop over the token stream: // https://github.com/wozeparrot/laddercraft/blob/master/utils/item_registry_generator.zig // Store the entries hashed by filename (corresponds with library file names) var json_entries = std.StringHashMap(JsonEntry).init(alloc); var json_stream = std.json.TokenStream.init(info_json); const context = .{.allocator = alloc}; std.debug.assert((try json_stream.next()).? == .ObjectBegin); // '{' <--- at the very beginning while (true) { // So, ideally we could peek at the next token to see if it's a string // or an ObjectEnd '}' (expected when we're out of entries). But for // this kind of one-off utility, let's just assume the inability to // parse another entry means we've come to the end. const my_name = std.json.parse([]const u8, &json_stream, context) catch break; var my_value = try std.json.parse(JsonEntry, &json_stream, context); my_value.name = my_name; try json_entries.put(my_name, my_value); // my_value.print(); } // std.debug.print("Read {} entries from {s}\n", .{json_entries.count(), in_fname_entries}); // Terminal progress indicator var progress: std.Progress = .{}; var progress_main = try progress.start("", 0); progress_main.activate(); defer progress_main.end(); var progress_analysis = progress_main.start("Reading library...", 0); progress_analysis.activate(); // So we can sort em var filenames = std.ArrayList([]const u8,).init(alloc); // defer filenames.deinit(); // Loop through files in input directory while (walker.next() catch |e| fatalArgs("Could not read next file: {}", .{e})) |entry| { if (!std.mem.endsWith(u8, entry.path, ".zig")) continue; // Chop the dirname + "/" from the front of the path const filename = entry.path[dirname.len+1..]; // Do we already have this one? if (decls_by_filename.contains(filename)) continue; const my_filename = try alloc.dupe(u8, filename); // Show current file as progress var progress_file = progress_analysis.start(filename, 1); progress_file.activate(); defer progress_file.end(); // Store decl lists in hashmap by filename and store filename itself var list = try getDeclsFromFile(alloc, entry.path); list.name = my_filename; if (json_entries.contains(my_filename) ) { list.json_entry = json_entries.get(my_filename).?; } try decls_by_filename.put(my_filename, list); try filenames.append(my_filename); } progress_analysis.end(); // Sort the filenames std.sort.sort( []const u8, filenames.items, {}, stringAsc); var progress_writing = progress_main.start("Writing HTML...", 0); progress_writing.activate(); defer progress_writing.end(); const output_file = std.fs.cwd().createFile(out_fname_html, .{ .read = true }) catch |e| fatalArgs("Could not open {s} for writing: {}", .{out_fname_html, e}); defer output_file.close(); const w = output_file.writer(); const html_head = std.fs.cwd().readFileAlloc(alloc, in_fname_head, max_read_filesize) catch |e| { fatalArgs("Could not read file {s}: {}", .{in_fname_head, e}); }; try w.writeAll(html_head); // std.debug.print("{s}\n", .{filenames.items}); for (filenames.items) |fname| { cur_file = fname; try decls_by_filename.get(fname).?.htmlStringify(w); } try w.writeAll("</div></body></html>"); } // Comparison function for filename string sorting pub fn stringAsc(context: void, lhs: []const u8, rhs: []const u8) bool { return std.mem.lessThan(u8, lhs, rhs); } fn getDeclsFromFile( alloc: *std.mem.Allocator, fname: []const u8, ) error{OutOfMemory}!DeclInfoLists { const zig_code = std.fs.cwd().readFileAlloc(alloc, fname, max_read_filesize) catch |e| { fatalArgs("Could not read lib source file {s}: {}", .{fname, e}); }; tree = std.zig.parse(alloc, zig_code) catch |e| { fatalArgs("Could not parse lib source file {s}: {}", .{ fname, e }); }; // defer tree.deinit(alloc); const decls = tree.rootDecls(); const decl_list = try getDeclInfoList(alloc, decls); return decl_list; } fn getDeclInfoList( alloc: *std.mem.Allocator, list_d: []const ast.Node.Index, ) error{OutOfMemory}!DeclInfoLists { const node_tags = tree.nodes.items(.tag); const node_datas = tree.nodes.items(.data); const main_tokens = tree.nodes.items(.main_token); const token_starts = tree.tokens.items(.start); var list = try DeclInfoLists.init(alloc); for (list_d) |member| { if (!util.isNodePublic(tree, member)) continue; const tag = node_tags[member]; // Tag // We're only concerned about these types. Looks like just // test and comptime being skipped currently, but an inclusion // list will be easier to keep current, maybe? if (tag != .local_var_decl and tag != .global_var_decl and tag != .simple_var_decl and tag != .aligned_var_decl and tag != .fn_proto and tag != .fn_proto_multi and tag != .fn_proto_one and tag != .fn_proto_simple and tag != .fn_decl and tag != .container_field and tag != .container_field_init and tag != .container_field_align and tag != .identifier and tag != .error_value) { continue; } const decl_addr = member; const q_ftoken = tree.firstToken(member); const q_ltoken = tree.lastToken(member); const q_start = token_starts[q_ftoken]; const q_end = token_starts[q_ltoken + 1]; var decl_info_lists: ?DeclInfoLists = null; var destination_list = &list.fields; var kind = DeclKind.field; if (tag == .fn_decl) { // We are only interested in "type functions" which // create and return a type. destination_list = &list.funcs; decl_info_lists = getFunctionDecls(alloc, decl_addr); kind = DeclKind.func; } if (tag == .global_var_decl or tag == .local_var_decl or tag == .simple_var_decl or tag == .aligned_var_decl) { destination_list = &list.values; kind = DeclKind.value; decl_info_lists = getVarDecls(alloc, decl_addr); if (decl_info_lists) |_| { destination_list = &list.types; kind = DeclKind.type; } } try destination_list.append(.{ .kind = kind, .decl_info_lists = decl_info_lists, .src_line = std.zig.findLineColumn(tree.source, q_start).line, .name = try alloc.dupe(u8, util.getDeclName(tree, member).?), .tag = tag, }); } return list; } fn getVarDecls(alloc: *std.mem.Allocator, decl_addr: ast.Node.Index) ?DeclInfoLists { const vardecl = util.varDecl(tree, decl_addr).?; const node_tags = tree.nodes.items(.tag); const node_datas = tree.nodes.items(.data); const main_tokens = tree.nodes.items(.main_token); const token_starts = tree.tokens.items(.start); const name_loc = vardecl.ast.mut_token + 1; const name = tree.tokenSlice(name_loc); const init = node_datas[decl_addr].rhs; const rhst = node_tags[init]; // we find if the var is a container, we dont wanna display the full thing if it is // then we recurse over it var buf: [2]ast.Node.Index = undefined; var cd = getContainer(node_datas[decl_addr].rhs, &buf); if (cd) |container_decl| { const offset = if (container_decl.ast.enum_token != null) if (rhst == .tagged_union_enum_tag or rhst == .tagged_union_enum_tag_trailing) @as(u32, 7) else @as(u32, 4) else @as(u32, 1); return getDeclInfoList(alloc, container_decl.ast.members) catch null; } return null; } fn getFunctionDecls(alloc: *std.mem.Allocator, decl_addr: ast.Node.Index) ?DeclInfoLists { const node_tags = tree.nodes.items(.tag); const node_datas = tree.nodes.items(.data); const main_tokens = tree.nodes.items(.main_token); const token_starts = tree.tokens.items(.start); const proto = node_datas[decl_addr].lhs; const block = node_datas[decl_addr].rhs; var params: [1]ast.Node.Index = undefined; const fn_proto = util.fnProto( tree, proto, &params, ).?; const sig = util.getFunctionSignature(tree, fn_proto); var sub_cont_ty: ?[]const u8 = null; return if (util.isTypeFunction(tree, fn_proto)) blk: { const ret = util.findReturnStatement(tree, fn_proto, block) orelse break :blk null; if (node_datas[ret].lhs == 0) break :blk null; var buf: [2]ast.Node.Index = undefined; const container = getContainer(node_datas[ret].lhs, &buf) orelse break :blk null; const offset = if (container.ast.enum_token != null) if (node_tags[node_datas[ret].lhs] == .tagged_union_enum_tag or node_tags[node_datas[ret].lhs] == .tagged_union_enum_tag_trailing) @as(u32, 7) else @as(u32, 4) else @as(u32, 1); sub_cont_ty = tree.source[token_starts[tree.firstToken(node_datas[ret].lhs)]..token_starts[ main_tokens[node_datas[ret].lhs] + offset ]]; break :blk getDeclInfoList(alloc, container.ast.members) catch |e| { return null; }; } else null; } fn getContainer(decl_addr: ast.Node.Index, buf: *[2]ast.Node.Index) ?ast.full.ContainerDecl { const node_tags = tree.nodes.items(.tag); const node_datas = tree.nodes.items(.data); const main_tokens = tree.nodes.items(.main_token); const token_starts = tree.tokens.items(.start); const rhst = node_tags[decl_addr]; // Recurse over it return if (rhst == .container_decl or rhst == .container_decl_trailing) tree.containerDecl(decl_addr) else if (rhst == .container_decl_arg or rhst == .container_decl_arg_trailing) tree.containerDeclArg(decl_addr) else if (rhst == .container_decl_two or rhst == .container_decl_two_trailing) blk: { break :blk tree.containerDeclTwo(buf, decl_addr); } else if (rhst == .tagged_union or rhst == .tagged_union_trailing) tree.taggedUnion(decl_addr) else if (rhst == .tagged_union_two or rhst == .tagged_union_two_trailing) blk: { break :blk tree.taggedUnionTwo(buf, decl_addr); } else if (rhst == .tagged_union_enum_tag or rhst == .tagged_union_enum_tag_trailing) tree.taggedUnionEnumTag(decl_addr) else null; } fn fatal(s: []const u8) noreturn { std.log.emerg("{s}\n", .{s}); std.process.exit(1); } fn fatalArgs(comptime s: []const u8, args: anytype) noreturn { std.log.emerg(s, args); std.process.exit(1); }
const std = @import("std"); const SDL = @import("sdl2"); const target_os = @import("builtin").os; pub fn main() !void { try SDL.init(.{ .video = true, .events = true, .audio = true, }); defer SDL.quit(); var window = try SDL.createWindow( "SDL.zig Basic Demo", .{ .centered = {} }, .{ .centered = {} }, 640, 480, .{ .shown = true }, ); defer window.destroy(); var renderer = try SDL.createRenderer(window, null, .{ .accelerated = true }); defer renderer.destroy(); mainLoop: while (true) { while (SDL.pollEvent()) |ev| { switch (ev) { .quit => { break :mainLoop; }, .key_down => |key| { switch (key.scancode) { .escape => break :mainLoop, else => std.log.info("key pressed: {}\n", .{key.scancode}), } }, else => {}, } } try renderer.setColorRGB(0, 0, 0); try renderer.clear(); try renderer.setColor(SDL.Color.parse("#F7A41D") catch unreachable); try renderer.drawRect(SDL.Rectangle{ .x = 270, .y = 215, .width = 100, .height = 50, }); if (target_os.tag != .linux) { // Ubuntu CI doesn't have this function available yet try renderer.drawGeometry( null, &[_]SDL.Vertex{ .{ .position = .{ .x = 400, .y = 150 }, .color = SDL.Color.rgb(255, 0, 0), }, .{ .position = .{ .x = 350, .y = 200 }, .color = SDL.Color.rgb(0, 0, 255), }, .{ .position = .{ .x = 450, .y = 200 }, .color = SDL.Color.rgb(0, 255, 0), }, }, null, ); } renderer.present(); } }
const std = @import("std"); pub fn __truncsfhf2(a: f32) callconv(.C) u16 { return @bitCast(u16, @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f16, f32, a })); } pub fn __truncdfhf2(a: f64) callconv(.C) u16 { return @bitCast(u16, @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f16, f64, a })); } pub fn __trunctfsf2(a: f128) callconv(.C) f32 { return @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f32, f128, a }); } pub fn __trunctfdf2(a: f128) callconv(.C) f64 { return @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f64, f128, a }); } pub fn __truncdfsf2(a: f64) callconv(.C) f32 { return @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f32, f64, a }); } pub fn __aeabi_d2f(a: f64) callconv(.AAPCS) f32 { @setRuntimeSafety(false); return @call(.{ .modifier = .always_inline }, __truncdfsf2, .{a}); } pub fn __aeabi_d2h(a: f64) callconv(.AAPCS) u16 { @setRuntimeSafety(false); return @call(.{ .modifier = .always_inline }, __truncdfhf2, .{a}); } pub fn __aeabi_f2h(a: f32) callconv(.AAPCS) u16 { @setRuntimeSafety(false); return @call(.{ .modifier = .always_inline }, __truncsfhf2, .{a}); } fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t { const src_rep_t = std.meta.IntType(false, @typeInfo(src_t).Float.bits); const dst_rep_t = std.meta.IntType(false, @typeInfo(dst_t).Float.bits); const srcSigBits = std.math.floatMantissaBits(src_t); const dstSigBits = std.math.floatMantissaBits(dst_t); const SrcShift = std.math.Log2Int(src_rep_t); const DstShift = std.math.Log2Int(dst_rep_t); // Various constants whose values follow from the type parameters. // Any reasonable optimizer will fold and propagate all of these. const srcBits = src_t.bit_count; const srcExpBits = srcBits - srcSigBits - 1; const srcInfExp = (1 << srcExpBits) - 1; const srcExpBias = srcInfExp >> 1; const srcMinNormal = 1 << srcSigBits; const srcSignificandMask = srcMinNormal - 1; const srcInfinity = srcInfExp << srcSigBits; const srcSignMask = 1 << (srcSigBits + srcExpBits); const srcAbsMask = srcSignMask - 1; const roundMask = (1 << (srcSigBits - dstSigBits)) - 1; const halfway = 1 << (srcSigBits - dstSigBits - 1); const srcQNaN = 1 << (srcSigBits - 1); const srcNaNCode = srcQNaN - 1; const dstBits = dst_t.bit_count; const dstExpBits = dstBits - dstSigBits - 1; const dstInfExp = (1 << dstExpBits) - 1; const dstExpBias = dstInfExp >> 1; const underflowExponent = srcExpBias + 1 - dstExpBias; const overflowExponent = srcExpBias + dstInfExp - dstExpBias; const underflow = underflowExponent << srcSigBits; const overflow = overflowExponent << srcSigBits; const dstQNaN = 1 << (dstSigBits - 1); const dstNaNCode = dstQNaN - 1; // Break a into a sign and representation of the absolute value const aRep: src_rep_t = @bitCast(src_rep_t, a); const aAbs: src_rep_t = aRep & srcAbsMask; const sign: src_rep_t = aRep & srcSignMask; var absResult: dst_rep_t = undefined; if (aAbs -% underflow < aAbs -% overflow) { // The exponent of a is within the range of normal numbers in the // destination format. We can convert by simply right-shifting with // rounding and adjusting the exponent. absResult = @truncate(dst_rep_t, aAbs >> (srcSigBits - dstSigBits)); absResult -%= @as(dst_rep_t, srcExpBias - dstExpBias) << dstSigBits; const roundBits: src_rep_t = aAbs & roundMask; if (roundBits > halfway) { // Round to nearest absResult += 1; } else if (roundBits == halfway) { // Ties to even absResult += absResult & 1; } } else if (aAbs > srcInfinity) { // a is NaN. // Conjure the result by beginning with infinity, setting the qNaN // bit and inserting the (truncated) trailing NaN field. absResult = @intCast(dst_rep_t, dstInfExp) << dstSigBits; absResult |= dstQNaN; absResult |= @intCast(dst_rep_t, ((aAbs & srcNaNCode) >> (srcSigBits - dstSigBits)) & dstNaNCode); } else if (aAbs >= overflow) { // a overflows to infinity. absResult = @intCast(dst_rep_t, dstInfExp) << dstSigBits; } else { // a underflows on conversion to the destination type or is an exact // zero. The result may be a denormal or zero. Extract the exponent // to get the shift amount for the denormalization. const aExp = @intCast(u32, aAbs >> srcSigBits); const shift = @intCast(u32, srcExpBias - dstExpBias - aExp + 1); const significand: src_rep_t = (aRep & srcSignificandMask) | srcMinNormal; // Right shift by the denormalization amount with sticky. if (shift > srcSigBits) { absResult = 0; } else { const sticky: src_rep_t = significand << @intCast(SrcShift, srcBits - shift); const denormalizedSignificand: src_rep_t = significand >> @intCast(SrcShift, shift) | sticky; absResult = @intCast(dst_rep_t, denormalizedSignificand >> (srcSigBits - dstSigBits)); const roundBits: src_rep_t = denormalizedSignificand & roundMask; if (roundBits > halfway) { // Round to nearest absResult += 1; } else if (roundBits == halfway) { // Ties to even absResult += absResult & 1; } } } const result: dst_rep_t align(@alignOf(dst_t)) = absResult | @truncate(dst_rep_t, sign >> @intCast(SrcShift, srcBits - dstBits)); return @bitCast(dst_t, result); } test "import truncXfYf2" { _ = @import("truncXfYf2_test.zig"); }
const std = @import("std"); const builtin = @import("builtin"); const TypeInfo = std.builtin.TypeInfo; const testing = std.testing; fn testTypes(comptime types: []const type) !void { inline for (types) |testType| { try testing.expect(testType == @Type(@typeInfo(testType))); } } test "Type.MetaType" { try testing.expect(type == @Type(TypeInfo{ .Type = undefined })); try testTypes(&[_]type{type}); } test "Type.Void" { try testing.expect(void == @Type(TypeInfo{ .Void = undefined })); try testTypes(&[_]type{void}); } test "Type.Bool" { try testing.expect(bool == @Type(TypeInfo{ .Bool = undefined })); try testTypes(&[_]type{bool}); } test "Type.NoReturn" { try testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined })); try testTypes(&[_]type{noreturn}); } test "Type.Int" { try testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } })); try testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } })); try testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } })); try testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } })); try testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } })); try testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } })); try testTypes(&[_]type{ u8, u32, i64 }); } test "Type.ComptimeFloat" { try testTypes(&[_]type{comptime_float}); } test "Type.ComptimeInt" { try testTypes(&[_]type{comptime_int}); } test "Type.Undefined" { try testTypes(&[_]type{@TypeOf(undefined)}); } test "Type.Null" { try testTypes(&[_]type{@TypeOf(null)}); } test "Type.EnumLiteral" { try testTypes(&[_]type{ @TypeOf(.Dummy), }); } test "Type.Pointer" { try testTypes(&[_]type{ // One Value Pointer Types *u8, *const u8, *volatile u8, *const volatile u8, *align(4) u8, *align(4) const u8, *align(4) volatile u8, *align(4) const volatile u8, *align(8) u8, *align(8) const u8, *align(8) volatile u8, *align(8) const volatile u8, *allowzero u8, *allowzero const u8, *allowzero volatile u8, *allowzero const volatile u8, *allowzero align(4) u8, *allowzero align(4) const u8, *allowzero align(4) volatile u8, *allowzero align(4) const volatile u8, // Many Values Pointer Types [*]u8, [*]const u8, [*]volatile u8, [*]const volatile u8, [*]align(4) u8, [*]align(4) const u8, [*]align(4) volatile u8, [*]align(4) const volatile u8, [*]align(8) u8, [*]align(8) const u8, [*]align(8) volatile u8, [*]align(8) const volatile u8, [*]allowzero u8, [*]allowzero const u8, [*]allowzero volatile u8, [*]allowzero const volatile u8, [*]allowzero align(4) u8, [*]allowzero align(4) const u8, [*]allowzero align(4) volatile u8, [*]allowzero align(4) const volatile u8, // Slice Types []u8, []const u8, []volatile u8, []const volatile u8, []align(4) u8, []align(4) const u8, []align(4) volatile u8, []align(4) const volatile u8, []align(8) u8, []align(8) const u8, []align(8) volatile u8, []align(8) const volatile u8, []allowzero u8, []allowzero const u8, []allowzero volatile u8, []allowzero const volatile u8, []allowzero align(4) u8, []allowzero align(4) const u8, []allowzero align(4) volatile u8, []allowzero align(4) const volatile u8, // C Pointer Types [*c]u8, [*c]const u8, [*c]volatile u8, [*c]const volatile u8, [*c]align(4) u8, [*c]align(4) const u8, [*c]align(4) volatile u8, [*c]align(4) const volatile u8, [*c]align(8) u8, [*c]align(8) const u8, [*c]align(8) volatile u8, [*c]align(8) const volatile u8, }); }
const arduino = @import("arduino"); const gpio = arduino.gpio; // Necessary, and has the side effect of pulling in the needed _start method pub const panic = arduino.start.panicLogUart; // display 4 digits // using a kyx-3942bg and a 74hc595 shift register // pins: // 74hc595 pins in: const SHIFTREG_DS = 11; // data const SHIFTREG_STCP = 12; // shift-in clock (up edge) const SHIFTREG_SHCP = 10; // register storage clock (up edge) // out pins Q7..Q0 -> 3942bg in: 16 13 3 5 11 15 7 14 // other 3942bg pins const LED_1 = 4; const LED_2 = 5; const LED_6 = 6; const LED_8 = 7; fn shiftOut(comptime DS_pin: comptime_int, comptime SHC_pin: comptime_int, comptime STC_pin: comptime_int, value: u8) void { gpio.setPin(STC_pin, .low); var v = value; var i: u8 = 0; while (i < 8) : (i += 1) { if (v & 0b1000_0000 != 0) gpio.setPin(DS_pin, .high) else gpio.setPin(DS_pin, .low); gpio.setPin(SHC_pin, .high); v <<= 1; gpio.setPin(SHC_pin, .low); } gpio.setPin(STC_pin, .high); } fn setLCDDigit(comptime sel_pin: comptime_int, digit: u4) void { const digit_segments = [_]u8{ 0b0_0111111, // 0 0b0_0000110, // 1 0b0_1011011, // 2 0b0_1001111, // 3 0b0_1100110, // 4 0b0_1101101, // 5 0b0_1111101, // 6 0b0_0000111, // 7 0b0_1111111, // 8 0b0_1101111, // 9 0b0_1110111, // A 0b0_1111100, // b 0b0_0111001, // C 0b0_1011110, // d 0b0_1111001, // E 0b0_1110001, // F }; shiftOut(SHIFTREG_DS, SHIFTREG_SHCP, SHIFTREG_STCP, ~digit_segments[digit]); gpio.setPin(sel_pin, .high); arduino.cpu.delayMicroseconds(250); gpio.setPin(sel_pin, .low); } pub fn main() void { arduino.uart.init(arduino.cpu.CPU_FREQ, 115200); gpio.setMode(SHIFTREG_DS, .output); gpio.setMode(SHIFTREG_STCP, .output); gpio.setMode(SHIFTREG_SHCP, .output); gpio.setMode(LED_1, .output); gpio.setMode(LED_2, .output); gpio.setMode(LED_6, .output); gpio.setMode(LED_8, .output); // var counter:u16=0; // while (true) { // setLCDDigit(LED_1, 0); // setLCDDigit(LED_2, 0); // setLCDDigit(LED_6, 0); // setLCDDigit(LED_8, @intCast(u4, (counter>>8)&0x0F)); // counter +%= 1; // } // set 4 digits LCD to the analog pin A0 value: const A0 = 14; gpio.setMode(A0, .input); const REDLED = 9; gpio.setMode(REDLED, .output_analog); gpio.setPin(REDLED, .high); while (true) { const potar = gpio.getPinAnalog(0); setLCDDigit(LED_8, @intCast(u4, (potar >> 0) & 0x000F)); setLCDDigit(LED_6, @intCast(u4, (potar >> 4) & 0x000F)); setLCDDigit(LED_2, @intCast(u4, (potar >> 8) & 0x000F)); setLCDDigit(LED_1, @intCast(u4, (potar >> 12) & 0x000F)); gpio.setPinAnalog(REDLED, @intCast(u8, potar & 0xFF)); } }
//! Specifically shared between both build.zig and main app code. pub const ZigVersion = enum { master, @"0.7.0", @"0.7.1", @"0.8.0", @"0.8.1", };
fn bad_eql_1(a: []u8, b: []u8) bool { return a == b; } const EnumWithData = union(enum) { One: void, Two: i32, }; fn bad_eql_2(a: *const EnumWithData, b: *const EnumWithData) bool { return a.* == b.*; } export fn entry1() usize { return @sizeOf(@TypeOf(bad_eql_1)); } export fn entry2() usize { return @sizeOf(@TypeOf(bad_eql_2)); } // error // backend=stage1 // target=native // // tmp.zig:2:14: error: operator not allowed for type '[]u8' // tmp.zig:9:16: error: operator not allowed for type 'EnumWithData'
// SPDX-License-Identifier: MIT // Copyright (c) 2015-2020 Zig Contributors // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const uefi = @import("std").os.uefi; const Event = uefi.Event; const Guid = uefi.Guid; const InputKey = uefi.protocols.InputKey; const Status = uefi.Status; /// Character input devices, e.g. Keyboard pub const SimpleTextInputProtocol = extern struct { _reset: fn (*const SimpleTextInputProtocol, bool) callconv(.C) usize, _read_key_stroke: fn (*const SimpleTextInputProtocol, *InputKey) callconv(.C) Status, wait_for_key: Event, /// Resets the input device hardware. pub fn reset(self: *const SimpleTextInputProtocol, verify: bool) Status { return self._reset(self, verify); } /// Reads the next keystroke from the input device. pub fn readKeyStroke(self: *const SimpleTextInputProtocol, input_key: *InputKey) Status { return self._read_key_stroke(self, input_key); } pub const guid align(8) = Guid{ .time_low = 0x387477c1, .time_mid = 0x69c7, .time_high_and_version = 0x11d2, .clock_seq_high_and_reserved = 0x8e, .clock_seq_low = 0x39, .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b }, }; };
//! We do this instead of @cImport because the self-hosted compiler is easier //! to bootstrap if it does not depend on translate-c. /// Do not compare directly to .True, use toBool() instead. pub const Bool = enum(c_int) { False, True, _, pub fn fromBool(b: bool) Bool { return @intToEnum(Bool, @boolToInt(b)); } pub fn toBool(b: Bool) bool { return b != .False; } }; pub const AttributeIndex = c_uint; /// Make sure to use the *InContext functions instead of the global ones. pub const Context = opaque { pub const create = LLVMContextCreate; extern fn LLVMContextCreate() *const Context; pub const dispose = LLVMContextDispose; extern fn LLVMContextDispose(C: *const Context) void; pub const createEnumAttribute = LLVMCreateEnumAttribute; extern fn LLVMCreateEnumAttribute(*const Context, KindID: c_uint, Val: u64) *const Attribute; pub const createStringAttribute = LLVMCreateStringAttribute; extern fn LLVMCreateStringAttribute(*const Context, Key: [*]const u8, Key_Len: c_uint, Value: [*]const u8, Value_Len: c_uint) *const Attribute; pub const intType = LLVMIntTypeInContext; extern fn LLVMIntTypeInContext(C: *const Context, NumBits: c_uint) *const Type; pub const halfType = LLVMHalfTypeInContext; extern fn LLVMHalfTypeInContext(C: *const Context) *const Type; pub const floatType = LLVMFloatTypeInContext; extern fn LLVMFloatTypeInContext(C: *const Context) *const Type; pub const doubleType = LLVMDoubleTypeInContext; extern fn LLVMDoubleTypeInContext(C: *const Context) *const Type; pub const x86FP80Type = LLVMX86FP80TypeInContext; extern fn LLVMX86FP80TypeInContext(C: *const Context) *const Type; pub const fp128Type = LLVMFP128TypeInContext; extern fn LLVMFP128TypeInContext(C: *const Context) *const Type; pub const voidType = LLVMVoidTypeInContext; extern fn LLVMVoidTypeInContext(C: *const Context) *const Type; pub const structType = LLVMStructTypeInContext; extern fn LLVMStructTypeInContext( C: *const Context, ElementTypes: [*]const *const Type, ElementCount: c_uint, Packed: Bool, ) *const Type; const structCreateNamed = LLVMStructCreateNamed; extern fn LLVMStructCreateNamed(C: *const Context, Name: [*:0]const u8) *const Type; pub const constString = LLVMConstStringInContext; extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) *const Value; pub const constStruct = LLVMConstStructInContext; extern fn LLVMConstStructInContext( C: *const Context, ConstantVals: [*]const *const Value, Count: c_uint, Packed: Bool, ) *const Value; pub const createBasicBlock = LLVMCreateBasicBlockInContext; extern fn LLVMCreateBasicBlockInContext(C: *const Context, Name: [*:0]const u8) *const BasicBlock; pub const appendBasicBlock = LLVMAppendBasicBlockInContext; extern fn LLVMAppendBasicBlockInContext(C: *const Context, Fn: *const Value, Name: [*:0]const u8) *const BasicBlock; pub const createBuilder = LLVMCreateBuilderInContext; extern fn LLVMCreateBuilderInContext(C: *const Context) *const Builder; }; pub const Value = opaque { pub const addAttributeAtIndex = LLVMAddAttributeAtIndex; extern fn LLVMAddAttributeAtIndex(*const Value, Idx: AttributeIndex, A: *const Attribute) void; pub const removeEnumAttributeAtIndex = LLVMRemoveEnumAttributeAtIndex; extern fn LLVMRemoveEnumAttributeAtIndex(F: *const Value, Idx: AttributeIndex, KindID: c_uint) void; pub const getFirstBasicBlock = LLVMGetFirstBasicBlock; extern fn LLVMGetFirstBasicBlock(Fn: *const Value) ?*const BasicBlock; pub const appendExistingBasicBlock = LLVMAppendExistingBasicBlock; extern fn LLVMAppendExistingBasicBlock(Fn: *const Value, BB: *const BasicBlock) void; pub const addIncoming = LLVMAddIncoming; extern fn LLVMAddIncoming( PhiNode: *const Value, IncomingValues: [*]const *const Value, IncomingBlocks: [*]const *const BasicBlock, Count: c_uint, ) void; pub const getNextInstruction = LLVMGetNextInstruction; extern fn LLVMGetNextInstruction(Inst: *const Value) ?*const Value; pub const typeOf = LLVMTypeOf; extern fn LLVMTypeOf(Val: *const Value) *const Type; pub const setGlobalConstant = LLVMSetGlobalConstant; extern fn LLVMSetGlobalConstant(GlobalVar: *const Value, IsConstant: Bool) void; pub const setLinkage = LLVMSetLinkage; extern fn LLVMSetLinkage(Global: *const Value, Linkage: Linkage) void; pub const setUnnamedAddr = LLVMSetUnnamedAddr; extern fn LLVMSetUnnamedAddr(Global: *const Value, HasUnnamedAddr: Bool) void; pub const deleteGlobal = LLVMDeleteGlobal; extern fn LLVMDeleteGlobal(GlobalVar: *const Value) void; pub const getNextGlobalAlias = LLVMGetNextGlobalAlias; extern fn LLVMGetNextGlobalAlias(GA: *const Value) *const Value; pub const getAliasee = LLVMAliasGetAliasee; extern fn LLVMAliasGetAliasee(Alias: *const Value) *const Value; pub const setAliasee = LLVMAliasSetAliasee; extern fn LLVMAliasSetAliasee(Alias: *const Value, Aliasee: *const Value) void; pub const constInBoundsGEP = LLVMConstInBoundsGEP; extern fn LLVMConstInBoundsGEP( ConstantVal: *const Value, ConstantIndices: [*]const *const Value, NumIndices: c_uint, ) *const Value; pub const constBitCast = LLVMConstBitCast; extern fn LLVMConstBitCast(ConstantVal: *const Value, ToType: *const Type) *const Value; pub const constIntToPtr = LLVMConstIntToPtr; extern fn LLVMConstIntToPtr(ConstantVal: *const Value, ToType: *const Type) *const Value; pub const constPtrToInt = LLVMConstPtrToInt; extern fn LLVMConstPtrToInt(ConstantVal: *const Value, ToType: *const Type) *const Value; pub const setWeak = LLVMSetWeak; extern fn LLVMSetWeak(CmpXchgInst: *const Value, IsWeak: Bool) void; pub const setOrdering = LLVMSetOrdering; extern fn LLVMSetOrdering(MemoryAccessInst: *const Value, Ordering: AtomicOrdering) void; pub const setVolatile = LLVMSetVolatile; extern fn LLVMSetVolatile(MemoryAccessInst: *const Value, IsVolatile: Bool) void; pub const setAlignment = LLVMSetAlignment; extern fn LLVMSetAlignment(V: *const Value, Bytes: c_uint) void; pub const getFunctionCallConv = LLVMGetFunctionCallConv; extern fn LLVMGetFunctionCallConv(Fn: *const Value) CallConv; pub const setFunctionCallConv = LLVMSetFunctionCallConv; extern fn LLVMSetFunctionCallConv(Fn: *const Value, CC: CallConv) void; pub const setValueName = LLVMSetValueName; extern fn LLVMSetValueName(Val: *const Value, Name: [*:0]const u8) void; pub const setValueName2 = LLVMSetValueName2; extern fn LLVMSetValueName2(Val: *const Value, Name: [*]const u8, NameLen: usize) void; pub const deleteFunction = LLVMDeleteFunction; extern fn LLVMDeleteFunction(Fn: *const Value) void; pub const addSretAttr = ZigLLVMAddSretAttr; extern fn ZigLLVMAddSretAttr(fn_ref: *const Value, ArgNo: c_uint, type_val: *const Type) void; pub const setCallSret = ZigLLVMSetCallSret; extern fn ZigLLVMSetCallSret(Call: *const Value, return_type: *const Type) void; pub const getParam = LLVMGetParam; extern fn LLVMGetParam(Fn: *const Value, Index: c_uint) *const Value; pub const setInitializer = LLVMSetInitializer; extern fn LLVMSetInitializer(GlobalVar: *const Value, ConstantVal: *const Value) void; pub const addCase = LLVMAddCase; extern fn LLVMAddCase(Switch: *const Value, OnVal: *const Value, Dest: *const BasicBlock) void; }; pub const Type = opaque { pub const constNull = LLVMConstNull; extern fn LLVMConstNull(Ty: *const Type) *const Value; pub const constAllOnes = LLVMConstAllOnes; extern fn LLVMConstAllOnes(Ty: *const Type) *const Value; pub const constInt = LLVMConstInt; extern fn LLVMConstInt(IntTy: *const Type, N: c_ulonglong, SignExtend: Bool) *const Value; pub const constIntOfArbitraryPrecision = LLVMConstIntOfArbitraryPrecision; extern fn LLVMConstIntOfArbitraryPrecision(IntTy: *const Type, NumWords: c_uint, Words: [*]const u64) *const Value; pub const constReal = LLVMConstReal; extern fn LLVMConstReal(RealTy: *const Type, N: f64) *const Value; pub const constArray = LLVMConstArray; extern fn LLVMConstArray(ElementTy: *const Type, ConstantVals: [*]const *const Value, Length: c_uint) *const Value; pub const constNamedStruct = LLVMConstNamedStruct; extern fn LLVMConstNamedStruct( StructTy: *const Type, ConstantVals: [*]const *const Value, Count: c_uint, ) *const Value; pub const getUndef = LLVMGetUndef; extern fn LLVMGetUndef(Ty: *const Type) *const Value; pub const pointerType = LLVMPointerType; extern fn LLVMPointerType(ElementType: *const Type, AddressSpace: c_uint) *const Type; pub const arrayType = LLVMArrayType; extern fn LLVMArrayType(ElementType: *const Type, ElementCount: c_uint) *const Type; pub const vectorType = LLVMVectorType; extern fn LLVMVectorType(ElementType: *const Type, ElementCount: c_uint) *const Type; pub const structSetBody = LLVMStructSetBody; extern fn LLVMStructSetBody( StructTy: *const Type, ElementTypes: [*]*const Type, ElementCount: c_uint, Packed: Bool, ) void; pub const structGetTypeAtIndex = LLVMStructGetTypeAtIndex; extern fn LLVMStructGetTypeAtIndex(StructTy: *const Type, i: c_uint) *const Type; pub const getTypeKind = LLVMGetTypeKind; extern fn LLVMGetTypeKind(Ty: *const Type) TypeKind; pub const getElementType = LLVMGetElementType; extern fn LLVMGetElementType(Ty: *const Type) *const Type; }; pub const Module = opaque { pub const createWithName = LLVMModuleCreateWithNameInContext; extern fn LLVMModuleCreateWithNameInContext(ModuleID: [*:0]const u8, C: *const Context) *const Module; pub const dispose = LLVMDisposeModule; extern fn LLVMDisposeModule(*const Module) void; pub const verify = LLVMVerifyModule; extern fn LLVMVerifyModule(*const Module, Action: VerifierFailureAction, OutMessage: *[*:0]const u8) Bool; pub const setModuleDataLayout = LLVMSetModuleDataLayout; extern fn LLVMSetModuleDataLayout(*const Module, *const TargetData) void; pub const addFunction = LLVMAddFunction; extern fn LLVMAddFunction(*const Module, Name: [*:0]const u8, FunctionTy: *const Type) *const Value; pub const addFunctionInAddressSpace = ZigLLVMAddFunctionInAddressSpace; extern fn ZigLLVMAddFunctionInAddressSpace(*const Module, Name: [*:0]const u8, FunctionTy: *const Type, AddressSpace: c_uint) *const Value; pub const getNamedFunction = LLVMGetNamedFunction; extern fn LLVMGetNamedFunction(*const Module, Name: [*:0]const u8) ?*const Value; pub const getIntrinsicDeclaration = LLVMGetIntrinsicDeclaration; extern fn LLVMGetIntrinsicDeclaration(Mod: *const Module, ID: c_uint, ParamTypes: ?[*]*const Type, ParamCount: usize) *const Value; pub const printToString = LLVMPrintModuleToString; extern fn LLVMPrintModuleToString(*const Module) [*:0]const u8; pub const addGlobal = LLVMAddGlobal; extern fn LLVMAddGlobal(M: *const Module, Ty: *const Type, Name: [*:0]const u8) *const Value; pub const addGlobalInAddressSpace = LLVMAddGlobalInAddressSpace; extern fn LLVMAddGlobalInAddressSpace(M: *const Module, Ty: *const Type, Name: [*:0]const u8, AddressSpace: c_uint) *const Value; pub const getNamedGlobal = LLVMGetNamedGlobal; extern fn LLVMGetNamedGlobal(M: *const Module, Name: [*:0]const u8) ?*const Value; pub const dump = LLVMDumpModule; extern fn LLVMDumpModule(M: *const Module) void; pub const getFirstGlobalAlias = LLVMGetFirstGlobalAlias; extern fn LLVMGetFirstGlobalAlias(M: *const Module) *const Value; pub const getLastGlobalAlias = LLVMGetLastGlobalAlias; extern fn LLVMGetLastGlobalAlias(M: *const Module) *const Value; pub const addAlias = LLVMAddAlias; extern fn LLVMAddAlias( M: *const Module, Ty: *const Type, Aliasee: *const Value, Name: [*:0]const u8, ) *const Value; pub const getNamedGlobalAlias = LLVMGetNamedGlobalAlias; extern fn LLVMGetNamedGlobalAlias( M: *const Module, /// Empirically, LLVM will call strlen() on `Name` and so it /// must be both null terminated and also have `NameLen` set /// to the size. Name: [*:0]const u8, NameLen: usize, ) ?*const Value; }; pub const lookupIntrinsicID = LLVMLookupIntrinsicID; extern fn LLVMLookupIntrinsicID(Name: [*]const u8, NameLen: usize) c_uint; pub const disposeMessage = LLVMDisposeMessage; extern fn LLVMDisposeMessage(Message: [*:0]const u8) void; pub const VerifierFailureAction = enum(c_int) { AbortProcess, PrintMessage, ReturnStatus, }; pub const constNeg = LLVMConstNeg; extern fn LLVMConstNeg(ConstantVal: *const Value) *const Value; pub const constVector = LLVMConstVector; extern fn LLVMConstVector( ScalarConstantVals: [*]*const Value, Size: c_uint, ) *const Value; pub const getEnumAttributeKindForName = LLVMGetEnumAttributeKindForName; extern fn LLVMGetEnumAttributeKindForName(Name: [*]const u8, SLen: usize) c_uint; pub const getInlineAsm = LLVMGetInlineAsm; extern fn LLVMGetInlineAsm( Ty: *const Type, AsmString: [*]const u8, AsmStringSize: usize, Constraints: [*]const u8, ConstraintsSize: usize, HasSideEffects: Bool, IsAlignStack: Bool, Dialect: InlineAsmDialect, CanThrow: Bool, ) *const Value; pub const functionType = LLVMFunctionType; extern fn LLVMFunctionType( ReturnType: *const Type, ParamTypes: [*]const *const Type, ParamCount: c_uint, IsVarArg: Bool, ) *const Type; pub const InlineAsmDialect = enum(c_uint) { ATT, Intel }; pub const Attribute = opaque {}; pub const Builder = opaque { pub const dispose = LLVMDisposeBuilder; extern fn LLVMDisposeBuilder(Builder: *const Builder) void; pub const positionBuilder = LLVMPositionBuilder; extern fn LLVMPositionBuilder( Builder: *const Builder, Block: *const BasicBlock, Instr: *const Value, ) void; pub const positionBuilderAtEnd = LLVMPositionBuilderAtEnd; extern fn LLVMPositionBuilderAtEnd(Builder: *const Builder, Block: *const BasicBlock) void; pub const getInsertBlock = LLVMGetInsertBlock; extern fn LLVMGetInsertBlock(Builder: *const Builder) *const BasicBlock; pub const buildZExt = LLVMBuildZExt; extern fn LLVMBuildZExt( *const Builder, Value: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildSExt = LLVMBuildSExt; extern fn LLVMBuildSExt( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildCall = ZigLLVMBuildCall; extern fn ZigLLVMBuildCall( *const Builder, Fn: *const Value, Args: [*]const *const Value, NumArgs: c_uint, CC: CallConv, attr: CallAttr, Name: [*:0]const u8, ) *const Value; pub const buildRetVoid = LLVMBuildRetVoid; extern fn LLVMBuildRetVoid(*const Builder) *const Value; pub const buildRet = LLVMBuildRet; extern fn LLVMBuildRet(*const Builder, V: *const Value) *const Value; pub const buildUnreachable = LLVMBuildUnreachable; extern fn LLVMBuildUnreachable(*const Builder) *const Value; pub const buildAlloca = LLVMBuildAlloca; extern fn LLVMBuildAlloca(*const Builder, Ty: *const Type, Name: [*:0]const u8) *const Value; pub const buildStore = LLVMBuildStore; extern fn LLVMBuildStore(*const Builder, Val: *const Value, Ptr: *const Value) *const Value; pub const buildLoad = LLVMBuildLoad; extern fn LLVMBuildLoad(*const Builder, PointerVal: *const Value, Name: [*:0]const u8) *const Value; pub const buildNeg = LLVMBuildNeg; extern fn LLVMBuildNeg(*const Builder, V: *const Value, Name: [*:0]const u8) *const Value; pub const buildNot = LLVMBuildNot; extern fn LLVMBuildNot(*const Builder, V: *const Value, Name: [*:0]const u8) *const Value; pub const buildFAdd = LLVMBuildFAdd; extern fn LLVMBuildFAdd(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildAdd = LLVMBuildAdd; extern fn LLVMBuildAdd(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNSWAdd = LLVMBuildNSWAdd; extern fn LLVMBuildNSWAdd(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNUWAdd = LLVMBuildNUWAdd; extern fn LLVMBuildNUWAdd(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSAddSat = ZigLLVMBuildSAddSat; extern fn ZigLLVMBuildSAddSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildUAddSat = ZigLLVMBuildUAddSat; extern fn ZigLLVMBuildUAddSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildFSub = LLVMBuildFSub; extern fn LLVMBuildFSub(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSub = LLVMBuildSub; extern fn LLVMBuildSub(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNSWSub = LLVMBuildNSWSub; extern fn LLVMBuildNSWSub(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNUWSub = LLVMBuildNUWSub; extern fn LLVMBuildNUWSub(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSSubSat = ZigLLVMBuildSSubSat; extern fn ZigLLVMBuildSSubSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildUSubSat = ZigLLVMBuildUSubSat; extern fn ZigLLVMBuildUSubSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildFMul = LLVMBuildFMul; extern fn LLVMBuildFMul(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildMul = LLVMBuildMul; extern fn LLVMBuildMul(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNSWMul = LLVMBuildNSWMul; extern fn LLVMBuildNSWMul(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNUWMul = LLVMBuildNUWMul; extern fn LLVMBuildNUWMul(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSMulFixSat = ZigLLVMBuildSMulFixSat; extern fn ZigLLVMBuildSMulFixSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildUMulFixSat = ZigLLVMBuildUMulFixSat; extern fn ZigLLVMBuildUMulFixSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildUDiv = LLVMBuildUDiv; extern fn LLVMBuildUDiv(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSDiv = LLVMBuildSDiv; extern fn LLVMBuildSDiv(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildFDiv = LLVMBuildFDiv; extern fn LLVMBuildFDiv(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildURem = LLVMBuildURem; extern fn LLVMBuildURem(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSRem = LLVMBuildSRem; extern fn LLVMBuildSRem(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildFRem = LLVMBuildFRem; extern fn LLVMBuildFRem(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildAnd = LLVMBuildAnd; extern fn LLVMBuildAnd(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildLShr = LLVMBuildLShr; extern fn LLVMBuildLShr(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildAShr = LLVMBuildAShr; extern fn LLVMBuildAShr(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildShl = LLVMBuildShl; extern fn LLVMBuildShl(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNUWShl = ZigLLVMBuildNUWShl; extern fn ZigLLVMBuildNUWShl(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildNSWShl = ZigLLVMBuildNSWShl; extern fn ZigLLVMBuildNSWShl(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildSShlSat = ZigLLVMBuildSShlSat; extern fn ZigLLVMBuildSShlSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildUShlSat = ZigLLVMBuildUShlSat; extern fn ZigLLVMBuildUShlSat(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildOr = LLVMBuildOr; extern fn LLVMBuildOr(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildXor = LLVMBuildXor; extern fn LLVMBuildXor(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildIntCast2 = LLVMBuildIntCast2; extern fn LLVMBuildIntCast2(*const Builder, Val: *const Value, DestTy: *const Type, IsSigned: Bool, Name: [*:0]const u8) *const Value; pub const buildBitCast = LLVMBuildBitCast; extern fn LLVMBuildBitCast(*const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8) *const Value; pub const buildInBoundsGEP = LLVMBuildInBoundsGEP; extern fn LLVMBuildInBoundsGEP( B: *const Builder, Pointer: *const Value, Indices: [*]const *const Value, NumIndices: c_uint, Name: [*:0]const u8, ) *const Value; pub const buildInBoundsGEP2 = LLVMBuildInBoundsGEP2; extern fn LLVMBuildInBoundsGEP2( B: *const Builder, Ty: *const Type, Pointer: *const Value, Indices: [*]const *const Value, NumIndices: c_uint, Name: [*:0]const u8, ) *const Value; pub const buildICmp = LLVMBuildICmp; extern fn LLVMBuildICmp(*const Builder, Op: IntPredicate, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildFCmp = LLVMBuildFCmp; extern fn LLVMBuildFCmp(*const Builder, Op: RealPredicate, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildBr = LLVMBuildBr; extern fn LLVMBuildBr(*const Builder, Dest: *const BasicBlock) *const Value; pub const buildCondBr = LLVMBuildCondBr; extern fn LLVMBuildCondBr(*const Builder, If: *const Value, Then: *const BasicBlock, Else: *const BasicBlock) *const Value; pub const buildSwitch = LLVMBuildSwitch; extern fn LLVMBuildSwitch(*const Builder, V: *const Value, Else: *const BasicBlock, NumCases: c_uint) *const Value; pub const buildPhi = LLVMBuildPhi; extern fn LLVMBuildPhi(*const Builder, Ty: *const Type, Name: [*:0]const u8) *const Value; pub const buildExtractValue = LLVMBuildExtractValue; extern fn LLVMBuildExtractValue( *const Builder, AggVal: *const Value, Index: c_uint, Name: [*:0]const u8, ) *const Value; pub const buildExtractElement = LLVMBuildExtractElement; extern fn LLVMBuildExtractElement( *const Builder, VecVal: *const Value, Index: *const Value, Name: [*:0]const u8, ) *const Value; pub const buildInsertElement = LLVMBuildInsertElement; extern fn LLVMBuildInsertElement( *const Builder, VecVal: *const Value, EltVal: *const Value, Index: *const Value, Name: [*:0]const u8, ) *const Value; pub const buildPtrToInt = LLVMBuildPtrToInt; extern fn LLVMBuildPtrToInt( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildIntToPtr = LLVMBuildIntToPtr; extern fn LLVMBuildIntToPtr( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildStructGEP = LLVMBuildStructGEP; extern fn LLVMBuildStructGEP( B: *const Builder, Pointer: *const Value, Idx: c_uint, Name: [*:0]const u8, ) *const Value; pub const buildTrunc = LLVMBuildTrunc; extern fn LLVMBuildTrunc( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildInsertValue = LLVMBuildInsertValue; extern fn LLVMBuildInsertValue( *const Builder, AggVal: *const Value, EltVal: *const Value, Index: c_uint, Name: [*:0]const u8, ) *const Value; pub const buildAtomicCmpXchg = LLVMBuildAtomicCmpXchg; extern fn LLVMBuildAtomicCmpXchg( builder: *const Builder, ptr: *const Value, cmp: *const Value, new_val: *const Value, success_ordering: AtomicOrdering, failure_ordering: AtomicOrdering, is_single_threaded: Bool, ) *const Value; pub const buildSelect = LLVMBuildSelect; extern fn LLVMBuildSelect( *const Builder, If: *const Value, Then: *const Value, Else: *const Value, Name: [*:0]const u8, ) *const Value; pub const buildFence = LLVMBuildFence; extern fn LLVMBuildFence( B: *const Builder, ordering: AtomicOrdering, singleThread: Bool, Name: [*:0]const u8, ) *const Value; pub const buildAtomicRmw = LLVMBuildAtomicRMW; extern fn LLVMBuildAtomicRMW( B: *const Builder, op: AtomicRMWBinOp, PTR: *const Value, Val: *const Value, ordering: AtomicOrdering, singleThread: Bool, ) *const Value; pub const buildFPToUI = LLVMBuildFPToUI; extern fn LLVMBuildFPToUI( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildFPToSI = LLVMBuildFPToSI; extern fn LLVMBuildFPToSI( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildUIToFP = LLVMBuildUIToFP; extern fn LLVMBuildUIToFP( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildSIToFP = LLVMBuildSIToFP; extern fn LLVMBuildSIToFP( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildFPTrunc = LLVMBuildFPTrunc; extern fn LLVMBuildFPTrunc( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildFPExt = LLVMBuildFPExt; extern fn LLVMBuildFPExt( *const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8, ) *const Value; pub const buildMemSet = ZigLLVMBuildMemSet; extern fn ZigLLVMBuildMemSet( B: *const Builder, Ptr: *const Value, Val: *const Value, Len: *const Value, Align: c_uint, is_volatile: bool, ) *const Value; pub const buildMemCpy = ZigLLVMBuildMemCpy; extern fn ZigLLVMBuildMemCpy( B: *const Builder, Dst: *const Value, DstAlign: c_uint, Src: *const Value, SrcAlign: c_uint, Size: *const Value, is_volatile: bool, ) *const Value; pub const buildMaxNum = ZigLLVMBuildMaxNum; extern fn ZigLLVMBuildMaxNum(builder: *const Builder, LHS: *const Value, RHS: *const Value, name: [*:0]const u8) *const Value; pub const buildMinNum = ZigLLVMBuildMinNum; extern fn ZigLLVMBuildMinNum(builder: *const Builder, LHS: *const Value, RHS: *const Value, name: [*:0]const u8) *const Value; pub const buildUMax = ZigLLVMBuildUMax; extern fn ZigLLVMBuildUMax(builder: *const Builder, LHS: *const Value, RHS: *const Value, name: [*:0]const u8) *const Value; pub const buildUMin = ZigLLVMBuildUMin; extern fn ZigLLVMBuildUMin(builder: *const Builder, LHS: *const Value, RHS: *const Value, name: [*:0]const u8) *const Value; pub const buildSMax = ZigLLVMBuildSMax; extern fn ZigLLVMBuildSMax(builder: *const Builder, LHS: *const Value, RHS: *const Value, name: [*:0]const u8) *const Value; pub const buildSMin = ZigLLVMBuildSMin; extern fn ZigLLVMBuildSMin(builder: *const Builder, LHS: *const Value, RHS: *const Value, name: [*:0]const u8) *const Value; pub const buildExactUDiv = LLVMBuildExactUDiv; extern fn LLVMBuildExactUDiv(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildExactSDiv = LLVMBuildExactSDiv; extern fn LLVMBuildExactSDiv(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; }; pub const IntPredicate = enum(c_uint) { EQ = 32, NE = 33, UGT = 34, UGE = 35, ULT = 36, ULE = 37, SGT = 38, SGE = 39, SLT = 40, SLE = 41, }; pub const RealPredicate = enum(c_uint) { OEQ = 1, OGT = 2, OGE = 3, OLT = 4, OLE = 5, ONE = 6, ORD = 7, UNO = 8, UEQ = 9, UGT = 10, UGE = 11, ULT = 12, ULE = 13, UNE = 14, }; pub const BasicBlock = opaque { pub const deleteBasicBlock = LLVMDeleteBasicBlock; extern fn LLVMDeleteBasicBlock(BB: *const BasicBlock) void; pub const getFirstInstruction = LLVMGetFirstInstruction; extern fn LLVMGetFirstInstruction(BB: *const BasicBlock) ?*const Value; }; pub const TargetMachine = opaque { pub const create = ZigLLVMCreateTargetMachine; extern fn ZigLLVMCreateTargetMachine( T: *const Target, Triple: [*:0]const u8, CPU: ?[*:0]const u8, Features: ?[*:0]const u8, Level: CodeGenOptLevel, Reloc: RelocMode, CodeModel: CodeModel, function_sections: bool, float_abi: ABIType, abi_name: ?[*:0]const u8, ) *const TargetMachine; pub const dispose = LLVMDisposeTargetMachine; extern fn LLVMDisposeTargetMachine(T: *const TargetMachine) void; pub const emitToFile = ZigLLVMTargetMachineEmitToFile; extern fn ZigLLVMTargetMachineEmitToFile( T: *const TargetMachine, M: *const Module, ErrorMessage: *[*:0]const u8, is_debug: bool, is_small: bool, time_report: bool, tsan: bool, lto: bool, asm_filename: ?[*:0]const u8, bin_filename: ?[*:0]const u8, llvm_ir_filename: ?[*:0]const u8, bitcode_filename: ?[*:0]const u8, ) bool; pub const createTargetDataLayout = LLVMCreateTargetDataLayout; extern fn LLVMCreateTargetDataLayout(*const TargetMachine) *const TargetData; }; pub const TargetData = opaque { pub const dispose = LLVMDisposeTargetData; extern fn LLVMDisposeTargetData(*const TargetData) void; }; pub const CodeModel = enum(c_int) { Default, JITDefault, Tiny, Small, Kernel, Medium, Large, }; pub const CodeGenOptLevel = enum(c_int) { None, Less, Default, Aggressive, }; pub const RelocMode = enum(c_int) { Default, Static, PIC, DynamicNoPIC, ROPI, RWPI, ROPI_RWPI, }; pub const CodeGenFileType = enum(c_int) { AssemblyFile, ObjectFile, }; pub const ABIType = enum(c_int) { /// Target-specific (either soft or hard depending on triple, etc). Default, /// Soft float. Soft, // Hard float. Hard, }; pub const Target = opaque { pub const getFromTriple = LLVMGetTargetFromTriple; extern fn LLVMGetTargetFromTriple(Triple: [*:0]const u8, T: **const Target, ErrorMessage: *[*:0]const u8) Bool; }; pub extern fn LLVMInitializeAArch64TargetInfo() void; pub extern fn LLVMInitializeAMDGPUTargetInfo() void; pub extern fn LLVMInitializeARMTargetInfo() void; pub extern fn LLVMInitializeAVRTargetInfo() void; pub extern fn LLVMInitializeBPFTargetInfo() void; pub extern fn LLVMInitializeHexagonTargetInfo() void; pub extern fn LLVMInitializeLanaiTargetInfo() void; pub extern fn LLVMInitializeMipsTargetInfo() void; pub extern fn LLVMInitializeMSP430TargetInfo() void; pub extern fn LLVMInitializeNVPTXTargetInfo() void; pub extern fn LLVMInitializePowerPCTargetInfo() void; pub extern fn LLVMInitializeRISCVTargetInfo() void; pub extern fn LLVMInitializeSparcTargetInfo() void; pub extern fn LLVMInitializeSystemZTargetInfo() void; pub extern fn LLVMInitializeWebAssemblyTargetInfo() void; pub extern fn LLVMInitializeX86TargetInfo() void; pub extern fn LLVMInitializeXCoreTargetInfo() void; pub extern fn LLVMInitializeM68kTargetInfo() void; pub extern fn LLVMInitializeCSKYTargetInfo() void; pub extern fn LLVMInitializeVETargetInfo() void; pub extern fn LLVMInitializeARCTargetInfo() void; pub extern fn LLVMInitializeAArch64Target() void; pub extern fn LLVMInitializeAMDGPUTarget() void; pub extern fn LLVMInitializeARMTarget() void; pub extern fn LLVMInitializeAVRTarget() void; pub extern fn LLVMInitializeBPFTarget() void; pub extern fn LLVMInitializeHexagonTarget() void; pub extern fn LLVMInitializeLanaiTarget() void; pub extern fn LLVMInitializeMipsTarget() void; pub extern fn LLVMInitializeMSP430Target() void; pub extern fn LLVMInitializeNVPTXTarget() void; pub extern fn LLVMInitializePowerPCTarget() void; pub extern fn LLVMInitializeRISCVTarget() void; pub extern fn LLVMInitializeSparcTarget() void; pub extern fn LLVMInitializeSystemZTarget() void; pub extern fn LLVMInitializeWebAssemblyTarget() void; pub extern fn LLVMInitializeX86Target() void; pub extern fn LLVMInitializeXCoreTarget() void; pub extern fn LLVMInitializeM68kTarget() void; pub extern fn LLVMInitializeVETarget() void; pub extern fn LLVMInitializeCSKYTarget() void; pub extern fn LLVMInitializeARCTarget() void; pub extern fn LLVMInitializeAArch64TargetMC() void; pub extern fn LLVMInitializeAMDGPUTargetMC() void; pub extern fn LLVMInitializeARMTargetMC() void; pub extern fn LLVMInitializeAVRTargetMC() void; pub extern fn LLVMInitializeBPFTargetMC() void; pub extern fn LLVMInitializeHexagonTargetMC() void; pub extern fn LLVMInitializeLanaiTargetMC() void; pub extern fn LLVMInitializeMipsTargetMC() void; pub extern fn LLVMInitializeMSP430TargetMC() void; pub extern fn LLVMInitializeNVPTXTargetMC() void; pub extern fn LLVMInitializePowerPCTargetMC() void; pub extern fn LLVMInitializeRISCVTargetMC() void; pub extern fn LLVMInitializeSparcTargetMC() void; pub extern fn LLVMInitializeSystemZTargetMC() void; pub extern fn LLVMInitializeWebAssemblyTargetMC() void; pub extern fn LLVMInitializeX86TargetMC() void; pub extern fn LLVMInitializeXCoreTargetMC() void; pub extern fn LLVMInitializeM68kTargetMC() void; pub extern fn LLVMInitializeCSKYTargetMC() void; pub extern fn LLVMInitializeVETargetMC() void; pub extern fn LLVMInitializeARCTargetMC() void; pub extern fn LLVMInitializeAArch64AsmPrinter() void; pub extern fn LLVMInitializeAMDGPUAsmPrinter() void; pub extern fn LLVMInitializeARMAsmPrinter() void; pub extern fn LLVMInitializeAVRAsmPrinter() void; pub extern fn LLVMInitializeBPFAsmPrinter() void; pub extern fn LLVMInitializeHexagonAsmPrinter() void; pub extern fn LLVMInitializeLanaiAsmPrinter() void; pub extern fn LLVMInitializeMipsAsmPrinter() void; pub extern fn LLVMInitializeMSP430AsmPrinter() void; pub extern fn LLVMInitializeNVPTXAsmPrinter() void; pub extern fn LLVMInitializePowerPCAsmPrinter() void; pub extern fn LLVMInitializeRISCVAsmPrinter() void; pub extern fn LLVMInitializeSparcAsmPrinter() void; pub extern fn LLVMInitializeSystemZAsmPrinter() void; pub extern fn LLVMInitializeWebAssemblyAsmPrinter() void; pub extern fn LLVMInitializeX86AsmPrinter() void; pub extern fn LLVMInitializeXCoreAsmPrinter() void; pub extern fn LLVMInitializeM68kAsmPrinter() void; pub extern fn LLVMInitializeVEAsmPrinter() void; pub extern fn LLVMInitializeARCAsmPrinter() void; pub extern fn LLVMInitializeAArch64AsmParser() void; pub extern fn LLVMInitializeAMDGPUAsmParser() void; pub extern fn LLVMInitializeARMAsmParser() void; pub extern fn LLVMInitializeAVRAsmParser() void; pub extern fn LLVMInitializeBPFAsmParser() void; pub extern fn LLVMInitializeHexagonAsmParser() void; pub extern fn LLVMInitializeLanaiAsmParser() void; pub extern fn LLVMInitializeMipsAsmParser() void; pub extern fn LLVMInitializeMSP430AsmParser() void; pub extern fn LLVMInitializePowerPCAsmParser() void; pub extern fn LLVMInitializeRISCVAsmParser() void; pub extern fn LLVMInitializeSparcAsmParser() void; pub extern fn LLVMInitializeSystemZAsmParser() void; pub extern fn LLVMInitializeWebAssemblyAsmParser() void; pub extern fn LLVMInitializeX86AsmParser() void; pub extern fn LLVMInitializeM68kAsmParser() void; pub extern fn LLVMInitializeCSKYAsmParser() void; pub extern fn LLVMInitializeVEAsmParser() void; extern fn ZigLLDLinkCOFF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int; extern fn ZigLLDLinkELF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int; extern fn ZigLLDLinkWasm(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int; pub const LinkCOFF = ZigLLDLinkCOFF; pub const LinkELF = ZigLLDLinkELF; pub const LinkWasm = ZigLLDLinkWasm; pub const ObjectFormatType = enum(c_int) { Unknown, COFF, ELF, GOFF, MachO, Wasm, XCOFF, }; pub const WriteArchive = ZigLLVMWriteArchive; extern fn ZigLLVMWriteArchive( archive_name: [*:0]const u8, file_names_ptr: [*]const [*:0]const u8, file_names_len: usize, os_type: OSType, ) bool; pub const OSType = enum(c_int) { UnknownOS, Ananas, CloudABI, Darwin, DragonFly, FreeBSD, Fuchsia, IOS, KFreeBSD, Linux, Lv2, MacOSX, NetBSD, OpenBSD, Solaris, Win32, ZOS, Haiku, Minix, RTEMS, NaCl, AIX, CUDA, NVCL, AMDHSA, PS4, ELFIAMCU, TvOS, WatchOS, Mesa3D, Contiki, AMDPAL, HermitCore, Hurd, WASI, Emscripten, }; pub const ArchType = enum(c_int) { UnknownArch, arm, armeb, aarch64, aarch64_be, aarch64_32, arc, avr, bpfel, bpfeb, csky, hexagon, m68k, mips, mipsel, mips64, mips64el, msp430, ppc, ppcle, ppc64, ppc64le, r600, amdgcn, riscv32, riscv64, sparc, sparcv9, sparcel, systemz, tce, tcele, thumb, thumbeb, x86, x86_64, xcore, nvptx, nvptx64, le32, le64, amdil, amdil64, hsail, hsail64, spir, spir64, kalimba, shave, lanai, wasm32, wasm64, renderscript32, renderscript64, ve, }; pub const ParseCommandLineOptions = ZigLLVMParseCommandLineOptions; extern fn ZigLLVMParseCommandLineOptions(argc: usize, argv: [*]const [*:0]const u8) void; pub const WriteImportLibrary = ZigLLVMWriteImportLibrary; extern fn ZigLLVMWriteImportLibrary( def_path: [*:0]const u8, arch: ArchType, output_lib_path: [*c]const u8, kill_at: bool, ) bool; pub const Linkage = enum(c_uint) { External, AvailableExternally, LinkOnceAny, LinkOnceODR, LinkOnceODRAutoHide, WeakAny, WeakODR, Appending, Internal, Private, DLLImport, DLLExport, ExternalWeak, Ghost, Common, LinkerPrivate, LinkerPrivateWeak, }; pub const AtomicOrdering = enum(c_uint) { NotAtomic = 0, Unordered = 1, Monotonic = 2, Acquire = 4, Release = 5, AcquireRelease = 6, SequentiallyConsistent = 7, }; pub const AtomicRMWBinOp = enum(c_int) { Xchg, Add, Sub, And, Nand, Or, Xor, Max, Min, UMax, UMin, FAdd, FSub, }; pub const TypeKind = enum(c_int) { Void, Half, Float, Double, X86_FP80, FP128, PPC_FP128, Label, Integer, Function, Struct, Array, Pointer, Vector, Metadata, X86_MMX, Token, ScalableVector, BFloat, X86_AMX, }; pub const CallConv = enum(c_uint) { C = 0, Fast = 8, Cold = 9, GHC = 10, HiPE = 11, WebKit_JS = 12, AnyReg = 13, PreserveMost = 14, PreserveAll = 15, Swift = 16, CXX_FAST_TLS = 17, X86_StdCall = 64, X86_FastCall = 65, ARM_APCS = 66, ARM_AAPCS = 67, ARM_AAPCS_VFP = 68, MSP430_INTR = 69, X86_ThisCall = 70, PTX_Kernel = 71, PTX_Device = 72, SPIR_FUNC = 75, SPIR_KERNEL = 76, Intel_OCL_BI = 77, X86_64_SysV = 78, Win64 = 79, X86_VectorCall = 80, HHVM = 81, HHVM_C = 82, X86_INTR = 83, AVR_INTR = 84, AVR_SIGNAL = 85, AVR_BUILTIN = 86, AMDGPU_VS = 87, AMDGPU_GS = 88, AMDGPU_PS = 89, AMDGPU_CS = 90, AMDGPU_KERNEL = 91, X86_RegCall = 92, AMDGPU_HS = 93, MSP430_BUILTIN = 94, AMDGPU_LS = 95, AMDGPU_ES = 96, AArch64_VectorCall = 97, }; pub const CallAttr = enum(c_int) { Auto, NeverTail, NeverInline, AlwaysTail, AlwaysInline, }; pub const address_space = struct { pub const default: c_uint = 0; // See llvm/lib/Target/X86/X86.h pub const x86_64 = x86; pub const x86 = struct { pub const gs: c_uint = 256; pub const fs: c_uint = 257; pub const ss: c_uint = 258; pub const ptr32_sptr: c_uint = 270; pub const ptr32_uptr: c_uint = 271; pub const ptr64: c_uint = 272; }; // See llvm/lib/Target/AVR/AVR.h pub const avr = struct { pub const data_memory: c_uint = 0; pub const program_memory: c_uint = 1; }; // See llvm/lib/Target/NVPTX/NVPTX.h pub const nvptx = struct { pub const generic: c_uint = 0; pub const global: c_uint = 1; pub const constant: c_uint = 2; pub const shared: c_uint = 3; pub const param: c_uint = 4; pub const local: c_uint = 5; }; // See llvm/lib/Target/AMDGPU/AMDGPU.h pub const amdgpu = struct { pub const flat: c_uint = 0; pub const global: c_uint = 1; pub const region: c_uint = 2; pub const local: c_uint = 3; pub const constant: c_uint = 4; pub const private: c_uint = 5; pub const constant_32bit: c_uint = 6; pub const buffer_fat_pointer: c_uint = 7; pub const param_d: c_uint = 6; pub const param_i: c_uint = 7; pub const constant_buffer_0: c_uint = 8; pub const constant_buffer_1: c_uint = 9; pub const constant_buffer_2: c_uint = 10; pub const constant_buffer_3: c_uint = 11; pub const constant_buffer_4: c_uint = 12; pub const constant_buffer_5: c_uint = 13; pub const constant_buffer_6: c_uint = 14; pub const constant_buffer_7: c_uint = 15; pub const constant_buffer_8: c_uint = 16; pub const constant_buffer_9: c_uint = 17; pub const constant_buffer_10: c_uint = 18; pub const constant_buffer_11: c_uint = 19; pub const constant_buffer_12: c_uint = 20; pub const constant_buffer_13: c_uint = 21; pub const constant_buffer_14: c_uint = 22; pub const constant_buffer_15: c_uint = 23; }; };
const std = @import("std"); const Allocator = std.mem.Allocator; const List = std.ArrayList; const Map = std.AutoHashMap; const StrMap = std.StringHashMap; const BitSet = std.DynamicBitSet; const Str = []const u8; const util = @import("util.zig"); const gpa = util.gpa; const data = @embedFile("../data/day11.txt"); // const data = @embedFile("../data/day11-tst.txt"); pub fn main() !void { const w = 10; const h = 10; var grid = [_]u8{0} ** (w * h); var flashed = [_]bool{false} ** (w * h); var it = tokenize(u8, data, "\r\n"); var j: isize = 0; while (it.next()) |line| : (j += 1) { for (line) |c, i| { grid[idx(@intCast(isize, i), j)] = c - '0'; } } var flash_count: usize = 0; // for ([_]u0{0} ** 100) |_, i| { // Uncomment this for step1 for ([_]u0{0} ** 1000) |_, i| { flash_count += step(grid[0..], flashed[0..]); var all_flashed = true; for (grid) |n| { if (n != 0) { all_flashed = false; break; } } if (all_flashed) { print("{}\n", .{i}); break; } } print("{}\n", .{flash_count}); } pub fn idx(i: isize, j: isize) usize { return @intCast(usize, i + 10 * j); } pub fn printGrid(grid: []u8) void { var i: isize = 0; while (i < 10) : (i += 1) { var j: isize = 0; while (j < 10) : (j += 1) { print("{d: >3.}", .{grid[idx(j, i)]}); } print("\n", .{}); } print("\n", .{}); } pub fn step(grid: []u8, flashed: []bool) usize { std.mem.set(bool, flashed, false); for (grid) |*v| { v.* += 1; } var flash_count: usize = 0; while (true) { var had_flash = false; for (grid) |v, i| { if (v > 9 and !flashed[i]) { flash(@intCast(isize, i), grid, flashed); had_flash = true; flash_count += 1; } } if (!had_flash) { break; } } for (grid) |*v, i| { if (flashed[i]) { v.* = 0; } } return flash_count; } fn flash(index: isize, grid: []u8, flashed: []bool) void { flashed[@intCast(usize, index)] = true; var cx: isize = @mod(index, 10); var cy: isize = @divTrunc(index, 10); assert(index == (cx + 10 * cy)); var j: isize = -1; while (j <= 1) : (j += 1) { var y = cy + j; if (y < 0 or y >= 10) continue; var i: isize = -1; while (i <= 1) : (i += 1) { var x = cx + i; if (x < 0 or x >= 10) continue; if (x == cx and y == cy) continue; grid[idx(x, y)] += 1; } } } // Useful stdlib functions const tokenize = std.mem.tokenize; const split = std.mem.split; const indexOf = std.mem.indexOfScalar; const indexOfAny = std.mem.indexOfAny; const indexOfStr = std.mem.indexOfPosLinear; const lastIndexOf = std.mem.lastIndexOfScalar; const lastIndexOfAny = std.mem.lastIndexOfAny; const lastIndexOfStr = std.mem.lastIndexOfLinear; const trim = std.mem.trim; const sliceMin = std.mem.min; const sliceMax = std.mem.max; const parseInt = std.fmt.parseInt; const parseFloat = std.fmt.parseFloat; const min = std.math.min; const min3 = std.math.min3; const max = std.math.max; const max3 = std.math.max3; const print = std.debug.print; const assert = std.debug.assert; const sort = std.sort.sort; const asc = std.sort.asc; const desc = std.sort.desc;