|
| 1 | +#include <algorithm> |
| 2 | + |
| 3 | +#include <cegis/instrument/instrument_var_ops.h> |
| 4 | +#include <cegis/refactor/instructionset/processor_types.h> |
| 5 | +#include <cegis/refactor/instructionset/processor_symbols.h> |
| 6 | +#include <cegis/refactor/instructionset/cegis_instruction_factory.h> |
| 7 | + |
| 8 | +namespace |
| 9 | +{ |
| 10 | +class arithmetic_assignt |
| 11 | +{ |
| 12 | + const irep_idt id; |
| 13 | + const typet type; |
| 14 | +public: |
| 15 | + arithmetic_assignt(const irep_idt &id, const typet &type) : |
| 16 | + id(id), type(type) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + goto_programt::targett operator()(const symbol_tablet &st, |
| 21 | + const std::string &func_name, goto_programt &body, |
| 22 | + goto_programt::targett pos) const |
| 23 | + { |
| 24 | + pos->type=goto_program_instruction_typet::ASSIGN; |
| 25 | + pos->source_location=default_cegis_source_location(); |
| 26 | + const dereference_exprt lhs(cegis_operand(st, func_name, type, 1)); |
| 27 | + const dereference_exprt rhs(cegis_operand(st, func_name, type, 2)); |
| 28 | + const binary_exprt result(lhs, id, rhs); |
| 29 | + pos->code=code_assignt(cegis_operand(st, func_name, type, 0), result); |
| 30 | + return pos; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +class arithmetic_instructionst |
| 35 | +{ |
| 36 | + const typet &type; |
| 37 | + const instruction_descriptiont::typest sig; |
| 38 | +public: |
| 39 | + arithmetic_instructionst(const typet &type) : |
| 40 | + type(type), sig( { type, type, type }) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + instruction_descriptiont plus() const |
| 45 | + { |
| 46 | + return instruction_descriptiont(sig, arithmetic_assignt(ID_plus, type)); |
| 47 | + } |
| 48 | + |
| 49 | + instruction_descriptiont minus() const |
| 50 | + { |
| 51 | + return instruction_descriptiont(sig, arithmetic_assignt(ID_minus, type)); |
| 52 | + } |
| 53 | + |
| 54 | + instruction_descriptiont mult() const |
| 55 | + { |
| 56 | + return instruction_descriptiont(sig, arithmetic_assignt(ID_mult, type)); |
| 57 | + } |
| 58 | + |
| 59 | + instruction_descriptiont div() const |
| 60 | + { |
| 61 | + return instruction_descriptiont(sig, arithmetic_assignt(ID_div, type)); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +void insert( |
| 66 | + std::map<instruction_descriptiont::typest, instruction_descriptionst> &result, |
| 67 | + const instruction_descriptiont &instr) |
| 68 | +{ |
| 69 | + result[instr.signature].push_back(instr); |
| 70 | +} |
| 71 | +} |
| 72 | + |
| 73 | +ordered_instructionst get_instructions_for_types( |
| 74 | + const cegis_operand_datat &signature) |
| 75 | +{ |
| 76 | + ordered_instructionst result; |
| 77 | + for (const cegis_operand_datat::value_type &typeWithSlots : signature) |
| 78 | + { |
| 79 | + const typet &type=typeWithSlots.first; |
| 80 | + if (!is_cegis_primitive(type)) |
| 81 | + assert(!"Class type operand generation not supported."); |
| 82 | + const arithmetic_instructionst arith(type); |
| 83 | + insert(result, arith.plus()); |
| 84 | + insert(result, arith.minus()); |
| 85 | + insert(result, arith.mult()); |
| 86 | + insert(result, arith.div()); |
| 87 | + } |
| 88 | + return result; |
| 89 | +} |
| 90 | + |
| 91 | +instruction_descriptionst::size_type num_instrs( |
| 92 | + const ordered_instructionst &instrs) |
| 93 | +{ |
| 94 | + return std::accumulate(instrs.begin(), instrs.end(), 0, |
| 95 | + [](const size_t count, const ordered_instructionst::value_type &instrs) |
| 96 | + { return count + instrs.second.size();}); |
| 97 | +} |
0 commit comments