Exemplo n.º 1
0
        public static List <VeinModule> GetDeps()
        {
            var list = new List <VeinModule>();


            var stl = new VeinModuleBuilder("stl", new Version(2, 3));

            foreach (var type in VeinCore.All)
            {
                stl.InternTypeName(type.FullName);
                stl.InternString(type.Name);
                stl.InternString(type.Path);
                foreach (var field in type.Fields)
                {
                    stl.InternFieldName(field.FullName);
                    stl.InternString(field.Name);
                }
                foreach (var method in type.Methods)
                {
                    stl.InternString(method.Name);
                    foreach (var argument in method.Arguments)
                    {
                        stl.InternString(argument.Name);
                    }
                }
                stl.class_table.Add(type);
            }
            list.Add(stl);
            return(list);
        }
Exemplo n.º 2
0
        public void TestIL()
        {
            var module = new VeinModuleBuilder("xuy");
            var clazz  = module.DefineClass("global::vein/lang/svack_pidars");

            clazz.Flags = ClassFlags.Public | ClassFlags.Static;
            var method = clazz.DefineMethod("insert_dick_into_svack", MethodFlags.Public, VeinTypeCode.TYPE_VOID.AsClass(), ("x", VeinTypeCode.TYPE_STRING));

            method.Flags = MethodFlags.Public | MethodFlags.Static;
            var body = method.GetGenerator();

            body.Emit(OpCodes.LDC_I4_S, 1448);
            body.Emit(OpCodes.LDC_I4_S, 228);
            body.Emit(OpCodes.ADD);
            body.Emit(OpCodes.LDC_I4_S, 2);
            body.Emit(OpCodes.XOR);
            body.Emit(OpCodes.RESERVED_0);
            body.Emit(OpCodes.LDF, "x");
            body.Emit(OpCodes.RET);


            var body_module = module.BakeByteArray();


            var asm = new IshtarAssembly();

            asm.AddSegment((".code", body_module));

            //IshtarAssembly.WriteTo(asm, new DirectoryInfo(@"C:\Users\ls-mi\Desktop\"));
        }
Exemplo n.º 3
0
        public void ManualGenCallExternFunction()
        {
            var module = new VeinModuleBuilder("hello_world");
            var clazz  = module.DefineClass("hello_world%global::wave/lang/program");

            clazz.Flags = ClassFlags.Public | ClassFlags.Static;


            var method = clazz.DefineMethod("master", MethodFlags.Public | MethodFlags.Static,
                                            VeinTypeCode.TYPE_VOID.AsClass());
            var body = method.GetGenerator();

            var @while = body.DefineLabel();

            body.UseLabel(@while);
            body.Emit(OpCodes.NOP);
            body.Emit(OpCodes.NEWOBJ, VeinCore.StringClass);
            body.Emit(OpCodes.RESERVED_2);
            body.Emit(OpCodes.JMP, @while);
            body.Emit(OpCodes.RET);


            var body_module = module.BakeByteArray();


            var asm = new IshtarAssembly {
                Name = module.Name
            };

            asm.AddSegment((".code", body_module));

            IshtarAssembly.WriteTo(asm, new DirectoryInfo(@"C:\Users\ls-mi\Desktop\"));
            File.WriteAllText($@"C:\Users\ls-mi\Desktop\{module.Name}.wvil", module.BakeDebugString());
        }
Exemplo n.º 4
0
        public void AST2ILTest()
        {
            var w   = new VeinSyntax();
            var ast = w.CompilationUnit.ParseVein(
                " class Program { main(): void { if(ze()) return x; else { return d();  } } }");

            var module = new VeinModuleBuilder("foo");

            foreach (var member in ast.Members)
            {
                if (member is ClassDeclarationSyntax classMember)
                {
                    var @class = module.DefineClass($"global::vein/lang/{classMember.Identifier}");

                    foreach (var methodMember in classMember.Methods)
                    {
                        var method    = @class.DefineMethod(methodMember.Identifier.ExpressionString, VeinTypeCode.TYPE_VOID.AsClass());
                        var generator = method.GetGenerator();

                        foreach (var statement in methodMember.Body.Statements)
                        {
                            var st = statement;
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
    public static ILGenerator CreateGenerator(params VeinArgumentRef[] args)
    {
        var module = new VeinModuleBuilder(Guid.NewGuid().ToString());
        var @class = new ClassBuilder(module, $"{module.Name}%global::foo/bar");
        var method = @class.DefineMethod("foo", VeinTypeCode.TYPE_VOID.AsClass(), args);

        return(method.GetGenerator());
    }
Exemplo n.º 6
0
        public static ILGenerator CreateGenerator(params VeinArgumentRef[] args)
        {
            var module = new VeinModuleBuilder(Guid.NewGuid().ToString());
            var @class = module.DefineClass("global::foo/bar");
            var method = @class.DefineMethod("foo", VeinTypeCode.TYPE_VOID.AsClass(), args);

            var gen = method.GetGenerator();
            var ctx = new GeneratorContext();

            ctx.Module = module;
            ctx.Classes.Add(@class.FullName, @class);
            ctx.CurrentMethod = method;
            gen.StoreIntoMetadata("context", ctx);
            ctx.CreateScope();
            return(gen);
        }
Exemplo n.º 7
0
        public void Test()
        {
            var module = new VeinModuleBuilder("xuy");
            var clazz  = module.DefineClass("xuy%global::vein/lang/svack_pidars");

            clazz.Flags = ClassFlags.Public | ClassFlags.Static;
            var method = clazz.DefineMethod("insert_dick_into_svack", MethodFlags.Public, VeinTypeCode.TYPE_VOID.AsClass(), ("x", VeinTypeCode.TYPE_STRING));

            method.Flags = MethodFlags.Public | MethodFlags.Static;
            var gen = method.GetGenerator();

            var l1 = gen.DefineLabel();
            var l2 = gen.DefineLabel();

            gen.Emit(OpCodes.ADD);
            gen.Emit(OpCodes.LDC_I4_S, 228);
            gen.Emit(OpCodes.ADD);
            gen.Emit(OpCodes.JMP_HQ, l2);
            gen.Emit(OpCodes.LDC_I4_S, 228);
            gen.Emit(OpCodes.ADD);
            gen.Emit(OpCodes.LDC_I4_S, 228);
            gen.Emit(OpCodes.LDC_I4_S, 228);
            gen.Emit(OpCodes.ADD);
            gen.Emit(OpCodes.JMP_HQ, l1);
            gen.UseLabel(l1);
            gen.Emit(OpCodes.SUB);
            gen.Emit(OpCodes.SUB);
            gen.UseLabel(l2);
            gen.Emit(OpCodes.SUB);
            gen.Emit(OpCodes.SUB);


            module.BakeDebugString();


            //File.WriteAllText(@"C:\Users\ls-mi\Desktop\mana.il",
            //    module.BakeDebugString());

            var asm = new IshtarAssembly {
                Name = "woodo"
            };

            asm.AddSegment((".code", method.BakeByteArray()));

            //IshtarAssembly.WriteTo(asm, new DirectoryInfo(@"C:\Users\ls-mi\Desktop\"));
        }
Exemplo n.º 8
0
        public static void WriteTypeName(this BinaryWriter bin, QualityTypeName type, VeinModuleBuilder module)
        {
            var key = module.InternTypeName(type);

            bin.Write(key);
        }
Exemplo n.º 9
0
        public void WriteTest()
        {
            var verSR    = new Version(2, 2, 2, 2);
            var moduleSR = new VeinModuleBuilder("set1", verSR);
            {
                moduleSR.Deps.AddRange(GetDeps());


                var @class = moduleSR.DefineClass("set1%global::wave/lang/SR");


                @class.Flags = ClassFlags.Public | ClassFlags.Static;
                var method = @class.DefineMethod("blank", MethodFlags.Public | MethodFlags.Static,
                                                 VeinTypeCode.TYPE_VOID.AsClass());

                var gen = method.GetGenerator();

                gen.Emit(OpCodes.NOP);

                moduleSR.BakeByteArray();
                moduleSR.BakeDebugString();

                var blank = new IshtarAssembly(moduleSR)
                {
                    Name = "set1", Version = verSR
                };


                IshtarAssembly.WriteTo(blank, new DirectoryInfo("C:/wavelib"));
            }


            {
                var ver    = new Version(2, 2, 2, 2);
                var module = new VeinModuleBuilder("set2", ver);
                module.Deps.AddRange(GetDeps());


                var @class = module.DefineClass("set2%global::wave/lang/DR");


                @class.Flags = ClassFlags.Public | ClassFlags.Static;
                var method = @class.DefineMethod("blank", MethodFlags.Public | MethodFlags.Static,
                                                 VeinTypeCode.TYPE_VOID.AsClass());

                var gen = method.GetGenerator();

                gen.Emit(OpCodes.NOP);

                module.BakeByteArray();
                module.BakeDebugString();

                module.Deps.Add(moduleSR);

                var blank = new IshtarAssembly(module)
                {
                    Name = "set2", Version = ver
                };


                IshtarAssembly.WriteTo(blank, new DirectoryInfo("C:/wavelib"));
            }
        }
Exemplo n.º 10
0
        public void ManualGen()
        {
            var module = new VeinModuleBuilder("satl");
            var clazz  = module.DefineClass("satl%global::wave/lang/program");

            clazz.Flags = ClassFlags.Public | ClassFlags.Static;


            var fib = clazz.DefineMethod("fib",
                                         MethodFlags.Public | MethodFlags.Static,
                                         VeinTypeCode.TYPE_I8.AsClass(), ("x", VeinTypeCode.TYPE_I8));

            var fibGen = fib.GetGenerator();

            //fibGen.Emit(OpCodes.LDC_I8_1);
            //fibGen.Emit(OpCodes.LDC_I8_1);
            //fibGen.Emit(OpCodes.EQL);
            //fibGen.Emit(OpCodes.RET);

            var label_if_1 = fibGen.DefineLabel();
            var label_if_2 = fibGen.DefineLabel();
            var for_1      = fibGen.DefineLabel();
            var for_body   = fibGen.DefineLabel();

            // if (x == 0) return 0;
            fibGen.Emit(OpCodes.LDARG_1);
            fibGen.Emit(OpCodes.JMP_T, label_if_1);
            fibGen.Emit(OpCodes.LDC_I4_0);
            fibGen.Emit(OpCodes.RET);
            fibGen.UseLabel(label_if_1);
            // if (x == 1) return 1;
            fibGen.Emit(OpCodes.LDARG_1);
            fibGen.Emit(OpCodes.LDC_I4_1);
            fibGen.Emit(OpCodes.JMP_NN, label_if_2);
            fibGen.Emit(OpCodes.LDC_I4_1);
            fibGen.Emit(OpCodes.RET);
            fibGen.UseLabel(label_if_2);
            // var first, second, nth, i = 0;
            fibGen.EnsureLocal("first", VeinTypeCode.TYPE_I4.AsClass());
            fibGen.EnsureLocal("second", VeinTypeCode.TYPE_I4.AsClass());
            fibGen.EnsureLocal("nth", VeinTypeCode.TYPE_I4.AsClass());
            fibGen.EnsureLocal("i", VeinTypeCode.TYPE_I4.AsClass());
            // second, nth = 1; i = 2;
            fibGen.Emit(OpCodes.LDC_I4_1); fibGen.Emit(OpCodes.STLOC_1);
            fibGen.Emit(OpCodes.LDC_I4_1); fibGen.Emit(OpCodes.STLOC_2);
            fibGen.Emit(OpCodes.LDC_I4_2); fibGen.Emit(OpCodes.STLOC_3);

            // for
            //
            fibGen.Emit(OpCodes.JMP, for_1);
            fibGen.UseLabel(for_body);
            fibGen.Emit(OpCodes.LDLOC_0);
            fibGen.Emit(OpCodes.LDLOC_1);
            fibGen.Emit(OpCodes.ADD);
            fibGen.Emit(OpCodes.STLOC_2);

            fibGen.Emit(OpCodes.LDLOC_1);
            fibGen.Emit(OpCodes.STLOC_0);

            fibGen.Emit(OpCodes.LDLOC_2);
            fibGen.Emit(OpCodes.STLOC_1);

            // i++
            fibGen.Emit(OpCodes.LDLOC_3);
            fibGen.Emit(OpCodes.LDC_I4_1);
            fibGen.Emit(OpCodes.ADD);
            fibGen.Emit(OpCodes.STLOC_3);

            // var exceptionType =
            //    module.FindType(new QualityTypeName("std%global::wave/lang/Exception")).AsClass();

            //fibGen.Emit(OpCodes.NEWOBJ, exceptionType.FullName);
            //fibGen.Emit(OpCodes.CALL, exceptionType.FindMethod("ctor()"));
            //fibGen.Emit(OpCodes.THROW);


            // i <= n
            fibGen.UseLabel(for_1);
            fibGen.Emit(OpCodes.NOP);
            fibGen.Emit(OpCodes.LDARG_1);
            fibGen.Emit(OpCodes.LDLOC_3);
            fibGen.Emit(OpCodes.JMP_LQ, for_body);
            // return nth;
            fibGen.Emit(OpCodes.LDLOC_2);
            fibGen.Emit(OpCodes.RET);

            var method = clazz.DefineMethod("master", MethodFlags.Public | MethodFlags.Static,
                                            VeinTypeCode.TYPE_VOID.AsClass());
            var body = method.GetGenerator();



            body.Emit(OpCodes.LDC_I4_S, 120 /**/);
            body.Emit(OpCodes.CALL, fib);
            body.Emit(OpCodes.RESERVED_0);
            body.Emit(OpCodes.RESERVED_1);
            body.Emit(OpCodes.RET);
            //body.Emit(OpCodes.CALL);


            var body_module = module.BakeByteArray();


            var asm = new IshtarAssembly {
                Name = module.Name
            };

            asm.AddSegment((".code", body_module));

            IshtarAssembly.WriteTo(asm, new DirectoryInfo(@"C:\Users\ls-mi\Desktop\"));
            File.WriteAllText($@"C:\Users\ls-mi\Desktop\{module.Name}.wvil", module.BakeDebugString());
        }