rw_engine_.reset();
}
+static LoadInst *createLoad(IRBuilder<> &B, Value *addr, bool isVolatile = false)
+{
+#if LLVM_MAJOR_VERSION >= 14
+ return B.CreateLoad(addr->getType()->getPointerElementType(), addr, isVolatile);
+#else
+ return B.CreateLoad(addr, isVolatile);
+#endif
+}
+
+static Value *createInBoundsGEP(IRBuilder<> &B, Value *ptr, ArrayRef<Value *>idxlist)
+{
+#if LLVM_MAJOR_VERSION >= 14
+ return B.CreateInBoundsGEP(ptr->getType()->getScalarType()->getPointerElementType(),
+ ptr, idxlist);
+#else
+ return B.CreateInBoundsGEP(ptr, idxlist);
+#endif
+}
+
static void debug_printf(Module *mod, IRBuilder<> &B, const string &fmt, vector<Value *> args) {
GlobalVariable *fmt_gvar = B.CreateGlobalString(fmt, "fmt");
- args.insert(args.begin(), B.CreateInBoundsGEP(fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)})));
+ args.insert(args.begin(), createInBoundsGEP(B, fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)})));
args.insert(args.begin(), B.getInt64((uintptr_t)stderr));
Function *fprintf_fn = mod->getFunction("fprintf");
if (!fprintf_fn) {
*fmt += "%n";
B.CreateStore(B.getInt32(0), nread);
GlobalVariable *fmt_gvar = B.CreateGlobalString(*fmt, "fmt");
- (*args)[1] = B.CreateInBoundsGEP(fmt_gvar, {B.getInt64(0), B.getInt64(0)});
- (*args)[0] = B.CreateLoad(sptr);
+ (*args)[1] = createInBoundsGEP(B, fmt_gvar, {B.getInt64(0), B.getInt64(0)});
+ (*args)[0] = createLoad(B, sptr);
args->push_back(nread);
CallInst *call = B.CreateCall(sscanf_fn, *args);
call->setTailCall(true);
B.SetInsertPoint(label_false);
// s = &s[nread];
B.CreateStore(
- B.CreateInBoundsGEP(B.CreateLoad(sptr), B.CreateLoad(nread, true)), sptr);
+ createInBoundsGEP(B, createLoad(B, sptr), createLoad(B, nread, true)), sptr);
args->resize(2);
fmt->clear();
*fmt += "x";
else
*fmt += "i";
- args->push_back(is_writer ? B.CreateLoad(out) : out);
+ args->push_back(is_writer ? createLoad(B, out) : out);
}
}
GlobalVariable *fmt_gvar = B.CreateGlobalString(fmt, "fmt");
- args[2] = B.CreateInBoundsGEP(fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)}));
+ args[2] = createInBoundsGEP(B, fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)}));
if (0)
debug_printf(mod, B, "%d %p %p\n", vector<Value *>({arg_len, arg_out, arg_in}));
#endif
}
+LoadInst *CodegenLLVM::createLoad(Value *addr)
+{
+#if LLVM_MAJOR_VERSION >= 14
+ return B.CreateLoad(addr->getType()->getPointerElementType(), addr);
+#else
+ return B.CreateLoad(addr);
+#endif
+}
+
+Value *CodegenLLVM::createInBoundsGEP(Value *ptr, ArrayRef<Value *>idxlist)
+{
+#if LLVM_MAJOR_VERSION >= 14
+ return B.CreateInBoundsGEP(ptr->getType()->getScalarType()->getPointerElementType(),
+ ptr, idxlist);
+#else
+ return B.CreateInBoundsGEP(ptr, idxlist);
+#endif
+}
+
StatusTuple CodegenLLVM::visit_block_stmt_node(BlockStmtNode *n) {
// enter scope
emit("%s%s->%s", n->decl_->scope_id(), n->c_str(), n->sub_name_.c_str());
auto it = vars_.find(n->decl_);
if (it == vars_.end()) return mkstatus_(n, "Cannot locate variable %s in vars_ table", n->c_str());
- LoadInst *load_1 = B.CreateLoad(it->second);
+ LoadInst *load_1 = createLoad(it->second);
vector<Value *> indices({B.getInt32(0), B.getInt32(n->sub_decl_->slot_)});
- expr_ = B.CreateInBoundsGEP(load_1, indices);
+ expr_ = createInBoundsGEP(load_1, indices);
if (!n->is_lhs())
- expr_ = B.CreateLoad(pop_expr());
+ expr_ = createLoad(pop_expr());
}
}
} else {
auto it = vars_.find(n->decl_);
if (it == vars_.end()) return mkstatus_(n, "Cannot locate variable %s in vars_ table", n->c_str());
- expr_ = n->is_lhs() ? it->second : (Value *)B.CreateLoad(it->second);
+ expr_ = n->is_lhs() ? it->second : (Value *)createLoad(it->second);
}
} else {
if (n->sub_name_.size()) {
vector<Value *> indices({const_int(0), const_int(n->sub_decl_->slot_, 32)});
expr_ = B.CreateGEP(nullptr, it->second, indices);
if (!n->is_lhs())
- expr_ = B.CreateLoad(pop_expr());
+ expr_ = createLoad(pop_expr());
} else {
if (n->bitop_) {
// ident is holding a host endian number, don't use dext
if (n->is_lhs() || n->decl_->is_struct())
expr_ = it->second;
else
- expr_ = B.CreateLoad(it->second);
+ expr_ = createLoad(it->second);
}
}
}
}
if (n->is_ref()) {
// e.g.: @ip.hchecksum, return offset of the header within packet
- LoadInst *offset_ptr = B.CreateLoad(offset_mem);
+ LoadInst *offset_ptr = createLoad(offset_mem);
Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3));
expr_ = B.CreateIntCast(skb_hdr_offset, B.getInt64Ty(), false);
} else if (n->is_lhs()) {
emit("bpf_dins_pkt(pkt, %s + %zu, %zu, %zu, ", n->id_->c_str(), bit_offset >> 3, bit_offset & 0x7, bit_width);
Function *store_fn = mod_->getFunction("bpf_dins_pkt");
if (!store_fn) return mkstatus_(n, "unable to find function bpf_dins_pkt");
- LoadInst *skb_ptr = B.CreateLoad(skb_mem);
+ LoadInst *skb_ptr = createLoad(skb_mem);
Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy());
- LoadInst *offset_ptr = B.CreateLoad(offset_mem);
+ LoadInst *offset_ptr = createLoad(offset_mem);
Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3));
Value *rhs = B.CreateIntCast(pop_expr(), B.getInt64Ty(), false);
createCall(store_fn, vector<Value *>({skb_ptr8, skb_hdr_offset, B.getInt64(bit_offset & 0x7),
emit("bpf_dext_pkt(pkt, %s + %zu, %zu, %zu)", n->id_->c_str(), bit_offset >> 3, bit_offset & 0x7, bit_width);
Function *load_fn = mod_->getFunction("bpf_dext_pkt");
if (!load_fn) return mkstatus_(n, "unable to find function bpf_dext_pkt");
- LoadInst *skb_ptr = B.CreateLoad(skb_mem);
+ LoadInst *skb_ptr = createLoad(skb_mem);
Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy());
- LoadInst *offset_ptr = B.CreateLoad(offset_mem);
+ LoadInst *offset_ptr = createLoad(offset_mem);
Value *skb_hdr_offset = B.CreateAdd(offset_ptr, B.getInt64(bit_offset >> 3));
expr_ = createCall(load_fn, vector<Value *>({skb_ptr8, skb_hdr_offset,
B.getInt64(bit_offset & 0x7), B.getInt64(bit_width)}));
VariableDeclStmtNode *skb_decl;
Value *skb_mem;
TRY2(lookup_var(n, "skb", scopes_->current_var(), &skb_decl, &skb_mem));
- LoadInst *skb_ptr = B.CreateLoad(skb_mem);
+ LoadInst *skb_ptr = createLoad(skb_mem);
Value *skb_ptr8 = B.CreateBitCast(skb_ptr, B.getInt8PtrTy());
expr_ = createCall(csum_fn, vector<Value *>({skb_ptr8, offset, old_val, new_val, flags}));
B.SetInsertPoint(label_end);
vector<Value *> indices({B.getInt32(0), B.getInt32(n->sub_decl_->slot_)});
- expr_ = B.CreateInBoundsGEP(result, indices);
+ expr_ = createInBoundsGEP(result, indices);
} else {
B.CreateCondBr(B.CreateIsNotNull(result), label_then, label_end);
B.SetInsertPoint(label_then);
vector<Value *> indices({B.getInt32(0), B.getInt32(n->sub_decl_->slot_)});
- Value *field = B.CreateInBoundsGEP(result, indices);
+ Value *field = createInBoundsGEP(result, indices);
B.CreateBr(label_end);
B.SetInsertPoint(label_end);
if (result == vars_.end()) return mkstatus_(n, "unable to find memory for _result built-in");
vars_[leaf_n] = result->second;
- Value *load_1 = B.CreateLoad(result->second);
+ Value *load_1 = createLoad(result->second);
Value *is_null = B.CreateIsNotNull(load_1);
Function *parent = B.GetInsertBlock()->getParent();
auto result = vars_.find(result_decl);
if (result == vars_.end()) return mkstatus_(n, "unable to find memory for _result built-in");
- Value *load_1 = B.CreateLoad(result->second);
+ Value *load_1 = createLoad(result->second);
Value *is_null = B.CreateIsNull(load_1);
Function *parent = B.GetInsertBlock()->getParent();
// always return something
B.SetInsertPoint(label_return);
- B.CreateRet(B.CreateLoad(retval_));
+ B.CreateRet(createLoad(retval_));
}
return StatusTuple::OK();