mirror of
https://github.com/irmen/prog8.git
synced 2026-10-23 12:02:29 +00:00
Compare commits
17
Commits
v12.2-beta3
...
v12.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2d1dffbe0 | ||
|
|
4008280a62 | ||
|
|
2e7235ec02 | ||
|
|
a4330f5048 | ||
|
|
f65aeff231 | ||
|
|
505243aff7 | ||
|
|
d0f2fc13ba | ||
|
|
53d51dcd38 | ||
|
|
c1aa0e49fe | ||
|
|
e1e72d4317 | ||
|
|
feec343cbc | ||
|
|
5cdc4233f3 | ||
|
|
1ae89d18cc | ||
|
|
eb19d00a1e | ||
|
|
e01ee10ef0 | ||
|
|
f4b3cb12e1 | ||
|
|
77ced60ad6 |
No files matched your search
@@ -9,6 +9,9 @@ compatibility: opencode
|
||||
|
||||
You are writing **6502/65C02 assembly** using **64tass syntax**, in separate `*.asm` files or embedded in a Prog8 program (inside `%asm {{ }}` blocks or `asmsub` routines). Follow all rules below.
|
||||
|
||||
## Git Operations
|
||||
- When moving, renaming, or deleting git-tracked files, **always use `git mv` or `git rm`** instead of plain `mv`/`rm`. This preserves history and properly stages the change. Plain `mv`/`rm` causes git to see them as delete+add (losing history).
|
||||
|
||||
## Debugging Generated Assembly
|
||||
- The prog8 compiler outputs `*.asm` (assembly source) and (with -asmlist option) also `*.list` (full listing with address/symbols) when compiling. Inspect these to debug generated code, verify optimizations, and trace instruction sequences.
|
||||
|
||||
@@ -165,3 +168,8 @@ loop ; do work here
|
||||
lda p8v_myword ; loads LSB
|
||||
ldy p8v_myword+1 ; loads MSB (word variables are stored LSB-first)
|
||||
```
|
||||
|
||||
## IRQ Handler Best Practices
|
||||
- Keep handlers extremely short and fast — they run with interrupts disabled and steal cycles from the main program
|
||||
- Do NOT do lengthy processing, I/O, or complex subroutine calls inside the handler
|
||||
- Instead, set a boolean flag or semaphore that the main loop checks periodically, and do the actual work there
|
||||
@@ -12,6 +12,7 @@ You are working with **Prog8** source code (`.p8` files) or its Intermediate Rep
|
||||
Follow ALL the rules below carefully.
|
||||
|
||||
## General & Setup
|
||||
- **Git operations**: When moving, renaming, or deleting git-tracked files, **always use `git mv` or `git rm`** instead of plain `mv`/`rm`. This preserves history and properly stages the change. Plain `mv`/`rm` causes git to see them as delete+add (losing history).
|
||||
- A program = a `main` block containing a `start` subroutine entry point, plus optional other subroutines/blocks
|
||||
- Add `%zeropage basicsafe` at the top of your program to allow clean return on exit (instead of resetting the machine/emulator)
|
||||
- Module imports: `%import modulename` — no `as` aliasing. Use the module's defined prefix (e.g., `%import textio` → `txt.xxx`)
|
||||
@@ -20,8 +21,10 @@ Follow ALL the rules below carefully.
|
||||
- **Output directory**: `prog8c -out outdir input.p8` (default: same dir as source)
|
||||
- **Compilation outputs**: `*.prg` (program binary), `*.asm` (assembly listing), `*.list` (full listing), `*.p8ir` (IR for VM), `*.vice-mon-list` (Vice debug symbols)
|
||||
- `.p8ir` files contain the **Intermediate Representation** — a sequence of named chunks, each containing typed instructions and virtual registers. This is a target-independent representation of the program, executable by the built-in Virtual Machine via `prog8c -vm file.p8ir`. Useful for debugging the compiler's code generation path without involving 6502 assembly.
|
||||
- **Debugging switches**: `-noopt` (disable optimizations), `-printast1` (parsed AST), `-printast2` (optimized simple AST), `-compareir` (compare IR outputs)
|
||||
- **TODO list**: Check `docs/source/todo.rst` to see what features are NOT yet implemented
|
||||
- **Debugging switches**: `-noopt` (disable optimizations), `-printast1` (parsed AST), `-printast2` (optimized simple AST), `-compareir` (compare IR outputs), `-dumpsymbols` (print all symbols), `-dumpvars` (print all variables)
|
||||
- **Library search (preferred)**: `prog8c -libsearch <regex>` — search for a regex pattern in the embedded library files. Extremely useful to quickly find library routines, variables, strings, or signatures (e.g., `prog8c -libsearch "txt\."` lists all textio routines; `prog8c -libsearch "sin"` finds math functions)
|
||||
- **Library dump**: `prog8c -libdump <dir>` — extract all embedded library source files into a directory for direct inspection (less common, use `-libsearch` first)
|
||||
- **Other useful flags**: `-quiet` (suppress messages), `-warnimplicitcasts` (warn on implicit type widening), `-daemon` (keep a background compiler process alive to speed up multiple successive compilations — must be passed on every `prog8c` invocation)
|
||||
- **Test programs**: add `%zeropage basicsafe` and `%option no_sysinit` at top
|
||||
- **`sys` module**: always available, no import needed
|
||||
- **CX16 debugging**: Add `%encoding iso`, call `txt.iso()` in `start()`, end with `sys.poweroff_system()`. For emulator: `x16emu -echo iso -run -prg input.prg 2>&1 | grep ...`
|
||||
@@ -32,17 +35,31 @@ Follow ALL the rules below carefully.
|
||||
- **float requires `%import floats`** at top of file, else compiler errors
|
||||
- Arrays: max 256 bytes (512 for split word arrays). For larger data, use `memory()` + pointers
|
||||
- `memory(name, size)` returns a `uword` address to a statically reserved block of memory
|
||||
- Struct initialization: `^^StructType ptr = ^^StructType:[val1,val2,...]` (the `^^StructType:` can be omitted if inferable)
|
||||
- To point a typed pointer at a `memory()` block, assign the uword directly: `^^MyStruct ptr = memory("name", size)`. The `^^Type:expression` syntax (below) only works with array literals, not general uword expressions.
|
||||
- Struct initialization: `^^StructType ptr = ^^StructType:[val1,val2,...]` (the `^^StructType:` can be omitted if inferable). This `^^Type:[...]` syntax does NOT work with variables or `memory()` — only literal arrays.
|
||||
- Struct definitions must be inside a block, not at file level.
|
||||
- Struct fields: only simple types + `str` allowed. NO arrays as fields. `str` in struct = `^^ubyte`
|
||||
- Word/pointer arrays split into LSB/MSB by default. Override with `@nosplit`
|
||||
- **No call stack**: all variables statically allocated. No recursion without manual stack management
|
||||
- Variables zero-initialized (globals at start, locals on subroutine entry)
|
||||
- `@shared` marks variables as "used by external code" (assembly), prevents optimization
|
||||
- `@shared`, `@zp`, `@requirezp`, `@dirty`, `@nosplit` are **tags** that go on variable declarations. **Place them after the datatype (and array specifiers), before the variable name(s):**
|
||||
```
|
||||
private ubyte[8] @shared vera_storage ; single var with tag
|
||||
ubyte @zp @shared varname ; multiple tags allowed
|
||||
ubyte @requirezp var1 ; require zeropage
|
||||
ubyte[] @shared names ; array with tag
|
||||
```
|
||||
The grammar is: `[private] datatype [arraydims] [tags...] identifierlist`
|
||||
- `@shared` marks variables as "used by external code" (assembly), prevents the optimizer from removing them
|
||||
- `@zp`/`@requirezp`: use sparingly — only for pointers (limited zeropage space)
|
||||
- Pointer-like typed pointers (`^^type`) support C-style scaled arithmetic; `uword` pointers always treat element as 1 byte
|
||||
- `&` = untyped address (uword); `&&` = typed pointer
|
||||
- Available zeropage scratch: `P8ZP_SCRATCH_B1`, `P8ZP_SCRATCH_REG`, `P8ZP_SCRATCH_W1`, `P8ZP_SCRATCH_W2`, `P8ZP_SCRATCH_PTR` — and cx16 virtual registers R0-R15 on all targets
|
||||
- Virtual registers (`cx16.r0`–`cx16.r15`): global 16-bit, NOT preserved across calls. R12-R15 may be clobbered by long ops. Save/restore in IRQs with `cx16.save_virtual_registers()`/`cx16.restore_virtual_registers()`
|
||||
- Virtual registers (`cx16.r0`–`cx16.r15`): global 16-bit, NOT preserved across calls.
|
||||
- **WARNING: Virtual registers in ISR/IRQ handlers**: The virtual registers R0-R15 are *not preserved* across the IRQ handler call. If your handler uses them, it will corrupt the interrupted program's state. Either avoid using them in the handler, or save/restore with `cx16.save_virtual_registers()` / `cx16.restore_virtual_registers()`. This applies to all targets, not just CX16.
|
||||
- **WARNING: Long operations clobber R12-R15**: Some operations on `long` values use R12-R15 as temporary storage and will silently overwrite them. Do not rely on R12-R15 values when working with longs, and avoid using R12-R15 explicitly if your code uses long arithmetic.
|
||||
- **WARNING: VERA registers in ISR handlers (CX16)**: If your IRQ handler reads or writes VERA control registers (e.g., `cx16.VERA_DATA0`, `cx16.VERA_ADDR_L`, etc.), you must save and restore the VERA context around the handler's work using `cx16.save_vera_context()` / `cx16.restore_vera_context()`. Without this, the handler will corrupt any VERA operations (tilemap updates, sprite positioning, etc.) happening in the interrupted main program.
|
||||
- **IRQ handler best practices**: Keep handlers extremely short and fast — they run with interrupts disabled and steal cycles from the main program. Do NOT do lengthy processing, I/O, or complex subroutine calls inside the handler. Instead, set a boolean flag or semaphore that the main loop checks periodically, and do the actual work there.
|
||||
- Math performance: integer trig (`math.sin8`, `math.cos8`) uses fast LUTs; float trig (`floats.sin`/`cos`) is much slower
|
||||
|
||||
## Recursion & Stack Management
|
||||
@@ -62,6 +79,7 @@ No call stack for variable storage — recursion overwrites locals. To handle it
|
||||
- String concat is expensive on 6502. Prefer separate prints over concatenation
|
||||
- **Efficient buffer iteration**: prefer `ptr++` + `@(ptr)` over `@(buffer + offset)`. Exception: if offset is a `ubyte` (≤ 255), `buffer[offset]` works fine
|
||||
- `@(ptr)` = peek/poke byte. For words: `peekw`/`pokew`, longs: `peekl`/`pokel`, floats: `peekf`/`pokef`, bools: `peekbool`/`pokebool`
|
||||
- Assigning a uword address to a `str` field of a struct is done by direct assignment. Compute the address in a uword variable first, then assign: `cx16.r0 = &namebufs + offset; entry.name = cx16.r0`. The `^^ubyte:(expr)` cast syntax does not parse — use a temp uword instead.
|
||||
- Use `len(array)` instead of hardcoded sizes
|
||||
- Array indexing is 0-based: `arr[0]` is first element
|
||||
- Static memory only — real dynamic allocation impossible, but can emulate with a simple arena allocator over a `memory()` slab
|
||||
@@ -81,7 +99,7 @@ No call stack for variable storage — recursion overwrites locals. To handle it
|
||||
if x < 5
|
||||
txt.print("small")
|
||||
```
|
||||
- `defer` defers statement execution until scope exit
|
||||
- `defer` defers statement execution until scope exit. Multiple defers fire in **reverse registration order** (LIFO / stack order — last deferred runs first). A defer is only registered if execution reaches that statement — conditional paths that skip the `defer` line will not register it.
|
||||
- `goto`, labels, jump lists allowed
|
||||
- **Common mistake**: `and`/`or` for bitmasking — use `&`/`|` instead!
|
||||
|
||||
@@ -183,7 +201,6 @@ For low-level assembly that gets arguments via registers and returns values in r
|
||||
- **Clobbers**: `clobbers (A, X, Y)` — list all hardware registers modified by the routine.
|
||||
- **Parameter names** are for documentation and type checking only. Use the registers in your assembly code.
|
||||
- **Inlining**: `inline asmsub` will paste the assembly code directly at the call site, avoiding `jsr`/`rts` overhead.
|
||||
- **Symbol prefixes** in assembly: `p8v_` (vars), `p8s_` (subs), `p8b_` (blocks), etc.
|
||||
|
||||
## External Subroutines (extsub)
|
||||
Used to call routines at fixed memory addresses (like ROM KERNAL routines or third-party drivers).
|
||||
@@ -239,7 +256,6 @@ Used to call routines at fixed memory addresses (like ROM KERNAL routines or thi
|
||||
- **No `elif`**: use nested `else { if ... }`
|
||||
- Type casting: `expression as type` (e.g., `bytevar as word`). `as` has very low precedence (lower than arithmetic)
|
||||
- **No automatic type widening**: `byte*byte=byte` (overflow possible!). Cast explicitly
|
||||
- **No block scope**: `for`/`if` don't introduce scope. Only subroutines do. Variables in blocks are hoisted to subroutine level
|
||||
- **No bare `{ }` blocks** like C/Java
|
||||
- Indentation: 4 spaces for .p8 and .asm files (no tabs)
|
||||
- Character encoding: 6502 targets use PETSCII by default (call `txt.lowercase()` at start for lowercase). Virtual target uses ISO (`%encoding iso` + `txt.iso()`)
|
||||
@@ -248,6 +264,3 @@ Used to call routines at fixed memory addresses (like ROM KERNAL routines or thi
|
||||
- Avoid `globals.XXXX` — move constants closer to where they're used
|
||||
- Member access through pointers: use `.` for both direct and pointer access. The compiler infers the type. For complex assignment targets, `^^` may be needed: `ptr^^.field = value`
|
||||
- Qualified names: must use full path from top level (e.g., `cx16.r0`, not relative)
|
||||
- **No block scope** — variables in `if`/`for`/`repeat` blocks are hoisted to subroutine level
|
||||
- **No reassigning strings** after declaration — use `strings.copy()`/`strings.ncopy()`
|
||||
- **Pre-allocate string buffers** — `""` allocates nothing
|
||||
+1
-1
@@ -5,6 +5,7 @@
|
||||
.idea/shelf/
|
||||
.junie/
|
||||
.junie/memory/
|
||||
.serena
|
||||
build/
|
||||
dist/
|
||||
output/
|
||||
@@ -38,7 +39,6 @@ parsetab.py
|
||||
.pytest_cache/
|
||||
.attach_pid*
|
||||
compiler/lib/
|
||||
|
||||
.gradle
|
||||
sd*.img
|
||||
*.d64
|
||||
|
||||
@@ -12,9 +12,9 @@ endif
|
||||
|
||||
all:
|
||||
$(GRADLE) installdist installshadowdist
|
||||
@echo "compiler launch script can be found here: compiler/build/install/compiler-shadow/bin/prog8c"
|
||||
@echo "compiler launch script can be found here: compiler/build/install/prog8c-shadow/bin/prog8c"
|
||||
|
||||
test:
|
||||
$(GRADLE) build
|
||||
@echo "compiler launch script can be found here: compiler/build/install/compiler-shadow/bin/prog8c"
|
||||
@echo "compiler launch script can be found here: compiler/build/install/prog8c-shadow/bin/prog8c"
|
||||
|
||||
@@ -15,10 +15,12 @@ which aims to provide many conveniences over raw assembly code (even when using
|
||||
|
||||
**Want to buy me a coffee or a pizza perhaps?**
|
||||
|
||||
This project was created over the last couple of years by dedicating thousands of hours of my free time to it, to make it the best I possibly can.
|
||||
This project was created over the last couple of years by dedicating a huge amoung of my free time to it, to make it the best I possibly can.
|
||||
If you like Prog8, and think it's worth a nice cup of hot coffee or a delicious pizza,
|
||||
you can help me out a little bit over at [ko-fi.com/irmen](https://ko-fi.com/irmen) or [PayPal](https://paypal.me/irmendejong)
|
||||
|
||||
[](https://jb.gg/OpenSource)
|
||||
I am using JetBrains products to develop the Prog8 compiler.
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
|
||||
@@ -13,9 +13,10 @@ import kotlin.io.path.Path
|
||||
|
||||
object ImportFileSystem {
|
||||
|
||||
val userHome: Path get() = Path.of(System.getProperty("user.home"))
|
||||
|
||||
fun expandTilde(path: String): String = if (path.startsWith("~")) {
|
||||
val userHome = System.getProperty("user.home")
|
||||
userHome + path.drop(1)
|
||||
userHome.toString() + path.drop(1)
|
||||
} else {
|
||||
path
|
||||
}
|
||||
|
||||
@@ -136,9 +136,27 @@ internal class AssemblyProgram(
|
||||
}
|
||||
|
||||
val proc = ProcessBuilder(assemblerCommand)
|
||||
if(!options.quiet)
|
||||
proc.inheritIO()
|
||||
val process = proc.start()
|
||||
.redirectErrorStream(true)
|
||||
|
||||
if (options.quiet) {
|
||||
proc.redirectOutput(ProcessBuilder.Redirect.DISCARD)
|
||||
}
|
||||
|
||||
val process = try {
|
||||
proc.start()
|
||||
} catch (e: Exception) {
|
||||
System.err.println("daemon: assembler failed to start: ${e.message}")
|
||||
return false
|
||||
}
|
||||
|
||||
if (!options.quiet) {
|
||||
process.inputStream.bufferedReader().use { reader ->
|
||||
reader.forEachLine {
|
||||
println(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val result = process.waitFor()
|
||||
if (result == 0) {
|
||||
removeGeneratedLabelsFromMonlist()
|
||||
|
||||
@@ -109,8 +109,10 @@ class Inliner(private val program: Program, private val options: CompilationOpti
|
||||
stmt.target is NumericLiteral
|
||||
}
|
||||
|
||||
if (!subroutine.isAsmSubroutine) {
|
||||
// NOTE: We allow subroutines with ANY number of parameters to be considered for inlining.
|
||||
val hasRegisterAliasedParams = subroutine.parameters.any { it.registerOrPair != null }
|
||||
if (!subroutine.isAsmSubroutine && (subroutine.parameters.isEmpty() || !hasRegisterAliasedParams)) {
|
||||
// NOTE: We allow subroutines with parameters to be considered for inlining,
|
||||
// unless they are assembly subroutines or have register-aliased parameters (which cannot be inlined as they depend on specific CPU registers to be set).
|
||||
// For void calls where the body doesn't use the parameters, inlining is trivial -
|
||||
// we just remove the call entirely (no parameter substitution needed).
|
||||
// For calls that return values or use parameters in the body, we need simple arguments
|
||||
@@ -227,15 +229,12 @@ class Inliner(private val program: Program, private val options: CompilationOpti
|
||||
if (!sub.inline)
|
||||
return false to "subroutine not marked as inlineable"
|
||||
|
||||
// Check parameter argument complexity
|
||||
if (sub.parameters.isNotEmpty()) {
|
||||
if (sub.isAsmSubroutine) {
|
||||
return false to "parameterized asmsub cannot be inlined"
|
||||
}
|
||||
// Only inline if all arguments are simple (literals or identifiers)
|
||||
if (!fcall.args.all { it is NumericLiteral || it is IdentifierReference }) {
|
||||
val complexArgs = fcall.args.filterNot { it is NumericLiteral || it is IdentifierReference }
|
||||
return false to "complex arguments (${complexArgs.size}x): ${complexArgs.map { it::class.simpleName }.joinToString(", ")}"
|
||||
if (sub.parameters.any { it.registerOrPair != null }) {
|
||||
return false to "parameterized subroutine with register aliased parameters cannot be inlined"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,7 +335,7 @@ class Inliner(private val program: Program, private val options: CompilationOpti
|
||||
fun possiblyInlineFunctionBody(toInline: Statement): Iterable<AstModification> {
|
||||
val inlinedStatement = substituteParameters(sub, origNode as IFunctionCall, toInline) as Statement
|
||||
|
||||
// println(">>> INLINER: INLINED '${sub.name}' at ${functionCallStatement.position} (body inserted, ${sub.parameters.size} param(s))")
|
||||
// println(">>> INLINER: INLINED '${sub.name}' at ${functionCallStatement.position} (body inserted)")
|
||||
return if (origNode !== toInline) {
|
||||
sub.hasBeenInlined = true
|
||||
listOf(AstReplaceNode(origNode, inlinedStatement, parent))
|
||||
@@ -344,7 +343,7 @@ class Inliner(private val program: Program, private val options: CompilationOpti
|
||||
noModifications
|
||||
}
|
||||
|
||||
if(sub.inline && sub.parameters.size >= 0) {
|
||||
if(sub.inline) {
|
||||
// Get the first non-parameter statement (skip VarDecl with SUBROUTINEPARAM origin)
|
||||
val bodyStmt = sub.statements.firstOrNull { it !is VarDecl || it.origin != VarDeclOrigin.SUBROUTINEPARAM }
|
||||
val hasOnlyBodyAndReturn = bodyStmt != null && (
|
||||
|
||||
@@ -4,6 +4,23 @@
|
||||
; and then calling the update() routine periodically to apply the changes.
|
||||
; You can read and write all parameters in the Voice structure as desired.
|
||||
; For efficiency reasons, the volume envelope state is not kept in there though.
|
||||
;
|
||||
; Envelope timing (ASR - Attack, Sustain, Release):
|
||||
; The envelope advances by one tick each time update() is called.
|
||||
; The actual real-time duration depends on how often you call update().
|
||||
; If you call it every vsync (60Hz), 1 tick = ~16.7ms.
|
||||
; Attack and Release produce linear volume ramps.
|
||||
; Higher speed = faster ramp (shorter duration).
|
||||
; Timing formula: duration_ticks = ceil(256 / speed)
|
||||
; speed=1 -> 256 ticks (at 60Hz: ~4.3s)
|
||||
; speed=4 -> 64 ticks (at 60Hz: ~1.07s)
|
||||
; speed=64 -> 4 ticks (at 60Hz: ~67ms)
|
||||
; speed=255 -> 1 tick (instant envelope change on next update() call)
|
||||
; Even speed=255 applies on the *next* call to ``update()`` — there is always at least ~1 frame of delay if you depend on the normal update cycle.
|
||||
; For truly immediate changes (0ms), set the voice struct fields and call ``update()`` directly.
|
||||
; Sustain holds for speed ticks (0-255) then transitions to release.
|
||||
; If speed=0 the envelope stalls at that phase (no progression - useful for infinite sustain).
|
||||
; The envelope volume is tracked internally as 8.8 fixed point.
|
||||
|
||||
; Vera PSG registers:
|
||||
; $1F9C0 - $1F9FF 16 blocks of 4 PSG registers (16 voices)
|
||||
@@ -43,21 +60,25 @@ psg2 {
|
||||
ubyte volume ; 0-63, use setvolume() to adjust this if you are also using ADSR envelope
|
||||
ubyte waveform ; PULSE/SQUARE,SAWTOOTH,TRIANGLE,NOISE
|
||||
ubyte pulsewidth ; 0-63
|
||||
uword frequency
|
||||
uword frequency ; direct VERA PSG register value (no scaling): freqword = HERZ(Hz) / 0.3725290298461914
|
||||
}
|
||||
|
||||
|
||||
; envelope parameters are kept in arrays for maximum efficiency
|
||||
ubyte[16] envelope_states
|
||||
ubyte[16] envelope_attacks
|
||||
ubyte[16] envelope_sustains
|
||||
ubyte[16] envelope_releases
|
||||
ubyte[16] envelope_maxvolumes
|
||||
uword[16] envelope_volumes ; 8.8 fixed point (scaled by 256)
|
||||
private ubyte[16] envelope_states
|
||||
private ubyte[16] envelope_attacks
|
||||
private ubyte[16] envelope_sustains
|
||||
private ubyte[16] envelope_releases
|
||||
private ubyte[16] envelope_maxvolumes
|
||||
private uword[16] envelope_volumes ; 8.8 fixed point (scaled by 256)
|
||||
|
||||
; private VERA context save buffer, so we don't clobber the global _vera_storage in syslib
|
||||
; (which is shared with the cx16 module's save_vera_context and would cause a race
|
||||
; condition if update() is interrupted by a VSYNC IRQ handler that also saves VERA context)
|
||||
private ubyte[8] @shared vera_storage
|
||||
|
||||
^^Voice voices = memory("psg2voices", 16*sizeof(Voice), 0) ; can't use array of structs so use mem block
|
||||
^^Voice vptr
|
||||
private ^^Voice vptr
|
||||
|
||||
sub init() {
|
||||
; -- required before using this module; initializes all registers to default (off) values
|
||||
@@ -133,7 +154,7 @@ psg2 {
|
||||
}
|
||||
|
||||
sub update() -> bool {
|
||||
; -- update adsr envelopes, then write all 16 voices to Vera PSG
|
||||
; -- update adsr envelopes, then write all 16 voices to Vera PSG. This call is IRQ-safe!
|
||||
; This has to be called every time you want to apply changed psg voice parameters.
|
||||
; If you want to use real-time volume envelopes (Attack-Sustain-Release),
|
||||
; you have to call this routine every 1/60th second, for example from your vsync irq handler.
|
||||
@@ -155,11 +176,16 @@ psg2 {
|
||||
when envelope_states[voice] {
|
||||
EnvelopeState::ATTACK -> {
|
||||
; while current volume is less than max volume, increase volume by maxvolume * (attackspeed /256.0)
|
||||
; special case: speed=255 jumps directly to max volume (instant)
|
||||
maxvolume = mkword(envelope_maxvolumes[voice], 0)
|
||||
currentvolume = envelope_volumes[voice]
|
||||
if currentvolume < maxvolume {
|
||||
newvolume = currentvolume + (maxvolume >> 8)*envelope_attacks[voice]
|
||||
newvolume = min(newvolume, maxvolume)
|
||||
if envelope_attacks[voice] == 255
|
||||
newvolume = maxvolume
|
||||
else {
|
||||
newvolume = currentvolume + (maxvolume >> 8)*envelope_attacks[voice]
|
||||
newvolume = min(newvolume, maxvolume)
|
||||
}
|
||||
envelope_volumes[voice] = newvolume
|
||||
vptr.volume = msb(newvolume)
|
||||
} else
|
||||
@@ -173,13 +199,18 @@ psg2 {
|
||||
}
|
||||
EnvelopeState::RELEASE -> {
|
||||
; while current volume is not zero, decrease volume by maxvolume * (releasespeed /256.0)
|
||||
; special case: speed=255 jumps directly to zero (instant)
|
||||
currentvolume = envelope_volumes[voice]
|
||||
if currentvolume > 0 {
|
||||
uword subtraction = (currentvolume>>8)*envelope_releases[voice]
|
||||
if subtraction > currentvolume
|
||||
if envelope_releases[voice] == 255
|
||||
newvolume = 0
|
||||
else
|
||||
newvolume = currentvolume - subtraction
|
||||
else {
|
||||
uword subtraction = (envelope_maxvolumes[voice] as uword) * envelope_releases[voice]
|
||||
if subtraction > currentvolume
|
||||
newvolume = 0
|
||||
else
|
||||
newvolume = currentvolume - subtraction
|
||||
}
|
||||
envelope_volumes[voice] = newvolume
|
||||
vptr.volume = msb(newvolume)
|
||||
} else
|
||||
@@ -190,7 +221,7 @@ psg2 {
|
||||
vptr++
|
||||
}
|
||||
|
||||
cx16.save_vera_context()
|
||||
psg2_save_vera_context()
|
||||
cx16.VERA_CTRL = 0
|
||||
cx16.VERA_ADDR = lsw(cx16.VERA_PSG_BASE)
|
||||
cx16.VERA_ADDR_H = %00010000 | msw(cx16.VERA_PSG_BASE)
|
||||
@@ -207,7 +238,7 @@ psg2 {
|
||||
}
|
||||
vptr++
|
||||
}
|
||||
cx16.restore_vera_context()
|
||||
psg2_restore_vera_context()
|
||||
cx16.r3 = popw()
|
||||
cx16.r2 = popw()
|
||||
cx16.r1 = popw()
|
||||
@@ -215,4 +246,50 @@ psg2 {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private asmsub psg2_save_vera_context() clobbers(A) {
|
||||
%asm {{
|
||||
; save VERA registers to our own private storage (not clobbering syslib's global _vera_storage)
|
||||
lda cx16.VERA_ADDR_L
|
||||
sta p8b_psg2.p8v_vera_storage
|
||||
lda cx16.VERA_ADDR_M
|
||||
sta p8b_psg2.p8v_vera_storage+1
|
||||
lda cx16.VERA_ADDR_H
|
||||
sta p8b_psg2.p8v_vera_storage+2
|
||||
lda cx16.VERA_CTRL
|
||||
sta p8b_psg2.p8v_vera_storage+3
|
||||
eor #1
|
||||
sta p8b_psg2.p8v_vera_storage+7
|
||||
sta cx16.VERA_CTRL
|
||||
lda cx16.VERA_ADDR_L
|
||||
sta p8b_psg2.p8v_vera_storage+4
|
||||
lda cx16.VERA_ADDR_M
|
||||
sta p8b_psg2.p8v_vera_storage+5
|
||||
lda cx16.VERA_ADDR_H
|
||||
sta p8b_psg2.p8v_vera_storage+6
|
||||
rts
|
||||
}}
|
||||
}
|
||||
|
||||
private asmsub psg2_restore_vera_context() clobbers(A) {
|
||||
%asm {{
|
||||
lda p8b_psg2.p8v_vera_storage+7
|
||||
sta cx16.VERA_CTRL
|
||||
lda p8b_psg2.p8v_vera_storage+6
|
||||
sta cx16.VERA_ADDR_H
|
||||
lda p8b_psg2.p8v_vera_storage+5
|
||||
sta cx16.VERA_ADDR_M
|
||||
lda p8b_psg2.p8v_vera_storage+4
|
||||
sta cx16.VERA_ADDR_L
|
||||
lda p8b_psg2.p8v_vera_storage+3
|
||||
sta cx16.VERA_CTRL
|
||||
lda p8b_psg2.p8v_vera_storage+2
|
||||
sta cx16.VERA_ADDR_H
|
||||
lda p8b_psg2.p8v_vera_storage+1
|
||||
sta cx16.VERA_ADDR_M
|
||||
lda p8b_psg2.p8v_vera_storage+0
|
||||
sta cx16.VERA_ADDR_L
|
||||
rts
|
||||
}}
|
||||
}
|
||||
}
|
||||
@@ -440,4 +440,40 @@ cx16 {
|
||||
r14 = cx16.save_virtual_registers.storage[14]
|
||||
r15 = cx16.save_virtual_registers.storage[15]
|
||||
}
|
||||
|
||||
|
||||
private sub print_error (str message) {
|
||||
%ir {{
|
||||
loadm.w r99000,cx16.print_error.message
|
||||
syscall 3 (r99000.w)
|
||||
}}
|
||||
}
|
||||
|
||||
sub rombank(ubyte bank) {
|
||||
if bank==0
|
||||
return
|
||||
|
||||
print_error("\nerror: rombank() only accepts 0 - aborting")
|
||||
sys.exit(1)
|
||||
}
|
||||
|
||||
sub rambank(ubyte bank) {
|
||||
if bank==0
|
||||
return
|
||||
|
||||
print_error("\nerror: rambank() only accepts 0 - aborting")
|
||||
sys.exit(1)
|
||||
}
|
||||
|
||||
inline sub getrombank() -> ubyte {
|
||||
return 0
|
||||
}
|
||||
|
||||
inline sub getrambank() -> ubyte {
|
||||
return 0
|
||||
}
|
||||
|
||||
sub numbanks() -> uword {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package prog8
|
||||
|
||||
import kotlinx.cli.*
|
||||
import prog8.ast.AstException
|
||||
import prog8.code.core.Position
|
||||
import prog8.code.source.ImportFileSystem
|
||||
import prog8.code.source.ImportFileSystem.expandTilde
|
||||
import prog8.code.target.CompilationTargets
|
||||
@@ -280,9 +281,14 @@ private fun compileMain(args: Array<String>): Boolean {
|
||||
}
|
||||
|
||||
} else if (daemonMode == true) {
|
||||
if((startEmulator1==true || startEmulator2==true) && moduleFiles.size>1) {
|
||||
System.err.println("can't start emulator when multiple module files are specified")
|
||||
return false
|
||||
}
|
||||
for(filepathRaw in moduleFiles) {
|
||||
val filepath = pathFrom(filepathRaw).normalize()
|
||||
val filepath = pathFrom(filepathRaw).normalize().toAbsolutePath()
|
||||
val txtcolors = if(plainText==true) ErrorReporter.PlainText else ErrorReporter.AnsiColors
|
||||
val absoluteSrcDirs = srcdirs.map { Path.of(it).toAbsolutePath().toString() }
|
||||
val compilerArgs = CompilerArguments(
|
||||
filepath,
|
||||
if(checkSource==true) false else dontOptimize != true,
|
||||
@@ -309,12 +315,34 @@ private fun compileMain(args: Array<String>): Boolean {
|
||||
profilingInstrumentation == true,
|
||||
nostdlib == true,
|
||||
processedSymbols,
|
||||
srcdirs,
|
||||
absoluteSrcDirs,
|
||||
outputPath,
|
||||
cwd = Path.of(System.getProperty("user.dir")),
|
||||
errors = ErrorReporter(txtcolors)
|
||||
)
|
||||
if (!compileViaDaemon(compilerArgs))
|
||||
val response = compileViaDaemon(compilerArgs, plainText == true)
|
||||
if (response == null || !response.ok)
|
||||
return false
|
||||
|
||||
if (startEmulator1 == true || startEmulator2 == true) {
|
||||
if (response.outputFiles.isEmpty()) {
|
||||
println("\nCan't start emulator because no program was assembled.")
|
||||
} else {
|
||||
val programPathRaw = response.outputFiles.first()
|
||||
val programPath = Path.of(programPathRaw.removeSuffix(".prg"))
|
||||
|
||||
val target = getCompilationTargetByName(compilationTarget!!)
|
||||
if (startEmulator1 == true) {
|
||||
if (target is VMTarget) {
|
||||
target.launchEmulatorWithTrace(programPath, quietAll==true, vmTrace==true)
|
||||
} else {
|
||||
target.launchEmulator(1, programPath, quietAll==true)
|
||||
}
|
||||
} else {
|
||||
target.launchEmulator(2, programPath, quietAll==true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
@@ -378,6 +406,7 @@ private fun compileMain(args: Array<String>): Boolean {
|
||||
}
|
||||
|
||||
val programNameInPath = outputPath.resolve(compilationResult.compilerAst.name)
|
||||
val programPath = Path.of(programNameInPath.toString().removeSuffix(".prg"))
|
||||
|
||||
if (compareIR != null) {
|
||||
compareIrFiles(outputPath.resolve("${compilationResult.compilerAst.name}.p8ir"), Path(compareIR!!))
|
||||
@@ -386,12 +415,12 @@ private fun compileMain(args: Array<String>): Boolean {
|
||||
if (startEmulator1 == true) {
|
||||
if (compilationResult.compilationOptions.compTarget is VMTarget) {
|
||||
(compilationResult.compilationOptions.compTarget as VMTarget).launchEmulatorWithTrace(
|
||||
programNameInPath, quietAll==true, vmTrace==true)
|
||||
programPath, quietAll==true, vmTrace==true)
|
||||
} else {
|
||||
compilationResult.compilationOptions.compTarget.launchEmulator(1, programNameInPath, quietAll==true)
|
||||
compilationResult.compilationOptions.compTarget.launchEmulator(1, programPath, quietAll==true)
|
||||
}
|
||||
} else if (startEmulator2 == true)
|
||||
compilationResult.compilationOptions.compTarget.launchEmulator(2, programNameInPath, quietAll==true)
|
||||
compilationResult.compilationOptions.compTarget.launchEmulator(2, programPath, quietAll==true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -591,7 +620,7 @@ private fun scanLibraryFiles(dump: String?, searchPattern: String?) {
|
||||
if(pattern!=null) {
|
||||
println("You can also have a look in the documentation for the libraries at https://prog8.readthedocs.io/en/latest/libraries.html")
|
||||
println("The library source files are available in the Github repository at https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib")
|
||||
println("Searching for pattern '$pattern' in embedded library files.\n")
|
||||
println("Searching for pattern '$searchPattern' in embedded library files.\n")
|
||||
}
|
||||
if(dumpPath!=null) {
|
||||
println("Dumping embedded library files into $dumpPath\n")
|
||||
@@ -603,20 +632,22 @@ private fun scanLibraryFiles(dump: String?, searchPattern: String?) {
|
||||
println("Note: the exported library source files have the same software license as the compiler itself.\n")
|
||||
}
|
||||
|
||||
fun search(path: Path) {
|
||||
fun search(path: Path, currentPattern: Regex, maxHits: Int = Int.MAX_VALUE): Int {
|
||||
val hits = mutableListOf<String>()
|
||||
path.useLines { lines ->
|
||||
lines.forEachIndexed { index, line ->
|
||||
if (pattern!!.containsMatchIn(line)) {
|
||||
if (currentPattern.containsMatchIn(line)) {
|
||||
hits.add("${(index + 1).toString().padStart(6)}: ${line.trimStart()}")
|
||||
if (hits.size >= maxHits) return@useLines
|
||||
}
|
||||
}
|
||||
}
|
||||
if(hits.isNotEmpty()) {
|
||||
if (hits.isNotEmpty()) {
|
||||
println("Found in Library file '${path.absolutePathString().drop(libraryPrefix.length+1)}':")
|
||||
hits.forEach(::println)
|
||||
println()
|
||||
}
|
||||
return hits.size
|
||||
}
|
||||
|
||||
fun dump(path: Path) {
|
||||
@@ -630,13 +661,27 @@ private fun scanLibraryFiles(dump: String?, searchPattern: String?) {
|
||||
val jarUrl = object {}.javaClass.protectionDomain.codeSource.location
|
||||
FileSystems.newFileSystem(URI.create("jar:$jarUrl"), emptyMap<String, String>()).use { fs ->
|
||||
val dir = fs.getPath(libraryPrefix)
|
||||
Files.walk(dir).use { paths ->
|
||||
paths.filter { Files.isRegularFile(it) }.forEach { path ->
|
||||
if(pattern!=null)
|
||||
search(path)
|
||||
else
|
||||
dump(path)
|
||||
val allFiles = Files.walk(dir).use { paths -> paths.filter { Files.isRegularFile(it) }.toList() }
|
||||
if (searchPattern != null) {
|
||||
val p = pattern!!
|
||||
var totalHits = 0
|
||||
allFiles.forEach { totalHits += search(it, p) }
|
||||
if (totalHits == 0 && searchPattern.length >= 2 && searchPattern.none { it in "\\.[]{}()^$|?*+" }) {
|
||||
val fuzzyPatternString = searchPattern.map { Regex.escape(it.toString()) }.joinToString(".{0,3}?")
|
||||
val fuzzyPattern = fuzzyPatternString.toRegex(RegexOption.IGNORE_CASE)
|
||||
println("No exact matches found. Trying fuzzy search...\n")
|
||||
var totalFuzzyHits = 0
|
||||
for (file in allFiles) {
|
||||
val remaining = 100 - totalFuzzyHits
|
||||
if (remaining <= 0) break
|
||||
totalFuzzyHits += search(file, fuzzyPattern, remaining)
|
||||
}
|
||||
if (totalFuzzyHits >= 100) {
|
||||
println("... (stopped after 100 hits)")
|
||||
}
|
||||
}
|
||||
} else if (dumpPath != null) {
|
||||
allFiles.forEach { dump(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -644,27 +689,46 @@ private fun scanLibraryFiles(dump: String?, searchPattern: String?) {
|
||||
|
||||
// ---- daemon client ----
|
||||
|
||||
private fun compileViaDaemon(compilerArgs: CompilerArguments): Boolean {
|
||||
private fun compileViaDaemon(compilerArgs: CompilerArguments, plainText: Boolean): DaemonResponse? {
|
||||
val socketPath = CompilerDaemon.getDefaultSocketPath()
|
||||
var channel = connectToDaemon(socketPath)
|
||||
var wasExisting = true
|
||||
|
||||
if (channel == null) {
|
||||
wasExisting = false
|
||||
println("Starting prog8c daemon...")
|
||||
if (!startDaemonProcess()) {
|
||||
System.err.println("Failed to start prog8c daemon")
|
||||
return false
|
||||
return null
|
||||
}
|
||||
channel = connectToDaemon(socketPath, 4_000)
|
||||
if (channel == null) {
|
||||
System.err.println("prog8c daemon did not start in time")
|
||||
return false
|
||||
return null
|
||||
}
|
||||
println("prog8c daemon started.")
|
||||
} else {
|
||||
println("Using existing prog8c daemon at $socketPath")
|
||||
}
|
||||
|
||||
val ok = communicateWithDaemon(channel, compilerArgs)
|
||||
try { channel.close() } catch (_: Exception) {}
|
||||
return ok
|
||||
val response = communicateWithDaemon(channel, compilerArgs, plainText)
|
||||
if (response != null && (response.ok || !wasExisting || response.versionError == null)) return response
|
||||
|
||||
// Existing daemon rejected us (version mismatch) and has self-terminated.
|
||||
// Start a fresh daemon.
|
||||
println("Starting new prog8c daemon (previous was stale)...")
|
||||
if (!startDaemonProcess()) {
|
||||
System.err.println("Failed to start prog8c daemon")
|
||||
return null
|
||||
}
|
||||
channel = connectToDaemon(socketPath, 4_000)
|
||||
if (channel == null) {
|
||||
System.err.println("prog8c daemon did not start in time")
|
||||
return null
|
||||
}
|
||||
println("prog8c daemon (new) started.")
|
||||
|
||||
return communicateWithDaemon(channel, compilerArgs, plainText)
|
||||
}
|
||||
|
||||
private fun startDaemonProcess(): Boolean {
|
||||
@@ -687,6 +751,7 @@ private fun startDaemonProcess(): Boolean {
|
||||
cmd.add("--daemon-server")
|
||||
|
||||
val pb = ProcessBuilder(cmd)
|
||||
pb.directory(ImportFileSystem.userHome.toFile())
|
||||
pb.inheritIO()
|
||||
pb.start()
|
||||
true
|
||||
@@ -718,13 +783,13 @@ private fun connectToDaemon(socketPath: Path, timeoutMs: Long): SocketChannel? {
|
||||
return null
|
||||
}
|
||||
|
||||
private fun communicateWithDaemon(channel: SocketChannel, compilerArgs: CompilerArguments): Boolean {
|
||||
private fun communicateWithDaemon(channel: SocketChannel, compilerArgs: CompilerArguments, plainText: Boolean): DaemonResponse? {
|
||||
try {
|
||||
val writer = BufferedWriter(OutputStreamWriter(Channels.newOutputStream(channel), Charsets.UTF_8))
|
||||
val reader = BufferedReader(InputStreamReader(Channels.newInputStream(channel), Charsets.UTF_8))
|
||||
|
||||
val request = DaemonRequest(
|
||||
version = prog8.buildversion.GIT_SHA,
|
||||
version = prog8.buildversion.BUILD_UNIX_TIME.toString(),
|
||||
filepath = compilerArgs.filepath.toString(),
|
||||
optimize = compilerArgs.optimize,
|
||||
writeAssembly = compilerArgs.writeAssembly,
|
||||
@@ -751,7 +816,8 @@ private fun communicateWithDaemon(channel: SocketChannel, compilerArgs: Compiler
|
||||
nostdlib = compilerArgs.nostdlib,
|
||||
symbolDefs = compilerArgs.symbolDefs,
|
||||
sourceDirs = compilerArgs.sourceDirs,
|
||||
outputDir = compilerArgs.outputDir.toString()
|
||||
outputDir = compilerArgs.outputDir.toString(),
|
||||
cwd = compilerArgs.cwd.toString()
|
||||
)
|
||||
|
||||
val requestJson = DaemonProtocol.encodeRequest(request)
|
||||
@@ -759,30 +825,43 @@ private fun communicateWithDaemon(channel: SocketChannel, compilerArgs: Compiler
|
||||
writer.newLine()
|
||||
writer.flush()
|
||||
|
||||
val responseJson = reader.readLine()
|
||||
val responseJson = try {
|
||||
reader.readLine()
|
||||
} catch (e: IOException) {
|
||||
System.err.println("daemon connection lost: ${e.message}")
|
||||
return null
|
||||
}
|
||||
if (responseJson == null) {
|
||||
System.err.println("daemon connection lost")
|
||||
return false
|
||||
return null
|
||||
}
|
||||
|
||||
val response = DaemonProtocol.decodeResponse(responseJson)
|
||||
|
||||
if (response.versionError != null) {
|
||||
System.err.println(response.versionError)
|
||||
return false
|
||||
return response
|
||||
}
|
||||
|
||||
System.out.print(response.output)
|
||||
System.out.flush()
|
||||
|
||||
val txtcolors = if(plainText) ErrorReporter.PlainText else ErrorReporter.AnsiColors
|
||||
val reporter = ErrorReporter(txtcolors)
|
||||
for (error in response.errors) {
|
||||
val stream = if (error.severity == "ERROR") System.err else System.out
|
||||
stream.println("${error.file?.let { "$it:" } ?: ""}${error.line}:${error.col} ${error.severity} ${error.message}")
|
||||
val pos = Position(error.file ?: "", error.line, error.startCol, error.endCol)
|
||||
when (error.severity) {
|
||||
"ERROR" -> reporter.err(error.message, pos)
|
||||
"WARNING" -> reporter.warn(error.message, pos)
|
||||
"INFO" -> reporter.info(error.message, pos)
|
||||
}
|
||||
}
|
||||
reporter.report()
|
||||
|
||||
System.out.print(response.stdout)
|
||||
System.out.flush()
|
||||
System.err.print(response.stderr)
|
||||
System.out.flush()
|
||||
System.err.flush()
|
||||
|
||||
return response.ok
|
||||
return response
|
||||
} finally {
|
||||
try { channel.close() } catch (_: Exception) {}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import prog8.parser.MultipleParseErrors
|
||||
import prog8.parser.ParseError
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.absolute
|
||||
import kotlin.io.path.isRegularFile
|
||||
import kotlin.io.path.nameWithoutExtension
|
||||
import kotlin.system.exitProcess
|
||||
@@ -73,6 +74,7 @@ class CompilerArguments(val filepath: Path,
|
||||
val symbolDefs: Map<String, String>,
|
||||
val sourceDirs: List<String> = emptyList(),
|
||||
val outputDir: Path = Path(""),
|
||||
val cwd: Path = Path("").absolute(),
|
||||
val errors: IErrorReporter = ErrorReporter(ErrorReporter.AnsiColors))
|
||||
|
||||
|
||||
@@ -111,6 +113,7 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
|
||||
compTarget,
|
||||
args.sourceDirs,
|
||||
libraryDirs,
|
||||
args.cwd,
|
||||
args.quietAll,
|
||||
args.nostdlib
|
||||
)
|
||||
@@ -408,13 +411,14 @@ fun parseMainModule(filepath: Path,
|
||||
compTarget: ICompilationTarget,
|
||||
sourceDirs: List<String>,
|
||||
libraryDirs: List<String>,
|
||||
cwd: Path,
|
||||
quiet: Boolean,
|
||||
nostdlib: Boolean): Triple<Program, CompilationOptions, List<Path>> {
|
||||
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
|
||||
val program = Program(filepath.nameWithoutExtension, bf, compTarget, compTarget)
|
||||
bf.program = program
|
||||
|
||||
val importer = ModuleImporter(program, compTarget.name, errors, sourceDirs, libraryDirs, quiet, nostdlib)
|
||||
val importer = ModuleImporter(program, compTarget.name, errors, sourceDirs, libraryDirs, cwd, quiet, nostdlib)
|
||||
val importedModuleResult = importer.importMainModule(filepath)
|
||||
importedModuleResult.onErr { throw it }
|
||||
errors.report()
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package prog8.compiler
|
||||
|
||||
import prog8.buildversion.GIT_SHA
|
||||
import prog8.buildversion.BUILD_UNIX_TIME
|
||||
import prog8.code.core.Position
|
||||
import prog8.code.source.ImportFileSystem
|
||||
import prog8.code.source.SourceCode
|
||||
import java.io.*
|
||||
import java.net.StandardProtocolFamily
|
||||
import java.net.UnixDomainSocketAddress
|
||||
@@ -20,7 +22,7 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
fun getDefaultSocketPath(): Path {
|
||||
val baseDir = System.getenv("XDG_RUNTIME_DIR")
|
||||
?.let { Path.of(it) }
|
||||
?: Path.of(System.getProperty("user.home"), ".cache")
|
||||
?: ImportFileSystem.userHome.resolve(".cache")
|
||||
return baseDir.resolve("prog8c-daemon.sock")
|
||||
}
|
||||
|
||||
@@ -31,6 +33,8 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
}
|
||||
}
|
||||
|
||||
private var shutdownRequested = false
|
||||
|
||||
fun run() {
|
||||
socketPath.deleteIfExists()
|
||||
socketPath.parent?.let { Files.createDirectories(it) }
|
||||
@@ -71,6 +75,7 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
handleClient(client)
|
||||
client.close()
|
||||
lastRequestTime = System.currentTimeMillis()
|
||||
if (shutdownRequested) break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,13 +99,14 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
return
|
||||
}
|
||||
|
||||
// Version check
|
||||
if (request.version != GIT_SHA) {
|
||||
// Version check (build timestamp must match)
|
||||
if (request.version != BUILD_UNIX_TIME.toString()) {
|
||||
val resp = DaemonResponse(
|
||||
ok = false,
|
||||
versionError = "daemon version mismatch: client=${request.version.take(8)}, daemon=${GIT_SHA.take(8)}",
|
||||
versionError = "daemon version mismatch: build time differs",
|
||||
errors = emptyList(),
|
||||
output = "",
|
||||
stdout = "",
|
||||
stderr = "",
|
||||
t_ms = 0,
|
||||
outputFiles = emptyList(),
|
||||
importedFiles = emptyList()
|
||||
@@ -108,19 +114,22 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
writer.write(DaemonProtocol.encodeResponse(resp))
|
||||
writer.newLine()
|
||||
writer.flush()
|
||||
System.err.println("daemon: version mismatch, shutting down")
|
||||
shutdownRequested = true
|
||||
return
|
||||
}
|
||||
|
||||
// Capture stdout/stderr
|
||||
val outBytes = ByteArrayOutputStream()
|
||||
val errBytes = ByteArrayOutputStream()
|
||||
val combinedBytes = ByteArrayOutputStream()
|
||||
val combinedStream = PrintStream(combinedBytes, true, Charsets.UTF_8.name())
|
||||
val oldOut = System.out
|
||||
val oldErr = System.err
|
||||
System.setOut(PrintStream(outBytes, true, Charsets.UTF_8.name()))
|
||||
System.setErr(PrintStream(errBytes, true, Charsets.UTF_8.name()))
|
||||
System.setOut(combinedStream)
|
||||
System.setErr(combinedStream)
|
||||
|
||||
val daemonErr = DaemonErrorReporter()
|
||||
var result: CompilationResult? = null
|
||||
ImportFileSystem.clearCaches()
|
||||
val t_ms = try {
|
||||
measureTime {
|
||||
val compilerArgs = request.toCompilerArguments(daemonErr)
|
||||
@@ -140,12 +149,12 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
val outputFiles = if (result != null)
|
||||
listOfNotNull(
|
||||
request.outputDir.let { d ->
|
||||
val name = request.filepath.substringBeforeLast('.').substringAfterLast('/').substringAfterLast('\\')
|
||||
val name = Path.of(request.filepath).fileName.toString().substringBeforeLast('.')
|
||||
val dir = if (d == ".") "" else "$d/"
|
||||
listOf(
|
||||
"${dir}${name}.prg".takeIf { request.writeAssembly },
|
||||
"${dir}${name}.asm".takeIf { request.writeAssembly },
|
||||
"${dir}${name}.p8ir".takeIf { !request.writeAssembly }
|
||||
"${dir}${name}.prg".takeIf { request.writeAssembly && request.compilationTarget != "virtual" },
|
||||
"${dir}${name}.asm".takeIf { request.writeAssembly && request.compilationTarget != "virtual" },
|
||||
"${dir}${name}.p8ir".takeIf { !request.writeAssembly || request.compilationTarget == "virtual" }
|
||||
).filterNotNull()
|
||||
}
|
||||
).flatten()
|
||||
@@ -154,21 +163,43 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
|
||||
val importedFiles = result?.importedFiles?.map { it.toString() } ?: emptyList()
|
||||
|
||||
val combinedOutput = outBytes.toString(Charsets.UTF_8.name()) + errBytes.toString(Charsets.UTF_8.name())
|
||||
val stdout = combinedBytes.toString(Charsets.UTF_8.name())
|
||||
val stderr = ""
|
||||
|
||||
val resp = DaemonResponse(
|
||||
ok = result != null,
|
||||
versionError = null,
|
||||
errors = daemonErr.getMessages().map { msg ->
|
||||
// Strip "ERROR ", "WARNING ", "INFO " prefixes
|
||||
var message = if (msg.message.startsWith("${msg.severity} ")) {
|
||||
msg.message.substring(msg.severity.length + 1)
|
||||
} else {
|
||||
msg.message
|
||||
}
|
||||
|
||||
// Strip path prefix if it exists (e.g. file:///...:line:col: )
|
||||
// The path prefix usually ends with a colon followed by a space
|
||||
val pathRegex = Regex("^file:///[^:]+:[0-9]+:[0-9]+: ")
|
||||
message = message.replace(pathRegex, "")
|
||||
|
||||
DaemonError(
|
||||
severity = msg.severity,
|
||||
message = msg.message,
|
||||
file = msg.position?.file,
|
||||
message = message,
|
||||
file = msg.position?.let {
|
||||
val origin = it.file
|
||||
if (SourceCode.isLibraryResource(origin)) {
|
||||
origin
|
||||
} else {
|
||||
ImportFileSystem.userHome.resolve(origin).normalize().toString()
|
||||
}
|
||||
},
|
||||
line = msg.position?.line ?: 1,
|
||||
col = msg.position?.startCol ?: 1
|
||||
startCol = msg.position?.startCol ?: 1,
|
||||
endCol = msg.position?.endCol ?: 1
|
||||
)
|
||||
},
|
||||
output = combinedOutput,
|
||||
stdout = stdout,
|
||||
stderr = stderr,
|
||||
t_ms = t_ms,
|
||||
outputFiles = outputFiles,
|
||||
importedFiles = importedFiles
|
||||
@@ -212,6 +243,7 @@ internal class CompilerDaemon(private val socketPath: Path) {
|
||||
symbolDefs = symbolDefs,
|
||||
sourceDirs = sourceDirs,
|
||||
outputDir = Path.of(outputDir),
|
||||
cwd = Path.of(cwd),
|
||||
errors = daemonErr
|
||||
)
|
||||
}
|
||||
|
||||
@@ -29,14 +29,16 @@ internal data class DaemonRequest(
|
||||
val nostdlib: Boolean,
|
||||
val symbolDefs: Map<String, String>,
|
||||
val sourceDirs: List<String>,
|
||||
val outputDir: String
|
||||
val outputDir: String,
|
||||
val cwd: String
|
||||
)
|
||||
|
||||
internal data class DaemonResponse(
|
||||
val ok: Boolean,
|
||||
val versionError: String?,
|
||||
val errors: List<DaemonError>,
|
||||
val output: String,
|
||||
val stdout: String,
|
||||
val stderr: String,
|
||||
val t_ms: Long,
|
||||
val outputFiles: List<String>,
|
||||
val importedFiles: List<String>
|
||||
@@ -47,7 +49,8 @@ internal data class DaemonError(
|
||||
val message: String,
|
||||
val file: String?,
|
||||
val line: Int,
|
||||
val col: Int
|
||||
val startCol: Int,
|
||||
val endCol: Int
|
||||
)
|
||||
|
||||
|
||||
@@ -84,6 +87,7 @@ internal object DaemonProtocol {
|
||||
append(prop("symbolDefs", req.symbolDefs))
|
||||
append(prop("sourceDirs", req.sourceDirs))
|
||||
append(prop("outputDir", req.outputDir))
|
||||
append(prop("cwd", req.cwd))
|
||||
append("null") // placeholder, gets overwritten by trimEnd
|
||||
setLength(length - 5)
|
||||
append('}')
|
||||
@@ -101,13 +105,15 @@ internal object DaemonProtocol {
|
||||
append(prop("message", e.message))
|
||||
append(propOpt("file", e.file))
|
||||
append(prop("line", e.line))
|
||||
append(prop("col", e.col))
|
||||
append(prop("startCol", e.startCol))
|
||||
append(prop("endCol", e.endCol))
|
||||
append("null")
|
||||
setLength(length - 4)
|
||||
append('}')
|
||||
}
|
||||
append("],")
|
||||
append(prop("output", resp.output))
|
||||
append(prop("stdout", resp.stdout))
|
||||
append(prop("stderr", resp.stderr))
|
||||
append(prop("t_ms", resp.t_ms))
|
||||
append(prop("outputFiles", resp.outputFiles))
|
||||
append(prop("importedFiles", resp.importedFiles))
|
||||
@@ -126,7 +132,8 @@ internal object DaemonProtocol {
|
||||
message = em["message"] as String,
|
||||
file = em["file"] as? String,
|
||||
line = (em["line"] as Number).toInt(),
|
||||
col = (em["col"] as Number).toInt()
|
||||
startCol = (em["startCol"] as Number).toInt(),
|
||||
endCol = (em["endCol"] as Number).toInt()
|
||||
)
|
||||
}
|
||||
val outputFiles = (map["outputFiles"] as? List<*>)?.map { it as String } ?: emptyList()
|
||||
@@ -135,7 +142,8 @@ internal object DaemonProtocol {
|
||||
ok = map["ok"] as Boolean,
|
||||
versionError = map["versionError"] as? String,
|
||||
errors = errors,
|
||||
output = map["output"] as String,
|
||||
stdout = map["stdout"] as String,
|
||||
stderr = map["stderr"] as String,
|
||||
t_ms = (map["t_ms"] as Number).toLong(),
|
||||
outputFiles = outputFiles,
|
||||
importedFiles = importedFiles
|
||||
@@ -172,7 +180,8 @@ internal object DaemonProtocol {
|
||||
nostdlib = map["nostdlib"] as Boolean,
|
||||
symbolDefs = (map["symbolDefs"] as? Map<*, *>)?.mapKeys { it.key as String }?.mapValues { it.value as String } ?: emptyMap(),
|
||||
sourceDirs = (map["sourceDirs"] as? List<*>)?.map { it as String } ?: emptyList(),
|
||||
outputDir = map["outputDir"] as String
|
||||
outputDir = map["outputDir"] as String,
|
||||
cwd = map["cwd"] as String
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ import prog8.parser.Prog8Parser
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.absolute
|
||||
import kotlin.io.path.exists
|
||||
|
||||
|
||||
@@ -24,6 +23,7 @@ class ModuleImporter(private val program: Program,
|
||||
val errors: IErrorReporter,
|
||||
sourceDirs: List<String>,
|
||||
libraryDirs: List<String>,
|
||||
val cwd: Path,
|
||||
val quiet: Boolean,
|
||||
val nostdlib: Boolean = false) {
|
||||
|
||||
@@ -31,15 +31,18 @@ class ModuleImporter(private val program: Program,
|
||||
private val libraryPaths: List<Path> = libraryDirs.map { Path(it).sanitize() }.toSortedSet().toList()
|
||||
|
||||
fun importMainModule(filePath: Path): Result<Module, NoSuchFileException> {
|
||||
val searchIn = (listOf(Path("").absolute()) + sourcePaths).toSortedSet()
|
||||
val searchIn = sourcePaths.toSortedSet()
|
||||
val normalizedFilePath = filePath.normalize()
|
||||
|
||||
if (normalizedFilePath.exists()) {
|
||||
printCompileInfo(normalizedFilePath.toAbsolutePath())
|
||||
return Ok(importModule(ImportFileSystem.getFile(normalizedFilePath)))
|
||||
}
|
||||
|
||||
for(path in searchIn) {
|
||||
val programPath = path.resolve(normalizedFilePath)
|
||||
if(programPath.exists()) {
|
||||
if(!quiet) {
|
||||
println("Compiling program ${Path("").absolute().relativize(programPath)}")
|
||||
println("Compiler target: $compilationTargetName")
|
||||
}
|
||||
printCompileInfo(programPath)
|
||||
val source = ImportFileSystem.getFile(programPath)
|
||||
return Ok(importModule(source))
|
||||
}
|
||||
@@ -49,6 +52,13 @@ class ModuleImporter(private val program: Program,
|
||||
reason = "Searched in $searchIn"))
|
||||
}
|
||||
|
||||
private fun printCompileInfo(programPath: Path) {
|
||||
if(!quiet) {
|
||||
println("Compiling program ${cwd.toAbsolutePath().relativize(programPath)}")
|
||||
println("Compiler target: $compilationTargetName")
|
||||
}
|
||||
}
|
||||
|
||||
fun importImplicitLibraryModule(name: String): Module? {
|
||||
val import = Directive("%import", listOf(
|
||||
DirectiveArg(name, 42u, position = Position("~implicit-import~", 0, 0, 0))
|
||||
|
||||
@@ -979,7 +979,9 @@ internal class AstChecker(private val program: Program,
|
||||
|
||||
// CONST can only occur on simple types (byte, word, float) and pointers
|
||||
if(decl.type==VarDeclType.CONST) {
|
||||
if (!decl.datatype.isNumericOrBool && !decl.datatype.isPointer)
|
||||
if(decl.datatype.isString)
|
||||
err("strings cannot be made const they are always potentially mutable")
|
||||
else if (!decl.datatype.isNumericOrBool && !decl.datatype.isPointer)
|
||||
err("const can only be used on numbers, booleans and pointers")
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -25,7 +25,8 @@ internal class LiteralsToAutoVarsAndRecombineIdentifiers(private val program: Pr
|
||||
return noModifications
|
||||
}
|
||||
}
|
||||
val scopedName = program.internString(string)
|
||||
val isInStruct = findParentNode<StaticStructInitializer>(string) != null
|
||||
val scopedName = program.internString(string, deduplicate = !isInStruct)
|
||||
val identifier = IdentifierReference(scopedName, string.position)
|
||||
return listOf(AstReplaceNode(string, identifier, parent))
|
||||
}
|
||||
|
||||
@@ -46,7 +46,17 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
|
||||
VarDeclType.CONST -> {
|
||||
// change the vardecl type itself as well, but only if new type is smaller
|
||||
if(valueDt.largerSizeThan(decl.datatype)) {
|
||||
val constValue = decl.value!!.constValue(program)!!
|
||||
val constValue = decl.value!!.constValue(program)
|
||||
if(constValue==null) {
|
||||
// non-numeric const value, such as a string literal assigned to a pointer type
|
||||
// convert it to a regular variable instead
|
||||
if(decl.value is StringLiteral && decl.datatype.isPointer) {
|
||||
val vardecl = decl.copy(DataType.STR)
|
||||
vardecl.type = VarDeclType.VAR
|
||||
return listOf(AstReplaceNode(decl, vardecl, parent))
|
||||
}
|
||||
return noModifications
|
||||
}
|
||||
errors.err("value '${constValue.number}' out of range for ${decl.datatype}", constValue.position)
|
||||
} else if(decl.datatype.largerSizeThan(valueDt)) {
|
||||
// don't make it signed if it was unsigned and vice versa, except when it is a long const declaration
|
||||
@@ -54,7 +64,7 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
|
||||
(valueDt.isSigned && decl.datatype.isUnsigned ||
|
||||
valueDt.isUnsigned && decl.datatype.isSigned))
|
||||
{
|
||||
val constValue = decl.value!!.constValue(program)!!
|
||||
val constValue = decl.value!!.constValue(program) ?: return noModifications
|
||||
errors.err("value '${constValue.number}' out of range for ${decl.datatype}", constValue.position)
|
||||
} else {
|
||||
val changed = decl.copy(valueDt)
|
||||
|
||||
@@ -218,7 +218,7 @@ class TestCompilerOnExamplesCx16: FunSpec({
|
||||
ExampleSizes("audioroutines", cx16SizeOptimized=948, cx16SizeUnoptimized=1244),
|
||||
ExampleSizes("automatons", cx16SizeOptimized=1037, cx16SizeUnoptimized=1342),
|
||||
ExampleSizes("balloonflight", cx16SizeOptimized=3187, cx16SizeUnoptimized=4163),
|
||||
ExampleSizes("bdmusic", cx16SizeOptimized=2386, cx16SizeUnoptimized=2761),
|
||||
ExampleSizes("bdmusic", cx16SizeOptimized=2427, cx16SizeUnoptimized=2792),
|
||||
ExampleSizes("bobs", cx16SizeOptimized=2800, cx16SizeUnoptimized=3414),
|
||||
ExampleSizes("bubbleuniverse", cx16SizeOptimized=1450, cx16SizeUnoptimized=1958),
|
||||
ExampleSizes("charfade", cx16SizeOptimized=1054, cx16SizeUnoptimized=1362),
|
||||
@@ -296,7 +296,7 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({
|
||||
ExampleSizes("screencodes", c64SizeOptimized=667, c64SizeUnoptimized=687, cx16SizeOptimized=655, cx16SizeUnoptimized=959),
|
||||
ExampleSizes("swirl", c64SizeOptimized=828, c64SizeUnoptimized=831, cx16SizeOptimized=923, cx16SizeUnoptimized=1208),
|
||||
ExampleSizes("swirl-float", c64SizeOptimized=708, c64SizeUnoptimized=735, cx16SizeOptimized=595, cx16SizeUnoptimized=906),
|
||||
ExampleSizes("tehtriz", c64SizeOptimized=4333, c64SizeUnoptimized=4504, cx16SizeOptimized=6211, cx16SizeUnoptimized=6742),
|
||||
ExampleSizes("tehtriz", c64SizeOptimized=4333, c64SizeUnoptimized=4504, cx16SizeOptimized=6252, cx16SizeUnoptimized=6773),
|
||||
ExampleSizes("textelite", c64SizeOptimized=11440, c64SizeUnoptimized=12423, cx16SizeOptimized=10778, cx16SizeUnoptimized=13046),
|
||||
ExampleSizes("pointers/animalgame", c64SizeOptimized=1570, c64SizeUnoptimized=2126, cx16SizeOptimized=1605, cx16SizeUnoptimized=2412),
|
||||
ExampleSizes("pointers/binarytree", c64SizeOptimized=2562, c64SizeUnoptimized=2640, cx16SizeOptimized=2085, cx16SizeUnoptimized=2389),
|
||||
@@ -335,17 +335,17 @@ class TestCompilerOnExamplesVirtual: FunSpec({
|
||||
|
||||
val onlyVirtual = cartesianProduct(
|
||||
listOf(
|
||||
ExampleSizes("bouncegfx", virtualInstrCountOptimized=186, virtualInstrCountUnoptimized=824, virtualRegCountOptimized=92, virtualRegCountUnoptimized=380),
|
||||
ExampleSizes("bsieve", virtualInstrCountOptimized=158, virtualInstrCountUnoptimized=1939, virtualRegCountOptimized=66, virtualRegCountUnoptimized=860),
|
||||
ExampleSizes("fountain", virtualInstrCountOptimized=150, virtualInstrCountUnoptimized=791, virtualRegCountOptimized=75, virtualRegCountUnoptimized=363),
|
||||
ExampleSizes("pixelshader", virtualInstrCountOptimized=43, virtualInstrCountUnoptimized=286, virtualRegCountOptimized=15, virtualRegCountUnoptimized=109),
|
||||
ExampleSizes("sincos", virtualInstrCountOptimized=276, virtualInstrCountUnoptimized=898, virtualRegCountOptimized=133, virtualRegCountUnoptimized=403),
|
||||
ExampleSizes("pointers/animalgame", virtualInstrCountOptimized=221, virtualInstrCountUnoptimized=2083, virtualRegCountOptimized=103, virtualRegCountUnoptimized=933),
|
||||
ExampleSizes("pointers/binarytree", virtualInstrCountOptimized=510, virtualInstrCountUnoptimized=2237, virtualRegCountOptimized=251, virtualRegCountUnoptimized=1020),
|
||||
ExampleSizes("pointers/hashtable", virtualInstrCountOptimized=578, virtualInstrCountUnoptimized=2255, virtualRegCountOptimized=293, virtualRegCountUnoptimized=1045),
|
||||
ExampleSizes("pointers/sortedlist", virtualInstrCountOptimized=267, virtualInstrCountUnoptimized=2406, virtualRegCountOptimized=118, virtualRegCountUnoptimized=1086),
|
||||
ExampleSizes("pointers/fountain-virtual", virtualInstrCountOptimized=145, virtualInstrCountUnoptimized=785, virtualRegCountOptimized=70, virtualRegCountUnoptimized=357),
|
||||
ExampleSizes("pointers/sorting", virtualInstrCountOptimized=369, virtualInstrCountUnoptimized=2251, virtualRegCountOptimized=173, virtualRegCountUnoptimized=1010)
|
||||
ExampleSizes("bouncegfx", virtualInstrCountOptimized=186, virtualInstrCountUnoptimized=846, virtualRegCountOptimized=92, virtualRegCountUnoptimized=386),
|
||||
ExampleSizes("bsieve", virtualInstrCountOptimized=158, virtualInstrCountUnoptimized=1961, virtualRegCountOptimized=66, virtualRegCountUnoptimized=866),
|
||||
ExampleSizes("fountain", virtualInstrCountOptimized=150, virtualInstrCountUnoptimized=813, virtualRegCountOptimized=75, virtualRegCountUnoptimized=369),
|
||||
ExampleSizes("pixelshader", virtualInstrCountOptimized=43, virtualInstrCountUnoptimized=308, virtualRegCountOptimized=15, virtualRegCountUnoptimized=115),
|
||||
ExampleSizes("sincos", virtualInstrCountOptimized=276, virtualInstrCountUnoptimized=920, virtualRegCountOptimized=133, virtualRegCountUnoptimized=409),
|
||||
ExampleSizes("pointers/animalgame", virtualInstrCountOptimized=221, virtualInstrCountUnoptimized=2105, virtualRegCountOptimized=103, virtualRegCountUnoptimized=939),
|
||||
ExampleSizes("pointers/binarytree", virtualInstrCountOptimized=510, virtualInstrCountUnoptimized=2259, virtualRegCountOptimized=251, virtualRegCountUnoptimized=1026),
|
||||
ExampleSizes("pointers/hashtable", virtualInstrCountOptimized=578, virtualInstrCountUnoptimized=2277, virtualRegCountOptimized=293, virtualRegCountUnoptimized=1051),
|
||||
ExampleSizes("pointers/sortedlist", virtualInstrCountOptimized=267, virtualInstrCountUnoptimized=2428, virtualRegCountOptimized=118, virtualRegCountUnoptimized=1092),
|
||||
ExampleSizes("pointers/fountain-virtual", virtualInstrCountOptimized=145, virtualInstrCountUnoptimized=807, virtualRegCountOptimized=70, virtualRegCountUnoptimized=363),
|
||||
ExampleSizes("pointers/sorting", virtualInstrCountOptimized=369, virtualInstrCountUnoptimized=2273, virtualRegCountOptimized=173, virtualRegCountUnoptimized=1016)
|
||||
),
|
||||
listOf(false, true)
|
||||
)
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.kotest.matchers.shouldBe
|
||||
import io.kotest.matchers.shouldNotBe
|
||||
import io.kotest.matchers.string.shouldContain
|
||||
import io.kotest.matchers.string.shouldStartWith
|
||||
import java.nio.file.Path
|
||||
import prog8.code.INTERNED_STRINGS_MODULENAME
|
||||
import prog8.code.core.ZeropageType
|
||||
import prog8.code.target.C64Target
|
||||
@@ -96,7 +97,7 @@ main {
|
||||
|
||||
val filepath = outputDir.resolve("$filenameBase.p8")
|
||||
filepath.toFile().writeText(sourceText)
|
||||
val (program, options, importedfiles) = parseMainModule(filepath, errors, C64Target(), emptyList(), emptyList(), false, false)
|
||||
val (program, options, importedfiles) = parseMainModule(filepath, errors, C64Target(), emptyList(), emptyList(), Path.of("."), false, false)
|
||||
|
||||
program.toplevelModule.name shouldBe filenameBase
|
||||
withClue("all imports other than the test source must have been internal resources library files") {
|
||||
|
||||
@@ -29,7 +29,7 @@ class TestModuleImporter: FunSpec({
|
||||
}
|
||||
|
||||
fun makeImporter(errors: IErrorReporter? = null, searchIn: Iterable<String>) =
|
||||
ModuleImporter(program, "blah", errors ?: ErrorReporterForTests(false), searchIn.toList(), emptyList(), false)
|
||||
ModuleImporter(program, "blah", errors ?: ErrorReporterForTests(false), searchIn.toList(), emptyList(), Path(".").absolute(), false)
|
||||
|
||||
fun makeImporter(errors: IErrorReporter?, vararg searchIn: String): ModuleImporter {
|
||||
return makeImporter(errors, searchIn.asList())
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package prog8tests.compiler
|
||||
|
||||
import io.kotest.assertions.fail
|
||||
import io.kotest.assertions.withClue
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.engine.spec.tempdir
|
||||
@@ -14,6 +15,7 @@ import prog8.ast.ParentSentinel
|
||||
import prog8.ast.Program
|
||||
import prog8.ast.expressions.*
|
||||
import prog8.ast.statements.*
|
||||
import prog8.ast.walk.IAstVisitor
|
||||
import prog8.code.ast.*
|
||||
import prog8.code.core.BaseDataType
|
||||
import prog8.code.core.DataType
|
||||
@@ -1231,18 +1233,26 @@ main {
|
||||
val result = compileText(Cx16Target(), true, src, outputDir, writeAssembly = false)!!
|
||||
val st = result.compilerAst.entrypoint.statements
|
||||
st.size shouldBe 4
|
||||
val ifCond1 = (st[0] as IfElse).condition as BinaryExpression
|
||||
val ifCond2 = (st[1] as IfElse).condition as BinaryExpression
|
||||
val ifCond3 = (st[2] as IfElse).condition as BinaryExpression
|
||||
(ifCond1.left as FunctionCallExpression).target.nameInSource shouldBe listOf("f_open")
|
||||
(ifCond2.left as FunctionCallExpression).target.nameInSource shouldBe listOf("f_open")
|
||||
(ifCond3.left as FunctionCallExpression).target.nameInSource shouldBe listOf("f_open")
|
||||
val right1 = ifCond1.right as BinaryExpression
|
||||
val right2 = ifCond2.right as BinaryExpression
|
||||
val right3 = ifCond3.right as BinaryExpression
|
||||
(right1.left as FunctionCallExpression).target.nameInSource shouldBe listOf("f_read")
|
||||
(right2.left as FunctionCallExpression).target.nameInSource shouldBe listOf("f_read")
|
||||
(right3.left as FunctionCallExpression).target.nameInSource shouldBe listOf("f_read")
|
||||
|
||||
fun assertCondition(cond: Expression) {
|
||||
fun findFunctionCalls(expr: Expression): List<FunctionCallExpression> {
|
||||
return when (expr) {
|
||||
is FunctionCallExpression -> listOf(expr)
|
||||
is BinaryExpression -> findFunctionCalls(expr.left) + findFunctionCalls(expr.right)
|
||||
is ContainmentCheck -> findFunctionCalls(expr.element) + findFunctionCalls(expr.iterable)
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
val calls = findFunctionCalls(cond)
|
||||
if (calls.isNotEmpty()) {
|
||||
calls.any { it.target.nameInSource == listOf("f_open") } shouldBe true
|
||||
calls.any { it.target.nameInSource == listOf("f_read") } shouldBe true
|
||||
}
|
||||
}
|
||||
|
||||
assertCondition((st[0] as IfElse).condition)
|
||||
assertCondition((st[1] as IfElse).condition)
|
||||
assertCondition((st[2] as IfElse).condition)
|
||||
}
|
||||
|
||||
test("eliminate same target register assignments") {
|
||||
@@ -1786,6 +1796,36 @@ main {
|
||||
}
|
||||
}
|
||||
|
||||
test("call with two parameters where one has a register alias is not inlined") {
|
||||
// Tests that a function call with two parameters where one has a register alias is NOT inlined
|
||||
val src = """
|
||||
main {
|
||||
ubyte @shared v1 = 10
|
||||
ubyte @shared v2 = 20
|
||||
|
||||
sub start() {
|
||||
ubyte a
|
||||
a = get_two(1, 2)
|
||||
cx16.r0 = a
|
||||
}
|
||||
sub get_two(ubyte x, ubyte y @R0) -> ubyte {
|
||||
return v1
|
||||
}
|
||||
}"""
|
||||
val result = compileText(VMTarget(), true, src, outputDir, writeAssembly = false)!!
|
||||
val startSub = result.compilerAst.entrypoint
|
||||
val stmts = startSub.statements
|
||||
|
||||
// Should NOT be inlined, so the FunctionCallExpression should still exist in start()
|
||||
val callNodes = stmts.filter { stmt ->
|
||||
if (stmt is Assignment) stmt.value is FunctionCallExpression && (stmt.value as FunctionCallExpression).target.nameInSource.last() == "get_two"
|
||||
else false
|
||||
}
|
||||
|
||||
// There should be exactly one call remaining
|
||||
callNodes.size shouldBe 1
|
||||
}
|
||||
|
||||
test("inline void call with six parameters") {
|
||||
// Tests that void calls with six parameters are inlined when args are simple
|
||||
val src = """
|
||||
@@ -1992,4 +2032,57 @@ main {
|
||||
(y3.left as PtPrefix).operator shouldBe "-"
|
||||
(y3.right as PtNumber).number shouldBe 3.0
|
||||
}
|
||||
|
||||
test("string literals in struct instances are not deduplicated") {
|
||||
val src = """
|
||||
main {
|
||||
struct Node {
|
||||
str name
|
||||
}
|
||||
|
||||
^^Node n1 = ^^Node:["test"]
|
||||
^^Node n2 = ^^Node:["test"]
|
||||
|
||||
sub start() {
|
||||
cx16.r0 = n1^^.name
|
||||
cx16.r1 = n2^^.name
|
||||
}
|
||||
}
|
||||
"""
|
||||
val errors = ErrorReporterForTests()
|
||||
val result = compileText(VMTarget(), true, src, outputDir, errors = errors)
|
||||
if (result == null) {
|
||||
println(errors.errors)
|
||||
fail("Compilation failed: " + errors.errors)
|
||||
}
|
||||
|
||||
class InitializerCollector : IAstVisitor {
|
||||
val initializers = mutableListOf<StaticStructInitializer>()
|
||||
override fun visit(initializer: StaticStructInitializer) {
|
||||
initializers.add(initializer)
|
||||
// Default traversal
|
||||
initializer.structname.accept(this)
|
||||
initializer.args.forEach { it.accept(this) }
|
||||
}
|
||||
}
|
||||
|
||||
val collector = InitializerCollector()
|
||||
result.compilerAst.modules.forEach { it.accept(collector) }
|
||||
|
||||
collector.initializers.size shouldBe 2
|
||||
|
||||
// The first argument is the string (the "name" field)
|
||||
fun getIdentifier(expr: Expression): IdentifierReference {
|
||||
return when (expr) {
|
||||
is IdentifierReference -> expr
|
||||
is AddressOf -> expr.identifier!!
|
||||
else -> fail("Expected IdentifierReference or AddressOf, got " + expr::class.simpleName)
|
||||
}
|
||||
}
|
||||
val name1 = getIdentifier(collector.initializers[0].args[0])
|
||||
val name2 = getIdentifier(collector.initializers[1].args[0])
|
||||
|
||||
// They should point to different interned strings
|
||||
name1.nameInSource shouldNotBe name2.nameInSource
|
||||
}
|
||||
})
|
||||
@@ -498,6 +498,21 @@ main {
|
||||
compileText(Cx16Target(), true, src, outputDir, writeAssembly = false) shouldNotBe null
|
||||
}
|
||||
|
||||
test("const string to pointer type compiles") {
|
||||
val src = """
|
||||
main {
|
||||
sub start() {
|
||||
const ^^ubyte name = "irmen"
|
||||
thing(name)
|
||||
}
|
||||
|
||||
sub thing(str x) {
|
||||
cx16.r0L++
|
||||
}
|
||||
}"""
|
||||
compileText(VMTarget(), true, src, outputDir, writeAssembly = false) shouldNotBe null
|
||||
}
|
||||
|
||||
test("const long with large unsigned long values should be converted to signed longs") {
|
||||
val src = $$"""
|
||||
main {
|
||||
|
||||
@@ -1090,5 +1090,19 @@ main {
|
||||
result shouldNotBe null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
test("some dummy support for rombank() rambank() in virtual target") {
|
||||
val src = """
|
||||
main {
|
||||
sub start() {
|
||||
cx16.r0 = cx16.numbanks()
|
||||
cx16.rambank(0)
|
||||
cx16.rombank(0)
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
val result = compileText(VMTarget(), false, src, outputDir)
|
||||
result shouldNotBe null
|
||||
}
|
||||
|
||||
})
|
||||
@@ -74,7 +74,7 @@ class Program(val name: String,
|
||||
|
||||
private val internedStringsReferenceCounts = mutableMapOf<VarDecl, Int>()
|
||||
|
||||
fun internString(string: StringLiteral): List<String> {
|
||||
fun internString(string: StringLiteral, deduplicate: Boolean = true): List<String> {
|
||||
// Move a string literal into the internal, deduplicated, string pool
|
||||
// replace it with a variable declaration that points to the entry in the pool.
|
||||
|
||||
@@ -100,19 +100,20 @@ class Program(val name: String,
|
||||
return Pair(listOf(INTERNED_STRINGS_MODULENAME, decl.name), decl)
|
||||
}
|
||||
|
||||
val existingDecl = internedStringsBlock.statements.filterIsInstance<VarDecl>().singleOrNull {
|
||||
val declString = it.value as StringLiteral
|
||||
declString.encoding == string.encoding && declString.value == string.value
|
||||
}
|
||||
return if (existingDecl != null) {
|
||||
internedStringsReferenceCounts[existingDecl] = internedStringsReferenceCounts.getValue(existingDecl)+1
|
||||
existingDecl.scopedName
|
||||
}
|
||||
else {
|
||||
val (newName, newDecl) = addNewInternedStringvar(string)
|
||||
internedStringsReferenceCounts[newDecl] = 1
|
||||
newName
|
||||
if (deduplicate) {
|
||||
val existingDecl = internedStringsBlock.statements.filterIsInstance<VarDecl>().singleOrNull {
|
||||
val declString = it.value as StringLiteral
|
||||
declString.encoding == string.encoding && declString.value == string.value
|
||||
}
|
||||
if (existingDecl != null) {
|
||||
internedStringsReferenceCounts[existingDecl] = internedStringsReferenceCounts.getValue(existingDecl)+1
|
||||
return existingDecl.scopedName
|
||||
}
|
||||
}
|
||||
|
||||
val (newName, newDecl) = addNewInternedStringvar(string)
|
||||
internedStringsReferenceCounts[newDecl] = 1
|
||||
return newName
|
||||
}
|
||||
|
||||
fun removeInternedStringsFromRemovedSubroutine(sub: Subroutine) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
Prog8 compiler v12.2-BETA2 by Irmen de Jong (irmen@razorvine.net)
|
||||
Prerelease version from git commit 1dbaa6e0 in branch master
|
||||
Prog8 compiler v12.2 by Irmen de Jong (irmen@razorvine.net)
|
||||
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
|
||||
|
||||
Compiling program import-all-c128.p8
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
Prog8 compiler v12.2-BETA2 by Irmen de Jong (irmen@razorvine.net)
|
||||
Prerelease version from git commit 1dbaa6e0 in branch master
|
||||
Prog8 compiler v12.2 by Irmen de Jong (irmen@razorvine.net)
|
||||
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
|
||||
|
||||
Compiling program import-all-c64.p8
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
Prog8 compiler v12.2-SNAPSHOT by Irmen de Jong (irmen@razorvine.net)
|
||||
Prerelease version from git commit 8bf744cc in branch master
|
||||
Prog8 compiler v12.2 by Irmen de Jong (irmen@razorvine.net)
|
||||
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
|
||||
|
||||
Compiling program import-all-cx16.p8
|
||||
@@ -765,14 +764,7 @@ psg2 {
|
||||
const ubyte SAWTOOTH
|
||||
const ubyte SQUARE
|
||||
const ubyte TRIANGLE
|
||||
ubyte[] envelope_attacks
|
||||
ubyte[] envelope_maxvolumes
|
||||
ubyte[] envelope_releases
|
||||
ubyte[] envelope_states
|
||||
ubyte[] envelope_sustains
|
||||
uword[] envelope_volumes
|
||||
^^psg2.Voice voices
|
||||
^^psg2.Voice vptr
|
||||
envelope (ubyte voice_num, ubyte attack, ubyte sustain, ubyte release)
|
||||
frequency (ubyte voice_num, uword freq)
|
||||
getvoice (ubyte voice_num) -> ^^psg2.Voice
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
Prog8 compiler v12.2-BETA2 by Irmen de Jong (irmen@razorvine.net)
|
||||
Prerelease version from git commit 1dbaa6e0 in branch master
|
||||
Prog8 compiler v12.2 by Irmen de Jong (irmen@razorvine.net)
|
||||
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
|
||||
|
||||
Compiling program import-all-pet32.p8
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
Prog8 compiler v12.2-BETA2 by Irmen de Jong (irmen@razorvine.net)
|
||||
Prerelease version from git commit 1dbaa6e0 in branch master
|
||||
Prog8 compiler v12.2 by Irmen de Jong (irmen@razorvine.net)
|
||||
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
|
||||
|
||||
Compiling program import-all-virtual.p8
|
||||
@@ -612,7 +611,12 @@ cx16 {
|
||||
&word r9s
|
||||
&byte r9sH
|
||||
&byte r9sL
|
||||
getrambank () -> ubyte
|
||||
getrombank () -> ubyte
|
||||
numbanks () -> uword
|
||||
rambank (ubyte bank)
|
||||
restore_virtual_registers ()
|
||||
rombank (ubyte bank)
|
||||
save_virtual_registers ()
|
||||
}
|
||||
|
||||
|
||||
@@ -124,8 +124,7 @@ Foreign function interface (external/ROM calls)
|
||||
.. index:: single: Subroutines; Foreign/ROM
|
||||
|
||||
- You can use the ``extsub`` keyword to define the call signature of foreign functions (ROM routines or external routines elsewhere in RAM) in a natural way.
|
||||
Calling those generates code that is as efficient or even more efficient as calling regular subroutines.
|
||||
No additional stubs are needed.
|
||||
Calling those generates code that is as efficient or even more efficient as calling regular subroutines. No additional stubs are needed.
|
||||
- High level support of memory banking: an ``extsub`` can be defined with the memory bank number (constant or variable) where the routine is located in,
|
||||
and then when you call it as usual, the compiler takes care of the required bank switching.
|
||||
|
||||
|
||||
@@ -220,6 +220,8 @@ One or more .p8 module files
|
||||
|
||||
``-libsearch pattern``
|
||||
Search in the embedded library files for occurrences of the given regex pattern.
|
||||
If no exact matches are found, the compiler automatically attempts a "fuzzy" search
|
||||
by inserting wildcards between the characters of the search pattern.
|
||||
|
||||
``-libdump path``
|
||||
Extract all embedded library files into the given output directory.
|
||||
|
||||
+22
-2
@@ -54,6 +54,12 @@ Summary of Major Language Milestones
|
||||
| 12.0 | **``long`` datatype**, **structs reintroduced**, |
|
||||
| | **typed pointers** |
|
||||
+-----------+-------------------------------------------------------------+
|
||||
| 12.1 | ``swap()`` builtin, ``psg2`` module, PET32 floats/gfx/snd, |
|
||||
| | ``strings.split()``/``next_token()``, C128 2MHz mode |
|
||||
+-----------+-------------------------------------------------------------+
|
||||
| 12.2 | Long loop support, ``private``, enum syntax, 2D arrays, |
|
||||
| | new modules (serial, wavfile, adpcm, lineclip) |
|
||||
+-----------+-------------------------------------------------------------+
|
||||
|
||||
|
||||
Breaking Changes Summary
|
||||
@@ -72,6 +78,8 @@ Major breaking changes that require code modifications when upgrading:
|
||||
- **v10.2**: Stricter boolean types
|
||||
- **v10.4**: Namespace reorganization (``cx16`` to ``cbm``)
|
||||
- **v12.0**: Structs reintroduced (different from v1.11), typed pointers
|
||||
- **v12.1**: Combined virtual register renames (``R0R1_32`` → ``R0R1``, etc.)
|
||||
- **v12.2**: ``swap()`` is now a statement (not a function), ``math.crc16()`` and ``math.crc16_start()`` require new parameters, ``private`` is now a reserved keyword
|
||||
|
||||
|
||||
2019–2022 — Early Development (Python to Kotlin Transition)
|
||||
@@ -216,15 +224,27 @@ Major breaking changes that require code modifications when upgrading:
|
||||
- **Float to int casts** (truncating)
|
||||
- **Double buffering** in monogfx module
|
||||
|
||||
**v12.0–v12.2** — November 2024–Latest
|
||||
**v12.0–v12.1** — November 2024–February 2026
|
||||
- **``long`` datatype** — native 32-bit signed integers
|
||||
- **Structs reintroduced** — grouping multiple fields together
|
||||
- **Typed pointers** — can point to structs and specific types (not just ``uword`` addresses)
|
||||
- **PET32 modules**: ``petgfx``, ``petsnd``, ``diskio``
|
||||
- **New builtins**: ``swap()``, ``offsetof()``
|
||||
- **New builtin**: ``swap()``
|
||||
- **C128 enhancements**: ``fast()``/``slow()`` for 2MHz mode
|
||||
- **String operations**: case-insensitive comparison, ``strings.split()``, ``strings.next_token()``
|
||||
- **Virtual target improvements**: ``f_seek()``, ``f_tell()``, ``diskname()``, ``loadlib()``
|
||||
|
||||
**v12.2** — May 2026
|
||||
- **Long loop support** — for-loops with long counters on 6502, long math (division, sqrt, min/max/clamp/abs)
|
||||
- **``private`` keyword** — for structs, enums, aliases, variables, and subroutines
|
||||
- **Const pointers** — compile-time constant-folding for const pointers
|
||||
- **``enum`` syntax** — concise constant list declarations
|
||||
- **2D array support** — ``ubyte[3][4] matrix``
|
||||
- **``swap()`` is now a statement** — more efficient than the previous builtin function
|
||||
- **``math.crc16()`` signature changed** — now requires ``initvalue`` and ``xorout`` parameters for flexibility
|
||||
- **New compiler options**: ``-daemon`` (IDE integration), ``-nostdlib``
|
||||
- **New modules**: ``serial`` (CX16 UART+ZiModem), ``lineclip``, ``wavfile``, ``adpcm``
|
||||
- **``-libsearch`` fuzzy search fallback** — finds libraries even with partial names
|
||||
|
||||
|
||||
*This document summarizes major and minor releases. Bugfix releases (e.g., v12.0.1, v12.1.1) are omitted for brevity.*
|
||||
@@ -1156,9 +1156,52 @@ Available for the Cx16 target.
|
||||
Contains an abstraction for the Vera's PSG (programmable sound generator) to play simple waveforms.
|
||||
It has better consistent envelope timings than the older ``psg`` module, and easier access to all voice parameters if desired.
|
||||
It includes an interrupt handler routine for handling automatic ASR volume envelopes as well.
|
||||
|
||||
Envelope timing (one tick = one call to ``update()``):
|
||||
|
||||
- Attack and Release are linear volume ramps: ``duration_ticks = ceil(256 / speed)``
|
||||
- Higher speed = faster ramp (shorter duration)
|
||||
- speed=1 -> 256 ticks; speed=4 -> 64 ticks; speed=255 -> 1 tick
|
||||
- Even speed=255 applies on the *next* call to ``update()`` — there is always at least ~1 frame of delay if you depend on the normal update cycle.
|
||||
- For truly immediate changes (0ms), set the voice struct fields and call ``update()`` directly.
|
||||
- Sustain holds for ``speed`` ticks then transitions to Release
|
||||
- speed=0 stalls at that phase (useful for infinite sustain)
|
||||
- Actual real-time duration depends on how often ``update()`` is called.
|
||||
If called every vsync at 60Hz, each tick is ~16.7ms.
|
||||
|
||||
Available routines:
|
||||
|
||||
``init ()``
|
||||
Initialize the module, set all voices to default off state.
|
||||
|
||||
``off ()``
|
||||
Turn off all voices immediately, then call ``update`` automatically.
|
||||
|
||||
``voice (voice_num, channels, volume, waveform, pulsewidth)``
|
||||
Set all parameters for a voice (frequency unchanged). Disables any active envelope.
|
||||
|
||||
``frequency (voice_num, freq)``
|
||||
Set frequency word for the voice. This is the raw VERA PSG register value (no scaling, written directly to the hardware): ``freq = HERZ(Hz) / 0.3725290298461914``.
|
||||
|
||||
``volume (voice_num, vol)``
|
||||
Set volume directly (0-63). Disables any active envelope. Also sets the max volume for subsequent envelope use.
|
||||
|
||||
``envelope (voice_num, attack, sustain, release)``
|
||||
Start ASR envelope on the voice (volume starts from 0). ``attack`` and ``release`` are speeds 0-255, ``sustain`` is duration in ticks.
|
||||
Speed 255 means "instant".
|
||||
|
||||
``getvoice (voice_num) -> ^Voice``
|
||||
Return pointer to a voice's parameter struct (for direct field access).
|
||||
|
||||
``update () -> bool``
|
||||
Advance all active envelopes by one tick, then write all 16 voices to the VERA PSG registers. This call is IRQ-safe!
|
||||
|
||||
Waveform constants: ``PULSE``, ``SAWTOOTH``, ``TRIANGLE``, ``NOISE``
|
||||
Channel constants: ``LEFT``, ``RIGHT``, ``BOTH``, ``DISABLED``
|
||||
|
||||
See the examples/cx16/bdmusic.p8 program for ideas how to use it.
|
||||
|
||||
Read the :source:`psg source code <compiler/res/prog8lib/cx16/psg2.p8>` to see what's in there.
|
||||
Read the :source:`psg2 source code <compiler/res/prog8lib/cx16/psg2.p8>` to see everything that's in there.
|
||||
|
||||
|
||||
serial (cx16 only)
|
||||
|
||||
@@ -19,7 +19,9 @@ Weird Heisenbug
|
||||
|
||||
Future Things and Ideas
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
- make enums strongly typed instead of just syntactic sugar for ints (see enum-strong-type.md for the plan)
|
||||
- make enums strongly typed instead of just syntactic sugar for ints (see ideas/enum-strong-type.md for the plan)
|
||||
- arrays inline in structs (see ideas/arrays-in-structs.md)
|
||||
- add %option private_symbols to make access mode private by default; need (new) 'public' keyword to explicitly mark symbols public.
|
||||
- symboldump: some sort of javadocs generated from the p8 source files (instead of just the function signatures). Use markdown for formatting, not html.
|
||||
- why are (interned) strings stored as initialization value in the SymbolTable AND as string nodes in the interned string block? Something seems redundant here?
|
||||
- when implementing unsigned longs: remove the (multiple?) "TODO "hack" to allow unsigned long constants to be used as values for signed longs, without needing a cast
|
||||
@@ -35,7 +37,7 @@ Future Things and Ideas
|
||||
- sizeof(pointer) is now always 2 (an uword), make this a variable in the ICompilationTarget so that it could be 4 at the time we might ad a 32-bits 68000 target for example. Much code assumes word size addresses though.
|
||||
- add float support to the configurable compiler targets. Restrictions: just have "cbm-style floats" as an option (to that it can slot into the current float codegen), where all you have to specify is the addresses of AYINT and GIVAYF and FADDT and all their friends.
|
||||
- Change scoping rules for qualified symbols so that they don't always start from the root but behave like other programming languages (look in local scope first), maybe only when qualified symbol starts with '.' such as: .local.value = 33
|
||||
- something to reduce the need to use fully qualified names all the time. 'with' ? Or 'using <prefix>'?
|
||||
- something to reduce the need to use fully qualified names all the time. 'with <prefix>' or 'using <prefix>'?
|
||||
- Improve register load order in subroutine call args assignments:
|
||||
in certain situations (need examples!), the "wrong" order of evaluation of function call arguments is done which results
|
||||
in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!)
|
||||
@@ -65,7 +67,6 @@ Romable (%option romable)
|
||||
|
||||
IR/VM
|
||||
^^^^^
|
||||
- getting it in shape for code generation: the IR file should be able to encode every detail about a prog8 program (the VM doesn't have to actually be able to run all of it though!)
|
||||
- maybe change all branch instructions to have 2 exits (label if branch condition true, and label if false) instead of 1, and get rid of the implicit "next code chunk" link between chunks.
|
||||
- implement more TODOs in AssignmentGen?
|
||||
- add more optimizations in IRPeepholeOptimizer?
|
||||
|
||||
+2
-10
@@ -1,18 +1,10 @@
|
||||
%import textio
|
||||
%zeropage basicsafe
|
||||
%option no_sysinit
|
||||
%encoding iso
|
||||
|
||||
main {
|
||||
sub start() {
|
||||
long @shared @nozp size = 147320
|
||||
while size>0 {
|
||||
size--
|
||||
if size & $ff == 0 {
|
||||
txt.chrout('.')
|
||||
}
|
||||
}
|
||||
txt.nl()
|
||||
;;sys.poweroff_system()
|
||||
const ^^ubyte name = "irmen"
|
||||
txt.print(name)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -4,4 +4,4 @@ org.gradle.parallel=true
|
||||
org.gradle.daemon=true
|
||||
org.gradle.configuration-cache=false
|
||||
kotlin.code.style=official
|
||||
version=12.2-BETA3
|
||||
version=12.2
|
||||
@@ -0,0 +1,282 @@
|
||||
|
||||
# Requirements
|
||||
|
||||
### Overview & Goals
|
||||
Support inline arrays by-value inside struct fields. Currently, struct fields can only hold scalar types (bool, byte, ubyte, word, uword, long, float) and pointers. Arrays embedded directly in a struct's memory layout are not supported. The goal is to allow:
|
||||
|
||||
```prog8
|
||||
struct Entry {
|
||||
ubyte age
|
||||
ubyte[10] name ; 10 bytes inline, not a pointer
|
||||
word[4] scores
|
||||
}
|
||||
```
|
||||
|
||||
When a struct instance is created, the array data is stored contiguously within the struct's memory block, not as a separate pointer to external data. This differs from `str` fields, which only store a 2-byte pointer.
|
||||
|
||||
### Scope
|
||||
|
||||
#### In Scope
|
||||
- 1D arrays of basic types (`ubyte[N]`, `word[N]`, `uword[N]`, `long[N]`, `float[N]`, `bool[N]`) as struct fields
|
||||
- `@nosplit` tag for word arrays in structs
|
||||
- `ubyte[][] name` (empty array sig, size inferred from initializer)
|
||||
- Reading and writing individual elements: `entry.name[i] = val`, `val = entry.name[i]`
|
||||
- Initialization of struct instances with array fields via `^^Entry : [...]`
|
||||
- `sizeof(Entry)` and `offsetof(Entry.name)` for structs with array fields
|
||||
- Struct total size still limited to 256 bytes
|
||||
|
||||
#### Out of Scope
|
||||
- 2D arrays as struct fields initially (but grammar can accept them for future use)
|
||||
- Entire-array operations on array fields (e.g., `copy entry.name, source` on the field) -- use element-by-element copying
|
||||
- Arrays of structs inside structs
|
||||
- `str` fields changed from pointer to inline (keep `str` as pointer)
|
||||
- Variable-length array fields
|
||||
- Array fields of struct pointer types (`^^Foo[N]`)
|
||||
|
||||
### User Stories
|
||||
- As a Prog8 developer, I want to store small fixed-size arrays inline in structs so that I can model data structures like file system entries, game objects with stats, or C-style structs without needing separate memory allocations.
|
||||
- As a Prog8 developer, I want `sizeof` and `offsetof` to correctly reflect the full inline storage of array fields so that I can use them in assembly and memory layout calculations.
|
||||
- As a Prog8 developer, I want to read and write individual array elements in a struct field using natural `instance.field[i]` syntax.
|
||||
|
||||
|
||||
# Technical Design
|
||||
|
||||
### Current Implementation
|
||||
Struct fields are stored as `Array<Pair<DataType, String>>` in `StructDecl` (compiler AST) and `List<Pair<DataType, String>>` in `StStruct` (simple AST). The `DataType` does not store array dimensions; that information lives separately in `VarDecl.arraysize`. For structs, there is no such separate storage.
|
||||
|
||||
Key restrictions currently in place:
|
||||
1. **Grammar** (`Prog8ANTLR.g4:174`): `structfielddecl: datatype identifierlist` -- no array index syntax
|
||||
2. **AstChecker** (`AstChecker.kt:2301`): rejects non-basic, non-numeric, non-pointer field types
|
||||
3. **Memory layout** (`StructDecl.memsize()`:527, `offsetof()`:533): passes `numElements=1` to `sizer.memorySize()` -- correct for scalar fields, but for array fields would need to pass the actual array dimension instead
|
||||
|
||||
Field access `ptr.field[idx]` on a struct pointer hits an error/TODO at `AstChecker.kt:1583-1605` because the `.` operator with an array-indexed right side is not handled for struct pointers.
|
||||
|
||||
### Key Decisions
|
||||
- **`StructField` data class**: Introduce a proper data class for fields to carry array dimension alongside type and name, rather than continuing with `Pair<DataType, String>`. This provides a clean extension point and avoids parallel arrays.
|
||||
- **Grammar mirrors `vardecl`**: The `(arrayindex arrayindex? | EMPTYARRAYSIG)?` suffix follows the exact pattern used for variable declarations, keeping the language consistent.
|
||||
- **Array dimension in DataType**: Use `DataType.elementToArray()` to create array types (with `isArray=true`, `sub=elementType`) for array fields, just like `VarDecl` does. The concrete dimension lives in `StructField.arraySize`.
|
||||
- **Element-level access only**: `ptr.field` on its own (without index) is not a valid rvalue for array fields; you must write `ptr.field[i]`. This keeps code generation simple and avoids the need for struct-field-as-array-reference semantics.
|
||||
- **Initialization**: Use nested ArrayLiteral values for array fields in `^^Type : [...]` initializer. The invariant `args.size == fields.size` is preserved -- each field gets one argument. For `ubyte[N]` fields, string literals are also accepted as initializer shorthand, auto-expanded to individual bytes.
|
||||
|
||||
### Proposed Changes
|
||||
|
||||
#### 1. Grammar (`Prog8ANTLR.g4:174`)
|
||||
```
|
||||
- structfielddecl: datatype identifierlist ;
|
||||
+ structfielddecl: datatype (arrayindex arrayindex? | EMPTYARRAYSIG)? identifierlist ;
|
||||
```
|
||||
|
||||
#### 2. `StructField` data class (new, or inline in `StructDecl`)
|
||||
```kotlin
|
||||
data class StructField(
|
||||
val type: DataType,
|
||||
val name: String,
|
||||
val arraySize: ArrayIndex? = null // null = scalar
|
||||
) {
|
||||
val isArray: Boolean get() = arraySize != null
|
||||
}
|
||||
```
|
||||
|
||||
**`StructDecl` changes** (`AstStatements.kt`):
|
||||
- `fields: Array<StructField>` replaces `Array<Pair<DataType, String>>`
|
||||
- `memsize()`: element-count-aware: `if(field.isArray) sizer.memorySize(field.type, resolvedSize) else sizer.memorySize(field.type, 1)`
|
||||
- `offsetof()`: same treatment for field offset accumulation
|
||||
- `getFieldType()`: unchanged (already delegates to `DataType`)
|
||||
- `copy()`, `sameas()`: updated for new field type
|
||||
- `offsetof()` constraint: field offsets must remain ≤ 255 (Y-register limit)
|
||||
|
||||
#### 3. `Antlr2KotlinVisitor.getStructField()` (`Antlr2KotlinVisitor.kt:801`)
|
||||
- Read `ctx.arrayindex()` from context
|
||||
- If array indices present, convert base DataType via `elementToArray()` (same as line 616/926)
|
||||
- Return `StructField(dt, name, arraySize)` instead of `Pair<DataType, String>`
|
||||
|
||||
#### 4. `StStruct` changes (`SymbolTable.kt`)
|
||||
- `fields: List<StructField>` replaces `List<Pair<DataType, String>>`
|
||||
- `getField()`: compute offsets with element-count-aware `sizer.memorySize()`
|
||||
- Update `size` calculation in constructor and `SymbolTableMaker`
|
||||
|
||||
#### 5. AstChecker changes (`AstChecker.kt`)
|
||||
|
||||
**Field type validation (line 2301)**:
|
||||
```kotlin
|
||||
struct.fields.forEach { field ->
|
||||
val dt = field.type
|
||||
val allowed = dt.isBasic
|
||||
|| dt.base.isNumericOrBool
|
||||
|| dt.isPointer
|
||||
|| (dt.isArray && dt.sub!!.isNumericOrBool)
|
||||
if(!allowed)
|
||||
errors.err("only booleans, numeric and pointer fields allowed in a struct: '${field.name}'", struct.position)
|
||||
}
|
||||
```
|
||||
|
||||
**`.` operator with array field (lines 1583-1605)**:
|
||||
Handle the case where `left` is a struct pointer and `right` is `ArrayIndexedExpression("field", idx)`. Validate the field exists and is an array type. Set the result type to the element type of the array.
|
||||
|
||||
#### 6. 6502 Code Generator (`PointerAssignmentsGen.kt`)
|
||||
|
||||
`deref()` and `operatorDereference()` for `ptr.field[idx]`:
|
||||
- Compute address as `base_pointer + field_offset + (index * element_size)`
|
||||
- If `field_offset + (max_index * element_size) > 255`, the Y-register can't hold the full offset; emit code to add the high byte to the pointer
|
||||
- This is already handled for general array indexing on pointers; struct field offset just needs to be added to the base
|
||||
|
||||
#### 7. IR Code Generator (`ExpressionGen.kt`, `AssignmentGen.kt`)
|
||||
|
||||
`traverseRestOfDerefChainToCalculateFinalAddress()`:
|
||||
- When a field in the deref chain is an array and has an index, add `index * element_size` to the computed address
|
||||
- Scale the index by element size before adding to the field offset
|
||||
|
||||
#### 8. StaticStructInitializer changes (`AstChecker.kt:2681`)
|
||||
|
||||
Current validation at line 2698 enforces `args.size == struct.fields.size`. With array fields, the invariant is preserved: each array field receives a single initializer value which is an ArrayLiteral (or string literal for `ubyte[N]`).
|
||||
|
||||
- Accept ArrayLiteral or string literal as initializer value for array fields
|
||||
- Validate element count in ArrayLiteral matches the array dimension
|
||||
- For `ubyte[N]` fields, accept a string literal as shorthand; expand/crop to N bytes
|
||||
- Allow `[]` (empty array sig) for uninitialized array fields
|
||||
- Update `SymbolTableMaker.handleStructAllocation()` to process nested initializer values
|
||||
|
||||
Example:
|
||||
```prog8
|
||||
struct Entry {
|
||||
ubyte age
|
||||
ubyte[10] name
|
||||
word[3] scores
|
||||
}
|
||||
|
||||
^^Entry entry = ^^Entry : [25, "Hello", [100, 200, 300]]
|
||||
```
|
||||
|
||||
#### 9. `AstToplevel.searchStructFieldRef()` (line 227)
|
||||
- Uses `getFieldType()` which returns `DataType` already -- should work if `getFieldType` returns the array DataType
|
||||
- The `StructFieldRef` node gets the field's type; subsequent array indexing should resolve the element type
|
||||
|
||||
#### 10. `AstToSourceTextConverter`
|
||||
- Update `visit(StructDecl)` to emit array dimensions for struct fields
|
||||
|
||||
### Data Models
|
||||
|
||||
```kotlin
|
||||
data class StructField(
|
||||
val type: DataType,
|
||||
val name: String,
|
||||
val arraySize: ArrayIndex? = null
|
||||
)
|
||||
```
|
||||
|
||||
Used in:
|
||||
- `StructDecl.fields: Array<StructField>` (compiler AST)
|
||||
- `StStruct.fields: List<StructField>` (simple AST)
|
||||
- `Antlr2KotlinVisitor.getStructField()` return type
|
||||
|
||||
The `ArrayIndex` class already exists (`AstStatements.kt:600`) with `indexExpr: Expression` and supports const-evaluation.
|
||||
|
||||
|
||||
# Migration / Compatibility
|
||||
|
||||
No breaking changes to existing code. All existing struct declarations continue to work unchanged. The change is purely additive:
|
||||
- Existing scalar/pointer fields: `isArray=false` in `StructField`, behavior identical to current `Pair<DataType, String>`
|
||||
- New array fields: only accessible when explicitly declared with `type[N] varname`
|
||||
|
||||
Existing `AstChecker` tests that check the "only booleans, numeric and pointer fields" error message need updating to also permit array types.
|
||||
|
||||
Existing tests accessing `struct.fields[i].first` / `.second` need updating to use `.type` / `.name` instead (or add backward-compat extension properties).
|
||||
|
||||
|
||||
# Testing
|
||||
|
||||
### Validation Approach
|
||||
Primary testing uses the **virtual target** and prog8c's IR debugging features. This allows fast iteration without needing 6502 emulators.
|
||||
|
||||
**Debugging workflow for each change:**
|
||||
1. **Syntax check**: `prog8c -check input.p8` -- quick semantic validation
|
||||
2. **AST inspection**: `prog8c -target virtual -printast1 input.p8` -- inspect compiler AST after parsing
|
||||
3. **IR inspection**: `prog8c -target virtual -out /tmp/out input.p8` then examine the generated `.p8ir` file
|
||||
4. **IR comparison**: `prog8c -target virtual -compareir baseline.p8ir new.p8ir` -- compare IR instruction changes
|
||||
5. **VM execution**: `prog8c -target virtual -emu input.p8` -- run and see stdout output
|
||||
6. **VM trace**: `prog8c -vm input.p8ir -vmtrace` -- step through IR instructions with location tracking
|
||||
7. **Disable optimizations**: `prog8c -target virtual -noopt -emu input.p8` -- isolate optimizer issues
|
||||
8. **Full regression**: `gradle build --console=plain` -- run all unit tests
|
||||
|
||||
**Important**: Always test with `-noopt` first to isolate optimizer issues from code generation bugs. Use `-compareir` to see what instructions change when optimizations are applied.
|
||||
|
||||
Final verification against the existing test suite via `gradle build --console=plain`.
|
||||
|
||||
### Key Scenarios
|
||||
|
||||
| Scenario | Test |
|
||||
|----------|------|
|
||||
| Basic array field | `struct S { ubyte[4] data }` -- create instance, write/read elements |
|
||||
| Word array | `struct S { word[3] vals }` -- test split word and `@nosplit` |
|
||||
| Mixed fields | `struct S { ubyte id; ubyte[8] name; word count }` -- correct offsets |
|
||||
| Nested struct with array | `struct Inner { ubyte[4] data }` then `struct Outer { ^^Inner ptr }` |
|
||||
| sizeof/offsetof | `sizeof(S)` = 8 for `ubyte[8]`, `offsetof(S.data)` = 1 if preceded by `ubyte id` |
|
||||
| Initialization | `^^S : [1, 'a','b','c','d', 1000]` -- array field from byte values |
|
||||
| Error: too large | `struct S { ubyte[300] data }` -- rejects (> 256 bytes) |
|
||||
| Error: invalid type | `struct S { str[3] names }` -- rejects arrays of strings (complex) |
|
||||
| Error: non-const array size | `struct S { ubyte[n] data }` -- rejects non-constant dimensions |
|
||||
| Field offset > 255 | struct with carefully laid out array fields that push offset past 255 (requires extra pointer math) |
|
||||
| Existing structs unchanged | All existing struct tests continue to pass |
|
||||
|
||||
### Test Changes
|
||||
- New VM test class or extend `TestCompilerVirtual.kt` for array-in-struct runtime tests
|
||||
- New codegen test in `TestExecution6502` for 6502 code generation with array fields
|
||||
- Update `TestPointers.kt` struct tests if field access patterns change
|
||||
- Update `TestAstChecks.kt` for relaxed field type validation
|
||||
- Update `TestConst.kt` for `sizeof`/`offsetof` with array fields
|
||||
|
||||
|
||||
# Delivery Steps
|
||||
|
||||
### Step 1: Grammar, Parser, and AST Changes
|
||||
The parser and AST now support array index declarations in struct fields.
|
||||
|
||||
- Update `structfielddecl` grammar rule in `Prog8ANTLR.g4`.
|
||||
- Create `StructField` data class to manage array dimensions.
|
||||
- Update `StructDecl` to store `StructField`s.
|
||||
- Update `Antlr2KotlinVisitor.getStructField()` to process array indices.
|
||||
- Update `AstToSourceTextConverter` to emit array dimensions.
|
||||
- **Verify**: parser successfully parses `struct S { ubyte[10] name }`.
|
||||
|
||||
### Step 2: Semantic Analysis and Validation
|
||||
The compiler correctly validates array field types and struct initializers.
|
||||
|
||||
- Relax field type check in `AstChecker.struct` visitor to permit array types.
|
||||
- Handle `.` operator with `field[idx]` for struct pointers.
|
||||
- Handle `StaticStructInitializer` validation for array field initializers.
|
||||
- Update `AstToplevel.searchStructFieldRef()` for proper symbol resolution.
|
||||
- **Verify**: valid structs compile, and invalid declarations produce appropriate errors.
|
||||
|
||||
### Step 3: Memory Layout and Symbol Table
|
||||
Struct field offsets and total sizes correctly account for array fields.
|
||||
|
||||
- Update `StructDecl.memsize()` and `offsetof()` to compute array field sizes.
|
||||
- Update `StStruct` and `SymbolTableMaker` to handle array field storage.
|
||||
- **Verify**: `sizeof` and `offsetof` return correct values based on element counts.
|
||||
|
||||
### Step 4: 6502 Code Generation
|
||||
The 6502 backend correctly handles array field offsets in pointer assignments.
|
||||
|
||||
- Update `PointerAssignmentsGen` to handle array field offsets with indexing.
|
||||
- Handle offset > 255 case by emitting extra pointer math.
|
||||
- Verify generated 64tass `.struct` directives and `.dstruct` data layouts.
|
||||
- **Verify**: compiled programs run correctly in the 6502 simulator/emulator.
|
||||
|
||||
### Step 5: IR + VM Code Generation
|
||||
The IR generator correctly calculates addresses for array field indexed access.
|
||||
|
||||
- Update IR expression and assignment generators to handle array field access.
|
||||
- **Verify using IR debugging workflow**:
|
||||
- `prog8c -check` for syntax errors.
|
||||
- `prog8c -target virtual -printast1` to inspect AST nodes.
|
||||
- `prog8c -target virtual -out` then inspect `.p8ir` for correct indexing opcodes.
|
||||
- `prog8c -target virtual -emu` to run and verify output.
|
||||
- `prog8c -vm file.p8ir -vmtrace` to trace execution and verify field access addresses.
|
||||
|
||||
### Step 6: Testing
|
||||
All new functionality is covered by automated unit and integration tests.
|
||||
|
||||
- Add VM runtime tests for array-in-struct field access.
|
||||
- Add 6502 simulator-based tests.
|
||||
- Add error-case coverage for invalid field declarations.
|
||||
- Run full suite: `gradle build --console=plain`.
|
||||
@@ -41,6 +41,21 @@ Currently, `Enumeration` nodes in the `compilerAst` are lowered into standard co
|
||||
# Testing
|
||||
|
||||
### Validation Approach
|
||||
Primary testing uses the **virtual target** and prog8c's IR debugging features. This allows fast iteration without needing 6502 emulators.
|
||||
|
||||
**Debugging workflow for each change:**
|
||||
1. **Syntax check**: `prog8c -check input.p8` -- quick semantic validation
|
||||
2. **AST inspection**: `prog8c -target virtual -printast1 input.p8` -- inspect compiler AST (verify enum nodes are preserved)
|
||||
3. **Simple AST inspection**: `prog8c -target virtual -printast2 input.p8` -- inspect Simple AST (verify enum erasure to integers)
|
||||
4. **IR inspection**: `prog8c -target virtual -out /tmp/out input.p8` then examine the generated `.p8ir` file
|
||||
5. **IR comparison**: `prog8c -target virtual -compareir baseline.p8ir new.p8ir` -- compare IR instruction changes when optimizing
|
||||
6. **VM execution**: `prog8c -target virtual -emu input.p8` -- run and see stdout output
|
||||
7. **VM trace**: `prog8c -vm input.p8ir -vmtrace` -- step through IR instructions with location tracking
|
||||
8. **Disable optimizations**: `prog8c -target virtual -noopt -emu input.p8` -- isolate optimizer issues
|
||||
9. **Full regression**: `gradle build --console=plain` -- run all unit tests
|
||||
|
||||
**Important**: Always test with `-noopt` first to isolate optimizer issues from code generation bugs. Use `-compareir` to see what instructions change when optimizations are applied.
|
||||
|
||||
- **Type Checking Tests**: Add test cases that explicitly verify:
|
||||
- Implicit assignment/comparison of incompatible types (expect failure).
|
||||
- Explicit cast (Enum ↔ Int) (expect success).
|
||||
@@ -49,3 +64,25 @@ Currently, `Enumeration` nodes in the `compilerAst` are lowered into standard co
|
||||
- Use an enum member without proper type qualification.
|
||||
- **AST Printing Verification**: Use `-printast1` and `-printast2` to verify that strongly typed enum nodes are correctly displayed in the AST output.
|
||||
- **Runtime Verification**: Ensure that the code generation produces correct code for enums (e.g., correct values, proper size) using the `virtual` target and `vmtrace`.
|
||||
|
||||
Final verification against the existing test suite via `gradle build --console=plain`.
|
||||
|
||||
### Delivery Plan
|
||||
|
||||
### Stage 1: Add Enum Type Support and Erasure Logic
|
||||
Enum types are introduced and correctly preserved in the compiler AST.
|
||||
|
||||
- Implement `EnumSubType` class in `codeCore` and add `BaseDataType.ENUM`.
|
||||
- Update `CompilerAST` and `SimplifiedAstMaker` to preserve and later erase enum declarations.
|
||||
|
||||
### Stage 2: Implement Strict Type Checking and Casting
|
||||
Strong typing for enums is enforced by the compiler.
|
||||
|
||||
- Update `TypeChecker` and `SymbolTable` to enforce strict type checking for enums.
|
||||
- Implement explicit casting rules for enums and disallow implicit/cross-enum conversions.
|
||||
|
||||
### Stage 3: Verification and Testing
|
||||
Enum type implementation is verified with unit and integration tests.
|
||||
|
||||
- Update AST printing for enums.
|
||||
- Add unit tests for type safety, casting, and AST representation verification.
|
||||
@@ -1,101 +0,0 @@
|
||||
|
||||
# Requirements
|
||||
|
||||
### Overview & Goals
|
||||
Extend the Prog8 `Inliner` to support subroutines with parameters for multi-value returns and expression context calls. Additionally, enable the `inline` keyword for regular subroutines, allowing developers to explicitly suggest inlining for performance-critical code. Currently, the inliner is limited to parameterless subroutines in expression context, and the `inline` keyword is forcefully disabled for regular subroutines with a misleading warning.
|
||||
|
||||
### Scope
|
||||
#### In Scope
|
||||
- Parameterized subroutines in multi-value return assignments (e.g., `a, b = foo(x, y)`).
|
||||
- Parameterized subroutines in expression context (e.g., `a = foo(x) + 1`).
|
||||
- Substitution of parameter references with actual argument values (literals or simple identifiers).
|
||||
- Expansion of supported return expressions to include simple binary/prefix/typecast operations.
|
||||
- Enabling `inline` keyword support for regular subroutines by removing the restriction in `AstChecker`.
|
||||
- Ensuring `Inliner` properly processes manually marked `inline` subroutines (including scoping analysis).
|
||||
|
||||
#### Out of Scope
|
||||
- Support for complex arguments with side effects (calls like `foo(bar())` will still be excluded from inlining).
|
||||
- Multi-statement subroutines in expression context.
|
||||
|
||||
### User Stories
|
||||
- As a Prog8 developer, I want to use `inline` subroutines with parameters and return values so that I can write clean, modular code without the performance penalty of subroutine calls.
|
||||
- As a Prog8 developer, I want the `inline` keyword to be respected for regular subroutines, so I can explicitly optimize specific parts of my code.
|
||||
- As a compiler, I want to automatically inline simple parameterized subroutines to reduce execution time and code size.
|
||||
|
||||
|
||||
# Technical Design
|
||||
|
||||
### Current Implementation
|
||||
The `Inliner` uses a two-phase approach:
|
||||
1. `DetermineInlineSubs` identifies simple subroutines and marks them as `inline`.
|
||||
2. The `Inliner` itself visits call sites and replaces calls to `inline` subroutines with their bodies.
|
||||
|
||||
Currently, `AstChecker` disables `inline` for regular subroutines and emits a warning stating it has no effect. However, the `Inliner` already performs automatic inlining for simple regular subroutines, making this warning misleading. Furthermore, the `Inliner` explicitly rejects parameterized subroutines in expression context.
|
||||
|
||||
### Key Decisions
|
||||
- **Parameter Substitution**: Use a specialized `AstWalker` (`ParameterSubstitutor`) to replace parameter references in the inlined body with copies of the call arguments.
|
||||
- **Simple Arguments Only**: Maintain the restriction that arguments must be `NumericLiteral` or `IdentifierReference` to safely allow duplication without side-effect issues.
|
||||
- **Scoping**: Ensure `makeFullyScoped` is called on all inlineable subroutines (including those manually marked `inline`) to prevent name collisions after inlining.
|
||||
- **Respect `inline` Keyword**: Stop unsetting the `inline` flag in `AstChecker` for regular subroutines. This allows the user to suggest inlining even for cases that might not be picked up by the automatic inliner's heuristic (though they must still meet basic inlineability criteria).
|
||||
|
||||
### Proposed Changes
|
||||
#### `Inliner.kt`
|
||||
- **`ParameterSubstitutor`**: A new private class to perform the replacement of parameters with arguments.
|
||||
- **`substitute(node, paramMap)`**: A helper function to apply substitution to a copied node.
|
||||
- **`after(Assignment)`**: Implement zipping of `sub.parameters` and `fcall.args` to perform substitution in all return values of a multi-assignment.
|
||||
- **`before(FunctionCallExpression)`**: Implement substitution for single-value returns.
|
||||
- **`possiblyInlineFunctionBody`**: Use substitution for void calls, fixing a bug where parameters used in the body were incorrectly handled.
|
||||
- **`DetermineInlineSubs`**:
|
||||
- Expand `isBodyInlineable` to allow `BinaryExpression`, `PrefixExpression`, and `TypecastExpression`.
|
||||
- Modify the visit condition to process all regular subroutines (not just those with `inline=false`), ensuring `makeFullyScoped` is called for manually marked `inline` subroutines.
|
||||
- If a subroutine is already `inline`, verify it meets the inlineability criteria; if not, it will naturally fail at call sites via `canInlineAtCallSite`.
|
||||
|
||||
#### `AstChecker.kt`
|
||||
- Remove the block that disables `inline` for regular subroutines and emits the "no effect" warning.
|
||||
|
||||
### Data Models / Contracts
|
||||
- `paramMap`: `Map<VarDecl, Expression>` where `VarDecl` is the parameter declaration and `Expression` is the argument passed at the call site.
|
||||
|
||||
|
||||
# Testing
|
||||
|
||||
### Validation Approach
|
||||
Verification will be performed using the automated test suite in `TestOptimization.kt`.
|
||||
|
||||
### Key Scenarios
|
||||
- **Multi-return with parameters**: `a, b = foo(1, 2)` where `foo(x, y)` returns `x+1, y+1`.
|
||||
- **Expression context with parameters**: `a = foo(10) + gv`.
|
||||
- **Void call with used parameters**: `foo(10)` where `foo(x)` calls `txt.print_ub(x)`.
|
||||
- **Nested inlining**: `a = foo(bar(10))` where both are inlineable.
|
||||
- **Manual `inline` keyword**: Verify that `inline sub foo() { return 1 }` is inlined and no longer emits a "no effect" warning.
|
||||
|
||||
### Test Changes
|
||||
- Enable `xtest("inline call with one return value and one parameter")`.
|
||||
- Enable `xtest("inline call with two return values and two parameters")`.
|
||||
- Add new tests for complex return expressions and void calls with parameters.
|
||||
|
||||
|
||||
# Delivery Steps
|
||||
|
||||
### Step 1: Relax inlining restrictions
|
||||
Relax inlining restrictions for regular subroutines.
|
||||
- Modify `AstChecker.kt` to remove the block that forcefully disables the `inline` flag on regular subroutines and emits a "no effect" warning.
|
||||
- Update `canInlineAtCallSiteWithReason` in `Inliner.kt` to allow expression context calls (including multi-value returns) for subroutines with parameters, provided all arguments are simple (literals or identifiers).
|
||||
- Update `DetermineInlineSubs` in `Inliner.kt` to ensure it processes all regular subroutines for scoping (calls `isBodyInlineable` even if already marked `inline`).
|
||||
|
||||
### Step 2: Implement parameter substitution logic
|
||||
Implement parameter substitution logic.
|
||||
- Add a private `ParameterSubstitutor` class in `Inliner.kt` that inherits from `AstWalker` and replaces `IdentifierReference` nodes that target subroutine parameters with copies of the corresponding argument expressions.
|
||||
- Expand `isBodyInlineable(Return)` in `DetermineInlineSubs` to allow `BinaryExpression`, `PrefixExpression`, and `TypecastExpression` as return values if their operands are also inlineable. This enables inlining of subroutines like `return x + 1`.
|
||||
|
||||
### Step 3: Complete inlining implementation
|
||||
Complete inlining implementation for all call contexts.
|
||||
- Update `Inliner.after(Assignment)` to handle multi-value returns from parameterized subroutines by zipping parameters with arguments and applying `ParameterSubstitutor` to each return value.
|
||||
- Update `Inliner.before(FunctionCallExpression)` to handle single-value returns from parameterized subroutines using `ParameterSubstitutor`.
|
||||
- Update `possiblyInlineFunctionBody` to handle void calls with parameters by applying `ParameterSubstitutor` to the inlined body statement. This fix ensures that parameters used in the body are correctly replaced by argument values.
|
||||
|
||||
### Step 4: Verification and Testing
|
||||
Verify the changes with tests.
|
||||
- Enable the previously disabled `xtest` cases in `TestOptimization.kt` that were blocked by missing parameter substitution.
|
||||
- Add new test cases to `TestOptimization.kt` covering void calls with used parameters, complex return expressions involving parameters, and the `inline` keyword on regular subroutines.
|
||||
- Run the full compiler test suite to ensure no regressions in other optimization passes or code generation.
|
||||
@@ -16,41 +16,39 @@ internal object ControlFlowOptimizers {
|
||||
* if false {A} else {B} -> B
|
||||
*/
|
||||
fun optimizeConditionalExpressions(program: PtProgram, errors: IErrorReporter): Int {
|
||||
var changes = 0
|
||||
val toOptimize = mutableListOf<Triple<PtIfElse, Boolean, PtNode>>()
|
||||
|
||||
walkAst(program) { node: PtNode, depth: Int ->
|
||||
if (node is PtIfElse) {
|
||||
val condition = node.condition
|
||||
val trueConst = condition.asConstValue()
|
||||
val parent = node.parent
|
||||
val index = parent.children.indexOf(node)
|
||||
|
||||
// if true { A } else { B } -> A
|
||||
if (trueConst == 1.0 || (condition is PtBool && condition.value)) {
|
||||
val ifScope = node.ifScope
|
||||
// Replace if-else with the true part statements
|
||||
parent.removeChildAt(index)
|
||||
// Insert the if scope statements in reverse order to maintain order
|
||||
ifScope.children.reversed().forEach { stmt ->
|
||||
parent.add(index, stmt)
|
||||
stmt.parent = parent
|
||||
}
|
||||
changes++
|
||||
toOptimize.add(Triple(node, true, node.parent))
|
||||
}
|
||||
// if false { A } else { B } -> B
|
||||
else if (trueConst == 0.0 || (condition is PtBool && !condition.value)) {
|
||||
val elseScope = node.elseScope
|
||||
// Replace if-else with the false part statements
|
||||
parent.removeChildAt(index)
|
||||
// Insert the else scope statements in reverse order to maintain order
|
||||
elseScope.children.reversed().forEach { stmt ->
|
||||
parent.add(index, stmt)
|
||||
stmt.parent = parent
|
||||
}
|
||||
changes++
|
||||
toOptimize.add(Triple(node, false, node.parent))
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
var changes = 0
|
||||
for ((node, takeTrue, parent) in toOptimize) {
|
||||
val index = parent.children.indexOf(node)
|
||||
if (index == -1) continue
|
||||
|
||||
parent.removeChildAt(index)
|
||||
val scope = if (takeTrue) node.ifScope else node.elseScope
|
||||
// Insert the scope statements in reverse order to maintain order
|
||||
scope.children.reversed().forEach { stmt ->
|
||||
parent.add(index, stmt)
|
||||
stmt.parent = parent
|
||||
}
|
||||
changes++
|
||||
}
|
||||
return changes
|
||||
}
|
||||
|
||||
@@ -58,26 +56,34 @@ internal object ControlFlowOptimizers {
|
||||
* Optimizes dead conditional branches in when statements.
|
||||
*/
|
||||
fun optimizeDeadConditionalBranches(program: PtProgram): Int {
|
||||
var changes = 0
|
||||
val toOptimize = mutableListOf<PtWhen>()
|
||||
|
||||
walkAst(program) { node: PtNode, depth: Int ->
|
||||
if (node is PtWhen) {
|
||||
val choices = node.choices.children
|
||||
// If we have a when with only an else choice, convert to if-else or just the else block
|
||||
if (choices.size == 1 && choices[0] is PtWhenChoice && (choices[0] as PtWhenChoice).isElse) {
|
||||
val elseChoice = choices[0] as PtWhenChoice
|
||||
val index = node.parent.children.indexOf(node)
|
||||
val parent = node.parent
|
||||
parent.removeChildAt(index)
|
||||
// Insert the else part statements
|
||||
elseChoice.statements.children.reversed().forEach { stmt ->
|
||||
parent.add(index, stmt)
|
||||
stmt.parent = parent
|
||||
}
|
||||
changes++
|
||||
toOptimize.add(node)
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
var changes = 0
|
||||
for (node in toOptimize) {
|
||||
if (!node.parent.children.contains(node)) continue
|
||||
|
||||
val elseChoice = node.choices.children[0] as PtWhenChoice
|
||||
val index = node.parent.children.indexOf(node)
|
||||
val parent = node.parent
|
||||
parent.removeChildAt(index)
|
||||
// Insert the else part statements
|
||||
elseChoice.statements.children.reversed().forEach { stmt ->
|
||||
parent.add(index, stmt)
|
||||
stmt.parent = parent
|
||||
}
|
||||
changes++
|
||||
}
|
||||
return changes
|
||||
}
|
||||
|
||||
@@ -85,42 +91,51 @@ internal object ControlFlowOptimizers {
|
||||
* Simplifies single-choice when statements to if-else.
|
||||
*/
|
||||
fun optimizeSingleWhens(program: PtProgram, errors: IErrorReporter): Int {
|
||||
var changes = 0
|
||||
val toOptimize = mutableListOf<PtWhen>()
|
||||
|
||||
walkAst(program) { node: PtNode, depth: Int ->
|
||||
if(node is PtWhen && node.choices.children.size==2) {
|
||||
val choice1 = node.choices.children[0] as PtWhenChoice
|
||||
val choice2 = node.choices.children[1] as PtWhenChoice
|
||||
if(choice1.isElse && choice2.values.children.size==1 || choice2.isElse && choice1.values.children.size==1) {
|
||||
errors.info("when can be simplified into an if-else", node.position)
|
||||
val truescope: PtNodeGroup
|
||||
val elsescope: PtNodeGroup
|
||||
val comparisonValue : PtNumber
|
||||
if(choice1.isElse) {
|
||||
truescope = choice2.statements
|
||||
elsescope = choice1.statements
|
||||
comparisonValue = choice2.values.children.single() as PtNumber
|
||||
} else {
|
||||
truescope = choice1.statements
|
||||
elsescope = choice2.statements
|
||||
comparisonValue = choice1.values.children.single() as PtNumber
|
||||
}
|
||||
val ifelse = PtIfElse(node.position)
|
||||
val condition = PtBinaryExpression("==", DataType.BOOL, node.position)
|
||||
condition.add(node.value)
|
||||
condition.add(comparisonValue)
|
||||
ifelse.add(condition)
|
||||
ifelse.add(truescope)
|
||||
ifelse.add(elsescope)
|
||||
ifelse.parent = node.parent
|
||||
val index = node.parent.children.indexOf(node)
|
||||
node.parent.setChild(index, ifelse)
|
||||
changes++
|
||||
toOptimize.add(node)
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
var changes = 0
|
||||
for (node in toOptimize) {
|
||||
if (!node.parent.children.contains(node)) continue
|
||||
|
||||
val choice1 = node.choices.children[0] as PtWhenChoice
|
||||
val choice2 = node.choices.children[1] as PtWhenChoice
|
||||
|
||||
errors.info("when can be simplified into an if-else", node.position)
|
||||
val truescope: PtNodeGroup
|
||||
val elsescope: PtNodeGroup
|
||||
val comparisonValue: PtNumber
|
||||
if (choice1.isElse) {
|
||||
truescope = choice2.statements
|
||||
elsescope = choice1.statements
|
||||
comparisonValue = choice2.values.children.single() as PtNumber
|
||||
} else {
|
||||
truescope = choice1.statements
|
||||
elsescope = choice2.statements
|
||||
comparisonValue = choice1.values.children.single() as PtNumber
|
||||
}
|
||||
val ifelse = PtIfElse(node.position)
|
||||
val condition = PtBinaryExpression("==", DataType.BOOL, node.position)
|
||||
condition.add(node.value)
|
||||
condition.add(comparisonValue)
|
||||
ifelse.add(condition)
|
||||
ifelse.add(truescope)
|
||||
ifelse.add(elsescope)
|
||||
ifelse.parent = node.parent
|
||||
val index = node.parent.children.indexOf(node)
|
||||
node.parent.setChild(index, ifelse)
|
||||
changes++
|
||||
}
|
||||
return changes
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user