-
Notifications
You must be signed in to change notification settings - Fork 1
/
FACT_error.c
113 lines (93 loc) · 3.03 KB
/
FACT_error.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This file is part of FACT.
*
* FACT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FACT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FACT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FACT.h"
#include "FACT_types.h"
#include "FACT_alloc.h"
#include "FACT_vm.h"
#include "FACT_error.h"
#include <stdarg.h>
/* Structure to map instruction address to files and line numbers. */
static size_t num_maps = 0;
struct map {
size_t line;
const char *file_name;
} *addr_map = NULL;
const struct map NO_MAP = {
.line = 0, /* Line cannot be 0 in any other context. */
.file_name = NULL /* Same goes for file_name. */
};
int FACT_add_line (const char *file, size_t line, size_t addr) /* Map an address to line number and file name. */
{
size_t i;
/* If the addr was already mapped, re-map it. */
if (addr < num_maps) {
addr_map[addr].line = line;
addr_map[addr].file_name = file;
return -1; /* return -1 on re-map. */
}
/* Allocate or Reallocate the addr_map. */
if (num_maps == 0) {
addr_map = FACT_malloc (sizeof (struct map) * (addr + 1));
num_maps = 1;
} else
addr_map = FACT_realloc (addr_map, sizeof (struct map) * (addr + 1));
/* Set the new mapping. */
addr_map[addr].line = line;
addr_map[addr].file_name = file;
/* Set all the maps in no man's land to NO_MAP. */
for (i = num_maps; i < addr; i++)
addr_map[i] = NO_MAP;
num_maps = addr + 1;
return 0; /* Return 0 on success. */
}
size_t FACT_get_line (size_t addr) /* Get a line number from an address. */
{
if (addr >= num_maps)
return 0; /* Return 0 on error. */
while (addr_map[addr].line == 0 && addr > 0)
addr--;
return addr_map[addr].line;
}
const char *FACT_get_file (size_t addr) /* Get a file name from an address. */
{
if (addr >= num_maps)
return NULL; /* Return NULL on error. */
while (addr_map[addr].line == 0 && addr < num_maps)
addr++;
return (addr >= num_maps
? NULL
: addr_map[addr].file_name);
}
void FACT_throw_error (FACT_scope_t scope, const char *fmt, ...)
/* Set curr_err and long jump back to the error handler. */
{
char *buff;
va_list args;
FACT_error_t err;
/* Allocate the buffer. Make sure to free it later. */
buff = FACT_malloc_atomic (MAX_ERR_LEN + 1);
/* Get the formatted string. */
va_start (args, fmt);
vsnprintf (buff, MAX_ERR_LEN, fmt, args);
va_end (args);
err.what = buff;
curr_thread->curr_err = err; /* Set the error. */
longjmp (handle_err, 1); /* Jump back. */
}
void FACT_print_error (FACT_error_t err) /* Print out an error to stderr. */
{
/* ... */
}