Beispiel #1
0
        public virtual void Emit(IlBuilder builder)
        {
            builder.EmitString(".method");
            foreach (string flag in Flags)
            {
                builder.EmitString(" " + flag);
            }
            builder.EmitLine($" {ReturnType.Name} {Name}({ArgList}) cil managed");
            builder.EmitLine("{");

            if (!IsAbstract && Instructions.Count > 0)
            {
                builder.Output.AddIndentation();

                //TODO: Allow for selection of entrypoint
                if (Name == "Main")
                {
                    builder.EmitLine(".entrypoint");
                }

                int startLoc = builder.Output.ReserveLocation();

                if (Locals.Count > 0)
                {
                    string types = "";
                    foreach (var t in Locals)
                    {
                        types += t.Type.Name + ",";
                    }
                    builder.EmitLine(".locals init (" + types[..^ 1] + ")");
Beispiel #2
0
        public override void Emit(IlBuilder builder)
        {
            List <string> metadata = new List <string>();

            if (Properties.ContainsKey("Version"))
            {
                metadata.Add($".ver {Properties["Version"].Replace('.', ':')}");
            }
            if (Properties.ContainsKey("PublicKeyToken"))
            {
                metadata.Add($".publickeytoken = ({Properties["PublicKeyToken"].ToUpper()})");
            }


            if (metadata.Count == 0)
            {
                builder.EmitLine($".assembly extern {Name}{{}}");
            }
            else
            {
                builder.EmitLine($".assembly extern {Name}");
                builder.EmitLine("{");
                builder.Output.AddIndentation();
                foreach (string m in metadata)
                {
                    builder.EmitLine(m);
                }
                builder.Output.ReduceIndentation();
                builder.EmitLine("}");
            }
        }
Beispiel #3
0
        public override void Emit(IlBuilder builder)
        {
            base.Emit(builder);
            _symbolConversion?.Emit(builder);

            if (_method.IsInstance)
            {
                if (((Owner == null || Owner.IsStatic) && _thisPtr == null) || _staticCall)
                {
                    ErrorManager.ExitWithError(new Exception("Can't call instance function from static context"));
                }


                if (!_valueType)
                {
                    builder.PushValue(_thisPtr ?? Owner.ThisPointer);
                }
                else
                {
                    builder.PushAddress(_thisPtr as CodeSymbol);
                }
            }

            foreach (var p in _parameters)
            {
                builder.PushValue(p);
            }

            if (_method.IsVirtual && !_baseAccess)
            {
                if (_valueType)
                {
                    //NOTE: This is sometimes emitted while not needed, like with System.Int32.ToString(), as these methods appear
                    //as virtual but actually aren't. I don't know why, but it still works so whatever.
                    builder.EmitLine("constrained. " + _thisPtr.Type.Name);
                }

                builder.EmitOpCode(OpCodes.Callvirt, _method.FullSignature);
            }
            else
            {
                builder.EmitOpCode(OpCodes.Call, _method.FullSignature);
            }

            builder.ShrinkStack(_parameters.Length);

            if (_return != CodeType.Void)
            {
                if (!_expression)
                {
                    builder.EmitOpCode(OpCodes.Pop);
                }
                else
                {
                    builder.ExpandStack(1);
                }
            }
        }
Beispiel #4
0
        public void Emit(IlBuilder builder)
        {
            string instance = IsStatic ? "" : "instance ";

            builder.EmitLine($".property {instance}{Type.Name} {Name} ()");
            builder.EmitLine("{");
            builder.Output.AddIndentation();

            if (Get != null)
            {
                builder.EmitLine(".get " + Get.CallSignature);
            }

            if (Set != null)
            {
                builder.EmitLine(".set " + Set.CallSignature);
            }

            builder.Output.ReduceIndentation();
            builder.EmitLine("}");
        }
Beispiel #5
0
        public override void Emit(IlBuilder builder)
        {
            base.Emit(builder);
            if (_opening)
            {
                if (_type == null)
                {
                    builder.EmitLine(_name);
                }
                else
                {
                    builder.EmitLine(_name + " " + _type.ShortName);
                }

                builder.EmitLine("{");
                builder.Output.AddIndentation();
            }
            else
            {
                builder.Output.ReduceIndentation();
                builder.EmitLine("}");
            }
        }
Beispiel #6
0
        public void Emit(IlBuilder builder)
        {
            string genericParams =
                IsGeneric ? '<' + string.Join(',', NamespaceContext.GenericParameters.Keys) + '>' : "";

            builder.EmitLine($".class private {string.Join(' ', Flags)} auto ansi beforefieldinit {FullName}{genericParams} extends {BaseType.ShortName}");
            builder.EmitLine("{");
            builder.EmitLine();
            builder.Output.AddIndentation();

            foreach (var field in Fields)
            {
                field.Emit(builder);
            }

            foreach (var property in Properties)
            {
                property.Emit(builder);
            }

            builder.EmitLine();

            Initializer.Emit(builder);

            foreach (var c in Constructors)
            {
                c.Emit(builder);
            }

            foreach (var method in Methods)
            {
                method.Emit(builder);
            }

            builder.Output.ReduceIndentation();
            builder.EmitLine("}");
            builder.EmitLine("");
        }
Beispiel #7
0
 public void Emit(IlBuilder builder)
 {
     builder.EmitLine($".field {Access} {string.Join(" ", Keywords)} {Type.Name} {Name}");
 }