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