Exemple #1
0
        public void AddNewMember(string memberName, bool isStatic, ClepsType memberType)
        {
            Debug.Assert(!DoesClassContainMember(memberName));

            if (isStatic)
            {
                if (memberType.IsBasicType)
                {
                    StaticMemberVariables.Add(memberName, memberType);
                }
                else
                {
                    StaticMemberMethods.Add(memberName, memberType);
                }
            }
            else
            {
                if (memberType.IsBasicType)
                {
                    MemberVariables.Add(memberName, memberType);
                }
                else
                {
                    MemberMethods.Add(memberName, memberType);
                }
            }
        }
        public LLVMTypeRef? GetPrimitiveLLVMTypeOrNull(ClepsType clepsType)
        {
            if (clepsType.IsFunctionType)
            {
                throw new Exception("Cannot convert function cleps type to llvm type");
            }

            if (clepsType.IsVoidType)
            {
                return LLVM.VoidTypeInContext(Context);
            }

            string identifierText = clepsType.RawTypeName;
            int indirectionLevel = clepsType.PtrIndirectionLevel;

            if (identifierText == "System.LLVMTypes.I1")
            {
                return GetPtrToLLVMType(LLVM.Int1TypeInContext(Context), indirectionLevel);
            }
            else if (identifierText == "System.LLVMTypes.I32")
            {
                return GetPtrToLLVMType(LLVM.Int32TypeInContext(Context), indirectionLevel);
            }

            return null;
        }
Exemple #3
0
        public LLVMTypeRef?GetLLVMTypeOrNull(ClepsType clepsType)
        {
            LLVMTypeRef?primitiveLLVMType = GetPrimitiveLLVMTypeOrNull(clepsType);

            if (primitiveLLVMType != null)
            {
                return(primitiveLLVMType);
            }

            string identifierText   = clepsType.RawTypeName;
            int    indirectionLevel = clepsType.PtrIndirectionLevel;

            LLVMTypeRef classLLVMType;
            LLVMTypeRef?ret = null;

            if (ClassSkeletons.TryGetValue(identifierText, out classLLVMType))
            {
                ret = classLLVMType;
                if (clepsType.IsArrayType)
                {
                    foreach (uint arrayDim in clepsType.ArrayDimensions)
                    {
                        ret = LLVM.ArrayType(ret.Value, arrayDim);
                    }
                }

                ret = GetPtrToLLVMType(ret.Value, indirectionLevel);
            }

            return(ret);
        }
Exemple #4
0
 public void AddRawLLVMTypeMapping(string className, ClepsType rawLLVMTypeMap)
 {
     Debug.Assert(LoadedClassesAndMembers.ContainsKey(className));
     Debug.Assert(!RawLLVMTypeMappingClasses.ContainsKey(rawLLVMTypeMap));
     RawLLVMTypeMappingClasses.Add(rawLLVMTypeMap, LoadedClassesAndMembers[className]);
     LoadedClassesAndMembers[className].AddRawLLVMTypeMapping(rawLLVMTypeMap);
 }
        public LLVMTypeRef?GetPrimitiveLLVMTypeOrNull(ClepsType clepsType)
        {
            if (clepsType.IsFunctionType)
            {
                throw new Exception("Cannot convert function cleps type to llvm type");
            }

            if (clepsType.IsVoidType)
            {
                return(LLVM.VoidTypeInContext(Context));
            }

            string identifierText   = clepsType.RawTypeName;
            int    indirectionLevel = clepsType.PtrIndirectionLevel;

            if (identifierText == "System.LLVMTypes.I1")
            {
                return(GetPtrToLLVMType(LLVM.Int1TypeInContext(Context), indirectionLevel));
            }
            else if (identifierText == "System.LLVMTypes.I32")
            {
                return(GetPtrToLLVMType(LLVM.Int32TypeInContext(Context), indirectionLevel));
            }

            return(null);
        }
Exemple #6
0
        public void AddNewMember(string memberName, bool isStatic, ClepsType memberType)
        {
            Debug.Assert(!DoesClassContainMember(memberName));

            if (isStatic)
            {
                if (memberType.IsBasicType)
                {
                    StaticMemberVariables.Add(memberName, memberType);
                }
                else
                {
                    StaticMemberMethods.Add(memberName, memberType);
                }
            }
            else
            {
                if (memberType.IsBasicType)
                {
                    MemberVariables.Add(memberName, memberType);
                }
                else
                {
                    MemberMethods.Add(memberName, memberType);
                }
            }
        }
Exemple #7
0
        private static bool IsEqual(ClepsType c1, ClepsType c2)
        {
            if (Object.ReferenceEquals(c1, c2))
            {
                return(true);
            }
            else if (Object.ReferenceEquals(c1, null) || Object.ReferenceEquals(c2, null))
            {
                return(false);
            }

            if (c1.IsVoidType == true && c2.IsVoidType == true)
            {
                return(true);
            }

            if (c1.IsFunctionType == true && c2.IsFunctionType == true &&
                c1.FunctionReturnType == c2.FunctionReturnType &&
                c1.FunctionParameters.Count == c2.FunctionParameters.Count &&
                c1.FunctionParameters.Zip(c2.FunctionParameters, (p1, p2) => p1 == p2).All(e => e))
            {
                return(true);
            }

            if (c1.IsBasicType == true && c2.IsBasicType == true &&
                c1.RawTypeName == c2.RawTypeName &&
                c1.ArrayDimensions.SequenceEqual(c2.ArrayDimensions) &&
                c1.PtrIndirectionLevel == c2.PtrIndirectionLevel)
            {
                return(true);
            }

            return(false);
        }
Exemple #8
0
 public void AddRawLLVMTypeMapping(string className, ClepsType rawLLVMTypeMap)
 {
     Debug.Assert(LoadedClassesAndMembers.ContainsKey(className));
     Debug.Assert(!RawLLVMTypeMappingClasses.ContainsKey(rawLLVMTypeMap));
     RawLLVMTypeMappingClasses.Add(rawLLVMTypeMap, LoadedClassesAndMembers[className]);
     LoadedClassesAndMembers[className].AddRawLLVMTypeMapping(rawLLVMTypeMap);
 }
Exemple #9
0
        private static bool IsEqual(ClepsType c1, ClepsType c2)
        {
            if(Object.ReferenceEquals(c1, c2))
            {
                return true;
            }
            else if(Object.ReferenceEquals(c1, null) || Object.ReferenceEquals(c2, null))
            {
                return false;
            }

            if (c1.IsVoidType == true && c2.IsVoidType == true)
            {
                return true;
            }

            if (c1.IsFunctionType == true && c2.IsFunctionType == true &&
                c1.FunctionReturnType == c2.FunctionReturnType &&
                c1.FunctionParameters.Count == c2.FunctionParameters.Count &&
                c1.FunctionParameters.Zip(c2.FunctionParameters, (p1, p2) => p1 == p2).All(e => e))
            {
                return true;
            }

            if (c1.IsBasicType == true && c2.IsBasicType == true && 
                c1.RawTypeName == c2.RawTypeName &&
                c1.ArrayDimensions.SequenceEqual(c2.ArrayDimensions) &&
                c1.PtrIndirectionLevel == c2.PtrIndirectionLevel)
            {
                return true;
            }

            return false;
        }
        public LLVMTypeRef? GetLLVMTypeOrNull(ClepsType clepsType)
        {
            LLVMTypeRef? primitiveLLVMType = GetPrimitiveLLVMTypeOrNull(clepsType);

            if (primitiveLLVMType != null)
            {
                return primitiveLLVMType;
            }

            string identifierText = clepsType.RawTypeName;
            int indirectionLevel = clepsType.PtrIndirectionLevel;

            LLVMTypeRef classLLVMType;
            LLVMTypeRef? ret = null;

            if (ClassSkeletons.TryGetValue(identifierText, out classLLVMType))
            {
                ret = classLLVMType;
                if (clepsType.IsArrayType)
                {
                    foreach (uint arrayDim in clepsType.ArrayDimensions)
                    {
                        ret = LLVM.ArrayType(ret.Value, arrayDim);
                    }
                }

                ret = GetPtrToLLVMType(ret.Value, indirectionLevel);
            }

            return ret;
        }
Exemple #11
0
        public static ClepsType GetPointerToBasicType(ClepsType basicType)
        {
            if (!basicType.IsBasicType)
            {
                throw new Exception("Expected basic type");
            }

            return(GetBasicType(basicType.RawTypeName, basicType.ArrayDimensions, basicType.PtrIndirectionLevel + 1));
        }
Exemple #12
0
        public void SetLLVMRegister(string variableName, ClepsType variableType, LLVMValueRef llvmValueRef)
        {
            var localVariablesBlockWithVar = LocalVariableBlocks.Where(b => b.ContainsKey(variableName)).LastOrDefault();
            Debug.Assert(localVariablesBlockWithVar != null);
            
            LLVMRegister ret = localVariablesBlockWithVar[variableName];
            Debug.Assert(ret.VariableType == variableType);

            localVariablesBlockWithVar[variableName] = new LLVMRegister(variableType, llvmValueRef);
        }
Exemple #13
0
        public static ClepsType GetArrayAccessOnType(ClepsType arrayType, int accessDimensions)
        {
            Debug.Assert(arrayType.IsArrayType);
            Debug.Assert(arrayType.ArrayDimensions.Count >= accessDimensions);

            List <uint> newDims = arrayType.ArrayDimensions.Take(arrayType.ArrayDimensions.Count - accessDimensions).ToList();
            ClepsType   ret     = GetBasicType(arrayType.RawTypeName, newDims, arrayType.PtrIndirectionLevel);

            return(ret);
        }
Exemple #14
0
        public override bool Equals(object obj)
        {
            if (obj == null || typeof(ClepsType) != obj.GetType())
            {
                return(false);
            }

            ClepsType p = (ClepsType)obj;

            return(this == p);
        }
Exemple #15
0
        public void SetLLVMRegister(string variableName, ClepsType variableType, LLVMValueRef llvmValueRef)
        {
            var localVariablesBlockWithVar = LocalVariableBlocks.Where(b => b.ContainsKey(variableName)).LastOrDefault();

            Debug.Assert(localVariablesBlockWithVar != null);

            LLVMRegister ret = localVariablesBlockWithVar[variableName];

            Debug.Assert(ret.VariableType == variableType);

            localVariablesBlockWithVar[variableName] = new LLVMRegister(variableType, llvmValueRef);
        }
Exemple #16
0
 public static ClepsType GetFunctionType(ClepsType functionReturnType, List <ClepsType> functionParameters)
 {
     return(new ClepsType()
     {
         IsFunctionType = true,
         IsVoidType = false,
         IsBasicType = false,
         IsPointerType = false,
         FunctionReturnType = functionReturnType,
         FunctionParameters = functionParameters
     });
 }
Exemple #17
0
        public void AddNewMember(string className, string memberName, bool isStatic, ClepsType memberType)
        {
            Debug.Assert(LoadedClassesAndMembers.ContainsKey(className));
            LoadedClassesAndMembers[className].AddNewMember(memberName, isStatic, memberType);

            if (memberType.IsFunctionType && isStatic &&
                (
                    memberType.FunctionReturnType == ClepsType.GetBasicType("System.Types.Int32", new List <uint>() /* array dims */, 0 /* ptrIndirectionLevel */) ||
                    memberType.FunctionReturnType == ClepsType.GetBasicType("System.LLVMTypes.I32", new List <uint>() /* array dims */, 0 /* ptrIndirectionLevel */)
                ) &&
                memberType.FunctionParameters.Count == 0 && memberName == "Main")
            {
                string fullyQualifiedName = className + "." + memberName;
                MainFunctionFullNames.Add(fullyQualifiedName);
            }
        }
Exemple #18
0
        public void AddNewMember(string className, string memberName, bool isStatic, ClepsType memberType)
        {
            Debug.Assert(LoadedClassesAndMembers.ContainsKey(className));
            LoadedClassesAndMembers[className].AddNewMember(memberName, isStatic, memberType);

            if (memberType.IsFunctionType && isStatic && 
                (
                    memberType.FunctionReturnType == ClepsType.GetBasicType("System.Types.Int32", new List<uint>() /* array dims */, 0 /* ptrIndirectionLevel */) ||
                    memberType.FunctionReturnType == ClepsType.GetBasicType("System.LLVMTypes.I32", new List<uint>() /* array dims */, 0 /* ptrIndirectionLevel */)
                )
                && memberType.FunctionParameters.Count == 0 && memberName == "Main")
            {
                string fullyQualifiedName = className + "." + memberName;
                MainFunctionFullNames.Add(fullyQualifiedName);
            }
        }
        public ClepsType GetClepsNativeLLVMType(LLVMTypeRef llvmType)
        {
            if (llvmType.TypeKind == LLVMTypeKind.LLVMIntegerTypeKind)
            {
                if (llvmType.GetIntTypeWidth() == 1)
                {
                    return(ClepsType.GetBasicType("System.LLVMTypes.I1", 0 /* ptr indirection level */));
                }
                else if (llvmType.GetIntTypeWidth() == 32)
                {
                    return(ClepsType.GetBasicType("System.LLVMTypes.I32", 0 /* ptr indirection level */));
                }
            }
            else if (llvmType.TypeKind == LLVMTypeKind.LLVMVoidTypeKind)
            {
                //void type does not have a special implementation in Cleps - use the same thing for LLVM or cleps native types
                return(ClepsType.GetVoidType());
            }

            throw new Exception("Unknown llvm type - cannot convert to cleps native type");
        }
        public LLVMTypeRef?GetLLVMTypeOrNull(ClepsType clepsType)
        {
            LLVMTypeRef?primitiveLLVMType = GetPrimitiveLLVMTypeOrNull(clepsType);

            if (primitiveLLVMType != null)
            {
                return(primitiveLLVMType);
            }

            string identifierText   = clepsType.RawTypeName;
            int    indirectionLevel = clepsType.PtrIndirectionLevel;

            LLVMTypeRef ret;

            if (ClassSkeletons.TryGetValue(identifierText, out ret))
            {
                return(GetPtrToLLVMType(ClassSkeletons[identifierText], indirectionLevel));
            }

            return(null);
        }
Exemple #21
0
 public void AddLocalVariable(string variableName, ClepsType variableType, LLVMValueRef llvmValueRef)
 {
     Debug.Assert(LocalVariableBlocks.Count > 0);
     LocalVariableBlocks.Last().Add(variableName, new LLVMRegister(variableType, llvmValueRef));
 }
Exemple #22
0
 public bool RawLLVMTypeMappingExists(ClepsType rawLLVMTypeMap)
 {
     return RawLLVMTypeMappingClasses.ContainsKey(rawLLVMTypeMap);
 }
Exemple #23
0
 public void AddRawLLVMTypeMapping(ClepsType rawLLVMTypeMap)
 {
     Debug.Assert(RawLLVMTypeMap == null);
     RawLLVMTypeMap = rawLLVMTypeMap;
 }
Exemple #24
0
 public LLVMRegister(ClepsType variableType, LLVMValueRef llvmPtrValueRef)
 {
     VariableType = variableType;
     LLVMPtrValueRef = llvmPtrValueRef;
 }
 public bool IsPrimitiveLLVMType(ClepsType clepsType)
 {
     return(GetPrimitiveLLVMTypeOrNull(clepsType) != null);
 }
Exemple #26
0
 public static ClepsType GetFunctionType(ClepsType functionReturnType, List<ClepsType> functionParameters)
 {
     return new ClepsType()
     {
         IsFunctionType = true,
         IsVoidType = false,
         IsBasicType = false,
         IsPointerType = false,
         FunctionReturnType = functionReturnType,
         FunctionParameters = functionParameters
     };
 }
Exemple #27
0
 public LLVMRegister(ClepsType variableType, LLVMValueRef llvmPtrValueRef)
 {
     VariableType    = variableType;
     LLVMPtrValueRef = llvmPtrValueRef;
 }
Exemple #28
0
 public void AddRawLLVMTypeMapping(ClepsType rawLLVMTypeMap)
 {
     Debug.Assert(RawLLVMTypeMap == null);
     RawLLVMTypeMap = rawLLVMTypeMap;
 }
Exemple #29
0
        public static ClepsType GetArrayAccessOnType(ClepsType arrayType, int accessDimensions)
        {
            Debug.Assert(arrayType.IsArrayType);
            Debug.Assert(arrayType.ArrayDimensions.Count >= accessDimensions);

            List<uint> newDims = arrayType.ArrayDimensions.Take(arrayType.ArrayDimensions.Count - accessDimensions).ToList();
            ClepsType ret = GetBasicType(arrayType.RawTypeName, newDims, arrayType.PtrIndirectionLevel);
            return ret;
        }
Exemple #30
0
 public bool RawLLVMTypeMappingExists(ClepsType rawLLVMTypeMap)
 {
     return(RawLLVMTypeMappingClasses.ContainsKey(rawLLVMTypeMap));
 }
Exemple #31
0
 public void AddLocalVariable(string variableName, ClepsType variableType, LLVMValueRef llvmValueRef)
 {
     Debug.Assert(LocalVariableBlocks.Count > 0);
     LocalVariableBlocks.Last().Add(variableName, new LLVMRegister(variableType, llvmValueRef));
 }
Exemple #32
0
        public static ClepsType GetPointerToBasicType(ClepsType basicType)
        {
            if(!basicType.IsBasicType)
            {
                throw new Exception("Expected basic type");
            }

            return GetBasicType(basicType.RawTypeName, basicType.ArrayDimensions, basicType.PtrIndirectionLevel + 1);
        }
 public bool IsPrimitiveLLVMType(ClepsType clepsType)
 {
     return GetPrimitiveLLVMTypeOrNull(clepsType) != null;
 }