/* codegen.h

   This header file was created for the 2010 version of the code generation tools. Updated in 2012.
*/

/* The code generation module expects processes satisfying the following

   - Nondeterministic places: If a place is nondeterministic and one of its
     output transitions is controlled by the supervisor or participates in a
     process synchronization, then permission to fire is always requested 
     from the supervisor. (NOT YET IMPLEMENTED.)

   - The supervisor format: It is assumed that each transition has a 
     one label. (That is, though multiple transitions may share a common 
     label, the expectation is that exactly one label (no more, no less)
     is assigned to each transition.) Process transitions that are 
     synchronized are assumed to have the same label. The supervisor is 
     connected to the plant using the conventional parallel composition of 
     PNs. 

   - The select function: If defined, it should return the number of 
     transitions selected. Let n be that number. The select function writes the
     list of enabled transitions by writing their data to the first n elements
     of the array ___TR of ProcessTemplate.c.

     It is assumed that for a pn structure, the select[i] item contains a 
     string indicating how the select function should be called. Example:
     select[0] = "myselect(1, x, y);", where x and y should be defined in 
     the context of the segment[i] code. Note that select[0] does not specify
     any assignment of the return value. The code generation module is 
     responsible for writing the assignment of the return value. 

*/



#define INDENT 2 // indentation increment used in generated C code

#define DBG3 is_verbose()>=3

#define LINEDIR "/*___LINEDIR*/" // Read lines below before changing!
// The string of LINEDIR is not completely arbitrary: FillTemplate assumes
// that the first character is not repeated, excepting the last character.
// LINEDIR is inserted within the text sent to FillTemplate in order to mark
// points at which #line directives should be inserted redirecting  
// error messages from the compiler to the actual file that is compiled.


struct input { // the nonzero field among 'buf' and 'file' is to be used
  FILE *file;
  char *buf;
};


int FillTemplate(struct input template, FILE* outp, char* outpname, \
		 char* inclname, char* preamble, ...); // in filltmpl.lex
// This function uses 'template' in order to generate a customized output file.
// 'outpname' denotes the name of the outp file.
// 'outp' is the file that FillTemplate writes.  
// 'preamble', if nonzero, is written at the beginning of this file.
// If 'inclname' is nonzero, it is used to replace spcommon.h in 
// #include"spcommon.h" with 'inclname'.
// This function expects cblock objects for the optional arguments.
// If no 'line' directives should be written, the 'file' field of 
// the cblock object should be zero.


FILE* searchTemplate(char* s); // in plantCompiler.c

char* GetFileName(const char* s, const char *ext);
// Similar to "asprintf(&x, "%s.%s", s, ext); return x;" 

void compileSupervisor(specs *sp, pns *supervisor, process **procTypes, \
		       int nbrTypes, TrData** TrInfo, struct synclist* sl);

void compileProcessArray(specs *sp, process **procArray, int nbrElements, \
			 TrData** TrInfo, int** actlabels);
