private SymbolOperand GetInternalAllocateStringCallTarget(ITypeSystem typeSystem) { RuntimeType runtimeType = typeSystem.GetType(@"Mosa.Internal.Runtime"); RuntimeMethod callTarget = runtimeType.FindMethod(@"AllocateString"); return SymbolOperand.FromMethod(callTarget); }
private SymbolOperand GetInternalAllocateStringCallTarget(ITypeSystem typeSystem) { RuntimeType runtimeType = typeSystem.GetType(@"Mosa.Internal.Runtime"); RuntimeMethod callTarget = runtimeType.FindMethod(@"AllocateString"); return(SymbolOperand.FromMethod(callTarget)); }
RuntimeMethod ITypeSystem.GetImplementationForInternalCall(RuntimeMethod internalCall) { Debug.Assert(internalCall != null, @"internalCall is null."); if (null == internalCall) { throw new ArgumentNullException(@"internalCall"); } // Return value RuntimeMethod result = null; // Shortcut: If the call was resolved previously, scan it there if (this.internalCallTargets.TryGetValue(internalCall, out result)) { return(result); } /* * FIXME: * - The following sequence requires that mscorlib is available in the test runtime. * - Maybe we should be smarter about its use though. * - Commented out right now, as we don't load assembly dependencies yet. */ ITypeSystem ts = (ITypeSystem)this; // FIXME: Include this when we're loading mscorlib //RuntimeType rtMia = ts.GetType(@"System.Runtime.CompilerServices.MethodImplAttribute"); //MethodImplAttribute mia = (MethodImplAttribute)internalCall.GetAttributes(rtMia, true); //Debug.Assert(MethodImplOptions.InternalCall == (mia.Value & MethodImplOptions.InternalCall), @"Method is not InternalCall."); //if (MethodImplOptions.InternalCall != (mia.Value & MethodImplOptions.InternalCall)) // throw new ArgumentException(@"Method not marked as an InternalCall.", @"internalCall"); RuntimeType callImplType = ts.GetType("Mosa.Runtime.Vm.InternalCallImplAttribute"); object[] callDefAttrs = internalCall.GetCustomAttributes(callImplType); Debug.Assert(0 != callDefAttrs.Length, @"No runtime call definition for icall!"); Debug.Assert(1 == callDefAttrs.Length, @"Only one call definition for icall supported! Additional ones ignored!"); InternalCallImplAttribute callDef = (InternalCallImplAttribute)callDefAttrs[0]; // Scan all known types for icalls foreach (RuntimeType rType in internalTypes) { foreach (RuntimeMethod rMethod in rType.Methods) { object[] callImpls = rMethod.GetCustomAttributes(callImplType); foreach (InternalCallImplAttribute callImpl in callImpls) { if (callDef.Match(callImpl) == true) { // We found the sought icall, save it and return it this.internalCallTargets[internalCall] = rMethod; return(rMethod); } } } } throw new NotImplementedException(@"Requested InternalCall not loaded or not implemented."); }
/// <summary> /// Gets the base type. /// </summary> /// <returns>The base type.</returns> protected override RuntimeType GetBaseType() { ITypeSystem typeSystem = RuntimeBase.Instance.TypeLoader; return(typeSystem.GetType(this.Module, this.baseTypeToken)); }
/// <summary> /// Sets the attributes. /// </summary> /// <param name="module">The module.</param> /// <param name="owner">The owner.</param> /// <param name="attributes">The attributes.</param> private void SetAttributes(IMetadataModule module, TokenTypes owner, List <CustomAttributeRow> attributes) { ITypeSystem ts = (ITypeSystem)this; // Convert the custom attribute rows to RuntimeAttribute instances RuntimeAttribute[] ra = new RuntimeAttribute[attributes.Count]; for (int i = 0; i < attributes.Count; i++) { ra[i] = new RuntimeAttribute(module, attributes[i]); } // The following switch matches the AttributeTargets enumeration against // metadata tables, which make valid targets for an attribute. switch (owner & TokenTypes.TableMask) { case TokenTypes.Assembly: // AttributeTargets.Assembly break; case TokenTypes.TypeDef: // AttributeTargets.Class // AttributeTargets.Delegate // AttributeTargets.Enum // AttributeTargets.Interface // AttributeTargets.Struct RuntimeType type = ts.GetType(module, owner); type.SetAttributes(ra); if (rtCallTypeAttribute != null) { if (type.IsDefined(rtCallTypeAttribute) == true) { this.internalTypes.Add(type); } } break; case TokenTypes.MethodDef: // AttributeTargets.Constructor // AttributeTargets.Method RuntimeMethod method = ts.GetMethod(module, owner); method.SetAttributes(ra); break; case TokenTypes.Event: // AttributeTargets.Event break; case TokenTypes.Field: // AttributeTargets.Field break; case TokenTypes.GenericParam: // AttributeTargets.GenericParameter break; case TokenTypes.Module: // AttributeTargets.Module break; case TokenTypes.Param: // AttributeTargets.Parameter // AttributeTargets.ReturnValue break; case TokenTypes.Property: // AttributeTargets.StackFrameIndex break; } }
/// <summary> /// Parses an elementary field, parameter or property definition. /// </summary> /// <param name="module">The metadata module, which contains the attribute blob.</param> /// <param name="reader">The binary reader to read data From.</param> /// <param name="sigType">The signature type of the field, parameter or property to read.</param> /// <returns>An object, which represents the value read From the attribute blob.</returns> /// <exception cref="System.NotSupportedException"><paramref name="sigType"/> is not yet supported.</exception> private static object ParseElem(IMetadataModule module, BinaryReader reader, SigType sigType) { object result; switch (sigType.Type) { case CilElementType.Boolean: result = (1 == reader.ReadByte()); break; case CilElementType.Char: result = (char)reader.ReadUInt16(); break; case CilElementType.I1: result = reader.ReadSByte(); break; case CilElementType.I2: result = reader.ReadInt16(); break; case CilElementType.I4: result = reader.ReadInt32(); break; case CilElementType.I8: result = reader.ReadInt64(); break; case CilElementType.U1: result = reader.ReadByte(); break; case CilElementType.U2: result = reader.ReadUInt16(); break; case CilElementType.U4: result = reader.ReadUInt32(); break; case CilElementType.U8: result = reader.ReadUInt64(); break; case CilElementType.R4: result = reader.ReadSingle(); break; case CilElementType.R8: result = reader.ReadDouble(); break; case CilElementType.String: result = ParseSerString(reader); break; case CilElementType.Type: { string typeName = ParseSerString(reader); result = Type.GetType(typeName); } break; case CilElementType.Class: { string typeName = ParseSerString(reader); string[] type = typeName.Split(','); if (type.Length > 1) { result = Type.GetType(typeName); } else { result = Type.GetType(typeName + ", " + module.Name); } } break; case CilElementType.ValueType: { ValueTypeSigType vtSigType = sigType as ValueTypeSigType; ITypeSystem ts = RuntimeBase.Instance.TypeLoader; RuntimeType type = ts.GetType(module, vtSigType.Token); RuntimeType baseType = type.BaseType; if ("System" == baseType.Namespace && "Enum" == baseType.Name) { // Retrieve the value__ field to get the enums integer type Debug.Assert(type.Fields.Count == 1, "More than one field in the enum."); RuntimeField value = type.Fields[0]; Debug.Assert(value.Name == "value__", "First field of enum not named value__"); result = ParseElem(module, reader, value.Type); Type enumType = Type.GetType(type.Namespace + "." + type.Name); result = Enum.ToObject(enumType, result); } else { throw new NotSupportedException(); } } break; case CilElementType.Object: throw new NotSupportedException(); default: throw new NotSupportedException(); } return(result); }