From 68b75fd558e34c8b0d70bbfcd0709fab59db383c Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 28 Nov 2021 02:34:53 +0100 Subject: [PATCH] fix: also allow pass-by-reference arguments to builtin functions that accept UWORD (adds implicit type cast) --- .../compiler/astprocessing/TypecastsAdder.kt | 12 ++++++ examples/test.p8 | 42 +++++++++++-------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt index dde304128..b02e9eee5 100644 --- a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt +++ b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt @@ -192,6 +192,18 @@ class TypecastsAdder(val program: Program, val errors: IErrorReporter) : AstWalk 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, pair.second.position), + call as Node) + break + } + } } } } diff --git a/examples/test.p8 b/examples/test.p8 index 672e6d5a7..319e59672 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,28 +3,34 @@ main { + ubyte[23] savedata + ubyte[17] cargohold = 0 + sub start() { test_stack.test() - uword @shared uw - ubyte @shared ub - word @shared ww + sys.memcopy(&savedata + 2, cargohold, len(cargohold)) + sys.memcopy(cargohold, &savedata + 2, len(cargohold)) - push(127) - 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) +; uword @shared uw +; ubyte @shared ub +; word @shared ww +; +; push(127) +; 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()