demonstartes a bug in the tarval module
[libfirm] / ir / be / test / asm_test.c
1 #include <stdio.h>
2 #include <assert.h>
3
4 #ifdef __i386__
5 static inline unsigned char inb(const unsigned short port)
6 {
7     unsigned char val;
8
9     __asm__ __volatile__ ("inb  %w1, %0" : "=a"(val) : "dN"(port));
10
11     return val;
12 }
13
14 static inline void outb(const unsigned short port, const unsigned char val)
15 {
16         int k = val; /* just here to test the b modifier in %b0 */
17     __asm__ __volatile__ ("outb %b0, %1" : : "a"(k), "dN"(port));
18 }
19
20 static void sincostest(double arg)
21 {
22         double cos, sin;
23
24         __asm__ ("fsincos" : "=t"(cos), "=u"(sin) : "0" (arg));
25         printf("Arg: %f Sin: %f Cos: %f\n", arg, sin, cos);
26 }
27
28 static inline int mov_noeax(int val)
29 {
30         int res;
31
32         __asm__ ("movl %1, %0" : "=r"(res) : "ri" (val) : "eax");
33
34         return res;
35 }
36
37 static inline unsigned short swap16(unsigned short x)
38 {
39         __asm__("xchgb %b0, %h0 /* in: %1 out: %0 */" : "=q" (x) : "0" (x));
40         return x;
41 }
42
43 static inline unsigned int swap32(unsigned int x)
44 {
45         __asm__("bswap %0 /* %1 */" : "=r" (x) : "0" (x));
46         return x;
47 }
48
49 void inc(int *v)
50 {
51         __asm__("incl %0" : "+rm" (*v) : : "cc");
52 }
53
54 void inc2(int *v)
55 {
56         __asm__("incl %0" : "+m" (*v) : : "cc");
57 }
58
59 #if 1
60 typedef struct kernel_fd_set {
61         int bla;
62         int blup;
63 } kernel_fd_set;
64 #else
65 typedef int kernel_fd_set;
66 #endif
67
68 void fd_set(int fd, kernel_fd_set* set) {
69         __asm__("btsl %1,%0" : "=m" (*(set)) : "r" (fd) : "cc");
70 }
71
72 int fd_isset(int fd, kernel_fd_set *set) {
73         unsigned char result;
74
75         __asm__ __volatile__("btl %1,%2\n"
76                          "\tsetb %0"
77                         : "=q" (result)
78                         : "r" (fd),  "m" (*set)
79                         : "cc");
80         return result;
81 }
82
83 int justcompile(void)
84 {
85         outb(123, 42);
86         outb(12345, 42);
87         return inb(20) + inb(5);
88 }
89
90 int main()
91 {
92         kernel_fd_set s;
93         int k;
94
95         fd_set(20, &s);
96         assert(fd_isset(20, &s));
97
98         printf("Swap16(0xAABB): %X Swap32(0xAABBCCDD): %X\n",
99                swap16(0xAABB), swap32(0xAABBCCDD));
100         k = 41;
101         inc(&k);
102         printf("mov(inc(41)): %d\n", mov_noeax(k));
103
104         return mov_noeax(0);
105 }
106
107 #else
108
109 int main()
110 {
111         printf("Warning: asmtest only work on x86\n");
112         return 0;
113 }
114
115 #endif