2020-07-28 18:46:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "src/sksl/SkSLDehydrator.h"
|
|
|
|
|
2020-08-26 23:46:27 +00:00
|
|
|
#include <map>
|
|
|
|
|
2020-07-28 18:46:53 +00:00
|
|
|
#include "src/sksl/SkSLRehydrator.h"
|
|
|
|
#include "src/sksl/ir/SkSLBinaryExpression.h"
|
|
|
|
#include "src/sksl/ir/SkSLBreakStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLConstructor.h"
|
|
|
|
#include "src/sksl/ir/SkSLContinueStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLDiscardStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLDoStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLEnum.h"
|
|
|
|
#include "src/sksl/ir/SkSLExpressionStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLField.h"
|
|
|
|
#include "src/sksl/ir/SkSLFieldAccess.h"
|
|
|
|
#include "src/sksl/ir/SkSLForStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLFunctionCall.h"
|
|
|
|
#include "src/sksl/ir/SkSLFunctionDeclaration.h"
|
|
|
|
#include "src/sksl/ir/SkSLFunctionDefinition.h"
|
|
|
|
#include "src/sksl/ir/SkSLIfStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLIndexExpression.h"
|
2020-09-09 18:18:53 +00:00
|
|
|
#include "src/sksl/ir/SkSLInlineMarker.h"
|
2020-07-28 18:46:53 +00:00
|
|
|
#include "src/sksl/ir/SkSLIntLiteral.h"
|
|
|
|
#include "src/sksl/ir/SkSLInterfaceBlock.h"
|
|
|
|
#include "src/sksl/ir/SkSLNullLiteral.h"
|
|
|
|
#include "src/sksl/ir/SkSLPostfixExpression.h"
|
|
|
|
#include "src/sksl/ir/SkSLPrefixExpression.h"
|
|
|
|
#include "src/sksl/ir/SkSLProgramElement.h"
|
|
|
|
#include "src/sksl/ir/SkSLReturnStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLSetting.h"
|
|
|
|
#include "src/sksl/ir/SkSLStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLSwitchCase.h"
|
|
|
|
#include "src/sksl/ir/SkSLSwitchStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLSwizzle.h"
|
|
|
|
#include "src/sksl/ir/SkSLSymbol.h"
|
|
|
|
#include "src/sksl/ir/SkSLSymbolTable.h"
|
|
|
|
#include "src/sksl/ir/SkSLTernaryExpression.h"
|
|
|
|
#include "src/sksl/ir/SkSLUnresolvedFunction.h"
|
|
|
|
#include "src/sksl/ir/SkSLVarDeclarations.h"
|
|
|
|
#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
|
|
|
|
#include "src/sksl/ir/SkSLVariable.h"
|
|
|
|
#include "src/sksl/ir/SkSLWhileStatement.h"
|
|
|
|
|
|
|
|
#ifdef SKSL_STANDALONE
|
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
static constexpr int HEADER_SIZE = 2;
|
|
|
|
|
|
|
|
class AutoDehydratorSymbolTable {
|
|
|
|
public:
|
|
|
|
AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
|
|
|
|
: fDehydrator(dehydrator) {
|
|
|
|
dehydrator->fSymbolMap.emplace_back();
|
|
|
|
if (symbols) {
|
|
|
|
dehydrator->write(*symbols);
|
|
|
|
} else {
|
|
|
|
dehydrator->writeU8(Rehydrator::kVoid_Command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoDehydratorSymbolTable() {
|
|
|
|
fDehydrator->fSymbolMap.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Dehydrator* fDehydrator;
|
|
|
|
};
|
|
|
|
|
|
|
|
void Dehydrator::write(Layout l) {
|
|
|
|
if (l == Layout()) {
|
|
|
|
this->writeU8(Rehydrator::kDefaultLayout_Command);
|
|
|
|
} else if (l == Layout::builtin(l.fBuiltin)) {
|
|
|
|
this->writeS8(Rehydrator::kBuiltinLayout_Command);
|
|
|
|
this->writeS16(l.fBuiltin);
|
|
|
|
} else {
|
|
|
|
this->writeS8(Rehydrator::kLayout_Command);
|
|
|
|
fBody.write32(l.fFlags);
|
|
|
|
this->writeS8(l.fLocation);
|
|
|
|
this->writeS8(l.fOffset);
|
|
|
|
this->writeS8(l.fBinding);
|
|
|
|
this->writeS8(l.fIndex);
|
|
|
|
this->writeS8(l.fSet);
|
|
|
|
this->writeS16(l.fBuiltin);
|
|
|
|
this->writeS8(l.fInputAttachmentIndex);
|
|
|
|
this->writeS8((int) l.fFormat);
|
|
|
|
this->writeS8(l.fPrimitive);
|
|
|
|
this->writeS8(l.fMaxVertices);
|
|
|
|
this->writeS8(l.fInvocations);
|
|
|
|
this->write(l.fMarker);
|
|
|
|
this->write(l.fWhen);
|
|
|
|
this->writeS8(l.fKey);
|
|
|
|
this->writeS8((int) l.fCType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(Modifiers m) {
|
|
|
|
if (m == Modifiers()) {
|
|
|
|
this->writeU8(Rehydrator::kDefaultModifiers_Command);
|
|
|
|
} else {
|
|
|
|
if (m.fFlags <= 255) {
|
|
|
|
this->writeU8(Rehydrator::kModifiers8Bit_Command);
|
|
|
|
this->write(m.fLayout);
|
|
|
|
this->writeU8(m.fFlags);
|
|
|
|
} else {
|
|
|
|
this->writeU8(Rehydrator::kModifiers_Command);
|
|
|
|
this->write(m.fLayout);
|
|
|
|
this->writeS32(m.fFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(StringFragment s) {
|
|
|
|
this->write(String(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(String s) {
|
|
|
|
auto found = fStrings.find(s);
|
|
|
|
int offset;
|
|
|
|
if (found == fStrings.end()) {
|
|
|
|
offset = fStringBuffer.str().length() + HEADER_SIZE;
|
|
|
|
fStrings.insert({ s, offset });
|
|
|
|
SkASSERT(s.length() <= 255);
|
|
|
|
fStringBuffer.write8(s.length());
|
|
|
|
fStringBuffer.writeString(s);
|
|
|
|
} else {
|
|
|
|
offset = found->second;
|
|
|
|
}
|
|
|
|
this->writeU16(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const Symbol& s) {
|
|
|
|
uint16_t id = this->symbolId(&s, false);
|
|
|
|
if (id) {
|
|
|
|
this->writeU8(Rehydrator::kSymbolRef_Command);
|
|
|
|
this->writeU16(id);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
switch (s.kind()) {
|
|
|
|
case Symbol::Kind::kFunctionDeclaration: {
|
2020-08-18 14:40:03 +00:00
|
|
|
const FunctionDeclaration& f = s.as<FunctionDeclaration>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kFunctionDeclaration_Command);
|
|
|
|
this->writeId(&f);
|
|
|
|
this->write(f.fModifiers);
|
|
|
|
this->write(f.fName);
|
|
|
|
this->writeU8(f.fParameters.size());
|
|
|
|
for (const Variable* p : f.fParameters) {
|
|
|
|
this->writeU16(this->symbolId(p));
|
|
|
|
}
|
|
|
|
this->write(f.fReturnType);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Symbol::Kind::kUnresolvedFunction: {
|
2020-08-18 14:40:03 +00:00
|
|
|
const UnresolvedFunction& f = s.as<UnresolvedFunction>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kUnresolvedFunction_Command);
|
|
|
|
this->writeId(&f);
|
|
|
|
this->writeU8(f.fFunctions.size());
|
2020-08-25 17:33:02 +00:00
|
|
|
for (const FunctionDeclaration* funcDecl : f.fFunctions) {
|
|
|
|
this->write(*funcDecl);
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Symbol::Kind::kType: {
|
2020-08-18 14:40:03 +00:00
|
|
|
const Type& t = s.as<Type>();
|
2020-09-08 14:22:09 +00:00
|
|
|
switch (t.typeKind()) {
|
|
|
|
case Type::TypeKind::kArray:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kArrayType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.componentType());
|
|
|
|
this->writeU8(t.columns());
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Type::TypeKind::kEnum:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kEnumType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.fName);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Type::TypeKind::kNullable:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kNullableType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.componentType());
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Type::TypeKind::kStruct:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kStructType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.fName);
|
|
|
|
this->writeU8(t.fields().size());
|
|
|
|
for (const Type::Field& f : t.fields()) {
|
|
|
|
this->write(f.fModifiers);
|
|
|
|
this->write(f.fName);
|
|
|
|
this->write(*f.fType);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this->writeU8(Rehydrator::kSystemType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.fName);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Symbol::Kind::kVariable: {
|
2020-08-18 14:40:03 +00:00
|
|
|
const Variable& v = s.as<Variable>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kVariable_Command);
|
|
|
|
this->writeId(&v);
|
|
|
|
this->write(v.fModifiers);
|
|
|
|
this->write(v.fName);
|
2020-09-11 16:27:26 +00:00
|
|
|
this->write(v.type());
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(v.fStorage);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Symbol::Kind::kField: {
|
2020-08-18 14:40:03 +00:00
|
|
|
const Field& f = s.as<Field>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kField_Command);
|
|
|
|
this->writeU16(this->symbolId(&f.fOwner));
|
|
|
|
this->writeU8(f.fFieldIndex);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Symbol::Kind::kExternal:
|
2020-07-28 18:46:53 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const SymbolTable& symbols) {
|
|
|
|
this->writeU8(Rehydrator::kSymbolTable_Command);
|
|
|
|
this->writeU16(symbols.fOwnedSymbols.size());
|
|
|
|
for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
|
|
|
|
this->write(*s);
|
|
|
|
}
|
|
|
|
this->writeU16(symbols.fSymbols.size());
|
2020-07-31 17:18:02 +00:00
|
|
|
std::map<StringFragment, const Symbol*> ordered;
|
2020-07-28 18:46:53 +00:00
|
|
|
for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) {
|
2020-07-31 17:18:02 +00:00
|
|
|
ordered.insert(p);
|
|
|
|
}
|
|
|
|
for (std::pair<StringFragment, const Symbol*> p : ordered) {
|
2020-07-28 18:46:53 +00:00
|
|
|
this->write(p.first);
|
|
|
|
bool found = false;
|
|
|
|
for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
|
|
|
|
if (symbols.fOwnedSymbols[i].get() == p.second) {
|
|
|
|
this->writeU16(i);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkASSERT(found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const Expression* e) {
|
|
|
|
if (e) {
|
2020-09-08 14:22:09 +00:00
|
|
|
switch (e->kind()) {
|
|
|
|
case Expression::Kind::kBinary: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const BinaryExpression& b = e->as<BinaryExpression>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kBinary_Command);
|
2020-09-22 19:05:37 +00:00
|
|
|
this->write(&b.left());
|
|
|
|
this->writeU8((int) b.getOperator());
|
|
|
|
this->write(&b.right());
|
2020-09-11 16:27:26 +00:00
|
|
|
this->write(b.type());
|
2020-07-28 18:46:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kBoolLiteral: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const BoolLiteral& b = e->as<BoolLiteral>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kBoolLiteral_Command);
|
2020-09-28 13:18:15 +00:00
|
|
|
this->writeU8(b.value());
|
2020-07-28 18:46:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kConstructor: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const Constructor& c = e->as<Constructor>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kConstructor_Command);
|
2020-09-11 16:27:26 +00:00
|
|
|
this->write(c.type());
|
2020-09-29 16:41:35 +00:00
|
|
|
this->writeU8(c.arguments().size());
|
|
|
|
for (const auto& a : c.arguments()) {
|
2020-07-28 18:46:53 +00:00
|
|
|
this->write(a.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kExternalFunctionCall:
|
|
|
|
case Expression::Kind::kExternalValue:
|
2020-07-28 18:46:53 +00:00
|
|
|
// not implemented; doesn't seem like we'll ever need them from within an include
|
|
|
|
// file
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kFieldAccess: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const FieldAccess& f = e->as<FieldAccess>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kFieldAccess_Command);
|
|
|
|
this->write(f.fBase.get());
|
|
|
|
this->writeU8(f.fFieldIndex);
|
|
|
|
this->writeU8(f.fOwnerKind);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kFloatLiteral: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const FloatLiteral& f = e->as<FloatLiteral>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kFloatLiteral_Command);
|
|
|
|
FloatIntUnion u;
|
2020-10-01 16:13:17 +00:00
|
|
|
u.fFloat = f.value();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeS32(u.fInt);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kFunctionCall: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const FunctionCall& f = e->as<FunctionCall>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kFunctionCall_Command);
|
2020-09-11 16:27:26 +00:00
|
|
|
this->write(f.type());
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeId(&f.fFunction);
|
|
|
|
this->writeU8(f.fArguments.size());
|
|
|
|
for (const auto& a : f.fArguments) {
|
|
|
|
this->write(a.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kIndex: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const IndexExpression& i = e->as<IndexExpression>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kIndex_Command);
|
|
|
|
this->write(i.fBase.get());
|
|
|
|
this->write(i.fIndex.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kIntLiteral: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const IntLiteral& i = e->as<IntLiteral>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kIntLiteral_Command);
|
2020-09-28 20:27:18 +00:00
|
|
|
this->writeS32(i.value());
|
2020-07-28 18:46:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kNullLiteral:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kNullLiteral_Command);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kPostfix: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const PostfixExpression& p = e->as<PostfixExpression>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kPostfix_Command);
|
|
|
|
this->writeU8((int) p.fOperator);
|
|
|
|
this->write(p.fOperand.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kPrefix: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const PrefixExpression& p = e->as<PrefixExpression>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kPrefix_Command);
|
|
|
|
this->writeU8((int) p.fOperator);
|
|
|
|
this->write(p.fOperand.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kSetting: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const Setting& s = e->as<Setting>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kSetting_Command);
|
|
|
|
this->write(s.fName);
|
|
|
|
this->write(s.fValue.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kSwizzle: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const Swizzle& s = e->as<Swizzle>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kSwizzle_Command);
|
|
|
|
this->write(s.fBase.get());
|
|
|
|
this->writeU8(s.fComponents.size());
|
|
|
|
for (int c : s.fComponents) {
|
|
|
|
this->writeU8(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kTernary: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const TernaryExpression& t = e->as<TernaryExpression>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kTernary_Command);
|
|
|
|
this->write(t.fTest.get());
|
|
|
|
this->write(t.fIfTrue.get());
|
|
|
|
this->write(t.fIfFalse.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kVariableReference: {
|
2020-08-18 13:24:00 +00:00
|
|
|
const VariableReference& v = e->as<VariableReference>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kVariableReference_Command);
|
2020-09-24 19:01:27 +00:00
|
|
|
this->writeId(v.fVariable);
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(v.fRefKind);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Expression::Kind::kFunctionReference:
|
|
|
|
case Expression::Kind::kTypeReference:
|
|
|
|
case Expression::Kind::kDefined:
|
2020-07-28 18:46:53 +00:00
|
|
|
// shouldn't appear in finished code
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->writeU8(Rehydrator::kVoid_Command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const Statement* s) {
|
|
|
|
if (s) {
|
2020-09-08 14:22:09 +00:00
|
|
|
switch (s->kind()) {
|
|
|
|
case Statement::Kind::kBlock: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const Block& b = s->as<Block>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kBlock_Command);
|
2020-09-25 18:31:59 +00:00
|
|
|
AutoDehydratorSymbolTable symbols(this, b.symbolTable());
|
|
|
|
this->writeU8(b.children().size());
|
|
|
|
for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
|
2020-08-25 17:33:02 +00:00
|
|
|
this->write(blockStmt.get());
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
2020-09-25 18:31:59 +00:00
|
|
|
this->writeU8(b.isScope());
|
2020-07-28 18:46:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kBreak:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kBreak_Command);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kContinue:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kContinue_Command);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kDiscard:
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kDiscard_Command);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kDo: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const DoStatement& d = s->as<DoStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kDo_Command);
|
2020-09-28 17:14:19 +00:00
|
|
|
this->write(d.statement().get());
|
|
|
|
this->write(d.test().get());
|
2020-07-28 18:46:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kExpression: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const ExpressionStatement& e = s->as<ExpressionStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kExpressionStatement_Command);
|
2020-09-30 13:29:55 +00:00
|
|
|
this->write(e.expression().get());
|
2020-07-28 18:46:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kFor: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const ForStatement& f = s->as<ForStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kFor_Command);
|
|
|
|
this->write(f.fInitializer.get());
|
|
|
|
this->write(f.fTest.get());
|
|
|
|
this->write(f.fNext.get());
|
|
|
|
this->write(f.fStatement.get());
|
|
|
|
this->write(f.fSymbols);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kIf: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const IfStatement& i = s->as<IfStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kIf_Command);
|
|
|
|
this->writeU8(i.fIsStatic);
|
|
|
|
this->write(i.fTest.get());
|
|
|
|
this->write(i.fIfTrue.get());
|
|
|
|
this->write(i.fIfFalse.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-09 18:18:53 +00:00
|
|
|
case Statement::Kind::kInlineMarker: {
|
|
|
|
const InlineMarker& i = s->as<InlineMarker>();
|
|
|
|
this->writeU8(Rehydrator::kInlineMarker_Command);
|
|
|
|
this->writeId(i.fFuncDecl);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kNop:
|
2020-07-28 18:46:53 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kReturn: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const ReturnStatement& r = s->as<ReturnStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kReturn_Command);
|
|
|
|
this->write(r.fExpression.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kSwitch: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const SwitchStatement& ss = s->as<SwitchStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kSwitch_Command);
|
|
|
|
this->writeU8(ss.fIsStatic);
|
|
|
|
AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
|
|
|
|
this->write(ss.fValue.get());
|
|
|
|
this->writeU8(ss.fCases.size());
|
2020-08-25 17:33:02 +00:00
|
|
|
for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) {
|
2020-07-28 18:46:53 +00:00
|
|
|
this->write(sc->fValue.get());
|
|
|
|
this->writeU8(sc->fStatements.size());
|
2020-08-25 17:33:02 +00:00
|
|
|
for (const std::unique_ptr<Statement>& stmt : sc->fStatements) {
|
2020-07-28 18:46:53 +00:00
|
|
|
this->write(stmt.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-11 19:53:40 +00:00
|
|
|
case Statement::Kind::kSwitchCase:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kVarDeclaration: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const VarDeclaration& v = s->as<VarDeclaration>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kVarDeclaration_Command);
|
|
|
|
this->writeU16(this->symbolId(v.fVar));
|
|
|
|
this->writeU8(v.fSizes.size());
|
2020-08-25 17:33:02 +00:00
|
|
|
for (const std::unique_ptr<Expression>& sizeExpr : v.fSizes) {
|
|
|
|
this->write(sizeExpr.get());
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
|
|
|
this->write(v.fValue.get());
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kVarDeclarations: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const VarDeclarationsStatement& v = s->as<VarDeclarationsStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->write(*v.fDeclaration);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case Statement::Kind::kWhile: {
|
2020-08-18 13:30:51 +00:00
|
|
|
const WhileStatement& w = s->as<WhileStatement>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kWhile_Command);
|
|
|
|
this->write(w.fTest.get());
|
|
|
|
this->write(w.fStatement.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->writeU8(Rehydrator::kVoid_Command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const ProgramElement& e) {
|
2020-09-08 14:22:09 +00:00
|
|
|
switch (e.kind()) {
|
|
|
|
case ProgramElement::Kind::kEnum: {
|
2020-08-19 21:48:31 +00:00
|
|
|
const Enum& en = e.as<Enum>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kEnum_Command);
|
2020-09-29 21:05:54 +00:00
|
|
|
this->write(en.typeName());
|
|
|
|
AutoDehydratorSymbolTable symbols(this, en.symbols());
|
|
|
|
for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
|
2020-09-08 14:22:09 +00:00
|
|
|
SkASSERT(s->kind() == Symbol::Kind::kVariable);
|
2020-07-28 18:46:53 +00:00
|
|
|
Variable& v = (Variable&) *s;
|
2020-08-18 13:24:00 +00:00
|
|
|
SkASSERT(v.fInitialValue);
|
|
|
|
const IntLiteral& i = v.fInitialValue->as<IntLiteral>();
|
2020-09-28 20:27:18 +00:00
|
|
|
this->writeS32(i.value());
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case ProgramElement::Kind::kExtension:
|
2020-07-28 18:46:53 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case ProgramElement::Kind::kFunction: {
|
2020-08-19 21:48:31 +00:00
|
|
|
const FunctionDefinition& f = e.as<FunctionDefinition>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kFunctionDefinition_Command);
|
|
|
|
this->writeU16(this->symbolId(&f.fDeclaration));
|
|
|
|
this->write(f.fBody.get());
|
|
|
|
this->writeU8(f.fReferencedIntrinsics.size());
|
2020-07-31 17:18:02 +00:00
|
|
|
std::set<uint16_t> ordered;
|
2020-07-28 18:46:53 +00:00
|
|
|
for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) {
|
2020-07-31 17:18:02 +00:00
|
|
|
ordered.insert(this->symbolId(ref));
|
|
|
|
}
|
|
|
|
for (uint16_t ref : ordered) {
|
|
|
|
this->writeU16(ref);
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case ProgramElement::Kind::kInterfaceBlock: {
|
2020-08-19 21:48:31 +00:00
|
|
|
const InterfaceBlock& i = e.as<InterfaceBlock>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kInterfaceBlock_Command);
|
|
|
|
this->write(i.fVariable);
|
|
|
|
this->write(i.fTypeName);
|
|
|
|
this->write(i.fInstanceName);
|
|
|
|
this->writeU8(i.fSizes.size());
|
|
|
|
for (const auto& s : i.fSizes) {
|
|
|
|
this->write(s.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 14:22:09 +00:00
|
|
|
case ProgramElement::Kind::kModifiers:
|
2020-07-28 18:46:53 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case ProgramElement::Kind::kSection:
|
2020-07-28 18:46:53 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2020-09-08 14:22:09 +00:00
|
|
|
case ProgramElement::Kind::kVar: {
|
2020-08-19 21:48:31 +00:00
|
|
|
const VarDeclarations& v = e.as<VarDeclarations>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeU8(Rehydrator::kVarDeclarations_Command);
|
|
|
|
this->write(v.fBaseType);
|
|
|
|
this->writeU8(v.fVars.size());
|
2020-08-25 17:33:02 +00:00
|
|
|
for (const auto& var : v.fVars) {
|
|
|
|
this->write(var.get());
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
|
|
|
|
this->writeU8(Rehydrator::kElements_Command);
|
|
|
|
this->writeU8(elements.size());
|
|
|
|
for (const auto& e : elements) {
|
|
|
|
this->write(*e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::finish(OutputStream& out) {
|
|
|
|
out.write16(fStringBuffer.str().size());
|
|
|
|
out.writeString(fStringBuffer.str());
|
|
|
|
out.writeString(fBody.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif
|