fix IR repeat loop codegen when amount is 0

This commit is contained in:
Irmen de Jong
2022-09-25 20:21:35 +02:00
parent 387a4b7c35
commit 1a56743bb1
9 changed files with 101 additions and 59 deletions

View File

@@ -915,15 +915,18 @@ class IRCodeGen(
} }
} }
val repeatLabel = createLabelName()
val skipRepeatLabel = createLabelName()
val code = IRCodeChunk(repeat.position) val code = IRCodeChunk(repeat.position)
val counterReg = vmRegisters.nextFree() val counterReg = vmRegisters.nextFree()
val vmDt = vmType(repeat.count.type) val vmDt = vmType(repeat.count.type)
code += expressionEval.translateExpression(repeat.count, counterReg, -1) code += expressionEval.translateExpression(repeat.count, counterReg, -1)
val repeatLabel = createLabelName() code += IRCodeInstruction(Opcode.BZ, vmDt, reg1=counterReg, labelSymbol = skipRepeatLabel)
code += IRCodeLabel(repeatLabel) code += IRCodeLabel(repeatLabel)
code += translateNode(repeat.statements) code += translateNode(repeat.statements)
code += IRCodeInstruction(Opcode.DEC, vmDt, reg1=counterReg) code += IRCodeInstruction(Opcode.DEC, vmDt, reg1=counterReg)
code += IRCodeInstruction(Opcode.BNZ, vmDt, reg1=counterReg, labelSymbol = repeatLabel) code += IRCodeInstruction(Opcode.BNZ, vmDt, reg1=counterReg, labelSymbol = repeatLabel)
code += IRCodeLabel(skipRepeatLabel)
return code return code
} }

View File

@@ -120,4 +120,19 @@ sub input_chars (uword buffer) -> ubyte {
}} }}
} }
sub plot (ubyte col, ubyte row) {
; use ANSI escape sequence to position the cursor
txt.chrout(27)
txt.chrout('[')
txt.print_ub(row)
txt.chrout(';')
txt.print_ub(col)
txt.chrout('H')
}
sub setchr (ubyte col, ubyte row, ubyte char) {
plot(col, row)
txt.chrout(char)
}
} }

View File

@@ -3,8 +3,6 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- fix examples/vm/textelite.p8 having wrong randomization? (starts with wrong planet)
... ...

View File

@@ -5,28 +5,65 @@ main {
sub start() { sub start() {
uword[] words = [1111,2222,0,4444,3333] ubyte v1 = 1
uword v2 = 1
txt.print_ub(all(words)) ubyte counterb
txt.nl() uword counter
txt.print_ub(any(words))
txt.nl()
sort(words)
uword ww repeat v1-1 {
for ww in words { txt.print("!")
txt.print_uw(ww)
txt.spc()
} }
txt.nl()
reverse(words) repeat v2-1 {
for ww in words { txt.print("?")
txt.print_uw(ww)
txt.spc()
} }
txt.nl()
for counterb in 0 to v1 {
txt.print("y1")
}
for counter in 0 to v2 {
txt.print("y2")
}
repeat v1 {
txt.print("ok1")
}
repeat v2 {
txt.print("ok2")
}
repeat v1-1 {
txt.print("!")
}
repeat v2-1 {
txt.print("?")
}
while v1-1 {
txt.print("%")
}
while v2-1 {
txt.print("*")
}
for counterb in 0 to v1-1 {
txt.print("@")
}
for counter in 0 to v2-1 {
txt.print("y#")
}
repeat 0 {
txt.print("zero1")
}
repeat $0000 {
txt.print("zero2")
}
} }
} }

View File

@@ -22,6 +22,7 @@ main {
txt.lowercase() txt.lowercase()
txt.print("\u000c\n --- TextElite v1.2 ---\n") txt.print("\u000c\n --- TextElite v1.2 ---\n")
planet.set_seed(0, 0)
galaxy.travel_to(1, numforLave) galaxy.travel_to(1, numforLave)
market.init(0) ; Lave's market is seeded with 0 market.init(0) ; Lave's market is seeded with 0
ship.init() ship.init()

View File

@@ -1,6 +1,3 @@
%import textio
%zeropage basicsafe
; NOTE: meant to test to virtual machine output target (use -target virtual) ; NOTE: meant to test to virtual machine output target (use -target virtual)
main { main {
@@ -24,6 +21,7 @@ main {
repeat { repeat {
fade() fade()
plot_particles() plot_particles()
sys.wait(1)
sys.waitvsync() sys.waitvsync()
} }

View File

@@ -1,6 +1,3 @@
%import textio
%zeropage basicsafe
; NOTE: meant to test to virtual machine output target (use -target virtual) ; NOTE: meant to test to virtual machine output target (use -target virtual)
main { main {
@@ -23,6 +20,9 @@ main {
yy++ yy++
} }
shifter+=4 shifter+=4
sys.wait(1)
sys.waitvsync()
} }
} }
} }

View File

@@ -1,5 +1,4 @@
%import math %import math
%import textio
; Draw sine and cosine graphs. The sine and cosine functions are table lookups ; Draw sine and cosine graphs. The sine and cosine functions are table lookups
; where the tables are generated by 64tass list functions. ; where the tables are generated by 64tass list functions.
@@ -27,8 +26,7 @@ main {
sys.gfx_clear(0) sys.gfx_clear(0)
circles() circles()
repeat { sys.wait(120)
}
} }
sub sincos255() { sub sincos255() {

View File

@@ -14,9 +14,10 @@ main {
sub start() { sub start() {
txt.lowercase() txt.lowercase()
txt.print("--- TextElite v1.2 ---\n") txt.print("\n--- TextElite v1.2 ---\n")
txt.print("VirtualMachine edition: no disk saving, bad text layout!\n") txt.print("VirtualMachine edition: no disk saving, bad market table layout!\n")
planet.set_seed(0, 0)
galaxy.travel_to(1, numforLave) galaxy.travel_to(1, numforLave)
market.init(0) ; Lave's market is seeded with 0 market.init(0) ; Lave's market is seeded with 0
ship.init() ship.init()
@@ -435,11 +436,10 @@ galaxy {
ubyte py = planet.y ubyte py = planet.y
uword current_name = &planet.name uword current_name = &planet.name
ubyte pn = 0 ubyte pn = 0
uword scaling = 8 uword scaling = 2
if local if local
scaling = 2 scaling = 1
scaling /= 2
init(number) init(number)
txt.clear_screen() txt.clear_screen()
txt.print("Galaxy #") txt.print("Galaxy #")
@@ -457,7 +457,7 @@ galaxy {
ubyte distance = planet.distance(px, py) ubyte distance = planet.distance(px, py)
if distance <= max_distance { if distance <= max_distance {
planet.name = make_current_planet_name() planet.name = make_current_planet_name()
planet.name[0] |= 32 ; uppercase first letter planet.name[0] = string.upperchar(planet.name[0])
uword tx = planet.x uword tx = planet.x
uword ty = planet.y uword ty = planet.y
if local { if local {
@@ -465,27 +465,30 @@ galaxy {
ty = ty + 24 - py ty = ty + 24 - py
} }
tx /= scaling tx /= scaling
ty /= scaling*2 ty /= scaling*4
ubyte sx = lsb(tx) ubyte sx = lsb(tx)
ubyte sy = lsb(ty) ubyte sy = lsb(ty)
ubyte char = '*' ubyte char = '*'
if planet.number==current_planet if planet.number==current_planet
char = '%' char = '%'
if local or planet.number==current_planet { if local or planet.number==current_planet {
;; NOT SUPPORTED txt.plot(2+sx-2, 2+sy+1) txt.plot(2+sx-2, 2+sy+1)
txt.print(current_name) txt.print(current_name)
if distance { if distance {
;; NOT SUPPORTED txt.plot(2+sx-2, 2+sy+2) txt.plot(2+sx-2, 2+sy+2)
util.print_10s(distance) util.print_10s(distance)
txt.print(" LY") txt.print(" LY")
} }
} }
;; NOT SUPPROTED txt.setchr(2+sx, 2+sy, char) txt.setchr(2+sx, 2+sy, char)
} }
pn++ pn++
} until pn==0 } until pn==0
;; NOT SUPPORTED txt.plot(0,36) if local
txt.plot(0, 20)
else
txt.plot(0, 33)
txt.nl() txt.nl()
travel_to(number, current_planet) travel_to(number, current_planet)
@@ -603,18 +606,6 @@ galaxy {
rol(xl) rol(xl)
return mkword(xh, xl) return mkword(xh, xl)
} }
sub debug_seed() {
txt.print("\ngalaxy #")
txt.print_ub(number)
txt.print("\ngalaxy seed0=")
txt.print_uwhex(galaxy.seed[0], true)
txt.print("\ngalaxy seed1=")
txt.print_uwhex(galaxy.seed[1], true)
txt.print("\ngalaxy seed2=")
txt.print_uwhex(galaxy.seed[2], true)
txt.nl()
}
} }
planet { planet {
@@ -723,7 +714,7 @@ planet {
} }
} }
randname[nx] = 0 randname[nx] = 0
randname[0] |= 32 ; uppercase first letter randname[0] = string.upperchar(randname[0])
return randname return randname
} }
@@ -795,12 +786,12 @@ planet {
source_ptr = source_stack[stack_ptr] source_ptr = source_stack[stack_ptr]
} else { } else {
if c == $b0 { if c == $b0 {
@(result_ptr) = name[0] | 32 @(result_ptr) = string.upperchar(name[0])
result_ptr++ result_ptr++
concat_string(&name + 1) concat_string(&name + 1)
} }
else if c == $b1 { else if c == $b1 {
@(result_ptr) = name[0] | 32 @(result_ptr) = string.upperchar(name[0])
result_ptr++ result_ptr++
ubyte ni ubyte ni
for ni in 1 to len(name) { for ni in 1 to len(name) {
@@ -914,9 +905,10 @@ planet {
} }
sub print_name_uppercase() { sub print_name_uppercase() {
ubyte c str uppername = "????????"
for c in name uppername = name
txt.chrout(c | 32) void string.upper(uppername)
txt.print(uppername)
} }
sub getword(ubyte listnum, ubyte wordidx) -> uword { sub getword(ubyte listnum, ubyte wordidx) -> uword {
@@ -933,8 +925,8 @@ util {
if pc == 0 if pc == 0
return true return true
; to lowercase for case insensitive compare: ; to lowercase for case insensitive compare:
pc &= 127 pc = string.lowerchar(pc)
sc &= 127 sc = string.lowerchar(sc)
if pc != sc if pc != sc
return false return false
prefixptr++ prefixptr++