-2

I am setting up some large width integer types, and so I am making heavy use of macros to make the types usable as much as possible like the basic integer types. An issue I keep running into is that I can have the most straightforward and easily implemented solutions if I make generous use of _Generic expressions in my macro expansions, as opposed to minimizing _Generic use and relying more on macros and possible multiple versions of operations.

So, my question is if _Generic expressions are in practice the same as macros. Are there optimizer issues with _Generic's that are not a problem with macros? Are there differences in compilation through some other mechanism?

What makes me nervous is that _Generics conceptually are almost identical to macros, and so why are they syntactically expressions?

I understand that the answer to this question is compiler related, but I imagine all reasonable compilers will have similar behaviour.

Some responses suggest I should explain how _Generics and macros are similar with regard to my question. Both replace their invocation with code specific to a circumstance. A macro has more general rules for how to produce the replacement, where a _Generic must make a selection from specific inputs based on the type of the first. The point is that both are conceptually preprocessing ideas, in that they determine what code is actually compiled.

jack
  • 282
  • 2
  • 9
  • 1
    Why do you think that `_Generic` are as macros? They are completely different kinds of animals, aren't they? – Jens Gustedt May 12 '15 at 22:48
  • 1
    A macro converts its arguments to code with which the macro is replaced. A _Generic does the same thing in a more restricted but sometimes more powerful way: the arguments have to be assignment-expressions possibly with type prefixes, and the output is selected from the input. They both share the same basic principal of replacing the invocation with code specific to the situation, which conceptually makes most sense before anything else in compilation stage 7. – jack May 12 '15 at 22:53

1 Answers1

0

If _Generic and macros are similar for you because they can be evaluated before run-time, do you also think that sizeof (except for VLA), _Static_assert or constant expressions are similar to macros?

Remember that at pre-processing time, the implementation has no knowledge of types, keywords, etc. and is only processing tokens.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    A _Generic is conceptually replaced with the appropriate code before conversion of c code to machine code. Obviously I don't mean that translation stage 4 deals with _Generics. I mean that it is part of what is done before dealing with the actual creation of machine code. Attacking my question based on semantic issues that clearly do not indicate a poorly conceived question is only detrimental. – jack May 12 '15 at 23:16
  • 1
    @jack, this is not attacking your question, this is trying to answer it. Your concept of "before the actual creation of machine code" is just fuzzy. Compilers nowadays work in many different stages, different steps that transform program text into machine code. All of that can be seen as "preprocessing" if you wish ... or not. Voting to close the question. – Jens Gustedt May 12 '15 at 23:21
  • 1
    @jack Nobody is "attacking" your question, `_Generic` is processed in translation phase 7 the same way `_Static_assert` or `sizeof` for non-VLA operand are, I cannot see any difference between these. – ouah May 12 '15 at 23:22
  • 1
    _Generics could be processed in stage 4 very reasonably. That is what I mean by them conceptually being like macros. _Generics are inherently different from most expressions in this sense. _Generics are more syntactic than semantic. So it makes sense to ask if they are treated this way, or if there are typically any issues in their management. There is nothing fuzzy about that. – jack May 12 '15 at 23:27
  • @jack How could they be? Even `_Static_assert` which is required to be processed at compile-time could not. As we said there is no knowledge of types or of C syntax in phase 4. You are talking of a translation phase which does not exist in C. – ouah May 12 '15 at 23:35
  • 1
    The first six stages of translation are essentially syntax modification. There is no concept of optimization, and the assembly language need not even be known. _Generics could be replaced with their selected expressions in this context, with only very mild knowledge of implementation details, like for example which fundamental integer type size_t aliases. If this is done properly, when assembly is beginning to be considered, _Generics should be gone (or a conceptual equivalent of this would apply). If _Generics behave differently than hard coding the selected expression, I want to know that – jack May 12 '15 at 23:47