diff --git ragel/Makefile.am ragel/Makefile.am index 5031c66..6582a52 100644 --- ragel/Makefile.am +++ ragel/Makefile.am @@ -13,6 +13,7 @@ ragel_SOURCES = \ dotcodegen.h parsetree.h rlscan.h version.h cdflat.h common.h \ csftable.h fsmgraph.h pcheck.h rubycodegen.h xmlcodegen.h cdftable.h \ csgoto.h gendata.h ragel.h rubyfflat.h goipgoto.h \ + mlcodegen.h mltable.h mlftable.h mlflat.h mlfflat.h mlgoto.h \ main.cpp parsetree.cpp parsedata.cpp fsmstate.cpp fsmbase.cpp \ fsmattach.cpp fsmmin.cpp fsmgraph.cpp fsmap.cpp rlscan.cpp rlparse.cpp \ inputdata.cpp common.cpp redfsm.cpp gendata.cpp cdcodegen.cpp \ @@ -20,7 +21,8 @@ ragel_SOURCES = \ cdipgoto.cpp cdsplit.cpp javacodegen.cpp rubycodegen.cpp rubytable.cpp \ rubyftable.cpp rubyflat.cpp rubyfflat.cpp rbxgoto.cpp cscodegen.cpp \ cstable.cpp csftable.cpp csflat.cpp csfflat.cpp csgoto.cpp csfgoto.cpp \ - csipgoto.cpp cssplit.cpp dotcodegen.cpp xmlcodegen.cpp goipgoto.cpp + csipgoto.cpp cssplit.cpp dotcodegen.cpp xmlcodegen.cpp goipgoto.cpp \ + mlcodegen.cpp mltable.cpp mlftable.cpp mlflat.cpp mlfflat.cpp mlgoto.cpp BUILT_SOURCES = \ rlscan.cpp rlparse.h rlparse.cpp version.h diff --git ragel/common.cpp ragel/common.cpp index 2a45bd6..9a01624 100644 --- ragel/common.cpp +++ ragel/common.cpp @@ -49,6 +49,9 @@ HostType hostTypesC[] = #define U16BIT_MIN 0 #define U16BIT_MAX 65535 +#define S31BIT_MIN -1073741824l +#define S31BIT_MAX 1073741823l + #define S32BIT_MIN –2147483648l #define S32BIT_MAX 2147483647l @@ -114,12 +117,19 @@ HostType hostTypesCSharp[] = { "ulong", 0, "ulong", false, 0, ULONG_MAX, 8 } }; +HostType hostTypesOCaml[] = +{ +// { "char", 0, "char", false, 0, UCHAR_MAX, 1 }, + { "int", 0, "int", true, S31BIT_MIN, S31BIT_MAX, 4 }, +}; + HostLang hostLangC = { HostLang::C, hostTypesC, 8, hostTypesC+0, true }; HostLang hostLangD = { HostLang::D, hostTypesD, 9, hostTypesD+2, true }; HostLang hostLangGo = { HostLang::Go, hostTypesGo, 7, hostTypesGo+0, false }; HostLang hostLangJava = { HostLang::Java, hostTypesJava, 4, hostTypesJava+2, false }; HostLang hostLangRuby = { HostLang::Ruby, hostTypesRuby, 2, hostTypesRuby+0, false }; HostLang hostLangCSharp = { HostLang::CSharp, hostTypesCSharp, 9, hostTypesCSharp+4, true }; +HostLang hostLangOCaml = { HostLang::OCaml, hostTypesOCaml, 1, hostTypesOCaml+0, false }; HostLang *hostLang = &hostLangC; diff --git ragel/common.h ragel/common.h index c31997b..2f5fe02 100644 --- ragel/common.h +++ ragel/common.h @@ -124,7 +124,7 @@ struct HostLang /* Target language. */ enum Lang { - C, D, Go, Java, Ruby, CSharp + C, D, Go, Java, Ruby, CSharp, OCaml }; Lang lang; @@ -142,6 +142,7 @@ extern HostLang hostLangGo; extern HostLang hostLangJava; extern HostLang hostLangRuby; extern HostLang hostLangCSharp; +extern HostLang hostLangOCaml; HostType *findAlphType( const char *s1 ); HostType *findAlphType( const char *s1, const char *s2 ); diff --git ragel/gendata.cpp ragel/gendata.cpp index 5e7ddfc..4c036a9 100644 --- ragel/gendata.cpp +++ ragel/gendata.cpp @@ -51,6 +51,12 @@ #include "goipgoto.h" +#include "mltable.h" +#include "mlftable.h" +#include "mlflat.h" +#include "mlfflat.h" +#include "mlgoto.h" + #include "rubytable.h" #include "rubyftable.h" #include "rubyflat.h" @@ -265,6 +271,38 @@ CodeGenData *csharpMakeCodeGen( const char *sourceFileName, const char *fsmName, return codeGen; } +/* Invoked by the parser when a ragel definition is opened. */ +CodeGenData *ocamlMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out ) +{ + CodeGenData *codeGen = 0; + + switch ( codeStyle ) { + case GenTables: + codeGen = new OCamlTabCodeGen(out); + break; + case GenFTables: + codeGen = new OCamlFTabCodeGen(out); + break; + case GenFlat: + codeGen = new OCamlFlatCodeGen(out); + break; + case GenFFlat: + codeGen = new OCamlFFlatCodeGen(out); + break; + case GenGoto: + codeGen = new OCamlGotoCodeGen(out); + break; + default: + cerr << "I only support the -T0 -T1 -F0 -F1 and -G0 output styles for OCaml.\n"; + exit(1); + } + + codeGen->sourceFileName = sourceFileName; + codeGen->fsmName = fsmName; + + return codeGen; +} + CodeGenData *makeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out ) { @@ -283,6 +321,8 @@ CodeGenData *makeCodeGen( const char *sourceFileName, const char *fsmName, ostre cgd = rubyMakeCodeGen( sourceFileName, fsmName, out ); else if ( hostLang == &hostLangCSharp ) cgd = csharpMakeCodeGen( sourceFileName, fsmName, out ); + else if ( hostLang == &hostLangOCaml ) + cgd = ocamlMakeCodeGen( sourceFileName, fsmName, out ); return cgd; } @@ -301,6 +341,8 @@ void lineDirective( ostream &out, const char *fileName, int line ) rubyLineDirective( out, fileName, line ); else if ( hostLang == &hostLangCSharp ) csharpLineDirective( out, fileName, line ); + else if ( hostLang == &hostLangOCaml ) + ocamlLineDirective( out, fileName, line ); } } diff --git ragel/gendata.h ragel/gendata.h index 20c38e8..07eab32 100644 --- ragel/gendata.h +++ ragel/gendata.h @@ -48,6 +48,7 @@ void javaLineDirective( ostream &out, const char *fileName, int line ); void gothicLineDirective( ostream &out, const char *fileName, int line ); void rubyLineDirective( ostream &out, const char *fileName, int line ); void csharpLineDirective( ostream &out, const char *fileName, int line ); +void ocamlLineDirective( ostream &out, const char *fileName, int line ); void genLineDirective( ostream &out ); void lineDirective( ostream &out, const char *fileName, int line ); diff --git ragel/inputdata.cpp ragel/inputdata.cpp index aa6297d..6c30006 100644 --- ragel/inputdata.cpp +++ ragel/inputdata.cpp @@ -94,6 +94,15 @@ void InputData::csharpDefaultFileName( const char *inputFile ) } } +/* Invoked by the parser when the root element is opened. */ +void InputData::ocamlDefaultFileName( const char *inputFile ) +{ + /* If the output format is code and no output file name is given, then + * make a default. */ + if ( outputFileName == 0 ) + outputFileName = fileNameFromStem( inputFile, ".ml" ); +} + void InputData::makeOutputStream() { if ( ! generateDot && ! generateXML ) { @@ -114,6 +123,9 @@ void InputData::makeOutputStream() case HostLang::CSharp: csharpDefaultFileName( inputFileName ); break; + case HostLang::OCaml: + ocamlDefaultFileName( inputFileName ); + break; } } diff --git ragel/inputdata.h ragel/inputdata.h index 4f50c60..09a62c1 100644 --- ragel/inputdata.h +++ ragel/inputdata.h @@ -97,6 +97,7 @@ struct InputData void javaDefaultFileName( const char *inputFile ); void rubyDefaultFileName( const char *inputFile ); void csharpDefaultFileName( const char *inputFile ); + void ocamlDefaultFileName( const char *inputFile ); void writeLanguage( std::ostream &out ); void writeXML( std::ostream &out ); diff --git ragel/main.cpp ragel/main.cpp index dccfe6b..cdada44 100644 --- ragel/main.cpp +++ ragel/main.cpp @@ -125,6 +125,7 @@ void usage() " -J The host language is Java\n" " -R The host language is Ruby\n" " -A The host language is C#\n" +" -O The host language is OCaml\n" "line directives: (C/D/Ruby/C#)\n" " -L Inhibit writing of #line directives\n" "code style: (C/D/Java/Ruby/C#)\n" @@ -218,7 +219,7 @@ void escapeLineDirectivePath( std::ostream &out, char *path ) void processArgs( int argc, const char **argv, InputData &id ) { - ParamCheck pc("xo:dnmleabjkS:M:I:CDJZRAvHh?-:sT:F:G:P:LpV", argc, argv); + ParamCheck pc("xo:dnmleabjkS:M:I:CDJZRAOvHh?-:sT:F:G:P:LpV", argc, argv); /* FIXME: Need to check code styles VS langauge. */ @@ -328,6 +329,9 @@ void processArgs( int argc, const char **argv, InputData &id ) case 'A': hostLang = &hostLangCSharp; break; + case 'O': + hostLang = &hostLangOCaml; + break; /* Version and help. */ case 'v': diff --git ragel/mlcodegen.cpp ragel/mlcodegen.cpp new file mode 100644 index 0000000..429c97c --- /dev/null +++ ragel/mlcodegen.cpp @@ -0,0 +1,737 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 "ragel.h" +#include "mlcodegen.h" +#include "redfsm.h" +#include "gendata.h" +#include +#include +#include +#include + +using std::ostream; +using std::ostringstream; +using std::string; +using std::cerr; +using std::endl; + +using std::istream; +using std::ifstream; +using std::ostream; +using std::ios; +using std::cin; +using std::cout; +using std::cerr; +using std::endl; + +void ocamlLineDirective( ostream &out, const char *fileName, int line ) +{ + if ( noLineDirectives ) + return; + + /* Write the line info for to the input file. */ + out << "# " << line << " \""; + for ( const char *pc = fileName; *pc != 0; pc++ ) { + if ( *pc == '\\' || *pc == '"' ) + out << "\\"; + out << *pc; + } + out << "\"\n"; +} + +void OCamlCodeGen::genLineDirective( ostream &out ) +{ + std::streambuf *sbuf = out.rdbuf(); + output_filter *filter = static_cast(sbuf); + ocamlLineDirective( out, filter->fileName, filter->line + 1 ); +} + + +/* Init code gen with in parameters. */ +OCamlCodeGen::OCamlCodeGen( ostream &out ) +: + CodeGenData(out) +{ +} + +unsigned int OCamlCodeGen::arrayTypeSize( unsigned long maxVal ) +{ + long long maxValLL = (long long) maxVal; + HostType *arrayType = keyOps->typeSubsumes( maxValLL ); + assert( arrayType != 0 ); + return arrayType->size; +} + +string OCamlCodeGen::ARRAY_TYPE( unsigned long maxVal ) +{ + return ARRAY_TYPE( maxVal, false ); +} + +string OCamlCodeGen::ARRAY_TYPE( unsigned long maxVal, bool forceSigned ) +{ + long long maxValLL = (long long) maxVal; + HostType *arrayType; + if (forceSigned) + arrayType = keyOps->typeSubsumes(true, maxValLL); + else + arrayType = keyOps->typeSubsumes( maxValLL ); + assert( arrayType != 0 ); + + string ret = arrayType->data1; + if ( arrayType->data2 != 0 ) { + ret += " "; + ret += arrayType->data2; + } + return ret; +} + +/* Write out the fsm name. */ +string OCamlCodeGen::FSM_NAME() +{ + return fsmName; +} + +/* Emit the offset of the start state as a decimal integer. */ +string OCamlCodeGen::START_STATE_ID() +{ + ostringstream ret; + ret << redFsm->startState->id; + return ret.str(); +}; + +/* Write out the array of actions. */ +std::ostream &OCamlCodeGen::ACTIONS_ARRAY() +{ + out << "\t0; "; + int totalActions = 1; + for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) { + /* Write out the length, which will never be the last character. */ + out << act->key.length() << ARR_SEP(); + /* Put in a line break every 8 */ + if ( totalActions++ % 8 == 7 ) + out << "\n\t"; + + for ( GenActionTable::Iter item = act->key; item.lte(); item++ ) { + out << item->value->actionId; + if ( ! (act.last() && item.last()) ) + out << ARR_SEP(); + + /* Put in a line break every 8 */ + if ( totalActions++ % 8 == 7 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + + +/* +string OCamlCodeGen::ACCESS() +{ + ostringstream ret; + if ( accessExpr != 0 ) + INLINE_LIST( ret, accessExpr, 0, false ); + return ret.str(); +} +*/ + +string OCamlCodeGen::make_access(char const* name, GenInlineList* x, bool prefix = true) +{ + ostringstream ret; + if ( x == 0 ) + { + if (prefix && accessExpr != 0) + { + INLINE_LIST( ret, accessExpr, 0, false); + ret << name; + } + else + ret << name << ".contents"; // ref cell + } + else { + ret << "("; + INLINE_LIST( ret, x, 0, false ); + ret << ")"; + } + return ret.str(); +} + +string OCamlCodeGen::P() { return make_access("p", pExpr, false); } +string OCamlCodeGen::PE() { return make_access("pe", peExpr, false); } +string OCamlCodeGen::vEOF() { return make_access("eof", eofExpr, false); } +string OCamlCodeGen::vCS() { return make_access("cs", csExpr); } +string OCamlCodeGen::TOP() { return make_access("top", topExpr); } +string OCamlCodeGen::STACK() { return make_access("stack", stackExpr); } +string OCamlCodeGen::ACT() { return make_access("act", actExpr); } +string OCamlCodeGen::TOKSTART() { return make_access("ts", tokstartExpr); } +string OCamlCodeGen::TOKEND() { return make_access("te", tokendExpr); } + +string OCamlCodeGen::GET_WIDE_KEY() +{ + if ( redFsm->anyConditions() ) + return "_widec"; + else + { ostringstream ret; ret << "Char.code " << GET_KEY(); return ret.str(); } +} + +string OCamlCodeGen::GET_WIDE_KEY( RedStateAp *state ) +{ + if ( state->stateCondList.length() > 0 ) + return "_widec"; + else + { ostringstream ret; ret << "Char.code " << GET_KEY(); return ret.str(); } +} + +/* Write out level number of tabs. Makes the nested binary search nice + * looking. */ +string OCamlCodeGen::TABS( int level ) +{ + string result; + while ( level-- > 0 ) + result += "\t"; + return result; +} + +/* Write out a key from the fsm code gen. Depends on wether or not the key is + * signed. */ +string OCamlCodeGen::KEY( Key key ) +{ + ostringstream ret; + if ( keyOps->isSigned || !hostLang->explicitUnsigned ) + ret << key.getVal(); + else + ret << (unsigned long) key.getVal() << 'u'; + return ret.str(); +} + +string OCamlCodeGen::ALPHA_KEY( Key key ) +{ + ostringstream ret; + ret << key.getVal(); + /* + if (key.getVal() > 0xFFFF) { + ret << key.getVal(); + } else { + ret << "'\\u" << std::hex << std::setw(4) << std::setfill('0') << + key.getVal() << "'"; + } + */ + //ret << "(char) " << key.getVal(); + return ret.str(); +} + +void OCamlCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish ) +{ +// The parser gives fexec two children. + ret << "begin " << P() << " <- "; + INLINE_LIST( ret, item->children, targState, inFinish ); + ret << " - 1 end; "; +} + +void OCamlCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item, + int targState, int inFinish ) +{ + ret << + " begin match " << ACT() << " with\n"; + + for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) { + /* Write the case label, the action and the case break. */ + if ( lma->lmId < 0 ) + ret << " | _ ->\n"; + else + ret << " | " << lma->lmId << " ->\n"; + + /* Write the block and close it off. */ + ret << " begin "; + INLINE_LIST( ret, lma->children, targState, inFinish ); + ret << " end\n"; + } + + ret << + " end;\n" + "\t"; +} + +void OCamlCodeGen::SET_ACT( ostream &ret, GenInlineItem *item ) +{ + ret << ACT() << " <- " << item->lmId << "; "; +} + +void OCamlCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item ) +{ + /* The tokend action sets tokend. */ + ret << TOKEND() << " <- " << P(); + if ( item->offset != 0 ) + out << "+" << item->offset; + out << "; "; +} + +void OCamlCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item ) +{ + ret << TOKEND(); +} + +void OCamlCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item ) +{ + ret << TOKSTART() << " <- " << NULL_ITEM() << "; "; +} + +void OCamlCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item ) +{ + ret << ACT() << " <- 0;"; +} + +void OCamlCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item ) +{ + ret << TOKSTART() << " <- " << P() << "; "; +} + +void OCamlCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item, + int targState, bool inFinish ) +{ + if ( item->children->length() > 0 ) { + /* Write the block and close it off. */ + ret << "begin "; + INLINE_LIST( ret, item->children, targState, inFinish ); + ret << " end"; + } +} + + +/* Write out an inline tree structure. Walks the list and possibly calls out + * to virtual functions than handle language specific items in the tree. */ +void OCamlCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList, + int targState, bool inFinish ) +{ + for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) { + switch ( item->type ) { + case GenInlineItem::Text: + ret << item->data; + break; + case GenInlineItem::Goto: + GOTO( ret, item->targState->id, inFinish ); + break; + case GenInlineItem::Call: + CALL( ret, item->targState->id, targState, inFinish ); + break; + case GenInlineItem::Next: + NEXT( ret, item->targState->id, inFinish ); + break; + case GenInlineItem::Ret: + RET( ret, inFinish ); + break; + case GenInlineItem::PChar: + ret << P(); + break; + case GenInlineItem::Char: + ret << GET_KEY(); + break; + case GenInlineItem::Hold: + ret << P() << " <- " << P() << " - 1; "; + break; + case GenInlineItem::Exec: + EXEC( ret, item, targState, inFinish ); + break; + case GenInlineItem::Curs: + CURS( ret, inFinish ); + break; + case GenInlineItem::Targs: + TARGS( ret, inFinish, targState ); + break; + case GenInlineItem::Entry: + ret << item->targState->id; + break; + case GenInlineItem::GotoExpr: + GOTO_EXPR( ret, item, inFinish ); + break; + case GenInlineItem::CallExpr: + CALL_EXPR( ret, item, targState, inFinish ); + break; + case GenInlineItem::NextExpr: + NEXT_EXPR( ret, item, inFinish ); + break; + case GenInlineItem::LmSwitch: + LM_SWITCH( ret, item, targState, inFinish ); + break; + case GenInlineItem::LmSetActId: + SET_ACT( ret, item ); + break; + case GenInlineItem::LmSetTokEnd: + SET_TOKEND( ret, item ); + break; + case GenInlineItem::LmGetTokEnd: + GET_TOKEND( ret, item ); + break; + case GenInlineItem::LmInitTokStart: + INIT_TOKSTART( ret, item ); + break; + case GenInlineItem::LmInitAct: + INIT_ACT( ret, item ); + break; + case GenInlineItem::LmSetTokStart: + SET_TOKSTART( ret, item ); + break; + case GenInlineItem::SubAction: + SUB_ACTION( ret, item, targState, inFinish ); + break; + case GenInlineItem::Break: + BREAK( ret, targState ); + break; + } + } +} +/* Write out paths in line directives. Escapes any special characters. */ +string OCamlCodeGen::LDIR_PATH( char *path ) +{ + ostringstream ret; + for ( char *pc = path; *pc != 0; pc++ ) { + if ( *pc == '\\' ) + ret << "\\\\"; + else + ret << *pc; + } + return ret.str(); +} + +void OCamlCodeGen::ACTION( ostream &ret, GenAction *action, int targState, bool inFinish ) +{ + /* Write the preprocessor line info for going into the source file. */ + ocamlLineDirective( ret, action->loc.fileName, action->loc.line ); + + /* Write the block and close it off. */ + ret << "\t\tbegin "; + INLINE_LIST( ret, action->inlineList, targState, inFinish ); + ret << " end;\n"; +} + +void OCamlCodeGen::CONDITION( ostream &ret, GenAction *condition ) +{ + ret << "\n"; + ocamlLineDirective( ret, condition->loc.fileName, condition->loc.line ); + INLINE_LIST( ret, condition->inlineList, 0, false ); +} + +string OCamlCodeGen::ERROR_STATE() +{ + ostringstream ret; + if ( redFsm->errState != 0 ) + ret << redFsm->errState->id; + else + ret << "-1"; + return ret.str(); +} + +string OCamlCodeGen::FIRST_FINAL_STATE() +{ + ostringstream ret; + if ( redFsm->firstFinState != 0 ) + ret << redFsm->firstFinState->id; + else + ret << redFsm->nextStateId; + return ret.str(); +} + +void OCamlCodeGen::writeInit() +{ + out << " begin\n"; + + if ( !noCS ) + out << "\t" << vCS() << " <- " << START() << ";\n"; + + /* If there are any calls, then the stack top needs initialization. */ + if ( redFsm->anyActionCalls() || redFsm->anyActionRets() ) + out << "\t" << TOP() << " <- 0;\n"; + + if ( hasLongestMatch ) { + out << + " " << TOKSTART() << " <- " << NULL_ITEM() << ";\n" + " " << TOKEND() << " <- " << NULL_ITEM() << ";\n" + " " << ACT() << " <- 0;\n"; + } + out << " end;\n"; +} + +string OCamlCodeGen::PRE_INCR(string val) +{ + ostringstream ret; + ret << "(" << val << " <- " << val << " + 1; " << val << ")"; + return ret.str(); +} + +string OCamlCodeGen::POST_INCR(string val) +{ + ostringstream ret; + ret << "(let temp = " << val << " in " << val << " <- " << val << " + 1; temp)"; + return ret.str(); +} + +string OCamlCodeGen::PRE_DECR(string val) +{ + ostringstream ret; + ret << "(" << val << " <- " << val << " - 1; " << val << ")"; + return ret.str(); +} + +string OCamlCodeGen::POST_DECR(string val) +{ + ostringstream ret; + ret << "(let temp = " << val << " in " << val << " <- " << val << " - 1; temp)"; + return ret.str(); +} + +string OCamlCodeGen::DATA_PREFIX() +{ + if ( data_prefix.empty() ) // init + { + data_prefix = string(fsmName) + "_"; + if (data_prefix.size() > 0) + data_prefix[0] = ::tolower(data_prefix[0]); // uncapitalize + } + if ( !noPrefix ) + return data_prefix; + return ""; +} + +/* Emit the alphabet data type. */ +string OCamlCodeGen::ALPH_TYPE() +{ + string ret = keyOps->alphType->data1; + if ( keyOps->alphType->data2 != 0 ) { + ret += " "; + ret += + keyOps->alphType->data2; + } + return ret; +} + +/* Emit the alphabet data type. */ +string OCamlCodeGen::WIDE_ALPH_TYPE() +{ + string ret; + if ( redFsm->maxKey <= keyOps->maxKey ) + ret = ALPH_TYPE(); + else { + long long maxKeyVal = redFsm->maxKey.getLongLong(); + HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal ); + assert( wideType != 0 ); + + ret = wideType->data1; + if ( wideType->data2 != 0 ) { + ret += " "; + ret += wideType->data2; + } + } + return ret; +} + +void OCamlCodeGen::STATE_IDS() +{ + if ( redFsm->startState != 0 ) + STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << TOP_SEP (); + + if ( !noFinal ) + STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << TOP_SEP(); + + if ( !noError ) + STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << TOP_SEP(); + + out << "\n"; + + if ( entryPointNames.length() > 0 ) { + for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) { + STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) << + " = " << entryPointIds[en.pos()] << TOP_SEP(); + } + out << "\n"; + } +} + + +void OCamlCodeGen::writeStart() +{ + out << START_STATE_ID(); +} + +void OCamlCodeGen::writeFirstFinal() +{ + out << FIRST_FINAL_STATE(); +} + +void OCamlCodeGen::writeError() +{ + out << ERROR_STATE(); +} + +string OCamlCodeGen::GET_KEY() +{ + ostringstream ret; + if ( getKeyExpr != 0 ) { + /* Emit the user supplied method of retrieving the key. */ + ret << "("; + INLINE_LIST( ret, getKeyExpr, 0, false ); + ret << ")"; + } + else { + /* Expression for retrieving the key, use simple dereference. */ + ret << "data.[" << P() << "]"; + } + return ret.str(); +} +string OCamlCodeGen::NULL_ITEM() +{ + return "-1"; +} + +string OCamlCodeGen::POINTER() +{ + // XXX C# has no pointers + // multiple items seperated by commas can also be pointer types. + return " "; +} + +string OCamlCodeGen::PTR_CONST() +{ + return ""; +} + +std::ostream &OCamlCodeGen::OPEN_ARRAY( string type, string name ) +{ + out << "let " << name << " : " << type << " array = [|" << endl; + return out; +} + +std::ostream &OCamlCodeGen::CLOSE_ARRAY() +{ + return out << "|]" << TOP_SEP(); +} + +string OCamlCodeGen::TOP_SEP() +{ + return "\n"; // original syntax +} + +string OCamlCodeGen::ARR_SEP() +{ + return "; "; +} + +string OCamlCodeGen::AT(const string& array, const string& index) +{ + ostringstream ret; + ret << array << ".(" << index << ")"; + return ret.str(); +} + +std::ostream &OCamlCodeGen::STATIC_VAR( string type, string name ) +{ + out << "let " << name << " : " << type; + return out; +} + +string OCamlCodeGen::ARR_OFF( string ptr, string offset ) +{ + // XXX C# can't do pointer arithmetic + return "&" + ptr + "[" + offset + "]"; +} + +string OCamlCodeGen::CAST( string type ) +{ + return ""; +// return "(" + type + ")"; +} + +string OCamlCodeGen::UINT( ) +{ + return "uint"; +} + +std::ostream &OCamlCodeGen::SWITCH_DEFAULT() +{ + out << " | _ -> ()\n"; + return out; +} + +string OCamlCodeGen::CTRL_FLOW() +{ + return "if true then "; +} + +void OCamlCodeGen::finishRagelDef() +{ + if ( codeStyle == GenGoto || codeStyle == GenFGoto || + codeStyle == GenIpGoto || codeStyle == GenSplit ) + { + /* For directly executable machines there is no required state + * ordering. Choose a depth-first ordering to increase the + * potential for fall-throughs. */ + redFsm->depthFirstOrdering(); + } + else { + /* The frontend will do this for us, but it may be a good idea to + * force it if the intermediate file is edited. */ + redFsm->sortByStateId(); + } + + /* Choose default transitions and the single transition. */ + redFsm->chooseDefaultSpan(); + + /* Maybe do flat expand, otherwise choose single. */ + if ( codeStyle == GenFlat || codeStyle == GenFFlat ) + redFsm->makeFlat(); + else + redFsm->chooseSingle(); + + /* If any errors have occured in the input file then don't write anything. */ + if ( gblErrorCount > 0 ) + return; + + if ( codeStyle == GenSplit ) + redFsm->partitionFsm( numSplitPartitions ); + + if ( codeStyle == GenIpGoto || codeStyle == GenSplit ) + redFsm->setInTrans(); + + /* Anlayze Machine will find the final action reference counts, among + * other things. We will use these in reporting the usage + * of fsm directives in action code. */ + analyzeMachine(); + + /* Determine if we should use indicies. */ + calcIndexSize(); +} + +ostream &OCamlCodeGen::source_warning( const InputLoc &loc ) +{ + cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: "; + return cerr; +} + +ostream &OCamlCodeGen::source_error( const InputLoc &loc ) +{ + gblErrorCount += 1; + assert( sourceFileName != 0 ); + cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": "; + return cerr; +} + diff --git ragel/mlcodegen.h ragel/mlcodegen.h new file mode 100644 index 0000000..3df047b --- /dev/null +++ ragel/mlcodegen.h @@ -0,0 +1,205 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 + */ + +#ifndef _MLCODEGEN_H +#define _MLCODEGEN_H + +#include +#include +#include +#include "common.h" +#include "gendata.h" + +using std::string; +using std::ostream; + +/* Forwards. */ +/* +struct RedFsmAp; +struct RedStateAp; +struct CodeGenData; +struct GenAction; +struct NameInst; +struct GenInlineItem; +struct GenInlineList; +struct RedAction; +struct LongestMatch; +struct LongestMatchPart; +*/ + +/* Integer array line length. */ +#define IALL 8 + +//string itoa( int i ); + +/* + * class OCamlCodeGen + */ +class OCamlCodeGen : public CodeGenData +{ +public: + OCamlCodeGen( ostream &out ); + virtual ~OCamlCodeGen() {} + + virtual void finishRagelDef(); + virtual void writeInit(); + virtual void writeStart(); + virtual void writeFirstFinal(); + virtual void writeError(); + +protected: + string data_prefix; + + string FSM_NAME(); + string START_STATE_ID(); + ostream &ACTIONS_ARRAY(); + string GET_WIDE_KEY(); + string GET_WIDE_KEY( RedStateAp *state ); + string TABS( int level ); + string KEY( Key key ); + string ALPHA_KEY( Key key ); + string LDIR_PATH( char *path ); + void ACTION( ostream &ret, GenAction *action, int targState, bool inFinish ); + void CONDITION( ostream &ret, GenAction *condition ); + string ALPH_TYPE(); + string WIDE_ALPH_TYPE(); + string ARRAY_TYPE( unsigned long maxVal ); + string ARRAY_TYPE( unsigned long maxVal, bool forceSigned ); + + virtual string ARR_OFF( string ptr, string offset ); + virtual string CAST( string type ); + virtual string UINT(); + virtual string NULL_ITEM(); + virtual string POINTER(); + virtual string GET_KEY(); + virtual ostream &SWITCH_DEFAULT(); + + string P(); + string PE(); + string vEOF(); + +// string ACCESS(); + string vCS(); + string STACK(); + string TOP(); + string TOKSTART(); + string TOKEND(); + string ACT(); + + // ++x + string PRE_INCR(string); + string PRE_DECR(string); + + // x++ + string POST_INCR(string); + string POST_DECR(string); + + string DATA_PREFIX(); + string PM() { return "_" + DATA_PREFIX() + "partition_map"; } + string C() { return "_" + DATA_PREFIX() + "cond_spaces"; } + string CK() { return "_" + DATA_PREFIX() + "cond_keys"; } + string K() { return "_" + DATA_PREFIX() + "trans_keys"; } + string I() { return "_" + DATA_PREFIX() + "indicies"; } + string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; } + string KO() { return "_" + DATA_PREFIX() + "key_offsets"; } + string IO() { return "_" + DATA_PREFIX() + "index_offsets"; } + string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; } + string SL() { return "_" + DATA_PREFIX() + "single_lengths"; } + string RL() { return "_" + DATA_PREFIX() + "range_lengths"; } + string A() { return "_" + DATA_PREFIX() + "actions"; } + string TA() { return "_" + DATA_PREFIX() + "trans_actions"; } + string TT() { return "_" + DATA_PREFIX() + "trans_targs"; } + string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; } + string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; } + string EA() { return "_" + DATA_PREFIX() + "eof_actions"; } + string ET() { return "_" + DATA_PREFIX() + "eof_trans"; } + string SP() { return "_" + DATA_PREFIX() + "key_spans"; } + string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; } + string START() { return DATA_PREFIX() + "start"; } + string ERROR() { return DATA_PREFIX() + "error"; } + string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; } + string CTXDATA() { return DATA_PREFIX() + "ctxdata"; } + + void INLINE_LIST( ostream &ret, GenInlineList *inlineList, int targState, bool inFinish ); + virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0; + virtual void CALL( 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 RET( ostream &ret, bool inFinish ) = 0; + virtual void BREAK( ostream &ret, int targState ) = 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 ); + 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 SUB_ACTION( ostream &ret, GenInlineItem *item, + int targState, bool inFinish ); + void STATE_IDS(); + + string ERROR_STATE(); + string FIRST_FINAL_STATE(); + + virtual string PTR_CONST(); + virtual ostream &OPEN_ARRAY( string type, string name ); + virtual ostream &CLOSE_ARRAY(); + virtual ostream &STATIC_VAR( string type, string name ); + + virtual string CTRL_FLOW(); + + // toplevel phrase separator + string TOP_SEP(); + // array elements separator + string ARR_SEP(); + // access array + string AT(const string& array, const string& index); + + string make_access(char const* name, GenInlineList* x, bool prefix); + + ostream &source_warning(const InputLoc &loc); + ostream &source_error(const InputLoc &loc); + + unsigned int arrayTypeSize( unsigned long maxVal ); + + bool outLabelUsed; + bool testEofUsed; + bool againLabelUsed; + bool useIndicies; + +public: + /* Determine if we should use indicies. */ + virtual void calcIndexSize() {} + + void genLineDirective( ostream &out ); +}; + +#define MAX(a, b) (a > b ? a : b) + +#endif diff --git ragel/mlfflat.cpp ragel/mlfflat.cpp new file mode 100644 index 0000000..9251a9a --- /dev/null +++ ragel/mlfflat.cpp @@ -0,0 +1,419 @@ +/* + * Copyright 2004-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 "ragel.h" +#include "mlfflat.h" +#include "redfsm.h" +#include "gendata.h" + +std::ostream &OCamlFFlatCodeGen::TO_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->toStateAction != 0 ) + act = state->toStateAction->actListId+1; + out << act; + return out; +} + +std::ostream &OCamlFFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->fromStateAction != 0 ) + act = state->fromStateAction->actListId+1; + out << act; + return out; +} + +std::ostream &OCamlFFlatCodeGen::EOF_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->eofAction != 0 ) + act = state->eofAction->actListId+1; + out << act; + return out; +} + +/* Write out the function for a transition. */ +std::ostream &OCamlFFlatCodeGen::TRANS_ACTION( RedTransAp *trans ) +{ + int action = 0; + if ( trans->action != 0 ) + action = trans->action->actListId+1; + out << action; + return out; +} + +/* Write out the function switch. This switch is keyed on the values + * of the func index. */ +std::ostream &OCamlFFlatCodeGen::TO_STATE_ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numToStateRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, false ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +/* Write out the function switch. This switch is keyed on the values + * of the func index. */ +std::ostream &OCamlFFlatCodeGen::FROM_STATE_ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numFromStateRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, false ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlFFlatCodeGen::EOF_ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numEofRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, true ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +/* Write out the function switch. This switch is keyed on the values + * of the func index. */ +std::ostream &OCamlFFlatCodeGen::ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numTransRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, false ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +void OCamlFFlatCodeGen::writeData() +{ + if ( redFsm->anyConditions() ) { + OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() ); + COND_KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() ); + COND_KEY_SPANS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() ); + CONDS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() ); + COND_INDEX_OFFSET(); + CLOSE_ARRAY() << + "\n"; + } + + OPEN_ARRAY( WIDE_ALPH_TYPE(), K() ); + KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() ); + KEY_SPANS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() ); + FLAT_INDEX_OFFSET(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() ); + INDICIES(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() ); + TRANS_TARGS(); + CLOSE_ARRAY() << + "\n"; + + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() ); + TRANS_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyToStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() ); + TO_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyFromStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() ); + FROM_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() ); + EOF_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofTrans() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() ); + EOF_TRANS(); + CLOSE_ARRAY() << + "\n"; + } + + STATE_IDS(); + + out << "type state = { mutable keys : int; mutable trans : int; }" + << TOP_SEP(); + + out << "exception Goto_match" << TOP_SEP(); + out << "exception Goto_again" << TOP_SEP(); + out << "exception Goto_eof_trans" << TOP_SEP(); +} + +void OCamlFFlatCodeGen::writeExec() +{ + testEofUsed = false; + outLabelUsed = false; + initVarTypes(); + + out << + " begin\n"; +// " " << slenType << " _slen"; + +// if ( redFsm->anyRegCurStateRef() ) +// out << ", _ps"; + +// out << ";\n"; +// out << " " << transType << " _trans"; + +// if ( redFsm->anyConditions() ) +// out << ", _cond"; + +// out << ";\n"; + +// out << +// " " << "int _keys;\n" +// " " << indsType << " _inds;\n"; + /* + " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n" + " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxIndex) << POINTER() << "_inds;\n";*/ + + out << + " let state = { keys = 0; trans = 0; } in\n" + " let rec do_start () =\n"; + +// if ( redFsm->anyConditions() ) { +// out << +// " " << condsType << " _conds;\n" +// " " << WIDE_ALPH_TYPE() << " _widec;\n"; +// } + + if ( !noEnd ) { + testEofUsed = true; + out << + " if " << P() << " = " << PE() << " then\n" + " do_test_eof ()\n" + "\telse\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " if " << vCS() << " = " << redFsm->errState->id << " then\n" + " do_out ()\n" + "\telse\n"; + } + out << "\tdo_resume ()\n"; + + out << "and do_resume () =\n"; + + if ( redFsm->anyFromStateActions() ) { + out << + " begin match " << AT( FSA(), vCS() ) << " with\n"; + FROM_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + "\n"; + } + + if ( redFsm->anyConditions() ) + COND_TRANSLATE(); + + out << "\tbegin try\n"; + LOCATE_TRANS(); + out << "\twith Goto_match -> () end;\n"; + + out << "\tdo_eof_trans ()\n"; + +// if ( redFsm->anyEofTrans() ) + out << "and do_eof_trans () =\n"; + + if ( redFsm->anyRegCurStateRef() ) + out << " let ps = " << vCS() << " in\n"; + + out << + " " << vCS() << " <- " << AT( TT() ,"state.trans" ) << ";\n" + "\n"; + + if ( redFsm->anyRegActions() ) { + out << + " begin try if " << AT( TA() , "state.trans" ) << " = 0 then\n" + " raise Goto_again;\n" + "\n" + " match " << AT( TA(), "state.trans" ) << " with\n"; + ACTION_SWITCH(); + SWITCH_DEFAULT() << + " with Goto_again -> () end;\n" + "\n"; + } + out << "\tdo_again ()\n"; + +// if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || +// redFsm->anyActionCalls() || redFsm->anyActionRets() ) + out << "\tand do_again () =\n"; + + if ( redFsm->anyToStateActions() ) { + out << + " begin match " << AT( TSA(), vCS() ) << " with\n"; + TO_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + "\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " match " << vCS() << " with\n" + "\t| " << redFsm->errState->id << " -> do_out ()\n" + "\t| _ ->\n"; + } + + out << "\t" << P() << " <- " << P() << " + 1;\n"; + + if ( !noEnd ) { + out << + " if " << P() << " <> " << PE() << " then\n" + " do_resume ()\n" + "\telse do_test_eof ()\n"; + } + else { + out << + " do_resume ()\n"; + } + +// if ( testEofUsed ) + out << "and do_test_eof () =\n"; + + if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) { + out << + " if " << P() << " = " << vEOF() << " then\n" + " begin try\n"; + + if ( redFsm->anyEofTrans() ) { + out << + " if " << AT( ET(), vCS() ) << " > 0 then\n" + " begin\n" + " state.trans <- " << CAST(transType) << "(" << AT( ET(), vCS() ) << " - 1);\n" + " raise Goto_eof_trans;\n" + " end;\n"; + } + + if ( redFsm->anyEofActions() ) { + out << + " begin match " << AT( EA(), vCS() ) << " with\n"; + EOF_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n"; + } + + out << + " with Goto_again -> do_again ()\n" + " | Goto_eof_trans -> do_eof_trans () end\n" + "\n"; + } + else + { + out << "\t()\n"; + } + + if ( outLabelUsed ) + out << " and do_out () = ()\n"; + + out << "\tin do_start ()\n"; + out << " end;\n"; +} + diff --git ragel/mlfflat.h ragel/mlfflat.h new file mode 100644 index 0000000..242e6b9 --- /dev/null +++ ragel/mlfflat.h @@ -0,0 +1,55 @@ +/* + * Copyright 2004-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 + */ + +#ifndef _MLFFLAT_H +#define _MLFFLAT_H + +#include +#include "mlflat.h" + +/* Forwards. */ +//struct CodeGenData; + +/* + * OCamlFFlatCodeGen + */ +class OCamlFFlatCodeGen : public OCamlFlatCodeGen +{ +public: + OCamlFFlatCodeGen( ostream &out ) : OCamlFlatCodeGen(out) {} +private: + std::ostream &TO_STATE_ACTION_SWITCH(); + std::ostream &FROM_STATE_ACTION_SWITCH(); + std::ostream &EOF_ACTION_SWITCH(); + std::ostream &ACTION_SWITCH(); + + virtual std::ostream &TO_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &FROM_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &EOF_ACTION( RedStateAp *state ); + virtual std::ostream &TRANS_ACTION( RedTransAp *trans ); + + virtual void writeData(); + virtual void writeExec(); +}; + +#endif diff --git ragel/mlflat.cpp ragel/mlflat.cpp new file mode 100644 index 0000000..7dccdcc --- /dev/null +++ ragel/mlflat.cpp @@ -0,0 +1,911 @@ +/* + * Copyright 2004-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 +#include "ragel.h" +#include "mlflat.h" +#include "redfsm.h" +#include "gendata.h" + +std::ostream &OCamlFlatCodeGen::TO_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->toStateAction != 0 ) + act = state->toStateAction->location+1; + out << act; + return out; +} + +std::ostream &OCamlFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->fromStateAction != 0 ) + act = state->fromStateAction->location+1; + out << act; + return out; +} + +std::ostream &OCamlFlatCodeGen::EOF_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->eofAction != 0 ) + act = state->eofAction->location+1; + out << act; + return out; +} + +std::ostream &OCamlFlatCodeGen::TRANS_ACTION( RedTransAp *trans ) +{ + /* If there are actions, emit them. Otherwise emit zero. */ + int act = 0; + if ( trans->action != 0 ) + act = trans->action->location+1; + out << act; + return out; +} + +std::ostream &OCamlFlatCodeGen::TO_STATE_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numToStateRefs > 0 ) { + /* Write the case label, the action and the case break */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlFlatCodeGen::FROM_STATE_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numFromStateRefs > 0 ) { + /* Write the case label, the action and the case break */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlFlatCodeGen::EOF_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numEofRefs > 0 ) { + /* Write the case label, the action and the case break */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, true ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + + +std::ostream &OCamlFlatCodeGen::ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numTransRefs > 0 ) { + /* Write the case label, the action and the case break */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + + +std::ostream &OCamlFlatCodeGen::FLAT_INDEX_OFFSET() +{ + out << "\t"; + int totalStateNum = 0, curIndOffset = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write the index offset. */ + out << curIndOffset; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + + /* Move the index offset ahead. */ + if ( st->transList != 0 ) + curIndOffset += keyOps->span( st->lowKey, st->highKey ); + + if ( st->defTrans != 0 ) + curIndOffset += 1; + } + out << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::KEY_SPANS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write singles length. */ + unsigned long long span = 0; + if ( st->transList != 0 ) + span = keyOps->span( st->lowKey, st->highKey ); + out << span; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::TO_STATE_ACTIONS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + TO_STATE_ACTION(st); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::FROM_STATE_ACTIONS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + FROM_STATE_ACTION(st); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::EOF_ACTIONS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + EOF_ACTION(st); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::EOF_TRANS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + + long trans = 0; + if ( st->eofTrans != 0 ) { + assert( st->eofTrans->pos >= 0 ); + trans = st->eofTrans->pos+1; + } + out << trans; + + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + + +std::ostream &OCamlFlatCodeGen::COND_KEYS() +{ + out << '\t'; + int totalTrans = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Emit just cond low key and cond high key. */ + out << ALPHA_KEY( st->condLowKey ) << ARR_SEP(); + out << ALPHA_KEY( st->condHighKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << /*"(char) " <<*/ 0 << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::COND_KEY_SPANS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write singles length. */ + unsigned long long span = 0; + if ( st->condList != 0 ) + span = keyOps->span( st->condLowKey, st->condHighKey ); + out << span; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::CONDS() +{ + int totalTrans = 0; + out << '\t'; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + if ( st->condList != 0 ) { + /* Walk the singles. */ + unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey ); + for ( unsigned long long pos = 0; pos < span; pos++ ) { + if ( st->condList[pos] != 0 ) + out << st->condList[pos]->condSpaceId + 1 << ARR_SEP(); + else + out << "0" << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::COND_INDEX_OFFSET() +{ + out << "\t"; + int totalStateNum = 0, curIndOffset = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write the index offset. */ + out << curIndOffset; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + + /* Move the index offset ahead. */ + if ( st->condList != 0 ) + curIndOffset += keyOps->span( st->condLowKey, st->condHighKey ); + } + out << "\n"; + return out; +} + + +std::ostream &OCamlFlatCodeGen::KEYS() +{ + out << '\t'; + int totalTrans = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Emit just low key and high key. */ + out << ALPHA_KEY( st->lowKey ) << ARR_SEP(); + out << ALPHA_KEY( st->highKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << /*"(char) " <<*/ 0 << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::INDICIES() +{ + int totalTrans = 0; + out << '\t'; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + if ( st->transList != 0 ) { + /* Walk the singles. */ + unsigned long long span = keyOps->span( st->lowKey, st->highKey ); + for ( unsigned long long pos = 0; pos < span; pos++ ) { + out << st->transList[pos]->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + /* The state's default index goes next. */ + if ( st->defTrans != 0 ) + out << st->defTrans->id << ARR_SEP(); + + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlFlatCodeGen::TRANS_TARGS() +{ + /* Transitions must be written ordered by their id. */ + RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()]; + for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) + transPtrs[trans->id] = trans; + + /* Keep a count of the num of items in the array written. */ + out << '\t'; + int totalStates = 0; + for ( int t = 0; t < redFsm->transSet.length(); t++ ) { + /* Record the position, need this for eofTrans. */ + RedTransAp *trans = transPtrs[t]; + trans->pos = t; + + /* Write out the target state. */ + out << trans->targ->id; + if ( t < redFsm->transSet.length()-1 ) { + out << ARR_SEP(); + if ( ++totalStates % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] transPtrs; + return out; +} + + +std::ostream &OCamlFlatCodeGen::TRANS_ACTIONS() +{ + /* Transitions must be written ordered by their id. */ + RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()]; + for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) + transPtrs[trans->id] = trans; + + /* Keep a count of the num of items in the array written. */ + out << '\t'; + int totalAct = 0; + for ( int t = 0; t < redFsm->transSet.length(); t++ ) { + /* Write the function for the transition. */ + RedTransAp *trans = transPtrs[t]; + TRANS_ACTION( trans ); + if ( t < redFsm->transSet.length()-1 ) { + out << ARR_SEP(); + if ( ++totalAct % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] transPtrs; + return out; +} + +void OCamlFlatCodeGen::LOCATE_TRANS() +{ + std::ostringstream temp; + temp << "inds + (\n" + " if slen > 0 && " << AT( K(), "keys" ) << " <= Char.code " << GET_WIDE_KEY() << " &&\n" + " Char.code " << GET_WIDE_KEY() << " <= " << AT( K(), "keys+1" ) << " then\n" + " Char.code " << GET_WIDE_KEY() << " - " << AT(K(), "keys" ) << " else slen)"; + out << + " let keys = " << vCS() << " lsl 1 in\n" + " let inds = " << AT( IO(), vCS() ) << " in\n" + "\n" + " let slen = " << AT( SP(), vCS() ) << " in\n" + " state.trans <- " << AT( I(), temp.str() ) << ";\n" + "\n"; +} + +void OCamlFlatCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish ) +{ + ret << "begin " << vCS() << " <- " << gotoDest << "; " << + CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlFlatCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) +{ + ret << "begin " << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, 0, inFinish ); + ret << "); " << CTRL_FLOW() << " raise Goto_again end"; +} + +void OCamlFlatCodeGen::CURS( ostream &ret, bool inFinish ) +{ + ret << "(_ps)"; +} + +void OCamlFlatCodeGen::TARGS( ostream &ret, bool inFinish, int targState ) +{ + ret << "(" << vCS() << ")"; +} + +void OCamlFlatCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish ) +{ + ret << vCS() << " <- " << nextDest << ";"; +} + +void OCamlFlatCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) +{ + ret << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, 0, inFinish ); + ret << ");"; +} + +void OCamlFlatCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish ) +{ + if ( prePushExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, prePushExpr, 0, false ); + } + + ret << "begin " << AT( STACK(), POST_INCR(TOP()) ) << " <- " << vCS() << "; "; + ret << vCS() << " <- " << callDest << "; " << CTRL_FLOW() << "raise Goto_again end "; + + if ( prePushExpr != 0 ) + ret << "end"; +} + +void OCamlFlatCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ) +{ + if ( prePushExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, prePushExpr, 0, false ); + } + + ret << "begin " << AT(STACK(), POST_INCR(TOP()) ) << " <- " << vCS() << "; " << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, targState, inFinish ); + ret << "); " << CTRL_FLOW() << "raise Goto_again end "; + + if ( prePushExpr != 0 ) + ret << "end"; +} + +void OCamlFlatCodeGen::RET( ostream &ret, bool inFinish ) +{ + ret << "begin " << vCS() << " <- " << AT(STACK(), PRE_DECR(TOP()) ) << "; "; + + if ( postPopExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, postPopExpr, 0, false ); + ret << "end "; + } + + ret << CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlFlatCodeGen::BREAK( ostream &ret, int targState ) +{ + outLabelUsed = true; + ret << "begin " << P() << " <- " << P() << " + 1; " << CTRL_FLOW() << "raise Goto_out end"; +} + +void OCamlFlatCodeGen::writeData() +{ + /* If there are any transtion functions then output the array. If there + * are none, don't bother emitting an empty array that won't be used. */ + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() ); + ACTIONS_ARRAY(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyConditions() ) { + OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() ); + COND_KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() ); + COND_KEY_SPANS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() ); + CONDS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() ); + COND_INDEX_OFFSET(); + CLOSE_ARRAY() << + "\n"; + } + + OPEN_ARRAY( WIDE_ALPH_TYPE(), K() ); + KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() ); + KEY_SPANS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() ); + FLAT_INDEX_OFFSET(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() ); + INDICIES(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() ); + TRANS_TARGS(); + CLOSE_ARRAY() << + "\n"; + + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() ); + TRANS_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyToStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() ); + TO_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyFromStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() ); + FROM_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() ); + EOF_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofTrans() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() ); + EOF_TRANS(); + CLOSE_ARRAY() << + "\n"; + } + + STATE_IDS(); + + out << "type state = { mutable trans : int; mutable acts : int; mutable nacts : int; }" + << TOP_SEP(); + + out << "exception Goto_match" << TOP_SEP(); + out << "exception Goto_again" << TOP_SEP(); + out << "exception Goto_eof_trans" << TOP_SEP(); +} + +void OCamlFlatCodeGen::COND_TRANSLATE() +{ + out << + " _widec = " << GET_KEY() << ";\n"; + + out << + " _keys = " << vCS() << "<<1;\n" + " _conds = " << CO() << "[" << vCS() << "];\n" +// " _keys = " << ARR_OFF( CK(), "(" + vCS() + "<<1)" ) << ";\n" +// " _conds = " << ARR_OFF( C(), CO() + "[" + vCS() + "]" ) << ";\n" + "\n" + " _slen = " << CSP() << "[" << vCS() << "];\n" + " if (_slen > 0 && " << CK() << "[_keys] <=" + << GET_WIDE_KEY() << " &&\n" + " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys+1])\n" + " _cond = " << C() << "[_conds+" << GET_WIDE_KEY() << " - " << + CK() << "[_keys]];\n" + " else\n" + " _cond = 0;" + "\n"; + /* XXX This version of the code doesn't work because Mono is weird. Works + * fine in Microsoft's csc, even though the bug report filed claimed it + * didn't. + " _slen = " << CSP() << "[" << vCS() << "];\n" + " _cond = _slen > 0 && " << CK() << "[_keys] <=" + << GET_WIDE_KEY() << " &&\n" + " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys+1] ?\n" + " " << C() << "[_conds+" << GET_WIDE_KEY() << " - " << CK() + << "[_keys]] : 0;\n" + "\n"; + */ + out << + " switch ( _cond ) {\n"; + for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) { + GenCondSpace *condSpace = csi; + out << " case " << condSpace->condSpaceId + 1 << ": {\n"; + out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" << + KEY(condSpace->baseKey) << " + (" << GET_KEY() << + " - " << KEY(keyOps->minKey) << "));\n"; + + for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) { + out << TABS(2) << "if ( "; + CONDITION( out, *csi ); + Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize()); + out << " ) _widec += " << condValOffset << ";\n"; + } + + out << " }\n"; + out << " break;\n"; + } + + SWITCH_DEFAULT(); + + out << + " }\n"; +} + +void OCamlFlatCodeGen::writeExec() +{ + testEofUsed = false; + outLabelUsed = false; + initVarTypes(); + + out << + " begin\n"; +// " " << slenType << " _slen"; + +// if ( redFsm->anyRegCurStateRef() ) +// out << ", _ps"; + +// out << +// " " << transType << " _trans"; + +// if ( redFsm->anyConditions() ) +// out << ", _cond"; +// out << ";\n"; + +// if ( redFsm->anyToStateActions() || +// redFsm->anyRegActions() || redFsm->anyFromStateActions() ) +// { +// out << +// " int _acts;\n" +// " int _nacts;\n"; +// } + +// out << +// " " << "int _keys;\n" +// " " << indsType << " _inds;\n"; + /* + " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n" + " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxIndex) << POINTER() << "_inds;\n";*/ + + if ( redFsm->anyConditions() ) { + out << + " " << condsType << " _conds;\n" + " " << WIDE_ALPH_TYPE() << " _widec;\n"; + } + + out << "\n"; + + out << + " let state = { trans = 0; acts = 0; nacts = 0; } in\n" + " let rec do_start () =\n"; + + if ( !noEnd ) { + testEofUsed = true; + out << + " if " << P() << " = " << PE() << " then\n" + " do_test_eof ()\n" + "\telse\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " if " << vCS() << " = " << redFsm->errState->id << " then\n" + " do_out ()\n" + "\telse\n"; + } + + out << "\tdo_resume ()\n"; + + out << "and do_resume () =\n"; + + if ( redFsm->anyFromStateActions() ) { + out << + " state.acts <- " << AT( FSA(), vCS() ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + FROM_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n" + " done;\n" + "\n"; + } + + if ( redFsm->anyConditions() ) + COND_TRANSLATE(); + +// out << "\tbegin try\n"; + LOCATE_TRANS(); +// out << "\twith Goto_match -> () end;\n"; + + out << "\tdo_eof_trans ()\n"; + +// if ( redFsm->anyEofTrans() ) + out << "and do_eof_trans () =\n"; + + if ( redFsm->anyRegCurStateRef() ) + out << " let ps = " << vCS() << " in\n"; + + out << + " " << vCS() << " <- " << AT( TT() ,"state.trans" ) << ";\n" + "\n"; + + if ( redFsm->anyRegActions() ) { + out << + "\tbegin try\n" + " match " << AT( TA(), "state.trans" ) << " with\n" + "\t| 0 -> raise Goto_again\n" + "\t| _ ->\n" + " state.acts <- " << AT( TA(), "state.trans" ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + " done\n" + "\twith Goto_again -> () end;\n"; + } + out << "\tdo_again ()\n"; + +// if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || +// redFsm->anyActionCalls() || redFsm->anyActionRets() ) + out << "\tand do_again () =\n"; + + if ( redFsm->anyToStateActions() ) { + out << + " state.acts <- " << AT( TSA(), vCS() ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + TO_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n" + " done;\n" + "\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " match " << vCS() << " with\n" + "\t| " << redFsm->errState->id << " -> do_out ()\n" + "\t| _ ->\n"; + } + + out << "\t" << P() << " <- " << P() << " + 1;\n"; + + if ( !noEnd ) { + out << + " if " << P() << " <> " << PE() << " then\n" + " do_resume ()\n" + "\telse do_test_eof ()\n"; + } + else { + out << + " do_resume ()\n"; + } + +// if ( testEofUsed ) + out << "and do_test_eof () =\n"; + + if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) { + out << + " if " << P() << " = " << vEOF() << " then\n" + " begin try\n"; + + if ( redFsm->anyEofTrans() ) { + out << + " if " << AT( ET(), vCS() ) << " > 0 then\n" + " begin\n" + " state.trans <- " << CAST(transType) << "(" << AT( ET(), vCS() ) << " - 1);\n" + " raise Goto_eof_trans;\n" + " end\n"; + } + + if ( redFsm->anyEofActions() ) { + out << + " let __acts = ref " << AT( EA(), vCS() ) << " in\n" + " let __nacts = ref " << AT( A(), "!__acts" ) << " in\n" + " incr __acts;\n" + " while !__nacts > 0 do\n" + " decr __nacts;\n" + " begin match " << AT( A(), POST_INCR("__acts.contents") ) << " with\n"; + EOF_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + " done\n"; + } + + out << + " with Goto_again -> do_again ()\n" + " | Goto_eof_trans -> do_eof_trans () end\n" + "\n"; + } + else + { + out << "\t()\n"; + } + + if ( outLabelUsed ) + out << " and do_out () = ()\n"; + + out << "\tin do_start ()\n"; + out << " end;\n"; +} + +void OCamlFlatCodeGen::initVarTypes() +{ + slenType = ARRAY_TYPE(MAX(redFsm->maxSpan, redFsm->maxCondSpan)); + transType = ARRAY_TYPE(redFsm->maxIndex+1); + indsType = ARRAY_TYPE(redFsm->maxFlatIndexOffset); + condsType = ARRAY_TYPE(redFsm->maxCondIndexOffset); +} diff --git ragel/mlflat.h ragel/mlflat.h new file mode 100644 index 0000000..6da1819 --- /dev/null +++ ragel/mlflat.h @@ -0,0 +1,91 @@ +/* + * Copyright 2004-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 + */ + +#ifndef _MLFLAT_H +#define _MLFLAT_H + +#include +#include "mlcodegen.h" + +/* Forwards. */ +//struct CodeGenData; +//struct NameInst; +//struct RedTransAp; +//struct RedStateAp; + +/* + * OCamlFlatCodeGen + */ +class OCamlFlatCodeGen : public OCamlCodeGen +{ +public: + OCamlFlatCodeGen( ostream &out ) : OCamlCodeGen(out) {} + virtual ~OCamlFlatCodeGen() { } + +protected: + std::ostream &TO_STATE_ACTION_SWITCH(); + std::ostream &FROM_STATE_ACTION_SWITCH(); + std::ostream &EOF_ACTION_SWITCH(); + std::ostream &ACTION_SWITCH(); + std::ostream &KEYS(); + std::ostream &INDICIES(); + std::ostream &FLAT_INDEX_OFFSET(); + std::ostream &KEY_SPANS(); + std::ostream &TO_STATE_ACTIONS(); + std::ostream &FROM_STATE_ACTIONS(); + std::ostream &EOF_ACTIONS(); + std::ostream &EOF_TRANS(); + std::ostream &TRANS_TARGS(); + std::ostream &TRANS_ACTIONS(); + void LOCATE_TRANS(); + + std::ostream &COND_INDEX_OFFSET(); + void COND_TRANSLATE(); + std::ostream &CONDS(); + std::ostream &COND_KEYS(); + std::ostream &COND_KEY_SPANS(); + + void GOTO( ostream &ret, int gotoDest, bool inFinish ); + void CALL( ostream &ret, int callDest, int targState, bool inFinish ); + void NEXT( ostream &ret, int nextDest, bool inFinish ); + void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ); + void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ); + void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ); + void CURS( ostream &ret, bool inFinish ); + void TARGS( ostream &ret, bool inFinish, int targState ); + void RET( ostream &ret, bool inFinish ); + void BREAK( ostream &ret, int targState ); + + virtual std::ostream &TO_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &FROM_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &EOF_ACTION( RedStateAp *state ); + virtual std::ostream &TRANS_ACTION( RedTransAp *trans ); + + virtual void writeData(); + virtual void writeExec(); + + void initVarTypes(); + string slenType, transType, indsType, condsType; +}; + +#endif diff --git ragel/mlftable.cpp ragel/mlftable.cpp new file mode 100644 index 0000000..7cf258e --- /dev/null +++ ragel/mlftable.cpp @@ -0,0 +1,462 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 "ragel.h" +#include "mlftable.h" +#include "redfsm.h" +#include "gendata.h" + +/* Determine if we should use indicies or not. */ +void OCamlFTabCodeGen::calcIndexSize() +{ + int sizeWithInds = 0, sizeWithoutInds = 0; + + /* Calculate cost of using with indicies. */ + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + int totalIndex = st->outSingle.length() + st->outRange.length() + + (st->defTrans == 0 ? 0 : 1); + sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex; + } + sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length(); + if ( redFsm->anyActions() ) + sizeWithInds += arrayTypeSize(redFsm->maxActListId) * redFsm->transSet.length(); + + /* Calculate the cost of not using indicies. */ + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + int totalIndex = st->outSingle.length() + st->outRange.length() + + (st->defTrans == 0 ? 0 : 1); + sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex; + if ( redFsm->anyActions() ) + sizeWithoutInds += arrayTypeSize(redFsm->maxActListId) * totalIndex; + } + + /* If using indicies reduces the size, use them. */ + useIndicies = sizeWithInds < sizeWithoutInds; +} + +std::ostream &OCamlFTabCodeGen::TO_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->toStateAction != 0 ) + act = state->toStateAction->actListId+1; + out << act; + return out; +} + +std::ostream &OCamlFTabCodeGen::FROM_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->fromStateAction != 0 ) + act = state->fromStateAction->actListId+1; + out << act; + return out; +} + +std::ostream &OCamlFTabCodeGen::EOF_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->eofAction != 0 ) + act = state->eofAction->actListId+1; + out << act; + return out; +} + + +/* Write out the function for a transition. */ +std::ostream &OCamlFTabCodeGen::TRANS_ACTION( RedTransAp *trans ) +{ + int action = 0; + if ( trans->action != 0 ) + action = trans->action->actListId+1; + out << action; + return out; +} + +/* Write out the function switch. This switch is keyed on the values + * of the func index. */ +std::ostream &OCamlFTabCodeGen::TO_STATE_ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numToStateRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, false ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +/* Write out the function switch. This switch is keyed on the values + * of the func index. */ +std::ostream &OCamlFTabCodeGen::FROM_STATE_ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numFromStateRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, false ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlFTabCodeGen::EOF_ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numEofRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, true ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +/* Write out the function switch. This switch is keyed on the values + * of the func index. */ +std::ostream &OCamlFTabCodeGen::ACTION_SWITCH() +{ + /* Loop the actions. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numTransRefs > 0 ) { + /* Write the entry label. */ + out << "\t| " << redAct->actListId+1 << " ->\n"; + + /* Write each action in the list of action items. */ + for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ ) + ACTION( out, item->value, 0, false ); + + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +void OCamlFTabCodeGen::writeData() +{ + if ( redFsm->anyConditions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() ); + COND_OFFSETS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() ); + COND_LENS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() ); + COND_KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() ); + COND_SPACES(); + CLOSE_ARRAY() << + "\n"; + } + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() ); + KEY_OFFSETS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( WIDE_ALPH_TYPE(), K() ); + KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() ); + SINGLE_LENS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() ); + RANGE_LENS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() ); + INDEX_OFFSETS(); + CLOSE_ARRAY() << + "\n"; + + if ( useIndicies ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() ); + INDICIES(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() ); + TRANS_TARGS_WI(); + CLOSE_ARRAY() << + "\n"; + + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() ); + TRANS_ACTIONS_WI(); + CLOSE_ARRAY() << + "\n"; + } + } + else { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() ); + TRANS_TARGS(); + CLOSE_ARRAY() << + "\n"; + + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() ); + TRANS_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + } + + if ( redFsm->anyToStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() ); + TO_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyFromStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() ); + FROM_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() ); + EOF_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofTrans() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() ); + EOF_TRANS(); + CLOSE_ARRAY() << + "\n"; + } + + STATE_IDS(); + + out << "type state = { mutable keys : int; mutable trans : int; }" + << TOP_SEP(); + + out << "exception Goto_match" << TOP_SEP(); + out << "exception Goto_again" << TOP_SEP(); + out << "exception Goto_eof_trans" << TOP_SEP(); +} + +void OCamlFTabCodeGen::writeExec() +{ + testEofUsed = false; + outLabelUsed = false; + initVarTypes(); + + out << + " begin\n"; + +// if ( redFsm->anyRegCurStateRef() ) +// out << klenType ", _ps"; + + out << + " let state = { keys = 0; trans = 0; } in\n" + " let rec do_start () =\n"; + +// if ( redFsm->anyConditions() ) +// out << " " << WIDE_ALPH_TYPE() << " _widec;\n"; + + if ( !noEnd ) { + testEofUsed = true; + out << + " if " << P() << " = " << PE() << " then\n" + " do_test_eof ()\n" + "\telse\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " if " << vCS() << " = " << redFsm->errState->id << " then\n" + " do_out ()\n" + "\telse\n"; + } + out << "\tdo_resume ()\n"; + + out << "and do_resume () =\n"; + + if ( redFsm->anyFromStateActions() ) { + out << + " begin match " << AT( FSA(), vCS() ) << " with\n"; + FROM_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + "\n"; + } + + if ( redFsm->anyConditions() ) + COND_TRANSLATE(); + + out << "\tbegin try\n"; + LOCATE_TRANS(); + out << "\twith Goto_match -> () end;\n"; + + out << + "\tdo_match ()\n"; + + out << "and do_match () =\n"; + + if ( useIndicies ) + out << " state.trans <- " << CAST(transType) << AT( I(), "state.trans" ) << ";\n"; + + out << "\tdo_eof_trans ()\n"; + +// if ( redFsm->anyEofTrans() ) + out << "and do_eof_trans () =\n"; + + if ( redFsm->anyRegCurStateRef() ) + out << " let ps = " << vCS() << " in\n"; + + out << + " " << vCS() << " <- " << AT( TT() ,"state.trans" ) << ";\n" + "\n"; + + if ( redFsm->anyRegActions() ) { + out << + " begin try if " << AT( TA() , "state.trans" ) << " = 0 then\n" + " raise Goto_again;\n" + "\n" + " match " << AT( TA(), "state.trans" ) << " with\n"; + ACTION_SWITCH(); + SWITCH_DEFAULT() << + " with Goto_again -> () end;\n" + "\n"; + } + out << "\tdo_again ()\n"; + +// if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || +// redFsm->anyActionCalls() || redFsm->anyActionRets() ) + out << "\tand do_again () =\n"; + + if ( redFsm->anyToStateActions() ) { + out << + " begin match " << AT( TSA(), vCS() ) << " with\n"; + TO_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + "\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " match " << vCS() << " with\n" + "\t| " << redFsm->errState->id << " -> do_out ()\n" + "\t| _ ->\n"; + } + + out << "\t" << P() << " <- " << P() << " + 1;\n"; + + if ( !noEnd ) { + out << + " if " << P() << " <> " << PE() << " then\n" + " do_resume ()\n" + "\telse do_test_eof ()\n"; + } + else { + out << + " do_resume ()\n"; + } + +// if ( testEofUsed ) + out << "and do_test_eof () =\n"; + + if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) { + out << + " if " << P() << " = " << vEOF() << " then\n" + " begin try\n"; + + if ( redFsm->anyEofTrans() ) { + out << + " if " << AT( ET(), vCS() ) << " > 0 then\n" + " begin\n" + " state.trans <- " << CAST(transType) << "(" << AT( ET(), vCS() ) << " - 1);\n" + " raise Goto_eof_trans;\n" + " end;\n"; + } + + if ( redFsm->anyEofActions() ) { + out << + " begin match " << AT( EA(), vCS() ) << " with\n"; + EOF_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n"; + } + + out << + " with Goto_again -> do_again ()\n" + " | Goto_eof_trans -> do_eof_trans () end\n" + "\n"; + } + else + { + out << "\t()\n"; + } + + if ( outLabelUsed ) + out << " and do_out () = ()\n"; + + out << "\tin do_start ()\n"; + out << " end;\n"; +} + diff --git ragel/mlftable.h ragel/mlftable.h new file mode 100644 index 0000000..da88e84 --- /dev/null +++ ragel/mlftable.h @@ -0,0 +1,56 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 + */ + +#ifndef _MLFTABLE_H +#define _MLFTABLE_H + +#include +#include "mltable.h" + +/* Forwards. */ +//struct CodeGenData; + + +/* + * OCamlFTabCodeGen + */ +class OCamlFTabCodeGen : public OCamlTabCodeGen +{ +public: + OCamlFTabCodeGen( ostream &out ) : OCamlTabCodeGen(out) {} +private: + std::ostream &TO_STATE_ACTION_SWITCH(); + std::ostream &FROM_STATE_ACTION_SWITCH(); + std::ostream &EOF_ACTION_SWITCH(); + std::ostream &ACTION_SWITCH(); + + virtual std::ostream &TO_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &FROM_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &EOF_ACTION( RedStateAp *state ); + virtual std::ostream &TRANS_ACTION( RedTransAp *trans ); + virtual void writeData(); + virtual void writeExec(); + virtual void calcIndexSize(); +}; + +#endif diff --git ragel/mlgoto.cpp ragel/mlgoto.cpp new file mode 100644 index 0000000..0c9d298 --- /dev/null +++ ragel/mlgoto.cpp @@ -0,0 +1,821 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 "ragel.h" +#include "mlgoto.h" +#include "redfsm.h" +#include "bstmap.h" +#include "gendata.h" + +/* Emit the goto to take for a given transition. */ +std::ostream &OCamlGotoCodeGen::TRANS_GOTO( RedTransAp *trans, int level ) +{ + out << TABS(level) << "tr" << trans->id << " ()"; + return out; +} + +std::ostream &OCamlGotoCodeGen::TO_STATE_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numToStateRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlGotoCodeGen::FROM_STATE_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numFromStateRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlGotoCodeGen::EOF_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numEofRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, true ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlGotoCodeGen::ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numTransRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + out << "\t()\n"; + } + } + + genLineDirective( out ); + return out; +} + +void OCamlGotoCodeGen::GOTO_HEADER( RedStateAp *state ) +{ + /* Label the state. */ + out << "| " << state->id << " ->\n"; +} + + +void OCamlGotoCodeGen::emitSingleSwitch( RedStateAp *state ) +{ + /* Load up the singles. */ + int numSingles = state->outSingle.length(); + RedTransEl *data = state->outSingle.data; + + if ( numSingles == 1 ) { + /* If there is a single single key then write it out as an if. */ + out << "\tif " << GET_WIDE_KEY(state) << " = " << + KEY(data[0].lowKey) << " then\n\t\t"; + + /* Virtual function for writing the target of the transition. */ + TRANS_GOTO(data[0].value, 0) << " else\n"; + } + else if ( numSingles > 1 ) { + /* Write out single keys in a switch if there is more than one. */ + out << "\tmatch " << GET_WIDE_KEY(state) << " with\n"; + + /* Write out the single indicies. */ + for ( int j = 0; j < numSingles; j++ ) { + out << "\t\t| " << ALPHA_KEY(data[j].lowKey) << " -> "; + TRANS_GOTO(data[j].value, 0) << "\n"; + } + + out << "\t\t| _ ->\n"; + } +} + +void OCamlGotoCodeGen::emitRangeBSearch( RedStateAp *state, int level, int low, int high, RedTransAp* def) +{ + /* Get the mid position, staying on the lower end of the range. */ + int mid = (low + high) >> 1; + RedTransEl *data = state->outRange.data; + + /* Determine if we need to look higher or lower. */ + bool anyLower = mid > low; + bool anyHigher = mid < high; + + /* Determine if the keys at mid are the limits of the alphabet. */ + bool limitLow = data[mid].lowKey == keyOps->minKey; + bool limitHigh = data[mid].highKey == keyOps->maxKey; + + if ( anyLower && anyHigher ) { + /* Can go lower and higher than mid. */ + out << TABS(level) << "if " << GET_WIDE_KEY(state) << " < " << + KEY(data[mid].lowKey) << " then begin\n"; + emitRangeBSearch( state, level+1, low, mid-1, def ); + out << TABS(level) << " end else if " << GET_WIDE_KEY(state) << " > " << + KEY(data[mid].highKey) << " then begin\n"; + emitRangeBSearch( state, level+1, mid+1, high, def ); + out << TABS(level) << " end else\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n"; + } + else if ( anyLower && !anyHigher ) { + /* Can go lower than mid but not higher. */ + out << TABS(level) << "if " << GET_WIDE_KEY(state) << " < " << + KEY(data[mid].lowKey) << " then begin\n"; + emitRangeBSearch( state, level+1, low, mid-1, def ); + + /* if the higher is the highest in the alphabet then there is no + * sense testing it. */ + if ( limitHigh ) { + out << TABS(level) << " end else\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n"; + } + else { + out << TABS(level) << " end else if " << GET_WIDE_KEY(state) << " <= " << + KEY(data[mid].highKey) << " then\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n" << TABS(level) << "else\n"; + TRANS_GOTO(def, level+1) << "\n"; + } + } + else if ( !anyLower && anyHigher ) { + /* Can go higher than mid but not lower. */ + out << TABS(level) << "if " << GET_WIDE_KEY(state) << " > " << + KEY(data[mid].highKey) << " then begin\n"; + emitRangeBSearch( state, level+1, mid+1, high, def ); + + /* If the lower end is the lowest in the alphabet then there is no + * sense testing it. */ + if ( limitLow ) { + out << TABS(level) << " end else\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n"; + } + else { + out << TABS(level) << " end else if " << GET_WIDE_KEY(state) << " >= " << + KEY(data[mid].lowKey) << " then\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n" << TABS(level) << "else\n"; + TRANS_GOTO(def, level+1) << "\n"; + } + } + else { + /* Cannot go higher or lower than mid. It's mid or bust. What + * tests to do depends on limits of alphabet. */ + if ( !limitLow && !limitHigh ) { + out << TABS(level) << "if " << KEY(data[mid].lowKey) << " <= " << + GET_WIDE_KEY(state) << " && " << GET_WIDE_KEY(state) << " <= " << + KEY(data[mid].highKey) << " then\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n" << TABS(level) << "else\n"; + TRANS_GOTO(def, level+1) << "\n"; + } + else if ( limitLow && !limitHigh ) { + out << TABS(level) << "if " << GET_WIDE_KEY(state) << " <= " << + KEY(data[mid].highKey) << " then\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n" << TABS(level) << "else\n"; + TRANS_GOTO(def, level+1) << "\n"; + } + else if ( !limitLow && limitHigh ) { + out << TABS(level) << "if " << KEY(data[mid].lowKey) << " <= " << + GET_WIDE_KEY(state) << " then\n"; + TRANS_GOTO(data[mid].value, level+1) << "\n" << TABS(level) << "else\n"; + TRANS_GOTO(def, level+1) << "\n"; + } + else { + /* Both high and low are at the limit. No tests to do. */ + TRANS_GOTO(data[mid].value, level+1) << "\n"; + } + } +} + +void OCamlGotoCodeGen::STATE_GOTO_ERROR() +{ + /* Label the state and bail immediately. */ + outLabelUsed = true; + RedStateAp *state = redFsm->errState; + out << "| " << state->id << " ->\n"; + out << " do_out ()\n"; +} + +void OCamlGotoCodeGen::COND_TRANSLATE( GenStateCond *stateCond, int level ) +{ + GenCondSpace *condSpace = stateCond->condSpace; + out << TABS(level) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" << + KEY(condSpace->baseKey) << " + (" << GET_KEY() << + " - " << KEY(keyOps->minKey) << "));\n"; + + for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) { + out << TABS(level) << "if ( "; + CONDITION( out, *csi ); + Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize()); + out << " ) _widec += " << condValOffset << ";\n"; + } +} + +void OCamlGotoCodeGen::emitCondBSearch( RedStateAp *state, int level, int low, int high ) +{ + /* Get the mid position, staying on the lower end of the range. */ + int mid = (low + high) >> 1; + GenStateCond **data = state->stateCondVect.data; + + /* Determine if we need to look higher or lower. */ + bool anyLower = mid > low; + bool anyHigher = mid < high; + + /* Determine if the keys at mid are the limits of the alphabet. */ + bool limitLow = data[mid]->lowKey == keyOps->minKey; + bool limitHigh = data[mid]->highKey == keyOps->maxKey; + + if ( anyLower && anyHigher ) { + /* Can go lower and higher than mid. */ + out << TABS(level) << "if ( " << GET_KEY() << " < " << + KEY(data[mid]->lowKey) << " ) {\n"; + emitCondBSearch( state, level+1, low, mid-1 ); + out << TABS(level) << "} else if ( " << GET_KEY() << " > " << + KEY(data[mid]->highKey) << " ) {\n"; + emitCondBSearch( state, level+1, mid+1, high ); + out << TABS(level) << "} else {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + else if ( anyLower && !anyHigher ) { + /* Can go lower than mid but not higher. */ + out << TABS(level) << "if ( " << GET_KEY() << " < " << + KEY(data[mid]->lowKey) << " ) {\n"; + emitCondBSearch( state, level+1, low, mid-1 ); + + /* if the higher is the highest in the alphabet then there is no + * sense testing it. */ + if ( limitHigh ) { + out << TABS(level) << "} else {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + else { + out << TABS(level) << "} else if ( " << GET_KEY() << " <= " << + KEY(data[mid]->highKey) << " ) {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + } + else if ( !anyLower && anyHigher ) { + /* Can go higher than mid but not lower. */ + out << TABS(level) << "if ( " << GET_KEY() << " > " << + KEY(data[mid]->highKey) << " ) {\n"; + emitCondBSearch( state, level+1, mid+1, high ); + + /* If the lower end is the lowest in the alphabet then there is no + * sense testing it. */ + if ( limitLow ) { + out << TABS(level) << "} else {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + else { + out << TABS(level) << "} else if ( " << GET_KEY() << " >= " << + KEY(data[mid]->lowKey) << " ) {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + } + else { + /* Cannot go higher or lower than mid. It's mid or bust. What + * tests to do depends on limits of alphabet. */ + if ( !limitLow && !limitHigh ) { + out << TABS(level) << "if ( " << KEY(data[mid]->lowKey) << " <= " << + GET_KEY() << " && " << GET_KEY() << " <= " << + KEY(data[mid]->highKey) << " ) {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + else if ( limitLow && !limitHigh ) { + out << TABS(level) << "if ( " << GET_KEY() << " <= " << + KEY(data[mid]->highKey) << " ) {\n"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + else if ( !limitLow && limitHigh ) { + out << TABS(level) << "if ( " << KEY(data[mid]->lowKey) << " <= " << + GET_KEY() << " )\n {"; + COND_TRANSLATE(data[mid], level+1); + out << TABS(level) << "}\n"; + } + else { + /* Both high and low are at the limit. No tests to do. */ + COND_TRANSLATE(data[mid], level); + } + } +} + +std::ostream &OCamlGotoCodeGen::STATE_GOTOS() +{ + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + if ( st == redFsm->errState ) + STATE_GOTO_ERROR(); + else { + /* Writing code above state gotos. */ + GOTO_HEADER( st ); + out << "\tbegin\n"; + + 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, st->defTrans ); + else + /* Write the default transition. */ + TRANS_GOTO( st->defTrans, 1 ) << "\n"; + + out << "\tend\n"; + } + } + return out; +} + +std::ostream &OCamlGotoCodeGen::TRANSITIONS() +{ + /* Emit any transitions that have functions and that go to + * this state. */ + for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) { + /* Write the label for the transition so it can be jumped to. */ + out << " and tr" << trans->id << " () = "; + + /* Destination state. */ + if ( trans->action != 0 && trans->action->anyCurStateRef() ) + out << "_ps = " << vCS() << ";"; + out << vCS() << " <- " << trans->targ->id << "; "; + + if ( trans->action != 0 ) { + /* Write out the transition func. */ + out << "f" << trans->action->actListId << " ()\n"; + } + else { + /* No code to execute, just loop around. */ + out << "do_again ()\n"; + } + } + return out; +} + +std::ostream &OCamlGotoCodeGen::EXEC_FUNCS() +{ + /* Make labels that set acts and jump to execFuncs. Loop func indicies. */ + for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) { + if ( redAct->numTransRefs > 0 ) { + out << " and f" << redAct->actListId << " () = " << + "state.acts <- " << itoa( redAct->location+1 ) << "; " + "execFuncs ()\n"; + } + } + + out << + "\n" + "and execFuncs () =\n" + " state.nacts <- " << AT( A(), POST_INCR( "state.acts") ) << ";\n" + " begin try while " << POST_DECR("state.nacts") << " > 0 do\n" + " match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + ACTION_SWITCH(); + SWITCH_DEFAULT() << + " done with Goto_again -> () end;\n" + " do_again ()\n"; + return out; +} + +unsigned int OCamlGotoCodeGen::TO_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->toStateAction != 0 ) + act = state->toStateAction->location+1; + return act; +} + +unsigned int OCamlGotoCodeGen::FROM_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->fromStateAction != 0 ) + act = state->fromStateAction->location+1; + return act; +} + +unsigned int OCamlGotoCodeGen::EOF_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->eofAction != 0 ) + act = state->eofAction->location+1; + return act; +} + +std::ostream &OCamlGotoCodeGen::TO_STATE_ACTIONS() +{ + /* Take one off for the psuedo start state. */ + int numStates = redFsm->stateList.length(); + unsigned int *vals = new unsigned int[numStates]; + memset( vals, 0, sizeof(unsigned int)*numStates ); + + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) + vals[st->id] = TO_STATE_ACTION(st); + + out << "\t"; + for ( int st = 0; st < redFsm->nextStateId; st++ ) { + /* Write any eof action. */ + out << vals[st]; + if ( st < numStates-1 ) { + out << ARR_SEP(); + if ( (st+1) % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] vals; + return out; +} + +std::ostream &OCamlGotoCodeGen::FROM_STATE_ACTIONS() +{ + /* Take one off for the psuedo start state. */ + int numStates = redFsm->stateList.length(); + unsigned int *vals = new unsigned int[numStates]; + memset( vals, 0, sizeof(unsigned int)*numStates ); + + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) + vals[st->id] = FROM_STATE_ACTION(st); + + out << "\t"; + for ( int st = 0; st < redFsm->nextStateId; st++ ) { + /* Write any eof action. */ + out << vals[st]; + if ( st < numStates-1 ) { + out << ARR_SEP(); + if ( (st+1) % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] vals; + return out; +} + +std::ostream &OCamlGotoCodeGen::EOF_ACTIONS() +{ + /* Take one off for the psuedo start state. */ + int numStates = redFsm->stateList.length(); + unsigned int *vals = new unsigned int[numStates]; + memset( vals, 0, sizeof(unsigned int)*numStates ); + + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) + vals[st->id] = EOF_ACTION(st); + + out << "\t"; + for ( int st = 0; st < redFsm->nextStateId; st++ ) { + /* Write any eof action. */ + out << vals[st]; + if ( st < numStates-1 ) { + out << ARR_SEP(); + if ( (st+1) % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] vals; + return out; +} + +std::ostream &OCamlGotoCodeGen::FINISH_CASES() +{ + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* States that are final and have an out action need a case. */ + if ( st->eofAction != 0 ) { + /* Write the case label. */ + out << "\t\t| " << st->id << " -> "; + + /* Write the goto func. */ + out << "f" << st->eofAction->actListId << " ()\n"; + } + } + + return out; +} + +void OCamlGotoCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish ) +{ + ret << "begin " << vCS() << " <- " << gotoDest << "; " << + CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlGotoCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) +{ + ret << "begin " << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, 0, inFinish ); + ret << "); " << CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlGotoCodeGen::CURS( ostream &ret, bool inFinish ) +{ + ret << "(_ps)"; +} + +void OCamlGotoCodeGen::TARGS( ostream &ret, bool inFinish, int targState ) +{ + ret << "(" << vCS() << ")"; +} + +void OCamlGotoCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish ) +{ + ret << vCS() << " <- " << nextDest << ";"; +} + +void OCamlGotoCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) +{ + ret << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, 0, inFinish ); + ret << ");"; +} + +void OCamlGotoCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish ) +{ + if ( prePushExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, prePushExpr, 0, false ); + } + + ret << "begin " << AT( STACK(), POST_INCR(TOP()) ) << " <- " << vCS() << "; "; + ret << vCS() << " <- " << callDest << "; " << CTRL_FLOW() << "raise Goto_again end "; + + if ( prePushExpr != 0 ) + ret << "end"; +} + +void OCamlGotoCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ) +{ + if ( prePushExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, prePushExpr, 0, false ); + } + + ret << "begin " << AT(STACK(), POST_INCR(TOP()) ) << " <- " << vCS() << "; " << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, targState, inFinish ); + ret << "); " << CTRL_FLOW() << "raise Goto_again end "; + + if ( prePushExpr != 0 ) + ret << "end"; +} + +void OCamlGotoCodeGen::RET( ostream &ret, bool inFinish ) +{ + ret << "begin " << vCS() << " <- " << AT(STACK(), PRE_DECR(TOP()) ) << "; "; + + if ( postPopExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, postPopExpr, 0, false ); + ret << "end "; + } + + ret << CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlGotoCodeGen::BREAK( ostream &ret, int targState ) +{ + outLabelUsed = true; + ret << "begin " << P() << " <- " << P() << " + 1; " << CTRL_FLOW() << "raise Goto_out end"; +} + +void OCamlGotoCodeGen::writeData() +{ + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() ); + ACTIONS_ARRAY(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyToStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() ); + TO_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyFromStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() ); + FROM_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() ); + EOF_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + STATE_IDS(); + + out << "type state = { mutable acts : " << ARRAY_TYPE(redFsm->maxActionLoc) << + " ; mutable nacts : " << ARRAY_TYPE(redFsm->maxActArrItem) << "; }" + << TOP_SEP(); + + out << "exception Goto_again" << TOP_SEP(); +} + +void OCamlGotoCodeGen::writeExec() +{ + testEofUsed = false; + outLabelUsed = false; + + out << " begin\n"; + +// if ( redFsm->anyRegCurStateRef() ) +// out << " int _ps = 0;\n"; + + if ( redFsm->anyToStateActions() || redFsm->anyRegActions() + || redFsm->anyFromStateActions() ) + { + out << " let state = { acts = 0; nacts = 0; } in\n"; + } + +// if ( redFsm->anyConditions() ) +// out << " " << WIDE_ALPH_TYPE() << " _widec;\n"; + + out << "\n"; + out << " let rec do_start () =\n"; + + if ( !noEnd ) { + testEofUsed = true; + out << + " if " << P() << " = " << PE() << " then\n" + " do_test_eof ()\n" + "\telse\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " if " << vCS() << " = " << redFsm->errState->id << " then\n" + " do_out ()\n" + "\telse\n"; + } + out << "\tdo_resume ()\n"; + + out << "and do_resume () =\n"; + + if ( redFsm->anyFromStateActions() ) { + out << + " state.acts <- " << AT( FSA(), vCS() ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + FROM_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n" + " done;\n" + "\n"; + } + + out << + " begin match " << vCS() << " with\n"; + STATE_GOTOS(); + SWITCH_DEFAULT() << + " end\n" + "\n"; + TRANSITIONS() << + "\n"; + + if ( redFsm->anyRegActions() ) + EXEC_FUNCS() << "\n"; + +// if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || +// redFsm->anyActionCalls() || redFsm->anyActionRets() ) + out << "\tand do_again () =\n"; + + if ( redFsm->anyToStateActions() ) { + out << + " state.acts <- " << AT( TSA(), vCS() ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + TO_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n" + " done;\n" + "\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " match " << vCS() << " with\n" + "\t| " << redFsm->errState->id << " -> do_out ()\n" + "\t| _ ->\n"; + } + + out << "\t" << P() << " <- " << P() << " + 1;\n"; + + if ( !noEnd ) { + out << + " if " << P() << " <> " << PE() << " then\n" + " do_resume ()\n" + "\telse do_test_eof ()\n"; + } + else { + out << + " do_resume ()\n"; + } + +// if ( testEofUsed ) + out << "and do_test_eof () =\n"; + + if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) { + out << + " if " << P() << " = " << vEOF() << " then\n" + " begin\n"; + + if ( redFsm->anyEofTrans() ) { + out << + " match " << vCS() << " with\n"; + + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + if ( st->eofTrans != 0 ) + out << " | " << st->id << " -> tr" << st->eofTrans->id << " ()\n"; + } + + out << "\t| _ -> ();\n"; + } + + if ( redFsm->anyEofActions() ) { + out << + " let __acts = ref " << AT( EA(), vCS() ) << " in\n" + " let __nacts = ref " << AT( A(), "!__acts" ) << " in\n" + " incr __acts;\n" + " begin try while !__nacts > 0 do\n" + " decr __nacts;\n" + " begin match " << AT( A(), POST_INCR("__acts.contents") ) << " with\n"; + EOF_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + " done with Goto_again -> do_again () end;\n"; + } + + out << + " end\n" + "\n"; + } + else + { + out << "\t()\n"; + } + + if ( outLabelUsed ) + out << " and do_out () = ()\n"; + + out << "\tin do_start ()\n"; + out << " end;\n"; +} diff --git ragel/mlgoto.h ragel/mlgoto.h new file mode 100644 index 0000000..50aeb32 --- /dev/null +++ ragel/mlgoto.h @@ -0,0 +1,89 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 + */ + +#ifndef _MLGOTO_H +#define _MLGOTO_H + +#include +#include "mlcodegen.h" + +/* Forwards. */ +//struct CodeGenData; +//struct NameInst; +//struct RedTransAp; +//struct RedStateAp; +//struct GenStateCond; + +/* + * OCamlGotoCodeGen + */ +class OCamlGotoCodeGen : virtual public OCamlCodeGen +{ +public: + OCamlGotoCodeGen( ostream &out ) : OCamlCodeGen(out) {} + std::ostream &TO_STATE_ACTION_SWITCH(); + std::ostream &FROM_STATE_ACTION_SWITCH(); + std::ostream &EOF_ACTION_SWITCH(); + std::ostream &ACTION_SWITCH(); + std::ostream &STATE_GOTOS(); + std::ostream &TRANSITIONS(); + std::ostream &EXEC_FUNCS(); + std::ostream &FINISH_CASES(); + + void GOTO( ostream &ret, int gotoDest, bool inFinish ); + void CALL( ostream &ret, int callDest, int targState, bool inFinish ); + void NEXT( ostream &ret, int nextDest, bool inFinish ); + void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ); + void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ); + void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ); + void CURS( ostream &ret, bool inFinish ); + void TARGS( ostream &ret, bool inFinish, int targState ); + void RET( ostream &ret, bool inFinish ); + void BREAK( ostream &ret, int targState ); + + virtual unsigned int TO_STATE_ACTION( RedStateAp *state ); + virtual unsigned int FROM_STATE_ACTION( RedStateAp *state ); + virtual unsigned int EOF_ACTION( RedStateAp *state ); + + std::ostream &TO_STATE_ACTIONS(); + std::ostream &FROM_STATE_ACTIONS(); + std::ostream &EOF_ACTIONS(); + + void COND_TRANSLATE( GenStateCond *stateCond, int level ); + void emitCondBSearch( RedStateAp *state, int level, int low, int high ); + void STATE_CONDS( RedStateAp *state, bool genDefault ); + + virtual std::ostream &TRANS_GOTO( RedTransAp *trans, int level ); + + void emitSingleSwitch( RedStateAp *state ); + void emitRangeBSearch( RedStateAp *state, int level, int low, int high, RedTransAp* def ); + + /* Called from STATE_GOTOS just before writing the gotos */ + virtual void GOTO_HEADER( RedStateAp *state ); + virtual void STATE_GOTO_ERROR(); + + virtual void writeData(); + virtual void writeExec(); +}; + +#endif diff --git ragel/mltable.cpp ragel/mltable.cpp new file mode 100644 index 0000000..a4e94a3 --- /dev/null +++ ragel/mltable.cpp @@ -0,0 +1,1131 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 "ragel.h" +#include "mltable.h" +#include "redfsm.h" +#include "gendata.h" + +/* Determine if we should use indicies or not. */ +void OCamlTabCodeGen::calcIndexSize() +{ + int sizeWithInds = 0, sizeWithoutInds = 0; + + /* Calculate cost of using with indicies. */ + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + int totalIndex = st->outSingle.length() + st->outRange.length() + + (st->defTrans == 0 ? 0 : 1); + sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex; + } + sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length(); + if ( redFsm->anyActions() ) + sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length(); + + /* Calculate the cost of not using indicies. */ + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + int totalIndex = st->outSingle.length() + st->outRange.length() + + (st->defTrans == 0 ? 0 : 1); + sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex; + if ( redFsm->anyActions() ) + sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex; + } + + /* If using indicies reduces the size, use them. */ + useIndicies = sizeWithInds < sizeWithoutInds; +} + +std::ostream &OCamlTabCodeGen::TO_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->toStateAction != 0 ) + act = state->toStateAction->location+1; + out << act; + return out; +} + +std::ostream &OCamlTabCodeGen::FROM_STATE_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->fromStateAction != 0 ) + act = state->fromStateAction->location+1; + out << act; + return out; +} + +std::ostream &OCamlTabCodeGen::EOF_ACTION( RedStateAp *state ) +{ + int act = 0; + if ( state->eofAction != 0 ) + act = state->eofAction->location+1; + out << act; + return out; +} + + +std::ostream &OCamlTabCodeGen::TRANS_ACTION( RedTransAp *trans ) +{ + /* If there are actions, emit them. Otherwise emit zero. */ + int act = 0; + if ( trans->action != 0 ) + act = trans->action->location+1; + out << act; + return out; +} + +std::ostream &OCamlTabCodeGen::TO_STATE_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numToStateRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlTabCodeGen::FROM_STATE_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numFromStateRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, false ); + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlTabCodeGen::EOF_ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numEofRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " ->\n"; + ACTION( out, act, 0, true ); + } + } + + genLineDirective( out ); + return out; +} + + +std::ostream &OCamlTabCodeGen::ACTION_SWITCH() +{ + /* Walk the list of functions, printing the cases. */ + for ( GenActionList::Iter act = actionList; act.lte(); act++ ) { + /* Write out referenced actions. */ + if ( act->numTransRefs > 0 ) { + /* Write the case label, the action and the case break. */ + out << "\t| " << act->actionId << " -> \n"; + ACTION( out, act, 0, false ); + } + } + + genLineDirective( out ); + return out; +} + +std::ostream &OCamlTabCodeGen::COND_OFFSETS() +{ + out << "\t"; + int totalStateNum = 0, curKeyOffset = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write the key offset. */ + out << curKeyOffset; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + + /* Move the key offset ahead. */ + curKeyOffset += st->stateCondList.length(); + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::KEY_OFFSETS() +{ + out << "\t"; + int totalStateNum = 0, curKeyOffset = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write the key offset. */ + out << curKeyOffset; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + + /* Move the key offset ahead. */ + curKeyOffset += st->outSingle.length() + st->outRange.length()*2; + } + out << "\n"; + return out; +} + + +std::ostream &OCamlTabCodeGen::INDEX_OFFSETS() +{ + out << "\t"; + int totalStateNum = 0, curIndOffset = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write the index offset. */ + out << curIndOffset; + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + + /* Move the index offset ahead. */ + curIndOffset += st->outSingle.length() + st->outRange.length(); + if ( st->defTrans != 0 ) + curIndOffset += 1; + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::COND_LENS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write singles length. */ + out << st->stateCondList.length(); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + + +std::ostream &OCamlTabCodeGen::SINGLE_LENS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write singles length. */ + out << st->outSingle.length(); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::RANGE_LENS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Emit length of range index. */ + out << st->outRange.length(); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::TO_STATE_ACTIONS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + TO_STATE_ACTION(st); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::FROM_STATE_ACTIONS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + FROM_STATE_ACTION(st); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::EOF_ACTIONS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + EOF_ACTION(st); + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::EOF_TRANS() +{ + out << "\t"; + int totalStateNum = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Write any eof action. */ + long trans = 0; + if ( st->eofTrans != 0 ) { + assert( st->eofTrans->pos >= 0 ); + trans = st->eofTrans->pos+1; + } + out << trans; + + if ( !st.last() ) { + out << ARR_SEP(); + if ( ++totalStateNum % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + return out; +} + + +std::ostream &OCamlTabCodeGen::COND_KEYS() +{ + out << '\t'; + int totalTrans = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Loop the state's transitions. */ + for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) { + /* Lower key. */ + out << ALPHA_KEY( sc->lowKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + + /* Upper key. */ + out << ALPHA_KEY( sc->highKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::COND_SPACES() +{ + out << '\t'; + int totalTrans = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Loop the state's transitions. */ + for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) { + /* Cond Space id. */ + out << sc->condSpace->condSpaceId << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::KEYS() +{ + out << '\t'; + int totalTrans = 0; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Loop the singles. */ + for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { + out << ALPHA_KEY( stel->lowKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Loop the state's transitions. */ + for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { + /* Lower key. */ + out << ALPHA_KEY( rtel->lowKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + + /* Upper key. */ + out << ALPHA_KEY( rtel->highKey ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::INDICIES() +{ + int totalTrans = 0; + out << '\t'; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Walk the singles. */ + for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { + out << stel->value->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Walk the ranges. */ + for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { + out << rtel->value->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* The state's default index goes next. */ + if ( st->defTrans != 0 ) { + out << st->defTrans->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::TRANS_TARGS() +{ + int totalTrans = 0; + out << '\t'; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Walk the singles. */ + for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { + RedTransAp *trans = stel->value; + out << trans->targ->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Walk the ranges. */ + for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { + RedTransAp *trans = rtel->value; + out << trans->targ->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* The state's default target state. */ + if ( st->defTrans != 0 ) { + RedTransAp *trans = st->defTrans; + out << trans->targ->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + if ( st->eofTrans != 0 ) { + RedTransAp *trans = st->eofTrans; + trans->pos = totalTrans; + out << trans->targ->id << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + + +std::ostream &OCamlTabCodeGen::TRANS_ACTIONS() +{ + int totalTrans = 0; + out << '\t'; + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + /* Walk the singles. */ + for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) { + RedTransAp *trans = stel->value; + TRANS_ACTION( trans ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* Walk the ranges. */ + for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) { + RedTransAp *trans = rtel->value; + TRANS_ACTION( trans ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + + /* The state's default index goes next. */ + if ( st->defTrans != 0 ) { + RedTransAp *trans = st->defTrans; + TRANS_ACTION( trans ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { + if ( st->eofTrans != 0 ) { + RedTransAp *trans = st->eofTrans; + TRANS_ACTION( trans ) << ARR_SEP(); + if ( ++totalTrans % IALL == 0 ) + out << "\n\t"; + } + } + + /* Output one last number so we don't have to figure out when the last + * entry is and avoid writing a comma. */ + out << 0 << "\n"; + return out; +} + +std::ostream &OCamlTabCodeGen::TRANS_TARGS_WI() +{ + /* Transitions must be written ordered by their id. */ + RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()]; + for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) + transPtrs[trans->id] = trans; + + /* Keep a count of the num of items in the array written. */ + out << '\t'; + int totalStates = 0; + for ( int t = 0; t < redFsm->transSet.length(); t++ ) { + /* Record the position, need this for eofTrans. */ + RedTransAp *trans = transPtrs[t]; + trans->pos = t; + + /* Write out the target state. */ + out << trans->targ->id; + if ( t < redFsm->transSet.length()-1 ) { + out << ARR_SEP(); + if ( ++totalStates % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] transPtrs; + return out; +} + + +std::ostream &OCamlTabCodeGen::TRANS_ACTIONS_WI() +{ + /* Transitions must be written ordered by their id. */ + RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()]; + for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) + transPtrs[trans->id] = trans; + + /* Keep a count of the num of items in the array written. */ + out << '\t'; + int totalAct = 0; + for ( int t = 0; t < redFsm->transSet.length(); t++ ) { + /* Write the function for the transition. */ + RedTransAp *trans = transPtrs[t]; + TRANS_ACTION( trans ); + if ( t < redFsm->transSet.length()-1 ) { + out << ARR_SEP(); + if ( ++totalAct % IALL == 0 ) + out << "\n\t"; + } + } + out << "\n"; + delete[] transPtrs; + return out; +} + +void OCamlTabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish ) +{ + ret << "begin " << vCS() << " <- " << gotoDest << "; " << + CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlTabCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) +{ + ret << "begin " << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, 0, inFinish ); + ret << "); " << CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlTabCodeGen::CURS( ostream &ret, bool inFinish ) +{ + ret << "(_ps)"; +} + +void OCamlTabCodeGen::TARGS( ostream &ret, bool inFinish, int targState ) +{ + ret << "(" << vCS() << ")"; +} + +void OCamlTabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish ) +{ + ret << vCS() << " <- " << nextDest << ";"; +} + +void OCamlTabCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) +{ + ret << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, 0, inFinish ); + ret << ");"; +} + +void OCamlTabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish ) +{ + if ( prePushExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, prePushExpr, 0, false ); + } + + ret << "begin " << AT( STACK(), POST_INCR(TOP()) ) << " <- " << vCS() << "; "; + ret << vCS() << " <- " << callDest << "; " << CTRL_FLOW() << "raise Goto_again end "; + + if ( prePushExpr != 0 ) + ret << "end"; +} + +void OCamlTabCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ) +{ + if ( prePushExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, prePushExpr, 0, false ); + } + + ret << "begin " << AT(STACK(), POST_INCR(TOP()) ) << " <- " << vCS() << "; " << vCS() << " <- ("; + INLINE_LIST( ret, ilItem->children, targState, inFinish ); + ret << "); " << CTRL_FLOW() << "raise Goto_again end "; + + if ( prePushExpr != 0 ) + ret << "end"; +} + +void OCamlTabCodeGen::RET( ostream &ret, bool inFinish ) +{ + ret << "begin " << vCS() << " <- " << AT(STACK(), PRE_DECR(TOP()) ) << "; "; + + if ( postPopExpr != 0 ) { + ret << "begin "; + INLINE_LIST( ret, postPopExpr, 0, false ); + ret << "end "; + } + + ret << CTRL_FLOW() << "raise Goto_again end"; +} + +void OCamlTabCodeGen::BREAK( ostream &ret, int targState ) +{ + outLabelUsed = true; + ret << "begin " << P() << " <- " << P() << " + 1; " << CTRL_FLOW() << "raise Goto_out end"; +} + +void OCamlTabCodeGen::writeData() +{ + /* If there are any transtion functions then output the array. If there + * are none, don't bother emitting an empty array that won't be used. */ + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() ); + ACTIONS_ARRAY(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyConditions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() ); + COND_OFFSETS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() ); + COND_LENS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() ); + COND_KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() ); + COND_SPACES(); + CLOSE_ARRAY() << + "\n"; + } + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() ); + KEY_OFFSETS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( WIDE_ALPH_TYPE(), K() ); + KEYS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() ); + SINGLE_LENS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() ); + RANGE_LENS(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() ); + INDEX_OFFSETS(); + CLOSE_ARRAY() << + "\n"; + + if ( useIndicies ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() ); + INDICIES(); + CLOSE_ARRAY() << + "\n"; + + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() ); + TRANS_TARGS_WI(); + CLOSE_ARRAY() << + "\n"; + + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() ); + TRANS_ACTIONS_WI(); + CLOSE_ARRAY() << + "\n"; + } + } + else { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() ); + TRANS_TARGS(); + CLOSE_ARRAY() << + "\n"; + + if ( redFsm->anyActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() ); + TRANS_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + } + + if ( redFsm->anyToStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() ); + TO_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyFromStateActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() ); + FROM_STATE_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofActions() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() ); + EOF_ACTIONS(); + CLOSE_ARRAY() << + "\n"; + } + + if ( redFsm->anyEofTrans() ) { + OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() ); + EOF_TRANS(); + CLOSE_ARRAY() << + "\n"; + } + + STATE_IDS(); + + out << "type state = { mutable keys : int; mutable trans : int; mutable acts : int; mutable nacts : int; }" + << TOP_SEP(); + + out << "exception Goto_match" << TOP_SEP(); + out << "exception Goto_again" << TOP_SEP(); + out << "exception Goto_eof_trans" << TOP_SEP(); +} + +void OCamlTabCodeGen::LOCATE_TRANS() +{ + out << + " state.keys <- " << AT( KO(), vCS() ) << ";\n" + " state.trans <- " << CAST(transType) << AT( IO(), vCS() ) << ";\n" + "\n" + " let klen = " << AT( SL(), vCS() ) << " in\n" + " if klen > 0 then begin\n" + " let lower : " << signedKeysType << " ref = ref state.keys in\n" + " let upper : " << signedKeysType << " ref = ref " << CAST(signedKeysType) << + "(state.keys + klen - 1) in\n" + " while !upper >= !lower do\n" + " let mid = " << CAST(signedKeysType) << " (!lower + ((!upper - !lower) / 2)) in\n" + " if Char.code " << GET_WIDE_KEY() << " < " << AT( K(), "mid" ) << " then\n" + " upper := " << CAST(signedKeysType) << " (mid - 1)\n" + " else if Char.code " << GET_WIDE_KEY() << " > " << AT( K(), "mid" ) << " then\n" + " lower := " << CAST(signedKeysType) << " (mid + 1)\n" + " else begin\n" + " state.trans <- state.trans + " << CAST(transType) << " (mid - state.keys);\n" + " raise Goto_match;\n" + " end\n" + " done;\n" + " state.keys <- state.keys + " << CAST(keysType) << " klen;\n" + " state.trans <- state.trans + " << CAST(transType) << " klen;\n" + " end;\n" + "\n" + " let klen = " << AT( RL(), vCS() ) << " in\n" + " if klen > 0 then begin\n" + " let lower : " << signedKeysType << " ref = ref state.keys in\n" + " let upper : " << signedKeysType << " ref = ref " << CAST(signedKeysType) << + "(state.keys + (klen * 2) - 2) in\n" + " while !upper >= !lower do\n" + " let mid = " << CAST(signedKeysType) << " (!lower + (((!upper - !lower) / 2) land (lnot 1))) in\n" + " if Char.code " << GET_WIDE_KEY() << " < " << AT( K() , "mid" ) << " then\n" + " upper := " << CAST(signedKeysType) << " (mid - 2)\n" + " else if Char.code " << GET_WIDE_KEY() << " > " << AT( K(), "mid+1" ) << " then\n" + " lower := " << CAST(signedKeysType) << " (mid + 2)\n" + " else begin\n" + " state.trans <- state.trans + " << CAST(transType) << "((mid - state.keys) / 2);\n" + " raise Goto_match;\n" + " end\n" + " done;\n" + " state.trans <- state.trans + " << CAST(transType) << " klen;\n" + " end;\n" + "\n"; +} + +void OCamlTabCodeGen::COND_TRANSLATE() +{ + out << + " _widec = " << GET_KEY() << ";\n" + " _klen = " << CL() << "[" << vCS() << "];\n" + " _keys = " << CAST(keysType) << " ("<< CO() << "[" << vCS() << "]*2);\n" + " if ( _klen > 0 ) {\n" + " " << signedKeysType << " _lower = _keys;\n" + " " << signedKeysType << " _mid;\n" + " " << signedKeysType << " _upper = " << CAST(signedKeysType) << + " (_keys + (_klen<<1) - 2);\n" + " while (true) {\n" + " if ( _upper < _lower )\n" + " break;\n" + "\n" + " _mid = " << CAST(signedKeysType) << + " (_lower + (((_upper-_lower) >> 1) & ~1));\n" + " if ( " << GET_WIDE_KEY() << " < " << CK() << "[_mid] )\n" + " _upper = " << CAST(signedKeysType) << " (_mid - 2);\n" + " else if ( " << GET_WIDE_KEY() << " > " << CK() << "[_mid+1] )\n" + " _lower = " << CAST(signedKeysType) << " (_mid + 2);\n" + " else {\n" + " switch ( " << C() << "[" << CO() << "[" << vCS() << "]" + " + ((_mid - _keys)>>1)] ) {\n"; + + for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) { + GenCondSpace *condSpace = csi; + out << " case " << condSpace->condSpaceId << ": {\n"; + out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" << + KEY(condSpace->baseKey) << " + (" << GET_KEY() << + " - " << KEY(keyOps->minKey) << "));\n"; + + for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) { + out << TABS(2) << "if ( "; + CONDITION( out, *csi ); + Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize()); + out << " ) _widec += " << condValOffset << ";\n"; + } + + out << + " break;\n" + " }\n"; + } + + SWITCH_DEFAULT(); + + out << + " }\n" + " break;\n" + " }\n" + " }\n" + " }\n" + "\n"; +} + +void OCamlTabCodeGen::writeExec() +{ + testEofUsed = false; + outLabelUsed = false; + initVarTypes(); + + out << + " begin\n"; +// " " << klenType << " _klen"; + +// if ( redFsm->anyRegCurStateRef() ) +// out << ", _ps"; + +/* + out << " " << transType << " _trans;\n"; + + if ( redFsm->anyConditions() ) + out << " " << WIDE_ALPH_TYPE() << " _widec;\n"; + + if ( redFsm->anyToStateActions() || redFsm->anyRegActions() + || redFsm->anyFromStateActions() ) + { + out << + " int _acts;\n" + " int _nacts;\n"; + } + + out << + " " << keysType << " _keys;\n" + "\n"; +// " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n" +*/ + + out << + " let state = { keys = 0; trans = 0; acts = 0; nacts = 0; } in\n" + " let rec do_start () =\n"; + if ( !noEnd ) { + testEofUsed = true; + out << + " if " << P() << " = " << PE() << " then\n" + " do_test_eof ()\n" + "\telse\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " if " << vCS() << " = " << redFsm->errState->id << " then\n" + " do_out ()\n" + "\telse\n"; + } + out << "\tdo_resume ()\n"; + + out << "and do_resume () =\n"; + + if ( redFsm->anyFromStateActions() ) { + out << + " state.acts <- " << AT( FSA(), vCS() ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + FROM_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n" + " done;\n" + "\n"; + } + + if ( redFsm->anyConditions() ) + COND_TRANSLATE(); + + out << "\tbegin try\n"; + LOCATE_TRANS(); + out << "\twith Goto_match -> () end;\n"; + + out << + "\tdo_match ()\n"; + + out << "and do_match () =\n"; + + if ( useIndicies ) + out << " state.trans <- " << CAST(transType) << AT( I(), "state.trans" ) << ";\n"; + + out << "\tdo_eof_trans ()\n"; + +// if ( redFsm->anyEofTrans() ) + out << "and do_eof_trans () =\n"; + + if ( redFsm->anyRegCurStateRef() ) + out << " let ps = " << vCS() << " in\n"; + + out << + " " << vCS() << " <- " << AT( TT() ,"state.trans" ) << ";\n" + "\n"; + + if ( redFsm->anyRegActions() ) { + out << + "\tbegin try\n" + " match " << AT( TA(), "state.trans" ) << " with\n" + "\t| 0 -> raise Goto_again\n" + "\t| _ ->\n" + " state.acts <- " << AT( TA(), "state.trans" ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + " done\n" + "\twith Goto_again -> () end;\n"; + } + out << "\tdo_again ()\n"; + +// if ( redFsm->anyRegActions() || redFsm->anyActionGotos() || +// redFsm->anyActionCalls() || redFsm->anyActionRets() ) + out << "\tand do_again () =\n"; + + if ( redFsm->anyToStateActions() ) { + out << + " state.acts <- " << AT( TSA(), vCS() ) << ";\n" + " state.nacts <- " << AT( A(), POST_INCR("state.acts") ) << ";\n" + " while " << POST_DECR("state.nacts") << " > 0 do\n" + " begin match " << AT( A(), POST_INCR("state.acts") ) << " with\n"; + TO_STATE_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end\n" + " done;\n" + "\n"; + } + + if ( redFsm->errState != 0 ) { + outLabelUsed = true; + out << + " match " << vCS() << " with\n" + "\t| " << redFsm->errState->id << " -> do_out ()\n" + "\t| _ ->\n"; + } + + out << "\t" << P() << " <- " << P() << " + 1;\n"; + + if ( !noEnd ) { + out << + " if " << P() << " <> " << PE() << " then\n" + " do_resume ()\n" + "\telse do_test_eof ()\n"; + } + else { + out << + " do_resume ()\n"; + } + +// if ( testEofUsed ) + out << "and do_test_eof () =\n"; + + if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) { + out << + " if " << P() << " = " << vEOF() << " then\n" + " begin try\n"; + + if ( redFsm->anyEofTrans() ) { + out << + " if " << AT( ET(), vCS() ) << " > 0 then\n" + " begin\n" + " state.trans <- " << CAST(transType) << "(" << AT( ET(), vCS() ) << " - 1);\n" + " raise Goto_eof_trans;\n" + " end\n"; + } + + if ( redFsm->anyEofActions() ) { + out << + " let __acts = ref " << AT( EA(), vCS() ) << " in\n" + " let __nacts = ref " << AT( A(), "!__acts" ) << " in\n" + " incr __acts;\n" + " while !__nacts > 0 do\n" + " decr __nacts;\n" + " begin match " << AT( A(), POST_INCR("__acts.contents") ) << " with\n"; + EOF_ACTION_SWITCH(); + SWITCH_DEFAULT() << + " end;\n" + " done\n"; + } + + out << + " with Goto_again -> do_again ()\n" + " | Goto_eof_trans -> do_eof_trans () end\n" + "\n"; + } + else + { + out << "\t()\n"; + } + + if ( outLabelUsed ) + out << " and do_out () = ()\n"; + + out << "\tin do_start ()\n"; + out << " end;\n"; +} + +void OCamlTabCodeGen::initVarTypes() +{ + int klenMax = MAX(MAX(redFsm->maxCondLen, redFsm->maxRangeLen), + redFsm->maxSingleLen); + int keysMax = MAX(MAX(redFsm->maxKeyOffset, klenMax), + redFsm->maxCondOffset); + int transMax = MAX(MAX(redFsm->maxIndex+1, redFsm->maxIndexOffset), keysMax); + transMax = MAX(transMax, klenMax); + transType = ARRAY_TYPE(transMax); + klenType = ARRAY_TYPE(klenMax); + keysType = ARRAY_TYPE(keysMax); + signedKeysType = ARRAY_TYPE(keysMax, true); +} diff --git ragel/mltable.h ragel/mltable.h new file mode 100644 index 0000000..505d378 --- /dev/null +++ ragel/mltable.h @@ -0,0 +1,102 @@ +/* + * Copyright 2001-2006 Adrian Thurston + * 2004 Erich Ocean + * 2005 Alan West + */ + +/* 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 + */ + +#ifndef _OCAMLTABCODEGEN_H +#define _OCAMLTABCODEGEN_H + +#include +#include "mlcodegen.h" + +/* Forwards. */ +/* +struct CodeGenData; +struct NameInst; +struct RedTransAp; +struct RedStateAp; +*/ + +/* + * OCamlTabCodeGen + */ +class OCamlTabCodeGen : public OCamlCodeGen +{ +public: + OCamlTabCodeGen( ostream &out ) : OCamlCodeGen(out) {} + virtual ~OCamlTabCodeGen() { } + virtual void writeData(); + virtual void writeExec(); + +protected: + std::ostream &TO_STATE_ACTION_SWITCH(); + std::ostream &FROM_STATE_ACTION_SWITCH(); + std::ostream &EOF_ACTION_SWITCH(); + std::ostream &ACTION_SWITCH(); + + std::ostream &COND_KEYS(); + std::ostream &COND_SPACES(); + std::ostream &KEYS(); + std::ostream &INDICIES(); + std::ostream &COND_OFFSETS(); + std::ostream &KEY_OFFSETS(); + std::ostream &INDEX_OFFSETS(); + std::ostream &COND_LENS(); + std::ostream &SINGLE_LENS(); + std::ostream &RANGE_LENS(); + std::ostream &TO_STATE_ACTIONS(); + std::ostream &FROM_STATE_ACTIONS(); + std::ostream &EOF_ACTIONS(); + std::ostream &EOF_TRANS(); + std::ostream &TRANS_TARGS(); + std::ostream &TRANS_ACTIONS(); + std::ostream &TRANS_TARGS_WI(); + std::ostream &TRANS_ACTIONS_WI(); + + void LOCATE_TRANS(); + + void COND_TRANSLATE(); + + void GOTO( ostream &ret, int gotoDest, bool inFinish ); + void CALL( ostream &ret, int callDest, int targState, bool inFinish ); + void NEXT( ostream &ret, int nextDest, bool inFinish ); + void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ); + void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ); + void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ); + void CURS( ostream &ret, bool inFinish ); + void TARGS( ostream &ret, bool inFinish, int targState ); + void RET( ostream &ret, bool inFinish ); + void BREAK( ostream &ret, int targState ); + + virtual std::ostream &TO_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &FROM_STATE_ACTION( RedStateAp *state ); + virtual std::ostream &EOF_ACTION( RedStateAp *state ); + virtual std::ostream &TRANS_ACTION( RedTransAp *trans ); + virtual void calcIndexSize(); + + void initVarTypes(); + string klenType; + string keysType; + string signedKeysType; + string transType; +}; + +#endif diff --git ragel/xmlcodegen.cpp ragel/xmlcodegen.cpp index 45f7e84..584784a 100644 --- ragel/xmlcodegen.cpp +++ ragel/xmlcodegen.cpp @@ -1407,6 +1407,7 @@ void InputData::writeLanguage( std::ostream &out ) case HostLang::Java: out << "Java"; break; case HostLang::Ruby: out << "Ruby"; break; case HostLang::CSharp: out << "C#"; break; + case HostLang::OCaml: out << "OCaml"; break; } out << "\""; }