summaryrefslogtreecommitdiff
path: root/Compiler/visitors/Expressions.cs
blob: e67688d582d5d950f6ad7ca3dd27426b4ad9668d (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
namespace Q1.Compiler.visitors;

public partial class CCompilerVisitor
{
    private string VisitExpression(CGrammarParser.ExpressionContext expression)
    {
        string? expressionString = this.Visit(expression) as string;
        if (expressionString == null)
            throw new CompilerException("Expression did not return a string");

        return expressionString;
    }
    
    public override string VisitCallExpression(CGrammarParser.CallExpressionContext context)
    {
        string functionName = context.ID().GetText();

        FunctionReference function = this.GetFunctionReference(functionName);
        FunctionOverload overload = function.GetOverload(context._params.Count);

        // We reverse arugments so when we perform pop, its in the right order
        CGrammarParser.ExpressionContext[] argumentsReversed = context._params.Reverse().ToArray();

        for (int i = 0; i < argumentsReversed.Length; i++)
        {
            CGrammarParser.ExpressionContext argument = argumentsReversed[i];
            string result = this.VisitExpression(argument);

            this.Instruction($"push {result}", $"arg {overload.ParameterTypes[i]} {overload.ParameterNames[i]}");
        }

        this.Instruction($"call {function.GetBranchName(overload)}", function.GetBranchFriendlyName(overload));

        return "V0";
    }
    public override string VisitParenthesizedExpression(CGrammarParser.ParenthesizedExpressionContext context)
    {
        return this.VisitExpression(context.expression());
    }
    public override string VisitVariableExpression(CGrammarParser.VariableExpressionContext context)
    {
        string varName = context.ID().GetText();
        VariableReference reference = this.FindVariable(varName);

        return $"[${reference.Address:X4}]";
    }
    public override string VisitAssignmentExpression(CGrammarParser.AssignmentExpressionContext context)
    {
        Target target = this.VisitTarget(context.target());
        string value = this.VisitExpression(context.expression());

        this.DynamicMove(target.Type, value, target.Value, $"{target.Value} = {value}");
        return target.Value;
    }
    public override string VisitTernaryExpression(CGrammarParser.TernaryExpressionContext context)
    {
        string cond = this.VisitExpression(context.cond);

        string thenBranch = this.GenerateBranchLabel("then");
        string endBranch = this.GenerateBranchLabel("end");
        string result;

        // 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

        this.Instruction($"bz {cond}", "check ternary condition");
        this.Instruction($"jmp {thenBranch}");

        result = this.VisitExpression(context.@else);
        this.Instruction($"mov {result}, V0", "store ternary fail result");
        this.Instruction($"jmp {endBranch}");

        this.Branch(thenBranch);
        result = this.VisitExpression(context.then);
        this.Instruction($"mov {result}, V0", "store ternary success result");

        this.Branch(endBranch);

        return "V0";
    }
    public override string VisitAddressOfExpression(CGrammarParser.AddressOfExpressionContext context)
    {
        Target target = this.VisitTarget(context.target());

        return target.Address;
    }
    public override string VisitDereferenceExpression(CGrammarParser.DereferenceExpressionContext context)
    {
        string value = this.VisitExpression(context.expression());

        this.Instruction($"mov {value}, V0", "move pointer into register for dereference");
        return "[V0]";
    }

    public override object? VisitIndexExpression(CGrammarParser.IndexExpressionContext context)
    {
        Target obj = this.VisitTarget(context.obj);
        if (obj.Type is not CTypePtr ptr)
            throw new CompilerException("Indexer target was not a pointer!");
        
        this.Instruction($"push {obj.Value}", "store index obj");

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

        this.Instruction($"mov {indexer}, V1",      "store indexer");
        this.Instruction($"pop V0",                 "restore index obj");
        this.Instruction($"mul V1, {ptr.Sub.Size}", $"multiply indexer by {ptr.Sub.Size}");
        this.Instruction($"add AX, V0",             "add indexer and index obj");
        this.Instruction($"mov AX, V0",             "move to general purpose register");
        return "[V0]";
    }

    public override object? VisitArrayExpression(CGrammarParser.ArrayExpressionContext context)
    {
        CType type = this.VisitCType(context.type());
        int? arrPtr = null;

        if (context._params.Count == 0)
            throw new CompilerException("Cannot create empty array");
        
        foreach (CGrammarParser.ExpressionContext? param in context._params)
        {
            int itemPtr = Alloc(type.Size);
            
            if (arrPtr == null)
                arrPtr = itemPtr;

            string value = this.VisitExpression(param);
            this.DynamicMove(type, value, $"[${itemPtr:X4}]");
        }

        return $"${arrPtr:X4}";
    }
}