The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

compile.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 1993, 2000 Christopher Seiwald.
00003  *
00004  * This file is part of Jam - see jam.c for Copyright information.
00005  */
00006 
00007 /*  This file is ALSO:
00008  *  (C) Copyright David Abrahams 2001. Permission to copy, use,
00009  *  modify, sell and distribute this software is granted provided this
00010  *  copyright notice appears in all copies. This software is provided
00011  *  "as is" without express or implied warranty, and with no claim as
00012  *  to its suitability for any purpose.
00013  */
00014 
00015 # include "jam.h"
00016 
00017 # include "lists.h"
00018 # include "parse.h"
00019 # include "compile.h"
00020 # include "variable.h"
00021 # include "expand.h"
00022 # include "rules.h"
00023 # include "newstr.h"
00024 # include "make.h"
00025 # include "search.h"
00026 # include "hdrmacro.h"
00027 # include "hash.h"
00028 # include "modules.h"
00029 # include "strings.h"
00030 # include "builtins.h"
00031 # include "class.h"
00032 
00033 # include <time.h>
00034 # include <assert.h>
00035 # include <string.h>
00036 
00037 /*
00038  * compile.c - compile parsed jam statements
00039  *
00040  * External routines:
00041  *
00042  *  compile_append() - append list results of two statements
00043  *      compile_eval() - evaluate if to determine which leg to compile
00044  *  compile_foreach() - compile the "for x in y" statement
00045  *  compile_if() - compile 'if' rule
00046  *  compile_while() - compile 'while' rule
00047  *  compile_include() - support for 'include' - call include() on file
00048  *  compile_list() - expand and return a list 
00049  *  compile_local() - declare (and set) local variables
00050  *  compile_null() - do nothing -- a stub for parsing
00051  *  compile_on() - run rule under influence of on-target variables
00052  *  compile_rule() - compile a single user defined rule
00053  *  compile_rules() - compile a chain of rules
00054  *  compile_set() - compile the "set variable" statement
00055  *  compile_setcomp() - support for `rule` - save parse tree 
00056  *  compile_setexec() - support for `actions` - save execution string 
00057  *  compile_settings() - compile the "on =" (set variable on exec) statement
00058  *  compile_switch() - compile 'switch' rule
00059  *
00060  * Internal routines:
00061  *
00062  *  debug_compile() - printf with indent to show rule expansion.
00063  *  evaluate_rule() - execute a rule invocation
00064  *
00065  *  builtin_depends() - DEPENDS/INCLUDES rule
00066  *  builtin_echo() - ECHO rule
00067  *  builtin_exit() - EXIT rule
00068  *  builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
00069  *
00070  * 02/03/94 (seiwald) - Changed trace output to read "setting" instead of 
00071  *          the awkward sounding "settings".
00072  * 04/12/94 (seiwald) - Combined build_depends() with build_includes().
00073  * 04/12/94 (seiwald) - actionlist() now just appends a single action.
00074  * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
00075  * 05/13/94 (seiwald) - include files are now bound as targets, and thus
00076  *          can make use of $(SEARCH)
00077  * 06/01/94 (seiwald) - new 'actions existing' does existing sources
00078  * 08/23/94 (seiwald) - Support for '+=' (append to variable)
00079  * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
00080  * 01/22/95 (seiwald) - Exit rule.
00081  * 02/02/95 (seiwald) - Always rule; LEAVES rule.
00082  * 02/14/95 (seiwald) - NoUpdate rule.
00083  * 09/11/00 (seiwald) - new evaluate_rule() for headers().
00084  * 09/11/00 (seiwald) - compile_xxx() now return LIST *.
00085  *          New compile_append() and compile_list() in
00086  *          support of building lists here, rather than
00087  *          in jamgram.yy.
00088  * 01/10/00 (seiwald) - built-ins split out to builtin.c.
00089  */
00090 
00091 static void debug_compile( int which, char *s, FRAME* frame );
00092 int glob( char *s, char *c );
00093 /* Internal functions from builtins.c */
00094 void backtrace( FRAME *frame );
00095 void backtrace_line( FRAME *frame );
00096 void print_source_line( PARSE* p );
00097 
00098 
00099 void frame_init( FRAME* frame )
00100 {
00101     frame->prev = 0;
00102     lol_init(frame->args);
00103     frame->module = root_module();
00104     frame->rulename = "module scope";
00105     frame->procedure = 0;
00106 }
00107 
00108 void frame_free( FRAME* frame )
00109 {
00110     lol_free( frame->args );
00111 }
00112 
00113 /*
00114  * compile_append() - append list results of two statements
00115  *
00116  *  parse->left more compile_append() by left-recursion
00117  *  parse->right    single rule
00118  */
00119 
00120 LIST *
00121 compile_append(
00122     PARSE   *parse,
00123     FRAME *frame )
00124 {
00125     /* Append right to left. */
00126 
00127     return list_append( 
00128         parse_evaluate( parse->left, frame ),
00129         parse_evaluate( parse->right, frame ) );
00130 }
00131 
00132 /*
00133  * compile_eval() - evaluate if to determine which leg to compile
00134  *
00135  * Returns:
00136  *      list    if expression true - compile 'then' clause
00137  *      L0      if expression false - compile 'else' clause
00138  */
00139 
00140 static int
00141 lcmp( LIST *t, LIST *s )
00142 {
00143         int status = 0;
00144 
00145         while( !status && ( t || s ) )
00146         {
00147             char *st = t ? t->string : "";
00148             char *ss = s ? s->string : "";
00149 
00150             status = strcmp( st, ss );
00151 
00152             t = t ? list_next( t ) : t;
00153             s = s ? list_next( s ) : s;
00154         }
00155 
00156         return status;
00157 }
00158 
00159 LIST *
00160 compile_eval(
00161         PARSE   *parse,
00162         FRAME   *frame )
00163 {
00164         LIST *ll, *lr, *s, *t;
00165         int status = 0;
00166 
00167         /* Short circuit lr eval for &&, ||, and 'in' */
00168 
00169         ll = parse_evaluate( parse->left, frame );
00170         lr = 0;
00171 
00172         switch( parse->num )
00173         {
00174         case EXPR_AND: 
00175         case EXPR_IN:   if( ll ) goto eval; break;
00176         case EXPR_OR:   if( !ll ) goto eval; break;
00177         default: eval:  lr = parse_evaluate( parse->right, frame );
00178         }
00179 
00180         /* Now eval */
00181 
00182         switch( parse->num )
00183         {
00184         case EXPR_NOT:  
00185                 if( !ll ) status = 1;
00186                 break;
00187 
00188         case EXPR_AND:
00189                 if( ll && lr ) status = 1;
00190                 break;
00191 
00192         case EXPR_OR:
00193                 if( ll || lr ) status = 1;
00194                 break;
00195 
00196         case EXPR_IN:
00197                 /* "a in b": make sure each of */
00198                 /* ll is equal to something in lr. */
00199 
00200                 for( t = ll; t; t = list_next( t ) )
00201                 {
00202                     for( s = lr; s; s = list_next( s ) )
00203                         if( !strcmp( t->string, s->string ) )
00204                             break;
00205                     if( !s ) break;
00206                 }
00207 
00208                 /* No more ll? Success */
00209 
00210                 if( !t ) status = 1;
00211 
00212                 break;
00213 
00214         case EXPR_EXISTS:       if( lcmp( ll, L0 ) != 0 ) status = 1; break;
00215         case EXPR_EQUALS:       if( lcmp( ll, lr ) == 0 ) status = 1; break;
00216         case EXPR_NOTEQ:        if( lcmp( ll, lr ) != 0 ) status = 1; break;
00217         case EXPR_LESS:         if( lcmp( ll, lr ) < 0  ) status = 1; break;
00218         case EXPR_LESSEQ:       if( lcmp( ll, lr ) <= 0 ) status = 1; break;
00219         case EXPR_MORE:         if( lcmp( ll, lr ) > 0  ) status = 1; break;
00220         case EXPR_MOREEQ:       if( lcmp( ll, lr ) >= 0 ) status = 1; break;
00221 
00222         }
00223 
00224         if( DEBUG_IF )
00225         {
00226             debug_compile( 0, "if", frame );
00227             list_print( ll );
00228             printf( "(%d) ", status );
00229             list_print( lr );
00230             printf( "\n" );
00231         }
00232 
00233         /* Find something to return. */
00234         /* In odd circumstances (like "" = "") */
00235         /* we'll have to return a new string. */
00236 
00237         if( !status ) t = 0;
00238         else if( ll ) t = ll, ll = 0;
00239         else if( lr ) t = lr, lr = 0;
00240         else t = list_new( L0, newstr( "1" ) );
00241 
00242         if( ll ) list_free( ll );
00243         if( lr ) list_free( lr );
00244         return t;
00245 }
00246 
00247 
00248 /*
00249  * compile_foreach() - compile the "for x in y" statement
00250  *
00251  * Compile_foreach() resets the given variable name to each specified
00252  * value, executing the commands enclosed in braces for each iteration.
00253  *
00254  *  parse->string   index variable
00255  *  parse->left variable values
00256  *  parse->right    rule to compile
00257  */
00258 
00259 LIST *
00260 compile_foreach(
00261     PARSE   *parse,
00262     FRAME *frame )
00263 {
00264     LIST    *nv = parse_evaluate( parse->left, frame );
00265     LIST    *l;
00266     SETTINGS *s = 0;
00267         
00268         if ( parse->num )
00269         {
00270             s = addsettings( s, 0, parse->string, L0 );
00271             pushsettings( s );
00272         }
00273 
00274     /* Call var_set to reset $(parse->string) for each val. */
00275 
00276     for( l = nv; l; l = list_next( l ) )
00277     {
00278         LIST *val = list_new( L0, copystr( l->string ) );
00279 
00280         var_set( parse->string, val, VAR_SET );
00281 
00282         list_free( parse_evaluate( parse->right, frame ) );
00283     }
00284 
00285         if ( parse->num )
00286         {
00287             popsettings( s );
00288             freesettings( s );
00289         }
00290 
00291     list_free( nv );
00292 
00293     return L0;
00294 }
00295 
00296 /*
00297  * compile_if() - compile 'if' rule
00298  *
00299  *  parse->left     condition tree
00300  *  parse->right        then tree
00301  *  parse->third        else tree
00302  */
00303 
00304 LIST *
00305 compile_if(
00306     PARSE   *p,
00307     FRAME *frame )
00308 {
00309     LIST *l = parse_evaluate( p->left, frame );
00310     if( l )
00311     {
00312         list_free( l );
00313         return parse_evaluate( p->right, frame );
00314     }
00315     else
00316     {
00317         return parse_evaluate( p->third, frame );
00318     }
00319 }
00320 
00321 LIST *
00322 compile_while(
00323     PARSE   *p,
00324     FRAME *frame )
00325 {
00326     LIST *r = 0;
00327     LIST *l;
00328     while ( l = parse_evaluate( p->left, frame ) )
00329     {
00330         list_free( l );
00331         if( r ) list_free( r );
00332         r = parse_evaluate( p->right, frame );
00333     }
00334     return r;
00335 }
00336 
00337 
00338 /*
00339  * compile_include() - support for 'include' - call include() on file
00340  *
00341  *  parse->left list of files to include (can only do 1)
00342  */
00343 
00344 LIST *
00345 compile_include(
00346     PARSE   *parse,
00347     FRAME *frame )
00348 {
00349     LIST    *nt = parse_evaluate( parse->left, frame );
00350 
00351     if( DEBUG_COMPILE )
00352     {
00353         debug_compile( 0, "include", frame);
00354         list_print( nt );
00355         printf( "\n" );
00356     }
00357 
00358     if( nt )
00359     {
00360         TARGET *t = bindtarget( nt->string );
00361 
00362             /* DWA 2001/10/22 - Perforce Jam clears the arguments here, which
00363              * prevents an included file from being treated as part of the body
00364              * of a rule. I didn't see any reason to do that, so I lifted the
00365              * restriction.
00366              */
00367                
00368         /* Bind the include file under the influence of */
00369         /* "on-target" variables.  Though they are targets, */
00370         /* include files are not built with make(). */
00371 
00372         pushsettings( t->settings );
00373         /* We don't expect that file to be included is generated by some
00374            action. Therefore, pass 0 as third argument. */
00375         t->boundname = search( t->name, &t->time, 0 );
00376         popsettings( t->settings );
00377 
00378         parse_file( t->boundname, frame );
00379     }
00380 
00381     list_free( nt );
00382 
00383     return L0;
00384 }
00385 
00386 static LIST* evaluate_in_module ( char* module_name, PARSE * p, FRAME* frame)
00387 {
00388     LIST* result;
00389 
00390     module_t* outer_module = frame->module;
00391     frame->module = module_name ? bindmodule( module_name ) : root_module();
00392 
00393     if ( outer_module != frame->module )
00394     {
00395         exit_module( outer_module );
00396         enter_module( frame->module );
00397     }
00398     
00399     result = parse_evaluate( p, frame );
00400     
00401     if ( outer_module != frame->module )
00402     {
00403         exit_module( frame->module );
00404         enter_module( outer_module );
00405         frame->module = outer_module;
00406     }
00407 
00408     return result;
00409 }
00410 
00411 LIST *
00412 compile_module(
00413     PARSE   *p,
00414     FRAME *frame )
00415 {
00416     /* Here we are entering a module declaration block. 
00417      */
00418     LIST* module_name = parse_evaluate( p->left, frame );
00419     LIST* result = evaluate_in_module( module_name ? module_name->string : 0, 
00420                                        p->right, frame );
00421     
00422     list_free( module_name );
00423     return result;
00424 }
00425 
00426 LIST *
00427 compile_class( 
00428     PARSE *p, 
00429     FRAME *frame )
00430 {
00434     char* class_module = 0;
00435 
00436     LIST* name = parse_evaluate( p->left->right, frame );
00437     LIST* bases = 0;
00438 
00439     if (p->left->left)
00440         bases = parse_evaluate( p->left->left->right, frame );
00441 
00442     class_module = make_class_module(name, bases, frame);    
00443     evaluate_in_module( class_module, p->right, frame );
00444 
00445     return L0;    
00446 }
00447 
00448 
00449 /*
00450  * compile_list() - expand and return a list 
00451  *
00452  *  parse->string - character string to expand
00453  */
00454 
00455 LIST *
00456 compile_list(
00457     PARSE   *parse,
00458     FRAME *frame )
00459 {
00460     /* voodoo 1 means: s is a copyable string */
00461     char *s = parse->string;
00462     return var_expand( L0, s, s + strlen( s ), frame->args, 1 );
00463 }
00464 
00465 /*
00466  * compile_local() - declare (and set) local variables
00467  *
00468  *  parse->left list of variables
00469  *  parse->right    list of values
00470  *  parse->third    rules to execute
00471  */
00472 
00473 LIST *
00474 compile_local(
00475     PARSE   *parse,
00476     FRAME *frame )
00477 {
00478     LIST *l;
00479     SETTINGS *s = 0;
00480     LIST    *nt = parse_evaluate( parse->left, frame );
00481     LIST    *ns = parse_evaluate( parse->right, frame );
00482     LIST    *result;
00483 
00484     if( DEBUG_COMPILE )
00485     {
00486         debug_compile( 0, "local", frame);
00487         list_print( nt );
00488         printf( " = " );
00489         list_print( ns );
00490         printf( "\n" );
00491     }
00492 
00493     /* Initial value is ns */
00494 
00495     for( l = nt; l; l = list_next( l ) )
00496         s = addsettings( s, 0, l->string, list_copy( (LIST*)0, ns ) );
00497 
00498     list_free( ns );
00499     list_free( nt );
00500 
00501     /* Note that callees of the current context get this "local" */
00502     /* variable, making it not so much local as layered. */
00503 
00504     pushsettings( s );
00505     result = parse_evaluate( parse->third, frame );
00506     popsettings( s );
00507 
00508     freesettings( s );
00509 
00510     return result;
00511 }
00512 
00513 /*
00514  * compile_null() - do nothing -- a stub for parsing
00515  */
00516 
00517 LIST *
00518 compile_null(
00519     PARSE   *parse,
00520     FRAME *frame )
00521 {
00522     return L0;
00523 }
00524 
00525 /*
00526  * compile_on() - run rule under influence of on-target variables
00527  *
00528  *      parse->left     list of files to include (can only do 1)
00529  *      parse->right    rule to run
00530  *
00531  * EXPERIMENTAL!
00532  */
00533 
00534 LIST *
00535 compile_on(
00536         PARSE   *parse,
00537         FRAME   *frame )
00538 {
00539         LIST    *nt = parse_evaluate( parse->left, frame );
00540         LIST    *result = 0;
00541 
00542         if( DEBUG_COMPILE )
00543         {
00544             debug_compile( 0, "on", frame );
00545             list_print( nt );
00546             printf( "\n" );
00547         }
00548 
00549         if( nt )
00550         {
00551             TARGET *t = bindtarget( nt->string );
00552             pushsettings( t->settings );
00553 
00554             result = parse_evaluate( parse->right, frame );
00555 
00556             popsettings( t->settings );
00557         }
00558 
00559         list_free( nt );
00560 
00561         return result;
00562 }
00563 
00564 
00565 /*
00566  * compile_rule() - compile a single user defined rule
00567  *
00568  *  parse->string   name of user defined rule
00569  *  parse->left parameters (list of lists) to rule, recursing left
00570  *
00571  * Wrapped around evaluate_rule() so that headers() can share it.
00572  */
00573 
00574 LIST *
00575 compile_rule(
00576     PARSE   *parse,
00577     FRAME *frame )
00578 {
00579     FRAME       inner[1];
00580     LIST    *result;
00581     PARSE   *p;
00582     
00583 
00584     /* Build up the list of arg lists */
00585 
00586     frame_init( inner );
00587     inner->prev = frame;
00588     inner->module = frame->module; /* This gets fixed up in evaluate_rule(), below */
00589     inner->procedure = parse;
00590 
00591     for( p = parse->left; p; p = p->left )
00592         lol_add( inner->args, parse_evaluate( p->right, frame ) );
00593 
00594     /* And invoke rule */
00595 
00596     result = evaluate_rule( parse->string, inner );
00597 
00598     frame_free( inner );
00599 
00600     return result;
00601 }
00602 
00603 static void argument_error( char* message, RULE* rule, FRAME* frame, LIST* arg )
00604 {
00605     LOL* actual = frame->args;
00606     assert( frame->procedure != 0 );
00607     backtrace_line( frame->prev );
00608     printf( "*** argument error\n* rule %s ( ", frame->rulename );
00609     lol_print( rule->arguments->data );
00610     printf( " )\n* called with: ( " );
00611     lol_print( actual );
00612     printf( " )\n* %s %s\n", message, arg ? arg->string : "" );
00613     print_source_line( rule->procedure );
00614     printf( "see definition of rule '%s' being called\n", rule->name );
00615     backtrace( frame->prev );
00616     exit(1);
00617 }
00618 
00619 /* define delimiters for type check elements in argument lists (and
00620  * return type specifications, eventually)
00621  */
00622 # define TYPE_OPEN_DELIM '['
00623 # define TYPE_CLOSE_DELIM ']'
00624 
00625 /* is_type_name - true iff the given string represents a type check
00626  * specification
00627  */
00628 static int
00629 is_type_name( char* s )
00630 {
00631     return s[0] == TYPE_OPEN_DELIM
00632         && s[strlen(s) - 1] == TYPE_CLOSE_DELIM;
00633 }
00634 
00635 /*
00636  * arg_modifier - if the next element of formal is a single character,
00637  * return that; return 0 otherwise.  Used to extract "*+?" modifiers
00638  * from argument lists.
00639  */
00640 static char
00641 arg_modifier( LIST* formal )
00642 {
00643     if ( formal->next )
00644     {
00645         char *next = formal->next->string;
00646         if ( next && next[0] != 0 && next[1] == 0 )
00647             return next[0];
00648     }
00649     return 0;
00650 }
00651 
00652 /*
00653  * type_check - checks that each element of values satisfies the
00654  * requirements of type_name.
00655  *
00656  *      caller   - the frame of the rule calling the rule whose
00657  *                 arguments are being checked
00658  *
00659  *      called   - the rule being called
00660  *
00661  *      arg_name - a list element containing the name of the argument
00662  *                 being checked
00663  */
00664 static void
00665 type_check( char* type_name, LIST *values, FRAME* caller, RULE* called, LIST* arg_name )
00666 {
00667     static module_t *typecheck = 0;
00668 
00669     /* if nothing to check, bail now */
00670     if ( !values || !type_name )
00671         return;
00672 
00673     if ( !typecheck )
00674         typecheck = bindmodule(".typecheck");
00675 
00676     /* if the checking rule can't be found, also bail */
00677     {
00678         RULE checker_, *checker = &checker_;
00679 
00680         checker->name = type_name;
00681         if ( !typecheck->rules || !hashcheck( typecheck->rules, (HASHDATA**)&checker ) )
00682             return;
00683     }
00684     
00685     exit_module( caller->module );
00686     
00687     while ( values != 0 )
00688     {
00689         LIST *error;
00690         FRAME frame[1];
00691         frame_init( frame );
00692         frame->module = typecheck;
00693         frame->prev = caller;
00694 
00695         enter_module( typecheck );
00696         /* Prepare the argument list */
00697         lol_add( frame->args, list_new( L0, values->string ) );
00698         error = evaluate_rule( type_name, frame );
00699         
00700         exit_module( typecheck );
00701         
00702         if ( error )
00703             argument_error( error->string, called, caller, arg_name );
00704 
00705         frame_free( frame );
00706                 values = values->next;
00707     }
00708 
00709     enter_module( caller->module );
00710 }
00711 
00712 /*
00713  * collect_arguments() - local argument checking and collection
00714  */
00715 static SETTINGS *
00716 collect_arguments( RULE* rule, FRAME* frame )
00717 {
00718     SETTINGS *locals = 0;
00719     
00720     LOL* all_actual = frame->args;
00721     LOL *all_formal = rule->arguments ? rule->arguments->data : 0;
00722     if ( all_formal ) /* Nothing to set; nothing to check */
00723     {
00724         int max = all_formal->count > all_actual->count
00725             ? all_formal->count
00726             : all_actual->count;
00727         
00728         int n;
00729         for ( n = 0; n < max ; ++n )
00730         {
00731             LIST *actual = lol_get( all_actual, n );
00732             char *type_name = 0;
00733             
00734             LIST *formal;
00735             for ( formal = lol_get( all_formal, n ); formal; formal = formal->next )
00736             {
00737                 char* name = formal->string;
00738 
00739                 if ( is_type_name(name) )
00740                 {
00741                     if ( type_name )
00742                         argument_error( "missing argument name before type name:", rule, frame, formal );
00743                     
00744                     if ( !formal->next )
00745                         argument_error( "missing argument name after type name:", rule, frame, formal );
00746 
00747                     type_name = formal->string;
00748                 }
00749                 else
00750                 {
00751                     LIST* value = 0;
00752                     char modifier;
00753                     LIST* arg_name = formal; /* hold the argument name for type checking */
00754                     
00755                     /* Stop now if a variable number of arguments are specified */
00756                     if ( name[0] == '*' && name[1] == 0 )
00757                         return locals;
00758 
00759                     modifier = arg_modifier( formal );
00760                 
00761                     if ( !actual && modifier != '?' && modifier != '*' )
00762                         argument_error( "missing argument", rule, frame, formal );
00763 
00764                     switch ( modifier )
00765                     {
00766                     case '+':
00767                     case '*':
00768                         value = list_copy( 0, actual );
00769                         actual = 0;
00770                         /* skip an extra element for the modifier */
00771                         formal = formal->next; 
00772                         break;
00773                     case '?':
00774                         /* skip an extra element for the modifier */
00775                         formal = formal->next; 
00776                         /* fall through */
00777                     default:
00778                         if ( actual ) /* in case actual is missing */
00779                         {
00780                             value = list_new( 0, actual->string );
00781                             actual = actual->next;
00782                         }
00783                     }
00784                 
00785                     locals = addsettings( locals, 0, name, value );
00786                     type_check( type_name, value, frame, rule, arg_name );
00787                     type_name = 0;
00788                 }
00789             }
00790             
00791             if ( actual )
00792             {
00793                 argument_error( "extra argument", rule, frame, actual );
00794             }
00795         }
00796     }
00797     return locals;
00798 }
00799 
00800 struct profile_info
00801 {
00802     char* name;                 /* name of rule being called */
00803     clock_t cumulative;         /* cumulative time spent in rule */
00804     clock_t net;                /* time spent in rule proper */
00805     unsigned long num_entries;  /* number of time rule was entered */
00806     unsigned long stack_count;  /* number of the times this function is present in stack */
00807 };
00808 typedef struct profile_info profile_info;
00809 
00810 struct profile_frame
00811 {
00812     profile_info* info;               /* permanent storage where data accumulates */
00813     clock_t overhead;                 /* overhead for profiling in this call */
00814     clock_t entry_time;               /* time of last entry to rule */
00815     struct profile_frame* caller;     /* stack frame of caller */
00816     clock_t subrules;                 /* time spent in subrules */
00817 };
00818 typedef struct profile_frame profile_frame;
00819 
00820 static profile_frame* profile_stack = 0;
00821 static struct hash* profile_hash = 0;
00822 
00823 static void profile_enter( char* rulename, profile_frame* frame )
00824 {
00825     clock_t start = clock();
00826     profile_info info, *p = &info;
00827     
00828     if ( !profile_hash )
00829         profile_hash = hashinit(sizeof(profile_info), "profile");
00830 
00831     info.name = rulename;
00832     
00833     if ( hashenter( profile_hash, (HASHDATA **)&p ) )
00834         p->cumulative = p->net = p->num_entries = p->stack_count = 0;
00835 
00836     ++(p->num_entries);
00837     ++(p->stack_count);
00838     
00839     frame->info = p;
00840     
00841     frame->caller = profile_stack;
00842     profile_stack = frame;
00843 
00844     frame->entry_time = clock();
00845     frame->overhead = 0;
00846     frame->subrules = 0;
00847 
00848     /* caller pays for the time it takes to play with the hash table */
00849     if ( frame->caller )
00850         frame->caller->overhead += frame->entry_time - start;
00851 }
00852     
00853 static void profile_exit(profile_frame* frame)
00854 {
00855     /* cumulative time for this call */
00856     clock_t t = clock() - frame->entry_time - frame->overhead;
00857     /* If this rule is already present on the stack, don't add the time for
00858        this instance. */
00859     if (frame->info->stack_count == 1)
00860         frame->info->cumulative += t;
00861     /* Net time does not depend on presense of the same rule in call stack. */
00862     frame->info->net += t - frame->subrules;
00863         
00864     if (frame->caller)
00865     {
00866         /* caller's cumulative time must account for this overhead */
00867         frame->caller->overhead += frame->overhead;
00868         frame->caller->subrules += t;
00869     }
00870     /* pop this stack frame */
00871     --frame->info->stack_count;
00872     profile_stack = frame->caller;
00873 }
00874 
00875 static void dump_profile_entry(void* p_, void* ignored)
00876 {
00877     profile_info* p = (profile_info*)p_;
00878     printf("%10d %10d %10d %s\n", p->cumulative, p->net, p->num_entries, p->name);
00879 }
00880 
00881 void profile_dump()
00882 {
00883     if ( profile_hash )
00884     {
00885         printf("%10s %10s %10s %s\n", "gross", "net", "# entries", "name");
00886         hashenumerate( profile_hash, dump_profile_entry, 0 );
00887     }
00888 }
00889 
00890 /*
00891  * evaluate_rule() - execute a rule invocation
00892  */
00893 
00894 LIST *
00895 evaluate_rule(
00896     char    *rulename,
00897     FRAME *frame )
00898 {
00899     LIST      *result = L0;
00900     RULE          *rule;
00901     profile_frame prof[1];
00902     module_t    *prev_module = frame->module;
00903     
00904     LIST      *l;
00905     {
00906         LOL arg_context_, *arg_context = &arg_context_;
00907         if ( !frame->prev )
00908             lol_init(arg_context);
00909         else
00910             arg_context = frame->prev->args;
00911         
00912         l = var_expand( L0, rulename, rulename+strlen(rulename), arg_context, 0 );
00913     }
00914 
00915     if ( !l )
00916     {
00917         backtrace_line( frame->prev );
00918         printf( "warning: rulename %s expands to empty string\n", rulename );
00919         backtrace( frame->prev );
00920         return result;
00921     }
00922 
00923     rulename = l->string;
00924     rule = bindrule( l->string, frame->module );
00925 
00926     /* drop the rule name */
00927     l = list_pop_front( l );
00928 
00929     /* tack the rest of the expansion onto the front of the first argument */
00930     frame->args->list[0] = list_append( l, lol_get( frame->args, 0 ) );
00931 
00932     if ( DEBUG_COMPILE )
00933     {
00934         /* Try hard to indicate in which module the rule is going to execute */
00935         if ( rule->module != frame->module
00936              && rule->procedure != 0 && strcmp(rulename, rule->procedure->rulename) )
00937         {
00938             char buf[256] = "";
00939             strncat( buf, rule->module->name, sizeof(buf) - 1 );
00940             strncat( buf, rule->name, sizeof(buf) - 1 );
00941             debug_compile( 1, buf, frame);
00942         }
00943         else
00944         {
00945             debug_compile( 1, rulename, frame);
00946         }
00947 
00948         lol_print( frame->args );
00949         printf( "\n" );
00950     }
00951     
00952     if ( rule->procedure && rule->module != prev_module )
00953     {
00954         /* propagate current module to nested rule invocations */
00955         frame->module = rule->module;
00956         
00957         /* swap variables */
00958         exit_module( prev_module );
00959         enter_module( rule->module );
00960     }
00961         
00962     /* record current rule name in frame */
00963     if ( rule->procedure )
00964     {
00965         frame->rulename = rulename;
00966         /* and enter record profile info */
00967         if ( DEBUG_PROFILE )
00968             profile_enter( rule->procedure->rulename, prof );
00969     }
00970 
00971     /* Check traditional targets $(<) and sources $(>) */
00972 
00973     if( !rule->actions && !rule->procedure )
00974     {
00975         backtrace_line( frame->prev );
00976         printf( "rule %s unknown in module %s\n", rule->name, frame->module->name );
00977         backtrace( frame->prev );
00978         exit(1);
00979     }
00980 
00981     /* If this rule will be executed for updating the targets */
00982     /* then construct the action for make(). */
00983 
00984     if( rule->actions )
00985     {
00986         TARGETS *t;
00987         ACTION  *action;
00988 
00989         /* The action is associated with this instance of this rule */
00990 
00991         action = (ACTION *)malloc( sizeof( ACTION ) );
00992         memset( (char *)action, '\0', sizeof( *action ) );
00993 
00994         action->rule = rule;
00995         action->targets = targetlist( (TARGETS *)0, lol_get( frame->args, 0 ) );
00996         action->sources = targetlist( (TARGETS *)0, lol_get( frame->args, 1 ) );
00997 
00998         /* Append this action to the actions of each target */
00999 
01000         for( t = action->targets; t; t = t->next )
01001             t->target->actions = actionlist( t->target->actions, action );
01002     }
01003 
01004     /* Now recursively compile any parse tree associated with this rule */
01005     /* refer/free to ensure rule not freed during use */
01006 
01007     if( rule->procedure )
01008     {
01009         SETTINGS *local_args = collect_arguments( rule, frame );
01010         PARSE *parse = rule->procedure;
01011         parse_refer( parse );
01012         
01013         pushsettings( local_args );
01014         result = parse_evaluate( parse, frame );
01015         popsettings( local_args );
01016         freesettings( local_args );
01017         
01018         parse_free( parse );
01019     }
01020 
01021     if ( frame->module != prev_module )
01022     {
01023         exit_module( frame->module );
01024         enter_module( prev_module );
01025     }
01026 
01027     if ( DEBUG_PROFILE && rule->procedure )
01028         profile_exit( prof );
01029 
01030     if( DEBUG_COMPILE )
01031         debug_compile( -1, 0, frame);
01032 
01033     return result;
01034 }
01035 
01036 /*
01037  * compile_rules() - compile a chain of rules
01038  *
01039  *      parse->left     single rule
01040  *      parse->right    more compile_rules() by right-recursion
01041  */
01042 
01043 LIST *
01044 compile_rules(
01045     PARSE   *parse,
01046     FRAME *frame )
01047 {
01048     /* Ignore result from first statement; return the 2nd. */
01049         /* Optimize recursion on the right by looping. */
01050 
01051     do list_free( parse_evaluate( parse->left, frame ) );
01052     while( (parse = parse->right)->func == compile_rules );
01053 
01054     return parse_evaluate( parse, frame );
01055 }
01056 
01057 /*
01058  * compile_set() - compile the "set variable" statement
01059  *
01060  *  parse->left variable names
01061  *  parse->right    variable values 
01062  *  parse->num  ASSIGN_SET/APPEND/DEFAULT
01063  */
01064 
01065 LIST *
01066 compile_set(
01067     PARSE   *parse,
01068     FRAME *frame )
01069 {
01070     LIST    *nt = parse_evaluate( parse->left, frame );
01071     LIST    *ns = parse_evaluate( parse->right, frame );
01072     LIST    *l;
01073     int     setflag;
01074     char    *trace;
01075 
01076     switch( parse->num )
01077     {
01078     case ASSIGN_SET:    setflag = VAR_SET; trace = "="; break;
01079     case ASSIGN_APPEND: setflag = VAR_APPEND; trace = "+="; break;
01080     case ASSIGN_DEFAULT:    setflag = VAR_DEFAULT; trace = "?="; break;
01081     default:        setflag = VAR_SET; trace = ""; break;
01082     }
01083 
01084     if( DEBUG_COMPILE )
01085     {
01086         debug_compile( 0, "set", frame);
01087         list_print( nt );
01088         printf( " %s ", trace );
01089         list_print( ns );
01090         printf( "\n" );
01091     }
01092 
01093     /* Call var_set to set variable */
01094     /* var_set keeps ns, so need to copy it */
01095 
01096     for( l = nt; l; l = list_next( l ) )
01097         var_set( l->string, list_copy( L0, ns ), setflag );
01098 
01099     list_free( nt );
01100 
01101     return ns;
01102 }
01103 
01104 /*
01105  * compile_setcomp() - support for `rule` - save parse tree 
01106  *
01107  *  parse->string   rule name
01108  *  parse->left rules for rule
01109  *  parse->right optional list-of-lists describing arguments
01110  */
01111 
01112 LIST *
01113 compile_setcomp(
01114     PARSE   *parse,
01115     FRAME *frame)
01116 {
01117     argument_list* arg_list = 0;
01118     
01119     /* Create new LOL describing argument requirements if supplied */
01120     if ( parse->right )
01121     {
01122         PARSE *p;
01123         arg_list = args_new();
01124         for( p = parse->right; p; p = p->left )
01125             lol_add( arg_list->data, parse_evaluate( p->right, frame ) );
01126     }
01127     
01128     new_rule_body( frame->module, parse->string, arg_list, parse->left, !parse->num );
01129     return L0;
01130 }
01131 
01132 /*
01133  * compile_setexec() - support for `actions` - save execution string 
01134  *
01135  *  parse->string   rule name
01136  *  parse->string1  OS command string
01137  *  parse->num  flags
01138  *  parse->left `bind` variables
01139  *
01140  * Note that the parse flags (as defined in compile.h) are transfered
01141  * directly to the rule flags (as defined in rules.h).
01142  */
01143 
01144 LIST *
01145 compile_setexec(
01146     PARSE   *parse,
01147     FRAME *frame )
01148 {
01149     LIST* bindlist = parse_evaluate( parse->left, frame );
01150 
01151     new_rule_actions( frame->module, parse->string, parse->string1, bindlist, parse->num );
01152 
01153     return L0;
01154 }
01155 
01156 /*
01157  * compile_settings() - compile the "on =" (set variable on exec) statement
01158  *
01159  *  parse->left variable names
01160  *  parse->right    target name 
01161  *  parse->third    variable value 
01162  *  parse->num  ASSIGN_SET/APPEND   
01163  */
01164 
01165 LIST *
01166 compile_settings(
01167     PARSE   *parse,
01168     FRAME *frame )
01169 {
01170     LIST    *nt = parse_evaluate( parse->left, frame );
01171     LIST    *ns = parse_evaluate( parse->third, frame );
01172     LIST    *targets = parse_evaluate( parse->right, frame );
01173     LIST    *ts;
01174     int append = parse->num == ASSIGN_APPEND;
01175 
01176     if( DEBUG_COMPILE )
01177     {
01178         debug_compile( 0, "set", frame);
01179         list_print( nt );
01180         printf( " on " );
01181         list_print( targets );
01182         printf( " %s ", append ? "+=" : "=" );
01183         list_print( ns );
01184         printf( "\n" );
01185     }
01186 
01187     /* Call addsettings to save variable setting */
01188     /* addsettings keeps ns, so need to copy it */
01189     /* Pass append flag to addsettings() */
01190 
01191     for( ts = targets; ts; ts = list_next( ts ) )
01192     {
01193         TARGET  *t = bindtarget( ts->string );
01194         LIST    *l;
01195 
01196         for( l = nt; l; l = list_next( l ) )
01197         t->settings = addsettings( t->settings, append, 
01198                 l->string, list_copy( (LIST*)0, ns ) );
01199     }
01200 
01201     list_free( nt );
01202     list_free( targets );
01203 
01204     return ns;
01205 }
01206 
01207 /*
01208  * compile_switch() - compile 'switch' rule
01209  *
01210  *  parse->left switch value (only 1st used)
01211  *  parse->right    cases
01212  *
01213  *  cases->left 1st case
01214  *  cases->right    next cases
01215  *
01216  *  case->string    argument to match
01217  *  case->left  parse tree to execute
01218  */
01219 
01220 LIST *
01221 compile_switch(
01222     PARSE   *parse,
01223     FRAME *frame )
01224 {
01225     LIST    *nt = parse_evaluate( parse->left, frame );
01226     LIST    *result = 0;
01227 
01228     if( DEBUG_COMPILE )
01229     {
01230         debug_compile( 0, "switch", frame);
01231         list_print( nt );
01232         printf( "\n" );
01233     }
01234 
01235     /* Step through cases */
01236 
01237     for( parse = parse->right; parse; parse = parse->right )
01238     {
01239         if( !glob( parse->left->string, nt ? nt->string : "" ) )
01240         {
01241         /* Get & exec parse tree for this case */
01242         parse = parse->left->left;
01243         result = parse_evaluate( parse, frame );
01244         break;
01245         }
01246     }
01247 
01248     list_free( nt );
01249 
01250     return result;
01251 }
01252 
01253 /*
01254  * debug_compile() - printf with indent to show rule expansion.
01255  */
01256 
01257 static void
01258 debug_compile( int which, char *s, FRAME* frame )
01259 {
01260     static int level = 0;
01261     static char indent[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
01262 
01263     if ( which >= 0 )
01264     {
01265       int i;
01266       
01267       print_source_line( frame->procedure );
01268       
01269       i = (level+1)*2;
01270       while ( i > 35 )
01271       {
01272         printf( indent );
01273         i -= 35;
01274       }
01275 
01276       printf( "%*.*s ", i, i, indent );
01277     }
01278 
01279     if( s )
01280         printf( "%s ", s );
01281 
01282     level += which;
01283 }

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1