www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Simulating computed goto

reply NonNull <non-null use.startmail.com> writes:
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
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
parent NonNull <non-null use.startmail.com> writes:
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:
 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/
Very good tool! Thanks.
Nov 26 2020
prev sibling next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
parent reply NonNull <non-null use.startmail.com> writes:
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.


 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.
Yes this is about efficiency.
 Is there a good way to simulate computed goto in D?
With a switch statement. ;)
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?
Nov 25 2020
parent Dukc <ajieskola gmail.com> writes:
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
prev sibling parent reply Dukc <ajieskola gmail.com> writes:
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
parent NonNull <non-null use.startmail.com> writes:
On Thursday, 26 November 2020 at 04:42:08 UTC, Dukc wrote:
 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.
Interesting idea!
Nov 26 2020