Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some support for classes without TypeInfos #2765

Merged
merged 1 commit into from
Jul 10, 2018

Conversation

kinke
Copy link
Member

@kinke kinke commented Jul 6, 2018

For -betterC and/or a minimal object.d without TypeInfo:

  • Skip TypeInfo emission for classes and interfaces.
  • Emit null as first vtable member.

Makes dmd-testsuite's {compilable,runnable}/minimal2.d work.

@kinke
Copy link
Member Author

kinke commented Jul 6, 2018

Pinging @JinShil.

// emit the interfaceInfosZ symbol if necessary
if (cd->vtblInterfaces && cd->vtblInterfaces->dim > 0) {
irAggr->getInterfaceArraySymbol(); // initializer is applied when it's built
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore the comment, this only declared it. It should be declared lazily on-demand anyway.

@JinShil
Copy link

JinShil commented Jul 6, 2018

cc @ibuclaw

I never got this working for -betterC in DMD. I ran into a problem due to -betterC's janky implementation of importing object.d at compile-time, but then pulling the rug out from under it at link-time: dlang/dmd#8324

@ibuclaw
Copy link
Contributor

ibuclaw commented Jul 6, 2018

@JinShil - I generate typeinfo layouts if the user doesn't define them in object.d. But ended up doing the same with removing vtable references, albeit with more code as it required some refactoring. D-Programming-GDC/gdc@ef979f6

@kinke
Copy link
Member Author

kinke commented Jul 6, 2018

I never got this working for -betterC in DMD.

When making runnable/minimal2.d's main() extern(C) (may be an LDC quirk) and compiling with ldc2 -betterC minimal2.d, I get 4 undefined symbols, the virtual base methods from Object in druntime's object.o, required for the vtables (LDC defines those in the module declaring the class (and only there), regardless of whether they are used or not). So a custom object.d is still required to get it to link.

@JinShil
Copy link

JinShil commented Jul 6, 2018

Sorry if this turned out to be a PITA. My intention was that if TypeInfo and Object weren't defined, class instances would not be supported, and nothing from Object would even need to be emitted: See dsymbolsem in https://github.com/dlang/dmd/pull/8204/files It seemed to work right for DMD. I expected the compiler to determine what it needed to emit from what was defined in object.d. It appears the compiler is still redundantly hardcoding things that are already defined in druntime, so when druntime isn't actually there, it's out of sync.

My intention in all of this is for druntime to be less special and treated just like any other library: If a symbol is not declared, but is used, the compiler emits an error just like it would for any other library. If a symbol is not declared, but also not used, it would merrily carry on. It was an idea I proposed almost 4 years ago. When @WalterBright implemented -betterC, I saw the chance to make it work.

@kinke
Copy link
Member Author

kinke commented Jul 6, 2018

I think I understand what you're trying to achieve, and see value in it.

All I'm saying is that you still need a custom object.d with either no Object (so that your new check kicks in, only implicitly deriving top-level classes from Object if there is such a class in object.d) or some Object base class (and compile/link the object.d file if it has any virtual methods). Apart from that, -betterC alone seems to suffice to use classes to some extent already with this PR.
I guess there's no way around a custom object.d here, as I would still like to be able to specify a custom Object for -betterC code.

My intention is to also enable usage of class instances with a minimal runtime, e.g., I don't see why virtual function calls shouldn't work (excl. interfaces) with manually (or scope?) allocated instances. E.g., this works (no druntime dependency) with an empty object.d (=> no implicit Object base class, and no TypeInfo emission due to missing TypeInfo):

class A
{
    int x;
    bool foo() const { return false; }
}

class B : A
{
    override bool foo() const { return true; }
}

__gshared immutable A a = new A();
__gshared immutable B b = new B();

extern(C) int main()
{
    assert(!a.foo());
    assert((cast(A) b).foo());
    return 0;
}

@JohanEngelen
Copy link
Member

@kinke Perhaps it's easiest to create a tests/baremetal/ directory with lit configuration that automatically specifies a modified ldc.conf that does not include the std libs nor object.d etc. ? I think it would ease adding testcases

@kinke
Copy link
Member Author

kinke commented Jul 7, 2018

@JohanEngelen: Good idea, I implemented something similar in the WebAssembly-PR, e46cf56. I'll add the code above as further test here after that one is merged.

For -betterC and/or a minimal object.d without TypeInfo:

* Skip TypeInfo emission for classes and interfaces.
* Emit null as first vtable member.

Makes dmd-testsuite's {compilable,runnable}/minimal2.d work.
@kinke kinke merged commit 494bc8b into ldc-developers:master Jul 10, 2018
@kinke kinke deleted the minimal2 branch July 10, 2018 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants