Commit e5c7a61f authored by Alastair Robertson's avatar Alastair Robertson

Add delete() function

parent 2ee7b564
......@@ -146,6 +146,11 @@ void CodegenLLVM::visit(AssignMapCallStatement &assignment)
b_.CreateStore(b_.CreateAdd(oldval, b_.getInt64(1)), newval);
b_.CreateMapUpdateElem(map, key, newval);
}
else if (call.func == "delete")
{
AllocaInst *key = getMapKey(map);
b_.CreateMapDeleteElem(map, key);
}
else
{
abort();
......
......@@ -103,6 +103,25 @@ void IRBuilderBPF::CreateMapUpdateElem(Map &map, AllocaInst *key, Value *val)
CallInst *call = CreateCall(update_func, {map_ptr, key, val, flags});
}
void IRBuilderBPF::CreateMapDeleteElem(Map &map, AllocaInst *key)
{
Value *map_ptr = CreateBpfPseudoCall(map);
Value *flags = getInt64(0);
// int map_delete_elem(&map, &key)
// Return: 0 on success or negative error
FunctionType *delete_func_type = FunctionType::get(
getInt64Ty(),
{getInt8PtrTy(), getInt8PtrTy()},
false);
PointerType *delete_func_ptr_type = PointerType::get(delete_func_type, 0);
Constant *delete_func = ConstantExpr::getCast(
Instruction::IntToPtr,
getInt64(BPF_FUNC_map_delete_elem),
delete_func_ptr_type);
CallInst *call = CreateCall(delete_func, {map_ptr, key});
}
CallInst *IRBuilderBPF::CreateGetNs()
{
// u64 ktime_get_ns()
......
......@@ -21,6 +21,7 @@ public:
CallInst *CreateBpfPseudoCall(Map &map);
LoadInst *CreateMapLookupElem(Map &map, AllocaInst *key);
void CreateMapUpdateElem(Map &map, AllocaInst *key, Value *val);
void CreateMapDeleteElem(Map &map, AllocaInst *key);
CallInst *CreateGetNs();
CallInst *CreateGetPidTgid();
CallInst *CreateGetUidGid();
......
......@@ -51,6 +51,13 @@ void SemanticAnalyser::visit(Call &call)
err_ << nargs << " provided)" << std::endl;
}
}
else if (call.func == "delete") {
// Don't assign a type
if (nargs != 0) {
err_ << "delete() should take 0 arguments (";
err_ << nargs << " provided)" << std::endl;
}
}
else {
type_ = Type::none;
err_ << "Unknown function: '" << call.func << "'" << std::endl;
......
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