2016-07-01 15:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-10-26 14:35:22 +00:00
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "SkSLCompiler.h"
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
#include "ast/SkSLASTPrecision.h"
|
2016-10-13 20:25:34 +00:00
|
|
|
#include "SkSLCFGGenerator.h"
|
2016-12-12 20:33:30 +00:00
|
|
|
#include "SkSLGLSLCodeGenerator.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "SkSLIRGenerator.h"
|
|
|
|
#include "SkSLParser.h"
|
|
|
|
#include "SkSLSPIRVCodeGenerator.h"
|
|
|
|
#include "ir/SkSLExpression.h"
|
|
|
|
#include "ir/SkSLIntLiteral.h"
|
2016-10-12 13:39:56 +00:00
|
|
|
#include "ir/SkSLModifiersDeclaration.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "ir/SkSLSymbolTable.h"
|
2016-10-20 16:54:00 +00:00
|
|
|
#include "ir/SkSLUnresolvedFunction.h"
|
2016-10-13 20:25:34 +00:00
|
|
|
#include "ir/SkSLVarDeclarations.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "SkMutex.h"
|
|
|
|
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
|
|
|
|
// include the built-in shader symbols as static strings
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
static const char* SKSL_INCLUDE =
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "sksl.include"
|
|
|
|
;
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
static const char* SKSL_VERT_INCLUDE =
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "sksl_vert.include"
|
|
|
|
;
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
static const char* SKSL_FRAG_INCLUDE =
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "sksl_frag.include"
|
|
|
|
;
|
|
|
|
|
2017-02-16 21:37:32 +00:00
|
|
|
static const char* SKSL_GEOM_INCLUDE =
|
|
|
|
#include "sksl_geom.include"
|
|
|
|
;
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
namespace SkSL {
|
|
|
|
|
2016-10-26 14:35:22 +00:00
|
|
|
Compiler::Compiler()
|
2016-07-01 15:22:01 +00:00
|
|
|
: fErrorCount(0) {
|
|
|
|
auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this));
|
|
|
|
auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this));
|
2016-07-25 17:08:54 +00:00
|
|
|
fIRGenerator = new IRGenerator(&fContext, symbols, *this);
|
2016-07-01 15:22:01 +00:00
|
|
|
fTypes = types;
|
2016-07-25 17:08:54 +00:00
|
|
|
#define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \
|
|
|
|
fContext.f ## t ## _Type.get())
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(Void);
|
|
|
|
ADD_TYPE(Float);
|
|
|
|
ADD_TYPE(Vec2);
|
|
|
|
ADD_TYPE(Vec3);
|
|
|
|
ADD_TYPE(Vec4);
|
|
|
|
ADD_TYPE(Double);
|
|
|
|
ADD_TYPE(DVec2);
|
|
|
|
ADD_TYPE(DVec3);
|
|
|
|
ADD_TYPE(DVec4);
|
|
|
|
ADD_TYPE(Int);
|
|
|
|
ADD_TYPE(IVec2);
|
|
|
|
ADD_TYPE(IVec3);
|
|
|
|
ADD_TYPE(IVec4);
|
|
|
|
ADD_TYPE(UInt);
|
|
|
|
ADD_TYPE(UVec2);
|
|
|
|
ADD_TYPE(UVec3);
|
|
|
|
ADD_TYPE(UVec4);
|
|
|
|
ADD_TYPE(Bool);
|
|
|
|
ADD_TYPE(BVec2);
|
|
|
|
ADD_TYPE(BVec3);
|
|
|
|
ADD_TYPE(BVec4);
|
|
|
|
ADD_TYPE(Mat2x2);
|
2016-11-21 15:39:35 +00:00
|
|
|
types->addWithoutOwnership(SkString("mat2x2"), fContext.fMat2x2_Type.get());
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(Mat2x3);
|
|
|
|
ADD_TYPE(Mat2x4);
|
|
|
|
ADD_TYPE(Mat3x2);
|
|
|
|
ADD_TYPE(Mat3x3);
|
2016-11-21 15:39:35 +00:00
|
|
|
types->addWithoutOwnership(SkString("mat3x3"), fContext.fMat3x3_Type.get());
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(Mat3x4);
|
|
|
|
ADD_TYPE(Mat4x2);
|
|
|
|
ADD_TYPE(Mat4x3);
|
|
|
|
ADD_TYPE(Mat4x4);
|
2016-11-21 15:39:35 +00:00
|
|
|
types->addWithoutOwnership(SkString("mat4x4"), fContext.fMat4x4_Type.get());
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(GenType);
|
|
|
|
ADD_TYPE(GenDType);
|
|
|
|
ADD_TYPE(GenIType);
|
|
|
|
ADD_TYPE(GenUType);
|
|
|
|
ADD_TYPE(GenBType);
|
|
|
|
ADD_TYPE(Mat);
|
|
|
|
ADD_TYPE(Vec);
|
|
|
|
ADD_TYPE(GVec);
|
|
|
|
ADD_TYPE(GVec2);
|
|
|
|
ADD_TYPE(GVec3);
|
|
|
|
ADD_TYPE(GVec4);
|
|
|
|
ADD_TYPE(DVec);
|
|
|
|
ADD_TYPE(IVec);
|
|
|
|
ADD_TYPE(UVec);
|
|
|
|
ADD_TYPE(BVec);
|
|
|
|
|
|
|
|
ADD_TYPE(Sampler1D);
|
|
|
|
ADD_TYPE(Sampler2D);
|
|
|
|
ADD_TYPE(Sampler3D);
|
2016-10-12 13:39:56 +00:00
|
|
|
ADD_TYPE(SamplerExternalOES);
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(SamplerCube);
|
|
|
|
ADD_TYPE(Sampler2DRect);
|
|
|
|
ADD_TYPE(Sampler1DArray);
|
|
|
|
ADD_TYPE(Sampler2DArray);
|
|
|
|
ADD_TYPE(SamplerCubeArray);
|
|
|
|
ADD_TYPE(SamplerBuffer);
|
|
|
|
ADD_TYPE(Sampler2DMS);
|
|
|
|
ADD_TYPE(Sampler2DMSArray);
|
|
|
|
|
2016-11-11 21:08:03 +00:00
|
|
|
ADD_TYPE(ISampler2D);
|
|
|
|
|
2016-11-16 17:06:01 +00:00
|
|
|
ADD_TYPE(Image2D);
|
|
|
|
ADD_TYPE(IImage2D);
|
|
|
|
|
2016-11-22 14:44:03 +00:00
|
|
|
ADD_TYPE(SubpassInput);
|
|
|
|
ADD_TYPE(SubpassInputMS);
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(GSampler1D);
|
|
|
|
ADD_TYPE(GSampler2D);
|
|
|
|
ADD_TYPE(GSampler3D);
|
|
|
|
ADD_TYPE(GSamplerCube);
|
|
|
|
ADD_TYPE(GSampler2DRect);
|
|
|
|
ADD_TYPE(GSampler1DArray);
|
|
|
|
ADD_TYPE(GSampler2DArray);
|
|
|
|
ADD_TYPE(GSamplerCubeArray);
|
|
|
|
ADD_TYPE(GSamplerBuffer);
|
|
|
|
ADD_TYPE(GSampler2DMS);
|
|
|
|
ADD_TYPE(GSampler2DMSArray);
|
|
|
|
|
|
|
|
ADD_TYPE(Sampler1DShadow);
|
|
|
|
ADD_TYPE(Sampler2DShadow);
|
|
|
|
ADD_TYPE(SamplerCubeShadow);
|
|
|
|
ADD_TYPE(Sampler2DRectShadow);
|
|
|
|
ADD_TYPE(Sampler1DArrayShadow);
|
|
|
|
ADD_TYPE(Sampler2DArrayShadow);
|
|
|
|
ADD_TYPE(SamplerCubeArrayShadow);
|
|
|
|
ADD_TYPE(GSampler2DArrayShadow);
|
|
|
|
ADD_TYPE(GSamplerCubeArrayShadow);
|
|
|
|
|
2016-11-21 20:59:48 +00:00
|
|
|
SkString skCapsName("sk_Caps");
|
|
|
|
Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName,
|
|
|
|
*fContext.fSkCaps_Type, Variable::kGlobal_Storage);
|
|
|
|
fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag ignored1;
|
|
|
|
std::vector<std::unique_ptr<ProgramElement>> ignored2;
|
2016-11-21 15:39:35 +00:00
|
|
|
this->internalConvertProgram(SkString(SKSL_INCLUDE), &ignored1, &ignored2);
|
2016-10-20 16:54:00 +00:00
|
|
|
fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
|
2016-07-01 15:22:01 +00:00
|
|
|
ASSERT(!fErrorCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
Compiler::~Compiler() {
|
|
|
|
delete fIRGenerator;
|
|
|
|
}
|
|
|
|
|
2016-10-13 20:25:34 +00:00
|
|
|
// add the definition created by assigning to the lvalue to the definition set
|
2017-01-19 18:32:00 +00:00
|
|
|
void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
|
|
|
|
DefinitionMap* definitions) {
|
2016-10-13 20:25:34 +00:00
|
|
|
switch (lvalue->fKind) {
|
|
|
|
case Expression::kVariableReference_Kind: {
|
|
|
|
const Variable& var = ((VariableReference*) lvalue)->fVariable;
|
|
|
|
if (var.fStorage == Variable::kLocal_Storage) {
|
|
|
|
(*definitions)[&var] = expr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kSwizzle_Kind:
|
|
|
|
// We consider the variable written to as long as at least some of its components have
|
|
|
|
// been written to. This will lead to some false negatives (we won't catch it if you
|
|
|
|
// write to foo.x and then read foo.y), but being stricter could lead to false positives
|
2016-10-26 14:35:22 +00:00
|
|
|
// (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
|
|
|
|
// but since we pass foo as a whole it is flagged as an error) unless we perform a much
|
2016-10-13 20:25:34 +00:00
|
|
|
// more complicated whole-program analysis. This is probably good enough.
|
2016-10-26 14:35:22 +00:00
|
|
|
this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
2016-10-13 20:25:34 +00:00
|
|
|
definitions);
|
|
|
|
break;
|
|
|
|
case Expression::kIndex_Kind:
|
|
|
|
// see comments in Swizzle
|
2016-10-26 14:35:22 +00:00
|
|
|
this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
2016-10-13 20:25:34 +00:00
|
|
|
definitions);
|
|
|
|
break;
|
|
|
|
case Expression::kFieldAccess_Kind:
|
|
|
|
// see comments in Swizzle
|
2016-10-26 14:35:22 +00:00
|
|
|
this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
2016-10-13 20:25:34 +00:00
|
|
|
definitions);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// not an lvalue, can't happen
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add local variables defined by this node to the set
|
2016-10-26 14:35:22 +00:00
|
|
|
void Compiler::addDefinitions(const BasicBlock::Node& node,
|
2017-01-19 18:32:00 +00:00
|
|
|
DefinitionMap* definitions) {
|
2016-10-13 20:25:34 +00:00
|
|
|
switch (node.fKind) {
|
|
|
|
case BasicBlock::Node::kExpression_Kind: {
|
2017-01-19 18:32:00 +00:00
|
|
|
ASSERT(node.fExpression);
|
|
|
|
const Expression* expr = (Expression*) node.fExpression->get();
|
|
|
|
switch (expr->fKind) {
|
|
|
|
case Expression::kBinary_Kind: {
|
|
|
|
BinaryExpression* b = (BinaryExpression*) expr;
|
|
|
|
if (b->fOperator == Token::EQ) {
|
|
|
|
this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
|
|
|
|
} else if (Token::IsAssignment(b->fOperator)) {
|
|
|
|
this->addDefinition(
|
|
|
|
b->fLeft.get(),
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kPrefix_Kind: {
|
|
|
|
const PrefixExpression* p = (PrefixExpression*) expr;
|
|
|
|
if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
|
|
|
|
this->addDefinition(
|
|
|
|
p->fOperand.get(),
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
|
|
|
}
|
|
|
|
break;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
2017-01-19 18:32:00 +00:00
|
|
|
case Expression::kPostfix_Kind: {
|
|
|
|
const PostfixExpression* p = (PostfixExpression*) expr;
|
|
|
|
if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
|
|
|
|
this->addDefinition(
|
|
|
|
p->fOperand.get(),
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
2017-02-06 18:53:07 +00:00
|
|
|
|
2017-01-19 18:32:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BasicBlock::Node::kStatement_Kind: {
|
2017-02-06 18:53:07 +00:00
|
|
|
const Statement* stmt = (Statement*) node.fStatement;
|
2016-10-13 20:25:34 +00:00
|
|
|
if (stmt->fKind == Statement::kVarDeclarations_Kind) {
|
2017-01-19 18:32:00 +00:00
|
|
|
VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt;
|
2017-02-06 18:53:07 +00:00
|
|
|
for (VarDeclaration& decl : vd->fDeclaration->fVars) {
|
|
|
|
if (decl.fValue) {
|
|
|
|
(*definitions)[decl.fVar] = &decl.fValue;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
|
|
|
|
BasicBlock& block = cfg->fBlocks[blockId];
|
|
|
|
|
|
|
|
// compute definitions after this block
|
2017-01-19 18:32:00 +00:00
|
|
|
DefinitionMap after = block.fBefore;
|
2016-10-13 20:25:34 +00:00
|
|
|
for (const BasicBlock::Node& n : block.fNodes) {
|
|
|
|
this->addDefinitions(n, &after);
|
|
|
|
}
|
|
|
|
|
|
|
|
// propagate definitions to exits
|
|
|
|
for (BlockId exitId : block.fExits) {
|
|
|
|
BasicBlock& exit = cfg->fBlocks[exitId];
|
|
|
|
for (const auto& pair : after) {
|
2017-01-19 18:32:00 +00:00
|
|
|
std::unique_ptr<Expression>* e1 = pair.second;
|
|
|
|
auto found = exit.fBefore.find(pair.first);
|
|
|
|
if (found == exit.fBefore.end()) {
|
|
|
|
// exit has no definition for it, just copy it
|
|
|
|
workList->insert(exitId);
|
2016-10-13 20:25:34 +00:00
|
|
|
exit.fBefore[pair.first] = e1;
|
|
|
|
} else {
|
2017-01-19 18:32:00 +00:00
|
|
|
// exit has a (possibly different) value already defined
|
|
|
|
std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
|
2016-10-13 20:25:34 +00:00
|
|
|
if (e1 != e2) {
|
|
|
|
// definition has changed, merge and add exit block to worklist
|
|
|
|
workList->insert(exitId);
|
2017-02-27 18:26:45 +00:00
|
|
|
if (e1 && e2) {
|
|
|
|
exit.fBefore[pair.first] =
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression;
|
2017-02-27 18:26:45 +00:00
|
|
|
} else {
|
|
|
|
exit.fBefore[pair.first] = nullptr;
|
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns a map which maps all local variables in the function to null, indicating that their value
|
|
|
|
// is initially unknown
|
2017-01-19 18:32:00 +00:00
|
|
|
static DefinitionMap compute_start_state(const CFG& cfg) {
|
|
|
|
DefinitionMap result;
|
2016-10-26 14:35:22 +00:00
|
|
|
for (const auto& block : cfg.fBlocks) {
|
|
|
|
for (const auto& node : block.fNodes) {
|
2016-10-13 20:25:34 +00:00
|
|
|
if (node.fKind == BasicBlock::Node::kStatement_Kind) {
|
2017-01-19 18:32:00 +00:00
|
|
|
ASSERT(node.fStatement);
|
2017-02-06 18:53:07 +00:00
|
|
|
const Statement* s = node.fStatement;
|
2016-10-13 20:25:34 +00:00
|
|
|
if (s->fKind == Statement::kVarDeclarations_Kind) {
|
|
|
|
const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
|
2017-02-06 18:53:07 +00:00
|
|
|
for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
|
|
|
|
result[decl.fVar] = nullptr;
|
2016-10-26 14:35:22 +00:00
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-06 18:53:07 +00:00
|
|
|
void Compiler::scanCFG(const FunctionDefinition& f) {
|
|
|
|
CFG cfg = CFGGenerator().getCFG(f);
|
2016-10-13 20:25:34 +00:00
|
|
|
|
2017-02-06 18:53:07 +00:00
|
|
|
// compute the data flow
|
|
|
|
cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg);
|
2016-10-13 20:25:34 +00:00
|
|
|
std::set<BlockId> workList;
|
2017-02-06 18:53:07 +00:00
|
|
|
for (BlockId i = 0; i < cfg.fBlocks.size(); i++) {
|
2016-10-13 20:25:34 +00:00
|
|
|
workList.insert(i);
|
|
|
|
}
|
|
|
|
while (workList.size()) {
|
|
|
|
BlockId next = *workList.begin();
|
|
|
|
workList.erase(workList.begin());
|
2017-02-06 18:53:07 +00:00
|
|
|
this->scanCFG(&cfg, next, &workList);
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for unreachable code
|
|
|
|
for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
|
2016-10-26 14:35:22 +00:00
|
|
|
if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
|
2016-10-13 20:25:34 +00:00
|
|
|
cfg.fBlocks[i].fNodes.size()) {
|
2017-01-19 18:32:00 +00:00
|
|
|
Position p;
|
|
|
|
switch (cfg.fBlocks[i].fNodes[0].fKind) {
|
|
|
|
case BasicBlock::Node::kStatement_Kind:
|
2017-02-06 18:53:07 +00:00
|
|
|
p = cfg.fBlocks[i].fNodes[0].fStatement->fPosition;
|
2017-01-19 18:32:00 +00:00
|
|
|
break;
|
|
|
|
case BasicBlock::Node::kExpression_Kind:
|
|
|
|
p = (*cfg.fBlocks[i].fNodes[0].fExpression)->fPosition;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->error(p, SkString("unreachable"));
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fErrorCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-06 18:53:07 +00:00
|
|
|
// check for undefined variables, perform constant propagation
|
|
|
|
for (BasicBlock& b : cfg.fBlocks) {
|
|
|
|
DefinitionMap definitions = b.fBefore;
|
|
|
|
for (BasicBlock::Node& n : b.fNodes) {
|
|
|
|
if (n.fKind == BasicBlock::Node::kExpression_Kind) {
|
|
|
|
ASSERT(n.fExpression);
|
|
|
|
Expression* expr = n.fExpression->get();
|
|
|
|
if (n.fConstantPropagation) {
|
|
|
|
std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator,
|
|
|
|
definitions);
|
|
|
|
if (optimized) {
|
|
|
|
n.fExpression->reset(optimized.release());
|
|
|
|
expr = n.fExpression->get();
|
2017-02-02 21:11:39 +00:00
|
|
|
}
|
2017-02-06 18:53:07 +00:00
|
|
|
}
|
|
|
|
if (expr->fKind == Expression::kVariableReference_Kind) {
|
|
|
|
const Variable& var = ((VariableReference*) expr)->fVariable;
|
|
|
|
if (var.fStorage == Variable::kLocal_Storage &&
|
|
|
|
!definitions[&var]) {
|
|
|
|
this->error(expr->fPosition,
|
|
|
|
"'" + var.fName + "' has not been assigned");
|
2016-10-26 14:35:22 +00:00
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 18:53:07 +00:00
|
|
|
this->addDefinitions(n, &definitions);
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
2017-02-06 18:53:07 +00:00
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
|
|
|
|
// check for missing return
|
|
|
|
if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) {
|
|
|
|
if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
|
2016-11-21 15:39:35 +00:00
|
|
|
this->error(f.fPosition, SkString("function can exit without returning a value"));
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 15:39:35 +00:00
|
|
|
void Compiler::internalConvertProgram(SkString text,
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag* defaultPrecision,
|
2016-07-01 15:22:01 +00:00
|
|
|
std::vector<std::unique_ptr<ProgramElement>>* result) {
|
|
|
|
Parser parser(text, *fTypes, *this);
|
|
|
|
std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
|
|
|
|
if (fErrorCount) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
*defaultPrecision = Modifiers::kHighp_Flag;
|
2016-07-01 15:22:01 +00:00
|
|
|
for (size_t i = 0; i < parsed.size(); i++) {
|
|
|
|
ASTDeclaration& decl = *parsed[i];
|
|
|
|
switch (decl.fKind) {
|
|
|
|
case ASTDeclaration::kVar_Kind: {
|
2016-09-07 20:37:16 +00:00
|
|
|
std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations(
|
2016-10-26 14:35:22 +00:00
|
|
|
(ASTVarDeclarations&) decl,
|
2016-07-01 15:22:01 +00:00
|
|
|
Variable::kGlobal_Storage);
|
|
|
|
if (s) {
|
|
|
|
result->push_back(std::move(s));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ASTDeclaration::kFunction_Kind: {
|
|
|
|
std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction(
|
|
|
|
(ASTFunction&) decl);
|
2016-10-13 20:25:34 +00:00
|
|
|
if (!fErrorCount && f) {
|
|
|
|
this->scanCFG(*f);
|
2016-07-01 15:22:01 +00:00
|
|
|
result->push_back(std::move(f));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
case ASTDeclaration::kModifiers_Kind: {
|
|
|
|
std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration(
|
|
|
|
(ASTModifiersDeclaration&) decl);
|
|
|
|
if (f) {
|
|
|
|
result->push_back(std::move(f));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
case ASTDeclaration::kInterfaceBlock_Kind: {
|
|
|
|
std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock(
|
|
|
|
(ASTInterfaceBlock&) decl);
|
|
|
|
if (i) {
|
|
|
|
result->push_back(std::move(i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ASTDeclaration::kExtension_Kind: {
|
|
|
|
std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl);
|
|
|
|
if (e) {
|
|
|
|
result->push_back(std::move(e));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
case ASTDeclaration::kPrecision_Kind: {
|
|
|
|
*defaultPrecision = ((ASTPrecision&) decl).fPrecision;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
default:
|
|
|
|
ABORT("unsupported declaration: %s\n", decl.description().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 20:59:48 +00:00
|
|
|
std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text,
|
2016-12-12 20:33:30 +00:00
|
|
|
const Program::Settings& settings) {
|
2016-07-01 15:22:01 +00:00
|
|
|
fErrorText = "";
|
|
|
|
fErrorCount = 0;
|
2016-12-12 20:33:30 +00:00
|
|
|
fIRGenerator->start(&settings);
|
2016-07-25 17:08:54 +00:00
|
|
|
std::vector<std::unique_ptr<ProgramElement>> elements;
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag ignored;
|
2016-07-01 15:22:01 +00:00
|
|
|
switch (kind) {
|
|
|
|
case Program::kVertex_Kind:
|
2016-11-21 15:39:35 +00:00
|
|
|
this->internalConvertProgram(SkString(SKSL_VERT_INCLUDE), &ignored, &elements);
|
2016-07-01 15:22:01 +00:00
|
|
|
break;
|
|
|
|
case Program::kFragment_Kind:
|
2016-11-21 15:39:35 +00:00
|
|
|
this->internalConvertProgram(SkString(SKSL_FRAG_INCLUDE), &ignored, &elements);
|
2016-07-01 15:22:01 +00:00
|
|
|
break;
|
2017-02-16 21:37:32 +00:00
|
|
|
case Program::kGeometry_Kind:
|
|
|
|
this->internalConvertProgram(SkString(SKSL_GEOM_INCLUDE), &ignored, &elements);
|
|
|
|
break;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2016-10-20 16:54:00 +00:00
|
|
|
fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag defaultPrecision;
|
|
|
|
this->internalConvertProgram(text, &defaultPrecision, &elements);
|
2016-12-12 20:33:30 +00:00
|
|
|
auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext,
|
|
|
|
std::move(elements),
|
|
|
|
fIRGenerator->fSymbolTable,
|
|
|
|
fIRGenerator->fInputs));
|
2016-11-21 20:59:48 +00:00
|
|
|
fIRGenerator->finish();
|
2016-07-01 15:22:01 +00:00
|
|
|
this->writeErrorCount();
|
2016-12-12 20:33:30 +00:00
|
|
|
if (fErrorCount) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-25 17:08:54 +00:00
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
bool Compiler::toSPIRV(const Program& program, SkWStream& out) {
|
|
|
|
SPIRVCodeGenerator cg(&fContext, &program, this, &out);
|
|
|
|
bool result = cg.generateCode();
|
|
|
|
this->writeErrorCount();
|
2016-07-01 15:22:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
bool Compiler::toSPIRV(const Program& program, SkString* out) {
|
|
|
|
SkDynamicMemoryWStream buffer;
|
|
|
|
bool result = this->toSPIRV(program, buffer);
|
|
|
|
if (result) {
|
|
|
|
sk_sp<SkData> data(buffer.detachAsData());
|
|
|
|
*out = SkString((const char*) data->data(), data->size());
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2016-12-12 20:33:30 +00:00
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
bool Compiler::toGLSL(const Program& program, SkWStream& out) {
|
|
|
|
GLSLCodeGenerator cg(&fContext, &program, this, &out);
|
|
|
|
bool result = cg.generateCode();
|
|
|
|
this->writeErrorCount();
|
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
bool Compiler::toGLSL(const Program& program, SkString* out) {
|
2016-11-21 15:39:35 +00:00
|
|
|
SkDynamicMemoryWStream buffer;
|
2016-12-12 20:33:30 +00:00
|
|
|
bool result = this->toGLSL(program, buffer);
|
2016-07-01 15:22:01 +00:00
|
|
|
if (result) {
|
2016-11-21 15:39:35 +00:00
|
|
|
sk_sp<SkData> data(buffer.detachAsData());
|
|
|
|
*out = SkString((const char*) data->data(), data->size());
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2016-08-03 19:43:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
|
|
|
|
void Compiler::error(Position position, SkString msg) {
|
|
|
|
fErrorCount++;
|
|
|
|
fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
|
2016-11-21 20:59:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
SkString Compiler::errorText() {
|
|
|
|
SkString result = fErrorText;
|
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
void Compiler::writeErrorCount() {
|
|
|
|
if (fErrorCount) {
|
|
|
|
fErrorText += to_string(fErrorCount) + " error";
|
|
|
|
if (fErrorCount > 1) {
|
|
|
|
fErrorText += "s";
|
|
|
|
}
|
|
|
|
fErrorText += "\n";
|
2016-08-03 19:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
} // namespace
|