Bump corefx
[mono-project.git] / mono / tests / codegen2.cs
blobf914f93f6c96c3824838ce6e8ed7c0df42becbc3
1 using System;
2 using System.Reflection;
3 using System.Reflection.Emit;
5 class CGen {
7 public static int Main() {
8 AssemblyBuilder abuilder;
9 ModuleBuilder mbuilder;
10 TypeBuilder tbuilder, tb2;
11 FieldBuilder fbuilder;
12 PropertyBuilder pbuilder;
13 ILGenerator ilg;
14 AssemblyName an;
15 String name = "tcgen.exe";
16 MethodBuilder get_method, entryp;
17 TypeAttributes attrs = TypeAttributes.Public | TypeAttributes.Class;
18 MethodAttributes mattrs = MethodAttributes.Public | MethodAttributes.Static;
20 an = new AssemblyName ();
21 an.Name = name;
22 abuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save);
24 mbuilder = abuilder.DefineDynamicModule (name, name);
26 tbuilder = mbuilder.DefineType ("Test.CodeGen", attrs);
27 Type result = typeof(int);
28 Type[] param = new Type[] {typeof (String[])};
29 entryp = tbuilder.DefineMethod("Main", mattrs, result, param);
30 ilg = entryp.GetILGenerator (128);
31 ilg.DeclareLocal (typeof(int));
32 Label fail = ilg.DefineLabel ();
33 ilg.Emit (OpCodes.Ldc_I4_2);
34 ilg.Emit (OpCodes.Dup);
35 ilg.Emit (OpCodes.Add);
36 ilg.Emit (OpCodes.Stloc_0);
37 ilg.Emit (OpCodes.Ldc_I4_4);
38 ilg.Emit (OpCodes.Ldloc_0);
39 ilg.Emit (OpCodes.Sub);
40 ilg.Emit (OpCodes.Brfalse, fail);
41 ilg.Emit (OpCodes.Ldc_I4_1);
42 ilg.Emit (OpCodes.Ret);
43 ilg.MarkLabel (fail);
44 ilg.Emit (OpCodes.Ldc_I4_0);
45 ilg.Emit (OpCodes.Ret);
48 fbuilder = tbuilder.DefineField ("int_field", typeof(int), FieldAttributes.Private);
49 pbuilder = tbuilder.DefineProperty ("FieldI", PropertyAttributes.None, typeof(int), null);
50 get_method = tbuilder.DefineMethod("get_FieldI", MethodAttributes.Public, result, null);
51 ilg = get_method.GetILGenerator (128);
52 ilg.Emit (OpCodes.Ldloc_0);
53 ilg.Emit (OpCodes.Ldloc_0);
54 ilg.Emit (OpCodes.Ceq);
55 ilg.Emit (OpCodes.Ret);
56 pbuilder.SetGetMethod (get_method);
58 Type t = tbuilder.CreateType ();
59 abuilder.SetEntryPoint (entryp);
60 abuilder.Save (name);
61 return 0;