~swisschili/toolchain-6502

toolchain-6502/common.c -rw-r--r-- 777 bytes
a091d887swissChili Readme change to reflect status of assembler 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "common.h"

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

catch_t g_catch[MAX_CATCH_LEN];
unsigned g_catch_len;

void unwind()
{
	for (int i = g_catch_len - 1; i >= 0; i--)
	{
		g_catch[i].fn(g_catch[i].arg);
	}
}

void throw_(const char *msg, const char *file, unsigned int line)
{
	fprintf(stderr, "\033[31mException thrown:\033[33m %s:%d\033[0m %s\n", file, line, msg);
	unwind();
	exit(1);
}

void catch_(handle_t hdl, intptr_t arg)
{
	if (g_catch_len > MAX_CATCH_LEN)
	{
		THROW("Catch overflow");
	}

	g_catch[g_catch_len++] = (catch_t){ .fn = hdl, .arg = arg };
}

void catch_signal(int sig)
{
	if (sig == SIGSEGV)
		throw_("Segmentation fault", "unknown", 0);
}

__attribute__((constructor)) void init_catch()
{
	signal(SIGSEGV, catch_signal);
}