Beispiel #1
0
        private HLLocation ProcessConditionalExpression(IConditional pExpression)
        {
            HLLocation         locationCondition = ProcessExpression(pExpression.Condition);
            HLInstructionBlock blockParent       = mCurrentBlock;

            HLLocation locationResult = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            HLInstructionBlock blockTrueStart = CreateBlock(CreateLabel());

            mCurrentBlock = blockTrueStart;
            HLLocation locationTrue = ProcessExpression(pExpression.ResultIfTrue);

            mCurrentBlock.EmitAssignment(locationResult, locationTrue);
            HLInstructionBlock blockTrueEnd = mCurrentBlock;

            HLInstructionBlock blockFalseStart = CreateBlock(CreateLabel());

            mCurrentBlock = blockFalseStart;
            HLLocation locationFalse = ProcessExpression(pExpression.ResultIfFalse);

            mCurrentBlock.EmitAssignment(locationResult, locationFalse);
            HLInstructionBlock blockFalseEnd = mCurrentBlock;

            blockParent.EmitBranch(locationCondition, blockTrueStart.StartLabel, blockFalseStart.StartLabel);

            mCurrentBlock = CreateBlock(CreateLabel());
            blockTrueEnd.Terminate(mCurrentBlock.StartLabel);
            blockFalseEnd.Terminate(mCurrentBlock.StartLabel);

            return(locationResult);
        }
Beispiel #2
0
 private HLLocation ProcessBoundExpression(IBoundExpression pExpression)
 {
     if (pExpression.Definition is ILocalDefinition)
     {
         ILocalDefinition definition = pExpression.Definition as ILocalDefinition;
         HLLocation       location   = HLLocalLocation.Create(HLDomain.GetLocal(definition));
         if (pExpression.Type.ResolvedType.TypeCode == PrimitiveTypeCode.Reference)
         {
             location = location.AddressOf();
         }
         return(location);
     }
     else if (pExpression.Definition is IParameterDefinition)
     {
         return(HLParameterLocation.Create(HLDomain.GetParameter(pExpression.Definition as IParameterDefinition)));
     }
     else if (pExpression.Definition is IFieldDefinition || pExpression.Definition is IFieldReference)
     {
         IFieldDefinition definition         = pExpression.Definition is IFieldDefinition ? (IFieldDefinition)pExpression.Definition : ((IFieldReference)pExpression.Definition).ResolvedField;
         HLType           typeFieldContainer = HLDomain.GetOrCreateType(definition.Container);
         HLField          field = HLDomain.GetField(definition);
         if (field.IsStatic)
         {
             return(HLStaticFieldLocation.Create(field));
         }
         HLLocation instance = ProcessExpression(pExpression.Instance);
         return(HLFieldLocation.Create(instance, field));
     }
     throw new NotSupportedException();
 }
Beispiel #3
0
        internal void MapVirtualMethods()
        {
            if (BaseType != null)
            {
                BaseType.MapVirtualMethods();
                foreach (KeyValuePair <HLMethod, HLMethod> kv in BaseType.VirtualMap)
                {
                    VirtualMap[kv.Key] = kv.Value;
                }
            }
            foreach (HLMethod method in VirtualMap.Keys.ToList())
            {
                IMethodDefinition definition = TypeHelper.GetMethod(Definition, method.Definition, true);
                if (definition != Dummy.MethodDefinition)
                {
                    VirtualMap[method] = HLDomain.GetOrCreateMethod(definition);
                }
            }
            List <HLMethod> methodsVirtuals = Methods.FindAll(m => m.IsVirtual);

            foreach (HLMethod method in methodsVirtuals)
            {
                IMethodDefinition definition = TypeHelper.GetMethod(Definition, method.Definition, true);
                if (definition == Dummy.MethodDefinition)
                {
                    VirtualMap[method] = method;
                }
                else
                {
                    VirtualMap[method] = HLDomain.GetOrCreateMethod(definition);
                }
            }
        }
Beispiel #4
0
        private static HLType CreateType(ITypeDefinition pDefinition)
        {
            HLType type = new HLType();

            type.Definition        = pDefinition;
            type.Name              = pDefinition.ToString(); // TODO: Don't really like this, should use TypeHelper perhaps?
            type.Signature         = HLDomain.GetTypeSignature(pDefinition);
            sTypes[type.Signature] = type;

            ITypeReference referenceBase = pDefinition.BaseClasses.FirstOrDefault();

            if (referenceBase != null)
            {
                type.BaseType = GetOrCreateType(referenceBase);
                type.BaseType.MemberFields.ForEach(f => type.Fields.Add(f));
            }

            foreach (IFieldDefinition fieldDefinition in pDefinition.Fields.OrderBy(d => d.SequenceNumber))
            {
                CreateField(type, fieldDefinition);
            }

            IMethodDefinition definitionStaticConstructor = pDefinition.Methods.FirstOrDefault(d => d.IsStaticConstructor);

            if (definitionStaticConstructor != null)
            {
                type.StaticConstructor = GetOrCreateMethod(definitionStaticConstructor);
            }
            return(type);
        }
Beispiel #5
0
 private HLLocation ProcessTargetExpression(ITargetExpression pExpression)
 {
     if (pExpression.Definition is ILocalDefinition)
     {
         return(HLLocalLocation.Create(HLDomain.GetLocal(pExpression.Definition as ILocalDefinition)));
     }
     else if (pExpression.Definition is IParameterDefinition)
     {
         HLParameter parameter = HLDomain.GetParameter(pExpression.Definition as IParameterDefinition);
         parameter.RequiresAddressing = true;
         return(HLParameterLocation.Create(parameter));
     }
     else if (pExpression.Definition is IFieldDefinition || pExpression.Definition is IFieldReference)
     {
         IFieldDefinition definition         = pExpression.Definition is IFieldDefinition ? (IFieldDefinition)pExpression.Definition : ((IFieldReference)pExpression.Definition).ResolvedField;
         HLType           typeFieldContainer = HLDomain.GetOrCreateType(definition.Container);
         HLField          field = HLDomain.GetField(definition);
         if (field.IsStatic)
         {
             return(HLStaticFieldLocation.Create(field));
         }
         HLLocation instance = ProcessExpression(pExpression.Instance);
         return(HLFieldLocation.Create(instance, field));
     }
     else if (pExpression.Definition is IAddressDereference)
     {
         IAddressDereference definition      = pExpression.Definition as IAddressDereference;
         HLLocation          locationAddress = ProcessExpression(definition.Address);
         return(HLIndirectAddressLocation.Create(locationAddress, HLDomain.GetOrCreateType(pExpression.Type)));
     }
     throw new NotSupportedException();
 }
Beispiel #6
0
        private HLLocation ProcessArrayIndexerExpression(IArrayIndexer pExpression)
        {
            if (pExpression.Indices.Count() != 1)
            {
                throw new NotSupportedException();
            }

            HLLocation locationInstance = ProcessExpression(pExpression.IndexedObject);
            HLLocation locationIndex    = ProcessExpression(pExpression.Indices.First());
            HLType     typeElement      = HLDomain.GetOrCreateType(pExpression.Type);

            return(HLArrayElementLocation.Create(locationInstance, locationIndex, typeElement));
        }
Beispiel #7
0
        private HLLocation ProcessCreateDelegateInstanceExpression(ICreateDelegateInstance pExpression)
        {
            HLLocation locationInstance = null;

            if (pExpression.Instance != null)
            {
                locationInstance = ProcessExpression(pExpression.Instance);
            }
            HLMethod   methodCalled     = HLDomain.GetOrCreateMethod(pExpression.MethodToCallViaDelegate);
            HLLocation locationDelegate = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitNewDelegate(locationDelegate.Type, locationDelegate.AddressOf(), locationInstance, methodCalled, pExpression.IsVirtualDelegate);
            return(locationDelegate);
        }
Beispiel #8
0
        private void ProcessLocalDeclarationStatement(ILocalDeclarationStatement pStatement)
        {
            if (mCurrentBlock.Terminated)
            {
                mCurrentBlock = CreateBlock(CreateLabel());
            }

            if (pStatement.InitialValue == null)
            {
                return;
            }
            HLLocation locationLocalVariable = HLLocalLocation.Create(HLDomain.GetLocal(pStatement.LocalVariable));
            HLLocation locationInitialValue  = ProcessExpression(pStatement.InitialValue);

            mCurrentBlock.EmitAssignment(locationLocalVariable, locationInitialValue);
        }
Beispiel #9
0
 private HLLocation ProcessAddressableExpression(IAddressableExpression pExpression)
 {
     if (pExpression.Definition is ILocalDefinition)
     {
         return(HLLocalLocation.Create(HLDomain.GetLocal(pExpression.Definition as ILocalDefinition)));
     }
     else if (pExpression.Definition is IParameterDefinition)
     {
         return(HLParameterLocation.Create(HLDomain.GetParameter(pExpression.Definition as IParameterDefinition)));
     }
     else if (pExpression.Definition is IFieldDefinition || pExpression.Definition is IFieldReference)
     {
         IFieldDefinition definition         = pExpression.Definition is IFieldDefinition ? (IFieldDefinition)pExpression.Definition : ((IFieldReference)pExpression.Definition).ResolvedField;
         HLType           typeFieldContainer = HLDomain.GetOrCreateType(definition.Container);
         HLField          field = HLDomain.GetField(definition);
         if (field.IsStatic)
         {
             return(HLStaticFieldLocation.Create(field));
         }
         HLLocation instance = ProcessExpression(pExpression.Instance);
         return(HLFieldLocation.Create(instance, field));
     }
     else if (pExpression.Definition is IArrayIndexer)
     {
         IArrayIndexer definition = (IArrayIndexer)pExpression.Definition;
         if (definition.Indices.Count() != 1)
         {
             throw new NotSupportedException();
         }
         HLLocation locationInstance = ProcessExpression(pExpression.Instance);
         HLLocation locationIndex    = ProcessExpression(definition.Indices.First());
         HLType     typeElement      = HLDomain.GetOrCreateType(pExpression.Type);
         return(HLArrayElementLocation.Create(locationInstance, locationIndex, typeElement));
     }
     else if (pExpression.Definition is IDefaultValue)
     {
         HLType     typeDefaultValue     = HLDomain.GetOrCreateType(((IDefaultValue)pExpression.Definition).DefaultValueType);
         HLLocation locationDefaultValue = HLTemporaryLocation.Create(CreateTemporary(typeDefaultValue));
         mCurrentBlock.EmitAssignment(locationDefaultValue, HLDefaultLocation.Create(typeDefaultValue));
         return(locationDefaultValue);
     }
     throw new NotSupportedException();
 }
Beispiel #10
0
        private static HLLocal CreateLocal(ILocalDefinition pDefinition)
        {
            HLLocal local = new HLLocal();

            //local.Definition = pDefinition;
            local.Name               = pDefinition.Name.Value;
            local.Signature          = HLDomain.GetLocalSignature(pDefinition);
            sLocals[local.Signature] = local;

            //local.Container = GetOrCreateMethod(pDefinition.MethodDefinition);
            ITypeReference type = pDefinition.Type;

            if (pDefinition.IsReference)
            {
                type = MutableModelHelper.GetManagedPointerTypeReference(type, Host.InternFactory, type);
            }
            local.IsReference = pDefinition.IsReference;
            local.Type        = GetOrCreateType(type);
            return(local);
        }
Beispiel #11
0
        private static HLField CreateField(HLType pContainer, IFieldDefinition pDefinition)
        {
            HLField field = new HLField();

            //field.Definition = pDefinition;
            field.Name               = pDefinition.Name.Value;
            field.Signature          = HLDomain.GetFieldSignature(pDefinition);
            sFields[field.Signature] = field;

            field.Container = pContainer;
            pContainer.Fields.Add(field);

            field.IsStatic = pDefinition.IsStatic;
            field.IsCompileTimeConstant = pDefinition.IsCompileTimeConstant;
            if (field.IsCompileTimeConstant)
            {
                field.CompileTimeConstant = pDefinition.Constant.Value;
            }
            field.Type = GetOrCreateType(pDefinition.Type);
            return(field);
        }
Beispiel #12
0
        private static HLParameter CreateParameter(IParameterDefinition pDefinition)
        {
            HLParameter parameter = new HLParameter();

            //parameter.Definition = pDefinition;
            parameter.Name      = pDefinition.Name.Value;
            parameter.Signature = HLDomain.GetParameterSignature(pDefinition);
            sParameters[parameter.Signature] = parameter;

            //if (pDefinition.ContainingSignature is IMethodDefinition) parameter.MethodContainer = GetOrCreateMethod(pDefinition.ContainingSignature as IMethodDefinition);
            //else throw new NotSupportedException();
            ITypeReference type = pDefinition.Type;

            if (pDefinition.IsByReference)
            {
                type = MutableModelHelper.GetManagedPointerTypeReference(type, Host.InternFactory, type);
            }
            parameter.IsReference = pDefinition.IsByReference;
            parameter.Type        = GetOrCreateType(type);
            return(parameter);
        }
Beispiel #13
0
        private HLLocation ProcessCreateArrayExpression(ICreateArray pExpression)
        {
            if (pExpression.Rank != 1 || pExpression.Sizes.Count() != 1 || pExpression.LowerBounds.Count() > 0)
            {
                throw new NotSupportedException();
            }

            HLType     typeElement      = HLDomain.GetOrCreateType(pExpression.ElementType);
            HLLocation locationInstance = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));
            HLLocation locationSize     = ProcessExpression(pExpression.Sizes.First());

            mCurrentBlock.EmitNewArray(locationInstance.AddressOf(), locationSize, locationInstance.Type, typeElement);

            IExpression[] initializers = pExpression.Initializers.ToArray();
            for (int indexInitializer = 0; indexInitializer < initializers.Length; ++indexInitializer)
            {
                HLLocation locationInitializer  = ProcessExpression(initializers[indexInitializer]);
                HLLocation locationArrayElement = HLArrayElementLocation.Create(locationInstance, HLInt32LiteralLocation.Create(indexInitializer), typeElement);
                mCurrentBlock.EmitAssignment(locationArrayElement, locationInitializer);
            }

            return(locationInstance);
        }
Beispiel #14
0
        private HLLocation ProcessCreateObjectInstanceExpression(ICreateObjectInstance pExpression)
        {
            HLMethod          methodCalled        = HLDomain.GetOrCreateMethod(pExpression.MethodToCall);
            HLLocation        locationThis        = HLTemporaryLocation.Create(CreateTemporary(methodCalled.Container));
            List <HLLocation> locationsParameters = new List <HLLocation>();

            if (methodCalled.Container == HLDomain.SystemString)
            {
                locationsParameters.Add(locationThis.AddressOf());
            }
            else
            {
                mCurrentBlock.EmitNewObject(methodCalled.Container, locationThis.AddressOf());
                locationsParameters.Add(locationThis);
            }

            foreach (IExpression argument in pExpression.Arguments)
            {
                locationsParameters.Add(ProcessExpression(argument));
            }

            mCurrentBlock.EmitCall(methodCalled, false, null, locationsParameters);
            return(locationThis);
        }
Beispiel #15
0
        private HLLocation ProcessMethodCallExpression(IMethodCall pExpression)
        {
            List <HLLocation> locationsParameters = new List <HLLocation>();

            if (pExpression.ThisArgument.Type.TypeCode != PrimitiveTypeCode.Invalid)
            {
                locationsParameters.Add(ProcessExpression(pExpression.ThisArgument));
            }
            else if (pExpression.MethodToCall.IsStatic)
            {
                HLType typeContainer = null;
                if (pExpression.MethodToCall.ContainingType.ResolvedType.IsValueType)
                {
                    typeContainer = HLDomain.GetOrCreateType(MutableModelHelper.GetManagedPointerTypeReference(pExpression.MethodToCall.ContainingType, HLDomain.Host.InternFactory, pExpression.MethodToCall.ContainingType));
                }
                else
                {
                    typeContainer = HLDomain.GetOrCreateType(pExpression.MethodToCall.ContainingType);
                }
                locationsParameters.Add(HLNullLocation.Create(typeContainer));
            }
            foreach (IExpression argument in pExpression.Arguments)
            {
                locationsParameters.Add(ProcessExpression(argument));
            }

            HLLocation locationReturn = null;
            HLMethod   methodCalled   = HLDomain.GetOrCreateMethod(pExpression.MethodToCall);

            if (methodCalled.ReturnType.Definition.TypeCode != PrimitiveTypeCode.Void)
            {
                locationReturn = HLTemporaryLocation.Create(CreateTemporary(methodCalled.ReturnType));
            }
            mCurrentBlock.EmitCall(methodCalled, pExpression.IsVirtualCall, locationReturn, locationsParameters);
            return(locationReturn);
        }
Beispiel #16
0
        private HLLocation ProcessOnesComplementExpression(IOnesComplement pExpression)
        {
            HLLocation locationOperand   = ProcessExpression(pExpression.Operand);
            HLLocation locationTemporary = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitBitwiseNot(locationTemporary, locationOperand);
            return(locationTemporary);
        }
Beispiel #17
0
 private HLLocation ProcessTypeOfExpression(ITypeOf pExpression)
 {
     return(HLTypeOfLocation.Create(HLDomain.GetOrCreateType(pExpression.TypeToGet)));
 }
Beispiel #18
0
        private HLLocation ProcessUnaryNegationExpression(IUnaryNegation pExpression)
        {
            HLLocation locationOperand   = ProcessExpression(pExpression.Operand);
            HLLocation locationTemporary = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitBitwiseNegate(locationTemporary, locationOperand);
            return(locationTemporary);
        }
Beispiel #19
0
        private HLLocation ProcessAddressDereferenceExpression(IAddressDereference pExpression)
        {
            HLLocation locationAddress = ProcessExpression(pExpression.Address);

            return(HLIndirectAddressLocation.Create(locationAddress, HLDomain.GetOrCreateType(pExpression.Type)));
        }
Beispiel #20
0
 private HLLocation ProcessCompileTimeConstantExpression(ICompileTimeConstant pExpression)
 {
     if (pExpression.Value == null)
     {
         return(HLNullLocation.Create(HLDomain.GetOrCreateType(pExpression.Type)));
     }
     if (pExpression.Type.ResolvedType.IsEnum)
     {
         return(HLEnumLiteralLocation.Create(HLDomain.GetOrCreateType(pExpression.Type), pExpression.Value.ToString()));
     }
     if (pExpression.Value is bool)
     {
         return(HLBooleanLiteralLocation.Create((bool)pExpression.Value));
     }
     if (pExpression.Value is sbyte)
     {
         return(HLInt8LiteralLocation.Create((sbyte)pExpression.Value));
     }
     if (pExpression.Value is byte)
     {
         return(HLUInt8LiteralLocation.Create((byte)pExpression.Value));
     }
     if (pExpression.Value is char)
     {
         return(HLCharLiteralLocation.Create((char)pExpression.Value));
     }
     if (pExpression.Value is short)
     {
         return(HLInt16LiteralLocation.Create((short)pExpression.Value));
     }
     if (pExpression.Value is ushort)
     {
         return(HLUInt16LiteralLocation.Create((ushort)pExpression.Value));
     }
     if (pExpression.Value is float)
     {
         return(HLFloat32LiteralLocation.Create((float)pExpression.Value));
     }
     if (pExpression.Value is int)
     {
         return(HLInt32LiteralLocation.Create((int)pExpression.Value));
     }
     if (pExpression.Value is uint)
     {
         return(HLUInt32LiteralLocation.Create((uint)pExpression.Value));
     }
     if (pExpression.Value is double)
     {
         return(HLFloat64LiteralLocation.Create((double)pExpression.Value));
     }
     if (pExpression.Value is long)
     {
         return(HLInt64LiteralLocation.Create((long)pExpression.Value));
     }
     if (pExpression.Value is ulong)
     {
         return(HLUInt64LiteralLocation.Create((ulong)pExpression.Value));
     }
     if (pExpression.Value is string)
     {
         return(HLStringLiteralLocation.Create((string)pExpression.Value));
     }
     throw new NotSupportedException();
 }
Beispiel #21
0
        private HLLocation ProcessNotEqualityExpression(INotEquality pExpression)
        {
            HLLocation locationLeftOperand  = ProcessExpression(pExpression.LeftOperand);
            HLLocation locationRightOperand = ProcessExpression(pExpression.RightOperand);
            HLLocation locationTemporary    = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitCompare(HLCompareType.NotEqual, locationTemporary, locationLeftOperand, locationRightOperand);
            return(pExpression.ResultIsUnmodifiedLeftOperand ? locationLeftOperand : locationTemporary);
        }
Beispiel #22
0
        private HLLocation ProcessMultiplicationExpression(IMultiplication pExpression)
        {
            HLLocation locationLeftOperand  = ProcessExpression(pExpression.LeftOperand);
            HLLocation locationRightOperand = ProcessExpression(pExpression.RightOperand);
            HLLocation locationTemporary    = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitMultiply(locationTemporary, locationLeftOperand, locationRightOperand);
            return(pExpression.ResultIsUnmodifiedLeftOperand ? locationLeftOperand : locationTemporary);
        }
Beispiel #23
0
        private HLLocation ProcessLogicalNotExpression(ILogicalNot pExpression)
        {
            HLLocation locationOperand   = ProcessExpression(pExpression.Operand);
            HLLocation locationTemporary = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitBitwiseXor(locationTemporary, locationOperand, HLBooleanLiteralLocation.Create(true));
            return(locationTemporary);
        }
Beispiel #24
0
        private HLLocation ProcessExclusiveOrExpression(IExclusiveOr pExpression)
        {
            HLLocation locationLeftOperand  = ProcessExpression(pExpression.LeftOperand);
            HLLocation locationRightOperand = ProcessExpression(pExpression.RightOperand);
            HLLocation locationTemporary    = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitBitwiseXor(locationTemporary, locationLeftOperand, locationRightOperand);
            return(pExpression.ResultIsUnmodifiedLeftOperand ? locationLeftOperand : locationTemporary);
        }
Beispiel #25
0
 private HLLocation ProcessDefaultValueExpression(IDefaultValue pExpression)
 {
     return(HLDefaultLocation.Create(HLDomain.GetOrCreateType(pExpression.DefaultValueType)));
 }
Beispiel #26
0
        private HLLocation ProcessConversionExpression(IConversion pExpression)
        {
            HLLocation locationSource    = ProcessExpression(pExpression.ValueToConvert);
            HLLocation locationTemporary = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.TypeAfterConversion)));

            mCurrentBlock.EmitAssignment(locationTemporary, locationSource);
            return(locationTemporary);
        }
Beispiel #27
0
 private HLLocation ProcessSizeOfExpression(ISizeOf pExpression)
 {
     return(HLSizeOfLocation.Create(HLDomain.GetOrCreateType(pExpression.TypeToSize)));
 }