-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeGenerator.cs
53 lines (44 loc) · 1.52 KB
/
CodeGenerator.cs
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
using NIR.Instructions;
using NIR.Passes;
using NIR.Passes.Optimize;
using NIR.Passes.RegAlloc;
using System.Collections.Generic;
namespace NIR
{
public class CodeGenerator
{
IRProgram Program;
ArchitectureInfo Arch;
public CodeGenerator(IRProgram program, ArchitectureInfo arch)
{
Program = program;
Arch = arch;
}
public string Generate(bool optimize = true)
{
var symbols = new List<(IROp, IRName, IRType)>();
foreach (IROp op in Program.Body)
{
if (op is IRData dat)
symbols.Add((dat, dat.Name, new IRPointerType(new IRVoidType())));
else if (op is IRGlobal global)
symbols.Add((global, global.Name, global.Type));
else if (op is IRFunction func)
symbols.Add((func, func.Name, new IRPointerType(new IRVoidType())));
}
foreach (IROp op in Program.Body)
{
if (op is IRFunction func && !func.NoDefinition)
{
func.OutsideSymbols = symbols;
if (optimize)
new IROptimizer().Perform(func, Arch);
foreach (IRPass pass in Arch.ArchSpecificPasses)
pass.Perform(func, Arch);
new IRGreedyAlloc().Perform(func, Arch);
}
}
return Arch.Backend.CompileProgram(Program);
}
}
}