-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathgoto_program_irep.cpp
144 lines (116 loc) · 3.78 KB
/
goto_program_irep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************\
Module: goto_programt -> irep conversion
Author: CM Wintersteiger
Date: May 2007
\*******************************************************************/
/// \file
/// goto_programt -> irep conversion
#include "goto_program_irep.h"
#include <iostream>
#include <util/string2int.h>
void convert(const goto_programt::instructiont &instruction, irept &irep)
{
irep.set(ID_code, instruction.code);
if(instruction.function!="")
irep.set(ID_function, instruction.function);
if(instruction.source_location.is_not_nil())
irep.set(ID_location, instruction.source_location);
irep.set(ID_type, (long) instruction.type);
irep.set(ID_guard, instruction.guard);
if(!instruction.targets.empty())
{
irept &tgts=irep.add(ID_targets);
for(const auto &target : instruction.targets)
{
irept t(std::to_string(target->location_number));
tgts.move_to_sub(t);
}
}
if(!instruction.labels.empty())
{
irept &lbls = irep.add(ID_labels);
irept::subt &subs = lbls.get_sub();
subs.reserve(instruction.labels.size());
for(const auto &label : instruction.labels)
{
subs.push_back(irept(label));
}
}
}
void convert(
const irept &irep,
goto_programt::instructiont &instruction)
{
instruction.code=static_cast<const codet &>(irep.find(ID_code));
instruction.function = irep.find(ID_function).id();
instruction.source_location=
static_cast<const source_locationt &>(irep.find(ID_location));
instruction.type = static_cast<goto_program_instruction_typet>(
unsafe_string2unsigned(irep.find(ID_type).id_string()));
instruction.guard = static_cast<const exprt&>(irep.find(ID_guard));
// don't touch the targets, the goto_programt conversion does that
const irept &lbls=irep.find(ID_labels);
const irept::subt &lsubs=lbls.get_sub();
for(const auto &lsub : lsubs)
instruction.labels.push_back(lsub.id());
}
void convert(const goto_programt &program, irept &irep)
{
irep.id("goto-program");
irep.get_sub().reserve(program.instructions.size());
forall_goto_program_instructions(it, program)
{
irep.get_sub().push_back(irept());
convert(*it, irep.get_sub().back());
}
}
void convert(const irept &irep, goto_programt &program)
{
assert(irep.id()=="goto-program");
program.instructions.clear();
std::list< std::list<unsigned> > number_targets_list;
// convert instructions back
const irept::subt &subs = irep.get_sub();
for(irept::subt::const_iterator it=subs.begin();
it!=subs.end();
it++)
{
program.instructions.push_back(goto_programt::instructiont());
convert(*it, program.instructions.back());
number_targets_list.push_back(std::list<unsigned>());
const irept &targets=it->find(ID_targets);
const irept::subt &tsubs=targets.get_sub();
for(const auto &tsub : tsubs)
number_targets_list.back().push_back(
unsafe_string2unsigned(tsub.id_string()));
}
program.compute_location_numbers();
// resolve targets
std::list< std::list<unsigned> >::iterator nit=
number_targets_list.begin();
for(goto_programt::instructionst::iterator lit=
program.instructions.begin();
lit!=program.instructions.end() && nit!=number_targets_list.end();
lit++, nit++)
{
for(const unsigned t : *nit)
{
goto_programt::targett fit=program.instructions.begin();
for( ; fit!=program.instructions.end(); fit++)
{
if(fit->location_number==t)
{
lit->targets.push_back(fit);
break;
}
}
if(fit==program.instructions.end())
{
std::cout << "Warning: could not resolve target link "
<< "during irep->goto_program translation.\n";
throw 0;
}
}
}
program.update();
}