Opto is an optimizer and compiler generator for transforming SSA WebAssembly and compiling it to machine code. Opto comes with a term rewriting language.
For now the focus will be on interpreting the term rewriting language.
- interpreter
- terms
- wat_to_wat
- wat_to_x86
The goal is to be less academic and more high-level. Term rewriting is all about pattern matching.
// wat -> wat
@ignore_spaces() // Special pattern directive.
val := \d+ // Pattern.
allow_constants = 1 // Double. The only supported primitive type.
constants = {} // Hashmap. The only supported DS.
constant_fold := // Mapping Pattern.
"(#ty.const :val) (local.set #name)" => {
constants[#name] = (:val, #ty)
}
constant_propagation :=
"(local.get #name)" => {
(#val, #ty) = constants[#name]
"(#ty.const #val)"
}
apply_constants (:expr) { // Functions. They take patterns as arguments.
proof (allow_constants = 0) { // A proof statement.
constant_fold()
constant_propagation()
}
}
apply_constants()
// Checking if a rewrite is a result of constant_propagation
proof (:constant_propagation) {
@print("There is a constant here", :constant_propagation)
}
- Generate an x86 optimizer and compiler
./optogen --target-triple=x86-none-darwin --optfile=rules.opto -o compiler
- Optimize wasm file and generate x86 executable code
./compiler add.wasm
./add
- Binaryen
- Cranelift
- LLVM