Commit e3c2546b authored by oliver@apple.com's avatar oliver@apple.com

2010-07-10 Oliver Hunt <oliver@apple.com>

        Reviewed by Gavin Barraclough.

        Remove switches from inner expression loops in the parser
        https://bugs.webkit.org/show_bug.cgi?id=42035

        Use bitmasks and flags on the token types to identify unary and
        binary operators, rather than switching on the token type to
        identify them.

        * parser/JSParser.cpp:
        (JSC::isUnaryOp):
        (JSC::JSParser::isBinaryOperator):
        * parser/JSParser.h:
        (JSC::):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@63055 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent f027163b
2010-07-10 Oliver Hunt <oliver@apple.com>
Reviewed by Gavin Barraclough.
Remove switches from inner expression loops in the parser
https://bugs.webkit.org/show_bug.cgi?id=42035
Use bitmasks and flags on the token types to identify unary and
binary operators, rather than switching on the token type to
identify them.
* parser/JSParser.cpp:
(JSC::isUnaryOp):
(JSC::JSParser::isBinaryOperator):
* parser/JSParser.h:
(JSC::):
2010-07-09 Leon Clarke <leonclarke@google.com>
Reviewed by Adam Barth.
......
......@@ -59,6 +59,8 @@ namespace JSC {
#define TreeProperty typename TreeBuilder::Property
#define TreePropertyList typename TreeBuilder::PropertyList
COMPILE_ASSERT(LastUntaggedToken < 64, LessThan64UntaggedTokens);
// This matches v8
static const ptrdiff_t kMaxParserStackUsage = 128 * sizeof(void*) * 1024;
......@@ -160,7 +162,7 @@ private:
template <class TreeBuilder> ALWAYS_INLINE TreeConstDeclList parseConstDeclarationList(TreeBuilder& context);
enum FunctionRequirements { FunctionNoRequirements, FunctionNeedsName };
template <FunctionRequirements, class TreeBuilder> bool parseFunctionInfo(TreeBuilder&, const Identifier*&, TreeFormalParameterList&, TreeFunctionBody&, int& openBrace, int& closeBrace, int& bodyStartLine);
int isBinaryOperator(JSTokenType token);
ALWAYS_INLINE int isBinaryOperator(JSTokenType token);
bool allowAutomaticSemicolon();
bool autoSemiColon()
......@@ -1029,80 +1031,16 @@ template <class TreeBuilder> TreeExpression JSParser::parseConditionalExpression
return context.createConditionalExpr(cond, lhs, rhs);
}
static bool isUnaryOp(JSTokenType token)
ALWAYS_INLINE static bool isUnaryOp(JSTokenType token)
{
switch (token) {
case EXCLAMATION:
case TILDE:
case MINUS:
case PLUS:
case PLUSPLUS:
case AUTOPLUSPLUS:
case MINUSMINUS:
case AUTOMINUSMINUS:
case TYPEOF:
case VOIDTOKEN:
case DELETETOKEN:
return true;
default:
return false;
}
return token & UnaryOpTokenFlag;
}
int JSParser::isBinaryOperator(JSTokenType token)
{
switch (token) {
case OR:
return 1;
case AND:
return 2;
case BITOR:
return 3;
case BITXOR:
return 4;
case BITAND:
return 5;
case EQEQ:
case NE:
case STREQ:
case STRNEQ:
return 6;
case LT:
case GT:
case LE:
case GE:
case INSTANCEOF:
return 7;
case INTOKEN:
// same precedence as the above but needs a validity check
if (m_allowsIn)
return 7;
return 0;
case LSHIFT:
case RSHIFT:
case URSHIFT:
return 8;
case PLUS:
case MINUS:
return 9;
case TIMES:
case DIVIDE:
case MOD:
return 10;
default:
return 0;
}
if (m_allowsIn)
return token & (BinaryOpTokenPrecedenceMask << BinaryOpTokenAllowsInPrecedenceAdditionalShift);
return token & BinaryOpTokenPrecedenceMask;
}
template <class TreeBuilder> TreeExpression JSParser::parseBinaryExpression(TreeBuilder& context)
......
......@@ -32,6 +32,16 @@ class Identifier;
class JSGlobalData;
class SourceCode;
enum {
UnaryOpTokenFlag = 64,
BinaryOpTokenPrecedenceShift = 7,
BinaryOpTokenAllowsInPrecedenceAdditionalShift = 4,
BinaryOpTokenPrecedenceMask = 15 << BinaryOpTokenPrecedenceShift
};
#define BINARY_OP_PRECEDENCE(prec) (((prec) << BinaryOpTokenPrecedenceShift) | ((prec) << (BinaryOpTokenPrecedenceShift + BinaryOpTokenAllowsInPrecedenceAdditionalShift)))
#define IN_OP_PRECEDENCE(prec) ((prec) << (BinaryOpTokenPrecedenceShift + BinaryOpTokenAllowsInPrecedenceAdditionalShift))
enum JSTokenType {
NULLTOKEN,
TRUETOKEN,
......@@ -46,15 +56,10 @@ enum JSTokenType {
CONTINUE,
FUNCTION,
RETURN,
VOIDTOKEN,
DELETETOKEN,
IF,
THISTOKEN,
DO,
WHILE,
INTOKEN,
INSTANCEOF,
TYPEOF,
SWITCH,
WITH,
RESERVED,
......@@ -63,43 +68,7 @@ enum JSTokenType {
CATCH,
FINALLY,
DEBUGGER,
IF_WITHOUT_ELSE,
ELSE,
EQUAL,
EQEQ,
NE,
STREQ,
STRNEQ,
LT,
GT,
LE,
GE,
OR,
AND,
PLUSPLUS,
MINUSMINUS,
LSHIFT,
RSHIFT,
URSHIFT,
PLUSEQUAL,
MINUSEQUAL,
MULTEQUAL,
DIVEQUAL,
LSHIFTEQUAL,
RSHIFTEQUAL,
URSHIFTEQUAL,
ANDEQUAL,
MODEQUAL,
XOREQUAL,
OREQUAL,
BITOR,
BITAND,
BITXOR,
PLUS,
MINUS,
TIMES,
DIVIDE,
MOD,
OPENBRACE,
CLOSEBRACE,
OPENPAREN,
......@@ -108,18 +77,61 @@ enum JSTokenType {
CLOSEBRACKET,
COMMA,
QUESTION,
TILDE,
EXCLAMATION,
NUMBER,
IDENT,
STRING,
AUTOPLUSPLUS,
AUTOMINUSMINUS,
SEMICOLON,
COLON,
DOT,
ERRORTOK,
EOFTOK
EOFTOK,
EQUAL,
PLUSEQUAL,
MINUSEQUAL,
MULTEQUAL,
DIVEQUAL,
LSHIFTEQUAL,
RSHIFTEQUAL,
URSHIFTEQUAL,
ANDEQUAL,
MODEQUAL,
XOREQUAL,
OREQUAL,
LastUntaggedToken,
// Begin tagged tokens
PLUSPLUS = 0 | UnaryOpTokenFlag,
MINUSMINUS = 1 | UnaryOpTokenFlag,
EXCLAMATION = 2 | UnaryOpTokenFlag,
TILDE = 3 | UnaryOpTokenFlag,
AUTOPLUSPLUS = 4 | UnaryOpTokenFlag,
AUTOMINUSMINUS = 5 | UnaryOpTokenFlag,
TYPEOF = 6 | UnaryOpTokenFlag,
VOIDTOKEN = 7 | UnaryOpTokenFlag,
DELETETOKEN = 8 | UnaryOpTokenFlag,
OR = 0 | BINARY_OP_PRECEDENCE(1),
AND = 1 | BINARY_OP_PRECEDENCE(2),
BITOR = 2 | BINARY_OP_PRECEDENCE(3),
BITXOR = 3 | BINARY_OP_PRECEDENCE(4),
BITAND = 4 | BINARY_OP_PRECEDENCE(5),
EQEQ = 5 | BINARY_OP_PRECEDENCE(6),
NE = 6 | BINARY_OP_PRECEDENCE(6),
STREQ = 7 | BINARY_OP_PRECEDENCE(6),
STRNEQ = 8 | BINARY_OP_PRECEDENCE(6),
LT = 9 | BINARY_OP_PRECEDENCE(7),
GT = 10 | BINARY_OP_PRECEDENCE(7),
LE = 11 | BINARY_OP_PRECEDENCE(7),
GE = 12 | BINARY_OP_PRECEDENCE(7),
INSTANCEOF = 13 | BINARY_OP_PRECEDENCE(7),
INTOKEN = 14 | IN_OP_PRECEDENCE(7),
LSHIFT = 15 | BINARY_OP_PRECEDENCE(8),
RSHIFT = 16 | BINARY_OP_PRECEDENCE(8),
URSHIFT = 17 | BINARY_OP_PRECEDENCE(8),
PLUS = 18 | BINARY_OP_PRECEDENCE(9) | UnaryOpTokenFlag,
MINUS = 19 | BINARY_OP_PRECEDENCE(9) | UnaryOpTokenFlag,
TIMES = 20 | BINARY_OP_PRECEDENCE(10),
DIVIDE = 21 | BINARY_OP_PRECEDENCE(10),
MOD = 22 | BINARY_OP_PRECEDENCE(10)
};
union JSTokenData {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment