home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / c-tools / vbcc / doc / vbcc.doc < prev   
Encoding:
Text File  |  1996-06-17  |  20.0 KB  |  549 lines

  1. vbcc - C compiler (c) in 1995-96 by Volker Barthelmann
  2. vbpp - C preprocessor (c) in 1995-96 by Thorsten Schaaps
  3.  
  4.  
  5. INTRODUCTION
  6.  
  7.     vbcc is a free portable and retargetable ANSI C compiler.
  8.     It is split into a target independant and a target dependant part and
  9.     supports emulating datatypes of the target machine on any other machine
  10.     so that it is possible to e.g. make a crosscompiler for a 64bit machine
  11.     on a 32bit machine.
  12.  
  13.     The target independant part generates a form of intermediate code
  14.     (quads) which has to be dealt with by the code generator. This
  15.     intermediate code is rather cpu independant (apart from register usage),
  16.     but the target independant part of vbcc uses informations about the
  17.     target machine while generating this code.
  18.  
  19.     If you are interested in writing a code generator for vbcc, contact
  20.     me (the necessary documents are not written yet).
  21.  
  22.     This document only deals with the target independant parts of vbcc.
  23.     Be sure to read all the documents in the doc directory for your machine.
  24.  
  25.  
  26. LEGAL
  27.  
  28.     vbcc is (c) in 1995-96 by Volker Barthelmann. The builtin preprocessor
  29.     (consisting of the files preproc.c and vbpp.h) is written and (c) by
  30.     Thorsten Schaaps. All other code is (c) by Volker Barthelmann.
  31.     vbcc may be freely redistributed as long as no modifications are made
  32.     and nothing is charged for it.
  33.     Non-commercial usage of vbcc is allowed without any restrictions.
  34.     Commercial usage needs my written consent.
  35.  
  36.     Sending me money, gifts, postcards etc. would of course be very nice
  37.     and may encourage further development of vbcc, but is not legally or
  38.     morally necessary to use vbcc.
  39.  
  40.  
  41. INSTALLATION
  42.  
  43.     The installation is system dependant and covered in another manual.
  44.  
  45.  
  46. USAGE
  47.  
  48.     Usually vbcc will be called by a frontend. However if you call it
  49.     directly, it has to be done like this (and most of the options
  50.     should be passed through to vbcc by the frontend).
  51.  
  52.     vbcc [options] file
  53.  
  54.     The following options are supported by the machine independant part
  55.     of vbcc:
  56.  
  57.     -quiet      Do not print the copyright notice.
  58.  
  59.     -ic1        Write the intermediate code before optimizing to file.ic1.
  60.  
  61.     -ic2        Write the intermediate code after optimizing to file.ic2.
  62.  
  63.     -debug=n    Set the debug level to n.
  64.  
  65.     -noasm      Do not generate assembler output (only for testing).
  66.  
  67.     -O=n        Turns optimizing options on/off; every bit set in n turns
  68.                 on an option.
  69.                 (See section on optimizing.)
  70.  
  71.     -maxoptpasses=n
  72.                 Set maximum number of optimizer passes to n.
  73.                 (See section on optimizing.)
  74.  
  75.     -inline-size=n
  76.                 Set the maximum 'size' of functions to be inlined.
  77.                 (See section on optimizing.)
  78.  
  79.     -ansi       Switch to ANSI mode.
  80.                 In ANSI mode every warning with ANSI violation will be an
  81.                 error and all other warnings are suppressed.
  82.                 Also assignments between pointers to type and pointers to
  83.                 unsigned type will be errors.
  84.  
  85.     -maxerrors=n
  86.                 Abort the compilation after n errors; do not stop if n==0.
  87.  
  88.     -dontwarn=n
  89.                 Suppress warning number n; suppress all warnings if n<0.
  90.                 (See the section on errors/warnings.)
  91.  
  92.     -warn=n
  93.                 Turn on warning number n; turn on all warnings if n<0.
  94.                 (See the section on errors/warnings.)
  95.  
  96.     -nested-comments
  97.                 Allow nested comments (not ANSI conforming).
  98.                 Has no effect if the builtin preprocessor is disabled.
  99.  
  100.     -cpp-comments
  101.                 Allow C++ style comments (not ANSI conforming).
  102.                 Has no effect if the builtin preprocessor is disabled.
  103.  
  104.     -macro-redefinition
  105.                 Allow redefinition of macros (not ANSI conforming).
  106.                 Has no effect if the builtin preprocessor is disabled.
  107.  
  108.     -no-trigraphs
  109.                 Prevents expansion of trigraphs (not ANSI conforming).
  110.                 Has no effect if the builtin preprocessor is disabled.
  111.  
  112.     -no-preprocessor
  113.                 Do not invoke the builtin preprocessor vbpp.
  114.  
  115.     -E          Only preprocess the file and write the preprocessed
  116.                 source to <file>.i.
  117.  
  118.     The assembler output will be saved to file.asm (if file already contained
  119.     a suffix, this will first be removed; same applies to .ic1/.ic2)
  120.  
  121.  
  122. SOME INTERNALS
  123.  
  124.     I try to make vbcc as ANSI compliant as possible (but this is not that
  125.     easy, because I do not have the ANSI document), so I am only mentioning
  126.     some things I consider interesting.
  127.  
  128.     First, there still may be some bugs, but vbcc seems stable now unless
  129.     you use the optimizer.
  130.  
  131.  
  132. ERRORS/WARNINGS
  133.  
  134.     vbcc knows the following kinds of messages:
  135.  
  136.     fatal errors        Something is badly wrong and further compilation is
  137.                         impossible or pointless. vbcc will abort.
  138.                         E.g. no source file or really corrupt source.
  139.  
  140.     errors              There was an error and vbcc cannot generate useful
  141.                         code. Compilation continues, but no code should be
  142.                         generated.
  143.                         E.g. unknown identifiers.
  144.  
  145.     warnings (1)        Warnings with ANSI-violations. The program is not
  146.                         ANSI-conforming, but vbcc will generate code that
  147.                         could be what you want (or not).
  148.                         E.g. missing semicolon.
  149.  
  150.     warnings (2)        The code has no ANSI-violations, but contains some
  151.                         strange things you should perhaps look at.
  152.                         E.g. unused variables.
  153.  
  154.     Errors or the first kind of warnings are always displayed and cannot
  155.     be suppressed.
  156.  
  157.     Only some warnings of the second kind are turned on by default.
  158.     Many of them are very useful for some, but annoying to others and
  159.     their usability may depend on programming style. As I do not want
  160.     to force anyone to a certain style I recommend everyone to find
  161.     their own preferences.
  162.  
  163.     A good way to do this is starting with all warnings turned on by
  164.     -warn=-1 so you will see all possible warnings. Now everytime you
  165.     get a warning you do not find useful, turn that one off with
  166.     -dontwarn=n.
  167.     The file errors.doc contains a list of all errors/warnings, sometimes
  168.     with more detailed descriptions. This might be very useful, too.
  169.  
  170.     See the docs on your frontend on how to configure it to your
  171.     preferences.
  172.  
  173.  
  174. DATA TYPES
  175.  
  176.     vbcc can handle the following atomic data types:
  177.  
  178.     signed/unsigned char/short/int/long  (signed is always default)
  179.     float/double  (long double is always the same as double)
  180.  
  181.     However several of them can be identical in certain implementations.
  182.  
  183.  
  184. OPTIMIZATIONS
  185.  
  186.     vbcc can compile with or without global optimizations.
  187.     But note that the optimizer is not yet finished and is known to generate
  188.     corrupt code in some cases. So only use it with extreme care.
  189.  
  190.     In the first compilation phase every function is parsed into a tree
  191.     structure one expression after the other. Then type-checking and some
  192.     minor optimizations like constant-folding or some algebraic
  193.     simplifications are done on the trees.
  194.     This phase of the translation is identical in optimizing and
  195.     non-optimizing compilation.
  196.  
  197.     Then intermediate code is generated from the trees. In non-optimizing
  198.     compilation temporaries needed to evaluate the expression are
  199.     immediately assigned to registers if possible. In optimizing compilation
  200.     a new variable is generated for each temporary required.
  201.     Also for certain constructs like loops different intermediate code
  202.     is produced in optimizing compilation.
  203.     Some minor optimizations are performed while generating the intermediate
  204.     code (simple elimination of unreachable code, some optimizations on
  205.     branches etc.).
  206.  
  207.     After intermediate code for the whole function has been generated
  208.     simple register allocation may be done in non-optimizing compilation
  209.     if bit 1 has been set in the -O option.
  210.     After that the intermediate code is passed to the code generator and
  211.     then all memory for the function, its variables etc. is freed.
  212.  
  213.     In optimizing compilation flowgraphs are constructed, data flow analysis
  214.     is performed and many passes are made over the function's intermediate
  215.     code. Code may be moved around, new variables may be added, other
  216.     variables removed etc. etc. (for more detailed information on the
  217.     performed optimizations look at the description for the -O option
  218.     below).
  219.  
  220.     Many of the optimization routines depend on each other and if one
  221.     routine finds an optimization this often enables other routines to
  222.     find further ones and some routines only do a first step and let
  223.     other routines 'clean up' after it. Because of this vbcc usually
  224.     makes many passes until no further optimizations are found.
  225.     To avoid possible extremely long optimization times the number of
  226.     those passes can be limited with the -maxoptpasses=n option (the
  227.     default value is max. 10 passes).
  228.  
  229.     Now it will be decided if the compiled function is a candidate for
  230.     inlining. In this case the intermediate code as well as the data
  231.     structures for the local variables will be copied and stored until
  232.     compilation of the entire translation-unit has finished.
  233.  
  234.     After those phases register allocation should be done. As temporaries
  235.     have not been assigned to registers up to this point, register
  236.     allocation is crucial in optimizing compilation (actually if optimizing
  237.     is desired there are rarely reasons not to turn on all flags - also
  238.     note that some flags MUST be turned on or vbcc may even crash).
  239.  
  240.     Note that optimizing compilation can take MUCH more time and needs
  241.     MUCH more memory. It is hard to predict how much time and space it
  242.     needs, but usually it roughly depends on length of a function (time
  243.     and space needed will usually increase more than linear with the
  244.     length of a function).
  245.  
  246.  
  247.     At the moment the following bits in the -O option are recognized:
  248.  
  249.  
  250.     Bit 0 (1)           Register allocation
  251.  
  252.     This is the only flag that has any effect in non-optimizing compilation.
  253.  
  254.     In non-optimizing compilation any registers that have never been used
  255.     for temporaries in this function are used for register variables in a
  256.     simple way.
  257.     For each variable a priority to registerize it is computed (this has
  258.     already been done during generation of intermediate code). This value
  259.     usually reflects how much can be gained by putting it in a register.
  260.     Then for every free register the variable with the highest priority
  261.     that can be stored in that register is assigned that register for
  262.     the entire function.
  263.     This improves the generated code quite a bit.
  264.     The effect on compile time is hard to predict, but it should be very
  265.     small - this optimization may even speed up the compilation.
  266.  
  267.     In optimizing compilation several passes are made:
  268.  
  269.     - First all temporaries are assigned to registers in basic blocks.
  270.       Temporaries are recognized by utilising data flow information on
  271.       active variables and one variable can be a temporary at one or
  272.       several points although it is alive over several basic blocks at
  273.       another point.
  274.  
  275.     - Then vbcc computes approximate savings that can be obtained by
  276.       holding a variable in a register within a loop and assigns the
  277.       most used variables to registers within loops (starting with
  278.       innermost loops).
  279.       Information on the function's loop structure and active variables
  280.       are used.
  281.  
  282.     - Now about the same is done for the whole function, i.e. vbcc looks
  283.       for variables to put in a register for the entire function.
  284.  
  285.     - At last the basic blocks are scanned and if there are variables
  286.       that can efficiently be put in a register for one basic block
  287.       this is done (at the moment this pass is done only if bit 6 is
  288.       set, but this may change in the future and bit 6 may have a
  289.       different meaning then).
  290.       Then instructions to copy variables from memory to a register and
  291.       back are inserted when necessary.
  292.  
  293.  
  294.     Bit 1 (2)           activate optimizing compilation
  295.  
  296.     This flag turns on the optimizer. If it is set to zero no global
  297.     optimizations will be performed no matter what the other flags are set
  298.     to.
  299.     When turned on slightly different intermediate code will be generated
  300.     by the first translation phases.
  301.     Also the following optimizations are performed:
  302.  
  303.     - A flow graph is constructed and unused labels are deleted.
  304.  
  305.     - Unreachable code is eliminated.
  306.  
  307.     - Jump optimizations are performed.
  308.  
  309.     - Several peephole optimizations like constant folding and algebraic
  310.       simplifications are performed on the intermediate code.
  311.  
  312.     - Identical statements at the beginning/end of basic blocks are
  313.       moved to the successors/predecessors under certain conditions.
  314.  
  315.  
  316.     Bit 2 (4)           common subexpression elimination
  317.  
  318.     The intermediate code is scanned for common subexpressions that can be
  319.     eliminated. Also copy propagation is performed.
  320.     This can be done only within basic blocks or over the whole function
  321.     depending on bit 5.
  322.     If global cse is selected data flow analysis for available expressions
  323.     and available copies is performed.
  324.  
  325.     Example for common subexpression elimination:
  326.  
  327.     q(int a,int b)
  328.     {
  329.         if(c){
  330.             f(a+b);
  331.         }else{
  332.             g(a+b);
  333.         }
  334.         return(a+b);
  335.     }
  336.  
  337.     will become something like:
  338.  
  339.     q(int a,int b)
  340.     {
  341.         int t;
  342.         if(c){
  343.             t=a+b;
  344.             f(t);
  345.         }else{
  346.             t=a+b;
  347.             g(t);
  348.         }
  349.         return(t);
  350.     }
  351.  
  352.     Often common subexpressions occur in array indexing where they are not
  353.     as obvious to the programmer.
  354.  
  355.     Note that the local versions of these optimizations are only restricted
  356.     versions of the global ones. They operate on the intermediate code
  357.     rather than on trees and therefore are slower than they could be
  358.     on compilers that only perform local versions.
  359.  
  360.  
  361.     Bit 3 (8)           constant propagation
  362.  
  363.     Variables which are known to have a constant value at one time are
  364.     replaced by constants.
  365.     This can be done only within basic blocks or over the whole function
  366.     depending on bit 5.
  367.     If global constant propagation is selected data flow analysis for
  368.     reaching definitions is performed.
  369.  
  370.     Note that the local versions of these optimizations are only restricted
  371.     versions of the global ones. They operate on the intermediate code
  372.     rather than on trees and therefore are slower than they could be
  373.     on compilers that only perform local versions.
  374.  
  375.  
  376.     Bit 4 (16)          reserved for future use
  377.  
  378.  
  379.     Bit 5 (32)          global optimization
  380.  
  381.     Some optimizations are available in local and global versions. This
  382.     flag turns on the global versions.
  383.     At the moment this effects common subexpression elimination, copy
  384.     propagation and constant propagation.
  385.  
  386.     Also if this flag is not turned on only one optimization pass is done
  387.     whereas several are done if it is turned on.
  388.  
  389.     Not turning on this flag results in worse code and often shorter
  390.     compile time. However there are cases where this increases compile
  391.     time, too.
  392.  
  393.     It seems reasonable to me to always turn it on.
  394.  
  395.  
  396.     Bit 6 (64)          register allocation on basic blocks/
  397.                         reserved for future use
  398.  
  399.                         See Bit 1.
  400.  
  401.  
  402.     Bit 7 (128)         reserved for future use
  403.  
  404.     Bit 8 (256)         reserved for future use
  405.  
  406.     Bit 9 (512)         reserved for future use
  407.  
  408.     Bit 10 (1024)       reserved for future use
  409.  
  410.     Bit 11 (2048)       reserved for future use
  411.  
  412.  
  413.     Bit 12 (4096)       function inlining
  414.  
  415.     The intermediate code of functions that meet certain conditions
  416.     (mainly adjustable by -inline-size) is kept in memory for the entire
  417.     translation unit and subsequent calls to this function are replaced
  418.     with this code.
  419.  
  420.     This way constant arguments can be propagated across the function
  421.     and certain parts of the function may be omitted. Also common
  422.     subexpressions across the functions can be eliminated.
  423.  
  424.     An inlined function call is about the same as a macro expansion (but
  425.     safer).
  426.  
  427.  
  428.     Also look at the documentation for the target dependant part of vbcc.
  429.     There may be additional machine specific optimization options.
  430.  
  431.  
  432. PRAGMA
  433.  
  434.     At the moment vbcc accepts the following #pragma-directives (note that
  435.     case is important and commands etc. must be separated by exactly one
  436.     space at the moment):
  437.  
  438.     #pragma type <expr>;            Write the type of <expr> to stdout.
  439.                                     This is mainly intended for testing.
  440.  
  441.     #pragma tree <expr>;            Write the parse-tree of <expr> to stdout.
  442.                                     This is mainly intended for testing.
  443.  
  444.     #pragma only-inline on          The following functions are prepared for
  445.                                     inlining, but no code is generated. This
  446.                                     can be used e.g. in header-files to
  447.                                     supply inline versions of certain
  448.                                     functions.
  449.                                     -inline-size is ignored in this mode -
  450.                                     every function gets prepared for inlining.
  451.  
  452.                                     Do not use this with functions that have
  453.                                     local static variables!
  454.  
  455.     #pragma only-inline off         The following functions are translated
  456.                                     as usual again.
  457.  
  458.  
  459. KNOWN PROBLEMS
  460.  
  461.     Some known target independant problems of vbcc at the moment:
  462.  
  463.     - Some size limits are still hardcoded into the source (the maximum
  464.       nesting of blocks and the maximum length of input lines).
  465.  
  466.     - Switch statements do never use jump-tables.
  467.  
  468.     - Bitfields are not supported (they are always used as int) - this should
  469.       be ANSI compliant, however.
  470.  
  471.     - The source was written with an optimizing compiler in mind and is
  472.       somewhat inefficient.
  473.  
  474.     - Initialized data is kept in memory for the whole translation-unit.
  475.       This enables some optimizations, but very much memory is needed if
  476.       a translation-unit contains many initializations.
  477.  
  478.     - The source is not strictly ANSI conforming (i.e. fully portable), yet.
  479.       E.g. there may be some external identifiers with more than 6
  480.       identical characters (haven't checked yet) and other problems.
  481.       However in practice it is very portable.
  482.  
  483.     - Code for accessing volatile objects may not be generated if the result
  484.       is not used.
  485.  
  486.     - Messages that do not refer to a particular line (e.g. unused variable
  487.       warnings or warnings found in optimizing) get printed with a more
  488.       or less random line number.
  489.  
  490.     - long double is not really supported (see errors.doc).
  491.  
  492.     - The optimizer is not finished and is not stable.
  493.  
  494.  
  495. THE FUTURE
  496.  
  497.     - Bugfixing where necessary.
  498.  
  499.     - Completing the builtin preprocessor
  500.  
  501.     - Perhaps adding possibility to use precompiled header-files.
  502.  
  503.     - Finishing the optimizer (fixing some bugs and adding several other
  504.       optimizations - mainly loop-optimizations, alias-optimizations and
  505.       interprocedural optimizations).
  506.  
  507.     - Certain features like register parameters.
  508.  
  509.     - Other code generators.
  510.  
  511.     - If you have any other ideas, tell me.
  512.  
  513.  
  514.  
  515. CREDITS
  516.  
  517.     All those who wrote parts of the vbcc distribution, made suggestions,
  518.     answered my questions, tested vbcc, reported errors or were otherwise
  519.     involved in the development of vbcc (in descending alphabetical order,
  520.     under work, not complete):
  521.  
  522.     Frank Wille
  523.     Ralph Schmidt
  524.     Markus Schmidinger
  525.     Thorsten Schaaps
  526.     Joerg Plate
  527.     Gunther Nikl
  528.     Joern Maass
  529.     Kai Kohlmorgen
  530.     Dirk Holtwick
  531.     Volker Graf
  532.     Matthias Fleischer
  533.     Robert Ennals
  534.     Thomas Dorn
  535.     Walter Doerwald
  536.     Lars Dannenberg
  537.     Michael Bode
  538.     Michael Bauer
  539.     Juergen Barthelmann
  540.     Thomas Arnhold
  541.     Thomas Aglassinger
  542.  
  543.  
  544. Volker Barthelmann                                      volker@vb.franken.de
  545. Kennedy-Ring 39
  546. 91301 Forchheim
  547. Germany
  548.  
  549.