summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorNagi5Yeq <wu@x-nagi.com>2020-05-29 00:08:49 +0800
committerNagi5Yeq <wu@x-nagi.com>2020-05-29 00:08:49 +0800
commitd4e6ae6f8237f18d9d0b51e7a6404118dffb709f (patch)
treeeb0969f20e5959866e535ac8fe5c84db55595ab5 /node
parent404afbef3c9223bc44ddd91066c52c44349e43c7 (diff)
add pointer and shifting support
Diffstat (limited to 'node')
-rw-r--r--node/ExprNode.cc11
-rw-r--r--node/ExprNode.hh11
-rw-r--r--node/StatementNode.cc20
3 files changed, 32 insertions, 10 deletions
diff --git a/node/ExprNode.cc b/node/ExprNode.cc
index 4183120..ba505fc 100644
--- a/node/ExprNode.cc
+++ b/node/ExprNode.cc
@@ -66,6 +66,17 @@ Value L2RCastingNode::CodeGen() {
return context_->GetBuilder()->CreateLoad(type_->GetLLVMType(), left);
}
+GetAddressNode::GetAddressNode(Context* context,
+ std::shared_ptr<ExprNode> lvalue)
+ : ExprNode(context)
+ , lvalue_(lvalue) {}
+
+Value GetAddressNode::CodeGen() {
+ Value left = lvalue_->CodeGen();
+ type_ = context_->GetTypeManager()->GetPointerType(lvalue_->GetType());
+ return left;
+}
+
IdentifierNode::IdentifierNode(Context* context, const std::string& name)
: ExprNode(context)
, name_(name) {}
diff --git a/node/ExprNode.hh b/node/ExprNode.hh
index c80d955..62c8af9 100644
--- a/node/ExprNode.hh
+++ b/node/ExprNode.hh
@@ -60,6 +60,17 @@ class L2RCastingNode : public ExprNode {
std::shared_ptr<ExprNode> lvalue_;
};
+// like L2RCastingNode, but won't do the load, type is the pointer to lvalue
+// instead.
+class GetAddressNode : public ExprNode {
+ public:
+ GetAddressNode(Context* context, std::shared_ptr<ExprNode> lvalue);
+ virtual Value CodeGen() override;
+
+ protected:
+ std::shared_ptr<ExprNode> lvalue_;
+};
+
class IdentifierNode : public ExprNode {
public:
IdentifierNode(Context* context, const std::string& name);
diff --git a/node/StatementNode.cc b/node/StatementNode.cc
index 9ae5a9c..5eb17b2 100644
--- a/node/StatementNode.cc
+++ b/node/StatementNode.cc
@@ -49,12 +49,12 @@ Value IfStatementNode::CodeGen() {
llvm::BasicBlock::Create(GlobalLLVMContext, "then", body);
llvm::BasicBlock* EndBlock =
llvm::BasicBlock::Create(GlobalLLVMContext, "endif");
- Value ExprValue = manager->CreateCast(boolean, expr_->GetType(),
- expr_->CodeGen(), context_);
+ Value cond = expr_->CodeGen();
+ cond = manager->CreateCast(boolean, expr_->GetType(), cond, context_);
if (ElsePart_ != nullptr) {
llvm::BasicBlock* ElseBlock =
llvm::BasicBlock::Create(GlobalLLVMContext, "else");
- builder->CreateCondBr(ExprValue, ThenBlock, ElseBlock);
+ builder->CreateCondBr(cond, ThenBlock, ElseBlock);
builder->SetInsertPoint(ThenBlock);
ThenPart_->CodeGen();
builder->CreateBr(EndBlock);
@@ -66,7 +66,7 @@ Value IfStatementNode::CodeGen() {
builder->SetInsertPoint(EndBlock);
return nullptr;
}
- builder->CreateCondBr(ExprValue, ThenBlock, EndBlock);
+ builder->CreateCondBr(cond, ThenBlock, EndBlock);
builder->SetInsertPoint(ThenBlock);
ThenPart_->CodeGen();
builder->CreateBr(EndBlock);
@@ -96,9 +96,9 @@ Value WhileStatementNode::CodeGen() {
llvm::BasicBlock::Create(GlobalLLVMContext, "done");
builder->CreateBr(StartBlock);
builder->SetInsertPoint(StartBlock);
- Value ExprValue = manager->CreateCast(boolean, expr_->GetType(),
- expr_->CodeGen(), context_);
- builder->CreateCondBr(ExprValue, LoopBlock, EndBlock);
+ Value cond = expr_->CodeGen();
+ cond = manager->CreateCast(boolean, expr_->GetType(), cond, context_);
+ builder->CreateCondBr(cond, LoopBlock, EndBlock);
body->getBasicBlockList().push_back(LoopBlock);
builder->SetInsertPoint(LoopBlock);
WhileBody_->CodeGen();
@@ -128,9 +128,9 @@ Value RepeatStatementNode::CodeGen() {
builder->CreateBr(LoopBlock);
builder->SetInsertPoint(LoopBlock);
RepeatBody_->CodeGen();
- Value ExprValue = manager->CreateCast(boolean, expr_->GetType(),
- expr_->CodeGen(), context_);
- builder->CreateCondBr(ExprValue, EndBlock, LoopBlock);
+ Value cond = expr_->CodeGen();
+ cond = manager->CreateCast(boolean, expr_->GetType(), cond, context_);
+ builder->CreateCondBr(cond, EndBlock, LoopBlock);
body->getBasicBlockList().push_back(EndBlock);
builder->SetInsertPoint(EndBlock);
return nullptr;