home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / vbcc / doc / vbcc.doc < prev    next >
Encoding:
Text File  |  1997-01-31  |  24.3 KB  |  632 lines

  1. vbcc - C compiler (c) in 1995-97 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-97 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.     -o=ofile    Write the generated assembler output to <ofile> rather than
  66.                 the default file.
  67.  
  68.     -noasm      Do not generate assembler output (only for testing).
  69.  
  70.     -O=n        Turns optimizing options on/off; every bit set in n turns
  71.                 on an option.
  72.                 (See section on optimizing.)
  73.  
  74.     -maxoptpasses=n
  75.                 Set maximum number of optimizer passes to n.
  76.                 (See section on optimizing.)
  77.  
  78.     -inline-size=n
  79.                 Set the maximum 'size' of functions to be inlined.
  80.                 (See section on optimizing.)
  81.  
  82.     -unroll-size=n
  83.                 Set the maximum 'size' of unrolled loops.
  84.                 (See section on optimizing.)
  85.  
  86.     -fp-associative
  87.                 Floating point operations do not obey the law of
  88.                 associativity, e.g. (a+b)+c==a+(b+c) is not true for all
  89.                 floating point numbers a,b,c. Therefore certain optimizations
  90.                 depending on this property cannot be performed on floating
  91.                 point numbers.
  92.                 With this option you can tell vbcc to treat floating point
  93.                 operations as associative and perform those optimizations
  94.                 even if that may change the results in some cases (not
  95.                 ANSI conforming).
  96.  
  97.     -no-alias-opt
  98.                 If the optimizer is turned on vbcc has to make assumptions
  99.                 on aliasing (i.e. which pointer can point to which
  100.                 objects at a given time). If this option is specified
  101.                 vbcc will make worst-case assumptions and some
  102.                 non-conforming programs could be made to work that way.
  103.  
  104.     -no-multiple-ccs
  105.                 If the code generator supports multiple condition code
  106.                 registers vbcc will try to use them when optimizing.
  107.                 This flag prevents vbcc from using them.
  108.  
  109.     -iso
  110.     -ansi       Switch to ANSI/ISO mode.
  111.                 In ANSI mode warning 209 will be printed by default.
  112.                 '__reg' will not be recognized.
  113.                 Also assignments between pointers to type and pointers to
  114.                 unsigned type will cause warnings.
  115.  
  116.     -maxerrors=n
  117.                 Abort the compilation after n errors; do not stop if n==0.
  118.  
  119.     -dontwarn=n
  120.                 Suppress warning number n; suppress all warnings if n<0.
  121.                 (See the section on errors/warnings.)
  122.  
  123.     -warn=n
  124.                 Turn on warning number n; turn on all warnings if n<0.
  125.                 (See the section on errors/warnings.)
  126.  
  127.     -strip-path
  128.                 Strip the path of filenames in error messages if necessary.
  129.                 Error messages may look more convenient to some people that
  130.                 way but using this together with message browsers or
  131.                 similar programs could cause trouble.
  132.  
  133.     -nested-comments
  134.                 Allow nested comments (not ANSI conforming).
  135.                 Has no effect if the builtin preprocessor is disabled.
  136.  
  137.     -cpp-comments
  138.                 Allow C++ style comments (not ANSI conforming).
  139.                 Has no effect if the builtin preprocessor is disabled.
  140.  
  141.     -macro-redefinition
  142.                 Allow redefinition of macros (not ANSI conforming).
  143.                 Has no effect if the builtin preprocessor is disabled.
  144.  
  145.     -no-trigraphs
  146.                 Prevents expansion of trigraphs (not ANSI conforming).
  147.                 Has no effect if the builtin preprocessor is disabled.
  148.  
  149.     -no-preprocessor
  150.                 Do not invoke the builtin preprocessor vbpp.
  151.  
  152.     -E          Only preprocess the file and write the preprocessed
  153.                 source to <file>.i.
  154.  
  155.     -dontkeep-initialized-data
  156.                 By default vbcc keeps all data of initializations in memory
  157.                 during the whole compilation (it can sometimes make use
  158.                 of this when optimizing). This can take some amount of
  159.                 memory though. If this option is specified vbcc does not
  160.                 keep this data in memory and uses less memory.
  161.                 This has not been tested very well yet.
  162.  
  163.  
  164.     The assembler output will be saved to file.asm (if file already contained
  165.     a suffix, this will first be removed; same applies to .ic1/.ic2)
  166.  
  167.  
  168. SOME INTERNALS
  169.  
  170.     I try to make vbcc as ANSI compliant as possible (but this is not that
  171.     easy, because I do not have the ANSI document), so I am only mentioning
  172.     some things I consider interesting.
  173.  
  174.  
  175. ERRORS/WARNINGS
  176.  
  177.     vbcc knows the following kinds of messages:
  178.  
  179.     fatal errors        Something is badly wrong and further compilation is
  180.                         impossible or pointless. vbcc will abort.
  181.                         E.g. no source file or really corrupt source.
  182.  
  183.     errors              There was an error and vbcc cannot generate useful
  184.                         code. Compilation continues, but no code should be
  185.                         generated.
  186.                         E.g. unknown identifiers.
  187.  
  188.     warnings (1)        Warnings with ANSI-violations. The program is not
  189.                         ANSI-conforming, but vbcc will generate code that
  190.                         could be what you want (or not).
  191.                         E.g. missing semicolon.
  192.  
  193.     warnings (2)        The code has no ANSI-violations, but contains some
  194.                         strange things you should perhaps look at.
  195.                         E.g. unused variables.
  196.  
  197.     Errors or the first kind of warnings are always displayed and cannot
  198.     be suppressed.
  199.  
  200.     Only some warnings of the second kind are turned on by default.
  201.     Many of them are very useful for some, but annoying to others and
  202.     their usability may depend on programming style. As I do not want
  203.     to force anyone to a certain style I recommend everyone to find
  204.     their own preferences.
  205.  
  206.     A good way to do this is starting with all warnings turned on by
  207.     -warn=-1 so you will see all possible warnings. Now everytime you
  208.     get a warning you do not find useful, turn that one off with
  209.     -dontwarn=n.
  210.     The file errors.doc contains a list of all errors/warnings, sometimes
  211.     with more detailed descriptions. This might be very useful, too.
  212.  
  213.     See the docs on your frontend on how to configure it to your
  214.     preferences.
  215.  
  216.  
  217. DATA TYPES
  218.  
  219.     vbcc can handle the following atomic data types:
  220.  
  221.     signed/unsigned char/short/int/long  (signed is always default)
  222.     float/double  (long double is always the same as double)
  223.  
  224.     However several of them can be identical in certain implementations.
  225.  
  226.  
  227. OPTIMIZATIONS
  228.  
  229.     vbcc can compile with or without global optimizations.
  230.     But note that the optimizer is not yet finished and has not been
  231.     tested much. So only use it with care.
  232.  
  233.     In the first compilation phase every function is parsed into a tree
  234.     structure one expression after the other. Then type-checking and some
  235.     minor optimizations like constant-folding or some algebraic
  236.     simplifications are done on the trees.
  237.     This phase of the translation is identical in optimizing and
  238.     non-optimizing compilation.
  239.  
  240.     Then intermediate code is generated from the trees. In non-optimizing
  241.     compilation temporaries needed to evaluate the expression are
  242.     immediately assigned to registers if possible. In optimizing compilation
  243.     a new variable is generated for each temporary required.
  244.     Also for certain constructs like loops different intermediate code
  245.     is produced in optimizing compilation.
  246.     Some minor optimizations are performed while generating the intermediate
  247.     code (simple elimination of unreachable code, some optimizations on
  248.     branches etc.).
  249.  
  250.     After intermediate code for the whole function has been generated
  251.     simple register allocation may be done in non-optimizing compilation
  252.     if bit 1 has been set in the -O option.
  253.     After that the intermediate code is passed to the code generator and
  254.     then all memory for the function, its variables etc. is freed.
  255.  
  256.     In optimizing compilation flowgraphs are constructed, data flow analysis
  257.     is performed and many passes are made over the function's intermediate
  258.     code. Code may be moved around, new variables may be added, other
  259.     variables removed etc. etc. (for more detailed information on the
  260.     performed optimizations look at the description for the -O option
  261.     below).
  262.  
  263.     Many of the optimization routines depend on each other and if one
  264.     routine finds an optimization this often enables other routines to
  265.     find further ones and some routines only do a first step and let
  266.     other routines 'clean up' after it. Because of this vbcc usually
  267.     makes many passes until no further optimizations are found.
  268.     To avoid possible extremely long optimization times the number of
  269.     those passes can be limited with the -maxoptpasses=n option (the
  270.     default value is max. 10 passes).
  271.  
  272.     Now it will be decided if the compiled function is a candidate for
  273.     inlining. In this case the intermediate code as well as the data
  274.     structures for the local variables will be copied and stored until
  275.     compilation of the entire translation-unit has finished.
  276.  
  277.     After those phases register allocation should be done. As temporaries
  278.     have not been assigned to registers up to this point, register
  279.     allocation is crucial in optimizing compilation (actually if optimizing
  280.     is desired there are rarely reasons not to turn on all flags - also
  281.     note that some flags MUST be turned on or vbcc may even crash).
  282.  
  283.     Note that optimizing compilation can take MUCH more time and needs
  284.     MUCH more memory. It is hard to predict how much time and space it
  285.     needs, but usually it roughly depends on length of a function (time
  286.     and space needed will usually increase more than linear with the
  287.     length of a function).
  288.  
  289.  
  290.     At the moment the following bits in the -O option are recognized:
  291.  
  292.  
  293.     Bit 0 (1)           Register allocation
  294.  
  295.     This is the only flag that has any effect in non-optimizing compilation.
  296.  
  297.     In non-optimizing compilation any registers that have never been used
  298.     for temporaries in this function are used for register variables in a
  299.     simple way.
  300.     For each variable a priority to registerize it is computed (this has
  301.     already been done during generation of intermediate code). This value
  302.     usually reflects how much can be gained by putting it in a register.
  303.     Then for every free register the variable with the highest priority
  304.     that can be stored in that register is assigned that register for
  305.     the entire function.
  306.     This improves the generated code quite a bit.
  307.     The effect on compile time is hard to predict, but it should be very
  308.     small - this optimization may even speed up the compilation.
  309.  
  310.     In optimizing compilation several passes are made:
  311.  
  312.     - First all temporaries are assigned to registers in basic blocks.
  313.       Temporaries are recognized by utilising data flow information on
  314.       active variables and one variable can be a temporary at one or
  315.       several points although it is alive over several basic blocks at
  316.       another point.
  317.  
  318.     - Then vbcc computes approximate savings that can be obtained by
  319.       holding a variable in a register within a certain program region
  320.       (usually a loop) and assigns the most used variables to registers
  321.       within this region.
  322.       Information on the function's loop structure and active variables
  323.       are used.
  324.  
  325.  
  326.     Bit 1 (2)           activate optimizing compilation
  327.  
  328.     This flag turns on the optimizer. If it is set to zero no global
  329.     optimizations will be performed no matter what the other flags are set
  330.     to.
  331.     When turned on slightly different intermediate code will be generated
  332.     by the first translation phases.
  333.     Also the following optimizations are performed:
  334.  
  335.     - A flow graph is constructed and unused labels are deleted.
  336.  
  337.     - Unreachable code is eliminated.
  338.  
  339.     - Jump optimizations are performed.
  340.  
  341.     - Several peephole optimizations like constant folding and algebraic
  342.       simplifications are performed on the intermediate code.
  343.  
  344.     - Identical statements at the beginning/end of basic blocks are
  345.       moved to the successors/predecessors under certain conditions.
  346.  
  347.  
  348.     Bit 2 (4)           common subexpression elimination
  349.  
  350.     The intermediate code is scanned for common subexpressions that can be
  351.     eliminated. Also copy propagation is performed.
  352.     This can be done only within basic blocks or over the whole function
  353.     depending on bit 5.
  354.     If global cse is selected data flow analysis for available expressions
  355.     and available copies is performed.
  356.  
  357.     Note that the local versions of these optimizations are only restricted
  358.     versions of the global ones. They operate on the intermediate code
  359.     rather than on trees and therefore are slower than they could be
  360.     on compilers that only perform local versions.
  361.  
  362.  
  363.     Bit 3 (8)           constant propagation
  364.  
  365.     Variables which are known to have a constant value at one time are
  366.     replaced by constants.
  367.     This can be done only within basic blocks or over the whole function
  368.     depending on bit 5.
  369.     If global constant propagation is selected data flow analysis for
  370.     reaching definitions is performed.
  371.  
  372.     Note that the local versions of these optimizations are only restricted
  373.     versions of the global ones. They operate on the intermediate code
  374.     rather than on trees and therefore are slower than they could be
  375.     on compilers that only perform local versions.
  376.  
  377.  
  378.     Bit 4 (16)          elimination of dead code
  379.  
  380.     Code which computes a value that is never used will be eliminated.
  381.     Lots of dead code may be generated during the process of optimizing
  382.     so this optimizations is crucial.
  383.  
  384.  
  385.     Bit 5 (32)          global optimization
  386.  
  387.     Some optimizations are available in local and global versions. This
  388.     flag turns on the global versions.
  389.     At the moment this effects common subexpression elimination, copy
  390.     propagation, constant propagation and loop optimizations.
  391.  
  392.     Also if this flag is not turned on only one optimization pass is done
  393.     whereas several are done if it is turned on.
  394.  
  395.     Not turning on this flag results in worse code and often shorter
  396.     compile time. However there are cases where this increases compile
  397.     time, too.
  398.  
  399.  
  400.     Bit 6 (64)          reserved for future use
  401.  
  402.  
  403.     Bit 7 (128)         loop optimizations
  404.  
  405.     vbcc will try to identify loops and perform the following optimizations
  406.     on the loops it finds:
  407.  
  408.     - frequency-reduction: loop-invariant operations will be moved out of
  409.                            the loop
  410.  
  411.     - strength-reduction:  linear functions of induction variables will be
  412.                            replaced by additional induction variables
  413.  
  414.     These only work in conjunction with bit 5 (32).
  415.  
  416.  
  417.     Bit 8 (256)         merge variable space
  418.  
  419.     vbcc tries to place variables at the same memory addresses if possible.
  420.  
  421.  
  422.     Bit 9 (512)         reserved for future use
  423.  
  424.  
  425.     Bit 10 (1024)       move assignments out of loops
  426.  
  427.     If bits 5, 7 and 10 are set vbcc will try to move loop-invariant
  428.     assignments out of loops.
  429.  
  430.  
  431.     Bit 11 (2048)       loop-unrolling
  432.  
  433.     vbcc tries to unroll certain loops. Only works together with bit 5 (32)
  434.     and bit 7 (128). At the moment a loop is only unrolled if the number
  435.     of iterations can be determined at compile time. In the future loops
  436.     may also be unrolled if the number of iterations can be calculateed at
  437.     loop entry.
  438.     With -unroll-size you can specify how many intermediate instructions
  439.     the unrolled loop should have at most.
  440.  
  441.  
  442.     Bit 12 (4096)       function inlining
  443.  
  444.     The intermediate code of functions that meet certain conditions
  445.     (mainly adjustable by -inline-size) is kept in memory for the entire
  446.     translation unit and subsequent calls to this function are replaced
  447.     with this code.
  448.  
  449.     This way constant arguments can be propagated across the function
  450.     and certain parts of the function may be omitted. Also common
  451.     subexpressions across the functions can be eliminated.
  452.  
  453.     An inlined function call is about the same as a macro expansion (but
  454.     safer).
  455.  
  456.     Also look at #pragma only-inline in the following section.
  457.  
  458.  
  459.  
  460.     Also look at the documentation for the target dependant part of vbcc.
  461.     There may be additional machine specific optimization options.
  462.  
  463.  
  464. EXTENSIONS
  465.  
  466.     At the moment vbcc accepts the following #pragma-directives:
  467.  
  468.     #pragma printflike <function>   This tells vbcc to handle <function>
  469.     #pragma scanflike <function>    specially.
  470.                                     <function> must be an already declared
  471.                                     function with external linkage that
  472.                                     takes a variable number of arguments
  473.                                     and a const char * as the last fixed
  474.                                     parameter.
  475.                                     If such a function is called with a
  476.                                     string-constant as format-string vbcc
  477.                                     will check if the arguments seem to match
  478.                                     the format-specifiers in the
  479.                                     format-string according to the rules of
  480.                                     printf or scanf.
  481.                                     Also vbcc will replace the call by a
  482.                                     call to a simplified version according
  483.                                     to the following rules if such a function
  484.                                     has been declared with external linkage:
  485.  
  486.                                     If no format-specifiers are used at all
  487.                                     __v0<function> will be called.
  488.  
  489.                                     If no qualifiers are used and only
  490.                                     d,i,x,X,o,s,c are used __v1<function> will
  491.                                     be called.
  492.  
  493.                                     If no floating-point arguments are used
  494.                                     __v2<function> will be called.
  495.  
  496.  
  497.     #pragma only-inline on          The following functions are prepared for
  498.                                     inlining, but no code is generated. This
  499.                                     can be used e.g. in header-files to
  500.                                     supply inline versions of certain
  501.                                     functions.
  502.                                     -inline-size is ignored in this mode -
  503.                                     every function gets prepared for inlining.
  504.  
  505.                                     Do not use this with functions that have
  506.                                     local static variables!
  507.  
  508.     #pragma only-inline off         The following functions are translated
  509.                                     as usual again.
  510.  
  511.     #pragma opt <n>                 Sets the optimization options to <n>
  512.                                     (similar to -O=<n>) for the following
  513.                                     functions.
  514.                                     Never use this inside a function!
  515.  
  516.     #pragma type <expr>             Write the type of <expr> to stdout.
  517.                                     This is mainly intended for testing.
  518.  
  519.     #pragma tree <expr>             Write the parse-tree of <expr> to stdout.
  520.                                     This is mainly intended for testing.
  521.  
  522.     Register parameters:
  523.  
  524.     If the parameters for certain functions should be passed in certain
  525.     registers you can specify the registers with __reg("<reg>") in the
  526.     prototype, e.g.
  527.  
  528.         void f(__reg("d0") int x, __reg("a0") char *y) { ... }
  529.  
  530.     The names of the available registers depend on the code generator.
  531.     Note that a matching prototype must be in scope when calling such
  532.     a function or wrong code will be generated.
  533.     Therefore it is not useful to use register parameters in an old-style
  534.     function definition.
  535.  
  536.     If the code generator cannot handle the specified register for a
  537.     certain type this will cause an error. Note that this may happen
  538.     although the register could store that type if the code generator
  539.     does not know about it.
  540.  
  541.     Also note that this may force vbcc to create worse code.
  542.  
  543.     __reg is not recognized when ANSI/ISO mode is turned on.
  544.  
  545.  
  546. KNOWN PROBLEMS
  547.  
  548.     Some known target independant problems of vbcc at the moment:
  549.  
  550.     - Some size limits are still hardcoded into the program (the maximum
  551.       nesting of blocks and the maximum length of input lines).
  552.  
  553.     - Switch statements do never use jump-tables.
  554.  
  555.     - Bitfields are not supported (they are always used as int) - this should
  556.       be ANSI compliant, however.
  557.  
  558.     - The source was written with an optimizing compiler in mind and is
  559.       somewhat inefficient.
  560.  
  561.     - The source is not strictly ANSI conforming (i.e. fully portable), yet.
  562.       E.g. there may be some external identifiers with more than 6
  563.       identical characters (haven't checked yet) and other problems.
  564.       However in practice it is very portable.
  565.  
  566.     - Code for accessing volatile objects may not be generated if the result
  567.       is not used.
  568.  
  569.     - long double is not really supported (see errors.doc).
  570.  
  571.     - The optimizer is not finished and may have a few bugs.
  572.  
  573.  
  574. THE FUTURE
  575.  
  576.     - Bugfixing where necessary.
  577.  
  578.     - Completing the builtin preprocessor
  579.  
  580.     - Perhaps adding possibility to use precompiled header-files.
  581.  
  582.     - Finishing/improving the optimizer.
  583.  
  584.     - Certain features like register parameters.
  585.  
  586.     - Other code generators.
  587.  
  588.     - If you have any other ideas, tell me.
  589.  
  590.  
  591.  
  592. CREDITS
  593.  
  594.     All those who wrote parts of the vbcc distribution, made suggestions,
  595.     answered my questions, tested vbcc, reported errors or were otherwise
  596.     involved in the development of vbcc (in descending alphabetical order,
  597.     under work, not complete):
  598.  
  599.     Frank Wille
  600.     Johnny Tevessen
  601.     Ralph Schmidt
  602.     Markus Schmidinger
  603.     Thorsten Schaaps
  604.     Joerg Plate
  605.     Gunther Nikl
  606.     Robert Claus Mueller
  607.     Joern Maass
  608.     Aki M Laukkanen
  609.     Kai Kohlmorgen
  610.     Dirk Holtwick
  611.     Kasper Graversen
  612.     Volker Graf
  613.     Matthias Fleischer
  614.     Alexander Fichtner
  615.     Robert Ennals
  616.     Thomas Dorn
  617.     Walter Doerwald
  618.     Aaron Digulla
  619.     Lars Dannenberg
  620.     Michael Bode
  621.     Michael Bauer
  622.     Juergen Barthelmann
  623.     Thomas Arnhold
  624.     Thomas Aglassinger
  625.  
  626.  
  627. Volker Barthelmann                                      volker@vb.franken.de
  628. Kennedy-Ring 39
  629. 91301 Forchheim
  630. Germany
  631.  
  632.