Skip to content

Instantly share code, notes, and snippets.

View tekknolagi's full-sized avatar
🚴

Max Bernstein tekknolagi

🚴
View GitHub Profile
@tekknolagi
tekknolagi / sym_test.cc
Created October 8, 2022 03:09 — forked from aeppert/sym_test.cc
Enumerate and demangle symbols within an executable's own symbol table.
#include <stdio.h>
#include <stdlib.h>
#include <cxxabi.h>
#include <link.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#!/bin/bash
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <depfile> <command...>" >&2
exit 1
fi
# The .d file we're going to write.
DEPSFILE=$1
shift
@tekknolagi
tekknolagi / gartersnake.py
Last active July 7, 2022 23:09
A small Python to C compiler...
import ast
import sys
import json
NoSemicolon = True
class CompileError(Exception):
pass
# Copyright Andy Chu
class switch(object):
"""A ContextManager that translates to a C switch statement."""
def __init__(self, value):
# type: (int) -> None
self.value = value
def __enter__(self):
# type: () -> switch
@tekknolagi
tekknolagi / enum_bitmask.hpp
Created May 3, 2022 06:50 — forked from StrikerX3/enum_bitmask.hpp
Type-safe enum bitmasks
/*
Type-safe enum class bitmasks.
Based on work by Andre Haupt from his blog at
http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/
To enable enum classes to be used as bitmasks, use the ENABLE_BITMASK_OPERATORS
macro:
enum class MyBitmask {
None = 0b0000,
One = 0b0001,
Two = 0b0010,
// Need Clang 13 for __attribute__((musttail))
// clang++-13 -std=c++11 -g -O0 stencil.cpp
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>
#include <sys/mman.h>
(() => {
////////////////////////////////////////////////////////////////////////////////
// Dragging
const dragTarget = document.getElementById('dragTarget');
const uploadFiles = document.getElementById('uploadFiles');
const loadExample = document.getElementById('loadExample');
let dragging = 0;
let filesInput;
@tekknolagi
tekknolagi / stack_machine.cpp
Created February 2, 2021 04:11 — forked from glampert/stack_machine.cpp
1-register stack caching test for stack-based interpreters.
//
// Simple stack-based virtual machine tests.
//
// I use two slightly different strategies to manage the
// interpreter stack: First is using the "naive" approach
// of keeping a stack pointer that is bumped/decremented
// for every instruction that has stack operands. The second
// approach uses a "stack cache" register to cache the
// Top-of-Stack (TOS) value.
//
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// TODO(max): Consider writing this in Rust or Nim for some extra reader
// appeal.
typedef enum {
kInt,
@tekknolagi
tekknolagi / aoc8-compiler.py
Created December 13, 2020 22:36
Advent of Code Handheld Halting Compiler
import sys
def compile(prog):
result = (
"#include <stdbool.h>\n"
"#include <stdio.h>\n"
"#include <string.h>\n"
"int execute() {\n"
"int acc = 0;\n"