Commit aeb86d57 authored by Daniel Clifford's avatar Daniel Clifford Committed by Commit Bot

[torque]: Add constexpr keyword/types for compile-time evaluation

Torque expressions of type constexpr are evaluated at compile-time
rather than runtime. They are backed by C++ types rather than
TNode<X> types, so the macro functions that are called by generated
C++ code expect values to be computed when the snapshot is generated
rather than by TurboFan-generated code.

Specifically, "if" statements can have a constexpr modifier. With this
modifier, a type of "constexpr bool" is expected rather than "bool",
and in that case instead of generating a CSA BranchIf, it generates
a C++ "if (<bool expression>)" that generates code for only the true or
false path based on the bool value at torque-execution (compile time)
rather than generating both paths (including inserting phi nodes
for variables modified on either branch at the re-merge at the end
of the if) and dynamically dispatching to the true or false path
during d8/Chrome/node.js execution (runtime) using a CSA BranchIf.

Change-Id: I8238e25aaadbfc618847e04556e96a3949ea5a8d
Reviewed-on: https://chromium-review.googlesource.com/1042085
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53001}
parent 40a95443
......@@ -23,7 +23,7 @@ module array {
if (IsArraySpeciesProtectorCellInvalid()) goto Bailout;
// Fast path only works on fast elements kind and with writable length.
let elementsKind: int32 = EnsureArrayPushable(map) otherwise Bailout;
let elementsKind: ElementsKind = EnsureArrayPushable(map) otherwise Bailout;
if (!IsFastElementsKind(elementsKind)) goto Bailout;
// For now, only support non-double fast elements
......
This diff is collapsed.
......@@ -14,7 +14,7 @@ module typed_array {
extern builtin TypedArrayStoreElementFromTagged(
Context, JSTypedArray, Smi, Smi, Object);
extern macro NumberIsNaN(Number): bit;
extern macro NumberIsNaN(Number): bool;
macro CallCompareWithDetachedCheck(
context: Context, array: JSTypedArray, comparefn: Callable, a: Object,
......@@ -164,7 +164,7 @@ module typed_array {
Store(context, array, kind, low_end, element);
low_end++;
} else if (order > 0) {
let break_for: bit = no;
let break_for: bool = no;
while (order > 0) {
high_start--;
......
......@@ -223,6 +223,12 @@ class V8_EXPORT_PRIVATE CodeStubAssembler : public compiler::CodeAssembler {
TNode<Int32T> HashSeed();
Node* IntPtrOrSmiConstant(int value, ParameterMode mode);
TNode<BoolT> BoolConstant(bool value) {
return value ? Int32TrueConstant() : Int32FalseConstant();
}
TNode<Smi> LanguageModeConstant(LanguageMode mode) {
return SmiConstant(static_cast<int>(mode));
}
bool IsIntPtrOrSmiConstantZero(Node* test, ParameterMode mode);
bool TryGetIntPtrOrSmiConstantValue(Node* maybe_constant, int* value,
......
......@@ -22,6 +22,7 @@ CONVERT_KEYWORD: 'convert';
FOR: 'for';
WHILE: 'while';
RETURN: 'return';
CONSTEXPR: 'constexpr';
CONTINUE: 'continue';
BREAK: 'break';
GOTO: 'goto';
......@@ -81,7 +82,7 @@ DECREMENT: '--';
NOT: '!';
STRING_LITERAL : ('"' ( ESCAPE | ~('"' | '\\' | '\n' | '\r') ) * '"')
| ('\'' ( ESCAPE | ~('"' | '\\' | '\n' | '\r') ) * '\'');
| ('\'' ( ESCAPE | ~('\'' | '\\' | '\n' | '\r') ) * '\'');
fragment ESCAPE : '\\' ( '\'' | '\\' | '"' );
IDENTIFIER : [A-Za-z][0-9A-Za-z_]* ;
......@@ -115,7 +116,7 @@ DECIMAL_LITERAL
| MINUS? DECIMAL_INTEGER_LITERAL EXPONENT_PART?
;
type : IDENTIFIER;
type : CONSTEXPR? IDENTIFIER;
typeList : '(' type? (',' type)* ')';
typeListMaybeVarArgs: '(' type? (',' type)* (',' VARARGS)? ')'
......@@ -225,7 +226,7 @@ variableDeclaration: LET IDENTIFIER ':' type;
variableDeclarationWithInitialization: variableDeclaration ('=' expression)?;
helperCallStatement: (TAIL)? helperCall;
expressionStatement: assignment;
ifStatement: IF '(' expression ')' statementBlock ('else' statementBlock)?;
ifStatement: IF CONSTEXPR? '(' expression ')' statementBlock ('else' statementBlock)?;
whileLoop: WHILE '(' expression ')' statementBlock;
returnStatement: RETURN expression?;
breakStatement: BREAK;
......@@ -259,9 +260,10 @@ statementBlock
helperBody : statementScope;
generatesDeclaration: 'generates' STRING_LITERAL;
extendsDeclaration: 'extends' IDENTIFIER;
typeDeclaration : 'type' IDENTIFIER extendsDeclaration? generatesDeclaration? ';';
generatesDeclaration: 'generates' STRING_LITERAL;
constexprDeclaration: 'constexpr' STRING_LITERAL;
typeDeclaration : 'type' IDENTIFIER extendsDeclaration? generatesDeclaration? constexprDeclaration?';';
externalBuiltin : 'extern' JAVASCRIPT? BUILTIN IDENTIFIER typeList optionalType ';';
externalMacro : 'extern' (IMPLICIT? 'operator' STRING_LITERAL)? MACRO IDENTIFIER typeListMaybeVarArgs optionalType optionalLabelList ';';
......
......@@ -251,15 +251,20 @@ class TorqueBaseListener : public TorqueListener {
void enterHelperBody(TorqueParser::HelperBodyContext* /*ctx*/) override {}
void exitHelperBody(TorqueParser::HelperBodyContext* /*ctx*/) override {}
void enterExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* /*ctx*/) override {}
void exitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* /*ctx*/) override {}
void enterGeneratesDeclaration(
TorqueParser::GeneratesDeclarationContext* /*ctx*/) override {}
void exitGeneratesDeclaration(
TorqueParser::GeneratesDeclarationContext* /*ctx*/) override {}
void enterExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* /*ctx*/) override {}
void exitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* /*ctx*/) override {}
void enterConstexprDeclaration(
TorqueParser::ConstexprDeclarationContext* /*ctx*/) override {}
void exitConstexprDeclaration(
TorqueParser::ConstexprDeclarationContext* /*ctx*/) override {}
void enterTypeDeclaration(
TorqueParser::TypeDeclarationContext* /*ctx*/) override {}
......
......@@ -270,13 +270,18 @@ class TorqueBaseVisitor : public TorqueVisitor {
return visitChildren(ctx);
}
antlrcpp::Any visitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* ctx) override {
return visitChildren(ctx);
}
antlrcpp::Any visitGeneratesDeclaration(
TorqueParser::GeneratesDeclarationContext* ctx) override {
return visitChildren(ctx);
}
antlrcpp::Any visitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* ctx) override {
antlrcpp::Any visitConstexprDeclaration(
TorqueParser::ConstexprDeclarationContext* ctx) override {
return visitChildren(ctx);
}
......
This diff is collapsed.
......@@ -47,53 +47,54 @@ class TorqueLexer : public antlr4::Lexer {
FOR = 32,
WHILE = 33,
RETURN = 34,
CONTINUE = 35,
BREAK = 36,
GOTO = 37,
OTHERWISE = 38,
TRY = 39,
CATCH = 40,
LABEL = 41,
LABELS = 42,
TAIL = 43,
ISNT = 44,
IS = 45,
LET = 46,
ASSERT = 47,
UNREACHABLE_TOKEN = 48,
DEBUG_TOKEN = 49,
ASSIGNMENT = 50,
ASSIGNMENT_OPERATOR = 51,
EQUAL = 52,
PLUS = 53,
MINUS = 54,
MULTIPLY = 55,
DIVIDE = 56,
MODULO = 57,
BIT_OR = 58,
BIT_AND = 59,
BIT_NOT = 60,
MAX = 61,
MIN = 62,
NOT_EQUAL = 63,
LESS_THAN = 64,
LESS_THAN_EQUAL = 65,
GREATER_THAN = 66,
GREATER_THAN_EQUAL = 67,
SHIFT_LEFT = 68,
SHIFT_RIGHT = 69,
SHIFT_RIGHT_ARITHMETIC = 70,
VARARGS = 71,
EQUALITY_OPERATOR = 72,
INCREMENT = 73,
DECREMENT = 74,
NOT = 75,
STRING_LITERAL = 76,
IDENTIFIER = 77,
WS = 78,
BLOCK_COMMENT = 79,
LINE_COMMENT = 80,
DECIMAL_LITERAL = 81
CONSTEXPR = 35,
CONTINUE = 36,
BREAK = 37,
GOTO = 38,
OTHERWISE = 39,
TRY = 40,
CATCH = 41,
LABEL = 42,
LABELS = 43,
TAIL = 44,
ISNT = 45,
IS = 46,
LET = 47,
ASSERT = 48,
UNREACHABLE_TOKEN = 49,
DEBUG_TOKEN = 50,
ASSIGNMENT = 51,
ASSIGNMENT_OPERATOR = 52,
EQUAL = 53,
PLUS = 54,
MINUS = 55,
MULTIPLY = 56,
DIVIDE = 57,
MODULO = 58,
BIT_OR = 59,
BIT_AND = 60,
BIT_NOT = 61,
MAX = 62,
MIN = 63,
NOT_EQUAL = 64,
LESS_THAN = 65,
LESS_THAN_EQUAL = 66,
GREATER_THAN = 67,
GREATER_THAN_EQUAL = 68,
SHIFT_LEFT = 69,
SHIFT_RIGHT = 70,
SHIFT_RIGHT_ARITHMETIC = 71,
VARARGS = 72,
EQUALITY_OPERATOR = 73,
INCREMENT = 74,
DECREMENT = 75,
NOT = 76,
STRING_LITERAL = 77,
IDENTIFIER = 78,
WS = 79,
BLOCK_COMMENT = 80,
LINE_COMMENT = 81,
DECIMAL_LITERAL = 82
};
explicit TorqueLexer(antlr4::CharStream* input);
......
......@@ -236,15 +236,20 @@ class TorqueListener : public antlr4::tree::ParseTreeListener {
virtual void enterHelperBody(TorqueParser::HelperBodyContext* ctx) = 0;
virtual void exitHelperBody(TorqueParser::HelperBodyContext* ctx) = 0;
virtual void enterExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* ctx) = 0;
virtual void exitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* ctx) = 0;
virtual void enterGeneratesDeclaration(
TorqueParser::GeneratesDeclarationContext* ctx) = 0;
virtual void exitGeneratesDeclaration(
TorqueParser::GeneratesDeclarationContext* ctx) = 0;
virtual void enterExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* ctx) = 0;
virtual void exitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* ctx) = 0;
virtual void enterConstexprDeclaration(
TorqueParser::ConstexprDeclarationContext* ctx) = 0;
virtual void exitConstexprDeclaration(
TorqueParser::ConstexprDeclarationContext* ctx) = 0;
virtual void enterTypeDeclaration(
TorqueParser::TypeDeclarationContext* ctx) = 0;
......
This diff is collapsed.
......@@ -47,53 +47,54 @@ class TorqueParser : public antlr4::Parser {
FOR = 32,
WHILE = 33,
RETURN = 34,
CONTINUE = 35,
BREAK = 36,
GOTO = 37,
OTHERWISE = 38,
TRY = 39,
CATCH = 40,
LABEL = 41,
LABELS = 42,
TAIL = 43,
ISNT = 44,
IS = 45,
LET = 46,
ASSERT = 47,
UNREACHABLE_TOKEN = 48,
DEBUG_TOKEN = 49,
ASSIGNMENT = 50,
ASSIGNMENT_OPERATOR = 51,
EQUAL = 52,
PLUS = 53,
MINUS = 54,
MULTIPLY = 55,
DIVIDE = 56,
MODULO = 57,
BIT_OR = 58,
BIT_AND = 59,
BIT_NOT = 60,
MAX = 61,
MIN = 62,
NOT_EQUAL = 63,
LESS_THAN = 64,
LESS_THAN_EQUAL = 65,
GREATER_THAN = 66,
GREATER_THAN_EQUAL = 67,
SHIFT_LEFT = 68,
SHIFT_RIGHT = 69,
SHIFT_RIGHT_ARITHMETIC = 70,
VARARGS = 71,
EQUALITY_OPERATOR = 72,
INCREMENT = 73,
DECREMENT = 74,
NOT = 75,
STRING_LITERAL = 76,
IDENTIFIER = 77,
WS = 78,
BLOCK_COMMENT = 79,
LINE_COMMENT = 80,
DECIMAL_LITERAL = 81
CONSTEXPR = 35,
CONTINUE = 36,
BREAK = 37,
GOTO = 38,
OTHERWISE = 39,
TRY = 40,
CATCH = 41,
LABEL = 42,
LABELS = 43,
TAIL = 44,
ISNT = 45,
IS = 46,
LET = 47,
ASSERT = 48,
UNREACHABLE_TOKEN = 49,
DEBUG_TOKEN = 50,
ASSIGNMENT = 51,
ASSIGNMENT_OPERATOR = 52,
EQUAL = 53,
PLUS = 54,
MINUS = 55,
MULTIPLY = 56,
DIVIDE = 57,
MODULO = 58,
BIT_OR = 59,
BIT_AND = 60,
BIT_NOT = 61,
MAX = 62,
MIN = 63,
NOT_EQUAL = 64,
LESS_THAN = 65,
LESS_THAN_EQUAL = 66,
GREATER_THAN = 67,
GREATER_THAN_EQUAL = 68,
SHIFT_LEFT = 69,
SHIFT_RIGHT = 70,
SHIFT_RIGHT_ARITHMETIC = 71,
VARARGS = 72,
EQUALITY_OPERATOR = 73,
INCREMENT = 74,
DECREMENT = 75,
NOT = 76,
STRING_LITERAL = 77,
IDENTIFIER = 78,
WS = 79,
BLOCK_COMMENT = 80,
LINE_COMMENT = 81,
DECIMAL_LITERAL = 82
};
enum {
......@@ -150,18 +151,19 @@ class TorqueParser : public antlr4::Parser {
RuleStatementScope = 50,
RuleStatementBlock = 51,
RuleHelperBody = 52,
RuleGeneratesDeclaration = 53,
RuleExtendsDeclaration = 54,
RuleTypeDeclaration = 55,
RuleExternalBuiltin = 56,
RuleExternalMacro = 57,
RuleExternalRuntime = 58,
RuleBuiltinDeclaration = 59,
RuleMacroDeclaration = 60,
RuleConstDeclaration = 61,
RuleDeclaration = 62,
RuleModuleDeclaration = 63,
RuleFile = 64
RuleExtendsDeclaration = 53,
RuleGeneratesDeclaration = 54,
RuleConstexprDeclaration = 55,
RuleTypeDeclaration = 56,
RuleExternalBuiltin = 57,
RuleExternalMacro = 58,
RuleExternalRuntime = 59,
RuleBuiltinDeclaration = 60,
RuleMacroDeclaration = 61,
RuleConstDeclaration = 62,
RuleDeclaration = 63,
RuleModuleDeclaration = 64,
RuleFile = 65
};
explicit TorqueParser(antlr4::TokenStream* input);
......@@ -228,8 +230,9 @@ class TorqueParser : public antlr4::Parser {
class StatementScopeContext;
class StatementBlockContext;
class HelperBodyContext;
class GeneratesDeclarationContext;
class ExtendsDeclarationContext;
class GeneratesDeclarationContext;
class ConstexprDeclarationContext;
class TypeDeclarationContext;
class ExternalBuiltinContext;
class ExternalMacroContext;
......@@ -246,6 +249,7 @@ class TorqueParser : public antlr4::Parser {
TypeContext(antlr4::ParserRuleContext* parent, size_t invokingState);
size_t getRuleIndex() const override;
antlr4::tree::TerminalNode* IDENTIFIER();
antlr4::tree::TerminalNode* CONSTEXPR();
void enterRule(antlr4::tree::ParseTreeListener* listener) override;
void exitRule(antlr4::tree::ParseTreeListener* listener) override;
......@@ -916,6 +920,7 @@ class TorqueParser : public antlr4::Parser {
ExpressionContext* expression();
std::vector<StatementBlockContext*> statementBlock();
StatementBlockContext* statementBlock(size_t i);
antlr4::tree::TerminalNode* CONSTEXPR();
void enterRule(antlr4::tree::ParseTreeListener* listener) override;
void exitRule(antlr4::tree::ParseTreeListener* listener) override;
......@@ -1147,6 +1152,21 @@ class TorqueParser : public antlr4::Parser {
HelperBodyContext* helperBody();
class ExtendsDeclarationContext : public antlr4::ParserRuleContext {
public:
ExtendsDeclarationContext(antlr4::ParserRuleContext* parent,
size_t invokingState);
size_t getRuleIndex() const override;
antlr4::tree::TerminalNode* IDENTIFIER();
void enterRule(antlr4::tree::ParseTreeListener* listener) override;
void exitRule(antlr4::tree::ParseTreeListener* listener) override;
antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor* visitor) override;
};
ExtendsDeclarationContext* extendsDeclaration();
class GeneratesDeclarationContext : public antlr4::ParserRuleContext {
public:
GeneratesDeclarationContext(antlr4::ParserRuleContext* parent,
......@@ -1162,12 +1182,12 @@ class TorqueParser : public antlr4::Parser {
GeneratesDeclarationContext* generatesDeclaration();
class ExtendsDeclarationContext : public antlr4::ParserRuleContext {
class ConstexprDeclarationContext : public antlr4::ParserRuleContext {
public:
ExtendsDeclarationContext(antlr4::ParserRuleContext* parent,
size_t invokingState);
ConstexprDeclarationContext(antlr4::ParserRuleContext* parent,
size_t invokingState);
size_t getRuleIndex() const override;
antlr4::tree::TerminalNode* IDENTIFIER();
antlr4::tree::TerminalNode* STRING_LITERAL();
void enterRule(antlr4::tree::ParseTreeListener* listener) override;
void exitRule(antlr4::tree::ParseTreeListener* listener) override;
......@@ -1175,7 +1195,7 @@ class TorqueParser : public antlr4::Parser {
antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor* visitor) override;
};
ExtendsDeclarationContext* extendsDeclaration();
ConstexprDeclarationContext* constexprDeclaration();
class TypeDeclarationContext : public antlr4::ParserRuleContext {
public:
......@@ -1185,6 +1205,7 @@ class TorqueParser : public antlr4::Parser {
antlr4::tree::TerminalNode* IDENTIFIER();
ExtendsDeclarationContext* extendsDeclaration();
GeneratesDeclarationContext* generatesDeclaration();
ConstexprDeclarationContext* constexprDeclaration();
void enterRule(antlr4::tree::ParseTreeListener* listener) override;
void exitRule(antlr4::tree::ParseTreeListener* listener) override;
......
......@@ -177,11 +177,14 @@ class TorqueVisitor : public antlr4::tree::AbstractParseTreeVisitor {
virtual antlrcpp::Any visitHelperBody(
TorqueParser::HelperBodyContext* context) = 0;
virtual antlrcpp::Any visitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* context) = 0;
virtual antlrcpp::Any visitGeneratesDeclaration(
TorqueParser::GeneratesDeclarationContext* context) = 0;
virtual antlrcpp::Any visitExtendsDeclaration(
TorqueParser::ExtendsDeclarationContext* context) = 0;
virtual antlrcpp::Any visitConstexprDeclaration(
TorqueParser::ConstexprDeclarationContext* context) = 0;
virtual antlrcpp::Any visitTypeDeclaration(
TorqueParser::TypeDeclarationContext* context) = 0;
......
......@@ -13,10 +13,17 @@ namespace torque {
namespace {
std::string GetType(TorqueParser::TypeContext* context) {
if (!context) return "void";
std::string result(context->CONSTEXPR() == nullptr ? ""
: CONSTEXPR_TYPE_PREFIX);
result += context->IDENTIFIER()->getSymbol()->getText();
return result;
}
std::string GetOptionalType(TorqueParser::OptionalTypeContext* context) {
if (!context) return "";
if (!context->type()) return "void";
return context->type()->IDENTIFIER()->getSymbol()->getText();
return GetType(context->type());
}
LabelAndTypesVector GetOptionalLabelAndTypeList(
......@@ -28,8 +35,7 @@ LabelAndTypesVector GetOptionalLabelAndTypeList(
new_label.name = label->IDENTIFIER()->getSymbol()->getText();
if (label->typeList() != nullptr) {
for (auto& type : label->typeList()->type()) {
new_label.types.emplace_back(
type->IDENTIFIER()->getSymbol()->getText());
new_label.types.emplace_back(GetType(type));
}
}
labels.emplace_back(new_label);
......@@ -95,8 +101,7 @@ antlrcpp::Any AstGenerator::visitParameterList(
for (auto* parameter : context->parameter()) {
parameter->accept(this);
result.names.push_back(parameter->IDENTIFIER()->getSymbol()->getText());
result.types.push_back(
parameter->type()->IDENTIFIER()->getSymbol()->getText());
result.types.push_back(GetType(parameter->type()));
}
return std::move(result);
}
......@@ -106,7 +111,7 @@ antlrcpp::Any AstGenerator::visitTypeList(
ParameterList result{{}, {}, false, {}};
result.types.reserve(context->type().size());
for (auto* type : context->type()) {
result.types.push_back(type->IDENTIFIER()->getSymbol()->getText());
result.types.push_back(GetType(type));
}
return std::move(result);
}
......@@ -116,7 +121,7 @@ antlrcpp::Any AstGenerator::visitTypeListMaybeVarArgs(
ParameterList result{{}, {}, context->VARARGS(), {}};
result.types.reserve(context->type().size());
for (auto* type : context->type()) {
result.types.push_back(type->IDENTIFIER()->getSymbol()->getText());
result.types.push_back(GetType(type));
}
return std::move(result);
}
......@@ -193,7 +198,7 @@ antlrcpp::Any AstGenerator::visitConstDeclaration(
TorqueParser::ConstDeclarationContext* context) {
return implicit_cast<Declaration*>(RegisterNode(new ConstDeclaration{
Pos(context), context->IDENTIFIER()->getSymbol()->getText(),
context->type()->IDENTIFIER()->getSymbol()->getText(),
GetType(context->type()),
StringLiteralUnquote(
context->STRING_LITERAL()->getSymbol()->getText())}));
}
......@@ -211,16 +216,23 @@ antlrcpp::Any AstGenerator::visitTypeDeclaration(
->getSymbol()
->getText());
}
if (context->constexprDeclaration()) {
result->constexpr_generates =
StringLiteralUnquote(context->constexprDeclaration()
->STRING_LITERAL()
->getSymbol()
->getText());
}
return implicit_cast<Declaration*>(result);
}
antlrcpp::Any AstGenerator::visitVariableDeclaration(
TorqueParser::VariableDeclarationContext* context) {
return RegisterNode(new VarDeclarationStatement{
Pos(context),
context->IDENTIFIER()->getSymbol()->getText(),
context->type()->IDENTIFIER()->getSymbol()->getText(),
{}});
return RegisterNode(
new VarDeclarationStatement{Pos(context),
context->IDENTIFIER()->getSymbol()->getText(),
GetType(context->type()),
{}});
}
antlrcpp::Any AstGenerator::visitVariableDeclarationWithInitialization(
......@@ -327,6 +339,7 @@ antlrcpp::Any AstGenerator::visitIfStatement(
IfStatement* result = RegisterNode(new IfStatement{
Pos(context),
std::move(context->expression()->accept(this).as<Expression*>()),
context->CONSTEXPR() != nullptr,
std::move(context->statementBlock(0)->accept(this).as<Statement*>()),
{}});
if (context->statementBlock(1))
......@@ -425,11 +438,11 @@ antlrcpp::Any AstGenerator::visitPrimaryExpression(
new StringLiteralExpression{Pos(context), e->getSymbol()->getText()}));
if (context->CONVERT_KEYWORD())
return implicit_cast<Expression*>(RegisterNode(new ConvertExpression{
Pos(context), context->type()->IDENTIFIER()->getSymbol()->getText(),
Pos(context), GetType(context->type()),
context->expression()->accept(this).as<Expression*>()}));
if (context->CAST_KEYWORD())
return implicit_cast<Expression*>(RegisterNode(new CastExpression{
Pos(context), context->type()->IDENTIFIER()->getSymbol()->getText(),
Pos(context), GetType(context->type()),
context->IDENTIFIER()->getSymbol()->getText(),
context->expression()->accept(this).as<Expression*>()}));
return context->expression()->accept(this);
......
......@@ -334,10 +334,15 @@ struct ExpressionStatement : Statement {
struct IfStatement : Statement {
DEFINE_AST_NODE_LEAF_BOILERPLATE(IfStatement)
IfStatement(SourcePosition p, Expression* c, Statement* t,
IfStatement(SourcePosition p, Expression* c, bool cexpr, Statement* t,
base::Optional<Statement*> f)
: Statement(kKind, p), condition(c), if_true(t), if_false(f) {}
: Statement(kKind, p),
condition(c),
is_constexpr(cexpr),
if_true(t),
if_false(f) {}
Expression* condition;
bool is_constexpr;
Statement* if_true;
base::Optional<Statement*> if_false;
};
......@@ -501,6 +506,7 @@ struct TypeDeclaration : Declaration {
std::string name;
base::Optional<std::string> extends;
base::Optional<std::string> generates;
base::Optional<std::string> constexpr_generates;
};
struct LabelAndTypes {
......
......@@ -72,10 +72,19 @@ class DeclarationVisitor : public FileVisitor {
void Visit(TailCallStatement* stmt) { Visit(stmt->call); }
void Visit(TypeDeclaration* decl) {
std::string generates_class_name =
decl->generates ? *decl->generates : decl->name;
declarations()->DeclareType(decl->pos, decl->name, generates_class_name,
decl->extends ? &*decl->extends : nullptr);
std::string extends = decl->extends ? *decl->extends : std::string("");
std::string* extends_ptr = decl->extends ? &extends : nullptr;
std::string generates =
decl->generates ? *decl->generates : std::string("");
declarations()->DeclareType(decl->pos, decl->name, generates, extends_ptr);
if (decl->constexpr_generates) {
std::string constexpr_name =
std::string(CONSTEXPR_TYPE_PREFIX) + decl->name;
declarations()->DeclareType(decl->pos, constexpr_name,
*decl->constexpr_generates, &(decl->name));
}
}
void Visit(ExternalBuiltinDeclaration* decl) {
......@@ -125,6 +134,7 @@ class DeclarationVisitor : public FileVisitor {
std::cout << "found declaration of external runtime " << decl->name
<< " with signature ";
}
Type return_type = declarations()->LookupType(decl->pos, decl->return_type);
TypeVector parameter_types =
GetTypeVector(decl->pos, decl->parameters.types);
......@@ -260,12 +270,16 @@ class DeclarationVisitor : public FileVisitor {
}
void Visit(IfStatement* stmt) {
PushControlSplit();
if (!stmt->is_constexpr) {
PushControlSplit();
}
DeclareExpressionForBranch(stmt->condition);
Visit(stmt->if_true);
if (stmt->if_false) Visit(*stmt->if_false);
auto changed_vars = PopControlSplit();
global_context_.AddControlSplitChangedVariables(stmt, changed_vars);
if (!stmt->is_constexpr) {
auto changed_vars = PopControlSplit();
global_context_.AddControlSplitChangedVariables(stmt, changed_vars);
}
}
void Visit(WhileStatement* stmt) {
......
......@@ -341,7 +341,7 @@ VisitResult ImplementationVisitor::Visit(LogicalOrExpression* expr) {
Label::cast(declarations()->LookupValue(expr->pos, kFalseLabelName));
GenerateLabelDefinition(false_label);
VisitResult left_result = Visit(expr->left);
if (left_result.type().IsBit()) {
if (left_result.type().IsBool()) {
Label* true_label =
Label::cast(declarations()->LookupValue(expr->pos, kTrueLabelName));
GenerateIndent();
......@@ -361,7 +361,7 @@ VisitResult ImplementationVisitor::Visit(LogicalAndExpression* expr) {
Label::cast(declarations()->LookupValue(expr->pos, kTrueLabelName));
GenerateLabelDefinition(true_label);
VisitResult left_result = Visit(expr->left);
if (left_result.type().IsBit()) {
if (left_result.type().IsBool()) {
Label* false_label =
Label::cast(declarations()->LookupValue(expr->pos, kFalseLabelName));
GenerateIndent();
......@@ -484,38 +484,70 @@ Type ImplementationVisitor::Visit(IfStatement* stmt) {
bool has_else = stmt->if_false.has_value();
Label* true_label = nullptr;
Label* false_label = nullptr;
{
Declarations::NodeScopeActivator scope(declarations(), &*stmt->condition);
true_label =
Label::cast(declarations()->LookupValue(stmt->pos, kTrueLabelName));
GenerateLabelDefinition(true_label);
false_label =
Label::cast(declarations()->LookupValue(stmt->pos, kFalseLabelName));
GenerateLabelDefinition(false_label, !has_else ? stmt : nullptr);
}
if (stmt->is_constexpr) {
VisitResult expression_result = Visit(stmt->condition);
Label* done_label = nullptr;
bool live = false;
if (has_else) {
done_label =
declarations()->DeclarePrivateLabel(stmt->pos, "if_done_label");
GenerateLabelDefinition(done_label, stmt);
if (!expression_result.type().Is(GetTypeOracle().GetConstexprBoolType())) {
std::stringstream stream;
stream
<< "expression should return type \"constexpr bool\" but doesn't at"
<< PositionAsString(stmt->pos);
ReportError(stream.str());
}
{
GenerateIndent();
source_out() << "if ((" << expression_result.variable() << ")) ";
ScopedIndent indent(this, false);
source_out() << std::endl;
Visit(stmt->if_true);
}
if (has_else) {
source_out() << " else ";
ScopedIndent indent(this, false);
source_out() << std::endl;
Visit(*stmt->if_false);
}
source_out() << std::endl;
return GetTypeOracle().GetVoidType();
} else {
done_label = false_label;
live = true;
}
std::vector<Statement*> blocks = {stmt->if_true};
std::vector<Label*> labels = {true_label, false_label};
if (has_else) blocks.push_back(*stmt->if_false);
if (GenerateExpressionBranch(stmt->condition, labels, blocks, done_label)) {
live = true;
}
if (live) {
GenerateLabelBind(done_label);
Label* true_label = nullptr;
Label* false_label = nullptr;
{
Declarations::NodeScopeActivator scope(declarations(), &*stmt->condition);
true_label =
Label::cast(declarations()->LookupValue(stmt->pos, kTrueLabelName));
GenerateLabelDefinition(true_label);
false_label =
Label::cast(declarations()->LookupValue(stmt->pos, kFalseLabelName));
GenerateLabelDefinition(false_label, !has_else ? stmt : nullptr);
}
Label* done_label = nullptr;
bool live = false;
if (has_else) {
done_label =
declarations()->DeclarePrivateLabel(stmt->pos, "if_done_label");
GenerateLabelDefinition(done_label, stmt);
} else {
done_label = false_label;
live = true;
}
std::vector<Statement*> blocks = {stmt->if_true};
std::vector<Label*> labels = {true_label, false_label};
if (has_else) blocks.push_back(*stmt->if_false);
if (GenerateExpressionBranch(stmt->condition, labels, blocks, done_label)) {
live = true;
}
if (live) {
GenerateLabelBind(done_label);
}
return live ? GetTypeOracle().GetVoidType()
: GetTypeOracle().GetNeverType();
}
return live ? GetTypeOracle().GetVoidType() : GetTypeOracle().GetNeverType();
}
Type ImplementationVisitor::Visit(WhileStatement* stmt) {
......@@ -605,7 +637,7 @@ Type ImplementationVisitor::Visit(AssertStatement* stmt) {
Expression* expression = stmt->expression;
VisitResult expression_result = Visit(stmt->expression);
if (expression_result.type() == GetTypeOracle().GetBitType()) {
if (expression_result.type() == GetTypeOracle().GetBoolType()) {
GenerateBranch(expression_result, true_label, false_label);
} else {
if (expression_result.type() != GetTypeOracle().GetNeverType()) {
......@@ -1009,7 +1041,8 @@ VisitResult ImplementationVisitor::GenerateOperation(
}
}
if (!return_type || (return_type->IsSubclass(handler.result_type))) {
if (!return_type || (GetTypeOracle().IsAssignableFrom(
*return_type, handler.result_type))) {
return GenerateCall(pos, handler.macro_name, arguments, false);
}
}
......@@ -1362,7 +1395,7 @@ bool ImplementationVisitor::GenerateExpressionBranch(
Declarations::NodeScopeActivator scope(declarations(), expression);
VisitResult expression_result = Visit(expression);
if (expression_result.type() == GetTypeOracle().GetBitType()) {
if (expression_result.type() == GetTypeOracle().GetBoolType()) {
GenerateBranch(expression_result, statement_labels[0], statement_labels[1]);
} else {
if (expression_result.type() != GetTypeOracle().GetNeverType()) {
......
......@@ -25,7 +25,11 @@ class TypeOracle {
Type GetArgumentsType() { return GetBuiltinType(ARGUMENTS_TYPE_STRING); }
Type GetBitType() { return GetBuiltinType(BIT_TYPE_STRING); }
Type GetBoolType() { return GetBuiltinType(BOOL_TYPE_STRING); }
Type GetConstexprBoolType() {
return GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING);
}
Type GetVoidType() { return GetBuiltinType(VOID_TYPE_STRING); }
......@@ -40,7 +44,8 @@ class TypeOracle {
Type GetConstInt31Type() { return GetBuiltinType(CONST_INT31_TYPE_STRING); }
bool IsAssignableFrom(Type to, Type from) {
if (to.IsSubclass(from)) return true;
if (to == from) return true;
if (to.IsSubclass(from) && !from.IsConstexpr()) return true;
return IsImplicitlyConverableFrom(to, from);
}
......
......@@ -14,9 +14,11 @@ namespace v8 {
namespace internal {
namespace torque {
static const char* const CONSTEXPR_TYPE_PREFIX = "constexpr ";
static const char* const NEVER_TYPE_STRING = "never";
static const char* const BRANCH_TYPE_STRING = "branch";
static const char* const BIT_TYPE_STRING = "bit";
static const char* const CONSTEXPR_BOOL_TYPE_STRING = "constexpr bool";
static const char* const BOOL_TYPE_STRING = "bool";
static const char* const VOID_TYPE_STRING = "void";
static const char* const ARGUMENTS_TYPE_STRING = "Arguments";
static const char* const TAGGED_TYPE_STRING = "tagged";
......@@ -25,9 +27,9 @@ static const char* const EXCEPTION_TYPE_STRING = "Exception";
static const char* const OBJECT_TYPE_STRING = "Object";
static const char* const STRING_TYPE_STRING = "String";
static const char* const INTPTR_TYPE_STRING = "intptr";
static const char* const CONST_INT31_TYPE_STRING = "const_int31";
static const char* const CONST_INT32_TYPE_STRING = "const_int32";
static const char* const CONST_FLOAT64_TYPE_STRING = "const_float64";
static const char* const CONST_INT31_TYPE_STRING = "constexpr int31";
static const char* const CONST_INT32_TYPE_STRING = "constexpr int32";
static const char* const CONST_FLOAT64_TYPE_STRING = "constexpr float64";
class Label;
......@@ -47,9 +49,14 @@ typedef struct Type {
bool IsException() const { return name() == EXCEPTION_TYPE_STRING; }
bool IsVoid() const { return name() == VOID_TYPE_STRING; }
bool IsNever() const { return name() == NEVER_TYPE_STRING; }
bool IsBit() const { return name() == BIT_TYPE_STRING; }
bool IsBool() const { return name() == BOOL_TYPE_STRING; }
bool IsVoidOrNever() const { return IsVoid() || IsNever(); }
bool IsConstexpr() const {
return name().substr(0, strlen(CONSTEXPR_TYPE_PREFIX)) ==
CONSTEXPR_TYPE_PREFIX;
}
const std::string& name() const;
const std::string& GetGeneratedTypeName() const;
......
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