summaryrefslogtreecommitdiff
path: root/testsuite/tests/codeGen/should_run/cgrun069_cmm.cmm
blob: 13a26aa012855d10dbbb99876ec1a8d1a6b2b405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "Cmm.h"

// Test that the Memcpy, Memmove, Memset GHC intrinsic functions
// are working correctly.

section "rodata" { memsetErr : bits8[] "Memset Error - align: %d size: %d\n"; }
section "rodata" { memcpyErr : bits8[] "Memcpy Error - align: %d size: %d\n"; }
// You have to call printf with the same number of args for every call.
// This is as the LLVM backend doesn't support vararg functions.
section "rodata" { memmoveErr : bits8[] "Memmove Error Occured\n"; }

memintrinTest (W_ dummy)
{
	W_ size, src, dst, off, set;
	bits8 set8;

	// Need two versions as memset takes a word for historical reasons
	// but really its a bits8. We check that setting has ben done correctly
	// at the bits8 level, so need bits8 version for checking.
	set = 4;
	set8 = 4::bits8;

	size = 1024;
// Alignment must be constant expression
#define alignV 4

	("ptr" src) = foreign "C" malloc(size);
	("ptr" dst) = foreign "C" malloc(size);

   // Test memset
	prim %memset(src, set, size, alignV);

   // Check memset worked
	off = 0;
while1:
		if (off == size) {
			goto while1_end;
		}

		if (bits8[src + off] != set8) {
			// call with two dummy args for LLVM's benefit.
			// they'll be ignored by printf
                        foreign "C" printf(memsetErr "ptr", 0, 0);
			goto while1_end;
		}

		off = off + 1;
		goto while1;

while1_end:

   // Test memcpy
        prim %memcpy(dst, src, size, alignV);

   // Check memcpy worked
	off = 0;
while2:
		if (off == size) {
			goto while2_end;
		}

		if (bits8[dst + off] != set8) {
                        foreign "C" printf(memcpyErr, 0, 0);
			goto while2_end;
		}

		off = off + 1;
		goto while2;

while2_end:

   // Test memove
	set = 8;
	set8 = 8::bits8;
	size = 100;
	W_ src2;
	src2 = src + 50;

        prim %memset(src, set, size, alignV);
        prim %memmove(src2, src, size, alignV);

   // Check memmove worked
	off = 0;
while3:
 		if (off == size) {
 			goto while3_end;
 		}

 		if (bits8[src2 + off] != set8) {
                        foreign "C" printf(memmoveErr "ptr", 0, 0);
 			goto while3_end;
 		}

 		off = off + 1;
 		goto while3;

while3_end:

	foreign "C" free(src);
	foreign "C" free(dst);

        return (0);
}
#undef alignV

// ---------------------------------------------------------------------
// Tests for unrolling

// We generate code for each configuration of alignment and size rather
// than looping over the possible alignments/sizes as the alignment and
// size needs to be statically known for unrolling to happen.

// Below we need both 'set' and 'set8' as memset takes a word for
// historical reasons but really its a bits8. We check that setting
// has ben done correctly at the bits8 level, so need bits8 version
// for checking.
#define TEST_MEMSET(ALIGN,SIZE)                                        \
    W_ size, src, dst, off, set;                                       \
    bits8 set8;                                                        \
    set = 4;                                                           \
    set8 = 4::bits8;                                                   \
    size = SIZE;                                                       \
    ("ptr" src) = foreign "C" malloc(size);                            \
    ("ptr" dst) = foreign "C" malloc(size);                            \
    prim %memset(src, set, size, ALIGN);                               \
    off = 0;                                                           \
loop:                                                                  \
    if (off == size) {                                                 \
        goto loop_end;                                                 \
    }                                                                  \
    if (bits8[src + off] != set8) {                                    \
        foreign "C" printf(memsetErr "ptr", ALIGN, SIZE);              \
        goto loop_end;                                                 \
    }                                                                  \
    off = off + 1;                                                     \
    goto loop;                                                         \
loop_end:                                                              \
    foreign "C" free(src);                                             \
    foreign "C" free(dst);                                             \
    return (0);

// This is not exactly beutiful but we need the separate functions to
// avoid collisions between labels.
//
// The specific tests are selected with knowledge of the implementation
// in mind in order to try to cover all branches and interesting corner
// cases.

testMemset8_0   (W_ dummy) { TEST_MEMSET(8,0); }
testMemset8_8   (W_ dummy) { TEST_MEMSET(8,8); }
testMemset8_9   (W_ dummy) { TEST_MEMSET(8,9); }
testMemset8_10  (W_ dummy) { TEST_MEMSET(8,10); }
testMemset8_11  (W_ dummy) { TEST_MEMSET(8,11); }
testMemset8_12  (W_ dummy) { TEST_MEMSET(8,12); }
testMemset8_13  (W_ dummy) { TEST_MEMSET(8,13); }
testMemset8_14  (W_ dummy) { TEST_MEMSET(8,14); }
testMemset8_15  (W_ dummy) { TEST_MEMSET(8,15); }
testMemset8_16  (W_ dummy) { TEST_MEMSET(8,16); }

testMemset4_0  (W_ dummy) { TEST_MEMSET(4,0); }
testMemset4_4  (W_ dummy) { TEST_MEMSET(4,4); }
testMemset4_5  (W_ dummy) { TEST_MEMSET(4,5); }
testMemset4_6  (W_ dummy) { TEST_MEMSET(4,6); }
testMemset4_7  (W_ dummy) { TEST_MEMSET(4,7); }
testMemset4_8  (W_ dummy) { TEST_MEMSET(4,8); }

#define TEST_MEMCPY(ALIGN,SIZE)                                        \
    W_ size, src, dst, off;                                            \
    size = SIZE;                                                       \
    ("ptr" src) = foreign "C" malloc(size);                            \
    ("ptr" dst) = foreign "C" malloc(size);                            \
    off = 0;                                                           \
init:                                                                  \
    if (off == size) {                                                 \
        goto init_end;                                                 \
    }                                                                  \
    bits8[src + off] = 0xaa;                                           \
    off = off + 1;                                                     \
    goto init;                                                         \
init_end:                                                              \
    prim %memcpy(dst, src, size, ALIGN);                               \
    off = 0;                                                           \
loop:                                                                  \
    if (off == size) {                                                 \
        goto loop_end;                                                 \
    }                                                                  \
    if (bits8[dst + off] != bits8[src + off]) {                        \
        foreign "C" printf(memcpyErr "ptr", ALIGN, SIZE);              \
        goto loop_end;                                                 \
    }                                                                  \
    off = off + 1;                                                     \
    goto loop;                                                         \
loop_end:                                                              \
   foreign "C" free(src);                                              \
   foreign "C" free(dst);                                              \
   return (0);

testMemcpy8_0   (W_ dummy) { TEST_MEMCPY(8,0); }
testMemcpy8_8   (W_ dummy) { TEST_MEMCPY(8,8); }
testMemcpy8_9   (W_ dummy) { TEST_MEMCPY(8,9); }
testMemcpy8_10  (W_ dummy) { TEST_MEMCPY(8,10); }
testMemcpy8_11  (W_ dummy) { TEST_MEMCPY(8,11); }
testMemcpy8_12  (W_ dummy) { TEST_MEMCPY(8,12); }
testMemcpy8_13  (W_ dummy) { TEST_MEMCPY(8,13); }
testMemcpy8_14  (W_ dummy) { TEST_MEMCPY(8,14); }
testMemcpy8_15  (W_ dummy) { TEST_MEMCPY(8,15); }
testMemcpy8_16  (W_ dummy) { TEST_MEMCPY(8,16); }

testMemcpy4_0  (W_ dummy) { TEST_MEMCPY(4,0); }
testMemcpy4_4  (W_ dummy) { TEST_MEMCPY(4,4); }
testMemcpy4_5  (W_ dummy) { TEST_MEMCPY(4,5); }
testMemcpy4_6  (W_ dummy) { TEST_MEMCPY(4,6); }
testMemcpy4_7  (W_ dummy) { TEST_MEMCPY(4,7); }
testMemcpy4_8  (W_ dummy) { TEST_MEMCPY(4,8); }