private static TypeGen MakeModuleSlotHolder() { TypeGen tg = snippetAssembly.DefinePublicType("moduleHolder_" + Interlocked.Increment(ref index), typeof(object)); tg.AddModuleField(typeof(PythonModule)); tg.myType.DefineDefaultConstructor(MethodAttributes.Public); return(tg); }
/// <summary> /// Generates a static entry point for stand-alone EXEs. We just new up our module dstructure /// and then call into Ops to get running. /// </summary> internal static CodeGen GenerateModuleEntryPoint(TypeGen tg, CodeGen init, string moduleName, IList <string> referencedAssemblies) { CodeGen main = tg.DefineMethod(MethodAttributes.Static | MethodAttributes.Public, "Main", typeof(int), Type.EmptyTypes, new string[] { }); main.SetCustomAttribute(new CustomAttributeBuilder(typeof(STAThreadAttribute).GetConstructor(Type.EmptyTypes), new object[0])); // Create instance of the module Slot instance = main.GetLocalTmp(tg.myType); main.EmitNew(tg.DefaultConstructor); instance.EmitSet(main); // Emit instance for the ExecuteCompiled call instance.EmitGet(main); // Emit the delegate to the init method main.EmitDelegate(init, typeof(InitializeModule), instance); main.EmitString(moduleName); // emit the references assemblies if (referencedAssemblies != null) { for (int i = 0; i < referencedAssemblies.Count; i++) { if (referencedAssemblies[i].ToLower().EndsWith("\\ironpython.dll")) { referencedAssemblies.RemoveAt(i); i--; } else { if (referencedAssemblies[i].IndexOf(Path.DirectorySeparatorChar) != -1) { referencedAssemblies[i] = referencedAssemblies[i].Substring(referencedAssemblies[i].LastIndexOf(Path.DirectorySeparatorChar) + 1); } if (referencedAssemblies[i].ToLower().EndsWith(".dll")) { referencedAssemblies[i] = referencedAssemblies[i].Substring(0, referencedAssemblies[i].Length - 4); } } } main.EmitStringArray(referencedAssemblies); } else { main.Emit(OpCodes.Ldnull); } // Call ExecuteCompiled main.EmitCall(typeof(IronPython.Hosting.PythonEngine), "ExecuteCompiled", new Type[] { typeof(CustomFieldIdDict), typeof(InitializeModule), typeof(string), typeof(string[]) }); main.EmitReturn(); return(main); }
internal static TypeGen GenerateModuleType(string moduleName, AssemblyGen ag) { TypeGen tg = ag.DefinePublicType(moduleName, typeof(CustomFieldIdDict)); tg.AddModuleField(typeof(PythonModule)); tg.DefaultConstructor = tg.myType.DefineDefaultConstructor(MethodAttributes.Public); return(tg); }
public static FrameCode GenerateSnippet(CompilerContext context, Stmt body, string name, bool printExprStmts) { GlobalSuite gs = Binder.Bind(body, context); if (name.Length == 0) { name = "<empty>"; // The empty string isn't a legal method name } CodeGen cg; List <object> staticData = null; TypeGen tg = null; cg = snippetAssembly.DefineDynamicMethod(name, typeof(object), new Type[] { typeof(Frame) }); staticData = new List <object>(); cg.staticData = staticData; cg.doNotCacheConstants = true; cg.ModuleSlot = cg.GetLocalTmp(typeof(PythonModule)); cg.ContextSlot = cg.GetArgumentSlot(0); cg.Names = CodeGen.CreateFrameNamespace(cg.ContextSlot); cg.Context = context; cg.printExprStmts = printExprStmts; if (printExprStmts) { cg.Names.EnsureLocalSlot(Name.Make("_")); } cg.ContextSlot.EmitGet(cg); cg.EmitFieldGet(typeof(Frame), "__module__"); cg.ModuleSlot.EmitSet(cg); if (context.TrueDivision) { cg.ContextSlot.EmitGet(cg); cg.EmitInt(1); cg.EmitCall(typeof(ICallerContext), "set_TrueDivision"); } gs.Emit(cg); if (!(body is ReturnStmt)) { cg.EmitPosition(Location.None, Location.None); cg.EmitReturn(null); } if (tg != null) { tg.FinishType(); } FrameCode frameCode = new FrameCode(name, (FrameCodeDelegate)cg.CreateDelegate(typeof(FrameCodeDelegate)), staticData); return(frameCode); }
public TypeGen DefineNestedType(string name, Type parent) { TypeBuilder tb = myType.DefineNestedType(name, TypeAttributes.NestedPublic); tb.SetParent(parent); TypeGen ret = new TypeGen(myAssembly, tb); nestedTypeGens.Add(ret); ret.AddModuleField(typeof(PythonModule)); return(ret); }
protected override KeyValuePair <MethodBuilder, Type> CompileForSave(TypeGen typeGen) { var lambda = RewriteForSave(typeGen, _code); MethodBuilder mb = typeGen.TypeBuilder.DefineMethod(lambda.Name ?? "lambda_method", CompilerHelpers.PublicStatic | MethodAttributes.SpecialName); lambda.CompileToMethod(mb, false); mb.SetCustomAttribute(new CustomAttributeBuilder( typeof(CachedOptimizedCodeAttribute).GetConstructor(new Type[] { typeof(string[]) }), new object[] { _names } )); return(new KeyValuePair <MethodBuilder, Type>(mb, typeof(LookupCompilationDelegate))); }
private static PythonModule DoGenerateModule(SystemState state, CompilerContext context, GlobalSuite gs, string moduleName, string sourceFileName, string outSuffix) { string fullPath; string outDir; string fileName; if (sourceFileName == "<stdin>") { fullPath = Environment.CurrentDirectory; outDir = Environment.CurrentDirectory; fileName = "__stdin__"; } else { fullPath = Path.GetFullPath(sourceFileName); outDir = Options.BinariesDirectory == null?Path.GetDirectoryName(fullPath) : Options.BinariesDirectory; fileName = Path.GetFileNameWithoutExtension(sourceFileName); } AssemblyGen ag = new AssemblyGen(moduleName + outSuffix, outDir, fileName + outSuffix + ".exe", true); ag.SetPythonSourceFile(fullPath); TypeGen tg = GenerateModuleType(moduleName, ag); CodeGen cg = GenerateModuleInitialize(context, gs, tg); CodeGen main = GenerateModuleEntryPoint(tg, cg, moduleName, null); ag.SetEntryPoint(main.MethodInfo, PEFileKinds.ConsoleApplication); Type ret = tg.FinishType(); Assembly assm = ag.DumpAndLoad(); ret = assm.GetType(moduleName); CustomFieldIdDict dict = (CustomFieldIdDict)ret.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); InitializeModule init = (InitializeModule)Delegate.CreateDelegate( typeof(InitializeModule), dict, "Initialize"); PythonModule pmod = new PythonModule(moduleName, dict, state, init); pmod.InitializeBuiltins(); return(pmod); }
public CodeGen DefineDynamicMethod(string methodName, Type returnType, Type[] paramTypes) { CodeGen cg; if (Options.GenerateDynamicMethods) { DynamicMethod target = new DynamicMethod(methodName + "##" + index++, returnType, paramTypes, myModule); cg = new CodeGen(this, null, target, target.GetILGenerator(), paramTypes); //Console.WriteLine("----> {0} DynamicMethod", target.Name); } else { TypeGen tg = DefinePublicType("Type" + methodName + index++, typeof(object)); cg = tg.DefineUserHiddenMethod(MethodAttributes.Public | MethodAttributes.Static, "Handle", returnType, paramTypes); } return(cg); }
public DictBuilder(TypeGen tg, Namespace names) { this.tg = tg; this.names = names; }
private static void FinishCustomDict(TypeGen tg, Namespace ns) { DictBuilder db = new DictBuilder(tg, ns); db.AddCustomDictMethods(); }
internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg, CustomModuleInit customInit) { CodeGen ncg = tg.DefineUserHiddenMethod(MethodAttributes.Public, "Initialize", typeof(void), Type.EmptyTypes); ncg.Context = context; if (Options.StaticModules) { ncg.Names = CodeGen.CreateStaticFieldNamespace(tg); } else { throw new NotImplementedException(); //.names = new FieldNamespace(null, tg, new ModuleSlot(tg.myType)); } if (context.TrueDivision) { ncg.ModuleSlot.EmitGet(ncg); ncg.EmitInt(1); ncg.EmitCall(typeof(ICallerContext), "set_TrueDivision"); } // Add __doc__ and __name__ ncg.Names.CreateGlobalSlot(Name.Make("__doc__")); ncg.Names.CreateGlobalSlot(Name.Make("__name__")); string doc = gs.GetDocString(); ncg.EmitStringOrNull(doc); ncg.EmitSet(Name.Make("__doc__")); if (customInit != null) { customInit(ncg); } gs.Emit(ncg); ncg.EmitPosition(Location.None, Location.None); ncg.EmitReturn(); FinishCustomDict(tg, ncg.Names); return(ncg); }
internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg) { return(GenerateModuleInitialize(context, gs, tg, null)); }
public StaticFieldSlotFactory(TypeGen typeGen) { this.typeGen = typeGen; }
public FieldSlotFactory(TypeGen typeGen, Slot instance) { this.typeGen = typeGen; this.instance = instance; }