www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Label as Values in D ?

reply Patric Dexheimer <patric.dexheimer gmail.com> writes:
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

Is possible to use Label as Values in D?
Or there is a way to do dynamic goto statements?

Ps: This is out of curiosity / performance reasons. I know the 
potential of bad design / ugly code ;)
Jun 05 2017
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
Patric Dexheimer wrote:

 https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

 Is possible to use Label as Values in D?
 Or there is a way to do dynamic goto statements?

 Ps: This is out of curiosity / performance reasons. I know the potential 
 of bad design / ugly code ;)
as we have nested functions, you can just initialize a table with function pointers, and use it instead of computed goto. this is way more powerful trick, as you can actually use any selector you want, in any way you want, and still have the speed of computed goto (most of the time).
Jun 05 2017
parent Patric Dexheimer <patric.dexheimer gmail.com> writes:
On Monday, 5 June 2017 at 13:16:43 UTC, ketmar wrote:
 Patric Dexheimer wrote:

 https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

 Is possible to use Label as Values in D?
 Or there is a way to do dynamic goto statements?

 Ps: This is out of curiosity / performance reasons. I know the 
 potential of bad design / ugly code ;)
as we have nested functions, you can just initialize a table with function pointers, and use it instead of computed goto. this is way more powerful trick, as you can actually use any selector you want, in any way you want, and still have the speed of computed goto (most of the time).
That´s nice, I take a look with compiler explorer and it indeed create a jump instead of a call. Not sure if fast as goto, w´ll have to benchmark it :)
Jun 05 2017
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Monday, 5 June 2017 at 11:40:28 UTC, Patric Dexheimer wrote:
 https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

 Is possible to use Label as Values in D?
 Or there is a way to do dynamic goto statements?

 Ps: This is out of curiosity / performance reasons. I know the 
 potential of bad design / ugly code ;)
It seems that labels are strictly compile time concepts. Once used to set the JMP offsets they go back into the nil. For dynamic goto i think that you're already been given the answer: jump tables can be emulated with array of delegates/functions, although probably slightly slower (CALL vs JMP, there can be a function frame for the CALL destination).
Jun 05 2017