Skip to content

Commit bc81ddf

Browse files
committed
unsigned integers for sizes of things
Closes #62.
1 parent 76f87cd commit bc81ddf

19 files changed

+266
-396
lines changed

doc/langref.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ Function Operation
386386
@shl_with_overflow(inline T: type, a: T, b: T, result: &T) -> bool *x = a << b
387387
```
388388

389-
### @memset(dest, c: u8, byte_count: isize)
389+
### @memset(dest, c: u8, byte_count: usize)
390390

391391
This function sets a region of memory to `c`. `dest` is a pointer.
392392

@@ -398,7 +398,7 @@ level code will not use this function, instead using something like this:
398398
for (dest) |*b| *b = c;
399399
```
400400

401-
### @memcpy(dest, source, byte_count: isize)
401+
### @memcpy(dest, source, byte_count: usize)
402402

403403
This function copies bytes from one region of memory to another. `dest` and
404404
`source` are both pointers and must not overlap.

src/analyze.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ static void slice_type_common_init(CodeGen *g, TypeTableEntry *child_type,
502502
entry->data.structure.fields[0].src_index = 0;
503503
entry->data.structure.fields[0].gen_index = 0;
504504
entry->data.structure.fields[1].name = buf_create_from_str("len");
505-
entry->data.structure.fields[1].type_entry = g->builtin_types.entry_isize;
505+
entry->data.structure.fields[1].type_entry = g->builtin_types.entry_usize;
506506
entry->data.structure.fields[1].src_index = 1;
507507
entry->data.structure.fields[1].gen_index = 1;
508508
}
@@ -546,7 +546,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
546546

547547
if (child_type->zero_bits) {
548548
LLVMTypeRef element_types[] = {
549-
g->builtin_types.entry_isize->type_ref,
549+
g->builtin_types.entry_usize->type_ref,
550550
};
551551
LLVMStructSetBody(entry->type_ref, element_types, 1, false);
552552

@@ -556,9 +556,9 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
556556
entry->data.structure.fields[0].gen_index = -1;
557557
entry->data.structure.fields[1].gen_index = 0;
558558

559-
TypeTableEntry *isize_type = g->builtin_types.entry_isize;
560-
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, isize_type->type_ref);
561-
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, isize_type->type_ref);
559+
TypeTableEntry *usize_type = g->builtin_types.entry_usize;
560+
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref);
561+
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, usize_type->type_ref);
562562
uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
563563

564564
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
@@ -570,7 +570,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
570570
len_debug_size_in_bits,
571571
len_debug_align_in_bits,
572572
len_offset_in_bits,
573-
0, isize_type->di_type),
573+
0, usize_type->di_type),
574574
};
575575
LLVMZigDIType *replacement_di_type = LLVMZigCreateDebugStructType(g->dbuilder,
576576
compile_unit_scope,
@@ -586,7 +586,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
586586
unsigned element_count = 2;
587587
LLVMTypeRef element_types[] = {
588588
pointer_type->type_ref,
589-
g->builtin_types.entry_isize->type_ref,
589+
g->builtin_types.entry_usize->type_ref,
590590
};
591591
LLVMStructSetBody(entry->type_ref, element_types, element_count, false);
592592

@@ -597,9 +597,9 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
597597
uint64_t ptr_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, pointer_type->type_ref);
598598
uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
599599

600-
TypeTableEntry *isize_type = g->builtin_types.entry_isize;
601-
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, isize_type->type_ref);
602-
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, isize_type->type_ref);
600+
TypeTableEntry *usize_type = g->builtin_types.entry_usize;
601+
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref);
602+
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, usize_type->type_ref);
603603
uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1);
604604

605605
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
@@ -617,7 +617,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
617617
len_debug_size_in_bits,
618618
len_debug_align_in_bits,
619619
len_offset_in_bits,
620-
0, isize_type->di_type),
620+
0, usize_type->di_type),
621621
};
622622
LLVMZigDIType *replacement_di_type = LLVMZigCreateDebugStructType(g->dbuilder,
623623
compile_unit_scope,
@@ -2817,10 +2817,10 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import,
28172817
context->fn_entry->struct_val_expr_alloca_list.append(&node->data.slice_expr.resolved_struct_val_expr);
28182818
}
28192819

2820-
analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.start);
2820+
analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.start);
28212821

28222822
if (node->data.slice_expr.end) {
2823-
analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.end);
2823+
analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.end);
28242824
}
28252825

28262826
return return_type;
@@ -2853,7 +2853,7 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i
28532853
return_type = g->builtin_types.entry_invalid;
28542854
}
28552855

2856-
analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.array_access_expr.subscript);
2856+
analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.array_access_expr.subscript);
28572857

28582858
return return_type;
28592859
}
@@ -3912,7 +3912,7 @@ static TypeTableEntry *analyze_array_type(CodeGen *g, ImportTableEntry *import,
39123912

39133913
if (size_node) {
39143914
TypeTableEntry *size_type = analyze_expression(g, import, context,
3915-
g->builtin_types.entry_isize, size_node);
3915+
g->builtin_types.entry_usize, size_node);
39163916
if (size_type->id == TypeTableEntryIdInvalid) {
39173917
return g->builtin_types.entry_invalid;
39183918
}
@@ -4042,10 +4042,10 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
40424042
Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
40434043
index_var_node->block_context = child_context;
40444044
node->data.for_expr.index_var = add_local_var(g, index_var_node, import, child_context, index_var_name,
4045-
g->builtin_types.entry_isize, true, nullptr);
4045+
g->builtin_types.entry_usize, true, nullptr);
40464046
} else {
40474047
node->data.for_expr.index_var = add_local_var(g, node, import, child_context, nullptr,
4048-
g->builtin_types.entry_isize, true, nullptr);
4048+
g->builtin_types.entry_usize, true, nullptr);
40494049
}
40504050

40514051
AstNode *for_body_node = node->data.for_expr.body;

src/codegen.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ static LLVMValueRef gen_err_name(CodeGen *g, AstNode *node) {
413413
}
414414

415415
LLVMValueRef indices[] = {
416-
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
416+
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
417417
err_val,
418418
};
419419
return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
@@ -937,7 +937,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
937937

938938
int len_index = wanted_type->data.structure.fields[1].gen_index;
939939
LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_expr->tmp_ptr, len_index, "");
940-
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
940+
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
941941
actual_type->data.array.len, false);
942942
LLVMBuildStore(g->builder, len_val, len_ptr);
943943

@@ -978,13 +978,13 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
978978

979979
LLVMValueRef new_len;
980980
if (dest_size == 1) {
981-
LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, src_size, false);
981+
LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, src_size, false);
982982
new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");
983983
} else if (src_size == 1) {
984-
LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, dest_size, false);
984+
LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, dest_size, false);
985985
if (want_debug_safety(g, node)) {
986986
LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");
987-
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_isize->type_ref);
987+
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
988988
LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
989989
LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SliceWidenOk");
990990
LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SliceWidenFail");
@@ -1159,12 +1159,12 @@ static LLVMValueRef gen_array_elem_ptr(CodeGen *g, AstNode *source_node, LLVMVal
11591159

11601160
if (array_type->id == TypeTableEntryIdArray) {
11611161
if (want_debug_safety(g, source_node)) {
1162-
LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
1162+
LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
11631163
array_type->data.array.len, false);
11641164
add_bounds_check(g, source_node, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
11651165
}
11661166
LLVMValueRef indices[] = {
1167-
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
1167+
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
11681168
subscript_value
11691169
};
11701170
set_debug_source_node(g, source_node);
@@ -1268,13 +1268,13 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
12681268
if (node->data.slice_expr.end) {
12691269
end_val = gen_expr(g, node->data.slice_expr.end);
12701270
} else {
1271-
end_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, array_type->data.array.len, false);
1271+
end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);
12721272
}
12731273

12741274
if (want_debug_safety(g, node)) {
12751275
add_bounds_check(g, node, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
12761276
if (node->data.slice_expr.end) {
1277-
LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
1277+
LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
12781278
array_type->data.array.len, false);
12791279
add_bounds_check(g, node, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);
12801280
}
@@ -1283,7 +1283,7 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
12831283
set_debug_source_node(g, node);
12841284
LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");
12851285
LLVMValueRef indices[] = {
1286-
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
1286+
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
12871287
start_val,
12881288
};
12891289
LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
@@ -1408,7 +1408,7 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva
14081408
if (struct_type->id == TypeTableEntryIdArray) {
14091409
Buf *name = &node->data.field_access_expr.field_name;
14101410
assert(buf_eql_str(name, "len"));
1411-
return LLVMConstInt(g->builtin_types.entry_isize->type_ref,
1411+
return LLVMConstInt(g->builtin_types.entry_usize->type_ref,
14121412
struct_type->data.array.len, false);
14131413
} else if (struct_type->id == TypeTableEntryIdStruct || (struct_type->id == TypeTableEntryIdPointer &&
14141414
struct_type->data.pointer.child_type->id == TypeTableEntryIdStruct))
@@ -2043,7 +2043,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, AstNode *source_node, LLVMValu
20432043
LLVMValueRef src_ptr = LLVMBuildBitCast(g->builder, src, ptr_u8, "");
20442044
LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, dest, ptr_u8, "");
20452045

2046-
TypeTableEntry *isize = g->builtin_types.entry_isize;
2046+
TypeTableEntry *usize = g->builtin_types.entry_usize;
20472047
uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);
20482048
uint64_t align_bytes = get_memcpy_align(g, type_entry);
20492049
assert(size_bytes > 0);
@@ -2052,7 +2052,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, AstNode *source_node, LLVMValu
20522052
LLVMValueRef params[] = {
20532053
dest_ptr, // dest pointer
20542054
src_ptr, // source pointer
2055-
LLVMConstInt(isize->type_ref, size_bytes, false),
2055+
LLVMConstInt(usize->type_ref, size_bytes, false),
20562056
LLVMConstInt(LLVMInt32Type(), align_bytes, false),
20572057
LLVMConstNull(LLVMInt1Type()), // is volatile
20582058
};
@@ -2871,8 +2871,8 @@ static LLVMValueRef gen_container_init_expr(CodeGen *g, AstNode *node) {
28712871
LLVMValueRef elem_val = gen_expr(g, field_node);
28722872

28732873
LLVMValueRef indices[] = {
2874-
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
2875-
LLVMConstInt(g->builtin_types.entry_isize->type_ref, i, false),
2874+
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
2875+
LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
28762876
};
28772877
set_debug_source_node(g, field_node);
28782878
LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
@@ -2989,7 +2989,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
29892989
VariableTableEntry *index_var = node->data.for_expr.index_var;
29902990
assert(index_var);
29912991
LLVMValueRef index_ptr = index_var->value_ref;
2992-
LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_isize->type_ref, 1, false);
2992+
LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
29932993

29942994
LLVMBasicBlockRef cond_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForCond");
29952995
LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
@@ -3005,7 +3005,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
30053005
LLVMValueRef len_val;
30063006
TypeTableEntry *child_type;
30073007
if (array_type->id == TypeTableEntryIdArray) {
3008-
len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
3008+
len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
30093009
array_type->data.array.len, false);
30103010
child_type = array_type->data.array.child_type;
30113011
} else if (array_type->id == TypeTableEntryIdStruct) {
@@ -3154,7 +3154,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
31543154
}
31553155
}
31563156
if (!ignore_uninit && want_debug_safety(g, source_node)) {
3157-
TypeTableEntry *isize = g->builtin_types.entry_isize;
3157+
TypeTableEntry *usize = g->builtin_types.entry_usize;
31583158
uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, variable->type->type_ref);
31593159
uint64_t align_bytes = get_memcpy_align(g, variable->type);
31603160

@@ -3163,7 +3163,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
31633163
LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
31643164
LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
31653165
LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, variable->value_ref, ptr_u8, "");
3166-
LLVMValueRef byte_count = LLVMConstInt(isize->type_ref, size_bytes, false);
3166+
LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);
31673167
LLVMValueRef align_in_bytes = LLVMConstInt(LLVMInt32Type(), align_bytes, false);
31683168
LLVMValueRef params[] = {
31693169
dest_ptr,
@@ -3771,7 +3771,7 @@ static LLVMValueRef gen_test_fn_val(CodeGen *g, FnTableEntry *fn_entry) {
37713771
LLVMSetGlobalConstant(str_global_val, true);
37723772
LLVMSetUnnamedAddr(str_global_val, true);
37733773

3774-
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, buf_len(fn_name), false);
3774+
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(fn_name), false);
37753775

37763776
LLVMTypeRef ptr_type = LLVMPointerType(g->builtin_types.entry_u8->type_ref, 0);
37773777
LLVMValueRef name_fields[] = {
@@ -3813,7 +3813,7 @@ static void generate_error_name_table(CodeGen *g) {
38133813

38143814
LLVMValueRef fields[] = {
38153815
LLVMConstBitCast(str_global, u8_ptr_type->type_ref),
3816-
LLVMConstInt(g->builtin_types.entry_isize->type_ref, buf_len(name), false),
3816+
LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),
38173817
};
38183818
values[i] = LLVMConstNamedStruct(str_type->type_ref, fields, 2);
38193819
}
@@ -3986,7 +3986,7 @@ static void do_code_gen(CodeGen *g) {
39863986
LLVMSetGlobalConstant(test_fn_array_val, true);
39873987
LLVMSetUnnamedAddr(test_fn_array_val, true);
39883988

3989-
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, g->test_fn_count, false);
3989+
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, g->test_fn_count, false);
39903990
LLVMTypeRef ptr_type = LLVMPointerType(LLVMTypeOf(test_fn_vals[0]), 0);
39913991
LLVMValueRef fields[] = {
39923992
LLVMConstBitCast(test_fn_array_val, ptr_type),
@@ -4611,7 +4611,7 @@ static void define_builtin_fns(CodeGen *g) {
46114611
builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
46124612
builtin_fn->param_types[0] = nullptr; // manually checked later
46134613
builtin_fn->param_types[1] = nullptr; // manually checked later
4614-
builtin_fn->param_types[2] = g->builtin_types.entry_isize;
4614+
builtin_fn->param_types[2] = g->builtin_types.entry_usize;
46154615
builtin_fn->ref_count = 1;
46164616

46174617
LLVMTypeRef param_types[] = {
@@ -4635,7 +4635,7 @@ static void define_builtin_fns(CodeGen *g) {
46354635
builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
46364636
builtin_fn->param_types[0] = nullptr; // manually checked later
46374637
builtin_fn->param_types[1] = g->builtin_types.entry_u8;
4638-
builtin_fn->param_types[2] = g->builtin_types.entry_isize;
4638+
builtin_fn->param_types[2] = g->builtin_types.entry_usize;
46394639
builtin_fn->ref_count = 1;
46404640

46414641
LLVMTypeRef param_types[] = {

0 commit comments

Comments
 (0)