Skip to content

Commit

Permalink
Removed capacity arguments from new_slice and slice_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Jan 3, 2017
1 parent cff1b3d commit b76f6a8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/checker/checker.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT(""), 0, false, Expr_Stmt},

{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_val"), 1, false, Expr_Expr},
Expand All @@ -181,7 +181,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {

// {STR_LIT("ptr_offset"), 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("max"), 2, false, Expr_Expr},
Expand Down
48 changes: 2 additions & 46 deletions src/checker/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
operand->type = make_type_pointer(c->allocator, type);
} break;
case BuiltinProc_new_slice: {
// new_slice :: proc(Type, len: int[, cap: int]) -> []Type
// new_slice :: proc(Type, len: int) -> []Type
Operand op = {0};
check_expr_or_type(c, &op, ce->args.e[0]);
Type *type = op.type;
Expand All @@ -2653,10 +2653,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
}

AstNode *len = ce->args.e[1];
AstNode *cap = NULL;
if (ce->args.count > 2) {
cap = ce->args.e[2];
}

check_expr(c, &op, len);
if (op.mode == Addressing_Invalid) {
Expand All @@ -2669,23 +2665,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
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->type = make_type_slice(c->allocator, type);
} break;
Expand Down Expand Up @@ -3166,7 +3145,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
#endif

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
Type *ptr_type = base_type(operand->type);
if (!is_type_pointer(ptr_type)) {
Expand All @@ -3185,10 +3164,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
}

AstNode *len = ce->args.e[1];
AstNode *cap = NULL;
if (ce->args.count > 2) {
cap = ce->args.e[2];
}

Operand op = {0};
check_expr(c, &op, len);
Expand All @@ -3203,25 +3178,6 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
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->mode = Addressing_Value;
} break;
Expand Down
29 changes: 7 additions & 22 deletions src/llir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_align = llir_make_const_int(allocator, a);

llirValue *len = 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);
}
llirValue *count = llir_emit_conv(proc, llir_build_expr(proc, ce->args.e[1]), 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);
args[0] = slice_size;
Expand All @@ -2880,10 +2876,8 @@ llirValue *llir_build_single_expr(llirProcedure *proc, AstNode *expr, TypeAndVal

llirValue *gep0 = llir_emit_struct_ep(proc, slice, 0);
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, gep1, len);
llir_emit_store(proc, gep2, cap);
llir_emit_store(proc, gep1, count);
return llir_emit_load(proc, slice);
} break;

Expand Down Expand Up @@ -3059,22 +3053,13 @@ llirValue *llir_build_single_expr(llirProcedure *proc, AstNode *expr, TypeAndVal
case BuiltinProc_slice_ptr: {
llir_emit_comment(proc, str_lit("slice_ptr"));
llirValue *ptr = llir_build_expr(proc, ce->args.e[0]);
llirValue *len = llir_build_expr(proc, ce->args.e[1]);
llirValue *cap = len;

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);
}

llirValue *count = llir_build_expr(proc, ce->args.e[1]);
count = llir_emit_conv(proc, count, t_int);

Type *slice_type = make_type_slice(proc->module->allocator, type_deref(llir_type(ptr)));
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, 1), len);
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 2), cap);
llir_emit_store(proc, llir_emit_struct_ep(proc, slice, 1), count);
return llir_emit_load(proc, slice);
} break;

Expand Down

0 comments on commit b76f6a8

Please sign in to comment.