]> git.sur5r.net Git - cc65/blob - doc/cc65.sgml
6ae45dcacb5340c12c3fd48a495f8fb1c6d3b11a
[cc65] / doc / cc65.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4 <title>cc65 Users Guide
5 <author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">
6 <date>2000-09-03, 2001-10-02, 2005-08-01
7
8 <abstract>
9 cc65 is a C compiler for 6502 targets. It supports several 6502 based home
10 computers like the Commodore and Atari machines, but it is easily retargetable.
11 </abstract>
12
13 <!-- Table of contents -->
14 <toc>
15
16 <!-- Begin the document -->
17
18
19 <sect>Overview<p>
20
21 cc65 was originally a C compiler for the Atari 8-bit machines written by
22 John R. Dunning. In prior releases I've described the compiler by listing
23 up the changes made by me. I have made many more changes in the meantime
24 (and rewritten major parts of the compiler), so I will no longer do that,
25 since the list would be too large and of no use to anyone. Instead I will
26 describe the compiler in respect to the ANSI/ISO C standard.
27
28 There are separate documents named <url url="library.html"> and <url
29 url="funcref.html"> that cover the library that is available for the compiler.
30 If you know C, and are interested in doing actual programming, the library
31 documentation is probably of much more use than this document.
32
33 If you need some hints for getting the best code out of the compiler, you
34 may have a look at <url url="coding.html"> which covers some code generation
35 issues.
36
37
38
39 <sect>Usage<p>
40
41 The compiler translates C files into files containing assembly code that
42 may be translated by the ca65 macroassembler (for more information about
43 the assembler, have a look at <url url="ca65.html">).
44
45
46 <sect1>Command line option overview<p>
47
48 The compiler may be called as follows:
49
50 <tscreen><verb>
51 ---------------------------------------------------------------------------
52 Usage: cc65 [options] file
53 Short options:
54   -Cl                   Make local variables static
55   -Dsym[=defn]          Define a symbol
56   -I dir                Set an include directory search path
57   -O                    Optimize code
58   -Oi                   Optimize code, inline more code
59   -Or                   Enable register variables
60   -Os                   Inline some known functions
61   -T                    Include source as comment
62   -V                    Print the compiler version number
63   -W name[,name]        Enable or disable warnings
64   -d                    Debug mode
65   -g                    Add debug info to object file
66   -h                    Help (this text)
67   -j                    Default characters are signed
68   -o name               Name the output file
69   -r                    Enable register variables
70   -t sys                Set the target system
71   -v                    Increase verbosity
72
73 Long options:
74   --add-source          Include source as comment
75   --bss-name seg        Set the name of the BSS segment
76   --check-stack         Generate stack overflow checks
77   --code-name seg       Set the name of the CODE segment
78   --codesize x          Accept larger code by factor x
79   --cpu type            Set cpu type
80   --create-dep          Create a make dependency file
81   --data-name seg       Set the name of the DATA segment
82   --debug               Debug mode
83   --debug-info          Add debug info to object file
84   --forget-inc-paths    Forget include search paths
85   --help                Help (this text)
86   --include-dir dir     Set an include directory search path
87   --register-space b    Set space available for register variables
88   --register-vars       Enable register variables
89   --rodata-name seg     Set the name of the RODATA segment
90   --signed-chars        Default characters are signed
91   --standard std        Language standard (c89, c99, cc65)
92   --static-locals       Make local variables static
93   --target sys          Set the target system
94   --verbose             Increase verbosity
95   --version             Print the compiler version number
96   --writable-strings    Make string literals writable
97 ---------------------------------------------------------------------------
98 </verb></tscreen>
99
100
101 <sect1>Command line options in detail<p>
102
103 Here is a description of all the command line options:
104
105 <descrip>
106
107   <label id="option-bss-name">
108   <tag><tt>--bss-name seg</tt></tag>
109
110   Set the name of the bss segment. See also <tt/<ref id="pragma-bss-name"
111   name="#pragma&nbsp;bss-name">/.
112
113
114   <label id="option-check-stack">
115   <tag><tt>--check-stack</tt></tag>
116
117   Tells the compiler to generate code that checks for stack overflows. See
118   <tt/<ref id="pragma-check-stack" name="#pragma&nbsp;check-stack">/ for an
119   explanation of this feature.
120
121
122   <label id="option-code-name">
123   <tag><tt>--code-name seg</tt></tag>
124
125   Set the name of the code segment. See also <tt/<ref id="pragma-code-name"
126   name="#pragma&nbsp;code-name">/
127
128
129   <label id="option-codesize">
130   <tag><tt>--codesize x</tt></tag>
131
132   This options allows finer control about speed vs. size decisions in the code
133   generation and optimization phases. It gives the allowed size increase
134   factor (in percent). The default is 100 when not using <tt/-Oi/ and 200 when
135   using <tt/-Oi/ (<tt/-Oi/ is the same as <tt/-O --codesize&nbsp;200/).
136
137
138   <label id="option-cpu">
139   <tag><tt>--cpu CPU</tt></tag>
140
141   A new, still experimental option. You may specify "6502" or "65C02" as
142   the CPU. 6502 is the default, so this will not change anything.
143   Specifying 65C02 will use a few 65C02 instructions when generating code.
144   Don't expect too much from this option: It is still new (and may have
145   bugs), and the additional instructions for the 65C02 are not that
146   overwhelming.
147
148
149   <label id="option-create-dep">
150   <tag><tt>--create-dep</tt></tag>
151
152   Tells the compiler to generate a file containing the dependency list for
153   the compiled module in makefile syntax. The file is named as the C input
154   file with the extension replaced by <tt/.u/.
155
156
157   <label id="option-data-name">
158   <tag><tt>--data-name seg</tt></tag>
159
160   Set the name of the data segment. See also <tt/<ref id="pragma-data-name"
161   name="#pragma&nbsp;data-name">/
162
163
164   <tag><tt>-d, --debug</tt></tag>
165
166   Enables debug mode, something that should not be needed for mere
167   mortals:-)
168
169
170   <tag><tt>-D sym[=definition]</tt></tag>
171
172   Define a macro on the command line. If no definition is given, the macro
173   is defined to the value "1".
174
175
176   <tag><tt>--forget-inc-paths</tt></tag>
177
178   Forget the builtin include paths. This is most useful when building
179   customized C or runtime libraries, in which case the standard header
180   files should be ignored.
181
182
183   <tag><tt>-g, --debug-info</tt></tag>
184
185   This will cause the compiler to insert a <tt/.DEBUGINFO/ command into the
186   generated assembler code. This will cause the assembler to include all
187   symbols in a special section in the object file.
188
189
190   <tag><tt>-h, --help</tt></tag>
191
192   Print the short option summary shown above.
193
194
195   <tag><tt>-o name</tt></tag>
196
197   Specify the name of the output file. If you don't specify a name, the
198   name of the C input file is used, with the extension replaced by ".s".
199
200
201   <label id="option-register-vars">
202   <tag><tt>-r, --register-vars</tt></tag>
203
204   <tt/-r/ will make the compiler honor the <tt/register/ keyword. Local
205   variables may be placed in registers (which are actually zero page
206   locations). There is some overhead involved with register variables, since
207   the old contents of the registers must be saved and restored. Since register
208   variables are of limited use without the optimizer, there is also a combined
209   switch: <tt/-Or/ will enable both, the optimizer and register variables.
210
211   For more information about register variables see <ref id="register-vars"
212   name="register variables">.
213
214   The compiler setting can also be changed within the source file by using
215   <tt/<ref id="pragma-register-vars" name="#pragma&nbsp;register-vars">/.
216
217
218   <label id="option-register-space">
219   <tag><tt>--register-space</tt></tag>
220
221   This option takes a numeric parameter and is used to specify, how much
222   zero page register space is available. Please note that just giving this
223   option will not increase or decrease by itself, it will just tell the
224   compiler about the available space. You will have to allocate that space
225   yourself using an assembler module with the necessary allocations, and a
226   linker configuration that matches the assembler module. The default value
227   for this option is 6 (bytes).
228
229   If you don't know what all this means, please don't use this option.
230
231
232   <label id="option-rodata-name">
233   <tag><tt>--rodata-name seg</tt></tag>
234
235   Set the name of the rodata segment (the segment used for readonly data).
236   See also <tt/<ref id="pragma-rodata-name" name="#pragma&nbsp;rodata-name">/
237
238   <label id="option-signed-chars">
239   <tag><tt>-j, --signed-chars</tt></tag>
240
241   Using this option, you can make the default characters signed. Since the
242   6502 has no provisions for sign extending characters (which is needed on
243   almost any load operation), this will make the code larger and slower. A
244   better way is to declare characters explicitly as "signed" if needed. You
245   can also use <tt><ref id="pragma-signed-chars"
246   name="#pragma&nbsp;signed-chars"></tt> for better control of this option.
247
248
249   <label id="option--standard">
250   <tag><tt>--standard std</tt></tag>
251
252   This option allows to set the language standard supported. The argument is
253   one of
254   <itemize>
255   <item>c89
256   <item>c99
257   <item>cc65
258   </itemize>
259
260   Please note that the compiler does not support the c99 standard and never
261   will. c99 mode is actually c89 mode with a few selected c99 extensions
262   (// comments for example).
263
264
265   <label id="option-standard">
266   <tag><tt>-t target, --target target</tt></tag>
267
268   This option is used to set the target system. The target system
269   determines things like the character set that is used for strings and
270   character constants. The following target systems are supported:
271
272   <itemize>
273   <item>none
274   <item>apple2
275   <item>apple2enh
276   <item>atari
277   <item>atmos
278   <item>c16 (works also for the c116 with memory up to 32K)
279   <item>c64
280   <item>c128
281   <item>cbm510 (CBM-II series with 40 column video)
282   <item>cbm610 (all CBM-II II computers with 80 column video)
283   <item>geos
284   <item>lunix
285   <item>lynx
286   <item>nes
287   <item>pet (all CBM PET systems except the 2001)
288   <item>plus4
289   <item>supervision
290   <item>vic20
291   </itemize>
292
293   <tag><tt>-v, --verbose</tt></tag>
294
295   Using this option, the compiler will be somewhat more verbose if errors
296   or warnings are encountered.
297
298
299   <label id="option-writable-strings">
300   <tag><tt>--writable-strings</tt></tag>
301
302   Make string literals writable by placing them into the data segment instead
303   of the rodata segment. You can also use <tt><ref id="pragma-writable-strings"
304   name="#pragma&nbsp;writable-strings"></tt> to control this option from within
305   the source file.
306
307
308   <label id="option-static-locals">
309   <tag><tt>-Cl, --static-locals</tt></tag>
310
311   Use static storage for local variables instead of storage on the stack.
312   Since the stack is emulated in software, this gives shorter and usually
313   faster code, but the code is no longer reentrant. The difference between
314   <tt/-Cl/ and declaring local variables as static yourself is, that
315   initializer code is executed each time, the function is entered. So when
316   using
317
318   <tscreen><verb>
319         void f (void)
320         {
321             unsigned a = 1;
322             ...
323         }
324   </verb></tscreen>
325
326   the variable <tt/a/ will always have the value <tt/1/ when entering the
327   function and using <tt/-Cl/, while in
328
329   <tscreen><verb>
330         void f (void)
331         {
332             static unsigned a = 1;
333             ....
334         }
335   </verb></tscreen>
336
337   the variable <tt/a/ will have the value <tt/1/ only the first time that the
338   function is entered, and will keep the old value from one call of the
339   function to the next.
340
341   You may also use <tt><ref id="pragma-static-locals"
342   name="#pragma&nbsp;static-locals"></tt> to change this setting in your
343   sources.
344
345
346   <label id="option-include-dir">
347   <tag><tt>-I dir, --include-dir dir</tt></tag>
348
349   Set a directory where the compiler searches for include files. You may
350   use this option multiple times to add more than one directory to the
351   search list.
352
353
354   <label id="option-O">
355   <tag><tt>-O, -Oi, -Or, -Os</tt></tag>
356
357   Enable an optimizer run over the produced code.
358
359   Using <tt/-Oi/, the code generator will inline some code where otherwise a
360   runtime functions would have been called, even if the generated code is
361   larger. This will not only remove the overhead for a function call, but will
362   make the code visible for the optimizer. <tt/-Oi/ is an alias for
363   <tt/-O --codesize&nbsp;200/.
364
365   <tt/-Or/ will make the compiler honor the <tt/register/ keyword. Local
366   variables may be placed in registers (which are actually zero page
367   locations). See also the <tt/<ref id="option-register-vars"
368   name="--register-vars">/ command line option, and the <ref
369   id="register-vars" name="discussion of register variables"> below.
370
371   Using <tt/-Os/ will force the compiler to inline some known functions from
372   the C library like strlen. Note: This has two consequences:
373   <p>
374   <itemize>
375   <item>You may not use names of standard C functions in your own code. If you
376         do that, your program is not standard compliant anyway, but using
377         <tt/-Os/ will actually break things.
378         <p>
379   <item>The inlined string and memory functions will not handle strings or
380         memory areas larger than 255 bytes. Similarly, the inlined <tt/is..()/
381         functions will not work with values outside the char. range (such as
382         <tt/EOF/).
383         <p>
384   </itemize>
385   <p>
386   It is possible to concatenate the modifiers for <tt/-O/. For example, to
387   enable register variables and inlining of known functions, you may use
388   <tt/-Ors/.
389
390
391   <tag><tt>-T, --add-source</tt></tag>
392
393   This include the source code as comments in the generated code. This is
394   normally not needed.
395
396
397   <tag><tt>-V, --version</tt></tag>
398
399   Print the version number of the compiler. When submitting a bug report,
400   please include the operating system you're using, and the compiler
401   version.
402
403
404   <label id="option-W">
405   <tag><tt>-W name[,name]</tt></tag>
406
407   This option allows to control warnings generated by the compiler. It is
408   followed by a comma separated list of warnings that should be enabled or
409   disabled. To disable a warning, its name is prefixed by a minus sign. If
410   no such prefix exists, or the name is prefixed by a plus sign, the warning
411   is enabled.
412
413   The following warning names are currently recognized:
414   <descrip>
415   <tag><tt/error/</tag>
416         Treat all warnings as errors.
417   <tag><tt/unknown-pragma/</tag>
418         Warn about known #pragmas.
419   <tag><tt/unused-label/</tag>
420         Warn about unused labels.
421   <tag><tt/unused-param/</tag>
422         Warn about unused function parameters.
423   <tag><tt/unused-var/</tag>
424         Warn about unused variables.
425   </descrip>
426
427   You may also use <tt><ref id="pragma-warn" name="#pragma&nbsp;warn"></tt> to
428   control this setting for smaller pieces of code from within your code.
429
430 </descrip><p>
431
432
433 <sect>Input and output<p>
434
435 The compiler will accept one C file per invocation and create a file with
436 the same base name, but with the extension replaced by ".s". The output
437 file contains assembler code suitable for the use with the ca65 macro
438 assembler.
439
440 Include files in single quotes are searched in the following places:
441 <enum>
442 <item>The current directory.
443 <item>The value of the environment variable <tt/CC65_INC/ if it is defined.
444 <item>Any directory added with the <tt/-I/ option on the command line.
445 </enum>
446
447 Include files in angle brackets are searched in the following places:
448 <enum>
449 <item>A compiled in directory which is often <tt>/usr/lib/cc65/include</tt> on
450       Linux systems.
451 <item>The value of the environment variable <tt/CC65_INC/ if it is defined.
452 <item>A subdirectory named <tt/include/ of the directory defined in the
453       environment variable <tt/CC65_HOME/, if it is defined.
454 <item>Any directory added with the <tt/-I/ option on the command line.
455 </enum>
456
457
458
459 <sect>Differences to the ISO standard<p>
460
461 Apart from the things listed below, the compiler does support additional
462 keywords, has several functions in the standard headers with names outside the
463 reserved namespace and a few syntax extensions. All these can be disabled with
464 the <tt><ref id="option--standard" name="--standard"></tt> command line
465 option. Its use for maximum standards compatibility is advised.
466
467 Here is a list of differences between the language, the compiler accepts,
468 and the one defined by the ISO standard:
469
470 <itemize>
471
472 <item>  The datatypes "float" and "double" are not available.
473         <p>
474 <item>  C Functions may not return structs (or unions), and structs may not
475         be passed as parameters by value. However, struct assignment *is*
476         possible.
477         <p>
478 <item>  Part of the C library is available only with fastcall calling
479         conventions (see below).  It means that you must not mix pointers to
480         those functions with pointers to user-written, not-fastcall functions.
481         <p>
482 <item>  The <tt/volatile/ keyword doesn't have an effect. This is not as bad
483         as it sounds, since the 6502 has so few registers that it isn't
484         possible to keep values in registers anyway.
485         <p>
486 </itemize>
487
488 There may be some more minor differences I'm currently not aware of. The
489 biggest problem is the missing float data type. With this limitation in
490 mind, you should be able to write fairly portable code.
491
492
493
494 <sect>Extensions<p>
495
496 This cc65 version has some extensions to the ISO C standard.
497
498 <itemize>
499
500 <item>  The compiler allows to insert assembler statements into the output
501         file. The syntax is
502
503         <tscreen><verb>
504         asm (&lt;string literal&gt;[, optional parameters]) ;
505         </verb></tscreen>
506         or
507         <tscreen><verb>
508         __asm__ (&lt;string literal&gt;[, optional parameters]) ;
509         </verb></tscreen>
510
511         The first form is in the user namespace and is disabled if the <tt/-A/
512         switch is given.
513
514         There is a whole section covering inline assembler statements,
515         <ref id="inline-asm" name="see there">.
516         <p>
517
518 <item>  There is a special calling convention named "fastcall".
519         The syntax for a function declaration using fastcall is
520
521         <tscreen><verb>
522         &lt;return type&gt; fastcall &lt;function name&gt; (&lt;parameter list&gt;)
523         </verb></tscreen>
524         or
525         <tscreen><verb>
526         &lt;return type&gt; __fastcall__ &lt;function name&gt; (&lt;parameter list&gt;)
527         </verb></tscreen>
528         An example would be
529         <tscreen><verb>
530         void __fastcall__ f (unsigned char c)
531         </verb></tscreen>
532         The first form of the fastcall keyword is in the user namespace and can
533         therefore be disabled with the <tt><ref id="option--standard"
534         name="--standard"></tt> command line option.
535
536         For functions declared as <tt/fastcall/, the rightmost parameter is not
537         pushed on the stack but left in the primary register when the function
538         is called. This will reduce the cost when calling assembler functions
539         significantly, especially when the function itself is rather small.
540         <p>
541
542 <item>  There are two pseudo variables named <tt/__AX__/ and <tt/__EAX__/.
543         Both refer to the primary register that is used by the compiler to
544         evaluate expressions or return function results. <tt/__AX__/ is of
545         type <tt/unsigned int/ and <tt/__EAX__/ of type <tt/long unsigned int/
546         respectively. The pseudo variables may be used as lvalue and rvalue as
547         every other variable. They are most useful together with short
548         sequences of assembler code. For example, the macro
549
550         <tscreen><verb>
551         #define hi(x)           \
552             (__AX__ = (x),      \
553              asm ("txa"),       \
554              asm ("ldx #$00"),  \
555              __AX__)
556         </verb></tscreen>
557
558         will give the high byte of any unsigned value.
559         <p>
560
561 <item>  Inside a function, the identifier <tt/__func__/ gives the name of the
562         current function as a string. Outside of functions, <tt/__func__/ is
563         undefined.
564         Example:
565
566         <tscreen><verb>
567         #define PRINT_DEBUG(s)  printf ("%s: %s\n", __func__, s);
568         </verb></tscreen>
569
570         The macro will print the name of the current function plus a given
571         string.
572         <p>
573
574 <item>  cc65 allows the initialization of <tt/void/ variables. This may be
575         used to create variable structures that are more compatible with
576         interfaces written for assembler languages. Here is an example:
577
578         <tscreen><verb>
579         void GCmd = {   (char)3, (unsigned)0x2000, (unsigned)0x3000 };
580         </verb></tscreen>
581
582         This will be translated as follows:
583
584         <tscreen><verb>
585         _GCmd:
586                 .byte   3
587                 .word   $2000
588                 .word   $3000
589         </verb></tscreen>
590
591         Since the variable is of type <tt/void/ you may not use it as is.
592         However, taking the address of the variable results in a <tt/void*/
593         which may be passed to any function expecting a pointer.
594
595         See the <url url="geos.html" name="GEOS library document"> for examples
596         on how to use this feature.
597         <p>
598
599 <item>  cc65 implements flexible array struct members as defined in the C99 ISO
600         standard. As an extension, these fields may be initialized. There are
601         several exceptions, however (which is probably the reason why the
602         standard does not define this feature, because it is highly
603         unorthogonal). Flexible array members cannot be initialized ...
604
605         <itemize>
606         <item>... when defining an array of structs with flexible
607                 members.
608         <item>... if such a struct is a member field of another struct
609                 which is not the last field.
610         <item>... if the struct which contains a flexible array member is
611                 declared as <tt/register/, and the size and compiler settings
612                 do allow the compiler actually to place the struct into the
613                 register bank in the zero page.
614         </itemize>
615
616         Please note that -- as defined in the ISO C standard -- the <tt/sizeof/
617         operator returns the struct size with the flexible array member having
618         size zero, even if it is initialized.
619         <p>
620
621 </itemize>
622 <p>
623
624
625 <sect>Predefined macros<p>
626
627 The compiler defines several macros at startup:
628
629 <descrip>
630
631   <tag><tt>__CC65__</tt></tag>
632
633   This macro is always defined. Its value is the version number of the
634   compiler in hex.  For example, version 2.10.1 of the compiler has this macro
635   defined as <tt/0x02A1/.
636
637   <tag><tt>__APPLE2__</tt></tag>
638
639   This macro is defined if the target is the Apple ][ (-t apple2).
640
641   <tag><tt>__APPLE2ENH__</tt></tag>
642
643   This macro is defined if the target is the enhanced Apple //e (-t apple2enh).
644
645   <tag><tt>__ATARI__</tt></tag>
646
647   This macro is defined if the target is one of the Atari computers
648   (400/800/130XL/800XL).
649
650   <tag><tt>__ATMOS__</tt></tag>
651
652   This macro is defined if the target is the Oric Atmos (-t atmos).
653
654   <tag><tt>__CBM__</tt></tag>
655
656   This macro is defined if the target system is one of the CBM targets.
657
658   <tag><tt>__C16__</tt></tag>
659
660   This macro is defined if the target is the c16 (-t c16).
661
662   <tag><tt>__C64__</tt></tag>
663
664   This macro is defined if the target is the c64 (-t c64).
665
666   <tag><tt>__C128__</tt></tag>
667
668   This macro is defined if the target is the c128 (-t c128).
669
670   <tag><tt>__CBM510__</tt></tag>
671
672   This macro is defined if the target is the CBM 500 series of computers.
673
674   <tag><tt>__CBM610__</tt></tag>
675
676   This macro is defined if the target is one of the CBM 600/700 family of
677   computers (called B series in the US).
678
679   <tag><tt>__GEOS__</tt></tag>
680
681   This macro is defined if you are compiling for the GEOS system (-t geos).
682
683   <tag><tt>__LUNIX__</tt></tag>
684
685   This macro is defined if you are compiling for the LUnix system (-t lunix).
686
687   <tag><tt>__LYNX__</tt></tag>
688
689   This macro is defined if the target is the Atari Lynx (-t lynx).
690
691   <tag><tt>__NES__</tt></tag>
692
693   This macro is defined if the target is the NES (-t nes).
694
695   <tag><tt>__PET__</tt></tag>
696
697   This macro is defined if the target is the PET family of computers (-t pet).
698
699   <tag><tt>__PLUS4__</tt></tag>
700
701   This macro is defined if the target is the plus/4 (-t plus4).
702
703   <tag><tt>__SUPERVISION__</tt></tag>
704
705   This macro is defined if the target is the supervision (-t supervision).
706
707   <tag><tt>__VIC20__</tt></tag>
708
709   This macro is defined if the target is the vic20 (-t vic20).
710
711   <tag><tt>__FILE__</tt></tag>
712
713   This macro expands to a string containing the name of the C source file.
714
715   <tag><tt>__LINE__</tt></tag>
716
717   This macro expands to the current line number.
718
719   <tag><tt>__CC65_STD__</tt></tag>
720
721   This macro is defined to one of the following depending on the <tt><ref
722   id="option--standard" name="--standard"></tt> command line option:
723   <itemize>
724   <item><tt/__CC65_STD_C89__/
725   <item><tt/__CC65_STD_C99__/
726   <item><tt/__CC65_STD_CC65__/
727   </itemize>
728
729   <tag><tt>__OPT__</tt></tag>
730
731   Is defined if the compiler was called with the <tt/-O/ command line option.
732
733   <tag><tt>__OPT_i__</tt></tag>
734
735   Is defined if the compiler was called with the <tt/-Oi/ command line option.
736
737   <tag><tt>__OPT_r__</tt></tag>
738
739   Is defined if the compiler was called with the <tt/-Or/ command line option.
740
741   <tag><tt>__OPT_s__</tt></tag>
742
743   Is defined if the compiler was called with the <tt/-Os/ command line option.
744
745 </descrip>
746
747
748 <sect>&num;pragmas<label id="pragmas"><p>
749
750 The compiler understands some pragmas that may be used to change code
751 generation and other stuff. Some of these pragmas understand a special form:
752 If the first parameter is <tt/push/, the old value is saved onto a stack
753 before changing it. The value may later be restored by using the <tt/pop/
754 parameter with the <tt/#pragma/.
755
756 <sect1><tt>#pragma bss-name (&lsqb;push,&rsqb; &lt;name&gt;)</tt><label id="pragma-bss-name"><p>
757
758   This pragma changes the name used for the BSS segment (the BSS segment
759   is used to store uninitialized data). The argument is a string enclosed
760   in double quotes.
761
762   Note: The default linker configuration file does only map the standard
763   segments. If you use other segments, you have to create a new linker
764   configuration file.
765
766   Beware: The startup code will zero only the default BSS segment. If you
767   use another BSS segment, you have to do that yourself, otherwise
768   uninitialized variables do not have the value zero.
769
770   The <tt/#pragma/ understands the push and pop parameters as explained above.
771
772   Example:
773   <tscreen><verb>
774         #pragma bss-name ("MyBSS")
775   </verb></tscreen>
776
777
778 <sect1><tt>#pragma charmap (&lt;index&gt;, &lt;code&gt;)</tt><label id="pragma-charmap"><p>
779
780   Each literal string and each literal character in the source is translated
781   by use of a translation table. This translation table is preset when the
782   compiler is started depending on the target system, for example to map
783   ISO-8859-1 characters into PETSCII if the target is a commodore machine.
784
785   This pragma allows to change entries in the translation table, so the
786   translation for individual characters, or even the complete table may be
787   adjusted.
788
789   Both arguments are assumed to be unsigned characters with a valid range of
790   1-255.
791
792   Beware of two pitfalls:
793
794     <itemize>
795     <item>The character index is actually the code of the character in the
796           C source, so character mappings do always depend on the source
797           character set. This means that <tt/#pragma&nbsp;charmap/ is not
798           portable -- it depends on the build environment.
799     <item>While it is possible to use character literals as indices, the
800           result may be somewhat unexpected, since character literals are
801           itself translated. For this reason I would suggest to avoid
802           character literals and use numeric character codes instead.
803     </itemize>
804
805   Example:
806   <tscreen><verb>
807         /* Use a space wherever an 'a' occurs in ISO-8859-1 source */
808         #pragma charmap (0x61, 0x20);
809   </verb></tscreen>
810
811
812 <sect1><tt>#pragma check-stack ([push,] on|off)</tt><label id="pragma-check-stack"><p>
813
814   Tells the compiler to insert calls to a stack checking subroutine to detect
815   stack overflows. The stack checking code will lead to somewhat larger and
816   slower programs, so you may want to use this pragma when debugging your
817   program and switch it off for the release version. If a stack overflow is
818   detected, the program is aborted.
819
820   If the argument is "off", stack checks are disabled (the default), otherwise
821   they're enabled.
822
823   The <tt/#pragma/ understands the push and pop parameters as explained above.
824
825 <sect1><tt>#pragma code-name ([push,] &lt;name&gt;)</tt><label id="pragma-code-name"><p>
826
827   This pragma changes the name used for the CODE segment (the CODE segment
828   is used to store executable code). The argument is a string enclosed in
829   double quotes.
830
831   Note: The default linker configuration file does only map the standard
832   segments. If you use other segments, you have to create a new linker
833   configuration file.
834
835   The <tt/#pragma/ understands the push and pop parameters as explained above.
836
837   Example:
838   <tscreen><verb>
839         #pragma code-name ("MyCODE")
840   </verb></tscreen>
841
842
843 <sect1><tt>#pragma codesize ([push,] &lt;int&gt;)</tt><label id="pragma-codesize"><p>
844
845   This pragma allows finer control about speed vs. size decisions in the code
846   generation and optimization phase. It gives the allowed size increase factor
847   (in percent). The default is can be changed by use of the <tt/<ref
848   id="option-codesize" name="--codesize">/ compiler option.
849
850   The <tt/#pragma/ understands the push and pop parameters as explained above.
851
852
853 <sect1><tt>#pragma data-name ([push,] &lt;name&gt;)</tt><label id="pragma-data-name"><p>
854
855   This pragma changes the name used for the DATA segment (the DATA segment
856   is used to store initialized data). The argument is a string enclosed in
857   double quotes.
858
859   Note: The default linker configuration file does only map the standard
860   segments. If you use other segments, you have to create a new linker
861   configuration file.
862
863   The <tt/#pragma/ understands the push and pop parameters as explained above.
864
865   Example:
866   <tscreen><verb>
867         #pragma data-name ("MyDATA")
868   </verb></tscreen>
869
870
871 <sect1><tt>#pragma optimize ([push,] on|off)</tt><label id="pragma-optimize"><p>
872
873   Switch optimization on or off. If the argument is "off", optimization is
874   disabled, otherwise it is enabled. Please note that this pragma only effects
875   whole functions. The setting in effect when the function is encountered will
876   determine if the generated code is optimized or not.
877
878   Optimization and code generation is also controlled by the <ref
879   id="pragma-codesize" name="codesize pragma">.
880
881   The default is "off", but may be changed with the <tt/<ref name="-O"
882   id="option-O">/ compiler option.
883
884   The <tt/#pragma/ understands the push and pop parameters as explained above.
885
886
887 <sect1><tt>#pragma rodata-name ([push,] &lt;name&gt;)</tt><label id="pragma-rodata-name"><p>
888
889   This pragma changes the name used for the RODATA segment (the RODATA
890   segment is used to store readonly data). The argument is a string
891   enclosed in double quotes.
892
893   Note: The default linker configuration file does only map the standard
894   segments. If you use other segments, you have to create a new linker
895   configuration file.
896
897   The <tt/#pragma/ understands the push and pop parameters as explained above.
898
899   Example:
900   <tscreen><verb>
901         #pragma rodata-name ("MyRODATA")
902   </verb></tscreen>
903
904
905 <sect1><tt>#pragma regvaraddr ([push,] on|off)</tt><label id="pragma-regvaraddr"><p>
906
907   The compiler does not allow to take the address of register variables.
908   The regvaraddr pragma changes this. Taking the address of a register
909   variable is allowed after using this pragma with "on" as argument.
910   Using "off" as an argument switches back to the default behaviour.
911
912   Beware: The C standard does not allow taking the address of a variable
913   declared as register. So your programs become non-portable if you use
914   this pragma. In addition, your program may not work. This is usually the
915   case if a subroutine is called with the address of a register variable,
916   and this subroutine (or a subroutine called from there) uses
917   register variables. So be careful with this #pragma.
918
919   The <tt/#pragma/ understands the push and pop parameters as explained above.
920
921   Example:
922   <tscreen><verb>
923         #pragma regvaraddr(on)  /* Allow taking the address
924                                  * of register variables
925                                  */
926   </verb></tscreen>
927
928
929 <sect1><tt>#pragma register-vars ([push,] on|off)</tt><label id="pragma-register-vars"><p>
930
931   Enables or disables use of register variables. If register variables are
932   disabled (the default), the <tt/register/ keyword is ignored. Register
933   variables are explained in more detail in <ref id="register-vars" name="a separate
934   chapter">.
935
936   The <tt/#pragma/ understands the push and pop parameters as explained above.
937
938
939 <sect1><tt>#pragma signed-chars ([push,] on|off)</tt><label id="pragma-signed-chars"><p>
940
941   Changes the signedness of the default character type. If the argument is
942   "on", default characters are signed, otherwise characters are unsigned. The
943   compiler default is to make characters unsigned since this creates a lot
944   better code. This default may be overridden by the <tt/<ref
945   name="--signed-chars" id="option-signed-chars">/ command line option.
946
947   The <tt/#pragma/ understands the push and pop parameters as explained above.
948
949
950 <sect1><tt>#pragma static-locals ([push,] on|off)</tt><label id="pragma-static-locals"<p>
951
952   Use variables in the bss segment instead of variables on the stack. This
953   pragma changes the default set by the compiler option <tt/<ref
954   name="--static-locals" id="option-static-locals">/. If the argument is "on",
955   local variables are allocated in the BSS segment, leading to shorter and in
956   most cases faster, but non-reentrant code.
957
958   The <tt/#pragma/ understands the push and pop parameters as explained above.
959
960
961 <sect1><tt>#pragma warn (name, [push,] on|off)</tt><label id="pragma-warn"><p>
962
963   Switch compiler warnings on or off. "name" is the name of a warning (see the
964   <tt/<ref name="-W" id="option-W">/ compiler option for a list). The name is
965   either followed by "pop", which restores the last pushed state, or by "on" or
966   "off", optionally preceeded by "push" to push the current state before
967   changing it.
968
969   Example:
970   <tscreen><verb>
971         /* Don't warn about the unused parameter in function func */
972         #pragma warn (unused-param, push, off)
973         static int func (int unused)
974         {
975             return 0;
976         }
977         #pragma warn (unused-param, pop)
978   </verb></tscreen>
979
980 <sect1><tt>#pragma writable-strings ([push,] on|off)</tt><label id="pragma-writable-strings"><p>
981
982   Changes the storage location of string literals. For historical reasons,
983   the C standard defines that string literals are of type "char[]", but
984   writing to such a literal causes undefined behaviour. Most compilers
985   (including cc65) place string literals in the read-only data segment, which
986   may cause problems with old C code that writes to string literals.
987
988   Using this pragma (or the corresponding command line option <tt/<ref
989   name="--writable-strings" id="option-writable-strings">/) causes the
990   literals to be placed in the data segment so they can be written to without
991   worry.
992
993   The <tt/#pragma/ understands the push and pop parameters as explained above.
994
995
996 <sect1><tt>#pragma zpsym (&lt;name&gt;)</tt><p>
997
998   Tell the compiler that the -- previously as external declared -- symbol with
999   the given name is a zero page symbol (usually from an assembler file).
1000   The compiler will create a matching import declaration for the assembler.
1001
1002   Example:
1003   <tscreen><verb>
1004         extern int foo;
1005         #pragma zpsym ("foo");  /* foo is in the zeropage */
1006   </verb></tscreen>
1007
1008
1009
1010
1011 <sect>Register variables<label id="register-vars"><p>
1012
1013 The runtime for all supported platforms has 6 bytes of zero page space
1014 available for register variables (this could be increased, but I think it's a
1015 good value). So you can declare register variables up to a total size of 6 per
1016 function. The compiler will allocate register space on a "first come, first
1017 served" base and convert any <tt/register/ declarations that exceed the
1018 available register space silently to <tt/auto/. Parameters can also be
1019 declared as <tt/register/, this will in fact give slightly shorter code than
1020 using a register variable.
1021
1022 Since a function must save the current values of the registers on entry and
1023 restore them on exit, there is an overhead associated with register variables,
1024 and this overhead is quite high (about 20 bytes per variable). This means that
1025 just declaring anything as <tt/register/ is not a good idea.
1026
1027 The best use for register variables are pointers, especially those that point
1028 to structures. The magic number here is about 3 uses of a struct field: If the
1029 function contains this number or even more, the generated code will be usually
1030 shorter and faster when using a register variable for the struct pointer. The
1031 reason for this is that the register variable can in many cases be used as a
1032 pointer directly. Having a pointer in an auto variable means that this pointer
1033 must first be copied into a zero page location, before it can be dereferenced.
1034
1035 Second best use for register variables are counters. However, there is not
1036 much difference in the code generated for counters, so you will need at least
1037 100 operations on this variable (for example in a loop) to make it worth the
1038 trouble. The only savings you get here are by the use of a zero page variable
1039 instead of one on the stack or in the data segment.
1040
1041 Register variables must be explicitly enabled, either by using <tt/<ref
1042 name="-Or" id="option-O">/ or <tt/<ref name="--register-vars"
1043 id="option-register-vars">/ on the command line or by use of <tt/<ref
1044 name="#pragma register-vars" id="pragma-register-vars">/. Register variables
1045 are only accepted on function top level, register variables declared in
1046 interior blocks are silently converted to <tt/auto/. With register variables
1047 disabled, all variables declared as <tt/register/ are actually auto variables.
1048
1049 Please take care when using register variables: While they are helpful and can
1050 lead to a tremendous speedup when used correctly, improper usage will cause
1051 bloated code and a slowdown.
1052
1053
1054
1055 <sect>Inline assembler<label id="inline-asm"><p>
1056
1057 The compiler allows to insert assembler statements into the output file. The
1058 syntax is
1059
1060 <tscreen><verb>
1061         asm (&lt;string literal&gt;[, optional parameters]) ;
1062 </verb></tscreen>
1063 or
1064 <tscreen><verb>
1065         __asm__ (&lt;string literal&gt;[, optional parameters]) ;
1066 </verb></tscreen>
1067 <p>
1068
1069 The first form is in the user namespace and is disabled by <tt><ref
1070 id="option--standard" name="--standard"></tt> if the argument is not <tt/cc65/.
1071
1072 The asm statement may be used inside a function and on global file level. An
1073 inline assembler statement is a primary expression, so it may also be used as
1074 part of an expression. Please note however that the result of an expression
1075 containing just an inline assembler statement is always of type <tt/void/.
1076
1077 The contents of the string literal are preparsed by the compiler and inserted
1078 into the generated assembly output, so that the can be further processed by
1079 the backend and especially the optimizer. For this reason, the compiler does
1080 only allow regular 6502 opcodes to be used with the inline assembler. Pseudo
1081 instructions (like <tt/.import/, <tt/.byte/ and so on) are <em/not/ allowed,
1082 even if the ca65 assembler (which is used to translate the generated assembler
1083 code) would accept them. The builtin inline assembler is not a replacement for
1084 the full blown macro assembler which comes with the compiler.
1085
1086 Note: Inline assembler statements are subject to all optimizations done by the
1087 compiler. There is currently no way to protect an inline assembler statement
1088 from being moved or removed completely by the optimizer. If in doubt, check
1089 the generated assembler output, or disable optimizations.
1090
1091 The string literal may contain format specifiers from the following list. For
1092 each format specifier, an argument is expected which is inserted instead of
1093 the format specifier before passing the assembly code line to the backend.
1094
1095 <itemize>
1096   <item><tt/%b/ - Numerical 8-bit value
1097   <item><tt/%w/ - Numerical 16-bit value
1098   <item><tt/%l/ - Numerical 32-bit value
1099   <item><tt/%v/ - Assembler name of a (global) variable or function
1100   <item><tt/%o/ - Stack offset of a (local) variable
1101   <item><tt/%g/ - Assembler name of a C label
1102   <item><tt/%s/ - The argument is converted to a string
1103   <item><tt/%%/ - The % sign itself
1104 </itemize><p>
1105
1106 Using these format specifiers, you can access C <tt/#defines/, variables or
1107 similar stuff from the inline assembler. For example, to load the value of
1108 a C <tt/#define/ into the Y register, one would use
1109
1110 <tscreen><verb>
1111         #define OFFS  23
1112         __asm__ ("ldy #%b", OFFS);
1113 </verb></tscreen>
1114
1115 Or, to access a struct member of a static variable:
1116
1117 <tscreen><verb>
1118         typedef struct {
1119             unsigned char x;
1120             unsigned char y;
1121             unsigned char color;
1122         } pixel_t;
1123         static pixel_t pixel;
1124         __asm__ ("ldy #%b", offsetof(pixel_t, color));
1125         __asm__ ("lda %v,y", pixel);
1126 </verb></tscreen>
1127 <p>
1128
1129 Note: Do not embed the assembler labels that are used as names of global
1130 variables or functions into your asm statements. Code like this
1131
1132 <tscreen><verb>
1133         int foo;
1134         int bar () { return 1; }
1135         __asm__ ("lda _foo");           /* DON'T DO THAT! */
1136         ...
1137         __asm__ ("jsr _bar");           /* DON'T DO THAT EITHER! */
1138 </verb></tscreen>
1139 <p>
1140
1141 may stop working if the way, the compiler generates these names is changed in
1142 a future version. Instead use the format specifiers from the table above:
1143
1144 <tscreen><verb>
1145         __asm__ ("lda %v", foo);        /* OK */
1146         ...
1147         __asm__ ("jsr %v", bar);        /* OK */
1148 </verb></tscreen>
1149 <p>
1150
1151
1152 <sect>Implementation-defined behavior<p>
1153
1154 This section describes the behavior of cc65 when the standard describes the
1155 behavior as implementation-defined.
1156
1157 (to be done)
1158
1159 <sect>Bugs/Feedback<p>
1160
1161 If you have problems using the compiler, if you find any bugs, or if you're
1162 doing something interesting with it, I would be glad to hear from you. Feel
1163 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
1164 name="uz@cc65.org">).
1165
1166
1167
1168 <sect>Copyright<p>
1169
1170 This is the original compiler copyright:
1171
1172 <tscreen><verb>
1173 --------------------------------------------------------------------------
1174   -*- Mode: Text -*-
1175
1176      This is the copyright notice for RA65, LINK65, LIBR65, and other
1177   Atari 8-bit programs.  Said programs are Copyright 1989, by John R.
1178   Dunning.  All rights reserved, with the following exceptions:
1179
1180       Anyone may copy or redistribute these programs, provided that:
1181
1182   1:  You don't charge anything for the copy.  It is permissable to
1183       charge a nominal fee for media, etc.
1184
1185   2:  All source code and documentation for the programs is made
1186       available as part of the distribution.
1187
1188   3:  This copyright notice is preserved verbatim, and included in
1189       the distribution.
1190
1191       You are allowed to modify these programs, and redistribute the
1192   modified versions, provided that the modifications are clearly noted.
1193
1194       There is NO WARRANTY with this software, it comes as is, and is
1195   distributed in the hope that it may be useful.
1196
1197       This copyright notice applies to any program which contains
1198   this text, or the refers to this file.
1199
1200       This copyright notice is based on the one published by the Free
1201   Software Foundation, sometimes known as the GNU project.  The idea
1202   is the same as theirs, ie the software is free, and is intended to
1203   stay that way.  Everybody has the right to copy, modify, and re-
1204   distribute this software.  Nobody has the right to prevent anyone
1205   else from copying, modifying or redistributing it.
1206
1207 --------------------------------------------------------------------------
1208 </verb></tscreen>
1209
1210 Small parts of the compiler (parts of the preprocessor and main parser) are
1211 still covered by this copyright. The main portion is covered by the usual
1212 cc65 license, which reads:
1213
1214 This software is provided 'as-is', without any expressed or implied
1215 warranty.  In no event will the authors be held liable for any damages
1216 arising from the use of this software.
1217
1218 Permission is granted to anyone to use this software for any purpose,
1219 including commercial applications, and to alter it and redistribute it
1220 freely, subject to the following restrictions:
1221
1222 <enum>
1223 <item>  The origin of this software must not be misrepresented; you must not
1224         claim that you wrote the original software. If you use this software
1225         in a product, an acknowledgment in the product documentation would be
1226         appreciated but is not required.
1227 <item>  Altered source versions must be plainly marked as such, and must not
1228         be misrepresented as being the original software.
1229 <item>  This notice may not be removed or altered from any source
1230         distribution.
1231 </enum>
1232
1233 </article>
1234