summaryrefslogtreecommitdiff
path: root/src/libfsm/idbase.cc
blob: 38850b56bdb8e5fcb668fc2207fdf8cd3fde6a7a (plain)
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
/*
 * Copyright 2008-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 "ragel.h"
#include "fsmgraph.h"
#include "parsedata.h"
#include "action.h"

/* Error reporting format. */
ErrorFormat errorFormat = ErrorFormatGNU;

void FsmCtx::finalizeInstance( FsmAp *graph )
{
	/* Resolve any labels that point to multiple states. Any labels that are
	 * still around are referenced only by gotos and calls and they need to be
	 * made into deterministic entry points. */
	graph->deterministicEntry();

	/*
	 * All state construction is now complete.
	 */

	/* Transfer actions from the out action tables to eof action tables. */
	for ( StateSet::Iter state = graph->finStateSet; state.lte(); state++ )
		graph->transferOutActions( *state );

	/* Transfer global error actions. */
	for ( StateList::Iter state = graph->stateList; state.lte(); state++ )
		graph->transferErrorActions( state, 0 );
	
	if ( fsmGbl->wantDupsRemoved )
		graph->removeActionDups();

	/* Remove unreachable states. There should be no dead end states. The
	 * subtract and intersection operators are the only places where they may
	 * be created and those operators clean them up. */
	graph->removeUnreachableStates();

	/* No more fsm operations are to be done. Action ordering numbers are
	 * no longer of use and will just hinder minimization. Clear them. */
	graph->nullActionKeys();

	/* Transition priorities are no longer of use. We can clear them
	 * because they will just hinder minimization as well. Clear them. */
	graph->clearAllPriorities();

	if ( graph->ctx->minimizeOpt != MinimizeNone ) {
		/* Minimize here even if we minimized at every op. Now that function
		 * keys have been cleared we may get a more minimal fsm. */
		switch ( graph->ctx->minimizeLevel ) {
			#ifdef TO_UPGRADE_CONDS
			case MinimizeApprox:
				graph->minimizeApproximate();
				break;
			#endif
			#ifdef TO_UPGRADE_CONDS
			case MinimizeStable:
				graph->minimizeStable();
				break;
			#endif
			case MinimizePartition1:
				graph->minimizePartition1();
				break;
			case MinimizePartition2:
				graph->minimizePartition2();
				break;
		}
	}

	graph->compressTransitions();

	createNfaActions( graph );
}

void FsmCtx::analyzeAction( Action *action, InlineList *inlineList )
{
	/* FIXME: Actions used as conditions should be very constrained. */
	for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
		if ( item->type == InlineItem::Call || item->type == InlineItem::CallExpr ||
				item->type == InlineItem::Ncall || item->type == InlineItem::NcallExpr )
		{
			action->anyCall = true;
		}

		/* Need to recurse into longest match items. */
		if ( item->type == InlineItem::LmSwitch ) {
			FsmLongestMatch *lm = item->longestMatch;
			for ( FsmLmPartList::Iter lmi = *lm->longestMatchList; lmi.lte(); lmi++ ) {
				if ( lmi->action != 0 )
					analyzeAction( action, lmi->action->inlineList );
			}
		}

		if ( item->type == InlineItem::LmOnLast || 
				item->type == InlineItem::LmOnNext ||
				item->type == InlineItem::LmOnLagBehind )
		{
			FsmLongestMatchPart *lmi = item->longestMatchPart;
			if ( lmi->action != 0 )
				analyzeAction( action, lmi->action->inlineList );
		}

		if ( item->children != 0 )
			analyzeAction( action, item->children );
	}
}

/* Check actions for bad uses of fsm directives. We don't go inside longest
 * match items in actions created by ragel, since we just want the user
 * actions. */
void FsmCtx::checkInlineList( Action *act, InlineList *inlineList )
{
	for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
		/* EOF checks. */
		if ( act->numEofRefs > 0 ) {
			switch ( item->type ) {
				/* Currently no checks. */
				default:
					break;
			}
		}

		/* Recurse. */
		if ( item->children != 0 )
			checkInlineList( act, item->children );
	}
}

void FsmCtx::checkAction( Action *action )
{
	/* Check for actions with calls that are embedded within a longest match
	 * machine. */
	if ( !action->isLmAction && action->numRefs() > 0 && action->anyCall ) {
		for ( NameInstVect::Iter ar = action->embedRoots; ar.lte(); ar++ ) {
			NameInst *check = *ar;
			while ( check != 0 ) {
				if ( check->isLongestMatch ) {
					fsmGbl->error(action->loc) << "within a scanner, fcall and fncall are permitted"
						" only in pattern actions" << endl;
					break;
				}
				check = check->parent;
			}
		}
	}

	checkInlineList( action, action->inlineList );
}

void FsmCtx::analyzeGraph( FsmAp *graph )
{
	for ( ActionList::Iter act = actionList; act.lte(); act++ )
		analyzeAction( act, act->inlineList );

	for ( StateList::Iter st = graph->stateList; st.lte(); st++ ) {
		/* The transition list. */
		for ( TransList::Iter trans = st->outList; trans.lte(); trans++ ) {
			//if ( trans->condSpace != 0 ) {
			//	for ( CondSet::Iter sci = trans->condSpace->condSet; sci.lte(); sci++ )
			//		(*sci)->numCondRefs += 1;
			//}

			if ( trans->plain() ) {
				for ( ActionTable::Iter at = trans->tdap()->actionTable; at.lte(); at++ )
					at->value->numTransRefs += 1;
			}
			else {
				for ( CondList::Iter cond = trans->tcap()->condList; cond.lte(); cond++ ) { 
					for ( ActionTable::Iter at = cond->actionTable; at.lte(); at++ )
						at->value->numTransRefs += 1;
				}
			}
		}

		for ( ActionTable::Iter at = st->toStateActionTable; at.lte(); at++ )
			at->value->numToStateRefs += 1;

		for ( ActionTable::Iter at = st->fromStateActionTable; at.lte(); at++ )
			at->value->numFromStateRefs += 1;

		for ( ActionTable::Iter at = st->eofActionTable; at.lte(); at++ )
			at->value->numEofRefs += 1;

		//for ( OutCondSet::Iter oci = st->outCondSet; oci.lte(); oci++ )
		//	oci->action->numCondRefs += 1;

		if ( st->nfaOut != 0 ) {
			for ( NfaTransList::Iter n = *st->nfaOut; n.lte(); n++ ) {
				for ( ActionTable::Iter ati = n->pushTable; ati.lte(); ati++ )
					ati->value->numNfaRefs += 1;

				for ( ActionTable::Iter ati = n->restoreTable; ati.lte(); ati++ )
					ati->value->numNfaRefs += 1;

				for ( ActionTable::Iter ati = n->popAction; ati.lte(); ati++ )
					ati->value->numNfaRefs += 1;

				for ( ActionTable::Iter ati = n->popTest; ati.lte(); ati++ )
					ati->value->numNfaRefs += 1;
			}
		}
	}

	/* Can't count on cond references in transitions, since we don't refcount
	 * the spaces. FIXME: That would be the proper solution. */
	for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
		for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
			(*csi)->numCondRefs += 1;
	}

	/* Checks for bad usage of directives in action code. */
	for ( ActionList::Iter act = actionList; act.lte(); act++ )
		checkAction( act );
}

/* This create an action that refs the original embed roots, if the optWrap arg
 * is supplied. */
Action *FsmCtx::newNfaWrapAction( const char *name, InlineList *inlineList, Action *optWrap )
{
	InputLoc loc;
	loc.line = 1;
	loc.col = 1;
	loc.fileName = "NONE";

	Action *action = new Action( loc, name, inlineList, nextCondId++ );

	if ( optWrap != 0 )
		action->embedRoots.append( optWrap->embedRoots );

	actionList.append( action );
	return action;
}

void FsmCtx::createNfaActions( FsmAp *fsm )
{
	for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
		if ( st->nfaOut != 0 ) {
			for ( NfaTransList::Iter n = *st->nfaOut; n.lte(); n++ ) {
				/* Move pop restore actions into poptest. Wrap to override the
				 * condition-like testing. */
				for ( ActionTable::Iter ati = n->restoreTable; ati.lte(); ati++ ) {
					n->popTest.setAction( ati->key, ati->value );
				}

				/* Move pop actions into pop test. Wrap to override the
				 * condition-like testing. */
				for ( ActionTable::Iter ati = n->popFrom; ati.lte(); ati++ ) {

					InlineList *il1 = new InlineList;
					il1->append( new InlineItem( InputLoc(),
								ati->value, InlineItem::NfaWrapAction ) );
					Action *wrap = newNfaWrapAction( "action_wrap", il1, ati->value );
					n->popTest.setAction( ORD_COND2, wrap );
				}

				/* Move condition evaluation into pop test. Wrap with condition
				 * execution. */
				if ( n->popCondSpace != 0 ) {
					InlineList *il1 = new InlineList;
					il1->append( new InlineItem( InputLoc(),
							n->popCondSpace, n->popCondKeys,
							InlineItem::NfaWrapConds ) );
					Action *wrap = newNfaWrapAction( "cond_wrap", il1, 0 );
					n->popTest.setAction( ORD_COND, wrap );
				}

				/* Move pop actions into pop test. Wrap to override the
				 * condition-like testing. */
				for ( ActionTable::Iter ati = n->popAction; ati.lte(); ati++ ) {

					InlineList *il1 = new InlineList;
					il1->append( new InlineItem( InputLoc(),
								ati->value, InlineItem::NfaWrapAction ) );
					Action *wrap = newNfaWrapAction( "action_wrap", il1, ati->value );
					n->popTest.setAction( ati->key, wrap );
				}
			}
		}
	}
}

void FsmCtx::prepareReduction( FsmAp *sectionGraph )
{
	/* Decide if an error state is necessary.
	 *  1. There is an error transition
	 *  2. There is a gap in the transitions
	 *  3. The longest match operator requires it. */
	if ( lmRequiresErrorState || sectionGraph->hasErrorTrans() )
		sectionGraph->errState = sectionGraph->addState();

	/* State numbers need to be assigned such that all final states have a
	 * larger state id number than all non-final states. This enables the
	 * first_final mechanism to function correctly. We also want states to be
	 * ordered in a predictable fashion. So we first apply a depth-first
	 * search, then do a stable sort by final state status, then assign
	 * numbers. */

	sectionGraph->depthFirstOrdering();
	sectionGraph->sortStatesByFinal();
	sectionGraph->setStateNumbers( 0 );
}


void translatedHostData( ostream &out, const std::string &data )
{
	const char *p = data.c_str();
	for ( const char *c = p; *c != 0; ) {
		if ( c[0] == '}' && ( c[1] == '@' || c[1] == '$' || c[1] == '=' ) ) {
			out << "@}@" << c[1];
			c += 2;
		}
		else if ( c[0] == '@' ) {
			out << "@@";
			c += 1;
		}
		// Have some escaping issues that these fix, but they lead to other problems.
		// Can be reproduced by passing "={}" through ragel and adding --colm-backend
		// else if ( c[0] == '=' ) {
		//	out << "@=";
		//	c += 1;
		//}
		// else if ( c[0] == '$' ) {
		//	out << "@$";
		//	c += 1;
		//}
		else {
			out << c[0];
			c += 1;
		}
	}
}


void FsmGbl::abortCompile( int code )
{
	throw AbortCompile( code );
}

/* Print the opening to a warning in the input, then return the error ostream. */
ostream &FsmGbl::warning( const InputLoc &loc )
{
	ostream &err = std::cerr;
	err << loc << ": warning: ";
	return err;
}

/* Print the opening to a program error, then return the error stream. */
ostream &FsmGbl::error()
{
	errorCount += 1;
	ostream &err = std::cerr;
	err << PROGNAME ": ";
	return err;
}

ostream &FsmGbl::error( const InputLoc &loc )
{
	errorCount += 1;
	ostream &err = std::cerr;
	err << loc << ": ";
	return err;
}

ostream &FsmGbl::error_plain()
{
	errorCount += 1;
	ostream &err = std::cerr;
	return err;
}


std::ostream &FsmGbl::stats()
{
	return std::cout;
}

/* Requested info. */
std::ostream &FsmGbl::info()
{
	return std::cout;
}

ostream &operator<<( ostream &out, const InputLoc &loc )
{
	assert( loc.fileName != 0 );
	switch ( errorFormat ) {
	case ErrorFormatMSVC:
		out << loc.fileName << "(" << loc.line;
		if ( loc.col )
			out << "," << loc.col;
		out << ")";
		break;

	default:
		out << loc.fileName << ":" << loc.line;
		if ( loc.col )
			out << ":" << loc.col;
		break;
	}
	return out;
}