mirror of
https://github.com/irmen/prog8.git
synced 2025-02-09 07:31:34 +00:00
improve function call arg type casting
This commit is contained in:
parent
cc81dd7d3e
commit
34aa21f7d9
@ -407,7 +407,7 @@ romsub $febd = kbdbuf_peek2() -> uword @AX ; alternative to above to
|
|||||||
romsub $fec0 = kbdbuf_get_modifiers() -> ubyte @A
|
romsub $fec0 = kbdbuf_get_modifiers() -> ubyte @A
|
||||||
romsub $fec3 = kbdbuf_put(ubyte key @A) clobbers(X)
|
romsub $fec3 = kbdbuf_put(ubyte key @A) clobbers(X)
|
||||||
romsub $fed2 = keymap(uword identifier @XY, bool read @Pc) -> bool @Pc
|
romsub $fed2 = keymap(uword identifier @XY, bool read @Pc) -> bool @Pc
|
||||||
romsub $ff68 = mouse_config(ubyte shape @A, ubyte resX @X, ubyte resY @Y) clobbers (A, X, Y)
|
romsub $ff68 = mouse_config(byte shape @A, ubyte resX @X, ubyte resY @Y) clobbers (A, X, Y)
|
||||||
romsub $ff6b = mouse_get(ubyte zpdataptr @X) -> ubyte @A
|
romsub $ff6b = mouse_get(ubyte zpdataptr @X) -> ubyte @A
|
||||||
romsub $ff71 = mouse_scan() clobbers(A, X, Y)
|
romsub $ff71 = mouse_scan() clobbers(A, X, Y)
|
||||||
romsub $ff53 = joystick_scan() clobbers(A, X, Y)
|
romsub $ff53 = joystick_scan() clobbers(A, X, Y)
|
||||||
@ -450,7 +450,7 @@ asmsub kbdbuf_clear() {
|
|||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
asmsub mouse_config2(ubyte shape @A) clobbers (A, X, Y) {
|
asmsub mouse_config2(byte shape @A) clobbers (A, X, Y) {
|
||||||
; -- convenience wrapper function that handles the screen resolution for mouse_config() for you
|
; -- convenience wrapper function that handles the screen resolution for mouse_config() for you
|
||||||
%asm {{
|
%asm {{
|
||||||
pha ; save shape
|
pha ; save shape
|
||||||
|
@ -9,6 +9,7 @@ import prog8.ast.statements.*
|
|||||||
import prog8.ast.walk.AstWalker
|
import prog8.ast.walk.AstWalker
|
||||||
import prog8.ast.walk.IAstModification
|
import prog8.ast.walk.IAstModification
|
||||||
import prog8.code.core.*
|
import prog8.code.core.*
|
||||||
|
import kotlin.math.sign
|
||||||
|
|
||||||
|
|
||||||
class TypecastsAdder(val program: Program, val options: CompilationOptions, val errors: IErrorReporter) : AstWalker() {
|
class TypecastsAdder(val program: Program, val options: CompilationOptions, val errors: IErrorReporter) : AstWalker() {
|
||||||
@ -245,96 +246,54 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
|||||||
private fun afterFunctionCallArgs(call: IFunctionCall): Iterable<IAstModification> {
|
private fun afterFunctionCallArgs(call: IFunctionCall): Iterable<IAstModification> {
|
||||||
// see if a typecast is needed to convert the arguments into the required parameter's type
|
// see if a typecast is needed to convert the arguments into the required parameter's type
|
||||||
val modifications = mutableListOf<IAstModification>()
|
val modifications = mutableListOf<IAstModification>()
|
||||||
|
val sub = call.target.targetStatement(program)
|
||||||
|
val params = when(sub) {
|
||||||
|
is BuiltinFunctionPlaceholder -> BuiltinFunctions.getValue(sub.name).parameters
|
||||||
|
is Subroutine -> sub.parameters.map { FParam(it.name, listOf(it.type).toTypedArray()) }
|
||||||
|
else -> emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
when(val sub = call.target.targetStatement(program)) {
|
params.zip(call.args).forEach {
|
||||||
is Subroutine -> {
|
val targetDt = it.first.possibleDatatypes.first()
|
||||||
sub.parameters.zip(call.args).forEach { (param, arg) ->
|
val argIdt = it.second.inferType(program)
|
||||||
val argItype = arg.inferType(program)
|
if (argIdt.isKnown) {
|
||||||
if(argItype.isKnown) {
|
val argDt = argIdt.getOr(DataType.UNDEFINED)
|
||||||
val argtype = argItype.getOr(DataType.UNDEFINED)
|
if (argDt !in it.first.possibleDatatypes) {
|
||||||
val requiredType = param.type
|
val identifier = it.second as? IdentifierReference
|
||||||
if (requiredType != argtype) {
|
val number = it.second as? NumericLiteral
|
||||||
if (argtype isAssignableTo requiredType) {
|
if(number!=null) {
|
||||||
// don't need a cast for pass-by-reference types that are assigned to UWORD
|
addTypecastOrCastedValueModification(modifications, it.second, targetDt, call as Node)
|
||||||
if(requiredType!=DataType.UWORD || argtype !in PassByReferenceDatatypes)
|
} else if(identifier!=null && targetDt==DataType.UWORD && argDt in PassByReferenceDatatypes) {
|
||||||
addTypecastOrCastedValueModification(modifications, arg, requiredType, call as Node)
|
if(!identifier.isSubroutineParameter(program)) {
|
||||||
} else if(requiredType == DataType.UWORD && argtype in PassByReferenceDatatypes) {
|
// We allow STR/ARRAY values for UWORD parameters.
|
||||||
// We allow STR/ARRAY values in place of UWORD parameters.
|
// If it's an array (not STR), take the address.
|
||||||
// Take their address instead, UNLESS it's a str parameter in the containing subroutine
|
if(argDt != DataType.STR) {
|
||||||
val identifier = arg as? IdentifierReference
|
|
||||||
if(identifier?.isSubroutineParameter(program)==false) {
|
|
||||||
modifications += IAstModification.ReplaceNode(
|
modifications += IAstModification.ReplaceNode(
|
||||||
identifier,
|
identifier,
|
||||||
AddressOf(identifier, null, arg.position),
|
AddressOf(identifier, null, it.second.position),
|
||||||
call as Node)
|
call as Node
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else if(arg is NumericLiteral) {
|
}
|
||||||
val cast = arg.cast(requiredType)
|
} else if(targetDt==DataType.BOOL) {
|
||||||
if(cast.isValid)
|
addTypecastOrCastedValueModification(modifications, it.second, DataType.BOOL, call as Node)
|
||||||
|
} else if(argDt isAssignableTo targetDt) {
|
||||||
|
if(argDt!=DataType.STR || targetDt!=DataType.UWORD)
|
||||||
|
addTypecastOrCastedValueModification(modifications, it.second, targetDt, call as Node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val identifier = it.second as? IdentifierReference
|
||||||
|
if(identifier!=null && targetDt==DataType.UWORD) {
|
||||||
|
// take the address of the identifier
|
||||||
modifications += IAstModification.ReplaceNode(
|
modifications += IAstModification.ReplaceNode(
|
||||||
arg,
|
identifier,
|
||||||
cast.valueOrZero(),
|
AddressOf(identifier, null, it.second.position),
|
||||||
call as Node)
|
|
||||||
} else if(requiredType==DataType.BOOL && argtype!=DataType.BOOL) {
|
|
||||||
// cast to bool
|
|
||||||
addTypecastOrCastedValueModification(modifications, arg, requiredType, call as Node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// if the argument is an identifier reference and the param is UWORD, add the missing &.
|
|
||||||
if(arg is IdentifierReference && DataType.UWORD == param.type) {
|
|
||||||
modifications += IAstModification.ReplaceNode(
|
|
||||||
arg,
|
|
||||||
AddressOf(arg, null, arg.position),
|
|
||||||
call as Node
|
call as Node
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
is BuiltinFunctionPlaceholder -> {
|
|
||||||
val func = BuiltinFunctions.getValue(sub.name)
|
|
||||||
func.parameters.zip(call.args).forEachIndexed { index, pair ->
|
|
||||||
val argItype = pair.second.inferType(program)
|
|
||||||
if (argItype.isKnown) {
|
|
||||||
val argtype = argItype.getOr(DataType.UNDEFINED)
|
|
||||||
if (pair.first.possibleDatatypes.all { argtype != it }) {
|
|
||||||
for (possibleType in pair.first.possibleDatatypes) {
|
|
||||||
if (argtype isAssignableTo possibleType) {
|
|
||||||
addTypecastOrCastedValueModification(modifications, pair.second, possibleType, call as Node)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
else if(DataType.UWORD in pair.first.possibleDatatypes && argtype in PassByReferenceDatatypes) {
|
|
||||||
// We allow STR/ARRAY values in place of UWORD parameters.
|
|
||||||
// Take their address instead, UNLESS it's a str parameter in the containing subroutine
|
|
||||||
val identifier = pair.second as? IdentifierReference
|
|
||||||
if(identifier?.isSubroutineParameter(program)==false) {
|
|
||||||
modifications += IAstModification.ReplaceNode(
|
|
||||||
call.args[index],
|
|
||||||
AddressOf(identifier, null, pair.second.position),
|
|
||||||
call as Node)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// if the argument is an identifier reference and the param is UWORD, add the missing &.
|
|
||||||
if(pair.second is IdentifierReference && DataType.UWORD in pair.first.possibleDatatypes) {
|
|
||||||
modifications += IAstModification.ReplaceNode(
|
|
||||||
call.args[index],
|
|
||||||
AddressOf(pair.second as IdentifierReference, null, pair.second.position),
|
|
||||||
call as Node
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> { }
|
|
||||||
}
|
|
||||||
|
|
||||||
return modifications
|
return modifications
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -448,7 +407,11 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
|||||||
if(expressionToCast is NumericLiteral && expressionToCast.type!=DataType.FLOAT) { // refuse to automatically truncate floats
|
if(expressionToCast is NumericLiteral && expressionToCast.type!=DataType.FLOAT) { // refuse to automatically truncate floats
|
||||||
val castedValue = expressionToCast.cast(requiredType)
|
val castedValue = expressionToCast.cast(requiredType)
|
||||||
if (castedValue.isValid) {
|
if (castedValue.isValid) {
|
||||||
|
val signOriginal = sign(expressionToCast.number)
|
||||||
|
val signCasted = sign(castedValue.valueOrZero().number)
|
||||||
|
if(signOriginal==signCasted) {
|
||||||
modifications += IAstModification.ReplaceNode(expressionToCast, castedValue.valueOrZero(), parent)
|
modifications += IAstModification.ReplaceNode(expressionToCast, castedValue.valueOrZero(), parent)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
TODO
|
TODO
|
||||||
====
|
====
|
||||||
|
|
||||||
- don't add implicit type casts to function call arguments, so diskio.f_read(bytevar, 0) will become a compilation error
|
|
||||||
|
|
||||||
- [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
|
- [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
|
||||||
- [on branch: ir-less-branch-opcodes] IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction
|
- [on branch: ir-less-branch-opcodes] IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction
|
||||||
- IR: reduce amount of CMP/CMPI after instructions that set the status bits correctly (LOADs? INC? Bitwise operations, etc), but only after setting the status bits is verified!
|
- IR: reduce amount of CMP/CMPI after instructions that set the status bits correctly (LOADs? INC? Bitwise operations, etc), but only after setting the status bits is verified!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user