diff --git a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt index 9c2b9e49b..ce5ef5067 100644 --- a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt @@ -57,8 +57,7 @@ class StatementOptimizer(private val program: Program, } override fun after(functionCallStatement: FunctionCallStatement, parent: Node): Iterable { - // TODO use functionCallStatement.target.targetStatement() is BuiltinFunctionStatementPlaceholder ? - if(functionCallStatement.target.nameInSource.size==1 && functionCallStatement.target.nameInSource[0] in functions.names) { + if(functionCallStatement.target.targetStatement(program) is BuiltinFunctionStatementPlaceholder) { val functionName = functionCallStatement.target.nameInSource[0] if (functionName in functions.purefunctionNames) { errors.warn("statement has no effect (function return value is discarded)", functionCallStatement.position) diff --git a/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt b/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt index 13e92d1bb..4cd1c13d3 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt @@ -367,7 +367,7 @@ private fun builtinSin8(args: List, position: Position, program: Pro throw SyntaxError("sin8 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.BYTE, 127.0 * sin(rad), position) + return NumericLiteralValue(DataType.BYTE, round(127.0 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -376,7 +376,7 @@ private fun builtinSin8u(args: List, position: Position, program: Pr throw SyntaxError("sin8u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.UBYTE, 128.0 + 127.5 * sin(rad), position) + return NumericLiteralValue(DataType.UBYTE, round(128.0 + 127.5 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -385,7 +385,7 @@ private fun builtinSinR8(args: List, position: Position, program: Pr throw SyntaxError("sinr8 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.BYTE, 127.0 * sin(rad), position) + return NumericLiteralValue(DataType.BYTE, round(127.0 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -394,7 +394,7 @@ private fun builtinSinR8u(args: List, position: Position, program: P throw SyntaxError("sinr8u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.UBYTE, 128.0 + 127.5 * sin(rad), position) + return NumericLiteralValue(DataType.UBYTE, round(128.0 + 127.5 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -403,7 +403,7 @@ private fun builtinCos8(args: List, position: Position, program: Pro throw SyntaxError("cos8 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.BYTE, 127.0 * cos(rad), position) + return NumericLiteralValue(DataType.BYTE, round(127.0 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -412,7 +412,7 @@ private fun builtinCos8u(args: List, position: Position, program: Pr throw SyntaxError("cos8u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.UBYTE, 128.0 + 127.5 * cos(rad), position) + return NumericLiteralValue(DataType.UBYTE, round(128.0 + 127.5 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -421,7 +421,7 @@ private fun builtinCosR8(args: List, position: Position, program: Pr throw SyntaxError("cosr8 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.BYTE, 127.0 * cos(rad), position) + return NumericLiteralValue(DataType.BYTE, round(127.0 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -430,7 +430,7 @@ private fun builtinCosR8u(args: List, position: Position, program: P throw SyntaxError("cosr8u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.UBYTE, 128.0 + 127.5 * cos(rad), position) + return NumericLiteralValue(DataType.UBYTE, round(128.0 + 127.5 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -439,7 +439,7 @@ private fun builtinSin16(args: List, position: Position, program: Pr throw SyntaxError("sin16 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.WORD, 32767.0 * sin(rad), position) + return NumericLiteralValue(DataType.WORD, round(32767.0 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -448,7 +448,7 @@ private fun builtinSin16u(args: List, position: Position, program: P throw SyntaxError("sin16u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.UWORD, 32768.0 + 32767.5 * sin(rad), position) + return NumericLiteralValue(DataType.UWORD, round(32768.0 + 32767.5 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -457,7 +457,7 @@ private fun builtinSinR16(args: List, position: Position, program: P throw SyntaxError("sinr16 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.WORD, 32767.0 * sin(rad), position) + return NumericLiteralValue(DataType.WORD, round(32767.0 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -466,7 +466,7 @@ private fun builtinSinR16u(args: List, position: Position, program: throw SyntaxError("sinr16u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.UWORD, 32768.0 + 32767.5 * sin(rad), position) + return NumericLiteralValue(DataType.UWORD, round(32768.0 + 32767.5 * sin(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -475,7 +475,7 @@ private fun builtinCos16(args: List, position: Position, program: Pr throw SyntaxError("cos16 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.WORD, 32767.0 * cos(rad), position) + return NumericLiteralValue(DataType.WORD, round(32767.0 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -484,7 +484,7 @@ private fun builtinCos16u(args: List, position: Position, program: P throw SyntaxError("cos16u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.UWORD, 32768.0 + 32767.5 * cos(rad), position) + return NumericLiteralValue(DataType.UWORD, round(32768.0 + 32767.5 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -493,7 +493,7 @@ private fun builtinCosR16(args: List, position: Position, program: P throw SyntaxError("cosr16 requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.WORD, 32767.0 * cos(rad), position) + return NumericLiteralValue(DataType.WORD, round(32767.0 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") @@ -502,7 +502,7 @@ private fun builtinCosR16u(args: List, position: Position, program: throw SyntaxError("cosr16u requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() val rad = constval.number / 180.0 * 2.0 * PI - return NumericLiteralValue(DataType.UWORD, 32768.0 + 32767.5 * cos(rad), position) + return NumericLiteralValue(DataType.UWORD, round(32768.0 + 32767.5 * cos(rad)), position) } @Suppress("UNUSED_PARAMETER") diff --git a/docs/source/todo.rst b/docs/source/todo.rst index e32719afa..242191beb 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -5,7 +5,7 @@ For next compiler release (7.4) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use GoSub to call subroutines (statements): - [DONE] allow separate assigns to subroutine's parameter variables / registers - - turn a regular subroutine call into assignments to the parameters + GoSub (take code from gosub branch) + - [DONE] turn a regular subroutine call into assignments to the parameters + GoSub (take code from gosub branch) Function call expression: - move args to assignments to params diff --git a/examples/test.p8 b/examples/test.p8 index 7a7f40adf..4891695b3 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,7 +6,8 @@ main { sub start() { uword xx - xx = random_name() + ubyte yy = 33 + void sin8u(yy) ; concat_string(random_name()) ; ubyte xx=20