asm.c 7.6 KB

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