Commit a966275e50
Changed files (10)
example
guess_number
src
std
example/guess_number/main.zig
@@ -1,3 +1,4 @@
+const builtin = @import("builtin");
const std = @import("std");
const io = std.io;
const fmt = std.fmt;
@@ -15,7 +16,7 @@ pub fn main() -> %void {
var seed_bytes: [@sizeOf(usize)]u8 = undefined;
%%os.getRandomBytes(seed_bytes[0..]);
- const seed = std.mem.readInt(seed_bytes, usize, true);
+ const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big);
var rand = Rand.init(seed);
const answer = rand.range(u8, 0, 100) + 1;
src/codegen.cpp
@@ -5129,7 +5129,19 @@ static void define_builtin_compile_vars(CodeGen *g) {
assert(FloatModeOptimized == 0);
assert(FloatModeStrict == 1);
}
- buf_appendf(contents, "pub const is_big_endian = %s;\n", bool_to_str(g->is_big_endian));
+ {
+ buf_appendf(contents,
+ "pub const Endian = enum {\n"
+ " Big,\n"
+ " Little,\n"
+ "};\n\n");
+ assert(FloatModeOptimized == 0);
+ assert(FloatModeStrict == 1);
+ }
+ {
+ const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little";
+ buf_appendf(contents, "pub const endian = %s;\n", endian_str);
+ }
buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));
buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);
buf_appendf(contents, "pub const arch = Arch.%s;\n", cur_arch);
std/os/child_process.zig
@@ -722,14 +722,14 @@ const ErrInt = @IntType(false, @sizeOf(error) * 8);
fn writeIntFd(fd: i32, value: ErrInt) -> %void {
var bytes: [@sizeOf(ErrInt)]u8 = undefined;
- mem.writeInt(bytes[0..], value, true);
+ mem.writeInt(bytes[0..], value, builtin.endian);
os.posixWrite(fd, bytes[0..]) %% return error.SystemResources;
}
fn readIntFd(fd: i32) -> %ErrInt {
var bytes: [@sizeOf(ErrInt)]u8 = undefined;
os.posixRead(fd, bytes[0..]) %% return error.SystemResources;
- return mem.readInt(bytes[0..], ErrInt, true);
+ return mem.readInt(bytes[0..], ErrInt, builtin.endian);
}
extern fn sigchld_handler(_: i32) {
std/special/compiler_rt/udivmod.zig
@@ -1,7 +1,7 @@
const builtin = @import("builtin");
const is_test = builtin.is_test;
-const low = if (builtin.is_big_endian) 1 else 0;
+const low = switch (builtin.endian) { builtin.Endian.Big => 1, builtin.Endian.Little => 0 };
const high = 1 - low;
pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) -> DoubleInt {
std/debug.zig
@@ -303,7 +303,7 @@ const Constant = struct {
return error.InvalidDebugInfo;
if (self.signed)
return error.InvalidDebugInfo;
- return mem.readInt(self.payload, u64, false);
+ return mem.readInt(self.payload, u64, builtin.Endian.Little);
}
};
@@ -479,7 +479,7 @@ fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, si
}
fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue {
- const block_len = %return in_stream.readVarInt(false, usize, size);
+ const block_len = %return in_stream.readVarInt(builtin.Endian.Little, usize, size);
return parseFormValueBlockLen(allocator, in_stream, block_len);
}
@@ -669,10 +669,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
continue;
}
- const version = %return in_stream.readInt(st.elf.is_big_endian, u16);
+ const version = %return in_stream.readInt(st.elf.endian, u16);
if (version != 2) return error.InvalidDebugInfo;
- const prologue_length = %return in_stream.readInt(st.elf.is_big_endian, u32);
+ const prologue_length = %return in_stream.readInt(st.elf.endian, u32);
const prog_start_offset = (%return in_file.getPos()) + prologue_length;
const minimum_instruction_length = %return in_stream.readByte();
@@ -739,7 +739,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
return error.MissingDebugInfo;
},
DW.LNE_set_address => {
- const addr = %return in_stream.readInt(st.elf.is_big_endian, usize);
+ const addr = %return in_stream.readInt(st.elf.endian, usize);
prog.address = addr;
},
DW.LNE_define_file => {
@@ -801,7 +801,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
prog.address += inc_addr;
},
DW.LNS_fixed_advance_pc => {
- const arg = %return in_stream.readInt(st.elf.is_big_endian, u16);
+ const arg = %return in_stream.readInt(st.elf.endian, u16);
prog.address += arg;
},
DW.LNS_set_prologue_end => {
@@ -839,13 +839,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
return;
const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
- const version = %return in_stream.readInt(st.elf.is_big_endian, u16);
+ const version = %return in_stream.readInt(st.elf.endian, u16);
if (version < 2 or version > 5) return error.InvalidDebugInfo;
const debug_abbrev_offset = if (is_64) {
- %return in_stream.readInt(st.elf.is_big_endian, u64)
+ %return in_stream.readInt(st.elf.endian, u64)
} else {
- %return in_stream.readInt(st.elf.is_big_endian, u32)
+ %return in_stream.readInt(st.elf.endian, u32)
};
const address_size = %return in_stream.readByte();
std/elf.zig
@@ -1,3 +1,4 @@
+const builtin = @import("builtin");
const std = @import("index.zig");
const io = std.io;
const math = std.math;
@@ -67,7 +68,7 @@ pub const Elf = struct {
in_file: &io.File,
auto_close_stream: bool,
is_64: bool,
- is_big_endian: bool,
+ endian: builtin.Endian,
file_type: FileType,
arch: Arch,
entry_addr: u64,
@@ -105,9 +106,9 @@ pub const Elf = struct {
else => return error.InvalidFormat,
};
- elf.is_big_endian = switch (%return in.readByte()) {
- 1 => false,
- 2 => true,
+ elf.endian = switch (%return in.readByte()) {
+ 1 => builtin.Endian.Little,
+ 2 => builtin.Endian.Big,
else => return error.InvalidFormat,
};
@@ -117,7 +118,7 @@ pub const Elf = struct {
// skip over padding
%return elf.in_file.seekForward(9);
- elf.file_type = switch (%return in.readInt(elf.is_big_endian, u16)) {
+ elf.file_type = switch (%return in.readInt(elf.endian, u16)) {
1 => FileType.Relocatable,
2 => FileType.Executable,
3 => FileType.Shared,
@@ -125,7 +126,7 @@ pub const Elf = struct {
else => return error.InvalidFormat,
};
- elf.arch = switch (%return in.readInt(elf.is_big_endian, u16)) {
+ elf.arch = switch (%return in.readInt(elf.endian, u16)) {
0x02 => Arch.Sparc,
0x03 => Arch.x86,
0x08 => Arch.Mips,
@@ -138,34 +139,34 @@ pub const Elf = struct {
else => return error.InvalidFormat,
};
- const elf_version = %return in.readInt(elf.is_big_endian, u32);
+ const elf_version = %return in.readInt(elf.endian, u32);
if (elf_version != 1) return error.InvalidFormat;
if (elf.is_64) {
- elf.entry_addr = %return in.readInt(elf.is_big_endian, u64);
- elf.program_header_offset = %return in.readInt(elf.is_big_endian, u64);
- elf.section_header_offset = %return in.readInt(elf.is_big_endian, u64);
+ elf.entry_addr = %return in.readInt(elf.endian, u64);
+ elf.program_header_offset = %return in.readInt(elf.endian, u64);
+ elf.section_header_offset = %return in.readInt(elf.endian, u64);
} else {
- elf.entry_addr = u64(%return in.readInt(elf.is_big_endian, u32));
- elf.program_header_offset = u64(%return in.readInt(elf.is_big_endian, u32));
- elf.section_header_offset = u64(%return in.readInt(elf.is_big_endian, u32));
+ elf.entry_addr = u64(%return in.readInt(elf.endian, u32));
+ elf.program_header_offset = u64(%return in.readInt(elf.endian, u32));
+ elf.section_header_offset = u64(%return in.readInt(elf.endian, u32));
}
// skip over flags
%return elf.in_file.seekForward(4);
- const header_size = %return in.readInt(elf.is_big_endian, u16);
+ const header_size = %return in.readInt(elf.endian, u16);
if ((elf.is_64 and header_size != 64) or
(!elf.is_64 and header_size != 52))
{
return error.InvalidFormat;
}
- const ph_entry_size = %return in.readInt(elf.is_big_endian, u16);
- const ph_entry_count = %return in.readInt(elf.is_big_endian, u16);
- const sh_entry_size = %return in.readInt(elf.is_big_endian, u16);
- const sh_entry_count = %return in.readInt(elf.is_big_endian, u16);
- elf.string_section_index = u64(%return in.readInt(elf.is_big_endian, u16));
+ const ph_entry_size = %return in.readInt(elf.endian, u16);
+ const ph_entry_count = %return in.readInt(elf.endian, u16);
+ const sh_entry_size = %return in.readInt(elf.endian, u16);
+ const sh_entry_count = %return in.readInt(elf.endian, u16);
+ elf.string_section_index = u64(%return in.readInt(elf.endian, u16));
if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat;
@@ -188,32 +189,32 @@ pub const Elf = struct {
if (sh_entry_size != 64) return error.InvalidFormat;
for (elf.section_headers) |*section| {
- section.name = %return in.readInt(elf.is_big_endian, u32);
- section.sh_type = %return in.readInt(elf.is_big_endian, u32);
- section.flags = %return in.readInt(elf.is_big_endian, u64);
- section.addr = %return in.readInt(elf.is_big_endian, u64);
- section.offset = %return in.readInt(elf.is_big_endian, u64);
- section.size = %return in.readInt(elf.is_big_endian, u64);
- section.link = %return in.readInt(elf.is_big_endian, u32);
- section.info = %return in.readInt(elf.is_big_endian, u32);
- section.addr_align = %return in.readInt(elf.is_big_endian, u64);
- section.ent_size = %return in.readInt(elf.is_big_endian, u64);
+ section.name = %return in.readInt(elf.endian, u32);
+ section.sh_type = %return in.readInt(elf.endian, u32);
+ section.flags = %return in.readInt(elf.endian, u64);
+ section.addr = %return in.readInt(elf.endian, u64);
+ section.offset = %return in.readInt(elf.endian, u64);
+ section.size = %return in.readInt(elf.endian, u64);
+ section.link = %return in.readInt(elf.endian, u32);
+ section.info = %return in.readInt(elf.endian, u32);
+ section.addr_align = %return in.readInt(elf.endian, u64);
+ section.ent_size = %return in.readInt(elf.endian, u64);
}
} else {
if (sh_entry_size != 40) return error.InvalidFormat;
for (elf.section_headers) |*section| {
// TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ?
- section.name = %return in.readInt(elf.is_big_endian, u32);
- section.sh_type = %return in.readInt(elf.is_big_endian, u32);
- section.flags = u64(%return in.readInt(elf.is_big_endian, u32));
- section.addr = u64(%return in.readInt(elf.is_big_endian, u32));
- section.offset = u64(%return in.readInt(elf.is_big_endian, u32));
- section.size = u64(%return in.readInt(elf.is_big_endian, u32));
- section.link = %return in.readInt(elf.is_big_endian, u32);
- section.info = %return in.readInt(elf.is_big_endian, u32);
- section.addr_align = u64(%return in.readInt(elf.is_big_endian, u32));
- section.ent_size = u64(%return in.readInt(elf.is_big_endian, u32));
+ section.name = %return in.readInt(elf.endian, u32);
+ section.sh_type = %return in.readInt(elf.endian, u32);
+ section.flags = u64(%return in.readInt(elf.endian, u32));
+ section.addr = u64(%return in.readInt(elf.endian, u32));
+ section.offset = u64(%return in.readInt(elf.endian, u32));
+ section.size = u64(%return in.readInt(elf.endian, u32));
+ section.link = %return in.readInt(elf.endian, u32);
+ section.info = %return in.readInt(elf.endian, u32);
+ section.addr_align = u64(%return in.readInt(elf.endian, u32));
+ section.ent_size = u64(%return in.readInt(elf.endian, u32));
}
}
std/endian.zig
@@ -9,8 +9,8 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {
swapIf(true, T, x)
}
-pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
- if (builtin.is_big_endian == is_be) swap(T, x) else x
+pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {
+ if (builtin.endian == endian) swap(T, x) else x
}
pub fn swap(comptime T: type, x: T) -> T {
std/io.zig
@@ -441,26 +441,26 @@ pub const InStream = struct {
}
pub fn readIntLe(self: &InStream, comptime T: type) -> %T {
- return self.readInt(false, T);
+ return self.readInt(builtin.Endian.Little, T);
}
pub fn readIntBe(self: &InStream, comptime T: type) -> %T {
- return self.readInt(true, T);
+ return self.readInt(builtin.Endian.Big, T);
}
- pub fn readInt(self: &InStream, is_be: bool, comptime T: type) -> %T {
+ pub fn readInt(self: &InStream, endian: builtin.Endian, comptime T: type) -> %T {
var bytes: [@sizeOf(T)]u8 = undefined;
%return self.readNoEof(bytes[0..]);
- return mem.readInt(bytes, T, is_be);
+ return mem.readInt(bytes, T, endian);
}
- pub fn readVarInt(self: &InStream, is_be: bool, comptime T: type, size: usize) -> %T {
+ pub fn readVarInt(self: &InStream, endian: builtin.Endian, comptime T: type, size: usize) -> %T {
assert(size <= @sizeOf(T));
assert(size <= 8);
var input_buf: [8]u8 = undefined;
const input_slice = input_buf[0..size];
%return self.readNoEof(input_slice);
- return mem.readInt(input_slice, T, is_be);
+ return mem.readInt(input_slice, T, endian);
}
std/mem.zig
@@ -1,6 +1,7 @@
const debug = @import("debug.zig");
const assert = debug.assert;
const math = @import("math/index.zig");
+const builtin = @import("builtin");
pub const Cmp = math.Cmp;
@@ -181,20 +182,23 @@ test "mem.indexOf" {
/// T specifies the return type, which must be large enough to store
/// the result.
/// See also ::readIntBE or ::readIntLE.
-pub fn readInt(bytes: []const u8, comptime T: type, big_endian: bool) -> T {
+pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) -> T {
if (T.bit_count == 8) {
return bytes[0];
}
var result: T = 0;
- if (big_endian) {
- for (bytes) |b| {
- result = (result << 8) | b;
- }
- } else {
- const ShiftType = math.Log2Int(T);
- for (bytes) |b, index| {
- result = result | (T(b) << ShiftType(index * 8));
- }
+ switch (endian) {
+ builtin.Endian.Big => {
+ for (bytes) |b| {
+ result = (result << 8) | b;
+ }
+ },
+ builtin.Endian.Little => {
+ const ShiftType = math.Log2Int(T);
+ for (bytes) |b, index| {
+ result = result | (T(b) << ShiftType(index * 8));
+ }
+ },
}
return result;
}
@@ -230,22 +234,25 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) -> T {
/// Writes an integer to memory with size equal to bytes.len. Pads with zeroes
/// to fill the entire buffer provided.
/// value must be an integer.
-pub fn writeInt(buf: []u8, value: var, big_endian: bool) {
+pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) {
const uint = @IntType(false, @typeOf(value).bit_count);
var bits = @truncate(uint, value);
- if (big_endian) {
- var index: usize = buf.len;
- while (index != 0) {
- index -= 1;
-
- buf[index] = @truncate(u8, bits);
- bits >>= 8;
- }
- } else {
- for (buf) |*b| {
- *b = @truncate(u8, bits);
- bits >>= 8;
- }
+ switch (endian) {
+ builtin.Endian.Big => {
+ var index: usize = buf.len;
+ while (index != 0) {
+ index -= 1;
+
+ buf[index] = @truncate(u8, bits);
+ bits >>= 8;
+ }
+ },
+ builtin.Endian.Little => {
+ for (buf) |*b| {
+ *b = @truncate(u8, bits);
+ bits >>= 8;
+ }
+ },
}
assert(bits == 0);
}
@@ -377,21 +384,21 @@ test "testReadInt" {
fn testReadIntImpl() {
{
const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 };
- assert(readInt(bytes, u32, true) == 0x12345678);
+ assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
assert(readIntBE(u32, bytes) == 0x12345678);
assert(readIntBE(i32, bytes) == 0x12345678);
- assert(readInt(bytes, u32, false) == 0x78563412);
+ assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412);
assert(readIntLE(u32, bytes) == 0x78563412);
assert(readIntLE(i32, bytes) == 0x78563412);
}
{
const buf = []u8{0x00, 0x00, 0x12, 0x34};
- const answer = readInt(buf, u64, true);
+ const answer = readInt(buf, u64, builtin.Endian.Big);
assert(answer == 0x00001234);
}
{
const buf = []u8{0x12, 0x34, 0x00, 0x00};
- const answer = readInt(buf, u64, false);
+ const answer = readInt(buf, u64, builtin.Endian.Little);
assert(answer == 0x00003412);
}
{
@@ -410,16 +417,16 @@ test "testWriteInt" {
fn testWriteIntImpl() {
var bytes: [4]u8 = undefined;
- writeInt(bytes[0..], u32(0x12345678), true);
+ writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big);
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
- writeInt(bytes[0..], u32(0x78563412), false);
+ writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little);
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
- writeInt(bytes[0..], u16(0x1234), true);
+ writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big);
assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
- writeInt(bytes[0..], u16(0x1234), false);
+ writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little);
assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
}
std/rand.zig
@@ -50,12 +50,12 @@ pub const Rand = struct {
pub fn fillBytes(r: &Rand, buf: []u8) {
var bytes_left = buf.len;
while (bytes_left >= @sizeOf(usize)) {
- mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), false);
+ mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), builtin.Endian.Little);
bytes_left -= @sizeOf(usize);
}
if (bytes_left > 0) {
var rand_val_array: [@sizeOf(usize)]u8 = undefined;
- mem.writeInt(rand_val_array[0..], r.rng.get(), false);
+ mem.writeInt(rand_val_array[0..], r.rng.get(), builtin.Endian.Little);
while (bytes_left > 0) {
buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];
bytes_left -= 1;
@@ -98,7 +98,7 @@ pub const Rand = struct {
while (true) {
r.fillBytes(rand_val_array[0..]);
- const rand_val = mem.readInt(rand_val_array, T, false);
+ const rand_val = mem.readInt(rand_val_array, T, builtin.Endian.Little);
if (rand_val < upper_bound) {
return start + (rand_val % total_range);
}