Go Down

Topic: How to view the raw C / AVR code generated by IDE before compliation? (Read 1 time) previous topic - next topic

eriknyquist

no practical reason, I'm simply interested in AVRs and I'd like to see which AVR registers are being modified. This will just make that a bit easier. I KNOW that these can be found in the .cpp and .h files but this means I won't have to sift through the whole thing, I can see only the registers that are actually being written in the compiled program

westfw

Quote
I'd like to see which AVR registers are being modified

Looking at the preprocessed output (from -E) will actually obscure this, compared to looking at the core/library.  The C compiler doesn't deal with named AVR-registers at the low level; they all become rather obscure looking operations on pointers.  For example, here is digitalWrite():
Code: [Select]

void digitalWrite(uint8_t pin, uint8_t val)
{
uint8_t timer = ( (__extension__({ uint16_t __addr16 = (uint16_t)((uint16_t)(digital_pin_to_timer_PGM + (pin))); uint8_t __result; __asm__ ( "lpm %0, Z" "\n\t" : "=r" (__result) : "z" (__addr16) ); __result; })) );
uint8_t bit = ( (__extension__({ uint16_t __addr16 = (uint16_t)((uint16_t)(digital_pin_to_bit_mask_PGM + (pin))); uint8_t __result; __asm__ ( "lpm %0, Z" "\n\t" : "=r" (__result) : "z" (__addr16) ); __result; })) );
uint8_t port = ( (__extension__({ uint16_t __addr16 = (uint16_t)((uint16_t)(digital_pin_to_port_PGM + (pin))); uint8_t __result; __asm__ ( "lpm %0, Z" "\n\t" : "=r" (__result) : "z" (__addr16) ); __result; })) );
volatile uint8_t *out;

if (port == 0) return;
if (timer != 0) turnOffPWM(timer);
out = ( (volatile uint8_t *)( (__extension__({ uint16_t __addr16 = (uint16_t)((uint16_t)(port_to_output_PGM + (port))); uint16_t __result; __asm__ ( "lpm %A0, Z+" "\n\t" "lpm %B0, Z" "\n\t" : "=r" (__result), "=z" (__addr16) : "1" (__addr16) ); __result; }))) );

uint8_t oldSREG = (*(volatile uint8_t *)((0x3F) + 0x20));
__asm__ __volatile__ ("cli" ::);

if (val == 0x0) {
  *out &= ~bit;
} else {
  *out |= bit;
}

(*(volatile uint8_t *)((0x3F) + 0x20)) = oldSREG;
}


Are you sure you don't want to look at the assembly language produced?  That's much more "concentrated", more stripped of extraneous garbage, and closer to actual AVR architecture stuff...

pYro_65


no practical reason, I'm simply interested in AVRs and I'd like to see which AVR registers are being modified. This will just make that a bit easier. I KNOW that these can be found in the .cpp and .h files but this means I won't have to sift through the whole thing, I can see only the registers that are actually being written in the compiled program


That's quite a bit different to viewing pre-processed code, sure the register macros are expanded to volatile pointer casts, but not quite what happens in the 'compiled program'

On windows to view the asm listing, type 'shell:sendto' into explorer, hit enter then copy the file I attached below in there.
You can then go into the IDE temp compile folder and right click on your sketch *.elf file then SendTo->AVR_ASM.bat

You will have to open the file in notepad and edit the path to the arduino directory.

But it will generate a text file containing the asm for your sketch.

westfw

For example, consider a statement in your sketch like:
Code: [Select]
PORTB = 0x12;
In the pre-processed C code (from -E), you would see:
Code: [Select]
  (*(volatile uint8_t *)((0x05) + 0x20)) = 0x12;

Which I claim is not very useful.  The assembler output would contain:
Quote
100:   82 e1           ldi     r24, 0x12       ; 18
102:   85 b9           out     0x05, r24       ; 5


The original source code is best.

Unfortunately, there is no way to get "partially pre-processed" code that might tell you something like:
Code: [Select]
digitalWriteFast(13,1);turns into
Code: [Select]
PORTD |= 0x40;
Fortunately, the Arduino code doesn't have many of the sort of complex macros that would make that sort of thing more useful.  digitalWrite() is a true function, and its source code is pretty understandable.



eriknyquist

pyro_65, thanks for that batch file, I ran it on my .elf  but I have a silly question...

where is the .asm file dumped?  I can't find it anywhere...

Go Up