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>
|
|
|
|
|
|
|
|
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;
|
|
|
|
public:
|
|
|
|
void setName(const std::string& name) {m_name = name;}
|
|
|
|
void reportParserErr(const SourceLocation& l, const char* format, ...);
|
|
|
|
void reportLexerErr(const SourceLocation& l, const char* format, ...);
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {}
|
|
|
|
};
|
|
|
|
void reset(const std::string& source);
|
|
|
|
Token consumeToken();
|
|
|
|
SourceLocation getLocation() const;
|
|
|
|
|
|
|
|
Parser(Diagnostics& diag) : m_diag(diag) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IR
|
|
|
|
{
|
|
|
|
enum OpType
|
|
|
|
{
|
|
|
|
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-10-09 23:55:13 +00:00
|
|
|
using RegID = int;
|
2015-10-09 02:08:10 +00:00
|
|
|
|
|
|
|
struct Instruction
|
|
|
|
{
|
|
|
|
OpType m_op = OpNone;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2015-10-09 23:55:13 +00:00
|
|
|
std::string m_name;
|
2015-10-09 02:08:10 +00:00
|
|
|
RegID m_target;
|
|
|
|
} m_call;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
atVec4f m_immVec;
|
|
|
|
RegID m_target;
|
|
|
|
} m_loadImm;
|
|
|
|
|
2015-10-09 23:55:13 +00:00
|
|
|
enum ArithmeticOpType
|
|
|
|
{
|
|
|
|
ArithmeticOpNone,
|
|
|
|
ArithmeticOpAdd,
|
|
|
|
ArithmeticOpSubtract,
|
|
|
|
ArithmeticOpMultiply,
|
|
|
|
ArithmeticOpDivide
|
|
|
|
};
|
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
struct
|
|
|
|
{
|
2015-10-09 23:55:13 +00:00
|
|
|
ArithmeticOpType m_op = ArithmeticOpNone;
|
2015-10-09 02:08:10 +00:00
|
|
|
RegID m_a;
|
|
|
|
RegID m_b;
|
|
|
|
RegID m_target;
|
|
|
|
} m_arithmetic;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2015-10-09 23:55:13 +00:00
|
|
|
RegID m_reg;
|
|
|
|
int m_idxs[4] = {-1};
|
2015-10-09 02:08:10 +00:00
|
|
|
RegID m_target;
|
|
|
|
} m_swizzle;
|
2015-10-09 23:55:13 +00:00
|
|
|
|
|
|
|
Instruction(OpType type) : m_op(type) {}
|
2015-10-09 02:08:10 +00:00
|
|
|
};
|
|
|
|
|
2015-10-09 23:55:13 +00:00
|
|
|
int m_regCount = 0;
|
2015-10-09 02:08:10 +00:00
|
|
|
std::vector<Instruction> m_instructions;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 */
|
|
|
|
static void ReconnectArithmetic(OperationNode* sn, OperationNode** lastSub, OperationNode** newSub);
|
|
|
|
|
|
|
|
/* Recursive IR compile funcs */
|
|
|
|
static void RecursiveFuncCompile(IR& ir, const Lexer::OperationNode* funcNode, IR::RegID target);
|
|
|
|
static void RecursiveGroupCompile(IR& ir, const Lexer::OperationNode* groupNode, IR::RegID target);
|
|
|
|
static void EmitVec3(IR& ir, const Lexer::OperationNode* funcNode, IR::RegID target);
|
|
|
|
static void EmitVec4(IR& ir, const Lexer::OperationNode* funcNode, IR::RegID target);
|
|
|
|
static void EmitArithmetic(IR& ir, const Lexer::OperationNode* arithNode, IR::RegID target);
|
|
|
|
static void EmitVectorSwizzle(IR& ir, const Lexer::OperationNode* swizNode, IR::RegID target);
|
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
public:
|
|
|
|
void reset();
|
|
|
|
void consumeAllTokens(Parser& parser);
|
|
|
|
IR compileIR() const;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
m_diag.setName(diagName);
|
|
|
|
m_parser.reset(source);
|
|
|
|
m_lexer.consumeAllTokens(m_parser);
|
|
|
|
return m_lexer.compileIR();
|
|
|
|
}
|
|
|
|
|
|
|
|
Frontend() : m_parser(m_diag), m_lexer(m_diag) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:39:43 +00:00
|
|
|
#endif // HECLFRONTEND_HPP
|