-
Notifications
You must be signed in to change notification settings - Fork 1
/
FACT_BIFs.c
230 lines (188 loc) · 5.05 KB
/
FACT_BIFs.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* 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_mpc.h"
#include "FACT_types.h"
#include "FACT_vm.h"
#include "FACT_error.h"
#include "FACT_alloc.h"
#include "FACT_hash.h"
#include "FACT_threads.h"
#include "FACT_strs.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* Macros for declaring FACT BIFs. */
#define FBIF(name) { #name, &FBIF_##name }
#define FBIF_DEC(name) static void FBIF_##name (void)
static void *get_arg (FACT_type);
FBIF_DEC (floor);
FBIF_DEC (print);
FBIF_DEC (str);
FBIF_DEC (size);
FBIF_DEC (error);
FBIF_DEC (throw);
FBIF_DEC (send);
FBIF_DEC (receive);
FBIF_DEC (parcels);
FBIF_DEC (exit);
FBIF_DEC (load);
FBIF_DEC (ID);
static const struct {
char *name;
void (*phys)(void);
} BIF_list[] = {
FBIF (floor),
FBIF (print),
FBIF (str),
FBIF (size),
FBIF (error),
FBIF (throw),
FBIF (send),
FBIF (receive),
FBIF (ID),
FBIF (exit),
FBIF (load),
};
#define NUM_FBIF ((sizeof BIF_list) / (sizeof BIF_list[0]))
#define GET_ARG_NUM() ((FACT_num_t) get_arg (NUM_TYPE))
#define GET_ARG_SCOPE() ((FACT_scope_t) get_arg (SCOPE_TYPE))
void FACT_add_BIFs (void) /* Add the built-in functions to the global table. */
{
int i;
FACT_t tvar;
FACT_scope_t temp;
tvar.type = SCOPE_TYPE;
/* Add each of the functions. */
for (i = 0; i < NUM_FBIF; i++) {
temp = FACT_alloc_scope ();
temp->name = BIF_list[i].name;
temp->lock_stat = HARD_LOCK;
temp->extrn_func = BIF_list[i].phys;
tvar.ap = temp;
FACT_add_to_table (&Furlow_globals, tvar);
}
/* Well that was pretty easy. */
}
bool FACT_is_BIF (void *func_addr)
{
int i;
for (i = 0; i < NUM_FBIF; i++) {
if (BIF_list[i].phys == func_addr)
return true;
}
return false;
}
static void FBIF_floor (void) /* Round a variable down. */
{
FACT_t push_val;
FACT_num_t res, arg;
arg = GET_ARG_NUM ();
if (arg->value->fp) {
res = FACT_alloc_num ();
mpz_set_f (res->value->intv, arg->value->fltv);
push_val.ap = res;
} else
push_val.ap = arg;
push_val.type = NUM_TYPE;
push_v (push_val);
}
static void FBIF_print (void) /* Print an ASCII string or numerical value. */
{
int len;
FACT_num_t arg;
static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
arg = GET_ARG_NUM();
pthread_mutex_lock(&print_lock);
/* If the argument is an array, print it as a string. Otherwise, print the
* numerical value.
*/
if (arg->array_size == 0) /* Print a newline, as it is a number */
len = printf("%s\n", mpc_get_str(arg->value)) - 1;
else
len = printf("%s", FACT_natos(arg));
fflush(stdout);
pthread_mutex_unlock(&print_lock);
push_constant_ui(len);
}
static void FBIF_str (void) /* Convert a number to a string. */
{
FACT_t push_val;
push_val.type = NUM_TYPE;
push_val.ap = FACT_stona (mpc_get_str (GET_ARG_NUM ()->value));
push_v (push_val);
}
static void FBIF_size (void) /* Return the size of an array. */
{
FACT_t push_val;
FACT_num_t res;
res = FACT_alloc_num ();
mpc_set_ui (res->value, GET_ARG_NUM ()->array_size);
push_val.type = NUM_TYPE;
push_val.ap = res;
push_v (push_val);
}
static void FBIF_error (void) /* Return the current error message. */
{
FACT_t push_val;
push_val.type = NUM_TYPE;
push_val.ap = FACT_stona ((char *) curr_thread->curr_err.what);
push_v (push_val);
}
static void FBIF_throw (void) /* Throw an error. */
{
FACT_num_t msg;
msg = GET_ARG_NUM ();
FACT_throw_error (CURR_THIS->caller, FACT_natos (msg));
}
static void FBIF_send (void) /* Send a message to a thread. */
{
FACT_num_t dest, msg;
// printf("entered\n");
msg = GET_ARG_NUM ();
dest = GET_ARG_NUM ();
FACT_send_message (msg, mpc_get_ui (dest->value));
push_constant_ui (0);
}
static void FBIF_receive (void) /* Pop the current thread's message queue. */
{
FACT_t res;
res.type = SCOPE_TYPE;
res.ap = FACT_get_next_message ();
push_v (res);
}
static void FBIF_ID(void)
{
push_constant_ui(curr_thread->thread_num);
}
static void FBIF_exit (void) /* Exit. */
{
exit (mpc_get_si (GET_ARG_NUM ()->value));
}
static void FBIF_load (void) /* Run a file in the current scope. */
{
FACT_load_file (FACT_natos (GET_ARG_NUM ()));
push_constant_ui (0);
}
static void *get_arg (FACT_type type_of_arg) /* Get an argument. */
{
FACT_t pop_res;
pop_res = pop_v ();
/* Throw an error if argument types do not match. */
if (type_of_arg != pop_res.type)
FACT_throw_error (CURR_THIS, "argument types do not match");
return pop_res.ap;
}