authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 10:35:55-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 10:36:31-05:00
loga966275e509670e750ef54a37f7202078aa4cf07
treeabd0b5da3a788fea0a82908370685a42d61c531e
parent67e6d9bc30dabc295262b1aee8fe5eb044ba5cdc

rename builtin.is_big_endian to builtin.endian

See #307

10 files changed, 117 insertions(+), 96 deletions(-)

example/guess_number/main.zig+2-1
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const std = @import("std");
23const io = std.io;
34const fmt = std.fmt;
......@@ -15,7 +16,7 @@ pub fn main() -> %void {
1516
1617 var seed_bytes: [@sizeOf(usize)]u8 = undefined;
1718 %%os.getRandomBytes(seed_bytes[0..]);
18 const seed = std.mem.readInt(seed_bytes, usize, true);
19 const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big);
1920 var rand = Rand.init(seed);
2021
2122 const answer = rand.range(u8, 0, 100) + 1;
src/codegen.cpp+13-1
......@@ -5129,7 +5129,19 @@ static void define_builtin_compile_vars(CodeGen *g) {
51295129 assert(FloatModeOptimized == 0);
51305130 assert(FloatModeStrict == 1);
51315131 }
5132 buf_appendf(contents, "pub const is_big_endian = %s;\n", bool_to_str(g->is_big_endian));
5132 {
5133 buf_appendf(contents,
5134 "pub const Endian = enum {\n"
5135 " Big,\n"
5136 " Little,\n"
5137 "};\n\n");
5138 assert(FloatModeOptimized == 0);
5139 assert(FloatModeStrict == 1);
5140 }
5141 {
5142 const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little";
5143 buf_appendf(contents, "pub const endian = %s;\n", endian_str);
5144 }
51335145 buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));
51345146 buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);
51355147 buf_appendf(contents, "pub const arch = Arch.%s;\n", cur_arch);
std/debug.zig+9-9
......@@ -303,7 +303,7 @@ const Constant = struct {
303303 return error.InvalidDebugInfo;
304304 if (self.signed)
305305 return error.InvalidDebugInfo;
306 return mem.readInt(self.payload, u64, false);
306 return mem.readInt(self.payload, u64, builtin.Endian.Little);
307307 }
308308};
309309
......@@ -479,7 +479,7 @@ fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, si
479479}
480480
481481fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue {
482 const block_len = %return in_stream.readVarInt(false, usize, size);
482 const block_len = %return in_stream.readVarInt(builtin.Endian.Little, usize, size);
483483 return parseFormValueBlockLen(allocator, in_stream, block_len);
484484}
485485
......@@ -669,10 +669,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
669669 continue;
670670 }
671671
672 const version = %return in_stream.readInt(st.elf.is_big_endian, u16);
672 const version = %return in_stream.readInt(st.elf.endian, u16);
673673 if (version != 2) return error.InvalidDebugInfo;
674674
675 const prologue_length = %return in_stream.readInt(st.elf.is_big_endian, u32);
675 const prologue_length = %return in_stream.readInt(st.elf.endian, u32);
676676 const prog_start_offset = (%return in_file.getPos()) + prologue_length;
677677
678678 const minimum_instruction_length = %return in_stream.readByte();
......@@ -739,7 +739,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
739739 return error.MissingDebugInfo;
740740 },
741741 DW.LNE_set_address => {
742 const addr = %return in_stream.readInt(st.elf.is_big_endian, usize);
742 const addr = %return in_stream.readInt(st.elf.endian, usize);
743743 prog.address = addr;
744744 },
745745 DW.LNE_define_file => {
......@@ -801,7 +801,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
801801 prog.address += inc_addr;
802802 },
803803 DW.LNS_fixed_advance_pc => {
804 const arg = %return in_stream.readInt(st.elf.is_big_endian, u16);
804 const arg = %return in_stream.readInt(st.elf.endian, u16);
805805 prog.address += arg;
806806 },
807807 DW.LNS_set_prologue_end => {
......@@ -839,13 +839,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
839839 return;
840840 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
841841
842 const version = %return in_stream.readInt(st.elf.is_big_endian, u16);
842 const version = %return in_stream.readInt(st.elf.endian, u16);
843843 if (version < 2 or version > 5) return error.InvalidDebugInfo;
844844
845845 const debug_abbrev_offset = if (is_64) {
846 %return in_stream.readInt(st.elf.is_big_endian, u64)
846 %return in_stream.readInt(st.elf.endian, u64)
847847 } else {
848 %return in_stream.readInt(st.elf.is_big_endian, u32)
848 %return in_stream.readInt(st.elf.endian, u32)
849849 };
850850
851851 const address_size = %return in_stream.readByte();
std/elf.zig+40-39
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const std = @import("index.zig");
23const io = std.io;
34const math = std.math;
......@@ -67,7 +68,7 @@ pub const Elf = struct {
6768 in_file: &io.File,
6869 auto_close_stream: bool,
6970 is_64: bool,
70 is_big_endian: bool,
71 endian: builtin.Endian,
7172 file_type: FileType,
7273 arch: Arch,
7374 entry_addr: u64,
......@@ -105,9 +106,9 @@ pub const Elf = struct {
105106 else => return error.InvalidFormat,
106107 };
107108
108 elf.is_big_endian = switch (%return in.readByte()) {
109 1 => false,
110 2 => true,
109 elf.endian = switch (%return in.readByte()) {
110 1 => builtin.Endian.Little,
111 2 => builtin.Endian.Big,
111112 else => return error.InvalidFormat,
112113 };
113114
......@@ -117,7 +118,7 @@ pub const Elf = struct {
117118 // skip over padding
118119 %return elf.in_file.seekForward(9);
119120
120 elf.file_type = switch (%return in.readInt(elf.is_big_endian, u16)) {
121 elf.file_type = switch (%return in.readInt(elf.endian, u16)) {
121122 1 => FileType.Relocatable,
122123 2 => FileType.Executable,
123124 3 => FileType.Shared,
......@@ -125,7 +126,7 @@ pub const Elf = struct {
125126 else => return error.InvalidFormat,
126127 };
127128
128 elf.arch = switch (%return in.readInt(elf.is_big_endian, u16)) {
129 elf.arch = switch (%return in.readInt(elf.endian, u16)) {
129130 0x02 => Arch.Sparc,
130131 0x03 => Arch.x86,
131132 0x08 => Arch.Mips,
......@@ -138,34 +139,34 @@ pub const Elf = struct {
138139 else => return error.InvalidFormat,
139140 };
140141
141 const elf_version = %return in.readInt(elf.is_big_endian, u32);
142 const elf_version = %return in.readInt(elf.endian, u32);
142143 if (elf_version != 1) return error.InvalidFormat;
143144
144145 if (elf.is_64) {
145 elf.entry_addr = %return in.readInt(elf.is_big_endian, u64);
146 elf.program_header_offset = %return in.readInt(elf.is_big_endian, u64);
147 elf.section_header_offset = %return in.readInt(elf.is_big_endian, u64);
146 elf.entry_addr = %return in.readInt(elf.endian, u64);
147 elf.program_header_offset = %return in.readInt(elf.endian, u64);
148 elf.section_header_offset = %return in.readInt(elf.endian, u64);
148149 } else {
149 elf.entry_addr = u64(%return in.readInt(elf.is_big_endian, u32));
150 elf.program_header_offset = u64(%return in.readInt(elf.is_big_endian, u32));
151 elf.section_header_offset = u64(%return in.readInt(elf.is_big_endian, u32));
150 elf.entry_addr = u64(%return in.readInt(elf.endian, u32));
151 elf.program_header_offset = u64(%return in.readInt(elf.endian, u32));
152 elf.section_header_offset = u64(%return in.readInt(elf.endian, u32));
152153 }
153154
154155 // skip over flags
155156 %return elf.in_file.seekForward(4);
156157
157 const header_size = %return in.readInt(elf.is_big_endian, u16);
158 const header_size = %return in.readInt(elf.endian, u16);
158159 if ((elf.is_64 and header_size != 64) or
159160 (!elf.is_64 and header_size != 52))
160161 {
161162 return error.InvalidFormat;
162163 }
163164
164 const ph_entry_size = %return in.readInt(elf.is_big_endian, u16);
165 const ph_entry_count = %return in.readInt(elf.is_big_endian, u16);
166 const sh_entry_size = %return in.readInt(elf.is_big_endian, u16);
167 const sh_entry_count = %return in.readInt(elf.is_big_endian, u16);
168 elf.string_section_index = u64(%return in.readInt(elf.is_big_endian, u16));
165 const ph_entry_size = %return in.readInt(elf.endian, u16);
166 const ph_entry_count = %return in.readInt(elf.endian, u16);
167 const sh_entry_size = %return in.readInt(elf.endian, u16);
168 const sh_entry_count = %return in.readInt(elf.endian, u16);
169 elf.string_section_index = u64(%return in.readInt(elf.endian, u16));
169170
170171 if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat;
171172
......@@ -188,32 +189,32 @@ pub const Elf = struct {
188189 if (sh_entry_size != 64) return error.InvalidFormat;
189190
190191 for (elf.section_headers) |*section| {
191 section.name = %return in.readInt(elf.is_big_endian, u32);
192 section.sh_type = %return in.readInt(elf.is_big_endian, u32);
193 section.flags = %return in.readInt(elf.is_big_endian, u64);
194 section.addr = %return in.readInt(elf.is_big_endian, u64);
195 section.offset = %return in.readInt(elf.is_big_endian, u64);
196 section.size = %return in.readInt(elf.is_big_endian, u64);
197 section.link = %return in.readInt(elf.is_big_endian, u32);
198 section.info = %return in.readInt(elf.is_big_endian, u32);
199 section.addr_align = %return in.readInt(elf.is_big_endian, u64);
200 section.ent_size = %return in.readInt(elf.is_big_endian, u64);
192 section.name = %return in.readInt(elf.endian, u32);
193 section.sh_type = %return in.readInt(elf.endian, u32);
194 section.flags = %return in.readInt(elf.endian, u64);
195 section.addr = %return in.readInt(elf.endian, u64);
196 section.offset = %return in.readInt(elf.endian, u64);
197 section.size = %return in.readInt(elf.endian, u64);
198 section.link = %return in.readInt(elf.endian, u32);
199 section.info = %return in.readInt(elf.endian, u32);
200 section.addr_align = %return in.readInt(elf.endian, u64);
201 section.ent_size = %return in.readInt(elf.endian, u64);
201202 }
202203 } else {
203204 if (sh_entry_size != 40) return error.InvalidFormat;
204205
205206 for (elf.section_headers) |*section| {
206207 // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ?
207 section.name = %return in.readInt(elf.is_big_endian, u32);
208 section.sh_type = %return in.readInt(elf.is_big_endian, u32);
209 section.flags = u64(%return in.readInt(elf.is_big_endian, u32));
210 section.addr = u64(%return in.readInt(elf.is_big_endian, u32));
211 section.offset = u64(%return in.readInt(elf.is_big_endian, u32));
212 section.size = u64(%return in.readInt(elf.is_big_endian, u32));
213 section.link = %return in.readInt(elf.is_big_endian, u32);
214 section.info = %return in.readInt(elf.is_big_endian, u32);
215 section.addr_align = u64(%return in.readInt(elf.is_big_endian, u32));
216 section.ent_size = u64(%return in.readInt(elf.is_big_endian, u32));
208 section.name = %return in.readInt(elf.endian, u32);
209 section.sh_type = %return in.readInt(elf.endian, u32);
210 section.flags = u64(%return in.readInt(elf.endian, u32));
211 section.addr = u64(%return in.readInt(elf.endian, u32));
212 section.offset = u64(%return in.readInt(elf.endian, u32));
213 section.size = u64(%return in.readInt(elf.endian, u32));
214 section.link = %return in.readInt(elf.endian, u32);
215 section.info = %return in.readInt(elf.endian, u32);
216 section.addr_align = u64(%return in.readInt(elf.endian, u32));
217 section.ent_size = u64(%return in.readInt(elf.endian, u32));
217218 }
218219 }
219220
std/endian.zig+2-2
......@@ -9,8 +9,8 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {
99 swapIf(true, T, x)
1010}
1111
12pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
13 if (builtin.is_big_endian == is_be) swap(T, x) else x
12pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {
13 if (builtin.endian == endian) swap(T, x) else x
1414}
1515
1616pub fn swap(comptime T: type, x: T) -> T {
std/io.zig+6-6
......@@ -441,26 +441,26 @@ pub const InStream = struct {
441441 }
442442
443443 pub fn readIntLe(self: &InStream, comptime T: type) -> %T {
444 return self.readInt(false, T);
444 return self.readInt(builtin.Endian.Little, T);
445445 }
446446
447447 pub fn readIntBe(self: &InStream, comptime T: type) -> %T {
448 return self.readInt(true, T);
448 return self.readInt(builtin.Endian.Big, T);
449449 }
450450
451 pub fn readInt(self: &InStream, is_be: bool, comptime T: type) -> %T {
451 pub fn readInt(self: &InStream, endian: builtin.Endian, comptime T: type) -> %T {
452452 var bytes: [@sizeOf(T)]u8 = undefined;
453453 %return self.readNoEof(bytes[0..]);
454 return mem.readInt(bytes, T, is_be);
454 return mem.readInt(bytes, T, endian);
455455 }
456456
457 pub fn readVarInt(self: &InStream, is_be: bool, comptime T: type, size: usize) -> %T {
457 pub fn readVarInt(self: &InStream, endian: builtin.Endian, comptime T: type, size: usize) -> %T {
458458 assert(size <= @sizeOf(T));
459459 assert(size <= 8);
460460 var input_buf: [8]u8 = undefined;
461461 const input_slice = input_buf[0..size];
462462 %return self.readNoEof(input_slice);
463 return mem.readInt(input_slice, T, is_be);
463 return mem.readInt(input_slice, T, endian);
464464 }
465465
466466
std/mem.zig+39-32
......@@ -1,6 +1,7 @@
11const debug = @import("debug.zig");
22const assert = debug.assert;
33const math = @import("math/index.zig");
4const builtin = @import("builtin");
45
56pub const Cmp = math.Cmp;
67
......@@ -181,20 +182,23 @@ test "mem.indexOf" {
181182/// T specifies the return type, which must be large enough to store
182183/// the result.
183184/// See also ::readIntBE or ::readIntLE.
184pub fn readInt(bytes: []const u8, comptime T: type, big_endian: bool) -> T {
185pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) -> T {
185186 if (T.bit_count == 8) {
186187 return bytes[0];
187188 }
188189 var result: T = 0;
189 if (big_endian) {
190 for (bytes) |b| {
191 result = (result << 8) | b;
192 }
193 } else {
194 const ShiftType = math.Log2Int(T);
195 for (bytes) |b, index| {
196 result = result | (T(b) << ShiftType(index * 8));
197 }
190 switch (endian) {
191 builtin.Endian.Big => {
192 for (bytes) |b| {
193 result = (result << 8) | b;
194 }
195 },
196 builtin.Endian.Little => {
197 const ShiftType = math.Log2Int(T);
198 for (bytes) |b, index| {
199 result = result | (T(b) << ShiftType(index * 8));
200 }
201 },
198202 }
199203 return result;
200204}
......@@ -230,22 +234,25 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) -> T {
230234/// Writes an integer to memory with size equal to bytes.len. Pads with zeroes
231235/// to fill the entire buffer provided.
232236/// value must be an integer.
233pub fn writeInt(buf: []u8, value: var, big_endian: bool) {
237pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) {
234238 const uint = @IntType(false, @typeOf(value).bit_count);
235239 var bits = @truncate(uint, value);
236 if (big_endian) {
237 var index: usize = buf.len;
238 while (index != 0) {
239 index -= 1;
240
241 buf[index] = @truncate(u8, bits);
242 bits >>= 8;
243 }
244 } else {
245 for (buf) |*b| {
246 *b = @truncate(u8, bits);
247 bits >>= 8;
248 }
240 switch (endian) {
241 builtin.Endian.Big => {
242 var index: usize = buf.len;
243 while (index != 0) {
244 index -= 1;
245
246 buf[index] = @truncate(u8, bits);
247 bits >>= 8;
248 }
249 },
250 builtin.Endian.Little => {
251 for (buf) |*b| {
252 *b = @truncate(u8, bits);
253 bits >>= 8;
254 }
255 },
249256 }
250257 assert(bits == 0);
251258}
......@@ -377,21 +384,21 @@ test "testReadInt" {
377384fn testReadIntImpl() {
378385 {
379386 const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 };
380 assert(readInt(bytes, u32, true) == 0x12345678);
387 assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
381388 assert(readIntBE(u32, bytes) == 0x12345678);
382389 assert(readIntBE(i32, bytes) == 0x12345678);
383 assert(readInt(bytes, u32, false) == 0x78563412);
390 assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412);
384391 assert(readIntLE(u32, bytes) == 0x78563412);
385392 assert(readIntLE(i32, bytes) == 0x78563412);
386393 }
387394 {
388395 const buf = []u8{0x00, 0x00, 0x12, 0x34};
389 const answer = readInt(buf, u64, true);
396 const answer = readInt(buf, u64, builtin.Endian.Big);
390397 assert(answer == 0x00001234);
391398 }
392399 {
393400 const buf = []u8{0x12, 0x34, 0x00, 0x00};
394 const answer = readInt(buf, u64, false);
401 const answer = readInt(buf, u64, builtin.Endian.Little);
395402 assert(answer == 0x00003412);
396403 }
397404 {
......@@ -410,16 +417,16 @@ test "testWriteInt" {
410417fn testWriteIntImpl() {
411418 var bytes: [4]u8 = undefined;
412419
413 writeInt(bytes[0..], u32(0x12345678), true);
420 writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big);
414421 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
415422
416 writeInt(bytes[0..], u32(0x78563412), false);
423 writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little);
417424 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
418425
419 writeInt(bytes[0..], u16(0x1234), true);
426 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big);
420427 assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
421428
422 writeInt(bytes[0..], u16(0x1234), false);
429 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little);
423430 assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
424431}
425432
std/os/child_process.zig+2-2
......@@ -722,14 +722,14 @@ const ErrInt = @IntType(false, @sizeOf(error) * 8);
722722
723723fn writeIntFd(fd: i32, value: ErrInt) -> %void {
724724 var bytes: [@sizeOf(ErrInt)]u8 = undefined;
725 mem.writeInt(bytes[0..], value, true);
725 mem.writeInt(bytes[0..], value, builtin.endian);
726726 os.posixWrite(fd, bytes[0..]) %% return error.SystemResources;
727727}
728728
729729fn readIntFd(fd: i32) -> %ErrInt {
730730 var bytes: [@sizeOf(ErrInt)]u8 = undefined;
731731 os.posixRead(fd, bytes[0..]) %% return error.SystemResources;
732 return mem.readInt(bytes[0..], ErrInt, true);
732 return mem.readInt(bytes[0..], ErrInt, builtin.endian);
733733}
734734
735735extern fn sigchld_handler(_: i32) {
std/rand.zig+3-3
......@@ -50,12 +50,12 @@ pub const Rand = struct {
5050 pub fn fillBytes(r: &Rand, buf: []u8) {
5151 var bytes_left = buf.len;
5252 while (bytes_left >= @sizeOf(usize)) {
53 mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), false);
53 mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), builtin.Endian.Little);
5454 bytes_left -= @sizeOf(usize);
5555 }
5656 if (bytes_left > 0) {
5757 var rand_val_array: [@sizeOf(usize)]u8 = undefined;
58 mem.writeInt(rand_val_array[0..], r.rng.get(), false);
58 mem.writeInt(rand_val_array[0..], r.rng.get(), builtin.Endian.Little);
5959 while (bytes_left > 0) {
6060 buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];
6161 bytes_left -= 1;
......@@ -98,7 +98,7 @@ pub const Rand = struct {
9898
9999 while (true) {
100100 r.fillBytes(rand_val_array[0..]);
101 const rand_val = mem.readInt(rand_val_array, T, false);
101 const rand_val = mem.readInt(rand_val_array, T, builtin.Endian.Little);
102102 if (rand_val < upper_bound) {
103103 return start + (rand_val % total_range);
104104 }
std/special/compiler_rt/udivmod.zig+1-1
......@@ -1,7 +1,7 @@
11const builtin = @import("builtin");
22const is_test = builtin.is_test;
33
4const low = if (builtin.is_big_endian) 1 else 0;
4const low = switch (builtin.endian) { builtin.Endian.Big => 1, builtin.Endian.Little => 0 };
55const high = 1 - low;
66
77pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) -> DoubleInt {