Skip to content

Commit 661e095

Browse files
committed
Stop using unsigned and uint64_t in interpreter
1 parent b860c26 commit 661e095

File tree

3 files changed

+74
-72
lines changed

3 files changed

+74
-72
lines changed

src/goto-programs/interpreter.cpp

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <fstream>
1919
#include <algorithm>
2020
#include <string.h>
21+
#include <type_traits>
2122

2223
#include <util/invariant.h>
2324
#include <util/std_types.h>
@@ -393,7 +394,7 @@ void interpretert::execute_other()
393394
mp_vectort tmp, rhs;
394395
evaluate(pc->code.op1(), tmp);
395396
mp_integer address=evaluate_address(pc->code.op0());
396-
auto size=get_size(pc->code.op0().type());
397+
const auto size=get_size(pc->code.op0().type());
397398
while(rhs.size()<size) rhs.insert(rhs.end(), tmp.begin(), tmp.end());
398399
if(size!=rhs.size())
399400
error() << "!! failed to obtain rhs (" << rhs.size() << " vs. "
@@ -434,7 +435,7 @@ irep_idt interpretert::get_component_id(
434435
{
435436
if(offset<=0)
436437
return it->id();
437-
auto size=get_size(it->type());
438+
const auto size=get_size(it->type());
438439
offset-=size;
439440
}
440441
return object;
@@ -453,7 +454,7 @@ typet interpretert::get_type(const irep_idt &id) const
453454
/// type
454455
exprt interpretert::get_value(
455456
const typet &type,
456-
uint64_t offset,
457+
mp_integer offset,
457458
bool use_non_det)
458459
{
459460
const typet real_type=ns.follow(type);
@@ -468,7 +469,7 @@ exprt interpretert::get_value(
468469
for(struct_typet::componentst::const_iterator it=components.begin();
469470
it!=components.end(); it++)
470471
{
471-
auto size=get_size(it->type());
472+
const auto size=get_size(it->type());
472473
const exprt operand=get_value(it->type(), offset);
473474
offset+=size;
474475
result.copy_to_operands(operand);
@@ -480,8 +481,8 @@ exprt interpretert::get_value(
480481
// Get size of array
481482
exprt result=array_exprt(to_array_type(real_type));
482483
const exprt &size_expr=static_cast<const exprt &>(type.find(ID_size));
483-
auto subtype_size=get_size(type.subtype());
484-
std::size_t count;
484+
const auto subtype_size=get_size(type.subtype());
485+
mp_integer count=0;
485486
if(size_expr.id()!=ID_constant)
486487
{
487488
count=base_address_to_actual_size(offset)/subtype_size;
@@ -490,12 +491,12 @@ exprt interpretert::get_value(
490491
{
491492
mp_integer mp_count;
492493
to_integer(size_expr, mp_count);
493-
count=integer2size_t(mp_count);
494+
count=mp_count;
494495
}
495496

496497
// Retrieve the value for each member in the array
497-
result.reserve_operands(count);
498-
for(unsigned i=0; i<count; i++)
498+
result.reserve_operands(integer2size_t(count));
499+
for(decltype(count) i=0; i<count; ++i)
499500
{
500501
const exprt operand=get_value(
501502
type.subtype(),
@@ -518,7 +519,7 @@ exprt interpretert::get_value(
518519
exprt interpretert::get_value(
519520
const typet &type,
520521
mp_vectort &rhs,
521-
uint64_t offset)
522+
mp_integer offset)
522523
{
523524
const typet real_type=ns.follow(type);
524525
PRECONDITION(!rhs.empty());
@@ -533,7 +534,7 @@ exprt interpretert::get_value(
533534
result.reserve_operands(components.size());
534535
for(const struct_union_typet::componentt &expr : components)
535536
{
536-
auto size=get_size(expr.type());
537+
const auto size=get_size(expr.type());
537538
const exprt operand=get_value(expr.type(), rhs, offset);
538539
offset+=size;
539540
result.copy_to_operands(operand);
@@ -546,8 +547,8 @@ exprt interpretert::get_value(
546547
const exprt &size_expr=static_cast<const exprt &>(type.find(ID_size));
547548

548549
// Get size of array
549-
auto subtype_size=get_size(type.subtype());
550-
unsigned count;
550+
const auto subtype_size=get_size(type.subtype());
551+
mp_integer count;
551552
if(unbounded_size(type))
552553
{
553554
count=base_address_to_actual_size(offset)/subtype_size;
@@ -556,12 +557,12 @@ exprt interpretert::get_value(
556557
{
557558
mp_integer mp_count;
558559
to_integer(size_expr, mp_count);
559-
count=integer2unsigned(mp_count);
560+
count=mp_count;
560561
}
561562

562563
// Retrieve the value for each member in the array
563-
result.reserve_operands(count);
564-
for(unsigned i=0; i<count; i++)
564+
result.reserve_operands(integer2size_t(count));
565+
for(std::remove_const<decltype(count)>::type i=0; i<count; ++i)
565566
{
566567
const exprt operand=get_value(type.subtype(), rhs,
567568
offset+i*subtype_size);
@@ -572,41 +573,41 @@ exprt interpretert::get_value(
572573
else if(real_type.id()==ID_floatbv)
573574
{
574575
ieee_floatt f(to_floatbv_type(type));
575-
f.unpack(rhs[offset]);
576+
f.unpack(rhs[integer2size_t(offset)]);
576577
return f.to_expr();
577578
}
578579
else if(real_type.id()==ID_fixedbv)
579580
{
580581
fixedbvt f;
581-
f.from_integer(rhs[offset]);
582+
f.from_integer(rhs[integer2size_t(offset)]);
582583
return f.to_expr();
583584
}
584585
else if(real_type.id()==ID_bool)
585586
{
586-
if(rhs[offset]!=0)
587+
if(rhs[integer2size_t(offset)]!=0)
587588
return true_exprt();
588589
else
589590
false_exprt();
590591
}
591592
else if(real_type.id()==ID_c_bool)
592593
{
593-
return from_integer(rhs[offset]!=0?1:0, type);
594+
return from_integer(rhs[integer2size_t(offset)]!=0?1:0, type);
594595
}
595596
else if((real_type.id()==ID_pointer) || (real_type.id()==ID_address_of))
596597
{
597-
if(rhs[offset]==0)
598+
if(rhs[integer2size_t(offset)]==0)
598599
{
599600
// NULL pointer
600601
constant_exprt result(type);
601602
result.set_value(ID_NULL);
602603
return result;
603604
}
604-
if(rhs[offset]<memory.size())
605+
if(rhs[integer2size_t(offset)]<memory.size())
605606
{
606607
// We want the symbol pointed to
607-
std::size_t address=integer2size_t(rhs[offset]);
608+
const auto address=rhs[integer2size_t(offset)];
608609
irep_idt identifier=address_to_identifier(address);
609-
auto offset=address_to_offset(address);
610+
const auto offset=address_to_offset(address);
610611
const typet type=get_type(identifier);
611612
exprt symbol_expr(ID_symbol, type);
612613
symbol_expr.set(ID_identifier, identifier);
@@ -625,19 +626,19 @@ exprt interpretert::get_value(
625626
return index_expr;
626627
}
627628

628-
error() << "interpreter: invalid pointer " << rhs[offset]
629+
error() << "interpreter: invalid pointer " << rhs[integer2size_t(offset)]
629630
<< " > object count " << memory.size() << eom;
630631
throw "interpreter: reading from invalid pointer";
631632
}
632633
else if(real_type.id()==ID_string)
633634
{
634635
// Strings are currently encoded by their irep_idt ID.
635636
return constant_exprt(
636-
irep_idt::make_from_table_index(rhs[offset].to_long()),
637+
irep_idt::make_from_table_index(rhs[integer2size_t(offset)].to_long()),
637638
type);
638639
}
639640
// Retrieve value of basic data type
640-
return from_integer(rhs[offset], type);
641+
return from_integer(rhs[integer2size_t(offset)], type);
641642
}
642643

643644
/// executes the assign statement at the current pc value
@@ -652,7 +653,7 @@ void interpretert::execute_assign()
652653
if(!rhs.empty())
653654
{
654655
mp_integer address=evaluate_address(code_assign.lhs());
655-
auto size=get_size(code_assign.lhs().type());
656+
const auto size=get_size(code_assign.lhs().type());
656657

657658
if(size!=rhs.size())
658659
error() << "!! failed to obtain rhs ("
@@ -680,9 +681,9 @@ void interpretert::execute_assign()
680681
side_effect_exprt side_effect=to_side_effect_expr(code_assign.rhs());
681682
if(side_effect.get_statement()==ID_nondet)
682683
{
683-
unsigned address=integer2unsigned(evaluate_address(code_assign.lhs()));
684-
auto size=get_size(code_assign.lhs().type());
685-
for(size_t i=0; i<size; i++)
684+
const auto address=evaluate_address(code_assign.lhs());
685+
const auto size=get_size(code_assign.lhs().type());
686+
for(std::remove_const<decltype(size)>::type i=0; i<size; ++i)
686687
{
687688
memory[address+i].initialized=
688689
memory_cellt::initializedt::READ_BEFORE_WRITTEN;
@@ -700,7 +701,7 @@ void interpretert::assign(
700701
{
701702
if((address+i)<memory.size())
702703
{
703-
std::size_t address_val=integer2size_t(address+i);
704+
const auto address_val=address+i;
704705
memory_cellt &cell=memory[address_val];
705706
if(show)
706707
{
@@ -750,7 +751,7 @@ void interpretert::execute_function_call()
750751
// Retrieve the empty last trace step struct we pushed for this step
751752
// of the interpreter run to fill it with the corresponding data
752753
goto_trace_stept &trace_step=steps.get_last_step();
753-
std::size_t address=integer2size_t(a);
754+
const auto address=a;
754755
#if 0
755756
const memory_cellt &cell=memory[address];
756757
#endif
@@ -799,7 +800,7 @@ void interpretert::execute_function_call()
799800
for(const auto &id : locals)
800801
{
801802
const symbolt &symbol=ns.lookup(id);
802-
frame.local_map[id]=integer2unsigned(build_memory_map(id, symbol.type));
803+
frame.local_map[id]=build_memory_map(id, symbol.type);
803804
}
804805

805806
// assign the arguments
@@ -863,7 +864,7 @@ void interpretert::build_memory_map()
863864

864865
void interpretert::build_memory_map(const symbolt &symbol)
865866
{
866-
uint64_t size=0;
867+
mp_integer size=0;
867868

868869
if(symbol.type.id()==ID_code)
869870
{
@@ -876,7 +877,7 @@ void interpretert::build_memory_map(const symbolt &symbol)
876877

877878
if(size!=0)
878879
{
879-
auto address=memory.size();
880+
const auto address=memory.size();
880881
memory.resize(address+size);
881882
memory_map[symbol.name]=address;
882883
inverse_memory_map[address]=symbol.name;
@@ -917,7 +918,7 @@ mp_integer interpretert::build_memory_map(
917918
{
918919
typet alloc_type=concretize_type(type);
919920
auto size=get_size(alloc_type);
920-
auto it=dynamic_types.find(id);
921+
const auto it=dynamic_types.find(id);
921922

922923
if(it!=dynamic_types.end())
923924
{
@@ -966,7 +967,7 @@ bool interpretert::unbounded_size(const typet &type)
966967
/// get allocated 2^32 address space each (of a 2^64 sized space).
967968
/// \param type: a structured type
968969
/// \return Size of the given type
969-
uint64_t interpretert::get_size(const typet &type)
970+
mp_integer interpretert::get_size(const typet &type)
970971
{
971972
if(unbounded_size(type))
972973
return 2ULL << 32ULL;
@@ -976,7 +977,7 @@ uint64_t interpretert::get_size(const typet &type)
976977
const struct_typet::componentst &components=
977978
to_struct_type(type).components();
978979

979-
uint64_t sum=0;
980+
mp_integer sum=0;
980981

981982
for(const auto &comp : components)
982983
{
@@ -993,7 +994,7 @@ uint64_t interpretert::get_size(const typet &type)
993994
const union_typet::componentst &components=
994995
to_union_type(type).components();
995996

996-
uint64_t max_size=0;
997+
mp_integer max_size=0;
997998

998999
for(const auto &comp : components)
9991000
{
@@ -1009,7 +1010,7 @@ uint64_t interpretert::get_size(const typet &type)
10091010
{
10101011
const exprt &size_expr=static_cast<const exprt &>(type.find(ID_size));
10111012

1012-
size_t subtype_size=get_size(type.subtype());
1013+
const auto subtype_size=get_size(type.subtype());
10131014

10141015
mp_vectort i;
10151016
evaluate(size_expr, i);
@@ -1021,7 +1022,7 @@ uint64_t interpretert::get_size(const typet &type)
10211022
mp_integer size_mp;
10221023
bool ret=to_integer(size_const, size_mp);
10231024
CHECK_RETURN(!ret);
1024-
return subtype_size*integer2unsigned(size_mp);
1025+
return subtype_size*size_mp;
10251026
}
10261027
return subtype_size;
10271028
}
@@ -1037,7 +1038,7 @@ exprt interpretert::get_value(const irep_idt &id)
10371038
// The dynamic type and the static symbol type may differ for VLAs,
10381039
// where the symbol carries a size expression and the dynamic type
10391040
// registry contains its actual length.
1040-
auto findit=dynamic_types.find(id);
1041+
const auto findit=dynamic_types.find(id);
10411042
typet get_type;
10421043
if(findit!=dynamic_types.end())
10431044
get_type=findit->second;
@@ -1047,7 +1048,7 @@ exprt interpretert::get_value(const irep_idt &id)
10471048
symbol_exprt symbol_expr(id, get_type);
10481049
mp_integer whole_lhs_object_address=evaluate_address(symbol_expr);
10491050

1050-
return get_value(get_type, integer2size_t(whole_lhs_object_address));
1051+
return get_value(get_type, whole_lhs_object_address);
10511052
}
10521053

10531054
void interpreter(

src/goto-programs/interpreter_class.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ class interpretert:public messaget
141141
return next_alloc_address-address;
142142
}
143143

144-
uint64_t base_address_to_actual_size(const mp_integer &address) const
144+
mp_integer base_address_to_actual_size(const mp_integer &address) const
145145
{
146146
auto memory_iter=memory.find(address);
147147
if(memory_iter==memory.end())
148148
return 0;
149-
std::size_t ret=0;
149+
mp_integer ret=0;
150150
mp_integer alloc_size=base_address_to_alloc_size(address);
151151
while(memory_iter!=memory.end() && memory_iter->first<(address+alloc_size))
152152
{
@@ -193,15 +193,16 @@ class interpretert:public messaget
193193
mp_integer build_memory_map(const irep_idt &id, const typet &type);
194194
typet concretize_type(const typet &type);
195195
bool unbounded_size(const typet &);
196-
uint64_t get_size(const typet &type);
196+
mp_integer get_size(const typet &type);
197197

198198
irep_idt get_component_id(const irep_idt &object, mp_integer offset);
199199
typet get_type(const irep_idt &id) const;
200200
exprt get_value(
201201
const typet &type,
202-
uint64_t offset=0,
202+
mp_integer offset=0,
203203
bool use_non_det=false);
204-
exprt get_value(const typet &type, mp_vectort &rhs, uint64_t offset=0);
204+
exprt get_value(
205+
const typet &type, mp_vectort &rhs, mp_integer offset=0);
205206
exprt get_value(const irep_idt &id);
206207

207208
void step();
@@ -217,7 +218,7 @@ class interpretert:public messaget
217218

218219
void allocate(
219220
const mp_integer &address,
220-
size_t size);
221+
mp_integer size);
221222

222223
void assign(
223224
const mp_integer &address,

0 commit comments

Comments
 (0)