32 cfg_base_nodet<T, java_bytecode_convert_methodt::method_offsett>>
51 for(
const auto &inst : amap)
56 (*this)[entry_map[inst.first]].PC=inst.first;
59 for(
const auto &inst : amap)
61 for(
auto succ : inst.second.successors)
66 for(
const auto &table_entry : method.exception_table)
68 auto findit=amap.find(table_entry.start_pc);
71 "Exception table entry doesn't point to an instruction?");
72 for(; findit->first<table_entry.end_pc; ++findit)
78 if(succit==amap.end())
80 const auto &thisinst=findit->second;
81 if(thisinst.successors.size()==1 &&
82 thisinst.successors.back()==succit->first)
85 entry_map.at(findit->first),
158 std::set<local_variable_with_holest *> >
171 auto findit=
order.find(a);
172 if(findit==
order.end())
174 return findit->second.count(b)>0;
188 std::set<local_variable_with_holest*> &result)
190 if(!result.insert(start).second)
192 auto findit=predecessor_map.find(start);
193 if(findit==predecessor_map.end())
195 for(
const auto pred : findit->second)
210 if(!(prevstatement.size()>=1 && prevstatement.substr(1, 5)==
"store"))
213 unsigned storeslotidx;
214 if(inst.
args.size()==1)
217 const auto &arg=inst.
args[0];
224 prevstatement[6]==
'_' && prevstatement.size()==8,
225 "expected store instruction looks like store_0, store_1...");
226 std::string storeslot(1, prevstatement[7]);
229 "store_? instructions should end in a digit");
232 return storeslotidx==slotidx;
259 local_variable_table_with_holest::iterator firstvar,
260 local_variable_table_with_holest::iterator varlimit,
261 std::vector<local_variable_with_holest *> &live_variable_at_address)
263 for(
auto it=firstvar, itend=varlimit; it!=itend; ++it)
265 if(it->var.start_pc+it->var.length>live_variable_at_address.size())
266 live_variable_at_address.resize(it->var.start_pc+it->var.length);
268 for(
auto idx = it->var.start_pc, idxlim = it->var.start_pc + it->var.length;
272 INVARIANT(!live_variable_at_address[idx],
"Local variable table clash?");
273 live_variable_at_address[idx]=&*it;
304 local_variable_table_with_holest::iterator firstvar,
305 local_variable_table_with_holest::iterator varlimit,
306 const std::vector<local_variable_with_holest *> &live_variable_at_address,
312 for(
auto it=firstvar, itend=varlimit; it!=itend; ++it)
318 it->var.index==firstvar->var.index,
319 "all entries are for the same local variable slot");
328 msg.
debug() <<
"jcm: ppm: processing var idx " << it->var.index
329 <<
" name '" << it->var.name <<
"' start-pc "
330 << it->var.start_pc <<
" len " << it->var.length
335 const auto end_pc = it->var.start_pc + it->var.length;
336 auto amapit=amap.find(end_pc);
338 amapit!=amap.begin(),
339 "current bytecode shall not be the first");
340 auto old_amapit=amapit;
342 if(old_amapit==amap.end())
345 end_pc>amapit->first,
346 "Instruction live range doesn't align to instruction boundary?");
354 auto new_start_pc = it->var.start_pc;
355 for(; amapit->first>=it->var.start_pc; --amapit)
357 for(
auto pred : amapit->second.predecessors)
365 (pred<live_variable_at_address.size() ?
366 live_variable_at_address[pred] :
382 auto inst_before_this=amapit;
384 inst_before_this!=amap.begin(),
385 "we shall not be on the first bytecode of the method");
387 if(amapit->first!=it->var.start_pc || inst_before_this->first!=pred)
392 msg.
warning() <<
"Local variable table: ignoring flow from "
393 <<
"out of range for " << it->var.name <<
' '
394 << pred <<
" -> " << amapit->first
399 *(inst_before_this->second.source),
402 msg.
warning() <<
"Local variable table: didn't find initializing "
403 <<
"store for predecessor of bytecode at address "
404 << amapit->first <<
" ("
405 << amapit->second.predecessors.size()
406 <<
" predecessors)" << msg.
eom;
407 throw "local variable table: unexpected live ranges";
415 if(pred_var->var.name!=it->var.name ||
416 pred_var->var.descriptor!=it->var.descriptor)
421 msg.
warning() <<
"Local variable table: ignoring flow from "
422 <<
"clashing variable for "
423 << it->var.name <<
' ' << pred <<
" -> "
429 predecessor_map[&*it].insert(pred_var);
436 it->var.length+=(it->var.start_pc-new_start_pc);
437 it->var.start_pc=new_start_pc;
449 const std::set<local_variable_with_holest *> &merge_vars,
455 std::numeric_limits<java_bytecode_convert_methodt::method_offsett>::max();
456 for(
auto v : merge_vars)
458 if(v->var.start_pc<first_pc)
459 first_pc=v->var.start_pc;
462 std::vector<java_bytecode_convert_methodt::method_offsett>
463 candidate_dominators;
464 for(
auto v : merge_vars)
466 const auto &dominator_nodeidx=
468 const auto &this_var_doms=
469 dominator_analysis.
cfg[dominator_nodeidx].dominators;
470 for(
const auto this_var_dom : this_var_doms)
471 if(this_var_dom<=first_pc)
472 candidate_dominators.push_back(this_var_dom);
474 std::sort(candidate_dominators.begin(), candidate_dominators.end());
479 for(
auto domit=candidate_dominators.rbegin(),
480 domitend=candidate_dominators.rend();
484 std::size_t repeats = 0;
486 while(domit!=domitend && *domit==dom)
491 INVARIANT(repeats <= merge_vars.size(),
"out of bounds");
492 if(repeats==merge_vars.size())
496 throw "variable live ranges with no common dominator?";
509 const std::set<local_variable_with_holest *> &merge_vars,
512 std::vector<local_variable_with_holest *> sorted_by_startpc(
513 merge_vars.begin(), merge_vars.end());
514 std::sort(sorted_by_startpc.begin(), sorted_by_startpc.end(),
lt_startpc);
519 expanded_live_range_start,
520 sorted_by_startpc[0]->var.start_pc);
521 for(
auto it = std::next(sorted_by_startpc.begin());
522 it != sorted_by_startpc.end();
525 auto &local_var = (*std::prev(it))->var;
527 merge_into, local_var.start_pc + local_var.length, (*it)->
var.
start_pc);
541 const std::set<local_variable_with_holest *> &merge_vars,
543 std::ostream &debug_out)
549 const auto found_dominator =
559 for(
auto v : merge_vars)
561 if(v->var.start_pc+v->var.length>last_pc)
562 last_pc=v->var.start_pc+v->var.length;
567 merge_into.
var.
length=last_pc-found_dominator;
570 debug_out <<
"Merged " << merge_vars.size() <<
" variables named "
571 << merge_into.
var.
name <<
"; new live range "
579 for(
auto &v : merge_vars)
598 local_variable_table_with_holest::iterator firstvar,
599 local_variable_table_with_holest::iterator varlimit,
606 std::vector<local_variable_with_holest *> live_variable_at_address;
616 live_variable_at_address,
625 for(
auto &kv : predecessor_map)
627 std::set<local_variable_with_holest *> closed_preds;
629 kv.second=std::move(closed_preds);
634 std::vector<local_variable_with_holest *> topsorted_vars;
635 for(
auto it=firstvar, itend=varlimit; it!=itend; ++it)
636 topsorted_vars.push_back(&*it);
638 std::sort(topsorted_vars.begin(), topsorted_vars.end(), comp);
641 for(
auto merge_into : topsorted_vars)
644 if(merge_into->var.length==0)
647 auto findit=predecessor_map.find(merge_into);
649 if(findit==predecessor_map.end())
652 const auto &merge_vars=findit->second;
653 INVARIANT(merge_vars.size()>=2,
"merging requires at least 2 variables");
656 *merge_into, merge_vars, dominator_analysis,
log.
status());
668 local_variable_table_with_holest::iterator &it1,
669 local_variable_table_with_holest::iterator &it2,
670 local_variable_table_with_holest::iterator itend)
679 auto index=it2->var.index;
680 while(it2!=itend && it2->var.index==index)
697 std::sort(vars.begin(), vars.end(),
lt_index);
701 auto it1=vars.begin();
703 auto itend=vars.end();
713 std::vector<local_variable_with_holest> &vars_with_holes)
716 for(
size_t i=0; i<(vars_with_holes.size()-toremove); ++i)
718 auto &v=vars_with_holes[i];
723 if(i!=vars_with_holes.size()-toremove)
724 std::swap(v, vars_with_holes[vars_with_holes.size()-toremove]);
731 vars_with_holes.resize(vars_with_holes.size()-toremove);
747 dominator_analysis(dominator_args);
754 log.
debug() <<
"jcm: setup-local-vars: lvt size "
761 std::vector<local_variable_with_holest> vars_with_holes;
776 catch(
const char *message)
778 log.
warning() <<
"Bytecode -> codet translation error: " << message
780 <<
"This is probably due to an unexpected LVT, "
781 <<
"falling back to translation without LVT" <<
messaget::eom;
796 for(
auto &v : vars_with_holes)
802 log.
debug() <<
"jcm: setup-local-vars: merged variable: idx " << v.var.index
803 <<
" name " << v.var.name <<
" v.var.descriptor '"
804 << v.var.descriptor <<
"' holes " << v.holes.size()
809 const size_t method_name_end = method_name.rfind(
":(");
810 const size_t class_name_end = method_name.rfind(
'.', method_name_end);
812 method_name_end != std::string::npos &&
813 class_name_end != std::string::npos,
814 "A method name has the format class `.` method `:(`signature`)`.");
815 const std::string class_name = method_name.substr(0, class_name_end);
817 const typet t = v.var.signature.has_value()
819 v.var.descriptor, v.var.signature, class_name)
822 std::ostringstream id_oss;
823 id_oss <<
method_id <<
"::" << v.var.start_pc <<
"::" << v.var.name;
826 result.
set(ID_C_base_name, v.var.name);
832 result, v.var.start_pc, v.var.length,
false, std::move(v.holes));
835 symbolt new_symbol{identifier, t, ID_java};
836 new_symbol.base_name=v.var.name;
837 new_symbol.pretty_name=
id2string(identifier).substr(6, std::string::npos);
838 new_symbol.is_file_local=
true;
839 new_symbol.is_thread_local=
true;
840 new_symbol.is_lvalue=
true;
859 size_t length=var.length;
860 if(address>=start_pc && address<(start_pc+length))
862 bool found_hole=
false;
863 for(
auto &hole : var.holes)
864 if(address>=hole.start_pc && address<(hole.start_pc+hole.length))
878 var_list.emplace_back(
880 return var_list.back();
struct bytecode_infot const bytecode_info[]
entryt & at(const goto_programt::const_targett &t)
entryt get_node_index(const goto_programt::const_targett &program_point) const
Get the graph node index for program_point.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
A generic directed graph with a parametric node type.
node_indext add_node(arguments &&... values)
void add_edge(node_indext a, node_indext b)
void set(const irep_idt &name, const irep_idt &value)
method_offsett slots_for_parameters
Number of local variable slots used by the JVM to pass parameters upon invocation of the method under...
std::vector< variablet > variablest
void setup_local_variables(const methodt &m, const address_mapt &amap)
See find_initializers_for_slot above for more detail.
const variablet & find_variable_for_slot(size_t address, variablest &var_list)
See above.
void find_initializers(local_variable_table_with_holest &vars, const address_mapt &amap, const java_cfg_dominatorst &doms)
See find_initializers_for_slot above for more detail.
std::vector< local_variable_with_holest > local_variable_table_with_holest
void find_initializers_for_slot(local_variable_table_with_holest::iterator firstvar, local_variable_table_with_holest::iterator varlimit, const address_mapt &amap, const java_cfg_dominatorst &doms)
Given a sequence of users of the same local variable slot, this figures out which ones are related by...
expanding_vectort< variablest > variables
bool is_parameter(const local_variablet &v)
Returns true iff the slot index of the local variable of a method (coming from the LVT) is a paramete...
std::map< method_offsett, converted_instructiont > address_mapt
symbol_table_baset & symbol_table
irep_idt method_id
Fully qualified name of the method under translation.
Class that provides messages with a built-in verbosity 'level'.
mstreamt & warning() const
mstreamt & status() const
message_handlert & get_message_handler()
Expression to hold a symbol (variable)
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
The type of an expression, extends irept.
const std::string & id2string(const irep_idt &d)
JAVA Bytecode Language Conversion.
static void maybe_add_hole(local_variable_with_holest &var, java_bytecode_convert_methodt::method_offsett from, java_bytecode_convert_methodt::method_offsett to)
See above.
std::map< local_variable_with_holest *, std::set< local_variable_with_holest * > > predecessor_mapt
static bool lt_index(const local_variable_with_holest &a, const local_variable_with_holest &b)
java_bytecode_convert_methodt::java_cfg_dominatorst java_cfg_dominatorst
static void populate_variable_address_map(local_variable_table_with_holest::iterator firstvar, local_variable_table_with_holest::iterator varlimit, std::vector< local_variable_with_holest * > &live_variable_at_address)
See above.
static bool is_store_to_slot(const java_bytecode_convert_methodt::instructiont &inst, unsigned slotidx)
See above.
static void populate_predecessor_map(local_variable_table_with_holest::iterator firstvar, local_variable_table_with_holest::iterator varlimit, const std::vector< local_variable_with_holest * > &live_variable_at_address, const address_mapt &amap, predecessor_mapt &predecessor_map, message_handlert &msg_handler)
Populates the predecessor_map with a graph from local variable table entries to their predecessors (t...
static void populate_live_range_holes(local_variable_with_holest &merge_into, const std::set< local_variable_with_holest * > &merge_vars, java_bytecode_convert_methodt::method_offsett expanded_live_range_start)
See above.
static bool lt_startpc(const local_variable_with_holest *a, const local_variable_with_holest *b)
java_bytecode_convert_methodt::address_mapt address_mapt
java_bytecode_convert_methodt::holet holet
static void cleanup_var_table(std::vector< local_variable_with_holest > &vars_with_holes)
See above.
static java_bytecode_convert_methodt::method_offsett get_common_dominator(const std::set< local_variable_with_holest * > &merge_vars, const java_cfg_dominatorst &dominator_analysis)
Used to find out where to put a variable declaration that subsumes several variable live ranges.
java_bytecode_convert_methodt::local_variable_with_holest local_variable_with_holest
static void walk_to_next_index(local_variable_table_with_holest::iterator &it1, local_variable_table_with_holest::iterator &it2, local_variable_table_with_holest::iterator itend)
Walk a vector, a contiguous block of entries with equal slot index at a time.
static void merge_variable_table_entries(local_variable_with_holest &merge_into, const std::set< local_variable_with_holest * > &merge_vars, const java_cfg_dominatorst &dominator_analysis, std::ostream &debug_out)
See above.
java_bytecode_convert_methodt::local_variable_table_with_holest local_variable_table_with_holest
static void gather_transitive_predecessors(local_variable_with_holest *start, const predecessor_mapt &predecessor_map, std::set< local_variable_with_holest * > &result)
See above.
std::optional< typet > java_type_from_string(const std::string &src, const std::string &class_name_prefix)
Transforms a string representation of a Java type into an internal type representation thereof.
std::optional< typet > java_type_from_string_with_exception(const std::string &descriptor, const std::optional< std::string > &signature, const std::string &class_name)
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
unsigned safe_string2unsigned(const std::string &str, int base)
bool operator()(local_variable_with_holest *a, local_variable_with_holest *b) const
is_predecessor_oft(const predecessor_mapt &_order)
const predecessor_mapt & order
std::vector< holet > holes
std::pair< const methodt &, const address_mapt & > method_with_amap
local_variable_tablet local_variable_table
procedure_local_cfg_baset()
java_bytecode_convert_methodt::method_with_amapt method_with_amapt
nodet & get_node(const java_bytecode_convert_methodt::method_offsett &instruction)
java_bytecode_convert_methodt::method_offsett get_node_index(const java_bytecode_convert_methodt::method_offsett &instruction) const
std::map< java_bytecode_convert_methodt::method_offsett, java_bytecode_convert_methodt::method_offsett > entry_mapt
static bool nodes_empty(const method_with_amapt &args)
const nodet & get_node(const java_bytecode_convert_methodt::method_offsett &instruction) const
static java_bytecode_convert_methodt::method_offsett get_first_node(const method_with_amapt &args)
grapht< cfg_base_nodet< T, java_bytecode_convert_methodt::method_offsett > > base_grapht
void operator()(const method_with_amapt &args)
static java_bytecode_convert_methodt::method_offsett get_last_node(const method_with_amapt &args)