[z88dk-dev] [z80asm] opportunity for small linking optimization

Bridge to the z88dk-developers mailing list
Post Reply
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

[z88dk-dev] [z80asm] opportunity for small linking optimization

Post by alvin »

In the new c lib, the asm implementation of library functions is separated from the C wrapper so that any C compiler can be accommodated by the library. Quite often this results in code like this:


============

;; C wrapper

_srand:

pop af
pop hl

push hl
push af

jp asm_srand

;; asm implementation

asm_srand:

; enter : hl = seed
;
; uses : af

ld a,h
or l
jr nz, seed_ok

inc l

seed_ok:

ld (__stdlib_seed),hl
ld (__stdlib_seed + 2),hl

ret

============


Because the linker will very likely run into use of "srand" first and then resolve "asm_srand", the code in memory usually looks like the above too. This means you will see "jp foo; foo:" in code in quite a few places.

Is it possible to get the linker to eliminate the jump in these cases? Even if there is a label attached to the jump, things should still be fine if the label points at the next byte after the jump.

If a large program calls 40 library functions, this has the potential to save 120 bytes. It's not huge but it does help.



------------------------------------------------------------------------------
pscust
Well known member
Posts: 194
Joined: Thu Jun 23, 2011 3:34 pm

Post by pscust »

It is a good idea and I have added it to the wish list for the new z80asm.

It cannot be implemented in the current z80asm because the object file
representation it uses is a binary image with patching information. To
implement link-time optimizations we need to store the intermediate code in
the object file - the linker must know if a 0xC3, 0x00, 0x00 is a jump
instruction or three data bytes.

I have started using the z88dk git repository and created a wiki page
listing the wish-list for the new assembler - please feel free to review
and edit it. The wiki is at https://github.com/z88dk/z80asm/wiki

Regards.
Paulo


On Thu, Aug 25, 2016 at 1:55 AM, alvin (alvin_albrecht@...) <
lists@...> wrote:
In the new c lib, the asm implementation of library functions is separated
from the C wrapper so that any C compiler can be accommodated by the
library. Quite often this results in code like this:


============

;; C wrapper

_srand:

pop af
pop hl

push hl
push af

jp asm_srand

;; asm implementation

asm_srand:

; enter : hl = seed
;
; uses : af

ld a,h
or l
jr nz, seed_ok

inc l

seed_ok:

ld (__stdlib_seed),hl
ld (__stdlib_seed + 2),hl

ret

============


Because the linker will very likely run into use of "srand" first and then
resolve "asm_srand", the code in memory usually looks like the above too.
This means you will see "jp foo; foo:" in code in quite a few places.

Is it possible to get the linker to eliminate the jump in these cases?
Even if there is a label attached to the jump, things should still be fine
if the label points at the next byte after the jump.

If a large program calls 40 library functions, this has the potential to
save 120 bytes. It's not huge but it does help.



------------------------------------------------------------
------------------
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

Adding reply not reflected in the forum:

It is a good idea and I have added it to the wish list for the new z80asm.

It cannot be implemented in the current z80asm because the object file representation it uses is a binary image with patching information. To implement link-time optimizations we need to store the intermediate code in the object file - the linker must know if a 0xC3, 0x00, 0x00 is a jump instruction or three data bytes.

I have started using the z88dk git repository and created a wiki page listing the wish-list for the new assembler - please feel free to review and edit it. The wiki is at https://github.com/z88dk/z80asm/wiki

Regards.
Paulo
------------------------------------------------------------------------------
Post Reply