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
|
/*
* Copyright 2007-2018 Adrian Thurston <thurston@colm.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <colm/pdarun.h>
#include <colm/program.h>
#include <colm/struct.h>
#include <colm/bytecode.h>
static void colm_list_add_after( list_t *list, list_el_t *prev_el, list_el_t *new_el );
static void colm_list_add_before( list_t *list, list_el_t *next_el, list_el_t *new_el);
list_el_t *colm_list_detach( list_t *list, list_el_t *el );
void colm_list_prepend( list_t *list, list_el_t *new_el )
{
colm_list_add_before( list, list->head, new_el );
}
void colm_list_append( list_t *list, list_el_t *new_el )
{
colm_list_add_after( list, list->tail, new_el );
}
list_el_t *colm_list_detach_head( list_t *list )
{
return colm_list_detach( list, list->head );
}
list_el_t *colm_list_detach_tail( list_t *list )
{
return colm_list_detach( list, list->tail );
}
long colm_list_length( list_t *list )
{
return list->list_len;
}
void colm_vlist_append( struct colm_program *prg, list_t *list, value_t value )
{
struct colm_struct *s = colm_struct_new( prg, list->generic_info->el_struct_id );
colm_struct_set_field( s, value_t, 0, value );
list_el_t *list_el = colm_struct_get_addr( s, list_el_t*, list->generic_info->el_offset );
colm_list_append( list, list_el );
}
void colm_vlist_prepend( struct colm_program *prg, list_t *list, value_t value )
{
struct colm_struct *s = colm_struct_new( prg, list->generic_info->el_struct_id );
colm_struct_set_field( s, value_t, 0, value );
list_el_t *list_el = colm_struct_get_addr( s, list_el_t*, list->generic_info->el_offset );
colm_list_prepend( list, list_el );
}
value_t colm_vlist_detach_tail( struct colm_program *prg, list_t *list )
{
list_el_t *list_el = list->tail;
colm_list_detach( list, list_el );
struct colm_struct *s = colm_generic_el_container( prg, list_el,
(list->generic_info - prg->rtd->generic_info) );
value_t val = colm_struct_get_field( s, value_t, 0 );
if ( list->generic_info->value_type == TYPE_TREE )
colm_tree_upref( prg, (tree_t*)val );
return val;
}
value_t colm_vlist_detach_head( struct colm_program *prg, list_t *list )
{
list_el_t *list_el = list->head;
colm_list_detach( list, list_el );
struct colm_struct *s = colm_generic_el_container( prg, list_el,
(list->generic_info - prg->rtd->generic_info) );
value_t val = colm_struct_get_field( s, value_t, 0 );
if ( list->generic_info->value_type == TYPE_TREE )
colm_tree_upref( prg, (tree_t*) val );
return val;
}
static void colm_list_add_after( list_t *list, list_el_t *prev_el, list_el_t *new_el )
{
/* Set the previous pointer of new_el to prev_el. We do
* this regardless of the state of the list. */
new_el->list_prev = prev_el;
/* Set forward pointers. */
if (prev_el == 0) {
/* There was no prev_el, we are inserting at the head. */
new_el->list_next = list->head;
list->head = new_el;
}
else {
/* There was a prev_el, we can access previous next. */
new_el->list_next = prev_el->list_next;
prev_el->list_next = new_el;
}
/* Set reverse pointers. */
if (new_el->list_next == 0) {
/* There is no next element. Set the tail pointer. */
list->tail = new_el;
}
else {
/* There is a next element. Set it's prev pointer. */
new_el->list_next->list_prev = new_el;
}
/* Update list length. */
list->list_len++;
}
static void colm_list_add_before( list_t *list, list_el_t *next_el, list_el_t *new_el)
{
/* Set the next pointer of the new element to next_el. We do
* this regardless of the state of the list. */
new_el->list_next = next_el;
/* Set reverse pointers. */
if (next_el == 0) {
/* There is no next elememnt. We are inserting at the tail. */
new_el->list_prev = list->tail;
list->tail = new_el;
}
else {
/* There is a next element and we can access next's previous. */
new_el->list_prev = next_el->list_prev;
next_el->list_prev = new_el;
}
/* Set forward pointers. */
if (new_el->list_prev == 0) {
/* There is no previous element. Set the head pointer.*/
list->head = new_el;
}
else {
/* There is a previous element, set it's next pointer to new_el. */
new_el->list_prev->list_next = new_el;
}
list->list_len++;
}
list_el_t *colm_list_detach( list_t *list, list_el_t *el )
{
/* Set forward pointers to skip over el. */
if (el->list_prev == 0)
list->head = el->list_next;
else
el->list_prev->list_next = el->list_next;
/* Set reverse pointers to skip over el. */
if (el->list_next == 0)
list->tail = el->list_prev;
else
el->list_next->list_prev = el->list_prev;
/* Update List length and return element we detached. */
list->list_len--;
return el;
}
void colm_list_destroy( struct colm_program *prg, tree_t **sp, struct colm_struct *s )
{
}
list_t *colm_list_new( struct colm_program *prg )
{
size_t memsize = sizeof(struct colm_list);
struct colm_list *list = (struct colm_list*) malloc( memsize );
memset( list, 0, memsize );
colm_struct_add( prg, (struct colm_struct *)list );
list->id = prg->rtd->struct_inbuilt_id;
list->destructor = &colm_list_destroy;
return list;
}
struct colm_struct *colm_list_get( struct colm_program *prg,
list_t *list, word_t gen_id, word_t field )
{
struct generic_info *gi = &prg->rtd->generic_info[gen_id];
list_el_t *result = 0;
switch ( field ) {
case 0:
result = list->head;
break;
case 1:
result = list->tail;
break;
default:
assert( 0 );
break;
}
struct colm_struct *s = result != 0 ?
colm_struct_container( result, gi->el_offset ) : 0;
return s;
}
struct colm_struct *colm_list_el_get( struct colm_program *prg,
list_el_t *list_el, word_t gen_id, word_t field )
{
struct generic_info *gi = &prg->rtd->generic_info[gen_id];
list_el_t *result = 0;
switch ( field ) {
case 0:
result = list_el->list_prev;
break;
case 1:
result = list_el->list_next;
break;
default:
assert( 0 );
break;
}
struct colm_struct *s = result != 0 ?
colm_struct_container( result, gi->el_offset ) : 0;
return s;
}
|