Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use InternPool for all types and constant values #15569

Merged
merged 205 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
205 commits
Select commit Hold shift + click to select a range
9aec275
stage2: start the InternPool transition
andrewrk May 2, 2023
cac60a0
std.builtin: give some enums integer types
andrewrk May 2, 2023
c7e84dd
InternPool: flesh out some of the implementations
andrewrk May 2, 2023
00f82f1
stage2: add `interned` AIR tag
andrewrk May 2, 2023
50f3373
stage2: isGenericPoison InternPool awareness
andrewrk May 3, 2023
b125063
InternPool: implement typeHasOnePossibleValue for simple_type
andrewrk May 3, 2023
d1887ab
InternPool: implement hasRuntimeBitsAdvanced for simple_type
andrewrk May 3, 2023
cdf6acb
InternPool: implement hasWellDefinedLayout for simple_type
andrewrk May 3, 2023
264292f
InternPool: implement resolveTypeFields
andrewrk May 3, 2023
e77dede
InternPool: implement typePtrOrOptionalPtrTy
andrewrk May 3, 2023
aa1bb55
InternPool: implement isSinglePointer
andrewrk May 3, 2023
4cd8a40
stage2: move float types to InternPool
andrewrk May 3, 2023
bcd4bb8
stage2: move named int types to InternPool
andrewrk May 3, 2023
836d8a1
stage2: move most simple types to InternPool
andrewrk May 4, 2023
ca3cf93
stage2: move most simple values to InternPool
andrewrk May 4, 2023
fb16ad3
Type: update to use InternPool for some methods
andrewrk May 4, 2023
85c69c5
Type.isSlice: make it InternPool aware
andrewrk May 4, 2023
9d422bf
stage2: move all integer types to InternPool
andrewrk May 4, 2023
5e63664
stage2: move many Type encodings to InternPool
andrewrk May 5, 2023
773fabf
InternPool: add the missing pointer data
andrewrk May 5, 2023
6ab8b6f
stage2: move undef, unreach, null values to InternPool
andrewrk May 5, 2023
41cdcd5
stage2: add a few more Value checks for InternPool
andrewrk May 5, 2023
75cf06c
std.mem.alignForwardGeneric: manually inline the assertions
andrewrk May 5, 2023
ac07dda
InternPool: enhance integer values
andrewrk May 5, 2023
2f05b14
Sema: update core comptime detection logic to be InternPool aware
andrewrk May 5, 2023
70a4b76
std.builtin.AddressSpace: allocate one more bit to this enum
andrewrk May 5, 2023
9ec0017
stage2: migrate many pointer types to the InternPool
andrewrk May 5, 2023
0471638
InternPool: add a dump function
andrewrk May 6, 2023
ce3cffb
fill out more InternPool Type methods
andrewrk May 6, 2023
3e6dd0d
stage2: add tmp_hack_arena for the InternPool transition
andrewrk May 6, 2023
80bf5af
fix AIR printing of interned constants
andrewrk May 6, 2023
08e9763
stage2: add missing comptimeOnly logic for InternPool
andrewrk May 6, 2023
31aee50
InternPool: add a slice encoding
andrewrk May 6, 2023
9626811
Sema: add typeHasOnePossibleValue logic for InternPool
andrewrk May 6, 2023
6350aab
InternPool: fix bug in addLimbsExtraAssumeCapacity
andrewrk May 6, 2023
27d641e
stage2: fix interned integer value printing
andrewrk May 6, 2023
f7bd427
LLVM backend: update integer constant lowering for InternPool
andrewrk May 6, 2023
73720b6
Sema: update onePossibleValue for InternPool
andrewrk May 6, 2023
75900ec
stage2: move integer values to InternPool
andrewrk May 7, 2023
6c713b4
InternPool: add an encoding for arrays with sentinels
andrewrk May 7, 2023
9d9e1a2
InternPool: implement indexToKey and equality for int values
andrewrk May 7, 2023
c1ca16d
wip: progress towards compiling tests
mlugg May 7, 2023
4c3c605
InternPool: add getCoercedInt to avoid copy in Sema
andrewrk May 7, 2023
2ffef60
Replace uses of Value.zero, Value.one, Value.negative_one
mlugg May 7, 2023
8699cdc
InternPool: fix UAF in getCoercedInt and add u16 int value encoding
andrewrk May 7, 2023
a5fb169
stage2: bug fixes related to Type/Value/InternPool
andrewrk May 7, 2023
4d88f82
stage2: implement intTagType logic
andrewrk May 7, 2023
4fe0c58
stage2: more InternPool-related fixes
andrewrk May 7, 2023
2f9b7dc
InternPool: add an int_u8 value encoding
andrewrk May 7, 2023
3116477
stage2: move empty struct type and value to InternPool
andrewrk May 8, 2023
8587e51
stage2: more InternPool related fixes
andrewrk May 8, 2023
ad06b24
Sema: introduce Value.enum_field_0
andrewrk May 8, 2023
fd674d9
InternPool: add indexToKey for empty struct types
andrewrk May 8, 2023
68b95a3
InternPool: add ptr-to-int value
andrewrk May 8, 2023
e94a81c
InternPool: add optional values
andrewrk May 8, 2023
275652f
stage2: move opaque types to InternPool
andrewrk May 8, 2023
8297f28
stage2: move struct types and aggregate values to InternPool
andrewrk May 10, 2023
3ba099b
stage2: move union types and values to InternPool
andrewrk May 11, 2023
1c7095c
add std.hash.uint32
andrewrk May 11, 2023
50bebb9
InternPool: ability to encode enums
andrewrk May 11, 2023
404cbc3
InternPool: fix deinit leaking inner maps
andrewrk May 11, 2023
5881a2d
stage2: move enum types into the InternPool
andrewrk May 12, 2023
466328d
InternPool: transition float values
mlugg May 9, 2023
d89807e
stage2: remove legacy Type array and array_sentinel
andrewrk May 12, 2023
88dbd62
stage2: move enum tag values into the InternPool
andrewrk May 12, 2023
d18881d
stage2: move anon tuples and anon structs to InternPool
andrewrk May 15, 2023
6a9a918
stage2: encode one-possible-value tuple specially
andrewrk May 15, 2023
1788216
stage2: move function types to InternPool
andrewrk May 16, 2023
f21ca3d
compiler: move `anyframe->T` to InternPool
andrewrk May 19, 2023
607737d
compiler: eliminate legacy Type.Tag.optional
andrewrk May 19, 2023
7bf91fc
compiler: eliminate legacy Type.Tag.pointer
andrewrk May 19, 2023
9ff514b
compiler: move error union types and error set types to InternPool
andrewrk May 20, 2023
d28fc5b
InternPool: add repeated aggregate storage
jacobly0 May 20, 2023
f8b6eb6
Sema: add coerceTupleToStruct result to the InternPool
jacobly0 May 20, 2023
c473594
Sema: port reify struct access to use InternPool
jacobly0 May 20, 2023
be78a12
Sema: port Value.decl_ptr to InternPool
jacobly0 May 20, 2023
115c089
Value: add `intern` and `unintern` to facilitate code conversion
jacobly0 May 20, 2023
dfb3521
compiler: remove var_args_param_type from SimpleType
andrewrk May 20, 2023
65d65f5
Module: remove tmp_hack_arena
andrewrk May 20, 2023
7e19c95
Sema: move `inferred_alloc_const/mut_type` to InternPool
andrewrk May 21, 2023
e4d498c
InternPool: add missing ensureCapacity call with enums
andrewrk May 21, 2023
25cd4bb
Type: hack around `isNoReturn` queries for the remaining legacy tags
jacobly0 May 20, 2023
cbf304d
InternPool: fix coersion issues
jacobly0 May 20, 2023
dfd91ab
InternPool: add more pointer values
jacobly0 May 21, 2023
9584fea
InternPool: fix logic bugs
jacobly0 May 21, 2023
be1b231
InternPool: add missing logic
jacobly0 May 21, 2023
84099e5
TypedValue: fix debug print crashes
jacobly0 May 21, 2023
a6fcf46
Value: remove legacy type values
jacobly0 May 21, 2023
a7c3ca3
InternPool: add lldb pretty printing for indices
jacobly0 May 21, 2023
484c3e8
llvm: fix incorrect slice lowering
jacobly0 May 21, 2023
1b64eed
remove the kludges from std.builtin
andrewrk May 24, 2023
01ca841
Sema: improve the types_to_resolve mechanism
andrewrk May 24, 2023
2052260
Sema: update zirSliceLength to avoid resolveInst(.none)
andrewrk May 24, 2023
5555bdc
InternPool: support int->comptime_int in getCoerced
andrewrk May 24, 2023
6e0de1d
InternPool: port most of value tags
jacobly0 May 22, 2023
1a4626d
InternPool: remove more legacy values
jacobly0 May 25, 2023
72e4ea3
InternPool: fix crashes up to in progress comptime mutation
jacobly0 May 25, 2023
70cc68e
Air: remove constant tag
jacobly0 May 25, 2023
5d0d589
Sema: fix some issues with the inferred alloc tag change
jacobly0 May 26, 2023
f37c0a4
Sema: inferred allocations no longer abuse type/value system
andrewrk May 26, 2023
9a738c0
Module: intern the values of decls when they are marked alive
jacobly0 May 26, 2023
9afa974
InternPool: fix enough crashes to run `build-obj` on a simple program
jacobly0 May 26, 2023
66c4396
AIR: eliminate the `values` array
andrewrk May 26, 2023
f2c7161
InternPool: fix more crashes
jacobly0 May 26, 2023
abded5c
TypedValue: implement more prints
jacobly0 May 26, 2023
63dc044
wasm: fix error union constant lowering
andrewrk May 26, 2023
ace5a5e
llvm: simplify control flow lowering structs
andrewrk May 26, 2023
d5f0ee0
codegen: fix lowering of constant structs
andrewrk May 26, 2023
9cd0ca9
Module: rename functions to make ownership checks explicit
jacobly0 May 26, 2023
1dc01f1
InternPool: fix build-exe and compiler-rt crashes
jacobly0 May 26, 2023
e156c1c
InternPool: correct the logic for struct size dump
andrewrk May 26, 2023
270f9e2
AstGen: generate tests with anyerror!void
andrewrk May 26, 2023
8011faa
compiler: fix populateTestFunctions InternPool usage
andrewrk May 26, 2023
a596ea6
CLI: introduce --verbose-intern-pool
andrewrk May 27, 2023
fc35843
C backend: InternPool fixes
andrewrk May 27, 2023
c8b0d4d
InternPool: optimize zigTypeTag()
andrewrk May 27, 2023
2d5bc01
behavior: get more test cases passing with llvm
jacobly0 May 27, 2023
d40b83d
behavior: pass more tests on llvm again
jacobly0 May 28, 2023
3b6ca1d
Module: move memoized data to the intern pool
jacobly0 May 28, 2023
3064d2a
behavior: additional llvm fixes
jacobly0 May 28, 2023
3269256
behavior: fix more compiler crashes
jacobly0 May 29, 2023
4f70863
InternPool: fix various pointer issues
jacobly0 May 29, 2023
a702af0
x86_64: fix InternPool regressions
jacobly0 May 29, 2023
5580a69
cbe: fix InternPool regressions
jacobly0 May 29, 2023
804740a
InternPool: avoid indexToKey recursion for type_slice
andrewrk May 29, 2023
b336866
InternPool: avoid indexToKey recursion for ptr_elem,ptr_field
andrewrk May 29, 2023
f2778f7
InternPool: avoid indexToKey recursion for only_possible_value
andrewrk May 29, 2023
f7177fb
InternPool: avoid indexToKey recursion for opt_payload
andrewrk May 30, 2023
55cda9a
InternPool: avoid indexToKey recursion for ptr_slice
andrewrk May 30, 2023
66ae42b
Sema: fix pointer arithmetic on single array pointers
jacobly0 May 30, 2023
a803e9c
Sema: fix vector comparison and interning of -0
jacobly0 May 30, 2023
27f1ad8
Module: add allowzero canonicalization to pointer types
jacobly0 May 30, 2023
66f83f2
InternPool: avoid indexToKey recursion for type_enum_auto
andrewrk May 30, 2023
61978c8
InternPool: eliminate indexToKey call graph cycle
andrewrk May 30, 2023
d0cd1c8
Sema: port lazy value usage to be InternPool aware
jacobly0 May 30, 2023
f673c98
Sema: fix sus overflow behavior in RangeSetUnhandledIterator
jacobly0 May 30, 2023
494d4f9
behavior: update for different inferred error set order
jacobly0 May 30, 2023
6b81546
Type: fix `@typeName` for `undefined`
jacobly0 May 30, 2023
90a877f
InternPool: pass by const pointer
andrewrk May 30, 2023
c7d65fa
std.hash: add xxhash to benchmark and fix its API
andrewrk May 31, 2023
82f6f16
InternPool: improve hashing performance
andrewrk May 31, 2023
91fb45a
Sema: fix comptime error set comparisons
jacobly0 May 31, 2023
1430ac2
Type: fix `@sizeOf(?anyerror)`
jacobly0 May 31, 2023
99531b0
Sema: make sentinel load through array pointer comptime known
jacobly0 May 31, 2023
a0d4ef0
InternPool: add representation for value of empty enums and unions
mlugg May 31, 2023
aed142e
InternPool: further optimize Key hashing
andrewrk May 31, 2023
d019229
Sema: avoid invalided key access
jacobly0 May 31, 2023
26fac15
math.big.int: fix ctz of zero
jacobly0 May 31, 2023
71c4077
Value: fix null test for c pointers
jacobly0 May 31, 2023
b2391a7
Sema: remove opv status from arrays with sentinels
jacobly0 May 31, 2023
870e384
Sema: elide comptime-checked slice safety
andrewrk May 31, 2023
bb52642
InternPool: remove memoized_decl
andrewrk May 31, 2023
c82a04d
InternPool: debug dump all the data
andrewrk Jun 1, 2023
08ae212
InternPool: fix key for empty array with sentinel
jacobly0 Jun 1, 2023
828756c
InternPool: fix element pointer type computations
jacobly0 Jun 1, 2023
123cfab
codegen: fix doubled global sentinels
jacobly0 Jun 1, 2023
a3b3ac0
llvm: fix lowering of lazy values
jacobly0 Jun 1, 2023
0777e98
Sema: disable repeated aggregate storage use with mismatching sentinel
jacobly0 Jun 1, 2023
cab79b0
lib: add const to avoid regression
jacobly0 Jun 1, 2023
9b48fc2
Allocate capture scopes in gpa instead of Decl.value_arena
mlugg Jun 1, 2023
8299ddf
InternPool: fix more key lifetime issues
jacobly0 Jun 1, 2023
e217442
wasm: implement missing case
jacobly0 Jun 1, 2023
dc18739
process: add more missing const
jacobly0 Jun 2, 2023
e8bcdca
Sema: fix in-memory coercion during comptime load
jacobly0 Jun 2, 2023
e017964
Sema: intern values of mutable decls after analysis
mlugg Jun 2, 2023
34dae73
std.hash: auto hash signed ints as bitcasts of unsigned ints
andrewrk Jun 2, 2023
69b7b91
compiler: eliminate Decl.value_arena and Sema.perm_arena
andrewrk Jun 2, 2023
f1c900c
compiler: avoid use of undefined memory
andrewrk Jun 2, 2023
35550c8
Module: fix populateTestFunctions UAF
andrewrk Jun 2, 2023
bc3b56f
llvm: fix undefined pointer type
jacobly0 Jun 2, 2023
fdfe730
InternPool: fix more key lifetime issues
jacobly0 Jun 2, 2023
04e66e6
InternPool: add optional coercion
jacobly0 Jun 2, 2023
da24ea7
Sema: rewrite `monomorphed_funcs` usage
jacobly0 Jun 2, 2023
7a59cd2
Sema: hack around UAF
jacobly0 Jun 2, 2023
0f80652
Sema: remove leftover references to value_arena
mlugg Jun 2, 2023
0fd52cd
InternPool: avoid aggregate null bytes storage
mlugg Jun 2, 2023
ad54f47
InternPool: optimize previous fix
jacobly0 Jun 2, 2023
6a15fc8
Sema: handle generic types when coercing functions in memory
jacobly0 Jun 2, 2023
e23b0a0
InternPool: fix yet more key lifetime issues
jacobly0 Jun 2, 2023
7c12e06
Sema: reword compile error about LLVM extensions and C import
andrewrk Jun 2, 2023
ab86b20
std.hash: improve small-key hashing in Wyhash
andrewrk Jun 3, 2023
2a6b918
stage2: pass most test cases under InternPool
mlugg Jun 3, 2023
7702af5
Sema: fix int arithmetic overflow checks
mlugg Jun 3, 2023
dce80f6
Sema: fix crashes accessing undefined values
jacobly0 Jun 4, 2023
44d8cf9
wasm: address behavior test regressions
jacobly0 Jun 4, 2023
f94c668
langref: fix error set order
jacobly0 Jun 10, 2023
a01bc77
llvm: fix name lifetime
jacobly0 Jun 10, 2023
b9a4eae
llvm: fix more name lifetimes
jacobly0 Jun 11, 2023
0ec012e
TypedValue: fix code formatting
jacobly0 Jun 11, 2023
7e5dea6
Sema: fix `std.builtin.Type.EnumField.value` when not auto-numbered
jacobly0 Jun 11, 2023
7507a76
link: use `Wasm.string_table` offsets for `Wasm.undefs` keys
jacobly0 Jun 11, 2023
2afc689
Sema: fix condition for emitting noreturn safety check
jacobly0 Jun 11, 2023
6360402
stage2: fix InternPool compile errors on 32-bit targets
mlugg Jun 11, 2023
5b6906c
InternPool: fix dbHelper after 4976b58
mlugg Jun 11, 2023
54460e3
Autodoc: make it work under InternPool
mlugg Jun 11, 2023
d37ebfc
InternPool: avoid as many slices pointing to `string_bytes` as possible
jacobly0 Jun 12, 2023
028f2ed
InternPool: fix one more compile error on 32-bit targets
jacobly0 Jun 12, 2023
9e61ba1
std.crypto.tls.Client: fix @memcpy crash in limitedOverlapCopy
mlugg Jun 12, 2023
415dbe9
link: fix compile error with only-c
mlugg Jun 12, 2023
1ec1498
build: add -Dno-bin option
mlugg Jun 12, 2023
52e7934
std.dwarf: fix findCompileUnit when ranges offset is given by const
mlugg Jun 12, 2023
3d48c40
Sema: redo monomorphed funcs to make more sense
jacobly0 Jun 13, 2023
ff35a18
Sema: intern values from resolved inferred allocs
mlugg Jun 13, 2023
4b7c1e5
tools: add LLDB pretty printer for InternPool.NullTerminatedString
mlugg Jun 13, 2023
2ad073e
link/Plan9: fix UAF of symbol names
andrewrk Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn build(b: *std.Build) !void {
const test_step = b.step("test", "Run all the tests");
const skip_install_lib_files = b.option(bool, "no-lib", "skip copying of lib/ files and langref to installation prefix. Useful for development") orelse false;
const skip_install_langref = b.option(bool, "no-langref", "skip copying of langref to the installation prefix") orelse skip_install_lib_files;
const no_bin = b.option(bool, "no-bin", "skip emitting compiler binary") orelse false;

const docgen_exe = b.addExecutable(.{
.name = "docgen",
Expand Down Expand Up @@ -166,6 +167,7 @@ pub fn build(b: *std.Build) !void {
exe.pie = pie;
exe.sanitize_thread = sanitize_thread;
exe.entitlements = entitlements;
if (no_bin) exe.emit_bin = .no_emit;

exe.build_id = b.option(
std.Build.Step.Compile.BuildId,
Expand Down
2 changes: 1 addition & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -10176,7 +10176,7 @@ pub fn main() void {

{#header_open|Invalid Error Set Cast#}
<p>At compile-time:</p>
{#code_begin|test_err|test_comptime_invalid_error_set_cast|'error.B' not a member of error set 'error{A,C}'#}
{#code_begin|test_err|test_comptime_invalid_error_set_cast|'error.B' not a member of error set 'error{C,A}'#}
const Set1 = error{
A,
B,
Expand Down
44 changes: 44 additions & 0 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,28 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
return self.items[prev_len..][0..n];
}

/// Resize the array, adding `n` new elements, which have `undefined` values.
/// The return value is a slice pointing to the newly allocated elements.
/// The returned pointer becomes invalid when the list is resized.
/// Resizes list if `self.capacity` is not large enough.
pub fn addManyAsSlice(self: *Self, n: usize) Allocator.Error![]T {
const prev_len = self.items.len;
try self.resize(self.items.len + n);
return self.items[prev_len..][0..n];
}

/// Resize the array, adding `n` new elements, which have `undefined` values.
/// The return value is a slice pointing to the newly allocated elements.
/// Asserts that there is already space for the new item without allocating more.
/// **Does not** invalidate element pointers.
/// The returned pointer becomes invalid when the list is resized.
pub fn addManyAsSliceAssumeCapacity(self: *Self, n: usize) []T {
assert(self.items.len + n <= self.capacity);
const prev_len = self.items.len;
self.items.len += n;
return self.items[prev_len..][0..n];
}

/// Remove and return the last element from the list.
/// Asserts the list has at least one item.
/// Invalidates pointers to the removed element.
Expand Down Expand Up @@ -949,6 +971,28 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
return self.items[prev_len..][0..n];
}

/// Resize the array, adding `n` new elements, which have `undefined` values.
/// The return value is a slice pointing to the newly allocated elements.
/// The returned pointer becomes invalid when the list is resized.
/// Resizes list if `self.capacity` is not large enough.
pub fn addManyAsSlice(self: *Self, allocator: Allocator, n: usize) Allocator.Error![]T {
const prev_len = self.items.len;
try self.resize(allocator, self.items.len + n);
return self.items[prev_len..][0..n];
}

/// Resize the array, adding `n` new elements, which have `undefined` values.
/// The return value is a slice pointing to the newly allocated elements.
/// Asserts that there is already space for the new item without allocating more.
/// **Does not** invalidate element pointers.
/// The returned pointer becomes invalid when the list is resized.
pub fn addManyAsSliceAssumeCapacity(self: *Self, n: usize) []T {
assert(self.items.len + n <= self.capacity);
const prev_len = self.items.len;
self.items.len += n;
return self.items[prev_len..][0..n];
}

/// Remove and return the last element from the list.
/// Asserts the list has at least one item.
/// Invalidates pointers to last element.
Expand Down
6 changes: 3 additions & 3 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub const Mode = OptimizeMode;

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const CallingConvention = enum {
pub const CallingConvention = enum(u8) {
/// This is the default Zig calling convention used when not using `export` on `fn`
/// and no other calling convention is specified.
Unspecified,
Expand Down Expand Up @@ -190,7 +190,7 @@ pub const CallingConvention = enum {

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const AddressSpace = enum {
pub const AddressSpace = enum(u5) {
generic,
gs,
fs,
Expand Down Expand Up @@ -283,7 +283,7 @@ pub const Type = union(enum) {

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Size = enum {
pub const Size = enum(u2) {
One,
Many,
Slice,
Expand Down
4 changes: 2 additions & 2 deletions lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ pub const ChildProcess = struct {
// can fail between fork() and execve().
// Therefore, we do all the allocation for the execve() before the fork().
// This means we must do the null-termination of argv and env vars here.
const argv_buf = try arena.allocSentinel(?[*:0]u8, self.argv.len, null);
const argv_buf = try arena.allocSentinel(?[*:0]const u8, self.argv.len, null);
for (self.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;

const envp = m: {
Expand All @@ -542,7 +542,7 @@ pub const ChildProcess = struct {
} else if (builtin.output_mode == .Exe) {
// Then we have Zig start code and this works.
// TODO type-safety for null-termination of `os.environ`.
break :m @ptrCast([*:null]?[*:0]u8, os.environ.ptr);
break :m @ptrCast([*:null]const ?[*:0]const u8, os.environ.ptr);
} else {
// TODO come up with a solution for this.
@compileError("missing std lib enhancement: ChildProcess implementation has no way to collect the environment variables to forward to the child process");
Expand Down
6 changes: 2 additions & 4 deletions lib/std/crypto/tls/Client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1256,10 +1256,8 @@ fn limitedOverlapCopy(frag: []u8, in: usize) void {
// A single, non-overlapping memcpy suffices.
@memcpy(frag[0..first.len], first);
} else {
// Need two memcpy calls because one alone would overlap.
@memcpy(frag[0..in], first[0..in]);
const leftover = first.len - in;
@memcpy(frag[in..][0..leftover], first[in..][0..leftover]);
// One memcpy call would overlap, so just do this instead.
std.mem.copyForwards(u8, frag, first);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/std/dwarf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ pub const DwarfInfo = struct {
const ranges_val = compile_unit.die.getAttr(AT.ranges) orelse continue;
const ranges_offset = switch (ranges_val.*) {
.SecOffset => |off| off,
.Const => |c| try c.asUnsignedLe(),
.RangeListOffset => |idx| off: {
if (compile_unit.is_64) {
const offset_loc = @intCast(usize, compile_unit.rnglists_base + 8 * idx);
Expand Down
14 changes: 14 additions & 0 deletions lib/std/hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ const xxhash = @import("hash/xxhash.zig");
pub const XxHash64 = xxhash.XxHash64;
pub const XxHash32 = xxhash.XxHash32;

/// This is handy if you have a u32 and want a u32 and don't want to take a
/// detour through many layers of abstraction elsewhere in the std.hash
/// namespace.
/// Copied from https://nullprogram.com/blog/2018/07/31/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This article was updated to point to skeeto/hash-prospector#19 for an improved function - skeeto/hash-prospector#19 (comment) or the previous comment on that issue seem to be the latest and greatest:

pub fn uint32(input: u32) u32 {
    var x: u32 = input;
    x ^= x >> 16;
    x *%= 0x21f0aaad;
    x ^= x >> 15;
    x *%= 0x735a2d97; // or 0xf35a2d97
    x ^= x >> 15;
    return x;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

pub fn uint32(input: u32) u32 {
var x: u32 = input;
x ^= x >> 16;
x *%= 0x7feb352d;
x ^= x >> 15;
x *%= 0x846ca68b;
x ^= x >> 16;
return x;
}

test {
_ = adler;
_ = auto_hash;
Expand Down
24 changes: 15 additions & 9 deletions lib/std/hash/auto_hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,21 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {

// Help the optimizer see that hashing an int is easy by inlining!
// TODO Check if the situation is better after #561 is resolved.
.Int => {
if (comptime meta.trait.hasUniqueRepresentation(Key)) {
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
} else {
// Take only the part containing the key value, the remaining
// bytes are undefined and must not be hashed!
const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable;
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });
}
.Int => |int| switch (int.signedness) {
.signed => hash(hasher, @bitCast(@Type(.{ .Int = .{
.bits = int.bits,
.signedness = .unsigned,
} }), key), strat),
.unsigned => {
if (comptime meta.trait.hasUniqueRepresentation(Key)) {
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
} else {
// Take only the part containing the key value, the remaining
// bytes are undefined and must not be hashed!
const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable;
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });
}
},
},

.Bool => hash(hasher, @boolToInt(key), strat),
Expand Down
7 changes: 5 additions & 2 deletions lib/std/math/big/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,9 @@ pub const Const = struct {
pub fn to(self: Const, comptime T: type) ConvertError!T {
switch (@typeInfo(T)) {
.Int => |info| {
// Make sure -0 is handled correctly.
if (self.eqZero()) return 0;

const UT = std.meta.Int(.unsigned, info.bits);

if (!self.fitsInTwosComp(info.signedness, info.bits)) {
Expand Down Expand Up @@ -2509,15 +2512,15 @@ pub const Const = struct {
return total_limb_lz + bits - total_limb_bits;
}

pub fn ctz(a: Const) Limb {
pub fn ctz(a: Const, bits: Limb) Limb {
// Limbs are stored in little-endian order.
var result: Limb = 0;
for (a.limbs) |limb| {
const limb_tz = @ctz(limb);
result += limb_tz;
if (limb_tz != @sizeOf(Limb) * 8) break;
}
return result;
return @min(result, bits);
}
};

Expand Down
3 changes: 2 additions & 1 deletion lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4226,7 +4226,8 @@ pub fn alignForwardLog2(addr: usize, log2_alignment: u8) usize {
/// The alignment must be a power of 2 and greater than 0.
/// Asserts that rounding up the address does not cause integer overflow.
pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {
assert(isValidAlignGeneric(T, alignment));
assert(alignment > 0);
assert(std.math.isPowerOfTwo(alignment));
return alignBackwardGeneric(T, addr + (alignment - 1), alignment);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/std/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ pub fn execve(
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();

const argv_buf = try arena.allocSentinel(?[*:0]u8, argv.len, null);
const argv_buf = try arena.allocSentinel(?[*:0]const u8, argv.len, null);
for (argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;

const envp = m: {
Expand All @@ -1143,7 +1143,7 @@ pub fn execve(
} else if (builtin.output_mode == .Exe) {
// Then we have Zig start code and this works.
// TODO type-safety for null-termination of `os.environ`.
break :m @ptrCast([*:null]?[*:0]u8, os.environ.ptr);
break :m @ptrCast([*:null]const ?[*:0]const u8, os.environ.ptr);
} else {
// TODO come up with a solution for this.
@compileError("missing std lib enhancement: std.process.execv implementation has no way to collect the environment variables to forward to the child process");
Expand Down
Loading