Skip to content

Instantly share code, notes, and snippets.

View nickdesaulniers's full-sized avatar

Nick Desaulniers nickdesaulniers

View GitHub Profile
@nickdesaulniers
nickdesaulniers / mul.c
Created April 3, 2013 07:31
simple multiplication used in JIT
// Compile me with: clang -c mul.c -o mul.o
int mul (int a, int b) {
return a * b;
}
@nickdesaulniers
nickdesaulniers / obj_dump.txt
Created April 3, 2013 07:40
gobjdump -j .text -d mul.o -M intel
$ gobjdump -j .text -d mul.o -M intel
Disassembly of section .text:
0000000000000000 <_mul>:
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
4: 89 7d fc mov DWORD PTR [rbp-0x4],edi
7: 89 75 f8 mov DWORD PTR [rbp-0x8],esi
a: 8b 75 fc mov esi,DWORD PTR [rbp-0x4]
@nickdesaulniers
nickdesaulniers / jit.c
Last active March 9, 2016 12:21
A simple JIT
#include <stdio.h> // printf
#include <string.h> // memcpy
#include <sys/mman.h> // mmap, munmap
int main () {
// Hexadecimal x86_64 machine code for: int mul (int a, int b) { return a * b; }
unsigned char code [] = {
0x55, // push rbp
0x48, 0x89, 0xe5, // mov rbp, rsp
0x89, 0x7d, 0xfc, // mov DWORD PTR [rbp-0x4],edi
@nickdesaulniers
nickdesaulniers / hello_world.rs
Created June 5, 2013 18:18
example code for emscripten issue
n add_three (x: int) -> int {
3 + x
}
fn double (x: int) -> int {
2 * x
}
#[no_mangle]
pub extern fn meaning_of_life () -> int {
@nickdesaulniers
nickdesaulniers / gist:5716003
Created June 5, 2013 18:21
Stack trace `emcc hello_world.bc`
undefined:175
return typeData.flatSize*8;
^
TypeError: Cannot read property 'flatSize' of undefined
at getBits (eval at globalEval (/Users/Nicholas/mozilla/emscripten/src/compiler.js:105:8), <anonymous>:175:20)
at isIllegalType (eval at globalEval (/Users/Nicholas/mozilla/emscripten/src/compiler.js:105:8), <anonymous>:181:14)
at parseNumerical (eval at globalEval (/Users/Nicholas/mozilla/emscripten/src/compiler.js:105:8), <anonymous>:809:39)
at parseParamTokens (eval at globalEval (/Users/Nicholas/mozilla/emscripten/src/compiler.js:105:8), <anonymous>:391:28)
at Object.substrate.addActor.processItem (eval at globalEval (/Users/Nicholas/mozilla/emscripten/src/compiler.js:105:8), <anonymous>:553:20)
at Object._lineSplitter [as processItem] (eval at globalEval (/Users/Nicholas/mozilla/emscripten/src/compiler.js:105:8), <anonymous>:118:35)
@nickdesaulniers
nickdesaulniers / hello_world.ll
Last active December 18, 2015 03:09
llvm-dis hello_world.bc -o hello_world.ll
; ModuleID = 'hello_world.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-apple-darwin"
%tydesc = type { i64, i64, void ({}*, %tydesc**, i8*)*, void ({}*, %tydesc**, i8*)*, void ({}*, %tydesc**, i8*)*, void ({}*, %tydesc**, i8*)*, i8*, i8* }
@_rust_crate_map_toplevel = global { i32, i8*, i64, [2 x i64] } { i32 1, i8* bitcast ({} ({ i64, %tydesc*, i8*, i8*, i8 } addrspace(1)*)* @_ZN7cleanup10annihilate17_c17425bfbdabf8296_07preE to i8*), i64 ptrtoint ([1 x { i64, i64 }]* @_rust_mod_map to i64), [2 x i64] [i64 ptrtoint (i64* @_rust_crate_map_std_0.7-pre_c3ca5d77d81b46c1 to i64), i64 0] }
@_rust_crate_map_std_0.7-pre_c3ca5d77d81b46c1 = external global i64
@_rust_mod_map = internal global [1 x { i64, i64 }] zeroinitializer
@rust_abi_version = constant i64 1
@nickdesaulniers
nickdesaulniers / gist:5717746
Created June 5, 2013 22:10
updated stack trace
EMCC_DEBUG=1 emcc hello_world.bc -s EXPORTED_FUNCTIONS="['_meaning_of_life']"
DEBUG emcc: invocation: /usr/local/bin/emcc hello_world.bc -s EXPORTED_FUNCTIONS=['_meaning_of_life']
WARNING root: LLVM version appears incorrect (seeing "Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)", expected "clang version 3.2")
INFO root: (Emscripten: Running sanity checks)
DEBUG emcc: compiling to bitcode
DEBUG emcc: copying bitcode file: hello_world.bc
DEBUG emcc: will generate JavaScript
DEBUG emcc: considering libcxx: we need set([]) and have set([])
DEBUG emcc: considering libcextra: we need set([]) and have set([])
@nickdesaulniers
nickdesaulniers / yahoo.coffee
Created August 31, 2013 06:43
coffee yahoo.coffee tsla 08-27-2013 08-29-2013
http = require 'http'
csv = require 'csv'
if process.argv.length is 2
url = 'http://ichart.finance.yahoo.com/table.csv?s=TSLA&a=07&b=28&c=2013&d=07&e=29&f=2013&g=d'
else if process.argv.length is 5
[_, _, symbol, start, end] = process.argv
[[a, b, c], [d, e, f]] = [start.split('-'), end.split('-')]
console.log a, b, c, d, e, f
url = "http://ichart.finance.yahoo.com/table.csv?s=#{symbol}&a=#{--a}&b=#{b}&c=#{c}&d=#{--d}&e=#{e}&f=#{f}&g=d"
@nickdesaulniers
nickdesaulniers / hello_world.ll
Created September 11, 2013 00:27
CLANG_ADD_VERSION=3.3 LLVM_ADD_VERSION=3.3 emcc hello_world.ll
; ModuleID = 'Crystal'
%String.0 = type { i32, i8 }
%MatchData = type { %String.0*, %String.0*, i32, i32*, i32 }
%StringBuilder.1 = type { i32, %"Array(String).2"* }
%"Array(String).2" = type { i32, i32, %String.0** }
%"struct.ABI::UnwindException" = type { i64, i64, i64, i64, i64, i32 }
%"Nil | UInt64" = type <{ i32, [2 x i32] }>
@symbol_table = internal global [0 x %String.0*] zeroinitializer
@nickdesaulniers
nickdesaulniers / visible.js
Created October 15, 2013 16:31
Visibility Change API
var lastKnownLanguage = navigator.language;
document.addEventListener("visibilitychange", function () {
if (document.visibilityState === "visible" &&
navigator.language !== lastKnownLanguage) {
// retranslate
}
});