aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/ExpressionGen.cc
blob: f3dadb758784d5db09015e303084475bdf3a0ed4 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

#include "cps/CodeGen.h"
#include "cps/ArrayHandler.h"
#include "cps/FileHandler.h"
#include "cps/Lexer.h"
#include "cps/TypeAST.h"
#include "Builtins.h"

using namespace llvm;
using namespace cps;

using namespace cps::detail;

Value *CodeGen::emitExpr(ExprAST *Expr) {
    if (auto *Cast = dynamic_cast<CastExprAST*>(Expr)) return emitCast(Cast);
    if (!Expr) return nullptr;

    if (auto *Num = dynamic_cast<IntegerExprAST*>(Expr)) {
        return ConstantInt::get(*TheContext, APInt(64, Num->getVal(), true));
    }

    if (auto *Real = dynamic_cast<RealExprAST*>(Expr)) {
        return ConstantFP::get(*TheContext, APFloat(Real->getVal()));
    }

    if (auto *Bool = dynamic_cast<BooleanExprAST*>(Expr)) {
        return ConstantInt::get(*TheContext, APInt(1, Bool->getVal() ? 1 : 0));
    }

    if (auto *Chr = dynamic_cast<CharExprAST*>(Expr)) {
        return ConstantInt::get(Type::getInt8Ty(*TheContext), static_cast<unsigned char>(Chr->getVal()));
    }

    if (auto *Str = dynamic_cast<StringExprAST*>(Expr)) {
        return StrHandler->createLiteral(Str->getVal());
    }

    if (auto *Var = dynamic_cast<VariableExprAST*>(Expr)) {
        const SymbolInfo *Info = getSymbolInfo(Var->getName());
        if (!Info) {
            // Enum values are bare identifiers; collisions with variables are
            // rejected at declaration time, so the order here cannot shadow.
            if (const EnumConstant *EC = Types->lookupEnumConstant(Var->getName())) {
                return ConstantInt::get(*TheContext, APInt(64, static_cast<uint64_t>(EC->Ordinal), true));
            }
            reportError("Unknown variable name %s", Var->getName().c_str());
            return nullptr;
        }
        if (Info->ConstantValue) return Info->ConstantValue;
        if (Info->IsArray) {
            return Info->Storage;
        }

        const TypeInfo *TypeInfo = resolveType(Info->TypeName);
        if (!TypeInfo || !TypeInfo->LLVMType) {
            reportError("Unknown type for variable %s", Var->getName().c_str());
            return nullptr;
        }

        Value *Loaded = Builder->CreateLoad(TypeInfo->LLVMType, Info->Storage, Var->getName().c_str());
        if (TypeInfo->isString()) {
            Loaded = emitStringNullGuard(Loaded);
        }
        return Loaded;
    }

    if (auto *ArrAcc = dynamic_cast<ArrayAccessExprAST*>(Expr)) {
        return Arrays->emitArrayAccess(ArrAcc, *this);
    }

    if (auto *Unary = dynamic_cast<UnaryExprAST*>(Expr)) {
        if (Unary->getOp() == tok_not) {
            const TypeInfo *Info = getExprTypeInfo(Unary->getOperand());
            Value *Operand = Info && Info->isC()
                ? convertNumericValue(emitExpr(Unary->getOperand()), Info, resolveType("BOOLEAN"))
                : emitCoercedExpr(Unary->getOperand(), resolveType("BOOLEAN"));
            if (Info && Info->Kind == TypeKind::CPointer) {
                reportError("NOT does not support C pointer operands");
                return nullptr;
            }
            if (!Operand) return nullptr;
            return Builder->CreateNot(Operand, "nottmp");
        }
        if (Unary->getOp() == '-') {
            const TypeInfo *Info = getExprTypeInfo(Unary->getOperand());
            if (Info && Info->Kind == TypeKind::CScalar && !Info->isVoid()) {
                const TypeInfo *Promoted = Types->promoteCScalar(Info);
                Value *Operand = convertNumericValue(emitExpr(Unary->getOperand()), Info, Promoted);
                if (!Operand) return nullptr;
                return Promoted->isReal() ? Builder->CreateFNeg(Operand, "c_neg")
                                          : Builder->CreateNeg(Operand, "c_neg");
            }
            if (!Info || Info->isC() || Info->isUserKind() || Info->isString() || Info->isVoid()) {
                reportError("Unary '-' requires a supported numeric operand");
                return nullptr;
            }
            Value *Operand = emitExpr(Unary->getOperand());
            if (!Operand) return nullptr;
            return ArithHandler->emitBinaryOp('-', ConstantInt::get(Type::getInt64Ty(*TheContext), 0),
                                             Operand, 0);
        }
        return nullptr;
    }

    if (auto *Bin = dynamic_cast<BinaryExprAST*>(Expr)) {
        // Whole arrays evaluate to raw data pointers and ^x to raw addresses;
        // neither is a value any operator may consume.
        if (isWholeArrayVar(Bin->getLHS()) || isWholeArrayVar(Bin->getRHS())) {
            reportError("A whole array cannot be used as an operand; index it");
            return nullptr;
        }
        if (dynamic_cast<AddrOfExprAST*>(Bin->getLHS()) ||
            dynamic_cast<AddrOfExprAST*>(Bin->getRHS())) {
            reportError("'^' (address-of) can only be used where a pointer type is expected");
            return nullptr;
        }

        // Enum/Record/Pointer operands never reach ArithmeticHandler or the
        // '&' path: their admissible operations are decided by name here.
        const TypeInfo *LT = getExprTypeInfo(Bin->getLHS());
        const TypeInfo *RT = getExprTypeInfo(Bin->getRHS());
        Value *L = emitExpr(Bin->getLHS());
        Value *R = emitExpr(Bin->getRHS());
        return emitBinaryOp(Bin->getOp(), L, LT, R, RT, Bin->getLine());
    }

    if (auto *Call = dynamic_cast<CallExprAST*>(Expr)) {
        std::string Name = Call->getCallee();
        if (const BuiltinInfo *B = findBuiltin(Name)) {
            if (Call->getArgs().size() != B->Arity) {
                reportError("%s expects %u arg%s", B->Name, B->Arity, B->Arity == 1 ? "" : "s");
                return nullptr;
            }
        }
        if (Name == "LENGTH") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            if (!Str) return nullptr;
            return StrHandler->emitLength(Str);
        }
        if (Name == "MID") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            Value *Start = emitCoercedExpr(Call->getArgs()[1].get(), resolveType("INTEGER"));
            Value *Len = emitCoercedExpr(Call->getArgs()[2].get(), resolveType("INTEGER"));
            if (!Str || !Start || !Len) return nullptr;
            return StrHandler->emitMid(Str, Start, Len);
        }
        if (Name == "RIGHT") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            Value *Len = emitCoercedExpr(Call->getArgs()[1].get(), resolveType("INTEGER"));
            if (!Str || !Len) return nullptr;
            return StrHandler->emitRight(Str, Len);
        }
        if (Name == "LEFT") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            Value *Len = emitCoercedExpr(Call->getArgs()[1].get(), resolveType("INTEGER"));
            if (!Str || !Len) return nullptr;
            return StrHandler->emitLeft(Str, Len);
        }
        if (Name == "LCASE") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            if (!Str) return nullptr;
            return StrHandler->emitLCase(Str);
        }
        if (Name == "UCASE") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            if (!Str) return nullptr;
            return StrHandler->emitUCase(Str);
        }
        if (Name == "ASC") {
            const TypeInfo *ArgType = getExprTypeInfo(Call->getArgs()[0].get());
            if (ArgType && (ArgType->isUserKind() || ArgType->isC())) {
                reportError("ASC expects a CHAR or STRING (got %s)", ArgType->Name.c_str());
                return nullptr;
            }
            Value *ArgVal = emitExpr(Call->getArgs()[0].get());
            if (!ArgVal) return nullptr;
            Value *CharVal = nullptr;
            if (ArgType && ArgType->isChar()) {
                CharVal = coerceValueToType(ArgVal, resolveType("CHAR"));
            } else {
                CharVal = Builder->CreateLoad(Type::getInt8Ty(*TheContext), ArgVal, "char_load");
            }
            Value *AscVal = Builder->CreateZExt(CharVal, Type::getInt32Ty(*TheContext), "asc_val");
            return coerceValueToType(AscVal, resolveType("INTEGER"));
        }
        if (Name == "CHR") {
            Value *IntVal = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("INTEGER"));
            if (!IntVal) return nullptr;
            Value *CharVal = Builder->CreateTrunc(IntVal, Type::getInt8Ty(*TheContext), "chr_val");
            return coerceValueToType(CharVal, resolveType("STRING"));
        }
        if (Name == "IS_NUM") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            if (!Str) return nullptr;
            return StrConvHandler->emitIsNum(Str);
        }
        if (Name == "NUM_TO_STR") {
            const TypeInfo *ArgType = getExprTypeInfo(Call->getArgs()[0].get());
            if (ArgType && (ArgType->isUserKind() || ArgType->isC())) {
                reportError("NUM_TO_STR expects a number (got %s)", ArgType->Name.c_str());
                return nullptr;
            }
            Value *NumV = emitExpr(Call->getArgs()[0].get());
            if (!NumV) return nullptr;
            bool IsReal = NumV->getType()->isDoubleTy();
            if (NumV->getType()->isIntegerTy(8)) {
                NumV = coerceValueToType(NumV, resolveType("INTEGER"));
            }
            return StrConvHandler->emitNumToStr(NumV, IsReal);
        }
        if (Name == "STR_TO_NUM") {
            Value *Str = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            if (!Str) return nullptr;
            return StrConvHandler->emitStrToNum(Str, true);
        }
        if (Name == "EOF") {
            if (isWholeArrayVar(Call->getArgs()[0].get())) {
                reportError("Cannot use a whole array as the EOF filename");
                return nullptr;
            }
            Value *FileName = emitCoercedExpr(Call->getArgs()[0].get(), resolveType("STRING"));
            if (!FileName) return nullptr;
            return Files->emitEof(FileName, Call->getLine());
        }

        std::vector<Value*> Args;
        if (!marshalCallArgs(Name, Call->getArgs(), Call->getLine(), Args)) return nullptr;
        return FuncGen->emitCallExpr(Call, Args);
    }

    if (auto *D = dynamic_cast<DesignatorExprAST*>(Expr)) {
        LValueInfo LV;
        if (!emitLValue(D, LV)) return nullptr;
        return loadFromLValue(LV);
    }

    // ^x produces a raw address; it is only admissible where a pointer type
    // is expected, which emitCoercedExpr enforces by pointee name.
    if (auto *Addr = dynamic_cast<AddrOfExprAST*>(Expr)) {
        LValueInfo LV;
        if (!emitLValue(Addr->getTarget(), LV)) return nullptr;
        return LV.Addr;
    }

    return nullptr;
}

// Ordinary binary expressions and CASE use the same typed operations.
Value *CodeGen::emitBinaryOp(int Op, Value *L, const TypeInfo *LT,
                             Value *R, const TypeInfo *RT, int Line) {
    if (!L || !R) return nullptr;
    if ((LT && LT->isC()) || (RT && RT->isC()))
        return emitCBinaryOp(Op, L, LT, R, RT, Line);
    if ((LT && LT->isUserKind()) || (RT && RT->isUserKind()))
        return emitUserKindBinaryOp(Op, L, LT, R, RT, Line);
    if (Op == '&') {
        L = coerceValueToType(L, resolveType("STRING"));
        R = coerceValueToType(R, resolveType("STRING"));
        return L && R ? StrHandler->emitConcat(L, R) : nullptr;
    }
    switch (Op) {
    case tok_eq: case tok_ne: case '<': case '>': case tok_le: case tok_ge:
        if ((LT && LT->isString()) || (RT && RT->isString())) {
            if (!(LT && LT->isString() && RT && RT->isString())) {
                reportError("STRING comparison requires two STRING operands (line %d)", Line);
                return nullptr;
            }
            return StrHandler->emitCompare(Op, L, R);
        }
        break;
    default: break;
    }
    return ArithHandler->emitBinaryOp(Op, L, R, Line);
}