Skip to content

Commit bf427fe

Browse files
committed
Extended draft implementation for CEGIS null object.
1 parent 0b3fc40 commit bf427fe

13 files changed

+189
-67
lines changed

src/cegis/cegis-util/instruction_iterator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,4 @@ instr_iteratort::operator goto_programt::targett() const
7171
return prog_it;
7272
}
7373

74-
goto_programt::targett instr_iteratort::body_end() const
75-
{
76-
return func_it->second.body.instructions.end();
77-
}
78-
7974
const instr_iteratort instr_iteratort::end;

src/cegis/cegis-util/instruction_iterator.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,6 @@ class instr_iteratort: public std::iterator<std::input_iterator_tag,
105105
*/
106106
operator goto_programt::targett() const;
107107

108-
/**
109-
* @brief
110-
*
111-
* @details
112-
*
113-
* @return
114-
*/
115-
goto_programt::targett body_end() const;
116-
117108
/**
118109
* @brief
119110
*

src/cegis/cegis-util/program_helper.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ const goto_programt &get_entry_body(const goto_functionst &gf)
1919
return get_body(gf, id2string(goto_functionst::entry_point()));
2020
}
2121

22-
class goto_programt &get_body(
23-
class goto_functionst &gf,
24-
const std::string &func_name)
22+
goto_programt &get_body(goto_functionst &gf, const std::string &func_name)
2523
{
2624
const irep_idt id(func_name);
2725
goto_functionst::function_mapt &function_map=gf.function_map;
@@ -32,8 +30,12 @@ class goto_programt &get_body(
3230
return f.body;
3331
}
3432

35-
const goto_programt &get_body(
36-
const goto_functionst &gf,
33+
goto_programt &get_body(goto_functionst &gf, const goto_programt::const_targett pos)
34+
{
35+
return get_body(gf, id2string(pos->function));
36+
}
37+
38+
const goto_programt &get_body(const goto_functionst &gf,
3739
const std::string &func_name)
3840
{
3941
const irep_idt id(func_name);

src/cegis/cegis-util/program_helper.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,24 @@ const goto_programt &get_entry_body(const goto_functionst &gf);
4444
*
4545
* @return
4646
*/
47-
class goto_programt &get_body(
48-
class goto_functionst &gf,
47+
goto_programt &get_body(
48+
goto_functionst &gf,
4949
const std::string &func_name);
5050

51+
/**
52+
* @brief
53+
*
54+
* @details
55+
*
56+
* @param gf
57+
* @param pos
58+
*
59+
* @return
60+
*/
61+
goto_programt &get_body(
62+
goto_functionst &gf,
63+
goto_programt::const_targett pos);
64+
5165
/**
5266
* @brief
5367
*

src/cegis/cegis-util/type_helper.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <util/std_types.h>
22
#include <util/symbol_table.h>
3+
#include <util/type_eq.h>
4+
#include <util/namespace.h>
35

46
#define TAG_PREFIX "tag-"
57

@@ -13,3 +15,30 @@ const typet &replace_struct_by_symbol_type(const symbol_tablet &st,
1315
tag+=id2string(to_struct_union_type(type).get_tag());
1416
return st.lookup(tag).type;
1517
}
18+
19+
namespace
20+
{
21+
bool instanceof(const typet &lhs, const typet &rhs, const namespacet &ns)
22+
{
23+
if (type_eq(lhs, rhs, ns)) return true;
24+
assert(ID_class == lhs.id());
25+
const class_typet &lhs_class=to_class_type(lhs);
26+
const irept::subt &bases=lhs_class.bases();
27+
for (const irept &base : bases)
28+
{
29+
const typet &type=static_cast<const typet &>(base.find(ID_type));
30+
if (instanceof(ns.follow(type), rhs, ns)) return true;
31+
}
32+
return false;
33+
}
34+
}
35+
36+
bool instanceof(const symbol_tablet &st, const typet &lhs, const typet &rhs)
37+
{
38+
const namespacet ns(st);
39+
const typet &resolved_lhs=ns.follow(lhs);
40+
const typet &resolved_rhs=ns.follow(rhs);
41+
if (ID_class != resolved_lhs.id() || ID_class != resolved_rhs.id())
42+
return type_eq(resolved_lhs, resolved_rhs, ns);
43+
return instanceof(resolved_lhs, resolved_rhs, ns);
44+
}

src/cegis/cegis-util/type_helper.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ const class typet &replace_struct_by_symbol_type(
2222
const class symbol_tablet &st,
2323
const typet &type);
2424

25+
/**
26+
* @brief
27+
*
28+
* @details
29+
*
30+
* @param lhs
31+
* @param rhs
32+
* @param st
33+
*/
34+
bool instanceof(const symbol_tablet &st, const typet &lhs, const typet &rhs);
35+
2536
#endif /* CEGIS_UTIL_TYPE_HELPER_H_ */
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
#include <cegis/refactor/environment/instrument_state_vars.h>
22

3-
void instrument_state_vars(const goto_programt::const_targett first,
4-
const goto_programt::const_targett last)
3+
void instrument_state_vars(goto_programt &body,
4+
const goto_programt::targett &first, const goto_programt::targett &last,
5+
std::function<bool(const goto_programt::instructiont &)> predicate)
56
{
67
// TODO: Implement
78
assert(false);
89
}
10+
11+
namespace
12+
{
13+
bool yes(const goto_programt::instructiont &instr)
14+
{
15+
return true;
16+
}
17+
}
18+
19+
void instrument_state_vars(goto_programt &body,
20+
const goto_programt::targett &first, const goto_programt::targett &last)
21+
{
22+
instrument_state_vars(body, first, last, yes);
23+
}

src/cegis/refactor/environment/instrument_state_vars.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,38 @@
1010
#ifndef CEGIS_REFACTOR_ENVIRONMENT_INSTRUMENT_STATE_VARS_H_
1111
#define CEGIS_REFACTOR_ENVIRONMENT_INSTRUMENT_STATE_VARS_H_
1212

13+
#include <functional>
14+
1315
#include <goto-programs/goto_program.h>
1416

1517
/**
1618
* @brief
1719
*
1820
* @details
1921
*
22+
* @param body
23+
* @param first
24+
* @param last
25+
* @param predicate
26+
*/
27+
void instrument_state_vars(
28+
goto_programt &body,
29+
const goto_programt::targett &first,
30+
const goto_programt::targett &last,
31+
std::function<bool(const goto_programt::instructiont &)> predicate);
32+
33+
/**
34+
* @brief
35+
*
36+
* @details
37+
*
38+
* @param body
2039
* @param first
2140
* @param last
2241
*/
2342
void instrument_state_vars(
24-
goto_programt::const_targett first,
25-
goto_programt::const_targett last);
43+
goto_programt &body,
44+
const goto_programt::targett &first,
45+
const goto_programt::targett &last);
2646

2747
#endif /* CEGIS_REFACTOR_ENVIRONMENT_INSTRUMENT_STATE_VARS_H_ */

src/cegis/refactor/instructionset/create_cegis_processor.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
#include <cegis/refactor/instructionset/create_cegis_processor.h>
22

3+
namespace
4+
{
5+
class type_collectort: public const_expr_visitort
6+
{
7+
public:
8+
std::set<typet> types;
9+
10+
virtual ~type_collectort()=default;
11+
12+
virtual void operator()(const exprt &expr)
13+
{
14+
types.insert(expr.type());
15+
}
16+
};
17+
}
18+
19+
std::set<typet> collect_context_types(goto_programt::const_targett first,
20+
const goto_programt::const_targett &last)
21+
{
22+
type_collectort collector;
23+
for (; first != last; ++first)
24+
first->code.visit(collector);
25+
return collector.types;
26+
}
27+
328
void create_cegis_processor(const std::set<typet> &state_types,
429
const size_t var_slots_per_state_type, const std::set<typet> &context_types)
530
{

src/cegis/refactor/instructionset/create_cegis_processor.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
#include <set>
1414

1515
#include <util/type.h>
16+
#include <goto-programs/goto_program.h>
17+
18+
/**
19+
* @brief
20+
*
21+
* @details
22+
*
23+
* @param first
24+
* @param last
25+
*
26+
* @return
27+
*/
28+
std::set<typet> collect_context_types(
29+
goto_programt::const_targett first,
30+
const goto_programt::const_targett &last);
1631

1732
/**
1833
* @brief
Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
#include <cegis/cegis-util/instruction_iterator.h>
2-
32
#include <cegis/refactor/options/refactor_program.h>
3+
#include <cegis/refactor/instructionset/create_cegis_processor.h>
44
#include <cegis/refactor/nullobject/range_collector.h>
55

6-
// XXX: Debug
7-
#include <iostream>
8-
// XXX: Debug
9-
106
namespace
117
{
8+
bool is_null(const exprt &expr)
9+
{
10+
if (ID_constant != expr.id()) return false;
11+
return ID_NULL == to_constant_expr(expr).get_value();
12+
}
13+
1214
bool is_null_comparison(const exprt &guard)
1315
{
1416
const irep_idt &id=guard.id();
1517
if (ID_equal != id && ID_notequal != id) return false;
1618
const binary_relation_exprt &binary=to_binary_relation_expr(guard);
17-
return ID_NULL == binary.lhs().id() || ID_NULL == binary.rhs().id();
18-
}
19-
20-
class null_reference_type_extractort: public const_expr_visitort
21-
{
22-
public:
23-
typet reference_type;
24-
25-
virtual ~null_reference_type_extractort()=default;
26-
27-
virtual void operator()(const exprt &expr)
28-
{
29-
if (ID_symbol == expr.id()) reference_type=expr.type();
30-
}
31-
};
32-
33-
typet get_reference_type(const exprt &guard)
34-
{
35-
null_reference_type_extractort extractor;
36-
guard.visit(extractor);
37-
return extractor.reference_type;
19+
return is_null(binary.lhs()) || is_null(binary.rhs());
3820
}
3921

4022
goto_ranget get_then_range(const goto_programt::targett &else_range_last)
@@ -52,25 +34,25 @@ void collect_nullobject_ranges(refactor_programt &prog)
5234
for (instr_iteratort it(prog.gf); it != instr_iteratort::end; ++it)
5335
{
5436
if (goto_program_instruction_typet::GOTO != it->type) continue;
55-
if (!is_null_comparison(it->guard)) continue;
37+
const exprt &guard=it->guard;
38+
if (!is_null_comparison(guard)) continue;
5639
const goto_programt::targett else_range_last(it->get_target());
5740
const goto_ranget else_range(++it, else_range_last);
5841
const goto_ranget then_range(get_then_range(else_range_last));
59-
if (ID_equal == it->guard.id())
42+
prog.sketches.push_back(refactor_programt::sketcht());
43+
refactor_programt::sketcht &sketch=prog.sketches.back();
44+
if (ID_equal == guard.id())
6045
{
61-
prog.spec_ranges.push_back(else_range);
62-
prog.input_ranges.push_back(then_range);
46+
sketch.spec_range=then_range;
47+
sketch.input_range=else_range;
6348
} else
6449
{
65-
prog.spec_ranges.push_back(then_range);
66-
prog.input_ranges.push_back(else_range);
50+
sketch.spec_range=else_range;
51+
sketch.input_range=then_range;
6752
}
53+
sketch.types=collect_context_types(then_range.first, then_range.second);
54+
const std::set<typet> tmp(
55+
collect_context_types(else_range.first, else_range.second));
56+
sketch.types.insert(tmp.begin(), tmp.end());
6857
}
69-
// XXX: Debug
70-
std::cout << "<collect_nullobject_range>" << std::endl;
71-
prog.gf.output(namespacet(prog.st), std::cout);
72-
std::cout << "</collect_nullobject_range>" << std::endl;
73-
// XXX: Debug
74-
// TODO: Implement
75-
assert(false);
7658
}

src/cegis/refactor/options/refactor_program.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ class refactor_programt
3939
*
4040
* @details
4141
*/
42-
std::deque<goto_ranget> input_ranges;
42+
class sketcht
43+
{
44+
public:
45+
goto_ranget input_range;
46+
goto_ranget spec_range;
47+
std::set<typet> types;
48+
};
4349

4450
/**
4551
* @brief
4652
*
4753
* @details
4854
*/
49-
std::deque<goto_ranget> spec_ranges;
55+
std::deque<sketcht> sketches;
5056

5157
/**
5258
* @brief

0 commit comments

Comments
 (0)