Shop OBEX P1 Docs P2 Docs Learn Events
Overly Aggressive Optimization? — Parallax Forums

Overly Aggressive Optimization?

SRLMSRLM Posts: 5,045
edited 2013-12-08 17:34 in Propeller 1
Hi All, I have a curious problem. I have the following code:
void ListFiles(const char * const parameter) {
    if (dc.ListFiles() == false) {
        //BluetoothError("SD not available.");
    }
}
bool ListFiles(void) {
        bool successFlag = false;
        ...
        if (DiskReady() == true) {
           ...
            successFlag = true;
        }
        return successFlag;
    }

When I run this program it does not work. When I change the first function to
void ListFiles(const char * const parameter) {
    dc.ListFiles();
    //if (dc.ListFiles() == false) {
        //BluetoothError("SD not available.");
    //}
}

the function works. But shouldn't the call in the conditional always be evaluated? ListFiles() can be called at any time (it's serial port controlled), so I don't see how the compiler can evaluate and reduce the function.

I'm running in -Os -mcmm. Curiously, the first function works when I enable via #define my debug serial port (which is not used in any of the functions here).

Anyway, does anybody have any hints or ideas?

Comments

  • ersmithersmith Posts: 6,054
    edited 2013-12-08 16:38
    It's a bit difficult to diagnose based on the snippets you posted -- it looks like it *should* work. Have you tried looking at the assembly language output by the compiler? Does the problem happen in -O1, or only -Os?

    Are you sure that dc.ListFiles() is declared properly in the scope of the first ListFiles? Could there be some kind of polymorphism issue?
  • SRLMSRLM Posts: 5,045
    edited 2013-12-08 16:44
    Unfortunately I can't post the full program. I figured that it might be tough to tell, but just wanted to know if I was missing anything too obvious.

    I forgot all about the assembly output. I've just had to get something working for the near term. I'll be able to dive more into this later in the week. I'll let you know what I find.

    No polymorphism here, though. The first ListFiles() is a function above main, the second ListFiles() is in an class. Besides the coincidence of sharing a name they don't have anything in common.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-12-08 17:34
    SRLM wrote:
    But shouldn't the call in the conditional always be evaluated?
    It depends, I guess. On the one hand, if there's nothing in the "then" clause to do, why evaluate the "if" part? On the other hand, if the "if" part causes side effects, it should probably still be evaluated. I'd have to side with "overaggressive," I think.

    -Phil
Sign In or Register to comment.