[Compiler/Decompiler] Better support for Bitfield and Enum types (#374)

* compiler fixes, a decent amount of decompiler stuff is working too

* more support in decompiler, fix some casts

* decompile static data too
This commit is contained in:
water111
2021-04-22 19:08:58 -04:00
committed by GitHub
parent 26accb8714
commit 060b125324
35 changed files with 561 additions and 283 deletions
+24 -3
View File
@@ -380,9 +380,10 @@ std::vector<Form*> compact_nested_logiors(GenericElement* input, const Env&) {
return result;
}
} // namespace
/*!
* If this could be an integer constant, figure out what the value is.
* TODO move this somewhere more general.
*/
std::optional<u64> get_goal_integer_constant(Form* in, const Env&) {
auto as_atom = form_as_atom(in);
@@ -405,8 +406,6 @@ std::optional<u64> get_goal_integer_constant(Form* in, const Env&) {
return {};
}
} // namespace
BitFieldDef BitFieldDef::from_constant(const BitFieldConstantDef& constant, FormPool& pool) {
BitFieldDef bfd;
bfd.field_name = constant.field_name;
@@ -474,4 +473,26 @@ Form* cast_to_bitfield(const BitFieldType* type_info,
return pool.alloc_single_element_form<CastElement>(nullptr, typespec, in);
}
Form* cast_to_bitfield_enum(const EnumType* type_info,
const TypeSpec& typespec,
FormPool& pool,
const Env& env,
Form* in) {
auto integer = get_goal_integer_constant(strip_int_or_uint_cast(in), env);
if (integer) {
auto elts =
decompile_bitfield_enum_from_int(TypeSpec(type_info->get_name()), env.dts->ts, *integer);
auto oper = GenericOperator::make_function(
pool.alloc_single_element_form<ConstantTokenElement>(nullptr, type_info->get_name()));
std::vector<Form*> form_elts;
for (auto& x : elts) {
form_elts.push_back(pool.alloc_single_element_form<ConstantTokenElement>(nullptr, x));
}
return pool.alloc_single_element_form<GenericElement>(nullptr, oper, form_elts);
} else {
// all failed, just return whatever.
return pool.alloc_single_element_form<CastElement>(nullptr, typespec, in);
}
}
} // namespace decompiler