Support C99 target
First of all, thank you for your work! A transpiler for C is of great value IMO.
I think it's better if we can support C99 in addition to C89 as another target for the following reasons:
-
C99 has feature that is not easy/possible to implement in C89(at least fully conforming C89). For example, VLA, FLA(Flexible array member), restrict... But, C2Y -> C99 is easy for all features(correct me if I am wrong).
Yes we have compiler extension for c89 so that some of C99 features are directly supported. But I think that typically means the compiler itself supports C99 already. And if we add those extension like
__restrict__to C89 mode as I suggested in #379 , what about other compiler that only supports C89? -
C99 is widely supported and widely used, I would say any sane platform supports C99. And i think 99% of the time, we are dealing with those platform.
I agree without your points.
Note that, flexible array member are already implemented
http://cakecc.org/playground.html?code=CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg%2BCgpzdHJ1Y3QgWCB7CiAgICBpbnQgY291bnQ7CiAgICBkb3VibGUgdmFsdWVzW107IC8vIGZsZXhpYmxlIGFycmF5Cn07CgovKgogICAgVGhlIHNpemUgb2YgYSBzdHJ1Y3R1cmUgd2l0aCBhIGZsZXhpYmxlIGFycmF5IG1lbWJlciBpcwogICAgZGV0ZXJtaW5lZCBhcyBpZiB0aGUgZmxleGlibGUgYXJyYXkgbWVtYmVyIHdlcmUgb21pdHRlZCwKICAgIEVYQ0VQVCB0aGF0IGl0IG1heSBoYXZlIG1vcmUgdHJhaWxpbmcgcGFkZGluZyB0aGFuIHRoZQogICAgb21pc3Npb24gd291bGQgaW1wbHkKKi8KCmludCBtYWluKCkgewoKICAgIGludCBuID0gMzsKCiAgICBwcmludGYoInNpemVvZihzdHJ1Y3QgWCkgPSAlZFxcbiIsIChpbnQpIHNpemVvZihzdHJ1Y3QgWCkpOwogICAgcHJpbnRmKCJhbGxvY2F0ZWQgPSAlZFxcbiIsIChpbnQpIHNpemVvZihzdHJ1Y3QgWCkgKyBuICogc2l6ZW9mKGRvdWJsZSkpOwoKICAgIHN0cnVjdCBYKiBwID0gbWFsbG9jKHNpemVvZihzdHJ1Y3QgWCkgKyBuICogc2l6ZW9mKGRvdWJsZSkpOwogICAgaWYgKHAgPT0gTlVMTCkgcmV0dXJuIDA7CgogICAgcC0%2BY291bnQgPSBuOwogICAgcC0%2BdmFsdWVzWzBdID0gMTAuMDsKICAgIHAtPnZhbHVlc1sxXSA9IDIwLjA7CiAgICBwLT52YWx1ZXNbMl0gPSAzMC4wOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcC0%2BY291bnQ7ICsraSkKICAgICAgICBwcmludGYoIiVmXFxuIiwgcC0%2BdmFsdWVzW2ldKTsKCiAgICBmcmVlKHApOwoKICAgIHJldHVybiAwOwp9Cgo%3D&to=-2&options=
and I am planning to add VLA.
But again, I agree with your points I think it can have more settings about input/output.
Restrict will be very easy to do, it will be added soon.
Note that, flexible array member are already implemented
http://cakecc.org/playground.html?code=CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg%2BCgpzdHJ1Y3QgWCB7CiAgICBpbnQgY291bnQ7CiAgICBkb3VibGUgdmFsdWVzW107IC8vIGZsZXhpYmxlIGFycmF5Cn07CgovKgogICAgVGhlIHNpemUgb2YgYSBzdHJ1Y3R1cmUgd2l0aCBhIGZsZXhpYmxlIGFycmF5IG1lbWJlciBpcwogICAgZGV0ZXJtaW5lZCBhcyBpZiB0aGUgZmxleGlibGUgYXJyYXkgbWVtYmVyIHdlcmUgb21pdHRlZCwKICAgIEVYQ0VQVCB0aGF0IGl0IG1heSBoYXZlIG1vcmUgdHJhaWxpbmcgcGFkZGluZyB0aGFuIHRoZQogICAgb21pc3Npb24gd291bGQgaW1wbHkKKi8KCmludCBtYWluKCkgewoKICAgIGludCBuID0gMzsKCiAgICBwcmludGYoInNpemVvZihzdHJ1Y3QgWCkgPSAlZFxcbiIsIChpbnQpIHNpemVvZihzdHJ1Y3QgWCkpOwogICAgcHJpbnRmKCJhbGxvY2F0ZWQgPSAlZFxcbiIsIChpbnQpIHNpemVvZihzdHJ1Y3QgWCkgKyBuICogc2l6ZW9mKGRvdWJsZSkpOwoKICAgIHN0cnVjdCBYKiBwID0gbWFsbG9jKHNpemVvZihzdHJ1Y3QgWCkgKyBuICogc2l6ZW9mKGRvdWJsZSkpOwogICAgaWYgKHAgPT0gTlVMTCkgcmV0dXJuIDA7CgogICAgcC0%2BY291bnQgPSBuOwogICAgcC0%2BdmFsdWVzWzBdID0gMTAuMDsKICAgIHAtPnZhbHVlc1sxXSA9IDIwLjA7CiAgICBwLT52YWx1ZXNbMl0gPSAzMC4wOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcC0%2BY291bnQ7ICsraSkKICAgICAgICBwcmludGYoIiVmXFxuIiwgcC0%2BdmFsdWVzW2ldKTsKCiAgICBmcmVlKHApOwoKICAgIHJldHVybiAwOwp9Cgo%3D&to=-2&options=
This works, but it's not perfect/equivalent.
For example, sizeof(struct X) changes after the compilation. This may causes issues, like allocating more memory or assertion failed.
Restrict will be very easy to do, it will be added soon.
I wonder how you are gonna do it. As I said __restrict__ is a compiler extension, and may not work on some C89 compiler.
Note that, flexible array member are already implemented http://cakecc.org/playground.html?code=CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg%2BCgpzdHJ1Y3QgWCB7CiAgICBpbnQgY291bnQ7CiAgICBkb3VibGUgdmFsdWVzW107IC8vIGZsZXhpYmxlIGFycmF5Cn07CgovKgogICAgVGhlIHNpemUgb2YgYSBzdHJ1Y3R1cmUgd2l0aCBhIGZsZXhpYmxlIGFycmF5IG1lbWJlciBpcwogICAgZGV0ZXJtaW5lZCBhcyBpZiB0aGUgZmxleGlibGUgYXJyYXkgbWVtYmVyIHdlcmUgb21pdHRlZCwKICAgIEVYQ0VQVCB0aGF0IGl0IG1heSBoYXZlIG1vcmUgdHJhaWxpbmcgcGFkZGluZyB0aGFuIHRoZQogICAgb21pc3Npb24gd291bGQgaW1wbHkKKi8KCmludCBtYWluKCkgewoKICAgIGludCBuID0gMzsKCiAgICBwcmludGYoInNpemVvZihzdHJ1Y3QgWCkgPSAlZFxcbiIsIChpbnQpIHNpemVvZihzdHJ1Y3QgWCkpOwogICAgcHJpbnRmKCJhbGxvY2F0ZWQgPSAlZFxcbiIsIChpbnQpIHNpemVvZihzdHJ1Y3QgWCkgKyBuICogc2l6ZW9mKGRvdWJsZSkpOwoKICAgIHN0cnVjdCBYKiBwID0gbWFsbG9jKHNpemVvZihzdHJ1Y3QgWCkgKyBuICogc2l6ZW9mKGRvdWJsZSkpOwogICAgaWYgKHAgPT0gTlVMTCkgcmV0dXJuIDA7CgogICAgcC0%2BY291bnQgPSBuOwogICAgcC0%2BdmFsdWVzWzBdID0gMTAuMDsKICAgIHAtPnZhbHVlc1sxXSA9IDIwLjA7CiAgICBwLT52YWx1ZXNbMl0gPSAzMC4wOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcC0%2BY291bnQ7ICsraSkKICAgICAgICBwcmludGYoIiVmXFxuIiwgcC0%2BdmFsdWVzW2ldKTsKCiAgICBmcmVlKHApOwoKICAgIHJldHVybiAwOwp9Cgo%3D&to=-2&options=
This works, but it's not perfect/equivalent. For example,
sizeof(struct X)changes after the compilation. This may causes issues, like allocating more memory or assertion failed.
This was one of the reasons why cake don't have sizeof out its output. I don't see any problem at this moment. Cake output is fixed for one target/platform.
Restrict will be very easy to do, it will be added soon.
I wonder how you are gonna do it. As I said
__restrict__is a compiler extension, and may not work on some C89 compiler.
The keyword will be chosen according whit the target, this already happens with thread_local for instance.
This was one of the reasons why cake don't have sizeof out its output. I don't see any problem at this moment. Cake output is fixed for one target/platform. The keyword will be chosen according whit the target, this already happens with thread_local for instance.
I see.
By the way, I find VLA is supported for both clang and gcc in C89 mode with -std=c89. The following code compiles:
#include <stdlib.h>
int main()
{
int x = 10;
int arr[x];
int (*p)[x] = malloc(sizeof(*p));
}
I am planning to add extra constant to keep the sizeof the VM type, and VM type will be pointers.
I need this for each dimension of the array, that can be a variable or constant.
VLA are implemented. (missing some details) But, C89 also makes easier to port the backbend to asm.
VLA are implemented. (missing some details) But, C89 also makes easier to port the backbend to asm.
So that means currently no plan for C99 support?
I think it is still an option, but not a priority. The current objective is to use the lowest-level features, because they define the possible paths when implementing a backend. Also, the C99/C11 features to be considered first are those that affect code generation (for example, restrict), while things like static_assert are not.So, "restrict" is actually a stronger candidate to be implemented than VLAs, for instance, or C11 static_assert or attributes. At this point cake is less a "transpiler" and more a compiler, and the backend at this point is C.