Volatile variables - Minix

This is a discussion on Volatile variables - Minix ; Does the volatile keyword have any affect under ACK? Im having a problem with interrupts that a variable isn't getting updated soon enough for the code to work with it, and as such it's being discarded. one suggested solution i've ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Volatile variables

  1. Volatile variables

    Does the volatile keyword have any affect under ACK? Im having a
    problem with interrupts that a variable isn't getting updated soon
    enough for the code to work with it, and as such it's being discarded.
    one suggested solution i've got is to try volatile. does with work with
    ACK though?
    -Ivan


  2. Re: Volatile variables

    In article <1141231725.524088.116550@z34g2000cwc.googlegroups. com>,
    Ivan Kelly wrote:
    >Does the volatile keyword have any affect under ACK? Im having a
    >problem with interrupts that a variable isn't getting updated soon
    >enough for the code to work with it, and as such it's being discarded.
    >one suggested solution i've got is to try volatile. does with work with
    >ACK though?


    I'm not sure whether volatile works correctly in all cases. However,
    usually ACK does the right thing without volatile (the optimizer is
    quite conservative).

    Most likely the bug is somewhere else.


    --
    That was it. Done. The faulty Monk was turned out into the desert where it
    could believe what it liked, including the idea that it had been hard done
    by. It was allowed to keep its horse, since horses were so cheap to make.
    -- Douglas Adams in Dirk Gently's Holistic Detective Agency

  3. Re: Volatile variables

    Hi Ivan,

    Ivan Kelly wrote:
    > Does the volatile keyword have any affect under ACK? Im having a
    > problem with interrupts that a variable isn't getting updated soon
    > enough for the code to work with it, and as such it's being discarded.
    > one suggested solution i've got is to try volatile. does with work with
    > ACK though?


    The short answer is yes.

    To see this, I tried the following experiment. I wrote the following
    (nonsense) code:

    [vol.c]
    void wait(int);
    void f( void ) {
    extern volatile x;
    while( x < 10 ) wait( 1 );
    }

    I then compiled this code into assembly as follows:

    > cc -c.s vol.c && mv vol.s volt.s
    > cc -c.s -Dvolatile= vol.c && mv vol.s volf.s


    and looked at the differences between volt.s and volf.s. The assembly
    code that is emitted is (with annotations of the differences):

    ..sect .text; .sect .rom; .sect .data; .sect .bss
    ..extern _f
    ..sect .text
    _f:
    push ebp
    mov ebp, esp
    I1_4:
    * push (_x) ; this code emitted with 'volatile'
    * pop edx
    * cmp edx,10

    * cmp (_x),10 ; this code emitted without 'volatile'
    jge I1_1
    push 1
    call _wait
    pop ecx
    jmp I1_4
    I1_4:
    leave

    So you can see that 'volatile' is having a direct effect on the
    assembly code that is produced - indeed, it is (correctly) forcing the
    value of x to be reloaded prior to the comparison.

    So the long anwer is also 'yes'.

    But the even longer answer is that I don't know why this is happening
    (!). Before compiling down to assembly language I tried comparing the
    differences between the intermediate EM code (i.e. using the switch
    '-c.e'). The em code is (again with the differences highlighted):

    mes 2,4,4
    exp $f
    exp $wait
    pro $f,0
    mes 9,0
    4
    loe x
    * nop ; only with 'volatile'
    loc 10
    cmi 4
    zlt *5
    bra *3
    5
    loc 1
    cal $wait
    asp 4
    bra *4
    3
    2
    1
    ret 0
    end 0
    mes 4,9,'vol.c\000'

    In other words the only difference between the intermediate EM code is
    an extra NOP, which somehow causes the 'volatile' behaviour to be
    expressed. It would be interesting to know to how...

    Kind Regards,

    Michael


  4. Re: Volatile variables

    Thanks for the reply. Seems my problem was unrelated to volatile at
    all. I had a mov where I should have had a movb. was porting at&t style
    code and thought that the trailing b was a at&t thing like the l in
    movl.

    -ivan


  5. Re: Volatile variables

    > In other words the only difference between the intermediate EM code is
    > an extra NOP, which somehow causes the 'volatile' behaviour to be
    > expressed. It would be interesting to know to how...


    It appears that NOP doesn't really mean 'no operation' in the ACK
    system, but instead means 'forget everything and then don't do
    anything'. These semantics get expressed in at least three distinct
    places in the ACK sources, most significantly in the back-end
    code-generators. The relevant bits from mach/i386/ncg/table are:

    pat nop
    kills ALL

    i.e. whenever an EM nop is seen, the code generator forgets everything
    that it has assumed about the machine registers. This can be seen in
    the other backend tables for different processors as well. There is
    also similar code in parts of EGO (the EM Global Optimiser).

    Hmmm... it's rather sly...

    Michael


  6. Re: Volatile variables

    Glad to hear you solved your problem. I loved the question - it gave me
    an excuse to fossick through the ACK sources... :-)

    Regards, Michael


+ Reply to Thread