summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2010-04-27 02:26:37 +0000
committerAdrian Thurston <thurston@complang.org>2010-04-27 02:26:37 +0000
commita9b946cf21d5342cdd16f2ec4053703a84732e58 (patch)
tree59a6c4b11b19048b09ec703d6c5a2e61e76170b9
parent10da2d1bea42dc1ae01adb19056f6c025f4d5139 (diff)
downloadcolm-a9b946cf21d5342cdd16f2ec4053703a84732e58.tar.gz
Finished off the porting to C.
Implemented state table compressions by overlaying arrays and using an 'owner' array to determine which entry belongs to which state.
-rw-r--r--colm/Makefile.in30
-rw-r--r--colm/bytecode.c (renamed from colm/bytecode.cpp)177
-rw-r--r--colm/bytecode.h27
-rw-r--r--colm/bytecode2.h2
-rw-r--r--colm/ctinput.cpp4
-rw-r--r--colm/debug.c8
-rw-r--r--colm/debug.h2
-rw-r--r--colm/fsmrun.c8
-rw-r--r--colm/fsmrun.h3
-rw-r--r--colm/main.cpp4
-rw-r--r--colm/parsedata.cpp6
-rw-r--r--colm/parsedata.h1
-rw-r--r--colm/pdabuild.cpp129
-rw-r--r--colm/pdacodegen.cpp13
-rw-r--r--colm/pdacodegen.h1
-rw-r--r--colm/pdarun.c12
-rw-r--r--colm/pdarun.h3
-rw-r--r--colm/tree.c (renamed from colm/tree2.c)0
-rw-r--r--colm/tree.h2
19 files changed, 256 insertions, 176 deletions
diff --git a/colm/Makefile.in b/colm/Makefile.in
index 58b6fa6d..042f6e30 100644
--- a/colm/Makefile.in
+++ b/colm/Makefile.in
@@ -61,11 +61,10 @@ COLM_SRC = \
pcheck.cpp \
ctinput.cpp
-RUNTIME_SRC = bytecode.cpp
-RUNTIME_SRC_C = map.c fsmrun.c pdarun.c list.c input.c debug.c codevect.c pool.c string.c tree2.c
-
-
-ALL_SRC = $(COLM_SRC) $(RUNTIME_SRC)
+RUNTIME_SRC = \
+ map.c fsmrun.c pdarun.c \
+ list.c input.c debug.c \
+ codevect.c pool.c string.c tree.c bytecode.c
# Files in ALL_SRC that are generated.
GEN_SRC = version.h lmscan.cpp lmparse.h lmparse.cpp
@@ -90,13 +89,10 @@ CC = @CC@
# Get objects and dependencies from sources.
COLM_OBJ = $(COLM_SRC:%.cpp=%.o)
-RUNTIME_OBJ_P = $(RUNTIME_SRC:%.cpp=%_p.o)
-RUNTIME_OBJ_D = $(RUNTIME_SRC:%.cpp=%_d.o)
-
-RUNTIME_OBJ_C_P = $(RUNTIME_SRC_C:%.c=%_p.o)
-RUNTIME_OBJ_C_D = $(RUNTIME_SRC_C:%.c=%_d.o)
+RUNTIME_OBJ_P = $(RUNTIME_SRC:%.c=%_p.o)
+RUNTIME_OBJ_D = $(RUNTIME_SRC:%.c=%_d.o)
-DEPS = $(COLM_SRC:%.cpp=.%.d) $(RUNTIME_SRC:%.cpp=.%_p.d) $(RUNTIME_SRC:%.cpp=.%_d.d)
+DEPS = $(COLM_SRC:%.cpp=.%.d) $(RUNTIME_SRC:%.c=.%_p.d) $(RUNTIME_SRC:%.c=.%_d.d)
# Rules.
all: colm $(RUNTIME_P) $(RUNTIME_D)
@@ -133,19 +129,11 @@ $(COLM_OBJ): %.o: %.cpp
@$(CXX) -M $(DEFS_COLM) $(INCS) $< > .$*.d
$(CXX) -c $(CFLAGS) $(DEFS_COLM) $(INCS) -o $@ $<
-$(RUNTIME_OBJ_P): %_p.o: %.cpp
- @$(CXX) -M -MT $@ $(DEFS_RT_P) $< > .$*_p.d
- $(CXX) -c $(CFLAGS) $(DEFS_RT_P) -o $@ $<
-
-$(RUNTIME_OBJ_D): %_d.o: %.cpp
- @$(CXX) -M -MT $@ $(DEFS_RT_D) $< > .$*_d.d
- $(CXX) -c $(CFLAGS) $(DEFS_RT_D) -o $@ $<
-
-$(RUNTIME_OBJ_C_P): %_p.o: %.c
+$(RUNTIME_OBJ_P): %_p.o: %.c
@$(CC) -M -MT $@ $(DEFS_RT_P) $< > .$*_p.d
$(CC) -c $(CFLAGS) $(DEFS_RT_P) -o $@ $<
-$(RUNTIME_OBJ_C_D): %_d.o: %.c
+$(RUNTIME_OBJ_D): %_d.o: %.c
@$(CC) -M -MT $@ $(DEFS_RT_D) $< > .$*_d.d
$(CC) -c $(CFLAGS) $(DEFS_RT_D) -o $@ $<
diff --git a/colm/bytecode.cpp b/colm/bytecode.c
index 9ec10703..59afafcb 100644
--- a/colm/bytecode.cpp
+++ b/colm/bytecode.c
@@ -19,36 +19,28 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include <iostream>
-#include <iomanip>
-#include <sstream>
#include <alloca.h>
#include <sys/mman.h>
-#include <sstream>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
-#include "bytecode.h"
#include "pdarun.h"
-#include "fsmrun.h"
-#include "pdarun.h"
-
-using std::cout;
-using std::cerr;
-using std::endl;
-using std::ostringstream;
-using std::string;
-using std::hex;
-
-exit_object endp;
-
-void operator<<( ostream &out, exit_object & )
-{
- out << endl;
- exit(1);
-}
-
+#include "fsmrun2.h"
+#include "tree.h"
+#include "bytecode2.h"
+#include "pool.h"
+#include "debug.h"
+#include "config.h"
+
+#undef COLM_LOG
+#undef COLM_LOG_BYTECODE
+#undef COLM_LOG_PARSE
+#undef COLM_LOG_MATCH
+#undef COLM_LOG_COMPILE
+
+#define true 1
+#define false 0
#define push(i) (*(--sp) = (i))
#define pop() (*sp++)
@@ -145,12 +137,12 @@ Tree *prepParseTree( Program *prg, Tree **sp, Tree *tree )
#endif
Kid *unused = 0;
tree = copyRealTree( prg, tree, 0, &unused, true );
- return tree;
+ return tree;
}
void sendTreeFrag( Program *prg, Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun,
InputStream *inputStream,
- Tree *tree, bool ignore )
+ Tree *tree, int ignore )
{
tree = prepParseTree( prg, sp, tree );
@@ -180,7 +172,7 @@ void sendTreeFrag( Program *prg, Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun,
incrementConsumed( pdaRun );
- ::ignore( pdaRun, send->tree );
+ ignoreTree( pdaRun, send->tree );
kidFree( pdaRun->prg, send );
}
else {
@@ -197,7 +189,7 @@ void sendTreeFrag( Program *prg, Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun,
}
}
-void parserSetContext( Tree **&sp, Program *prg, Accum *accum, Tree *val )
+void parserSetContext( Tree **sp, Program *prg, Accum *accum, Tree *val )
{
accum->pdaRun->context = splitTree( prg, val );
}
@@ -240,7 +232,7 @@ void setInput( Program *prg, Tree **sp, Accum *accum, Stream *stream )
treeUpref( (Tree*)accum->stream );
}
-void parseStream( Tree **&sp, Program *prg, Tree *input, Accum *accum, long stopId )
+void parseStream( Tree **sp, Program *prg, Tree *input, Accum *accum, long stopId )
{
accum->pdaRun->stopTarget = stopId;
@@ -249,7 +241,7 @@ void parseStream( Tree **&sp, Program *prg, Tree *input, Accum *accum, long stop
parseLoop( sp, accum->pdaRun, accum->fsmRun, stream->in );
}
-Word streamAppend( Tree **&sp, Program *prg, Tree *input, Stream *stream )
+Word streamAppend( Tree **sp, Program *prg, Tree *input, Stream *stream )
{
if ( input->id == LEL_ID_STR ) {
//assert(false);
@@ -280,7 +272,7 @@ Word streamAppend( Tree **&sp, Program *prg, Tree *input, Stream *stream )
}
}
-void parseFrag( Tree **&sp, Program *prg, Tree *input, Accum *accum, long stopId )
+void parseFrag( Tree **sp, Program *prg, Tree *input, Accum *accum, long stopId )
{
accum->pdaRun->stopTarget = stopId;
Stream *stream = (Stream*) extractInput( prg, accum );
@@ -323,7 +315,7 @@ void parseFrag( Tree **&sp, Program *prg, Tree *input, Accum *accum, long stopId
}
}
-void undoParseStream( Tree **&sp, Program *prg, Stream *input, Accum *accum, long consumed )
+void undoParseStream( Tree **sp, Program *prg, Stream *input, Accum *accum, long consumed )
{
if ( consumed < accum->pdaRun->consumed ) {
accum->pdaRun->numRetry += 1;
@@ -337,7 +329,7 @@ void undoParseStream( Tree **&sp, Program *prg, Stream *input, Accum *accum, lon
}
}
-Tree *parseFinish( Tree **&sp, Program *prg, Accum *accum, bool revertOn )
+Tree *parseFinish( Tree **sp, Program *prg, Accum *accum, int revertOn )
{
Stream *stream = (Stream*)extractInput( prg, accum );
@@ -365,7 +357,7 @@ Tree *parseFinish( Tree **&sp, Program *prg, Accum *accum, bool revertOn )
return tree;
}
-Tree *streamPull( Program *prg, FsmRun *fsmRun, Stream *stream, Tree *length )
+Tree *streamPull2( Program *prg, FsmRun *fsmRun, Stream *stream, Tree *length )
{
long len = ((Int*)length)->value;
Head *tokdata = streamPull( prg, fsmRun, stream->in, len );
@@ -380,7 +372,7 @@ void undoPull( Program *prg, FsmRun *fsmRun, Stream *stream, Tree *str )
undoStreamPull( fsmRun, stream->in, data, length );
}
-Word streamPush( Program *prg, Tree **&sp, Stream *stream, Tree *tree, bool ignore )
+Word streamPush( Program *prg, Tree **sp, Stream *stream, Tree *tree, int ignore )
{
if ( tree->id == LEL_ID_STR ) {
/* This should become a compile error. If it's text, it's up to the
@@ -428,7 +420,8 @@ Tree *getLocalSplit( Program *prg, Tree **frame, long field )
void downrefLocalTrees( Program *prg, Tree **sp, Tree **frame, char *trees, long treesLen )
{
- for ( long i = 0; i < treesLen; i++ ) {
+ long i;
+ for ( i = 0; i < treesLen; i++ ) {
#ifdef COLM_LOG_BYTECODE
if ( colm_log_bytecode ) {
cerr << "local tree downref: " << (long)trees[i] << endl;
@@ -439,18 +432,20 @@ void downrefLocalTrees( Program *prg, Tree **sp, Tree **frame, char *trees, long
}
}
-UserIter *uiterCreate( Tree **&sp, Program *prg, FunctionInfo *fi, long searchId )
+UserIter *uiterCreate( Tree ***psp, Program *prg, FunctionInfo *fi, long searchId )
{
+ Tree **sp = *psp;
pushn( sizeof(UserIter) / sizeof(Word) );
void *mem = ptop();
- UserIter *uiter = new(mem) UserIter;
+ UserIter *uiter = mem;
initUserIter( uiter, ptop(), fi->argSize, searchId );
+ *psp = sp;
return uiter;
}
void uiterInit( Program *prg, Tree **sp, UserIter *uiter,
- FunctionInfo *fi, bool revertOn )
+ FunctionInfo *fi, int revertOn )
{
/* Set up the first yeild so when we resume it starts at the beginning. */
uiter->ref.kid = 0;
@@ -463,15 +458,19 @@ void uiterInit( Program *prg, Tree **sp, UserIter *uiter,
uiter->resume = prg->rtd->frameInfo[fi->frameId].codeWC;
}
-void treeIterDestroy( Tree **&sp, TreeIter *iter )
+void treeIterDestroy( Tree ***psp, TreeIter *iter )
{
+ Tree **sp = *psp;
long curStackSize = iter->stackRoot - ptop();
assert( iter->stackSize == curStackSize );
popn( iter->stackSize );
+ *psp = sp;
}
-void userIterDestroy( Tree **&sp, UserIter *uiter )
+void userIterDestroy( Tree ***psp, UserIter *uiter )
{
+ Tree **sp = *psp;
+
/* We should always be coming from a yield. The current stack size will be
* nonzero and the stack size in the iterator will be correct. */
long curStackSize = uiter->stackRoot - ptop();
@@ -482,13 +481,16 @@ void userIterDestroy( Tree **&sp, UserIter *uiter )
popn( uiter->stackRoot - ptop() );
popn( sizeof(UserIter) / sizeof(Word) );
popn( argSize );
+
+ *psp = sp;
}
Tree *constructArgv( Program *prg, int argc, char **argv )
{
Tree *list = createGeneric( prg, 1 );
treeUpref( list );
- for ( int i = 0; i < argc; i++ ) {
+ int i;
+ for ( i = 0; i < argc; i++ ) {
Head *head = stringAllocPointer( prg, argv[i], strlen(argv[i]) );
Tree *arg = constructString( prg, head );
treeUpref( arg );
@@ -540,7 +542,8 @@ void initProgram( Program *prg, int argc, char **argv, int ctxDepParsing,
void clearGlobal( Program *prg, Tree **sp )
{
/* Downref all the fields in the global object. */
- for ( int g = 0; g < prg->rtd->globalSize; g++ ) {
+ int g;
+ for ( g = 0; g < prg->rtd->globalSize; g++ ) {
//assert( getAttr( global, g )->refs == 1 );
treeDownref( prg, sp, getAttr( prg->global, g ) );
}
@@ -581,31 +584,31 @@ void clearProgram( Program *prg, Tree **vm_stack, Tree **sp )
long kidLost = kidNumLost( prg );
if ( kidLost )
- cerr << "warning: lost kids: " << kidLost << endl;
+ message( "warning: lost kids: %ld\n", kidLost );
long treeLost = treeNumLost( prg );
if ( treeLost )
- cerr << "warning: lost trees: " << treeLost << endl;
+ message( "warning: lost trees: %ld\n", treeLost );
long parseTreeLost = parseTreeNumLost( prg );
if ( parseTreeLost )
- cerr << "warning: lost parse trees: " << parseTreeLost << endl;
+ message( "warning: lost parse trees: %ld\n", parseTreeLost );
long listLost = listElNumLost( prg );
if ( listLost )
- cerr << "warning: lost listEls: " << listLost << endl;
+ message( "warning: lost listEls: %ld\n", listLost );
long mapLost = mapElNumLost( prg );
if ( mapLost )
- cerr << "warning: lost mapEls: " << mapLost << endl;
+ message( "warning: lost mapEls: %ld\n", mapLost );
long headLost = headNumLost( prg );
if ( headLost )
- cerr << "warning: lost heads: " << headLost << endl;
+ message( "warning: lost heads: %ld\n", headLost );
long locationLost = locationNumLost( prg );
if ( locationLost )
- cerr << "warning: lost locations: " << locationLost << endl;
+ message( "warning: lost locations: %ld\n", locationLost );
kidClear( prg );
treeClear( prg );
@@ -998,8 +1001,7 @@ again:
return;
}
default: {
- cerr << "UNKNOWN INSTRUCTION: 0x" << hex << (ulong)instr[-1] <<
- " -- reverse code downref" << endl;
+ fatal( "UNKNOWN INSTRUCTION: -- reverse code downref\n" );
assert(false);
break;
}
@@ -1010,7 +1012,7 @@ again:
void execute( Execution *exec, Tree **sp )
{
/* If we have a lhs push it to the stack. */
- bool haveLhs = exec->lhs != 0;
+ int haveLhs = exec->lhs != 0;
if ( haveLhs )
push( exec->lhs );
@@ -1456,7 +1458,8 @@ again:
LangElInfo *lelInfo = prg->rtd->lelInfo;
char **mark = exec->captures;
- for ( int i = 0; i < lelInfo[exec->genId].numCaptureAttr; i++ ) {
+ int i;
+ for ( i = 0; i < lelInfo[exec->genId].numCaptureAttr; i++ ) {
CaptureAttr *ca = &prg->rtd->captureAttr[lelInfo[exec->genId].captureAttr + i];
Head *data = stringAllocFull( prg,
mark[ca->mark_enter], mark[ca->mark_leave] - mark[ca->mark_enter] );
@@ -1867,7 +1870,8 @@ again:
}
#endif
- pop();
+ Tree *f = pop();
+ f++;
Tree *integer = pop();
Tree *format = pop();
Head *res = stringSprintf( prg, (Str*)format, (Int*)integer );
@@ -2324,7 +2328,7 @@ again:
#endif
TreeIter *iter = (TreeIter*) plocal(field);
- treeIterDestroy( sp, iter );
+ treeIterDestroy( &sp, iter );
break;
}
case IN_REV_TRITER_FROM_REF: {
@@ -2544,12 +2548,13 @@ again:
Kid kid;
kid.tree = tree;
kid.next = 0;
- bool matched = matchPattern( bindings, prg, rootNode, &kid, false );
+ int matched = matchPattern( bindings, prg, rootNode, &kid, false );
if ( !matched )
memset( bindings, 0, sizeof(Tree*)*(1+numBindings) );
else {
- for ( int b = 1; b <= numBindings; b++ )
+ int b;
+ for ( b = 1; b <= numBindings; b++ )
assert( bindings[b] != 0 );
}
@@ -2562,7 +2567,8 @@ again:
Tree *result = matched ? tree : 0;
treeUpref( result );
push( result ? tree : 0 );
- for ( int b = 1; b <= numBindings; b++ ) {
+ int b;
+ for ( b = 1; b <= numBindings; b++ ) {
treeUpref( bindings[b] );
push( bindings[b] );
}
@@ -2848,7 +2854,7 @@ again:
#endif
Tree *stream = pop();
Tree *len = pop();
- Tree *string = streamPull( prg, exec->fsmRun, (Stream*)stream, len );
+ Tree *string = streamPull2( prg, exec->fsmRun, (Stream*)stream, len );
treeUpref( string );
push( string );
@@ -2954,7 +2960,8 @@ again:
int numBindings = prg->rtd->patReplInfo[patternId].numBindings;
Tree *bindings[1+numBindings];
- for ( int b = 1; b <= numBindings; b++ ) {
+ int b;
+ for ( b = 1; b <= numBindings; b++ ) {
bindings[b] = pop();
assert( bindings[b] != 0 );
}
@@ -3003,7 +3010,8 @@ again:
#endif
Tree *result = makeToken2( sp, prg, nargs );
- for ( long i = 0; i < nargs; i++ ) {
+ long i;
+ for ( i = 0; i < nargs; i++ ) {
Tree *arg = pop();
treeDownref( prg, sp, arg );
}
@@ -3021,7 +3029,8 @@ again:
#endif
Tree *result = makeTree( sp, prg, nargs );
- for ( long i = 0; i < nargs; i++ ) {
+ long i;
+ for ( i = 0; i < nargs; i++ ) {
Tree *arg = pop();
treeDownref( prg, sp, arg );
}
@@ -3581,7 +3590,7 @@ again:
treeDownref( prg, sp, obj );
- bool inserted = mapInsert( prg, (Map*)obj, key, val );
+ int inserted = mapInsert( prg, (Map*)obj, key, val );
Tree *result = inserted ? prg->trueVal : prg->falseVal;
treeUpref( result );
push( result );
@@ -3617,7 +3626,7 @@ again:
treeDownref( prg, sp, obj );
- bool inserted = mapInsert( prg, (Map*)obj, key, val );
+ int inserted = mapInsert( prg, (Map*)obj, key, val );
Tree *result = inserted ? prg->trueVal : prg->falseVal;
treeUpref( result );
push( result );
@@ -3784,7 +3793,7 @@ again:
#endif
/* Either both or neither. */
- assert( ( key == 0 ) xor ( val != 0 ) );
+ assert( ( key == 0 ) ^ ( val != 0 ) );
Tree *obj = pop();
if ( key != 0 )
@@ -3946,7 +3955,7 @@ again:
#endif
FunctionInfo *fi = prg->rtd->functionInfo + funcId;
- UserIter *uiter = uiterCreate( sp, prg, fi, searchId );
+ UserIter *uiter = uiterCreate( &sp, prg, fi, searchId );
local(field) = (SW) uiter;
/* This is a setup similar to as a call, only the frame structure
@@ -3976,7 +3985,7 @@ again:
#endif
FunctionInfo *fi = prg->rtd->functionInfo + funcId;
- UserIter *uiter = uiterCreate( sp, prg, fi, searchId );
+ UserIter *uiter = uiterCreate( &sp, prg, fi, searchId );
local(field) = (SW) uiter;
/* This is a setup similar to as a call, only the frame structure
@@ -4002,7 +4011,7 @@ again:
#endif
UserIter *uiter = (UserIter*) local(field);
- userIterDestroy( sp, uiter );
+ userIterDestroy( &sp, uiter );
break;
}
case IN_RET: {
@@ -4123,7 +4132,7 @@ again:
}
#endif
- cout.flush();
+ fflush( stdout );
goto out;
}
@@ -4132,13 +4141,12 @@ again:
* and can represent "not implemented" or "compiler error" because a
* variable holding instructions was not properly initialize. */
case IN_HALT: {
- cerr << "IN_HALT -- compiler did something wrong" << endl;
+ fatal( "IN_HALT -- compiler did something wrong\n" );
exit(1);
break;
}
default: {
- cerr << "UNKNOWN INSTRUCTION: 0x" << hex << (ulong)instr[-1] <<
- " -- something is wrong" << endl;
+ fatal( "UNKNOWN INSTRUCTION: -- something is wrong\n" );
assert(false);
break;
}
@@ -4151,16 +4159,17 @@ out:
void parseError( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, int tokId, Tree *tree )
{
- cerr << "error:" << inputStream->line << ": at token ";
- if ( tokId < 128 )
- cerr << "\"" << pdaRun->tables->rtd->lelInfo[tokId].name << "\"";
- else
- cerr << pdaRun->tables->rtd->lelInfo[tokId].name;
- if ( stringLength( tree->tokdata ) > 0 ) {
- cerr << " with data \"";
- cerr.write( stringData( tree->tokdata ),
- stringLength( tree->tokdata ) );
- cerr << "\"";
- }
- cerr << ": ";
+ fprintf( stderr, "error: %ld:\n", inputStream->line );
+// cerr << "error:" << inputStream->line << ": at token ";
+// if ( tokId < 128 )
+// cerr << "\"" << pdaRun->tables->rtd->lelInfo[tokId].name << "\"";
+//// else
+// cerr << pdaRun->tables->rtd->lelInfo[tokId].name;
+// if ( stringLength( tree->tokdata ) > 0 ) {
+// cerr << " with data \"";
+// cerr.write( stringData( tree->tokdata ),
+// stringLength( tree->tokdata ) );
+// cerr << "\"";
+// }
+// cerr << ": ";
}
diff --git a/colm/bytecode.h b/colm/bytecode.h
index 41229354..e633cc44 100644
--- a/colm/bytecode.h
+++ b/colm/bytecode.h
@@ -27,38 +27,11 @@
#include "pool.h"
#include "pdarun.h"
#include "map.h"
-
-#include <iostream>
-
-using std::cerr;
-using std::endl;
-using std::ostream;
-
#include "bytecode2.h"
-
-typedef struct _Kid Kid;
-typedef struct _Tree Tree;
-typedef struct _ParseTree ParseTree;
-typedef struct _ListEl ListEl;
-typedef struct _MapEl MapEl;
-typedef struct _List List;
-typedef struct _Map Map;
-typedef struct _Ref Ref;
-typedef struct _Pointer Pointer;
-typedef struct _Str Str;
-typedef struct _Int Int;
-typedef struct _PdaRun PdaRun;
-
-
-void rcodeDownref( Program *prg, Tree **sp, Code *instr );
-
#include "tree.h"
-Tree **stackAlloc();
-
/*
* Runtime environment
*/
-
#endif
diff --git a/colm/bytecode2.h b/colm/bytecode2.h
index 80b6f1f5..c25e90bf 100644
--- a/colm/bytecode2.h
+++ b/colm/bytecode2.h
@@ -456,6 +456,8 @@ void runProgram( Program *prg );
void allocGlobal( Program *prg );
void executeCode( Execution *exec, Tree **sp, Code *instr );
+void rcodeDownref( Program *prg, Tree **sp, Code *instr );
+Tree **stackAlloc();
#ifdef __cplusplus
}
diff --git a/colm/ctinput.cpp b/colm/ctinput.cpp
index 1fd0284f..52df0ce5 100644
--- a/colm/ctinput.cpp
+++ b/colm/ctinput.cpp
@@ -24,6 +24,10 @@
#include "input.h"
#include "fsmrun.h"
+#include <iostream>
+using std::cerr;
+using std::endl;
+
InputFuncs staticFuncs;
InputFuncs patternFuncs;
InputFuncs replFuncs;
diff --git a/colm/debug.c b/colm/debug.c
index 047ca518..dd8f7ff2 100644
--- a/colm/debug.c
+++ b/colm/debug.c
@@ -52,3 +52,11 @@ void fatal( const char *fmt, ... )
va_end( args );
exit(1);
}
+
+void message( const char *fmt, ... )
+{
+ va_list args;
+ va_start( args, fmt );
+ vfprintf( stderr, fmt, args );
+ va_end( args );
+}
diff --git a/colm/debug.h b/colm/debug.h
index 15f8a554..7f8519f9 100644
--- a/colm/debug.h
+++ b/colm/debug.h
@@ -37,6 +37,8 @@ int _debug( long realm, const char *fmt, ... );
int _check_realm( long realm );
extern long colmActiveRealm;
+void message( const char *fmt, ... );
+
#define REALM_BYTECODE 0x00000001
#define REALM_PARSE 0x00000002
#define REALM_MATCH 0x00000004
diff --git a/colm/fsmrun.c b/colm/fsmrun.c
index 8b5463cb..04df873a 100644
--- a/colm/fsmrun.c
+++ b/colm/fsmrun.c
@@ -473,7 +473,7 @@ void sendQueuedTokens( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *i
incrementConsumed( pdaRun );
- ignore( pdaRun, send->tree );
+ ignoreTree( pdaRun, send->tree );
kidFree( fsmRun->prg, send );
}
else {
@@ -668,7 +668,7 @@ void sendHandleError( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *in
}
}
-void ignore( PdaRun *pdaRun, Tree *tree )
+void ignoreTree( PdaRun *pdaRun, Tree *tree )
{
/* Add the ignore string to the head of the ignore list. */
Kid *ignore = kidAllocate( pdaRun->prg );
@@ -712,7 +712,7 @@ void sendIgnore( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, long
incrementConsumed( pdaRun );
/* Send it to the pdaRun. */
- ignore( pdaRun, tree );
+ ignoreTree( pdaRun, tree );
}
Head *extractMatch( Program *prg, FsmRun *fsmRun, InputStream *inputStream )
@@ -1046,7 +1046,7 @@ void sendTreeIgnore( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inp
incrementConsumed( pdaRun );
- ignore( pdaRun, tree );
+ ignoreTree( pdaRun, tree );
}
void parseLoop( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream )
diff --git a/colm/fsmrun.h b/colm/fsmrun.h
index 2efcc607..ed103741 100644
--- a/colm/fsmrun.h
+++ b/colm/fsmrun.h
@@ -44,9 +44,6 @@ void operator<<( std::ostream &out, exit_object & );
#include "fsmrun2.h"
-void execAction( FsmRun *fsmRun, GenAction *genAction );
-
-long scan_token( PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream );
#endif
diff --git a/colm/main.cpp b/colm/main.cpp
index 5b26c026..a8243e9d 100644
--- a/colm/main.cpp
+++ b/colm/main.cpp
@@ -292,7 +292,7 @@ void compileOutputPath( const char *argv0 )
int length = 1024 + 3*strlen(location) + strlen(outputFileName) + strlen(exec);
char command[length];
sprintf( command,
- "g++ -Wall -Wwrite-strings"
+ "gcc -Wall -Wwrite-strings"
" -I" PREFIX "/include"
" -g"
" -o %s"
@@ -317,7 +317,7 @@ void compileOutputRelative( const char *argv0 )
int length = 1024 + 3*strlen(location) + strlen(outputFileName) + strlen(exec);
char command[length];
sprintf( command,
- "g++ -Wall -Wwrite-strings"
+ "gcc -Wall -Wwrite-strings"
" -I%s.."
" -I%s../aapl"
" -g"
diff --git a/colm/parsedata.cpp b/colm/parsedata.cpp
index 9fa94c47..ab666350 100644
--- a/colm/parsedata.cpp
+++ b/colm/parsedata.cpp
@@ -41,6 +41,12 @@ using namespace std;
using std::ostringstream;
char machineMain[] = "main";
+exit_object endp;
+void operator<<( ostream &out, exit_object & )
+{
+ out << endl;
+ exit(1);
+}
/* Perform minimization after an operation according
* to the command line args. */
diff --git a/colm/parsedata.h b/colm/parsedata.h
index 41e6f9b4..994dd018 100644
--- a/colm/parsedata.h
+++ b/colm/parsedata.h
@@ -685,6 +685,7 @@ struct ParseData
bool makeFirstSetProd( Definition *prod, PdaState *state );
void makeFirstSets();
+ int findIndexOff( PdaTables *pdaTables, PdaGraph *pdaGraph, PdaState *state, int &currLen );
void trySetTime( PdaTrans *trans, long code, long &time );
void addRegion( PdaState *tabState, long pdaKey );
PdaState *followProd( PdaState *tabState, PdaState *prodState );
diff --git a/colm/pdabuild.cpp b/colm/pdabuild.cpp
index 630c50b6..a1fa1ae4 100644
--- a/colm/pdabuild.cpp
+++ b/colm/pdabuild.cpp
@@ -1711,13 +1711,65 @@ void ParseData::fillInPatterns( Program *prg )
}
+
+int ParseData::findIndexOff( PdaTables *pdaTables, PdaGraph *pdaGraph, PdaState *state, int &curLen )
+{
+ for ( int start = 0; start < curLen; ) {
+ int offset = start;
+ for ( TransMap::Iter trans = state->transMap; trans.lte(); trans++ ) {
+ if ( pdaTables->owners[offset] != -1 )
+ goto next_start;
+
+ offset++;
+ if ( ! trans.last() ) {
+ TransMap::Iter next = trans.next();
+ offset += next->key - trans->key - 1;
+ }
+ }
+
+ /* Got though the whole list without a conflict. */
+ return start;
+
+next_start:
+ start++;
+ }
+
+ return curLen;
+}
+
+struct CmpSpan
+{
+ static int compare( PdaState *state1, PdaState *state2 )
+ {
+ int dist1 = 0, dist2 = 0;
+
+ if ( state1->transMap.length() > 0 ) {
+ TransMap::Iter first1 = state1->transMap.first();
+ TransMap::Iter last1 = state1->transMap.last();
+ dist1 = last1->key - first1->key;
+ }
+
+ if ( state2->transMap.length() > 0 ) {
+ TransMap::Iter first2 = state2->transMap.first();
+ TransMap::Iter last2 = state2->transMap.last();
+ dist2 = last2->key - first2->key;
+ }
+
+ if ( dist1 < dist2 )
+ return 1;
+ else if ( dist2 < dist1 )
+ return -1;
+ return 0;
+ }
+};
+
PdaTables *ParseData::makePdaTables( PdaGraph *pdaGraph )
{
- int count, curOffset, pos;
+ int count, pos;
PdaTables *pdaTables = new PdaTables;
/*
- * Indicies.
+ * Counting max indices.
*/
count = 0;
for ( PdaStateList::Iter state = pdaGraph->stateList; state.lte(); state++ ) {
@@ -1725,27 +1777,64 @@ PdaTables *ParseData::makePdaTables( PdaGraph *pdaGraph )
count++;
if ( ! trans.last() ) {
TransMap::Iter next = trans.next();
- for ( long key = trans->key+1; key < next->key; key++ )
- count++;
+ count += next->key - trans->key - 1;
}
}
}
- pdaTables->indicies = new int[count];
+
+
+ /* Allocate indicies and owners. */
pdaTables->numIndicies = count;
+ pdaTables->indicies = new int[count];
+ pdaTables->owners = new int[count];
+ for ( long i = 0; i < count; i++ ) {
+ pdaTables->indicies[i] = -1;
+ pdaTables->owners[i] = -1;
+ }
+
+ /* Allocate offsets. */
+ int numStates = pdaGraph->stateList.length();
+ pdaTables->offsets = new unsigned int[numStates];
+ pdaTables->numStates = numStates;
+
+ /* Place transitions into indicies/owners */
+ PdaState **states = new PdaState*[numStates];
+ long ds = 0;
+ for ( PdaStateList::Iter state = pdaGraph->stateList; state.lte(); state++ )
+ states[ds++] = state;
+
+ /* Sorting baseded on span length. Gives an improvement, but incures a
+ * cost. Off for now. */
+ //MergeSort< PdaState*, CmpSpan > mergeSort;
+ //mergeSort.sort( states, numStates );
+
+ int indLen = 0;
+ for ( int s = 0; s < numStates; s++ ) {
+ PdaState *state = states[s];
+
+ int indOff = findIndexOff( pdaTables, pdaGraph, state, indLen );
+ pdaTables->offsets[state->stateNum] = indOff;
- count = 0;
- for ( PdaStateList::Iter state = pdaGraph->stateList; state.lte(); state++ ) {
for ( TransMap::Iter trans = state->transMap; trans.lte(); trans++ ) {
- pdaTables->indicies[count++] = trans->value->actionSetEl->key.id;
+ pdaTables->indicies[indOff] = trans->value->actionSetEl->key.id;
+ pdaTables->owners[indOff] = state->stateNum;
+ indOff++;
if ( ! trans.last() ) {
TransMap::Iter next = trans.next();
- for ( long key = trans->key+1; key < next->key; key++ )
- pdaTables->indicies[count++] = -1;
+ indOff += next->key - trans->key - 1;
}
}
+
+ if ( indOff > indLen )
+ indLen = indOff;
}
+ /* We allocated the max, but cmpression gives us less. */
+ pdaTables->numIndicies = indLen;
+ delete[] states;
+
+
/*
* Keys
*/
@@ -1769,26 +1858,6 @@ PdaTables *ParseData::makePdaTables( PdaGraph *pdaGraph )
}
/*
- * Offsets
- */
- count = pdaGraph->stateList.length();
- pdaTables->offsets = new unsigned int[count];
- pdaTables->numStates = count;
-
- count = 0;
- curOffset = 0;
- for ( PdaStateList::Iter state = pdaGraph->stateList; state.lte(); state++ ) {
- pdaTables->offsets[count++] = curOffset;
-
- /* Increment the offset. */
- if ( state->transMap.length() > 0 ) {
- TransMap::Iter first = state->transMap.first();
- TransMap::Iter last = state->transMap.last();
- curOffset += last->key - first->key + 1;
- }
- }
-
- /*
* Targs
*/
count = pdaGraph->actionSet.length();
diff --git a/colm/pdacodegen.cpp b/colm/pdacodegen.cpp
index c2567a25..fab5bffc 100644
--- a/colm/pdacodegen.cpp
+++ b/colm/pdacodegen.cpp
@@ -466,6 +466,18 @@ void PdaCodeGen::writeParserData( long id, PdaTables *tables )
}
out << "\n};\n\n";
+ out << "int " << prefix << owners() << "[] = {\n\t";
+ for ( int i = 0; i < tables->numIndicies; i++ ) {
+ out << tables->owners[i];
+
+ if ( i < tables->numIndicies-1 ) {
+ out << ", ";
+ if ( (i+1) % 8 == 0 )
+ out << "\n\t";
+ }
+ }
+ out << "\n};\n\n";
+
out << "int " << prefix << keys() << "[] = {\n\t";
for ( int i = 0; i < tables->numKeys; i++ ) {
out << tables->keys[i];
@@ -566,6 +578,7 @@ void PdaCodeGen::writeParserData( long id, PdaTables *tables )
"PdaTables " << prefix << "pdaTables =\n"
"{\n"
" " << prefix << indicies() << ",\n"
+ " " << prefix << owners() << ",\n"
" " << prefix << keys() << ",\n"
" " << prefix << offsets() << ",\n"
" " << prefix << targs() << ",\n"
diff --git a/colm/pdacodegen.h b/colm/pdacodegen.h
index 7ca32546..fce3d0dd 100644
--- a/colm/pdacodegen.h
+++ b/colm/pdacodegen.h
@@ -58,6 +58,7 @@ struct PdaCodeGen
String startState() { return PARSER() + "startState"; }
String indicies() { return PARSER() + "indicies"; }
+ String owners() { return PARSER() + "owners"; }
String keys() { return PARSER() + "keys"; }
String offsets() { return PARSER() + "offsets"; }
String targs() { return PARSER() + "targs"; }
diff --git a/colm/pdarun.c b/colm/pdarun.c
index 548efbb3..45dbcf12 100644
--- a/colm/pdarun.c
+++ b/colm/pdarun.c
@@ -358,7 +358,9 @@ void parseToken( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputSt
unsigned int *action;
int rhsLen;
Kid *lel;
+ int owner;
int induceReject;
+ int indPos;
/* The scanner will send a null token if it can't find a token. */
if ( input == 0 )
@@ -387,8 +389,14 @@ again:
lel->tree->id > pdaRun->tables->keys[(pdaRun->cs<<1)+1] )
goto parseError;
- pos = pdaRun->tables->indicies[pdaRun->tables->offsets[pdaRun->cs]
- + (lel->tree->id - pdaRun->tables->keys[pdaRun->cs<<1])];
+ indPos = pdaRun->tables->offsets[pdaRun->cs] +
+ (lel->tree->id - pdaRun->tables->keys[pdaRun->cs<<1]);
+
+ owner = pdaRun->tables->owners[indPos];
+ if ( owner != pdaRun->cs )
+ goto parseError;
+
+ pos = pdaRun->tables->indicies[indPos];
if ( pos < 0 )
goto parseError;
diff --git a/colm/pdarun.h b/colm/pdarun.h
index 1601602f..b5ba0f32 100644
--- a/colm/pdarun.h
+++ b/colm/pdarun.h
@@ -370,6 +370,7 @@ typedef struct _PdaTables
{
/* Parser table data. */
int *indicies;
+ int *owners;
int *keys;
unsigned int *offsets;
unsigned int *targs;
@@ -579,7 +580,7 @@ void pdaRunMatch( PdaRun *pdaRun, Kid *tree, Kid *pattern );
int pdaRunGetNextRegion( PdaRun *pdaRun, int offset );
void cleanParser( Tree **root, PdaRun *pdaRun );
-void ignore( PdaRun *pdaRun, Tree *tree );
+void ignoreTree( PdaRun *pdaRun, Tree *tree );
void parseToken( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream, Kid *input );
long undoParse( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream, Tree *tree );
void xml_print_list( RuntimeData *runtimeData, Kid *lel, int depth );
diff --git a/colm/tree2.c b/colm/tree.c
index b5c46526..b5c46526 100644
--- a/colm/tree2.c
+++ b/colm/tree.c
diff --git a/colm/tree.h b/colm/tree.h
index 29029ddb..e4c49dfd 100644
--- a/colm/tree.h
+++ b/colm/tree.h
@@ -28,8 +28,6 @@ extern "C" {
typedef struct _TreePair
{
-// TreePair() : key(0), val(0) {}
-
Tree *key;
Tree *val;
} TreePair;