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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/*
* 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
*/
#ifndef _REDFSM_H
#define _REDFSM_H
#include <assert.h>
#include <string.h>
#include <string>
#include "keyops.h"
#include "vector.h"
#include "dlist.h"
#include "compare.h"
#include "bstmap.h"
#include "bstset.h"
#include "avlmap.h"
#include "avltree.h"
#include "avlbasic.h"
#include "mergesort.h"
#include "sbstmap.h"
#include "sbstset.h"
#include "sbsttable.h"
#include "colm.h"
#include "fsmrun.h"
#include "fsmrun.h"
#define TRANS_ERR_TRANS 0
#define STATE_ERR_STATE 0
#define FUNC_NO_FUNC 0
using std::string;
struct RedState;
struct InlineList;
struct ParseData;
struct ObjField;
/* Element in list of actions. Contains the string for the code to exectute. */
struct GenAction
{
/* Data collected during parse. */
InputLoc loc;
char *name;
InlineList *inlineList;
int actionId;
MarkType markType;
ObjField *objField;
long markId;
int numTransRefs;
int numToStateRefs;
int numFromStateRefs;
int numEofRefs;
GenAction *prev, *next;
};
typedef DList<GenAction> GenActionList;
string nameOrLoc( GenAction *genAction );
/* Number of references in the final machine. */
inline int numRefs( GenAction *genAction )
{
return genAction->numTransRefs +
genAction->numToStateRefs +
genAction->numFromStateRefs +
genAction->numEofRefs;
}
/* Forwards. */
struct RedState;
struct FsmState;
/* Transistion GenAction Element. */
typedef SBstMapEl< int, GenAction* > GenActionTableEl;
/* Transition GenAction Table. */
struct GenActionTable
: public SBstMap< int, GenAction*, CmpOrd<int> >
{
void setAction( int ordering, GenAction *action );
void setActions( int *orderings, GenAction **actions, int nActs );
void setActions( const GenActionTable &other );
};
/* Compare of a whole action table element (key & value). */
struct GenCmpActionTableEl
{
static int compare( const GenActionTableEl &action1,
const GenActionTableEl &action2 )
{
if ( action1.key < action2.key )
return -1;
else if ( action1.key > action2.key )
return 1;
else if ( action1.value < action2.value )
return -1;
else if ( action1.value > action2.value )
return 1;
return 0;
}
};
/* Compare for GenActionTable. */
typedef CmpSTable< GenActionTableEl, GenCmpActionTableEl > GenCmpActionTable;
/* Set of states. */
typedef BstSet<RedState*> RedStateSet;
typedef BstSet<int> IntSet;
/* Reduced action. */
struct RedAction
:
public AvlTreeEl<RedAction>
{
RedAction( )
:
key(),
eofRefs(0),
numTransRefs(0),
numToStateRefs(0),
numFromStateRefs(0),
numEofRefs(0),
bAnyNextStmt(false),
bAnyCurStateRef(false),
bAnyBreakStmt(false)
{ }
const GenActionTable &getKey()
{ return key; }
GenActionTable key;
int actListId;
int location;
IntSet *eofRefs;
/* Number of references in the final machine. */
bool numRefs()
{ return numTransRefs + numToStateRefs + numFromStateRefs + numEofRefs; }
int numTransRefs;
int numToStateRefs;
int numFromStateRefs;
int numEofRefs;
bool anyNextStmt() { return bAnyNextStmt; }
bool anyCurStateRef() { return bAnyCurStateRef; }
bool anyBreakStmt() { return bAnyBreakStmt; }
bool bAnyNextStmt;
bool bAnyCurStateRef;
bool bAnyBreakStmt;
};
typedef AvlTree<RedAction, GenActionTable, GenCmpActionTable> GenActionTableMap;
/* Reduced transition. */
struct RedTrans
:
public AvlTreeEl<RedTrans>
{
RedTrans( RedState *targ, RedAction *action, int id )
: targ(targ), action(action), id(id), labelNeeded(true) { }
RedState *targ;
RedAction *action;
int id;
bool partitionBoundary;
bool labelNeeded;
};
/* Compare of transitions for the final reduction of transitions. Comparison
* is on target and the pointer to the shared action table. It is assumed that
* when this is used the action tables have been reduced. */
struct CmpRedTrans
{
static int compare( const RedTrans &t1, const RedTrans &t2 )
{
if ( t1.targ < t2.targ )
return -1;
else if ( t1.targ > t2.targ )
return 1;
else if ( t1.action < t2.action )
return -1;
else if ( t1.action > t2.action )
return 1;
else
return 0;
}
};
typedef AvlBasic<RedTrans, CmpRedTrans> RedTransSet;
/* Element in out range. */
struct RedTransEl
{
/* Constructors. */
RedTransEl( Key lowKey, Key highKey, RedTrans *value )
: lowKey(lowKey), highKey(highKey), value(value) { }
Key lowKey, highKey;
RedTrans *value;
};
typedef Vector<RedTransEl> RedTransList;
typedef Vector<RedState*> RedStateVect;
typedef BstMapEl<RedState*, unsigned long long> RedSpanMapEl;
typedef BstMap<RedState*, unsigned long long> RedSpanMap;
/* Compare used by span map sort. Reverse sorts by the span. */
struct CmpRedSpanMapEl
{
static int compare( const RedSpanMapEl &smel1, const RedSpanMapEl &smel2 )
{
if ( smel1.value > smel2.value )
return -1;
else if ( smel1.value < smel2.value )
return 1;
else
return 0;
}
};
/* Sorting state-span map entries by span. */
typedef MergeSort<RedSpanMapEl, CmpRedSpanMapEl> RedSpanMapSort;
/* Set of entry ids that go into this state. */
typedef Vector<int> EntryIdVect;
typedef Vector<char*> EntryNameVect;
/* Maps entry ids (defined by the frontend, to reduced state ids. */
typedef BstMap<int, int> RedEntryMap;
typedef BstMapEl<int, int> RedEntryMapEl;
typedef Vector<int> RegionToEntry;
typedef Vector< GenAction* > GenCondSet;
struct Condition
{
Condition( )
: key(0), baseKey(0) {}
Key key;
Key baseKey;
GenCondSet condSet;
Condition *next, *prev;
};
typedef DList<Condition> ConditionList;
struct GenCondSpace
{
Key baseKey;
GenCondSet condSet;
int condSpaceId;
GenCondSpace *next, *prev;
};
typedef DList<GenCondSpace> CondSpaceList;
struct GenStateCond
{
Key lowKey;
Key highKey;
GenCondSpace *condSpace;
GenStateCond *prev, *next;
};
typedef DList<GenStateCond> GenStateCondList;
typedef Vector<GenStateCond*> StateCondVect;
/* Reduced state. */
struct RedState
{
RedState()
:
defTrans(0),
condList(0),
transList(0),
isFinal(false),
labelNeeded(false),
outNeeded(false),
onStateList(false),
toStateAction(0),
fromStateAction(0),
eofAction(0),
eofTrans(0),
id(0),
bAnyRegCurStateRef(false),
partitionBoundary(false),
inTrans(0),
numInTrans(0)
{ }
/* Transitions out. */
RedTransList outSingle;
RedTransList outRange;
RedTrans *defTrans;
/* For flat conditions. */
Key condLowKey, condHighKey;
GenCondSpace **condList;
/* For flat keys. */
Key lowKey, highKey;
RedTrans **transList;
/* The list of states that transitions from this state go to. */
RedStateVect targStates;
bool isFinal;
bool labelNeeded;
bool outNeeded;
bool onStateList;
RedAction *toStateAction;
RedAction *fromStateAction;
RedAction *eofAction;
RedTrans *eofTrans;
int id;
GenStateCondList stateCondList;
StateCondVect stateCondVect;
/* Pointers for the list of states. */
RedState *prev, *next;
bool anyRegCurStateRef() { return bAnyRegCurStateRef; }
bool bAnyRegCurStateRef;
int partition;
bool partitionBoundary;
RedTrans **inTrans;
int numInTrans;
};
/* List of states. */
typedef DList<RedState> RedStateList;
/* Set of reduced transitons. Comparison is by pointer. */
typedef BstSet< RedTrans*, CmpOrd<RedTrans*> > RedTransPtrSet;
/* Next version of the fsm machine. */
struct RedFsm
{
RedFsm();
bool wantComplete;
bool forcedErrorState;
int nextActionId;
int nextTransId;
/* Next State Id doubles as the total number of state ids. */
int nextStateId;
RedTransSet transSet;
GenActionTableMap actionMap;
RedStateList stateList;
RedStateSet entryPoints;
RedState *startState;
RedState *errState;
RedTrans *errTrans;
RedTrans *errActionTrans;
RedState *firstFinState;
int numFinStates;
int nParts;
GenAction *allActions;
RedAction *allActionTables;
Condition *allConditions;
GenCondSpace *allCondSpaces;
RedState *allStates;
GenActionList genActionList;
ConditionList conditionList;
CondSpaceList condSpaceList;
EntryIdVect entryPointIds;
EntryNameVect entryPointNames;
RedEntryMap redEntryMap;
RegionToEntry regionToEntry;
bool bAnyToStateActions;
bool bAnyFromStateActions;
bool bAnyRegActions;
bool bAnyEofActions;
bool bAnyActionGotos;
bool bAnyActionCalls;
bool bAnyActionRets;
bool bAnyRegActionRets;
bool bAnyRegActionByValControl;
bool bAnyRegNextStmt;
bool bAnyRegCurStateRef;
bool bAnyRegBreak;
bool bAnyLmSwitchError;
bool bAnyConditions;
int maxState;
int maxSingleLen;
int maxRangeLen;
int maxKeyOffset;
int maxIndexOffset;
int maxIndex;
int maxActListId;
int maxActionLoc;
int maxActArrItem;
unsigned long long maxSpan;
unsigned long long maxCondSpan;
int maxFlatIndexOffset;
Key maxKey;
int maxCondOffset;
int maxCondLen;
int maxCondSpaceId;
int maxCondIndexOffset;
int maxCond;
bool anyActions();
bool anyToStateActions() { return bAnyToStateActions; }
bool anyFromStateActions() { return bAnyFromStateActions; }
bool anyRegActions() { return bAnyRegActions; }
bool anyEofActions() { return bAnyEofActions; }
bool anyActionGotos() { return bAnyActionGotos; }
bool anyActionCalls() { return bAnyActionCalls; }
bool anyActionRets() { return bAnyActionRets; }
bool anyRegActionRets() { return bAnyRegActionRets; }
bool anyRegActionByValControl() { return bAnyRegActionByValControl; }
bool anyRegNextStmt() { return bAnyRegNextStmt; }
bool anyRegCurStateRef() { return bAnyRegCurStateRef; }
bool anyRegBreak() { return bAnyRegBreak; }
bool anyLmSwitchError() { return bAnyLmSwitchError; }
bool anyConditions() { return bAnyConditions; }
GenCondSpace *findCondSpace( Key lowKey, Key highKey );
Condition *findCondition( Key key );
/* Is is it possible to extend a range by bumping ranges that span only
* one character to the singles array. */
bool canExtend( const RedTransList &list, int pos );
/* Pick single transitions from the ranges. */
void moveTransToSingle( RedState *state );
void chooseSingle();
void makeFlat();
/* Move a selected transition from ranges to default. */
void moveToDefault( RedTrans *defTrans, RedState *state );
/* Pick a default transition by largest span. */
RedTrans *chooseDefaultSpan( RedState *state );
void chooseDefaultSpan();
/* Pick a default transition by most number of ranges. */
RedTrans *chooseDefaultNumRanges( RedState *state );
void chooseDefaultNumRanges();
/* Pick a default transition tailored towards goto driven machine. */
RedTrans *chooseDefaultGoto( RedState *state );
void chooseDefaultGoto();
/* Ordering states by transition connections. */
void optimizeStateOrdering( RedState *state );
void optimizeStateOrdering();
/* Ordering states by transition connections. */
void depthFirstOrdering( RedState *state );
void depthFirstOrdering();
/* Set state ids. */
void sequentialStateIds();
void sortStateIdsByFinal();
/* Arrange states in by final id. This is a stable sort. */
void sortStatesByFinal();
/* Sorting states by id. */
void sortByStateId();
/* Locating the first final state. This is the final state with the lowest
* id. */
void findFirstFinState();
void assignActionLocs();
RedTrans *getErrorTrans();
RedState *getErrorState();
/* Is every char in the alphabet covered? */
bool alphabetCovered( RedTransList &outRange );
RedTrans *allocateTrans( RedState *targState, RedAction *actionTable );
void partitionFsm( int nParts );
void setInTrans();
void setValueLimits();
void assignActionIds();
void analyzeActionList( RedAction *redAct, InlineList *inlineList );
void analyzeAction( GenAction *act, InlineList *inlineList );
void findFinalActionRefs();
void analyzeMachine();
FsmTables *makeFsmTables();
};
#endif /* _REDFSM_H */
|