summaryrefslogtreecommitdiff
path: root/bxgen.c
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-11-16 07:43:03 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-11-16 07:43:03 +0100
commit394a9698b7cfeb263a8b8fce5d268c80b73a2c87 (patch)
treea2ea84317b75b2e2a8bfa2953050896451ea59f8 /bxgen.c
parentbb121b4e6099c41e6407d2593f251e0883723132 (diff)
downloadbxgen-394a9698b7cfeb263a8b8fce5d268c80b73a2c87.zip
Add command line arguments parsing
Diffstat (limited to 'bxgen.c')
-rwxr-xr-xbxgen.c88
1 files changed, 70 insertions, 18 deletions
diff --git a/bxgen.c b/bxgen.c
index 89ef052..7e1bc4b 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -47,6 +47,7 @@
#/ - ARM
#/ - WebAssembly
#/ - Implicit procedure prototypes
+#/ - Evaluation
#/ - Static single-assignment
#/ - Sea of Nodes
#/ - Optimization layers
@@ -88,7 +89,7 @@
#/
#/ Bugs
#/
-#/ - Segfault at 0x45bb82
+#/ - Linking with libc doesn't work
#/
#/ Done features
#/
@@ -585,7 +586,7 @@ i64 p_new(i64 unit, c8 *name);
i64 p_new_entry(i64 unit);
void p_add(i64 proc, i64 node);
-i64 u_new();
+i64 u_new(void);
void u_add(i64 unit, i64 proc);
void u_entry_point(i64 unit, i64 proc);
void u_elf_x86_64(i64 unit, c8 *output_file_name);
@@ -1227,31 +1228,31 @@ typedef struct {
unsigned first:1;
} Bits;
-u32 host_bit_order() {
+u32 host_bit_order(void) {
if ((*(Bits *) &(u8) { 1 }).first == 1)
return BIT_LE;
return BIT_BE;
}
-u32 host_byte_order() {
+u32 host_byte_order(void) {
if (((u8 *) &(u32) { 1 })[0] == 1)
return BYTE_LE;
return BYTE_BE;
}
-u32 host_word_order() {
+u32 host_word_order(void) {
if (((u16 *) &(u32) { 0x100 })[0] == 0x100)
return WORD_LE;
return WORD_BE;
}
-u32 host_dword_order() {
+u32 host_dword_order(void) {
if (((u32 *) &(u64) { 0x10000 })[0] == 0x10000)
return DWORD_LE;
return DWORD_BE;
}
-void check_f32_format() {
+void check_f32_format(void) {
// FIXME
if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull)
return;
@@ -1261,7 +1262,7 @@ void check_f32_format() {
FAIL("Unknown host floating-point number format",);
}
-u32 host_f64_dword_order() {
+u32 host_f64_dword_order(void) {
if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull)
return host_dword_order() == DWORD_LE ? F64_DWORD_LE : F64_DWORD_BE;
if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull)
@@ -1270,7 +1271,7 @@ u32 host_f64_dword_order() {
FAIL("Unknown host floating-point number format", 0);
}
-u32 host_data_ordering() {
+u32 host_data_ordering(void) {
return host_bit_order() |
host_byte_order() |
host_word_order() |
@@ -2300,7 +2301,6 @@ i64 ar_find_symbol_offset_by_name(
}
Buffer_Context elf_buffer_context(
- Pool * pool,
Linker_Context *linker,
i64 num_obj_files,
i64 elf_index
@@ -2664,7 +2664,7 @@ i64 unit_write_in_memory(
// Calculate section offsets
for (i64 elf_index = 0; elf_index < linker->num_obj_files; ++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
elf_checks(buf);
@@ -2745,7 +2745,7 @@ i64 unit_write_in_memory(
i64 prev_num_symbols = num_symbols;
for (i64 elf_index = 0; elf_index < linker->num_obj_files; ++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
i64 num_sections = elf_section_headers(buf).num;
Offset_Size strtab = elf_find_section_by_name(buf, SECTION_STRTAB, sizeof SECTION_STRTAB - 1).data;
@@ -2832,7 +2832,7 @@ i64 unit_write_in_memory(
// ----------------------------------------------------------
for (i64 elf_index = 0, sec_index_global = 0; elf_index < linker->num_obj_files; ++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
Offset_Num headers = elf_section_headers(buf);
for (i64 sec_index = 1; sec_index < headers.num; ++sec_index, ++sec_index_global) {
@@ -2902,7 +2902,7 @@ i64 unit_write_in_memory(
// Relocate defined symbols
for (i64 elf_index = 0, sec_index_global = 0; elf_index < linker->num_obj_files; ++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
Offset_Num headers = elf_section_headers(buf);
Offset_Size strtab = elf_find_section_by_name(buf, SECTION_STRTAB, sizeof SECTION_STRTAB - 1).data;
@@ -3074,7 +3074,7 @@ i64 unit_write_in_memory(
rel_index_global = 0, got_offset = 0;
elf_index < linker->num_obj_files;
++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
i64 num_sections = elf_section_headers(buf).num;
Offset_Size strtab = elf_find_section_by_name(buf, SECTION_STRTAB, sizeof SECTION_STRTAB - 1).data;
@@ -3186,7 +3186,7 @@ i64 unit_write_in_memory(
// Apply relocations
for (i64 elf_index = 0, sec_index_global = 0, rel_index_global = 0; elf_index < linker->num_obj_files; ++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
i64 num_sections = elf_section_headers(buf).num;
Offset_Size strtab = elf_find_section_by_name(buf, SECTION_STRTAB, sizeof SECTION_STRTAB - 1).data;
@@ -3639,7 +3639,7 @@ i64 unit_write_in_memory(
// Write sections into the output buffer
for (i64 elf_index = 0, sec_index_global = 0; elf_index < linker->num_obj_files; ++elf_index) {
- Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
+ Buffer_Context buf = elf_buffer_context(linker, linker->num_obj_files, elf_index);
Offset_Num headers = elf_section_headers(buf);
for (i64 sec_index = 1; sec_index < headers.num; ++sec_index, ++sec_index_global) {
@@ -4178,7 +4178,7 @@ void p_add(i64 proc, i64 node) {
proc_node_add(&g_pool, proc, node);
}
-i64 u_new() {
+i64 u_new(void) {
return unit_init(&g_pool, UNIT_CODE);
}
@@ -4345,6 +4345,8 @@ b8 two_plus_two(void) {
if (WEXITSTATUS(ret) != 4)
return 0;
+
+ system("rm test_two_plus_two");
}
return 1;
@@ -4373,6 +4375,8 @@ b8 sixty_nine(void) {
if (WEXITSTATUS(ret) != 69)
return 0;
+
+ system("rm test_sixty_nine");
}
return 1;
@@ -4381,8 +4385,56 @@ b8 sixty_nine(void) {
#endif // HELPERS
i32 main(i32 argc, c8 **argv) {
+ b8 is_ok = 1;
+ b8 print_version = 0;
+ b8 print_help = 0;
+
+ for (i32 i = 1; i < argc; ++i) {
+ if (argv[i][0] == '?' && argv[i][1] == '\0')
+ print_help = 1;
+ else if (argv[i][0] == '-') {
+ if (argv[i][1] == '-') {
+ i64 len = str_len(argv[i] + 2, argv[i] + 128);
+ if (str_eq(len, argv[i] + 2, 7, "version"))
+ print_version = 1;
+ else if (str_eq(len, argv[i] + 2, 4, "help"))
+ print_help = 1;
+ else {
+ LOG(ERROR, "Unknown argument %s", argv[i]);
+ is_ok = 0;
+ }
+ } else {
+ for (i32 j = 1; argv[i][j] != '\0'; ++j)
+ switch (argv[i][j]) {
+ case 'V': print_version = 1; break;
+ case 'h': print_help = 1; break;
+
+ default: {
+ LOG(ERROR, "Unknown option %c", argv[i][j]);
+ is_ok = 0;
+ }
+ }
+ }
+ } else {
+ LOG(ERROR, "Unknown argument %s", argv[i]);
+ is_ok = 0;
+ }
+ }
+
+ if (!is_ok)
+ return -1;
+
LOG(INFO, "bxgen " VERSION);
+ if (print_help) {
+ // TODO
+ LOG(ERROR, "Not implemented");
+ return 0;
+ }
+
+ if (print_version)
+ return 0;
+
i32 num_tests = 0;
i32 num_passed = 0;