forked from KPGPMC/furlang
chore: flat out the file structure
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
file(GLOB_RECURSE FURC_SRCS "${CMAKE_SOURCE_DIR}/src/furc/**.cpp")
|
||||
file(GLOB_RECURSE FURC_HDRS "${CMAKE_SOURCE_DIR}/include/furc/**.hpp")
|
||||
add_library(libfurc ${FURC_SRCS} ${FURC_HDRS})
|
||||
target_include_directories(libfurc PUBLIC ${CMAKE_SOURCE_DIR}/include/)
|
||||
target_compile_definitions(libfurc PRIVATE LIBFURC)
|
||||
set_target_properties(libfurc PROPERTIES PREFIX "")
|
||||
target_link_libraries(libfurc PUBLIC furlang libfurvm)
|
||||
add_executable(furc "${CMAKE_SOURCE_DIR}/src/furc/main.cpp")
|
||||
target_link_libraries(furc PRIVATE libfurc)
|
||||
|
||||
include(GoogleTest)
|
||||
|
||||
add_executable(furc_tests "${CMAKE_SOURCE_DIR}/test/furc/ssa.cpp")
|
||||
target_link_libraries(furc_tests PRIVATE libfurc GTest::gtest_main)
|
||||
gtest_discover_tests(furc_tests)
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "furc/back/furvm/target.hpp"
|
||||
|
||||
#include "furvm/function.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace furc {
|
||||
|
||||
furvm::mod furvm_generator::generate(const ir_module& mod) {
|
||||
if (!mod.variables.empty()) throw std::runtime_error("global variables are not supported for furvm yet");
|
||||
|
||||
furvm::mod furvmMod;
|
||||
for (const auto& func : mod.functions) {
|
||||
furvmMod.emplace_function(furvm::function_sig{ {}, {} }, furvmMod.bytecode().size());
|
||||
}
|
||||
|
||||
return furvmMod;
|
||||
}
|
||||
|
||||
} // namespace furc
|
||||
@@ -0,0 +1,206 @@
|
||||
#include "furc/front/lexer.hpp"
|
||||
|
||||
#include <cctype>
|
||||
#include <limits>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace furc {
|
||||
|
||||
token lexer::next_token() {
|
||||
if (m_peekToken.empty()) return get_token();
|
||||
auto tok = m_peekToken.front();
|
||||
m_peekToken.pop_front();
|
||||
return tok;
|
||||
}
|
||||
|
||||
token lexer::peek_token(std::size_t offset) {
|
||||
if (m_peekToken.size() <= offset) {
|
||||
while (m_peekToken.size() <= offset) {
|
||||
auto tok = get_token();
|
||||
m_peekToken.push_back(tok);
|
||||
}
|
||||
}
|
||||
return m_peekToken[offset];
|
||||
}
|
||||
|
||||
token lexer::get_token() {
|
||||
skip_spaces();
|
||||
|
||||
if (m_cursor >= m_content.size()) return { location(), token::EndOfFile };
|
||||
|
||||
auto loc = location();
|
||||
|
||||
if (std::isdigit(get()) != 0) {
|
||||
std::uint64_t value = get() - '0';
|
||||
next();
|
||||
while (m_cursor < m_content.size() && std::isdigit(get()) != 0) {
|
||||
static constexpr std::uint64_t MAX = std::numeric_limits<std::uint64_t>::max();
|
||||
static constexpr std::uint64_t MAX_MULTS = MAX / 10;
|
||||
static constexpr std::uint64_t LAST_DIGIT = MAX % 10;
|
||||
|
||||
if (value > MAX_MULTS) {
|
||||
return { location(), token::InvalidInteger };
|
||||
}
|
||||
std::uint64_t digit = get() - '0';
|
||||
if (value == MAX_MULTS && digit > LAST_DIGIT) {
|
||||
return { location(), token::InvalidInteger };
|
||||
}
|
||||
|
||||
value *= 10;
|
||||
value += digit;
|
||||
next();
|
||||
}
|
||||
return { loc, value };
|
||||
}
|
||||
|
||||
if (std::isalnum(get()) != 0 || get() == '_') {
|
||||
static std::unordered_map<std::string_view, token_t> s_keywords = {
|
||||
{ "func", token::Func },
|
||||
{ "return", token::Return },
|
||||
{ "if", token::If },
|
||||
{ "else", token::Else },
|
||||
{ "while", token::While },
|
||||
{ "public", token::Public },
|
||||
{ "private", token::Private },
|
||||
{ "pre", token::Pre },
|
||||
{ "post", token::Post },
|
||||
{ "pointerof", token::Pointerof },
|
||||
{ "sizeof", token::Sizeof },
|
||||
{ "lengthof", token::Lengthof },
|
||||
{ "s8", token::S8 },
|
||||
{ "u8", token::U8 },
|
||||
{ "s16", token::S16 },
|
||||
{ "u16", token::U16 },
|
||||
{ "s32", token::S32 },
|
||||
{ "u32", token::U32 },
|
||||
{ "s64", token::S64 },
|
||||
{ "u64", token::U64 },
|
||||
};
|
||||
|
||||
std::size_t begin = m_cursor;
|
||||
next();
|
||||
while (m_cursor < m_content.size() && (std::isalnum(get()) != 0 || get() == '_'))
|
||||
next();
|
||||
std::string_view name = m_content.substr(begin, m_cursor - begin);
|
||||
if (auto it = s_keywords.find(name); it != s_keywords.end()) {
|
||||
return { loc, it->second };
|
||||
}
|
||||
return { loc, token::Identifier, name };
|
||||
}
|
||||
|
||||
if (get() == '"') {
|
||||
next();
|
||||
|
||||
std::size_t begin = m_cursor;
|
||||
while (m_cursor < m_content.size() && get() != '"')
|
||||
next();
|
||||
if (m_cursor >= m_content.size()) return { location(), token::UnexpectedEOF };
|
||||
|
||||
next();
|
||||
return { loc, token::String, m_content.substr(begin, m_cursor - begin - 1) };
|
||||
}
|
||||
|
||||
if (get() == '\'') {
|
||||
next();
|
||||
bool slash = get() == '\\';
|
||||
if (slash) next();
|
||||
auto loc2 = location();
|
||||
char character = get();
|
||||
next();
|
||||
if (get() != '\'') return { location(), token::UnexpectedCharacter, get() };
|
||||
next();
|
||||
if (slash) {
|
||||
switch (character) {
|
||||
case '\\': character = '\\'; break;
|
||||
case 'n': character = '\n'; break;
|
||||
case 'r': character = '\r'; break;
|
||||
case 't': character = '\t'; break;
|
||||
default: return { loc2, token::UnexpectedCharacter, character };
|
||||
}
|
||||
}
|
||||
return { loc, token::Char, character };
|
||||
}
|
||||
|
||||
static std::unordered_map<std::string_view, token_t> s_tokens = {
|
||||
{ "(", token::LParen },
|
||||
{ ")", token::RParen },
|
||||
{ "{", token::LBrace },
|
||||
{ "}", token::RBrace },
|
||||
{ "[", token::LBracket },
|
||||
{ "]", token::RBracket },
|
||||
{ ";", token::Semicolon },
|
||||
{ ":", token::Colon },
|
||||
{ ",", token::Comma },
|
||||
{ ".", token::Dot },
|
||||
{ "+", token::Plus },
|
||||
{ "-", token::Minus },
|
||||
{ "*", token::Star },
|
||||
{ "/", token::Slash },
|
||||
{ "%", token::Percent },
|
||||
{ "&", token::Ampersand },
|
||||
{ "|", token::Pipe },
|
||||
{ "^", token::Hat },
|
||||
{ "&&", token::DblAmpersand },
|
||||
{ "||", token::DblPipe },
|
||||
{ "++", token::DblPlus },
|
||||
{ "--", token::DblMinus },
|
||||
{ "!", token::ExMark },
|
||||
{ "^^", token::CatEars },
|
||||
{ "=", token::Equals },
|
||||
{ "+=", token::PlusEquals },
|
||||
{ "-=", token::MinusEquals },
|
||||
{ "*=", token::StarEquals },
|
||||
{ "/=", token::SlashEquals },
|
||||
{ "%=", token::PercentEquals },
|
||||
{ "&=", token::AmpersandEquals },
|
||||
{ "|=", token::PipeEquals },
|
||||
{ "^=", token::HatEquals },
|
||||
{ "==", token::DblEquals },
|
||||
{ "!=", token::ExEquals },
|
||||
{ "<", token::LessThan },
|
||||
{ "<=", token::LessEquals },
|
||||
{ ">", token::GreaterThan },
|
||||
{ ">=", token::GreaterEquals },
|
||||
{ "->", token::SlimArrow },
|
||||
{ "=>", token::FatArrow },
|
||||
{ "@", token::Monkey },
|
||||
{ "#", token::Sha256 },
|
||||
};
|
||||
|
||||
std::size_t begin = m_cursor;
|
||||
std::size_t len = 1;
|
||||
while (begin + len - 1 < m_content.size() && s_tokens.find(m_content.substr(begin, len)) != s_tokens.end())
|
||||
++len;
|
||||
|
||||
if (len > 1) {
|
||||
auto type = s_tokens[m_content.substr(begin, len - 1)];
|
||||
m_cursor += len - 1;
|
||||
return { loc, type };
|
||||
}
|
||||
|
||||
return { loc, token::UnexpectedCharacter, get() };
|
||||
}
|
||||
|
||||
void lexer::next() {
|
||||
if (m_cursor < m_content.size()) ++m_cursor;
|
||||
}
|
||||
|
||||
constexpr char lexer::get(std::size_t offset) const {
|
||||
return m_content[m_cursor + offset];
|
||||
}
|
||||
|
||||
void lexer::skip_spaces() {
|
||||
while (m_cursor < m_content.size() && std::isspace(get()) != 0) {
|
||||
if (m_content[m_cursor++] == '\n') {
|
||||
++m_row;
|
||||
m_lineStart = m_cursor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constexpr token::location lexer::location() const {
|
||||
return { m_filepath, m_row, m_cursor - m_lineStart };
|
||||
}
|
||||
|
||||
} // namespace furc
|
||||
@@ -0,0 +1,385 @@
|
||||
#include "furc/front/parser.hpp"
|
||||
|
||||
#include "furc/front/ast.hpp"
|
||||
#include "furc/front/token.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
namespace furc {
|
||||
|
||||
ast parser::parse() {
|
||||
ast tree;
|
||||
while (m_lexer.peek_token().type != token::EndOfFile) {
|
||||
auto* decl = parse_decl();
|
||||
assert(decl);
|
||||
tree.decls.push_back(decl);
|
||||
}
|
||||
|
||||
return std::move(tree);
|
||||
}
|
||||
|
||||
stmt_node* parser::parse_stmt() {
|
||||
switch (m_lexer.peek_token().type) {
|
||||
case token::LBrace: return m_arena->allocate<comp_stmt_node>(parse_comp());
|
||||
case token::Return: {
|
||||
m_lexer.next_token();
|
||||
return_stmt_node node;
|
||||
if (m_lexer.peek_token().type != token::Semicolon) {
|
||||
node.value = parse_expr();
|
||||
}
|
||||
eat_token(token::Semicolon);
|
||||
return m_arena->allocate<return_stmt_node>(std::move(node));
|
||||
}
|
||||
case token::If: {
|
||||
m_lexer.next_token();
|
||||
eat_token(token::LParen);
|
||||
if_stmt_node node;
|
||||
node.cond = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
node.thenBranch = parse_stmt();
|
||||
if (m_lexer.peek_token().type == token::Else) {
|
||||
m_lexer.next_token();
|
||||
node.elseBranch = parse_stmt();
|
||||
}
|
||||
return m_arena->allocate<if_stmt_node>(std::move(node));
|
||||
}
|
||||
case token::While: {
|
||||
m_lexer.next_token();
|
||||
eat_token(token::LParen);
|
||||
while_stmt_node node;
|
||||
node.cond = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
node.body = parse_stmt();
|
||||
return m_arena->allocate<while_stmt_node>(std::move(node));
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
if (m_lexer.peek_token().type == token::Func) {
|
||||
eat_token(token::Func);
|
||||
func_decl_node func;
|
||||
|
||||
func.name = std::string(eat_token(token::Identifier).value.string);
|
||||
eat_token(token::LParen);
|
||||
if (m_lexer.peek_token().type != token::RParen) {
|
||||
do {
|
||||
var_decl_node param;
|
||||
|
||||
param.name = std::string(eat_token(token::Identifier).value.string);
|
||||
eat_token(token::Colon);
|
||||
param.type = parse_type();
|
||||
if (m_lexer.peek_token().type == token::Equals) {
|
||||
m_lexer.next_token();
|
||||
param.init = parse_expr();
|
||||
}
|
||||
func.params.emplace_back(std::move(param));
|
||||
} while (eat_token(token::Comma, token::RParen).type == token::Comma);
|
||||
} else {
|
||||
eat_token(token::RParen);
|
||||
}
|
||||
|
||||
if (m_lexer.peek_token().type == token::SlimArrow) {
|
||||
m_lexer.next_token();
|
||||
func.type = parse_type();
|
||||
}
|
||||
|
||||
if (m_lexer.peek_token().type == token::Semicolon) {
|
||||
m_lexer.next_token();
|
||||
return m_arena->allocate<func_decl_node>(std::move(func));
|
||||
}
|
||||
|
||||
func_decl_node::def_s def;
|
||||
|
||||
while (m_lexer.peek_token().type == token::Pre || m_lexer.peek_token().type == token::Post) {
|
||||
auto token = m_lexer.next_token();
|
||||
eat_token(token::LParen);
|
||||
auto* expr = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
|
||||
switch (token.type) {
|
||||
case token::Pre: {
|
||||
def.preConds.push_back(expr);
|
||||
} break;
|
||||
case token::Post: {
|
||||
def.postConds.push_back(expr);
|
||||
} break;
|
||||
default: throw std::runtime_error("unreachable");
|
||||
}
|
||||
}
|
||||
|
||||
def.body = parse_comp();
|
||||
func.def = std::move(def);
|
||||
|
||||
return m_arena->allocate<func_decl_node>(std::move(func));
|
||||
}
|
||||
|
||||
if (m_lexer.peek_token().type == token::Identifier &&
|
||||
(m_lexer.peek_token(1).type == token::Colon || m_lexer.peek_token(1).type == token::Equals)) {
|
||||
var_decl_node var;
|
||||
var.name = std::string(eat_token(token::Identifier).value.string);
|
||||
eat_token(token::Colon); // TODO: Auto-deduce the type
|
||||
var.type = parse_type();
|
||||
if (eat_token(token::Equals, token::Semicolon).type == token::Equals) {
|
||||
var.init = parse_expr();
|
||||
eat_token(token::Semicolon);
|
||||
}
|
||||
|
||||
return m_arena->allocate<var_decl_node>(std::move(var));
|
||||
}
|
||||
|
||||
auto* expr = parse_expr();
|
||||
eat_token(token::Semicolon);
|
||||
return expr;
|
||||
}
|
||||
|
||||
decl_node* parser::parse_decl() {
|
||||
auto first = eat_token(token::Identifier, token::Func);
|
||||
if (first.type == token::Func) {
|
||||
func_decl_node func;
|
||||
|
||||
func.name = std::string(eat_token(token::Identifier).value.string);
|
||||
eat_token(token::LParen);
|
||||
if (m_lexer.peek_token().type != token::RParen) {
|
||||
do {
|
||||
var_decl_node param;
|
||||
|
||||
param.name = std::string(eat_token(token::Identifier).value.string);
|
||||
eat_token(token::Colon);
|
||||
param.type = parse_type();
|
||||
if (m_lexer.peek_token().type == token::Equals) {
|
||||
m_lexer.next_token();
|
||||
param.init = parse_expr();
|
||||
}
|
||||
func.params.emplace_back(std::move(param));
|
||||
} while (eat_token(token::Comma, token::RParen).type == token::Comma);
|
||||
} else {
|
||||
eat_token(token::RParen);
|
||||
}
|
||||
|
||||
if (m_lexer.peek_token().type == token::SlimArrow) {
|
||||
m_lexer.next_token();
|
||||
func.type = parse_type();
|
||||
}
|
||||
|
||||
if (m_lexer.peek_token().type == token::Semicolon) {
|
||||
m_lexer.next_token();
|
||||
return m_arena->allocate<func_decl_node>(std::move(func));
|
||||
}
|
||||
|
||||
func_decl_node::def_s def;
|
||||
|
||||
while (m_lexer.peek_token().type == token::Pre || m_lexer.peek_token().type == token::Post) {
|
||||
auto token = m_lexer.next_token();
|
||||
eat_token(token::LParen);
|
||||
auto* expr = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
|
||||
switch (token.type) {
|
||||
case token::Pre: {
|
||||
def.preConds.push_back(expr);
|
||||
} break;
|
||||
case token::Post: {
|
||||
def.postConds.push_back(expr);
|
||||
} break;
|
||||
default: throw std::runtime_error("unreachable");
|
||||
}
|
||||
}
|
||||
|
||||
def.body = parse_comp();
|
||||
func.def = std::move(def);
|
||||
|
||||
return m_arena->allocate<func_decl_node>(std::move(func));
|
||||
}
|
||||
|
||||
var_decl_node var;
|
||||
var.name = std::string(first.value.string);
|
||||
eat_token(token::Colon); // TODO: Auto-deduce the type
|
||||
var.type = parse_type();
|
||||
if (eat_token(token::Equals, token::Semicolon).type == token::Equals) {
|
||||
var.init = parse_expr();
|
||||
eat_token(token::Semicolon);
|
||||
}
|
||||
|
||||
return m_arena->allocate<var_decl_node>(std::move(var));
|
||||
}
|
||||
|
||||
expr_node* parser::parse_expr() {
|
||||
return parse_expr_right(parse_expr_unary());
|
||||
}
|
||||
|
||||
ast_type parser::parse_type() {
|
||||
auto token =
|
||||
eat_token(token::S8, token::U8, token::S16, token::U16, token::S32, token::U32, token::S64, token::U64);
|
||||
switch (token.type) {
|
||||
case token::S8: return { ast_type::S8 };
|
||||
case token::U8: return { ast_type::U8 };
|
||||
case token::S16: return { ast_type::S16 };
|
||||
case token::U16: return { ast_type::U16 };
|
||||
case token::S32: return { ast_type::S32 };
|
||||
case token::U32: return { ast_type::U32 };
|
||||
case token::S64: return { ast_type::S64 };
|
||||
case token::U64: return { ast_type::U64 };
|
||||
default: throw std::runtime_error("unreachable");
|
||||
}
|
||||
}
|
||||
|
||||
comp_stmt_node parser::parse_comp() {
|
||||
comp_stmt_node comp;
|
||||
|
||||
eat_token(token::LBrace);
|
||||
while (m_lexer.peek_token().type != token::EndOfFile && m_lexer.peek_token().type != token::RBrace) {
|
||||
comp.stmts.push_back(parse_stmt());
|
||||
}
|
||||
eat_token(token::RBrace);
|
||||
|
||||
return comp;
|
||||
}
|
||||
|
||||
expr_node* parser::parse_expr_primary() {
|
||||
auto token = eat_token(token::Identifier, token::LParen, token::If, token::Integer, token::Char);
|
||||
switch (token.type) {
|
||||
case token::Identifier: {
|
||||
return m_arena->allocate<var_read_expr_node>(std::string(token.value.string));
|
||||
}
|
||||
case token::LParen: {
|
||||
group_expr_node group;
|
||||
group.inner = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
return m_arena->allocate<group_expr_node>(std::move(group));
|
||||
}
|
||||
case token::If: {
|
||||
if_expr_node ifExpr;
|
||||
eat_token(token::LParen);
|
||||
ifExpr.cond = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
ifExpr.thenExpr = parse_expr();
|
||||
eat_token(token::Else);
|
||||
ifExpr.elseExpr = parse_expr();
|
||||
return m_arena->allocate<if_expr_node>(std::move(ifExpr));
|
||||
}
|
||||
case token::Integer: {
|
||||
return m_arena->allocate<int_lit_node>(int_lit_node(token.value.integer));
|
||||
}
|
||||
case token::Char: {
|
||||
return m_arena->allocate<char_lit_node>(char_lit_node(token.value.character));
|
||||
}
|
||||
default: throw std::runtime_error("unreachable");
|
||||
}
|
||||
}
|
||||
|
||||
expr_node* parser::parse_expr_unary() {
|
||||
static std::unordered_map<token_t, unary_op_expr_node::unary_op_type> s_prefixOps = {
|
||||
{ token::Plus, unary_op_expr_node::Positive },
|
||||
{ token::Minus, unary_op_expr_node::Negative },
|
||||
{ token::DblPlus, unary_op_expr_node::PreInc },
|
||||
{ token::DblMinus, unary_op_expr_node::PreDec },
|
||||
{ token::Tilde, unary_op_expr_node::BinNot },
|
||||
{ token::ExMark, unary_op_expr_node::Not },
|
||||
|
||||
{ token::Sizeof, unary_op_expr_node::Sizeof },
|
||||
{ token::Pointerof, unary_op_expr_node::Pointerof },
|
||||
{ token::Lengthof, unary_op_expr_node::Lengthof },
|
||||
};
|
||||
|
||||
static std::unordered_map<token_t, unary_op_expr_node::unary_op_type> s_postfixOps = {
|
||||
{ token::DblPlus, unary_op_expr_node::PostInc },
|
||||
{ token::DblMinus, unary_op_expr_node::PostDec },
|
||||
};
|
||||
|
||||
auto it = s_prefixOps.find(m_lexer.peek_token().type);
|
||||
if (it == s_prefixOps.end()) {
|
||||
auto* expr = parse_expr_primary();
|
||||
while (true) {
|
||||
auto postIt = s_postfixOps.find(m_lexer.peek_token().type);
|
||||
if (postIt == s_postfixOps.end()) return expr;
|
||||
m_lexer.next_token();
|
||||
|
||||
unary_op_expr_node unary;
|
||||
unary.lhs = expr;
|
||||
unary.type = postIt->second;
|
||||
expr = m_arena->allocate<unary_op_expr_node>(std::move(unary));
|
||||
}
|
||||
}
|
||||
|
||||
auto token = m_lexer.next_token();
|
||||
|
||||
unary_op_expr_node unary;
|
||||
unary.lhs = parse_expr_unary();
|
||||
unary.type = it->second;
|
||||
return m_arena->allocate<unary_op_expr_node>(std::move(unary));
|
||||
}
|
||||
|
||||
expr_node* parser::parse_expr_right(expr_node* lhs, std::uint32_t precedence) {
|
||||
struct op_info {
|
||||
enum type_e {
|
||||
Binary = 0,
|
||||
FunctionCall,
|
||||
} type;
|
||||
std::uint32_t precedence;
|
||||
union {
|
||||
binary_op_expr_node::binary_op_type binary;
|
||||
};
|
||||
bool right = false;
|
||||
};
|
||||
|
||||
static std::unordered_map<token_t, op_info> s_ops = {
|
||||
{ token::Plus, { op_info::Binary, 4, binary_op_expr_node::Add } },
|
||||
{ token::Minus, { op_info::Binary, 4, binary_op_expr_node::Sub } },
|
||||
{ token::Star, { op_info::Binary, 3, binary_op_expr_node::Mul } },
|
||||
{ token::Slash, { op_info::Binary, 3, binary_op_expr_node::Div } },
|
||||
{ token::Percent, { op_info::Binary, 3, binary_op_expr_node::Mod } },
|
||||
{ token::DblLT, { op_info::Binary, 5, binary_op_expr_node::Shl } },
|
||||
{ token::DblGT, { op_info::Binary, 5, binary_op_expr_node::Shr } },
|
||||
{ token::Ampersand, { op_info::Binary, 8, binary_op_expr_node::BinAnd } },
|
||||
{ token::Pipe, { op_info::Binary, 10, binary_op_expr_node::BinOr } },
|
||||
{ token::Hat, { op_info::Binary, 9, binary_op_expr_node::BinXor } },
|
||||
{ token::DblAmpersand, { op_info::Binary, 11, binary_op_expr_node::And } },
|
||||
{ token::DblPipe, { op_info::Binary, 12, binary_op_expr_node::Or } },
|
||||
{ token::DblEquals, { op_info::Binary, 7, binary_op_expr_node::Equals } },
|
||||
{ token::ExEquals, { op_info::Binary, 7, binary_op_expr_node::NotEquals } },
|
||||
{ token::LessThan, { op_info::Binary, 6, binary_op_expr_node::LessThan } },
|
||||
{ token::LessEquals, { op_info::Binary, 6, binary_op_expr_node::LessEquals } },
|
||||
{ token::GreaterThan, { op_info::Binary, 6, binary_op_expr_node::GreaterThan } },
|
||||
{ token::GreaterEquals, { op_info::Binary, 6, binary_op_expr_node::GreaterEquals } },
|
||||
{ token::LParen, { op_info::FunctionCall, 1 } },
|
||||
};
|
||||
|
||||
while (true) {
|
||||
auto it = s_ops.find(m_lexer.peek_token().type);
|
||||
if (it == s_ops.end() || it->second.precedence >= precedence) return lhs;
|
||||
auto op = it->second;
|
||||
m_lexer.next_token();
|
||||
|
||||
if (op.type == op_info::FunctionCall) {
|
||||
func_call_expr_node funcCall;
|
||||
funcCall.lhs = lhs;
|
||||
|
||||
if (m_lexer.peek_token().type != token::RParen) {
|
||||
do {
|
||||
funcCall.args.push_back(parse_expr());
|
||||
} while (eat_token(token::Comma, token::RParen).type == token::Comma);
|
||||
} else {
|
||||
m_lexer.next_token();
|
||||
}
|
||||
|
||||
lhs = m_arena->allocate<func_call_expr_node>(std::move(funcCall));
|
||||
} else {
|
||||
binary_op_expr_node binary;
|
||||
binary.lhs = lhs;
|
||||
binary.rhs = parse_expr_unary();
|
||||
binary.type = op.binary;
|
||||
|
||||
auto nextIt = s_ops.find(m_lexer.peek_token().type);
|
||||
if (nextIt != s_ops.end()) {
|
||||
binary.rhs = parse_expr_right(binary.rhs, op.precedence + (op.right ? 1 : 0));
|
||||
}
|
||||
|
||||
lhs = m_arena->allocate<binary_op_expr_node>(std::move(binary));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace furc
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef LIBFURC
|
||||
|
||||
#include "furc/front/lexer.hpp"
|
||||
#include "furc/front/parser.hpp"
|
||||
#include "furc/middle/ir.hpp"
|
||||
#include "furc/middle/reg_gen.hpp"
|
||||
#include "furc/middle/ssa.hpp"
|
||||
#include "furlang/arena.hpp"
|
||||
|
||||
int main(void) {
|
||||
furlang::arena arena;
|
||||
|
||||
std::string_view content = R"(
|
||||
func main(argc: u64) -> s32 {
|
||||
x: s32 = 1 + 2 * 3;
|
||||
return if (x == 9) 1 else 0;
|
||||
}
|
||||
)";
|
||||
|
||||
furc::lexer lexer = { "<AK>", content };
|
||||
furc::parser parser = { std::move(lexer), arena };
|
||||
furc::ir_module irModule = furc::ir_generator::generate(parser.parse());
|
||||
for (auto& func : irModule.functions) {
|
||||
furc::ssa ssa(*func);
|
||||
furc::reg_gen gen(*func, ssa);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // LIBFURC
|
||||
@@ -0,0 +1,254 @@
|
||||
#include "furc/middle/ir.hpp"
|
||||
|
||||
#include "furc/front/ast.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace furc {
|
||||
|
||||
static ir_type ast_type_to_ir(const ast_type& type) {
|
||||
switch (type.type) {
|
||||
case ast_type::Void: return { ir_type::Void };
|
||||
case ast_type::S8: return { ir_type::S8 };
|
||||
case ast_type::U8: return { ir_type::U8 };
|
||||
case ast_type::S16: return { ir_type::S16 };
|
||||
case ast_type::U16: return { ir_type::U16 };
|
||||
case ast_type::S32: return { ir_type::S32 };
|
||||
case ast_type::U32: return { ir_type::U32 };
|
||||
case ast_type::S64: return { ir_type::S64 };
|
||||
case ast_type::U64: return { ir_type::U64 };
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
}
|
||||
|
||||
void ir_generator::visit_comp_stmt_node(const comp_stmt_node& node) {
|
||||
// TODO: Introduce scopes for statements to naturally allow variable shadowing.
|
||||
for (const auto& stmt : node.stmts) {
|
||||
stmt->accept(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_if_stmt_node(const if_stmt_node& node) {
|
||||
node.cond->accept(*this);
|
||||
auto* branch = context().terminate(context().last_register(), context().blockIdx + 1, 0);
|
||||
|
||||
context().new_next();
|
||||
node.thenBranch->accept(*this);
|
||||
context().new_next();
|
||||
branch->destination->value.blockPair.second = context().blockIdx; // NOLINT
|
||||
if (node.elseBranch != nullptr) {
|
||||
node.elseBranch->accept(*this);
|
||||
context().new_next();
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_while_stmt_node(const while_stmt_node& node) {
|
||||
context().new_next();
|
||||
auto header = context().blockIdx;
|
||||
node.cond->accept(*this);
|
||||
auto* branch = context().terminate(context().last_register(), context().blockIdx + 1, 0);
|
||||
|
||||
context().new_next();
|
||||
auto body = context().blockIdx;
|
||||
node.body->accept(*this);
|
||||
if (!context().blockPtr->is_terminated()) context().terminate(header);
|
||||
|
||||
context().new_next();
|
||||
branch->destination->value.blockPair.second = context().blockIdx; // NOLINT
|
||||
}
|
||||
|
||||
void ir_generator::visit_return_stmt_node(const return_stmt_node& node) {
|
||||
if (node.value == nullptr) {
|
||||
context().terminate();
|
||||
} else {
|
||||
node.value->accept(*this);
|
||||
context().terminate(context().last_register());
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_var_decl_node(const var_decl_node& node) {
|
||||
const auto* var = m_scope->allocate(m_module.arena, node.name, ast_type_to_ir(node.type));
|
||||
|
||||
if (node.init != nullptr) {
|
||||
if (m_context.empty()) {
|
||||
m_initContext.new_next();
|
||||
m_context.push(std::move(m_initContext));
|
||||
node.init->accept(*this);
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move, var->operand(), { context().last_register() } });
|
||||
m_initContext = std::move(m_context.top());
|
||||
m_context.pop();
|
||||
} else {
|
||||
node.init->accept(*this);
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move, var->operand(), { context().last_register() } });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_func_decl_node(const func_decl_node& node) {
|
||||
ir_function function;
|
||||
function.previous = m_scope;
|
||||
function.name = node.name;
|
||||
for (const auto& param : node.params) {
|
||||
function.params.push_back(ast_type_to_ir(param.type));
|
||||
function.allocate(m_module.arena, param.name, ast_type_to_ir(param.type));
|
||||
}
|
||||
function.retType = ast_type_to_ir(node.type);
|
||||
|
||||
if (node.def.has_value()) {
|
||||
auto* func = m_module.functions.emplace_back(m_module.arena.allocate<ir_function>(std::move(function)));
|
||||
m_context.emplace(func);
|
||||
m_scope = func;
|
||||
node.def->body.accept(*this);
|
||||
m_scope = m_scope->previous;
|
||||
m_context.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void ir_generator::visit_var_read_expr_node(const var_read_expr_node& node) {
|
||||
const auto* var = m_scope->variable(node.name);
|
||||
if (var == nullptr) throw std::runtime_error("unknown variable");
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move, context().next_register(), { var->operand() } });
|
||||
}
|
||||
|
||||
void ir_generator::visit_func_call_expr_node(const func_call_expr_node& node) {
|
||||
throw std::runtime_error("not implemented");
|
||||
}
|
||||
|
||||
void ir_generator::visit_group_expr_node(const group_expr_node& node) {
|
||||
node.inner->accept(*this);
|
||||
}
|
||||
|
||||
void ir_generator::visit_binary_op_expr_node(const binary_op_expr_node& node) {
|
||||
node.lhs->accept(*this);
|
||||
auto lhs = context().last_register();
|
||||
node.rhs->accept(*this);
|
||||
auto rhs = context().last_register();
|
||||
|
||||
switch (node.type) {
|
||||
case binary_op_expr_node::Add:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Add, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Sub:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Sub, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Mul:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Mul, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Div:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Div, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Mod:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Mod, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Shl:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Shl, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Shr:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Shr, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::BinAnd:
|
||||
context().add_instr(ir_instruction{ ir_instruction::BinAnd, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::BinOr:
|
||||
context().add_instr(ir_instruction{ ir_instruction::BinOr, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::BinXor:
|
||||
context().add_instr(ir_instruction{ ir_instruction::BinXor, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::And:
|
||||
context().add_instr(ir_instruction{ ir_instruction::And, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Or:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Or, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::Equals:
|
||||
context().add_instr(ir_instruction{ ir_instruction::Eq, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::NotEquals:
|
||||
context().add_instr(ir_instruction{ ir_instruction::NotEq, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::LessThan:
|
||||
context().add_instr(ir_instruction{ ir_instruction::LessThan, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::LessEquals:
|
||||
context().add_instr(ir_instruction{ ir_instruction::LessEq, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::GreaterThan:
|
||||
context().add_instr(ir_instruction{ ir_instruction::GreaterThan, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
case binary_op_expr_node::GreaterEquals:
|
||||
context().add_instr(ir_instruction{ ir_instruction::GreaterEq, context().next_register(), { lhs, rhs } });
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
}
|
||||
|
||||
void ir_generator::visit_unary_op_expr_node(const unary_op_expr_node& node) {
|
||||
if (node.type == unary_op_expr_node::PostInc || node.type == unary_op_expr_node::PostDec) {
|
||||
node.lhs->accept(*this);
|
||||
auto lhs = context().last_register();
|
||||
node.lhs->accept(*this);
|
||||
context().add_instr(node.type == unary_op_expr_node::PostInc ? ir_instruction::Increment
|
||||
: ir_instruction::Decrement,
|
||||
context().last_register());
|
||||
return;
|
||||
}
|
||||
|
||||
node.lhs->accept(*this);
|
||||
auto lhs = context().last_register();
|
||||
|
||||
switch (node.type) {
|
||||
case unary_op_expr_node::Positive: context().add_instr(ir_instruction::Positive, lhs);
|
||||
case unary_op_expr_node::Negative: context().add_instr(ir_instruction::Negative, lhs);
|
||||
case unary_op_expr_node::PreInc: context().add_instr(ir_instruction::Increment, lhs);
|
||||
case unary_op_expr_node::PreDec: context().add_instr(ir_instruction::Decrement, lhs);
|
||||
case unary_op_expr_node::BinNot: context().add_instr(ir_instruction::BinNot, lhs);
|
||||
case unary_op_expr_node::Not: context().add_instr(ir_instruction::Not, lhs);
|
||||
case unary_op_expr_node::Sizeof: context().add_instr(ir_instruction::Sizeof, lhs);
|
||||
case unary_op_expr_node::Pointerof: context().add_instr(ir_instruction::Pointerof, lhs);
|
||||
case unary_op_expr_node::Lengthof: context().add_instr(ir_instruction::Lenof, lhs);
|
||||
case unary_op_expr_node::PostInc:
|
||||
case unary_op_expr_node::PostDec: return;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
}
|
||||
|
||||
void ir_generator::visit_if_expr_node(const if_expr_node& node) {
|
||||
node.cond->accept(*this);
|
||||
auto* branch = context().terminate(context().last_register(), context().blockIdx + 1, 0);
|
||||
|
||||
context().new_next();
|
||||
auto thenBranch = context().blockIdx;
|
||||
node.thenExpr->accept(*this);
|
||||
auto thenReg = context().last_register();
|
||||
|
||||
context().new_next();
|
||||
branch->destination->value.blockPair.second = context().blockIdx; // NOLINT
|
||||
node.elseExpr->accept(*this);
|
||||
auto elseReg = context().last_register();
|
||||
auto resReg = context().next_register();
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move, resReg, { elseReg } });
|
||||
|
||||
context().new_next();
|
||||
auto epilogue = context().blockIdx;
|
||||
|
||||
context().go(thenBranch);
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move, resReg, { thenReg } });
|
||||
context().terminate(epilogue);
|
||||
context().go(epilogue);
|
||||
}
|
||||
|
||||
void ir_generator::visit_int_lit_node(const int_lit_node& node) {
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move,
|
||||
context().next_register(),
|
||||
{ ir_operand{ ir_operand::Integer, node.value } } });
|
||||
}
|
||||
|
||||
void ir_generator::visit_char_lit_node(const char_lit_node& node) {
|
||||
context().add_instr(ir_instruction{ ir_instruction::Move,
|
||||
context().next_register(),
|
||||
{ ir_operand{ ir_operand::Integer, static_cast<std::uint64_t>(node.value) } } });
|
||||
}
|
||||
|
||||
} // namespace furc
|
||||
@@ -0,0 +1,307 @@
|
||||
/**
|
||||
* Sources:
|
||||
* - Practical Improvements to the Construction and Deconstruction of Static Single Assignment Form:
|
||||
* https://web.archive.org/web/20100607003509/http://www.cs.rice.edu/~harv/my_papers/ssa.pdf
|
||||
* - A Simple, Fast Dominance Algorithm:
|
||||
* https://www.researchgate.net/publication/2569680_A_Simple_Fast_Dominance_Algorithm
|
||||
*/
|
||||
|
||||
#include "furc/middle/ssa.hpp"
|
||||
|
||||
#include "furc/middle/ir.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace furc {
|
||||
|
||||
void ssa::compute_cfg(const std::vector<ir_basic_block>& irBlocks, std::vector<cfg_block>& cfgBlocks) {
|
||||
cfgBlocks.resize(irBlocks.size());
|
||||
|
||||
for (std::size_t i = 0; i < irBlocks.size(); ++i) {
|
||||
const auto& block = irBlocks[i];
|
||||
if (block.instructions.empty()) continue;
|
||||
|
||||
const auto& termInstr = block.instructions.back();
|
||||
switch (termInstr.type) {
|
||||
case ir_instruction::Branch: {
|
||||
const auto& dst = termInstr.destination.value();
|
||||
assert(dst.type == ir_operand::Block);
|
||||
cfgBlocks[dst.value.block].preds.insert(i);
|
||||
cfgBlocks[i].sucs.insert(dst.value.block);
|
||||
} break;
|
||||
case ir_instruction::BranchCond: {
|
||||
const auto& dst = termInstr.destination.value();
|
||||
assert(dst.type == ir_operand::BlockPair);
|
||||
cfgBlocks[dst.value.blockPair.first].preds.insert(i);
|
||||
cfgBlocks[dst.value.blockPair.second].preds.insert(i);
|
||||
cfgBlocks[i].sucs.insert(dst.value.blockPair.first);
|
||||
cfgBlocks[i].sucs.insert(dst.value.blockPair.second);
|
||||
} break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa::collect_registers(const std::vector<ir_basic_block>& irBlocks,
|
||||
std::vector<register_info>& registers,
|
||||
std::unordered_set<std::uint64_t>& globals) {
|
||||
for (std::size_t i = 0; i < irBlocks.size(); ++i) {
|
||||
const auto& block = irBlocks[i];
|
||||
for (const auto& instr : block.instructions) {
|
||||
for (const auto& src : instr.sources) {
|
||||
if (src.type != ir_operand::Register) continue;
|
||||
const auto& reg = registers.at(src.value.reg.name);
|
||||
if (reg.sites.find(i) != reg.sites.end()) continue;
|
||||
globals.insert(src.value.reg.name);
|
||||
}
|
||||
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
|
||||
registers[instr.destination->value.reg.name].sites.insert(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa::build_dtree(const std::vector<cfg_block>& cfgBlocks,
|
||||
std::vector<ssa_block>& ssaBlocks,
|
||||
const std::vector<std::size_t>& order) {
|
||||
ssaBlocks[order.front()].idom = order.front();
|
||||
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
for (auto it = order.begin() + 1; it != order.end(); ++it) {
|
||||
static constexpr std::uint64_t INVALID = std::numeric_limits<std::uint64_t>::max();
|
||||
|
||||
std::uint64_t newIdom = -1;
|
||||
bool found = false;
|
||||
|
||||
for (std::uint64_t pred : cfgBlocks[*it].preds) {
|
||||
if (ssaBlocks[pred].idom == INVALID) continue;
|
||||
newIdom = found ? intersect(ssaBlocks, pred, newIdom) : pred;
|
||||
found = true;
|
||||
}
|
||||
if (ssaBlocks[*it].idom != newIdom) {
|
||||
ssaBlocks[*it].idom = newIdom;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa::compute_dfrontiers(const std::vector<cfg_block>& cfgBlocks, std::vector<ssa_block>& ssaBlocks) {
|
||||
for (std::uint64_t i = 0; i < ssaBlocks.size(); ++i) {
|
||||
if (cfgBlocks[i].preds.size() < 2) continue;
|
||||
const auto& cfgBlock = cfgBlocks[i];
|
||||
auto& ssaBlock = ssaBlocks[i];
|
||||
|
||||
for (std::uint64_t worker : cfgBlock.preds) {
|
||||
while (worker != ssaBlock.idom) {
|
||||
ssaBlocks[worker].df.insert(i);
|
||||
worker = ssaBlocks[worker].idom;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa::compute_rpo(std::vector<cfg_block>& cfgBlocks,
|
||||
std::vector<ssa_block>& ssaBlocks,
|
||||
std::vector<std::size_t>& order) {
|
||||
std::unordered_set<std::size_t> visited;
|
||||
if (!cfgBlocks.empty()) rpo_dfs(visited, order, 0, cfgBlocks);
|
||||
std::reverse(order.begin(), order.end());
|
||||
ssaBlocks.resize(cfgBlocks.size());
|
||||
for (std::size_t i = 0; i < order.size(); ++i) {
|
||||
ssaBlocks[order[i]].order = i;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa::ssaification(std::vector<ir_basic_block>& irBlocks,
|
||||
const std::vector<cfg_block>& cfgBlocks,
|
||||
const std::vector<ssa_block>& ssaBlocks,
|
||||
const std::vector<register_info>& registers,
|
||||
const std::unordered_set<std::uint64_t>& globals) {
|
||||
std::vector<std::uint64_t> worklist;
|
||||
|
||||
for (std::uint64_t i = 0; i < registers.size(); ++i) {
|
||||
const auto& reg = registers[i];
|
||||
if (reg.sites.size() < 2 || globals.find(i) == globals.end()) continue;
|
||||
worklist.insert(worklist.end(), reg.sites.begin(), reg.sites.end());
|
||||
|
||||
std::unordered_set<std::uint64_t> done;
|
||||
while (!worklist.empty()) {
|
||||
const auto blockIdx = worklist.back();
|
||||
worklist.pop_back();
|
||||
|
||||
for (auto frontier : ssaBlocks[blockIdx].df) {
|
||||
if (done.find(frontier) != done.end()) continue;
|
||||
done.insert(frontier);
|
||||
|
||||
auto& target = irBlocks[frontier];
|
||||
ir_instruction instr = { ir_instruction::Phi, ir_operand{ ir_operand::Register, i } };
|
||||
for (const auto& pred : cfgBlocks[frontier].preds)
|
||||
instr.sources.emplace_back(ir_operand::PhiPair, i, pred);
|
||||
target.instructions.emplace(target.instructions.begin(), std::move(instr));
|
||||
|
||||
if (reg.sites.find(frontier) == reg.sites.end()) worklist.push_back(frontier);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa::rename(std::vector<ir_basic_block>& irBlocks,
|
||||
std::size_t regCount,
|
||||
const std::vector<cfg_block>& cfgBlocks,
|
||||
std::vector<ssa_block>& ssaBlocks,
|
||||
const std::vector<std::uint64_t>& order) {
|
||||
std::vector<std::uint64_t> counters;
|
||||
std::vector<std::stack<std::uint64_t>> stacks;
|
||||
|
||||
counters.resize(regCount);
|
||||
stacks.resize(regCount);
|
||||
|
||||
for (auto it = order.begin() + 1; it != order.end(); ++it) {
|
||||
std::uint64_t parent = ssaBlocks[*it].idom;
|
||||
if (parent != std::numeric_limits<std::uint64_t>::max()) ssaBlocks[parent].children.emplace(*it);
|
||||
}
|
||||
|
||||
rename_rec(counters, stacks, irBlocks, cfgBlocks, ssaBlocks, order.front());
|
||||
}
|
||||
|
||||
void ssa::rename_rec(std::vector<std::uint64_t>& counters,
|
||||
std::vector<std::stack<std::uint64_t>>& stacks,
|
||||
std::vector<ir_basic_block>& irBlocks,
|
||||
const std::vector<cfg_block>& cfgBlocks,
|
||||
const std::vector<ssa_block>& ssaBlocks,
|
||||
std::size_t blockIdx) {
|
||||
std::unordered_map<std::uint64_t, std::size_t> pushed;
|
||||
|
||||
auto& block = irBlocks[blockIdx];
|
||||
for (auto& instr : block.instructions) {
|
||||
if (instr.type == ir_instruction::Phi) {
|
||||
auto reg = instr.destination->value.reg.name;
|
||||
stacks[reg].push(instr.destination->value.reg.ver = counters[reg]++);
|
||||
++pushed[reg];
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto& op : instr.sources) {
|
||||
if (op.type != ir_operand::Register) continue;
|
||||
auto reg = op.value.reg.name;
|
||||
op.value.reg.ver = stacks[reg].top();
|
||||
}
|
||||
|
||||
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
|
||||
auto reg = instr.destination->value.reg.name;
|
||||
stacks[reg].push(instr.destination->value.reg.ver = counters[reg]++);
|
||||
++pushed[reg];
|
||||
}
|
||||
|
||||
for (auto succIdx : cfgBlocks[blockIdx].sucs) {
|
||||
auto& succ = irBlocks[succIdx];
|
||||
for (auto& instr : succ.instructions) {
|
||||
if (instr.type != ir_instruction::Phi) break;
|
||||
for (auto& op : instr.sources) {
|
||||
if (op.value.phiPair.block != blockIdx) continue;
|
||||
op.value.phiPair.reg.ver = stacks[op.value.phiPair.reg.name].top();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (std::uint64_t child : ssaBlocks[blockIdx].children)
|
||||
rename_rec(counters, stacks, irBlocks, cfgBlocks, ssaBlocks, child);
|
||||
|
||||
for (auto [reg, count] : pushed)
|
||||
while ((count--) > 0)
|
||||
stacks[reg].pop();
|
||||
}
|
||||
|
||||
void ssa::rpo_dfs(std::unordered_set<std::size_t>& visited,
|
||||
std::vector<std::size_t>& order,
|
||||
std::size_t block,
|
||||
const std::vector<cfg_block>& blocks) {
|
||||
visited.insert(block);
|
||||
for (auto succ : blocks[block].sucs) {
|
||||
if (visited.find(succ) != visited.end()) continue;
|
||||
rpo_dfs(visited, order, succ, blocks);
|
||||
}
|
||||
order.push_back(block);
|
||||
}
|
||||
|
||||
std::size_t ssa::intersect(std::vector<ssa_block>& m_blocks, std::size_t b1, std::size_t b2) {
|
||||
while (b1 != b2) {
|
||||
while (m_blocks[b1].order > m_blocks[b2].order)
|
||||
b1 = m_blocks[b1].idom;
|
||||
while (m_blocks[b2].order > m_blocks[b1].order)
|
||||
b2 = m_blocks[b2].idom;
|
||||
}
|
||||
return b1;
|
||||
}
|
||||
|
||||
// // 5. Renaming
|
||||
// std::vector<std::uint64_t> counters;
|
||||
// std::vector<std::stack<std::uint64_t>> stacks;
|
||||
//
|
||||
// counters.resize(func.regCount);
|
||||
// stacks.resize(func.regCount);
|
||||
//
|
||||
// for (std::size_t i = 1; i < order.size(); ++i) {
|
||||
// std::size_t parent = blocks[order[i]].idom;
|
||||
// if (parent != std::numeric_limits<std::size_t>::max()) blocks[parent].children.emplace(order[i]);
|
||||
// }
|
||||
//
|
||||
// auto rename = [&counters, &stacks, &blocks, &func](auto& self, std::size_t blockIdx) -> void {
|
||||
// std::unordered_map<std::size_t, std::size_t> pushed;
|
||||
//
|
||||
// auto& block = func.blocks[blockIdx];
|
||||
// for (auto& instr : block.instructions) {
|
||||
// if (instr.type == ir_instruction::Phi) {
|
||||
// auto reg = instr.destination->value.reg.name;
|
||||
// auto idx = counters[reg]++;
|
||||
// instr.destination->value.reg.ver = idx;
|
||||
// stacks[reg].push(idx);
|
||||
// ++pushed[reg];
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// for (auto& op : instr.sources) {
|
||||
// if (op.type != ir_operand::Register) continue;
|
||||
// auto reg = op.value.reg.name;
|
||||
// op.value.reg.ver = stacks[reg].top();
|
||||
// }
|
||||
//
|
||||
// if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
|
||||
// auto reg = instr.destination->value.reg.name;
|
||||
// auto idx = counters[reg]++;
|
||||
// instr.destination->value.reg.ver = idx;
|
||||
// stacks[reg].push(idx);
|
||||
// ++pushed[reg];
|
||||
// }
|
||||
//
|
||||
// for (auto succIdx : blocks[blockIdx].sucs) {
|
||||
// auto& succ = func.blocks[succIdx];
|
||||
// for (auto& instr : succ.instructions) {
|
||||
// if (instr.type != ir_instruction::Phi) break;
|
||||
// for (auto& op : instr.sources) {
|
||||
// if (op.value.phiPair.block != blockIdx) continue;
|
||||
// op.value.phiPair.reg.ver = stacks[op.value.phiPair.reg.name].top();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// for (std::size_t child : blocks[blockIdx].children)
|
||||
// self(self, child);
|
||||
//
|
||||
// for (auto [reg, count] : pushed)
|
||||
// while (count--)
|
||||
// stacks[reg].pop();
|
||||
// };
|
||||
// rename(rename, order.front());
|
||||
// }
|
||||
|
||||
} // namespace furc
|
||||
Reference in New Issue
Block a user