Assembler Optimizer

Page 54/56
47 | 48 | 49 | 50 | 51 | 52 | 53 | | 55 | 56

By Arjan

Paladin (756)

Arjan's picture

02-01-2022, 14:57

Hi santiontanon, I added some issues to your github repository. If these are fixed then mdlz80optimizer can be used to optimize the code for that awesome SNES platformer port.

It still won't be able to generate a valid listing because there is no support for the --longptr commandline directive sjasmplus has to allow the PC to go over $ffff. Are you going to add support for commandline directives that changes the assemblers behaviour?

By santiontanon

Paragon (1568)

santiontanon's picture

03-01-2022, 00:50

Thanks Arjan! I was just going through them right now!! A few of them should be fairly easy, and I will try to add asap! Probably as soon as I am back from my Christmas vacation Smile

As for "--longptr", that should not be too hard (I think). If I remember correctly, I have the 16bit limit hardcoded in a few places in the codebase, but I hope I can just move that to a global constant that can be configured via commandline arguments. So, I can probably fix that too!

By Arjan

Paladin (756)

Arjan's picture

03-01-2022, 10:25

Great!

By ARTRAG

Enlighted (6690)

ARTRAG's picture

03-01-2022, 12:45

Hi Santi, I've left MDL for a while and now I'm trying to integrate it in an SDCC tool chain...
Do you have any example to follow for this compiler ?

By santiontanon

Paragon (1568)

santiontanon's picture

03-01-2022, 23:31

Oh, I did integrate MDL into the anchor's aweigh, and basically, in one of the Makefile rules, I just added a call to MDL like this:

@java -jar mdl.jar tmp/$*.source.asm -dialect sdcc -ro -po -asm-dialect tmp/$*.source.mdl.asm

That basically, optimized the sdcc-generated assembler files, before sending them for final assembly!

Would that work in your setup?

By Bengalack

Champion (480)

Bengalack's picture

05-01-2022, 19:17

ARTRAG wrote:

Hi Santi, I've left MDL for a while and now I'm trying to integrate it in an SDCC tool chain...
Do you have any example to follow for this compiler ?

I have been using it in my tool-chain. I make a bunch of rel-files up front from c and asm-files (put into OBJLIST), and then I link them together at the end.

In this case, %MSX_FILE_NAME% is "game". sdcc produces "game.asm". The trick after this, is to use the "-g" switch when generating the rel-file.

from make.bat:

...
set MODULE=%MSX_FILE_NAME%
set SRC=%MODULE%.c

set ASM_CALL=sdcc --less-pedantic -mz80 %OPT% -c -I. -o %MSX_OBJ_PATH%\ %SRC%
echo %ASM_CALL%
%ASM_CALL%

if %errorlevel% NEQ 0 (
EXIT %errorlevel%
)

set SRC=%MSX_OBJ_PATH%\%MODULE%.asm
set REL=%MSX_OBJ_PATH%\%MODULE%.rel


echo -------------------------------------------------
echo Use MDL on the .asm-file

set MDL=%SRC%.mdl.asm
set mdl_cmd=java -jar mdl.jar %SRC% -ansion -dialect sdcc -ro -po speed -asm-dialect %MDL%
echo %mdl_cmd%
%mdl_cmd%

set OBJLIST=%OBJLIST% %REL%

set ASM_CALL=sdasz80 %FLAGS% -g %REL% %MDL%
echo %ASM_CALL%
%ASM_CALL%
...

By ARTRAG

Enlighted (6690)

ARTRAG's picture

05-01-2022, 20:56

Thanks Bengalack!
I will try your script for sure! I'm just now starting with SDCC and I see that v4.1.12 has finally started to use registers to pass parameters. Nevertheless the generated code is in many ways not optimised.
This code is generated by a function of few lines by SDCC v4.1.12

_mySetAdjust::
;mytest.c:378: unsigned char value = ((x-8) & 15) | ((y-8) & 15)<<4;
	add	a, #0xf8
	and	a, #0x0f
	ld	c, a
	ld	a, l
	add	a, #0xf8
	and	a, #0x0f
	add	a, a
	add	a, a
	add	a, a
	add	a, a
	or	a, c
	ld	c, a
;mytest.c:379: Poke(0xFFF1,value);     					// Reg18 Save
	ld	hl, #0xfff1
	ld	(hl), c
;mytest.c:380: myVDPwrite(value,18);
	ld	l, #0x12
;	spillPairReg hl
;	spillPairReg hl
	ld	a, c
;mytest.c:381: }
	jp	_myVDPwrite

It is overall fine, expecially if compared to v4.1.0, where all parameters were passed on the stack, but this part

	ld	c, a
	ld	hl, #0xfff1
	ld	(hl), c
	ld	l, #0x12
	ld	a, c
	jp	_myVDPwrite

could become:

	ld	(#0xfff1),a
	ld	l, #0x12
	jp	_myVDPwrite

It is very annoying to see SDCC is leaving this kind of leftover...

By santiontanon

Paragon (1568)

santiontanon's picture

05-01-2022, 23:45

It would be very interesting if we can find some new patterns to add to MDL to compensate for known patterns that SDCC leaves unoptimized! For example if that example you show at the end is a common occurrence, we can add it as a pattern (if MDL does not optimize it yet!)

By ARTRAG

Enlighted (6690)

ARTRAG's picture

06-01-2022, 14:42

How would you implement a rule for the above example?
SDCC is always using HL or IX for loading values in ram even if a single byte

By santiontanon

Paragon (1568)

santiontanon's picture

07-01-2022, 01:27

I just added these two new patterns, and the code above is now optimized as you propose Smile

pattern: remove unnecessary ld ?reg1, ?reg2
0: ld ?reg2, ?reg1
1: *
2: ld ?reg1, ?reg2
replacement:
0: ld ?reg2, ?reg1
1: *
constraints:
regsNotModified(1, ?reg1)
regsNotModified(1, ?reg2)

pattern: replace ld ?reg2, a; ld hl, ?const1; ld (hl), ?reg2 with ld (?const1), a
0: ld ?reg2, a
1: ld hl, ?const1
2: ld (hl), ?reg2
replacement:
0: ld (?const1), a
constraints:
regsNotUsedAfter(2, ?reg2)
regsNotUsedAfter(2, hl)

So, that should be there in the next release Smile

Page 54/56
47 | 48 | 49 | 50 | 51 | 52 | 53 | | 55 | 56