/// <inheritdoc /> public LocalVariableSignature ImportLocalVariableSignature(LocalVariableSignature signature) { return(new LocalVariableSignature(signature.Variables.Select( x => new VariableSignature(ImportTypeSignature(x.VariableType)))) { Attributes = signature.Attributes }); }
public void VariablesTest() { // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem; var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); var methodTable = tableStream.GetTable <MethodDefinition>(); var signatureTable = tableStream.GetTable <StandAloneSignature>(); var variable = new VariableSignature(typeSystem.String); var variable2 = new VariableSignature(typeSystem.Int32); // create localvarsig. var localVarSig = new LocalVariableSignature(); localVarSig.Variables.Add(variable); localVarSig.Variables.Add(variable2); var signature = new StandAloneSignature(localVarSig); signatureTable.Add(signature); // write code. var body = methodTable[0].MethodBody; body.Signature = signature; body.Instructions.Clear(); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldloc, variable)); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop)); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldloc, variable2)); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop)); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ret)); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly, true); methodTable = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>().GetTable <MethodDefinition>(); var newBody = methodTable[0].MethodBody; Assert.IsNotNull(newBody.Signature); Assert.IsInstanceOfType(newBody.Signature.Signature, typeof(LocalVariableSignature)); var newLocalVarSig = (LocalVariableSignature)newBody.Signature.Signature; Assert.AreEqual(localVarSig.Variables.Count, newLocalVarSig.Variables.Count); for (int i = 0; i < localVarSig.Variables.Count; i++) { Utilities.ValidateType(localVarSig.Variables[i].VariableType, newLocalVarSig.Variables[i].VariableType); } Assert.IsInstanceOfType(newBody.Instructions[0].Operand, typeof(VariableSignature)); Utilities.ValidateType(variable.VariableType, ((VariableSignature)newBody.Instructions[0].Operand).VariableType); Assert.IsInstanceOfType(newBody.Instructions[2].Operand, typeof(VariableSignature)); Utilities.ValidateType(variable2.VariableType, ((VariableSignature)newBody.Instructions[2].Operand).VariableType); }
/// <summary> /// Sets the signature of local variables in the method. /// </summary> /// <param name="localVariableSignature">The local variable signature of the _method.</param> public void SetLocalVariableSignature(LocalVariableSignature localVariableSignature) { if (localVariableSignature == null) { throw new ArgumentNullException("localVariableSignature"); } _localsSig = localVariableSignature; _locals = new List <Operand> (new Operand[_localsSig.Types.Length]); _nextStackSlot = _locals.Count + 1; }
// NOT USED //private void ScheduleDependencyForCompilation(SigType signatureType) //{ // RuntimeType runtimeType = null; // TypeSigType typeSigType = signatureType as TypeSigType; // if (typeSigType != null) // { // runtimeType = moduleTypeSystem.GetType(typeSigType.Token); // } // else // { // GenericInstSigType genericSignatureType = signatureType as GenericInstSigType; // if (genericSignatureType != null) // { // RuntimeType genericType = moduleTypeSystem.GetType(genericSignatureType.BaseType.Token); // Console.WriteLine(@"Loaded generic type {0}", genericType.FullName); // runtimeType = new CilGenericType(moduleTypeSystem, genericType, genericSignatureType); // } // } // if (runtimeType != null) // { // compilationScheduler.ScheduleTypeForCompilation(runtimeType); // } //} /// <summary> /// Sets the signature of local variables in the method. /// </summary> /// <param name="localVariableSignature">The local variable signature of the _method.</param> public void SetLocalVariableSignature(LocalVariableSignature localVariableSignature) { if (localVariableSignature == null) { throw new ArgumentNullException(@"localVariableSignature"); } localsSig = localVariableSignature; int count = localsSig.Locals.Length; locals = new Operand[count]; nextStackSlot = locals.Length + 1; }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> public void Run() { using (Stream code = methodCompiler.GetInstructionStream()) { // Initialize the instruction methodCompiler.InstructionSet = new InstructionSet(256); // update the base class InstructionSet = methodCompiler.InstructionSet; using (codeReader = new BinaryReader(code)) { // The size of the code in bytes MethodHeader header = new MethodHeader(); ReadMethodHeader(codeReader, ref header); if (header.localsSignature.RID != 0) { StandAloneSigRow row = methodCompiler.Method.Module.MetadataModule.Metadata.ReadStandAloneSigRow(header.localsSignature); LocalVariableSignature localsSignature; if (methodCompiler.Method.DeclaringType is CilGenericType) { localsSignature = new LocalVariableSignature(methodCompiler.Method.Module.MetadataModule.Metadata, row.SignatureBlobIdx, (methodCompiler.Method.DeclaringType as CilGenericType).GenericArguments); } else { localsSignature = new LocalVariableSignature(methodCompiler.Method.Module.MetadataModule.Metadata, row.SignatureBlobIdx); } methodCompiler.SetLocalVariableSignature(localsSignature); } /* Decode the instructions */ Decode(methodCompiler, ref header); // When we leave, the operand stack must only contain the locals... //Debug.Assert(_operandStack.Count == _method.Locals.Count); } } }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> public void Run() { // The size of the code in bytes MethodHeader header = new MethodHeader(); using (Stream code = MethodCompiler.GetInstructionStream()) { // Initalize the instruction, setting the initalize size to 10 times the code stream MethodCompiler.InstructionSet = new InstructionSet((int)code.Length * 10); // update the base class InstructionSet = MethodCompiler.InstructionSet; using (BinaryReader reader = new BinaryReader(code)) { _compiler = MethodCompiler; _method = MethodCompiler.Method; _codeReader = reader; ReadMethodHeader(reader, ref header); //Debug.WriteLine("Decoding " + compiler.Method.ToString()); if (0 != header.localsSignature) { StandAloneSigRow row; IMetadataProvider md = _method.Module.Metadata; md.Read(header.localsSignature, out row); MethodCompiler.SetLocalVariableSignature(LocalVariableSignature.Parse(md, row.SignatureBlobIdx)); } /* Decode the instructions */ Decode(MethodCompiler, ref header); // When we leave, the operand stack must only contain the locals... //Debug.Assert(_operandStack.Count == _method.Locals.Count); _codeReader = null; _compiler = null; } } }