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"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
switch (s.fKind) {
|
|
|
|
case Symbol::kFunctionDeclaration_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Symbol::kUnresolvedFunction_Kind: {
|
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());
|
|
|
|
for (const FunctionDeclaration* f : f.fFunctions) {
|
|
|
|
this->write(*f);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Symbol::kType_Kind: {
|
2020-08-18 14:40:03 +00:00
|
|
|
const Type& t = s.as<Type>();
|
2020-07-28 18:46:53 +00:00
|
|
|
switch (t.kind()) {
|
|
|
|
case Type::kArray_Kind:
|
|
|
|
this->writeU8(Rehydrator::kArrayType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.componentType());
|
|
|
|
this->writeU8(t.columns());
|
|
|
|
break;
|
|
|
|
case Type::kEnum_Kind:
|
|
|
|
this->writeU8(Rehydrator::kEnumType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.fName);
|
|
|
|
break;
|
|
|
|
case Type::kNullable_Kind:
|
|
|
|
this->writeU8(Rehydrator::kNullableType_Command);
|
|
|
|
this->writeId(&t);
|
|
|
|
this->write(t.componentType());
|
|
|
|
break;
|
|
|
|
case Type::kStruct_Kind:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
case Symbol::kVariable_Kind: {
|
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);
|
|
|
|
this->write(v.fType);
|
|
|
|
this->writeU8(v.fStorage);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Symbol::kField_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Symbol::kExternal_Kind:
|
|
|
|
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) {
|
|
|
|
switch (e->fKind) {
|
|
|
|
case Expression::kBinary_Kind: {
|
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);
|
|
|
|
this->write(b.fLeft.get());
|
|
|
|
this->writeU8((int) b.fOperator);
|
|
|
|
this->write(b.fRight.get());
|
|
|
|
this->write(b.fType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kBoolLiteral_Kind: {
|
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);
|
|
|
|
this->writeU8(b.fValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kConstructor_Kind: {
|
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);
|
|
|
|
this->write(c.fType);
|
|
|
|
this->writeU8(c.fArguments.size());
|
|
|
|
for (const auto& a : c.fArguments) {
|
|
|
|
this->write(a.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kExternalFunctionCall_Kind:
|
|
|
|
case Expression::kExternalValue_Kind:
|
|
|
|
// not implemented; doesn't seem like we'll ever need them from within an include
|
|
|
|
// file
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
case Expression::kFieldAccess_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kFloatLiteral_Kind: {
|
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;
|
|
|
|
u.fFloat = f.fValue;
|
|
|
|
this->writeS32(u.fInt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kFunctionCall_Kind: {
|
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);
|
|
|
|
this->write(f.fType);
|
|
|
|
this->writeId(&f.fFunction);
|
|
|
|
this->writeU8(f.fArguments.size());
|
|
|
|
for (const auto& a : f.fArguments) {
|
|
|
|
this->write(a.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kIndex_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kIntLiteral_Kind: {
|
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);
|
|
|
|
this->writeS32(i.fValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kNullLiteral_Kind:
|
|
|
|
this->writeU8(Rehydrator::kNullLiteral_Command);
|
|
|
|
break;
|
|
|
|
case Expression::kPostfix_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kPrefix_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kSetting_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kSwizzle_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kTernary_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Expression::kVariableReference_Kind: {
|
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);
|
|
|
|
this->writeId(&v.fVariable);
|
|
|
|
this->writeU8(v.fRefKind);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kFunctionReference_Kind:
|
|
|
|
case Expression::kTypeReference_Kind:
|
|
|
|
case Expression::kDefined_Kind:
|
|
|
|
// shouldn't appear in finished code
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->writeU8(Rehydrator::kVoid_Command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dehydrator::write(const Statement* s) {
|
|
|
|
if (s) {
|
|
|
|
switch (s->fKind) {
|
|
|
|
case Statement::kBlock_Kind: {
|
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);
|
|
|
|
AutoDehydratorSymbolTable symbols(this, b.fSymbols);
|
|
|
|
this->writeU8(b.fStatements.size());
|
|
|
|
for (const auto& s : b.fStatements) {
|
|
|
|
this->write(s.get());
|
|
|
|
}
|
|
|
|
this->writeU8(b.fIsScope);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kBreak_Kind:
|
|
|
|
this->writeU8(Rehydrator::kBreak_Command);
|
|
|
|
break;
|
|
|
|
case Statement::kContinue_Kind:
|
|
|
|
this->writeU8(Rehydrator::kContinue_Command);
|
|
|
|
break;
|
|
|
|
case Statement::kDiscard_Kind:
|
|
|
|
this->writeU8(Rehydrator::kDiscard_Command);
|
|
|
|
break;
|
|
|
|
case Statement::kDo_Kind: {
|
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);
|
|
|
|
this->write(d.fStatement.get());
|
|
|
|
this->write(d.fTest.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kExpression_Kind: {
|
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);
|
|
|
|
this->write(e.fExpression.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kFor_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Statement::kIf_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Statement::kNop_Kind:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
case Statement::kReturn_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Statement::kSwitch_Kind: {
|
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());
|
|
|
|
for (const auto& sc : ss.fCases) {
|
|
|
|
this->write(sc->fValue.get());
|
|
|
|
this->writeU8(sc->fStatements.size());
|
|
|
|
for (const auto& stmt : sc->fStatements) {
|
|
|
|
this->write(stmt.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kVarDeclaration_Kind: {
|
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());
|
|
|
|
for (const auto& s : v.fSizes) {
|
|
|
|
this->write(s.get());
|
|
|
|
}
|
|
|
|
this->write(v.fValue.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kVarDeclarations_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case Statement::kWhile_Kind: {
|
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) {
|
|
|
|
switch (e.fKind) {
|
|
|
|
case ProgramElement::kEnum_Kind: {
|
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);
|
|
|
|
this->write(en.fTypeName);
|
|
|
|
AutoDehydratorSymbolTable symbols(this, en.fSymbols);
|
|
|
|
for (const auto& s : en.fSymbols->fOwnedSymbols) {
|
|
|
|
SkASSERT(s->fKind == Symbol::kVariable_Kind);
|
|
|
|
Variable& v = (Variable&) *s;
|
2020-08-18 13:24:00 +00:00
|
|
|
SkASSERT(v.fInitialValue);
|
|
|
|
const IntLiteral& i = v.fInitialValue->as<IntLiteral>();
|
2020-07-28 18:46:53 +00:00
|
|
|
this->writeS32(i.fValue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ProgramElement::kExtension_Kind:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
case ProgramElement::kFunction_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case ProgramElement::kInterfaceBlock_Kind: {
|
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;
|
|
|
|
}
|
|
|
|
case ProgramElement::kModifiers_Kind:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
case ProgramElement::kSection_Kind:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
case ProgramElement::kVar_Kind: {
|
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());
|
|
|
|
for (const auto& v : v.fVars) {
|
|
|
|
this->write(v.get());
|
|
|
|
}
|
|
|
|
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
|