ELVM IR
The small assembly language that C programs are compiled into before ELVM turns them into brainfuck, Whitespace or dozens of other esolangs.
Turing complete: with unbounded memory · ●●○ moderate
Tested with 4 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
ELVM, by shinh, is a compiler from C to esoteric languages. It first compiles C into this intermediate language, ELVM IR, and each backend then only has to translate a handful of simple instructions. That is why the IR is so plain. There are six registers, a flat memory of words and no multiplication or division; the C library does those with loops.
Read the story
Try It Online runs the IR directly with eli, ELVM's own interpreter. A program in it is ordinary assembly, one instruction per line.
The machine
| Code | Does |
|---|---|
mov dst, src | Copy a register, a number or a label into dst |
add dst, src / sub dst, src | Add or subtract, wrapping at 2^24 |
load dst, addr / store src, addr | Read or write a memory word |
putc src / getc dst | Print a byte; read one, 0 at the end |
eq ne lt gt le ge dst, src | Set dst to 1 or 0 |
jeq … jge target, a, b | Jump when the comparison holds |
jmp target / exit | Jump; stop |
name: | A label |
.text .data .long n .string "s" | Sections and data |
The registers are A, B, C, D, BP and SP. Words are 24 bits and compare as unsigned numbers. Data is laid out from address 0, and a .string ends in a 0 word, as in C.
Example
The last program walks a string in memory the way compiled C does.
The last program walks a string in memory the way compiled C does. It loads a word, stops at the 0, prints it and steps the address.
Patterns it teaches
- Counter machines: add, subtract and a conditional jump are all the arithmetic a backend has to provide.
Further
- ELVM, with the IR's description and every backend
See also
Also in Turing tarpits
Write your own in Run Convert a program from ELVM IR
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: elvm-ir.
Command line
shib run elvm-ir -e '.text
mov A, 48
loop:
putc A
add A, 1
jle loop, A, 57
exit'shib manual elvm-irSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/elvm-ir.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language