LCOV - code coverage report
Current view: directory - js/src/frontend - BytecodeEmitter.h (source / functions) Found Hit Coverage
Test: app.info Lines: 148 147 99.3 %
Date: 2012-06-02 Functions: 62 62 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  * vim: set ts=8 sw=4 et tw=79:
       3                 :  *
       4                 :  * ***** BEGIN LICENSE BLOCK *****
       5                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       6                 :  *
       7                 :  * The contents of this file are subject to the Mozilla Public License Version
       8                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       9                 :  * the License. You may obtain a copy of the License at
      10                 :  * http://www.mozilla.org/MPL/
      11                 :  *
      12                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      13                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      14                 :  * for the specific language governing rights and limitations under the
      15                 :  * License.
      16                 :  *
      17                 :  * The Original Code is Mozilla Communicator client code, released
      18                 :  * March 31, 1998.
      19                 :  *
      20                 :  * The Initial Developer of the Original Code is
      21                 :  * Netscape Communications Corporation.
      22                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      23                 :  * the Initial Developer. All Rights Reserved.
      24                 :  *
      25                 :  * Contributor(s):
      26                 :  *
      27                 :  * Alternatively, the contents of this file may be used under the terms of
      28                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      29                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      31                 :  * of those above. If you wish to allow use of your version of this file only
      32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      33                 :  * use your version of this file under the terms of the MPL, indicate your
      34                 :  * decision by deleting the provisions above and replace them with the notice
      35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      36                 :  * the provisions above, a recipient may use your version of this file under
      37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      38                 :  *
      39                 :  * ***** END LICENSE BLOCK ***** */
      40                 : 
      41                 : #ifndef BytecodeEmitter_h__
      42                 : #define BytecodeEmitter_h__
      43                 : 
      44                 : /*
      45                 :  * JS bytecode generation.
      46                 :  */
      47                 : #include "jstypes.h"
      48                 : #include "jsatom.h"
      49                 : #include "jsopcode.h"
      50                 : #include "jsscript.h"
      51                 : #include "jsprvtd.h"
      52                 : #include "jspubtd.h"
      53                 : 
      54                 : #include "frontend/Parser.h"
      55                 : #include "frontend/ParseMaps.h"
      56                 : 
      57                 : namespace js {
      58                 : 
      59                 : /*
      60                 :  * NB: If you add enumerators for scope statements, add them between STMT_WITH
      61                 :  * and STMT_CATCH, or you will break the STMT_TYPE_IS_SCOPE macro. If you add
      62                 :  * non-looping statement enumerators, add them before STMT_DO_LOOP or you will
      63                 :  * break the STMT_TYPE_IS_LOOP macro.
      64                 :  *
      65                 :  * Also remember to keep the statementName array in BytecodeEmitter.cpp in
      66                 :  * sync.
      67                 :  */
      68                 : enum StmtType {
      69                 :     STMT_LABEL,                 /* labeled statement:  L: s */
      70                 :     STMT_IF,                    /* if (then) statement */
      71                 :     STMT_ELSE,                  /* else clause of if statement */
      72                 :     STMT_SEQ,                   /* synthetic sequence of statements */
      73                 :     STMT_BLOCK,                 /* compound statement: { s1[;... sN] } */
      74                 :     STMT_SWITCH,                /* switch statement */
      75                 :     STMT_WITH,                  /* with statement */
      76                 :     STMT_CATCH,                 /* catch block */
      77                 :     STMT_TRY,                   /* try block */
      78                 :     STMT_FINALLY,               /* finally block */
      79                 :     STMT_SUBROUTINE,            /* gosub-target subroutine body */
      80                 :     STMT_DO_LOOP,               /* do/while loop statement */
      81                 :     STMT_FOR_LOOP,              /* for loop statement */
      82                 :     STMT_FOR_IN_LOOP,           /* for/in loop statement */
      83                 :     STMT_WHILE_LOOP,            /* while loop statement */
      84                 :     STMT_LIMIT
      85                 : };
      86                 : 
      87                 : inline bool
      88        20242961 : STMT_TYPE_IN_RANGE(uint16_t type, StmtType begin, StmtType end)
      89                 : {
      90        20242961 :     return begin <= type && type <= end;
      91                 : }
      92                 : 
      93                 : /*
      94                 :  * A comment on the encoding of the js::StmtType enum and type-testing macros:
      95                 :  *
      96                 :  * STMT_TYPE_MAYBE_SCOPE tells whether a statement type is always, or may
      97                 :  * become, a lexical scope.  It therefore includes block and switch (the two
      98                 :  * low-numbered "maybe" scope types) and excludes with (with has dynamic scope
      99                 :  * pending the "reformed with" in ES4/JS2).  It includes all try-catch-finally
     100                 :  * types, which are high-numbered maybe-scope types.
     101                 :  *
     102                 :  * STMT_TYPE_LINKS_SCOPE tells whether a js::StmtInfo of the given type eagerly
     103                 :  * links to other scoping statement info records.  It excludes the two early
     104                 :  * "maybe" types, block and switch, as well as the try and both finally types,
     105                 :  * since try and the other trailing maybe-scope types don't need block scope
     106                 :  * unless they contain let declarations.
     107                 :  *
     108                 :  * We treat WITH as a static scope because it prevents lexical binding from
     109                 :  * continuing further up the static scope chain. With the lost "reformed with"
     110                 :  * proposal for ES4, we would be able to model it statically, too.
     111                 :  */
     112                 : #define STMT_TYPE_MAYBE_SCOPE(type)                                           \
     113                 :     (type != STMT_WITH &&                                                     \
     114                 :      STMT_TYPE_IN_RANGE(type, STMT_BLOCK, STMT_SUBROUTINE))
     115                 : 
     116                 : #define STMT_TYPE_LINKS_SCOPE(type)                                           \
     117                 :     STMT_TYPE_IN_RANGE(type, STMT_WITH, STMT_CATCH)
     118                 : 
     119                 : #define STMT_TYPE_IS_TRYING(type)                                             \
     120                 :     STMT_TYPE_IN_RANGE(type, STMT_TRY, STMT_SUBROUTINE)
     121                 : 
     122                 : #define STMT_TYPE_IS_LOOP(type) ((type) >= STMT_DO_LOOP)
     123                 : 
     124                 : #define STMT_MAYBE_SCOPE(stmt)  STMT_TYPE_MAYBE_SCOPE((stmt)->type)
     125                 : #define STMT_LINKS_SCOPE(stmt)  (STMT_TYPE_LINKS_SCOPE((stmt)->type) ||       \
     126                 :                                  ((stmt)->flags & SIF_SCOPE))
     127                 : #define STMT_IS_TRYING(stmt)    STMT_TYPE_IS_TRYING((stmt)->type)
     128                 : #define STMT_IS_LOOP(stmt)      STMT_TYPE_IS_LOOP((stmt)->type)
     129                 : 
     130                 : struct StmtInfo {
     131                 :     uint16_t        type;           /* statement type */
     132                 :     uint16_t        flags;          /* flags, see below */
     133                 :     uint32_t        blockid;        /* for simplified dominance computation */
     134                 :     ptrdiff_t       update;         /* loop update offset (top if none) */
     135                 :     ptrdiff_t       breaks;         /* offset of last break in loop */
     136                 :     ptrdiff_t       continues;      /* offset of last continue in loop */
     137                 :     union {
     138                 :         JSAtom      *label;         /* name of LABEL */
     139                 :         StaticBlockObject *blockObj;/* block scope object */
     140                 :     };
     141                 :     StmtInfo        *down;          /* info for enclosing statement */
     142                 :     StmtInfo        *downScope;     /* next enclosing lexical scope */
     143                 : };
     144                 : 
     145                 : #define SIF_SCOPE        0x0001     /* statement has its own lexical scope */
     146                 : #define SIF_BODY_BLOCK   0x0002     /* STMT_BLOCK type is a function body */
     147                 : #define SIF_FOR_BLOCK    0x0004     /* for (let ...) induced block scope */
     148                 : 
     149                 : /*
     150                 :  * To reuse space in StmtInfo, rename breaks and continues for use during
     151                 :  * try/catch/finally code generation and backpatching. To match most common
     152                 :  * use cases, the macro argument is a struct, not a struct pointer. Only a
     153                 :  * loop, switch, or label statement info record can have breaks and continues,
     154                 :  * and only a for loop has an update backpatch chain, so it's safe to overlay
     155                 :  * these for the "trying" StmtTypes.
     156                 :  */
     157                 : #define CATCHNOTE(stmt)  ((stmt).update)
     158                 : #define GOSUBS(stmt)     ((stmt).breaks)
     159                 : #define GUARDJUMP(stmt)  ((stmt).continues)
     160                 : 
     161                 : #define SET_STATEMENT_TOP(stmt, top)                                          \
     162                 :     ((stmt)->update = (top), (stmt)->breaks = (stmt)->continues = (-1))
     163                 : 
     164                 : #define TCF_COMPILING           0x01 /* TreeContext is BytecodeEmitter */
     165                 : #define TCF_IN_FUNCTION         0x02 /* parsing inside function body */
     166                 : #define TCF_RETURN_EXPR         0x04 /* function has 'return expr;' */
     167                 : #define TCF_RETURN_VOID         0x08 /* function has 'return;' */
     168                 : #define TCF_IN_FOR_INIT         0x10 /* parsing init expr of for; exclude 'in' */
     169                 : #define TCF_FUN_SETS_OUTER_NAME 0x20 /* function set outer name (lexical or free) */
     170                 : #define TCF_FUN_PARAM_ARGUMENTS 0x40 /* function has parameter named arguments */
     171                 : #define TCF_FUN_LOCAL_ARGUMENTS 0x80 /* function may contain a local named arguments */
     172                 : #define TCF_FUN_USES_ARGUMENTS 0x100 /* function uses arguments except as a
     173                 :                                         parameter name */
     174                 : #define TCF_FUN_HEAVYWEIGHT    0x200 /* function needs Call object per call */
     175                 : #define TCF_FUN_IS_GENERATOR   0x400 /* parsed yield statement in function */
     176                 : #define TCF_FUN_USES_OWN_NAME  0x800 /* named function expression that uses its
     177                 :                                         own name */
     178                 : #define TCF_HAS_FUNCTION_STMT 0x1000 /* block contains a function statement */
     179                 : #define TCF_GENEXP_LAMBDA     0x2000 /* flag lambda from generator expression */
     180                 : #define TCF_COMPILE_N_GO      0x4000 /* compile-and-go mode of script, can
     181                 :                                         optimize name references based on scope
     182                 :                                         chain */
     183                 : #define TCF_NO_SCRIPT_RVAL    0x8000 /* API caller does not want result value
     184                 :                                         from global script */
     185                 : /*
     186                 :  * Set when parsing a declaration-like destructuring pattern.  This
     187                 :  * flag causes PrimaryExpr to create PN_NAME parse nodes for variable
     188                 :  * references which are not hooked into any definition's use chain,
     189                 :  * added to any tree context's AtomList, etc. etc.  CheckDestructuring
     190                 :  * will do that work later.
     191                 :  *
     192                 :  * The comments atop CheckDestructuring explain the distinction
     193                 :  * between assignment-like and declaration-like destructuring
     194                 :  * patterns, and why they need to be treated differently.
     195                 :  */
     196                 : #define TCF_DECL_DESTRUCTURING  0x10000
     197                 : 
     198                 : /*
     199                 :  * This function/global/eval code body contained a Use Strict Directive. Treat
     200                 :  * certain strict warnings as errors, and forbid the use of 'with'. See also
     201                 :  * TSF_STRICT_MODE_CODE, JSScript::strictModeCode, and JSREPORT_STRICT_ERROR.
     202                 :  */
     203                 : #define TCF_STRICT_MODE_CODE    0x20000
     204                 : 
     205                 : /* bits 0x40000 and 0x80000 are unused */
     206                 : 
     207                 : /*
     208                 :  * "Module pattern", i.e., a lambda that is immediately applied and the whole
     209                 :  * of an expression statement.
     210                 :  */
     211                 : #define TCF_FUN_MODULE_PATTERN 0x200000
     212                 : 
     213                 : /*
     214                 :  * Flag to prevent a non-escaping function from being optimized into a null
     215                 :  * closure (i.e., a closure that needs only its global object for free variable
     216                 :  * resolution), because this function contains a closure that needs one or more
     217                 :  * scope objects surrounding it (i.e., a Call object for an outer heavyweight
     218                 :  * function). See bug 560234.
     219                 :  */
     220                 : #define TCF_FUN_ENTRAINS_SCOPES 0x400000
     221                 : 
     222                 : /* The function calls 'eval'. */
     223                 : #define TCF_FUN_CALLS_EVAL       0x800000
     224                 : 
     225                 : /* The function mutates a positional (non-destructuring) parameter. */
     226                 : #define TCF_FUN_MUTATES_PARAMETER 0x1000000
     227                 : 
     228                 : /*
     229                 :  * Compiling an eval() script.
     230                 :  */
     231                 : #define TCF_COMPILE_FOR_EVAL     0x2000000
     232                 : 
     233                 : /*
     234                 :  * The function or a function that encloses it may define new local names
     235                 :  * at runtime through means other than calling eval.
     236                 :  */
     237                 : #define TCF_FUN_MIGHT_ALIAS_LOCALS  0x4000000
     238                 : 
     239                 : /*
     240                 :  * The script contains singleton initialiser JSOP_OBJECT.
     241                 :  */
     242                 : #define TCF_HAS_SINGLETONS       0x8000000
     243                 : 
     244                 : /*
     245                 :  * Some enclosing scope is a with-statement or E4X filter-expression.
     246                 :  */
     247                 : #define TCF_IN_WITH             0x10000000
     248                 : 
     249                 : /*
     250                 :  * This function does something that can extend the set of bindings in its
     251                 :  * call objects --- it does a direct eval in non-strict code, or includes a
     252                 :  * function statement (as opposed to a function definition).
     253                 :  *
     254                 :  * This flag is *not* inherited by enclosed or enclosing functions; it
     255                 :  * applies only to the function in whose flags it appears.
     256                 :  */
     257                 : #define TCF_FUN_EXTENSIBLE_SCOPE 0x20000000
     258                 : 
     259                 : /*
     260                 :  * The caller is JS_Compile*Script*.
     261                 :  */
     262                 : #define TCF_NEED_SCRIPT_GLOBAL 0x40000000
     263                 : 
     264                 : /*
     265                 :  * Flags to check for return; vs. return expr; in a function.
     266                 :  */
     267                 : #define TCF_RETURN_FLAGS        (TCF_RETURN_EXPR | TCF_RETURN_VOID)
     268                 : 
     269                 : /*
     270                 :  * Sticky deoptimization flags to propagate from FunctionBody.
     271                 :  */
     272                 : #define TCF_FUN_FLAGS           (TCF_FUN_SETS_OUTER_NAME |                    \
     273                 :                                  TCF_FUN_USES_ARGUMENTS  |                    \
     274                 :                                  TCF_FUN_PARAM_ARGUMENTS |                    \
     275                 :                                  TCF_FUN_LOCAL_ARGUMENTS |                    \
     276                 :                                  TCF_FUN_HEAVYWEIGHT     |                    \
     277                 :                                  TCF_FUN_IS_GENERATOR    |                    \
     278                 :                                  TCF_FUN_USES_OWN_NAME   |                    \
     279                 :                                  TCF_FUN_CALLS_EVAL      |                    \
     280                 :                                  TCF_FUN_MIGHT_ALIAS_LOCALS |                 \
     281                 :                                  TCF_FUN_MUTATES_PARAMETER |                  \
     282                 :                                  TCF_STRICT_MODE_CODE    |                    \
     283                 :                                  TCF_FUN_EXTENSIBLE_SCOPE)
     284                 : 
     285                 : struct BytecodeEmitter;
     286                 : 
     287                 : struct TreeContext {                /* tree context for semantic checks */
     288                 :     uint32_t        flags;          /* statement state flags, see above */
     289                 :     uint32_t        bodyid;         /* block number of program/function body */
     290                 :     uint32_t        blockidGen;     /* preincremented block number generator */
     291                 :     uint32_t        parenDepth;     /* nesting depth of parens that might turn out
     292                 :                                        to be generator expressions */
     293                 :     uint32_t        yieldCount;     /* number of |yield| tokens encountered at
     294                 :                                        non-zero depth in current paren tree */
     295                 :     uint32_t        argumentsCount; /* number of |arguments| references encountered
     296                 :                                        at non-zero depth in current paren tree */
     297                 :     StmtInfo        *topStmt;       /* top of statement info stack */
     298                 :     StmtInfo        *topScopeStmt;  /* top lexical scope statement */
     299                 :     StaticBlockObject *blockChain;  /* compile block scope chain (NB: one
     300                 :                                        deeper than the topScopeStmt/downScope
     301                 :                                        chain when in head of let block/expr) */
     302                 :     ParseNode       *blockNode;     /* parse node for a block with let declarations
     303                 :                                        (block with its own lexical scope)  */
     304                 :     AtomDecls       decls;          /* function, const, and var declarations */
     305                 :     Parser          *parser;        /* ptr to common parsing and lexing data */
     306                 :     ParseNode       *yieldNode;     /* parse node for a yield expression that might
     307                 :                                        be an error if we turn out to be inside a
     308                 :                                        generator expression */
     309                 :     ParseNode       *argumentsNode; /* parse node for an arguments variable that
     310                 :                                        might be an error if we turn out to be
     311                 :                                        inside a generator expression */
     312                 : 
     313                 :   private:
     314                 :     union {
     315                 :         JSFunction  *fun_;          /* function to store argument and variable
     316                 :                                        names when flags & TCF_IN_FUNCTION */
     317                 :         JSObject    *scopeChain_;   /* scope chain object for the script */
     318                 :     };
     319                 : 
     320                 :   public:
     321         3279049 :     JSFunction *fun() const {
     322         3279049 :         JS_ASSERT(inFunction());
     323         3279049 :         return fun_;
     324                 :     }
     325         1969640 :     void setFunction(JSFunction *fun) {
     326         1969640 :         JS_ASSERT(inFunction());
     327         1969640 :         fun_ = fun;
     328         1969640 :     }
     329         1730508 :     JSObject *scopeChain() const {
     330         1730508 :         JS_ASSERT(!inFunction());
     331         1730508 :         return scopeChain_;
     332                 :     }
     333          123858 :     void setScopeChain(JSObject *scopeChain) {
     334          123858 :         JS_ASSERT(!inFunction());
     335          123858 :         scopeChain_ = scopeChain;
     336          123858 :     }
     337                 : 
     338                 :     OwnedAtomDefnMapPtr lexdeps;    /* unresolved lexical name dependencies */
     339                 :     TreeContext     *parent;        /* enclosing function or global context */
     340                 :     unsigned           staticLevel;    /* static compilation unit nesting level */
     341                 : 
     342                 :     FunctionBox     *funbox;        /* null or box for function we're compiling
     343                 :                                        if (flags & TCF_IN_FUNCTION) and not in
     344                 :                                        js::frontend::CompileFunctionBody */
     345                 :     FunctionBox     *functionList;
     346                 : 
     347                 :     ParseNode       *innermostWith; /* innermost WITH parse node */
     348                 : 
     349                 :     Bindings        bindings;       /* bindings in this code, including
     350                 :                                        arguments if we're compiling a function */
     351                 :     Bindings::StackRoot bindingsRoot; /* root for stack allocated bindings. */
     352                 : 
     353                 :     void trace(JSTracer *trc);
     354                 : 
     355                 :     inline TreeContext(Parser *prs);
     356                 :     inline ~TreeContext();
     357                 : 
     358                 :     /*
     359                 :      * js::BytecodeEmitter derives from js::TreeContext; however, only the
     360                 :      * top-level BytecodeEmitters are actually used as full-fledged tree contexts
     361                 :      * (to hold decls and lexdeps). We can avoid allocation overhead by making
     362                 :      * this distinction explicit.
     363                 :      */
     364                 :     enum InitBehavior {
     365                 :         USED_AS_TREE_CONTEXT,
     366                 :         USED_AS_CODE_GENERATOR
     367                 :     };
     368                 : 
     369         2093498 :     bool init(JSContext *cx, InitBehavior ib = USED_AS_TREE_CONTEXT) {
     370         2093498 :         if (ib == USED_AS_CODE_GENERATOR)
     371          978344 :             return true;
     372         1115154 :         return decls.init() && lexdeps.ensureMap(cx);
     373                 :     }
     374                 : 
     375        30942004 :     unsigned blockid() { return topStmt ? topStmt->blockid : bodyid; }
     376                 : 
     377                 :     /*
     378                 :      * True if we are at the topmost level of a entire script or function body.
     379                 :      * For example, while parsing this code we would encounter f1 and f2 at
     380                 :      * body level, but we would not encounter f3 or f4 at body level:
     381                 :      *
     382                 :      *   function f1() { function f2() { } }
     383                 :      *   if (cond) { function f3() { if (cond) { function f4() { } } } }
     384                 :      */
     385         4099213 :     bool atBodyLevel() { return !topStmt || (topStmt->flags & SIF_BODY_BLOCK); }
     386                 : 
     387                 :     /* Test whether we're in a statement of given type. */
     388                 :     bool inStatement(StmtType type);
     389                 : 
     390        29748097 :     bool inStrictMode() const {
     391        29748097 :         return flags & TCF_STRICT_MODE_CODE;
     392                 :     }
     393                 : 
     394                 :     inline bool needStrictChecks();
     395                 : 
     396                 :     // Return true there is a generator function within |skip| lexical scopes
     397                 :     // (going upward) from this context's lexical scope. Always return true if
     398                 :     // this context is itself a generator.
     399                 :     bool skipSpansGenerator(unsigned skip);
     400                 : 
     401        14493901 :     bool compileAndGo() const { return flags & TCF_COMPILE_N_GO; }
     402        38131046 :     bool inFunction() const { return flags & TCF_IN_FUNCTION; }
     403                 : 
     404          975604 :     bool compiling() const { return flags & TCF_COMPILING; }
     405                 :     inline BytecodeEmitter *asBytecodeEmitter();
     406                 : 
     407          361889 :     bool usesArguments() const {
     408          361889 :         return flags & TCF_FUN_USES_ARGUMENTS;
     409                 :     }
     410                 : 
     411            9851 :     void noteCallsEval() {
     412            9851 :         flags |= TCF_FUN_CALLS_EVAL;
     413            9851 :     }
     414                 : 
     415         6992217 :     bool callsEval() const {
     416         6992217 :         return flags & TCF_FUN_CALLS_EVAL;
     417                 :     }
     418                 : 
     419            2724 :     void noteMightAliasLocals() {
     420            2724 :         flags |= TCF_FUN_MIGHT_ALIAS_LOCALS;
     421            2724 :     }
     422                 : 
     423         1919520 :     bool mightAliasLocals() const {
     424         1919520 :         return flags & TCF_FUN_MIGHT_ALIAS_LOCALS;
     425                 :     }
     426                 : 
     427                 :     void noteParameterMutation() {
     428                 :         JS_ASSERT(inFunction());
     429                 :         flags |= TCF_FUN_MUTATES_PARAMETER;
     430                 :     }
     431                 : 
     432            3332 :     bool mutatesParameter() const {
     433            3332 :         JS_ASSERT(inFunction());
     434            3332 :         return flags & TCF_FUN_MUTATES_PARAMETER;
     435                 :     }
     436                 : 
     437           27712 :     bool mayOverwriteArguments() const {
     438           27712 :         JS_ASSERT(inFunction());
     439           27712 :         JS_ASSERT_IF(inStrictMode(),
     440           27712 :                      !(flags & (TCF_FUN_PARAM_ARGUMENTS | TCF_FUN_LOCAL_ARGUMENTS)));
     441           27712 :         return !inStrictMode() &&
     442           20657 :                (callsEval() ||
     443           48369 :                 flags & (TCF_FUN_PARAM_ARGUMENTS | TCF_FUN_LOCAL_ARGUMENTS));
     444                 :     }
     445                 : 
     446           13865 :     void noteArgumentsNameUse(ParseNode *node) {
     447           13865 :         JS_ASSERT(inFunction());
     448           13865 :         JS_ASSERT(node->isKind(PNK_NAME));
     449           13865 :         JS_ASSERT(node->pn_atom == parser->context->runtime->atomState.argumentsAtom);
     450           13865 :         countArgumentsUse(node);
     451           13865 :         flags |= TCF_FUN_USES_ARGUMENTS;
     452           13865 :         if (funbox)
     453           12965 :             funbox->node->pn_dflags |= PND_FUNARG;
     454           13865 :     }
     455                 : 
     456                 :     /*
     457                 :      * Uses of |arguments| must be noted so that such uses can be forbidden if
     458                 :      * they occur inside generator expressions (which desugar to functions and
     459                 :      * yields, in which |arguments| would have an entirely different meaning).
     460                 :      */
     461           14316 :     void countArgumentsUse(ParseNode *node) {
     462           14316 :         JS_ASSERT(node->isKind(PNK_NAME));
     463           14316 :         JS_ASSERT(node->pn_atom == parser->context->runtime->atomState.argumentsAtom);
     464           14316 :         argumentsCount++;
     465           14316 :         argumentsNode = node;
     466           14316 :     }
     467                 : 
     468          988449 :     bool needsEagerArguments() const {
     469          988449 :         return inStrictMode() && ((usesArguments() && mutatesParameter()) || callsEval());
     470                 :     }
     471                 : 
     472            7687 :     void noteHasExtensibleScope() {
     473            7687 :         flags |= TCF_FUN_EXTENSIBLE_SCOPE;
     474            7687 :     }
     475                 : 
     476                 :     bool hasExtensibleScope() const {
     477                 :         return flags & TCF_FUN_EXTENSIBLE_SCOPE;
     478                 :     }
     479                 : 
     480         1099100 :     ParseNode *freeTree(ParseNode *pn) { return parser->freeTree(pn); }
     481                 : };
     482                 : 
     483                 : /*
     484                 :  * Return true if we need to check for conditions that elicit
     485                 :  * JSOPTION_STRICT warnings or strict mode errors.
     486                 :  */
     487         5761162 : inline bool TreeContext::needStrictChecks() {
     488         5761162 :     return parser->context->hasStrictOption() || inStrictMode();
     489                 : }
     490                 : 
     491                 : namespace frontend {
     492                 : 
     493                 : bool
     494                 : SetStaticLevel(TreeContext *tc, unsigned staticLevel);
     495                 : 
     496                 : bool
     497                 : GenerateBlockId(TreeContext *tc, uint32_t &blockid);
     498                 : 
     499                 : } /* namespace frontend */
     500                 : 
     501                 : struct TryNode {
     502                 :     JSTryNote       note;
     503                 :     TryNode       *prev;
     504                 : };
     505                 : 
     506                 : struct CGObjectList {
     507                 :     uint32_t            length;     /* number of emitted so far objects */
     508                 :     ObjectBox           *lastbox;   /* last emitted object */
     509                 : 
     510         2219428 :     CGObjectList() : length(0), lastbox(NULL) {}
     511                 : 
     512                 :     unsigned index(ObjectBox *objbox);
     513                 :     void finish(JSObjectArray *array);
     514                 : };
     515                 : 
     516         1109714 : class GCConstList {
     517                 :     Vector<Value> list;
     518                 :   public:
     519         1109714 :     GCConstList(JSContext *cx) : list(cx) {}
     520           55302 :     bool append(Value v) { return list.append(v); }
     521         2282294 :     size_t length() const { return list.length(); }
     522                 :     void finish(JSConstArray *array);
     523                 : };
     524                 : 
     525          120957 : struct GlobalScope {
     526          120957 :     GlobalScope(JSContext *cx, JSObject *globalObj, BytecodeEmitter *bce)
     527          120957 :       : globalObj(globalObj), bce(bce), defs(cx), names(cx)
     528          120957 :     { }
     529                 : 
     530           76581 :     struct GlobalDef {
     531                 :         JSAtom        *atom;        // If non-NULL, specifies the property name to add.
     532                 :         FunctionBox   *funbox;      // If non-NULL, function value for the property.
     533                 :                                     // This value is only set/used if atom is non-NULL.
     534                 :         uint32_t      knownSlot;    // If atom is NULL, this is the known shape slot.
     535                 : 
     536           68124 :         GlobalDef() { }
     537              39 :         GlobalDef(uint32_t knownSlot) : atom(NULL), knownSlot(knownSlot) { }
     538           67902 :         GlobalDef(JSAtom *atom, FunctionBox *box) : atom(atom), funbox(box) { }
     539                 :     };
     540                 : 
     541                 :     JSObject        *globalObj;
     542                 :     BytecodeEmitter *bce;
     543                 : 
     544                 :     /*
     545                 :      * This is the table of global names encountered during parsing. Each
     546                 :      * global name appears in the list only once, and the |names| table
     547                 :      * maps back into |defs| for fast lookup.
     548                 :      *
     549                 :      * A definition may either specify an existing global property, or a new
     550                 :      * one that must be added after compilation succeeds.
     551                 :      */
     552                 :     Vector<GlobalDef, 16> defs;
     553                 :     AtomIndexMap      names;
     554                 : };
     555                 : 
     556                 : struct BytecodeEmitter : public TreeContext
     557                 : {
     558                 :     struct {
     559                 :         jsbytecode  *base;          /* base of JS bytecode vector */
     560                 :         jsbytecode  *limit;         /* one byte beyond end of bytecode */
     561                 :         jsbytecode  *next;          /* pointer to next free bytecode */
     562                 :         jssrcnote   *notes;         /* source notes, see below */
     563                 :         unsigned       noteCount;      /* number of source notes so far */
     564                 :         unsigned       noteLimit;      /* limit number for source notes in notePool */
     565                 :         ptrdiff_t   lastNoteOffset; /* code offset for last source note */
     566                 :         unsigned       currentLine;    /* line number for tree-based srcnote gen */
     567                 :     } prolog, main, *current;
     568                 : 
     569                 :     OwnedAtomIndexMapPtr atomIndices; /* literals indexed for mapping */
     570                 :     AtomDefnMapPtr  roLexdeps;
     571                 :     unsigned           firstLine;      /* first line, for JSScript::NewScriptFromEmitter */
     572                 : 
     573                 :     int            stackDepth;     /* current stack depth in script frame */
     574                 :     unsigned           maxStackDepth;  /* maximum stack depth so far */
     575                 : 
     576                 :     unsigned           ntrynotes;      /* number of allocated so far try notes */
     577                 :     TryNode         *lastTryNode;   /* the last allocated try node */
     578                 : 
     579                 :     unsigned           arrayCompDepth; /* stack depth of array in comprehension */
     580                 : 
     581                 :     unsigned           emitLevel;      /* js::frontend::EmitTree recursion level */
     582                 : 
     583                 :     typedef HashMap<JSAtom *, Value> ConstMap;
     584                 :     ConstMap        constMap;       /* compile time constants */
     585                 : 
     586                 :     GCConstList     constList;      /* constants to be included with the script */
     587                 : 
     588                 :     CGObjectList    objectList;     /* list of emitted objects */
     589                 :     CGObjectList    regexpList;     /* list of emitted regexp that will be
     590                 :                                        cloned during execution */
     591                 : 
     592                 :     OwnedAtomIndexMapPtr upvarIndices; /* map of atoms to upvar indexes */
     593                 : 
     594                 :     UpvarCookies    upvarMap;       /* indexed upvar slot locations */
     595                 : 
     596                 :     GlobalScope     *globalScope;   /* frontend::CompileScript global scope, or null */
     597                 : 
     598                 :     typedef Vector<GlobalSlotArray::Entry, 16> GlobalUseVector;
     599                 : 
     600                 :     GlobalUseVector globalUses;     /* per-script global uses */
     601                 :     OwnedAtomIndexMapPtr globalMap; /* per-script map of global name to globalUses vector */
     602                 : 
     603                 :     /* Vectors of pn_cookie slot values. */
     604                 :     typedef Vector<uint32_t, 8> SlotVector;
     605                 :     SlotVector      closedArgs;
     606                 :     SlotVector      closedVars;
     607                 : 
     608                 :     uint16_t        typesetCount;   /* Number of JOF_TYPESET opcodes generated */
     609                 : 
     610                 :     BytecodeEmitter(Parser *parser, unsigned lineno);
     611                 :     bool init(JSContext *cx, TreeContext::InitBehavior ib = USED_AS_CODE_GENERATOR);
     612                 : 
     613           24079 :     JSContext *context() {
     614           24079 :         return parser->context;
     615                 :     }
     616                 : 
     617                 :     /*
     618                 :      * Note that BytecodeEmitters are magic: they own the arena "top-of-stack"
     619                 :      * space above their tempMark points. This means that you cannot alloc from
     620                 :      * tempLifoAlloc and save the pointer beyond the next BytecodeEmitter
     621                 :      * destructor call.
     622                 :      */
     623                 :     ~BytecodeEmitter();
     624                 : 
     625                 :     /*
     626                 :      * Adds a use of a variable that is statically known to exist on the
     627                 :      * global object.
     628                 :      *
     629                 :      * The actual slot of the variable on the global object is not known
     630                 :      * until after compilation. Properties must be resolved before being
     631                 :      * added, to avoid aliasing properties that should be resolved. This makes
     632                 :      * slot prediction based on the global object's free slot impossible. So,
     633                 :      * we use the slot to index into bce->globalScope->defs, and perform a
     634                 :      * fixup of the script at the very end of compilation.
     635                 :      *
     636                 :      * If the global use can be cached, |cookie| will be set to |slot|.
     637                 :      * Otherwise, |cookie| is set to the free cookie value.
     638                 :      */
     639                 :     bool addGlobalUse(JSAtom *atom, uint32_t slot, UpvarCookie *cookie);
     640                 : 
     641         1109121 :     bool hasUpvarIndices() const {
     642         1109121 :         return upvarIndices.hasMap() && !upvarIndices->empty();
     643                 :     }
     644                 : 
     645           71682 :     bool compilingForEval() const { return !!(flags & TCF_COMPILE_FOR_EVAL); }
     646         1229793 :     JSVersion version() const { return parser->versionWithFlags(); }
     647                 : 
     648                 :     bool shouldNoteClosedName(ParseNode *pn);
     649                 : 
     650                 :     JS_ALWAYS_INLINE
     651        28913292 :     bool makeAtomIndex(JSAtom *atom, jsatomid *indexp) {
     652        28913292 :         AtomIndexAddPtr p = atomIndices->lookupForAdd(atom);
     653        28913292 :         if (p) {
     654        16086065 :             *indexp = p.value();
     655        16086065 :             return true;
     656                 :         }
     657                 : 
     658        12827227 :         jsatomid index = atomIndices->count();
     659        12827227 :         if (!atomIndices->add(p, atom, index))
     660               0 :             return false;
     661                 : 
     662        12827227 :         *indexp = index;
     663        12827227 :         return true;
     664                 :     }
     665                 : 
     666          168356 :     bool checkSingletonContext() {
     667          168356 :         if (!compileAndGo() || inFunction())
     668          102733 :             return false;
     669          109193 :         for (StmtInfo *stmt = topStmt; stmt; stmt = stmt->down) {
     670           44586 :             if (STMT_IS_LOOP(stmt))
     671            1016 :                 return false;
     672                 :         }
     673           64607 :         flags |= TCF_HAS_SINGLETONS;
     674           64607 :         return true;
     675                 :     }
     676                 : 
     677                 :     bool needsImplicitThis();
     678                 : 
     679             704 :     TokenStream *tokenStream() { return &parser->tokenStream; }
     680                 : 
     681       397491067 :     jsbytecode *base() const { return current->base; }
     682       108146349 :     jsbytecode *limit() const { return current->limit; }
     683       325145828 :     jsbytecode *next() const { return current->next; }
     684       132109038 :     jsbytecode *code(ptrdiff_t offset) const { return base() + offset; }
     685       155834569 :     ptrdiff_t offset() const { return next() - base(); }
     686         1109121 :     jsbytecode *prologBase() const { return prolog.base; }
     687         3309956 :     ptrdiff_t prologOffset() const { return prolog.next - prolog.base; }
     688          452311 :     void switchToMain() { current = &main; }
     689          452311 :     void switchToProlog() { current = &prolog; }
     690                 : 
     691       162791191 :     jssrcnote *notes() const { return current->notes; }
     692        72911665 :     unsigned noteCount() const { return current->noteCount; }
     693        71179963 :     unsigned noteLimit() const { return current->noteLimit; }
     694        58161447 :     ptrdiff_t lastNoteOffset() const { return current->lastNoteOffset; }
     695        61919226 :     unsigned currentLine() const { return current->currentLine; }
     696                 : 
     697                 :     inline ptrdiff_t countFinalSourceNotes();
     698                 : };
     699                 : 
     700                 : inline BytecodeEmitter *
     701          413592 : TreeContext::asBytecodeEmitter()
     702                 : {
     703          413592 :     JS_ASSERT(compiling());
     704          413592 :     return static_cast<BytecodeEmitter *>(this);
     705                 : }
     706                 : 
     707                 : namespace frontend {
     708                 : 
     709                 : /*
     710                 :  * Emit one bytecode.
     711                 :  */
     712                 : ptrdiff_t
     713                 : Emit1(JSContext *cx, BytecodeEmitter *bce, JSOp op);
     714                 : 
     715                 : /*
     716                 :  * Emit two bytecodes, an opcode (op) with a byte of immediate operand (op1).
     717                 :  */
     718                 : ptrdiff_t
     719                 : Emit2(JSContext *cx, BytecodeEmitter *bce, JSOp op, jsbytecode op1);
     720                 : 
     721                 : /*
     722                 :  * Emit three bytecodes, an opcode with two bytes of immediate operands.
     723                 :  */
     724                 : ptrdiff_t
     725                 : Emit3(JSContext *cx, BytecodeEmitter *bce, JSOp op, jsbytecode op1, jsbytecode op2);
     726                 : 
     727                 : /*
     728                 :  * Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand.
     729                 :  */
     730                 : ptrdiff_t
     731                 : EmitN(JSContext *cx, BytecodeEmitter *bce, JSOp op, size_t extra);
     732                 : 
     733                 : /*
     734                 :  * Push the C-stack-allocated struct at stmt onto the stmtInfo stack.
     735                 :  */
     736                 : void
     737                 : PushStatement(TreeContext *tc, StmtInfo *stmt, StmtType type, ptrdiff_t top);
     738                 : 
     739                 : /*
     740                 :  * Push a block scope statement and link blockObj into tc->blockChain. To pop
     741                 :  * this statement info record, use PopStatementTC as usual, or if appropriate
     742                 :  * (if generating code), PopStatementBCE.
     743                 :  */
     744                 : void
     745                 : PushBlockScope(TreeContext *tc, StmtInfo *stmt, StaticBlockObject &blockObj, ptrdiff_t top);
     746                 : 
     747                 : /*
     748                 :  * Pop tc->topStmt. If the top StmtInfo struct is not stack-allocated, it
     749                 :  * is up to the caller to free it.
     750                 :  */
     751                 : void
     752                 : PopStatementTC(TreeContext *tc);
     753                 : 
     754                 : /*
     755                 :  * Like PopStatementTC(bce), also patch breaks and continues unless the top
     756                 :  * statement info record represents a try-catch-finally suite. May fail if a
     757                 :  * jump offset overflows.
     758                 :  */
     759                 : JSBool
     760                 : PopStatementBCE(JSContext *cx, BytecodeEmitter *bce);
     761                 : 
     762                 : /*
     763                 :  * Define and lookup a primitive jsval associated with the const named by atom.
     764                 :  * DefineCompileTimeConstant analyzes the constant-folded initializer at pn
     765                 :  * and saves the const's value in bce->constList, if it can be used at compile
     766                 :  * time. It returns true unless an error occurred.
     767                 :  *
     768                 :  * If the initializer's value could not be saved, DefineCompileTimeConstant
     769                 :  * calls will return the undefined value. DefineCompileTimeConstant tries
     770                 :  * to find a const value memorized for atom, returning true with *vp set to a
     771                 :  * value other than undefined if the constant was found, true with *vp set to
     772                 :  * JSVAL_VOID if not found, and false on error.
     773                 :  */
     774                 : JSBool
     775                 : DefineCompileTimeConstant(JSContext *cx, BytecodeEmitter *bce, JSAtom *atom, ParseNode *pn);
     776                 : 
     777                 : /*
     778                 :  * Find a lexically scoped variable (one declared by let, catch, or an array
     779                 :  * comprehension) named by atom, looking in tc's compile-time scopes.
     780                 :  *
     781                 :  * If a WITH statement is reached along the scope stack, return its statement
     782                 :  * info record, so callers can tell that atom is ambiguous. If slotp is not
     783                 :  * null, then if atom is found, set *slotp to its stack slot, otherwise to -1.
     784                 :  * This means that if slotp is not null, all the block objects on the lexical
     785                 :  * scope chain must have had their depth slots computed by the code generator,
     786                 :  * so the caller must be under EmitTree.
     787                 :  *
     788                 :  * In any event, directly return the statement info record in which atom was
     789                 :  * found. Otherwise return null.
     790                 :  */
     791                 : StmtInfo *
     792                 : LexicalLookup(TreeContext *tc, JSAtom *atom, int *slotp, StmtInfo *stmt = NULL);
     793                 : 
     794                 : /*
     795                 :  * Emit code into bce for the tree rooted at pn.
     796                 :  */
     797                 : JSBool
     798                 : EmitTree(JSContext *cx, BytecodeEmitter *bce, ParseNode *pn);
     799                 : 
     800                 : /*
     801                 :  * Emit function code using bce for the tree rooted at body.
     802                 :  */
     803                 : JSBool
     804                 : EmitFunctionScript(JSContext *cx, BytecodeEmitter *bce, ParseNode *body);
     805                 : 
     806                 : } /* namespace frontend */
     807                 : 
     808                 : /*
     809                 :  * Source notes generated along with bytecode for decompiling and debugging.
     810                 :  * A source note is a uint8_t with 5 bits of type and 3 of offset from the pc
     811                 :  * of the previous note. If 3 bits of offset aren't enough, extended delta
     812                 :  * notes (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset
     813                 :  * bits are emitted before the next note. Some notes have operand offsets
     814                 :  * encoded immediately after them, in note bytes or byte-triples.
     815                 :  *
     816                 :  *                 Source Note               Extended Delta
     817                 :  *              +7-6-5-4-3+2-1-0+           +7-6-5+4-3-2-1-0+
     818                 :  *              |note-type|delta|           |1 1| ext-delta |
     819                 :  *              +---------+-----+           +---+-----------+
     820                 :  *
     821                 :  * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE,
     822                 :  * SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode.
     823                 :  *
     824                 :  * NB: the js_SrcNoteSpec array in BytecodeEmitter.cpp is indexed by this
     825                 :  * enum, so its initializers need to match the order here.
     826                 :  *
     827                 :  * Note on adding new source notes: every pair of bytecodes (A, B) where A and
     828                 :  * B have disjoint sets of source notes that could apply to each bytecode may
     829                 :  * reuse the same note type value for two notes (snA, snB) that have the same
     830                 :  * arity in JSSrcNoteSpec. This is why SRC_IF and SRC_INITPROP have the same
     831                 :  * value below.
     832                 :  *
     833                 :  * Don't forget to update JSXDR_BYTECODE_VERSION in jsxdrapi.h for all such
     834                 :  * incompatible source note or other bytecode changes.
     835                 :  */
     836                 : enum SrcNoteType {
     837                 :     SRC_NULL        = 0,        /* terminates a note vector */
     838                 :     SRC_IF          = 1,        /* JSOP_IFEQ bytecode is from an if-then */
     839                 :     SRC_BREAK       = 1,        /* JSOP_GOTO is a break */
     840                 :     SRC_INITPROP    = 1,        /* disjoint meaning applied to JSOP_INITELEM or
     841                 :                                    to an index label in a regular (structuring)
     842                 :                                    or a destructuring object initialiser */
     843                 :     SRC_GENEXP      = 1,        /* JSOP_LAMBDA from generator expression */
     844                 :     SRC_IF_ELSE     = 2,        /* JSOP_IFEQ bytecode is from an if-then-else */
     845                 :     SRC_FOR_IN      = 2,        /* JSOP_GOTO to for-in loop condition from
     846                 :                                    before loop (same arity as SRC_IF_ELSE) */
     847                 :     SRC_FOR         = 3,        /* JSOP_NOP or JSOP_POP in for(;;) loop head */
     848                 :     SRC_WHILE       = 4,        /* JSOP_GOTO to for or while loop condition
     849                 :                                    from before loop, else JSOP_NOP at top of
     850                 :                                    do-while loop */
     851                 :     SRC_CONTINUE    = 5,        /* JSOP_GOTO is a continue, not a break;
     852                 :                                    JSOP_ENDINIT needs extra comma at end of
     853                 :                                    array literal: [1,2,,];
     854                 :                                    JSOP_DUP continuing destructuring pattern;
     855                 :                                    JSOP_POP at end of for-in */
     856                 :     SRC_DECL        = 6,        /* type of a declaration (var, const, let*) */
     857                 :     SRC_DESTRUCT    = 6,        /* JSOP_DUP starting a destructuring assignment
     858                 :                                    operation, with SRC_DECL_* offset operand */
     859                 :     SRC_PCDELTA     = 7,        /* distance forward from comma-operator to
     860                 :                                    next POP, or from CONDSWITCH to first CASE
     861                 :                                    opcode, etc. -- always a forward delta */
     862                 :     SRC_GROUPASSIGN = 7,        /* SRC_DESTRUCT variant for [a, b] = [c, d] */
     863                 :     SRC_DESTRUCTLET = 7,        /* JSOP_DUP starting a destructuring let
     864                 :                                    operation, with offset to JSOP_ENTERLET0 */
     865                 :     SRC_ASSIGNOP    = 8,        /* += or another assign-op follows */
     866                 :     SRC_COND        = 9,        /* JSOP_IFEQ is from conditional ?: operator */
     867                 :     SRC_BRACE       = 10,       /* mandatory brace, for scope or to avoid
     868                 :                                    dangling else */
     869                 :     SRC_HIDDEN      = 11,       /* opcode shouldn't be decompiled */
     870                 :     SRC_PCBASE      = 12,       /* distance back from annotated getprop or
     871                 :                                    setprop op to left-most obj.prop.subprop
     872                 :                                    bytecode -- always a backward delta */
     873                 :     SRC_LABEL       = 13,       /* JSOP_LABEL for label: with atomid immediate */
     874                 :     SRC_LABELBRACE  = 14,       /* JSOP_LABEL for label: {...} begin brace */
     875                 :     SRC_ENDBRACE    = 15,       /* JSOP_NOP for label: {...} end brace */
     876                 :     SRC_BREAK2LABEL = 16,       /* JSOP_GOTO for 'break label' with atomid */
     877                 :     SRC_CONT2LABEL  = 17,       /* JSOP_GOTO for 'continue label' with atomid */
     878                 :     SRC_SWITCH      = 18,       /* JSOP_*SWITCH with offset to end of switch,
     879                 :                                    2nd off to first JSOP_CASE if condswitch */
     880                 :     SRC_SWITCHBREAK = 18,       /* JSOP_GOTO is a break in a switch */
     881                 :     SRC_FUNCDEF     = 19,       /* JSOP_NOP for function f() with atomid */
     882                 :     SRC_CATCH       = 20,       /* catch block has guard */
     883                 :                                 /* 21 is unused */
     884                 :     SRC_NEWLINE     = 22,       /* bytecode follows a source newline */
     885                 :     SRC_SETLINE     = 23,       /* a file-absolute source line number note */
     886                 :     SRC_XDELTA      = 24        /* 24-31 are for extended delta notes */
     887                 : };
     888                 : 
     889                 : /*
     890                 :  * Constants for the SRC_DECL source note.
     891                 :  *
     892                 :  * NB: the var_prefix array in jsopcode.c depends on these dense indexes from
     893                 :  * SRC_DECL_VAR through SRC_DECL_LET.
     894                 :  */
     895                 : #define SRC_DECL_VAR            0
     896                 : #define SRC_DECL_CONST          1
     897                 : #define SRC_DECL_LET            2
     898                 : #define SRC_DECL_NONE           3
     899                 : 
     900                 : #define SN_TYPE_BITS            5
     901                 : #define SN_DELTA_BITS           3
     902                 : #define SN_XDELTA_BITS          6
     903                 : #define SN_TYPE_MASK            (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS)
     904                 : #define SN_DELTA_MASK           ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS))
     905                 : #define SN_XDELTA_MASK          ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS))
     906                 : 
     907                 : #define SN_MAKE_NOTE(sn,t,d)    (*(sn) = (jssrcnote)                          \
     908                 :                                           (((t) << SN_DELTA_BITS)             \
     909                 :                                            | ((d) & SN_DELTA_MASK)))
     910                 : #define SN_MAKE_XDELTA(sn,d)    (*(sn) = (jssrcnote)                          \
     911                 :                                           ((SRC_XDELTA << SN_DELTA_BITS)      \
     912                 :                                            | ((d) & SN_XDELTA_MASK)))
     913                 : 
     914                 : #define SN_IS_XDELTA(sn)        ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA)
     915                 : #define SN_TYPE(sn)             ((js::SrcNoteType)(SN_IS_XDELTA(sn)           \
     916                 :                                                    ? SRC_XDELTA               \
     917                 :                                                    : *(sn) >> SN_DELTA_BITS))
     918                 : #define SN_SET_TYPE(sn,type)    SN_MAKE_NOTE(sn, type, SN_DELTA(sn))
     919                 : #define SN_IS_GETTABLE(sn)      (SN_TYPE(sn) < SRC_NEWLINE)
     920                 : 
     921                 : #define SN_DELTA(sn)            ((ptrdiff_t)(SN_IS_XDELTA(sn)                 \
     922                 :                                              ? *(sn) & SN_XDELTA_MASK         \
     923                 :                                              : *(sn) & SN_DELTA_MASK))
     924                 : #define SN_SET_DELTA(sn,delta)  (SN_IS_XDELTA(sn)                             \
     925                 :                                  ? SN_MAKE_XDELTA(sn, delta)                  \
     926                 :                                  : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta))
     927                 : 
     928                 : #define SN_DELTA_LIMIT          ((ptrdiff_t)JS_BIT(SN_DELTA_BITS))
     929                 : #define SN_XDELTA_LIMIT         ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS))
     930                 : 
     931                 : /*
     932                 :  * Offset fields follow certain notes and are frequency-encoded: an offset in
     933                 :  * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffff] takes three, and
     934                 :  * the high bit of the first byte is set.
     935                 :  */
     936                 : #define SN_3BYTE_OFFSET_FLAG    0x80
     937                 : #define SN_3BYTE_OFFSET_MASK    0x7f
     938                 : 
     939                 : #define SN_MAX_OFFSET ((size_t)((ptrdiff_t)SN_3BYTE_OFFSET_FLAG << 16) - 1)
     940                 : 
     941                 : #define SN_LENGTH(sn)           ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \
     942                 :                                  : js_SrcNoteLength(sn))
     943                 : #define SN_NEXT(sn)             ((sn) + SN_LENGTH(sn))
     944                 : 
     945                 : /* A source note array is terminated by an all-zero element. */
     946                 : #define SN_MAKE_TERMINATOR(sn)  (*(sn) = SRC_NULL)
     947                 : #define SN_IS_TERMINATOR(sn)    (*(sn) == SRC_NULL)
     948                 : 
     949                 : namespace frontend {
     950                 : 
     951                 : /*
     952                 :  * Append a new source note of the given type (and therefore size) to bce's
     953                 :  * notes dynamic array, updating bce->noteCount. Return the new note's index
     954                 :  * within the array pointed at by bce->current->notes. Return -1 if out of
     955                 :  * memory.
     956                 :  */
     957                 : int
     958                 : NewSrcNote(JSContext *cx, BytecodeEmitter *bce, SrcNoteType type);
     959                 : 
     960                 : int
     961                 : NewSrcNote2(JSContext *cx, BytecodeEmitter *bce, SrcNoteType type, ptrdiff_t offset);
     962                 : 
     963                 : int
     964                 : NewSrcNote3(JSContext *cx, BytecodeEmitter *bce, SrcNoteType type, ptrdiff_t offset1,
     965                 :                ptrdiff_t offset2);
     966                 : 
     967                 : /*
     968                 :  * NB: this function can add at most one extra extended delta note.
     969                 :  */
     970                 : jssrcnote *
     971                 : AddToSrcNoteDelta(JSContext *cx, BytecodeEmitter *bce, jssrcnote *sn, ptrdiff_t delta);
     972                 : 
     973                 : JSBool
     974                 : FinishTakingSrcNotes(JSContext *cx, BytecodeEmitter *bce, jssrcnote *notes);
     975                 : 
     976                 : void
     977                 : FinishTakingTryNotes(BytecodeEmitter *bce, JSTryNoteArray *array);
     978                 : 
     979                 : } /* namespace frontend */
     980                 : 
     981                 : /*
     982                 :  * Finish taking source notes in cx's notePool, copying final notes to the new
     983                 :  * stable store allocated by the caller and passed in via notes. Return false
     984                 :  * on malloc failure, which means this function reported an error.
     985                 :  *
     986                 :  * Use this to compute the number of jssrcnotes to allocate and pass in via
     987                 :  * notes. This method knows a lot about details of FinishTakingSrcNotes, so
     988                 :  * DON'T CHANGE js::frontend::FinishTakingSrcNotes WITHOUT CHECKING WHETHER
     989                 :  * THIS METHOD NEEDS CORRESPONDING CHANGES!
     990                 :  */
     991                 : inline ptrdiff_t
     992         1109121 : BytecodeEmitter::countFinalSourceNotes()
     993                 : {
     994         1109121 :     ptrdiff_t diff = prologOffset() - prolog.lastNoteOffset;
     995         1109121 :     ptrdiff_t cnt = prolog.noteCount + main.noteCount + 1;
     996         1109121 :     if (prolog.noteCount && prolog.currentLine != firstLine) {
     997           17407 :         if (diff > SN_DELTA_MASK)
     998              12 :             cnt += JS_HOWMANY(diff - SN_DELTA_MASK, SN_XDELTA_MASK);
     999           17407 :         cnt += 2 + ((firstLine > SN_3BYTE_OFFSET_MASK) << 1);
    1000         1091714 :     } else if (diff > 0) {
    1001           34222 :         if (main.noteCount) {
    1002           34086 :             jssrcnote *sn = main.notes;
    1003                 :             diff -= SN_IS_XDELTA(sn)
    1004                 :                     ? SN_XDELTA_MASK - (*sn & SN_XDELTA_MASK)
    1005           34086 :                     : SN_DELTA_MASK - (*sn & SN_DELTA_MASK);
    1006                 :         }
    1007           34222 :         if (diff > 0)
    1008           27293 :             cnt += JS_HOWMANY(diff, SN_XDELTA_MASK);
    1009                 :     }
    1010         1109121 :     return cnt;
    1011                 : }
    1012                 : 
    1013                 : /*
    1014                 :  * To avoid offending js_SrcNoteSpec[SRC_DECL].arity, pack the two data needed
    1015                 :  * to decompile let into one ptrdiff_t:
    1016                 :  *   offset: offset to the LEAVEBLOCK(EXPR) op (not including ENTER/LEAVE)
    1017                 :  *   groupAssign: whether this was an optimized group assign ([x,y] = [a,b])
    1018                 :  */
    1019           45212 : inline ptrdiff_t PackLetData(size_t offset, bool groupAssign)
    1020                 : {
    1021           45212 :     JS_ASSERT(offset <= (size_t(-1) >> 1));
    1022           45212 :     return ptrdiff_t(offset << 1) | ptrdiff_t(groupAssign);
    1023                 : }
    1024                 : 
    1025            3186 : inline size_t LetDataToOffset(ptrdiff_t w)
    1026                 : {
    1027            3186 :     return size_t(w) >> 1;
    1028                 : }
    1029                 : 
    1030            3726 : inline bool LetDataToGroupAssign(ptrdiff_t w)
    1031                 : {
    1032            3726 :     return size_t(w) & 1;
    1033                 : }
    1034                 : 
    1035                 : } /* namespace js */
    1036                 : 
    1037                 : struct JSSrcNoteSpec {
    1038                 :     const char      *name;      /* name for disassembly/debugging output */
    1039                 :     int8_t          arity;      /* number of offset operands */
    1040                 : };
    1041                 : 
    1042                 : extern JS_FRIEND_DATA(JSSrcNoteSpec)  js_SrcNoteSpec[];
    1043                 : extern JS_FRIEND_API(unsigned)         js_SrcNoteLength(jssrcnote *sn);
    1044                 : 
    1045                 : /*
    1046                 :  * Get and set the offset operand identified by which (0 for the first, etc.).
    1047                 :  */
    1048                 : extern JS_FRIEND_API(ptrdiff_t)
    1049                 : js_GetSrcNoteOffset(jssrcnote *sn, unsigned which);
    1050                 : 
    1051                 : #endif /* BytecodeEmitter_h__ */

Generated by: LCOV version 1.7