avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] More observations: array of pointers to functions


From: Joerg Wunsch
Subject: Re: [avr-gcc-list] More observations: array of pointers to functions
Date: Mon, 26 Feb 2007 07:09:10 +0100 (MET)

David McNab <address@hidden> wrote:

>   // define a macro to create a 'function pointer table handle'
>   #define pgm_get_functab(tab) (FuncPtrTable)(&tab[0])

&tab[0] is the same as tab.

> I tried the approach of:

>    fptr = (FuncPtr)pgm_read_word(&funcs_table[0] + i * 2);

> but it has a huge footprint of 48 bytes ...

Does it?

foo.c:

#include <avr/pgmspace.h>

typedef  void (*FuncPtr)(void);
void func1(void);
void func2(void);
void func3(void);

FuncPtr funcs_table[] PROGMEM = {
    func1,
    func2,
    func3
};

void
dorun(int i)
{
  FuncPtr fptr;

  fptr = (FuncPtr)pgm_read_word(funcs_table + i);
  fptr();
}

yields:

..global dorun
        .type   dorun, @function
dorun:
        movw r30,r24
        lsl r30
        rol r31
        subi r30,lo8(-(funcs_table))
        sbci r31,hi8(-(funcs_table))
        lpm r24, Z+
        lpm r25, Z
        movw r30,r24
        icall
        ret

Btw., you apparently have problems still understanding how pointers
are calculated in C.  The complicated "&tab[0]" construct is a slight
indication of that, but you've also made the error of trying to
manually multiply by 2 in your access macro.  I left that off, as it
would have given completely wrong code.

-- 
cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]