Commit Graph

651 Commits

Author SHA1 Message Date
Giacomo Vercesi b042b5495a ModelTypeNames: fix printFunctionPrototype
The prototype of `printFunctionPrototype` has a mis-named argument
following the changes made in `2a7e06df`.
2023-10-23 10:20:54 +02:00
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
Andrea Gussoni 2f4e027ae7 RegionCFGTree: invoke findReachableNodes
Make use of the newly added `findReachableNodes` to identify the
reachable nodes from the `Then` or `Else` when there is no common
`PostDominator`.
2023-10-09 10:34:41 +02:00
Andrea Gussoni deee899ad0 RestructureCFG: improve first iteration outline
Improve the logic of the first iteration outline:
1) The cloning of the nodes involved in the first iteration outlining,
   is now performed with a series of DFS visits from all the late entry
   nodes in the `MetaRegion`, instead of performing a blind clone of all
   the nodes and removing those not needed.
2) Restoring the edges between the cloned nodes has been subject to
   minor changes.
3) The outline nodes are now assigned to a `MetaRegion` using a new
   logic, instead of blindly assigning them to the parent `MetaRegion`.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 4161834bc5 RestructureCFG: improve handling of retreatings
We now explicitly store the retreatings edges, that will eventually be
connected to the `continue` nodes, so that we can explicitly handle
them, without resorting to iterating over the predecessors of the
`EntryNode`, which although correct is a fragile method with respect to
defective situations.

In addition, we check that after the restructuring, all the retreating
edges that we connect to `continue` nodes, do point to the `EntryNode`
before the transformation.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 57099e9e5d MetaRegion: initialize Parent to nullptr 2023-10-09 10:34:41 +02:00
Pietro Fezzardi 3cfad9f3f9 MetaRegion::updateNodes: simplify and add assert 2023-10-09 10:34:41 +02:00
Djordje Todorovic 68400d9b30 Import from LLVM IR to MLIR
- Add PrepareLLVMIRForMLIR Pass.
  This Pass performs massaging of the LLVM IR
  produced by revng so it can be parsed/accepted
  for the LLVM MLIR Dialect.
- Introduce new pipeline branch: llvmir-to-mlir.yml.
- Add LLVMIRToMLIR step.
- Add ImportLLVMToMLIRPipe pipe.
2023-10-02 17:13:55 +02:00
Pietro Fezzardi 08939f2ae8 OpaqueExtractValues: set name for struct type
This commit sets a name for types we use in OpaqueExtractValues.
These are StructTypes that can only be returned from isolated functions
with RawFunctionType prototype on the model, or from helpers that do not
represent isolated functions.

The name is required because when we will converto to MLIR LLVM Dialect,
there are checks in place that forbid unnamed types.
The code re-uses code that is used to print C code, in order to make
sure that the struct names we use for this are the same as those we emit
in C, to ease debugging.

This commit also cleans up the unittest to make it more concise, while
still testing the same things.
2023-10-02 17:13:55 +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
Giacomo Vercesi d95c3b4732 Change extension for Decompiled container
The Decompiled container (which use FunctionStringMap) now uses
`.tar.gz` as the container format, as opposed to YAML. Change the
filename of the containers accordingly.
2023-09-06 11:23:40 +02:00
Alessandro Di Federico f3d6bbb047 s/ThePTMLCBuilder/B/g 2023-08-23 16:40:54 +02:00
Alessandro Di Federico 4e1fed5fa0 ModelToHeader: omit _enum_max_value_* when editing 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
Alessandro Di Federico f81e9d615b dumpModelToHeader: introduce struct for options
This commit also introduces a new option for emitting code right after
the `#include` section.
2023-08-23 16:37:39 +02:00
Alessandro Di Federico a207ef7c44 Minor changes 2023-08-23 16:12:51 +02:00
Pietro Fezzardi 4e32f05aba DLA: mergeNodes now uses ArrayRef argument
This allows for a greater flexibility in its users.
2023-08-21 16:17:36 +02:00
Pietro Fezzardi cac7fee07d LayoutTypeSystemNode: add NonScalar field
This field represents that fact that a node of the DLA Graph is not a
scalar and it comes from the Model.
2023-08-21 16:17:36 +02:00
Pietro Fezzardi 2120dd8108 Replace revng_unreachable with revng_abort
This ensures that we always detect nasty failures even in release
builds.
2023-08-18 15:48:55 +02:00
Pietro Fezzardi dfbb9a15b2 Backend: declare local variables for aggregates 2023-08-18 15:48:55 +02:00
Pietro Fezzardi 755b5176cf Drop unused method LayoutTypeSystem::getNodePtr 2023-07-26 10:41:18 +02:00
Pietro Fezzardi 8b2e3a70df ModelTypeNames: annotate stack args with STACK 2023-07-26 10:01:27 +02:00
Pietro Fezzardi 4bbac8220b PTMLCBuilder: improve const-correctness of methods 2023-07-26 10:01:27 +02:00
Djordje Todorovic 262e579128 ModelToHeader: annotation for enum underlying type 2023-07-25 12:03:57 +02:00
Djordje Todorovic 5c0167f792 Add ImportModelFromC Analysis
This analysis will be used for the following features:

1) Edit Type in the Model
2) Edit Function Prototype in the Model
3) Add Type to the Model

It takes C code as an input, parses it and produces a Model
type that represents the C type.
2023-07-24 11:51:47 +02:00
Djordje Todorovic e6b128c17b ModelToHeader: Print annotation __attributes__
This will be useful for `edit/add type` feature,
since we will be able to specify which ABI describes
a function, from the source code written in C.
2023-07-24 09:17:08 +02:00
Ivan Krysak 4a32781868 Backend: emit function comment 2023-07-21 04:58:51 +00:00
Ivan Krysak 11f07c6708 TypeNames: emit comments when outputting types 2023-07-21 04:58:50 +00:00
Ivan Krysak e0b956b1c2 Model: rework edit path helpers from the ground up 2023-07-21 04:58:50 +00:00
Andrea Gussoni 42f74ad998 RestructureCFG: use nodesBetween helper
Use the `nodesBetween` helper provided by the `GraphAlgorithms` header
instead of the custom `findReachableNodes` implementation.
2023-07-19 16:31:48 +02:00
Andrea Gussoni ac6139bc0f clift: import mlir::clift dialect 2023-07-07 17:48:13 +02:00
Ivan Krysak b0dcc3d9ee Formatting: forbid clang format off 2023-07-05 14:33:06 +02:00
Ivan Krysak fe0531868c Use Container::contains() where appropriate 2023-07-05 06:07:09 +00:00
Ivan Krysak 888e5371eb Formatting: change PenaltyReturnTypeOnItsOwnLine
The new value is 21.
2023-07-05 06:06:07 +00:00
Ivan Krysak 3c584cfabc Formatting: set AllowShortEnumsOnASingleLine
The new value is `false`.
2023-07-05 06:05:04 +00: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 c3d7125dd5 Drop old unused RestoreExtractValuesPass
Now we don't restore the extractvalue instructions anymore, so this
whole pass is dead code.
2023-06-30 10:48:59 +02:00
Andrea Gussoni 2162bd6f4a BeautifyGHAST: change Standard to WhileTrue
Rename the `LoopType` enum element `Standard` to `WhileTrue`, to better
convey the information that in that stage the loop as not been promoted.
2023-06-26 15:23:00 +02:00
Andrea Gussoni 3b07a6fba6 BeautifyGHAST: Introduce the hybrid not simplify
Introduce the hybrid beautifier aiming to simplify double `not`s (one on
the IR side (e.g., a `icmp ne`), and one on the `ExprNode` side on the
`GHAST` in the form of a `NotNode`.

To do this hybrid beautifier, we basically need to do the following:
- Implement a preliminary collection phase which computes which
  `ExprNode`s are affected by which `BasicBlock`.
- Compute the so-called consesus, i.e., verify that for all the
  `ExprNode`s affected by a certain `BasicBlock`, the transformation
  would be beneficial in terms of output. Basically:
    1) That the number of `!(!=)` transformed into `(==)` outscores the
       number of correlated `(!=)` which are transformed into `!(==)`.
    2) That the number of `!(==)` transformed into `(!=)` outscores the
       number of correlated `(==)` which are transformed into `!(!=)`.
- Actually perform the transformation for those situations where the
  consensus agrees.
2023-06-26 15:23:00 +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
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
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 a9eae1c96b Purge remaining traces of InsertValueInst
InsertValue has long been superseded by `struct_initialier`, so we don't
expect it in the IR we work on.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico 79c2ca38f6 Introduce SplitOverflowIntrinsicsPass
This pass splits calls `*.with.overflow*` intrinsics into the a pair of
instructions: the underlying operation and a call to an `Helper`-tagged
function that computes whether such operation overflowed.

For instance, we go from:

    %2 = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %0, i32 %1)
    %3 = extractvalue { i32, i1 } %2, 1
    br i1 %3, label %..., label %...

To:

    %2 = mul i32 %0, %1
    %3 = call i1 @mul_overflow_u32(i32 %0, i32 %1)
    br i1 %3, label %..., label %...

This saves from handling `struct` in the backend.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico 886e1696a2 llvmIntToModelType: adapt to opaque pointers
This commit introduces modelType, which supersedes `llvmIntToModelType`
in order to better handle translation of LLVM types into model types
after introduction of opaque pointer types.  The main differences is
that `modelType` accepts the `Value` (instead of just the `Type`), so it
can better handle `AllocaInst` and `GlobalVariable`, which provide
information about the pointee.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico f6ad90cfd5 WeightTraits<llvm::BasicBlock *>: fix mistake
We were always returning the same value instead of the computed weight.
2023-04-14 14:54:14 +02:00