2024-01-22 16:44:49 +08:00
|
|
|
#include <inttypes.h>
|
2023-10-09 16:02:15 +08:00
|
|
|
#include <math.h>
|
2023-10-03 17:24:02 +08:00
|
|
|
#include <stdbool.h>
|
2023-09-29 15:15:18 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-10-10 16:56:38 +08:00
|
|
|
double dbl_nan(void) {
|
2024-08-27 23:06:49 +08:00
|
|
|
return NAN;
|
2023-10-10 16:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double dbl_inf(void) {
|
2024-08-27 23:06:49 +08:00
|
|
|
return INFINITY;
|
2023-10-10 16:56:38 +08:00
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_bool(bool x) {
|
2023-10-30 17:22:32 +08:00
|
|
|
puts(x ? "True" : "False");
|
2023-10-03 17:24:02 +08:00
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_int32(int32_t x) {
|
2024-08-27 23:06:49 +08:00
|
|
|
printf("%"PRId32"\n", x);
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_int64(int64_t x) {
|
2024-08-27 23:06:49 +08:00
|
|
|
printf("%"PRId64"\n", x);
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_uint32(uint32_t x) {
|
2024-08-27 23:06:49 +08:00
|
|
|
printf("%"PRIu32"\n", x);
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_uint64(uint64_t x) {
|
2024-08-27 23:06:49 +08:00
|
|
|
printf("%"PRIu64"\n", x);
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_float64(double x) {
|
2023-10-09 16:02:15 +08:00
|
|
|
if (isnan(x)) {
|
|
|
|
puts("nan");
|
|
|
|
} else {
|
|
|
|
printf("%f\n", x);
|
|
|
|
}
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
2023-10-06 10:32:58 +08:00
|
|
|
|
2024-07-08 14:40:22 +08:00
|
|
|
void output_range(int32_t range[3]) {
|
|
|
|
printf("range(");
|
2024-07-09 13:55:48 +08:00
|
|
|
printf("%d, %d", range[0], range[1]);
|
2024-07-08 14:40:22 +08:00
|
|
|
if (range[2] != 1) {
|
|
|
|
printf(", %d", range[2]);
|
|
|
|
}
|
|
|
|
puts(")");
|
|
|
|
}
|
|
|
|
|
2023-10-06 10:32:58 +08:00
|
|
|
void output_asciiart(int32_t x) {
|
2024-08-27 23:06:49 +08:00
|
|
|
static const char *chars = " .,-:;i+hHM$*#@ ";
|
2023-10-30 17:22:32 +08:00
|
|
|
if (x < 0) {
|
|
|
|
putchar('\n');
|
|
|
|
} else {
|
|
|
|
putchar(chars[x]);
|
|
|
|
}
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct cslice {
|
2024-08-27 23:06:49 +08:00
|
|
|
void *data;
|
2024-07-29 13:18:15 +08:00
|
|
|
size_t len;
|
2023-09-29 15:15:18 +08:00
|
|
|
};
|
|
|
|
|
2024-08-27 23:06:49 +08:00
|
|
|
void output_int32_list(struct cslice *slice) {
|
|
|
|
const int32_t *data = (int32_t *) slice->data;
|
2023-10-30 17:22:32 +08:00
|
|
|
|
|
|
|
putchar('[');
|
2024-07-29 13:18:15 +08:00
|
|
|
for (size_t i = 0; i < slice->len; ++i) {
|
2023-10-30 17:22:32 +08:00
|
|
|
if (i == slice->len - 1) {
|
|
|
|
printf("%d", data[i]);
|
|
|
|
} else {
|
|
|
|
printf("%d, ", data[i]);
|
|
|
|
}
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
2023-10-30 17:22:32 +08:00
|
|
|
putchar(']');
|
|
|
|
putchar('\n');
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 23:06:49 +08:00
|
|
|
void output_str(struct cslice *slice) {
|
|
|
|
const char *data = (const char *) slice->data;
|
2023-09-29 15:15:18 +08:00
|
|
|
|
2024-07-29 13:18:15 +08:00
|
|
|
for (size_t i = 0; i < slice->len; ++i) {
|
2023-10-30 17:22:32 +08:00
|
|
|
putchar(data[i]);
|
|
|
|
}
|
2024-07-08 12:53:51 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 23:06:49 +08:00
|
|
|
void output_strln(struct cslice *slice) {
|
2024-07-08 12:53:51 +08:00
|
|
|
output_str(slice);
|
2023-10-30 17:22:32 +08:00
|
|
|
putchar('\n');
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2024-08-27 23:06:49 +08:00
|
|
|
uint64_t dbg_stack_address(__attribute__((unused)) struct cslice *slice) {
|
2023-10-30 17:22:32 +08:00
|
|
|
int i;
|
2024-08-27 23:06:49 +08:00
|
|
|
void *ptr = (void *) &i;
|
|
|
|
return (uintptr_t) ptr;
|
2023-10-03 18:02:45 +08:00
|
|
|
}
|
|
|
|
|
2023-09-29 15:15:18 +08:00
|
|
|
uint32_t __nac3_personality(uint32_t state, uint32_t exception_object, uint32_t context) {
|
2023-11-22 16:45:58 +08:00
|
|
|
printf("__nac3_personality(state: %u, exception_object: %u, context: %u)\n", state, exception_object, context);
|
2023-10-30 17:22:32 +08:00
|
|
|
exit(101);
|
|
|
|
__builtin_unreachable();
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
2024-07-17 21:22:08 +08:00
|
|
|
// See `struct Exception<'a>` in
|
|
|
|
// https://github.com/m-labs/artiq/blob/master/artiq/firmware/libeh/eh_artiq.rs
|
|
|
|
struct Exception {
|
|
|
|
uint32_t id;
|
|
|
|
struct cslice file;
|
|
|
|
uint32_t line;
|
|
|
|
uint32_t column;
|
|
|
|
struct cslice function;
|
|
|
|
struct cslice message;
|
|
|
|
int64_t param[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t __nac3_raise(struct Exception* e) {
|
|
|
|
printf("__nac3_raise called. Exception details:\n");
|
2024-08-27 23:06:49 +08:00
|
|
|
printf(" ID: %"PRIu32"\n", e->id);
|
|
|
|
printf(" Location: %*s:%"PRIu32":%"PRIu32"\n" , (int) e->file.len, (const char*) e->file.data, e->line, e->column);
|
|
|
|
printf(" Function: %*s\n" , (int) e->function.len, (const char*) e->function.data);
|
|
|
|
printf(" Message: \"%*s\"\n" , (int) e->message.len, (const char*) e->message.data);
|
|
|
|
printf(" Params: {0}=%"PRId64", {1}=%"PRId64", {2}=%"PRId64"\n", e->param[0], e->param[1], e->param[2]);
|
2023-10-30 17:22:32 +08:00
|
|
|
exit(101);
|
|
|
|
__builtin_unreachable();
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __nac3_end_catch(void) {}
|
|
|
|
|
|
|
|
extern int32_t run(void);
|
|
|
|
|
|
|
|
int main(void) {
|
2023-10-30 17:22:32 +08:00
|
|
|
run();
|
2023-09-29 15:15:18 +08:00
|
|
|
}
|