C compiler

PM development, programming, hacking and all that fun stuff.
User avatar
YasaSheep
Posts: 205
Joined: November 20th, 2005, 04:04
Location: Portland (Also the discord)
Contact:

Re: C compiler

Post by YasaSheep »

The wiki is a great resource for understanding the architecture. :] Alternatively, I'm sure JustBurn and asterisk particularly know a lot about how the PM works and you could get some info from them. (They made pokemini and minimon, respectively, by the way.)
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc »

Short report on the status. I was looking at the vbcc and it seems to be feasible. It probably would take a week of full time work or so. What has to be done is adapt either hc12 back-end, the generic one or write one from scratch. The problem is I'd have to clean up the hc12 specifics first. I was looking into other options and the vbcc seems to be the best in our case, considering the ratio outcome/effort. So no progress so far but I'm rather optimistic.
Last edited by zoranc on July 25th, 2012, 12:10, edited 1 time in total.
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc »

OK some moderate progress. I managed to compile a very simple C program: http://pastebin.com/33HCQNx5 It draws a diagonal line on the screen. The result is visible here: http://pastebin.com/HJTC9DP8

Some of the reasons it is so badly optimized code is that PM architecture is kind of weird. There are instruction for accessing [sp+offset] for 16 bit arguments but not for 8 bit. Also I have no optimization at all in the backend.

In the attachment you can see my changes. You have to put them on top of the official vbcc compiler: http://compilers.de/vbcc.html . You'll need unix like environment. I build it with MinGW and it works perfect. Also MacOS did it fine. I guess it would work on Linux too.

What I did is modified hc12 backend. It is still the beginning maybe 15%-20% done. Still a lot to be done! A mean a lot!!!
Attachments
vbcc_pm_001_add.zip
(35.28 KiB) Downloaded 1075 times
User avatar
YasaSheep
Posts: 205
Joined: November 20th, 2005, 04:04
Location: Portland (Also the discord)
Contact:

Re: C compiler

Post by YasaSheep »

Great to see. :D
Couple suggestions though:
I'd recommend using memory for variables instead of the stack, seems more elegant and in most cases will save you from adding to the stack.
And at least in pmas you can prepend a label with _ to make the name unique to only other labels under the previous label without a _
So those l#s could be _l# under main and then for multiple functions you could have the same _l# under it and it won't matter.

Also, add sp,-7? :S Why not sub sp,7?
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc »

Thank you for your interest!
Wa wrote:Great to see. :D
Couple suggestions though:
I'd recommend using memory for variables instead of the stack, seems more elegant and in most cases will save you from adding to the stack.
And at least in pmas you can prepend a label with _ to make the name unique to only other labels under the previous label without a _
So those l#s could be _l# under main and then for multiple functions you could have the same _l# under it and it won't matter.
I'm not sure that I understand you correctly, but I think you suggest static memory storage for local variables. This very bad idea for two reasons:

1. Consumes much more RAM (which is anyway tight on the PM). Imagine that main() calls functions f1(), f2() and f3() in a sequence. Each of f1-f3 requires 20 bytes for locals, that would make 60 bytes of the static space. On the other hand if they use stack it will be 20 bytes for all.

2. Much more important - recursive calls (including hidden recursion) will not work correctly.
Wa wrote:Also, add sp,-7? :S Why not sub sp,7?
Does it matter? They are equivalent. No particular reason to chose twice add, but the code is made for the assembler, not to be read by humans. It might ease simulation one day if there are fewer instructions but this is not so important.
User avatar
YasaSheep
Posts: 205
Joined: November 20th, 2005, 04:04
Location: Portland (Also the discord)
Contact:

Re: C compiler

Post by YasaSheep »

It doesn't matter, I was just curious.

But yes I was suggesting using the RAM. Stack or RAM doesn't matter so much though since it goes to the same place. I figured it'd just save you some ops in some places. :p
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc »

Short update. Compiler gets into semi-workable state. It is still pre-pre-alpha infancy, but I was able to compile and run some simple drawing routines. Most important is that I got a feeling of it so am a bit more confidant that I will be able to bring it into usable condition.

Only int type is supported at the moment, so char and possibly long are the next additions. Didn't test structs, and tested the arrays just a little. Most notable lacks or operations are multiplication, division and module. Any volunteer to write those in assembler?

Oh and no floating point support at all. Not sure if will ever get to those... Also no C standard library either.

Performance is not much my concern. I would first like to have it feature complete, and then maybe think of optimizing the generated code. Probably will upload some version of the compiler in the next days so you guys can try it.
Lupin
Team Pokémé
Posts: 94
Joined: January 10th, 2005, 13:06

Re: C compiler

Post by Lupin »

Good news. We don't really need the standard C library now...

I might try my luck on division/multiplication on the weekend. What bit sizes do you need to multiply/divide? 8*8=16 bit and 16*16=32 bit ?
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc »

8*8 = 16 is available as cpu instruction, so i'll use it. I will definitely need 16*16 = 16 very soon, and same for div, mod. Bigger versions 32*32=32 will be needed for the long ints and maybe also 16*16 = 32 but right now i'm not sure if i'll be able to use those.

You are right, concerning c-lib besides the only ones i can think of that would be useful now are sprintf and memcpy variations.

Once I get firs semi-usable version out will start a TODO list with priorities...
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc »

Brag time once more, this code: http://pastebin.com/ryZttMW2 produces the attached result.
Attachments
PutPixel.min
the binary
(12.2 KiB) Downloaded 1109 times
screen shot
screen shot
screen.png (7.77 KiB) Viewed 18549 times
Post Reply