diff --git a/codeCore/src/prog8/code/ast/AstPrinter.kt b/codeCore/src/prog8/code/ast/AstPrinter.kt index a1d54844b..bf3e07184 100644 --- a/codeCore/src/prog8/code/ast/AstPrinter.kt +++ b/codeCore/src/prog8/code/ast/AstPrinter.kt @@ -53,8 +53,6 @@ fun printAst(root: PtNode, skipLibraries: Boolean, output: (text: String) -> Uni "goto ${node.identifier.name}" else if(node.address!=null) "goto ${node.address.toHex()}" - else if(node.generatedLabel!=null) - "goto ${node.generatedLabel}" else "???" } diff --git a/codeCore/src/prog8/code/ast/AstStatements.kt b/codeCore/src/prog8/code/ast/AstStatements.kt index 593afd0ee..ee0570dd7 100644 --- a/codeCore/src/prog8/code/ast/AstStatements.kt +++ b/codeCore/src/prog8/code/ast/AstStatements.kt @@ -101,9 +101,8 @@ class PtIfElse(position: Position) : PtNode(position) { } -class PtJump(val identifier: PtIdentifier?, +class PtJump(val identifier: PtIdentifier?, // note: even ad-hoc labels are wrapped as an Identifier to simplify code. Just use dummy type and position. val address: UInt?, - val generatedLabel: String?, // TODO remove this ? always uses identifier... position: Position) : PtNode(position) { init { identifier?.let {it.parent = this } diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index d8aa4b11e..295d6c6fc 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -64,23 +64,10 @@ class AsmGen6502(val prefixSymbols: Boolean): ICodeGeneratorBackend { } } is PtJump -> { - if(node.identifier!=null) { - val stNode = st.lookup(node.identifier!!.name) - if(stNode==null) - throw AssemblyError("name not found ${node.identifier}") - if(stNode.astNode.definingBlock()?.noSymbolPrefixing!=true) { - val index = node.parent.children.indexOf(node) - nodesToPrefix += node.parent to index - } - } - else if(node.generatedLabel!=null) { - val stNode = st.lookup(node.generatedLabel!!) - if(stNode==null) - throw AssemblyError("name not found ${node.generatedLabel}") - if(stNode.astNode.definingBlock()?.noSymbolPrefixing!=true) { - val index = node.parent.children.indexOf(node) - nodesToPrefix += node.parent to index - } + val stNode = st.lookup(node.identifier!!.name) ?: throw AssemblyError("name not found ${node.identifier}") + if(stNode.astNode.definingBlock()?.noSymbolPrefixing!=true) { + val index = node.parent.children.indexOf(node) + nodesToPrefix += node.parent to index } } is PtBlock -> prefixNamedNode(node) @@ -159,13 +146,8 @@ private fun PtVariable.prefix(st: SymbolTable): PtVariable { } private fun PtJump.prefix(parent: PtNode, st: SymbolTable): PtJump { - val jump = if(identifier!=null) { - val prefixedIdent = identifier!!.prefix(this, st) - PtJump(prefixedIdent, address, generatedLabel, position) - } else { - val prefixedLabel = generatedLabel!!.split('.').map {"p8_$it" }.joinToString(".") - PtJump(null, address, prefixedLabel, position) - } + val prefixedIdent = identifier!!.prefix(this, st) + val jump = PtJump(prefixedIdent, address, position) jump.parent = parent return jump } @@ -688,7 +670,6 @@ class AsmGen6502Internal ( if (jump is PtJump) { // jump somewhere if X!=0 val label = when { - jump.generatedLabel!=null -> jump.generatedLabel!! jump.identifier!=null -> asmSymbolName(jump.identifier!!) jump.address!=null -> jump.address!!.toHex() else -> throw AssemblyError("weird jump") @@ -988,7 +969,6 @@ $repeatLabel""") private fun getJumpTarget(jump: PtJump): Pair { val ident = jump.identifier - val label = jump.generatedLabel val addr = jump.address return when { ident!=null -> { @@ -999,7 +979,6 @@ $repeatLabel""") else Pair(asmSymbolName(ident), false) } - label!=null -> Pair(label, false) addr!=null -> Pair(addr.toHex(), false) else -> Pair("????", false) } @@ -1249,7 +1228,6 @@ $repeatLabel""") val rightConstVal = right as? PtNumber val label = when { - jump.generatedLabel!=null -> jump.generatedLabel!! jump.identifier!=null -> asmSymbolName(jump.identifier!!) jump.address!=null -> jump.address!!.toHex() else -> throw AssemblyError("weird jump") diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 19235f17d..01406f053 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -316,7 +316,7 @@ class IRCodeGen( } addInstr(result, branchIns, null) } else { - val label = if(goto.generatedLabel!=null) goto.generatedLabel else goto.identifier!!.name + val label = goto.identifier!!.name val branchIns = when(branch.condition) { BranchCondition.CS -> IRInstruction(Opcode.BSTCS, labelSymbol = label) BranchCondition.CC -> IRInstruction(Opcode.BSTCC, labelSymbol = label) @@ -994,8 +994,6 @@ class IRCodeGen( } it += if (goto.address != null) IRInstruction(gotoOpcode, IRDataType.BYTE, reg1 = compResultReg, immediate = 0, address = goto.address?.toInt()) - else if (goto.generatedLabel != null) - IRInstruction(gotoOpcode, IRDataType.BYTE, reg1 = compResultReg, immediate = 0, labelSymbol = goto.generatedLabel) else IRInstruction(gotoOpcode, IRDataType.BYTE, reg1 = compResultReg, immediate = 0, labelSymbol = goto.identifier!!.name) } @@ -1016,8 +1014,6 @@ class IRCodeGen( private fun branchInstr(goto: PtJump, branchOpcode: Opcode) = if (goto.address != null) IRInstruction(branchOpcode, address = goto.address?.toInt()) - else if (goto.generatedLabel != null) - IRInstruction(branchOpcode, labelSymbol = goto.generatedLabel) else IRInstruction(branchOpcode, labelSymbol = goto.identifier!!.name) @@ -1053,8 +1049,6 @@ class IRCodeGen( } if (goto.address != null) addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = leftTr.resultReg, immediate = 0, address = goto.address?.toInt()), null) - else if (goto.generatedLabel != null) - addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = leftTr.resultReg, immediate = 0, labelSymbol = goto.generatedLabel), null) else addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = leftTr.resultReg, immediate = 0, labelSymbol = goto.identifier!!.name), null) } @@ -1102,8 +1096,6 @@ class IRCodeGen( } if (goto.address != null) addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = firstReg, immediate = number, address = goto.address?.toInt()), null) - else if (goto.generatedLabel != null) - addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = firstReg, immediate = number, labelSymbol = goto.generatedLabel), null) else addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = firstReg, immediate = number, labelSymbol = goto.identifier!!.name), null) } @@ -1161,8 +1153,6 @@ class IRCodeGen( } else { if (goto.address != null) addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = firstReg, reg2 = secondReg, address = goto.address?.toInt()), null) - else if (goto.generatedLabel != null) - addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = firstReg, reg2 = secondReg, labelSymbol = goto.generatedLabel), null) else addInstr(result, IRInstruction(opcode, irDtLeft, reg1 = firstReg, reg2 = secondReg, labelSymbol = goto.identifier!!.name), null) } @@ -1628,9 +1618,7 @@ class IRCodeGen( if(jump.address!=null) { chunk += IRInstruction(Opcode.JUMP, address = jump.address!!.toInt()) } else { - if (jump.generatedLabel != null) - chunk += IRInstruction(Opcode.JUMP, labelSymbol = jump.generatedLabel!!) - else if (jump.identifier != null) { + if (jump.identifier != null) { val symbol = symbolTable.lookup(jump.identifier!!.name) if(symbol?.type==StNodeType.MEMVAR || symbol?.type==StNodeType.STATICVAR) { val jumpReg = registers.nextFree() diff --git a/codeGenIntermediate/test/TestVmCodeGen.kt b/codeGenIntermediate/test/TestVmCodeGen.kt index 4969ad5ac..e72346f18 100644 --- a/codeGenIntermediate/test/TestVmCodeGen.kt +++ b/codeGenIntermediate/test/TestVmCodeGen.kt @@ -251,7 +251,7 @@ class TestVmCodeGen: FunSpec({ cmp1.add(PtIdentifier("main.start.f1", DataType.FLOAT, Position.DUMMY)) cmp1.add(PtNumber(DataType.FLOAT, 42.0, Position.DUMMY)) if1.add(cmp1) - if1.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, null, Position.DUMMY)) }) + if1.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, Position.DUMMY)) }) if1.add(PtNodeGroup()) sub.add(if1) val if2 = PtIfElse(Position.DUMMY) @@ -259,7 +259,7 @@ class TestVmCodeGen: FunSpec({ cmp2.add(PtIdentifier("main.start.f1", DataType.FLOAT, Position.DUMMY)) cmp2.add(PtNumber(DataType.FLOAT, 42.0, Position.DUMMY)) if2.add(cmp2) - if2.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, null, Position.DUMMY)) }) + if2.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, Position.DUMMY)) }) if2.add(PtNodeGroup()) sub.add(if2) block.add(sub) @@ -420,7 +420,7 @@ class TestVmCodeGen: FunSpec({ cmp1.add(PtIdentifier("main.start.ub1", DataType.UBYTE, Position.DUMMY)) cmp1.add(PtNumber(DataType.UBYTE, 42.0, Position.DUMMY)) if1.add(cmp1) - if1.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, null, Position.DUMMY)) }) + if1.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, Position.DUMMY)) }) if1.add(PtNodeGroup()) sub.add(if1) val if2 = PtIfElse(Position.DUMMY) @@ -428,7 +428,7 @@ class TestVmCodeGen: FunSpec({ cmp2.add(PtIdentifier("main.start.ub1", DataType.UBYTE, Position.DUMMY)) cmp2.add(PtNumber(DataType.UBYTE, 42.0, Position.DUMMY)) if2.add(cmp2) - if2.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, null, Position.DUMMY)) }) + if2.add(PtNodeGroup().also { it.add(PtJump(null, 0xc000u, Position.DUMMY)) }) if2.add(PtNodeGroup()) sub.add(if2) block.add(sub) diff --git a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt index 460d672c5..5482e056e 100644 --- a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt @@ -281,7 +281,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr private fun transform(srcIf: IfElse): PtNode { - fun codeForStatusflag(fcall: FunctionCallExpression, flag: Statusflag, equalToZero: Boolean): PtNodeGroup? { + fun codeForStatusflag(fcall: FunctionCallExpression, flag: Statusflag, equalToZero: Boolean): PtNodeGroup { // if the condition is a call to something that returns a boolean in a status register (C, Z, V, N), // a smarter branch is possible using a conditional branch node. val (branchTrue, branchFalse) = if(equalToZero) { @@ -326,7 +326,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr val scopedEndLabel = (srcIf.definingScope.scopedName + endLabel).joinToString(".") val elseLbl = PtIdentifier(scopedElseLabel, DataType.UNDEFINED, srcIf.position) val endLbl = PtIdentifier(scopedEndLabel, DataType.UNDEFINED, srcIf.position) - ifScope.add(PtJump(elseLbl, null, null, srcIf.position)) + ifScope.add(PtJump(elseLbl, null, srcIf.position)) val elseScope = PtNodeGroup() branch.add(ifScope) branch.add(elseScope) @@ -334,7 +334,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr for (stmt in srcIf.truepart.statements) nodes.add(transformStatement(stmt)) if(srcIf.elsepart.isNotEmpty()) - nodes.add(PtJump(endLbl, null, null, srcIf.position)) + nodes.add(PtJump(endLbl, null, srcIf.position)) nodes.add(PtLabel(elseLabel, srcIf.position)) if(srcIf.elsepart.isNotEmpty()) { for (stmt in srcIf.elsepart.statements) @@ -353,9 +353,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr if(fcall!=null) { val returnRegs = fcall.target.targetSubroutine(program)?.asmReturnvaluesRegisters if(returnRegs!=null && returnRegs.size==1 && returnRegs[0].statusflag!=null) { - val translated = codeForStatusflag(fcall, returnRegs[0].statusflag!!, binexpr.operator == "==") - if(translated!=null) - return translated + return codeForStatusflag(fcall, returnRegs[0].statusflag!!, binexpr.operator == "==") } } } @@ -364,9 +362,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr if (fcall != null) { val returnRegs = fcall.target.targetSubroutine(program)?.asmReturnvaluesRegisters if(returnRegs!=null && returnRegs.size==1 && returnRegs[0].statusflag!=null) { - val translated = codeForStatusflag(fcall, returnRegs[0].statusflag!!, false) - if(translated!=null) - return translated + return codeForStatusflag(fcall, returnRegs[0].statusflag!!, false) } } } @@ -391,10 +387,7 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr private fun transform(srcJump: Jump): PtJump { val identifier = if(srcJump.identifier!=null) transform(srcJump.identifier!!) else null - return PtJump(identifier, - srcJump.address, - srcJump.generatedLabel, - srcJump.position) + return PtJump(identifier, srcJump.address, srcJump.position) } private fun transform(label: Label): PtLabel = diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 0bf10297e..4fe2c5e1e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO ==== - optimize: flip if true/else blocks if the else block only contains a jump (invert condition!) -- remove PtJump generatedLabel (always seems to use identifier) - merge branch optimize-st for some optimizations regardign SymbolTable use