Ejemplo n.º 1
0
        public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
        {
            var method          = context.CompilerContext.Assembly.ManifestModule.ResolveMethod((int)operation.RawParameter);
            var t               = method.ReflectedType;
            var size            = 0;
            var referenceFields = 0;

            foreach (var f in t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                size += f.FieldType.GetStorageBytes();
                if (f.FieldType.IsReferenceCounted())
                {
                    referenceFields++;
                }
            }
            //var label = context.CompilerContext.Assembly.ManifestModule.ResolveMethod(operation.RawParameter).GetLabel();


            var vtable = t.IsGenericType ? "0" : $"{t.Name.ToValidName()}_VTable";
            // var ctor = $"{t.Name}_x_ctor";
            var ctor = "0";

            if (t.Assembly == typeof(Func <object>).Assembly)
            {
                ctor = $"{t.Name}_x_ctor".ToValidName();
            }
            return($"{size}, {referenceFields}, {vtable}, {ctor}");
        }
Ejemplo n.º 2
0
        public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
        {
            var field = context.CompilerContext.Assembly.ManifestModule.ResolveField((int)operation.RawParameter);
            var pos   = context.CompilerContext.GetFieldPosition(field);

            return($"{pos}");
        }
Ejemplo n.º 3
0
        public override string Emit(CompilerMethodContext context, ILOperation operation)
        {
            bool normalCall = false;
            var  methodInfo = context.CompilerContext.Assembly.ManifestModule.ResolveMethod((int)operation.OriginalParameter);

            // for Func<>
            if (!methodInfo.IsVirtual || methodInfo.ReflectedType.Name.StartsWith("Func"))
            {
                normalCall = true;
            }

            if (normalCall)
            {
                return(base.Emit(context, operation));
            }

            var index         = methodInfo.ReflectedType.GetVirtualMethodIndex(methodInfo);
            var stackPosition = methodInfo.GetParameters().Select(x => x.ParameterType.GetStorageBytes()).Sum() + 1;

            if (index == -1)
            {
                throw new InvalidOperationException($"Virtual method not found: {methodInfo.Name}. Type: {methodInfo.ReflectedType.Name}");
            }
            return($"#callVirt {index}, {stackPosition}");
        }
Ejemplo n.º 4
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var last = operation.StackContent.Last();

            last.CheckCompatible(context.GetLocalVariableType(VarIndex));
            operation.StackContent.RemoveLast(1);
        }
Ejemplo n.º 5
0
        public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
        {
            List <string> refList    = new List <string>();
            bool          isInstance = !context.Method.IsStatic;

            for (int i = 0; i < context.Method.GetParameters().Length; i++)
            {
                if (context.Method.GetParameters()[i].ParameterType.IsReferenceCounted())
                {
                    refList.Add(context.GetParameterReferencePosition(i + (isInstance ? 1 : 0)).ToString());
                }
            }

            var body      = context.Method.GetMethodBody();
            var variables = body.LocalVariables;

            for (int i = 0; i < variables.Count; i++)
            {
                if (variables[i].LocalType.IsReferenceCounted())
                {
                    refList.Add(context.GetLocalVariableReferencePosition(i).ToString());
                }
            }

            return($"{context.GetLocalStackSize()}, [{string.Join(',', refList)}]");
        }
Ejemplo n.º 6
0
        public override void SetNextInstructions(CompilerMethodContext context, ILOperation operation, ILOperation nextOperation)
        {
            operation.NextInstructions.Add(nextOperation);
            var jmpInstruction = context.Lines.FirstOrDefault(l => l.Label == _label);

            operation.NextInstructions.Add(jmpInstruction);
        }
Ejemplo n.º 7
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            operation.StackContent.RemoveLast(1);
            var field = context.CompilerContext.Assembly.ManifestModule.ResolveField((int)operation.OriginalParameter);

            operation.StackContent.Add(field.FieldType);
        }
Ejemplo n.º 8
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            operation.StackContent.RemoveLast(1);
            var type  = context.CompilerContext.Assembly.ManifestModule.ResolveType((int)operation.OriginalParameter);
            var dummy = Array.CreateInstance(type, 0);

            operation.StackContent.Add(dummy.GetType());
        }
Ejemplo n.º 9
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var last = operation.StackContent.Last();

            // last.CheckCompatible(typeof(long));
            operation.StackContent.RemoveLast(1);
            operation.StackContent.Add(typeof(int));
        }
Ejemplo n.º 10
0
        public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
        {
            var    field   = context.Method.ReflectedType.Module.ResolveField((int)operation.RawParameter);
            var    address = $"{field.ReflectedType.Name.ToValidName()}_field_{field.Name.ToValidName()}";
            string isRef   = field.FieldType.IsReferenceCounted() ? "1" : "0";

            return($"{address}, {isRef}");
        }
Ejemplo n.º 11
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var field = context.Method.ReflectedType.Module.ResolveField((int)operation.OriginalParameter);
            var last  = operation.StackContent.Last();

            last.CheckCompatible(field.FieldType);
            operation.StackContent.RemoveLast(1);
        }
Ejemplo n.º 12
0
 public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
 {
     if (!context.CompilerContext.StringValues.ContainsKey((int)operation.RawParameter))
     {
         context.CompilerContext.StringValues.Add((int)operation.RawParameter, context.CompilerContext.Assembly.ManifestModule.ResolveString((int)operation.RawParameter));
     }
     return($"string_{operation.RawParameter}");
 }
Ejemplo n.º 13
0
        public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
        {
            var memLabel = context.CompilerContext.GetInitValueLabel(string.Join(',', _mem));

            // var ctor = $"{context.TypeContext.Type.Name}_x_ctor";
            var ctor = 0;

            return($"{_size}, {_referenceFields}, {_vtable}, {memLabel}, {ctor}");
        }
Ejemplo n.º 14
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var elemType = operation.StackContent.Last().GetElementType();

            operation.RawParameter = elemType;

            operation.StackContent.RemoveLast(1);
            operation.StackContent.Add(typeof(int));
        }
Ejemplo n.º 15
0
        public override object ConvertParameter(CompilerMethodContext context, ILOperation operation)
        {
            var body      = context.Method.GetMethodBody();
            var variables = body.LocalVariables;
            var index     = context.GetLocalVariableReferencePosition((int)operation.RawParameter);
            var isRef     = variables[(int)operation.RawParameter].LocalType.IsReferenceCounted() ? "1" : "0";

            return($"{index}, {isRef}");
        }
Ejemplo n.º 16
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var method       = context.CompilerContext.Assembly.ManifestModule.ResolveMethod((int)operation.OriginalParameter) as MethodBase;
            var parameterNum = method.GetParameters().Length;

            operation.StackContent.RemoveLast(parameterNum);

            operation.StackContent.Add(typeof(object));
        }
Ejemplo n.º 17
0
 public override string Emit(CompilerMethodContext context, ILOperation operation)
 {
     if (operation.PreviousInstructions.First().StackContent.Last().GetStorageBytes() == 2)
     {
         return(base.Emit(context, operation));
     }
     else
     {
         return("; conv");
     }
 }
Ejemplo n.º 18
0
 public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
 {
     if (operation.NextInstructions[0].OpCode == ILOpCode.Conv_i8 || operation.NextInstructions[0].OpCode == ILOpCode.Conv_u8)
     {
         operation.StackContent.Add(typeof(long));
     }
     else
     {
         operation.StackContent.Add(typeof(int));
     }
 }
Ejemplo n.º 19
0
        public override void SetNextInstructions(CompilerMethodContext context, ILOperation operation, ILOperation nextOperation)
        {
            if (_jumpType != JumpType.UnConditional)
            {
                operation.NextInstructions.Add(nextOperation);
            }
            var label          = operation.RawParameter.ToString();
            var jmpInstruction = context.Lines.FirstOrDefault(l => l.Label == label);

            operation.NextInstructions.Add(jmpInstruction);
        }
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var last = operation.StackContent.Last();

            if (last != typeof(int) && last != typeof(uint) && last != typeof(long) && last != typeof(ulong))
            {
                throw new InvalidOperationException("Unsupported type in arithmetic operation.");
            }

            operation.StackContent.RemoveLast(1);
            operation.StackContent.Add(last);
        }
Ejemplo n.º 21
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            switch (_jumpType)
            {
            case JumpType.Conditional:
                operation.StackContent.RemoveLast(1);
                break;

            case JumpType.Compare:
                operation.StackContent.RemoveLast(2);
                break;
            }
        }
Ejemplo n.º 22
0
        public override void SetNextInstructions(CompilerMethodContext context, ILOperation operation, ILOperation nextOperation)
        {
            List <int> parameters = (List <int>)operation.RawParameter;

            foreach (var parameter in parameters)
            {
                var target         = parameter + operation.Position + parameters.Count * 4 + 5;
                var label          = $"{context.Method.GetLabel()}_{target}";
                var jmpInstruction = context.Lines.FirstOrDefault(l => l.Label == label);

                operation.NextInstructions.Add(jmpInstruction);
            }

            base.SetNextInstructions(context, operation, nextOperation);
        }
Ejemplo n.º 23
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var method       = context.CompilerContext.Assembly.ManifestModule.ResolveMethod((int)operation.OriginalParameter) as MethodBase;
            var parameterNum = method.GetParameters().Length + (method.IsStatic ? 0 : 1);

            operation.StackContent.RemoveLast(parameterNum);

            if (method is MethodInfo)
            {
                if (((MethodInfo)method).ReturnType != typeof(void))
                {
                    operation.StackContent.Add(((MethodInfo)method).ReturnType);
                }
            }
        }
Ejemplo n.º 24
0
        public override string Emit(CompilerMethodContext context, ILOperation operation)
        {
            var arrayType = (Type)operation.RawParameter;

            string command;

            if (arrayType.GetStorageBytes() == 2)
            {
                command = "#ldlen16";
            }
            else
            {
                command = "#ldlen";
            }
            return($"{command}");
        }
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var last  = operation.StackContent.Last();
            var last2 = operation.StackContent.Last(1);

            if (last != typeof(int) && last != typeof(uint) && last != typeof(long) && last != typeof(ulong))
            {
                throw new InvalidOperationException("Unsupported type in arithmetic operation.");
            }
            if (last2.GetStorageBytes() != last.GetStorageBytes())
            {
                throw new InvalidOperationException("2 types in stack must be equal.");
            }

            operation.StackContent.RemoveLast(2);
            operation.StackContent.Add(typeof(bool));
        }
Ejemplo n.º 26
0
        public override void SetStackContent(CompilerMethodContext context, ILOperation operation)
        {
            var method = context.Method as MethodInfo;

            if (method != null && method.ReturnType == typeof(void) && operation.StackContent.Count != 0)
            {
                throw new InvalidOperationException($"Stack should be empty. {context.Method.Name}");
            }

            if (method != null && method.ReturnType != typeof(void))
            {
                if (operation.StackContent.Count != 1)
                {
                    throw new InvalidOperationException($"Stack should contain 1 element. {context.Method.Name}");
                }

                operation.StackContent[0].CheckCompatible(method.ReturnType);
            }
        }
Ejemplo n.º 27
0
        public virtual string Emit(CompilerMethodContext context, ILOperation operation)
        {
            var command = Command;

            if (Is16BitSupported)
            {
                if (Is16Bit(context, operation))
                {
                    command += "16";
                }
                else
                {
                    command += "8";
                }
            }
            if (operation.RawParameter == null)
            {
                return(command);
            }
            return($"{command} {operation.RawParameter}");
        }
Ejemplo n.º 28
0
        public override string Emit(CompilerMethodContext context, ILOperation operation)
        {
            var arrayType = (Type)operation.RawParameter;

            string command;

            if (arrayType == typeof(string))
            {
                command = "#ldelem16";
            }
            else if (!arrayType.IsValueType)
            {
                command = "#ldelemRef";
            }
            else if (arrayType.GetStorageBytes() == 2)
            {
                command = "#ldelem16";
            }
            else
            {
                command = "#ldelem";
            }
            return($"{command}");
        }
Ejemplo n.º 29
0
        public override string Emit(CompilerMethodContext context, ILOperation operation)
        {
            var arrayType = operation.StackContent.Last().GetElementType();

            string command;

            if (arrayType == typeof(string))
            {
                command = "#newArr16";
            }
            else if (!arrayType.IsValueType)
            {
                command = "#newArrRef";
            }
            else if (arrayType.GetStorageBytes() == 2)
            {
                command = "#newArr16";
            }
            else
            {
                command = "#newArr";
            }
            return($"{command}");
        }
 public override bool Is16Bit(CompilerMethodContext context, ILOperation operation)
 {
     return(operation.PreviousInstructions[0].StackContent.Last().GetStorageBytes() == 2);
 }