esolang

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

Expected: Hi

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

CodeDoes
mov dst, srcCopy a register, a number or a label into dst
add dst, src / sub dst, srcAdd or subtract, wrapping at 2^24
load dst, addr / store src, addrRead or write a memory word
putc src / getc dstPrint a byte; read one, 0 at the end
eq ne lt gt le ge dst, srcSet dst to 1 or 0
jeq … jge target, a, bJump when the comparison holds
jmp target / exitJump; 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.
Expected: 0123456789
Expected: echo
Expected: Hi!

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

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-ir

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/elvm-ir.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language