Skip to content

Commit 2b06c7b

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 d4081c2 commit 2b06c7b

File tree

6 files changed

+53
-40
lines changed

6 files changed

+53
-40
lines changed

scripts/expected_doxygen_warnings.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ warning: Include graph for 'goto_instrument_parse_options.cpp' not generated, to
2222
warning: Included by graph for 'goto_functions.h' not generated, too many nodes (66), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2323
warning: Included by graph for 'goto_model.h' not generated, too many nodes (111), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2424
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (183), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
25-
warning: Included by graph for 'c_types.h' not generated, too many nodes (143), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
26-
warning: Included by graph for 'config.h' not generated, too many nodes (85), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
25+
warning: Included by graph for 'c_types.h' not generated, too many nodes (144), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
26+
warning: Included by graph for 'config.h' not generated, too many nodes (87), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2727
warning: Included by graph for 'exception_utils.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2828
warning: Included by graph for 'expr.h' not generated, too many nodes (87), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
2929
warning: Included by graph for 'expr_util.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

src/ansi-c/literals/convert_character_literal.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313

1414
#include <util/arith_tools.h>
1515
#include <util/c_types.h>
16+
#include <util/config.h>
1617
#include <util/std_expr.h>
1718

1819
#include "unescape_string.h"
@@ -51,7 +52,7 @@ exprt convert_character_literal(
5152
for(unsigned i=0; i<value.size(); i++)
5253
{
5354
mp_integer z=(unsigned char)(value[i]);
54-
z=z<<((value.size()-i-1)*8);
55+
z = z << ((value.size() - i - 1) * config.ansi_c.char_width);
5556
x+=z;
5657
}
5758

@@ -84,7 +85,7 @@ exprt convert_character_literal(
8485
for(unsigned i=0; i<value.size(); i++)
8586
{
8687
mp_integer z=(unsigned char)(value[i]);
87-
z=z<<((value.size()-i-1)*8);
88+
z = z << ((value.size() - i - 1) * config.ansi_c.char_width);
8889
x+=z;
8990
}
9091

src/goto-instrument/stack_depth.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Date: November 2011
1515

1616
#include <util/arith_tools.h>
1717
#include <util/bitvector_types.h>
18+
#include <util/c_types.h>
1819

1920
#include <goto-programs/goto_model.h>
2021

@@ -23,7 +24,7 @@ Date: November 2011
2324
symbol_exprt add_stack_depth_symbol(symbol_tablet &symbol_table)
2425
{
2526
const irep_idt identifier="$stack_depth";
26-
unsignedbv_typet type(sizeof(std::size_t)*8);
27+
typet type = size_type();
2728

2829
symbolt new_symbol;
2930
new_symbol.name=identifier;

src/util/pointer_offset_size.cpp

+42-34
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ optionalt<mp_integer> member_offset(
3838
{
3939
const std::size_t w = to_c_bit_field_type(comp.type()).get_width();
4040
bit_field_bits += w;
41-
result += bit_field_bits / 8;
42-
bit_field_bits %= 8;
41+
result += bit_field_bits / config.ansi_c.char_width;
42+
bit_field_bits %= config.ansi_c.char_width;
4343
}
4444
else if(comp.type().id() == ID_bool)
4545
{
4646
++bit_field_bits;
47-
result += bit_field_bits / 8;
48-
bit_field_bits %= 8;
47+
result += bit_field_bits / config.ansi_c.char_width;
48+
bit_field_bits %= config.ansi_c.char_width;
4949
}
5050
else
5151
{
@@ -92,7 +92,7 @@ pointer_offset_size(const typet &type, const namespacet &ns)
9292
auto bits = pointer_offset_bits(type, ns);
9393

9494
if(bits.has_value())
95-
return (*bits + 7) / 8;
95+
return (*bits + config.ansi_c.char_width - 1) / config.ansi_c.char_width;
9696
else
9797
return {};
9898
}
@@ -248,16 +248,16 @@ optionalt<exprt> member_offset_expr(
248248
{
249249
std::size_t w = to_c_bit_field_type(c.type()).get_width();
250250
bit_field_bits += w;
251-
const std::size_t bytes = bit_field_bits / 8;
252-
bit_field_bits %= 8;
251+
const std::size_t bytes = bit_field_bits / config.ansi_c.char_width;
252+
bit_field_bits %= config.ansi_c.char_width;
253253
if(bytes > 0)
254254
result = plus_exprt(result, from_integer(bytes, result.type()));
255255
}
256256
else if(c.type().id() == ID_bool)
257257
{
258258
++bit_field_bits;
259-
const std::size_t bytes = bit_field_bits / 8;
260-
bit_field_bits %= 8;
259+
const std::size_t bytes = bit_field_bits / config.ansi_c.char_width;
260+
bit_field_bits %= config.ansi_c.char_width;
261261
if(bytes > 0)
262262
result = plus_exprt(result, from_integer(bytes, result.type()));
263263
}
@@ -288,7 +288,9 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
288288
auto bits = pointer_offset_bits(array_type, ns);
289289

290290
if(bits.has_value())
291-
return from_integer((*bits + 7) / 8, size_type());
291+
return from_integer(
292+
(*bits + config.ansi_c.char_width - 1) / config.ansi_c.char_width,
293+
size_type());
292294
}
293295

294296
auto sub = size_of_expr(array_type.subtype(), ns);
@@ -315,7 +317,9 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
315317
auto bits = pointer_offset_bits(vector_type, ns);
316318

317319
if(bits.has_value())
318-
return from_integer((*bits + 7) / 8, size_type());
320+
return from_integer(
321+
(*bits + config.ansi_c.char_width - 1) / config.ansi_c.char_width,
322+
size_type());
319323
}
320324

321325
auto sub = size_of_expr(vector_type.subtype(), ns);
@@ -354,16 +358,16 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
354358
{
355359
std::size_t w = to_c_bit_field_type(c.type()).get_width();
356360
bit_field_bits += w;
357-
const std::size_t bytes = bit_field_bits / 8;
358-
bit_field_bits %= 8;
361+
const std::size_t bytes = bit_field_bits / config.ansi_c.char_width;
362+
bit_field_bits %= config.ansi_c.char_width;
359363
if(bytes > 0)
360364
result = plus_exprt(result, from_integer(bytes, result.type()));
361365
}
362366
else if(c.type().id() == ID_bool)
363367
{
364368
++bit_field_bits;
365-
const std::size_t bytes = bit_field_bits / 8;
366-
bit_field_bits %= 8;
369+
const std::size_t bytes = bit_field_bits / config.ansi_c.char_width;
370+
bit_field_bits %= config.ansi_c.char_width;
367371
if(bytes > 0)
368372
result = plus_exprt(result, from_integer(bytes, result.type()));
369373
}
@@ -409,7 +413,8 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
409413
}
410414
else
411415
{
412-
mp_integer sub_bytes = (*sub_bits + 7) / 8;
416+
mp_integer sub_bytes =
417+
(*sub_bits + config.ansi_c.char_width - 1) / config.ansi_c.char_width;
413418

414419
if(max_bytes>=0)
415420
{
@@ -443,19 +448,14 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
443448
type.id()==ID_c_bit_field)
444449
{
445450
std::size_t width=to_bitvector_type(type).get_width();
446-
std::size_t bytes=width/8;
447-
if(bytes*8!=width)
451+
std::size_t bytes = width / config.ansi_c.char_width;
452+
if(bytes * config.ansi_c.char_width != width)
448453
bytes++;
449454
return from_integer(bytes, size_type());
450455
}
451456
else if(type.id()==ID_c_enum)
452457
{
453-
std::size_t width =
454-
to_bitvector_type(to_c_enum_type(type).subtype()).get_width();
455-
std::size_t bytes=width/8;
456-
if(bytes*8!=width)
457-
bytes++;
458-
return from_integer(bytes, size_type());
458+
return size_of_expr(to_c_enum_type(type).subtype(), ns);
459459
}
460460
else if(type.id()==ID_c_enum_tag)
461461
{
@@ -469,11 +469,11 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
469469
{
470470
// the following is an MS extension
471471
if(type.get_bool(ID_C_ptr32))
472-
return from_integer(4, size_type());
472+
return from_integer(32 / config.ansi_c.char_width, size_type());
473473

474474
std::size_t width=to_bitvector_type(type).get_width();
475-
std::size_t bytes=width/8;
476-
if(bytes*8!=width)
475+
std::size_t bytes = width / config.ansi_c.char_width;
476+
if(bytes * config.ansi_c.char_width != width)
477477
bytes++;
478478
return from_integer(bytes, size_type());
479479
}
@@ -491,7 +491,7 @@ optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns)
491491
}
492492
else if(type.id()==ID_string)
493493
{
494-
return from_integer(32/8, size_type());
494+
return from_integer(32 / config.ansi_c.char_width, size_type());
495495
}
496496
else
497497
return {};
@@ -645,12 +645,17 @@ optionalt<exprt> get_subexpression_at_offset(
645645
// if this member completely contains the target, and this member is
646646
// byte-aligned, recurse into it
647647
if(
648-
offset_bytes * 8 >= m_offset_bits && m_offset_bits % 8 == 0 &&
649-
offset_bytes * 8 + *target_size_bits <= m_offset_bits + *m_size_bits)
648+
offset_bytes * config.ansi_c.char_width >= m_offset_bits &&
649+
m_offset_bits % config.ansi_c.char_width == 0 &&
650+
offset_bytes * config.ansi_c.char_width + *target_size_bits <=
651+
m_offset_bits + *m_size_bits)
650652
{
651653
const member_exprt member(expr, component.get_name(), component.type());
652654
return get_subexpression_at_offset(
653-
member, offset_bytes - m_offset_bits / 8, target_type_raw, ns);
655+
member,
656+
offset_bytes - m_offset_bits / config.ansi_c.char_width,
657+
target_type_raw,
658+
ns);
654659
}
655660

656661
m_offset_bits += *m_size_bits;
@@ -665,11 +670,14 @@ optionalt<exprt> get_subexpression_at_offset(
665670
// no arrays of non-byte-aligned, zero-, or unknown-sized objects
666671
if(
667672
elem_size_bits.has_value() && *elem_size_bits > 0 &&
668-
*elem_size_bits % 8 == 0 && *target_size_bits <= *elem_size_bits)
673+
*elem_size_bits % config.ansi_c.char_width == 0 &&
674+
*target_size_bits <= *elem_size_bits)
669675
{
670-
const mp_integer elem_size_bytes = *elem_size_bits / 8;
676+
const mp_integer elem_size_bytes =
677+
*elem_size_bits / config.ansi_c.char_width;
671678
const auto offset_inside_elem = offset_bytes % elem_size_bytes;
672-
const auto target_size_bytes = *target_size_bits / 8;
679+
const auto target_size_bytes =
680+
*target_size_bits / config.ansi_c.char_width;
673681
// only recurse if the cell completely contains the target
674682
if(offset_inside_elem + target_size_bytes <= elem_size_bytes)
675683
{

src/util/simplify_expr.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
774774
}
775775

776776
// circular casts through types shorter than `int`
777+
// we use fixed bit widths as this is specifically for the Java bytecode
778+
// front-end
777779
if(op_type == signedbv_typet(32) && expr.op().id() == ID_typecast)
778780
{
779781
if(expr_type==c_bool_typet(8) ||

src/util/simplify_expr_struct.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111
#include "arith_tools.h"
1212
#include "byte_operators.h"
1313
#include "c_types.h"
14+
#include "config.h"
1415
#include "invariant.h"
1516
#include "namespace.h"
1617
#include "pointer_offset_size.h"
@@ -194,7 +195,7 @@ simplify_exprt::simplify_member(const member_exprt &expr)
194195

195196
if(target_size.has_value())
196197
{
197-
mp_integer target_bits = target_size.value() * 8;
198+
mp_integer target_bits = target_size.value() * config.ansi_c.char_width;
198199
const auto bits = expr2bits(op, true, ns);
199200

200201
if(bits.has_value() &&

0 commit comments

Comments
 (0)