digitalmars.D.learn - Simulating computed goto
- NonNull (9/9) Nov 25 2020 For automatically generated code of some low level kinds it is
- Paul Backus (4/6) Nov 25 2020 The easiest way to find the answer to a question like this is to
- NonNull (2/8) Nov 26 2020 Very good tool! Thanks.
- H. S. Teoh (10/22) Nov 25 2020 FWIW, D's switch statement is flexible enough to directly write Duff's
- NonNull (16/25) Nov 25 2020 OK, so I took a look at switch documentation and tried out
- Dukc (8/15) Nov 26 2020 I quess you could define a string or a template mixin to automate
- Dukc (3/4) Nov 25 2020 I haven't used assembly myself, but it's possible that you can
- NonNull (2/6) Nov 26 2020 Interesting idea!
For automatically generated code of some low level kinds it is convenient to have "computed goto" like this: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html and D does not have this. A switch could be used to simulate it. But this would lead to what could have been a single jump being chained jumps. How good is optimization in ldc2, gdc, dmd at compiling chained jumps into one jump each time? Is there a good way to simulate computed goto in D?
Nov 25 2020
On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:How good is optimization in ldc2, gdc, dmd at compiling chained jumps into one jump each time?The easiest way to find the answer to a question like this is to use the compiler explorer: https://d.godbolt.org/
Nov 25 2020
On Wednesday, 25 November 2020 at 18:57:35 UTC, Paul Backus wrote:On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:Very good tool! Thanks.How good is optimization in ldc2, gdc, dmd at compiling chained jumps into one jump each time?The easiest way to find the answer to a question like this is to use the compiler explorer: https://d.godbolt.org/
Nov 26 2020
On Wed, Nov 25, 2020 at 06:44:52PM +0000, NonNull via Digitalmars-d-learn wrote:For automatically generated code of some low level kinds it is convenient to have "computed goto" like this: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html and D does not have this. A switch could be used to simulate it. But this would lead to what could have been a single jump being chained jumps.FWIW, D's switch statement is flexible enough to directly write Duff's device.How good is optimization in ldc2, gdc, dmd at compiling chained jumps into one jump each time?I'm pretty sure ldc2 and gdc will optimize away any such chained jumps. But if performance is important to you, I recommend *not* bothering with dmd.Is there a good way to simulate computed goto in D?With a switch statement. ;) T -- They pretend to pay us, and we pretend to work. -- Russian saying
Nov 25 2020
On Wednesday, 25 November 2020 at 19:04:45 UTC, H. S. Teoh wrote:FWIW, D's switch statement is flexible enough to directly write Duff's device.Yes this is about efficiency.How good is optimization in ldc2, gdc, dmd at compiling chained jumps into one jump each time?I'm pretty sure ldc2 and gdc will optimize away any such chained jumps. But if performance is important to you, I recommend *not* bothering with dmd.OK, so I took a look at switch documentation and tried out something. It looked like switch can have computed goto using case labels. case 5: //... goto case <expression>; //doesn't compile when <expression> is not a constant So to simulate computed goto have to 1. wrap switch(x) in a loop [ while(0) ] 2. inside each case recompute x (instead of jump to computed y) 3. jump back to execute switch again [ continue ] It does look as if a nested switch can contain case labels from an outer switch which is very good. Did not try this out. Any more ideas, advice?Is there a good way to simulate computed goto in D?With a switch statement. ;)
Nov 25 2020
On Wednesday, 25 November 2020 at 20:05:28 UTC, NonNull wrote:So to simulate computed goto have to 1. wrap switch(x) in a loop [ while(0) ] 2. inside each case recompute x (instead of jump to computed y) 3. jump back to execute switch again [ continue ] It does look as if a nested switch can contain case labels from an outer switch which is very good. Did not try this out. Any more ideas, advice?I quess you could define a string or a template mixin to automate this, if there are many functions doing this. Then again, you probably should NOT have many functions working this way -it is not at all a scalable way to program. It might be good for something that is called very often and thus needs to be fast, but not for regular code. Computed `goto`s are even worse in maintainability than normal `goto`s.
Nov 26 2020
On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:Is there a good way to simulate computed goto in D?I haven't used assembly myself, but it's possible that you can define a mixin that does this, using inline assembly.
Nov 25 2020
On Thursday, 26 November 2020 at 04:42:08 UTC, Dukc wrote:On Wednesday, 25 November 2020 at 18:44:52 UTC, NonNull wrote:Interesting idea!Is there a good way to simulate computed goto in D?I haven't used assembly myself, but it's possible that you can define a mixin that does this, using inline assembly.
Nov 26 2020