Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
export fn sum_mem_reader(data: [*]const u8, len: usize) u64 { const mr = &MemReader.init(data[0..len]); const reader = Reader(MemReader.Error).init(mr); var res: u64 = 0; while (byte(reader)) |c| { res += c; } else |_| {} return res; } const std = @import("std"); pub fn Reader(comptime E: type) type { return struct { pub const Error = E; const VTable = struct { read: fn (reader: *c_void, buf: []u8) Error![]u8, }; vtable: *const VTable, impl: *c_void, pub fn init(reader: var) @This() { const T = @typeOf(reader).Child; return @This(){ .vtable = comptime populate(VTable, T, T), .impl = @ptrCast(*c_void, reader), }; } pub fn read(reader: @This(), buf: []u8) Error![]u8 { return reader.vtable.read(reader.impl, buf); } }; } const MemReader = struct { pub const Error = error{}; buffer: []const u8, i: usize, pub fn init(buffer: []const u8) MemReader { return MemReader{ .buffer = buffer, .i = 0, }; } pub fn read(reader: *MemReader, buf: []u8) Error![]u8 { const buffer = reader.rest(); const len = std.math.min(buf.len, buffer.len); std.mem.copy(u8, buf, buffer[0..len]); reader.i += len; return buf[0..len]; } pub fn rest(reader: MemReader) []const u8 { return reader.buffer[reader.i..]; } }; pub fn byte(reader: var) !u8 { var buf: [1]u8 = undefined; const bytes = try reader.read(buf[0..]); if (bytes.len == 0) return error.EndOfStream; return bytes[0]; } const builtin = @import("builtin"); const debug = std.debug; const TypeInfo = builtin.TypeInfo; pub fn populate(comptime VTable: type, comptime Functions: type, comptime T: type) *const VTable { const GlobalStorage = struct { const vtable = blk: { var res: VTable = undefined; inline for (@typeInfo(VTable).Struct.fields) |field| { const Fn =@typeOf(@field(res, field.name)); const Expect = @typeInfo(Fn).Fn; const Actual = @typeInfo(@typeOf(@field(Functions, field.name))).Fn; debug.assert(!Expect.is_generic); debug.assert(!Expect.is_var_args); debug.assert(Expect.args.len > 0); debug.assert(Expect.async_allocator_type == null); debug.assert(Actual.async_allocator_type == null); debug.assert(Expect.calling_convention == Actual.calling_convention); debug.assert(Expect.is_generic == Actual.is_generic); debug.assert(Expect.is_var_args == Actual.is_var_args); debug.assert(Expect.return_type.? == Actual.return_type.?); debug.assert(Expect.args.len == Actual.args.len); for (Expect.args) |expect_arg, i| { const actual_arg = Actual.args[i]; debug.assert(!expect_arg.is_generic); debug.assert(expect_arg.is_generic == actual_arg.is_generic); debug.assert(expect_arg.is_noalias == actual_arg.is_noalias); // For the first arg. We enforce that it is a pointer, and // that the actual function takes *T. if (i == 0) { const expect_ptr = @typeInfo(expect_arg.arg_type.?).Pointer; const actual_ptr = @typeInfo(actual_arg.arg_type.?).Pointer; debug.assert(expect_ptr.size == TypeInfo.Pointer.Size.One); debug.assert(expect_ptr.size == actual_ptr.size); debug.assert(expect_ptr.is_const == actual_ptr.is_const); debug.assert(expect_ptr.is_volatile == actual_ptr.is_volatile); debug.assert(actual_ptr.child == T); } else { debug.assert(expect_arg.arg_type.? == actual_arg.arg_type.?); } } @field(res, field.name) = @ptrCast(Fn, @field(T, field.name)); } break :blk res; }; }; return &GlobalStorage.vtable; }
zig source #3
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const io = @import("std").io; export fn sum_stream(data: [*]const u8, len: usize) u64 { var stream = &io.SliceInStream.init(data[0..len]).stream; var res: u64 = 0; while (stream.readByte()) |c| { res += c; } else |_| {} return res; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree