Skip to content

Commit c8821b2

Browse files
Allow to remove instanceof when remove exceptions
This removes the need to always run remove instanceof after remove exceptions and allows remove_instanceof to be moved to a function-pass
1 parent 94b7658 commit c8821b2

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/goto-programs/remove_exceptions.cpp

+21-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Date: December 2016
1212
/// Remove exception handling
1313

1414
#include "remove_exceptions.h"
15+
#include "remove_instanceof.h"
1516

1617
#ifdef DEBUG
1718
#include <iostream>
@@ -43,8 +44,9 @@ Date: December 2016
4344
/// (in instruction->code.exception_list()) and a corresponding GOTO program
4445
/// target for each (in instruction->targets).
4546
/// Thrown instructions are currently always matched to tags using
46-
/// java_instanceof, so a language frontend wanting to use this class
47-
/// must use exceptions with a Java-compatible structure.
47+
/// java_instanceof, optionally lowered to a check on the @class_identifier
48+
/// field, so a language frontend wanting to use this class must use
49+
/// exceptions with a Java-compatible structure.
4850
///
4951
/// CATCH with a code_pop_catcht operand terminates a try-block begun by
5052
/// a code_push_catcht. At present the try block consists of the instructions
@@ -72,9 +74,6 @@ Date: December 2016
7274
/// instructions copy back to an ordinary local variable (or other expression)
7375
/// and set \#exception_value back to null, indicating the exception has been
7476
/// caught and normal control flow resumed.
75-
///
76-
/// Note that remove_exceptions introduces java_instanceof comparisons at
77-
/// present, so a remove_instanceof may be necessary after it completes.
7877
class remove_exceptionst
7978
{
8079
typedef std::vector<std::pair<
@@ -84,9 +83,11 @@ class remove_exceptionst
8483
public:
8584
explicit remove_exceptionst(
8685
symbol_tablet &_symbol_table,
87-
std::map<irep_idt, std::set<irep_idt>> &_exceptions_map):
88-
symbol_table(_symbol_table),
89-
exceptions_map(_exceptions_map)
86+
std::map<irep_idt, std::set<irep_idt>> &_exceptions_map,
87+
bool remove_added_instanceof)
88+
: symbol_table(_symbol_table),
89+
exceptions_map(_exceptions_map),
90+
remove_added_instanceof(remove_added_instanceof)
9091
{
9192
}
9293

@@ -95,6 +96,7 @@ class remove_exceptionst
9596
protected:
9697
symbol_tablet &symbol_table;
9798
std::map<irep_idt, std::set<irep_idt>> &exceptions_map;
99+
bool remove_added_instanceof;
98100

99101
void add_exceptional_returns(
100102
const irep_idt &function_id,
@@ -346,6 +348,9 @@ void remove_exceptionst::add_exception_dispatch_sequence(
346348

347349
binary_predicate_exprt check(exc_thrown, ID_java_instanceof, expr);
348350
t_exc->guard=check;
351+
352+
if(remove_added_instanceof)
353+
remove_instanceof(t_exc, goto_program, symbol_table);
349354
}
350355
}
351356
}
@@ -575,17 +580,21 @@ void remove_exceptionst::operator()(goto_functionst &goto_functions)
575580
/// removes throws/CATCH-POP/CATCH-PUSH
576581
void remove_exceptions(
577582
symbol_tablet &symbol_table,
578-
goto_functionst &goto_functions)
583+
goto_functionst &goto_functions,
584+
remove_exceptions_typest type)
579585
{
580586
const namespacet ns(symbol_table);
581587
std::map<irep_idt, std::set<irep_idt>> exceptions_map;
582588
uncaught_exceptions(goto_functions, ns, exceptions_map);
583-
remove_exceptionst remove_exceptions(symbol_table, exceptions_map);
589+
remove_exceptionst remove_exceptions(
590+
symbol_table,
591+
exceptions_map,
592+
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
584593
remove_exceptions(goto_functions);
585594
}
586595

587596
/// removes throws/CATCH-POP/CATCH-PUSH
588-
void remove_exceptions(goto_modelt &goto_model)
597+
void remove_exceptions(goto_modelt &goto_model, remove_exceptions_typest type)
589598
{
590-
remove_exceptions(goto_model.symbol_table, goto_model.goto_functions);
599+
remove_exceptions(goto_model.symbol_table, goto_model.goto_functions, type);
591600
}

src/goto-programs/remove_exceptions.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ Date: December 2016
2121
// Removes 'throw x' and CATCH-PUSH/CATCH-POP
2222
// and adds the required instrumentation (GOTOs and assignments)
2323

24-
void remove_exceptions(symbol_tablet &, goto_functionst &);
25-
void remove_exceptions(goto_modelt &);
24+
enum class remove_exceptions_typest
25+
{
26+
REMOVE_ADDED_INSTANCEOF,
27+
DONT_REMOVE_INSTANCEOF,
28+
};
29+
30+
void remove_exceptions(
31+
goto_modelt &goto_model,
32+
remove_exceptions_typest type =
33+
remove_exceptions_typest::DONT_REMOVE_INSTANCEOF);
2634

2735
#endif

src/jbmc/jbmc_parse_options.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ void jbmc_parse_optionst::process_goto_function(
649649
// Remove inline assembler; this needs to happen before
650650
// adding the library.
651651
remove_asm(function, symbol_table);
652+
// Removal of RTTI inspection:
653+
remove_instanceof(function, symbol_table);
652654
}
653655

654656
catch(const char *e)
@@ -692,10 +694,9 @@ bool jbmc_parse_optionst::process_goto_functions(
692694
cmdline.isset("pointer-check"));
693695
// Java virtual functions -> explicit dispatch tables:
694696
remove_virtual_functions(goto_model);
695-
// remove catch and throw (introduces instanceof)
696-
remove_exceptions(goto_model);
697-
// Similar removal of RTTI inspection:
698-
remove_instanceof(goto_model);
697+
// remove catch and throw (introduces instanceof but request it is removed)
698+
remove_exceptions(
699+
goto_model, remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
699700

700701
// instrument library preconditions
701702
instrument_preconditions(goto_model);

0 commit comments

Comments
 (0)