]> git.sur5r.net Git - cc65/blob - doc/cc65.sgml
42b2b0be38c130f0858249d3134fa106eacbe2ff
[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
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   -d                    Debug mode
58   -g                    Add debug info to object file
59   -h                    Help (this text)
60   -j                    Default characters are signed
61   -o name               Name the output file
62   -t sys                Set the target system
63   -v                    Increase verbosity
64   -A                    Strict ANSI mode
65   -Cl                   Make local variables static
66   -Dsym[=defn]          Define a symbol
67   -I dir                Set an include directory search path
68   -O                    Optimize code
69   -Oi                   Optimize code, inline more code
70   -Or                   Enable register variables
71   -Os                   Inline some known functions
72   -T                    Include source as comment
73   -V                    Print the compiler version number
74   -W                    Suppress warnings
75
76 Long options:
77   --ansi                Strict ANSI mode
78   --cpu type            Set cpu type
79   --debug               Debug mode
80   --debug-info          Add debug info to object file
81   --help                Help (this text)
82   --include-dir dir     Set an include directory search path
83   --signed-chars        Default characters are signed
84   --static-locals       Make local variables static
85   --target sys          Set the target system
86   --verbose             Increase verbosity
87   --version             Print the compiler version number
88 ---------------------------------------------------------------------------
89 </verb></tscreen>
90
91
92 <sect1>Command line options in detail<p>
93
94 Here is a description of all the command line options:
95
96 <descrip>
97
98   <label id="option-A">
99   <tag><tt>-A, --ansi</tt></tag>
100
101   This option disables any compiler exensions. Have a look at section 5
102   for a discussion of compiler extensions. In addition, the macro
103   <tt/__STRICT_ANSI__/ is defined, when using one of these options.
104
105
106   <tag><tt>--cpu CPU</tt></tag>
107
108   A new, still experimental option. You may specify "6502" or "65C02" as
109   the CPU. 6502 is the default, so this will not change anything.
110   Specifying 65C02 will use a few 65C02 instructions when generating code.
111   Don't expect too much from this option: It is still new (and may have
112   bugs), and the additional instructions for the 65C02 are not that
113   overwhelming.
114
115
116   <tag><tt>-d, --debug</tt></tag>
117
118   Enables debug mode, something that should not be needed for mere
119   mortals:-)
120
121
122   <tag><tt>-D sym[=definition]</tt></tag>
123
124   Define a macro on the command line. If no definition is given, the macro
125   is defined to the value "1".
126
127
128   <tag><tt>-g, --debug-info</tt></tag>
129
130   This will cause the compiler to insert a <tt/.DEBUGINFO/ command into the
131   generated assembler code. This will cause the assembler to include all
132   symbols in a special section in the object file.
133
134
135   <tag><tt>-h, --help</tt></tag>
136
137   Print the short option summary shown above.
138
139
140   <tag><tt>-j, --signed-chars</tt></tag>
141
142   Using this option, you can make the default characters signed. Since the
143   6502 has no provisions for sign extending characters (which is needed on
144   almost any load operation), this will make the code larger and slower. A
145   better way is to declare characters explicitly as "signed" if needed. You
146   can also use <tt><ref id="pragma-signedchars" name="#pragma
147   signedchars"></tt> for better control of this option.
148
149
150   <tag><tt>-t target, --target target</tt></tag>
151
152   This option is used to set the target system. The target system
153   determines things like the character set that is used for strings and
154   character constants. The following target systems are supported:
155
156   <itemize>
157   <item>none
158   <item>atari
159   <item>c64
160   <item>c128
161   <item>plus4
162   <item>cbm610 (all CBM series-II computers with 80 column video)
163   <item>pet (all CBM PET systems except the 2001)
164   <item>apple2
165   <item>geos
166   </itemize>
167
168   <tag><tt>-v, --verbose</tt></tag>
169
170   Using this option, the compiler will be somewhat more verbose if errors
171   or warnings are encountered.
172
173
174   <tag><tt>-Cl, --static-locals</tt></tag>
175
176   Use static storage for local variables instead of storage on the stack.
177   Since the stack is emulated in software, this gives shorter and usually
178   faster code, but the code is no longer reentrant. The difference between
179   <tt/-Cl/ and declaring local variables as static yourself is, that
180   initializer code is executed each time, the function is entered. So when
181   using
182
183   <tscreen><verb>
184         void f (void)
185         {
186             unsigned a = 1;
187             ...
188         }
189   </verb></tscreen>
190
191   the variable a will always have the value 1 when entering the function
192   and using <tt/-Cl/, while in
193
194   <tscreen><verb>
195         void f (void)
196         {
197             static unsigned a = 1;
198             ....
199         }
200   </verb></tscreen>
201
202   the variable a will have the value 1 only the first time, the function
203   is entered, and will keep the old value from one call of the function to
204   the next.
205
206   You may also use <tt><ref id="pragma-staticlocals" name="#pragma
207   staticlocals"></tt> to change this setting in your sources.
208
209
210   <tag><tt>-I dir, --include-dir dir</tt></tag>
211
212   Set a directory where the compiler searches for include files. You may
213   use this option multiple times to add more than one directory to the
214   search list.
215
216
217   <tag><tt>-o name</tt></tag>
218
219   Specify the name of the output file. If you don't specify a name, the
220   name of the C input file is used, with the extension replaced by ".s".
221
222
223   <tag><tt>-O, -Oi, -Or, -Os</tt></tag>
224
225   Enable an optimizer run over the produced code.
226
227   Using <tt/-Oi/, the code generator will inline some code where otherwise a
228   runtime functions would have been called, even if the generated code is
229   larger. This will not only remove the overhead for a function call, but will
230   make the code visible for the optimizer.
231
232   <tt/-Or/ will make the compiler honor the <tt/register/ keyword. Local
233   variables may be placed in registers (which are actually zero page
234   locations). There is some overhead involved with register variables, since
235   the old contents of the registers must be saved and restored. In addition,
236   the current implementation does not make good use of register variables, so
237   using <tt/-Or/ may make your program even slower and larger. Use with care!
238
239   Using <tt/-Os/ will force the compiler to inline some known functions from
240   the C library like strlen. Note: This has two consequences:
241   <p>
242   <itemize>
243   <item>You may not use names of standard C functions in your own code. If you
244         do that, your program is not standard compliant anyway, but using
245         <tt/-Os/ will actually break things.
246         <p>
247   <item>The inlined string and memory functions will not handle strings or
248         memory areas larger than 255 bytes. Similar, the inlined <tt/is..()/
249         functions will not work with values outside char range.
250         <p>
251   </itemize>
252   <p>
253   It is possible to concatenate the modifiers for <tt/-O/. For example, to
254   enable register variables and inlining of known functions, you may use
255   <tt/-Ors/.
256
257
258   <tag><tt>-T</tt></tag>
259
260   This include the source code as comments in the generated code. This is
261   normally not needed.
262
263
264   <tag><tt>-V, --version</tt></tag>
265
266   Print the version number of the compiler. When submitting a bug report,
267   please include the operating system you're using, and the compiler
268   version.
269
270
271   <tag><tt>-W</tt></tag>
272
273   This option will suppress any warnings generated by the compiler. Since
274   any source file may be written in a manner that it will not produce
275   compiler warnings, using this option is usually not a good idea.
276
277 </descrip><p>
278
279
280 <sect>Input and output<p>
281
282 The compiler will accept one C file per invocation and create a file with
283 the same base name, but with the extension replaced by ".s". The output
284 file contains assembler code suitable for the use with the ca65 macro
285 assembler.
286
287 In addition to the paths named in the <tt/-I/ option on the command line, the
288 directory named in the environment variable <tt/CC65_INC/ is added to the
289 search path for include files on startup.
290
291
292
293 <sect>Differences to the ISO standard<p>
294
295 Here is a list of differences between the language, the compiler accepts,
296 and the one defined by the ISO standard:
297
298 <itemize>
299
300 <item>  The compiler allows single line comments that start with //. This
301         feature is disabled in strict ANSI mode.
302         <p>
303 <item>  The compiler allows unnamed parameters in parameter lists. The
304         compiler will not issue warnings about unused parameters that don't
305         have a name. This feature is disabled in strict ANSI mode.
306         <p>
307 <item>  The compiler has some additional keywords:
308         <p>
309         <itemize>
310         <item><tt/asm/
311         <item><tt/__asm__/
312         <item><tt/fastcall/
313         <item><tt/__fastcall__/
314         <item><tt/__AX__/
315         <item><tt/__EAX__/
316         <item><tt/__func__/
317         <item><tt/__attribute__/
318         </itemize>
319         <p>
320         The keywords without the underlines are disabled in strict ANSI mode.
321         <p>
322 <item>  The datatypes "float" and "double" are not available.
323         <p>
324 <item>  The compiler does not support bit fields.
325         <p>
326 <item>  Initialization of local variables is only possible for scalar data
327         types (that is, not for arrays and structs).
328         <p>
329 <item>  Because of the "wrong" order of the parameters on the stack, there is
330         an additional macro needed to access parameters in a variable
331         parameter list in a C function.
332         <p>
333 <item>  Functions may not return structs. However, struct assignment *is*
334         possible.
335         <p>
336 <item>  Part of the C library is available only with fastcall calling
337         conventions (see below). This means, that you may not mix pointers to
338         those functions with pointers to user written functions.
339         <p>
340 </itemize>
341
342 There may be some more minor differences, I'm currently not aware off. The
343 biggest problem is the missing float data type. With this limitation in
344 mind, you should be able to write fairly portable code.
345
346
347
348 <sect>Extensions<p>
349
350 This cc65 version has some extensions to the ISO C standard.
351
352 <itemize>
353
354 <item>  The compiler allows // comments (like in C++ and in the proposed C9x
355         standard). This feature is disabled by <tt><ref id="option-A"
356         name="-A"></tt>.
357         <p>
358
359 <item>  The compiler allows to insert assembler statements into the output
360         file. The syntax is
361
362         <tscreen><verb>
363         asm (&lt;string literal&gt;) ;
364         </verb></tscreen>
365         or
366         <tscreen><verb>
367         __asm__ (&lt;string literal&gt;) ;
368         </verb></tscreen>
369
370         The first form is in the user namespace and is disabled if the <tt/-A/
371         switch is given.
372
373         The given string is inserted literally into the output file, and a
374         newline is appended. The statements in this string are not checked by
375         the compiler, so be careful!
376
377         The asm statement may be used inside a function and on global file
378         level.
379         <p>
380
381 <item>  There is a special calling convention named "fastcall". This calling
382         convention is currently only usable for functions written in
383         assembler. The syntax for a function declaration using fastcall is
384
385         <tscreen><verb>
386         <tt/&lt;return type&gt; fastcall &lt;function name&gt; (&lt;parameter list&gt;)/
387         </verb></tscreen>
388         or
389         <tscreen><verb>
390         <tt/&lt;return type&gt; __fastcall__ &lt;function name&gt; (&lt;parameter list&gt;)/
391         </verb></tscreen>
392         An example would be
393         <tscreen><verb>
394         <tt/void __fastcall__ f (unsigned char c)/
395         </verb></tscreen>
396         The first form of the fastcall keyword is in the user namespace and is
397         therefore disabled in strict ANSI mode.
398
399         For functions declared as <tt/fastcall/, the rightmost parameter is not
400         pushed on the stack but left in the primary register when the function
401         is called. This will reduce the cost when calling assembler functions
402         significantly, especially when the function itself is rather small.
403         <p>
404
405 <item>  There are two pseudo variables named <tt/__AX__/ and <tt/__EAX__/.
406         Both refer to the primary register that is used by the compiler to
407         evaluate expressions or return function results. <tt/__AX__/ is of
408         type <tt/unsigned int/ and <tt/__EAX__/ of type <tt/long unsigned int/
409         respectively. The pseudo variables may be used as lvalue and rvalue as
410         every other variable. They are most useful together with short
411         sequences of assembler code. For example, the macro
412
413         <tscreen><verb>
414         #define hi(x) (__AX__=(x),asm("\ttxa\n\tldx\t#$00",__AX__)
415         </verb></tscreen>
416
417         will give the high byte of any unsigned value.
418         <p>
419
420 <item>  Inside a function, the identifier <tt/__func__/ gives the name of the
421         current function as a string. Outside of functions, <tt/__func__/ is
422         undefined.
423         Example:
424
425         <tscreen><verb>
426         #define PRINT_DEBUG(s)  printf ("%s: %s\n", __func__, s);
427         </verb></tscreen>
428
429         The macro will print the name of the current function plus a given
430         string.
431         <p>
432 </itemize>
433 <p>
434
435
436 <sect>Predefined macros<p>
437
438 The compiler defines several macros at startup:
439
440 <descrip>
441
442   <tag><tt>__CC65__</tt></tag>
443
444   This macro is always defined. Its value is the version number of the
445   compiler in hex. Version 2.0.1 of the compiler will have this macro defined
446   as 0x0201.
447
448   <tag><tt>__CBM__</tt></tag>
449
450   This macro is defined if the target system is one of the CBM targets.
451
452   <tag><tt>__C64__</tt></tag>
453
454   This macro is defined if the target is the c64 (-t c64).
455
456   <tag><tt>__C128__</tt></tag>
457
458   This macro is defined if the target is the c128 (-t c128).
459
460   <tag><tt>__PLUS4__</tt></tag>
461
462   This macro is defined if the target is the plus/4 (-t plus4).
463
464   <tag><tt>__CBM610__</tt></tag>
465
466   This macro is defined if the target is one of the CBM 600/700 family of
467   computers (called B series in the US).
468
469   <tag><tt>__PET__</tt></tag>
470
471   This macro is defined if the target is the PET family of computers (-t pet).
472
473   <tag><tt>__ATARI__</tt></tag>
474
475   This macro is defined if the target is one of the Atari computers
476   (400/800/130XL/800XL).
477
478   <tag><tt>__APPLE2__</tt></tag>
479
480   This macro is defined if the target is the Apple ][ (-t apple2).
481
482   <tag><tt>__GEOS__</tt></tag>
483
484   This macro is defined if you are compiling for the GEOS system (-t geos).
485
486   <tag><tt>__FILE__</tt></tag>
487
488   This macro expands to a string containing the name of the C source file.
489
490   <tag><tt>__LINE__</tt></tag>
491
492   This macro expands to the current line number.
493
494   <tag><tt>__STRICT_ANSI__</tt></tag>
495
496   This macro is defined to 1 if the <tt/-A/ compiler option was given, and
497   undefined otherwise.
498
499   <tag><tt>__OPT__</tt></tag>
500
501   Is defined if the compiler was called with the <tt/-O/ command line option.
502
503   <tag><tt>__OPT_i__</tt></tag>
504
505   Is defined if the compiler was called with the <tt/-Oi/ command line option.
506
507   <tag><tt>__OPT_r__</tt></tag>
508
509   Is defined if the compiler was called with the <tt/-Or/ command line option.
510
511   <tag><tt>__OPT_s__</tt></tag>
512
513   Is defined if the compiler was called with the <tt/-Os/ command line option.
514
515 </descrip>
516
517
518 <sect>#pragmas<label id="pragmas"><p>
519
520 The compiler understands some pragmas that may be used to change code
521 generation and other stuff.
522
523
524 <sect1><tt>#pragma bssseg (&lt;name&gt;)</tt><p>
525
526   This pragma changes the name used for the BSS segment (the BSS segment
527   is used to store uninitialized data). The argument is a string enclosed
528   in double quotes.
529
530   Note: The default linker configuration file does only map the standard
531   segments. If you use other segments, you have to create a new linker
532   configuration file.
533
534   Beware: The startup code will zero only the default BSS segment. If you
535   use another BSS segment, you have to do that yourself, otherwise
536   uninitialized variables do not have the value zero.
537
538   Example:
539   <tscreen><verb>
540         #pragma bssseg ("MyBSS")
541   </verb></tscreen>
542
543
544 <sect1><tt>#pragma codeseg (&lt;name&gt;)</tt><p>
545
546   This pragma changes the name used for the CODE segment (the CODE segment
547   is used to store executable code). The argument is a string enclosed in
548   double quotes.
549
550   Note: The default linker configuration file does only map the standard
551   segments. If you use other segments, you have to create a new linker
552   configuration file.
553
554   Example:
555   <tscreen><verb>
556         #pragma bssseg ("MyCODE")
557   </verb></tscreen>
558
559
560 <sect1><tt>#pragma dataseg (&lt;name&gt;)</tt><p>
561
562   This pragma changes the name used for the DATA segment (the DATA segment
563   is used to store initialized data). The argument is a string enclosed in
564   double quotes.
565
566   Note: The default linker configuration file does only map the standard
567   segments. If you use other segments, you have to create a new linker
568   configuration file.
569
570   Example:
571   <tscreen><verb>
572         #pragma bssseg ("MyDATA")
573   </verb></tscreen>
574
575
576 <sect1><tt>#pragma rodataseg (&lt;name&gt;)</tt><p>
577
578   This pragma changes the name used for the RODATA segment (the RODATA
579   segment is used to store readonly data). The argument is a string
580   enclosed in double quotes.
581
582   Note: The default linker configuration file does only map the standard
583   segments. If you use other segments, you have to create a new linker
584   configuration file.
585
586   Example:
587   <tscreen><verb>
588         #pragma bssseg ("MyRODATA")
589   </verb></tscreen>
590
591
592 <sect1><tt>#pragma regvaraddr (&lt;const int&gt;)</tt><p>
593
594   The compiler does not allow to take the address of register variables.
595   The regvaraddr pragma changes this. Taking the address of a register
596   variable is allowed after using this pragma, if the argument is not
597   zero. Using an argument of zero changes back to the default behaviour.
598
599   Beware: The C standard does not allow taking the address of a variable
600   declared as register. So your programs become non-portable if you use
601   this pragma. In addition, your program may not work. This is usually the
602   case if a subroutine is called with the address of a register variable,
603   and this subroutine (or a subroutine called from there) uses itself
604   register variables. So be careful with this #pragma.
605
606   Example:
607   <tscreen><verb>
608         #pragma regvaraddr(1)   /* Allow taking the address
609                                  * of register variables
610                                  */
611   </verb></tscreen>
612
613
614 <sect1><tt>#pragma signedchars (&lt;const int&gt;)</tt><label
615 id="pragma-signedchars"><p>
616
617   Changed the signedness of the default character type. If the argument
618   is not zero, default characters are signed, otherwise characters are
619   unsigned. The compiler default is to make characters unsigned since this
620   creates a lot better code. This default may be overridden by the
621   <tt/--signed-chars/ command line option.
622
623
624 <sect1><tt>#pragma staticlocals (&lt;const int&gt;)</tt><label
625 id="pragma-staticlocals"<p>
626
627   Use variables in the bss segment instead of variables on the stack. This
628   pragma changes the default set by the compiler option <tt/-Cl/. If the
629   argument is not zero, local variables are allocated in the BSS segment,
630   leading to shorter and in most cases faster, but non-reentrant code.
631
632
633 <sect1><tt>#pragma zpsym (&lt;name&gt;)</tt><p>
634
635   Tell the compiler that the - previously as external declared - symbol with
636   the given name is a zero page symbol (usually from an assembler file).
637   The compiler will create a matching import declaration for the assembler.
638
639   Example:
640   <tscreen><verb>
641         extern int foo;
642         #pragma zpsym ("foo");  /* foo is in the zeropage */
643   </verb></tscreen>
644
645
646
647
648 <sect>Bugs/Feedback<p>
649
650 If you have problems using the compiler, if you find any bugs, or if you're
651 doing something interesting with it, I would be glad to hear from you. Feel
652 free to contact me by email (<htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">).
653
654
655
656 <sect>Copyright<p>
657
658 This is the original compiler copyright:
659
660 <tscreen><verb>
661 --------------------------------------------------------------------------
662   -*- Mode: Text -*-
663
664      This is the copyright notice for RA65, LINK65, LIBR65, and other
665   Atari 8-bit programs.  Said programs are Copyright 1989, by John R.
666   Dunning.  All rights reserved, with the following exceptions:
667
668       Anyone may copy or redistribute these programs, provided that:
669
670   1:  You don't charge anything for the copy.  It is permissable to
671       charge a nominal fee for media, etc.
672
673   2:  All source code and documentation for the programs is made
674       available as part of the distribution.
675
676   3:  This copyright notice is preserved verbatim, and included in
677       the distribution.
678
679       You are allowed to modify these programs, and redistribute the
680   modified versions, provided that the modifications are clearly noted.
681
682       There is NO WARRANTY with this software, it comes as is, and is
683   distributed in the hope that it may be useful.
684
685       This copyright notice applies to any program which contains
686   this text, or the refers to this file.
687
688       This copyright notice is based on the one published by the Free
689   Software Foundation, sometimes known as the GNU project.  The idea
690   is the same as theirs, ie the software is free, and is intended to
691   stay that way.  Everybody has the right to copy, modify, and re-
692   distribute this software.  Nobody has the right to prevent anyone
693   else from copying, modifying or redistributing it.
694
695 --------------------------------------------------------------------------
696 </verb></tscreen>
697
698 In acknowledgment of this copyright, I will place my own changes to the
699 compiler under the same copyright. Please note however, that the library
700 and all binutils are covered by another copyright, and that I'm planning
701 to do a complete rewrite of the compiler, after which the compiler
702 copyright will also change.
703
704 For the list of changes requested by this copyright see newvers.txt.
705
706
707 </article>
708