Type* iter_type = fornode->iter->type;
b32 can_iterate = 0;
if (types_are_compatible(iter_type, builtin_range_type_type)) {
+ if (fornode->by_pointer) {
+ onyx_report_error(fornode->var->token->pos, "Cannot iterate by pointer over a range.");
+ return 1;
+ }
+
can_iterate = 1;
// NOTE: Blindly copy the first range member's type which will
else if (iter_type->kind == Type_Kind_Slice) {
can_iterate = 1;
- fornode->var->type = iter_type->Slice.ptr_to_data->Pointer.elem;
+ if (fornode->by_pointer) fornode->var->type = iter_type->Slice.ptr_to_data;
+ else fornode->var->type = iter_type->Slice.ptr_to_data->Pointer.elem;
+
fornode->loop_type = For_Loop_Slice;
}
else if (iter_type->kind == Type_Kind_DynArray) {
can_iterate = 1;
- fornode->var->type = iter_type->DynArray.ptr_to_data->Pointer.elem;
+ if (fornode->by_pointer) fornode->var->type = iter_type->DynArray.ptr_to_data;
+ else fornode->var->type = iter_type->DynArray.ptr_to_data->Pointer.elem;
+
fornode->loop_type = For_Loop_DynArr;
}
// start_ptr
//
- u64 end_ptr_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
- u64 ptr_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
+ u64 end_ptr_local, ptr_local;
+ end_ptr_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
+
+ if (for_node->by_pointer) {
+ ptr_local = iter_local;
+ } else {
+ ptr_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
+ }
AstLocal* var = for_node->var;
- u64 elem_size = type_size_of(var->type);
b32 it_is_local = (b32) ((iter_local & LOCAL_IS_WASM) != 0);
u64 offset = 0;
+ u64 elem_size;
+ if (for_node->by_pointer) elem_size = type_size_of(var->type->Pointer.elem);
+ else elem_size = type_size_of(var->type);
+
WIL(WI_LOCAL_SET, end_ptr_local);
WIL(WI_LOCAL_TEE, ptr_local);
WIL(WI_LOCAL_GET, end_ptr_local);
WI(WI_I32_GE_S);
WID(WI_COND_JUMP, 0x02);
- // NOTE: Storing structs requires that the location to store it is,
- // the top most thing on the stack. Everything requires it to be
- // 'under' the other element being stored. -brendanfh 2020/09/04
- if (!it_is_local && var->type->kind != Type_Kind_Struct) {
- emit_local_location(mod, &code, var, &offset);
- }
+ if (!for_node->by_pointer) {
+ // NOTE: Storing structs requires that the location to store it is,
+ // the top most thing on the stack. Everything requires it to be
+ // 'under' the other element being stored. -brendanfh 2020/09/04
+ if (!it_is_local && var->type->kind != Type_Kind_Struct) {
+ emit_local_location(mod, &code, var, &offset);
+ }
- WIL(WI_LOCAL_GET, ptr_local);
- emit_load_instruction(mod, &code, var->type, 0);
- if (it_is_local) {
- WIL(WI_LOCAL_SET, iter_local);
- } else {
- if (var->type->kind != Type_Kind_Struct) {
- emit_store_instruction(mod, &code, var->type, offset);
+ WIL(WI_LOCAL_GET, ptr_local);
+ emit_load_instruction(mod, &code, var->type, 0);
+ if (it_is_local) {
+ WIL(WI_LOCAL_SET, iter_local);
} else {
- emit_local_location(mod, &code, var, &offset);
- emit_store_instruction(mod, &code, var->type, offset);
+ if (var->type->kind != Type_Kind_Struct) {
+ emit_store_instruction(mod, &code, var->type, offset);
+ } else {
+ emit_local_location(mod, &code, var, &offset);
+ emit_store_instruction(mod, &code, var->type, offset);
+ }
}
}