Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > violating procedure calling convention

Reply
Thread Tools

violating procedure calling convention

 
 
KIRAN
Guest
Posts: n/a
 
      04-13-2007
hi,

the grammer for any programming language says that when a function is
called by another function,the callee after executing it's body should
return to the point where it left in the caller..
Is there any technique to make the callee to return to some other
point(within the current process) other than the callee by changing
the call stack in callee...
My code runs on 86 processor
(If this thread is irrelevent to this group please kindly igore the
same)
thanks in advance

 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      04-13-2007
In article <(E-Mail Removed) .com>,
KIRAN <(E-Mail Removed)> wrote:
>hi,
>
>the grammer for any programming language says that when a function is
>called by another function,the callee after executing it's body should
>return to the point where it left in the caller..


The grammar says no such thing. The grammar only describes syntax;
what happens when it runs is a matter of semantics.

>Is there any technique to make the callee to return to some other
>point(within the current process) other than the callee by changing
>the call stack in callee...


Use INTERCAL, which has a COME FROM statement for precisely this purpose.

Alternatively, use a functional programming language that lets you use
explicit continuations and invoke a continuation instead of returning.

Both of these can be done in (correct, portable) C if you try hard enough,
but You Don't Want To Do That. Either re-frame the problem to fit your
tools or choose your tools to fit the problem.


dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)

He doesn't strike me as a learner, somehow. Let's hope we're wrong.
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
 
 
 
Tak-Shing Chan
Guest
Posts: n/a
 
      04-13-2007
On Fri, 13 Apr 2007, Dave Vandervies wrote:

> In article <(E-Mail Removed) .com>,
> KIRAN <(E-Mail Removed)> wrote:
>> hi,
>>
>> the grammer for any programming language says that when a function is
>> called by another function,the callee after executing it's body should
>> return to the point where it left in the caller..

>
> The grammar says no such thing. The grammar only describes syntax;
> what happens when it runs is a matter of semantics.
>
>> Is there any technique to make the callee to return to some other
>> point(within the current process) other than the callee by changing
>> the call stack in callee...

>
> Use INTERCAL, which has a COME FROM statement for precisely this purpose.
>
> Alternatively, use a functional programming language that lets you use
> explicit continuations and invoke a continuation instead of returning.
>
> Both of these can be done in (correct, portable) C if you try hard enough,
> but You Don't Want To Do That. Either re-frame the problem to fit your
> tools or choose your tools to fit the problem.


I don't quite agree with this anti-goto mentality.

In some cases, setjmp and longjmp are actually better than
reframing the problem.

Tak-Shing
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      04-13-2007
In article <(E-Mail Removed) .uk>,
Tak-Shing Chan <(E-Mail Removed)> wrote:
>On Fri, 13 Apr 2007, Dave Vandervies wrote:
>
>> In article <(E-Mail Removed) .com>,
>> KIRAN <(E-Mail Removed)> wrote:


>>> Is there any technique to make the callee to return to some other
>>> point(within the current process) other than the callee by changing
>>> the call stack in callee...

>>
>> Use INTERCAL, which has a COME FROM statement for precisely this purpose.
>>
>> Alternatively, use a functional programming language that lets you use
>> explicit continuations and invoke a continuation instead of returning.
>>
>> Both of these can be done in (correct, portable) C if you try hard enough,
>> but You Don't Want To Do That. Either re-frame the problem to fit your
>> tools or choose your tools to fit the problem.

>
> I don't quite agree with this anti-goto mentality.


I see no anti-goto mentality here.


> In some cases, setjmp and longjmp are actually better than
>reframing the problem.


There's not enough information to be certain, but the way the question
was stated is a pretty strong hint that the OP is looking for something
less restricted than setjmp/longjmp.


dave

--
Dave Vandervies (E-Mail Removed)
That only tells you the time of execution. To calculate the speed, you will
also need to know the distance the program travels within that time.
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      04-13-2007

"Dave Vandervies" <(E-Mail Removed)> wrote in message
news:evofbi$etf$(E-Mail Removed)...
> In article <(E-Mail Removed) .uk>,
> Tak-Shing Chan <(E-Mail Removed)> wrote:
>>On Fri, 13 Apr 2007, Dave Vandervies wrote:
>>
>>> In article <(E-Mail Removed) .com>,
>>> KIRAN <(E-Mail Removed)> wrote:

>
>>>> Is there any technique to make the callee to return to some other
>>>> point(within the current process) other than the callee by changing
>>>> the call stack in callee...
>>>
>>> Use INTERCAL, which has a COME FROM statement for precisely this
>>> purpose.
>>>
>>> Alternatively, use a functional programming language that lets you use
>>> explicit continuations and invoke a continuation instead of returning.
>>>
>>> Both of these can be done in (correct, portable) C if you try hard
>>> enough,
>>> but You Don't Want To Do That. Either re-frame the problem to fit your
>>> tools or choose your tools to fit the problem.

>>
>> I don't quite agree with this anti-goto mentality.

>
> I see no anti-goto mentality here.
>
>
>> In some cases, setjmp and longjmp are actually better than
>>reframing the problem.

>
> There's not enough information to be certain, but the way the question
> was stated is a pretty strong hint that the OP is looking for something
> less restricted than setjmp/longjmp.
>
>
> dave


But you commented that is can be done in correct, portable C.
Would you care to elaborate?


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-13-2007
KIRAN wrote:
> hi,
>
> the grammer for any programming language says that when a function is
> called by another function,the callee after executing it's body should
> return to the point where it left in the caller..
> Is there any technique to make the callee to return to some other
> point(within the current process) other than the callee by changing
> the call stack in callee...
> My code runs on 86 processor
> (If this thread is irrelevent to this group please kindly igore the
> same)
> thanks in advance


what you seem to want to do would, in some C-like language, look like
(in one of many variations):

LABEL caller_first_return, caller_second_return; /* ALGOL-like type */

void called(/* parameters */
{
int return_code;
/* some code */
return_to return_code == 0 ? caller_first_return : caller_second_return;
}

void caller(/* parameters */)
{
/* some code */
called(/* arguments */
caller_first_return:
/* block 1 */
caller_second_return:
/* blocl 2 */
}

Now, let's consider how this might be accomplished in C. Here is one of
many answers:


int called(/* parameters */
{
int return_code;
/* some code */
return return_code;
}

void caller(/* parameters */)
{
/* some code */
switch(called(/* arguments */) {
case 0:
/* block 1 */
case 1:
/* blocl 2 */
}
}

If the blocks are too large, then using function calls or goto
statements in the switch body can do the job.
Note that the C switch statement (unlike Pascal, for example) allows you
to drop through from case 0 to case 1, so the code you wanted to write
needs little modification other than that seen above.

 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      04-13-2007
In article <(E-Mail Removed)>,
Barry <(E-Mail Removed)> wrote:
>
>"Dave Vandervies" <(E-Mail Removed)> wrote in message
>news:evofbi$etf$(E-Mail Removed)...


>>>On Fri, 13 Apr 2007, Dave Vandervies wrote:


>>>> Alternatively, use a functional programming language that lets you use
>>>> explicit continuations and invoke a continuation instead of returning.
>>>>
>>>> Both of these can be done in (correct, portable) C if you try hard
>>>> enough,
>>>> but You Don't Want To Do That. Either re-frame the problem to fit your
>>>> tools or choose your tools to fit the problem.



>> There's not enough information to be certain, but the way the question
>> was stated is a pretty strong hint that the OP is looking for something
>> less restricted than setjmp/longjmp.


>But you commented that is can be done in correct, portable C.
>Would you care to elaborate?


Make the program use explicit continuations, then do it the same way
you'd do it if explicit continuations were built into the langauge.
(Doing this in C will require at least a partial CPS transform, since
any code that can cross a continuation capture or invocation boundary
needs to be able to return to the top-level trampoline loop and whatever
comes after that point needs to be accessible from the top level.)


So the program would run from a trampoline loop that looks not entirely
unlike:
--------
while(next_continuation)
{
next_continuation=invoke_continuation(next_continu ation);
}
--------

Then a normal function call would look something like:
--------
/*cont.args needs to be populated; for simplicity I'll assume
it's a void function
*/
cont->nargs=1; /*only the return continuation, no "real" args*/
cont->args[0]=magic_conversion(what_happens_next,current_state) ;
cont->func=called_function;
return cont;
--------
The called function would need to expect a return continuation as one
of its arguments, and it would return to its caller by doing:
--------
/*ret_cont is the continuation we got as an argument*/
ret_cont->nargs=1;
ret_cont->args[0]=magic_conversion(return_value);
return ret_cont;
--------
(Note that this is exactly the same mechanism that we used to call a
function. In a language like Scheme, the code looks exactly the same,
and in fact doesn't even need to know at the point of the call whether
it's calling a continuation or an ordinary function.)


To allow a function to return to arbitrary points in the code, simply
create a continuation for each point it might need to return to (either
by capturing a normally-existing one or by creating one for that point)
and make all of those available to the function. Then it can decide
where it wants to return to and just return the continuation for that
return point as its "next continuation".


(Once you have this, you can implement setjmp and longjmp on top of it
fairly easily; setjmp will save its return continuation in the jmp_buf
before invoking it, and longjmp will just invoke it again. The hard
part will be making sure you don't leak any temporary storage allocated
in between, since you can't just reset the stack pointer like you could
with a more typical stack-in-contiguous-memory implementation of the
function call stack.)


This is a lot of work and you end up with ugly code that's going to
be hard to understand for anybody who hasn't been exposed to the idea
before. Far better to just write code that needs it in a language that
has built-in support for it.


dave
(The biggest strength of C is that you can create any high-level
abstraction you want to use yourself; the biggest weakness of C is that
you have to create any high-level abstraction you want to use yourself.)

--
Dave Vandervies (E-Mail Removed)
Basically, there is no control structure you can imagine that
can't be implemented using call/cc. Even very silly ones.
--Bear in comp.lang.scheme
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      04-13-2007

"Dave Vandervies" <(E-Mail Removed)> wrote in message
news:evoo65$ide$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> Barry <(E-Mail Removed)> wrote:
>>
>>"Dave Vandervies" <(E-Mail Removed)> wrote in message
>>news:evofbi$etf$(E-Mail Removed)...

>
>>>>On Fri, 13 Apr 2007, Dave Vandervies wrote:

>
>>>>> Alternatively, use a functional programming language that lets you use
>>>>> explicit continuations and invoke a continuation instead of returning.
>>>>>
>>>>> Both of these can be done in (correct, portable) C if you try hard
>>>>> enough,
>>>>> but You Don't Want To Do That. Either re-frame the problem to fit
>>>>> your
>>>>> tools or choose your tools to fit the problem.

>
>
>>> There's not enough information to be certain, but the way the question
>>> was stated is a pretty strong hint that the OP is looking for something
>>> less restricted than setjmp/longjmp.

>
>>But you commented that is can be done in correct, portable C.
>>Would you care to elaborate?

>
> Make the program use explicit continuations, then do it the same way
> you'd do it if explicit continuations were built into the langauge.
> (Doing this in C will require at least a partial CPS transform, since
> any code that can cross a continuation capture or invocation boundary
> needs to be able to return to the top-level trampoline loop and whatever
> comes after that point needs to be accessible from the top level.)
>
>
> So the program would run from a trampoline loop that looks not entirely
> unlike:
> --------
> while(next_continuation)
> {
> next_continuation=invoke_continuation(next_continu ation);
> }
> --------
>
> Then a normal function call would look something like:
> --------
> /*cont.args needs to be populated; for simplicity I'll assume
> it's a void function
> */
> cont->nargs=1; /*only the return continuation, no "real" args*/
> cont->args[0]=magic_conversion(what_happens_next,current_state) ;
> cont->func=called_function;
> return cont;
> --------
> The called function would need to expect a return continuation as one
> of its arguments, and it would return to its caller by doing:
> --------
> /*ret_cont is the continuation we got as an argument*/
> ret_cont->nargs=1;
> ret_cont->args[0]=magic_conversion(return_value);
> return ret_cont;
> --------
> (Note that this is exactly the same mechanism that we used to call a
> function. In a language like Scheme, the code looks exactly the same,
> and in fact doesn't even need to know at the point of the call whether
> it's calling a continuation or an ordinary function.)
>
>
> To allow a function to return to arbitrary points in the code, simply
> create a continuation for each point it might need to return to (either
> by capturing a normally-existing one or by creating one for that point)
> and make all of those available to the function. Then it can decide
> where it wants to return to and just return the continuation for that
> return point as its "next continuation".
>
>
> (Once you have this, you can implement setjmp and longjmp on top of it
> fairly easily; setjmp will save its return continuation in the jmp_buf
> before invoking it, and longjmp will just invoke it again. The hard
> part will be making sure you don't leak any temporary storage allocated
> in between, since you can't just reset the stack pointer like you could
> with a more typical stack-in-contiguous-memory implementation of the
> function call stack.)
>
>
> This is a lot of work and you end up with ugly code that's going to
> be hard to understand for anybody who hasn't been exposed to the idea
> before. Far better to just write code that needs it in a language that
> has built-in support for it.
>
>
> dave
> (The biggest strength of C is that you can create any high-level
> abstraction you want to use yourself; the biggest weakness of C is that
> you have to create any high-level abstraction you want to use yourself.)
>
> --
> Dave Vandervies (E-Mail Removed)
> Basically, there is no control structure you can imagine that
> can't be implemented using call/cc. Even very silly ones.
> --Bear in comp.lang.scheme


That wasn't what the OP was asking.


 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      04-13-2007
In article <(E-Mail Removed)>,
Barry <(E-Mail Removed)> wrote:
>
>"Dave Vandervies" <(E-Mail Removed)> wrote in message
>news:evoo65$ide$(E-Mail Removed)...
>> In article <(E-Mail Removed)>,
>> Barry <(E-Mail Removed)> wrote:


>>>But you commented that is can be done in correct, portable C.
>>>Would you care to elaborate?

>>
>> Make the program use explicit continuations, then do it the same way
>> you'd do it if explicit continuations were built into the langauge.


(snip details of how to implement explicit continuations in C)


>That wasn't what the OP was asking.


The OP was asking how to make the called function return somewhere else.

If you have continuations, just write it in CPS and give it a continuation
for wherever you want it to return to.

You were the one who asked how to do it in C, which is what I gave you.


dave

--
Dave Vandervies (E-Mail Removed)
But my brain doesn't allow that (in the sense that I would not trust myself
to be sure of what's actually happening), so I wouldn't even want to do it.
--Richard Heathfield in comp.programming
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      04-13-2007

"Dave Vandervies" <(E-Mail Removed)> wrote in message
news:evorgt$jnd$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> Barry <(E-Mail Removed)> wrote:
>>
>>"Dave Vandervies" <(E-Mail Removed)> wrote in message
>>news:evoo65$ide$(E-Mail Removed)...
>>> In article <(E-Mail Removed)>,
>>> Barry <(E-Mail Removed)> wrote:

>
>>>>But you commented that is can be done in correct, portable C.
>>>>Would you care to elaborate?
>>>
>>> Make the program use explicit continuations, then do it the same way
>>> you'd do it if explicit continuations were built into the langauge.

>
> (snip details of how to implement explicit continuations in C)
>
>
>>That wasn't what the OP was asking.

>
> The OP was asking how to make the called function return somewhere else.
>
> If you have continuations, just write it in CPS and give it a continuation
> for wherever you want it to return to.
>
> You were the one who asked how to do it in C, which is what I gave you.
>
>


No, the OP asked how to return to an arbitrary statement in the C code.
Or that is how I interpreted the question.




 
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
Is Microsoft Violating Some Patents Covering Open Source? GraB NZ Computing 1 11-22-2006 07:32 PM
Violating sequence point? Frederick Gotham C Programming 15 07-06-2006 03:05 AM
Violating Sequence Point? Frederick Gotham C++ 5 07-05-2006 04:19 PM
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. bazzer ASP .Net 1 04-06-2006 01:31 PM
SMC violating Microsoft Licences agreements? ilovemyview@yahoo.com ASP .Net 4 09-06-2005 10:27 AM



Advertisments