Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better c support #1059

Merged
merged 56 commits into from
Dec 12, 2023
Merged

Better c support #1059

merged 56 commits into from
Dec 12, 2023

Conversation

sakehl
Copy link
Contributor

@sakehl sakehl commented Sep 7, 2023

I've added some better C support for several things.

  • Structs
  • Pointers/Arrays
  • As discussed in How to treat pointers & arrays in C backend #1056. I've chosen the "No difference between pointers and arrays" for now, since that has everything working for the cases I was interested in.
  • You can now declare a new array like int a[5], int a[] = {0, 1, 2, 3, 4} or int a[5] = {0, 1, 2, 3, 4}
  • Casts
    • I've added support to cast between integers and floats. Although floats are still eventually encoded as rationals.
  • Malloc / Free
    • malloc only works in this specific format: int *xs = (int *) malloc(5*sizeof(int)). It creates a pointer block with pointer_offset set to 0. In the example, it has a length of 5.
    • Free only works on pointer_blocks which have the pointer_offset set to 0. Furthermore, it requires write permission to the complete pointer_block, which is does not give back.
  • Parallel blocks #omp parallel for
    • (TODO) How it already was working: if you give a loop an iteration contract instead of a regular contract, it is treated as par block. It would be nice if it only did that if you gave the annotation #omp parallel for? Or alternatively, if #omp parallel for is present, it requires an iteration contract.
    • Fixed a bug, where when a loop had declarations and was converted to a par block, those declarations were moved towards a higher scope, which led to internal errors.
  • % in C
    • The % is now working in better correspondence with the c99 compiler. (although to be completely fair, it is compiler/architecture dependent).
    • If they ever change this in Viper, we should as well. See Inconsistent behavior of division viperproject/silver#297
    • For now at least this is working by defining % in C (cmod) as (similar as in the above mentioned issue):
function cmod(a: Int, b: Int ): Int
  requires b != 0
{
  let res == (a % b) in a > 0 || res == 0 ? res : res - abs(b)
}

cmod(36, 7) == 1
cmod(-36, 7) == -1
cmod(36, -7) == -1
cmod(-36, -7) == -1
  • Fixed a bug where inline and pure annotations were not actually processed
  • I've defined some header files
    • Mostly all (un)signed integers are mapped to int
    • Mostly all float types are mapped to float (but actually rational)

I did not make any test cases yet, which I could still make.

In some cases, I really had no clue how to get the correct blame for a node. So I made PanicBlame("TODO") in those cases, any advice on those would be great.

Copy link
Member

@pieter-bos pieter-bos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, nice work! I am ok with the general structure of the changes and I've tried to provide some useful feedback. If there's particular areas you want me to have a more detailed look at feel free to let me know.

src/col/vct/col/ast/Node.scala Outdated Show resolved Hide resolved
src/col/vct/col/resolve/lang/C.scala Show resolved Hide resolved
src/rewrite/vct/rewrite/lang/LangCToCol.scala Outdated Show resolved Hide resolved
src/rewrite/vct/rewrite/lang/LangCToCol.scala Outdated Show resolved Hide resolved
src/rewrite/vct/rewrite/lang/LangCToCol.scala Outdated Show resolved Hide resolved
src/rewrite/vct/rewrite/lang/LangCToCol.scala Outdated Show resolved Hide resolved
src/rewrite/vct/rewrite/lang/LangTypesToCol.scala Outdated Show resolved Hide resolved
Copy link
Contributor Author

@sakehl sakehl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am finished with everything now. And I tried to comment on some stuff you wrote or fix what you were saying.

I've tried to include tests for all the functionality I wrote. The bigger things in the files. The potential errors/verification failures as smaller tests in CSpec.scala.

How would you want me to proceed @pieter-bos ?

src/rewrite/vct/rewrite/CollectLocalDeclarations.scala Outdated Show resolved Hide resolved
src/rewrite/vct/rewrite/PrettifyBlocks.scala Outdated Show resolved Hide resolved
examples/concepts/c/math.c Outdated Show resolved Hide resolved
@pieter-bos
Copy link
Member

Very nice work! Let's get it merged (I'm just running the test suite now, since GitHub doesn't do that for forks)

@pieter-bos pieter-bos merged commit c326aa0 into utwente-fmt:dev Dec 12, 2023
@sakehl
Copy link
Contributor Author

sakehl commented Dec 13, 2023

Awesome, glad that it's merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants