xref: /minix/external/bsd/llvm/dist/clang/test/CodeGen/asm.c (revision 0a6a1f1d)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // PR10415
4f4a2713aSLionel Sambuc __asm__ ("foo1");
5f4a2713aSLionel Sambuc __asm__ ("foo2");
6f4a2713aSLionel Sambuc __asm__ ("foo3");
7f4a2713aSLionel Sambuc // CHECK: module asm "foo1"
8f4a2713aSLionel Sambuc // CHECK-NEXT: module asm "foo2"
9f4a2713aSLionel Sambuc // CHECK-NEXT: module asm "foo3"
10f4a2713aSLionel Sambuc 
t1(int len)11f4a2713aSLionel Sambuc void t1(int len) {
12f4a2713aSLionel Sambuc   __asm__ volatile("" : "=&r"(len), "+&r"(len));
13f4a2713aSLionel Sambuc }
14f4a2713aSLionel Sambuc 
t2(unsigned long long t)15f4a2713aSLionel Sambuc void t2(unsigned long long t)  {
16f4a2713aSLionel Sambuc   __asm__ volatile("" : "+m"(t));
17f4a2713aSLionel Sambuc }
18f4a2713aSLionel Sambuc 
t3(unsigned char * src,unsigned long long temp)19f4a2713aSLionel Sambuc void t3(unsigned char *src, unsigned long long temp) {
20f4a2713aSLionel Sambuc   __asm__ volatile("" : "+m"(temp), "+r"(src));
21f4a2713aSLionel Sambuc }
22f4a2713aSLionel Sambuc 
t4()23f4a2713aSLionel Sambuc void t4() {
24f4a2713aSLionel Sambuc   unsigned long long a;
25f4a2713aSLionel Sambuc   struct reg { unsigned long long a, b; } b;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc   __asm__ volatile ("":: "m"(a), "m"(b));
28f4a2713aSLionel Sambuc }
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc // PR3417
t5(int i)31f4a2713aSLionel Sambuc void t5(int i) {
32f4a2713aSLionel Sambuc   asm("nop" : "=r"(i) : "0"(t5));
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc // PR3641
t6(void)36f4a2713aSLionel Sambuc void t6(void) {
37f4a2713aSLionel Sambuc   __asm__ volatile("" : : "i" (t6));
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc 
t7(int a)40f4a2713aSLionel Sambuc void t7(int a) {
41f4a2713aSLionel Sambuc   __asm__ volatile("T7 NAMED: %[input]" : "+r"(a): [input] "i" (4));
42f4a2713aSLionel Sambuc   // CHECK: @t7(i32
43f4a2713aSLionel Sambuc   // CHECK: T7 NAMED: $1
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
t8()46f4a2713aSLionel Sambuc void t8() {
47f4a2713aSLionel Sambuc   __asm__ volatile("T8 NAMED MODIFIER: %c[input]" :: [input] "i" (4));
48f4a2713aSLionel Sambuc   // CHECK: @t8()
49f4a2713aSLionel Sambuc   // CHECK: T8 NAMED MODIFIER: ${0:c}
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc // PR3682
t9(unsigned int a)53f4a2713aSLionel Sambuc unsigned t9(unsigned int a) {
54f4a2713aSLionel Sambuc   asm("bswap %0 %1" : "+r" (a));
55f4a2713aSLionel Sambuc   return a;
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc // PR3908
t10(int r)59f4a2713aSLionel Sambuc void t10(int r) {
60f4a2713aSLionel Sambuc   __asm__("PR3908 %[lf] %[xx] %[li] %[r]" : [r] "+r" (r) : [lf] "mx" (0), [li] "mr" (0), [xx] "x" ((double)(0)));
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc // CHECK: @t10(
63f4a2713aSLionel Sambuc // CHECK:PR3908 $1 $3 $2 $0
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc // PR3373
t11(signed char input)67f4a2713aSLionel Sambuc unsigned t11(signed char input) {
68f4a2713aSLionel Sambuc   unsigned  output;
69f4a2713aSLionel Sambuc   __asm__("xyz"
70f4a2713aSLionel Sambuc           : "=a" (output)
71f4a2713aSLionel Sambuc           : "0" (input));
72f4a2713aSLionel Sambuc   return output;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc // PR3373
t12(unsigned input)76f4a2713aSLionel Sambuc unsigned char t12(unsigned input) {
77f4a2713aSLionel Sambuc   unsigned char output;
78f4a2713aSLionel Sambuc   __asm__("xyz"
79f4a2713aSLionel Sambuc           : "=a" (output)
80f4a2713aSLionel Sambuc           : "0" (input));
81f4a2713aSLionel Sambuc   return output;
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
t13(unsigned input)84f4a2713aSLionel Sambuc unsigned char t13(unsigned input) {
85f4a2713aSLionel Sambuc   unsigned char output;
86f4a2713aSLionel Sambuc   __asm__("xyz %1"
87f4a2713aSLionel Sambuc           : "=a" (output)
88f4a2713aSLionel Sambuc           : "0" (input));
89f4a2713aSLionel Sambuc   return output;
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc struct large {
93f4a2713aSLionel Sambuc   int x[1000];
94f4a2713aSLionel Sambuc };
95f4a2713aSLionel Sambuc 
t15(int x,struct large * P)96f4a2713aSLionel Sambuc unsigned long t15(int x, struct large *P) {
97f4a2713aSLionel Sambuc   __asm__("xyz "
98f4a2713aSLionel Sambuc           : "=r" (x)
99f4a2713aSLionel Sambuc           : "m" (*P), "0" (x));
100f4a2713aSLionel Sambuc   return x;
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc // bitfield destination of an asm.
104f4a2713aSLionel Sambuc struct S {
105f4a2713aSLionel Sambuc   int a : 4;
106f4a2713aSLionel Sambuc };
107f4a2713aSLionel Sambuc 
t14(struct S * P)108f4a2713aSLionel Sambuc void t14(struct S *P) {
109f4a2713aSLionel Sambuc   __asm__("abc %0" : "=r"(P->a) );
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc // PR4938
t16()113f4a2713aSLionel Sambuc int t16() {
114f4a2713aSLionel Sambuc   int a,b;
115f4a2713aSLionel Sambuc   asm ( "nop;"
116f4a2713aSLionel Sambuc        :"=%c" (a)
117f4a2713aSLionel Sambuc        : "r" (b)
118f4a2713aSLionel Sambuc        );
119f4a2713aSLionel Sambuc   return 0;
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc // PR6475
t17()123f4a2713aSLionel Sambuc void t17() {
124f4a2713aSLionel Sambuc   int i;
125f4a2713aSLionel Sambuc   __asm__ ( "nop": "=m"(i));
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc // CHECK: @t17()
128f4a2713aSLionel Sambuc // CHECK: call void asm "nop", "=*m,
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc // <rdar://problem/6841383>
t18(unsigned data)132f4a2713aSLionel Sambuc int t18(unsigned data) {
133f4a2713aSLionel Sambuc   int a, b;
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   asm("xyz" :"=a"(a), "=d"(b) : "a"(data));
136f4a2713aSLionel Sambuc   return a + b;
137f4a2713aSLionel Sambuc // CHECK: t18(i32
138f4a2713aSLionel Sambuc // CHECK: = call {{.*}}asm "xyz"
139f4a2713aSLionel Sambuc // CHECK-NEXT: extractvalue
140f4a2713aSLionel Sambuc // CHECK-NEXT: extractvalue
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc // PR6780
t19(unsigned data)144f4a2713aSLionel Sambuc int t19(unsigned data) {
145f4a2713aSLionel Sambuc   int a, b;
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc   asm("x{abc|def|ghi}z" :"=r"(a): "r"(data));
148f4a2713aSLionel Sambuc   return a + b;
149f4a2713aSLionel Sambuc   // CHECK: t19(i32
150f4a2713aSLionel Sambuc   // CHECK: = call {{.*}}asm "x$(abc$|def$|ghi$)z"
151f4a2713aSLionel Sambuc }
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc // PR6845 - Mismatching source/dest fp types.
t20(double x)154f4a2713aSLionel Sambuc double t20(double x) {
155f4a2713aSLionel Sambuc   register long double result;
156f4a2713aSLionel Sambuc   __asm __volatile ("frndint"  : "=t" (result) : "0" (x));
157f4a2713aSLionel Sambuc   return result;
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   // CHECK: @t20
160f4a2713aSLionel Sambuc   // CHECK: fpext double {{.*}} to x86_fp80
161f4a2713aSLionel Sambuc   // CHECK-NEXT: call x86_fp80 asm sideeffect "frndint"
162f4a2713aSLionel Sambuc   // CHECK: fptrunc x86_fp80 {{.*}} to double
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc 
t21(long double x)165f4a2713aSLionel Sambuc float t21(long double x) {
166f4a2713aSLionel Sambuc   register float result;
167f4a2713aSLionel Sambuc   __asm __volatile ("frndint"  : "=t" (result) : "0" (x));
168f4a2713aSLionel Sambuc   return result;
169f4a2713aSLionel Sambuc   // CHECK: @t21
170f4a2713aSLionel Sambuc   // CHECK: call x86_fp80 asm sideeffect "frndint"
171f4a2713aSLionel Sambuc   // CHECK-NEXT: fptrunc x86_fp80 {{.*}} to float
172f4a2713aSLionel Sambuc }
173f4a2713aSLionel Sambuc 
174f4a2713aSLionel Sambuc // <rdar://problem/8348447> - accept 'l' constraint
t22(unsigned char a,unsigned char b)175f4a2713aSLionel Sambuc unsigned char t22(unsigned char a, unsigned char b) {
176f4a2713aSLionel Sambuc   unsigned int la = a;
177f4a2713aSLionel Sambuc   unsigned int lb = b;
178f4a2713aSLionel Sambuc   unsigned int bigres;
179f4a2713aSLionel Sambuc   unsigned char res;
180f4a2713aSLionel Sambuc   __asm__ ("0:\n1:\n" : [bigres] "=la"(bigres) : [la] "0"(la), [lb] "c"(lb) :
181f4a2713aSLionel Sambuc                         "edx", "cc");
182f4a2713aSLionel Sambuc   res = bigres;
183f4a2713aSLionel Sambuc   return res;
184f4a2713aSLionel Sambuc }
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc // <rdar://problem/8348447> - accept 'l' constraint
t23(unsigned char a,unsigned char b)187f4a2713aSLionel Sambuc unsigned char t23(unsigned char a, unsigned char b) {
188f4a2713aSLionel Sambuc   unsigned int la = a;
189f4a2713aSLionel Sambuc   unsigned int lb = b;
190f4a2713aSLionel Sambuc   unsigned char res;
191f4a2713aSLionel Sambuc   __asm__ ("0:\n1:\n" : [res] "=la"(res) : [la] "0"(la), [lb] "c"(lb) :
192f4a2713aSLionel Sambuc                         "edx", "cc");
193f4a2713aSLionel Sambuc   return res;
194f4a2713aSLionel Sambuc }
195f4a2713aSLionel Sambuc 
t24(char c)196f4a2713aSLionel Sambuc void *t24(char c) {
197f4a2713aSLionel Sambuc   void *addr;
198f4a2713aSLionel Sambuc   // CHECK: @t24
199f4a2713aSLionel Sambuc   // CHECK: zext i8 {{.*}} to i32
200f4a2713aSLionel Sambuc   // CHECK-NEXT: call i8* asm "foobar"
201f4a2713aSLionel Sambuc   __asm__ ("foobar" : "=a" (addr) : "0" (c));
202f4a2713aSLionel Sambuc   return addr;
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc 
205f4a2713aSLionel Sambuc // PR10299 - fpsr, fpcr
t25(void)206f4a2713aSLionel Sambuc void t25(void)
207f4a2713aSLionel Sambuc {
208f4a2713aSLionel Sambuc   __asm__ __volatile__(					   \
209f4a2713aSLionel Sambuc 		       "finit"				   \
210f4a2713aSLionel Sambuc 		       :				   \
211f4a2713aSLionel Sambuc 		       :				   \
212f4a2713aSLionel Sambuc 		       :"st","st(1)","st(2)","st(3)",	   \
213f4a2713aSLionel Sambuc 			"st(4)","st(5)","st(6)","st(7)",   \
214f4a2713aSLionel Sambuc 			"fpsr","fpcr"			   \
215f4a2713aSLionel Sambuc 							   );
216f4a2713aSLionel Sambuc }
217f4a2713aSLionel Sambuc 
218f4a2713aSLionel Sambuc // rdar://10510405 - AVX registers
219f4a2713aSLionel Sambuc typedef long long __m256i __attribute__((__vector_size__(32)));
t26(__m256i * p)220f4a2713aSLionel Sambuc void t26 (__m256i *p) {
221f4a2713aSLionel Sambuc   __asm__ volatile("vmovaps  %0, %%ymm0" :: "m" (*(__m256i*)p) : "ymm0");
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc // Check to make sure the inline asm non-standard dialect attribute _not_ is
225f4a2713aSLionel Sambuc // emitted.
t27(void)226f4a2713aSLionel Sambuc void t27(void) {
227f4a2713aSLionel Sambuc   asm volatile("nop");
228f4a2713aSLionel Sambuc // CHECK: @t27
229f4a2713aSLionel Sambuc // CHECK: call void asm sideeffect "nop"
230f4a2713aSLionel Sambuc // CHECK-NOT: ia_nsdialect
231f4a2713aSLionel Sambuc // CHECK: ret void
232f4a2713aSLionel Sambuc }
233f4a2713aSLionel Sambuc 
234f4a2713aSLionel Sambuc // Check handling of '*' and '#' constraint modifiers.
t28(void)235f4a2713aSLionel Sambuc void t28(void)
236f4a2713aSLionel Sambuc {
237f4a2713aSLionel Sambuc   asm volatile ("/* %0 */" : : "i#*X,*r" (1));
238f4a2713aSLionel Sambuc // CHECK: @t28
239f4a2713aSLionel Sambuc // CHECK: call void asm sideeffect "/* $0 */", "i|r,~{dirflag},~{fpsr},~{flags}"(i32 1)
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc 
242*0a6a1f1dSLionel Sambuc static unsigned t29_var[1];
243*0a6a1f1dSLionel Sambuc 
t29(void)244*0a6a1f1dSLionel Sambuc void t29(void) {
245*0a6a1f1dSLionel Sambuc   asm volatile("movl %%eax, %0"
246*0a6a1f1dSLionel Sambuc                :
247*0a6a1f1dSLionel Sambuc                : "m"(t29_var));
248*0a6a1f1dSLionel Sambuc   // CHECK: @t29
249*0a6a1f1dSLionel Sambuc   // CHECK: call void asm sideeffect "movl %eax, $0", "*m,~{dirflag},~{fpsr},~{flags}"([1 x i32]* @t29_var)
250*0a6a1f1dSLionel Sambuc }
251*0a6a1f1dSLionel Sambuc 
t30(int len)252*0a6a1f1dSLionel Sambuc void t30(int len) {
253*0a6a1f1dSLionel Sambuc   __asm__ volatile(""
254*0a6a1f1dSLionel Sambuc                    : "+&&rm"(len));
255*0a6a1f1dSLionel Sambuc   // CHECK: @t30
256*0a6a1f1dSLionel Sambuc   // CHECK: call void asm sideeffect "", "=*&rm,0,~{dirflag},~{fpsr},~{flags}"
257*0a6a1f1dSLionel Sambuc }
258*0a6a1f1dSLionel Sambuc 
t31(int len)259*0a6a1f1dSLionel Sambuc void t31(int len) {
260*0a6a1f1dSLionel Sambuc   __asm__ volatile(""
261*0a6a1f1dSLionel Sambuc                    : "+%%rm"(len), "+rm"(len));
262*0a6a1f1dSLionel Sambuc   // CHECK: @t31
263*0a6a1f1dSLionel Sambuc   // CHECK: call void asm sideeffect "", "=*%rm,=*rm,0,1,~{dirflag},~{fpsr},~{flags}"
264*0a6a1f1dSLionel Sambuc }
265