A new well-known compiler

Since we do not have native C compilers on the Oric, this forum will be mostly be used by people using CC65 or the OSDK. But any general C related post will be welcome !
User avatar
Dbug
Site Admin
Posts: 2881
Joined: Fri Jan 06, 2006 10:00 pm
Location: Oslo, Norway
Contact:

Re: A new well-known compiler

Post by Dbug » Tue Feb 12, 2019 3:41 pm

I will not answer to all the comments, but I want to start by two things:
- I don't think Opt65 has anything to do with CC65, it's something written by a C64 guy many years ago, that's why it's old style C. And yes, if we could get that thing to work (and play nice with auto-modified code, etc...) it would be nice to have, even when coding in assembler.
- Fabrice sent me last year some new code for the LCC code generator, which is supposed to be better, but I did not have time to do anything about that, so if some people feel like giving a shot, I can forward you the changes.

Now, regarding that:
Let's face it, guys... a C compiler will never generate good code for a 6502 architecture. Simply C is not designed for so limited 8-bit processors.
I disagree and for many reasons, but first of all I want to state that I am not looking for the perfect optimizing C compiler for the Oric, just one that is moderately good and allows me to test algorithms before I convert them to ASM.
I agree that it's possible to have a BETTER C compiler than what currently exist, but "will never generated good code for a 6502 architecture" is not a belief: It's a fact.

The reason is that the C is doing some assumptions on the machine model, things like "memory is linear" and "fields of structures should not be reordered" and "you can dereference an element of an array and from there get the position of the next element".

If you program in clean C, you cannot *by design* have something that will run efficiently on a 6502.

The best example is something as simple as a structure that holds some sprites information, with a X,Y position, a pointer to the bitmap to draw, and some life points. Then you have a bunch of these and put that in an array.

In 6502, you would use instead one array for each 8 bit piece of information: An array for the X, one for the Y, one for the lower part of the pointer, one for the higher part of the pointer, one for the life points.

Obviously you can do that in C as well, but what I mean, is that if you are not going to use arrays of structs, you are not really doing C anymore, and if you somewhat modified the compiler to allow that, it would not be a standard C compiler anymore (but it would run MUCH faster).

So yes, you could write a very fast compiler that runs nicely on the 6502, but that would not be a C compiler anymore.

Regarding GCC, what would be nice is actually to have a C++ compiler instead of just C, that would allow for things like deep inlining and constexpression, which would basically give compile time optimized code generation, and references too.
Even if the compiler reaches only half of the speed of a human it does not matter for me since that is good enough to evaluate the sanity of my algorithm before I switch to assembly. Even between humans coding in assembly, a difference of half speed is very easy to reach so that would be already good. Hell, even one quarter or one height speed would be fine.
At the moment, I evaluate the speed difference between LCC65 and pure assembler (both code written by me) to be about a factor of 10 on standard code, and up to x50 on convoluted things involving loops and bit-twidling
cc65: 301 ticks (6.02s)
gcc: 97 ticks (1.94s)
Except if you look a bit lower on the thread, you'll see that somebody pointed out that CC65 was not being called with the optimisation parameters.

Which reminds me that in the OSDK we are running LCC with -O2 instead of -O3, maybe there's something worth checking there.

User avatar
NekoNoNiaow
Flying Officer
Posts: 246
Joined: Sun Jan 15, 2006 10:08 pm
Location: Montreal, Canadia

Re: A new well-known compiler

Post by NekoNoNiaow » Tue Feb 19, 2019 12:49 am

Chema wrote:
Tue Feb 12, 2019 2:16 pm
Yeah, I know. But nasty things start happening with automatic int promotions, such as:

Code: Select all

    unsigned char a=200, b=200,c;
    
    c=(a+b)/2;
You are absolutely right and I just got bitten by that issue: while debugging opt65, I noticed that some additions of 8 bit values were actually compiled into 16 bit values additions, thus generating more than twice the necessary amount of work.
I had forgotten these integral promotion rules of C/C++ but some compilers have options which allow to disable them so maybe lcc65 has them?
(http://www.keil.com/support/docs/1754.htm)
Chema wrote:
Tue Feb 12, 2019 2:16 pm
Indexing also produces sub-optimal results. You can manage arrays quite quickly on the 6502 if they are less than 256 bytes long. But the C compiler does not know about this and uses 16-bit arithmetic. You probably won't have string functions using Y indexing which only work for strings < 256 chars...
I am under the impression that modern compilers such as GCC or LLVM can generate adapted indexing code when they know about the size of the data types they are manipulating. I might be wrong though and in many cases you are right that they cannot detect that.
This said, with some hints (gcc's attributes) one could help them choose the proper ones.
Ex: __attribute( "8bitindex" ) char* mypointer = 0; // Indexes on this pointer will be 8bit.
Chema wrote:
Tue Feb 12, 2019 2:16 pm
And the register usage... well if only we had a few general-purpose registers... but there is only A, then the indexes, which can be used partially for some operations. You can use page zero, of course, which is what most compilers do, but beware that some (namely cc65) uses almost all the zero page for its needs (or that I read).
Yup, but modern compilers are quite good at that. They can juggle memory and registers very efficiently, even using X or Y for temporary storage to avoid touching memory if the code generating backend is properly written.
Chema wrote:
Tue Feb 12, 2019 2:16 pm

In any case, it was my fault jumping with my personal rants :wink: into your thread, because, as you said, you have clearly stated your idea many times, and my post was along another different path, and (frankly) completely unhelpful. I am really sorry for that. As I said it was just my discussion-eager me :mrgreen:

I sincerely apologize.
Hey, do not apologize, you provided interesting and valuable feedback! ;)
I certainly welcome discussion of any kind as long as it is on topic (and your input mostly was).

Chema wrote:
Tue Feb 12, 2019 2:16 pm
And that is indeed impressive! I know Fabrice Frances once took a look at the 65C02 target for gcc, but never adapted it for 6502... http://oric.free.fr/software.html
Argh, I did not know about that one but I suspect that version is now quite obsolete since GCC's internal architecture has evolved and improved a lot since (due to the pressure of LLVM).

User avatar
NekoNoNiaow
Flying Officer
Posts: 246
Joined: Sun Jan 15, 2006 10:08 pm
Location: Montreal, Canadia

Re: A new well-known compiler

Post by NekoNoNiaow » Tue Feb 19, 2019 1:03 am

Dbug wrote:
Tue Feb 12, 2019 3:41 pm
I will not answer to all the comments, but I want to start by two things:
- I don't think Opt65 has anything to do with CC65, it's something written by a C64 guy many years ago, that's why it's old style C. And yes, if we could get that thing to work (and play nice with auto-modified code, etc...) it would be nice to have, even when coding in assembler.
Oh, thanks for the information and sorry about the confusion I might have introduced.
And I confirm officially that I am working on opt65. I have been debugging it on a small example and so far I have not yet found any mis-optimizations but I guess that will arrive soon enough. ;)
Dbug wrote:
Tue Feb 12, 2019 3:41 pm
- Fabrice sent me last year some new code for the LCC code generator, which is supposed to be better, but I did not have time to do anything about that, so if some people feel like giving a shot, I can forward you the changes.
Oh, interesting. I could indeed give it a go as well.
That coupled with a working opt65 could be a nice update for the OSDK.
Dbug wrote:
Tue Feb 12, 2019 3:41 pm
I agree that it's possible to have a BETTER C compiler than what currently exist, but "will never generated good code for a 6502 architecture" is not a belief: It's a fact.
I globally agree, I only expect a "moderately good" result, not even a "good" one. ;)
Dbug wrote:
Tue Feb 12, 2019 3:41 pm
In 6502, you would use instead one array for each 8 bit piece of information: An array for the X, one for the Y, one for the lower part of the pointer, one for the higher part of the pointer, one for the life points.

Obviously you can do that in C as well, but what I mean, is that if you are not going to use arrays of structs, you are not really doing C anymore, and if you somewhat modified the compiler to allow that, it would not be a standard C compiler anymore (but it would run MUCH faster).
You are absolutely correct.
I actually write my C code in this style since my algorithms are really targeting the Oric and are not made to be platform agnostic.

In essence I am really using C only to have the convenience of easily readable variables and control flow branches but not using any structs or functions. For my needs, it is really only a high level assembly language of sort.
The only thing I currently expect from a "moderately good" compiler would be to generate assembly code that is reasonably understandable at first glance even if it is not super efficient, this way I could rewrite the algorithm starting from the generated code rather than from scratch.
Dbug wrote:
Tue Feb 12, 2019 3:41 pm
So yes, you could write a very fast compiler that runs nicely on the 6502, but that would not be a C compiler anymore.

Regarding GCC, what would be nice is actually to have a C++ compiler instead of just C, that would allow for things like deep inlining and constexpression, which would basically give compile time optimized code generation, and references too.
Absolutely! These would allow interesting optimizations indeed.
From what I understand, the version of GCC I linked to (in the Atari age forum) does support C++ since it is just the backend part of GCC and C++ has already been transformed into internal representation at this stage.
I do not know if that version supports constexpr though, you would have to verify. ;)
Dbug wrote:
Tue Feb 12, 2019 3:41 pm
cc65: 301 ticks (6.02s)
gcc: 97 ticks (1.94s)
Except if you look a bit lower on the thread, you'll see that somebody pointed out that CC65 was not being called with the optimisation parameters.

Which reminds me that in the OSDK we are running LCC with -O2 instead of -O3, maybe there's something worth checking there.
Ah, excellent point. I missed that.
I will experiment with -O3 while working on opt65! Thanks for the info!

User avatar
polluks
Pilot Officer
Posts: 65
Joined: Tue Jun 05, 2012 10:09 pm
Location: Germany
Contact:

Re: A new well-known compiler

Post by polluks » Tue Mar 05, 2019 2:21 pm

By the way opt65 was developed by Daniel Dallmann.
See his https://en.wikipedia.org/wiki/LUnix.
cc65 development
Oric Atmos + Cumulus
Acorn Electron

User avatar
NekoNoNiaow
Flying Officer
Posts: 246
Joined: Sun Jan 15, 2006 10:08 pm
Location: Montreal, Canadia

Re: A new well-known compiler

Post by NekoNoNiaow » Tue Mar 05, 2019 10:14 pm

polluks wrote:
Tue Mar 05, 2019 2:21 pm
By the way opt65 was developed by Daniel Dallmann.
See his https://en.wikipedia.org/wiki/LUnix.
Wow, Unix on the C64/128, fascinating! \(oO)/

(I wonder if that could be ported to the Oric/other-8-bitters? Probably.)
I could not find anything about Daniel Dallmann when I was looking for information about opt65 a few weeks ago so this might be a useful lead. Thanks! ;)

User avatar
ibisum
Wing Commander
Posts: 1078
Joined: Fri Apr 03, 2009 8:56 am

Re: A new well-known compiler

Post by ibisum » Wed Mar 06, 2019 9:23 pm

>(I wonder if that could be ported to the Oric/other-8-bitters? Probably.)

I think Orix has a bit more potential: http://orix.oric.org

User avatar
polluks
Pilot Officer
Posts: 65
Joined: Tue Jun 05, 2012 10:09 pm
Location: Germany
Contact:

Re: A new well-known compiler

Post by polluks » Thu Mar 21, 2019 1:56 pm

NekoNoNiaow wrote:
Tue Mar 05, 2019 10:14 pm
(I wonder if that could be ported to the Oric/other-8-bitters? Probably.)
Sure :wink: https://github.com/jedeoric/Lunix/blob/ ... EADME.ORIC
cc65 development
Oric Atmos + Cumulus
Acorn Electron

Post Reply