public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* functions calls Vs calls using function pointers
@ 2006-05-24 16:54 Digvijoy Chatterjee
  2006-05-25 10:46 ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Digvijoy Chatterjee @ 2006-05-24 16:54 UTC (permalink / raw)
  To: gcc-help

Hello,
I am trying to understand how cc1 works  ,and was going through the
gcc-4.0.3 source , and after a lot of research , found  functions for
parsing Declaration/Definitions(gcc-4.0.3/gcc/c-decl.c:
grokdeclarator) and "actual function calls" used inside a translation
unit ,in (gcc-4.0.3/gcc/calls.c:prepare_call_address )in the C
compilation process .

The problem however is when i choose to call a function using
function-pointers ,the name of the function pointer appears as "-" in
this prepare_call_address function (as seen in gdb). Can anyone point
me to a C file/function in the gcc source tree where the compiler
knows the exact  function-pointer name being used to call a function.
typedef void (*fptr) (int);
void A(int);
int main()
{
fptr fp;
fp=A;
fp();
}
void A(int i){printf("in A\n");}

when i try and compile this , using gcc, its quick to point an error :
too few arguments to fp;

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: functions calls Vs calls using function pointers
  2006-05-24 16:54 functions calls Vs calls using function pointers Digvijoy Chatterjee
@ 2006-05-25 10:46 ` Andrew Haley
  2006-05-25 11:07   ` Digvijoy Chatterjee
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Haley @ 2006-05-25 10:46 UTC (permalink / raw)
  To: Digvijoy Chatterjee; +Cc: gcc-help

Digvijoy Chatterjee writes:

 > I am trying to understand how cc1 works ,and was going through the
 > gcc-4.0.3 source , and after a lot of research , found functions
 > for parsing Declaration/Definitions(gcc-4.0.3/gcc/c-decl.c:
 > grokdeclarator) and "actual function calls" used inside a
 > translation unit ,in (gcc-4.0.3/gcc/calls.c:prepare_call_address
 > )in the C compilation process .
 > 
 > The problem however is when i choose to call a function using
 > function-pointers ,the name of the function pointer appears as "-"
 > in this prepare_call_address function (as seen in gdb). Can anyone
 > point me to a C file/function in the gcc source tree where the
 > compiler knows the exact function-pointer name being used to call a
 > function.

The problem I'm having with this (and probably everyone else is as
well) is that I don't really know what you're asking.

prepare_call_address() is called a long time after the C source code
has been parsed.  It's part of the code generation phase.

prepare_call_address() isn't passed a variable, it's passed an RTX.
That RTX can either be a symbol_ref or something more complex.  As
long as it, when executed, produces an address that's OK.

If you want to see the expression that is used to form the address,
put your breakpoint on expand_call() and look at the exp.

 > typedef void (*fptr) (int);
 > void A(int);
 > int main()
 > {
 > fptr fp;
 > fp=A;
 > fp();
 > }
 > void A(int i){printf("in A\n");}
 > 
 > when i try and compile this , using gcc, its quick to point an error :
 > too few arguments to fp;

Correct.  What's your question?

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: functions calls Vs calls using function pointers
  2006-05-25 10:46 ` Andrew Haley
@ 2006-05-25 11:07   ` Digvijoy Chatterjee
  2006-05-25 11:17     ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Digvijoy Chatterjee @ 2006-05-25 11:07 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc-help

On 5/25/06, Andrew Haley <aph@redhat.com> wrote:
> Digvijoy Chatterjee writes:
>
>  > I am trying to understand how cc1 works ,and was going through the
>  > gcc-4.0.3 source , and after a lot of research , found functions
>  > for parsing Declaration/Definitions(gcc-4.0.3/gcc/c-decl.c:
>  > grokdeclarator) and "actual function calls" used inside a
>  > translation unit ,in (gcc-4.0.3/gcc/calls.c:prepare_call_address
>  > )in the C compilation process .
>  >
>  > The problem however is when i choose to call a function using
>  > function-pointers ,the name of the function pointer appears as "-"
>  > in this prepare_call_address function (as seen in gdb). Can anyone
>  > point me to a C file/function in the gcc source tree where the
>  > compiler knows the exact function-pointer name being used to call a
>  > function.
>
> The problem I'm having with this (and probably everyone else is as
> well) is that I don't really know what you're asking.
>
> prepare_call_address() is called a long time after the C source code
> has been parsed.  It's part of the code generation phase.
>
> prepare_call_address() isn't passed a variable, it's passed an RTX.
> That RTX can either be a symbol_ref or something more complex.  As
> long as it, when executed, produces an address that's OK.
>
> If you want to see the expression that is used to form the address,
> put your breakpoint on expand_call() and look at the exp.
>
>  > typedef void (*fptr) (int);
>  > void A(int);
>  > int main()
>  > {
>  > fptr fp;
>  > fp=A;
>  > fp();
>  > }
>  > void A(int i){printf("in A\n");}
>  >
>  > when i try and compile this , using gcc, its quick to point an error :
>  > too few arguments to fp;
>
> Correct.  What's your question?
>
> Andrew.
>
The question is which function in which .c file of  the gcc source
tree , understands the  fact that a call to fp is actually a call to A
, and therefore complains about wrong arguments.
I want the name of the function where the compiler while parsing a .c
file associates a function pointer ,to the function which will be
called at runtime.
As an example the "prepare_call_address" in line no:167(approx) in
gcc-4.0.3/gcc/calls.c in the gcc source knows when a certain function
A is called in  main,but it doesnt know anything about fp, if i try to
print names of called functions in prepare_call_address  it prints "-"
and not fp nor A,  for an actual function call , it prints the name.
I hope i made myself clear.
Digz

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: functions calls Vs calls using function pointers
  2006-05-25 11:07   ` Digvijoy Chatterjee
@ 2006-05-25 11:17     ` Andrew Haley
       [not found]       ` <9dd10b1d0605250810v4db97920vec47ba85d02b70e6@mail.gmail.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Haley @ 2006-05-25 11:17 UTC (permalink / raw)
  To: Digvijoy Chatterjee; +Cc: gcc-help

Digvijoy Chatterjee writes:
 > On 5/25/06, Andrew Haley <aph@redhat.com> wrote:
 > > Digvijoy Chatterjee writes:
 > >
 > >  > I am trying to understand how cc1 works ,and was going through the
 > >  > gcc-4.0.3 source , and after a lot of research , found functions
 > >  > for parsing Declaration/Definitions(gcc-4.0.3/gcc/c-decl.c:
 > >  > grokdeclarator) and "actual function calls" used inside a
 > >  > translation unit ,in (gcc-4.0.3/gcc/calls.c:prepare_call_address
 > >  > )in the C compilation process .
 > >  >
 > >  > The problem however is when i choose to call a function using
 > >  > function-pointers ,the name of the function pointer appears as "-"
 > >  > in this prepare_call_address function (as seen in gdb). Can anyone
 > >  > point me to a C file/function in the gcc source tree where the
 > >  > compiler knows the exact function-pointer name being used to call a
 > >  > function.
 > >
 > > The problem I'm having with this (and probably everyone else is as
 > > well) is that I don't really know what you're asking.
 > >
 > > prepare_call_address() is called a long time after the C source code
 > > has been parsed.  It's part of the code generation phase.
 > >
 > > prepare_call_address() isn't passed a variable, it's passed an RTX.
 > > That RTX can either be a symbol_ref or something more complex.  As
 > > long as it, when executed, produces an address that's OK.
 > >
 > > If you want to see the expression that is used to form the address,
 > > put your breakpoint on expand_call() and look at the exp.
 > >
 > >  > typedef void (*fptr) (int);
 > >  > void A(int);
 > >  > int main()
 > >  > {
 > >  > fptr fp;
 > >  > fp=A;
 > >  > fp();
 > >  > }
 > >  > void A(int i){printf("in A\n");}
 > >  >
 > >  > when i try and compile this , using gcc, its quick to point an error :
 > >  > too few arguments to fp;
 > >
 > > Correct.  What's your question?

 > The question is which function in which .c file of  the gcc source
 > tree , understands the  fact that a call to fp is actually a call to A
 > , and therefore complains about wrong arguments.

That's not what happens.

You declare fp to be type void (*fptr) (int).

Therefore if you call a function through fp, you have to pass an int.
This is because of the type of variable fp, not because of A.

You can easily demonstrate this by compiling:

typedef void (*fptr) (int);

int main()
{
fptr fp;
fp();
}

 > I want the name of the function where the compiler while parsing a .c
 > file associates a function pointer ,to the function which will be
 > called at runtime.

This never happens, and therefore I can't answer your question.

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: functions calls Vs calls using function pointers
       [not found]       ` <9dd10b1d0605250810v4db97920vec47ba85d02b70e6@mail.gmail.com>
@ 2006-05-25 15:12         ` Digvijoy Chatterjee
  2006-05-25 15:51           ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Digvijoy Chatterjee @ 2006-05-25 15:12 UTC (permalink / raw)
  To: gcc-help

n 5/25/06, Andrew Haley <aph@redhat.com> wrote:
> Digvijoy Chatterjee writes:
>  > On 5/25/06, Andrew Haley <aph@redhat.com> wrote:
>  > > Digvijoy Chatterjee writes:
>  > >
>  > >  > I am trying to understand how cc1 works ,and was going through the
>  > >  > gcc-4.0.3 source , and after a lot of research , found functions
>  > >  > for parsing Declaration/Definitions(gcc-4.0.3/gcc/c-decl.c:
>  > >  > grokdeclarator) and "actual function calls" used inside a
>  > >  > translation unit ,in (gcc-4.0.3/gcc/calls.c:prepare_call_address
>  > >  > )in the C compilation process .
>  > >  >
>  > >  > The problem however is when i choose to call a function using
>  > >  > function-pointers ,the name of the function pointer appears as "-"
>  > >  > in this prepare_call_address function (as seen in gdb). Can anyone
>  > >  > point me to a C file/function in the gcc source tree where the
>  > >  > compiler knows the exact function-pointer name being used to call a
>  > >  > function.
>  > >
>  > > The problem I'm having with this (and probably everyone else is as
>  > > well) is that I don't really know what you're asking.
>  > >
>  > > prepare_call_address() is called a long time after the C source code
>  > > has been parsed.  It's part of the code generation phase.
>  > >
>  > > prepare_call_address() isn't passed a variable, it's passed an RTX.
>  > > That RTX can either be a symbol_ref or something more complex.  As
>  > > long as it, when executed, produces an address that's OK.
>  > >
>  > > If you want to see the expression that is used to form the address,
>  > > put your breakpoint on expand_call() and look at the exp.
>  > >
>  > >  > typedef void (*fptr) (int);
>  > >  > void A(int);
>  > >  > int main()
>  > >  > {
>  > >  > fptr fp;
>  > >  > fp=A;
>  > >  > fp();
>  > >  > }
>  > >  > void A(int i){printf("in A\n");}
>  > >  >
>  > >  > when i try and compile this , using gcc, its quick to point an error :
>  > >  > too few arguments to fp;
>  > >
>  > > Correct.  What's your question?
>
>  > The question is which function in which .c file of  the gcc source
>  > tree , understands the  fact that a call to fp is actually a call to A
>  > , and therefore complains about wrong arguments.
>
> That's not what happens.
>
> You declare fp to be type void (*fptr) (int).
>
> Therefore if you call a function through fp, you have to pass an int.
> This is because of the type of variable fp, not because of A.
>
> You can easily demonstrate this by compiling:
>
> typedef void (*fptr) (int);
>
> int main()
> {
> fptr fp;
> fp();
> }
>
>  > I want the name of the function where the compiler while parsing a .c
>  > file associates a function pointer ,to the function which will be
>  > called at runtime.
>
> This never happens, and therefore I can't answer your question.
>
> Andrew.
>
Right ,
Which is the file then which has code , to compare the erroneous call
fp(); to its declaration as in the typedef ?where does the compiler
know fp() is a function call ?
Regards
Digz

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: functions calls Vs calls using function pointers
  2006-05-25 15:12         ` Digvijoy Chatterjee
@ 2006-05-25 15:51           ` Andrew Haley
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2006-05-25 15:51 UTC (permalink / raw)
  To: Digvijoy Chatterjee; +Cc: gcc-help

Digvijoy Chatterjee writes:
 > In 5/25/06, Andrew Haley <aph@redhat.com> wrote:
 > >
 > > You declare fp to be type void (*fptr) (int).
 > >
 > > Therefore if you call a function through fp, you have to pass an int.
 > > This is because of the type of variable fp, not because of A.
 > >
 > > You can easily demonstrate this by compiling:
 > >
 > > typedef void (*fptr) (int);
 > >
 > > int main()
 > > {
 > > fptr fp;
 > > fp();
 > > }
 > >
 > >  > I want the name of the function where the compiler while parsing a .c
 > >  > file associates a function pointer ,to the function which will be
 > >  > called at runtime.
 > >
 > > This never happens, and therefore I can't answer your question.
 > >
 > > Andrew.
 > >
 > Right ,
 > Which is the file then which has code , to compare the erroneous call
 > fp(); to its declaration as in the typedef 

Look at convert_arguments in c-typeck.c.

 > ?where does the compiler know fp() is a function call ?

	case CPP_OPEN_PAREN:
	  /* Function call.  */
	  c_parser_consume_token (parser);
	  if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
	    exprlist = NULL_TREE;
	  else
	    exprlist = c_parser_expr_list (parser, true);
	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
				     "expected %<)%>");
	  expr.value = build_function_call (expr.value, exprlist);
	  expr.original_code = ERROR_MARK;
	  break;


in c-parser.c.

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-05-25 15:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-24 16:54 functions calls Vs calls using function pointers Digvijoy Chatterjee
2006-05-25 10:46 ` Andrew Haley
2006-05-25 11:07   ` Digvijoy Chatterjee
2006-05-25 11:17     ` Andrew Haley
     [not found]       ` <9dd10b1d0605250810v4db97920vec47ba85d02b70e6@mail.gmail.com>
2006-05-25 15:12         ` Digvijoy Chatterjee
2006-05-25 15:51           ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).