diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index 9bf06afc3..6b8fa6729 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -1,7 +1,6 @@ %option enable_floats - ~ main { sub start() { @@ -19,9 +18,24 @@ sub start() { s1 = "ciao" _vm_write_str(s1) + A=xyz() + asmxyz(333, 2) + asmxyz(333,2) + X=asmxyz2(333,2) return } +sub xyz() -> byte { + + return 33 +} + +asmsub asmxyz(v1: word @ XY, v2: byte @ A) -> clobbers() -> (byte @ A, word @ XY) { + return 44,4455 +} +asmsub asmxyz2(v1: word @ XY, v2: byte @ A) -> clobbers(X) -> (byte @ A) { + return 44 +} } diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 080835bb4..84e5817fb 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -60,7 +60,7 @@ Code Subroutine Defines a piece of code that can be called by its name from different locations in your code. - It accepts parameters and can return result values. + It accepts parameters and can return a value (optional). It can define its own variables, and it is even possible to define subroutines nested inside other subroutines. Their contents is scoped accordingly. @@ -465,22 +465,15 @@ Defining a subroutine ^^^^^^^^^^^^^^^^^^^^^ Subroutines are parts of the code that can be repeatedly invoked using a subroutine call from elsewhere. -Their definition, using the ``sub`` statement, includes the specification of the required input- and output parameters. +Their definition, using the ``sub`` statement, includes the specification of the required parameters and return value. Subroutines can be defined in a Block, but also nested inside another subroutine. Everything is scoped accordingly. -.. todo:: - re-introduce register based params and return values. - For now, only register based parameters are supported (A, X, Y and paired registers AX, AY and XY, - and various flags of the status register P: Pc (carry), Pz (zero), Pn (negative), Pv (overflow). - For subroutine return values, it is the same (registers, status flags). - Calling a subroutine ^^^^^^^^^^^^^^^^^^^^ The arguments in parentheses after the function name, should match the parameters in the subroutine definition. -The output variables must occur in the correct sequence of return values as specified -in the subroutine's definiton. It is possible to not store the return values but the compiler +It is possible to not store the return value but the compiler will issue a warning then telling you the result values of a subroutine call are discarded. .. caution:: diff --git a/docs/source/syntaxreference.rst b/docs/source/syntaxreference.rst index b9f66a569..7598ef103 100644 --- a/docs/source/syntaxreference.rst +++ b/docs/source/syntaxreference.rst @@ -398,13 +398,10 @@ You call a subroutine like this:: [ result = ] subroutinename_or_address ( [argument...] ) ; example: - outputvar1, outputvar2 = subroutine ( arg1, arg2, arg3 ) + resultvariable = subroutine ( arg1, arg2, arg3 ) Arguments are separated by commas. The argument list can also be empty if the subroutine takes no parameters. -If the subroutine returns one or more result values, you must use an assignment statement -to store those values somewhere. If the subroutine has no result values, you must -omit the assignment. @@ -413,7 +410,7 @@ Subroutine definitions The syntax is:: - sub ( [parameters] ) [ -> returnvalues ] { + sub ( [parameters] ) [ -> returntype ] { ... statements ... } @@ -424,24 +421,14 @@ The syntax is:: The open curly brace must immediately follow the subroutine result specification on the same line, and can have nothing following it. The close curly brace must be on its own line as well. +The parameters is a (possibly empty) comma separated list of ": " pairs specifying the input parameters. +The return type has to be specified if the subroutine returns a value. .. todo:: asmsub with assigning memory address to refer to predefined ROM subroutines asmsub with a regular body to precisely control what registers are used to call the subroutine -.. data:: parameters - - Comma separated list of ":" pairs specifying the input parameters. - Can be empty. - -.. data:: proc_results - - Comma separated list of result value datatypes. - A subroutine without return values must omit the whole part with the arrow - after the parameter list. - - Loops -----