Assembler Optimizer

Page 32/57
25 | 26 | 27 | 28 | 29 | 30 | 31 | | 33 | 34 | 35 | 36 | 37

By jltursan

Prophet (2619)

jltursan's picture

20-02-2021, 19:00

Summarizing:

ld hl, ?const

Needs to be corrected, no workaround.

    ld de, -(MAP)  ; +mdl
    add hl, de  ; +mdl

"(-(MAP) & 0xffff)" doesn't work either. It compiles but as Metalion says, I'm afraid this optimization rule can't be generalized to all words, the subtraction can't be higher than 32768.

; 		push hl		  ; -mdl
; 		pop hl  ; -mdl
		ld h, h  ; +mdl
		ld l, l  ; +mdl
		pop hl
		add hl,bc

Here's the untouched optimized source: https://www.sendspace.com/file/h1oaj4
Check function "shrap:"

Quote:

As for the binary not working after optimization, that worries me a bit more...

Fixing the above three problems it works. I've double-checked it and I can complete the mini-game without any flaw!

By wouter_

Champion (484)

wouter_'s picture

20-02-2021, 19:08

santiontanon wrote:

- I was thinking about the "push hl/pop hl" a bit more, and I stand corrected. If what you actually want is for the side-effect of having "hl" in the next position of the stack ...

Just a side note: When interrupts are possible (EI, or (non-MSX) machines that make use of NMIs) then at any point in time that next stack position can be overwritten. So in general it's not safe to use this trick.

By jltursan

Prophet (2619)

jltursan's picture

20-02-2021, 19:37

Quote:

If what you actually want is for the side-effect of having "hl" in the next position of the stack (so those "push hl/pop hl" should not be removed)

That's it, I want HL into the stack position and step to the next stack element. Of course the routine is executed with interrupts disabled, all "stack-blasting" routines need that.

About the -(MAP), the new syntax compiles but the program doesn't works as expected so it can't be applied for sure.

You're doing an incredible work!. I wonder what results you can get with another compilers like SDCC or Z88DK, that usually generate ugly sources.

By sjoerd

Hero (602)

sjoerd's picture

20-02-2021, 20:12

You should probably use "+(-(MAP) & 0xffff)". To make sure the assembler sees 'ld hl,x' instead of 'ld hl,(x)'.

By Grauw

Ascended (10581)

Grauw's picture

20-02-2021, 20:13

Or just remove the parentheses altogether, they’re unnecessary.

By santiontanon

Paragon (1636)

santiontanon's picture

20-02-2021, 20:29

ah, true! you are both right! It should be

    ld hl, -(MAP) & 0xffff

Without parenthesis. The parenthesis around "MAP" are inserted just in case MAP is a text macro that expands to an expression. So, those are for safety and are ok. I'll make the optimizer generate that instead then!

By jltursan

Prophet (2619)

jltursan's picture

20-02-2021, 21:19

Yes!, it compiles and works fine with "ld de, -(MAP) & 0xffff" Running Naked in a Field of Flowers

By santiontanon

Paragon (1636)

santiontanon's picture

20-02-2021, 22:44

Fantastic!!! ok, then I think we have the 3 problems solved Smile
- the "?const" was a bug (solved)
- push/pop I think the only way is to protect them with a "; mdl:no-opt" comment
- I just changed the pattern that generated the "-(MAP)" optimization to generate the expression above!

I am now fixing a few other issues that people reported to me and I hope to have time to release the new version tonight Smile

By santiontanon

Paragon (1636)

santiontanon's picture

21-02-2021, 01:34

Alright, I completed as many fixes/updates as I could this week (MANY thanks to everyone who reported issues/bugs here or via email/social media, it was awesome to have so many people trying MDL this week this time!).

There are many, many updates, but other than issues/bug fixes, maybe the main one is that there is a new flag "-asm-dialect" that generates output assembler in the target dialect. Eventually this will replace the functionality of the "-poapply" flag, which has many limitations. The problem with "-poapply" is that it takes the original source file and does text modifications to apply the optimizations. This makes it easy to preserve the original syntax, but makes it nearly impossible to work with code that has macros, for example (and a few other cases). I'll keep the "-poapply" flag around for a few versions (giving a deprecation warning) and then remove it.

In any case, new version here!
https://github.com/santiontanon/mdlz80optimizer/releases/tag/v1.5

For the next version, other than lots of minor things, my main goal is to support the "output" command of sjasm (and other dialects that have similar commands), which should expand the set of projects that MDL can be applied to! So, @jltursan, I am ready now for an AGD project that fully uses the "output" command :)

By jltursan

Prophet (2619)

jltursan's picture

21-02-2021, 11:10

Here you go: Double output test

It generates two outputs (you already have found that 3 different outputs can be generated). Needs a disk based machine to be executed.

Page 32/57
25 | 26 | 27 | 28 | 29 | 30 | 31 | | 33 | 34 | 35 | 36 | 37