Constant init prototype working master
authorEbersold <aebersol@n3150.home>
Tue, 12 Apr 2022 21:24:32 +0000 (23:24 +0200)
committerEbersold <aebersol@n3150.home>
Tue, 12 Apr 2022 21:24:32 +0000 (23:24 +0200)
15 files changed:
AST/ASTContext.cpp
CodeGen/CGFunction.cpp
CodeGen/CGModule.cpp
CodeGen/CodeGenType.cpp
IR/CMakeLists.txt
IR/Constant.cpp [new file with mode: 0644]
IR/GlobalVariable.cpp
include/AST/DeclVarDecl.h
include/AST/Expr.h
include/AST/ExprIntegerLiteral.h
include/AST/ExprStringLiteral.h
include/CodeGen/CodeGenModule.h
include/IR/Constant.h
include/IR/GlobalVariable.h
include/IR/Type.h

index fa935572e39b07d2aef883c705e7f20960830371..f6f9c546a9f811c74c901bcf5cd127acc64f9812 100644 (file)
@@ -105,10 +105,11 @@ ASTContext::getRecordType(const RecordDecl *D)
 Type &
 ASTContext::getConstantArrayType(Type *EltTy,long length)
 {
-  std::cerr<<"ASTContext::getConstantArrayType: TODO "<<length<<std::endl;
+  std::cerr<<"ASTContext::getConstantArrayType: TODO len: "<<length<<std::endl;
   ConstantArrayType *cat = new ConstantArrayType(EltTy,length);
+  m_ConstantArrays.push_back(cat);
   //Todo implement  
-  return CharTy;
+  return *cat;
 }
 
 Type &
index 5e3b1b676c7a586ff7d37356b42c573ceaff4725..c78213eaf58974a03bd344a8042e61c06a541802 100644 (file)
@@ -427,7 +427,13 @@ void CodeGenFunction::EmitOutputStmt(AST::OutputStmt *S)
         // 
         // 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::EmitOutputStmt Parameter is StringLiteral NOT GOOD missing declaration "<<u->getString()<<std::endl;
+        std::cerr<<"ERROR:  CodeGenFunction::EmitOutputStmt Parameter is StringLiteral NOT GOOD missing declaration "<<u->getString();
+        if (u->isConstantArray)
+        { 
+          std::cerr<<" const "<<std::endl;
+        }
+        else 
+          std::cerr<<" not const"<<std::endl;
         value = m_CGM.getOrCreateGlobal(u->getString(),false);
         if (c) 
         {
@@ -613,10 +619,20 @@ void CodeGenFunction::EmitCallExpr(AST::CallExpr *S)
       if ( AST::StringLiteral *u = dynamic_cast<AST::StringLiteral *>(*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 "<<u->getString()<<std::endl;
-        value = m_CGM.getOrCreateGlobal(u->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<aeb::lds::Type *>(value->getType())
+              ,u->getString().c_str());
+          reinterpret_cast<GlobalVariable *>(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<AST::ParmVarDecl *>(u->getDecl());
         if (pv)
         {
-         value = getCurrentFunction()->getArgument(pv->getName());
-         if (value)
-         {
+          value = getCurrentFunction()->getArgument(pv->getName());
+          if (value)
+          {
             std::cout<<"CodeGenFunction::EmitCallExpr "<<d->getName()<<" FOUND  Param "<<pv->getName()<<" Idx="<<ParamIdx<<" PV Type NULL ? "<<(pv->getType() == NULL)<<std::endl;
-         } else
-         {
+          } else
+          {
             std::cout<<"CodeGenFunction::EmitCallExpr ERROR function call parameter not globalVariable !!!!!! "<<ParamIdx<<std::endl;
             value = m_CGM.getOrCreateGlobal(nv);
-         }
+          }
         } else
-       {       
+        {
           //TODO BAD AND WRONG IT's A PARAMETER FROM THE CURRENT CALL STACK
           value = m_CGM.getOrCreateGlobal(nv);
-       }
+        }
 #ifdef CG_DEBUG 
         std::cout<<"CodeGenFunction::EmittCallExpr call "<<d->getName()<<" Param NamedDecl: "<<nv->getName()<<" "<<std::endl;
 #endif
index 1322811fe0f8e54db8214fa1e49a2d0c8f88e4ab..ed4df7d0fbd21eb16efab893b1c0188eaa55153b 100644 (file)
@@ -129,7 +129,7 @@ namespace CodeGen {
                    if (pv->getType() != NULL)
                    {
 #if 1
-                    iTy = getType().ConvertType(pv->getType());
+                     iTy = getType().ConvertType(pv->getType());
 #else
                      // std::cout<<"CodeGenModule::EmitGlobalFunctionDefinition "<<pv->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<AST::VarDecl &>(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<AST::StringLiteral *>(E)->getString().c_str());
+          break;
+          default:
+          std::cerr<<"getInitializer For: "<<D.getName()<<" No Type"<<std::endl;
+        }
+      }
+      return co;
+    }
+
     /**
      *
      */
@@ -280,7 +310,12 @@ namespace CodeGen {
       std::cout<<" "<<D->getName()<<std::endl;
       aeb::lds::Type *Ty = getType().ConvertType(D->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<<std::endl;
-      GV =m_Module->insertGlobal(_flag,constant);
+      if (_Ty != NULL)
+      {
+        std::cout<<"CodeGenModule::getOrCreateGlobal deprecated: "<<_flag<<std::endl;
+      } else
+      {
+        std::cout<<"CodeGenModule::getOrCreateGlobal "<<_flag<<" _Ty == NULL"<<std::endl;
+      }
+      aeb::lds::Type *Ty = getType().ConvertType(static_cast<AST::Type *>(_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 "<<D->getName()<<std::endl;
-      GV =m_Module->insertGlobal(D->getName(),Ty);
+      GV =m_Module->insertGlobal(D->getName(),false,Ty);
       return GV;
     }
 }
index a38e8f06304e354fbaf39ad7d9079ecf14438e60..75eed153e32cd0531b08db2060335be60ad1cebc 100644 (file)
@@ -77,6 +77,13 @@ namespace CodeGen {
           AST::TypedefType *TDTy = dynamic_cast<AST::TypedefType *>(Ty);
           AST::TypedefDecl *td   = static_cast<AST::TypedefDecl *>(TDTy->getDecl());
           return ConvertType(td->getBaseType());
+        }else if ( Ty->getClass() == AST::Type::ConstantArray ||
+                   Ty->getClass() == AST::Type::Array)
+        {
+          AST::ArrayType *ArTy = dynamic_cast<AST::ArrayType *>(Ty);
+          const AST::Type *ElType = ArTy->getElementType();
+          // Recursive call BAD
+          return ConvertType(const_cast<AST::Type *>(ElType));
         }else
         {
           std::cerr<<"CodeGenType::ConvertType Not builtin TODO return NULL not good"<<std::endl;
@@ -84,7 +91,7 @@ namespace CodeGen {
         }
       } else
       {
-        std::cerr<<"CodeGenType::ConvertType Not Type return NULL not good"<<std::endl;
+        std::cerr<<"CodeGenType::ConvertType  Ty==NULL return NULL not good"<<std::endl;
         return NULL;
       }
     }
index 6dc44e5aab9156781d056bf9d99df1754927df05..c6a032024bc6707a81847dfa8db06940b4c52717 100644 (file)
@@ -7,6 +7,7 @@ INCLUDE_DIRECTORIES(${aebutils_SOURCE_DIR})
 ADD_LIBRARY(commonIR
     LdsInstruction.cpp
     BasicBlock.cpp
+    Constant.cpp
     Transition.cpp
     Argument.cpp
     GlobalVariable.cpp
diff --git a/IR/Constant.cpp b/IR/Constant.cpp
new file mode 100644 (file)
index 0000000..1cab2bb
--- /dev/null
@@ -0,0 +1,59 @@
+#include <iostream>
+#include <algorithm>
+#include <cassert>
+#include <vector>
+#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;
+}
+
+}
+}
+
+
+
index 08b2b51f05da64a646a247938d5801ae7a771a1c..54d45cdf259e0da0f7574972365a450567ab68d1 100644 (file)
@@ -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<Constant *>(Op<0>().get());
 }
 
 }
index db5d67f526ab8fbdedcb33041e701a999677b01e..9590555f4823ff263a14a4ece611d5e31ed9da46 100644 (file)
@@ -31,7 +31,7 @@ class VarDecl : public ValueDecl
         void setInit(Expr *e)
         { m_Init = e; } ;
         ///
-        Expr *getInit(Expr *e)
+        Expr *getInit()
         { return dynamic_cast<AST::Expr *>(m_Init); };
 };
 /*
index bb47fe9b1fa1b52311a0f593c0b852529f3c817e..96b25d0aac8a941bc83224ae4a06409b383fd1f7 100644 (file)
@@ -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
          */ 
index 6ac26ff439838749dff4ef79cd51466817600c6b..814808962b9e6d457ce3d2ad265c8d531e0fecec 100644 (file)
@@ -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;
index f8dad6fdefea62328edb8360cda9ade4974544dd..1445cb292efd0318ec2507062b01f9d9d404fb8b 100644 (file)
@@ -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; } ;
index f0fc896b96811d37603a4fb39dbe963c9a08e349..62a28e5be7b3199031425b7d35948e089bdfd1c8 100644 (file)
@@ -63,6 +63,8 @@ class CodeGenModule {
 #endif      
       GlobalVariable * getOrCreateGlobal(const AST::ValueDecl *D);
   protected:
+  private:
+      aeb::lds::Constant *getInitializerFromVarDecl(const AST::VarDecl &D);
 };
 
 }
index d3bd30120c703604b595b3eda2f771ac8335912a..18fe7fe6029107f3f2d3b58b71b582ac794d7dc8 100644 (file)
@@ -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);
 
 };
 
index 81a99d2877ad004909c40184165a0fe2a4d505ca..29b0f95d6d956b0b34c1178422a24cae627b5b82 100644 (file)
@@ -26,7 +26,7 @@ class Constant;
  */
 class GlobalVariable  : public aeb::dlilist_node<GlobalVariable> , public Constant {
     protected:
-        friend class SymbolTableListTraits<GlobalVariable>;   
+    friend class SymbolTableListTraits<GlobalVariable>;
         // 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<GlobalVariable> , 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<GlobalVariable>::op_begin(this),1)
-              , m_Parent(NULL)
-        { Value::setName(flg); } ;
-#endif
+
         virtual ~GlobalVariable()  {} ;
 
         inline bool hasInitializer() const { return false; };
index 2aa746b38eb385fadb783e6db245a6675a5c0af9..8fff146f7e4e6aab0b0afce3f542c146ab5ff224 100644 (file)
@@ -26,7 +26,7 @@ class Type
         TimeTyID ,
         DurationTyID,
         FunctionTyID,
-       StructTyID  ,
+        StructTyID  ,
         ArrayTyID   ,
         PointerTyID  
     };