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
|
/*
* Copyright 2006 Adrian Thurston <thurston@cs.queensu.ca>
*/
/* This file is part of Ragel.
*
* Ragel 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.
*
* Ragel 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 Ragel; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "rlcodegen.h"
#include "splitcodegen.h"
#include "gendata.h"
#include <assert.h>
using std::ostream;
using std::ios;
using std::endl;
/* Emit the goto to take for a given transition. */
std::ostream &SplitCodeGen::TRANS_GOTO( RedTransAp *trans, int level )
{
if ( trans->targ->partition == currentPartition ) {
if ( trans->action != 0 ) {
/* Go to the transition which will go to the state. */
out << TABS(level) << "goto tr" << trans->id << ";";
}
else {
/* Go directly to the target state. */
out << TABS(level) << "goto st" << trans->targ->id << ";";
}
}
else {
if ( trans->action != 0 ) {
/* Go to the transition which will go to the state. */
out << TABS(level) << "goto ptr" << trans->id << ";";
trans->partitionBoundary = true;
}
else {
/* Go directly to the target state. */
out << TABS(level) << "goto pst" << trans->targ->id << ";";
trans->targ->partitionBoundary = true;
}
}
return out;
}
/* Called from before writing the gotos for each state. */
void SplitCodeGen::GOTO_HEADER( RedStateAp *state, bool stateInPartition )
{
bool anyWritten = IN_TRANS_ACTIONS( state );
if ( state->labelNeeded )
out << "st" << state->id << ":\n";
if ( state->toStateAction != 0 ) {
/* Remember that we wrote an action. Write every action in the list. */
anyWritten = true;
for ( ActionTable::Iter item = state->toStateAction->key; item.lte(); item++ )
ACTION( out, item->value, state->id, false );
}
/* Advance and test buffer pos. */
if ( state->labelNeeded ) {
if ( cgd->hasEnd ) {
out <<
" if ( ++" << P() << " == " << PE() << " )\n"
" goto _out" << state->id << ";\n";
}
else {
out <<
" " << P() << " += 1;\n";
}
}
/* Give the state a switch case. */
out << "case " << state->id << ":\n";
if ( state->fromStateAction != 0 ) {
/* Remember that we wrote an action. Write every action in the list. */
anyWritten = true;
for ( ActionTable::Iter item = state->fromStateAction->key; item.lte(); item++ )
ACTION( out, item->value, state->id, false );
}
if ( anyWritten )
genLineDirective( out );
/* Record the prev state if necessary. */
if ( state->anyRegCurStateRef() )
out << " _ps = " << state->id << ";\n";
}
std::ostream &SplitCodeGen::STATE_GOTOS( int partition )
{
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
if ( st->partition == partition ) {
if ( st == redFsm->errState )
STATE_GOTO_ERROR();
else {
/* We call into the base of the goto which calls back into us
* using virtual functions. Set the current partition rather
* than coding parameter passing throughout. */
currentPartition = partition;
/* Writing code above state gotos. */
GOTO_HEADER( st, st->partition == partition );
if ( st->stateCondVect.length() > 0 ) {
out << " _widec = " << GET_KEY() << ";\n";
emitCondBSearch( st, 1, 0, st->stateCondVect.length() - 1 );
}
/* Try singles. */
if ( st->outSingle.length() > 0 )
emitSingleSwitch( st );
/* Default case is to binary search for the ranges, if that fails then */
if ( st->outRange.length() > 0 )
emitRangeBSearch( st, 1, 0, st->outRange.length() - 1 );
/* Write the default transition. */
TRANS_GOTO( st->defTrans, 1 ) << "\n";
}
}
}
return out;
}
std::ostream &SplitCodeGen::PART_TRANS( int partition )
{
for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
if ( trans->partitionBoundary ) {
out <<
"ptr" << trans->id << ":\n";
if ( trans->action != 0 ) {
/* If the action contains a next, then we must preload the current
* state since the action may or may not set it. */
if ( trans->action->anyNextStmt() )
out << " " << CS() << " = " << trans->targ->id << ";\n";
/* Write each action in the list. */
for ( ActionTable::Iter item = trans->action->key; item.lte(); item++ )
ACTION( out, item->value, trans->targ->id, false );
}
out <<
" goto pst" << trans->targ->id << ";\n";
trans->targ->partitionBoundary = true;
}
}
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
if ( st->partitionBoundary ) {
out <<
" pst" << st->id << ":\n"
" " << CS() << " = " << st->id << ";\n";
if ( st->toStateAction != 0 ) {
/* Remember that we wrote an action. Write every action in the list. */
for ( ActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
ACTION( out, item->value, st->id, false );
genLineDirective( out );
}
ptOutLabelUsed = true;
out << " goto _pt_out; \n";
}
}
return out;
}
std::ostream &SplitCodeGen::EXIT_STATES( int partition )
{
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
if ( st->partition == partition && st->outNeeded ) {
outLabelUsed = true;
out << " _out" << st->id << ": " << CS() << " = " <<
st->id << "; goto _out; \n";
}
}
return out;
}
std::ostream &SplitCodeGen::PARTITION( int partition )
{
outLabelUsed = false;
ptOutLabelUsed = false;
/* Initialize the partition boundaries, which get set during the writing
* of states. After the state writing we will */
for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
trans->partitionBoundary = false;
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
st->partitionBoundary = false;
out << " " << ALPH_TYPE() << " *p = *_pp, *pe = *_ppe;\n";
if ( anyRegCurStateRef() )
out << " int _ps = 0;\n";
if ( anyConditions() )
out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
if ( useAgainLabel() ) {
out <<
" goto _resume;\n"
"\n"
"_again:\n"
" switch ( " << CS() << " ) {\n";
AGAIN_CASES() <<
" default: break;\n"
" }\n"
"\n";
if ( cgd->hasEnd ) {
outLabelUsed = true;
out <<
" if ( ++" << P() << " == " << PE() << " )\n"
" goto _out;\n";
}
else {
out <<
" " << P() << " += 1;\n";
}
out <<
"_resume:\n";
}
out <<
" switch ( " << CS() << " )\n {\n";
STATE_GOTOS( partition );
SWITCH_DEFAULT() <<
" }\n";
PART_TRANS( partition );
EXIT_STATES( partition );
if ( outLabelUsed ) {
out <<
"\n"
" _out:\n"
" *_pp = p;\n"
" *_ppe = pe;\n"
" return 0;\n";
}
if ( ptOutLabelUsed ) {
out <<
"\n"
" _pt_out:\n"
" *_pp = p;\n"
" *_ppe = pe;\n"
" return 1;\n";
}
return out;
}
std::ostream &SplitCodeGen::PART_MAP()
{
int *partMap = new int[redFsm->stateList.length()];
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
partMap[st->id] = st->partition;
out << "\t";
int totalItem = 0;
for ( int i = 0; i < redFsm->stateList.length(); i++ ) {
out << partMap[i];
if ( i != redFsm->stateList.length() - 1 ) {
out << ", ";
if ( ++totalItem % IALL == 0 )
out << "\n\t";
}
}
delete[] partMap;
return out;
}
void SplitCodeGen::writeOutData()
{
out <<
"static const int " << START() << " = " << START_STATE_ID() << ";\n"
"\n";
if ( cgd->writeFirstFinal ) {
out <<
"static const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
"\n";
}
if ( cgd->writeErr ) {
out <<
"static const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
"\n";
}
OPEN_ARRAY( ARRAY_TYPE(numSplitPartitions), PM() );
PART_MAP();
CLOSE_ARRAY() <<
"\n";
for ( int p = 0; p < redFsm->nParts; p++ ) {
out << "int partition" << p << "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
" **_ppe, struct " << FSM_NAME() << " *fsm );\n";
}
out << "\n";
}
std::ostream &SplitCodeGen::ALL_PARTITIONS()
{
/* compute the format string. */
int width = 0, high = redFsm->nParts - 1;
while ( high > 0 ) {
width++;
high /= 10;
}
assert( width <= 8 );
char suffFormat[] = "_%6.6d.c";
suffFormat[2] = suffFormat[4] = ( '0' + width );
for ( int p = 0; p < redFsm->nParts; p++ ) {
char suffix[10];
sprintf( suffix, suffFormat, p );
char *fn = fileNameFromStem( cgd->fileName, suffix );
char *include = fileNameFromStem( cgd->fileName, ".h" );
/* Create the filter on the output and open it. */
output_filter *partFilter = new output_filter;
partFilter->open( fn, ios::out|ios::trunc );
if ( !outFilter->is_open() ) {
error() << "error opening " << fn << " for writing" << endl;
exit(1);
}
/* Attach the new file to the output stream. */
std::streambuf *prev_rdbuf = out.rdbuf( partFilter );
out <<
"#include \"" << include << "\"\n"
"int partition" << p << "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
" **_ppe, struct " << FSM_NAME() << " *fsm )\n"
"{\n";
PARTITION( p ) <<
"}\n\n";
out.flush();
/* Fix the output stream. */
out.rdbuf( prev_rdbuf );
}
return out;
}
void SplitCodeGen::writeOutExec()
{
out <<
" {\n"
" int _stat = 0;\n";
if ( cgd->hasEnd ) {
out <<
" if ( " << P() << " == " << PE() << " )\n"
" goto _out;\n";
}
out << " goto _resume;\n";
/* In this reentry, to-state actions have already been executed on the
* partition-switch exit from the last partition. */
out << "_reenter:\n";
if ( cgd->hasEnd ) {
out <<
" if ( ++" << P() << " == " << PE() << " )\n"
" goto _out;\n";
}
else {
out <<
" " << P() << " += 1;\n";
}
out << "_resume:\n";
out <<
" switch ( " << PM() << "[" << CS() << "] ) {\n";
for ( int p = 0; p < redFsm->nParts; p++ ) {
out <<
" case " << p << ":\n"
" _stat = partition" << p << "( &p, &pe, fsm );\n"
" break;\n";
}
out <<
" }\n"
" if ( _stat )\n"
" goto _reenter;\n";
if ( cgd->hasEnd )
out << " _out: {}\n";
out <<
" }\n";
ALL_PARTITIONS();
}
void SplitCodeGen::setLabelsNeeded( RedStateAp *fromState, InlineList *inlineList )
{
for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
switch ( item->type ) {
case InlineItem::Goto: case InlineItem::Call: {
/* In split code gen we only need labels for transitions across
* partitions. */
if ( fromState->partition == item->targState->partition ){
/* Mark the target as needing a label. */
item->targState->labelNeeded = true;
}
break;
}
default: break;
}
if ( item->children != 0 )
setLabelsNeeded( fromState, item->children );
}
}
void SplitCodeGen::setLabelsNeeded( RedStateAp *fromState, RedTransAp *trans )
{
/* In the split code gen we don't need labels for transitions across
* partitions. */
if ( fromState->partition == trans->targ->partition ) {
/* If there is no action with a next statement, then the label will be
* needed. */
trans->labelNeeded = true;
if ( trans->action == 0 || !trans->action->anyNextStmt() )
trans->targ->labelNeeded = true;
}
/* Need labels for states that have goto or calls in action code
* invoked on characters (ie, not from out action code). */
if ( trans->action != 0 ) {
/* Loop the actions. */
for ( ActionTable::Iter act = trans->action->key; act.lte(); act++ ) {
/* Get the action and walk it's tree. */
setLabelsNeeded( fromState, act->value->inlineList );
}
}
}
/* Set up labelNeeded flag for each state. */
void SplitCodeGen::setLabelsNeeded()
{
/* If we use the _again label, then we the _again switch, which uses all
* labels. */
if ( useAgainLabel() ) {
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
st->labelNeeded = true;
}
else {
/* Do not use all labels by default, init all labelNeeded vars to false. */
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
st->labelNeeded = false;
for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
trans->labelNeeded = false;
if ( redFsm->errState != 0 && anyLmSwitchError() )
redFsm->errState->labelNeeded = true;
/* Walk all transitions and set only those that have targs. */
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
for ( RedTransList::Iter tel = st->outRange; tel.lte(); tel++ )
setLabelsNeeded( st, tel->value );
for ( RedTransList::Iter tel = st->outSingle; tel.lte(); tel++ )
setLabelsNeeded( st, tel->value );
if ( st->defTrans != 0 )
setLabelsNeeded( st, st->defTrans );
}
}
if ( cgd->hasEnd ) {
for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
st->outNeeded = st->labelNeeded;
}
else {
if ( redFsm->errState != 0 )
redFsm->errState->outNeeded = true;
for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
/* Any state with a transition in that has a break will need an
* out label. */
if ( trans->action != 0 && trans->action->anyBreakStmt() )
trans->targ->outNeeded = true;
}
}
}
|