Thread: [Plib-cvs] plib/src/psl psl.h,1.17,1.18 pslCodeGen.cxx,1.24,1.25 pslCompiler.cxx,1.22,1.23 pslCompil
Brought to you by:
sjbaker
From: Steve B. <sj...@us...> - 2002-09-15 04:34:43
|
Update of /cvsroot/plib/plib/src/psl In directory usw-pr-cvs1:/tmp/cvs-serv11840/plib/src/psl Modified Files: psl.h pslCodeGen.cxx pslCompiler.cxx pslCompiler.h pslContext.cxx pslContext.h pslDump.cxx pslExpression.cxx pslOpcodes.h pslProgram.cxx Log Message: Added PSL_DUMP and PSL_TRACE environment vars. Fixed evaluation of && and || so they don't compute their right-hand sides unless they have to. Index: psl.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/psl.h,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- psl.h 15 Sep 2002 01:34:24 -0000 1.17 +++ psl.h 15 Sep 2002 04:34:39 -0000 1.18 @@ -228,6 +228,8 @@ char *progName ; + int force_trace ; + public: pslProgram ( const pslExtension *ext, const char *_progName = NULL ) ; Index: pslCodeGen.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCodeGen.cxx,v retrieving revision 1.24 retrieving revision 1.25 diff -u -d -r1.24 -r1.25 --- pslCodeGen.cxx 15 Sep 2002 00:02:51 -0000 1.24 +++ pslCodeGen.cxx 15 Sep 2002 04:34:39 -0000 1.25 @@ -302,6 +302,28 @@ void pslCompiler::pushStackDup () { pushCodeByte ( OPCODE_STACK_DUPLICATE ) ; } +int pslCompiler::pushPeekJumpIfTrue ( int l ) +{ + pushCodeByte ( OPCODE_PEEK_JUMP_TRUE ) ; + + int res = next_code ; + + pushCodeAddr ( l ) ; + + return res ; +} + +int pslCompiler::pushPeekJumpIfFalse ( int l ) +{ + pushCodeByte ( OPCODE_PEEK_JUMP_FALSE ) ; + + int res = next_code ; + + pushCodeAddr ( l ) ; + + return res ; +} + int pslCompiler::pushJumpIfTrue ( int l ) { pushCodeByte ( OPCODE_JUMP_TRUE ) ; Index: pslCompiler.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.cxx,v retrieving revision 1.22 retrieving revision 1.23 diff -u -d -r1.22 -r1.23 --- pslCompiler.cxx 15 Sep 2002 00:02:51 -0000 1.22 +++ pslCompiler.cxx 15 Sep 2002 04:34:39 -0000 1.23 @@ -23,7 +23,7 @@ #include "pslLocal.h" - +#include "ul.h" int pslCompiler::compile ( const char *fname ) @@ -44,6 +44,8 @@ int pslCompiler::compile ( FILE *fd, const char *fname ) { + char *dump_env = getenv ( "PSL_DUMP" ) ; + init () ; _pslPushDefaultFile ( fd, (fname == NULL) ? progName : fname ) ; @@ -58,13 +60,21 @@ if ( num_errors != 0 ) { + if ( dump_env != NULL && + ulStrEqual ( dump_env, "on_error" ) ) + dump () ; + next_code = 0 ; pushCodeByte ( OPCODE_HALT ) ; } + else + if ( dump_env != NULL && + ulStrEqual ( dump_env, "always" ) ) + dump () ; return num_errors ; } - + int pslCompiler::pushReturnStatement () Index: pslCompiler.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.h,v retrieving revision 1.25 retrieving revision 1.26 diff -u -d -r1.25 -r1.26 --- pslCompiler.h 15 Sep 2002 01:34:24 -0000 1.25 +++ pslCompiler.h 15 Sep 2002 04:34:39 -0000 1.26 @@ -98,6 +98,8 @@ void pushGreaterEqual () ; void pushNotEqual () ; void pushEqual () ; + int pushPeekJumpIfFalse ( int l ) ; + int pushPeekJumpIfTrue ( int l ) ; int pushJumpIfFalse ( int l ) ; int pushJumpIfTrue ( int l ) ; int pushJump ( int l ) ; Index: pslContext.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslContext.cxx,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- pslContext.cxx 14 Sep 2002 06:12:32 -0000 1.12 +++ pslContext.cxx 15 Sep 2002 04:34:39 -0000 1.13 @@ -1,5 +1,4 @@ -/* - PLIB - A Suite of Portable Game Libraries +/* PLIB - A Suite of Portable Game Libraries Copyright (C) 1998,2002 Steve Baker This library is free software; you can redistribute it and/or @@ -442,6 +441,26 @@ case OPCODE_HALT : return PSL_PROGRAM_END ; /* Note: PC is *NOT* incremented. */ + + case OPCODE_PEEK_JUMP_TRUE : + if ( peekInt () ) + pc = code [ pc + 1 ] + ( code [ pc + 2 ] << 8 ) ; + else + { + sp-- ; + pc += 3 ; + } + return PSL_PROGRAM_CONTINUE ; + + case OPCODE_PEEK_JUMP_FALSE : + if ( peekInt () ) + { + sp-- ; + pc += 3 ; + } + else + pc = code [ pc + 1 ] + ( code [ pc + 2 ] << 8 ) ; + return PSL_PROGRAM_CONTINUE ; case OPCODE_JUMP_TRUE : if ( popInt () ) Index: pslContext.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslContext.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pslContext.h 15 Sep 2002 01:34:24 -0000 1.9 +++ pslContext.h 15 Sep 2002 04:34:39 -0000 1.10 @@ -62,6 +62,10 @@ void pushString ( const char *x ) { stack [ sp++ ] . set ( x ) ; } void pushNumber ( const pslNumber *x ) { stack [ sp++ ] . set ( x ) ; } + int peekInt () { return stack [ sp-1 ] . getInt () ; } + float peekFloat () { return stack [ sp-1 ] . getFloat () ; } + char *peekString () { return stack [ sp-1 ] . getString () ; } + void popVoid ( int n = 1 ) { sp -= n ; } int popInt () { return stack [ --sp ] . getInt () ; } float popFloat () { return stack [ --sp ] . getFloat () ; } Index: pslDump.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslDump.cxx,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- pslDump.cxx 14 Sep 2002 06:12:32 -0000 1.18 +++ pslDump.cxx 15 Sep 2002 04:34:39 -0000 1.19 @@ -65,6 +65,8 @@ { "CALLEXT", OPCODE_CALLEXT , 2 }, { "CALL", OPCODE_CALL , 3 }, { "RETURN", OPCODE_RETURN , 0 }, + { "PEEK_JUMP_FALSE", OPCODE_PEEK_JUMP_FALSE, 2 }, + { "PEEK_JUMP_TRUE", OPCODE_PEEK_JUMP_TRUE , 2 }, { "JUMP_FALSE", OPCODE_JUMP_FALSE , 2 }, { "JUMP_TRUE", OPCODE_JUMP_TRUE , 2 }, { "JUMP", OPCODE_JUMP , 2 }, @@ -262,6 +264,8 @@ code[addr+3] ) ; break ; + case OPCODE_PEEK_JUMP_FALSE : + case OPCODE_PEEK_JUMP_TRUE : case OPCODE_JUMP_FALSE : case OPCODE_JUMP_TRUE : fprintf ( fd, "\t\t%d", code[addr+1] + ( code[addr+2] << 8 ) ) ; Index: pslExpression.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslExpression.cxx,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- pslExpression.cxx 15 Sep 2002 00:02:51 -0000 1.14 +++ pslExpression.cxx 15 Sep 2002 04:34:39 -0000 1.15 @@ -337,27 +337,29 @@ if ( ! pushRelExpression () ) return FALSE ; - while ( TRUE ) - { - char c [ MAX_TOKEN ] ; + char c [ MAX_TOKEN ] ; + int shortcut ; - getToken ( c ) ; + getToken ( c ) ; - if ( strcmp ( c, "&&" ) != 0 && - strcmp ( c, "||" ) != 0 ) - { - ungetToken ( c ) ; - return TRUE ; - } + if ( strcmp ( c, "&&" ) == 0 ) + shortcut = pushPeekJumpIfFalse ( 0 ) ; + else + if ( strcmp ( c, "||" ) == 0 ) + shortcut = pushPeekJumpIfTrue ( 0 ) ; + else + { + ungetToken ( c ) ; + return TRUE ; + } - if ( ! pushRelExpression () ) - return FALSE ; + if ( ! pushBoolExpression () ) + return error ( "Missing expression following '&&' or '||'" ) ; - if ( strcmp ( c, "&&" ) == 0 ) - pushAndAnd () ; - else - pushOrOr () ; - } + code [ shortcut ] = next_code & 0xFF ; + code [ shortcut+1 ] = ( next_code >> 8 ) & 0xFF ; + + return TRUE ; } Index: pslOpcodes.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslOpcodes.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- pslOpcodes.h 14 Sep 2002 06:12:33 -0000 1.10 +++ pslOpcodes.h 15 Sep 2002 04:34:39 -0000 1.11 @@ -69,6 +69,8 @@ #define OPCODE_GET_PARAMETER 0x28 #define OPCODE_INCREMENT 0x29 #define OPCODE_DECREMENT 0x2A +#define OPCODE_PEEK_JUMP_FALSE 0x2B +#define OPCODE_PEEK_JUMP_TRUE 0x2C #define OPCODE_POP_ADD_VARIABLE 0x30 #define OPCODE_POP_SUB_VARIABLE 0x31 Index: pslProgram.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslProgram.cxx,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- pslProgram.cxx 15 Sep 2002 01:34:24 -0000 1.11 +++ pslProgram.cxx 15 Sep 2002 04:34:39 -0000 1.12 @@ -45,6 +45,11 @@ compiler-> init () ; context -> reset () ; + + char *force_trace_env = getenv ( "PSL_TRACE" ) ; + + force_trace = ( force_trace_env != NULL && + ulStrEqual ( force_trace_env, "always" ) ) ; } @@ -77,7 +82,9 @@ void pslProgram::dump () const { compiler-> dump () ; } void pslProgram::reset () { context -> reset () ; } -pslResult pslProgram::step () { return context -> step () ; } +pslResult pslProgram::step () { return force_trace ? + context -> trace () : + context -> step () ; } pslResult pslProgram::trace () { return context -> trace () ; } |