Commit 243899ee authored by Bednárek David RNDr. Ph.D.'s avatar Bednárek David RNDr. Ph.D.
Browse files

framework support for nested blocks and local struct/enum

parent 43b16f73
......@@ -47,14 +47,14 @@ namespace cecko {
defined_ = true;
}
void CKStructType::dump(CIOStream& os) const
void CKStructType::dump(CIOStream& os, const std::string& indent) const
{
if (defined_)
{
os << "struct " << get_name() << "{" << CIEndl;
os << indent << "struct " << get_name() << "{" << CIEndl;
for (auto&& a : elements_ordered_)
os << "\t" << a->get_type_pack().type->declaration(a->get_type_pack().is_const, a->get_name()) << ";" << CIEndl;
os << "};" << CIEndl;
os << indent << "\t" << a->get_type_pack().type->declaration(a->get_type_pack().is_const, a->get_name()) << ";" << CIEndl;
os << indent << "};" << CIEndl;
}
}
......@@ -67,22 +67,22 @@ namespace cecko {
defined_ = true;
}
void CKEnumType::dump(CIOStream& os) const
void CKEnumType::dump(CIOStream& os, const std::string& indent) const
{
if (defined_)
{
os << "enum " << get_name();
os << indent << "enum " << get_name();
std::string delim = "{";
for (auto&& a : elements_ordered_)
{
os << delim << CIEndl;
os << "\t" << a->declaration();
os << indent << "\t" << a->declaration();
delim = ",";
}
if ( delim == "{" )
os << delim;
else
os << CIEndl;
os << CIEndl << indent;
os << "};" << CIEndl;
}
}
......@@ -149,14 +149,6 @@ namespace cecko {
arrts_.for_each(dlambda);
fncts_.for_each(dlambda);
*/
os << "// --- ENUMS ---" << std::endl;
enmts_.for_each([&os](auto&& a) {
a->dump(os);
});
os << "// --- STRUCTS ---" << std::endl;
strts_.for_each([&os](auto&& a) {
a->dump(os);
});
}
std::string CKConstant::declaration() const
......@@ -164,11 +156,16 @@ namespace cecko {
return get_name() + "=" + std::to_string(value_->getValue().getZExtValue());
}
CKTypedefConstObs CKGlobalTable::declare_typedef(const std::string& name, const CKTypeRefPack& type_pack)
CKLocalTableObs CKAbstractScope::get_local()
{
return nullptr;
}
CKTypedefConstObs CKUniversalTable::declare_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
return typedefs_.try_emplace(name, type_pack);
}
CKConstantConstObs CKGlobalTable::declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value)
CKConstantConstObs CKUniversalTable::declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value)
{
return constants_.try_emplace(name, type, value);
}
......@@ -202,14 +199,21 @@ namespace cecko {
auto p = fncs_.find(n);
return p;
}
CKTypedefConstObs CKUniversalTable::find_typedef_here(const CIName& n) const
{
return typedefs_.find(n);
}
CKTypedefConstObs CKGlobalTable::find_typedef(const CIName& n) const
{
auto p = typedefs_.find(n);
return p;
return find_typedef_here(n);
}
CKNamedObs CKUniversalTable::find_constant_here(const CIName& n)
{
return constants_.find(n);
}
CKNamedObs CKGlobalTable::find(const CIName& n)
{
auto pc = constants_.find(n);
auto pc = find_constant_here(n);
if (pc)
return pc;
auto q = vars_.find(n);
......@@ -222,10 +226,7 @@ namespace cecko {
void CKGlobalTable::dump(CIOStream& os) const
{
os << "// --- GLOBAL TYPEDEFS ---" << std::endl;
auto typedeflambda = [&os](auto&& a) {
os << "typedef " << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
};
typedefs_.for_each(typedeflambda);
dump_universal(os, "");
os << "// --- FUNCTION DECLARATIONS ---" << std::endl;
auto decllambda = [&os](auto&& a) {
os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
......@@ -248,7 +249,6 @@ namespace cecko {
void CKLocalTable::varsFromArgs(CKIRBuilderRef builder, CKFunctionObs f, const CKFunctionFormalPackArray& formal_packs)
{
function_ = f;
auto f_type = f->get_function_type();
auto f_ir = f->get_function_ir();
auto n = f_type->get_function_arg_count();
......@@ -268,14 +268,6 @@ namespace cecko {
}
}
CKTypedefConstObs CKLocalTable::declare_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
return typedefs_.try_emplace(name, type_pack);
}
CKConstantConstObs CKLocalTable::declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value)
{
return constants_.try_emplace(name, type, value);
}
CKLocalVarObs CKLocalTable::varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack)
{
auto var = builder.CreateAlloca(type_pack.type->get_ir(), nullptr, name);
......@@ -284,14 +276,14 @@ namespace cecko {
CKTypedefConstObs CKLocalTable::find_typedef(const CIName& n) const
{
auto p = typedefs_.find(n);
auto p = find_typedef_here(n);
if (p)
return p;
return parent_scope_->find_typedef(n);
}
CKNamedObs CKLocalTable::find(const CIName& n)
{
auto pc = constants_.find(n);
auto pc = find_constant_here(n);
if (pc)
return pc;
auto pv = vars_.find(n);
......@@ -299,19 +291,55 @@ namespace cecko {
return pv;
return parent_scope_->find(n);
}
void CKLocalTable::dump(CIOStream& os) const
CKLocalTableObs CKLocalTable::get_local()
{
auto typedeflambda = [&os](auto&& a) {
os << "typedef " << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
return this;
}
CKLocalTableObs CKLocalTable::create_block()
{
auto b = std::make_unique< CKLocalTable>( this);
auto bo = &*b;
block_scopes_.push_back(std::move(b));
return bo;
}
CKLocalTableObs CKLocalTable::parent_block() const
{
return parent_scope_->get_local();
}
void CKUniversalTable::dump_universal(CIOStream& os, const std::string& indent) const
{
//os << "// --- ENUMS ---" << std::endl;
enmts_.for_each([&os, &indent](auto&& a) {
a->dump(os, indent);
});
//os << "// --- STRUCTS ---" << std::endl;
strts_.for_each([&os, &indent](auto&& a) {
a->dump(os, indent);
});
auto typedeflambda = [&os, &indent](auto&& a) {
os << indent << "typedef " << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
};
typedefs_.for_each(typedeflambda);
auto dlambda = [&os](auto&& a) {
}
void CKLocalTable::dump(CIOStream& os, const std::string& indent) const
{
dump_universal(os, indent);
auto dlambda = [&os, &indent](auto&& a) {
if (!a->is_arg())
{
a->dump(os);
a->dump(os, indent);
}
};
vars_.for_each(dlambda);
auto indent2 = indent + "\t";
for (auto&& a : block_scopes_)
{
os << indent << "{" << std::endl;
a->dump(os, indent2);
os << indent << "}" << std::endl;
}
}
CKLocalTableObs CKFunction::define(CKAbstractScopeObs parent, CKIRBuilderRef builder, CKFunctionFormalPackArray formal_packs)
......@@ -337,10 +365,12 @@ namespace cecko {
args += ",";
args += arg_type->declaration(arg_pack.is_const, !!arg_pack.name ? *arg_pack.name : std::string{});
}
if (args.empty())
args += "void";
}
os << f_type->get_function_return_type()->declaration(false, get_name() + "(" + args + ")") << "{" << CIEndl;
loctab_->dump(os);
loctab_->dump(os, "\t");
os << "}" << CIEndl;
}
......@@ -350,9 +380,9 @@ namespace cecko {
os << "\ttypedef " << get_type_pack().type->declaration(get_type_pack().is_const, get_name()) << ";" << CIEndl;
}
void CKVar::dump(CIOStream& os) const
void CKVar::dump(CIOStream& os, const std::string& indent) const
{
os << "\t" << get_type_pack().type->declaration(get_type_pack().is_const, get_name()) << ";" << CIEndl;
os << indent << get_type_pack().type->declaration(get_type_pack().is_const, get_name()) << ";" << CIEndl;
}
// CONTEXT
......@@ -409,6 +439,63 @@ namespace cecko {
builder_.ClearInsertionPoint();
}
void CKContext::enter_block()
{
assert(loctable_);
loctable_ = loctable_->create_block();
}
void CKContext::exit_block()
{
assert(loctable_);
loctable_ = loctable_->parent_block();
assert(loctable_);
}
CKStructTypeObs CKContext::declare_struct_type(const CIName& n)
{
if (!!loctable_)
{
return loctable_->declare_struct_type(n, module_->getContext());
}
else
{
return globtable_->declare_struct_type(n, module_->getContext());
}
}
CKStructTypeObs CKContext::find_struct_type(const CIName& n)
{
if (!!loctable_)
{
return loctable_->find_struct_type(n);
}
else
{
return globtable_->find_struct_type(n);
}
}
CKEnumTypeObs CKContext::declare_enum_type(const CIName& n)
{
if (!!loctable_)
{
return loctable_->declare_enum_type(n, get_int_type());
}
else
{
return globtable_->declare_enum_type(n, get_int_type());
}
}
CKEnumTypeObs CKContext::find_enum_type(const CIName& n)
{
if (!!loctable_)
{
return loctable_->find_enum_type(n);
}
else
{
return globtable_->find_enum_type(n);
}
}
CKTypedefConstObs CKContext::define_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
if (!!loctable_)
......@@ -488,7 +575,7 @@ namespace cecko {
auto t_ptr_char = typetable_.get_pointer_type({ t_char, false });
auto t_cptr_char = typetable_.get_pointer_type({ t_char, true });
auto t_file_s = typetable_.declare_struct_type("_file_s", module_->getContext());
auto t_file_s = globtable_.declare_struct_type("_file_s", module_->getContext());
globtable_.declare_typedef("FILE", { t_file_s, false });
......
......@@ -384,7 +384,7 @@ namespace cecko {
virtual CITypeMangle mangle() const { return "S" + get_name() + '$'; }
virtual CIDecl declaration(bool is_const, const CIDecl& dtor) const;
void dump(CIOStream& os) const;
void dump(CIOStream& os, const std::string& indent) const;
private:
using element_storage_type = CINamedStorage< CKStructElement>;
bool defined_;
......@@ -413,7 +413,7 @@ namespace cecko {
virtual CITypeMangle mangle() const { return "E" + get_name() + '$'; }
virtual CIDecl declaration(bool is_const, const CIDecl& dtor) const;
virtual CKIRTypeObs get_ir() const { return base_type_->get_ir(); }
void dump(CIOStream& os) const;
void dump(CIOStream& os, const std::string& indent) const;
private:
bool defined_;
CKTypeObs base_type_;
......@@ -467,17 +467,6 @@ namespace cecko {
CKPtrTypeObs get_pointer_type(const CKTypeRefPack& pack) { return ptrts_.emplace(pack); }
CKArrayTypeObs get_array_type(CKTypeObs element_type, CKIRConstantIntObs size) { return arrts_.emplace(element_type, size); }
CKFunctionTypeObs get_function_type(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic = false) { return fncts_.emplace(ret_type, std::move(arg_types), variadic); }
CKStructTypeObs declare_struct_type(const CIName& n, CKIRContextRef Context)
{
return strts_.try_emplace(n, Context, n);
}
CKStructTypeObs find_struct_type(const CIName& n) { return strts_.find(n); }
CKEnumTypeObs declare_enum_type(const CIName& n, CKTypeObs base_type)
{
return enmts_.try_emplace(n, base_type);
}
CKEnumTypeObs find_enum_type(const CIName& n) { return enmts_.find(n); }
void dump(CIOStream& os) const;
private:
CKVoidType voit_;
......@@ -487,8 +476,6 @@ namespace cecko {
CIHashedStorage< CKPtrType> ptrts_;
CIHashedStorage< CKArrayType> arrts_;
CIHashedStorage< CKFunctionType> fncts_;
CINamedStorage< CKStructType> strts_;
CINamedStorage< CKEnumType> enmts_;
};
class CKAbstractNamed : public CINamePtr, CIImmovable {
......@@ -548,7 +535,7 @@ namespace cecko {
virtual CKTypeObs get_type() const { return type_pack_.type; }
virtual bool is_const() const { return type_pack_.is_const; }
const CKTypeRefPack& get_type_pack() const { return type_pack_; }
void dump(CIOStream& os) const;
void dump(CIOStream& os, const std::string& indent) const;
private:
CKTypeRefPack type_pack_;
};
......@@ -588,6 +575,7 @@ namespace cecko {
virtual ~CKAbstractScope() {}
virtual CKTypedefConstObs find_typedef(const CIName& n) const = 0;
virtual CKNamedObs find(const CIName& n) = 0;
virtual CKLocalTableObs get_local();
};
using CKAbstractScopeObs = CKAbstractScope*;
......@@ -615,15 +603,37 @@ namespace cecko {
CKLocalTableOwner loctab_;
};
class CKUniversalTable : public CKAbstractScope {
public:
CKStructTypeObs declare_struct_type(const CIName& n, CKIRContextRef Context)
{
return strts_.try_emplace(n, Context, n);
}
CKStructTypeObs find_struct_type(const CIName& n) { return strts_.find(n); }
CKEnumTypeObs declare_enum_type(const CIName& n, CKTypeObs base_type)
{
return enmts_.try_emplace(n, base_type);
}
CKEnumTypeObs find_enum_type(const CIName& n) { return enmts_.find(n); }
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKConstantConstObs declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
CKTypedefConstObs find_typedef_here(const CIName& n) const;
CKNamedObs find_constant_here(const CIName& n);
void dump_universal(CIOStream& os, const std::string& indent) const;
private:
CINamedStorage< CKStructType> strts_;
CINamedStorage< CKEnumType> enmts_;
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKConstant> constants_;
};
using CKFunctionObs = CKFunction*;
using CKFunctionConstObs = const CKFunction*;
class CKGlobalTable : public CKAbstractScope {
class CKGlobalTable : public CKUniversalTable {
public:
CKGlobalTable()
{}
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKConstantConstObs declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
CKGlobalVarObs varDefine(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack);
CKGlobalVarObs declare_extern_variable(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack);
CKFunctionObs declare_function(const CIName& n, CKIRModuleObs M, CKFunctionTypeObs type);
......@@ -635,8 +645,6 @@ namespace cecko {
void dump(CIOStream& os) const;
private:
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKConstant> constants_;
CINamedStorage< CKFunction> fncs_;
CINamedStorage< CKGlobalVar> vars_;
};
......@@ -656,29 +664,30 @@ namespace cecko {
using CKLocalVarObs = CKLocalVar*;
class CKLocalTable : public CKAbstractScope {
class CKLocalTable : public CKUniversalTable {
public:
CKLocalTable(CKAbstractScopeObs parent)
: parent_scope_(parent), function_(nullptr)
: parent_scope_(parent)
{}
void varsFromArgs(CKIRBuilderRef builder, CKFunctionObs f, const CKFunctionFormalPackArray& formal_packs);
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKConstantConstObs declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
CKLocalVarObs varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack);
virtual CKTypedefConstObs find_typedef(const CIName& n) const;
virtual CKNamedObs find(const CIName& n);
void dump(CIOStream& os) const;
void dump(CIOStream& os, const std::string & indent) const;
virtual CKLocalTableObs get_local();
CKLocalTableObs create_block();
CKLocalTableObs parent_block() const;
private:
CKAbstractScopeObs parent_scope_;
CKFunctionObs function_;
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKConstant> constants_;
CINamedStorage< CKLocalVar> vars_;
std::vector< CKLocalTableOwner> block_scopes_;
};
// CONTEXT
......@@ -739,6 +748,8 @@ namespace cecko {
void enter_function(CKFunctionObs f, CKFunctionFormalPackArray pack);
void exit_function();
void enter_block();
void exit_block();
CKVoidTypeObs get_void_type() const { return typetable_->get_void_type(); }
CKBoolTypeObs get_bool_type() const { return typetable_->get_bool_type(); }
......@@ -747,10 +758,10 @@ namespace cecko {
CKPtrTypeObs get_pointer_type(const CKTypeRefPack& pack) { return typetable_->get_pointer_type(pack); }
CKArrayTypeObs get_array_type(CKTypeObs element_type, CKIRConstantIntObs size) { return typetable_->get_array_type(element_type, size); }
CKFunctionTypeObs get_function_type(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic = false) { return typetable_->get_function_type(ret_type, std::move(arg_types), variadic); }
CKStructTypeObs declare_struct_type(const CIName& n) { return typetable_->declare_struct_type(n, module_->getContext()); }
CKStructTypeObs find_struct_type(const CIName& n) { return typetable_->find_struct_type(n); }
CKEnumTypeObs declare_enum_type(const CIName& n) { return typetable_->declare_enum_type(n, get_int_type()); }
CKEnumTypeObs find_enum_type(const CIName& n) { return typetable_->find_enum_type(n); }
CKStructTypeObs declare_struct_type(const CIName& n);
CKStructTypeObs find_struct_type(const CIName& n);
CKEnumTypeObs declare_enum_type(const CIName& n);
CKEnumTypeObs find_enum_type(const CIName& n);
CKVarObs define_var(const std::string& name, const CKTypeRefPack& type_pack);
CKTypedefConstObs define_typedef(const std::string& name, const CKTypeRefPack& type_pack);
CKConstantConstObs define_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment