avr-llvm / llvm

[MERGED UPSTREAM] AVR backend for the LLVM compiler library
220 stars 21 forks source link

Invalid operand for instruction in inline assembly #91

Closed chun92 closed 9 years ago

chun92 commented 9 years ago

I have tried to compiler some led_blink tutorial code with clang, but I've encountered parsing error.

Following is the example code.

/* Hi Emacs, this is -*- c-mode -*- */
#include <avr/io.h>
#include <util/delay.h>

int main (void)
{
  unsigned char counter;
  /* set PORTB for output*/
  DDRB = 0xFF;

  while (1) 
  {
    /* set PORTB.2 high */
    PORTB = 0xFF;

    /* wait (10 * 120000) cycles = wait 1200000 cycles */
    counter = 0;
    while (counter != 50) 
    {   
      /* wait (30000 x 4) cycles = wait 120000 cycles */
      _delay_loop_2(30000);
      counter++;
    }   

    /* set PORTB.2 low */
    PORTB = 0x00;

    /* wait (10 * 120000) cycles = wait 1200000 cycles */
    counter = 0;
    while (counter != 50) 
    {   
      /* wait (30000 x 4) cycles = wait 120000 cycles */
      _delay_loop_2(30000);
      counter++;
    }   
  }

  return 1;
}

I tried to compile with following command and get below error message.

clang --target=avr -I../../include -mmcu=atmega328p -DF_CPU=16000000UL -Os -w -Wl,--gc-sections -ffunction-sections -fdata-sections flash.cpp -o flash1.elf

In file included from flash.cpp:4:
In file included from ../../include/util/delay.h:43:
../../include/util/delay_basic.h:106:3: error: invalid operand for instruction
                "1: sbiw %0,1" "\n\t"
                ^
<inline asm>:1:10: note: instantiated into assembly here
        1: sbiw r30,1
                ^
In file included from flash.cpp:4:
In file included from ../../include/util/delay.h:43:
../../include/util/delay_basic.h:106:3: error: invalid operand for instruction
                "1: sbiw %0,1" "\n\t"
                ^
<inline asm>:1:10: note: instantiated into assembly here
        1: sbiw r30,1
                ^
2 errors generated.
make: *** [flash1.elf] Error 1

avr-gcc replaces %0 to r30, but what's the meaning of the message; <inline asm>:1:10: note: instantiated into assembly here?

    void
    _delay_loop_2(uint16_t __count)
    {
      __asm__ volatile (
        "1: sbiw %0,1" "\n\t"
        "brne 1b"
        : "=w" (__count)
        : "0" (__count)
      );  
    }
dylanmckay commented 9 years ago

This problem is caused by issue #92.