为什么这些方法会使我的程序变得更大?

Ada*_*ile 2 c c++ function arduino

我正在处理一些Arduino代码并具有以下代码:

uint8_t world[24][2][3];
bool getDispPixel(uint8_t x, uint8_t y, uint8_t num)
{
    static uint8_t rowByte = 0; // 0 means top 8, 1 means bottom 8
    static uint8_t rowBit = 0;

    if(y > 7)
    {
        rowByte = 1;
        rowBit = x - 8;
    }
    else
    {
        rowByte = 0;
        rowBit = x;
    }

    return (world[x][rowByte][num] & (1 << rowBit)) > 0;
}

void setDispPixel(uint8_t x, uint8_t y, uint8_t num, bool state)
{
    static uint8_t rowByte = 0; // 0 means top 8, 1 means bottom 8
    static uint8_t rowBit = 0;

    if(y > 7)
    {
        rowByte = 1;
        rowBit = x - 8;
    }
    else
    {
        rowByte = 0;
        rowBit = x;
    }

    if(state)
        world[x][rowByte][num] |= (1 << rowBit);
    else
        world[x][rowByte][num] &= ~(1 << rowBit);
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是这些方法为程序添加了大小的TON.甚至只是它的一部分.如果我只从其中一个方法中注释掉以下部分,它会从程序大小中删除2536个字节!

if(y > 7)
{
    rowByte = 1;
    rowBit = x - 8;
}
else
{
    rowByte = 0;
    rowBit = x;
}
Run Code Online (Sandbox Code Playgroud)

两种方法都经常被调用,超过200次组合.如果它们被标记为内联,我会相信它,但它们不是.什么可能导致这个?

更新:如果我完全注释掉这些方法的内容,它会将大小减少20k!看起来每次调用该函数都会占用94个字节.不明白为什么......

Joe*_*e Z 5

如果Arduino工具链支持GCC扩展(并且一些快速搜索表明它确实如此),那么您可以使用__attribute__((noinline))这些函数禁用内联,如下所示:

bool getDispPixel(uint8_t x, uint8_t y, uint8_t num) __attribute__((noinline));
bool getDispPixel(uint8_t x, uint8_t y, uint8_t num)
{
    // body of the function here
}

void setDispPixel(uint8_t x, uint8_t y, uint8_t num, bool state) __attribute((noinline));
void setDispPixel(uint8_t x, uint8_t y, uint8_t num, bool state)
{
    // body of the function here
}
Run Code Online (Sandbox Code Playgroud)

额外的线看起来多余,但不是.这是扩展的语法如何工作.