Clean up SkSL operator utility functions

We had two copies of IsAssignment. Move everything into Compiler, with
consistent parameter names, etc...

Change-Id: Icc91da69075f4d20cac9b1a7328b717b05984b3f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/316223
Auto-Submit: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2020-09-10 14:47:24 -04:00 committed by Skia Commit-Bot
parent ff9dc8240e
commit 401a009aaa
6 changed files with 34 additions and 57 deletions

View File

@ -696,10 +696,10 @@ bool ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b, bool di
rType.typeKind() == Type::TypeKind::kMatrix);
Token::Kind op;
std::unique_ptr<LValue> lvalue;
if (is_assignment(b.fOperator)) {
if (Compiler::IsAssignment(b.fOperator)) {
lvalue = this->getLValue(*b.fLeft);
lvalue->load();
op = remove_assignment(b.fOperator);
op = Compiler::RemoveAssignment(b.fOperator);
} else {
this->writeExpression(*b.fLeft);
op = b.fOperator;

View File

@ -1890,8 +1890,8 @@ std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
return nullptr;
}
const char* Compiler::OperatorName(Token::Kind kind) {
switch (kind) {
const char* Compiler::OperatorName(Token::Kind op) {
switch (op) {
case Token::Kind::TK_PLUS: return "+";
case Token::Kind::TK_MINUS: return "-";
case Token::Kind::TK_STAR: return "*";
@ -1931,7 +1931,7 @@ const char* Compiler::OperatorName(Token::Kind kind) {
case Token::Kind::TK_MINUSMINUS: return "--";
case Token::Kind::TK_COMMA: return ",";
default:
ABORT("unsupported operator: %d\n", (int) kind);
ABORT("unsupported operator: %d\n", (int) op);
}
}
@ -1958,6 +1958,25 @@ bool Compiler::IsAssignment(Token::Kind op) {
}
}
Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
switch (op) {
case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
default: return op;
}
}
Position Compiler::position(int offset) {
SkASSERT(fSource);
int line = 1;

View File

@ -173,9 +173,14 @@ public:
return *fContext;
}
static const char* OperatorName(Token::Kind token);
static const char* OperatorName(Token::Kind op);
static bool IsAssignment(Token::Kind token);
// Returns true if op is '=' or any compound assignment operator ('+=', '-=', etc.)
static bool IsAssignment(Token::Kind op);
// Given a compound assignment operator, returns the non-assignment version of the operator
// (e.g. '+=' becomes '+')
static Token::Kind RemoveAssignment(Token::Kind op);
void processIncludeFile(Program::Kind kind, const char* path,
std::shared_ptr<SymbolTable> base,

View File

@ -2325,7 +2325,7 @@ SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, Outpu
std::unique_ptr<LValue> lvalue;
SpvId lhs;
if (is_assignment(b.fOperator)) {
if (Compiler::IsAssignment(b.fOperator)) {
lvalue = this->getLValue(*b.fLeft, out);
lhs = lvalue->load(out);
} else {
@ -2333,7 +2333,8 @@ SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, Outpu
lhs = this->writeExpression(*b.fLeft, out);
}
SpvId rhs = this->writeExpression(*b.fRight, out);
SpvId result = this->writeBinaryExpression(b.fLeft->fType, lhs, remove_assignment(b.fOperator),
SpvId result = this->writeBinaryExpression(b.fLeft->fType, lhs,
Compiler::RemoveAssignment(b.fOperator),
b.fRight->fType, rhs, b.fType, out);
if (lvalue) {
lvalue->store(result, out);

View File

@ -33,47 +33,6 @@ void write_stringstream(const StringStream& s, OutputStream& out) {
out.write(s.str().c_str(), s.str().size());
}
bool is_assignment(Token::Kind op) {
switch (op) {
case Token::Kind::TK_EQ: // fall through
case Token::Kind::TK_PLUSEQ: // fall through
case Token::Kind::TK_MINUSEQ: // fall through
case Token::Kind::TK_STAREQ: // fall through
case Token::Kind::TK_SLASHEQ: // fall through
case Token::Kind::TK_PERCENTEQ: // fall through
case Token::Kind::TK_SHLEQ: // fall through
case Token::Kind::TK_SHREQ: // fall through
case Token::Kind::TK_BITWISEOREQ: // fall through
case Token::Kind::TK_BITWISEXOREQ: // fall through
case Token::Kind::TK_BITWISEANDEQ: // fall through
case Token::Kind::TK_LOGICALOREQ: // fall through
case Token::Kind::TK_LOGICALXOREQ: // fall through
case Token::Kind::TK_LOGICALANDEQ:
return true;
default:
return false;
}
}
Token::Kind remove_assignment(Token::Kind op) {
switch (op) {
case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
default: return op;
}
}
#if !defined(SKSL_STANDALONE)
bool type_to_grsltype(const Context& context, const Type& type, GrSLType* outType) {
if (type == *context.fFloat_Type) { *outType = kFloat_GrSLType; return true; }

View File

@ -410,13 +410,6 @@ bool type_to_grsltype(const Context& context, const Type& type, GrSLType* outTyp
void write_stringstream(const StringStream& d, OutputStream& out);
// Returns true if op is '=' or any compound assignment operator ('+=', '-=', etc.)
bool is_assignment(Token::Kind op);
// Given a compound assignment operator, returns the non-assignment version of the operator (e.g.
// '+=' becomes '+')
Token::Kind remove_assignment(Token::Kind op);
NORETURN void sksl_abort();
} // namespace SkSL