set structure for IR

This commit is contained in:
2024-08-21 04:10:36 +00:00
parent 03e33d0a0f
commit ed1ba4b59a
17 changed files with 1423 additions and 66 deletions

View File

@ -1,6 +1,8 @@
#pragma once
#include <stdexcept>
#include <unordered_map>
#include <variant>
#include <vector>
enum class ASTNodeType {
// Expression nodes
NewArrayExpr,
@ -98,4 +100,38 @@ inline bool operator==(const ExprTypeInfo &l, const ExprTypeInfo &r) {
}
throw std::runtime_error("something strange happened");
}
inline bool operator!=(const ExprTypeInfo &l, const ExprTypeInfo &r) { return !(l == r); }
inline bool operator!=(const ExprTypeInfo &l, const ExprTypeInfo &r) { return !(l == r); }
class IRClassInfo {
public:
std::string class_name; // This data must be provided by user
std::vector<size_t> member_var_size; // This data must be provided by user. Each of them is the size of a member
// variable, which must be in [1,4]
std::unordered_map<std::string, size_t> member_var_offset; // This data must be provided by user
std::vector<size_t> member_var_pos_after_align;
size_t class_size_after_align;
void ArrangeSpace() {
size_t cur_pos = 0;
size_t align_size = 1;
for (size_t cur_size : member_var_size) {
if (cur_size != 1 && cur_size != 4) throw std::runtime_error("Invalid member variable size");
if (cur_pos % cur_size == 0) {
member_var_pos_after_align.push_back(cur_pos);
cur_pos += cur_size;
} else {
cur_pos += cur_size - (cur_pos % cur_size);
member_var_pos_after_align.push_back(cur_pos);
cur_pos += cur_size;
}
if (cur_size > align_size) align_size = cur_size;
if (cur_pos % align_size != 0) cur_pos += align_size - (cur_pos % align_size);
class_size_after_align = cur_pos;
}
}
};
class IRVariableInfo {
public:
enum class VariableType { global_variable, local_variable, member_variable };
std::string class_name;
std::string variable_name;
};