2015-05-15 22:39:43 +00:00
|
|
|
#ifndef HECLFRONTEND_HPP
|
|
|
|
#define HECLFRONTEND_HPP
|
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <forward_list>
|
|
|
|
#include <Athena/Types.hpp>
|
2015-11-10 02:06:27 +00:00
|
|
|
#include <Athena/DNA.hpp>
|
2015-10-14 02:16:21 +00:00
|
|
|
#include <HECL/HECL.hpp>
|
2015-10-09 02:08:10 +00:00
|
|
|
|
|
|
|
namespace HECL
|
|
|
|
{
|
|
|
|
namespace Frontend
|
|
|
|
{
|
|
|
|
|
|
|
|
struct SourceLocation
|
|
|
|
{
|
|
|
|
int line = -1;
|
|
|
|
int col = -1;
|
|
|
|
SourceLocation() = default;
|
|
|
|
SourceLocation(int l, int c) : line(l), col(c) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Diagnostics
|
|
|
|
{
|
|
|
|
std::string m_name;
|
2015-10-14 23:06:47 +00:00
|
|
|
std::string m_source;
|
2015-10-16 00:34:47 +00:00
|
|
|
std::string m_backend = "Backend";
|
2015-10-14 23:06:47 +00:00
|
|
|
std::string sourceDiagString(const SourceLocation& l, bool ansi=false) const;
|
2015-10-09 02:08:10 +00:00
|
|
|
public:
|
2015-10-14 23:06:47 +00:00
|
|
|
void reset(const std::string& name, const std::string& source) {m_name = name; m_source = source;}
|
2015-11-16 04:30:06 +00:00
|
|
|
void reset(const std::string& name) {m_name = name; m_source.clear();}
|
2015-10-16 00:34:47 +00:00
|
|
|
void setBackend(const std::string& backend) {m_backend = backend;}
|
|
|
|
void setBackend(const char* backend) {m_backend = backend;}
|
2015-10-09 02:08:10 +00:00
|
|
|
void reportParserErr(const SourceLocation& l, const char* format, ...);
|
|
|
|
void reportLexerErr(const SourceLocation& l, const char* format, ...);
|
2015-10-14 02:16:21 +00:00
|
|
|
void reportCompileErr(const SourceLocation& l, const char* format, ...);
|
|
|
|
void reportBackendErr(const SourceLocation& l, const char* format, ...);
|
2015-10-14 23:06:47 +00:00
|
|
|
|
|
|
|
const std::string& getName() const {return m_name;}
|
|
|
|
const std::string& getSource() const {return m_source;}
|
2015-10-09 02:08:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum TokenType
|
|
|
|
{
|
|
|
|
TokenNone,
|
|
|
|
TokenSourceBegin,
|
|
|
|
TokenSourceEnd,
|
|
|
|
TokenNumLiteral,
|
|
|
|
TokenVectorSwizzle,
|
|
|
|
TokenEvalGroupStart,
|
|
|
|
TokenEvalGroupEnd,
|
|
|
|
TokenFunctionStart,
|
|
|
|
TokenFunctionEnd,
|
|
|
|
TokenFunctionArgDelim,
|
|
|
|
TokenArithmeticOp,
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
Diagnostics& m_diag;
|
|
|
|
const std::string* m_source = nullptr;
|
|
|
|
std::string::const_iterator m_sourceIt;
|
|
|
|
std::vector<TokenType> m_parenStack;
|
|
|
|
bool m_reset = false;
|
|
|
|
void skipWhitespace(std::string::const_iterator& it);
|
|
|
|
public:
|
|
|
|
struct Token
|
|
|
|
{
|
|
|
|
TokenType m_type;
|
|
|
|
SourceLocation m_location;
|
|
|
|
std::string m_tokenString;
|
|
|
|
int m_tokenInt = 0;
|
|
|
|
float m_tokenFloat = 0.0;
|
|
|
|
Token() : m_type(TokenNone) {}
|
|
|
|
Token(TokenType type, SourceLocation loc) : m_type(type), m_location(loc) {}
|
2015-10-14 23:06:47 +00:00
|
|
|
const char* typeString() const
|
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case TokenNone:
|
|
|
|
return "None";
|
|
|
|
case TokenSourceBegin:
|
|
|
|
return "SourceBegin";
|
|
|
|
case TokenSourceEnd:
|
|
|
|
return "SourceEnd";
|
|
|
|
case TokenNumLiteral:
|
|
|
|
return "NumLiteral";
|
|
|
|
case TokenVectorSwizzle:
|
|
|
|
return "VectorSwizzle";
|
|
|
|
case TokenEvalGroupStart:
|
|
|
|
return "EvalGroupStart";
|
|
|
|
case TokenEvalGroupEnd:
|
|
|
|
return "EvalGroupEnd";
|
|
|
|
case TokenFunctionStart:
|
|
|
|
return "FunctionStart";
|
|
|
|
case TokenFunctionEnd:
|
|
|
|
return "FunctionEnd";
|
|
|
|
case TokenFunctionArgDelim:
|
|
|
|
return "FunctionArgDelim";
|
|
|
|
case TokenArithmeticOp:
|
|
|
|
return "ArithmeticOp";
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-09 02:08:10 +00:00
|
|
|
};
|
|
|
|
void reset(const std::string& source);
|
|
|
|
Token consumeToken();
|
|
|
|
SourceLocation getLocation() const;
|
|
|
|
|
|
|
|
Parser(Diagnostics& diag) : m_diag(diag) {}
|
|
|
|
};
|
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
using BigDNA = Athena::io::DNA<Athena::BigEndian>;
|
|
|
|
|
|
|
|
struct IR : BigDNA
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
2015-11-10 02:06:27 +00:00
|
|
|
Delete _d;
|
|
|
|
|
|
|
|
enum OpType : uint8_t
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
|
|
|
OpNone, /**< NOP */
|
|
|
|
OpCall, /**< Deferred function insertion for HECL backend using specified I/O regs */
|
|
|
|
OpLoadImm, /**< Load a constant (numeric literal) into register */
|
|
|
|
OpArithmetic, /**< Perform binary arithmetic between registers */
|
|
|
|
OpSwizzle /**< Vector insertion/extraction/swizzling operation */
|
|
|
|
};
|
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
using RegID = atUint16;
|
2015-10-09 02:08:10 +00:00
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
struct Instruction : BigDNA
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
2015-11-10 02:06:27 +00:00
|
|
|
Delete _d;
|
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
OpType m_op = OpNone;
|
2015-11-10 02:06:27 +00:00
|
|
|
RegID m_target = RegID(-1);
|
2015-10-14 02:16:21 +00:00
|
|
|
SourceLocation m_loc;
|
2015-10-09 02:08:10 +00:00
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
struct Call : BigDNA
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
2015-11-10 02:06:27 +00:00
|
|
|
DECL_DNA
|
|
|
|
String<-1> m_name;
|
|
|
|
Value<atUint16> m_argInstCount;
|
|
|
|
Vector<atUint16, DNA_COUNT(m_argInstCount)> m_argInstIdxs;
|
2015-10-09 02:08:10 +00:00
|
|
|
} m_call;
|
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
struct LoadImm : BigDNA
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
2015-11-10 02:06:27 +00:00
|
|
|
DECL_DNA
|
2015-11-14 23:40:32 +00:00
|
|
|
Value<atVec4f> m_immVec = {};
|
2015-10-09 02:08:10 +00:00
|
|
|
} m_loadImm;
|
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
enum ArithmeticOpType : uint8_t
|
2015-10-09 23:55:13 +00:00
|
|
|
{
|
|
|
|
ArithmeticOpNone,
|
|
|
|
ArithmeticOpAdd,
|
|
|
|
ArithmeticOpSubtract,
|
|
|
|
ArithmeticOpMultiply,
|
|
|
|
ArithmeticOpDivide
|
|
|
|
};
|
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
struct Arithmetic : BigDNA
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
2015-11-10 02:06:27 +00:00
|
|
|
DECL_DNA
|
|
|
|
Value<ArithmeticOpType> m_op = ArithmeticOpNone;
|
|
|
|
Value<atUint16> m_instIdxs[2];
|
2015-10-09 02:08:10 +00:00
|
|
|
} m_arithmetic;
|
|
|
|
|
2015-11-10 02:06:27 +00:00
|
|
|
struct Swizzle : BigDNA
|
2015-10-09 02:08:10 +00:00
|
|
|
{
|
2015-11-10 02:06:27 +00:00
|
|
|
DECL_DNA
|
|
|
|
Value<atInt8> m_idxs[4] = {-1, -1, -1, -1};
|
|
|
|
Value<atUint16> m_instIdx;
|
2015-10-09 02:08:10 +00:00
|
|
|
} m_swizzle;
|
2015-10-09 23:55:13 +00:00
|
|
|
|
2015-10-14 02:16:21 +00:00
|
|
|
Instruction(OpType type, const SourceLocation& loc) : m_op(type), m_loc(loc) {}
|
|
|
|
|
2015-10-15 22:24:34 +00:00
|
|
|
int getChildCount() const
|
2015-10-14 02:16:21 +00:00
|
|
|
{
|
|
|
|
switch (m_op)
|
|
|
|
{
|
|
|
|
case OpCall:
|
|
|
|
return m_call.m_argInstIdxs.size();
|
|
|
|
case OpArithmetic:
|
|
|
|
return 2;
|
|
|
|
case OpSwizzle:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
LogModule.report(LogVisor::FatalError, "invalid op type");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IR::Instruction& getChildInst(const IR& ir, size_t idx) const
|
|
|
|
{
|
|
|
|
switch (m_op)
|
|
|
|
{
|
|
|
|
case OpCall:
|
|
|
|
return ir.m_instructions.at(m_call.m_argInstIdxs.at(idx));
|
|
|
|
case OpArithmetic:
|
|
|
|
if (idx > 1)
|
|
|
|
LogModule.report(LogVisor::FatalError, "arithmetic child idx must be 0 or 1");
|
|
|
|
return ir.m_instructions.at(m_arithmetic.m_instIdxs[idx]);
|
|
|
|
case OpSwizzle:
|
|
|
|
if (idx > 0)
|
|
|
|
LogModule.report(LogVisor::FatalError, "swizzle child idx must be 0");
|
|
|
|
return ir.m_instructions.at(m_swizzle.m_instIdx);
|
|
|
|
default:
|
|
|
|
LogModule.report(LogVisor::FatalError, "invalid op type");
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const atVec4f& getImmVec() const
|
|
|
|
{
|
|
|
|
if (m_op != OpLoadImm)
|
|
|
|
LogModule.report(LogVisor::FatalError, "invalid op type");
|
|
|
|
return m_loadImm.m_immVec;
|
|
|
|
}
|
2015-11-10 02:06:27 +00:00
|
|
|
|
|
|
|
void read(Athena::io::IStreamReader& reader)
|
|
|
|
{
|
|
|
|
m_op = OpType(reader.readUByte());
|
|
|
|
m_target = reader.readUint16Big();
|
|
|
|
switch (m_op)
|
|
|
|
{
|
|
|
|
default: break;
|
|
|
|
case OpCall:
|
|
|
|
m_call.read(reader);
|
|
|
|
break;
|
|
|
|
case OpLoadImm:
|
|
|
|
m_loadImm.read(reader);
|
|
|
|
break;
|
|
|
|
case OpArithmetic:
|
|
|
|
m_arithmetic.read(reader);
|
|
|
|
break;
|
|
|
|
case OpSwizzle:
|
|
|
|
m_swizzle.read(reader);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Athena::io::IStreamWriter& writer) const
|
|
|
|
{
|
|
|
|
writer.writeUByte(m_op);
|
|
|
|
writer.writeUint16Big(m_target);
|
|
|
|
switch (m_op)
|
|
|
|
{
|
|
|
|
default: break;
|
|
|
|
case OpCall:
|
|
|
|
m_call.write(writer);
|
|
|
|
break;
|
|
|
|
case OpLoadImm:
|
|
|
|
m_loadImm.write(writer);
|
|
|
|
break;
|
|
|
|
case OpArithmetic:
|
|
|
|
m_arithmetic.write(writer);
|
|
|
|
break;
|
|
|
|
case OpSwizzle:
|
|
|
|
m_swizzle.write(writer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t binarySize(size_t sz) const
|
|
|
|
{
|
|
|
|
sz += 3;
|
|
|
|
switch (m_op)
|
|
|
|
{
|
|
|
|
default: break;
|
|
|
|
case OpCall:
|
|
|
|
sz = m_call.binarySize(sz);
|
|
|
|
break;
|
|
|
|
case OpLoadImm:
|
|
|
|
sz = m_loadImm.binarySize(sz);
|
|
|
|
break;
|
|
|
|
case OpArithmetic:
|
|
|
|
sz = m_arithmetic.binarySize(sz);
|
|
|
|
break;
|
|
|
|
case OpSwizzle:
|
|
|
|
sz = m_swizzle.binarySize(sz);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction(Athena::io::IStreamReader& reader) {read(reader);}
|
2015-10-09 02:08:10 +00:00
|
|
|
};
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
atUint64 m_hash = 0;
|
2015-11-10 02:06:27 +00:00
|
|
|
atUint16 m_regCount = 0;
|
2015-10-09 02:08:10 +00:00
|
|
|
std::vector<Instruction> m_instructions;
|
2015-11-10 02:06:27 +00:00
|
|
|
|
|
|
|
void read(Athena::io::IStreamReader& reader)
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
m_hash = reader.readUint64Big();
|
2015-11-10 02:06:27 +00:00
|
|
|
m_regCount = reader.readUint16Big();
|
|
|
|
atUint16 instCount = reader.readUint16Big();
|
|
|
|
m_instructions.clear();
|
|
|
|
m_instructions.reserve(instCount);
|
|
|
|
for (atUint16 i=0 ; i<instCount ; ++i)
|
|
|
|
m_instructions.emplace_back(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Athena::io::IStreamWriter& writer) const
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
writer.writeUint64Big(m_hash);
|
2015-11-10 02:06:27 +00:00
|
|
|
writer.writeUint16Big(m_regCount);
|
|
|
|
writer.writeUint16Big(m_instructions.size());
|
|
|
|
for (const Instruction& inst : m_instructions)
|
|
|
|
inst.write(writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t binarySize(size_t sz) const
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
sz += 12;
|
2015-11-10 02:06:27 +00:00
|
|
|
for (const Instruction& inst : m_instructions)
|
|
|
|
sz = inst.binarySize(sz);
|
|
|
|
return sz;
|
|
|
|
}
|
2015-10-09 02:08:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Lexer
|
|
|
|
{
|
|
|
|
friend class OperationNode;
|
|
|
|
Diagnostics& m_diag;
|
|
|
|
|
|
|
|
/* Intermediate tree-node for organizing tokens into operations */
|
|
|
|
struct OperationNode
|
|
|
|
{
|
|
|
|
Parser::Token m_tok;
|
|
|
|
OperationNode* m_prev = nullptr;
|
|
|
|
OperationNode* m_next = nullptr;
|
|
|
|
OperationNode* m_sub = nullptr;
|
|
|
|
|
|
|
|
OperationNode() {}
|
|
|
|
OperationNode(Parser::Token&& tok) : m_tok(std::move(tok)) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Pool of nodes to keep ownership (forward_list so pointers aren't invalidated) */
|
|
|
|
std::forward_list<OperationNode> m_pool;
|
|
|
|
|
|
|
|
/* Final lexed root function (IR comes from this) */
|
|
|
|
OperationNode* m_root = nullptr;
|
|
|
|
|
2015-10-09 23:55:13 +00:00
|
|
|
/* Helper for relinking operator precedence */
|
2015-10-14 02:16:21 +00:00
|
|
|
void ReconnectArithmetic(OperationNode* sn, OperationNode** lastSub, OperationNode** newSub) const;
|
2015-10-09 23:55:13 +00:00
|
|
|
|
|
|
|
/* Recursive IR compile funcs */
|
2015-10-14 02:16:21 +00:00
|
|
|
void RecursiveFuncCompile(IR& ir, const Lexer::OperationNode* funcNode, IR::RegID target) const;
|
|
|
|
void RecursiveGroupCompile(IR& ir, const Lexer::OperationNode* groupNode, IR::RegID target) const;
|
|
|
|
void EmitVec3(IR& ir, const Lexer::OperationNode* funcNode, IR::RegID target) const;
|
|
|
|
void EmitVec4(IR& ir, const Lexer::OperationNode* funcNode, IR::RegID target) const;
|
|
|
|
void EmitArithmetic(IR& ir, const Lexer::OperationNode* arithNode, IR::RegID target) const;
|
|
|
|
void EmitVectorSwizzle(IR& ir, const Lexer::OperationNode* swizNode, IR::RegID target) const;
|
2015-10-09 23:55:13 +00:00
|
|
|
|
2015-10-14 23:06:47 +00:00
|
|
|
static void PrintChain(const Lexer::OperationNode* begin, const Lexer::OperationNode* end);
|
|
|
|
static void PrintTree(const Lexer::OperationNode* node, int indent=0);
|
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
public:
|
|
|
|
void reset();
|
|
|
|
void consumeAllTokens(Parser& parser);
|
2015-11-16 04:30:06 +00:00
|
|
|
IR compileIR(atUint64 hash) const;
|
2015-10-09 02:08:10 +00:00
|
|
|
|
|
|
|
Lexer(Diagnostics& diag) : m_diag(diag) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Frontend
|
|
|
|
{
|
|
|
|
Diagnostics m_diag;
|
|
|
|
Parser m_parser;
|
|
|
|
Lexer m_lexer;
|
|
|
|
public:
|
|
|
|
IR compileSource(const std::string& source, const std::string& diagName)
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
Hash hash(source);
|
2015-10-14 23:06:47 +00:00
|
|
|
m_diag.reset(diagName, source);
|
2015-10-09 02:08:10 +00:00
|
|
|
m_parser.reset(source);
|
|
|
|
m_lexer.consumeAllTokens(m_parser);
|
2015-11-16 04:30:06 +00:00
|
|
|
return m_lexer.compileIR(hash);
|
2015-10-09 02:08:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 02:16:21 +00:00
|
|
|
Diagnostics& getDiagnostics() {return m_diag;}
|
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
Frontend() : m_parser(m_diag), m_lexer(m_diag) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:39:43 +00:00
|
|
|
#endif // HECLFRONTEND_HPP
|