예제 #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);
        }
		private void EmitReadEndPacked(Compiler.CompilerContext ctx, Compiler.Local list, Compiler.Local oldArr,
										Compiler.Local newArr, Type listType)
		{
			// leave this "using" here, as it can share the "FieldNumber" local with EmitReadList
			using(Compiler.Local oldLen = AppendToCollection ? new ProtoBuf.Compiler.Local(ctx, typeof(int)) : null) {
				Type[] copyToArrayInt32Args = new Type[] { typeof(Array), typeof(int) };

				if (AppendToCollection)
				{
					ctx.LoadLength(oldArr, true);
					ctx.CopyValue();
					ctx.StoreValue(oldLen);

					ctx.LoadAddress(list, listType);
					ctx.LoadValue(listType.GetProperty("Count"));
					ctx.Add();
					ctx.CreateArray(itemType, null); // length is on the stack
					ctx.StoreValue(newArr);

					ctx.LoadValue(oldLen);
					Compiler.CodeLabel nothingToCopy = ctx.DefineLabel();
					ctx.BranchIfFalse(nothingToCopy, true);
					ctx.LoadValue(oldArr);
					ctx.LoadValue(newArr);
					ctx.LoadValue(0); // index in target

					ctx.EmitCall(ExpectedType.GetMethod("CopyTo", copyToArrayInt32Args));
					ctx.MarkLabel(nothingToCopy);

					ctx.LoadValue(list);
					ctx.LoadValue(newArr);
					ctx.LoadValue(oldLen);
                        
				}
				else
				{
					ctx.LoadAddress(list, listType);
					ctx.LoadValue(listType.GetProperty("Count"));
					ctx.CreateArray(itemType, null);
					ctx.StoreValue(newArr);

					ctx.LoadAddress(list, listType);
					ctx.LoadValue(newArr);
					ctx.LoadValue(0);
				}

				copyToArrayInt32Args[0] = ExpectedType; // // prefer: CopyTo(T[], int)
				MethodInfo copyTo = listType.GetMethod("CopyTo", copyToArrayInt32Args);
				if (copyTo == null)
				{ // fallback: CopyTo(Array, int)
					copyToArrayInt32Args[1] = typeof(Array);
					copyTo = listType.GetMethod("CopyTo", copyToArrayInt32Args);
				}
				ctx.EmitCall(copyTo);
			}

			ctx.LoadValue(newArr);
		}