Skip to content

Instantly share code, notes, and snippets.

View matu3ba's full-sized avatar

Jan Ph. H. matu3ba

View GitHub Profile
@matu3ba
matu3ba / output_content1.txt
Last active February 15, 2023 22:50
Breaking Zigs default panic handler
Segmentation fault at address 0x3e800033cc6
/home/user/dev/git/zi/zig/master/build/stage3/lib/zig/std/os/linux/x86_64.zig:36:5: 0x22d97c in syscall2
(stacking_panics)
return asm volatile ("syscall"
^
/home/user/dev/git/zi/zig/master/build/stage3/lib/zig/std/os/linux.zig:991:49: 0x20f19f in nanosleep (sta
cking_panics)
return syscall2(.nanosleep, @ptrToInt(req), @ptrToInt(rem));
^
/home/user/dev/git/zi/zig/master/build/stage3/lib/zig/std/os.zig:5253:39: 0x20b681 in nanosleep (stacking
@matu3ba
matu3ba / aeabi.h
Created February 12, 2023 13:09 — forked from harieamjari/aeabi.h
aeabi.h declares the ARM run-time helper-function ABI for programs written in C.
/* aeabi.h - declares the ARM run-time helper-function ABI for programs written in C.
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
* In jurisdictions that recognize copyright laws, the author or authors
@matu3ba
matu3ba / reproduce.sh
Created February 8, 2023 20:14
sema_dump
#!/usr/bin/env sh
# Building zig in debug mode
cd zigrepo
mkdir -p build/ && cd build/ && cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH="$HOME/dev/git/bootstrap/zig-bootstrap/musl/out/host/" -GNinja && /usr/bin/time -v ninja install && cd ..
# add zig stages 3 to PATH
zig test --debug-log compilation -freference-trace --test-filter fubar arenas.zig
zig test --debug-log sema -freference-trace thread.zig
@matu3ba
matu3ba / initialization_footgun.zig
Last active February 5, 2023 19:55
Zig Result-Location-Semantics footgun
const std = @import("std");
fn t1init(alloc: std.mem.Allocator, path1: []const u8) ![]const u8 {
const stdout_wr = std.io.getStdOut().writer();
try stdout_wr.writeAll("t1init"); // prevent compiler to optimize away this fn
return try alloc.dupe(u8, path1);
}
fn t2init(alloc: std.mem.Allocator, path2: []const u8) ![]const u8 {
_ = path2;
@matu3ba
matu3ba / results.txt
Created February 1, 2023 21:48
Using c struct instead of return and writing overflow via pointer bears the same performance.
128bit
(ins)[misterspoon@pc tryzig]$ hyperfine ./addo_crt ./addo_cstruct
Benchmark 1: ./addo_crt
Time (mean ± σ): 1.083 s ± 0.006 s [User: 1.082 s, System: 0.001 s]
Range (min … max): 1.072 s … 1.094 s 10 runs
Benchmark 2: ./addo_cstruct
Time (mean ± σ): 1.077 s ± 0.003 s [User: 1.075 s, System: 0.001 s]
Range (min … max): 1.073 s … 1.082 s 10 runs
@matu3ba
matu3ba / bit_tricks.md
Last active February 1, 2023 10:48
some more bit tricks

Assuming MIN < x < MAX:

trick operation/effect note
x & (x - 1) clear lowest 1 bit if result 0, then x is 2
`x (x + 1)` set lowest 0 bit
`x (x - 1)` set all bits to right of lowest 1 bit
x & (x + 1) clear all bits to right of lowest 0 bit
x & -x extract lowest 1 bit
~x &amp; (x + 1) extract lowest 0 bit (as 1 bit)
@matu3ba
matu3ba / fs.diff
Created January 22, 2023 01:59
Count Steps to Directory With Entry as diff file
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index 31354a278..f06db4943 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -2085,6 +2085,44 @@ pub const Dir = struct {
}
}
+ pub const CountStepsToFolderWithEntryError = error{
+ InvalidKind,
@matu3ba
matu3ba / buildprocess.md
Created January 22, 2023 01:39
Bootstep Zig from wasm file
wasm2.c ──────►┌──────┐   ┌──────┐   ┌────┐   ┌──────┐   ┌────┐   ┌────┐   ┌────┐
               │wasm2c│──►│zig1.c│──►│zig1│──►│zig2.c│──►│zig2│──►│zig3│──►│zig4│
          ┌───►└──────┘ ┌►└──────┘┌─►└────┘   └──────┘┌─►└────┘   └────┘   └────┘
          │  zig1.wasm ─┘         │                   │     ▲        ▲        ▲
CC ───────┴───────────────────────┴───────────────────┘     │      binary-identical
                                                            │
idealized, see TODO:                                        │(optional)
used LLVM libs ─────────────────────►┌────┐                 │
 │LLVM│─────────────────┘
@matu3ba
matu3ba / child_process.zig
Created January 9, 2023 19:15
spawnMac changes for posix_spawn that I found no use case for yet: user owns the actions + attribute or users doesnt.
/// Use this for additional posix spawn attributes.
/// Do not set spawn attribute flags and do not modify stdin, stdout, stderr
/// behavior, because those are set in the platform-specific spawn method.
posix_attr: if (os.hasPosixSpawn) ?os.posix_spawn.Attr else void,
posix_actions: if (os.hasPosixSpawn) ?os.posix_spawn.Actions else void,
.posix_attr = if (os.hasPosixSpawn) null else undefined,
.posix_actions = if (os.hasPosixSpawn) null else undefined,
@matu3ba
matu3ba / zig_cache.md
Last active January 4, 2023 08:43
Big picture of the Zig caching system.

This is outdated now. Compilation does not use the tmp directory anymore.

** Big picture of the Zig caching system. ** This is a big picture of the Zig caching system, mostly motivated for me to understand the necessary bits to solve ziglang/zig#11643. It reflects the status quo of commit fcfeafe99a3ecc694a3475735c81a0d75b6da6d0. The core logic is defined in src/Cache.zig.

zig-cache/h contains the manifest files with varying content.