Skip to content

Commit 01977ac

Browse files
author
kroening
committed
Zero vs. non-zero integer value of null pointer constant now configurable
git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@3615 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent 351952e commit 01977ac

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/util/config.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ void configt::ansi_ct::set_arch_spec_i386()
248248
arch=ARCH_I386;
249249
endianness=IS_LITTLE_ENDIAN;
250250
char_is_unsigned=false;
251+
NULL_is_zero=true;
251252

252253
switch(mode)
253254
{
@@ -290,6 +291,7 @@ void configt::ansi_ct::set_arch_spec_x86_64()
290291
endianness=IS_LITTLE_ENDIAN;
291292
long_double_width=16*8;
292293
char_is_unsigned=false;
294+
NULL_is_zero=true;
293295

294296
switch(mode)
295297
{
@@ -340,6 +342,7 @@ void configt::ansi_ct::set_arch_spec_power(const irep_idt &subarch)
340342
endianness=IS_BIG_ENDIAN;
341343
long_double_width=16*8;
342344
char_is_unsigned=true;
345+
NULL_is_zero=true;
343346

344347
switch(mode)
345348
{
@@ -392,6 +395,7 @@ void configt::ansi_ct::set_arch_spec_arm(const irep_idt &subarch)
392395
arch=ARCH_ARM;
393396
endianness=IS_LITTLE_ENDIAN;
394397
char_is_unsigned=true;
398+
NULL_is_zero=true;
395399

396400
switch(mode)
397401
{
@@ -432,6 +436,7 @@ void configt::ansi_ct::set_arch_spec_alpha()
432436
endianness=IS_LITTLE_ENDIAN;
433437
long_double_width=16*8;
434438
char_is_unsigned=false;
439+
NULL_is_zero=true;
435440

436441
switch(mode)
437442
{
@@ -473,6 +478,7 @@ void configt::ansi_ct::set_arch_spec_mips(const irep_idt &subarch)
473478
endianness=IS_BIG_ENDIAN;
474479
long_double_width=8*8;
475480
char_is_unsigned=false;
481+
NULL_is_zero=true;
476482

477483
switch(mode)
478484
{
@@ -513,6 +519,7 @@ void configt::ansi_ct::set_arch_spec_s390()
513519
endianness=IS_BIG_ENDIAN;
514520
long_double_width=16*8;
515521
char_is_unsigned=true;
522+
NULL_is_zero=true;
516523

517524
switch(mode)
518525
{
@@ -550,6 +557,7 @@ void configt::ansi_ct::set_arch_spec_s390x()
550557
arch=ARCH_S390X;
551558
endianness=IS_BIG_ENDIAN;
552559
char_is_unsigned=true;
560+
NULL_is_zero=true;
553561

554562
switch(mode)
555563
{
@@ -588,6 +596,7 @@ void configt::ansi_ct::set_arch_spec_sparc()
588596
endianness=IS_BIG_ENDIAN;
589597
long_double_width=16*8;
590598
char_is_unsigned=false;
599+
NULL_is_zero=true;
591600

592601
switch(mode)
593602
{
@@ -626,6 +635,7 @@ void configt::ansi_ct::set_arch_spec_ia64()
626635
long_double_width=16*8;
627636
endianness=IS_LITTLE_ENDIAN;
628637
char_is_unsigned=false;
638+
NULL_is_zero=true;
629639

630640
switch(mode)
631641
{
@@ -667,6 +677,7 @@ void configt::ansi_ct::set_arch_spec_x32()
667677
arch=ARCH_X32;
668678
endianness=IS_LITTLE_ENDIAN;
669679
char_is_unsigned=false;
680+
NULL_is_zero=true;
670681

671682
switch(mode)
672683
{
@@ -713,6 +724,7 @@ bool configt::set(const cmdlinet &cmdline)
713724
ansi_c.os=ansi_ct::NO_OS;
714725
ansi_c.arch=ansi_ct::NO_ARCH;
715726
ansi_c.lib=configt::ansi_ct::LIB_NONE;
727+
ansi_c.NULL_is_zero=(size_t)((void*)0)==0;
716728

717729
// Default is ROUND_TO_EVEN, justified by C99:
718730
// 1 At program startup the floating-point environment is initialized as
@@ -824,6 +836,7 @@ bool configt::set(const cmdlinet &cmdline)
824836
ansi_c.arch=configt::ansi_ct::NO_ARCH;
825837
ansi_c.endianness=configt::ansi_ct::NO_ENDIANNESS;
826838
ansi_c.lib=configt::ansi_ct::LIB_NONE;
839+
ansi_c.NULL_is_zero=false;
827840

828841
if(sizeof(long int)==8)
829842
ansi_c.set_64();

src/util/config.h

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class configt
7777
ARCH_IA64, ARCH_X32 } archt;
7878
archt arch;
7979

80+
// architecture-specific integer value of null pointer constant
81+
bool NULL_is_zero;
82+
8083
void set_arch_spec_i386();
8184
void set_arch_spec_x86_64();
8285
void set_arch_spec_power(const irep_idt &subarch);

src/util/simplify_expr.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,17 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
316316
return false;
317317
}
318318

319-
#if 0
320-
// should be architecture-specific constant
321319
// casts from NULL to any integer
322320
if(op_type.id()==ID_pointer &&
323321
expr.op0().is_constant() &&
324322
to_constant_expr(expr.op0()).get_value()==ID_NULL &&
325-
(expr_type.id()==ID_unsignedbv || expr_type.id()==ID_signedbv))
323+
(expr_type.id()==ID_unsignedbv || expr_type.id()==ID_signedbv) &&
324+
config.ansi_c.NULL_is_zero)
326325
{
327326
exprt tmp=gen_zero(expr_type);
328327
expr.swap(tmp);
329328
return false;
330329
}
331-
#endif
332330

333331
// casts from pointer to integer
334332
// where width of integer >= width of pointer
@@ -790,12 +788,13 @@ static bool is_dereference_integer_object(
790788

791789
if(expr.op0().is_constant())
792790
{
793-
/*if(to_constant_expr(expr.op0()).get_value()==ID_NULL) // NULL
791+
if(to_constant_expr(expr.op0()).get_value()==ID_NULL &&
792+
config.ansi_c.NULL_is_zero) // NULL
794793
{
795-
address=XXX; // architecture-specific constant
794+
address=0;
796795
return true;
797796
}
798-
else*/ if(!to_integer(expr.op0(), address))
797+
else if(!to_integer(expr.op0(), address))
799798
return true;
800799
}
801800
}

0 commit comments

Comments
 (0)