From bd26cf094ec776940fb74075970921f8ee2c0590 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Sun, 30 Nov 2014 12:43:17 -0500 Subject: first cut at string-based tables (ppragel integration) TableArray can now generate strings instead of arrays. Conversion technique taken from ppragel.rl. Test suite passes, though with some warnings. refs #24. --- ragel/cdcodegen.cpp | 34 +++++++++-------- ragel/cdcodegen.h | 103 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 104 insertions(+), 33 deletions(-) diff --git a/ragel/cdcodegen.cpp b/ragel/cdcodegen.cpp index 804ffce4..7d1bb5c2 100644 --- a/ragel/cdcodegen.cpp +++ b/ragel/cdcodegen.cpp @@ -75,30 +75,34 @@ TableArray::TableArray( FsmCodeGen &codeGen, HostType *hostType, std::string nam name(name), out(codeGen.out), first(true), - ln(0) + ln(0), + str(stringTables) { } void TableArray::OPEN() { - codeGen.OPEN_ARRAY( hostType->TYPE(), name ); - out << "\t"; -} - -void TableArray::KEY( Key key ) -{ - fmt(); - if ( keyOps->isSigned || !hostLang->explicitUnsigned ) - out << key.getVal(); - else - out << (unsigned long) key.getVal() << 'u'; + if ( str ) { + out << "static const char S_" << name << "[] __attribute__((aligned (16))) = \n\t\""; + } + else { + codeGen.OPEN_ARRAY( hostType->TYPE(), name ); + out << "\t"; + } } void TableArray::CLOSE() { - out << "\n"; - codeGen.CLOSE_ARRAY(); - out << "\n"; + if ( str ) { + string type = hostType->TYPE(); + out << "\";\nstatic const " << type << " *" << name << + " = (const " << type << "*) S_" << name << ";\n\n"; + } + else { + out << "\n"; + codeGen.CLOSE_ARRAY(); + out << "\n"; + } } void FsmCodeGen::genLineDirective( ostream &out ) diff --git a/ragel/cdcodegen.h b/ragel/cdcodegen.h index 0edf0c01..21697297 100644 --- a/ragel/cdcodegen.h +++ b/ragel/cdcodegen.h @@ -26,9 +26,11 @@ #include #include +#include #include #include "common.h" #include "gendata.h" +#include "ragel.h" using std::string; using std::ostream; @@ -60,31 +62,95 @@ struct TableArray void fmt() { - if ( ! first ) { - out << ", "; - - if ( ++ln % IALL == 0 ) { - out << "\n\t"; - ln = 0; + if ( str ) { + if ( ! first ) { + if ( ++ln % IALL == 0 ) { + out << "\"\n\t\""; + ln = 0; + } + } + } + else { + if ( ! first ) { + out << ", "; + + if ( ++ln % IALL == 0 ) { + out << "\n\t"; + ln = 0; + } } } - first = false; } - void VAL( long long ll ) { fmt(); out << ll; } - void VAL( long l ) { fmt(); out << l; } - void VAL( int i ) { fmt(); out << i; } - void VAL( short s ) { fmt(); out << s; } - void VAL( char c ) { fmt(); out << c; } + void SVAL( long long value ) + { + char c; + short h; + int i; + long l; + unsigned char *p = 0; + int n = 0; + switch ( hostType->size ) { + case sizeof( char ): + c = value; + p = (unsigned char *)&c; + n = sizeof(char); + break; + case sizeof( short ): + h = value; + p = (unsigned char *)&h; + n = sizeof(short); + break; + case sizeof( int ): + i = value; + p = (unsigned char *)&i; + n = sizeof(int); + break; + case sizeof( long ): + l = value; + p = (unsigned char *)&l; + n = sizeof(long); + break; + } - void VAL( unsigned long long ull ) { fmt(); out << ull; } - void VAL( unsigned long ul ) { fmt(); out << ul; } - void VAL( unsigned int ui ) { fmt(); out << ui; } - void VAL( unsigned short us ) { fmt(); out << us; } - void VAL( unsigned char uc ) { fmt(); out << uc; } + std::ios_base::fmtflags prevFlags = out.flags( std::ios::hex ); + int prevFill = out.fill( '0' ); + + while ( n-- > 0 ) { + out << '\\'; + out << 'x'; + out << std::setw(2) << (unsigned int) *p++; + } - void KEY( Key key ); + out.flags( prevFlags ); + out.fill( prevFill ); + } + + void VAL( long long ll ) { fmt(); if (str) SVAL(ll); else out << ll; } + void VAL( long l ) { fmt(); if (str) SVAL(l); else out << l; } + void VAL( int i ) { fmt(); if (str) SVAL(i); else out << i; } + void VAL( short s ) { fmt(); if (str) SVAL(s); else out << s; } + void VAL( char c ) { fmt(); if (str) SVAL(c); else out << c; } + + void VAL( unsigned long long ull ) { fmt(); if (str) SVAL(ull); else out << ull; } + void VAL( unsigned long ul ) { fmt(); if (str) SVAL(ul); else out << ul; } + void VAL( unsigned int ui ) { fmt(); if (str) SVAL(ui); else out << ui; } + void VAL( unsigned short us ) { fmt(); if (str) SVAL(us); else out << us; } + void VAL( unsigned char uc ) { fmt(); if (str) SVAL(uc); else out << uc; } + + void KEY( Key key ) + { + fmt(); + if ( str ) + SVAL( key.getVal() ); + else { + if ( keyOps->isSigned || !hostLang->explicitUnsigned ) + out << key.getVal(); + else + out << (unsigned long) key.getVal() << 'u'; + } + } FsmCodeGen &codeGen; HostType *hostType; @@ -92,6 +158,7 @@ struct TableArray ostream &out; bool first; long ln; + bool str; }; /* -- cgit v1.2.1