0

i designed a small RISC in verilog. Which steps do I have to take to create a c compiler which uses my assembler-language? Or is it possible to modify a conventional compiler like gcc, cause I don't want to make things like linker, ...

Thanks

  • A GCC port and binutils port is not a trivial task. Is your instruction set like an existing processor that's already supported? – Rup Mar 07 '14 at 09:53
  • Been a member for a day and the first question please create/modify a C compiler. If and answer it would be a fee days work at least! – Ed Heal Mar 07 '14 at 09:54
  • 1
    I think you'd be better off trying to adapt LLVM / clang. You're going to have to investigate linker scripts, etc., whether you want to or not. – Brett Hale Mar 07 '14 at 09:55
  • 1
    @EdHeal, being a member for a day, doesn't neccesssarily mean that he started coding for a day. ;) – Devolus Mar 07 '14 at 10:01
  • @Devolus - I did not mean that. When breaking into a new group do not not test the waters a bit? – Ed Heal Mar 07 '14 at 10:05
  • There has been never a reason to be a member, I always found answers, except today... – user3392008 Mar 07 '14 at 10:06
  • @EdHeal, When I decided to join SO, I was also using it for several months already. So there is not really a relation between joining date and experience with the SO site. Though I agree that in 90% of the cases it seems so. :) – Devolus Mar 07 '14 at 10:55
  • @Rup that is a good suggestion! I've had quasi-success transforming simple RISC instructions (e.g. AVR 8bit ASM with `avr-gcc` or MIPS) to homegrown VM bytecode. – Morten Jensen Mar 07 '14 at 20:25

1 Answers1

0

You need to use an unmodified C lexer+parser (often called the front end), and a modified code generation component (the back end) to do that.

Eli Bendersky's pycparser can be used as the front end, and Atul's mini C compiler can be used as inspiration for the code generating back end: http://people.cs.uchicago.edu/~varmaa/mini_c/

With Eli Bendersky's pycparser, all you need to do is convert the AST to a Control Flow Graph (CFG) and generate code from there. It is easier to start with supporting a subset of C than the full shebang.

The two tools are written in Python, but you didn't mention any implementation language preferences :)

I have found most open sourcen compilers (except clang it seems) too tightly coupled to easily modify the back end. Clang and especially GCC are not easy to dive into, nowhere NEAR as easy as the two above. And since Eli's parser does full C99 (it parses everything I've thrown at it) it seem like a nice front end to use for further development. The examples on the Github project demonstrates most of the features of the project and it's easy to get started. The example that parses C to literal English is worth taking a look at, but may take a while to fully grok. It basically handles any C expression, so it is a good reference for how to handle the different nodes of the AST.

I also recommended the tools above, in my answer to this question: Build AST from C code

Community
  • 1
  • 1
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55