-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
use a builtin enum for calling conventions instead of keywords #661
Comments
I like this. Extra keywords can come back and bite you later. Java has annotations. Nim has odd looking pragmas etc. It seems like a good idea to have some way to apply metadata/modifiers to things without cluttering up the keyword set. I like where you are going with this. |
I like where this is going, but I don't see what other uses an enum annotation has for functions other than the calling convention. If you're going to introduce the notion of "you can annotate functions with enums", it better have more than one use case. I think this can be applicable to other function attributes, such as |
This was something I was wondering about a little while back so I generally like the proposal. I think it'd be wise as @phase mentioned on looking at the https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes We already expose some of these at the language level (such as align) and others aren't applicable to zig, (i.e. non-null). I can see more attribute like functionality wanted in the future, in which case we would need a system to handle many attributes. |
How so? |
The more keywords you have the more you have to watch your variable names etc. If you have multiple namespaces (i.e. struct field names do not collide with other names), then it is not so bad. Translating C to C++ can be "fun" sometimes because of the reserved words. |
In zig you can use |
* docgen supports obj_err code kind for demonstrating errors without explicit test cases * add documentation for `extern enum`. See #367 * remove coldcc keyword and add @setIsCold. See #661 * add compile errors for non-extern struct, enum, unions in function signatures * add .h file generation for extern struct, enum, unions
* instead of `async(allocator) call()`, now it is `async<allocator> call()`. * Fixes syntax ambiguity when leaving off the allocator * Fixes parse failure when call is a field access This sets a precedent for using `<` to pass arguments to a keyword. This will affect `enum`, `union`, and `fn` (see #661)
I'm into it. If you do it, I'd definitely support using this bracket notation over parentheses for struct/union modifiers too, for consistency and intuitiveness. It's cool that whatever is inside the brackets is a comptime expression.
Just to add to that list for inspiration, Rust has attributes, which have some usability drawbacks in my opinion:
Nitpick: this should probably be |
A few sparse thoughts since I need to mark some functions to have the "aapcs" calling convention.
|
It's not a calling convention in the enum you linked to, but a calling convention does define caller vs callee saved registers, etc, which are handled in prologue/epilogue code, so it seems a reasonable stretch to me. Semi-related: Instead of marking interrupts as having the "interrupt" attribute like in C, it makes sense to me to include those in the calling convention enum as well. It does after all, define how the function will be called and what registers it is expected to save and restore (among other things). I'm working on this issue right now, and that is my ultimate goal that made me interested in this issue specifically. I am also curious, why did you need to mark some functions as having that calling convention? What is your target? AAPCS is not the default for that target? |
On hard-float targets all the __aeabi intrinsics that deal with floating-point values must use the soft-float ABI. |
I updated this proposal to take into account tiehuis's comment and the existing align(expr) syntax on functions. |
So async still goes at the beginning like export, extern, and inline? |
Does this mean extern is only valid in front of a function prototype with no body, indicating that it is going to be resolved at link time or can extern also mean that if the calling convention is callconv(builtin.CC.Auto) (or missing) then it becomes callconv(builtin.CC.C)? |
Similarly, does async mean that specifying the calling convention is invalid? |
For Vectors there is not just one calling convention. x86_64 has 3 calling conventions (128-bit, 256-bit, and 512-bit SIMD width) and ARM, which generally just using the NEON one, is adding two more: SVE (multiple widths too!) and Helium for embedded cpus. On x86_64 and arm64 I think we should forbid exporting or calling with vector sizes that are bigger than the target architecture, but that doesn't take care of Helium. |
Why did this annotation end up after the arguments next to the return type rather than next to the |
I agree with this critique - it looks more awkward than if it was on the
Sorry @LemonBoy for the late response, but the way it works is that callers are not allowed to call "nakedcc" functions; they must |
right now we have these keywords for function calling conventions:
extern
- must have C ABI. Also used to indicate C ABI on structs, enums, and unions.stdcallcc
coldcc
- cold calling convention is a thing, but this might be better off as an optimizer hint that the function is cold rather than specifying a calling convention. Currently the line is blurred.nakedcc
- used for entry points like_start
and when inline assembly defines the whole function.If no calling convention is specified, then the calling convention is selected automatically (and currently defaults to the "fast calling convention" that LLVM has available.) Related issue: #105 when mixing automatic calling convention with specified calling conventions.
Any function can be
export
ed, and if the calling convention of an exported function is not specified, thenextern
is chosen.This proposal is to make a builtin enum:
And instead of keywords, the
fn
keyword has optional calling convention specifier that matchesalign
, like this:If a calling convention is not specified,
builtin.CC.Auto
is the default. This is similar to the endianness in pointers proposal (see #649). When displaying a fn type, the calling convention only has to be specified if it is notbuiltin.CC.Auto
.Then it becomes easier to add reflection with
@typeInfo(@typeOf(_start))
.builtin.TypeInfo.CallingConvention
should be pulled out intobuiltin.CallingConvention
Now that enum literals have arrived, this proposal is nicer:
The text was updated successfully, but these errors were encountered: