mirror of
https://github.com/irmen/prog8.git
synced 2025-02-16 22:30:46 +00:00
tweak while desugaring, moved postfixexpr optimizations to VariousCleanups regardless of optimizer setting because asmgen requires these for conditional expressions
This commit is contained in:
parent
371f084884
commit
9906b58818
@ -54,40 +54,6 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() {
|
|||||||
return mods
|
return mods
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun before(expr: PrefixExpression, parent: Node): Iterable<IAstModification> {
|
|
||||||
if (expr.operator == "+") {
|
|
||||||
// +X --> X
|
|
||||||
return listOf(IAstModification.ReplaceNode(expr, expr.expression, parent))
|
|
||||||
} else if (expr.operator == "not") {
|
|
||||||
when(expr.expression) {
|
|
||||||
is PrefixExpression -> {
|
|
||||||
// NOT(NOT(...)) -> ...
|
|
||||||
val pe = expr.expression as PrefixExpression
|
|
||||||
if(pe.operator == "not")
|
|
||||||
return listOf(IAstModification.ReplaceNode(expr, pe.expression, parent))
|
|
||||||
}
|
|
||||||
is BinaryExpression -> {
|
|
||||||
// NOT (xxxx) -> invert the xxxx
|
|
||||||
val be = expr.expression as BinaryExpression
|
|
||||||
val newExpr = when (be.operator) {
|
|
||||||
"<" -> BinaryExpression(be.left, ">=", be.right, be.position)
|
|
||||||
">" -> BinaryExpression(be.left, "<=", be.right, be.position)
|
|
||||||
"<=" -> BinaryExpression(be.left, ">", be.right, be.position)
|
|
||||||
">=" -> BinaryExpression(be.left, "<", be.right, be.position)
|
|
||||||
"==" -> BinaryExpression(be.left, "!=", be.right, be.position)
|
|
||||||
"!=" -> BinaryExpression(be.left, "==", be.right, be.position)
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newExpr != null)
|
|
||||||
return listOf(IAstModification.ReplaceNode(expr, newExpr, parent))
|
|
||||||
}
|
|
||||||
else -> return noModifications
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return noModifications
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
|
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
|
||||||
val leftVal = expr.left.constValue(program)
|
val leftVal = expr.left.constValue(program)
|
||||||
val rightVal = expr.right.constValue(program)
|
val rightVal = expr.right.constValue(program)
|
||||||
|
@ -265,10 +265,10 @@ private fun processAst(program: Program, errors: IErrorReporter, compilerOptions
|
|||||||
program.charLiteralsToUByteLiterals(compilerOptions.compTarget)
|
program.charLiteralsToUByteLiterals(compilerOptions.compTarget)
|
||||||
program.constantFold(errors, compilerOptions.compTarget)
|
program.constantFold(errors, compilerOptions.compTarget)
|
||||||
errors.report()
|
errors.report()
|
||||||
program.reorderStatements(errors, compilerOptions)
|
|
||||||
errors.report()
|
|
||||||
program.desugaring(errors)
|
program.desugaring(errors)
|
||||||
errors.report()
|
errors.report()
|
||||||
|
program.reorderStatements(errors, compilerOptions)
|
||||||
|
errors.report()
|
||||||
program.addTypecasts(errors, compilerOptions)
|
program.addTypecasts(errors, compilerOptions)
|
||||||
errors.report()
|
errors.report()
|
||||||
program.variousCleanups(program, errors)
|
program.variousCleanups(program, errors)
|
||||||
|
@ -92,24 +92,25 @@ if not CONDITION
|
|||||||
/*
|
/*
|
||||||
while CONDITION { STUFF }
|
while CONDITION { STUFF }
|
||||||
==>
|
==>
|
||||||
goto _whilecond
|
|
||||||
_whileloop:
|
_whileloop:
|
||||||
|
if NOT CONDITION goto _after
|
||||||
STUFF
|
STUFF
|
||||||
_whilecond:
|
goto _whileloop
|
||||||
if CONDITION goto _whileloop
|
_after:
|
||||||
*/
|
*/
|
||||||
val pos = whileLoop.position
|
val pos = whileLoop.position
|
||||||
val condLabel = makeLabel("whilecond", pos)
|
|
||||||
val loopLabel = makeLabel("whileloop", pos)
|
val loopLabel = makeLabel("whileloop", pos)
|
||||||
|
val afterLabel = makeLabel("afterwhile", pos)
|
||||||
|
val notCondition = PrefixExpression("not", whileLoop.condition, pos)
|
||||||
val replacement = AnonymousScope(mutableListOf(
|
val replacement = AnonymousScope(mutableListOf(
|
||||||
jumpLabel(condLabel),
|
|
||||||
loopLabel,
|
loopLabel,
|
||||||
whileLoop.body,
|
IfStatement(notCondition,
|
||||||
condLabel,
|
AnonymousScope(mutableListOf(jumpLabel(afterLabel)), pos),
|
||||||
IfStatement(whileLoop.condition,
|
|
||||||
AnonymousScope(mutableListOf(jumpLabel(loopLabel)), pos),
|
|
||||||
AnonymousScope(mutableListOf(), pos),
|
AnonymousScope(mutableListOf(), pos),
|
||||||
pos)
|
pos),
|
||||||
|
whileLoop.body,
|
||||||
|
jumpLabel(loopLabel),
|
||||||
|
afterLabel
|
||||||
), pos)
|
), pos)
|
||||||
return listOf(IAstModification.ReplaceNode(whileLoop, replacement, parent))
|
return listOf(IAstModification.ReplaceNode(whileLoop, replacement, parent))
|
||||||
}
|
}
|
||||||
|
@ -66,4 +66,37 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter)
|
|||||||
|
|
||||||
return noModifications
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun after(expr: PrefixExpression, parent: Node): Iterable<IAstModification> {
|
||||||
|
if(expr.operator=="+") {
|
||||||
|
// +X --> X
|
||||||
|
return listOf(IAstModification.ReplaceNode(expr, expr.expression, parent))
|
||||||
|
}
|
||||||
|
if(expr.operator=="not") {
|
||||||
|
val nestedPrefix = expr.expression as? PrefixExpression
|
||||||
|
if(nestedPrefix!=null && nestedPrefix.operator=="not") {
|
||||||
|
// NOT NOT X --> X
|
||||||
|
return listOf(IAstModification.ReplaceNode(expr, nestedPrefix.expression, parent))
|
||||||
|
}
|
||||||
|
val comparison = expr.expression as? BinaryExpression
|
||||||
|
if (comparison != null) {
|
||||||
|
// NOT COMPARISON ==> inverted COMPARISON
|
||||||
|
val invertedOperator =
|
||||||
|
when (comparison.operator) {
|
||||||
|
"==" -> "!="
|
||||||
|
"!=" -> "=="
|
||||||
|
"<" -> ">="
|
||||||
|
">" -> "<="
|
||||||
|
"<=" -> ">"
|
||||||
|
">=" -> "<"
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
if (invertedOperator != null) {
|
||||||
|
comparison.operator = invertedOperator
|
||||||
|
return listOf(IAstModification.ReplaceNode(expr, comparison, parent))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return noModifications
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ TODO
|
|||||||
|
|
||||||
For next compiler release (7.6)
|
For next compiler release (7.6)
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
change desugar of while loop so that no initial jump is required
|
|
||||||
optimize ifs containing only a jump: reuse old code from AsmGen translateComparisonExpressionWithJumpIfFalse?
|
optimize ifs containing only a jump: reuse old code from AsmGen translateComparisonExpressionWithJumpIfFalse?
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user