aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLCPPCodeGenerator.cpp
diff options
context:
space:
mode:
authorGravatar Ethan Nicholas <ethannicholas@google.com>2017-09-11 16:33:48 +0000
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-09-11 16:34:02 +0000
commit358515491a0d6891e6a709688a30ad087df1beb1 (patch)
treece64223230053df7db85c94b848ad526e64269cd /src/sksl/SkSLCPPCodeGenerator.cpp
parentc576e93d174f3106e072a2f506bca3990b541265 (diff)
Revert "Switch to the new SkSL lexer."
This reverts commit c576e93d174f3106e072a2f506bca3990b541265. Reason for revert: ASAN failures Original change's description: > Switch to the new SkSL lexer. > > This completely replaces flex with a new in-house lexical analyzer generator, > which we have done for performance and memory usage reasons. Flex requires us > to copy strings every time we need the text of a token, whereas this new lexer > allows us to handle strings as a (non-null-terminated) pointer and length > everywhere, eliminating most string copies. > > Bug: skia: > Change-Id: I2add26efc9e20cb699520e82abcf713af3968aca > Reviewed-on: https://skia-review.googlesource.com/39780 > Reviewed-by: Brian Salomon <bsalomon@google.com> > Commit-Queue: Ethan Nicholas <ethannicholas@google.com> TBR=bsalomon@google.com,ethannicholas@google.com Change-Id: If27b750a5f696d06a6bcffed12fe9f0598e084a6 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: skia: Reviewed-on: https://skia-review.googlesource.com/44881 Reviewed-by: Ethan Nicholas <ethannicholas@google.com> Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Diffstat (limited to 'src/sksl/SkSLCPPCodeGenerator.cpp')
-rw-r--r--src/sksl/SkSLCPPCodeGenerator.cpp77
1 files changed, 36 insertions, 41 deletions
diff --git a/src/sksl/SkSLCPPCodeGenerator.cpp b/src/sksl/SkSLCPPCodeGenerator.cpp
index e84954ee85..17bcf0d11f 100644
--- a/src/sksl/SkSLCPPCodeGenerator.cpp
+++ b/src/sksl/SkSLCPPCodeGenerator.cpp
@@ -14,7 +14,7 @@ namespace SkSL {
static bool needs_uniform_var(const Variable& var) {
return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
- var.fType.fName != "colorSpaceXform";
+ strcmp(var.fType.fName.c_str(), "colorSpaceXform");
}
CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
@@ -58,7 +58,7 @@ void CPPCodeGenerator::writeType(const Type& type) {
if (type.kind() == Type::kStruct_Kind) {
INHERITED::writeType(type);
} else {
- this->write(type.fName);
+ this->write(type.name());
}
}
void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
@@ -87,7 +87,7 @@ void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) {
this->write("%s");
if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
- fErrors.error(i.fIndex->fOffset,
+ fErrors.error(i.fIndex->fPosition,
"index into sk_TransformedCoords2D must be an integer literal");
return;
}
@@ -104,7 +104,7 @@ void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
} else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
this->write("%s");
if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
- fErrors.error(i.fIndex->fOffset,
+ fErrors.error(i.fIndex->fPosition,
"index into sk_TextureSamplers must be an integer literal");
return;
}
@@ -118,15 +118,16 @@ void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
}
static const char* default_value(const Type& type) {
- if (type.fName == "float") {
+ const char* name = type.name().c_str();
+ if (!strcmp(name, "float")) {
return "0.0";
- } else if (type.fName == "float2") {
+ } else if (!strcmp(name, "float2")) {
return "float2(0.0)";
- } else if (type.fName == "float3") {
+ } else if (!strcmp(name, "float3")) {
return "float30.0)";
- } else if (type.fName == "float4") {
+ } else if (!strcmp(name, "float4")) {
return "float4(0.0)";
- } else if (type.fName == "floatt4x4" || type.fName == "colorSpaceXform") {
+ } else if (!strcmp(name, "floatt4x4") || !strcmp(name, "colorSpaceXform")) {
return "float4x4(1.0)";
}
ABORT("unsupported default_value type\n");
@@ -154,8 +155,7 @@ void CPPCodeGenerator::writeRuntimeValue(const Type& type, const String& cppCode
fFormatArgs.push_back(cppCode + ".fX");
fFormatArgs.push_back(cppCode + ".fY");
} else {
- this->write(type.fName);
- this->write("\n");
+ printf("%s\n", type.name().c_str());
ABORT("unsupported runtime value type\n");
}
}
@@ -227,11 +227,11 @@ void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
}
fFormatArgs.push_back(code);
} else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
- String name(ref.fVariable.fName);
+ const char* name = ref.fVariable.fName.c_str();
this->writeRuntimeValue(ref.fVariable.fType,
- String::printf("_outer.%s()", name.c_str()).c_str());
+ String::printf("_outer.%s()", name).c_str());
} else {
- this->write(ref.fVariable.fName);
+ this->write(ref.fVariable.fName.c_str());
}
}
}
@@ -358,16 +358,14 @@ void CPPCodeGenerator::addUniform(const Variable& var) {
var.fType == *fContext.fColorSpaceXform_Type) {
type = "kMat44f_GrSLType";
} else {
- ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
- String(var.fName).c_str());
+ ABORT("unsupported uniform type: %s %s;\n", var.fType.name().c_str(), var.fName.c_str());
}
if (var.fModifiers.fLayout.fWhen.size()) {
this->writef(" if (%s) {\n ", var.fModifiers.fLayout.fWhen.c_str());
}
- String name(var.fName);
+ const char* name = var.fName.c_str();
this->writef(" %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
- "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision,
- name.c_str());
+ "%s, \"%s\");\n", HCodeGenerator::FieldName(name).c_str(), type, precision, name);
if (var.fModifiers.fLayout.fWhen.size()) {
this->write(" }\n");
}
@@ -382,7 +380,7 @@ void CPPCodeGenerator::writePrivateVars() {
if (is_private(*decl.fVar)) {
this->writef("%s %s;\n",
HCodeGenerator::FieldType(decl.fVar->fType).c_str(),
- String(decl.fVar->fName).c_str());
+ decl.fVar->fName.c_str());
}
}
}
@@ -397,7 +395,7 @@ void CPPCodeGenerator::writePrivateVarValues() {
VarDeclaration& decl = (VarDeclaration&) *raw;
if (is_private(*decl.fVar) && decl.fValue) {
this->writef("%s = %s;\n",
- String(decl.fVar->fName).c_str(),
+ decl.fVar->fName.c_str(),
decl.fValue->description().c_str());
}
}
@@ -416,12 +414,12 @@ bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
this->addUniform(*u);
if (u->fType == *fContext.fColorSpaceXform_Type) {
if (fNeedColorSpaceHelper) {
- fErrors.error(u->fOffset, "only a single ColorSpaceXform is supported");
+ fErrors.error(u->fPosition, "only a single ColorSpaceXform is supported");
}
fNeedColorSpaceHelper = true;
this->writef(" fColorSpaceHelper.emitCode(args.fUniformHandler, "
"_outer.%s().get());\n",
- String(u->fName).c_str());
+ u->fName.c_str());
}
}
this->writeSection(EMIT_CODE_SECTION);
@@ -455,8 +453,7 @@ void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
wroteProcessor = true;
this->writef(" {\n");
}
- String nameString(u->fName);
- const char* name = nameString.c_str();
+ const char* name = u->fName.c_str();
if (u->fType == *fContext.fFloat4_Type) {
this->writef(" const SkRect %sValue = _outer.%s();\n"
" %s.set4fv(%sVar, 1, (float*) &%sValue);\n",
@@ -488,13 +485,13 @@ void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
const VarDeclarations* decls = (const VarDeclarations*) p.get();
for (const auto& raw : decls->fVars) {
VarDeclaration& decl = (VarDeclaration&) *raw;
- String nameString(decl.fVar->fName);
- const char* name = nameString.c_str();
if (needs_uniform_var(*decl.fVar)) {
+ const char* name = decl.fVar->fName.c_str();
this->writef(" UniformHandle& %s = %sVar;\n"
" (void) %s;\n",
name, HCodeGenerator::FieldName(name).c_str(), name);
} else if (SectionAndParameterHelper::IsParameter(*decl.fVar)) {
+ const char* name = decl.fVar->fName.c_str();
if (!wroteProcessor) {
this->writef(" const %s& _outer = _proc.cast<%s>();\n", fullName,
fullName);
@@ -515,14 +512,14 @@ void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
void CPPCodeGenerator::writeClone() {
if (!this->writeSection(CLONE_SECTION)) {
if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
- fErrors.error(0, "fragment processors with custom @fields must also have a custom"
- "@clone");
+ fErrors.error(Position(1, 1), "fragment processors with custom @fields must also have "
+ "a custom @clone");
}
this->writef("%s::%s(const %s& src)\n"
": INHERITED(src.optimizationFlags())", fFullName.c_str(), fFullName.c_str(),
fFullName.c_str());
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
- String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
+ String fieldName = HCodeGenerator::FieldName(param->fName.c_str());
this->writef("\n, %s(%s)",
fieldName.c_str(),
("src." + fieldName).c_str());
@@ -538,7 +535,7 @@ void CPPCodeGenerator::writeClone() {
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
if (param->fType.kind() == Type::kSampler_Kind) {
this->writef(" this->addTextureSampler(&%s);\n",
- HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
+ HCodeGenerator::FieldName(param->fName.c_str()).c_str());
}
}
for (const Section* s : fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION)) {
@@ -575,8 +572,7 @@ void CPPCodeGenerator::writeGetKey() {
"GrProcessorKeyBuilder* b) const {\n",
fFullName.c_str());
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
- String nameString(param->fName);
- const char* name = nameString.c_str();
+ const char* name = param->fName.c_str();
if (param->fType == *fContext.fColorSpaceXform_Type) {
this->writef(" b->add32(GrColorSpaceXform::XformKey(%s.get()));\n",
HCodeGenerator::FieldName(name).c_str());
@@ -584,7 +580,7 @@ void CPPCodeGenerator::writeGetKey() {
}
if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
(param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
- fErrors.error(param->fOffset,
+ fErrors.error(param->fPosition,
"layout(key) may not be specified on uniforms");
}
switch (param->fModifiers.fLayout.fKey) {
@@ -612,7 +608,7 @@ void CPPCodeGenerator::writeGetKey() {
break;
case Layout::kIdentity_Key:
if (param->fType.kind() != Type::kMatrix_Kind) {
- fErrors.error(param->fOffset,
+ fErrors.error(param->fPosition,
"layout(key=identity) requires matrix type");
}
this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
@@ -660,15 +656,15 @@ bool CPPCodeGenerator::generateCode() {
this->writeSetData(uniforms);
this->writePrivateVars();
for (const auto& u : uniforms) {
+ const char* name = u->fName.c_str();
if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
- this->writef(" UniformHandle %sVar;\n",
- HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
+ this->writef(" UniformHandle %sVar;\n", HCodeGenerator::FieldName(name).c_str());
}
}
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
+ const char* name = param->fName.c_str();
if (needs_uniform_var(*param)) {
- this->writef(" UniformHandle %sVar;\n",
- HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
+ this->writef(" UniformHandle %sVar;\n", HCodeGenerator::FieldName(name).c_str());
}
}
if (fNeedColorSpaceHelper) {
@@ -685,8 +681,7 @@ bool CPPCodeGenerator::generateCode() {
" (void) that;\n",
fullName, fullName, fullName);
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
- String nameString(param->fName);
- const char* name = nameString.c_str();
+ const char* name = param->fName.c_str();
this->writef(" if (%s != that.%s) return false;\n",
HCodeGenerator::FieldName(name).c_str(),
HCodeGenerator::FieldName(name).c_str());