LCOV - code coverage report
Current view: directory - js/src/frontend - BytecodeEmitter.h (source / functions) Found Hit Coverage
Test: app.info Lines: 149 148 99.3 %
Date: 2012-04-07 Functions: 63 63 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         3417800 : STMT_TYPE_IN_RANGE(uint16_t type, StmtType begin, StmtType end)
      89                 : {
      90         3417800 :     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                 : JS_ENUM_HEADER(TreeContextFlags, uint32_t)
     165                 : {
     166                 :     /* TreeContext is BytecodeEmitter */
     167                 :     TCF_COMPILING =                            0x1,
     168                 : 
     169                 :     /* parsing inside function body */
     170                 :     TCF_IN_FUNCTION =                          0x2,
     171                 : 
     172                 :     /* function has 'return expr;' */
     173                 :     TCF_RETURN_EXPR =                          0x4,
     174                 : 
     175                 :     /* function has 'return;' */
     176                 :     TCF_RETURN_VOID =                          0x8,
     177                 : 
     178                 :     /* parsing init expr of for; exclude 'in' */
     179                 :     TCF_IN_FOR_INIT =                         0x10,
     180                 : 
     181                 :     /* function has parameter named arguments */
     182                 :     TCF_FUN_PARAM_ARGUMENTS =                 0x20,
     183                 : 
     184                 :     /* function may contain a local named arguments */
     185                 :     TCF_FUN_LOCAL_ARGUMENTS =                 0x40,
     186                 : 
     187                 :     /* function uses arguments except as a parameter name */
     188                 :     TCF_FUN_USES_ARGUMENTS =                  0x80,
     189                 : 
     190                 :     /* function needs Call object per call */
     191                 :     TCF_FUN_HEAVYWEIGHT =                    0x100,
     192                 : 
     193                 :     /* parsed yield statement in function */
     194                 :     TCF_FUN_IS_GENERATOR =                   0x200,
     195                 : 
     196                 :     /* named function expression that uses its own name */
     197                 :     TCF_FUN_USES_OWN_NAME =                  0x400,
     198                 : 
     199                 :     /* block contains a function statement */
     200                 :     TCF_HAS_FUNCTION_STMT =                  0x800,
     201                 : 
     202                 :     /* flag lambda from generator expression */
     203                 :     TCF_GENEXP_LAMBDA =                     0x1000,
     204                 : 
     205                 :     /* script can optimize name references based on scope chain */
     206                 :     TCF_COMPILE_N_GO =                      0x2000,
     207                 : 
     208                 :     /* API caller does not want result value from global script */
     209                 :     TCF_NO_SCRIPT_RVAL =                    0x4000,
     210                 : 
     211                 :     /*
     212                 :      * Set when parsing a declaration-like destructuring pattern.  This flag
     213                 :      * causes PrimaryExpr to create PN_NAME parse nodes for variable references
     214                 :      * which are not hooked into any definition's use chain, added to any tree
     215                 :      * context's AtomList, etc. etc.  CheckDestructuring will do that work
     216                 :      * later.
     217                 :      *
     218                 :      * The comments atop CheckDestructuring explain the distinction between
     219                 :      * assignment-like and declaration-like destructuring patterns, and why
     220                 :      * they need to be treated differently.
     221                 :      */
     222                 :     TCF_DECL_DESTRUCTURING =                0x8000,
     223                 : 
     224                 :     /*
     225                 :      * This function/global/eval code body contained a Use Strict Directive.
     226                 :      * Treat certain strict warnings as errors, and forbid the use of 'with'.
     227                 :      * See also TSF_STRICT_MODE_CODE, JSScript::strictModeCode, and
     228                 :      * JSREPORT_STRICT_ERROR.
     229                 :      */
     230                 :     TCF_STRICT_MODE_CODE =                 0x10000,
     231                 : 
     232                 :     /* The function calls 'eval'. */
     233                 :     TCF_FUN_CALLS_EVAL =                   0x20000,
     234                 : 
     235                 :     /* The function mutates a positional (non-destructuring) parameter. */
     236                 :     TCF_FUN_MUTATES_PARAMETER =            0x40000,
     237                 : 
     238                 :     /* Compiling an eval() script. */
     239                 :     TCF_COMPILE_FOR_EVAL =                0x100000,
     240                 : 
     241                 :     /*
     242                 :      * The function or a function that encloses it may define new local names
     243                 :      * at runtime through means other than calling eval.
     244                 :      */
     245                 :     TCF_FUN_MIGHT_ALIAS_LOCALS =          0x200000,
     246                 : 
     247                 :     /* The script contains singleton initialiser JSOP_OBJECT. */
     248                 :     TCF_HAS_SINGLETONS =                  0x400000,
     249                 : 
     250                 :     /* Some enclosing scope is a with-statement or E4X filter-expression. */
     251                 :     TCF_IN_WITH =                         0x800000,
     252                 : 
     253                 :     /*
     254                 :      * This function does something that can extend the set of bindings in its
     255                 :      * call objects --- it does a direct eval in non-strict code, or includes a
     256                 :      * function statement (as opposed to a function definition).
     257                 :      *
     258                 :      * This flag is *not* inherited by enclosed or enclosing functions; it
     259                 :      * applies only to the function in whose flags it appears.
     260                 :      */
     261                 :     TCF_FUN_EXTENSIBLE_SCOPE =           0x1000000,
     262                 : 
     263                 :     /* The caller is JS_Compile*Script*. */
     264                 :     TCF_NEED_SCRIPT_GLOBAL =             0x2000000
     265                 : 
     266                 : } JS_ENUM_FOOTER(TreeContextFlags);
     267                 : 
     268                 : /* Flags to check for return; vs. return expr; in a function. */
     269                 : static const uint32_t TCF_RETURN_FLAGS = TCF_RETURN_EXPR | TCF_RETURN_VOID;
     270                 : 
     271                 : /*
     272                 :  * Sticky deoptimization flags to propagate from FunctionBody.
     273                 :  */
     274                 : static const uint32_t TCF_FUN_FLAGS = TCF_FUN_USES_ARGUMENTS |
     275                 :                                       TCF_FUN_PARAM_ARGUMENTS |
     276                 :                                       TCF_FUN_LOCAL_ARGUMENTS |
     277                 :                                       TCF_FUN_HEAVYWEIGHT |
     278                 :                                       TCF_FUN_IS_GENERATOR |
     279                 :                                       TCF_FUN_USES_OWN_NAME |
     280                 :                                       TCF_FUN_CALLS_EVAL |
     281                 :                                       TCF_FUN_MIGHT_ALIAS_LOCALS |
     282                 :                                       TCF_FUN_MUTATES_PARAMETER |
     283                 :                                       TCF_STRICT_MODE_CODE |
     284                 :                                       TCF_FUN_EXTENSIBLE_SCOPE;
     285                 : 
     286                 : struct BytecodeEmitter;
     287                 : 
     288                 : struct TreeContext {                /* tree context for semantic checks */
     289                 :     uint32_t        flags;          /* statement state flags, see above */
     290                 :     uint32_t        bodyid;         /* block number of program/function body */
     291                 :     uint32_t        blockidGen;     /* preincremented block number generator */
     292                 :     uint32_t        parenDepth;     /* nesting depth of parens that might turn out
     293                 :                                        to be generator expressions */
     294                 :     uint32_t        yieldCount;     /* number of |yield| tokens encountered at
     295                 :                                        non-zero depth in current paren tree */
     296                 :     uint32_t        argumentsCount; /* number of |arguments| references encountered
     297                 :                                        at non-zero depth in current paren tree */
     298                 :     StmtInfo        *topStmt;       /* top of statement info stack */
     299                 :     StmtInfo        *topScopeStmt;  /* top lexical scope statement */
     300                 :     StaticBlockObject *blockChain;  /* compile block scope chain (NB: one
     301                 :                                        deeper than the topScopeStmt/downScope
     302                 :                                        chain when in head of let block/expr) */
     303                 :     ParseNode       *blockNode;     /* parse node for a block with let declarations
     304                 :                                        (block with its own lexical scope)  */
     305                 :     AtomDecls       decls;          /* function, const, and var declarations */
     306                 :     Parser          *parser;        /* ptr to common parsing and lexing data */
     307                 :     ParseNode       *yieldNode;     /* parse node for a yield expression that might
     308                 :                                        be an error if we turn out to be inside a
     309                 :                                        generator expression */
     310                 :     ParseNode       *argumentsNode; /* parse node for an arguments variable that
     311                 :                                        might be an error if we turn out to be
     312                 :                                        inside a generator expression */
     313                 : 
     314                 :   private:
     315                 :     union {
     316                 :         JSFunction  *fun_;          /* function to store argument and variable
     317                 :                                        names when flags & TCF_IN_FUNCTION */
     318                 :         JSObject    *scopeChain_;   /* scope chain object for the script */
     319                 :     };
     320                 : 
     321                 :   public:
     322          529552 :     JSFunction *fun() const {
     323          529552 :         JS_ASSERT(inFunction());
     324          529552 :         return fun_;
     325                 :     }
     326          321539 :     void setFunction(JSFunction *fun) {
     327          321539 :         JS_ASSERT(inFunction());
     328          321539 :         fun_ = fun;
     329          321539 :     }
     330          336946 :     JSObject *scopeChain() const {
     331          336946 :         JS_ASSERT(!inFunction());
     332          336946 :         return scopeChain_;
     333                 :     }
     334           98583 :     void setScopeChain(JSObject *scopeChain) {
     335           98583 :         JS_ASSERT(!inFunction());
     336           98583 :         scopeChain_ = scopeChain;
     337           98583 :     }
     338                 : 
     339                 :     OwnedAtomDefnMapPtr lexdeps;    /* unresolved lexical name dependencies */
     340                 :     TreeContext     *parent;        /* enclosing function or global context */
     341                 :     unsigned           staticLevel;    /* static compilation unit nesting level */
     342                 : 
     343                 :     FunctionBox     *funbox;        /* null or box for function we're compiling
     344                 :                                        if (flags & TCF_IN_FUNCTION) and not in
     345                 :                                        js::frontend::CompileFunctionBody */
     346                 :     FunctionBox     *functionList;
     347                 : 
     348                 :     ParseNode       *innermostWith; /* innermost WITH parse node */
     349                 : 
     350                 :     Bindings        bindings;       /* bindings in this code, including
     351                 :                                        arguments if we're compiling a function */
     352                 :     Bindings::StackRoot bindingsRoot; /* root for stack allocated bindings. */
     353                 : 
     354                 :     void trace(JSTracer *trc);
     355                 : 
     356                 :     inline TreeContext(Parser *prs);
     357                 :     inline ~TreeContext();
     358                 : 
     359                 :     /*
     360                 :      * js::BytecodeEmitter derives from js::TreeContext; however, only the
     361                 :      * top-level BytecodeEmitters are actually used as full-fledged tree contexts
     362                 :      * (to hold decls and lexdeps). We can avoid allocation overhead by making
     363                 :      * this distinction explicit.
     364                 :      */
     365                 :     enum InitBehavior {
     366                 :         USED_AS_TREE_CONTEXT,
     367                 :         USED_AS_CODE_GENERATOR
     368                 :     };
     369                 : 
     370          420122 :     bool init(JSContext *cx, InitBehavior ib = USED_AS_TREE_CONTEXT) {
     371          420122 :         if (ib == USED_AS_CODE_GENERATOR)
     372          154150 :             return true;
     373          265972 :         return decls.init() && lexdeps.ensureMap(cx);
     374                 :     }
     375                 : 
     376         5493836 :     unsigned blockid() { return topStmt ? topStmt->blockid : bodyid; }
     377                 : 
     378                 :     /*
     379                 :      * True if we are at the topmost level of a entire script or function body.
     380                 :      * For example, while parsing this code we would encounter f1 and f2 at
     381                 :      * body level, but we would not encounter f3 or f4 at body level:
     382                 :      *
     383                 :      *   function f1() { function f2() { } }
     384                 :      *   if (cond) { function f3() { if (cond) { function f4() { } } } }
     385                 :      */
     386          797007 :     bool atBodyLevel() { return !topStmt || (topStmt->flags & SIF_BODY_BLOCK); }
     387                 : 
     388                 :     /* Test whether we're in a statement of given type. */
     389                 :     bool inStatement(StmtType type);
     390                 : 
     391         4800704 :     bool inStrictMode() const {
     392         4800704 :         return flags & TCF_STRICT_MODE_CODE;
     393                 :     }
     394                 : 
     395                 :     inline bool needStrictChecks();
     396                 : 
     397                 :     // Return true there is a generator function within |skip| lexical scopes
     398                 :     // (going upward) from this context's lexical scope. Always return true if
     399                 :     // this context is itself a generator.
     400                 :     bool skipSpansGenerator(unsigned skip);
     401                 : 
     402         2332785 :     bool compileAndGo() const { return flags & TCF_COMPILE_N_GO; }
     403         7565230 :     bool inFunction() const { return flags & TCF_IN_FUNCTION; }
     404                 : 
     405          372881 :     bool compiling() const { return flags & TCF_COMPILING; }
     406                 :     inline BytecodeEmitter *asBytecodeEmitter();
     407                 : 
     408          161097 :     bool usesArguments() const {
     409          161097 :         return flags & TCF_FUN_USES_ARGUMENTS;
     410                 :     }
     411                 : 
     412            7988 :     void noteCallsEval() {
     413            7988 :         flags |= TCF_FUN_CALLS_EVAL;
     414            7988 :     }
     415                 : 
     416         1029643 :     bool callsEval() const {
     417         1029643 :         return flags & TCF_FUN_CALLS_EVAL;
     418                 :     }
     419                 : 
     420             432 :     void noteMightAliasLocals() {
     421             432 :         flags |= TCF_FUN_MIGHT_ALIAS_LOCALS;
     422             432 :     }
     423                 : 
     424         1973774 :     bool mightAliasLocals() const {
     425         1973774 :         return flags & TCF_FUN_MIGHT_ALIAS_LOCALS;
     426                 :     }
     427                 : 
     428                 :     void noteParameterMutation() {
     429                 :         JS_ASSERT(inFunction());
     430                 :         flags |= TCF_FUN_MUTATES_PARAMETER;
     431                 :     }
     432                 : 
     433             270 :     bool mutatesParameter() const {
     434             270 :         JS_ASSERT(inFunction());
     435             270 :         return flags & TCF_FUN_MUTATES_PARAMETER;
     436                 :     }
     437                 : 
     438          172751 :     bool mayOverwriteArguments() const {
     439          172751 :         JS_ASSERT(inFunction());
     440          172751 :         JS_ASSERT_IF(inStrictMode(),
     441          172751 :                      !(flags & (TCF_FUN_PARAM_ARGUMENTS | TCF_FUN_LOCAL_ARGUMENTS)));
     442          172751 :         return !inStrictMode() &&
     443          171409 :                (callsEval() ||
     444          344160 :                 flags & (TCF_FUN_PARAM_ARGUMENTS | TCF_FUN_LOCAL_ARGUMENTS));
     445                 :     }
     446                 : 
     447             207 :     void noteLocalOverwritesArguments() {
     448             207 :         flags |= TCF_FUN_LOCAL_ARGUMENTS;
     449             207 :     }
     450                 : 
     451            4311 :     void noteArgumentsNameUse(ParseNode *node) {
     452            4311 :         JS_ASSERT(inFunction());
     453            4311 :         JS_ASSERT(node->isKind(PNK_NAME));
     454            4311 :         JS_ASSERT(node->pn_atom == parser->context->runtime->atomState.argumentsAtom);
     455            4311 :         countArgumentsUse(node);
     456            4311 :         flags |= TCF_FUN_USES_ARGUMENTS;
     457            4311 :     }
     458                 : 
     459                 :     /*
     460                 :      * Uses of |arguments| must be noted so that such uses can be forbidden if
     461                 :      * they occur inside generator expressions (which desugar to functions and
     462                 :      * yields, in which |arguments| would have an entirely different meaning).
     463                 :      */
     464            4760 :     void countArgumentsUse(ParseNode *node) {
     465            4760 :         JS_ASSERT(node->isKind(PNK_NAME));
     466            4760 :         JS_ASSERT(node->pn_atom == parser->context->runtime->atomState.argumentsAtom);
     467            4760 :         argumentsCount++;
     468            4760 :         argumentsNode = node;
     469            4760 :     }
     470                 : 
     471          160592 :     bool needsEagerArguments() const {
     472          160592 :         return inStrictMode() && ((usesArguments() && mutatesParameter()) || callsEval());
     473                 :     }
     474                 : 
     475            4516 :     void noteHasExtensibleScope() {
     476            4516 :         flags |= TCF_FUN_EXTENSIBLE_SCOPE;
     477            4516 :     }
     478                 : 
     479             315 :     bool hasExtensibleScope() const {
     480             315 :         return flags & TCF_FUN_EXTENSIBLE_SCOPE;
     481                 :     }
     482                 : 
     483          341019 :     ParseNode *freeTree(ParseNode *pn) { return parser->freeTree(pn); }
     484                 : };
     485                 : 
     486                 : /*
     487                 :  * Return true if we need to check for conditions that elicit
     488                 :  * JSOPTION_STRICT warnings or strict mode errors.
     489                 :  */
     490         1162292 : inline bool TreeContext::needStrictChecks() {
     491         1162292 :     return parser->context->hasStrictOption() || inStrictMode();
     492                 : }
     493                 : 
     494                 : namespace frontend {
     495                 : 
     496                 : bool
     497                 : SetStaticLevel(TreeContext *tc, unsigned staticLevel);
     498                 : 
     499                 : bool
     500                 : GenerateBlockId(TreeContext *tc, uint32_t &blockid);
     501                 : 
     502                 : } /* namespace frontend */
     503                 : 
     504                 : struct TryNode {
     505                 :     JSTryNote       note;
     506                 :     TryNode       *prev;
     507                 : };
     508                 : 
     509                 : struct CGObjectList {
     510                 :     uint32_t            length;     /* number of emitted so far objects */
     511                 :     ObjectBox           *lastbox;   /* last emitted object */
     512                 : 
     513          519198 :     CGObjectList() : length(0), lastbox(NULL) {}
     514                 : 
     515                 :     unsigned index(ObjectBox *objbox);
     516                 :     void finish(JSObjectArray *array);
     517                 : };
     518                 : 
     519          259599 : class GCConstList {
     520                 :     Vector<Value> list;
     521                 :   public:
     522          259599 :     GCConstList(JSContext *cx) : list(cx) {}
     523           11153 :     bool append(Value v) { return list.append(v); }
     524          529353 :     size_t length() const { return list.length(); }
     525                 :     void finish(JSConstArray *array);
     526                 : };
     527                 : 
     528           94838 : struct GlobalScope {
     529           94838 :     GlobalScope(JSContext *cx, JSObject *globalObj, BytecodeEmitter *bce)
     530           94838 :       : globalObj(globalObj), bce(bce), defs(cx), names(cx)
     531           94838 :     { }
     532                 : 
     533           77268 :     struct GlobalDef {
     534                 :         JSAtom        *atom;        // If non-NULL, specifies the property name to add.
     535                 :         FunctionBox   *funbox;      // If non-NULL, function value for the property.
     536                 :                                     // This value is only set/used if atom is non-NULL.
     537                 :         uint32_t      knownSlot;    // If atom is NULL, this is the known shape slot.
     538                 : 
     539           68811 :         GlobalDef() { }
     540              39 :         GlobalDef(uint32_t knownSlot) : atom(NULL), knownSlot(knownSlot) { }
     541           68589 :         GlobalDef(JSAtom *atom, FunctionBox *box) : atom(atom), funbox(box) { }
     542                 :     };
     543                 : 
     544                 :     JSObject        *globalObj;
     545                 :     BytecodeEmitter *bce;
     546                 : 
     547                 :     /*
     548                 :      * This is the table of global names encountered during parsing. Each
     549                 :      * global name appears in the list only once, and the |names| table
     550                 :      * maps back into |defs| for fast lookup.
     551                 :      *
     552                 :      * A definition may either specify an existing global property, or a new
     553                 :      * one that must be added after compilation succeeds.
     554                 :      */
     555                 :     Vector<GlobalDef, 16> defs;
     556                 :     AtomIndexMap      names;
     557                 : };
     558                 : 
     559                 : struct BytecodeEmitter : public TreeContext
     560                 : {
     561                 :     struct {
     562                 :         jsbytecode  *base;          /* base of JS bytecode vector */
     563                 :         jsbytecode  *limit;         /* one byte beyond end of bytecode */
     564                 :         jsbytecode  *next;          /* pointer to next free bytecode */
     565                 :         jssrcnote   *notes;         /* source notes, see below */
     566                 :         unsigned       noteCount;      /* number of source notes so far */
     567                 :         unsigned       noteLimit;      /* limit number for source notes in notePool */
     568                 :         ptrdiff_t   lastNoteOffset; /* code offset for last source note */
     569                 :         unsigned       currentLine;    /* line number for tree-based srcnote gen */
     570                 :     } prolog, main, *current;
     571                 : 
     572                 :     OwnedAtomIndexMapPtr atomIndices; /* literals indexed for mapping */
     573                 :     AtomDefnMapPtr  roLexdeps;
     574                 :     unsigned           firstLine;      /* first line, for JSScript::NewScriptFromEmitter */
     575                 : 
     576                 :     int            stackDepth;     /* current stack depth in script frame */
     577                 :     unsigned           maxStackDepth;  /* maximum stack depth so far */
     578                 : 
     579                 :     unsigned           ntrynotes;      /* number of allocated so far try notes */
     580                 :     TryNode         *lastTryNode;   /* the last allocated try node */
     581                 : 
     582                 :     unsigned           arrayCompDepth; /* stack depth of array in comprehension */
     583                 : 
     584                 :     unsigned           emitLevel;      /* js::frontend::EmitTree recursion level */
     585                 : 
     586                 :     typedef HashMap<JSAtom *, Value> ConstMap;
     587                 :     ConstMap        constMap;       /* compile time constants */
     588                 : 
     589                 :     GCConstList     constList;      /* constants to be included with the script */
     590                 : 
     591                 :     CGObjectList    objectList;     /* list of emitted objects */
     592                 :     CGObjectList    regexpList;     /* list of emitted regexp that will be
     593                 :                                        cloned during execution */
     594                 : 
     595                 :     GlobalScope     *globalScope;   /* frontend::CompileScript global scope, or null */
     596                 : 
     597                 :     typedef Vector<GlobalSlotArray::Entry, 16> GlobalUseVector;
     598                 : 
     599                 :     GlobalUseVector globalUses;     /* per-script global uses */
     600                 :     OwnedAtomIndexMapPtr globalMap; /* per-script map of global name to globalUses vector */
     601                 : 
     602                 :     /* Vectors of pn_cookie slot values. */
     603                 :     typedef Vector<uint32_t, 8> SlotVector;
     604                 :     SlotVector      closedArgs;
     605                 :     SlotVector      closedVars;
     606                 : 
     607                 :     uint16_t        typesetCount;   /* Number of JOF_TYPESET opcodes generated */
     608                 : 
     609                 :     BytecodeEmitter(Parser *parser, unsigned lineno);
     610                 :     bool init(JSContext *cx, TreeContext::InitBehavior ib = USED_AS_CODE_GENERATOR);
     611                 : 
     612           23937 :     JSContext *context() {
     613           23937 :         return parser->context;
     614                 :     }
     615                 : 
     616                 :     /*
     617                 :      * Note that BytecodeEmitters are magic: they own the arena "top-of-stack"
     618                 :      * space above their tempMark points. This means that you cannot alloc from
     619                 :      * tempLifoAlloc and save the pointer beyond the next BytecodeEmitter
     620                 :      * destructor call.
     621                 :      */
     622                 :     ~BytecodeEmitter();
     623                 : 
     624                 :     /*
     625                 :      * Adds a use of a variable that is statically known to exist on the
     626                 :      * global object.
     627                 :      *
     628                 :      * The actual slot of the variable on the global object is not known
     629                 :      * until after compilation. Properties must be resolved before being
     630                 :      * added, to avoid aliasing properties that should be resolved. This makes
     631                 :      * slot prediction based on the global object's free slot impossible. So,
     632                 :      * we use the slot to index into bce->globalScope->defs, and perform a
     633                 :      * fixup of the script at the very end of compilation.
     634                 :      *
     635                 :      * If the global use can be cached, |cookie| will be set to |slot|.
     636                 :      * Otherwise, |cookie| is set to the free cookie value.
     637                 :      */
     638                 :     bool addGlobalUse(JSAtom *atom, uint32_t slot, UpvarCookie *cookie);
     639                 : 
     640           72465 :     bool compilingForEval() const { return !!(flags & TCF_COMPILE_FOR_EVAL); }
     641          353567 :     JSVersion version() const { return parser->versionWithFlags(); }
     642                 : 
     643                 :     bool shouldNoteClosedName(ParseNode *pn);
     644                 : 
     645                 :     JS_ALWAYS_INLINE
     646         2554558 :     bool makeAtomIndex(JSAtom *atom, jsatomid *indexp) {
     647         5109116 :         AtomIndexAddPtr p = atomIndices->lookupForAdd(atom);
     648         2554558 :         if (p) {
     649         1902094 :             *indexp = p.value();
     650         1902094 :             return true;
     651                 :         }
     652                 : 
     653          652464 :         jsatomid index = atomIndices->count();
     654          652464 :         if (!atomIndices->add(p, atom, index))
     655               0 :             return false;
     656                 : 
     657          652464 :         *indexp = index;
     658          652464 :         return true;
     659                 :     }
     660                 : 
     661           95341 :     bool checkSingletonContext() {
     662           95341 :         if (!compileAndGo() || inFunction())
     663           10821 :             return false;
     664          170270 :         for (StmtInfo *stmt = topStmt; stmt; stmt = stmt->down) {
     665           86780 :             if (STMT_IS_LOOP(stmt))
     666            1030 :                 return false;
     667                 :         }
     668           83490 :         flags |= TCF_HAS_SINGLETONS;
     669           83490 :         return true;
     670                 :     }
     671                 : 
     672                 :     bool needsImplicitThis();
     673                 : 
     674             163 :     TokenStream *tokenStream() { return &parser->tokenStream; }
     675                 : 
     676        34623299 :     jsbytecode *base() const { return current->base; }
     677         9217984 :     jsbytecode *limit() const { return current->limit; }
     678        26955123 :     jsbytecode *next() const { return current->next; }
     679        12940828 :     jsbytecode *code(ptrdiff_t offset) const { return base() + offset; }
     680        12189151 :     ptrdiff_t offset() const { return next() - base(); }
     681          259010 :     jsbytecode *prologBase() const { return prolog.base; }
     682          776001 :     ptrdiff_t prologOffset() const { return prolog.next - prolog.base; }
     683           63281 :     void switchToMain() { current = &main; }
     684           63281 :     void switchToProlog() { current = &prolog; }
     685                 : 
     686        10063280 :     jssrcnote *notes() const { return current->notes; }
     687         4333257 :     unsigned noteCount() const { return current->noteCount; }
     688         4292090 :     unsigned noteLimit() const { return current->noteLimit; }
     689         3457459 :     ptrdiff_t lastNoteOffset() const { return current->lastNoteOffset; }
     690         6273122 :     unsigned currentLine() const { return current->currentLine; }
     691                 : 
     692                 :     inline ptrdiff_t countFinalSourceNotes();
     693                 : };
     694                 : 
     695                 : inline BytecodeEmitter *
     696          158122 : TreeContext::asBytecodeEmitter()
     697                 : {
     698          158122 :     JS_ASSERT(compiling());
     699          158122 :     return static_cast<BytecodeEmitter *>(this);
     700                 : }
     701                 : 
     702                 : namespace frontend {
     703                 : 
     704                 : /*
     705                 :  * Emit one bytecode.
     706                 :  */
     707                 : ptrdiff_t
     708                 : Emit1(JSContext *cx, BytecodeEmitter *bce, JSOp op);
     709                 : 
     710                 : /*
     711                 :  * Emit two bytecodes, an opcode (op) with a byte of immediate operand (op1).
     712                 :  */
     713                 : ptrdiff_t
     714                 : Emit2(JSContext *cx, BytecodeEmitter *bce, JSOp op, jsbytecode op1);
     715                 : 
     716                 : /*
     717                 :  * Emit three bytecodes, an opcode with two bytes of immediate operands.
     718                 :  */
     719                 : ptrdiff_t
     720                 : Emit3(JSContext *cx, BytecodeEmitter *bce, JSOp op, jsbytecode op1, jsbytecode op2);
     721                 : 
     722                 : /*
     723                 :  * Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand.
     724                 :  */
     725                 : ptrdiff_t
     726                 : EmitN(JSContext *cx, BytecodeEmitter *bce, JSOp op, size_t extra);
     727                 : 
     728                 : /*
     729                 :  * Push the C-stack-allocated struct at stmt onto the stmtInfo stack.
     730                 :  */
     731                 : void
     732                 : PushStatement(TreeContext *tc, StmtInfo *stmt, StmtType type, ptrdiff_t top);
     733                 : 
     734                 : /*
     735                 :  * Push a block scope statement and link blockObj into tc->blockChain. To pop
     736                 :  * this statement info record, use PopStatementTC as usual, or if appropriate
     737                 :  * (if generating code), PopStatementBCE.
     738                 :  */
     739                 : void
     740                 : PushBlockScope(TreeContext *tc, StmtInfo *stmt, StaticBlockObject &blockObj, ptrdiff_t top);
     741                 : 
     742                 : /*
     743                 :  * Pop tc->topStmt. If the top StmtInfo struct is not stack-allocated, it
     744                 :  * is up to the caller to free it.
     745                 :  */
     746                 : void
     747                 : PopStatementTC(TreeContext *tc);
     748                 : 
     749                 : /*
     750                 :  * Like PopStatementTC(bce), also patch breaks and continues unless the top
     751                 :  * statement info record represents a try-catch-finally suite. May fail if a
     752                 :  * jump offset overflows.
     753                 :  */
     754                 : JSBool
     755                 : PopStatementBCE(JSContext *cx, BytecodeEmitter *bce);
     756                 : 
     757                 : /*
     758                 :  * Define and lookup a primitive jsval associated with the const named by atom.
     759                 :  * DefineCompileTimeConstant analyzes the constant-folded initializer at pn
     760                 :  * and saves the const's value in bce->constList, if it can be used at compile
     761                 :  * time. It returns true unless an error occurred.
     762                 :  *
     763                 :  * If the initializer's value could not be saved, DefineCompileTimeConstant
     764                 :  * calls will return the undefined value. DefineCompileTimeConstant tries
     765                 :  * to find a const value memorized for atom, returning true with *vp set to a
     766                 :  * value other than undefined if the constant was found, true with *vp set to
     767                 :  * JSVAL_VOID if not found, and false on error.
     768                 :  */
     769                 : JSBool
     770                 : DefineCompileTimeConstant(JSContext *cx, BytecodeEmitter *bce, JSAtom *atom, ParseNode *pn);
     771                 : 
     772                 : /*
     773                 :  * Find a lexically scoped variable (one declared by let, catch, or an array
     774                 :  * comprehension) named by atom, looking in tc's compile-time scopes.
     775                 :  *
     776                 :  * If a WITH statement is reached along the scope stack, return its statement
     777                 :  * info record, so callers can tell that atom is ambiguous. If slotp is not
     778                 :  * null, then if atom is found, set *slotp to its stack slot, otherwise to -1.
     779                 :  * This means that if slotp is not null, all the block objects on the lexical
     780                 :  * scope chain must have had their depth slots computed by the code generator,
     781                 :  * so the caller must be under EmitTree.
     782                 :  *
     783                 :  * In any event, directly return the statement info record in which atom was
     784                 :  * found. Otherwise return null.
     785                 :  */
     786                 : StmtInfo *
     787                 : LexicalLookup(TreeContext *tc, JSAtom *atom, int *slotp, StmtInfo *stmt = NULL);
     788                 : 
     789                 : /*
     790                 :  * Emit code into bce for the tree rooted at pn.
     791                 :  */
     792                 : JSBool
     793                 : EmitTree(JSContext *cx, BytecodeEmitter *bce, ParseNode *pn);
     794                 : 
     795                 : /*
     796                 :  * Emit function code using bce for the tree rooted at body.
     797                 :  */
     798                 : JSBool
     799                 : EmitFunctionScript(JSContext *cx, BytecodeEmitter *bce, ParseNode *body);
     800                 : 
     801                 : } /* namespace frontend */
     802                 : 
     803                 : /*
     804                 :  * Source notes generated along with bytecode for decompiling and debugging.
     805                 :  * A source note is a uint8_t with 5 bits of type and 3 of offset from the pc
     806                 :  * of the previous note. If 3 bits of offset aren't enough, extended delta
     807                 :  * notes (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset
     808                 :  * bits are emitted before the next note. Some notes have operand offsets
     809                 :  * encoded immediately after them, in note bytes or byte-triples.
     810                 :  *
     811                 :  *                 Source Note               Extended Delta
     812                 :  *              +7-6-5-4-3+2-1-0+           +7-6-5+4-3-2-1-0+
     813                 :  *              |note-type|delta|           |1 1| ext-delta |
     814                 :  *              +---------+-----+           +---+-----------+
     815                 :  *
     816                 :  * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE,
     817                 :  * SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode.
     818                 :  *
     819                 :  * NB: the js_SrcNoteSpec array in BytecodeEmitter.cpp is indexed by this
     820                 :  * enum, so its initializers need to match the order here.
     821                 :  *
     822                 :  * Note on adding new source notes: every pair of bytecodes (A, B) where A and
     823                 :  * B have disjoint sets of source notes that could apply to each bytecode may
     824                 :  * reuse the same note type value for two notes (snA, snB) that have the same
     825                 :  * arity in JSSrcNoteSpec. This is why SRC_IF and SRC_INITPROP have the same
     826                 :  * value below.
     827                 :  *
     828                 :  * Don't forget to update XDR_BYTECODE_VERSION in vm/Xdr.h for all such
     829                 :  * incompatible source note or other bytecode changes.
     830                 :  */
     831                 : enum SrcNoteType {
     832                 :     SRC_NULL        = 0,        /* terminates a note vector */
     833                 :     SRC_IF          = 1,        /* JSOP_IFEQ bytecode is from an if-then */
     834                 :     SRC_BREAK       = 1,        /* JSOP_GOTO is a break */
     835                 :     SRC_INITPROP    = 1,        /* disjoint meaning applied to JSOP_INITELEM or
     836                 :                                    to an index label in a regular (structuring)
     837                 :                                    or a destructuring object initialiser */
     838                 :     SRC_GENEXP      = 1,        /* JSOP_LAMBDA from generator expression */
     839                 :     SRC_IF_ELSE     = 2,        /* JSOP_IFEQ bytecode is from an if-then-else */
     840                 :     SRC_FOR_IN      = 2,        /* JSOP_GOTO to for-in loop condition from
     841                 :                                    before loop (same arity as SRC_IF_ELSE) */
     842                 :     SRC_FOR         = 3,        /* JSOP_NOP or JSOP_POP in for(;;) loop head */
     843                 :     SRC_WHILE       = 4,        /* JSOP_GOTO to for or while loop condition
     844                 :                                    from before loop, else JSOP_NOP at top of
     845                 :                                    do-while loop */
     846                 :     SRC_CONTINUE    = 5,        /* JSOP_GOTO is a continue, not a break;
     847                 :                                    JSOP_ENDINIT needs extra comma at end of
     848                 :                                    array literal: [1,2,,];
     849                 :                                    JSOP_DUP continuing destructuring pattern;
     850                 :                                    JSOP_POP at end of for-in */
     851                 :     SRC_DECL        = 6,        /* type of a declaration (var, const, let*) */
     852                 :     SRC_DESTRUCT    = 6,        /* JSOP_DUP starting a destructuring assignment
     853                 :                                    operation, with SRC_DECL_* offset operand */
     854                 :     SRC_PCDELTA     = 7,        /* distance forward from comma-operator to
     855                 :                                    next POP, or from CONDSWITCH to first CASE
     856                 :                                    opcode, etc. -- always a forward delta */
     857                 :     SRC_GROUPASSIGN = 7,        /* SRC_DESTRUCT variant for [a, b] = [c, d] */
     858                 :     SRC_DESTRUCTLET = 7,        /* JSOP_DUP starting a destructuring let
     859                 :                                    operation, with offset to JSOP_ENTERLET0 */
     860                 :     SRC_ASSIGNOP    = 8,        /* += or another assign-op follows */
     861                 :     SRC_COND        = 9,        /* JSOP_IFEQ is from conditional ?: operator */
     862                 :     SRC_BRACE       = 10,       /* mandatory brace, for scope or to avoid
     863                 :                                    dangling else */
     864                 :     SRC_HIDDEN      = 11,       /* opcode shouldn't be decompiled */
     865                 :     SRC_PCBASE      = 12,       /* distance back from annotated getprop or
     866                 :                                    setprop op to left-most obj.prop.subprop
     867                 :                                    bytecode -- always a backward delta */
     868                 :     SRC_LABEL       = 13,       /* JSOP_LABEL for label: with atomid immediate */
     869                 :     SRC_LABELBRACE  = 14,       /* JSOP_LABEL for label: {...} begin brace */
     870                 :     SRC_ENDBRACE    = 15,       /* JSOP_NOP for label: {...} end brace */
     871                 :     SRC_BREAK2LABEL = 16,       /* JSOP_GOTO for 'break label' with atomid */
     872                 :     SRC_CONT2LABEL  = 17,       /* JSOP_GOTO for 'continue label' with atomid */
     873                 :     SRC_SWITCH      = 18,       /* JSOP_*SWITCH with offset to end of switch,
     874                 :                                    2nd off to first JSOP_CASE if condswitch */
     875                 :     SRC_SWITCHBREAK = 18,       /* JSOP_GOTO is a break in a switch */
     876                 :     SRC_FUNCDEF     = 19,       /* JSOP_NOP for function f() with atomid */
     877                 :     SRC_CATCH       = 20,       /* catch block has guard */
     878                 :                                 /* 21 is unused */
     879                 :     SRC_NEWLINE     = 22,       /* bytecode follows a source newline */
     880                 :     SRC_SETLINE     = 23,       /* a file-absolute source line number note */
     881                 :     SRC_XDELTA      = 24        /* 24-31 are for extended delta notes */
     882                 : };
     883                 : 
     884                 : /*
     885                 :  * Constants for the SRC_DECL source note.
     886                 :  *
     887                 :  * NB: the var_prefix array in jsopcode.c depends on these dense indexes from
     888                 :  * SRC_DECL_VAR through SRC_DECL_LET.
     889                 :  */
     890                 : #define SRC_DECL_VAR            0
     891                 : #define SRC_DECL_CONST          1
     892                 : #define SRC_DECL_LET            2
     893                 : #define SRC_DECL_NONE           3
     894                 : 
     895                 : #define SN_TYPE_BITS            5
     896                 : #define SN_DELTA_BITS           3
     897                 : #define SN_XDELTA_BITS          6
     898                 : #define SN_TYPE_MASK            (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS)
     899                 : #define SN_DELTA_MASK           ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS))
     900                 : #define SN_XDELTA_MASK          ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS))
     901                 : 
     902                 : #define SN_MAKE_NOTE(sn,t,d)    (*(sn) = (jssrcnote)                          \
     903                 :                                           (((t) << SN_DELTA_BITS)             \
     904                 :                                            | ((d) & SN_DELTA_MASK)))
     905                 : #define SN_MAKE_XDELTA(sn,d)    (*(sn) = (jssrcnote)                          \
     906                 :                                           ((SRC_XDELTA << SN_DELTA_BITS)      \
     907                 :                                            | ((d) & SN_XDELTA_MASK)))
     908                 : 
     909                 : #define SN_IS_XDELTA(sn)        ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA)
     910                 : #define SN_TYPE(sn)             ((js::SrcNoteType)(SN_IS_XDELTA(sn)           \
     911                 :                                                    ? SRC_XDELTA               \
     912                 :                                                    : *(sn) >> SN_DELTA_BITS))
     913                 : #define SN_SET_TYPE(sn,type)    SN_MAKE_NOTE(sn, type, SN_DELTA(sn))
     914                 : #define SN_IS_GETTABLE(sn)      (SN_TYPE(sn) < SRC_NEWLINE)
     915                 : 
     916                 : #define SN_DELTA(sn)            ((ptrdiff_t)(SN_IS_XDELTA(sn)                 \
     917                 :                                              ? *(sn) & SN_XDELTA_MASK         \
     918                 :                                              : *(sn) & SN_DELTA_MASK))
     919                 : #define SN_SET_DELTA(sn,delta)  (SN_IS_XDELTA(sn)                             \
     920                 :                                  ? SN_MAKE_XDELTA(sn, delta)                  \
     921                 :                                  : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta))
     922                 : 
     923                 : #define SN_DELTA_LIMIT          ((ptrdiff_t)JS_BIT(SN_DELTA_BITS))
     924                 : #define SN_XDELTA_LIMIT         ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS))
     925                 : 
     926                 : /*
     927                 :  * Offset fields follow certain notes and are frequency-encoded: an offset in
     928                 :  * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffff] takes three, and
     929                 :  * the high bit of the first byte is set.
     930                 :  */
     931                 : #define SN_3BYTE_OFFSET_FLAG    0x80
     932                 : #define SN_3BYTE_OFFSET_MASK    0x7f
     933                 : 
     934                 : #define SN_MAX_OFFSET ((size_t)((ptrdiff_t)SN_3BYTE_OFFSET_FLAG << 16) - 1)
     935                 : 
     936                 : #define SN_LENGTH(sn)           ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \
     937                 :                                  : js_SrcNoteLength(sn))
     938                 : #define SN_NEXT(sn)             ((sn) + SN_LENGTH(sn))
     939                 : 
     940                 : /* A source note array is terminated by an all-zero element. */
     941                 : #define SN_MAKE_TERMINATOR(sn)  (*(sn) = SRC_NULL)
     942                 : #define SN_IS_TERMINATOR(sn)    (*(sn) == SRC_NULL)
     943                 : 
     944                 : namespace frontend {
     945                 : 
     946                 : /*
     947                 :  * Append a new source note of the given type (and therefore size) to bce's
     948                 :  * notes dynamic array, updating bce->noteCount. Return the new note's index
     949                 :  * within the array pointed at by bce->current->notes. Return -1 if out of
     950                 :  * memory.
     951                 :  */
     952                 : int
     953                 : NewSrcNote(JSContext *cx, BytecodeEmitter *bce, SrcNoteType type);
     954                 : 
     955                 : int
     956                 : NewSrcNote2(JSContext *cx, BytecodeEmitter *bce, SrcNoteType type, ptrdiff_t offset);
     957                 : 
     958                 : int
     959                 : NewSrcNote3(JSContext *cx, BytecodeEmitter *bce, SrcNoteType type, ptrdiff_t offset1,
     960                 :                ptrdiff_t offset2);
     961                 : 
     962                 : /*
     963                 :  * NB: this function can add at most one extra extended delta note.
     964                 :  */
     965                 : jssrcnote *
     966                 : AddToSrcNoteDelta(JSContext *cx, BytecodeEmitter *bce, jssrcnote *sn, ptrdiff_t delta);
     967                 : 
     968                 : JSBool
     969                 : FinishTakingSrcNotes(JSContext *cx, BytecodeEmitter *bce, jssrcnote *notes);
     970                 : 
     971                 : void
     972                 : FinishTakingTryNotes(BytecodeEmitter *bce, JSTryNoteArray *array);
     973                 : 
     974                 : } /* namespace frontend */
     975                 : 
     976                 : /*
     977                 :  * Finish taking source notes in cx's notePool, copying final notes to the new
     978                 :  * stable store allocated by the caller and passed in via notes. Return false
     979                 :  * on malloc failure, which means this function reported an error.
     980                 :  *
     981                 :  * Use this to compute the number of jssrcnotes to allocate and pass in via
     982                 :  * notes. This method knows a lot about details of FinishTakingSrcNotes, so
     983                 :  * DON'T CHANGE js::frontend::FinishTakingSrcNotes WITHOUT CHECKING WHETHER
     984                 :  * THIS METHOD NEEDS CORRESPONDING CHANGES!
     985                 :  */
     986                 : inline ptrdiff_t
     987          259010 : BytecodeEmitter::countFinalSourceNotes()
     988                 : {
     989          259010 :     ptrdiff_t diff = prologOffset() - prolog.lastNoteOffset;
     990          259010 :     ptrdiff_t cnt = prolog.noteCount + main.noteCount + 1;
     991          259010 :     if (prolog.noteCount && prolog.currentLine != firstLine) {
     992            1029 :         if (diff > SN_DELTA_MASK)
     993               9 :             cnt += JS_HOWMANY(diff - SN_DELTA_MASK, SN_XDELTA_MASK);
     994            1029 :         cnt += 2 + ((firstLine > SN_3BYTE_OFFSET_MASK) << 1);
     995          257981 :     } else if (diff > 0) {
     996           22845 :         if (main.noteCount) {
     997           22710 :             jssrcnote *sn = main.notes;
     998                 :             diff -= SN_IS_XDELTA(sn)
     999                 :                     ? SN_XDELTA_MASK - (*sn & SN_XDELTA_MASK)
    1000           22710 :                     : SN_DELTA_MASK - (*sn & SN_DELTA_MASK);
    1001                 :         }
    1002           22845 :         if (diff > 0)
    1003           19233 :             cnt += JS_HOWMANY(diff, SN_XDELTA_MASK);
    1004                 :     }
    1005          259010 :     return cnt;
    1006                 : }
    1007                 : 
    1008                 : /*
    1009                 :  * To avoid offending js_SrcNoteSpec[SRC_DECL].arity, pack the two data needed
    1010                 :  * to decompile let into one ptrdiff_t:
    1011                 :  *   offset: offset to the LEAVEBLOCK(EXPR) op (not including ENTER/LEAVE)
    1012                 :  *   groupAssign: whether this was an optimized group assign ([x,y] = [a,b])
    1013                 :  */
    1014            8416 : inline ptrdiff_t PackLetData(size_t offset, bool groupAssign)
    1015                 : {
    1016            8416 :     JS_ASSERT(offset <= (size_t(-1) >> 1));
    1017            8416 :     return ptrdiff_t(offset << 1) | ptrdiff_t(groupAssign);
    1018                 : }
    1019                 : 
    1020            3294 : inline size_t LetDataToOffset(ptrdiff_t w)
    1021                 : {
    1022            3294 :     return size_t(w) >> 1;
    1023                 : }
    1024                 : 
    1025            3888 : inline bool LetDataToGroupAssign(ptrdiff_t w)
    1026                 : {
    1027            3888 :     return size_t(w) & 1;
    1028                 : }
    1029                 : 
    1030                 : } /* namespace js */
    1031                 : 
    1032                 : struct JSSrcNoteSpec {
    1033                 :     const char      *name;      /* name for disassembly/debugging output */
    1034                 :     int8_t          arity;      /* number of offset operands */
    1035                 : };
    1036                 : 
    1037                 : extern JS_FRIEND_DATA(JSSrcNoteSpec)  js_SrcNoteSpec[];
    1038                 : extern JS_FRIEND_API(unsigned)         js_SrcNoteLength(jssrcnote *sn);
    1039                 : 
    1040                 : /*
    1041                 :  * Get and set the offset operand identified by which (0 for the first, etc.).
    1042                 :  */
    1043                 : extern JS_FRIEND_API(ptrdiff_t)
    1044                 : js_GetSrcNoteOffset(jssrcnote *sn, unsigned which);
    1045                 : 
    1046                 : #endif /* BytecodeEmitter_h__ */

Generated by: LCOV version 1.7