From adcb0891fbb0227de268d3df639b2bc816498ec7 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 7 Apr 2024 12:50:22 +0300 Subject: [PATCH] restructure project layout --- build.zig | 44 +++++++------ .../read_file.c => 01_read_file/main.c} | 0 .../malloc_read.c => 02_malloc_read/main.c} | 1 + .../main.c} | 0 .../main.c} | 0 .../write_loop.c => 05_write_loop/main.c} | 1 + src/{ => 05_write_loop}/write_loops.asm | 0 src/{ => 05_write_loop}/write_loops.h | 0 .../main.c} | 1 + .../multi_nop_loop.asm | 0 src/{ => 06_write_bytes_asm}/multi_nop_loop.h | 0 src/{ => 07_load_uop}/load_uop.asm | 0 src/{ => 07_load_uop}/load_uop.h | 0 src/{tests/load_uop.c => 07_load_uop/main.c} | 1 + .../store_uop.c => 08_store_uop/main.c} | 0 src/{ => 08_store_uop}/store_uop.asm | 0 src/{ => 08_store_uop}/store_uop.h | 0 .../main.c} | 0 .../short_load_uop.asm | 0 src/{ => 09_short_load_uop}/short_load_uop.h | 0 .../read_widths.c => 10_read_widths/main.c} | 1 + src/{ => 10_read_widths}/read_widths.asm | 0 src/{ => 10_read_widths}/read_widths.h | 0 src/{ => 11_cache_size}/cyclic_load_bytes.asm | 0 src/{ => 11_cache_size}/cyclic_load_bytes.h | 0 .../cache_size.c => 11_cache_size/main.c} | 0 src/main.c | 64 ------------------- 27 files changed, 28 insertions(+), 85 deletions(-) rename src/{tests/read_file.c => 01_read_file/main.c} (100%) rename src/{tests/malloc_read.c => 02_malloc_read/main.c} (99%) rename src/{tests/write_all_bytes.c => 03_write_all_bytes/main.c} (100%) rename src/{tests/write_backward.c => 04_write_backward/main.c} (100%) rename src/{tests/write_loop.c => 05_write_loop/main.c} (99%) rename src/{ => 05_write_loop}/write_loops.asm (100%) rename src/{ => 05_write_loop}/write_loops.h (100%) rename src/{tests/write_bytes_asm.c => 06_write_bytes_asm/main.c} (99%) rename src/{ => 06_write_bytes_asm}/multi_nop_loop.asm (100%) rename src/{ => 06_write_bytes_asm}/multi_nop_loop.h (100%) rename src/{ => 07_load_uop}/load_uop.asm (100%) rename src/{ => 07_load_uop}/load_uop.h (100%) rename src/{tests/load_uop.c => 07_load_uop/main.c} (99%) rename src/{tests/store_uop.c => 08_store_uop/main.c} (100%) rename src/{ => 08_store_uop}/store_uop.asm (100%) rename src/{ => 08_store_uop}/store_uop.h (100%) rename src/{tests/short_load_uop.c => 09_short_load_uop/main.c} (100%) rename src/{ => 09_short_load_uop}/short_load_uop.asm (100%) rename src/{ => 09_short_load_uop}/short_load_uop.h (100%) rename src/{tests/read_widths.c => 10_read_widths/main.c} (99%) rename src/{ => 10_read_widths}/read_widths.asm (100%) rename src/{ => 10_read_widths}/read_widths.h (100%) rename src/{ => 11_cache_size}/cyclic_load_bytes.asm (100%) rename src/{ => 11_cache_size}/cyclic_load_bytes.h (100%) rename src/{tests/cache_size.c => 11_cache_size/main.c} (100%) delete mode 100644 src/main.c diff --git a/build.zig b/build.zig index f570872..c0ce427 100644 --- a/build.zig +++ b/build.zig @@ -14,11 +14,13 @@ fn addLinuxAssembly(b: *Builder, filename: []const u8) !std.Build.LazyPath { return output_obj; } -fn addAllLinuxAssmeblies(b: *Builder) !std.ArrayList(std.Build.LazyPath) { - var linux_assemblies = std.ArrayList(std.Build.LazyPath).init(b.allocator); +fn addAllLinuxAssmeblies(b: *Builder, path: []const u8) !std.ArrayList(std.Build.LazyPath) { + const allocator = b.allocator; + + var linux_assemblies = std.ArrayList(std.Build.LazyPath).init(allocator); errdefer linux_assemblies.deinit(); - var dir = try std.fs.cwd().openIterableDir("src", .{ }); + var dir = try std.fs.cwd().openIterableDir(path, .{ }); var it = dir.iterate(); while (try it.next()) |file| { if (file.kind != .file) continue; @@ -26,8 +28,8 @@ fn addAllLinuxAssmeblies(b: *Builder) !std.ArrayList(std.Build.LazyPath) { const ext = std.fs.path.extension(file.name); if (!std.mem.eql(u8, ext, ".asm")) continue; - const assembly_path = try std.mem.concat(b.allocator, u8, &.{ "src/", file.name }); - defer b.allocator.free(assembly_path); + const assembly_path = try std.fs.path.join(allocator, &.{ path, file.name }); + defer allocator.free(assembly_path); try linux_assemblies.append(try addLinuxAssembly(b, assembly_path)); } @@ -39,32 +41,32 @@ pub fn build(b: *Builder) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - var linux_assemblies = try addAllLinuxAssmeblies(b); - defer linux_assemblies.deinit(); + const allocator = b.allocator; - var dir = try std.fs.cwd().openIterableDir("src/tests", .{ }); + var dir = try std.fs.cwd().openIterableDir("src", .{ }); var it = dir.iterate(); - while (try it.next()) |file| { - if (file.kind != .file) continue; + while (try it.next()) |entry| { + if (entry.kind != .directory) continue; - const ext = std.fs.path.extension(file.name); - if (!std.mem.eql(u8, ext, ".c")) continue; + const program_dir = try std.fs.path.join(allocator, &.{ "src", entry.name }); + defer allocator.free(program_dir); - const source_file_path = try std.mem.concat(b.allocator, u8, &.{ "src/tests/", file.name }); - defer b.allocator.free(source_file_path); - - const executable_name = std.fs.path.stem(file.name); + const main_c = try std.fs.path.join(allocator, &.{ program_dir, "main.c" }); + defer allocator.free(main_c); const exe = b.addExecutable(.{ - .name = executable_name, - .root_source_file = .{ .path = source_file_path }, + .name = entry.name, + .root_source_file = .{ .path = main_c }, .optimize = optimize, .target = target }); + exe.addIncludePath(.{ .path = program_dir }); exe.addIncludePath(.{ .path = "src" }); exe.linkLibC(); - for (linux_assemblies.items) |obj| { + var assemblies = try addAllLinuxAssmeblies(b, program_dir); + defer assemblies.deinit(); + for (assemblies.items) |obj| { exe.addObjectFile(obj); } @@ -75,9 +77,9 @@ pub fn build(b: *Builder) !void { run_exe.addArgs(args); } - const step_description = try std.fmt.allocPrint(b.allocator, "Run '{s}' test", .{source_file_path}); + const step_description = try std.fmt.allocPrint(b.allocator, "Run '{s}' test", .{ entry.name }); defer b.allocator.free(step_description); - const run_step = b.step(executable_name, step_description); + const run_step = b.step(entry.name, step_description); run_step.dependOn(&run_exe.step); } } diff --git a/src/tests/read_file.c b/src/01_read_file/main.c similarity index 100% rename from src/tests/read_file.c rename to src/01_read_file/main.c diff --git a/src/tests/malloc_read.c b/src/02_malloc_read/main.c similarity index 99% rename from src/tests/malloc_read.c rename to src/02_malloc_read/main.c index 3d7fbd6..7045257 100644 --- a/src/tests/malloc_read.c +++ b/src/02_malloc_read/main.c @@ -119,3 +119,4 @@ int main(int argc, char **argv) { return 0; } + diff --git a/src/tests/write_all_bytes.c b/src/03_write_all_bytes/main.c similarity index 100% rename from src/tests/write_all_bytes.c rename to src/03_write_all_bytes/main.c diff --git a/src/tests/write_backward.c b/src/04_write_backward/main.c similarity index 100% rename from src/tests/write_backward.c rename to src/04_write_backward/main.c diff --git a/src/tests/write_loop.c b/src/05_write_loop/main.c similarity index 99% rename from src/tests/write_loop.c rename to src/05_write_loop/main.c index e5d0d35..99aee6a 100644 --- a/src/tests/write_loop.c +++ b/src/05_write_loop/main.c @@ -54,3 +54,4 @@ int main() { return 0; } + diff --git a/src/write_loops.asm b/src/05_write_loop/write_loops.asm similarity index 100% rename from src/write_loops.asm rename to src/05_write_loop/write_loops.asm diff --git a/src/write_loops.h b/src/05_write_loop/write_loops.h similarity index 100% rename from src/write_loops.h rename to src/05_write_loop/write_loops.h diff --git a/src/tests/write_bytes_asm.c b/src/06_write_bytes_asm/main.c similarity index 99% rename from src/tests/write_bytes_asm.c rename to src/06_write_bytes_asm/main.c index eb2ec38..9f89ce6 100644 --- a/src/tests/write_bytes_asm.c +++ b/src/06_write_bytes_asm/main.c @@ -38,3 +38,4 @@ int main() { return 0; } + diff --git a/src/multi_nop_loop.asm b/src/06_write_bytes_asm/multi_nop_loop.asm similarity index 100% rename from src/multi_nop_loop.asm rename to src/06_write_bytes_asm/multi_nop_loop.asm diff --git a/src/multi_nop_loop.h b/src/06_write_bytes_asm/multi_nop_loop.h similarity index 100% rename from src/multi_nop_loop.h rename to src/06_write_bytes_asm/multi_nop_loop.h diff --git a/src/load_uop.asm b/src/07_load_uop/load_uop.asm similarity index 100% rename from src/load_uop.asm rename to src/07_load_uop/load_uop.asm diff --git a/src/load_uop.h b/src/07_load_uop/load_uop.h similarity index 100% rename from src/load_uop.h rename to src/07_load_uop/load_uop.h diff --git a/src/tests/load_uop.c b/src/07_load_uop/main.c similarity index 99% rename from src/tests/load_uop.c rename to src/07_load_uop/main.c index 4c85231..7f3fd96 100644 --- a/src/tests/load_uop.c +++ b/src/07_load_uop/main.c @@ -38,3 +38,4 @@ int main() { return 0; } + diff --git a/src/tests/store_uop.c b/src/08_store_uop/main.c similarity index 100% rename from src/tests/store_uop.c rename to src/08_store_uop/main.c diff --git a/src/store_uop.asm b/src/08_store_uop/store_uop.asm similarity index 100% rename from src/store_uop.asm rename to src/08_store_uop/store_uop.asm diff --git a/src/store_uop.h b/src/08_store_uop/store_uop.h similarity index 100% rename from src/store_uop.h rename to src/08_store_uop/store_uop.h diff --git a/src/tests/short_load_uop.c b/src/09_short_load_uop/main.c similarity index 100% rename from src/tests/short_load_uop.c rename to src/09_short_load_uop/main.c diff --git a/src/short_load_uop.asm b/src/09_short_load_uop/short_load_uop.asm similarity index 100% rename from src/short_load_uop.asm rename to src/09_short_load_uop/short_load_uop.asm diff --git a/src/short_load_uop.h b/src/09_short_load_uop/short_load_uop.h similarity index 100% rename from src/short_load_uop.h rename to src/09_short_load_uop/short_load_uop.h diff --git a/src/tests/read_widths.c b/src/10_read_widths/main.c similarity index 99% rename from src/tests/read_widths.c rename to src/10_read_widths/main.c index 317aa23..3433b3e 100644 --- a/src/tests/read_widths.c +++ b/src/10_read_widths/main.c @@ -37,3 +37,4 @@ int main() { return 0; } + diff --git a/src/read_widths.asm b/src/10_read_widths/read_widths.asm similarity index 100% rename from src/read_widths.asm rename to src/10_read_widths/read_widths.asm diff --git a/src/read_widths.h b/src/10_read_widths/read_widths.h similarity index 100% rename from src/read_widths.h rename to src/10_read_widths/read_widths.h diff --git a/src/cyclic_load_bytes.asm b/src/11_cache_size/cyclic_load_bytes.asm similarity index 100% rename from src/cyclic_load_bytes.asm rename to src/11_cache_size/cyclic_load_bytes.asm diff --git a/src/cyclic_load_bytes.h b/src/11_cache_size/cyclic_load_bytes.h similarity index 100% rename from src/cyclic_load_bytes.h rename to src/11_cache_size/cyclic_load_bytes.h diff --git a/src/tests/cache_size.c b/src/11_cache_size/main.c similarity index 100% rename from src/tests/cache_size.c rename to src/11_cache_size/main.c diff --git a/src/main.c b/src/main.c deleted file mode 100644 index ca707ea..0000000 --- a/src/main.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include -#include - -#include "repetition_tester.c" - -#define RPROF_IMPLEMENTATION -#include "rprof.h" - -#include "main_read_file.c" -#include "main_write_bytes_asm.c" -#include "main_write_all_bytes.c" -#include "main_malloc_read.c" -#include "main_write_backward.c" -#include "main_write_loop.c" -#include "main_load_uop.c" -#include "main_store_uop.c" -#include "main_short_load_uop.c" -#include "main_read_widths.c" - -int main(int argc, char **argv) { - if (argc < 2) { - printf("Usage: %s \n", argv[0]); - return -1; - } - - char *test_name = argv[1]; - - if (!strncmp(test_name, "write_bytes_asm", sizeof("write_bytes_asm"))) { - return main_test_write_bytes_asm(); - } else if (!strncmp(test_name, "read_widths", sizeof("read_widths"))) { - return main_test_read_widths(); - } else if (!strncmp(test_name, "write_all_bytes", sizeof("write_bytes"))) { - return main_test_write_all_bytes(); - } else if (!strncmp(test_name, "load_uop", sizeof("load_uop"))) { - return main_test_load_uop(); - } else if (!strncmp(test_name, "store_uop", sizeof("store_uop"))) { - return main_test_store_uop(); - } else if (!strncmp(test_name, "short_load_uop", sizeof("short_load_uop"))) { - return main_test_short_load_uop(); - } else if (!strncmp(test_name, "write_loop", sizeof("write_loop"))) { - return main_test_write_loop(); - } else if (!strncmp(test_name, "write_backward", sizeof("write_backward"))) { - return main_test_write_backward(); - } else if (!strncmp(test_name, "read_file", sizeof("read_file"))) { - if (argc < 3) { - printf("Usage: %s read_file \n", argv[0]); - return -1; - } - - return main_test_read_file(argv[2]); - } else if (!strncmp(test_name, "malloc_read", sizeof("malloc_read"))) { - if (argc < 3) { - printf("Usage: %s malloc_read \n", argv[0]); - return -1; - } - - return main_test_malloc_read(argv[2]); - } else { - printf("ERROR: Unknown test case '%s'\n", test_name); - return -1; - } -}