diff options
-rwxr-xr-x | bxgen.c | 61 |
1 files changed, 55 insertions, 6 deletions
@@ -3,8 +3,8 @@ #/ #/ bxgen.c #/ -#/ Binary executable code generation and linking. -#/ Compiler backend. +#/ Binary executable code generation and linking. +#/ Compiler backend. #/ #/ ---------------------------------------------------------------- #/ @@ -62,6 +62,10 @@ #/ - ELF header #/ - IO static dispatch #/ +#/ ---------------------------------------------------------------- +#/ +#/ (C) 2024 Mitya Selivanov <guattari.tech>, MIT License +#/ #/ ================================================================ #/ #/ Self-compilation shell script @@ -970,11 +974,29 @@ i64 read_i64(i32 endianness, void *v, void *v_end) { } f32 read_f32(i32 endianness, void *v, void *v_end) { + if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) != 0x40301fcbull && + (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) != 0x40301fcb00000000ull) + BX_ASSERT(0 && "Unknown native floating-point number format"); + return *(f32 *) &(u32) { read_u32(endianness, v, v_end) }; } -f32 read_f64(i32 endianness, void *v, void *v_end) { - return *(f64 *) &(u64) { read_u64(endianness, v, v_end) }; +f32 read_f64(i32 endianness, i32 f64_word_order, void *v, void *v_end) { + b8 native_word_order_same; + + if ( (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull) + native_word_order_same = 1; + else if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) + native_word_order_same = 0; + else + BX_ASSERT(0 && "Unknown native floating-point number format"); + + u64 x = read_u64(endianness, v, v_end); + + if (native_word_order_same != (endianness == f64_word_order)) + x = ((x & 0xffffffffull) << 32) | ((x >> 32) & 0xffffffffull); + + return *(f64 *) &x; } void write_i16(i32 endianness, i16 x, void *v, void *v_end) { @@ -990,11 +1012,29 @@ void write_i64(i32 endianness, i64 x, void *v, void *v_end) { } void write_f32(i32 endianness, f32 x, void *v, void *v_end) { + if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) != 0x40301fcbull && + (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) != 0x40301fcb00000000ull) + BX_ASSERT(0 && "Unknown native floating-point number format"); + write_u32(endianness, *(u32 *) &x, v, v_end); } -void write_f64(i32 endianness, f64 x, void *v, void *v_end) { - write_u64(endianness, *(u64 *) &x, v, v_end); +void write_f64(i32 endianness, i32 f64_word_order, f64 x, void *v, void *v_end) { + b8 native_word_order_same; + + if ( (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull) + native_word_order_same = 1; + else if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) + native_word_order_same = 0; + else + BX_ASSERT(0 && "Unknown native floating-point number format"); + + if (native_word_order_same == (endianness == f64_word_order)) + write_u64(endianness, *(u64 *) &x, v, v_end); + else { + write_u32(endianness, *(((u32 *) &x) + 1), (u8 *) v, v_end); + write_u32(endianness, * (u32 *) &x, ((u8 *) v) + 4, v_end); + } } // ================================================================ @@ -1022,6 +1062,15 @@ void write_f64(i32 endianness, f64 x, void *v, void *v_end) { // https://defuse.ca/online-x86-assembler.htm // https://shell-storm.org/online/Online-Assembler-and-Disassembler/ // +// Linux syscall +// https://man7.org/linux/man-pages/man2/intro.2.html +// https://man7.org/linux/man-pages/man2/syscalls.2.html +// https://man7.org/linux/man-pages/man2/syscall.2.html +// +// ---------------------------------------------------------------- +// +// TODO Experiment with mapping several p_vaddr into one p_paddr. +// // ================================================================ #include <stdio.h> // TEMP |