New SNES C Compiler

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
New SNES C Compiler
by on (#78012)
Hi all,

It's been a while since I've been to the forum, but I've finished my compiler construction class and really enjoyed building the compiler this semester. I'm thinking about building a C compiler for the SNES from scratch. I know that I could use GCC as a base for the compiler, but the source code is a bit archaic. I'd like to make it as close to ANSI C as possible granted the functions are capable on the hardware. I understand things like floating point operations won't work without a math coprocessor. The 65816 has the ability to shell out floating point operations to a coprocessor from what I understand so it could be possible to add support for this later. Also, would it be possible to handle 32 bit integers since the SNES has 16 bit registers? I know that the 65816 can operate in 16 or 8 bit mode but I suppose we could store the low and hi byte in two registers while in 16 bit mode so 32 bit integers could be possible as well. As far as multiplication and division, I understand the SNES has hardware support for those. I remember reading that the CPU cycles required for hardware multiply and divide are not precisely known and thus can't be emulated perfectly. I'll look into implementing bit shifting where possible so that there can be code optimizations on multiplication and division.

If I build all the necessary parts such as scanner, parser, semantic analyzer, escape analyzer, and code generation, would you guys be willing to help with code optimizations and or libraries for the SNES? I'll write the register allocator and liveness analyzer for the compiler as well, but I'm not as familiar with the SNES architecture as I am with the MIPS. Please let me know your thoughts. Thanks much.

by on (#78033)
Quote:
The 65816 has the ability to shell out floating point operations to a coprocessor from what I understand

I suppose it could if you had built a custom cart with an FPU. No SNES game out there does afaik. Fixed point was used by some of the SNES DSPs.

Quote:
I suppose we could store the low and hi byte in two registers while in 16 bit mode so 32 bit integers could be possible as well

Only the accumulator can be used for arithmetic operations (besides INC/DEC) so you need to use a few zeropage variables for this.

Quote:
code optimizations

Pattern optimizers aren't that complicated. Scan for pattern X, replace with shorter/faster pattern Y. I've already written one for use with tcc-816, which you can find here.

by on (#78105)
Thanks for the pointers. I have finally finished my semester classes and finals so I should have some more time to work on the compiler. For the lexical analyzer, I'm using Flex and Bison with the ANSI C grammars specified here: http://www.quut.com/c/ANSI-C-grammar-y.html and http://www.quut.com/c/ANSI-C-grammar-l-1998.html

Once I can get a pretty printer working, I will create a Google code repository so that people may check out the code. Then I will create the semantic analyzer to do type checking and syntax checking. Hopefully that phase will not take long.

by on (#78109)
pcmantinker wrote:
Thanks for the pointers. I have finally finished my semester classes and finals so I should have some more time to work on the compiler. For the lexical analyzer, I'm using Flex and Bison with the ANSI C grammars specified here: http://www.quut.com/c/ANSI-C-grammar-y.html and http://www.quut.com/c/ANSI-C-grammar-l-1998.html

Once I can get a pretty printer working, I will create a Google code repository so that people may check out the code. Then I will create the semantic analyzer to do type checking and syntax checking. Hopefully that phase will not take long.


flex and bison are ok (I've used them before twice), but I recommend at least investigating using the "lemon parser". Its really neat:

http://www.hwaci.com/sw/lemon/