diff --git a/codeAst/src/prog8/code/ast/AstExpressions.kt b/codeAst/src/prog8/code/ast/AstExpressions.kt index 7174f7363..a6647bb0c 100644 --- a/codeAst/src/prog8/code/ast/AstExpressions.kt +++ b/codeAst/src/prog8/code/ast/AstExpressions.kt @@ -38,7 +38,11 @@ class PtArray(type: DataType, position: Position): PtExpression(type, position) } -class PtBuiltinFunctionCall(val name: String, val void: Boolean, type: DataType, position: Position) : PtExpression(type, position) { +class PtBuiltinFunctionCall(val name: String, + val void: Boolean, + val hasNoSideEffects: Boolean, + type: DataType, + position: Position) : PtExpression(type, position) { init { if(!void) require(type!=DataType.UNDEFINED) @@ -47,7 +51,7 @@ class PtBuiltinFunctionCall(val name: String, val void: Boolean, type: DataType, val args: List get() = children.map { it as PtExpression } override fun printProperties() { - print("$name void=$void") + print("$name void=$void noSideFx=$hasNoSideEffects") } } diff --git a/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt index 4777e8ae8..07bd01aba 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt @@ -77,7 +77,7 @@ internal class ExpressionGen(private val codeGen: CodeGen) { segWithArg } is PtBuiltinFunctionCall -> { - val segWithArg = PtBuiltinFunctionCall(segment.name, segment.void, segment.type, segment.position) + val segWithArg = PtBuiltinFunctionCall(segment.name, segment.void, segment.hasNoSideEffects, segment.type, segment.position) segWithArg.children.add(0, PtIdentifier(listOf(":vmreg-$sourceReg"), listOf(":vmreg-$sourceReg"), sourceDt, segment.position)) segWithArg } diff --git a/compiler/src/prog8/compiler/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/IntermediateAstMaker.kt index 51e4d6187..265874ec6 100644 --- a/compiler/src/prog8/compiler/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/IntermediateAstMaker.kt @@ -149,7 +149,8 @@ class IntermediateAstMaker(val program: Program) { private fun transform(srcNode: BuiltinFunctionCallStatement): PtBuiltinFunctionCall { val type = builtinFunctionReturnType(srcNode.name, srcNode.args, program).getOr(DataType.UNDEFINED) - val call = PtBuiltinFunctionCall(srcNode.name, true, type, srcNode.position) + val noSideFx = BuiltinFunctions.getValue(srcNode.name).pure + val call = PtBuiltinFunctionCall(srcNode.name, true, noSideFx, type, srcNode.position) for (arg in srcNode.args) call.add(transformExpression(arg)) return call @@ -436,7 +437,8 @@ class IntermediateAstMaker(val program: Program) { private fun transform(srcCall: BuiltinFunctionCall): PtBuiltinFunctionCall { val type = srcCall.inferType(program).getOrElse { throw FatalAstException("unknown dt") } - val call = PtBuiltinFunctionCall(srcCall.name, false, type, srcCall.position) + val noSideFx = BuiltinFunctions.getValue(srcCall.name).pure + val call = PtBuiltinFunctionCall(srcCall.name, false, noSideFx, type, srcCall.position) for (arg in srcCall.args) call.add(transformExpression(arg)) return call