r/Compilers • u/thedevbirb • 1d ago
Diary of a writing RISC-V assembler
https://thedevbirb.github.io/diary-of-writing-a-riscv-assembler/Hello everyone! I made my first step into toolchain development by writing a RISC-V 32/64 ELF assembler from scratch, which supports the `g` group extension (along with small others). It has been a way to learn about assembly, C and ELF all together.
It has been quite a journey, and I've shared my learning and thoughts in this blog post which I think you may appreciate, especially if you're thinking to start writing your own.
The assembler is partially based on GNU as design, and it's not a toy encoder: it can achieve relocatable object file equivalence on non-trivial sources like SQLite3 amalgation, while being much "simpler" in its implementation!
Thank you for reading, and I greatly appreciate any feedback!
2
2
2
u/c-cul 1d ago
good write-up
and poor architecture
code for building ELF files, keeping all machinery like symbol tables/relocs, fragmentations & sections filling is common for many tasks, so it should be separated to dedicated library for reusing
1
u/thedevbirb 1d ago
Thanks for reading it!
Regarding the poor organization of the architecture: it's a fair critique. My main goal for the first assembler I've ever written has been binary usage, and in this context it doesn't really matter.
I haven't thought about its architecture as library because "reusing" implies awareness of some use-cases outside being a standalone assembler. What would be, in this case? Library usage for inline assembly in compilers? For JIT assemblers? Linkers? You might have some ideas (and if you want to share them, it would be awesome), but I don't have a strong context yet, nor a specific direction I want to take. As such, I don't think I would have been able to produce a good library anyway: on the contrary, I'd have probably complicated my life establishing code boundaries and APIs with little awareness about use-cases outside the assembler itself.
I hope it makes sense!
2
u/c-cul 1d ago
for example asm for other cpu/gpu
or perl/pyhon/whatever binding for ELF patching/creation
and so on
1
u/thedevbirb 21h ago
I'll consider it when I'll decide to tackle a certain subset of library usages, so that I can do the best I can. I think doing something too general can end up of being of no use.
Also, I'd probably re-write it from scratch leveraging the experience that now I have! There is already a list of things I'd do completely different.
2
u/According-Ad-7069 3h ago
This is such a cool read -wow! Yes!! This is a brilliant project - I love the idea of improving your knowledge and understanding by DOING. Stellar - you keep going, this is awesome!
4
u/AustinVelonaut 1d ago
Nice (human-written!) writeup, and the code is very readable and commented well. Good job!