1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-11-17 09:04:43 +00:00

Improve Common expression generation

Removed unnecessary parenthesis from Common-style expressions, which
are used by 64tass and ACME.
This commit is contained in:
Andy McFadden 2020-07-02 13:00:02 -07:00
parent 7d1d7f9c56
commit 4d06bb24eb
5 changed files with 81 additions and 71 deletions

View File

@ -705,8 +705,6 @@ namespace SourceGen {
// just use the byte-select operators, for wider ops we get only as fancy as we // just use the byte-select operators, for wider ops we get only as fancy as we
// need to be. // need to be.
int adjustment, symbolValue;
// Start by remapping the label, if necessary. The remapped label may have a // Start by remapping the label, if necessary. The remapped label may have a
// local-variable prefix character. // local-variable prefix character.
string symLabel = sym.Label; string symLabel = sym.Label;
@ -727,6 +725,7 @@ namespace SourceGen {
if (operandLen == 1) { if (operandLen == 1) {
// Use the byte-selection operator to get the right piece. In 64tass the // Use the byte-selection operator to get the right piece. In 64tass the
// selection operator has a very low precedence, similar to Merlin 32. // selection operator has a very low precedence, similar to Merlin 32.
int symbolValue;
string selOp; string selOp;
if (dfd.SymbolRef.ValuePart == WeakSymbolRef.Part.Bank) { if (dfd.SymbolRef.ValuePart == WeakSymbolRef.Part.Bank) {
symbolValue = (sym.Value >> 16) & 0xff; symbolValue = (sym.Value >> 16) & 0xff;
@ -757,16 +756,18 @@ namespace SourceGen {
sb.Append(symLabel); sb.Append(symLabel);
sb.Append(')'); sb.Append(')');
} else { } else {
// no adjustment required // no adjustment required, or no byte-selection
sb.Append(selOp); sb.Append(selOp);
sb.Append(symLabel); sb.Append(symLabel);
} }
int adjustment = operandValue - symbolValue;
sb.Append(formatter.FormatAdjustment(adjustment));
} else if (operandLen <= 4) { } else if (operandLen <= 4) {
// Operands and values should be 8/16/24 bit unsigned quantities. 32-bit // Operands and values should be 8/16/24 bit unsigned quantities. 32-bit
// support is really there so you can have a 24-bit pointer in a 32-bit hole. // support is really there so you can have a 24-bit pointer in a 32-bit hole.
// Might need to adjust this if 32-bit signed quantities become interesting. // Might need to adjust this if 32-bit signed quantities become interesting.
uint mask = 0xffffffff >> ((4 - operandLen) * 8); uint mask = 0xffffffff >> ((4 - operandLen) * 8);
int rightShift; int rightShift, symbolValue;
if (dfd.SymbolRef.ValuePart == WeakSymbolRef.Part.Bank) { if (dfd.SymbolRef.ValuePart == WeakSymbolRef.Part.Bank) {
symbolValue = (sym.Value >> 16); symbolValue = (sym.Value >> 16);
rightShift = 16; rightShift = 16;
@ -777,6 +778,7 @@ namespace SourceGen {
symbolValue = sym.Value; symbolValue = sym.Value;
rightShift = 0; rightShift = 0;
} }
bool hasShift = (rightShift != 0);
if ((flags & FormatNumericOpFlags.IsPcRel) != 0) { if ((flags & FormatNumericOpFlags.IsPcRel) != 0) {
// PC-relative operands are funny, because an 8- or 16-bit value is always // PC-relative operands are funny, because an 8- or 16-bit value is always
@ -809,45 +811,53 @@ namespace SourceGen {
} }
operandValue = (int)(operandValue & mask); operandValue = (int)(operandValue & mask);
int adjustment = operandValue - symbolValue;
// Generate one of: // Possibilities:
// label [+ adj] // label
// (label >> rightShift) [+ adj] // label + adj
// (label & mask) [+ adj] // label >> rightShift
// ((label >> rightShift) & mask) [+ adj] // (label >> rightShift) + adj
// label & mask
// (label & mask) + adj
// (label >> rightShift) & mask
// ((label >> rightShift) & mask) + adj
if (rightShift != 0 || needMask) {
if ((flags & FormatNumericOpFlags.HasHashPrefix) == 0) {
sb.Append("0+");
}
if (rightShift != 0 && needMask) {
sb.Append("((");
} else {
sb.Append("(");
}
}
sb.Append(symLabel); sb.Append(symLabel);
if (rightShift != 0) { if (rightShift != 0) {
sb.Append(" >> "); sb.Append(" >> ");
sb.Append(rightShift.ToString()); sb.Append(rightShift.ToString());
sb.Append(')');
} }
if (needMask) { if (needMask) {
if (rightShift != 0) {
sb.Insert(0, '(');
sb.Append(')');
}
sb.Append(" & "); sb.Append(" & ");
sb.Append(formatter.FormatHexValue((int)mask, 2)); sb.Append(formatter.FormatHexValue((int)mask, 2));
sb.Append(')'); }
if (adjustment != 0) {
if (needMask || rightShift != 0) {
sb.Insert(0, '(');
sb.Append(')');
}
sb.Append(formatter.FormatAdjustment(adjustment));
}
// Starting with a '(' makes it look like an indirect operand, so we need
// to prefix the expression with a no-op addition.
if (sb[0] == '(' && (flags & FormatNumericOpFlags.HasHashPrefix) == 0) {
sb.Insert(0, "0+");
} }
} else { } else {
Debug.Assert(false, "bad numeric len"); Debug.Assert(false, "bad numeric len");
sb.Append("?????"); sb.Append("?????");
symbolValue = 0;
} }
adjustment = operandValue - symbolValue;
sb.Append(formatter.FormatAdjustment(adjustment));
} }
/// <summary> /// <summary>
@ -862,7 +872,7 @@ namespace SourceGen {
// and divide.) This means that, if we want to mask off the low 16 bits and add one // and divide.) This means that, if we want to mask off the low 16 bits and add one
// to a label, we can write "start & $ffff + 1" rather than "(start & $ffff) + 1". // to a label, we can write "start & $ffff + 1" rather than "(start & $ffff) + 1".
// //
// This is particularly convenient for PEA, since "PEA (start & $ffff)" looks like // This is particularly convenient for PEA, since "PEA (start & $ffff) + 1" looks like
// we're trying to use a (non-existent) indirect form of PEA. We can write things // we're trying to use a (non-existent) indirect form of PEA. We can write things
// in a simpler way. // in a simpler way.

View File

@ -40,23 +40,23 @@ start lda #zip
.byte >start .byte >start
.word zip .word zip
.word absl .word absl
.word 0+(absl >> 8) .word absl >> 8
.word absl-$1000 .word absl-$1000
.word 0+(absl >> 8)-16 .word 0+(absl >> 8)-16
.word absh .word absh
.word 0+(absh >> 8) .word absh >> 8
.word absh-$f000 .word absh-$f000
.word 0+(absh >> 8)+16 .word 0+(absh >> 8)+16
.word start .word start
.word 0+(start >> 8) .word start >> 8
.word start+1 .word start+1
.word 0+(start >> 8) .word start >> 8
.byte $fe,$ed .byte $fe,$ed
.long zip .long zip
.long absh .long absh
.long 0+(absh >> 8) .long absh >> 8
.long start .long start
.long 0+(start >> 8) .long start >> 8
_L23A3 jmp _L1000_1 _L23A3 jmp _L1000_1

View File

@ -39,23 +39,23 @@ start lda #zip
!byte >start !byte >start
!word zip !word zip
!word absl !word absl
!word 0+(absl >> 8) !word absl >> 8
!word absl-$1000 !word absl-$1000
!word 0+(absl >> 8)-16 !word 0+(absl >> 8)-16
!word absh !word absh
!word 0+(absh >> 8) !word absh >> 8
!word absh-$f000 !word absh-$f000
!word 0+(absh >> 8)+16 !word 0+(absh >> 8)+16
!word start !word start
!word 0+(start >> 8) !word start >> 8
!word start+1 !word start+1
!word 0+(start >> 8) !word start >> 8
!byte $fe,$ed !byte $fe,$ed
!24 zip !24 zip
!24 absh !24 absh
!24 0+(absh >> 8) !24 absh >> 8
!24 start !24 start
!24 0+(start >> 8) !24 start >> 8
@L23A3 jmp @L1000_1 @L23A3 jmp @L1000_1

View File

@ -34,15 +34,15 @@ start clc
lda #>start lda #>start
lda #`start lda #`start
pea $feed pea $feed
pea 0+(start & $ffff) pea start & $ffff
pea $0001 pea $0001
pea $3456 pea $3456
pea $0012 pea $0012
pea absh pea absh
pea 0+(start & $ffff) pea start & $ffff
pea 0+(start >> 16) pea start >> 16
pea 0+(biggie & $ffff) pea biggie & $ffff
pea 0+(biggie >> 16) pea biggie >> 16
lda zip+1 lda zip+1
lda @wzip+1 lda @wzip+1
lda @lzip+1 lda @lzip+1
@ -65,21 +65,21 @@ start clc
lda #zip+16 lda #zip+16
lda #zip+64 lda #zip+64
lda #absl lda #absl
lda #(absl >> 8) lda #absl >> 8
lda #absl-$1000 lda #absl-$1000
lda #(absl >> 8)-16 lda #(absl >> 8)-16
lda #(absl >> 16) lda #absl >> 16
lda #absh lda #absh
lda #(absh >> 8) lda #absh >> 8
lda #absh-$f000 lda #absh-$f000
lda #(absh >> 8)+16 lda #(absh >> 8)+16
lda #(absh >> 16)+1 lda #(absh >> 16)+1
lda #(start & $ffff) lda #start & $ffff
lda #(start >> 8) lda #start >> 8
lda #(start >> 16) lda #start >> 16
lda #(biggie & $ffff) lda #biggie & $ffff
lda #(biggie >> 8) lda #biggie >> 8
lda #(biggie >> 16) lda #biggie >> 16
bra _skipdata bra _skipdata
.byte zip .byte zip
@ -90,32 +90,32 @@ start clc
.byte `start .byte `start
.word zip .word zip
.word absl .word absl
.word 0+(absl >> 8) .word absl >> 8
.word absl-$1000 .word absl-$1000
.word 0+(absl >> 8)-16 .word 0+(absl >> 8)-16
.word absh .word absh
.word 0+(absh >> 8) .word absh >> 8
.word absh-$f000 .word absh-$f000
.word 0+(absh >> 8)+16 .word 0+(absh >> 8)+16
.word 0+(start & $ffff) .word start & $ffff
.word 0+(start >> 8) .word start >> 8
.word 0+(start >> 16) .word start >> 16
.word 0+(start & $ffff)+1 .word 0+(start & $ffff)+1
.word 0+(start >> 8) .word start >> 8
.word 0+(start >> 16) .word start >> 16
.byte $fe,$ed .byte $fe,$ed
.long zip .long zip
.long absh .long absh
.long 0+(absh >> 8) .long absh >> 8
.long start .long start
.long 0+(start >> 8) .long start >> 8
.long 0+(start >> 16) .long start >> 16
.dword zip .dword zip
.dword absh .dword absh
.dword 0+(absh >> 8) .dword absh >> 8
.dword start-1 .dword start-1
.dword 0+(start >> 8) .dword start >> 8
.dword 0+(start >> 16) .dword start >> 16
_skipdata lda #(biggie >> 16)-1 _skipdata lda #(biggie >> 16)-1
mvn #`biggie,#(`biggie)-17 mvn #`biggie,#(`biggie)-17
@ -207,7 +207,7 @@ _L118E lda #<thirty2+2
.xl .xl
lda #(thirty2 & $ffff)+3 lda #(thirty2 & $ffff)+3
lda #((thirty2 >> 8) & $ffff)+4 lda #((thirty2 >> 8) & $ffff)+4
lda #(thirty2 >> 16) lda #thirty2 >> 16
rts rts
.here .here

View File

@ -17,14 +17,14 @@ lodat .byte $00
.logical $440000 .logical $440000
L440000 cmp L440000 L440000 cmp L440000
L440004 lda L440000 L440004 lda L440000
lda @w0+(L440000 & $ffff) lda @wL440000 & $ffff
lda zero lda zero
bmi L440004 bmi L440004
per high44 per high44
bne high44 bne high44
brl L44FFC0 brl L44FFC0
dat44 .word 0+(dat44 & $ffff) dat44 .word dat44 & $ffff
.long dat44 .long dat44
.here .here
@ -39,8 +39,8 @@ _L44FFCB jml _L2000
.here .here
.logical $2000 .logical $2000
_L2000 bit _L2000 _L2000 bit _L2000
pea 0+(dat44 & $ffff) pea dat44 & $ffff
pea 0+(dat44 >> 16) pea dat44 >> 16
bne skip bne skip
jml [lodat] jml [lodat]