ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Add support for Objective-C class/static methods (2.080 feature)

Open kinke opened this issue 7 years ago • 14 comments
trafficstars

I.e., port https://github.com/dlang/dmd/pull/7762 and re-enable dmd-testsuite's runnable/objc_call_static.d.

kinke avatar Apr 27 '18 22:04 kinke

Not sure if it helps but now there’s a specification of the Objective-C ABI [1].

[1] https://github.com/dlang/dmd/blob/master/compiler/docs/objective-c_abi.md

jacob-carlborg avatar May 14 '18 06:05 jacob-carlborg

This might be an excuse for getting your hands dirty at the LDC codebase. ;) Be warned - it's highly addictive. - My interest in Objective-C and Apple in general is 0, so don't count on me.

kinke avatar May 14 '18 20:05 kinke

Is it necessary to support the legacy/non fragile ABI?

jacob-carlborg avatar May 30 '18 14:05 jacob-carlborg

No idea how much more work that would be, as it currently seems to only trivially affect some section names in gen/objcgen.cpp; but modern/non-fragile ABI support only is clearly better than none, so it's definitely no requirement to get a PR merged.

kinke avatar May 31 '18 11:05 kinke

Currently it's only segments/sections that are different. IIRC supporting class methods will require more differences for the different ABIs. Later features will require even more differences, that's why I didn't implement the legacy ABI in DMD and because 32bit on macOS is dead.

but modern/non-fragile ABI support only is clearly better than none, so it's definitely no requirement to get a PR merged.

That's good to hear :+1:.

jacob-carlborg avatar May 31 '18 11:05 jacob-carlborg

https://www.bountysource.com/issues/57688078-add-support-for-objective-c-class-static-methods-2-080-feature

s-ludwig avatar Jan 31 '20 16:01 s-ludwig

Hello, I'm willing to cooperate at this stuff at some point in near future since I'm nearing the end of my engine port for MacOS, I also wish to make it run on iPhone (the main reason of the port), but LDC hasn't implemented that yet.

Would you give me some advice into how I could get started implementing that?

MrcSnm avatar Mar 21 '23 19:03 MrcSnm

I'd suggest starting with an as-simple-as-possible example for clang and studying the produced textual IR (e.g., on godbolt or via -emit-llvm IIRC), to get an idea of what's needed. And then trying to replicate the relevant stuff in LDC codegen, probably somewhere in gen/functions.cpp (Dto{Declare,Define}Function()).

So you need this particular feature for iOS, but not for macOS?

kinke avatar Mar 21 '23 23:03 kinke

Let me give an overview:

  • For testing, DMD already does a great job for me being able to implement Objective-C stuff (currently the Metal API) as it is enough for me to develop.
  • When talking about market share, MacOS almost don't have any appeal, it is basically a development environment only. Now if you talk about iOS, it has a market share as huge as Android (when you're talking about paying people), so far, I believe that it is very important having a D project which is able to run with 99% of D code in all major platforms, specially since every beginner asks themselves whether D code has been tested or used somewhere. This was the main thing that made me take the decision to port my engine to macOS/iOS

MrcSnm avatar Mar 22 '23 00:03 MrcSnm

There's already a basic implementation available. LDC can do Objective-C method calls [1]. If you search for objc_msgSend you'll see there's an implementation for each of the architectures supported by Apple. I did the implementation in DMD so I can help out bit if there's any questions. But I don't think I have time to do implementation myself.

[1] https://github.com/ldc-developers/ldc/blob/906037988f064bc61dbd671abc820f87bc10d128/gen/abi/x86-64.cpp#L382

jacob-carlborg avatar Mar 22 '23 06:03 jacob-carlborg

I'm more than happy to give you a guided tour of LDC over the weekend. Are you going to beerconf?

thewilsonator avatar Mar 22 '23 10:03 thewilsonator

@jacob-carlborg Thanks a lot! Okay I'll surely ask you :)

@thewilsonator Yup I can participate on it :D Getting onto compiling things and starting to being able to test is the main concern I have, with help from both you I'm pretty sure I can implement that

MrcSnm avatar Mar 22 '23 11:03 MrcSnm

@MrcSnm as for getting started with the Objective-C parts, I suggest the following:

  1. Start by having a look at the ABI specification [1]. Some parts of the text are more of a general information of how things work and some parts are more like detailed reference. The specification is x86-64 specific, but both x86-64 and ARM64 uses the modern ABI so I would expect most of it to be the same. What will be different is which and when the objc_msgSend functions are used. What could also be different are sizes and alignments. Keep in mind that the specification is reverse engineered so it might not be 100% correct.
  2. Then look at what's already implemented in LDC and verify that's correct. For example, I doubt that [2] is implemented correctly.
  3. Then continue and implemented the missing functionality, one feature at the time. Check the output with assembly/IR/object dumps and compare with what Clang outputs.

When it comes to tests, most of the existing tests in the DMD test suite are end to end tests. Some are implicit and some are explicit. I would suggest also writing IR tests, if that's something LDC is using (@kinke?).

Also, I wouldn't bother with any 32-bit platforms. They're all obsolete.

[1] https://github.com/dlang/dmd/blob/master/compiler/docs/objective-c_abi.md [2] https://github.com/ldc-developers/ldc/blob/906037988f064bc61dbd671abc820f87bc10d128/gen/abi/aarch64.cpp#L177

jacob-carlborg avatar Mar 23 '23 19:03 jacob-carlborg

When it comes to tests, most of the existing tests in the DMD test suite are end to end tests. Some are implicit and some are explicit. I would suggest also writing IR tests, if that's something LDC is using (@kinke?).

Certainly, see e.g. tests/codegen.

JohanEngelen avatar Mar 23 '23 21:03 JohanEngelen