optimize codegen for while loops with empty body

This commit is contained in:
Irmen de Jong 2024-12-09 03:21:11 +01:00
parent b2e821755c
commit 26d0a174db
4 changed files with 101 additions and 48 deletions

View File

@ -1172,7 +1172,6 @@ _default_aflow_handler
asmsub set_vsync_irq_handler(uword address @AY) clobbers(A) {
; Sets the VSYNC irq handler to use with enable_irq_handlers(). Also enables VSYNC irqs.
; NOTE: unless a proper irq handler is already running, you should enclose this call in set_irqd() / clear_irqd() to avoid system crashes.
%asm {{
php
sei
@ -1188,7 +1187,6 @@ asmsub set_vsync_irq_handler(uword address @AY) clobbers(A) {
asmsub set_line_irq_handler(uword rasterline @R0, uword address @AY) clobbers(A,Y) {
; Sets the LINE irq handler to use with enable_irq_handlers(), for the given rasterline. Also enables LINE irqs.
; You can use sys.set_rasterline() later to adjust the rasterline on which to trigger.
; NOTE: unless a proper irq handler is already running, you should enclose this call in set_irqd() / clear_irqd() to avoid system crashes.
%asm {{
php
sei
@ -1206,7 +1204,6 @@ asmsub set_line_irq_handler(uword rasterline @R0, uword address @AY) clobbers(A,
asmsub set_sprcol_irq_handler(uword address @AY) clobbers(A) {
; Sets the SPRCOL irq handler to use with enable_irq_handlers(). Also enables SPRCOL irqs.
; NOTE: unless a proper irq handler is already running, you should enclose this call in set_irqd() / clear_irqd() to avoid system crashes.
%asm {{
php
sei
@ -1221,7 +1218,6 @@ asmsub set_sprcol_irq_handler(uword address @AY) clobbers(A) {
asmsub set_aflow_irq_handler(uword address @AY) clobbers(A) {
; Sets the AFLOW irq handler to use with enable_irq_handlers(). Also enables AFLOW irqs.
; NOTE: unless a proper irq handler is already running, you should enclose this call in set_irqd() / clear_irqd() to avoid system crashes.
; NOTE: the handler itself must fill the audio fifo buffer to at least 25% full again (1 KB) or the aflow irq will keep triggering!
%asm {{
php

View File

@ -301,6 +301,17 @@ internal class StatementReorderer(
return noModifications
}
override fun after(whileLoop: WhileLoop, parent: Node): Iterable<IAstModification> {
if(whileLoop.body.isEmpty()) {
// convert while C {} to do {} until not C (because codegen of the latter is more optimized)
val invertedCondition = invertCondition(whileLoop.condition, program)
val until = UntilLoop(whileLoop.body, invertedCondition, whileLoop.position)
return listOf(IAstModification.ReplaceNode(whileLoop, until, parent))
}
return noModifications
}
private fun checkCopyArrayValue(assign: Assignment) {
val identifier = assign.target.identifier!!
val targetVar = identifier.targetVarDecl(program)!!

View File

@ -1,6 +1,17 @@
TODO
====
Halloween is stuck on a black screen
Optimize bitwise operations on word values where only the msb or lsb is touched: (already done for simple expressions!)
cx16.r0 = (cx16.r0 & $a000) | $0055
cx16.r0 = (cx16.r0 | $a000) ^ $0055
cx16.r0 = (cx16.r0 ^ $a000) & $0055
Optimize (x & y) == y expressions especially in loop conditions
update zsmkit to newest version that includes the on_deck routines when stabilized

View File

@ -1,51 +1,86 @@
%import textio
%import palette
%zeropage basicsafe
%option no_sysinit
main {
uword[] @shared colors = [
$000, ; 0 = black
$fff, ; 1 = white
$800, ; 2 = red
$afe, ; 3 = cyan
$c4c, ; 4 = purple
$0c5, ; 5 = green
$00a, ; 6 = blue
$ee7, ; 7 = yellow
$d85, ; 8 = orange
$640, ; 9 = brown
$f77, ; 10 = light red
$333, ; 11 = dark grey
$777, ; 12 = medium grey
$af6, ; 13 = light green
$08f, ; 14 = light blue
$bbb ; 15 = light grey
]
ubyte[] @shared colors_b = [
$00, $00, ; 0 = black
$0f, $ff, ; 1 = white
$08, $00, ; 2 = red
$0a, $fe, ; 3 = cyan
$0c, $4c, ; 4 = purple
$00, $c5, ; 5 = green
$00, $0a, ; 6 = blue
$0e, $e7, ; 7 = yellow
$0d, $85, ; 8 = orange
$06, $40, ; 9 = brown
$0f, $77, ; 10 = light red
$03, $33, ; 11 = dark grey
$07, $77, ; 12 = medium grey
$0a, $f6, ; 13 = light green
$00, $8f, ; 14 = light blue
$0b, $bb ; 15 = light grey
]
sub start() {
palette.set_grayscale(0)
sys.wait(60)
palette.set_rgb_be(colors_b, 16, 0)
while cx16.r0L & 1 == 0 {
}
while cx16.r0L & 1 == 1 {
}
while cx16.r0L & 64 == 0 {
}
while cx16.r0L & 64 == 64 {
}
do { }
until cx16.r0L & 1 == 0
do { }
until cx16.r0L & 1 == 1
do { }
until cx16.r0L & 64 == 0
do { }
until cx16.r0L & 64 == 64
while cx16.r0L & 1 == 0 {
cx16.r0L++
}
while cx16.r0L & 1 == 1 {
cx16.r0L++
}
while cx16.r0L & 64 == 0 {
cx16.r0L++
}
while cx16.r0L & 64 == 64 {
cx16.r0L++
}
do {
cx16.r0L++
}
until cx16.r0L & 1 == 0
do {
cx16.r0L++
}
until cx16.r0L & 1 == 1
do {
cx16.r0L++
}
until cx16.r0L & 64 == 0
do {
cx16.r0L++
}
until cx16.r0L & 64 == 64
/*
sys.set_irqd()
cx16.VERA_IEN = 1 ; only vsync irqs
repeat {
while (cx16.VERA_ISR & 1)==0 {
; wait for vsync
}
cx16.VERA_ISR = 1 ; clear vsync irq status
palette.set_color(6, $ff0)
repeat 2000 {
cx16.r0++
}
palette.set_color(6, $00f)
}
*/
}
}