DeqingSun / ch55xduino

An Arduino-like programming API for the CH55X
GNU Lesser General Public License v2.1
448 stars 86 forks source link

Use type-generic macros #54

Closed leo60228 closed 2 years ago

leo60228 commented 3 years ago

According to the README:

There is no free C++ compiler for MCS51 chip, we can not use polymorph functions. So you can not expect the compiler will choose a function according to the parameter's type.

C11 adds _Generic for this purpose. Here's a trivial example: https://godbolt.org/z/Yv8sznf3v

However, this currently can't be used for print due to an SDCC bug.

DeqingSun commented 3 years ago

@leo60228 thanks for this info. I'll check if this can be implemented into the package.

DeqingSun commented 3 years ago

@leo60228 Frankly speaking I have no idea what "_Generic" means or how it compiles (yet). But it seems work. I'll see how to add it into the package.

Screen Shot 2021-09-29 at 9 07 01 AM

leo60228 commented 3 years ago

_Generic is a relatively new feature (C99 had polymorphic functions in the standard library, but those were implemented in a compiler-specific way until C11). SDCC supports it but there's several open bugs. I don't think it's usable for print yet, but it could be with some bugfixes, and might be useful for other functions.

DeqingSun commented 3 years ago

@leo60228 Sorry I ask the question before I learn some basics about _Generic. Why you think it is not usable for print? It seems my sketch compiles and works as expected.

leo60228 commented 3 years ago

There's a cryptic compiler error if you use it with a pointer, which is why char* was commented out. I linked an existing bug report for this in the original issue.

On Wed, Sep 29, 2021, 9:13 AM Deqing Sun @.***> wrote:

@leo60228 https://github.com/leo60228 Sorry I ask the question before I learn some basics about _Generic. Why you think it is not usable for print? It seems my sketch compiles and works as expected.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/DeqingSun/ch55xduino/issues/54#issuecomment-930163578, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB7X32PG37EFDPVLKK5WBB3UEMGJHANCNFSM5E6UQG2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

DeqingSun commented 3 years ago

@leo60228 Thank you and I get it when trying to use the pointer.

Internal error: validateLink failed in SPEC_NOUN(type) @ /Users/sdcc-builder/build/sdcc-build/orig/sdcc/src/SDCCast.c:5401: expected SPECIFIER, got DECLARATOR

I'll subscribe to that ticket and see when it get fixed. Not supporting pointer makes implementing the _Generic to print pointless.

DeqingSun commented 2 years ago

Update: The bug fixing is now funded. Wish it get fixed soon

https://sourceforge.net/p/sdcc/wiki/SDCC-STD-UX/

DeqingSun commented 2 years ago

Generic is functional after SDCC commit 13402.

This repo now supports Generic for print.

https://github.com/DeqingSun/ch55xduino/blob/ch55xduino/ch55xduino/ch55x/cores/ch55xduino/genericPrintSelection.h