Merge remote-tracking branch 'origin/master' into fix-field-alignment-kludge

This commit is contained in:
Andrew Kelley 2019-08-23 11:43:37 -04:00
commit 3865b6ad8f
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
23 changed files with 456 additions and 246 deletions

View File

@ -895,6 +895,8 @@ struct AstNodeStructField {
Buf *name;
AstNode *type;
AstNode *value;
// populated if the "align(A)" is present
AstNode *align_expr;
};
struct AstNodeStringLiteral {

View File

@ -1147,7 +1147,7 @@ static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_
if (type_is_invalid(align_result->type))
return false;
uint32_t align_bytes = bigint_as_unsigned(&align_result->data.x_bigint);
uint32_t align_bytes = bigint_as_u32(&align_result->data.x_bigint);
if (align_bytes == 0) {
add_node_error(g, node, buf_sprintf("alignment must be >= 1"));
return false;
@ -1179,7 +1179,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
return true;
}
expand_undef_array(g, array_val);
size_t len = bigint_as_unsigned(&len_field->data.x_bigint);
size_t len = bigint_as_usize(&len_field->data.x_bigint);
Buf *result = buf_alloc();
buf_resize(result, len);
for (size_t i = 0; i < len; i += 1) {
@ -1189,7 +1189,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
add_node_error(g, node, buf_sprintf("use of undefined value"));
return false;
}
uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint);
uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint);
assert(big_c <= UINT8_MAX);
uint8_t c = (uint8_t)big_c;
buf_ptr(result)[i] = c;
@ -2384,19 +2384,25 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
if (field->gen_index == SIZE_MAX)
continue;
// TODO: https://github.com/ziglang/zig/issues/1512
size_t this_field_align;
if (packed) {
this_field_align = 1;
AstNode *align_expr = field->decl_node->data.struct_field.align_expr;
if (align_expr != nullptr) {
if (!analyze_const_align(g, &struct_type->data.structure.decls_scope->base, align_expr,
&field->align))
{
struct_type->data.structure.resolve_status = ResolveStatusInvalid;
return err;
}
} else if (packed) {
field->align = 1;
} else {
if ((err = type_val_resolve_abi_align(g, field->type_val, &this_field_align))) {
if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
struct_type->data.structure.resolve_status = ResolveStatusInvalid;
return err;
}
}
if (this_field_align > struct_type->abi_align) {
struct_type->abi_align = this_field_align;
if (field->align > struct_type->abi_align) {
struct_type->abi_align = field->align;
}
}
@ -6008,7 +6014,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
{
if (is_slice(type_entry)) {
ConstExprValue *len_val = &const_val->data.x_struct.fields[slice_len_index];
size_t len = bigint_as_unsigned(&len_val->data.x_bigint);
size_t len = bigint_as_usize(&len_val->data.x_bigint);
ConstExprValue *ptr_val = &const_val->data.x_struct.fields[slice_ptr_index];
if (ptr_val->special == ConstValSpecialUndef) {

View File

@ -15,6 +15,8 @@
#include <limits>
#include <algorithm>
static uint64_t bigint_as_unsigned(const BigInt *bigint);
static void bigint_normalize(BigInt *dest) {
const uint64_t *digits = bigint_ptr(dest);
@ -1660,7 +1662,7 @@ size_t bigint_clz(const BigInt *bi, size_t bit_count) {
return count;
}
uint64_t bigint_as_unsigned(const BigInt *bigint) {
static uint64_t bigint_as_unsigned(const BigInt *bigint) {
assert(!bigint->is_negative);
if (bigint->digit_count == 0) {
return 0;
@ -1671,6 +1673,25 @@ uint64_t bigint_as_unsigned(const BigInt *bigint) {
}
}
uint64_t bigint_as_u64(const BigInt *bigint)
{
return bigint_as_unsigned(bigint);
}
uint32_t bigint_as_u32(const BigInt *bigint) {
uint64_t value64 = bigint_as_unsigned(bigint);
uint32_t value32 = (uint32_t)value64;
assert (value64 == value32);
return value32;
}
size_t bigint_as_usize(const BigInt *bigint) {
uint64_t value64 = bigint_as_unsigned(bigint);
size_t valueUsize = (size_t)value64;
assert (value64 == valueUsize);
return valueUsize;
}
int64_t bigint_as_signed(const BigInt *bigint) {
if (bigint->digit_count == 0) {
return 0;

View File

@ -36,7 +36,10 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);
void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count, bool is_negative);
// panics if number won't fit
uint64_t bigint_as_unsigned(const BigInt *bigint);
uint64_t bigint_as_u64(const BigInt *bigint);
uint32_t bigint_as_u32(const BigInt *bigint);
size_t bigint_as_usize(const BigInt *bigint);
int64_t bigint_as_signed(const BigInt *bigint);
static inline const uint64_t *bigint_ptr(const BigInt *bigint) {

View File

@ -2872,7 +2872,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
eval_min_max_value_int(g, int_type, &biggest_possible_err_val, true);
if (bigint_fits_in_bits(&biggest_possible_err_val, 64, false) &&
bigint_as_unsigned(&biggest_possible_err_val) < g->errors_by_index.length)
bigint_as_usize(&biggest_possible_err_val) < g->errors_by_index.length)
{
ok_bit = neq_zero_bit;
} else {

View File

@ -5768,7 +5768,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf)));
return irb->codegen->invalid_instruction;
}
bit_offset_start = bigint_as_unsigned(node->data.pointer_type.bit_offset_start);
bit_offset_start = bigint_as_u32(node->data.pointer_type.bit_offset_start);
}
uint32_t host_int_bytes = 0;
@ -5780,7 +5780,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
buf_sprintf("value %s too large for u32 byte count", buf_ptr(val_buf)));
return irb->codegen->invalid_instruction;
}
host_int_bytes = bigint_as_unsigned(node->data.pointer_type.host_int_bytes);
host_int_bytes = bigint_as_u32(node->data.pointer_type.host_int_bytes);
}
if (host_int_bytes != 0 && bit_offset_start >= host_int_bytes * 8) {
@ -11589,7 +11589,7 @@ static IrInstruction *ir_analyze_int_to_err(IrAnalyze *ira, IrInstruction *sourc
return ira->codegen->invalid_instruction;
}
size_t index = bigint_as_unsigned(&val->data.x_bigint);
size_t index = bigint_as_usize(&val->data.x_bigint);
result->value.data.x_err_set = ira->codegen->errors_by_index.at(index);
return result;
} else {
@ -12554,7 +12554,7 @@ static bool ir_resolve_const_align(CodeGen *codegen, IrExecutable *exec, AstNode
if ((err = ir_resolve_const_val(codegen, exec, source_node, const_val, UndefBad)))
return false;
uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint);
uint32_t align_bytes = bigint_as_u32(&const_val->data.x_bigint);
if (align_bytes == 0) {
exec_add_error_node(codegen, exec, source_node, buf_sprintf("alignment must be >= 1"));
return false;
@ -12594,7 +12594,7 @@ static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *i
if (!const_val)
return false;
*out = bigint_as_unsigned(&const_val->data.x_bigint);
*out = bigint_as_u64(&const_val->data.x_bigint);
return true;
}
@ -12642,7 +12642,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic
if (!const_val)
return false;
*out = (AtomicOrder)bigint_as_unsigned(&const_val->data.x_enum_tag);
*out = (AtomicOrder)bigint_as_u32(&const_val->data.x_enum_tag);
return true;
}
@ -12662,7 +12662,7 @@ static bool ir_resolve_atomic_rmw_op(IrAnalyze *ira, IrInstruction *value, Atomi
if (!const_val)
return false;
*out = (AtomicRmwOp)bigint_as_unsigned(&const_val->data.x_enum_tag);
*out = (AtomicRmwOp)bigint_as_u32(&const_val->data.x_enum_tag);
return true;
}
@ -12682,7 +12682,7 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, IrInstruction *value, Glob
if (!const_val)
return false;
*out = (GlobalLinkageId)bigint_as_unsigned(&const_val->data.x_enum_tag);
*out = (GlobalLinkageId)bigint_as_u32(&const_val->data.x_enum_tag);
return true;
}
@ -12702,7 +12702,7 @@ static bool ir_resolve_float_mode(IrAnalyze *ira, IrInstruction *value, FloatMod
if (!const_val)
return false;
*out = (FloatMode)bigint_as_unsigned(&const_val->data.x_enum_tag);
*out = (FloatMode)bigint_as_u32(&const_val->data.x_enum_tag);
return true;
}
@ -12731,7 +12731,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
return array_val->data.x_array.data.s_buf;
}
expand_undef_array(ira->codegen, array_val);
size_t len = bigint_as_unsigned(&len_field->data.x_bigint);
size_t len = bigint_as_usize(&len_field->data.x_bigint);
Buf *result = buf_alloc();
buf_resize(result, len);
for (size_t i = 0; i < len; i += 1) {
@ -12741,7 +12741,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
ir_add_error(ira, casted_value, buf_sprintf("use of undefined value"));
return nullptr;
}
uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint);
uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint);
assert(big_c <= UINT8_MAX);
uint8_t c = (uint8_t)big_c;
buf_ptr(result)[i] = c;
@ -13891,7 +13891,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
op1_array_val = ptr_val->data.x_ptr.data.base_array.array_val;
op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
ConstExprValue *len_val = &op1_val->data.x_struct.fields[slice_len_index];
op1_array_end = op1_array_index + bigint_as_unsigned(&len_val->data.x_bigint);
op1_array_end = op1_array_index + bigint_as_usize(&len_val->data.x_bigint);
} else {
ir_add_error(ira, op1,
buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));
@ -13924,7 +13924,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
op2_array_val = ptr_val->data.x_ptr.data.base_array.array_val;
op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
ConstExprValue *len_val = &op2_val->data.x_struct.fields[slice_len_index];
op2_array_end = op2_array_index + bigint_as_unsigned(&len_val->data.x_bigint);
op2_array_end = op2_array_index + bigint_as_usize(&len_val->data.x_bigint);
} else {
ir_add_error(ira, op2,
buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name)));
@ -16803,7 +16803,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type);
uint64_t ptr_align = get_ptr_align(ira->codegen, return_type);
if (instr_is_comptime(casted_elem_index)) {
uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint);
uint64_t index = bigint_as_u64(&casted_elem_index->value.data.x_bigint);
if (array_type->id == ZigTypeIdArray) {
uint64_t array_len = array_type->data.array.len;
if (index >= array_len) {
@ -16965,7 +16965,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
ConstExprValue *len_field = &array_ptr_val->data.x_struct.fields[slice_len_index];
IrInstruction *result = ir_const(ira, &elem_ptr_instruction->base, return_type);
ConstExprValue *out_val = &result->value;
uint64_t slice_len = bigint_as_unsigned(&len_field->data.x_bigint);
uint64_t slice_len = bigint_as_u64(&len_field->data.x_bigint);
if (index >= slice_len) {
ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
buf_sprintf("index %" ZIG_PRI_u64 " outside slice of size %" ZIG_PRI_u64,
@ -21250,7 +21250,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
ConstExprValue *len_val = &val->data.x_struct.fields[slice_len_index];
if (value_is_comptime(len_val)) {
known_len = bigint_as_unsigned(&len_val->data.x_bigint);
known_len = bigint_as_u64(&len_val->data.x_bigint);
have_known_len = true;
}
}
@ -21607,7 +21607,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
zig_panic("TODO memset on null ptr");
}
size_t count = bigint_as_unsigned(&count_val->data.x_bigint);
size_t count = bigint_as_usize(&count_val->data.x_bigint);
size_t end = start + count;
if (end > bound_end) {
ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
@ -21704,7 +21704,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
return ira->codegen->invalid_instruction;
if (dest_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
size_t count = bigint_as_unsigned(&count_val->data.x_bigint);
size_t count = bigint_as_usize(&count_val->data.x_bigint);
ConstExprValue *dest_elements;
size_t dest_start;
@ -21988,7 +21988,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
case ConstPtrSpecialBaseArray:
array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index;
rel_end = bigint_as_unsigned(&len_val->data.x_bigint);
rel_end = bigint_as_usize(&len_val->data.x_bigint);
break;
case ConstPtrSpecialBaseStruct:
zig_panic("TODO slice const inner struct");
@ -22001,7 +22001,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
case ConstPtrSpecialHardCodedAddr:
array_val = nullptr;
abs_offset = 0;
rel_end = bigint_as_unsigned(&len_val->data.x_bigint);
rel_end = bigint_as_usize(&len_val->data.x_bigint);
break;
case ConstPtrSpecialFunction:
zig_panic("TODO slice of slice cast from function");
@ -22012,7 +22012,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
zig_unreachable();
}
uint64_t start_scalar = bigint_as_unsigned(&casted_start->value.data.x_bigint);
uint64_t start_scalar = bigint_as_u64(&casted_start->value.data.x_bigint);
if (!ptr_is_undef && start_scalar > rel_end) {
ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
return ira->codegen->invalid_instruction;
@ -22020,7 +22020,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
uint64_t end_scalar;
if (end) {
end_scalar = bigint_as_unsigned(&end->value.data.x_bigint);
end_scalar = bigint_as_u64(&end->value.data.x_bigint);
} else {
end_scalar = rel_end;
}
@ -23622,7 +23622,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
BigInt bn;
bigint_read_twos_complement(&bn, buf, codegen->builtin_types.entry_usize->data.integral.bit_count,
codegen->is_big_endian, false);
val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn);
val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_usize(&bn);
return ErrorNone;
}
case ZigTypeIdArray:
@ -23815,7 +23815,7 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc
if (!val)
return ira->codegen->invalid_instruction;
uint64_t addr = bigint_as_unsigned(&val->data.x_bigint);
uint64_t addr = bigint_as_u64(&val->data.x_bigint);
if (!ptr_allows_addr_zero(ptr_type) && addr == 0) {
ir_add_error(ira, source_instr,
buf_sprintf("pointer type '%s' does not allow address zero", buf_ptr(&ptr_type->name)));

View File

@ -1125,29 +1125,27 @@ Error os_get_cwd(Buf *out_cwd) {
#endif
}
#if defined(ZIG_OS_WINDOWS)
#define is_wprefix(s, prefix) \
(wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
static bool is_stderr_cyg_pty(void) {
HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
if (stderr_handle == INVALID_HANDLE_VALUE)
return false;
int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
FILE_NAME_INFO *nameinfo;
WCHAR *p = NULL;
bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) {
#if defined(ZIG_OS_WINDOWS)
HANDLE handle = (HANDLE)_get_osfhandle(fd);
// Cygwin/msys's pty is a pipe.
if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) {
return 0;
if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) {
return false;
}
nameinfo = (FILE_NAME_INFO *)allocate<char>(size);
int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
WCHAR *p = NULL;
FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate<char>(size);
if (nameinfo == NULL) {
return 0;
return false;
}
// Check the name of the pipe:
// '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master'
if (GetFileInformationByHandleEx(stderr_handle, FileNameInfo, nameinfo, size)) {
if (GetFileInformationByHandleEx(handle, FileNameInfo, nameinfo, size)) {
nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
p = nameinfo->FileName;
if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */
@ -1180,12 +1178,14 @@ static bool is_stderr_cyg_pty(void) {
}
free(nameinfo);
return (p != NULL);
}
#else
return false;
#endif
}
bool os_stderr_tty(void) {
#if defined(ZIG_OS_WINDOWS)
return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty();
return _isatty(fileno(stderr)) != 0 || os_is_cygwin_pty(fileno(stderr));
#elif defined(ZIG_OS_POSIX)
return isatty(STDERR_FILENO) != 0;
#else
@ -1486,7 +1486,7 @@ WORD original_console_attributes = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BL
void os_stderr_set_color(TermColor color) {
#if defined(ZIG_OS_WINDOWS)
if (is_stderr_cyg_pty()) {
if (os_stderr_tty()) {
set_color_posix(color);
return;
}

View File

@ -11,6 +11,7 @@
#include "list.hpp"
#include "buffer.hpp"
#include "error.hpp"
#include "target.hpp"
#include "zig_llvm.h"
#include "windows_sdk.h"
@ -88,6 +89,11 @@ struct Termination {
#define OsFile int
#endif
#if defined(ZIG_OS_WINDOWS)
#undef fileno
#define fileno _fileno
#endif
struct OsTimeStamp {
uint64_t sec;
uint64_t nsec;
@ -152,6 +158,8 @@ Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf
Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
Error ATTRIBUTE_MUST_USE os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd);
Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList<Buf *> &paths);
#endif

View File

@ -782,24 +782,26 @@ static AstNode *ast_parse_var_decl(ParseContext *pc) {
return res;
}
// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)?
// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)?
static AstNode *ast_parse_container_field(ParseContext *pc) {
Token *identifier = eat_token_if(pc, TokenIdSymbol);
if (identifier == nullptr)
return nullptr;
AstNode *type_expr = nullptr;
if (eat_token_if(pc, TokenIdColon) != nullptr)
if (eat_token_if(pc, TokenIdColon) != nullptr) {
type_expr = ast_expect(pc, ast_parse_type_expr);
}
AstNode *align_expr = ast_parse_byte_align(pc);
AstNode *expr = nullptr;
if (eat_token_if(pc, TokenIdEq) != nullptr)
expr = ast_expect(pc, ast_parse_expr);
AstNode *res = ast_create_node(pc, NodeTypeStructField, identifier);
res->data.struct_field.name = token_buf(identifier);
res->data.struct_field.type = type_expr;
res->data.struct_field.value = expr;
res->data.struct_field.align_expr = align_expr;
return res;
}

View File

@ -491,6 +491,16 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) {
return ErrorNone;
}
static ZigLLVM_EnvironmentType target_get_win32_abi() {
FILE* files[] = { stdin, stdout, stderr, nullptr };
for (int i = 0; files[i] != nullptr; i++) {
if (os_is_cygwin_pty(fileno(files[i]))) {
return ZigLLVM_GNU;
}
}
return ZigLLVM_MSVC;
}
void get_native_target(ZigTarget *target) {
// first zero initialize
*target = {};
@ -505,6 +515,9 @@ void get_native_target(ZigTarget *target) {
&target->abi,
&oformat);
target->os = get_zig_os_type(os_type);
if (target->os == OsWindows) {
target->abi = target_get_win32_abi();
}
target->is_native = true;
if (target->abi == ZigLLVM_UnknownEnvironment) {
target->abi = target_default_abi(target->arch, target->os);
@ -1601,7 +1614,7 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) {
return ZigLLVM_GNU;
case OsUefi:
case OsWindows:
return ZigLLVM_MSVC;
return ZigLLVM_MSVC;
case OsLinux:
case OsWASI:
return ZigLLVM_Musl;

View File

@ -1,8 +1,10 @@
// zig run benchmark.zig --release-fast --override-std-dir ..
const builtin = @import("builtin");
const std = @import("std");
const std = @import("../std.zig");
const time = std.time;
const Timer = time.Timer;
const crypto = @import("../crypto.zig");
const crypto = std.crypto;
const KiB = 1024;
const MiB = 1024 * KiB;
@ -14,7 +16,7 @@ const Crypto = struct {
name: []const u8,
};
const hashes = []Crypto{
const hashes = [_]Crypto{
Crypto{ .ty = crypto.Md5, .name = "md5" },
Crypto{ .ty = crypto.Sha1, .name = "sha1" },
Crypto{ .ty = crypto.Sha256, .name = "sha256" },
@ -45,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: var, comptime bytes: comptime_int) !u64 {
return throughput;
}
const macs = []Crypto{
const macs = [_]Crypto{
Crypto{ .ty = crypto.Poly1305, .name = "poly1305" },
Crypto{ .ty = crypto.HmacMd5, .name = "hmac-md5" },
Crypto{ .ty = crypto.HmacSha1, .name = "hmac-sha1" },
@ -75,7 +77,7 @@ pub fn benchmarkMac(comptime Mac: var, comptime bytes: comptime_int) !u64 {
return throughput;
}
const exchanges = []Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }};
const exchanges = [_]Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }};
pub fn benchmarkKeyExchange(comptime DhKeyExchange: var, comptime exchange_count: comptime_int) !u64 {
std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length);
@ -135,13 +137,16 @@ pub fn main() !void {
var buffer: [1024]u8 = undefined;
var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
const args = try std.os.argsAlloc(&fixed.allocator);
const args = try std.process.argsAlloc(&fixed.allocator);
var filter: ?[]u8 = "";
var i: usize = 1;
while (i < args.len) : (i += 1) {
if (std.mem.eql(u8, args[i], "--seed")) {
if (std.mem.eql(u8, args[i], "--mode")) {
try stdout.print("{}\n", builtin.mode);
return;
} else if (std.mem.eql(u8, args[i], "--seed")) {
i += 1;
if (i == args.len) {
usage();

View File

@ -269,8 +269,8 @@ pub const Blake2b512 = Blake2b(512);
fn Blake2b(comptime out_len: usize) type {
return struct {
const Self = @This();
const block_length = 128;
const digest_length = out_len / 8;
pub const block_length = 128;
pub const digest_length = out_len / 8;
const iv = [8]u64{
0x6a09e667f3bcc908,

View File

@ -420,8 +420,8 @@ pub const Sha512 = Sha2_64(Sha512Params);
fn Sha2_64(comptime params: Sha2Params64) type {
return struct {
const Self = @This();
const block_length = 128;
const digest_length = params.out_len / 8;
pub const block_length = 128;
pub const digest_length = params.out_len / 8;
s: [8]u64,
// Streaming Cache

273
std/hash/benchmark.zig Normal file
View File

@ -0,0 +1,273 @@
// zig run benchmark.zig --release-fast --override-std-dir ..
const builtin = @import("builtin");
const std = @import("std");
const time = std.time;
const Timer = time.Timer;
const hash = std.hash;
const KiB = 1024;
const MiB = 1024 * KiB;
const GiB = 1024 * MiB;
var prng = std.rand.DefaultPrng.init(0);
const Hash = struct {
ty: type,
name: []const u8,
has_iterative_api: bool = true,
init_u8s: ?[]const u8 = null,
init_u64: ?u64 = null,
};
const siphash_key = "0123456789abcdef";
const hashes = [_]Hash{
Hash{
.ty = hash.Wyhash,
.name = "wyhash",
.init_u64 = 0,
},
Hash{
.ty = hash.SipHash64(1, 3),
.name = "siphash(1,3)",
.init_u8s = siphash_key,
},
Hash{
.ty = hash.SipHash64(2, 4),
.name = "siphash(2,4)",
.init_u8s = siphash_key,
},
Hash{
.ty = hash.Fnv1a_64,
.name = "fnv1a",
},
Hash{
.ty = hash.Adler32,
.name = "adler32",
},
Hash{
.ty = hash.crc.Crc32WithPoly(.IEEE),
.name = "crc32-slicing-by-8",
},
Hash{
.ty = hash.crc.Crc32SmallWithPoly(.IEEE),
.name = "crc32-half-byte-lookup",
},
Hash{
.ty = hash.CityHash32,
.name = "cityhash-32",
.has_iterative_api = false,
},
Hash{
.ty = hash.CityHash64,
.name = "cityhash-64",
.has_iterative_api = false,
},
Hash{
.ty = hash.Murmur2_32,
.name = "murmur2-32",
.has_iterative_api = false,
},
Hash{
.ty = hash.Murmur2_64,
.name = "murmur2-64",
.has_iterative_api = false,
},
Hash{
.ty = hash.Murmur3_32,
.name = "murmur3-32",
.has_iterative_api = false,
},
};
const Result = struct {
hash: u64,
throughput: u64,
};
const block_size: usize = 8192;
pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
var h = blk: {
if (H.init_u8s) |init| {
break :blk H.ty.init(init);
}
if (H.init_u64) |init| {
break :blk H.ty.init(init);
}
break :blk H.ty.init();
};
var block: [block_size]u8 = undefined;
prng.random.bytes(block[0..]);
var offset: usize = 0;
var timer = try Timer.start();
const start = timer.lap();
while (offset < bytes) : (offset += block.len) {
h.update(block[0..]);
}
const end = timer.read();
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
return Result{
.hash = h.final(),
.throughput = throughput,
};
}
pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !Result {
const key_count = bytes / key_size;
var block: [block_size]u8 = undefined;
prng.random.bytes(block[0..]);
var i: usize = 0;
var timer = try Timer.start();
const start = timer.lap();
var sum: u64 = 0;
while (i < key_count) : (i += 1) {
const small_key = block[0..key_size];
sum +%= blk: {
if (H.init_u8s) |init| {
break :blk H.ty.hash(init, small_key);
}
if (H.init_u64) |init| {
break :blk H.ty.hash(init, small_key);
}
break :blk H.ty.hash(small_key);
};
}
const end = timer.read();
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
return Result{
.hash = sum,
.throughput = throughput,
};
}
fn usage() void {
std.debug.warn(
\\throughput_test [options]
\\
\\Options:
\\ --filter [test-name]
\\ --seed [int]
\\ --count [int]
\\ --key-size [int]
\\ --iterative-only
\\ --help
\\
);
}
fn mode(comptime x: comptime_int) comptime_int {
return if (builtin.mode == builtin.Mode.Debug) x / 64 else x;
}
// TODO(#1358): Replace with builtin formatted padding when available.
fn printPad(stdout: var, s: []const u8) !void {
var i: usize = 0;
while (i < 12 - s.len) : (i += 1) {
try stdout.print(" ");
}
try stdout.print("{}", s);
}
pub fn main() !void {
var stdout_file = try std.io.getStdOut();
var stdout_out_stream = stdout_file.outStream();
const stdout = &stdout_out_stream.stream;
var buffer: [1024]u8 = undefined;
var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
const args = try std.process.argsAlloc(&fixed.allocator);
var filter: ?[]u8 = "";
var count: usize = mode(128 * MiB);
var key_size: usize = 32;
var seed: u32 = 0;
var test_iterative_only = false;
var i: usize = 1;
while (i < args.len) : (i += 1) {
if (std.mem.eql(u8, args[i], "--mode")) {
try stdout.print("{}\n", builtin.mode);
return;
} else if (std.mem.eql(u8, args[i], "--seed")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
seed = try std.fmt.parseUnsigned(u32, args[i], 10);
// we seed later
} else if (std.mem.eql(u8, args[i], "--filter")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
filter = args[i];
} else if (std.mem.eql(u8, args[i], "--count")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
const c = try std.fmt.parseUnsigned(usize, args[i], 10);
count = c * MiB;
} else if (std.mem.eql(u8, args[i], "--key-size")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
key_size = try std.fmt.parseUnsigned(usize, args[i], 10);
if (key_size > block_size) {
try stdout.print("key_size cannot exceed block size of {}\n", block_size);
std.os.exit(1);
}
} else if (std.mem.eql(u8, args[i], "--iterative-only")) {
test_iterative_only = true;
} else if (std.mem.eql(u8, args[i], "--help")) {
usage();
return;
} else {
usage();
std.os.exit(1);
}
}
inline for (hashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
if (!test_iterative_only or H.has_iterative_api) {
try stdout.print("{}\n", H.name);
// Always reseed prior to every call so we are hashing the same buffer contents.
// This allows easier comparison between different implementations.
if (H.has_iterative_api) {
prng.seed(seed);
const result = try benchmarkHash(H, count);
try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash);
}
if (!test_iterative_only) {
prng.seed(seed);
const result_small = try benchmarkHashSmallKeys(H, key_size, count);
try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash);
}
}
}
}
}

View File

@ -9,17 +9,17 @@ const std = @import("../std.zig");
const debug = std.debug;
const testing = std.testing;
pub const Polynomial = struct {
const IEEE = 0xedb88320;
const Castagnoli = 0x82f63b78;
const Koopman = 0xeb31d82e;
pub const Polynomial = enum(u32) {
IEEE = 0xedb88320,
Castagnoli = 0x82f63b78,
Koopman = 0xeb31d82e,
};
// IEEE is by far the most common CRC and so is aliased by default.
pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);
pub const Crc32 = Crc32WithPoly(.IEEE);
// slicing-by-8 crc32 implementation.
pub fn Crc32WithPoly(comptime poly: u32) type {
pub fn Crc32WithPoly(comptime poly: Polynomial) type {
return struct {
const Self = @This();
const lookup_tables = comptime block: {
@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ poly;
crc = (crc >> 1) ^ @enumToInt(poly);
} else {
crc = (crc >> 1);
}
@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
}
test "crc32 ieee" {
const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
const Crc32Ieee = Crc32WithPoly(.IEEE);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
@ -108,7 +108,7 @@ test "crc32 ieee" {
}
test "crc32 castagnoli" {
const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
@ -116,7 +116,7 @@ test "crc32 castagnoli" {
}
// half-byte lookup table implementation.
pub fn Crc32SmallWithPoly(comptime poly: u32) type {
pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
return struct {
const Self = @This();
const lookup_table = comptime block: {
@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ poly;
crc = (crc >> 1) ^ @enumToInt(poly);
} else {
crc = (crc >> 1);
}
@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
}
test "small crc32 ieee" {
const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
@ -172,7 +172,7 @@ test "small crc32 ieee" {
}
test "small crc32 castagnoli" {
const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);

View File

@ -152,8 +152,8 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
pub fn hash(key: []const u8, input: []const u8) T {
var c = Self.init(key);
c.update(input);
return c.final();
@inlineCall(c.update, input);
return @inlineCall(c.final);
}
};
}

View File

@ -1,148 +0,0 @@
const builtin = @import("builtin");
const std = @import("std");
const time = std.time;
const Timer = time.Timer;
const hash = std.hash;
const KiB = 1024;
const MiB = 1024 * KiB;
const GiB = 1024 * MiB;
var prng = std.rand.DefaultPrng.init(0);
const Hash = struct {
ty: type,
name: []const u8,
init_u8s: ?[]const u8 = null,
init_u64: ?u64 = null,
};
const siphash_key = "0123456789abcdef";
const hashes = [_]Hash{
Hash{ .ty = hash.Wyhash, .name = "wyhash", .init_u64 = 0 },
Hash{ .ty = hash.SipHash64(1, 3), .name = "siphash(1,3)", .init_u8s = siphash_key },
Hash{ .ty = hash.SipHash64(2, 4), .name = "siphash(2,4)", .init_u8s = siphash_key },
Hash{ .ty = hash.Fnv1a_64, .name = "fnv1a" },
Hash{ .ty = hash.Crc32, .name = "crc32" },
};
const Result = struct {
hash: u64,
throughput: u64,
};
pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
var h = blk: {
if (H.init_u8s) |init| {
break :blk H.ty.init(init);
}
if (H.init_u64) |init| {
break :blk H.ty.init(init);
}
break :blk H.ty.init();
};
var block: [8192]u8 = undefined;
prng.random.bytes(block[0..]);
var offset: usize = 0;
var timer = try Timer.start();
const start = timer.lap();
while (offset < bytes) : (offset += block.len) {
h.update(block[0..]);
}
const end = timer.read();
const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
return Result{
.hash = h.final(),
.throughput = throughput,
};
}
fn usage() void {
std.debug.warn(
\\throughput_test [options]
\\
\\Options:
\\ --filter [test-name]
\\ --seed [int]
\\ --count [int]
\\ --help
\\
);
}
fn mode(comptime x: comptime_int) comptime_int {
return if (builtin.mode == builtin.Mode.Debug) x / 64 else x;
}
// TODO(#1358): Replace with builtin formatted padding when available.
fn printPad(stdout: var, s: []const u8) !void {
var i: usize = 0;
while (i < 12 - s.len) : (i += 1) {
try stdout.print(" ");
}
try stdout.print("{}", s);
}
pub fn main() !void {
var stdout_file = try std.io.getStdOut();
var stdout_out_stream = stdout_file.outStream();
const stdout = &stdout_out_stream.stream;
var buffer: [1024]u8 = undefined;
var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
const args = try std.process.argsAlloc(&fixed.allocator);
var filter: ?[]u8 = "";
var count: usize = mode(128 * MiB);
var i: usize = 1;
while (i < args.len) : (i += 1) {
if (std.mem.eql(u8, args[i], "--seed")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
const seed = try std.fmt.parseUnsigned(u32, args[i], 10);
prng.seed(seed);
} else if (std.mem.eql(u8, args[i], "--filter")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
filter = args[i];
} else if (std.mem.eql(u8, args[i], "--count")) {
i += 1;
if (i == args.len) {
usage();
std.os.exit(1);
}
const c = try std.fmt.parseUnsigned(u32, args[i], 10);
count = c * MiB;
} else if (std.mem.eql(u8, args[i], "--help")) {
usage();
return;
} else {
usage();
std.os.exit(1);
}
}
inline for (hashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
const result = try benchmarkHash(H, count);
try printPad(stdout, H.name);
try stdout.print(": {:4} MiB/s [{:16}]\n", result.throughput / (1 * MiB), result.hash);
}
}
}

View File

@ -116,8 +116,8 @@ pub const Wyhash = struct {
pub fn hash(seed: u64, input: []const u8) u64 {
var c = Wyhash.init(seed);
c.update(input);
return c.final();
@inlineCall(c.update, input);
return @inlineCall(c.final);
}
};

View File

@ -65,7 +65,7 @@ pub const CreateFileError = error{
InvalidUtf8,
/// On Windows, file paths cannot contain these characters:
/// '/', '*', '?', '"', '<', '>', '|'
/// '*', '?', '"', '<', '>', '|', and '/' (when the ABI is not GNU)
BadPathName,
Unexpected,
@ -836,11 +836,10 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16)
// > converting the name to an NT-style name, except when using the "\\?\"
// > prefix as detailed in the following sections.
// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation
// Because we want the larger maximum path length for absolute paths, we
// disallow forward slashes in zig std lib file functions on Windows.
for (s) |byte| {
switch (byte) {
'/', '*', '?', '"', '<', '>', '|' => return error.BadPathName,
'*', '?', '"', '<', '>', '|' => return error.BadPathName,
'/' => if (builtin.abi == .msvc) return error.BadPathName,
else => {},
}
}

View File

@ -761,6 +761,7 @@ pub const Node = struct {
name_token: TokenIndex,
type_expr: ?*Node,
value_expr: ?*Node,
align_expr: ?*Node,
pub fn iterate(self: *ContainerField, index: usize) ?*Node {
var i = index;

View File

@ -380,16 +380,18 @@ fn parseVarDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
return &node.base;
}
/// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)?
/// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)?
fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
const name_token = eatToken(it, .Identifier) orelse return null;
const type_expr = if (eatToken(it, .Colon)) |_|
try expectNode(arena, it, tree, parseTypeExpr, AstError{
var align_expr: ?*Node = null;
var type_expr: ?*Node = null;
if (eatToken(it, .Colon)) |_| {
type_expr = try expectNode(arena, it, tree, parseTypeExpr, AstError{
.ExpectedTypeExpr = AstError.ExpectedTypeExpr{ .token = it.index },
})
else
null;
});
align_expr = try parseByteAlign(arena, it, tree);
}
const value_expr = if (eatToken(it, .Equal)) |_|
try expectNode(arena, it, tree, parseExpr, AstError{
@ -406,6 +408,7 @@ fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
.name_token = name_token,
.type_expr = type_expr,
.value_expr = value_expr,
.align_expr = align_expr,
};
return &node.base;
}

View File

@ -166,6 +166,15 @@ test "zig fmt: doc comments on param decl" {
);
}
test "zig fmt: aligned struct field" {
try testCanonical(
\\pub const S = struct {
\\ f: i32 align(32),
\\};
\\
);
}
test "zig fmt: preserve space between async fn definitions" {
try testCanonical(
\\async fn a() void {}

View File

@ -206,7 +206,20 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i
} else if (field.type_expr != null and field.value_expr == null) {
try renderToken(tree, stream, field.name_token, indent, start_col, Space.None); // name
try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // :
return renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type,
if (field.align_expr) |align_value_expr| {
try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Space); // type
const lparen_token = tree.prevToken(align_value_expr.firstToken());
const align_kw = tree.prevToken(lparen_token);
const rparen_token = tree.nextToken(align_value_expr.lastToken());
try renderToken(tree, stream, align_kw, indent, start_col, Space.None); // align
try renderToken(tree, stream, lparen_token, indent, start_col, Space.None); // (
try renderExpression(allocator, stream, tree, indent, start_col, align_value_expr, Space.None); // alignment
try renderToken(tree, stream, rparen_token, indent, start_col, Space.Comma); // )
} else {
try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type,
}
} else if (field.type_expr == null and field.value_expr != null) {
try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name
try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // =