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