copilot icon indicating copy to clipboard operation
copilot copied to clipboard

`copilot-c99`: Can C99 generator declare stream functions as static?

Open simondlevy opened this issue 11 months ago • 11 comments

C code output by Copilot declares variables static but functions global; e.g.:

static size_t s3_idx = (0);

float s0_get(size_t x) {
  return (s0)[((s0_idx) + (x)) % (1)];
}

Global declaration of functions causes duplicate-name conflicts when linking together multiple C sources output by Copilot. Issue could probably be handled by a script, but I'm wondering why the local functions are being declared without static to begin with.

simondlevy avatar Mar 20 '24 22:03 simondlevy

That's a good point. I see no reason to make them public and I agree that it would be better to make functions static.

ivanperez-keera avatar Mar 21 '24 01:03 ivanperez-keera

@fdedden Did you have a chance to look into this? Do you think this can be done without negative impact?

ivanperez-keera avatar Apr 17 '24 14:04 ivanperez-keera

Dear Simon, thank you for the suggestion. Yes, it is indeed better to have these functions defined as static. It forces the so called 'storage duration' to be as long as the execution of the program (which is what we want here). As you mentioned, adding static to global functions keeps the function private to the execution unit (similar to a file in this case) as well.

The addition of the keyword has already been implemented in January for the 3.18 release of Copilot (see 6779d032d2f8f0a6b5e7d7067e75c6ce901d41e9) to comply with the MISRA C coding standard. It should suffice for you to upgrade to at least the 3.18 release.

Please let me know if you have any more questions.

Reference to the C99 standard, section 6.2.2 and 6.2.4: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf.

fdedden avatar Apr 19 '24 10:04 fdedden

The addition of the keyword has already been implemented in January for the 3.18

I think I only added it for variables, not functions, didn't I?

ivanperez-keera avatar Apr 20 '24 22:04 ivanperez-keera

The addition of the keyword has already been implemented in January for the 3.18

I think I only added it for variables, not functions, didn't I?

No? The commit I mentioned (https://github.com/Copilot-Language/copilot/commit/6779d032d2f8f0a6b5e7d7067e75c6ce901d41e9) adds static to the generator and accessor functions, and omits the step function (which is what we want).

The variables have been defined static since I wrote the first version of the C99 backend.

Let me also ping @simondlevy, since I forgot to do that in my initial reply.

fdedden avatar Apr 22 '24 07:04 fdedden

You're right. I completely forgot! :open_mouth:

ivanperez-keera avatar Apr 22 '24 07:04 ivanperez-keera

@simondlevy have you been able to try with the latest version?

ivanperez-keera avatar May 07 '24 14:05 ivanperez-keera

I bought a new computer recently and installed Copilot on it following the instructions as usual. When I tried the heater example, the compiled C header still did make the declarations static.

simondlevy avatar May 09 '24 07:05 simondlevy