Last active
February 21, 2026 23:47
-
-
Save sueszli/f971a7f0f8fcdf9bc60d318d11662b38 to your computer and use it in GitHub Desktop.
x86 backend for xdsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; $ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; $ 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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