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
|
/*
* Copyright 2006-2011 Adrian Thurston <thurston@complang.org>
*/
/* This file is part of Colm.
*
* Colm 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 2 of the License, or
* (at your option) any later version.
*
* Colm 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 Colm; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <colm/pdarun.h>
#include <colm/fsmrun.h>
#include <colm/tree.h>
#include <colm/bytecode.h>
#include <colm/pool.h>
#include <colm/debug.h>
#include <colm/config.h>
#include <alloca.h>
#include <sys/mman.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
void colmInit( long debugRealm )
{
/* Always on because because logging is controlled with ifdefs in\n" the
* runtime lib. */
colm_log_bytecode = 1;
colm_log_parse = 1;
colm_log_match = 1;
colm_log_compile = 1;
colm_log_conds = 1;
colmActiveRealm = debugRealm;
initInputFuncs();
}
void colmRunProgram( Program *prg )
{
assert( sizeof(Int) <= sizeof(Tree) );
assert( sizeof(Str) <= sizeof(Tree) );
assert( sizeof(Pointer) <= sizeof(Tree) );
assert( sizeof(Map) <= sizeof(MapEl) );
assert( sizeof(List) <= sizeof(MapEl) );
assert( sizeof(Stream) <= sizeof(MapEl) );
assert( sizeof(Parser) <= sizeof(MapEl) );
/* Allocate the global variable. */
allocGlobal( prg );
/*
* Allocate the VM stack.
*/
prg->vm_stack = stackAlloc();
prg->vm_root = &prg->vm_stack[VM_STACK_SIZE];
/*
* Execute
*/
if ( prg->rtd->rootCodeLen > 0 ) {
//RtCodeVect rcodeCollect;
Execution execution;
initExecution( &execution, 0, 0, 0, 0, prg->rtd->rootFrameId );
mainExecution( prg, &execution, prg->rtd->rootCode );
}
}
void clearGlobal( Program *prg, Tree **sp )
{
/* Downref all the fields in the global object. */
int g;
for ( g = 0; g < prg->rtd->globalSize; g++ ) {
//assert( getAttr( global, g )->refs == 1 );
treeDownref( prg, sp, getAttr( prg->global, g ) );
}
/* Free the global object. */
if ( prg->rtd->globalSize > 0 )
freeAttrs( prg, prg->global->child );
treeFree( prg, prg->global );
}
void allocGlobal( Program *prg )
{
/* Alloc the global. */
Tree *tree = treeAllocate( prg );
tree->child = allocAttrs( prg, prg->rtd->globalSize );
tree->refs = 1;
prg->global = tree;
}
Tree **stackAlloc()
{
//return new Tree*[VM_STACK_SIZE];
return (Tree**)mmap( 0, sizeof(Tree*)*VM_STACK_SIZE,
PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 );
}
Tree **vm_root( struct ColmProgram *prg )
{
return prg->vm_root;
}
Tree *returnVal( struct ColmProgram *prg )
{
return prg->returnVal;
}
Program *colmNewProgram( RuntimeData *rtd, int argc, const char **argv )
{
Program *prg = malloc(sizeof(Program));
memset( prg, 0, sizeof(Program) );
prg->argc = argc;
prg->argv = argv;
prg->rtd = rtd;
prg->ctxDepParsing = 1;
prg->global = 0;
prg->heap = 0;
prg->stdinVal = 0;
prg->stdoutVal = 0;
prg->stderrVal = 0;
prg->nextIlGen = 0;
prg->induceExit = 0;
prg->exitStatus = 0;
initPoolAlloc( &prg->kidPool, sizeof(Kid) );
initPoolAlloc( &prg->treePool, sizeof(Tree) );
initPoolAlloc( &prg->parseTreePool, sizeof(ParseTree) );
initPoolAlloc( &prg->listElPool, sizeof(ListEl) );
initPoolAlloc( &prg->mapElPool, sizeof(MapEl) );
initPoolAlloc( &prg->headPool, sizeof(Head) );
initPoolAlloc( &prg->locationPool, sizeof(Location) );
initPoolAlloc( &prg->ilPool, sizeof(IgnoreList) );
Int *trueInt = (Int*) treeAllocate( prg );
trueInt->id = LEL_ID_BOOL;
trueInt->refs = 1;
trueInt->value = 1;
Int *falseInt = (Int*) treeAllocate( prg );
falseInt->id = LEL_ID_BOOL;
falseInt->refs = 1;
falseInt->value = 0;
prg->trueVal = (Tree*)trueInt;
prg->falseVal = (Tree*)falseInt;
prg->allocRunBuf = 0;
prg->returnVal = 0;
prg->lastParseError = 0;
return prg;
}
int colmDeleteProgram( Program *prg )
{
Tree **sp = prg->vm_root;
int exitStatus = prg->exitStatus;
#ifdef COLM_LOG_BYTECODE
if ( colm_log_bytecode ) {
cerr << "clearing the prg" << endl;
}
#endif
treeDownref( prg, sp, prg->returnVal );
treeDownref( prg, sp, prg->lastParseError );
clearGlobal( prg, sp );
/* Clear the heap. */
Kid *a = prg->heap;
while ( a != 0 ) {
Kid *next = a->next;
treeDownref( prg, sp, a->tree );
kidFree( prg, a );
a = next;
}
//assert( trueVal->refs == 1 );
//assert( falseVal->refs == 1 );
treeDownref( prg, sp, prg->trueVal );
treeDownref( prg, sp, prg->falseVal );
treeDownref( prg, sp, (Tree*)prg->stdinVal );
treeDownref( prg, sp, (Tree*)prg->stdoutVal );
treeDownref( prg, sp, (Tree*)prg->stderrVal );
long kidLost = kidNumLost( prg );
if ( kidLost )
message( "warning: lost kids: %ld\n", kidLost );
long treeLost = treeNumLost( prg );
if ( treeLost )
message( "warning: lost trees: %ld\n", treeLost );
long parseTreeLost = parseTreeNumLost( prg );
if ( parseTreeLost )
message( "warning: lost parse trees: %ld\n", parseTreeLost );
long listLost = listElNumLost( prg );
if ( listLost )
message( "warning: lost listEls: %ld\n", listLost );
long mapLost = mapElNumLost( prg );
if ( mapLost )
message( "warning: lost mapEls: %ld\n", mapLost );
long headLost = headNumLost( prg );
if ( headLost )
message( "warning: lost heads: %ld\n", headLost );
long locationLost = locationNumLost( prg );
if ( locationLost )
message( "warning: lost locations: %ld\n", locationLost );
long ilLost = ilNumLost( prg );
if ( ilLost )
message( "warning: lost ignore lists: %ld\n", ilLost );
kidClear( prg );
treeClear( prg );
headClear( prg );
parseTreeClear( prg );
listElClear( prg );
mapElClear( prg );
locationClear( prg );
ilClear( prg );
//memset( vm_stack, 0, sizeof(Tree*) * VM_STACK_SIZE);
RunBuf *rb = prg->allocRunBuf;
while ( rb != 0 ) {
RunBuf *next = rb->next;
free( rb );
rb = next;
}
free( prg );
return exitStatus;
}
|