diff --git a/compiler/build.gradle b/compiler/build.gradle index ca33c700b..07c24f808 100644 --- a/compiler/build.gradle +++ b/compiler/build.gradle @@ -1,11 +1,11 @@ buildscript { dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0" } } plugins { - // id "org.jetbrains.kotlin.jvm" version "1.3.72" + // id "org.jetbrains.kotlin.jvm" version "1.4.0" id 'application' id 'org.jetbrains.dokka' version "0.9.18" id 'com.github.johnrengelman.shadow' version '5.2.0' diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index 86ca86348..f2979462f 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -79,7 +79,7 @@ fun compileProgram(filepath: Path, private fun parseImports(filepath: Path, errors: ErrorReporter): Triple> { println("Parsing...") - val importer = ModuleImporter(errors) + val importer = ModuleImporter() val programAst = Program(moduleName(filepath.fileName), mutableListOf()) importer.importModule(programAst, filepath) errors.handle() diff --git a/compiler/src/prog8/compiler/Zeropage.kt b/compiler/src/prog8/compiler/Zeropage.kt index 6ad868577..f0a53bece 100644 --- a/compiler/src/prog8/compiler/Zeropage.kt +++ b/compiler/src/prog8/compiler/Zeropage.kt @@ -39,13 +39,13 @@ abstract class Zeropage(protected val options: CompilationOptions) { if(free.size > 0) { if(size==1) { - for(candidate in free.min()!! .. free.max()!!+1) { + for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1) { if(loneByte(candidate)) return makeAllocation(candidate, 1, datatype, scopedname) } return makeAllocation(free[0], 1, datatype, scopedname) } - for(candidate in free.min()!! .. free.max()!!+1) { + for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1) { if (sequentialFree(candidate, size)) return makeAllocation(candidate, size, datatype, scopedname) } diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 36ecb64fc..d117e9801 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -40,7 +40,7 @@ internal class AsmGen(private val program: Program, private val forloopsAsmGen = ForLoopsAsmGen(program, this) private val postincrdecrAsmGen = PostIncrDecrAsmGen(program, this) private val functioncallAsmGen = FunctionCallAsmGen(program, this) - private val assignmentAsmGen = AssignmentAsmGen(program, errors, this) + private val assignmentAsmGen = AssignmentAsmGen(program, this) private val expressionsAsmGen = ExpressionsAsmGen(program, this) internal val loopEndLabels = ArrayDeque() internal val blockLevelVarInits = mutableMapOf>() @@ -1002,21 +1002,6 @@ $endLabel""") } } - internal fun translateArrayIndexIntoY(expr: ArrayIndexedExpression) { - when (val index = expr.arrayspec.index) { - is NumericLiteralValue -> throw AssemblyError("this should be optimized directly") - is IdentifierReference -> { - val indexName = asmIdentifierName(index) - out(" ldy $indexName") - } - // TODO optimize more cases, see translateArrayIndexIntoA - else -> { - expressionsAsmGen.translateExpression(index) - out(" inx | ldy $ESTACK_LO_HEX,x") - } - } - } - internal fun translateExpression(expression: Expression) = expressionsAsmGen.translateExpression(expression) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AssignmentAsmGen.kt index c69f105f6..db33a9698 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AssignmentAsmGen.kt @@ -15,7 +15,7 @@ import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX import prog8.compiler.toHex -internal class AssignmentAsmGen(private val program: Program, private val errors: ErrorReporter, private val asmgen: AsmGen) { +internal class AssignmentAsmGen(private val program: Program, private val asmgen: AsmGen) { internal fun translate(assign: Assignment) { if(assign.isInplace) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt index 85d27fa81..22f5b9d25 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt @@ -194,7 +194,6 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge private val optimizedByteMultiplications = setOf(3,5,6,7,9,10,11,12,13,14,15,20,25,40) private val optimizedWordMultiplications = setOf(3,5,6,7,9,10,12,15,20,25,40) - private val powersOfTwo = setOf(0,1,2,4,8,16,32,64,128,256) private fun translateExpression(expr: BinaryExpression) { val leftIDt = expr.left.inferType(program) diff --git a/compiler/src/prog8/functions/BuiltinFunctions.kt b/compiler/src/prog8/functions/BuiltinFunctions.kt index 6f13c9e11..166e251df 100644 --- a/compiler/src/prog8/functions/BuiltinFunctions.kt +++ b/compiler/src/prog8/functions/BuiltinFunctions.kt @@ -103,9 +103,9 @@ val BuiltinFunctions = mapOf( FParam("length", setOf(DataType.UBYTE))), null) ) -fun builtinMax(array: List): Number = array.maxBy { it.toDouble() }!! +fun builtinMax(array: List): Number = array.maxByOrNull { it.toDouble() }!! -fun builtinMin(array: List): Number = array.minBy { it.toDouble() }!! +fun builtinMin(array: List): Number = array.minByOrNull { it.toDouble() }!! fun builtinSum(array: List): Number = array.sumByDouble { it.toDouble() } @@ -312,7 +312,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.toDouble() /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.BYTE, (127.0 * sin(rad)).toShort(), position) + return NumericLiteralValue(DataType.BYTE, (127.0 * sin(rad)).toInt().toShort(), position) } private fun builtinSin8u(args: List, position: Position, program: Program): NumericLiteralValue { @@ -320,7 +320,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.toDouble() /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * sin(rad)).toShort(), position) + return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * sin(rad)).toInt().toShort(), position) } private fun builtinCos8(args: List, position: Position, program: Program): NumericLiteralValue { @@ -328,7 +328,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.toDouble() /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.BYTE, (127.0 * cos(rad)).toShort(), position) + return NumericLiteralValue(DataType.BYTE, (127.0 * cos(rad)).toInt().toShort(), position) } private fun builtinCos8u(args: List, position: Position, program: Program): NumericLiteralValue { @@ -336,7 +336,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.toDouble() /256.0 * 2.0 * PI - return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * cos(rad)).toShort(), position) + return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * cos(rad)).toInt().toShort(), position) } private fun builtinSin16(args: List, position: Position, program: Program): NumericLiteralValue { @@ -375,7 +375,7 @@ private fun builtinSgn(args: List, position: Position, program: Prog if (args.size != 1) throw SyntaxError("sgn requires one argument", position) val constval = args[0].constValue(program) ?: throw NotConstArgumentException() - return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toShort(), position) + return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toInt().toShort(), position) } private fun numericLiteral(value: Number, position: Position): NumericLiteralValue { diff --git a/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt index 702b03837..8fed8e1b2 100644 --- a/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -179,7 +179,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private } -internal class ConstantFoldingOptimizer(private val program: Program, private val errors: ErrorReporter) : AstWalker() { +internal class ConstantFoldingOptimizer(private val program: Program) : AstWalker() { private val noModifications = emptyList() override fun before(memread: DirectMemoryRead, parent: Node): Iterable { diff --git a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt index 3e92d4a9d..fbce69410 100644 --- a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt @@ -6,7 +6,6 @@ import prog8.ast.base.* import prog8.ast.expressions.* import prog8.ast.processing.AstWalker import prog8.ast.processing.IAstModification -import prog8.ast.statements.Assignment import kotlin.math.abs import kotlin.math.log2 import kotlin.math.pow diff --git a/compiler/src/prog8/optimizer/Extensions.kt b/compiler/src/prog8/optimizer/Extensions.kt index 7f151a36f..72f44e4cb 100644 --- a/compiler/src/prog8/optimizer/Extensions.kt +++ b/compiler/src/prog8/optimizer/Extensions.kt @@ -10,7 +10,7 @@ internal fun Program.constantFold(errors: ErrorReporter) { if(errors.isEmpty()) { replacer.applyModifications() - val optimizer = ConstantFoldingOptimizer(this, errors) + val optimizer = ConstantFoldingOptimizer(this) optimizer.visit(this) while (errors.isEmpty() && optimizer.applyModifications() > 0) { optimizer.visit(this) diff --git a/compiler/src/prog8/parser/ModuleParsing.kt b/compiler/src/prog8/parser/ModuleParsing.kt index 8e596d314..a3a2a883a 100644 --- a/compiler/src/prog8/parser/ModuleParsing.kt +++ b/compiler/src/prog8/parser/ModuleParsing.kt @@ -34,7 +34,7 @@ internal class CustomLexer(val modulePath: Path, input: CharStream?) : prog8Lexe internal fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.') -internal class ModuleImporter(private val errors: ErrorReporter) { +internal class ModuleImporter() { internal fun importModule(program: Program, filePath: Path): Module { print("importing '${moduleName(filePath.fileName)}'")