many things, cosmo build!

This commit is contained in:
veclavtlica
2026-10-12 21:12:47 +03:00
parent ba4131abbf
commit 8d2bb5575e
29 changed files with 699 additions and 312 deletions
+138
View File
@@ -0,0 +1,138 @@
/*
Generic std parts, building on top of platform specifics.
*/
#include "../fix.h"
#include "../std.h"
#include "../str.h"
#include <stdarg.h>
#include <stdint.h>
static uint8_t com_std_any_format_u32(uint32_t v, char buf[10]) {
uint8_t c = 0;
if (v == 0) {
buf[10 - (++c)] = '0';
} else {
while (v > 0) {
buf[10 - (++c)] = '0' + (v % 10);
v /= 10;
}
}
return c;
}
static uint8_t com_std_any_format_i32(int32_t v, char buf[11]) {
bool neg = v < 0;
uint8_t c = 0;
if (v == 0) {
buf[11 - (++c)] = '0';
} else {
while (v > 0) {
buf[11 - (++c)] = '0' + (v % 10);
v /= 10;
}
}
if (neg)
buf[11 - (++c)] = '-';
return c;
}
/* Just assume 5 points of precision. */
static uint8_t com_std_any_format_f64(double v, char buf[11 + 1 + 5]) {
bool neg = v < 0;
uint8_t c = 0;
double frac_part = v - (double)(int32_t)v;
for (int i = 0; i < 5; i++) {
frac_part *= 10.0;
uint8_t digit = (unsigned int)frac_part;
buf[17 - (++c)] = digit + '0';
frac_part -= digit;
}
buf[17 - (++c)] = '.';
c += com_std_any_format_i32((int32_t)v, &buf[c]);
return c;
}
/* We can make it buffered, but who cares, it's only for testing. */
void com_std_print_(int32_t start, ...) {
va_list args;
va_start(args, start);
bool afterstring = false;
char type = start;
do {
/* Put space if not proceeding string. */
if (!afterstring && type != 'c')
com_std_write(COM_STD_OUTFD, " ", 1);
afterstring = false;
switch (type) {
/* uint32_t case. */
case 'u': {
uint32_t const v = va_arg(args, uint32_t);
char buf[10];
uint8_t c = com_std_any_format_u32(v, buf);
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
break;
}
/* null terminated string case. */
case 'c': {
char const *const v = va_arg(args, char *);
com_std_write(COM_STD_OUTFD, v, com_str_ext_len(v));
afterstring = true;
break;
}
/* TODO: FIX THIS */
/* com_fix case. */
case 'f': {
com_fix_t const v = va_arg(args, com_fix_t);
char buf[11];
int32_t const int_part = com_fix_int(v);
uint8_t c = com_std_any_format_i32(int_part, buf);
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
uint32_t const decimal_val = (com_fix_frac(v) * 10000) >> 16;
uint8_t cd = com_std_any_format_u32(decimal_val, buf);
buf[sizeof buf - (++cd)] = '.';
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - cd], cd);
break;
}
/* double floating point case. */
case 'd': {
double const v = va_arg(args, double);
char buf[17];
uint8_t c = com_std_any_format_f64(v, buf);
com_std_write(COM_STD_OUTFD, &buf[sizeof buf - c], c);
}
/* End of things. */
case '.':
break;
default:
com_std_assert(false && "Unhandled format type");
}
type = va_arg(args, int32_t);
} while (type != '.');
com_std_write(COM_STD_OUTFD, "\n", 1);
va_end(args);
}
void com_std_print_str(char const *str, uint32_t len, bool newline) {
com_std_write(COM_STD_OUTFD, str, len);
if (newline)
com_std_write(COM_STD_OUTFD, "\n", 1);
}
+13 -8
View File
@@ -1,7 +1,9 @@
#include "../std.h"
#include "../str.h"
#include "any.c"
#include <assert.h>
#include <poll.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
@@ -11,13 +13,7 @@
void *com_std_alloc(void *ptr, size_t size) {
return size != 0 ? realloc(ptr, size) : (free(ptr), NULL);
}
void com_std_assert(bool holds) { assert(holds); }
void com_std_printf(char const *fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
double com_std_sqrt(double v) { return sqrt(v); }
double com_std_fabs(double v) { return fabs(v); }
double com_std_sin(double v) { return sin(v); }
@@ -43,6 +39,15 @@ int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
return fread(data, 1, upto, fd);
}
bool com_std_has_data(com_std_fd fd) {
struct pollfd pfd = {.fd = (intptr_t)fd, .events = POLLIN};
int ret = poll(&pfd, 1, 0);
if (ret > 0) {
return pfd.revents & POLLIN;
} else
return false;
}
bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
com_std_assert(fd != NULL && bytes != 0);
return fseek(fd, bytes, SEEK_CUR) == 0;
+18
View File
@@ -6,6 +6,7 @@
#include "../std.h"
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -44,6 +45,23 @@ inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
return ret;
}
inline bool com_std_has_data(com_std_fd fd) {
struct pollfd pfd = {
.fd = (intptr_t)fd,
.events = POLLIN,
};
long ret;
__asm__ volatile("syscall"
: "=a"(ret)
: "a"(7), "D"(&pfd), "S"(1), "d"(0)
: "rcx", "r11", "memory");
if (ret > 0) {
return pfd.revents & POLLIN;
} else
return false;
}
inline bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
long ret;
__asm__ volatile("syscall"
+18
View File
@@ -6,6 +6,7 @@
#include "../std.h"
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -44,6 +45,23 @@ inline int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
return ret;
}
inline bool com_std_has_data(com_std_fd fd) {
struct pollfd pfd = {
.fd = (intptr_t)fd,
.events = POLLIN,
};
long ret;
__asm__ volatile("int $0x80"
: "=a"(ret)
: "a"(168), "b"(&pfd), "c"(1), "d"(0)
: "memory", "cc");
if (ret > 0) {
return pfd.revents & POLLIN;
} else
return false;
}
inline bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
long ret;
__asm__ volatile("int $0x80"
+3 -31
View File
@@ -2,41 +2,13 @@
Generic linux std parts.
*/
#include "../def.h"
#include "any.c"
#include "nolibc.c"
#include "../std.h"
#include "../str.h"
#include <stdarg.h>
#include <stdint.h>
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
void com_std_assert(bool holds) {
/* TODO: Write line info to stderr as well. */
if (!holds)
com_std_exit(-1);
}
/* TODO: Figure out a way to make it easy to deal with. */
void com_std_printf(char const *fmt, ...) {
com_std_write((void *)1, fmt, com_str_ext_len(fmt));
}
void com_std_print_str(char const *str, uint32_t len, bool newline) {
com_std_write((void *)1, str, len);
if (newline)
com_std_write((void *)1, "\n", 1);
}
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
double com_std_fabs(double v) { return __builtin_fabs(v); }
#if COM_DEF_COMPILER == COM_DEF_COMPILER_CLANG
double com_std_sin(double v) { return __builtin_sin(v); }
double com_std_cos(double v) { return __builtin_cos(v); }
#else
/* TODO: Use fsin/fcos, no matter how slow. */
double com_std_sin(double v) { return 0; }
double com_std_cos(double v) { return 0; }
#endif
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
// __builtin_memcpy(dest, src, n);
// };
+46
View File
@@ -0,0 +1,46 @@
/*
Generic no libc std parts.
*/
#include <stddef.h>
/* Compiler depends on them with low optimization level. */
void *memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
for (size_t i = 0; i < n; ++i)
((char *)dest)[i] = ((char *)src)[i];
return dest;
};
void *memset(void *__restrict dest, int s, size_t n) {
for (size_t i = 0; i < n; ++i)
((char *)dest)[i] = s;
return dest;
};
size_t strlen(char const *s) {
char const *p = s;
for (; *p != 0; ++p) {
}
return p - s;
}
double com_std_sin(double v) {
double result;
__asm__ __volatile__("fldl %1\n\t"
"fsin\n\t"
"fstpl %0"
: "=g"(result)
: "m"(v)
: "st", "st(1)");
return result;
}
double com_std_cos(double v) {
double result;
__asm__ __volatile__("fldl %1\n\t"
"fcos\n\t"
"fstpl %0"
: "=g"(result)
: "m"(v));
return result;
}
+2 -6
View File
@@ -1,11 +1,11 @@
#include "../std.h"
#include "any.c"
#include <stdbool.h>
#include <stddef.h>
void *com_std_alloc(void *ptr, size_t size) { return NULL; }
void com_std_assert(bool holds) {}
void com_std_printf(char const *fmt, ...) {}
double com_std_sqrt(double v) { return __builtin_sqrt(v); }
double com_std_fabs(double v) { return __builtin_fabs(v); }
@@ -35,7 +35,3 @@ void com_std_exit(uint32_t status) {
__builtin_unreachable();
}
void com_std_close(com_std_fd fd) { return; }
// void memcpy(void *__restrict dest, const void *__restrict src, size_t n) {
// __builtin_memcpy(dest, src, n);
// };