mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-21 05:45:19 +00:00
Add intrinsics.mem_copy and intrinsics.mem_copy_non_overlapping
This commit is contained in:
@@ -105,17 +105,9 @@ mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
|
||||
if src == nil {
|
||||
return dst;
|
||||
}
|
||||
|
||||
// NOTE(bill): This _must_ be implemented like C's memmove
|
||||
foreign _ {
|
||||
when size_of(rawptr) == 8 {
|
||||
@(link_name="llvm.memmove.p0i8.p0i8.i64")
|
||||
llvm_memmove :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
|
||||
} else {
|
||||
@(link_name="llvm.memmove.p0i8.p0i8.i32")
|
||||
llvm_memmove :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
|
||||
}
|
||||
}
|
||||
llvm_memmove(dst, src, len);
|
||||
intrinsics.mem_copy(dst, src, len);
|
||||
return dst;
|
||||
}
|
||||
|
||||
@@ -123,17 +115,9 @@ mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> r
|
||||
if src == nil {
|
||||
return dst;
|
||||
}
|
||||
|
||||
// NOTE(bill): This _must_ be implemented like C's memcpy
|
||||
foreign _ {
|
||||
when size_of(rawptr) == 8 {
|
||||
@(link_name="llvm.memcpy.p0i8.p0i8.i64")
|
||||
llvm_memcpy :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
|
||||
} else {
|
||||
@(link_name="llvm.memcpy.p0i8.p0i8.i32")
|
||||
llvm_memcpy :: proc "none" (dst, src: rawptr, len: int, is_volatile: bool = false) ---;
|
||||
}
|
||||
}
|
||||
llvm_memcpy(dst, src, len);
|
||||
intrinsics.mem_copy_non_overlapping(dst, src, len);
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
@@ -2053,6 +2053,59 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
|
||||
}
|
||||
break;
|
||||
|
||||
case BuiltinProc_mem_copy:
|
||||
case BuiltinProc_mem_copy_non_overlapping:
|
||||
{
|
||||
operand->mode = Addressing_NoValue;
|
||||
operand->type = t_invalid;
|
||||
|
||||
Operand dst = {};
|
||||
Operand src = {};
|
||||
Operand len = {};
|
||||
check_expr(c, &dst, ce->args[0]);
|
||||
check_expr(c, &src, ce->args[1]);
|
||||
check_expr(c, &len, ce->args[2]);
|
||||
if (dst.mode == Addressing_Invalid) {
|
||||
return false;
|
||||
}
|
||||
if (src.mode == Addressing_Invalid) {
|
||||
return false;
|
||||
}
|
||||
if (len.mode == Addressing_Invalid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (!is_type_pointer(dst.type)) {
|
||||
gbString str = type_to_string(dst.type);
|
||||
error(dst.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
|
||||
gb_string_free(str);
|
||||
return false;
|
||||
}
|
||||
if (!is_type_pointer(src.type)) {
|
||||
gbString str = type_to_string(src.type);
|
||||
error(src.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_procs[id].name), str);
|
||||
gb_string_free(str);
|
||||
return false;
|
||||
}
|
||||
if (!is_type_integer(len.type)) {
|
||||
gbString str = type_to_string(len.type);
|
||||
error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_procs[id].name), str);
|
||||
gb_string_free(str);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (len.mode == Addressing_Constant) {
|
||||
i64 n = exact_value_to_i64(len.value);
|
||||
if (n < 0) {
|
||||
gbString str = expr_to_string(len.expr);
|
||||
error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_procs[id].name), str);
|
||||
gb_string_free(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case BuiltinProc_atomic_fence:
|
||||
case BuiltinProc_atomic_fence_acq:
|
||||
|
||||
@@ -58,6 +58,9 @@ enum BuiltinProcId {
|
||||
|
||||
BuiltinProc_sqrt,
|
||||
|
||||
BuiltinProc_mem_copy,
|
||||
BuiltinProc_mem_copy_non_overlapping,
|
||||
|
||||
BuiltinProc_volatile_store,
|
||||
BuiltinProc_volatile_load,
|
||||
|
||||
@@ -282,6 +285,9 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
||||
|
||||
{STR_LIT("sqrt"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
{STR_LIT("mem_copy"), 3, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
|
||||
{STR_LIT("mem_copy_non_overlapping"), 3, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
|
||||
|
||||
{STR_LIT("volatile_store"), 2, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
|
||||
{STR_LIT("volatile_load"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
|
||||
@@ -9451,6 +9451,44 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
|
||||
return res;
|
||||
}
|
||||
|
||||
case BuiltinProc_mem_copy:
|
||||
case BuiltinProc_mem_copy_non_overlapping:
|
||||
{
|
||||
|
||||
|
||||
lbValue dst = lb_build_expr(p, ce->args[0]);
|
||||
lbValue src = lb_build_expr(p, ce->args[1]);
|
||||
lbValue len = lb_build_expr(p, ce->args[2]);
|
||||
dst = lb_emit_conv(p, dst, t_rawptr);
|
||||
src = lb_emit_conv(p, src, t_rawptr);
|
||||
len = lb_emit_conv(p, len, t_int);
|
||||
|
||||
char const *name = nullptr;
|
||||
switch (id) {
|
||||
case BuiltinProc_mem_copy: name = "llvm.memmove"; break;
|
||||
case BuiltinProc_mem_copy_non_overlapping: name = "llvm.memcpy"; break;
|
||||
}
|
||||
|
||||
LLVMTypeRef types[3] = {
|
||||
lb_type(p->module, t_rawptr),
|
||||
lb_type(p->module, t_rawptr),
|
||||
lb_type(p->module, t_int)
|
||||
};
|
||||
unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
|
||||
GB_ASSERT_MSG(id != 0, "Unable to find %s.%s.%s.%s", name, LLVMPrintTypeToString(types[0]), LLVMPrintTypeToString(types[1]), LLVMPrintTypeToString(types[2]));
|
||||
LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
|
||||
|
||||
LLVMValueRef args[4] = {};
|
||||
args[0] = dst.value;
|
||||
args[1] = src.value;
|
||||
args[2] = len.value;
|
||||
args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, false); // is_volatile parameter
|
||||
|
||||
LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
case BuiltinProc_atomic_fence:
|
||||
LLVMBuildFence(p->builder, LLVMAtomicOrderingSequentiallyConsistent, false, "");
|
||||
|
||||
Reference in New Issue
Block a user