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
/*
* jit-meta.c - Functions for manipulating metadata lists.
*
* Copyright (C) 2004 Southern Storm Software, Pty Ltd.
*
* This file is part of the libjit library.
*
* The libjit library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 2.1 of
* the License, or (at your option) any later version.
*
* The libjit library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the libjit library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "jit-internal.h"
/*@
@section Metadata handling
@cindex Metadata handling
@cindex jit-meta.h
Many of the structures in the @code{libjit} library can have user-supplied
metadata associated with them. Metadata may be used to store dependency
graphs, branch prediction information, or any other information that is
useful to optimizers or code generators.
Metadata can also be used by higher level user code to store information
about the structures that is specific to the user's virtual machine or
language.
The library structures have special-purpose metadata routines associated
with them (e.g. @code{jit_function_set_meta}, @code{jit_block_get_meta}).
However, sometimes you may wish to create your own metadata lists and
attach them to your own structures. The functions below enable you
to do this:
@*/
/*@
* @deftypefun int jit_meta_set (jit_meta_t *@var{list}, int @var{type}, void *@var{data}, jit_meta_free_func @var{free_data}, jit_function_t @var{pool_owner})
* Set a metadata value on a list. If the @var{type} is already present
* in the list, then its previous value will be freed. The @var{free_func}
* is called when the metadata value is freed with @code{jit_meta_free}
* or @code{jit_meta_destroy}. Returns zero if out of memory.
*
* If @var{pool_owner} is not NULL, then the metadata value will persist
* until the specified function is finished building. Normally you would
* set this to NULL.
*
* Metadata type values of 10000 or greater are reserved for internal use.
* They should never be used by external user code.
* @end deftypefun
@*/
int jit_meta_set(jit_meta_t *list, int type, void *data,
jit_meta_free_func free_data, jit_function_t pool_owner)
{
jit_meta_t current;
/* See if we already have this type in the list */
current = *list;
while(current != 0)
{
if(current->type == type)
{
if(data == current->data)
{
/* The value is unchanged, so don't free the previous value */
return 1;
}
if(current->free_data)
{
(*(current->free_data))(current->data);
}
current->data = data;
current->free_data = free_data;
return 1;
}
current = current->next;
}
/* Create a new metadata block and add it to the list */
if(pool_owner)
{
if((current = jit_memory_pool_alloc
(&(pool_owner->builder->meta_pool), struct _jit_meta)) == 0)
{
return 0;
}
}
else
{
if((current = jit_new(struct _jit_meta)) == 0)
{
return 0;
}
}
current->type = type;
current->data = data;
current->free_data = free_data;
current->next = *list;
current->pool_owner = pool_owner;
*list = current;
return 1;
}
/*@
* @deftypefun {void *} jit_meta_get (jit_meta_t @var{list}, int @var{type})
* Get the value associated with @var{type} in the specified @var{list}.
* Returns NULL if @var{type} is not present.
* @end deftypefun
@*/
void *jit_meta_get(jit_meta_t list, int type)
{
while(list != 0)
{
if(list->type == type)
{
return list->data;
}
list = list->next;
}
return 0;
}
/*@
* @deftypefun void jit_meta_free (jit_meta_t *@var{list}, int @var{type})
* Free the metadata value in the @var{list} that has the
* specified @var{type}. Does nothing if the @var{type}
* is not present.
* @end deftypefun
@*/
void jit_meta_free(jit_meta_t *list, int type)
{
jit_meta_t current = *list;
jit_meta_t prev = 0;
while(current != 0)
{
if(current->type == type)
{
if(current->free_data)
{
(*(current->free_data))(current->data);
current->free_data = 0;
}
if(prev)
{
prev->next = current->next;
}
else
{
*list = current->next;
}
if(current->pool_owner)
{
jit_memory_pool_dealloc
(&(current->pool_owner->builder->meta_pool), current);
}
else
{
jit_free(current);
}
return;
}
else
{
prev = current;
current = current->next;
}
}
}
/*@
* @deftypefun void jit_meta_destroy (jit_meta_t *@var{list})
* Destroy all of the metadata values in the specified @var{list}.
* @end deftypefun
@*/
void jit_meta_destroy(jit_meta_t *list)
{
jit_meta_t current = *list;
jit_meta_t next;
while(current != 0)
{
next = current->next;
if(current->free_data)
{
(*(current->free_data))(current->data);
current->free_data = 0;
}
if(current->pool_owner)
{
jit_memory_pool_dealloc
(&(current->pool_owner->builder->meta_pool), current);
}
else
{
jit_free(current);
}
current = next;
}
}
void _jit_meta_free_one(void *meta)
{
jit_meta_t current = (jit_meta_t)meta;
if(current->free_data)
{
(*(current->free_data))(current->data);
}
}