changed error reporting function interface

This commit is contained in:
Francesco Cozzuto
2023-01-20 01:22:20 +01:00
parent 9cc7121cd9
commit 0980cc193d
29 changed files with 391 additions and 437 deletions
+69 -107
View File
@@ -8,41 +8,48 @@
#include "runtime/timing.h"
#include "noja.h"
static void print_error(const char *type, Error *error)
static void serializeProfilingResults(Runtime *runtime, const char *file)
{
if(type == NULL)
fprintf(stderr, "Error");
else if(error->internal)
fprintf(stderr, "Internal Error");
else
fprintf(stderr, "%s Error", type);
TimingTable *table = Runtime_GetTimingTable(runtime);
if (table == NULL)
return;
fprintf(stderr, ": %s.", error->message);
#ifdef DEBUG
if(error->file != NULL)
{
if(error->line > 0 && error->func != NULL)
fprintf(stderr, " (Reported in %s:%d in %s)", error->file, error->line, error->func);
else if(error->line > 0 && error->func == NULL)
fprintf(stderr, " (Reported in %s:%d)", error->file, error->line);
else if(error->line < 1 && error->func != NULL)
fprintf(stderr, " (Reported in %s in %s)", error->file, error->func);
FILE *stream = fopen(file, "wb");
if (stream == NULL) {
fprintf(stderr, "Failed to serialize profiling results\n");
return;
}
#endif
fprintf(stderr, "\n");
const FunctionExecutionSummary *summary;
size_t count;
summary = TimingTable_getSummary(table, &count);
assert(summary != NULL);
for (size_t i = 0; i < count; i++) {
if (summary[i].calls > 0) {
fprintf(stream, "%20s - %s - %ld calls - %.2lfus\n",
summary[i].name,
Source_GetName(summary[i].src),
summary[i].calls,
summary[i].time * 1000000);
}
}
fclose(stream);
fprintf(stderr, "Wrote profiling result to %s\n", file);
}
#include <signal.h>
Runtime *runt = NULL;
Runtime *runtime = NULL;
static void signalHandler(int signo)
{
(void) signo;
if (runt != NULL)
Runtime_Interrupt(runt);
if (runtime != NULL)
Runtime_Interrupt(runtime);
}
static _Bool interpret(Executable *exe, bool time, size_t heap)
@@ -50,13 +57,13 @@ static _Bool interpret(Executable *exe, bool time, size_t heap)
RuntimeConfig config = Runtime_GetDefaultConfigs();
config.time = time;
runt = Runtime_New(heap, config);
if(runt == NULL)
runtime = Runtime_New(heap, config);
if(runtime == NULL)
{
Error error;
Error_Init(&error);
Error_Report(&error, 1, "Couldn't initialize runtime");
print_error(NULL, &error);
Error_Report(&error, ErrorType_INTERNAL, "Couldn't initialize runtime");
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}
@@ -70,55 +77,47 @@ static _Bool interpret(Executable *exe, bool time, size_t heap)
// expects a pointer to [Error] can receive a [RuntimeError]
// upcasted to [Error].
RuntimeError error;
RuntimeError_Init(&error, runt); // Here we specify the runtime to snapshot in case of failure.
RuntimeError_Init(&error, runtime); // Here we specify the runtime to snapshot in case of failure.
{
Object *native_bins = Object_NewStaticMap(bins_basic, bins_basic_init, runt, (Error*) &error);
Object *native_bins = Object_NewStaticMap(bins_basic, bins_basic_init, runtime, (Error*) &error);
if(native_bins == NULL)
{
assert(error.base.occurred == 1);
print_error(NULL, (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
RuntimeError_Free(&error);
Runtime_Free(runt);
Runtime_Free(runtime);
return 0;
}
// Just to execute the prelude
Runtime_SetBuiltins(runt, native_bins);
Runtime_SetBuiltins(runtime, native_bins);
extern char start_noja[];
Source *prelude = Source_FromString("<prelude>", start_noja, -1, (Error*) &error);
if (prelude == NULL) {
assert(error.base.occurred == 1);
print_error(NULL, (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
RuntimeError_Free(&error);
Runtime_Free(runt);
Runtime_Free(runtime);
return 0;
}
CompilationErrorType errtyp;
Executable *prelude_exe = compile(prelude, (Error*) &error, &errtyp);
Executable *prelude_exe = compile(prelude, (Error*) &error);
if(prelude_exe == NULL) {
const char *errname;
switch(errtyp) {
default:
case CompilationErrorType_INTERNAL: errname = NULL; break;
case CompilationErrorType_SYNTAX: errname = "Syntax"; break;
case CompilationErrorType_SEMANTIC: errname = "Semantic"; break;
}
print_error(errname, (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
RuntimeError_Free(&error);
Runtime_Free(runt);
Runtime_Free(runtime);
Source_Free(prelude);
return 0;
}
Object *rets[8];
int retc = run(runt, (Error*) &error, prelude_exe, 0, NULL, NULL, 0, rets);
int retc = run(runtime, (Error*) &error, prelude_exe, 0, NULL, NULL, 0, rets);
if(retc < 0) {
print_error("Runtime", (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
RuntimeError_Free(&error);
Runtime_Free(runt);
Runtime_Free(runtime);
Source_Free(prelude);
Executable_Free(prelude_exe);
return 0;
@@ -128,43 +127,43 @@ static _Bool interpret(Executable *exe, bool time, size_t heap)
// Need to remake the native built-ins because
// running the script invalidated the previous
// pointer.
native_bins = Object_NewStaticMap(bins_basic, bins_basic_init, runt, (Error*) &error);
native_bins = Object_NewStaticMap(bins_basic, bins_basic_init, runtime, (Error*) &error);
if(native_bins == NULL)
{
assert(error.base.occurred == 1);
print_error(NULL, (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
RuntimeError_Free(&error);
Runtime_Free(runt);
Runtime_Free(runtime);
Source_Free(prelude);
Executable_Free(prelude_exe);
return 0;
}
Object *all_bins = Object_NewClosure(native_bins, noja_bins, Runtime_GetHeap(runt), (Error*) &error);
Object *all_bins = Object_NewClosure(native_bins, noja_bins, Runtime_GetHeap(runtime), (Error*) &error);
if (all_bins == NULL) {
print_error(NULL, (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
RuntimeError_Free(&error);
Runtime_Free(runt);
Runtime_Free(runtime);
Source_Free(prelude);
Executable_Free(prelude_exe);
return 0;
}
Runtime_SetBuiltins(runt, all_bins);
Runtime_SetBuiltins(runtime, all_bins);
Source_Free(prelude);
Executable_Free(prelude_exe);
}
Object *rets[8];
int retc = run(runt, (Error*) &error, exe, 0, NULL, NULL, 0, rets);
int retc = run(runtime, (Error*) &error, exe, 0, NULL, NULL, 0, rets);
// NOTE: The pointer to the builtins object is invalidated
// now because it may be moved by the garbage collector.
if(retc < 0)
{
print_error("Runtime", (Error*) &error);
Error_Print((Error*) &error, ErrorType_RUNTIME);
if(error.snapshot == NULL)
fprintf(stderr, "No snapshot available.\n");
@@ -174,38 +173,9 @@ static _Bool interpret(Executable *exe, bool time, size_t heap)
RuntimeError_Free(&error);
}
TimingTable *table = Runtime_GetTimingTable(runt);
if (table != NULL) {
serializeProfilingResults(runtime, "profiling-results.txt");
const char *file = "profiling-results.txt";
FILE *stream = fopen(file, "wb");
if (stream == NULL) {
fprintf(stderr, "Failed to serialize profiling results\n");
} else {
const FunctionExecutionSummary *summary;
size_t count;
summary = TimingTable_getSummary(table, &count);
assert(summary != NULL);
for (size_t i = 0; i < count; i++) {
if (summary[i].calls > 0) {
fprintf(stream, "%20s - %s - %ld calls - %.2lfus\n",
summary[i].name,
Source_GetName(summary[i].src),
summary[i].calls,
summary[i].time * 1000000);
}
}
fclose(stream);
fprintf(stderr, "Wrote profiling result to %s\n", file);
}
}
Runtime_Free(runt);
Runtime_Free(runtime);
return retc > -1;
}
@@ -213,17 +183,9 @@ static Executable *compile_source_and_print_error_on_failure(Source *src)
{
Error error;
Error_Init(&error);
CompilationErrorType errtyp;
Executable *exe = compile(src, &error, &errtyp);
Executable *exe = compile(src, &error);
if(exe == NULL) {
const char *errname;
switch(errtyp) {
default:
case CompilationErrorType_INTERNAL: errname = NULL; break;
case CompilationErrorType_SYNTAX: errname = "Syntax"; break;
case CompilationErrorType_SEMANTIC: errname = "Semantic"; break;
}
print_error(errname, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return NULL;
}
@@ -249,7 +211,7 @@ static _Bool interpret_file(const char *file, bool time, size_t heap)
if(src == NULL)
{
assert(error.occurred == 1);
print_error(NULL, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}
@@ -277,7 +239,7 @@ static _Bool interpret_code(const char *code, bool time, size_t heap)
if(src == NULL)
{
assert(error.occurred);
print_error(NULL, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}
@@ -305,14 +267,14 @@ static _Bool interpret_asm_file(const char *file, bool time, size_t heap)
if(src == NULL)
{
assert(error.occurred == 1);
print_error(NULL, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}
Executable *exe = assemble(src, &error);
if(exe == NULL) {
print_error("Assemblation", &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Source_Free(src);
Error_Free(&error);
return 0;
@@ -336,14 +298,14 @@ static _Bool interpret_asm_code(const char *code, bool time, size_t heap)
if(src == NULL)
{
assert(error.occurred);
print_error(NULL, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}
Executable *exe = assemble(src, &error);
if(exe == NULL) {
print_error("Assemblation", &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Source_Free(src);
Error_Free(&error);
return 0;
@@ -367,7 +329,7 @@ static _Bool disassemble_file(const char *file)
if(src == NULL)
{
assert(error.occurred == 1);
print_error(NULL, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}
@@ -388,7 +350,7 @@ static _Bool disassemble_code(const char *code)
if(src == NULL)
{
assert(error.occurred);
print_error(NULL, &error);
Error_Print(&error, ErrorType_UNSPECIFIED);
Error_Free(&error);
return 0;
}