mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-04-08 14:37:40 +00:00
Eliminated PassNDowngradeConstantTypeConversion. Simplified PassNAddTypeConversionAssignment.
This commit is contained in:
parent
2a14671496
commit
009b141ef1
@ -233,7 +233,6 @@ public class Compiler {
|
||||
List<Pass2SsaOptimization> optimizations = new ArrayList<>();
|
||||
optimizations.add(new PassNAddNumberTypeConversions(program));
|
||||
optimizations.add(new PassNAddArrayNumberTypeConversions(program));
|
||||
optimizations.add(new PassNDowngradeConstantTypeConversions(program));
|
||||
optimizations.add(new PassNTypeInference(program));
|
||||
optimizations.add(new PassNTypeIdSimplification(program));
|
||||
optimizations.add(new Pass2SizeOfSimplification(program));
|
||||
|
@ -908,8 +908,8 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
||||
RValue rangeLastValue = (RValue) visit(rangeLastCtx);
|
||||
RValue rangeFirstValue = (RValue) visit(rangeFirstCtx);
|
||||
if(varType!=null) {
|
||||
if(rangeFirstValue instanceof ConstantInteger) ((ConstantInteger) rangeFirstValue).setType(varType);
|
||||
if(rangeLastValue instanceof ConstantInteger) ((ConstantInteger) rangeLastValue).setType(varType);
|
||||
if(rangeFirstValue instanceof ConstantInteger) ((ConstantInteger) rangeFirstValue).setType(SymbolType.NUMBER);
|
||||
if(rangeLastValue instanceof ConstantInteger) ((ConstantInteger) rangeLastValue).setType(SymbolType.NUMBER);
|
||||
}
|
||||
Statement stmtInit = new StatementAssignment(lValue.getRef(), rangeFirstValue, new StatementSource(ctx), Comment.NO_COMMENTS);
|
||||
sequence.addStatement(stmtInit);
|
||||
|
@ -63,15 +63,6 @@ public class PassNAddTypeConversionAssignment extends Pass2SsaOptimization {
|
||||
}
|
||||
|
||||
}
|
||||
if(leftType instanceof SymbolTypeIntegerFixed && SymbolType.isInteger(rightType)) {
|
||||
SymbolType conversionType = SymbolTypeConversion.convertedMathType((SymbolTypeInteger) leftType, (SymbolTypeInteger) rightType);
|
||||
// Add cast to the right Type if needed
|
||||
if(leftType.equals(conversionType) && !rightType.equals(conversionType)) {
|
||||
getLog().append("Adding type conversion cast (" + conversionType + ") " + binary.getRight().toString() + " in " + (currentStmt == null ? "" : currentStmt.toString(getProgram(), false)));
|
||||
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
|
||||
modified.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -1,90 +0,0 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.iterator.ProgramExpressionBinary;
|
||||
import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
|
||||
import dk.camelot64.kickc.model.types.SymbolType;
|
||||
import dk.camelot64.kickc.model.types.SymbolTypeInference;
|
||||
import dk.camelot64.kickc.model.types.SymbolTypePointer;
|
||||
import dk.camelot64.kickc.model.values.*;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Downgrade any number expression cast that are part of a WORD+NUMBER expression to BYTE if the number is small enough to fit in the byte.
|
||||
*/
|
||||
public class PassNDowngradeConstantTypeConversions extends Pass2SsaOptimization {
|
||||
|
||||
public PassNDowngradeConstantTypeConversions(Program program) {
|
||||
super(program);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean step() {
|
||||
AtomicBoolean modified = new AtomicBoolean(false);
|
||||
ProgramExpressionIterator.execute(getProgram(), (binaryExpression, currentStmt, stmtIt, currentBlock) -> {
|
||||
if(binaryExpression instanceof ProgramExpressionBinary) {
|
||||
ProgramExpressionBinary binary = (ProgramExpressionBinary) binaryExpression;
|
||||
RValue left = binary.getLeft();
|
||||
RValue right = binary.getRight();
|
||||
if(isConstantWord(left) && isWordLike(right) && isByteLike((ConstantValue) left)) {
|
||||
getLog().append("Downgrading number conversion cast to (byte) " + binary.getLeft().toString() + " in " + (currentStmt == null ? "" : currentStmt.toString(getProgram(), false)));
|
||||
binary.addLeftCast(SymbolType.BYTE, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
|
||||
modified.set(true);
|
||||
} else if(isConstantWord(right) && isWordLike(left) && isByteLike((ConstantValue) right)) {
|
||||
getLog().append("Downgrading number conversion cast to (byte) " + binary.getRight().toString() + " in " + (currentStmt == null ? "" : currentStmt.toString(getProgram(), false)));
|
||||
binary.addRightCast(SymbolType.BYTE, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
|
||||
modified.set(true);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
return modified.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a constant value is a byte-like value (can be represented inside a BYTE)
|
||||
*
|
||||
* @param constantValue The value to examine
|
||||
* @return true if the value is a byte-like value
|
||||
*/
|
||||
public boolean isByteLike(ConstantValue constantValue) {
|
||||
ConstantLiteral constantLiteral = (constantValue).calculateLiteral(getScope());
|
||||
if(constantLiteral instanceof ConstantInteger) {
|
||||
ConstantInteger constantInteger = (ConstantInteger) constantLiteral;
|
||||
Long value = constantInteger.getValue();
|
||||
if(SymbolType.BYTE.contains(value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a value is a word-like value (WORD or Pointer)
|
||||
*
|
||||
* @param rValue The value to examine
|
||||
* @return true if the value is a word-like value
|
||||
*/
|
||||
public boolean isWordLike(RValue rValue) {
|
||||
SymbolType symbolType = SymbolTypeInference.inferType(getProgram().getScope(), rValue);
|
||||
return SymbolType.WORD.equals(symbolType) || symbolType instanceof SymbolTypePointer;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines if the passed value is a constant cast to WORD
|
||||
*
|
||||
* @param rValue The value to examine
|
||||
* @return true if the value is a constant cast to word
|
||||
*/
|
||||
public boolean isConstantWord(RValue rValue) {
|
||||
if(rValue instanceof ConstantInteger && SymbolType.WORD.equals(((ConstantInteger) rValue).getType())) {
|
||||
return true;
|
||||
}
|
||||
if((rValue instanceof ConstantCastValue) && SymbolType.WORD.equals(((ConstantCastValue) rValue).getToType()))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -335,13 +335,14 @@ lin16u_gen: {
|
||||
sta step
|
||||
lda stepf+1
|
||||
sta step+1
|
||||
lda #<0
|
||||
sta val
|
||||
sta val+1
|
||||
lda min
|
||||
sta val+2
|
||||
lda min+1
|
||||
sta val+3
|
||||
lda #0
|
||||
sta val
|
||||
sta val+1
|
||||
sta i
|
||||
sta i+1
|
||||
b1:
|
||||
|
@ -2,25 +2,22 @@
|
||||
.pc = $801 "Basic"
|
||||
:BasicUpstart(main)
|
||||
.pc = $80d "Program"
|
||||
.label print_line_cursor = 3
|
||||
.label print_line_cursor = 2
|
||||
.label print_char_cursor = 7
|
||||
main: {
|
||||
.label i = 2
|
||||
lda #0
|
||||
sta i
|
||||
ldy #0
|
||||
b1:
|
||||
lda i
|
||||
tya
|
||||
ldx #$80
|
||||
jsr sub
|
||||
lda i
|
||||
tya
|
||||
ldx #$40
|
||||
jsr sub
|
||||
lda i
|
||||
tya
|
||||
ldx #$40
|
||||
jsr sub
|
||||
inc i
|
||||
lda #9
|
||||
cmp i
|
||||
iny
|
||||
cpy #9
|
||||
bne b1
|
||||
jsr print_cls
|
||||
lda #<$400
|
||||
@ -74,9 +71,9 @@ print_ln: {
|
||||
rts
|
||||
}
|
||||
// Print a signed word as HEX
|
||||
// print_sword(signed word zeropage(5) w)
|
||||
// print_sword(signed word zeropage(4) w)
|
||||
print_sword: {
|
||||
.label w = 5
|
||||
.label w = 4
|
||||
lda w+1
|
||||
bpl b1
|
||||
lda #'-'
|
||||
@ -95,9 +92,9 @@ print_sword: {
|
||||
rts
|
||||
}
|
||||
// Print a word as HEX
|
||||
// print_word(word zeropage(5) w)
|
||||
// print_word(word zeropage(4) w)
|
||||
print_word: {
|
||||
.label w = 5
|
||||
.label w = 4
|
||||
lda w+1
|
||||
sta print_byte.b
|
||||
jsr print_byte
|
||||
@ -107,9 +104,9 @@ print_word: {
|
||||
rts
|
||||
}
|
||||
// Print a byte as HEX
|
||||
// print_byte(byte zeropage(2) b)
|
||||
// print_byte(byte zeropage(6) b)
|
||||
print_byte: {
|
||||
.label b = 2
|
||||
.label b = 6
|
||||
lda b
|
||||
lsr
|
||||
lsr
|
||||
@ -138,7 +135,7 @@ print_char: {
|
||||
}
|
||||
// Clear the screen. Also resets current line/char cursor.
|
||||
print_cls: {
|
||||
.label sc = 3
|
||||
.label sc = 2
|
||||
lda #<$400
|
||||
sta sc
|
||||
lda #>$400
|
||||
@ -161,20 +158,16 @@ print_cls: {
|
||||
}
|
||||
// sub(byte register(A) idx, byte register(X) s)
|
||||
sub: {
|
||||
.label _1 = 3
|
||||
asl
|
||||
tay
|
||||
txa
|
||||
sta _1
|
||||
lda #0
|
||||
sta _1+1
|
||||
lda words,y
|
||||
sec
|
||||
sbc _1
|
||||
sta words,y
|
||||
lda words+1,y
|
||||
sbc _1+1
|
||||
sta words+1,y
|
||||
stx $ff
|
||||
tax
|
||||
lda words,x
|
||||
sbc $ff
|
||||
sta words,x
|
||||
bcs !+
|
||||
dec words+1,x
|
||||
!:
|
||||
rts
|
||||
}
|
||||
print_hextab: .text "0123456789abcdef"
|
||||
|
Loading…
x
Reference in New Issue
Block a user