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

fmwk support for typedef, builtin FILE

parent c0e34fde
...@@ -49,10 +49,13 @@ namespace cecko { ...@@ -49,10 +49,13 @@ namespace cecko {
void CKStructType::dump(CIOStream& os) const void CKStructType::dump(CIOStream& os) const
{ {
os << "struct " << get_name() << "{" << CIEndl; if (defined_)
for (auto&& a : elements_ordered_) {
os << "\t" << a->get_type_pack().type->declaration(a->get_type_pack().is_const, a->get_name()) << ";" << CIEndl; os << "struct " << get_name() << "{" << CIEndl;
os << "};" << 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;
}
} }
CKFunctionType::CKFunctionType(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic) CKFunctionType::CKFunctionType(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic)
...@@ -117,11 +120,16 @@ namespace cecko { ...@@ -117,11 +120,16 @@ namespace cecko {
arrts_.for_each(dlambda); arrts_.for_each(dlambda);
fncts_.for_each(dlambda); fncts_.for_each(dlambda);
*/ */
os << "// --- STRUCTS ---" << std::endl;
strts_.for_each([&os](auto&& a) { strts_.for_each([&os](auto&& a) {
a->dump(os); a->dump(os);
}); });
} }
CKTypedefConstObs CKGlobalTable::declare_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
return typedefs_.try_emplace(name, type_pack);
}
CKGlobalVarObs CKGlobalTable::varDefine(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack) CKGlobalVarObs CKGlobalTable::varDefine(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack)
{ {
auto irtp = type_pack.type->get_ir(); auto irtp = type_pack.type->get_ir();
...@@ -152,6 +160,11 @@ namespace cecko { ...@@ -152,6 +160,11 @@ namespace cecko {
auto p = fncs_.find(n); auto p = fncs_.find(n);
return p; return p;
} }
CKTypedefConstObs CKGlobalTable::find_typedef(const CIName& n) const
{
auto p = typedefs_.find(n);
return p;
}
CKNamedObs CKGlobalTable::find(const CIName& n) CKNamedObs CKGlobalTable::find(const CIName& n)
{ {
auto q = vars_.find(n); auto q = vars_.find(n);
...@@ -163,14 +176,22 @@ namespace cecko { ...@@ -163,14 +176,22 @@ namespace cecko {
void CKGlobalTable::dump(CIOStream& os) const 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);
os << "// --- FUNCTION DECLARATIONS ---" << std::endl;
auto decllambda = [&os](auto&& a) { auto decllambda = [&os](auto&& a) {
os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl; os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
}; };
fncs_.for_each(decllambda); fncs_.for_each(decllambda);
os << "// --- GLOBAL VARIABLES ---" << std::endl;
auto varlambda = [&os](auto&& a) { auto varlambda = [&os](auto&& a) {
os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl; os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
}; };
vars_.for_each(varlambda); vars_.for_each(varlambda);
os << "// --- FUNCTION DEFINITIONS ---" << std::endl;
auto deflambda = [&os](auto&& a) { auto deflambda = [&os](auto&& a) {
if (a->is_defined()) if (a->is_defined())
{ {
...@@ -202,12 +223,23 @@ namespace cecko { ...@@ -202,12 +223,23 @@ namespace cecko {
} }
} }
CKTypedefConstObs CKLocalTable::declare_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
return typedefs_.try_emplace(name, type_pack);
}
CKLocalVarObs CKLocalTable::varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack) CKLocalVarObs CKLocalTable::varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack)
{ {
auto var = builder.CreateAlloca(type_pack.type->get_ir(), nullptr, name); auto var = builder.CreateAlloca(type_pack.type->get_ir(), nullptr, name);
return vars_.try_emplace(name, type_pack, var, false); return vars_.try_emplace(name, type_pack, var, false);
} }
CKTypedefConstObs CKLocalTable::find_typedef(const CIName& n) const
{
auto p = typedefs_.find(n);
if (p)
return p;
return parent_scope_->find_typedef(n);
}
CKNamedObs CKLocalTable::find(const CIName& n) CKNamedObs CKLocalTable::find(const CIName& n)
{ {
auto p = vars_.find(n); auto p = vars_.find(n);
...@@ -217,6 +249,10 @@ namespace cecko { ...@@ -217,6 +249,10 @@ namespace cecko {
} }
void CKLocalTable::dump(CIOStream& os) const void CKLocalTable::dump(CIOStream& os) const
{ {
auto typedeflambda = [&os](auto&& a) {
os << "typedef " << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
};
typedefs_.for_each(typedeflambda);
auto dlambda = [&os](auto&& a) { auto dlambda = [&os](auto&& a) {
if (!a->is_arg()) if (!a->is_arg())
{ {
...@@ -257,6 +293,11 @@ namespace cecko { ...@@ -257,6 +293,11 @@ namespace cecko {
os << "}" << CIEndl; os << "}" << CIEndl;
} }
void CKTypedef::dump(CIOStream& os) const
{
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
{ {
os << "\t" << get_type_pack().type->declaration(get_type_pack().is_const, get_name()) << ";" << CIEndl; os << "\t" << get_type_pack().type->declaration(get_type_pack().is_const, get_name()) << ";" << CIEndl;
...@@ -316,6 +357,17 @@ namespace cecko { ...@@ -316,6 +357,17 @@ namespace cecko {
builder_.ClearInsertionPoint(); builder_.ClearInsertionPoint();
} }
CKTypedefConstObs CKContext::define_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
if (!!loctable_)
{
return loctable_->declare_typedef(name, type_pack);
}
else
{
return globtable_->declare_typedef(name, type_pack);
}
}
CKVarObs CKContext::define_var(const std::string& name, const CKTypeRefPack& type_pack) CKVarObs CKContext::define_var(const std::string& name, const CKTypeRefPack& type_pack)
{ {
if (!!loctable_) if (!!loctable_)
...@@ -339,6 +391,30 @@ namespace cecko { ...@@ -339,6 +391,30 @@ namespace cecko {
} }
} }
CKTypedefConstObs CKContext::find_typedef(const CIName& n) const
{
if (!!loctable_)
{
return loctable_->find_typedef(n);
}
else
{
return globtable_->find_typedef(n);
}
}
bool CKContext::is_typedef(const CIName& n) const
{
if (!!loctable_)
{
return !! loctable_->find_typedef(n);
}
else
{
return !! globtable_->find_typedef(n);
}
}
void CKTables::declare_library() void CKTables::declare_library()
{ {
auto t_void = typetable_.get_void_type(); auto t_void = typetable_.get_void_type();
...@@ -349,6 +425,10 @@ namespace cecko { ...@@ -349,6 +425,10 @@ namespace cecko {
auto t_ptr_char = typetable_.get_pointer_type({ t_char, false }); auto t_ptr_char = typetable_.get_pointer_type({ t_char, false });
auto t_cptr_char = typetable_.get_pointer_type({ t_char, true }); auto t_cptr_char = typetable_.get_pointer_type({ t_char, true });
auto t_file_s = typetable_.declare_struct_type("_file_s", module_->getContext());
globtable_.declare_typedef("FILE", { t_file_s, false });
globtable_.declare_function("printf", module_, typetable_.get_function_type(t_int, { t_cptr_char }, true), "ckrt_printf"); globtable_.declare_function("printf", module_, typetable_.get_function_type(t_int, { t_cptr_char }, true), "ckrt_printf");
globtable_.declare_function("scanf", module_, typetable_.get_function_type(t_int, { t_cptr_char }, true), "ckrt_scanf"); globtable_.declare_function("scanf", module_, typetable_.get_function_type(t_int, { t_cptr_char }, true), "ckrt_scanf");
globtable_.declare_function("sprintf", module_, typetable_.get_function_type(t_int, { t_ptr_char, t_cptr_char }, true), "ckrt_sprintf"); globtable_.declare_function("sprintf", module_, typetable_.get_function_type(t_int, { t_ptr_char, t_cptr_char }, true), "ckrt_sprintf");
......
...@@ -459,17 +459,35 @@ namespace cecko { ...@@ -459,17 +459,35 @@ namespace cecko {
class CKAbstractNamed : public CINamePtr, CIImmovable { class CKAbstractNamed : public CINamePtr, CIImmovable {
public: public:
virtual ~CKAbstractNamed() {} virtual ~CKAbstractNamed() {}
virtual bool is_typedef() const { return false; }
virtual bool is_var() const { return false; } virtual bool is_var() const { return false; }
virtual bool is_function() const { return false; } virtual bool is_function() const { return false; }
virtual bool is_const() const { return false; } virtual bool is_const() const { return false; }
virtual CKTypeObs get_type() const = 0; virtual CKTypeObs get_type() const = 0;
virtual CKIRValueObs get_ir() const = 0; virtual CKIRValueObs get_ir() const { return nullptr; };
virtual CKIRFunctionObs get_function_ir() const { return nullptr; }; virtual CKIRFunctionObs get_function_ir() const { return nullptr; };
virtual void dump(CIOStream& os) const = 0; virtual void dump(CIOStream& os) const = 0;
}; };
using CKNamedObs = CKAbstractNamed*; using CKNamedObs = CKAbstractNamed*;
class CKTypedef : public CKAbstractNamed {
public:
CKTypedef(CKTypeRefPack type_pack)
: type_pack_(type_pack)
{}
virtual bool is_typedef() const { return true; }
virtual bool is_var() const { return false; }
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_; }
virtual void dump(CIOStream& os) const;
private:
CKTypeRefPack type_pack_;
};
using CKTypedefConstObs = const CKTypedef*;
class CKVar : public CKAbstractNamed { class CKVar : public CKAbstractNamed {
public: public:
CKVar(CKTypeRefPack type_pack) CKVar(CKTypeRefPack type_pack)
...@@ -517,6 +535,7 @@ namespace cecko { ...@@ -517,6 +535,7 @@ namespace cecko {
class CKAbstractScope : CIImmovable { class CKAbstractScope : CIImmovable {
public: public:
virtual ~CKAbstractScope() {} virtual ~CKAbstractScope() {}
virtual CKTypedefConstObs find_typedef(const CIName& n) const = 0;
virtual CKNamedObs find(const CIName& n) = 0; virtual CKNamedObs find(const CIName& n) = 0;
}; };
...@@ -552,16 +571,19 @@ namespace cecko { ...@@ -552,16 +571,19 @@ namespace cecko {
public: public:
CKGlobalTable() CKGlobalTable()
{} {}
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKGlobalVarObs varDefine(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack); 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); CKGlobalVarObs declare_extern_variable(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack);
CKFunctionObs declare_function(const CIName& n, CKIRModuleObs M, CKFunctionTypeObs type); CKFunctionObs declare_function(const CIName& n, CKIRModuleObs M, CKFunctionTypeObs type);
CKFunctionObs declare_function(const CIName& n, CKIRModuleObs M, CKFunctionTypeObs type, const std::string& irname); CKFunctionObs declare_function(const CIName& n, CKIRModuleObs M, CKFunctionTypeObs type, const std::string& irname);
CKFunctionObs find_function(const CIName& n); CKFunctionObs find_function(const CIName& n);
CKFunctionConstObs find_function(const CIName& n) const; CKFunctionConstObs find_function(const CIName& n) const;
virtual CKTypedefConstObs find_typedef(const CIName& n) const;
virtual CKNamedObs find(const CIName& n); virtual CKNamedObs find(const CIName& n);
void dump(CIOStream& os) const; void dump(CIOStream& os) const;
private: private:
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKFunction> fncs_; CINamedStorage< CKFunction> fncs_;
CINamedStorage< CKGlobalVar> vars_; CINamedStorage< CKGlobalVar> vars_;
}; };
...@@ -589,8 +611,10 @@ namespace cecko { ...@@ -589,8 +611,10 @@ namespace cecko {
void varsFromArgs(CKIRBuilderRef builder, CKFunctionObs f, const CKFunctionFormalPackArray& formal_packs); void varsFromArgs(CKIRBuilderRef builder, CKFunctionObs f, const CKFunctionFormalPackArray& formal_packs);
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKLocalVarObs varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack); 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); virtual CKNamedObs find(const CIName& n);
void dump(CIOStream& os) const; void dump(CIOStream& os) const;
...@@ -598,6 +622,7 @@ namespace cecko { ...@@ -598,6 +622,7 @@ namespace cecko {
private: private:
CKAbstractScopeObs parent_scope_; CKAbstractScopeObs parent_scope_;
CKFunctionObs function_; CKFunctionObs function_;
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKLocalVar> vars_; CINamedStorage< CKLocalVar> vars_;
}; };
...@@ -670,11 +695,15 @@ namespace cecko { ...@@ -670,11 +695,15 @@ namespace cecko {
CKStructTypeObs declare_struct_type(const CIName& n) { return typetable_->declare_struct_type(n, module_->getContext()); } 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); } CKStructTypeObs find_struct_type(const CIName& n) { return typetable_->find_struct_type(n); }
CKVarObs define_var(const std::string& name, const CKTypeRefPack& type_pack); CKVarObs define_var(const std::string& name, const CKTypeRefPack& type_pack);
CKTypedefConstObs define_typedef(const std::string& name, const CKTypeRefPack& type_pack);
CKFunctionObs declare_function(const CIName& n, CKFunctionTypeObs type) CKFunctionObs declare_function(const CIName& n, CKFunctionTypeObs type)
{ {
return globtable_->declare_function(n, module_, type); return globtable_->declare_function(n, module_, type);
} }
CKNamedObs find(const CIName& n); CKNamedObs find(const CIName& n);
CKTypedefConstObs CKContext::find_typedef(const CIName& n) const;
bool is_typedef(const CIName& n) const;
CKIRConstantIntObs get_int32_constant(std::int_fast32_t v) CKIRConstantIntObs get_int32_constant(std::int_fast32_t v)
{ {
......
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