authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-08 16:50:16-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-08 16:50:16-05:00
log027a0c46aef9828ab07d6342030c1ba5b5e4f769
treee796bb65af29fa7ce53a0076e4c0b3e6932385fd
parent1ca983dbd7af661d3f1f840138ecba9dc0a6e372
parent4d5d0d3adad09e9ce34d281327c424de6402fcdb

Merge remote-tracking branch 'origin/master' into llvm8


14 files changed, 365 insertions(+), 26 deletions(-)

doc/langref.html.in+12
......@@ -5316,6 +5316,18 @@ comptime {
53165316 </p>
53175317 {#header_close#}
53185318
5319 {#header_open|@bitreverse#}
5320 <pre>{#syntax#}@bitreverse(comptime T: type, value: T) T{#endsyntax#}</pre>
5321 <p>{#syntax#}T{#endsyntax#} accepts any integer type.</p>
5322 <p>
5323 Reverses the bitpattern of an integer value, including the sign bit if applicable.
5324 </p>
5325 <p>
5326 For example 0b10110110 ({#syntax#}u8 = 182{#endsyntax#}, {#syntax#}i8 = -74{#endsyntax#})
5327 becomes 0b01101101 ({#syntax#}u8 = 109{#endsyntax#}, {#syntax#}i8 = 109{#endsyntax#}).
5328 </p>
5329 {#header_close#}
5330
53195331 {#header_open|@byteOffsetOf#}
53205332 <pre>{#syntax#}@byteOffsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre>
53215333 <p>
src/all_types.hpp+14
......@@ -1166,6 +1166,7 @@ struct ZigTypeFn {
11661166 FnGenParamInfo *gen_param_info;
11671167
11681168 LLVMTypeRef raw_type_ref;
1169 ZigLLVMDIType *raw_di_type;
11691170
11701171 ZigType *bound_fn_parent;
11711172};
......@@ -1415,6 +1416,7 @@ enum BuiltinFnId {
14151416 BuiltinFnIdAtomicRmw,
14161417 BuiltinFnIdAtomicLoad,
14171418 BuiltinFnIdBswap,
1419 BuiltinFnIdBitReverse,
14181420};
14191421
14201422struct BuiltinFnEntry {
......@@ -1488,6 +1490,7 @@ enum ZigLLVMFnId {
14881490 ZigLLVMFnIdCeil,
14891491 ZigLLVMFnIdSqrt,
14901492 ZigLLVMFnIdBswap,
1493 ZigLLVMFnIdBitReverse,
14911494};
14921495
14931496enum AddSubMul {
......@@ -1520,6 +1523,9 @@ struct ZigLLVMFnKey {
15201523 struct {
15211524 uint32_t bit_count;
15221525 } bswap;
1526 struct {
1527 uint32_t bit_count;
1528 } bit_reverse;
15231529 } data;
15241530};
15251531
......@@ -2162,6 +2168,7 @@ enum IrInstructionId {
21622168 IrInstructionIdMarkErrRetTracePtr,
21632169 IrInstructionIdSqrt,
21642170 IrInstructionIdBswap,
2171 IrInstructionIdBitReverse,
21652172 IrInstructionIdErrSetCast,
21662173 IrInstructionIdToBytes,
21672174 IrInstructionIdFromBytes,
......@@ -3262,6 +3269,13 @@ struct IrInstructionBswap {
32623269 IrInstruction *op;
32633270};
32643271
3272struct IrInstructionBitReverse {
3273 IrInstruction base;
3274
3275 IrInstruction *type;
3276 IrInstruction *op;
3277};
3278
32653279static const size_t slice_ptr_index = 0;
32663280static const size_t slice_len_index = 1;
32673281
src/analyze.cpp+8-1
......@@ -1220,7 +1220,10 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
12201220 fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
12211221 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
12221222 fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
1223 fn_type->di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
1223 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
1224 fn_type->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
1225 LLVMStoreSizeOfType(g->target_data_ref, fn_type->type_ref),
1226 LLVMABIAlignmentOfType(g->target_data_ref, fn_type->type_ref), "");
12241227 }
12251228
12261229 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
......@@ -6121,6 +6124,8 @@ uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey x) {
61216124 return (uint32_t)(x.data.floating.bit_count) * (uint32_t)2225366385;
61226125 case ZigLLVMFnIdBswap:
61236126 return (uint32_t)(x.data.bswap.bit_count) * (uint32_t)3661994335;
6127 case ZigLLVMFnIdBitReverse:
6128 return (uint32_t)(x.data.bit_reverse.bit_count) * (uint32_t)2621398431;
61246129 case ZigLLVMFnIdOverflowArithmetic:
61256130 return ((uint32_t)(x.data.overflow_arithmetic.bit_count) * 87135777) +
61266131 ((uint32_t)(x.data.overflow_arithmetic.add_sub_mul) * 31640542) +
......@@ -6141,6 +6146,8 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
61416146 return a.data.pop_count.bit_count == b.data.pop_count.bit_count;
61426147 case ZigLLVMFnIdBswap:
61436148 return a.data.bswap.bit_count == b.data.bswap.bit_count;
6149 case ZigLLVMFnIdBitReverse:
6150 return a.data.bit_reverse.bit_count == b.data.bit_reverse.bit_count;
61446151 case ZigLLVMFnIdFloor:
61456152 case ZigLLVMFnIdCeil:
61466153 case ZigLLVMFnIdSqrt:
src/bigint.cpp+1
......@@ -1722,3 +1722,4 @@ void bigint_incr(BigInt *x) {
17221722
17231723 bigint_add(x, &copy, &one);
17241724}
1725
src/codegen.cpp+17-1
......@@ -649,7 +649,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
649649 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
650650 fn_di_scope, buf_ptr(&fn_table_entry->symbol_name), "",
651651 import->di_file, line_number,
652 fn_table_entry->type_entry->di_type, is_internal_linkage,
652 fn_table_entry->type_entry->data.fn.raw_di_type, is_internal_linkage,
653653 is_definition, scope_line, flags, is_optimized, nullptr);
654654
655655 scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
......@@ -3789,6 +3789,11 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
37893789 n_args = 1;
37903790 key.id = ZigLLVMFnIdBswap;
37913791 key.data.bswap.bit_count = (uint32_t)int_type->data.integral.bit_count;
3792 } else if (fn_id == BuiltinFnIdBitReverse) {
3793 fn_name = "bitreverse";
3794 n_args = 1;
3795 key.id = ZigLLVMFnIdBitReverse;
3796 key.data.bit_reverse.bit_count = (uint32_t)int_type->data.integral.bit_count;
37923797 } else {
37933798 zig_unreachable();
37943799 }
......@@ -5096,6 +5101,14 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInst
50965101 return LLVMBuildTrunc(g->builder, shifted, int_type->type_ref, "");
50975102}
50985103
5104static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutable *executable, IrInstructionBitReverse *instruction) {
5105 LLVMValueRef op = ir_llvm_value(g, instruction->op);
5106 ZigType *int_type = instruction->base.value.type;
5107 assert(int_type->id == ZigTypeIdInt);
5108 LLVMValueRef fn_val = get_int_builtin_fn(g, instruction->base.value.type, BuiltinFnIdBitReverse);
5109 return LLVMBuildCall(g->builder, fn_val, &op, 1, "");
5110}
5111
50995112static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
51005113 AstNode *source_node = instruction->source_node;
51015114 Scope *scope = instruction->scope;
......@@ -5335,6 +5348,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
53355348 return ir_render_sqrt(g, executable, (IrInstructionSqrt *)instruction);
53365349 case IrInstructionIdBswap:
53375350 return ir_render_bswap(g, executable, (IrInstructionBswap *)instruction);
5351 case IrInstructionIdBitReverse:
5352 return ir_render_bit_reverse(g, executable, (IrInstructionBitReverse *)instruction);
53385353 }
53395354 zig_unreachable();
53405355}
......@@ -6758,6 +6773,7 @@ static void define_builtin_fns(CodeGen *g) {
67586773 create_builtin_fn(g, BuiltinFnIdFromBytes, "bytesToSlice", 2);
67596774 create_builtin_fn(g, BuiltinFnIdThis, "This", 0);
67606775 create_builtin_fn(g, BuiltinFnIdBswap, "bswap", 2);
6776 create_builtin_fn(g, BuiltinFnIdBitReverse, "bitreverse", 2);
67616777}
67626778
67636779static const char *bool_to_str(bool b) {
src/ir.cpp+128-20
......@@ -861,6 +861,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionBswap *) {
861861 return IrInstructionIdBswap;
862862}
863863
864static constexpr IrInstructionId ir_instruction_id(IrInstructionBitReverse *) {
865 return IrInstructionIdBitReverse;
866}
867
864868static constexpr IrInstructionId ir_instruction_id(IrInstructionCheckRuntimeScope *) {
865869 return IrInstructionIdCheckRuntimeScope;
866870}
......@@ -2721,6 +2725,17 @@ static IrInstruction *ir_build_bswap(IrBuilder *irb, Scope *scope, AstNode *sour
27212725 return &instruction->base;
27222726}
27232727
2728static IrInstruction *ir_build_bit_reverse(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type, IrInstruction *op) {
2729 IrInstructionBitReverse *instruction = ir_build_instruction<IrInstructionBitReverse>(irb, scope, source_node);
2730 instruction->type = type;
2731 instruction->op = op;
2732
2733 if (type != nullptr) ir_ref_instruction(type, irb->current_basic_block);
2734 ir_ref_instruction(op, irb->current_basic_block);
2735
2736 return &instruction->base;
2737}
2738
27242739static IrInstruction *ir_build_check_runtime_scope(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *scope_is_comptime, IrInstruction *is_comptime) {
27252740 IrInstructionCheckRuntimeScope *instruction = ir_build_instruction<IrInstructionCheckRuntimeScope>(irb, scope, source_node);
27262741 instruction->scope_is_comptime = scope_is_comptime;
......@@ -3646,7 +3661,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
36463661 Buf *name = fn_ref_expr->data.symbol_expr.symbol;
36473662 auto entry = irb->codegen->builtin_fn_table.maybe_get(name);
36483663
3649 if (!entry) {
3664 if (!entry) { // new built in not found
36503665 add_node_error(irb->codegen, node,
36513666 buf_sprintf("invalid builtin function: '%s'", buf_ptr(name)));
36523667 return irb->codegen->invalid_instruction;
......@@ -4720,6 +4735,21 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47204735 IrInstruction *result = ir_build_bswap(irb, scope, node, arg0_value, arg1_value);
47214736 return ir_lval_wrap(irb, scope, result, lval);
47224737 }
4738 case BuiltinFnIdBitReverse:
4739 {
4740 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4741 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4742 if (arg0_value == irb->codegen->invalid_instruction)
4743 return arg0_value;
4744
4745 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4746 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4747 if (arg1_value == irb->codegen->invalid_instruction)
4748 return arg1_value;
4749
4750 IrInstruction *result = ir_build_bit_reverse(irb, scope, node, arg0_value, arg1_value);
4751 return ir_lval_wrap(irb, scope, result, lval);
4752 }
47234753 }
47244754 zig_unreachable();
47254755}
......@@ -14547,8 +14577,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1454714577 return ira->codegen->invalid_instruction;
1454814578 if (type_is_invalid(struct_val->type))
1454914579 return ira->codegen->invalid_instruction;
14550 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];
14551 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type,
14580 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry,
1455214581 is_const, is_volatile, PtrLenSingle, align_bytes,
1455314582 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
1455414583 (uint32_t)host_int_bytes_for_result_type);
......@@ -16853,16 +16882,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind
1685316882
1685416883static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) {
1685516884 Error err;
16856 static ConstExprValue *type_info_var = nullptr; // TODO oops this global variable made it past code review
16857 static ZigType *type_info_type = nullptr; // TODO oops this global variable made it past code review
16858 if (type_info_var == nullptr) {
16859 type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
16860 assert(type_info_var->type->id == ZigTypeIdMetaType);
16885 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
16886 assert(type_info_var->type->id == ZigTypeIdMetaType);
16887 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));
1686116888
16862 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));
16863 type_info_type = type_info_var->data.x_type;
16864 assert(type_info_type->id == ZigTypeIdUnion);
16865 }
16889 ZigType *type_info_type = type_info_var->data.x_type;
16890 assert(type_info_type->id == ZigTypeIdUnion);
1686616891
1686716892 if (type_name == nullptr && root == nullptr)
1686816893 return type_info_type;
......@@ -17075,12 +17100,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1707517100 ensure_field_index(fn_def_val->type, "return_type", 7);
1707617101 fn_def_fields[7].special = ConstValSpecialStatic;
1707717102 fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;
17078 if (fn_entry->src_implicit_return_type != nullptr)
17079 fn_def_fields[7].data.x_type = fn_entry->src_implicit_return_type;
17080 else if (fn_entry->type_entry->data.fn.gen_return_type != nullptr)
17081 fn_def_fields[7].data.x_type = fn_entry->type_entry->data.fn.gen_return_type;
17082 else
17083 fn_def_fields[7].data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
17103 fn_def_fields[7].data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
1708417104 // arg_names: [][] const u8
1708517105 ensure_field_index(fn_def_val->type, "arg_names", 8);
1708617106 size_t fn_arg_count = fn_entry->variable_list.length;
......@@ -20150,8 +20170,29 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t
2015020170 val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn);
2015120171 return ErrorNone;
2015220172 }
20153 case ZigTypeIdArray:
20154 zig_panic("TODO buf_read_value_bytes array type");
20173 case ZigTypeIdArray: {
20174 uint64_t elem_size = type_size(ira->codegen, val->type->data.array.child_type);
20175 size_t len = val->type->data.array.len;
20176
20177 switch (val->data.x_array.special) {
20178 case ConstArraySpecialNone:
20179 val->data.x_array.data.s_none.elements = create_const_vals(len);
20180 for (size_t i = 0; i < len; i++) {
20181 ConstExprValue *elem = &val->data.x_array.data.s_none.elements[i];
20182 elem->special = ConstValSpecialStatic;
20183 elem->type = val->type->data.array.child_type;
20184 if ((err = buf_read_value_bytes(ira, source_node, buf + (elem_size * i), elem)))
20185 return err;
20186 }
20187 break;
20188 case ConstArraySpecialUndef:
20189 zig_panic("TODO buf_read_value_bytes ConstArraySpecialUndef array type");
20190 case ConstArraySpecialBuf:
20191 zig_panic("TODO buf_read_value_bytes ConstArraySpecialBuf array type");
20192 }
20193
20194 return ErrorNone;
20195 }
2015520196 case ZigTypeIdStruct:
2015620197 switch (val->type->data.structure.layout) {
2015720198 case ContainerLayoutAuto: {
......@@ -20333,6 +20374,7 @@ static IrInstruction *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstru
2033320374
2033420375 IrInstruction *result = ir_const(ira, &instruction->base, dest_type);
2033520376 result->value.data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
20377 result->value.data.x_ptr.mut = ConstPtrMutRuntimeVar;
2033620378 result->value.data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&val->data.x_bigint);
2033720379 return result;
2033820380 }
......@@ -21115,6 +21157,69 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction
2111521157 return result;
2111621158}
2111721159
21160static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstructionBitReverse *instruction) {
21161 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);
21162 if (type_is_invalid(int_type))
21163 return ira->codegen->invalid_instruction;
21164
21165 IrInstruction *op = instruction->op->child;
21166 if (type_is_invalid(op->value.type))
21167 return ira->codegen->invalid_instruction;
21168
21169 if (int_type->id != ZigTypeIdInt) {
21170 ir_add_error(ira, instruction->type,
21171 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
21172 return ira->codegen->invalid_instruction;
21173 }
21174
21175 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
21176 if (type_is_invalid(casted_op->value.type))
21177 return ira->codegen->invalid_instruction;
21178
21179 if (int_type->data.integral.bit_count == 0) {
21180 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
21181 bigint_init_unsigned(&result->value.data.x_bigint, 0);
21182 return result;
21183 }
21184
21185 if (instr_is_comptime(casted_op)) {
21186 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);
21187 if (!val)
21188 return ira->codegen->invalid_instruction;
21189
21190 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
21191 size_t num_bits = int_type->data.integral.bit_count;
21192 size_t buf_size = (num_bits + 7) / 8;
21193 uint8_t *comptime_buf = allocate_nonzero<uint8_t>(buf_size);
21194 uint8_t *result_buf = allocate_nonzero<uint8_t>(buf_size);
21195 memset(comptime_buf,0,buf_size);
21196 memset(result_buf,0,buf_size);
21197
21198 bigint_write_twos_complement(&val->data.x_bigint,comptime_buf,num_bits,ira->codegen->is_big_endian);
21199
21200 size_t bit_i = 0;
21201 size_t bit_rev_i = num_bits - 1;
21202 for (; bit_i < num_bits; bit_i++, bit_rev_i--) {
21203 if (comptime_buf[bit_i / 8] & (1 << (bit_i % 8))) {
21204 result_buf[bit_rev_i / 8] |= (1 << (bit_rev_i % 8));
21205 }
21206 }
21207
21208 bigint_read_twos_complement(&result->value.data.x_bigint,
21209 result_buf,
21210 int_type->data.integral.bit_count,
21211 ira->codegen->is_big_endian,
21212 int_type->data.integral.is_signed);
21213
21214 return result;
21215 }
21216
21217 IrInstruction *result = ir_build_bit_reverse(&ira->new_irb, instruction->base.scope,
21218 instruction->base.source_node, nullptr, casted_op);
21219 result->value.type = int_type;
21220 return result;
21221}
21222
2111821223
2111921224static IrInstruction *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstructionEnumToInt *instruction) {
2112021225 Error err;
......@@ -21453,6 +21558,8 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2145321558 return ir_analyze_instruction_sqrt(ira, (IrInstructionSqrt *)instruction);
2145421559 case IrInstructionIdBswap:
2145521560 return ir_analyze_instruction_bswap(ira, (IrInstructionBswap *)instruction);
21561 case IrInstructionIdBitReverse:
21562 return ir_analyze_instruction_bit_reverse(ira, (IrInstructionBitReverse *)instruction);
2145621563 case IrInstructionIdIntToErr:
2145721564 return ir_analyze_instruction_int_to_err(ira, (IrInstructionIntToErr *)instruction);
2145821565 case IrInstructionIdErrToInt:
......@@ -21675,6 +21782,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2167521782 case IrInstructionIdPromiseResultType:
2167621783 case IrInstructionIdSqrt:
2167721784 case IrInstructionIdBswap:
21785 case IrInstructionIdBitReverse:
2167821786 case IrInstructionIdAtomicLoad:
2167921787 case IrInstructionIdIntCast:
2168021788 case IrInstructionIdFloatCast:
src/ir_print.cpp+15
......@@ -1335,6 +1335,18 @@ static void ir_print_bswap(IrPrint *irp, IrInstructionBswap *instruction) {
13351335 fprintf(irp->f, ")");
13361336}
13371337
1338static void ir_print_bit_reverse(IrPrint *irp, IrInstructionBitReverse *instruction) {
1339 fprintf(irp->f, "@bitreverse(");
1340 if (instruction->type != nullptr) {
1341 ir_print_other_instruction(irp, instruction->type);
1342 } else {
1343 fprintf(irp->f, "null");
1344 }
1345 fprintf(irp->f, ",");
1346 ir_print_other_instruction(irp, instruction->op);
1347 fprintf(irp->f, ")");
1348}
1349
13381350static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
13391351 ir_print_prefix(irp, instruction);
13401352 switch (instruction->id) {
......@@ -1751,6 +1763,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17511763 case IrInstructionIdBswap:
17521764 ir_print_bswap(irp, (IrInstructionBswap *)instruction);
17531765 break;
1766 case IrInstructionIdBitReverse:
1767 ir_print_bit_reverse(irp, (IrInstructionBitReverse *)instruction);
1768 break;
17541769 case IrInstructionIdAtomicLoad:
17551770 ir_print_atomic_load(irp, (IrInstructionAtomicLoad *)instruction);
17561771 break;
std/c/freebsd.zig+27
......@@ -22,6 +22,8 @@ pub extern "c" fn openat(fd: c_int, path: ?[*]const u8, flags: c_int) c_int;
2222pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int;
2323pub extern "c" fn setuid(uid: c_uint) c_int;
2424pub extern "c" fn kill(pid: c_int, sig: c_int) c_int;
25pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
26pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
2527
2628/// Renamed from `kevent` to `Kevent` to avoid conflict with function name.
2729pub const Kevent = extern struct {
......@@ -91,3 +93,28 @@ pub const dirent = extern struct {
9193 d_pad1: u16,
9294 d_name: [256]u8,
9395};
96
97pub const in_port_t = u16;
98pub const sa_family_t = u16;
99
100pub const sockaddr = extern union {
101 in: sockaddr_in,
102 in6: sockaddr_in6,
103};
104
105pub const sockaddr_in = extern struct {
106 len: u8,
107 family: sa_family_t,
108 port: in_port_t,
109 addr: [16]u8,
110 zero: [8]u8,
111};
112
113pub const sockaddr_in6 = extern struct {
114 len: u8,
115 family: sa_family_t,
116 port: in_port_t,
117 flowinfo: u32,
118 addr: [16]u8,
119 scope_id: u32,
120};
std/os/freebsd/index.zig+27
......@@ -26,6 +26,21 @@ pub const PROT_READ = 1;
2626pub const PROT_WRITE = 2;
2727pub const PROT_EXEC = 4;
2828
29pub const CLOCK_REALTIME = 0;
30pub const CLOCK_VIRTUAL = 1;
31pub const CLOCK_PROF = 2;
32pub const CLOCK_MONOTONIC = 4;
33pub const CLOCK_UPTIME = 5;
34pub const CLOCK_UPTIME_PRECISE = 7;
35pub const CLOCK_UPTIME_FAST = 8;
36pub const CLOCK_REALTIME_PRECISE = 9;
37pub const CLOCK_REALTIME_FAST = 10;
38pub const CLOCK_MONOTONIC_PRECISE = 11;
39pub const CLOCK_MONOTONIC_FAST = 12;
40pub const CLOCK_SECOND = 13;
41pub const CLOCK_THREAD_CPUTIME_ID = 14;
42pub const CLOCK_PROCESS_CPUTIME_ID = 15;
43
2944pub const MAP_FAILED = maxInt(usize);
3045pub const MAP_SHARED = 0x0001;
3146pub const MAP_PRIVATE = 0x0002;
......@@ -510,6 +525,10 @@ pub const TIOCGPKT = 0x80045438;
510525pub const TIOCGPTLCK = 0x80045439;
511526pub const TIOCGEXCL = 0x80045440;
512527
528pub const sockaddr = c.sockaddr;
529pub const sockaddr_in = c.sockaddr_in;
530pub const sockaddr_in6 = c.sockaddr_in6;
531
513532fn unsigned(s: i32) u32 {
514533 return @bitCast(u32, s);
515534}
......@@ -699,6 +718,14 @@ pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
699718 return errnoWrap(c.nanosleep(req, rem));
700719}
701720
721pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
722 return errnoWrap(c.clock_gettime(clk_id, tp));
723}
724
725pub fn clock_getres(clk_id: i32, tp: *timespec) usize {
726 return errnoWrap(c.clock_getres(clk_id, tp));
727}
728
702729pub fn setuid(uid: u32) usize {
703730 return errnoWrap(c.setuid(uid));
704731}
std/os/time.zig+4-4
......@@ -61,7 +61,7 @@ pub fn timestamp() u64 {
6161/// Get the posix timestamp, UTC, in milliseconds
6262pub const milliTimestamp = switch (builtin.os) {
6363 Os.windows => milliTimestampWindows,
64 Os.linux => milliTimestampPosix,
64 Os.linux, Os.freebsd => milliTimestampPosix,
6565 Os.macosx, Os.ios => milliTimestampDarwin,
6666 else => @compileError("Unsupported OS"),
6767};
......@@ -179,7 +179,7 @@ pub const Timer = struct {
179179 debug.assert(err != windows.FALSE);
180180 self.start_time = @intCast(u64, start_time);
181181 },
182 Os.linux => {
182 Os.linux, Os.freebsd => {
183183 //On Linux, seccomp can do arbitrary things to our ability to call
184184 // syscalls, including return any errno value it wants and
185185 // inconsistently throwing errors. Since we can't account for
......@@ -215,7 +215,7 @@ pub const Timer = struct {
215215 var clock = clockNative() - self.start_time;
216216 return switch (builtin.os) {
217217 Os.windows => @divFloor(clock * ns_per_s, self.frequency),
218 Os.linux => clock,
218 Os.linux, Os.freebsd => clock,
219219 Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom),
220220 else => @compileError("Unsupported OS"),
221221 };
......@@ -236,7 +236,7 @@ pub const Timer = struct {
236236
237237 const clockNative = switch (builtin.os) {
238238 Os.windows => clockWindows,
239 Os.linux => clockLinux,
239 Os.linux, Os.freebsd => clockLinux,
240240 Os.macosx, Os.ios => clockDarwin,
241241 else => @compileError("Unsupported OS"),
242242 };
test/behavior.zig+1
......@@ -9,6 +9,7 @@ comptime {
99 _ = @import("cases/bitcast.zig");
1010 _ = @import("cases/bool.zig");
1111 _ = @import("cases/bswap.zig");
12 _ = @import("cases/bitreverse.zig");
1213 _ = @import("cases/bugs/1076.zig");
1314 _ = @import("cases/bugs/1111.zig");
1415 _ = @import("cases/bugs/1277.zig");
test/cases/bitreverse.zig created+81
......@@ -0,0 +1,81 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const minInt = std.math.minInt;
4
5test "@bitreverse" {
6 comptime testBitReverse();
7 testBitReverse();
8}
9
10fn testBitReverse() void {
11 // using comptime_ints, unsigned
12 assert(@bitreverse(u0, 0) == 0);
13 assert(@bitreverse(u5, 0x12) == 0x9);
14 assert(@bitreverse(u8, 0x12) == 0x48);
15 assert(@bitreverse(u16, 0x1234) == 0x2c48);
16 assert(@bitreverse(u24, 0x123456) == 0x6a2c48);
17 assert(@bitreverse(u32, 0x12345678) == 0x1e6a2c48);
18 assert(@bitreverse(u40, 0x123456789a) == 0x591e6a2c48);
19 assert(@bitreverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
20 assert(@bitreverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
21 assert(@bitreverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
22 assert(@bitreverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
23
24 // using runtime uints, unsigned
25 var num0: u0 = 0;
26 assert(@bitreverse(u0, num0) == 0);
27 var num5: u5 = 0x12;
28 assert(@bitreverse(u5, num5) == 0x9);
29 var num8: u8 = 0x12;
30 assert(@bitreverse(u8, num8) == 0x48);
31 var num16: u16 = 0x1234;
32 assert(@bitreverse(u16, num16) == 0x2c48);
33 var num24: u24 = 0x123456;
34 assert(@bitreverse(u24, num24) == 0x6a2c48);
35 var num32: u32 = 0x12345678;
36 assert(@bitreverse(u32, num32) == 0x1e6a2c48);
37 var num40: u40 = 0x123456789a;
38 assert(@bitreverse(u40, num40) == 0x591e6a2c48);
39 var num48: u48 = 0x123456789abc;
40 assert(@bitreverse(u48, num48) == 0x3d591e6a2c48);
41 var num56: u56 = 0x123456789abcde;
42 assert(@bitreverse(u56, num56) == 0x7b3d591e6a2c48);
43 var num64: u64 = 0x123456789abcdef1;
44 assert(@bitreverse(u64, num64) == 0x8f7b3d591e6a2c48);
45 var num128: u128 = 0x123456789abcdef11121314151617181;
46 assert(@bitreverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);
47
48 // using comptime_ints, signed, positive
49 assert(@bitreverse(i0, 0) == 0);
50 assert(@bitreverse(i8, @bitCast(i8, u8(0x92))) == @bitCast(i8, u8( 0x49)));
51 assert(@bitreverse(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16( 0x2c48)));
52 assert(@bitreverse(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24( 0x6a2c48)));
53 assert(@bitreverse(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32( 0x1e6a2c48)));
54 assert(@bitreverse(i40, @bitCast(i40, u40(0x123456789a))) == @bitCast(i40, u40( 0x591e6a2c48)));
55 assert(@bitreverse(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48( 0x3d591e6a2c48)));
56 assert(@bitreverse(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56( 0x7b3d591e6a2c48)));
57 assert(@bitreverse(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64,u64(0x8f7b3d591e6a2c48)));
58 assert(@bitreverse(i128, @bitCast(i128,u128(0x123456789abcdef11121314151617181))) == @bitCast(i128,u128(0x818e868a828c84888f7b3d591e6a2c48)));
59
60 // using comptime_ints, signed, negative. Compare to runtime ints returned from llvm.
61 var neg5: i5 = minInt(i5) + 1;
62 assert(@bitreverse(i5, minInt(i5) + 1) == @bitreverse(i5, neg5));
63 var neg8: i8 = -18;
64 assert(@bitreverse(i8, -18) == @bitreverse(i8, neg8));
65 var neg16: i16 = -32694;
66 assert(@bitreverse(i16, -32694) == @bitreverse(i16, neg16));
67 var neg24: i24 = -6773785;
68 assert(@bitreverse(i24, -6773785) == @bitreverse(i24, neg24));
69 var neg32: i32 = -16773785;
70 assert(@bitreverse(i32, -16773785) == @bitreverse(i32, neg32));
71 var neg40: i40 = minInt(i40) + 12345;
72 assert(@bitreverse(i40, minInt(i40) + 12345) == @bitreverse(i40, neg40));
73 var neg48: i48 = minInt(i48) + 12345;
74 assert(@bitreverse(i48, minInt(i48) + 12345) == @bitreverse(i48, neg48));
75 var neg56: i56 = minInt(i56) + 12345;
76 assert(@bitreverse(i56, minInt(i56) + 12345) == @bitreverse(i56, neg56));
77 var neg64: i64 = minInt(i64) + 12345;
78 assert(@bitreverse(i64, minInt(i64) + 12345) == @bitreverse(i64, neg64));
79 var neg128: i128 = minInt(i128) + 12345;
80 assert(@bitreverse(i128, minInt(i128) + 12345) == @bitreverse(i128, neg128));
81}
test/cases/inttoptr.zig+14
......@@ -11,3 +11,17 @@ fn randomAddressToFunction() void {
1111 var addr: usize = 0xdeadbeef;
1212 var ptr = @intToPtr(fn () void, addr);
1313}
14
15test "mutate through ptr initialized with constant intToPtr value" {
16 forceCompilerAnalyzeBranchHardCodedPtrDereference(false);
17}
18
19fn forceCompilerAnalyzeBranchHardCodedPtrDereference(x: bool) void {
20 const hardCodedP = @intToPtr(*volatile u8, 0xdeadbeef);
21 if (x) {
22 hardCodedP.* = hardCodedP.* | 10;
23 } else {
24 return;
25 }
26}
27
test/cases/ptrcast.zig+16
......@@ -34,3 +34,19 @@ fn testReinterpretBytesAsExternStruct() void {
3434 var val = ptr.c;
3535 assertOrPanic(val == 5);
3636}
37
38test "reinterpret struct field at comptime" {
39 const numLittle = comptime Bytes.init(0x12345678);
40 assertOrPanic(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes));
41}
42
43const Bytes = struct {
44 bytes: [4]u8,
45
46 pub fn init(v: u32) Bytes {
47 var res: Bytes = undefined;
48 @ptrCast(*align(1) u32, &res.bytes).* = v;
49
50 return res;
51 }
52};