Bug 14931 – switch doesn't work with any pointers

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-08-18T14:54:19Z
Last change time
2024-12-13T18:44:15Z
Assigned to
No Owner
Creator
Andrei Alexandrescu
See also
https://issues.dlang.org/show_bug.cgi?id=5862, https://issues.dlang.org/show_bug.cgi?id=6176
Moved to GitHub: dmd#17720 →

Comments

Comment #0 by andrei — 2015-08-18T14:54:19Z
Comment #1 by andrei — 2015-08-18T14:57:01Z
Consider: void fun(T)() {} alias Ptr = void function() pure nothrow @nogc @safe; void main(string[] argv) { enum a = &fun!int; static immutable Ptr b = &fun!double; static assert(a != b); Ptr p; switch (p) { case a: break; case b: break; } } The first three lines in main() clarify that pointers to functions can be evaluated and compared during compilation. Yet the switch statement does not work. This applies to general pointers - switch does not work with any pointers, yet it should work because pointers are comparable and there exist constant pointers.
Comment #2 by dfj1esp02 — 2015-08-18T15:53:53Z
As I understand from the discussion, the idea is to not only compile it, but to generate a jump table for such switch statement instead of being a syntax sugar for if-else chain?
Comment #3 by andrei — 2015-08-18T16:44:00Z
Yah, accept it and subject it to the usual assumptions and optimizations that go for equivalent integral-based switch statements.
Comment #4 by yebblies — 2015-08-18T23:44:40Z
(In reply to Andrei Alexandrescu from comment #3) > Yah, accept it and subject it to the usual assumptions and optimizations > that go for equivalent integral-based switch statements. There is no guarantee that integral switches get turned into jump tables.
Comment #5 by andrei — 2015-08-19T00:48:40Z
(In reply to yebblies from comment #4) > (In reply to Andrei Alexandrescu from comment #3) > > Yah, accept it and subject it to the usual assumptions and optimizations > > that go for equivalent integral-based switch statements. > > There is no guarantee that integral switches get turned into jump tables. Understood, thanks. Just handle them the same is all.
Comment #6 by yebblies — 2015-08-19T01:05:12Z
(In reply to Andrei Alexandrescu from comment #5) > (In reply to yebblies from comment #4) > > (In reply to Andrei Alexandrescu from comment #3) > > > Yah, accept it and subject it to the usual assumptions and optimizations > > > that go for equivalent integral-based switch statements. > > > > There is no guarantee that integral switches get turned into jump tables. > > Understood, thanks. Just handle them the same is all. We should be able to support pointer switch, but we can't do pointer switch jump tables since the pointer's integer representation isn't known until link time.
Comment #7 by petar.p.kirov — 2015-08-19T03:18:20Z
Is it possible to generate a jump table with placeholders and fill them at link time?
Comment #8 by yebblies — 2015-08-19T03:47:02Z
(In reply to ZombineDev from comment #7) > Is it possible to generate a jump table with placeholders and fill them at > link time? How can you choose which entry goes where in the jump table if you don't know the pointer values?
Comment #9 by bugzilla — 2015-08-19T06:51:37Z
The only thing you can do is have two tables. The first is a table of pointer values, upon which a binary search is done to convert the switch expression into an integer. Then use the integer to index into the second table of jump addresses. Of course, you can do this manually in conventional D anyway, and get just as good results.
Comment #10 by yebblies — 2015-08-19T07:45:28Z
(In reply to Walter Bright from comment #9) > The only thing you can do is have two tables. The first is a table of > pointer values, upon which a binary search is done to convert the switch > expression into an integer. Then use the integer to index into the second > table of jump addresses. > > Of course, you can do this manually in conventional D anyway, and get just > as good results. Wouldn't you need to sort the pointer list at runtime?
Comment #11 by dfj1esp02 — 2015-08-19T07:49:54Z
(In reply to Andrei Alexandrescu from comment #3) > Yah, accept it and subject it to the usual assumptions and optimizations > that go for equivalent integral-based switch statements. If you go for equivalence, enum-based switch will be faster, because it's not equivalent to runtime switch.
Comment #12 by petar.p.kirov — 2015-08-19T09:52:23Z
(In reply to yebblies from comment #8) > How can you choose which entry goes where in the jump table if you don't > know the pointer values? I'm not familiar with what advanced linker options are available for us, so I don't know if it's possible.
Comment #13 by yebblies — 2015-08-20T02:19:24Z
(In reply to ZombineDev from comment #12) > (In reply to yebblies from comment #8) > > How can you choose which entry goes where in the jump table if you don't > > know the pointer values? > > I'm not familiar with what advanced linker options are available for us, so > I don't know if it's possible. Pretty much no options.
Comment #14 by robert.schadek — 2024-12-13T18:44:15Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17720 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB