Note on sdcc

A General Discussion forum for TI calculators

Moderator: MaxCoderz Staff

King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Note on sdcc

Post by King Harold »

When you have a function which calls an other function, the arguments are first read from the stacking using:

Code: Select all

push ix
ld ix,0
add ix,sp

ld l,(ix+2)
ld h,(ix+3)
;etc..
;do stuff

ld sp,ix
pop ix
ret
Ok, that is somewhat odd..

But now comes the strange part:
Then the arguments that the function which is called by your function are pushed on the stack..
What? those things you just laboriously read from it??
..exactly..

That you need a copy of the variables I can understand, but not like this.

Like so:

Code: Select all

long Div(long l1, long l2)
{
	return l1/l2;
}
Becomes:

Code: Select all

;test.c:1: long Div(long l1, long l2)
;	genLabel
;	genFunction
;	---------------------------------
; Function Div
; ---------------------------------
_Div_start::
_Div:
	push	ix
	ld	ix,#0
	add	ix,sp
;test.c:3: return l1/l2;
;	genIpush
; _saveRegsForCall: sendSetSize: 0 deInUse: 0 bcInUse: 0 deSending: 0
;	AOP_STK for 
	ld	l,10(ix)
	ld	h,11(ix)
	push	hl
	ld	l,8(ix)
	ld	h,9(ix)
	push	hl
;	genIpush
;	AOP_STK for 
	ld	l,6(ix)
	ld	h,7(ix)
	push	hl
	ld	l,4(ix)
	ld	h,5(ix)
	push	hl
;	genCall
	call	__divslong_rrx_s
	ld	b,h
	ld	c,l
	pop	af
	pop	af
	pop	af
	pop	af
;	genRet
; Dump of IC_LEFT: type AOP_REG size 4
;	 reg = debc
	ld	l,c
	ld	h,b
;	genLabel
;	genEndFunction
	pop	ix
	ret
_Div_end::
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

King Harold: SDCC is trying to be a "real" compiler and implement a stack frame with ix. That is how C compilers do it like GCC, and the others. That is why it is simply illogical to try and target a C compiler to z80.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Well, SDCC shouldn't try to be so "real" then, this code is ridiculous.
Apart from this stack mess though, sdcc isn't too bad

It would be better if you could use fastcall..
User avatar
DJ_O
Calc King
Posts: 2323
Joined: Mon 20 Dec, 2004 6:47 pm
Location: Quebec (Canada)
Contact:

Post by DJ_O »

what is SDCC? Would someone have started competition with Halifax's HACC compiler? :roll:
ImageImageImageImage
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

DJ Omnimaga wrote:what is SDCC? Would someone have started competition with Halifax's HACC compiler? :roll:
Not exactly, sdcc has been around for quite a while...
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

Yes it has been around. Optimization isn't the best, but it is dealable.

@King_Harold: Don't gripe about it here, talk to the developer. It is easier for the compiler to maintain a stack frame though.
User avatar
DJ_O
Calc King
Posts: 2323
Joined: Mon 20 Dec, 2004 6:47 pm
Location: Quebec (Canada)
Contact:

Post by DJ_O »

aaah I see, thanks for the link CoBB.
ImageImageImageImage
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Halifax wrote:Yes it has been around. Optimization isn't the best, but it is dealable.

@King_Harold: Don't gripe about it here, talk to the developer. It is easier for the compiler to maintain a stack frame though.
Good idea, but hey, I can live with it (after all, I could just optimize it a bit myself easily)
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

Or code all assembly ^^
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

I usually do that, but I find some routines (especially floating-point math) a bit annoying to code in assembly..

edit: Wouldn't it be far more practical if z80-C compilers would just take a few lines of code (a function of whatever) and turn it in to optimized assembly? Then just copy-paste it to your own assembly and make sure the inputs and outputs are right and that it preserves the right things etc..
Then there would be no need for annoying stack frames that get implemented in the slowest possible (without adding do-nothing stuff) way.
Also, I've been feeling the need sometimes to have a compiler write (for example) a division routine for things bigger than 1 byte (I'm lazy I know) but they all cheat and use their library (which is usually ok, but not in this case).
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

Yes, floating point math is tedious.

Uhh, but I have no idea what you just said. There is more of a process to C compilation, then I think you really realize. Why don't you try writing down a few examples of C yourself, and see how you would optimize them. Is the process to time consuming? Not at all possible to do in a compiler?

Believe me it is harder than you think.
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Post by kalan_vod »

King Harold wrote:I usually do that, but I find some routines (especially floating-point math) a bit annoying to code in assembly..

edit: Wouldn't it be far more practical if z80-C compilers would just take a few lines of code (a function of whatever) and turn it in to optimized assembly? Then just copy-paste it to your own assembly and make sure the inputs and outputs are right and that it preserves the right things etc..
Then there would be no need for annoying stack frames that get implemented in the slowest possible (without adding do-nothing stuff) way.
Also, I've been feeling the need sometimes to have a compiler write (for example) a division routine for things bigger than 1 byte (I'm lazy I know) but they all cheat and use their library (which is usually ok, but not in this case).
Similar to EZAsm, but not a good way of doing it.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Halifax wrote:Yes, floating point math is tedious.

Uhh, but I have no idea what you just said. There is more of a process to C compilation, then I think you really realize. Why don't you try writing down a few examples of C yourself, and see how you would optimize them. Is the process to time consuming? Not at all possible to do in a compiler?

Believe me it is harder than you think.
Oh no it isn't, I know full well how hard it is. But then, I never said I could write a better compiler/optimizer. Other people should (there are bound to be people who can)

Well isn't that what tools are for? Do tedious work for you?
If it doesn't, I'd consider it useless. If the work is not tedious, I could just as well do it myself.

Anyway, if people can write fast/good assembly, then in theory compilers could too. So why don't they? Because their developers just aim for something that works instead of something that is good? Because that is easier? Imo they miss the point.
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Well, sdcc wasnt designed specifically with calcs in mind. Plus a lot of things might not be 100% performance critical ... not to mention the whole point of using a c compiler would be to save time.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
Timendus
Calc King
Posts: 1729
Joined: Sun 23 Jan, 2005 12:37 am
Location: Netherlands
Contact:

Post by Timendus »

King Harold wrote:Wouldn't it be far more practical if z80-C compilers would just take a few lines of code (a function of whatever) and turn it in to optimized assembly? Then just copy-paste it to your own assembly and make sure the inputs and outputs are right and that it preserves the right things etc..
You could do that, but it would make any project unmanageable. You'd be working with code that you don't really understand, that is not commented, et cetera. What if you get a bug somewhere? You'd be hunting it for ages, especially in a team project where you don't understand much of the other (commented, I'd hope) code either.

The reason I'd want to work in C instead of assembly, especially on a team project, is to get more manageable and more structured code; this would do the opposite.
http://clap.timendus.com/ - The Calculator Link Alternative Protocol
http://api.timendus.com/ - Make your life easier, leave the coding to the API
http://vera.timendus.com/ - The calc lover's OS
Post Reply