Skip to content

Commit 5c03b6d

Browse files
New class for call_stack
Extract a call_stackt class from goto_symex_statet to declutter the class definition. This makes some calls a bit more verbose, but can be clearer. For instance, it is not obvious what `top()` represent in the context of goto_symex_state, whereas `call_stack().top()` is more explicit.
1 parent a6db341 commit 5c03b6d

17 files changed

+85
-63
lines changed

jbmc/src/java_bytecode/java_bmc_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void java_setup_symex(
2828
// for some reason)
2929
symex.add_loop_unwind_handler(
3030
[&goto_model](
31-
const goto_symex_statet::call_stackt &context,
31+
const call_stackt &context,
3232
unsigned loop_num,
3333
unsigned unwind,
3434
unsigned &max_unwind)

jbmc/src/java_bytecode/java_enum_static_init_unwind_handler.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Author: Chris Smowton, [email protected]
2424
/// \param context: the current call stack
2525
/// \return the name of an enclosing function that may be defined on the
2626
/// relevant enum type, or an empty string if we don't find one.
27-
static irep_idt
28-
find_enum_function_on_stack(const goto_symex_statet::call_stackt &context)
27+
static irep_idt find_enum_function_on_stack(const call_stackt &context)
2928
{
3029
static irep_idt reference_array_clone_id =
3130
"java::array[reference].clone:()Ljava/lang/Object;";
@@ -64,7 +63,7 @@ find_enum_function_on_stack(const goto_symex_statet::call_stackt &context)
6463
/// unwind_count is <= the enumeration size, or unknown (defer / no decision)
6564
/// otherwise.
6665
tvt java_enum_static_init_unwind_handler(
67-
const goto_symex_statet::call_stackt &context,
66+
const call_stackt &context,
6867
unsigned loop_number,
6968
unsigned unwind_count,
7069
unsigned &unwind_max,

jbmc/src/java_bytecode/java_enum_static_init_unwind_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Author: Chris Smowton, [email protected]
1818
#include <util/threeval.h>
1919

2020
tvt java_enum_static_init_unwind_handler(
21-
const goto_symex_statet::call_stackt &context,
21+
const call_stackt &context,
2222
unsigned loop_number,
2323
unsigned unwind_count,
2424
unsigned &unwind_max,

jbmc/src/jbmc/jbmc_parse_options.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ int jbmc_parse_optionst::doit()
514514
configure_bmc = [](bmct &bmc, const symbol_tablet &symbol_table) {
515515
bmc.add_loop_unwind_handler(
516516
[&symbol_table](
517-
const goto_symex_statet::call_stackt &context,
517+
const call_stackt &context,
518518
unsigned loop_number,
519519
unsigned unwind,
520520
unsigned &max_unwind) {

src/goto-checker/symex_bmc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void symex_bmct::merge_goto(const goto_statet &goto_state, statet &state)
105105

106106
bool symex_bmct::should_stop_unwind(
107107
const symex_targett::sourcet &source,
108-
const goto_symex_statet::call_stackt &context,
108+
const call_stackt &context,
109109
unsigned unwind)
110110
{
111111
const irep_idt id = goto_programt::loop_id(source.function_id, *source.pc);

src/goto-checker/symex_bmc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class symex_bmct : public goto_symext
4242
/// enforced. They return true to halt unwinding, false to authorise
4343
/// unwinding, or Unknown to indicate they have no opinion.
4444
typedef std::function<
45-
tvt(const goto_symex_statet::call_stackt &, unsigned, unsigned, unsigned &)>
45+
tvt(const call_stackt &, unsigned, unsigned, unsigned &)>
4646
loop_unwind_handlert;
4747

4848
/// Recursion unwind handlers take the function ID, the unwind count so far,
@@ -98,7 +98,7 @@ class symex_bmct : public goto_symext
9898

9999
bool should_stop_unwind(
100100
const symex_targett::sourcet &source,
101-
const goto_symex_statet::call_stackt &context,
101+
const call_stackt &context,
102102
unsigned unwind) override;
103103

104104
bool get_unwind_recursion(

src/goto-symex/call_stack.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*******************************************************************\
2+
3+
Module: Symbolic Execution
4+
5+
Author: Romain Brenguier, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_GOTO_SYMEX_CALL_STACK_H
10+
#define CPROVER_GOTO_SYMEX_CALL_STACK_H
11+
12+
#include "frame.h"
13+
14+
class call_stackt : public std::vector<framet>
15+
{
16+
public:
17+
framet &top()
18+
{
19+
PRECONDITION(!empty());
20+
return back();
21+
}
22+
23+
const framet &top() const
24+
{
25+
PRECONDITION(!empty());
26+
return back();
27+
}
28+
29+
framet &new_frame(symex_targett::sourcet calling_location)
30+
{
31+
emplace_back(calling_location);
32+
return back();
33+
}
34+
35+
void pop()
36+
{
37+
pop_back();
38+
}
39+
40+
const framet &previous_frame()
41+
{
42+
return *(--(--end()));
43+
}
44+
};
45+
46+
#endif // CPROVER_GOTO_SYMEX_CALL_STACK_H

src/goto-symex/goto_symex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class goto_symext
245245
// with false we continue.
246246
virtual bool should_stop_unwind(
247247
const symex_targett::sourcet &source,
248-
const goto_symex_statet::call_stackt &context,
248+
const call_stackt &context,
249249
unsigned unwind);
250250

251251
virtual void loop_bound_exceeded(statet &, const exprt &guard);

src/goto-symex/goto_symex_state.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ goto_symex_statet::goto_symex_statet(const symex_targett::sourcet &_source)
3333
: goto_statet(_source), symex_target(nullptr), record_events(true), dirty()
3434
{
3535
threads.resize(1);
36-
new_frame();
36+
call_stack().new_frame(source);
3737
}
3838

3939
goto_symex_statet::~goto_symex_statet()=default;

src/goto-symex/goto_symex_state.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Author: Daniel Kroening, [email protected]
2424
#include <util/make_unique.h>
2525
#include <goto-programs/goto_function.h>
2626

27+
#include "call_stack.h"
2728
#include "frame.h"
2829
#include "goto_state.h"
2930
#include "renaming_level.h"
@@ -139,8 +140,6 @@ class goto_symex_statet final : public goto_statet
139140
return id;
140141
}
141142

142-
typedef std::vector<framet> call_stackt;
143-
144143
call_stackt &call_stack()
145144
{
146145
PRECONDITION(source.thread_nr < threads.size());
@@ -153,28 +152,6 @@ class goto_symex_statet final : public goto_statet
153152
return threads[source.thread_nr].call_stack;
154153
}
155154

156-
framet &top()
157-
{
158-
PRECONDITION(!call_stack().empty());
159-
return call_stack().back();
160-
}
161-
162-
const framet &top() const
163-
{
164-
PRECONDITION(!call_stack().empty());
165-
return call_stack().back();
166-
}
167-
168-
framet &new_frame()
169-
{
170-
call_stack().emplace_back(source);
171-
return top();
172-
}
173-
174-
void pop_frame() { call_stack().pop_back(); }
175-
176-
const framet &previous_frame() { return *(--(--call_stack().end())); }
177-
178155
void print_backtrace(std::ostream &) const;
179156

180157
// threads

src/goto-symex/symex_assign.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void goto_symext::symex_assign(
6767
assignment_type=symex_targett::assignment_typet::HIDDEN;
6868

6969
// We hide if we are in a hidden function.
70-
if(state.top().hidden_function)
70+
if(state.call_stack().top().hidden_function)
7171
assignment_type=symex_targett::assignment_typet::HIDDEN;
7272

7373
// We hide if we are executing a hidden instruction.

src/goto-symex/symex_decl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ void goto_symext::symex_decl(statet &state, const symbol_exprt &expr)
7878

7979
// we hide the declaration of auxiliary variables
8080
// and if the statement itself is hidden
81-
bool hidden=
82-
ns.lookup(expr.get_identifier()).is_auxiliary ||
83-
state.top().hidden_function ||
84-
state.source.pc->source_location.get_hide();
81+
bool hidden = ns.lookup(expr.get_identifier()).is_auxiliary ||
82+
state.call_stack().top().hidden_function ||
83+
state.source.pc->source_location.get_hide();
8584

8685
target.decl(
8786
state.guard.as_expr(),

src/goto-symex/symex_function_call.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void goto_symext::parameter_assignments(
124124
assignment_typet assignment_type;
125125

126126
// We hide if we are in a hidden function.
127-
if(state.top().hidden_function)
127+
if(state.call_stack().top().hidden_function)
128128
assignment_type =
129129
symex_targett::assignment_typet::HIDDEN_ACTUAL_PARAMETER;
130130
else
@@ -233,10 +233,10 @@ void goto_symext::symex_function_call_code(
233233
if(emplace_safe_pointers_result.second)
234234
emplace_safe_pointers_result.first->second(goto_function.body);
235235

236-
const bool stop_recursing=get_unwind_recursion(
236+
const bool stop_recursing = get_unwind_recursion(
237237
identifier,
238238
state.source.thread_nr,
239-
state.top().loop_iterations[identifier].count);
239+
state.call_stack().top().loop_iterations[identifier].count);
240240

241241
// see if it's too much
242242
if(stop_recursing)
@@ -264,7 +264,8 @@ void goto_symext::symex_function_call_code(
264264
a = state.rename(std::move(a), ns);
265265

266266
// we hide the call if the caller and callee are both hidden
267-
const bool hidden = state.top().hidden_function && goto_function.is_hidden();
267+
const bool hidden =
268+
state.call_stack().top().hidden_function && goto_function.is_hidden();
268269

269270
// record the call
270271
target.function_call(
@@ -292,7 +293,7 @@ void goto_symext::symex_function_call_code(
292293

293294
// produce a new frame
294295
PRECONDITION(!state.call_stack().empty());
295-
framet &frame = state.new_frame();
296+
framet &frame = state.call_stack().new_frame(state.source);
296297

297298
// preserve locality of local variables
298299
locality(identifier, state, goto_function, ns);
@@ -305,7 +306,7 @@ void goto_symext::symex_function_call_code(
305306
frame.function_identifier=identifier;
306307
frame.hidden_function=goto_function.is_hidden();
307308

308-
const framet &p_frame = state.previous_frame();
309+
const framet &p_frame = state.call_stack().previous_frame();
309310
for(const auto &pair : p_frame.loop_iterations)
310311
{
311312
if(pair.second.is_recursion)
@@ -326,7 +327,7 @@ static void pop_frame(goto_symext::statet &state)
326327
PRECONDITION(!state.call_stack().empty());
327328

328329
{
329-
framet &frame = state.top();
330+
const framet &frame = state.call_stack().top();
330331

331332
// restore program counter
332333
symex_transition(state, frame.calling_location.pc, false);
@@ -355,13 +356,13 @@ static void pop_frame(goto_symext::statet &state)
355356
}
356357
}
357358

358-
state.pop_frame();
359+
state.call_stack().pop();
359360
}
360361

361362
/// do function call by inlining
362363
void goto_symext::symex_end_of_function(statet &state)
363364
{
364-
const bool hidden = state.top().hidden_function;
365+
const bool hidden = state.call_stack().top().hidden_function;
365366

366367
// first record the return
367368
target.function_return(
@@ -387,7 +388,7 @@ static void locality(
387388

388389
get_local_identifiers(goto_function, local_identifiers);
389390

390-
framet &frame = state.top();
391+
framet &frame = state.call_stack().top();
391392

392393
for(std::set<irep_idt>::const_iterator
393394
it=local_identifiers.begin();

src/goto-symex/symex_goto.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Author: Daniel Kroening, [email protected]
2525
void goto_symext::symex_goto(statet &state)
2626
{
2727
const goto_programt::instructiont &instruction=*state.source.pc;
28-
framet &frame = state.top();
2928

3029
if(state.guard.is_false())
3130
{
@@ -87,7 +86,7 @@ void goto_symext::symex_goto(statet &state)
8786
const auto loop_id =
8887
goto_programt::loop_id(state.source.function_id, *state.source.pc);
8988

90-
unsigned &unwind = frame.loop_iterations[loop_id].count;
89+
unsigned &unwind = state.call_stack().top().loop_iterations[loop_id].count;
9190
unwind++;
9291

9392
if(should_stop_unwind(state.source, state.call_stack(), unwind))
@@ -219,7 +218,7 @@ void goto_symext::symex_goto(statet &state)
219218
// put a copy of the current state into the state-queue, to be used by
220219
// merge_gotos when we visit new_state_pc
221220
framet::goto_state_listt &goto_state_list =
222-
state.top().goto_state_map[new_state_pc];
221+
state.call_stack().top().goto_state_map[new_state_pc];
223222
goto_state_list.emplace_back(state);
224223

225224
symex_transition(state, state_pc, backward);
@@ -297,7 +296,7 @@ void goto_symext::symex_goto(statet &state)
297296

298297
void goto_symext::merge_gotos(statet &state)
299298
{
300-
framet &frame = state.top();
299+
framet &frame = state.call_stack().top();
301300

302301
// first, see if this is a target at all
303302
auto state_map_it = frame.goto_state_map.find(state.source.pc);
@@ -578,7 +577,7 @@ void goto_symext::loop_bound_exceeded(
578577

579578
bool goto_symext::should_stop_unwind(
580579
const symex_targett::sourcet &,
581-
const goto_symex_statet::call_stackt &,
580+
const call_stackt &,
582581
unsigned)
583582
{
584583
// by default, we keep going

src/goto-symex/symex_main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void symex_transition(
5353
// 1. the transition from state.source.pc to "to" is not a backwards goto
5454
// or
5555
// 2. we are arriving from an outer loop
56-
framet &frame = state.top();
56+
framet &frame = state.call_stack().top();
5757
const goto_programt::instructiont &instruction=*to;
5858
for(const auto &i_e : instruction.incoming_edges)
5959
if(i_e->is_goto() && i_e->is_backwards_goto() &&
@@ -234,7 +234,7 @@ void goto_symext::symex_with_state(
234234
// as state.symbol_table might go out of scope
235235
reset_namespacet reset_ns(ns);
236236

237-
PRECONDITION(state.top().end_of_function->is_end_function());
237+
PRECONDITION(state.call_stack().top().end_of_function->is_end_function());
238238

239239
symex_threaded_step(state, get_goto_function);
240240
if(should_pause_symex)
@@ -300,9 +300,10 @@ std::unique_ptr<goto_symext::statet> goto_symext::initialize_entry_point_state(
300300

301301
goto_programt::const_targett limit =
302302
std::prev(start_function->body.instructions.end());
303-
state->top().end_of_function = limit;
304-
state->top().calling_location.pc = state->top().end_of_function;
305-
state->top().hidden_function = start_function->is_hidden();
303+
state->call_stack().top().end_of_function = limit;
304+
state->call_stack().top().calling_location.pc =
305+
state->call_stack().top().end_of_function;
306+
state->call_stack().top().hidden_function = start_function->is_hidden();
306307

307308
state->symex_target = &target;
308309

src/goto-symex/symex_start_thread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ void goto_symext::symex_start_thread(statet &state)
4242
statet::threadt &new_thread=state.threads.back();
4343
new_thread.pc=thread_target;
4444
new_thread.guard=state.guard;
45-
new_thread.call_stack.push_back(state.top());
45+
new_thread.call_stack.push_back(state.call_stack().top());
4646
new_thread.call_stack.back().local_objects.clear();
4747
new_thread.call_stack.back().goto_state_map.clear();
4848
#if 0
4949
new_thread.abstract_events=&(target.new_thread(cur_thread.abstract_events));
5050
#endif
5151

5252
// create a copy of the local variables for the new thread
53-
framet &frame = state.top();
53+
framet &frame = state.call_stack().top();
5454

5555
for(auto c_it = state.level2.current_names.begin();
5656
c_it != state.level2.current_names.end();

0 commit comments

Comments
 (0)