229 Commits

Author SHA1 Message Date
Pietro Fezzardi 48a708c07c Support printing NULL in decompiled C code 2023-10-19 16:18:41 +02:00
Pietro Fezzardi a97287cef3 ModelToPTMLTypeHelpers: refactor APIs
This commit reorders the argument passed to various `printDeclaration`
and `printDefinition` helper functions, to make them more uniform across
each other, and to accept the same arguments in the same order.
2023-10-18 15:19:53 +02:00
Alessandro Di Federico 4fe4d05895 CCodeGenerator::ParentPrototype: drop Parent 2023-10-06 17:14:29 +02:00
Alessandro Di Federico 2a7e06df1f printFunctionPrototype: add newline after ABI
This commit forces getLocationAttribute's argument to be false.
2023-10-06 17:14:29 +02:00
Alessandro Di Federico 6141717440 CCodeGenerator: emit comment after noreturn calls 2023-10-06 17:14:27 +02:00
Alessandro Di Federico 9f3b832009 CCodeGenerator: add space in struct initializers 2023-10-05 19:18:38 +02:00
Alessandro Di Federico cb8aa3d929 Adopt .prototype()
Also adopt `QualifiedType::getFunctionType` where appropriate in order
to unwrap typedefs.
2023-10-05 19:18:38 +02:00
Pietro Fezzardi 8d9f07d7a7 Change indentation of all decompiled C code
This commit changes the indentation of all the C code we emit, from 4
whitespaces to only 2 whitespaces.
2023-09-21 15:17:44 +02:00
Pietro Fezzardi 01f338be29 Add progress bars to DecompileFunction.cpp 2023-09-20 16:36:21 +02:00
Alessandro Di Federico f3d6bbb047 s/ThePTMLCBuilder/B/g 2023-08-23 16:40:54 +02:00
Alessandro Di Federico 489dddd0e7 TupleTreeReference::isValid is for assertions only 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 330d2b6cc7 Model: rework how we name things
This commit:

* Introduces `_` as a prefix for all non-user entities we emit in
  decompiled code.
  Also, some names have been changed to be more concise.
  Specifically, the following entities have changed:
  `_ENUM_UNDERLYING`, `_ABI`, `_REG`, `_padding_at_`,
  `_artificial_struct_`, `_artificial_wrapper_`, `_stack`,
  `_break_from_loop_`, `_var_`, `_stack_arguments`,
  `_artificial_struct_returned_`, `_enum_max_value_`.
* Introduce _PACKED for `__attribute__((packed))`.
* `EnumEntry` name: drop the `EnumType` name prefix.
2023-08-23 16:37:39 +02:00
Pietro Fezzardi dfbb9a15b2 Backend: declare local variables for aggregates 2023-08-18 15:48:55 +02:00
Giacomo Vercesi 2d8e11fe74 DecompileFunction: use scope-location
Change debug locations to use the new `scope-location` tag to allow
switching between C and ASM code.
2023-07-31 11:25:45 +02:00
Ivan Krysak 4a32781868 Backend: emit function comment 2023-07-21 04:58:51 +00:00
Ivan Krysak e0b956b1c2 Model: rework edit path helpers from the ground up 2023-07-21 04:58:50 +00:00
Ivan Krysak fe0531868c Use Container::contains() where appropriate 2023-07-05 06:07:09 +00:00
Pietro Fezzardi 1b4079b496 DecompileFunction.cpp: HTML-escape strings in PTML 2023-06-30 10:48:59 +02:00
Pietro Fezzardi dd3836112d DecompileFunction.cpp: handle enum comparisons 2023-06-30 10:48:59 +02:00
Pietro Fezzardi 6acd265c5a Add revng helpers to decompile llvm::UndefValue
Before this commit, we used to decomple UndefValue to the literal 0.

This was causing problems in some cases, such as switch(0 /*undef*/)
where clang was issuing a warning that was impossible do disable without
turning off -Wall alltogether, which is undesirable.

This commit introduces a set of helper functions in
revng-primitive-types.h, that are now used to avoid emitting undef as
constant.
This is more semantically meaningful when looking at the decompiled
code, and it has the nice side effect that it silences the clang
warnings mentioned above.
2023-06-30 10:48:59 +02:00
Pietro Fezzardi e988932442 DecompileFunction.cpp: fix 128 bit literals 2023-06-30 10:48:59 +02:00
Pietro Fezzardi e2791443fb Remove extractvalue instructions in decompilation
Now extractvalue instruction are replaced by dedicated
OpaqueExtractValue custom opcode, that prevents LLVM from doing strange
things with extractvalues during optimizations (such as e.g. sinking).

This is important since extractvalue instructions and struct-typed
values in general in our LLVM IR are not real first-class citizens, but
only a byproduct of the binary lifting process, and they actually
represent bundles of registers that are returned from isolated
functions.
2023-06-30 10:48:59 +02:00
Pietro Fezzardi 220a7718e8 Fix getModelGEPToken 2023-06-30 10:48:59 +02:00
Pietro Fezzardi 0dd6e75800 Fix flattenTypedefsIgnoringConst 2023-06-30 10:48:59 +02:00
Pietro Fezzardi f7dfd1e4ca Backend: drop duplicated do-while condition 2023-06-23 16:39:03 +02:00
Pietro Fezzardi 3016c2ad41 MakeModelGEPPass: enable operator [] on pointers
This commit enables to emit accesses with the square bracket array
access operator on pointers.

This is accomplished by adding an additional mandatory argument to
ModelGEP (AND NOT to ModelGEPRef) to represent this case.

MakeModelGEPPass is updated to take this into account, together with all
the other passes that handle ModelGEPs.
2023-06-20 18:15:24 +02:00
Pietro Fezzardi 6bf0de84ce Recompile tests with -Wimplicitly-unsigned-literal
This warning was disabled in the past. This commit handles the cas
properly and re-enables the warning in recompilation tests.
2023-06-14 16:06:11 +02:00
Pietro Fezzardi ea3c6216ad DecompileFunction.cpp: ban floats only on ICmp
Before this commit we were asserting to never find float types on any
binary operator, which was way too strict.
2023-06-13 17:05:41 +02:00
Valentina Sona 8409c435a6 Add decompilation support for (x != 0) -> (x)
Not equal is handled separatedly during the decompilation process in
order to add the appropriate cast.

Currently, the code only triggers when the not equal is the outermost
operation in the expression - case for which there are no tests in the
test suite.
2023-06-12 14:17:34 +02:00
Valentina Sona 8dfd7c2d96 Add custom opcode BooleanNot 2023-06-12 14:17:34 +02:00
Djordje Todorovic 286eb4d734 c-backend: Introduce PTMLCBuilder
This will be used to generate plain C.
2023-06-09 15:40:23 +02:00
Pietro Fezzardi a263270858 Remove redundant casts on comparisons 2023-06-09 09:28:40 +02:00
Pietro Fezzardi faa1fd5169 Add missing whitespace before } in decompiled C 2023-06-09 09:23:01 +02:00
Pietro Fezzardi b4ac5b59c6 Drop useless \n at after StructInitializers in C 2023-06-09 09:22:59 +02:00
Pietro Fezzardi 412dd85556 DecompileFunction.cpp: avoid useless casts in ICmp 2023-05-12 16:12:48 +02:00
Pietro Fezzardi 9b62b751ee DecompileFunction.cpp: refactor emitting constants 2023-05-12 15:21:22 +02:00
Giacomo Vercesi a4ad571e61 rcc: Fix typos
Fix the typos detected by `codespell`
2023-05-11 10:04:32 +02:00
Djordje Todorovic ca7bc2e442 c-backend: Generate Inline Types in PTML
In ModelToHeader generate Inline Types in PTML by handling
Structs, Unions and Enums.

In addition, during the ModelToHeader we avoid producing
Structs that describe stacks, and in the DecompileFunction we
produce the definition of it inline if it is safe (referenced
only once).
2023-05-03 13:13:28 +02:00
Alessandro Di Federico ff6352bab4 Add missing headers 2023-04-28 13:54:12 +02:00
Pietro Fezzardi 503bfd8056 DecompileFunction: drop wrong constant PTML tags
These were a leftover of when `UnaryMinus` and `BinaryNot` were thought
of as only applied to constants, but this is not the case anymore.
2023-04-28 08:16:46 +02:00
Alessandro Di Federico 2bacc7ab68 DecompileFunction: fix handling of {and,or} i1
We used to emit bitwise `&` and `|` but, given that we emit `i1` as
`bool`, it's more appropriate to use `&&` and `||`.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico b09526a5b4 DecompileFunction: drop redundant handling of IntToPtr
They were already handle above since `isa<IntToPtr>` implies
`isa<CastInst>`. Same for `PtrToInt`.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico b73bb90d0c Handle the freeze instruction 2023-04-14 14:54:14 +02:00
Alessandro Di Federico 2deb057364 DecompileFunction: introduce buildInvalid
Factor code to emit poison and undef values.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico 3d5e0d3106 DecompileFunction: update handling of strings
Simplify the emission of string literas after the introduction of opaque
pointers, which basically take out of the equation GEP ConstantExpr.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico b9dcb977bd Adopt CallBase::arg_size 2023-04-14 14:54:14 +02:00
Alessandro Di Federico dd383076ab Drop arg_operands in favor of args 2023-04-14 14:54:14 +02:00
Alessandro Di Federico ab089a6d3a Adopt clang-format 16 2023-04-14 14:54:14 +02:00
Pietro Fezzardi a37dc1cd77 DecompileFunction.cpp: statement-based emission
This commit refactors the C Backend as follows.
- The emission of basic blocks is now statement based.
  There are some instructions that are handled as statements.
  Those instructions are the points where the codegen starts from when
  emitting C.
  Conversely, all the other instructions are initially ignored when
  iterating on instructions in a basic block, and are only reached when
  traversing the dataflow that originates from statements.
- The TokenMap, that originally included all the string representing all
  the instructions, is now used only to store the names of funciton
  arguments and instructions representing local variables.
- Starting from each statement-like instruction, the C codegen is driven
  by a traversal of the dataflow of the operands. The dataflow is
  traversed recursively with the `getToken` method, materializing
  strings on the fly until the visit reaches "leaves" i.e. instruction
  that represent local variables, constants, or function arguments.

Adopting this approach allowed to reduce the peak memory consumption of
the backend, as well as making it faster on our benchmarks.
2023-04-06 14:56:14 +02:00
Pietro Fezzardi fa2d721c81 DecompileFunction.cpp: drop support for load/store
Since when we added the -exit-ssa pass, loads and stores are never
supposed to reach the C backend anymore.
This commit drops the code that supports them, that was effectively dead
code since a quite long time.
2023-04-04 09:33:22 +02:00