1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-11 04:29:53 +00:00

add type cast in simple assignment of numbers; allow integer constants to be finalized to a fixed value; bugfix memcpy

This commit is contained in:
Travis Fisher 2020-12-04 23:38:14 -05:00
parent 28729cbd45
commit 4aec255c95
8 changed files with 10660 additions and 10645 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,114 +1,114 @@
//KICKC FRAGMENT CACHE f8d7c2682 f8d7c43d0 //KICKC FRAGMENT CACHE f8d7c0934 f8d7c2682
//FRAGMENT vbuz1=vbuc1 //FRAGMENT vbuz1=vbuc1
lda #{c1} lda #{c1}
sta {z1} sta {z1}
//FRAGMENT vbuz1_lt_vbuc1_then_la1 //FRAGMENT vbuz1_lt_vbuc1_then_la1
lda {z1} lda {z1}
cmp #{c1} cmp #{c1}
bcc {la1} bcc {la1}
//FRAGMENT pbuc1_derefidx_vbuz1=vbuc2 //FRAGMENT pbuc1_derefidx_vbuz1=vbuc2
lda #{c2} lda #{c2}
ldy {z1} ldy {z1}
sta {c1},y sta {c1},y
//FRAGMENT vbuz1=vbuz2_band_vbuc1 //FRAGMENT vbuz1=vbuz2_band_vbuc1
lda #{c1} lda #{c1}
and {z2} and {z2}
sta {z1} sta {z1}
//FRAGMENT vbuz1_neq_0_then_la1 //FRAGMENT vbuz1_neq_0_then_la1
lda {z1} lda {z1}
cmp #0 cmp #0
bne {la1} bne {la1}
//FRAGMENT vbuz1=vbuz1_plus_vbuc1 //FRAGMENT vbuz1=vbuz1_plus_vbuc1
lda #{c1} lda #{c1}
clc clc
adc {z1} adc {z1}
sta {z1} sta {z1}
//FRAGMENT vbuz1=_inc_vbuz1 //FRAGMENT vbuz1=_inc_vbuz1
inc {z1} inc {z1}
//FRAGMENT vbuaa_lt_vbuc1_then_la1 //FRAGMENT vbuaa_lt_vbuc1_then_la1
cmp #{c1} cmp #{c1}
bcc {la1} bcc {la1}
//FRAGMENT pbuc1_derefidx_vbuaa=vbuc2 //FRAGMENT pbuc1_derefidx_vbuaa=vbuc2
tay tay
lda #{c2} lda #{c2}
sta {c1},y sta {c1},y
//FRAGMENT pbuc1_derefidx_vbuxx=vbuc2 //FRAGMENT pbuc1_derefidx_vbuxx=vbuc2
lda #{c2} lda #{c2}
sta {c1},x sta {c1},x
//FRAGMENT pbuc1_derefidx_vbuyy=vbuc2 //FRAGMENT pbuc1_derefidx_vbuyy=vbuc2
lda #{c2} lda #{c2}
sta {c1},y sta {c1},y
//FRAGMENT vbuaa=vbuz1_band_vbuc1 //FRAGMENT vbuaa=vbuz1_band_vbuc1
lda #{c1}
and {z1}
//FRAGMENT vbuxx=vbuz1_band_vbuc1
lda #{c1} lda #{c1}
and {z1} and {z1}
//FRAGMENT vbuxx=vbuz1_band_vbuc1 tax
//FRAGMENT vbuyy=vbuz1_band_vbuc1
lda #{c1} lda #{c1}
and {z1} and {z1}
tax tay
//FRAGMENT vbuyy=vbuz1_band_vbuc1 //FRAGMENT vbuz1=vbuxx_band_vbuc1
lda #{c1}
and {z1}
tay
//FRAGMENT vbuz1=vbuxx_band_vbuc1
txa txa
and #{c1} and #{c1}
sta {z1} sta {z1}
//FRAGMENT vbuaa=vbuxx_band_vbuc1 //FRAGMENT vbuaa=vbuxx_band_vbuc1
txa
and #{c1}
//FRAGMENT vbuxx=vbuxx_band_vbuc1
txa txa
and #{c1} and #{c1}
//FRAGMENT vbuxx=vbuxx_band_vbuc1 tax
//FRAGMENT vbuyy=vbuxx_band_vbuc1
txa txa
and #{c1} and #{c1}
tax tay
//FRAGMENT vbuyy=vbuxx_band_vbuc1 //FRAGMENT vbuz1=vbuyy_band_vbuc1
txa
and #{c1}
tay
//FRAGMENT vbuz1=vbuyy_band_vbuc1
tya tya
and #{c1} and #{c1}
sta {z1} sta {z1}
//FRAGMENT vbuaa=vbuyy_band_vbuc1 //FRAGMENT vbuaa=vbuyy_band_vbuc1
tya
and #{c1}
//FRAGMENT vbuxx=vbuyy_band_vbuc1
tya tya
and #{c1} and #{c1}
//FRAGMENT vbuxx=vbuyy_band_vbuc1 tax
//FRAGMENT vbuyy=vbuyy_band_vbuc1
tya tya
and #{c1} and #{c1}
tax tay
//FRAGMENT vbuyy=vbuyy_band_vbuc1 //FRAGMENT vbuaa_neq_0_then_la1
tya
and #{c1}
tay
//FRAGMENT vbuaa_neq_0_then_la1
cmp #0 cmp #0
bne {la1} bne {la1}
//FRAGMENT vbuxx=vbuxx_plus_vbuc1 //FRAGMENT vbuxx=vbuxx_plus_vbuc1
txa txa
clc clc
adc #{c1} adc #{c1}
tax tax
//FRAGMENT vbuyy=vbuyy_plus_vbuc1 //FRAGMENT vbuyy=vbuyy_plus_vbuc1
tya tya
clc clc
adc #{c1} adc #{c1}
tay tay
//FRAGMENT vbuxx_lt_vbuc1_then_la1 //FRAGMENT vbuxx_lt_vbuc1_then_la1
cpx #{c1} cpx #{c1}
bcc {la1} bcc {la1}
//FRAGMENT vbuxx=vbuc1 //FRAGMENT vbuxx=vbuc1
ldx #{c1} ldx #{c1}
//FRAGMENT vbuxx=_inc_vbuxx //FRAGMENT vbuxx=_inc_vbuxx
inx inx
//FRAGMENT vbuyy=vbuc1 //FRAGMENT vbuyy=vbuc1
ldy #{c1} ldy #{c1}
//FRAGMENT vbuyy_lt_vbuc1_then_la1 //FRAGMENT vbuyy_lt_vbuc1_then_la1
cpy #{c1} cpy #{c1}
bcc {la1} bcc {la1}
//FRAGMENT vbuyy=_inc_vbuyy //FRAGMENT vbuyy=_inc_vbuyy
iny iny
//FRAGMENT vbuxx_neq_0_then_la1 //FRAGMENT vbuxx_neq_0_then_la1
cpx #0 cpx #0
bne {la1} bne {la1}
//FRAGMENT vbuyy_neq_0_then_la1 //FRAGMENT vbuyy_neq_0_then_la1
cpy #0 cpy #0
bne {la1} bne {la1}

File diff suppressed because it is too large Load Diff

View File

@ -1,67 +1,67 @@
//KICKC FRAGMENT CACHE f8d7c2682 f8d7c43d0 //KICKC FRAGMENT CACHE f8d7c0934 f8d7c2682
//FRAGMENT vbuz1=_deref_pbuc1 //FRAGMENT vbuz1=_deref_pbuc1
lda {c1} lda {c1}
sta {z1} sta {z1}
//FRAGMENT vbuz1=vbuz2_plus_1 //FRAGMENT vbuz1=vbuz2_plus_1
lda {z2} lda {z2}
inc inc
sta {z1} sta {z1}
//FRAGMENT _deref_pbuc1=vbuz1 //FRAGMENT _deref_pbuc1=vbuz1
lda {z1} lda {z1}
sta {c1} sta {c1}
//FRAGMENT vbuaa=_deref_pbuc1 //FRAGMENT vbuaa=_deref_pbuc1
lda {c1} lda {c1}
//FRAGMENT vbuxx=_deref_pbuc1 //FRAGMENT vbuxx=_deref_pbuc1
ldx {c1} ldx {c1}
//FRAGMENT vbuz1=vbuaa_plus_1 //FRAGMENT vbuz1=vbuaa_plus_1
inc inc
sta {z1} sta {z1}
//FRAGMENT vbuz1=vbuxx_plus_1 //FRAGMENT vbuz1=vbuxx_plus_1
inx inx
stx {z1} stx {z1}
//FRAGMENT _deref_pbuc1=vbuaa //FRAGMENT _deref_pbuc1=vbuaa
sta {c1} sta {c1}
//FRAGMENT vbuyy=_deref_pbuc1 //FRAGMENT vbuyy=_deref_pbuc1
ldy {c1} ldy {c1}
//FRAGMENT vbuz1=vbuyy_plus_1 //FRAGMENT vbuz1=vbuyy_plus_1
iny iny
sty {z1} sty {z1}
//FRAGMENT vbuaa=vbuz1_plus_1 //FRAGMENT vbuaa=vbuz1_plus_1
lda {z1} lda {z1}
inc inc
//FRAGMENT vbuaa=vbuaa_plus_1 //FRAGMENT vbuaa=vbuaa_plus_1
inc inc
//FRAGMENT vbuaa=vbuxx_plus_1 //FRAGMENT vbuaa=vbuxx_plus_1
txa txa
inc inc
//FRAGMENT vbuaa=vbuyy_plus_1 //FRAGMENT vbuaa=vbuyy_plus_1
tya tya
inc inc
//FRAGMENT vbuxx=vbuz1_plus_1 //FRAGMENT vbuxx=vbuz1_plus_1
ldx {z1} ldx {z1}
inx inx
//FRAGMENT _deref_pbuc1=vbuxx //FRAGMENT _deref_pbuc1=vbuxx
stx {c1} stx {c1}
//FRAGMENT vbuxx=vbuaa_plus_1 //FRAGMENT vbuxx=vbuaa_plus_1
tax tax
inx inx
//FRAGMENT vbuxx=vbuxx_plus_1 //FRAGMENT vbuxx=vbuxx_plus_1
inx inx
//FRAGMENT vbuxx=vbuyy_plus_1 //FRAGMENT vbuxx=vbuyy_plus_1
tya tya
inc inc
tax tax
//FRAGMENT vbuyy=vbuz1_plus_1 //FRAGMENT vbuyy=vbuz1_plus_1
ldy {z1} ldy {z1}
iny iny
//FRAGMENT _deref_pbuc1=vbuyy //FRAGMENT _deref_pbuc1=vbuyy
sty {c1} sty {c1}
//FRAGMENT vbuyy=vbuaa_plus_1 //FRAGMENT vbuyy=vbuaa_plus_1
tay tay
iny iny
//FRAGMENT vbuyy=vbuxx_plus_1 //FRAGMENT vbuyy=vbuxx_plus_1
txa txa
inc inc
tay tay
//FRAGMENT vbuyy=vbuyy_plus_1 //FRAGMENT vbuyy=vbuyy_plus_1
iny iny

View File

@ -851,7 +851,7 @@ public interface ProgramValue {
@Override @Override
public void set(Value val) { public void set(Value val) {
memsetValue.setSize((ConstantRef) val); memsetValue.setSize((ConstantValue) val);
} }
} }
@ -872,7 +872,7 @@ public interface ProgramValue {
@Override @Override
public void set(Value val) { public void set(Value val) {
memcpyValue.setSize((ConstantRef) val); memcpyValue.setSize((ConstantValue) val);
} }
} }

View File

@ -6,6 +6,7 @@ import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.types.SymbolTypeConversion; import dk.camelot64.kickc.model.types.SymbolTypeConversion;
import dk.camelot64.kickc.model.types.SymbolTypeInference; import dk.camelot64.kickc.model.types.SymbolTypeInference;
import dk.camelot64.kickc.model.types.SymbolTypeIntegerFixed;
import dk.camelot64.kickc.model.values.RValue; import dk.camelot64.kickc.model.values.RValue;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -27,21 +28,35 @@ public class PassNAddNumberTypeConversions extends Pass2SsaOptimization {
ProgramExpressionBinary binary = (ProgramExpressionBinary) binaryExpression; ProgramExpressionBinary binary = (ProgramExpressionBinary) binaryExpression;
RValue left = binary.getLeft(); RValue left = binary.getLeft();
RValue right = binary.getRight(); RValue right = binary.getRight();
SymbolType castType = SymbolTypeConversion.getNumberCastType(left, right, getScope(), currentStmt);
if(castType != null) { // for simple assignments the right-hand-side is converted to the type of the left-hand side (6.5.16.1.2)
// Convert both left and right to the found type if(binary instanceof ProgramExpressionBinary.ProgramExpressionBinaryAssignmentLValue ||
binary instanceof ProgramExpressionBinary.ProgramExpressionBinaryAssignmentRValue)
{
SymbolType leftType = SymbolTypeInference.inferType(getProgram().getScope(), left); SymbolType leftType = SymbolTypeInference.inferType(getProgram().getScope(), left);
if(SymbolType.NUMBER.equals(leftType)) { SymbolType rightType = SymbolTypeInference.inferType(getProgram().getScope(), right);
getLog().append("Adding number conversion cast (" + castType + ") " + binary.getLeft().toString() + " in " + (currentStmt==null?"":currentStmt.toString(getProgram(), false))); if (leftType instanceof SymbolTypeIntegerFixed && SymbolType.NUMBER.equals(rightType))
binary.addLeftCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getScope()); {
getLog().append("Adding cast to assignment (" + leftType + ") " + binary.getRight().toString() + " in " + ((currentStmt == null) ? "" : currentStmt.toString(getProgram(), false)));
binary.addRightCast(leftType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
modified.set(true); modified.set(true);
} }
SymbolType rightType = SymbolTypeInference.inferType(getProgram().getScope(), right); } else {
if(SymbolType.NUMBER.equals(rightType)) { SymbolType castType = SymbolTypeConversion.getNumberCastType(left, right, getScope(), currentStmt);
getLog().append("Adding number conversion cast (" + castType + ") " + binary.getRight().toString() + " in " + ((currentStmt==null)?"":currentStmt.toString(getProgram(), false))); if(castType != null) {
binary.addRightCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getScope()); // Convert both left and right to the found type
SymbolType leftType = SymbolTypeInference.inferType(getProgram().getScope(), left);
modified.set(true); if(SymbolType.NUMBER.equals(leftType)) {
getLog().append("Adding number conversion cast (" + castType + ") " + binary.getLeft().toString() + " in " + (currentStmt==null?"":currentStmt.toString(getProgram(), false)));
binary.addLeftCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getScope());
modified.set(true);
}
SymbolType rightType = SymbolTypeInference.inferType(getProgram().getScope(), right);
if(SymbolType.NUMBER.equals(rightType)) {
getLog().append("Adding number conversion cast (" + castType + ") " + binary.getRight().toString() + " in " + ((currentStmt==null)?"":currentStmt.toString(getProgram(), false)));
binary.addRightCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getScope());
modified.set(true);
}
} }
} }
} }

View File

@ -28,12 +28,12 @@ public class PassNFinalizeNumberTypeConversions extends Pass2SsaOptimization {
ProgramValueIterator.execute(getProgram(), (programValue, currentStmt, stmtIt, currentBlock) -> { ProgramValueIterator.execute(getProgram(), (programValue, currentStmt, stmtIt, currentBlock) -> {
if(programValue.get() instanceof ConstantInteger) { if(programValue.get() instanceof ConstantInteger) {
ConstantInteger constantInteger = (ConstantInteger) programValue.get(); ConstantInteger constantInteger = (ConstantInteger) programValue.get();
if(SymbolType.UNUMBER.equals(constantInteger.getType())) { if(SymbolType.UNUMBER.equals(constantInteger.getType()) || (SymbolType.NUMBER.equals(constantInteger.getType()) && constantInteger.getValue()>=0)) {
SymbolType integerType = SymbolTypeConversion.getSmallestUnsignedFixedIntegerType(constantInteger, getScope()); SymbolType integerType = SymbolTypeConversion.getSmallestUnsignedFixedIntegerType(constantInteger, getScope());
programValue.set(new ConstantInteger(constantInteger.getValue(), integerType)); programValue.set(new ConstantInteger(constantInteger.getValue(), integerType));
getLog().append("Finalized unsigned number type "+programValue.get().toString(getProgram())); getLog().append("Finalized unsigned number type "+programValue.get().toString(getProgram()));
modified.set(true); modified.set(true);
} else if(SymbolType.SNUMBER.equals(constantInteger.getType())) { } else if(SymbolType.SNUMBER.equals(constantInteger.getType()) || (SymbolType.NUMBER.equals(constantInteger.getType()) && constantInteger.getValue()<0)) {
SymbolType integerType = SymbolTypeConversion.getSmallestSignedFixedIntegerType(constantInteger, getScope()); SymbolType integerType = SymbolTypeConversion.getSmallestSignedFixedIntegerType(constantInteger, getScope());
programValue.set(new ConstantInteger(constantInteger.getValue(), integerType)); programValue.set(new ConstantInteger(constantInteger.getValue(), integerType));
getLog().append("Finalized signed number type "+programValue.get().toString(getProgram())); getLog().append("Finalized signed number type "+programValue.get().toString(getProgram()));