Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > global goto in c

Reply
Thread Tools

global goto in c

 
 
fir
Guest
Posts: n/a
 
      06-06-2013
I want to ask if some global goto
could be done in c (specifically in c with
inline assembly extension) (in this
time I cannot even run compiler and test
it myself so sorry for that)

something like

inline goto(void* label)
{
asm
{
jmp label;
}
}

I understand that in c you could
get adress of any "label:" you could
then pass it to assembly as a jump
argument and then it should work (?)

(prof. fir)

 
Reply With Quote
 
 
 
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      06-06-2013
fir <(E-Mail Removed)> wrote:
> I want to ask if some global goto
> could be done in c


You mean like longjmp()?

-- glen
 
Reply With Quote
 
 
 
 
fir
Guest
Posts: n/a
 
      06-06-2013
W dniu czwartek, 6 czerwca 2013 10:47:28 UTC+2 użytkownik glen herrmannsfeldt napisał:
> fir <(E-Mail Removed)> wrote:
>
> > I want to ask if some global goto

>
> > could be done in c

>
>
>
> You mean like longjmp()?
>


no, like

e()
{
label:

}

main()
{
goto(label); //label is any label in any function

}
 
Reply With Quote
 
Xavier Roche
Guest
Posts: n/a
 
      06-06-2013
On 06/06/2013 11:29 AM, fir wrote:
> no, like
>
> e()
> {
> label:
>
> }
>
> main()
> {
> goto(label); //label is any label in any function
>
> }


Short answer: no. Entering in another function or inside a block require
to have some stack/heap initialization, and this is just not possible.
setjmp/longjmp tricks may be used, but only to rollback from a given
point (ie. going back in a context deeper in the stack has a totally
undefined behavior - correct me if I am wrong)

 
Reply With Quote
 
fir
Guest
Posts: n/a
 
      06-06-2013
W dniu czwartek, 6 czerwca 2013 11:37:39 UTC+2 użytkownik Xavier Rochenapisał:
> On 06/06/2013 11:29 AM, fir wrote:
>
> > no, like

>
> >

>
> > e()

>
> > {

>
> > label:

>
> >

>
> > }

>
> >

>
> > main()

>
> > {

>
> > goto(label); //label is any label in any function

>
> >

>
> > }

>
>
>
> Short answer: no. Entering in another function or inside a block require
>
> to have some stack/heap initialization, and this is just not possible.
>
> setjmp/longjmp tricks may be used, but only to rollback from a given
>
> point (ie. going back in a context deeper in the stack has a totally
>
> undefined behavior - correct me if I am wrong)


you do not need to use stack variables
just

f()
{

f1_label:

// some code

goto(main1_label);

}

main()
[
goto(f1_label);
//some code

main1_label:
}

should probably work, but i am not sure
as to casting labels on void* and then
passing this to assembly
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-06-2013

"fir" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I want to ask if some global goto
> could be done in c (specifically in c with
> inline assembly extension) (in this
> time I cannot even run compiler and test
> it myself so sorry for that)
>
> something like
>
> inline goto(void* label)
> {
> asm
> {
> jmp label;
> }
> }
>
> I understand that in c you could
> get adress of any "label:" you could
> then pass it to assembly as a jump
> argument and then it should work (?)


No. With gcc, you can obtain a label address using &&label, but the label
has to be within scope, which means in the local function.

It is possible to assign that label address to a file-scope variable, and
attempt to 'goto' that address from another function, but it will likely
crash because of other considerations. It will in any case need tricky setup
of those addresses.

--
Bartc

 
Reply With Quote
 
Johann Klammer
Guest
Posts: n/a
 
      06-06-2013
fir wrote:
> W dniu czwartek, 6 czerwca 2013 11:37:39 UTC+2 użytkownik Xavier Roche napisał:
>> On 06/06/2013 11:29 AM, fir wrote:
>>
>>> no, like

>>
>>>

>>
>>> e()

>>
>>> {

>>
>>> label:

>>
>>>

>>
>>> }

>>
>>>

>>
>>> main()

>>
>>> {

>>
>>> goto(label); //label is any label in any function

>>
>>>

>>
>>> }

>>
>>
>>
>> Short answer: no. Entering in another function or inside a block require
>>
>> to have some stack/heap initialization, and this is just not possible.
>>
>> setjmp/longjmp tricks may be used, but only to rollback from a given
>>
>> point (ie. going back in a context deeper in the stack has a totally
>>
>> undefined behavior - correct me if I am wrong)

>
> you do not need to use stack variables
> just
>
> f()
> {
>
> f1_label:
>
> // some code
>
> goto(main1_label);
>
> }
>
> main()
> [
> goto(f1_label);
> //some code
>
> main1_label:
> }
>
> should probably work, but i am not sure
> as to casting labels on void* and then
> passing this to assembly


Yes, that'll probably work, but why the function, then?
You cannot use it for return, because the return address is not on stack
frame and base ptr etc are all wrong... it would jump to nirvana... If
you are trying to set up something coroutine like, you'll want to look
at POSIX' ucontext.h. It's available on most unices and similar
functionality can often be hacked up on other OSs.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-06-2013
On 6/6/2013 4:16 AM, fir wrote:
> I want to ask if some global goto
> could be done in c (specifically in c with
> inline assembly extension) (in this
> time I cannot even run compiler and test
> it myself so sorry for that)
>
> something like
>
> inline goto(void* label)
> {
> asm
> {
> jmp label;
> }
> }
>
> I understand that in c you could
> get adress of any "label:" you could
> then pass it to assembly as a jump
> argument and then it should work (?)


No.

First, in C there is no way to take the address of a label.

Second, if you could somehow get the address of a label (some
compilers offer this capability as an extension), there is no
assurance that a void* could represent that address.

Third, if you could somehow get the label's address and cram
it into a void* (or some other type), jumping into the middle of
a piece of code without first establishing the environment the
code relies on is a bad idea. For example, if the code at the
point of the label expects a frame pointer to aim HERE and a
return address to live THERE and register R5 to hold the value
of `a+b', how will you establish the preconditions?

Fourth -- Oh, the heck with it. Just "No" suffices.

> (prof. fir)


Out of curiosity: What subject do you teach, Professor?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
fir
Guest
Posts: n/a
 
      06-06-2013
W dniu czwartek, 6 czerwca 2013 14:59:47 UTC+2 użytkownik Eric Sosman napisał:
> On 6/6/2013 4:16 AM, fir wrote:
>
> > I want to ask if some global goto

>
> > could be done in c (specifically in c with

>
> > inline assembly extension) (in this

>
> > time I cannot even run compiler and test

>
> > it myself so sorry for that)

>
> >

>
> > something like

>
> >

>
> > inline goto(void* label)

>
> > {

>
> > asm

>
> > {

>
> > jmp label;

>
> > }

>
> > }

>
> >

>
> > I understand that in c you could

>
> > get adress of any "label:" you could

>
> > then pass it to assembly as a jump

>
> > argument and then it should work (?)

>
>
>
> No.
>
>
>
> First, in C there is no way to take the address of a label.
>
>
>
> Second, if you could somehow get the address of a label (some
>
> compilers offer this capability as an extension), there is no
>
> assurance that a void* could represent that address.
>
>
>
> Third, if you could somehow get the label's address and cram
>
> it into a void* (or some other type), jumping into the middle of
>
> a piece of code without first establishing the environment the
>
> code relies on is a bad idea. For example, if the code at the
>
> point of the label expects a frame pointer to aim HERE and a
>
> return address to live THERE and register R5 to hold the value
>
> of `a+b', how will you establish the preconditions?
>
>
>
> Fourth -- Oh, the heck with it. Just "No" suffices.
>
>
>
> > (prof. fir)

>
>
>
> Out of curiosity: What subject do you teach, Professor?
>
>

I am specally interested in the c language
improvements. (but have not to much time
this time to stop here, just asking the
question)
 
Reply With Quote
 
fir
Guest
Posts: n/a
 
      06-06-2013
W dniu czwartek, 6 czerwca 2013 14:28:12 UTC+2 użytkownik Bart napisał:
> "fir" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > I want to ask if some global goto

>
> > could be done in c (specifically in c with

>
> > inline assembly extension) (in this

>
> > time I cannot even run compiler and test

>
> > it myself so sorry for that)

>
> >

>
> > something like

>
> >

>
> > inline goto(void* label)

>
> > {

>
> > asm

>
> > {

>
> > jmp label;

>
> > }

>
> > }

>
> >

>
> > I understand that in c you could

>
> > get adress of any "label:" you could

>
> > then pass it to assembly as a jump

>
> > argument and then it should work (?)

>
>
>
> No. With gcc, you can obtain a label address using &&label, but the label
>
> has to be within scope, which means in the local function.
>
>
>
> It is possible to assign that label address to a file-scope variable, and
>
> attempt to 'goto' that address from another function, but it will likely
>
> crash because of other considerations. It will in any case need tricky setup
>
> of those addresses.
>
>

If so it is damn sad, i think this label
adresses could be avaliable/accesible.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
VHDL Goto statement ? Skybuck Flying VHDL 9 08-26-2005 01:46 PM
Re: VHDL Goto statement ? Skybuck Flying VHDL 0 08-08-2005 03:21 AM
goto statement is recommened in systemc? youngsun park VHDL 2 11-18-2003 03:47 PM



Advertisments