Skip to content

Commit dd4210c

Browse files
committed
Do not hardcode char/bytes having 8 bits
The C standard does not guarantee that char is exactly 8 bits, and there are DSPs (such Texas Instruments C55x) that do not have 8-bit-bytes. Use the configuration value instead.
1 parent 3b64b94 commit dd4210c

16 files changed

+156
-93
lines changed

src/analyses/goto_rw.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Date: April 2010
1111
#include <limits>
1212
#include <algorithm>
1313

14+
#include <util/config.h>
1415
#include <util/std_code.h>
1516
#include <util/std_expr.h>
1617
#include <util/pointer_offset_size.h>
@@ -235,7 +236,7 @@ void rw_range_sett::get_objects_byte_extract(
235236
get_objects_rec(mode, be.op(), -1, size);
236237
else
237238
{
238-
index*=8;
239+
index*=config.ansi_c.char_width;
239240
if(index>=pointer_offset_bits(be.op().type(), ns))
240241
return;
241242

src/analyses/invariant_set.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ void invariant_sett::add_type_bounds(const exprt &expr, const typet &type)
556556
{
557557
unsigned op_width=to_unsignedbv_type(type).get_width();
558558

559+
// TODO - 8 appears to be a magic number here
559560
if(op_width<=8)
560561
{
561562
unsigned a;

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ bool ansi_c_entry_point(
299299
}
300300

301301
{
302+
// TODO I have no idea what MAX refers to, or how the
303+
// int_width-4 relates to it
302304
// assume argc is at most MAX/8-1
303305
mp_integer upper_bound=
304306
power(2, config.ansi_c.int_width-4);

src/ansi-c/padding.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Function: alignment
2929

3030
mp_integer alignment(const typet &type, const namespacet &ns)
3131
{
32+
const unsigned bits=config.ansi_c.char_width;
33+
3234
// we need to consider a number of different cases:
3335
// - alignment specified in the source, which will be recorded in
3436
// ID_C_alignment
@@ -85,7 +87,7 @@ mp_integer alignment(const typet &type, const namespacet &ns)
8587
type.id()==ID_c_bool)
8688
{
8789
std::size_t width=to_bitvector_type(type).get_width();
88-
result=width%8?width/8+1:width/8;
90+
result=width%bits?width/bits+1:width/bits;
8991
}
9092
else if(type.id()==ID_c_enum)
9193
result=alignment(type.subtype(), ns);
@@ -94,7 +96,7 @@ mp_integer alignment(const typet &type, const namespacet &ns)
9496
else if(type.id()==ID_pointer)
9597
{
9698
std::size_t width=config.ansi_c.pointer_width;
97-
result=width%8?width/8+1:width/8;
99+
result=width%bits?width/bits+1:width/bits;
98100
}
99101
else if(type.id()==ID_symbol)
100102
result=alignment(ns.follow(type), ns);
@@ -152,9 +154,10 @@ void add_padding(struct_typet &type, const namespacet &ns)
152154
else if(bit_field_bits!=0)
153155
{
154156
// not on a byte-boundary?
155-
if((bit_field_bits%8)!=0)
157+
if((bit_field_bits%config.ansi_c.char_width)!=0)
156158
{
157-
std::size_t pad=8-bit_field_bits%8;
159+
std::size_t pad=
160+
config.ansi_c.char_width-bit_field_bits%config.ansi_c.char_width;
158161
c_bit_field_typet padding_type(unsignedbv_typet(pad), pad);
159162

160163
struct_typet::componentt component;
@@ -174,9 +177,10 @@ void add_padding(struct_typet &type, const namespacet &ns)
174177
}
175178

176179
// Add padding at the end?
177-
if((bit_field_bits%8)!=0)
180+
if((bit_field_bits%config.ansi_c.char_width)!=0)
178181
{
179-
std::size_t pad=8-bit_field_bits%8;
182+
std::size_t pad=
183+
config.ansi_c.char_width-bit_field_bits%config.ansi_c.char_width;
180184
c_bit_field_typet padding_type(unsignedbv_typet(pad), pad);
181185

182186
struct_typet::componentt component;
@@ -225,8 +229,12 @@ void add_padding(struct_typet &type, const namespacet &ns)
225229
max_alignment=a;
226230

227231
std::size_t w=to_c_bit_field_type(it_type).get_width();
228-
std::size_t bytes;
229-
for(bytes=0; w>bit_field_bits; ++bytes, bit_field_bits+=8) {}
232+
std::size_t bytes=0;
233+
while(w>bit_field_bits)
234+
{
235+
++bytes;
236+
bit_field_bits+=config.ansi_c.char_width;
237+
}
230238
bit_field_bits-=w;
231239
offset+=bytes;
232240
continue;
@@ -252,7 +260,7 @@ void add_padding(struct_typet &type, const namespacet &ns)
252260
mp_integer pad=a-displacement;
253261

254262
unsignedbv_typet padding_type;
255-
padding_type.set_width(integer2unsigned(pad*8));
263+
padding_type.set_width(integer2unsigned(pad*config.ansi_c.char_width));
256264

257265
struct_typet::componentt component;
258266
component.type()=padding_type;
@@ -274,8 +282,8 @@ void add_padding(struct_typet &type, const namespacet &ns)
274282

275283
if(bit_field_bits!=0)
276284
{
277-
// these are now assumed to be multiples of 8
278-
offset+=bit_field_bits/8;
285+
// these are now assumed to be multiples of bytes
286+
offset+=bit_field_bits/config.ansi_c.char_width;
279287
}
280288

281289
// any explicit alignment for the struct?
@@ -309,7 +317,7 @@ void add_padding(struct_typet &type, const namespacet &ns)
309317
mp_integer pad=max_alignment-displacement;
310318

311319
unsignedbv_typet padding_type;
312-
padding_type.set_width(integer2unsigned(pad*8));
320+
padding_type.set_width(integer2unsigned(pad*config.ansi_c.char_width));
313321

314322
// we insert after any final 'flexible member'
315323
struct_typet::componentt component;
@@ -336,16 +344,16 @@ Function: add_padding
336344

337345
void add_padding(union_typet &type, const namespacet &ns)
338346
{
339-
mp_integer max_alignment=alignment(type, ns)*8;
347+
mp_integer max_alignment=alignment(type, ns)*config.ansi_c.char_width;
340348
mp_integer size_bits=pointer_offset_bits(type, ns);
341349

342350
union_typet::componentst &components=type.components();
343351

344352
// Is the union packed?
345353
if(type.get_bool(ID_C_packed))
346354
{
347-
// The size needs to be a multiple of 8 only.
348-
max_alignment=8;
355+
// The size needs to be a multiple of bytes only.
356+
max_alignment=config.ansi_c.char_width;
349357
}
350358

351359
// The size must be a multiple of the alignment, or

src/goto-instrument/stack_depth.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Date: November 2011
1414
#include <util/arith_tools.h>
1515
#include <util/cprover_prefix.h>
1616

17+
#include <ansi-c/c_types.h>
18+
1719
#include <goto-programs/goto_functions.h>
1820

1921
#include "stack_depth.h"
@@ -33,7 +35,7 @@ Function: add_stack_depth_symbol
3335
symbol_exprt add_stack_depth_symbol(symbol_tablet &symbol_table)
3436
{
3537
const irep_idt identifier="$stack_depth";
36-
signedbv_typet type(sizeof(int)*8);
38+
typet type=size_type();
3739

3840
symbolt new_symbol;
3941
new_symbol.name=identifier;

src/goto-programs/vcd_goto_trace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ std::string as_vcd_binary(
8282
type.id()==ID_bv)
8383
width=string2integer(type.get_string(ID_width));
8484
else
85-
width=pointer_offset_size(type, ns)*8;
85+
width=pointer_offset_bits(type, ns);
8686

8787
if(width>=0)
8888
return std::string(integer2size_t(width), 'x');

src/solvers/flattening/boolbv.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, kroening@kroening.com
1010
#include <map>
1111
#include <set>
1212

13+
#include <util/config.h>
1314
#include <util/symbol.h>
1415
#include <util/mp_arith.h>
1516
#include <util/arith_tools.h>
@@ -235,7 +236,10 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
235236

236237
if(expr.type().id()==ID_unsignedbv ||
237238
expr.type().id()==ID_signedbv)
238-
return bv_utils.build_constant(op_width/8, result_width);
239+
return
240+
bv_utils.build_constant(
241+
op_width/config.ansi_c.char_width,
242+
result_width);
239243
}
240244
else if(expr.id()==ID_case)
241245
return convert_case(expr);

src/solvers/flattening/boolbv_byte_extract.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, kroening@kroening.com
99
#include <cassert>
1010

1111
#include <util/arith_tools.h>
12+
#include <util/config.h>
1213
#include <util/std_expr.h>
1314
#include <util/byte_operators.h>
1415
#include <util/endianness_map.h>
@@ -76,11 +77,12 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
7677
#if 0
7778
if(expr.id()==ID_byte_extract_big_endian &&
7879
expr.type().id()==ID_c_bit_field &&
79-
(width%8)!=0)
80+
(width%config.ansi_c.char_width)!=0)
8081
{
8182
byte_extract_exprt tmp=expr;
8283
// round up
83-
to_c_bit_field_type(tmp.type()).set_width(width+8-width%8);
84+
to_c_bit_field_type(tmp.type()).set_width(
85+
width+config.ansi_c.char_width-width%config.ansi_c.char_width);
8486
convert_byte_extract(tmp, bv);
8587
bv.resize(width); // chop down
8688
return;
@@ -116,7 +118,7 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
116118
bv.resize(width);
117119

118120
// see if the byte number is constant
119-
unsigned byte_width=8;
121+
unsigned byte_width=config.ansi_c.char_width;
120122

121123
mp_integer index;
122124
if(!to_integer(offset, index))

src/solvers/flattening/boolbv_byte_update.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, kroening@kroening.com
1111

1212
#include <util/arith_tools.h>
1313
#include <util/byte_operators.h>
14+
#include <util/config.h>
1415
#include <util/endianness_map.h>
1516

1617
#include "boolbv.h"
@@ -49,7 +50,7 @@ bvt boolbvt::convert_byte_update(const byte_update_exprt &expr)
4950

5051
const bvt &value_bv=convert_bv(value);
5152
std::size_t update_width=value_bv.size();
52-
std::size_t byte_width=8;
53+
std::size_t byte_width=config.ansi_c.char_width;
5354

5455
if(update_width>bv.size())
5556
update_width=bv.size();
@@ -60,7 +61,7 @@ bvt boolbvt::convert_byte_update(const byte_update_exprt &expr)
6061
if(!to_integer(offset_expr, index))
6162
{
6263
// yes!
63-
mp_integer offset=index*8;
64+
mp_integer offset=index*byte_width;
6465

6566
if(offset+update_width>mp_integer(bv.size()) || offset<0)
6667
{

src/solvers/flattening/flatten_byte_operators.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, kroening@kroening.com
66
77
\*******************************************************************/
88

9+
#include <util/config.h>
910
#include <util/expr.h>
1011
#include <util/std_types.h>
1112
#include <util/std_expr.h>
@@ -66,7 +67,7 @@ exprt flatten_byte_extract(
6667
const typet &offset_type=ns.follow(offset.type());
6768

6869
// byte-array?
69-
if(element_width==8)
70+
if(element_width==config.ansi_c.char_width)
7071
{
7172
// get 'width'-many bytes, and concatenate
7273
std::size_t width_bytes=integer2unsigned(num_elements);
@@ -84,7 +85,8 @@ exprt flatten_byte_extract(
8485
op.push_back(index_expr);
8586
}
8687

87-
// TODO this doesn't seem correct if size_bits%8!=0 as more
88+
// TODO this doesn't seem correct if
89+
// size_bits%config.ansi_c.char_width!=0 as more
8890
// bits than the original expression will be returned.
8991
if(width_bytes==1)
9092
return op.front();
@@ -107,9 +109,11 @@ exprt flatten_byte_extract(
107109
concatenation_exprt concat(
108110
unsignedbv_typet(integer2unsigned(element_width*num_elements)));
109111

110-
assert(element_width%8==0);
112+
assert(element_width%config.ansi_c.char_width==0);
111113
exprt first_index=
112-
div_exprt(offset, from_integer(element_width/8, offset_type));
114+
div_exprt(
115+
offset,
116+
from_integer(element_width/config.ansi_c.char_width, offset_type));
113117

114118
// byte extract will do the appropriate mapping, thus MSB comes
115119
// last here (as opposed to the above, where no further byte
@@ -125,8 +129,9 @@ exprt flatten_byte_extract(
125129
}
126130

127131
// the new offset is offset%width
128-
mod_exprt new_offset(offset,
129-
from_integer(element_width/8, offset_type));
132+
mod_exprt new_offset(
133+
offset,
134+
from_integer(element_width/config.ansi_c.char_width, offset_type));
130135

131136
// build new byte-extract expression
132137
byte_extract_exprt tmp(src);
@@ -154,11 +159,14 @@ exprt flatten_byte_extract(
154159
adjusted_offset=offset;
155160
else
156161
{
157-
exprt width_constant=from_integer(op0_bits/8-1, offset_type);
162+
exprt width_constant=
163+
from_integer(op0_bits/config.ansi_c.char_width-1, offset_type);
158164
adjusted_offset=minus_exprt(width_constant, offset);
159165
}
160166

161-
mult_exprt times_eight(adjusted_offset, from_integer(8, offset_type));
167+
mult_exprt times_eight(
168+
adjusted_offset,
169+
from_integer(config.ansi_c.char_width, offset_type));
162170

163171
// cast to generic bit-vector
164172
std::size_t op0_width=integer2unsigned(op0_bits);
@@ -378,31 +386,37 @@ exprt flatten_byte_update(
378386
t.id()==ID_pointer)
379387
{
380388
// do a shift, mask and OR
381-
std::size_t width=integer2size_t(pointer_offset_size(t, ns)*8);
389+
std::size_t width=integer2size_t(pointer_offset_bits(t, ns));
382390

383391
assert(width!=0);
384392

385-
if(element_size*8>width)
393+
if(element_size*config.ansi_c.char_width>width)
386394
throw "flatten_byte_update to update element that is too large";
387395

388396
// build mask
389397
exprt mask=
390-
from_integer(power(2, element_size*8)-1, unsignedbv_typet(width));
398+
from_integer(
399+
power(2, element_size*config.ansi_c.char_width)-1,
400+
unsignedbv_typet(width));
391401

392402
// zero-extend the value, but only if needed
393403
exprt value_extended;
394404

395-
if(width>integer2unsigned(element_size)*8)
405+
if(width>integer2unsigned(element_size)*config.ansi_c.char_width)
396406
value_extended=
397407
concatenation_exprt(
398408
from_integer(
399-
0, unsignedbv_typet(width-integer2unsigned(element_size)*8)),
409+
0,
410+
unsignedbv_typet(
411+
width-integer2unsigned(element_size)*config.ansi_c.char_width)),
400412
src.op2(), t);
401413
else
402414
value_extended=src.op2();
403415

404416
const typet &offset_type=ns.follow(src.op1().type());
405-
mult_exprt offset_times_eight(src.op1(), from_integer(8, offset_type));
417+
mult_exprt offset_times_eight(
418+
src.op1(),
419+
from_integer(config.ansi_c.char_width, offset_type));
406420

407421
binary_predicate_exprt offset_ge_zero(
408422
offset_times_eight,

0 commit comments

Comments
 (0)