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