Repository navigation
Expand file tree
/
Copy pathDynCodeCrossTargetTests.cpp
More file actions
287 lines (267 loc) · 8.49 KB
/
Copy pathDynCodeCrossTargetTests.cpp
File metadata and controls
287 lines (267 loc) · 8.49 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "NeverCTestFixture.h"
class DynCodeCrossTargetTest : public NeverCTest {};
TEST_F(DynCodeCrossTargetTest, PureComputation) {
auto src = tmpFile("cross_pure.c");
writeFile(src, R"(
int dyncode_entry(int a, int b) {
int acc = 0;
for (int i = 0; i < 4; ++i) acc = acc * 31 + a + b * i;
return acc;
}
)");
dyncodeCrossCompile("pure", src.string());
}
TEST_F(DynCodeCrossTargetTest, Echo) {
auto src = tmpFile("cross_echo.c");
writeFile(src, R"(
#include <unistd.h>
int main(void) { write(1, "OK\n", 3); _exit(0); return 0; }
)");
dyncodeCrossCompile("echo", src.string());
}
TEST_F(DynCodeCrossTargetTest, KRDeclarations) {
auto src = tmpFile("cross_kr.c");
writeFile(src, R"(
typedef __SIZE_TYPE__ size_t;
long write(int, const void *, size_t);
void _exit(int);
int main(void) { write(1, "KR\n", 3); _exit(0); return 0; }
)");
dyncodeCrossCompile("kr", src.string());
}
TEST_F(DynCodeCrossTargetTest, Heavy) {
auto src = tmpFile("cross_heavy.c");
writeFile(src, R"(
#include <string.h>
#include <unistd.h>
int main(void) {
char dst[32];
const char *src = "abcdefghijklmnop";
memset(dst, 0, sizeof(dst));
memcpy(dst, src, 16);
if (memcmp(dst, src, 16) != 0) _exit(1);
if ((int)strlen(dst) != 16) _exit(2);
write(1, dst, 16);
write(1, "\n", 1);
_exit(0);
return 0;
}
)");
dyncodeCrossCompile("heavy", src.string());
}
TEST_F(DynCodeCrossTargetTest, R0PureCompute) {
auto src = tmpFile("cross_r0_pure.c");
writeFile(src, R"(
int dyncode_entry(int seed) {
int acc = seed;
for (int i = 0; i < 5; ++i) acc = acc * 17 + 3;
return acc;
}
)");
dyncodeCrossCompileKernel("r0_pure", src.string());
}
TEST_F(DynCodeCrossTargetTest, R0Extern) {
auto src = tmpFile("cross_r0_extern.c");
writeFile(src, R"(
#include <neverc/dyncode/kernel.h>
extern void platform_log(const char *msg);
NEVERC_KERNEL_ENTRY
int dyncode_entry(int seed) {
platform_log("hello from dyncode");
return seed * 31;
}
)");
dyncodeCrossCompileKernel("r0_extern", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3Atomics) {
auto src = tmpFile("cross_r3_atomic.c");
writeFile(src, R"(
int dyncode_entry(int seed) {
int a = 0; long long b = 0;
__atomic_store_n(&a, seed, __ATOMIC_SEQ_CST);
int x = __atomic_load_n(&a, __ATOMIC_SEQ_CST);
int y = __atomic_fetch_add(&a, 7, __ATOMIC_SEQ_CST);
__atomic_store_n(&b, (long long)seed << 8, __ATOMIC_SEQ_CST);
long long z = __atomic_fetch_or(&b, 0xff, __ATOMIC_SEQ_CST);
return x + y + (int)(z >> 8);
}
)");
dyncodeCrossCompile("r3_atomic", src.string());
dyncodeCrossCompileKernel("r0_atomic", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3Float) {
auto src = tmpFile("cross_r3_float.c");
writeFile(src, R"(
int dyncode_entry(int a, int b) {
double x = (double)a * 3.14;
double y = (double)b / 2.0;
float z = (float)a * 1.5f + (float)b * 0.25f;
return (int)(x + y + (double)z);
}
)");
dyncodeCrossCompile("r3_float", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3Struct) {
auto src = tmpFile("cross_struct.c");
writeFile(src, R"(
struct point { int x, y; };
struct point make_pt(int x, int y) { struct point p = {x, y}; return p; }
int sum_pt(struct point p) { return p.x + p.y; }
int dyncode_entry(int a, int b) {
struct point p = make_pt(a, b);
return sum_pt(p) + p.x * p.y;
}
)");
dyncodeCrossCompile("r3_struct", src.string());
dyncodeCrossCompileKernel("r0_struct", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3Bitops) {
auto src = tmpFile("cross_bitops.c");
writeFile(src, R"(
int dyncode_entry(unsigned a, unsigned long long b) {
int r = 0;
r += __builtin_clz(a | 1);
r += __builtin_ctz(a | 0x100);
r += __builtin_popcount(a);
r += __builtin_clzll(b | 1);
r += __builtin_ctzll(b | 0x100ULL);
r += __builtin_popcountll(b);
return r;
}
)");
dyncodeCrossCompile("r3_bitops", src.string());
dyncodeCrossCompileKernel("r0_bitops", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3ThreadLocal) {
auto src = tmpFile("cross_tls.c");
writeFile(src, R"(
_Thread_local int counter = 0;
int dyncode_entry(int x) { counter += x; return counter * 3; }
)");
dyncodeCrossCompile("r3_tls", src.string());
dyncodeCrossCompileKernel("r0_tls", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3DesignatedInit) {
auto src = tmpFile("cross_desig.c");
writeFile(src, R"(
struct point { int x, y, z; };
int dyncode_entry(int a, int b) {
struct point p = {.x = a, .y = b, .z = a + b};
int sum = 0;
int arr[5] = {[0] = a, [4] = b};
for (int i = 0; i < 5; ++i) sum += arr[i];
return p.x + p.y + p.z + sum;
}
)");
dyncodeCrossCompile("r3_desig", src.string());
dyncodeCrossCompileKernel("r0_desig", src.string());
}
TEST_F(DynCodeCrossTargetTest, R3VLA) {
auto src = tmpFile("cross_vla.c");
writeFile(src, R"(
int dyncode_entry(int n) {
int buf[n];
for (int i = 0; i < n; ++i) buf[i] = i * 3 + 1;
int s = 0;
for (int i = 0; i < n; ++i) s += buf[i];
return s;
}
)");
dyncodeCrossCompile("r3_vla", src.string());
}
TEST_F(DynCodeCrossTargetTest, OptMatrix) {
auto src = tmpFile("cross_opt.c");
writeFile(src, R"(
#include <unistd.h>
int dyncode_entry(void) {
static const char m[] = "ok\n";
write(1, m, 3);
return 0;
}
)");
static const char *triples[] = {
"arm64-apple-macos", "x86_64-apple-macos",
"aarch64-linux-gnu", "x86_64-linux-gnu",
"aarch64-linux-android29", "x86_64-linux-android29",
"aarch64-pc-windows-msvc", "x86_64-pc-windows-msvc",
};
for (auto *opt : {"-O0", "-O1", "-O2", "-O3", "-Os", "-Oz"}) {
for (auto *triple : triples) {
SCOPED_TRACE(std::string(opt) + " " + triple);
auto bin = tmpFile(std::string("opt_") + opt + "_" + triple + ".bin");
auto r = ncc({"-fdyncode", opt, "-target", triple, src.string(), "-o",
bin.string()});
EXPECT_TRUE(r.exitCode == 0 && fs::exists(bin) && fileSize(bin) > 0)
<< opt << " " << triple << ": failed\n" << r.err;
}
}
}
// Negative diagnostics
TEST_F(DynCodeCrossTargetTest, RejectLibm) {
auto src = tmpFile("cross_libm.c");
writeFile(src, R"(
double sqrt(double); double sin(double);
int dyncode_entry(int a) {
return (int)(sqrt((double)a) + sin((double)a));
}
)");
auto r = ncc({"-fdyncode", "-target", "aarch64-linux-gnu", src.string(),
"-o", tmpFile("libm.bin").string()});
EXPECT_NE(r.exitCode, 0);
EXPECT_TRUE(r.stderrContains("libm transcendental") ||
r.contains("libm transcendental"));
}
TEST_F(DynCodeCrossTargetTest, RejectStdio) {
auto src = tmpFile("cross_stdio.c");
writeFile(src, R"(
int printf(const char *fmt, ...);
int dyncode_entry(int a) { printf("a=%d\n", a); return a; }
)");
auto r = ncc({"-fdyncode", "-target", "aarch64-linux-gnu", src.string(),
"-o", tmpFile("stdio.bin").string()});
EXPECT_NE(r.exitCode, 0);
EXPECT_TRUE(r.stderrContains("stdio call") || r.contains("stdio call"));
}
TEST_F(DynCodeCrossTargetTest, RejectHeap) {
auto src = tmpFile("cross_heap.c");
writeFile(src, R"(
void *malloc(unsigned long); void free(void *);
int dyncode_entry(int n) {
char *p = (char *)malloc((unsigned long)n);
free(p);
return 0;
}
)");
auto r = ncc({"-fdyncode", "-fno-dyncode-heap-arena", "-target",
"aarch64-linux-gnu", src.string(), "-o",
tmpFile("heap.bin").string()});
EXPECT_NE(r.exitCode, 0);
EXPECT_TRUE(r.stderrContains("heap allocator") ||
r.contains("heap allocator"));
}
TEST_F(DynCodeCrossTargetTest, RejectConstructor) {
auto src = tmpFile("cross_ctor.c");
writeFile(src, R"(
__attribute__((constructor)) static void init(void) {}
int dyncode_entry(int x) { return x; }
)");
auto r = ncc({"-fdyncode", "-target", "aarch64-linux-gnu", src.string(),
"-o", tmpFile("ctor.bin").string()});
EXPECT_NE(r.exitCode, 0);
auto all = r.err + r.out;
EXPECT_TRUE(all.find("global constructors are not allowed") !=
std::string::npos);
}
TEST_F(DynCodeCrossTargetTest, PrintOnlyModes) {
auto src = tmpFile("cross_print_only.c");
writeFile(src, "int dyncode_entry(int x) { return x + 1; }");
for (auto *flag : {"-###", "-fdriver-only", "-ccc-print-phases",
"-ccc-print-bindings"}) {
SCOPED_TRACE(flag);
auto bin = tmpFile(std::string("print_only_") + flag + ".bin");
auto r = ncc({"-fdyncode", flag, "-target", "aarch64-linux-gnu",
src.string(), "-o", bin.string()});
EXPECT_FALSE(fs::exists(bin))
<< flag << " should not produce output file";
}
}