This repository has been archived by the owner on Sep 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlabel.nw
347 lines (272 loc) · 10.8 KB
/
label.nw
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
% -*- mode: Noweb; noweb-code-mode: c-mode -*- %
\ifx\nointro\undefined
This document contains the interface and implementation of labels used in the interpreter.
\fi
% ----------------------------------------------------------------------------
\interface{[[label]] : {\PAL} Labels and Namespace Management}
% ----------------------------------------------------------------------------
We define our own sort of labels to allow more information than a simple
memory location to be passed around at runtime.
<<label.h>>=
#ifndef _LABEL_H
#define _LABEL_H
#include <lua.h>
#include <mclib.h>
#include "assemblyunit.h"
#include "controlflow.h"
<<type definitions>>
<<global variable declarations>>
<<function prototypes>>
<<macro definitions>>
#endif /* _LABEL_H */
@
% ----------------------------------------------------------------------------
\subsection{Data Structures}
% ----------------------------------------------------------------------------
For the purpose of storing all labels in one namespace (both explicitly defined labels and the labels demarcating {\PAL} procedures), we create a new type.
<<type definitions>>=
typedef struct {
enum {
CMM_LABEL, CMM_PROCEDURE, CMM_CFUNCTION
} ty;
union {
RAddr raddr;
procedure *proc ;
unsigned int cfunc_id;
} lbl;
assembly_unit *unit;
} CMM_label;
@
% ----------------------------------------------------------------------------
\subsection{Global Variables}
% ----------------------------------------------------------------------------
We maintain a global table to represent the namespace of exported symbols
(could include {\PAL} symbols declared as exports as well as exported external
symbols like properly registered C functions).
<<global variable declarations>>=
extern table exports_table;
@
<<global variable definitions>>=
table exports_table;
@
% ----------------------------------------------------------------------------
\subsection{Managing Memory for [[CMM_label]]s}
% ----------------------------------------------------------------------------
To abstract away from the details of memory management, this interface
provides a function to create a new [[CMM_label]] and a function that frees
all existing [[CMM_label]]s in memory.
<<function prototypes>>=
CMM_label *CMM_label_new(void);
void CMM_label_free_all(void);
@
Note that [[CMM_label_new]] returns a pointer to an \emph{uninitialized}
[[CMM_label]].
% ----------------------------------------------------------------------------
\subsection{Helpers}
% ----------------------------------------------------------------------------
\paragraph{Manipulating [[CMM_label]]s}
We provide a few convenience macros for observing [[CMM_label]]s, regardless
of their ``label type''.
[[CMM_label_defined]] returns 0 if and only if the [[RAddr]] associated with
the given [[CMM_label]] has yet been tied down to a location within an
[[RBlock]].
[[CMM_label_raddr]] returns the [[RAddr]] associated with a [[CMM_label]] and
[[CMM_label_location]] returns the permanent relocation associated with a
[[CMM_label]].
Note that [[CMM_label_raddr]] returns a valid lvalue.
Also note that it is an unchecked runtime error to use the value returned by
[[CMM_label_location]] if the [[RAddr]] associated with the given
[[CMM_label]] has not yet been permanently relocated.
<<faux prototypes>>=
int CMM_label_defined(CMM_label *lbl);
RAddr CMM_label_raddr(CMM_label *lbl);
bytecodeptr CMM_label_location(CMM_label *lbl);
@
\paragraph{Finding a Label}
[[get_label]] takes a [[lua_State]] and a string [[name]], expects this string
to also be atop the Lua stack within the specified [[lua_State]], pops the
string off the Lua stack, and returns the [[CMM_label]] structure associated
with the label named [[name]].
If this label was not previously defined, [[get_label]] creates a fresh
[[CMM_label]] and links it with the string [[name]] for future queries.
[[CMM_label]]s created in this way are temporarily initialized as
[[CMM_LABEL]]s.
<<function prototypes>>=
CMM_label *get_label(lua_State *L, const char *name);
@
To capture a reference to an offset from a given [[CMM_label]], we provide
[[get_label_offset]].
<<function prototypes>>=
CMM_label *get_label_offset(CMM_label *lbl, int offset);
@
Once {\PAL} code has been loaded into memory, clients may use our exported
function [[cmm_find_export]] to find the location associated with an exported
{\PAL} label.
A [[NULL]] pointer is returned if the given string cannot be found in the
exported symbol table.
<<function prototypes>>=
CMM_label *cmm_find_export(const char *arg);
@
% ----------------------------------------------------------------------------
\implementation{{\PAL} Labels and Namespace Management}
% ----------------------------------------------------------------------------
<<label.c>>=
#include "label.h"
#include <assert.h>
#include <stdlib.h>
#include "table.h"
#include "lualink.h"
#include "interp.h"
#include <cii/arena.h>
<<internal static variable definitions>>
<<global variable definitions>>
<<function definitions>>
@
% ----------------------------------------------------------------------------
\subsection{Memory Allocation}
% ----------------------------------------------------------------------------
We make use of Hanson's ``arenas'' (see \emph{C Interfaces and Implementations})
to assist in allocating and de-allocating memory for [[CMM_label]]s.
<<internal static variable definitions>>=
static Arena_T CMM_label_arena = NULL;
@
<<function definitions>>=
CMM_label *CMM_label_new(void) {
CMM_label *lbl;
if (CMM_label_arena == NULL) {
CMM_label_arena = Arena_new();
mem_assert(CMM_label_arena);
}
lbl = (CMM_label *) Arena_alloc(CMM_label_arena, sizeof(CMM_label),
__FILE__, __LINE__);
mem_assert(lbl);
return lbl;
}
void CMM_label_free_all(void) {
if (CMM_label_arena != NULL) {
Arena_free(CMM_label_arena);
Arena_dispose(&CMM_label_arena);
CMM_label_arena = NULL;
}
}
@
% ----------------------------------------------------------------------------
\subsection{Utility Macros}
% ----------------------------------------------------------------------------
Macro [[CMM_label_defined]] simplifies testing whether or not a given
[[CMM_label]] has yet been defined (i.e., tied down to a location in an
[[RBlock]]).
<<macro definitions>>=
#define CMM_label_defined(LBL) \
(((LBL)->ty == CMM_PROCEDURE && \
block_defined((LBL)->lbl.proc->raddr->label->block)) || \
((LBL)->ty == CMM_LABEL && \
block_defined((LBL)->lbl.raddr->label->block)) || \
((LBL)->ty == CMM_CFUNCTION))
@
Macro [[CMM_label_raddr]] returns the [[RAddr]] to which a [[CMM_label]] is
tied, and [[CMM_label_location]] returns the location to which a [[CMM_label]]
is tied.
Note that the value returned by this macro is a valid lvalue.
It is an unchecked runtime error (will likely lead to segfault) to get the
[[CMM_label_raddr]] or [[CMM_label_location]] of a [[CMM_CFUNCTION]]-typed
[[CMM_label]].
<<macro definitions>>=
#define CMM_label_raddr(LBL) \
( (LBL)->ty == CMM_PROCEDURE ? (LBL)->lbl.proc->raddr \
: (LBL)->lbl.raddr)
#define CMM_label_location(LBL) \
((bytecodeptr) ((LBL)->ty == CMM_PROCEDURE ? location((LBL)->lbl.proc->raddr)\
: location((LBL)->lbl.raddr)))
@
% ----------------------------------------------------------------------------
\subsection{Utility Functions}
% ----------------------------------------------------------------------------
[[get_label]] expects to see a string atop the Lua stack and leaves the Lua stack with one less item than it started with.
We look to see if there is a value associated with [[name]] in the Lua label
table.
If there is a value attached with [[name]] in the table, we return it.
<<function definitions>>=
CMM_label *get_label(lua_State *L, const char *name) {
CMM_label *lbl;
/* expects a string atop the stack ! */
lua_assert_isstring(L, -1, "cannot define a nameless label in C--");
<<find Lua label table>>
lua_insert(L, -2); /* swap stack positions of key string and table */
lua_pushvalue(L, -1); /* make copy of key string atop stack */
lua_gettable(L, -3); /* look up string to see if is defined */
if (lua_isuserdata(L, -1) &&
lua_tag(L, -1) == lua_CMM_label_tag &&
(lbl = (CMM_label *) lua_touserdata(L, -1)) != NULL)
{
lua_pop(L, 3); /* pop userdata, key string, table */
} else {
RLabel label;
RAddr addr;
<<create new [[CMM_label]] and store in label table>>
lua_pop(L, 1); /* pop table */
}
return lbl;
}
@
We call [[lua_pushtable]] to help us find the label table.
<<find Lua label table>>=
lua_pushtable(L, unit->label_table);
@
If [[name]] has no non-nil value attached to it in the table, we create a new [[RAddr]], store it in the table under the key [[name]], and return it.
<<create new [[CMM_label]] and store in label table>>=
label = label_new(name);
addr = addr_new(label, 0);
lbl = CMM_label_new();
lbl->ty = CMM_LABEL;
lbl->lbl.raddr = addr;
lua_pop(L, 1); /* pop nil off */
lua_pushusertag(L, (void *) lbl, lua_CMM_label_tag);
lua_settable(L, -3); /* pops off pointer, key string */
@
Helper [[get_label_offset]] simply constructs a new [[RAddr]] with the given
offset from the [[RAddr]] stored in the given [[CMM_label]].
<<function definitions>>=
CMM_label *get_label_offset(CMM_label *lbl, int offset) {
RAddr old = CMM_label_raddr(lbl);
CMM_label *new;
cmm_assert(old != NULL && old->label != NULL,
"cannot get_label_offset from undeclared label");
new = CMM_label_new();
new->ty = CMM_LABEL;
new->lbl.raddr = addr_new(old->label, 0);
return new;
}
@
\paragraph{Exported Functions}
Exported function [[cmm_find_export]] finds a pointer to the location in
program memory associated with the exported {\PAL} symbol [[arg]].
We find the [[arg]] label by making a query to the Lua label table and making
sure that the [[RAddr]] associated with the label has a valid permanent
address.
WARNING FIX [[unit]] is currently set here; this could break something.
<<function definitions>>=
CMM_label *cmm_find_export(const char *arg) {
lua_State *L = exports_table.L;
CMM_label *lbl;
RAddr raddr;
lua_pushtable(L, exports_table);
lua_pushstring(L, arg);
lua_gettable(L, -2);
if (!lua_isuserdata(L, -1) ||
lua_tag(L, -1) != lua_CMM_label_tag ||
(lbl = lua_touserdata(L, -1)) == NULL ||
!CMM_label_defined(lbl))
{
lua_pop(L, 2); /* pop bogus value, table */
return NULL; /* signify failure */
}
lua_pop(L, 2); /* pop label, table */
raddr = CMM_label_raddr(lbl);
assert(location_known(raddr));
assert(location(raddr) != 0);
/* FIX consider removing this */
unit = lbl->unit;
return lbl;
}
@