summaryrefslogtreecommitdiff
path: root/node/LiteralNode.cc
blob: 1e9c4b735f33d4ecc49c5c031a9ca5cfe922ce93 (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
#include <llvm/ADT/APFloat.h>
#include <llvm/ADT/APInt.h>

#include "Driver.hh"
#include "Node.hh"

namespace pcc {
template <> Value BooleanLiteralNode::CodeGen() {
    llvm::LLVMContext& context = driver_->GetModule()->getContext();
    if (value_ == true) {
        return llvm::ConstantInt::getTrue(context);
    } else {
        return llvm::ConstantInt::getFalse(context);
    }
}

template <> Value IntegerLiteralNode::CodeGen() {
    llvm::LLVMContext& context = driver_->GetModule()->getContext();
    llvm::APInt value(32, (uint64_t) value_, true);
    return llvm::ConstantInt::get(context, value);
}

template <> Value RealLiteralNode::CodeGen() {
    llvm::LLVMContext& context = driver_->GetModule()->getContext();
    llvm::APFloat value(value_);
    return llvm::ConstantFP::get(context, value);
}

template <> Value StringLiteralNode::CodeGen() {
    llvm::Module* module = driver_->GetModule();
    llvm::LLVMContext& context = module->getContext();
    llvm::Constant* str = llvm::ConstantDataArray::getString(context, value_);
    llvm::Type* type = str->getType();
    llvm::GlobalVariable* var = new llvm::GlobalVariable(
        *module, type, true, llvm::GlobalVariable::InternalLinkage, str);
    llvm::Constant* zero =
        llvm::Constant::getNullValue(llvm::IntegerType::getInt32Ty(context));
    llvm::Constant* indices[] = {zero, zero};
    llvm::Constant* ptr =
        llvm::ConstantExpr::getGetElementPtr(type, var, indices, true);
    return ptr;
}
} // namespace pcc