added target() function

This commit is contained in:
Irmen de Jong 2020-12-22 06:13:14 +01:00
parent 684e081399
commit bba4f84503
4 changed files with 23 additions and 3 deletions

View File

@ -6,6 +6,9 @@ import prog8.ast.expressions.*
import prog8.ast.statements.StructDecl import prog8.ast.statements.StructDecl
import prog8.ast.statements.VarDecl import prog8.ast.statements.VarDecl
import prog8.compiler.CompilerException import prog8.compiler.CompilerException
import prog8.compiler.target.C64Target
import prog8.compiler.target.CompilationTarget
import prog8.compiler.target.Cx16Target
import kotlin.math.* import kotlin.math.*
@ -142,6 +145,7 @@ private val functionSignatures: List<FSignature> = listOf(
FSignature("clear_irqd" , false, emptyList(), null), FSignature("clear_irqd" , false, emptyList(), null),
FSignature("read_flags" , false, emptyList(), DataType.UBYTE), FSignature("read_flags" , false, emptyList(), DataType.UBYTE),
FSignature("progend" , true, emptyList(), DataType.UWORD), FSignature("progend" , true, emptyList(), DataType.UWORD),
FSignature("target" , true, emptyList(), DataType.UBYTE, ::builtinTarget),
FSignature("swap" , false, listOf(FParam("first", NumericDatatypes), FParam("second", NumericDatatypes)), null), FSignature("swap" , false, listOf(FParam("first", NumericDatatypes), FParam("second", NumericDatatypes)), null),
FSignature("memcopy" , false, listOf( FSignature("memcopy" , false, listOf(
FParam("from", IterableDatatypes + DataType.UWORD), FParam("from", IterableDatatypes + DataType.UWORD),
@ -480,6 +484,17 @@ private fun builtinSgn(args: List<Expression>, position: Position, program: Prog
return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toInt().toShort(), position) return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toInt().toShort(), position)
} }
private fun builtinTarget(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
if (args.isNotEmpty())
throw SyntaxError("target requires no arguments", position)
val target = when(CompilationTarget.instance) {
is C64Target -> 64
is Cx16Target -> 16
else -> throw CompilerException("unrecognised compilation target")
}
return NumericLiteralValue(DataType.UBYTE, target, position)
}
private fun numericLiteral(value: Number, position: Position): NumericLiteralValue { private fun numericLiteral(value: Number, position: Position): NumericLiteralValue {
val floatNum=value.toDouble() val floatNum=value.toDouble()
val tweakedValue: Number = val tweakedValue: Number =

View File

@ -912,6 +912,13 @@ set_irqd() / clear_irqd()
swap(x, y) swap(x, y)
Swap the values of numerical variables (or memory locations) x and y in a fast way. Swap the values of numerical variables (or memory locations) x and y in a fast way.
target()
Returns byte value designating the target machine that the program was compiled for.
The following return values are currently defined:
- 16 = compiled for CommanderX16 with 65C02 CPU
- 64 = compiled for Commodore-64 with 6502/6510 CPU
progend() progend()
Returns the last address of the program in memory + 1. Returns the last address of the program in memory + 1.
Can be used to load dynamic data after the program, instead of hardcoding something. Can be used to load dynamic data after the program, instead of hardcoding something.

View File

@ -2,7 +2,6 @@
TODO TODO
==== ====
- add target() function that returns 16=cx16, 64=C64
- Cx16 target: support full-screen 640x480 and 320x240 graphics? That requires our own custom graphics routines though to draw lines. - Cx16 target: support full-screen 640x480 and 320x240 graphics? That requires our own custom graphics routines though to draw lines.
- hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine) - hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine)
- make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_' - make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_'

View File

@ -83,8 +83,7 @@ _y .byte 0
} }
sub start () { sub start () {
ubyte ss = withasm(derp(1), 33,66) txt.print_ub(target())
txt.print_ub(ss)
txt.chrout('\n') txt.chrout('\n')
; cx16.r0 = 65535 ; cx16.r0 = 65535