authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-08 17:38:25-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-08 17:38:25-04:00
log54675b060ae6139f60e111521b9a2688f66977a0
treed0e91290608737fbdeca7e9504bd640bc34a2a7a
parent9ca798bc75bddac4d3db05463f1a42cfd7906bd3

add ptrToInt builtin, remove usize(ptr) cast

closes #415

9 files changed, 110 insertions(+), 80 deletions(-)

src/all_types.hpp+1
......@@ -1226,6 +1226,7 @@ enum BuiltinFnId {
12261226 BuiltinFnIdPtrCast,
12271227 BuiltinFnIdBitCast,
12281228 BuiltinFnIdIntToPtr,
1229 BuiltinFnIdPtrToInt,
12291230 BuiltinFnIdEnumTagName,
12301231 BuiltinFnIdFieldParentPtr,
12311232 BuiltinFnIdOffsetOf,
src/codegen.cpp+1
......@@ -4545,6 +4545,7 @@ static void define_builtin_fns(CodeGen *g) {
45454545 create_builtin_fn(g, BuiltinFnIdPtrCast, "ptrCast", 2);
45464546 create_builtin_fn(g, BuiltinFnIdBitCast, "bitCast", 2);
45474547 create_builtin_fn(g, BuiltinFnIdIntToPtr, "intToPtr", 2);
4548 create_builtin_fn(g, BuiltinFnIdPtrToInt, "ptrToInt", 1);
45484549 create_builtin_fn(g, BuiltinFnIdEnumTagName, "enumTagName", 1);
45494550 create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3);
45504551 create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);
src/ir.cpp+52-31
......@@ -4372,6 +4372,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43724372
43734373 return ir_build_int_to_ptr(irb, scope, node, arg0_value, arg1_value);
43744374 }
4375 case BuiltinFnIdPtrToInt:
4376 {
4377 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4378 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4379 if (arg0_value == irb->codegen->invalid_instruction)
4380 return arg0_value;
4381
4382 return ir_build_ptr_to_int(irb, scope, node, arg0_value);
4383 }
43754384 case BuiltinFnIdEnumTagName:
43764385 {
43774386 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -7336,30 +7345,6 @@ static IrInstruction *ir_analyze_widen_or_shorten(IrAnalyze *ira, IrInstruction
73367345 return result;
73377346}
73387347
7339static IrInstruction *ir_analyze_ptr_to_int(IrAnalyze *ira, IrInstruction *source_instr,
7340 IrInstruction *target, TypeTableEntry *wanted_type)
7341{
7342 assert(wanted_type->id == TypeTableEntryIdInt);
7343
7344 if (instr_is_comptime(target)) {
7345 ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
7346 if (!val)
7347 return ira->codegen->invalid_instruction;
7348 if (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
7349 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
7350 source_instr->source_node, wanted_type);
7351 bigint_init_unsigned(&result->value.data.x_bigint, val->data.x_ptr.data.hard_coded_addr.addr);
7352 return result;
7353 }
7354 }
7355
7356 IrInstruction *result = ir_build_ptr_to_int(&ira->new_irb, source_instr->scope,
7357 source_instr->source_node, target);
7358 result->value.type = wanted_type;
7359 return result;
7360}
7361
7362
73637348static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *source_instr,
73647349 IrInstruction *target, TypeTableEntry *wanted_type)
73657350{
......@@ -7500,7 +7485,6 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
75007485 TypeTableEntry *wanted_type, IrInstruction *value)
75017486{
75027487 TypeTableEntry *actual_type = value->value.type;
7503 TypeTableEntry *usize_type = ira->codegen->builtin_types.entry_usize;
75047488
75057489 if (type_is_invalid(wanted_type) || type_is_invalid(actual_type)) {
75067490 return ira->codegen->invalid_instruction;
......@@ -7521,11 +7505,6 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
75217505 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false);
75227506 }
75237507
7524 // explicit cast from pointer to usize
7525 if (wanted_type == usize_type && type_is_codegen_pointer(actual_type)) {
7526 return ir_analyze_ptr_to_int(ira, source_instr, value, wanted_type);
7527 }
7528
75297508 // explicit widening or shortening cast
75307509 if ((wanted_type->id == TypeTableEntryIdInt &&
75317510 actual_type->id == TypeTableEntryIdInt) ||
......@@ -13937,6 +13916,47 @@ static TypeTableEntry *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
1393713916 zig_unreachable();
1393813917}
1393913918
13919static TypeTableEntry *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstructionPtrToInt *instruction) {
13920 IrInstruction *target = instruction->target->other;
13921 if (type_is_invalid(target->value.type))
13922 return ira->codegen->builtin_types.entry_invalid;
13923
13924 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
13925
13926 if (!(target->value.type->id == TypeTableEntryIdPointer ||
13927 target->value.type->id == TypeTableEntryIdFn ||
13928 (target->value.type->id == TypeTableEntryIdMaybe &&
13929 (target->value.type->data.maybe.child_type->id == TypeTableEntryIdPointer ||
13930 target->value.type->data.maybe.child_type->id == TypeTableEntryIdFn))))
13931 {
13932 ir_add_error(ira, target,
13933 buf_sprintf("expected pointer, found '%s'", buf_ptr(&target->value.type->name)));
13934 return ira->codegen->builtin_types.entry_invalid;
13935 }
13936
13937 if (instr_is_comptime(target)) {
13938 ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
13939 if (!val)
13940 return ira->codegen->builtin_types.entry_invalid;
13941 if (target->value.type->id == TypeTableEntryIdMaybe) {
13942 val = val->data.x_maybe;
13943 }
13944 if (val->type->id == TypeTableEntryIdPointer && val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
13945 IrInstruction *result = ir_create_const(&ira->new_irb, instruction->base.scope,
13946 instruction->base.source_node, usize);
13947 bigint_init_unsigned(&result->value.data.x_bigint, val->data.x_ptr.data.hard_coded_addr.addr);
13948 ir_link_new_instruction(result, &instruction->base);
13949 return usize;
13950 }
13951 }
13952
13953 IrInstruction *result = ir_build_ptr_to_int(&ira->new_irb, instruction->base.scope,
13954 instruction->base.source_node, target);
13955 result->value.type = usize;
13956 ir_link_new_instruction(result, &instruction->base);
13957 return usize;
13958}
13959
1394013960static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
1394113961 switch (instruction->id) {
1394213962 case IrInstructionIdInvalid:
......@@ -13948,7 +13968,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1394813968 case IrInstructionIdStructFieldPtr:
1394913969 case IrInstructionIdEnumFieldPtr:
1395013970 case IrInstructionIdInitEnum:
13951 case IrInstructionIdPtrToInt:
1395213971 zig_unreachable();
1395313972 case IrInstructionIdReturn:
1395413973 return ir_analyze_instruction_return(ira, (IrInstructionReturn *)instruction);
......@@ -14106,6 +14125,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1410614125 return ir_analyze_instruction_bit_cast(ira, (IrInstructionBitCast *)instruction);
1410714126 case IrInstructionIdIntToPtr:
1410814127 return ir_analyze_instruction_int_to_ptr(ira, (IrInstructionIntToPtr *)instruction);
14128 case IrInstructionIdPtrToInt:
14129 return ir_analyze_instruction_ptr_to_int(ira, (IrInstructionPtrToInt *)instruction);
1410914130 case IrInstructionIdEnumTagName:
1411014131 return ir_analyze_instruction_enum_tag_name(ira, (IrInstructionEnumTagName *)instruction);
1411114132 case IrInstructionIdFieldParentPtr:
std/debug.zig+1-1
......@@ -83,7 +83,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
8383
8484 var ignored_count: usize = 0;
8585
86 var fp = usize(@frameAddress());
86 var fp = @ptrToInt(@frameAddress());
8787 while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) {
8888 if (ignored_count < ignore_frame_count) {
8989 ignored_count += 1;
std/os/linux.zig+39-39
......@@ -337,11 +337,11 @@ pub fn dup2(old: i32, new: i32) -> usize {
337337}
338338
339339pub fn chdir(path: &const u8) -> usize {
340 arch.syscall1(arch.SYS_chdir, usize(path))
340 arch.syscall1(arch.SYS_chdir, @ptrToInt(path))
341341}
342342
343343pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) -> usize {
344 arch.syscall3(arch.SYS_execve, usize(path), usize(argv), usize(envp))
344 arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp))
345345}
346346
347347pub fn fork() -> usize {
......@@ -349,50 +349,50 @@ pub fn fork() -> usize {
349349}
350350
351351pub fn getcwd(buf: &u8, size: usize) -> usize {
352 arch.syscall2(arch.SYS_getcwd, usize(buf), size)
352 arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size)
353353}
354354
355355pub fn getdents(fd: i32, dirp: &u8, count: usize) -> usize {
356 arch.syscall3(arch.SYS_getdents, usize(fd), usize(dirp), usize(count))
356 arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), usize(count))
357357}
358358
359359pub fn isatty(fd: i32) -> bool {
360360 var wsz: winsize = undefined;
361 return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, usize(&wsz)) == 0;
361 return arch.syscall3(arch.SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
362362}
363363
364364pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) -> usize {
365 arch.syscall3(arch.SYS_readlink, usize(path), usize(buf_ptr), buf_len)
365 arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len)
366366}
367367
368368pub fn mkdir(path: &const u8, mode: usize) -> usize {
369 arch.syscall2(arch.SYS_mkdir, usize(path), mode)
369 arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode)
370370}
371371
372372pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: usize)
373373 -> usize
374374{
375 arch.syscall6(arch.SYS_mmap, usize(address), length, prot, flags, usize(fd), offset)
375 arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), offset)
376376}
377377
378378pub fn munmap(address: &u8, length: usize) -> usize {
379 arch.syscall2(arch.SYS_munmap, usize(address), length)
379 arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length)
380380}
381381
382382pub fn read(fd: i32, buf: &u8, count: usize) -> usize {
383 arch.syscall3(arch.SYS_read, usize(fd), usize(buf), count)
383 arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count)
384384}
385385
386386pub fn rmdir(path: &const u8) -> usize {
387 arch.syscall1(arch.SYS_rmdir, usize(path))
387 arch.syscall1(arch.SYS_rmdir, @ptrToInt(path))
388388}
389389
390390pub fn symlink(existing: &const u8, new: &const u8) -> usize {
391 arch.syscall2(arch.SYS_symlink, usize(existing), usize(new))
391 arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new))
392392}
393393
394394pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) -> usize {
395 arch.syscall4(arch.SYS_pread, usize(fd), usize(buf), count, offset)
395 arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset)
396396}
397397
398398pub fn pipe(fd: &[2]i32) -> usize {
......@@ -400,31 +400,31 @@ pub fn pipe(fd: &[2]i32) -> usize {
400400}
401401
402402pub fn pipe2(fd: &[2]i32, flags: usize) -> usize {
403 arch.syscall2(arch.SYS_pipe2, usize(fd), flags)
403 arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags)
404404}
405405
406406pub fn write(fd: i32, buf: &const u8, count: usize) -> usize {
407 arch.syscall3(arch.SYS_write, usize(fd), usize(buf), count)
407 arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count)
408408}
409409
410410pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) -> usize {
411 arch.syscall4(arch.SYS_pwrite, usize(fd), usize(buf), count, offset)
411 arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset)
412412}
413413
414414pub fn rename(old: &const u8, new: &const u8) -> usize {
415 arch.syscall2(arch.SYS_rename, usize(old), usize(new))
415 arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new))
416416}
417417
418418pub fn open(path: &const u8, flags: usize, perm: usize) -> usize {
419 arch.syscall3(arch.SYS_open, usize(path), flags, perm)
419 arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm)
420420}
421421
422422pub fn create(path: &const u8, perm: usize) -> usize {
423 arch.syscall2(arch.SYS_creat, usize(path), perm)
423 arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm)
424424}
425425
426426pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) -> usize {
427 arch.syscall4(arch.SYS_openat, usize(dirfd), usize(path), flags, mode)
427 arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode)
428428}
429429
430430pub fn close(fd: i32) -> usize {
......@@ -441,7 +441,7 @@ pub fn exit(status: i32) -> noreturn {
441441}
442442
443443pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize {
444 arch.syscall3(arch.SYS_getrandom, usize(buf), count, usize(flags))
444 arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags))
445445}
446446
447447pub fn kill(pid: i32, sig: i32) -> usize {
......@@ -449,11 +449,11 @@ pub fn kill(pid: i32, sig: i32) -> usize {
449449}
450450
451451pub fn unlink(path: &const u8) -> usize {
452 arch.syscall1(arch.SYS_unlink, usize(path))
452 arch.syscall1(arch.SYS_unlink, @ptrToInt(path))
453453}
454454
455455pub fn waitpid(pid: i32, status: &i32, options: i32) -> usize {
456 arch.syscall4(arch.SYS_wait4, usize(pid), usize(status), usize(options), 0)
456 arch.syscall4(arch.SYS_wait4, usize(pid), @ptrToInt(status), usize(options), 0)
457457}
458458
459459const NSIG = 65;
......@@ -471,15 +471,15 @@ pub fn raise(sig: i32) -> i32 {
471471}
472472
473473fn blockAllSignals(set: &sigset_t) {
474 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8);
474 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8);
475475}
476476
477477fn blockAppSignals(set: &sigset_t) {
478 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8);
478 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8);
479479}
480480
481481fn restoreSignals(set: &sigset_t) {
482 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8);
482 _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8);
483483}
484484
485485
......@@ -537,11 +537,11 @@ pub const iovec = extern struct {
537537//
538538
539539pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize {
540 arch.syscall3(arch.SYS_getsockname, usize(fd), usize(addr), usize(len))
540 arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len))
541541}
542542
543543pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize {
544 arch.syscall3(arch.SYS_getpeername, usize(fd), usize(addr), usize(len))
544 arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len))
545545}
546546
547547pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize {
......@@ -549,29 +549,29 @@ pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize {
549549}
550550
551551pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> usize {
552 arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen))
552 arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen))
553553}
554554
555555pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> usize {
556 arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen))
556 arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen))
557557}
558558
559559pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) -> usize {
560 arch.syscall3(arch.SYS_sendmsg, usize(fd), usize(msg), flags)
560 arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags)
561561}
562562
563563pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize {
564 arch.syscall3(arch.SYS_connect, usize(fd), usize(addr), usize(len))
564 arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len))
565565}
566566
567567pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) -> usize {
568 arch.syscall3(arch.SYS_recvmsg, usize(fd), usize(msg), flags)
568 arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags)
569569}
570570
571571pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32,
572572 noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> usize
573573{
574 arch.syscall6(arch.SYS_recvfrom, usize(fd), usize(buf), len, flags, usize(addr), usize(alen))
574 arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen))
575575}
576576
577577pub fn shutdown(fd: i32, how: i32) -> usize {
......@@ -579,7 +579,7 @@ pub fn shutdown(fd: i32, how: i32) -> usize {
579579}
580580
581581pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize {
582 arch.syscall3(arch.SYS_bind, usize(fd), usize(addr), usize(len))
582 arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len))
583583}
584584
585585pub fn listen(fd: i32, backlog: i32) -> usize {
......@@ -587,11 +587,11 @@ pub fn listen(fd: i32, backlog: i32) -> usize {
587587}
588588
589589pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) -> usize {
590 arch.syscall6(arch.SYS_sendto, usize(fd), usize(buf), len, flags, usize(addr), usize(alen))
590 arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen))
591591}
592592
593593pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> usize {
594 arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), usize(&fd[0]))
594 arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0]))
595595}
596596
597597pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize {
......@@ -599,7 +599,7 @@ pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usiz
599599}
600600
601601pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) -> usize {
602 arch.syscall4(arch.SYS_accept4, usize(fd), usize(addr), usize(len), flags)
602 arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags)
603603}
604604
605605// error NameTooLong;
......@@ -634,5 +634,5 @@ pub const stat = arch.stat;
634634pub const timespec = arch.timespec;
635635
636636pub fn fstat(fd: i32, stat_buf: &stat) -> usize {
637 arch.syscall2(arch.SYS_fstat, usize(fd), usize(stat_buf))
637 arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf))
638638}
test/cases/cast.zig+2-2
......@@ -4,13 +4,13 @@ const mem = @import("std").mem;
44test "int to ptr cast" {
55 const x = usize(13);
66 const y = @intToPtr(&u8, x);
7 const z = usize(y);
7 const z = @ptrToInt(y);
88 assert(z == 13);
99}
1010
1111test "integer literal to pointer cast" {
1212 const vga_mem = @intToPtr(&u16, 0xB8000);
13 assert(usize(vga_mem) == 0xB8000);
13 assert(@ptrToInt(vga_mem) == 0xB8000);
1414}
1515
1616test "pointer reinterpret const float to int" {
test/cases/sizeof_and_typeof.zig+4-4
......@@ -28,7 +28,7 @@ test "offsetOf" {
2828
2929 // Non-packed struct fields can be moved/padded
3030 const a: A = undefined;
31 assert(usize(&a.a) - usize(&a) == @offsetOf(A, "a"));
32 assert(usize(&a.b) - usize(&a) == @offsetOf(@typeOf(a), "b"));
33 assert(usize(&a.c) - usize(&a) == @offsetOf(@typeOf(a), "c"));
34}
\ No newline at end of file
31 assert(@ptrToInt(&a.a) - @ptrToInt(&a) == @offsetOf(A, "a"));
32 assert(@ptrToInt(&a.b) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "b"));
33 assert(@ptrToInt(&a.c) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "c"));
34}
test/cases/slice.zig+2-2
......@@ -3,9 +3,9 @@ const assert = @import("std").debug.assert;
33const x = @intToPtr(&i32, 0x1000)[0..0x500];
44const y = x[0x100..];
55test "compile time slice of pointer to hard coded address" {
6 assert(usize(x.ptr) == 0x1000);
6 assert(@ptrToInt(x.ptr) == 0x1000);
77 assert(x.len == 0x500);
88
9 assert(usize(y.ptr) == 0x1100);
9 assert(@ptrToInt(y.ptr) == 0x1100);
1010 assert(y.len == 0x400);
1111}
test/compile_errors.zig+8-1
......@@ -1769,7 +1769,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
17691769
17701770 cases.add("save reference to inline function",
17711771 \\export fn foo() {
1772 \\ quux(usize(bar));
1772 \\ quux(@ptrToInt(bar));
17731773 \\}
17741774 \\inline fn bar() { }
17751775 \\extern fn quux(usize);
......@@ -1952,4 +1952,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
19521952 \\}
19531953 ,
19541954 ".tmp_source.zig:2:9: error: fractional component prevents float value 12.340000 from being casted to type 'i32'");
1955
1956 cases.add("non pointer given to @ptrToInt",
1957 \\export fn entry(x: i32) -> usize {
1958 \\ @ptrToInt(x)
1959 \\}
1960 ,
1961 ".tmp_source.zig:2:15: error: expected pointer, found 'i32'");
19551962}