268 Commits

Author SHA1 Message Date
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
Pietro Fezzardi 53c39d32f2 DecompileFunction.cpp: drop support for allocas
Since when we added the -exit-ssa pass, allocas 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
Kacper Kołodziej c61c59c474 Handle StringLiteral tag in DecompileFunction 2023-04-04 09:31:10 +02:00
Ivan Krysak 47213e1669 Backend: remove an obsolete log file 2023-03-15 10:28:24 +01:00
Ivan Krysak 85fcd92f0e Backend: reword a comment 2023-03-15 10:28:24 +01:00
Ivan Krysak c2f0fc19e5 ABI: FunctionType.h -> FunctionType/Layout.h 2023-03-15 10:28:24 +01:00
Djordje Todorovic 5482083e79 DecompileFunction.cpp: print debug info in PTML 2023-03-08 09:57:48 +01:00
Pietro Fezzardi f3e466c989 DecompileFunction.cpp: fix format for int literals 2023-03-01 12:22:35 +01:00
Kacper Kołodziej 5b94a5a649 HelpersToHeaders: skip unnecessary helpers 2023-02-07 17:43:05 +01:00
Pietro Fezzardi 97b23935de Handle FunctionTags::Exceptional
This is necessary for e.g. the abort function.
2023-01-17 11:14:47 +01:00
Kacper Kołodziej 568bd24900 Add UnaryMinus and BinaryNot 2023-01-09 15:01:16 +01:00
Andrea Gussoni 80ea234167 Fix ContinueNode emission 2023-01-05 16:33:21 +00:00
Pietro Fezzardi e9d84264ff Reduce the number of emitted local variables in C
This commit does various things oriented at reducing the number of local
variables emitted in C:
- MarkAssignments now know that @Copy and @Assign involving
  @LocalVariable only have side effects that affect the local variable
  itself; this enables to reduce the number of times we're forced to
  emit a local variable due to interfering side effects
- Drop the @AssignmentMarker FunctionTag; AddAssignmentMarkerPass now
  doesn't emit @AssignmentMarker anymore; instead it emits groups of
  @LocalVariable, @Copy, and @Assign, which benefit from the previous
  point
- Drop 2 MarkAssignments::Reasons: HasManyUses and HasUsesOutsideOfBB;
  both these have now been aggregated into the AlwaysAssign reason for
  simplicity, representing all reasons non involving side effects
- Update BeautifyGHAST and how it reasons about side effects when
  beautifying; before this commit it used @AssignmentMarker, now it
  looks at @Assign
- Simplify ExitSSA; before this commit it was trying hard to be smart on
  where it emitted the store instructions representing the incoming
  values of the PHI that was being destroyed; this seemed smart when we
  originally did it but it generated C code that was not really better
  to read, so this useless complexity is finally gone
2022-12-24 02:47:10 +01:00
Pietro Fezzardi c201990e5b Print stack variable at beginning of function body 2022-12-24 02:44:38 +01:00
Massimo Fioravanti 87aef3fbad revng-c now uses Model accessors 2022-12-12 11:35:52 +01:00
Pietro Fezzardi de0b22a842 DecompileFunction.cpp: avoid useless newline 2022-12-05 15:59:42 +01:00
Pietro Fezzardi e1d8d4ff9e DecompileFunction.cpp: fix hexLiteral
formatv("{0:x}", Int) was printing the address of the integer, not the
integer itself.
2022-12-02 14:40:09 +01:00
Kacper Kołodziej 83ab07d7fb Enable printing integers as bool, hex or char
This commit adds a new pass, PrettyIntFormatting, that injects calls to
decorator functions print_hex, print_char, and print_bool around
llvm::ConstantInt in various situations.

It also updates the rest of passes of the decompilation pipelin to
understand these new decorator functions and to properly emit decorated
integer literals in the decompiled C code.
2022-12-02 10:08:00 +01:00
Pietro Fezzardi ee0dc97464 Uniform printing return types of functions 2022-11-30 18:16:13 +01:00
Pietro Fezzardi 389a184fc2 Handle CABIFunctionTypes returning struct types 2022-11-30 18:16:13 +01:00
Alessandro Di Federico 800b8e6f16 DecompileFunction: handle string literals
We now emit global variables that are NULL-terminated strings inline.
2022-11-30 18:16:13 +01:00
Pietro Fezzardi d34abf3706 DecompileFunction.cpp: fix ModelGEP traversal
handleSpecialFunction was still traversing Qualifiers in the wrong
order.
2022-11-30 18:16:13 +01:00
Pietro Fezzardi 2ab5b5d9bf Fix handling of UndefValue and PoisonValue 2022-11-30 18:16:13 +01:00