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