Home | History | Annotate | Download | only in Sema
      1 // RUN: %clang_cc1 %s -Wno-private-extern -triple i386-pc-linux-gnu -verify -fsyntax-only
      2 
      3 void f() {
      4   int i;
      5 
      6   asm ("foo\n" : : "a" (i + 2));
      7   asm ("foo\n" : : "a" (f())); // expected-error {{invalid type 'void' in asm input}}
      8 
      9   asm ("foo\n" : "=a" (f())); // expected-error {{invalid lvalue in asm output}}
     10   asm ("foo\n" : "=a" (i + 2)); // expected-error {{invalid lvalue in asm output}}
     11 
     12   asm ("foo\n" : [symbolic_name] "=a" (i) : "[symbolic_name]" (i));
     13   asm ("foo\n" : "=a" (i) : "[" (i)); // expected-error {{invalid input constraint '[' in asm}}
     14   asm ("foo\n" : "=a" (i) : "[foo" (i)); // expected-error {{invalid input constraint '[foo' in asm}}
     15   asm ("foo\n" : "=a" (i) : "[symbolic_name]" (i)); // expected-error {{invalid input constraint '[symbolic_name]' in asm}}
     16 }
     17 
     18 void clobbers() {
     19   asm ("nop" : : : "ax", "#ax", "%ax");
     20   asm ("nop" : : : "eax", "rax", "ah", "al");
     21   asm ("nop" : : : "0", "%0", "#0");
     22   asm ("nop" : : : "foo"); // expected-error {{unknown register name 'foo' in asm}}
     23   asm ("nop" : : : "52");
     24   asm ("nop" : : : "104"); // expected-error {{unknown register name '104' in asm}}
     25   asm ("nop" : : : "-1"); // expected-error {{unknown register name '-1' in asm}}
     26   asm ("nop" : : : "+1"); // expected-error {{unknown register name '+1' in asm}}
     27 }
     28 
     29 // rdar://6094010
     30 void test3() {
     31   int x;
     32   asm(L"foo" : "=r"(x)); // expected-error {{wide string}}
     33   asm("foo" : L"=r"(x)); // expected-error {{wide string}}
     34 }
     35 
     36 // <rdar://problem/6156893>
     37 void test4(const volatile void *addr)
     38 {
     39     asm ("nop" : : "r"(*addr)); // expected-error {{invalid type 'const volatile void' in asm input for constraint 'r'}}
     40     asm ("nop" : : "m"(*addr));
     41 
     42     asm ("nop" : : "r"(test4(addr))); // expected-error {{invalid type 'void' in asm input for constraint 'r'}}
     43     asm ("nop" : : "m"(test4(addr))); // expected-error {{invalid lvalue in asm input for constraint 'm'}}
     44 
     45     asm ("nop" : : "m"(f())); // expected-error {{invalid lvalue in asm input for constraint 'm'}}
     46 }
     47 
     48 // <rdar://problem/6512595>
     49 void test5() {
     50   asm("nop" : : "X" (8));
     51 }
     52 
     53 // PR3385
     54 void test6(long i) {
     55   asm("nop" : : "er"(i));
     56 }
     57 
     58 void asm_string_tests(int i) {
     59   asm("%!");   // simple asm string, %! is not an error.
     60   asm("%!" : );   // expected-error {{invalid % escape in inline assembly string}}
     61   asm("xyz %" : );   // expected-error {{invalid % escape in inline assembly string}}
     62 
     63   asm ("%[somename]" :: [somename] "i"(4)); // ok
     64   asm ("%[somename]" :: "i"(4)); // expected-error {{unknown symbolic operand name in inline assembly string}}
     65   asm ("%[somename" :: "i"(4)); // expected-error {{unterminated symbolic operand name in inline assembly string}}
     66   asm ("%[]" :: "i"(4)); // expected-error {{empty symbolic operand name in inline assembly string}}
     67 
     68   // PR3258
     69   asm("%9" :: "i"(4)); // expected-error {{invalid operand number in inline asm string}}
     70   asm("%1" : "+r"(i)); // ok, referring to input.
     71 }
     72 
     73 // PR4077
     74 int test7(unsigned long long b) {
     75   int a;
     76   asm volatile("foo %0 %1" : "=a" (a) :"0" (b)); // expected-error {{input with type 'unsigned long long' matching output with type 'int'}}
     77   return a;
     78 }
     79 
     80 // <rdar://problem/7574870>
     81 asm volatile (""); // expected-warning {{meaningless 'volatile' on asm outside function}}
     82 
     83 // PR3904
     84 void test8(int i) {
     85   // A number in an input constraint can't point to a read-write constraint.
     86   asm("" : "+r" (i), "=r"(i) :  "0" (i)); // expected-error{{invalid input constraint '0' in asm}}
     87 }
     88 
     89 // PR3905
     90 void test9(int i) {
     91   asm("" : [foo] "=r" (i), "=r"(i) : "1[foo]"(i)); // expected-error{{invalid input constraint '1[foo]' in asm}}
     92   asm("" : [foo] "=r" (i), "=r"(i) : "[foo]1"(i)); // expected-error{{invalid input constraint '[foo]1' in asm}}
     93 }
     94 
     95 register int g asm("dx"); // expected-error{{global register variables are not supported}}
     96 
     97 void test10(void){
     98   static int g asm ("g_asm") = 0;
     99   extern int gg asm ("gg_asm");
    100   __private_extern__ int ggg asm ("ggg_asm");
    101 
    102   int a asm ("a_asm"); // expected-warning{{ignored asm label 'a_asm' on automatic variable}}
    103   auto int aa asm ("aa_asm"); // expected-warning{{ignored asm label 'aa_asm' on automatic variable}}
    104 
    105   register int r asm ("cx");
    106   register int rr asm ("rr_asm"); // expected-error{{unknown register name 'rr_asm' in asm}}
    107 }
    108 
    109 // This is just an assert because of the boolean conversion.
    110 // Feel free to change the assembly to something sensible if it causes a problem.
    111 // rdar://problem/9414925
    112 void test11(void) {
    113   _Bool b;
    114   asm volatile ("movb %%gs:%P2,%b0" : "=q"(b) : "0"(0), "i"(5L));
    115 }
    116 
    117 void test12(void) {
    118   register int cc __asm ("cc"); // expected-error{{unknown register name 'cc' in asm}}
    119 }
    120 
    121 // PR10223
    122 void test13(void) {
    123   void *esp;
    124   __asm__ volatile ("mov %%esp, %o" : "=r"(esp) : : ); // expected-error {{invalid % escape in inline assembly string}}
    125 }
    126 
    127 // <rdar://problem/12700799>
    128 struct S;  // expected-note 2 {{forward declaration of 'struct S'}}
    129 void test14(struct S *s) {
    130   __asm("": : "a"(*s)); // expected-error {{dereference of pointer to incomplete type 'struct S'}}
    131   __asm("": "=a" (*s) :); // expected-error {{dereference of pointer to incomplete type 'struct S'}}
    132 }
    133 
    134 // PR15759.
    135 double test15() {
    136   double ret = 0;
    137   __asm("0.0":"="(ret)); // expected-error {{invalid output constraint '=' in asm}}
    138   __asm("0.0":"=&"(ret)); // expected-error {{invalid output constraint '=&' in asm}}
    139   __asm("0.0":"+?"(ret)); // expected-error {{invalid output constraint '+?' in asm}}
    140   __asm("0.0":"+!"(ret)); // expected-error {{invalid output constraint '+!' in asm}}
    141   __asm("0.0":"+#"(ret)); // expected-error {{invalid output constraint '+#' in asm}}
    142   __asm("0.0":"+*"(ret)); // expected-error {{invalid output constraint '+*' in asm}}
    143   __asm("0.0":"=%"(ret)); // expected-error {{invalid output constraint '=%' in asm}}
    144   __asm("0.0":"=,="(ret)); // expected-error {{invalid output constraint '=,=' in asm}}
    145   __asm("0.0":"=,g"(ret)); // no-error
    146   __asm("0.0":"=g"(ret)); // no-error
    147   return ret;
    148 }
    149