-
Notifications
You must be signed in to change notification settings - Fork 30
/
snek-func.c
167 lines (143 loc) · 3.94 KB
/
snek-func.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
/*
* Copyright © 2018 Keith Packard <[email protected]>
*
* This program 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.
*
* This program 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.
*/
#include "snek.h"
snek_code_t *snek_stash_code;
snek_func_t *
snek_func_alloc(snek_code_t *code)
{
snek_func_t *func;
snek_stash_code = code;
func = snek_alloc(sizeof (snek_func_t) + snek_parse_nformal * sizeof (snek_id_t));
code = snek_stash_code;
snek_stash_code = NULL;
if (!func)
return NULL;
func->code = snek_pool_offset(code);
func->nformal = snek_parse_nformal;
func->nrequired = snek_parse_nformal - snek_parse_nnamed;
memcpy(func->formals, snek_parse_formals, snek_parse_nformal * sizeof (snek_id_t));
return func;
}
/*
* Assign a function formal from an actual value.
*
* Check to make sure there aren't duplicate assignments,
* e.g. a positional actual and named actual that end up
* naming the same formal.
*/
static bool __attribute__((noinline))
snek_func_actual(snek_id_t id, snek_poly_t value, uint8_t pos)
{
snek_variable_t *existing = &snek_frame->variables[snek_frame->nvariables-1];
snek_variable_t *insert = &snek_frame->variables[pos];
/* Check existing formals to see if we're duplicating one of
* those
*/
while (existing > insert) {
if (existing->id == id)
return false;
existing--;
}
/* All good, assign the name and value in the frame */
existing->id = id;
existing->value = value;
return true;
}
/*
* Make sure a named actual matches a known formal
*/
static bool
snek_func_check_formal(snek_id_t id, snek_func_t *func)
{
for (uint8_t f = 0; f < func->nformal; f++)
if (func->formals[f] == id)
return true;
return false;
}
/*
* Create a new frame holding all of the actuals
*/
bool
snek_func_push(uint8_t nposition, uint8_t nnamed, snek_offset_t ip)
{
/* Allocate the frame first so that
* nothing moves during the rest of the function
*/
uint8_t nparam = nposition + nnamed;
if (!snek_frame_push(ip, nparam))
return false;
snek_func_t *func = snek_poly_to_func(snek_a);
/* Check to make sure we don't pass more actuals by position
* than there are formals in the function
*/
if (nposition > func->nformal) {
snek_error_args(func->nformal, nposition);
goto fail_frame;
}
uint8_t pos = nparam;
snek_offset_t save_stackp = snek_stackp;
snek_poly_t value;
snek_id_t id;
/* Assign formals from actuals */
while (pos) {
pos--;
value = snek_stack_pop();
if (pos >= nposition) {
/* named actuals come last */
id = snek_stack_pop_soffset();
/* Make sure the actual matches a formal */
if (!snek_func_check_formal(id, func))
goto fail;
} else {
/* positional actuals come first */
id = func->formals[pos];
}
/* Insert into the frame, checking for duplicates */
if (!snek_func_actual(id, value, pos))
goto fail;
}
/* Make sure all required formals were given values */
for (pos = 0; pos < func->nrequired; pos++)
if (!snek_frame->variables[pos].id) {
id = func->formals[pos];
goto fail;
}
return true;
fail:
/* Restore stack and complain about the invalid actual */
snek_stackp = save_stackp;
snek_error_arg(id);
fail_frame:
/* Remove the invalid frame */
(void) snek_frame_pop();
return false;
}
snek_offset_t
snek_func_size(void *addr)
{
snek_func_t *func = addr;
return (snek_offset_t) sizeof (snek_func_t) + func->nformal * (snek_offset_t) sizeof (snek_id_t);
}
void
snek_func_mark(void *addr)
{
snek_func_t *func = addr;
snek_mark_offset(&snek_code_mem, func->code);
}
void
snek_func_move(void *addr)
{
snek_func_t *func = addr;
snek_move_offset(&snek_code_mem, &func->code);
}