add integer literal to pointer explicit cast

closes #227
This commit is contained in:
Andrew Kelley 2017-01-31 13:38:04 -05:00
parent eb00aa21f5
commit b258fdb532
2 changed files with 35 additions and 0 deletions

View File

@ -6424,6 +6424,27 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc
return result;
}
static IrInstruction *ir_analyze_int_lit_to_ptr(IrAnalyze *ira, IrInstruction *source_instr,
IrInstruction *target, TypeTableEntry *wanted_type)
{
assert(wanted_type->id == TypeTableEntryIdPointer);
ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
if (!val)
return ira->codegen->invalid_instruction;
TypeTableEntry *usize_type = ira->codegen->builtin_types.entry_usize;
if (!ir_num_lit_fits_in_other_type(ira, target, usize_type))
return ira->codegen->invalid_instruction;
IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
source_instr->source_node, wanted_type, val->depends_on_compile_var);
result->value.data.x_ptr.base_ptr = nullptr;
result->value.data.x_ptr.index = bignum_to_twos_complement(&val->data.x_bignum);
result->value.data.x_ptr.special = ConstPtrSpecialRuntime;
return result;
}
static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *source_instr,
IrInstruction *target, TypeTableEntry *wanted_type)
{
@ -6491,6 +6512,13 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
return ir_analyze_int_to_ptr(ira, source_instr, value, wanted_type);
}
// explicit cast from number literal to pointer
if (wanted_type_canon->id == TypeTableEntryIdPointer &&
(actual_type_canon->id == TypeTableEntryIdNumLitInt))
{
return ir_analyze_int_lit_to_ptr(ira, source_instr, value, wanted_type);
}
// explicit widening or shortening cast
if ((wanted_type_canon->id == TypeTableEntryIdInt &&
actual_type_canon->id == TypeTableEntryIdInt) ||

View File

@ -8,3 +8,10 @@ fn intToPtrCast() {
const z = usize(y);
assert(z == 13);
}
fn numLitIntToPtrCast() {
@setFnTest(this);
const vga_mem = (&u16)(0xB8000);
assert(usize(vga_mem) == 0xB8000);
}