Commit d445bb28 by Greg Fischer

Create separate OpSource for each included file

parent 9ed38739
...@@ -1297,6 +1297,10 @@ TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const gl ...@@ -1297,6 +1297,10 @@ TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const gl
text.append("#line 1\n"); text.append("#line 1\n");
text.append(glslangIntermediate->getSourceText()); text.append(glslangIntermediate->getSourceText());
builder.setSourceText(text); builder.setSourceText(text);
// Pass name and text for all included files
const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText();
for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr)
builder.addInclude(iItr->first, iItr->second);
} }
stdBuiltins = builder.import("GLSL.std.450"); stdBuiltins = builder.import("GLSL.std.450");
if (glslangIntermediate->usingVulkanMemoryModel()) { if (glslangIntermediate->usingVulkanMemoryModel()) {
......
...@@ -114,20 +114,8 @@ void Builder::setLine(int lineNum, const char* filename) ...@@ -114,20 +114,8 @@ void Builder::setLine(int lineNum, const char* filename)
currentLine = lineNum; currentLine = lineNum;
currentFile = filename; currentFile = filename;
if (emitOpLines) { if (emitOpLines) {
// If filename previously seen, use its id, else create a string spv::Id strId = getStringId(filename);
// and put it in the map. addLine(strId, currentLine, 0);
auto sItr = stringIds.find(filename);
if (sItr != stringIds.end()) {
addLine(sItr->second, currentLine, 0);
} else {
Instruction* fileString =
new Instruction(getUniqueId(), NoType, OpString);
fileString->addStringOperand(filename);
spv::Id stringId = fileString->getResultId();
strings.push_back(std::unique_ptr<Instruction>(fileString));
addLine(stringId, currentLine, 0);
stringIds[filename] = stringId;
}
} }
} }
} }
...@@ -2843,7 +2831,8 @@ void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* els ...@@ -2843,7 +2831,8 @@ void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* els
// OpSource // OpSource
// [OpSourceContinued] // [OpSourceContinued]
// ... // ...
void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const void Builder::dumpSourceInstructions(const spv::Id fileId, const std::string& text,
std::vector<unsigned int>& out) const
{ {
const int maxWordCount = 0xFFFF; const int maxWordCount = 0xFFFF;
const int opSourceWordCount = 4; const int opSourceWordCount = 4;
...@@ -2855,14 +2844,14 @@ void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const ...@@ -2855,14 +2844,14 @@ void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const
sourceInst.addImmediateOperand(source); sourceInst.addImmediateOperand(source);
sourceInst.addImmediateOperand(sourceVersion); sourceInst.addImmediateOperand(sourceVersion);
// File operand // File operand
if (sourceFileStringId != NoResult) { if (fileId != NoResult) {
sourceInst.addIdOperand(sourceFileStringId); sourceInst.addIdOperand(fileId);
// Source operand // Source operand
if (sourceText.size() > 0) { if (text.size() > 0) {
int nextByte = 0; int nextByte = 0;
std::string subString; std::string subString;
while ((int)sourceText.size() - nextByte > 0) { while ((int)text.size() - nextByte > 0) {
subString = sourceText.substr(nextByte, nonNullBytesPerInstruction); subString = text.substr(nextByte, nonNullBytesPerInstruction);
if (nextByte == 0) { if (nextByte == 0) {
// OpSource // OpSource
sourceInst.addStringOperand(subString.c_str()); sourceInst.addStringOperand(subString.c_str());
...@@ -2882,6 +2871,14 @@ void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const ...@@ -2882,6 +2871,14 @@ void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const
} }
} }
// Dump an OpSource[Continued] sequence for the source and every include file
void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const
{
dumpSourceInstructions(sourceFileStringId, sourceText, out);
for (auto iItr = includeFiles.begin(); iItr != includeFiles.end(); ++iItr)
dumpSourceInstructions(iItr->first, *iItr->second, out);
}
void Builder::dumpInstructions(std::vector<unsigned int>& out, const std::vector<std::unique_ptr<Instruction> >& instructions) const void Builder::dumpInstructions(std::vector<unsigned int>& out, const std::vector<std::unique_ptr<Instruction> >& instructions) const
{ {
for (int i = 0; i < (int)instructions.size(); ++i) { for (int i = 0; i < (int)instructions.size(); ++i) {
......
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
#include <sstream> #include <sstream>
#include <stack> #include <stack>
#include <unordered_map> #include <unordered_map>
#include <map>
namespace spv { namespace spv {
...@@ -74,20 +75,33 @@ public: ...@@ -74,20 +75,33 @@ public:
source = lang; source = lang;
sourceVersion = version; sourceVersion = version;
} }
void setSourceFile(const std::string& file) spv::Id getStringId(const std::string& str)
{ {
Instruction* fileString = new Instruction(getUniqueId(), NoType, OpString); auto sItr = stringIds.find(str);
const char* file_c_str = file.c_str(); if (sItr != stringIds.end())
return sItr->second;
spv::Id strId = getUniqueId();
Instruction* fileString = new Instruction(strId, NoType, OpString);
const char* file_c_str = str.c_str();
fileString->addStringOperand(file_c_str); fileString->addStringOperand(file_c_str);
sourceFileStringId = fileString->getResultId();
strings.push_back(std::unique_ptr<Instruction>(fileString)); strings.push_back(std::unique_ptr<Instruction>(fileString));
stringIds[file_c_str] = sourceFileStringId; stringIds[file_c_str] = strId;
return strId;
}
void setSourceFile(const std::string& file)
{
sourceFileStringId = getStringId(file);
} }
void setSourceText(const std::string& text) { sourceText = text; } void setSourceText(const std::string& text) { sourceText = text; }
void addSourceExtension(const char* ext) { sourceExtensions.push_back(ext); } void addSourceExtension(const char* ext) { sourceExtensions.push_back(ext); }
void addModuleProcessed(const std::string& p) { moduleProcesses.push_back(p.c_str()); } void addModuleProcessed(const std::string& p) { moduleProcesses.push_back(p.c_str()); }
void setEmitOpLines() { emitOpLines = true; } void setEmitOpLines() { emitOpLines = true; }
void addExtension(const char* ext) { extensions.insert(ext); } void addExtension(const char* ext) { extensions.insert(ext); }
void addInclude(const std::string& name, const std::string& text)
{
spv::Id incId = getStringId(name);
includeFiles[incId] = &text;
}
Id import(const char*); Id import(const char*);
void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem) void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem)
{ {
...@@ -658,6 +672,7 @@ public: ...@@ -658,6 +672,7 @@ public:
void createAndSetNoPredecessorBlock(const char*); void createAndSetNoPredecessorBlock(const char*);
void createSelectionMerge(Block* mergeBlock, unsigned int control); void createSelectionMerge(Block* mergeBlock, unsigned int control);
void dumpSourceInstructions(std::vector<unsigned int>&) const; void dumpSourceInstructions(std::vector<unsigned int>&) const;
void dumpSourceInstructions(const spv::Id fileId, const std::string& text, std::vector<unsigned int>&) const;
void dumpInstructions(std::vector<unsigned int>&, const std::vector<std::unique_ptr<Instruction> >&) const; void dumpInstructions(std::vector<unsigned int>&, const std::vector<std::unique_ptr<Instruction> >&) const;
void dumpModuleProcesses(std::vector<unsigned int>&) const; void dumpModuleProcesses(std::vector<unsigned int>&) const;
...@@ -708,6 +723,9 @@ public: ...@@ -708,6 +723,9 @@ public:
// map from strings to their string ids // map from strings to their string ids
std::unordered_map<std::string, spv::Id> stringIds; std::unordered_map<std::string, spv::Id> stringIds;
// map from include file name ids to their contents
std::map<spv::Id, const std::string*> includeFiles;
// The stream for outputting warnings and errors. // The stream for outputting warnings and errors.
SpvBuildLogger* logger; SpvBuildLogger* logger;
}; // end Builder class }; // end Builder class
......
...@@ -668,6 +668,8 @@ public: ...@@ -668,6 +668,8 @@ public:
const std::string& getSourceFile() const { return sourceFile; } const std::string& getSourceFile() const { return sourceFile; }
void addSourceText(const char* text) { sourceText = sourceText + text; } void addSourceText(const char* text) { sourceText = sourceText + text; }
const std::string& getSourceText() const { return sourceText; } const std::string& getSourceText() const { return sourceText; }
const std::map<std::string, std::string>& getIncludeText() const { return includeText; }
void addIncludeText(const char* name, const char* text, size_t len) { includeText[name].assign(text,len); }
void addProcesses(const std::vector<std::string>& p) void addProcesses(const std::vector<std::string>& p)
{ {
for (int i = 0; i < (int)p.size(); ++i) for (int i = 0; i < (int)p.size(); ++i)
...@@ -815,6 +817,9 @@ protected: ...@@ -815,6 +817,9 @@ protected:
std::string sourceFile; std::string sourceFile;
std::string sourceText; std::string sourceText;
// Included text. First string is a name, second is the included text
std::map<std::string, std::string> includeText;
// for OpModuleProcessed, or equivalent // for OpModuleProcessed, or equivalent
TProcesses processes; TProcesses processes;
......
...@@ -653,6 +653,7 @@ int TPpContext::CPPinclude(TPpToken* ppToken) ...@@ -653,6 +653,7 @@ int TPpContext::CPPinclude(TPpToken* ppToken)
epilogue << (res->headerData[res->headerLength - 1] == '\n'? "" : "\n") << epilogue << (res->headerData[res->headerLength - 1] == '\n'? "" : "\n") <<
"#line " << directiveLoc.line + forNextLine << " " << directiveLoc.getStringNameOrNum() << "\n"; "#line " << directiveLoc.line + forNextLine << " " << directiveLoc.getStringNameOrNum() << "\n";
pushInput(new TokenizableIncludeFile(directiveLoc, prologue.str(), res, epilogue.str(), this)); pushInput(new TokenizableIncludeFile(directiveLoc, prologue.str(), res, epilogue.str(), this));
parseContext.intermediate.addIncludeText(res->headerName.c_str(), res->headerData, res->headerLength);
// There's no "current" location anymore. // There's no "current" location anymore.
parseContext.setCurrentColumn(0); parseContext.setCurrentColumn(0);
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment