Exemplo n.º 1
0
        private LLVMValueRef DeclareLocal(IMethod method)
        {
            var funType = GetFunctionPrototype(method);
            var result  = LLVM.AddFunction(Module, Mangler.Mangle(method, true), funType);

            result.SetLinkage(GetLinkageForLocal(method));
            if (!method.IsStatic)
            {
                LLVM.AddAttributeAtIndex(result, (LLVMAttributeIndex)1, CreateEnumAttribute("nonnull"));
            }
            return(result);
        }
Exemplo n.º 2
0
        public LLVMValueRef DefineStaticField(IField field)
        {
            LLVMValueRef result;

            if (fieldDecls.TryGetValue(field, out result))
            {
                return(result);
            }

            if (!field.IsStatic)
            {
                throw new InvalidOperationException($"Cannot define non-static field '{field.FullName}' as a global.");
            }

            var type = ImportType(field.FieldType);
            var name = Mangler.Mangle(field, true);

            result = LLVM.AddGlobal(Module, type, name);
            result.SetInitializer(LLVM.ConstNull(type));
            result.SetLinkage(GetLinkageForLocal(field));
            fieldDecls[field] = result;
            return(result);
        }
Exemplo n.º 3
0
        private LLVMTypeRef ImportTypeImpl(IType type)
        {
            var intSpec = type.GetIntegerSpecOrNull();

            if (intSpec != null)
            {
                return(LLVM.IntTypeInContext(Context, (uint)intSpec.Size));
            }
            else if (type == TypeSystem.Float32)
            {
                return(LLVM.FloatTypeInContext(Context));
            }
            else if (type == TypeSystem.Float64)
            {
                return(LLVM.DoubleTypeInContext(Context));
            }
            else if (type == TypeSystem.NaturalInt || type == TypeSystem.NaturalUInt)
            {
                return(LLVM.PointerType(LLVM.Int8TypeInContext(Context), 0));
            }
            else if (type is PointerType)
            {
                var elemType = ((PointerType)type).ElementType;
                if (elemType == TypeSystem.Void)
                {
                    return(LLVM.PointerType(LLVM.Int8TypeInContext(Context), 0));
                }
                else
                {
                    return(LLVM.PointerType(ImportType(elemType), 0));
                }
            }
            else if (type == TypeSystem.Void)
            {
                return(LLVM.VoidTypeInContext(Context));
            }
            else
            {
                var result = LLVM.StructCreateNamed(Context, Mangler.Mangle(type, true));
                importCache[type] = result;
                var fieldTypes     = new List <LLVMTypeRef>();
                var fieldNumbering = new Dictionary <IField, int>();
                var baseNumbering  = new Dictionary <IType, int>();
                foreach (var baseType in type.BaseTypes)
                {
                    var importedBase = ImportType(baseType);
                    if (importedBase.TypeKind != LLVMTypeKind.LLVMStructTypeKind ||
                        importedBase.CountStructElementTypes() > 0)
                    {
                        // Do not include empty base types.
                        baseNumbering[baseType] = fieldTypes.Count;
                        fieldTypes.Add(importedBase);
                    }
                }
                foreach (var field in type.Fields.Where(f => !f.IsStatic))
                {
                    fieldNumbering[field] = fieldTypes.Count;
                    fieldTypes.Add(ImportType(field.FieldType));
                }
                fieldIndices[type] = fieldNumbering;
                baseIndices[type]  = baseNumbering;
                LLVM.StructSetBody(result, fieldTypes.ToArray(), false);
                return(result);
            }
        }