blob: 04b7cb19eb83a2d0672d0518d4269ace543ac155 [file] [log] [blame]
Fariborz Jahanianf85f3382012-08-17 21:44:55 +00001// RUN: %clang_cc1 %s -Wno-private-extern -triple i386-pc-linux-gnu -verify -fsyntax-only
Anders Carlsson80a5ea32007-11-23 19:43:50 +00002
Renato Golin2e31e4e2014-06-05 16:45:22 +00003
Chris Lattner3fa25c62009-03-11 00:06:36 +00004void f() {
Anders Carlsson80a5ea32007-11-23 19:43:50 +00005 int i;
6
7 asm ("foo\n" : : "a" (i + 2));
8 asm ("foo\n" : : "a" (f())); // expected-error {{invalid type 'void' in asm input}}
Eric Christopherf7d303c2011-07-07 23:11:01 +00009
Anders Carlsson80a5ea32007-11-23 19:43:50 +000010 asm ("foo\n" : "=a" (f())); // expected-error {{invalid lvalue in asm output}}
11 asm ("foo\n" : "=a" (i + 2)); // expected-error {{invalid lvalue in asm output}}
Anders Carlssona79203b2009-01-18 01:56:57 +000012
13 asm ("foo\n" : [symbolic_name] "=a" (i) : "[symbolic_name]" (i));
14 asm ("foo\n" : "=a" (i) : "[" (i)); // expected-error {{invalid input constraint '[' in asm}}
15 asm ("foo\n" : "=a" (i) : "[foo" (i)); // expected-error {{invalid input constraint '[foo' in asm}}
16 asm ("foo\n" : "=a" (i) : "[symbolic_name]" (i)); // expected-error {{invalid input constraint '[symbolic_name]' in asm}}
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +000017
18 asm ("foo\n" : : "" (i)); // expected-error {{invalid input constraint '' in asm}}
19 asm ("foo\n" : "=a" (i) : "" (i)); // expected-error {{invalid input constraint '' in asm}}
Anders Carlsson80a5ea32007-11-23 19:43:50 +000020}
Anders Carlsson290aa852007-11-25 00:25:21 +000021
Chris Lattner3fa25c62009-03-11 00:06:36 +000022void clobbers() {
Anders Carlsson290aa852007-11-25 00:25:21 +000023 asm ("nop" : : : "ax", "#ax", "%ax");
24 asm ("nop" : : : "eax", "rax", "ah", "al");
25 asm ("nop" : : : "0", "%0", "#0");
26 asm ("nop" : : : "foo"); // expected-error {{unknown register name 'foo' in asm}}
27 asm ("nop" : : : "52");
Marina Yatsina41c45fa2016-02-03 11:32:08 +000028 asm ("nop" : : : "204"); // expected-error {{unknown register name '204' in asm}}
Anders Carlsson290aa852007-11-25 00:25:21 +000029 asm ("nop" : : : "-1"); // expected-error {{unknown register name '-1' in asm}}
30 asm ("nop" : : : "+1"); // expected-error {{unknown register name '+1' in asm}}
Marina Yatsinac42fd032016-12-26 12:23:42 +000031 register void *clobber_conflict asm ("%rcx");
32 register void *no_clobber_conflict asm ("%rax");
33 int a,b,c;
34 asm ("nop" : "=r" (no_clobber_conflict) : "r" (clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
35 asm ("nop" : "=r" (clobber_conflict) : "r" (no_clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
36 asm ("nop" : "=r" (clobber_conflict) : "r" (clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
37 asm ("nop" : "=c" (a) : "r" (no_clobber_conflict) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
38 asm ("nop" : "=r" (no_clobber_conflict) : "c" (c) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
39 asm ("nop" : "=r" (clobber_conflict) : "c" (c) : "%rcx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
40 asm ("nop" : "=a" (a) : "b" (b) : "%rcx", "%rbx"); // expected-error {{asm-specifier for input or output variable conflicts with asm clobber list}}
Anders Carlsson290aa852007-11-25 00:25:21 +000041}
Chris Lattner07096892008-07-23 06:46:56 +000042
43// rdar://6094010
44void test3() {
45 int x;
46 asm(L"foo" : "=r"(x)); // expected-error {{wide string}}
47 asm("foo" : L"=r"(x)); // expected-error {{wide string}}
48}
49
Anders Carlsson224fca82009-01-20 20:49:22 +000050// <rdar://problem/6156893>
51void test4(const volatile void *addr)
52{
Chris Lattner53fa0492010-09-05 00:04:01 +000053 asm ("nop" : : "r"(*addr)); // expected-error {{invalid type 'const volatile void' in asm input for constraint 'r'}}
Anders Carlsson224fca82009-01-20 20:49:22 +000054 asm ("nop" : : "m"(*addr));
55
56 asm ("nop" : : "r"(test4(addr))); // expected-error {{invalid type 'void' in asm input for constraint 'r'}}
57 asm ("nop" : : "m"(test4(addr))); // expected-error {{invalid lvalue in asm input for constraint 'm'}}
58
Anders Carlssondc6ceca2009-01-21 06:27:20 +000059 asm ("nop" : : "m"(f())); // expected-error {{invalid lvalue in asm input for constraint 'm'}}
60}
61
62// <rdar://problem/6512595>
Chris Lattnera41b8472009-03-10 23:51:40 +000063void test5() {
Eric Christopherf7d303c2011-07-07 23:11:01 +000064 asm("nop" : : "X" (8));
Anders Carlsson224fca82009-01-20 20:49:22 +000065}
Anders Carlssona0b89212009-01-24 18:03:09 +000066
67// PR3385
Chris Lattnera41b8472009-03-10 23:51:40 +000068void test6(long i) {
Anders Carlssona0b89212009-01-24 18:03:09 +000069 asm("nop" : : "er"(i));
Anders Carlsson570c3572009-01-27 20:38:24 +000070}
Chris Lattnera41b8472009-03-10 23:51:40 +000071
Chris Lattner14311922009-03-11 00:23:13 +000072void asm_string_tests(int i) {
Eric Christopherf7d303c2011-07-07 23:11:01 +000073 asm("%!"); // simple asm string, %! is not an error.
Chris Lattnera41b8472009-03-10 23:51:40 +000074 asm("%!" : ); // expected-error {{invalid % escape in inline assembly string}}
Chris Lattner3fa25c62009-03-11 00:06:36 +000075 asm("xyz %" : ); // expected-error {{invalid % escape in inline assembly string}}
76
77 asm ("%[somename]" :: [somename] "i"(4)); // ok
78 asm ("%[somename]" :: "i"(4)); // expected-error {{unknown symbolic operand name in inline assembly string}}
79 asm ("%[somename" :: "i"(4)); // expected-error {{unterminated symbolic operand name in inline assembly string}}
80 asm ("%[]" :: "i"(4)); // expected-error {{empty symbolic operand name in inline assembly string}}
Eric Christopherf7d303c2011-07-07 23:11:01 +000081
Chris Lattner14311922009-03-11 00:23:13 +000082 // PR3258
83 asm("%9" :: "i"(4)); // expected-error {{invalid operand number in inline asm string}}
84 asm("%1" : "+r"(i)); // ok, referring to input.
Chris Lattnera41b8472009-03-10 23:51:40 +000085}
Chris Lattner574428e2009-04-26 18:22:24 +000086
87// PR4077
88int test7(unsigned long long b) {
89 int a;
Chris Lattnercb66c732009-05-03 07:04:21 +000090 asm volatile("foo %0 %1" : "=a" (a) :"0" (b)); // expected-error {{input with type 'unsigned long long' matching output with type 'int'}}
Chris Lattner574428e2009-04-26 18:22:24 +000091 return a;
92}
John McCall9dfb1622010-01-25 22:27:48 +000093
94// <rdar://problem/7574870>
95asm volatile (""); // expected-warning {{meaningless 'volatile' on asm outside function}}
Anders Carlssonda1f5fc2010-11-03 02:22:29 +000096
97// PR3904
Anders Carlsson2d5f8b42010-11-03 02:54:51 +000098void test8(int i) {
Anders Carlssonda1f5fc2010-11-03 02:22:29 +000099 // A number in an input constraint can't point to a read-write constraint.
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000100 asm("" : "+r" (i), "=r"(i) : "0" (i)); // expected-error{{invalid input constraint '0' in asm}}
101}
102
103// PR3905
104void test9(int i) {
105 asm("" : [foo] "=r" (i), "=r"(i) : "1[foo]"(i)); // expected-error{{invalid input constraint '1[foo]' in asm}}
106 asm("" : [foo] "=r" (i), "=r"(i) : "[foo]1"(i)); // expected-error{{invalid input constraint '[foo]1' in asm}}
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000107}
Rafael Espindola478abca2011-01-01 21:47:03 +0000108
109void test10(void){
Abramo Bagnara13392232011-01-11 15:16:52 +0000110 static int g asm ("g_asm") = 0;
111 extern int gg asm ("gg_asm");
112 __private_extern__ int ggg asm ("ggg_asm");
113
114 int a asm ("a_asm"); // expected-warning{{ignored asm label 'a_asm' on automatic variable}}
115 auto int aa asm ("aa_asm"); // expected-warning{{ignored asm label 'aa_asm' on automatic variable}}
116
117 register int r asm ("cx");
118 register int rr asm ("rr_asm"); // expected-error{{unknown register name 'rr_asm' in asm}}
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000119 register int rrr asm ("%"); // expected-error{{unknown register name '%' in asm}}
Rafael Espindola478abca2011-01-01 21:47:03 +0000120}
John McCalldfbf9342011-05-10 23:39:47 +0000121
122// This is just an assert because of the boolean conversion.
123// Feel free to change the assembly to something sensible if it causes a problem.
124// rdar://problem/9414925
125void test11(void) {
126 _Bool b;
127 asm volatile ("movb %%gs:%P2,%b0" : "=q"(b) : "0"(0), "i"(5L));
128}
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000129
130void test12(void) {
131 register int cc __asm ("cc"); // expected-error{{unknown register name 'cc' in asm}}
132}
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000133
134// PR10223
135void test13(void) {
136 void *esp;
137 __asm__ volatile ("mov %%esp, %o" : "=r"(esp) : : ); // expected-error {{invalid % escape in inline assembly string}}
138}
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000139
140// <rdar://problem/12700799>
Bill Wendling55ab0c52013-03-27 23:26:09 +0000141struct S; // expected-note 2 {{forward declaration of 'struct S'}}
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000142void test14(struct S *s) {
143 __asm("": : "a"(*s)); // expected-error {{dereference of pointer to incomplete type 'struct S'}}
144 __asm("": "=a" (*s) :); // expected-error {{dereference of pointer to incomplete type 'struct S'}}
145}
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000146
147// PR15759.
148double test15() {
149 double ret = 0;
150 __asm("0.0":"="(ret)); // expected-error {{invalid output constraint '=' in asm}}
151 __asm("0.0":"=&"(ret)); // expected-error {{invalid output constraint '=&' in asm}}
152 __asm("0.0":"+?"(ret)); // expected-error {{invalid output constraint '+?' in asm}}
153 __asm("0.0":"+!"(ret)); // expected-error {{invalid output constraint '+!' in asm}}
154 __asm("0.0":"+#"(ret)); // expected-error {{invalid output constraint '+#' in asm}}
155 __asm("0.0":"+*"(ret)); // expected-error {{invalid output constraint '+*' in asm}}
156 __asm("0.0":"=%"(ret)); // expected-error {{invalid output constraint '=%' in asm}}
157 __asm("0.0":"=,="(ret)); // expected-error {{invalid output constraint '=,=' in asm}}
158 __asm("0.0":"=,g"(ret)); // no-error
159 __asm("0.0":"=g"(ret)); // no-error
160 return ret;
161}
Renato Golin2e31e4e2014-06-05 16:45:22 +0000162
Marina Yatsina33eb7752017-06-26 15:55:51 +0000163void iOutputConstraint(int x){
164 __asm ("nop" : "=ir" (x) : :); // no-error
165 __asm ("nop" : "=ri" (x) : :); // no-error
166 __asm ("nop" : "=ig" (x) : :); // no-error
167 __asm ("nop" : "=im" (x) : :); // no-error
168 __asm ("nop" : "=imr" (x) : :); // no-error
169 __asm ("nop" : "=i" (x) : :); // expected-error{{invalid output constraint '=i' in asm}}
170 __asm ("nop" : "+i" (x) : :); // expected-error{{invalid output constraint '+i' in asm}}
171 __asm ("nop" : "=ii" (x) : :); // expected-error{{invalid output constraint '=ii' in asm}}
172 __asm ("nop" : "=nr" (x) : :); // no-error
173 __asm ("nop" : "=rn" (x) : :); // no-error
174 __asm ("nop" : "=ng" (x) : :); // no-error
175 __asm ("nop" : "=nm" (x) : :); // no-error
176 __asm ("nop" : "=nmr" (x) : :); // no-error
177 __asm ("nop" : "=n" (x) : :); // expected-error{{invalid output constraint '=n' in asm}}
178 __asm ("nop" : "+n" (x) : :); // expected-error{{invalid output constraint '+n' in asm}}
179 __asm ("nop" : "=nn" (x) : :); // expected-error{{invalid output constraint '=nn' in asm}}
180 __asm ("nop" : "=Fr" (x) : :); // no-error
181 __asm ("nop" : "=rF" (x) : :); // no-error
182 __asm ("nop" : "=Fg" (x) : :); // no-error
183 __asm ("nop" : "=Fm" (x) : :); // no-error
184 __asm ("nop" : "=Fmr" (x) : :); // no-error
185 __asm ("nop" : "=F" (x) : :); // expected-error{{invalid output constraint '=F' in asm}}
186 __asm ("nop" : "+F" (x) : :); // expected-error{{invalid output constraint '+F' in asm}}
187 __asm ("nop" : "=FF" (x) : :); // expected-error{{invalid output constraint '=FF' in asm}}
188 __asm ("nop" : "=Er" (x) : :); // no-error
189 __asm ("nop" : "=rE" (x) : :); // no-error
190 __asm ("nop" : "=Eg" (x) : :); // no-error
191 __asm ("nop" : "=Em" (x) : :); // no-error
192 __asm ("nop" : "=Emr" (x) : :); // no-error
193 __asm ("nop" : "=E" (x) : :); // expected-error{{invalid output constraint '=E' in asm}}
194 __asm ("nop" : "+E" (x) : :); // expected-error{{invalid output constraint '+E' in asm}}
195 __asm ("nop" : "=EE" (x) : :); // expected-error{{invalid output constraint '=EE' in asm}}
196}
197
Renato Golin2e31e4e2014-06-05 16:45:22 +0000198// PR19837
199struct foo {
200 int a;
Renato Golin2e31e4e2014-06-05 16:45:22 +0000201};
Akira Hatanaka8c26ea62015-11-18 00:15:28 +0000202register struct foo bar asm("esp"); // expected-error {{bad type for named register variable}}
203register float baz asm("esp"); // expected-error {{bad type for named register variable}}
204
205register int r0 asm ("edi"); // expected-error {{register 'edi' unsuitable for global register variables on this target}}
206register long long r1 asm ("esp"); // expected-error {{size of register 'esp' does not match variable size}}
207register int r2 asm ("esp");
Akira Hatanaka88ef7282014-07-18 23:30:30 +0000208
209double f_output_constraint(void) {
210 double result;
211 __asm("foo1": "=f" (result)); // expected-error {{invalid output constraint '=f' in asm}}
212 return result;
213}
David Majnemerc63fa612014-12-29 04:09:59 +0000214
215void fn1() {
216 int l;
217 __asm__(""
218 : [l] "=r"(l)
219 : "[l],m"(l)); // expected-error {{asm constraint has an unexpected number of alternatives: 1 vs 2}}
220}
David Majnemera0040df2015-01-10 10:43:19 +0000221
222void fn2() {
223 int l;
224 __asm__(""
225 : "+&m"(l)); // expected-error {{invalid output constraint '+&m' in asm}}
226}
David Majnemer50cb0552015-01-11 08:52:38 +0000227
228void fn3() {
229 int l;
230 __asm__(""
231 : "+#r"(l)); // expected-error {{invalid output constraint '+#r' in asm}}
232}
David Majnemerc71a5662015-01-11 09:39:03 +0000233
234void fn4() {
235 int l;
236 __asm__(""
237 : "=r"(l)
David Majnemerb5d2d452015-01-14 05:11:09 +0000238 : "m#"(l));
David Majnemerc71a5662015-01-11 09:39:03 +0000239}
David Majnemer55164f92015-01-11 09:57:13 +0000240
241void fn5() {
242 int l;
243 __asm__(""
244 : [g] "+r"(l)
245 : "[g]"(l)); // expected-error {{invalid input constraint '[g]' in asm}}
246}
David Majnemerb6b56432015-01-11 10:22:41 +0000247
248void fn6() {
249 int a;
250 __asm__(""
251 : "=rm"(a), "=rm"(a)
252 : "11m"(a)) // expected-error {{invalid input constraint '11m' in asm}}
253}
Alexander Musmaneae29e22015-06-05 13:40:59 +0000254
255// PR14269
256typedef struct test16_foo {
257 unsigned int field1 : 1;
258 unsigned int field2 : 2;
259 unsigned int field3 : 3;
260} test16_foo;
Andrey Bokhankod9eab9c2015-08-03 10:38:10 +0000261typedef __attribute__((vector_size(16))) int test16_bar;
Akira Hatanaka8c26ea62015-11-18 00:15:28 +0000262register int test16_baz asm("esp");
Andrey Bokhankod9eab9c2015-08-03 10:38:10 +0000263
Alexander Musmaneae29e22015-06-05 13:40:59 +0000264void test16()
265{
Andrey Bokhankod9eab9c2015-08-03 10:38:10 +0000266 test16_foo a;
267 test16_bar b;
268
Alexander Musmaneae29e22015-06-05 13:40:59 +0000269 __asm__("movl $5, %0"
Andrey Bokhankod9eab9c2015-08-03 10:38:10 +0000270 : "=rm" (a.field2)); // expected-error {{reference to a bit-field in asm input with a memory constraint '=rm'}}
Alexander Musmaneae29e22015-06-05 13:40:59 +0000271 __asm__("movl $5, %0"
272 :
Andrey Bokhankod9eab9c2015-08-03 10:38:10 +0000273 : "m" (a.field3)); // expected-error {{reference to a bit-field in asm output with a memory constraint 'm'}}
274 __asm__("movl $5, %0"
275 : "=rm" (b[2])); // expected-error {{reference to a vector element in asm input with a memory constraint '=rm'}}
276 __asm__("movl $5, %0"
277 :
278 : "m" (b[3])); // expected-error {{reference to a vector element in asm output with a memory constraint 'm'}}
279 __asm__("movl $5, %0"
280 : "=rm" (test16_baz)); // expected-error {{reference to a global register variable in asm input with a memory constraint '=rm'}}
281 __asm__("movl $5, %0"
282 :
283 : "m" (test16_baz)); // expected-error {{reference to a global register variable in asm output with a memory constraint 'm'}}
Alexander Musmaneae29e22015-06-05 13:40:59 +0000284}
285
Alexander Musman8e261be2015-09-21 14:41:00 +0000286int test17(int t0)
287{
288 int r0, r1;
289 __asm ("addl %2, %2\n\t"
290 "movl $123, %0"
291 : "=a" (r0),
292 "=&r" (r1)
293 : "1" (t0), // expected-note {{constraint '1' is already present here}}
294 "1" (t0)); // expected-error {{more than one input constraint matches the same output '1'}}
295 return r0 + r1;
296}
297