11 #include "../clib/clib.h" 12 #include "../clib/logfacility.h" 13 #include "../clib/passert.h" 14 #include "../clib/rawtypes.h" 15 #include "../clib/stlutil.h" 106 if ( token.
id > 255 )
108 throw std::runtime_error(
"Trying to write an illegal token" );
113 switch ( token.
type )
166 static_cast<BTokenType>(
174 static_cast<unsigned>( token.
dval ) ),
190 static_cast<BTokenType>(
240 ERROR_PRINT <<
"AddToken: Can't handle TYP_CONTROL: " << token <<
"\n";
241 throw std::runtime_error(
"Unexpected token in AddToken() (1)" );
246 ERROR_PRINT <<
"AddToken: Can't handle " << token <<
"\n";
247 throw std::runtime_error(
"Unexpected Token passed to AddToken() (2)" );
257 FILE* fp = fopen( fname,
"wb" );
266 fwrite( &hdr,
sizeof hdr, 1, fp );
273 memset( &progdef_hdr, 0,
sizeof progdef_hdr );
275 sechdr.
length =
sizeof progdef_hdr;
276 fwrite( &sechdr,
sizeof sechdr, 1, fp );
278 fwrite( &progdef_hdr,
sizeof progdef_hdr, 1, fp );
281 for (
auto& module : program.
modules )
285 fwrite( &sechdr,
sizeof sechdr, 1, fp );
288 memset( &modhdr, 0,
sizeof modhdr );
290 modhdr.
nfuncs =
static_cast<unsigned int>( module->used_functions.size() );
291 fwrite( &modhdr,
sizeof modhdr, 1, fp );
292 for (
auto& module_func : module->used_functions )
295 memset( &func, 0,
sizeof func );
299 func.
nargs =
static_cast<unsigned char>( module_func->nargs );
301 fwrite( &func,
sizeof func, 1, fp );
307 fwrite( &sechdr,
sizeof sechdr, 1, fp );
312 fwrite( &sechdr,
sizeof sechdr, 1, fp );
320 fwrite( &sechdr,
sizeof sechdr, 1, fp );
324 bef.
nargs = elem.nargs;
326 fwrite( &bef,
sizeof bef, 1, fp );
342 module_count =
static_cast<unsigned int>( prog.
modules.size() );
345 sourcelines_count =
static_cast<unsigned int>( prog.
sourcelines.size() );
346 fileline_count =
static_cast<unsigned int>( prog.
fileline.size() );
351 unsigned cnt =
static_cast<unsigned int>(
blocks[block].localvarnames.size() );
369 FILE* fp = fopen( fname,
"wb" );
375 fptext = fopen( ( std::string( fname ) +
".txt" ).c_str(),
"wt" );
381 fwrite( &count,
sizeof count, 1, fp );
384 fwrite( &count,
sizeof count, 1, fp );
388 fprintf( fptext,
"File %u: %s\n", i,
dbg_filenames[i].c_str() );
389 count =
static_cast<unsigned int>(
dbg_filenames[i].size() + 1 );
390 fwrite( &count,
sizeof count, 1, fp );
394 fwrite( &count,
sizeof count, 1, fp );
398 fprintf( fptext,
"Global %u: %s\n", i,
globalvarnames[i].c_str() );
399 count =
static_cast<unsigned int>(
globalvarnames[i].size() + 1 );
400 fwrite( &count,
sizeof count, 1, fp );
404 fwrite( &count,
sizeof count, 1, fp );
417 fwrite( &ins,
sizeof ins, 1, fp );
419 count =
static_cast<unsigned int>(
blocks.size() );
420 fwrite( &count,
sizeof count, 1, fp );
421 for (
unsigned i = 0; i <
blocks.size(); ++i )
426 fprintf( fptext,
"Block %u:\n", i );
432 fwrite( &tmp,
sizeof tmp, 1, fp );
434 fwrite( &tmp,
sizeof tmp, 1, fp );
436 count =
static_cast<unsigned int>( block.
localvarnames.size() );
437 fwrite( &count,
sizeof count, 1, fp );
440 unsigned int varlast =
static_cast<unsigned int>( varfirst + block.
localvarnames.size() - 1 );
441 if ( varlast >= varfirst )
444 fprintf( fptext,
" Local variables %u-%u: \n", varfirst, varlast );
449 fprintf( fptext,
" %u: %s\n", varfirst + j, varname.c_str() );
451 count =
static_cast<unsigned int>( varname.size() + 1 );
452 fwrite( &count,
sizeof count, 1, fp );
453 fwrite( varname.c_str(),
count, 1, fp );
458 fwrite( &count,
sizeof count, 1, fp );
464 fprintf( fptext,
"Function %u: %s\n", i, func.
name.c_str() );
465 fprintf( fptext,
" FirstPC=%u, lastPC=%u\n", func.
firstPC, func.
lastPC );
467 count =
static_cast<unsigned int>( func.
name.size() + 1 );
468 fwrite( &count,
sizeof count, 1, fp );
469 fwrite( func.
name.c_str(),
count, 1, fp );
472 fwrite( &tmp,
sizeof tmp, 1, fp );
474 fwrite( &tmp,
sizeof tmp, 1, fp );
505 os <<
instr[atPC].token;
530 blocks.push_back( block );
538 blocks.push_back( block );
550 if ( elem >=
blocks.size() )
562 func.
name = funcname;
571 size += 3 *
sizeof( std::string* ) +
sourcelines.capacity() *
sizeof( std::string );
573 size += l.capacity();
574 size += 3 *
sizeof( std::string* ) +
fileline.capacity() *
sizeof( std::string );
576 size += l.capacity();
577 size += 3 *
sizeof( std::string* ) +
function_decls.capacity() *
sizeof( std::string );
579 size += l.capacity();
580 size += 3 *
sizeof( std::string* ) +
globalvarnames.capacity() *
sizeof( std::string );
582 size += l.capacity();
583 size += 3 *
sizeof( std::string* ) +
dbg_filenames.capacity() *
sizeof( std::string );
585 size += l.capacity();
586 size += 3 *
sizeof(
unsigned* ) +
dbg_filenum.capacity() *
sizeof( unsigned );
587 size += 3 *
sizeof(
unsigned* ) +
dbg_linenum.capacity() *
sizeof( unsigned );
588 size += 3 *
sizeof(
unsigned* ) +
dbg_ins_blocks.capacity() *
sizeof( unsigned );
std::vector< unsigned > dbg_ins_blocks
void append_tok(const StoredToken &token, unsigned *position=NULL)
void setcontext(const CompilerContext &ctx)
#define ESCRIPT_FILE_VER_CURRENT
std::vector< bool > dbg_ins_statementbegin
const char * tokval() const
#define BSCRIPT_FILE_MAGIC0
std::vector< EPExportedFunction > exported_functions
Addresses forward_callers
#define OSTRINGSTREAM_STR(x)
void addfunction(std::string name, unsigned firstPC, unsigned lastPC)
int write(const char *fname)
virtual void write(FILE *fp)
unsigned parentvariables(unsigned parent)
std::vector< EPDbgFunction > dbg_functions
int add_dbg_filename(const std::string &filename)
EScriptProgramCheckpoint(const EScriptProgram &)
unsigned int get_write_length() const
std::vector< std::string > globalvarnames
#define BSCRIPT_FILE_MAGIC1
void commit(const EScriptProgram &prog)
std::vector< std::string > dbg_filenames
void append(const char *string, unsigned &position)
std::vector< std::string > fileline
unsigned count(void) const
std::vector< std::string > sourcelines
std::string dbg_get_instruction(size_t atPC) const
std::vector< Instruction > instr
unsigned length(void) const
void update_dbg_pos(const Token &tkn)
std::vector< EPDbgBlock > blocks
unsigned int count() const
std::vector< std::string > function_decls
StoredTokenContainer tokens
size_t sizeEstimate() const
int write_dbg(const char *fname, bool gen_txt)
std::vector< unsigned > dbg_linenum
std::vector< unsigned > dbg_filenum
std::vector< std::string > localvarnames
void addlocalvar(const std::string &localvarname)
unsigned varcount(unsigned block)
void addToken(const Token &token)
bool commit(const std::string &basename)
void append(const StoredToken &stoken)
std::vector< FunctionalityModule * > modules