basically write codegen

This commit is contained in:
2024-08-28 14:21:05 +00:00
parent 46f659053b
commit f93e9f481c
8 changed files with 392 additions and 52 deletions

View File

@ -6,21 +6,8 @@
#include "tools.h"
namespace NaiveBackend {
inline size_t CalcSize(const LLVMType &tp) {
if (std::holds_alternative<LLVMIRIntType>(tp)) {
auto &int_tp = std::get<LLVMIRIntType>(tp);
return (int_tp.bits + 7) / 8;
} else if (std::holds_alternative<LLVMIRPTRType>(tp)) {
return 4;
} else if (std::holds_alternative<LLVMVOIDType>(tp)) {
throw std::runtime_error("Cannot calculate size of void type");
return 0;
} else if (std::holds_alternative<LLVMIRCLASSTYPE>(tp)) {
throw std::runtime_error("Cannot calculate size of class type");
} else
throw std::runtime_error("Unknown type");
}
inline void ScanForVar(FuncLayout &layout, std::shared_ptr<ActionItem> action) {
inline void ScanForVar(FuncLayout &layout, std::shared_ptr<ActionItem> action,
const std::unordered_map<std::string, IRClassInfo> &low_level_class_info) {
if (std::dynamic_pointer_cast<JMPActionItem>(action)) {
throw std::runtime_error("JMPActionItem should not be in the layout");
} else if (auto binary_act = std::dynamic_pointer_cast<BinaryOperationAction>(action)) {
@ -44,7 +31,19 @@ inline void ScanForVar(FuncLayout &layout, std::shared_ptr<ActionItem> action) {
if (get_element_act->result_full == "") {
throw std::runtime_error("GetElementPtrAction should have a result_full");
}
layout.AllocateItem(get_element_act->result_full, CalcSize(get_element_act->ty));
if (get_element_act->indices.size() == 1) {
layout.AllocateItem(get_element_act->result_full, CalcSize(get_element_act->ty));
} else if (get_element_act->indices.size() == 2) {
if (get_element_act->indices[0] != "0")
throw std::runtime_error("GetElementPtrAction with non-zero base index is not supported");
size_t element_idx = std::stoi(get_element_act->indices[1]);
auto class_ty = std::get<LLVMIRCLASSTYPE>(get_element_act->ty);
const IRClassInfo &class_info = low_level_class_info.at(class_ty.class_name_full);
size_t sz = class_info.member_var_size[element_idx];
layout.AllocateItem(get_element_act->result_full, sz);
} else {
throw std::runtime_error("GetElementPtrAction with more than 2 indices is not supported");
}
} else if (auto icmp_act = std::dynamic_pointer_cast<ICMPAction>(action)) {
if (icmp_act->result_full == "") {
throw std::runtime_error("ICMPAction should have a result_full");