summaryrefslogtreecommitdiff
path: root/Compiler/Optimizer.cs
blob: 4646c1ca4f91935f8848f52892924797fcf79635 (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
namespace Q1.Compiler;

using System.Text.RegularExpressions;

public class Optimizer
{
    private string _instructions;
    private int _currentIndex = 0;
    
    public Optimizer(List<string> instructions)
    {
        this._instructions = string.Join(Environment.NewLine, instructions);
    }
    
    public string Optimize()
    {
        string output = this._instructions;
        output = this.RemoveRedundantFallbackReturns(output);
        
        return output;
    }
    
    private string RemoveRedundantFallbackReturns(string input)
    {
        string pattern = @"(mov .*, V0\n\s*ret\n)\s*mov 0, V0\n\s*ret";
        return Regex.Replace(input, pattern, "$1");
    }
}