Simulating computed goto

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Nov 25 19:04:45 UTC 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


More information about the Digitalmars-d-learn mailing list