/
vit1251
/
cmm
Обзор
Документация
Войти
/
vit1251
/
cmm
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/codegen.h
88 строк
3 KB
Vitold S
Commits added a large set of example and demo programs,
26 июл 2026, 14:31
26 июл 2026, 14:31
8e0776e
Код
Авторство
О чём код?
#pragma once #include "ast.h" #include <string> #include <memory> #include <unordered_map> #include <unordered_set> #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Type.h" #include "llvm/IR/Value.h" struct StructInfo { llvm::StructType* type = nullptr; std::vector<std::string> field_names; std::vector<std::string> field_type_names; std::unordered_map<std::string, unsigned> field_index; std::vector<std::string> type_params; // generic params, empty for concrete StructDecl* decl = nullptr; // original decl for monomorphization }; class Codegen { public: Codegen(); ~Codegen(); void generate(Program& prog, const std::string& module_name = "cmm_module"); void resolve_imports(Program& prog, const std::string& main_source_dir); void printIR(const std::string& path); void emitObject(const std::string& path); // Register struct/fn from .o import bool import_struct(const StructDecl& sd); void import_fn(const std::string& fn_name, llvm::Type* ret_type, std::vector<llvm::Type*> param_types); llvm::Type* resolve_type(const std::string& name); llvm::Type* monomorphize_struct(const std::string& base, const std::string& full_name); const std::unordered_map<std::string, StructInfo>& structs() const { return structs_; } llvm::Module* module() { return mod_.get(); } llvm::LLVMContext& context() { return *ctx_; } private: void register_struct(const StructDecl& sd); void codegen_fn_decl(FnDecl& fn); llvm::Value* codegen_block(Block& block, llvm::Type* expected_type); llvm::Value* codegen_stmt(Stmt& stmt); llvm::Value* codegen_expr(Expr& expr, llvm::Type* expected); llvm::Value* codegen_string_literal(const std::string& val); llvm::AllocaInst* create_entry_alloca(llvm::Type* ty, const std::string& name); void generate_start(); // Monomorphize: generate concrete methods for a generic instantiation void monomorphize_methods(const std::string& base, const std::string& full_name, const std::vector<std::string>& type_args); llvm::LLVMContext& ctx(); llvm::IRBuilder<>& builder() { return *builder_; } std::unique_ptr<llvm::LLVMContext> ctx_; std::unique_ptr<llvm::Module> mod_; std::unique_ptr<llvm::IRBuilder<>> builder_; llvm::Function* current_func_ = nullptr; // Per-function state struct FnState { std::unordered_map<std::string, llvm::AllocaInst*> locals; std::unordered_map<std::string, std::string> local_types; // name → type name std::unordered_set<std::string> dropped; // variables after drop() std::vector<std::vector<std::unique_ptr<Expr>>> defer_stack; }; FnState fn_; // Struct info std::unordered_map<std::string, StructInfo> structs_; // Generic impl methods: base_type_name → [template FnDecls] std::unordered_map<std::string, std::vector<FnDecl>> generic_methods_; // Name resolution: alias → original_name for filtered imports std::unordered_map<std::string, std::string> use_aliases_; std::unordered_set<std::string> use_allowed_; // names allowed by filtered imports std::unordered_map<std::string, std::string> fn_source_module_; // function_name → source file };