Pol  Revision:4b29d2b
eprog.h
Go to the documentation of this file.
1 
8 #ifndef BSCRIPT_EPROG_H
9 #define BSCRIPT_EPROG_H
10 
11 #include <iosfwd>
12 #include <stdio.h>
13 #include <string>
14 #include <vector>
15 
16 #include "../clib/boostutils.h"
17 #include "../clib/rawtypes.h"
18 #include "../clib/refptr.h"
19 #include "executortype.h"
20 #include "symcont.h"
21 #include "token.h"
22 
23 namespace Pol
24 {
25 namespace Plib
26 {
27 class Package;
28 }
29 namespace Bscript
30 {
31 class CompilerContext;
32 class FunctionalityModule;
33 
35 {
36 public:
37  Instruction( ExecInstrFunc f ) : token(), func( f ), cycles( 0 ) {}
38  Instruction() : token(), func( 0 ), cycles( 0 ) {}
41  mutable unsigned int cycles;
42 };
43 
44 struct EPDbgInstr
45 {
46  unsigned int blockidx;
47  unsigned int flags;
48  unsigned int fileidx;
49  unsigned int line;
50  unsigned int srclineidx;
51  unsigned int functionidx;
52 };
53 
54 struct EPDbgBlock
55 {
56  unsigned parentblockidx;
57  unsigned parentvariables;
58  std::vector<std::string> localvarnames;
59 };
60 
62 {
63  std::string name;
64  unsigned nargs;
65  unsigned PC;
66 };
67 
69 {
70  std::string name;
71  unsigned firstPC;
72  unsigned lastPC;
73 };
74 
75 struct BSCRIPT_SECTION_HDR;
76 
78 {
79 public:
81  void addToken( const Token& token );
82  void append( const StoredToken& stoken );
83  void append( const StoredToken& stoken, unsigned* posn );
84  void append( const StoredToken& stoken, const CompilerContext& ctx );
85  void append( const StoredToken& stoken, const CompilerContext& ctx, unsigned* posn );
86  void erase();
87  void clear_modules();
88  unsigned nglobals;
89  unsigned expectedArgs;
92  std::vector<FunctionalityModule*> modules;
95 
96  void dump( std::ostream& os );
97  void dump_casejmp( std::ostream& os, const Token& token );
98  int write( const char* fname );
99  int read( const char* fname );
100  int read_dbg_file();
101  int read_progdef_hdr( FILE* fp );
102  int read_module( FILE* fp );
103  int read_globalvarnames( FILE* fp );
104  int read_exported_functions( FILE* fp, BSCRIPT_SECTION_HDR* hdr );
105  int _readToken( Token& token, unsigned position ) const;
106  int create_instructions();
107 
108  int write_dbg( const char* fname, bool gen_txt );
109 
110  // compiler only:
111  unsigned program_PC;
112  std::string program_decl;
113  std::vector<std::string> sourcelines;
114  std::vector<std::string> fileline;
115  std::vector<std::string> function_decls;
116 
117  std::vector<EPExportedFunction> exported_functions;
118 
119  // executor only:
120  unsigned short version;
121  unsigned int invocations;
122  u64 instr_cycles; // FIXME need an enable-profiling flag
124  std::vector<Instruction> instr;
125 
126  // debug data:
128  unsigned savecurblock;
129  unsigned curblock;
130  unsigned curfile;
131  unsigned curline;
133  std::vector<std::string> globalvarnames;
134  std::vector<EPDbgBlock> blocks;
135  std::vector<EPDbgFunction> dbg_functions;
136  std::vector<std::string> dbg_filenames;
137 
138  // per instruction:
139  std::vector<unsigned> dbg_filenum;
140  std::vector<unsigned> dbg_linenum;
141  std::vector<unsigned> dbg_ins_blocks;
142  std::vector<bool> dbg_ins_statementbegin;
143  void setcontext( const CompilerContext& ctx );
144  void setstatementbegin();
145 
146  void enterfunction();
147  void leavefunction();
148  void enterblock();
149  void leaveblock();
150  void addlocalvar( const std::string& localvarname );
151  void addfunction( std::string name, unsigned firstPC, unsigned lastPC );
152  void update_dbg_pos( const Token& tkn );
153  void add_ins_dbg_info();
154 
155  int add_dbg_filename( const std::string& filename );
156  std::string dbg_get_instruction( size_t atPC ) const;
157 
158  unsigned varcount( unsigned block );
159  unsigned parentvariables( unsigned parent );
160  size_t sizeEstimate() const;
161 
162 private:
164  ~EScriptProgram();
165  friend class ref_ptr<EScriptProgram>;
166 };
167 
169 {
170 public:
171  explicit EScriptProgramCheckpoint( const EScriptProgram& );
172  void commit( const EScriptProgram& prog );
173  void rollback( EScriptProgram& prog ) const;
174 
175  unsigned module_count;
176  unsigned tokens_count;
177  unsigned symbols_length;
179  unsigned fileline_count;
180 };
181 }
182 }
183 #endif
Instruction(ExecInstrFunc f)
Definition: eprog.h:37
std::vector< unsigned > dbg_ins_blocks
Definition: eprog.h:141
unsigned int blockidx
Definition: eprog.h:46
unsigned parentvariables
Definition: eprog.h:57
unsigned int line
Definition: eprog.h:49
std::vector< bool > dbg_ins_statementbegin
Definition: eprog.h:142
std::vector< EPExportedFunction > exported_functions
Definition: eprog.h:117
unsigned int srclineidx
Definition: eprog.h:50
SymbolContainer symbols
Definition: eprog.h:94
boost_utils::script_name_flystring name
Definition: eprog.h:91
unsigned short version
Definition: eprog.h:120
unsigned int fileidx
Definition: eprog.h:48
void(Executor::* ExecInstrFunc)(const Instruction &)
Definition: executortype.h:16
std::vector< EPDbgFunction > dbg_functions
Definition: eprog.h:135
unsigned long long u64
Definition: rawtypes.h:38
std::vector< std::string > globalvarnames
Definition: eprog.h:133
Definition: refptr.h:65
std::vector< std::string > dbg_filenames
Definition: eprog.h:136
unsigned int functionidx
Definition: eprog.h:51
std::vector< std::string > fileline
Definition: eprog.h:114
std::vector< std::string > sourcelines
Definition: eprog.h:113
unsigned int flags
Definition: eprog.h:47
std::vector< Instruction > instr
Definition: eprog.h:124
std::vector< EPDbgBlock > blocks
Definition: eprog.h:134
boost::flyweight< std::string, boost::flyweights::tag< script_name_tag >, FLYWEIGHT_HASH_FACTORY > script_name_flystring
Definition: boostutils.h:148
std::vector< std::string > function_decls
Definition: eprog.h:115
StoredTokenContainer tokens
Definition: eprog.h:93
Plib::Package const * pkg
Definition: eprog.h:123
std::vector< unsigned > dbg_linenum
Definition: eprog.h:140
std::vector< unsigned > dbg_filenum
Definition: eprog.h:139
unsigned parentblockidx
Definition: eprog.h:56
std::vector< std::string > localvarnames
Definition: eprog.h:58
unsigned int cycles
Definition: eprog.h:41
bool commit(const std::string &basename)
Definition: uimport.cpp:968
unsigned int invocations
Definition: eprog.h:121
Definition: berror.cpp:12
std::vector< FunctionalityModule * > modules
Definition: eprog.h:92
std::string program_decl
Definition: eprog.h:112
ExecInstrFunc func
Definition: eprog.h:40