aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/PTC/PtcInfo.cs
blob: b28b2dc1f58eaa4b0f3bbe55ac46bd681c3ffe1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using ARMeilleure.CodeGen.Unwinding;
using System;
using System.IO;

namespace ARMeilleure.Translation.PTC
{
    class PtcInfo : IDisposable
    {
        private readonly BinaryWriter _relocWriter;
        private readonly BinaryWriter _unwindInfoWriter;

        public byte[] Code { get; set; }

        public MemoryStream RelocStream      { get; }
        public MemoryStream UnwindInfoStream { get; }

        public int RelocEntriesCount { get; private set; }

        public PtcInfo()
        {
            RelocStream      = new MemoryStream();
            UnwindInfoStream = new MemoryStream();

            _relocWriter      = new BinaryWriter(RelocStream,      EncodingCache.UTF8NoBOM, true);
            _unwindInfoWriter = new BinaryWriter(UnwindInfoStream, EncodingCache.UTF8NoBOM, true);

            RelocEntriesCount = 0;
        }

        public void WriteRelocEntry(RelocEntry relocEntry)
        {
            _relocWriter.Write((int)relocEntry.Position);
            _relocWriter.Write((byte)relocEntry.Symbol.Type);
            _relocWriter.Write((ulong)relocEntry.Symbol.Value);

            RelocEntriesCount++;
        }

        public void WriteUnwindInfo(UnwindInfo unwindInfo)
        {
            _unwindInfoWriter.Write((int)unwindInfo.PushEntries.Length);

            foreach (UnwindPushEntry unwindPushEntry in unwindInfo.PushEntries)
            {
                _unwindInfoWriter.Write((int)unwindPushEntry.PseudoOp);
                _unwindInfoWriter.Write((int)unwindPushEntry.PrologOffset);
                _unwindInfoWriter.Write((int)unwindPushEntry.RegIndex);
                _unwindInfoWriter.Write((int)unwindPushEntry.StackOffsetOrAllocSize);
            }

            _unwindInfoWriter.Write((int)unwindInfo.PrologSize);
        }

        public void Dispose()
        {
            _relocWriter.Dispose();
            _unwindInfoWriter.Dispose();

            RelocStream.Dispose();
            UnwindInfoStream.Dispose();
        }
    }
}