hth313 / Calypsi-tool-chains

Overview of the Calypsi tool chain and open source support packages
16 stars 0 forks source link

Feature request: _Generic (or some equivalent) #22

Closed KyleCardoza closed 1 year ago

KyleCardoza commented 1 year ago

I know Calypsi targets C99, and I'm not trying to push for it being C11 or C23 instead -- I'm just wondering if it would be possible for there to be some way I could implement the type-generic tgmath.h macros for my project. _Generic (if it's less work for you, like, if the Clang frontend can do the grunt work?) would be ideal, since I can use it for other stuff too, but even just a compiler builtin or something would be enough.

hth313 commented 1 year ago

I looked at it and _Generic is supported by Clang, but the AST translation I have in place does not handle it properly. I have fixed it for the next release. I played with the following code:

extern void stringFunc(char*);
extern void otherStringFunc(char*);
extern void string4Func(char[4]);

#define F(X) _Generic(&(X),        \
  default: otherStringFunc,        \
  char**: stringFunc,              \
  char(*)[4]: string4Func,         \
  char const**: stringFunc,        \
  char const(*)[4]: string4Func   \
  )(X)

void foo(char *p) {
  F(p);
  F("foo");
  F("fooxx");
}

This now results in:

0015                  void foo(char *p) {
    \ 000000                      .section farcode,text
    \ 000000                      .public foo
    \ 000000          foo:
0016                    F(p);
    \ 000000 22......             jsl     long:stringFunc
0017                    F("foo");
    \ 000004 a9....               lda     ##_StringLiteral_foo
    \ 000007 22......             jsl     long:string4Func
0018                    F("fooxx");
    \ 00000b a9....               lda     ##_StringLiteral_fooxx
    \ 00000e 22......             jsl     long:otherStringFunc
0019                  }
    \ 000012 6b                   rtl
KyleCardoza commented 1 year ago

Fantastic! You made my day! Thank you. Is there like a tip jar or something I can throw money at?

hth313 commented 1 year ago

I was laid off from my current job three weeks ago, due to cost cuts and slowdown in the economy, so any contribution to the tip jar is welcome, but do not feel it is a requirement. If you want to, there is my Paypal hth313@gmail.com that can be used. I appreciate the comments you make, it widens my knowledge about C extensions and more recent things added to the language. It helps me improve the products and some of it I may end up using myself eventually.

KyleCardoza commented 1 year ago

What I do is, I set up a Ko-Fi (https://ko-fi.com) account and a Patreon, to allow people to support my work.

Far as comments go, I do have one other feature request at the moment, if you find you have time to look at it: __attribute__((overloadable)), which is a Clang-specific attribute that applies C++-compatible name mangling function overloading to the plain C language. It's a bit easier to use than _Generic, but I have no idea if it's easier to make Calypsi use it.

KyleCardoza commented 1 year ago

We'll leave off asking about shoot-for-the-moon things like C++ or Objective-C support or the like for another day.

hth313 commented 1 year ago

Feel free to open a new ticket with the overloadable attribute, but I will probably need to wait a bit looking into it as I have work ahead with some library matters, board support and new targets.

C++ is of course supported by the Clang front end, there are a couple of issues for me supporting it.

  1. C++ is a complex language and it would be nice to restrict it, at least that would make it more approachable for smaller targets. Like for example avoid exceptions and rtti. I am not sure if the Clang front end will allow me to pick individual features easily, which I prefer to do if going that direction. C++ has also been changing a lot in recent years.
  2. I do not have C++ tests and perennial that I am aware of is commercial.
  3. I see making a good C compiler and improve the code generator a more obtainable goal. I do not do this full time, and even if I did, there are more urgent things to do.
  4. I would consider Rust as an alternative to C++, but it is also a lot of work and not in line with what I currently feel is important to focus on. Rust may not yet be mature enough to bet on.
KyleCardoza commented 1 year ago

I agree fully re: C++ -- I avoid using it at all costs, myself. Do you have similar feelings towards Objective-C, which is also supported by Clang but only adds things to standard C, rather than changing things that are already there? C++ is far more different from C than Objective-C is.

This is strictly hypothetical, of course, and you shouldn't be expected to provide the Obj-C runtime (I have a couple of good ones here, one of which has been thoroughly tested on low-end platforms like DOS PCs and the Nintendo DS!), but it's an interesting thought experiment at least.

hth313 commented 1 year ago

I have honestly never used Objective-C so I do not really have much opinions about it, or even considered it. Rust is on my radar, if there at some point may be another language, though making further use of Clang may make more sense.

The thing is that I have so many other things that feels more important. A lot of that is also work that appeals to me. I would also like to find more time to use my own products.

KyleCardoza commented 1 year ago

Makes total sense to me. Just a thought experiment is all. Love the compiler, by the way.

hth313 commented 1 year ago

Money for the tip jar reached the destination (me), thank you!

KyleCardoza commented 1 year ago

No worries. Sorry I don't have more to spare at the moment, but if you set up a Patreon I can promote it for you.