Skip to content

Instantly share code, notes, and snippets.

@evanw
evanw / 0_stackify.ts
Last active March 24, 2024 00:02
Example "stackify" algorithm for turning SSA into WASM
// This code demonstrates a simplified "stackification" algorithm to turn
// instructions in a basic block back into a tree. This is useful when
// generating WebAssembly code from assembly instructions in SSA form.
//
// It's the algorithm used by LLVM's WebAssembly backend, viewable here:
// https://github.com/llvm-mirror/llvm/blob/master/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
type InsKind =
'Add' |
'LocalSet' |
@evanw
evanw / chart.png
Created September 20, 2012 03:13
WebSocket Speed Test
chart.png
# This is a simple wrapper for the "emcc" command from the emscripten compiler.
# Unlike the emscripten SDK script, it lets you specify the compiler version
# number. Just pass the version number using the "--version" flag and put the
# flags for "emcc" after the "--":
#
# python emcc_wrapper.py --version 1.27.0 -- -O3 -s NO_EXIT_RUNTIME=1 ...
#
import os
import sys
import pipes
{
"presets": [
["env", {
"targets": {
"browsers": ["chrome 85"]
}
}]
]
}
@evanw
evanw / Cargo.toml
Created April 24, 2018 00:16
Example crazy Rust compile error
[package]
name = "multiplayer"
version = "0.0.1"
publish = false
[dependencies]
futures = "0.1.18"
hyper = "0.11.17"
tokio-core = "0.1.12"
tokio-signal = "0.1.4"
@evanw
evanw / rust-wtf.rs
Created April 1, 2018 02:33
Rust's float parser has correctness issues
fn main() {
// Ok(0.000000000...0000000005)
println!("{:?}", "4.9406564584124654e-324".parse::<f64>());
// WTF???
// Err(ParseFloatError { kind: Invalid })
println!("{:?}", "4.94065645841246545e-324".parse::<f64>());
// Ok(0.000000000...0000000005)
println!("{:?}", "4.9406564584124655e-324".parse::<f64>());
@evanw
evanw / FGNode.cpp
Created July 30, 2017 00:16
An optimizer bug with emscripten's WebAssembly backend
#include "FGNode.h"
#include <stdio.h>
bool Node::supportsFillPaintData() const {
return usesGeometryCache(_fields._type);
}
const PaintData& Node::fillPaintData() const {
const auto* value = _fields._fillPaintData.get();
if (!supportsFillPaintData() || !value) {
/*
This demonstrates a problem I ran into when writing a parser in Rust. It
appears that adding more branches to a match expression causes the function
to consume more stack space. The stack space used in this example isn't too
extreme but in my actual parser, each function call uses around 15kb of stack
space which adds up very quickly. It only takes around 30 nested calls to
use 512kb of stack space, after which a stack overflow crash occurs.
Here's the output of this program on https://play.rust-lang.org/:
@evanw
evanw / .gitmodules
Created March 16, 2016 03:59
Compiler benchmark
[submodule "skew"]
path = skew
url = git@github.com:evanw/skew.git
[submodule "typescript"]
path = typescript
url = https://git01.codeplex.com/typescript
[submodule "coffeescript"]
path = coffeescript
url = git@github.com:jashkenas/coffeescript.git
@evanw
evanw / Makefile
Created November 28, 2013 19:33
PNaCl bug with RequestFilteringInputEvents
SDK = ~/nacl_sdk/pepper_canary
build:
$(SDK)/toolchain/mac_pnacl/bin/pnacl-clang++ -o filtering.bc filtering.cpp -O2 -I $(SDK)/include -L $(SDK)/lib/pnacl/Release -lppapi_cpp -lppapi
$(SDK)/toolchain/mac_pnacl/bin/pnacl-finalize -o filtering.pexe filtering.bc