|
|
1.1.1.2 ! root 1: Info file internals, produced by Makeinfo, -*- Text -*- ! 2: from input file internals.texinfo. ! 3: 1.1 root 4: 5: 6: This file documents the internals of the GNU compiler. 7: 1.1.1.2 ! root 8: Copyright (C) 1988 Free Software Foundation, Inc. 1.1 root 9: 10: Permission is granted to make and distribute verbatim copies of 11: this manual provided the copyright notice and this permission notice 12: are preserved on all copies. 13: 14: Permission is granted to copy and distribute modified versions of this 15: manual under the conditions for verbatim copying, provided also that the 1.1.1.2 ! root 16: section entitled ``GNU CC General Public License'' is included exactly as 1.1 root 17: in the original, and provided that the entire resulting derived work is 18: distributed under the terms of a permission notice identical to this one. 19: 20: Permission is granted to copy and distribute translations of this manual 21: into another language, under the above conditions for modified versions, 1.1.1.2 ! root 22: except that the section entitled ``GNU CC General Public License'' and ! 23: this permission notice may be included in translations approved by the ! 24: Free Software Foundation instead of in the original English. 1.1 root 25: 26: 27: 1.1.1.2 ! root 28: 1.1 root 29: 30: 1.1.1.2 ! root 31: File: internals, Node: Top, Next: Copying, Up: (DIR) 1.1 root 32: 33: Introduction 34: ************ 35: 1.1.1.2 ! root 36: This manual documents how to run, install and port the GNU C compiler, as ! 37: well as its new features and incompatibilities, and how to report bugs. ! 38: 1.1 root 39: 40: * Menu: 41: 42: * Copying:: GNU CC General Public License says 43: how you can copy and share GNU CC. 1.1.1.2 ! root 44: * Contributors:: People who have contributed to GNU CC. ! 45: * Options:: Command options supported by `gcc'. 1.1 root 46: * Installation:: How to configure, compile and install GNU CC. 1.1.1.2 ! root 47: * Incompatibilities:: Incompatibilities of GNU CC. ! 48: * Extensions:: GNU extensions to the C language. ! 49: * Bugs:: How to report bugs (if you want to get them fixed). 1.1 root 50: * Portability:: Goals of GNU CC's portability features. 1.1.1.2 ! root 51: * Interface:: Function-call interface of GNU CC output. 1.1 root 52: * Passes:: Order of passes, what they do, and what each file is for. 53: * RTL:: The intermediate representation that most passes work on. 54: * Machine Desc:: How to write machine description instruction patterns. 55: * Machine Macros:: How to write the machine description C macros. 56: 1.1.1.2 ! root 57: 1.1 root 58: 1.1.1.2 ! root 59: File: internals, Node: Copying, Next: Contributors, Prev: Top, Up: Top 1.1 root 60: 61: GNU CC GENERAL PUBLIC LICENSE 62: ***************************** 63: 1.1.1.2 ! root 64: (Clarified 11 Feb 1988) 1.1 root 65: 1.1.1.2 ! root 66: The license agreements of most software companies keep you at the mercy ! 67: of those companies. By contrast, our general public license is intended to ! 68: give everyone the right to share GNU CC. To make sure that you get the ! 69: rights we want you to have, we need to make restrictions that forbid anyone ! 70: to deny you these rights or to ask you to surrender the rights. Hence this ! 71: license agreement. ! 72: ! 73: Specifically, we want to make sure that you have the right to give away ! 74: copies of GNU CC, that you receive source code or else can get it if you ! 75: want it, that you can change GNU CC or use pieces of it in new free ! 76: programs, and that you know you can do these things. 1.1 root 77: 1.1.1.2 ! root 78: To make sure that everyone has such rights, we have to forbid you to ! 79: deprive anyone else of these rights. For example, if you distribute copies ! 80: of GNU CC, you must give the recipients all the rights that you have. You ! 81: must make sure that they, too, receive or can get the source code. And you ! 82: must tell them their rights. ! 83: ! 84: Also, for our own protection, we must make certain that everyone finds ! 85: out that there is no warranty for GNU CC. If GNU CC is modified by someone ! 86: else and passed on, we want its recipients to know that what they have is ! 87: not what we distributed, so that any problems introduced by others will not ! 88: reflect on our reputation. ! 89: ! 90: Therefore we (Richard Stallman and the Free Software Foundation, Inc.) ! 91: make the following terms which say what you must do to be allowed to ! 92: distribute or change GNU CC. 1.1 root 93: 94: COPYING POLICIES 95: ================ 96: 1.1.1.2 ! root 97: 1. You may copy and distribute verbatim copies of GNU CC source code as you ! 98: receive it, in any medium, provided that you conspicuously and 1.1 root 99: appropriately publish on each copy a valid copyright notice 1.1.1.2 ! root 100: ``Copyright (C) 1988 Free Software Foundation, Inc.'' (or with ! 101: whatever year is appropriate); keep intact the notices on all files ! 102: that refer to this License Agreement and to the absence of any ! 103: warranty; and give any other recipients of the GNU CC program a copy ! 104: of this License Agreement along with the program. You may charge a ! 105: distribution fee for the physical act of transferring a copy. ! 106: ! 107: 2. You may modify your copy or copies of GNU CC or any portion of it, and ! 108: copy and distribute such modifications under the terms of Paragraph 1 ! 109: above, provided that you also do the following: ! 110: ! 111: * cause the modified files to carry prominent notices stating that ! 112: you changed the files and the date of any change; and ! 113: ! 114: * cause the whole of any work that you distribute or publish, that ! 115: in whole or in part contains or is a derivative of GNU CC or any ! 116: part thereof, to be licensed at no charge to all third parties on ! 117: terms identical to those contained in this License Agreement ! 118: (except that you may choose to grant more extensive warranty ! 119: protection to some or all third parties, at your option). ! 120: 1.1 root 121: * You may charge a distribution fee for the physical act of 122: transferring a copy, and you may at your option offer warranty 123: protection in exchange for a fee. 124: 1.1.1.2 ! root 125: Mere aggregation of another unrelated program with this program (or ! 126: its derivative) on a volume of a storage or distribution medium does ! 127: not bring the other program under the scope of these terms. ! 128: ! 129: 3. You may copy and distribute GNU CC (or a portion or derivative of it, ! 130: under Paragraph 2) in object code or executable form under the terms ! 131: of Paragraphs 1 and 2 above provided that you also do one of the ! 132: following: ! 133: ! 134: * accompany it with the complete corresponding machine-readable ! 135: source code, which must be distributed under the terms of ! 136: Paragraphs 1 and 2 above; or, ! 137: ! 138: * accompany it with a written offer, valid for at least three ! 139: years, to give any third party free (except for a nominal ! 140: shipping charge) a complete machine-readable copy of the ! 141: corresponding source code, to be distributed under the terms of ! 142: Paragraphs 1 and 2 above; or, ! 143: ! 144: * accompany it with the information you received as to where the ! 145: corresponding source code may be obtained. (This alternative is ! 146: allowed only for noncommercial distribution and only if you ! 147: received the program in object code or executable form alone.) ! 148: ! 149: For an executable file, complete source code means all the source code ! 150: for all modules it contains; but, as a special exception, it need not ! 151: include source code for modules which are standard libraries that ! 152: accompany the operating system on which the executable file runs. ! 153: ! 154: 4. You may not copy, sublicense, distribute or transfer GNU CC except as ! 155: expressly provided under this License Agreement. Any attempt ! 156: otherwise to copy, sublicense, distribute or transfer GNU CC is void ! 157: and your rights to use the program under this License agreement shall ! 158: be automatically terminated. However, parties who have received ! 159: computer software programs from you with this License Agreement will ! 160: not have their licenses terminated so long as such parties remain in ! 161: full compliance. ! 162: ! 163: 5. If you wish to incorporate parts of GNU CC into other free programs ! 164: whose distribution conditions are different, write to the Free ! 165: Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not ! 166: yet worked out a simple rule that can be stated here, but we will ! 167: often permit this. We will be guided by the two goals of preserving ! 168: the free status of all derivatives of our free software and of ! 169: promoting the sharing and reuse of software. ! 170: ! 171: Your comments and suggestions about our licensing policies and our software ! 172: are welcome! Please contact the Free Software Foundation, Inc., 675 Mass ! 173: Ave, Cambridge, MA 02139, or call (617) 876-3296. 1.1 root 174: 175: NO WARRANTY 176: =========== 177: 178: BECAUSE GNU CC IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY NO 1.1.1.2 ! root 179: WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT WHEN ! 180: OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD M. ! 181: STALLMAN AND/OR OTHER PARTIES PROVIDE GNU CC "AS IS" WITHOUT WARRANTY OF ! 182: ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE ! 183: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 184: THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GNU CC IS WITH YOU. ! 185: SHOULD GNU CC PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY ! 186: SERVICING, REPAIR OR CORRECTION. ! 187: ! 188: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M. STALLMAN, ! 189: THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY WHO MAY MODIFY ! 190: AND REDISTRIBUTE GNU CC AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, ! 191: INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR ! 192: CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE (INCLUDING ! 193: BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES ! 194: SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY ! 195: OTHER PROGRAMS) GNU CC, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF ! 196: SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. 1.1 root 197: 198: 1.1.1.2 ! root 199: File: internals, Node: Contributors, Next: Options, Prev: Copying, Up: Top ! 200: ! 201: Contributors to GNU CC ! 202: ********************** ! 203: ! 204: In addition to Richard Stallman, several people have written parts of GNU CC. ! 205: ! 206: * The idea of using RTL and some of the optimization ideas came from the ! 207: U. of Arizona Portable Optimizer, written by Jack Davidson and ! 208: Christopher Fraser. See ``Register Allocation and Exhaustive Peephole ! 209: Optimization'', Software Practice and Experience 14 (9), Sept. 1984, ! 210: 857-866. ! 211: ! 212: * Paul Rubin wrote most of the preprocessor. ! 213: ! 214: * Leonard Tower wrote parts of the parser, RTL generator, RTL ! 215: definitions, and of the Vax machine description. ! 216: ! 217: * Ted Lemon wrote parts of the RTL reader and printer. ! 218: ! 219: * Nobuyuki Hikichi of Software Research Associates, Tokyo, contributed ! 220: the support for the SONY NEWS machine. ! 221: ! 222: * Charles LaBrec contributed the support for the Integrated Solutions ! 223: 68020 system. 1.1 root 224: 1.1.1.2 ! root 225: * Michael Tiemann of MCC wrote the description of the National ! 226: Semiconductor 32000 series cpu, with some contributions from Jan Stein ! 227: of the Chalmers Computer Club. Tiemann also wrote the code for inline ! 228: function integration. 1.1 root 229: 1.1.1.2 ! root 230: * Michael Kashtan of SRI adapted GNU CC to the Vomit-Making System. ! 231: ! 232: * Alex Crain provided changes for the 3b1. ! 233: ! 234: * Chris Hanson and another person who should remind me of his name ! 235: assisted in making GNU CC work on HP-UX for the 9000 series 300. ! 236: ! 237: ! 238: File: internals, Node: Options, Next: Installation, Prev: Contributors, Up: Top ! 239: ! 240: GNU CC Command Options ! 241: ********************** ! 242: ! 243: The GNU C compiler uses a command syntax much like the Unix C compiler. ! 244: The `gcc' program accepts options and file names as operands. Multiple ! 245: single-letter options may *not* be grouped: `-dr' is very different from ! 246: `-d -r'. ! 247: ! 248: When you invoke GNU CC, it normally does preprocessing, compilation, ! 249: assembly and linking. File names which end in `.c' are taken as C source ! 250: to be preprocessed and compiled; compiler output files plus any input files ! 251: with names ending in `.s' are assembled; then the resulting object files, ! 252: plus any other input files, are linked together to produce an executable. ! 253: ! 254: Command options allow you to stop this process at an intermediate stage. ! 255: For example, the `-c' option says not to run the linker. Then the output ! 256: consists of object files output by the assembler. ! 257: ! 258: Other command options are passed on to one stage. Some options control the ! 259: preprocessor and others the compiler itself. Yet other options control the ! 260: assembler and linker; these are not documented here because the GNU ! 261: assembler and linker are not yet released. ! 262: ! 263: Here are the options to control the overall compilation process, including ! 264: those that say whether to link, whether to assemble, and so on. ! 265: ! 266: `-o FILE' ! 267: Place output in file FILE. This applies regardless to whatever sort ! 268: of output is being produced, whether it be an executable file, an ! 269: object file, an assembler file or preprocessed C code. ! 270: ! 271: If `-o' is not specified, the default is to put an excutable file in ! 272: `a.out', the object file `SOURCE.c' in `SOURCE.o', an assembler file ! 273: in `SOURCE.s', and preprocessed C on standard output. ! 274: ! 275: `-c' ! 276: Compile or assemble the source files, but do not link. Produce object ! 277: files with names made by replacing `.c' or `.s' with `.o' at the end ! 278: of the input file names. Do nothing at all for object files specified ! 279: as input. ! 280: ! 281: `-S' ! 282: Compile into assembler code but do not assemble. The assembler output ! 283: file name is made by replacing `.c' with `.s' at the end of the input ! 284: file name. Do nothing at all for assembler source files or object ! 285: files specified as input. ! 286: ! 287: `-E' ! 288: Run only the C preprocessor. Preprocess all the C source files ! 289: specified and output the results to standard output. ! 290: ! 291: `-v' ! 292: Compiler driver program prints the commands it executes as it runs the ! 293: preprocessor, compiler proper, assembler and linker. Some of these ! 294: are directed to print their own version numbers. ! 295: ! 296: `-BPREFIX' ! 297: Compiler driver program tries PREFIX as a prefix for each program it ! 298: tries to run. These programs are `cpp', `cc1', `as' and `ld'. ! 299: ! 300: For each subprogram to be run, the compiler driver first tries the ! 301: `-B' prefix, if any. If that name is not found, or if `-B' was not ! 302: specified, the driver tries two standard prefixes, which are ! 303: `/usr/lib/gcc-' and `/usr/local/lib/gcc-'. If neither of those ! 304: results in a file name that is found, the unmodified program name is ! 305: searched for using the directories specified in your `PATH' ! 306: environment variable. ! 307: ! 308: The run-time support file `gnulib' is also searched for using the `-B' ! 309: prefix, if needed. If it is not found there, the two standard ! 310: prefixes above are tried, and that is all. The file is left out of ! 311: the link if it is not found by those means. Most of the time, on most ! 312: machines, you can do without it. ! 313: ! 314: These options control the details of C compilation itself. ! 315: ! 316: `-ansi' ! 317: Support all ANSI standard C programs. ! 318: ! 319: This turns off certain features of GNU C that are incompatible with ! 320: ANSI C, such as the `asm', `inline' and `typeof' keywords, and ! 321: predefined macros such as `unix' and `vax' that identify the type of ! 322: system you are using. It also enables the undesirable and rarely used ! 323: ANSI trigraph feature. ! 324: ! 325: The `-ansi' option does not cause non-ANSI programs to be rejected ! 326: gratuitously. For that, `-pedantic' is required in addition to `-ansi'. ! 327: ! 328: The macro `__STRICT_ANSI__' is predefined when the `-ansi' option is ! 329: used. Some header files may notice this macro and refrain from ! 330: declaring certain functions or defining certain macros that the ANSI ! 331: standard doesn't call for; this is to avoid interfering with any ! 332: programs that might use these names for other things. ! 333: ! 334: `-traditional' ! 335: Attempt to support some aspects of traditional C compilers. ! 336: Specifically: ! 337: ! 338: * All `extern' declarations take effect globally even if they are ! 339: written inside of a function definition. This includes implicit ! 340: declarations of functions. ! 341: ! 342: * The keywords `typeof', `inline', `signed', `const' and `volatile' ! 343: are not recognized. ! 344: ! 345: * Comparisons between pointers and integers are always allowed. ! 346: ! 347: * Integer types `unsigned short' and `unsigned char' promote to ! 348: `unsigned int'. ! 349: ! 350: * In the preprocessor, comments convert to nothing at all, rather ! 351: than to a space. This allows traditional token concatenation. ! 352: ! 353: * In the preprocessor, single and double quote characters are ! 354: ignored when scanning macro definitions, so that macro arguments ! 355: can be replaced even within a string or character constant. ! 356: Quote characters are also ignored when skipping text inside a ! 357: failing conditional directive. ! 358: ! 359: `-O' ! 360: Optimize. Optimizing compilation takes somewhat more time, and a lot ! 361: more memory for a large function. ! 362: ! 363: Without `-O', the compiler's goal is to reduce the cost of compilation ! 364: and to make debugging produce the expected results. Statements are ! 365: independent: if you stop the program with a breakpoint between ! 366: statements, you can then assign a new value to any variable or change ! 367: the program counter to any other statement in the function and get ! 368: exactly the results you would expect from the source code. ! 369: ! 370: Without `-O', only variables declared `register' are allocated in ! 371: registers. The resulting compiled code is a little worse than ! 372: produced by PCC without `-O'. ! 373: ! 374: With `-O', the compiler tries to reduce code size and execution time. ! 375: ! 376: Some of the `-f' options described below turn specific kinds of ! 377: optimization on or off. ! 378: ! 379: `-g' 1.1 root 380: Produce debugging information in DBX format. 1.1.1.2 ! root 381: ! 382: Unlike most other C compilers, GNU CC allows you to use `-g' with ! 383: `-O'. The shortcuts taken by optimized code may occasionally produce ! 384: surprising results: some variables you declared may not exist at all; ! 385: flow of control may briefly move where you did not expect it; some ! 386: statements may not be executed because they compute constant results ! 387: or their values were already at hand; some statements may execute in ! 388: different places because they were moved out of loops. Nevertheless ! 389: it proves possible to debug optimized output. This makes it ! 390: reasonable to use the optimizer for programs that might have bugs. ! 391: ! 392: `-gg' ! 393: Produce debugging information in GDB's own format. This requires the ! 394: GNU assembler and linker in order to work. ! 395: ! 396: `-w' ! 397: Inhibit all warning messages. ! 398: ! 399: `-W' ! 400: Print extra warning messages for these events: ! 401: ! 402: * An automatic variable is used without first being initialized. ! 403: ! 404: These warnings are possible only in optimizing compilation, ! 405: because they require data flow information that is computed only ! 406: when optimizing. They occur only for variables that are ! 407: candidates for register allocation. Therefore, they do not occur ! 408: for a variable that is declared `volatile', or whose address is ! 409: taken, or whose size is other than 1, 2, 4 or 8 bytes. Also, ! 410: they do not occur for structures, unions or arrays, even when ! 411: they are in registers. ! 412: ! 413: Note that there may be no warning about a variable that is used ! 414: only to compute a value that itself is never used, because such ! 415: computations may be deleted by the flow analysis pass before the ! 416: warnings are printed. ! 417: ! 418: These warnings are made optional because GNU CC is not smart ! 419: enough to see all the reasons why the code might be correct ! 420: despite appearing to have an error. Here is one example of how ! 421: this can happen: ! 422: ! 423: { ! 424: int x; ! 425: switch (y) ! 426: { ! 427: case 1: x = 1; ! 428: break; ! 429: case 2: x = 4; ! 430: break; ! 431: case 3: x = 5; ! 432: } ! 433: foo (x); ! 434: } ! 435: ! 436: ! 437: If the value of `y' is always 1, 2 or 3, then `x' is always ! 438: initialized, but GNU CC doesn't know this. Here is another ! 439: common case: ! 440: ! 441: { ! 442: int save_y; ! 443: if (change_y) save_y = y, y = new_y; ! 444: ... ! 445: if (change_y) y = save_y; ! 446: } ! 447: ! 448: ! 449: This has no bug because `x' is used only if it is set. ! 450: ! 451: * A nonvolatile automatic variable might be changed by a call to ! 452: `longjmp'. These warnings as well are possible only in ! 453: optimizing compilation. ! 454: ! 455: The compiler sees only the calls to `setjmp'. It cannot know ! 456: where `longjmp' will be called; in fact, a signal handler could ! 457: call it at any point in the code. As a result, you may get a ! 458: warning even when there is in fact no problem because `longjmp' ! 459: cannot in fact be called at the place which would cause a problem. ! 460: ! 461: * A function can return either with or without a value. (Falling ! 462: off the end of the function body is considered returning without ! 463: a value.) For example, this function would inspire such a warning: ! 464: ! 465: foo (a) ! 466: { ! 467: if (a > 0) ! 468: return a; ! 469: } ! 470: ! 471: ! 472: Spurious warnings can occur because GNU CC does not realize that ! 473: certain functions (including `abort' and `longjmp') will never ! 474: return. ! 475: ! 476: In the future, other useful warnings may also be enabled by this option. ! 477: ! 478: `-Wimplicit' ! 479: Warn whenever a function is implicitly declared. ! 480: ! 481: `-Wreturn-type' ! 482: Warn whenever a function is defined with a return-type that defaults ! 483: to `int'. Also warn about any `return' statement with no return-value ! 484: in a function whose return-type is not `void'. ! 485: ! 486: `-Wcomment' ! 487: Warn whenever a comment-start sequence `/*' appears in a comment. ! 488: ! 489: `-p' ! 490: Generate extra code to write profile information suitable for the ! 491: analysis program `prof'. ! 492: ! 493: `-pg' ! 494: Generate extra code to write profile information suitable for the ! 495: analysis program `gprof'. ! 496: ! 497: `-nostdinc' ! 498: Don't search the standard directories for include files. Only the ! 499: directories you specify explicitly with the `-I' option will be ! 500: searched. ! 501: ! 502: `-nostdlib' ! 503: Don't use the standard system libraries and startup files when ! 504: linking. Only the files you specify (plus `gnulib') will be passed to ! 505: the linker. ! 506: ! 507: `-mMACHINESPEC' ! 508: Machine-dependent option specifying something about the type of target ! 509: machine. These options are defined by the macro `TARGET_SWITCHES' in ! 510: the machine description. The default for the options is also defined ! 511: by that macro, which enables you to change the defaults. ! 512: ! 513: These are the `-m' options defined in the 68000 machine description: ! 514: ! 515: `-m68020' ! 516: Generate output for a 68020 (rather than a 68000). This is the ! 517: default if you use the unmodified sources. ! 518: ! 519: `-m68000' ! 520: Generate output for a 68000 (rather than a 68020). ! 521: ! 522: `-m68881' ! 523: Generate output containing 68881 instructions for floating point. ! 524: This is the default if you use the unmodified sources. ! 525: ! 526: `-msoft-float' ! 527: Generate output containing library calls for floating point. ! 528: ! 529: `-mshort' ! 530: Consider type `int' to be 16 bits wide, like `short int'. ! 531: ! 532: `-mnobitfield' ! 533: Do not use the bit-field instructions. `-m68000' implies ! 534: `-mnobitfield'. ! 535: ! 536: `-mbitfield' ! 537: Do use the bit-field instructions. `-m68020' implies ! 538: `-mbitfield'. This is the default if you use the unmodified ! 539: sources. ! 540: ! 541: `-mrtd' ! 542: Use a different function-calling convention, in which functions ! 543: that take a fixed number of arguments return with the `rtd' ! 544: instruction, which pops their arguments while returning. This ! 545: saves one instruction in the caller since there is no need to pop ! 546: the arguments there. ! 547: ! 548: This calling convention is incompatible with the one normally ! 549: used on Unix, so you cannot use it if you need to call libraries ! 550: compiled with the Unix compiler. ! 551: ! 552: Also, you must provide function prototypes for all functions that ! 553: take variable numbers of arguments (including `printf'); ! 554: otherwise incorrect code will be generated for calls to those ! 555: functions. ! 556: ! 557: In addition, seriously incorrect code will result if you call a ! 558: function with too many arguments. (Normally, extra arguments are ! 559: harmlessly ignored.) ! 560: ! 561: The `rtd' instruction is supported by the 68010 and 68020 ! 562: processors, but not by the 68000. ! 563: ! 564: These `-m' options are defined in the Vax machine description: ! 565: ! 566: `-munix' ! 567: Do not output certain jump instructions (`aobleq' and so on) that ! 568: the Unix assembler for the Vax cannot handle across long ranges. ! 569: ! 570: `-mgnu' ! 571: Do output those jump instructions, on the assumption that you ! 572: will assemble with the GNU assembler. ! 573: ! 574: `-fFLAG' ! 575: Specify machine-independent flags. These are the flags: ! 576: ! 577: `-ffloat-store' ! 578: Do not store floating-point variables in registers. This ! 579: prevents undesirable excess precision on machines such as the ! 580: 68000 where the floating registers (of the 68881) keep more ! 581: precision than a `double' is supposed to have. ! 582: ! 583: For most programs, the excess precision does only good, but a few ! 584: programs rely on the precise definition of IEEE floating point. ! 585: Use `-ffloat-store' for such programs. ! 586: ! 587: `-fno-asm' ! 588: Do not recognize `asm', `inline' or `typeof' as a keyword. These ! 589: words may then be used as identifiers. ! 590: ! 591: `-fno-defer-pop' ! 592: Always pop the arguments to each function call as soon as that ! 593: function returns. Normally the compiler (when optimizing) lets ! 594: arguments accumulate on the stack for several function calls and ! 595: pops them all at once. ! 596: ! 597: `-fcombine-regs' ! 598: Allow the combine pass to combine an instruction that copies one ! 599: register into another. This might or might not produce better ! 600: code when used in addition to `-O'. I am interested in hearing ! 601: about the difference this makes. ! 602: ! 603: `-fforce-mem' ! 604: Force memory operands to be copied into registers before doing ! 605: arithmetic on them. This may produce better code by making all ! 606: memory references potential common subexpressions. When they are ! 607: not common subexpressions, instruction combination should ! 608: eliminate the separate register-load. I am interested in hearing ! 609: about the difference this makes. ! 610: ! 611: `-fforce-addr' ! 612: Force memory address constants to be copied into registers before ! 613: doing arithmetic on them. This may produce better code just as ! 614: `-fforce-mem' may. I am interested in hearing about the ! 615: difference this makes. ! 616: ! 617: `-fomit-frame-pointer' ! 618: Don't keep the frame pointer in a register for functions that ! 619: don't need one. This avoids the instructions to save, set up and ! 620: restore frame pointers; it also makes an extra register available ! 621: in many functions. *It also makes debugging impossible.* ! 622: ! 623: On some machines, such as the Vax, this flag has no effect, ! 624: because the standard calling sequence automatically handles the ! 625: frame pointer and nothing is saved by pretending it doesn't ! 626: exist. The machine-description macro `FRAME_POINTER_REQUIRED' ! 627: controls whether a target machine supports this flag. *note ! 628: Registers::. ! 629: ! 630: `-finline-functions' ! 631: Integrate all simple functions into their callers. The compiler ! 632: heuristically decides which functions are simple enough to be ! 633: worth integrating in this way. ! 634: ! 635: If all calls to a given function are integrated, and the function ! 636: is declared `static', then the function is normally not output as ! 637: assembler code in its own right. ! 638: ! 639: `-fkeep-inline-functions' ! 640: Even if all calls to a given function are integrated, and the ! 641: function is declared `static', nevertheless output a separate ! 642: run-time callable version of the function. ! 643: ! 644: `-fwritable-strings' ! 645: Store string constants in the writable data segment and don't ! 646: uniquize them. This is for compatibility with old programs which ! 647: assume they can write into string constants. Writing into string ! 648: constants is a very bad idea; ``constants'' should be constant. ! 649: ! 650: `-fno-function-cse' ! 651: Do not put function addresses in registers; make each instruction ! 652: that calls a constant function contain the function's address ! 653: explicitly. ! 654: ! 655: This option results in less efficient code, but some strange ! 656: hacks that alter the assembler output may be confused by the ! 657: optimizations performed when this option is not used. ! 658: ! 659: `-fvolatile' ! 660: Consider all memory references through pointers to be volatile. ! 661: ! 662: `-funsigned-char' ! 663: Let the type `char' be the unsigned, like `unsigned char'. ! 664: ! 665: Each kind of machine has a default for what `char' should be. It ! 666: is either like `unsigned char' by default or like `signed char' ! 667: by default. (Actually, at present, the default is always signed.) ! 668: ! 669: The type `char' is always a distinct type from either `signed ! 670: char' or `unsigned char', even though its behavior is always just ! 671: like one of those two. ! 672: ! 673: `-fsigned-char' ! 674: Let the type `char' be signed, like `signed char'. ! 675: ! 676: `-ffixed-REG' ! 677: Treat the register named REG as a fixed register; generated code ! 678: should never refer to it (except perhaps as a stack pointer, ! 679: frame pointer or in some other fixed role). ! 680: ! 681: REG must be the name of a register. The register names accepted ! 682: are machine-specific and are defined in the `REGISTER_NAMES' ! 683: macro in the machine description macro file. ! 684: ! 685: `-fcall-used-REG' ! 686: Treat the register named REG as an allocatable register that is ! 687: clobbered by function calls. It may be allocated for temporaries ! 688: or variables that do not live across a call. Functions compiled ! 689: this way will not save and restore the register REG. ! 690: ! 691: Use of this flag for a register that has a fixed pervasive role ! 692: in the machine's execution model, such as the stack pointer or ! 693: frame pointer, will produce disastrous results. ! 694: ! 695: `-fcall-saved-REG' ! 696: Treat the register named REG as an allocatable register saved by ! 697: functions. It may be allocated even for temporaries or variables ! 698: that live across a call. Functions compiled this way will save ! 699: and restore the register REG if they use it. ! 700: ! 701: Use of this flag for a register that has a fixed pervasive role ! 702: in the machine's execution model, such as the stack pointer or ! 703: frame pointer, will produce disastrous results. ! 704: ! 705: A different sort of disaster will result from the use of this ! 706: flag for a register in which function values are may be returned. ! 707: ! 708: `-dLETTERS' ! 709: Says to make debugging dumps at times specified by LETTERS. Here are ! 710: the possible letters: ! 711: ! 712: `r' 1.1 root 713: Dump after RTL generation. 1.1.1.2 ! root 714: `j' 1.1 root 715: Dump after first jump optimization. 1.1.1.2 ! root 716: `J' ! 717: Dump after last jump optimization. ! 718: `s' ! 719: Dump after CSE (including the jump optimization that sometimes ! 720: follows CSE). ! 721: `L' 1.1 root 722: Dump after loop optimization. 1.1.1.2 ! root 723: `f' 1.1 root 724: Dump after flow analysis. 1.1.1.2 ! root 725: `c' 1.1 root 726: Dump after instruction combination. 1.1.1.2 ! root 727: `l' 1.1 root 728: Dump after local register allocation. 1.1.1.2 ! root 729: `g' 1.1 root 730: Dump after global register allocation. 1.1.1.2 ! root 731: `m' ! 732: Print statistics on memory usage, at the end of the run. ! 733: ! 734: `-pedantic' ! 735: Issue all the warnings demanded by strict ANSI standard C; reject all ! 736: programs that use forbidden extensions. ! 737: ! 738: Valid ANSI standard C programs should compile properly with or without ! 739: this option (though a rare few will require `-ansi'). However, ! 740: without this option, certain GNU extensions and traditional C features ! 741: are supported as well. With this option, they are rejected. There is ! 742: no reason to use this option; it exists only to satisfy pedants. ! 743: ! 744: These options control the C preprocessor, which is run on each C source ! 745: file before actual compilation. If you use the `-E' option, nothing is ! 746: done except C preprocessing. Some of these options make sense only ! 747: together with `-E' because they request preprocessor output that is not ! 748: suitable for actual compilation. ! 749: ! 750: `-C' 1.1 root 751: Tell the preprocessor not to discard comments. Used with the `-E' 1.1.1.2 ! root 752: option. ! 753: ! 754: `-IDIR' 1.1 root 755: Search directory DIR for include files. 1.1.1.2 ! root 756: ! 757: `-M' ! 758: Tell the preprocessor to output a rule suitable for `make' describing ! 759: the dependencies of each source file. For each source file, the ! 760: preprocessor outputs one `make'-rule whose target is the object file ! 761: name for that source file and whose dependencies are all the files ! 762: `#include'd in it. This rule may be a single line or may be continued ! 763: with `\'-newline if it is long. ! 764: ! 765: `-M' implies `-E'. ! 766: ! 767: `-MM' ! 768: Like `-M' but the output mentions only the user-header files included ! 769: with `#include "FILE"'. System header files included with `#include ! 770: <FILE>' are omitted. ! 771: ! 772: `-MM' implies `-E'. ! 773: ! 774: `-DMACRO' 1.1 root 775: Define macro MACRO with the empty string as its definition. 1.1.1.2 ! root 776: ! 777: `-DMACRO=DEFN' 1.1 root 778: Define macro MACRO as DEFN. 1.1.1.2 ! root 779: ! 780: `-UMACRO' 1.1 root 781: Undefine macro MACRO. 1.1.1.2 ! root 782: ! 783: `-T' ! 784: Support ANSI C trigraphs. You don't want to know about this ! 785: brain-damage. The `-ansi' option also has this effect. 1.1 root 786: 787: 1.1.1.2 ! root 788: File: internals, Node: Installation, Next: Incompatibilities, Prev: Options, Up: Top 1.1 root 789: 790: Installing GNU CC 791: ***************** 792: 1.1.1.2 ! root 793: Here is the procedure for installing GNU CC on a Unix system. ! 794: ! 795: ! 796: * Menu: ! 797: ! 798: * VMS Install:: See below for installation on VMS. ! 799: ! 800: ! 801: 1. Edit `Makefile'. If you are using HPUX, you must make a few changes ! 802: described in comments at the beginning of the file. ! 803: ! 804: 2. Choose configuration files. ! 805: ! 806: * Make a symbolic link named `config.h' to the top-level config ! 807: file for the machine you are using (*Note Config::.). This file ! 808: is responsible for defining information about the host machine. ! 809: It includes `tm.h'. ! 810: ! 811: The file's name should be `config-MACHINE.h'. On VMS, use ! 812: `config-vms.h' rather than `config-vax.h'. On the HP 9000 series ! 813: 300, use `config-hp9k3.h' rather than `config-m68k.h'. ! 814: ! 815: If your system does not support symbolic links, you might want to ! 816: set up `config.h' to contain a `#include' command which refers to ! 817: the appropriate file. ! 818: ! 819: * Make a symbolic link named `tm.h' to the machine-description ! 820: macro file for your machine (its name should be `tm-MACHINE.h'). ! 821: ! 822: For the 68000/68020, do not use `tm-m68k.h' directly; instead use ! 823: one of the files `tm-sun3.h', `tm-sun2.h', `tm-isi68.h', ! 824: `tm-news800.h' or `tm-3b1.h'. Each of those files includes ! 825: `tm-m68k.h' but sets up a few things differently as appropriate ! 826: to the specific model of machine. ! 827: ! 828: There are two files you can use for a 680x0 running HPUX: ! 829: `tm-hp9k320.h' and `tm-hp9k320g.h'. Use the former if you are ! 830: installing GNU CC alone. The latter is for another option where ! 831: GNU CC together with the GNU assembler, linker, debugger and ! 832: other utilities are used to replace all of HPUX that deals with ! 833: compilation. Not all of the pieces of GNU software needed for ! 834: this mode of operation are as yet in distribution; full ! 835: instructions will appear here in the future. ! 836: ! 837: For the 32000, use `tm-sequent.h' if you are using a Sequent ! 838: machine; otherwise, use `tm-ns32k.h'. ! 839: ! 840: For the vax, use `tm-vax.h' on BSD Unix, `tm-ultrix.h' on Ultrix, ! 841: or `tm-vms.h' on VMS. ! 842: ! 843: * Make a symbolic link named `md' to the machine description ! 844: pattern file (its name should be `MACHINE.md'). ! 845: ! 846: * Make a symbolic link named `aux-output.c' to the output ! 847: subroutine file for your machine (its name should be ! 848: `OUTPUT-MACHINE.c'). ! 849: ! 850: 3. Make sure the Bison parser generator is installed. (This is unnecessary ! 851: if the Bison output file `parse.tab.c' is more recent than `parse.y' ! 852: and you do not plan to change `parse.y'.) ! 853: ! 854: Note that if you have an old version of Bison you may get an error ! 855: from the line with the `%expect' directive. If so, simply remove that ! 856: line from `parse.y' and proceed. ! 857: ! 858: 4. If you are using a Sun, make sure the environment variable ! 859: `FLOAT_OPTION' is not set. If this option were set to `f68881' when ! 860: `gnulib' is compiled, the resulting code would demand to be linked ! 861: with a special startup file and will not link properly without special ! 862: pains. ! 863: ! 864: 5. Build the compiler. Just type `make' in the compiler directory. ! 865: ! 866: 6. Move the first-stage object files and executables into a subdirectory ! 867: with this command: ! 868: ! 869: make stage1 ! 870: ! 871: ! 872: The files are moved into a subdirectory named `stage1'. Once ! 873: installation is complete, you may wish to delete these files with `rm ! 874: -r stage1'. ! 875: ! 876: 7. Recompile the compiler with itself, with this command: ! 877: ! 878: make CC=stage1/gcc CFLAGS="-g -O -Bstage1/" ! 879: ! 880: ! 881: On a 68000 or 68020 system lacking floating point hardware, unless you ! 882: have selected a `tm.h' file that expects by default that there is no ! 883: such hardware, do this instead: ! 884: ! 885: make CC=stage1/gcc CFLAGS="-g -O -Bstage1/ -msoft-float" ! 886: ! 887: ! 888: 8. If you wish to test the compiler by compiling it with itself one more ! 889: time, do this: ! 890: ! 891: make stage2 ! 892: make CC=stage2/gcc CFLAGS="-g -O -Bstage2/" ! 893: foreach file (*.o) ! 894: cmp $file stage2/$file ! 895: end ! 896: ! 897: ! 898: This will notify you if any of these stage 3 object files differs from ! 899: those of stage 2. Any difference, no matter how innocuous, indicates ! 900: that the stage 2 compiler has compiled GNU CC incorrectly, and is ! 901: therefore a potentially serious bug which you should investigate and ! 902: report (*Note Bugs::.). ! 903: ! 904: 9. Install the compiler driver, the compiler's passes and run-time support. ! 905: You can use the following command: ! 906: ! 907: make install ! 908: ! 909: ! 910: This copies the files `cc1', `cpp' and `gnulib' to files `gcc-cc1', ! 911: `gcc-cpp' and `gcc-gnulib' in directory `/usr/local/lib', which is ! 912: where the compiler driver program looks for them. It also copies the ! 913: driver program `gcc' into the directory `/usr/local', so that it ! 914: appears in typical execution search paths. ! 915: ! 916: *Warning: the GNU CPP may not work for `ioctl.h', `ttychars.h' and ! 917: other system header files unless the `-traditional' option is used.* ! 918: The bug is in the header files: at least on some machines, they rely ! 919: on behavior that is incompatible with ANSI C. This behavior consists ! 920: of substituting for macro argument names when they appear inside of ! 921: character constants. The `-traditional' option tells GNU CC to behave ! 922: the way these headers expect. ! 923: ! 924: Because of this problem, you might prefer to configure GNU CC to use ! 925: the system's own C preprocessor. To do so, make the file ! 926: `/usr/local/lib/gcc-cpp' a link to `/lib/cpp'. ! 927: ! 928: Alternatively, on Sun systems and 4.3BSD at least, you can correct the ! 929: include files by running the shell script `fixincludes'. This ! 930: installs modified, corrected copies of the files `ioctl.h' and ! 931: `ttychars.h' in a special directory where only GNU CC will normally ! 932: look for them. ! 933: ! 934: The file `/usr/include/vaxuba/qvioctl.h' used in the X window system ! 935: needs a similar correction. ! 936: ! 937: If you cannot install the compiler's passes and run-time support in ! 938: `/usr/local/lib', you can alternatively use the `-B' option to specify a ! 939: prefix by which they may be found. The compiler concatenates the prefix ! 940: with the names `cpp', `cc1' and `gnulib'. Thus, you can put the files in ! 941: a directory `/usr/foo/gcc' and specify `-B/usr/foo/gcc/' when you run GNU CC. ! 942: ! 943: ! 944: File: internals, Node: VMS Install, Prev: Installation, Up: Installation ! 945: ! 946: Installing GNU CC on VMS ! 947: ======================== ! 948: ! 949: The VMS version of GNU CC is normally distributed as a Backup saveset, so ! 950: the only installation required is to copy the files. But here is how to ! 951: rebuild GNU CC if you change it: ! 952: ! 953: 1. Copy the file `tm-vms.h' to `tm.h', `config-vms.h' to `config.h', ! 954: `vax.md' to `md.' and `output-vax.c' to `aux-output.c'. ! 955: ! 956: 2. Type `@make' to do recompile everything. ! 957: ! 958: ! 959: File: internals, Node: Incompatibilities, Next: Extensions, Prev: Installation, Up: Top ! 960: ! 961: Incompatibilities of GNU CC ! 962: *************************** ! 963: ! 964: There are several noteworthy incompatibilities between GNU C and most ! 965: existing (non-ANSI) versions of C. ! 966: ! 967: Ultimately our intention is that the `-traditional' option will eliminate ! 968: most of these incompatibilities by telling GNU C to behave like the other C ! 969: compilers. ! 970: ! 971: * GNU CC normally makes string constants read-only. If several ! 972: identical-looking string constants are used, GNU CC stores only one ! 973: copy of the string. ! 974: ! 975: One consequence is that you cannot call `mktemp' with a string ! 976: constant argument. The function `mktemp' always alters the string its ! 977: argument points to. ! 978: ! 979: Another consequence is that `sscanf' does not work on some systems ! 980: when passed a string constant as its format control string. This is ! 981: because `sscanf' incorrectly tries to write into the string constant. ! 982: ! 983: The best solution to these problems is to change the program to use ! 984: `char'-array variables with initialization strings for these purposes ! 985: instead of string constants. But if this is not possible, you can use ! 986: the `-fwritable-strings' flag, which directs GNU CC to handle string ! 987: constants the same way most C compilers do. ! 988: ! 989: * GNU CC does not substitute macro arguments when they appear inside of ! 990: string constants. For example, the following macro in GNU CC ! 991: ! 992: #define foo(a) "a" ! 993: ! 994: ! 995: will produce output `"a"' regardless of what the argument A is. ! 996: ! 997: The `-traditional' option directs GNU CC to handle such cases (among ! 998: others) in the old-fashioned (non-ANSI) fashion. ! 999: ! 1000: * When you use `setjmp' and `longjmp', the only automatic variables ! 1001: guaranteed to remain valid are those declared `volatile'. This is a ! 1002: consequence of automatic register allocation. Consider this function: ! 1003: ! 1004: jmp_buf j; 1.1 root 1005: 1.1.1.2 ! root 1006: foo () ! 1007: { ! 1008: int a, b; 1.1 root 1009: 1.1.1.2 ! root 1010: a = fun1 (); ! 1011: if (setjmp (j)) ! 1012: return a; 1.1 root 1013: 1.1.1.2 ! root 1014: a = fun2 (); ! 1015: /* `longjmp (j)' may be occur in `fun3'. */ ! 1016: return a + fun3 (); ! 1017: } 1.1 root 1018: 1019: 1.1.1.2 ! root 1020: Here `a' may or may not be restored to its first value when the ! 1021: `longjmp' occurs. If `a' is allocated in a register, then its first ! 1022: value is restored; otherwise, it keeps the last value stored in it. 1.1 root 1023: 1.1.1.2 ! root 1024: If you use the `-W' option with the `-O' option, you will get a ! 1025: warning when GNU CC thinks such a problem might be possible. 1.1 root 1026: 1.1.1.2 ! root 1027: * Declarations of external variables and functions within a block apply ! 1028: only to the block containing the declaration. In other words, they ! 1029: have the same scope as any other declaration in the same place. 1.1 root 1030: 1.1.1.2 ! root 1031: In some other C compilers, a `extern' declaration affects all the rest ! 1032: of the file even if it happens within a block. ! 1033: ! 1034: The `-traditional' option directs GNU C to treat all `extern' ! 1035: declarations as global, like traditional compilers. ! 1036: ! 1037: * In traditional C, you can combine `long', etc., with a typedef name, ! 1038: as shown here: ! 1039: ! 1040: typedef int foo; ! 1041: typedef long foo bar; ! 1042: ! 1043: ! 1044: In ANSI C, this is not allowed: `long' and other type modifiers ! 1045: require an explicit `int'. Because this criterion is expressed by ! 1046: Bison grammar rules rather than C code, the `-traditional' flag cannot ! 1047: alter it. ! 1048: ! 1049: * When compiling functions that return structures or unions, GNU CC ! 1050: output code uses a method different from that used on most versions of ! 1051: Unix. As a result, code compiled with GNU CC cannot call a ! 1052: structure-returning function compiled with PCC, and vice versa. ! 1053: ! 1054: The method used by GCC is as follows: a structure or union which is 1, ! 1055: 2, 4 or 8 bytes long is returned like a scalar. A structure or union ! 1056: with any other size is stored into an address supplied by the caller ! 1057: in a special, fixed register. ! 1058: ! 1059: PCC usually handles all sizes of structures and unions by returning ! 1060: the address of a block of static storage containing the value. This ! 1061: method is not used in GCC because it is slower and nonreentrant. ! 1062: ! 1063: On systems where PCC works this way, you may be able to make ! 1064: GCC-compiled code call such functions that were compiled with PCC by ! 1065: declaring them to return a pointer to the structure or union instead ! 1066: of the structure or union itself. For example, instead of this: ! 1067: ! 1068: struct foo nextfoo (); ! 1069: ! 1070: ! 1071: write this: ! 1072: ! 1073: struct foo *nextfoo (); ! 1074: #define nextfoo *nextfoo 1.1 root 1075: 1.1.1.2 ! root 1076: ! 1077: (Note that this assumes you are using the GNU preprocessor, so that ! 1078: the ANSI antirecursion rules for macro expansions are effective.) 1.1 root 1079: 1080: 1.1.1.2 ! root 1081: File: internals, Node: Extensions, Next: Bugs, Prev: Incompatibilities, Up: Top ! 1082: ! 1083: GNU Extensions to the C Language ! 1084: ******************************** 1.1 root 1085: 1.1.1.2 ! root 1086: GNU C provides several language features not found in ANSI standard C. ! 1087: (The `-pedantic' option directs GNU CC to print a warning message if any of ! 1088: these features is used.) To test for the availability of these features in ! 1089: conditional compilation, check for a predefined macro `__GNUC__', which is ! 1090: always defined under GNU CC. 1.1 root 1091: 1092: 1093: * Menu: 1094: 1.1.1.2 ! root 1095: * Statement Exprs:: Putting statements and declarations inside expressions. ! 1096: * Naming Types:: Giving a name to the type of some expression. ! 1097: * Typeof:: `typeof': referring to the type of an expression. ! 1098: * Lvalues:: Using `?:', `,' and casts in lvalues. ! 1099: * Conditionals:: Omitting the middle operand of a `?:' expression. ! 1100: * Zero-Length:: Zero-length arrays. ! 1101: * Variable-Length:: Arrays whose length is computed at run time. ! 1102: * Subscripting:: Any array can be subscripted, even if not an lvalue. ! 1103: * Pointer Arith:: Arithmetic on `void'-pointers and function pointers. ! 1104: * Constructors:: Constructor expressions give structures, unions ! 1105: or arrays as values. ! 1106: * Dollar Signs:: Dollar sign is allowed in identifiers. ! 1107: * Alignment:: Inquiring about the alignment of a type or variable. ! 1108: * Inline:: Defining inline functions (as fast as macros). ! 1109: * Extended Asm:: Assembler instructions with C expressions as operands. ! 1110: (With them you can define ``built-in'' functions.) ! 1111: * Asm Labels:: Specifying the assembler name to use for a C symbol. ! 1112: 1.1 root 1113: 1114: 1.1.1.2 ! root 1115: File: internals, Node: Statement Exprs, Next: Naming Types, Prev: Extensions, Up: Extensions 1.1 root 1116: 1.1.1.2 ! root 1117: Statements and Declarations inside of Expressions ! 1118: ================================================= 1.1 root 1119: 1.1.1.2 ! root 1120: A compound statement in parentheses may appear inside an expression in GNU ! 1121: C. This allows you to declare variables within an expression. For example: 1.1 root 1122: 1.1.1.2 ! root 1123: ({ int y = foo (); int z; ! 1124: if (y > 0) z = y; ! 1125: else z = - y; ! 1126: z; }) 1.1 root 1127: 1128: 1.1.1.2 ! root 1129: is a valid (though slightly more complex than necessary) expression for the ! 1130: absolute value of `foo ()'. 1.1 root 1131: 1.1.1.2 ! root 1132: This feature is especially useful in making macro definitions ``safe'' (so ! 1133: that they evaluate each operand exactly once). For example, the ! 1134: ``maximum'' function is commonly defined as a macro in standard C as follows: 1.1 root 1135: 1.1.1.2 ! root 1136: #define max(a,b) ((a) > (b) ? (a) : (b)) 1.1 root 1137: 1138: 1.1.1.2 ! root 1139: But this definition computes either A or B twice, with bad results if the ! 1140: operand has side effects. In GNU C, if you know the type of the operands ! 1141: (here let's assume `int'), you can define the macro safely as follows: 1.1 root 1142: 1.1.1.2 ! root 1143: #define maxint(a,b) \ ! 1144: ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) 1.1 root 1145: 1146: 1.1.1.2 ! root 1147: Embedded statements are not allowed in constant expressions, such as the ! 1148: value of an enumeration constant, the width of a bit field, or the initial ! 1149: value of a static variable. 1.1 root 1150: 1.1.1.2 ! root 1151: If you don't know the type of the operand, you can still do this, but you ! 1152: must use `typeof' (*Note Typeof::.) or type naming (*Note Naming Types::.). 1.1 root 1153: 1154: 1.1.1.2 ! root 1155: File: internals, Node: Naming Types, Next: Typeof, Prev: Statement Exprs, Up: Extensions ! 1156: ! 1157: Naming an Expression's Type ! 1158: =========================== ! 1159: ! 1160: You can give a name to the type of an expression using a `typedef' ! 1161: declaration with an initializer. Here is how to define NAME as a type name ! 1162: for the type of EXP: ! 1163: ! 1164: typedef NAME = EXP; 1.1 root 1165: 1166: 1.1.1.2 ! root 1167: This is useful in conjunction with the statements-within-expressions ! 1168: feature. Here is how the two together can be used to define a safe ! 1169: ``maximum'' macro that operates on any arithmetic type: 1.1 root 1170: 1.1.1.2 ! root 1171: #define max(a,b) \ ! 1172: ({typedef _ta = (a), _tb = (b); \ ! 1173: _ta _a = (a); _tb _b = (b); \ ! 1174: _a > _b ? _a : _b; }) ! 1175: ! 1176: ! 1177: The reason for using names that start with underscores for the local ! 1178: variables is to avoid conflicts with variable names that occur within the ! 1179: expressions that are substituted for `a' and `b'. Eventually we hope to ! 1180: design a new form of declaration syntax that allows you to declare ! 1181: variables whose scopes start only after their initializers; this will be a ! 1182: more reliable way to prevent such conflicts. 1.1 root 1183: 1184: 1.1.1.2 ! root 1185: File: internals, Node: Typeof, Next: Lvalues, Prev: Naming Types, Up: Extensions ! 1186: ! 1187: Referring to a Type with `typeof' ! 1188: ================================= ! 1189: ! 1190: Another way to refer to the type of an expression is with `typeof'. The ! 1191: syntax of using of this keyword looks like `sizeof', but the construct acts ! 1192: semantically like a type name defined with `typedef'. ! 1193: ! 1194: There are two ways of writing the argument to `typeof': with an expression ! 1195: or with a type. Here is an example with an expression: ! 1196: ! 1197: typeof (x[0](1)) ! 1198: ! 1199: ! 1200: This assumes that `x' is an array of functions; the type described is that ! 1201: of the values of the functions. ! 1202: ! 1203: Here is an example with a typename as the argument: ! 1204: ! 1205: typeof (int *) 1.1 root 1206: 1207: 1.1.1.2 ! root 1208: Here the type described is that of pointers to `int'. 1.1 root 1209: 1.1.1.2 ! root 1210: A `typeof'-construct can be used anywhere a typedef name could be used. ! 1211: For example, you can use it in a declaration, in a cast, or inside of ! 1212: `sizeof' or `typeof'. ! 1213: ! 1214: * This declares `y' with the type of what `x' points to. ! 1215: ! 1216: typeof (*x) y; ! 1217: ! 1218: ! 1219: * This declares `y' as an array of such values. ! 1220: ! 1221: typeof (*x) y[4]; ! 1222: ! 1223: ! 1224: * This declares `y' as an array of pointers to characters: ! 1225: ! 1226: typeof (typeof (char *)[4]) y; ! 1227: ! 1228: ! 1229: It is equivalent to the following traditional C declaration: ! 1230: ! 1231: char *y[4]; ! 1232: ! 1233: ! 1234: To see the meaning of the declaration using `typeof', and why it might ! 1235: be a useful way to write, let's rewrite it with these macros: ! 1236: ! 1237: #define pointer(T) typeof(T *) ! 1238: #define array(T, N) typeof(T [N]) ! 1239: ! 1240: ! 1241: Now the declaration can be rewritten this way: ! 1242: ! 1243: array (pointer (char), 4) y; ! 1244: ! 1245: ! 1246: Thus, `array (pointer (char), 4)' is the type of arrays of 4 pointers ! 1247: to `char'. ! 1248: ! 1249:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.