summaryrefslogtreecommitdiff
path: root/Compiler/visitors/FlowControl.cs
blob: 944595dd8951c027649f81c2bebd80e09efd62d2 (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
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
169
170
171
172
173
174
175
176
177
namespace Q1.Compiler.visitors;

public partial class CCompilerVisitor
{
    public override string? VisitBlock(CGrammarParser.BlockContext context)
    {
        this.EnterScope();

        this.VisitChildren(context);

        this.ExitScope();

        return null;
    }
    public override string? VisitReturnStatement(CGrammarParser.ReturnStatementContext context)
    {
        if (context.expression() != null)
        {
            string expression = this.VisitExpression(context.expression());
            this.Instruction($"mov {expression}, V0", "return value");
            this.Instruction($"ret");
        }
        else
            this.Instruction("ret");

        return null;
    }
    public override string? VisitIfStatement(CGrammarParser.IfStatementContext context)
    {
        string cond = this.VisitExpression(context.expression());
        string endBranch = this.GenerateBranchLabel("end");

        if (context.@else != null)
        {
            this.Instruction($"bz {cond}", "check if condition");
            // Else branch is first since it saves us from having to perform a NOT operation.
            // If cond, jump to then, else fall through to else, then jump to end

            string thenBranch = this.GenerateBranchLabel("then");

            this.Instruction($"jmp {thenBranch}");

            this.GeneratedComment("if failed");
            if (context.@else != null)
                this.Visit(context.@else);
            this.Instruction($"jmp {endBranch}");

            this.Branch(thenBranch, "if succeeded");
            this.Visit(context.then);
        }
        else
        {
            this.Instruction($"not {cond}");
            this.Instruction($"bz", "check if condition");
            this.Instruction($"jmp {endBranch}", "if succeeds, continue, else jump to end");
            this.Visit(context.then);
        }

        this.Branch(endBranch);

        return null;
    }
    public override string? VisitForExprStatement(CGrammarParser.ForExprStatementContext context)
    {
        string bodyBranch = this.GenerateBranchLabel("body");
        string endBranch = this.GenerateBranchLabel("end");

        context.LoopBodyBranch = bodyBranch;
        context.LoopEndBranch  = endBranch;

        this.EnterScope();
        this.Visit(context.initializer);

        this.Branch(bodyBranch);
        string cond = this.VisitExpression(context.cond);

        this.Instruction($"not {cond}");
        this.Instruction($"bz");
        this.Instruction($"jmp {endBranch}", "if for condition fails, jump to end");

        this.Visit(context.body);
        this.Visit(context.post);

        this.Branch(endBranch);
        this.ExitScope();

        return null;
    }
    public override string? VisitForDeclStatement(CGrammarParser.ForDeclStatementContext context)
    {
        string bodyBranch = this.GenerateBranchLabel("body");
        string endBranch = this.GenerateBranchLabel("end");

        context.LoopBodyBranch = bodyBranch;
        context.LoopEndBranch  = endBranch;

        this.EnterScope();
        this.Visit(context.initializer);

        this.Branch(bodyBranch);
        string cond = this.VisitExpression(context.cond);

        this.Instruction($"not {cond}");
        this.Instruction($"bz");
        this.Instruction($"jmp {endBranch}", "if for condition fails, jump to end");

        this.Visit(context.body);
        this.Visit(context.post);
        
        this.Instruction($"jmp {bodyBranch}");

        this.Branch(endBranch);
        this.ExitScope();

        return null;
    }
    public override string? VisitWhileStatement(CGrammarParser.WhileStatementContext context)
    {
        string bodyBranch = this.GenerateBranchLabel("body");
        string endBranch = this.GenerateBranchLabel("end");

        context.LoopBodyBranch = bodyBranch;
        context.LoopEndBranch  = endBranch;

        this.Branch(bodyBranch);
        string cond = this.VisitExpression(context.cond);

        this.Instruction($"not {cond}");
        this.Instruction($"bz");
        this.Instruction($"jmp {endBranch}", "if while condition fails, jump to end");

        this.Visit(context.body);
        this.Instruction($"jmp {bodyBranch}");

        this.Branch(endBranch);

        return null;
    }
    public override string? VisitDoWhileStatement(CGrammarParser.DoWhileStatementContext context)
    {
        string bodyBranch = this.GenerateBranchLabel("body");
        string endBranch = this.GenerateBranchLabel("end");

        context.LoopBodyBranch = bodyBranch;
        context.LoopEndBranch  = endBranch;

        this.Branch(bodyBranch);
        this.Visit(context.body);

        string cond = this.VisitExpression(context.cond);

        this.Instruction($"bz {cond}");
        this.Instruction($"jmp {bodyBranch}", "if while condition succeeds, jump back to start");

        this.Branch(endBranch);

        return null;
    }
    public override string? VisitBreakStatement(CGrammarParser.BreakStatementContext context)
    {
        if (context.LoopEndBranch == null)
            throw new CompilerException("Cannot break, not inside loop with end");

        this.Instruction($"jmp {context.LoopEndBranch}", "break");

        return null;
    }
    public override string? VisitContinueStatement(CGrammarParser.ContinueStatementContext context)
    {
        if (context.LoopBodyBranch == null)
            throw new CompilerException("Cannot skip body (continue), not inside loop with body");

        this.Instruction($"jmp {context.LoopBodyBranch}", "continue");

        return null;
    }
}