/// <summary> /// Link time code generator used to compile dynamically created methods during link time. /// </summary> /// <param name="compiler">The assembly compiler used to compile this method.</param> /// <param name="methodName">The name of the created method.</param> /// <param name="instructionSet">The instruction set.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"><paramref name="compiler"/>, <paramref name="methodName"/> or <paramref name="instructionSet"/> is null.</exception> /// <exception cref="System.ArgumentException"><paramref name="methodName"/> is invalid.</exception> public static LinkerGeneratedMethod Compile(AssemblyCompiler compiler, string methodName, InstructionSet instructionSet, ITypeSystem typeSystem) { if (compiler == null) throw new ArgumentNullException(@"compiler"); if (methodName == null) throw new ArgumentNullException(@"methodName"); if (methodName.Length == 0) throw new ArgumentException(@"Invalid method name."); LinkerGeneratedType compilerGeneratedType = typeSystem.InternalTypeModule.GetType(@"Mosa.Tools.Compiler", @"LinkerGenerated") as LinkerGeneratedType; // Create the type if we need to. if (compilerGeneratedType == null) { compilerGeneratedType = new LinkerGeneratedType(typeSystem.InternalTypeModule, @"Mosa.Tools.Compiler", @"LinkerGenerated", null); typeSystem.AddInternalType(compilerGeneratedType); } MethodSignature signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]); // Create the method // HACK: <$> prevents the method from being called from CIL LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeSystem.InternalTypeModule, "<$>" + methodName, compilerGeneratedType, signature); compilerGeneratedType.AddMethod(method); LinkerMethodCompiler methodCompiler = new LinkerMethodCompiler(compiler, compiler.Pipeline.FindFirst<ICompilationSchedulerStage>(), method, instructionSet); methodCompiler.Compile(); return method; }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> void IAssemblyCompilerStage.Run() { if (!secondStage) { IntPtr entryPoint = WriteMultibootEntryPoint(); WriteMultibootHeader(entryPoint); secondStage = true; } else { ITypeInitializerSchedulerStage typeInitializerSchedulerStage = this.compiler.Pipeline.FindFirst <ITypeInitializerSchedulerStage>(); SigType I4 = new SigType(CilElementType.I4); RegisterOperand ecx = new RegisterOperand(I4, GeneralPurposeRegister.ECX); RegisterOperand eax = new RegisterOperand(I4, GeneralPurposeRegister.EAX); RegisterOperand ebx = new RegisterOperand(I4, GeneralPurposeRegister.EBX); InstructionSet instructionSet = new InstructionSet(16); Context ctx = new Context(instructionSet, -1); ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, ecx, new ConstantOperand(I4, 0x200000)); ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, new MemoryOperand(I4, ecx.Register, new IntPtr(0x0)), eax); ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, new MemoryOperand(I4, ecx.Register, new IntPtr(0x4)), ebx); SymbolOperand entryPoint = SymbolOperand.FromMethod(typeInitializerSchedulerStage.Method); ctx.AppendInstruction(CPUx86.Instruction.CallInstruction, null, entryPoint); ctx.AppendInstruction(CPUx86.Instruction.RetInstruction); LinkerGeneratedMethod method = LinkTimeCodeGenerator.Compile(this.compiler, @"MultibootInit", instructionSet, typeSystem); this.linker.EntryPoint = this.linker.GetSymbol(method.ToString()); } }
private RuntimeMethod GenerateMethod(string nameSpace, string typeName, string methodName) { var type = new LinkerGeneratedType(typeModule, nameSpace, typeName, null); MethodSignature signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]); // Create the method LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeModule, methodName, type, signature); type.AddMethod(method); return(method); }
private void CompileObjectEquals(string typeName) { LinkerGeneratedType type = new LinkerGeneratedType(typeModule, @"System", typeName, null); MethodSignature signature = new MethodSignature(BuiltInSigType.Boolean, new SigType[] { BuiltInSigType.Object }); // Create the method LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeModule, @"Equals", type, signature); method.Parameters.Add(new RuntimeParameter(@"obj", 0, ParameterAttributes.In)); type.AddMethod(method); this.Compile(method); }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> void IAssemblyCompilerStage.Run() { ITypeModule mainTypeModule = typeSystem.MainTypeModule; if (mainTypeModule.MetadataModule.EntryPoint.RID != 0) { RuntimeMethod entrypoint = mainTypeModule.GetMethod(mainTypeModule.MetadataModule.EntryPoint); Schedule(entrypoint); } ctx.AppendInstruction(IR.Instruction.EpilogueInstruction); ctx.Other = 0; method = LinkTimeCodeGenerator.Compile(compiler, @"AssemblyInit", instructionSet, typeSystem); }
/// <summary> /// Link time code generator used to compile dynamically created methods during link time. /// </summary> /// <param name="compiler">The assembly compiler used to compile this method.</param> /// <param name="methodName">The name of the created method.</param> /// <param name="instructionSet">The instruction set.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"><paramref name="compiler"/>, <paramref name="methodName"/> or <paramref name="instructionSet"/> is null.</exception> /// <exception cref="System.ArgumentException"><paramref name="methodName"/> is invalid.</exception> public static LinkerGeneratedMethod Compile(AssemblyCompiler compiler, string methodName, InstructionSet instructionSet, ITypeSystem typeSystem) { if (compiler == null) { throw new ArgumentNullException(@"compiler"); } if (methodName == null) { throw new ArgumentNullException(@"methodName"); } if (methodName.Length == 0) { throw new ArgumentException(@"Invalid method name."); } LinkerGeneratedType compilerGeneratedType = typeSystem.InternalTypeModule.GetType(@"Mosa.Tools.Compiler", @"LinkerGenerated") as LinkerGeneratedType; // Create the type if we need to. if (compilerGeneratedType == null) { compilerGeneratedType = new LinkerGeneratedType(typeSystem.InternalTypeModule, @"Mosa.Tools.Compiler", @"LinkerGenerated", null); typeSystem.AddInternalType(compilerGeneratedType); } MethodSignature signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]); // Create the method // HACK: <$> prevents the method from being called from CIL LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeSystem.InternalTypeModule, "<$>" + methodName, compilerGeneratedType, signature); compilerGeneratedType.AddMethod(method); LinkerMethodCompiler methodCompiler = new LinkerMethodCompiler(compiler, compiler.Pipeline.FindFirst <ICompilationSchedulerStage>(), method, instructionSet); methodCompiler.Compile(); return(method); }
private void CompileObjectEquals(string typeName) { LinkerGeneratedType type = new LinkerGeneratedType(moduleTypeSystem, @"System", typeName); // Create the method LinkerGeneratedMethod method = new LinkerGeneratedMethod(moduleTypeSystem, @"Equals", type); method.Parameters.Add(new RuntimeParameter(null, @"obj", 0, ParameterAttributes.In)); method.SetSignature(new MethodSignature(BuiltInSigType.Boolean, new SigType[] { BuiltInSigType.Object })); type.AddMethod(method); this.Compile(method); }
private RuntimeMethod GenerateMethod(string nameSpace, string typeName, string methodName) { var type = new LinkerGeneratedType(moduleTypeSystem, nameSpace, typeName); // Create the method LinkerGeneratedMethod method = new LinkerGeneratedMethod(moduleTypeSystem, methodName, type); type.AddMethod(method); return method; }
private RuntimeMethod GenerateMethod(string nameSpace, string typeName, string methodName) { var type = new LinkerGeneratedType(typeModule, nameSpace, typeName, null); MethodSignature signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]); // Create the method LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeModule, methodName, type, signature); type.AddMethod(method); return method; }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> void IAssemblyCompilerStage.Run() { IModuleTypeSystem mainModule = typeSystem.GetMainModuleTypeSystem(); if (mainModule.MetadataModule.EntryPoint != TokenTypes.Module) { RuntimeMethod entrypoint = mainModule.GetMethod(mainModule.MetadataModule.EntryPoint); Schedule(entrypoint); } ctx.AppendInstruction(IR.Instruction.EpilogueInstruction); ctx.Other = 0; method = LinkTimeCodeGenerator.Compile(compiler, @"AssemblyInit", instructionSet, typeSystem); }