aboutsummaryrefslogtreecommitdiff

hdass

hachem's dumb assembly super-set, pronounced "hd ass", or "headass"...depends on how you feel that day.

the entire point of this project is to provide a middle ground between c and assembly. if we think about why we still write assembly today, it's usually because we need direct control over what the cpu is doing. we want control over the exact instructions being executed, the memory, the stack and everything else that higher-level programming languages normally abstract away. the thing is, not every program written in assembly actually needs that control. sometimes you want to write something close to the machine without having to manuall deal with every tiny detail yourself. you still want registers, explicit control over memory and a good understanding of what your program is doing, but you don't necessarily need to manually express everything as individual assembly instructions.

this is where this piece of shit comes in. it's not quite high-level enough to be a c-like language, but it's also not low-level enough to be as annoying to write as raw assembly. the goal is to sit somewhere in between, keeping the parts of assembly that make it useful while making the parts that don't need to be painful a little nicer to work with. hdass transpiles into multiple flavours of assembly, currently nasm and fasm (masm is planned), rather than directly producing machine code. the idea is to provide a single language for writing low-level programs while allowing the backend to translate code into the assembler syntax you want to target. you're still ultimately producing assembly, and you're never particularly far away from the code that gets assembled. the goal isn't to hide the machine from you or turn assembly into c. there are already plenty of high-level languages that do that. hdass just fills the gap between the two, where you might want some more convenience whilst writing assembly without taking away the reason you wanted to work close to the machine in the first place.

examples

here's a simple "hello, world!" world program written using hdass' syntax:

[entry: main]

const SYS_WRITE = 1
const SYS_EXIT = 60

const STDOUT = 1

data message = "type shi\n"

proc main
{
    rax = SYS_WRITE
    rdi = STDOUT
    rsi = message
    rdx = message.len
    syscall

    rax = SYS_EXIT
    rdi = 0
    syscall
}

the program still directly controls the registers used for the system calls. nothing is hiding what the program is doing. more examples can be found in examples/.

documentation

the docs live in docs/:

  • getting started — build the compiler, write and run a first program on x86-64 and aarch64.
  • language reference — directives, declarations, statements, control flow, memory, floats, raw instructions and extensions.
  • targets — architectures and assemblers, what each supports, and the per-architecture syscall abis.
  • internals — the compiler pipeline and how to add a backend or architecture.

building

the build is driven by meson. configure a build directory and compile the compiler:

meson setup build
meson compile -C build

this produces the hdass binary at build/hdass. the default build turns on the address and undefined-behaviour sanitizers; for an optimised build without them, configure a separate directory:

meson setup build-release --buildtype=release -Db_sanitize=none
meson compile -C build-release

to run the unit tests:

meson test -C build

if cppcheck is installed, ninja -C build cppcheck runs static analysis over the sources.

hdass emits x86-64 assembly, so to actually assemble and run its output you need an x86-64 linux toolchain. the bundled docker environment provides a consistent one on any host, including apple silicon, where the amd64 image runs under emulation. the image is a debian base with nasm, fasm, ld (binutils), a c toolchain and meson/ninja preinstalled.

start the container (this builds the image the first time):

docker compose up -d --build

open a shell inside it. the repository is bind-mounted at /hdass, so edits on the host are visible immediately:

docker compose exec hdass bash

from inside the container you can build the compiler and take a program all the way to a running executable:

meson setup build-linux && meson compile -C build-linux
./build-linux/hdass examples/hello_world.hdass -o hello.asm
nasm -f elf64 hello.asm -o hello.o
ld -e main hello.o -o hello
./hello

hdass targets nasm by default; pass -t fasm to emit fasm instead, which assembles in a single step:

./build-linux/hdass -t fasm examples/hello_world.hdass -o hello.asm
fasm hello.asm hello.o
ld -e main hello.o -o hello

to transpile, assemble, link and run every program in examples/ and check its output, use the end-to-end test script (also from inside the container):

meson setup build-linux && meson compile -C build-linux && ./scripts/test_examples.sh

or, from the host, run the whole suite (unit tests plus the example tests) in one shot, bringing the container up if needed:

./scripts/docker_test.sh

to build, assemble, link and run a single program in the container:

./scripts/run.sh examples/circle.hdass

when you're finished, stop and remove the container:

docker compose down

disclaimer

hdass is extremely experimental. the language, syntax and semantics are still being figured out, so things will probably change, sometimes because there is a better way to do something and sometimes because i decided the old syntax looked stupid.

license

this project is licensed under the mit license. see license for more information.