Exemple #1
0
 public void EmitInitializers(ILRunner pRunner)
 {
     if (StructType.HasInitializer)
     {
         StructType.Initializer.Emit(pRunner);
     }
 }
Exemple #2
0
 public override void Emit(ILRunner pRunner)
 {
     foreach (var s in Statements)
     {
         s.Emit(pRunner);
     }
 }
Exemple #3
0
        public override void Emit(ILRunner pRunner)
        {
            Condition.Emit(pRunner);
            Label end = pRunner.Emitter.DefineLabel();

            if (Else != null)
            {
                //If the condition is not true, jump to else
                Label e = pRunner.Emitter.DefineLabel();
                pRunner.Emitter.Emit(OpCodes.Brfalse, e);

                Body.Emit(pRunner);
                if (!ReturnsInBody)
                {
                    pRunner.Emitter.Emit(OpCodes.Br, end);
                }

                pRunner.Emitter.MarkLabel(e);
                Else.Emit(pRunner);
            }
            else
            {
                if (!ReturnsInBody)
                {
                    pRunner.Emitter.Emit(OpCodes.Brfalse, end);
                }
                Body.Emit(pRunner);
            }

            if (!ReturnsInBody)
            {
                pRunner.Emitter.MarkLabel(end);
            }
        }
Exemple #4
0
 public void Emit(ILRunner pRunner)
 {
     pRunner.OverrideGenerator = _builder.GetILGenerator();
     _block.Emit(pRunner);
     pRunner.Emitter.Emit(OpCodes.Ret);
     pRunner.OverrideGenerator = null;
 }
Exemple #5
0
        public override void Emit(ILRunner pRunner)
        {
            foreach (var s in Structs)
            {
                s.Emit(pRunner);
            }

            foreach (var f in Methods)
            {
                f.EmitDefinition(pRunner);
            }
            foreach (var c in Casts)
            {
                c.EmitDefinition(pRunner);
            }

            foreach (var s in Structs)
            {
                s.EmitInitializers(pRunner);
                s.StructType.Create();
            }

            foreach (var f in Methods)
            {
                f.Emit(pRunner);
            }
            foreach (var c in Casts)
            {
                c.Emit(pRunner);
            }
            pRunner.Finish();
        }
        public override void Emit(ILRunner pRunner)
        {
            switch (NumberType)
            {
            case NumberType.I16:
                pRunner.Emitter.Emit(OpCodes.Ldc_I4, short.Parse(Value));
                break;

            case NumberType.I32:
                pRunner.EmitInt(int.Parse(Value));
                break;

            case NumberType.I64:
                pRunner.Emitter.Emit(OpCodes.Ldc_R8, long.Parse(Value));
                break;

            case NumberType.Double:
                pRunner.Emitter.Emit(OpCodes.Ldc_R8, double.Parse(Value));
                break;

            case NumberType.Float:
                pRunner.Emitter.Emit(OpCodes.Ldc_R4, float.Parse(Value));
                break;
            }
        }
Exemple #7
0
        public override void Emit(ILRunner pRunner)
        {
            foreach (var v in Values)
            {
                v.Emit(pRunner);
            }
            if (Values.Count > 1)
            {
                SmallType[] types = new SmallType[Values.Count];
                for (int i = 0; i < Values.Count; i++)
                {
                    types[i] = Values[i].Type;
                }
                var t = SmallType.CreateTupleOf(types).ToSystemType();

                Type[] systemTypes = new Type[types.Length];
                for (int i = 0; i < types.Length; i++)
                {
                    systemTypes[i] = types[i].ToSystemType();
                }
                var c = t.GetConstructor(systemTypes);
                pRunner.Emitter.Emit(OpCodes.Newobj, c);
            }

            pRunner.Emitter.Emit(OpCodes.Ret);
        }
Exemple #8
0
 public override void Emit(ILRunner pRunner)
 {
     Left.Emit(pRunner);
     Right.Emit(pRunner);
     pRunner.Emitter.Emit(OpCodes.Clt);
     pRunner.EmitBool(false);
     pRunner.Emitter.Emit(OpCodes.Ceq);
 }
Exemple #9
0
 public override void Emit(ILRunner pRunner)
 {
     Name.PreEmitForAssignment(pRunner);
     Name.Emit(pRunner);
     Value.Emit(pRunner);
     pRunner.Emitter.Emit(OpCodes.Mul);
     Name.PostEmitForAssignment(pRunner);
 }
Exemple #10
0
 public override void Emit(ILRunner pRunner)
 {
     Value.LoadAddress = IsRef;
     Value.Emit(pRunner);
     if (CreateCopy)
     {
         pRunner.Emitter.Emit(OpCodes.Ldind_Ref);
     }
 }
Exemple #11
0
        public override void Emit(ILRunner pRunner)
        {
            var constructor = typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int) });

            pRunner.EmitInt(_year);
            pRunner.EmitInt(_month);
            pRunner.EmitInt(_day);
            pRunner.Emitter.Emit(OpCodes.Call, constructor);
        }
        public override void Emit(ILRunner pRunner)
        {
            Left.Emit(pRunner);
            Right.Emit(pRunner);

            var m = typeof(Math).GetMethod("Pow", new Type[] { typeof(double), typeof(double) });

            pRunner.Emitter.Emit(OpCodes.Call, m);
        }
        public override void Emit(ILRunner pRunner)
        {
            MethodDefinition def = MetadataCache.GetMethod(this);

            foreach (var a in Arguments)
            {
                a.Emit(pRunner);
            }

            if (def.Name == "ToString")
            {
                Type[] types = new Type[Arguments.Count];
                for (int i = 0; i < Arguments.Count; i++)
                {
                    types[i] = Arguments[i].Type.ToSystemType();
                }
                var m = InstanceType.GetMethod(Value, types);
                pRunner.Emitter.Emit(OpCodes.Callvirt, m);
            }
            else
            {
                if (TypeParameters.Count == 0)
                {
                    pRunner.Emitter.Emit(OpCodes.Call, def.CallSite);
                }
                else
                {
                    Type[] types = new Type[TypeParameters.Count];
                    for (int i = 0; i < TypeParameters.Count; i++)
                    {
                        types[i] = SmallType.FromString("", TypeParameters[i]).ToSystemType();
                        if (types[i] == null)
                        {
                            var t = pRunner.CurrentMethod.TypeHints;
                            for (int j = 0; j < t.Count; j++)
                            {
                                if (TypeParameters[i].Equals(t[j].Name, StringComparison.OrdinalIgnoreCase))
                                {
                                    types[i] = t[j].ToSystemType();
                                }
                            }
                        }
                    }
                    var cs = def.CallSite.MakeGenericMethod(types);
                    pRunner.Emitter.Emit(OpCodes.Call, cs);
                }
            }

            if (def.ReturnTypes.Length > 0)
            {
                //TODO Should move to some sort of visitor? This will fail eventually if I allow struct functions
                if (Parent.GetType() == typeof(BlockSyntax) || !string.IsNullOrEmpty(Namespace) && Parent.Parent.GetType() == typeof(BlockSyntax))
                {
                    pRunner.Emitter.Emit(OpCodes.Pop);
                }
            }
        }
Exemple #14
0
 public override void Emit(ILRunner pRunner)
 {
     if (Name.Kind == SyntaxKind.ArrayAccessExpression)
     {
         Name.LoadAddress = true;
     }
     Name.Emit(pRunner);
     Value.Emit(pRunner);
 }
Exemple #15
0
 public LocalBuilder GetLocal(ILRunner pRunner)
 {
     if (!_created)
     {
         _lb      = pRunner.CreateLocal(Value, Type);
         _created = true;
     }
     return(_lb);
 }
Exemple #16
0
 public override void PreEmitForAssignment(ILRunner pRunner)
 {
     LoadAddress = true;
     Identifier.Emit(pRunner);
     if (Identifier.Local.IsAddress)
     {
         pRunner.Emitter.Emit(OpCodes.Ldind_Ref);
     }
     Index.Emit(pRunner);
 }
        public override void Emit(ILRunner pRunner)
        {
            var i = (IdentifierSyntax)Value;

            i.PreEmitForAssignment(pRunner);
            Value.Emit(pRunner);
            pRunner.EmitInt(1);
            pRunner.Emitter.Emit(OpCodes.Add);
            i.PostEmitForAssignment(pRunner);
        }
Exemple #18
0
        public override void Emit(ILRunner pRunner)
        {
            Left.Emit(pRunner);
            Right.Emit(pRunner);

            var a = Assembly.Load(new AssemblyName("mscorlib"));
            var t = a.GetType("System.String");
            var m = t.GetMethod("Concat", new Type[] { typeof(string), typeof(string) });

            pRunner.Emitter.Emit(OpCodes.Call, m);
        }
Exemple #19
0
 public override void Emit(ILRunner pRunner)
 {
     pRunner.SetCurrentMethod(_def);
     if (_def.ExternMethod == null)
     {
         Body.Emit(pRunner);
         if (_def.ReturnTypes.Length == 0)
         {
             pRunner.Emitter.Emit(OpCodes.Ret);
         }
     }
 }
Exemple #20
0
 public virtual void PostEmitForAssignment(ILRunner pRunner)
 {
     if (!Local.Field && !LoadAddress)
     {
         var lb = Local.GetLocal(pRunner);
         pRunner.Emitter.Emit(OpCodes.Stloc, lb);
     }
     else if (Local.Field)
     {
         pRunner.Emitter.Emit(OpCodes.Stfld, Local.GetField());
     }
 }
Exemple #21
0
        public void Emit(ILRunner pRunner, IdentifierSyntax pNode)
        {
            if (Parameter)
            {
                var pb = pRunner.GetParameter(Value, out short i);
                if (pb == null)
                {
                    throw new InvalidOperationException("Invalid var");
                }

                if (pNode.LoadAddress && !IsAddress)
                {
                    pRunner.Emitter.Emit(OpCodes.Ldarga_S, i);
                }
                else
                {
                    pRunner.Emitter.Emit(OpCodes.Ldarg, i);
                }
            }
            else if (Field)
            {
                var fd = Type.GetField(Value);
                if (fd == null)
                {
                    throw new InvalidOperationException("Invalid var");
                }

                pRunner.Emitter.Emit(OpCodes.Ldarg_0);
                pRunner.Emitter.Emit(OpCodes.Ldfld, fd.Type.ToSystemType());
            }
            else
            {
                if (!_created)
                {
                    _lb      = pRunner.CreateLocal(Value, Type);
                    _created = true;
                }
                if (_lb == null)
                {
                    throw new InvalidOperationException("Invalid var");
                }

                if (pNode.LoadAddress)
                {
                    pRunner.Emitter.Emit(OpCodes.Ldloca_S, _lb);
                }
                else
                {
                    pRunner.Emitter.Emit(OpCodes.Ldloc, _lb);
                }
            }
        }
Exemple #22
0
        public override void Emit(ILRunner pRunner)
        {
            Name.PreEmitForAssignment(pRunner);
            Name.Emit(pRunner);
            Value.Emit(pRunner);

            var a = Assembly.Load(new AssemblyName("mscorlib"));
            var t = a.GetType("System.String");
            var m = t.GetMethod("Concat", new Type[] { typeof(string), typeof(string) });

            pRunner.Emitter.Emit(OpCodes.Call, m);

            Name.PostEmitForAssignment(pRunner);
        }
Exemple #23
0
        public void Run(string pPath, CompilationOptions pOptions)
        {
            if (!System.IO.Directory.Exists(pOptions.OutputPath))
            {
                ReportError(CompilerErrorType.InvalidPath, new TextSpan(), pOptions.OutputPath);
                PrintAllErrors("");
                return;
            }

            var sw = new Stopwatch();

            sw.Start();
            _path = pPath;
            _errors.Clear();
            MetadataCache.Clear();

            string text     = System.IO.File.ReadAllText(pPath);
            var    p        = Parser.Create(new Lexing.SmallLangDefinition());
            var    ilRunner = new ILRunner(pOptions);
            var    ws       = p.Parse(text);

            if (PrintAllErrors(text))
            {
                return;
            }

            ws = ws.Accept <Syntax.WorkspaceSyntax>(new GroupAssignmentSyntaxRewriter());
            ws.Accept(new DefinitionVisitor(pOptions));
            ws = ws.Accept <Syntax.WorkspaceSyntax>(new ReferenceBinderRewriter());

            if (PrintAllErrors(text))
            {
                return;
            }

            ws.Accept(new TypeInferenceVisitor());
            ws.Accept(new Typer());
            ws.Accept(new ValidationVisitor());

            if (PrintAllErrors(text))
            {
                return;
            }

            ws.Emit(ilRunner);

            sw.Stop();
            Console.WriteLine("Compiled " + _path + " in " + sw.ElapsedMilliseconds.ToString() + "ms");
        }
Exemple #24
0
        public override void Emit(ILRunner pRunner)
        {
            Label start = pRunner.Emitter.DefineLabel();

            pRunner.Emitter.MarkLabel(start);
            Condition.Emit(pRunner);

            Label end = pRunner.Emitter.DefineLabel();

            pRunner.Emitter.Emit(OpCodes.Brfalse, end);

            Body.Emit(pRunner);
            pRunner.Emitter.Emit(OpCodes.Br, start);
            pRunner.Emitter.MarkLabel(end);
        }
Exemple #25
0
 public override void Emit(ILRunner pRunner)
 {
     Identifier.Emit(pRunner);
     if (Identifier.Local.IsAddress)
     {
         pRunner.Emitter.Emit(OpCodes.Ldind_Ref);
     }
     Index.Emit(pRunner);
     if (Type.IsValueType && LoadAddress)
     {
         pRunner.Emitter.Emit(OpCodes.Ldelema, Type.ToSystemType());
     }
     else
     {
         pRunner.Emitter.Emit(OpCodes.Ldelem, Type.ToSystemType());
     }
 }
Exemple #26
0
        public override void Emit(ILRunner pRunner)
        {
            Identifier.PreEmitForAssignment(pRunner);
            Value.Emit(pRunner);
            Identifier.PostEmitForAssignment(pRunner);
            if (Identifier.Type.HasInitializer && Value.GetType() == typeof(ObjectInitializerExpressionSyntax))
            {
                if (Identifier.GetType() == typeof(MemberAccessSyntax))
                {
                    pRunner.Emitter.Emit(OpCodes.Ldloca_S, ((MemberAccessSyntax)Identifier).Value.Local.GetLocal(pRunner));
                }
                else
                {
                    pRunner.Emitter.Emit(OpCodes.Ldloca_S, Identifier.Local.GetLocal(pRunner));
                }

                pRunner.Emitter.Emit(OpCodes.Call, Value.Type.Initializer.Info);
            }
        }
Exemple #27
0
 public virtual void PreEmitForAssignment(ILRunner pRunner)
 {
     if (LoadAddress)
     {
         if (!Local.Parameter)
         {
             var lb = Local.GetLocal(pRunner);
             pRunner.Emitter.Emit(OpCodes.Ldloca_S, lb);
         }
         else
         {
             var pb = Local.GetParameter(pRunner);
             pRunner.Emitter.Emit(OpCodes.Ldarg, pb);
         }
     }
     else if (Local.Field)
     {
         pRunner.Emitter.Emit(OpCodes.Ldarg_0);
     }
 }
        public override void Emit(ILRunner pRunner)
        {
            Label end     = pRunner.Emitter.DefineLabel();
            Label lbFalse = pRunner.Emitter.DefineLabel();
            Label lbTrue  = pRunner.Emitter.DefineLabel();

            Left.Emit(pRunner);
            pRunner.Emitter.Emit(OpCodes.Brfalse_S, lbFalse);

            Right.Emit(pRunner);
            pRunner.Emitter.Emit(OpCodes.Brtrue_S, lbTrue);

            pRunner.Emitter.MarkLabel(lbFalse);
            pRunner.EmitBool(false);
            pRunner.Emitter.Emit(OpCodes.Br_S, end);

            pRunner.Emitter.MarkLabel(lbTrue);
            pRunner.EmitBool(true);
            pRunner.Emitter.MarkLabel(end);
        }
Exemple #29
0
        public override void PostEmitForAssignment(ILRunner pRunner)
        {
            var t = Struct.Type;

            if (t.IsArray)
            {
                t = t.GetElementType();
            }
            var type = t.ToSystemType();

            System.Reflection.FieldInfo f = null;
            if (IsTypeBuilder(type))
            {
                f = TypeBuilder.GetField(type, type.GetGenericTypeDefinition().GetField(Value));
            }
            else
            {
                f = type.GetField(Value);
            }
            pRunner.Emitter.Emit(OpCodes.Stfld, f);
        }
 public override void Emit(ILRunner pRunner)
 {
     if (Type == Value.Type)
     {
         Value.Emit(pRunner);
     }
     else if (Type == SmallType.Object)
     {
         pRunner.Emitter.Emit(OpCodes.Box, Value.Type.ToSystemType());
     }
     else
     {
         Value.Emit(pRunner);
         if (Type == SmallType.I16)
         {
             pRunner.Emitter.Emit(OpCodes.Conv_I2);
         }
         else if (Type == SmallType.I32)
         {
             pRunner.Emitter.Emit(OpCodes.Conv_I4);
         }
         else if (Type == SmallType.I64)
         {
             pRunner.Emitter.Emit(OpCodes.Conv_I8);
         }
         else if (Type == SmallType.Double)
         {
             pRunner.Emitter.Emit(OpCodes.Conv_R8);
         }
         else if (Type == SmallType.Float)
         {
             pRunner.Emitter.Emit(OpCodes.Conv_R4);
         }
         else
         {
             var def = MetadataCache.GetCast(Value.Type, Type);
             pRunner.Emitter.Emit(OpCodes.Call, def.CallSite);
         }
     }
 }