Assembler Optimizer

Page 27/57
20 | 21 | 22 | 23 | 24 | 25 | 26 | | 28 | 29 | 30 | 31 | 32

By santiontanon

Paragon (1639)

santiontanon's picture

31-01-2021, 20:12

Updated Smile

So, binary constants of the form 0b0000 should be supported now, and I also added a few of those weird asdz80 spellings for opcodes that were missing.

Latest release in github, as usual: https://github.com/santiontanon/mdlz80optimizer/releases

By Bengalack

Hero (590)

Bengalack's picture

31-01-2021, 20:34

Great.

Possibly, the most off-putting notation may be the indexing-notation Smile The offset is written outside the parenthesis, like this:

ld a, -5(iy)

The documentation for the asz80-compiler is here: https://shop-pdp.net/ashtml/asz80.htm Check for "Implicit Operand Instructions". Thing is, they have an "alias" for several commands. It means that when the command is like "add b", they also accepts "add a, b". Now, people don't usually write "add a, b", but SDCC does. In my example, things make sense, but when you come to this:

rr [reg] is the same as rr a, [reg], then it becomes wierd.

Note that the documentation is wrong for these two:

dec     a,[]
inc     a,[]

They do not work, and should/need not be added.

By Bengalack

Hero (590)

Bengalack's picture

31-01-2021, 20:33

I tested my .s-file again. now I get this:

C:\source\repos\noname\project>java -jar mdl.jar allsorts.s -dialect sdcc -poapply -po
ERROR: parseRestofTheLine: Cannot parse line allsorts.s#2127: .rept 31  ;# note, one below too  left over tokens: [31, ;# note, one below too]

I use the .rept/.endm macro quite a bit.

Not sure how your macro support is, but here are two other examples of macros that might need to be handled?

This macro can take two parameters:

;-------------------------
; Modifies: A
;-------------------------
.macro vdpOut, registercommand, data
	ld 	a, data
	out ( #0x99 ), a
	ld 	a, registercommand
	out ( #0x99 ), a
.endm

And another variant being used is the following. I'm doing it like this when there are labels inside the macro:

;-------------------------
; Modifies: AF
; Wait for VDP-commands. No parameter should be given
;-------------------------
.macro vdpReadyNI ?noparameter_VDPready

	ld	a,#2
	out	(#0x99),a          ;select status register 2
	ld	a,#128+#15
	out	(#0x99),a
noparameter_VDPready:
	in	a,(#0x99)

	and #1
	jp	nz, noparameter_VDPready	; wait TODO: double check that we really can pump values from in. and not set reg for every read.

	xor	a						; always set S=0 when leaving
	out	(#0x99),a
	ld	a,#128+#15 ; 0x8F
	out (#0x99),a
.endm

By Bengalack

Hero (590)

Bengalack's picture

31-01-2021, 20:39

I also tested the SDCC-generated asm-file again. I see some warnings on redefines. It's the result of inline-assembly in the c-code. It can be removed, if it is a problem. Is it? Anyway, output is this:

C:\source\repos\noname\project>java -jar mdl.jar Debug\\objs\\game.asm -dialect sdcc -poapply -po
WARNING: Redefining symbol EXPTBL
WARNING: First defined in Debug\\objs\\game.asm, 12640 as 64705: EXPTBL: .equ 64705
WARNING: Redefined in Debug\\objs\\game.asm#12667:      EXPTBL  .equ 0xFCC1
WARNING: Redefining symbol EXPTBL
WARNING: First defined in Debug\\objs\\game.asm, 12640 as 64705: EXPTBL: .equ 64705
WARNING: Redefined in Debug\\objs\\game.asm#12697:      EXPTBL  .equ 0xFCC1
ERROR: PatternBasedOptimizer: error initializing patterns! java.io.FileNotFoundException: data/\pbo-patterns-core.txt
INFO: PatternBasedOptimizer: 0 patterns applied, 0 bytes, 0 t-states saved.
INFO: Generating optimized file Debug\\objs\\game.asm.mdl.asm

By santiontanon

Paragon (1639)

santiontanon's picture

31-01-2021, 21:30

Thanks for the macros! Those I can add easily! There's code for this already in other dialects, so I just need to associate it with the SDCC dialect as well Smile

As for the redefinition warnings are ok, they are just warnings. The main error here is that it does not seem to find pbo-patterns-core.txt. Hmm, I remember TheNestruo pushing a few fixes to MDL to fix path issues under Windows (I develop on unix), and it seemed to work fine on his Windows setup. I need to get myself access to Windows to test this myself. Let me see if I can setup some VirtualBox machine or something (which I need anyway for something else, and I was procrastinating on it haha).

edit: I got a Windows VM running under VirtualBox, and indeed I get the same error. But now at least I can test it. I'll get it fixed asap! Smile

By santiontanon

Paragon (1639)

santiontanon's picture

31-01-2021, 23:02

Ok, I have good and bad news Smile

- the good news is that all of those macros are now supported, and that the Windows path issue was a small issue that was trivial to fix
- the bad news is that optimizing files in the SDCC dialect that contains macros will be problematic.

The problem is this: MDL's optimizer doesn't know anything about macros (it only knows about standard zilog notation). So, the first thing MDL does upoon loading an assembler file is to expand all macros, and translate all the fancy syntax other assemblers support to standard zilog code. Then the optimizer is run. This means that MDL might want to apply different optimizations to instantiations of the same macro, but in the original source code, the macro only appears once.

The only solution I can see is to instantiate all the macros in the generated optimized file (this is what happens when you use the "-asm " flag). However, MDL doesn't know how to WRITE to the different dialects, and the output file will be in standard zilog notation, which SDCC's assembler/linker will not like down the line. The "-poapply" flag was added to precisely address this problem, and it takes the original assembler file as is, and just replaces the few lines that MDL modified. Since MDL only modifies lines with assembler instructions, this is fine when there are no macros, and I don't need to worry about weird assembler dialect syntax (e.g. defining "sections", "pages", etc. that different assemblers require). However, that's not enough in the case of SDCC+macros...

So, long story short: supporting SDCC+macros requires MDL to know how to generate asdz80 assembler, which it can't fully do right now (it can parse it, but not generate it). I'm adding to do items to my to do list, but unfortunately that'll take a bit longer to address Sad

Sorry about that!

By Grauw

Ascended (10603)

Grauw's picture

31-01-2021, 23:51

Grauw wrote:
santiontanon wrote:

btw, any updates on your sequence optimization Grauw?

Not much news, it works and compression is decent, but I still need to wrap it up nicely, e.g. it doesn’t run in O(n) yet atm. Also I want to have replayer code for different sound chips. However a lot of work went into getting the compression implementation to work so I got a bit of fatigue, and the project’s on pause at the moment.

2016 LLVM Developers’ Meeting: J. Paquette "Reducing Code Size Using Outlining" (slides)

“Maintaining a low code size overhead is important in computing domains where memory is a scarce resource. Outlining is an optimization which identifies similar regions of code and replaces them with calls to a function. This talk introduces a novel (sic) method of compressing code using an interprocedural outliner on LLVM MIR.”

By santiontanon

Paragon (1639)

santiontanon's picture

01-02-2021, 01:36

Very interesting, thanks for sharing! definitively along the lines you were investigating! I like the way she formulated the problem, as something like that would not be too hard to implement! Added to the to-do list. Now I'm wondering which one do I want to try first, the "reorganizer" or the "outliner" Smile

By Grauw

Ascended (10603)

Grauw's picture

01-02-2021, 02:27

I think the reorganizer is easier to get started with, and lays down groundwork that would be useful for “outlining” (cf. inlining).

What LLVM does it splits code into basic blocks which start with a labeled entry point, and end at a terminator (jump or ret). There is no fall-through for conditionals, in that case a jump is introduced; a block can terminate with a conditional jump followed by an unconditional jump (forming a “select”). The basic blocks of code you will end up with form a control flow graph (with cycles), and the blocks can be freely moved around. The next step is to determine some optimal ordering, and eliminating unconditional jumps where possible, possibly flipping the condition of preceding conditional jumps.

I think inlining subroutines which are only used once (or based on a cost metric) is also an interesting transform that can be done in this realm.

And from that I think you can see that “outlining” also operates within these mechanisms.

By nihirash

Rookie (21)

nihirash's picture

01-02-2021, 07:35

Some folks using LD a, 1, b, 3, c, 10, (addr), hl or push hl, bc, de constructions in sjasmplus.

Will it support it?

Page 27/57
20 | 21 | 22 | 23 | 24 | 25 | 26 | | 28 | 29 | 30 | 31 | 32