From 4683bd51ec92552d0ed7735634c6ab3d9721bfed Mon Sep 17 00:00:00 2001 From: thurston Date: Tue, 30 Jan 2007 21:13:05 +0000 Subject: [PATCH] Did some more reorganization in support of separating out a redfsm.a library from code generation. The makeCodeGen function is now a callback which will be executed by the redfsm.a library and implemented by the code generator executable. git-svn-id: http://svn.complang.org/ragel/trunk@39 052ea7fc-9027-0410-9066-f65837a77df0 --- rlcodegen/fsmcodegen.cpp | 95 +++++++++++++++++++++++++++++++++++++++++- rlcodegen/fsmcodegen.h | 5 ++- rlcodegen/gendata.cpp | 105 ++++------------------------------------------- rlcodegen/gendata.h | 3 +- 4 files changed, 107 insertions(+), 101 deletions(-) diff --git a/rlcodegen/fsmcodegen.cpp b/rlcodegen/fsmcodegen.cpp index 8eab761..34bd14a 100644 --- a/rlcodegen/fsmcodegen.cpp +++ b/rlcodegen/fsmcodegen.cpp @@ -29,6 +29,16 @@ #include #include +#include "tabcodegen.h" +#include "ftabcodegen.h" +#include "flatcodegen.h" +#include "fflatcodegen.h" +#include "gotocodegen.h" +#include "fgotocodegen.h" +#include "ipgotocodegen.h" +#include "splitcodegen.h" +#include "javacodegen.h" + using std::ostream; using std::ostringstream; using std::string; @@ -49,12 +59,93 @@ bool onlyWhitespace( char *str ) return true; } +/* Generate the codegen depending on the command line options given. */ +FsmCodeGen *makeCodeGen( CodeGenData *cgd ) +{ + FsmCodeGen *codeGen = 0; + switch ( hostLangType ) { + case CCode: + switch ( codeStyle ) { + case GenTables: + codeGen = new CTabCodeGen(cgd->out); + break; + case GenFTables: + codeGen = new CFTabCodeGen(cgd->out); + break; + case GenFlat: + codeGen = new CFlatCodeGen(cgd->out); + break; + case GenFFlat: + codeGen = new CFFlatCodeGen(cgd->out); + break; + case GenGoto: + codeGen = new CGotoCodeGen(cgd->out); + break; + case GenFGoto: + codeGen = new CFGotoCodeGen(cgd->out); + break; + case GenIpGoto: + codeGen = new CIpGotoCodeGen(cgd->out); + break; + case GenSplit: + codeGen = new CSplitCodeGen(cgd->out); + break; + } + break; + + case DCode: + switch ( codeStyle ) { + case GenTables: + codeGen = new DTabCodeGen(cgd->out); + break; + case GenFTables: + codeGen = new DFTabCodeGen(cgd->out); + break; + case GenFlat: + codeGen = new DFlatCodeGen(cgd->out); + break; + case GenFFlat: + codeGen = new DFFlatCodeGen(cgd->out); + break; + case GenGoto: + codeGen = new DGotoCodeGen(cgd->out); + break; + case GenFGoto: + codeGen = new DFGotoCodeGen(cgd->out); + break; + case GenIpGoto: + codeGen = new DIpGotoCodeGen(cgd->out); + break; + case GenSplit: + codeGen = new DSplitCodeGen(cgd->out); + break; + } + break; + + case JavaCode: + switch ( codeStyle ) { + case GenTables: + codeGen = new JavaTabCodeGen(cgd->out); + break; + default: + assert(false); + break; + } + break; + } + + codeGen->cgd = cgd; + + return codeGen; +} + + /* Init code gen with in parameters. */ FsmCodeGen::FsmCodeGen( ostream &out ) : - cgd(0), redFsm(0), - out(out) + out(out), + cgd(0) { } diff --git a/rlcodegen/fsmcodegen.h b/rlcodegen/fsmcodegen.h index 42d0fd0..d459925 100644 --- a/rlcodegen/fsmcodegen.h +++ b/rlcodegen/fsmcodegen.h @@ -174,16 +174,17 @@ protected: virtual void calcIndexSize() {} /* Are there any regular transition functions, any out transition functions. */ - CodeGenData *cgd; RedFsmAp *redFsm; bool outLabelUsed; bool againLabelUsed; -protected: ostream &out; bool useIndicies; + +public: + CodeGenData *cgd; }; class CCodeGen : virtual public FsmCodeGen diff --git a/rlcodegen/gendata.cpp b/rlcodegen/gendata.cpp index 05aae0e..7cedf61 100644 --- a/rlcodegen/gendata.cpp +++ b/rlcodegen/gendata.cpp @@ -23,15 +23,6 @@ /* Code Generators. */ #include "gvdotgen.h" -#include "tabcodegen.h" -#include "ftabcodegen.h" -#include "flatcodegen.h" -#include "fflatcodegen.h" -#include "gotocodegen.h" -#include "fgotocodegen.h" -#include "ipgotocodegen.h" -#include "splitcodegen.h" -#include "javacodegen.h" #include @@ -285,83 +276,6 @@ void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum } -/* Generate the codegen depending on the command line options given. */ -void CodeGenData::makeCodeGen() -{ - switch ( hostLangType ) { - case CCode: - switch ( codeStyle ) { - case GenTables: - codeGen = new CTabCodeGen(out); - break; - case GenFTables: - codeGen = new CFTabCodeGen(out); - break; - case GenFlat: - codeGen = new CFlatCodeGen(out); - break; - case GenFFlat: - codeGen = new CFFlatCodeGen(out); - break; - case GenGoto: - codeGen = new CGotoCodeGen(out); - break; - case GenFGoto: - codeGen = new CFGotoCodeGen(out); - break; - case GenIpGoto: - codeGen = new CIpGotoCodeGen(out); - break; - case GenSplit: - codeGen = new CSplitCodeGen(out); - break; - } - break; - - case DCode: - switch ( codeStyle ) { - case GenTables: - codeGen = new DTabCodeGen(out); - break; - case GenFTables: - codeGen = new DFTabCodeGen(out); - break; - case GenFlat: - codeGen = new DFlatCodeGen(out); - break; - case GenFFlat: - codeGen = new DFFlatCodeGen(out); - break; - case GenGoto: - codeGen = new DGotoCodeGen(out); - break; - case GenFGoto: - codeGen = new DFGotoCodeGen(out); - break; - case GenIpGoto: - codeGen = new DIpGotoCodeGen(out); - break; - case GenSplit: - codeGen = new DSplitCodeGen(out); - break; - } - break; - - case JavaCode: - switch ( codeStyle ) { - case GenTables: - codeGen = new JavaTabCodeGen(out); - break; - default: - assert(false); - break; - } - break; - } - - codeGen->cgd = this; -} - CondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey ) { for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) { @@ -681,9 +595,6 @@ void CodeGenData::analyzeMachine() /* Set the maximums of various values used for deciding types. */ setValueLimits(); - - /* Determine if we should use indicies. */ - codeGen->calcIndexSize(); } /* Generate the code for an fsm. Assumes parseData is set up properly. Called @@ -696,7 +607,7 @@ void CodeGenData::prepareMachine() /* Do this before distributing transitions out to singles and defaults * makes life easier. */ - Key maxKey = findMaxKey(); + redFsm->maxKey = findMaxKey(); redFsm->assignActionLocs(); @@ -744,16 +655,18 @@ void CodeGenData::prepareMachine() if ( codeStyle == GenIpGoto || codeStyle == GenSplit ) redFsm->setInTrans(); - /* Make a code generator that will output the header/code. */ - if ( codeGen == 0 ) - makeCodeGen(); - codeGen->redFsm = redFsm; - /* 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(); - redFsm->maxKey = maxKey; + + /* Make a code generator that will output the header/code. */ + if ( codeGen == 0 ) + codeGen = makeCodeGen( this ); + codeGen->redFsm = redFsm; + + /* Determine if we should use indicies. */ + codeGen->calcIndexSize(); } void CodeGenData::generateGraphviz() diff --git a/rlcodegen/gendata.h b/rlcodegen/gendata.h index fd0f7af..ec40216 100644 --- a/rlcodegen/gendata.h +++ b/rlcodegen/gendata.h @@ -142,7 +142,6 @@ struct CodeGenData bool setAlphType( char *data ); - void makeCodeGen(); void generateGraphviz(); void resolveTargetStates( InlineList *inlineList ); Key findMaxKey(); @@ -167,4 +166,6 @@ extern CodeGenData *cgd; void lineDirective( ostream &out, char *fileName, int line ); void genLineDirective( ostream &out ); +FsmCodeGen *makeCodeGen( CodeGenData *cgd ); + #endif /* _GENDATA_H */ -- 2.7.4