Please explain me this C code fragment

This is a discussion on Please explain me this C code fragment within the C Programming forums, part of the General Programming Boards category; Hi all! Can anyone explain me fine the following code? extern uint8_t code_gen_prologue[]; #define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr) in ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Question Please explain me this C code fragment

    Hi all!
    Can anyone explain me fine the following code?

    extern uint8_t code_gen_prologue[];
    #define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)

    in another file of the same project:

    uint8_t code_gen_prologue[1024] code_gen_section;

    Thank you very much in advance!
    Stefano B.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Code:
    extern uint8_t code_gen_prologue[];
    This is declaration of a variable with external linkage named "code_gen_prologue" which is an array of uint8_t (single-byte unsigned integer). It just informs the compiler that there is such a variable.

    Code:
    uint8_t code_gen_prologue[1024] code_gen_section;
    This is definition of the above variable (here the variable is really "created" for the linker).

    Code:
    #define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)
    Here is a nasty macrodefinition which simply calls machine code held by code_gen_prologie, passing there some parameter (tb_ptr).

    Summary:
    code_gen_prologue is a pure machine code containing some function, if you want to call this function you need to use the macro.
    Last edited by kmdv; 12-16-2010 at 10:29 AM.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Question

    Hi!
    Thank you for your quick answer!

    uint8_t code_gen_prologue[1024] code_gen_section;
    I don't understand; if it was only:

    Code:
    uint8_t code_gen_prologue[1024]
    It would be an array of 1024 uint8_t .. what is the third name?
    I am used to defining variables as:

    PHP Code:
    type identifier 
    what is the third name?

    a nasty macrodefinition which simply calls machine code held by code_gen_prologie, passing there some parameter (tb_ptr).
    Do you mean it is enough that cryptic line for the process to execute binary code contained in that address?? o.O
    Please I beg you, I really need to understand how this happens ..
    I really can't understand that line
    what do you mean by passing there some parameter? What does that parameter do?

    Thank you very much again!

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Quote Originally Posted by stefboombastic View Post
    Hi!
    It would be an array of 1024 uint8_t .. what is the third name?
    code_gen_section is probably a macro expanding to some initialization routine.

    Quote Originally Posted by stefboombastic View Post
    Hi!
    Do you mean it is enough that cryptic line for the process to execute binary code contained in that address?? o.O
    This is a simple macrodefinition. I will not be explaining what macrodefinitions are. Google this and of course 'function pointers'.
    Code:
    ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)
    (long REGPARM (*)(void *)) is a type: a pointer pointing to a function, which takes one (void*) parameter. code_gen_prologue is an array, array's name when used is considered to be a pointer to its first element, thus you are casting here pointer to the first byte of array to pointer to function (...).
    Ellipsis with tb_ptr mean 'call function under this address and pass there whatever tb_ptr is'

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Thumbs up

    Thaaaaank you!!!!!!!!!!!!!!! You really rock man!!!!

    code_gen_section is probably a macro expanding to some initialization routine.
    yup it was a macro.. a compiler directive for alignment! How silly of me! I really didn't think it could be another macro ..

    (long REGPARM (*)(void *)) is a type: a pointer pointing to a function ...
    Yeeeeees!!! Thank you your explanation is super clear!!!
    I know perfectly what a pointer to a function is .. if it was
    Code:
    long REGPARM (*myf)(void *)
    I'd recognize it immediately .. but it was enough having only the * for confusing me :[

    This is a simple macrodefinition. I will not be explaining what macrodefinitions are. Google this
    Do you mean "macro definition", that is the simple definition of a macro? Or does it exist something called "macrodefinition"?

    Ellipsis with tb_ptr mean 'call function under this address and pass there whatever tb_ptr is'
    So at that address there is just the binary translation of a function that takes a void* as parameter and return long REGPARM, that's why that call works, isn't it?

    Super thank you!!!

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Quote Originally Posted by stefboombastic View Post
    Thaaaaank you!!!!!!!!!!!!!!! You really rock man!!!!
    Do you mean "macro definition", that is the simple definition of a macro? Or does it exist something called "macrodefinition"?
    Macro definition.

    Quote Originally Posted by stefboombastic View Post
    So at that address there is just the binary translation of a function that takes a void* as parameter and return long REGPARM, that's why that call works, isn't it?
    Yes, but REGPARM is probably a macro to calling convention. It's not a type, but way the program passes arguments to function.
    I never put signature, but I decided to make an exception.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Thumbs up

    Yes, but REGPARM is probably a macro to calling convention. It's not a type, but way the program passes arguments to function.
    Yup exactly!

    Wow! you managed to solve in few words doubts I've been wasting days for!
    If you lived around here I'd surely pay you the best drink ever!
    Thank you for all your help!
    Best Regards!
    Stefano B.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about red-black tree and little code fragment.
    By mindvera in forum C++ Programming
    Replies: 0
    Last Post: 07-05-2010, 11:42 AM
  2. Can anyone explain what this section code is doing ?
    By zidangus in forum C Programming
    Replies: 8
    Last Post: 06-22-2010, 05:25 AM
  3. Explain working of the code
    By varadharajan in forum C Programming
    Replies: 9
    Last Post: 02-09-2010, 01:51 PM
  4. explain this code please
    By lastresort in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2005, 11:02 AM
  5. code fragment pls help
    By Damo in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21