blob: 114f8efeba8bcef24a26319599fe08bb7c783b24 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_COMPILER
9#define SKSL_COMPILER
10
ethannicholas22f939e2016-10-13 13:25:34 -070011#include <set>
ethannicholasb3058bd2016-07-01 08:22:01 -070012#include <vector>
13#include "ir/SkSLProgram.h"
14#include "ir/SkSLSymbolTable.h"
ethannicholas22f939e2016-10-13 13:25:34 -070015#include "SkSLCFGGenerator.h"
ethannicholasd598f792016-07-25 10:08:54 -070016#include "SkSLContext.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070017#include "SkSLErrorReporter.h"
Ethan Nicholas3605ace2016-11-21 15:59:48 -050018#include "SkSLIRGenerator.h"
ethannicholasf789b382016-08-03 12:43:36 -070019#include "SkSLGLSLCodeGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070020
ethannicholas5961bc92016-10-12 06:39:56 -070021#define SK_FRAGCOLOR_BUILTIN 10001
22
ethannicholasb3058bd2016-07-01 08:22:01 -070023namespace SkSL {
24
25class IRGenerator;
26
27/**
28 * Main compiler entry point. This is a traditional compiler design which first parses the .sksl
29 * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to
30 * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce
31 * compiled output.
ethannicholas5961bc92016-10-12 06:39:56 -070032 *
33 * See the README for information about SkSL.
ethannicholasb3058bd2016-07-01 08:22:01 -070034 */
35class Compiler : public ErrorReporter {
36public:
37 Compiler();
38
39 ~Compiler();
40
Ethan Nicholas3605ace2016-11-21 15:59:48 -050041 std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text,
42 std::unordered_map<SkString, CapValue> caps);
ethannicholasb3058bd2016-07-01 08:22:01 -070043
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050044 bool toSPIRV(Program::Kind kind, const SkString& text, SkWStream& out);
ethannicholasf789b382016-08-03 12:43:36 -070045
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050046 bool toSPIRV(Program::Kind kind, const SkString& text, SkString* out);
ethannicholasf789b382016-08-03 12:43:36 -070047
Brian Salomon94efbf52016-11-29 13:43:05 -050048 bool toGLSL(Program::Kind kind, const SkString& text, const GrShaderCaps& caps,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050049 SkWStream& out);
ethannicholasf789b382016-08-03 12:43:36 -070050
Brian Salomon94efbf52016-11-29 13:43:05 -050051 bool toGLSL(Program::Kind kind, const SkString& text, const GrShaderCaps& caps,
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050052 SkString* out);
ethannicholasb3058bd2016-07-01 08:22:01 -070053
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050054 void error(Position position, SkString msg) override;
ethannicholasb3058bd2016-07-01 08:22:01 -070055
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050056 SkString errorText();
ethannicholasb3058bd2016-07-01 08:22:01 -070057
58 void writeErrorCount();
59
60private:
ethannicholas22f939e2016-10-13 13:25:34 -070061 void addDefinition(const Expression* lvalue, const Expression* expr,
62 std::unordered_map<const Variable*, const Expression*>* definitions);
63
64 void addDefinitions(const BasicBlock::Node& node,
65 std::unordered_map<const Variable*, const Expression*>* definitions);
66
67 void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList);
68
69 void scanCFG(const FunctionDefinition& f);
ethannicholasb3058bd2016-07-01 08:22:01 -070070
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050071 void internalConvertProgram(SkString text,
ethannicholas5961bc92016-10-12 06:39:56 -070072 Modifiers::Flag* defaultPrecision,
ethannicholasf789b382016-08-03 12:43:36 -070073 std::vector<std::unique_ptr<ProgramElement>>* result);
ethannicholasb3058bd2016-07-01 08:22:01 -070074
75 std::shared_ptr<SymbolTable> fTypes;
76 IRGenerator* fIRGenerator;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050077 SkString fSkiaVertText; // FIXME store parsed version instead
ethannicholasb3058bd2016-07-01 08:22:01 -070078
ethannicholasd598f792016-07-25 10:08:54 -070079 Context fContext;
ethannicholasb3058bd2016-07-01 08:22:01 -070080 int fErrorCount;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050081 SkString fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -070082};
83
84} // namespace
85
86#endif