This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

a proposal ?

I have a thought that would make many processes much more effective and, being only one voice, I would like to know if there is a demand before I propose it to Keil.

This is the result of me having to change a switch statement to a series of IFs to make an ISR process fast enough.

I realize, and thus do not complain, that Keil caould not implemet an ANSI-C switch more efficient than the way it is done.

'n' below will be either 2 or 3 (if Keil decide to implement, they can choose)

a Proposal:
if #pragma QUICKSWITCH then
a) no case value can be more than 255
b) all case values must be multiples of n
c) the switch will be implemented as a jump table

Erik

Parents
  • In my opinion, a pragma would still be useful.

    It's nice that we know that the code generator just happens to generate a jump table when labels are in sequence. But that's not necessarily convenient for the programmer, sometimes can generate extra code (when two labels not in order need to do the same thing, for instance), and isn't a documented "contract" between the source and the compiler. Nothing prevents this quirk of the code generator from changing in the next release. And if you have to share code between two compilers, both compilers may not agree on a convention for source layout to indicate code generation choices.

    A pragma, defined in the manual, would provide a way to force the needed behavior in the cases where it's necessary in a stable and consistent manner.

Reply
  • In my opinion, a pragma would still be useful.

    It's nice that we know that the code generator just happens to generate a jump table when labels are in sequence. But that's not necessarily convenient for the programmer, sometimes can generate extra code (when two labels not in order need to do the same thing, for instance), and isn't a documented "contract" between the source and the compiler. Nothing prevents this quirk of the code generator from changing in the next release. And if you have to share code between two compilers, both compilers may not agree on a convention for source layout to indicate code generation choices.

    A pragma, defined in the manual, would provide a way to force the needed behavior in the cases where it's necessary in a stable and consistent manner.

Children
  • isn't a documented "contract" between the source and the compiler.

    Note that since the language itself doesn't provide any way of spelling out such a contract, it can only exist between the source and one given compiler, using some language extension. That would tend to completely invalidate your other point:

    And if you have to share code between two compilers, both compilers may not agree on a convention for source layout to indicate code generation choices.

    Those two compilers won't agree on the meaning of some language extension either.

    Let's face it: the only realistic way to get something like this interpreted the same way by more than one compiler is to feed it into your national standardization organization and keeping your fingers crossed for about 10 years until it possibly comes out the other end as a revised standard.

    A pragma, defined in the manual, would provide a way to force the needed behavior in the cases where it's necessary in a stable and consistent manner.

    It's just as likely to mean nothing, or worse yet, something completely different, to that hypothetical other compiler your colleague will have to port the code to next week. #pragma's (except the couple C99 ones) and portability are mutually contradictive concepts.

  • portability my a..

    The one and only interest in the means of execution of any statement is throughput. If you are throughput critical, then there is no 'portability'.

    Should you use #pragma QUICKSWITCH for any reason other that throughput, it does not matter one iota what 'that other compiler' does with it.

    The very exisience of the verb 'portinng' is proof that portability is a myth. Reuse, template, basis sure - but 'portable' spare me.

    Yes, where there is no timing concerns and memory is ample you may have a case for 'portability' but from a '51 to a HC08 come on!

    Erik

  • "If you are throughput critical, ..."

    then write it in assembly language.

  • "If you are throughput critical, ..."

    then write it in assembly language.


    I do, but if there is a way I can do it ib C that would still be preferable.

    The statement "then write it in assembly language" sure does kill the argument re portability :)

    Have a great weekend

    Erik

  • Those two compilers won't agree on the meaning of some language extension either.

    True, but it does at least give you a common place to put the toolchain-specific notes. #pragma itself is standard. So, you'd wind up with

    #if __C51__
    #pragma Keils_pragma_for_jumptable
    #endif
    #if __GCC___
    #pragma GCCs_pragma_for_jump_table
    #endif

    // common code for the actual "C" part.

    If you really hate #ifs in your code, you could make that series a single file so that the change in the actual .c file reduces to

    #include "pragma_jumptable"

    The pragma also documents the requirement that this particular switch must be a jump table much more clearly than relying on a source formatting convention. (Might as well say all function declarations have to start in column 8, while all statements have to start in column 12.)

    As for using the C source proper, consider the worst case -- one compiler decides that in order, ascending values secretly means "jump table", while the other compiler decides it means if-then-else. You're exactly right that this contract is one per compiler, which is exactly why it can't be in the source itself. That source has to be fed to all of the compilers. You can't both order the switch labels ascending and order them descending at the same time.

    So, the magic signal to convey these sorts of wishes has to be something other than the source itself. That's what #pragma was invented to do.

    Standardizing the pragmas themselves would be nice as well. Pragmas also could use a per-vendor "namespace". Lots of compilers, for example, support "#pragma pack", but they don't all mean the same thing by it. Compilers can ignore #pragmas they don't recognize, but there's not a good way to make sure they don't misinterpret a pragma they do recognize, but with different semantics.