Commit Graph

3168 Commits

Author SHA1 Message Date
Andrea Gussoni afbb3531f5 Name of the BasicBlockNode is now a StringRef
The `Name` field of the `BasicBlockNode` class is now of
`llvm::StringRef` type instead of a simple `std::string`.
2019-04-15 10:03:08 +02:00
Andrea Gussoni 2c3056527d Remove any pointer to BasicBlock inside BBNode
The `BasicBlockNode` does not contain anymore any pointer to the
`llvm::BasicBlock` object that generated the `BasicBlockNode`.

This change is necessary for decoupling completely the `BasicBlockNode`
and `RegionCFG` classes from the LLVM IR, so that we can build up a
`RegionCFG` from any object which implements the `GraphTraits`.

The `RegionCFG` does not contain anymore a map between
`llvm::BasicBlock` and `BasicBlockNode`.
2019-04-15 10:03:08 +02:00
Andrea Gussoni e9f2b4239a Remove backedges refresh 2019-04-14 20:16:51 +02:00
Pietro Fezzardi 743b2e522c Fix switches with empty cases 2019-04-12 18:25:15 +02:00
Andrea Gussoni e8b59c2ff0 Update Backedges instead of refreshing it
Once we compute the set of backedges, they should be fixed and should
not change during the restructuring phase, otherwise we may end-up
inserting loops including outlined node if the information about the
backedges changes completely during the analysis (edge that were
backedges and for which a corresponding SCS was computed become forward
edges, and viceversa, and this behaviors break our assumptions that
outlined node do not take part in loops ever).

This means that we need to keep updated the set of backedges computed at
the beginning instead of recomputing it at each restructuring iteration.

We need to take particular care in updating the information in presence
of the default `SetNode` inserted in case of an entry dispatcher.

The actual implementation is pretty naive, and the update of the
`Backedges` `std::set` is for sure not optimal, we should consider using
a `std::multimap` for it.

We should also consider updating the reverse postorder instead of
recomputing it, but at it seems that recomputing it may cause
sub-optimal restructuring, but no errors should be triggered.
2019-04-12 15:01:49 +02:00
Alessandro Di Federico d50fbe969a Update documentation and add revng. to metadata 2019-04-11 19:51:32 +02:00
Pietro Fezzardi b3da9f0429 Fix wrong assertion on binary ExprNode expressions 2019-04-11 18:14:23 +02:00
Andrea Gussoni 16c3d87a3b Fix check nodes with only then or else branch
Match correctly `IfCheckNodes` which have only the `then` or `else`
branch, that may appear due to conditional nodes blacklisting (the not
identified branch becomes the fallthrough branch).
2019-04-11 17:45:10 +02:00
Alessandro Di Federico d77ad08917 Drop leftover references to revamb 2019-04-11 17:44:39 +02:00
Andrea Gussoni d7e62c8777 New SCS merge step for abnormal retreating.
New SCS merge step for situations in which we have an abnormal edge
exiting from an SCS.

This criterion, during the preprocessing of the metaregions, if an SCS
with an abnormal retreating edge is found (an abnormal retreating is a
retreating edge which target is outside the scope of the current SCS
under analysis), merges the metaregion under analysis with the one which
has been created starting from the backedge.

This preprocessing phase is made in a fixed point fashion.
2019-04-11 16:44:59 +02:00
Andrea Gussoni ad31920b63 Fix for default SetNode membership.
When creating the collapsed node, remember to add in the substitution
phase of the old nodes of the regions (relative to the outer regions),
also any nodes added as default `SetNode`s for any entry dispatcher
inserted.
2019-04-11 16:35:10 +02:00
Andrea Gussoni 0afaa0412c Create new break/continue for each edge
Create a new `break` node for each outgoing edge from the SCS, and a new
`continue` node for each retreating edge.
2019-04-11 16:32:56 +02:00
Andrea Gussoni c3654b5ef7 Additional asserts to verify restructuring
Add an assert which verify that all the targets of the retreating edges
fall inside the SCS under analysis.

Add other asserts to verify that the graphs passed to the comb pass are
acyclic after restructuring.
2019-04-11 16:29:46 +02:00
Andrea Gussoni b1cf76ef4b removeNotReachables removes from MetaRegion
The `removeNotReachables` method now can take care of removing any
reference of the removed nodes from any `MetaRegion` which contains any
reference to it.
2019-04-09 20:12:41 +02:00
Andrea Gussoni 17d6256de7 Add isDAG method for RegionCFG
Added a new method which can be used to check that the `RegionCFG` under
analysis does not contain any cycle.
2019-04-09 20:11:04 +02:00
Andrea Gussoni 5aee9c2711 Fix iterator usage and map access
Now avoiding `predecessors()` iterator invalidation and enforcing
bounds check when using `ClonedMap` during first iteration outlining.

Also avoiding usage of `FirstCandidate` once the real head of a
`RegionCFG` has been elected (only `Head` ptr should be used).
2019-04-09 20:09:12 +02:00
Andrea Gussoni 34913cad29 Fix predecessor iterator invalidation. 2019-04-09 11:55:00 +02:00
Andrea Gussoni 486ddeb4b6 Fix first iteration outling edges movement
In case of `check` nodes some edges could not be attached to the head
dispatcher, if necessary.
2019-04-09 11:38:44 +02:00
Pietro Fezzardi 3f01eed105 Fix setTrue, setFalse, and removePredecessor 2019-04-09 11:27:48 +02:00
Andrea Gussoni eddd6e1924 Change a use of addEdge with moveEdgeTarget 2019-04-09 10:49:10 +02:00
Andrea Gussoni f061095caa Fix first iteration outling
Fix an incorrect iteration over the predecessors of outlined nodes,
which caused iteration invalidation since we are modifying edges in the
meanwhile.
2019-04-09 10:33:57 +02:00
Andrea Gussoni 3ee8eb964b Fix addEdge usage in favor of moveEdgeTarget
Changed an incorrect use of the `addEdge` method in favor of
`moveEdgeTarget` when inserting the `set` nodes.
2019-04-09 09:41:00 +02:00
Pietro Fezzardi 0648b41585 Fix wrong removal of edges 2019-04-08 19:06:27 +02:00
Andrea Gussoni 05fb7a6ccc Fix the conditional nodes blacklisting
We add an additional check for the conditional nodes blacklisting,
meaning that we need to verify that we dominates at least 1 of the
reachable sets for each branch departing from the conditional node.
2019-04-08 09:58:09 +02:00
Pietro Fezzardi ed3d1b1ab8 Properly print bool variables from stdbool.h 2019-04-05 18:52:13 +02:00
Pietro Fezzardi 28a9804143 Print int types from inttypes.h 2019-04-04 17:30:48 +02:00
Pietro Fezzardi 3f7965cf7f Fix emission of global struct types 2019-04-02 16:19:27 +02:00
Pietro Fezzardi 490b5eaef8 Apply clang format 2019-04-02 15:30:17 +02:00
Pietro Fezzardi 9e4c5f0ece Add support for functions returning structs 2019-04-02 11:57:07 +02:00
Pietro Fezzardi 6e710dede1 Remove unused variable 2019-03-29 11:17:21 +01:00
Pietro Fezzardi 8329b822e0 Run PHIASAPAssignmentInfo only on isolated funcs 2019-03-29 11:08:13 +01:00
Pietro Fezzardi a231d013d0 improve lazy serialization of branch conditions 2019-03-29 11:08:13 +01:00
Pietro Fezzardi 9dc97a360e ASTBuildAnalysis: skip over Stores of undef values 2019-03-29 11:08:13 +01:00
Pietro Fezzardi 25d9a8f3d5 Handle Set/Check nodes for loop state variables 2019-03-26 13:51:06 +01:00
Pietro Fezzardi d50f9d0036 Improve prototypes of ASTNode members 2019-03-25 16:08:41 +01:00
Andrea Gussoni 9dc26b6d05 Create SwitchCheckNode
We now match the `IfCheckNode` trees created by the dispatchers
insertion as an unique switch.

To this we introduced a new `SwitchCheckNode` AST type, which cannot
inherith from the `SwitchNode` since the underlying container for the
`case`s is different (the index is saved as an `unsigned`, while instead
in the `SwitchNode` we use a `ConstantInt *`.
2019-03-21 14:25:48 +01:00
Pietro Fezzardi a5175908dc Fix emission of statements after decompiler rework 2019-03-21 11:59:06 +01:00
Pietro Fezzardi fd66e59e25 Fix emission of switches 2019-03-21 10:46:40 +01:00
Pietro Fezzardi 8993ae644d Fix flattening of ExprNode 2019-03-21 10:46:25 +01:00
Pietro Fezzardi 65e1d9e35b Emit switch 2019-03-20 19:37:29 +01:00
Pietro Fezzardi dadbf54631 Rework decompiler 2019-03-20 17:47:04 +01:00
Andrea Gussoni c778004638 Apply short circuit only when possible.
Apply the short circuit simplification only if the basic block
associated with the internal node requires no statement serialization to
happen in the decompiled code. We use the information provided by the
`MarkForSerialization` to know this.
2019-03-20 16:57:41 +01:00
Andrea Gussoni 8aaae2548b IfNode now using ExprNode
The condition inside the `IfNode`s are now represented using a custom
`ExprNode` object, which has a new hierarchy of possible nodes present
in an expression (`not`, `and` and `or`).

Moved the `flipEmptyThen` post-processing inside the beautify pass.

The ownership of the `ExprNode` is kept inside the `ASTTree` object.
This means that during the flattening we also need to transfer these
objects and to adjust the pointers inside the `IfNode`s.
2019-03-20 15:24:28 +01:00
Andrea Gussoni 0cc9ddbdd5 Add llvm::Value condition to SwitchNode.
Add a new private member in the `SwitchNode` to keep track of the
original `llvm::Value` which originated the `SwitchNode`.
2019-03-19 14:52:13 +01:00
Andrea Gussoni 3cf9ffd3ca Move beautify passes in DecompilationPass
Moved a lot of passes that apply optimizations on the AST in the
decompilation pass.

All the optimization functions are now in a dedicated file
(`CDecompilerBeautify.cpp`) and the only function used as interface with
the `CDecompilerAction` pass is the `beautifyAST` function.

This means that now the simplifications will be applied on the already
flattened AST.

Some basic transformations have been left in the `RestructureCFG` pass,
to avoid having an AST of poor quality as output of the pass.
2019-03-14 18:03:36 +01:00
Andrea Gussoni c930d1a7fd Remove pointer to CFGNode from ASTNode.
Remove the pointer to the corresponding `BasicBlockNode` inside each
`ASTNode`.
We now keep directly a pointer to the original `llvm::BasicBlock`.

To do this we need to explicitly mantain some information, like the
emptyness of the node (dummy node in the `RegionCFG`).

Some little changes (like the way we retrieve the CFGNode corresponding
to an ASTNode) in the flattening have been necessary too.
2019-03-14 12:23:25 +01:00
Pietro Fezzardi 77a6601837 Use new MarkForSerialization in CDecompilerAction 2019-03-14 10:06:25 +01:00
Pietro Fezzardi bc6e2fcb01 Add MarkForSerialization MonotoneFramework 2019-03-14 09:50:14 +01:00
Pietro Fezzardi debdb6a9f4 Move PHIASAPAssignmentInfo in separate library 2019-03-14 09:50:14 +01:00
Pietro Fezzardi b4758ae02d Move PHI analysis in PHIASAPAssignmentInfo 2019-03-14 09:49:43 +01:00