Beispiel #1
0
        private void EmitWriteArrayLoop(Compiler.CompilerContext ctx, Compiler.Local i, Compiler.Local arr)
        {
            // i = 0
            ctx.LoadValue(0);
            ctx.StoreValue(i);

            // range test is last (to minimise branches)
            Compiler.CodeLabel loopTest = ctx.DefineLabel(), processItem = ctx.DefineLabel();
            ctx.Branch(loopTest, false);
            ctx.MarkLabel(processItem);

            // {...}
            ctx.LoadArrayValue(arr, i);
            if (SupportNull)
            {
                Tail.EmitWrite(ctx, null);
            }
            else
            {
                ctx.WriteNullCheckedTail(itemType, Tail, null);
            }

            // i++
            ctx.LoadValue(i);
            ctx.LoadValue(1);
            ctx.Add();
            ctx.StoreValue(i);

            // i < arr.Length
            ctx.MarkLabel(loopTest);
            ctx.LoadValue(i);
            ctx.LoadLength(arr, false);
            ctx.BranchIfLess(processItem, false);
        }
Beispiel #2
0
        protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            using (Compiler.Local oldValue = ctx.GetLocalWithValue(expectedType, valueFrom))
                using (Compiler.Local token = new Compiler.Local(ctx, ctx.MapType(typeof(SubItemToken))))
                    using (Compiler.Local field = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
                    {
                        ctx.LoadReaderWriter();
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("StartSubItem"));
                        ctx.StoreValue(token);

                        Compiler.CodeLabel next = ctx.DefineLabel(), processField = ctx.DefineLabel(), end = ctx.DefineLabel();

                        ctx.MarkLabel(next);

                        ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int)));
                        ctx.CopyValue();
                        ctx.StoreValue(field);
                        ctx.LoadValue(Tag); // = 1 - process
                        ctx.BranchIfEqual(processField, true);
                        ctx.LoadValue(field);
                        ctx.LoadValue(1); // < 1 - exit
                        ctx.BranchIfLess(end, false);

                        // default: skip
                        ctx.LoadReaderWriter();
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField"));
                        ctx.Branch(next, true);

                        // process
                        ctx.MarkLabel(processField);
                        if (Tail.RequiresOldValue)
                        {
                            if (expectedType.IsValueType)
                            {
                                ctx.LoadAddress(oldValue, expectedType);
                                ctx.EmitCall(expectedType.GetMethod("GetValueOrDefault", Helpers.EmptyTypes));
                            }
                            else
                            {
                                ctx.LoadValue(oldValue);
                            }
                        }
                        Tail.EmitRead(ctx, null);
                        // note we demanded always returns a value
                        if (expectedType.IsValueType)
                        {
                            ctx.EmitCtor(expectedType, Tail.ExpectedType); // re-nullable<T> it
                        }
                        ctx.StoreValue(oldValue);
                        ctx.Branch(next, false);

                        // outro
                        ctx.MarkLabel(end);

                        ctx.LoadValue(token);
                        ctx.LoadReaderWriter();
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("EndSubItem"));
                        ctx.LoadValue(oldValue); // load the old value
                    }
        }