undid failed attempt of using sys.push/sys.pop for stack args - now using new push(), pushw(), pop(), popw() builtin functions

This commit is contained in:
Irmen de Jong 2021-11-28 01:22:40 +01:00
parent 02348924d0
commit e8f4686430
11 changed files with 182 additions and 147 deletions

View File

@ -1390,9 +1390,15 @@ $label nop""")
private fun translate(jump: Jump) {
if(jump.isGosub) {
saveXbeforeCall(jump as GoSub)
out(" jsr ${getJumpTarget(jump)}")
restoreXafterCall(jump as GoSub)
val tgt = jump.identifier!!.targetSubroutine(program)
if(tgt!=null && tgt.isAsmSubroutine) {
// no need to rescue X , this has been taken care of already
out(" jsr ${getJumpTarget(jump)}")
} else {
saveXbeforeCall(jump as GoSub)
out(" jsr ${getJumpTarget(jump)}")
restoreXafterCall(jump as GoSub)
}
}
else
jmp(getJumpTarget(jump))

View File

@ -67,8 +67,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
"peek" -> throw AssemblyError("peek() should have been replaced by @()")
"pokew" -> funcPokeW(fcall)
"poke" -> throw AssemblyError("poke() should have been replaced by @()")
// "push", "pushw" -> funcPush(fcall, func)
// "pop", "popw" -> funcPop(func)
"push", "pushw" -> funcPush(fcall, func)
"pop", "popw" -> funcPop(fcall, func)
"cmp" -> funcCmp(fcall)
"callfar" -> funcCallFar(fcall)
"callrom" -> funcCallRom(fcall)
@ -76,29 +76,93 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
}
}
// private fun funcPop(func: FSignature) {
// if(func.name=="pop") {
// asmgen.out(" pla")
// } else {
// if (asmgen.isTargetCpu(CpuType.CPU65c02))
// asmgen.out(" ply | pla")
// else
// asmgen.out(" pla | tay | pla")
// }
// }
//
// private fun funcPush(fcall: IFunctionCall, func: FSignature) {
// if(func.name=="push") {
// asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.A)
// asmgen.out(" pha")
// } else {
// asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY)
// if (asmgen.isTargetCpu(CpuType.CPU65c02))
// asmgen.out(" pha | phy")
// else
// asmgen.out(" pha | tya | pha")
// }
// }
private fun funcPop(fcall: IFunctionCall, func: FSignature) {
require(fcall.args[0] is IdentifierReference) {
"attempt to pop a value into a differently typed variable, or in something else that isn't supported ${(fcall as Node).position}"
}
val target = (fcall.args[0] as IdentifierReference).targetVarDecl(program)!!
val parameter = target.subroutineParameter
if(parameter!=null) {
val sub = parameter.definingSubroutine!!
require(sub.isAsmSubroutine) {
"push/pop arg passing only supported on asmsubs ${(fcall as Node).position}"
}
val reg = sub.asmParameterRegisters[sub.parameters.indexOf(parameter)]
if(reg.statusflag!=null)
TODO("can't assign value to processor statusflag directly")
else {
if (func.name == "pop") {
if (asmgen.isTargetCpu(CpuType.CPU65c02)) {
when (reg.registerOrPair) {
RegisterOrPair.A -> asmgen.out(" pla")
RegisterOrPair.X -> asmgen.out(" plx")
RegisterOrPair.Y -> asmgen.out(" ply")
in Cx16VirtualRegisters -> asmgen.out(" pla | sta cx16.${reg.registerOrPair!!.name.lowercase()}")
else -> throw AssemblyError("invalid target register ${reg.registerOrPair}")
}
} else {
when (reg.registerOrPair) {
// TODO make sure that A is pushed first so popped last, so that those store/load to save A are no longer needed (c64)
RegisterOrPair.A -> asmgen.out(" pla")
RegisterOrPair.X -> asmgen.out(" sta P8ZP_SCRATCH_REG | pla | tax | lda P8ZP_SCRATCH_REG")
RegisterOrPair.Y -> asmgen.out(" sta P8ZP_SCRATCH_REG | pla | tay | lda P8ZP_SCRATCH_REG")
in Cx16VirtualRegisters -> asmgen.out(" pla | sta cx16.${reg.registerOrPair!!.name.lowercase()}")
else -> throw AssemblyError("invalid target register ${reg.registerOrPair}")
}
}
} else {
if (asmgen.isTargetCpu(CpuType.CPU65c02))
when (reg.registerOrPair) {
RegisterOrPair.AX -> asmgen.out(" plx | pla")
RegisterOrPair.AY -> asmgen.out(" ply | pla")
RegisterOrPair.XY -> asmgen.out(" ply | plx")
in Cx16VirtualRegisters -> {
val regname = reg.registerOrPair!!.name.lowercase()
asmgen.out(" pla | sta cx16.$regname+1 | pla | sta cx16.$regname")
}
else -> throw AssemblyError("invalid target register ${reg.registerOrPair}")
}
else {
when (reg.registerOrPair) {
RegisterOrPair.AX -> asmgen.out(" pla | tax | pla")
RegisterOrPair.AY -> asmgen.out(" pla | tay | pla")
RegisterOrPair.XY -> asmgen.out(" pla | tay | pla | tax")
in Cx16VirtualRegisters -> {
val regname = reg.registerOrPair!!.name.lowercase()
asmgen.out(" pla | sta cx16.$regname+1 | pla | sta cx16.$regname")
}
else -> throw AssemblyError("invalid target register ${reg.registerOrPair}")
}
}
}
}
} else {
val tgt = AsmAssignTarget(TargetStorageKind.VARIABLE, program, asmgen, target.datatype, (fcall as Node).definingSubroutine, variableAsmName = asmgen.asmVariableName(target.name))
if (func.name == "pop") {
asmgen.out(" pla")
asmgen.assignRegister(RegisterOrPair.A, tgt)
} else {
if (asmgen.isTargetCpu(CpuType.CPU65c02))
asmgen.out(" ply | pla")
else
asmgen.out(" pla | tay | pla")
asmgen.assignRegister(RegisterOrPair.AY, tgt)
}
}
}
private fun funcPush(fcall: IFunctionCall, func: FSignature) {
if(func.name=="push") {
asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.A)
asmgen.out(" pha")
} else {
asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY)
if (asmgen.isTargetCpu(CpuType.CPU65c02))
asmgen.out(" pha | phy")
else
asmgen.out(" pha | tya | pha")
}
}
private fun funcCallFar(fcall: IFunctionCall) {
if(asmgen.options.compTarget !is Cx16Target)

View File

@ -648,43 +648,6 @@ _longcopy
}}
}
inline asmsub push(ubyte value @A) {
%asm {{
pha
}}
}
inline asmsub pop() -> ubyte @A {
%asm {{
pla
}}
}
inline asmsub popx() -> ubyte @X {
%asm {{
sta P8ZP_SCRATCH_REG
pla
tax
lda P8ZP_SCRATCH_REG
}}
}
inline asmsub pushw(uword value @AY) {
%asm {{
pha
tya
pha
}}
}
inline asmsub popw() -> uword @AY {
%asm {{
pla
tay
pla
}}
}
inline asmsub clear_carry() {
%asm {{
clc

View File

@ -899,38 +899,6 @@ sys {
}}
}
inline asmsub push(ubyte value @A) {
%asm {{
pha
}}
}
inline asmsub pop() -> ubyte @A {
%asm {{
pla
}}
}
inline asmsub popx() -> ubyte @X {
%asm {{
plx
}}
}
inline asmsub pushw(uword value @AY) {
%asm {{
pha
phy
}}
}
inline asmsub popw() -> uword @AY {
%asm {{
ply
pla
}}
}
inline asmsub clear_carry() {
%asm {{
clc

View File

@ -399,7 +399,20 @@ internal class StatementReorderer(val program: Program,
private fun replaceCallAsmSubStatementWithGosub(function: Subroutine, call: FunctionCallStatement, parent: Node): Iterable<IAstModification> {
if(function.parameters.isEmpty()) {
// 0 params -> just GoSub
return listOf(IAstModification.ReplaceNode(call, GoSub(null, call.target, null, call.position), parent))
val modifications = mutableListOf<IAstModification>()
if(function.shouldSaveX()) {
modifications += IAstModification.InsertBefore(
call,
FunctionCallStatement(IdentifierReference(listOf("sys", "rsavex"), call.position), mutableListOf(), true, call.position),
parent as IStatementContainer
)
modifications += IAstModification.InsertAfter(
call,
FunctionCallStatement(IdentifierReference(listOf("sys", "rrestorex"), call.position), mutableListOf(), true, call.position),
parent as IStatementContainer
)
}
return modifications + IAstModification.ReplaceNode(call, GoSub(null, call.target, null, call.position), parent)
} else if(!options.compTarget.asmsubArgsHaveRegisterClobberRisk(call.args)) {
// no register clobber risk, let the asmgen assign values to the registers directly.
return noModifications
@ -409,12 +422,18 @@ internal class StatementReorderer(val program: Program,
errors.warn("slow argument passing used to avoid register clobbering", call.position)
val argOrder = options.compTarget.asmsubArgsEvalOrder(function)
val modifications = mutableListOf<IAstModification>()
if(function.shouldSaveX())
if(function.shouldSaveX()) {
modifications += IAstModification.InsertBefore(
call,
FunctionCallStatement(IdentifierReference(listOf("sys", "rsavex"), call.position), mutableListOf(), true, call.position),
parent as IStatementContainer
)
modifications += IAstModification.InsertAfter(
call,
FunctionCallStatement(IdentifierReference(listOf("sys", "rrestorex"), call.position), mutableListOf(), true, call.position),
parent as IStatementContainer
)
}
argOrder.reversed().forEach {
val arg = call.args[it]
val param = function.parameters[it]
@ -425,49 +444,19 @@ internal class StatementReorderer(val program: Program,
argOrder.forEach {
val param = function.parameters[it]
val targetName = function.scopedName + param.name
val popassign =
if(function.asmParameterRegisters[it].registerOrPair == RegisterOrPair.X)
popCallAssignX(targetName, param.type, call.position)
else
popCallAssign(targetName, param.type, call.position)
modifications += IAstModification.InsertBefore(call, popassign, parent as IStatementContainer)
val pop = popCall(targetName, param.type, call.position)
modifications += IAstModification.InsertBefore(call, pop, parent as IStatementContainer)
}
if(function.shouldSaveX())
modifications += IAstModification.InsertAfter(
call,
FunctionCallStatement(IdentifierReference(listOf("sys", "rrestorex"), call.position), mutableListOf(), true, call.position),
parent as IStatementContainer
)
return modifications + IAstModification.ReplaceNode(call, GoSub(null, call.target, null, call.position), parent)
}
}
private fun popCallAssignX(targetName: List<String>, dt: DataType, position: Position): Assignment {
val func = IdentifierReference(listOf("sys", "popx"), position)
val popcall = when(dt) {
DataType.UBYTE -> FunctionCall(func, mutableListOf(), position)
DataType.BYTE -> TypecastExpression(FunctionCall(func, mutableListOf(), position), DataType.UBYTE, true, position)
else -> throw FatalAstException("invalid dt $dt")
}
return Assignment(
AssignTarget(IdentifierReference(targetName, position), null, null, position),
popcall, position
)
}
private fun popCallAssign(targetName: List<String>, dt: DataType, position: Position): Assignment {
val func = IdentifierReference(listOf("sys", if(dt in ByteDatatypes) "pop" else "popw"), position)
val popcall = when(dt) {
DataType.UBYTE, DataType.UWORD -> FunctionCall(func, mutableListOf(), position)
in PassByReferenceDatatypes -> FunctionCall(func, mutableListOf(), position)
DataType.BYTE -> TypecastExpression(FunctionCall(func, mutableListOf(), position), DataType.UBYTE, true, position)
DataType.WORD -> TypecastExpression(FunctionCall(func, mutableListOf(), position), DataType.UWORD, true, position)
else -> throw FatalAstException("invalid dt $dt")
}
return Assignment(
AssignTarget(IdentifierReference(targetName, position), null, null, position),
popcall, position
private fun popCall(targetName: List<String>, dt: DataType, position: Position): FunctionCallStatement {
return FunctionCallStatement(
IdentifierReference(listOf(if(dt in ByteDatatypes) "pop" else "popw"), position),
mutableListOf(IdentifierReference(targetName, position)),
true, position
)
}
@ -481,7 +470,7 @@ internal class StatementReorderer(val program: Program,
}
return FunctionCallStatement(
IdentifierReference(listOf("sys", if(dt in ByteDatatypes) "push" else "pushw"), position),
IdentifierReference(listOf(if(dt in ByteDatatypes) "push" else "pushw"), position),
mutableListOf(pushvalue),
true, position
)

View File

@ -142,10 +142,10 @@ private val functionSignatures: List<FSignature> = listOf(
FSignature("peekw" , true, listOf(FParam("address", arrayOf(DataType.UWORD))), DataType.UWORD),
FSignature("poke" , false, listOf(FParam("address", arrayOf(DataType.UWORD)), FParam("value", arrayOf(DataType.UBYTE))), null),
FSignature("pokew" , false, listOf(FParam("address", arrayOf(DataType.UWORD)), FParam("value", arrayOf(DataType.UWORD))), null),
// FSignature("pop" , false, emptyList(), DataType.UBYTE),
// FSignature("popw" , false, emptyList(), DataType.UWORD),
// FSignature("push" , false, listOf(FParam("value", ByteDatatypes)), null),
// FSignature("pushw" , false, listOf(FParam("value", WordDatatypes)), null),
FSignature("pop" , false, listOf(FParam("target", ByteDatatypes)), null),
FSignature("popw" , false, listOf(FParam("target", WordDatatypes)), null),
FSignature("push" , false, listOf(FParam("value", ByteDatatypes)), null),
FSignature("pushw" , false, listOf(FParam("value", WordDatatypes)), null),
FSignature("rnd" , false, emptyList(), DataType.UBYTE),
FSignature("rndw" , false, emptyList(), DataType.UWORD),
FSignature("rndf" , false, emptyList(), DataType.FLOAT),

View File

@ -861,6 +861,20 @@ poke(address, value)
pokew(address, value)
writes the word value at the given address in memory, in usual little-endian lsb/msb byte order.
push(value)
pushes a byte value on the CPU hardware stack. Lowlevel function that should normally not be used.
pushw(value)
pushes a 16-bit word value on the CPU hardware stack. Lowlevel function that should normally not be used.
pop(variable)
pops a byte value off the CPU hardware stack into the given variable. Only variables can be used.
Lowlevel function that should normally not be used.
popw(value)
pops a 16-bit word value off the CPU hardware stack into the given variable. Only variables can be used.
Lowlevel function that should normally not be used.
rnd()
returns a pseudo-random byte from 0..255

View File

@ -7,9 +7,24 @@ main {
test_stack.test()
uword @shared uw
uw= 0
routine(uw+11, 22,33, true, 44)
routine2(uw+11, 22,33, true, 44)
ubyte @shared ub
push(127)
popw(ub) ; TODO give type error
pop(ub)
txt.print_ub(ub)
txt.nl()
pushw(32767)
popw(uw)
txt.print_uw(uw)
txt.nl()
uw=10000
routines(44,uw+123)
routines2(44,uw+123)
routine(uw+123, 22,33, true, 44)
routine2(uw+123, 22,33, true, 44)
test_stack.test()
@ -31,6 +46,13 @@ main {
txt.nl()
}
sub routines(ubyte bb, uword num) {
txt.print_ub(bb)
txt.spc()
txt.print_uw(num)
txt.nl()
}
; TODO make switch R3 use Pc instead and make that work !
asmsub routine2(uword num @AY, ubyte a1 @R1, ubyte a2 @R2, ubyte switch @R3, ubyte a3 @X) {
%asm {{
@ -47,4 +69,13 @@ main {
}}
}
asmsub routines2(ubyte bb @X, uword num @AY) {
%asm {{
sta routines.num
sty routines.num+1
stx routines.bb
jmp routines
}}
}
}

View File

@ -14,7 +14,7 @@
<keywords keywords="&amp;;-&gt;;@;\$;and;as;asmsub;break;clobbers;do;downto;else;false;for;goto;if;if_cc;if_cs;if_eq;if_mi;if_ne;if_neg;if_nz;if_pl;if_pos;if_vc;if_vs;if_z;in;inline;not;or;repeat;return;romsub;step;sub;to;true;until;when;while;xor;~" ignore_case="false" />
<keywords2 keywords="%address;%asm;%asmbinary;%asminclude;%breakpoint;%import;%launcher;%option;%output;%zeropage;%zpreserved" />
<keywords3 keywords="byte;const;float;shared;str;ubyte;uword;void;word;zp" />
<keywords4 keywords="abs;acos;all;any;asin;atan;avg;callfar;callrom;ceil;cmp;cos;cos16;cos16u;cos8;cos8u;cosr16;cosr16u;cosr8;cosr8u;deg;floor;len;ln;log2;lsb;lsl;lsr;max;memory;min;mkword;msb;peek;peekw;poke;pokew;rad;reverse;rnd;rndf;rndw;rol;rol2;ror;ror2;round;sgn;sin;sin16;sin16u;sin8;sin8u;sinr16;sinr16u;sinr8;sinr8u;sizeof;sort;sqrt;sqrt16;sum;swap;tan" />
<keywords4 keywords="abs;acos;all;any;asin;atan;avg;callfar;callrom;ceil;cmp;cos;cos16;cos16u;cos8;cos8u;cosr16;cosr16u;cosr8;cosr8u;deg;floor;len;ln;log2;lsb;lsl;lsr;max;memory;min;mkword;msb;peek;peekw;poke;pokew;pop;popw;push;pushw;rad;reverse;rnd;rndf;rndw;rol;rol2;ror;ror2;round;sgn;sin;sin16;sin16u;sin8;sin8u;sinr16;sinr16u;sinr8;sinr8u;sizeof;sort;sqrt;sqrt16;sum;swap;tan" />
</highlighting>
<extensionMap>
<mapping ext="p8" />

View File

@ -27,7 +27,7 @@
<Keywords name="Keywords1">void const&#x000D;&#x000A;str&#x000D;&#x000A;byte ubyte&#x000D;&#x000A;word uword&#x000D;&#x000A;float&#x000D;&#x000A;zp shared</Keywords>
<Keywords name="Keywords2">%address&#x000D;&#x000A;%asm&#x000D;&#x000A;%asmbinary&#x000D;&#x000A;%asminclude&#x000D;&#x000A;%breakpoint&#x000D;&#x000A;%import&#x000D;&#x000A;%launcher&#x000D;&#x000A;%option&#x000D;&#x000A;%output&#x000D;&#x000A;%zeropage&#x000D;&#x000A;%zpreserved</Keywords>
<Keywords name="Keywords3">inline sub asmsub romsub&#x000D;&#x000A;clobbers&#x000D;&#x000A;asm&#x000D;&#x000A;if&#x000D;&#x000A;when else&#x000D;&#x000A;if_cc if_cs if_eq if_mi if_neg if_nz if_pl if_pos if_vc if_vs if_z&#x000D;&#x000A;for in step do while repeat&#x000D;&#x000A;break return goto</Keywords>
<Keywords name="Keywords4">abs acos all any asin atan avg callfar callrom ceil cmp cos cos16 cos16u cos8 cos8u cosr8 cosr8u cosr16 cosr16u deg floor len ln log2 lsb lsl lsr max memory min mkword msb peek peekw poke pokew rad reverse rnd rndf rndw rol rol2 ror ror2 round sgn sin sin16 sin16u sin8 sin8u sinr8 sinr8u sinr16 sinr16u sizeof sort sqrt sqrt16 sum swap tan&#x000D;&#x000A;</Keywords>
<Keywords name="Keywords4">abs acos all any asin atan avg callfar callrom ceil cmp cos cos16 cos16u cos8 cos8u cosr8 cosr8u cosr16 cosr16u deg floor len ln log2 lsb lsl lsr max memory min mkword msb peek peekw poke pokew push pushw pop popw rad reverse rnd rndf rndw rol rol2 ror ror2 round sgn sin sin16 sin16u sin8 sin8u sinr8 sinr8u sinr16 sinr16u sizeof sort sqrt sqrt16 sum swap tan&#x000D;&#x000A;</Keywords>
<Keywords name="Keywords5">true false&#x000D;&#x000A;not and or xor&#x000D;&#x000A;as to downto</Keywords>
<Keywords name="Keywords6"></Keywords>
<Keywords name="Keywords7"></Keywords>

View File

@ -15,7 +15,7 @@ syn keyword prog8BuiltInFunc sqrt16 sqrt tan
syn keyword prog8BuiltInFunc any all len max min reverse sum sort
" Miscellaneous functions
syn keyword prog8BuiltInFunc cmp lsb msb mkword peek peekw poke pokew rnd rndw
syn keyword prog8BuiltInFunc cmp lsb msb mkword peek peekw poke pokew rnd rndw push pushw pop popw
syn keyword prog8BuiltInFunc rndf rol rol2 ror ror2 sizeof
syn keyword prog8BuiltInFunc swap memory callfar callrom