Simple printf and restore assert stuff

This commit is contained in:
bumbread 2022-06-26 17:42:17 +11:00
parent 0f6a9ff988
commit e1a2205fc7
12 changed files with 147 additions and 25 deletions

View File

@ -52,6 +52,9 @@ long int strtol(const char *restrict nptr, char **restrict endptr, int base);
long long int strtoll(const char *restrict nptr, char **restrict endptr, int base); long long int strtoll(const char *restrict nptr, char **restrict endptr, int base);
unsigned long int strtoul(const char *restrict nptr, char **restrict endptr, int base); unsigned long int strtoul(const char *restrict nptr, char **restrict endptr, int base);
unsigned long long int strtoull(const char *restrict nptr, char **restrict endptr, int base); unsigned long long int strtoull(const char *restrict nptr, char **restrict endptr, int base);
char *itoa(int value, char *str, int base);
int rand(void); int rand(void);
void srand(unsigned int seed); void srand(unsigned int seed);
@ -63,6 +66,7 @@ void free(void *ptr);
void *malloc(size_t size); void *malloc(size_t size);
void *realloc(void *ptr, size_t size); void *realloc(void *ptr, size_t size);
_Noreturn void abort(void); _Noreturn void abort(void);
int atexit(void (*func)(void)); int atexit(void (*func)(void));
// int at_quick_exit(void (*func)(void)); // int at_quick_exit(void (*func)(void));

43
src/code/fmt/gen_fmt.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdarg.h>
#define ctype char
#define pfx(f) f
#include "gen_fmt.h"
#undef ctype
#undef pfx
#define ctype wchar_t
#define pfx(f) w ## f
#include "gen_fmt.h"
#undef ctype
#undef pfx
static int fprintfcb(void *ctx, char ch) {
FILE *f = ctx;
return fputc(ch, f) != EOF;
}
int vfprintf(FILE *stream, const char *fmt, va_list args) {
return vprintfcb(stream, fprintfcb, fmt, args);
}
int vprintf(const char *fmt, va_list args) {
return vprintfcb(stdout, fprintfcb, fmt, args);
}
int fprintf(FILE *stream, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int len = vfprintf(stream, fmt, args);
va_end(args);
return len;
}
int printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int len = vprintf(fmt, args);
va_end(args);
return len;
}

54
src/code/fmt/gen_fmt.h Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
typedef int (pfx(cbfn))(void *ctx, ctype ch);
static int pfx(vprintfcb)(
void *ctx,
cbfn *cb,
const ctype *fmt,
va_list args
) {
int w = 0;
while(*fmt) {
while(*fmt && *fmt != '%') {
if(!cb(ctx, *fmt))
goto end;
++w;
++fmt;
}
if(*fmt == '%') {
++fmt;
char f = *fmt++;
switch(f) {
case 's': {
char *str = va_arg(args, char *);
while(*str) {
if(!cb(ctx, *str))
goto end;
++str;
++w;
}
} break;
case 'd': case 'i': {
int num = va_arg(args, int);
char buffer[20];
char *str = buffer;
itoa(num, buffer, 10);
while(*str) {
if(!cb(ctx, *str))
goto end;
++str;
++w;
}
} break;
}
}
}
goto end;
print_str:
end:
return w;
}

View File

@ -4,7 +4,7 @@
#include <errno.h> #include <errno.h>
#include <stdint.h> #include <stdint.h>
#define ln2 0.69314718055994530941723212145817 static double LN2 = 0.693147180559945309417232121458176;
static double HALF_PI = 1.570796326794896619231321691639751; static double HALF_PI = 1.570796326794896619231321691639751;
static double PI = 3.141592653589793238462643383279502; static double PI = 3.141592653589793238462643383279502;
static double LOG2E = 1.442695040888963407359924681001892; static double LOG2E = 1.442695040888963407359924681001892;
@ -14,20 +14,20 @@ static double LOG2E = 1.442695040888963407359924681001892;
#define ftype float #define ftype float
#define suffix(name) name ## f #define suffix(name) name ## f
#include "cordic/cordic_dataf.c" #include "cordic/cordic_dataf.c"
#include "generic.h" #include "gen_math.h"
#undef ftype #undef ftype
#undef suffix #undef suffix
#define ftype double #define ftype double
#define suffix(name) name #define suffix(name) name
#include "cordic/cordic_data.c" #include "cordic/cordic_data.c"
#include "generic.h" #include "gen_math.h"
#undef ftype #undef ftype
#undef suffix #undef suffix
#define ftype long double #define ftype long double
#define suffix(name) name ## l #define suffix(name) name ## l
#include "cordic/cordic_datal.c" #include "cordic/cordic_datal.c"
#include "generic.h" #include "gen_math.h"
#undef ftype #undef ftype
#undef suffix #undef suffix

View File

@ -221,7 +221,7 @@ ftype suffix(asin)(ftype x) {
// } // }
ftype suffix(exp2)(ftype x) { ftype suffix(exp2)(ftype x) {
return suffix(exp)(x * ln2); return suffix(exp)(x * LN2);
} }
ftype suffix(expm1)(ftype x) { ftype suffix(expm1)(ftype x) {

View File

@ -5,9 +5,7 @@
#include <limits.h> #include <limits.h>
#include <errno.h> #include <errno.h>
#include <math.h> #include <math.h>
#include <string.h>
// TODO: strto*: locale-based parsing
// TODO: correctly rounded base 16 floats parsing
// Call me weak if you want but I'm actually too lazy to type // Call me weak if you want but I'm actually too lazy to type
// them out every time, also they take up a lot of horiz space. // them out every time, also they take up a lot of horiz space.
@ -339,3 +337,22 @@ float strtof(const char *restrict nptr, char **restrict endptr) {
long double strtold(const char *restrict nptr, char **restrict endptr) { long double strtold(const char *restrict nptr, char **restrict endptr) {
return (long double)strtod_generic(nptr, endptr); return (long double)strtod_generic(nptr, endptr);
} }
char *itoa(int value, char *str, int base) {
int sign = 0;
if(value < 0) {
sign = 1;
value = -value;
}
char buf[20] = {0};
char *bufp = buf + sizeof buf - 1;
do {
*--bufp = value%base+'0';
value /= base;
} while(value != 0);
if(sign) {
*--bufp = '-';
}
strcpy(str, bufp);
return str;
}

View File

@ -7,6 +7,7 @@
#include <shellapi.h> #include <shellapi.h>
#include <DbgHelp.h> #include <DbgHelp.h>
void _print_stack_trace();
void _setup_timer(void); void _setup_timer(void);
void _setup_eh(); void _setup_eh();
void _setup_heap(); void _setup_heap();

View File

@ -2,7 +2,7 @@
#include <assert.h> #include <assert.h>
#include <win.h> #include <win.h>
//#include <stdio.h> // printf #include <stdio.h> // printf
#include <stdlib.h> // abort #include <stdlib.h> // abort
void _Noreturn _assert( void _Noreturn _assert(
@ -11,11 +11,11 @@ void _Noreturn _assert(
char const *file, char const *file,
int line int line
) { ) {
// printf("Assertion failed: %s\n", cond); printf("Assertion failed: %s\n", cond);
// printf(" Function: %s\n", func); printf(" Function: %s\n", func);
// printf(" File: %s\n", file); printf(" File: %s\n", file);
// printf(" Line: %d\n", line); printf(" Line: %d\n", line);
// printf("Trace:\n"); printf("Trace:\n");
// _os_print_stack_trace(); _print_stack_trace();
abort(); abort();
} }

View File

@ -1,8 +1,9 @@
#include <win.h> #include <win.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
void _os_print_stack_trace() { void _print_stack_trace() {
HANDLE process = GetCurrentProcess(); HANDLE process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE); SymInitialize(process, NULL, TRUE);
@ -14,13 +15,14 @@ void _os_print_stack_trace() {
symbol->SizeOfStruct = sizeof(SYMBOL_INFO); symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
for(size_t i = 0; i < frames; i++) { for(size_t i = 0; i < frames; i++) {
SymFromAddr(process, (DWORD64)stack[i], 0, symbol); SymFromAddr(process, (DWORD64)stack[i], 0, symbol);
if(strcmp(symbol->Name, "BaseThreadInitThunk") == 0) break; // if(strcmp(symbol->Name, "BaseThreadInitThunk") == 0) break;
if(strcmp(symbol->Name, "mainCRTStartup") == 0) break; // if(strcmp(symbol->Name, "mainCRTStartup") == 0) break;
// printf(" %u: 0x%"PRIx64" (%s)\n", printf(//" %u: 0x%"PRIx64" (%s)\n",
// (int)(frames-i-1), " %d: %s\n",
// symbol->Address, (int)(frames-i-1),
// symbol->Name //symbol->Address,
// ); symbol->Name
);
} }
free(symbol); free(symbol);
} }

View File

@ -1,8 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main() { int main() {
for(int i = 0; i != 512; ++i) printf("Hello, %i World!\n", __LINE__);
fputc('Z', stdout);
return 0; return 0;
} }

1
todo
View File

@ -37,6 +37,7 @@ stdlib.h:
qsort qsort
Better PRNG Better PRNG
MB_CUR_MAX should be locale-dependent MB_CUR_MAX should be locale-dependent
strtod base 16 should be correctly rounded
Probably other stuff Probably other stuff
threads.h: threads.h: