# cc65 Users Guide

## Ullrich von Bassewitz,
Greg King

cc65 is a C compiler for 6502 targets. It supports several 6502 based home computers like the Commodore and Atari machines, but it is easily retargetable.

## 1. Overview

## 2. Usage

- 2.1 Command line option overview
- 2.2 Command line options in detail

## 3. Input and output

## 4. Differences to the ISO standard

## 5. Extensions

## 6. Predefined macros

## 7. #pragmas

- 7.1 `#pragma allow-eager-inline ([push,] on|off)`
- 7.2 `#pragma bss-name ([push,] <name>)`
- 7.3 `#pragma charmap (<index>, <code>)`
- 7.4 `#pragma check-stack ([push,] on|off)`
- 7.5 `#pragma code-name ([push,] <name>)`
- 7.6 `#pragma codesize ([push,] <int>)`
- 7.7 `#pragma data-name ([push,] <name>)`
- 7.8 `#pragma inline-stdfuncs ([push,] on|off)`
- 7.9 `#pragma local-strings ([push,] on|off)`
- 7.10 `#pragma message (<message>)`
- 7.11 `#pragma optimize ([push,] on|off)`
- 7.12 `#pragma rodata-name ([push,] <name>)`
- 7.13 `#pragma regvaraddr ([push,] on|off)`
- 7.14 `#pragma register-vars ([push,] on|off)`
- 7.15 `#pragma signed-chars ([push,] on|off)`
- 7.16 `#pragma static-locals ([push,] on|off)`
- 7.17 `#pragma warn (name, [push,] on|off)`
- 7.18 `#pragma wrapped-call (push, <name>, <identifier>)`
- 7.19 `#pragma writable-strings ([push,] on|off)`
- 7.20 `#pragma zpsym (<name>)`

## 8. Register variables

## 9. Inline assembler

## 10. Implementation-defined behavior

## 11. Copyright

## 1. Overview

cc65 was originally a C compiler for the Atari 8-bit machines written by John R. Dunning. In prior releases I've described the compiler by listing up the changes made by me. I have made many more changes in the meantime (and rewritten major parts of the compiler), so I will no longer do that, since the list would be too large and of no use to anyone. Instead I will describe the compiler in respect to the ANSI/ISO C standard.

There are separate documents named library.html and funcref.html that cover the library that is available for the compiler. If you know C, and are interested in doing actual programming, the library documentation is probably of much more use than this document.

If you need some hints for getting the best code out of the compiler, you may have a look at coding.html which covers some code generation issues.

## 2. Usage

The compiler translates C files into files containing assembly code that may be translated by the ca65 macroassembler (for more information about the assembler, have a look at ca65.html).

## 2.1 Command line option overview

The compiler may be called as follows:

> `

>     ---------------------------------------------------------------------------
>     Usage: cc65 [options] file
>     Short options:
>       -Cl                           Make local variables static
>       -Dsym[=defn]                  Define a symbol
>       -E                            Stop after the preprocessing stage
>       -I dir                        Set an include directory search path
>       -O                            Optimize code
>       -Oi                           Optimize code, inline more code
>       -Or                           Enable register variables
>       -Os                           Inline some standard functions
>       -T                            Include source as comment
>       -V                            Print the compiler version number
>       -W warning[,...]              Suppress warnings
>       -d                            Debug mode
>       -g                            Add debug info to object file
>       -h                            Help (this text)
>       -j                            Default characters are signed
>       -mm model                     Set the memory model
>       -o name                       Name the output file
>       -r                            Enable register variables
>       -t sys                        Set the target system
>       -v                            Increase verbosity

>     Long options:
>       --add-source                  Include source as comment
>       --all-cdecl                   Make functions default to __cdecl__
>       --bss-name seg                Set the name of the BSS segment
>       --check-stack                 Generate stack overflow checks
>       --code-name seg               Set the name of the CODE segment
>       --codesize x                  Accept larger code by factor x
>       --cpu type                    Set cpu type (6502, 65c02)
>       --create-dep name             Create a make dependency file
>       --create-full-dep name        Create a full make dependency file
>       --data-name seg               Set the name of the DATA segment
>       --debug                       Debug mode
>       --debug-info                  Add debug info to object file
>       --debug-opt name              Debug optimization steps
>       --dep-target target           Use this dependency target
>       --disable-opt name            Disable an optimization step
>       --eagerly-inline-funcs        Eagerly inline some known functions
>       --enable-opt name             Enable an optimization step
>       --help                        Help (this text)
>       --include-dir dir             Set an include directory search path
>       --inline-stdfuncs             Inline some standard functions
>       --list-opt-steps              List all optimizer steps and exit
>       --list-warnings               List available warning types for -W
>       --local-strings               Emit string literals immediately
>       --memory-model model          Set the memory model
>       --register-space b            Set space available for register variables
>       --register-vars               Enable register variables
>       --rodata-name seg             Set the name of the RODATA segment
>       --signed-chars                Default characters are signed
>       --standard std                Language standard (c89, c99, cc65)
>       --static-locals               Make local variables static
>       --target sys                  Set the target system
>       --verbose                     Increase verbosity
>       --version                     Print the compiler version number
>       --writable-strings            Make string literals writable
>     ---------------------------------------------------------------------------

> `

## 2.2 Command line options in detail

Here is a description of all the command line options:

`--all-cdecl`

  Tells the compiler that functions which aren't declared explicitly with either the `__cdecl__` or `__fastcall__` calling conventions should have the cdecl convention. (Normally, functions that aren't variadic are fast-called.)

`--bss-name seg`

  Set the name of the bss segment. See also `#pragma bss-name`.

`--check-stack`

  Tells the compiler to generate code that checks for stack overflows. See `#pragma check-stack` for an explanation of this feature.

`--code-name seg`

  Set the name of the code segment. See also `#pragma code-name`

`--codesize x`

  This options allows finer control about speed vs. size decisions in the code generation and optimization phases. It gives the allowed size increase factor (in percent). The default is 100 when not using `-Oi` and 200 when using `-Oi` (`-Oi` is the same as `-O --codesize 200`).

`--cpu CPU`

  Set the CPU, the compiler generates code for. You may specify "6502" or "65C02" as the CPU. The default depends on the selected target (see option `-t`). It is the 6502 CPU for most targets or if no target has been set. Specifying 65C02 will use a few 65C02 instructions when generating code. Don't expect too much from this option: In most cases the difference in size and speed is just 1-2%.

`--create-dep name`

  Tells the compiler to generate a file containing the dependency list for the compiled module in makefile syntax. The output is written to a file with the given name. The output does not include system include files (in angle brackets).

`--create-full-dep name`

  Tells the compiler to generate a file containing the dependency list for the compiled module in makefile syntax. The output is written to a file with the given name. The output does include system include files (in angle brackets).

`--data-name seg`

  Set the name of the data segment. See also `#pragma data-name`

`-d, --debug`

  Enables debug mode, something that should not be needed for mere mortals:-)

`--dep-target target`

  When generating a dependency file, don't use the actual output file as the target of the dependency, but the file specified with this option. The option has no effect if neither `--create-dep` nor `--create-full-dep` is specified.

`-D sym[=definition]`

  Define a macro on the command line. If no definition is given, the macro is defined to the value "1".

`-g, --debug-info`

  This will cause the compiler to insert a `.DEBUGINFO` command into the generated assembler code. This will cause the assembler to include all symbols in a special section in the object file.

`--eagerly-inline-funcs`

  Have the compiler eagerly inline these functions from the C library:

  - `memcpy()`
  - `memset()`
  - `strcmp()`
  - `strcpy()`
  - `strlen()`
  - most of the functions declared in `<ctype.h>`

  Note: This has two consequences:

  - You may not use names of standard C functions for your own functions. If you do that, your program is not standard-compliant anyway; but, using `--eagerly-inline-funcs` actually will break things.
  - The inlined string and memory functions will not handle strings or memory areas larger than 255 bytes. Similarly, the inlined `is..()` functions will not work with values outside the char. range (such as `EOF`).

  `--eagerly-inline-funcs` implies the `--inline-stdfuncs` command line option.

  See also `#pragma allow-eager-inline`.

`-h, --help`

  Print the short option summary shown above.

`--inline-stdfuncs`

  Allow the compiler to inline some standard functions from the C library like strlen. This will not only remove the overhead for a function call, but will make the code visible for the optimizer. See also the `-Os` command line option and `#pragma inline-stdfuncs`.

`--list-warnings`

  List the names of warning types available for use with `-W`.

`--local-strings`

  Emit string literals into the rodata segment as soon as they're encountered in the source (even if you do nothing but get the sizeof those strings). The default is to keep string literals until end of assembly, merge read only literals if possible, and then output the literals into the data or rodata segment that is active at that point. Use of this option prevents merging of duplicate strings, but the options that change the name of one of the data segments will work.

  You can also use `#pragma local-strings` for fine grained control.

`-o name`

  Specify the name of the output file. If you don't specify a name, the name of the C input file is used, with the extension replaced by ".s".

`-r, --register-vars`

  `-r` will make the compiler honor the `register` keyword. Local variables may be placed in registers (which are actually zero page locations). There is some overhead involved with register variables, since the old contents of the registers must be saved and restored. Since register variables are of limited use without the optimizer, there is also a combined switch: `-Or` will enable both, the optimizer and register variables.

  For more information about register variables see register variables.

  The compiler setting can also be changed within the source file by using `#pragma register-vars`.

`--register-space`

  This option takes a numeric parameter and is used to specify, how much zero page register space is available. Please note that just giving this option will not increase or decrease by itself, it will just tell the compiler about the available space. You will have to allocate that space yourself using an assembler module with the necessary allocations, and a linker configuration that matches the assembler module. The default value for this option is 6 (bytes).

  If you don't know what all this means, please don't use this option.

`--rodata-name seg`

  Set the name of the rodata segment (the segment used for readonly data). See also `#pragma rodata-name`

`-j, --signed-chars`

  Using this option, you can make the default characters signed. Since the 6502 has no provisions for sign extending characters (which is needed on almost any load operation), this will make the code larger and slower. A better way is to declare characters explicitly as "signed" if needed. You can also use `#pragma signed-chars` for better control of this option.

`--standard std`

  This option allows to set the language standard supported. The argument is one of

  c89

    This disables anything that is illegal in C89/C90. Among those things are `//` comments and the non-standard keywords without underscores. Please note that cc65 is not a fully C89 compliant compiler despite this option. A few more things (like floats) are missing.

  c99

    This enables a few features from the C99 standard. With this option, `//` comments are allowed. It will also cause warnings and even errors in a few situations that are allowed with `--standard c89`. For example, a call to a function without a prototype is an error in this mode.

  cc65

    This is the default mode. It is like c99 mode, but additional features are enabled. Among these are "void data", non-standard keywords without the underlines, unnamed function parameters and the requirement for main() to return an int.

  Please note that the compiler does not support the C99 standard and never will. c99 mode is actually c89 mode with a few selected C99 extensions.

`-t target, --target target`

  This option is used to set the target system. The target system determines the character set that is used for strings and character constants and the default CPU. The CPU setting can be overriden by use of the `--cpu` option.

  The following target systems are supported:

  - none
  - apple2
  - apple2enh
  - atari
  - atarixl
  - atmos
  - c16 (works also for the c116 with memory up to 32K)
  - c64
  - c128
  - cbm510 (CBM-II series with 40 column video)
  - cbm610 (all CBM-II II computers with 80 column video)
  - geos-apple
  - geos-cbm
  - lunix
  - lynx
  - nes
  - osic1p
  - pet (all CBM PET systems except the 2001)
  - plus4
  - sim6502
  - sim65c02
  - supervision
  - telestrat
  - vic20
`-v, --verbose`

  Using this option, the compiler will be somewhat more verbose if errors or warnings are encountered.

`--writable-strings`

  Make string literals writable by placing them into the data segment instead of the rodata segment. You can also use `#pragma writable-strings` to control this option from within the source file.

`-Cl, --static-locals`

  Use static storage for local variables instead of storage on the stack. Since the stack is emulated in software, this gives shorter and usually faster code, but the code is no longer reentrant. The difference between `-Cl` and declaring local variables as static yourself is, that initializer code is executed each time, the function is entered. So when using

  > `

  >             void f (void)
  >             {
  >                 unsigned a = 1;
  >                 ...
  >             }

  > `

  the variable `a` will always have the value `1` when entering the function and using `-Cl`, while in

  > `

  >             void f (void)
  >             {
  >                 static unsigned a = 1;
  >                 ....
  >             }

  > `

  the variable `a` will have the value `1` only the first time that the function is entered, and will keep the old value from one call of the function to the next.

  You may also use `#pragma static-locals` to change this setting in your sources.

`-I dir, --include-dir dir`

  Set a directory where the compiler searches for include files. You may use this option multiple times to add more than one directory to the search list.

`-O, -Oi, -Or, -Os`

  Enable an optimizer run over the produced code.

  Using `-Oi`, the code generator will inline some code where otherwise a runtime functions would have been called, even if the generated code is larger. This will not only remove the overhead for a function call, but will make the code visible for the optimizer. `-Oi` is an alias for `-O --codesize 200`.

  `-Or` will make the compiler honor the `register` keyword. Local variables may be placed in registers (which are actually zero page locations). See also the `--register-vars` command line option, and the discussion of register variables below.

  Using `-Os` will allow the compiler to inline some standard functions from the C library like strlen. This will not only remove the overhead for a function call, but will make the code visible for the optimizer. See also the `--inline-stdfuncs` command line option.

  It is possible to concatenate the modifiers for `-O`. For example, to enable register variables and inlining of standard functions, you may use `-Ors`.

`-T, --add-source`

  This include the source code as comments in the generated code. This is normally not needed.

`-V, --version`

  Print the version number of the compiler. When submitting a bug report, please include the operating system you're using, and the compiler version.

`-W name[,name,...]`

  This option allows to control warnings generated by the compiler. It is followed by a comma-separated list of warnings that should be enabled or disabled. To disable a warning, its name is prefixed by a minus sign. If no such prefix exists, or the name is prefixed by a plus sign, the warning is enabled.

  The following warning names currently are recognized:

  `const-comparison`

    Warn if the result of a comparison is constant.

  `error`

    Treat all warnings as errors.

  `no-effect`

    Warn about statements that don't have an effect.

  `remap-zero`

    Warn about a `#pragma charmap()` that changes a character's code number from/to 0x00.

  `struct-param`

    Warn when passing structs by value.

  `unknown-pragma`

    Warn about #pragmas that aren't recognized by cc65.

  `unused-label`

    Warn about unused labels.

  `unused-param`

    Warn about unused function parameters.

  `unused-var`

    Warn about unused variables.

  The full list of available warning names can be retrieved by using the option `--list-warnings`.

  You may use also `#pragma warn` to control this setting, for smaller pieces of code, from within your sources.

## 3. Input and output

The compiler will accept one C file per invocation and create a file with the same base name, but with the extension replaced by ".s". The output file contains assembler code suitable for use with the ca65 macro assembler.

Include files in quotes are searched in the following places:

1. The current file's directory.
2. Any directory added with the `-I` option on the command line.
3. The value of the environment variable `CC65_INC` if it is defined.

Include files in angle brackets are searched in the following places:

1. Any directory added with the `-I` option on the command line.
2. The value of the environment variable `CC65_INC` if it is defined.
3. A subdirectory named `include` of the directory defined in the environment variable `CC65_HOME`, if it is defined.
4. An optionally compiled-in directory.

## 4. Differences to the ISO standard

Apart from the things listed below, the compiler does support additional keywords, has several functions in the standard headers with names outside the reserved namespace and a few syntax extensions. All these can be disabled with the `--standard` command line option. Its use for maximum standards compatibility is advised.

Here is a list of differences between the language, the compiler accepts, and the one defined by the ISO standard:

- The datatypes "float" and "double" are not available.
- C Functions may not return structs (or unions), and structs may not be passed as parameters by value. However, struct assignment *is* possible.
- Most of the C library is available with only the fastcall calling convention ( see below). It means that you must not mix pointers to those functions with pointers to user-written, cdecl functions (the calling conventions are incompatible).
- The `volatile` keyword has almost no effect. That is not as bad as it sounds, since the 6502 has so few registers that it isn't possible to keep values in registers anyway.

There may be some more minor differences I'm currently not aware of. The biggest problem is the missing float data type. With this limitation in mind, you should be able to write fairly portable code.

## 5. Extensions

This cc65 version has some extensions to the ISO C standard.

- The compiler allows to insert assembler statements into the output file. The syntax is

  > `

  >             asm [optional volatile] (<string literal>[, optional parameters]) ;

  > `

  or

  > `

  >             __asm__ [optional volatile] (<string literal>[, optional parameters]) ;

  > `

  The first form is in the user namespace; and, is disabled if the `-A` switch is given. There is a whole section covering inline assembler statements, see there.

- The normal calling convention -- for non-variadic functions -- is named "fastcall". The syntax for a function declaration that explicitly uses fastcall is

  > `

  >             <return type> fastcall <function name> (<parameter list>)

  > `

  or

  > `

  >             <return type> __fastcall__ <function name> (<parameter list>)

  > `

  An example is

  > `

  >             void __fastcall__ f (unsigned char c)

  > `

  The first form of the fastcall keyword is in the user namespace and can therefore be disabled with the `--standard` command line option. For functions that are `fastcall`, the rightmost parameter is not pushed on the stack but left in the primary register when the function is called. That significantly reduces the cost of calling those functions.
- There is another calling convention named "cdecl". Variadic functions (their prototypes have an ellipsis [`...`]) always use that convention. The syntax for a function declaration using cdecl is

  > `

  >             <return type> cdecl <function name> (<parameter list>)

  > `

  or

  > `

  >             <return type> __cdecl__ <function name> (<parameter list>)

  > `

  An example is

  > `

  >             int* __cdecl__ f (unsigned char c)

  > `

  The first form of the cdecl keyword is in the user namespace; and therefore, can be disabled with the `--standard` command-line option. For functions that are `cdecl`, the rightmost parameter is pushed onto the stack before the function is called. That increases the cost of calling those functions, especially when they are called from many places.
- There are two pseudo variables named `__AX__` and `__EAX__`. Both refer to the primary register that is used by the compiler to evaluate expressions or return function results. `__AX__` is of type `unsigned int` and `__EAX__` of type `long unsigned int` respectively. The pseudo variables may be used as lvalue and rvalue as every other variable. They are most useful together with short sequences of assembler code. For example, the macro

  > `

  >             #define hi(x)           \
  >                 (__AX__ = (x),      \
  >                  asm ("txa"),       \
  >                  asm ("ldx #$00"),  \
  >                  __AX__)

  > `

  will give the high byte of any unsigned value.
- Inside a function, the identifier `__func__` gives the name of the current function as a string. Outside of functions, `__func__` is undefined. Example:

  > `

  >             #define PRINT_DEBUG(s)  printf ("%s: %s\n", __func__, s);

  > `

  The macro will print the name of the current function plus a given string.
- cc65 allows the initialization of `void` variables. This may be used to create arbitrary structures that are more compatible with interfaces written for assembler languages. Here is an example:

  > `

  >             void GCmd = { (char)3, (unsigned)0x2000, (unsigned)0x3000 };

  > `

  That will be translated as follows:

  > `

  >             _GCmd:
  >                     .byte   3
  >                     .word   $2000
  >                     .word   $3000

  > `

  Since the variable is of type `void`, you may not use it as-is. However, taking the address of the variable results in a `void*` which may be passed to any function expecting a pointer. Also, the `sizeof` operator will give the length of the initializer:

  > `

  >             GLen = sizeof GCmd;

  > `

  will assign the value 5 to `GLen`. See the GEOS library document for examples on how to use that feature.
- cc65 implements flexible array struct members as defined in the C99 ISO standard. As an extension, these fields may be initialized. There are several exceptions, however (which is probably the reason why the standard does not define this feature, because it is highly unorthogonal). Flexible array members cannot be initialized ...
  - ... when defining an array of structs with flexible members.
  - ... if such a struct is a member field of another struct which is not the last field.
  - ... if the struct which contains a flexible array member is declared as `register`, and the size and compiler settings do allow the compiler actually to place the struct into the register bank in the zero page.
  Please note that -- as defined in the ISO C standard -- the `sizeof` operator returns the struct size with the flexible array member having size zero, even if it is initialized.
- Computed gotos, a GCC extension, has limited support. With it you can use fast jump tables from C. You can take the address of a label with a double ampersand, putting them in a static const array of type void *. Then you can jump to one of these labels as follows:

  > `

  >             static const void * const jumptable[] = {
  >                     &&add,
  >                     &&sub
  >             };
  >             goto *jumptable[somevar];

  >             add:
  >             ...code...

  > `

  In the jump table, no expressions are supported. The array index used in the goto must be a simple variable or a constant.

## 6. Predefined macros

The compiler defines several macros at startup:

`__APPLE2__`

  This macro is defined if the target is the Apple ][ (-t apple2) or the enhanced Apple //e (-t apple2enh).

`__APPLE2ENH__`

  This macro is defined if the target is the enhanced Apple //e (-t apple2enh).

`__ATARI2600__`

  This macro is defined if the target is the Atari 2600 game console.

`__ATARI5200__`

  This macro is defined if the target is the Atari 5200 game console.

`__ATARI__`

  This macro is defined if the target is the Atari 400/800 (-t atari) or the Atari 800XL/130XE (-t atarixl).

`__ATARIXL__`

  This macro is defined if the target is the Atari 800XL/130XE (-t atarixl).

`__ATMOS__`

  This macro is defined if the target is the Oric Atmos (-t atmos).

`__C128__`

  This macro is defined if the target is the Commodore 128 (-t c128).

`__C16__`

  This macro is defined if the target is the Commodore 16/116 (-t c16) or the Commodore Plus/4 (-t plus4).

`__C64__`

  This macro is defined if the target is the Commodore 64 (-t c64).

`__CBM__`

  This macro is defined if the target system is one of the CBM targets.

`__CBM510__`

  This macro is defined if the target is the CBM 500 series of computers.

`__CBM610__`

  This macro is defined if the target is one of the CBM 600/700 family of computers (called B series in the US).

`__CC65__`

  This macro is always defined. Its value is the version number of the compiler in hex. For example, version 2.14 of the compiler has this macro defined as `0x02E0`.

`__CC65_STD__`

  This macro is defined to one of the following depending on the `--standard` command line option:

  - `__CC65_STD_C89__`
  - `__CC65_STD_C99__`
  - `__CC65_STD_CC65__`
`__DATE__`

  This macro expands to the date of translation of the preprocessing translation unit in the form "Mmm dd yyyy".

`__EAGERLY_INLINE_FUNCS__`

  Is defined if the compiler was called with the `--eagerly-inline-funcs` command line option.

`__FILE__`

  This macro expands to a string containing the name of the C source file.

`__GEOS__`

  This macro is defined if you are compiling for one of the GEOS systems.

`__GEOS_APPLE__`

  This macro is defined if you are compiling for the Apple GEOS system (-t geos-apple).

`__GEOS_CBM__`

  This macro is defined if you are compiling for the GEOS 64/128 system (-t geos-cbm).

`__LINE__`

  This macro expands to the current line number.

`__LUNIX__`

  This macro is defined if you are compiling for the LUnix system (-t lunix).

`__LYNX__`

  This macro is defined if the target is the Atari Lynx (-t lynx).

`__NES__`

  This macro is defined if the target is the Nintendo Entertainment System (-t nes).

`__OPT__`

  Is defined if the compiler was called with the `-O` command line option.

`__OPT_i__`

  Is defined if the compiler was called with the `-Oi` command line option.

`__OPT_r__`

  Is defined if the compiler was called with the `-Or` command line option.

`__OPT_s__`

  Is defined if the compiler was called with the `-Os` command line option.

`__OSIC1P__`

  This macro is defined if the target is the Ohio Scientific Challenger 1P (-t osic1p).

`__PET__`

  This macro is defined if the target is the PET family of computers (-t pet).

`__PLUS4__`

  This macro is defined if the target is the Commodore Plus/4 (-t plus4).

`__STDC_HOSTED__`

  This macro is expands to the integer constant 1.

`__SIM6502__`

  This macro is defined if the target is sim65 in 6502 mode (-t sim6502).

`__SIM65C02__`

  This macro is defined if the target is sim65 in 65C02 mode (-t sim65c02).

`__SUPERVISION__`

  This macro is defined if the target is the Supervision (-t supervision).

`__TELESTRAT__`

  This macro is defined if the target is the Telestrat (-t telestrat).

`__TIME__`

  This macro expands to the time of translation of the preprocessing translation unit in the form "hh:mm:ss".

`__VIC20__`

  This macro is defined if the target is the Commodore VIC20 (-t vic20).

## 7. #pragmas

The compiler understands some pragmas that may be used to change code generation and other stuff. Some of these pragmas understand a special form: If the first parameter is `push`, the old value is saved onto a stack before changing it. The value may later be restored by using the `pop` parameter with the `#pragma`.

## 7.1 `#pragma allow-eager-inline ([push,] on|off)`

Allow eager inlining of known functions. If the argument is "off", eager inlining is disabled, otherwise it is enabled. Please note that (in contrast to the `--eagerly-inline-funcs` command line option) this pragma does not imply the `--inline-stdfuncs` command line option. Rather it marks code to be safe for eager inlining of known functions if inlining of standard functions is enabled.

The `#pragma` understands the push and pop parameters as explained above.

## 7.2 `#pragma bss-name ([push,] <name>)`

This pragma changes the name used for the BSS segment (the BSS segment is used to store uninitialized data). The argument is a string enclosed in double quotes.

Note: The default linker configuration file does only map the standard segments. If you use other segments, you have to create a new linker configuration file.

Beware: The startup code will zero only the default BSS segment. If you use another BSS segment, you have to do that yourself, otherwise uninitialized variables do not have the value zero.

The `#pragma` understands the push and pop parameters as explained above.

Example:

> `

>             #pragma bss-name ("MyBSS")

> `

## 7.3 `#pragma charmap (<index>, <code>)`

Each literal string and each literal character in the source is translated by use of a translation table. That translation table is preset when the compiler is started, depending on the target system; for example, to map ISO-8859-1 characters into PETSCII if the target is a Commodore machine.

This pragma allows to change entries in the translation table, so the translation for individual characters, or even the complete table may be adjusted. Both arguments are assumed to be unsigned characters with a valid range of 0-255.

Beware of some pitfalls:

- The character index is actually the code of the character in the C source; so, character mappings do always depend on the source character set. That means that `#pragma charmap()` is not portable -- it depends on the build environment.
- While it is possible to use character literals as indices, the result may be somewhat unexpected, since character literals are themselves translated. For that reason, I would suggest to avoid character literals, and use numeric character codes instead.
- It is risky to change index `0x00`, because string functions depend on it. If it is changed, then the `'\0'` at the end of string literals will become non-zero. Functions that are used on those literals won't stop at the end of them. cc65 will warn you if you do change that code number. You can turn off that `remap-zero` warning if you are certain that you know what you are doing (see `#pragma warn()`).

Example:

> `

>       /* Use a space wherever an 'a' occurs in ISO-8859-1 source */
>       #pragma charmap (0x61, 0x20);

> `

## 7.4 `#pragma check-stack ([push,] on|off)`

Tells the compiler to insert calls to a stack checking subroutine to detect stack overflows. The stack checking code will lead to somewhat larger and slower programs, so you may want to use this pragma when debugging your program and switch it off for the release version. If a stack overflow is detected, the program is aborted.

If the argument is "off", stack checks are disabled (the default), otherwise they're enabled.

The `#pragma` understands the push and pop parameters as explained above.

## 7.5 `#pragma code-name ([push,] <name>)`

This pragma changes the name used for the CODE segment (the CODE segment is used to store executable code). The argument is a string enclosed in double quotes.

Note: The default linker configuration file does only map the standard segments. If you use other segments, you have to create a new linker configuration file.

The `#pragma` understands the push and pop parameters as explained above.

Example:

> `

>             #pragma code-name ("MyCODE")

> `

## 7.6 `#pragma codesize ([push,] <int>)`

This pragma allows finer control about speed vs. size decisions in the code generation and optimization phase. It gives the allowed size increase factor (in percent). The default is can be changed by use of the `--codesize` compiler option.

The `#pragma` understands the push and pop parameters as explained above.

## 7.7 `#pragma data-name ([push,] <name>)`

This pragma changes the name used for the DATA segment (the DATA segment is used to store initialized data). The argument is a string enclosed in double quotes.

Note: The default linker configuration file does only map the standard segments. If you use other segments, you have to create a new linker configuration file.

The `#pragma` understands the push and pop parameters as explained above.

Example:

> `

>             #pragma data-name ("MyDATA")

> `

## 7.8 `#pragma inline-stdfuncs ([push,] on|off)`

Allow the compiler to inline some standard functions from the C library like strlen. If the argument is "off", inlining is disabled, otherwise it is enabled.

See also the `--inline-stdfuncs` command line option.

The `#pragma` understands the push and pop parameters as explained above.

## 7.9 `#pragma local-strings ([push,] on|off)`

When "on", emit string literals to the data segment when they're encountered in the source. The default ("off") is to keep string literals until end of assembly, merge read only literals if possible, and then output the literals into the data or rodata segment that is active at that point.

Using this `#pragma` it is possible to control the behaviour from within the source. When `#pragma local-strings` is active, string literals are output immediately, which means that they go into the currently active data or rodata segment, but cannot be merged. When inactive, string literals are remembered and output as a whole when translation is finished.

## 7.10 `#pragma message (<message>)`

This pragma is used to display informational messages at compile-time.

The message intented to be displayed must be a string literal.

Example:

> `

>             #pragma message ("in a bottle")

> `

Results in the compiler outputting the following to stderr:

> `

>             example.c(42): Note: in a bottle

> `

## 7.11 `#pragma optimize ([push,] on|off)`

Switch optimization on or off. If the argument is "off", optimization is disabled, otherwise it is enabled. Please note that this pragma only effects whole functions. The setting in effect when the function is encountered will determine if the generated code is optimized or not.

Optimization and code generation is also controlled by the codesize pragma.

The default is "off", but may be changed with the `-O` compiler option.

The `#pragma` understands the push and pop parameters as explained above.

## 7.12 `#pragma rodata-name ([push,] <name>)`

This pragma changes the name used for the RODATA segment (the RODATA segment is used to store readonly data). The argument is a string enclosed in double quotes.

Note: The default linker configuration file does only map the standard segments. If you use other segments, you have to create a new linker configuration file.

The `#pragma` understands the push and pop parameters as explained above.

Example:

> `

>             #pragma rodata-name ("MyRODATA")

> `

## 7.13 `#pragma regvaraddr ([push,] on|off)`

The compiler does not allow to take the address of register variables. The regvaraddr pragma changes this. Taking the address of a register variable is allowed after using this pragma with "on" as argument. Using "off" as an argument switches back to the default behaviour.

Beware: The C standard does not allow taking the address of a variable declared as register. So your programs become non-portable if you use this pragma. In addition, your program may not work. This is usually the case if a subroutine is called with the address of a register variable, and this subroutine (or a subroutine called from there) uses register variables. So be careful with this #pragma.

The `#pragma` understands the push and pop parameters as explained above.

Example:

> `

>             #pragma regvaraddr(on)  /* Allow taking the address
>                                      * of register variables
>                                      */

> `

## 7.14 `#pragma register-vars ([push,] on|off)`

Enables or disables use of register variables. If register variables are disabled (the default), the `register` keyword is ignored. Register variables are explained in more detail in a separate chapter.

The `#pragma` understands the push and pop parameters as explained above.

## 7.15 `#pragma signed-chars ([push,] on|off)`

Changes the signedness of the default character type. If the argument is "on", default characters are signed, otherwise characters are unsigned. The compiler default is to make characters unsigned since this creates a lot better code. This default may be overridden by the `--signed-chars` command line option.

The `#pragma` understands the push and pop parameters as explained above.

## 7.16 `#pragma static-locals ([push,] on|off)`

Use variables in the bss segment instead of variables on the stack. This pragma changes the default set by the compiler option `--static-locals`. If the argument is "on", local variables are allocated in the BSS segment, leading to shorter and in most cases faster, but non-reentrant code.

The `#pragma` understands the push and pop parameters as explained above.

## 7.17 `#pragma warn (name, [push,] on|off)`

Switch compiler warnings on or off. "name" is the name of a warning (see the `-W` compiler option for a list). The name is followed either by "pop", which restores the last pushed state, or by "on" or "off", optionally preceeded by "push" to push the current state before changing it.

Example:

> `

>             /* Don't warn about the unused parameter in function func */
>             #pragma warn (unused-param, push, off)
>             static int func (int unused)
>             {
>                 return 0;
>             }
>             #pragma warn (unused-param, pop)

> `

## 7.18 `#pragma wrapped-call (push, <name>, <identifier>)`

This pragma sets a wrapper for functions, often used for trampolines.

The name is a function returning `void`, and taking no parameters. It must preserve the CPU's `A` and `X` registers if it wraps any `__fastcall__` functions that have parameters. It must preserve the `Y` register if it wraps any variadic functions (they have "`...`" in their prototypes).

The identifier is an 8-bit number that's set into `tmp4`.

The address of a wrapped function is passed in `ptr4`. The wrapper can call that function by using "`jsr callptr4`".

This feature is useful, for example, with banked memory, to switch banks automatically to where a wrapped function resides, and then to restore the previous bank when it returns.

The `#pragma` requires the push or pop argument as explained above.

Example:

> `

>     /* Note that this code can be in a header. */
>     void mytrampoline(void); /* Doesn't corrupt __AX__ */

>     #pragma wrapped-call (push, mytrampoline, 5)
>     void somefunc1(void);
>     void somefunc2(int, char *);
>     #pragma wrapped-call (pop)

> `

## 7.19 `#pragma writable-strings ([push,] on|off)`

Changes the storage location of string literals. For historical reasons, the C standard defines that string literals are of type "char[]", but writing to such a literal causes undefined behaviour. Most compilers (including cc65) place string literals in the read-only data segment, which may cause problems with old C code that writes to string literals.

Using this pragma (or the corresponding command line option `--writable-strings`) causes the literals to be placed in the data segment so they can be written to without worry.

The `#pragma` understands the push and pop parameters as explained above.

## 7.20 `#pragma zpsym (<name>)`

Tell the compiler that the -- previously as external declared -- symbol with the given name is a zero page symbol (usually from an assembler file). The compiler will create a matching import declaration for the assembler.

Example:

> `

>             extern int foo;
>             #pragma zpsym ("foo");  /* foo is in the zeropage */

> `

## 8. Register variables

The runtime for all supported platforms has 6 bytes of zero page space available for register variables (this could be increased, but I think it's a good value). So you can declare register variables up to a total size of 6 per function. The compiler will allocate register space on a "first come, first served" base and convert any `register` declarations that exceed the available register space silently to `auto`. Parameters can also be declared as `register`, this will in fact give slightly shorter code than using a register variable.

Since a function must save the current values of the registers on entry and restore them on exit, there is an overhead associated with register variables, and this overhead is quite high (about 20 bytes per variable). This means that just declaring anything as `register` is not a good idea.

The best use for register variables are pointers, especially those that point to structures. The magic number here is about 3 uses of a struct field: If the function contains this number or even more, the generated code will be usually shorter and faster when using a register variable for the struct pointer. The reason for this is that the register variable can in many cases be used as a pointer directly. Having a pointer in an auto variable means that this pointer must first be copied into a zero page location, before it can be dereferenced.

Second best use for register variables are counters. However, there is not much difference in the code generated for counters, so you will need at least 100 operations on this variable (for example in a loop) to make it worth the trouble. The only savings you get here are by the use of a zero page variable instead of one on the stack or in the data segment.

Register variables must be explicitly enabled, either by using `-Or` or `--register-vars` on the command line or by use of `#pragma register-vars`. Register variables are only accepted on function top level, register variables declared in interior blocks are silently converted to `auto`. With register variables disabled, all variables declared as `register` are actually auto variables.

Please take care when using register variables: While they are helpful and can lead to a tremendous speedup when used correctly, improper usage will cause bloated code and a slowdown.

## 9. Inline assembler

The compiler allows to insert assembler statements into the output file. The syntax is

> `

>             asm [optional volatile] (<string literal>[, optional parameters]) ;

> `

or

> `

>             __asm__ [optional volatile] (<string literal>[, optional parameters]) ;

> `

The first form is in the user namespace; and, is disabled by `--standard` if the argument is not `cc65`.

The `asm` statement can be used only inside a function. Please note that the result of an inline assembler expression is always of type `void`.

The contents of the string literal are preparsed by the compiler; and, inserted into the generated assembly output, so that it can be processed further by the backend -- and, especially the optimizer. For that reason, the compiler does allow only regular 6502 opcodes to be used with the inline assembler. Pseudo instructions (like `.import`, `.byte`, and so on) are not allowed, even if the ca65 assembler (which is used to translate the generated assembler code) would accept them. The built-in inline assembler is not a replacement for the full-blown macro assembler which comes with the compiler.

Note: Inline assembler statements are subject to all optimizations done by the compiler. There currently is no way to protect an inline assembler statement -- alone -- from being moved or removed completely by the optimizer. If in doubt, check the generated assembler output; or, disable optimizations (for that function).

As a shortcut, you can put the `volatile` qualifier in your `asm` statements. It will disable optimization for the functions in which those `asm volatile` statements sit. The effect is the same as though you put `#pragma optimize(push, off)` above those functions, and `#pragma optimize(pop)` below those functions.

The string literal may contain format specifiers from the following list. For each format specifier, an argument is expected which is inserted instead of the format specifier, before passing the assembly code line to the backend.

- `%b` - Numerical 8-bit value
- `%w` - Numerical 16-bit value
- `%l` - Numerical 32-bit value
- `%v` - Assembler name of a global variable or function
- `%o` - Stack offset of a local variable
- `%g` - Assembler name of a C label
- `%s` - The argument is converted to a string
- `%%` - The % sign itself

Using those format specifiers, you can access C `#defines`, variables, or similar stuff from the inline assembler. For example, to load the value of a C `#define` into the Y index register, one would use

> `

>             #define OFFS  23
>             __asm__ ("ldy #%b", OFFS);

> `

Or, to access a struct member of a static variable:

> `

>             typedef struct {
>                 unsigned char x;
>                 unsigned char y;
>                 unsigned char color;
>             } pixel_t;
>             static pixel_t pixel;
>             __asm__ ("ldy #%b", offsetof(pixel_t, color));
>             __asm__ ("lda %v,y", pixel);

> `

The next example shows how to use global variables to exchange data between C and assembler; and, how to handle assembler jumps:

> `

>             static unsigned char globalSubA, globalSubB, globalSubResult;

>             /* return a-b, return 255 if b>a */
>             unsigned char sub (unsigned char a, unsigned char b)
>             {
>                 globalSubA = a;
>                 globalSubB = b;
>                 __asm__ ("sec");
>                 __asm__ ("lda %v", globalSubA);
>                 __asm__ ("sbc %v", globalSubB);
>                 __asm__ ("bcs %g", jumpSubNoError);
>                 __asm__ ("lda #$FF");
>             jumpSubNoError:
>                 __asm__ ("sta %v", globalSubResult);
>                 return globalSubResult;
>             }

> `

Arrays also can be accessed:

> `

>             static const unsigned char globalSquareTable[] = {
>                 0, 1, 4, 9, 16, 25, 36, 49, 64, 81,
>                 100, 121, 144, 169, 196, 225
>             };
>             static unsigned char globalSquareA, globalSquareResult;

>             /* return a*a for a<16, else 255 */
>             unsigned char square (unsigned char a)
>             {
>                 if (a > 15) {
>                     return 255;
>                 }
>                 globalSquareA = a;
>                 __asm__ ("ldx %v", globalSquareA);
>                 __asm__ ("lda %v,x", globalSquareTable);
>                 __asm__ ("sta %v", globalSquareResult);
>                 return globalSquareResult;
>             }

> `

Note: Do not embed the assembler labels that are used as names of global variables or functions into your `asm` statements. Code such as this:

> `

>             int foo;
>             int bar (void) { return 1; }
>             ...
>             __asm__ ("lda _foo");           /* DON'T DO THAT! */
>             ...
>             __asm__ ("jsr _bar");           /* DON'T DO THAT EITHER! */

> `

might stop working if the way that the compiler generates those names is changed in a future version. Instead, use the format specifiers from the table above:

> `

>             __asm__ ("lda %v", foo);        /* OK */
>             ...
>             __asm__ ("jsr %v", bar);        /* OK */

> `

## 10. Implementation-defined behavior

This section describes the behavior of cc65 when the standard describes the behavior as implementation-defined.

(to be done)

## 11. Copyright

This is the original compiler copyright:

> `

>     --------------------------------------------------------------------------
>       -*- Mode: Text -*-

>          This is the copyright notice for RA65, LINK65, LIBR65, and other
>       Atari 8-bit programs.  Said programs are Copyright 1989, by John R.
>       Dunning.  All rights reserved, with the following exceptions:

>           Anyone may copy or redistribute these programs, provided that:

>       1:  You don't charge anything for the copy.  It is permissable to
>           charge a nominal fee for media, etc.

>       2:  All source code and documentation for the programs is made
>           available as part of the distribution.

>       3:  This copyright notice is preserved verbatim, and included in
>           the distribution.

>           You are allowed to modify these programs, and redistribute the
>       modified versions, provided that the modifications are clearly noted.

>           There is NO WARRANTY with this software, it comes as is, and is
>       distributed in the hope that it may be useful.

>           This copyright notice applies to any program which contains
>       this text, or the refers to this file.

>           This copyright notice is based on the one published by the Free
>       Software Foundation, sometimes known as the GNU project.  The idea
>       is the same as theirs, ie the software is free, and is intended to
>       stay that way.  Everybody has the right to copy, modify, and re-
>       distribute this software.  Nobody has the right to prevent anyone
>       else from copying, modifying or redistributing it.

>     --------------------------------------------------------------------------

> `

Small parts of the compiler (parts of the preprocessor and main parser) are still covered by this copyright. The main portion is covered by the usual cc65 license, which reads:

This software is provided 'as-is', without any expressed or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.