Switched to ManagedByteCodeWriter

This commit is contained in:
Yoshi Askharoun
2021-10-06 14:29:28 -05:00
parent ee3e4de91e
commit 40ba4250e8
36 changed files with 187 additions and 279 deletions
@@ -172,7 +172,7 @@ namespace Microsoft.Iris.CodeModel.Cpp
public override bool SupportsTypeConversion(TypeSchema fromType) => false;
public override void EncodeBinary(ByteCodeWriter writer, object instance)
public override void EncodeBinary(ManagedByteCodeWriter writer, object instance)
{
}
+1 -1
View File
@@ -68,7 +68,7 @@ namespace Microsoft.Iris.Data
else if (_buffer != null)
{
if (_requiresMemoryFree)
FreeNativeBuffer(new IntPtr(_buffer.AsMemory().Pin().Pointer));
_buffer.AsMemory().Pin().Dispose();
_buffer = null;
}
_status = ResourceStatus.NeedsAcquire;
@@ -278,7 +278,7 @@ namespace Microsoft.Iris.Markup
public override bool SupportsTypeConversion(TypeSchema fromType) => _isFrameworkEnum && Int32Schema.Type.IsAssignableFrom(fromType);
public override void EncodeBinary(ByteCodeWriter writer, object instance)
public override void EncodeBinary(ManagedByteCodeWriter writer, object instance)
{
if (_type.IsEnum)
{
@@ -1,228 +0,0 @@
// Decompiled with JetBrains decompiler
// Type: Microsoft.Iris.Markup.ByteCodeWriter
// Assembly: UIX, Version=4.8.0.0, Culture=neutral, PublicKeyToken=ddd0da4d3e678217
// MVID: A56C6C9D-B7F6-46A9-8BDE-B3D9B8D60B11
// Assembly location: C:\Program Files\Zune\UIX.dll
using Microsoft.Iris.OS;
using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace Microsoft.Iris.Markup
{
internal class ByteCodeWriter
{
private const int BLOCK_SIZE = 4096;
private byte[] _scratch = new byte[8];
private uint _cbFreeInBlock;
private uint _totalSize;
private byte[] _currentBlock;
private ArrayList _blockList = new ArrayList();
public uint DataSize => _totalSize;
public void WriteByte(byte value)
{
_scratch[0] = value;
Write(_scratch, 1U);
}
public void WriteByte(OpCode value)
{
_scratch[0] = (byte)value;
Write(_scratch, 1U);
}
public void WriteBool(bool value)
{
_scratch[0] = 0;
if (value) _scratch[0] = 1;
Write(_scratch, 1U);
}
public void WriteChar(char value)
{
_scratch[0] = (byte)value;
_scratch[1] = (byte)((uint)value >> 8);
Write(_scratch, 2U);
}
public void WriteUInt16(ushort value)
{
_scratch[0] = (byte)value;
_scratch[1] = (byte)((uint)value >> 8);
Write(_scratch, 2U);
}
public void WriteUInt16(int rawValue)
{
ushort num = (ushort)rawValue;
_scratch[0] = (byte)num;
_scratch[1] = (byte)((uint)num >> 8);
Write(_scratch, 2U);
}
public void WriteUInt16(uint rawValue)
{
ushort num = (ushort)rawValue;
_scratch[0] = (byte)num;
_scratch[1] = (byte)((uint)num >> 8);
Write(_scratch, 2U);
}
public void WriteInt32(int value)
{
_scratch[0] = (byte)value;
_scratch[1] = (byte)(value >> 8);
_scratch[2] = (byte)(value >> 16);
_scratch[3] = (byte)(value >> 24);
Write(_scratch, 4U);
}
public void WriteUInt32(uint value)
{
_scratch[0] = (byte)value;
_scratch[1] = (byte)(value >> 8);
_scratch[2] = (byte)(value >> 16);
_scratch[3] = (byte)(value >> 24);
Write(_scratch, 4U);
}
public void WriteInt64(long value) => WriteUInt64((ulong)value);
public void WriteUInt64(ulong value)
{
_scratch[0] = (byte)value;
_scratch[1] = (byte)(value >> 8);
_scratch[2] = (byte)(value >> 16);
_scratch[3] = (byte)(value >> 24);
_scratch[4] = (byte)(value >> 32);
_scratch[5] = (byte)(value >> 40);
_scratch[6] = (byte)(value >> 48);
_scratch[7] = (byte)(value >> 56);
Write(_scratch, 8U);
}
public unsafe void WriteSingle(float value)
{
uint num = *(uint*)&value;
_scratch[0] = (byte)num;
_scratch[1] = (byte)(num >> 8);
_scratch[2] = (byte)(num >> 16);
_scratch[3] = (byte)(num >> 24);
Write(_scratch, 4U);
}
public unsafe void WriteDouble(double value) => WriteInt64(*(long*)&value);
public void WriteString(string value)
{
if (value == null)
{
WriteUInt16(ushort.MaxValue);
}
else
{
if (value.Length >= short.MaxValue)
throw new ArgumentException("String too long");
bool flag = false;
foreach (char ch in value)
{
if (ch > 'ÿ')
{
flag = true;
break;
}
}
uint length = (uint)value.Length;
if (!flag)
length |= 32768U;
WriteUInt16((ushort)length);
foreach (char ch in value)
{
if (flag)
WriteChar(ch);
else
WriteByte((byte)ch);
}
}
}
public void Write(ManagedByteCodeReader value) => Write(value, 0U);
public unsafe void Write(ManagedByteCodeReader value, uint offset)
{
IntPtr intPtr = value.ToIntPtr(out long size);
if (offset > size)
throw new ArgumentOutOfRangeException(nameof(offset));
Write(new IntPtr(intPtr.ToInt64() + offset), (uint)(size - offset));
}
public unsafe void Write(byte[] buffer, uint count)
{
fixed (byte* pbData = buffer)
Write(pbData, count);
}
public unsafe void Write(IntPtr buffer, uint count) => Write((byte*)buffer.ToPointer(), count);
private unsafe void Write(byte* pbData, uint cbData)
{
while (cbData > 0U)
{
if (_cbFreeInBlock == 0U)
{
_currentBlock = new byte[4096];
_blockList.Add(_currentBlock);
_cbFreeInBlock = 4096U;
}
uint num1 = cbData <= _cbFreeInBlock ? cbData : _cbFreeInBlock;
uint num2 = 4096U - _cbFreeInBlock;
Marshal.Copy(new IntPtr(pbData), _currentBlock, (int)num2, (int)num1);
pbData += (int)num1;
cbData -= num1;
_cbFreeInBlock -= num1;
_totalSize += num1;
}
}
public void Overwrite(uint offset, uint value)
{
if (offset + 4U > _totalSize)
throw new ArgumentException("Invalid offset");
OverwriteByte(offset, (byte)value);
OverwriteByte(offset + 1U, (byte)(value >> 8));
OverwriteByte(offset + 2U, (byte)(value >> 16));
OverwriteByte(offset + 3U, (byte)(value >> 24));
}
private void OverwriteByte(uint offset, byte value) => ((byte[])_blockList[(int)(offset / 4096U)])[(offset % 4096U)] = value;
private unsafe byte* ComposeFinalBuffer(out uint totalSize)
{
byte* pointer = (byte*)NativeApi.MemAlloc(_totalSize, false).ToPointer();
byte* numPtr = pointer;
for (int index = 0; index < _blockList.Count - 1; ++index)
{
Marshal.Copy((byte[])_blockList[index], 0, new IntPtr(numPtr), 4096);
numPtr += 4096;
}
uint num = 4096U - _cbFreeInBlock;
if (_currentBlock != null && num != 0U)
Marshal.Copy(_currentBlock, 0, new IntPtr(numPtr), (int)num);
totalSize = _totalSize;
_blockList.Clear();
_currentBlock = null;
_cbFreeInBlock = 0U;
_totalSize = 0U;
return pointer;
}
public unsafe ManagedByteCodeReader CreateReader()
{
uint totalSize;
return new ManagedByteCodeReader(new IntPtr(ComposeFinalBuffer(out totalSize)), totalSize, true);
}
}
}
@@ -631,7 +631,7 @@ namespace Microsoft.Iris.Markup
}
else
{
ByteCodeWriter byteCodeWriter = new ByteCodeWriter();
ManagedByteCodeWriter byteCodeWriter = new ManagedByteCodeWriter();
byteCodeWriter.Write(_reader.GetAddress(_objectSectionStart), num);
reader = byteCodeWriter.CreateReader();
}
@@ -6,5 +6,5 @@
namespace Microsoft.Iris.Markup
{
internal delegate void EncodeBinaryHandler(ByteCodeWriter writer, object instance);
internal delegate void EncodeBinaryHandler(ManagedByteCodeWriter writer, object instance);
}
+1 -1
View File
@@ -168,7 +168,7 @@ namespace Microsoft.Iris.Markup
public override bool SupportsTypeConversion(TypeSchema fromType) => StringSchema.Type.IsAssignableFrom(fromType) || Int32Schema.Type.IsAssignableFrom(fromType);
public override void EncodeBinary(ByteCodeWriter writer, object instance) => writer.WriteInt32(ValueFromObject(instance));
public override void EncodeBinary(ManagedByteCodeWriter writer, object instance) => writer.WriteInt32(ValueFromObject(instance));
public override object DecodeBinary(ManagedByteCodeReader reader) => EnumValueToObject(reader.ReadInt32());
@@ -27,7 +27,7 @@ namespace Microsoft.Iris.Markup
public override bool SupportsTypeConversion(TypeSchema fromType) => _frameworkSchema.SupportsTypeConversion(fromType);
public override void EncodeBinary(ByteCodeWriter writer, object instance) => _frameworkSchema.EncodeBinary(writer, instance);
public override void EncodeBinary(ManagedByteCodeWriter writer, object instance) => _frameworkSchema.EncodeBinary(writer, instance);
public override object DecodeBinary(ManagedByteCodeReader reader) => _frameworkSchema.DecodeBinary(reader);
@@ -0,0 +1,143 @@
using Microsoft.Iris.OS;
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
namespace Microsoft.Iris.Markup
{
internal class ManagedByteCodeWriter
{
private const int BLOCK_SIZE = 4096;
private byte[] _scratch = new byte[8];
private BinaryWriter _writer;
private MemoryStream _stream;
private uint _cbFreeInBlock;
private uint _totalSize;
private byte[] _currentBlock;
private ArrayList _blockList = new ArrayList();
internal ManagedByteCodeWriter()
{
_stream = new MemoryStream();
_writer = new BinaryWriter(_stream);
}
public uint DataSize => _totalSize;
public void WriteByte(byte value) => _writer.Write(value);
public void WriteByte(OpCode value) => _writer.Write((byte)value);
public void WriteBool(bool value) => _writer.Write(value ? 1 : 0);
public void WriteChar(char value) => _writer.Write(value);
public void WriteUInt16(ushort value) => _writer.Write(value);
public void WriteUInt16(int rawValue) => WriteUInt16((ushort)rawValue);
public void WriteUInt16(uint rawValue) => WriteUInt16((ushort)rawValue);
public void WriteInt32(int value) => _writer.Write(value);
public void WriteUInt32(uint value) => _writer.Write(value);
public void WriteInt64(long value) => _writer.Write(value);
public void WriteUInt64(ulong value) => _writer.Write(value);
public void WriteSingle(float value) => _writer.Write(value);
public unsafe void WriteDouble(double value) => _writer.Write(value);
public void WriteString(string value)
{
if (value == null)
{
WriteUInt16(ushort.MaxValue);
}
else
{
if (value.Length >= short.MaxValue)
throw new ArgumentException("String too long");
bool flag = false;
foreach (char ch in value)
{
if (ch > 'ÿ')
{
flag = true;
break;
}
}
uint length = (uint)value.Length;
if (!flag)
length |= 32768U;
WriteUInt16((ushort)length);
foreach (char ch in value)
{
if (flag)
WriteChar(ch);
else
WriteByte((byte)ch);
}
}
}
public void Write(ManagedByteCodeReader value) => Write(value, 0U);
public unsafe void Write(ManagedByteCodeReader value, uint offset)
{
IntPtr intPtr = value.ToIntPtr(out long size);
if (offset > size)
throw new ArgumentOutOfRangeException(nameof(offset));
Write(new IntPtr(intPtr.ToInt64() + offset), (uint)(size - offset));
}
public unsafe void Write(byte[] buffer, uint count)
{
fixed (byte* pbData = buffer)
Write(pbData, count);
}
public unsafe void Write(IntPtr buffer, uint count) => Write((byte*)buffer.ToPointer(), count);
private unsafe void Write(byte* pbData, uint cbData)
{
while (cbData > 0U)
{
if (_cbFreeInBlock == 0U)
{
_currentBlock = new byte[4096];
_blockList.Add(_currentBlock);
_cbFreeInBlock = 4096U;
}
uint num1 = cbData <= _cbFreeInBlock ? cbData : _cbFreeInBlock;
uint num2 = 4096U - _cbFreeInBlock;
Marshal.Copy(new IntPtr(pbData), _currentBlock, (int)num2, (int)num1);
pbData += (int)num1;
cbData -= num1;
_cbFreeInBlock -= num1;
_totalSize += num1;
}
}
public void Overwrite(uint offset, uint value)
{
if (offset + 4U > _totalSize)
throw new ArgumentException("Invalid offset");
OverwriteByte(offset, (byte)value);
OverwriteByte(offset + 1U, (byte)(value >> 8));
OverwriteByte(offset + 2U, (byte)(value >> 16));
OverwriteByte(offset + 3U, (byte)(value >> 24));
}
private void OverwriteByte(uint offset, byte value) => ((byte[])_blockList[(int)(offset / 4096U)])[(offset % 4096U)] = value;
public unsafe ManagedByteCodeReader CreateReader()
{
_writer.Flush();
return new ManagedByteCodeReader(_stream.ToArray(), true);
}
}
}
@@ -14,7 +14,7 @@ namespace Microsoft.Iris.Markup
{
internal class MarkupCompiler
{
private ByteCodeWriter _writer;
private ManagedByteCodeWriter _writer;
private MarkupLoadResult _loadResult;
private MarkupBinaryDataTable _binaryDataTable;
private bool _usingSharedBinaryDataTable;
@@ -70,7 +70,7 @@ namespace Microsoft.Iris.Markup
for (int index = 0; index < compilands.Length; ++index)
{
ErrorWatermark watermark2 = ErrorManager.Watermark;
ByteCodeWriter writer = Run((MarkupLoadResult)vector[index], markupBinaryDataTable);
ManagedByteCodeWriter writer = Run((MarkupLoadResult)vector[index], markupBinaryDataTable);
if (!watermark2.ErrorsDetected)
SaveCompiledOutput(writer, compilands[index].OutputFileName);
}
@@ -80,7 +80,7 @@ namespace Microsoft.Iris.Markup
return !watermark1.ErrorsDetected;
}
private static void SaveCompiledOutput(ByteCodeWriter writer, string outputFile)
private static void SaveCompiledOutput(ManagedByteCodeWriter writer, string outputFile)
{
ErrorWatermark watermark = ErrorManager.Watermark;
IntPtr invalidHandleValue = Win32Api.INVALID_HANDLE_VALUE;
@@ -102,18 +102,14 @@ namespace Microsoft.Iris.Markup
reader?.Dispose(typeof(MarkupSystem));
}
public static ByteCodeWriter Run(
MarkupLoadResult loadResult,
MarkupBinaryDataTable sharedBinaryDataTable)
public static ManagedByteCodeWriter Run(MarkupLoadResult loadResult, MarkupBinaryDataTable sharedBinaryDataTable)
{
return new MarkupCompiler().InternalRun(loadResult, sharedBinaryDataTable);
}
private ByteCodeWriter InternalRun(
MarkupLoadResult loadResult,
MarkupBinaryDataTable sharedBinaryDataTable)
private ManagedByteCodeWriter InternalRun(MarkupLoadResult loadResult, MarkupBinaryDataTable sharedBinaryDataTable)
{
_writer = new ByteCodeWriter();
_writer = new ManagedByteCodeWriter();
_loadResult = loadResult;
PersistHeader();
PersistTableOfContents(sharedBinaryDataTable);
@@ -577,8 +573,7 @@ namespace Microsoft.Iris.Markup
}
}
public static ByteCodeWriter CompileBinaryDataTable(
MarkupBinaryDataTable binaryDataTable)
public static ManagedByteCodeWriter CompileBinaryDataTable(MarkupBinaryDataTable binaryDataTable)
{
SourceMarkupLoadResult markupLoadResult = new SourceMarkupLoadResult(binaryDataTable.Uri);
markupLoadResult.RegisterUsage(markupLoadResult);
@@ -594,8 +589,8 @@ namespace Microsoft.Iris.Markup
markupLoadResult.SetImportTables(importTables);
markupLoadResult.SetLineNumberTable(new MarkupLineNumberTable());
markupLoadResult.LineNumberTable.PrepareForRuntimeUse();
markupLoadResult.SetObjectSection(new ByteCodeWriter().CreateReader());
ByteCodeWriter byteCodeWriter = Run(markupLoadResult, null);
markupLoadResult.SetObjectSection(new ManagedByteCodeWriter().CreateReader());
ManagedByteCodeWriter byteCodeWriter = Run(markupLoadResult, null);
markupLoadResult.UnregisterUsage(markupLoadResult);
return byteCodeWriter;
}
@@ -14,7 +14,7 @@ namespace Microsoft.Iris.Markup
{
internal class MarkupEncoder
{
private ByteCodeWriter _writer;
private ManagedByteCodeWriter _writer;
private MarkupConstantsTable _constantsTable;
private MarkupLineNumberTable _lineNumberTable;
private string _sourceFilePathBestGuess;
@@ -34,7 +34,7 @@ namespace Microsoft.Iris.Markup
string sourceFilePathBestGuess)
{
_sourceFilePathBestGuess = sourceFilePathBestGuess;
_writer = new ByteCodeWriter();
_writer = new ManagedByteCodeWriter();
if (parseResult.ClassList.Count > 0)
{
for (int index = 0; index < parseResult.ClassList.Count; ++index)
@@ -293,7 +293,7 @@ namespace Microsoft.Iris.Markup
public override bool SupportsBinaryEncoding => false;
public override void EncodeBinary(ByteCodeWriter writer, object instance)
public override void EncodeBinary(ManagedByteCodeWriter writer, object instance)
{
}
+2 -5
View File
@@ -79,14 +79,11 @@ namespace Microsoft.Iris.Markup
public virtual EventSchema[] Events => (EventSchema[])null;
public abstract Result TypeConverter(
object from,
TypeSchema fromType,
out object instance);
public abstract Result TypeConverter(object from, TypeSchema fromType, out object instance);
public abstract bool SupportsTypeConversion(TypeSchema fromType);
public abstract void EncodeBinary(ByteCodeWriter writer, object instance);
public abstract void EncodeBinary(ManagedByteCodeWriter writer, object instance);
public abstract object DecodeBinary(ManagedByteCodeReader reader);
@@ -14,7 +14,7 @@ namespace Microsoft.Iris.Markup.UIX
private static object Construct() => BooleanBoxes.FalseBox;
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
bool flag = (bool)instanceObj;
writer.WriteBool(flag);
@@ -16,7 +16,7 @@ namespace Microsoft.Iris.Markup.UIX
private static object Construct() => (byte)0;
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
byte num = (byte)instanceObj;
writer.WriteByte(num);
@@ -14,7 +14,7 @@ namespace Microsoft.Iris.Markup.UIX
private static object Construct() => char.MinValue;
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
char ch = (char)instanceObj;
writer.WriteChar(ch);
@@ -268,7 +268,7 @@ namespace Microsoft.Iris.Markup.UIX
return result3;
}
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
Color color = (Color)instanceObj;
writer.WriteUInt32(color.Value);
@@ -16,7 +16,7 @@ namespace Microsoft.Iris.Markup.UIX
private static object Construct() => 0.0;
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
double num = (double)instanceObj;
writer.WriteDouble(num);
@@ -65,7 +65,7 @@ namespace Microsoft.Iris.Markup.UIX
return new Inset(parameter, parameter, parameter, parameter);
}
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
Inset inset = (Inset)instanceObj;
writer.WriteInt32(inset.Left);
@@ -22,7 +22,7 @@ namespace Microsoft.Iris.Markup.UIX
private static object Construct() => Int32Boxes.ZeroBox;
private static void EncodeBinary(ByteCodeWriter writer, object instanceObj)
private static void EncodeBinary(ManagedByteCodeWriter writer, object instanceObj)
{
int num = (int)instanceObj;
writer.WriteInt32(num);

Some files were not shown because too many files have changed in this diff Show More