From 6f1b812b44d372c690ce074ace12076329fd84c8 Mon Sep 17 00:00:00 2001 From: Ebersold Date: Tue, 12 Apr 2022 23:24:32 +0200 Subject: [PATCH] Constant init prototype working --- AST/ASTContext.cpp | 5 +-- CodeGen/CGFunction.cpp | 38 ++++++++++++++------ CodeGen/CGModule.cpp | 57 +++++++++++++++++++++++++++--- CodeGen/CodeGenType.cpp | 9 ++++- IR/CMakeLists.txt | 1 + IR/Constant.cpp | 59 ++++++++++++++++++++++++++++++++ IR/GlobalVariable.cpp | 8 ++++- include/AST/DeclVarDecl.h | 2 +- include/AST/Expr.h | 2 +- include/AST/ExprIntegerLiteral.h | 3 ++ include/AST/ExprStringLiteral.h | 4 +-- include/CodeGen/CodeGenModule.h | 2 ++ include/IR/Constant.h | 58 ++++++++++++++++++++++++++++++- include/IR/GlobalVariable.h | 9 ++--- include/IR/Type.h | 2 +- 15 files changed, 226 insertions(+), 33 deletions(-) create mode 100644 IR/Constant.cpp diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index fa93557..f6f9c54 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -105,10 +105,11 @@ ASTContext::getRecordType(const RecordDecl *D) Type & ASTContext::getConstantArrayType(Type *EltTy,long length) { - std::cerr<<"ASTContext::getConstantArrayType: TODO "<getString()<getString(); + if (u->isConstantArray) + { + std::cerr<<" const "<getString(),false); if (c) { @@ -613,10 +619,20 @@ void CodeGenFunction::EmitCallExpr(AST::CallExpr *S) if ( AST::StringLiteral *u = dynamic_cast(*it) ) { // - // This is wrong. I should check from which context is the parameter. + // TODO This is wrong. I should check from which context is the parameter. // Only if the parameter name is from global context std::cerr<<"ERROR: CodeGenFunction::EmitCallExpr Parameter is StringLiteral NOT GOOD missing declaration "<getString()<getString(),false); + + value = m_CGM.getOrCreateGlobal(u->getString(),u->isConstantArray,u->getType()); +#if 1 + if (u->isConstantArray) + { + aeb::lds::Constant *co = aeb::lds::ConstantDataArray::getString( + const_cast(value->getType()) + ,u->getString().c_str()); + reinterpret_cast(value)->setInitializer(co); + } +#endif if (c) { // Call c function there is no definition or body @@ -634,20 +650,20 @@ void CodeGenFunction::EmitCallExpr(AST::CallExpr *S) AST::ParmVarDecl *pv = dynamic_cast(u->getDecl()); if (pv) { - value = getCurrentFunction()->getArgument(pv->getName()); - if (value) - { + value = getCurrentFunction()->getArgument(pv->getName()); + if (value) + { std::cout<<"CodeGenFunction::EmitCallExpr "<getName()<<" FOUND Param "<getName()<<" Idx="<getType() == NULL)<getName()<<" Param NamedDecl: "<getName()<<" "<getType() != NULL) { #if 1 - iTy = getType().ConvertType(pv->getType()); + iTy = getType().ConvertType(pv->getType()); #else // std::cout<<"CodeGenModule::EmitGlobalFunctionDefinition "<getName()<<" Has type "; switch (pv->getType()->getClass()) @@ -270,6 +270,36 @@ namespace CodeGen { } + aeb::lds::Constant * + CodeGenModule::getInitializerFromVarDecl(const AST::VarDecl &D) + { + aeb::lds::Constant *co = NULL; + if (D.hasInit()) + { + AST::Expr *E = const_cast(D).getInit(); + AST::Expr::EvalResult r; + E->evalute(r); + aeb::lds::Type *vTy = getType().ConvertType(E->getType()); + + switch (vTy->getTypeID()) + { + case aeb::lds::Type::IntegerTyID : + co = aeb::lds::ConstantInt::getInteger( + vTy + ,r.u.l); + break; + case aeb::lds::Type::ArrayTyID : + co = aeb::lds::ConstantDataArray::getString( + vTy + ,reinterpret_cast(E)->getString().c_str()); + break; + default: + std::cerr<<"getInitializer For: "<getName()<getType()); GV =m_Module->insertGlobal(D->getName(),true,Ty); - } + // Is there a default value + if (aeb::lds::Constant *co = getInitializerFromVarDecl(*D)) + { + GV->setInitializer(co); + } + } /** * */ @@ -291,6 +326,11 @@ namespace CodeGen { ,D->getName()); aeb::lds::Type *Ty = getType().ConvertType(D->getType()); GV =m_Module->insertGlobal(D->getName(),false,Ty); + // Is there a default value + if (aeb::lds::Constant *co = getInitializerFromVarDecl(*D)) + { + GV->setInitializer(co); + } } /** * @@ -310,8 +350,15 @@ namespace CodeGen { GlobalVariable * CodeGenModule::getOrCreateGlobal(const std::string &_flag,bool constant,void *_Ty ) { GlobalVariable *GV; - std::cout<<"CodeGenModule::getOrCreateGlobal deprecated: "<<_flag<insertGlobal(_flag,constant); + if (_Ty != NULL) + { + std::cout<<"CodeGenModule::getOrCreateGlobal deprecated: "<<_flag<(_Ty)); + GV =m_Module->insertGlobal(_flag,constant,Ty); return GV; } #endif @@ -320,7 +367,7 @@ namespace CodeGen { GlobalVariable *GV; aeb::lds::Type *Ty = getType().ConvertType(D->getType()); //std::cout<<"CodeGenModule::getOrCreateGlobal "<getName()<insertGlobal(D->getName(),Ty); + GV =m_Module->insertGlobal(D->getName(),false,Ty); return GV; } } diff --git a/CodeGen/CodeGenType.cpp b/CodeGen/CodeGenType.cpp index a38e8f0..75eed15 100644 --- a/CodeGen/CodeGenType.cpp +++ b/CodeGen/CodeGenType.cpp @@ -77,6 +77,13 @@ namespace CodeGen { AST::TypedefType *TDTy = dynamic_cast(Ty); AST::TypedefDecl *td = static_cast(TDTy->getDecl()); return ConvertType(td->getBaseType()); + }else if ( Ty->getClass() == AST::Type::ConstantArray || + Ty->getClass() == AST::Type::Array) + { + AST::ArrayType *ArTy = dynamic_cast(Ty); + const AST::Type *ElType = ArTy->getElementType(); + // Recursive call BAD + return ConvertType(const_cast(ElType)); }else { std::cerr<<"CodeGenType::ConvertType Not builtin TODO return NULL not good"< +#include +#include +#include +#include "IR/Value.h" +#include "IR/Type.h" +#if 0 +#include "IR/Transition.h" +#include "IR/Behavior.h" +#include "IR/Function.h" +#include "IR/Signal.h" +#include "IR/Argument.h" +#include "IR/BasicBlock.h" +#endif +#define FUNCTION_SYMTAB +// May be Create TraitsSigArgumentImpl.h +#include "IR/Constant.h" +namespace aeb { + // Template instantiation +namespace lds { + + +/** + * ConstantInt + * + */ +ConstantInt::ConstantInt(Type *Ty,unsigned long _vTy,long l) + : ConstantData(Ty,_vTy) , m_Value(l) +{ +} + +Constant * +ConstantInt::getInteger(Type *Ty,long _v) +{ + return new ConstantInt(Ty,ConstantIntVal, _v); +} + +/** + * ConstantDataArray Implementation + * + */ +ConstantDataArray::ConstantDataArray(Type *Ty,unsigned _vTy) + : ConstantData(Ty,_vTy) +{ +} + +Constant * +ConstantDataArray::getString(Type *Ty,const char *_val) +{ + Constant *c = new ConstantDataArray(Ty,ConstantArrayVal); + c->setName(_val); + return c; +} + +} +} + + + diff --git a/IR/GlobalVariable.cpp b/IR/GlobalVariable.cpp index 08b2b51..54d45cd 100644 --- a/IR/GlobalVariable.cpp +++ b/IR/GlobalVariable.cpp @@ -28,12 +28,18 @@ GlobalVariable::GlobalVariable(const std::string &flg , bool constant,Type *_Ty) void GlobalVariable::setInitializer(Constant *InitVal) { + if (InitVal) + { + Op<0>().set(InitVal); + } else + { // Null may be reset + } } Constant * GlobalVariable::getInitializer() { - return NULL; + return static_cast(Op<0>().get()); } } diff --git a/include/AST/DeclVarDecl.h b/include/AST/DeclVarDecl.h index db5d67f..9590555 100644 --- a/include/AST/DeclVarDecl.h +++ b/include/AST/DeclVarDecl.h @@ -31,7 +31,7 @@ class VarDecl : public ValueDecl void setInit(Expr *e) { m_Init = e; } ; /// - Expr *getInit(Expr *e) + Expr *getInit() { return dynamic_cast(m_Init); }; }; /* diff --git a/include/AST/Expr.h b/include/AST/Expr.h index bb47fe9..96b25d0 100644 --- a/include/AST/Expr.h +++ b/include/AST/Expr.h @@ -41,7 +41,7 @@ class Expr : public Stmt { * May be set this function virtual. It might need more * memory */ - bool evalute(EvalResult &result) ; + virtual bool evalute(EvalResult &result) ; /** * evaluate constant expression should exist too */ diff --git a/include/AST/ExprIntegerLiteral.h b/include/AST/ExprIntegerLiteral.h index 6ac26ff..8148089 100644 --- a/include/AST/ExprIntegerLiteral.h +++ b/include/AST/ExprIntegerLiteral.h @@ -12,6 +12,9 @@ class IntegerLiteral : public Expr { * In term should return an APValue or Value type */ unsigned int get_value() const { return m_value; }; + + virtual bool evalute(EvalResult &result) + { result.u.l = m_value; return true; } protected: unsigned int m_value; diff --git a/include/AST/ExprStringLiteral.h b/include/AST/ExprStringLiteral.h index f8dad6f..1445cb2 100644 --- a/include/AST/ExprStringLiteral.h +++ b/include/AST/ExprStringLiteral.h @@ -11,8 +11,8 @@ class StringLiteral : public Expr std::string m_string; public: bool isConstantArray; - StringLiteral(const std::string &s,Type *_t) - : isConstantArray(false), Expr(StringLiteralClass,_t),m_string(s) { + StringLiteral(const std::string &s,Type *_t,bool _isConst = false) + : isConstantArray(_isConst), Expr(StringLiteralClass,_t),m_string(s) { } std::string getString() const {return m_string; } ; diff --git a/include/CodeGen/CodeGenModule.h b/include/CodeGen/CodeGenModule.h index f0fc896..62a28e5 100644 --- a/include/CodeGen/CodeGenModule.h +++ b/include/CodeGen/CodeGenModule.h @@ -63,6 +63,8 @@ class CodeGenModule { #endif GlobalVariable * getOrCreateGlobal(const AST::ValueDecl *D); protected: + private: + aeb::lds::Constant *getInitializerFromVarDecl(const AST::VarDecl &D); }; } diff --git a/include/IR/Constant.h b/include/IR/Constant.h index d3bd301..18fe7fe 100644 --- a/include/IR/Constant.h +++ b/include/IR/Constant.h @@ -25,8 +25,64 @@ class Constant : public User { : User(Ty,vty,u,nbOperand) {} public: + +}; + + +/** + * + * + */ +class ConstantData : public Constant +{ + friend class Constant; + protected: + ConstantData(Type *Ty,unsigned _vTy) : Constant(Ty,_vTy,NULL,0) {} + protected: + void * operator new(size_t S) { return User::operator new (S,0) ; } + public: + void operator delete(void *_ptr) { return User::operator delete(_ptr) ; } + + ConstantData(const ConstantData &_c) = delete; +}; + +/** + * + * + */ +class ConstantInt : public ConstantData +{ + friend class Constant; + private: + long m_Value; + ConstantInt(Type *Ty,unsigned long _vTy,long _d = 0) ; + protected: + + public: + ConstantInt(const ConstantInt &_c) = delete; // Need some basic Constructors - Constant *getInteger(Type *Ty,long _value) { return NULL ; }; + static Constant *getInteger(Type *Ty,long _value) ; + + inline long getValue() { return m_Value ; } +}; + +/** + * + * + */ +class ConstantDataArray : public ConstantData +{ + private: + ConstantDataArray(Type *Ty,unsigned vTy); + // For test purpose + std::string m_Value; + public: + + ConstantDataArray(const ConstantDataArray &_c) = delete; + /** + * + */ + static Constant *getString(Type *_Ty,const char *_value); }; diff --git a/include/IR/GlobalVariable.h b/include/IR/GlobalVariable.h index 81a99d2..29b0f95 100644 --- a/include/IR/GlobalVariable.h +++ b/include/IR/GlobalVariable.h @@ -26,7 +26,7 @@ class Constant; */ class GlobalVariable : public aeb::dlilist_node , public Constant { protected: - friend class SymbolTableListTraits; + friend class SymbolTableListTraits; // TODO the information bellow are stored in Value Module *m_Parent; bool m_IsConstant : 1; /* Is it a constant Global Variable */ @@ -34,12 +34,7 @@ class GlobalVariable : public aeb::dlilist_node , public Consta GlobalVariable(const GlobalVariable &_g) : Constant(*this) {} public: GlobalVariable(const std::string &flg , bool constant,Type *_Ty = NULL); -#if 0 - : m_IsConstant(constant) - , Constant(_Ty, GlobalVariableVal,OperandTraits::op_begin(this),1) - , m_Parent(NULL) - { Value::setName(flg); } ; -#endif + virtual ~GlobalVariable() {} ; inline bool hasInitializer() const { return false; }; diff --git a/include/IR/Type.h b/include/IR/Type.h index 2aa746b..8fff146 100644 --- a/include/IR/Type.h +++ b/include/IR/Type.h @@ -26,7 +26,7 @@ class Type TimeTyID , DurationTyID, FunctionTyID, - StructTyID , + StructTyID , ArrayTyID , PointerTyID }; -- 2.30.2