diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.c b/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.c index c2ec36a..1e2494e 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.c @@ -65,9 +65,16 @@ #define DOUBLEMARK 316 #define POINTSAT 317 +extern char* xmalloc(int size); +extern void Message(char *s, int l); +extern void set_up_momentum(int value,int token); +extern void automata_driver(int value); +extern char* copys(char *); + #line 5 "annotate.y" #include "macro.h" +#include #include "compatible.h" #ifdef SYS5 @@ -146,8 +153,8 @@ typedef union { char AnnExTensionNumber[255]; /* to symbole right for the annotation */ static int Recog_My_Token(); static int look_up_specialfunction(); - static unMYGETC(); - static MYGETC(); + static char unMYGETC(char c); + static char MYGETC(); static int map_assgn_op(); #ifndef YYLTYPE @@ -623,6 +630,7 @@ while (0) #define YYERRCODE 256 #ifndef YYPURE +int yylex_annotate(); #define YYLEX yylex_annotate() #endif @@ -677,7 +685,7 @@ static int yydebug; /* nonzero means print parse trace */ #ifdef __GNUC__ int yyparse_annotate(void); #endif - + #if __GNUC__ > 1 /* GNU C and GNU C++ define this. */ #define __yy_bcopy(FROM,TO,COUNT) __builtin_memcpy(TO,FROM,COUNT) #else /* not GNU C or C++ */ @@ -2258,7 +2266,7 @@ char *p; register char *p2, *p1; //extern char *malloc(); - newbuf = (char*)malloc((unsigned)(newlength+1)); + newbuf = (char*)malloc(newlength+1); #ifdef __SPF addToCollection(__LINE__, __FILE__,newbuf, 0); #endif @@ -2996,8 +3004,7 @@ look_up_annotate(st) return (PTR_HASH) pt; } -static -MYGETC() +static char MYGETC() { if (LENSTRINGTOPARSE <= PTTOSTRINGTOPARSE) @@ -3013,9 +3020,7 @@ MYGETC() return STRINGTOPARSE[ PTTOSTRINGTOPARSE-1]; } -static -unMYGETC(c) -char c; +static char unMYGETC(char c) { if (LENSTRINGTOPARSE <= PTTOSTRINGTOPARSE) return EOF; @@ -3086,10 +3091,10 @@ look_up_specialfunction(str) return TRUE; } if (strcmp(specialfunction[i],"Dummy") == 0) - return NULL; + return 0; } - return NULL; + return 0; } diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.h b/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.h index d6924fe..f257958 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.h +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.tab.h @@ -71,5 +71,4 @@ typedef union { #define DOUBLEMARK 316 #define POINTSAT 317 - extern YYSTYPE yylval; diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.y b/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.y index 7340e46..12226f1 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.y +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/annotate.y @@ -15,7 +15,13 @@ #ifdef _NEEDALLOCAH_ # include #endif - + +extern char* xmalloc(int size); +extern void Message(char *s, int l); +extern void set_up_momentum(int value,int token); +extern void automata_driver(int value); +extern char* copys(char *); + #define ON 1 #define OFF 0 #define OTHER 2 @@ -182,8 +188,8 @@ extern POINTER newNode(); char AnnExTensionNumber[255]; /* to symbole right for the annotation */ static int Recog_My_Token(); static int look_up_specialfunction(); - static unMYGETC(); - static MYGETC(); + static char unMYGETC(char c); + static char MYGETC(); static int map_assgn_op(); %} @@ -1114,7 +1120,7 @@ char *p; register char *p2, *p1; extern char *malloc(); - newbuf = malloc((unsigned)(newlength+1)); + newbuf = malloc(newlength+1)); p2 = newbuf; p1 = newbuf + newlength + 1; @@ -1844,8 +1850,7 @@ look_up(st) return (PTR_HASH) pt; } -static -MYGETC() +static char MYGETC() { if (LENSTRINGTOPARSE <= PTTOSTRINGTOPARSE) @@ -1861,9 +1866,7 @@ MYGETC() return STRINGTOPARSE[ PTTOSTRINGTOPARSE-1]; } -static -unMYGETC(c) -char c; +static char unMYGETC(char c) { if (LENSTRINGTOPARSE <= PTTOSTRINGTOPARSE) return EOF; @@ -1931,10 +1934,10 @@ look_up_specialfunction(str) return TRUE; } if (strcmp(specialfunction[i],"Dummy") == 0) - return NULL; + return 0; } - return NULL; + return 0; } diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/comments.c b/dvm/fdvm/trunk/Sage/lib/newsrc/comments.c index 7458719..5159ce6 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/comments.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/comments.c @@ -16,8 +16,15 @@ #include "vextern.h" #include "annotate.tab.h" +extern void Message(char *s, int l); + void reset_semicoln_handler(); void reset(); +int class_struct(int value); +int is_declare(int value); +int declare_symb(int value); +int block_like(int state); +int keep_original(int state); int lastdecl_id; /* o if no main_type appeared */ int left_paren ; @@ -484,8 +491,7 @@ int value ; } -class_struct(value) -register int value ; +int class_struct(int value) { switch (value) { case ENUM : @@ -496,8 +502,7 @@ register int value ; } } -declare_symb(value) -register int value ; +int declare_symb(int value) { switch (value) { case TYPENAME : @@ -523,8 +528,7 @@ void reset() /* put_line_queue(line_pos_1,line_pos_fname); */ } -block_like(state) -int state ; +int block_like(int state) { switch( state) { @@ -541,9 +545,7 @@ int state ; } } -int -is_declare(value) -int value ; +int is_declare(int value) { switch (value) { case TYPENAME: @@ -594,8 +596,7 @@ void reset_semicoln_handler() } -keep_original(state) -int state; +int keep_original(int state) { switch (state) { case ELSE_EXPECTED_STATE: diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/toolsann.c b/dvm/fdvm/trunk/Sage/lib/newsrc/toolsann.c index 23cd164..ec02171 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/toolsann.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/toolsann.c @@ -47,6 +47,7 @@ extern int PTTOSTRINGTOPARSE; extern PTR_BFND ANNOTATIONSCOPE; extern PTR_TYPE global_int_annotation; extern char AnnExTensionNumber[]; +extern int yyparse_annotate(void); /* FORWARD DECLARATION */ int Get_Scope_Of_Annotation(); @@ -100,7 +101,7 @@ static char *Defined_Value_Str[MAX_ANNOTATION]; static int Defined_Value_Value[MAX_ANNOTATION]; /* Indicate if comment is an annotation */ -Is_Annotation(str) +int Is_Annotation(str) char *str; { @@ -122,7 +123,7 @@ char *str; } } -Is_Annotation_Cont(str) +int Is_Annotation_Cont(str) char *str; { @@ -229,7 +230,7 @@ char *str; /* basically go thrue the program and parse annotation, and set if they are defined */ -initAnnotation() +int initAnnotation() { PTR_CMNT cmnt; PTR_BFND ptbif; diff --git a/dvm/fdvm/trunk/parser/hash.c b/dvm/fdvm/trunk/parser/hash.c index df64ba1..46121e7 100644 --- a/dvm/fdvm/trunk/parser/hash.c +++ b/dvm/fdvm/trunk/parser/hash.c @@ -1,284 +1,286 @@ -/*********************************************************************/ -/* pC++/Sage++ Copyright (C) 1993 */ -/* Indiana University University of Oregon University of Rennes */ -/*********************************************************************/ - -/* - * hash.c -- hash table routines - */ - -#include - -#include "compatible.h" -#ifdef SYS5 -#include -#else -#include -#endif - -#include "defs.h" -#include "symb.h" -#include "defines.h" -#include "bif.h" -#include "extern.h" - -extern int parstate; -extern PTR_BFND cur_bfnd, pred_bfnd, global_bfnd; -extern PTR_TYPE vartype, global_default, impltype[]; -extern void make_prog_header(); -PTR_TYPE install_array(); -PTR_LLND make_llnd(); -PTR_SYMB make_symb(); -char *chkalloc(); -void free(); -void errstr(); - -PTR_HASH hash_table[hashMax]; - - -/* - * init_hash -- initialize the hash table - */ -void -init_hash() -{ - register int i; - - for (i = 0; i < hashMax; i++) - hash_table[i] = HSNULL; -} - - -/* - * Hash(string) -- compute hash value of string. - */ -int -hash(string) - register char *string; -{ - register int i; - - for (i = 0; *string;) - i += *string++; - return (i % hashMax); -} - - -/* - * look_up(string) -- lookup string in the hash table and - * install it if not there - */ -PTR_HASH -look_up(string, decl_type) -register char *string; -int decl_type; -{ - int i; - register PTR_HASH entry; - PTR_BFND cur_scope(), p; - - i = hash(string); - p = cur_scope(); - for (entry = hash_table[i]; entry; entry = entry->next_entry) { - if (!strcmp(string, entry->ident) && (entry->id_attr)) { - if ((entry->id_attr->scope == p) || - ((entry->id_attr->variant==FUNCTION_NAME) && - (p->variant==FUNC_HEDR) && - (p->entry.Template.symbol==entry->id_attr))) - return (entry); - if (decl_type == SOFT) { - for (p=cur_scope(); NEW_SCOPE(p); p = p->control_parent) { - if (entry->id_attr->scope == p) - return (entry); - } - if (entry->id_attr->scope == p) - return (entry); - } - } - } - entry = (struct hash_entry *) chkalloc(sizeof(struct hash_entry)); - entry->ident = copys(string); - entry->next_entry = hash_table[i]; - hash_table[i] = entry; - return (entry); -} - - -PTR_HASH -correct_symtab(h, type) -PTR_HASH h; -int type; -{ - int i; - PTR_HASH entry; - - i = hash(h->ident); - for (entry = hash_table[i]; entry; entry = entry->next_entry) { - if (!strcmp(h->ident, entry->ident) - && ( !(entry->id_attr) - ||(entry->id_attr->variant==type)) - && (h != entry)) - break; - } - if (!entry) return h; - if (hash_table[i] != h) { - fprintf (stderr, "Bug in correct_symtab\n"); - return h; - } - hash_table[i] = hash_table[i]->next_entry; -#ifdef __SPF - removeFromCollection(h); -#endif - free((char *)h); - return hash_table[i]; -} - - -/* - * Checks whether the "name" is installed and installs as a SOFT - * entry if not. - */ -PTR_LLND -check_and_install(h, d, ndim) -PTR_HASH h; -PTR_LLND d; -int ndim; -{ - PTR_BFND cur_scope(); - PTR_SYMB install_entry(); - PTR_TYPE p = NULL; - PTR_SYMB s; - PTR_LLND r; - void set_type(), err(); - - /* Check if the variable is already declared */ - if ((s = h->id_attr) && (s->scope == cur_scope()) && s->type) { - if (d && s->type->variant != T_ARRAY) { - p = install_array(d, s->type, ndim); - s->type = p; - } - } - else { - if (h->id_attr && h->id_attr->type) - p = h->id_attr->type; - else if (!undeftype) - p = impltype[*h->ident - 'a']; - else - err("Variable type unknown",327); - if (d) - p = install_array(d, p, ndim); - s = install_entry(h, SOFT); - set_type(s, p, LOCAL); - } - if (d) { - r = p->entry.ar_decl.ranges - = make_llnd(fi,ARRAY_REF, d, LLNULL, s); - } - else - r = make_llnd(fi,VAR_REF, LLNULL, LLNULL, s); - return make_llnd(fi,EXPR_LIST, r, LLNULL, SMNULL); -} - - - -/* - * install_entry takes a pointer to a hash entry and - * makes another symbol table entry for the same id - */ -PTR_SYMB -install_entry(entry, decl_type) -PTR_HASH entry; -int decl_type; -{ - register PTR_SYMB symb_ptr; - PTR_BFND cur_scope(); - PTR_BFND p; - void err(); - - if (decl_type == HARD && entry->id_attr && - entry->id_attr->scope != cur_scope()) - entry = look_up(entry->ident, HARD); - if ((entry->id_attr) && ((entry->id_attr->scope == cur_scope()) || - ((entry->id_attr->variant==FUNCTION_NAME) && - (entry->id_attr->scope->variant==FUNC_HEDR)))) - { - if (entry->id_attr->decl == SOFT) { - entry->id_attr->decl = decl_type; - return(entry->id_attr); - } - if (decl_type == SOFT) - return(entry->id_attr); - /* else */ - errstr("Redeclaration of identifier: %s",entry->id_attr->ident,328); - /*(void)fprintf(stderr, "id: %s\n", entry->id_attr->ident);*/ - return (SMNULL); - } - symb_ptr = make_symb(fi,DEFAULT, entry->ident); - for (p=cur_scope(); - NEW_SCOPE(p) && (decl_type==SOFT); - p = p->control_parent) - ; - symb_ptr->scope = p; - symb_ptr->outer = entry->id_attr; - symb_ptr->parent = entry; - symb_ptr->decl = decl_type; - entry->id_attr = symb_ptr; - symb_ptr->id_list = SMNULL; - return (symb_ptr); -} - - - -PTR_SYMB -get_proc_symbol(entry) - PTR_HASH entry; -{ - register PTR_SYMB symb_ptr; - PTR_BFND cur_scope(); - - symb_ptr = make_symb(fi, PROCEDURE_NAME, entry->ident); - symb_ptr->scope = global_bfnd; - symb_ptr->outer = entry->id_attr; - symb_ptr->parent = entry; - entry->id_attr = symb_ptr; - return (symb_ptr); -} - -/* -PTR_BFND -cur_scope() -{ - register PTR_BFND p; -*/ - /* Takes cares of main program unit begining without a PROGRAM - statement. After rewrite of statement processing has been done, - strengthen ( weaken? ) the test. - */ -/* - if ((pred_bfnd->variant == GLOBAL) && (parstate == OUTSIDE)) - { - make_prog_header(); - return (pred_bfnd); - } - - for (p = pred_bfnd; - (p->variant != PROG_HEDR) && - (p->variant != PROC_HEDR) && - (p->variant != PROS_HEDR) && - (p->variant != FUNC_HEDR) && - (p->variant != BLOCK_DATA) && - (p->variant != FORALL_NODE) && - (p->variant != GLOBAL) && - (p->variant != CDOALL_NODE) && - (p->variant != SDOALL_NODE) && - (p->variant != DOACROSS_NODE) && - (p->variant != STRUCT_DECL); - p = p->control_parent); - ; - - return (p); -} -*/ - - - +/*********************************************************************/ +/* pC++/Sage++ Copyright (C) 1993 */ +/* Indiana University University of Oregon University of Rennes */ +/*********************************************************************/ + +/* + * hash.c -- hash table routines + */ + +#include + +#include "compatible.h" +#ifdef SYS5 +#include +#else +#include +#endif + +#include "defs.h" +#include "symb.h" +#include "defines.h" +#include "bif.h" +#include "extern.h" + +extern int parstate; +extern PTR_BFND cur_bfnd, pred_bfnd, global_bfnd; +extern PTR_TYPE vartype, global_default, impltype[]; +extern void make_prog_header(); +PTR_TYPE install_array(); +PTR_LLND make_llnd(); +PTR_SYMB make_symb(); +char *chkalloc(); +void free(); +void errstr(); + +PTR_HASH hash_table[hashMax]; + +#ifdef __SPF +extern void removeFromCollection(void *pointer); +#endif +/* + * init_hash -- initialize the hash table + */ +void +init_hash() +{ + register int i; + + for (i = 0; i < hashMax; i++) + hash_table[i] = HSNULL; +} + + +/* + * Hash(string) -- compute hash value of string. + */ +int +hash(string) + register char *string; +{ + register int i; + + for (i = 0; *string;) + i += *string++; + return (i % hashMax); +} + + +/* + * look_up(string) -- lookup string in the hash table and + * install it if not there + */ +PTR_HASH +look_up(string, decl_type) +register char *string; +int decl_type; +{ + int i; + register PTR_HASH entry; + PTR_BFND cur_scope(), p; + + i = hash(string); + p = cur_scope(); + for (entry = hash_table[i]; entry; entry = entry->next_entry) { + if (!strcmp(string, entry->ident) && (entry->id_attr)) { + if ((entry->id_attr->scope == p) || + ((entry->id_attr->variant==FUNCTION_NAME) && + (p->variant==FUNC_HEDR) && + (p->entry.Template.symbol==entry->id_attr))) + return (entry); + if (decl_type == SOFT) { + for (p=cur_scope(); NEW_SCOPE(p); p = p->control_parent) { + if (entry->id_attr->scope == p) + return (entry); + } + if (entry->id_attr->scope == p) + return (entry); + } + } + } + entry = (struct hash_entry *) chkalloc(sizeof(struct hash_entry)); + entry->ident = copys(string); + entry->next_entry = hash_table[i]; + hash_table[i] = entry; + return (entry); +} + + +PTR_HASH +correct_symtab(h, type) +PTR_HASH h; +int type; +{ + int i; + PTR_HASH entry; + + i = hash(h->ident); + for (entry = hash_table[i]; entry; entry = entry->next_entry) { + if (!strcmp(h->ident, entry->ident) + && ( !(entry->id_attr) + ||(entry->id_attr->variant==type)) + && (h != entry)) + break; + } + if (!entry) return h; + if (hash_table[i] != h) { + fprintf (stderr, "Bug in correct_symtab\n"); + return h; + } + hash_table[i] = hash_table[i]->next_entry; +#ifdef __SPF + removeFromCollection(h); +#endif + free((char *)h); + return hash_table[i]; +} + + +/* + * Checks whether the "name" is installed and installs as a SOFT + * entry if not. + */ +PTR_LLND +check_and_install(h, d, ndim) +PTR_HASH h; +PTR_LLND d; +int ndim; +{ + PTR_BFND cur_scope(); + PTR_SYMB install_entry(); + PTR_TYPE p = NULL; + PTR_SYMB s; + PTR_LLND r; + void set_type(), err(); + + /* Check if the variable is already declared */ + if ((s = h->id_attr) && (s->scope == cur_scope()) && s->type) { + if (d && s->type->variant != T_ARRAY) { + p = install_array(d, s->type, ndim); + s->type = p; + } + } + else { + if (h->id_attr && h->id_attr->type) + p = h->id_attr->type; + else if (!undeftype) + p = impltype[*h->ident - 'a']; + else + err("Variable type unknown",327); + if (d) + p = install_array(d, p, ndim); + s = install_entry(h, SOFT); + set_type(s, p, LOCAL); + } + if (d) { + r = p->entry.ar_decl.ranges + = make_llnd(fi,ARRAY_REF, d, LLNULL, s); + } + else + r = make_llnd(fi,VAR_REF, LLNULL, LLNULL, s); + return make_llnd(fi,EXPR_LIST, r, LLNULL, SMNULL); +} + + + +/* + * install_entry takes a pointer to a hash entry and + * makes another symbol table entry for the same id + */ +PTR_SYMB +install_entry(entry, decl_type) +PTR_HASH entry; +int decl_type; +{ + register PTR_SYMB symb_ptr; + PTR_BFND cur_scope(); + PTR_BFND p; + void err(); + + if (decl_type == HARD && entry->id_attr && + entry->id_attr->scope != cur_scope()) + entry = look_up(entry->ident, HARD); + if ((entry->id_attr) && ((entry->id_attr->scope == cur_scope()) || + ((entry->id_attr->variant==FUNCTION_NAME) && + (entry->id_attr->scope->variant==FUNC_HEDR)))) + { + if (entry->id_attr->decl == SOFT) { + entry->id_attr->decl = decl_type; + return(entry->id_attr); + } + if (decl_type == SOFT) + return(entry->id_attr); + /* else */ + errstr("Redeclaration of identifier: %s",entry->id_attr->ident,328); + /*(void)fprintf(stderr, "id: %s\n", entry->id_attr->ident);*/ + return (SMNULL); + } + symb_ptr = make_symb(fi,DEFAULT, entry->ident); + for (p=cur_scope(); + NEW_SCOPE(p) && (decl_type==SOFT); + p = p->control_parent) + ; + symb_ptr->scope = p; + symb_ptr->outer = entry->id_attr; + symb_ptr->parent = entry; + symb_ptr->decl = decl_type; + entry->id_attr = symb_ptr; + symb_ptr->id_list = SMNULL; + return (symb_ptr); +} + + + +PTR_SYMB +get_proc_symbol(entry) + PTR_HASH entry; +{ + register PTR_SYMB symb_ptr; + PTR_BFND cur_scope(); + + symb_ptr = make_symb(fi, PROCEDURE_NAME, entry->ident); + symb_ptr->scope = global_bfnd; + symb_ptr->outer = entry->id_attr; + symb_ptr->parent = entry; + entry->id_attr = symb_ptr; + return (symb_ptr); +} + +/* +PTR_BFND +cur_scope() +{ + register PTR_BFND p; +*/ + /* Takes cares of main program unit begining without a PROGRAM + statement. After rewrite of statement processing has been done, + strengthen ( weaken? ) the test. + */ +/* + if ((pred_bfnd->variant == GLOBAL) && (parstate == OUTSIDE)) + { + make_prog_header(); + return (pred_bfnd); + } + + for (p = pred_bfnd; + (p->variant != PROG_HEDR) && + (p->variant != PROC_HEDR) && + (p->variant != PROS_HEDR) && + (p->variant != FUNC_HEDR) && + (p->variant != BLOCK_DATA) && + (p->variant != FORALL_NODE) && + (p->variant != GLOBAL) && + (p->variant != CDOALL_NODE) && + (p->variant != SDOALL_NODE) && + (p->variant != DOACROSS_NODE) && + (p->variant != STRUCT_DECL); + p = p->control_parent); + ; + + return (p); +} +*/ + + + diff --git a/dvm/fdvm/trunk/parser/low_hpf.c b/dvm/fdvm/trunk/parser/low_hpf.c index d7a6996..e74423d 100644 --- a/dvm/fdvm/trunk/parser/low_hpf.c +++ b/dvm/fdvm/trunk/parser/low_hpf.c @@ -1,1077 +1,1006 @@ - -#include - -#include -#include /* ANSI variable argument header */ - -#include "compatible.h" /* Make different system compatible... (PHB) */ -#ifdef SYS5 -#include -#else -#include -#endif - -#include "vpc.h" -#include "macro.h" -#include "ext_lib.h" -#define HPF_STRING 2 - -/*int debug =NO; used in db.c*/ - -/*static int number_of_bif_node = 0;*/ -/*int number_of_ll_node = 0; this counters are useless anymore */ -/*static int number_of_symb_node = 0; -static int number_of_type_node = 0;*/ - -/* FORWARD DECLARATIONS (phb) */ -void Message(); -char * filter(); -int *evaluateExpression(); - -extern int write_nodes(); -extern char* Tool_Unparse2_LLnode(); -extern void Init_HPFUnparser(); -extern char* Tool_Unparse_Bif (); -extern char* Tool_Unparse_Type(); - -#define MAXFIELDSYMB 10 -#define MAXFIELDTYPE 10 - -#ifdef __SPF_BUILT_IN_PARSER -static int Warning_count = 0; -//static PTR_FILE pointer_on_file_proj; -//static char* default_filename; - -/* records propoerties and type of node */ -//static char node_code_type[LAST_CODE]; -/* Number of argument-words in each kind of tree-node. */ -//static int node_code_length[LAST_CODE]; -//static enum typenode node_code_kind[LAST_CODE]; - -/* special table for infos on type and symbol */ -static char info_type[LAST_CODE][MAXFIELDTYPE]; -static char info_symb[LAST_CODE][MAXFIELDSYMB]; -static char general_info[LAST_CODE][MAXFIELDSYMB]; -#else -PTR_FILE pointer_on_file_proj; -char* default_filename; -int Warning_count = 0; - -/* records propoerties and type of node */ -char node_code_type[LAST_CODE]; -/* Number of argument-words in each kind of tree-node. */ -int node_code_length[LAST_CODE]; -enum typenode node_code_kind[LAST_CODE]; - -/* special table for infos on type and symbol */ -char info_type[LAST_CODE][MAXFIELDTYPE]; -char info_symb[LAST_CODE][MAXFIELDSYMB]; -char general_info[LAST_CODE][MAXFIELDSYMB]; -#endif - -/*static struct bif_stack_level *stack_level = NULL; -static struct bif_stack_level *current_level = NULL;*/ - -PTR_BFND getFunctionHeader(); - -/***************************************************************************** - * * - * Procedure of general use * - * * - *****************************************************************************/ - -/* Modified to return a pointer (64bit clean) (phb) */ -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -char* xmalloc_temp(size) -#else -char* xmalloc(size) -#endif - int size; -{ - char *val; - val = (char *) malloc (size); - - if (val == 0) - Message("Virtual memory exhausted (malloc failed)",0); - return val; -} - -/* list of allocated data */ -static ptstack_chaining Current_Allocated_Data = NULL; -static ptstack_chaining First_STACK= NULL; - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -void make_a_malloc_stack_temp() -#else -void make_a_malloc_stack() -#endif -{ - ptstack_chaining pt; - - pt = (ptstack_chaining) malloc(sizeof(struct stack_chaining)); - if (!pt) - { - Message("sorry : out of memory\n",0); - exit(1); - } - - if (Current_Allocated_Data) - Current_Allocated_Data->next = pt; - pt->first = NULL; - pt->last = NULL; - pt->prev = Current_Allocated_Data; - if (Current_Allocated_Data) - pt->level = Current_Allocated_Data->level +1; - else - pt->level = 0; -/* printf("make_a_malloc_stack %d \n",pt->level);*/ - Current_Allocated_Data = pt; - if (First_STACK == NULL) - First_STACK = pt; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -char* mymalloc_temp(size) -#else -char* mymalloc(size) -#endif -int size; -{ - char *pt1; - ptchaining pt2; - if (!Current_Allocated_Data) - { - Message("Allocated Stack not defined\n",0); - exit(1); - } - -/* if (Current_Allocated_Data->level > 0) - printf("mymalloc %d \n", Current_Allocated_Data->level); */ - pt1 = (char *) malloc(size); - if (!pt1) - { - Message("sorry : out of memory\n",0); - exit(1); - } - - pt2 = (ptchaining) malloc(sizeof(struct chaining)); - if (!pt2 ) - { - Message("sorry : out of memory\n",0); - exit(1); - } - - pt2->zone = pt1; - pt2->list = NULL; - - if (Current_Allocated_Data->first == NULL) - Current_Allocated_Data->first = pt2; - - if (Current_Allocated_Data->last == NULL) - Current_Allocated_Data->last = pt2; - else - { - Current_Allocated_Data->last->list = pt2; - Current_Allocated_Data->last = pt2; - } - return pt1; -} - -/***************** Provides infos on nodes ******************************** - * * - * based on the table info in include dir *.def * - * * - **************************************************************************/ - -#ifndef __SPF_BUILT_IN_PARSER -/***************************************************************************/ -int isATypeNode(variant) -int variant; -{ - return (TYPENODE == (int) node_code_kind[variant]); -} - -/***************************************************************************/ -int isASymbNode(variant) -int variant; -{ - return (SYMBNODE == (int) node_code_kind[variant]); -} - -/***************************************************************************/ -int isABifNode(variant) -int variant; -{ - return (BIFNODE == (int) node_code_kind[variant]); -} - -/***************************************************************************/ -int isALoNode(variant) -int variant; -{ - return (LLNODE == (int) node_code_kind[variant]); -} -#endif - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int hasTypeBaseType_temp(variant) -#else -int hasTypeBaseType(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("hasTypeBaseType not applied to a type node",0); - return 0; - } - if (info_type[variant][2] == 'b') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isStructType_temp(variant) -#else -int isStructType(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("isStructType not applied to a type node",0); - return 0; - } - if (info_type[variant][0] == 's') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isPointerType_temp(variant) -#else -int isPointerType(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("isPointerType not applied to a type node",0); - return 0; - } - if (info_type[variant][0] == 'p') - return TRUE; - else - return FALSE; -} - - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isUnionType_temp(variant) -#else -int isUnionType(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("isUnionType not applied to a type node",0); - return 0; - } - if (info_type[variant][0] == 'u') - return TRUE; - else - return FALSE; -} - - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isEnumType_temp(variant) -#else -int isEnumType(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("EnumType not applied to a type node",0); - return 0; - } - if (info_type[variant][0] == 'e') - return TRUE; - else - return FALSE; -} - - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int hasTypeSymbol_temp(variant) -#else -int hasTypeSymbol(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("hasTypeSymbol not applied to a type node",0); - return 0; - } - if (info_type[variant][1] == 's') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isAtomicType_temp(variant) -#else -int isAtomicType(variant) -#endif -int variant; -{ - if (!isATypeNode(variant)) - { - Message("isAtomicType not applied to a type node",0); - return 0; - } - if (info_type[variant][0] == 'a') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int hasNodeASymb_temp(variant) -#else -int hasNodeASymb(variant) -#endif -int variant; -{ - if ((!isABifNode(variant)) && (!isALoNode(variant))) - { - Message("hasNodeASymb not applied to a bif or low level node",0); - return 0; - } - if (general_info[variant][2] == 's') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isNodeAConst_temp(variant) -#else -int isNodeAConst(variant) -#endif -int variant; -{ - if ((!isABifNode(variant)) && (!isALoNode(variant))) - { - Message("isNodeAConst not applied to a bif or low level node",0); - return 0; - } - if (general_info[variant][1] == 'c') - return TRUE; - else - return FALSE; -} - - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isAStructDeclBif_temp(variant) -#else -int isAStructDeclBif(variant) -#endif -int variant; -{ - if (!isABifNode(variant)) - { - Message("isAStructDeclBif not applied to a bif",0); - return 0; - } - if (general_info[variant][1] == 's') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isAUnionDeclBif_temp(variant) -#else -int isAUnionDeclBif(variant) -#endif -int variant; -{ - if (!isABifNode(variant)) - { - Message("isAUnionDeclBif not applied to a bif",0); - return 0; - } - if (general_info[variant][1] == 'u') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isAEnumDeclBif_temp(variant) -#else -int isAEnumDeclBif(variant) -#endif -int variant; -{ - if (!isABifNode(variant)) - { - Message("isAEnumDeclBif not applied to a bif",0); - return 0; - } - if (general_info[variant][1] == 'e') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isADeclBif_temp(variant) -#else -int isADeclBif(variant) -#endif -int variant; -{ - if (!isABifNode(variant)) - { - Message("isADeclBif not applied to a bif",0); - return 0; - } - if (general_info[variant][0] == 'd') - return TRUE; - else - return FALSE; -} - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int isAControlEnd_temp(variant) -#else -int isAControlEnd(variant) -#endif -int variant; -{ - if (!isABifNode(variant)) - { - Message("isAControlEnd not applied to a bif",0); - return 0; - } - if (general_info[variant][0] == 'c') - return TRUE; - else - return FALSE; -} - - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -void Message_temp(s, l) -#else -void Message(s, l) -#endif -char *s; -int l; -{ - if (l != 0) - fprintf(stderr,"Warning : %s line %d\n",s, l); - else - fprintf(stderr,"Warning : %s\n",s); - Warning_count++; -} -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -int Check_Lang_Fortran_temp(proj) -#else -int Check_Lang_Fortran(proj) -#endif -PTR_PROJ proj; -{ - PTR_FILE ptf; - PTR_BLOB ptb; - /* Change FALSE to TRUE */ - if (!proj) - return TRUE; - for (ptb = PROJ_FILE_CHAIN (proj); ptb ; ptb = BLOB_NEXT (ptb)) - { - ptf = (PTR_FILE) BLOB_VALUE (ptb); - /* if (debug) - fprintf(stderr,"%s\n",FILE_FILENAME (ptf)); */ - - if (FILE_LANGUAGE(ptf) != ForSrc) - return(FALSE); - } - return(TRUE); -} - - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -char* filter_temp(s) -#else -char* filter(s) -#endif -char *s; -{ - char c; - int i = 1; - char temp[1024]; - int temp_i = 0; - int buf_i = 0; - int commentline = 0; - char *resul, *init; - - if (!s) return NULL; - if (strlen(s)==0) return s; - - /* allocate very simple, but to redo later, allocate two times the size */ - make_a_malloc_stack(); - resul = (char *) mymalloc((int)(2*strlen(s))); - memset(resul, 0, 2*strlen(s)); - init = resul; - /* find the separator */ - c = s[0]; - - if ((c != ' ') - &&(c != '\n') - && (c != '0') - && (c != '1') - && (c != '2') - && (c != '3') - && (c != '4') - && (c != '5') - && (c != '6') - && (c != '7') - && (c != '8') - && (c != '9')) - if ( c=='!') commentline = HPF_STRING; - else commentline = 1; - else - commentline = 0; - - temp_i = 0; - i = 0; - buf_i =0; - while (c!='\0') - { - c = s[i]; - temp[ buf_i] = c; - if (c == '\n') - { - temp[ buf_i+1] = '\0'; - sprintf(resul,"%s",temp); - resul = resul + strlen(temp); - temp_i = -1; - buf_i = -1; - if ((s[i+1] != ' ') - &&(s[i+1] != '\n') - && (s[i+1] != '0') - && (s[i+1] != '1') - && (s[i+1] != '2') - && (s[i+1] != '3') - && (s[i+1] != '4') - && (s[i+1] != '5') - && (s[i+1] != '6') - && (s[i+1] != '7') - && (s[i+1] != '8') - && (s[i+1] != '9')) - if (s[i+1] == '!') commentline = HPF_STRING; - else commentline = 1; - else - commentline = 0; - } else - { - if ((temp_i == 71) && !commentline) - { - /* insert where necessary */ - temp[ buf_i+1] = '\0'; - sprintf(resul,"%s\n",temp); - resul = resul + strlen(temp)+1; - sprintf(resul," +"); - resul = resul + strlen(" +"); - commentline = 0; - memset(temp, 0, 1024); - temp_i = strlen(" +")-1; - buf_i = -1; - } - if ((temp_i == 71) && (commentline==HPF_STRING)) - { - /* insert where necessary */ - int count=0; - for(;s[i]!='$';i--,count++) - { - if (strncmp(&(s[i]),"ONTO", strlen("ONTO"))== 0) - break; - if (strncmp(&(s[i]),"BEGIN", strlen("BEGIN"))== 0) - break; - if (strncmp(&(s[i]),"ON", strlen("ON"))== 0) - { - i+=3;count-=3; - break; - } - if (strncmp(&(s[i]),"WITH", strlen("WITH"))== 0) - break; - if (strncmp(&(s[i]),"NEW", strlen("NEW"))== 0) - break; - if (strncmp(&(s[i]),"REDUCTION", strlen("REDUCTION"))== 0) - break; - if (strncmp(&(s[i]),"TEMPLATE", strlen("TEMPLATE"))== 0) - break; - if (strncmp(&(s[i]),"SHADOW", strlen("SHADOW"))== 0) - break; - if (strncmp(&(s[i]),"INHERIT", strlen("INHERIT"))== 0) - break; - if (strncmp(&(s[i]),"DYNAMIC", strlen("DYNAMIC"))== 0) - break; - if (strncmp(&(s[i]),"DIMENSION", strlen("DIMENSION"))== 0) - break; - if (strncmp(&(s[i]),"PROCESSORS", strlen("PROCESSORS"))== 0) - break; - if (strncmp(&(s[i]),"DISTRIBUTE", strlen("DISTRIBUTE"))== 0) - break; - if (strncmp(&(s[i]),"ALIGN", strlen("ALIGN"))== 0) - break; - if (strncmp(&(s[i]),"::", strlen("::"))== 0) - { - /*i+=3;count-=3;*/ - break; - } - } - i--;count++; - if (count<36) - temp[ buf_i+1-count] = '\0'; - else - { - i+=count; - temp[ buf_i+1] = '\0'; - } - sprintf(resul,"%s\n",temp); - resul = resul + strlen(temp)+1; - sprintf(resul,"!HPF$*"); - resul = resul + strlen("!HPF$*"); - /*8commentline = 0;*/ - memset(temp, 0, 1024); - temp_i = strlen("!HPF$*")-1; - buf_i = -1; - } - } - i++; - temp_i++; - buf_i++; - } - - return init; -} -/* preset some values of symbols for evaluateExpression*/ -#define ALLOCATECHUNKVALUE 100 -static PTR_SYMB *ValuesSymb = NULL; -static int *ValuesInt = NULL; -static int NbValues = 0; -static int NbElement = 0; - -/***************************************************************************/ -#ifdef __SPF_BUILT_IN_PARSER -void allocateValueEvaluate_temp() -#else -void allocateValueEvaluate() -#endif -{ - int i; - PTR_SYMB *pt1; - int *pt2; - - pt1 = (PTR_SYMB *) xmalloc((int)( sizeof(PTR_SYMB *) * - (NbValues + ALLOCATECHUNKVALUE))); - pt2 = (int *) xmalloc((int)( sizeof(int *) * (NbValues + ALLOCATECHUNKVALUE))); - - for (i=0; i + +#include +#include /* ANSI variable argument header */ + +#include "compatible.h" /* Make different system compatible... (PHB) */ +#ifdef SYS5 +#include +#else +#include +#endif + +#include "vpc.h" +#include "macro.h" +#include "ext_lib.h" +#define HPF_STRING 2 + +/*int debug =NO; used in db.c*/ + +/*static int number_of_bif_node = 0;*/ +/*int number_of_ll_node = 0; this counters are useless anymore */ +/*static int number_of_symb_node = 0; +static int number_of_type_node = 0;*/ + +/* FORWARD DECLARATIONS (phb) */ +void Message(); +char * filter(); +int *evaluateExpression(); + +extern int write_nodes(); +extern char* Tool_Unparse2_LLnode(); +extern void Init_HPFUnparser(); +extern char* Tool_Unparse_Bif (); +extern char* Tool_Unparse_Type(); + +#define MAXFIELDSYMB 10 +#define MAXFIELDTYPE 10 + +#ifdef __SPF +extern void removeFromCollection(void *pointer); +#endif + +#ifdef __SPF_BUILT_IN_PARSER +static int Warning_count = 0; +//static PTR_FILE pointer_on_file_proj; +//static char* default_filename; + +/* records propoerties and type of node */ +//static char node_code_type[LAST_CODE]; +/* Number of argument-words in each kind of tree-node. */ +//static int node_code_length[LAST_CODE]; +//static enum typenode node_code_kind[LAST_CODE]; + +/* special table for infos on type and symbol */ +static char info_type[LAST_CODE][MAXFIELDTYPE]; +static char info_symb[LAST_CODE][MAXFIELDSYMB]; +static char general_info[LAST_CODE][MAXFIELDSYMB]; +#else +PTR_FILE pointer_on_file_proj; +char* default_filename; +int Warning_count = 0; + +/* records propoerties and type of node */ +char node_code_type[LAST_CODE]; +/* Number of argument-words in each kind of tree-node. */ +int node_code_length[LAST_CODE]; +enum typenode node_code_kind[LAST_CODE]; + +/* special table for infos on type and symbol */ +char info_type[LAST_CODE][MAXFIELDTYPE]; +char info_symb[LAST_CODE][MAXFIELDSYMB]; +char general_info[LAST_CODE][MAXFIELDSYMB]; +#endif + +/*static struct bif_stack_level *stack_level = NULL; +static struct bif_stack_level *current_level = NULL;*/ + +PTR_BFND getFunctionHeader(); +#ifdef __SPF_BUILT_IN_PARSER + +#define xmalloc xmalloc_temp +#define make_a_malloc_stack make_a_malloc_stack_temp +#define mymalloc mymalloc_temp +#define isATypeNode isATypeNode_temp +#define isASymbNode isASymbNode_temp +#define isABifNode isABifNode_temp +#define isALoNode isALoNode_temp +#define hasTypeBaseType hasTypeBaseType_temp +#define isStructType isStructType_temp +#define isPointerType isPointerType_temp +#define isUnionType isUnionType_temp +#define isEnumType isEnumType_temp +#define hasTypeSymbol hasTypeSymbol_temp +#define isAtomicType isAtomicType_temp +#define hasNodeASymb hasNodeASymb_temp +#define isNodeAConst isNodeAConst_temp +#define isAStructDeclBif isAStructDeclBif_temp +#define isAUnionDeclBif isAUnionDeclBif_temp +#define isAEnumDeclBif isAEnumDeclBif_temp +#define isADeclBif isADeclBif_temp +#define isAControlEnd isAControlEnd_temp +#define Message Message_temp +#define Check_Lang_Fortran Check_Lang_Fortran_temp +#define filter filter_temp +#define allocateValueEvaluate allocateValueEvaluate_temp +#define addElementEvaluate addElementEvaluate_temp +#define getElementEvaluate getElementEvaluate_temp +#define resetPresetEvaluate resetPresetEvaluate_temp +#define evaluateExpression evaluateExpression_temp +#define patternMatchExpression patternMatchExpression_temp +#define Follow_Llnd Follow_Llnd_temp + +#endif + +/***************************************************************************** + * * + * Procedure of general use * + * * + *****************************************************************************/ + +/***************************************************************************/ +void Message(s, l) +char *s; +int l; +{ + if (l != 0) + fprintf(stderr,"Warning : %s line %d\n",s, l); + else + fprintf(stderr,"Warning : %s\n",s); + Warning_count++; +} + +/* Modified to return a pointer (64bit clean) (phb) */ +/***************************************************************************/ +char* xmalloc(size) + int size; +{ + char *val; + val = (char *) malloc (size); + + if (val == 0) + Message("Virtual memory exhausted (malloc failed)",0); + return val; +} + +/* list of allocated data */ +static ptstack_chaining Current_Allocated_Data = NULL; +static ptstack_chaining First_STACK= NULL; + +/***************************************************************************/ +void make_a_malloc_stack() +{ + ptstack_chaining pt; + + pt = (ptstack_chaining) malloc(sizeof(struct stack_chaining)); + if (!pt) + { + Message("sorry : out of memory\n",0); + exit(1); + } + + if (Current_Allocated_Data) + Current_Allocated_Data->next = pt; + pt->first = NULL; + pt->last = NULL; + pt->prev = Current_Allocated_Data; + if (Current_Allocated_Data) + pt->level = Current_Allocated_Data->level +1; + else + pt->level = 0; +/* printf("make_a_malloc_stack %d \n",pt->level);*/ + Current_Allocated_Data = pt; + if (First_STACK == NULL) + First_STACK = pt; +} + +/***************************************************************************/ +char* mymalloc(size) +int size; +{ + char *pt1; + ptchaining pt2; + if (!Current_Allocated_Data) + { + Message("Allocated Stack not defined\n",0); + exit(1); + } + +/* if (Current_Allocated_Data->level > 0) + printf("mymalloc %d \n", Current_Allocated_Data->level); */ + pt1 = (char *) malloc(size); + if (!pt1) + { + Message("sorry : out of memory\n",0); + exit(1); + } + + pt2 = (ptchaining) malloc(sizeof(struct chaining)); + if (!pt2 ) + { + Message("sorry : out of memory\n",0); + exit(1); + } + + pt2->zone = pt1; + pt2->list = NULL; + + if (Current_Allocated_Data->first == NULL) + Current_Allocated_Data->first = pt2; + + if (Current_Allocated_Data->last == NULL) + Current_Allocated_Data->last = pt2; + else + { + Current_Allocated_Data->last->list = pt2; + Current_Allocated_Data->last = pt2; + } + return pt1; +} + +/***************** Provides infos on nodes ******************************** + * * + * based on the table info in include dir *.def * + * * + **************************************************************************/ + +/***************************************************************************/ +int isATypeNode(variant) +int variant; +{ + return (TYPENODE == (int) node_code_kind[variant]); +} + +/***************************************************************************/ +int isASymbNode(variant) +int variant; +{ + return (SYMBNODE == (int) node_code_kind[variant]); +} + +/***************************************************************************/ +int isABifNode(variant) +int variant; +{ + return (BIFNODE == (int) node_code_kind[variant]); +} + +/***************************************************************************/ +int isALoNode(variant) +int variant; +{ + return (LLNODE == (int) node_code_kind[variant]); +} + +/***************************************************************************/ +int hasTypeBaseType(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("hasTypeBaseType not applied to a type node",0); + return 0; + } + if (info_type[variant][2] == 'b') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isStructType(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("isStructType not applied to a type node",0); + return 0; + } + if (info_type[variant][0] == 's') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isPointerType(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("isPointerType not applied to a type node",0); + return 0; + } + if (info_type[variant][0] == 'p') + return TRUE; + else + return FALSE; +} + + +/***************************************************************************/ +int isUnionType(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("isUnionType not applied to a type node",0); + return 0; + } + if (info_type[variant][0] == 'u') + return TRUE; + else + return FALSE; +} + + +/***************************************************************************/ +int isEnumType(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("EnumType not applied to a type node",0); + return 0; + } + if (info_type[variant][0] == 'e') + return TRUE; + else + return FALSE; +} + + +/***************************************************************************/ +int hasTypeSymbol(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("hasTypeSymbol not applied to a type node",0); + return 0; + } + if (info_type[variant][1] == 's') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isAtomicType(variant) +int variant; +{ + if (!isATypeNode(variant)) + { + Message("isAtomicType not applied to a type node",0); + return 0; + } + if (info_type[variant][0] == 'a') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int hasNodeASymb(variant) +int variant; +{ + if ((!isABifNode(variant)) && (!isALoNode(variant))) + { + Message("hasNodeASymb not applied to a bif or low level node",0); + return 0; + } + if (general_info[variant][2] == 's') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isNodeAConst(variant) +int variant; +{ + if ((!isABifNode(variant)) && (!isALoNode(variant))) + { + Message("isNodeAConst not applied to a bif or low level node",0); + return 0; + } + if (general_info[variant][1] == 'c') + return TRUE; + else + return FALSE; +} + + +/***************************************************************************/ +int isAStructDeclBif(variant) +int variant; +{ + if (!isABifNode(variant)) + { + Message("isAStructDeclBif not applied to a bif",0); + return 0; + } + if (general_info[variant][1] == 's') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isAUnionDeclBif(variant) +int variant; +{ + if (!isABifNode(variant)) + { + Message("isAUnionDeclBif not applied to a bif",0); + return 0; + } + if (general_info[variant][1] == 'u') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isAEnumDeclBif(variant) +int variant; +{ + if (!isABifNode(variant)) + { + Message("isAEnumDeclBif not applied to a bif",0); + return 0; + } + if (general_info[variant][1] == 'e') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isADeclBif(variant) +int variant; +{ + if (!isABifNode(variant)) + { + Message("isADeclBif not applied to a bif",0); + return 0; + } + if (general_info[variant][0] == 'd') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int isAControlEnd(variant) +int variant; +{ + if (!isABifNode(variant)) + { + Message("isAControlEnd not applied to a bif",0); + return 0; + } + if (general_info[variant][0] == 'c') + return TRUE; + else + return FALSE; +} + +/***************************************************************************/ +int Check_Lang_Fortran(proj) +PTR_PROJ proj; +{ + PTR_FILE ptf; + PTR_BLOB ptb; + /* Change FALSE to TRUE */ + if (!proj) + return TRUE; + for (ptb = PROJ_FILE_CHAIN (proj); ptb ; ptb = BLOB_NEXT (ptb)) + { + ptf = (PTR_FILE) BLOB_VALUE (ptb); + /* if (debug) + fprintf(stderr,"%s\n",FILE_FILENAME (ptf)); */ + + if (FILE_LANGUAGE(ptf) != ForSrc) + return(FALSE); + } + return(TRUE); +} + + +/***************************************************************************/ +char* filter(s) +char *s; +{ + char c; + int i = 1; + char temp[1024]; + int temp_i = 0; + int buf_i = 0; + int commentline = 0; + char *resul, *init; + + if (!s) return NULL; + if (strlen(s)==0) return s; + + /* allocate very simple, but to redo later, allocate two times the size */ + make_a_malloc_stack(); + resul = (char *) mymalloc((int)(2*strlen(s))); + memset(resul, 0, 2*strlen(s)); + init = resul; + /* find the separator */ + c = s[0]; + + if ((c != ' ') + &&(c != '\n') + && (c != '0') + && (c != '1') + && (c != '2') + && (c != '3') + && (c != '4') + && (c != '5') + && (c != '6') + && (c != '7') + && (c != '8') + && (c != '9')) + if ( c=='!') commentline = HPF_STRING; + else commentline = 1; + else + commentline = 0; + + temp_i = 0; + i = 0; + buf_i =0; + while (c!='\0') + { + c = s[i]; + temp[ buf_i] = c; + if (c == '\n') + { + temp[ buf_i+1] = '\0'; + sprintf(resul,"%s",temp); + resul = resul + strlen(temp); + temp_i = -1; + buf_i = -1; + if ((s[i+1] != ' ') + &&(s[i+1] != '\n') + && (s[i+1] != '0') + && (s[i+1] != '1') + && (s[i+1] != '2') + && (s[i+1] != '3') + && (s[i+1] != '4') + && (s[i+1] != '5') + && (s[i+1] != '6') + && (s[i+1] != '7') + && (s[i+1] != '8') + && (s[i+1] != '9')) + if (s[i+1] == '!') commentline = HPF_STRING; + else commentline = 1; + else + commentline = 0; + } else + { + if ((temp_i == 71) && !commentline) + { + /* insert where necessary */ + temp[ buf_i+1] = '\0'; + sprintf(resul,"%s\n",temp); + resul = resul + strlen(temp)+1; + sprintf(resul," +"); + resul = resul + strlen(" +"); + commentline = 0; + memset(temp, 0, 1024); + temp_i = strlen(" +")-1; + buf_i = -1; + } + if ((temp_i == 71) && (commentline==HPF_STRING)) + { + /* insert where necessary */ + int count=0; + for(;s[i]!='$';i--,count++) + { + if (strncmp(&(s[i]),"ONTO", strlen("ONTO"))== 0) + break; + if (strncmp(&(s[i]),"BEGIN", strlen("BEGIN"))== 0) + break; + if (strncmp(&(s[i]),"ON", strlen("ON"))== 0) + { + i+=3;count-=3; + break; + } + if (strncmp(&(s[i]),"WITH", strlen("WITH"))== 0) + break; + if (strncmp(&(s[i]),"NEW", strlen("NEW"))== 0) + break; + if (strncmp(&(s[i]),"REDUCTION", strlen("REDUCTION"))== 0) + break; + if (strncmp(&(s[i]),"TEMPLATE", strlen("TEMPLATE"))== 0) + break; + if (strncmp(&(s[i]),"SHADOW", strlen("SHADOW"))== 0) + break; + if (strncmp(&(s[i]),"INHERIT", strlen("INHERIT"))== 0) + break; + if (strncmp(&(s[i]),"DYNAMIC", strlen("DYNAMIC"))== 0) + break; + if (strncmp(&(s[i]),"DIMENSION", strlen("DIMENSION"))== 0) + break; + if (strncmp(&(s[i]),"PROCESSORS", strlen("PROCESSORS"))== 0) + break; + if (strncmp(&(s[i]),"DISTRIBUTE", strlen("DISTRIBUTE"))== 0) + break; + if (strncmp(&(s[i]),"ALIGN", strlen("ALIGN"))== 0) + break; + if (strncmp(&(s[i]),"::", strlen("::"))== 0) + { + /*i+=3;count-=3;*/ + break; + } + } + i--;count++; + if (count<36) + temp[ buf_i+1-count] = '\0'; + else + { + i+=count; + temp[ buf_i+1] = '\0'; + } + sprintf(resul,"%s\n",temp); + resul = resul + strlen(temp)+1; + sprintf(resul,"!HPF$*"); + resul = resul + strlen("!HPF$*"); + /*8commentline = 0;*/ + memset(temp, 0, 1024); + temp_i = strlen("!HPF$*")-1; + buf_i = -1; + } + } + i++; + temp_i++; + buf_i++; + } + + return init; +} +/* preset some values of symbols for evaluateExpression*/ +#define ALLOCATECHUNKVALUE 100 +static PTR_SYMB *ValuesSymb = NULL; +static int *ValuesInt = NULL; +static int NbValues = 0; +static int NbElement = 0; + +/***************************************************************************/ +void allocateValueEvaluate() +{ + int i; + PTR_SYMB *pt1; + int *pt2; + + pt1 = (PTR_SYMB *) xmalloc((int)( sizeof(PTR_SYMB *) * + (NbValues + ALLOCATECHUNKVALUE))); + pt2 = (int *) xmalloc((int)( sizeof(int *) * (NbValues + ALLOCATECHUNKVALUE))); + + for (i=0; i -#include "db.h" -#include -#include - -extern int blklevel; -extern void warn1(); -extern PTR_BFND cur_scope(); -PTR_LABEL make_label(); -extern PTR_FILE fi; -void free(); - -extern PTR_CMNT comments; -extern PTR_FNAME cur_thread_file; -extern PTR_FNAME the_file; -extern int yylineno; -extern int mod_offset; -extern PTR_BFND last_bfnd; -extern PTR_BFND head_bfnd, last_bfnd; -extern PTR_LLND head_llnd; -extern PTR_SYMB head_symb; -extern PTR_TYPE head_type; -extern PTR_LABEL head_label; - -/* - * eqn -- checks if first n characters of two strings are the same - * - * input: - * n - length to be checked - * a - string1 - * b - string2 - * - * output: - * YES if the first n characters are same. NO, otherwise. - */ -int -eqn(n, a, b) - register int n; - register unsigned char *a, *b; -{ - while (--n >= 0) - if ((isupper(*a) ? tolower(*a++) : *a++) != *b++) - return (NO); - return (YES); -} - -/* - * StringConcatenation -- concatenate strings - */ -char *StringConcatenation(char *s1, char*s2) -{ - char *res = (char*)malloc(strlen(s1)+strlen(s2)+1); - res[0] = '\0'; - strcat(res, s1); - strcat(res, s2); - return res; -} - -/* - * convci - converts an ASCII string to binary - * - * input: - * n - length of the string - * s - the string to be converted - * - * output: - * the converted long value - */ -long -convci(n, s) - register int n; - register char *s; -{ - register long sum; - - sum = 0; - while (n-- > 0) - sum = 10 * sum + (*s++ - '0'); - return (sum); -} - - -/* - * convic -- converts a long integer to ASCII string - * - * input: - * n - the number to be converted - * - * output: - * the converted string - */ -char * -convic(n) - long n; -{ - static char s[20]; - register char *t; - - s[19] = '\0'; - t = s + 19; - - do { - *--t = '0' + n % 10; - n /= 10; - } while (n > 0); - - return (t); -} - - -/* - * Get a BIF node - */ -PTR_BFND -get_bfnd(fid,node_type, symb_ptr, ll1, ll2, ll3) -PTR_FILE fid; -int node_type; -PTR_SYMB symb_ptr; -PTR_LLND ll1, ll2, ll3; -{ - PTR_BFND new_bfnd, make_bfnd(); - - new_bfnd = make_bfnd(fid, node_type, symb_ptr, ll1, ll2, ll3); - new_bfnd->filename = the_file; - /*new_bfnd->filename = cur_thread_file;*/ /*podd 18.04.99*/ - new_bfnd->entry.Template.cmnt_ptr = comments; - new_bfnd->entry.Template.bl_ptr1 = BLNULL; - new_bfnd->entry.Template.bl_ptr2 = BLNULL; - new_bfnd->g_line = yylineno; - new_bfnd->l_line = yylineno - mod_offset; - last_bfnd = new_bfnd; - return (new_bfnd); -} - - -void -release_nodes() -{ - register PTR_BFND p1 = head_bfnd; - register PTR_LLND p2 = head_llnd; - register PTR_SYMB p3 = head_symb; - register PTR_TYPE p4 = head_type; - register PTR_LABEL p5 =head_label; - register PTR_BFND t1; - register PTR_LLND t2; - register PTR_SYMB t3; - register PTR_TYPE t4; - register PTR_LABEL t5; - - while (p1) { - t1 = p1; - p1 = p1->thread; -#ifdef __SPF - removeFromCollection(t1); -#endif - free ((char *)t1); - } - - while (p2) { - t2 = p2; - p2 = p2->thread; -#ifdef __SPF - removeFromCollection(t2); -#endif - free ((char *)t2); - } - - while (p3) { - t3 = p3; - p3 = p3->thread; -#ifdef __SPF - removeFromCollection(t3); -#endif - free ((char *)t3); - } - - while (p4) { - t4 = p4; - p4 = p4->thread; -#ifdef __SPF - removeFromCollection(t4); -#endif - free ((char *)t4); - } - - while (p5) { - t5 = p5; - p5 = p5->next; -#ifdef __SPF - removeFromCollection(t5); -#endif - free ((char *)t5); - } -} - +/*********************************************************************/ +/* pC++/Sage++ Copyright (C) 1993 */ +/* Indiana University University of Oregon University of Rennes */ +/*********************************************************************/ + +/* + * misc.c + * + * Misellanious help routines + */ + +#include "defs.h" +#include "defines.h" +#include +#include "db.h" +#include +#include + +extern int blklevel; +extern void warn1(); +extern PTR_BFND cur_scope(); +PTR_LABEL make_label(); +extern PTR_FILE fi; +void free(); + +extern PTR_CMNT comments; +extern PTR_FNAME cur_thread_file; +extern PTR_FNAME the_file; +extern int yylineno; +extern int mod_offset; +extern PTR_BFND last_bfnd; +extern PTR_BFND head_bfnd, last_bfnd; +extern PTR_LLND head_llnd; +extern PTR_SYMB head_symb; +extern PTR_TYPE head_type; +extern PTR_LABEL head_label; + +#ifdef __SPF +extern void removeFromCollection(void *pointer); +#endif + +/* + * eqn -- checks if first n characters of two strings are the same + * + * input: + * n - length to be checked + * a - string1 + * b - string2 + * + * output: + * YES if the first n characters are same. NO, otherwise. + */ +int +eqn(n, a, b) + register int n; + register unsigned char *a, *b; +{ + while (--n >= 0) + if ((isupper(*a) ? tolower(*a++) : *a++) != *b++) + return (NO); + return (YES); +} + +/* + * StringConcatenation -- concatenate strings + */ +char *StringConcatenation(char *s1, char*s2) +{ + char *res = (char*)malloc(strlen(s1)+strlen(s2)+1); + res[0] = '\0'; + strcat(res, s1); + strcat(res, s2); + return res; +} + +/* + * convci - converts an ASCII string to binary + * + * input: + * n - length of the string + * s - the string to be converted + * + * output: + * the converted long value + */ +long +convci(n, s) + register int n; + register char *s; +{ + register long sum; + + sum = 0; + while (n-- > 0) + sum = 10 * sum + (*s++ - '0'); + return (sum); +} + + +/* + * convic -- converts a long integer to ASCII string + * + * input: + * n - the number to be converted + * + * output: + * the converted string + */ +char * +convic(n) + long n; +{ + static char s[20]; + register char *t; + + s[19] = '\0'; + t = s + 19; + + do { + *--t = '0' + n % 10; + n /= 10; + } while (n > 0); + + return (t); +} + + +/* + * Get a BIF node + */ +PTR_BFND +get_bfnd(fid,node_type, symb_ptr, ll1, ll2, ll3) +PTR_FILE fid; +int node_type; +PTR_SYMB symb_ptr; +PTR_LLND ll1, ll2, ll3; +{ + PTR_BFND new_bfnd, make_bfnd(); + + new_bfnd = make_bfnd(fid, node_type, symb_ptr, ll1, ll2, ll3); + new_bfnd->filename = the_file; + /*new_bfnd->filename = cur_thread_file;*/ /*podd 18.04.99*/ + new_bfnd->entry.Template.cmnt_ptr = comments; + new_bfnd->entry.Template.bl_ptr1 = BLNULL; + new_bfnd->entry.Template.bl_ptr2 = BLNULL; + new_bfnd->g_line = yylineno; + new_bfnd->l_line = yylineno - mod_offset; + last_bfnd = new_bfnd; + return (new_bfnd); +} + + +void +release_nodes() +{ + register PTR_BFND p1 = head_bfnd; + register PTR_LLND p2 = head_llnd; + register PTR_SYMB p3 = head_symb; + register PTR_TYPE p4 = head_type; + register PTR_LABEL p5 =head_label; + register PTR_BFND t1; + register PTR_LLND t2; + register PTR_SYMB t3; + register PTR_TYPE t4; + register PTR_LABEL t5; + + while (p1) { + t1 = p1; + p1 = p1->thread; +#ifdef __SPF + removeFromCollection(t1); +#endif + free ((char *)t1); + } + + while (p2) { + t2 = p2; + p2 = p2->thread; +#ifdef __SPF + removeFromCollection(t2); +#endif + free ((char *)t2); + } + + while (p3) { + t3 = p3; + p3 = p3->thread; +#ifdef __SPF + removeFromCollection(t3); +#endif + free ((char *)t3); + } + + while (p4) { + t4 = p4; + p4 = p4->thread; +#ifdef __SPF + removeFromCollection(t4); +#endif + free ((char *)t4); + } + + while (p5) { + t5 = p5; + p5 = p5->next; +#ifdef __SPF + removeFromCollection(t5); +#endif + free ((char *)t5); + } +} + diff --git a/dvm/fdvm/trunk/parser/unparse_hpf.c b/dvm/fdvm/trunk/parser/unparse_hpf.c index d74d5bb..a06c60a 100644 --- a/dvm/fdvm/trunk/parser/unparse_hpf.c +++ b/dvm/fdvm/trunk/parser/unparse_hpf.c @@ -1,4971 +1,4895 @@ - /************************************************************************** - * * - * Unparser for toolbox * - * * - *************************************************************************/ - -#include - -#include "compatible.h" /* Make different system compatible... (PHB) */ -#ifdef SYS5 -#include -#else -#include -#endif -#include - -#include "dvm_tag.h" -#include "fdvm.h" -#include "macro.h" -#include "ext_lib.h" -#include "ext_low.h" -static int TabNumber =0; -static int Number_Of_Flag = 0; -#define TASK_PROC_GENERATE 0 -#define MAXFLAG 64 -#define MAXLFLAG 256 -#define MAXLEVEL 256 -#define IS_DISTRIBUTE_ARRAY(A) ((SYMB_ATTR((A)) & DISTRIBUTE_BIT) || (SYMB_ATTR((A)) & ALIGN_BIT) || (SYMB_ATTR((A)) & INHERIT_BIT)) -char *copys(char *); - -PTR_SYMB SymbolID[MAXFLAG]; -int Number_Of_Symbol = 0; -int On_count = 0; -int TaskRegionUnparse = 0; -int HPF_VERSION=0; -int errnumber=0; -int NumberOfIndependent=0; -static char TabOfFlag[MAXFLAG][MAXLFLAG]; -static int FlagLenght[MAXFLAG]; -static int FlagLevel[MAXFLAG]; -static int FlagOn[MAXLEVEL][MAXFLAG]; -#define MAXLENGHTBUF 750000 -static int Buf_pointer = 0; -static char UnpBuf[MAXLENGHTBUF]; -static char *Buf_address; -#ifdef __SPF_BUILT_IN_PARSER -static int CommentOut = 0; -#else -int CommentOut = 0; -#endif -int Pointer = 0; -char *hpfname; -#define C_Initialized 1 -#define Fortran_Initialized 2 -static int Parser_Initiated = 0; - -#ifdef __SPF_BUILT_IN_PARSER -static PTR_FILE current_file = NULL; -#else -PTR_FILE current_file=NULL; -#endif - -PTR_LLND On_Clause=NULL; -PTR_LLND ReductionList=NULL; -PTR_LLND NewSpecList=NULL; -extern void Message(); -/* FORWARD DECLARATIONS */ -int BufPutString(); -PTR_LLND FindMapDir(); -PTR_LLND ChangeRedistributeOntoTask(); -PTR_LLND FindRealignDir(); -PTR_LLND FindRedistributeDir(); -PTR_LLND FindDynamicDir(); -int UnparseEndofCircle(); -PTR_BFND FindDistrAlignCombinedDir(); -PTR_LLND FindPointerDescriptor(); -void gen_hpf_name (); -void PointerDeclaration(); -int ArrayOfPointerDeclaration(); -void GenerateType(); -void Init_HPFUnparser(); -void ResetSymbolId(); -int NumberOfForNode(); -int FindPointerDir(); -int FindPointerDeclaration(); -int FindCommonHeapDeclaration(); -int Puttab(); -int Find_SaveSymbol(); -int CheckNullDistribution(); -char *Tool_Unparse_Bif(); -char *Tool_Unparse2_LLnode (); -char * Tool_Unparse_Type(); -char * Tool_Unparse_Symbol(); -PTR_BFND FindBeginingOfBlock(); -PTR_BFND FindEndOfBlock(); -int CheckAcross(); -int CheckReduction(); -int IfReduction(); -int FindRedInExpr(); -PTR_LLND AddToReductionList(); -int FindInNewList(); -int isForNodeEndStmt(); -int ForNodeStmt(); -PTR_LLND FreeReductionList(); - -#include "f90.h" - -typedef struct -{ - char *str; - char *(* fct)(); -} UNP_EXPR; - - -static UNP_EXPR Unparse_Def[LAST_CODE]; - -/************ Unparse Flags **************/ -static int In_Write_Flag = 0; -static int Rec_Port_Decl = 0; -static int In_Param_Flag = 0; -static int In_Impli_Flag = 0; -static int In_Class_Flag = 0; -/*****************************************/ - -/*************** TYPE names in ASCII form ****************/ -static char *ftype_name[] = {"integer", - "real", - "double precision", - "character", - "logical", - "character", - "gate", - "event", - "sequence", - "", - "", - "", - "", - "complex", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "double complex" -};static char *ctype_name[] = {"int", - "float", - "double", - "char", - "logical", - "char", - "gate", - "event", - "sequence", - "error1", - "error2", - "error3", - "error4", - "complex", - "void", - "error6", - "error7", - "error8", - "error9", - "error10", - "error11", - "error12", - "ElementType", - "error14", - "error15", - "error16", - "error17", - "error18", - "error19", - "error20", - "error21", - "error22", - "error23" -}; - -static -char *ridpointers[] = { - "-error1-", /* unused */ - "-error2-", /* int */ - "char", /* char */ - "float", /* float */ - "double", /* double */ - "void", /* void */ - "-error3-", /* unused1 */ - "unsigned", /* unsigned */ - "short", /* short */ - "long", /* long */ - "auto", /* auto */ - "static", /* static */ - "extern", /* extern */ - "register", /* register */ - "typedef", /* typedef */ - "signed", /* signed */ - "const", /* const */ - "volatile", /* volatile */ - "private", /* private */ - "future", /* future */ - "virtual", /* virtual */ - "inline", /* inline */ - "friend", /* friend */ - "-error4-", /* public */ - "-error5-", /* protected */ - "Sync", /* CC++ sync */ - "global", /* CC++ global */ - "atomic", /* CC++ atomic */ - "__private", /* for KSR */ - "restrict" -}; - -/*********************************************************/ - -/******* Precedence table of operators for Fortran *******/ -static char precedence[] = {5, /* .eq. */ - 5, /* .lt. */ - 5, /* .gt. */ - 5, /* .ne. */ - 5, /* .le. */ - 5, /* .ge. */ - 3, /* + */ - 3, /* - */ - 8, /* .or. */ - 2, /* * */ - 2, /* / */ - 0, /* none */ - 7, /* .and. */ - 1, /* ** */ - 0, /* none */ - 4, /* // */ - 8, /* .xor. */ - 9, /* .eqv. */ - 9, /* .neqv. */ - 1, /* Minus_op*/ - 1 /* not op */ - }; - -#define type_index(X) (X-T_INT) /* gives the index of a type to access the Table "ftype_name" from a type code */ -#define binop(n) (n >= EQ_OP && n <= NEQV_OP) /* gives the boolean value of the operation "n" being binary (not unary) */ -/* In order to change ON-block procedure call */ -typedef struct func_call *PTR_FCALL; - -struct func_call - { - PTR_LLND func_ref; - PTR_BFND first; - PTR_BFND last; - PTR_FCALL next; - }; - -PTR_LLND parameter_list=NULL; -PTR_SYMB function_name=NULL; -int ON_BLOCK=0; -int ON_BEGIN=0; -PTR_FCALL TaskRegion=NULL; -extern char *chkalloc(); -#define ALLOC(x) (struct x *) chkalloc(sizeof(struct x)) -#define FUNC_REF(NODE) ((NODE)->func_ref) -#define FUNC_FIRST(NODE) ((NODE)->first) -#define FUNC_LAST(NODE) ((NODE)->last) -#define FUNC_NEXT(NODE) ((NODE)->next) - -void UnparseTaskRegion(PTR_FCALL TaskRegion) -{ -PTR_BFND bif; -PTR_FCALL temp=NULL; -TaskRegionUnparse=1; -for(;TaskRegion;temp=TaskRegion,TaskRegion=FUNC_NEXT(TaskRegion)) - { - PTR_LLND llnd; - if (temp) - { - FUNC_NEXT(temp)=NULL; - FUNC_FIRST(temp)=NULL; - FUNC_LAST(temp)=NULL; -#ifdef __SPF - removeFromCollection(FUNC_REF(temp)); - removeFromCollection(temp); -#endif - free(FUNC_REF(temp)); - free(temp); - } - BufPutString("\n",0); - Puttab(); - BufPutString("subroutine ",0); - Tool_Unparse2_LLnode(FUNC_REF(TaskRegion)); - BufPutString("\n",0); - llnd=NODE_OPERAND0(FUNC_REF(TaskRegion)); - for(;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - if (NODE_CODE(NODE_OPERAND0(llnd))==ARRAY_REF) - { - if (NODE_SYMB(NODE_OPERAND0(llnd))) - { - PTR_SYMB sym=NODE_SYMB(NODE_OPERAND0(llnd)); - if ((SYMB_ATTR(sym)&DISTRIBUTE_BIT)|| - (SYMB_ATTR(sym)&ALIGN_BIT)) - { - Puttab(); - BufPutString("DIMENSION ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString("(",0); - Tool_Unparse2_LLnode(TYPE_DECL_RANGES(SYMB_TYPE(sym))); - BufPutString(")",0); - BufPutString("\n",0); - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("INHERIT ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString("\n",0); - } - else - { - Puttab(); - BufPutString("DIMENSION ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString("(",0); - Tool_Unparse2_LLnode(TYPE_DECL_RANGES(SYMB_TYPE(sym))); - BufPutString(")",0); - BufPutString("\n",0); - } - } - } - else - { - PTR_SYMB sym=NODE_SYMB(NODE_OPERAND0(llnd)); - if ((NODE_CODE(NODE_OPERAND0(llnd))==CONST_REF)|| - (NODE_CODE(NODE_OPERAND0(llnd))==VAR_REF)) - { - Puttab(); - Tool_Unparse_Type(SYMB_TYPE(sym)); - BufPutString(" ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString("\n",0); - } - } - } - for(bif=BIF_NEXT(FUNC_FIRST(TaskRegion));bif&&(bif!=FUNC_LAST(TaskRegion));bif=BIF_NEXT(bif)) - Tool_Unparse_Bif(bif); - Puttab(); - BufPutString("end \n\n",0); - } -if (temp) - { - FUNC_NEXT(temp)=NULL; - FUNC_FIRST(temp)=NULL; - FUNC_LAST(temp)=NULL; -#ifdef __SPF - removeFromCollection(FUNC_REF(temp)); - removeFromCollection(temp); -#endif - free(FUNC_REF(temp)); - free(temp); - } - -TaskRegionUnparse=0; -} - - -PTR_FCALL -FindLast ( ptr ) -PTR_FCALL ptr; -{ -PTR_FCALL ptr_func=ptr; -if (!ptr) return NULL; -while(ptr_func) - { - if (FUNC_NEXT(ptr_func) == NULL) - return ptr_func; - ptr_func = FUNC_NEXT(ptr_func); - } -return NULL; -} - -PTR_LLND -make_llnode (node_type, ll1, ll2, symb_ptr) - int node_type; - PTR_LLND ll1, ll2; - PTR_SYMB symb_ptr; -{ - PTR_LLND new_llnd; - - new_llnd = ALLOC (llnd); - new_llnd->variant = node_type; - new_llnd->type = TYNULL; - new_llnd->entry.Template.ll_ptr1 = ll1; - new_llnd->entry.Template.ll_ptr2 = ll2; - switch (node_type) { - case INT_VAL: - /*new_llnd->entry.ival = (int) symb_ptr;*/ - break; - case BOOL_VAL: - /*new_llnd->entry.bval = (int) symb_ptr;*/ - break; - default: - new_llnd->entry.Template.symbol = symb_ptr; - break; - } - return (new_llnd); -} - -PTR_SYMB -make_funcsymb (string) - char *string; -{ - PTR_SYMB new_symb; - new_symb = ALLOC (symb); - new_symb->variant = ARRAY_REF; - new_symb->ident = copys (string); - return (new_symb); -} - -void ResetSymbolDovar() -{ -PTR_SYMB symb; -for (symb = current_file->head_symb; symb ; symb = SYMB_NEXT (symb)) - if (SYMB_DOVAR (symb)) SYMB_DOVAR (symb) &=~2 ; -} - - - -/* In order to change ON-block procedure call end*/ - -/* manage the unparse buffer */ - -#ifdef __SPF_BUILT_IN_PARSER -void DealWith_Rid_temp(typei, flg) -#else -void DealWith_Rid(typei, flg) -#endif - PTR_TYPE typei; - int flg; /* if 1 then do virtual */ -{ int j; - - int index; - PTR_TYPE type; - if (!typei) - return; - - for (type = typei; type; ) - { - switch(TYPE_CODE(type)) - { - case T_POINTER : - case T_REFERENCE : - case T_FUNCTION : - case T_ARRAY : - type = TYPE_BASE(type); - break; - case T_MEMBER_POINTER: - type = TYPE_COLL_BASE(type); - case T_DESCRIPT : - index = TYPE_LONG_SHORT(type); - /* printf("index = %d\n", index); */ - if( index & BIT_RESTRICT) { - BufPutString(ridpointers[(int)RID_RESTRICT],0); - BufPutString(" ", 0); - } - if( index & BIT_KSRPRIVATE) { - BufPutString(ridpointers[(int)RID_KSRPRIVATE],0); - BufPutString(" ", 0); - } - if( index & BIT_EXTERN) { - BufPutString(ridpointers[(int)RID_EXTERN],0); - BufPutString(" ", 0); - } - if( index & BIT_TYPEDEF) { - BufPutString(ridpointers[(int)RID_TYPEDEF],0); - BufPutString(" ", 0); - } - for (j=1; j< MAX_BIT; j= j*2) - { - switch (index & j) - { - case (int) BIT_PRIVATE: BufPutString(ridpointers[(int)RID_PRIVATE],0); - break; - case (int) BIT_FUTURE: BufPutString(ridpointers[(int)RID_FUTURE],0); - break; - case (int) BIT_VIRTUAL: if(flg) BufPutString(ridpointers[(int)RID_VIRTUAL],0); - break; - case (int) BIT_ATOMIC: if(flg) BufPutString(ridpointers[(int)RID_ATOMIC],0); - break; - case (int) BIT_INLINE: BufPutString(ridpointers[(int)RID_INLINE],0); - break; - case (int) BIT_UNSIGNED: BufPutString(ridpointers[(int)RID_UNSIGNED],0); - break; - case (int) BIT_SIGNED : BufPutString(ridpointers[(int)RID_SIGNED],0); - break; - case (int) BIT_SHORT : BufPutString(ridpointers[(int)RID_SHORT],0); - break; - case (int) BIT_LONG : BufPutString(ridpointers[(int)RID_LONG],0); - break; - case (int) BIT_VOLATILE: BufPutString(ridpointers[(int)RID_VOLATILE],0); - break; - case (int) BIT_CONST : BufPutString(ridpointers[(int)RID_CONST],0); - break; - case (int) BIT_GLOBL : BufPutString(ridpointers[(int)RID_GLOBL],0); - break; - case (int) BIT_SYNC : BufPutString(ridpointers[(int)RID_SYNC],0); - break; - case (int) BIT_TYPEDEF : /* BufPutString(ridpointers[(int)RID_TYPEDEF],0); */ - break; - case (int) BIT_EXTERN : /* BufPutString(ridpointers[(int)RID_EXTERN],0); */ - break; - case (int) BIT_AUTO : BufPutString(ridpointers[(int)RID_AUTO],0); - break; - case (int) BIT_STATIC : BufPutString(ridpointers[(int)RID_STATIC],0); - break; - case (int) BIT_REGISTER: BufPutString(ridpointers[(int)RID_REGISTER],0); - break; - case (int) BIT_FRIEND: BufPutString(ridpointers[(int)RID_FRIEND],0); - - } - if ((index & j) != 0) - BufPutString(" ",0); - } - type = TYPE_DESCRIP_BASE_TYPE(type); - break; - default: - type = NULL; - } - } -} - -#ifdef __SPF_BUILT_IN_PARSER -int is_overloaded_type_temp(bif) -#else -int is_overloaded_type(bif) -#endif - PTR_BFND bif; -{ - PTR_LLND ll; - if(!bif) return 0; - ll = BIF_LL1(bif); - while(ll && (NODE_SYMB(ll) == NULL)) ll = NODE_OPERAND0(ll); - if(ll == NULL) return 0; - if(SYMB_ATTR(NODE_SYMB(ll)) & OVOPERATOR) return 1; - else return 0; -} - -#ifdef __SPF_BUILT_IN_PARSER -PTR_TYPE Find_Type_For_Bif_temp(bif) -#else -PTR_TYPE Find_Type_For_Bif(bif) -#endif - PTR_BFND bif; -{ - PTR_TYPE type = NULL; - if (BIF_LL1(bif) && (NODE_CODE(BIF_LL1(bif)) == EXPR_LIST)) - { PTR_LLND tp; - tp = BIF_LL1(bif); - for (tp = NODE_OPERAND0(tp); tp && (type == NULL); ) - { - switch (NODE_CODE(tp)) { - case BIT_NUMBER: - case ASSGN_OP : - case ARRAY_OP: - case FUNCTION_OP : - case CLASSINIT_OP: - case ADDRESS_OP: - case DEREF_OP : - tp = NODE_OPERAND0(tp); - break ; - case SCOPE_OP: - tp = NODE_OPERAND1(tp); - break; - case FUNCTION_REF: - case ARRAY_REF: - case VAR_REF: - if (tp) - { if(!NODE_SYMB(tp)){ - printf("syntax error at line %d\n", bif->g_line); - exit(1); - } - else - type = SYMB_TYPE(NODE_SYMB(tp)); - } - tp = NULL; - break ; - default: - type = NODE_TYPE(tp); - break; - } - } - } - return type; -} - -#ifdef __SPF_BUILT_IN_PARSER -int Find_Protection_For_Bif_temp(bif) -#else -int Find_Protection_For_Bif(bif) -#endif - PTR_BFND bif; -{ - int protect = 0; - if (BIF_LL1(bif) && (BIF_CODE(BIF_LL1(bif)) == EXPR_LIST)) - { PTR_LLND tp; - tp = BIF_LL1(bif); - for (tp = NODE_OPERAND0(tp); tp && (protect == 0); ) /*(protect == NULL)*/ - { - switch (NODE_CODE(tp)) { - case BIT_NUMBER: - case ASSGN_OP : - case ARRAY_OP: - case FUNCTION_OP : - case CLASSINIT_OP: - case ADDRESS_OP: - case DEREF_OP : - tp = NODE_OPERAND0(tp); - break ; - case SCOPE_OP: - tp = NODE_OPERAND1(tp); - break; - case FUNCTION_REF: - case ARRAY_REF: - case VAR_REF: - if (tp) - protect = SYMB_ATTR(NODE_SYMB(tp)); - tp = NULL; - break ; - } - } - } - return protect; -} - -#ifdef __SPF_BUILT_IN_PARSER -PTR_TYPE Find_BaseType_temp(ptype) -#else -PTR_TYPE Find_BaseType(ptype) -#endif - PTR_TYPE ptype; -{ - PTR_TYPE pt; - - if (!ptype) - return NULL; - pt = TYPE_BASE (ptype); - if (pt) - { int j; - j = 0; - while ((j < 100) && pt) - { - if (TYPE_CODE(pt) == DEFAULT) break; - if (TYPE_CODE(pt) == T_INT) break; - if (TYPE_CODE(pt) == T_FLOAT) break; - if (TYPE_CODE(pt) == T_DOUBLE) break; - if (TYPE_CODE(pt) == T_CHAR) break; - if (TYPE_CODE(pt) == T_BOOL) break; - if (TYPE_CODE(pt) == T_STRING) break; - if (TYPE_CODE(pt) == T_COMPLEX) break; - if (TYPE_CODE(pt) == T_DCOMPLEX) break; - if (TYPE_CODE(pt) == T_VOID) break; - if (TYPE_CODE(pt) == T_UNKNOWN) break; - if (TYPE_CODE(pt) == T_DERIVED_TYPE) break; - if (TYPE_CODE(pt) == T_DERIVED_COLLECTION) break; - if (TYPE_CODE(pt) == T_DERIVED_TEMPLATE) break; - if (TYPE_CODE(pt) == T_DERIVED_CLASS) break; - if (TYPE_CODE(pt) == T_CLASS) break; - if (TYPE_CODE(pt) == T_COLLECTION) break; - if (TYPE_CODE(pt) == T_DESCRIPT) break; /* by dbg */ - - pt = TYPE_BASE (pt); - j++; - } - if (j == 100) - { - Message("Looping in getting the Basetype; sorry",0); - exit(1); - } - } - return pt; -} - -#ifdef __SPF_BUILT_IN_PARSER -PTR_TYPE Find_BaseType2_temp(ptype) -#else -PTR_TYPE Find_BaseType2(ptype) /* breaks out of the loop for pointers and references BW */ -#endif - PTR_TYPE ptype; -{ - PTR_TYPE pt; - - if (!ptype) - return NULL; - pt = TYPE_BASE (ptype); - if (pt) - { int j; - j = 0; - while ((j < 100) && pt) - { - if (TYPE_CODE(pt) == T_REFERENCE) break; - if (TYPE_CODE(pt) == T_POINTER) break; - if (TYPE_CODE(pt) == DEFAULT) break; - if (TYPE_CODE(pt) == T_INT) break; - if (TYPE_CODE(pt) == T_FLOAT) break; - if (TYPE_CODE(pt) == T_DOUBLE) break; - if (TYPE_CODE(pt) == T_CHAR) break; - if (TYPE_CODE(pt) == T_BOOL) break; - if (TYPE_CODE(pt) == T_STRING) break; - if (TYPE_CODE(pt) == T_COMPLEX) break; - if (TYPE_CODE(pt) == T_DCOMPLEX) break; - if (TYPE_CODE(pt) == T_VOID) break; - if (TYPE_CODE(pt) == T_UNKNOWN) break; - if (TYPE_CODE(pt) == T_DERIVED_TYPE) break; - if (TYPE_CODE(pt) == T_DERIVED_COLLECTION) break; - if (TYPE_CODE(pt) == T_DERIVED_CLASS) break; - if (TYPE_CODE(pt) == T_CLASS) break; - if (TYPE_CODE(pt) == T_COLLECTION) break; - if (TYPE_CODE(pt) == T_DESCRIPT) break; /* by dbg */ - - pt = TYPE_BASE (pt); - j++; - } - if (j == 100) - { - Message("Looping in getting the Basetype; sorry",0); - exit(1); - } - } - return pt; -} - - -#ifdef __SPF_BUILT_IN_PARSER -char* create_unp_str_temp(str) -#else -char *create_unp_str(str) -#endif - char *str; -{ - char *pt; - - if (!str) - return NULL; - - pt = (char *) xmalloc((int)(strlen(str)+1)); - memset(pt, 0, strlen(str)+1); - strcpy(pt,str); - return pt; -} - -#ifdef __SPF_BUILT_IN_PARSER -char* alloc_str_temp(size) -#else -char *alloc_str(size) -#endif - int size; -{ - char *pt; - - if (!(size++)) return NULL; - pt = (char *) xmalloc(size); - memset(pt, 0, size); - return pt; -} - -#ifdef __SPF_BUILT_IN_PARSER -int Reset_Unparser_temp() -#else -int Reset_Unparser() -#endif -{ - int i,j; - - /* initialize the number of flag */ - Number_Of_Flag = 0; - for (i=0; i < MAXFLAG ; i++) - { - TabOfFlag[i][0] = '\0'; - FlagLenght[i] = 0; - for(j=0; j= MAXLENGHTBUF) - { - Message("Unparse Buffer Full",0); - return 0; - } - Buf_address[Buf_pointer] = c; - Buf_pointer++; - return 1; -} - -#ifdef __SPF_BUILT_IN_PARSER -int BufPutString_temp(s,len) -#else -int BufPutString(s, len) -#endif - char *s; - int len; -{ - int length; - if (!s) - { - Message("Null String in BufPutString",0); - return 0; - } - length = len; - if (length <= 0) - length = strlen(s); - - if (Buf_pointer + length>= MAXLENGHTBUF) - { - Message("Unparse Buffer Full",0); - return 0; - } - strncpy(&(Buf_address[Buf_pointer]),s,length); - Buf_pointer += length; - return 1; -} - -#ifdef __SPF_BUILT_IN_PARSER -int BufPutInt_temp(i) -#else -int BufPutInt(i) -#endif - int i; -{ - int length; - char s[MAXLFLAG]; - - sprintf(s,"%d",i); - length = strlen(s); - - if (Buf_pointer + length>= MAXLENGHTBUF) - { - Message("Unparse Buffer Full",0); - return 0; - } - strncpy(&(Buf_address[Buf_pointer]),s,length); - Buf_pointer += length; - return 1; -} - -#ifdef __SPF_BUILT_IN_PARSER -int Get_Flag_val_temp(str, i) -#else -int Get_Flag_val(str, i) -#endif - char *str; - int *i; -{ - int j, con; - char sflag[MAXLFLAG]; - (*i)++; /* skip the paranthesis */ - /* extract the flag name */ - j = *i; - con = 0; - - while ((str[j] != '\0') && (str[j] != ')')) - { - sflag[con] = str[j]; - con ++; - j ++; - } - sflag[con] = '\0'; - con ++; - - /* look in table if flag is in */ - - for (j = 0 ; j < Number_Of_Flag; j++) - { - if (strncmp(TabOfFlag[j],sflag, con) == 0) - break; - } - *i += con; - if (j >= Number_Of_Flag) - { - /* not found */ - return 0; - } - else - return FlagOn[FlagLevel[j]][j]; - -} - -#ifdef __SPF_BUILT_IN_PARSER -void Treat_Flag_temp(str, i, val) -#else -void Treat_Flag(str, i, val) -#endif - char *str; - int *i; - int val; -{ - int j, con; - char sflag[MAXLFLAG]; - (*i)++; /* skip the paranthesis */ - /* extract the flag name */ - j = *i; - con = 0; - - while ((str[j] != '\0') && (str[j] != ')')) - { - sflag[con] = str[j]; - con ++; - j ++; - } - sflag[con] = '\0'; - con ++; - - /* look in table if flag is in */ - - for (j = 0 ; j < Number_Of_Flag; j++) - { - if (strncmp(TabOfFlag[j],sflag, con) == 0) - break; - } - if (j >= Number_Of_Flag) - { - /* not found */ - strcpy(TabOfFlag[Number_Of_Flag],sflag); - FlagOn[0][Number_Of_Flag] = val; - FlagLenght[Number_Of_Flag] = con-1; - Number_Of_Flag++; - } else - FlagOn[FlagLevel[j]][j] += val; - *i += con; -} - -#ifdef __SPF_BUILT_IN_PARSER -void PushPop_Flag_temp(str, i, val) -#else -void PushPop_Flag(str, i, val) -#endif - char *str; - int *i; - int val; -{ - int j, con; - char sflag[MAXLFLAG]; - (*i)++; /* skip the paranthesis */ - /* extract the flag name */ - j = *i; - con = 0; - - while ((str[j] != '\0') && (str[j] != ')')) - { - sflag[con] = str[j]; - con ++; - j ++; - } - sflag[con] = '\0'; - con ++; - - /* look in table if flag is in */ - - for (j = 0 ; j < Number_Of_Flag; j++) - { - if (strncmp(TabOfFlag[j],sflag, con) == 0) - break; - } - if (j < Number_Of_Flag) - { - /* if a pop, clear old value befor poping */ - if(val< 0) FlagOn[FlagLevel[j]][j] = 0; /* added by dbg to make sure initialized */ - FlagLevel[j] += val; - if (FlagLevel[j] < 0) - FlagLevel[j] = 0; - if (FlagLevel[j] >= MAXLEVEL) - { - Message("Stack of flag overflow; abort()",0); - abort(); - } - } - /* else printf("WARNING(unparser): unknow flag pushed or popped:%s\n",sflag); */ - *i += con; -} - -char * Tool_Unparse_Type(); - -#ifdef __SPF_BUILT_IN_PARSER -char* Tool_Unparse_Symbol_temp(symb) -#else -char *Tool_Unparse_Symbol (symb) -#endif - PTR_SYMB symb; -{ - PTR_TYPE ov_type; - if (!symb) - return NULL; - if (SYMB_IDENT(symb)) - { - if((SYMB_ATTR(symb) & OVOPERATOR)){ - ov_type = SYMB_TYPE(symb); - if(TYPE_CODE(ov_type) == T_DESCRIPT){ - if(TYPE_LONG_SHORT(ov_type) == BIT_VIRTUAL && In_Class_Flag){ - BufPutString ("virtual ",0); - if(TYPE_LONG_SHORT(ov_type) == BIT_ATOMIC) BufPutString ("atomic ",0); - ov_type = TYPE_DESCRIP_BASE_TYPE(ov_type); - } - if(TYPE_LONG_SHORT(ov_type) == BIT_INLINE){ - BufPutString ("inline ",0); - ov_type = TYPE_DESCRIP_BASE_TYPE(ov_type); - } - } - } else ov_type = NULL; - - if ((SYMB_ATTR(symb) & OVOPERATOR) || - (strcmp(SYMB_IDENT(symb),"()")==0) || - (strcmp(SYMB_IDENT(symb),"*")==0) || - (strcmp(SYMB_IDENT(symb),"+")==0) || - (strcmp(SYMB_IDENT(symb),"-")==0) || - (strcmp(SYMB_IDENT(symb),"/")==0) || - (strcmp(SYMB_IDENT(symb),"=")==0) || - (strcmp(SYMB_IDENT(symb),"%")==0) || - (strcmp(SYMB_IDENT(symb),"&")==0) || - (strcmp(SYMB_IDENT(symb),"|")==0) || - (strcmp(SYMB_IDENT(symb),"!")==0) || - (strcmp(SYMB_IDENT(symb),"~")==0) || - (strcmp(SYMB_IDENT(symb),"^")==0) || - (strcmp(SYMB_IDENT(symb),"+=")==0) || - (strcmp(SYMB_IDENT(symb),"-=")==0) || - (strcmp(SYMB_IDENT(symb),"*=")==0) || - (strcmp(SYMB_IDENT(symb),"/=")==0) || - (strcmp(SYMB_IDENT(symb),"%=")==0) || - (strcmp(SYMB_IDENT(symb),"^=")==0) || - (strcmp(SYMB_IDENT(symb),"&=")==0) || - (strcmp(SYMB_IDENT(symb),"|=")==0) || - (strcmp(SYMB_IDENT(symb),"<<")==0) || - (strcmp(SYMB_IDENT(symb),">>")==0) || - (strcmp(SYMB_IDENT(symb),"<<=")==0) || - (strcmp(SYMB_IDENT(symb),">>=")==0) || - (strcmp(SYMB_IDENT(symb),"==")==0) || - (strcmp(SYMB_IDENT(symb),"!=")==0) || - (strcmp(SYMB_IDENT(symb),"<=")==0) || - (strcmp(SYMB_IDENT(symb),">=")==0) || - (strcmp(SYMB_IDENT(symb),"<")==0) || - (strcmp(SYMB_IDENT(symb),">")==0) || - (strcmp(SYMB_IDENT(symb),"&&")==0) || - (strcmp(SYMB_IDENT(symb),"||")==0) || - (strcmp(SYMB_IDENT(symb),"++")==0) || - (strcmp(SYMB_IDENT(symb),"--")==0) || - (strcmp(SYMB_IDENT(symb),"->")==0) || - (strcmp(SYMB_IDENT(symb),"->*")==0) || - (strcmp(SYMB_IDENT(symb),",")==0) || - (strcmp(SYMB_IDENT(symb),"new")==0) || - (strcmp(SYMB_IDENT(symb),"delete")==0) || - (strcmp(SYMB_IDENT(symb),"[]")==0) ) - BufPutString ("operator ",0); - } - /* - if(ov_type) Tool_Unparse_Type(ov_type, 0); - else */ - BufPutString (SYMB_IDENT(symb),0); - return Buf_address; -} - - -typedef struct -{ - int typ; - union {char *S; - long I; - } val; -} operand; - -/* macro def. of operand type */ -#define UNDEF_TYP 0 -#define STRING_TYP 1 -#define INTEGER_TYP 2 - -/* macro def. of comparison operators */ -#define COMP_UNDEF -1 /* Bodin */ -#define COMP_EQUAL 0 -#define COMP_DIFF 1 - - -#ifdef __SPF_BUILT_IN_PARSER -void Get_Type_Operand_temp(str, iptr, ptype, Op) -#else -void Get_Type_Operand (str, iptr, ptype,Op) -#endif - char *str; - int *iptr; - PTR_TYPE ptype; - operand *Op; -{ - - Op->typ = UNDEF_TYP; - if (strncmp(&(str[*iptr]),"%CHECKFLAG", strlen("%CHECKFLAG"))== 0) - { - Op->typ = INTEGER_TYP; - *iptr += strlen("%CHECKFLAG"); - Op->val.I = Get_Flag_val(str, iptr); - } else - if (strncmp(&(str[*iptr]),"%STRCST", strlen("%STRCST"))== 0) /* %STRCST : String Constant */ - { - int i_save; - - *iptr += strlen("%STRCST"); - while (str[*iptr] == ' ') {(*iptr)++;} /* skip spaces before string */ - if (str[*iptr] != '\'') - { - Message (" *** Missing \"'\" after %STRCST *** ",0); - } - i_save = ++(*iptr); - while ((str[*iptr] != '\0') && (str[*iptr] != '\'')) (*iptr)++; - Op->val.S = alloc_str ((*iptr) - i_save); - strncpy (Op->val.S, &(str[i_save]), (*iptr) - i_save); - Op->typ = STRING_TYP; - } else - if (strncmp(&(str[*iptr]),"%NULL", strlen("%NULL"))== 0) /* %NULL : Integer Constant (or false boolean) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = 0; - *iptr += strlen("%NULL"); - } else - if (strncmp(&(str[*iptr]),"%INIMPLI", strlen("%INIMPLI"))== 0) /* %INIMPLI : In_Impli_Statement (integer / boolean flag) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = In_Impli_Flag; - *iptr += strlen("%INIMPLI"); - } else - { - Message (" *** Unknown operand in %IF (condition) for Type Node *** ",0); - } -} - -#ifdef __SPF_BUILT_IN_PARSER -void Get_LL_Operand_temp(str, iptr, ll, Op) -#else -void Get_LL_Operand (str, iptr, ll, Op) -#endif - char *str; - int *iptr; - PTR_LLND ll; - operand *Op; -{ - - Op->typ = UNDEF_TYP; - if (strncmp(&(str[*iptr]),"%CHECKFLAG", strlen("%CHECKFLAG"))== 0) - { - Op->typ = INTEGER_TYP; - *iptr += strlen("%CHECKFLAG"); - Op->val.I = Get_Flag_val(str, iptr); - } else - if (strncmp(&(str[*iptr]),"%STRCST", strlen("%STRCST"))== 0) /* %STRCST : String Constant */ - { - int i_save; - - *iptr += strlen("%STRCST"); - while (str[*iptr] == ' ') {(*iptr)++;} /* skip spaces before string */ - if (str[*iptr] != '\'') - { - Message (" *** Missing \"'\" after %STRCST *** ",0); - } - i_save = ++(*iptr); - while ((str[*iptr] != '\0') && (str[*iptr] != '\'')) (*iptr)++; - Op->val.S = alloc_str ((*iptr) - i_save); - strncpy (Op->val.S, &(str[i_save]), (*iptr) - i_save); - Op->typ = STRING_TYP; - } else - if (strncmp(&(str[*iptr]),"%SYMBOL", strlen("%SYMBOL"))== 0) /* %SYMBOL : Symbol pointer (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) NODE_SYMB (ll); - *iptr += strlen("%SYMBOL"); - } else - if (strncmp(&(str[*iptr]),"%SYMBID", strlen("%SYMBID"))== 0) /* %SYMBID : Symbol identifier (string) */ - { - Op->typ = STRING_TYP; - if (NODE_SYMB (ll)) - Op->val.S = SYMB_IDENT (NODE_SYMB (ll)); - else - Op->val.S = NULL; - *iptr += strlen("%SYMBID"); - } else - if (strncmp(&(str[*iptr]),"%NULL", strlen("%NULL"))== 0) /* %NULL : Integer Constant (or false boolean) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = 0; - *iptr += strlen("%NULL"); - } else - if (strncmp(&(str[*iptr]),"%LL1", strlen("%LL1"))== 0) /* %LL1 : Low Level Node 1 (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) NODE_TEMPLATE_LL1 (ll); - *iptr += strlen("%LL1"); - } else - if (strncmp(&(str[*iptr]),"%LL2", strlen("%LL2"))== 0) /* %LL2 : Low Level Node 2 (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) NODE_TEMPLATE_LL2 (ll); - *iptr += strlen("%LL2"); - } else - if (strncmp(&(str[*iptr]),"%LABUSE", strlen("%LABUSE"))== 0) /* %LABUSE : label ptr (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) NODE_LABEL (ll); - *iptr += strlen("%LABUSE"); - } else - if (strncmp(&(str[*iptr]),"%L1CODE", strlen("%L1CODE"))== 0) /* %L1CODE : Code (variant) of Low Level Node 1 (integer) */ - { - Op->typ = INTEGER_TYP; - if (NODE_TEMPLATE_LL1 (ll)) - Op->val.I = NODE_CODE (NODE_TEMPLATE_LL1 (ll)); - else - Op->val.I = (long) NULL; - *iptr += strlen("%L1CODE"); - } else - if (strncmp(&(str[*iptr]),"%L2CODE", strlen("%L2CODE"))== 0) /* %L2CODE : Code (variant) of Low Level Node 2 (integer) */ - { - Op->typ = INTEGER_TYP; - if (NODE_TEMPLATE_LL2 (ll)) - Op->val.I = NODE_CODE (NODE_TEMPLATE_LL2 (ll)); - else - Op->val.I = (long) NULL; - *iptr += strlen("%L2CODE"); - } else - if (strncmp(&(str[*iptr]),"%INWRITE", strlen("%INWRITE"))== 0) /* %INWRITE : In_Write_Statement (integer / boolean flag) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = In_Write_Flag; - *iptr += strlen("%INWRITE"); - } else - if (strncmp(&(str[*iptr]),"%RECPORT", strlen("%RECPORT"))== 0) /* %RECPORT : reccursive_port_decl (integer / boolean flag) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = Rec_Port_Decl; - *iptr += strlen("%RECPORT"); - } else - if (strncmp(&(str[*iptr]),"%INPARAM", strlen("%INPARAM"))== 0) /* %INPARAM : In_Param_Statement (integer / boolean flag) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = In_Param_Flag; - *iptr += strlen("%INPARAM"); - } else - if (strncmp(&(str[*iptr]),"%INIMPLI", strlen("%INIMPLI"))== 0) /* %INIMPLI : In_Impli_Statement (integer / boolean flag) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = In_Impli_Flag; - *iptr += strlen("%INIMPLI"); - } else - if (strncmp(&(str[*iptr]),"%L1L2*L1CODE", strlen("%L1L2*L1CODE"))== 0) /* %L1L2L1CODE : Code (variant) of Low Level Node 1 of Low Level Node 2 of Low Level Node 1 (integer) */ - { - PTR_LLND temp; - - Op->typ = INTEGER_TYP; - if (NODE_OPERAND0(ll)) - { - temp = NODE_OPERAND0(ll); - while (temp && NODE_OPERAND1(temp)) temp = NODE_OPERAND1(temp); - if (temp && NODE_OPERAND0(temp)) - Op->val.I = NODE_CODE (NODE_OPERAND0(temp)); - else - Op->val.I = (long) NULL; - } - else - Op->val.I = (long) NULL; - *iptr += strlen("%L1L2*L1CODE"); - } else - { - Message (" *** Unknown operand in %IF (condition) for LL Node *** ",0); - } -} - -#ifdef __SPF_BUILT_IN_PARSER -void Get_Bif_Operand_temp(str, iptr, bif, Op) -#else -void Get_Bif_Operand (str, iptr, bif,Op) -#endif - char *str; - int *iptr; - PTR_BFND bif; - operand *Op; -{ - - Op->typ = UNDEF_TYP; - if (strncmp(&(str[*iptr]),"%ELSIFBLOB2", strlen("%ELSIFBLOB2"))== 0) - { - Op->typ = INTEGER_TYP; - *iptr += strlen("%ELSIFBLOB2"); - if (BIF_BLOB2(bif) && (BIF_CODE(BLOB_VALUE(BIF_BLOB2(bif))) == ELSEIF_NODE)) - Op->val.I = 1; - else - Op->val.I = 0; - } else - if (strncmp(&(str[*iptr]),"%LABEL", strlen("%LABEL"))== 0) - { - Op->typ = INTEGER_TYP; - *iptr += strlen("%LABEL"); - Op->val.I = (long) BIF_LABEL(bif); - } else - if (strncmp(&(str[*iptr]),"%CHECKFLAG", strlen("%CHECKFLAG"))== 0) - { - Op->typ = INTEGER_TYP; - *iptr += strlen("%CHECKFLAG"); - Op->val.I = Get_Flag_val(str, iptr); - } else - if (strncmp(&(str[*iptr]),"%BLOB1", strlen("%BLOB1"))== 0) - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_BLOB1(bif); - *iptr += strlen("%BLOB1"); - } else - if (strncmp(&(str[*iptr]),"%BLOB2", strlen("%BLOB2"))== 0) - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_BLOB2(bif); - *iptr += strlen("%BLOB2"); - } else - if (strncmp(&(str[*iptr]),"%BIFCP", strlen("%BIFCP"))== 0) - { - Op->typ = INTEGER_TYP; - if (BIF_CP(bif)) - Op->val.I = BIF_CODE(BIF_CP(bif)); - else - Op->val.I = 0; - *iptr += strlen("%BIFCP"); - } else - if (strncmp(&(str[*iptr]),"%VALINT", strlen("%VALINT"))== 0) - { - Op->typ = INTEGER_TYP; - Op->val.I = atoi(&(str[*iptr + strlen("%VALINT")])); /* %VALINT-12232323 space is necessary after the number*/ - /* skip to next statement */ - while (str[*iptr] != ' ') (*iptr)++; - } else - if (strncmp(&(str[*iptr]),"%RECURSBIT", strlen("%RECURSBIT"))== 0) /* %RECURSBIT : Symbol Attribut (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = RECURSIVE_BIT; - *iptr += strlen("%RECURSBIT"); - } else - if (strncmp(&(str[*iptr]),"%EXPR_LIST", strlen("%EXPR_LIST"))== 0) /* %EXPR_LIST : int constant EXPR_LIST code for Low Level Node (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = EXPR_LIST; - *iptr += strlen("%EXPR_LIST"); - } else - if (strncmp(&(str[*iptr]),"%SPEC_PAIR", strlen("%SPEC_PAIR"))== 0) /* %SPEC_PAIR : int constant SPEC_PAIR code for Low Level Node (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = SPEC_PAIR; - *iptr += strlen("%SPEC_PAIR"); - } else - if (strncmp(&(str[*iptr]),"%IOACCESS", strlen("%IOACCESS"))== 0) /* %IOACCESS : int constant IOACCESS code for Low Level Node (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = IOACCESS; - *iptr += strlen("%IOACCESS"); - } else - if (strncmp(&(str[*iptr]),"%STRCST", strlen("%STRCST"))== 0) /* %STRCST : String Constant */ - { - int i_save; - - *iptr += strlen("%STRCST"); - while (str[*iptr] == ' ') {(*iptr)++;} /* skip spaces before string */ - if (str[*iptr] != '\'') - { - Message (" *** Missing \"'\" after %STRCST *** ",0); - } - i_save = ++(*iptr); - while ((str[*iptr] != '\0') && (str[*iptr] != '\'')) (*iptr)++; - Op->val.S = alloc_str ((*iptr) - i_save); - strncpy (Op->val.S, &(str[i_save]), (*iptr) - i_save); - Op->typ = STRING_TYP; - (*iptr)++; /* skip the ' */ - } else - if (strncmp(&(str[*iptr]),"%SYMBOL", strlen("%SYMBOL"))== 0) /* %SYMBOL : Symbol pointer (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_SYMB (bif); - *iptr += strlen("%SYMBOL"); - } else - if (strncmp(&(str[*iptr]),"%SATTR", strlen("%SATTR"))== 0) /* %SATTR : Symbol Attribut (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (BIF_SYMB (bif))->attr; - *iptr += strlen("%SATTR"); - } else - if (strncmp(&(str[*iptr]),"%SYMBID", strlen("%SYMBID"))== 0) /* %SYMBID : Symbol identifier (string) */ - { - Op->typ = STRING_TYP; - if (BIF_SYMB (bif)) - Op->val.S = SYMB_IDENT (BIF_SYMB (bif)); - else - Op->val.S = NULL; - *iptr += strlen("%SYMBID"); - } else - if (strncmp(&(str[*iptr]),"%NULL", strlen("%NULL"))== 0) /* %NULL : Integer Constant (or false boolean) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = 0; - *iptr += strlen("%NULL"); - } else - if (strncmp(&(str[*iptr]),"%LL1", strlen("%LL1"))== 0) /* %LL1 : Low Level Node 1 (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_LL1 (bif); - *iptr += strlen("%LL1"); - } else - if (strncmp(&(str[*iptr]),"%LL2", strlen("%LL2"))== 0) /* %LL2 : Low Level Node 2 (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_LL2 (bif); - *iptr += strlen("%LL2"); - } else - if (strncmp(&(str[*iptr]),"%LL3", strlen("%LL3"))== 0) /* %LL3 : Low Level Node 3 (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_LL3 (bif); - *iptr += strlen("%LL3"); - } else - if (strncmp(&(str[*iptr]),"%LABUSE", strlen("%LABUSE"))== 0) /* %LABUSE : label ptr (used for do : doend) (integer) */ - { - Op->typ = INTEGER_TYP; - Op->val.I = (long) BIF_LABEL_USE (bif); - *iptr += strlen("%LABUSE"); - } else - if (strncmp(&(str[*iptr]),"%L1CODE", strlen("%L1CODE"))== 0) /* %L1CODE : Code (variant) of Low Level Node 1 (integer) */ - { - Op->typ = INTEGER_TYP; - if (BIF_LL1 (bif)) - Op->val.I = NODE_CODE (BIF_LL1 (bif)); - else - Op->val.I = (long) NULL; - *iptr += strlen("%L1CODE"); - } else - if (strncmp(&(str[*iptr]),"%L2CODE", strlen("%L2CODE"))== 0) /* %L2CODE : Code (variant) of Low Level Node 2 (integer) */ - { - Op->typ = INTEGER_TYP; - if (BIF_LL2 (bif)) - Op->val.I = NODE_CODE (BIF_LL2 (bif)); - else - Op->val.I = (long) NULL; - *iptr += strlen("%L2CODE"); - } else - if (strncmp(&(str[*iptr]),"%L1L2L1CODE", strlen("%L1L2L1CODE"))== 0) /* %L1L2L1CODE : Code (variant) of Low Level Node 1 of Low Level Node 2 of Low Level Node 1 (integer) */ - { - Op->typ = INTEGER_TYP; - if (BIF_LL1 (bif) && NODE_TEMPLATE_LL2 (BIF_LL1 (bif)) && NODE_TEMPLATE_LL1 (NODE_TEMPLATE_LL2 (BIF_LL1 (bif)))) - Op->val.I = NODE_CODE (NODE_TEMPLATE_LL1 (NODE_TEMPLATE_LL2 (BIF_LL1 (bif)))); - else - Op->val.I = (long) NULL; - *iptr += strlen("%L1L2L1CODE"); - } else - if (strncmp(&(str[*iptr]),"%L1L2*L1CODE", strlen("%L1L2*L1CODE"))== 0) /* %L1L2L1CODE : Code (variant) of Low Level Node 1 of Low Level Node 2 of Low Level Node 1 (integer) */ - { - PTR_LLND temp; - - Op->typ = INTEGER_TYP; - if (BIF_LL1 (bif) && NODE_TEMPLATE_LL2 (BIF_LL1 (bif)) && NODE_TEMPLATE_LL1 (NODE_TEMPLATE_LL2 (BIF_LL1 (bif)))) - { - temp = BIF_LL1 (bif); - while (NODE_OPERAND1(temp)) temp = NODE_OPERAND1(temp); - if (NODE_TEMPLATE_LL1 (temp)) - Op->val.I = NODE_CODE (NODE_TEMPLATE_LL1 (temp)); - else - Op->val.I = (long) NULL; - } - else - Op->val.I = (long) NULL; - *iptr += strlen("%L1L2*L1CODE"); - } else - if (strncmp(&(str[*iptr]),"%L2L1STR", strlen("%L2L1STR"))== 0) /* %L2L1STR : String (string_val) of Low Level Node 1 of Low Level Node 2 (string) */ - { - Op->typ = STRING_TYP; - if (BIF_LL2 (bif) && NODE_TEMPLATE_LL1 (BIF_LL2 (bif))) - Op->val.S = NODE_STR (NODE_TEMPLATE_LL1 (BIF_LL2 (bif))); - else - Op->val.S = NULL; - *iptr += strlen("%L2L1STR"); - } else - { - Message (" *** Unknown operand in %IF (condition) for Bif Node *** ",0); - } -} - -#ifdef __SPF_BUILT_IN_PARSER -int GetComp_temp(str, iptr) -#else -int GetComp (str, iptr) -#endif - char *str; - int *iptr; -{ - int Comp; - - if (strncmp(&(str[*iptr]),"==", strlen("==")) == 0) /* == : Equal */ - { - Comp = COMP_EQUAL; - *iptr += strlen("=="); - } else - if (strncmp(&(str[*iptr]),"!=", strlen("!=")) == 0) /* != : Different */ - { - Comp = COMP_DIFF; - *iptr += strlen("!="); - } else - { - Message (" *** Unknown comparison operator in %IF (condition) *** ",0); - Comp = COMP_UNDEF; - } - return Comp; -} - -#ifdef __SPF_BUILT_IN_PARSER -int Eval_Type_Condition_temp(str, ptype) -#else -int Eval_Type_Condition(str, ptype) -#endif - char *str; - PTR_TYPE ptype; -{ - int Result = 0; - int i = 0; - operand Op1, Op2; - int Comp; - - while (str[i] == ' ') {i++;} /* skip spaces before '(condition)' */ - if (str[i++] != '(') - { - Message (" *** Missing (condition) after %IF *** ",0); - - return 0; - } else - while (str[i] == ' ') {i++;} /* skip spaces before first operand */ - Get_Type_Operand(str, &i, ptype, &Op1); - while (str[i] == ' ') {i++;} /* skip spaces before the comparison operator */ - Comp = GetComp(str, &i); - while (str[i] == ' ') {i++;} /* skip spaces before second operand */ - Get_Type_Operand(str, &i, ptype, &Op2); - while (str[i] == ' ') {i++;} /* skip spaces before the closing round bracket */ - if (str[i] != ')') - { - Message (" *** Missing ')' after %IF (condition *** ",0); - return i; - } else - i++; - if ((Op1.typ != UNDEF_TYP) && (Op1.typ == Op2.typ) && (Comp !=COMP_UNDEF)) - { - switch (Op1.typ) - { - case STRING_TYP : Result = strcmp (Op1.val.S, Op2.val.S); - break; - case INTEGER_TYP : Result = Op1.val.I - Op2.val.I; - break; - } - if (Comp == COMP_EQUAL) Result = !Result; - if (Result) return i; /* continue from here to the corresponding %ELSE if exists */ - else /* continue at the corresponding %ELSE */ - { - int ifcount_local = 1; - while (str[i]) - { - while (str[i] != '%') { - if (str[i]) i++; - else return i; - } - i++; - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ - { - ifcount_local++; - i += strlen("IF"); - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ - { - ifcount_local--; - i += strlen("ENDIF"); - if (ifcount_local == 0) return i; - } else - if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* Counts %ELSE ; stop skipping if corresponding*/ - { - i += strlen("ELSE"); - if (ifcount_local == 1) return i; - } - } - return i; - } - } else - { - Message (" *** Error in condition for %IF command *** 1",0); - return i; - } -} - -#ifdef __SPF_BUILT_IN_PARSER -int Eval_LLND_Condition_temp(str, ll) -#else -int Eval_LLND_Condition(str, ll) -#endif - char *str; - PTR_LLND ll; -{ - int Result = 0; - int i = 0; - operand Op1, Op2; - int Comp = 0; - - while (str[i] == ' ') {i++;} /* skip spaces before '(condition)' */ - if (str[i++] != '(') - { - Message (" *** Missing (condition) after %IF *** ",0); - return 0; - } else - while (str[i] == ' ') {i++;} /* skip spaces before first operand */ - Get_LL_Operand(str, &i, ll, &Op1); - while (str[i] == ' ') {i++;} /* skip spaces before the comparison operator */ - Comp = GetComp(str, &i); - while (str[i] == ' ') {i++;} /* skip spaces before second operand */ - Get_LL_Operand(str, &i, ll, &Op2); - while (str[i] == ' ') {i++;} /* skip spaces before the closing round bracket */ - if (str[i] != ')') - { - Message (" *** Missing ')' after %IF (condition *** ",0); - i++; - return i; - } else - i++; - - if ((Op1.typ != UNDEF_TYP) && (Op1.typ == Op2.typ) && (Comp != COMP_UNDEF)) - { - switch (Op1.typ) - { - case STRING_TYP : Result = strcmp (Op1.val.S, Op2.val.S); - break; - case INTEGER_TYP : Result = Op1.val.I - Op2.val.I; - break; - } - if (Comp == COMP_EQUAL) Result = !Result; - if (Result) return i; /* continue from here to the corresponding %ELSE if exists */ - else /* continue at the corresponding %ELSE */ - { - int ifcount_local = 1; - while (str[i]) - { - while (str[i] != '%') { - if (str[i]) i++; - else return i; - } - i++; - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ - { - ifcount_local++; - i += strlen("IF"); - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ - { - ifcount_local--; - i += strlen("ENDIF"); - if (ifcount_local == 0) return i; - } else - if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* Counts %ELSE ; stop skipping if corresponding*/ - { - i += strlen("ELSE"); - if (ifcount_local == 1) return i; - } - } - return i; - } - } else - { - Message (" *** Error in condition for %IF command *** 2",0); - return i; - } -} - -#ifdef __SPF_BUILT_IN_PARSER -int Eval_Bif_Condition_temp(str, bif) -#else -int Eval_Bif_Condition(str, bif) -#endif - char *str; - PTR_BFND bif; -{ - int Result = 0; - int i = 0; - operand Op1, Op2; - int Comp; - - while (str[i] == ' ') {i++;} /* skip spaces before '(condition)' */ - if (str[i++] != '(') - { - Message (" *** Missing (condition) after %IF *** ",0); - return 0; - } else - while (str[i] == ' ') {i++;} /* skip spaces before first operand */ - Get_Bif_Operand(str, &i, bif, &Op1); - while (str[i] == ' ') {i++;} /* skip spaces before the comparison operator */ - Comp = GetComp(str, &i); - while (str[i] == ' ') {i++;} /* skip spaces before second operand */ - Get_Bif_Operand(str, &i, bif, &Op2); - while (str[i] == ' ') {i++;} /* skip spaces before the closing round bracket */ - - if (str[i] != ')') - { - Message (" *** Missing ')' after %IF (condition *** ",0); - return i; - } else - i++; - if ((Op1.typ != UNDEF_TYP) && (Op1.typ == Op2.typ) && (Comp != COMP_UNDEF)) - { - switch (Op1.typ) - { - case STRING_TYP : Result = strcmp (Op1.val.S, Op2.val.S); - break; - case INTEGER_TYP : Result = Op1.val.I - Op2.val.I; - break; - } - if (Comp == COMP_EQUAL) Result = !Result; - if (Result) return i; /* continue from here to the corresponding %ELSE if exists */ - else /* continue at the corresponding %ELSE */ - { - int ifcount_local = 1; - while (str[i]) - { - while (str[i] != '%') { - if (str[i]) i++; - else return i; - } - i++; - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ - { - ifcount_local++; - i += strlen("IF"); - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ - { - ifcount_local--; - i += strlen("ENDIF"); - if (ifcount_local == 0) return i; - } else - if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* Counts %ELSE ; stop skipping if corresponding*/ - { - i += strlen("ELSE"); - if (ifcount_local == 1) return i; - } - } - return i; - } - } else - { - Message (" *** Error in condition for %IF command *** 3",0); - return i; - } -} - -#ifdef __SPF_BUILT_IN_PARSER -int SkipToEndif_temp(str) -#else -int SkipToEndif (str) -#endif - char *str; -{ - int ifcount_local = 1; - int i = 0; - - while (str[i]) - { - while (str[i] != '%') { - if (str[i]) i++; - else return i; - } - i++; - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ - { - ifcount_local++; - i += strlen("IF"); - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ - { - ifcount_local--; - i += strlen("ENDIF"); - if (ifcount_local == 0) return i; - } - } - return i; -} - -char *Tool_Unparse2_LLnode (); - -#ifdef __SPF_BUILT_IN_PARSER -char* Tool_Unparse_Type_temp(ptype) -#else -char *Tool_Unparse_Type (ptype) -#endif - PTR_TYPE ptype; - /*int def;*/ /* def = 1 : defined type */ - /* def = 0 : named type */ -{ - int variant; - int kind; - char *str; - char c; - int i; - - if (!ptype) - return NULL; - - variant = TYPE_CODE (ptype); - kind = (int) node_code_kind [(int) variant]; - if (kind != (int)TYPENODE) - Message ("Error in Unparse, not a type node", 0); - - str = Unparse_Def [variant].str; - - /* now we have to interpret the code to unparse it */ - - if (str == NULL) - return NULL; - if (strcmp ( str, "n") == 0) - { - Message("Node not define for unparse",0); - return NULL; - } - - - i = 0 ; - c = str[i]; - while (c != '\0') - { - if (c == '%') - { - i++; - c = str[i]; - /******** WE HAVE TO INTERPRET THE COMMAND *********/ - if (c == '%') /* %% : Percent Sign */ - { - BufPutString ("%",0); - i++; - } else - if (strncmp(&(str[i]),"ERROR", strlen("ERROR"))== 0) /* %ERROR : Generate error message */ - { - Message("Error Node not defined",0); - BufPutInt(variant); - BufPutString ("-----TYPE ERROR--------",0); - i += strlen("ERROR"); - } else - if (strncmp(&(str[i]),"NL", strlen("NL"))== 0) /* %NL : NewLine */ - { - /*int j;*/ - BufPutChar ('\n'); -/* for (j = 0; j < TabNumber; j++) - if (j>1) - BufPutString (" ",0); - else - BufPutString (" ",0);*/ - i += strlen("NL"); - } else - if (strncmp(&(str[i]),"NOTABNL", strlen("NOTABNL"))== 0) /* %NL : NewLine */ - { - BufPutChar ('\n'); - i += strlen("NOTABNL"); - } else - if (strncmp(&(str[i]),"RIDPT", strlen("RIDPT"))== 0) - { - DealWith_Rid(ptype,In_Class_Flag); - i += strlen("RIDPT"); - } else - if (strncmp(&(str[i]),"TABNAME", strlen("TABNAME"))== 0) /* %TABNAME : Self Name from Table */ - { - if (Check_Lang_Fortran(cur_proj)) - BufPutString (ftype_name [type_index (TYPE_CODE (ptype))],0); - else - { - BufPutString (ctype_name [type_index (TYPE_CODE (ptype))],0); - } - i += strlen("TABNAME"); - } else - if (strncmp(&(str[i]),"TAB", strlen("TAB"))== 0) /* %TAB : Tab */ - { - BufPutString (" ",0); /* cychen */ - i += strlen("TAB"); - } else - if (strncmp(&(str[i]),"SETFLAG", strlen("SETFLAG"))== 0) - { - i = i + strlen("SETFLAG"); - Treat_Flag(str, &i,1); - } else - if (strncmp(&(str[i]),"UNSETFLAG", strlen("UNSETFLAG"))== 0) - { - i = i + strlen("UNSETFLAG"); - Treat_Flag(str, &i,-1); - } else - if (strncmp(&(str[i]),"PUSHFLAG", strlen("PUSHFLAG"))== 0) - { - i = i + strlen("PUSHFLAG"); - PushPop_Flag(str, &i,1); - } else - if (strncmp(&(str[i]),"POPFLAG", strlen("POPFLAG"))== 0) - { - i = i + strlen("POPFLAG"); - PushPop_Flag(str, &i,-1); - } else - if (strncmp(&(str[i]),"PUTTAB", strlen("PUTTAB"))== 0) /* %TAB : Tab */ - { - int j; - for (j = 0; j < TabNumber; j++) - if (j>0) - BufPutString (" ",0); - else - BufPutString (" ",0); /* cychen */ - i += strlen("PUTTAB"); - } else - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* %IF : If ; syntax : %IF (condition) then_bloc [%ELSE else_bloc] %ENDIF */ - { - i += strlen("IF"); - i += Eval_Type_Condition(&(str[i]), ptype); - } else - if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* %ELSE : Else */ - { - i += strlen("ELSE"); - i += SkipToEndif(&(str[i])); /* skip to the corresponding endif */ - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* %ENDIF : End of If */ - { - i += strlen("ENDIF"); - } else - if (strncmp(&(str[i]),"SUBTYPE", strlen("SUBTYPE"))== 0) /* %SUBTYPE : find the next type for (CAST) */ - { - PTR_TYPE pt; - pt = TYPE_BASE(ptype); - if(pt) Tool_Unparse_Type(pt); - i += strlen("SUBTYPE"); - } else - if (strncmp(&(str[i]),"BASETYPE", strlen("BASETYPE"))== 0) /* %BASETYPE : Base Type Name Identifier */ - { - if (Check_Lang_Fortran(cur_proj)) - BufPutString (ftype_name [type_index (TYPE_CODE (TYPE_BASE (ptype)))],0); - else - { - PTR_TYPE pt; - pt = Find_BaseType(ptype); - if (pt) - { - Tool_Unparse_Type(pt); - } else{ - /* printf("offeding node type node: %d\n", ptype->id); - Message("basetype not found",0); - */ - } - } - i += strlen("BASETYPE"); - } else - - if (strncmp(&(str[i]),"FBASETYPE", strlen("FBASETYPE"))== 0) /* %FBASETYPE : Base Type Name Identifier */ - { - PTR_TYPE pt; - pt = Find_BaseType2(ptype); - if (pt) - { - Tool_Unparse_Type(pt); - } else{ - /* printf("offeding node type node: %d\n", ptype->id); - Message("basetype not found",0); - */ - } - i += strlen("FBASETYPE"); - } else - - - if (strncmp(&(str[i]),"STAR", strlen("STAR"))== 0) - { - PTR_TYPE pt; - int flg; - pt = ptype; - /* while (pt) */ - { - if (TYPE_CODE(pt) == T_POINTER){ - BufPutString ("*",0); - flg = pt->entry.Template.dummy5; - if(flg & BIT_RESTRICT) BufPutString(" restrict ",0); - if(flg & BIT_CONST) BufPutString(" const ",0); - if(flg & BIT_GLOBL) BufPutString(" global ",0); - if(flg & BIT_SYNC) BufPutString(" Sync ",0); - if(flg & BIT_VOLATILE) BufPutString(" volatile ",0); - } - else - if (TYPE_CODE(pt) == T_REFERENCE){ - BufPutString ("&",0); - flg = pt->entry.Template.dummy5; - if(flg & BIT_RESTRICT) BufPutString(" restrict ",0); - if(flg & BIT_CONST) BufPutString(" const ",0); - if(flg & BIT_GLOBL) BufPutString(" global ",0); - if(flg & BIT_SYNC) BufPutString(" Sync ",0); - if(flg & BIT_VOLATILE) BufPutString(" volatile ",0); - } - /* else - break; - if(TYPE_CODE(pt) == T_MEMBER_POINTER) - pt = TYPE_COLL_BASE(pt); - else pt = TYPE_BASE(pt); */ - } - i += strlen("STAR"); - } else - if (strncmp(&(str[i]),"RANGES", strlen("RANGES"))== 0) /* %RANGES : Ranges */ - { - Tool_Unparse2_LLnode (TYPE_RANGES (ptype)); - i += strlen("RANGES"); - } else - if (strncmp(&(str[i]),"NAMEID", strlen("NAMEID"))== 0) /* %NAMEID : Name Identifier */ - { - if (ptype->name) - BufPutString ( ptype->name->ident,0); - else - { - BufPutString ("-------TYPE ERROR (NAMEID)------",0); - } - i += strlen("NAMEID"); - } else - if (strncmp(&(str[i]),"SYMBID", strlen("SYMBID"))== 0) /* %NAMEID : Name Identifier */ - { - if (TYPE_SYMB_DERIVE(ptype)){ - PTR_SYMB cname; - cname = TYPE_SYMB_DERIVE(ptype); - if(TYPE_CODE(ptype) == T_DERIVED_TYPE){ - if((SYMB_CODE(cname) == STRUCT_NAME) && (SYMB_TYPE(cname) == NULL) - &&(BIF_CODE(SYMB_SCOPE(cname)) == GLOBAL)) - BufPutString("struct ", 0); - if((SYMB_CODE(cname) == CLASS_NAME) && (SYMB_TYPE(cname) == NULL) - &&(BIF_CODE(SYMB_SCOPE(cname)) == GLOBAL)) - BufPutString("class ", 0); - if((SYMB_CODE(cname) == UNION_NAME) && (SYMB_TYPE(cname) == NULL) - &&(BIF_CODE(SYMB_SCOPE(cname)) == GLOBAL)) - BufPutString("union ", 0); - } - if(TYPE_SCOPE_SYMB_DERIVE(ptype) && TYPE_CODE(ptype) != T_DERIVED_TEMPLATE) { - Tool_Unparse_Symbol(TYPE_SCOPE_SYMB_DERIVE(ptype)); - BufPutString("::",0); - } - Tool_Unparse_Symbol(cname); - } - else if(TYPE_CODE(ptype) == T_MEMBER_POINTER) - Tool_Unparse_Symbol(TYPE_COLL_NAME(ptype)); - else - { - printf("node = %d, variant = %d\n",TYPE_ID(ptype), TYPE_CODE(ptype)); - BufPutString ("-------TYPE ERROR (ISYMBD)------",0); - } - i += strlen("SYMBID"); - } else - if (strncmp(&(str[i]),"RANGLL1", strlen("RANGLL1"))== 0) /* %RANGLL1 : Low Level Node 1 of Ranges */ - { - if (TYPE_RANGES (ptype)) - Tool_Unparse2_LLnode (NODE_TEMPLATE_LL1 (TYPE_RANGES (ptype))); - i += strlen("RANGLL1"); - } else - if (strncmp(&(str[i]),"COLLBASE", strlen("COLLBASE"))== 0) /* %COLL BASE */ - { - if (TYPE_COLL_BASE(ptype)) - Tool_Unparse_Type(TYPE_COLL_BASE(ptype)); - i += strlen("COLLBASE"); - } else - if (strncmp(&(str[i]),"TMPLARGS", strlen("TMPLARGS"))== 0) /* %RANGLL1 : Low Level Node 1 of Ranges */ - { - if (TYPE_TEMPL_ARGS(ptype)) - Tool_Unparse2_LLnode(TYPE_TEMPL_ARGS(ptype)); - i += strlen("TMPLARGS"); - } else - Message (" *** Unknown type node COMMAND *** ",0); - } - else - { - BufPutChar (c); - i++; - } - c = str[i]; - } - return Buf_address; -} - -#ifdef __SPF_BUILT_IN_PARSER -char* Tool_Unparse2_LLnode_temp(ll) -#else -char *Tool_Unparse2_LLnode(ll) -#endif - PTR_LLND ll; -{ - int variant; - int kind; - char *str; - char c; - int i; - - if (!ll) - return NULL; - - variant = NODE_CODE (ll); - /*BufPutInt(variant); - printf("LLNODE : %i\n%s",variant,Buf_address);*/ - if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&& - ((variant==ARRAY_REF)|| - (variant==VAR_REF)|| - (variant==CONST_REF))&& - ON_BLOCK&&NODE_SYMB(ll)) - { - PTR_LLND ptr,new_node,new_symb; - if (!SYMB_DOVAR(NODE_SYMB(ll))&& - (!(SYMB_ATTR(NODE_SYMB(ll))&TASK_BIT))&& - (!(SYMB_ATTR(NODE_SYMB(ll))&PROCESSORS_BIT))) - { - new_symb=make_llnode(variant,LLNULL,LLNULL,NODE_SYMB(ll)); - new_node=make_llnode(EXPR_LIST,new_symb,LLNULL,SMNULL); - if (parameter_list) - { - ptr=Follow_Llnd(parameter_list,2); - NODE_OPERAND1(ptr)=new_node; - } - if (!parameter_list) parameter_list=new_node; - SYMB_DOVAR(NODE_SYMB(ll))=2; - } - } - kind = (int) node_code_kind[(int) variant]; - if (kind != (int)LLNODE) - { - Message("Error in Unparse, not a llnd node",0); - BufPutInt(variant); - BufPutString ("------ERROR--------",0); - return NULL; - } - str = Unparse_Def[variant].str; - /* now we have to interpret the code to unparse it */ - - if (str == NULL) - return NULL; - if (strcmp( str, "n") == 0) - return NULL; - - i = 0 ; - c = str[i]; - while (c != '\0') - { - if (c == '%') - { - i++; - c = str[i]; - /******** WE HAVE TO INTERPRET THE COMMAND *********/ - if (c == '%') /* %% : Percent Sign */ - { - BufPutString ("%",0); - i++; - } else - if (strncmp(&(str[i]),"ERROR", strlen("ERROR"))== 0) /* %ERROR : Generate error message */ - { - Message ("--- unparsing error[0] : ",0); - BufPutInt(variant); - BufPutString ("------ERROR--------",0); - i += strlen("ERROR"); - } else - if (strncmp(&(str[i]),"NL", strlen("NL"))== 0) /* %NL : NewLine */ - { - /* int j;*/ - BufPutChar ('\n'); -/* for (j = 0; j < TabNumber; j++) - if (j>1) - BufPutString (" ",0); - else - BufPutString (" ",0);*/ - i += strlen("NL"); - } else - if (strncmp(&(str[i]),"TAB", strlen("TAB"))== 0) /* %TAB : Tab */ - { - BufPutString (" ",0); /* cychen */ - i += strlen("TAB"); - } else - if (strncmp(&(str[i]),"BACK", strlen("BACK"))== 0) - { - Buf_pointer--; - i += strlen("BACK"); - } else - - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* %IF : If ; syntax : %IF (condition) then_bloc [%ELSE else_bloc] %ENDIF */ - { - i += strlen("IF"); - i += Eval_LLND_Condition(&(str[i]), ll); - } else - if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* %ELSE : Else */ - { - i += strlen("ELSE"); - i += SkipToEndif(&(str[i])); /* skip to the corresponding endif */ - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* %ENDIF : End of If */ - { - i += strlen("ENDIF"); - } else - if (strncmp(&(str[i]),"LL1", strlen("LL1"))== 0) /* %LL1 : Low Level Node 1 */ - { - Tool_Unparse2_LLnode(NODE_TEMPLATE_LL1(ll)); - i += strlen("LL1"); - } else - if (strncmp(&(str[i]),"LL2", strlen("LL2"))== 0) /* %LL2 : Low Level Node 2 */ - { - Tool_Unparse2_LLnode(NODE_TEMPLATE_LL2(ll)); - i += strlen("LL2"); - } else - if (strncmp(&(str[i]),"SYMBID", strlen("SYMBID"))== 0) /* %SYMBID : Symbol identifier */ - { - Tool_Unparse_Symbol (NODE_SYMB (ll)); - i += strlen("SYMBID"); - } else - if (strncmp(&(str[i]),"NEWSPEC", strlen("NEWSPEC"))== 0) - { - if (NewSpecList) - { -#ifdef __SPF - removeFromCollection(NewSpecList); -#endif - free(NewSpecList); - } - NewSpecList=NODE_OPERAND0(ll); - i += strlen("NEWSPEC"); - } else - if (strncmp(&(str[i]),"COMMONDECL", strlen("COMMONDECL"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ - { - int count=0; - if ((NODE_CODE(ll)==COMM_LIST)&&NODE_OPERAND0(ll)) count=FindCommonHeapDeclaration(NODE_OPERAND0(ll)); - if (!count) - { - count=0; - Treat_Flag("(POINTER)",&count, 1); - } - i += strlen("COMMONDECL"); - } else - if (strncmp(&(str[i]),"CHECK_HEAP", strlen("CHECK_HEAP"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ - { - /* char *str; - if (ll) str=SYMB_IDENT(NODE_SYMB (ll)); - if (!strcmp(str,"heap")||!strcmp(str,"HEAP")) */ - if(SYMB_ATTR(NODE_SYMB (ll))&HEAP_BIT) - { - /*BufPutString("",0);*/ - Tool_Unparse2_LLnode (NODE_OPERAND0(ll)); - return Buf_address; - } - i += strlen("CHECK_HEAP"); - } else - if (strncmp(&(str[i]),"CHECK_PTR", strlen("CHECK_PTR"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ - { - if (ll) - if(SYMB_ATTR(NODE_SYMB (ll))&DVM_POINTER_ARRAY_BIT) - BufPutString("%PTR",0); - i += strlen("CHECK_PTR"); - } else - if (strncmp(&(str[i]),"CHECK_PROC_REF", strlen("CHECK_PROC_REF"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ - { - int k=0; - if (ll) - if(SYMB_ATTR(NODE_SYMB (ll))&PROCESSORS_BIT) - if (!Get_Flag_val("(PROC_REF)",&k)) - return Buf_address; - i += strlen("CHECK_PROC_REF"); - } else - if (strncmp(&(str[i]),"DELETE_COMMA", strlen("DELETE_COMMA"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ - { - if (Buf_address[Buf_pointer-1]==',') - Buf_pointer--; - i += strlen("DELETE_COMMA"); - } else - if (strncmp(&(str[i]),"CHECK_DYNAMIC", strlen("CHECK_DYNAMIC"))== 0) - { - if (NODE_OPERAND0(ll)||NODE_OPERAND1(ll)) - i += strlen("CHECK_DYNAMIC"); - else { - int k=0; - if (Get_Flag_val("(DYNAMIC)",&k)) - { - Buf_pointer--; - return Buf_address; - } - k=0; - Treat_Flag("(DYNAMIC)",&k,1); - BufPutString("DYNAMIC",0); - i += strlen("CHECK_DYNAMIC"); - return Buf_address; - } - } else - - if (strncmp(&(str[i]),"SAVE_SYMBOL", strlen("SAVE_SYMBOL"))== 0) /* %SAVE_SYMBOL : Reduction variables list */ - { - if (Find_SaveSymbol (ll)) - { - if (Buf_address[Buf_pointer-1]==',') Buf_pointer--; - return Buf_address; - } - i += strlen("SAVE_SYMBOL"); - } else - if (strncmp(&(str[i]),"DELETE_SYMBOL", strlen("DELETE_SYMBOL"))== 0) /* %DELETE_SYMBOL : Clear reduction variables list */ - { - Number_Of_Symbol=0; - i += strlen("DELETE_SYMBOL"); - } else - if (strncmp(&(str[i]),"POINTER_NAME", strlen("POINTER_NAME"))== 0) /* %POINTER_NAME : We should change the definition of Pointer descriptor */ - { - if ((NODE_CODE(ll)==VAR_REF)||(NODE_CODE(ll)==ARRAY_REF)) - if(SYMB_ATTR(NODE_SYMB (ll))&DVM_POINTER_BIT) - { - Tool_Unparse_Symbol (NODE_SYMB (ll)); - if (NODE_CODE(ll)==ARRAY_REF) - { - if(NODE_OPERAND0(ll)) - { - BufPutChar('('); - Tool_Unparse2_LLnode (NODE_OPERAND0(ll)); - BufPutChar(')'); - } - } - return Buf_address; - } - i += strlen("POINTER_NAME"); - } else - if (strncmp(&(str[i]),"DOPROC", strlen("DOPROC"))== 0) /* for subclass qualification */ - { int flg; - if(NODE_TYPE(ll) && (NODE_CODE(NODE_TYPE(ll)) == T_DESCRIPT)){ - flg = (NODE_TYPE(ll))->entry.Template.dummy5; - if(flg & BIT_VIRTUAL) BufPutString(" virtual ",0); - if(flg & BIT_ATOMIC) BufPutString(" atomic ",0); - if(flg & BIT_PRIVATE) BufPutString(" private ",0); - if(flg & BIT_PROTECTED) BufPutString(" protected ",0); - if(flg & BIT_PUBLIC) BufPutString(" public ",0); - } - else BufPutString(" public ", 0); - /* note: this last else condition is to fix a bug in - the dep2C++ which does not create the right types - when converting a collection to a class. - */ - i += strlen("DOPROC"); - } else - if (strncmp(&(str[i]),"TYPE", strlen("TYPE"))== 0) /* %TYPE : Type */ - { - if(NODE_SYMB(ll) && (SYMB_ATTR(NODE_SYMB(ll)) & OVOPERATOR)){ - /* this is an overloaded operator. don't do type */ - } - else{ Tool_Unparse_Type (NODE_TYPE (ll)); } - i += strlen("TYPE"); - } else - if (strncmp(&(str[i]),"L1SYMBCST", strlen("L1SYMBCST"))== 0) /* %L1SYMBCST : Constant Value of Low Level Node Symbol */ - { - if (NODE_TEMPLATE_LL1 (ll) && NODE_SYMB (NODE_TEMPLATE_LL1 (ll))) - { - Tool_Unparse2_LLnode((NODE_SYMB (NODE_TEMPLATE_LL1 (ll)))->entry.const_value); - } - i += strlen("L1SYMBCST"); - } else - if (strncmp(&(str[i]),"INTVAL", strlen("INTVAL"))== 0) /* %INTVAL : Integer Value */ - { - if (NODE_INT_CST_LOW (ll) >= 0) - { - BufPutInt (NODE_INT_CST_LOW (ll)); - } else - { - BufPutString ("(",0); - BufPutInt (NODE_INT_CST_LOW (ll)); - BufPutString (")",0); - } - i += strlen("INTVAL"); - } else - if (strncmp(&(str[i]),"STATENO", strlen("STATENO"))== 0) /* %STATENO : Statement number */ - { - if (NODE_LABEL (ll)) - { - BufPutInt ((int)( LABEL_STMTNO (NODE_LABEL (ll)))); - } - i += strlen("STATENO"); - } else - if (strncmp(&(str[i]),"STRVAL", strlen("STRVAL"))== 0) /* %STRVAL : String Value */ - { - BufPutString (NODE_STR (ll),0); - i += strlen("STRVAL"); - } else - if (strncmp(&(str[i]),"BOOLVAL", strlen("BOOLVAL"))== 0) /* %BOOLVAL : String Value */ - { - BufPutString (NODE_BV (ll) ? ".TRUE." : ".FALSE.",0); - i += strlen("BOOLVAL"); - } else - if (strncmp(&(str[i]),"CHARVAL", strlen("CHARVAL"))== 0) /* %CHARVAL : Char Value */ - { - switch(NODE_CV(ll)){ - case '\n':BufPutChar('\\'); BufPutChar('n'); break; - case '\t':BufPutChar('\\'); BufPutChar('t'); break; - case '\r':BufPutChar('\\'); BufPutChar('r'); break; - case '\f':BufPutChar('\\'); BufPutChar('f'); break; - case '\b':BufPutChar('\\'); BufPutChar('b'); break; - case '\a':BufPutChar('\\'); BufPutChar('a'); break; - case '\v':BufPutChar('\\'); BufPutChar('v'); break; - default: - BufPutChar (NODE_CV (ll)); - } - i += strlen("CHARVAL"); - } else - if (strncmp(&(str[i]),"ORBPL1", strlen("ORBPL1"))== 0) /* %ORBPL1 : Openning Round Brackets on Precedence of Low Level Node 1 */ - { - int llvar = NODE_CODE (NODE_TEMPLATE_LL1 (ll)); - if (binop (llvar) && (precedence [variant - EQ_OP] < precedence [llvar - EQ_OP])) - BufPutString ("(",0); - i += strlen("ORBPL1"); - } else - if (strncmp(&(str[i]),"CRBPL1", strlen("CRBPL1"))== 0) /* %CRBPL1 : Closing Round Brackets on Precedence of Low Level Node 1 */ - { - int llvar = NODE_CODE (NODE_TEMPLATE_LL1 (ll)); - if (binop (llvar) && (precedence [variant - EQ_OP] < precedence [llvar - EQ_OP])) - BufPutString (")",0); - i += strlen("CRBPL1"); - } else - if (strncmp(&(str[i]),"ORBPL2", strlen("ORBPL2"))== 0) /* %ORBPL2 : Openning Round Brackets on Precedence of Low Level Node 2 */ - { - int llvar = NODE_CODE (NODE_TEMPLATE_LL2 (ll)); - if (binop (llvar) && (precedence [variant - EQ_OP] <= precedence [llvar - EQ_OP])) - BufPutString ("(",0); - i += strlen("ORBPL2"); - } else - if (strncmp(&(str[i]),"CRBPL2", strlen("CRBPL2"))== 0) /* %CRBPL2 : Closing Round Brackets on Precedence of Low Level Node 2 */ - { - int llvar = NODE_CODE (NODE_TEMPLATE_LL2 (ll)); - if (binop (llvar) && (precedence [variant - EQ_OP] <= precedence [llvar - EQ_OP])) - BufPutString (")",0); - i += strlen("CRBPL2"); - } else - if (strncmp(&(str[i]),"SETFLAG", strlen("SETFLAG"))== 0) - { - i = i + strlen("SETFLAG"); - Treat_Flag(str, &i,1); - } else - if (strncmp(&(str[i]),"UNSETFLAG", strlen("UNSETFLAG"))== 0) - { - i = i + strlen("UNSETFLAG"); - Treat_Flag(str, &i,-1); - } else - if (strncmp(&(str[i]),"PUSHFLAG", strlen("PUSHFLAG"))== 0) - { - i = i + strlen("PUSHFLAG"); - PushPop_Flag(str, &i,1); - } else - if (strncmp(&(str[i]),"POPFLAG", strlen("POPFLAG"))== 0) - { - i = i + strlen("POPFLAG"); - PushPop_Flag(str, &i,-1); - } else - if (strncmp(&(str[i]),"PURE", strlen("PURE"))== 0) /* for pure function declarations */ - { - PTR_SYMB symb; - symb = NODE_SYMB(ll); - if(symb && (SYMB_TEMPLATE_DUMMY8(symb) & 128)) BufPutString ("= 0",0); - i += strlen("PURE"); - } - else - if (strncmp(&(str[i]),"CNSTF", strlen("CNSTF"))== 0) /* for const functions */ - { - PTR_SYMB symb; - if (NODE_SYMB (ll)){ - symb = BIF_SYMB (ll); - if(SYMB_TEMPLATE_DUMMY8(symb) & 64) BufPutString(" const",0); - } - i += strlen("CNSTF"); - } else - if (strncmp(&(str[i]),"CNSTCHK", strlen("CNSTCHK"))== 0) /* do "const", vol" after * */ - { - int flg; - PTR_TYPE t; - if((t = NODE_TYPE(ll)) &&( (NODE_CODE(t) == T_POINTER) || - (NODE_CODE(t) == T_REFERENCE))){ - flg = t->entry.Template.dummy5; - if(flg & BIT_RESTRICT) BufPutString(" restrict ",0); - if(flg & BIT_CONST) BufPutString(" const ",0); - if(flg & BIT_GLOBL) BufPutString(" global ",0); - if(flg & BIT_SYNC) BufPutString(" Sync ",0); - if(flg & BIT_VOLATILE) BufPutString(" volatile ",0); - } - i += strlen("CNSTCHK"); - } - else - if (strncmp(&(str[i]),"VARLISTTY", strlen("VARLISTTY"))== 0) /* %VARLIST : list of variables / parameters */ - { - PTR_SYMB symb, s; - PTR_LLND args, arg_item = NULL, t; - PTR_TYPE typ; - int new_op_flag; /* 1 if this is a new op */ - new_op_flag = 0; - if(NODE_CODE(ll) == CAST_OP ){ - args = NODE_OPERAND1(ll); - new_op_flag = 1; - } - else if(NODE_CODE(ll) != FUNCTION_OP){ - args = NODE_OPERAND0(ll); - /* symb = SYMB_FUNC_PARAM(NODE_SYMB(ll)); */ - } - else { /* this is a pointer to a function parameter */ - args = NODE_OPERAND1(ll); - t = NODE_OPERAND0(ll); /* node_code(t) == deref_op */ - t = NODE_OPERAND0(t); /* node_code(t) == var_ref */ - s = NODE_SYMB(t); - if(s) symb = SYMB_NEXT(s); - else symb = NULL; - } - while (args ) - { - int typflag; - if(new_op_flag) t = args; - else{ - arg_item = NODE_OPERAND0(args); - t = arg_item; - typflag = 1; - while(t && typflag){ - if((NODE_CODE(t) == VAR_REF) || (NODE_CODE(t) == ARRAY_REF)) - typflag = 0; - else if (NODE_CODE(t) == SCOPE_OP) t = NODE_OPERAND1(t); - else t = NODE_OPERAND0(t); - } - } - if(t){ - symb = NODE_SYMB(t); - typ = NODE_TYPE(t); - if(symb && (typ == NULL)) typ = SYMB_TYPE(symb); - if(new_op_flag || symb ) { - typflag = 1; - while(typ && typflag){ - if(TYPE_CODE(typ) == T_ARRAY || - TYPE_CODE(typ) == T_FUNCTION || - TYPE_CODE(typ) == T_REFERENCE || - TYPE_CODE(typ) == T_POINTER) typ = TYPE_BASE(typ); - else if(TYPE_CODE(typ) == T_MEMBER_POINTER) - typ = TYPE_COLL_BASE(typ); - else typflag = 0; - } - } - if(typ) Tool_Unparse_Type (typ); - BufPutString (" ",0); - } - else printf("unp could not find var ref!\n"); - if(new_op_flag){ - Tool_Unparse2_LLnode(args); - args = LLNULL; - new_op_flag = 0; - } - else{ - Tool_Unparse2_LLnode(arg_item); - args = NODE_OPERAND1(args); - } - if (args) BufPutString (", ",0); - } - i += strlen("VARLISTTY"); - } - else - if (strncmp(&(str[i]),"VARLIST", strlen("VARLIST"))== 0) /* %VARLIST : list of variables / parameters */ - { - PTR_SYMB symb; - if (NODE_SYMB (ll)) - symb = SYMB_FUNC_PARAM (NODE_SYMB (ll)); - else - symb = NULL; - while (symb) - { - BufPutString ( SYMB_IDENT (symb),0); - symb = SYMB_NEXT_DECL (symb); - if (symb) BufPutString (", ",0); - } - i += strlen("VARLIST"); - } else - if (strncmp(&(str[i]),"STRINGLEN", strlen("STRINGLEN"))== 0) - { - PTR_SYMB symb; - PTR_TYPE type; - if (NODE_SYMB (ll)) - symb = NODE_SYMB (ll); - else - symb = NULL; - if (symb) - { - type = SYMB_TYPE(symb); - if (type && (TYPE_CODE(type) == T_ARRAY)) - { - type = Find_BaseType(type); - } - if (type && (TYPE_CODE(type) == T_STRING)) - { - if (TYPE_RANGES(type)) - Tool_Unparse2_LLnode(TYPE_RANGES(type)); - } - } - i += strlen("STRINGLEN"); - } else - if (strncmp(&(str[i]),"CHPFCONTTAB", strlen("CHPFCONTTAB"))== 0) /* %TAB : Tab */ - { - int j; - BufPutString ("\n!HPF$*",0); - for (j = 0; j < TabNumber; j++) - if (j>0) - BufPutString (" ",0); - else - BufPutString (" ",0); - Buf_pointer-=7; - i += strlen("CHPFCONTTAB"); - } else - Message (" *** Unknown low level node COMMAND *** ",0); - } - else - { - BufPutChar ( c); - i++; /* Bodin */ - } - c = str[i]; - } - return Buf_address; -} - -#ifdef __SPF_BUILT_IN_PARSER -char* Tool_Unparse_Bif_temp(bif) -#else -char *Tool_Unparse_Bif(bif) -#endif - PTR_BFND bif; -{ - int variant; - int kind; - char *str; - char c; - int i; - int AfterDOLabel=0; - - if (!bif) - return NULL; - - variant = BIF_CODE(bif); - /*BufPutInt(variant); - printf("BIFNODE : %i\n%s",variant,Buf_address);*/ - - /*BufPutInt(isForNodeEndStmt(bif)); - BufPutInt(variant);*/ - kind = (int) node_code_kind[(int) variant]; - if (kind != (int)BIFNODE) - Message("Error in Unparse, not a bif node",0); - str = Unparse_Def[variant].str; - if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&TaskRegionUnparse) - { if (!BIF_ID(bif)) return Buf_address; - else BIF_ID(bif)=0; - } - if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&(variant==CONTROL_END)) - if (NODE_CODE(BIF_CP(bif))==PROG_HEDR) - if (TaskRegion) - { - if (TabNumber>1) TabNumber--; - Puttab(); - BufPutString("end \n\n",0); - UnparseTaskRegion(TaskRegion); - return Buf_address; - } - if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&(variant==DVM_ON_DIR)) - { - char *name; - int i,temp=Buf_pointer; - PTR_FCALL fcall,new_node; - Tool_Unparse2_LLnode(BIF_LL1(bif)); - name=xmalloc(Buf_pointer-temp); - for(i=0;i+temp0) - { - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("INDEPENDENT\n",0); - } - NumberOfIndependent--; - i = i + strlen("CHECK_INDEPENDENT_DO"); - } else - if (strncmp(&(str[i]),"RESET_INDEPENDENT_DO", strlen("RESET_INDEPENDENT_DO"))== 0) - { - NumberOfIndependent=0; - i = i + strlen("RESET_INDEPENDENT_DO"); - } else - if (strncmp(&(str[i]),"UNSETFLAG", strlen("UNSETFLAG"))== 0) - { - i = i + strlen("UNSETFLAG"); - Treat_Flag(str, &i,-1); - } else - if (strncmp(&(str[i]),"PUSHFLAG", strlen("PUSHFLAG"))== 0) - { - i = i + strlen("PUSHFLAG"); - PushPop_Flag(str, &i,1); - } else - if (strncmp(&(str[i]),"POPFLAG", strlen("POPFLAG"))== 0) - { - i = i + strlen("POPFLAG"); - PushPop_Flag(str, &i,-1); - } else - if (strncmp(&(str[i]),"ERROR", strlen("ERROR"))== 0) /* %ERROR : Generate error message */ - { - Message("--- stmt unparsing error[1] : ",0); - i += strlen("ERROR"); - BufPutString (" *** UNPARSING ERROR OCCURRED HERE ***\n",0); - } else - if (strncmp(&(str[i]),"NL", strlen("NL"))== 0) /* %NL : NewLine */ - { - int j=0; - if (!Get_Flag_val("(NO_NL)",&j)) - BufPutChar ('\n'); -/* for (j = 0; j < TabNumber; j++) - if (j>1) - BufPutString (" ",0); - else - BufPutString (" ",0);*/ - i += strlen("NL"); - } else - if (strncmp(&(str[i]),"NOTABNL", strlen("NOTABNL"))== 0) /* %NL : NewLine */ - { - BufPutChar ('\n'); - i += strlen("NOTABNL"); - } else - if (strncmp(&(str[i]),"TAB", strlen("TAB"))== 0) /* %TAB : Tab */ - { - BufPutString (" ",0); /* cychen */ - i += strlen("TAB"); - } else - if (strncmp(&(str[i]),"PUTTAB", strlen("PUTTAB"))== 0) /* %TAB : Tab */ - { - int j; - int k=0; - if (!Get_Flag_val("(NO_NL)",&k)) - for (j = 0; j < TabNumber; j++) - if (j>0) - BufPutString (" ",0); - else - BufPutString (" ",0); /* cychen */ - i += strlen("PUTTAB"); - } else - if (strncmp(&(str[i]),"CHPFTAB", strlen("CHPFTAB"))== 0) /* %TAB : Tab */ - { - int j; - BufPutString ("!HPF$",0); - for (j = 0; j < TabNumber; j++) - if (j>0) - BufPutString (" ",0); - else - BufPutString (" ",0); - Buf_pointer-=6; - i += strlen("CHPFTAB"); - } else - if (strncmp(&(str[i]),"GETRED", strlen("GETRED"))== 0) /* %TAB : Tab */ - { - if (FindRedInExpr(BIF_LL1(bif),BIF_LL2 (bif))) - { - if (BIF_LL1(bif)&&NODE_SYMB(BIF_LL1(bif))&&(!IS_DISTRIBUTE_ARRAY(NODE_SYMB(BIF_LL1(bif))))&&(!SYMB_DOVAR(NODE_SYMB(BIF_LL1(bif))))) - { - BufPutString("Reduction var :",0); - Tool_Unparse2_LLnode(BIF_LL1(bif)); - BufPutString("\n",0); - if (!FindInNewList(NewSpecList,BIF_LL1(bif))) - if(!FindInNewList(ReductionList,BIF_LL1(bif))) - { - ReductionList=AddToReductionList(ReductionList,BIF_LL1(bif)); - BufPutString("Reduction LIST :",0); - Tool_Unparse2_LLnode(ReductionList); - BufPutString("\n",0); - } - } - } - i += strlen("GETRED"); - } else - if (strncmp(&(str[i]),"INSERT_REDUCTION", strlen("INSERT_REDUCTION"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ - { - if (ReductionList) - { - BufPutString("MY_REDUCTION (",0); - Tool_Unparse2_LLnode(ReductionList); - BufPutString(")",0); - ReductionList=FreeReductionList(ReductionList); - /*if (NewSpecList) free(NewSpecList);*/ - } - i += strlen("INSERT_REDUCTION"); - } else - if (strncmp(&(str[i]),"CHPFCONTTAB", strlen("CHPFCONTTAB"))== 0) /* %TAB : Tab */ - { - int j; - BufPutString ("\n!HPF$*",0); - for (j = 0; j < TabNumber; j++) - if (j>0) - BufPutString (" ",0); - else - BufPutString (" ",0); - Buf_pointer-=7; - i += strlen("CHPFCONTTAB"); - } else - if (strncmp(&(str[i]),"INCTAB", strlen("INCTAB"))== 0) /* increment tab */ - { - TabNumber++; - i += strlen("INCTAB"); - } else - if (strncmp(&(str[i]),"DECTAB", strlen("DECTAB"))== 0) /*deccrement tab */ - { - if (Check_Lang_Fortran(cur_proj)) - { - if (TabNumber>1) - TabNumber--; - } else - TabNumber--; - i += strlen("DECTAB"); - } else - if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* %IF : If ; syntax : %IF (condition) then_bloc [%ELSE else_bloc] %ENDIF */ - { - i += strlen("IF"); - i += Eval_Bif_Condition(&(str[i]), bif); - } else - if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* %ELSE : Else */ - { - i += strlen("ELSE"); - i += SkipToEndif(&(str[i])); /* skip to the corresponding endif */ - } else - if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* %ENDIF : End of If */ - { - i += strlen("ENDIF"); - } else - if (strncmp(&(str[i]),"BLOB1", strlen("BLOB1"))== 0) /* %BLOB1 : All Blob 1 */ - { - PTR_BLOB blob; - for (blob = BIF_BLOB1(bif);blob; blob = BLOB_NEXT (blob)) - { - Tool_Unparse_Bif(BLOB_VALUE(blob)); - } - i += strlen("BLOB1"); - } else - if (strncmp(&(str[i]),"BLOB2", strlen("BLOB2"))== 0) /* %BLOB2 : All Blob 2 */ - { - PTR_BLOB blob; - - for (blob = BIF_BLOB2(bif);blob; blob = BLOB_NEXT (blob)) - { - Tool_Unparse_Bif(BLOB_VALUE(blob)); - } - i += strlen("BLOB2"); - } else - if (strncmp(&(str[i]),"LL1", strlen("LL1"))== 0) /* %LL1 : Low Level Node 1 */ - { - Tool_Unparse2_LLnode(BIF_LL1(bif)); - i += strlen("LL1"); - } else - if (strncmp(&(str[i]),"LL2", strlen("LL2"))== 0) /* %LL2 : Low Level Node 2 */ - { - Tool_Unparse2_LLnode (BIF_LL2 (bif)); - i += strlen("LL2"); - } else - if (strncmp(&(str[i]),"LL3", strlen("LL3"))== 0) /* %LL3 : Low Level Node 3 */ - { - Tool_Unparse2_LLnode(BIF_LL3(bif)); - i += strlen("LL3"); - } else - if (strncmp(&(str[i]),"L2L2", strlen("L2L2"))== 0) /* %L2L2 : Low Level Node 2 of Low Level Node 2 */ - { - if (BIF_LL2 (bif)) - Tool_Unparse2_LLnode (NODE_TEMPLATE_LL2 (BIF_LL2 (bif))); - i += strlen("L2L2"); - } else - if (strncmp(&(str[i]),"FUNHD", strlen("FUNHD"))== 0) /* %FUNHD track down a function header */ - { - PTR_LLND p; - p = BIF_LL1(bif); - while(p && NODE_CODE(p) != FUNCTION_REF) p = NODE_OPERAND0(p); - if(p == NULL) printf("unparse error in FUNHD!!\n"); - else Tool_Unparse2_LLnode(p); - i += strlen("FUNHD"); - } else - if (strncmp(&(str[i]),"SYMBIDFUL", strlen("SYMBIDFUL"))== 0) /* %SYMBID : Symbol identifier */ - { - if (BIF_SYMB(bif) && SYMB_MEMBER_BASENAME(BIF_SYMB(bif))) - { - Tool_Unparse_Symbol(SYMB_MEMBER_BASENAME(BIF_SYMB(bif))); - BufPutString("::",0); - } - Tool_Unparse_Symbol(BIF_SYMB(bif)); - i += strlen("SYMBIDFUL"); - } else - if (strncmp(&(str[i]),"SYMBID", strlen("SYMBID"))== 0) /* %SYMBID : Symbol identifier */ - { - Tool_Unparse_Symbol(BIF_SYMB(bif)); - i += strlen("SYMBID"); - } else - if (strncmp(&(str[i]),"SYMBSCOPE", strlen("SYMBSCOPE"))== 0) /* %SYMBSCOPE : Symbol identifier */ - { - if (BIF_SYMB(bif) && SYMB_MEMBER_BASENAME(BIF_SYMB(bif))) - { - Tool_Unparse_Symbol(SYMB_MEMBER_BASENAME(BIF_SYMB(bif))); - } - i += strlen("SYMBSCOPE"); - } else - if (strncmp(&(str[i]),"SYMBDC", strlen("SYMBDC"))== 0) /* %SYMBSCOPE : Symbol identifier */ - { - if (BIF_LL3(bif) || - (BIF_SYMB(bif) && SYMB_MEMBER_BASENAME(BIF_SYMB(bif)))) - { - BufPutString("::",0); - } - i += strlen("SYMBDC"); - } else - - if (strncmp(&(str[i]),"STATENO", strlen("STATENO"))== 0) /* %STATENO : Statement number */ - { - if (BIF_LABEL_USE (bif)) - { - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL_USE (bif)))); - } - i += strlen("STATENO"); - } else - if (strncmp(&(str[i]),"LABELENDIF", strlen("LABELENDIF"))== 0) /* %STATENO : Statement number */ - { - PTR_BFND temp; - PTR_BLOB blob; - - temp = NULL; - if (!BIF_BLOB2(bif)) - blob = BIF_BLOB1(bif); - else - blob = BIF_BLOB2(bif); - for (;blob; blob = BLOB_NEXT (blob)) - { - temp = BLOB_VALUE(blob); - if (temp && (BIF_CODE(temp) == CONTROL_END)) - { - if (BIF_LABEL(temp)) - break; - } - temp = NULL; - } - if (temp && BIF_LABEL(temp)) - { - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); - } - i += strlen("LABELENDIF"); - } else - if (strncmp(&(str[i]),"LABNAME", strlen("LABNAME")) == 0) /* %LABNAME for C labels: added by dbg */ - { - if(BIF_LABEL_USE(bif)){ - if(LABEL_SYMB(BIF_LABEL_USE(bif))) - BufPutString (SYMB_IDENT(LABEL_SYMB(BIF_LABEL_USE(bif))), 0); - else printf("label-symbol error\n"); - } else printf("label error\n"); - i += strlen("LABNAME"); - } else - if (strncmp(&(str[i]),"LABEL", strlen("LABEL"))== 0) /* %STATENO : Statement number */ - { - if (BIF_LABEL(bif)) - { - /*if (BIF_CP(bif)&&(BIF_CODE(BIF_CP(bif))==FOR_NODE)&& - (HPF_VERSION==2)) - { - AfterDOLabel=1; - if (BIF_LABEL_USE(BIF_CP(bif))&& - (LABEL_STMTNO (BIF_LABEL(bif))!=LABEL_STMTNO (BIF_LABEL_USE(BIF_CP(bif))))) - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); - else if (!BIF_LABEL_USE(BIF_CP(bif))) - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); - } - else21052001*/ - /*BufPutInt (LABEL_STMTNO (BIF_LABEL(bif)));*/ - if (BIF_CP(bif)&&(BIF_CODE(BIF_CP(bif))==FOR_NODE)&& - (HPF_VERSION==2)) - { - AfterDOLabel=1; - if (BIF_LABEL_USE(BIF_CP(bif))&& - (LABEL_STMTNO (BIF_LABEL(bif))!=LABEL_STMTNO (BIF_LABEL_USE(BIF_CP(bif))))) - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); - else if (!BIF_LABEL_USE(BIF_CP(bif))) - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); - } - else - BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); - } - i += strlen("LABEL"); - } else - if (strncmp(&(str[i]),"SYMBTYPE", strlen("SYMBTYPE"))== 0) /* SYMBTYPE : Type of Symbol */ - { - if (BIF_SYMB (bif) && SYMB_TYPE (BIF_SYMB (bif))) - { - if (Check_Lang_Fortran(cur_proj)) - BufPutString ( ftype_name [type_index (TYPE_CODE (SYMB_TYPE (BIF_SYMB (bif))))],0); - else if((SYMB_ATTR(BIF_SYMB(bif)) & OVOPERATOR ) == 0){ - PTR_LLND el; - el = BIF_LL1(bif); - if((BIF_CODE(BIF_CP(bif)) == TEMPLATE_FUNDECL) && - el && NODE_TYPE(el)) - Tool_Unparse_Type(NODE_TYPE(el)); - else - Tool_Unparse_Type(SYMB_TYPE (BIF_SYMB (bif))); - } - } - i += strlen("SYMBTYPE"); - } else - if (strncmp(&(str[i]),"CNSTF", strlen("CNSTF"))== 0) /* for const functions */ - { - PTR_SYMB symb; - if (BIF_SYMB (bif)){ - symb = BIF_SYMB (bif); - /* if(SYMB_TEMPLATE_DUMMY8(symb) & 64) BufPutString(" const",0); */ - } - i += strlen("CNSTF"); - } else - if (strncmp(&(str[i]),"VARLISTTY", strlen("VARLISTTY"))== 0) /* %VARLIST : list of variables / parameters */ - { - PTR_SYMB symb; - if (BIF_SYMB (bif)) - symb = SYMB_FUNC_PARAM (BIF_SYMB (bif)); - else - symb = NULL; - while (symb) - { - Tool_Unparse_Type (SYMB_TYPE(symb)); - BufPutString (" ",0); - BufPutString ( SYMB_IDENT (symb),0); - symb = SYMB_NEXT_DECL (symb); - if (symb) BufPutString (", ",0); - } - i += strlen("VARLISTTY"); - } else - if (strncmp(&(str[i]),"TMPLARGS", strlen("TMPLARGS"))== 0) - { - PTR_SYMB symb; - PTR_LLND args, arg_item, t; - PTR_TYPE typ; - if(BIF_CODE(bif) == FUNC_HEDR) args = BIF_LL3(bif); - else args = BIF_LL1(bif); - while (args ) - { - int typflag; - arg_item = NODE_OPERAND0(args); - if(arg_item == NULL) printf("MAJOR TEMPLATE UNPARSE ERROR. contact dbg \n"); - t = arg_item; - typflag = 1; - while(t && typflag){ - if((NODE_CODE(t) == VAR_REF) || (NODE_CODE(t) == ARRAY_REF)) - typflag = 0; - else if (NODE_CODE(t) == SCOPE_OP) t = NODE_OPERAND1(t); - else t = NODE_OPERAND0(t); - } - if(t){ - symb = NODE_SYMB(t); - typ = NODE_TYPE(t); - if(typ == NULL) typ = SYMB_TYPE(symb); - if((int)strlen(symb->ident) > 0){ /* special case for named arguments */ - typflag = 1; - while(typ && typflag){ - if(TYPE_CODE(typ) == T_ARRAY || - TYPE_CODE(typ) == T_FUNCTION || - TYPE_CODE(typ) == T_REFERENCE || - TYPE_CODE(typ) == T_POINTER) typ = TYPE_BASE(typ); - else if(TYPE_CODE(typ) == T_MEMBER_POINTER) - typ = TYPE_COLL_BASE(typ); - else typflag = 0; - } - } - else BufPutString("class ", 0); - Tool_Unparse_Type (typ); - BufPutString (" ",0); - } - /* else printf("could not find var ref!\n"); */ - Tool_Unparse2_LLnode(arg_item); - args = NODE_OPERAND1(args); - if (args) BufPutString (", ",0); - } - i += strlen("TMPLARGS"); - } else - if (strncmp(&(str[i]),"CONSTRU", strlen("CONSTRU"))== 0) - { - PTR_LLND ll; - if (BIF_LL1(bif)) - { - ll = NODE_OPERAND0(BIF_LL1(bif)); - if (ll) - ll = NODE_OPERAND1(ll); - if (ll) - { - BufPutString (":",0); - Tool_Unparse2_LLnode(ll); - } - } - i += strlen("CONSTRU"); - } else - if (strncmp(&(str[i]),"L1SYMBID", strlen("L1SYMBID"))== 0) /* %L1SYMBID : Symbol of Low Level Node 1 */ - { - if (BIF_LL1 (bif)) - Tool_Unparse_Symbol (NODE_SYMB (BIF_LL1 (bif))); - i += strlen("L1SYMBID"); - } else - if (strncmp(&(str[i]),"VARLIST", strlen("VARLIST"))== 0) /* %VARLIST : list of variables / parameters */ - { - PTR_SYMB symb; - if (BIF_SYMB (bif)) - symb = SYMB_FUNC_PARAM (BIF_SYMB (bif)); - else - symb = NULL; - while (symb) - { - BufPutString ( SYMB_IDENT (symb),0); - symb = SYMB_NEXT_DECL (symb); - if (symb) BufPutString (", ",0); - } - i += strlen("VARLIST"); - } else - if (strncmp(&(str[i]),"DELETE_COMMA", strlen("DELETE_COMMA"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ - { - if (Buf_address[Buf_pointer-1]==',') - Buf_pointer--; - i += strlen("DELETE_COMMA"); - } else - if (strncmp(&(str[i]),"TASKERROR0", strlen("TASKERROR0"))== 0) - { - /*Message("Error in block-task-region\n end-task-region-directive: CDVM$ END TASK_REGION is missing before TASK_REGION construct.",BIF_LINE(bif));*/ - fprintf(stderr,"Error 210 on line %d of %s : End-task-region-directive: CDVM$ END TASK_REGION is missing before TASK_REGION construct\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - i += strlen("TASKERROR0"); - } else - if (strncmp(&(str[i]),"TASKERROR1", strlen("TASKERROR1"))== 0) - { - /*Message("Error in block-task-region\n task-region-directive: CDVM$ TASK_REGION is missing before on-block.",BIF_LINE(bif));*/ - fprintf(stderr,"Error 211 on line %d of %s : CDVM$ TASK_REGION is missing before on-block \n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - i += strlen("TASKERROR1"); - } else - if (strncmp(&(str[i]),"TASKERROR2", strlen("TASKERROR2"))== 0) - { - /*Message("Error in block-task-region\n task-region-directive: CDVM$ TASK_REGION is missing before end-on-directive.",BIF_LINE(bif));*/ - fprintf(stderr,"Error 212 on line %d of %s : CDVM$ TASK_REGION is missing before end-on-directive \n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - i += strlen("TASKERROR2"); - } else - if (strncmp(&(str[i]),"TASKERROR3", strlen("TASKERROR3"))== 0) - { - /*Message("Error in block-task-region\n end-task-region-directive: CDVM$ END TASK_REGION is missing before end of program.",BIF_LINE(bif));*/ - fprintf(stderr,"Error 213 on line %d of %s : End-task-region-directive: CDVM$ END TASK_REGION is missing before end of program\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - i += strlen("TASKERROR3"); - } else - if (strncmp(&(str[i]),"TASKERROR4", strlen("TASKERROR4"))== 0) - { - /*Message("Error in block-task-region\n task-region-directive: CDVM$ TASK_REGION is missing before end-task-region-directive.",BIF_LINE(bif));*/ - fprintf(stderr,"Error 214 on line %d of %s : End-task-region-directive: CDVM$ TASK_REGION is missing before end-task-region-directive\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - i += strlen("TASKERROR4"); - } else - if (strncmp(&(str[i]),"ONERROR", strlen("ONERROR"))== 0) - { - if (On_count) - { - fprintf(stderr,"Error 215 on line %d of %s : Error in TASK_REGION construct\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - fprintf(stderr,"Warning 216 on line %d of %s : Incorrect number of ON and END_ON directives\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - /*Message("Error in TASK_REGION construct\nThis may be due to incorrect number of ON and END_ON directives.",BIF_LINE(bif));*/ - On_count=0; - } - i += strlen("ONERROR"); - } else - if (strncmp(&(str[i]),"HPF1_POINTER", strlen("HPF1_POINTER"))== 0) - { - /*Message("Error in DVM_POINTER_DIR\n Can`t work this pointers in HPF1.",BIF_LINE(bif));*/ - fprintf(stderr,"Error 197 on line %d of %s : Can`t work this pointers in HPF1\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - i += strlen("HPF1_POINTER"); - } else - if (strncmp(&(str[i]),"ONPLUS", strlen("ONPLUS"))== 0) - { - int k=0; - On_count++; - Treat_Flag("(ON_REGION)",&k,1); - i += strlen("ONPLUS"); - } - else - if (strncmp(&(str[i]),"ONMINUS", strlen("ONMINUS"))== 0) - { - int k=0; - On_count--; - Treat_Flag("(ON_REGION)",&k,-1); - i += strlen("ONMINUS"); - } - else - if (strncmp(&(str[i]),"ONINIT", strlen("ONINIT"))== 0) - { - On_count=0; - i += strlen("ONINIT"); - } - else - if (strncmp(&(str[i]),"RESETID", strlen("RESETID"))== 0) - { - ResetSymbolId(); - i += strlen("RESETID"); - } - else - if (strncmp(&(str[i]),"CHANGE_TABNUMBER", strlen("CHANGE_TABNUMBER"))== 0) - { - int count=0,label; - count=NumberOfForNode(bif,&label); - if(count) - TabNumber-=count; - if (TabNumber<1) TabNumber=1; - i += strlen("CHANGE_TABNUMBER"); - } - else - if (strncmp(&(str[i]),"FIND_DO", strlen("FIND_DO"))== 0) /* %FIND_DO : We need to find the corresponding FOR_NODE for CONT_STAT*/ - { - i += strlen("FIND_DO"); - /*21052001if (UnparseEndofCircle(bif)) - return Buf_address;*/ - if (UnparseEndofCircle(bif)) - return Buf_address; - } else - if (strncmp(&(str[i]),"SAVE", strlen("SAVE"))== 0) - { - Pointer=Buf_pointer; - i += strlen("SAVE"); - } else - if (strncmp(&(str[i]),"LOAD", strlen("LOAD"))== 0) - { - Buf_pointer=Pointer; - i += strlen("LOAD"); - } else - if (strncmp(&(str[i]),"FIND_MAP", strlen("FIND_MAP"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - PTR_LLND llnd; - if (BIF_LL1(bif)) - { - llnd=FindMapDir(BIF_LL1(bif),bif); - if (llnd) - { - Tool_Unparse2_LLnode(llnd); - } - else - { - /*Message ("Error in Unparse, can`t find a corresponding MAP_DIR", BIF_LINE(bif));*/ - fprintf(stderr,"Error 195 on line %d of %s : Can`t find a corresponding MAP_DIR\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - Tool_Unparse2_LLnode(BIF_LL1(bif)); - } - } - i += strlen("FIND_MAP"); - } else - if (strncmp(&(str[i]),"CHECK_REDISTRIBUTE_DIR", strlen("CHECK_REDISTRIBUTE_DIR"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - PTR_LLND llnd; - if (BIF_LL1(bif)) - { - for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - if (FindRedistributeDir(NODE_OPERAND0(llnd),bif)) - { - if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) - { - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("DYNAMIC ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString("\n",0); - } - } - } - i += strlen("CHECK_REDISTRIBUTE_DIR"); - } else - if (strncmp(&(str[i]),"CHECK_REALIGN_DIR", strlen("CHECK_REALIGN_DIR"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - PTR_LLND llnd; - if (BIF_LL1(bif)) - { - for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - if (FindRealignDir(NODE_OPERAND0(llnd),bif)) - { - if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) - { - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("DYNAMIC ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString("\n",0); - } - } - } - i += strlen("CHECK_REALIGN_DIR"); - } else - if (strncmp(&(str[i]),"CHECK_FORMAT_NULL", strlen("CHECK_FORMAT_NULL"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - if (BIF_LL1(bif)) - { - if (!BIF_LL2(bif)&&!BIF_LL3(bif)) - { - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("DYNAMIC ",0); - Tool_Unparse2_LLnode(BIF_LL1(bif)); - BufPutString("\n",0); - return Buf_address; - } - } - i += strlen("CHECK_FORMAT_NULL"); - } else - if (strncmp(&(str[i]),"CHECK_DVMBIT", strlen("CHECK_DVMBIT"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - if (BIF_LL1(bif)) - { - PTR_LLND ptr; - if (BIF_CODE(bif)==DVM_ALIGN_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_ALIGN_BIT) - { - fprintf(stderr,"Error 200 on line %d of %s:Object '%s' already has ALIGN_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_ALIGN_BIT; - } - } - if (BIF_CODE(bif)==HPF_PROCESSORS_STAT) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_PROCESSORS_BIT) - { - fprintf(stderr,"Error 201 on line %d of %s :Object '%s' already has PROCESSORS_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_PROCESSORS_BIT; - } - } - - if (BIF_CODE(bif)==DVM_DISTRIBUTE_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_DISTRIBUTE_BIT) - { - fprintf(stderr,"Error 202 on line %d of %s :Object '%s' already has DISTRIBUTE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_DISTRIBUTE_BIT; - } - } - if (BIF_CODE(bif)==HPF_TEMPLATE_STAT) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_TEMPLATE_BIT) - { - fprintf(stderr,"Error 203 on line %d of %s :Object '%s' already has TEMPLATE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_TEMPLATE_BIT; - } - } - if (BIF_CODE(bif)==DVM_INHERIT_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_INHERIT_BIT) - { - fprintf(stderr,"Error 204 on line %d of %s :Object '%s' already has INHERIT_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_INHERIT_BIT; - } - } - if (BIF_CODE(bif)==DVM_DYNAMIC_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_DYNAMIC_BIT) - { - fprintf(stderr,"Error 205 on line %d of %s :Object '%s' already has DYNAMIC_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_DYNAMIC_BIT; - } - } - if (BIF_CODE(bif)==DVM_SHADOW_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_SHADOW_BIT) - { - fprintf(stderr,"Error 206 on line %d of %s :Object '%s' already has SHADOW_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_SHADOW_BIT; - } - } - if (BIF_CODE(bif)==DVM_TASK_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_TASK_BIT) - { - fprintf(stderr,"Error 207 on line %d of %s :Object '%s' already has TASK_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_TASK_BIT; - } - } - if (BIF_CODE(bif)==DVM_POINTER_DIR) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_DVM_POINTER_BIT) - { - fprintf(stderr,"Error 208 on line %d of %s :Object '%s' already has DVM_POINTER_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else SYMB_ID(NODE_SYMB(llnd))=ALREADY_DVM_POINTER_BIT; - } - } - if (BIF_CODE(bif)==DVM_VAR_DECL) - { - PTR_LLND ptr_llnd; - int BIT=0; - for(ptr_llnd=BIF_LL3(bif);ptr_llnd&&(NODE_CODE(ptr_llnd)==EXPR_LIST);ptr_llnd=NODE_OPERAND1(ptr_llnd)) - { - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==DISTRIBUTE_OP) - BIT|=ALREADY_DISTRIBUTE_BIT; - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==PROCESSORS_OP) - BIT|=ALREADY_PROCESSORS_BIT; - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==ALIGN_OP) - { - BIT|=ALREADY_ALIGN_BIT; - } - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==TEMPLATE_OP) - { - BIT|=ALREADY_TEMPLATE_BIT; - } - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==SHADOW_OP) - { - BIT|=ALREADY_SHADOW_BIT; - } - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==DYNAMIC_OP) - { - BIT|=ALREADY_DYNAMIC_BIT; - } - } - for(ptr=BIF_LL1(bif);BIT&&ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - PTR_LLND llnd=NODE_OPERAND0(ptr); - if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; - if ((BIT&ALREADY_DYNAMIC_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_DYNAMIC_BIT)) - { - fprintf(stderr,"Error 205 on line %d of %s :Object '%s' already has DYNAMIC_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else - if (BIT&ALREADY_DYNAMIC_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_DYNAMIC_BIT; - if ((BIT&ALREADY_PROCESSORS_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_PROCESSORS_BIT)) - { - fprintf(stderr,"Error 201 on line %d of %s :Object '%s' already has PROCESSORS_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else - if (BIT&ALREADY_PROCESSORS_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_PROCESSORS_BIT; - if ((BIT&ALREADY_SHADOW_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_SHADOW_BIT)) - { - fprintf(stderr,"Error 206 on line %d of %s :Object '%s' already has SHADOW_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else - if (BIT&ALREADY_SHADOW_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_SHADOW_BIT; - if ((BIT&ALREADY_TEMPLATE_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_TEMPLATE_BIT)) - { - fprintf(stderr,"Error 203 on line %d of %s :Object '%s' already has TEMPLATE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else - if (BIT&ALREADY_TEMPLATE_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_TEMPLATE_BIT; - if ((BIT&&ALREADY_ALIGN_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_ALIGN_BIT)) - { - fprintf(stderr,"Error 200 on line %d of %s :Object '%s' already has ALIGN_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else - if (BIT&ALREADY_ALIGN_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_ALIGN_BIT; - if ((BIT&ALREADY_DISTRIBUTE_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_DISTRIBUTE_BIT)) - { - fprintf(stderr,"Error 202 on line %d of %s :Object '%s' already has DISTRIBUTE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); - errnumber++; - } - else - if (BIT&ALREADY_DISTRIBUTE_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_DISTRIBUTE_BIT; - } - } - } - i += strlen("CHECK_DVMBIT"); - } else - if (strncmp(&(str[i]),"CHECK_COMBINED_DIR", strlen("CHECK_COMBINED_DIR"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - PTR_LLND llnd; - if (BIF_LL1(bif)) - { - for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - PTR_SYMB s=SMNULL; - if (NODE_CODE(NODE_OPERAND0(llnd))==ARRAY_REF) - s=NODE_SYMB(NODE_OPERAND0(llnd)); - if (!s) continue; - if(s->attr & DISTRIBUTE_BIT) - if (FindRedistributeDir(NODE_OPERAND0(llnd),bif)) - { - if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) - { - BufPutString("\n!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("DYNAMIC ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - } - } - if(s->attr & ALIGN_BIT) - if (FindRealignDir(NODE_OPERAND0(llnd),bif)) - { - if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) - { - BufPutString("\n!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("DYNAMIC ",0); - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - } - } - - } - } - i += strlen("CHECK_COMBINED_DIR"); - } else - if (strncmp(&(str[i]),"CHECK_SHADOW_OP", strlen("CHECK_SHADOW_OP"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ - { - if (BIF_CODE(bif)==DVM_VAR_DECL) - { - PTR_LLND ptr_llnd; - int OpNumber=0; - int Shadow=0; - for(ptr_llnd=BIF_LL3(bif);ptr_llnd&&(NODE_CODE(ptr_llnd)==EXPR_LIST);ptr_llnd=NODE_OPERAND1(ptr_llnd)) - { - if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==SHADOW_OP) - Shadow=1; - else OpNumber++; - } - if ((Shadow==1)&&!OpNumber) return Buf_address; - } - i += strlen("CHECK_SHADOW_OP"); - } else - if (strncmp(&(str[i]),"CHECK_DAC_DIR", strlen("CHECK_DAC_DIR"))== 0) /* %CHECK_DAC_DIR : We need to find FDVM-directive that can be transformed into DYNAMIC-directive*/ - { - PTR_LLND llnd; - PTR_BFND ptr_bif; - int pointer,beg; - beg=Buf_pointer; - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("DYNAMIC ",0); - pointer=Buf_pointer; - if (BIF_LL1(bif)) - { - for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - if ((ptr_bif=FindDistrAlignCombinedDir(NODE_OPERAND0(llnd),bif))) - { - if (!CheckNullDistribution(ptr_bif)) - { - Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); - BufPutString(" ,",0); - } - } - Buf_pointer--; - BufPutString("\n",0); - } - if (pointer==Buf_pointer) Buf_pointer=beg; - return Buf_address; - i += strlen("CHECK_DAC_DIR"); - } else - if (strncmp(&(str[i]),"UNPARSE_ON", strlen("UNPARSE_ON"))== 0) /* %APPEND_ON : We need to append ON clause after FOR statement in INDEPENDENT cycle for DVM_PARALLEL_TASK_DIR*/ - { - PTR_LLND llnd; - if (On_Clause) - { - llnd=FindMapDir(On_Clause,bif); - if (llnd) - { - BufPutString("!HPF$",0); - Puttab(); - Buf_pointer-=5; - BufPutString("ON ( ",0); - Tool_Unparse2_LLnode(llnd); - BufPutString(" ) BEGIN",0); - } - else - { - /*Message ("Error in Unparse, can`t find a corresponding MAP_DIR for DVM_PARALLEL_TASK_DIR", BIF_LINE(bif));*/ - fprintf(stderr,"Error 195 on line %d of %s : Can`t find a corresponding MAP_DIR\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - } - } - i += strlen("UNPARSE_ON"); - } else - if (strncmp(&(str[i]),"CHECK_ACROSS", strlen("CHECK_ACROSS"))== 0) - { - i = i + strlen("CHECK_ACROSS"); - if (CheckAcross(bif)) return Buf_address; - } else - if (strncmp(&(str[i]),"CHECK_REDUCTION", strlen("CHECK_REDUCTION"))== 0) - { - i = i + strlen("CHECK_REDUCTION"); - if (CheckReduction(bif)) return Buf_address; - } else - if (strncmp(&(str[i]),"FIND_REDUCTION", strlen("FIND_REDUCTION"))== 0) - { - ForNodeStmt(BIF_NEXT(bif)); - i = i + strlen("FIND_REDUCTION"); - } else - - if (strncmp(&(str[i]),"PAR_TASK_MAP", strlen("PAR_TASK_MAP"))== 0) /* %PAR_TASK_MAP : We need to save ON clause for DVM_PARALLEL_TASK_DIR*/ - { - On_Clause=BIF_LL1(bif); - i += strlen("PAR_TASK_MAP"); - } else - if (strncmp(&(str[i]),"CHECK_REDISTRIBUTE_ON_MAP", strlen("CHECK_REDISTRIBUTE_ON_MAP"))== 0) /* %CHECK_REDISTRIBUTE_ON_MAP : We need to find the corresponding MAP_DIR*/ - { - PTR_LLND llnd,ptr_llnd; - if (BIF_LL3(bif)) - { - ptr_llnd=ChangeRedistributeOntoTask(BIF_LL3(bif)); - if (ptr_llnd) - { - llnd=FindMapDir(ptr_llnd,bif); - if (llnd) - { - /*if (HPF_VERSION==1) - Tool_Unparse_Symbol(NODE_SYMB(llnd)); - else */ - Tool_Unparse2_LLnode(llnd); - } - else - { - /*Message ("Error in Unparse, can`t find a corresponding MAP_DIR", BIF_LINE(bif));*/ - fprintf(stderr,"Error 195 on line %d of %s : Can`t find a corresponding MAP_DIR\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - Tool_Unparse2_LLnode(BIF_LL3(bif)); - } - } - else Tool_Unparse2_LLnode(BIF_LL3(bif)); - } - i += strlen("CHECK_REDISTRIBUTE_ON_MAP"); - } else - if (strncmp(&(str[i]),"CHECK_ALLOCATE", strlen("CHECK_ALLOCATE"))== 0) /* %CHECK_ALLOCATE : We must change the allocation for DVM`s pointers*/ - { - PTR_LLND llnd; - if (NODE_CODE(BIF_LL2(bif))==FUNC_CALL) - { - if (!strcmp(SYMB_IDENT(NODE_SYMB(BIF_LL2(bif))),"allocate")) - if ((NODE_CODE(BIF_LL1(bif))==ARRAY_REF)&& (SYMB_ATTR(NODE_SYMB(BIF_LL1(bif)))&DVM_POINTER_BIT)) - { - int count=0; - char *str; - count=FindPointerDir(NODE_SYMB(BIF_LL1(bif)),bif); - if (count) - { - int i=0; - BufPutString("ALLOCATE(", 0); - Tool_Unparse2_LLnode(BIF_LL1(bif)); - BufPutChar('('); - llnd=NODE_OPERAND0(BIF_LL2(bif)); - if (!llnd||(NODE_CODE(llnd)!=EXPR_LIST)) - { - /*Message("Incorrect call of allocate function",BIF_LINE(bif));*/ - fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - BufPutString("))\n", 0); - return Buf_address; - }; - if (NODE_OPERAND0(llnd)) - { - if (NODE_CODE(NODE_OPERAND0(llnd))==VAR_REF) - { - fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - fprintf(stderr,"Warning 197 on line %d of %s : You can`t use variable as SDIM array function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - /*Message("Incorrect call of allocate function",0); - Message("You can`t use variable as SDIM array",BIF_LINE(bif));*/ - BufPutString("))\n", 0); - return Buf_address; - } - else - if (NODE_CODE(NODE_OPERAND0(llnd))==ARRAY_REF) - { - int ar_index=0; - PTR_LLND ptr1=LLNULL,ptr2=LLNULL; - str=SYMB_IDENT(NODE_SYMB(NODE_OPERAND0(llnd))); - llnd=NODE_OPERAND0(NODE_OPERAND0(llnd)); - if (!llnd) - { - for(i=1;i<=count;i++) - { - if (i!=1) BufPutChar(','); - BufPutString(str,0); - BufPutChar('('); - BufPutInt(i); - BufPutChar(')'); - } - BufPutString("))\n", 0); - return Buf_address; - }; - if (NODE_OPERAND0(llnd)) ptr1=NODE_OPERAND0(llnd); - if (NODE_OPERAND1(llnd)) ptr2=NODE_OPERAND0(NODE_OPERAND1(llnd)); - if (ptr1&&!ptr2) - { - /*Message("Incorrect call of allocate function",BIF_LINE(bif));*/ - fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - BufPutString("))\n", 0); - return Buf_address; - }; - if (ptr1&&ptr2&&(NODE_CODE(ptr1)==INT_VAL)) - { - ar_index=1; - if(NODE_CODE(ptr2)==INT_VAL) - { - fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - fprintf(stderr,"Warning 198 on line %d of %s : You can`t use 2 integer constants in SDIM array\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - /*Message("Incorrect call of allocate function",0); - Message("You can`t use 2 integer constants in SDIM array",BIF_LINE(bif));*/ - BufPutString("))\n", 0); - return Buf_address; - }; - } - else - if (ptr1&&ptr2&&(NODE_CODE(ptr2)==INT_VAL)) - ar_index=2; - else - { - fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - fprintf(stderr,"Warning 199 on line %d of %s : You can`t use 2 variables in SDIM array\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - /* Message("Incorrect call of allocate function",0); - Message("You can`t use 2 variables in SDIM array",BIF_LINE(bif));*/ - BufPutString("))\n", 0); - return Buf_address; - }; - for(i=1;(i<=count)&&ar_index;i++) - { - if (i!=1) BufPutChar(','); - BufPutString(str,0); - BufPutChar('('); - if (ar_index==1) - { - BufPutInt(i); - BufPutChar(','); - Tool_Unparse2_LLnode(ptr2); - } - else - { - Tool_Unparse2_LLnode(ptr1); - BufPutChar(','); - BufPutInt(i); - } - BufPutChar(')'); - } - } - } - BufPutChar(')'); - BufPutString(")\n", 0); - return Buf_address; - } - } - } - else - if (NODE_CODE(bif)==ASSIGN_STAT) - { - llnd=BIF_LL1(bif); - if(((NODE_CODE(llnd)==ARRAY_REF)||(NODE_CODE(llnd)==VAR_REF))&&NODE_SYMB(llnd)&&(SYMB_ATTR(NODE_SYMB(llnd))&DVM_POINTER_BIT)) - /* if(SYMB_ATTR(NODE_SYMB(llnd))&DVM_POINTER_BIT)*/ - { - Tool_Unparse2_LLnode(llnd); - BufPutString(" => ",0); - Tool_Unparse2_LLnode(BIF_LL2(bif)); - BufPutString("\n",0); - return Buf_address; - } - } - i += strlen("CHECK_ALLOCATE"); - } else - if (strncmp(&(str[i]),"CHECK_FUNC", strlen("CHECK_FUNC"))== 0) /* %CHECK_FUNC : We must change function ALLOCATE*/ - { - int count=0; - if (NODE_SYMB(bif)) - if (!strcmp(SYMB_IDENT(NODE_SYMB(bif)),"ALLOCATE")|| - !strcmp(SYMB_IDENT(NODE_SYMB(bif)),"allocate")) - Treat_Flag("(COMMENT)",&count,1); - i += strlen("CHECK_FUNC"); - } else - if (strncmp(&(str[i]),"DECLARATION", strlen("DECLARATION"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ - { - int count=0; - if (BIF_LL1(bif)) count=FindPointerDeclaration(BIF_LL1(bif)); - if (!count) - { - count=0; - Treat_Flag("(POINTER)",&count, 1); - } - i += strlen("DECLARATION"); - } else - if (strncmp(&(str[i]),"DESCRIPTOR", strlen("DESCRIPTOR"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ - { - PTR_LLND ptr,llnd; - if (BIF_LL1(bif)) - { - for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - { - if (NODE_OPERAND0(ptr)) - { - llnd=FindPointerDescriptor(NODE_OPERAND0(ptr),bif); - if (NODE_OPERAND0(llnd)) continue; - if (!llnd) - { - /*Message("Can`t find a descriptor for POINTER",BIF_LINE(bif));*/ - fprintf(stderr,"Error 222 on line %d of %s : Can`t find a descriptor for POINTER\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); - errnumber++; - Tool_Unparse2_LLnode(NODE_OPERAND0(ptr)); - } - else Tool_Unparse2_LLnode(llnd); - BufPutChar(','); - } - } - Buf_pointer--; - } - i += strlen("DESCRIPTOR"); - } else - if (strncmp(&(str[i]),"PTR_ARRAY", strlen("PTR_ARRAY"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ - { - if (BIF_LL1(bif)) - { - ArrayOfPointerDeclaration(bif); - PointerDeclaration(bif); - return Buf_address; - } - i += strlen("PTR_ARRAY"); - } else - - if (strncmp(&(str[i]),"RIDPT", strlen("RIDPT"))== 0) - { - PTR_TYPE type = NULL; - - type = Find_Type_For_Bif(bif); - if (type ) - { - DealWith_Rid(type, In_Class_Flag); - } - else if(BIF_CODE(bif) == CLASS_DECL) - { - DealWith_Rid(SYMB_TYPE(BIF_SYMB(bif)), In_Class_Flag); - } - i += strlen("RIDPT"); - } else - if (strncmp(&(str[i]),"INCLASSON", strlen("INCLASSON"))== 0) - { - In_Class_Flag = 1; - i += strlen("INCLASSON"); - } else - if (strncmp(&(str[i]),"INCLASSOFF", strlen("INCLASSOFF"))== 0) - { - In_Class_Flag = 0; - i += strlen("INCLASSOFF"); - } else - if (strncmp(&(str[i]),"INWRITEON", strlen("INWRITEON"))== 0) /* %INWRITEON : In_Write_Statement Flag ON */ - { - In_Write_Flag = 1; - i += strlen("INWRITEON"); - } else - if (strncmp(&(str[i]),"INWRITEOFF", strlen("INWRITEOFF"))== 0) /* %INWRITEOFF : In_Write_Statement Flag OFF */ - { - In_Write_Flag = 0; - i += strlen("INWRITEOFF"); - } else - if (strncmp(&(str[i]),"RECPORTON", strlen("RECPORTON"))== 0) /* %RECPORTON : recursive_port_decl Flag ON */ - { - Rec_Port_Decl = 1; - i += strlen("RECPORTON"); - } else - if (strncmp(&(str[i]),"RECPORTOFF", strlen("RECPORTOFF"))== 0) /* %RECPORTOFF : recursive_port_decl Flag OFF */ - { - Rec_Port_Decl = 0; - i += strlen("RECPORTOFF"); - } else - - if (strncmp(&(str[i]),"INPARAMON", strlen("INPARAMON"))== 0) /* %INPARAMON : In_Param_Statement Flag ON */ - { - In_Param_Flag = 1; - i += strlen("INPARAMON"); - } else - if (strncmp(&(str[i]),"INPARAMOFF", strlen("INPARAMOFF"))== 0) /* %INPARAMOFF : In_Param_Statement Flag OFF */ - { - In_Param_Flag = 0; - i += strlen("INPARAMOFF"); - } else - if (strncmp(&(str[i]),"INIMPLION", strlen("INIMPLION"))== 0) /* %INIMPLION : In_Impli_Statement Flag ON */ - { - In_Impli_Flag = 1; - i += strlen("INIMPLION"); - } else - if (strncmp(&(str[i]),"INIMPLIOFF", strlen("INIMPLIOFF"))== 0) /* %INIMPLIOFF : In_Impli_Statement Flag OFF */ - { - In_Impli_Flag = 0; - i += strlen("INIMPLIOFF"); - } else - if (strncmp(&(str[i]),"TYPE", strlen("TYPE"))== 0) - { - PTR_TYPE type = NULL; - type = Find_Type_For_Bif(bif); - if (!type) - { - Message("TYPE not found",0); - BufPutString("------TYPE ERROR----",0); - } - if( !is_overloaded_type(bif) ) - Tool_Unparse_Type (type); - i += strlen("TYPE"); - } else - if (strncmp(&(str[i]),"PROTECTION", strlen("PROTECTION"))== 0) - { - int protect = 0; /*protect = NULL*/ - protect = Find_Protection_For_Bif(bif); - if (protect) - { - if (protect & 128) - { - /* BufPutString("MethodOfElement:\n",0); a temporary fix until dep2C++ done */ - BufPutString("public:\n", 0); - } else - { - switch (protect) - { /* find the definition of the flag someday */ - case 64: BufPutString("public:\n",0); break; - case 32: BufPutString("protected:\n",0); break; - case 16: BufPutString("private:\n",0); break; - } - } - } - i += strlen("PROTECTION"); - } else - if (strncmp(&(str[i]),"DUMMY", strlen("DUMMY"))== 0) /* %DUMMY Do nothing */ - { - i += strlen("DUMMY"); - } else - { - Message (" *** Unknown bif node COMMAND *** ",0); - Message (&(str[i]),0); - } - } - else - { - BufPutChar( c); - i++; - } - c = str[i]; - } - if (AfterDOLabel&&(HPF_VERSION==2)) UnparseEndofCircle(bif); - if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&(variant==DVM_ON_DIR)) - { - ON_BLOCK=1; - ON_BEGIN=Buf_pointer; - }; - return Buf_address; -} - -void DefineHPF1() -{ -#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].str = create_unp_str( NAME); -#include"unparse1.hpf" -#undef DEFNODECODE - -#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].fct = NULL; -#include"unparse1.hpf" -#undef DEFNODECODE -} -void DefineHPF2() -{ -#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].str = create_unp_str( NAME); -#include"unparse.hpf" -#undef DEFNODECODE - -#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].fct = NULL; -#include"unparse.hpf" -#undef DEFNODECODE -} - -void Init_HPFUnparser() -{ - int i,j; - CommentOut=1; /*Ignore comment*/ - if (Parser_Initiated != Fortran_Initialized) - { - /*printf("\nHPF_VERSION = %d",HPF_VERSION);*/ - if (HPF_VERSION==1) DefineHPF1(); - else DefineHPF2(); - Parser_Initiated = Fortran_Initialized; - /* set the first tabulation */ - TabNumber = 1; - } - /* initialize the number of flag */ - Number_Of_Flag = 0; - for (i=0; i < MAXFLAG ; i++) - { - TabOfFlag[i][0] = '\0'; - FlagLenght[i] = 0; - for(j=0; jhead_bfnd))); - return errnumber; -} - -PTR_LLND FindMapDir(PTR_LLND ptr_llnd,PTR_BFND ptr_bif) -{ -PTR_BFND bif,end=FindEndOfBlock(ptr_bif); -for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) - { - if (BIF_CODE(bif)!=DVM_MAP_DIR) continue; - if (patternMatchExpression(ptr_llnd,BIF_LL1(bif))) - { - return BIF_LL2(bif); - } - } -return NULL; -} - -int NumberOfForNode(PTR_BFND ptrbif,int *label) -{ -int incircle=0; -PTR_BFND bif,end=FindEndOfBlock(ptrbif); -int count=0; -if (!BIF_LABEL(ptrbif)) return 0; -for(bif=FindBeginingOfBlock(ptrbif);bif&&(bif!=end);bif=BIF_NEXT(bif)) - { - if (incircle&&(BIF_CODE(bif)==GOTO_NODE)) - { - if (BIF_LL3(bif)&&NODE_LABEL (BIF_LL3(bif))) - { - if ( LABEL_STMTNO (NODE_LABEL (BIF_LL3(bif)))==LABEL_STMTNO (BIF_LABEL(ptrbif))) - { - *label=LABEL_STMTNO (BIF_LABEL(ptrbif)); - } - } - - } - if (BIF_CODE(bif)!=FOR_NODE) continue; - if (BIF_LABEL_USE (bif)) - { - if (LABEL_STMTNO (BIF_LABEL_USE (bif))==LABEL_STMTNO (BIF_LABEL(ptrbif))) - { - count++; - incircle=1; - } - - } - } -return count; -} - -int FindPointerDir(PTR_SYMB symb,PTR_BFND ptr_bif) -{ -PTR_BFND bif,end=FindEndOfBlock(ptr_bif); -for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) - { - PTR_LLND llnd; - if (BIF_CODE(bif)!=DVM_POINTER_DIR) continue; - for(llnd=BIF_LL1(bif);llnd;llnd=NODE_OPERAND1(llnd)) - if (!strcmp(SYMB_IDENT(NODE_SYMB(NODE_OPERAND0(llnd))),SYMB_IDENT(symb))) - { - int count=0; - char *str; - str=funparse_llnd(BIF_LL2(bif)); - for(;*str!='\0';str++) - if (*str==':') count++; - return count; - } - } -return 0; -} - -int Puttab() -{ - int j; - for (j = 0; j < TabNumber; j++) - if (j>0) - BufPutString (" ",0); - else - BufPutString (" ",0); /* cychen */ -return j; -} - -PTR_LLND ChangeRedistributeOntoTask(PTR_LLND llnd) -{ -if (!llnd) return LLNULL; -/*if (NODE_CODE(llnd)!=EXPR_LIST) return LLNULL; -if (NODE_CODE(NODE_OPERAND0(llnd))!=ARRAY_REF) return LLNULL; -if (!(SYMB_ATTR(NODE_SYMB(NODE_OPERAND0(llnd)))&TASK_BIT)) return LLNULL;*/ -if (NODE_CODE(llnd)!=ARRAY_REF) return LLNULL; -if (!(SYMB_ATTR(NODE_SYMB(llnd))&TASK_BIT)) return LLNULL; -/*return NODE_OPERAND0(llnd);*/ -return llnd; -} - -int Find_SaveSymbol(PTR_LLND llnd) -{ -int i; -if (NODE_CODE(llnd)!=VAR_REF) return 0; -for (i=0;ihead_bfnd;bif;bif=BIF_NEXT(bif)) - { - PTR_LLND ptr; - int ok=0; - if (BIF_CODE(bif)!=DVM_VAR_DECL) continue; - for(ptr=BIF_LL3(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) - if (NODE_CODE(NODE_OPERAND0(ptr))==DYNAMIC_OP) ok=1; - if (BIF_LL1(bif)) - { - PTR_LLND llnd; - for(llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - if (patternMatchExpression(ptr_llnd,NODE_OPERAND0(llnd))) - if (ok) - return NODE_OPERAND0(llnd); - else return 0; - } - } - } -*/ -return NULL; -} - -int UnparseEndofCircle(PTR_BFND bif) -{ -int TabNum; -int i,count=0,label=0; -TabNum=TabNumber; -count=NumberOfForNode(bif,&label); -if (label) BufPutInt(label); -if(count) - { - if (TabNumber>1) TabNumber--; - for(i=0;i1) TabNumber--; - k=0; - Treat_Flag("(TASK_DIR)",&k,-1); - TabNum--; - } - } - Puttab(); - if (TabNumber>1) TabNumber--; - BufPutString("end do\n",0); - } - TabNumber=TabNum; - } -return count; -/*21052001*/ -} - -PTR_BFND FindDistrAlignCombinedDir(PTR_LLND ptr_llnd,PTR_BFND ptr_bif) -{ -PTR_BFND bif,end=FindEndOfBlock(ptr_bif); -for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) - { - if ((BIF_CODE(bif)==DVM_DISTRIBUTE_DIR)|| - (BIF_CODE(bif)==DVM_ALIGN_DIR)|| - (BIF_CODE(bif)==DVM_VAR_DECL)) - { - if (BIF_LL1(bif)) - { - PTR_LLND llnd; - for(llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - if (patternMatchExpression(ptr_llnd,NODE_OPERAND0(llnd))) - { - return bif; - } - } - } - } - } -return NULL; -} - -PTR_LLND FindPointerDescriptor(PTR_LLND ptr_llnd,PTR_BFND ptr_bif) -{ -PTR_BFND bif,end=FindEndOfBlock(ptr_bif); -for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) - { - if (BIF_CODE(bif)!=VAR_DECL) continue; - if (BIF_LL1(bif)) - { - PTR_LLND llnd; - for(llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - if (!strcmp(SYMB_IDENT(NODE_SYMB(ptr_llnd)),SYMB_IDENT(NODE_SYMB(NODE_OPERAND0(llnd))))) - return NODE_OPERAND0(llnd); - } - } - } -return NULL; -} - -int ArrayOfPointerDeclaration(PTR_BFND bif) -{ -PTR_LLND llnd=BIF_LL1(bif); -int ok=1,empty=1; -int count=0; -for (;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - PTR_LLND descr; - descr=FindPointerDescriptor(NODE_OPERAND0(llnd),bif); - if (ok&&!count) - { - char *str=NULL; - str=funparse_llnd(BIF_LL2(bif)); - for(;*str!='\0';str++) - if (*str==':') count++; - } - if (count&&descr&&NODE_OPERAND0(descr)) - { - char FLAG[10]; - int i=0; - if (count>10) - { - Message("Can`t work : too many dimensions in DVM_POINTER_DIR",0); - return 0; - } - sprintf(FLAG,"(PTRTYPE%i)",count); - if (ok&&!Get_Flag_val(FLAG,&i)) - { - i=0; - Treat_Flag(FLAG, &i,1); - GenerateType(count, BIF_LL3(bif)); - }; - if(ok) - { - Puttab(); - BufPutString("TYPE (PTR_ARRAY",0); - BufPutInt(count); - BufPutString(") :: ",0); - ok=0; - empty=0; - BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); - SYMB_ATTR(NODE_SYMB(descr))|=DVM_POINTER_ARRAY_BIT; - BufPutChar('('); - Tool_Unparse2_LLnode(NODE_OPERAND0(descr)); - BufPutChar(')'); - continue; - } - if (!ok) - { - empty=0; - BufPutChar(','); - BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); - SYMB_ATTR(NODE_SYMB(descr))|=DVM_POINTER_ARRAY_BIT; - BufPutChar('('); - Tool_Unparse2_LLnode(NODE_OPERAND0(descr)); - BufPutChar(')'); - } - } - } -if (!empty) BufPutChar('\n'); -return 1; -} - -void PointerDeclaration(PTR_BFND bif) -{ -PTR_LLND llnd=BIF_LL1(bif); -int ok=1,empty=1; -int count=0; -for (;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - PTR_LLND descr; - descr=FindPointerDescriptor(NODE_OPERAND0(llnd),bif); - if (ok&&!count) - { - char *str=NULL; - str=funparse_llnd(BIF_LL2(bif)); - for(;*str!='\0';str++) - if (*str==':') count++; - } - if ((count)&&descr&&!NODE_OPERAND0(descr)) - { - if(ok) - { - Puttab(); - Tool_Unparse2_LLnode(BIF_LL3(bif)); - BufPutString(", POINTER, DIMENSION (",0); - Tool_Unparse2_LLnode(BIF_LL2(bif)); - BufPutString(") :: ",0); - ok=0; - empty=0; - BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); - continue; - } - if (!ok) - { - empty=0; - BufPutChar(','); - BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); - } - } - } -if (!empty) BufPutChar('\n'); -} - -void GenerateType(int count, PTR_LLND llnd_type) -{ - int i; - Puttab(); - BufPutString("TYPE PTR_ARRAY",0); - BufPutInt(count); - BufPutChar('\n'); - TabNumber++; - Puttab(); - Tool_Unparse2_LLnode(llnd_type); - BufPutString(", POINTER, DIMENSION (",0); - for(i=0;i= 0 ; i --) - if ( filename[i] == '.' ) - break; - hpfname[i+1]='h'; - hpfname[i+2]='p'; - hpfname[i+3]='f'; - hpfname[i+4]='\0'; -} - -int CheckNullDistribution(PTR_BFND bif) -{ -if (BIF_CODE(bif)==DVM_DISTRIBUTE_DIR) - { - if (!BIF_LL2(bif)&&!BIF_LL3(bif)) - return 1; - }; -if (BIF_CODE(bif)==DVM_ALIGN_DIR) - { - if (!BIF_LL2(bif)&&!BIF_LL3(bif)) - return 1; - }; -if (BIF_CODE(bif)==DVM_VAR_DECL) - { - PTR_LLND llnd=BIF_LL3(bif); - for(;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) - { - /*BufPutInt(NODE_CODE(NODE_OPERAND0(llnd)));*/ - if (NODE_CODE(NODE_OPERAND0(llnd))==DYNAMIC_OP) - return 1; - if (NODE_CODE(NODE_OPERAND0(llnd))==DISTRIBUTE_OP) - if (!NODE_OPERAND0(NODE_OPERAND0(llnd))&&!NODE_OPERAND1(NODE_OPERAND0(llnd))) - return 1; - if (NODE_CODE(NODE_OPERAND0(llnd))==ALIGN_OP) - if (!NODE_OPERAND0(NODE_OPERAND0(llnd))&&!NODE_OPERAND1(NODE_OPERAND0(llnd))) - return 1; - }; - } -/*BufPutInt(1234567);*/ -return 0; -} - -void ResetSymbolId() -{ -int i,j; -char FLAG[10]; -PTR_SYMB symb; -for (symb = current_file->head_symb; symb ; symb = SYMB_NEXT (symb)) - SYMB_ID (symb) = 0; -for (j=0;j<10;j++) - { - sprintf(FLAG,"(PTRTYPE%i)",j); - i=0; - if (Get_Flag_val(FLAG,&i)) - { - i=0; - Treat_Flag(FLAG, &i,-1); - }; - } -} - -PTR_BFND FindBeginingOfBlock(PTR_BFND ptr_bif) -{ -PTR_BFND bif,ptr=NULL; -for(bif=current_file->head_bfnd;bif&&(bif!=ptr_bif);bif=BIF_NEXT(bif)) - { - if ((BIF_CODE(bif)==PROG_HEDR)|| - (BIF_CODE(bif)==PROC_HEDR)|| - (BIF_CODE(bif)==PROS_HEDR)) - ptr=bif; - } -return ptr; -} - -PTR_BFND FindEndOfBlock(PTR_BFND ptr_bif) -{ -PTR_BFND bif; -for(bif=ptr_bif;bif;bif=BIF_NEXT(bif)) - { - if (BIF_CODE(bif)==CONTROL_END) - if ((BIF_CODE(BIF_CP(bif))==PROG_HEDR)|| - (BIF_CODE(BIF_CP(bif))==PROC_HEDR)|| - (BIF_CODE(BIF_CP(bif))==PROS_HEDR)) - { - return bif; - } - } -return NULL; -} - -int CheckAcross(PTR_BFND bif) -{ -PTR_LLND llnd; -if (!bif) return 0; -for (llnd=BIF_LL2(bif);llnd&&NODE_CODE(llnd)==EXPR_LIST;llnd=NODE_OPERAND1(llnd)) - { - if (NODE_OPERAND0(llnd)&&(NODE_CODE(NODE_OPERAND0(llnd))==ACROSS_OP)) - return 1; - } -return 0; -} -int CheckReduction(PTR_BFND bif) -{ -PTR_LLND llnd; -if (!bif) return 0; -for (llnd=BIF_LL2(bif);llnd&&NODE_CODE(llnd)==EXPR_LIST;llnd=NODE_OPERAND1(llnd)) - { - if (NODE_OPERAND0(llnd)&&(NODE_CODE(NODE_OPERAND0(llnd))==REDUCTION_OP)) - return 1; - } -return 0; -} - -int IfReduction(PTR_LLND e1, PTR_LLND e2) -{ - if(!e1||!e2) return(0); - if(NODE_CODE(e1) != NODE_CODE(e2)) - return(0); - if(NODE_CODE(e1) != VAR_REF && NODE_CODE(e1) != ARRAY_REF) - return(0); - if(NODE_SYMB(e1) != NODE_SYMB(e2)) - return(0); - if(NODE_CODE(e1) == ARRAY_REF && !patternMatchExpression(NODE_OPERAND0(e1),NODE_OPERAND0(e2))) - return(0); - return (1); -} -int FindRedInExpr(PTR_LLND red, PTR_LLND expr) -{ -if(!expr) return 0; -if (!red) return 0; -if (NODE_CODE(red)!=VAR_REF && NODE_CODE(red)!=ARRAY_REF) return 0; - -if(NODE_CODE(red)==VAR_REF && NODE_CODE(red) == NODE_CODE(expr)) - { - if (NODE_SYMB(red)== NODE_SYMB(expr)) - return 1; - else return 0; - } - -if(NODE_CODE(red)==ARRAY_REF && NODE_CODE(red) == NODE_CODE(expr)) - { - if (NODE_SYMB(red) == NODE_SYMB(expr)) - return(patternMatchExpression(NODE_OPERAND0(red),NODE_OPERAND0(expr))); - } -return (FindRedInExpr(red,NODE_OPERAND0(expr))+FindRedInExpr(red,NODE_OPERAND1(expr))); -} - -PTR_LLND AddToReductionList(PTR_LLND redlist, PTR_LLND newred) -{ -PTR_LLND new_node,ptr; -new_node=make_llnode(EXPR_LIST,newred,LLNULL,SMNULL); -if (new_node) - { - if (redlist) - { - ptr=Follow_Llnd(redlist,2); - NODE_OPERAND1(ptr)=new_node; - } - if (!redlist) redlist=new_node; - } -else - { - return NULL; - } -return redlist; -} - -int FindInNewList(PTR_LLND newlist, PTR_LLND red) -{ -PTR_LLND ExprList; -if (!newlist) return 0; - -if (!red) return 0; -if (NODE_CODE(red)!=VAR_REF && NODE_CODE(red)!=ARRAY_REF) return 0; -for (ExprList=newlist;ExprList&&(NODE_CODE(ExprList)==EXPR_LIST);ExprList=NODE_OPERAND1(ExprList)) - { - if (NODE_CODE(NODE_OPERAND0(ExprList))==VAR_REF || NODE_CODE(NODE_OPERAND0(ExprList))==ARRAY_REF ) - if (NODE_SYMB(NODE_OPERAND0(ExprList))==NODE_SYMB(red)) - return 1; - } -return 0; -} - -int isForNodeEndStmt(PTR_BFND stmt) -{ - PTR_LABEL lab, do_lab; - PTR_BFND parent; - if(!(lab=BIF_LABEL(stmt)) && BIF_CODE(stmt) != CONTROL_END) /*the statement has no label and*/ - return(0); /*is not ENDDO */ - parent = BIF_CP(stmt); - if (parent) - { - if(BIF_CODE(parent)!=FOR_NODE) /*parent isn't DO statement*/ - return(0); - do_lab = BIF_LABEL_USE(parent); /* label of loop end or NULL*/ - if(do_lab) /* DO statement with label */ - if(lab && LABEL_STMTNO(lab) == LABEL_STMTNO(do_lab)) - /* the statement label is the label of loop end*/ - return(1); - else - return(0); - else /* DO statement without label */ - if(BIF_CODE(stmt) == CONTROL_END) - return(1); - else - return(0); - } - else return (0); -} -int ForNodeStmt(PTR_BFND stmt) -{ -PTR_BFND bif; -int count=0,label; -for(bif=stmt;bif;bif=BIF_NEXT(bif)) - { - /*BufPutString("Count=",0); - BufPutInt(count);*/ - if (BIF_CODE(bif)==ASSIGN_STAT) - { - if (FindRedInExpr(BIF_LL1(bif),BIF_LL2 (bif))) - { - if (BIF_LL1(bif)&&NODE_SYMB(BIF_LL1(bif))&&(!IS_DISTRIBUTE_ARRAY(NODE_SYMB(BIF_LL1(bif))))&&(!SYMB_DOVAR(NODE_SYMB(BIF_LL1(bif))))) - { - /*BufPutString("Reduction var :",0); - Tool_Unparse2_LLnode(BIF_LL1(bif)); - BufPutString("\n",0);*/ - if (!FindInNewList(NewSpecList,BIF_LL1(bif))) - if(!FindInNewList(ReductionList,BIF_LL1(bif))) - { - ReductionList=AddToReductionList(ReductionList,BIF_LL1(bif)); - /*BufPutString("Reduction LIST :",0); - Tool_Unparse2_LLnode(ReductionList); - BufPutString("\n",0);*/ - } - } - } - continue; - } - if (BIF_CODE(bif)==FOR_NODE) - { - count++; - continue; - } - else - { - if (isForNodeEndStmt(bif)) - { - if (BIF_CODE(bif)==CONTROL_END) - count--; - else - count-=NumberOfForNode(bif,&label); - } - } - if (!count) break; - } -return 0; -} - -PTR_LLND FreeReductionList(PTR_LLND redlist) -{ -PTR_LLND llnd,ptr; -llnd=redlist; -while((ptr=llnd)) - { - if (NODE_OPERAND1(llnd) == NULL) - break; - llnd = NODE_OPERAND1(llnd); - NODE_OPERAND0(ptr)=NULL; - NODE_OPERAND1(ptr)=NULL; -#ifdef __SPF - removeFromCollection(ptr); -#endif - free(ptr); - } -return (NULL); -} - -int ForNodeLabel(PTR_BFND stmt) -{ -PTR_BFND bif; -int count=0,label; -/*int LABEL=0;*/ -for(bif=stmt;bif;bif=BIF_NEXT(bif)) - { - if (BIF_CODE(bif)==FOR_NODE) - { - count++; - continue; - } - else - { - if (isForNodeEndStmt(bif)) - { - if (BIF_CODE(bif)==CONTROL_END) - count--; - else - count-=NumberOfForNode(bif,&label); - } - } - if (!count) break; - } -return 0; -} - - - - - - - - - + /************************************************************************** + * * + * Unparser for toolbox * + * * + *************************************************************************/ + +#include + +#include "compatible.h" /* Make different system compatible... (PHB) */ +#ifdef SYS5 +#include +#else +#include +#endif +#include + +#include "dvm_tag.h" +#include "fdvm.h" +#include "macro.h" +#include "ext_lib.h" +#include "ext_low.h" +static int TabNumber =0; +static int Number_Of_Flag = 0; +#define TASK_PROC_GENERATE 0 +#define MAXFLAG 64 +#define MAXLFLAG 256 +#define MAXLEVEL 256 +#define IS_DISTRIBUTE_ARRAY(A) ((SYMB_ATTR((A)) & DISTRIBUTE_BIT) || (SYMB_ATTR((A)) & ALIGN_BIT) || (SYMB_ATTR((A)) & INHERIT_BIT)) +char *copys(char *); + +PTR_SYMB SymbolID[MAXFLAG]; +int Number_Of_Symbol = 0; +int On_count = 0; +int TaskRegionUnparse = 0; +int HPF_VERSION=0; +int errnumber=0; +int NumberOfIndependent=0; +static char TabOfFlag[MAXFLAG][MAXLFLAG]; +static int FlagLenght[MAXFLAG]; +static int FlagLevel[MAXFLAG]; +static int FlagOn[MAXLEVEL][MAXFLAG]; +#define MAXLENGHTBUF 750000 +static int Buf_pointer = 0; +static char UnpBuf[MAXLENGHTBUF]; +static char *Buf_address; +#ifdef __SPF_BUILT_IN_PARSER +static int CommentOut = 0; +#else +int CommentOut = 0; +#endif +int Pointer = 0; +char *hpfname; +#define C_Initialized 1 +#define Fortran_Initialized 2 +static int Parser_Initiated = 0; + +#ifdef __SPF +extern void removeFromCollection(void *pointer); +#endif + +#ifdef __SPF_BUILT_IN_PARSER + +static PTR_FILE current_file = NULL; +#define DealWith_Rid DealWith_Rid_temp +#define is_overloaded_type is_overloaded_type_temp +#define Find_Type_For_Bif Find_Type_For_Bif_temp +#define Find_Protection_For_Bif Find_Protection_For_Bif_temp +#define Find_BaseType Find_BaseType_temp +#define Find_BaseType2 Find_BaseType2_temp +#define create_unp_str create_unp_str_temp +#define alloc_str alloc_str_temp +#define Reset_Unparser Reset_Unparser_temp +#define BufPutChar BufPutChar_temp +#define BufPutString BufPutString_temp +#define BufPutInt BufPutInt_temp +#define Get_Flag_val Get_Flag_val_temp +#define Treat_Flag Treat_Flag_temp +#define PushPop_Flag PushPop_Flag_temp +#define Tool_Unparse_Symbol Tool_Unparse_Symbol_temp +#define Get_Type_Operand Get_Type_Operand_temp +#define Get_LL_Operand Get_LL_Operand_temp +#define Get_Bif_Operand Get_Bif_Operand_temp +#define GetComp GetComp_temp +#define Eval_Type_Condition Eval_Type_Condition_temp +#define Eval_LLND_Condition Eval_LLND_Condition_temp +#define Eval_Bif_Condition Eval_Bif_Condition_temp +#define SkipToEndif SkipToEndif_temp +#define Tool_Unparse_Type Tool_Unparse_Type_temp +#define Tool_Unparse2_LLnode Tool_Unparse2_LLnode_temp +#define Tool_Unparse_Bif Tool_Unparse_Bif_temp + +#else +PTR_FILE current_file=NULL; +#endif + +PTR_LLND On_Clause=NULL; +PTR_LLND ReductionList=NULL; +PTR_LLND NewSpecList=NULL; +extern void Message(); +/* FORWARD DECLARATIONS */ +int BufPutString(); +PTR_LLND FindMapDir(); +PTR_LLND ChangeRedistributeOntoTask(); +PTR_LLND FindRealignDir(); +PTR_LLND FindRedistributeDir(); +PTR_LLND FindDynamicDir(); +int UnparseEndofCircle(); +PTR_BFND FindDistrAlignCombinedDir(); +PTR_LLND FindPointerDescriptor(); +void gen_hpf_name (); +void PointerDeclaration(); +int ArrayOfPointerDeclaration(); +void GenerateType(); +void Init_HPFUnparser(); +void ResetSymbolId(); +int NumberOfForNode(); +int FindPointerDir(); +int FindPointerDeclaration(); +int FindCommonHeapDeclaration(); +int Puttab(); +int Find_SaveSymbol(); +int CheckNullDistribution(); +char *Tool_Unparse_Bif(); +char *Tool_Unparse2_LLnode (); +char * Tool_Unparse_Type(); +char * Tool_Unparse_Symbol(); +PTR_BFND FindBeginingOfBlock(); +PTR_BFND FindEndOfBlock(); +int CheckAcross(); +int CheckReduction(); +int IfReduction(); +int FindRedInExpr(); +PTR_LLND AddToReductionList(); +int FindInNewList(); +int isForNodeEndStmt(); +int ForNodeStmt(); +PTR_LLND FreeReductionList(); + +#include "f90.h" + +typedef struct +{ + char *str; + char *(* fct)(); +} UNP_EXPR; + + +static UNP_EXPR Unparse_Def[LAST_CODE]; + +/************ Unparse Flags **************/ +static int In_Write_Flag = 0; +static int Rec_Port_Decl = 0; +static int In_Param_Flag = 0; +static int In_Impli_Flag = 0; +static int In_Class_Flag = 0; +/*****************************************/ + +/*************** TYPE names in ASCII form ****************/ +static char *ftype_name[] = {"integer", + "real", + "double precision", + "character", + "logical", + "character", + "gate", + "event", + "sequence", + "", + "", + "", + "", + "complex", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "double complex" +};static char *ctype_name[] = {"int", + "float", + "double", + "char", + "logical", + "char", + "gate", + "event", + "sequence", + "error1", + "error2", + "error3", + "error4", + "complex", + "void", + "error6", + "error7", + "error8", + "error9", + "error10", + "error11", + "error12", + "ElementType", + "error14", + "error15", + "error16", + "error17", + "error18", + "error19", + "error20", + "error21", + "error22", + "error23" +}; + +static +char *ridpointers[] = { + "-error1-", /* unused */ + "-error2-", /* int */ + "char", /* char */ + "float", /* float */ + "double", /* double */ + "void", /* void */ + "-error3-", /* unused1 */ + "unsigned", /* unsigned */ + "short", /* short */ + "long", /* long */ + "auto", /* auto */ + "static", /* static */ + "extern", /* extern */ + "register", /* register */ + "typedef", /* typedef */ + "signed", /* signed */ + "const", /* const */ + "volatile", /* volatile */ + "private", /* private */ + "future", /* future */ + "virtual", /* virtual */ + "inline", /* inline */ + "friend", /* friend */ + "-error4-", /* public */ + "-error5-", /* protected */ + "Sync", /* CC++ sync */ + "global", /* CC++ global */ + "atomic", /* CC++ atomic */ + "__private", /* for KSR */ + "restrict" +}; + +/*********************************************************/ + +/******* Precedence table of operators for Fortran *******/ +static char precedence[] = {5, /* .eq. */ + 5, /* .lt. */ + 5, /* .gt. */ + 5, /* .ne. */ + 5, /* .le. */ + 5, /* .ge. */ + 3, /* + */ + 3, /* - */ + 8, /* .or. */ + 2, /* * */ + 2, /* / */ + 0, /* none */ + 7, /* .and. */ + 1, /* ** */ + 0, /* none */ + 4, /* // */ + 8, /* .xor. */ + 9, /* .eqv. */ + 9, /* .neqv. */ + 1, /* Minus_op*/ + 1 /* not op */ + }; + +#define type_index(X) (X-T_INT) /* gives the index of a type to access the Table "ftype_name" from a type code */ +#define binop(n) (n >= EQ_OP && n <= NEQV_OP) /* gives the boolean value of the operation "n" being binary (not unary) */ +/* In order to change ON-block procedure call */ +typedef struct func_call *PTR_FCALL; + +struct func_call + { + PTR_LLND func_ref; + PTR_BFND first; + PTR_BFND last; + PTR_FCALL next; + }; + +PTR_LLND parameter_list=NULL; +PTR_SYMB function_name=NULL; +int ON_BLOCK=0; +int ON_BEGIN=0; +PTR_FCALL TaskRegion=NULL; +extern char *chkalloc(); +#define ALLOC(x) (struct x *) chkalloc(sizeof(struct x)) +#define FUNC_REF(NODE) ((NODE)->func_ref) +#define FUNC_FIRST(NODE) ((NODE)->first) +#define FUNC_LAST(NODE) ((NODE)->last) +#define FUNC_NEXT(NODE) ((NODE)->next) + +void UnparseTaskRegion(PTR_FCALL TaskRegion) +{ +PTR_BFND bif; +PTR_FCALL temp=NULL; +TaskRegionUnparse=1; +for(;TaskRegion;temp=TaskRegion,TaskRegion=FUNC_NEXT(TaskRegion)) + { + PTR_LLND llnd; + if (temp) + { + FUNC_NEXT(temp)=NULL; + FUNC_FIRST(temp)=NULL; + FUNC_LAST(temp)=NULL; +#ifdef __SPF + removeFromCollection(FUNC_REF(temp)); + removeFromCollection(temp); +#endif + free(FUNC_REF(temp)); + free(temp); + } + BufPutString("\n",0); + Puttab(); + BufPutString("subroutine ",0); + Tool_Unparse2_LLnode(FUNC_REF(TaskRegion)); + BufPutString("\n",0); + llnd=NODE_OPERAND0(FUNC_REF(TaskRegion)); + for(;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + if (NODE_CODE(NODE_OPERAND0(llnd))==ARRAY_REF) + { + if (NODE_SYMB(NODE_OPERAND0(llnd))) + { + PTR_SYMB sym=NODE_SYMB(NODE_OPERAND0(llnd)); + if ((SYMB_ATTR(sym)&DISTRIBUTE_BIT)|| + (SYMB_ATTR(sym)&ALIGN_BIT)) + { + Puttab(); + BufPutString("DIMENSION ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString("(",0); + Tool_Unparse2_LLnode(TYPE_DECL_RANGES(SYMB_TYPE(sym))); + BufPutString(")",0); + BufPutString("\n",0); + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("INHERIT ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString("\n",0); + } + else + { + Puttab(); + BufPutString("DIMENSION ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString("(",0); + Tool_Unparse2_LLnode(TYPE_DECL_RANGES(SYMB_TYPE(sym))); + BufPutString(")",0); + BufPutString("\n",0); + } + } + } + else + { + PTR_SYMB sym=NODE_SYMB(NODE_OPERAND0(llnd)); + if ((NODE_CODE(NODE_OPERAND0(llnd))==CONST_REF)|| + (NODE_CODE(NODE_OPERAND0(llnd))==VAR_REF)) + { + Puttab(); + Tool_Unparse_Type(SYMB_TYPE(sym)); + BufPutString(" ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString("\n",0); + } + } + } + for(bif=BIF_NEXT(FUNC_FIRST(TaskRegion));bif&&(bif!=FUNC_LAST(TaskRegion));bif=BIF_NEXT(bif)) + Tool_Unparse_Bif(bif); + Puttab(); + BufPutString("end \n\n",0); + } +if (temp) + { + FUNC_NEXT(temp)=NULL; + FUNC_FIRST(temp)=NULL; + FUNC_LAST(temp)=NULL; +#ifdef __SPF + removeFromCollection(FUNC_REF(temp)); + removeFromCollection(temp); +#endif + free(FUNC_REF(temp)); + free(temp); + } + +TaskRegionUnparse=0; +} + + +PTR_FCALL +FindLast ( ptr ) +PTR_FCALL ptr; +{ +PTR_FCALL ptr_func=ptr; +if (!ptr) return NULL; +while(ptr_func) + { + if (FUNC_NEXT(ptr_func) == NULL) + return ptr_func; + ptr_func = FUNC_NEXT(ptr_func); + } +return NULL; +} + +PTR_LLND +make_llnode (node_type, ll1, ll2, symb_ptr) + int node_type; + PTR_LLND ll1, ll2; + PTR_SYMB symb_ptr; +{ + PTR_LLND new_llnd; + + new_llnd = ALLOC (llnd); + new_llnd->variant = node_type; + new_llnd->type = TYNULL; + new_llnd->entry.Template.ll_ptr1 = ll1; + new_llnd->entry.Template.ll_ptr2 = ll2; + switch (node_type) { + case INT_VAL: + /*new_llnd->entry.ival = (int) symb_ptr;*/ + break; + case BOOL_VAL: + /*new_llnd->entry.bval = (int) symb_ptr;*/ + break; + default: + new_llnd->entry.Template.symbol = symb_ptr; + break; + } + return (new_llnd); +} + +PTR_SYMB +make_funcsymb (string) + char *string; +{ + PTR_SYMB new_symb; + new_symb = ALLOC (symb); + new_symb->variant = ARRAY_REF; + new_symb->ident = copys (string); + return (new_symb); +} + +void ResetSymbolDovar() +{ +PTR_SYMB symb; +for (symb = current_file->head_symb; symb ; symb = SYMB_NEXT (symb)) + if (SYMB_DOVAR (symb)) SYMB_DOVAR (symb) &=~2 ; +} + + + +/* In order to change ON-block procedure call end*/ + +/* manage the unparse buffer */ + +void DealWith_Rid(typei, flg) + PTR_TYPE typei; + int flg; /* if 1 then do virtual */ +{ int j; + + int index; + PTR_TYPE type; + if (!typei) + return; + + for (type = typei; type; ) + { + switch(TYPE_CODE(type)) + { + case T_POINTER : + case T_REFERENCE : + case T_FUNCTION : + case T_ARRAY : + type = TYPE_BASE(type); + break; + case T_MEMBER_POINTER: + type = TYPE_COLL_BASE(type); + case T_DESCRIPT : + index = TYPE_LONG_SHORT(type); + /* printf("index = %d\n", index); */ + if( index & BIT_RESTRICT) { + BufPutString(ridpointers[(int)RID_RESTRICT],0); + BufPutString(" ", 0); + } + if( index & BIT_KSRPRIVATE) { + BufPutString(ridpointers[(int)RID_KSRPRIVATE],0); + BufPutString(" ", 0); + } + if( index & BIT_EXTERN) { + BufPutString(ridpointers[(int)RID_EXTERN],0); + BufPutString(" ", 0); + } + if( index & BIT_TYPEDEF) { + BufPutString(ridpointers[(int)RID_TYPEDEF],0); + BufPutString(" ", 0); + } + for (j=1; j< MAX_BIT; j= j*2) + { + switch (index & j) + { + case (int) BIT_PRIVATE: BufPutString(ridpointers[(int)RID_PRIVATE],0); + break; + case (int) BIT_FUTURE: BufPutString(ridpointers[(int)RID_FUTURE],0); + break; + case (int) BIT_VIRTUAL: if(flg) BufPutString(ridpointers[(int)RID_VIRTUAL],0); + break; + case (int) BIT_ATOMIC: if(flg) BufPutString(ridpointers[(int)RID_ATOMIC],0); + break; + case (int) BIT_INLINE: BufPutString(ridpointers[(int)RID_INLINE],0); + break; + case (int) BIT_UNSIGNED: BufPutString(ridpointers[(int)RID_UNSIGNED],0); + break; + case (int) BIT_SIGNED : BufPutString(ridpointers[(int)RID_SIGNED],0); + break; + case (int) BIT_SHORT : BufPutString(ridpointers[(int)RID_SHORT],0); + break; + case (int) BIT_LONG : BufPutString(ridpointers[(int)RID_LONG],0); + break; + case (int) BIT_VOLATILE: BufPutString(ridpointers[(int)RID_VOLATILE],0); + break; + case (int) BIT_CONST : BufPutString(ridpointers[(int)RID_CONST],0); + break; + case (int) BIT_GLOBL : BufPutString(ridpointers[(int)RID_GLOBL],0); + break; + case (int) BIT_SYNC : BufPutString(ridpointers[(int)RID_SYNC],0); + break; + case (int) BIT_TYPEDEF : /* BufPutString(ridpointers[(int)RID_TYPEDEF],0); */ + break; + case (int) BIT_EXTERN : /* BufPutString(ridpointers[(int)RID_EXTERN],0); */ + break; + case (int) BIT_AUTO : BufPutString(ridpointers[(int)RID_AUTO],0); + break; + case (int) BIT_STATIC : BufPutString(ridpointers[(int)RID_STATIC],0); + break; + case (int) BIT_REGISTER: BufPutString(ridpointers[(int)RID_REGISTER],0); + break; + case (int) BIT_FRIEND: BufPutString(ridpointers[(int)RID_FRIEND],0); + + } + if ((index & j) != 0) + BufPutString(" ",0); + } + type = TYPE_DESCRIP_BASE_TYPE(type); + break; + default: + type = NULL; + } + } +} + +int is_overloaded_type(bif) + PTR_BFND bif; +{ + PTR_LLND ll; + if(!bif) return 0; + ll = BIF_LL1(bif); + while(ll && (NODE_SYMB(ll) == NULL)) ll = NODE_OPERAND0(ll); + if(ll == NULL) return 0; + if(SYMB_ATTR(NODE_SYMB(ll)) & OVOPERATOR) return 1; + else return 0; +} + +PTR_TYPE Find_Type_For_Bif(bif) + PTR_BFND bif; +{ + PTR_TYPE type = NULL; + if (BIF_LL1(bif) && (NODE_CODE(BIF_LL1(bif)) == EXPR_LIST)) + { PTR_LLND tp; + tp = BIF_LL1(bif); + for (tp = NODE_OPERAND0(tp); tp && (type == NULL); ) + { + switch (NODE_CODE(tp)) { + case BIT_NUMBER: + case ASSGN_OP : + case ARRAY_OP: + case FUNCTION_OP : + case CLASSINIT_OP: + case ADDRESS_OP: + case DEREF_OP : + tp = NODE_OPERAND0(tp); + break ; + case SCOPE_OP: + tp = NODE_OPERAND1(tp); + break; + case FUNCTION_REF: + case ARRAY_REF: + case VAR_REF: + if (tp) + { if(!NODE_SYMB(tp)){ + printf("syntax error at line %d\n", bif->g_line); + exit(1); + } + else + type = SYMB_TYPE(NODE_SYMB(tp)); + } + tp = NULL; + break ; + default: + type = NODE_TYPE(tp); + break; + } + } + } + return type; +} + +int Find_Protection_For_Bif(bif) + PTR_BFND bif; +{ + int protect = 0; + if (BIF_LL1(bif) && (BIF_CODE(BIF_LL1(bif)) == EXPR_LIST)) + { PTR_LLND tp; + tp = BIF_LL1(bif); + for (tp = NODE_OPERAND0(tp); tp && (protect == 0); ) /*(protect == NULL)*/ + { + switch (NODE_CODE(tp)) { + case BIT_NUMBER: + case ASSGN_OP : + case ARRAY_OP: + case FUNCTION_OP : + case CLASSINIT_OP: + case ADDRESS_OP: + case DEREF_OP : + tp = NODE_OPERAND0(tp); + break ; + case SCOPE_OP: + tp = NODE_OPERAND1(tp); + break; + case FUNCTION_REF: + case ARRAY_REF: + case VAR_REF: + if (tp) + protect = SYMB_ATTR(NODE_SYMB(tp)); + tp = NULL; + break ; + } + } + } + return protect; +} + +PTR_TYPE Find_BaseType(ptype) + PTR_TYPE ptype; +{ + PTR_TYPE pt; + + if (!ptype) + return NULL; + pt = TYPE_BASE (ptype); + if (pt) + { int j; + j = 0; + while ((j < 100) && pt) + { + if (TYPE_CODE(pt) == DEFAULT) break; + if (TYPE_CODE(pt) == T_INT) break; + if (TYPE_CODE(pt) == T_FLOAT) break; + if (TYPE_CODE(pt) == T_DOUBLE) break; + if (TYPE_CODE(pt) == T_CHAR) break; + if (TYPE_CODE(pt) == T_BOOL) break; + if (TYPE_CODE(pt) == T_STRING) break; + if (TYPE_CODE(pt) == T_COMPLEX) break; + if (TYPE_CODE(pt) == T_DCOMPLEX) break; + if (TYPE_CODE(pt) == T_VOID) break; + if (TYPE_CODE(pt) == T_UNKNOWN) break; + if (TYPE_CODE(pt) == T_DERIVED_TYPE) break; + if (TYPE_CODE(pt) == T_DERIVED_COLLECTION) break; + if (TYPE_CODE(pt) == T_DERIVED_TEMPLATE) break; + if (TYPE_CODE(pt) == T_DERIVED_CLASS) break; + if (TYPE_CODE(pt) == T_CLASS) break; + if (TYPE_CODE(pt) == T_COLLECTION) break; + if (TYPE_CODE(pt) == T_DESCRIPT) break; /* by dbg */ + + pt = TYPE_BASE (pt); + j++; + } + if (j == 100) + { + Message("Looping in getting the Basetype; sorry",0); + exit(1); + } + } + return pt; +} + +PTR_TYPE Find_BaseType2(ptype) /* breaks out of the loop for pointers and references BW */ + PTR_TYPE ptype; +{ + PTR_TYPE pt; + + if (!ptype) + return NULL; + pt = TYPE_BASE (ptype); + if (pt) + { int j; + j = 0; + while ((j < 100) && pt) + { + if (TYPE_CODE(pt) == T_REFERENCE) break; + if (TYPE_CODE(pt) == T_POINTER) break; + if (TYPE_CODE(pt) == DEFAULT) break; + if (TYPE_CODE(pt) == T_INT) break; + if (TYPE_CODE(pt) == T_FLOAT) break; + if (TYPE_CODE(pt) == T_DOUBLE) break; + if (TYPE_CODE(pt) == T_CHAR) break; + if (TYPE_CODE(pt) == T_BOOL) break; + if (TYPE_CODE(pt) == T_STRING) break; + if (TYPE_CODE(pt) == T_COMPLEX) break; + if (TYPE_CODE(pt) == T_DCOMPLEX) break; + if (TYPE_CODE(pt) == T_VOID) break; + if (TYPE_CODE(pt) == T_UNKNOWN) break; + if (TYPE_CODE(pt) == T_DERIVED_TYPE) break; + if (TYPE_CODE(pt) == T_DERIVED_COLLECTION) break; + if (TYPE_CODE(pt) == T_DERIVED_CLASS) break; + if (TYPE_CODE(pt) == T_CLASS) break; + if (TYPE_CODE(pt) == T_COLLECTION) break; + if (TYPE_CODE(pt) == T_DESCRIPT) break; /* by dbg */ + + pt = TYPE_BASE (pt); + j++; + } + if (j == 100) + { + Message("Looping in getting the Basetype; sorry",0); + exit(1); + } + } + return pt; +} + +char *create_unp_str(str) + char *str; +{ + char *pt; + + if (!str) + return NULL; + + pt = (char *) xmalloc((int)(strlen(str)+1)); + memset(pt, 0, strlen(str)+1); + strcpy(pt,str); + return pt; +} + +char *alloc_str(size) + int size; +{ + char *pt; + + if (!(size++)) return NULL; + pt = (char *) xmalloc(size); + memset(pt, 0, size); + return pt; +} + +int Reset_Unparser() +{ + int i,j; + + /* initialize the number of flag */ + Number_Of_Flag = 0; + for (i=0; i < MAXFLAG ; i++) + { + TabOfFlag[i][0] = '\0'; + FlagLenght[i] = 0; + for(j=0; j= MAXLENGHTBUF) + { + Message("Unparse Buffer Full",0); + return 0; + } + Buf_address[Buf_pointer] = c; + Buf_pointer++; + return 1; +} + +int BufPutString(s, len) + char *s; + int len; +{ + int length; + if (!s) + { + Message("Null String in BufPutString",0); + return 0; + } + length = len; + if (length <= 0) + length = strlen(s); + + if (Buf_pointer + length>= MAXLENGHTBUF) + { + Message("Unparse Buffer Full",0); + return 0; + } + strncpy(&(Buf_address[Buf_pointer]),s,length); + Buf_pointer += length; + return 1; +} + +int BufPutInt(i) + int i; +{ + int length; + char s[MAXLFLAG]; + + sprintf(s,"%d",i); + length = strlen(s); + + if (Buf_pointer + length>= MAXLENGHTBUF) + { + Message("Unparse Buffer Full",0); + return 0; + } + strncpy(&(Buf_address[Buf_pointer]),s,length); + Buf_pointer += length; + return 1; +} + +int Get_Flag_val(str, i) + char *str; + int *i; +{ + int j, con; + char sflag[MAXLFLAG]; + (*i)++; /* skip the paranthesis */ + /* extract the flag name */ + j = *i; + con = 0; + + while ((str[j] != '\0') && (str[j] != ')')) + { + sflag[con] = str[j]; + con ++; + j ++; + } + sflag[con] = '\0'; + con ++; + + /* look in table if flag is in */ + + for (j = 0 ; j < Number_Of_Flag; j++) + { + if (strncmp(TabOfFlag[j],sflag, con) == 0) + break; + } + *i += con; + if (j >= Number_Of_Flag) + { + /* not found */ + return 0; + } + else + return FlagOn[FlagLevel[j]][j]; + +} + +void Treat_Flag(str, i, val) + char *str; + int *i; + int val; +{ + int j, con; + char sflag[MAXLFLAG]; + (*i)++; /* skip the paranthesis */ + /* extract the flag name */ + j = *i; + con = 0; + + while ((str[j] != '\0') && (str[j] != ')')) + { + sflag[con] = str[j]; + con ++; + j ++; + } + sflag[con] = '\0'; + con ++; + + /* look in table if flag is in */ + + for (j = 0 ; j < Number_Of_Flag; j++) + { + if (strncmp(TabOfFlag[j],sflag, con) == 0) + break; + } + if (j >= Number_Of_Flag) + { + /* not found */ + strcpy(TabOfFlag[Number_Of_Flag],sflag); + FlagOn[0][Number_Of_Flag] = val; + FlagLenght[Number_Of_Flag] = con-1; + Number_Of_Flag++; + } else + FlagOn[FlagLevel[j]][j] += val; + *i += con; +} + +void PushPop_Flag(str, i, val) + char *str; + int *i; + int val; +{ + int j, con; + char sflag[MAXLFLAG]; + (*i)++; /* skip the paranthesis */ + /* extract the flag name */ + j = *i; + con = 0; + + while ((str[j] != '\0') && (str[j] != ')')) + { + sflag[con] = str[j]; + con ++; + j ++; + } + sflag[con] = '\0'; + con ++; + + /* look in table if flag is in */ + + for (j = 0 ; j < Number_Of_Flag; j++) + { + if (strncmp(TabOfFlag[j],sflag, con) == 0) + break; + } + if (j < Number_Of_Flag) + { + /* if a pop, clear old value befor poping */ + if(val< 0) FlagOn[FlagLevel[j]][j] = 0; /* added by dbg to make sure initialized */ + FlagLevel[j] += val; + if (FlagLevel[j] < 0) + FlagLevel[j] = 0; + if (FlagLevel[j] >= MAXLEVEL) + { + Message("Stack of flag overflow; abort()",0); + abort(); + } + } + /* else printf("WARNING(unparser): unknow flag pushed or popped:%s\n",sflag); */ + *i += con; +} + +char * Tool_Unparse_Type(); + +char *Tool_Unparse_Symbol (symb) + PTR_SYMB symb; +{ + PTR_TYPE ov_type; + if (!symb) + return NULL; + if (SYMB_IDENT(symb)) + { + if((SYMB_ATTR(symb) & OVOPERATOR)){ + ov_type = SYMB_TYPE(symb); + if(TYPE_CODE(ov_type) == T_DESCRIPT){ + if(TYPE_LONG_SHORT(ov_type) == BIT_VIRTUAL && In_Class_Flag){ + BufPutString ("virtual ",0); + if(TYPE_LONG_SHORT(ov_type) == BIT_ATOMIC) BufPutString ("atomic ",0); + ov_type = TYPE_DESCRIP_BASE_TYPE(ov_type); + } + if(TYPE_LONG_SHORT(ov_type) == BIT_INLINE){ + BufPutString ("inline ",0); + ov_type = TYPE_DESCRIP_BASE_TYPE(ov_type); + } + } + } else ov_type = NULL; + + if ((SYMB_ATTR(symb) & OVOPERATOR) || + (strcmp(SYMB_IDENT(symb),"()")==0) || + (strcmp(SYMB_IDENT(symb),"*")==0) || + (strcmp(SYMB_IDENT(symb),"+")==0) || + (strcmp(SYMB_IDENT(symb),"-")==0) || + (strcmp(SYMB_IDENT(symb),"/")==0) || + (strcmp(SYMB_IDENT(symb),"=")==0) || + (strcmp(SYMB_IDENT(symb),"%")==0) || + (strcmp(SYMB_IDENT(symb),"&")==0) || + (strcmp(SYMB_IDENT(symb),"|")==0) || + (strcmp(SYMB_IDENT(symb),"!")==0) || + (strcmp(SYMB_IDENT(symb),"~")==0) || + (strcmp(SYMB_IDENT(symb),"^")==0) || + (strcmp(SYMB_IDENT(symb),"+=")==0) || + (strcmp(SYMB_IDENT(symb),"-=")==0) || + (strcmp(SYMB_IDENT(symb),"*=")==0) || + (strcmp(SYMB_IDENT(symb),"/=")==0) || + (strcmp(SYMB_IDENT(symb),"%=")==0) || + (strcmp(SYMB_IDENT(symb),"^=")==0) || + (strcmp(SYMB_IDENT(symb),"&=")==0) || + (strcmp(SYMB_IDENT(symb),"|=")==0) || + (strcmp(SYMB_IDENT(symb),"<<")==0) || + (strcmp(SYMB_IDENT(symb),">>")==0) || + (strcmp(SYMB_IDENT(symb),"<<=")==0) || + (strcmp(SYMB_IDENT(symb),">>=")==0) || + (strcmp(SYMB_IDENT(symb),"==")==0) || + (strcmp(SYMB_IDENT(symb),"!=")==0) || + (strcmp(SYMB_IDENT(symb),"<=")==0) || + (strcmp(SYMB_IDENT(symb),">=")==0) || + (strcmp(SYMB_IDENT(symb),"<")==0) || + (strcmp(SYMB_IDENT(symb),">")==0) || + (strcmp(SYMB_IDENT(symb),"&&")==0) || + (strcmp(SYMB_IDENT(symb),"||")==0) || + (strcmp(SYMB_IDENT(symb),"++")==0) || + (strcmp(SYMB_IDENT(symb),"--")==0) || + (strcmp(SYMB_IDENT(symb),"->")==0) || + (strcmp(SYMB_IDENT(symb),"->*")==0) || + (strcmp(SYMB_IDENT(symb),",")==0) || + (strcmp(SYMB_IDENT(symb),"new")==0) || + (strcmp(SYMB_IDENT(symb),"delete")==0) || + (strcmp(SYMB_IDENT(symb),"[]")==0) ) + BufPutString ("operator ",0); + } + /* + if(ov_type) Tool_Unparse_Type(ov_type, 0); + else */ + BufPutString (SYMB_IDENT(symb),0); + return Buf_address; +} + + +typedef struct +{ + int typ; + union {char *S; + long I; + } val; +} operand; + +/* macro def. of operand type */ +#define UNDEF_TYP 0 +#define STRING_TYP 1 +#define INTEGER_TYP 2 + +/* macro def. of comparison operators */ +#define COMP_UNDEF -1 /* Bodin */ +#define COMP_EQUAL 0 +#define COMP_DIFF 1 + + +void Get_Type_Operand (str, iptr, ptype,Op) + char *str; + int *iptr; + PTR_TYPE ptype; + operand *Op; +{ + + Op->typ = UNDEF_TYP; + if (strncmp(&(str[*iptr]),"%CHECKFLAG", strlen("%CHECKFLAG"))== 0) + { + Op->typ = INTEGER_TYP; + *iptr += strlen("%CHECKFLAG"); + Op->val.I = Get_Flag_val(str, iptr); + } else + if (strncmp(&(str[*iptr]),"%STRCST", strlen("%STRCST"))== 0) /* %STRCST : String Constant */ + { + int i_save; + + *iptr += strlen("%STRCST"); + while (str[*iptr] == ' ') {(*iptr)++;} /* skip spaces before string */ + if (str[*iptr] != '\'') + { + Message (" *** Missing \"'\" after %STRCST *** ",0); + } + i_save = ++(*iptr); + while ((str[*iptr] != '\0') && (str[*iptr] != '\'')) (*iptr)++; + Op->val.S = alloc_str ((*iptr) - i_save); + strncpy (Op->val.S, &(str[i_save]), (*iptr) - i_save); + Op->typ = STRING_TYP; + } else + if (strncmp(&(str[*iptr]),"%NULL", strlen("%NULL"))== 0) /* %NULL : Integer Constant (or false boolean) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = 0; + *iptr += strlen("%NULL"); + } else + if (strncmp(&(str[*iptr]),"%INIMPLI", strlen("%INIMPLI"))== 0) /* %INIMPLI : In_Impli_Statement (integer / boolean flag) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = In_Impli_Flag; + *iptr += strlen("%INIMPLI"); + } else + { + Message (" *** Unknown operand in %IF (condition) for Type Node *** ",0); + } +} + +void Get_LL_Operand (str, iptr, ll, Op) + char *str; + int *iptr; + PTR_LLND ll; + operand *Op; +{ + + Op->typ = UNDEF_TYP; + if (strncmp(&(str[*iptr]),"%CHECKFLAG", strlen("%CHECKFLAG"))== 0) + { + Op->typ = INTEGER_TYP; + *iptr += strlen("%CHECKFLAG"); + Op->val.I = Get_Flag_val(str, iptr); + } else + if (strncmp(&(str[*iptr]),"%STRCST", strlen("%STRCST"))== 0) /* %STRCST : String Constant */ + { + int i_save; + + *iptr += strlen("%STRCST"); + while (str[*iptr] == ' ') {(*iptr)++;} /* skip spaces before string */ + if (str[*iptr] != '\'') + { + Message (" *** Missing \"'\" after %STRCST *** ",0); + } + i_save = ++(*iptr); + while ((str[*iptr] != '\0') && (str[*iptr] != '\'')) (*iptr)++; + Op->val.S = alloc_str ((*iptr) - i_save); + strncpy (Op->val.S, &(str[i_save]), (*iptr) - i_save); + Op->typ = STRING_TYP; + } else + if (strncmp(&(str[*iptr]),"%SYMBOL", strlen("%SYMBOL"))== 0) /* %SYMBOL : Symbol pointer (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) NODE_SYMB (ll); + *iptr += strlen("%SYMBOL"); + } else + if (strncmp(&(str[*iptr]),"%SYMBID", strlen("%SYMBID"))== 0) /* %SYMBID : Symbol identifier (string) */ + { + Op->typ = STRING_TYP; + if (NODE_SYMB (ll)) + Op->val.S = SYMB_IDENT (NODE_SYMB (ll)); + else + Op->val.S = NULL; + *iptr += strlen("%SYMBID"); + } else + if (strncmp(&(str[*iptr]),"%NULL", strlen("%NULL"))== 0) /* %NULL : Integer Constant (or false boolean) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = 0; + *iptr += strlen("%NULL"); + } else + if (strncmp(&(str[*iptr]),"%LL1", strlen("%LL1"))== 0) /* %LL1 : Low Level Node 1 (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) NODE_TEMPLATE_LL1 (ll); + *iptr += strlen("%LL1"); + } else + if (strncmp(&(str[*iptr]),"%LL2", strlen("%LL2"))== 0) /* %LL2 : Low Level Node 2 (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) NODE_TEMPLATE_LL2 (ll); + *iptr += strlen("%LL2"); + } else + if (strncmp(&(str[*iptr]),"%LABUSE", strlen("%LABUSE"))== 0) /* %LABUSE : label ptr (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) NODE_LABEL (ll); + *iptr += strlen("%LABUSE"); + } else + if (strncmp(&(str[*iptr]),"%L1CODE", strlen("%L1CODE"))== 0) /* %L1CODE : Code (variant) of Low Level Node 1 (integer) */ + { + Op->typ = INTEGER_TYP; + if (NODE_TEMPLATE_LL1 (ll)) + Op->val.I = NODE_CODE (NODE_TEMPLATE_LL1 (ll)); + else + Op->val.I = (long) NULL; + *iptr += strlen("%L1CODE"); + } else + if (strncmp(&(str[*iptr]),"%L2CODE", strlen("%L2CODE"))== 0) /* %L2CODE : Code (variant) of Low Level Node 2 (integer) */ + { + Op->typ = INTEGER_TYP; + if (NODE_TEMPLATE_LL2 (ll)) + Op->val.I = NODE_CODE (NODE_TEMPLATE_LL2 (ll)); + else + Op->val.I = (long) NULL; + *iptr += strlen("%L2CODE"); + } else + if (strncmp(&(str[*iptr]),"%INWRITE", strlen("%INWRITE"))== 0) /* %INWRITE : In_Write_Statement (integer / boolean flag) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = In_Write_Flag; + *iptr += strlen("%INWRITE"); + } else + if (strncmp(&(str[*iptr]),"%RECPORT", strlen("%RECPORT"))== 0) /* %RECPORT : reccursive_port_decl (integer / boolean flag) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = Rec_Port_Decl; + *iptr += strlen("%RECPORT"); + } else + if (strncmp(&(str[*iptr]),"%INPARAM", strlen("%INPARAM"))== 0) /* %INPARAM : In_Param_Statement (integer / boolean flag) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = In_Param_Flag; + *iptr += strlen("%INPARAM"); + } else + if (strncmp(&(str[*iptr]),"%INIMPLI", strlen("%INIMPLI"))== 0) /* %INIMPLI : In_Impli_Statement (integer / boolean flag) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = In_Impli_Flag; + *iptr += strlen("%INIMPLI"); + } else + if (strncmp(&(str[*iptr]),"%L1L2*L1CODE", strlen("%L1L2*L1CODE"))== 0) /* %L1L2L1CODE : Code (variant) of Low Level Node 1 of Low Level Node 2 of Low Level Node 1 (integer) */ + { + PTR_LLND temp; + + Op->typ = INTEGER_TYP; + if (NODE_OPERAND0(ll)) + { + temp = NODE_OPERAND0(ll); + while (temp && NODE_OPERAND1(temp)) temp = NODE_OPERAND1(temp); + if (temp && NODE_OPERAND0(temp)) + Op->val.I = NODE_CODE (NODE_OPERAND0(temp)); + else + Op->val.I = (long) NULL; + } + else + Op->val.I = (long) NULL; + *iptr += strlen("%L1L2*L1CODE"); + } else + { + Message (" *** Unknown operand in %IF (condition) for LL Node *** ",0); + } +} + +void Get_Bif_Operand (str, iptr, bif,Op) + char *str; + int *iptr; + PTR_BFND bif; + operand *Op; +{ + + Op->typ = UNDEF_TYP; + if (strncmp(&(str[*iptr]),"%ELSIFBLOB2", strlen("%ELSIFBLOB2"))== 0) + { + Op->typ = INTEGER_TYP; + *iptr += strlen("%ELSIFBLOB2"); + if (BIF_BLOB2(bif) && (BIF_CODE(BLOB_VALUE(BIF_BLOB2(bif))) == ELSEIF_NODE)) + Op->val.I = 1; + else + Op->val.I = 0; + } else + if (strncmp(&(str[*iptr]),"%LABEL", strlen("%LABEL"))== 0) + { + Op->typ = INTEGER_TYP; + *iptr += strlen("%LABEL"); + Op->val.I = (long) BIF_LABEL(bif); + } else + if (strncmp(&(str[*iptr]),"%CHECKFLAG", strlen("%CHECKFLAG"))== 0) + { + Op->typ = INTEGER_TYP; + *iptr += strlen("%CHECKFLAG"); + Op->val.I = Get_Flag_val(str, iptr); + } else + if (strncmp(&(str[*iptr]),"%BLOB1", strlen("%BLOB1"))== 0) + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_BLOB1(bif); + *iptr += strlen("%BLOB1"); + } else + if (strncmp(&(str[*iptr]),"%BLOB2", strlen("%BLOB2"))== 0) + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_BLOB2(bif); + *iptr += strlen("%BLOB2"); + } else + if (strncmp(&(str[*iptr]),"%BIFCP", strlen("%BIFCP"))== 0) + { + Op->typ = INTEGER_TYP; + if (BIF_CP(bif)) + Op->val.I = BIF_CODE(BIF_CP(bif)); + else + Op->val.I = 0; + *iptr += strlen("%BIFCP"); + } else + if (strncmp(&(str[*iptr]),"%VALINT", strlen("%VALINT"))== 0) + { + Op->typ = INTEGER_TYP; + Op->val.I = atoi(&(str[*iptr + strlen("%VALINT")])); /* %VALINT-12232323 space is necessary after the number*/ + /* skip to next statement */ + while (str[*iptr] != ' ') (*iptr)++; + } else + if (strncmp(&(str[*iptr]),"%RECURSBIT", strlen("%RECURSBIT"))== 0) /* %RECURSBIT : Symbol Attribut (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = RECURSIVE_BIT; + *iptr += strlen("%RECURSBIT"); + } else + if (strncmp(&(str[*iptr]),"%EXPR_LIST", strlen("%EXPR_LIST"))== 0) /* %EXPR_LIST : int constant EXPR_LIST code for Low Level Node (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = EXPR_LIST; + *iptr += strlen("%EXPR_LIST"); + } else + if (strncmp(&(str[*iptr]),"%SPEC_PAIR", strlen("%SPEC_PAIR"))== 0) /* %SPEC_PAIR : int constant SPEC_PAIR code for Low Level Node (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = SPEC_PAIR; + *iptr += strlen("%SPEC_PAIR"); + } else + if (strncmp(&(str[*iptr]),"%IOACCESS", strlen("%IOACCESS"))== 0) /* %IOACCESS : int constant IOACCESS code for Low Level Node (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = IOACCESS; + *iptr += strlen("%IOACCESS"); + } else + if (strncmp(&(str[*iptr]),"%STRCST", strlen("%STRCST"))== 0) /* %STRCST : String Constant */ + { + int i_save; + + *iptr += strlen("%STRCST"); + while (str[*iptr] == ' ') {(*iptr)++;} /* skip spaces before string */ + if (str[*iptr] != '\'') + { + Message (" *** Missing \"'\" after %STRCST *** ",0); + } + i_save = ++(*iptr); + while ((str[*iptr] != '\0') && (str[*iptr] != '\'')) (*iptr)++; + Op->val.S = alloc_str ((*iptr) - i_save); + strncpy (Op->val.S, &(str[i_save]), (*iptr) - i_save); + Op->typ = STRING_TYP; + (*iptr)++; /* skip the ' */ + } else + if (strncmp(&(str[*iptr]),"%SYMBOL", strlen("%SYMBOL"))== 0) /* %SYMBOL : Symbol pointer (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_SYMB (bif); + *iptr += strlen("%SYMBOL"); + } else + if (strncmp(&(str[*iptr]),"%SATTR", strlen("%SATTR"))== 0) /* %SATTR : Symbol Attribut (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (BIF_SYMB (bif))->attr; + *iptr += strlen("%SATTR"); + } else + if (strncmp(&(str[*iptr]),"%SYMBID", strlen("%SYMBID"))== 0) /* %SYMBID : Symbol identifier (string) */ + { + Op->typ = STRING_TYP; + if (BIF_SYMB (bif)) + Op->val.S = SYMB_IDENT (BIF_SYMB (bif)); + else + Op->val.S = NULL; + *iptr += strlen("%SYMBID"); + } else + if (strncmp(&(str[*iptr]),"%NULL", strlen("%NULL"))== 0) /* %NULL : Integer Constant (or false boolean) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = 0; + *iptr += strlen("%NULL"); + } else + if (strncmp(&(str[*iptr]),"%LL1", strlen("%LL1"))== 0) /* %LL1 : Low Level Node 1 (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_LL1 (bif); + *iptr += strlen("%LL1"); + } else + if (strncmp(&(str[*iptr]),"%LL2", strlen("%LL2"))== 0) /* %LL2 : Low Level Node 2 (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_LL2 (bif); + *iptr += strlen("%LL2"); + } else + if (strncmp(&(str[*iptr]),"%LL3", strlen("%LL3"))== 0) /* %LL3 : Low Level Node 3 (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_LL3 (bif); + *iptr += strlen("%LL3"); + } else + if (strncmp(&(str[*iptr]),"%LABUSE", strlen("%LABUSE"))== 0) /* %LABUSE : label ptr (used for do : doend) (integer) */ + { + Op->typ = INTEGER_TYP; + Op->val.I = (long) BIF_LABEL_USE (bif); + *iptr += strlen("%LABUSE"); + } else + if (strncmp(&(str[*iptr]),"%L1CODE", strlen("%L1CODE"))== 0) /* %L1CODE : Code (variant) of Low Level Node 1 (integer) */ + { + Op->typ = INTEGER_TYP; + if (BIF_LL1 (bif)) + Op->val.I = NODE_CODE (BIF_LL1 (bif)); + else + Op->val.I = (long) NULL; + *iptr += strlen("%L1CODE"); + } else + if (strncmp(&(str[*iptr]),"%L2CODE", strlen("%L2CODE"))== 0) /* %L2CODE : Code (variant) of Low Level Node 2 (integer) */ + { + Op->typ = INTEGER_TYP; + if (BIF_LL2 (bif)) + Op->val.I = NODE_CODE (BIF_LL2 (bif)); + else + Op->val.I = (long) NULL; + *iptr += strlen("%L2CODE"); + } else + if (strncmp(&(str[*iptr]),"%L1L2L1CODE", strlen("%L1L2L1CODE"))== 0) /* %L1L2L1CODE : Code (variant) of Low Level Node 1 of Low Level Node 2 of Low Level Node 1 (integer) */ + { + Op->typ = INTEGER_TYP; + if (BIF_LL1 (bif) && NODE_TEMPLATE_LL2 (BIF_LL1 (bif)) && NODE_TEMPLATE_LL1 (NODE_TEMPLATE_LL2 (BIF_LL1 (bif)))) + Op->val.I = NODE_CODE (NODE_TEMPLATE_LL1 (NODE_TEMPLATE_LL2 (BIF_LL1 (bif)))); + else + Op->val.I = (long) NULL; + *iptr += strlen("%L1L2L1CODE"); + } else + if (strncmp(&(str[*iptr]),"%L1L2*L1CODE", strlen("%L1L2*L1CODE"))== 0) /* %L1L2L1CODE : Code (variant) of Low Level Node 1 of Low Level Node 2 of Low Level Node 1 (integer) */ + { + PTR_LLND temp; + + Op->typ = INTEGER_TYP; + if (BIF_LL1 (bif) && NODE_TEMPLATE_LL2 (BIF_LL1 (bif)) && NODE_TEMPLATE_LL1 (NODE_TEMPLATE_LL2 (BIF_LL1 (bif)))) + { + temp = BIF_LL1 (bif); + while (NODE_OPERAND1(temp)) temp = NODE_OPERAND1(temp); + if (NODE_TEMPLATE_LL1 (temp)) + Op->val.I = NODE_CODE (NODE_TEMPLATE_LL1 (temp)); + else + Op->val.I = (long) NULL; + } + else + Op->val.I = (long) NULL; + *iptr += strlen("%L1L2*L1CODE"); + } else + if (strncmp(&(str[*iptr]),"%L2L1STR", strlen("%L2L1STR"))== 0) /* %L2L1STR : String (string_val) of Low Level Node 1 of Low Level Node 2 (string) */ + { + Op->typ = STRING_TYP; + if (BIF_LL2 (bif) && NODE_TEMPLATE_LL1 (BIF_LL2 (bif))) + Op->val.S = NODE_STR (NODE_TEMPLATE_LL1 (BIF_LL2 (bif))); + else + Op->val.S = NULL; + *iptr += strlen("%L2L1STR"); + } else + { + Message (" *** Unknown operand in %IF (condition) for Bif Node *** ",0); + } +} + +int GetComp (str, iptr) + char *str; + int *iptr; +{ + int Comp; + + if (strncmp(&(str[*iptr]),"==", strlen("==")) == 0) /* == : Equal */ + { + Comp = COMP_EQUAL; + *iptr += strlen("=="); + } else + if (strncmp(&(str[*iptr]),"!=", strlen("!=")) == 0) /* != : Different */ + { + Comp = COMP_DIFF; + *iptr += strlen("!="); + } else + { + Message (" *** Unknown comparison operator in %IF (condition) *** ",0); + Comp = COMP_UNDEF; + } + return Comp; +} + +int Eval_Type_Condition(str, ptype) + char *str; + PTR_TYPE ptype; +{ + int Result = 0; + int i = 0; + operand Op1, Op2; + int Comp; + + while (str[i] == ' ') {i++;} /* skip spaces before '(condition)' */ + if (str[i++] != '(') + { + Message (" *** Missing (condition) after %IF *** ",0); + + return 0; + } else + while (str[i] == ' ') {i++;} /* skip spaces before first operand */ + Get_Type_Operand(str, &i, ptype, &Op1); + while (str[i] == ' ') {i++;} /* skip spaces before the comparison operator */ + Comp = GetComp(str, &i); + while (str[i] == ' ') {i++;} /* skip spaces before second operand */ + Get_Type_Operand(str, &i, ptype, &Op2); + while (str[i] == ' ') {i++;} /* skip spaces before the closing round bracket */ + if (str[i] != ')') + { + Message (" *** Missing ')' after %IF (condition *** ",0); + return i; + } else + i++; + if ((Op1.typ != UNDEF_TYP) && (Op1.typ == Op2.typ) && (Comp !=COMP_UNDEF)) + { + switch (Op1.typ) + { + case STRING_TYP : Result = strcmp (Op1.val.S, Op2.val.S); + break; + case INTEGER_TYP : Result = Op1.val.I - Op2.val.I; + break; + } + if (Comp == COMP_EQUAL) Result = !Result; + if (Result) return i; /* continue from here to the corresponding %ELSE if exists */ + else /* continue at the corresponding %ELSE */ + { + int ifcount_local = 1; + while (str[i]) + { + while (str[i] != '%') { + if (str[i]) i++; + else return i; + } + i++; + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ + { + ifcount_local++; + i += strlen("IF"); + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ + { + ifcount_local--; + i += strlen("ENDIF"); + if (ifcount_local == 0) return i; + } else + if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* Counts %ELSE ; stop skipping if corresponding*/ + { + i += strlen("ELSE"); + if (ifcount_local == 1) return i; + } + } + return i; + } + } else + { + Message (" *** Error in condition for %IF command *** 1",0); + return i; + } +} + +int Eval_LLND_Condition(str, ll) + char *str; + PTR_LLND ll; +{ + int Result = 0; + int i = 0; + operand Op1, Op2; + int Comp = 0; + + while (str[i] == ' ') {i++;} /* skip spaces before '(condition)' */ + if (str[i++] != '(') + { + Message (" *** Missing (condition) after %IF *** ",0); + return 0; + } else + while (str[i] == ' ') {i++;} /* skip spaces before first operand */ + Get_LL_Operand(str, &i, ll, &Op1); + while (str[i] == ' ') {i++;} /* skip spaces before the comparison operator */ + Comp = GetComp(str, &i); + while (str[i] == ' ') {i++;} /* skip spaces before second operand */ + Get_LL_Operand(str, &i, ll, &Op2); + while (str[i] == ' ') {i++;} /* skip spaces before the closing round bracket */ + if (str[i] != ')') + { + Message (" *** Missing ')' after %IF (condition *** ",0); + i++; + return i; + } else + i++; + + if ((Op1.typ != UNDEF_TYP) && (Op1.typ == Op2.typ) && (Comp != COMP_UNDEF)) + { + switch (Op1.typ) + { + case STRING_TYP : Result = strcmp (Op1.val.S, Op2.val.S); + break; + case INTEGER_TYP : Result = Op1.val.I - Op2.val.I; + break; + } + if (Comp == COMP_EQUAL) Result = !Result; + if (Result) return i; /* continue from here to the corresponding %ELSE if exists */ + else /* continue at the corresponding %ELSE */ + { + int ifcount_local = 1; + while (str[i]) + { + while (str[i] != '%') { + if (str[i]) i++; + else return i; + } + i++; + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ + { + ifcount_local++; + i += strlen("IF"); + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ + { + ifcount_local--; + i += strlen("ENDIF"); + if (ifcount_local == 0) return i; + } else + if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* Counts %ELSE ; stop skipping if corresponding*/ + { + i += strlen("ELSE"); + if (ifcount_local == 1) return i; + } + } + return i; + } + } else + { + Message (" *** Error in condition for %IF command *** 2",0); + return i; + } +} + +int Eval_Bif_Condition(str, bif) + char *str; + PTR_BFND bif; +{ + int Result = 0; + int i = 0; + operand Op1, Op2; + int Comp; + + while (str[i] == ' ') {i++;} /* skip spaces before '(condition)' */ + if (str[i++] != '(') + { + Message (" *** Missing (condition) after %IF *** ",0); + return 0; + } else + while (str[i] == ' ') {i++;} /* skip spaces before first operand */ + Get_Bif_Operand(str, &i, bif, &Op1); + while (str[i] == ' ') {i++;} /* skip spaces before the comparison operator */ + Comp = GetComp(str, &i); + while (str[i] == ' ') {i++;} /* skip spaces before second operand */ + Get_Bif_Operand(str, &i, bif, &Op2); + while (str[i] == ' ') {i++;} /* skip spaces before the closing round bracket */ + + if (str[i] != ')') + { + Message (" *** Missing ')' after %IF (condition *** ",0); + return i; + } else + i++; + if ((Op1.typ != UNDEF_TYP) && (Op1.typ == Op2.typ) && (Comp != COMP_UNDEF)) + { + switch (Op1.typ) + { + case STRING_TYP : Result = strcmp (Op1.val.S, Op2.val.S); + break; + case INTEGER_TYP : Result = Op1.val.I - Op2.val.I; + break; + } + if (Comp == COMP_EQUAL) Result = !Result; + if (Result) return i; /* continue from here to the corresponding %ELSE if exists */ + else /* continue at the corresponding %ELSE */ + { + int ifcount_local = 1; + while (str[i]) + { + while (str[i] != '%') { + if (str[i]) i++; + else return i; + } + i++; + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ + { + ifcount_local++; + i += strlen("IF"); + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ + { + ifcount_local--; + i += strlen("ENDIF"); + if (ifcount_local == 0) return i; + } else + if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* Counts %ELSE ; stop skipping if corresponding*/ + { + i += strlen("ELSE"); + if (ifcount_local == 1) return i; + } + } + return i; + } + } else + { + Message (" *** Error in condition for %IF command *** 3",0); + return i; + } +} + +int SkipToEndif (str) + char *str; +{ + int ifcount_local = 1; + int i = 0; + + while (str[i]) + { + while (str[i] != '%') { + if (str[i]) i++; + else return i; + } + i++; + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* Counts %IF */ + { + ifcount_local++; + i += strlen("IF"); + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* Counts %ENDIF ; stop skipping if corresponding */ + { + ifcount_local--; + i += strlen("ENDIF"); + if (ifcount_local == 0) return i; + } + } + return i; +} + +char *Tool_Unparse2_LLnode (); + +char *Tool_Unparse_Type (ptype) + PTR_TYPE ptype; + /*int def;*/ /* def = 1 : defined type */ + /* def = 0 : named type */ +{ + int variant; + int kind; + char *str; + char c; + int i; + + if (!ptype) + return NULL; + + variant = TYPE_CODE (ptype); + kind = (int) node_code_kind [(int) variant]; + if (kind != (int)TYPENODE) + Message ("Error in Unparse, not a type node", 0); + + str = Unparse_Def [variant].str; + + /* now we have to interpret the code to unparse it */ + + if (str == NULL) + return NULL; + if (strcmp ( str, "n") == 0) + { + Message("Node not define for unparse",0); + return NULL; + } + + + i = 0 ; + c = str[i]; + while (c != '\0') + { + if (c == '%') + { + i++; + c = str[i]; + /******** WE HAVE TO INTERPRET THE COMMAND *********/ + if (c == '%') /* %% : Percent Sign */ + { + BufPutString ("%",0); + i++; + } else + if (strncmp(&(str[i]),"ERROR", strlen("ERROR"))== 0) /* %ERROR : Generate error message */ + { + Message("Error Node not defined",0); + BufPutInt(variant); + BufPutString ("-----TYPE ERROR--------",0); + i += strlen("ERROR"); + } else + if (strncmp(&(str[i]),"NL", strlen("NL"))== 0) /* %NL : NewLine */ + { + /*int j;*/ + BufPutChar ('\n'); +/* for (j = 0; j < TabNumber; j++) + if (j>1) + BufPutString (" ",0); + else + BufPutString (" ",0);*/ + i += strlen("NL"); + } else + if (strncmp(&(str[i]),"NOTABNL", strlen("NOTABNL"))== 0) /* %NL : NewLine */ + { + BufPutChar ('\n'); + i += strlen("NOTABNL"); + } else + if (strncmp(&(str[i]),"RIDPT", strlen("RIDPT"))== 0) + { + DealWith_Rid(ptype,In_Class_Flag); + i += strlen("RIDPT"); + } else + if (strncmp(&(str[i]),"TABNAME", strlen("TABNAME"))== 0) /* %TABNAME : Self Name from Table */ + { + if (Check_Lang_Fortran(cur_proj)) + BufPutString (ftype_name [type_index (TYPE_CODE (ptype))],0); + else + { + BufPutString (ctype_name [type_index (TYPE_CODE (ptype))],0); + } + i += strlen("TABNAME"); + } else + if (strncmp(&(str[i]),"TAB", strlen("TAB"))== 0) /* %TAB : Tab */ + { + BufPutString (" ",0); /* cychen */ + i += strlen("TAB"); + } else + if (strncmp(&(str[i]),"SETFLAG", strlen("SETFLAG"))== 0) + { + i = i + strlen("SETFLAG"); + Treat_Flag(str, &i,1); + } else + if (strncmp(&(str[i]),"UNSETFLAG", strlen("UNSETFLAG"))== 0) + { + i = i + strlen("UNSETFLAG"); + Treat_Flag(str, &i,-1); + } else + if (strncmp(&(str[i]),"PUSHFLAG", strlen("PUSHFLAG"))== 0) + { + i = i + strlen("PUSHFLAG"); + PushPop_Flag(str, &i,1); + } else + if (strncmp(&(str[i]),"POPFLAG", strlen("POPFLAG"))== 0) + { + i = i + strlen("POPFLAG"); + PushPop_Flag(str, &i,-1); + } else + if (strncmp(&(str[i]),"PUTTAB", strlen("PUTTAB"))== 0) /* %TAB : Tab */ + { + int j; + for (j = 0; j < TabNumber; j++) + if (j>0) + BufPutString (" ",0); + else + BufPutString (" ",0); /* cychen */ + i += strlen("PUTTAB"); + } else + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* %IF : If ; syntax : %IF (condition) then_bloc [%ELSE else_bloc] %ENDIF */ + { + i += strlen("IF"); + i += Eval_Type_Condition(&(str[i]), ptype); + } else + if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* %ELSE : Else */ + { + i += strlen("ELSE"); + i += SkipToEndif(&(str[i])); /* skip to the corresponding endif */ + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* %ENDIF : End of If */ + { + i += strlen("ENDIF"); + } else + if (strncmp(&(str[i]),"SUBTYPE", strlen("SUBTYPE"))== 0) /* %SUBTYPE : find the next type for (CAST) */ + { + PTR_TYPE pt; + pt = TYPE_BASE(ptype); + if(pt) Tool_Unparse_Type(pt); + i += strlen("SUBTYPE"); + } else + if (strncmp(&(str[i]),"BASETYPE", strlen("BASETYPE"))== 0) /* %BASETYPE : Base Type Name Identifier */ + { + if (Check_Lang_Fortran(cur_proj)) + BufPutString (ftype_name [type_index (TYPE_CODE (TYPE_BASE (ptype)))],0); + else + { + PTR_TYPE pt; + pt = Find_BaseType(ptype); + if (pt) + { + Tool_Unparse_Type(pt); + } else{ + /* printf("offeding node type node: %d\n", ptype->id); + Message("basetype not found",0); + */ + } + } + i += strlen("BASETYPE"); + } else + + if (strncmp(&(str[i]),"FBASETYPE", strlen("FBASETYPE"))== 0) /* %FBASETYPE : Base Type Name Identifier */ + { + PTR_TYPE pt; + pt = Find_BaseType2(ptype); + if (pt) + { + Tool_Unparse_Type(pt); + } else{ + /* printf("offeding node type node: %d\n", ptype->id); + Message("basetype not found",0); + */ + } + i += strlen("FBASETYPE"); + } else + + + if (strncmp(&(str[i]),"STAR", strlen("STAR"))== 0) + { + PTR_TYPE pt; + int flg; + pt = ptype; + /* while (pt) */ + { + if (TYPE_CODE(pt) == T_POINTER){ + BufPutString ("*",0); + flg = pt->entry.Template.dummy5; + if(flg & BIT_RESTRICT) BufPutString(" restrict ",0); + if(flg & BIT_CONST) BufPutString(" const ",0); + if(flg & BIT_GLOBL) BufPutString(" global ",0); + if(flg & BIT_SYNC) BufPutString(" Sync ",0); + if(flg & BIT_VOLATILE) BufPutString(" volatile ",0); + } + else + if (TYPE_CODE(pt) == T_REFERENCE){ + BufPutString ("&",0); + flg = pt->entry.Template.dummy5; + if(flg & BIT_RESTRICT) BufPutString(" restrict ",0); + if(flg & BIT_CONST) BufPutString(" const ",0); + if(flg & BIT_GLOBL) BufPutString(" global ",0); + if(flg & BIT_SYNC) BufPutString(" Sync ",0); + if(flg & BIT_VOLATILE) BufPutString(" volatile ",0); + } + /* else + break; + if(TYPE_CODE(pt) == T_MEMBER_POINTER) + pt = TYPE_COLL_BASE(pt); + else pt = TYPE_BASE(pt); */ + } + i += strlen("STAR"); + } else + if (strncmp(&(str[i]),"RANGES", strlen("RANGES"))== 0) /* %RANGES : Ranges */ + { + Tool_Unparse2_LLnode (TYPE_RANGES (ptype)); + i += strlen("RANGES"); + } else + if (strncmp(&(str[i]),"NAMEID", strlen("NAMEID"))== 0) /* %NAMEID : Name Identifier */ + { + if (ptype->name) + BufPutString ( ptype->name->ident,0); + else + { + BufPutString ("-------TYPE ERROR (NAMEID)------",0); + } + i += strlen("NAMEID"); + } else + if (strncmp(&(str[i]),"SYMBID", strlen("SYMBID"))== 0) /* %NAMEID : Name Identifier */ + { + if (TYPE_SYMB_DERIVE(ptype)){ + PTR_SYMB cname; + cname = TYPE_SYMB_DERIVE(ptype); + if(TYPE_CODE(ptype) == T_DERIVED_TYPE){ + if((SYMB_CODE(cname) == STRUCT_NAME) && (SYMB_TYPE(cname) == NULL) + &&(BIF_CODE(SYMB_SCOPE(cname)) == GLOBAL)) + BufPutString("struct ", 0); + if((SYMB_CODE(cname) == CLASS_NAME) && (SYMB_TYPE(cname) == NULL) + &&(BIF_CODE(SYMB_SCOPE(cname)) == GLOBAL)) + BufPutString("class ", 0); + if((SYMB_CODE(cname) == UNION_NAME) && (SYMB_TYPE(cname) == NULL) + &&(BIF_CODE(SYMB_SCOPE(cname)) == GLOBAL)) + BufPutString("union ", 0); + } + if(TYPE_SCOPE_SYMB_DERIVE(ptype) && TYPE_CODE(ptype) != T_DERIVED_TEMPLATE) { + Tool_Unparse_Symbol(TYPE_SCOPE_SYMB_DERIVE(ptype)); + BufPutString("::",0); + } + Tool_Unparse_Symbol(cname); + } + else if(TYPE_CODE(ptype) == T_MEMBER_POINTER) + Tool_Unparse_Symbol(TYPE_COLL_NAME(ptype)); + else + { + printf("node = %d, variant = %d\n",TYPE_ID(ptype), TYPE_CODE(ptype)); + BufPutString ("-------TYPE ERROR (ISYMBD)------",0); + } + i += strlen("SYMBID"); + } else + if (strncmp(&(str[i]),"RANGLL1", strlen("RANGLL1"))== 0) /* %RANGLL1 : Low Level Node 1 of Ranges */ + { + if (TYPE_RANGES (ptype)) + Tool_Unparse2_LLnode (NODE_TEMPLATE_LL1 (TYPE_RANGES (ptype))); + i += strlen("RANGLL1"); + } else + if (strncmp(&(str[i]),"COLLBASE", strlen("COLLBASE"))== 0) /* %COLL BASE */ + { + if (TYPE_COLL_BASE(ptype)) + Tool_Unparse_Type(TYPE_COLL_BASE(ptype)); + i += strlen("COLLBASE"); + } else + if (strncmp(&(str[i]),"TMPLARGS", strlen("TMPLARGS"))== 0) /* %RANGLL1 : Low Level Node 1 of Ranges */ + { + if (TYPE_TEMPL_ARGS(ptype)) + Tool_Unparse2_LLnode(TYPE_TEMPL_ARGS(ptype)); + i += strlen("TMPLARGS"); + } else + Message (" *** Unknown type node COMMAND *** ",0); + } + else + { + BufPutChar (c); + i++; + } + c = str[i]; + } + return Buf_address; +} + +char *Tool_Unparse2_LLnode(ll) + PTR_LLND ll; +{ + int variant; + int kind; + char *str; + char c; + int i; + + if (!ll) + return NULL; + + variant = NODE_CODE (ll); + /*BufPutInt(variant); + printf("LLNODE : %i\n%s",variant,Buf_address);*/ + if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&& + ((variant==ARRAY_REF)|| + (variant==VAR_REF)|| + (variant==CONST_REF))&& + ON_BLOCK&&NODE_SYMB(ll)) + { + PTR_LLND ptr,new_node,new_symb; + if (!SYMB_DOVAR(NODE_SYMB(ll))&& + (!(SYMB_ATTR(NODE_SYMB(ll))&TASK_BIT))&& + (!(SYMB_ATTR(NODE_SYMB(ll))&PROCESSORS_BIT))) + { + new_symb=make_llnode(variant,LLNULL,LLNULL,NODE_SYMB(ll)); + new_node=make_llnode(EXPR_LIST,new_symb,LLNULL,SMNULL); + if (parameter_list) + { + ptr=Follow_Llnd(parameter_list,2); + NODE_OPERAND1(ptr)=new_node; + } + if (!parameter_list) parameter_list=new_node; + SYMB_DOVAR(NODE_SYMB(ll))=2; + } + } + kind = (int) node_code_kind[(int) variant]; + if (kind != (int)LLNODE) + { + Message("Error in Unparse, not a llnd node",0); + BufPutInt(variant); + BufPutString ("------ERROR--------",0); + return NULL; + } + str = Unparse_Def[variant].str; + /* now we have to interpret the code to unparse it */ + + if (str == NULL) + return NULL; + if (strcmp( str, "n") == 0) + return NULL; + + i = 0 ; + c = str[i]; + while (c != '\0') + { + if (c == '%') + { + i++; + c = str[i]; + /******** WE HAVE TO INTERPRET THE COMMAND *********/ + if (c == '%') /* %% : Percent Sign */ + { + BufPutString ("%",0); + i++; + } else + if (strncmp(&(str[i]),"ERROR", strlen("ERROR"))== 0) /* %ERROR : Generate error message */ + { + Message ("--- unparsing error[0] : ",0); + BufPutInt(variant); + BufPutString ("------ERROR--------",0); + i += strlen("ERROR"); + } else + if (strncmp(&(str[i]),"NL", strlen("NL"))== 0) /* %NL : NewLine */ + { + /* int j;*/ + BufPutChar ('\n'); +/* for (j = 0; j < TabNumber; j++) + if (j>1) + BufPutString (" ",0); + else + BufPutString (" ",0);*/ + i += strlen("NL"); + } else + if (strncmp(&(str[i]),"TAB", strlen("TAB"))== 0) /* %TAB : Tab */ + { + BufPutString (" ",0); /* cychen */ + i += strlen("TAB"); + } else + if (strncmp(&(str[i]),"BACK", strlen("BACK"))== 0) + { + Buf_pointer--; + i += strlen("BACK"); + } else + + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* %IF : If ; syntax : %IF (condition) then_bloc [%ELSE else_bloc] %ENDIF */ + { + i += strlen("IF"); + i += Eval_LLND_Condition(&(str[i]), ll); + } else + if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* %ELSE : Else */ + { + i += strlen("ELSE"); + i += SkipToEndif(&(str[i])); /* skip to the corresponding endif */ + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* %ENDIF : End of If */ + { + i += strlen("ENDIF"); + } else + if (strncmp(&(str[i]),"LL1", strlen("LL1"))== 0) /* %LL1 : Low Level Node 1 */ + { + Tool_Unparse2_LLnode(NODE_TEMPLATE_LL1(ll)); + i += strlen("LL1"); + } else + if (strncmp(&(str[i]),"LL2", strlen("LL2"))== 0) /* %LL2 : Low Level Node 2 */ + { + Tool_Unparse2_LLnode(NODE_TEMPLATE_LL2(ll)); + i += strlen("LL2"); + } else + if (strncmp(&(str[i]),"SYMBID", strlen("SYMBID"))== 0) /* %SYMBID : Symbol identifier */ + { + Tool_Unparse_Symbol (NODE_SYMB (ll)); + i += strlen("SYMBID"); + } else + if (strncmp(&(str[i]),"NEWSPEC", strlen("NEWSPEC"))== 0) + { + if (NewSpecList) + { +#ifdef __SPF + removeFromCollection(NewSpecList); +#endif + free(NewSpecList); + } + NewSpecList=NODE_OPERAND0(ll); + i += strlen("NEWSPEC"); + } else + if (strncmp(&(str[i]),"COMMONDECL", strlen("COMMONDECL"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ + { + int count=0; + if ((NODE_CODE(ll)==COMM_LIST)&&NODE_OPERAND0(ll)) count=FindCommonHeapDeclaration(NODE_OPERAND0(ll)); + if (!count) + { + count=0; + Treat_Flag("(POINTER)",&count, 1); + } + i += strlen("COMMONDECL"); + } else + if (strncmp(&(str[i]),"CHECK_HEAP", strlen("CHECK_HEAP"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ + { + /* char *str; + if (ll) str=SYMB_IDENT(NODE_SYMB (ll)); + if (!strcmp(str,"heap")||!strcmp(str,"HEAP")) */ + if(SYMB_ATTR(NODE_SYMB (ll))&HEAP_BIT) + { + /*BufPutString("",0);*/ + Tool_Unparse2_LLnode (NODE_OPERAND0(ll)); + return Buf_address; + } + i += strlen("CHECK_HEAP"); + } else + if (strncmp(&(str[i]),"CHECK_PTR", strlen("CHECK_PTR"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ + { + if (ll) + if(SYMB_ATTR(NODE_SYMB (ll))&DVM_POINTER_ARRAY_BIT) + BufPutString("%PTR",0); + i += strlen("CHECK_PTR"); + } else + if (strncmp(&(str[i]),"CHECK_PROC_REF", strlen("CHECK_PROC_REF"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ + { + int k=0; + if (ll) + if(SYMB_ATTR(NODE_SYMB (ll))&PROCESSORS_BIT) + if (!Get_Flag_val("(PROC_REF)",&k)) + return Buf_address; + i += strlen("CHECK_PROC_REF"); + } else + if (strncmp(&(str[i]),"DELETE_COMMA", strlen("DELETE_COMMA"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ + { + if (Buf_address[Buf_pointer-1]==',') + Buf_pointer--; + i += strlen("DELETE_COMMA"); + } else + if (strncmp(&(str[i]),"CHECK_DYNAMIC", strlen("CHECK_DYNAMIC"))== 0) + { + if (NODE_OPERAND0(ll)||NODE_OPERAND1(ll)) + i += strlen("CHECK_DYNAMIC"); + else { + int k=0; + if (Get_Flag_val("(DYNAMIC)",&k)) + { + Buf_pointer--; + return Buf_address; + } + k=0; + Treat_Flag("(DYNAMIC)",&k,1); + BufPutString("DYNAMIC",0); + i += strlen("CHECK_DYNAMIC"); + return Buf_address; + } + } else + + if (strncmp(&(str[i]),"SAVE_SYMBOL", strlen("SAVE_SYMBOL"))== 0) /* %SAVE_SYMBOL : Reduction variables list */ + { + if (Find_SaveSymbol (ll)) + { + if (Buf_address[Buf_pointer-1]==',') Buf_pointer--; + return Buf_address; + } + i += strlen("SAVE_SYMBOL"); + } else + if (strncmp(&(str[i]),"DELETE_SYMBOL", strlen("DELETE_SYMBOL"))== 0) /* %DELETE_SYMBOL : Clear reduction variables list */ + { + Number_Of_Symbol=0; + i += strlen("DELETE_SYMBOL"); + } else + if (strncmp(&(str[i]),"POINTER_NAME", strlen("POINTER_NAME"))== 0) /* %POINTER_NAME : We should change the definition of Pointer descriptor */ + { + if ((NODE_CODE(ll)==VAR_REF)||(NODE_CODE(ll)==ARRAY_REF)) + if(SYMB_ATTR(NODE_SYMB (ll))&DVM_POINTER_BIT) + { + Tool_Unparse_Symbol (NODE_SYMB (ll)); + if (NODE_CODE(ll)==ARRAY_REF) + { + if(NODE_OPERAND0(ll)) + { + BufPutChar('('); + Tool_Unparse2_LLnode (NODE_OPERAND0(ll)); + BufPutChar(')'); + } + } + return Buf_address; + } + i += strlen("POINTER_NAME"); + } else + if (strncmp(&(str[i]),"DOPROC", strlen("DOPROC"))== 0) /* for subclass qualification */ + { int flg; + if(NODE_TYPE(ll) && (NODE_CODE(NODE_TYPE(ll)) == T_DESCRIPT)){ + flg = (NODE_TYPE(ll))->entry.Template.dummy5; + if(flg & BIT_VIRTUAL) BufPutString(" virtual ",0); + if(flg & BIT_ATOMIC) BufPutString(" atomic ",0); + if(flg & BIT_PRIVATE) BufPutString(" private ",0); + if(flg & BIT_PROTECTED) BufPutString(" protected ",0); + if(flg & BIT_PUBLIC) BufPutString(" public ",0); + } + else BufPutString(" public ", 0); + /* note: this last else condition is to fix a bug in + the dep2C++ which does not create the right types + when converting a collection to a class. + */ + i += strlen("DOPROC"); + } else + if (strncmp(&(str[i]),"TYPE", strlen("TYPE"))== 0) /* %TYPE : Type */ + { + if(NODE_SYMB(ll) && (SYMB_ATTR(NODE_SYMB(ll)) & OVOPERATOR)){ + /* this is an overloaded operator. don't do type */ + } + else{ Tool_Unparse_Type (NODE_TYPE (ll)); } + i += strlen("TYPE"); + } else + if (strncmp(&(str[i]),"L1SYMBCST", strlen("L1SYMBCST"))== 0) /* %L1SYMBCST : Constant Value of Low Level Node Symbol */ + { + if (NODE_TEMPLATE_LL1 (ll) && NODE_SYMB (NODE_TEMPLATE_LL1 (ll))) + { + Tool_Unparse2_LLnode((NODE_SYMB (NODE_TEMPLATE_LL1 (ll)))->entry.const_value); + } + i += strlen("L1SYMBCST"); + } else + if (strncmp(&(str[i]),"INTVAL", strlen("INTVAL"))== 0) /* %INTVAL : Integer Value */ + { + if (NODE_INT_CST_LOW (ll) >= 0) + { + BufPutInt (NODE_INT_CST_LOW (ll)); + } else + { + BufPutString ("(",0); + BufPutInt (NODE_INT_CST_LOW (ll)); + BufPutString (")",0); + } + i += strlen("INTVAL"); + } else + if (strncmp(&(str[i]),"STATENO", strlen("STATENO"))== 0) /* %STATENO : Statement number */ + { + if (NODE_LABEL (ll)) + { + BufPutInt ((int)( LABEL_STMTNO (NODE_LABEL (ll)))); + } + i += strlen("STATENO"); + } else + if (strncmp(&(str[i]),"STRVAL", strlen("STRVAL"))== 0) /* %STRVAL : String Value */ + { + BufPutString (NODE_STR (ll),0); + i += strlen("STRVAL"); + } else + if (strncmp(&(str[i]),"BOOLVAL", strlen("BOOLVAL"))== 0) /* %BOOLVAL : String Value */ + { + BufPutString (NODE_BV (ll) ? ".TRUE." : ".FALSE.",0); + i += strlen("BOOLVAL"); + } else + if (strncmp(&(str[i]),"CHARVAL", strlen("CHARVAL"))== 0) /* %CHARVAL : Char Value */ + { + switch(NODE_CV(ll)){ + case '\n':BufPutChar('\\'); BufPutChar('n'); break; + case '\t':BufPutChar('\\'); BufPutChar('t'); break; + case '\r':BufPutChar('\\'); BufPutChar('r'); break; + case '\f':BufPutChar('\\'); BufPutChar('f'); break; + case '\b':BufPutChar('\\'); BufPutChar('b'); break; + case '\a':BufPutChar('\\'); BufPutChar('a'); break; + case '\v':BufPutChar('\\'); BufPutChar('v'); break; + default: + BufPutChar (NODE_CV (ll)); + } + i += strlen("CHARVAL"); + } else + if (strncmp(&(str[i]),"ORBPL1", strlen("ORBPL1"))== 0) /* %ORBPL1 : Openning Round Brackets on Precedence of Low Level Node 1 */ + { + int llvar = NODE_CODE (NODE_TEMPLATE_LL1 (ll)); + if (binop (llvar) && (precedence [variant - EQ_OP] < precedence [llvar - EQ_OP])) + BufPutString ("(",0); + i += strlen("ORBPL1"); + } else + if (strncmp(&(str[i]),"CRBPL1", strlen("CRBPL1"))== 0) /* %CRBPL1 : Closing Round Brackets on Precedence of Low Level Node 1 */ + { + int llvar = NODE_CODE (NODE_TEMPLATE_LL1 (ll)); + if (binop (llvar) && (precedence [variant - EQ_OP] < precedence [llvar - EQ_OP])) + BufPutString (")",0); + i += strlen("CRBPL1"); + } else + if (strncmp(&(str[i]),"ORBPL2", strlen("ORBPL2"))== 0) /* %ORBPL2 : Openning Round Brackets on Precedence of Low Level Node 2 */ + { + int llvar = NODE_CODE (NODE_TEMPLATE_LL2 (ll)); + if (binop (llvar) && (precedence [variant - EQ_OP] <= precedence [llvar - EQ_OP])) + BufPutString ("(",0); + i += strlen("ORBPL2"); + } else + if (strncmp(&(str[i]),"CRBPL2", strlen("CRBPL2"))== 0) /* %CRBPL2 : Closing Round Brackets on Precedence of Low Level Node 2 */ + { + int llvar = NODE_CODE (NODE_TEMPLATE_LL2 (ll)); + if (binop (llvar) && (precedence [variant - EQ_OP] <= precedence [llvar - EQ_OP])) + BufPutString (")",0); + i += strlen("CRBPL2"); + } else + if (strncmp(&(str[i]),"SETFLAG", strlen("SETFLAG"))== 0) + { + i = i + strlen("SETFLAG"); + Treat_Flag(str, &i,1); + } else + if (strncmp(&(str[i]),"UNSETFLAG", strlen("UNSETFLAG"))== 0) + { + i = i + strlen("UNSETFLAG"); + Treat_Flag(str, &i,-1); + } else + if (strncmp(&(str[i]),"PUSHFLAG", strlen("PUSHFLAG"))== 0) + { + i = i + strlen("PUSHFLAG"); + PushPop_Flag(str, &i,1); + } else + if (strncmp(&(str[i]),"POPFLAG", strlen("POPFLAG"))== 0) + { + i = i + strlen("POPFLAG"); + PushPop_Flag(str, &i,-1); + } else + if (strncmp(&(str[i]),"PURE", strlen("PURE"))== 0) /* for pure function declarations */ + { + PTR_SYMB symb; + symb = NODE_SYMB(ll); + if(symb && (SYMB_TEMPLATE_DUMMY8(symb) & 128)) BufPutString ("= 0",0); + i += strlen("PURE"); + } + else + if (strncmp(&(str[i]),"CNSTF", strlen("CNSTF"))== 0) /* for const functions */ + { + PTR_SYMB symb; + if (NODE_SYMB (ll)){ + symb = BIF_SYMB (ll); + if(SYMB_TEMPLATE_DUMMY8(symb) & 64) BufPutString(" const",0); + } + i += strlen("CNSTF"); + } else + if (strncmp(&(str[i]),"CNSTCHK", strlen("CNSTCHK"))== 0) /* do "const", vol" after * */ + { + int flg; + PTR_TYPE t; + if((t = NODE_TYPE(ll)) &&( (NODE_CODE(t) == T_POINTER) || + (NODE_CODE(t) == T_REFERENCE))){ + flg = t->entry.Template.dummy5; + if(flg & BIT_RESTRICT) BufPutString(" restrict ",0); + if(flg & BIT_CONST) BufPutString(" const ",0); + if(flg & BIT_GLOBL) BufPutString(" global ",0); + if(flg & BIT_SYNC) BufPutString(" Sync ",0); + if(flg & BIT_VOLATILE) BufPutString(" volatile ",0); + } + i += strlen("CNSTCHK"); + } + else + if (strncmp(&(str[i]),"VARLISTTY", strlen("VARLISTTY"))== 0) /* %VARLIST : list of variables / parameters */ + { + PTR_SYMB symb, s; + PTR_LLND args, arg_item = NULL, t; + PTR_TYPE typ; + int new_op_flag; /* 1 if this is a new op */ + new_op_flag = 0; + if(NODE_CODE(ll) == CAST_OP ){ + args = NODE_OPERAND1(ll); + new_op_flag = 1; + } + else if(NODE_CODE(ll) != FUNCTION_OP){ + args = NODE_OPERAND0(ll); + /* symb = SYMB_FUNC_PARAM(NODE_SYMB(ll)); */ + } + else { /* this is a pointer to a function parameter */ + args = NODE_OPERAND1(ll); + t = NODE_OPERAND0(ll); /* node_code(t) == deref_op */ + t = NODE_OPERAND0(t); /* node_code(t) == var_ref */ + s = NODE_SYMB(t); + if(s) symb = SYMB_NEXT(s); + else symb = NULL; + } + while (args ) + { + int typflag; + if(new_op_flag) t = args; + else{ + arg_item = NODE_OPERAND0(args); + t = arg_item; + typflag = 1; + while(t && typflag){ + if((NODE_CODE(t) == VAR_REF) || (NODE_CODE(t) == ARRAY_REF)) + typflag = 0; + else if (NODE_CODE(t) == SCOPE_OP) t = NODE_OPERAND1(t); + else t = NODE_OPERAND0(t); + } + } + if(t){ + symb = NODE_SYMB(t); + typ = NODE_TYPE(t); + if(symb && (typ == NULL)) typ = SYMB_TYPE(symb); + if(new_op_flag || symb ) { + typflag = 1; + while(typ && typflag){ + if(TYPE_CODE(typ) == T_ARRAY || + TYPE_CODE(typ) == T_FUNCTION || + TYPE_CODE(typ) == T_REFERENCE || + TYPE_CODE(typ) == T_POINTER) typ = TYPE_BASE(typ); + else if(TYPE_CODE(typ) == T_MEMBER_POINTER) + typ = TYPE_COLL_BASE(typ); + else typflag = 0; + } + } + if(typ) Tool_Unparse_Type (typ); + BufPutString (" ",0); + } + else printf("unp could not find var ref!\n"); + if(new_op_flag){ + Tool_Unparse2_LLnode(args); + args = LLNULL; + new_op_flag = 0; + } + else{ + Tool_Unparse2_LLnode(arg_item); + args = NODE_OPERAND1(args); + } + if (args) BufPutString (", ",0); + } + i += strlen("VARLISTTY"); + } + else + if (strncmp(&(str[i]),"VARLIST", strlen("VARLIST"))== 0) /* %VARLIST : list of variables / parameters */ + { + PTR_SYMB symb; + if (NODE_SYMB (ll)) + symb = SYMB_FUNC_PARAM (NODE_SYMB (ll)); + else + symb = NULL; + while (symb) + { + BufPutString ( SYMB_IDENT (symb),0); + symb = SYMB_NEXT_DECL (symb); + if (symb) BufPutString (", ",0); + } + i += strlen("VARLIST"); + } else + if (strncmp(&(str[i]),"STRINGLEN", strlen("STRINGLEN"))== 0) + { + PTR_SYMB symb; + PTR_TYPE type; + if (NODE_SYMB (ll)) + symb = NODE_SYMB (ll); + else + symb = NULL; + if (symb) + { + type = SYMB_TYPE(symb); + if (type && (TYPE_CODE(type) == T_ARRAY)) + { + type = Find_BaseType(type); + } + if (type && (TYPE_CODE(type) == T_STRING)) + { + if (TYPE_RANGES(type)) + Tool_Unparse2_LLnode(TYPE_RANGES(type)); + } + } + i += strlen("STRINGLEN"); + } else + if (strncmp(&(str[i]),"CHPFCONTTAB", strlen("CHPFCONTTAB"))== 0) /* %TAB : Tab */ + { + int j; + BufPutString ("\n!HPF$*",0); + for (j = 0; j < TabNumber; j++) + if (j>0) + BufPutString (" ",0); + else + BufPutString (" ",0); + Buf_pointer-=7; + i += strlen("CHPFCONTTAB"); + } else + Message (" *** Unknown low level node COMMAND *** ",0); + } + else + { + BufPutChar ( c); + i++; /* Bodin */ + } + c = str[i]; + } + return Buf_address; +} + +char *Tool_Unparse_Bif(bif) + PTR_BFND bif; +{ + int variant; + int kind; + char *str; + char c; + int i; + int AfterDOLabel=0; + + if (!bif) + return NULL; + + variant = BIF_CODE(bif); + /*BufPutInt(variant); + printf("BIFNODE : %i\n%s",variant,Buf_address);*/ + + /*BufPutInt(isForNodeEndStmt(bif)); + BufPutInt(variant);*/ + kind = (int) node_code_kind[(int) variant]; + if (kind != (int)BIFNODE) + Message("Error in Unparse, not a bif node",0); + str = Unparse_Def[variant].str; + if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&TaskRegionUnparse) + { if (!BIF_ID(bif)) return Buf_address; + else BIF_ID(bif)=0; + } + if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&(variant==CONTROL_END)) + if (NODE_CODE(BIF_CP(bif))==PROG_HEDR) + if (TaskRegion) + { + if (TabNumber>1) TabNumber--; + Puttab(); + BufPutString("end \n\n",0); + UnparseTaskRegion(TaskRegion); + return Buf_address; + } + if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&(variant==DVM_ON_DIR)) + { + char *name; + int i,temp=Buf_pointer; + PTR_FCALL fcall,new_node; + Tool_Unparse2_LLnode(BIF_LL1(bif)); + name=xmalloc(Buf_pointer-temp); + for(i=0;i+temp0) + { + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("INDEPENDENT\n",0); + } + NumberOfIndependent--; + i = i + strlen("CHECK_INDEPENDENT_DO"); + } else + if (strncmp(&(str[i]),"RESET_INDEPENDENT_DO", strlen("RESET_INDEPENDENT_DO"))== 0) + { + NumberOfIndependent=0; + i = i + strlen("RESET_INDEPENDENT_DO"); + } else + if (strncmp(&(str[i]),"UNSETFLAG", strlen("UNSETFLAG"))== 0) + { + i = i + strlen("UNSETFLAG"); + Treat_Flag(str, &i,-1); + } else + if (strncmp(&(str[i]),"PUSHFLAG", strlen("PUSHFLAG"))== 0) + { + i = i + strlen("PUSHFLAG"); + PushPop_Flag(str, &i,1); + } else + if (strncmp(&(str[i]),"POPFLAG", strlen("POPFLAG"))== 0) + { + i = i + strlen("POPFLAG"); + PushPop_Flag(str, &i,-1); + } else + if (strncmp(&(str[i]),"ERROR", strlen("ERROR"))== 0) /* %ERROR : Generate error message */ + { + Message("--- stmt unparsing error[1] : ",0); + i += strlen("ERROR"); + BufPutString (" *** UNPARSING ERROR OCCURRED HERE ***\n",0); + } else + if (strncmp(&(str[i]),"NL", strlen("NL"))== 0) /* %NL : NewLine */ + { + int j=0; + if (!Get_Flag_val("(NO_NL)",&j)) + BufPutChar ('\n'); +/* for (j = 0; j < TabNumber; j++) + if (j>1) + BufPutString (" ",0); + else + BufPutString (" ",0);*/ + i += strlen("NL"); + } else + if (strncmp(&(str[i]),"NOTABNL", strlen("NOTABNL"))== 0) /* %NL : NewLine */ + { + BufPutChar ('\n'); + i += strlen("NOTABNL"); + } else + if (strncmp(&(str[i]),"TAB", strlen("TAB"))== 0) /* %TAB : Tab */ + { + BufPutString (" ",0); /* cychen */ + i += strlen("TAB"); + } else + if (strncmp(&(str[i]),"PUTTAB", strlen("PUTTAB"))== 0) /* %TAB : Tab */ + { + int j; + int k=0; + if (!Get_Flag_val("(NO_NL)",&k)) + for (j = 0; j < TabNumber; j++) + if (j>0) + BufPutString (" ",0); + else + BufPutString (" ",0); /* cychen */ + i += strlen("PUTTAB"); + } else + if (strncmp(&(str[i]),"CHPFTAB", strlen("CHPFTAB"))== 0) /* %TAB : Tab */ + { + int j; + BufPutString ("!HPF$",0); + for (j = 0; j < TabNumber; j++) + if (j>0) + BufPutString (" ",0); + else + BufPutString (" ",0); + Buf_pointer-=6; + i += strlen("CHPFTAB"); + } else + if (strncmp(&(str[i]),"GETRED", strlen("GETRED"))== 0) /* %TAB : Tab */ + { + if (FindRedInExpr(BIF_LL1(bif),BIF_LL2 (bif))) + { + if (BIF_LL1(bif)&&NODE_SYMB(BIF_LL1(bif))&&(!IS_DISTRIBUTE_ARRAY(NODE_SYMB(BIF_LL1(bif))))&&(!SYMB_DOVAR(NODE_SYMB(BIF_LL1(bif))))) + { + BufPutString("Reduction var :",0); + Tool_Unparse2_LLnode(BIF_LL1(bif)); + BufPutString("\n",0); + if (!FindInNewList(NewSpecList,BIF_LL1(bif))) + if(!FindInNewList(ReductionList,BIF_LL1(bif))) + { + ReductionList=AddToReductionList(ReductionList,BIF_LL1(bif)); + BufPutString("Reduction LIST :",0); + Tool_Unparse2_LLnode(ReductionList); + BufPutString("\n",0); + } + } + } + i += strlen("GETRED"); + } else + if (strncmp(&(str[i]),"INSERT_REDUCTION", strlen("INSERT_REDUCTION"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ + { + if (ReductionList) + { + BufPutString("MY_REDUCTION (",0); + Tool_Unparse2_LLnode(ReductionList); + BufPutString(")",0); + ReductionList=FreeReductionList(ReductionList); + /*if (NewSpecList) free(NewSpecList);*/ + } + i += strlen("INSERT_REDUCTION"); + } else + if (strncmp(&(str[i]),"CHPFCONTTAB", strlen("CHPFCONTTAB"))== 0) /* %TAB : Tab */ + { + int j; + BufPutString ("\n!HPF$*",0); + for (j = 0; j < TabNumber; j++) + if (j>0) + BufPutString (" ",0); + else + BufPutString (" ",0); + Buf_pointer-=7; + i += strlen("CHPFCONTTAB"); + } else + if (strncmp(&(str[i]),"INCTAB", strlen("INCTAB"))== 0) /* increment tab */ + { + TabNumber++; + i += strlen("INCTAB"); + } else + if (strncmp(&(str[i]),"DECTAB", strlen("DECTAB"))== 0) /*deccrement tab */ + { + if (Check_Lang_Fortran(cur_proj)) + { + if (TabNumber>1) + TabNumber--; + } else + TabNumber--; + i += strlen("DECTAB"); + } else + if (strncmp(&(str[i]),"IF", strlen("IF"))== 0) /* %IF : If ; syntax : %IF (condition) then_bloc [%ELSE else_bloc] %ENDIF */ + { + i += strlen("IF"); + i += Eval_Bif_Condition(&(str[i]), bif); + } else + if (strncmp(&(str[i]),"ELSE", strlen("ELSE"))== 0) /* %ELSE : Else */ + { + i += strlen("ELSE"); + i += SkipToEndif(&(str[i])); /* skip to the corresponding endif */ + } else + if (strncmp(&(str[i]),"ENDIF", strlen("ENDIF"))== 0) /* %ENDIF : End of If */ + { + i += strlen("ENDIF"); + } else + if (strncmp(&(str[i]),"BLOB1", strlen("BLOB1"))== 0) /* %BLOB1 : All Blob 1 */ + { + PTR_BLOB blob; + for (blob = BIF_BLOB1(bif);blob; blob = BLOB_NEXT (blob)) + { + Tool_Unparse_Bif(BLOB_VALUE(blob)); + } + i += strlen("BLOB1"); + } else + if (strncmp(&(str[i]),"BLOB2", strlen("BLOB2"))== 0) /* %BLOB2 : All Blob 2 */ + { + PTR_BLOB blob; + + for (blob = BIF_BLOB2(bif);blob; blob = BLOB_NEXT (blob)) + { + Tool_Unparse_Bif(BLOB_VALUE(blob)); + } + i += strlen("BLOB2"); + } else + if (strncmp(&(str[i]),"LL1", strlen("LL1"))== 0) /* %LL1 : Low Level Node 1 */ + { + Tool_Unparse2_LLnode(BIF_LL1(bif)); + i += strlen("LL1"); + } else + if (strncmp(&(str[i]),"LL2", strlen("LL2"))== 0) /* %LL2 : Low Level Node 2 */ + { + Tool_Unparse2_LLnode (BIF_LL2 (bif)); + i += strlen("LL2"); + } else + if (strncmp(&(str[i]),"LL3", strlen("LL3"))== 0) /* %LL3 : Low Level Node 3 */ + { + Tool_Unparse2_LLnode(BIF_LL3(bif)); + i += strlen("LL3"); + } else + if (strncmp(&(str[i]),"L2L2", strlen("L2L2"))== 0) /* %L2L2 : Low Level Node 2 of Low Level Node 2 */ + { + if (BIF_LL2 (bif)) + Tool_Unparse2_LLnode (NODE_TEMPLATE_LL2 (BIF_LL2 (bif))); + i += strlen("L2L2"); + } else + if (strncmp(&(str[i]),"FUNHD", strlen("FUNHD"))== 0) /* %FUNHD track down a function header */ + { + PTR_LLND p; + p = BIF_LL1(bif); + while(p && NODE_CODE(p) != FUNCTION_REF) p = NODE_OPERAND0(p); + if(p == NULL) printf("unparse error in FUNHD!!\n"); + else Tool_Unparse2_LLnode(p); + i += strlen("FUNHD"); + } else + if (strncmp(&(str[i]),"SYMBIDFUL", strlen("SYMBIDFUL"))== 0) /* %SYMBID : Symbol identifier */ + { + if (BIF_SYMB(bif) && SYMB_MEMBER_BASENAME(BIF_SYMB(bif))) + { + Tool_Unparse_Symbol(SYMB_MEMBER_BASENAME(BIF_SYMB(bif))); + BufPutString("::",0); + } + Tool_Unparse_Symbol(BIF_SYMB(bif)); + i += strlen("SYMBIDFUL"); + } else + if (strncmp(&(str[i]),"SYMBID", strlen("SYMBID"))== 0) /* %SYMBID : Symbol identifier */ + { + Tool_Unparse_Symbol(BIF_SYMB(bif)); + i += strlen("SYMBID"); + } else + if (strncmp(&(str[i]),"SYMBSCOPE", strlen("SYMBSCOPE"))== 0) /* %SYMBSCOPE : Symbol identifier */ + { + if (BIF_SYMB(bif) && SYMB_MEMBER_BASENAME(BIF_SYMB(bif))) + { + Tool_Unparse_Symbol(SYMB_MEMBER_BASENAME(BIF_SYMB(bif))); + } + i += strlen("SYMBSCOPE"); + } else + if (strncmp(&(str[i]),"SYMBDC", strlen("SYMBDC"))== 0) /* %SYMBSCOPE : Symbol identifier */ + { + if (BIF_LL3(bif) || + (BIF_SYMB(bif) && SYMB_MEMBER_BASENAME(BIF_SYMB(bif)))) + { + BufPutString("::",0); + } + i += strlen("SYMBDC"); + } else + + if (strncmp(&(str[i]),"STATENO", strlen("STATENO"))== 0) /* %STATENO : Statement number */ + { + if (BIF_LABEL_USE (bif)) + { + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL_USE (bif)))); + } + i += strlen("STATENO"); + } else + if (strncmp(&(str[i]),"LABELENDIF", strlen("LABELENDIF"))== 0) /* %STATENO : Statement number */ + { + PTR_BFND temp; + PTR_BLOB blob; + + temp = NULL; + if (!BIF_BLOB2(bif)) + blob = BIF_BLOB1(bif); + else + blob = BIF_BLOB2(bif); + for (;blob; blob = BLOB_NEXT (blob)) + { + temp = BLOB_VALUE(blob); + if (temp && (BIF_CODE(temp) == CONTROL_END)) + { + if (BIF_LABEL(temp)) + break; + } + temp = NULL; + } + if (temp && BIF_LABEL(temp)) + { + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); + } + i += strlen("LABELENDIF"); + } else + if (strncmp(&(str[i]),"LABNAME", strlen("LABNAME")) == 0) /* %LABNAME for C labels: added by dbg */ + { + if(BIF_LABEL_USE(bif)){ + if(LABEL_SYMB(BIF_LABEL_USE(bif))) + BufPutString (SYMB_IDENT(LABEL_SYMB(BIF_LABEL_USE(bif))), 0); + else printf("label-symbol error\n"); + } else printf("label error\n"); + i += strlen("LABNAME"); + } else + if (strncmp(&(str[i]),"LABEL", strlen("LABEL"))== 0) /* %STATENO : Statement number */ + { + if (BIF_LABEL(bif)) + { + /*if (BIF_CP(bif)&&(BIF_CODE(BIF_CP(bif))==FOR_NODE)&& + (HPF_VERSION==2)) + { + AfterDOLabel=1; + if (BIF_LABEL_USE(BIF_CP(bif))&& + (LABEL_STMTNO (BIF_LABEL(bif))!=LABEL_STMTNO (BIF_LABEL_USE(BIF_CP(bif))))) + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); + else if (!BIF_LABEL_USE(BIF_CP(bif))) + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); + } + else21052001*/ + /*BufPutInt (LABEL_STMTNO (BIF_LABEL(bif)));*/ + if (BIF_CP(bif)&&(BIF_CODE(BIF_CP(bif))==FOR_NODE)&& + (HPF_VERSION==2)) + { + AfterDOLabel=1; + if (BIF_LABEL_USE(BIF_CP(bif))&& + (LABEL_STMTNO (BIF_LABEL(bif))!=LABEL_STMTNO (BIF_LABEL_USE(BIF_CP(bif))))) + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); + else if (!BIF_LABEL_USE(BIF_CP(bif))) + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); + } + else + BufPutInt ((int)(LABEL_STMTNO (BIF_LABEL(bif)))); + } + i += strlen("LABEL"); + } else + if (strncmp(&(str[i]),"SYMBTYPE", strlen("SYMBTYPE"))== 0) /* SYMBTYPE : Type of Symbol */ + { + if (BIF_SYMB (bif) && SYMB_TYPE (BIF_SYMB (bif))) + { + if (Check_Lang_Fortran(cur_proj)) + BufPutString ( ftype_name [type_index (TYPE_CODE (SYMB_TYPE (BIF_SYMB (bif))))],0); + else if((SYMB_ATTR(BIF_SYMB(bif)) & OVOPERATOR ) == 0){ + PTR_LLND el; + el = BIF_LL1(bif); + if((BIF_CODE(BIF_CP(bif)) == TEMPLATE_FUNDECL) && + el && NODE_TYPE(el)) + Tool_Unparse_Type(NODE_TYPE(el)); + else + Tool_Unparse_Type(SYMB_TYPE (BIF_SYMB (bif))); + } + } + i += strlen("SYMBTYPE"); + } else + if (strncmp(&(str[i]),"CNSTF", strlen("CNSTF"))== 0) /* for const functions */ + { + PTR_SYMB symb; + if (BIF_SYMB (bif)){ + symb = BIF_SYMB (bif); + /* if(SYMB_TEMPLATE_DUMMY8(symb) & 64) BufPutString(" const",0); */ + } + i += strlen("CNSTF"); + } else + if (strncmp(&(str[i]),"VARLISTTY", strlen("VARLISTTY"))== 0) /* %VARLIST : list of variables / parameters */ + { + PTR_SYMB symb; + if (BIF_SYMB (bif)) + symb = SYMB_FUNC_PARAM (BIF_SYMB (bif)); + else + symb = NULL; + while (symb) + { + Tool_Unparse_Type (SYMB_TYPE(symb)); + BufPutString (" ",0); + BufPutString ( SYMB_IDENT (symb),0); + symb = SYMB_NEXT_DECL (symb); + if (symb) BufPutString (", ",0); + } + i += strlen("VARLISTTY"); + } else + if (strncmp(&(str[i]),"TMPLARGS", strlen("TMPLARGS"))== 0) + { + PTR_SYMB symb; + PTR_LLND args, arg_item, t; + PTR_TYPE typ; + if(BIF_CODE(bif) == FUNC_HEDR) args = BIF_LL3(bif); + else args = BIF_LL1(bif); + while (args ) + { + int typflag; + arg_item = NODE_OPERAND0(args); + if(arg_item == NULL) printf("MAJOR TEMPLATE UNPARSE ERROR. contact dbg \n"); + t = arg_item; + typflag = 1; + while(t && typflag){ + if((NODE_CODE(t) == VAR_REF) || (NODE_CODE(t) == ARRAY_REF)) + typflag = 0; + else if (NODE_CODE(t) == SCOPE_OP) t = NODE_OPERAND1(t); + else t = NODE_OPERAND0(t); + } + if(t){ + symb = NODE_SYMB(t); + typ = NODE_TYPE(t); + if(typ == NULL) typ = SYMB_TYPE(symb); + if((int)strlen(symb->ident) > 0){ /* special case for named arguments */ + typflag = 1; + while(typ && typflag){ + if(TYPE_CODE(typ) == T_ARRAY || + TYPE_CODE(typ) == T_FUNCTION || + TYPE_CODE(typ) == T_REFERENCE || + TYPE_CODE(typ) == T_POINTER) typ = TYPE_BASE(typ); + else if(TYPE_CODE(typ) == T_MEMBER_POINTER) + typ = TYPE_COLL_BASE(typ); + else typflag = 0; + } + } + else BufPutString("class ", 0); + Tool_Unparse_Type (typ); + BufPutString (" ",0); + } + /* else printf("could not find var ref!\n"); */ + Tool_Unparse2_LLnode(arg_item); + args = NODE_OPERAND1(args); + if (args) BufPutString (", ",0); + } + i += strlen("TMPLARGS"); + } else + if (strncmp(&(str[i]),"CONSTRU", strlen("CONSTRU"))== 0) + { + PTR_LLND ll; + if (BIF_LL1(bif)) + { + ll = NODE_OPERAND0(BIF_LL1(bif)); + if (ll) + ll = NODE_OPERAND1(ll); + if (ll) + { + BufPutString (":",0); + Tool_Unparse2_LLnode(ll); + } + } + i += strlen("CONSTRU"); + } else + if (strncmp(&(str[i]),"L1SYMBID", strlen("L1SYMBID"))== 0) /* %L1SYMBID : Symbol of Low Level Node 1 */ + { + if (BIF_LL1 (bif)) + Tool_Unparse_Symbol (NODE_SYMB (BIF_LL1 (bif))); + i += strlen("L1SYMBID"); + } else + if (strncmp(&(str[i]),"VARLIST", strlen("VARLIST"))== 0) /* %VARLIST : list of variables / parameters */ + { + PTR_SYMB symb; + if (BIF_SYMB (bif)) + symb = SYMB_FUNC_PARAM (BIF_SYMB (bif)); + else + symb = NULL; + while (symb) + { + BufPutString ( SYMB_IDENT (symb),0); + symb = SYMB_NEXT_DECL (symb); + if (symb) BufPutString (", ",0); + } + i += strlen("VARLIST"); + } else + if (strncmp(&(str[i]),"DELETE_COMMA", strlen("DELETE_COMMA"))== 0) /* %CHECK_HEAP : Pointer HEAP(PA) -> PA */ + { + if (Buf_address[Buf_pointer-1]==',') + Buf_pointer--; + i += strlen("DELETE_COMMA"); + } else + if (strncmp(&(str[i]),"TASKERROR0", strlen("TASKERROR0"))== 0) + { + /*Message("Error in block-task-region\n end-task-region-directive: CDVM$ END TASK_REGION is missing before TASK_REGION construct.",BIF_LINE(bif));*/ + fprintf(stderr,"Error 210 on line %d of %s : End-task-region-directive: CDVM$ END TASK_REGION is missing before TASK_REGION construct\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + i += strlen("TASKERROR0"); + } else + if (strncmp(&(str[i]),"TASKERROR1", strlen("TASKERROR1"))== 0) + { + /*Message("Error in block-task-region\n task-region-directive: CDVM$ TASK_REGION is missing before on-block.",BIF_LINE(bif));*/ + fprintf(stderr,"Error 211 on line %d of %s : CDVM$ TASK_REGION is missing before on-block \n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + i += strlen("TASKERROR1"); + } else + if (strncmp(&(str[i]),"TASKERROR2", strlen("TASKERROR2"))== 0) + { + /*Message("Error in block-task-region\n task-region-directive: CDVM$ TASK_REGION is missing before end-on-directive.",BIF_LINE(bif));*/ + fprintf(stderr,"Error 212 on line %d of %s : CDVM$ TASK_REGION is missing before end-on-directive \n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + i += strlen("TASKERROR2"); + } else + if (strncmp(&(str[i]),"TASKERROR3", strlen("TASKERROR3"))== 0) + { + /*Message("Error in block-task-region\n end-task-region-directive: CDVM$ END TASK_REGION is missing before end of program.",BIF_LINE(bif));*/ + fprintf(stderr,"Error 213 on line %d of %s : End-task-region-directive: CDVM$ END TASK_REGION is missing before end of program\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + i += strlen("TASKERROR3"); + } else + if (strncmp(&(str[i]),"TASKERROR4", strlen("TASKERROR4"))== 0) + { + /*Message("Error in block-task-region\n task-region-directive: CDVM$ TASK_REGION is missing before end-task-region-directive.",BIF_LINE(bif));*/ + fprintf(stderr,"Error 214 on line %d of %s : End-task-region-directive: CDVM$ TASK_REGION is missing before end-task-region-directive\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + i += strlen("TASKERROR4"); + } else + if (strncmp(&(str[i]),"ONERROR", strlen("ONERROR"))== 0) + { + if (On_count) + { + fprintf(stderr,"Error 215 on line %d of %s : Error in TASK_REGION construct\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + fprintf(stderr,"Warning 216 on line %d of %s : Incorrect number of ON and END_ON directives\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + /*Message("Error in TASK_REGION construct\nThis may be due to incorrect number of ON and END_ON directives.",BIF_LINE(bif));*/ + On_count=0; + } + i += strlen("ONERROR"); + } else + if (strncmp(&(str[i]),"HPF1_POINTER", strlen("HPF1_POINTER"))== 0) + { + /*Message("Error in DVM_POINTER_DIR\n Can`t work this pointers in HPF1.",BIF_LINE(bif));*/ + fprintf(stderr,"Error 197 on line %d of %s : Can`t work this pointers in HPF1\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + i += strlen("HPF1_POINTER"); + } else + if (strncmp(&(str[i]),"ONPLUS", strlen("ONPLUS"))== 0) + { + int k=0; + On_count++; + Treat_Flag("(ON_REGION)",&k,1); + i += strlen("ONPLUS"); + } + else + if (strncmp(&(str[i]),"ONMINUS", strlen("ONMINUS"))== 0) + { + int k=0; + On_count--; + Treat_Flag("(ON_REGION)",&k,-1); + i += strlen("ONMINUS"); + } + else + if (strncmp(&(str[i]),"ONINIT", strlen("ONINIT"))== 0) + { + On_count=0; + i += strlen("ONINIT"); + } + else + if (strncmp(&(str[i]),"RESETID", strlen("RESETID"))== 0) + { + ResetSymbolId(); + i += strlen("RESETID"); + } + else + if (strncmp(&(str[i]),"CHANGE_TABNUMBER", strlen("CHANGE_TABNUMBER"))== 0) + { + int count=0,label; + count=NumberOfForNode(bif,&label); + if(count) + TabNumber-=count; + if (TabNumber<1) TabNumber=1; + i += strlen("CHANGE_TABNUMBER"); + } + else + if (strncmp(&(str[i]),"FIND_DO", strlen("FIND_DO"))== 0) /* %FIND_DO : We need to find the corresponding FOR_NODE for CONT_STAT*/ + { + i += strlen("FIND_DO"); + /*21052001if (UnparseEndofCircle(bif)) + return Buf_address;*/ + if (UnparseEndofCircle(bif)) + return Buf_address; + } else + if (strncmp(&(str[i]),"SAVE", strlen("SAVE"))== 0) + { + Pointer=Buf_pointer; + i += strlen("SAVE"); + } else + if (strncmp(&(str[i]),"LOAD", strlen("LOAD"))== 0) + { + Buf_pointer=Pointer; + i += strlen("LOAD"); + } else + if (strncmp(&(str[i]),"FIND_MAP", strlen("FIND_MAP"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + PTR_LLND llnd; + if (BIF_LL1(bif)) + { + llnd=FindMapDir(BIF_LL1(bif),bif); + if (llnd) + { + Tool_Unparse2_LLnode(llnd); + } + else + { + /*Message ("Error in Unparse, can`t find a corresponding MAP_DIR", BIF_LINE(bif));*/ + fprintf(stderr,"Error 195 on line %d of %s : Can`t find a corresponding MAP_DIR\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + Tool_Unparse2_LLnode(BIF_LL1(bif)); + } + } + i += strlen("FIND_MAP"); + } else + if (strncmp(&(str[i]),"CHECK_REDISTRIBUTE_DIR", strlen("CHECK_REDISTRIBUTE_DIR"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + PTR_LLND llnd; + if (BIF_LL1(bif)) + { + for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + if (FindRedistributeDir(NODE_OPERAND0(llnd),bif)) + { + if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) + { + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("DYNAMIC ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString("\n",0); + } + } + } + i += strlen("CHECK_REDISTRIBUTE_DIR"); + } else + if (strncmp(&(str[i]),"CHECK_REALIGN_DIR", strlen("CHECK_REALIGN_DIR"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + PTR_LLND llnd; + if (BIF_LL1(bif)) + { + for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + if (FindRealignDir(NODE_OPERAND0(llnd),bif)) + { + if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) + { + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("DYNAMIC ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString("\n",0); + } + } + } + i += strlen("CHECK_REALIGN_DIR"); + } else + if (strncmp(&(str[i]),"CHECK_FORMAT_NULL", strlen("CHECK_FORMAT_NULL"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + if (BIF_LL1(bif)) + { + if (!BIF_LL2(bif)&&!BIF_LL3(bif)) + { + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("DYNAMIC ",0); + Tool_Unparse2_LLnode(BIF_LL1(bif)); + BufPutString("\n",0); + return Buf_address; + } + } + i += strlen("CHECK_FORMAT_NULL"); + } else + if (strncmp(&(str[i]),"CHECK_DVMBIT", strlen("CHECK_DVMBIT"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + if (BIF_LL1(bif)) + { + PTR_LLND ptr; + if (BIF_CODE(bif)==DVM_ALIGN_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_ALIGN_BIT) + { + fprintf(stderr,"Error 200 on line %d of %s:Object '%s' already has ALIGN_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_ALIGN_BIT; + } + } + if (BIF_CODE(bif)==HPF_PROCESSORS_STAT) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_PROCESSORS_BIT) + { + fprintf(stderr,"Error 201 on line %d of %s :Object '%s' already has PROCESSORS_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_PROCESSORS_BIT; + } + } + + if (BIF_CODE(bif)==DVM_DISTRIBUTE_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_DISTRIBUTE_BIT) + { + fprintf(stderr,"Error 202 on line %d of %s :Object '%s' already has DISTRIBUTE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_DISTRIBUTE_BIT; + } + } + if (BIF_CODE(bif)==HPF_TEMPLATE_STAT) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_TEMPLATE_BIT) + { + fprintf(stderr,"Error 203 on line %d of %s :Object '%s' already has TEMPLATE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_TEMPLATE_BIT; + } + } + if (BIF_CODE(bif)==DVM_INHERIT_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_INHERIT_BIT) + { + fprintf(stderr,"Error 204 on line %d of %s :Object '%s' already has INHERIT_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_INHERIT_BIT; + } + } + if (BIF_CODE(bif)==DVM_DYNAMIC_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_DYNAMIC_BIT) + { + fprintf(stderr,"Error 205 on line %d of %s :Object '%s' already has DYNAMIC_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_DYNAMIC_BIT; + } + } + if (BIF_CODE(bif)==DVM_SHADOW_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_SHADOW_BIT) + { + fprintf(stderr,"Error 206 on line %d of %s :Object '%s' already has SHADOW_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_SHADOW_BIT; + } + } + if (BIF_CODE(bif)==DVM_TASK_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_TASK_BIT) + { + fprintf(stderr,"Error 207 on line %d of %s :Object '%s' already has TASK_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_TASK_BIT; + } + } + if (BIF_CODE(bif)==DVM_POINTER_DIR) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if (SYMB_ID(NODE_SYMB(llnd))==ALREADY_DVM_POINTER_BIT) + { + fprintf(stderr,"Error 208 on line %d of %s :Object '%s' already has DVM_POINTER_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else SYMB_ID(NODE_SYMB(llnd))=ALREADY_DVM_POINTER_BIT; + } + } + if (BIF_CODE(bif)==DVM_VAR_DECL) + { + PTR_LLND ptr_llnd; + int BIT=0; + for(ptr_llnd=BIF_LL3(bif);ptr_llnd&&(NODE_CODE(ptr_llnd)==EXPR_LIST);ptr_llnd=NODE_OPERAND1(ptr_llnd)) + { + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==DISTRIBUTE_OP) + BIT|=ALREADY_DISTRIBUTE_BIT; + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==PROCESSORS_OP) + BIT|=ALREADY_PROCESSORS_BIT; + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==ALIGN_OP) + { + BIT|=ALREADY_ALIGN_BIT; + } + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==TEMPLATE_OP) + { + BIT|=ALREADY_TEMPLATE_BIT; + } + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==SHADOW_OP) + { + BIT|=ALREADY_SHADOW_BIT; + } + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==DYNAMIC_OP) + { + BIT|=ALREADY_DYNAMIC_BIT; + } + } + for(ptr=BIF_LL1(bif);BIT&&ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + PTR_LLND llnd=NODE_OPERAND0(ptr); + if (!llnd||(NODE_CODE(llnd)!=ARRAY_REF)) break; + if ((BIT&ALREADY_DYNAMIC_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_DYNAMIC_BIT)) + { + fprintf(stderr,"Error 205 on line %d of %s :Object '%s' already has DYNAMIC_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else + if (BIT&ALREADY_DYNAMIC_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_DYNAMIC_BIT; + if ((BIT&ALREADY_PROCESSORS_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_PROCESSORS_BIT)) + { + fprintf(stderr,"Error 201 on line %d of %s :Object '%s' already has PROCESSORS_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else + if (BIT&ALREADY_PROCESSORS_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_PROCESSORS_BIT; + if ((BIT&ALREADY_SHADOW_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_SHADOW_BIT)) + { + fprintf(stderr,"Error 206 on line %d of %s :Object '%s' already has SHADOW_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else + if (BIT&ALREADY_SHADOW_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_SHADOW_BIT; + if ((BIT&ALREADY_TEMPLATE_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_TEMPLATE_BIT)) + { + fprintf(stderr,"Error 203 on line %d of %s :Object '%s' already has TEMPLATE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else + if (BIT&ALREADY_TEMPLATE_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_TEMPLATE_BIT; + if ((BIT&&ALREADY_ALIGN_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_ALIGN_BIT)) + { + fprintf(stderr,"Error 200 on line %d of %s :Object '%s' already has ALIGN_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else + if (BIT&ALREADY_ALIGN_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_ALIGN_BIT; + if ((BIT&ALREADY_DISTRIBUTE_BIT)&&(SYMB_ID(NODE_SYMB(llnd))==ALREADY_DISTRIBUTE_BIT)) + { + fprintf(stderr,"Error 202 on line %d of %s :Object '%s' already has DISTRIBUTE_BIT\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name,SYMB_IDENT(NODE_SYMB(llnd))); + errnumber++; + } + else + if (BIT&ALREADY_DISTRIBUTE_BIT) SYMB_ID(NODE_SYMB(llnd))=ALREADY_DISTRIBUTE_BIT; + } + } + } + i += strlen("CHECK_DVMBIT"); + } else + if (strncmp(&(str[i]),"CHECK_COMBINED_DIR", strlen("CHECK_COMBINED_DIR"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + PTR_LLND llnd; + if (BIF_LL1(bif)) + { + for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + PTR_SYMB s=SMNULL; + if (NODE_CODE(NODE_OPERAND0(llnd))==ARRAY_REF) + s=NODE_SYMB(NODE_OPERAND0(llnd)); + if (!s) continue; + if(s->attr & DISTRIBUTE_BIT) + if (FindRedistributeDir(NODE_OPERAND0(llnd),bif)) + { + if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) + { + BufPutString("\n!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("DYNAMIC ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + } + } + if(s->attr & ALIGN_BIT) + if (FindRealignDir(NODE_OPERAND0(llnd),bif)) + { + if (!FindDynamicDir(NODE_OPERAND0(llnd),bif)) + { + BufPutString("\n!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("DYNAMIC ",0); + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + } + } + + } + } + i += strlen("CHECK_COMBINED_DIR"); + } else + if (strncmp(&(str[i]),"CHECK_SHADOW_OP", strlen("CHECK_SHADOW_OP"))== 0) /* %FIND_MAP : We need to find the corresponding MAP_DIR*/ + { + if (BIF_CODE(bif)==DVM_VAR_DECL) + { + PTR_LLND ptr_llnd; + int OpNumber=0; + int Shadow=0; + for(ptr_llnd=BIF_LL3(bif);ptr_llnd&&(NODE_CODE(ptr_llnd)==EXPR_LIST);ptr_llnd=NODE_OPERAND1(ptr_llnd)) + { + if (NODE_CODE(NODE_OPERAND0(ptr_llnd))==SHADOW_OP) + Shadow=1; + else OpNumber++; + } + if ((Shadow==1)&&!OpNumber) return Buf_address; + } + i += strlen("CHECK_SHADOW_OP"); + } else + if (strncmp(&(str[i]),"CHECK_DAC_DIR", strlen("CHECK_DAC_DIR"))== 0) /* %CHECK_DAC_DIR : We need to find FDVM-directive that can be transformed into DYNAMIC-directive*/ + { + PTR_LLND llnd; + PTR_BFND ptr_bif; + int pointer,beg; + beg=Buf_pointer; + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("DYNAMIC ",0); + pointer=Buf_pointer; + if (BIF_LL1(bif)) + { + for (llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + if ((ptr_bif=FindDistrAlignCombinedDir(NODE_OPERAND0(llnd),bif))) + { + if (!CheckNullDistribution(ptr_bif)) + { + Tool_Unparse2_LLnode(NODE_OPERAND0(llnd)); + BufPutString(" ,",0); + } + } + Buf_pointer--; + BufPutString("\n",0); + } + if (pointer==Buf_pointer) Buf_pointer=beg; + return Buf_address; + i += strlen("CHECK_DAC_DIR"); + } else + if (strncmp(&(str[i]),"UNPARSE_ON", strlen("UNPARSE_ON"))== 0) /* %APPEND_ON : We need to append ON clause after FOR statement in INDEPENDENT cycle for DVM_PARALLEL_TASK_DIR*/ + { + PTR_LLND llnd; + if (On_Clause) + { + llnd=FindMapDir(On_Clause,bif); + if (llnd) + { + BufPutString("!HPF$",0); + Puttab(); + Buf_pointer-=5; + BufPutString("ON ( ",0); + Tool_Unparse2_LLnode(llnd); + BufPutString(" ) BEGIN",0); + } + else + { + /*Message ("Error in Unparse, can`t find a corresponding MAP_DIR for DVM_PARALLEL_TASK_DIR", BIF_LINE(bif));*/ + fprintf(stderr,"Error 195 on line %d of %s : Can`t find a corresponding MAP_DIR\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + } + } + i += strlen("UNPARSE_ON"); + } else + if (strncmp(&(str[i]),"CHECK_ACROSS", strlen("CHECK_ACROSS"))== 0) + { + i = i + strlen("CHECK_ACROSS"); + if (CheckAcross(bif)) return Buf_address; + } else + if (strncmp(&(str[i]),"CHECK_REDUCTION", strlen("CHECK_REDUCTION"))== 0) + { + i = i + strlen("CHECK_REDUCTION"); + if (CheckReduction(bif)) return Buf_address; + } else + if (strncmp(&(str[i]),"FIND_REDUCTION", strlen("FIND_REDUCTION"))== 0) + { + ForNodeStmt(BIF_NEXT(bif)); + i = i + strlen("FIND_REDUCTION"); + } else + + if (strncmp(&(str[i]),"PAR_TASK_MAP", strlen("PAR_TASK_MAP"))== 0) /* %PAR_TASK_MAP : We need to save ON clause for DVM_PARALLEL_TASK_DIR*/ + { + On_Clause=BIF_LL1(bif); + i += strlen("PAR_TASK_MAP"); + } else + if (strncmp(&(str[i]),"CHECK_REDISTRIBUTE_ON_MAP", strlen("CHECK_REDISTRIBUTE_ON_MAP"))== 0) /* %CHECK_REDISTRIBUTE_ON_MAP : We need to find the corresponding MAP_DIR*/ + { + PTR_LLND llnd,ptr_llnd; + if (BIF_LL3(bif)) + { + ptr_llnd=ChangeRedistributeOntoTask(BIF_LL3(bif)); + if (ptr_llnd) + { + llnd=FindMapDir(ptr_llnd,bif); + if (llnd) + { + /*if (HPF_VERSION==1) + Tool_Unparse_Symbol(NODE_SYMB(llnd)); + else */ + Tool_Unparse2_LLnode(llnd); + } + else + { + /*Message ("Error in Unparse, can`t find a corresponding MAP_DIR", BIF_LINE(bif));*/ + fprintf(stderr,"Error 195 on line %d of %s : Can`t find a corresponding MAP_DIR\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + Tool_Unparse2_LLnode(BIF_LL3(bif)); + } + } + else Tool_Unparse2_LLnode(BIF_LL3(bif)); + } + i += strlen("CHECK_REDISTRIBUTE_ON_MAP"); + } else + if (strncmp(&(str[i]),"CHECK_ALLOCATE", strlen("CHECK_ALLOCATE"))== 0) /* %CHECK_ALLOCATE : We must change the allocation for DVM`s pointers*/ + { + PTR_LLND llnd; + if (NODE_CODE(BIF_LL2(bif))==FUNC_CALL) + { + if (!strcmp(SYMB_IDENT(NODE_SYMB(BIF_LL2(bif))),"allocate")) + if ((NODE_CODE(BIF_LL1(bif))==ARRAY_REF)&& (SYMB_ATTR(NODE_SYMB(BIF_LL1(bif)))&DVM_POINTER_BIT)) + { + int count=0; + char *str; + count=FindPointerDir(NODE_SYMB(BIF_LL1(bif)),bif); + if (count) + { + int i=0; + BufPutString("ALLOCATE(", 0); + Tool_Unparse2_LLnode(BIF_LL1(bif)); + BufPutChar('('); + llnd=NODE_OPERAND0(BIF_LL2(bif)); + if (!llnd||(NODE_CODE(llnd)!=EXPR_LIST)) + { + /*Message("Incorrect call of allocate function",BIF_LINE(bif));*/ + fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + BufPutString("))\n", 0); + return Buf_address; + }; + if (NODE_OPERAND0(llnd)) + { + if (NODE_CODE(NODE_OPERAND0(llnd))==VAR_REF) + { + fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + fprintf(stderr,"Warning 197 on line %d of %s : You can`t use variable as SDIM array function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + /*Message("Incorrect call of allocate function",0); + Message("You can`t use variable as SDIM array",BIF_LINE(bif));*/ + BufPutString("))\n", 0); + return Buf_address; + } + else + if (NODE_CODE(NODE_OPERAND0(llnd))==ARRAY_REF) + { + int ar_index=0; + PTR_LLND ptr1=LLNULL,ptr2=LLNULL; + str=SYMB_IDENT(NODE_SYMB(NODE_OPERAND0(llnd))); + llnd=NODE_OPERAND0(NODE_OPERAND0(llnd)); + if (!llnd) + { + for(i=1;i<=count;i++) + { + if (i!=1) BufPutChar(','); + BufPutString(str,0); + BufPutChar('('); + BufPutInt(i); + BufPutChar(')'); + } + BufPutString("))\n", 0); + return Buf_address; + }; + if (NODE_OPERAND0(llnd)) ptr1=NODE_OPERAND0(llnd); + if (NODE_OPERAND1(llnd)) ptr2=NODE_OPERAND0(NODE_OPERAND1(llnd)); + if (ptr1&&!ptr2) + { + /*Message("Incorrect call of allocate function",BIF_LINE(bif));*/ + fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + BufPutString("))\n", 0); + return Buf_address; + }; + if (ptr1&&ptr2&&(NODE_CODE(ptr1)==INT_VAL)) + { + ar_index=1; + if(NODE_CODE(ptr2)==INT_VAL) + { + fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + fprintf(stderr,"Warning 198 on line %d of %s : You can`t use 2 integer constants in SDIM array\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + /*Message("Incorrect call of allocate function",0); + Message("You can`t use 2 integer constants in SDIM array",BIF_LINE(bif));*/ + BufPutString("))\n", 0); + return Buf_address; + }; + } + else + if (ptr1&&ptr2&&(NODE_CODE(ptr2)==INT_VAL)) + ar_index=2; + else + { + fprintf(stderr,"Error 196 on line %d of %s : Incorrect call of allocate function\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + fprintf(stderr,"Warning 199 on line %d of %s : You can`t use 2 variables in SDIM array\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + /* Message("Incorrect call of allocate function",0); + Message("You can`t use 2 variables in SDIM array",BIF_LINE(bif));*/ + BufPutString("))\n", 0); + return Buf_address; + }; + for(i=1;(i<=count)&&ar_index;i++) + { + if (i!=1) BufPutChar(','); + BufPutString(str,0); + BufPutChar('('); + if (ar_index==1) + { + BufPutInt(i); + BufPutChar(','); + Tool_Unparse2_LLnode(ptr2); + } + else + { + Tool_Unparse2_LLnode(ptr1); + BufPutChar(','); + BufPutInt(i); + } + BufPutChar(')'); + } + } + } + BufPutChar(')'); + BufPutString(")\n", 0); + return Buf_address; + } + } + } + else + if (NODE_CODE(bif)==ASSIGN_STAT) + { + llnd=BIF_LL1(bif); + if(((NODE_CODE(llnd)==ARRAY_REF)||(NODE_CODE(llnd)==VAR_REF))&&NODE_SYMB(llnd)&&(SYMB_ATTR(NODE_SYMB(llnd))&DVM_POINTER_BIT)) + /* if(SYMB_ATTR(NODE_SYMB(llnd))&DVM_POINTER_BIT)*/ + { + Tool_Unparse2_LLnode(llnd); + BufPutString(" => ",0); + Tool_Unparse2_LLnode(BIF_LL2(bif)); + BufPutString("\n",0); + return Buf_address; + } + } + i += strlen("CHECK_ALLOCATE"); + } else + if (strncmp(&(str[i]),"CHECK_FUNC", strlen("CHECK_FUNC"))== 0) /* %CHECK_FUNC : We must change function ALLOCATE*/ + { + int count=0; + if (NODE_SYMB(bif)) + if (!strcmp(SYMB_IDENT(NODE_SYMB(bif)),"ALLOCATE")|| + !strcmp(SYMB_IDENT(NODE_SYMB(bif)),"allocate")) + Treat_Flag("(COMMENT)",&count,1); + i += strlen("CHECK_FUNC"); + } else + if (strncmp(&(str[i]),"DECLARATION", strlen("DECLARATION"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ + { + int count=0; + if (BIF_LL1(bif)) count=FindPointerDeclaration(BIF_LL1(bif)); + if (!count) + { + count=0; + Treat_Flag("(POINTER)",&count, 1); + } + i += strlen("DECLARATION"); + } else + if (strncmp(&(str[i]),"DESCRIPTOR", strlen("DESCRIPTOR"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ + { + PTR_LLND ptr,llnd; + if (BIF_LL1(bif)) + { + for(ptr=BIF_LL1(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + { + if (NODE_OPERAND0(ptr)) + { + llnd=FindPointerDescriptor(NODE_OPERAND0(ptr),bif); + if (NODE_OPERAND0(llnd)) continue; + if (!llnd) + { + /*Message("Can`t find a descriptor for POINTER",BIF_LINE(bif));*/ + fprintf(stderr,"Error 222 on line %d of %s : Can`t find a descriptor for POINTER\n",BIF_LINE(bif),BIF_FILE_NAME(bif)->name); + errnumber++; + Tool_Unparse2_LLnode(NODE_OPERAND0(ptr)); + } + else Tool_Unparse2_LLnode(llnd); + BufPutChar(','); + } + } + Buf_pointer--; + } + i += strlen("DESCRIPTOR"); + } else + if (strncmp(&(str[i]),"PTR_ARRAY", strlen("PTR_ARRAY"))== 0) /* %DECLARATION : We need to change declaration of Pointer descriptor */ + { + if (BIF_LL1(bif)) + { + ArrayOfPointerDeclaration(bif); + PointerDeclaration(bif); + return Buf_address; + } + i += strlen("PTR_ARRAY"); + } else + + if (strncmp(&(str[i]),"RIDPT", strlen("RIDPT"))== 0) + { + PTR_TYPE type = NULL; + + type = Find_Type_For_Bif(bif); + if (type ) + { + DealWith_Rid(type, In_Class_Flag); + } + else if(BIF_CODE(bif) == CLASS_DECL) + { + DealWith_Rid(SYMB_TYPE(BIF_SYMB(bif)), In_Class_Flag); + } + i += strlen("RIDPT"); + } else + if (strncmp(&(str[i]),"INCLASSON", strlen("INCLASSON"))== 0) + { + In_Class_Flag = 1; + i += strlen("INCLASSON"); + } else + if (strncmp(&(str[i]),"INCLASSOFF", strlen("INCLASSOFF"))== 0) + { + In_Class_Flag = 0; + i += strlen("INCLASSOFF"); + } else + if (strncmp(&(str[i]),"INWRITEON", strlen("INWRITEON"))== 0) /* %INWRITEON : In_Write_Statement Flag ON */ + { + In_Write_Flag = 1; + i += strlen("INWRITEON"); + } else + if (strncmp(&(str[i]),"INWRITEOFF", strlen("INWRITEOFF"))== 0) /* %INWRITEOFF : In_Write_Statement Flag OFF */ + { + In_Write_Flag = 0; + i += strlen("INWRITEOFF"); + } else + if (strncmp(&(str[i]),"RECPORTON", strlen("RECPORTON"))== 0) /* %RECPORTON : recursive_port_decl Flag ON */ + { + Rec_Port_Decl = 1; + i += strlen("RECPORTON"); + } else + if (strncmp(&(str[i]),"RECPORTOFF", strlen("RECPORTOFF"))== 0) /* %RECPORTOFF : recursive_port_decl Flag OFF */ + { + Rec_Port_Decl = 0; + i += strlen("RECPORTOFF"); + } else + + if (strncmp(&(str[i]),"INPARAMON", strlen("INPARAMON"))== 0) /* %INPARAMON : In_Param_Statement Flag ON */ + { + In_Param_Flag = 1; + i += strlen("INPARAMON"); + } else + if (strncmp(&(str[i]),"INPARAMOFF", strlen("INPARAMOFF"))== 0) /* %INPARAMOFF : In_Param_Statement Flag OFF */ + { + In_Param_Flag = 0; + i += strlen("INPARAMOFF"); + } else + if (strncmp(&(str[i]),"INIMPLION", strlen("INIMPLION"))== 0) /* %INIMPLION : In_Impli_Statement Flag ON */ + { + In_Impli_Flag = 1; + i += strlen("INIMPLION"); + } else + if (strncmp(&(str[i]),"INIMPLIOFF", strlen("INIMPLIOFF"))== 0) /* %INIMPLIOFF : In_Impli_Statement Flag OFF */ + { + In_Impli_Flag = 0; + i += strlen("INIMPLIOFF"); + } else + if (strncmp(&(str[i]),"TYPE", strlen("TYPE"))== 0) + { + PTR_TYPE type = NULL; + type = Find_Type_For_Bif(bif); + if (!type) + { + Message("TYPE not found",0); + BufPutString("------TYPE ERROR----",0); + } + if( !is_overloaded_type(bif) ) + Tool_Unparse_Type (type); + i += strlen("TYPE"); + } else + if (strncmp(&(str[i]),"PROTECTION", strlen("PROTECTION"))== 0) + { + int protect = 0; /*protect = NULL*/ + protect = Find_Protection_For_Bif(bif); + if (protect) + { + if (protect & 128) + { + /* BufPutString("MethodOfElement:\n",0); a temporary fix until dep2C++ done */ + BufPutString("public:\n", 0); + } else + { + switch (protect) + { /* find the definition of the flag someday */ + case 64: BufPutString("public:\n",0); break; + case 32: BufPutString("protected:\n",0); break; + case 16: BufPutString("private:\n",0); break; + } + } + } + i += strlen("PROTECTION"); + } else + if (strncmp(&(str[i]),"DUMMY", strlen("DUMMY"))== 0) /* %DUMMY Do nothing */ + { + i += strlen("DUMMY"); + } else + { + Message (" *** Unknown bif node COMMAND *** ",0); + Message (&(str[i]),0); + } + } + else + { + BufPutChar( c); + i++; + } + c = str[i]; + } + if (AfterDOLabel&&(HPF_VERSION==2)) UnparseEndofCircle(bif); + if (TASK_PROC_GENERATE&&(HPF_VERSION==2)&&(variant==DVM_ON_DIR)) + { + ON_BLOCK=1; + ON_BEGIN=Buf_pointer; + }; + return Buf_address; +} + +void DefineHPF1() +{ +#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].str = create_unp_str( NAME); +#include"unparse1.hpf" +#undef DEFNODECODE + +#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].fct = NULL; +#include"unparse1.hpf" +#undef DEFNODECODE +} +void DefineHPF2() +{ +#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].str = create_unp_str( NAME); +#include"unparse.hpf" +#undef DEFNODECODE + +#define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].fct = NULL; +#include"unparse.hpf" +#undef DEFNODECODE +} + +void Init_HPFUnparser() +{ + int i,j; + CommentOut=1; /*Ignore comment*/ + if (Parser_Initiated != Fortran_Initialized) + { + /*printf("\nHPF_VERSION = %d",HPF_VERSION);*/ + if (HPF_VERSION==1) DefineHPF1(); + else DefineHPF2(); + Parser_Initiated = Fortran_Initialized; + /* set the first tabulation */ + TabNumber = 1; + } + /* initialize the number of flag */ + Number_Of_Flag = 0; + for (i=0; i < MAXFLAG ; i++) + { + TabOfFlag[i][0] = '\0'; + FlagLenght[i] = 0; + for(j=0; jhead_bfnd))); + return errnumber; +} + +PTR_LLND FindMapDir(PTR_LLND ptr_llnd,PTR_BFND ptr_bif) +{ +PTR_BFND bif,end=FindEndOfBlock(ptr_bif); +for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) + { + if (BIF_CODE(bif)!=DVM_MAP_DIR) continue; + if (patternMatchExpression(ptr_llnd,BIF_LL1(bif))) + { + return BIF_LL2(bif); + } + } +return NULL; +} + +int NumberOfForNode(PTR_BFND ptrbif,int *label) +{ +int incircle=0; +PTR_BFND bif,end=FindEndOfBlock(ptrbif); +int count=0; +if (!BIF_LABEL(ptrbif)) return 0; +for(bif=FindBeginingOfBlock(ptrbif);bif&&(bif!=end);bif=BIF_NEXT(bif)) + { + if (incircle&&(BIF_CODE(bif)==GOTO_NODE)) + { + if (BIF_LL3(bif)&&NODE_LABEL (BIF_LL3(bif))) + { + if ( LABEL_STMTNO (NODE_LABEL (BIF_LL3(bif)))==LABEL_STMTNO (BIF_LABEL(ptrbif))) + { + *label=LABEL_STMTNO (BIF_LABEL(ptrbif)); + } + } + + } + if (BIF_CODE(bif)!=FOR_NODE) continue; + if (BIF_LABEL_USE (bif)) + { + if (LABEL_STMTNO (BIF_LABEL_USE (bif))==LABEL_STMTNO (BIF_LABEL(ptrbif))) + { + count++; + incircle=1; + } + + } + } +return count; +} + +int FindPointerDir(PTR_SYMB symb,PTR_BFND ptr_bif) +{ +PTR_BFND bif,end=FindEndOfBlock(ptr_bif); +for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) + { + PTR_LLND llnd; + if (BIF_CODE(bif)!=DVM_POINTER_DIR) continue; + for(llnd=BIF_LL1(bif);llnd;llnd=NODE_OPERAND1(llnd)) + if (!strcmp(SYMB_IDENT(NODE_SYMB(NODE_OPERAND0(llnd))),SYMB_IDENT(symb))) + { + int count=0; + char *str; + str=funparse_llnd(BIF_LL2(bif)); + for(;*str!='\0';str++) + if (*str==':') count++; + return count; + } + } +return 0; +} + +int Puttab() +{ + int j; + for (j = 0; j < TabNumber; j++) + if (j>0) + BufPutString (" ",0); + else + BufPutString (" ",0); /* cychen */ +return j; +} + +PTR_LLND ChangeRedistributeOntoTask(PTR_LLND llnd) +{ +if (!llnd) return LLNULL; +/*if (NODE_CODE(llnd)!=EXPR_LIST) return LLNULL; +if (NODE_CODE(NODE_OPERAND0(llnd))!=ARRAY_REF) return LLNULL; +if (!(SYMB_ATTR(NODE_SYMB(NODE_OPERAND0(llnd)))&TASK_BIT)) return LLNULL;*/ +if (NODE_CODE(llnd)!=ARRAY_REF) return LLNULL; +if (!(SYMB_ATTR(NODE_SYMB(llnd))&TASK_BIT)) return LLNULL; +/*return NODE_OPERAND0(llnd);*/ +return llnd; +} + +int Find_SaveSymbol(PTR_LLND llnd) +{ +int i; +if (NODE_CODE(llnd)!=VAR_REF) return 0; +for (i=0;ihead_bfnd;bif;bif=BIF_NEXT(bif)) + { + PTR_LLND ptr; + int ok=0; + if (BIF_CODE(bif)!=DVM_VAR_DECL) continue; + for(ptr=BIF_LL3(bif);ptr&&(NODE_CODE(ptr)==EXPR_LIST);ptr=NODE_OPERAND1(ptr)) + if (NODE_CODE(NODE_OPERAND0(ptr))==DYNAMIC_OP) ok=1; + if (BIF_LL1(bif)) + { + PTR_LLND llnd; + for(llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + if (patternMatchExpression(ptr_llnd,NODE_OPERAND0(llnd))) + if (ok) + return NODE_OPERAND0(llnd); + else return 0; + } + } + } +*/ +return NULL; +} + +int UnparseEndofCircle(PTR_BFND bif) +{ +int TabNum; +int i,count=0,label=0; +TabNum=TabNumber; +count=NumberOfForNode(bif,&label); +if (label) BufPutInt(label); +if(count) + { + if (TabNumber>1) TabNumber--; + for(i=0;i1) TabNumber--; + k=0; + Treat_Flag("(TASK_DIR)",&k,-1); + TabNum--; + } + } + Puttab(); + if (TabNumber>1) TabNumber--; + BufPutString("end do\n",0); + } + TabNumber=TabNum; + } +return count; +/*21052001*/ +} + +PTR_BFND FindDistrAlignCombinedDir(PTR_LLND ptr_llnd,PTR_BFND ptr_bif) +{ +PTR_BFND bif,end=FindEndOfBlock(ptr_bif); +for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) + { + if ((BIF_CODE(bif)==DVM_DISTRIBUTE_DIR)|| + (BIF_CODE(bif)==DVM_ALIGN_DIR)|| + (BIF_CODE(bif)==DVM_VAR_DECL)) + { + if (BIF_LL1(bif)) + { + PTR_LLND llnd; + for(llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + if (patternMatchExpression(ptr_llnd,NODE_OPERAND0(llnd))) + { + return bif; + } + } + } + } + } +return NULL; +} + +PTR_LLND FindPointerDescriptor(PTR_LLND ptr_llnd,PTR_BFND ptr_bif) +{ +PTR_BFND bif,end=FindEndOfBlock(ptr_bif); +for(bif=FindBeginingOfBlock(ptr_bif);bif&&(bif!=end);bif=BIF_NEXT(bif)) + { + if (BIF_CODE(bif)!=VAR_DECL) continue; + if (BIF_LL1(bif)) + { + PTR_LLND llnd; + for(llnd=BIF_LL1(bif);llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + if (!strcmp(SYMB_IDENT(NODE_SYMB(ptr_llnd)),SYMB_IDENT(NODE_SYMB(NODE_OPERAND0(llnd))))) + return NODE_OPERAND0(llnd); + } + } + } +return NULL; +} + +int ArrayOfPointerDeclaration(PTR_BFND bif) +{ +PTR_LLND llnd=BIF_LL1(bif); +int ok=1,empty=1; +int count=0; +for (;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + PTR_LLND descr; + descr=FindPointerDescriptor(NODE_OPERAND0(llnd),bif); + if (ok&&!count) + { + char *str=NULL; + str=funparse_llnd(BIF_LL2(bif)); + for(;*str!='\0';str++) + if (*str==':') count++; + } + if (count&&descr&&NODE_OPERAND0(descr)) + { + char FLAG[10]; + int i=0; + if (count>10) + { + Message("Can`t work : too many dimensions in DVM_POINTER_DIR",0); + return 0; + } + sprintf(FLAG,"(PTRTYPE%i)",count); + if (ok&&!Get_Flag_val(FLAG,&i)) + { + i=0; + Treat_Flag(FLAG, &i,1); + GenerateType(count, BIF_LL3(bif)); + }; + if(ok) + { + Puttab(); + BufPutString("TYPE (PTR_ARRAY",0); + BufPutInt(count); + BufPutString(") :: ",0); + ok=0; + empty=0; + BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); + SYMB_ATTR(NODE_SYMB(descr))|=DVM_POINTER_ARRAY_BIT; + BufPutChar('('); + Tool_Unparse2_LLnode(NODE_OPERAND0(descr)); + BufPutChar(')'); + continue; + } + if (!ok) + { + empty=0; + BufPutChar(','); + BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); + SYMB_ATTR(NODE_SYMB(descr))|=DVM_POINTER_ARRAY_BIT; + BufPutChar('('); + Tool_Unparse2_LLnode(NODE_OPERAND0(descr)); + BufPutChar(')'); + } + } + } +if (!empty) BufPutChar('\n'); +return 1; +} + +void PointerDeclaration(PTR_BFND bif) +{ +PTR_LLND llnd=BIF_LL1(bif); +int ok=1,empty=1; +int count=0; +for (;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + PTR_LLND descr; + descr=FindPointerDescriptor(NODE_OPERAND0(llnd),bif); + if (ok&&!count) + { + char *str=NULL; + str=funparse_llnd(BIF_LL2(bif)); + for(;*str!='\0';str++) + if (*str==':') count++; + } + if ((count)&&descr&&!NODE_OPERAND0(descr)) + { + if(ok) + { + Puttab(); + Tool_Unparse2_LLnode(BIF_LL3(bif)); + BufPutString(", POINTER, DIMENSION (",0); + Tool_Unparse2_LLnode(BIF_LL2(bif)); + BufPutString(") :: ",0); + ok=0; + empty=0; + BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); + continue; + } + if (!ok) + { + empty=0; + BufPutChar(','); + BufPutString(SYMB_IDENT(NODE_SYMB(descr)),0); + } + } + } +if (!empty) BufPutChar('\n'); +} + +void GenerateType(int count, PTR_LLND llnd_type) +{ + int i; + Puttab(); + BufPutString("TYPE PTR_ARRAY",0); + BufPutInt(count); + BufPutChar('\n'); + TabNumber++; + Puttab(); + Tool_Unparse2_LLnode(llnd_type); + BufPutString(", POINTER, DIMENSION (",0); + for(i=0;i= 0 ; i --) + if ( filename[i] == '.' ) + break; + hpfname[i+1]='h'; + hpfname[i+2]='p'; + hpfname[i+3]='f'; + hpfname[i+4]='\0'; +} + +int CheckNullDistribution(PTR_BFND bif) +{ +if (BIF_CODE(bif)==DVM_DISTRIBUTE_DIR) + { + if (!BIF_LL2(bif)&&!BIF_LL3(bif)) + return 1; + }; +if (BIF_CODE(bif)==DVM_ALIGN_DIR) + { + if (!BIF_LL2(bif)&&!BIF_LL3(bif)) + return 1; + }; +if (BIF_CODE(bif)==DVM_VAR_DECL) + { + PTR_LLND llnd=BIF_LL3(bif); + for(;llnd&&(NODE_CODE(llnd)==EXPR_LIST);llnd=NODE_OPERAND1(llnd)) + { + /*BufPutInt(NODE_CODE(NODE_OPERAND0(llnd)));*/ + if (NODE_CODE(NODE_OPERAND0(llnd))==DYNAMIC_OP) + return 1; + if (NODE_CODE(NODE_OPERAND0(llnd))==DISTRIBUTE_OP) + if (!NODE_OPERAND0(NODE_OPERAND0(llnd))&&!NODE_OPERAND1(NODE_OPERAND0(llnd))) + return 1; + if (NODE_CODE(NODE_OPERAND0(llnd))==ALIGN_OP) + if (!NODE_OPERAND0(NODE_OPERAND0(llnd))&&!NODE_OPERAND1(NODE_OPERAND0(llnd))) + return 1; + }; + } +/*BufPutInt(1234567);*/ +return 0; +} + +void ResetSymbolId() +{ +int i,j; +char FLAG[10]; +PTR_SYMB symb; +for (symb = current_file->head_symb; symb ; symb = SYMB_NEXT (symb)) + SYMB_ID (symb) = 0; +for (j=0;j<10;j++) + { + sprintf(FLAG,"(PTRTYPE%i)",j); + i=0; + if (Get_Flag_val(FLAG,&i)) + { + i=0; + Treat_Flag(FLAG, &i,-1); + }; + } +} + +PTR_BFND FindBeginingOfBlock(PTR_BFND ptr_bif) +{ +PTR_BFND bif,ptr=NULL; +for(bif=current_file->head_bfnd;bif&&(bif!=ptr_bif);bif=BIF_NEXT(bif)) + { + if ((BIF_CODE(bif)==PROG_HEDR)|| + (BIF_CODE(bif)==PROC_HEDR)|| + (BIF_CODE(bif)==PROS_HEDR)) + ptr=bif; + } +return ptr; +} + +PTR_BFND FindEndOfBlock(PTR_BFND ptr_bif) +{ +PTR_BFND bif; +for(bif=ptr_bif;bif;bif=BIF_NEXT(bif)) + { + if (BIF_CODE(bif)==CONTROL_END) + if ((BIF_CODE(BIF_CP(bif))==PROG_HEDR)|| + (BIF_CODE(BIF_CP(bif))==PROC_HEDR)|| + (BIF_CODE(BIF_CP(bif))==PROS_HEDR)) + { + return bif; + } + } +return NULL; +} + +int CheckAcross(PTR_BFND bif) +{ +PTR_LLND llnd; +if (!bif) return 0; +for (llnd=BIF_LL2(bif);llnd&&NODE_CODE(llnd)==EXPR_LIST;llnd=NODE_OPERAND1(llnd)) + { + if (NODE_OPERAND0(llnd)&&(NODE_CODE(NODE_OPERAND0(llnd))==ACROSS_OP)) + return 1; + } +return 0; +} +int CheckReduction(PTR_BFND bif) +{ +PTR_LLND llnd; +if (!bif) return 0; +for (llnd=BIF_LL2(bif);llnd&&NODE_CODE(llnd)==EXPR_LIST;llnd=NODE_OPERAND1(llnd)) + { + if (NODE_OPERAND0(llnd)&&(NODE_CODE(NODE_OPERAND0(llnd))==REDUCTION_OP)) + return 1; + } +return 0; +} + +int IfReduction(PTR_LLND e1, PTR_LLND e2) +{ + if(!e1||!e2) return(0); + if(NODE_CODE(e1) != NODE_CODE(e2)) + return(0); + if(NODE_CODE(e1) != VAR_REF && NODE_CODE(e1) != ARRAY_REF) + return(0); + if(NODE_SYMB(e1) != NODE_SYMB(e2)) + return(0); + if(NODE_CODE(e1) == ARRAY_REF && !patternMatchExpression(NODE_OPERAND0(e1),NODE_OPERAND0(e2))) + return(0); + return (1); +} +int FindRedInExpr(PTR_LLND red, PTR_LLND expr) +{ +if(!expr) return 0; +if (!red) return 0; +if (NODE_CODE(red)!=VAR_REF && NODE_CODE(red)!=ARRAY_REF) return 0; + +if(NODE_CODE(red)==VAR_REF && NODE_CODE(red) == NODE_CODE(expr)) + { + if (NODE_SYMB(red)== NODE_SYMB(expr)) + return 1; + else return 0; + } + +if(NODE_CODE(red)==ARRAY_REF && NODE_CODE(red) == NODE_CODE(expr)) + { + if (NODE_SYMB(red) == NODE_SYMB(expr)) + return(patternMatchExpression(NODE_OPERAND0(red),NODE_OPERAND0(expr))); + } +return (FindRedInExpr(red,NODE_OPERAND0(expr))+FindRedInExpr(red,NODE_OPERAND1(expr))); +} + +PTR_LLND AddToReductionList(PTR_LLND redlist, PTR_LLND newred) +{ +PTR_LLND new_node,ptr; +new_node=make_llnode(EXPR_LIST,newred,LLNULL,SMNULL); +if (new_node) + { + if (redlist) + { + ptr=Follow_Llnd(redlist,2); + NODE_OPERAND1(ptr)=new_node; + } + if (!redlist) redlist=new_node; + } +else + { + return NULL; + } +return redlist; +} + +int FindInNewList(PTR_LLND newlist, PTR_LLND red) +{ +PTR_LLND ExprList; +if (!newlist) return 0; + +if (!red) return 0; +if (NODE_CODE(red)!=VAR_REF && NODE_CODE(red)!=ARRAY_REF) return 0; +for (ExprList=newlist;ExprList&&(NODE_CODE(ExprList)==EXPR_LIST);ExprList=NODE_OPERAND1(ExprList)) + { + if (NODE_CODE(NODE_OPERAND0(ExprList))==VAR_REF || NODE_CODE(NODE_OPERAND0(ExprList))==ARRAY_REF ) + if (NODE_SYMB(NODE_OPERAND0(ExprList))==NODE_SYMB(red)) + return 1; + } +return 0; +} + +int isForNodeEndStmt(PTR_BFND stmt) +{ + PTR_LABEL lab, do_lab; + PTR_BFND parent; + if(!(lab=BIF_LABEL(stmt)) && BIF_CODE(stmt) != CONTROL_END) /*the statement has no label and*/ + return(0); /*is not ENDDO */ + parent = BIF_CP(stmt); + if (parent) + { + if(BIF_CODE(parent)!=FOR_NODE) /*parent isn't DO statement*/ + return(0); + do_lab = BIF_LABEL_USE(parent); /* label of loop end or NULL*/ + if(do_lab) /* DO statement with label */ + if(lab && LABEL_STMTNO(lab) == LABEL_STMTNO(do_lab)) + /* the statement label is the label of loop end*/ + return(1); + else + return(0); + else /* DO statement without label */ + if(BIF_CODE(stmt) == CONTROL_END) + return(1); + else + return(0); + } + else return (0); +} +int ForNodeStmt(PTR_BFND stmt) +{ +PTR_BFND bif; +int count=0,label; +for(bif=stmt;bif;bif=BIF_NEXT(bif)) + { + /*BufPutString("Count=",0); + BufPutInt(count);*/ + if (BIF_CODE(bif)==ASSIGN_STAT) + { + if (FindRedInExpr(BIF_LL1(bif),BIF_LL2 (bif))) + { + if (BIF_LL1(bif)&&NODE_SYMB(BIF_LL1(bif))&&(!IS_DISTRIBUTE_ARRAY(NODE_SYMB(BIF_LL1(bif))))&&(!SYMB_DOVAR(NODE_SYMB(BIF_LL1(bif))))) + { + /*BufPutString("Reduction var :",0); + Tool_Unparse2_LLnode(BIF_LL1(bif)); + BufPutString("\n",0);*/ + if (!FindInNewList(NewSpecList,BIF_LL1(bif))) + if(!FindInNewList(ReductionList,BIF_LL1(bif))) + { + ReductionList=AddToReductionList(ReductionList,BIF_LL1(bif)); + /*BufPutString("Reduction LIST :",0); + Tool_Unparse2_LLnode(ReductionList); + BufPutString("\n",0);*/ + } + } + } + continue; + } + if (BIF_CODE(bif)==FOR_NODE) + { + count++; + continue; + } + else + { + if (isForNodeEndStmt(bif)) + { + if (BIF_CODE(bif)==CONTROL_END) + count--; + else + count-=NumberOfForNode(bif,&label); + } + } + if (!count) break; + } +return 0; +} + +PTR_LLND FreeReductionList(PTR_LLND redlist) +{ +PTR_LLND llnd,ptr; +llnd=redlist; +while((ptr=llnd)) + { + if (NODE_OPERAND1(llnd) == NULL) + break; + llnd = NODE_OPERAND1(llnd); + NODE_OPERAND0(ptr)=NULL; + NODE_OPERAND1(ptr)=NULL; +#ifdef __SPF + removeFromCollection(ptr); +#endif + free(ptr); + } +return (NULL); +} + +int ForNodeLabel(PTR_BFND stmt) +{ +PTR_BFND bif; +int count=0,label; +/*int LABEL=0;*/ +for(bif=stmt;bif;bif=BIF_NEXT(bif)) + { + if (BIF_CODE(bif)==FOR_NODE) + { + count++; + continue; + } + else + { + if (isForNodeEndStmt(bif)) + { + if (BIF_CODE(bif)==CONTROL_END) + count--; + else + count-=NumberOfForNode(bif,&label); + } + } + if (!count) break; + } +return 0; +} + + + + + + + + + diff --git a/sapfor/experts/Sapfor_2017/SageNewSrc/CMakeLists.txt b/sapfor/experts/Sapfor_2017/SageNewSrc/CMakeLists.txt index 18c136f..460202e 100644 --- a/sapfor/experts/Sapfor_2017/SageNewSrc/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/SageNewSrc/CMakeLists.txt @@ -48,6 +48,5 @@ if (MSVC_IDE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP") else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3") endif() \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/SageAnalysisTool/OmegaForSage/ip.cpp b/sapfor/experts/Sapfor_2017/_src/SageAnalysisTool/OmegaForSage/ip.cpp index 96b61cf..7378642 100644 --- a/sapfor/experts/Sapfor_2017/_src/SageAnalysisTool/OmegaForSage/ip.cpp +++ b/sapfor/experts/Sapfor_2017/_src/SageAnalysisTool/OmegaForSage/ip.cpp @@ -1,4112 +1,4112 @@ -/* $Id */ - -/* - * Source code for an implementation of the Omega test, a integer programming algorithm for dependence analysis, by - * William Pugh, appeared in Supercomputing '91 and CACM Aug 92 - * - * C Source written by: William Pugh Dept. of Computer Science Univ. of Maryland, College Park pugh@cs.umd.edu - * - * Options SPEED - don't bother checking debugging flags eliminateRedundantConstraints - use expensive methods to - * eliminate all redundant constraints singleResult - only produce a single simplified result verifySimplication - - * simplifyProblem checks for problem with no soloutions reduceWithSubstitutions - if not defined, convert - * substitutions back to EQs APROX - approximate inexact reductions - */ - - -#define maxWildCards 18 -#ifndef APROX -#define APROX 0 -#endif - -#ifdef _WIN32 -extern "C" void addToCollection(const int line, const char *file, void *pointer, int type); -extern "C" void removeFromCollection(void *pointer); -#endif - -#include "include/portable.h" -#include "include/flags.h" -#include -#include -#include -#include "include/ip.h" - - -#define EQ_type 1 -#define GEQ_type 2 -#define keyMult 31 -#define indexMult 17 -#define abs(x) ((x) >= 0 ? (x) : -(x)) -#define max(x,y) ((x) > (y) ? (x) : (y)) -#define min(x,y) ((x) < (y) ? (x) : (y)) -#define setMax(m,x) { if ((m) < (x)) m = (x); } -#define setMin(m,x) { if ((m) > (x)) m = (x); } -#ifndef TRUE -# define TRUE 1 -#endif -#ifndef FALSE -# define FALSE 0 -#endif -#define nVars (problemPtr->getVarsN()) -#define nEQ (problemPtr->getNumEqs()) -#define nGEQ (problemPtr->getNumGEqs()) -#define nSUB (problemPtr->getNumSUBs()) -#define SUBs (problemPtr->_SUBs) -#define GEQs (problemPtr->_GEQs) -#define EQs (problemPtr->_EQs) -#define safeVars (problemPtr->_safeVars) -#define printEQ(e) printEqn(problemPtr,e,0,0) -#define printGEQ(e) printEqn(problemPtr,e,1,0) -#define printGEQextra(e) printEqn(problemPtr,e,1,1) -#define variable(i) orgVariable(problemPtr->_var[i]) -#define orgVariable(i) (i == 0 ? "1" : (i < 0?wildName[-i]: (*problemPtr->_getVarName)(i,problemPtr->_getVarNameArgs))) -#define doTrace (trace && TRACE) -#define doDelete(e,nV) {if (DEBUG) {fprintf(outputFile,"Deleting %d (last:%d): ",e,nGEQ-1); printGEQ(&GEQs[e]);fprintf(outputFile,"\n");}; \ - if (e < nGEQ-1) eqnncpy (&GEQs[e], &GEQs[nGEQ - 1],(nV)); \ - (problemPtr->addNumGEqs(-1));} - -#define doDeleteExtra(e,nV) {if (DEBUG) {fprintf(outputFile,"Deleting %d: ",e); printGEQextra(&GEQs[e]);fprintf(outputFile,"\n");}; \ - if (e < nGEQ-1) eqnncpy (&GEQs[e], &GEQs[nGEQ - 1],(nV)); \ - (problemPtr->addNumGEqs(-1));} -#define isRed(e) (desiredResult == SIMPLIFY && (e)->color) - - -static int mayBeRed = 0; -#define hashTableSize 550 -#define maxKeys 500 - -#ifdef SPEED -#define TRACE 0 -#define DBUG 0 -#define DEBUG 0 -#else -#define TRACE (debugLevel) -#define DBUG (debugLevel > 1) -#define DEBUG (debugLevel > 2) -#endif -extern void addingEqualityConstraint(Problem * problemPtr, int e); - -#define mallocProblem ((Problem *) malloc(sizeof(Problem))) - -static struct _eqn hashMaster[hashTableSize]; - -int nonConvex = 0; - -int firstCheckForRedundantEquations = 0; - -static int doItAgain; - -static int conservative = 0; - -static FILE *outputFile; /* printProblem writes its output to this file */ - -static int nextKey; -static char wildName[200][20]; -static int nextWildCard = 0; -static int trace = 1; -static int depth = 0; -static int foundReduction; -static int packing[maxVars]; - -static int inApproximateMode = 0; - -#if reduceWithSubstitutions -int reduceWithSubs = 1; -#else -int reduceWithSubs = 0; -#endif - -static int pleaseNoEqualitiesInSimplifiedProblems = 0; - -int hashVersion = 0; - -#define noProblem ((Problem *) 0) - -Problem *originalProblem = noProblem; - -void noProcedure(Problem *problemPtr) { } -void(*whenReduced) (Problem *problemPtr) = noProcedure; - -static int gcd(int b, int a) -{ - if (b == 1) - return (1); - while (b != 0) { - register int t = b; - b = a % b; - a = t; - }; - return (a); -} - - -int checkIfSingleVar(Eqn e, int i) -{ - for (; i > 0; i--) - if (e->coef[i]) { - i--; - break; - }; - for (; i > 0; i--) - if (e->coef[i]) - break; - return (i == 0); -} - - -void initializeVariables(Problem *p) -{ - int i; - for (i = p->getVarsN(); i > 0; i--) - p->forwardingAddress[i] = p->_var[i] = i; - p->variablesInitialized = 1; -} - -void initializeProblem(Problem *p) -{ - p->hashVersion = hashVersion; - p->variablesInitialized = 0; - p->variablesFreed = 0; - p->_safeVars = 0; - p->_init(0, 0, 0, 0); -} - -void problemcpy(Problem *p1, Problem *p2) -{ - int e, i; - p1->hashVersion = p2->hashVersion; - p1->variablesInitialized = p2->variablesInitialized; - p1->variablesFreed = p2->variablesFreed; - p1->_safeVars = p2->_safeVars; - p1->_init(p2->getNumEqs(), p2->getNumGEqs(), p2->getNumSUBs(), p2->getVarsN()); - - for (e = p2->getNumEqs() - 1; e >= 0; e--) - eqnncpy(&(p1->_EQs[e]), &(p2->_EQs[e]), p2->getVarsN()); - - for (e = p2->getNumGEqs() - 1; e >= 0; e--) - eqnncpy(&(p1->_GEQs[e]), &(p2->_GEQs[e]), p2->getVarsN()); - - for (e = p2->getNumSUBs() - 1; e >= 0; e--) - eqnncpy(&(p1->_SUBs[e]), &(p2->_SUBs[e]), p2->getVarsN()); - - p1->_var.resize(p2->getVarsN() + 1); - - for (i = 0; i <= p2->getVarsN(); i++) - p1->_var[i] = p2->_var[i]; - - p1->forwardingAddress.resize(maxVars + 2); - for (i = 0; i <= maxVars; i++) - p1->forwardingAddress[i] = p2->forwardingAddress[i]; - p1->_getVarName = p2->_getVarName; - p1->_getVarNameArgs = p2->_getVarNameArgs; -} - - - -static void printTerm(Problem *problemPtr, Eqn e, int c) -{ - int i; - int first; - int n = nVars; - int wentFirst = -1; - first = 1; - for (i = 1; i <= n; i++) - if (c * e->coef[i] > 0) { - - first = 0; - wentFirst = i; - - if (c * e->coef[i] == 1) - fprintf(outputFile, "%s", variable(i)); - else - fprintf(outputFile, "%d%s", c * e->coef[i], variable(i)); - break; - }; - for (i = 1; i <= n; i++) - if (i != wentFirst && c * e->coef[i] != 0) { - if (!first && c * e->coef[i] > 0) - fprintf(outputFile, "+"); - - first = 0; - - if (c * e->coef[i] == 1) - fprintf(outputFile, "%s", variable(i)); - else if (c * e->coef[i] == -1) - fprintf(outputFile, "-%s", variable(i)); - else - fprintf(outputFile, "%d%s", c * e->coef[i], variable(i)); - }; - if (!first && c * e->coef[0] > 0) - fprintf(outputFile, "+"); - if (first || c * e->coef[0] != 0) - fprintf(outputFile, "%d", c * e->coef[0]); -} - - -void printEqn(Problem *problemPtr, Eqn e, int test, int extra) -{ - char buf[maxVars * 12 + 80]; - - sprintEqn(buf, problemPtr, e, test, extra); - fprintf(outputFile, "%s", buf); -} - -void sprintEqn(char *str, Problem *problemPtr, Eqn e, int test, int extra) -{ - int i; - int first; - int n = nVars + extra; - - if (test) { - if (DEBUG && e->touched) { - sprintf(str, "!"); - while (*str) - str++; - } - else if (DBUG && !e->touched && e->key != 0) { - sprintf(str, "%d: ", e->key); - while (*str) - str++; - } - } - if (e->color) { - sprintf(str, "["); - while (*str) - str++; - } - first = 1; - for (i = 0; i <= n; i++) - if (e->coef[i] < 0) { - if (!first) { - sprintf(str, "+"); - while (*str) - str++; - } - else - first = 0; - if (i == 0) { - sprintf(str, "%d", -e->coef[i]); - while (*str) - str++; - } - else if (e->coef[i] == -1) { - sprintf(str, "%s", variable(i)); - while (*str) - str++; - } - else { - sprintf(str, "%d%s", -e->coef[i], variable(i)); - while (*str) - str++; - } - }; - if (first) { - sprintf(str, "0"); - while (*str) - str++; - } - if (test == 0) { - sprintf(str, " = "); - while (*str) - str++; - } - else { - sprintf(str, " <= "); - while (*str) - str++; - } - - first = 1; - for (i = 0; i <= n; i++) - if (e->coef[i] > 0) { - if (!first) { - sprintf(str, "+"); - while (*str) - str++; - } - else - first = 0; - if (i == 0) { - sprintf(str, "%d", e->coef[i]); - while (*str) - str++; - } - else if (e->coef[i] == 1) { - sprintf(str, "%s", variable(i)); - while (*str) - str++; - } - else { - sprintf(str, "%d%s", e->coef[i], variable(i)); - while (*str) - str++; - } - } - if (first) { - sprintf(str, "0"); - while (*str) - str++; - } - if (e->color) { - sprintf(str, "]"); - while (*str) - str++; - } -} - -void printVars(Problem *problemPtr) -{ - int i; - fprintf(outputFile, "variables = "); - if (problemPtr->_safeVars > 0) - fprintf(outputFile, "("); - for (i = 1; i <= nVars; i++) { - fprintf(outputFile, "%s", variable(i)); - if (i == problemPtr->_safeVars) - fprintf(outputFile, ")"); - if (i < nVars) - fprintf(outputFile, ", "); - }; - fprintf(outputFile, "\n"); -} - -void printProblem(Problem *problemPtr) -{ - int e; - - if (!problemPtr->variablesInitialized) - initializeVariables(problemPtr); - printVars(problemPtr); - for (e = 0; e < nEQ; e++) { - printEQ(&EQs[e]); - fprintf(outputFile, "\n"); - }; - for (e = 0; e < nGEQ; e++) { - printGEQ(&GEQs[e]); - fprintf(outputFile, "\n"); - }; - for (e = 0; e < nSUB; e++) { - Eqn eq = &SUBs[e]; - if (DBUG && eq->color) - fprintf(outputFile, "["); - if (eq->key > 0) - fprintf(outputFile, "%s := ", orgVariable(eq->key)); - else - fprintf(outputFile, "#%d := ", eq->key); - printTerm(problemPtr, eq, 1); - if (DBUG && eq->color) - fprintf(outputFile, "]"); - fprintf(outputFile, "\n"); - }; - fflush(outputFile); -} - - -void printRedEquations(Problem *problemPtr) -{ - int e; - - if (!problemPtr->variablesInitialized) - initializeVariables(problemPtr); - printVars(problemPtr); - for (e = 0; e < nEQ; e++) { - if (EQs[e].color == red) { - printEQ(&EQs[e]); - fprintf(outputFile, "\n"); - } - }; - for (e = 0; e < nGEQ; e++) { - if (GEQs[e].color == red) { - printGEQ(&GEQs[e]); - fprintf(outputFile, "\n"); - } - }; - for (e = 0; e < nSUB; e++) { - if (SUBs[e].color == red) { - Eqn eq = &SUBs[e]; - if (DBUG && eq->color) - fprintf(outputFile, "["); - if (eq->key > 0) - fprintf(outputFile, "%s := ", orgVariable(eq->key)); - else - fprintf(outputFile, "#%d := ", eq->key); - printTerm(problemPtr, eq, 1); - if (DBUG && eq->color) - fprintf(outputFile, "]"); - fprintf(outputFile, "\n"); - } - }; - fflush(outputFile); -} - - -void prettyPrintProblem(Problem *problemPtr) -{ - int e; - int v; - int live[maxGEQs]; - int v1, v2, v3; - int t, change; - int stuffPrinted = 0; - - - typedef enum { - none, le, lt - } partialOrderType; - - partialOrderType po[maxVars][maxVars]; - int poE[maxVars][maxVars]; - int lastLinks[maxVars]; - int firstLinks[maxVars]; - int chainLength[maxVars]; - int chain[maxVars]; - int i, m, multiprint; - - - if (!problemPtr->variablesInitialized) - initializeVariables(problemPtr); - - if (nVars > 0) { - eliminateRedundant(problemPtr, 0); - - for (e = 0; e < nEQ; e++) { - if (stuffPrinted) - fprintf(outputFile, "; "); - stuffPrinted = 1; - printEQ(&EQs[e]); - }; - - for (e = 0; e < nGEQ; e++) - live[e] = TRUE; - - while (1) { - for (v = 1; v <= nVars; v++) { - lastLinks[v] = firstLinks[v] = 0; - chainLength[v] = 0; - for (v2 = 1; v2 <= nVars; v2++) - po[v][v2] = none; - }; - - for (e = 0; e < nGEQ; e++) - if (live[e]) { - for (v = 1; v <= nVars; v++) { - if (GEQs[e].coef[v] == 1) - firstLinks[v]++; - else if (GEQs[e].coef[v] == -1) - lastLinks[v]++; - }; - - v1 = nVars; - while (v1 > 0 && GEQs[e].coef[v1] == 0) - v1--; - v2 = v1 - 1; - while (v2 > 0 && GEQs[e].coef[v2] == 0) - v2--; - v3 = v2 - 1; - while (v3 > 0 && GEQs[e].coef[v3] == 0) - v3--; - - if (GEQs[e].coef[0] > 0 || GEQs[e].coef[0] < -1 - || v2 <= 0 || v3 > 0 - || GEQs[e].coef[v1] * GEQs[e].coef[v2] != -1) { - /* Not a partial order relation */ - - } - else { - if (GEQs[e].coef[v1] == 1) { - v3 = v2; - v2 = v1; - v1 = v3; - }; - /* relation is v1 <= v2 or v1 < v2 */ - po[v1][v2] = ((GEQs[e].coef[0] == 0) ? le : lt); - poE[v1][v2] = e; - }; - } - for (v = 1; v <= nVars; v++) - chainLength[v] = lastLinks[v]; - - /* Just in case nVars <= 0 */ - change = FALSE; - for (t = 0; t < nVars; t++) { - change = FALSE; - for (v1 = 1; v1 <= nVars; v1++) - for (v2 = 1; v2 <= nVars; v2++) - if (po[v1][v2] != none && - chainLength[v1] <= chainLength[v2]) { - chainLength[v1] = chainLength[v2] + 1; - change = TRUE; - } - }; - - if (change) { - /* caught in cycle */ - assert(0); - }; - - - v = 1; - for (v1 = 2; v1 <= nVars; v1++) - if (chainLength[v1] + firstLinks[v1] > chainLength[v] + firstLinks[v]) - v = v1; - - if (chainLength[v] == 0) - break; - - if (stuffPrinted) - fprintf(outputFile, "; "); - stuffPrinted = 1; - /* chain starts at v */ - /* print firstLinks */ - { - int tmp, first; - first = 1; - for (e = 0; e < nGEQ; e++) - if (live[e] && GEQs[e].coef[v] == 1) { - if (!first) - fprintf(outputFile, ", "); - tmp = GEQs[e].coef[v]; - GEQs[e].coef[v] = 0; - printTerm(problemPtr, &GEQs[e], -1); - GEQs[e].coef[v] = tmp; - live[e] = FALSE; - first = 0; - }; - if (!first) - fprintf(outputFile, " <= "); - }; - - - /* find chain */ - chain[0] = v; - m = 1; - while (1) { - /* print chain */ - for (v2 = 1; v2 <= nVars; v2++) - if (po[v][v2] && chainLength[v] == 1 + chainLength[v2]) - break; - if (v2 > nVars) - break; - chain[m++] = v2; - v = v2; - } - - fprintf(outputFile, "%s", variable(chain[0])); - - multiprint = 0; - for (i = 1; i < m; i++) { - v = chain[i - 1]; - v2 = chain[i]; - if (po[v][v2] == le) - fprintf(outputFile, " <= "); - else - fprintf(outputFile, " < "); - fprintf(outputFile, "%s", variable(v2)); - live[poE[v][v2]] = FALSE; - if (!multiprint && i < m - 1) - for (v3 = 1; v3 <= nVars; v3++) { - if (v == v3 || v2 == v3) - continue; - if (po[v][v2] != po[v][v3]) - continue; - if (po[v2][chain[i + 1]] != po[v3][chain[i + 1]]) - continue; - fprintf(outputFile, ",%s", variable(v3)); - live[poE[v][v3]] = FALSE; - live[poE[v3][chain[i + 1]]] = FALSE; - multiprint = 1; - } - else - multiprint = 0; - }; - - v = chain[m - 1]; - /* print lastLinks */ - { - int tmp, first; - first = 1; - for (e = 0; e < nGEQ; e++) - if (live[e] && GEQs[e].coef[v] == -1) { - if (!first) - fprintf(outputFile, ", "); - else - fprintf(outputFile, " <= "); - tmp = GEQs[e].coef[v]; - GEQs[e].coef[v] = 0; - printTerm(problemPtr, &GEQs[e], 1); - GEQs[e].coef[v] = tmp; - live[e] = FALSE; - first = 0; - }; - }; - }; - - - for (e = 0; e < nGEQ; e++) - if (live[e]) { - if (stuffPrinted) - fprintf(outputFile, "; "); - stuffPrinted = 1; - printGEQ(&GEQs[e]); - }; - - for (e = 0; e < nSUB; e++) { - Eqn eq = &SUBs[e]; - if (stuffPrinted) - fprintf(outputFile, "; "); - stuffPrinted = 1; - if (eq->key > 0) - fprintf(outputFile, "%s := ", orgVariable(eq->key)); - else - fprintf(outputFile, "#%d := ", eq->key); - printTerm(problemPtr, eq, 1); - }; - }; - fflush(outputFile); -} - - -static void nameWildcard(Problem *problemPtr, int i) -{ - --nextWildCard; - if (nextWildCard < -maxWildCards) - nextWildCard = -1; - problemPtr->_var[i] = nextWildCard; -} - -static int addNewWildcard(Problem *problemPtr) -{ - int e; - int i = ++safeVars; - problemPtr->addToVarsN(1); - //nVars++; - if (nVars != i) { - for (e = nGEQ - 1; e >= 0; e--) { - if (GEQs[e].coef[i] != 0) - GEQs[e].touched = TRUE; - GEQs[e].coef[nVars] = GEQs[e].coef[i]; - - }; - for (e = nEQ - 1; e >= 0; e--) { - EQs[e].coef[nVars] = EQs[e].coef[i]; - }; - for (e = nSUB - 1; e >= 0; e--) { - SUBs[e].coef[nVars] = SUBs[e].coef[i]; - }; - problemPtr->_var[nVars] = problemPtr->_var[i]; - }; - for (e = nGEQ - 1; e >= 0; e--) - GEQs[e].coef[i] = 0; - for (e = nEQ - 1; e >= 0; e--) - EQs[e].coef[i] = 0; - for (e = nSUB - 1; e >= 0; e--) - SUBs[e].coef[i] = 0; - nameWildcard(problemPtr, i); - return (i); -} - -extern void substitute(Problem * problemPtr, Eqn sub, int i, int c); -extern void deleteVariable(Problem *problemPt, int j); - -static void doMod(Problem *problemPtr, int factor, int e, int j) -/* Solve e = factor alpha for x_j and substitute */ -{ - int k, i; - struct _eqn eq; - int nFactor; - int killJ = FALSE; - eqncpy(&eq, &EQs[e]); - for (k = nVars; k >= 0; k--) { - eq.coef[k] = intMod(eq.coef[k], factor); - if (2 * eq.coef[k] >= factor) - eq.coef[k] -= factor; - }; - nFactor = eq.coef[j]; - if (j <= safeVars && problemPtr->_var[j] > 0) { - i = addNewWildcard(problemPtr); - eq.coef[nVars] = eq.coef[i]; - eq.coef[j] = 0; - eq.coef[i] = -factor; - killJ = TRUE; - } - else { - eq.coef[j] = -factor; - if (problemPtr->_var[j] > 0) - nameWildcard(problemPtr, j); - }; - substitute(problemPtr, &eq, j, nFactor); - for (k = nVars; k >= 0; k--) - EQs[e].coef[k] = EQs[e].coef[k] / factor; - if (killJ) - deleteVariable(problemPtr, j); - if (DEBUG) { - fprintf(outputFile, "Mod-ing and normalizing produces:\n"); - printProblem(problemPtr); - }; -} - -void -negateGEQ(Problem * problemPtr, int e) -{ - - int i; - for (i = nVars; i >= 0; i--) - GEQs[e].coef[i] = -GEQs[e].coef[i]; - GEQs[e].coef[0]--; - GEQs[e].touched = TRUE; -} - -static int verifyProblem(Problem *problemPtr) -{ - int result, e, anyColor; - Problem tmpProblem; - /*tmpProblem = mallocProblem; - if (tmpProblem == NULL) - { - printf("ERROR!!\n"); - } - tmpProblem->_init();*/ - - problemcpy(&tmpProblem, problemPtr); - tmpProblem._safeVars = 0; - tmpProblem.setNumSUBs(0); - anyColor = 0; - for (e = nGEQ - 1; e >= 0; e--) - anyColor |= GEQs[e].color; - anyColor |= pleaseNoEqualitiesInSimplifiedProblems; - if (anyColor) { - originalProblem = noProblem; - } - else - originalProblem = problemPtr; - if (DBUG) { - fprintf(outputFile, "verifying problem"); - if (anyColor) - fprintf(outputFile, " (color mode)"); - fprintf(outputFile, " :\n"); - printProblem(problemPtr); - }; - - result = solve(&tmpProblem, UNKNOWN); - originalProblem = noProblem; - //free(tmpProblem); - if (DBUG) { - if (result) - fprintf(outputFile, "verified problem\n"); - else - fprintf(outputFile, "disproved problem\n"); - printProblem(problemPtr); - }; - return result; -} - - -#define implies(A,B) (A==(A&B)) - -void resurrectSubs(Problem * problemPtr); - -int eliminateRedundant(Problem *problemPtr, bool expensive) -{ - int e, e1, e2, e3, p, q, i, k, alpha, alpha1, alpha2, alpha3; - int c; - int isDead[maxGEQs]; - unsigned int P[maxGEQs], Z[maxGEQs], N[maxGEQs]; - unsigned int PP, PZ, PN; /* possible Positives, possible zeros & possible negatives */ - - for (e = nGEQ - 1; e >= 0; e--) { - int tmp = 1; - isDead[e] = 0; - P[e] = Z[e] = N[e] = 0; - for (i = nVars; i >= 1; i--) { - if (GEQs[e].coef[i] > 0) - P[e] |= tmp; - else if (GEQs[e].coef[i] < 0) - N[e] |= tmp; - else - Z[e] |= tmp; - tmp <<= 1; - } - } - - - for (e1 = nGEQ - 1; e1 >= 0; e1--) - if (!isDead[e1]) - for (e2 = e1 - 1; e2 >= 0; e2--) - if (!isDead[e2]) { - for (p = nVars; p > 1; p--) { - for (q = p - 1; q > 0; q--) { - alpha = (GEQs[e1].coef[p] * GEQs[e2].coef[q] - GEQs[e2].coef[p] * GEQs[e1].coef[q]); - if (alpha != 0) - goto foundPQ; - }; - }; - continue; - - foundPQ: - PZ = (Z[e1] & Z[e2]) | (P[e1] & N[e2]) | (N[e1] & P[e2]); - PP = P[e1] | P[e2]; - PN = N[e1] | N[e2]; - - for (e3 = nGEQ - 1; e3 >= 0; e3--) - if (e3 != e1 && e3 != e2) { - - if (!implies(Z[e3], PZ)) - goto nextE3; - - alpha1 = GEQs[e2].coef[q] * GEQs[e3].coef[p] - GEQs[e2].coef[p] * GEQs[e3].coef[q]; - alpha2 = -(GEQs[e1].coef[q] * GEQs[e3].coef[p] - GEQs[e1].coef[p] * GEQs[e3].coef[q]); - alpha3 = alpha; - - if (alpha1 * alpha2 <= 0) - goto nextE3; - if (alpha1 < 0) { - alpha1 = -alpha1; - alpha2 = -alpha2; - alpha3 = -alpha3; - } - if (alpha3 > 0) { - /* Trying to prove e3 is redundant */ - if (!implies(P[e3], PP) | !implies(N[e3], PN)) - goto nextE3; - if (!GEQs[e3].color && (GEQs[e1].color || GEQs[e2].color)) - goto nextE3; - - /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */ - for (k = nVars; k >= 1; k--) - if (alpha3 * GEQs[e3].coef[k] - != alpha1 * GEQs[e1].coef[k] + alpha2 * GEQs[e2].coef[k]) - goto nextE3; - - c = alpha1 * GEQs[e1].coef[0] + alpha2 * GEQs[e2].coef[0]; - if (c < alpha3 * (GEQs[e3].coef[0] + 1)) { - if (DBUG) { - printProblem(problemPtr); - fprintf(outputFile, "found redundant inequality\n"); - fprintf(outputFile, "alpha1, alpha2, alpha3 = %d,%d,%d\n", - alpha1, alpha2, alpha3); - - printGEQ(&(GEQs[e1])); - fprintf(outputFile, "\n"); - printGEQ(&(GEQs[e2])); - fprintf(outputFile, "\n=> "); - printGEQ(&(GEQs[e3])); - fprintf(outputFile, "\n\n"); - }; - - isDead[e3] = 1; - } - } - else { - /* - * trying to prove e3 <= 0 and therefore e3 = 0, or trying to prove e3 < 0, and - * therefore the problem has no solutions - * - */ - if (!implies(P[e3], PN) | !implies(N[e3], PP)) - goto nextE3; - if (GEQs[e1].color || GEQs[e2].color || GEQs[e3].color) - goto nextE3; - - alpha3 = alpha3; - /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */ - for (k = nVars; k >= 1; k--) - if (alpha3 * GEQs[e3].coef[k] - != alpha1 * GEQs[e1].coef[k] + alpha2 * GEQs[e2].coef[k]) - goto nextE3; - - c = alpha1 * GEQs[e1].coef[0] + alpha2 * GEQs[e2].coef[0]; - if (c < alpha3 * (GEQs[e3].coef[0])) { - - /* We just proved e3 < 0, so no solutions exist */ - if (DBUG) { - printProblem(problemPtr); - fprintf(outputFile, "found implied over tight inequality\n"); - fprintf(outputFile, "alpha1, alpha2, alpha3 = %d,%d,%d\n", - alpha1, alpha2, -alpha3); - printGEQ(&(GEQs[e1])); - fprintf(outputFile, "\n"); - printGEQ(&(GEQs[e2])); - fprintf(outputFile, "\n=> not "); - printGEQ(&(GEQs[e3])); - fprintf(outputFile, "\n\n"); - }; - return (0); - - } - else if (c < alpha3 * (GEQs[e3].coef[0] + 1)) { - - /* We just proved that e3 <=0, so e3 = 0 */ - if (DBUG) { - printProblem(problemPtr); - fprintf(outputFile, "found implied tight inequality\n"); - fprintf(outputFile, "alpha1, alpha2, alpha3 = %d,%d,%d\n", - alpha1, alpha2, -alpha3); - printGEQ(&(GEQs[e1])); - fprintf(outputFile, "\n"); - printGEQ(&(GEQs[e2])); - fprintf(outputFile, "\n=> inverse "); - printGEQ(&(GEQs[e3])); - fprintf(outputFile, "\n\n"); - }; - //eqncpy(&EQs[nEQ++], &GEQs[e3]); - const int numEQ = nEQ; - problemPtr->addNumEqs(1); - eqncpy(&EQs[numEQ], &GEQs[e3]); - - - assert(nEQ <= maxEQs); - addingEqualityConstraint(problemPtr, nEQ - 1); - isDead[e3] = 1; - - } - } - nextE3:; - } - } - for (e = nGEQ - 1; e >= 0; e--) - if (isDead[e]) - doDelete(e, nVars); - - /* if (nEQ) return (solve(problemPtr, SIMPLIFY)); */ - - if (!expensive) - return (1); - { - Problem tmpProblem; - int oldTrace = trace; - trace = 0; - /*tmpProblem = mallocProblem; - if (tmpProblem == NULL) - { - printf("ERROR!!\n"); - } - tmpProblem->_init();*/ - - conservative++; - for (e = nGEQ - 1; e >= 0; e--) { - if (DEBUG) { - fprintf(outputFile, "checking equation %d to see if it is redundant: ", e); - printGEQ(&(GEQs[e])); - fprintf(outputFile, "\n"); - }; - problemcpy(&tmpProblem, problemPtr); - negateGEQ(&tmpProblem, e); - tmpProblem._safeVars = 0; - tmpProblem.variablesFreed = 0; - if (!solve(&tmpProblem, FALSE)) - doDelete(e, nVars); - }; - trace = oldTrace; - //free(tmpProblem); - conservative--; - }; -#if reduceWithSubstitutions -#else - resurrectSubs(problemPtr); - assert(nSUB == 0); -#endif - return (1); -} - - -int smoothWeirdEquations(Problem *problemPtr) -{ - int e1, e2, e3, p, q, k, alpha, alpha1, alpha2, alpha3; - int c; - int v; - int result = 0; - - for (e1 = nGEQ - 1; e1 >= 0; e1--) - if (!GEQs[e1].color) { - int g = 999999; - for (v = nVars; v >= 1; v--) - if (GEQs[e1].coef[v] != 0 && abs(GEQs[e1].coef[v]) < g) - g = abs(GEQs[e1].coef[v]); - if (g > 20) { - - e3 = nGEQ; - for (v = nVars; v >= 1; v--) - GEQs[e3].coef[v] = intDiv(6 * GEQs[e1].coef[v] + g / 2, g); - GEQs[e3].color = 0; - GEQs[e3].touched = 1; - GEQs[e3].coef[0] = 9997; - if (DBUG) { - fprintf(outputFile, "Checking to see if we can derive: "); - printGEQ(&GEQs[e3]); - fprintf(outputFile, "\n from: "); - printGEQ(&GEQs[e1]); - fprintf(outputFile, "\n"); - }; - - - for (e2 = nGEQ - 1; e2 >= 0; e2--) - if (e1 != e2 && !GEQs[e2].color) { - for (p = nVars; p > 1; p--) { - for (q = p - 1; q > 0; q--) { - alpha = (GEQs[e1].coef[p] * GEQs[e2].coef[q] - GEQs[e2].coef[p] * GEQs[e1].coef[q]); - if (alpha != 0) - goto foundPQ; - }; - }; - continue; - - foundPQ: - - alpha1 = GEQs[e2].coef[q] * GEQs[e3].coef[p] - GEQs[e2].coef[p] * GEQs[e3].coef[q]; - alpha2 = -(GEQs[e1].coef[q] * GEQs[e3].coef[p] - GEQs[e1].coef[p] * GEQs[e3].coef[q]); - alpha3 = alpha; - - if (alpha1 * alpha2 <= 0) - continue; - if (alpha1 < 0) { - alpha1 = -alpha1; - alpha2 = -alpha2; - alpha3 = -alpha3; - } - if (alpha3 > 0) { - /* Trying to prove e3 is redundant */ - - /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */ - for (k = nVars; k >= 1; k--) - if (alpha3 * GEQs[e3].coef[k] - != alpha1 * GEQs[e1].coef[k] + alpha2 * GEQs[e2].coef[k]) - goto nextE2; - - c = alpha1 * GEQs[e1].coef[0] + alpha2 * GEQs[e2].coef[0]; - if (c < alpha3 * (GEQs[e3].coef[0] + 1)) - GEQs[e3].coef[0] = intDiv(c, alpha3); - - } - nextE2:; - } - if (GEQs[e3].coef[0] < 9997) { - result++; - problemPtr->addNumGEqs(1); - //nGEQ++; - if (DBUG) { - fprintf(outputFile, "Smoothing wierd equations; adding:\n"); - printGEQ(&GEQs[e3]); - fprintf(outputFile, "\nto:\n"); - printProblem(problemPtr); - fprintf(outputFile, "\n\n"); - }; - }; - } - } - return (result); -} - -void -coalesce(Problem * problemPtr) -{ - int e, e2, colors; - int isDead[maxGEQs]; - - colors = 0; - for (e = 0; e < nGEQ; e++) - if (GEQs[e].color) - colors++; - if (colors < 2) - return; - for (e = 0; e < nGEQ; e++) - isDead[e] = 0; - for (e = 0; e < nGEQ; e++) - if (GEQs[e].color) - for (e2 = e + 1; e2 < nGEQ; e2++) - if (GEQs[e].key == -GEQs[e2].key - && GEQs[e].coef[0] == -GEQs[e2].coef[0]) - { - const int numEQ = nEQ; - problemPtr->addNumEqs(1); - eqncpy(&EQs[numEQ], &GEQs[e]); - - assert(nEQ <= maxEQs); - isDead[e] = 1; - isDead[e2] = 1; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (isDead[e]) { - doDelete(e, nVars); - } -} - -void eliminateRed(Problem *problemPtr, bool eliminateAll) -{ - int e, e2, e3, i, j, k, a, alpha1, alpha2; - int c = 0; - int isDead[maxGEQs]; - int deadCount = 0; - - if (DEBUG) { - fprintf(outputFile, "in eliminate RED:\n"); - printProblem(problemPtr); - }; - if (nEQ > 0) { - simplifyProblem(problemPtr); - }; - - for (e = nGEQ - 1; e >= 0; e--) - isDead[e] = 0; - for (e = nGEQ - 1; e >= 0; e--) - if (!GEQs[e].color && !isDead[e]) - for (e2 = e - 1; e2 >= 0; e2--) - if (!GEQs[e2].color && !isDead[e2]) { - a = 0; - for (i = nVars; i > 1; i--) { - for (j = i - 1; j > 0; j--) { - a = (GEQs[e].coef[i] * GEQs[e2].coef[j] - GEQs[e2].coef[i] * GEQs[e].coef[j]); - if (a != 0) - goto foundPair; - }; - }; - continue; - - foundPair: - if (DEBUG) { - fprintf(outputFile, "found two equations to combine, i = %s, ", variable(i)); - fprintf(outputFile, "j = %s, alpha = %d\n", variable(j), a); - printGEQ(&(GEQs[e])); - fprintf(outputFile, "\n"); - printGEQ(&(GEQs[e2])); - fprintf(outputFile, "\n"); - }; - for (e3 = nGEQ - 1; e3 >= 0; e3--) - if (GEQs[e3].color) { - alpha1 = GEQs[e2].coef[j] * GEQs[e3].coef[i] - GEQs[e2].coef[i] * GEQs[e3].coef[j]; - alpha2 = -(GEQs[e].coef[j] * GEQs[e3].coef[i] - GEQs[e].coef[i] * GEQs[e3].coef[j]); - if ((a > 0 && alpha1 > 0 && alpha2 > 0) || (a < 0 && alpha1 < 0 && alpha2 < 0)) { - if (DEBUG) { - fprintf(outputFile, "alpha1 = %d, alpha2 = %d; comparing against: ", alpha1, alpha2); - printGEQ(&(GEQs[e3])); - fprintf(outputFile, "\n"); - }; - for (k = nVars; k >= 0; k--) { - c = alpha1 * GEQs[e].coef[k] + alpha2 * GEQs[e2].coef[k]; - if (c != a * GEQs[e3].coef[k]) - break; - if (DEBUG && k > 0) - fprintf(outputFile, " %s: %d, %d\n", variable(k), c, a * GEQs[e3].coef[k]); - }; - if (k < 0 - || (k == 0 && - ((a > 0 && c < a * GEQs[e3].coef[k]) || (a < 0 && c > a * GEQs[e3].coef[k])))) { - if (DEBUG) { - deadCount++; - fprintf(outputFile, "red equation#%d is dead (%d dead so far, %d remain)\n", - e3, deadCount, nGEQ - deadCount); - printGEQ(&(GEQs[e])); - fprintf(outputFile, "\n"); - printGEQ(&(GEQs[e2])); - fprintf(outputFile, "\n"); - printGEQ(&(GEQs[e3])); - fprintf(outputFile, "\n"); - }; - isDead[e3] = 1; - }; - }; - }; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (isDead[e]) - doDelete(e, nVars); - - - if (DBUG) { - fprintf(outputFile, "in eliminate RED, easy tests done:\n"); - printProblem(problemPtr); - }; - - { - int redFound = 0; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].color) - redFound = 1; - if (!redFound) { - if (DBUG) - fprintf(outputFile, "fast checks worked\n"); -#if reduceWithSubstitutions -#else - assert(pleaseNoEqualitiesInSimplifiedProblems || nSUB == 0); -#endif - return; - }; - } - -#ifndef verifySimplication - if (!verifyProblem(problemPtr)) - return; -#endif - - { - Problem tmpProblem; - int oldTrace = trace; - trace = 0; - conservative++; - /*tmpProblem = mallocProblem; - if (tmpProblem == NULL) - { - printf("ERROR!!\n"); - } - tmpProblem->_init();*/ - - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].color) { - if (DBUG) { - fprintf(outputFile, "checking equation %d to see if it is redundant: ", e); - printGEQ(&(GEQs[e])); - fprintf(outputFile, "\n"); - }; - problemcpy(&tmpProblem, problemPtr); - negateGEQ(&tmpProblem, e); - tmpProblem._safeVars = 0; - tmpProblem.variablesFreed = 0; - tmpProblem.setNumSUBs(0); - if (!solve(&tmpProblem, FALSE)) { - if (DBUG) - fprintf(outputFile, "it is redundant\n"); - doDelete(e, nVars); - } - else { - if (DBUG) - fprintf(outputFile, "it is not redundant\n"); - if (!eliminateAll) { - if (DBUG) - fprintf(outputFile, "no need to check other red equations\n"); - break; - } - }; - }; - trace = oldTrace; - conservative--; - - //free(tmpProblem); - }; - /* simplifyProblem(problemPtr); */ -#if reduceWithSubstitutions -#else - assert(pleaseNoEqualitiesInSimplifiedProblems || nSUB == 0); -#endif -} - - - -void swap(int *i, int *j) -{ - int tmp; - tmp = *i; - *i = *j; - *j = tmp; -} - -void chainUnprotect(Problem *problemPtr) -{ - int i, e; - int unprotect[maxVars]; - for (i = 1; i <= problemPtr->_safeVars; i++) { - unprotect[i] = (problemPtr->_var[i] < 0); - for (e = nSUB - 1; e >= 0; e--) - if (SUBs[e].coef[i]) - unprotect[i] = 0; - }; - if (DBUG) { - fprintf(outputFile, "Doing chain reaction unprotection\n"); - printProblem(problemPtr); - for (i = 1; i <= problemPtr->_safeVars; i++) - if (unprotect[i]) - fprintf(outputFile, "unprotecting %s\n", variable(i)); - }; - for (i = 1; i <= problemPtr->_safeVars; i++) - if (unprotect[i]) { - /* wild card */ - if (i < problemPtr->_safeVars) { - int j = problemPtr->_safeVars; - swap(&problemPtr->_var[i], &problemPtr->_var[j]); - for (e = nGEQ - 1; e >= 0; e--) { - GEQs[e].touched = 1; - swap(&GEQs[e].coef[i], &GEQs[e].coef[j]); - }; - for (e = nEQ - 1; e >= 0; e--) - swap(&EQs[e].coef[i], &EQs[e].coef[j]); - for (e = nSUB - 1; e >= 0; e--) - swap(&SUBs[e].coef[i], &SUBs[e].coef[j]); - swap(&unprotect[i], &unprotect[j]); - i--; - }; - problemPtr->_safeVars--; - }; - if (DBUG) { - fprintf(outputFile, "After chain reactions\n"); - printProblem(problemPtr); - }; -} - -void -resurrectSubs(Problem * problemPtr) -{ - if (problemPtr->getNumSUBs() > 0 && !pleaseNoEqualitiesInSimplifiedProblems) { - int i, e, n, m; - if (DBUG) { - fprintf(outputFile, - "problem reduced, bringing variables back to life\n"); - printProblem(problemPtr); - }; - for (i = 1; i <= problemPtr->_safeVars; i++) - if (problemPtr->_var[i] < 0) { - /* wild card */ - if (i < problemPtr->_safeVars) { - int j = problemPtr->_safeVars; - swap(&problemPtr->_var[i], &problemPtr->_var[j]); - for (e = nGEQ - 1; e >= 0; e--) { - GEQs[e].touched = 1; - swap(&GEQs[e].coef[i], &GEQs[e].coef[j]); - }; - for (e = nEQ - 1; e >= 0; e--) - swap(&EQs[e].coef[i], &EQs[e].coef[j]); - for (e = nSUB - 1; e >= 0; e--) - swap(&SUBs[e].coef[i], &SUBs[e].coef[j]); - i--; - }; - problemPtr->_safeVars--; - }; - - m = problemPtr->getNumSUBs(); - n = max(nVars, problemPtr->_safeVars + m); - for (e = nGEQ - 1; e >= 0; e--) - { - if (singleVarGEQ(GEQs[e], nVars)) - { - i = abs(GEQs[e].key); - if (i >= problemPtr->_safeVars + 1) - GEQs[e].key += (GEQs[e].key > 0 ? m : -m); - } - else - { - GEQs[e].touched = TRUE; - GEQs[e].key = 0; - } - } - - const int tmpNV = nVars; - problemPtr->addToVarsN(m); - - for (i = tmpNV; i >= problemPtr->_safeVars + 1; i--) - { - problemPtr->_var[i + m] = problemPtr->_var[i]; - for (e = nGEQ - 1; e >= 0; e--) - GEQs[e].coef[i + m] = GEQs[e].coef[i]; - for (e = nEQ - 1; e >= 0; e--) - EQs[e].coef[i + m] = EQs[e].coef[i]; - for (e = nSUB - 1; e >= 0; e--) - SUBs[e].coef[i + m] = SUBs[e].coef[i]; - } - - for (i = problemPtr->_safeVars + m; i >= problemPtr->_safeVars + 1; i--) - { - for (e = nGEQ - 1; e >= 0; e--) - GEQs[e].coef[i] = 0; - for (e = nEQ - 1; e >= 0; e--) - EQs[e].coef[i] = 0; - for (e = nSUB - 1; e >= 0; e--) - SUBs[e].coef[i] = 0; - } - //nVars += m; - - for (e = nSUB - 1; e >= 0; e--) - { - problemPtr->_var[problemPtr->_safeVars + 1 + e] = SUBs[e].key; - - const int numEQ = nEQ; - problemPtr->addNumEqs(1); - - eqncpy(&(EQs[numEQ]), &(SUBs[e])); - EQs[numEQ].coef[problemPtr->_safeVars + 1 + e] = -1; - if (DBUG) - { - fprintf(outputFile, "brought back: "); - printEQ(&EQs[numEQ]); - fprintf(outputFile, "\n"); - } - //nEQ++; - assert(nEQ <= maxEQs); - } - problemPtr->_safeVars += m; - //nSUB = 0; - - problemPtr->setNumSUBs(0); - for (i = problemPtr->_safeVars + 1; i <= nVars; i++) { - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].color && GEQs[e].coef[i]) { - /* variable i is in red equations */ - int e2; - for (e2 = nEQ - 1; e2 >= 0; e2--) - if (EQs[e2].coef[i]) - EQs[e2].color = 1; - break; - } - } - - if (DBUG) { - fprintf(outputFile, "variables brought back to life\n"); - printProblem(problemPtr); - }; - } -} - - -static void problemReduced(Problem *problemPtr) -{ -#ifdef verifySimplification - int result; - result = verifyProblem(problemPtr); - if (result && nEQ > 0) - doItAgain = 1; - else if (result) -#endif - { -#ifdef eliminateRedundantConstraints - if (!eliminateRedundant(problemPtr, 1)) - return; -#endif - foundReduction = TRUE; - if (!pleaseNoEqualitiesInSimplifiedProblems) - coalesce(problemPtr); - if (reduceWithSubs || pleaseNoEqualitiesInSimplifiedProblems) - chainUnprotect(problemPtr); - else - resurrectSubs(problemPtr); - -#ifndef singleResult - (*whenReduced) (problemPtr); -#endif - if (omegaPrintResult == 1) { - fprintf(outputFile, "-------------------------------------------\n"); - fprintf(outputFile, "problem reduced:\n"); - printProblem(problemPtr); - fprintf(outputFile, "-------------------------------------------\n"); - }; - - - }; -} - - -static void freeEliminations(Problem *problemPtr, int fv) -/* do free eliminations */ -{ - int tryAgain = 1; - int i, e, e2; - int nV = nVars; - while (tryAgain) { - tryAgain = 0; - for (i = nV; i > fv; i--) { - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i]) - break; - if (e < 0) - e2 = e; - else if (GEQs[e].coef[i] > 0) { - for (e2 = e - 1; e2 >= 0; e2--) - if (GEQs[e2].coef[i] < 0) - break; - } - else { - for (e2 = e - 1; e2 >= 0; e2--) - if (GEQs[e2].coef[i] > 0) - break; - }; - if (e2 < 0) { - int e3; - for (e3 = nSUB - 1; e3 >= 0; e3--) - if (SUBs[e3].coef[i]) - break; - if (e3 >= 0) - continue; - for (e3 = nEQ - 1; e3 >= 0; e3--) - if (EQs[e3].coef[i]) - break; - if (e3 >= 0) - continue; - if (DBUG) - fprintf(outputFile, "a free elimination of %s\n", variable(i)); - if (e >= 0) { - doDelete(e, nV); - for (e--; e >= 0; e--) - if (GEQs[e].coef[i]) - doDelete(e, nV); - tryAgain = (i < nV); - }; - deleteVariable(problemPtr, i); - nV = nVars; - }; - }; - }; - - if (DEBUG) { - fprintf(outputFile, "\nafter free eliminations:\n"); - printProblem(problemPtr); - fprintf(outputFile, "\n"); - }; -} - -static void freeRedEliminations(Problem * problemPtr) -/* do free red eliminations */ -{ - int tryAgain = 1; - int i, e, e2; - int nV = nVars; - int isRedVar[maxVars]; - int isDeadVar[maxVars]; - int isDeadGEQ[maxGEQs]; - for (i = nV; i > 0; i--) { - isRedVar[i] = 0; - isDeadVar[i] = 0; - }; - for (e = nGEQ - 1; e >= 0; e--) { - isDeadGEQ[e] = 0; - if (GEQs[e].color) - for (i = nV; i > 0; i--) - if (GEQs[e].coef[i] != 0) - isRedVar[i] = 1; - }; - - while (tryAgain) { - tryAgain = 0; - for (i = nV; i > 0; i--) - if (!isRedVar[i] && !isDeadVar[i]) { - for (e = nGEQ - 1; e >= 0; e--) - if (!isDeadGEQ[e] && GEQs[e].coef[i]) - break; - if (e < 0) - e2 = e; - else if (GEQs[e].coef[i] > 0) { - for (e2 = e - 1; e2 >= 0; e2--) - if (!isDeadGEQ[e2] && GEQs[e2].coef[i] < 0) - break; - } - else { - for (e2 = e - 1; e2 >= 0; e2--) - if (!isDeadGEQ[e2] && GEQs[e2].coef[i] > 0) - break; - }; - if (e2 < 0) { - int e3; - for (e3 = nSUB - 1; e3 >= 0; e3--) - if (SUBs[e3].coef[i]) - break; - if (e3 >= 0) - continue; - for (e3 = nEQ - 1; e3 >= 0; e3--) - if (EQs[e3].coef[i]) - break; - if (e3 >= 0) - continue; - if (DBUG) - fprintf(outputFile, "a free red elimination of %s\n", variable(i)); - for (; e >= 0; e--) - if (GEQs[e].coef[i]) - isDeadGEQ[e] = 1; - tryAgain = 1; - isDeadVar[i] = 1; - }; - }; - }; - - for (e = nGEQ - 1; e >= 0; e--) - if (isDeadGEQ[e]) - doDelete(e, nV); - - for (i = nV; i > 0; i--) - if (isDeadVar[i]) - deleteVariable(problemPtr, i); - - - if (DEBUG) { - fprintf(outputFile, "\nafter free red eliminations:\n"); - printProblem(problemPtr); - fprintf(outputFile, "\n"); - }; -} - -void addingEqualityConstraint(Problem *problemPtr, int e) -{ - int e2, i, j; - - if (originalProblem != noProblem && originalProblem != problemPtr && !conservative) - { - //e2 = originalProblem->_numEQs++; - e2 = originalProblem->getNumEqs(); - originalProblem->addNumEqs(1); - - if (DBUG) - fprintf(outputFile, - "adding equality constraint %d to outer problem\n", - e2); - eqnnzero(&originalProblem->_EQs[e2], originalProblem->getVarsN()); - for (i = nVars; i >= 1; i--) { - for (j = originalProblem->getVarsN(); j >= 1; j--) - if (originalProblem->_var[j] == problemPtr->_var[i]) - break; - if (j <= 0) { - if (DBUG) - fprintf(outputFile, "retracting\n"); - //originalProblem->_numEQs--; - originalProblem->addNumEqs(-1); - return; - }; - originalProblem->_EQs[e2].coef[j] = EQs[e].coef[i]; - }; - originalProblem->_EQs[e2].coef[0] = EQs[e].coef[0]; - if (DBUG) - printProblem(originalProblem); - }; -} - - - - -void substitute(Problem * problemPtr, Eqn sub, int i, int c) -{ - int e, j, j0, k; - int topVar; - - { - int *p = &packing[0]; - for (k = nVars; k >= 0; k--) - if (sub->coef[k]) - *(p++) = k; - topVar = (p - &packing[0]) - 1; - }; - - - if (DBUG || doTrace) { - fprintf(outputFile, "substituting using %s := ", variable(i)); - printTerm(problemPtr, sub, -c); - fprintf(outputFile, "\n"); - printVars(problemPtr); - }; - - if (topVar < 0) { - for (e = nEQ - 1; e >= 0; e--) - EQs[e].coef[i] = 0; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i] != 0) { - GEQs[e].touched = TRUE; - GEQs[e].coef[i] = 0; - }; - for (e = nSUB - 1; e >= 0; e--) - SUBs[e].coef[i] = 0; - - if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { - //register Eqn eqn = &(SUBs[nSUB++]); - problemPtr->addNumSUBs(1); - register Eqn eqn = &(SUBs[nSUB - 1]); - - for (k = nVars; k >= 0; k--) - eqn->coef[k] = 0; - eqn->key = problemPtr->_var[i]; - eqn->color = 0; - }; - } - else if (topVar == 0 && packing[0] == 0) { - c = -sub->coef[0] * c; - for (e = nEQ - 1; e >= 0; e--) { - EQs[e].coef[0] += EQs[e].coef[i] * c; - EQs[e].coef[i] = 0; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i] != 0) { - GEQs[e].coef[0] += GEQs[e].coef[i] * c; - GEQs[e].coef[i] = 0; - GEQs[e].touched = TRUE; - }; - for (e = nSUB - 1; e >= 0; e--) { - SUBs[e].coef[0] += SUBs[e].coef[i] * c; - SUBs[e].coef[i] = 0; - }; - if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { - //register Eqn eqn = &(SUBs[nSUB++]); - problemPtr->addNumSUBs(1); - register Eqn eqn = &(SUBs[nSUB - 1]); - - for (k = nVars; k >= 1; k--) - eqn->coef[k] = 0; - eqn->coef[0] = c; - eqn->key = problemPtr->_var[i]; - eqn->color = 0; - }; - } - else { - for (e = nEQ - 1; e >= 0; e--) { - register Eqn eqn = &(EQs[e]); - k = eqn->coef[i]; - if (k != 0) { - k = c * k; - eqn->coef[i] = 0; - for (j = topVar; j >= 0; j--) { - j0 = packing[j]; - eqn->coef[j0] -= sub->coef[j0] * k; - }; - }; - if (DEBUG) { - printEQ(eqn); - fprintf(outputFile, "\n"); - }; - }; - for (e = nGEQ - 1; e >= 0; e--) { - register Eqn eqn = &(GEQs[e]); - k = eqn->coef[i]; - if (k != 0) { - k = c * k; - eqn->touched = TRUE; - eqn->coef[i] = 0; - for (j = topVar; j >= 0; j--) { - j0 = packing[j]; - eqn->coef[j0] -= sub->coef[j0] * k; - }; - }; - if (DEBUG) { - printGEQ(eqn); - fprintf(outputFile, "\n"); - }; - }; - for (e = nSUB - 1; e >= 0; e--) { - register Eqn eqn = &(SUBs[e]); - k = eqn->coef[i]; - if (k != 0) { - k = c * k; - eqn->coef[i] = 0; - for (j = topVar; j >= 0; j--) { - j0 = packing[j]; - eqn->coef[j0] -= sub->coef[j0] * k; - }; - }; - if (DEBUG) { - fprintf(outputFile, "%s := ", orgVariable(eqn->key)); - printTerm(problemPtr, eqn, 1); - fprintf(outputFile, "\n"); - }; - }; - - if (DEBUG) - fprintf(outputFile, "---\n\n"); - if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { - register Eqn eqn; - //eqn = &(SUBs[nSUB++]); - problemPtr->addNumSUBs(1); - eqn = &(SUBs[nSUB - 1]); - - c = -c; - for (k = nVars; k >= 0; k--) - eqn->coef[k] = c * (sub->coef[k]); - eqn->key = problemPtr->_var[i]; - eqn->color = sub->color; - }; - }; - -} - -void substituteRed(Problem *problemPtr, Eqn sub, int i, int c, bool * foundBlack) -{ - int e, j, j0, k; - int topVar; - - { - int *p = &packing[0]; - for (k = nVars; k >= 0; k--) - if (sub->coef[k]) - *(p++) = k; - topVar = (p - &packing[0]) - 1; - }; - - *foundBlack = 0; - - if (DBUG || doTrace) { - if (sub->color) - fprintf(outputFile, "["); - fprintf(outputFile, "substituting using %s := ", variable(i)); - printTerm(problemPtr, sub, -c); - if (sub->color) - fprintf(outputFile, "]"); - fprintf(outputFile, "\n"); - printVars(problemPtr); - }; - - for (e = nEQ - 1; e >= 0; e--) { - register Eqn eqn = &(EQs[e]); - k = eqn->coef[i]; - if (k != 0) { - if (!eqn->color) - *foundBlack = 1; - else { - k = c * k; - eqn->coef[i] = 0; - for (j = topVar; j >= 0; j--) { - j0 = packing[j]; - eqn->coef[j0] -= sub->coef[j0] * k; - }; - }; - }; - if (DEBUG) { - printEQ(eqn); - fprintf(outputFile, "\n"); - }; - }; - for (e = nGEQ - 1; e >= 0; e--) { - register Eqn eqn = &(GEQs[e]); - k = eqn->coef[i]; - if (k != 0) { - if (!eqn->color) - *foundBlack = 1; - else { - k = c * k; - eqn->touched = TRUE; - eqn->coef[i] = 0; - for (j = topVar; j >= 0; j--) { - j0 = packing[j]; - eqn->coef[j0] -= sub->coef[j0] * k; - }; - }; - }; - if (DEBUG) { - printGEQ(eqn); - fprintf(outputFile, "\n"); - }; - }; - for (e = nSUB - 1; e >= 0; e--) { - register Eqn eqn = &(SUBs[e]); - k = eqn->coef[i]; - if (k != 0) { - if (!eqn->color) - *foundBlack = 1; - else { - k = c * k; - eqn->coef[i] = 0; - for (j = topVar; j >= 0; j--) { - j0 = packing[j]; - eqn->coef[j0] -= sub->coef[j0] * k; - }; - }; - }; - if (DEBUG) { - fprintf(outputFile, "%s := ", orgVariable(eqn->key)); - printTerm(problemPtr, eqn, 1); - fprintf(outputFile, "\n"); - }; - }; - - if (DEBUG) - fprintf(outputFile, "---\n\n"); - if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { - *foundBlack = 1; - }; - -} - - - -void deleteVariable(Problem *problemPtr, int i) -{ - int nV = nVars; - int e; - if (i < safeVars) { - int j = safeVars; - for (e = nGEQ - 1; e >= 0; e--) { - GEQs[e].touched = TRUE; - GEQs[e].coef[i] = GEQs[e].coef[j]; - GEQs[e].coef[j] = GEQs[e].coef[nV]; - }; - for (e = nEQ - 1; e >= 0; e--) { - EQs[e].coef[i] = EQs[e].coef[j]; - EQs[e].coef[j] = EQs[e].coef[nV]; - }; - for (e = nSUB - 1; e >= 0; e--) { - SUBs[e].coef[i] = SUBs[e].coef[j]; - SUBs[e].coef[j] = SUBs[e].coef[nV]; - }; - problemPtr->_var[i] = problemPtr->_var[j]; - problemPtr->_var[j] = problemPtr->_var[nV]; - } - else if (i < nV) { - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[nV]) { - GEQs[e].coef[i] = GEQs[e].coef[nV]; - GEQs[e].touched = TRUE; - }; - for (e = nEQ - 1; e >= 0; e--) - EQs[e].coef[i] = EQs[e].coef[nV]; - for (e = nSUB - 1; e >= 0; e--) - SUBs[e].coef[i] = SUBs[e].coef[nV]; - problemPtr->_var[i] = problemPtr->_var[nV]; - }; - if (i <= safeVars) - safeVars--; - problemPtr->addToVarsN(-1); - //nVars--; -} - -static void convertEQtoGEQs(Problem * problemPtr, int eq) -{ - int i; - if (DBUG) - fprintf(outputFile, "Converting Eq to GEQs\n"); - - int numGE = nGEQ; - problemPtr->addNumGEqs(1); - - eqncpy(&GEQs[numGE], &EQs[eq]); - GEQs[numGE].touched = 1; - //nGEQ++; - - numGE = nGEQ; - problemPtr->addNumGEqs(1); - - eqncpy(&GEQs[numGE], &EQs[eq]); - GEQs[numGE].touched = 1; - for (i = 0; i <= nVars; i++) - GEQs[numGE].coef[i] = -GEQs[numGE].coef[i]; - //nGEQ++; - - if (DBUG) - printProblem(problemPtr); -} - - -static void doElimination(Problem *problemPtr, int e, int i) -{ - struct _eqn sub; - int c; - int nV = nVars; - if (DBUG || doTrace) - fprintf(outputFile, "eliminating variable %s\n", variable(i)); - eqncpy(&sub, &EQs[e]); - c = sub.coef[i]; - sub.coef[i] = 0; - if (c == 1 || c == -1) { - if (EQs[e].color) { - bool fB; - substituteRed(problemPtr, &sub, i, c, &fB); - if (fB) - convertEQtoGEQs(problemPtr, e); - else - deleteVariable(problemPtr, i); - } - else { - substitute(problemPtr, &sub, i, c); - deleteVariable(problemPtr, i); - } - } - else { - int a = abs(c); - if (TRACE) - fprintf(outputFile, "performing non-exact elimination, c = %d\n", c); - assert(inApproximateMode); - for (e = nEQ - 1; e >= 0; e--) - if (EQs[e].coef[i]) { - register Eqn eqn = &(EQs[e]); - int j, k; - for (j = nV; j >= 0; j--) - eqn->coef[j] *= a; - k = eqn->coef[i]; - eqn->coef[i] = 0; - for (j = nV; j >= 0; j--) - eqn->coef[j] -= sub.coef[j] * k / c; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i]) { - register Eqn eqn = &(GEQs[e]); - int j, k; - for (j = nV; j >= 0; j--) - eqn->coef[j] *= a; - k = eqn->coef[i]; - eqn->coef[i] = 0; - for (j = nV; j >= 0; j--) - eqn->coef[j] -= sub.coef[j] * k / c; - eqn->touched = 1; - }; - for (e = nSUB - 1; e >= 0; e--) - if (SUBs[e].coef[i]) { - register Eqn eqn = &(SUBs[e]); - int j, k; - assert(0); - for (j = nV; j >= 0; j--) - eqn->coef[j] *= a; - k = eqn->coef[i]; - eqn->coef[i] = 0; - for (j = nV; j >= 0; j--) - eqn->coef[j] -= sub.coef[j] * k / c; - }; - deleteVariable(problemPtr, i); - }; -} - -static int solveEQ(Problem *problemPtr, int desiredResult) -{ - int i, j, e; - int g, g2; - g = 0; - - - if (DBUG && nEQ > 0) { - fprintf(outputFile, "\nSolveEQ\n"); - printProblem(problemPtr); - fprintf(outputFile, "\n"); - }; - - if (mayBeRed) { - i = 0; - j = nEQ - 1; - while (1) { - struct _eqn eq; - while (i <= j && EQs[i].color) - i++; - while (i <= j && !EQs[j].color) - j--; - if (i >= j) - break; - eqncpy(&eq, &EQs[i]); - eqncpy(&EQs[i], &EQs[j]); - eqncpy(&EQs[j], &eq); - i++; - j--; - }; - }; - - - - /* Eliminate all EQ equations */ - for (e = nEQ - 1; e >= 0; e--) { - register Eqn eqn = &(EQs[e]); - int sv; - - if (inApproximateMode && !nGEQ && safeVars == nVars) - return (TRUE); - - if (DEBUG) - fprintf(outputFile, "----\n"); - for (i = nVars; i > 0; i--) - if ((g = eqn->coef[i]) != 0) - break; - /* - * if (isRed(eqn)) { if (DBUG) fprintf(outputFile, "Can't handle Red equality\n"); - * convertEQtoGEQs(problemPtr,eqn); if (DBUG) printProblem(problemPtr); continue; }; - */ - for (j = i - 1; j > 0; j--) - if (eqn->coef[j]) - break; - if (j == 0) { - if (eqn->coef[0] % g != 0) { - if (DBUG) - printEQ(eqn); - if (DBUG) - fprintf(outputFile, "\nequations have no solution \n"); - return (FALSE); - }; - eqn->coef[0] = eqn->coef[0] / g; - eqn->coef[i] = 1; - //nEQ--; - problemPtr->addNumEqs(-1); - doElimination(problemPtr, e, i); - continue; - } - else if (j == -1) { - if (eqn->coef[0] != 0) { - if (DBUG) - printEQ(eqn); - if (DBUG) - fprintf(outputFile, "\nequations have no solution \n"); - return (FALSE); - }; - //nEQ--; - problemPtr->addNumEqs(-1); - continue; - }; - /* i == position of last non-zero coef */ - /* g == coef of i */ - /* j == position of next non-zero coef */ - - if (g < 0) - g = -g; - if (g == 1) { - //nEQ--; - problemPtr->addNumEqs(-1); - doElimination(problemPtr, e, i); - } - else { - int k = j; - bool promotionPossible = - (j <= safeVars && safeVars + 1 == i - && !isRed(eqn) - && !inApproximateMode); - if (DEBUG && promotionPossible) - fprintf(outputFile, " Promotion possible\n"); - normalizeEQ: - if (j > safeVars) { - for (; g != 1 && j > safeVars; j--) - g = gcd(abs(eqn->coef[j]), g); - g2 = g; - } - else if (i > safeVars) - g2 = g; - else - g2 = 0; - for (; g != 1 && j > 0; j--) - g = gcd(abs(eqn->coef[j]), g); - if (g > 1) { - if (eqn->coef[0] % g != 0) { - if (DBUG) - printEQ(eqn); - if (DBUG) - fprintf(outputFile, "\nequations have no solution \n"); - return (FALSE); - }; - - for (j = 0; j <= nVars; j++) - eqn->coef[j] /= g; - g2 = g2 / g; - }; - if (g2 > 1) { - int e2; - for (e2 = e - 1; e2 >= 0; e2--) - if (EQs[e2].coef[i]) - break; - if (e2 == -1) - for (e2 = nGEQ - 1; e2 >= 0; e2--) - if (GEQs[e2].coef[i]) - break; - if (e2 == -1) - for (e2 = nSUB - 1; e2 >= 0; e2--) - if (SUBs[e2].coef[i]) - break; - if (e2 == -1) { - bool change = 0; - if (DBUG) - fprintf(outputFile, "Ha! We own it! \n"); - if (DEBUG) - printEQ(eqn); - if (DEBUG) - fprintf(outputFile, " \n"); - g = eqn->coef[i]; - g = abs(g); - for (j = i - 1; j >= 0; j--) { - int t = eqn->coef[j]; - t = intMod(t, g); - if (2 * t >= g) - t -= g; - if (t != eqn->coef[j]) { - eqn->coef[j] = t; - change = 1; - }; - }; - if (!change) { - if (DBUG) - fprintf(outputFile, "So what?\n"); - } - else { - nameWildcard(problemPtr, i); - if (DEBUG) - printEQ(eqn); - if (DEBUG) - fprintf(outputFile, " \n"); - e++; - continue; - }; - }; - } - if (promotionPossible) { - if (DEBUG) { - fprintf(outputFile, "promoting %s to safety\n", variable(i)); - printVars(problemPtr); - }; - safeVars++; - if (problemPtr->_var[i] > 0) - nameWildcard(problemPtr, i); - promotionPossible = 0; - j = k; - goto normalizeEQ; - }; - - if (g2 > 1 && !inApproximateMode) - { - if (DEBUG) - fprintf(outputFile, "adding equation to handle safe variable \n"); - if (DEBUG) - printEQ(eqn); - if (DEBUG) - fprintf(outputFile, "\n----\n"); - i = addNewWildcard(problemPtr); - //nEQ++; - problemPtr->addNumEqs(1); - - assert(nEQ <= maxEQs); - eqnzero(&EQs[e + 1]); - eqnncpy(&EQs[e + 1], eqn, safeVars); - for (j = nVars; j >= 0; j--) { - EQs[e + 1].coef[j] = intMod(EQs[e + 1].coef[j], g2); - if (2 * EQs[e + 1].coef[j] >= g2) - EQs[e + 1].coef[j] -= g2; - }; - EQs[e + 1].coef[i] = g2; - e += 2; - if (DEBUG) - printProblem(problemPtr); - continue; - }; - - sv = safeVars; - if (g2 == 0) - sv = 0; - - /* find variable to eliminate */ - if (g2 > 1) { - assert(inApproximateMode); - if (TRACE) { - fprintf(outputFile, "non-exact elimination: "); - printEQ(eqn); - fprintf(outputFile, "\n"); - printProblem(problemPtr); - fflush(outputFile); - }; - for (i = nVars; i > sv; i--) - if (EQs[e].coef[i] != 0) - break; - } - else - for (i = nVars; i > sv; i--) - if (EQs[e].coef[i] == 1 || EQs[e].coef[i] == -1) - break; - - if (i > sv) { - //nEQ--; - problemPtr->addNumEqs(-1); - doElimination(problemPtr, e, i); - if (g2 > 1 && TRACE) { - fprintf(outputFile, "result of non-exact elimination:\n"); - printProblem(problemPtr); - fflush(outputFile); - }; - } - else { - int factor = 100000; - j = 0; - if (DEBUG) - fprintf(outputFile, "doing moding\n"); - for (i = nVars; i != sv; i--) - if ((EQs[e].coef[i] & 1) != 0) { - j = i; - i--; - for (; i != sv; i--) - if ((EQs[e].coef[i] & 1) != 0) - break; - break; - }; - if (j != 0 && i == sv) { - doMod(problemPtr, 2, e, j); - e++; - continue; - }; - - j = 0; - for (i = nVars; i != sv; i--) - if (EQs[e].coef[i] != 0 && factor > abs(EQs[e].coef[i]) + 1) { - factor = abs(EQs[e].coef[i]) + 1; - j = i; - }; - if (j == sv) { - fprintf(outputFile, "should not have happened\n"); - Exit(2); - }; - doMod(problemPtr, factor, e, j); - /* go back and try this equation again */ - e++; - }; - }; - - - }; - //nEQ = 0; - problemPtr->setNumEqs(0); - return (UNKNOWN); -} - - -static int solveGEQ(Problem *problemPtr, int desiredResult); -static int solveDepth = 0; - -int solve(Problem *problemPtr, int desiredResult) -{ - int result; - - assert(nVars >= safeVars); - if (desiredResult != SIMPLIFY) - safeVars = 0; - - solveDepth++; - if (solveDepth > 50) { - fprintf(outputFile, "Solve depth = %d, inApprox = %d, aborting\n", - solveDepth, inApproximateMode); - printProblem(problemPtr); - fflush(outputFile); -#ifndef SPEED - debugLevel = 3; -#else - Exit(2); -#endif - if (solveDepth > 60) - Exit(2); - }; - - do { - doItAgain = 0; - if (solveEQ(problemPtr, desiredResult) == FALSE) { - solveDepth--; - return (FALSE); - }; - if (inApproximateMode && !nGEQ) { - result = TRUE; - foundReduction = 1; - break; - } - else - result = solveGEQ(problemPtr, desiredResult); - } while (doItAgain && desiredResult == SIMPLIFY); - solveDepth--; - -#if reduceWithSubstitutions -#else - resurrectSubs(problemPtr); - assert(pleaseNoEqualitiesInSimplifiedProblems || !result || nSUB == 0); -#endif - - return (result); -} - -static int fastLookup[maxKeys * 2]; - -static int solveGEQ(Problem *problemPtr, int desiredResult) -{ - int i, j, k, e; - int nV, fv; - int result; - int coupledSubscripts; - int eliminateAgain; - int smoothed = 0; - j = 0; -solveGEQstart: - while (1) { - if (nGEQ > maxGEQs) { - fprintf(outputFile, "TOO MANY EQUATIONS GENERATED\n"); - Exit(2); - }; - if (DBUG) - fprintf(outputFile, "\nSolveGEQ(%d):\n", desiredResult); - if (DEBUG) - printProblem(problemPtr); - if (DBUG) - fprintf(outputFile, "\n"); - - nV = nVars; - - if (nV == 1) { - int uColor = black; - int lColor = black; - int upperBound = posInfinity; - int lowerBound = negInfinity; - for (e = nGEQ - 1; e >= 0; e--) { - int a = GEQs[e].coef[1]; - int c = GEQs[e].coef[0]; - /* our equation is ax + c >= 0, or ax >= -c, or c >= -ax */ - if (a == 0) { - if (c < 0) { - if (DBUG) - fprintf(outputFile, "equations have no solution \n"); - return (FALSE); - }; - } - else if (a > 0) { - if (a != 1) - c = intDiv(c, a); - if (lowerBound < -c || - (lowerBound == -c && !isRed(&GEQs[e]))) { - lowerBound = -c; - lColor = GEQs[e].color; - } - } - else { - if (a != -1) - c = intDiv(c, -a); - if (upperBound > c || - (upperBound == c && !isRed(&GEQs[e]))) { - upperBound = c; - uColor = GEQs[e].color; - } - }; - }; - if (DEBUG) - fprintf(outputFile, "upper bound = %d\n", upperBound); - if (DEBUG) - fprintf(outputFile, "lower bound = %d\n", lowerBound); - if (lowerBound > upperBound) { - if (DBUG) - fprintf(outputFile, "equations have no solution \n"); - return (FALSE); - }; - if (desiredResult == SIMPLIFY) { - //nGEQ = 0; - problemPtr->setNumGEqs(0); - if (safeVars == 1) { - - if (lowerBound == upperBound && !uColor && !lColor) { - EQs[0].coef[0] = -lowerBound; - EQs[0].coef[1] = 1; - EQs[0].color = 0; - //nEQ = 1; - problemPtr->setNumEqs(1); - return (solve(problemPtr, desiredResult)); - } - else { - if (lowerBound > negInfinity) { - GEQs[0].coef[0] = -lowerBound; - GEQs[0].coef[1] = 1; - GEQs[0].key = 1; - GEQs[0].color = lColor; - GEQs[0].touched = 0; - //nGEQ = 1; - problemPtr->setNumGEqs(1); - } - - if (upperBound < posInfinity) - { - const int numGE = nGEQ; - problemPtr->addNumGEqs(1); - - GEQs[numGE].coef[0] = upperBound; - GEQs[numGE].coef[1] = -1; - GEQs[numGE].key = -1; - GEQs[numGE].color = uColor; - GEQs[numGE].touched = 0; - //nGEQ++; - } - } - } - else - problemPtr->setVarsN(0); - problemReduced(problemPtr); - return (FALSE); - }; - if (originalProblem != noProblem && !lColor && !uColor && !conservative && lowerBound == upperBound) { - EQs[0].coef[0] = -lowerBound; - EQs[0].coef[1] = 1; - //nEQ = 1; - problemPtr->setNumEqs(1); - addingEqualityConstraint(problemPtr, 0); - }; - return (TRUE); - }; - - if (!problemPtr->variablesFreed) { - problemPtr->variablesFreed = 1; - if (desiredResult != SIMPLIFY) - freeEliminations(problemPtr, 0); - else - freeEliminations(problemPtr, safeVars); - nV = nVars; - if (nV == 1) - continue; - }; - - - coupledSubscripts = 0; - for (e = 0; e < nGEQ; e++) { - if (!GEQs[e].touched) { - if (!singleVarGEQ(GEQs[e], nV)) - coupledSubscripts = 1; - } - else { - register int g; - int topVar; - int i0; - int hashCode; - - - { - int *p = &packing[0]; - for (k = 1; k <= nV; k++) - if (GEQs[e].coef[k]) { - *(p++) = k; - }; - topVar = (p - &packing[0]) - 1; - }; - - if (topVar == -1) { - if (GEQs[e].coef[0] < 0) { - if (DBUG) - printGEQ(&GEQs[e]); - if (DBUG) - fprintf(outputFile, "\nequations have no solution \n"); - return (FALSE); - }; - doDelete(e, nV); - e--; - continue; - } - else if (topVar == 0) { - int singleVar = packing[0]; - g = GEQs[e].coef[singleVar]; - if (g > 0) { - GEQs[e].coef[singleVar] = 1; - GEQs[e].key = singleVar; - } - else { - g = -g; - GEQs[e].coef[singleVar] = -1; - GEQs[e].key = -singleVar; - }; - if (g > 1) - GEQs[e].coef[0] = intDiv(GEQs[e].coef[0], g); - } - else { - - coupledSubscripts = 1; - i0 = topVar; - i = packing[i0--]; - g = GEQs[e].coef[i]; - hashCode = g * (i + 3); - if (g < 0) - g = -g; - for (; i0 >= 0; i0--) { - register int x; - i = packing[i0]; - x = GEQs[e].coef[i]; - hashCode = hashCode * keyMult * (i + 3) + x; - if (x < 0) - x = -x; - if (x == 1) { - g = 1; - i0--; - break; - } - else - g = gcd(x, g); - }; - for (; i0 >= 0; i0--) { - register int x; - i = packing[i0]; - x = GEQs[e].coef[i]; - hashCode = hashCode * keyMult * (i + 3) + x; - }; - if (g > 1) { - GEQs[e].coef[0] = intDiv(GEQs[e].coef[0], g); - i0 = topVar; - i = packing[i0--]; - GEQs[e].coef[i] = GEQs[e].coef[i] / g; - hashCode = GEQs[e].coef[i] * (i + 3); - for (; i0 >= 0; i0--) { - i = packing[i0]; - GEQs[e].coef[i] = GEQs[e].coef[i] / g; - hashCode = hashCode * keyMult * (i + 3) + GEQs[e].coef[i]; - }; - }; - - { - register int g2 = abs(hashCode); - if (DEBUG) { - fprintf(outputFile, "Hash code = %d, eqn = ", hashCode); - printGEQ(&GEQs[e]); - fprintf(outputFile, "\n"); - }; - j = g2 % hashTableSize; - while (1) { - Eqn proto = &(hashMaster[j]); - if (proto->touched == g2) { - if (proto->coef[0] == topVar) { - if (hashCode >= 0) - for (i0 = topVar; i0 >= 0; i0--) { - i = packing[i0]; - if (GEQs[e].coef[i] != proto->coef[i]) - break; - } - else - for (i0 = topVar; i0 >= 0; i0--) { - i = packing[i0]; - if (GEQs[e].coef[i] != -proto->coef[i]) - break; - }; - if (i0 < 0) { - if (hashCode >= 0) - GEQs[e].key = proto->key; - else - GEQs[e].key = -proto->key; - break; - }; - }; - } - else if (proto->touched < 0) { - eqnzero(proto); - if (hashCode >= 0) - for (i0 = topVar; i0 >= 0; i0--) { - i = packing[i0]; - proto->coef[i] = GEQs[e].coef[i]; - } - else - for (i0 = topVar; i0 >= 0; i0--) { - i = packing[i0]; - proto->coef[i] = -GEQs[e].coef[i]; - } - proto->coef[0] = topVar; - proto->touched = g2; - if (DEBUG) - fprintf(outputFile, " constraint key = %d\n", nextKey); - proto->key = nextKey++; - if (proto->key > maxKeys) { - fprintf(outputFile, "too many hash keys generated \n"); - fflush(outputFile); - Exit(2); - }; - if (hashCode >= 0) - GEQs[e].key = proto->key; - else - GEQs[e].key = -proto->key; - break; - }; - j = (j + 1) % hashTableSize; - }; - }; - }; - - GEQs[e].touched = FALSE; - }; - - { - int eKey = GEQs[e].key; - int e2; - if (e > 0) { - int cTerm = GEQs[e].coef[0]; - e2 = fastLookup[maxKeys - eKey]; - if (e2 < e && GEQs[e2].key == -eKey) { - if (GEQs[e2].coef[0] < -cTerm) { - if (DBUG) { - printGEQ(&GEQs[e]); - fprintf(outputFile, "\n"); - printGEQ(&GEQs[e2]); - fprintf(outputFile, "\nequations have no solution \n"); - }; - return (FALSE); - }; - if (GEQs[e2].coef[0] == -cTerm && !GEQs[e2].color && !GEQs[e].color) - { - /* - * if (!addIt && 0) { int i; for (i = safeVars + 1; i <= nVars; i++) if (GEQs[e].coef[i] == - * 1 || GEQs[e].coef[i] == -1) addIt = 1; }; - */ - const int numEQ = nEQ; - problemPtr->addNumEqs(1); - - eqncpy(&EQs[numEQ], &GEQs[e]); - if (!GEQs[e2].color) - addingEqualityConstraint(problemPtr, numEQ); - assert(!GEQs[e2].color); - assert(!GEQs[e].color); - //nEQ++; - - assert(nEQ <= maxEQs); - } - } - - e2 = fastLookup[maxKeys + eKey]; - if (e2 < e && GEQs[e2].key == eKey) { - if (GEQs[e2].coef[0] > cTerm) { - GEQs[e2].coef[0] = cTerm; - GEQs[e2].color = GEQs[e].color; - } - else if (GEQs[e2].coef[0] == cTerm) - GEQs[e2].color &= GEQs[e].color; - doDelete(e, nV); - e--; - continue; - }; - }; - fastLookup[maxKeys + eKey] = e; - }; - }; - - nV = nVars; - - if ((doTrace && desiredResult == SIMPLIFY) || DBUG) { - fprintf(outputFile, "\nafter normalization:\n"); - printProblem(problemPtr); - fprintf(outputFile, "\n"); - fprintf(outputFile, "eliminating variable using fourier-motzkin elimination\n"); - }; - - do { - eliminateAgain = 0; - - if (nEQ > 0) - return (solve(problemPtr, desiredResult)); - - if (!coupledSubscripts) { - if (safeVars == 0) - { - //nGEQ = 0; - problemPtr->setNumGEqs(0); - } - else - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].key > safeVars || -safeVars > GEQs[e].key) - doDelete(e, nV); - problemPtr->setVarsN(safeVars); - //nVars = safeVars; - if (desiredResult == SIMPLIFY) { - problemReduced(problemPtr); - return (FALSE); - }; - return (TRUE); - }; - - if (desiredResult != SIMPLIFY) - fv = 0; - else - fv = safeVars; - - if (nGEQ == 0) { - if (desiredResult == SIMPLIFY) { - problemPtr->setVarsN(safeVars); - //nVars = safeVars; - problemReduced(problemPtr); - return (FALSE); - }; - return (TRUE); - }; - if (desiredResult == SIMPLIFY && nV == safeVars) { - problemReduced(problemPtr); - return (FALSE); - }; - - - if (nGEQ > maxGEQs - 30 || nGEQ > 2 * nV * nV + 4 * nV + 10) { - if (DBUG) - fprintf(outputFile, "TOO MANY EQUATIONS; %d equations, %d variables, ELIMINATING REDUNDANT ONES\n", nGEQ, nV); - if (!eliminateRedundant(problemPtr, 0)) - return 0; - nV = nVars; - if (nEQ > 0) - return (solve(problemPtr, desiredResult)); - if (DBUG) - fprintf(outputFile, "END ELIMINATION OF REDUNDANT EQUATIONS\n"); - }; - - - - { - int minC, maxC, minCj; - int lowerBoundCount; - int e2, Le, Ue; - int possibleEasyIntSolution; - int exact = 0; - int luckyExact = 0; - int newEqns = 0; - minCj = 0; - Le = 0; - lowerBoundCount = 0; - - if (desiredResult != SIMPLIFY) - fv = 0; - else - fv = safeVars; - - { - int best = 1000000; - int jLe, jLowerBoundCount, upperBoundCount; - - jLe = 0; - jLowerBoundCount = 0; - - - for (i = nV; i != fv; i--) { - int score; - int ub = -2; - int lb = -2; - int lucky = 0; - minC = maxC = 0; - lowerBoundCount = 0; - upperBoundCount = 0; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i] < 0) { - setMin(minC, GEQs[e].coef[i]); - upperBoundCount++; - if (GEQs[e].coef[i] < -1) { - if (ub == -2) - ub = e; - else - ub = -1; - }; - } - else if (GEQs[e].coef[i] > 0) { - setMax(maxC, GEQs[e].coef[i]); - lowerBoundCount++; - Le = e; - if (GEQs[e].coef[i] > 1) { - if (lb == -2) - lb = e; - else - lb = -1; - }; - }; - if (lowerBoundCount == 0 || upperBoundCount == 0) { - lowerBoundCount = 0; - break; - }; - if (ub >= 0 && lb >= 0 && GEQs[lb].key == -GEQs[ub].key) { - int Lc = GEQs[lb].coef[i]; - int Uc = -GEQs[ub].coef[i]; - int diff = Lc * GEQs[ub].coef[0] + Uc * GEQs[lb].coef[0]; - lucky = (diff >= (Uc - 1) * (Lc - 1)); - }; - if (maxC == 1 || minC == -1 || lucky || APROX || inApproximateMode) { - newEqns = score = upperBoundCount * lowerBoundCount; - if (DEBUG) - fprintf(outputFile, - "For %s, exact, score = %d*%d, range = %d ... %d, \nlucky = %d, APROX = %d, inApproximateMode=%d \n", - variable(i), upperBoundCount, lowerBoundCount, - minC, maxC, lucky, APROX, inApproximateMode); - if (!exact || score < best) { - - best = score; - j = i; - minCj = minC; - jLe = Le; - jLowerBoundCount = lowerBoundCount; - exact = 1; - luckyExact = lucky; - if (score == 1) - break; - }; - } - else if (!exact) { - if (DEBUG) - fprintf(outputFile, - "For %s, non-exact, score = %d*%d, range = %d ... %d \n", - variable(i), upperBoundCount, lowerBoundCount, - minC, maxC); - newEqns = upperBoundCount * lowerBoundCount; - score = maxC - minC; - if (best > score) { - best = score; - j = i; - minCj = minC; - jLe = Le; - jLowerBoundCount = lowerBoundCount; - }; - }; - }; - if (lowerBoundCount == 0) { - freeEliminations(problemPtr, safeVars); - nV = nVars; - continue; - }; - i = j; - minC = minCj; - Le = jLe; - lowerBoundCount = jLowerBoundCount; - }; - -#ifdef singleResult - if (desiredResult == SIMPLIFY && !exact) { - nonConvex = 1; - problemReduced(problemPtr); - return (TRUE); - }; -#endif - - if ((doTrace && desiredResult == SIMPLIFY) || DBUG) { - fprintf(outputFile, "going to eliminate %s\n", variable(i)); - if (luckyExact) - fprintf(outputFile, "(a lucky exact elimination)\n"); - else if (exact) - fprintf(outputFile, "(an exact elimination)\n"); - }; - - - smoothed = 0; - - if (i != nV) { - int t; - j = nVars; - if (DEBUG) { - fprintf(outputFile, "Swapping %d and %d\n", i, j); - printProblem(problemPtr); - }; - swap(&problemPtr->_var[i], &problemPtr->_var[j]); - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i] != GEQs[e].coef[j]) { - GEQs[e].touched = TRUE; - t = GEQs[e].coef[i]; - GEQs[e].coef[i] = GEQs[e].coef[j]; - GEQs[e].coef[j] = t; - }; - for (e = nSUB - 1; e >= 0; e--) - if (SUBs[e].coef[i] != SUBs[e].coef[j]) { - t = SUBs[e].coef[i]; - SUBs[e].coef[i] = SUBs[e].coef[j]; - SUBs[e].coef[j] = t; - }; - if (DEBUG) { - fprintf(outputFile, "Swapping complete \n"); - printProblem(problemPtr); - fprintf(outputFile, "\n"); - }; - - i = j; - } - else if (DEBUG) { - fprintf(outputFile, "No swap needed\n"); - printProblem(problemPtr); - }; - problemPtr->addToVarsN(-1); - //nVars--; - nV = nVars; - - if (exact) { -#define maxDead maxGEQs - if (nV == 1) { - int upperBound = posInfinity; - int lowerBound = negInfinity; - int ub_color = -1; - int lb_color = -1; - int constantTerm, coefficient; - int topEqn = nGEQ - 1; - int Lc = GEQs[Le].coef[i]; - for (Le = topEqn; Le >= 0; Le--) - if ((Lc = GEQs[Le].coef[i]) == 0) { - if (GEQs[Le].coef[1] == 1) { - constantTerm = -GEQs[Le].coef[0]; - if (constantTerm > lowerBound || - (constantTerm == lowerBound && - !isRed(&GEQs[Le]))) { - lowerBound = constantTerm; - lb_color = GEQs[Le].color; - } - if (DEBUG) { - if (GEQs[Le].color == black) - fprintf(outputFile, " :::=> %s >= %d\n", - variable(1), constantTerm); - else - fprintf(outputFile, " :::=> [%s >= %d]\n", - variable(1), constantTerm); - } - } - else { - constantTerm = GEQs[Le].coef[0]; - if (constantTerm < upperBound || - (constantTerm == upperBound - && !isRed(&GEQs[Le]))) { - upperBound = constantTerm; - ub_color = GEQs[Le].color; - } - if (DEBUG) { - if (GEQs[Le].color == black) - fprintf(outputFile, " :::=> %s <= %d\n", - variable(1), constantTerm); - else - fprintf(outputFile, " :::=> [%s <= %d]\n", - variable(1), constantTerm); - } - }; - } - else if (Lc > 0) { - for (Ue = topEqn; Ue >= 0; Ue--) - if (GEQs[Ue].coef[i] < 0) { - if (GEQs[Le].key != -GEQs[Ue].key) { - int Uc = -GEQs[Ue].coef[i]; - coefficient = GEQs[Ue].coef[1] * Lc + GEQs[Le].coef[1] * Uc; - constantTerm = GEQs[Ue].coef[0] * Lc + GEQs[Le].coef[0] * Uc; - if (DEBUG) { - printGEQextra(&(GEQs[Ue])); - fprintf(outputFile, "\n"); - printGEQextra(&(GEQs[Le])); - fprintf(outputFile, "\n"); - }; - if (coefficient > 0) { - constantTerm = -(intDiv(constantTerm, coefficient)); - /* assert(black == 0) */ - if (constantTerm > lowerBound || - (constantTerm == lowerBound && - (desiredResult != SIMPLIFY || - (GEQs[Ue].color == black && GEQs[Le].color == black)))) { - lowerBound = constantTerm; - lb_color = GEQs[Ue].color || GEQs[Le].color; - } - if (DEBUG) { - if (GEQs[Ue].color || GEQs[Le].color) - fprintf(outputFile, " ::=> [%s >= %d]\n", - variable(1), constantTerm); - else - fprintf(outputFile, " ::=> %s >= %d\n", - variable(1), constantTerm); - } - } - else { - constantTerm = (intDiv(constantTerm, -coefficient)); - if (constantTerm < upperBound || - (constantTerm == upperBound && - GEQs[Ue].color == black && GEQs[Le].color == black)) { - upperBound = constantTerm; - ub_color = GEQs[Ue].color || GEQs[Le].color; - } - if (DEBUG) { - if (GEQs[Ue].color || GEQs[Le].color) - fprintf(outputFile, " ::=> [%s <= %d]\n", - variable(1), constantTerm); - else - fprintf(outputFile, " ::=> %s <= %d\n", - variable(1), constantTerm); - } - } - }; - }; - }; - //nGEQ = 0; - problemPtr->setNumGEqs(0); - if (DEBUG) - fprintf(outputFile, - " therefore, %c%d <= %c%s%c <= %d%c\n", - lb_color ? '[' : ' ', lowerBound, - (lb_color && !ub_color) ? ']' : ' ', - variable(1), - (!lb_color && ub_color) ? '[' : ' ', - upperBound, ub_color ? ']' : ' '); - if (lowerBound > upperBound) - return (FALSE); - if (safeVars == 1) { - if (upperBound == lowerBound - && !(ub_color | lb_color) - && !pleaseNoEqualitiesInSimplifiedProblems) { - //nEQ++; - problemPtr->addNumEqs(1); - EQs[0].coef[1] = -1; - EQs[0].coef[0] = upperBound; - EQs[0].color = ub_color | lb_color; - if (desiredResult == SIMPLIFY && - !EQs[0].color) - return (solve(problemPtr, desiredResult)); - }; - if (upperBound != posInfinity) { - GEQs[0].coef[1] = -1; - GEQs[0].coef[0] = upperBound; - GEQs[0].color = ub_color; - GEQs[0].key = -1; - GEQs[0].touched = 0; - //nGEQ++; - problemPtr->addNumGEqs(1); - } - - if (lowerBound != negInfinity) - { - const int numGE = nGEQ; - problemPtr->addNumGEqs(1); - - GEQs[numGE].coef[1] = 1; - GEQs[numGE].coef[0] = -lowerBound; - GEQs[numGE].color = lb_color; - GEQs[numGE].key = 1; - GEQs[numGE].touched = 0; - //nGEQ++; - } - } - - if (desiredResult == SIMPLIFY) { - problemReduced(problemPtr); - return (FALSE); - } - else { - if (!conservative && - (desiredResult != SIMPLIFY || - (!lb_color && !ub_color)) - && originalProblem != noProblem && lowerBound == upperBound) { - for (i = originalProblem->getVarsN(); i >= 0; i--) - if (originalProblem->_var[i] == problemPtr->_var[1]) - break; - if (i == 0) - break; - //e = originalProblem->_numEQs++; - e = originalProblem->getNumEqs(); - problemPtr->addNumEqs(1); - - eqnnzero(&originalProblem->_EQs[e], originalProblem->getVarsN()); - originalProblem->_EQs[e].coef[i] = -1; - originalProblem->_EQs[e].coef[0] = upperBound; - if (DEBUG) { - fprintf(outputFile, "adding equality constraint %d to outer problem\n", - e); - printProblem(originalProblem); - }; - }; - return (TRUE); - }; - }; - eliminateAgain = 1; - - if (lowerBoundCount == 1) { - struct _eqn lbEqn; - int Lc = GEQs[Le].coef[i]; - if (DBUG) - fprintf(outputFile, "an inplace elimination\n"); - - eqnncpy(&lbEqn, &GEQs[Le], (nV + 1)); - doDeleteExtra(Le, nV + 1); - for (Ue = nGEQ - 1; Ue >= 0; Ue--) - if (GEQs[Ue].coef[i] < 0) - if (lbEqn.key == -GEQs[Ue].key) { - doDeleteExtra(Ue, nV + 1); - } - else { - int Uc = -GEQs[Ue].coef[i]; - GEQs[Ue].touched = TRUE; - GEQs[Ue].color |= lbEqn.color; - eliminateAgain = 0; - for (k = 0; k <= nV; k++) - GEQs[Ue].coef[k] = GEQs[Ue].coef[k] * Lc + lbEqn.coef[k] * Uc; - if (DEBUG) { - printGEQ(&(GEQs[Ue])); - fprintf(outputFile, "\n"); - }; - }; - continue; - } - else { - int deadEqns[maxDead]; - int numDead = 0; - int topEqn = nGEQ - 1; - lowerBoundCount--; - if (DEBUG) - fprintf(outputFile, "lower bound count = %d\n", lowerBoundCount); - for (Le = topEqn; Le >= 0; Le--) - if (GEQs[Le].coef[i] > 0) { - int Lc = GEQs[Le].coef[i]; - for (Ue = topEqn; Ue >= 0; Ue--) - if (GEQs[Ue].coef[i] < 0) { - if (GEQs[Le].key != -GEQs[Ue].key) { - int Uc = -GEQs[Ue].coef[i]; - if (numDead == 0) - { - //e2 = nGEQ++; - e2 = nGEQ; - problemPtr->addNumGEqs(1); - } - else - e2 = deadEqns[--numDead]; - assert(e2 < maxGEQs); - if (DEBUG) - fprintf(outputFile, "Le = %d, Ue = %d, gen = %d\n", Le, Ue, e2); - if (DEBUG) { - printGEQextra(&(GEQs[Le])); - fprintf(outputFile, "\n"); - printGEQextra(&(GEQs[Ue])); - fprintf(outputFile, "\n"); - }; - eliminateAgain = 0; - - for (k = nV; k >= 0; k--) - GEQs[e2].coef[k] = GEQs[Ue].coef[k] * Lc + GEQs[Le].coef[k] * Uc; - GEQs[e2].coef[nV + 1] = 0; - GEQs[e2].touched = TRUE; - GEQs[e2].color = GEQs[Ue].color | GEQs[Le].color; - - if (DEBUG) { - printGEQ(&(GEQs[e2])); - fprintf(outputFile, "\n"); - }; - }; - if (lowerBoundCount == 0) { - deadEqns[numDead++] = Ue; - if (DEBUG) - fprintf(outputFile, "Killed %d\n", Ue); - }; - }; - lowerBoundCount--; - deadEqns[numDead++] = Le; - if (DEBUG) - fprintf(outputFile, "Killed %d\n", Le); - }; - { - int isDead[maxGEQs]; - for (e = nGEQ - 1; e >= 0; e--) - isDead[e] = FALSE; - while (numDead > 0) { - e = deadEqns[--numDead]; - isDead[e] = TRUE; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (isDead[e]) - doDeleteExtra(e, nV + 1); - }; - continue; - }; - } - else { - Problem *rS, *iS; - - rS = mallocProblem; -#ifdef _WIN32 - addToCollection(__LINE__, __FILE__, rS, 1); -#endif - initializeProblem(rS); - iS = mallocProblem; -#ifdef _WIN32 - addToCollection(__LINE__, __FILE__, iS, 1); -#endif - initializeProblem(iS); - rS->_getVarName = problemPtr->_getVarName; - rS->_getVarNameArgs = problemPtr->_getVarNameArgs; - iS->_getVarName = problemPtr->_getVarName; - iS->_getVarNameArgs = problemPtr->_getVarNameArgs; - - e2 = 0; - possibleEasyIntSolution = TRUE; - for (e = 0; e < nGEQ; e++) - if (GEQs[e].coef[i] == 0) { - eqncpy(&(rS->_GEQs[e2]), &GEQs[e]); - eqncpy(&(iS->_GEQs[e2]), &GEQs[e]); - if (DEBUG) { - int t; - fprintf(outputFile, "Copying (%d, %d): ", i, GEQs[e].coef[i]); - printGEQextra(&GEQs[e]); - fprintf(outputFile, "\n"); - for (t = 0; t <= nV + 1; t++) - fprintf(outputFile, "%d ", GEQs[e].coef[t]); - fprintf(outputFile, "\n"); - - }; - e2++; - assert(e2 < maxGEQs); - }; - for (Le = nGEQ - 1; Le >= 0; Le--) - if (GEQs[Le].coef[i] > 0) { - int Lc = GEQs[Le].coef[i]; - for (Ue = nGEQ - 1; Ue >= 0; Ue--) - if (GEQs[Ue].coef[i] < 0) - if (GEQs[Le].key != -GEQs[Ue].key) { - int Uc = -GEQs[Ue].coef[i]; - rS->_GEQs[e2].touched = iS->_GEQs[e2].touched = TRUE; - if (DEBUG) { - fprintf(outputFile, "---\n"); - fprintf(outputFile, "Le = %d, Ue = %d, gen = %d\n", Le, Ue, e2); - printGEQextra(&GEQs[Le]); - fprintf(outputFile, "\n"); - printGEQextra(&GEQs[Ue]); - fprintf(outputFile, "\n"); - }; - - for (k = nV; k >= 0; k--) - iS->_GEQs[e2].coef[k] = rS->_GEQs[e2].coef[k] = - GEQs[Ue].coef[k] * Lc + GEQs[Le].coef[k] * Uc; - iS->_GEQs[e2].coef[0] -= (Uc - 1) * (Lc - 1); - iS->_GEQs[e2].color = rS->_GEQs[e2].color - = GEQs[Ue].color || GEQs[Le].color; - - if (DEBUG) { - printGEQ(&(rS->_GEQs[e2])); - fprintf(outputFile, "\n"); - }; - e2++; - assert(e2 < maxGEQs); - } - else { - int Uc = -GEQs[Ue].coef[i]; - if (GEQs[Ue].coef[0] * Lc + GEQs[Le].coef[0] * Uc - (Uc - 1) * (Lc - 1) < 0) - possibleEasyIntSolution = FALSE; - }; - - }; - iS->variablesInitialized = rS->variablesInitialized = 1; - iS->setVarsN(nVars); - rS->setVarsN(nVars); - //iS->_numGEQs = rS->_numGEQs = e2; - //iS->_numEQs = rS->_numEQs = 0; - //iS->_numSUBs = rS->_numSUBs = nSUB; - iS->setNumGEqs(e2); - rS->setNumGEqs(e2); - - iS->setNumEqs(0); - rS->setNumEqs(0); - - iS->setNumSUBs(nSUB); - rS->setNumSUBs(nSUB); - - iS->_safeVars = rS->_safeVars = safeVars; - { - int t; - for (t = nV; t >= 0; t--) - rS->_var[t] = problemPtr->_var[t]; - for (t = nV; t >= 0; t--) - iS->_var[t] = problemPtr->_var[t]; - for (e = nSUB - 1; e >= 0; e--) { - eqnncpy(&(rS->_SUBs[e]), &(SUBs[e]), nVars); - eqnncpy(&(iS->_SUBs[e]), &(SUBs[e]), nVars); - }; - }; - problemPtr->addToVarsN(1); - //nVars++; - nV = nVars; - if (desiredResult != TRUE) { - int t = trace; - if (DBUG) - fprintf(outputFile, "\nreal solution(%d):\n", depth); - depth++; - trace = 0; - if (originalProblem == noProblem) { - originalProblem = problemPtr; - result = solveGEQ(rS, FALSE); - originalProblem = noProblem; - } - else - result = solveGEQ(rS, FALSE); - trace = t; - depth--; - if (result == FALSE) { -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - free(rS); - free(iS); - return (result); - }; - - if (nEQ > 0) { -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - /* An equality constraint must have been found */ - free(rS); - free(iS); - return (solve(problemPtr, desiredResult)); - }; - }; - if (desiredResult != FALSE) { - if (possibleEasyIntSolution) { - if (DBUG) - fprintf(outputFile, "\ninteger solution(%d):\n", depth); - depth++; - conservative++; - result = solveGEQ(iS, desiredResult); - conservative--; - depth--; - if (result != FALSE) { -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - free(rS); - free(iS); - return (result); - }; - }; - if (DBUG) - fprintf(outputFile, "have to do exact analysis\n"); - { - int worstLowerBoundConstant = -minC; - int lowerBounds = 0; - int lowerBound[maxGEQs]; - int smallest; - int t; - conservative++; - for (e = 0; e < nGEQ; e++) - if (GEQs[e].coef[i] > 1) - lowerBound[lowerBounds++] = e; - /* sort array */ - for (j = 0; j < lowerBounds; j++) { - smallest = j; - for (k = j + 1; k < lowerBounds; k++) - if (GEQs[lowerBound[smallest]].coef[i] > GEQs[lowerBound[k]].coef[i]) - smallest = k; - t = lowerBound[smallest]; - lowerBound[smallest] = lowerBound[j]; - lowerBound[j] = t; - }; - if (DEBUG) { - fprintf(outputFile, "lower bound coeeficients = "); - for (j = 0; j < lowerBounds; j++) - fprintf(outputFile, " %d", GEQs[lowerBound[j]].coef[i]); - fprintf(outputFile, "\n"); - }; - - - for (j = 0; j < lowerBounds; j++) { - int maxIncr; - int c; - e = lowerBound[j]; - maxIncr = ((GEQs[e].coef[i] - 1) * (worstLowerBoundConstant - 1) - 1) - / worstLowerBoundConstant; - - /* maxIncr += 2; */ - if ((doTrace && desiredResult == SIMPLIFY) || DBUG) { - fprintf(outputFile, "for equation "); - printGEQ(&GEQs[e]); - fprintf(outputFile, "\ntry decrements from 0 to %d\n", maxIncr); - printProblem(problemPtr); - }; - if (maxIncr > 50) { - if (!smoothed && smoothWeirdEquations(problemPtr)) { - conservative--; -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - free(rS); - free(iS); - smoothed = 1; - goto solveGEQstart; - }; - }; - eqncpy(&EQs[0], &GEQs[e]); - /* - * if (GEQs[e].color) fprintf(outputFile,"warning: adding black equality constraint - * based on red inequality\n"); - */ - EQs[0].color = black; - eqnzero(&GEQs[e]); - GEQs[e].touched = TRUE; - //nEQ = 1; - problemPtr->setNumEqs(1); - for (c = maxIncr; c >= 0; c--) { - if (DBUG) - fprintf(outputFile, "trying next decrement of %d\n", maxIncr - c); - if (DBUG) - printProblem(problemPtr); - problemcpy(rS, problemPtr); - if (DEBUG) - printProblem(rS); - result = solve(rS, desiredResult); - if (result == TRUE) { -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - free(rS); - free(iS); - conservative--; - return (TRUE); - }; - EQs[0].coef[0]--; - }; - if (j + 1 < lowerBounds) { - //nEQ = 0; - problemPtr->setNumEqs(0); - eqncpy(&GEQs[e], &EQs[0]); - GEQs[e].touched = 1; - GEQs[e].color = black; - problemcpy(rS, problemPtr); - if (DEBUG) - fprintf(outputFile, "exhausted lower bound, checking if still feasible "); - result = solve(rS, FALSE); - if (result == FALSE) - break; - }; - }; - if ((doTrace && desiredResult == SIMPLIFY) || DBUG) - fprintf(outputFile, "fall-off the end\n"); -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - free(rS); - free(iS); - - conservative--; - return (FALSE); - }; - }; -#ifdef _WIN32 - removeFromCollection(rS); - removeFromCollection(iS); -#endif - free(rS); - free(iS); - }; - return (UNKNOWN); - }; - } while (eliminateAgain); - }; -} - - -/* - * Return 1 if red equations constrain the set of possible solutions. We assume that there are solutions to the black - * equations by themselves, so if there is no solution to the combined problem, we return 1. - */ - -int hasRedEquations(Problem * problemPtr, bool expensive) -{ - int result; - int e; - int i; - - if (TRACE) { - fprintf(outputFile, "Checking for red equations:\n"); - printProblem(problemPtr); - }; - - pleaseNoEqualitiesInSimplifiedProblems++; - mayBeRed++; - result = !simplifyProblem(problemPtr); - mayBeRed--; - pleaseNoEqualitiesInSimplifiedProblems--; - if (result) { - return 1; - } - freeRedEliminations(problemPtr); - - - assert(nEQ == 0); - - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].color == red) - result = 1; - if (!result) - return 0; - - if (!expensive) - for (i = safeVars; i >= 1; i--) { - int ub = 0; - int lb = 0; - for (e = nGEQ - 1; e >= 0; e--) { - if (GEQs[e].coef[i]) - if (GEQs[e].coef[i] > 0) - lb |= (1 + GEQs[e].color); - else /* (GEQs[e].coef[i] < 0) */ - ub |= (1 + GEQs[e].color); - } - if (ub == 2 || lb == 2) { - if (DBUG) - fprintf(outputFile, "checks for upper/lower bounds worked!\n"); -#if reduceWithSubstitutions -#else - resurrectSubs(problemPtr); - assert(nSUB == 0); -#endif - return 1; - }; - }; - - - if (TRACE) - fprintf(outputFile, "*** Doing potentially expensive elimination tests for red equations\n"); - pleaseNoEqualitiesInSimplifiedProblems++; - eliminateRed(problemPtr, expensive); - pleaseNoEqualitiesInSimplifiedProblems--; - result = 0; - assert(nEQ == 0); - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].color == red) - result = 1; - if (TRACE) - if (!result) - fprintf(outputFile, "******************** Redudant Red Equations eliminated!!\n"); - else - fprintf(outputFile, "******************** Red Equations remain\n"); - if (DEBUG) - printProblem(problemPtr); - -#if reduceWithSubstitutions -#else - resurrectSubs(problemPtr); - assert(nSUB == 0); -#endif - return result; -} - -int simplifyApproximate(Problem *problemPtr) -{ - int result; - - inApproximateMode = 1; - if (TRACE) - fprintf(outputFile, "Entering Approximate Mode\n"); - - result = simplifyProblem(problemPtr); - - if (nVars != safeVars) { - fprintf(outputFile, "assertion blown!\n"); - printProblem(problemPtr); - fflush(outputFile); - assert(nVars == safeVars); - }; - - if (TRACE) - fprintf(outputFile, "Leaving Approximate Mode\n"); - inApproximateMode = 0; - -#if reduceWithSubstitutions -#else - assert(nSUB == 0); -#endif - return (result); -} - -int simplifyProblem(Problem *problemPtr) -{ - int i; - foundReduction = FALSE; - - if (!problemPtr->variablesInitialized) { - initializeVariables(problemPtr); - }; - - -#ifdef clearForwardingPointers - for (i = 1; i <= nVars; i++) - if (problemPtr->_var[i] > 0) - problemPtr->forwardingAddress[problemPtr->_var[i]] = 0; -#endif - - if (nextKey * 3 > maxKeys) { - int e; - hashVersion++; - nextKey = maxVars + 1; - for (e = nGEQ - 1; e >= 0; e--) - GEQs[e].touched = TRUE; - for (i = 0; i < hashTableSize; i++) - hashMaster[i].touched = -1; - problemPtr->hashVersion = hashVersion; - } - else if (problemPtr->hashVersion != hashVersion) { - int e; - for (e = nGEQ - 1; e >= 0; e--) - GEQs[e].touched = TRUE; - problemPtr->hashVersion = hashVersion; - }; - - nonConvex = 0; - if (nVars > nEQ + 3 * safeVars) - freeEliminations(problemPtr, safeVars); - - solve(problemPtr, SIMPLIFY); - if (foundReduction) { - for (i = 1; i <= safeVars; i++) - problemPtr->forwardingAddress[problemPtr->_var[i]] = i; - for (i = 0; i < nSUB; i++) - problemPtr->forwardingAddress[SUBs[i].key] = -i - 1; - }; -#if reduceWithSubstitutions -#else - assert(pleaseNoEqualitiesInSimplifiedProblems - || !foundReduction || nSUB == 0); -#endif - return (foundReduction); -} - - -void unprotectVariable(Problem *problemPtr, int v) -{ - int e, t, j, i; - i = problemPtr->forwardingAddress[v]; - if (i < 0) { - i = -1 - i; - //nSUB--; - problemPtr->addNumSUBs(-1); - if (i < nSUB) { - eqncpy(&SUBs[i], &SUBs[nSUB]); - problemPtr->forwardingAddress[SUBs[i].key] = -i - 1; - }; - } - else { - int bringToLife[maxVars]; - int comingBack = 0; - int e2; - for (e = nSUB - 1; e >= 0; e--) - if (bringToLife[e] = (SUBs[e].coef[i] != 0)) - comingBack++; - - for (e2 = nSUB - 1; e2 >= 0; e2--) - if (bringToLife[e2]) { - problemPtr->addToVarsN(1); - //nVars++; - safeVars++; - if (safeVars < nVars) { - for (e = nGEQ - 1; e >= 0; e--) { - GEQs[e].coef[nVars] = GEQs[e].coef[safeVars]; - GEQs[e].coef[safeVars] = 0; - }; - for (e = nEQ - 1; e >= 0; e--) { - EQs[e].coef[nVars] = EQs[e].coef[safeVars]; - EQs[e].coef[safeVars] = 0; - }; - for (e = nSUB - 1; e >= 0; e--) { - SUBs[e].coef[nVars] = SUBs[e].coef[safeVars]; - SUBs[e].coef[safeVars] = 0; - }; - problemPtr->_var[nVars] = problemPtr->_var[safeVars]; - problemPtr->forwardingAddress[problemPtr->_var[nVars]] = nVars; - } - else { - for (e = nGEQ - 1; e >= 0; e--) { - GEQs[e].coef[safeVars] = 0; - }; - for (e = nEQ - 1; e >= 0; e--) { - EQs[e].coef[safeVars] = 0; - }; - for (e = nSUB - 1; e >= 0; e--) { - SUBs[e].coef[safeVars] = 0; - }; - }; - - problemPtr->_var[safeVars] = SUBs[e2].key; - problemPtr->forwardingAddress[SUBs[e2].key] = safeVars; - - const int numEQ = nEQ; - problemPtr->addNumEqs(1); - - eqncpy(&(EQs[numEQ]), &(SUBs[e2])); - //EQs[nEQ++].coef[problemPtr->_safeVars] = -1; - EQs[numEQ].coef[problemPtr->_safeVars] = -1; - - assert(nEQ <= maxEQs); - if (e2 < nSUB - 1) - eqncpy(&(SUBs[e2]), &(SUBs[nSUB - 1])); - //nSUB--; - problemPtr->addNumSUBs(-1); - }; - - - - - if (i < safeVars) { - j = safeVars; - for (e = nSUB - 1; e >= 0; e--) { - t = SUBs[e].coef[j]; - SUBs[e].coef[j] = SUBs[e].coef[i]; - SUBs[e].coef[i] = t; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[j] != GEQs[e].coef[i]) { - GEQs[e].touched = TRUE; - t = GEQs[e].coef[j]; - GEQs[e].coef[j] = GEQs[e].coef[i]; - GEQs[e].coef[i] = t; - }; - for (e = nEQ - 1; e >= 0; e--) { - t = EQs[e].coef[j]; - EQs[e].coef[j] = EQs[e].coef[i]; - EQs[e].coef[i] = t; - }; - t = problemPtr->_var[j]; - problemPtr->_var[j] = problemPtr->_var[i]; - problemPtr->_var[i] = t; - problemPtr->forwardingAddress[problemPtr->_var[i]] = i; - problemPtr->forwardingAddress[problemPtr->_var[j]] = j; - }; - safeVars--; - }; - chainUnprotect(problemPtr); - -} - - -int constrainVariableSign(Problem *problemPtr, int color, int i, int sign) -{ - int nV = nVars; - int e, k, j; - - k = problemPtr->forwardingAddress[i]; - if (k < 0) { - k = -1 - k; - - if (sign != 0) { - //e = nGEQ++; - e = nGEQ; - problemPtr->addNumGEqs(1); - - eqncpy(&GEQs[e], &SUBs[k]); - for (j = 0; j <= nV; j++) - GEQs[e].coef[j] *= sign; - GEQs[e].coef[0]--; - GEQs[e].touched = 1; - GEQs[e].color = color; - } - else { - //e = nEQ++; - e = nEQ; - problemPtr->addNumEqs(1); - - assert(nEQ <= maxEQs); - eqncpy(&EQs[e], &SUBs[k]); - EQs[e].color = color; - }; - - } - else if (sign != 0) { - //e = nGEQ++; - e = nGEQ; - problemPtr->addNumGEqs(1); - - eqnzero(&GEQs[e]); - GEQs[e].coef[k] = sign; - GEQs[e].coef[0] = -1; - GEQs[e].touched = 1; - GEQs[e].color = color; - } - else { - //e = nEQ++; - e = nEQ; - problemPtr->addNumEqs(1); - - assert(nEQ <= maxEQs); - eqnzero(&EQs[e]); - EQs[e].coef[k] = 1; - EQs[e].color = color; - }; - unprotectVariable(problemPtr, i); - return (simplifyProblem(problemPtr)); -} - -void constrainVariableValue(Problem *problemPtr, int color, int i, int value) -{ - int e, k; - - k = problemPtr->forwardingAddress[i]; - if (k < 0) { - k = -1 - k; - - //e = nEQ++; - e = nEQ; - problemPtr->addNumEqs(1); - - assert(nEQ <= maxEQs); - eqncpy(&EQs[e], &SUBs[k]); - EQs[e].coef[0] -= value; - - } - else { - //e = nEQ++; - e = nEQ; - problemPtr->addNumEqs(1); - - eqnzero(&EQs[e]); - EQs[e].coef[k] = 1; - EQs[e].coef[0] = -value; - }; - EQs[e].color = color; -} - -int queryVariable(Problem *problemPtr, int i, int *lowerBound, int *upperBound) -{ - int nV = nVars; - int e, j; - int isSimple; - int coupled = FALSE; - i = problemPtr->forwardingAddress[i]; - - (*lowerBound) = negInfinity; - (*upperBound) = posInfinity; - - if (i < 0) { - int easy = TRUE; - i = -i - 1; - for (j = 1; j <= nV; j++) - if (SUBs[i].coef[j] != 0) - easy = FALSE; - if (easy) { - *upperBound = *lowerBound = SUBs[i].coef[0]; - return (FALSE); - }; - return (TRUE); - }; - - for (e = nSUB - 1; e >= 0; e--) - if (SUBs[e].coef[i] != 0) - coupled = TRUE; - - for (e = nEQ - 1; e >= 0; e--) - if (EQs[e].coef[i] != 0) { - isSimple = TRUE; - for (j = 1; j <= nV; j++) - if (i != j && EQs[e].coef[j] != 0) { - isSimple = FALSE; - coupled = TRUE; - break; - }; - if (!isSimple) - continue; - else { - *lowerBound = *upperBound = -EQs[e].coef[i] * EQs[e].coef[0]; - return (FALSE); - }; - }; - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[i] != 0) { - if (GEQs[e].key == i) { - setMax(*lowerBound, -GEQs[e].coef[0]); - } - else if (GEQs[e].key == -i) { - setMin(*upperBound, GEQs[e].coef[0]); - } - else - coupled = TRUE; - }; - return (coupled); -} - -extern void queryCoupledVariable(Problem *problemPtr, int i, int *l, int *u, int *couldBeZero, int lowerBound, int upperBound); - -int queryVariableBounds(Problem *problemPtr, int i, int *l, int *u) -{ - int coupled; - *l = negInfinity; - *u = posInfinity; - coupled = queryVariable(problemPtr, i, l, u); - if (!coupled || (nVars == 1 && problemPtr->forwardingAddress[i] == 1)) - return 0; - if (abs(problemPtr->forwardingAddress[i]) == 1 && nVars + nSUB == 2 && nEQ + nSUB == 1) { - int couldBeZero; - queryCoupledVariable(problemPtr, i, l, u, &couldBeZero, negInfinity, posInfinity); - return 0; - }; - return 1; -} - -void queryCoupledVariable(Problem *problemPtr, int i, int *l, int *u, int *couldBeZero, int lowerBound, int upperBound) -{ - int e, b1, b2; - Eqn eqn; - int sign; - int v; - - if (abs(problemPtr->forwardingAddress[i]) != 1 || nVars + nSUB != 2 || nEQ + nSUB != 1) { - fprintf(outputFile, "queryCoupledVariablecalled with bad parameters\n"); - printProblem(problemPtr); - Exit(2); - }; - - if (problemPtr->forwardingAddress[i] == -1) { - eqn = &SUBs[0]; - sign = 1; - v = 1; - } - else { - eqn = &EQs[0]; - sign = -eqn->coef[1]; - v = 2; - }; - - /* Variable i is defined in terms of variable v */ - - for (e = nGEQ - 1; e >= 0; e--) - if (GEQs[e].coef[v] != 0) { - if (GEQs[e].coef[v] == 1) { - setMax(lowerBound, -GEQs[e].coef[0]) - } - else { - setMin(upperBound, GEQs[e].coef[0]); - }; - }; - /* lowerBound and upperBound are bounds on the value of v */ - - if (lowerBound > upperBound) { - *l = posInfinity; - *u = negInfinity; - *couldBeZero = 0; - return; - }; - if (lowerBound == negInfinity) { - if (eqn->coef[v] > 0) - b1 = sign * negInfinity; - else - b1 = -sign * negInfinity; - } - else - b1 = sign * (eqn->coef[0] + eqn->coef[v] * lowerBound); - if (upperBound == posInfinity) { - if (eqn->coef[v] > 0) - b2 = sign * posInfinity; - else - b2 = -sign * posInfinity; - } - else - b2 = sign * (eqn->coef[0] + eqn->coef[v] * upperBound); - - /* b1 and b2 are bounds on the value of i (don't know which is upper bound) */ - if (b1 <= b2) { - setMax(*l, b1); - setMin(*u, b2); - } - else { - setMax(*l, b2); - setMin(*u, b1); - }; - *couldBeZero = *l <= 0 && 0 <= *u && intMod(eqn->coef[0], abs(eqn->coef[v])) == 0; -} - - - -int queryVariableSigns(Problem *problemPtr, int i, int dd_lt, int dd_eq, int dd_gt, int lowerBound, int upperBound, bool *distKnown, int *dist) -{ - int result; - int l, u; - int couldBeZero; - - l = negInfinity; - u = posInfinity; - - queryCoupledVariable(problemPtr, i, &l, &u, &couldBeZero, lowerBound, upperBound); - result = 0; - if (l < 0) - result |= dd_gt; - if (u > 0) - result |= dd_lt; - if (couldBeZero) - result |= dd_eq; - if (l == u) { - *distKnown = 1; - *dist = l; - } - else - *distKnown = 0; - return (result); -} - - -static int omegaInitialized = 0; - -void initializeOmega() -{ - int i; - if (omegaInitialized) - return; - - nextWildCard = 0; - nextKey = maxVars + 1; - for (i = 0; i < hashTableSize; i++) - hashMaster[i].touched = -1; - - sprintf(wildName[1], "alpha"); - sprintf(wildName[2], "beta"); - sprintf(wildName[3], "gamma"); - sprintf(wildName[4], "delta"); - sprintf(wildName[5], "tau"); - sprintf(wildName[6], "sigma"); - sprintf(wildName[7], "chi"); - sprintf(wildName[8], "omega"); - sprintf(wildName[9], "pi"); - sprintf(wildName[10], "ni"); - sprintf(wildName[11], "Alpha"); - sprintf(wildName[12], "Beta"); - sprintf(wildName[13], "Gamma"); - sprintf(wildName[14], "Delta"); - sprintf(wildName[15], "Tau"); - sprintf(wildName[16], "Sigma"); - sprintf(wildName[17], "Chi"); - sprintf(wildName[18], "Omega"); - sprintf(wildName[19], "Pi"); - - omegaInitialized = 1; -} - - -void setOutputFile(FILE * file) -{ - /* sets the file to which printProblem should send its output to "file" */ - - outputFile = file; - -} /* end setOutputFile(FILE *file) */ +/* $Id */ + +/* + * Source code for an implementation of the Omega test, a integer programming algorithm for dependence analysis, by + * William Pugh, appeared in Supercomputing '91 and CACM Aug 92 + * + * C Source written by: William Pugh Dept. of Computer Science Univ. of Maryland, College Park pugh@cs.umd.edu + * + * Options SPEED - don't bother checking debugging flags eliminateRedundantConstraints - use expensive methods to + * eliminate all redundant constraints singleResult - only produce a single simplified result verifySimplication - + * simplifyProblem checks for problem with no soloutions reduceWithSubstitutions - if not defined, convert + * substitutions back to EQs APROX - approximate inexact reductions + */ + + +#define maxWildCards 18 +#ifndef APROX +#define APROX 0 +#endif + +#ifdef _WIN32 +extern "C" void addToCollection(const int line, const char *file, void *pointer, int type); +extern "C" void removeFromCollection(void *pointer); +#endif + +#include "include/portable.h" +#include "include/flags.h" +#include +#include +#include +#include "include/ip.h" + + +#define EQ_type 1 +#define GEQ_type 2 +#define keyMult 31 +#define indexMult 17 +#define abs(x) ((x) >= 0 ? (x) : -(x)) +#define max(x,y) ((x) > (y) ? (x) : (y)) +#define min(x,y) ((x) < (y) ? (x) : (y)) +#define setMax(m,x) { if ((m) < (x)) m = (x); } +#define setMin(m,x) { if ((m) > (x)) m = (x); } +#ifndef TRUE +# define TRUE 1 +#endif +#ifndef FALSE +# define FALSE 0 +#endif +#define nVars (problemPtr->getVarsN()) +#define nEQ (problemPtr->getNumEqs()) +#define nGEQ (problemPtr->getNumGEqs()) +#define nSUB (problemPtr->getNumSUBs()) +#define SUBs (problemPtr->_SUBs) +#define GEQs (problemPtr->_GEQs) +#define EQs (problemPtr->_EQs) +#define safeVars (problemPtr->_safeVars) +#define printEQ(e) printEqn(problemPtr,e,0,0) +#define printGEQ(e) printEqn(problemPtr,e,1,0) +#define printGEQextra(e) printEqn(problemPtr,e,1,1) +#define variable(i) orgVariable(problemPtr->_var[i]) +#define orgVariable(i) (i == 0 ? "1" : (i < 0?wildName[-i]: (*problemPtr->_getVarName)(i,problemPtr->_getVarNameArgs))) +#define doTrace (trace && TRACE) +#define doDelete(e,nV) {if (DEBUG) {fprintf(outputFile,"Deleting %d (last:%d): ",e,nGEQ-1); printGEQ(&GEQs[e]);fprintf(outputFile,"\n");}; \ + if (e < nGEQ-1) eqnncpy (&GEQs[e], &GEQs[nGEQ - 1],(nV)); \ + (problemPtr->addNumGEqs(-1));} + +#define doDeleteExtra(e,nV) {if (DEBUG) {fprintf(outputFile,"Deleting %d: ",e); printGEQextra(&GEQs[e]);fprintf(outputFile,"\n");}; \ + if (e < nGEQ-1) eqnncpy (&GEQs[e], &GEQs[nGEQ - 1],(nV)); \ + (problemPtr->addNumGEqs(-1));} +#define isRed(e) (desiredResult == SIMPLIFY && (e)->color) + + +static int mayBeRed = 0; +#define hashTableSize 550 +#define maxKeys 500 + +#ifdef SPEED +#define TRACE 0 +#define DBUG 0 +#define DEBUG 0 +#else +#define TRACE (debugLevel) +#define DBUG (debugLevel > 1) +#define DEBUG (debugLevel > 2) +#endif +extern void addingEqualityConstraint(Problem * problemPtr, int e); + +#define mallocProblem ((Problem *) malloc(sizeof(Problem))) + +static struct _eqn hashMaster[hashTableSize]; + +int nonConvex = 0; + +int firstCheckForRedundantEquations = 0; + +static int doItAgain; + +static int conservative = 0; + +static FILE *outputFile; /* printProblem writes its output to this file */ + +static int nextKey; +static char wildName[200][20]; +static int nextWildCard = 0; +static int trace = 1; +static int depth = 0; +static int foundReduction; +static int packing[maxVars]; + +static int inApproximateMode = 0; + +#if reduceWithSubstitutions +int reduceWithSubs = 1; +#else +int reduceWithSubs = 0; +#endif + +static int pleaseNoEqualitiesInSimplifiedProblems = 0; + +int hashVersion = 0; + +#define noProblem ((Problem *) 0) + +Problem *originalProblem = noProblem; + +void noProcedure(Problem *problemPtr) { } +void(*whenReduced) (Problem *problemPtr) = noProcedure; + +static int gcd(int b, int a) +{ + if (b == 1) + return (1); + while (b != 0) { + int t = b; + b = a % b; + a = t; + }; + return (a); +} + + +int checkIfSingleVar(Eqn e, int i) +{ + for (; i > 0; i--) + if (e->coef[i]) { + i--; + break; + }; + for (; i > 0; i--) + if (e->coef[i]) + break; + return (i == 0); +} + + +void initializeVariables(Problem *p) +{ + int i; + for (i = p->getVarsN(); i > 0; i--) + p->forwardingAddress[i] = p->_var[i] = i; + p->variablesInitialized = 1; +} + +void initializeProblem(Problem *p) +{ + p->hashVersion = hashVersion; + p->variablesInitialized = 0; + p->variablesFreed = 0; + p->_safeVars = 0; + p->_init(0, 0, 0, 0); +} + +void problemcpy(Problem *p1, Problem *p2) +{ + int e, i; + p1->hashVersion = p2->hashVersion; + p1->variablesInitialized = p2->variablesInitialized; + p1->variablesFreed = p2->variablesFreed; + p1->_safeVars = p2->_safeVars; + p1->_init(p2->getNumEqs(), p2->getNumGEqs(), p2->getNumSUBs(), p2->getVarsN()); + + for (e = p2->getNumEqs() - 1; e >= 0; e--) + eqnncpy(&(p1->_EQs[e]), &(p2->_EQs[e]), p2->getVarsN()); + + for (e = p2->getNumGEqs() - 1; e >= 0; e--) + eqnncpy(&(p1->_GEQs[e]), &(p2->_GEQs[e]), p2->getVarsN()); + + for (e = p2->getNumSUBs() - 1; e >= 0; e--) + eqnncpy(&(p1->_SUBs[e]), &(p2->_SUBs[e]), p2->getVarsN()); + + p1->_var.resize(p2->getVarsN() + 1); + + for (i = 0; i <= p2->getVarsN(); i++) + p1->_var[i] = p2->_var[i]; + + p1->forwardingAddress.resize(maxVars + 2); + for (i = 0; i <= maxVars; i++) + p1->forwardingAddress[i] = p2->forwardingAddress[i]; + p1->_getVarName = p2->_getVarName; + p1->_getVarNameArgs = p2->_getVarNameArgs; +} + + + +static void printTerm(Problem *problemPtr, Eqn e, int c) +{ + int i; + int first; + int n = nVars; + int wentFirst = -1; + first = 1; + for (i = 1; i <= n; i++) + if (c * e->coef[i] > 0) { + + first = 0; + wentFirst = i; + + if (c * e->coef[i] == 1) + fprintf(outputFile, "%s", variable(i)); + else + fprintf(outputFile, "%d%s", c * e->coef[i], variable(i)); + break; + }; + for (i = 1; i <= n; i++) + if (i != wentFirst && c * e->coef[i] != 0) { + if (!first && c * e->coef[i] > 0) + fprintf(outputFile, "+"); + + first = 0; + + if (c * e->coef[i] == 1) + fprintf(outputFile, "%s", variable(i)); + else if (c * e->coef[i] == -1) + fprintf(outputFile, "-%s", variable(i)); + else + fprintf(outputFile, "%d%s", c * e->coef[i], variable(i)); + }; + if (!first && c * e->coef[0] > 0) + fprintf(outputFile, "+"); + if (first || c * e->coef[0] != 0) + fprintf(outputFile, "%d", c * e->coef[0]); +} + + +void printEqn(Problem *problemPtr, Eqn e, int test, int extra) +{ + char buf[maxVars * 12 + 80]; + + sprintEqn(buf, problemPtr, e, test, extra); + fprintf(outputFile, "%s", buf); +} + +void sprintEqn(char *str, Problem *problemPtr, Eqn e, int test, int extra) +{ + int i; + int first; + int n = nVars + extra; + + if (test) { + if (DEBUG && e->touched) { + sprintf(str, "!"); + while (*str) + str++; + } + else if (DBUG && !e->touched && e->key != 0) { + sprintf(str, "%d: ", e->key); + while (*str) + str++; + } + } + if (e->color) { + sprintf(str, "["); + while (*str) + str++; + } + first = 1; + for (i = 0; i <= n; i++) + if (e->coef[i] < 0) { + if (!first) { + sprintf(str, "+"); + while (*str) + str++; + } + else + first = 0; + if (i == 0) { + sprintf(str, "%d", -e->coef[i]); + while (*str) + str++; + } + else if (e->coef[i] == -1) { + sprintf(str, "%s", variable(i)); + while (*str) + str++; + } + else { + sprintf(str, "%d%s", -e->coef[i], variable(i)); + while (*str) + str++; + } + }; + if (first) { + sprintf(str, "0"); + while (*str) + str++; + } + if (test == 0) { + sprintf(str, " = "); + while (*str) + str++; + } + else { + sprintf(str, " <= "); + while (*str) + str++; + } + + first = 1; + for (i = 0; i <= n; i++) + if (e->coef[i] > 0) { + if (!first) { + sprintf(str, "+"); + while (*str) + str++; + } + else + first = 0; + if (i == 0) { + sprintf(str, "%d", e->coef[i]); + while (*str) + str++; + } + else if (e->coef[i] == 1) { + sprintf(str, "%s", variable(i)); + while (*str) + str++; + } + else { + sprintf(str, "%d%s", e->coef[i], variable(i)); + while (*str) + str++; + } + } + if (first) { + sprintf(str, "0"); + while (*str) + str++; + } + if (e->color) { + sprintf(str, "]"); + while (*str) + str++; + } +} + +void printVars(Problem *problemPtr) +{ + int i; + fprintf(outputFile, "variables = "); + if (problemPtr->_safeVars > 0) + fprintf(outputFile, "("); + for (i = 1; i <= nVars; i++) { + fprintf(outputFile, "%s", variable(i)); + if (i == problemPtr->_safeVars) + fprintf(outputFile, ")"); + if (i < nVars) + fprintf(outputFile, ", "); + }; + fprintf(outputFile, "\n"); +} + +void printProblem(Problem *problemPtr) +{ + int e; + + if (!problemPtr->variablesInitialized) + initializeVariables(problemPtr); + printVars(problemPtr); + for (e = 0; e < nEQ; e++) { + printEQ(&EQs[e]); + fprintf(outputFile, "\n"); + }; + for (e = 0; e < nGEQ; e++) { + printGEQ(&GEQs[e]); + fprintf(outputFile, "\n"); + }; + for (e = 0; e < nSUB; e++) { + Eqn eq = &SUBs[e]; + if (DBUG && eq->color) + fprintf(outputFile, "["); + if (eq->key > 0) + fprintf(outputFile, "%s := ", orgVariable(eq->key)); + else + fprintf(outputFile, "#%d := ", eq->key); + printTerm(problemPtr, eq, 1); + if (DBUG && eq->color) + fprintf(outputFile, "]"); + fprintf(outputFile, "\n"); + }; + fflush(outputFile); +} + + +void printRedEquations(Problem *problemPtr) +{ + int e; + + if (!problemPtr->variablesInitialized) + initializeVariables(problemPtr); + printVars(problemPtr); + for (e = 0; e < nEQ; e++) { + if (EQs[e].color == red) { + printEQ(&EQs[e]); + fprintf(outputFile, "\n"); + } + }; + for (e = 0; e < nGEQ; e++) { + if (GEQs[e].color == red) { + printGEQ(&GEQs[e]); + fprintf(outputFile, "\n"); + } + }; + for (e = 0; e < nSUB; e++) { + if (SUBs[e].color == red) { + Eqn eq = &SUBs[e]; + if (DBUG && eq->color) + fprintf(outputFile, "["); + if (eq->key > 0) + fprintf(outputFile, "%s := ", orgVariable(eq->key)); + else + fprintf(outputFile, "#%d := ", eq->key); + printTerm(problemPtr, eq, 1); + if (DBUG && eq->color) + fprintf(outputFile, "]"); + fprintf(outputFile, "\n"); + } + }; + fflush(outputFile); +} + + +void prettyPrintProblem(Problem *problemPtr) +{ + int e; + int v; + int live[maxGEQs]; + int v1, v2, v3; + int t, change; + int stuffPrinted = 0; + + + typedef enum { + none, le, lt + } partialOrderType; + + partialOrderType po[maxVars][maxVars]; + int poE[maxVars][maxVars]; + int lastLinks[maxVars]; + int firstLinks[maxVars]; + int chainLength[maxVars]; + int chain[maxVars]; + int i, m, multiprint; + + + if (!problemPtr->variablesInitialized) + initializeVariables(problemPtr); + + if (nVars > 0) { + eliminateRedundant(problemPtr, 0); + + for (e = 0; e < nEQ; e++) { + if (stuffPrinted) + fprintf(outputFile, "; "); + stuffPrinted = 1; + printEQ(&EQs[e]); + }; + + for (e = 0; e < nGEQ; e++) + live[e] = TRUE; + + while (1) { + for (v = 1; v <= nVars; v++) { + lastLinks[v] = firstLinks[v] = 0; + chainLength[v] = 0; + for (v2 = 1; v2 <= nVars; v2++) + po[v][v2] = none; + }; + + for (e = 0; e < nGEQ; e++) + if (live[e]) { + for (v = 1; v <= nVars; v++) { + if (GEQs[e].coef[v] == 1) + firstLinks[v]++; + else if (GEQs[e].coef[v] == -1) + lastLinks[v]++; + }; + + v1 = nVars; + while (v1 > 0 && GEQs[e].coef[v1] == 0) + v1--; + v2 = v1 - 1; + while (v2 > 0 && GEQs[e].coef[v2] == 0) + v2--; + v3 = v2 - 1; + while (v3 > 0 && GEQs[e].coef[v3] == 0) + v3--; + + if (GEQs[e].coef[0] > 0 || GEQs[e].coef[0] < -1 + || v2 <= 0 || v3 > 0 + || GEQs[e].coef[v1] * GEQs[e].coef[v2] != -1) { + /* Not a partial order relation */ + + } + else { + if (GEQs[e].coef[v1] == 1) { + v3 = v2; + v2 = v1; + v1 = v3; + }; + /* relation is v1 <= v2 or v1 < v2 */ + po[v1][v2] = ((GEQs[e].coef[0] == 0) ? le : lt); + poE[v1][v2] = e; + }; + } + for (v = 1; v <= nVars; v++) + chainLength[v] = lastLinks[v]; + + /* Just in case nVars <= 0 */ + change = FALSE; + for (t = 0; t < nVars; t++) { + change = FALSE; + for (v1 = 1; v1 <= nVars; v1++) + for (v2 = 1; v2 <= nVars; v2++) + if (po[v1][v2] != none && + chainLength[v1] <= chainLength[v2]) { + chainLength[v1] = chainLength[v2] + 1; + change = TRUE; + } + }; + + if (change) { + /* caught in cycle */ + assert(0); + }; + + + v = 1; + for (v1 = 2; v1 <= nVars; v1++) + if (chainLength[v1] + firstLinks[v1] > chainLength[v] + firstLinks[v]) + v = v1; + + if (chainLength[v] == 0) + break; + + if (stuffPrinted) + fprintf(outputFile, "; "); + stuffPrinted = 1; + /* chain starts at v */ + /* print firstLinks */ + { + int tmp, first; + first = 1; + for (e = 0; e < nGEQ; e++) + if (live[e] && GEQs[e].coef[v] == 1) { + if (!first) + fprintf(outputFile, ", "); + tmp = GEQs[e].coef[v]; + GEQs[e].coef[v] = 0; + printTerm(problemPtr, &GEQs[e], -1); + GEQs[e].coef[v] = tmp; + live[e] = FALSE; + first = 0; + }; + if (!first) + fprintf(outputFile, " <= "); + }; + + + /* find chain */ + chain[0] = v; + m = 1; + while (1) { + /* print chain */ + for (v2 = 1; v2 <= nVars; v2++) + if (po[v][v2] && chainLength[v] == 1 + chainLength[v2]) + break; + if (v2 > nVars) + break; + chain[m++] = v2; + v = v2; + } + + fprintf(outputFile, "%s", variable(chain[0])); + + multiprint = 0; + for (i = 1; i < m; i++) { + v = chain[i - 1]; + v2 = chain[i]; + if (po[v][v2] == le) + fprintf(outputFile, " <= "); + else + fprintf(outputFile, " < "); + fprintf(outputFile, "%s", variable(v2)); + live[poE[v][v2]] = FALSE; + if (!multiprint && i < m - 1) + for (v3 = 1; v3 <= nVars; v3++) { + if (v == v3 || v2 == v3) + continue; + if (po[v][v2] != po[v][v3]) + continue; + if (po[v2][chain[i + 1]] != po[v3][chain[i + 1]]) + continue; + fprintf(outputFile, ",%s", variable(v3)); + live[poE[v][v3]] = FALSE; + live[poE[v3][chain[i + 1]]] = FALSE; + multiprint = 1; + } + else + multiprint = 0; + }; + + v = chain[m - 1]; + /* print lastLinks */ + { + int tmp, first; + first = 1; + for (e = 0; e < nGEQ; e++) + if (live[e] && GEQs[e].coef[v] == -1) { + if (!first) + fprintf(outputFile, ", "); + else + fprintf(outputFile, " <= "); + tmp = GEQs[e].coef[v]; + GEQs[e].coef[v] = 0; + printTerm(problemPtr, &GEQs[e], 1); + GEQs[e].coef[v] = tmp; + live[e] = FALSE; + first = 0; + }; + }; + }; + + + for (e = 0; e < nGEQ; e++) + if (live[e]) { + if (stuffPrinted) + fprintf(outputFile, "; "); + stuffPrinted = 1; + printGEQ(&GEQs[e]); + }; + + for (e = 0; e < nSUB; e++) { + Eqn eq = &SUBs[e]; + if (stuffPrinted) + fprintf(outputFile, "; "); + stuffPrinted = 1; + if (eq->key > 0) + fprintf(outputFile, "%s := ", orgVariable(eq->key)); + else + fprintf(outputFile, "#%d := ", eq->key); + printTerm(problemPtr, eq, 1); + }; + }; + fflush(outputFile); +} + + +static void nameWildcard(Problem *problemPtr, int i) +{ + --nextWildCard; + if (nextWildCard < -maxWildCards) + nextWildCard = -1; + problemPtr->_var[i] = nextWildCard; +} + +static int addNewWildcard(Problem *problemPtr) +{ + int e; + int i = ++safeVars; + problemPtr->addToVarsN(1); + //nVars++; + if (nVars != i) { + for (e = nGEQ - 1; e >= 0; e--) { + if (GEQs[e].coef[i] != 0) + GEQs[e].touched = TRUE; + GEQs[e].coef[nVars] = GEQs[e].coef[i]; + + }; + for (e = nEQ - 1; e >= 0; e--) { + EQs[e].coef[nVars] = EQs[e].coef[i]; + }; + for (e = nSUB - 1; e >= 0; e--) { + SUBs[e].coef[nVars] = SUBs[e].coef[i]; + }; + problemPtr->_var[nVars] = problemPtr->_var[i]; + }; + for (e = nGEQ - 1; e >= 0; e--) + GEQs[e].coef[i] = 0; + for (e = nEQ - 1; e >= 0; e--) + EQs[e].coef[i] = 0; + for (e = nSUB - 1; e >= 0; e--) + SUBs[e].coef[i] = 0; + nameWildcard(problemPtr, i); + return (i); +} + +extern void substitute(Problem * problemPtr, Eqn sub, int i, int c); +extern void deleteVariable(Problem *problemPt, int j); + +static void doMod(Problem *problemPtr, int factor, int e, int j) +/* Solve e = factor alpha for x_j and substitute */ +{ + int k, i; + struct _eqn eq; + int nFactor; + int killJ = FALSE; + eqncpy(&eq, &EQs[e]); + for (k = nVars; k >= 0; k--) { + eq.coef[k] = intMod(eq.coef[k], factor); + if (2 * eq.coef[k] >= factor) + eq.coef[k] -= factor; + }; + nFactor = eq.coef[j]; + if (j <= safeVars && problemPtr->_var[j] > 0) { + i = addNewWildcard(problemPtr); + eq.coef[nVars] = eq.coef[i]; + eq.coef[j] = 0; + eq.coef[i] = -factor; + killJ = TRUE; + } + else { + eq.coef[j] = -factor; + if (problemPtr->_var[j] > 0) + nameWildcard(problemPtr, j); + }; + substitute(problemPtr, &eq, j, nFactor); + for (k = nVars; k >= 0; k--) + EQs[e].coef[k] = EQs[e].coef[k] / factor; + if (killJ) + deleteVariable(problemPtr, j); + if (DEBUG) { + fprintf(outputFile, "Mod-ing and normalizing produces:\n"); + printProblem(problemPtr); + }; +} + +void +negateGEQ(Problem * problemPtr, int e) +{ + + int i; + for (i = nVars; i >= 0; i--) + GEQs[e].coef[i] = -GEQs[e].coef[i]; + GEQs[e].coef[0]--; + GEQs[e].touched = TRUE; +} + +static int verifyProblem(Problem *problemPtr) +{ + int result, e, anyColor; + Problem tmpProblem; + /*tmpProblem = mallocProblem; + if (tmpProblem == NULL) + { + printf("ERROR!!\n"); + } + tmpProblem->_init();*/ + + problemcpy(&tmpProblem, problemPtr); + tmpProblem._safeVars = 0; + tmpProblem.setNumSUBs(0); + anyColor = 0; + for (e = nGEQ - 1; e >= 0; e--) + anyColor |= GEQs[e].color; + anyColor |= pleaseNoEqualitiesInSimplifiedProblems; + if (anyColor) { + originalProblem = noProblem; + } + else + originalProblem = problemPtr; + if (DBUG) { + fprintf(outputFile, "verifying problem"); + if (anyColor) + fprintf(outputFile, " (color mode)"); + fprintf(outputFile, " :\n"); + printProblem(problemPtr); + }; + + result = solve(&tmpProblem, UNKNOWN); + originalProblem = noProblem; + //free(tmpProblem); + if (DBUG) { + if (result) + fprintf(outputFile, "verified problem\n"); + else + fprintf(outputFile, "disproved problem\n"); + printProblem(problemPtr); + }; + return result; +} + + +#define implies(A,B) (A==(A&B)) + +void resurrectSubs(Problem * problemPtr); + +int eliminateRedundant(Problem *problemPtr, bool expensive) +{ + int e, e1, e2, e3, p, q, i, k, alpha, alpha1, alpha2, alpha3; + int c; + int isDead[maxGEQs]; + unsigned int P[maxGEQs], Z[maxGEQs], N[maxGEQs]; + unsigned int PP, PZ, PN; /* possible Positives, possible zeros & possible negatives */ + + for (e = nGEQ - 1; e >= 0; e--) { + int tmp = 1; + isDead[e] = 0; + P[e] = Z[e] = N[e] = 0; + for (i = nVars; i >= 1; i--) { + if (GEQs[e].coef[i] > 0) + P[e] |= tmp; + else if (GEQs[e].coef[i] < 0) + N[e] |= tmp; + else + Z[e] |= tmp; + tmp <<= 1; + } + } + + + for (e1 = nGEQ - 1; e1 >= 0; e1--) + if (!isDead[e1]) + for (e2 = e1 - 1; e2 >= 0; e2--) + if (!isDead[e2]) { + for (p = nVars; p > 1; p--) { + for (q = p - 1; q > 0; q--) { + alpha = (GEQs[e1].coef[p] * GEQs[e2].coef[q] - GEQs[e2].coef[p] * GEQs[e1].coef[q]); + if (alpha != 0) + goto foundPQ; + }; + }; + continue; + + foundPQ: + PZ = (Z[e1] & Z[e2]) | (P[e1] & N[e2]) | (N[e1] & P[e2]); + PP = P[e1] | P[e2]; + PN = N[e1] | N[e2]; + + for (e3 = nGEQ - 1; e3 >= 0; e3--) + if (e3 != e1 && e3 != e2) { + + if (!implies(Z[e3], PZ)) + goto nextE3; + + alpha1 = GEQs[e2].coef[q] * GEQs[e3].coef[p] - GEQs[e2].coef[p] * GEQs[e3].coef[q]; + alpha2 = -(GEQs[e1].coef[q] * GEQs[e3].coef[p] - GEQs[e1].coef[p] * GEQs[e3].coef[q]); + alpha3 = alpha; + + if (alpha1 * alpha2 <= 0) + goto nextE3; + if (alpha1 < 0) { + alpha1 = -alpha1; + alpha2 = -alpha2; + alpha3 = -alpha3; + } + if (alpha3 > 0) { + /* Trying to prove e3 is redundant */ + if (!implies(P[e3], PP) | !implies(N[e3], PN)) + goto nextE3; + if (!GEQs[e3].color && (GEQs[e1].color || GEQs[e2].color)) + goto nextE3; + + /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */ + for (k = nVars; k >= 1; k--) + if (alpha3 * GEQs[e3].coef[k] + != alpha1 * GEQs[e1].coef[k] + alpha2 * GEQs[e2].coef[k]) + goto nextE3; + + c = alpha1 * GEQs[e1].coef[0] + alpha2 * GEQs[e2].coef[0]; + if (c < alpha3 * (GEQs[e3].coef[0] + 1)) { + if (DBUG) { + printProblem(problemPtr); + fprintf(outputFile, "found redundant inequality\n"); + fprintf(outputFile, "alpha1, alpha2, alpha3 = %d,%d,%d\n", + alpha1, alpha2, alpha3); + + printGEQ(&(GEQs[e1])); + fprintf(outputFile, "\n"); + printGEQ(&(GEQs[e2])); + fprintf(outputFile, "\n=> "); + printGEQ(&(GEQs[e3])); + fprintf(outputFile, "\n\n"); + }; + + isDead[e3] = 1; + } + } + else { + /* + * trying to prove e3 <= 0 and therefore e3 = 0, or trying to prove e3 < 0, and + * therefore the problem has no solutions + * + */ + if (!implies(P[e3], PN) | !implies(N[e3], PP)) + goto nextE3; + if (GEQs[e1].color || GEQs[e2].color || GEQs[e3].color) + goto nextE3; + + alpha3 = alpha3; + /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */ + for (k = nVars; k >= 1; k--) + if (alpha3 * GEQs[e3].coef[k] + != alpha1 * GEQs[e1].coef[k] + alpha2 * GEQs[e2].coef[k]) + goto nextE3; + + c = alpha1 * GEQs[e1].coef[0] + alpha2 * GEQs[e2].coef[0]; + if (c < alpha3 * (GEQs[e3].coef[0])) { + + /* We just proved e3 < 0, so no solutions exist */ + if (DBUG) { + printProblem(problemPtr); + fprintf(outputFile, "found implied over tight inequality\n"); + fprintf(outputFile, "alpha1, alpha2, alpha3 = %d,%d,%d\n", + alpha1, alpha2, -alpha3); + printGEQ(&(GEQs[e1])); + fprintf(outputFile, "\n"); + printGEQ(&(GEQs[e2])); + fprintf(outputFile, "\n=> not "); + printGEQ(&(GEQs[e3])); + fprintf(outputFile, "\n\n"); + }; + return (0); + + } + else if (c < alpha3 * (GEQs[e3].coef[0] + 1)) { + + /* We just proved that e3 <=0, so e3 = 0 */ + if (DBUG) { + printProblem(problemPtr); + fprintf(outputFile, "found implied tight inequality\n"); + fprintf(outputFile, "alpha1, alpha2, alpha3 = %d,%d,%d\n", + alpha1, alpha2, -alpha3); + printGEQ(&(GEQs[e1])); + fprintf(outputFile, "\n"); + printGEQ(&(GEQs[e2])); + fprintf(outputFile, "\n=> inverse "); + printGEQ(&(GEQs[e3])); + fprintf(outputFile, "\n\n"); + }; + //eqncpy(&EQs[nEQ++], &GEQs[e3]); + const int numEQ = nEQ; + problemPtr->addNumEqs(1); + eqncpy(&EQs[numEQ], &GEQs[e3]); + + + assert(nEQ <= maxEQs); + addingEqualityConstraint(problemPtr, nEQ - 1); + isDead[e3] = 1; + + } + } + nextE3:; + } + } + for (e = nGEQ - 1; e >= 0; e--) + if (isDead[e]) + doDelete(e, nVars); + + /* if (nEQ) return (solve(problemPtr, SIMPLIFY)); */ + + if (!expensive) + return (1); + { + Problem tmpProblem; + int oldTrace = trace; + trace = 0; + /*tmpProblem = mallocProblem; + if (tmpProblem == NULL) + { + printf("ERROR!!\n"); + } + tmpProblem->_init();*/ + + conservative++; + for (e = nGEQ - 1; e >= 0; e--) { + if (DEBUG) { + fprintf(outputFile, "checking equation %d to see if it is redundant: ", e); + printGEQ(&(GEQs[e])); + fprintf(outputFile, "\n"); + }; + problemcpy(&tmpProblem, problemPtr); + negateGEQ(&tmpProblem, e); + tmpProblem._safeVars = 0; + tmpProblem.variablesFreed = 0; + if (!solve(&tmpProblem, FALSE)) + doDelete(e, nVars); + }; + trace = oldTrace; + //free(tmpProblem); + conservative--; + }; +#if reduceWithSubstitutions +#else + resurrectSubs(problemPtr); + assert(nSUB == 0); +#endif + return (1); +} + + +int smoothWeirdEquations(Problem *problemPtr) +{ + int e1, e2, e3, p, q, k, alpha, alpha1, alpha2, alpha3; + int c; + int v; + int result = 0; + + for (e1 = nGEQ - 1; e1 >= 0; e1--) + if (!GEQs[e1].color) { + int g = 999999; + for (v = nVars; v >= 1; v--) + if (GEQs[e1].coef[v] != 0 && abs(GEQs[e1].coef[v]) < g) + g = abs(GEQs[e1].coef[v]); + if (g > 20) { + + e3 = nGEQ; + for (v = nVars; v >= 1; v--) + GEQs[e3].coef[v] = intDiv(6 * GEQs[e1].coef[v] + g / 2, g); + GEQs[e3].color = 0; + GEQs[e3].touched = 1; + GEQs[e3].coef[0] = 9997; + if (DBUG) { + fprintf(outputFile, "Checking to see if we can derive: "); + printGEQ(&GEQs[e3]); + fprintf(outputFile, "\n from: "); + printGEQ(&GEQs[e1]); + fprintf(outputFile, "\n"); + }; + + + for (e2 = nGEQ - 1; e2 >= 0; e2--) + if (e1 != e2 && !GEQs[e2].color) { + for (p = nVars; p > 1; p--) { + for (q = p - 1; q > 0; q--) { + alpha = (GEQs[e1].coef[p] * GEQs[e2].coef[q] - GEQs[e2].coef[p] * GEQs[e1].coef[q]); + if (alpha != 0) + goto foundPQ; + }; + }; + continue; + + foundPQ: + + alpha1 = GEQs[e2].coef[q] * GEQs[e3].coef[p] - GEQs[e2].coef[p] * GEQs[e3].coef[q]; + alpha2 = -(GEQs[e1].coef[q] * GEQs[e3].coef[p] - GEQs[e1].coef[p] * GEQs[e3].coef[q]); + alpha3 = alpha; + + if (alpha1 * alpha2 <= 0) + continue; + if (alpha1 < 0) { + alpha1 = -alpha1; + alpha2 = -alpha2; + alpha3 = -alpha3; + } + if (alpha3 > 0) { + /* Trying to prove e3 is redundant */ + + /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */ + for (k = nVars; k >= 1; k--) + if (alpha3 * GEQs[e3].coef[k] + != alpha1 * GEQs[e1].coef[k] + alpha2 * GEQs[e2].coef[k]) + goto nextE2; + + c = alpha1 * GEQs[e1].coef[0] + alpha2 * GEQs[e2].coef[0]; + if (c < alpha3 * (GEQs[e3].coef[0] + 1)) + GEQs[e3].coef[0] = intDiv(c, alpha3); + + } + nextE2:; + } + if (GEQs[e3].coef[0] < 9997) { + result++; + problemPtr->addNumGEqs(1); + //nGEQ++; + if (DBUG) { + fprintf(outputFile, "Smoothing wierd equations; adding:\n"); + printGEQ(&GEQs[e3]); + fprintf(outputFile, "\nto:\n"); + printProblem(problemPtr); + fprintf(outputFile, "\n\n"); + }; + }; + } + } + return (result); +} + +void +coalesce(Problem * problemPtr) +{ + int e, e2, colors; + int isDead[maxGEQs]; + + colors = 0; + for (e = 0; e < nGEQ; e++) + if (GEQs[e].color) + colors++; + if (colors < 2) + return; + for (e = 0; e < nGEQ; e++) + isDead[e] = 0; + for (e = 0; e < nGEQ; e++) + if (GEQs[e].color) + for (e2 = e + 1; e2 < nGEQ; e2++) + if (GEQs[e].key == -GEQs[e2].key + && GEQs[e].coef[0] == -GEQs[e2].coef[0]) + { + const int numEQ = nEQ; + problemPtr->addNumEqs(1); + eqncpy(&EQs[numEQ], &GEQs[e]); + + assert(nEQ <= maxEQs); + isDead[e] = 1; + isDead[e2] = 1; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (isDead[e]) { + doDelete(e, nVars); + } +} + +void eliminateRed(Problem *problemPtr, bool eliminateAll) +{ + int e, e2, e3, i, j, k, a, alpha1, alpha2; + int c = 0; + int isDead[maxGEQs]; + int deadCount = 0; + + if (DEBUG) { + fprintf(outputFile, "in eliminate RED:\n"); + printProblem(problemPtr); + }; + if (nEQ > 0) { + simplifyProblem(problemPtr); + }; + + for (e = nGEQ - 1; e >= 0; e--) + isDead[e] = 0; + for (e = nGEQ - 1; e >= 0; e--) + if (!GEQs[e].color && !isDead[e]) + for (e2 = e - 1; e2 >= 0; e2--) + if (!GEQs[e2].color && !isDead[e2]) { + a = 0; + for (i = nVars; i > 1; i--) { + for (j = i - 1; j > 0; j--) { + a = (GEQs[e].coef[i] * GEQs[e2].coef[j] - GEQs[e2].coef[i] * GEQs[e].coef[j]); + if (a != 0) + goto foundPair; + }; + }; + continue; + + foundPair: + if (DEBUG) { + fprintf(outputFile, "found two equations to combine, i = %s, ", variable(i)); + fprintf(outputFile, "j = %s, alpha = %d\n", variable(j), a); + printGEQ(&(GEQs[e])); + fprintf(outputFile, "\n"); + printGEQ(&(GEQs[e2])); + fprintf(outputFile, "\n"); + }; + for (e3 = nGEQ - 1; e3 >= 0; e3--) + if (GEQs[e3].color) { + alpha1 = GEQs[e2].coef[j] * GEQs[e3].coef[i] - GEQs[e2].coef[i] * GEQs[e3].coef[j]; + alpha2 = -(GEQs[e].coef[j] * GEQs[e3].coef[i] - GEQs[e].coef[i] * GEQs[e3].coef[j]); + if ((a > 0 && alpha1 > 0 && alpha2 > 0) || (a < 0 && alpha1 < 0 && alpha2 < 0)) { + if (DEBUG) { + fprintf(outputFile, "alpha1 = %d, alpha2 = %d; comparing against: ", alpha1, alpha2); + printGEQ(&(GEQs[e3])); + fprintf(outputFile, "\n"); + }; + for (k = nVars; k >= 0; k--) { + c = alpha1 * GEQs[e].coef[k] + alpha2 * GEQs[e2].coef[k]; + if (c != a * GEQs[e3].coef[k]) + break; + if (DEBUG && k > 0) + fprintf(outputFile, " %s: %d, %d\n", variable(k), c, a * GEQs[e3].coef[k]); + }; + if (k < 0 + || (k == 0 && + ((a > 0 && c < a * GEQs[e3].coef[k]) || (a < 0 && c > a * GEQs[e3].coef[k])))) { + if (DEBUG) { + deadCount++; + fprintf(outputFile, "red equation#%d is dead (%d dead so far, %d remain)\n", + e3, deadCount, nGEQ - deadCount); + printGEQ(&(GEQs[e])); + fprintf(outputFile, "\n"); + printGEQ(&(GEQs[e2])); + fprintf(outputFile, "\n"); + printGEQ(&(GEQs[e3])); + fprintf(outputFile, "\n"); + }; + isDead[e3] = 1; + }; + }; + }; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (isDead[e]) + doDelete(e, nVars); + + + if (DBUG) { + fprintf(outputFile, "in eliminate RED, easy tests done:\n"); + printProblem(problemPtr); + }; + + { + int redFound = 0; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].color) + redFound = 1; + if (!redFound) { + if (DBUG) + fprintf(outputFile, "fast checks worked\n"); +#if reduceWithSubstitutions +#else + assert(pleaseNoEqualitiesInSimplifiedProblems || nSUB == 0); +#endif + return; + }; + } + +#ifndef verifySimplication + if (!verifyProblem(problemPtr)) + return; +#endif + + { + Problem tmpProblem; + int oldTrace = trace; + trace = 0; + conservative++; + /*tmpProblem = mallocProblem; + if (tmpProblem == NULL) + { + printf("ERROR!!\n"); + } + tmpProblem->_init();*/ + + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].color) { + if (DBUG) { + fprintf(outputFile, "checking equation %d to see if it is redundant: ", e); + printGEQ(&(GEQs[e])); + fprintf(outputFile, "\n"); + }; + problemcpy(&tmpProblem, problemPtr); + negateGEQ(&tmpProblem, e); + tmpProblem._safeVars = 0; + tmpProblem.variablesFreed = 0; + tmpProblem.setNumSUBs(0); + if (!solve(&tmpProblem, FALSE)) { + if (DBUG) + fprintf(outputFile, "it is redundant\n"); + doDelete(e, nVars); + } + else { + if (DBUG) + fprintf(outputFile, "it is not redundant\n"); + if (!eliminateAll) { + if (DBUG) + fprintf(outputFile, "no need to check other red equations\n"); + break; + } + }; + }; + trace = oldTrace; + conservative--; + + //free(tmpProblem); + }; + /* simplifyProblem(problemPtr); */ +#if reduceWithSubstitutions +#else + assert(pleaseNoEqualitiesInSimplifiedProblems || nSUB == 0); +#endif +} + + + +void swap(int *i, int *j) +{ + int tmp; + tmp = *i; + *i = *j; + *j = tmp; +} + +void chainUnprotect(Problem *problemPtr) +{ + int i, e; + int unprotect[maxVars]; + for (i = 1; i <= problemPtr->_safeVars; i++) { + unprotect[i] = (problemPtr->_var[i] < 0); + for (e = nSUB - 1; e >= 0; e--) + if (SUBs[e].coef[i]) + unprotect[i] = 0; + }; + if (DBUG) { + fprintf(outputFile, "Doing chain reaction unprotection\n"); + printProblem(problemPtr); + for (i = 1; i <= problemPtr->_safeVars; i++) + if (unprotect[i]) + fprintf(outputFile, "unprotecting %s\n", variable(i)); + }; + for (i = 1; i <= problemPtr->_safeVars; i++) + if (unprotect[i]) { + /* wild card */ + if (i < problemPtr->_safeVars) { + int j = problemPtr->_safeVars; + swap(&problemPtr->_var[i], &problemPtr->_var[j]); + for (e = nGEQ - 1; e >= 0; e--) { + GEQs[e].touched = 1; + swap(&GEQs[e].coef[i], &GEQs[e].coef[j]); + }; + for (e = nEQ - 1; e >= 0; e--) + swap(&EQs[e].coef[i], &EQs[e].coef[j]); + for (e = nSUB - 1; e >= 0; e--) + swap(&SUBs[e].coef[i], &SUBs[e].coef[j]); + swap(&unprotect[i], &unprotect[j]); + i--; + }; + problemPtr->_safeVars--; + }; + if (DBUG) { + fprintf(outputFile, "After chain reactions\n"); + printProblem(problemPtr); + }; +} + +void +resurrectSubs(Problem * problemPtr) +{ + if (problemPtr->getNumSUBs() > 0 && !pleaseNoEqualitiesInSimplifiedProblems) { + int i, e, n, m; + if (DBUG) { + fprintf(outputFile, + "problem reduced, bringing variables back to life\n"); + printProblem(problemPtr); + }; + for (i = 1; i <= problemPtr->_safeVars; i++) + if (problemPtr->_var[i] < 0) { + /* wild card */ + if (i < problemPtr->_safeVars) { + int j = problemPtr->_safeVars; + swap(&problemPtr->_var[i], &problemPtr->_var[j]); + for (e = nGEQ - 1; e >= 0; e--) { + GEQs[e].touched = 1; + swap(&GEQs[e].coef[i], &GEQs[e].coef[j]); + }; + for (e = nEQ - 1; e >= 0; e--) + swap(&EQs[e].coef[i], &EQs[e].coef[j]); + for (e = nSUB - 1; e >= 0; e--) + swap(&SUBs[e].coef[i], &SUBs[e].coef[j]); + i--; + }; + problemPtr->_safeVars--; + }; + + m = problemPtr->getNumSUBs(); + n = max(nVars, problemPtr->_safeVars + m); + for (e = nGEQ - 1; e >= 0; e--) + { + if (singleVarGEQ(GEQs[e], nVars)) + { + i = abs(GEQs[e].key); + if (i >= problemPtr->_safeVars + 1) + GEQs[e].key += (GEQs[e].key > 0 ? m : -m); + } + else + { + GEQs[e].touched = TRUE; + GEQs[e].key = 0; + } + } + + const int tmpNV = nVars; + problemPtr->addToVarsN(m); + + for (i = tmpNV; i >= problemPtr->_safeVars + 1; i--) + { + problemPtr->_var[i + m] = problemPtr->_var[i]; + for (e = nGEQ - 1; e >= 0; e--) + GEQs[e].coef[i + m] = GEQs[e].coef[i]; + for (e = nEQ - 1; e >= 0; e--) + EQs[e].coef[i + m] = EQs[e].coef[i]; + for (e = nSUB - 1; e >= 0; e--) + SUBs[e].coef[i + m] = SUBs[e].coef[i]; + } + + for (i = problemPtr->_safeVars + m; i >= problemPtr->_safeVars + 1; i--) + { + for (e = nGEQ - 1; e >= 0; e--) + GEQs[e].coef[i] = 0; + for (e = nEQ - 1; e >= 0; e--) + EQs[e].coef[i] = 0; + for (e = nSUB - 1; e >= 0; e--) + SUBs[e].coef[i] = 0; + } + //nVars += m; + + for (e = nSUB - 1; e >= 0; e--) + { + problemPtr->_var[problemPtr->_safeVars + 1 + e] = SUBs[e].key; + + const int numEQ = nEQ; + problemPtr->addNumEqs(1); + + eqncpy(&(EQs[numEQ]), &(SUBs[e])); + EQs[numEQ].coef[problemPtr->_safeVars + 1 + e] = -1; + if (DBUG) + { + fprintf(outputFile, "brought back: "); + printEQ(&EQs[numEQ]); + fprintf(outputFile, "\n"); + } + //nEQ++; + assert(nEQ <= maxEQs); + } + problemPtr->_safeVars += m; + //nSUB = 0; + + problemPtr->setNumSUBs(0); + for (i = problemPtr->_safeVars + 1; i <= nVars; i++) { + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].color && GEQs[e].coef[i]) { + /* variable i is in red equations */ + int e2; + for (e2 = nEQ - 1; e2 >= 0; e2--) + if (EQs[e2].coef[i]) + EQs[e2].color = 1; + break; + } + } + + if (DBUG) { + fprintf(outputFile, "variables brought back to life\n"); + printProblem(problemPtr); + }; + } +} + + +static void problemReduced(Problem *problemPtr) +{ +#ifdef verifySimplification + int result; + result = verifyProblem(problemPtr); + if (result && nEQ > 0) + doItAgain = 1; + else if (result) +#endif + { +#ifdef eliminateRedundantConstraints + if (!eliminateRedundant(problemPtr, 1)) + return; +#endif + foundReduction = TRUE; + if (!pleaseNoEqualitiesInSimplifiedProblems) + coalesce(problemPtr); + if (reduceWithSubs || pleaseNoEqualitiesInSimplifiedProblems) + chainUnprotect(problemPtr); + else + resurrectSubs(problemPtr); + +#ifndef singleResult + (*whenReduced) (problemPtr); +#endif + if (omegaPrintResult == 1) { + fprintf(outputFile, "-------------------------------------------\n"); + fprintf(outputFile, "problem reduced:\n"); + printProblem(problemPtr); + fprintf(outputFile, "-------------------------------------------\n"); + }; + + + }; +} + + +static void freeEliminations(Problem *problemPtr, int fv) +/* do free eliminations */ +{ + int tryAgain = 1; + int i, e, e2; + int nV = nVars; + while (tryAgain) { + tryAgain = 0; + for (i = nV; i > fv; i--) { + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i]) + break; + if (e < 0) + e2 = e; + else if (GEQs[e].coef[i] > 0) { + for (e2 = e - 1; e2 >= 0; e2--) + if (GEQs[e2].coef[i] < 0) + break; + } + else { + for (e2 = e - 1; e2 >= 0; e2--) + if (GEQs[e2].coef[i] > 0) + break; + }; + if (e2 < 0) { + int e3; + for (e3 = nSUB - 1; e3 >= 0; e3--) + if (SUBs[e3].coef[i]) + break; + if (e3 >= 0) + continue; + for (e3 = nEQ - 1; e3 >= 0; e3--) + if (EQs[e3].coef[i]) + break; + if (e3 >= 0) + continue; + if (DBUG) + fprintf(outputFile, "a free elimination of %s\n", variable(i)); + if (e >= 0) { + doDelete(e, nV); + for (e--; e >= 0; e--) + if (GEQs[e].coef[i]) + doDelete(e, nV); + tryAgain = (i < nV); + }; + deleteVariable(problemPtr, i); + nV = nVars; + }; + }; + }; + + if (DEBUG) { + fprintf(outputFile, "\nafter free eliminations:\n"); + printProblem(problemPtr); + fprintf(outputFile, "\n"); + }; +} + +static void freeRedEliminations(Problem * problemPtr) +/* do free red eliminations */ +{ + int tryAgain = 1; + int i, e, e2; + int nV = nVars; + int isRedVar[maxVars]; + int isDeadVar[maxVars]; + int isDeadGEQ[maxGEQs]; + for (i = nV; i > 0; i--) { + isRedVar[i] = 0; + isDeadVar[i] = 0; + }; + for (e = nGEQ - 1; e >= 0; e--) { + isDeadGEQ[e] = 0; + if (GEQs[e].color) + for (i = nV; i > 0; i--) + if (GEQs[e].coef[i] != 0) + isRedVar[i] = 1; + }; + + while (tryAgain) { + tryAgain = 0; + for (i = nV; i > 0; i--) + if (!isRedVar[i] && !isDeadVar[i]) { + for (e = nGEQ - 1; e >= 0; e--) + if (!isDeadGEQ[e] && GEQs[e].coef[i]) + break; + if (e < 0) + e2 = e; + else if (GEQs[e].coef[i] > 0) { + for (e2 = e - 1; e2 >= 0; e2--) + if (!isDeadGEQ[e2] && GEQs[e2].coef[i] < 0) + break; + } + else { + for (e2 = e - 1; e2 >= 0; e2--) + if (!isDeadGEQ[e2] && GEQs[e2].coef[i] > 0) + break; + }; + if (e2 < 0) { + int e3; + for (e3 = nSUB - 1; e3 >= 0; e3--) + if (SUBs[e3].coef[i]) + break; + if (e3 >= 0) + continue; + for (e3 = nEQ - 1; e3 >= 0; e3--) + if (EQs[e3].coef[i]) + break; + if (e3 >= 0) + continue; + if (DBUG) + fprintf(outputFile, "a free red elimination of %s\n", variable(i)); + for (; e >= 0; e--) + if (GEQs[e].coef[i]) + isDeadGEQ[e] = 1; + tryAgain = 1; + isDeadVar[i] = 1; + }; + }; + }; + + for (e = nGEQ - 1; e >= 0; e--) + if (isDeadGEQ[e]) + doDelete(e, nV); + + for (i = nV; i > 0; i--) + if (isDeadVar[i]) + deleteVariable(problemPtr, i); + + + if (DEBUG) { + fprintf(outputFile, "\nafter free red eliminations:\n"); + printProblem(problemPtr); + fprintf(outputFile, "\n"); + }; +} + +void addingEqualityConstraint(Problem *problemPtr, int e) +{ + int e2, i, j; + + if (originalProblem != noProblem && originalProblem != problemPtr && !conservative) + { + //e2 = originalProblem->_numEQs++; + e2 = originalProblem->getNumEqs(); + originalProblem->addNumEqs(1); + + if (DBUG) + fprintf(outputFile, + "adding equality constraint %d to outer problem\n", + e2); + eqnnzero(&originalProblem->_EQs[e2], originalProblem->getVarsN()); + for (i = nVars; i >= 1; i--) { + for (j = originalProblem->getVarsN(); j >= 1; j--) + if (originalProblem->_var[j] == problemPtr->_var[i]) + break; + if (j <= 0) { + if (DBUG) + fprintf(outputFile, "retracting\n"); + //originalProblem->_numEQs--; + originalProblem->addNumEqs(-1); + return; + }; + originalProblem->_EQs[e2].coef[j] = EQs[e].coef[i]; + }; + originalProblem->_EQs[e2].coef[0] = EQs[e].coef[0]; + if (DBUG) + printProblem(originalProblem); + }; +} + + + + +void substitute(Problem * problemPtr, Eqn sub, int i, int c) +{ + int e, j, j0, k; + int topVar; + + { + int *p = &packing[0]; + for (k = nVars; k >= 0; k--) + if (sub->coef[k]) + *(p++) = k; + topVar = (p - &packing[0]) - 1; + }; + + + if (DBUG || doTrace) { + fprintf(outputFile, "substituting using %s := ", variable(i)); + printTerm(problemPtr, sub, -c); + fprintf(outputFile, "\n"); + printVars(problemPtr); + }; + + if (topVar < 0) { + for (e = nEQ - 1; e >= 0; e--) + EQs[e].coef[i] = 0; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i] != 0) { + GEQs[e].touched = TRUE; + GEQs[e].coef[i] = 0; + }; + for (e = nSUB - 1; e >= 0; e--) + SUBs[e].coef[i] = 0; + + if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { + //Eqn eqn = &(SUBs[nSUB++]); + problemPtr->addNumSUBs(1); + Eqn eqn = &(SUBs[nSUB - 1]); + + for (k = nVars; k >= 0; k--) + eqn->coef[k] = 0; + eqn->key = problemPtr->_var[i]; + eqn->color = 0; + }; + } + else if (topVar == 0 && packing[0] == 0) { + c = -sub->coef[0] * c; + for (e = nEQ - 1; e >= 0; e--) { + EQs[e].coef[0] += EQs[e].coef[i] * c; + EQs[e].coef[i] = 0; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i] != 0) { + GEQs[e].coef[0] += GEQs[e].coef[i] * c; + GEQs[e].coef[i] = 0; + GEQs[e].touched = TRUE; + }; + for (e = nSUB - 1; e >= 0; e--) { + SUBs[e].coef[0] += SUBs[e].coef[i] * c; + SUBs[e].coef[i] = 0; + }; + if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { + //Eqn eqn = &(SUBs[nSUB++]); + problemPtr->addNumSUBs(1); + Eqn eqn = &(SUBs[nSUB - 1]); + + for (k = nVars; k >= 1; k--) + eqn->coef[k] = 0; + eqn->coef[0] = c; + eqn->key = problemPtr->_var[i]; + eqn->color = 0; + }; + } + else { + for (e = nEQ - 1; e >= 0; e--) { + Eqn eqn = &(EQs[e]); + k = eqn->coef[i]; + if (k != 0) { + k = c * k; + eqn->coef[i] = 0; + for (j = topVar; j >= 0; j--) { + j0 = packing[j]; + eqn->coef[j0] -= sub->coef[j0] * k; + }; + }; + if (DEBUG) { + printEQ(eqn); + fprintf(outputFile, "\n"); + }; + }; + for (e = nGEQ - 1; e >= 0; e--) { + Eqn eqn = &(GEQs[e]); + k = eqn->coef[i]; + if (k != 0) { + k = c * k; + eqn->touched = TRUE; + eqn->coef[i] = 0; + for (j = topVar; j >= 0; j--) { + j0 = packing[j]; + eqn->coef[j0] -= sub->coef[j0] * k; + }; + }; + if (DEBUG) { + printGEQ(eqn); + fprintf(outputFile, "\n"); + }; + }; + for (e = nSUB - 1; e >= 0; e--) { + Eqn eqn = &(SUBs[e]); + k = eqn->coef[i]; + if (k != 0) { + k = c * k; + eqn->coef[i] = 0; + for (j = topVar; j >= 0; j--) { + j0 = packing[j]; + eqn->coef[j0] -= sub->coef[j0] * k; + }; + }; + if (DEBUG) { + fprintf(outputFile, "%s := ", orgVariable(eqn->key)); + printTerm(problemPtr, eqn, 1); + fprintf(outputFile, "\n"); + }; + }; + + if (DEBUG) + fprintf(outputFile, "---\n\n"); + if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { + Eqn eqn; + //eqn = &(SUBs[nSUB++]); + problemPtr->addNumSUBs(1); + eqn = &(SUBs[nSUB - 1]); + + c = -c; + for (k = nVars; k >= 0; k--) + eqn->coef[k] = c * (sub->coef[k]); + eqn->key = problemPtr->_var[i]; + eqn->color = sub->color; + }; + }; + +} + +void substituteRed(Problem *problemPtr, Eqn sub, int i, int c, bool * foundBlack) +{ + int e, j, j0, k; + int topVar; + + { + int *p = &packing[0]; + for (k = nVars; k >= 0; k--) + if (sub->coef[k]) + *(p++) = k; + topVar = (p - &packing[0]) - 1; + }; + + *foundBlack = 0; + + if (DBUG || doTrace) { + if (sub->color) + fprintf(outputFile, "["); + fprintf(outputFile, "substituting using %s := ", variable(i)); + printTerm(problemPtr, sub, -c); + if (sub->color) + fprintf(outputFile, "]"); + fprintf(outputFile, "\n"); + printVars(problemPtr); + }; + + for (e = nEQ - 1; e >= 0; e--) { + Eqn eqn = &(EQs[e]); + k = eqn->coef[i]; + if (k != 0) { + if (!eqn->color) + *foundBlack = 1; + else { + k = c * k; + eqn->coef[i] = 0; + for (j = topVar; j >= 0; j--) { + j0 = packing[j]; + eqn->coef[j0] -= sub->coef[j0] * k; + }; + }; + }; + if (DEBUG) { + printEQ(eqn); + fprintf(outputFile, "\n"); + }; + }; + for (e = nGEQ - 1; e >= 0; e--) { + Eqn eqn = &(GEQs[e]); + k = eqn->coef[i]; + if (k != 0) { + if (!eqn->color) + *foundBlack = 1; + else { + k = c * k; + eqn->touched = TRUE; + eqn->coef[i] = 0; + for (j = topVar; j >= 0; j--) { + j0 = packing[j]; + eqn->coef[j0] -= sub->coef[j0] * k; + }; + }; + }; + if (DEBUG) { + printGEQ(eqn); + fprintf(outputFile, "\n"); + }; + }; + for (e = nSUB - 1; e >= 0; e--) { + Eqn eqn = &(SUBs[e]); + k = eqn->coef[i]; + if (k != 0) { + if (!eqn->color) + *foundBlack = 1; + else { + k = c * k; + eqn->coef[i] = 0; + for (j = topVar; j >= 0; j--) { + j0 = packing[j]; + eqn->coef[j0] -= sub->coef[j0] * k; + }; + }; + }; + if (DEBUG) { + fprintf(outputFile, "%s := ", orgVariable(eqn->key)); + printTerm(problemPtr, eqn, 1); + fprintf(outputFile, "\n"); + }; + }; + + if (DEBUG) + fprintf(outputFile, "---\n\n"); + if (i <= problemPtr->_safeVars && problemPtr->_var[i] >= 0) { + *foundBlack = 1; + }; + +} + + + +void deleteVariable(Problem *problemPtr, int i) +{ + int nV = nVars; + int e; + if (i < safeVars) { + int j = safeVars; + for (e = nGEQ - 1; e >= 0; e--) { + GEQs[e].touched = TRUE; + GEQs[e].coef[i] = GEQs[e].coef[j]; + GEQs[e].coef[j] = GEQs[e].coef[nV]; + }; + for (e = nEQ - 1; e >= 0; e--) { + EQs[e].coef[i] = EQs[e].coef[j]; + EQs[e].coef[j] = EQs[e].coef[nV]; + }; + for (e = nSUB - 1; e >= 0; e--) { + SUBs[e].coef[i] = SUBs[e].coef[j]; + SUBs[e].coef[j] = SUBs[e].coef[nV]; + }; + problemPtr->_var[i] = problemPtr->_var[j]; + problemPtr->_var[j] = problemPtr->_var[nV]; + } + else if (i < nV) { + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[nV]) { + GEQs[e].coef[i] = GEQs[e].coef[nV]; + GEQs[e].touched = TRUE; + }; + for (e = nEQ - 1; e >= 0; e--) + EQs[e].coef[i] = EQs[e].coef[nV]; + for (e = nSUB - 1; e >= 0; e--) + SUBs[e].coef[i] = SUBs[e].coef[nV]; + problemPtr->_var[i] = problemPtr->_var[nV]; + }; + if (i <= safeVars) + safeVars--; + problemPtr->addToVarsN(-1); + //nVars--; +} + +static void convertEQtoGEQs(Problem * problemPtr, int eq) +{ + int i; + if (DBUG) + fprintf(outputFile, "Converting Eq to GEQs\n"); + + int numGE = nGEQ; + problemPtr->addNumGEqs(1); + + eqncpy(&GEQs[numGE], &EQs[eq]); + GEQs[numGE].touched = 1; + //nGEQ++; + + numGE = nGEQ; + problemPtr->addNumGEqs(1); + + eqncpy(&GEQs[numGE], &EQs[eq]); + GEQs[numGE].touched = 1; + for (i = 0; i <= nVars; i++) + GEQs[numGE].coef[i] = -GEQs[numGE].coef[i]; + //nGEQ++; + + if (DBUG) + printProblem(problemPtr); +} + + +static void doElimination(Problem *problemPtr, int e, int i) +{ + struct _eqn sub; + int c; + int nV = nVars; + if (DBUG || doTrace) + fprintf(outputFile, "eliminating variable %s\n", variable(i)); + eqncpy(&sub, &EQs[e]); + c = sub.coef[i]; + sub.coef[i] = 0; + if (c == 1 || c == -1) { + if (EQs[e].color) { + bool fB; + substituteRed(problemPtr, &sub, i, c, &fB); + if (fB) + convertEQtoGEQs(problemPtr, e); + else + deleteVariable(problemPtr, i); + } + else { + substitute(problemPtr, &sub, i, c); + deleteVariable(problemPtr, i); + } + } + else { + int a = abs(c); + if (TRACE) + fprintf(outputFile, "performing non-exact elimination, c = %d\n", c); + assert(inApproximateMode); + for (e = nEQ - 1; e >= 0; e--) + if (EQs[e].coef[i]) { + Eqn eqn = &(EQs[e]); + int j, k; + for (j = nV; j >= 0; j--) + eqn->coef[j] *= a; + k = eqn->coef[i]; + eqn->coef[i] = 0; + for (j = nV; j >= 0; j--) + eqn->coef[j] -= sub.coef[j] * k / c; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i]) { + Eqn eqn = &(GEQs[e]); + int j, k; + for (j = nV; j >= 0; j--) + eqn->coef[j] *= a; + k = eqn->coef[i]; + eqn->coef[i] = 0; + for (j = nV; j >= 0; j--) + eqn->coef[j] -= sub.coef[j] * k / c; + eqn->touched = 1; + }; + for (e = nSUB - 1; e >= 0; e--) + if (SUBs[e].coef[i]) { + Eqn eqn = &(SUBs[e]); + int j, k; + assert(0); + for (j = nV; j >= 0; j--) + eqn->coef[j] *= a; + k = eqn->coef[i]; + eqn->coef[i] = 0; + for (j = nV; j >= 0; j--) + eqn->coef[j] -= sub.coef[j] * k / c; + }; + deleteVariable(problemPtr, i); + }; +} + +static int solveEQ(Problem *problemPtr, int desiredResult) +{ + int i, j, e; + int g, g2; + g = 0; + + + if (DBUG && nEQ > 0) { + fprintf(outputFile, "\nSolveEQ\n"); + printProblem(problemPtr); + fprintf(outputFile, "\n"); + }; + + if (mayBeRed) { + i = 0; + j = nEQ - 1; + while (1) { + struct _eqn eq; + while (i <= j && EQs[i].color) + i++; + while (i <= j && !EQs[j].color) + j--; + if (i >= j) + break; + eqncpy(&eq, &EQs[i]); + eqncpy(&EQs[i], &EQs[j]); + eqncpy(&EQs[j], &eq); + i++; + j--; + }; + }; + + + + /* Eliminate all EQ equations */ + for (e = nEQ - 1; e >= 0; e--) { + Eqn eqn = &(EQs[e]); + int sv; + + if (inApproximateMode && !nGEQ && safeVars == nVars) + return (TRUE); + + if (DEBUG) + fprintf(outputFile, "----\n"); + for (i = nVars; i > 0; i--) + if ((g = eqn->coef[i]) != 0) + break; + /* + * if (isRed(eqn)) { if (DBUG) fprintf(outputFile, "Can't handle Red equality\n"); + * convertEQtoGEQs(problemPtr,eqn); if (DBUG) printProblem(problemPtr); continue; }; + */ + for (j = i - 1; j > 0; j--) + if (eqn->coef[j]) + break; + if (j == 0) { + if (eqn->coef[0] % g != 0) { + if (DBUG) + printEQ(eqn); + if (DBUG) + fprintf(outputFile, "\nequations have no solution \n"); + return (FALSE); + }; + eqn->coef[0] = eqn->coef[0] / g; + eqn->coef[i] = 1; + //nEQ--; + problemPtr->addNumEqs(-1); + doElimination(problemPtr, e, i); + continue; + } + else if (j == -1) { + if (eqn->coef[0] != 0) { + if (DBUG) + printEQ(eqn); + if (DBUG) + fprintf(outputFile, "\nequations have no solution \n"); + return (FALSE); + }; + //nEQ--; + problemPtr->addNumEqs(-1); + continue; + }; + /* i == position of last non-zero coef */ + /* g == coef of i */ + /* j == position of next non-zero coef */ + + if (g < 0) + g = -g; + if (g == 1) { + //nEQ--; + problemPtr->addNumEqs(-1); + doElimination(problemPtr, e, i); + } + else { + int k = j; + bool promotionPossible = + (j <= safeVars && safeVars + 1 == i + && !isRed(eqn) + && !inApproximateMode); + if (DEBUG && promotionPossible) + fprintf(outputFile, " Promotion possible\n"); + normalizeEQ: + if (j > safeVars) { + for (; g != 1 && j > safeVars; j--) + g = gcd(abs(eqn->coef[j]), g); + g2 = g; + } + else if (i > safeVars) + g2 = g; + else + g2 = 0; + for (; g != 1 && j > 0; j--) + g = gcd(abs(eqn->coef[j]), g); + if (g > 1) { + if (eqn->coef[0] % g != 0) { + if (DBUG) + printEQ(eqn); + if (DBUG) + fprintf(outputFile, "\nequations have no solution \n"); + return (FALSE); + }; + + for (j = 0; j <= nVars; j++) + eqn->coef[j] /= g; + g2 = g2 / g; + }; + if (g2 > 1) { + int e2; + for (e2 = e - 1; e2 >= 0; e2--) + if (EQs[e2].coef[i]) + break; + if (e2 == -1) + for (e2 = nGEQ - 1; e2 >= 0; e2--) + if (GEQs[e2].coef[i]) + break; + if (e2 == -1) + for (e2 = nSUB - 1; e2 >= 0; e2--) + if (SUBs[e2].coef[i]) + break; + if (e2 == -1) { + bool change = 0; + if (DBUG) + fprintf(outputFile, "Ha! We own it! \n"); + if (DEBUG) + printEQ(eqn); + if (DEBUG) + fprintf(outputFile, " \n"); + g = eqn->coef[i]; + g = abs(g); + for (j = i - 1; j >= 0; j--) { + int t = eqn->coef[j]; + t = intMod(t, g); + if (2 * t >= g) + t -= g; + if (t != eqn->coef[j]) { + eqn->coef[j] = t; + change = 1; + }; + }; + if (!change) { + if (DBUG) + fprintf(outputFile, "So what?\n"); + } + else { + nameWildcard(problemPtr, i); + if (DEBUG) + printEQ(eqn); + if (DEBUG) + fprintf(outputFile, " \n"); + e++; + continue; + }; + }; + } + if (promotionPossible) { + if (DEBUG) { + fprintf(outputFile, "promoting %s to safety\n", variable(i)); + printVars(problemPtr); + }; + safeVars++; + if (problemPtr->_var[i] > 0) + nameWildcard(problemPtr, i); + promotionPossible = 0; + j = k; + goto normalizeEQ; + }; + + if (g2 > 1 && !inApproximateMode) + { + if (DEBUG) + fprintf(outputFile, "adding equation to handle safe variable \n"); + if (DEBUG) + printEQ(eqn); + if (DEBUG) + fprintf(outputFile, "\n----\n"); + i = addNewWildcard(problemPtr); + //nEQ++; + problemPtr->addNumEqs(1); + + assert(nEQ <= maxEQs); + eqnzero(&EQs[e + 1]); + eqnncpy(&EQs[e + 1], eqn, safeVars); + for (j = nVars; j >= 0; j--) { + EQs[e + 1].coef[j] = intMod(EQs[e + 1].coef[j], g2); + if (2 * EQs[e + 1].coef[j] >= g2) + EQs[e + 1].coef[j] -= g2; + }; + EQs[e + 1].coef[i] = g2; + e += 2; + if (DEBUG) + printProblem(problemPtr); + continue; + }; + + sv = safeVars; + if (g2 == 0) + sv = 0; + + /* find variable to eliminate */ + if (g2 > 1) { + assert(inApproximateMode); + if (TRACE) { + fprintf(outputFile, "non-exact elimination: "); + printEQ(eqn); + fprintf(outputFile, "\n"); + printProblem(problemPtr); + fflush(outputFile); + }; + for (i = nVars; i > sv; i--) + if (EQs[e].coef[i] != 0) + break; + } + else + for (i = nVars; i > sv; i--) + if (EQs[e].coef[i] == 1 || EQs[e].coef[i] == -1) + break; + + if (i > sv) { + //nEQ--; + problemPtr->addNumEqs(-1); + doElimination(problemPtr, e, i); + if (g2 > 1 && TRACE) { + fprintf(outputFile, "result of non-exact elimination:\n"); + printProblem(problemPtr); + fflush(outputFile); + }; + } + else { + int factor = 100000; + j = 0; + if (DEBUG) + fprintf(outputFile, "doing moding\n"); + for (i = nVars; i != sv; i--) + if ((EQs[e].coef[i] & 1) != 0) { + j = i; + i--; + for (; i != sv; i--) + if ((EQs[e].coef[i] & 1) != 0) + break; + break; + }; + if (j != 0 && i == sv) { + doMod(problemPtr, 2, e, j); + e++; + continue; + }; + + j = 0; + for (i = nVars; i != sv; i--) + if (EQs[e].coef[i] != 0 && factor > abs(EQs[e].coef[i]) + 1) { + factor = abs(EQs[e].coef[i]) + 1; + j = i; + }; + if (j == sv) { + fprintf(outputFile, "should not have happened\n"); + Exit(2); + }; + doMod(problemPtr, factor, e, j); + /* go back and try this equation again */ + e++; + }; + }; + + + }; + //nEQ = 0; + problemPtr->setNumEqs(0); + return (UNKNOWN); +} + + +static int solveGEQ(Problem *problemPtr, int desiredResult); +static int solveDepth = 0; + +int solve(Problem *problemPtr, int desiredResult) +{ + int result; + + assert(nVars >= safeVars); + if (desiredResult != SIMPLIFY) + safeVars = 0; + + solveDepth++; + if (solveDepth > 50) { + fprintf(outputFile, "Solve depth = %d, inApprox = %d, aborting\n", + solveDepth, inApproximateMode); + printProblem(problemPtr); + fflush(outputFile); +#ifndef SPEED + debugLevel = 3; +#else + Exit(2); +#endif + if (solveDepth > 60) + Exit(2); + }; + + do { + doItAgain = 0; + if (solveEQ(problemPtr, desiredResult) == FALSE) { + solveDepth--; + return (FALSE); + }; + if (inApproximateMode && !nGEQ) { + result = TRUE; + foundReduction = 1; + break; + } + else + result = solveGEQ(problemPtr, desiredResult); + } while (doItAgain && desiredResult == SIMPLIFY); + solveDepth--; + +#if reduceWithSubstitutions +#else + resurrectSubs(problemPtr); + assert(pleaseNoEqualitiesInSimplifiedProblems || !result || nSUB == 0); +#endif + + return (result); +} + +static int fastLookup[maxKeys * 2]; + +static int solveGEQ(Problem *problemPtr, int desiredResult) +{ + int i, j, k, e; + int nV, fv; + int result; + int coupledSubscripts; + int eliminateAgain; + int smoothed = 0; + j = 0; +solveGEQstart: + while (1) { + if (nGEQ > maxGEQs) { + fprintf(outputFile, "TOO MANY EQUATIONS GENERATED\n"); + Exit(2); + }; + if (DBUG) + fprintf(outputFile, "\nSolveGEQ(%d):\n", desiredResult); + if (DEBUG) + printProblem(problemPtr); + if (DBUG) + fprintf(outputFile, "\n"); + + nV = nVars; + + if (nV == 1) { + int uColor = black; + int lColor = black; + int upperBound = posInfinity; + int lowerBound = negInfinity; + for (e = nGEQ - 1; e >= 0; e--) { + int a = GEQs[e].coef[1]; + int c = GEQs[e].coef[0]; + /* our equation is ax + c >= 0, or ax >= -c, or c >= -ax */ + if (a == 0) { + if (c < 0) { + if (DBUG) + fprintf(outputFile, "equations have no solution \n"); + return (FALSE); + }; + } + else if (a > 0) { + if (a != 1) + c = intDiv(c, a); + if (lowerBound < -c || + (lowerBound == -c && !isRed(&GEQs[e]))) { + lowerBound = -c; + lColor = GEQs[e].color; + } + } + else { + if (a != -1) + c = intDiv(c, -a); + if (upperBound > c || + (upperBound == c && !isRed(&GEQs[e]))) { + upperBound = c; + uColor = GEQs[e].color; + } + }; + }; + if (DEBUG) + fprintf(outputFile, "upper bound = %d\n", upperBound); + if (DEBUG) + fprintf(outputFile, "lower bound = %d\n", lowerBound); + if (lowerBound > upperBound) { + if (DBUG) + fprintf(outputFile, "equations have no solution \n"); + return (FALSE); + }; + if (desiredResult == SIMPLIFY) { + //nGEQ = 0; + problemPtr->setNumGEqs(0); + if (safeVars == 1) { + + if (lowerBound == upperBound && !uColor && !lColor) { + EQs[0].coef[0] = -lowerBound; + EQs[0].coef[1] = 1; + EQs[0].color = 0; + //nEQ = 1; + problemPtr->setNumEqs(1); + return (solve(problemPtr, desiredResult)); + } + else { + if (lowerBound > negInfinity) { + GEQs[0].coef[0] = -lowerBound; + GEQs[0].coef[1] = 1; + GEQs[0].key = 1; + GEQs[0].color = lColor; + GEQs[0].touched = 0; + //nGEQ = 1; + problemPtr->setNumGEqs(1); + } + + if (upperBound < posInfinity) + { + const int numGE = nGEQ; + problemPtr->addNumGEqs(1); + + GEQs[numGE].coef[0] = upperBound; + GEQs[numGE].coef[1] = -1; + GEQs[numGE].key = -1; + GEQs[numGE].color = uColor; + GEQs[numGE].touched = 0; + //nGEQ++; + } + } + } + else + problemPtr->setVarsN(0); + problemReduced(problemPtr); + return (FALSE); + }; + if (originalProblem != noProblem && !lColor && !uColor && !conservative && lowerBound == upperBound) { + EQs[0].coef[0] = -lowerBound; + EQs[0].coef[1] = 1; + //nEQ = 1; + problemPtr->setNumEqs(1); + addingEqualityConstraint(problemPtr, 0); + }; + return (TRUE); + }; + + if (!problemPtr->variablesFreed) { + problemPtr->variablesFreed = 1; + if (desiredResult != SIMPLIFY) + freeEliminations(problemPtr, 0); + else + freeEliminations(problemPtr, safeVars); + nV = nVars; + if (nV == 1) + continue; + }; + + + coupledSubscripts = 0; + for (e = 0; e < nGEQ; e++) { + if (!GEQs[e].touched) { + if (!singleVarGEQ(GEQs[e], nV)) + coupledSubscripts = 1; + } + else { + int g; + int topVar; + int i0; + int hashCode; + + + { + int *p = &packing[0]; + for (k = 1; k <= nV; k++) + if (GEQs[e].coef[k]) { + *(p++) = k; + }; + topVar = (p - &packing[0]) - 1; + }; + + if (topVar == -1) { + if (GEQs[e].coef[0] < 0) { + if (DBUG) + printGEQ(&GEQs[e]); + if (DBUG) + fprintf(outputFile, "\nequations have no solution \n"); + return (FALSE); + }; + doDelete(e, nV); + e--; + continue; + } + else if (topVar == 0) { + int singleVar = packing[0]; + g = GEQs[e].coef[singleVar]; + if (g > 0) { + GEQs[e].coef[singleVar] = 1; + GEQs[e].key = singleVar; + } + else { + g = -g; + GEQs[e].coef[singleVar] = -1; + GEQs[e].key = -singleVar; + }; + if (g > 1) + GEQs[e].coef[0] = intDiv(GEQs[e].coef[0], g); + } + else { + + coupledSubscripts = 1; + i0 = topVar; + i = packing[i0--]; + g = GEQs[e].coef[i]; + hashCode = g * (i + 3); + if (g < 0) + g = -g; + for (; i0 >= 0; i0--) { + int x; + i = packing[i0]; + x = GEQs[e].coef[i]; + hashCode = hashCode * keyMult * (i + 3) + x; + if (x < 0) + x = -x; + if (x == 1) { + g = 1; + i0--; + break; + } + else + g = gcd(x, g); + }; + for (; i0 >= 0; i0--) { + int x; + i = packing[i0]; + x = GEQs[e].coef[i]; + hashCode = hashCode * keyMult * (i + 3) + x; + }; + if (g > 1) { + GEQs[e].coef[0] = intDiv(GEQs[e].coef[0], g); + i0 = topVar; + i = packing[i0--]; + GEQs[e].coef[i] = GEQs[e].coef[i] / g; + hashCode = GEQs[e].coef[i] * (i + 3); + for (; i0 >= 0; i0--) { + i = packing[i0]; + GEQs[e].coef[i] = GEQs[e].coef[i] / g; + hashCode = hashCode * keyMult * (i + 3) + GEQs[e].coef[i]; + }; + }; + + { + int g2 = abs(hashCode); + if (DEBUG) { + fprintf(outputFile, "Hash code = %d, eqn = ", hashCode); + printGEQ(&GEQs[e]); + fprintf(outputFile, "\n"); + }; + j = g2 % hashTableSize; + while (1) { + Eqn proto = &(hashMaster[j]); + if (proto->touched == g2) { + if (proto->coef[0] == topVar) { + if (hashCode >= 0) + for (i0 = topVar; i0 >= 0; i0--) { + i = packing[i0]; + if (GEQs[e].coef[i] != proto->coef[i]) + break; + } + else + for (i0 = topVar; i0 >= 0; i0--) { + i = packing[i0]; + if (GEQs[e].coef[i] != -proto->coef[i]) + break; + }; + if (i0 < 0) { + if (hashCode >= 0) + GEQs[e].key = proto->key; + else + GEQs[e].key = -proto->key; + break; + }; + }; + } + else if (proto->touched < 0) { + eqnzero(proto); + if (hashCode >= 0) + for (i0 = topVar; i0 >= 0; i0--) { + i = packing[i0]; + proto->coef[i] = GEQs[e].coef[i]; + } + else + for (i0 = topVar; i0 >= 0; i0--) { + i = packing[i0]; + proto->coef[i] = -GEQs[e].coef[i]; + } + proto->coef[0] = topVar; + proto->touched = g2; + if (DEBUG) + fprintf(outputFile, " constraint key = %d\n", nextKey); + proto->key = nextKey++; + if (proto->key > maxKeys) { + fprintf(outputFile, "too many hash keys generated \n"); + fflush(outputFile); + Exit(2); + }; + if (hashCode >= 0) + GEQs[e].key = proto->key; + else + GEQs[e].key = -proto->key; + break; + }; + j = (j + 1) % hashTableSize; + }; + }; + }; + + GEQs[e].touched = FALSE; + }; + + { + int eKey = GEQs[e].key; + int e2; + if (e > 0) { + int cTerm = GEQs[e].coef[0]; + e2 = fastLookup[maxKeys - eKey]; + if (e2 < e && GEQs[e2].key == -eKey) { + if (GEQs[e2].coef[0] < -cTerm) { + if (DBUG) { + printGEQ(&GEQs[e]); + fprintf(outputFile, "\n"); + printGEQ(&GEQs[e2]); + fprintf(outputFile, "\nequations have no solution \n"); + }; + return (FALSE); + }; + if (GEQs[e2].coef[0] == -cTerm && !GEQs[e2].color && !GEQs[e].color) + { + /* + * if (!addIt && 0) { int i; for (i = safeVars + 1; i <= nVars; i++) if (GEQs[e].coef[i] == + * 1 || GEQs[e].coef[i] == -1) addIt = 1; }; + */ + const int numEQ = nEQ; + problemPtr->addNumEqs(1); + + eqncpy(&EQs[numEQ], &GEQs[e]); + if (!GEQs[e2].color) + addingEqualityConstraint(problemPtr, numEQ); + assert(!GEQs[e2].color); + assert(!GEQs[e].color); + //nEQ++; + + assert(nEQ <= maxEQs); + } + } + + e2 = fastLookup[maxKeys + eKey]; + if (e2 < e && GEQs[e2].key == eKey) { + if (GEQs[e2].coef[0] > cTerm) { + GEQs[e2].coef[0] = cTerm; + GEQs[e2].color = GEQs[e].color; + } + else if (GEQs[e2].coef[0] == cTerm) + GEQs[e2].color &= GEQs[e].color; + doDelete(e, nV); + e--; + continue; + }; + }; + fastLookup[maxKeys + eKey] = e; + }; + }; + + nV = nVars; + + if ((doTrace && desiredResult == SIMPLIFY) || DBUG) { + fprintf(outputFile, "\nafter normalization:\n"); + printProblem(problemPtr); + fprintf(outputFile, "\n"); + fprintf(outputFile, "eliminating variable using fourier-motzkin elimination\n"); + }; + + do { + eliminateAgain = 0; + + if (nEQ > 0) + return (solve(problemPtr, desiredResult)); + + if (!coupledSubscripts) { + if (safeVars == 0) + { + //nGEQ = 0; + problemPtr->setNumGEqs(0); + } + else + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].key > safeVars || -safeVars > GEQs[e].key) + doDelete(e, nV); + problemPtr->setVarsN(safeVars); + //nVars = safeVars; + if (desiredResult == SIMPLIFY) { + problemReduced(problemPtr); + return (FALSE); + }; + return (TRUE); + }; + + if (desiredResult != SIMPLIFY) + fv = 0; + else + fv = safeVars; + + if (nGEQ == 0) { + if (desiredResult == SIMPLIFY) { + problemPtr->setVarsN(safeVars); + //nVars = safeVars; + problemReduced(problemPtr); + return (FALSE); + }; + return (TRUE); + }; + if (desiredResult == SIMPLIFY && nV == safeVars) { + problemReduced(problemPtr); + return (FALSE); + }; + + + if (nGEQ > maxGEQs - 30 || nGEQ > 2 * nV * nV + 4 * nV + 10) { + if (DBUG) + fprintf(outputFile, "TOO MANY EQUATIONS; %d equations, %d variables, ELIMINATING REDUNDANT ONES\n", nGEQ, nV); + if (!eliminateRedundant(problemPtr, 0)) + return 0; + nV = nVars; + if (nEQ > 0) + return (solve(problemPtr, desiredResult)); + if (DBUG) + fprintf(outputFile, "END ELIMINATION OF REDUNDANT EQUATIONS\n"); + }; + + + + { + int minC, maxC, minCj; + int lowerBoundCount; + int e2, Le, Ue; + int possibleEasyIntSolution; + int exact = 0; + int luckyExact = 0; + int newEqns = 0; + minCj = 0; + Le = 0; + lowerBoundCount = 0; + + if (desiredResult != SIMPLIFY) + fv = 0; + else + fv = safeVars; + + { + int best = 1000000; + int jLe, jLowerBoundCount, upperBoundCount; + + jLe = 0; + jLowerBoundCount = 0; + + + for (i = nV; i != fv; i--) { + int score; + int ub = -2; + int lb = -2; + int lucky = 0; + minC = maxC = 0; + lowerBoundCount = 0; + upperBoundCount = 0; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i] < 0) { + setMin(minC, GEQs[e].coef[i]); + upperBoundCount++; + if (GEQs[e].coef[i] < -1) { + if (ub == -2) + ub = e; + else + ub = -1; + }; + } + else if (GEQs[e].coef[i] > 0) { + setMax(maxC, GEQs[e].coef[i]); + lowerBoundCount++; + Le = e; + if (GEQs[e].coef[i] > 1) { + if (lb == -2) + lb = e; + else + lb = -1; + }; + }; + if (lowerBoundCount == 0 || upperBoundCount == 0) { + lowerBoundCount = 0; + break; + }; + if (ub >= 0 && lb >= 0 && GEQs[lb].key == -GEQs[ub].key) { + int Lc = GEQs[lb].coef[i]; + int Uc = -GEQs[ub].coef[i]; + int diff = Lc * GEQs[ub].coef[0] + Uc * GEQs[lb].coef[0]; + lucky = (diff >= (Uc - 1) * (Lc - 1)); + }; + if (maxC == 1 || minC == -1 || lucky || APROX || inApproximateMode) { + newEqns = score = upperBoundCount * lowerBoundCount; + if (DEBUG) + fprintf(outputFile, + "For %s, exact, score = %d*%d, range = %d ... %d, \nlucky = %d, APROX = %d, inApproximateMode=%d \n", + variable(i), upperBoundCount, lowerBoundCount, + minC, maxC, lucky, APROX, inApproximateMode); + if (!exact || score < best) { + + best = score; + j = i; + minCj = minC; + jLe = Le; + jLowerBoundCount = lowerBoundCount; + exact = 1; + luckyExact = lucky; + if (score == 1) + break; + }; + } + else if (!exact) { + if (DEBUG) + fprintf(outputFile, + "For %s, non-exact, score = %d*%d, range = %d ... %d \n", + variable(i), upperBoundCount, lowerBoundCount, + minC, maxC); + newEqns = upperBoundCount * lowerBoundCount; + score = maxC - minC; + if (best > score) { + best = score; + j = i; + minCj = minC; + jLe = Le; + jLowerBoundCount = lowerBoundCount; + }; + }; + }; + if (lowerBoundCount == 0) { + freeEliminations(problemPtr, safeVars); + nV = nVars; + continue; + }; + i = j; + minC = minCj; + Le = jLe; + lowerBoundCount = jLowerBoundCount; + }; + +#ifdef singleResult + if (desiredResult == SIMPLIFY && !exact) { + nonConvex = 1; + problemReduced(problemPtr); + return (TRUE); + }; +#endif + + if ((doTrace && desiredResult == SIMPLIFY) || DBUG) { + fprintf(outputFile, "going to eliminate %s\n", variable(i)); + if (luckyExact) + fprintf(outputFile, "(a lucky exact elimination)\n"); + else if (exact) + fprintf(outputFile, "(an exact elimination)\n"); + }; + + + smoothed = 0; + + if (i != nV) { + int t; + j = nVars; + if (DEBUG) { + fprintf(outputFile, "Swapping %d and %d\n", i, j); + printProblem(problemPtr); + }; + swap(&problemPtr->_var[i], &problemPtr->_var[j]); + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i] != GEQs[e].coef[j]) { + GEQs[e].touched = TRUE; + t = GEQs[e].coef[i]; + GEQs[e].coef[i] = GEQs[e].coef[j]; + GEQs[e].coef[j] = t; + }; + for (e = nSUB - 1; e >= 0; e--) + if (SUBs[e].coef[i] != SUBs[e].coef[j]) { + t = SUBs[e].coef[i]; + SUBs[e].coef[i] = SUBs[e].coef[j]; + SUBs[e].coef[j] = t; + }; + if (DEBUG) { + fprintf(outputFile, "Swapping complete \n"); + printProblem(problemPtr); + fprintf(outputFile, "\n"); + }; + + i = j; + } + else if (DEBUG) { + fprintf(outputFile, "No swap needed\n"); + printProblem(problemPtr); + }; + problemPtr->addToVarsN(-1); + //nVars--; + nV = nVars; + + if (exact) { +#define maxDead maxGEQs + if (nV == 1) { + int upperBound = posInfinity; + int lowerBound = negInfinity; + int ub_color = -1; + int lb_color = -1; + int constantTerm, coefficient; + int topEqn = nGEQ - 1; + int Lc = GEQs[Le].coef[i]; + for (Le = topEqn; Le >= 0; Le--) + if ((Lc = GEQs[Le].coef[i]) == 0) { + if (GEQs[Le].coef[1] == 1) { + constantTerm = -GEQs[Le].coef[0]; + if (constantTerm > lowerBound || + (constantTerm == lowerBound && + !isRed(&GEQs[Le]))) { + lowerBound = constantTerm; + lb_color = GEQs[Le].color; + } + if (DEBUG) { + if (GEQs[Le].color == black) + fprintf(outputFile, " :::=> %s >= %d\n", + variable(1), constantTerm); + else + fprintf(outputFile, " :::=> [%s >= %d]\n", + variable(1), constantTerm); + } + } + else { + constantTerm = GEQs[Le].coef[0]; + if (constantTerm < upperBound || + (constantTerm == upperBound + && !isRed(&GEQs[Le]))) { + upperBound = constantTerm; + ub_color = GEQs[Le].color; + } + if (DEBUG) { + if (GEQs[Le].color == black) + fprintf(outputFile, " :::=> %s <= %d\n", + variable(1), constantTerm); + else + fprintf(outputFile, " :::=> [%s <= %d]\n", + variable(1), constantTerm); + } + }; + } + else if (Lc > 0) { + for (Ue = topEqn; Ue >= 0; Ue--) + if (GEQs[Ue].coef[i] < 0) { + if (GEQs[Le].key != -GEQs[Ue].key) { + int Uc = -GEQs[Ue].coef[i]; + coefficient = GEQs[Ue].coef[1] * Lc + GEQs[Le].coef[1] * Uc; + constantTerm = GEQs[Ue].coef[0] * Lc + GEQs[Le].coef[0] * Uc; + if (DEBUG) { + printGEQextra(&(GEQs[Ue])); + fprintf(outputFile, "\n"); + printGEQextra(&(GEQs[Le])); + fprintf(outputFile, "\n"); + }; + if (coefficient > 0) { + constantTerm = -(intDiv(constantTerm, coefficient)); + /* assert(black == 0) */ + if (constantTerm > lowerBound || + (constantTerm == lowerBound && + (desiredResult != SIMPLIFY || + (GEQs[Ue].color == black && GEQs[Le].color == black)))) { + lowerBound = constantTerm; + lb_color = GEQs[Ue].color || GEQs[Le].color; + } + if (DEBUG) { + if (GEQs[Ue].color || GEQs[Le].color) + fprintf(outputFile, " ::=> [%s >= %d]\n", + variable(1), constantTerm); + else + fprintf(outputFile, " ::=> %s >= %d\n", + variable(1), constantTerm); + } + } + else { + constantTerm = (intDiv(constantTerm, -coefficient)); + if (constantTerm < upperBound || + (constantTerm == upperBound && + GEQs[Ue].color == black && GEQs[Le].color == black)) { + upperBound = constantTerm; + ub_color = GEQs[Ue].color || GEQs[Le].color; + } + if (DEBUG) { + if (GEQs[Ue].color || GEQs[Le].color) + fprintf(outputFile, " ::=> [%s <= %d]\n", + variable(1), constantTerm); + else + fprintf(outputFile, " ::=> %s <= %d\n", + variable(1), constantTerm); + } + } + }; + }; + }; + //nGEQ = 0; + problemPtr->setNumGEqs(0); + if (DEBUG) + fprintf(outputFile, + " therefore, %c%d <= %c%s%c <= %d%c\n", + lb_color ? '[' : ' ', lowerBound, + (lb_color && !ub_color) ? ']' : ' ', + variable(1), + (!lb_color && ub_color) ? '[' : ' ', + upperBound, ub_color ? ']' : ' '); + if (lowerBound > upperBound) + return (FALSE); + if (safeVars == 1) { + if (upperBound == lowerBound + && !(ub_color | lb_color) + && !pleaseNoEqualitiesInSimplifiedProblems) { + //nEQ++; + problemPtr->addNumEqs(1); + EQs[0].coef[1] = -1; + EQs[0].coef[0] = upperBound; + EQs[0].color = ub_color | lb_color; + if (desiredResult == SIMPLIFY && + !EQs[0].color) + return (solve(problemPtr, desiredResult)); + }; + if (upperBound != posInfinity) { + GEQs[0].coef[1] = -1; + GEQs[0].coef[0] = upperBound; + GEQs[0].color = ub_color; + GEQs[0].key = -1; + GEQs[0].touched = 0; + //nGEQ++; + problemPtr->addNumGEqs(1); + } + + if (lowerBound != negInfinity) + { + const int numGE = nGEQ; + problemPtr->addNumGEqs(1); + + GEQs[numGE].coef[1] = 1; + GEQs[numGE].coef[0] = -lowerBound; + GEQs[numGE].color = lb_color; + GEQs[numGE].key = 1; + GEQs[numGE].touched = 0; + //nGEQ++; + } + } + + if (desiredResult == SIMPLIFY) { + problemReduced(problemPtr); + return (FALSE); + } + else { + if (!conservative && + (desiredResult != SIMPLIFY || + (!lb_color && !ub_color)) + && originalProblem != noProblem && lowerBound == upperBound) { + for (i = originalProblem->getVarsN(); i >= 0; i--) + if (originalProblem->_var[i] == problemPtr->_var[1]) + break; + if (i == 0) + break; + //e = originalProblem->_numEQs++; + e = originalProblem->getNumEqs(); + problemPtr->addNumEqs(1); + + eqnnzero(&originalProblem->_EQs[e], originalProblem->getVarsN()); + originalProblem->_EQs[e].coef[i] = -1; + originalProblem->_EQs[e].coef[0] = upperBound; + if (DEBUG) { + fprintf(outputFile, "adding equality constraint %d to outer problem\n", + e); + printProblem(originalProblem); + }; + }; + return (TRUE); + }; + }; + eliminateAgain = 1; + + if (lowerBoundCount == 1) { + struct _eqn lbEqn; + int Lc = GEQs[Le].coef[i]; + if (DBUG) + fprintf(outputFile, "an inplace elimination\n"); + + eqnncpy(&lbEqn, &GEQs[Le], (nV + 1)); + doDeleteExtra(Le, nV + 1); + for (Ue = nGEQ - 1; Ue >= 0; Ue--) + if (GEQs[Ue].coef[i] < 0) + if (lbEqn.key == -GEQs[Ue].key) { + doDeleteExtra(Ue, nV + 1); + } + else { + int Uc = -GEQs[Ue].coef[i]; + GEQs[Ue].touched = TRUE; + GEQs[Ue].color |= lbEqn.color; + eliminateAgain = 0; + for (k = 0; k <= nV; k++) + GEQs[Ue].coef[k] = GEQs[Ue].coef[k] * Lc + lbEqn.coef[k] * Uc; + if (DEBUG) { + printGEQ(&(GEQs[Ue])); + fprintf(outputFile, "\n"); + }; + }; + continue; + } + else { + int deadEqns[maxDead]; + int numDead = 0; + int topEqn = nGEQ - 1; + lowerBoundCount--; + if (DEBUG) + fprintf(outputFile, "lower bound count = %d\n", lowerBoundCount); + for (Le = topEqn; Le >= 0; Le--) + if (GEQs[Le].coef[i] > 0) { + int Lc = GEQs[Le].coef[i]; + for (Ue = topEqn; Ue >= 0; Ue--) + if (GEQs[Ue].coef[i] < 0) { + if (GEQs[Le].key != -GEQs[Ue].key) { + int Uc = -GEQs[Ue].coef[i]; + if (numDead == 0) + { + //e2 = nGEQ++; + e2 = nGEQ; + problemPtr->addNumGEqs(1); + } + else + e2 = deadEqns[--numDead]; + assert(e2 < maxGEQs); + if (DEBUG) + fprintf(outputFile, "Le = %d, Ue = %d, gen = %d\n", Le, Ue, e2); + if (DEBUG) { + printGEQextra(&(GEQs[Le])); + fprintf(outputFile, "\n"); + printGEQextra(&(GEQs[Ue])); + fprintf(outputFile, "\n"); + }; + eliminateAgain = 0; + + for (k = nV; k >= 0; k--) + GEQs[e2].coef[k] = GEQs[Ue].coef[k] * Lc + GEQs[Le].coef[k] * Uc; + GEQs[e2].coef[nV + 1] = 0; + GEQs[e2].touched = TRUE; + GEQs[e2].color = GEQs[Ue].color | GEQs[Le].color; + + if (DEBUG) { + printGEQ(&(GEQs[e2])); + fprintf(outputFile, "\n"); + }; + }; + if (lowerBoundCount == 0) { + deadEqns[numDead++] = Ue; + if (DEBUG) + fprintf(outputFile, "Killed %d\n", Ue); + }; + }; + lowerBoundCount--; + deadEqns[numDead++] = Le; + if (DEBUG) + fprintf(outputFile, "Killed %d\n", Le); + }; + { + int isDead[maxGEQs]; + for (e = nGEQ - 1; e >= 0; e--) + isDead[e] = FALSE; + while (numDead > 0) { + e = deadEqns[--numDead]; + isDead[e] = TRUE; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (isDead[e]) + doDeleteExtra(e, nV + 1); + }; + continue; + }; + } + else { + Problem *rS, *iS; + + rS = mallocProblem; +#ifdef _WIN32 + addToCollection(__LINE__, __FILE__, rS, 1); +#endif + initializeProblem(rS); + iS = mallocProblem; +#ifdef _WIN32 + addToCollection(__LINE__, __FILE__, iS, 1); +#endif + initializeProblem(iS); + rS->_getVarName = problemPtr->_getVarName; + rS->_getVarNameArgs = problemPtr->_getVarNameArgs; + iS->_getVarName = problemPtr->_getVarName; + iS->_getVarNameArgs = problemPtr->_getVarNameArgs; + + e2 = 0; + possibleEasyIntSolution = TRUE; + for (e = 0; e < nGEQ; e++) + if (GEQs[e].coef[i] == 0) { + eqncpy(&(rS->_GEQs[e2]), &GEQs[e]); + eqncpy(&(iS->_GEQs[e2]), &GEQs[e]); + if (DEBUG) { + int t; + fprintf(outputFile, "Copying (%d, %d): ", i, GEQs[e].coef[i]); + printGEQextra(&GEQs[e]); + fprintf(outputFile, "\n"); + for (t = 0; t <= nV + 1; t++) + fprintf(outputFile, "%d ", GEQs[e].coef[t]); + fprintf(outputFile, "\n"); + + }; + e2++; + assert(e2 < maxGEQs); + }; + for (Le = nGEQ - 1; Le >= 0; Le--) + if (GEQs[Le].coef[i] > 0) { + int Lc = GEQs[Le].coef[i]; + for (Ue = nGEQ - 1; Ue >= 0; Ue--) + if (GEQs[Ue].coef[i] < 0) + if (GEQs[Le].key != -GEQs[Ue].key) { + int Uc = -GEQs[Ue].coef[i]; + rS->_GEQs[e2].touched = iS->_GEQs[e2].touched = TRUE; + if (DEBUG) { + fprintf(outputFile, "---\n"); + fprintf(outputFile, "Le = %d, Ue = %d, gen = %d\n", Le, Ue, e2); + printGEQextra(&GEQs[Le]); + fprintf(outputFile, "\n"); + printGEQextra(&GEQs[Ue]); + fprintf(outputFile, "\n"); + }; + + for (k = nV; k >= 0; k--) + iS->_GEQs[e2].coef[k] = rS->_GEQs[e2].coef[k] = + GEQs[Ue].coef[k] * Lc + GEQs[Le].coef[k] * Uc; + iS->_GEQs[e2].coef[0] -= (Uc - 1) * (Lc - 1); + iS->_GEQs[e2].color = rS->_GEQs[e2].color + = GEQs[Ue].color || GEQs[Le].color; + + if (DEBUG) { + printGEQ(&(rS->_GEQs[e2])); + fprintf(outputFile, "\n"); + }; + e2++; + assert(e2 < maxGEQs); + } + else { + int Uc = -GEQs[Ue].coef[i]; + if (GEQs[Ue].coef[0] * Lc + GEQs[Le].coef[0] * Uc - (Uc - 1) * (Lc - 1) < 0) + possibleEasyIntSolution = FALSE; + }; + + }; + iS->variablesInitialized = rS->variablesInitialized = 1; + iS->setVarsN(nVars); + rS->setVarsN(nVars); + //iS->_numGEQs = rS->_numGEQs = e2; + //iS->_numEQs = rS->_numEQs = 0; + //iS->_numSUBs = rS->_numSUBs = nSUB; + iS->setNumGEqs(e2); + rS->setNumGEqs(e2); + + iS->setNumEqs(0); + rS->setNumEqs(0); + + iS->setNumSUBs(nSUB); + rS->setNumSUBs(nSUB); + + iS->_safeVars = rS->_safeVars = safeVars; + { + int t; + for (t = nV; t >= 0; t--) + rS->_var[t] = problemPtr->_var[t]; + for (t = nV; t >= 0; t--) + iS->_var[t] = problemPtr->_var[t]; + for (e = nSUB - 1; e >= 0; e--) { + eqnncpy(&(rS->_SUBs[e]), &(SUBs[e]), nVars); + eqnncpy(&(iS->_SUBs[e]), &(SUBs[e]), nVars); + }; + }; + problemPtr->addToVarsN(1); + //nVars++; + nV = nVars; + if (desiredResult != TRUE) { + int t = trace; + if (DBUG) + fprintf(outputFile, "\nreal solution(%d):\n", depth); + depth++; + trace = 0; + if (originalProblem == noProblem) { + originalProblem = problemPtr; + result = solveGEQ(rS, FALSE); + originalProblem = noProblem; + } + else + result = solveGEQ(rS, FALSE); + trace = t; + depth--; + if (result == FALSE) { +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + free(rS); + free(iS); + return (result); + }; + + if (nEQ > 0) { +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + /* An equality constraint must have been found */ + free(rS); + free(iS); + return (solve(problemPtr, desiredResult)); + }; + }; + if (desiredResult != FALSE) { + if (possibleEasyIntSolution) { + if (DBUG) + fprintf(outputFile, "\ninteger solution(%d):\n", depth); + depth++; + conservative++; + result = solveGEQ(iS, desiredResult); + conservative--; + depth--; + if (result != FALSE) { +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + free(rS); + free(iS); + return (result); + }; + }; + if (DBUG) + fprintf(outputFile, "have to do exact analysis\n"); + { + int worstLowerBoundConstant = -minC; + int lowerBounds = 0; + int lowerBound[maxGEQs]; + int smallest; + int t; + conservative++; + for (e = 0; e < nGEQ; e++) + if (GEQs[e].coef[i] > 1) + lowerBound[lowerBounds++] = e; + /* sort array */ + for (j = 0; j < lowerBounds; j++) { + smallest = j; + for (k = j + 1; k < lowerBounds; k++) + if (GEQs[lowerBound[smallest]].coef[i] > GEQs[lowerBound[k]].coef[i]) + smallest = k; + t = lowerBound[smallest]; + lowerBound[smallest] = lowerBound[j]; + lowerBound[j] = t; + }; + if (DEBUG) { + fprintf(outputFile, "lower bound coeeficients = "); + for (j = 0; j < lowerBounds; j++) + fprintf(outputFile, " %d", GEQs[lowerBound[j]].coef[i]); + fprintf(outputFile, "\n"); + }; + + + for (j = 0; j < lowerBounds; j++) { + int maxIncr; + int c; + e = lowerBound[j]; + maxIncr = ((GEQs[e].coef[i] - 1) * (worstLowerBoundConstant - 1) - 1) + / worstLowerBoundConstant; + + /* maxIncr += 2; */ + if ((doTrace && desiredResult == SIMPLIFY) || DBUG) { + fprintf(outputFile, "for equation "); + printGEQ(&GEQs[e]); + fprintf(outputFile, "\ntry decrements from 0 to %d\n", maxIncr); + printProblem(problemPtr); + }; + if (maxIncr > 50) { + if (!smoothed && smoothWeirdEquations(problemPtr)) { + conservative--; +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + free(rS); + free(iS); + smoothed = 1; + goto solveGEQstart; + }; + }; + eqncpy(&EQs[0], &GEQs[e]); + /* + * if (GEQs[e].color) fprintf(outputFile,"warning: adding black equality constraint + * based on red inequality\n"); + */ + EQs[0].color = black; + eqnzero(&GEQs[e]); + GEQs[e].touched = TRUE; + //nEQ = 1; + problemPtr->setNumEqs(1); + for (c = maxIncr; c >= 0; c--) { + if (DBUG) + fprintf(outputFile, "trying next decrement of %d\n", maxIncr - c); + if (DBUG) + printProblem(problemPtr); + problemcpy(rS, problemPtr); + if (DEBUG) + printProblem(rS); + result = solve(rS, desiredResult); + if (result == TRUE) { +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + free(rS); + free(iS); + conservative--; + return (TRUE); + }; + EQs[0].coef[0]--; + }; + if (j + 1 < lowerBounds) { + //nEQ = 0; + problemPtr->setNumEqs(0); + eqncpy(&GEQs[e], &EQs[0]); + GEQs[e].touched = 1; + GEQs[e].color = black; + problemcpy(rS, problemPtr); + if (DEBUG) + fprintf(outputFile, "exhausted lower bound, checking if still feasible "); + result = solve(rS, FALSE); + if (result == FALSE) + break; + }; + }; + if ((doTrace && desiredResult == SIMPLIFY) || DBUG) + fprintf(outputFile, "fall-off the end\n"); +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + free(rS); + free(iS); + + conservative--; + return (FALSE); + }; + }; +#ifdef _WIN32 + removeFromCollection(rS); + removeFromCollection(iS); +#endif + free(rS); + free(iS); + }; + return (UNKNOWN); + }; + } while (eliminateAgain); + }; +} + + +/* + * Return 1 if red equations constrain the set of possible solutions. We assume that there are solutions to the black + * equations by themselves, so if there is no solution to the combined problem, we return 1. + */ + +int hasRedEquations(Problem * problemPtr, bool expensive) +{ + int result; + int e; + int i; + + if (TRACE) { + fprintf(outputFile, "Checking for red equations:\n"); + printProblem(problemPtr); + }; + + pleaseNoEqualitiesInSimplifiedProblems++; + mayBeRed++; + result = !simplifyProblem(problemPtr); + mayBeRed--; + pleaseNoEqualitiesInSimplifiedProblems--; + if (result) { + return 1; + } + freeRedEliminations(problemPtr); + + + assert(nEQ == 0); + + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].color == red) + result = 1; + if (!result) + return 0; + + if (!expensive) + for (i = safeVars; i >= 1; i--) { + int ub = 0; + int lb = 0; + for (e = nGEQ - 1; e >= 0; e--) { + if (GEQs[e].coef[i]) + if (GEQs[e].coef[i] > 0) + lb |= (1 + GEQs[e].color); + else /* (GEQs[e].coef[i] < 0) */ + ub |= (1 + GEQs[e].color); + } + if (ub == 2 || lb == 2) { + if (DBUG) + fprintf(outputFile, "checks for upper/lower bounds worked!\n"); +#if reduceWithSubstitutions +#else + resurrectSubs(problemPtr); + assert(nSUB == 0); +#endif + return 1; + }; + }; + + + if (TRACE) + fprintf(outputFile, "*** Doing potentially expensive elimination tests for red equations\n"); + pleaseNoEqualitiesInSimplifiedProblems++; + eliminateRed(problemPtr, expensive); + pleaseNoEqualitiesInSimplifiedProblems--; + result = 0; + assert(nEQ == 0); + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].color == red) + result = 1; + if (TRACE) + if (!result) + fprintf(outputFile, "******************** Redudant Red Equations eliminated!!\n"); + else + fprintf(outputFile, "******************** Red Equations remain\n"); + if (DEBUG) + printProblem(problemPtr); + +#if reduceWithSubstitutions +#else + resurrectSubs(problemPtr); + assert(nSUB == 0); +#endif + return result; +} + +int simplifyApproximate(Problem *problemPtr) +{ + int result; + + inApproximateMode = 1; + if (TRACE) + fprintf(outputFile, "Entering Approximate Mode\n"); + + result = simplifyProblem(problemPtr); + + if (nVars != safeVars) { + fprintf(outputFile, "assertion blown!\n"); + printProblem(problemPtr); + fflush(outputFile); + assert(nVars == safeVars); + }; + + if (TRACE) + fprintf(outputFile, "Leaving Approximate Mode\n"); + inApproximateMode = 0; + +#if reduceWithSubstitutions +#else + assert(nSUB == 0); +#endif + return (result); +} + +int simplifyProblem(Problem *problemPtr) +{ + int i; + foundReduction = FALSE; + + if (!problemPtr->variablesInitialized) { + initializeVariables(problemPtr); + }; + + +#ifdef clearForwardingPointers + for (i = 1; i <= nVars; i++) + if (problemPtr->_var[i] > 0) + problemPtr->forwardingAddress[problemPtr->_var[i]] = 0; +#endif + + if (nextKey * 3 > maxKeys) { + int e; + hashVersion++; + nextKey = maxVars + 1; + for (e = nGEQ - 1; e >= 0; e--) + GEQs[e].touched = TRUE; + for (i = 0; i < hashTableSize; i++) + hashMaster[i].touched = -1; + problemPtr->hashVersion = hashVersion; + } + else if (problemPtr->hashVersion != hashVersion) { + int e; + for (e = nGEQ - 1; e >= 0; e--) + GEQs[e].touched = TRUE; + problemPtr->hashVersion = hashVersion; + }; + + nonConvex = 0; + if (nVars > nEQ + 3 * safeVars) + freeEliminations(problemPtr, safeVars); + + solve(problemPtr, SIMPLIFY); + if (foundReduction) { + for (i = 1; i <= safeVars; i++) + problemPtr->forwardingAddress[problemPtr->_var[i]] = i; + for (i = 0; i < nSUB; i++) + problemPtr->forwardingAddress[SUBs[i].key] = -i - 1; + }; +#if reduceWithSubstitutions +#else + assert(pleaseNoEqualitiesInSimplifiedProblems + || !foundReduction || nSUB == 0); +#endif + return (foundReduction); +} + + +void unprotectVariable(Problem *problemPtr, int v) +{ + int e, t, j, i; + i = problemPtr->forwardingAddress[v]; + if (i < 0) { + i = -1 - i; + //nSUB--; + problemPtr->addNumSUBs(-1); + if (i < nSUB) { + eqncpy(&SUBs[i], &SUBs[nSUB]); + problemPtr->forwardingAddress[SUBs[i].key] = -i - 1; + }; + } + else { + int bringToLife[maxVars]; + int comingBack = 0; + int e2; + for (e = nSUB - 1; e >= 0; e--) + if (bringToLife[e] = (SUBs[e].coef[i] != 0)) + comingBack++; + + for (e2 = nSUB - 1; e2 >= 0; e2--) + if (bringToLife[e2]) { + problemPtr->addToVarsN(1); + //nVars++; + safeVars++; + if (safeVars < nVars) { + for (e = nGEQ - 1; e >= 0; e--) { + GEQs[e].coef[nVars] = GEQs[e].coef[safeVars]; + GEQs[e].coef[safeVars] = 0; + }; + for (e = nEQ - 1; e >= 0; e--) { + EQs[e].coef[nVars] = EQs[e].coef[safeVars]; + EQs[e].coef[safeVars] = 0; + }; + for (e = nSUB - 1; e >= 0; e--) { + SUBs[e].coef[nVars] = SUBs[e].coef[safeVars]; + SUBs[e].coef[safeVars] = 0; + }; + problemPtr->_var[nVars] = problemPtr->_var[safeVars]; + problemPtr->forwardingAddress[problemPtr->_var[nVars]] = nVars; + } + else { + for (e = nGEQ - 1; e >= 0; e--) { + GEQs[e].coef[safeVars] = 0; + }; + for (e = nEQ - 1; e >= 0; e--) { + EQs[e].coef[safeVars] = 0; + }; + for (e = nSUB - 1; e >= 0; e--) { + SUBs[e].coef[safeVars] = 0; + }; + }; + + problemPtr->_var[safeVars] = SUBs[e2].key; + problemPtr->forwardingAddress[SUBs[e2].key] = safeVars; + + const int numEQ = nEQ; + problemPtr->addNumEqs(1); + + eqncpy(&(EQs[numEQ]), &(SUBs[e2])); + //EQs[nEQ++].coef[problemPtr->_safeVars] = -1; + EQs[numEQ].coef[problemPtr->_safeVars] = -1; + + assert(nEQ <= maxEQs); + if (e2 < nSUB - 1) + eqncpy(&(SUBs[e2]), &(SUBs[nSUB - 1])); + //nSUB--; + problemPtr->addNumSUBs(-1); + }; + + + + + if (i < safeVars) { + j = safeVars; + for (e = nSUB - 1; e >= 0; e--) { + t = SUBs[e].coef[j]; + SUBs[e].coef[j] = SUBs[e].coef[i]; + SUBs[e].coef[i] = t; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[j] != GEQs[e].coef[i]) { + GEQs[e].touched = TRUE; + t = GEQs[e].coef[j]; + GEQs[e].coef[j] = GEQs[e].coef[i]; + GEQs[e].coef[i] = t; + }; + for (e = nEQ - 1; e >= 0; e--) { + t = EQs[e].coef[j]; + EQs[e].coef[j] = EQs[e].coef[i]; + EQs[e].coef[i] = t; + }; + t = problemPtr->_var[j]; + problemPtr->_var[j] = problemPtr->_var[i]; + problemPtr->_var[i] = t; + problemPtr->forwardingAddress[problemPtr->_var[i]] = i; + problemPtr->forwardingAddress[problemPtr->_var[j]] = j; + }; + safeVars--; + }; + chainUnprotect(problemPtr); + +} + + +int constrainVariableSign(Problem *problemPtr, int color, int i, int sign) +{ + int nV = nVars; + int e, k, j; + + k = problemPtr->forwardingAddress[i]; + if (k < 0) { + k = -1 - k; + + if (sign != 0) { + //e = nGEQ++; + e = nGEQ; + problemPtr->addNumGEqs(1); + + eqncpy(&GEQs[e], &SUBs[k]); + for (j = 0; j <= nV; j++) + GEQs[e].coef[j] *= sign; + GEQs[e].coef[0]--; + GEQs[e].touched = 1; + GEQs[e].color = color; + } + else { + //e = nEQ++; + e = nEQ; + problemPtr->addNumEqs(1); + + assert(nEQ <= maxEQs); + eqncpy(&EQs[e], &SUBs[k]); + EQs[e].color = color; + }; + + } + else if (sign != 0) { + //e = nGEQ++; + e = nGEQ; + problemPtr->addNumGEqs(1); + + eqnzero(&GEQs[e]); + GEQs[e].coef[k] = sign; + GEQs[e].coef[0] = -1; + GEQs[e].touched = 1; + GEQs[e].color = color; + } + else { + //e = nEQ++; + e = nEQ; + problemPtr->addNumEqs(1); + + assert(nEQ <= maxEQs); + eqnzero(&EQs[e]); + EQs[e].coef[k] = 1; + EQs[e].color = color; + }; + unprotectVariable(problemPtr, i); + return (simplifyProblem(problemPtr)); +} + +void constrainVariableValue(Problem *problemPtr, int color, int i, int value) +{ + int e, k; + + k = problemPtr->forwardingAddress[i]; + if (k < 0) { + k = -1 - k; + + //e = nEQ++; + e = nEQ; + problemPtr->addNumEqs(1); + + assert(nEQ <= maxEQs); + eqncpy(&EQs[e], &SUBs[k]); + EQs[e].coef[0] -= value; + + } + else { + //e = nEQ++; + e = nEQ; + problemPtr->addNumEqs(1); + + eqnzero(&EQs[e]); + EQs[e].coef[k] = 1; + EQs[e].coef[0] = -value; + }; + EQs[e].color = color; +} + +int queryVariable(Problem *problemPtr, int i, int *lowerBound, int *upperBound) +{ + int nV = nVars; + int e, j; + int isSimple; + int coupled = FALSE; + i = problemPtr->forwardingAddress[i]; + + (*lowerBound) = negInfinity; + (*upperBound) = posInfinity; + + if (i < 0) { + int easy = TRUE; + i = -i - 1; + for (j = 1; j <= nV; j++) + if (SUBs[i].coef[j] != 0) + easy = FALSE; + if (easy) { + *upperBound = *lowerBound = SUBs[i].coef[0]; + return (FALSE); + }; + return (TRUE); + }; + + for (e = nSUB - 1; e >= 0; e--) + if (SUBs[e].coef[i] != 0) + coupled = TRUE; + + for (e = nEQ - 1; e >= 0; e--) + if (EQs[e].coef[i] != 0) { + isSimple = TRUE; + for (j = 1; j <= nV; j++) + if (i != j && EQs[e].coef[j] != 0) { + isSimple = FALSE; + coupled = TRUE; + break; + }; + if (!isSimple) + continue; + else { + *lowerBound = *upperBound = -EQs[e].coef[i] * EQs[e].coef[0]; + return (FALSE); + }; + }; + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[i] != 0) { + if (GEQs[e].key == i) { + setMax(*lowerBound, -GEQs[e].coef[0]); + } + else if (GEQs[e].key == -i) { + setMin(*upperBound, GEQs[e].coef[0]); + } + else + coupled = TRUE; + }; + return (coupled); +} + +extern void queryCoupledVariable(Problem *problemPtr, int i, int *l, int *u, int *couldBeZero, int lowerBound, int upperBound); + +int queryVariableBounds(Problem *problemPtr, int i, int *l, int *u) +{ + int coupled; + *l = negInfinity; + *u = posInfinity; + coupled = queryVariable(problemPtr, i, l, u); + if (!coupled || (nVars == 1 && problemPtr->forwardingAddress[i] == 1)) + return 0; + if (abs(problemPtr->forwardingAddress[i]) == 1 && nVars + nSUB == 2 && nEQ + nSUB == 1) { + int couldBeZero; + queryCoupledVariable(problemPtr, i, l, u, &couldBeZero, negInfinity, posInfinity); + return 0; + }; + return 1; +} + +void queryCoupledVariable(Problem *problemPtr, int i, int *l, int *u, int *couldBeZero, int lowerBound, int upperBound) +{ + int e, b1, b2; + Eqn eqn; + int sign; + int v; + + if (abs(problemPtr->forwardingAddress[i]) != 1 || nVars + nSUB != 2 || nEQ + nSUB != 1) { + fprintf(outputFile, "queryCoupledVariablecalled with bad parameters\n"); + printProblem(problemPtr); + Exit(2); + }; + + if (problemPtr->forwardingAddress[i] == -1) { + eqn = &SUBs[0]; + sign = 1; + v = 1; + } + else { + eqn = &EQs[0]; + sign = -eqn->coef[1]; + v = 2; + }; + + /* Variable i is defined in terms of variable v */ + + for (e = nGEQ - 1; e >= 0; e--) + if (GEQs[e].coef[v] != 0) { + if (GEQs[e].coef[v] == 1) { + setMax(lowerBound, -GEQs[e].coef[0]) + } + else { + setMin(upperBound, GEQs[e].coef[0]); + }; + }; + /* lowerBound and upperBound are bounds on the value of v */ + + if (lowerBound > upperBound) { + *l = posInfinity; + *u = negInfinity; + *couldBeZero = 0; + return; + }; + if (lowerBound == negInfinity) { + if (eqn->coef[v] > 0) + b1 = sign * negInfinity; + else + b1 = -sign * negInfinity; + } + else + b1 = sign * (eqn->coef[0] + eqn->coef[v] * lowerBound); + if (upperBound == posInfinity) { + if (eqn->coef[v] > 0) + b2 = sign * posInfinity; + else + b2 = -sign * posInfinity; + } + else + b2 = sign * (eqn->coef[0] + eqn->coef[v] * upperBound); + + /* b1 and b2 are bounds on the value of i (don't know which is upper bound) */ + if (b1 <= b2) { + setMax(*l, b1); + setMin(*u, b2); + } + else { + setMax(*l, b2); + setMin(*u, b1); + }; + *couldBeZero = *l <= 0 && 0 <= *u && intMod(eqn->coef[0], abs(eqn->coef[v])) == 0; +} + + + +int queryVariableSigns(Problem *problemPtr, int i, int dd_lt, int dd_eq, int dd_gt, int lowerBound, int upperBound, bool *distKnown, int *dist) +{ + int result; + int l, u; + int couldBeZero; + + l = negInfinity; + u = posInfinity; + + queryCoupledVariable(problemPtr, i, &l, &u, &couldBeZero, lowerBound, upperBound); + result = 0; + if (l < 0) + result |= dd_gt; + if (u > 0) + result |= dd_lt; + if (couldBeZero) + result |= dd_eq; + if (l == u) { + *distKnown = 1; + *dist = l; + } + else + *distKnown = 0; + return (result); +} + + +static int omegaInitialized = 0; + +void initializeOmega() +{ + int i; + if (omegaInitialized) + return; + + nextWildCard = 0; + nextKey = maxVars + 1; + for (i = 0; i < hashTableSize; i++) + hashMaster[i].touched = -1; + + sprintf(wildName[1], "alpha"); + sprintf(wildName[2], "beta"); + sprintf(wildName[3], "gamma"); + sprintf(wildName[4], "delta"); + sprintf(wildName[5], "tau"); + sprintf(wildName[6], "sigma"); + sprintf(wildName[7], "chi"); + sprintf(wildName[8], "omega"); + sprintf(wildName[9], "pi"); + sprintf(wildName[10], "ni"); + sprintf(wildName[11], "Alpha"); + sprintf(wildName[12], "Beta"); + sprintf(wildName[13], "Gamma"); + sprintf(wildName[14], "Delta"); + sprintf(wildName[15], "Tau"); + sprintf(wildName[16], "Sigma"); + sprintf(wildName[17], "Chi"); + sprintf(wildName[18], "Omega"); + sprintf(wildName[19], "Pi"); + + omegaInitialized = 1; +} + + +void setOutputFile(FILE * file) +{ + /* sets the file to which printProblem should send its output to "file" */ + + outputFile = file; + +} /* end setOutputFile(FILE *file) */