Commit a48fce1c authored by Alastair Robertson's avatar Alastair Robertson

Add dereference operator

parent c198723b
...@@ -80,6 +80,7 @@ std::string opstr(Unop &unop) ...@@ -80,6 +80,7 @@ std::string opstr(Unop &unop)
switch (unop.op) { switch (unop.op) {
case bpftrace::Parser::token::LNOT: return "!"; case bpftrace::Parser::token::LNOT: return "!";
case bpftrace::Parser::token::BNOT: return "~"; case bpftrace::Parser::token::BNOT: return "~";
case bpftrace::Parser::token::MUL: return "dereference";
default: abort(); default: abort();
} }
} }
......
...@@ -124,6 +124,13 @@ void CodegenLLVM::visit(Unop &unop) ...@@ -124,6 +124,13 @@ void CodegenLLVM::visit(Unop &unop)
switch (unop.op) { switch (unop.op) {
case bpftrace::Parser::token::LNOT: expr_ = b_.CreateNot(expr_); break; case bpftrace::Parser::token::LNOT: expr_ = b_.CreateNot(expr_); break;
case bpftrace::Parser::token::BNOT: expr_ = b_.CreateNeg(expr_); break; case bpftrace::Parser::token::BNOT: expr_ = b_.CreateNeg(expr_); break;
case bpftrace::Parser::token::MUL:
{
AllocaInst *dst = b_.CreateAllocaBPF();
b_.CreateProbeRead(dst, b_.getInt64(8), expr_);
expr_ = b_.CreateLoad(dst);
break;
}
default: abort(); default: abort();
} }
} }
......
...@@ -148,6 +148,7 @@ expr : INT { $$ = new ast::Integer($1); } ...@@ -148,6 +148,7 @@ expr : INT { $$ = new ast::Integer($1); }
| expr BXOR expr { $$ = new ast::Binop($1, token::BXOR, $3); } | expr BXOR expr { $$ = new ast::Binop($1, token::BXOR, $3); }
| LNOT expr { $$ = new ast::Unop(token::LNOT, $2); } | LNOT expr { $$ = new ast::Unop(token::LNOT, $2); }
| BNOT expr { $$ = new ast::Unop(token::BNOT, $2); } | BNOT expr { $$ = new ast::Unop(token::BNOT, $2); }
| MUL expr { $$ = new ast::Unop(token::MUL, $2); }
; ;
call : IDENT "(" ")" { $$ = new ast::Call($1); } call : IDENT "(" ")" { $$ = new ast::Call($1); }
......
...@@ -142,6 +142,11 @@ void SemanticAnalyser::visit(Binop &binop) ...@@ -142,6 +142,11 @@ void SemanticAnalyser::visit(Binop &binop)
void SemanticAnalyser::visit(Unop &unop) void SemanticAnalyser::visit(Unop &unop)
{ {
unop.expr->accept(*this); unop.expr->accept(*this);
if (type_ != Type::integer) {
err_ << "The " << opstr(unop) << " operator can not be used on expressions of type " << type_ << std::endl;
}
type_ = Type::integer; type_ = Type::integer;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment