Hi, In the user manual:
<http://www.nongnu.org/avr-libc/user-manual/optimization.html> there is a discussion about the unexpected code generation from: #define cli() __asm volatile( "cli" ::: "memory" ) #define sei() __asm volatile( "sei" ::: "memory" ) unsigned int ivar; void test2( unsigned int val ) { val = 65535U / val; cli(); ivar = val; sei(); } This came up recently in a gcc-help mailing list question - the problem is that the call to __udivmodhi4 may be generated after the cli instruction, disabling interrupts for longer than necessary. The web page says there is no way to force the desired code generation (with "val" being calculated before "cli"). However, there /is/ a way to get the right results - using a fake assembly input to force the calculation: #define cli() __asm volatile( "cli" ::: "memory" ) #define sei() __asm volatile( "sei" ::: "memory" ) unsigned int ivar; void test2( unsigned int val ) { val = 65535U / val; asm volatile("" :: "" (val)); cli(); ivar = val; sei(); } The memory clobber on cli() and sei() ensures that no memory operations are moved before or after those statements. But as already noted, the memory clobber does not affect non-memory operations such as calculations or register-only manipulation. But the extra "asm volatile" here with a fake input tells the compiler that "val" is an input to the (empty) assembly, and must therefore be calculated before the statement is executed. The empty input constraint (no "r" or "m") gives the compiler complete freedom about where it wants to put this fake input - all we are saying is that the value "val" must be calculated before executing asm volatile("" :: "" (val)) Generating assembly from this (using gcc-4.5.1, which is the latest avr-gcc I have installed at the moment) shows the division being done before the cli() - the code is optimal and correct, with no unnecessary memory operations (as you would need by making "val" volatile). Could the documentation and web page be updated to include this solution, for the benefit of others who find it (not just avr-libc users)? -- Vennlig hilsen / Best regards *David Brown* Westcontrol AS Systemutvikler / Systems Developer Tlf.: +47 5174 1031 Mob.: +47 977 37 328 E-mail: da...@westcontrol.com Web: http://www.westcontrol.com _______________________________________________ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev