aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Diagnostics/Logger.cs
blob: d7f61230c84ac2fb51aa07e88e2d07e840303d3d (plain) (blame)
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
using ARMeilleure.Translation;
using System;
using System.Diagnostics;

namespace ARMeilleure.Diagnostics
{
    static class Logger
    {
        private static long _startTime;

        private static readonly long[] _accumulatedTime;

        static Logger()
        {
            _accumulatedTime = new long[(int)PassName.Count];
        }

        [Conditional("M_DEBUG")]
        public static void StartPass(PassName name)
        {
            WriteOutput(name + " pass started...");

            _startTime = Stopwatch.GetTimestamp();
        }

        [Conditional("M_DEBUG")]
        public static void EndPass(PassName name, ControlFlowGraph cfg)
        {
            EndPass(name);

            WriteOutput("IR after " + name + " pass:");

            WriteOutput(IRDumper.GetDump(cfg));
        }

        [Conditional("M_DEBUG")]
        public static void EndPass(PassName name)
        {
            long elapsedTime = Stopwatch.GetTimestamp() - _startTime;

            _accumulatedTime[(int)name] += elapsedTime;

            WriteOutput($"{name} pass ended after {GetMilliseconds(_accumulatedTime[(int)name])} ms...");
        }

        private static long GetMilliseconds(long ticks)
        {
            return (long)(((double)ticks / Stopwatch.Frequency) * 1000);
        }

        private static void WriteOutput(string text)
        {
            Console.WriteLine(text);
        }
    }
}