Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Kelley andrewrk

View GitHub Profile
@andrewrk
andrewrk / mystery.md
Last active Oct 6, 2021
trying to figure out what's going wrong with this linking
View mystery.md

I have an object file created by LLVM:

$ readelf -s test.o

Symbol table '.symtab' contains 8 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test
     2: 0000000000000010    22 FUNC    LOCAL  DEFAULT    2 start.callMain2
@andrewrk
andrewrk / 0test.zig
Created Aug 4, 2021
stage2 generics demo
View 0test.zig
const std = @import("std");
const expect = std.testing.expect;
test "example" {
var x: usize = 0;
x += checkSize(i32);
x += checkSize(bool);
try expect(x == 5);
}
@andrewrk
andrewrk / adapted_hash_maps.zig
Last active Jul 23, 2021
how to use hash map contexts to save memory when doing a string table
View adapted_hash_maps.zig
const std = @import("std");
const mem = std.mem;
const Foo = struct {
string_bytes: std.ArrayListUnmanaged(u8),
/// Key is string_bytes index.
string_table: std.HashMapUnmanaged(u32, void, IndexContext, std.hash_map.default_max_load_percentage),
};
const IndexContext = struct {
@andrewrk
andrewrk / output.txt
Created Jul 3, 2021
benchmarking the zig tokenizer
View output.txt
[nix-shell:~/Downloads/zig/build-release]$ ./zig build-exe test.zig -OReleaseFast
[nix-shell:~/Downloads/zig/build-release]$ ls -hl ../lib/std/special/compiler_rt/udivmod{t,d}i4_test.zig test.zig
-rw-r--r-- 1 andy users 1.9M May 10 21:29 ../lib/std/special/compiler_rt/udivmoddi4_test.zig
-rw-r--r-- 1 andy users 9.8M May 10 21:29 ../lib/std/special/compiler_rt/udivmodti4_test.zig
-rw-r--r-- 1 andy users 1.2K Jul 3 16:46 test.zig
[nix-shell:~/Downloads/zig/build-release]$ ./test test.zig
tokenizer: 25 MiB/s
@andrewrk
andrewrk / codegen.cpp
Created May 11, 2021
reset error return trace index
View codegen.cpp
if (instruction->operand != nullptr &&
instruction->operand->value->type->id != ZigTypeIdErrorSet &&
instruction->operand->value->type->id != ZigTypeIdErrorUnion)
{
// Reset error return trace index.
bool is_llvm_alloca = false;
LLVMValueRef err_ret_trace_ptr = get_cur_err_ret_trace_val(g, instruction->base.base.scope, &is_llvm_alloca);
if (err_ret_trace_ptr != nullptr) {
size_t index_field_index = g->stack_trace_type->data.structure.fields[0]->gen_index;
LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, err_ret_trace_ptr, (unsigned)index_field_index, "");
View hash_map.diff
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig
index 83a061dfe..fc1f18771 100644
--- a/lib/std/array_hash_map.zig
+++ b/lib/std/array_hash_map.zig
@@ -571,21 +571,30 @@ pub fn ArrayHashMapUnmanaged(
}
pub fn getIndex(self: Self, key: K) ?usize {
+ const Matcher = struct {
+ const Key = K;
@andrewrk
andrewrk / demo.zig
Created Mar 20, 2021
demonstration of zig's default unit testing allocator catching various mistakes
View demo.zig
const std = @import("std");
// leak
test "example 1" {
var array = std.ArrayList(i32).init(std.testing.allocator);
try array.append(1234);
}
// double free
@andrewrk
andrewrk / a.ll
Created Feb 26, 2021
LLVM 12 sret LLVMDumpModule
View a.ll
; Function Attrs: nobuiltin noredzone nounwind sspstrong
define void @entry() #1 !dbg !2228 {
Entry:
%0 = alloca %Foo, align 4
call fastcc void @doit(%Foo* sret %0), !dbg !2232
ret void, !dbg !2234
}
; Function Attrs: nobuiltin noredzone nounwind sspstrong
define internal fastcc void @doit(%Foo* nonnull sret %0) unnamed_addr #1 !dbg !2235 {
@andrewrk
andrewrk / builtin.zig
Created Feb 23, 2021
`zig build-obj --show-builtin` on an M1. ziglang/zig commit d9e46dceeca3f66b87e6b2e36415417495d2d2a0
View builtin.zig
usingnamespace @import("std").builtin;
/// Deprecated
pub const arch = Target.current.cpu.arch;
/// Deprecated
pub const endian = Target.current.cpu.arch.endian();
/// Zig version. When writing code that supports multiple versions of Zig, prefer
/// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
pub const zig_version = try @import("std").SemanticVersion.parse("0.8.0-dev.1159+d9e46dcee");
@andrewrk
andrewrk / static-window.c
Created Nov 20, 2020
experimenting with reloading a static binary to make it dynamic
View static-window.c
#include <stdbool.h>
#include <stddef.h>
void __attribute__((weak)) dlopen(const char *filename, int flags);
int __attribute__((weak)) dlclose(void *handle);
struct ZigWindowFns {
void (*dlopen)(const char *filename, int flags);
int (*dlclose)(void *handle);
};