Beispiel #1
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            StringBuilder ASMResult = new StringBuilder();

            ASMResult.AppendLine("; Method Table - " + CurrentTypeName);
            ASMResult.AppendLine("GLOBAL " + CurrentTypeId + "_MethodTable:data");
            ASMResult.AppendLine(CurrentTypeId + "_MethodTable:");

            foreach (Tuple <string, string> aMethodInfo in AllMethodInfos)
            {
                string MethodID      = aMethodInfo.Item1;
                string MethodIDValue = aMethodInfo.Item2;

                foreach (Tuple <string, int> anEntryFieldInfo in TableEntryFieldInfos)
                {
                    string allocString = ASMUtilities.GetAllocStringForSize(anEntryFieldInfo.Item2);
                    switch (anEntryFieldInfo.Item1)
                    {
                    case "MethodID":
                        ASMResult.AppendLine(allocString + " " + MethodIDValue);
                        break;

                    case "MethodPtr":
                        ASMResult.AppendLine(allocString + " " + MethodID);
                        break;
                    }
                }
            }

            ASMResult.AppendLine("; Method Table End - " + CurrentTypeName);

            return(ASMResult.ToString());
        }
Beispiel #2
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            StringBuilder LiteralASM = new StringBuilder();

            //This is UTF-16 (Unicode)/ASCII text
            LiteralASM.AppendLine(string.Format("GLOBAL {0}:data", Id));
            LiteralASM.AppendLine(string.Format("{0}:", Id));
            //Put in type info as FOS_System.String type
            LiteralASM.AppendLine(string.Format("dd {0}", StringTypeId));
            //Put in string length bytes
            LiteralASM.Append("db ");
            for (int i = 0; i < 3; i++)
            {
                LiteralASM.Append(LengthBytes[i]);
                LiteralASM.Append(", ");
            }
            LiteralASM.Append(LengthBytes[3]);

            if (Characters.Length > 0)
            {
                //Put in string characters (as words)
                LiteralASM.Append("\ndw ");
                for (int i = 0; i < (Characters.Length - 1); i++)
                {
                    LiteralASM.Append((uint)Characters[i]);
                    LiteralASM.Append(", ");
                }
                LiteralASM.Append((uint)Characters.Last());
            }

            LiteralASM.AppendLine();

            return(LiteralASM.ToString());
        }
Beispiel #3
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            StringBuilder ASMResult = new StringBuilder();

            ASMResult.AppendLine(".globl " + TypeId);
            ASMResult.AppendLine(".align 2");
            ASMResult.AppendLine(TypeId + ":");

            foreach (Tuple <string, Types.TypeInfo> aFieldInfo in FieldInformation)
            {
                string allocStr = ASMUtilities.GetAllocStringForSize(
                    aFieldInfo.Item2.IsValueType ? aFieldInfo.Item2.SizeOnHeapInBytes : aFieldInfo.Item2.SizeOnStackInBytes);
                switch (aFieldInfo.Item1)
                {
                case "Size":
                    ASMResult.AppendLine(allocStr + " " + SizeVal);
                    break;

                case "Id":
                    ASMResult.AppendLine(allocStr + " " + IdVal);
                    break;

                case "StackSize":
                    ASMResult.AppendLine(allocStr + " " + StackSizeVal);
                    break;

                case "IsValueType":
                    ASMResult.AppendLine(allocStr + " " + IsValueTypeVal);
                    break;

                case "MethodTablePtr":
                    ASMResult.AppendLine(allocStr + " " + MethodTablePointer);
                    break;

                case "IsPointer":
                    ASMResult.AppendLine(allocStr + " " + IsPointerTypeVal);
                    break;

                case "TheBaseType":
                    ASMResult.AppendLine(allocStr + " " + BaseTypeIdVal);
                    break;

                case "FieldTablePtr":
                    ASMResult.AppendLine(allocStr + " " + FieldTablePointer);
                    break;

                case "Signature":
                    ASMResult.AppendLine(allocStr + " " + TypeSignatureLiteralLabel);
                    break;

                case "IdString":
                    ASMResult.AppendLine(allocStr + " " + TypeIdLiteralLabel);
                    break;
                }
            }
            ASMResult.AppendLine();

            return(ASMResult.ToString());
        }
Beispiel #4
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     if (Signed)
     {
         return("idiv " + Arg);
     }
     else
     {
         return("div " + Arg);
     }
 }
Beispiel #5
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     if (WithCarry)
     {
         return("adc " + Dest + ", " + Src);
     }
     else
     {
         return("add " + Dest + ", " + Src);
     }
 }
Beispiel #6
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     if (Signed)
     {
         return("mult " + Src1 + ", " + Src2);
     }
     else
     {
         return("multu " + Src1 + ", " + Src2);
     }
 }
Beispiel #7
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     if (!string.IsNullOrWhiteSpace(Count))
     {
         return("shld " + Dest + ", " + Src + ", " + Count);
     }
     else
     {
         return("shl " + Dest + ", " + Src);
     }
 }
Beispiel #8
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     if (WithBorrow)
     {
         return("sbb " + Dest + ", " + Src);
     }
     else
     {
         return("sub " + Dest + ", " + Src);
     }
 }
Beispiel #9
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     if (Signed)
     {
         if (!string.IsNullOrWhiteSpace(Aux))
         {
             return("imul " + Arg + ", " + Src + ", " + Aux);
         }
         else if (!string.IsNullOrWhiteSpace(Src))
         {
             return("imul " + Arg + ", " + Src);
         }
         else
         {
             return("imul " + Arg);
         }
     }
     else
     {
         return("mul " + Arg);
     }
 }
Beispiel #10
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            int    numBytes = -(int)Size;
            string storeOp  = "";

            switch (Size)
            {
            case OperandSize.Byte:
                storeOp = "sb";
                break;

            case OperandSize.Halfword:
                storeOp = "sh";
                break;

            case OperandSize.Word:
                storeOp = "sw";
                break;
            }
            return(@"addi $sp, $sp, " + numBytes + "\r\n" +
                   storeOp + " " + Src + @", 0($sp)");
        }
Beispiel #11
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            StringBuilder ASMResult = new StringBuilder();

            ASMResult.AppendLine("# Field Table - " + CurrentTypeName);
            ASMResult.AppendLine(".globl " + CurrentTypeId + "_FieldTable");
            ASMResult.AppendLine(".align 2");
            ASMResult.AppendLine(CurrentTypeId + "_FieldTable:");

            foreach (Tuple <string, string, string> aFieldInfo in AllFieldInfos)
            {
                string fieldOffsetVal = aFieldInfo.Item1;
                string fieldSizeVal   = aFieldInfo.Item2;
                string fieldTypeIdVal = aFieldInfo.Item3;

                foreach (Tuple <string, int> anEntryFieldInfo in TableEntryFieldInfos)
                {
                    string allocStr = ASMUtilities.GetAllocStringForSize(anEntryFieldInfo.Item2);
                    switch (anEntryFieldInfo.Item1)
                    {
                    case "Offset":
                        ASMResult.AppendLine(allocStr + " " + fieldOffsetVal);
                        break;

                    case "Size":
                        ASMResult.AppendLine(allocStr + " " + fieldSizeVal);
                        break;

                    case "FieldType":
                        ASMResult.AppendLine(allocStr + " " + fieldTypeIdVal);
                        break;
                    }
                }
            }
            ASMResult.AppendLine("# Field Table End - " + CurrentTypeName);

            return(ASMResult.ToString());
        }
Beispiel #12
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            int    numBytes = (int)Size;
            string loadOp   = "";

            switch (Size)
            {
            case OperandSize.Byte:
                if (SignExtend)
                {
                    loadOp = "lb";
                }
                else
                {
                    loadOp = "lbu";
                }
                break;

            case OperandSize.Halfword:
                if (SignExtend)
                {
                    loadOp = "lh";
                }
                else
                {
                    loadOp = "lhu";
                }
                break;

            case OperandSize.Word:
                loadOp = "lw";
                break;
            }
            return(loadOp + " " + Dest + ", 0($sp)\r\n" +
                   "addi $sp, $sp, " + numBytes);
        }
Beispiel #13
0
        public override void CleanUpAssemblyCode(ASM.ASMBlock TheBlock, ref string ASMText)
        {
            // Create lists of extern and global labels
            TheBlock.ExternalLabels.Clear();
            List <string> ExternLines = ASMText.Replace("\r", "")
                                        .Split('\n')
                                        .Where(x => x.ToLower().Contains("extern "))
                                        .Select(x => x.Split(' ')[1].Split(':')[0])
                                        .ToList();

            TheBlock.ExternalLabels.AddRange(ExternLines);

            TheBlock.GlobalLabels.Clear();
            List <string> GlobalLines = ASMText.Replace("\r", "")
                                        .Split('\n')
                                        .Where(x => x.ToLower().Contains("global "))
                                        .Select(x => x.Split(' ')[1].Split(':')[0])
                                        .ToList();

            TheBlock.GlobalLabels.AddRange(GlobalLines);

            ASMText = ASMText.Replace("GLOBAL ", "global ");
            ASMText = ASMText.Replace("EXTERN ", "extern ");
        }
Beispiel #14
0
        /// <summary>
        /// Scans the specified non-plugged IL block.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="theMethodInfo">The method which generated the IL block.</param>
        /// <param name="theILBlock">The IL block to scan.</param>
        /// <returns>CompileResult.OK.</returns>
        private static CompileResult ScanNonpluggedILBlock(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            CompileResult result = CompileResult.OK;

            ASM.ASMBlock TheASMBlock = new ASM.ASMBlock()
            {
                OriginMethodInfo = theMethodInfo,
                Priority         = theMethodInfo.Priority
            };

            ILConversionState convState = new ILConversionState()
            {
                TheILLibrary      = TheLibrary,
                CurrentStackFrame = new StackFrame(),
                Input             = theILBlock,
                Result            = TheASMBlock
            };

            foreach (ILOp anOp in theILBlock.ILOps)
            {
                try
                {
                    string commentText = TheASMBlock.GenerateILOpLabel(convState.PositionOf(anOp), "") + "  --  " + anOp.opCode.ToString() + " -- Offset: " + anOp.Offset.ToString("X2");

                    ASM.ASMOp newCommentOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.Comment, commentText);
                    TheASMBlock.ASMOps.Add(newCommentOp);

                    int currCount = TheASMBlock.ASMOps.Count;
                    if (anOp is ILOps.MethodStart)
                    {
                        TargetArchitecture.MethodStartOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.MethodEnd)
                    {
                        TargetArchitecture.MethodEndOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.StackSwitch)
                    {
                        TargetArchitecture.StackSwitchOp.Convert(convState, anOp);
                    }
                    else
                    {
                        ILOp ConverterOp = TargetArchitecture.TargetILOps[(ILOp.OpCodes)anOp.opCode.Value];
                        ConverterOp.Convert(convState, anOp);
                    }

                    if (anOp.LabelRequired)
                    {
                        if (currCount < TheASMBlock.ASMOps.Count)
                        {
                            TheASMBlock.ASMOps[currCount].ILLabelPosition = convState.PositionOf(anOp);
                            TheASMBlock.ASMOps[currCount].RequiresILLabel = true;
                        }
                    }
                }
                catch (KeyNotFoundException)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], "Conversion IL op not found: " + Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value) + "."));
                }
                catch (InvalidOperationException)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value)));
                }
                catch (NotSupportedException ex)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], "An IL op reported something as not supported. " + Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value) + ". " + ex.Message));
                }
                catch (Exception ex)
                {
                    result = CompileResult.Fail;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], ex.Message));
                }
            }

            TheLibrary.TheASMLibrary.ASMBlocks.Add(TheASMBlock);

            return(result);
        }
Beispiel #15
0
        /// <summary>
        /// Scans the specified type's non-static fields.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="TheTypeInfo">The type to scan the non-static fields of.</param>
        /// <param name="FieldTablesBlock">The ASM block for the fields table for the library currently being compiled.</param>
        private static void ScanFields(ILLibrary TheLibrary, Types.TypeInfo TheTypeInfo, ASM.ASMBlock FieldTablesBlock)
        {
            string currentTypeId   = TheTypeInfo.ID;
            string currentTypeName = TheTypeInfo.UnderlyingType.FullName;
            List <Tuple <string, string, string> > AllFieldInfo = new List <Tuple <string, string, string> >();

            if (TheTypeInfo.UnderlyingType.BaseType == null || (TheTypeInfo.UnderlyingType.BaseType.FullName != "System.Array" &&
                                                                TheTypeInfo.UnderlyingType.BaseType.FullName != "System.MulticastDelegate"))
            {
                foreach (Types.FieldInfo anOwnField in TheTypeInfo.FieldInfos)
                {
                    if (!anOwnField.IsStatic)
                    {
                        Types.TypeInfo FieldTypeInfo = TheLibrary.GetTypeInfo(anOwnField.FieldType);

                        string fieldOffsetVal = anOwnField.OffsetInBytes.ToString();
                        string fieldSizeVal   = (FieldTypeInfo.IsValueType ? FieldTypeInfo.SizeOnHeapInBytes : FieldTypeInfo.SizeOnStackInBytes).ToString();
                        string fieldTypeIdVal = FieldTypeInfo.ID;

                        FieldTablesBlock.AddExternalLabel(fieldTypeIdVal);
                        AllFieldInfo.Add(new Tuple <string, string, string>(fieldOffsetVal, fieldSizeVal, fieldTypeIdVal));
                    }
                }
            }

            string parentTypeFieldTablePtr = "0";
            bool   parentPtrIsExternal     = false;

            if (TheTypeInfo.UnderlyingType.BaseType != null)
            {
                if (!TheTypeInfo.UnderlyingType.BaseType.AssemblyQualifiedName.Contains("mscorlib"))
                {
                    Types.TypeInfo baseTypeInfo = TheLibrary.GetTypeInfo(TheTypeInfo.UnderlyingType.BaseType);
                    parentPtrIsExternal = (ScannedTypes.ContainsKey(baseTypeInfo.ID) &&
                                           ScannedTypes[baseTypeInfo.ID] != TheLibrary) || !TheLibrary.TypeInfos.Contains(baseTypeInfo);
                    parentTypeFieldTablePtr = baseTypeInfo.ID + "_FieldTable";
                }
            }
            {
                string fieldOffsetVal = "0";
                string fieldSizeVal   = "0";
                string fieldTypeIdVal = parentTypeFieldTablePtr;

                if (parentPtrIsExternal)
                {
                    FieldTablesBlock.AddExternalLabel(fieldTypeIdVal);
                }

                AllFieldInfo.Add(new Tuple <string, string, string>(fieldOffsetVal, fieldSizeVal, fieldTypeIdVal));
            }

            List <Tuple <string, int> > TableEntryFieldInfos = GetSpecialClassFieldInfo(TheLibrary, typeof(Attributes.FieldInfoStructAttribute));

            ASM.ASMOp newFieldTableOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.FieldTable,
                                                                       currentTypeId, currentTypeName, AllFieldInfo, TableEntryFieldInfos);
            FieldTablesBlock.Append(newFieldTableOp);
        }
Beispiel #16
0
        /// <summary>
        /// Scans the specified type's methods.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="TheTypeInfo">The type to scan the methods of.</param>
        /// <param name="MethodTablesBlock">The ASM block for the methods table for the library currently being compiled.</param>
        private static void ScanMethods(ILLibrary TheLibrary, Types.TypeInfo TheTypeInfo, ASM.ASMBlock MethodTablesBlock)
        {
            string currentTypeId   = TheTypeInfo.ID;
            string currentTypeName = TheTypeInfo.UnderlyingType.FullName;

            List <Tuple <string, string> > AllMethodInfo = new List <Tuple <string, string> >();

            if (TheTypeInfo.UnderlyingType.BaseType == null || TheTypeInfo.UnderlyingType.BaseType.FullName != "System.Array")
            {
                foreach (Types.MethodInfo aMethodInfo in TheTypeInfo.MethodInfos)
                {
                    if (!aMethodInfo.IsStatic && !aMethodInfo.UnderlyingInfo.IsAbstract)
                    {
                        string methodID      = aMethodInfo.ID;
                        string methodIDValue = aMethodInfo.IDValue.ToString();

                        MethodTablesBlock.AddExternalLabel(methodID);

                        AllMethodInfo.Add(new Tuple <string, string>(methodID, methodIDValue));
                    }
                }
            }

            string parentTypeMethodTablePtr = "0";
            bool   parentPtrIsExternal      = false;

            if (TheTypeInfo.UnderlyingType.BaseType != null)
            {
                if (!TheTypeInfo.UnderlyingType.BaseType.AssemblyQualifiedName.Contains("mscorlib"))
                {
                    Types.TypeInfo baseTypeInfo = TheLibrary.GetTypeInfo(TheTypeInfo.UnderlyingType.BaseType);
                    parentPtrIsExternal = (ScannedTypes.ContainsKey(baseTypeInfo.ID) && ScannedTypes[baseTypeInfo.ID] != TheLibrary) ||
                                          !TheLibrary.TypeInfos.Contains(baseTypeInfo);
                    parentTypeMethodTablePtr = baseTypeInfo.ID + "_MethodTable";
                }
            }
            {
                string methodID      = parentTypeMethodTablePtr;
                string methodIDValue = "0";

                if (parentPtrIsExternal)
                {
                    MethodTablesBlock.AddExternalLabel(methodID);
                }

                AllMethodInfo.Add(new Tuple <string, string>(methodID, methodIDValue));
            }

            List <Tuple <string, int> > TableEntryFieldInfos = GetSpecialClassFieldInfo(TheLibrary, typeof(Attributes.MethodInfoStructAttribute));

            ASM.ASMOp newMethodTableOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.MethodTable,
                                                                        currentTypeId, currentTypeName, AllMethodInfo, TableEntryFieldInfos);
            MethodTablesBlock.Append(newMethodTableOp);
        }
Beispiel #17
0
        /// <summary>
        /// Scans the specified type's static fields.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="TheTypeInfo">The type to scan the static fields of.</param>
        /// <param name="StaticFieldsBlock">The ASM block for the static fields for the library currently being compiled.</param>
        private static void ScanStaticFields(ILLibrary TheLibrary, Types.TypeInfo TheTypeInfo, ASM.ASMBlock StaticFieldsBlock)
        {
            foreach (Types.FieldInfo aFieldInfo in TheTypeInfo.FieldInfos)
            {
                if (aFieldInfo.IsStatic)
                {
                    Types.TypeInfo fieldTypeInfo = TheLibrary.GetTypeInfo(aFieldInfo.FieldType);

                    string FieldID = aFieldInfo.ID;
                    string Size    = fieldTypeInfo.SizeOnStackInBytes.ToString();

                    ASM.ASMOp newStaticFieldOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.StaticField, FieldID, Size);
                    StaticFieldsBlock.Append(newStaticFieldOp);
                }
            }
        }
Beispiel #18
0
        /// <summary>
        /// Scans the specified type (excludes fields and methods).
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="TheTypeInfo">The type to scan.</param>
        /// <param name="TypesTableBlock">The ASM block for the types table for the library currently being compiled.</param>
        private static void ScanType(ILLibrary TheLibrary, Types.TypeInfo TheTypeInfo, ASM.ASMBlock TypesTableBlock)
        {
            string TypeId             = TheTypeInfo.ID;
            string SizeVal            = TheTypeInfo.SizeOnHeapInBytes.ToString();
            string IdVal              = (TypesScanned++).ToString();
            string StackSizeVal       = TheTypeInfo.SizeOnStackInBytes.ToString();
            string IsValueTypeVal     = (TheTypeInfo.IsValueType ? "1" : "0");
            string MethodTablePointer = TypeId + "_MethodTable";
            string IsPointerTypeVal   = (TheTypeInfo.IsPointer ? "1" : "0");
            string BaseTypeIdVal      = "0";

            if (TheTypeInfo.UnderlyingType.BaseType != null)
            {
                if (!TheTypeInfo.UnderlyingType.BaseType.AssemblyQualifiedName.Contains("mscorlib"))
                {
                    Types.TypeInfo baseTypeInfo = TheLibrary.GetTypeInfo(TheTypeInfo.UnderlyingType.BaseType);
                    BaseTypeIdVal = baseTypeInfo.ID;
                    //Declared external to this library, so won't appear in this library's type tables
                    if ((ScannedTypes.ContainsKey(baseTypeInfo.ID) &&
                         ScannedTypes[baseTypeInfo.ID] != TheLibrary) ||
                        !TheLibrary.TypeInfos.Contains(baseTypeInfo))
                    {
                        TypesTableBlock.AddExternalLabel(BaseTypeIdVal);
                    }
                }
            }
            string FieldTablePointer         = TypeId + "_FieldTable";
            string TypeSignatureLiteralLabel = TheLibrary.AddStringLiteral(TheTypeInfo.UnderlyingType.FullName); // Legacy
            string TypeIdLiteralLabel        = TheLibrary.AddStringLiteral(TheTypeInfo.ID);

            Types.TypeInfo         typeTypeInfo  = ILLibrary.SpecialClasses[typeof(Attributes.TypeClassAttribute)].First();
            List <Types.FieldInfo> OrderedFields = typeTypeInfo.FieldInfos.Where(x => !x.IsStatic).OrderBy(x => x.OffsetInBytes).ToList();
            List <Tuple <string, Types.TypeInfo> > FieldInformation = new List <Tuple <string, Types.TypeInfo> >();

            foreach (Types.FieldInfo aTypeField in OrderedFields)
            {
                Types.TypeInfo FieldTypeInfo = TheLibrary.GetTypeInfo(aTypeField.FieldType);
                FieldInformation.Add(new Tuple <string, Types.TypeInfo>(aTypeField.Name, FieldTypeInfo));
            }

            ASM.ASMOp newTypeTableOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.TypeTable,
                                                                      TypeId, SizeVal, IdVal, StackSizeVal, IsValueTypeVal, MethodTablePointer, IsPointerTypeVal,
                                                                      BaseTypeIdVal, FieldTablePointer, TypeSignatureLiteralLabel, TypeIdLiteralLabel, FieldInformation);
            TypesTableBlock.Append(newTypeTableOp);

            TypesTableBlock.AddExternalLabel(MethodTablePointer);
            TypesTableBlock.AddExternalLabel(FieldTablePointer);
            TypesTableBlock.AddExternalLabel(TypeSignatureLiteralLabel);
            TypesTableBlock.AddExternalLabel(TypeIdLiteralLabel);
        }
Beispiel #19
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     return("push " + ASMUtilities.GetOpSizeStr(Size) + " " + Src);
 }
Beispiel #20
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     return("neg " + Arg);
 }
Beispiel #21
0
        /// <summary>
        /// Scans the specified non-plugged IL block.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="theMethodInfo">The method which generated the IL block.</param>
        /// <param name="theILBlock">The IL block to scan.</param>
        /// <returns>CompileResult.OK.</returns>
        private static CompileResult ScanNonpluggedILBlock(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            CompileResult result = CompileResult.OK;

            ASM.ASMBlock TheASMBlock = new ASM.ASMBlock()
            {
                OriginMethodInfo = theMethodInfo,
                Priority = theMethodInfo.Priority
            };
            
            ILConversionState convState = new ILConversionState()
            {
                TheILLibrary = TheLibrary,
                CurrentStackFrame = new StackFrame(),
                Input = theILBlock,
                Result = TheASMBlock
            };
            foreach (ILOp anOp in theILBlock.ILOps)
            {
                try
                {
                    string commentText = TheASMBlock.GenerateILOpLabel(convState.PositionOf(anOp), "") + "  --  " + anOp.opCode.ToString() + " -- Offset: " + anOp.Offset.ToString("X2");
                    
                    ASM.ASMOp newCommentOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.Comment, commentText);
                    TheASMBlock.ASMOps.Add(newCommentOp);
                    
                    int currCount = TheASMBlock.ASMOps.Count;
                    if (anOp is ILOps.MethodStart)
                    {
                        TargetArchitecture.MethodStartOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.MethodEnd)
                    {
                        TargetArchitecture.MethodEndOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.StackSwitch)
                    {
                        TargetArchitecture.StackSwitchOp.Convert(convState, anOp);
                    }
                    else
                    {
                        ILOp ConverterOp = TargetArchitecture.TargetILOps[(ILOp.OpCodes)anOp.opCode.Value];
                        ConverterOp.Convert(convState, anOp);
                    }

                    if (anOp.LabelRequired)
                    {
                        if (currCount < TheASMBlock.ASMOps.Count)
                        {
                            TheASMBlock.ASMOps[currCount].ILLabelPosition = convState.PositionOf(anOp);
                            TheASMBlock.ASMOps[currCount].RequiresILLabel = true;
                        }
                    }
                }
                catch (KeyNotFoundException)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), "Conversion for IL op not found."));
                }
                catch (InvalidOperationException ex)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), ex.Message));
                }
                catch (NotSupportedException ex)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), "An IL op reported something as not supported : " + ex.Message));
                }
                catch (Exception ex)
                {
                    result = CompileResult.Fail;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), ex.Message));
                }
            }

            TheLibrary.TheASMLibrary.ASMBlocks.Add(TheASMBlock);

            return result;
        }
Beispiel #22
0
        public override string Convert(ASM.ASMBlock theBlock)
        {
            string jmpOp = "";

            switch (JumpType)
            {
            case JmpOp.Jump:
                jmpOp = "jmp";
                break;

            case JmpOp.JumpZero:
                jmpOp = "jz";
                break;

            case JmpOp.JumpNotZero:
                jmpOp = "jnz";
                break;

            case JmpOp.JumpEqual:
                jmpOp = "je";
                break;

            case JmpOp.JumpNotEqual:
                jmpOp = "jne";
                break;

            case JmpOp.JumpLessThan:
                if (UnsignedTest)
                {
                    jmpOp = "jb";
                }
                else
                {
                    jmpOp = "jl";
                }
                break;

            case JmpOp.JumpGreaterThan:
                if (UnsignedTest)
                {
                    jmpOp = "ja";
                }
                else
                {
                    jmpOp = "jg";
                }
                break;

            case JmpOp.JumpLessThanEqual:
                if (UnsignedTest)
                {
                    jmpOp = "jbe";
                }
                else
                {
                    jmpOp = "jle";
                }
                break;

            case JmpOp.JumpGreaterThanEqual:
                if (UnsignedTest)
                {
                    jmpOp = "jae";
                }
                else
                {
                    jmpOp = "jge";
                }
                break;
            }

            return(jmpOp + " " + theBlock.GenerateILOpLabel(DestILPosition, Extension));
        }
Beispiel #23
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     return("cdq");
 }
Beispiel #24
0
        /// <summary>
        /// Scans the specified library and any dependencies.
        /// </summary>
        /// <param name="TheLibrary">The library to scan.</param>
        /// <returns>
        /// CompileResult.OK if completed successfully.
        /// Otherwise CompileResult.PartialFail or CompileResult.Error depending on
        /// the extent of the problem.
        /// </returns>
        public static CompileResult Scan(ILLibrary TheLibrary)
        {
            CompileResult result = CompileResult.OK;

            if (TheLibrary.ILScanned)
            {
                return(result);
            }
            TheLibrary.ILScanned = true;

            foreach (ILLibrary depLib in TheLibrary.Dependencies)
            {
                Scan(depLib);
            }

            // Create / Add Static Fields ASM Block
            ASM.ASMBlock StaticFieldsBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 9
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(StaticFieldsBlock);

            // Create / Add Types Table ASM Block
            ASM.ASMBlock TypesTableBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 8
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(TypesTableBlock);

            // Create / Add Method Tables ASM Block
            ASM.ASMBlock MethodTablesBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) + 0
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(MethodTablesBlock);

            // Create / Add Field Tables ASM Block
            ASM.ASMBlock FieldTablesBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) + 1
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(FieldTablesBlock);

            // Don't use foreach or you get collection modified exceptions
            for (int i = 0; i < TheLibrary.TypeInfos.Count; i++)
            {
                Types.TypeInfo aTypeInfo = TheLibrary.TypeInfos[i];
                if (!ScannedTypes.ContainsKey(aTypeInfo.ID))
                {
                    ScannedTypes.Add(aTypeInfo.ID, TheLibrary);
                    ScanStaticFields(TheLibrary, aTypeInfo, StaticFieldsBlock);
                    ScanType(TheLibrary, aTypeInfo, TypesTableBlock);
                    ScanMethods(TheLibrary, aTypeInfo, MethodTablesBlock);
                    ScanFields(TheLibrary, aTypeInfo, FieldTablesBlock);
                }
            }

            foreach (Types.MethodInfo aMethodInfo in TheLibrary.ILBlocks.Keys)
            {
                ILBlock       anILBlock    = TheLibrary.ILBlocks[aMethodInfo];
                CompileResult singleResult = CompileResult.OK;

                if (anILBlock.Plugged)
                {
                    singleResult = ScanPluggedILBlock(TheLibrary, aMethodInfo, anILBlock);
                }
                else
                {
                    singleResult = ScanNonpluggedILBlock(TheLibrary, aMethodInfo, anILBlock);
                }

                if (result != CompileResult.OK)
                {
                    result = singleResult;
                }
            }

            // Create / Add String Literals ASM Block
            #region String Literals Block

            ASM.ASMBlock StringLiteralsBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 10
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(StringLiteralsBlock);

            string StringTypeId = ILLibrary.SpecialClasses[typeof(Attributes.StringClassAttribute)].First().ID;
            StringLiteralsBlock.AddExternalLabel(StringTypeId);
            foreach (KeyValuePair <string, string> aStringLiteral in TheLibrary.StringLiterals)
            {
                string value       = aStringLiteral.Value;
                byte[] lengthBytes = BitConverter.GetBytes(value.Length);

                ASM.ASMOp newLiteralOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.StringLiteral,
                                                                        aStringLiteral.Key, StringTypeId, lengthBytes, value.ToCharArray());

                StringLiteralsBlock.Append(newLiteralOp);
            }

            #endregion

            return(result);
        }
Beispiel #25
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     return("sllv " + Dest + ", " + Src + ", " + BitsReg);
 }
Beispiel #26
0
        /// <summary>
        /// Scans the specified library and any dependencies.
        /// </summary>
        /// <param name="TheLibrary">The library to scan.</param>
        /// <returns>
        /// CompileResult.OK if completed successfully. 
        /// Otherwise CompileResult.PartialFail or CompileResult.Error depending on 
        /// the extent of the problem.
        /// </returns>
        public static CompileResult Scan(ILLibrary TheLibrary)
        {
            CompileResult result = CompileResult.OK;

            if (TheLibrary.ILScanned)
            {
                return result;
            }
            TheLibrary.ILScanned = true;

            foreach (ILLibrary depLib in TheLibrary.Dependencies)
            {
                Scan(depLib);
            }
            
            // Create / Add Static Fields ASM Block
            ASM.ASMBlock StaticFieldsBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 9
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(StaticFieldsBlock);

            // Create / Add Types Table ASM Block
            ASM.ASMBlock TypesTableBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 8
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(TypesTableBlock);

            // Create / Add Method Tables ASM Block
            ASM.ASMBlock MethodTablesBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) + 0
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(MethodTablesBlock);

            // Create / Add Field Tables ASM Block
            ASM.ASMBlock FieldTablesBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) + 1
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(FieldTablesBlock);

            // Don't use foreach or you get collection modified exceptions
            for (int i = 0; i < TheLibrary.TypeInfos.Count; i++)
            {
                Types.TypeInfo aTypeInfo = TheLibrary.TypeInfos[i];
                if (!ScannedTypes.ContainsKey(aTypeInfo.ID))
                {
                    ScannedTypes.Add(aTypeInfo.ID, TheLibrary);
                    ScanStaticFields(TheLibrary, aTypeInfo, StaticFieldsBlock);
                    ScanType(TheLibrary, aTypeInfo, TypesTableBlock);
                    ScanMethods(TheLibrary, aTypeInfo, MethodTablesBlock);
                    ScanFields(TheLibrary, aTypeInfo, FieldTablesBlock);
                }
            }

            foreach (Types.MethodInfo aMethodInfo in TheLibrary.ILBlocks.Keys)
            {
                ILBlock anILBlock = TheLibrary.ILBlocks[aMethodInfo];
                CompileResult singleResult = CompileResult.OK;

                if (anILBlock.Plugged)
                {
                    singleResult = ScanPluggedILBlock(TheLibrary, aMethodInfo, anILBlock);
                }
                else
                {
                    singleResult = ScanNonpluggedILBlock(TheLibrary, aMethodInfo, anILBlock);
                }
            
                if (result != CompileResult.OK)
                {
                    result = singleResult;
                }
            }

            // Create / Add String Literals ASM Block
            #region String Literals Block

            ASM.ASMBlock StringLiteralsBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 10
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(StringLiteralsBlock);

            string StringTypeId = ILLibrary.SpecialClasses[typeof(Attributes.StringClassAttribute)].First().ID;
            StringLiteralsBlock.AddExternalLabel(StringTypeId);
            foreach (KeyValuePair<string, string> aStringLiteral in TheLibrary.StringLiterals)
            {
                string value = aStringLiteral.Value;
                byte[] lengthBytes = BitConverter.GetBytes(value.Length);

                ASM.ASMOp newLiteralOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.StringLiteral, 
                    aStringLiteral.Key, StringTypeId, lengthBytes, value.ToCharArray());

                StringLiteralsBlock.Append(newLiteralOp);
            }

            #endregion

            return result;
        }
Beispiel #27
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     return("cmp " + Arg1 + ", " + Arg2);
 }
Beispiel #28
0
 public override string Convert(ASM.ASMBlock theBlock)
 {
     return("lw $ra, 0($sp)\r\n" +
            "addi $sp, $sp, 4\r\n" +
            "j $ra");
 }