mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
Removed capacity arguments from new_slice and slice_ptr
This commit is contained in:
@@ -157,7 +157,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
|
|||||||
{STR_LIT(""), 0, false, Expr_Stmt},
|
{STR_LIT(""), 0, false, Expr_Stmt},
|
||||||
|
|
||||||
{STR_LIT("new"), 1, false, Expr_Expr},
|
{STR_LIT("new"), 1, false, Expr_Expr},
|
||||||
{STR_LIT("new_slice"), 2, true, Expr_Expr},
|
{STR_LIT("new_slice"), 2, false, Expr_Expr},
|
||||||
|
|
||||||
{STR_LIT("size_of"), 1, false, Expr_Expr},
|
{STR_LIT("size_of"), 1, false, Expr_Expr},
|
||||||
{STR_LIT("size_of_val"), 1, false, Expr_Expr},
|
{STR_LIT("size_of_val"), 1, false, Expr_Expr},
|
||||||
@@ -181,7 +181,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
|
|||||||
|
|
||||||
// {STR_LIT("ptr_offset"), 2, false, Expr_Expr},
|
// {STR_LIT("ptr_offset"), 2, false, Expr_Expr},
|
||||||
// {STR_LIT("ptr_sub"), 2, false, Expr_Expr},
|
// {STR_LIT("ptr_sub"), 2, false, Expr_Expr},
|
||||||
{STR_LIT("slice_ptr"), 2, true, Expr_Expr},
|
{STR_LIT("slice_ptr"), 2, false, Expr_Expr},
|
||||||
|
|
||||||
{STR_LIT("min"), 2, false, Expr_Expr},
|
{STR_LIT("min"), 2, false, Expr_Expr},
|
||||||
{STR_LIT("max"), 2, false, Expr_Expr},
|
{STR_LIT("max"), 2, false, Expr_Expr},
|
||||||
|
|||||||
+2
-46
@@ -2643,7 +2643,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
|||||||
operand->type = make_type_pointer(c->allocator, type);
|
operand->type = make_type_pointer(c->allocator, type);
|
||||||
} break;
|
} break;
|
||||||
case BuiltinProc_new_slice: {
|
case BuiltinProc_new_slice: {
|
||||||
// new_slice :: proc(Type, len: int[, cap: int]) -> []Type
|
// new_slice :: proc(Type, len: int) -> []Type
|
||||||
Operand op = {0};
|
Operand op = {0};
|
||||||
check_expr_or_type(c, &op, ce->args.e[0]);
|
check_expr_or_type(c, &op, ce->args.e[0]);
|
||||||
Type *type = op.type;
|
Type *type = op.type;
|
||||||
@@ -2653,10 +2653,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
|||||||
}
|
}
|
||||||
|
|
||||||
AstNode *len = ce->args.e[1];
|
AstNode *len = ce->args.e[1];
|
||||||
AstNode *cap = NULL;
|
|
||||||
if (ce->args.count > 2) {
|
|
||||||
cap = ce->args.e[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
check_expr(c, &op, len);
|
check_expr(c, &op, len);
|
||||||
if (op.mode == Addressing_Invalid) {
|
if (op.mode == Addressing_Invalid) {
|
||||||
@@ -2669,23 +2665,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cap != NULL) {
|
|
||||||
check_expr(c, &op, cap);
|
|
||||||
if (op.mode == Addressing_Invalid) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!is_type_integer(op.type)) {
|
|
||||||
gbString type_str = type_to_string(operand->type);
|
|
||||||
error_node(call, "Capacity for `new_slice` must be an integer, got `%s`", type_str);
|
|
||||||
gb_string_free(type_str);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (ce->args.count > 3) {
|
|
||||||
error_node(call, "Too many arguments to `new_slice`, expected either 2 or 3");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
operand->mode = Addressing_Value;
|
operand->mode = Addressing_Value;
|
||||||
operand->type = make_type_slice(c->allocator, type);
|
operand->type = make_type_slice(c->allocator, type);
|
||||||
} break;
|
} break;
|
||||||
@@ -3166,7 +3145,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
case BuiltinProc_slice_ptr: {
|
case BuiltinProc_slice_ptr: {
|
||||||
// slice_ptr :: proc(a: ^T, len: int[, cap: int]) -> []T
|
// slice_ptr :: proc(a: ^T, len: int) -> []T
|
||||||
// ^T cannot be rawptr
|
// ^T cannot be rawptr
|
||||||
Type *ptr_type = base_type(operand->type);
|
Type *ptr_type = base_type(operand->type);
|
||||||
if (!is_type_pointer(ptr_type)) {
|
if (!is_type_pointer(ptr_type)) {
|
||||||
@@ -3185,10 +3164,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
|||||||
}
|
}
|
||||||
|
|
||||||
AstNode *len = ce->args.e[1];
|
AstNode *len = ce->args.e[1];
|
||||||
AstNode *cap = NULL;
|
|
||||||
if (ce->args.count > 2) {
|
|
||||||
cap = ce->args.e[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
Operand op = {0};
|
Operand op = {0};
|
||||||
check_expr(c, &op, len);
|
check_expr(c, &op, len);
|
||||||
@@ -3203,25 +3178,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cap != NULL) {
|
|
||||||
check_expr(c, &op, cap);
|
|
||||||
if (op.mode == Addressing_Invalid)
|
|
||||||
return false;
|
|
||||||
if (!is_type_integer(op.type)) {
|
|
||||||
gbString type_str = type_to_string(operand->type);
|
|
||||||
error_node(call,
|
|
||||||
"Capacity for `slice_ptr` must be an integer, got `%s`",
|
|
||||||
type_str);
|
|
||||||
gb_string_free(type_str);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (ce->args.count > 3) {
|
|
||||||
error_node(call,
|
|
||||||
"Too many arguments to `slice_ptr`, expected either 2 or 3");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
|
operand->type = make_type_slice(c->allocator, ptr_type->Pointer.elem);
|
||||||
operand->mode = Addressing_Value;
|
operand->mode = Addressing_Value;
|
||||||
} break;
|
} break;
|
||||||
|
|||||||
+7
-22
@@ -2860,15 +2860,11 @@ llirValue *llir_build_single_expr(llirProcedure *proc, AstNode *expr, TypeAndVal
|
|||||||
llirValue *elem_size = llir_make_const_int(allocator, s);
|
llirValue *elem_size = llir_make_const_int(allocator, s);
|
||||||
llirValue *elem_align = llir_make_const_int(allocator, a);
|
llirValue *elem_align = llir_make_const_int(allocator, a);
|
||||||
|
|
||||||
llirValue *len = llir_emit_conv(proc, llir_build_expr(proc, ce->args.e[1]), t_int);
|
llirValue *count = llir_emit_conv(proc, llir_build_expr(proc, ce->args.e[1]), t_int);
|
||||||
llirValue *cap = len;
|
|
||||||
if (ce->args.count == 3) {
|
|
||||||
cap = llir_emit_conv(proc, llir_build_expr(proc, ce->args.e[2]), t_int);
|
|
||||||
}
|
|
||||||
|
|
||||||
llir_emit_slice_bounds_check(proc, ast_node_token(ce->args.e[1]), v_zero, len, false);
|
llir_emit_slice_bounds_check(proc, ast_node_token(ce->args.e[1]), v_zero, count, false);
|
||||||
|
|
||||||
llirValue *slice_size = llir_emit_arith(proc, Token_Mul, elem_size, cap, t_int);
|
llirValue *slice_size = llir_emit_arith(proc, Token_Mul, elem_size, count, t_int);
|
||||||
|
|
||||||
llirValue **args = gb_alloc_array(allocator, llirValue *, 2);
|
llirValue **args = gb_alloc_array(allocator, llirValue *, 2);
|
||||||
args[0] = slice_size;
|
args[0] = slice_size;
|
||||||
@@ -2880,10 +2876,8 @@ llirValue *llir_build_single_expr(llirProcedure *proc, AstNode *expr, TypeAndVal
|
|||||||
|
|
||||||
llirValue *gep0 = llir_emit_struct_ep(proc, slice, 0);
|
llirValue *gep0 = llir_emit_struct_ep(proc, slice, 0);
|
||||||
llirValue *gep1 = llir_emit_struct_ep(proc, slice, 1);
|
llirValue *gep1 = llir_emit_struct_ep(proc, slice, 1);
|
||||||
llirValue *gep2 = llir_emit_struct_ep(proc, slice, 2);
|
|
||||||
llir_emit_store(proc, gep0, ptr);
|
llir_emit_store(proc, gep0, ptr);
|
||||||
llir_emit_store(proc, gep1, len);
|
llir_emit_store(proc, gep1, count);
|
||||||
llir_emit_store(proc, gep2, cap);
|
|
||||||
return llir_emit_load(proc, slice);
|
return llir_emit_load(proc, slice);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
@@ -3059,22 +3053,13 @@ llirValue *llir_build_single_expr(llirProcedure *proc, AstNode *expr, TypeAndVal
|
|||||||
case BuiltinProc_slice_ptr: {
|
case BuiltinProc_slice_ptr: {
|
||||||
llir_emit_comment(proc, str_lit("slice_ptr"));
|
llir_emit_comment(proc, str_lit("slice_ptr"));
|
||||||
llirValue *ptr = llir_build_expr(proc, ce->args.e[0]);
|
llirValue *ptr = llir_build_expr(proc, ce->args.e[0]);
|
||||||
llirValue *len = llir_build_expr(proc, ce->args.e[1]);
|
llirValue *count = llir_build_expr(proc, ce->args.e[1]);
|
||||||
llirValue *cap = len;
|
count = llir_emit_conv(proc, count, t_int);
|
||||||
|
|
||||||
len = llir_emit_conv(proc, len, t_int);
|
|
||||||
|
|
||||||
if (ce->args.count == 3) {
|
|
||||||
cap = llir_build_expr(proc, ce->args.e[2]);
|
|
||||||
cap = llir_emit_conv(proc, cap, t_int);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Type *slice_type = make_type_slice(proc->module->allocator, type_deref(llir_type(ptr)));
|
Type *slice_type = make_type_slice(proc->module->allocator, type_deref(llir_type(ptr)));
|
||||||
llirValue *slice = llir_add_local_generated(proc, slice_type);
|
llirValue *slice = llir_add_local_generated(proc, slice_type);
|
||||||
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 0), ptr);
|
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 0), ptr);
|
||||||
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 1), len);
|
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 1), count);
|
||||||
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 2), cap);
|
|
||||||
return llir_emit_load(proc, slice);
|
return llir_emit_load(proc, slice);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user