summaryrefslogtreecommitdiff
path: root/libfsm/codegen.h
blob: dcc24e3bf87bd8e06227634df346b2340994244a (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
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
/*
 * Copyright 2001-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.
 */

#ifndef _C_CODEGEN_H
#define _C_CODEGEN_H

#include <iostream>
#include <string>
#include <stdio.h>
#include "common.h"
#include "gendata.h"
#include "vector.h"
#include "idbase.h"

using std::string;
using std::ostream;

/* Integer array line length. */
//#define IALL 8

#define IALL_INTEGRAL 8
#define IALL_STRING 128


/* Forwards. */
struct RedFsmAp;
struct RedStateAp;
struct CodeGenData;
struct GenAction;
struct NameInst;
struct GenInlineItem;
struct GenInlineList;
struct RedAction;
struct FsmLongestMatch;
struct LongestMatchPart;

string itoa( int i );

struct Variable
{
	Variable( const char *name ) : name(name), isReferenced(false) {}

	const std::string ref() { isReferenced = true; return name; }

	const char *name;
	bool isReferenced;
};

struct GotoLabel
{
	GotoLabel( const char *name ) : name(name), isReferenced(false) {}

	const std::string ref() { isReferenced = true; return name; }

	const char *name;
	bool isReferenced;
};

std::ostream &operator<<( std::ostream &out, GotoLabel &l );
std::ostream &operator<<( std::ostream &out, Variable &v );

struct TableArray;
typedef Vector<TableArray*> ArrayVector;
class CodeGen;

struct TableArray
{
	enum State {
		InitialState = 1,
		AnalyzePass,
		GeneratePass
	};

	TableArray( const char *name, CodeGen &codeGen );

	void start();
	void startAnalyze();
	void startGenerate();

	void setType( std::string type, int width, bool isChar )
	{
		this->type = type;
		this->width = width;
		this->isChar = isChar;
	}

	std::string ref();

	void value( long long v );

	void valueAnalyze( long long v );
	void valueGenerate( long long v );
	void stringGenerate( long long value );

	void finish();
	void finishAnalyze();
	void finishGenerate();

	void setState( TableArray::State state )
		{ this->state = state; }

	long long size();

	State state;
	const char *name;
	std::string type;
	int width;
	bool isSigned;
	bool isChar;
	bool stringTables;
	int iall;
	long long values;
	long long min;
	long long max;
	CodeGen &codeGen;
	std::ostream &out;
	int ln;
	bool isReferenced;
	bool started;
};

struct IlOpts
{
	IlOpts( int targState, bool inFinish, bool csForced )
		: targState(targState), inFinish(inFinish), csForced(csForced) {}

	int targState;
	bool inFinish;
	bool csForced;
};


/*
 * class CodeGen
 */
class CodeGen : public CodeGenData
{
public:
	CodeGen( const CodeGenArgs &args );

	virtual ~CodeGen() {}

	virtual void writeInit();
	virtual void writeStart();
	virtual void writeFirstFinal();
	virtual void writeError();
	virtual void statsSummary();

protected:
	friend struct TableArray;
	typedef Vector<TableArray*> ArrayVector;
	ArrayVector arrayVector;

	Variable cpc;
	Variable pop_test;
	Variable new_recs;
	Variable alt;

	string FSM_NAME();
	string START_STATE_ID();
	void taActions();
	string KEY( Key key );
	string LDIR_PATH( char *path );

	void ACTION( ostream &ret, GenAction *action, IlOpts opts );
	void NFA_CONDITION( ostream &ret, GenAction *condition, bool last );
	void NFA_POP_TEST_EXEC();
	void CONDITION( ostream &ret, GenAction *condition );
	string ALPH_TYPE();

	bool isAlphTypeSigned();
	long long tableData;
	RagelBackend backend;
	bool stringTables;
	BackendFeature backendFeature;

	TableArray nfaTargs;
	TableArray nfaOffsets;
	TableArray nfaPushActions;
	TableArray nfaPopTrans;

	virtual string GET_KEY();

	string P();
	string PE();
	string vEOF();

	string ACCESS();
	string vCS();
	string STACK();
	string TOP();
	string TOKSTART();
	string TOKEND();
	string ACT();

	string DATA_PREFIX();
	string START() { return DATA_PREFIX() + "start"; }
	string ERROR() { return DATA_PREFIX() + "error"; }
	string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }

	/* Declare a variable only if referenced. */
	void DECLARE( std::string type, Variable &var, std::string init = "" );

	string CAST( string type );

	string ARR_TYPE( const TableArray &ta )
		{ return ta.type; }

	string ARR_REF( TableArray &ta )
		{ return ta.ref(); }

	void INLINE_EXPR( ostream &ret, GenInlineList *inlineList );
	void INLINE_BLOCK( ostream &ret, GenInlineExpr *inlineExpr );
	void INLINE_PLAIN( ostream &ret, GenInlineExpr *inlineExpr );

	void INLINE_LIST( ostream &ret, GenInlineList *inlineList,
			int targState, bool inFinish, bool csForced );
	virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
	virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
	virtual void NCALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
	virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
	virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
	virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
	virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem,
			int targState, bool inFinish ) = 0;
	virtual void NCALL_EXPR( ostream &ret, GenInlineItem *ilItem,
			int targState, bool inFinish ) = 0;
	virtual void RET( ostream &ret, bool inFinish ) = 0;
	virtual void NRET( ostream &ret, bool inFinish ) = 0;
	virtual void BREAK( ostream &ret, int targState, bool csForced ) = 0;
	virtual void NBREAK( ostream &ret, int targState, bool csForced ) = 0;
	virtual void CURS( ostream &ret, bool inFinish ) = 0;
	virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
	void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
	void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState,
			int inFinish, bool csForced );
	void LM_EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
	void SET_ACT( ostream &ret, GenInlineItem *item );
	void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
	void INIT_ACT( ostream &ret, GenInlineItem *item );
	void SET_TOKSTART( ostream &ret, GenInlineItem *item );
	void SET_TOKEND( ostream &ret, GenInlineItem *item );
	void GET_TOKEND( ostream &ret, GenInlineItem *item );

	void HOST_STMT( ostream &ret, GenInlineItem *item, int targState, bool inFinish, bool csForced );
	void HOST_EXPR( ostream &ret, GenInlineItem *item, int targState, bool inFinish, bool csForced );
	void HOST_TEXT( ostream &ret, GenInlineItem *item, int targState, bool inFinish, bool csForced );
	void GEN_STMT( ostream &ret, GenInlineItem *item, int targState, bool inFinish, bool csForced );
	void GEN_EXPR( ostream &ret, GenInlineItem *item, int targState, bool inFinish, bool csForced );

	void STATE_IDS();

	string ERROR_STATE();
	string FIRST_FINAL_STATE();

	string STR( int v );

	void VALUE( string type, string name, string value );

	string ACCESS_OPER()
		{ return backend == Direct ? "" : " -> "; }

	string OPEN_HOST_EXPR()
		{ return backend == Direct ? "(" : "host( \"-\", 1 ) ={"; }

	string OPEN_HOST_EXPR( string fileName, int line )
	{
		return backend == Direct ? "(" : "host( \"" + fileName + "\", " + STR(line) + " ) ={";
	}

	string CLOSE_HOST_EXPR()
		{ return backend == Direct ? ")" : "}="; }

	string OPEN_HOST_BLOCK( string fileName, int line )
	{
		if ( backend == Direct ) {
			std::stringstream ss;
			ss << "{\n" ;
			(*genLineDirective)( ss, lineDirectives, line, fileName.c_str() );
			return ss.str();
		}
		else {
			return "host( \"" + fileName + "\", " + STR(line) + " ) ${";
		}
	}

	string OPEN_HOST_BLOCK( GenInlineExpr *inlineExpr )
	{
		return OPEN_HOST_BLOCK( inlineExpr->loc.fileName, inlineExpr->loc.line );
	}

	string CLOSE_HOST_BLOCK()
		{ return backend == Direct ? "}\n" : "}$"; }

	string OPEN_HOST_PLAIN()
		{ return backend == Direct ? "" : "host( \"-\", 1 ) @{"; }

	string CLOSE_HOST_PLAIN()
		{ return backend == Direct ? "" : "}@"; }

	string OPEN_GEN_EXPR()
		{ return backend == Direct ? "(" : "={"; }

	string CLOSE_GEN_EXPR()
		{ return backend == Direct ? ")" : "}="; }

	string OPEN_GEN_BLOCK()
		{ return backend == Direct ? "{" : "${"; }

	string CLOSE_GEN_BLOCK()
		{ return backend == Direct ? "}" : "}$"; }

	string OPEN_GEN_PLAIN()
		{ return backend == Direct ? "" : "@{"; }

	string CLOSE_GEN_PLAIN()
		{ return backend == Direct ? "" : "}@"; }

	string INT()
		{ return "int"; }

	string UINT()
		{ return backend == Direct ? "unsigned int" : "uint"; }

	string INDEX( string type, string name )
	{
		if ( backend == Direct )
			return "const " + type + " *" + name;
		else
			return "index " + type + " " + name;
	}

	string INDEX( string type )
	{
		if ( backend == Direct )
			return "const " + type + " *";
		else
			return "index " + type + " ";
	}

	string LABEL( string name )
	{
		return name + ": ";
	}

	string EMIT_LABEL( GotoLabel label )
	{
		if ( label.isReferenced )
			return std::string(label.name) + ": {}\n";
		else
			return "";
	}

	string OFFSET( string arr, string off )
	{
		if ( backend == Direct )
			return "( " + arr + " + (" + off + "))";
		else
			return "offset( " + arr + ", " + off + " )";
	}

	string TRUE()
	{
		if ( backend == Direct )
			return "1";
		else
			return "TRUE";
	}

	string DEREF( string arr, string off )
	{
		if ( backend == Direct )
			return "(*( " + off + "))";
		else
			return "deref( " + arr + ", " + off + " )";
	}

	string CASE( string val )
	{
		if ( backend == Direct )
			return "case " + val + ": ";
		else
			return "case " + val;
	}

	string DEFAULT()
	{
		if ( backend == Direct )
			return "default:";
		else
			return "default";
	}

	string CEND( )
	{
		if ( backend == Direct )
			return " break; ";
		else
			return " ";
	}

	string FALLTHROUGH()
	{
		if ( backend == Direct )
			return " ";
		else
			return "fallthrough;";
	}

	string NIL()
	{
		if ( backend == Direct )
			return "0";
		else
			return "nil";
	}

	string EXPORT( string type, string name, string value )
	{
		if ( backend == Direct )
			return "#define " + name + " " + value;
		else
			return "export " + type + " " + name + " " + value + ";";
	}

	void NFA_POST_POP();
	virtual void NFA_PUSH( std::string );
	virtual void NFA_POP() = 0;
	virtual void LOCATE_TRANS() {}
	virtual void LOCATE_COND() {}
	virtual void EOF_TRANS() {}


	virtual void COND_EXEC( std::string expr ) {}
	virtual void COND_BIN_SEARCH( Variable &var, TableArray &keys, std::string ok, std::string error ) {}

public:
	virtual void writeExports();
};

#endif