Add loopinv tool that supports C to imp conversion for LFP-based loop invariant computation#1
Open
flyingapricot wants to merge 1 commit into
Open
Conversation
… invariant computation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This MR introduces the first half of the loopinv tool, which is to build constraint abstractions of loops in C, and then use it to compute loop invariants via LFP.
Description
The tool currently converts C source code into Imp syntax, which serves as the constraint abstraction representation.
The existing imp tool then processes this Imp code and invokes fixCalc to compute the Least Fixed Point (LFP), which represents the loop invariant relation.
This PR focuses solely on the C-to-Imp conversion and integration with the existing imp tool, it does not yet generate ACSL annotations for Frama-C.
High-Level Approach
C source code is first tokenized by the lexer (Lexer.hs), which breaks the input into meaningful tokens such as keywords, identifiers, operators, and literals.
These tokens are then processed by a recursive descent parser (Parser.hs), which builds an Abstract Syntax Tree (AST) representing the program structure. The AST types are defined in AST.hs and support a subset of C including function definitions, variable declarations, while loops, assignments, and arithmetic/logical expressions.
Once the AST is constructed, the Imp emitter (ImpEmit.hs) traverses the tree and generates equivalent Imp code. The key syntactic differences handled during this translation include converting = to := for assignments, adding do after while conditions, and using ; as a statement separator rather than terminator. The main driver (Main.hs) orchestrates this pipeline: it reads the C file, runs the conversion, writes the generated Imp file, and then invokes the existing imp tool with the +infer flag to compute the LFP via fixCalc.
The output displayed is the raw result from the imp tool, which includes the computed LFP relation. This relation captures all possible input-output behaviors of the loop. For example, showing that a loop variable increases by a certain amount each iteration and what condition holds upon termination.
Usage
cd fixcalc_src make loopinv ./loopinv-bin loopinv/examples/test1.c