Skip to content

Instantly share code, notes, and snippets.

@sueszli
Last active February 21, 2026 23:47
Show Gist options
  • Select an option

  • Save sueszli/f971a7f0f8fcdf9bc60d318d11662b38 to your computer and use it in GitHub Desktop.

Select an option

Save sueszli/f971a7f0f8fcdf9bc60d318d11662b38 to your computer and use it in GitHub Desktop.
x86 backend for xdsl
; $ docker run --rm --platform linux/amd64 -v "$PWD":/app -w /app alpine sh -c "apk add nasm binutils && nasm -f elf64 hello-docker.asm -o hello.o && ld hello.o -o hello && ./hello"
global _start
section .text
_start:
mov rax, 1 ; write(
mov rdi, 1 ; STDOUT_FILENO,
mov rsi, msg ; "Hello, world!\n",
mov rdx, msglen ; sizeof("Hello, world!\n")
syscall ; );
mov rax, 60 ; exit(
mov rdi, 0 ; EXIT_SUCCESS
syscall ; );
section .rodata
msg: db "Hello, world!", 10
msglen: equ $ - msg
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "setuptools<70",
# "wheel",
# "six",
# "opcodes==0.3.13",
# "peachpy @ git+https://github.com/Maratyszcza/PeachPy.git"
# ]
# ///
# `/usr/bin/python3` also contains x86 cpython
# $ arch -x86_64 uv run --no-build-isolation --python /usr/bin/python3 hello-peachpy.py
from peachpy import *
from peachpy.x86_64 import *
x = Argument(int32_t)
y = Argument(int32_t)
with Function("Add", (x, y), int32_t) as asm_function:
reg_x = GeneralPurposeRegister32()
reg_y = GeneralPurposeRegister32()
LOAD.ARGUMENT(reg_x, x)
LOAD.ARGUMENT(reg_y, y)
ADD(reg_x, reg_y)
RETURN(reg_x)
python_function = asm_function.finalize(abi.detect()).encode().load()
print(python_function(2, 2)) # -> prints "4"
; $ nasm -f macho64 hello-rosetta.asm -o hello.o && ld hello.o -o hello -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch x86_64 -macos_version_min 11.0 -w && ./hello
global _start
section .text
_start:
mov rax, 0x2000004 ; write(
mov rdi, 1 ; STDOUT_FILENO,
lea rsi, [rel msg] ; "Hello, world!\n",
mov rdx, msglen ; sizeof("Hello, world!\n")
syscall ; );
mov rax, 0x2000001 ; exit(
mov rdi, 0 ; EXIT_SUCCESS
syscall ; );
section .data
msg: db "Hello, world!", 10
msglen: equ $ - msg
@sueszli
Copy link
Author

sueszli commented Feb 21, 2026

project idea: xdsl x86 codegen

the goal is to replicate the encode and load style api of peachpy, then compare the result with llvmlite

start off by writing a matmul with 2 floats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment