2021-04-04 10:55:29 +00:00
=================
Technical details
=================
2020-12-19 02:18:40 +00:00
All variables are static in memory
----------------------------------
All variables are allocated statically, there is no concept of dynamic heap or stack frames.
Essentially all variables are global (but scoped) and can be accessed and modified anywhere,
2022-10-28 21:39:54 +00:00
but care should be taken of course to avoid unexpected side effects.
2020-12-19 02:18:40 +00:00
Especially when you're dealing with interrupts or re-entrant routines: don't modify variables
that you not own or else you will break stuff.
2023-02-22 21:48:45 +00:00
Variables that are not put into zeropage, will be put into a special 'BSS' section for the assembler.
This section is usually placed at the end of the resulting program but because it only contains empty space
it won't actually increase the size of the resulting program binary.
Prog8 takes care of properly filling this memory area with zeros at program startup and then reinitializes
the subset of variables that have a nonzero initialization value.
2023-02-22 21:44:29 +00:00
It is possible to relocate the BSS section using a compiler option
so that more system ram is available for the program code itself.
2020-12-19 02:18:40 +00:00
2023-07-02 17:38:44 +00:00
.. _symbol-prefixing:
2022-11-29 23:15:13 +00:00
2023-06-29 22:29:50 +00:00
Symbol prefixing in generated Assembly code
-------------------------------------------
*All* symbols in the prog8 program will be prefixed with `` p8_ `` in the generated assembly code.
This is to avoid naming conflicts with CPU registers, assembly instructions, etc.
So if you're referencing symbols from the prog8 program in inlined assembly code, you have to take
this into account. Stick a `` p8_ `` in front of everything that you want to reference that is coming
from a prog8 source file.
2023-07-02 17:38:44 +00:00
All elements in scoped names such as `` main.routine.var1 `` are prefixed so this becomes `` p8_main.p8_routine.p8_var1 `` .
2023-06-29 22:29:50 +00:00
.. attention ::
2023-07-02 17:38:44 +00:00
Symbols from library modules are *not* prefixed and can be used
in assembly code as-is. So you can write::
2023-06-29 22:29:50 +00:00
2023-07-02 17:38:44 +00:00
%asm {{
lda #'a'
jsr cbm.CHROUT
}}
2022-11-29 23:15:13 +00:00
2020-12-19 02:18:40 +00:00
Software stack for expression evaluation
----------------------------------------
Prog8 uses a software stack to evaluate complex expressions that it can't calculate in-place or
directly into the target variable, register, or memory location.
'software stack' means: seperated and not using the processor's hardware stack.
The software stack is implemented as follows:
2023-02-21 21:52:04 +00:00
- 2*128 bytes = 1 page of memory allocated for this, exact locations vary per machine target.
For the C64 this page is at $cf00-$cfff.
For the Commander X16 it is at $0700-$07ff (top of the "golden ram" area).
2022-09-20 02:04:47 +00:00
This default location can be overridden using the `-esa` command line option.
2020-12-19 02:18:40 +00:00
- these are the high and low bytes of the values on the stack (it's a 'split 16 bit word stack')
- for byte values just the lsb page is used, for word values both pages
- float values (5 bytes) are chopped up into 2 words and 1 byte on this stack.
- the X register is permanently allocated to be the stack pointer in the software stack.
- you can use the X register as long as you're not using the software stack.
But you *must* make sure it is saved and restored after the code that modifies it,
otherwise the evaluation stack gets corrupted.
Subroutine Calling Convention
-----------------------------
Calling a subroutine requires three steps:
#. preparing the arguments (if any) and passing them to the routine
#. calling the routine
2022-10-28 21:39:15 +00:00
#. preparing the return value (if any) and returning that from the call.
2020-12-19 02:18:40 +00:00
Calling the routine is just a simple JSR instruction, but the other two work like this:
`` asmsub `` routines
^^^^^^^^^^^^^^^^^^^
2022-10-29 12:10:11 +00:00
These are usually declarations of Kernal (ROM) routines or low-level assembly only routines,
2020-12-19 02:18:40 +00:00
that have their arguments solely passed into specific registers.
Sometimes even via a processor status flag such as the Carry flag.
Return values also via designated registers.
2020-12-19 02:36:52 +00:00
The processor status flag is preserved on returning so you can immediately act on that for instance
via a special branch instruction such as `` if_z `` or `` if_cs `` etc.
2020-12-19 02:18:40 +00:00
regular subroutines
^^^^^^^^^^^^^^^^^^^
- subroutine parameters are just variables scoped to the subroutine.
- the arguments passed in a call are evaluated (using the eval-stack if needed) and then
copied into those variables.
2022-01-05 22:21:45 +00:00
Using variables for this sometimes can seem inefficient but it's required to allow subroutines to work locally
2020-12-19 02:27:08 +00:00
with their parameters and allow them to modify them as required, without changing the
variables used in the call's arguments. If you want to get rid of this overhead you'll
have to make an `` asmsub `` routine in assembly instead.
2022-01-05 22:21:45 +00:00
- the order of evaluation of subroutine call arguments *is unspecified* and should not be relied upon.
2020-12-19 02:18:40 +00:00
- the return value is passed back to the caller via cpu register(s):
Byte values will be put in `` A `` .
Word values will be put in `` A `` + `` Y `` register pair.
2022-10-29 12:12:10 +00:00
Float values will be put in the `` FAC1 `` float 'register' (BASIC allocated this somewhere in ram).
2020-12-19 02:18:40 +00:00
Calls to builtin functions are treated in a special way:
Generally if they have a single argument it's passed in a register or register pair.
Multiple arguments are passed like a normal subroutine, into variables.
Some builtin functions have a fully custom implementation.
2020-12-19 02:36:52 +00:00
The compiler will warn about routines that are called and that return a value, if you're not
2022-10-28 21:39:15 +00:00
doing something with that returnvalue. This can be on purpose if you're simply not interested in it.
2020-12-19 02:36:52 +00:00
Use the `` void `` keyword in front of the subroutine call to get rid of the warning in that case.
2021-04-04 10:55:29 +00:00
The 6502 CPU's X-register: off-limits
-------------------------------------
Prog8 uses the cpu's X-register as a pointer in its internal expression evaluation stack.
When only writing code in Prog8, this is taken care of behind the scenes for you by the compiler.
2022-10-29 12:10:11 +00:00
However when you are including or linking with assembly routines or Kernal/ROM calls that *do*
2021-04-04 10:55:29 +00:00
use the X register (either clobbering it internally, or using it as a parameter, or return value register),
those calls will destroy Prog8's stack pointer and this will result in invalid calculations.
You should avoid using the X register in your assembly code, or take preparations.
If you make sure that the value of the X register is preserved before calling a routine
that uses it, and restored when the routine is done, you'll be ok.
Routines that return a value in the X register can be called from Prog8 but the return value is
inaccessible unless you write a short piece of inline assembly code to deal with it yourself, such as::
ubyte returnvalue
%asm {{
stx P8ZP_SCRATCH_REG ; use 'phx/plx' if using 65c02 cpu
ldx #10
jsr routine_using_x
stx returnvalue
ldx P8ZP_SCRATCH_REG
}}
; now use 'returnvalue' variable
Prog8 also provides some help to deal with this:
- you should use a `` clobbers(X) `` specification for asmsub routines that modify the X register; the compiler will preserve it for you automatically when such a routine is called
2021-11-29 22:13:04 +00:00
- the `` rsavex() `` and `` rrestorex() `` builtin functions can preserve and restore the X register
- the `` rsave() `` and `` rrestore() `` builtin functions can preserve and restore *all* registers (but this is very slow and overkill if you only need to save X)
2022-09-21 15:21:41 +00:00
Compiler Internals
------------------
Here is a diagram of how the compiler translates your program source code into a binary program:
.. image :: prog8compiler.svg
Some notes and references into the compiler's source code modules:
#. The `` compileProgram() `` function (in the `` compiler `` module) does all the coordination and basically drives all of the flow shown in the diagram.
#. ANTLR is a Java parser generator and is used for initial parsing of the source code. (`` parser `` module)
#. Most of the compiler and the optimizer operate on the *Compiler AST* . These are complicated
syntax nodes closely representing the Prog8 program structure. (`` compilerAst `` module)
2023-03-07 23:02:41 +00:00
#. For code generation, a much simpler AST has been defined that replaces the *Compiler AST* .
Most notably, node type information is now baked in. (`` codeCore `` module, Pt- classes)
2022-09-21 15:21:41 +00:00
#. An *Intermediate Representation* has been defined that is generated from the intermediate AST. This IR
is more or less a machine code language for a virtual machine - and indeed this is what the built-in
2022-10-28 21:39:15 +00:00
prog8 VM will execute if you use the 'virtual' compilation target and use `` -emu `` to launch the VM.
2022-09-26 17:52:12 +00:00
(`` intermediate `` and `` codeGenIntermediate `` modules, and `` virtualmachine `` module for the VM related stuff)
2023-03-07 23:02:41 +00:00
#. The code generator backends all implement a common interface `` ICodeGeneratorBackend `` defined in the `` codeCore `` module.
Currently they get handed the program Ast, Symboltable and several other things.
If the code generator wants it can use the `` IRCodeGen `` class from the `` codeGenIntermediate `` module
to convert the Ast into IR first. The VM target uses this, but the 6502 codegen doesn't right now.