| ... | @@ -2412,6 +2412,25 @@ static void ir_render(CodeGen *g, FnTableEntry *fn_entry) { | ... | @@ -2412,6 +2412,25 @@ static void ir_render(CodeGen *g, FnTableEntry *fn_entry) { |
| 2412 | } | 2412 | } |
| 2413 | } | 2413 | } |
| 2414 | | 2414 | |
| | 2415 | static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index) { |
| | 2416 | ConstExprValue *parent_array = array_const_val->data.x_array.parent_array; |
| | 2417 | LLVMValueRef base_ptr; |
| | 2418 | if (parent_array) { |
| | 2419 | size_t parent_array_index = array_const_val->data.x_array.parent_array_index; |
| | 2420 | base_ptr = gen_const_ptr_array_recursive(g, parent_array, parent_array_index); |
| | 2421 | } else { |
| | 2422 | render_const_val(g, array_const_val); |
| | 2423 | render_const_val_global(g, array_const_val); |
| | 2424 | base_ptr = array_const_val->llvm_global; |
| | 2425 | } |
| | 2426 | TypeTableEntry *usize = g->builtin_types.entry_usize; |
| | 2427 | LLVMValueRef indices[] = { |
| | 2428 | LLVMConstNull(usize->type_ref), |
| | 2429 | LLVMConstInt(usize->type_ref, index, false), |
| | 2430 | }; |
| | 2431 | return LLVMConstInBoundsGEP(base_ptr, indices, 2); |
| | 2432 | } |
| | 2433 | |
| 2415 | static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { | 2434 | static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { |
| 2416 | TypeTableEntry *canon_type = get_underlying_type(const_val->type); | 2435 | TypeTableEntry *canon_type = get_underlying_type(const_val->type); |
| 2417 | assert(!canon_type->zero_bits); | 2436 | assert(!canon_type->zero_bits); |
| ... | @@ -2554,7 +2573,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { | ... | @@ -2554,7 +2573,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { |
| 2554 | render_const_val_global(g, const_val); | 2573 | render_const_val_global(g, const_val); |
| 2555 | size_t index = const_val->data.x_ptr.index; | 2574 | size_t index = const_val->data.x_ptr.index; |
| 2556 | ConstExprValue *base_ptr = const_val->data.x_ptr.base_ptr; | 2575 | ConstExprValue *base_ptr = const_val->data.x_ptr.base_ptr; |
| 2557 | TypeTableEntry *usize = g->builtin_types.entry_usize; | | |
| 2558 | if (base_ptr) { | 2576 | if (base_ptr) { |
| 2559 | if (index == SIZE_MAX) { | 2577 | if (index == SIZE_MAX) { |
| 2560 | render_const_val(g, base_ptr); | 2578 | render_const_val(g, base_ptr); |
| ... | @@ -2568,25 +2586,20 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { | ... | @@ -2568,25 +2586,20 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { |
| 2568 | assert(array_const_val->type->id == TypeTableEntryIdArray); | 2586 | assert(array_const_val->type->id == TypeTableEntryIdArray); |
| 2569 | if (array_const_val->type->zero_bits) { | 2587 | if (array_const_val->type->zero_bits) { |
| 2570 | // make this a null pointer | 2588 | // make this a null pointer |
| 2571 | TypeTableEntry *usize_type = g->builtin_types.entry_usize; | 2589 | TypeTableEntry *usize = g->builtin_types.entry_usize; |
| 2572 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize_type->type_ref), | 2590 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref), |
| 2573 | const_val->type->type_ref); | 2591 | const_val->type->type_ref); |
| 2574 | render_const_val_global(g, const_val); | 2592 | render_const_val_global(g, const_val); |
| 2575 | return const_val->llvm_value; | 2593 | return const_val->llvm_value; |
| 2576 | } | 2594 | } |
| 2577 | render_const_val(g, array_const_val); | 2595 | LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, index); |
| 2578 | render_const_val_global(g, array_const_val); | | |
| 2579 | LLVMValueRef indices[] = { | | |
| 2580 | LLVMConstNull(usize->type_ref), | | |
| 2581 | LLVMConstInt(usize->type_ref, index, false), | | |
| 2582 | }; | | |
| 2583 | LLVMValueRef uncasted_ptr_val = LLVMConstInBoundsGEP(array_const_val->llvm_global, indices, 2); | | |
| 2584 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref); | 2596 | LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref); |
| 2585 | const_val->llvm_value = ptr_val; | 2597 | const_val->llvm_value = ptr_val; |
| 2586 | render_const_val_global(g, const_val); | 2598 | render_const_val_global(g, const_val); |
| 2587 | return ptr_val; | 2599 | return ptr_val; |
| 2588 | } | 2600 | } |
| 2589 | } else { | 2601 | } else { |
| | 2602 | TypeTableEntry *usize = g->builtin_types.entry_usize; |
| 2590 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, index, false), const_val->type->type_ref); | 2603 | const_val->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, index, false), const_val->type->type_ref); |
| 2591 | render_const_val_global(g, const_val); | 2604 | render_const_val_global(g, const_val); |
| 2592 | return const_val->llvm_value; | 2605 | return const_val->llvm_value; |