diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index d8db59e6e..355f313da 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -1,7 +1,10 @@ package prog8.codegen.cpu6502 import com.github.michaelbull.result.fold -import prog8.ast.* +import prog8.ast.IFunctionCall +import prog8.ast.Node +import prog8.ast.ParentSentinel +import prog8.ast.Program import prog8.ast.base.FatalAstException import prog8.ast.expressions.* import prog8.ast.statements.* @@ -10,7 +13,6 @@ import prog8.code.core.* import prog8.codegen.cpu6502.assignment.* import prog8.compiler.BuiltinFunctions import prog8.compiler.builtinFunctionReturnType -import prog8.code.core.SourceCode import java.util.* import kotlin.io.path.Path import kotlin.io.path.writeLines diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AssemblyProgram.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AssemblyProgram.kt index 85896dbf5..da518f042 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AssemblyProgram.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AssemblyProgram.kt @@ -4,7 +4,6 @@ import com.github.michaelbull.result.Ok import com.github.michaelbull.result.Result import com.github.michaelbull.result.mapError import prog8.code.core.* -import prog8.code.core.SourceCode import java.io.File import java.nio.file.Path import kotlin.io.path.Path diff --git a/codeGenVirtual/src/prog8/codegen/virtual/AssignmentGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/AssignmentGen.kt index 42e178f31..dceec1f78 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/AssignmentGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/AssignmentGen.kt @@ -28,11 +28,10 @@ internal class AssignmentGen(private val codeGen: CodeGen, private val expressio val address = codeGen.allocations.get(ident.targetName) assignSelfInMemory(address, assignment.value, assignment) } else if(memory != null) { - if(memory.address is PtNumber) { + if(memory.address is PtNumber) assignSelfInMemory((memory.address as PtNumber).number.toInt(), assignment.value, assignment) - } else { + else fallbackAssign(assignment) - } } else if(array!=null) { // TODO in-place array element assignment? fallbackAssign(assignment) @@ -58,8 +57,8 @@ internal class AssignmentGen(private val codeGen: CodeGen, private val expressio code // do nothing, mem=mem null assignment. else { // read and write a (i/o) memory location to itself. - code += VmCodeInstruction(Opcode.LOADM, vmDt, reg1 = 0, value = address) - code += VmCodeInstruction(Opcode.STOREM, vmDt, reg1 = 0, value = address) + code += VmCodeInstruction(Opcode.LOADM, vmDt, reg1 =0, value = address) + code += VmCodeInstruction(Opcode.STOREM, vmDt, reg1=0, value = address) code } } diff --git a/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt index a1da9ccd7..a352ae38c 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt @@ -208,7 +208,7 @@ class CodeGen(internal val program: PtProgram, // iterate over a zero-terminated string code += VmCodeInstruction(Opcode.LOAD, VmDataType.BYTE, reg1=indexReg, value=0) code += VmCodeLabel(loopLabel) - code += VmCodeInstruction(Opcode.LOADX, VmDataType.BYTE, reg1=0, reg2=indexReg, value = arrayAddress) + code += VmCodeInstruction(Opcode.LOADX, VmDataType.BYTE, reg1 = 0, reg2=indexReg, value = arrayAddress) code += VmCodeInstruction(Opcode.BZ, VmDataType.BYTE, reg1=0, labelSymbol = endLabel) code += VmCodeInstruction(Opcode.STOREM, VmDataType.BYTE, reg1=0, value = loopvarAddress) code += translateNode(forLoop.statements) diff --git a/codeOptimizers/src/prog8/optimizer/BinExprSplitter.kt b/codeOptimizers/src/prog8/optimizer/BinExprSplitter.kt index 408fdc76c..cd548561f 100644 --- a/codeOptimizers/src/prog8/optimizer/BinExprSplitter.kt +++ b/codeOptimizers/src/prog8/optimizer/BinExprSplitter.kt @@ -3,7 +3,9 @@ package prog8.optimizer import prog8.ast.IStatementContainer import prog8.ast.Node import prog8.ast.Program -import prog8.ast.expressions.* +import prog8.ast.expressions.BinaryExpression +import prog8.ast.expressions.IdentifierReference +import prog8.ast.expressions.TypecastExpression import prog8.ast.getTempVar import prog8.ast.statements.AssignTarget import prog8.ast.statements.Assignment diff --git a/compiler/src/prog8/compiler/ModuleImporter.kt b/compiler/src/prog8/compiler/ModuleImporter.kt index 28e417274..fa06feb19 100644 --- a/compiler/src/prog8/compiler/ModuleImporter.kt +++ b/compiler/src/prog8/compiler/ModuleImporter.kt @@ -9,8 +9,8 @@ import prog8.ast.statements.Directive import prog8.ast.statements.DirectiveArg import prog8.code.core.IErrorReporter import prog8.code.core.Position -import prog8.parser.Prog8Parser import prog8.code.core.SourceCode +import prog8.parser.Prog8Parser import java.io.File import java.nio.file.Path import kotlin.io.path.* diff --git a/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt index e1c6f5d97..d6088afd3 100644 --- a/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt +++ b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt @@ -1,6 +1,9 @@ package prog8.compiler.astprocessing -import prog8.ast.* +import prog8.ast.IFunctionCall +import prog8.ast.IPipe +import prog8.ast.Node +import prog8.ast.Program import prog8.ast.base.FatalAstException import prog8.ast.expressions.Expression import prog8.ast.expressions.FunctionCallExpression diff --git a/compiler/test/ModuleImporterTests.kt b/compiler/test/ModuleImporterTests.kt index d6a9e6459..f9d8c96e7 100644 --- a/compiler/test/ModuleImporterTests.kt +++ b/compiler/test/ModuleImporterTests.kt @@ -11,15 +11,11 @@ import io.kotest.matchers.shouldBe import io.kotest.matchers.string.shouldContain import prog8.ast.Program import prog8.code.core.IErrorReporter -import prog8.compiler.ModuleImporter -import prog8.parser.ParseError import prog8.code.core.SourceCode import prog8.code.core.internedStringsModuleName -import prog8tests.helpers.Helpers -import prog8tests.helpers.DummyFunctions -import prog8tests.helpers.DummyMemsizer -import prog8tests.helpers.DummyStringEncoder -import prog8tests.helpers.ErrorReporterForTests +import prog8.compiler.ModuleImporter +import prog8.parser.ParseError +import prog8tests.helpers.* import kotlin.io.path.* diff --git a/compiler/test/TestCallgraph.kt b/compiler/test/TestCallgraph.kt index d3fdfa33d..b087a8280 100644 --- a/compiler/test/TestCallgraph.kt +++ b/compiler/test/TestCallgraph.kt @@ -9,10 +9,10 @@ import io.kotest.matchers.string.shouldContain import prog8.ast.Program import prog8.ast.statements.Block import prog8.ast.statements.Subroutine +import prog8.code.core.SourceCode import prog8.code.target.C64Target import prog8.compiler.CallGraph import prog8.parser.Prog8Parser.parseModule -import prog8.code.core.SourceCode import prog8tests.helpers.* class TestCallgraph: FunSpec({ diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index d5202b0ff..a722142bd 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -8,8 +8,8 @@ import prog8.code.target.Cx16Target import prog8.compiler.CompilationResult import prog8.compiler.CompilerArguments import prog8.compiler.compileProgram -import prog8tests.helpers.Helpers import prog8tests.helpers.Combinations +import prog8tests.helpers.Helpers import java.nio.file.Path import kotlin.io.path.absolute import kotlin.io.path.exists diff --git a/compiler/test/TestCompilerOnImportsAndIncludes.kt b/compiler/test/TestCompilerOnImportsAndIncludes.kt index 1852de970..d3ec21fc4 100644 --- a/compiler/test/TestCompilerOnImportsAndIncludes.kt +++ b/compiler/test/TestCompilerOnImportsAndIncludes.kt @@ -10,7 +10,8 @@ import prog8.ast.expressions.StringLiteral import prog8.ast.statements.FunctionCallStatement import prog8.ast.statements.Label import prog8.code.target.Cx16Target -import prog8tests.helpers.* +import prog8tests.helpers.Helpers +import prog8tests.helpers.compileFile import kotlin.io.path.name diff --git a/compiler/test/TestCompilerOnRanges.kt b/compiler/test/TestCompilerOnRanges.kt index cfc8cf0f6..31e3b39d4 100644 --- a/compiler/test/TestCompilerOnRanges.kt +++ b/compiler/test/TestCompilerOnRanges.kt @@ -17,8 +17,8 @@ import prog8.code.core.Encoding import prog8.code.core.Position import prog8.code.target.C64Target import prog8.code.target.Cx16Target -import prog8tests.helpers.ErrorReporterForTests import prog8tests.helpers.Combinations +import prog8tests.helpers.ErrorReporterForTests import prog8tests.helpers.compileText diff --git a/compiler/test/TestMemory.kt b/compiler/test/TestMemory.kt index 198d0b55d..f7f5a006d 100644 --- a/compiler/test/TestMemory.kt +++ b/compiler/test/TestMemory.kt @@ -12,10 +12,10 @@ import prog8.ast.expressions.PrefixExpression import prog8.ast.statements.* import prog8.code.core.DataType import prog8.code.core.Position +import prog8.code.core.SourceCode import prog8.code.core.ZeropageWish import prog8.code.target.C64Target import prog8.compiler.printProgram -import prog8.code.core.SourceCode import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer import prog8tests.helpers.DummyStringEncoder diff --git a/compiler/test/TestPipes.kt b/compiler/test/TestPipes.kt index c60099b4b..337b7b44a 100644 --- a/compiler/test/TestPipes.kt +++ b/compiler/test/TestPipes.kt @@ -13,11 +13,11 @@ import prog8.ast.statements.Pipe import prog8.ast.statements.VarDecl import prog8.code.core.DataType import prog8.code.core.Position +import prog8.code.core.SourceCode import prog8.code.target.C64Target +import prog8.code.target.VMTarget import prog8.compiler.astprocessing.AstPreprocessor import prog8.parser.Prog8Parser.parseModule -import prog8.code.core.SourceCode -import prog8.code.target.VMTarget import prog8tests.helpers.* diff --git a/compiler/test/ast/TestAstToSourceText.kt b/compiler/test/ast/TestAstToSourceText.kt index 5eb917313..76a7dbfed 100644 --- a/compiler/test/ast/TestAstToSourceText.kt +++ b/compiler/test/ast/TestAstToSourceText.kt @@ -6,10 +6,10 @@ import io.kotest.matchers.string.shouldContain import prog8.ast.AstToSourceTextConverter import prog8.ast.Module import prog8.ast.Program -import prog8.parser.ParseError -import prog8.parser.Prog8Parser.parseModule import prog8.code.core.SourceCode import prog8.code.core.internedStringsModuleName +import prog8.parser.ParseError +import prog8.parser.Prog8Parser.parseModule import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer import prog8tests.helpers.DummyStringEncoder diff --git a/compiler/test/ast/TestIdentifierRef.kt b/compiler/test/ast/TestIdentifierRef.kt index 7d27d04c3..1839fe5a8 100644 --- a/compiler/test/ast/TestIdentifierRef.kt +++ b/compiler/test/ast/TestIdentifierRef.kt @@ -10,8 +10,8 @@ import prog8.ast.expressions.NumericLiteral import prog8.ast.expressions.PrefixExpression import prog8.ast.statements.* import prog8.code.core.Position -import prog8.parser.Prog8Parser import prog8.code.core.SourceCode +import prog8.parser.Prog8Parser import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer import prog8tests.helpers.DummyStringEncoder diff --git a/compiler/test/ast/TestIntermediateAst.kt b/compiler/test/ast/TestIntermediateAst.kt index 3464529d0..6fc2ffa85 100644 --- a/compiler/test/ast/TestIntermediateAst.kt +++ b/compiler/test/ast/TestIntermediateAst.kt @@ -6,7 +6,6 @@ import io.kotest.matchers.ints.shouldBeGreaterThan import io.kotest.matchers.shouldBe import prog8.code.ast.* import prog8.code.core.DataType -import prog8.code.core.Position import prog8.code.target.C64Target import prog8.compiler.IntermediateAstMaker import prog8tests.helpers.compileText diff --git a/compiler/test/ast/TestProg8Parser.kt b/compiler/test/ast/TestProg8Parser.kt index d2a93fde7..521983e53 100644 --- a/compiler/test/ast/TestProg8Parser.kt +++ b/compiler/test/ast/TestProg8Parser.kt @@ -17,15 +17,11 @@ import prog8.ast.Node import prog8.ast.Program import prog8.ast.expressions.* import prog8.ast.statements.* -import prog8.code.core.DataType -import prog8.code.core.Encoding -import prog8.code.core.Position -import prog8.code.core.ZeropageWish +import prog8.code.core.* import prog8.code.target.C64Target import prog8.code.target.cbm.PetsciiEncoding import prog8.parser.ParseError import prog8.parser.Prog8Parser.parseModule -import prog8.code.core.SourceCode import prog8tests.helpers.* import kotlin.io.path.Path import kotlin.io.path.isRegularFile diff --git a/compiler/test/ast/TestSubroutines.kt b/compiler/test/ast/TestSubroutines.kt index 4a4bd3927..45227e62c 100644 --- a/compiler/test/ast/TestSubroutines.kt +++ b/compiler/test/ast/TestSubroutines.kt @@ -5,8 +5,8 @@ import io.kotest.matchers.shouldBe import prog8.ast.statements.Block import prog8.ast.statements.Subroutine import prog8.code.core.DataType -import prog8.parser.Prog8Parser.parseModule import prog8.code.core.SourceCode +import prog8.parser.Prog8Parser.parseModule class TestSubroutines: AnnotationSpec() { diff --git a/compiler/test/ast/TestVarious.kt b/compiler/test/ast/TestVarious.kt index f3eb74adb..e602bfd32 100644 --- a/compiler/test/ast/TestVarious.kt +++ b/compiler/test/ast/TestVarious.kt @@ -6,7 +6,6 @@ import io.kotest.matchers.shouldNotBe import prog8.ast.IFunctionCall import prog8.ast.expressions.IdentifierReference import prog8.ast.expressions.StringLiteral -import prog8.ast.statements.Assignment import prog8.ast.statements.InlineAssembly import prog8.ast.statements.VarDecl import prog8.code.core.Position diff --git a/compiler/test/codegeneration/TestAsmGenSymbols.kt b/compiler/test/codegeneration/TestAsmGenSymbols.kt index 0dbbcb16f..8fdf583d1 100644 --- a/compiler/test/codegeneration/TestAsmGenSymbols.kt +++ b/compiler/test/codegeneration/TestAsmGenSymbols.kt @@ -13,7 +13,6 @@ import prog8.code.target.C64Target import prog8.code.target.c64.C64Zeropage import prog8.codegen.cpu6502.AsmGen import prog8.compiler.astprocessing.SymbolTableMaker -import prog8.code.core.SourceCode import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer import prog8tests.helpers.DummyStringEncoder diff --git a/compilerAst/src/prog8/ast/Program.kt b/compilerAst/src/prog8/ast/Program.kt index 9ca9a1839..8d09c0b11 100644 --- a/compilerAst/src/prog8/ast/Program.kt +++ b/compilerAst/src/prog8/ast/Program.kt @@ -6,7 +6,6 @@ import prog8.ast.expressions.StringLiteral import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor import prog8.code.core.* -import prog8.code.core.SourceCode /*********** Everything starts from here, the Program; zero or more modules *************/ diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 9c6aeb74d..2984c0a4e 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -8,7 +8,6 @@ import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.code.core.* import prog8.parser.Prog8ANTLRParser -import prog8.code.core.SourceCode import java.nio.file.Path import kotlin.io.path.isRegularFile diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 1ab326b15..11338a502 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -1,6 +1,9 @@ package prog8.ast.expressions -import prog8.ast.* +import prog8.ast.IFunctionCall +import prog8.ast.IPipe +import prog8.ast.Node +import prog8.ast.Program import prog8.ast.base.ExpressionError import prog8.ast.base.FatalAstException import prog8.ast.base.UndefinedSymbolError diff --git a/compilerAst/src/prog8/compiler/BuiltinFunctions.kt b/compilerAst/src/prog8/compiler/BuiltinFunctions.kt index 75fd4b0a8..48ecb8614 100644 --- a/compilerAst/src/prog8/compiler/BuiltinFunctions.kt +++ b/compilerAst/src/prog8/compiler/BuiltinFunctions.kt @@ -7,7 +7,10 @@ import prog8.ast.base.SyntaxError import prog8.ast.expressions.* import prog8.ast.statements.VarDecl import prog8.code.core.* -import kotlin.math.* +import kotlin.math.abs +import kotlin.math.floor +import kotlin.math.sign +import kotlin.math.sqrt private typealias ConstExpressionCaller = (args: List, position: Position, program: Program) -> NumericLiteral diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 6e7d30cd4..f7ba6dee7 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,8 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- add McCarthy evaluation to shortcircuit and/or expressions. First do ifs by splitting them up? Then do expressions that compute a value? +- vm: don't use register 0/1 "as convenience" where it's not required, just allocate a new register anyway. + search for reg.\s?=\s?0 ... @@ -19,11 +20,12 @@ Future Things and Ideas ^^^^^^^^^^^^^^^^^^^^^^^ Compiler: +- add McCarthy evaluation to shortcircuit and/or expressions. First do ifs by splitting them up? Then do expressions that compute a value? - vm: codeGen: various TODOs to tweak code - vm: implement remaining sin/cos functions in math.p8 - vm: somehow deal with asmsubs otherwise the vm IR can't fully encode all of prog8 - vm: don't store symbol names in instructions to make optimizing the IR easier? but what about jumps to labels. And it's no longer readable by humans. -- vm: how to remove all unused subroutines? (in the assembly codegen, we let 64tass solve this for us) +- vm: how to remove all unused subroutines? (in the 6502 assembly codegen, we let 64tass solve this for us) - vm: rather than being able to jump to any 'address' (IPTR), use 'blocks' that have entry and exit points -> even better dead code elimination possible too - vm: add more assignments to translateInplaceAssign() - Inliner: also inline function call expressions, and remove it from the StatementOptimizer diff --git a/examples/test.p8 b/examples/test.p8 index 9f11c10cb..84fa138ee 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -17,19 +17,26 @@ other { main { + sub ands(ubyte arg, ubyte b1, ubyte b2, ubyte b3, ubyte b4) -> ubyte { + return arg>b1 and arg>b2 and arg>b3 and arg>b4 + } + + sub ors(ubyte arg, ubyte b1, ubyte b2, ubyte b3, ubyte b4) -> ubyte { + return arg==b1 or arg==b2 or arg==b3 or arg==b4 + } sub start() { - ubyte @shared ix = other.getter() - ix = other.getter() - ix++ - ix = other.getter() - ix++ - ix = other.getter() - ix++ - ix = other.getter() - ix++ - ix = other.getter() - ix++ + ubyte @shared a + ubyte @shared b + + txt.print_ub(ands(10, 2,3,4,5)) + txt.spc() + txt.print_ub(ands(10, 20,3,4,5)) + txt.spc() + txt.print_ub(ors(10, 2,3,40,5)) + txt.spc() + txt.print_ub(ors(10, 1,10,40,5)) + txt.spc() ; ; a "pixelshader": ; sys.gfx_enable(0) ; enable lo res screen diff --git a/virtualmachine/src/prog8/vm/SysCalls.kt b/virtualmachine/src/prog8/vm/SysCalls.kt index ecf73ab62..2f89091fe 100644 --- a/virtualmachine/src/prog8/vm/SysCalls.kt +++ b/virtualmachine/src/prog8/vm/SysCalls.kt @@ -1,6 +1,6 @@ package prog8.vm -import kotlin.math.* +import kotlin.math.min /* SYSCALLS: