LCOV - code coverage report
Current view: top level - lib - optimizer.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 7 100.0 %
Date: 2014-11-22 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* optimizer.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "as2js/optimizer.h"
      37             : #include    "as2js/message.h"
      38             : #include    "optimizer_tables.h"
      39             : 
      40             : 
      41             : namespace as2js
      42             : {
      43             : namespace Optimizer
      44             : {
      45             : 
      46             : 
      47             : /** \brief The as2js optimizer.
      48             :  *
      49             :  * This function goes through all the available optimizations and
      50             :  * processes them whenever they apply to your code.
      51             :  *
      52             :  * Errors may be generated whenever a problem is found.
      53             :  *
      54             :  * Also some potential errors such as a division or modulo by
      55             :  * zero can legally occur in your input program so in that case the
      56             :  * optimizer generates a warning to let you know that such a division
      57             :  * was found, but no error to speak of.
      58             :  *
      59             :  * The function reports the total number of errors that were generated
      60             :  * while optimizing.
      61             :  *
      62             :  * At any point after parsing, the program can be passed through
      63             :  * the optimizer. This means removing all the possible expressions and
      64             :  * statements which can be removed to make the code smaller in the end.
      65             :  * The optimizations applied can be tweaked using options ('use ...;').
      66             :  *
      67             :  * In most cases the compiler already takes care of calling the optimizer
      68             :  * at appropriate times. Since it is a static function, it can directly
      69             :  * be called as in:
      70             :  *
      71             :  * \code
      72             :  *    Optimizer::Optimize(root);
      73             :  * \endcode
      74             :  *
      75             :  * Where root is a Node representing the root of the optimization (anything
      76             :  * outside of the root does not get optimized.)
      77             :  *
      78             :  * The optimize() function tries to remove all possible expressions
      79             :  * and statements which will have no effect in the final output
      80             :  * (by default, certain things such as x + 0, may not be removed since
      81             :  * such may have an effect... if x is a string, then x + 0 concatenates
      82             :  * zero to that string.)
      83             :  *
      84             :  * The root parameter may be what was returned by the Parser::parse()
      85             :  * function of the. However, in most cases, the compiler only optimizes
      86             :  * part of the tree as required (because many parts cannot be optimized
      87             :  * and it will make things generally faster.)
      88             :  *
      89             :  * The optimizations are organized in C++ tables that get linked
      90             :  * in the compiler as read-only static data. These are organized
      91             :  * in many separate files because of the large amount of possible
      92             :  * optimizations. There is a list of the various files in the
      93             :  * optimizer:
      94             :  *
      95             :  * \li optimizer.cpp -- the main Optimizer object implementation;
      96             :  * all the other files are considered private.
      97             :  *
      98             :  * \li optimizer_matches.ci and optimizer_matches.cpp -- the tables
      99             :  * (.ci) and the functions (.cpp) used to match a tree of nodes
     100             :  * and thus determine whether an optimization can be applied or not.
     101             :  *
     102             :  * \li optimizer_tables.cpp and optimizer_tables.h -- the top level
     103             :  * tables of the optimizer. These are used to search for optimizations
     104             :  * that can be applied against your tree of nodes. The header defines
     105             :  * private classes, structures, etc.
     106             :  *
     107             :  * \li optimizer_values.ci -- a set of tables representing literal
     108             :  * values as in some cases an optimization can be applied if a
     109             :  * literal has a specific value.
     110             :  *
     111             :  * \li optimizer_optimize.ci and optimizer_optimize.cpp -- a set
     112             :  * of optimizations defined using tables and corresponding functions
     113             :  * to actually apply the optimizations to a tree of nodes.
     114             :  *
     115             :  * \li optimizer_additive.ci -- optimizations for '+' and '-', including
     116             :  * string concatenations.
     117             :  *
     118             :  * \li optimizer_assignments.ci -- optimizations for all assignments, '=',
     119             :  * '+=', '-=', '*=', etc.
     120             :  *
     121             :  * \li optimizer_bitwise.ci -- optimizations for '~', '&', '|', '^', '<<', '>>',
     122             :  * '>>>', '<!', and '>!'.
     123             :  *
     124             :  * \li optimizer_compare.ci -- optimizations for '<=>'.
     125             :  *
     126             :  * \li optimizer_conditional.ci -- optimizations for 'a ? b : c'.
     127             :  *
     128             :  * \li optimizer_equality.ci -- optimizations for '==', '!=', '===', '!==',
     129             :  * and '~~'.
     130             :  *
     131             :  * \li optimizer_logical.ci -- optimizations for '!', '&&', '||', and '^^'.
     132             :  *
     133             :  * \li optimizer_match.ci -- optimizations for '~=' and '!~'.
     134             :  *
     135             :  * \li optimizer_multiplicative.ci -- optimizations for '*', '/', '%',
     136             :  * and '**'.
     137             :  *
     138             :  * \li optimizer_relational.ci -- optimizations for '<', '<=', '>',
     139             :  * and '>='.
     140             :  *
     141             :  * \li optimizer_statments.ci -- optimizations for 'if', 'while', 'do',
     142             :  * and "directives" (blocks).
     143             :  *
     144             :  * \important
     145             :  * It is important to note that this function is not unlikely going
     146             :  * to modify your tree (even if you do not think there is a possible
     147             :  * optimization). This means the caller should not expect the node to
     148             :  * still be the same pointer and possibly not at the same location in
     149             :  * the parent node (many nodes get deleted.)
     150             :  *
     151             :  * \param[in] node  The node to optimize.
     152             :  *
     153             :  * \return The number of errors generated while optimizing.
     154             :  */
     155         425 : int optimize(Node::pointer_t& node)
     156             : {
     157         425 :     int const errcnt(Message::error_count());
     158             : 
     159         426 :     optimizer_details::optimize_tree(node);
     160             : 
     161             :     // This may not be at the right place because the caller may be
     162             :     // looping through a list of children too... (although we have
     163             :     // an important not in the documentation... that does not mean
     164             :     // much, does it?)
     165         424 :     if(node)
     166             :     {
     167         423 :         node->clean_tree();
     168             :     }
     169             : 
     170         424 :     return Message::error_count() - errcnt;
     171             : }
     172             : 
     173             : 
     174             : 
     175             : }
     176             : // namespace Optimizer
     177          63 : }
     178             : // namespace as2js
     179             : 
     180             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10