Пример #1
0
        public static uint WriteImpl(MemoryPool memory, InstructionCollection instructionCollection)
        {
            using (var stream = new MemoryStream())
                using (var writer = new BinaryWriter(stream))
                {
                    var instructions = instructionCollection.GetInstructions();
                    foreach (var instruction in instructions)
                    {
                        if (instruction.Type == InstructionType.Padding)
                        {
                            continue;
                        }

                        writer.Write((Byte)instruction.Type);
                        if (InstructionAlignment.IsAligned(instruction.Type))
                        {
                            while (writer.BaseStream.Position % Constants.IntPtrSize != 0)
                            {
                                writer.Write((Byte)0);
                            }
                        }
                        InstructionParameterWriter.Write(writer, memory, instruction);
                    }

                    memory.AllocateBytesForPadding(Constants.IntPtrSize);
                    return(memory.WriteDataToNewChunk(stream.ToArray()).StartAddress);
                }
        }
Пример #2
0
        // returns: array start address
        static public uint WritePlainArray <T>(MemoryPool memory, ICollection <T> list)
        {
            memory.AllocateBytesForPadding(Constants.IntPtrSize);
            if (list.Count == 0)
            {
                return(memory.Allocate(0).StartAddress);
            }

            var arrayMemory = memory.Allocate((uint)list.Count * PlainArrayElementWriter.GetElementSize(list.First()));

            using (var stream = new MemoryStream(arrayMemory.Memory, true))
                using (var writer = new BinaryWriter(stream))
                {
                    foreach (var item in list)
                    {
                        PlainArrayElementWriter.WriteInAllocatedMemory(writer, memory, item);
                    }

                    if (stream.Position != arrayMemory.Memory.Length)
                    {
                        throw new InvalidDataException("Unexpected array size!");
                    }
                }

            return(arrayMemory.StartAddress);
        }
Пример #3
0
        public static uint WriteArrayOfPointers <T>(MemoryPool memory, ICollection <T> list)
        {
            memory.AllocateBytesForPadding(Constants.IntPtrSize);
            var arrayChunk = memory.Allocate((uint)list.Count * Constants.IntPtrSize);

            using (var arrayStream = new MemoryStream(arrayChunk.Memory, true))
                using (var arrayWriter = new BinaryWriter(arrayStream))
                {
                    foreach (var item in list)
                    {
                        if (item == null)
                        {
                            arrayWriter.Write((UInt32)0);
                        }

                        memory.AllocateBytesForPadding(Constants.IntPtrSize);
                        var address = DataWriter.Write(memory, item);

                        arrayWriter.Write((UInt32)address);
                    }
                }

            return(arrayChunk.StartAddress);
        }
Пример #4
0
        public static byte[] Write(UInt32 AptDataEntryOffset, ConstantData constantData)
        {
            var memory = new MemoryPool();

            memory.WriteDataToNewChunk(Encoding.ASCII.GetBytes(Constants.ConstFileMagic));
            memory.AllocateBytesForPadding(4);

            // write entryOffset, entriesCount and 32
            using (var header = new MemoryStream(memory.Allocate(Constants.IntPtrSize * 3).Memory, true))
                using (var headerWriter = new BinaryWriter(header))
                {
                    headerWriter.Write((UInt32)AptDataEntryOffset);
                    headerWriter.Write((UInt32)constantData.Entries.Count);
                    headerWriter.Write((UInt32)32);
                }

            // Assuming sizeof(ConstantEntry) is always 8 bytes...
            const uint entrySize    = Constants.IntPtrSize + Constants.IntPtrSize;
            var        entriesChunk = memory.Allocate((uint)constantData.Entries.Count * entrySize);

            using (var entriesOutput = new MemoryStream(entriesChunk.Memory))
                using (var entriesWriter = new BinaryWriter(entriesOutput))
                {
                    foreach (var entry in constantData.Entries)
                    {
                        entriesWriter.Write((UInt32)entry.Type);

                        switch (entry.Type)
                        {
                        case ConstantEntryType.Undef:
                            throw new InvalidDataException("Undefined const entry");

                        case ConstantEntryType.String:
                            var address = DataWriter.Write(memory, (String)entry.Value);
                            entriesWriter.Write((UInt32)address);
                            break;

                        case ConstantEntryType.Register:
                            entriesWriter.Write((UInt32)entry.Value);
                            break;

                        case ConstantEntryType.Boolean:
                            entriesWriter.WriteBooleanUInt32((Boolean)entry.Value);
                            break;

                        case ConstantEntryType.Float:
                            entriesWriter.Write((Single)entry.Value);
                            break;

                        case ConstantEntryType.Integer:
                            entriesWriter.Write((Int32)entry.Value);
                            break;

                        case ConstantEntryType.Lookup:
                            entriesWriter.Write((UInt32)entry.Value);
                            break;

                        default:
                            throw new InvalidDataException();
                        }
                    }
                }

            return(memory.GetMemoryStream().ToArray());
        }
 public static uint WriteImpl(MemoryPool memory, string data)
 {
     memory.AllocateBytesForPadding(Constants.IntPtrSize);
     return(memory.WriteDataToNewChunk(Encoding.ASCII.GetBytes(data + '\0')).StartAddress);
 }