提交 86ed5435 编写于 作者: T Tomas Matousek

BlobWrite clean up and tests

上级 4c0cb3e5
......@@ -30,7 +30,7 @@
<Compile Include="Emit\CustomDebugInfoTests.cs" />
<Compile Include="InternalUtilities\StringExtensionsTests.cs" />
<Compile Include="MetadataReferences\AssemblyIdentityExtensions.cs" />
<Compile Include="PEWriter\BinaryWriterTests.cs" />
<Compile Include="PEWriter\BlobWriterTests.cs" />
<Compile Include="SimpleAnalyzerAssemblyLoaderTests.cs" />
<Compile Include="Text\LargeEncodedTextTests.cs" />
<Compile Include="Text\SourceTextStreamTests.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Xunit;
namespace Microsoft.CodeAnalysis.Collections.UnitTests
{
public class ByteSequenceComparerTests
{
[Fact]
public void Equals1()
{
Assert.True(ByteSequenceComparer.Equals(new byte[] { }, new byte[] { }));
Assert.True(ByteSequenceComparer.Equals(new byte[] { 1 }, new byte[] { 1 }));
Assert.False(ByteSequenceComparer.Equals(new byte[] { 1 }, new byte[] { 2 }));
Assert.True(ByteSequenceComparer.Equals(new byte[] { 1, 2 }, new byte[] { 1, 2 }));
Assert.False(ByteSequenceComparer.Equals(new byte[] { 1, 2 }, new byte[] { 1, 3 }));
}
[Fact]
public void Equals2()
{
Assert.True(ByteSequenceComparer.Equals(new byte[] { }, 0, new byte[] { }, 0, 0));
Assert.True(ByteSequenceComparer.Equals(new byte[] { 1 }, 0, new byte[] { }, 0, 0));
Assert.True(ByteSequenceComparer.Equals(new byte[] { 1 }, 1, new byte[] { 1 }, 1, 0));
Assert.True(ByteSequenceComparer.Equals(new byte[] { 1 }, 0, new byte[] { 1 }, 0, 1));
Assert.False(ByteSequenceComparer.Equals(new byte[] { 1 }, 0, new byte[] { 2 }, 0, 1));
Assert.True(ByteSequenceComparer.Equals(new byte[] { 1, 2 }, 1, new byte[] { 2 }, 0, 1));
}
[Fact]
public void Equals3()
{
var b = new byte[] { 1, 2, 1 };
Assert.True(ByteSequenceComparer.Equals(b, b));
Assert.True(ByteSequenceComparer.Equals(b, 0, b, 0, 1));
Assert.True(ByteSequenceComparer.Equals(b, 2, b, 2, 1));
Assert.True(ByteSequenceComparer.Equals(b, 0, b, 2, 1));
Assert.False(ByteSequenceComparer.Equals(b, 0, b, 1, 1));
Assert.False(ByteSequenceComparer.Equals(null, b));
Assert.False(ByteSequenceComparer.Equals(null, new byte[] { }));
Assert.True(ByteSequenceComparer.Equals(null, null));
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Cci;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.UnitTests.PEWriter
{
public class BinaryWriterTests
public class BlobWriterTests
{
private static byte[] CompressUnsignedInteger(int value)
{
var writer = new BlobWriter();
writer.WriteCompressedUInt((uint)value);
writer.WriteCompressedInteger((uint)value);
return writer.ToArray();
}
......@@ -58,5 +55,157 @@ public void CompressSignedIntegersFromSpecExamples()
AssertEx.Equal(new byte[] { 0xDF, 0xFF, 0xFF, 0xFE }, CompressSignedInteger(268435455));
AssertEx.Equal(new byte[] { 0xC0, 0x00, 0x00, 0x01 }, CompressSignedInteger(-268435456));
}
[Fact]
public void WritePrimitive()
{
var writer = new BlobWriter(4);
writer.WriteUInt32(0x11223344);
writer.WriteUInt16(0x5566);
writer.WriteByte(0x77);
writer.WriteUInt64(0x8899aabbccddeeff);
writer.WriteInt32(-1);
writer.WriteInt16(-2);
writer.WriteSByte(-3);
writer.WriteBoolean(true);
writer.WriteBoolean(false);
writer.WriteInt64(unchecked((long)0xfedcba0987654321));
writer.WriteDateTime(new DateTime(0x1112223334445556));
writer.WriteDecimal(102030405060.70m);
writer.WriteDouble(double.NaN);
writer.WriteSingle(float.NegativeInfinity);
AssertEx.Equal(new byte[]
{
0x44, 0x33, 0x22, 0x11,
0x66, 0x55,
0x77,
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
0xff, 0xff, 0xff, 0xff,
0xfe, 0xff,
0xfd,
0x01,
0x00,
0x21, 0x43, 0x65, 0x87, 0x09, 0xBA, 0xDC, 0xFE,
0x56, 0x55, 0x44, 0x34, 0x33, 0x22, 0x12, 0x11,
0x02, 0xD6, 0xE0, 0x9A, 0x94, 0x47, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
0x00, 0x00, 0x80, 0xFF
}, writer.ToArray());
}
[Fact]
public void WriteBytes1()
{
var writer = new BlobWriter(4);
writer.WriteBytes(new byte[] { 1, 2, 3, 4 });
writer.WriteBytes(new byte[] { });
writer.WriteBytes(new byte[] { }, 0, 0);
writer.WriteBytes(new byte[] { 5, 6, 7, 8 });
writer.WriteBytes(new byte[] { 9 });
writer.WriteBytes(new byte[] { 0x0a }, 0, 0);
writer.WriteBytes(new byte[] { 0x0b }, 0, 1);
writer.WriteBytes(new byte[] { 0x0c }, 1, 0);
writer.WriteBytes(new byte[] { 0x0d, 0x0e }, 1, 1);
AssertEx.Equal(new byte[]
{
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09,
0x0b,
0x0e
}, writer.ToArray());
}
[Fact]
public void WriteBytes2()
{
var writer = new BlobWriter(4);
writer.WriteBytes(0xff, 0);
writer.WriteBytes(1, 4);
writer.WriteBytes(0xff, 0);
writer.WriteBytes(2, 10);
writer.WriteBytes(0xff, 0);
writer.WriteBytes(3, 1);
AssertEx.Equal(new byte[]
{
0x01, 0x01, 0x01, 0x01,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x03
}, writer.ToArray());
}
[Fact]
public void WriteAlignPad()
{
var writer = new BlobWriter(4);
writer.WriteByte(0x01);
writer.PadTo(2);
writer.WriteByte(0x02);
writer.Align(4);
writer.Align(4);
writer.WriteByte(0x03);
writer.Align(4);
writer.WriteByte(0x04);
writer.WriteByte(0x05);
writer.Align(8);
writer.WriteByte(0x06);
writer.Align(2);
writer.Align(1);
AssertEx.Equal(new byte[]
{
0x01, 0x00, 0x02, 0x00,
0x03, 0x00, 0x00, 0x00,
0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x06, 0x00
}, writer.ToArray());
}
[Fact]
public void WriteUTF8()
{
var writer = new BlobWriter(4);
writer.WriteUTF8("a");
writer.WriteUTF8("");
writer.WriteUTF8("bc");
writer.WriteUTF8("d");
writer.WriteUTF8("");
writer.WriteUTF8(Encoding.UTF8.GetString(new byte[]
{
0x00,
0xC2, 0x80,
0xE1, 0x88, 0xB4
}));
writer.WriteUTF8("\0\ud800"); // hi surrogate
writer.WriteUTF8("\0\udc00"); // lo surrogate
writer.WriteUTF8("\0\ud800\udc00"); // pair
writer.WriteUTF8("\0\udc00\ud800"); // lo + hi
AssertEx.Equal(new byte[]
{
(byte)'a',
(byte)'b', (byte)'c',
(byte)'d',
0x00, 0xC2, 0x80, 0xE1, 0x88, 0xB4,
0x00, 0xED, 0xA0, 0x80,
0x00, 0xED, 0xB0, 0x80,
0x00, 0xF0, 0x90, 0x80, 0x80,
0x00, 0xED, 0xB0, 0x80, 0xED, 0xA0, 0x80
}, writer.ToArray());
}
}
}
......@@ -896,7 +896,7 @@ private void RealizeBlocks()
WriteOpCode(writer, ILOpCode.Switch);
var switchBlock = (SwitchBlock)block;
writer.WriteUint(switchBlock.BranchesCount);
writer.WriteUInt32(switchBlock.BranchesCount);
int switchBlockEnd = switchBlock.Start + switchBlock.TotalSize;
......@@ -905,7 +905,7 @@ private void RealizeBlocks()
foreach (var branchBlock in blockBuilder)
{
writer.WriteInt(branchBlock.Start - switchBlockEnd);
writer.WriteInt32(branchBlock.Start - switchBlockEnd);
}
blockBuilder.Free();
......@@ -925,11 +925,11 @@ private void RealizeBlocks()
{
sbyte btOffset = (sbyte)offset;
Debug.Assert(btOffset == offset);
writer.WriteSbyte(btOffset);
writer.WriteSByte(btOffset);
}
else
{
writer.WriteInt(offset);
writer.WriteInt32(offset);
}
}
......
......@@ -39,13 +39,13 @@ internal void EmitOpCode(ILOpCode code, int stackAdjustment)
internal void EmitToken(string value)
{
uint token = module?.GetFakeStringTokenForIL(value) ?? 0xFFFF;
this.GetCurrentWriter().WriteUint(token);
this.GetCurrentWriter().WriteUInt32(token);
}
internal void EmitToken(Microsoft.Cci.IReference value, SyntaxNode syntaxNode, DiagnosticBag diagnostics)
{
uint token = module?.GetFakeSymbolTokenForIL(value, syntaxNode, diagnostics) ?? 0xFFFF;
this.GetCurrentWriter().WriteUint(token);
this.GetCurrentWriter().WriteUInt32(token);
}
internal void EmitArrayBlockInitializer(ImmutableArray<byte> data, SyntaxNode syntaxNode, DiagnosticBag diagnostics)
......@@ -679,29 +679,29 @@ internal void EmitStringConstant(string value)
private void EmitInt8(sbyte int8)
{
this.GetCurrentWriter().WriteSbyte(int8);
this.GetCurrentWriter().WriteSByte(int8);
}
private void EmitInt32(int int32)
{
this.GetCurrentWriter().WriteInt(int32);
this.GetCurrentWriter().WriteInt32(int32);
}
private void EmitInt64(long int64)
{
this.GetCurrentWriter().WriteLong(int64);
this.GetCurrentWriter().WriteInt64(int64);
}
private void EmitFloat(float floatValue)
{
int int32 = BitConverter.ToInt32(BitConverter.GetBytes(floatValue), 0);
this.GetCurrentWriter().WriteInt(int32);
this.GetCurrentWriter().WriteInt32(int32);
}
private void EmitDouble(double doubleValue)
{
long int64 = BitConverter.DoubleToInt64Bits(doubleValue);
this.GetCurrentWriter().WriteLong(int64);
this.GetCurrentWriter().WriteInt64(int64);
}
private static void WriteOpCode(Cci.BlobWriter writer, ILOpCode code)
......
......@@ -667,11 +667,11 @@ public void Serialize(Cci.BlobWriter writer)
switch (this.Discriminator)
{
case ConstantValueTypeDiscriminator.Boolean:
writer.WriteBool(this.BooleanValue);
writer.WriteBoolean(this.BooleanValue);
break;
case ConstantValueTypeDiscriminator.SByte:
writer.WriteSbyte(this.SByteValue);
writer.WriteSByte(this.SByteValue);
break;
case ConstantValueTypeDiscriminator.Byte:
......@@ -680,23 +680,23 @@ public void Serialize(Cci.BlobWriter writer)
case ConstantValueTypeDiscriminator.Char:
case ConstantValueTypeDiscriminator.Int16:
writer.WriteShort(this.Int16Value);
writer.WriteInt16(this.Int16Value);
break;
case ConstantValueTypeDiscriminator.UInt16:
writer.WriteUshort(this.UInt16Value);
writer.WriteUInt16(this.UInt16Value);
break;
case ConstantValueTypeDiscriminator.Single:
writer.WriteFloat(this.SingleValue);
writer.WriteSingle(this.SingleValue);
break;
case ConstantValueTypeDiscriminator.Int32:
writer.WriteInt(this.Int32Value);
writer.WriteInt32(this.Int32Value);
break;
case ConstantValueTypeDiscriminator.UInt32:
writer.WriteUint(this.UInt32Value);
writer.WriteUInt32(this.UInt32Value);
break;
case ConstantValueTypeDiscriminator.Double:
......@@ -704,11 +704,11 @@ public void Serialize(Cci.BlobWriter writer)
break;
case ConstantValueTypeDiscriminator.Int64:
writer.WriteLong(this.Int64Value);
writer.WriteInt64(this.Int64Value);
break;
case ConstantValueTypeDiscriminator.UInt64:
writer.WriteUlong(this.UInt64Value);
writer.WriteUInt64(this.UInt64Value);
break;
default: throw ExceptionUtilities.UnexpectedValue(this.Discriminator);
......
......@@ -611,7 +611,7 @@ protected override int SerializeLocalVariablesSignature(IMethodBody body)
{
var writer = BlobWriter.GetInstance();
writer.WriteByte(0x07);
writer.WriteCompressedUInt((uint)localVariables.Length);
writer.WriteCompressedInteger((uint)localVariables.Length);
foreach (ILocalDefinition local in localVariables)
{
......
......@@ -135,7 +135,7 @@ internal void SerializeLocalSlots(Cci.BlobWriter writer)
if (syntaxOffsetBaseline != -1)
{
writer.WriteByte(SyntaxOffsetBaseline);
writer.WriteCompressedUInt((uint)(-syntaxOffsetBaseline));
writer.WriteCompressedInteger((uint)(-syntaxOffsetBaseline));
}
foreach (LocalSlotDebugInfo localSlot in this.LocalSlots)
......@@ -160,11 +160,11 @@ internal void SerializeLocalSlots(Cci.BlobWriter writer)
}
writer.WriteByte(b);
writer.WriteCompressedUInt((uint)(localSlot.Id.SyntaxOffset - syntaxOffsetBaseline));
writer.WriteCompressedInteger((uint)(localSlot.Id.SyntaxOffset - syntaxOffsetBaseline));
if (hasOrdinal)
{
writer.WriteCompressedUInt((uint)localSlot.Id.Ordinal);
writer.WriteCompressedInteger((uint)localSlot.Id.Ordinal);
}
}
}
......@@ -240,7 +240,7 @@ internal void SerializeLocalSlots(Cci.BlobWriter writer)
internal void SerializeLambdaMap(Cci.BlobWriter writer)
{
Debug.Assert(this.MethodOrdinal >= -1);
writer.WriteCompressedUInt((uint)(this.MethodOrdinal + 1));
writer.WriteCompressedInteger((uint)(this.MethodOrdinal + 1));
int syntaxOffsetBaseline = -1;
foreach (ClosureDebugInfo info in this.Closures)
......@@ -259,12 +259,12 @@ internal void SerializeLambdaMap(Cci.BlobWriter writer)
}
}
writer.WriteCompressedUInt((uint)(-syntaxOffsetBaseline));
writer.WriteCompressedUInt((uint)this.Closures.Length);
writer.WriteCompressedInteger((uint)(-syntaxOffsetBaseline));
writer.WriteCompressedInteger((uint)this.Closures.Length);
foreach (ClosureDebugInfo info in this.Closures)
{
writer.WriteCompressedUInt((uint)(info.SyntaxOffset - syntaxOffsetBaseline));
writer.WriteCompressedInteger((uint)(info.SyntaxOffset - syntaxOffsetBaseline));
}
foreach (LambdaDebugInfo info in this.Lambdas)
......@@ -272,8 +272,8 @@ internal void SerializeLambdaMap(Cci.BlobWriter writer)
Debug.Assert(info.ClosureOrdinal >= LambdaDebugInfo.MinClosureOrdinal);
Debug.Assert(info.LambdaId.Generation == 0);
writer.WriteCompressedUInt((uint)(info.SyntaxOffset - syntaxOffsetBaseline));
writer.WriteCompressedUInt((uint)(info.ClosureOrdinal - LambdaDebugInfo.MinClosureOrdinal));
writer.WriteCompressedInteger((uint)(info.SyntaxOffset - syntaxOffsetBaseline));
writer.WriteCompressedInteger((uint)(info.ClosureOrdinal - LambdaDebugInfo.MinClosureOrdinal));
}
}
......
......@@ -61,7 +61,7 @@ internal static uint Align(uint position, uint alignment)
internal static int Align(int position, int alignment)
{
Debug.Assert(position >= 0 && alignment >= 0);
Debug.Assert(position >= 0 && alignment > 0);
Debug.Assert(CountBits(alignment) == 1);
int result = position & ~(alignment - 1);
......
......@@ -73,7 +73,7 @@ private void MaybeFlush()
if (_logData.Length >= bufferFlushLimit)
{
_hashAlgorithm.TransformBlock(_logData.Buffer, _logData.Position);
_logData.Position = 0;
_logData.Clear();
}
}
......@@ -81,7 +81,7 @@ internal ContentId ContentIdFromLog()
{
Debug.Assert(_logData != null);
_hashAlgorithm.TransformFinalBlock(_logData.Buffer, _logData.Position);
_logData.Position = 0;
_logData.Clear();
return ContentId.FromHash(_hashAlgorithm.Hash.ToImmutableArray());
}
......@@ -126,10 +126,10 @@ public bool LogOperation(PdbWriterOperation op)
public void LogArgument(uint[] data)
{
_logData.WriteInt(data.Length);
_logData.WriteInt32(data.Length);
for (int i = 0; i < data.Length; i++)
{
_logData.WriteUint(data[i]);
_logData.WriteUInt32(data[i]);
}
MaybeFlush();
}
......@@ -142,7 +142,7 @@ public void LogArgument(string data)
public void LogArgument(uint data)
{
_logData.WriteUint(data);
_logData.WriteUInt32(data);
}
public void LogArgument(byte data)
......@@ -166,12 +166,12 @@ public void LogArgument(int[] data)
public void LogArgument(long data)
{
_logData.WriteLong(data);
_logData.WriteInt64(data);
}
public void LogArgument(int data)
{
_logData.WriteInt(data);
_logData.WriteInt32(data);
}
public void LogArgument(object data)
......
......@@ -139,7 +139,7 @@ private static BlobWriter SerializeRecord(byte kind, Action<BlobWriter> data)
cmw.WriteByte(0);
// length (will be patched)
cmw.WriteUint(0);
cmw.WriteUInt32(0);
data(cmw);
......@@ -152,11 +152,11 @@ private static BlobWriter SerializeRecord(byte kind, Action<BlobWriter> data)
cmw.WriteByte(0);
}
cmw.Position = alignmentSizeAndLengthPosition;
cmw.SetPosition(alignmentSizeAndLengthPosition);
cmw.WriteByte(alignmentSize);
cmw.WriteUint((uint)alignedLength);
cmw.WriteUInt32((uint)alignedLength);
cmw.Position = length;
cmw.SetPosition(length);
return cmw;
}
......@@ -174,9 +174,9 @@ private static void SerializeReferenceToIteratorClass(string iteratorClassName,
cmw.Align(4);
uint length = 10 + (uint)iteratorClassName.Length * 2;
if ((length & 3) != 0) length += 4 - (length & 3);
cmw.WriteUint(length);
cmw.WriteUInt32(length);
cmw.WriteUTF16(iteratorClassName);
cmw.WriteShort(0);
cmw.WriteInt16(0);
cmw.Align(4);
Debug.Assert(cmw.Position == length);
customDebugInfo.Add(cmw);
......@@ -195,20 +195,20 @@ private static void SerializeStateMachineLocalScopes(IMethodBody methodBody, Arr
cmw.WriteByte(CDI.CdiVersion);
cmw.WriteByte(CDI.CdiKindStateMachineHoistedLocalScopes);
cmw.Align(4);
cmw.WriteUint(12 + numberOfScopes * 8);
cmw.WriteUint(numberOfScopes);
cmw.WriteUInt32(12 + numberOfScopes * 8);
cmw.WriteUInt32(numberOfScopes);
foreach (var scope in scopes)
{
if (scope.IsDefault)
{
cmw.WriteUint(0);
cmw.WriteUint(0);
cmw.WriteUInt32(0);
cmw.WriteUInt32(0);
}
else
{
// Dev12 C# emits end-inclusive range
cmw.WriteUint((uint)scope.StartOffset);
cmw.WriteUint((uint)scope.EndOffset - 1);
cmw.WriteUInt32((uint)scope.StartOffset);
cmw.WriteUInt32((uint)scope.EndOffset - 1);
}
}
......@@ -253,8 +253,8 @@ private static void SerializeDynamicLocalInfo(IMethodBody methodBody, ArrayBuild
cmw.WriteByte(CDI.CdiKindDynamicLocals);
cmw.Align(4);
// size = Version,Kind + size + cBuckets + (dynamicCount * sizeOf(Local Blob))
cmw.WriteUint(4 + 4 + 4 + (uint)dynamicLocals.Count * blobSize);//Size of the Dynamic Block
cmw.WriteUint((uint)dynamicLocals.Count);
cmw.WriteUInt32(4 + 4 + 4 + (uint)dynamicLocals.Count * blobSize);//Size of the Dynamic Block
cmw.WriteUInt32((uint)dynamicLocals.Count);
int localIndex = 0;
foreach (ILocalDefinition local in dynamicLocals)
......@@ -277,7 +277,7 @@ private static void SerializeDynamicLocalInfo(IMethodBody methodBody, ArrayBuild
}
}
cmw.WriteBytes(flag); //Written Flag
cmw.WriteUint((uint)dynamicTransformFlags.Length); //Written Length
cmw.WriteUInt32((uint)dynamicTransformFlags.Length); //Written Length
}
else
{
......@@ -287,12 +287,12 @@ private static void SerializeDynamicLocalInfo(IMethodBody methodBody, ArrayBuild
if (localIndex < dynamicVariableCount)
{
// Dynamic variable
cmw.WriteUint((uint)local.SlotIndex);
cmw.WriteUInt32((uint)local.SlotIndex);
}
else
{
// Dynamic constant
cmw.WriteUint(0);
cmw.WriteUInt32(0);
}
char[] localName = new char[64];
......@@ -358,11 +358,11 @@ private void SerializeNamespaceScopeMetadata(EmitContext context, IMethodBody me
cmw.WriteByte(CDI.CdiKindUsingInfo);
cmw.Align(4);
cmw.WriteUint(streamLength = BitArithmeticUtilities.Align((uint)usingCounts.Count * 2 + 10, 4));
cmw.WriteUshort((ushort)usingCounts.Count);
cmw.WriteUInt32(streamLength = BitArithmeticUtilities.Align((uint)usingCounts.Count * 2 + 10, 4));
cmw.WriteUInt16((ushort)usingCounts.Count);
foreach (ushort uc in usingCounts)
{
cmw.WriteUshort(uc);
cmw.WriteUInt16(uc);
}
cmw.Align(4);
......@@ -427,8 +427,8 @@ private void SerializeReferenceToMethodWithModuleInfo(ArrayBuilder<BlobWriter> c
cmw.WriteByte(CDI.CdiVersion);
cmw.WriteByte(CDI.CdiKindForwardToModuleInfo);
cmw.Align(4);
cmw.WriteUint(12);
cmw.WriteUint((uint)_methodTokenWithModuleInfo);
cmw.WriteUInt32(12);
cmw.WriteUInt32((uint)_methodTokenWithModuleInfo);
customDebugInfo.Add(cmw);
}
......@@ -438,8 +438,8 @@ private void SerializeReferenceToPreviousMethodWithUsingInfo(ArrayBuilder<BlobWr
cmw.WriteByte(CDI.CdiVersion);
cmw.WriteByte(CDI.CdiKindForwardInfo);
cmw.Align(4);
cmw.WriteUint(12);
cmw.WriteUint((uint)_previousMethodTokenWithUsingInfo);
cmw.WriteUInt32(12);
cmw.WriteUInt32((uint)_previousMethodTokenWithUsingInfo);
customDebugInfo.Add(cmw);
}
}
......
......@@ -46,7 +46,7 @@ public void WriteData(BlobWriter resourceWriter)
}
var count = (int)(stream.Length - stream.Position);
resourceWriter.WriteInt(count);
resourceWriter.WriteInt32(count);
resourceWriter.Write(stream, count);
resourceWriter.Align(8);
......
......@@ -141,7 +141,7 @@ internal sealed class MetadataHeapsBuilder
_blobHeapStartOffset = blobHeapStartOffset;
// Unlike other heaps, #Guid heap in EnC delta is zero-padded.
_guidWriter.Pad(guidHeapStartOffset);
_guidWriter.WriteBytes(0, guidHeapStartOffset);
}
internal BlobIdx GetBlobIndex(BlobWriter stream)
......@@ -162,7 +162,7 @@ internal BlobIdx GetBlobIndex(ImmutableArray<byte> blob)
_blobHeapSize += BlobWriter.GetCompressedIntegerSize(blob.Length) + blob.Length;
}
return index;
}
......@@ -268,9 +268,11 @@ public int GetUserStringToken(string str)
if (!_userStrings.TryGetValue(str, out index))
{
Debug.Assert(!_streamsAreComplete);
index = _userStringWriter.Position + _userStringHeapStartOffset;
_userStrings.Add(str, index);
_userStringWriter.WriteCompressedUInt((uint)str.Length * 2 + 1);
_userStringWriter.WriteCompressedInteger((uint)str.Length * 2 + 1);
_userStringWriter.WriteUTF16(str);
// Write out a trailing byte indicating if the string is really quite simple
......@@ -432,9 +434,9 @@ public void WriteTo(BlobWriter writer, out int guidHeapStartOffset)
private void WriteAlignedBlobHeap(BlobWriter writer)
{
int heapStart = writer.Position;
// ensure enough space in the buffer:
writer.Position += _blobHeapSize;
writer.SetPosition(writer.Position + _blobHeapSize);
// Perf consideration: With large heap the following loop may cause a lot of cache misses
// since the order of entries in _blobs dictionary depends on the hash of the array values,
......@@ -445,23 +447,23 @@ private void WriteAlignedBlobHeap(BlobWriter writer)
int heapOffset = entry.Value.HeapPosition;
var blob = entry.Key;
writer.Position = heapStart + heapOffset;
writer.WriteCompressedUInt((uint)blob.Length);
writer.SetPosition(heapStart + heapOffset);
writer.WriteCompressedInteger((uint)blob.Length);
writer.WriteBytes(blob);
}
Debug.Assert(writer.Length - heapStart == _blobHeapSize);
// add padding:
writer.Position = writer.Length;
writer.Write(0, BitArithmeticUtilities.Align(_blobHeapSize, 4) - _blobHeapSize);
writer.SetPosition(writer.Length);
writer.WriteBytes(0, BitArithmeticUtilities.Align(_blobHeapSize, 4) - _blobHeapSize);
}
private static void WriteAligned(BlobWriter source, BlobWriter target)
{
int length = source.Length;
source.WriteTo(target);
target.Write(0, BitArithmeticUtilities.Align(length, 4) - length);
target.WriteBytes(0, BitArithmeticUtilities.Align(length, 4) - length);
}
}
}
......@@ -199,14 +199,14 @@ private BlobIdx SerializeLocalConstantSignature(ILocalDefinition localConstant)
if (value is decimal)
{
writer.WriteByte(0x11);
writer.WriteCompressedUInt(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
writer.WriteCompressedInteger(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
writer.WriteDecimal((decimal)value);
}
else if (value is DateTime)
{
writer.WriteByte(0x11);
writer.WriteCompressedUInt(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
writer.WriteCompressedInteger(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
writer.WriteDateTime((DateTime)value);
}
......@@ -233,7 +233,7 @@ private BlobIdx SerializeLocalConstantSignature(ILocalDefinition localConstant)
// EnumType
if (type.IsEnum)
{
writer.WriteCompressedUInt(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
writer.WriteCompressedInteger(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
}
}
else if (this.module.IsPlatformType(type, PlatformType.SystemObject))
......@@ -243,7 +243,7 @@ private BlobIdx SerializeLocalConstantSignature(ILocalDefinition localConstant)
else
{
writer.WriteByte((byte)(type.IsValueType ? 0x11 : 0x12));
writer.WriteCompressedUInt(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
writer.WriteCompressedInteger(GetTypeDefOrRefCodedIndex(type, treatRefAsPotentialTypeSpec: true));
}
return _debugHeapsOpt.GetBlobIndex(writer);
......@@ -271,8 +271,8 @@ private void SerializeImport(BlobWriter writer, AssemblyReferenceAlias alias)
{
// <import> ::= AliasAssemblyReference <alias> <target-assembly>
writer.WriteByte((byte)ImportDefinitionKind.AliasAssemblyReference);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(alias.Name)));
writer.WriteCompressedUInt((uint)GetOrAddAssemblyRefIndex(alias.Assembly));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(alias.Name)));
writer.WriteCompressedInteger((uint)GetOrAddAssemblyRefIndex(alias.Assembly));
}
private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
......@@ -285,8 +285,8 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
// <import> ::= ImportXmlNamespace <alias> <target-namespace>
writer.WriteByte((byte)ImportDefinitionKind.ImportXmlNamespace);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.TargetXmlNamespaceOpt)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.TargetXmlNamespaceOpt)));
}
else if (import.TargetTypeOpt != null)
{
......@@ -297,7 +297,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
{
// <import> ::= AliasType <alias> <target-type>
writer.WriteByte((byte)ImportDefinitionKind.AliasType);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
}
else
{
......@@ -305,7 +305,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
writer.WriteByte((byte)ImportDefinitionKind.ImportType);
}
writer.WriteCompressedUInt(GetTypeDefOrRefCodedIndex(import.TargetTypeOpt, treatRefAsPotentialTypeSpec: true)); // TODO: index in release build
writer.WriteCompressedInteger(GetTypeDefOrRefCodedIndex(import.TargetTypeOpt, treatRefAsPotentialTypeSpec: true)); // TODO: index in release build
}
else if (import.TargetNamespaceOpt != null)
{
......@@ -315,7 +315,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
{
// <import> ::= AliasAssemblyNamespace <alias> <target-assembly> <target-namespace>
writer.WriteByte((byte)ImportDefinitionKind.AliasAssemblyNamespace);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
}
else
{
......@@ -323,7 +323,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyNamespace);
}
writer.WriteCompressedUInt((uint)GetAssemblyRefIndex(import.TargetAssemblyOpt));
writer.WriteCompressedInteger((uint)GetAssemblyRefIndex(import.TargetAssemblyOpt));
}
else
{
......@@ -331,7 +331,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
{
// <import> ::= AliasNamespace <alias> <target-namespace>
writer.WriteByte((byte)ImportDefinitionKind.AliasNamespace);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
}
else
{
......@@ -342,7 +342,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
// TODO: cache?
string namespaceName = TypeNameSerializer.BuildQualifiedNamespaceName(import.TargetNamespaceOpt);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(namespaceName)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(namespaceName)));
}
else
{
......@@ -351,7 +351,7 @@ private void SerializeImport(BlobWriter writer, UsedNamespaceOrType import)
Debug.Assert(import.TargetAssemblyOpt == null);
writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyReferenceAlias);
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(_debugHeapsOpt.GetBlobIndexUtf8(import.AliasOpt)));
}
}
......@@ -512,13 +512,13 @@ private void SerializeAsyncMethodSteppingInfo(AsyncMethodBodyDebugInfo asyncInfo
var writer = new BlobWriter();
writer.WriteUint((uint)((long)asyncInfo.CatchHandlerOffset + 1));
writer.WriteUInt32((uint)((long)asyncInfo.CatchHandlerOffset + 1));
for (int i = 0; i < asyncInfo.ResumeOffsets.Length; i++)
{
writer.WriteUint((uint)asyncInfo.YieldOffsets[i]);
writer.WriteUint((uint)asyncInfo.ResumeOffsets[i]);
writer.WriteCompressedUInt((uint)moveNextMethodRid);
writer.WriteUInt32((uint)asyncInfo.YieldOffsets[i]);
writer.WriteUInt32((uint)asyncInfo.ResumeOffsets[i]);
writer.WriteCompressedInteger((uint)moveNextMethodRid);
}
_customDebugInformationTable.Add(new CustomDebugInformationRow
......@@ -541,8 +541,8 @@ private void SerializeStateMachineLocalScopes(IMethodBody methodBody, int method
foreach (var scope in scopes)
{
writer.WriteUint((uint)scope.StartOffset);
writer.WriteUint((uint)scope.Length);
writer.WriteUInt32((uint)scope.StartOffset);
writer.WriteUInt32((uint)scope.Length);
}
_customDebugInformationTable.Add(new CustomDebugInformationRow
......@@ -572,8 +572,8 @@ private BlobIdx SerializeSequencePoints(int localSignatureRowId, ImmutableArray<
uint currentDocumentRowId = GetOrAddDocument(sequencePoints[0].Document, documentIndex);
// header:
writer.WriteCompressedUInt((uint)localSignatureRowId);
writer.WriteCompressedUInt(currentDocumentRowId);
writer.WriteCompressedInteger((uint)localSignatureRowId);
writer.WriteCompressedInteger(currentDocumentRowId);
for (int i = 0; i < sequencePoints.Length; i++)
{
......@@ -581,24 +581,24 @@ private BlobIdx SerializeSequencePoints(int localSignatureRowId, ImmutableArray<
if (documentRowId != currentDocumentRowId)
{
// document record:
writer.WriteCompressedUInt(0);
writer.WriteCompressedUInt(documentRowId);
writer.WriteCompressedInteger(0);
writer.WriteCompressedInteger(documentRowId);
currentDocumentRowId = documentRowId;
}
// delta IL offset:
if (i > 0)
{
writer.WriteCompressedUInt((uint)(sequencePoints[i].Offset - sequencePoints[i - 1].Offset));
writer.WriteCompressedInteger((uint)(sequencePoints[i].Offset - sequencePoints[i - 1].Offset));
}
else
{
writer.WriteCompressedUInt((uint)sequencePoints[i].Offset);
writer.WriteCompressedInteger((uint)sequencePoints[i].Offset);
}
if (sequencePoints[i].IsHidden)
{
writer.WriteShort(0);
writer.WriteInt16(0);
continue;
}
......@@ -609,8 +609,8 @@ private BlobIdx SerializeSequencePoints(int localSignatureRowId, ImmutableArray<
if (previousNonHiddenStartLine < 0)
{
Debug.Assert(previousNonHiddenStartColumn < 0);
writer.WriteCompressedUInt((uint)sequencePoints[i].StartLine);
writer.WriteCompressedUInt((uint)sequencePoints[i].StartColumn);
writer.WriteCompressedInteger((uint)sequencePoints[i].StartLine);
writer.WriteCompressedInteger((uint)sequencePoints[i].StartColumn);
}
else
{
......@@ -633,11 +633,11 @@ private void SerializeDeltaLinesAndColumns(BlobWriter writer, SequencePoint sequ
// only hidden sequence points have zero width
Debug.Assert(deltaLines != 0 || deltaColumns != 0 || sequencePoint.IsHidden);
writer.WriteCompressedUInt((uint)deltaLines);
writer.WriteCompressedInteger((uint)deltaLines);
if (deltaLines == 0)
{
writer.WriteCompressedUInt((uint)deltaColumns);
writer.WriteCompressedInteger((uint)deltaColumns);
}
else
{
......@@ -689,7 +689,7 @@ private BlobIdx SerializeDocumentName(string name)
foreach (var part in name.Split(separator))
{
BlobIdx partIndex = _debugHeapsOpt.GetBlobIndex(ImmutableArray.Create(s_utf8Encoding.GetBytes(part)));
writer.WriteCompressedUInt((uint)_debugHeapsOpt.ResolveBlobIndex(partIndex));
writer.WriteCompressedInteger((uint)_debugHeapsOpt.ResolveBlobIndex(partIndex));
}
return _debugHeapsOpt.GetBlobIndex(writer);
......@@ -777,8 +777,8 @@ private void SerializeLocalScopeTable(BlobWriter writer, MetadataSizes metadataS
writer.WriteReference(row.ImportScope, metadataSizes.ImportScopeIndexSize);
writer.WriteReference(row.VariableList, metadataSizes.LocalVariableIndexSize);
writer.WriteReference(row.ConstantList, metadataSizes.LocalConstantIndexSize);
writer.WriteUint(row.StartOffset);
writer.WriteUint(row.Length);
writer.WriteUInt32(row.StartOffset);
writer.WriteUInt32(row.Length);
}
}
......@@ -786,8 +786,8 @@ private void SerializeLocalVariableTable(BlobWriter writer, MetadataSizes metada
{
foreach (var row in _localVariableTable)
{
writer.WriteUshort(row.Attributes);
writer.WriteUshort(row.Index);
writer.WriteUInt16(row.Attributes);
writer.WriteUInt16(row.Index);
writer.WriteReference((uint)_debugHeapsOpt.ResolveStringIndex(row.Name), metadataSizes.StringIndexSize);
}
}
......
......@@ -4,6 +4,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using Roslyn.Utilities;
......@@ -275,6 +276,9 @@ internal class ResourceSection
{
internal ResourceSection(byte[] sectionBytes, uint[] relocations)
{
Debug.Assert(sectionBytes != null);
Debug.Assert(relocations != null);
SectionBytes = sectionBytes;
Relocations = relocations;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册