| ... | @@ -1,438 +0,0 @@ |
| 1 | const std = @import("std"); |
| 2 | const TestContext = @import("../../src/test.zig").TestContext; |
| 3 | const build_options = @import("build_options"); |
| 4 | |
| 5 | // These tests should work with all platforms, but we're using linux_x64 for |
| 6 | // now for consistency. Will be expanded eventually. |
| 7 | const linux_x64 = std.zig.CrossTarget{ |
| 8 | .cpu_arch = .x86_64, |
| 9 | .os_tag = .linux, |
| 10 | }; |
| 11 | |
| 12 | pub fn addCases(ctx: *TestContext) !void { |
| 13 | { |
| 14 | var case = ctx.exeUsingLlvmBackend("simple addition and subtraction", linux_x64); |
| 15 | |
| 16 | case.addCompareOutput( |
| 17 | \\fn add(a: i32, b: i32) i32 { |
| 18 | \\ return a + b; |
| 19 | \\} |
| 20 | \\ |
| 21 | \\pub export fn main() c_int { |
| 22 | \\ var a: i32 = -5; |
| 23 | \\ const x = add(a, 7); |
| 24 | \\ var y = add(2, 0); |
| 25 | \\ y -= x; |
| 26 | \\ return y; |
| 27 | \\} |
| 28 | , ""); |
| 29 | } |
| 30 | |
| 31 | { |
| 32 | var case = ctx.exeUsingLlvmBackend("shift right + left", linux_x64); |
| 33 | |
| 34 | case.addCompareOutput( |
| 35 | \\pub export fn main() c_int { |
| 36 | \\ var i: u32 = 16; |
| 37 | \\ assert(i >> 1, 8); |
| 38 | \\ return 0; |
| 39 | \\} |
| 40 | \\fn assert(a: u32, b: u32) void { |
| 41 | \\ if (a != b) unreachable; |
| 42 | \\} |
| 43 | , ""); |
| 44 | case.addCompareOutput( |
| 45 | \\pub export fn main() c_int { |
| 46 | \\ var i: u32 = 16; |
| 47 | \\ assert(i << 1, 32); |
| 48 | \\ return 0; |
| 49 | \\} |
| 50 | \\fn assert(a: u32, b: u32) void { |
| 51 | \\ if (a != b) unreachable; |
| 52 | \\} |
| 53 | , ""); |
| 54 | } |
| 55 | |
| 56 | { |
| 57 | var case = ctx.exeUsingLlvmBackend("llvm hello world", linux_x64); |
| 58 | |
| 59 | case.addCompareOutput( |
| 60 | \\extern fn puts(s: [*:0]const u8) c_int; |
| 61 | \\ |
| 62 | \\pub export fn main() c_int { |
| 63 | \\ _ = puts("hello world!"); |
| 64 | \\ return 0; |
| 65 | \\} |
| 66 | , "hello world!" ++ std.cstr.line_sep); |
| 67 | } |
| 68 | |
| 69 | { |
| 70 | var case = ctx.exeUsingLlvmBackend("simple if statement", linux_x64); |
| 71 | |
| 72 | case.addCompareOutput( |
| 73 | \\fn add(a: i32, b: i32) i32 { |
| 74 | \\ return a + b; |
| 75 | \\} |
| 76 | \\ |
| 77 | \\fn assert(ok: bool) void { |
| 78 | \\ if (!ok) unreachable; |
| 79 | \\} |
| 80 | \\ |
| 81 | \\pub export fn main() c_int { |
| 82 | \\ assert(add(1,2) == 3); |
| 83 | \\ return 0; |
| 84 | \\} |
| 85 | , ""); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | var case = ctx.exeUsingLlvmBackend("blocks", linux_x64); |
| 90 | |
| 91 | case.addCompareOutput( |
| 92 | \\fn assert(ok: bool) void { |
| 93 | \\ if (!ok) unreachable; |
| 94 | \\} |
| 95 | \\ |
| 96 | \\fn foo(ok: bool) i32 { |
| 97 | \\ const val: i32 = blk: { |
| 98 | \\ var x: i32 = 1; |
| 99 | \\ if (!ok) break :blk x + 9; |
| 100 | \\ break :blk x + 19; |
| 101 | \\ }; |
| 102 | \\ return val + 10; |
| 103 | \\} |
| 104 | \\ |
| 105 | \\pub export fn main() c_int { |
| 106 | \\ assert(foo(false) == 20); |
| 107 | \\ assert(foo(true) == 30); |
| 108 | \\ return 0; |
| 109 | \\} |
| 110 | , ""); |
| 111 | } |
| 112 | |
| 113 | { |
| 114 | var case = ctx.exeUsingLlvmBackend("nested blocks", linux_x64); |
| 115 | |
| 116 | case.addCompareOutput( |
| 117 | \\fn assert(ok: bool) void { |
| 118 | \\ if (!ok) unreachable; |
| 119 | \\} |
| 120 | \\ |
| 121 | \\fn foo(ok: bool) i32 { |
| 122 | \\ var val: i32 = blk: { |
| 123 | \\ const val2: i32 = another: { |
| 124 | \\ if (!ok) break :blk 10; |
| 125 | \\ break :another 10; |
| 126 | \\ }; |
| 127 | \\ break :blk val2 + 10; |
| 128 | \\ }; |
| 129 | \\ return val; |
| 130 | \\} |
| 131 | \\ |
| 132 | \\pub export fn main() c_int { |
| 133 | \\ assert(foo(false) == 10); |
| 134 | \\ assert(foo(true) == 20); |
| 135 | \\ return 0; |
| 136 | \\} |
| 137 | , ""); |
| 138 | } |
| 139 | |
| 140 | { |
| 141 | var case = ctx.exeUsingLlvmBackend("while loops", linux_x64); |
| 142 | |
| 143 | case.addCompareOutput( |
| 144 | \\fn assert(ok: bool) void { |
| 145 | \\ if (!ok) unreachable; |
| 146 | \\} |
| 147 | \\ |
| 148 | \\pub export fn main() c_int { |
| 149 | \\ var sum: u32 = 0; |
| 150 | \\ var i: u32 = 0; |
| 151 | \\ while (i < 5) : (i += 1) { |
| 152 | \\ sum += i; |
| 153 | \\ } |
| 154 | \\ assert(sum == 10); |
| 155 | \\ assert(i == 5); |
| 156 | \\ return 0; |
| 157 | \\} |
| 158 | , ""); |
| 159 | } |
| 160 | |
| 161 | { |
| 162 | var case = ctx.exeUsingLlvmBackend("optionals", linux_x64); |
| 163 | |
| 164 | case.addCompareOutput( |
| 165 | \\fn assert(ok: bool) void { |
| 166 | \\ if (!ok) unreachable; |
| 167 | \\} |
| 168 | \\ |
| 169 | \\pub export fn main() c_int { |
| 170 | \\ var opt_val: ?i32 = 10; |
| 171 | \\ var null_val: ?i32 = null; |
| 172 | \\ |
| 173 | \\ var val1: i32 = opt_val.?; |
| 174 | \\ const val1_1: i32 = opt_val.?; |
| 175 | \\ var ptr_val1 = &(opt_val.?); |
| 176 | \\ const ptr_val1_1 = &(opt_val.?); |
| 177 | \\ |
| 178 | \\ var val2: i32 = null_val orelse 20; |
| 179 | \\ const val2_2: i32 = null_val orelse 20; |
| 180 | \\ |
| 181 | \\ var value: i32 = 20; |
| 182 | \\ var ptr_val2 = &(null_val orelse value); |
| 183 | \\ |
| 184 | \\ const val3 = opt_val orelse 30; |
| 185 | \\ var val3_var = opt_val orelse 30; |
| 186 | \\ |
| 187 | \\ assert(val1 == 10); |
| 188 | \\ assert(val1_1 == 10); |
| 189 | \\ assert(ptr_val1.* == 10); |
| 190 | \\ assert(ptr_val1_1.* == 10); |
| 191 | \\ |
| 192 | \\ assert(val2 == 20); |
| 193 | \\ assert(val2_2 == 20); |
| 194 | \\ assert(ptr_val2.* == 20); |
| 195 | \\ |
| 196 | \\ assert(val3 == 10); |
| 197 | \\ assert(val3_var == 10); |
| 198 | \\ |
| 199 | \\ (null_val orelse val2) = 1234; |
| 200 | \\ assert(val2 == 1234); |
| 201 | \\ |
| 202 | \\ (opt_val orelse val2) = 5678; |
| 203 | \\ assert(opt_val.? == 5678); |
| 204 | \\ |
| 205 | \\ return 0; |
| 206 | \\} |
| 207 | , ""); |
| 208 | } |
| 209 | |
| 210 | { |
| 211 | var case = ctx.exeUsingLlvmBackend("for loop", linux_x64); |
| 212 | |
| 213 | case.addCompareOutput( |
| 214 | \\fn assert(ok: bool) void { |
| 215 | \\ if (!ok) unreachable; |
| 216 | \\} |
| 217 | \\ |
| 218 | \\pub export fn main() c_int { |
| 219 | \\ var x: u32 = 0; |
| 220 | \\ for ("hello") |_| { |
| 221 | \\ x += 1; |
| 222 | \\ } |
| 223 | \\ assert("hello".len == x); |
| 224 | \\ return 0; |
| 225 | \\} |
| 226 | , ""); |
| 227 | } |
| 228 | |
| 229 | { |
| 230 | var case = ctx.exeUsingLlvmBackend("@rem", linux_x64); |
| 231 | case.addCompareOutput( |
| 232 | \\fn assert(ok: bool) void { |
| 233 | \\ if (!ok) unreachable; |
| 234 | \\} |
| 235 | \\fn rem(lhs: i32, rhs: i32, expected: i32) bool { |
| 236 | \\ return @rem(lhs, rhs) == expected; |
| 237 | \\} |
| 238 | \\pub export fn main() c_int { |
| 239 | \\ assert(rem(-5, 3, -2)); |
| 240 | \\ assert(rem(5, 3, 2)); |
| 241 | \\ return 0; |
| 242 | \\} |
| 243 | , ""); |
| 244 | } |
| 245 | |
| 246 | { |
| 247 | var case = ctx.exeUsingLlvmBackend("invalid address space coercion", linux_x64); |
| 248 | case.addError( |
| 249 | \\fn entry(a: *addrspace(.gs) i32) *i32 { |
| 250 | \\ return a; |
| 251 | \\} |
| 252 | \\pub export fn main() void { _ = entry; } |
| 253 | , &[_][]const u8{ |
| 254 | ":2:12: error: expected *i32, found *addrspace(.gs) i32", |
| 255 | }); |
| 256 | } |
| 257 | |
| 258 | { |
| 259 | var case = ctx.exeUsingLlvmBackend("pointer keeps address space", linux_x64); |
| 260 | case.compiles( |
| 261 | \\fn entry(a: *addrspace(.gs) i32) *addrspace(.gs) i32 { |
| 262 | \\ return a; |
| 263 | \\} |
| 264 | \\pub export fn main() void { _ = entry; } |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | { |
| 269 | var case = ctx.exeUsingLlvmBackend("pointer to explicit generic address space coerces to implicit pointer", linux_x64); |
| 270 | case.compiles( |
| 271 | \\fn entry(a: *addrspace(.generic) i32) *i32 { |
| 272 | \\ return a; |
| 273 | \\} |
| 274 | \\pub export fn main() void { _ = entry; } |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | { |
| 279 | var case = ctx.exeUsingLlvmBackend("pointers with different address spaces", linux_x64); |
| 280 | case.addError( |
| 281 | \\fn entry(a: *addrspace(.gs) i32) *addrspace(.fs) i32 { |
| 282 | \\ return a; |
| 283 | \\} |
| 284 | \\pub export fn main() void { _ = entry; } |
| 285 | , &[_][]const u8{ |
| 286 | ":2:12: error: expected *addrspace(.fs) i32, found *addrspace(.gs) i32", |
| 287 | }); |
| 288 | } |
| 289 | |
| 290 | { |
| 291 | var case = ctx.exeUsingLlvmBackend("pointers with different address spaces", linux_x64); |
| 292 | case.addError( |
| 293 | \\fn entry(a: ?*addrspace(.gs) i32) *i32 { |
| 294 | \\ return a.?; |
| 295 | \\} |
| 296 | \\pub export fn main() void { _ = entry; } |
| 297 | , &[_][]const u8{ |
| 298 | ":2:13: error: expected *i32, found *addrspace(.gs) i32", |
| 299 | }); |
| 300 | } |
| 301 | |
| 302 | { |
| 303 | var case = ctx.exeUsingLlvmBackend("invalid pointer keeps address space when taking address of dereference", linux_x64); |
| 304 | case.addError( |
| 305 | \\fn entry(a: *addrspace(.gs) i32) *i32 { |
| 306 | \\ return &a.*; |
| 307 | \\} |
| 308 | \\pub export fn main() void { _ = entry; } |
| 309 | , &[_][]const u8{ |
| 310 | ":2:12: error: expected *i32, found *addrspace(.gs) i32", |
| 311 | }); |
| 312 | } |
| 313 | |
| 314 | { |
| 315 | var case = ctx.exeUsingLlvmBackend("pointer keeps address space when taking address of dereference", linux_x64); |
| 316 | case.compiles( |
| 317 | \\fn entry(a: *addrspace(.gs) i32) *addrspace(.gs) i32 { |
| 318 | \\ return &a.*; |
| 319 | \\} |
| 320 | \\pub export fn main() void { _ = entry; } |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | { |
| 325 | var case = ctx.exeUsingLlvmBackend("address spaces pointer access chaining: array pointer", linux_x64); |
| 326 | case.compiles( |
| 327 | \\fn entry(a: *addrspace(.gs) [1]i32) *addrspace(.gs) i32 { |
| 328 | \\ return &a[0]; |
| 329 | \\} |
| 330 | \\pub export fn main() void { _ = entry; } |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | { |
| 335 | var case = ctx.exeUsingLlvmBackend("address spaces pointer access chaining: pointer to optional array", linux_x64); |
| 336 | case.compiles( |
| 337 | \\fn entry(a: *addrspace(.gs) ?[1]i32) *addrspace(.gs) i32 { |
| 338 | \\ return &a.*.?[0]; |
| 339 | \\} |
| 340 | \\pub export fn main() void { _ = entry; } |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | { |
| 345 | var case = ctx.exeUsingLlvmBackend("address spaces pointer access chaining: struct pointer", linux_x64); |
| 346 | case.compiles( |
| 347 | \\const A = struct{ a: i32 }; |
| 348 | \\fn entry(a: *addrspace(.gs) A) *addrspace(.gs) i32 { |
| 349 | \\ return &a.a; |
| 350 | \\} |
| 351 | \\pub export fn main() void { _ = entry; } |
| 352 | ); |
| 353 | } |
| 354 | |
| 355 | { |
| 356 | var case = ctx.exeUsingLlvmBackend("address spaces pointer access chaining: complex", linux_x64); |
| 357 | case.compiles( |
| 358 | \\const A = struct{ a: ?[1]i32 }; |
| 359 | \\fn entry(a: *addrspace(.gs) [1]A) *addrspace(.gs) i32 { |
| 360 | \\ return &a[0].a.?[0]; |
| 361 | \\} |
| 362 | \\pub export fn main() void { _ = entry; } |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | { |
| 367 | var case = ctx.exeUsingLlvmBackend("dereferencing through multiple pointers with address spaces", linux_x64); |
| 368 | case.compiles( |
| 369 | \\fn entry(a: *addrspace(.fs) *addrspace(.gs) *i32) *i32 { |
| 370 | \\ return a.*.*; |
| 371 | \\} |
| 372 | \\pub export fn main() void { _ = entry; } |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | { |
| 377 | var case = ctx.exeUsingLlvmBackend("f segment address space reading and writing", linux_x64); |
| 378 | case.addCompareOutput( |
| 379 | \\fn assert(ok: bool) void { |
| 380 | \\ if (!ok) unreachable; |
| 381 | \\} |
| 382 | \\ |
| 383 | \\fn setFs(value: c_ulong) void { |
| 384 | \\ asm volatile ( |
| 385 | \\ \\syscall |
| 386 | \\ : |
| 387 | \\ : [number] "{rax}" (158), |
| 388 | \\ [code] "{rdi}" (0x1002), |
| 389 | \\ [val] "{rsi}" (value), |
| 390 | \\ : "rcx", "r11", "memory" |
| 391 | \\ ); |
| 392 | \\} |
| 393 | \\ |
| 394 | \\fn getFs() c_ulong { |
| 395 | \\ var result: c_ulong = undefined; |
| 396 | \\ asm volatile ( |
| 397 | \\ \\syscall |
| 398 | \\ : |
| 399 | \\ : [number] "{rax}" (158), |
| 400 | \\ [code] "{rdi}" (0x1003), |
| 401 | \\ [ptr] "{rsi}" (@ptrToInt(&result)), |
| 402 | \\ : "rcx", "r11", "memory" |
| 403 | \\ ); |
| 404 | \\ return result; |
| 405 | \\} |
| 406 | \\ |
| 407 | \\var test_value: u64 = 12345; |
| 408 | \\ |
| 409 | \\pub export fn main() c_int { |
| 410 | \\ const orig_fs = getFs(); |
| 411 | \\ |
| 412 | \\ setFs(@ptrToInt(&test_value)); |
| 413 | \\ assert(getFs() == @ptrToInt(&test_value)); |
| 414 | \\ |
| 415 | \\ var test_ptr = @intToPtr(*allowzero addrspace(.fs) u64, 0); |
| 416 | \\ assert(test_ptr.* == 12345); |
| 417 | \\ test_ptr.* = 98765; |
| 418 | \\ assert(test_value == 98765); |
| 419 | \\ |
| 420 | \\ setFs(orig_fs); |
| 421 | \\ return 0; |
| 422 | \\} |
| 423 | , ""); |
| 424 | } |
| 425 | |
| 426 | { |
| 427 | // This worked in stage1 and we expressly do not want this to work in stage2 |
| 428 | var case = ctx.exeUsingLlvmBackend("any typed null to any typed optional", linux_x64); |
| 429 | case.addError( |
| 430 | \\pub export fn main() void { |
| 431 | \\ var a: ?*anyopaque = undefined; |
| 432 | \\ a = @as(?usize, null); |
| 433 | \\} |
| 434 | , &[_][]const u8{ |
| 435 | ":3:21: error: expected *anyopaque, found ?usize", |
| 436 | }); |
| 437 | } |
| 438 | } |