Skip to content

Instantly share code, notes, and snippets.

@mbillingr
mbillingr / tailtest.ll
Last active June 15, 2019 10:52
Tailcall experiments in LLVM IR
%T = type { i8, i64 }
declare %T* @allocate_new_pair()
declare %T @number(i32 %num)
define i1 @is_null(%T %obj) {
%tag = extractvalue %T %obj, 0
%cond = icmp eq i8 %tag, 0
br i1 %cond, label %yes, label %no
@mbillingr
mbillingr / scheme_compile.py
Created June 14, 2019 10:25
Some scheme compilation concepts with llvmlite
from ctypes import CFUNCTYPE, c_int32, c_voidp, c_char
import llvmlite.binding as llvm
from llvmlite import ir
# All these initializations are required for code generation!
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter() # yes, even this one
@mbillingr
mbillingr / cps_transform.py
Created June 16, 2019 19:42
Algorithms for transforming lisp code to continuation passing style (CPS)
# This is a python translation of the continuation passing style conversions
# found in http://matt.might.net/articles/cps-conversion/
count = 1
def gensym(name):
global count
name = name + str(count)
count += 1
return name
@mbillingr
mbillingr / lispatterns.rs
Last active June 17, 2019 20:16
(WIP) Lisp-like pattern matching with rust macros
#[derive(Debug, Clone, PartialEq)]
pub enum Obj {
Nil,
Sym(&'static str),
Pair(Box<Obj>, Box<Obj>),
}
impl Obj {
pub fn cons(car: Obj, cdr: Obj) -> Self {
Obj::Pair(Box::new(car), Box::new(cdr))
@mbillingr
mbillingr / Cargo.toml
Last active August 1, 2019 08:34
Forth JIT with Cranelift
[package]
edition = "2018"
name = "forth"
version = "1.7.0"
[dependencies]
cranelift = "0.37.0"
cranelift-module = "0.37.0"
cranelift-simplejit = "0.37.0"
#[derive(Debug, Copy, Clone)]
enum AtomicValue {
Undefined,
Nil,
Integer(i64),
Pointer(*mut Self),
Record(*mut Self, u32),
Pair(*mut Self),
@mbillingr
mbillingr / gc.rs
Created September 10, 2019 07:47
A toy Mark-and-Sweep garbage collector with some serious issues
use std::collections::HashSet;
fn main() {
let mut gc = Memory::new();
gc.set_base(estimate_stack_pointer_address());
let s = Box::new(Box::new(inner_main(&mut gc))); // Disaster!
gc.collect_stack();
inner_main(&mut gc);
gc.collect_stack();
println!("==============");
@mbillingr
mbillingr / macroluator.rs
Created January 9, 2020 22:31
(WIP) A little Scheme implementation within Rust macros
use std::cell::Cell;
macro_rules! scm_meaning {
(()) => {Scm::Nil};
(<) => {Scm::from(Scm::less)};
(>) => {Scm::from(Scm::greater)};
(+) => {Scm::from(Scm::add)};
(-) => {Scm::from(Scm::sub)};
(*) => {Scm::from(Scm::mul)};
(/) => {Scm::from(Scm::div)};
@mbillingr
mbillingr / compile.scm
Created July 23, 2020 20:44
Scheme->Rust compiler prototype
(define (meaning e r)
(if (atom? e)
(if (symbol? e) (meaning-reference e r)
(meaning-quotation e r))
(case (car e)
((quote) (meaning-quotation (cadr e) r))
((set!) (meaning-assignment (cadr e) (caddr e) r))
((begin) (meaning-sequence (cdr e) r))
((if) (meaning-alternative (cadr e) (caddr e) (cadddr e) r))
((lambda) (meaning-abstraction (cadr e) (cddr e) r))
@mbillingr
mbillingr / main1.rs
Created July 28, 2020 19:29
Case study what compiling Scheme to Rust could look like
// Variant 1: globals in struct
/*
(import (scheme base) (example counter))
(display (count)) (newline)
(display (count)) (newline)
*/
use example_counter::*;