Revert "Revert "moved SkSL BoolLiteral data into IRNode""
This reverts commit 5648572f4a
.
Change-Id: If9b1f46f4d4d94beca6953d0fef3b8d79c88572d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/320059
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
parent
7a6935a528
commit
59d660c075
@ -901,7 +901,7 @@ bool ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b, bool di
|
||||
|
||||
void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
|
||||
this->write(ByteCodeInstruction::kPushImmediate);
|
||||
this->write32(b.fValue ? ~0 : 0);
|
||||
this->write32(b.value() ? ~0 : 0);
|
||||
}
|
||||
|
||||
void ByteCodeGenerator::writeConstructor(const Constructor& c) {
|
||||
|
@ -479,7 +479,7 @@ void CFGGenerator::addLValue(CFG& cfg, std::unique_ptr<Expression>* e) {
|
||||
}
|
||||
|
||||
static bool is_true(Expression& expr) {
|
||||
return expr.is<BoolLiteral>() && expr.as<BoolLiteral>().fValue;
|
||||
return expr.is<BoolLiteral>() && expr.as<BoolLiteral>().value();
|
||||
}
|
||||
|
||||
void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) {
|
||||
|
@ -881,7 +881,7 @@ void Compiler::simplifyExpression(DefinitionMap& definitions,
|
||||
if (t->fTest->kind() == Expression::Kind::kBoolLiteral) {
|
||||
// ternary has a constant test, replace it with either the true or
|
||||
// false branch
|
||||
if (t->fTest->as<BoolLiteral>().fValue) {
|
||||
if (t->fTest->as<BoolLiteral>().value()) {
|
||||
(*iter)->setExpression(std::move(t->fIfTrue));
|
||||
} else {
|
||||
(*iter)->setExpression(std::move(t->fIfFalse));
|
||||
@ -1292,7 +1292,7 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
|
||||
IfStatement& i = stmt->as<IfStatement>();
|
||||
if (i.fTest->kind() == Expression::Kind::kBoolLiteral) {
|
||||
// constant if, collapse down to a single branch
|
||||
if (i.fTest->as<BoolLiteral>().fValue) {
|
||||
if (i.fTest->as<BoolLiteral>().value()) {
|
||||
SkASSERT(i.fIfTrue);
|
||||
(*iter)->setStatement(std::move(i.fIfTrue));
|
||||
} else {
|
||||
|
@ -267,7 +267,7 @@ void Dehydrator::write(const Expression* e) {
|
||||
case Expression::Kind::kBoolLiteral: {
|
||||
const BoolLiteral& b = e->as<BoolLiteral>();
|
||||
this->writeU8(Rehydrator::kBoolLiteral_Command);
|
||||
this->writeU8(b.fValue);
|
||||
this->writeU8(b.value());
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::kConstructor: {
|
||||
|
@ -1008,7 +1008,7 @@ void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p,
|
||||
}
|
||||
|
||||
void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
|
||||
this->write(b.fValue ? "true" : "false");
|
||||
this->write(b.value() ? "true" : "false");
|
||||
}
|
||||
|
||||
void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
|
||||
|
@ -532,7 +532,7 @@ std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
|
||||
}
|
||||
if (test->kind() == Expression::Kind::kBoolLiteral) {
|
||||
// static boolean value, fold down to a single branch
|
||||
if (test->as<BoolLiteral>().fValue) {
|
||||
if (test->as<BoolLiteral>().value()) {
|
||||
return ifTrue;
|
||||
} else if (ifFalse) {
|
||||
return ifFalse;
|
||||
@ -1710,7 +1710,7 @@ static std::unique_ptr<Expression> short_circuit_boolean(const Context& context,
|
||||
Token::Kind op,
|
||||
const Expression& right) {
|
||||
SkASSERT(left.kind() == Expression::Kind::kBoolLiteral);
|
||||
bool leftVal = left.as<BoolLiteral>().fValue;
|
||||
bool leftVal = left.as<BoolLiteral>().value();
|
||||
if (op == Token::Kind::TK_LOGICALAND) {
|
||||
// (true && expr) -> (expr) and (false && expr) -> (false)
|
||||
return leftVal ? right.clone()
|
||||
@ -1753,8 +1753,8 @@ std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
|
||||
// types, which will let us be more intelligent about this.
|
||||
if (left.kind() == Expression::Kind::kBoolLiteral &&
|
||||
right.kind() == Expression::Kind::kBoolLiteral) {
|
||||
bool leftVal = left.as<BoolLiteral>().fValue;
|
||||
bool rightVal = right.as<BoolLiteral>().fValue;
|
||||
bool leftVal = left.as<BoolLiteral>().value();
|
||||
bool rightVal = right.as<BoolLiteral>().value();
|
||||
bool result;
|
||||
switch (op) {
|
||||
case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break;
|
||||
@ -2013,7 +2013,7 @@ std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode&
|
||||
}
|
||||
if (test->kind() == Expression::Kind::kBoolLiteral) {
|
||||
// static boolean test, just return one of the branches
|
||||
if (test->as<BoolLiteral>().fValue) {
|
||||
if (test->as<BoolLiteral>().value()) {
|
||||
return ifTrue;
|
||||
} else {
|
||||
return ifFalse;
|
||||
@ -2409,7 +2409,7 @@ std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(const ASTNode&
|
||||
}
|
||||
if (base->kind() == Expression::Kind::kBoolLiteral) {
|
||||
return std::make_unique<BoolLiteral>(fContext, base->fOffset,
|
||||
!base->as<BoolLiteral>().fValue);
|
||||
!base->as<BoolLiteral>().value());
|
||||
}
|
||||
break;
|
||||
case Token::Kind::TK_BITWISENOT:
|
||||
|
@ -907,7 +907,7 @@ void MetalCodeGenerator::writePostfixExpression(const PostfixExpression& p,
|
||||
}
|
||||
|
||||
void MetalCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
|
||||
this->write(b.fValue ? "true" : "false");
|
||||
this->write(b.value() ? "true" : "false");
|
||||
}
|
||||
|
||||
void MetalCodeGenerator::writeIntLiteral(const IntLiteral& i) {
|
||||
|
@ -2503,7 +2503,7 @@ SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, Out
|
||||
}
|
||||
|
||||
SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
|
||||
if (b.fValue) {
|
||||
if (b.value()) {
|
||||
if (fBoolTrue == 0) {
|
||||
fBoolTrue = this->nextId();
|
||||
this->writeInstruction(SpvOpConstantTrue, this->getType(b.type()), fBoolTrue,
|
||||
|
@ -16,15 +16,19 @@ namespace SkSL {
|
||||
/**
|
||||
* Represents 'true' or 'false'.
|
||||
*/
|
||||
struct BoolLiteral : public Expression {
|
||||
class BoolLiteral : public Expression {
|
||||
public:
|
||||
static constexpr Kind kExpressionKind = Kind::kBoolLiteral;
|
||||
|
||||
BoolLiteral(const Context& context, int offset, bool value)
|
||||
: INHERITED(offset, kExpressionKind, context.fBool_Type.get())
|
||||
, fValue(value) {}
|
||||
: INHERITED(offset, kExpressionKind, BoolLiteralData{context.fBool_Type.get(), value}) {}
|
||||
|
||||
bool value() const {
|
||||
return this->boolLiteralData().fValue;
|
||||
}
|
||||
|
||||
String description() const override {
|
||||
return String(fValue ? "true" : "false");
|
||||
return String(this->value() ? "true" : "false");
|
||||
}
|
||||
|
||||
bool hasProperty(Property property) const override {
|
||||
@ -36,22 +40,19 @@ struct BoolLiteral : public Expression {
|
||||
}
|
||||
|
||||
bool compareConstant(const Context& context, const Expression& other) const override {
|
||||
BoolLiteral& b = (BoolLiteral&) other;
|
||||
return fValue == b.fValue;
|
||||
const BoolLiteral& b = other.as<BoolLiteral>();
|
||||
return this->value() == b.value();
|
||||
}
|
||||
|
||||
std::unique_ptr<Expression> clone() const override {
|
||||
return std::unique_ptr<Expression>(new BoolLiteral(fOffset, fValue, &this->type()));
|
||||
return std::unique_ptr<Expression>(new BoolLiteral(fOffset, this->value(), &this->type()));
|
||||
}
|
||||
|
||||
const bool fValue;
|
||||
|
||||
using INHERITED = Expression;
|
||||
|
||||
private:
|
||||
BoolLiteral(int offset, bool value, const Type* type)
|
||||
: INHERITED(offset, kExpressionKind, type)
|
||||
, fValue(value) {}
|
||||
: INHERITED(offset, kExpressionKind, BoolLiteralData{type, value}) {}
|
||||
|
||||
using INHERITED = Expression;
|
||||
};
|
||||
|
||||
} // namespace SkSL
|
||||
|
@ -56,6 +56,11 @@ struct Expression : public IRNode {
|
||||
kContainsRTAdjust
|
||||
};
|
||||
|
||||
Expression(int offset, Kind kind, BoolLiteralData data)
|
||||
: INHERITED(offset, (int) kind, data) {
|
||||
SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
|
||||
}
|
||||
|
||||
Expression(int offset, Kind kind, const Type* type)
|
||||
: INHERITED(offset, (int) kind, type) {
|
||||
SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
|
||||
|
@ -11,18 +11,25 @@
|
||||
|
||||
namespace SkSL {
|
||||
|
||||
IRNode::IRNode(int offset, int kind, BlockData data, std::vector<std::unique_ptr<Statement>> stmts)
|
||||
IRNode::IRNode(int offset, int kind, const BlockData& data,
|
||||
std::vector<std::unique_ptr<Statement>> stmts)
|
||||
: fOffset(offset)
|
||||
, fKind(kind)
|
||||
, fData(data)
|
||||
, fStatementChildren(std::move(stmts)) {}
|
||||
|
||||
IRNode::IRNode(int offset, int kind, const BoolLiteralData& data)
|
||||
: fOffset(offset)
|
||||
, fKind(kind)
|
||||
, fData(data) {}
|
||||
|
||||
|
||||
IRNode::IRNode(int offset, int kind, const Type* data)
|
||||
: fOffset(offset)
|
||||
, fKind(kind)
|
||||
, fData(data) {}
|
||||
|
||||
IRNode::IRNode(int offset, int kind, TypeTokenData data)
|
||||
IRNode::IRNode(int offset, int kind, const TypeTokenData& data)
|
||||
: fOffset(offset)
|
||||
, fKind(kind)
|
||||
, fData(data) {}
|
||||
|
@ -50,6 +50,8 @@ public:
|
||||
|
||||
const Type& type() const {
|
||||
switch (fData.fKind) {
|
||||
case NodeData::Kind::kBoolLiteral:
|
||||
return *this->boolLiteralData().fType;
|
||||
case NodeData::Kind::kType:
|
||||
return *this->typeData();
|
||||
case NodeData::Kind::kTypeToken:
|
||||
@ -68,6 +70,11 @@ protected:
|
||||
bool fIsScope;
|
||||
};
|
||||
|
||||
struct BoolLiteralData {
|
||||
const Type* fType;
|
||||
bool fValue;
|
||||
};
|
||||
|
||||
struct TypeTokenData {
|
||||
const Type* fType;
|
||||
Token::Kind fToken;
|
||||
@ -75,45 +82,64 @@ protected:
|
||||
|
||||
struct NodeData {
|
||||
char fBytes[std::max({sizeof(BlockData),
|
||||
sizeof(BoolLiteralData),
|
||||
sizeof(Type*),
|
||||
sizeof(TypeTokenData)})];
|
||||
|
||||
enum class Kind {
|
||||
kBlock,
|
||||
kBoolLiteral,
|
||||
kType,
|
||||
kTypeToken,
|
||||
} fKind;
|
||||
|
||||
NodeData() = default;
|
||||
|
||||
NodeData(BlockData data)
|
||||
NodeData(const BlockData& data)
|
||||
: fKind(Kind::kBlock) {
|
||||
new(reinterpret_cast<BlockData*>(fBytes)) BlockData{std::move(data.fSymbolTable),
|
||||
data.fIsScope};
|
||||
*(new(fBytes) BlockData) = data;
|
||||
}
|
||||
|
||||
NodeData(const BoolLiteralData& data)
|
||||
: fKind(Kind::kBoolLiteral) {
|
||||
*(new(fBytes) BoolLiteralData) = data;
|
||||
}
|
||||
|
||||
NodeData(const Type* data)
|
||||
: fKind(Kind::kType) {
|
||||
memcpy(fBytes, &data, sizeof(data));
|
||||
*(new(fBytes) const Type*) = data;
|
||||
}
|
||||
|
||||
NodeData(TypeTokenData data)
|
||||
NodeData(const TypeTokenData& data)
|
||||
: fKind(Kind::kTypeToken) {
|
||||
memcpy(fBytes, &data, sizeof(data));
|
||||
*(new(fBytes) TypeTokenData) = data;
|
||||
}
|
||||
|
||||
~NodeData() {
|
||||
if (fKind == Kind::kBlock) {
|
||||
reinterpret_cast<BlockData*>(fBytes)->~BlockData();
|
||||
switch (fKind) {
|
||||
case Kind::kBlock:
|
||||
reinterpret_cast<BlockData*>(fBytes)->~BlockData();
|
||||
break;
|
||||
case Kind::kBoolLiteral:
|
||||
reinterpret_cast<BoolLiteralData*>(fBytes)->~BoolLiteralData();
|
||||
break;
|
||||
case Kind::kType:
|
||||
break;
|
||||
case Kind::kTypeToken:
|
||||
reinterpret_cast<TypeTokenData*>(fBytes)->~TypeTokenData();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
IRNode(int offset, int kind, BlockData data, std::vector<std::unique_ptr<Statement>> stmts);
|
||||
IRNode(int offset, int kind, const BlockData& data,
|
||||
std::vector<std::unique_ptr<Statement>> stmts);
|
||||
|
||||
IRNode(int offset, int kind, const BoolLiteralData& data);
|
||||
|
||||
IRNode(int offset, int kind, const Type* data = nullptr);
|
||||
|
||||
IRNode(int offset, int kind, TypeTokenData data);
|
||||
IRNode(int offset, int kind, const TypeTokenData& data);
|
||||
|
||||
IRNode(const IRNode& other);
|
||||
|
||||
@ -166,6 +192,11 @@ protected:
|
||||
return *reinterpret_cast<const BlockData*>(fData.fBytes);
|
||||
}
|
||||
|
||||
const BoolLiteralData& boolLiteralData() const {
|
||||
SkASSERT(fData.fKind == NodeData::Kind::kBoolLiteral);
|
||||
return *reinterpret_cast<const BoolLiteralData*>(fData.fBytes);
|
||||
}
|
||||
|
||||
const Type* typeData() const {
|
||||
SkASSERT(fData.fKind == NodeData::Kind::kType);
|
||||
return *reinterpret_cast<const Type* const*>(fData.fBytes);
|
||||
|
Loading…
Reference in New Issue
Block a user