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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.Translation;
using System;
using System.Collections.Generic;
using System.Text;
namespace ARMeilleure.Diagnostics
{
static class IRDumper
{
private const string Indentation = " ";
public static string GetDump(ControlFlowGraph cfg)
{
StringBuilder sb = new StringBuilder();
Dictionary<Operand, string> localNames = new Dictionary<Operand, string>();
string indentation = string.Empty;
void IncreaseIndentation()
{
indentation += Indentation;
}
void DecreaseIndentation()
{
indentation = indentation.Substring(0, indentation.Length - Indentation.Length);
}
void AppendLine(string text)
{
sb.AppendLine(indentation + text);
}
IncreaseIndentation();
foreach (BasicBlock block in cfg.Blocks)
{
string blockName = GetBlockName(block);
if (block.Next != null)
{
blockName += $" (next {GetBlockName(block.Next)})";
}
if (block.Branch != null)
{
blockName += $" (branch {GetBlockName(block.Branch)})";
}
blockName += ":";
AppendLine(blockName);
IncreaseIndentation();
foreach (Node node in block.Operations)
{
string[] sources = new string[node.SourcesCount];
string instName = string.Empty;
if (node is PhiNode phi)
{
for (int index = 0; index < sources.Length; index++)
{
string phiBlockName = GetBlockName(phi.GetBlock(index));
string operName = GetOperandName(phi.GetSource(index), localNames);
sources[index] = $"({phiBlockName}: {operName})";
}
instName = "Phi";
}
else if (node is Operation operation)
{
for (int index = 0; index < sources.Length; index++)
{
sources[index] = GetOperandName(operation.GetSource(index), localNames);
}
instName = operation.Instruction.ToString();
}
string allSources = string.Join(", ", sources);
string line = instName + " " + allSources;
if (node.Destination != null)
{
line = GetOperandName(node.Destination, localNames) + " = " + line;
}
AppendLine(line);
}
DecreaseIndentation();
}
return sb.ToString();
}
private static string GetBlockName(BasicBlock block)
{
return $"block{block.Index}";
}
private static string GetOperandName(Operand operand, Dictionary<Operand, string> localNames)
{
if (operand == null)
{
return "<NULL>";
}
string name = string.Empty;
if (operand.Kind == OperandKind.LocalVariable)
{
if (!localNames.TryGetValue(operand, out string localName))
{
localName = "%" + localNames.Count;
localNames.Add(operand, localName);
}
name = localName;
}
else if (operand.Kind == OperandKind.Register)
{
Register reg = operand.GetRegister();
switch (reg.Type)
{
case RegisterType.Flag: name = "b" + reg.Index; break;
case RegisterType.Integer: name = "r" + reg.Index; break;
case RegisterType.Vector: name = "v" + reg.Index; break;
}
}
else if (operand.Kind == OperandKind.Constant)
{
name = "0x" + operand.Value.ToString("X");
}
else
{
name = operand.Kind.ToString().ToLower();
}
return GetTypeName(operand.Type) + " " + name;
}
private static string GetTypeName(OperandType type)
{
switch (type)
{
case OperandType.FP32: return "f32";
case OperandType.FP64: return "f64";
case OperandType.I32: return "i32";
case OperandType.I64: return "i64";
case OperandType.None: return "none";
case OperandType.V128: return "v128";
}
throw new ArgumentException($"Invalid operand type \"{type}\".");
}
}
}
|