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
......@@ -48,12 +48,15 @@ namespace cecko {
}
void CKStructType::dump(CIOStream& os) const
{
if (defined_)
{
os << "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;
}
}
CKFunctionType::CKFunctionType(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic)
: ret_type_(ret_type), arg_types_(std::move(arg_types)), variadic_(variadic), irt_(nullptr)
......@@ -117,11 +120,16 @@ namespace cecko {
arrts_.for_each(dlambda);
fncts_.for_each(dlambda);
*/
os << "// --- STRUCTS ---" << std::endl;
strts_.for_each([&os](auto&& a) {
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)
{
auto irtp = type_pack.type->get_ir();
......@@ -152,6 +160,11 @@ namespace cecko {
auto p = fncs_.find(n);
return p;
}
CKTypedefConstObs CKGlobalTable::find_typedef(const CIName& n) const
{
auto p = typedefs_.find(n);
return p;
}
CKNamedObs CKGlobalTable::find(const CIName& n)
{
auto q = vars_.find(n);
......@@ -163,14 +176,22 @@ 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);
os << "// --- FUNCTION DECLARATIONS ---" << std::endl;
auto decllambda = [&os](auto&& a) {
os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
};
fncs_.for_each(decllambda);
os << "// --- GLOBAL VARIABLES ---" << std::endl;
auto varlambda = [&os](auto&& a) {
os << a->get_type()->declaration(false, a->get_name()) << ";" << CIEndl;
};
vars_.for_each(varlambda);
os << "// --- FUNCTION DEFINITIONS ---" << std::endl;
auto deflambda = [&os](auto&& a) {
if (a->is_defined())
{
......@@ -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)
{
auto var = builder.CreateAlloca(type_pack.type->get_ir(), nullptr, name);
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)
{
auto p = vars_.find(n);
......@@ -217,6 +249,10 @@ namespace cecko {
}
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) {
if (!a->is_arg())
{
......@@ -257,6 +293,11 @@ namespace cecko {
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
{
os << "\t" << get_type_pack().type->declaration(get_type_pack().is_const, get_name()) << ";" << CIEndl;
......@@ -316,6 +357,17 @@ namespace cecko {
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)
{
if (!!loctable_)
......@@ -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()
{
auto t_void = typetable_.get_void_type();
......@@ -349,6 +425,10 @@ 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());
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("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");
......
......@@ -459,17 +459,35 @@ namespace cecko {
class CKAbstractNamed : public CINamePtr, CIImmovable {
public:
virtual ~CKAbstractNamed() {}
virtual bool is_typedef() const { return false; }
virtual bool is_var() const { return false; }
virtual bool is_function() const { return false; }
virtual bool is_const() const { return false; }
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 void dump(CIOStream& os) const = 0;
};
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 {
public:
CKVar(CKTypeRefPack type_pack)
......@@ -517,6 +535,7 @@ namespace cecko {
class CKAbstractScope : CIImmovable {
public:
virtual ~CKAbstractScope() {}
virtual CKTypedefConstObs find_typedef(const CIName& n) const = 0;
virtual CKNamedObs find(const CIName& n) = 0;
};
......@@ -552,16 +571,19 @@ namespace cecko {
public:
CKGlobalTable()
{}
CKTypedefConstObs declare_typedef(const CIName& 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);
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 find_function(const CIName& n);
CKFunctionConstObs find_function(const CIName& n) const;
virtual CKTypedefConstObs find_typedef(const CIName& n) const;
virtual CKNamedObs find(const CIName& n);
void dump(CIOStream& os) const;
private:
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKFunction> fncs_;
CINamedStorage< CKGlobalVar> vars_;
};
......@@ -589,8 +611,10 @@ namespace cecko {
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);
virtual CKTypedefConstObs find_typedef(const CIName& n) const;
virtual CKNamedObs find(const CIName& n);
void dump(CIOStream& os) const;
......@@ -598,6 +622,7 @@ namespace cecko {
private:
CKAbstractScopeObs parent_scope_;
CKFunctionObs function_;
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKLocalVar> vars_;
};
......@@ -670,11 +695,15 @@ namespace cecko {
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); }
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)
{
return globtable_->declare_function(n, module_, type);
}
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)
{
......
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