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