Howto“goto address_x;” [英] Howto "goto address_x;"

查看:95
本文介绍了Howto“goto address_x;”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,请不要讨论goto是否是邪恶的。

对于生成的代码,这可能非常有意义。


知道如何让C转到

一个已分配给变量的地址,即


MyLabel会很有趣:

....

address_x = MyLabel;

....

goto address_x;

我试图将这些头寸打包成一个开关声明,然后将'b $ b''地址_x''变成'enum''类型的变量,例如


enum {Label1,Label2,Label3,...} address_x;


GotoStartPoint:

switch(address_x){

case Label1:...

case Label2:...

case Label3:...

}

....

address_x = Label2;

转到GotoStartPoint;


但是当我看到汇编程序代码,它不如跳转到分配给av的地址那样有效ariable。


有没有什么办法可以获得好处?


最好的问候


弗兰克

Before I start, please let''s not discuss whether goto is evil or not.
For generated code, this can make perfect sense.

It would be interesting to know a trick how to get C to do a goto to
an address that has been assigned to a variable, i.e.

MyLabel:
....
address_x = MyLabel;
....
goto address_x;
I tried to pack the positions into a switch statement and made
''address_x'' a variable of type ''enum'', such as

enum { Label1, Label2, Label3, ... } address_x;

GotoStartPoint:
switch( address_x ) {
case Label1: ...
case Label2: ...
case Label3: ...
}
....
address_x = Label2;
goto GotoStartPoint;

But when I look at the assembler code, it is not as efficient as a
jump to an address assigned to a variable.

Is there any way to get arround that?

Best Regards

Frank

推荐答案

Frank写道:
Frank wrote:

知道一个技巧怎么样会很有趣让C做一个转到

已分配给变量的地址,即
It would be interesting to know a trick how to get C to do a goto to
an address that has been assigned to a variable, i.e.



(剪断有些令人困惑的代码)。 br />

您/实际上/想要实现的目标是什么?剪切的代码看起来像

试图解决一些你还没解释过的问题。

(snip somewhat confusing code).

What are you /actually/ trying to achieve ? The snipped code looks like
an attempt to solve some problem or other which you haven''t explained.


1月4日,12: 49 * am,Frank< fsch ... @ googlemail.comwrote:
On Jan 4, 12:49*am, Frank <fsch...@googlemail.comwrote:

在开始之前,*请不要讨论goto是否是邪恶的。

对于生成的代码,这可能非常有意义。


知道一个如何让C转到
已分配给变量的地址,即


MyLabel:

...

* * * address_x = MyLabel;

...

* * *转到地址_x;


我试图打包这些职位转换成一个switch语句并使得'b $ b''address_x''变量类型为''enum'',例如


enum {Label1,Label2,Label3 ,...} address_x;


GotoStartPoint:

switch(address_x){

case Label1:...

ca se Label2:...

case Label3:...}

...

address_x = Label2;

转到GotoStartPoint;


但是当我查看汇编程序代码时,它的效率不如

跳转到分配的地址变量。


有没有什么方法可以得到它?
Before I start, *please let''s not discuss whether goto is evil or not.
For generated code, this can make perfect sense.

It would be interesting to know a trick how to get C to do a goto to
an address that has been assigned to a variable, i.e.

MyLabel:
...
* * *address_x = MyLabel;
...
* * *goto address_x;

I tried to pack the positions into a switch statement and made
''address_x'' a variable of type ''enum'', such as

enum { Label1, Label2, Label3, ... } address_x;

GotoStartPoint:
switch( address_x ) {
case Label1: ...
case Label2: ...
case Label3: ...}

...
address_x = Label2;
goto GotoStartPoint;

But when I look at the assembler code, it is not as efficient as a
jump to an address assigned to a variable.

Is there any way to get arround that?



在写协同程序之前,问问自己,它真的值得吗?

毕竟,有人将不得不保持它。


无论如何,这将显示你的目标:
http://www.google.com/search?hl=en&q=coroutines



" Frank" < fs ***** @ googlemail.com写信息

新闻:a2 ************************* ********* @ e6g2000p rf.googlegroups.com ...

"Frank" <fs*****@googlemail.comwrote in message
news:a2**********************************@e6g2000p rf.googlegroups.com...

在开始之前,请不要讨论goto是否是邪恶的或者没有。

对于生成的代码,这可能非常有意义。


知道一个如何让C去做goto的技巧会很有趣

已分配给变量的地址,即


MyLabel:

...

address_x = MyLabel;

...

转到地址_x;


我试图将这些头寸打包成一个开关声明并且制作了

''address_x'''enum''类型的变量,例如


enum {Label1,Label2,Label3,... } address_x;


GotoStartPoint:

开关(address_x){

case Label1:...

case Label2:...

case Label3:...

}

...

address_x = Label2;

转到GotoStartPoint;


但是当我查看汇编程序代码时,它的效率不如

跳转到分配给变量的地址。


有没有什么方法可以获得它的好处?


最好的问候


Frank
Before I start, please let''s not discuss whether goto is evil or not.
For generated code, this can make perfect sense.

It would be interesting to know a trick how to get C to do a goto to
an address that has been assigned to a variable, i.e.

MyLabel:
...
address_x = MyLabel;
...
goto address_x;
I tried to pack the positions into a switch statement and made
''address_x'' a variable of type ''enum'', such as

enum { Label1, Label2, Label3, ... } address_x;

GotoStartPoint:
switch( address_x ) {
case Label1: ...
case Label2: ...
case Label3: ...
}
...
address_x = Label2;
goto GotoStartPoint;

But when I look at the assembler code, it is not as efficient as a
jump to an address assigned to a variable.

Is there any way to get arround that?

Best Regards

Frank



GNU C具有标签变量的扩展名,允许您跳转到任何

这样的评估地址。


标签作为价值

================


您可以使用一元运算符&&来获取当前函数

(或包含函数)中定义的标签的地址。值为

类型`void *''。该值是常量,可以在该类型的

常量有效的任何地方使用。例如:


void * ptr;

/ * ... * /

ptr =&& foo;


要使用这些值,您需要能够跳转到一个。这是用计算的goto语句(1),`goto * EXP;''完成的
。例如,


goto * ptr;


允许任何类型为void *的表达式。

使用这些常量的一种方法是初始化一个静态数组

作为跳转表:


static void * array [ ] = {&& foo,&& bar,&& hack};


然后您可以选择带索引的标签,如下所示:


goto * array [i];

GNU C has the extension of label variables and allows you to jump to any
such evaluated address.

Labels as Values
================

You can get the address of a label defined in the current function
(or a containing function) with the unary operator `&&''. The value has
type `void *''. This value is a constant and can be used wherever a
constant of that type is valid. For example:

void *ptr;
/* ... */
ptr = &&foo;

To use these values, you need to be able to jump to one. This is
done with the computed goto statement(1), `goto *EXP;''. For example,

goto *ptr;

Any expression of type `void *'' is allowed.

One way of using these constants is in initializing a static array
that will serve as a jump table:

static void *array[] = { &&foo, &&bar, &&hack };

Then you can select a label with indexing, like this:

goto *array[i];


这篇关于Howto“goto address_x;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆