Jump to address syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guillaume Dargaud

    Jump to address syntax

    Hello all,
    I have an example of working code under my eyes that goes as follow:

    unsigned long address=0x40000 0;
    (void (*)(void)addres s)();

    It's supposed to jump start a kernel loaded at that address from a small
    bootloader.

    But my cross compiler chokes on the second line (89) and I must say I've
    tried variations of the syntax without success:

    TestGpio/TestGpio.c: In function 'main':
    TestGpio/TestGpio.c:89: error: expected ')' before numeric constant
    TestGpio/TestGpio.c:89: error: expected expression before ')' token

    It would make more sense to me as:
    ((void *)address)();
    TestGpio/TestGpio.c: In function 'main':
    TestGpio/TestGpio.c:90: error: called object '4194304u' is not a function

    Maybe this one ?:
    ((void *)(void)address )();
    TestGpio/TestGpio.c: In function 'main':
    TestGpio/TestGpio.c:90: error: void value not ignored as it ought to be

    --
    Guillaume Dargaud



  • Robert Gamble

    #2
    Re: Jump to address syntax

    On Jul 16, 6:44 am, "Guillaume Dargaud"
    <use_the_form_o n_my_contact_p. ..@www.gdargaud .netwrote:
    Hello all,
    I have an example of working code under my eyes that goes as follow:
    >
    unsigned long address=0x40000 0;
    (void (*)(void)addres s)();
    >
    It's supposed to jump start a kernel loaded at that address from a small
    bootloader.
    >
    But my cross compiler chokes on the second line (89) and I must say I've
    tried variations of the syntax without success:
    >
    TestGpio/TestGpio.c: In function 'main':
    TestGpio/TestGpio.c:89: error: expected ')' before numeric constant
    TestGpio/TestGpio.c:89: error: expected expression before ')' token
    >
    It would make more sense to me as:
    ((void *)address)();
    TestGpio/TestGpio.c: In function 'main':
    TestGpio/TestGpio.c:90: error: called object '4194304u' is not a function
    >
    Maybe this one ?:
    ((void *)(void)address )();
    TestGpio/TestGpio.c: In function 'main':
    TestGpio/TestGpio.c:90: error: void value not ignored as it ought to be
    The syntax to cast address to a function pointer and then call it
    would look like this:

    ((void (*)(void))addre ss)();

    It might be clearer to do something like this though:

    void (*fptr)(void) = (void (*)(void))addre ss;
    fptr();

    --
    Robert Gamble

    Comment

    • Eric Sosman

      #3
      Re: Jump to address syntax

      Guillaume Dargaud wrote:
      Hello all,
      I have an example of working code under my eyes that goes as follow:
      >
      unsigned long address=0x40000 0;
      (void (*)(void)addres s)();
      [...]
      ITYM `((void (*)(void))addre ss)();'.

      Usually I disapprove of smuggling pointer-ness into a
      typedef'ed type, but I make an exception for function pointers
      because a pointerized typedef seems to help readability:

      typedef void (*VFptr)(void);
      ((VFptr)address )();

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • Richard Tobin

        #4
        Re: Jump to address syntax

        In article <g5kjem$ajj$1@c cpntc8.in2p3.fr >,
        >(void (*)(void)addres s)();
        You're trying to cast address to type "void (*)(void)". So you put
        that type in parentheses before the variable:

        (void (*)(void))addre ss

        and you want to call it; you'll need more parentheses to get the
        grouping right:

        ((void (*)(void))addre ss)();

        -- Richard
        --
        Please remember to mention me / in tapes you leave behind.

        Comment

        • Ben Bacarisse

          #5
          Re: Jump to address syntax

          Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
          Guillaume Dargaud wrote:
          >Hello all,
          >I have an example of working code under my eyes that goes as follow:
          >>
          >unsigned long address=0x40000 0;
          >(void (*)(void)addres s)();
          >[...]
          >
          ITYM `((void (*)(void))addre ss)();'.
          >
          Usually I disapprove of smuggling pointer-ness into a
          typedef'ed type, but I make an exception for function pointers
          because a pointerized typedef seems to help readability:
          >
          typedef void (*VFptr)(void);
          ((VFptr)address )();
          I used to make this same exception, but is

          typedef void VFptr(void);
          ((VFptr *)address)();

          really any less readable?

          (I think I got in to the habit because some pre-standard C compilers
          refused to typedef a function type. I can't find any evidence for now
          except a sense memory of surprise when I discovered that it is
          standardised. Am I misremembering? )

          --
          Ben.

          Comment

          • Guillaume Dargaud

            #6
            Re: Jump to address syntax

            >typedef void (*VFptr)(void);
            >((VFptr)addres s)();
            or
            typedef void VFptr(void);
            ((VFptr *)address)();
            Thanks, yes those two forms are a lot clearer.
            --
            Guillaume Dargaud



            Comment

            • Peter Nilsson

              #7
              Re: Jump to address syntax

              Guillaume Dargaud wrote:
              typedef void (*VFptr)(void);
              ((VFptr)address )();
              >
              or
              >
              typedef void VFptr(void);
              ((VFptr *)address)();
              This reads as a pointer to a function pointer. I'd prefer...

              typedef void VFunc(void);
              ((VFunc *)address)();
              >
              Thanks, yes those two forms are a lot clearer.
              In cases where you're calling the function often, it's clearer still
              to transfer the casts to the declaration, or hide it in a function
              macro...

              #define frobnisticate() ((VFunc *) 0x400000)
              or
              VFunc *frobnisticate = (VFunc *) 0x400000;

              frobnisticate() ;

              --
              Peter

              Comment

              • Ben Bacarisse

                #8
                Re: Jump to address syntax

                Peter Nilsson <airia@acay.com .auwrites:
                Guillaume Dargaud wrote:
                typedef void (*VFptr)(void);
                ((VFptr)address )();
                >>
                >or
                >>
                typedef void VFptr(void);
                ((VFptr *)address)();
                >
                This reads as a pointer to a function pointer. I'd prefer...
                >
                typedef void VFunc(void);
                ((VFunc *)address)();
                Yes so would I :-) That was not Guillaume Dargaud's fault -- when I
                flipped the * from the typedef to the cast I kept the name to show the
                symmetry but it would have been wiser to change the name at the same
                time.

                --
                Ben.

                Comment

                Working...