Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / asm.c
blob630a5e85dd913180deda71223bc2a78c19400283
1 // RUN: %clang_cc1 %s -Wno-private-extern -triple i386-pc-linux-gnu -verify -fsyntax-only
3 void f(void) {
4 int i;
6 asm ("foo\n" : : "a" (i + 2));
7 asm ("foo\n" : : "a" (f())); // expected-error {{invalid type 'void' in asm input}}
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}}
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}}
17 asm ("foo\n" : : "" (i)); // expected-error {{invalid input constraint '' in asm}}
18 asm ("foo\n" : "=a" (i) : "" (i)); // expected-error {{invalid input constraint '' in asm}}
21 void clobbers(void) {
22 asm ("nop" : : : "ax", "#ax", "%ax");
23 asm ("nop" : : : "eax", "rax", "ah", "al");
24 asm ("nop" : : : "0", "%0", "#0");
25 asm ("nop" : : : "foo"); // expected-error {{unknown register name 'foo' in asm}}
26 asm ("nop" : : : "52");
27 asm ("nop" : : : "204"); // expected-error {{unknown register name '204' in asm}}
28 asm ("nop" : : : "-1"); // expected-error {{unknown register name '-1' in asm}}
29 asm ("nop" : : : "+1"); // expected-error {{unknown register name '+1' in asm}}
30 register void *clobber_conflict asm ("%rcx");
31 register void *no_clobber_conflict asm ("%rax");
32 int a,b,c;
33 asm ("nop" : "=r" (no_clobber_conflict) : "r" (clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
34 asm ("nop" : "=r" (clobber_conflict) : "r" (no_clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
35 asm ("nop" : "=r" (clobber_conflict) : "r" (clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
36 asm ("nop" : "=c" (a) : "r" (no_clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
37 asm ("nop" : "=r" (no_clobber_conflict) : "c" (c) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
38 asm ("nop" : "=r" (clobber_conflict) : "c" (c) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
39 asm ("nop" : "=a" (a) : "b" (b) : "%rcx", "%rbx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
42 void test3(void) {
43 int x;
44 asm(L"foo" : "=r"(x)); // expected-error {{wide string}}
45 asm("foo" : L"=r"(x)); // expected-error {{wide string}}
48 void test4(const volatile void *addr)
50 asm ("nop" : : "r"(*addr)); /* expected-error {{invalid type 'const volatile void' in asm input for constraint 'r'}}
51 expected-warning {{ISO C does not allow indirection on operand of type 'const volatile void *'}} */
52 asm ("nop" : : "m"(*addr)); // expected-warning {{ISO C does not allow indirection on operand of type 'const volatile void *'}}
54 asm ("nop" : : "r"(test4(addr))); // expected-error {{invalid type 'void' in asm input for constraint 'r'}}
55 asm ("nop" : : "m"(test4(addr))); // expected-error {{invalid lvalue in asm input for constraint 'm'}}
57 asm ("nop" : : "m"(f())); // expected-error {{invalid lvalue in asm input for constraint 'm'}}
60 void test5(void) {
61 asm("nop" : : "X" (8));
64 // PR3385
65 void test6(long i) {
66 asm("nop" : : "er"(i));
69 void asm_string_tests(int i) {
70 asm("%!"); // simple asm string, %! is not an error.
71 asm("%!" : ); // expected-error {{invalid % escape in inline assembly string}}
72 asm("xyz %" : ); // expected-error {{invalid % escape in inline assembly string}}
74 asm ("%[somename]" :: [somename] "i"(4)); // ok
75 asm ("%[somename]" :: "i"(4)); // expected-error {{unknown symbolic operand name in inline assembly string}}
76 asm ("%[somename" :: "i"(4)); // expected-error {{unterminated symbolic operand name in inline assembly string}}
77 asm ("%[]" :: "i"(4)); // expected-error {{empty symbolic operand name in inline assembly string}}
79 // PR3258
80 asm("%9" :: "i"(4)); // expected-error {{invalid operand number in inline asm string}}
81 asm("%1" : "+r"(i)); // ok, referring to input.
84 // PR4077
85 int test7(unsigned long long b) {
86 int a;
87 asm volatile("foo %0 %1" : "=a" (a) :"0" (b)); // expected-error {{input with type 'unsigned long long' matching output with type 'int'}}
88 return a;
91 // PR3904
92 void test8(int i) {
93 // A number in an input constraint can't point to a read-write constraint.
94 asm("" : "+r" (i), "=r"(i) : "0" (i)); // expected-error{{invalid input constraint '0' in asm}}
97 // PR3905
98 void test9(int i) {
99 asm("" : [foo] "=r" (i), "=r"(i) : "1[foo]"(i)); // expected-error{{invalid input constraint '1[foo]' in asm}}
100 asm("" : [foo] "=r" (i), "=r"(i) : "[foo]1"(i)); // expected-error{{invalid input constraint '[foo]1' in asm}}
103 void test10(void){
104 static int g asm ("g_asm") = 0;
105 extern int gg asm ("gg_asm");
106 __private_extern__ int ggg asm ("ggg_asm");
108 int a asm ("a_asm"); // expected-warning{{ignored asm label 'a_asm' on automatic variable}}
109 auto int aa asm ("aa_asm"); // expected-warning{{ignored asm label 'aa_asm' on automatic variable}}
111 register int r asm ("cx");
112 register int rr asm ("rr_asm"); // expected-error{{unknown register name 'rr_asm' in asm}}
113 register int rrr asm ("%"); // expected-error{{unknown register name '%' in asm}}
116 // This is just an assert because of the boolean conversion.
117 // Feel free to change the assembly to something sensible if it causes a problem.
118 void test11(void) {
119 _Bool b;
120 asm volatile ("movb %%gs:%P2,%b0" : "=q"(b) : "0"(0), "i"(5L));
123 void test12(void) {
124 register int cc __asm ("cc"); // expected-error{{unknown register name 'cc' in asm}}
127 // PR10223
128 void test13(void) {
129 void *esp;
130 __asm__ volatile ("mov %%esp, %o" : "=r"(esp) : : ); // expected-error {{invalid % escape in inline assembly string}}
133 struct S; // expected-note 2 {{forward declaration of 'struct S'}}
134 void test14(struct S *s) {
135 __asm("": : "a"(*s)); // expected-error {{dereference of pointer to incomplete type 'struct S'}}
136 __asm("": "=a" (*s) :); // expected-error {{dereference of pointer to incomplete type 'struct S'}}
139 // PR15759.
140 double test15(void) {
141 double ret = 0;
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":"+!"(ret)); // expected-error {{invalid output constraint '+!' in asm}}
146 __asm("0.0":"+#"(ret)); // expected-error {{invalid output constraint '+#' in asm}}
147 __asm("0.0":"+*"(ret)); // expected-error {{invalid output constraint '+*' in asm}}
148 __asm("0.0":"=%"(ret)); // expected-error {{invalid output constraint '=%' in asm}}
149 __asm("0.0":"=,="(ret)); // expected-error {{invalid output constraint '=,=' in asm}}
150 __asm("0.0":"=,g"(ret)); // no-error
151 __asm("0.0":"=g"(ret)); // no-error
152 return ret;
155 void iOutputConstraint(int x){
156 __asm ("nop" : "=ir" (x) : :); // no-error
157 __asm ("nop" : "=ri" (x) : :); // no-error
158 __asm ("nop" : "=ig" (x) : :); // no-error
159 __asm ("nop" : "=im" (x) : :); // no-error
160 __asm ("nop" : "=imr" (x) : :); // no-error
161 __asm ("nop" : "=i" (x) : :); // expected-error{{invalid output constraint '=i' in asm}}
162 __asm ("nop" : "+i" (x) : :); // expected-error{{invalid output constraint '+i' in asm}}
163 __asm ("nop" : "=ii" (x) : :); // expected-error{{invalid output constraint '=ii' in asm}}
164 __asm ("nop" : "=nr" (x) : :); // no-error
165 __asm ("nop" : "=rn" (x) : :); // no-error
166 __asm ("nop" : "=ng" (x) : :); // no-error
167 __asm ("nop" : "=nm" (x) : :); // no-error
168 __asm ("nop" : "=nmr" (x) : :); // no-error
169 __asm ("nop" : "=n" (x) : :); // expected-error{{invalid output constraint '=n' in asm}}
170 __asm ("nop" : "+n" (x) : :); // expected-error{{invalid output constraint '+n' in asm}}
171 __asm ("nop" : "=nn" (x) : :); // expected-error{{invalid output constraint '=nn' in asm}}
172 __asm ("nop" : "=Fr" (x) : :); // no-error
173 __asm ("nop" : "=rF" (x) : :); // no-error
174 __asm ("nop" : "=Fg" (x) : :); // no-error
175 __asm ("nop" : "=Fm" (x) : :); // no-error
176 __asm ("nop" : "=Fmr" (x) : :); // no-error
177 __asm ("nop" : "=F" (x) : :); // expected-error{{invalid output constraint '=F' in asm}}
178 __asm ("nop" : "+F" (x) : :); // expected-error{{invalid output constraint '+F' in asm}}
179 __asm ("nop" : "=FF" (x) : :); // expected-error{{invalid output constraint '=FF' in asm}}
180 __asm ("nop" : "=Er" (x) : :); // no-error
181 __asm ("nop" : "=rE" (x) : :); // no-error
182 __asm ("nop" : "=Eg" (x) : :); // no-error
183 __asm ("nop" : "=Em" (x) : :); // no-error
184 __asm ("nop" : "=Emr" (x) : :); // no-error
185 __asm ("nop" : "=E" (x) : :); // expected-error{{invalid output constraint '=E' in asm}}
186 __asm ("nop" : "+E" (x) : :); // expected-error{{invalid output constraint '+E' in asm}}
187 __asm ("nop" : "=EE" (x) : :); // expected-error{{invalid output constraint '=EE' in asm}}
190 // PR19837
191 struct foo {
192 int a;
194 register struct foo bar asm("esp"); // expected-error {{bad type for named register variable}}
195 register float baz asm("esp"); // expected-error {{bad type for named register variable}}
197 register int r0 asm ("edi"); // expected-error {{register 'edi' unsuitable for global register variables on this target}}
198 register long long r1 asm ("esp"); // expected-error {{size of register 'esp' does not match variable size}}
199 register int r2 asm ("esp");
201 double f_output_constraint(void) {
202 double result;
203 __asm("foo1": "=f" (result)); // expected-error {{invalid output constraint '=f' in asm}}
204 return result;
207 void fn1(void) {
208 int l;
209 __asm__(""
210 : [l] "=r"(l)
211 : "[l],m"(l)); // expected-error {{asm constraint has an unexpected number of alternatives: 1 vs 2}}
214 void fn2(void) {
215 int l;
216 __asm__(""
217 : "+&m"(l)); // expected-error {{invalid output constraint '+&m' in asm}}
220 void fn3(void) {
221 int l;
222 __asm__(""
223 : "+#r"(l)); // expected-error {{invalid output constraint '+#r' in asm}}
226 void fn4(void) {
227 int l;
228 __asm__(""
229 : "=r"(l)
230 : "m#"(l));
233 void fn5(void) {
234 int l;
235 __asm__(""
236 : [g] "+r"(l)
237 : "[g]"(l)); // expected-error {{invalid input constraint '[g]' in asm}}
240 void fn6(void) {
241 int a;
242 __asm__(""
243 : "=rm"(a), "=rm"(a)
244 : "11m"(a)); // expected-error {{invalid input constraint '11m' in asm}}
247 // PR14269
248 typedef struct test16_foo {
249 unsigned int field1 : 1;
250 unsigned int field2 : 2;
251 unsigned int field3 : 3;
252 } test16_foo;
253 typedef __attribute__((vector_size(16))) int test16_bar;
254 register int test16_baz asm("esp");
256 void test16(void)
258 test16_foo a;
259 test16_bar b;
261 __asm__("movl $5, %0"
262 : "=rm" (a.field2)); // expected-error {{reference to a bit-field in asm input with a memory constraint '=rm'}}
263 __asm__("movl $5, %0"
265 : "m" (a.field3)); // expected-error {{reference to a bit-field in asm output with a memory constraint 'm'}}
266 __asm__("movl $5, %0"
267 : "=rm" (b[2])); // expected-error {{reference to a vector element in asm input with a memory constraint '=rm'}}
268 __asm__("movl $5, %0"
270 : "m" (b[3])); // expected-error {{reference to a vector element in asm output with a memory constraint 'm'}}
271 __asm__("movl $5, %0"
272 : "=rm" (test16_baz)); // expected-error {{reference to a global register variable in asm input with a memory constraint '=rm'}}
273 __asm__("movl $5, %0"
275 : "m" (test16_baz)); // expected-error {{reference to a global register variable in asm output with a memory constraint 'm'}}
278 int test17(int t0)
280 int r0, r1;
281 __asm ("addl %2, %2\n\t"
282 "movl $123, %0"
283 : "=a" (r0),
284 "=&r" (r1)
285 : "1" (t0), // expected-note {{constraint '1' is already present here}}
286 "1" (t0)); // expected-error {{more than one input constraint matches the same output '1'}}
287 return r0 + r1;
290 void test18(void)
292 // expected-error@+2 {{duplicate use of asm operand name "lab"}}
293 // expected-note@+1 {{asm operand name "lab" first referenced here}}
294 asm goto ("" : : : : lab, lab, lab2, lab);
295 // expected-error@+2 {{duplicate use of asm operand name "lab"}}
296 // expected-note@+1 {{asm operand name "lab" first referenced here}}
297 asm goto ("xorw %[lab], %[lab]; je %l[lab]" : : [lab] "i" (0) : : lab);
298 lab:;
299 lab2:;
300 int x,x1;
301 // expected-error@+2 {{duplicate use of asm operand name "lab"}}
302 // expected-note@+1 {{asm operand name "lab" first referenced here}}
303 asm ("" : [lab] "=r" (x),[lab] "+r" (x) : [lab1] "r" (x));
304 // expected-error@+2 {{duplicate use of asm operand name "lab"}}
305 // expected-note@+1 {{asm operand name "lab" first referenced here}}
306 asm ("" : [lab] "=r" (x1) : [lab] "r" (x));
307 // expected-error@+1 {{invalid operand number in inline asm string}}
308 asm ("jne %l0":::);
309 asm goto ("jne %l0"::::lab);
312 typedef struct _st_size64 {
313 int a;
314 char b;
315 } st_size64;
317 typedef struct _st_size96 {
318 int a;
319 int b;
320 int c;
321 } st_size96;
323 typedef struct _st_size16 {
324 char a;
325 char b;
326 } st_size16;
328 typedef struct _st_size32 {
329 char a;
330 char b;
331 char c;
332 char d;
333 } st_size32;
335 typedef struct _st_size128 {
336 int a;
337 int b;
338 int c;
339 int d;
340 } st_size128;
342 void test19(long long x)
344 st_size64 a;
345 st_size96 b;
346 st_size16 c;
347 st_size32 d;
348 st_size128 e;
349 asm ("" : "=rm" (a): "0" (1)); // no-error
350 asm ("" : "=rm" (d): "0" (1)); // no-error
351 asm ("" : "=rm" (c): "0" (x)); // no-error
352 // FIXME: This case is actually supported by codegen.
353 asm ("" : "=rm" (x): "0" (a)); // expected-error {{unsupported inline asm: input with type 'st_size64' (aka 'struct _st_size64') matching output with type 'long long'}}
354 // FIXME: This case is actually supported by codegen.
355 asm ("" : "=rm" (a): "0" (d)); // expected-error {{unsupported inline asm: input with type 'st_size32' (aka 'struct _st_size32') matching output with type 'st_size64' (aka 'struct _st_size64')}}
356 asm ("" : "=rm" (b): "0" (1)); // expected-error {{impossible constraint in asm: can't store value into a register}}
357 // FIXME: This case should be supported by codegen, but it fails now.
358 asm ("" : "=rm" (e): "0" (1)); // no-error
359 // FIXME: This case should be supported by codegen, but it fails now.
360 asm ("" : "=rm" (x): "0" (e)); // expected-error {{unsupported inline asm: input with type 'st_size128' (aka 'struct _st_size128') matching output with type 'long long'}}