forked from M-Labs/nac3
1
0
Fork 0
nac3/nac3standalone/demo/demo.c

152 lines
3.1 KiB
C

#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define usize size_t
double dbl_nan(void) {
return NAN;
}
double dbl_inf(void) {
return INFINITY;
}
void output_bool(bool x) {
puts(x ? "True" : "False");
}
void output_int32(int32_t x) {
printf("%"PRId32"\n", x);
}
void output_int64(int64_t x) {
printf("%"PRId64"\n", x);
}
void output_uint32(uint32_t x) {
printf("%"PRIu32"\n", x);
}
void output_uint64(uint64_t x) {
printf("%"PRIu64"\n", x);
}
void output_float64(double x) {
if (isnan(x)) {
puts("nan");
} else {
printf("%f\n", x);
}
}
void output_range(int32_t range[3]) {
printf("range(");
if (range[0] != 0) {
printf("%d, ", range[0]);
}
printf("%d", range[1]);
if (range[2] != 1) {
printf(", %d", range[2]);
}
puts(")");
}
void output_asciiart(int32_t x) {
static const char *chars = " .,-:;i+hHM$*#@ ";
if (x < 0) {
putchar('\n');
} else {
putchar(chars[x]);
}
}
struct cslice {
void *data;
usize len;
};
void output_int32_list(const struct cslice *slice) {
const int32_t *data = (int32_t *) slice->data;
putchar('[');
for (usize i = 0; i < slice->len; ++i) {
if (i == slice->len - 1) {
printf("%d", data[i]);
} else {
printf("%d, ", data[i]);
}
}
putchar(']');
putchar('\n');
}
void output_str(const struct cslice *slice) {
const char *data = (const char *) slice->data;
for (usize i = 0; i < slice->len; ++i) {
putchar(data[i]);
}
}
void output_strln(const struct cslice *slice) {
output_str(slice);
putchar('\n');
}
uint64_t dbg_stack_address(__attribute__((unused)) const struct cslice *slice) {
int i;
void *ptr = (void *) &i;
return (uintptr_t) ptr;
}
uint32_t __nac3_personality(uint32_t state, uint32_t exception_object, uint32_t context) {
printf("__nac3_personality(state: %u, exception_object: %u, context: %u)\n", state, exception_object, context);
exit(101);
__builtin_unreachable();
}
struct exception {
uint32_t name;
struct cslice file;
uint32_t line;
uint32_t col;
struct cslice func;
struct cslice message;
uint64_t param0;
uint64_t param1;
uint64_t param2;
};
void output_exception(const struct exception *ex) {
fputs("exception { location: ", stdout);
output_str(&ex->file);
printf(":%u:%u, func: ", ex->line, ex->col);
output_str(&ex->func);
fputs(", message: ", stdout);
output_str(&ex->message);
printf(", params: [%lu, %lu, %lu] }\n", ex->param0, ex->param1, ex->param2);
}
uint32_t __nac3_raise(uint32_t state, uint32_t exception_object, uint32_t context) {
printf("__nac3_raise(state: %#10x, exception_object: %#10x, context: %#10x)\n", state, exception_object, context);
struct exception *ex = (struct exception *)((0x7ffffffful << 16) | state);
output_exception(ex);
exit(101);
__builtin_unreachable();
}
void __nac3_end_catch(void) {}
extern int32_t run(void);
int main(void) {
run();
}