Skip to content

Commit

Permalink
Cache scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed Jul 12, 2017
1 parent 5a31e24 commit 664a4c0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
58 changes: 52 additions & 6 deletions src/cache/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ enum {
F_EXPR,
F_FILEMAP,
F_FILEPATH,
F_FLAGS,
F_FUNC,
F_HASREFMEMBER,
F_ID,
F_INDEX,
F_INSTANCE,
F_LATENT,
F_LIB,
// 20
F_LIB,
F_LINE,
F_MAXPARAM,
F_MINPARAM,
Expand All @@ -50,19 +51,20 @@ enum {
F_NAMESPACEFRAGMENT,
F_NULLABLE,
F_OBJECT,
F_OFFSET,
// 30
F_OFFSET,
F_OPCODE,
F_PARAM,
F_PATH,
F_POS,
F_REF,
F_RETURNSPEC,
F_SCRIPT,
F_SCRIPTCALLABLE,
F_SIZE,
// 40
F_SPEC,
F_STORAGE,
// 40
F_STORAGEINDEX,
F_STRICT,
F_STRUCTURE,
Expand All @@ -71,9 +73,9 @@ enum {
F_TYPE,
F_TYPEALIAS,
F_VALUE,
// 50
F_VALUESTRING,
F_VAR,
// 50
F_UNREACHABLE,
F_UPMOST,
};
Expand Down Expand Up @@ -115,6 +117,7 @@ static void save_impl( struct saver* saver, struct func* func );
static void save_param_list( struct saver* saver, struct param* param );
static void save_const_expr( struct saver* saver, struct expr* expr );
static void save_object( struct saver* saver, struct object* object );
static void save_script( struct saver* saver, struct script* script );
static void save_pos( struct saver* saver, struct pos* pos );
static int map_file( struct saver* saver, int id );
static const char* name_s( struct saver* saver, struct name* name, bool full );
Expand Down Expand Up @@ -178,12 +181,20 @@ static void save_namespace_path( struct saver* saver,

static void save_namespace_member_list( struct saver* saver,
struct ns_fragment* fragment ) {
// Objects.
struct list_iter i;
list_iterate( &fragment->objects, &i );
while ( ! list_end( &i ) ) {
save_namespace_member( saver, list_data( &i ) );
list_next( &i );
}
// Scripts.
// Scripts are saved for the purpose of generating warnings.
list_iterate( &fragment->scripts, &i );
while ( ! list_end( &i ) ) {
save_script( saver, list_data( &i ) );
list_next( &i );
}
}

static void save_namespace_member( struct saver* saver,
Expand Down Expand Up @@ -545,6 +556,16 @@ static void save_object( struct saver* saver, struct object* object ) {
WF( saver, F_END );
}

// NOTE: Script parameters are not saved.
static void save_script( struct saver* saver, struct script* script ) {
WF( saver, F_SCRIPT );
save_pos( saver, &script->pos );
save_const_expr( saver, script->number );
WV( saver, F_TYPE, &script->type );
WV( saver, F_FLAGS, &script->flags );
WF( saver, F_END );
}

static void save_pos( struct saver* saver, struct pos* pos ) {
WF( saver, F_POS );
WV( saver, F_LINE, &pos->line );
Expand Down Expand Up @@ -631,6 +652,7 @@ static struct param* restore_param_list( struct restorer* restorer );
static struct expr* restore_const_expr( struct restorer* restorer );
static void restore_object( struct restorer* restorer, struct object* object,
int node );
static void restore_script( struct restorer* restorer );
static void restore_pos( struct restorer* restorer, struct pos* pos );
static struct file_entry* map_id( struct restorer* restorer, int id );

Expand Down Expand Up @@ -746,8 +768,18 @@ static void restore_namespace_list( struct restorer* restorer ) {
}

static void restore_namespace_member_list( struct restorer* restorer ) {
while ( f_peek( restorer->r ) != F_END ) {
restore_namespace_member( restorer );
bool done = false;
while ( ! done ) {
switch ( f_peek( restorer->r ) ) {
case F_SCRIPT:
restore_script( restorer );
break;
case F_END:
done = true;
break;
default:
restore_namespace_member( restorer );
}
}
}

Expand Down Expand Up @@ -1221,6 +1253,20 @@ static void restore_object( struct restorer* restorer, struct object* object,
RF( restorer, F_END );
}

static void restore_script( struct restorer* restorer ) {
RF( restorer, F_SCRIPT );
struct script* script = t_alloc_script();
restore_pos( restorer, &script->pos );
script->number = restore_const_expr( restorer );
RV( restorer, F_TYPE, &script->type );
RV( restorer, F_FLAGS, &script->flags );
RF( restorer, F_END );
list_append( &restorer->lib->scripts, script );
list_append( &restorer->lib->objects, script );
list_append( &restorer->ns_fragment->scripts, script );
list_append( &restorer->ns_fragment->runnables, script );
}

static void restore_pos( struct restorer* restorer, struct pos* pos ) {
RF( restorer, F_POS );
RV( restorer, F_LINE, &pos->line );
Expand Down
15 changes: 2 additions & 13 deletions src/parse/dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ static void init_script_reading( struct script_reading* reading,
reading->param_tail = NULL;
reading->num_param = 0;
reading->type = SCRIPT_TYPE_CLOSED;
reading->flags = 0;
reading->flags = SCRIPT_FLAG_NONE;
reading->param_specified = false;
}

Expand Down Expand Up @@ -2534,24 +2534,13 @@ static void read_script_body( struct parse* parse,

static struct script* add_script( struct parse* parse,
struct script_reading* reading ) {
struct script* script = mem_alloc( sizeof( *script ) );
script->node.type = NODE_SCRIPT;
struct script* script = t_alloc_script();
script->pos = reading->pos;
script->number = reading->number;
script->type = reading->type;
script->flags = reading->flags;
script->params = reading->param;
script->body = NULL;
script->nested_funcs = NULL;
script->nested_calls = NULL;
list_init( &script->labels );
list_init( &script->vars );
list_init( &script->funcscope_vars );
script->assigned_number = 0;
script->num_param = reading->num_param;
script->offset = 0;
script->size = 0;
script->named_script = false;
list_append( &parse->lib->scripts, script );
list_append( &parse->lib->objects, script );
list_append( &parse->ns_fragment->scripts, script );
Expand Down
22 changes: 22 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -1168,3 +1168,25 @@ const char* t_get_lang_lib_dir( struct task* task, int lang ) {
return NULL;
}
}

struct script* t_alloc_script( void ) {
struct script* script = mem_alloc( sizeof( *script ) );
script->node.type = NODE_SCRIPT;
t_init_pos_id( &script->pos, INTERNALFILE_COMPILER );
script->number = NULL;
script->type = SCRIPT_TYPE_CLOSED;
script->flags = SCRIPT_FLAG_NONE;
script->params = NULL;
script->body = NULL;
script->nested_funcs = NULL;
script->nested_calls = NULL;
list_init( &script->labels );
list_init( &script->vars );
list_init( &script->funcscope_vars );
script->assigned_number = 0;
script->num_param = 0;
script->offset = 0;
script->size = 0;
script->named_script = false;
return script;
}
2 changes: 2 additions & 0 deletions src/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ struct script {
SCRIPT_TYPE_TOTAL = SCRIPT_TYPE_NEXTFREENUMBER
} type;
enum {
SCRIPT_FLAG_NONE = 0x0,
SCRIPT_FLAG_NET = 0x1,
SCRIPT_FLAG_CLIENTSIDE = 0x2
} flags;
Expand Down Expand Up @@ -1356,5 +1357,6 @@ struct include_history_entry* t_alloc_include_history_entry(
struct include_history_entry* t_decode_include_history_entry(
struct task* task, int id );
const char* t_get_lang_lib_dir( struct task* task, int lang );
struct script* t_alloc_script( void );

#endif

0 comments on commit 664a4c0

Please sign in to comment.