2023-06-25 19:35:30 +00:00
===================
Compiling a program
===================
2018-08-06 23:23:34 +00:00
2019-07-12 18:31:18 +00:00
.. _building_compiler:
2019-01-29 22:32:43 +00:00
First, getting a working compiler
---------------------------------
2019-07-02 02:56:31 +00:00
Before you can compile Prog8 programs, you'll have to download or build the compiler itself.
2024-09-27 18:19:28 +00:00
Then make sure you have installed the :ref: `requirements` .
2019-07-02 02:56:31 +00:00
Then you can choose a few ways to get a compiler:
2019-01-29 22:32:43 +00:00
2023-11-08 21:47:05 +00:00
**Download an official release version from Github:**
2019-01-29 22:32:43 +00:00
2024-10-16 16:36:19 +00:00
#. download a recent "fat-jar" (called something like "prog8c-all.jar") from `the releases on Github <https://github.com/irmen/prog8/releases> `_
#. run the compiler with "java -jar prog8c.jar" to see how you can use it (use the correct name and version of the jar file you've downloaded).
2019-07-12 18:31:18 +00:00
2024-02-12 19:42:00 +00:00
**Or, install via a Package Manager:**
2024-02-21 16:11:36 +00:00
Currently, it's only available on `AUR <https://wiki.archlinux.org/title/Arch_User_Repository> `_ for Arch Linux and compatible systems.
The package is called `"prog8" <https://aur.archlinux.org/packages/prog8> `_ .
2024-02-12 19:42:00 +00:00
This package, alongside the compiler itself, also globally installs syntax highlighting for `` vim `` and `` nano `` .
2024-10-16 16:36:19 +00:00
In order to run compiler, you can type `` prog8c `` . The usage of those commands is exactly the same as with the `` java -jar `` method.
2024-02-12 19:42:00 +00:00
2024-02-21 16:11:36 +00:00
In case you prefer to install AUR packages in a traditional manner, make sure to install `"tass64" package <https://aur.archlinux.org/packages/tass64> `_
before installing prog8, as `makepkg <https://wiki.archlinux.org/title/Makepkg> `_ itself doesn't fetch AUR dependencies.
2024-02-12 19:42:00 +00:00
2023-11-08 21:47:05 +00:00
**Or, download a bleeding edge development version from Github:**
#. find the latest CI build on `the actions page on Github <https://github.com/irmen/prog8/actions> `_
#. download the zipped jar artifact from that build, and unzip it.
2024-10-16 16:36:19 +00:00
#. run the compiler with "java -jar prog8c.jar" (use the correct name and version of the jar file you've downloaded).
2023-11-08 21:47:05 +00:00
**Or, use the Gradle build system to build it yourself from source:**
2019-07-12 18:31:18 +00:00
The Gradle build system is used to build the compiler.
2023-07-04 20:41:38 +00:00
The most interesting gradle commands to run are probably the ones listed below.
(Note: if you have a recent gradle installed on your system already, you can probably replace the `` ./gradlew `` wrapper commands with just the regular `` gradle `` command.)
2019-07-12 18:31:18 +00:00
2021-10-30 10:01:52 +00:00
`` ./gradlew build ``
2019-07-12 18:31:18 +00:00
Builds the compiler code and runs all available checks and unit-tests.
2021-10-30 10:01:52 +00:00
Also automatically runs the installDist and installShadowDist tasks.
2023-07-04 20:41:38 +00:00
Read below at those tasks for where the resulting compiler jar file gets written.
2019-07-12 18:31:18 +00:00
`` ./gradlew installDist ``
Builds the compiler and installs it with scripts to run it, in the directory
2024-10-16 16:36:19 +00:00
`` ./compiler/build/install/prog8c ``
2019-07-12 18:31:18 +00:00
`` ./gradlew installShadowDist ``
Creates a 'fat-jar' that contains the compiler and all dependencies, in a single
executable .jar file, and includes few start scripts to run it.
The output can be found in `` .compiler/build/install/compiler-shadow/ ``
`` ./gradlew shadowDistZip ``
Creates a zipfile with the above in it, for easy distribution.
This file can be found in `` ./compiler/build/distributions/ ``
2019-07-02 02:56:31 +00:00
2022-09-20 02:04:47 +00:00
For normal use, the `` installDist `` task should suffice and after succesful completion, you can start the compiler with:
2019-07-02 02:56:31 +00:00
2024-10-16 16:36:19 +00:00
`` ./compiler/build/install/prog8c/bin/prog8c <options> <sourcefile> ``
2019-01-29 22:32:43 +00:00
2022-09-20 02:04:47 +00:00
(You should probably make an alias or link...)
2019-01-29 22:32:43 +00:00
2020-10-17 18:35:36 +00:00
.. hint ::
2021-06-03 19:21:44 +00:00
Development and testing is done on Linux using the IntelliJ IDEA IDE,
2024-05-18 16:59:32 +00:00
but the actual prog8 compiler should run on all operating systems that provide a java runtime (version 11 or newer).
2022-09-20 02:04:47 +00:00
If you do have trouble building or running the compiler on your operating system, please let me know!
2019-01-29 22:32:43 +00:00
2024-09-06 20:51:26 +00:00
To successfully build and debug in IDEA, you have to do two things manually first:
1. you have to generate the buildversion file, do this with the shell command: `` gradle createVersionFile ``
2. manually generate the Antlr-parser classes first.
The easiest way to build the parser classes this is the following:
2022-08-21 11:32:31 +00:00
1. make sure you have the Antlr4 plugin installed in IDEA
2. right click the grammar file Prog8ANTLR.g4 in the parser project, and choose "Generate Antlr Recognizer" from the menu.
3. rebuild the full project.
Alternatively you can also use the Makefile in the antlr directory to generate the parser, but for development the
Antlr4 plugin provides several extremely handy features so you'll probably want to have it installed anyway.
.. image :: _static/antlrparser.png
:alt: Generating the Antlr4 parser files
2019-01-29 22:32:43 +00:00
2024-09-27 18:19:28 +00:00
.. _requirements:
2019-01-29 22:32:43 +00:00
2024-09-27 18:19:28 +00:00
Required additional tools
-------------------------
`64tass <https://sourceforge.net/projects/tass64/> `_ - cross assembler. Install this program somewhere on your shell's search path.
It's easy to compile yourself, but a recent precompiled .exe (only for Windows) can be obtained from
`the files section <https://sourceforge.net/projects/tass64/files/binaries/> `_ in the official project on sourceforge.
*You need at least version 1.58.0 of this assembler.*
If you are on Linux, there's probably a "64tass" package in the repositories, but check if it is a recent enough version.
2018-08-06 23:23:34 +00:00
2024-09-27 18:19:28 +00:00
A **Java runtime (jre or jdk), version 11 or newer** is required to run the prog8 compiler itself.
If you're scared of Oracle's licensing terms, get one of the versions of another vendor. Even Microsoft provides their own version.
Other OpenJDK builds can be found at `Adoptium <https://adoptium.net/temurin/releases/?version=11> `_ .
For MacOS you can also use the Homebrew system to install a recent version of OpenJDK.
2018-08-06 23:23:34 +00:00
2021-11-21 12:53:22 +00:00
Running the compiler
--------------------
2018-08-06 23:23:34 +00:00
2021-11-21 12:53:22 +00:00
You run the Prog8 compiler on a main source code module file.
2018-08-06 23:23:34 +00:00
Other modules that this code needs will be loaded and processed via imports from within that file.
The compiler will link everything together into one output program at the end.
2019-01-24 01:43:25 +00:00
If you start the compiler without arguments, it will print a short usage text.
2021-11-21 12:53:22 +00:00
For normal use the compiler can be invoked with the command:
2018-08-06 23:23:34 +00:00
2024-10-16 16:36:19 +00:00
`` $ java -jar prog8c.jar -target cx16 sourcefile.p8 ``
2018-08-06 23:23:34 +00:00
2021-11-21 12:53:22 +00:00
(Use the appropriate name and version of the jar file downloaded from one of the Git releases.
Other ways to invoke the compiler are also available: see the introduction page about how
2024-09-27 18:19:28 +00:00
to build and run the compiler yourself. The `` -target `` option is always required, in this case we
2023-06-03 20:22:13 +00:00
tell it to compile a program for the Commander X16)
2018-09-28 19:28:16 +00:00
2019-01-24 01:43:25 +00:00
By default, assembly code is generated and written to `` sourcefile.asm `` .
2021-11-21 12:53:22 +00:00
It is then (automatically) fed to the `64tass <https://sourceforge.net/projects/tass64/> `_ assembler tool
2022-10-28 21:39:15 +00:00
that creates the final runnable program.
2021-11-21 12:53:22 +00:00
Command line options
^^^^^^^^^^^^^^^^^^^^
One or more .p8 module files
Specify the main module file(s) to compile.
Every file specified is a separate program.
`` -help `` , `` -h ``
Prints short command line usage information.
`` -target <compilation target> ``
2023-06-03 17:14:45 +00:00
Sets the target output of the compiler. This option is required.
2023-08-12 21:24:41 +00:00
`` c64 `` = Commodore 64, `` c128 `` = Commodore 128, `` cx16 `` = Commander X16, `` pet32 `` - Commodore PET model 4032,
`` atari `` = Atari 800 XL, `` virtual `` = builtin virtual machine.
2021-11-21 12:53:22 +00:00
`` -srcdirs <pathlist> ``
Specify a list of extra paths (separated with ':'), to search in for imported modules.
Useful if you have library modules somewhere that you want to re-use,
or to switch implementations of certain routines via a command line switch.
`` -emu `` , `` -emu2 ``
Auto-starts target system emulator after successful compilation.
emu2 starts the alternative emulator if available.
The compiled program and the symbol and breakpoint lists
2022-12-30 14:10:13 +00:00
(for the machine code monitor) are immediately loaded into the emulator (if it supports them)
2021-11-21 12:53:22 +00:00
`` -out <directory> ``
sets directory location for output files instead of current directory
`` -noasm ``
Do not create assembly code and output program.
Useful for debugging or doing quick syntax checks.
`` -noopt ``
Don't perform any code optimizations.
Useful for debugging or faster compilation cycles.
`` -optfloatx ``
Also optimize float expressions if optimizations are enabled.
Warning: can increase program size significantly if a lot of floating point expressions are used.
`` -watch ``
Enables continuous compilation mode (watches for file changes).
This greatly increases compilation speed on subsequent runs:
almost instant compilation times (less than a second) can be achieved in this mode.
The compiler will compile your program and then instead of exiting, it waits for any changes in the module source files.
As soon as a change happens, the program gets compiled again.
Note that it is possible to use the watch mode with multiple modules as well, but it will
recompile everything in that list even if only one of the files got updated.
2023-07-30 15:10:54 +00:00
`` -warnshadow ``
Tells the assembler to issue warning messages about symbol shadowing.
These *can* be problematic, but usually aren't because prog8 has different scoping rules
than the assembler has.
You may want to watch out for shadowing of builtin names though. Especially 'a', 'x' and 'y'
as those are the cpu register names and if you shadow those, the assembler might
interpret certain instructions differently and produce unexpected opcodes (like LDA X getting
turned into TXA, or not, depending on the symbol 'x' being defined in your own assembly code or not)
2024-10-27 12:49:00 +00:00
`` -addmissingrts ``
Enables old compiler behavior that silently adds RTS to asmsubs that don't have one.
This was done to fix asmsubs so they return properly to the caller instead of crashing the program.
However the new compiler behavior is to not silently modify the code anymore and instead give an error message
that tells you how to fix the problem. This option may go away in future version.
2021-11-21 12:53:22 +00:00
`` -quietasm ``
Don't print assembler output results.
2021-11-03 21:52:08 +00:00
2021-12-30 13:38:40 +00:00
`` -asmlist ``
Generate an assembler listing file as well.
2023-12-26 14:30:18 +00:00
`` -check ``
2024-09-27 18:19:28 +00:00
Quickly check the program for errors. No actual compilation will be performed.
2023-12-26 14:30:18 +00:00
2024-01-22 20:07:51 +00:00
`` -breakinstr <instruction> ``
Also output the specified CPU instruction for a `` %breakpoint `` , as well as the entry in the vice monitor list file.
2023-10-15 19:55:09 +00:00
This can be useful on emulators/systems that don't parse the breakpoint information in the list file,
2024-01-22 20:07:51 +00:00
such as the X16Emu emulator for the Commander X16.
Useful instructions to consider are `` brk `` and `` stp `` .
For example for the Commander X16 emulator, `` stp `` is useful because it can actually tyrigger
a breakpoint halt in the debugger when this is enabled by running the emulator with -debug.
2023-10-15 19:55:09 +00:00
2022-02-05 20:25:19 +00:00
`` -expericodegen ``
Use experimental code generation backend (*incomplete* ).
2024-01-01 21:48:19 +00:00
`` -printast1 ``
Prints the "compiler AST" (the internal representation of the program) after all processing steps.
`` -printast2 ``
Prints the "intermediate AST" which is the reduced representation of the program.
This is what is used in the code generators, to generate the executable code from.
2024-04-09 17:30:04 +00:00
`` -dumpvars ``
print a dump of the variables in the program
`` -dumpsymbols ``
print a dump of the variable declarations and subroutine signatures
2023-08-02 19:26:40 +00:00
`` -sourcelines ``
Also include the original prog8 source code lines as comments in the generated assembly code file,
mixed in between the actual generated assembly code.
This can be useful for debugging purposes to see what assembly was generated for what prog8 source code.
2023-05-30 17:08:34 +00:00
`` -splitarrays ``
Treat all word arrays as tagged with @split so they are all lsb/msb split into memory.
This removes the need to add @split yourself but some programs may fail to compile with
this option as not all array operations are implemented yet on split arrays.
2022-07-06 21:45:41 +00:00
`` -vm ``
2022-09-20 10:14:33 +00:00
load and run a p8-virt or p8-ir listing in the internal VirtualMachine instead of compiling a prog8 program file..
2022-07-06 21:45:41 +00:00
`` -D SYMBOLNAME=VALUE ``
Add this user-defined symbol directly to the beginning of the generated assembly file.
Can be repeated to define multiple symbols.
2023-06-26 22:27:34 +00:00
`` -varshigh <rambank> ``
2023-08-09 22:17:50 +00:00
Places uninitialized non-zeropage variables in a separate memory area, instead of inside the program itself.
This increases the amount of system ram available for program code.
The size of the increase depends on the program but can be several hundreds of bytes or more.
The location of the memory area for these variables depends on the compilation target machine:
2023-02-22 21:44:29 +00:00
2023-08-09 22:17:50 +00:00
c64: $C000 - $CFFF ; 4 kB, and the specified rambank number is ignored
2023-02-22 21:44:29 +00:00
2023-08-09 22:17:50 +00:00
cx16: $A000 - $BFFF ; 8 kB in the specified HiRAM bank (note: no auto bank switching is done, you must make sure yourself that this HiRAM bank is active when accessing these variables!)
2023-02-22 21:44:29 +00:00
If you use this option, you can no longer use the part of the above memory area that is
2023-08-09 22:17:50 +00:00
alotted to the variables, for your own purposes. The output of the 64tass assembler step at the
2023-02-22 21:44:29 +00:00
end of compilation shows precise details of where and how much memory is used by the variables
2023-08-09 22:17:50 +00:00
(it's called 'BSS' section or Gap at the address mentioned above).
Assembling the program will fail if there are too many variables to fit in a single high ram bank.
2023-02-22 21:44:29 +00:00
2023-12-23 19:11:50 +00:00
`` -varsgolden ``
Like `` -varshigh `` , but places the variables in the $0400-$07FF "golden ram" area instead.
Because this is in normal system memory, there are no bank switching issues.
This mode is only available on the Commander X16.
2024-04-09 17:30:04 +00:00
`` -slabshigh ``
put memory() slabs in high memory area instead of at the end of the program.
On the cx16 target the value specifies the HiRAM bank to use, on other systems this value is ignored.
`` -slabsgolden ``
put memory() slabs in 'golden ram' memory area instead of at the end of the program.
On the cx16 target this is $0400-07ff. This is unavailable on other systems.
2024-04-09 21:59:54 +00:00
`` -bytes2float <bytes> ``
convert a comma separated list of bytes from the target system to a float value.
NOTE: you need to supply a target option too, and also still have to supply a dummy module file name as well!
`` -float2bytes <number> ``
convert floating point number to a list of bytes for the target system.
NOTE: you need to supply a target option too, and also still have to supply a dummy module file name as well!
2024-04-09 17:30:04 +00:00
2018-08-06 23:23:34 +00:00
Module source code files
------------------------
2018-09-15 14:35:15 +00:00
A module source file is a text file with the `` .p8 `` suffix, containing the program's source code.
2018-08-06 23:23:34 +00:00
It consists of compilation options and other directives, imports of other modules,
and source code for one or more code blocks.
2020-08-27 16:10:22 +00:00
Prog8 has various *LIBRARY* modules that are defined in special internal files provided by the compiler.
You should not overwrite these or reuse their names.
2019-01-24 01:43:25 +00:00
They are embedded into the packaged release version of the compiler so you don't have to worry about
where they are, but their names are still reserved.
2018-08-06 23:23:34 +00:00
2021-10-13 16:16:51 +00:00
Importing other source files and specifying search location(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can create multiple source files yourself to modularize your large programs into
multiple module files. You can also create "library" modules this way with handy routines,
that can be shared among programs. By importing those module files, you can use them in other modules.
It is possible to tell the compiler where it should look for these files, by using
2023-02-06 22:36:19 +00:00
the `` srcdirs `` command line option. This can also be a lo-fi way to use different source files
for different compilation targets if you wish. Which is useful as currently the compiler
doesn't have conditional compilation like #ifdef/#endif in C.
2019-08-05 21:36:24 +00:00
2018-08-06 23:23:34 +00:00
.. _debugging:
2022-12-30 14:10:13 +00:00
Debugging (with VICE or Box16)
------------------------------
2018-08-06 23:23:34 +00:00
There's support for using the monitor and debugging capabilities of the rather excellent
2022-10-29 12:12:10 +00:00
`VICE emulator <http://vice-emu.sourceforge.net/> `_ .
2018-08-06 23:23:34 +00:00
The `` %breakpoint `` directive (see :ref: `directives` ) in the source code instructs the compiler to put
a *breakpoint* at that position. Some systems use a BRK instruction for this, but
this will usually halt the machine altogether instead of just suspending execution.
2018-09-15 14:21:05 +00:00
Prog8 issues a NOP instruction instead and creates a 'virtual' breakpoint at this position.
2018-08-06 23:23:34 +00:00
All breakpoints are then written to a file called "programname.vice-mon-list",
2022-12-30 14:10:13 +00:00
which is meant to be used by the VICE and Box16 emulators.
2022-10-29 12:12:10 +00:00
It contains a series of commands for VICE's monitor, including source labels and the breakpoint settings.
2019-01-24 01:43:25 +00:00
If you use the emulator autostart feature of the compiler, it will take care of this for you.
2022-10-29 12:12:10 +00:00
If you launch VICE manually, you'll have to use a command line option to load this file:
2018-08-06 23:23:34 +00:00
`` $ x64 -moncommands programname.vice-mon-list ``
2022-10-29 12:12:10 +00:00
VICE will then use the label names in memory disassembly, and will activate any breakpoints as well.
If your running program hits one of the breakpoints, VICE will halt execution and drop you into the monitor.
2018-08-06 23:23:34 +00:00
2022-12-30 14:10:13 +00:00
Box16 is the alternative emulator for the Commander X16 and it also includes debugging facilities
that support these symbol and breakpoint lists.
2018-08-06 23:23:34 +00:00
Troubleshooting
---------------
2023-05-31 16:49:21 +00:00
Compiler doesn't run, complains about "UnsupportedClassVersionError"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-18 16:59:32 +00:00
You need to install and use JDK version 11 or newer to run the prog8 compiler. Check this with "java -version".
2023-05-31 18:09:03 +00:00
See :ref: `requirements` .
The computer just resets (at the end of the program)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In the default compiler configuration, it is not safely possible to return back to the BASIC prompt when
your program exits. The only reliable thing to do is to reboot the system.
This is due to the fact that in this mode, prog8 will overwrite important BASIC and Kernal variables in zero page memory.
To avoid the reset from happening, use an empty `` repeat `` loop at the end of your program to keep it from exiting.
2023-05-31 16:49:21 +00:00
Alternatively, if you want your program to exit cleanly back to the BASIC prompt,
you have to use `` %zeropage basicsafe `` , see :ref: `directives` .
2023-05-31 18:09:03 +00:00
The reason this is not the default is that it is very beneficial to have more zeropage space available to the program,
2024-02-18 21:25:26 +00:00
and programs that have to return cleanly to the BASIC prompt are considered to be the exception.
2023-05-31 18:09:03 +00:00
2023-05-31 16:49:21 +00:00
Odd text and screen colors at start
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Prog8 will reset the screen mode and colors to a uniform well-known state. If you don't like the
2023-05-31 18:09:03 +00:00
default text and screen colors, you can simply change them yourself to whatever you want at the
start of your program. It depends on the computer system how you do this but there are some
routines in the textio module to help you with this.
2023-05-31 16:49:21 +00:00
Alternatively you can choose to disable this re-initialization altogether
using `` %option no_sysinit `` , see :ref: `directives` .
Floats error
^^^^^^^^^^^^
2023-05-31 18:09:03 +00:00
Are you getting an assembler error about undefined symbols such as `` not defined 'floats' `` ?
2020-09-21 16:21:24 +00:00
This happens when your program uses floating point values, and you forgot to import `` floats `` library.
2019-01-05 15:09:05 +00:00
If you use floating points, the compiler needs routines from that library.
2020-09-21 16:21:24 +00:00
Fix it by adding an `` %import floats `` .
2019-01-24 01:43:25 +00:00
2023-06-11 15:51:18 +00:00
Gradle error when building the compiler yourself
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you get a gradle build error containing the line "No matching toolchains found for requested specification"
somewhere, it means that the Gradle build tool can't locate the correct version of the JDK to use.
The file "gradle.properties" contains a line like this: `` javaVersion=11 `` .
You can do one of two things to fix the build error:
- install a JDK with that version,
- or change the version number to match the JDK version that *is* installed on your system (must be >= 11)
2019-01-24 01:43:25 +00:00
2023-08-28 14:55:28 +00:00
Strange assembler errors
^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-04 21:50:16 +00:00
If the compilation of your program fails in the assembly step, please check that you have
the required version of the 64tass assembler installed. See :ref: `requirements` .
2023-07-21 20:40:07 +00:00
Also make sure that inside hand-written inlined assembly,
you don't use symbols named just a single letter (especially 'a', 'x' and 'y').
Sometimes these are interpreted as the CPU register of that name. To avoid such confusions,
always use 2 or more letters for symbols in your assembly code.
2023-07-04 21:50:16 +00:00
2023-08-28 14:55:28 +00:00
'shadowing' warnings form the assembler
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Avoid using 'a', 'x' or 'y' as symbols in your inlined assembly code.
Also avoid using 64tass' built-in function or type names as symbols in your inlined assembly code.
The 64tass manual contains `a list of those <https://tass64.sourceforge.net/#functions> `_ .
2023-07-04 21:50:16 +00:00
2019-01-24 01:43:25 +00:00
Examples
--------
2024-04-07 17:32:44 +00:00
A bunch of example programs can be found in the 'examples' directory of the source tree.
There are cross-platform examples that can be compiled for various systems unaltered,
and there are also examples specific to certain computers (C64, X16, etcetera).
So for instance, to compile and run the Commodore 64 rasterbars example program, use this command::
2019-01-25 00:35:46 +00:00
2024-10-16 16:36:19 +00:00
$ java -jar prog8c.jar -target c64 -emu examples/c64/rasterbars.p8
2019-01-25 00:35:46 +00:00
or::
2024-10-16 16:36:19 +00:00
$ /path/to/prog8c -target c64 -emu examples/c64/rasterbars.p8
2019-01-25 00:35:46 +00:00