Add Pointer Arithmetic

This commit is contained in:
Ginger Bill
2016-10-12 17:51:36 +01:00
parent f5318c46d1
commit f3209584a3
12 changed files with 414 additions and 173 deletions
+23 -18
View File
@@ -126,8 +126,8 @@ enum BuiltinProcId {
BuiltinProc_swizzle,
BuiltinProc_ptr_offset,
BuiltinProc_ptr_sub,
// BuiltinProc_ptr_offset,
// BuiltinProc_ptr_sub,
BuiltinProc_slice_ptr,
BuiltinProc_min,
@@ -170,8 +170,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT("swizzle"), 1, true, Expr_Expr},
{STR_LIT("ptr_offset"), 2, false, Expr_Expr},
{STR_LIT("ptr_sub"), 2, false, Expr_Expr},
// {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("min"), 2, false, Expr_Expr},
@@ -757,6 +757,10 @@ void add_type_info_type(Checker *c, Type *t) {
}
} break;
case Type_Maybe:
add_type_info_type(c, bt->Maybe.elem);
break;
case Type_Pointer:
add_type_info_type(c, bt->Pointer.elem);
break;
@@ -905,25 +909,26 @@ void init_preload_types(Checker *c) {
t_type_info_member = record->other_fields[0]->type;
t_type_info_member_ptr = make_type_pointer(c->allocator, t_type_info_member);
if (record->field_count != 17) {
if (record->field_count != 18) {
compiler_error("Invalid `Type_Info` layout");
}
t_type_info_named = record->fields[ 1]->type;
t_type_info_integer = record->fields[ 2]->type;
t_type_info_float = record->fields[ 3]->type;
t_type_info_string = record->fields[ 4]->type;
t_type_info_boolean = record->fields[ 5]->type;
t_type_info_pointer = record->fields[ 6]->type;
t_type_info_maybe = record->fields[ 7]->type;
t_type_info_procedure = record->fields[ 8]->type;
t_type_info_array = record->fields[ 9]->type;
t_type_info_slice = record->fields[10]->type;
t_type_info_vector = record->fields[11]->type;
t_type_info_tuple = record->fields[12]->type;
t_type_info_struct = record->fields[13]->type;
t_type_info_union = record->fields[14]->type;
t_type_info_raw_union = record->fields[15]->type;
t_type_info_enum = record->fields[16]->type;
t_type_info_any = record->fields[ 4]->type;
t_type_info_string = record->fields[ 5]->type;
t_type_info_boolean = record->fields[ 6]->type;
t_type_info_pointer = record->fields[ 7]->type;
t_type_info_maybe = record->fields[ 8]->type;
t_type_info_procedure = record->fields[ 9]->type;
t_type_info_array = record->fields[10]->type;
t_type_info_slice = record->fields[11]->type;
t_type_info_vector = record->fields[12]->type;
t_type_info_tuple = record->fields[13]->type;
t_type_info_struct = record->fields[14]->type;
t_type_info_union = record->fields[15]->type;
t_type_info_raw_union = record->fields[16]->type;
t_type_info_enum = record->fields[17]->type;
}
if (t_allocator == NULL) {
+94 -7
View File
@@ -555,6 +555,8 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecke
struct_type->Record.fields = reordered_fields;
}
type_set_offsets(c->sizes, c->allocator, struct_type);
}
void check_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker *cycle_checker) {
@@ -1232,13 +1234,27 @@ b32 check_binary_op(Checker *c, Operand *o, Token op) {
// TODO(bill): Handle errors correctly
Type *type = base_type(base_vector_type(o->type));
switch (op.kind) {
case Token_Add:
case Token_Sub:
case Token_SubEq:
if (!is_type_numeric(type) && !is_type_pointer(type)) {
error(op, "Operator `%.*s` is only allowed with numeric or pointer expressions", LIT(op.string));
return false;
}
if (is_type_pointer(type)) {
o->type = t_int;
}
if (base_type(type) == t_rawptr) {
gbString str = type_to_string(type);
defer (gb_string_free(str));
error(ast_node_token(o->expr), "Invalid pointer type for pointer arithmetic: `%s`", str);
return false;
}
break;
case Token_Add:
case Token_Mul:
case Token_Quo:
case Token_AddEq:
case Token_SubEq:
case Token_MulEq:
case Token_QuoEq:
if (!is_type_numeric(type)) {
@@ -1732,6 +1748,44 @@ String check_down_cast_name(Type *dst_, Type *src_) {
return result;
}
Operand check_ptr_addition(Checker *c, TokenKind op, Operand *ptr, Operand *offset, AstNode *node) {
GB_ASSERT(node->kind == AstNode_BinaryExpr);
ast_node(be, BinaryExpr, node);
GB_ASSERT(is_type_pointer(ptr->type));
GB_ASSERT(is_type_integer(offset->type));
GB_ASSERT(op == Token_Add || op == Token_Sub);
Operand operand = {};
operand.mode = Addressing_Value;
operand.type = ptr->type;
operand.expr = node;
if (base_type(ptr->type) == t_rawptr) {
gbString str = type_to_string(ptr->type);
defer (gb_string_free(str));
error(ast_node_token(node), "Invalid pointer type for pointer arithmetic: `%s`", str);
operand.mode = Addressing_Invalid;
return operand;
}
if (ptr->mode == Addressing_Constant && offset->mode == Addressing_Constant) {
i64 elem_size = type_size_of(c->sizes, c->allocator, ptr->type);
i64 ptr_val = ptr->value.value_pointer;
i64 offset_val = exact_value_to_integer(offset->value).value_integer;
i64 new_ptr_val = ptr_val;
if (op == Token_Add) {
new_ptr_val += elem_size*offset_val;
} else {
new_ptr_val -= elem_size*offset_val;
}
operand.mode = Addressing_Constant;
operand.value = make_exact_value_pointer(new_ptr_val);
}
return operand;
}
void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
PROF_PROC();
@@ -1904,14 +1958,35 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
Token op = be->op;
if (token_is_shift(op)) {
check_shift(c, x, y, node);
return;
}
if (op.kind == Token_Add || op.kind == Token_Sub) {
if (is_type_pointer(x->type) && is_type_integer(y->type)) {
*x = check_ptr_addition(c, op.kind, x, y, node);
return;
} else if (is_type_integer(x->type) && is_type_pointer(y->type)) {
if (op.kind == Token_Sub) {
gbString lhs = expr_to_string(x->expr);
gbString rhs = expr_to_string(y->expr);
defer (gb_string_free(lhs));
defer (gb_string_free(rhs));
error(ast_node_token(node), "Invalid pointer arithmetic, did you mean `%s %.*s %s`?", rhs, LIT(op.string), lhs);
x->mode = Addressing_Invalid;
return;
}
*x = check_ptr_addition(c, op.kind, y, x, node);
return;
}
}
convert_to_typed(c, x, y->type);
if (x->mode == Addressing_Invalid) return;
if (x->mode == Addressing_Invalid) {
return;
}
convert_to_typed(c, y, x->type);
if (y->mode == Addressing_Invalid) {
x->mode = Addressing_Invalid;
@@ -1952,12 +2027,14 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
b32 fail = false;
switch (y->value.kind) {
case ExactValue_Integer:
if (y->value.value_integer == 0)
if (y->value.value_integer == 0) {
fail = true;
}
break;
case ExactValue_Float:
if (y->value.value_float == 0.0)
if (y->value.value_float == 0.0) {
fail = true;
}
break;
}
@@ -1975,6 +2052,14 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
ExactValue b = y->value;
Type *type = base_type(x->type);
if (is_type_pointer(type)) {
GB_ASSERT(op.kind == Token_Sub);
i64 bytes = a.value_pointer - b.value_pointer;
i64 diff = bytes/type_size_of(c->sizes, c->allocator, type);
x->value = make_exact_value_pointer(diff);
return;
}
if (type->kind != Type_Basic) {
gbString xt = type_to_string(x->type);
defer (gb_string_free(xt));
@@ -2788,6 +2873,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
operand->mode = Addressing_Value;
} break;
#if 0
case BuiltinProc_ptr_offset: {
// ptr_offset :: proc(ptr: ^T, offset: int) -> ^T
// ^T cannot be rawptr
@@ -2890,6 +2976,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
operand->mode = Addressing_Value;
}
} break;
#endif
case BuiltinProc_slice_ptr: {
// slice_ptr :: proc(a: ^T, len: int[, cap: int]) -> []T
+4 -2
View File
@@ -1007,14 +1007,16 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
Operand operand = {Addressing_Invalid};
AstNode binary_expr = {AstNode_BinaryExpr};
ast_node(be, BinaryExpr, &binary_expr);
be->op = op;
be->op = op;
be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add));
// NOTE(bill): Only use the first one will be used
be->left = as->lhs[0];
be->right = as->rhs[0];
check_binary_expr(c, &operand, &binary_expr);
if (operand.mode == Addressing_Invalid)
if (operand.mode == Addressing_Invalid) {
return;
}
// NOTE(bill): Only use the first one will be used
check_assignment_variable(c, &operand, as->lhs[0]);
} break;
+25 -21
View File
@@ -367,6 +367,7 @@ gb_global Type *t_type_info_member_ptr = NULL;
gb_global Type *t_type_info_named = NULL;
gb_global Type *t_type_info_integer = NULL;
gb_global Type *t_type_info_float = NULL;
gb_global Type *t_type_info_any = NULL;
gb_global Type *t_type_info_string = NULL;
gb_global Type *t_type_info_boolean = NULL;
gb_global Type *t_type_info_pointer = NULL;
@@ -380,7 +381,6 @@ gb_global Type *t_type_info_struct = NULL;
gb_global Type *t_type_info_union = NULL;
gb_global Type *t_type_info_raw_union = NULL;
gb_global Type *t_type_info_enum = NULL;
gb_global Type *t_type_info_any = NULL;
gb_global Type *t_allocator = NULL;
gb_global Type *t_allocator_ptr = NULL;
@@ -990,8 +990,11 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t);
i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, i64 index);
i64 align_formula(i64 size, i64 align) {
i64 result = size + align-1;
return result - result%align;
if (align > 0) {
i64 result = size + align-1;
return result - result%align;
}
return size;
}
i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
@@ -1025,16 +1028,7 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
case Type_Record: {
switch (t->Record.kind) {
case TypeRecord_Struct:
if (!t->Record.struct_is_packed) {
i64 max = 1;
for (isize i = 0; i < t->Record.field_count; i++) {
i64 align = type_align_of(s, allocator, t->Record.fields[i]->type);
if (max < align) {
max = align;
}
}
return max;
} else if (t->Record.field_count > 0) {
if (t->Record.field_count > 0) {
return type_align_of(s, allocator, t->Record.fields[0]->type);
}
break;
@@ -1141,17 +1135,19 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
i64 total_size_in_bits = bit_size * count;
i64 total_size = (total_size_in_bits+7)/8;
return total_size;
// i64 alignment = align_formula(size, align);
// return alignment*(count-1) + size;
} break;
case Type_Slice: // ptr + len + cap
return 3 * s.word_size;
case Type_Maybe: // value + bool
return type_size_of(s, allocator, t->Maybe.elem) + type_size_of(s, allocator, t_bool);
case Type_Maybe: { // value + bool
Type *elem = t->Maybe.elem;
i64 align = type_align_of(s, allocator, elem);
i64 size = align_formula(type_size_of(s, allocator, elem), align);
size += type_size_of(s, allocator, t_bool);
return align_formula(size, align);
}
case Type_Record: {
switch (t->Record.kind) {
@@ -1161,7 +1157,10 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
return 0;
}
type_set_offsets(s, allocator, t);
return t->Record.struct_offsets[count-1] + type_size_of(s, allocator, t->Record.fields[count-1]->type);
// TODO(bill): Is this how it should work?
i64 size = t->Record.struct_offsets[count-1] + type_size_of(s, allocator, t->Record.fields[count-1]->type);
i64 align = type_align_of(s, allocator, t);
return align_formula(size, align);
} break;
case TypeRecord_Union: {
@@ -1174,7 +1173,10 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
max = size;
}
// NOTE(bill): Align to int
return align_formula(max, s.word_size) + type_size_of(s, allocator, t_int);
i64 align = type_align_of(s, allocator, t);
isize size = align_formula(max, s.word_size);
size += type_size_of(s, allocator, t_int);
return align_formula(size, align);
} break;
case TypeRecord_RawUnion: {
@@ -1185,7 +1187,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
if (max < size)
max = size;
}
return max;
// TODO(bill): Is this how it should work?
i64 align = type_align_of(s, allocator, t);
return align_formula(max, align);
} break;
case TypeRecord_Enum: {