/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_gen_code_c[] = "@(#)gen_code.C	1.17 11/13/92 11:23:01 LLNL ";


#include "decls.h"
#include "abs.h"
#include "gen_code.h"

void gen_exception_goto(FILE *out,int z,char *inst,char *lbl)
{
   GEN_LINEDIR
   INDENT(out,z);
   fprintf(out,"__RO_GOTO_EXCEPTION(%s,%s);\n",inst,lbl);
}

void gen_exception(FILE *out,
		   int z,char *err,char *lbl,char *instname)
{
   INDENT(out,z);
   fprintf(out,"{ __RO_RAISE_RUNTIME(%s,%s,%s); }\n",
	   instname,err,lbl);
}

static int exception_label_counter = 0;
char *gen_exception_label()
{
   char * label;
   int digits;
   int temp = exception_label_counter;

   digits = 1;
   if (exception_label_counter > 9) {
      digits = 0;
      while (temp) {
	 digits++;
	 temp = temp/10;
      }
   }
   label = (char*)malloc(strlen("__ROl_EXCEPTION_")+digits+1);
   if (!label) nomem();
   sprintf(label,"__ROl_EXCEPTION_%d",exception_label_counter++);
   return(label);
}

void gen_exception_header(FILE *out, char *exception_label, char *ok_label)

{
 	/* If we get to here, jump over the exception block, to the
	   "OK" label. */

   GEN_LINEDIR
   INDENT(out,1);
   fprintf(out,"goto %s;\n",ok_label);

	/* Lay down the exception label */

   GEN_LINEDIR
   fprintf(out,"\n%s:\n",exception_label);

   GEN_LINEDIR
   INDENT(out,1);
   fprintf(out,"if (__ROr_raised_runtime(%s))\n",GETINSTNAME);
   gen_exception_goto(out,2,GETINSTNAME,exception_label);
   GEN_LINEDIR
   INDENT(out,1);
   fprintf(out,"switch(%s->except_state.entry->exception_id) {\n",GETINSTNAME);

}

void gen_exception_general_error(FILE *out,char *status_expr, char *msg_expr)

{
   if (status_expr)
   {
      INDENT(out,2);
      fprintf(out,"(%s) = %s->except_state.entry->code;\n",
		status_expr,GETINSTNAME);
   }
   if (msg_expr)
   {
      INDENT(out,2);
      fprintf(out,"sprintf((%s),%s->except_state.entry->text,",msg_expr,(GETINSTNAME));
      INDENT(out,2);
      fprintf(out,"    %s->except_state.customize ? %s->except_state.customize : \"???\");\n",GETINSTNAME,GETINSTNAME);
      INDENT(out,2);
      fprintf(out,"if (%s->except_state.customize)\n",GETINSTNAME);
      INDENT(out,3);
      fprintf(out,"free(%s->except_state.customize);\n",GETINSTNAME);
   }
   INDENT(out,2); 
   fprintf(out,"V(%s->exclusion);\n",GETINSTNAME); 
}

void gen_exception_com_error(FILE *out,char *status_expr, char *msg_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_COM_ERROR:\n");
   gen_exception_general_error(out,status_expr,msg_expr);
}

void gen_exception_internal_error(FILE *out,char *status_expr, char *msg_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_INTERNAL_ERROR:\n");
   gen_exception_general_error(out,status_expr,msg_expr);
}

void gen_exception_protocol_error(FILE *out,char *status_expr, char *msg_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_PROTOCOL_ERROR:\n");
   gen_exception_general_error(out,status_expr,msg_expr);
}


void gen_exception_missing_op(FILE *out, char *op_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_MISSING_OP:\n");
   INDENT(out,2);
   fprintf(out,"__ROr_missing_op_xlate(%s,NULL,(%s));\n",
	GETINSTNAME,
	op_expr ? op_expr : "NULL");
   INDENT(out,2); 
   fprintf(out,"V(%s->exclusion);\n",GETINSTNAME); 
}

void gen_exception_bad_op_id(FILE *out, char *op_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_BAD_OP_ID:\n");
   if (op_expr)
   {
      INDENT(out,2);
      fprintf(out,"(%s) = %s->except_state.parameters.bad_op_id.opid;\n",
		op_expr,GETINSTNAME);
   }
   INDENT(out,2); 
   fprintf(out,"V(%s->exclusion);\n",GETINSTNAME); 
}

void gen_exception_missing_arg(FILE *out, char *op_expr, char *arg_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_MISSING_ARG:\n");
   INDENT(out,2);
   fprintf(out,"__ROr_missing_arg_xlate(%s,NULL,(%s),(%s));\n",
	GETINSTNAME,
	op_expr ? op_expr : "NULL",
	arg_expr ? arg_expr : "NULL");
   INDENT(out,2); 
   fprintf(out,"V(%s->exclusion);\n",GETINSTNAME); 
}

void gen_exception_name_error(FILE *out,char *status_expr, char *pos_expr)

{
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"case __ROm_EXCEPTION_NAME_ERROR:\n");
   if (status_expr)
   {
      INDENT(out,2);
      fprintf(out,"(%s) = %s->except_state_parameters.name_error.status;\n",
		status_expr,GETINSTNAME);
   }
   if (pos_expr)
   {
      INDENT(out,2);
      fprintf(out,"(%s) = %s->except_state_parameters.name_error.position;\n",
		pos_expr,GETINSTNAME);
   }
   INDENT(out,2); 
   fprintf(out,"V(%s->exclusion);\n",GETINSTNAME); 
}

void gen_default_handlers(FILE *out,Handler_Masks mask, char *ok_label)

{
   GEN_LINEDIR
   INDENT(out,1); 
   fprintf(out,"/* DEFAULT HANDLERS FOLLOW */\n"); 
   if (!(mask&MASK_COM_ERROR))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_COM_ERROR:\n");
   }
   if (!(mask&MASK_INTERNAL_ERROR))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_INTERNAL_ERROR:\n");
   }
   if (!(mask&MASK_PROTOCOL_ERROR))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_PROTOCOL_ERROR:\n");
   }
   if (!(mask&MASK_MISSING_OP))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_MISSING_OP:\n");
   }
   if (!(mask&MASK_MISSING_ARG))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_MISSING_ARG:\n");
   }
   if (!(mask&MASK_BAD_OP_ID))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_BAD_OP_ID:\n");
   }
   if (!(mask&MASK_NAME_ERROR))
   {
      GEN_LINEDIR
      INDENT(out,1);
      fprintf(out,"case __ROm_EXCEPTION_NAME_ERROR:\n");
   }
   if (!(mask&MASK_DONE)) 
   { 
      GEN_LINEDIR
      INDENT(out,1); 
      fprintf(out,"case __ROm_EXCEPTION_DONE:\n"); 
   } 
   GEN_LINEDIR
   INDENT(out,1);
   fprintf(out,"default:\n");
   GEN_LINEDIR
   INDENT(out,2);
   fprintf(out,"V(%s->exclusion);\n",GETINSTNAME);
   GEN_LINEDIR
   INDENT(out,2);
   fprintf(out,"__ROr_exception_exit(%s);\n",GETINSTNAME);
   GEN_LINEDIR
   INDENT(out,1);
   fprintf(out,"}\n");

	/* Lay down the "OK" label, which jumps over the exception block. */

   GEN_LINEDIR
   fprintf(out,"\n%s: ;\n",ok_label);
}
 

void gen_context_names(char *acn,char *pcn)

{
   if (!acn)
      acn = "1.3.6.1.4.6.383.0.0.0";
   if (!pcn)
      pcn = "1.3.6.1.4.6.383.0.0.1";
   fprintf(out,"static char __ROc_%s_acn[] = \"%s\";\n",
		global.interface_name,acn);
   fprintf(out,"static char __ROc_%s_pcn[] = \"%s\";\n\n",
		global.interface_name,pcn);
}


void gen_log_statement(FILE *out,int z,char *title, char *inst, char *e_label)

{
   GEN_LINEDIR
   INDENT(out,z);
   FPRINTF(out,"if (__ROr_log7statement(%s,\"%s\",\"%s\",%d,\"%s\"))\n",inst,title,inst,linedir_line, linedir_file);
   gen_exception_goto(out,z+1,inst,e_label);
}

void gen_log_type(FILE *out, 
                  int z,
		  char *inst, 
		  char *label, 
		  char *vexpr, 
		  char *print_routine, 
		  int ptr_already, 
		  char *e_label)
{
   char expbuf[512];

   sprintf(expbuf,"%s",label);
   INDENT(out,z);
   fprintf(out,"if (__ROr_log7type(%s,\"%s\",%c(%s),%s))\n",
                inst,
                expbuf,
                ptr_already ? ' ' : '&',
                vexpr,
                print_routine);
   gen_exception_goto(out,z+1,GETINSTNAME,e_label);
}

void gen_getheader(FILE *out, int z,char *lbl,char *instname,
		   __RO_int32 specific, char *specific_error)
{
   INDENT(out,z)
   fprintf(out,"{  if ((%s->getP_header)(%s,&__RO_type,&__RO_length))\n",
      instname,instname);
   gen_exception_goto(out,z+2,instname,lbl);
   if (specific != -1) { 
      INDENT(out,z+1);
      fprintf(out,"if (__RO_type != 0x%8.8x)\n",specific); 
      gen_exception(out,z+2,specific_error,lbl,instname); 
   }
   INDENT(out,z); 
   fprintf(out,"}\n");
}

void gen_log_operation(FILE *out,int z,char *title, char *inst,
			char *iid, char *lid, char *e_label)

{
   INDENT(out,z); 
   fprintf(out,"if (__ROr_log7operation(%s,\"%s\",\"%s\",__RO_operation,%s,%s,%d,\"%s\"))\n",
               inst,
	       title,
	       inst,
	       iid ? iid : "NULL",
	       lid ? lid : "NULL",
	       linedir_line,
	       linedir_file);
   gen_exception_goto(out,z+1,inst,e_label);

}

void gen_accept_start(FILE *out,
		      instance_element *inst_ele,
		      char *iidvar,
		      char *lidvar,
		      boolean dont_P_semaphore)
{
   active_instance_element *active_inst;
   char *oklab, *errlab;

   if (dont_P_semaphore) {
      FPRINTF(out,"\n{   /* accept part of call statement */\n");
   }
   else {
      FPRINTF(out,"\n{   /* accept statement */\n");
   }
   if (dont_P_semaphore) {
      oklab = GETOKLAB;
      errlab = GETEXCEPTIONLAB;
   }
   else {
      oklab = gen_exception_label();
      errlab = gen_exception_label();
   }
   active_inst = new active_instance_element(
		   inst_ele, 
		   errlab,
		   oklab,
		   iidvar,
		   lidvar);
   active_instances_ptr->insert(active_inst);
   INDENT(out,1);
   fprintf(out,"int __RO_operation;\n");
   INDENT(out,1);
   fprintf(out,"__RO_int32 __RO_type;\n");
   INDENT(out,1);
   fprintf(out,"unsigned long __RO_l, __RO_l2, __RO_size, __RO_length;\n\n");
   INDENT(out,1);
   fprintf(out,"long *__RO_invokeID, *__RO_linkedID;\n\n");

   if (!dont_P_semaphore) {
      if (global.gen_log)
	 gen_log_statement(out,
                           1,
			   "ENTERING accept statement",
			   GETINSTNAME,
			   GETEXCEPTIONLAB);
   }

   INDENT(out,1); 
   if (dont_P_semaphore)
      fprintf(out,"/* Don't P(%s->exclusion); */\n",GETINSTNAME);
   else
      fprintf(out,"P(%s->exclusion);\n",GETINSTNAME);

   INDENT(out,1); 
   fprintf(out,"if (%s->sending_nesting)\n",GETINSTNAME);
   INDENT(out,2); 
   fprintf(out,"if (__ROr_STDflush_instance(%s))\n",GETINSTNAME);
   gen_exception_goto(out,3,GETINSTNAME,GETEXCEPTIONLAB);

   INDENT(out,1); 
   fprintf(out,"if ((%s->getRO_start)(%s,&__RO_invokeID,&__RO_linkedID,&__RO_operation))\n",
       GETINSTNAME,GETINSTNAME);
   gen_exception_goto(out,2,GETINSTNAME,GETEXCEPTIONLAB);
   if (iidvar)
   {
      INDENT(out,1);
      fprintf(out,"(%s) = __RO_invokeID;",iidvar);
   }
   if (lidvar)
   {
      INDENT(out,1);
      fprintf(out,"(%s) = __RO_linkedID;",lidvar);
   }

   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"switch(__RO_operation) {\n");
}

/* For real ACCEPT statement */

void gen_accept_end(FILE *out,boolean *seen,char *e_label)
{

   fprintf(out,"\n");
   for (int i=0; i<GETINTERF->optable_gen->operation_count(); i++)
      if (!seen[i])
      {
         INDENT(out,1);
         fprintf(out,"case %d:\n",i);
      }
   INDENT(out,1);
   fprintf(out,"case -1:\n");
   INDENT(out,2);
   fprintf(out,"{__RO_RAISE_MISSING_OP(%s,__RO_operation,%s);}\n",GETINSTNAME,e_label);
   INDENT(out,2);
   fprintf(out,"break;\n\n");

   INDENT(out,1);
   fprintf(out,"default:\n");
   gen_exception(out,
		 2,
		 "__ROx_INTERNAL_UNKNOWNOP",
		 e_label,
		 GETINSTNAME);
   INDENT(out,2);
   fprintf(out,"break;\n\n");

   INDENT(out,1);
   fprintf(out,"} /* END switch(__RO_operation) */\n\n");
   if (global.gen_log)
      gen_log_statement(out,
                        1,
			"EXITING accept statement",
			GETINSTNAME,
			GETEXCEPTIONLAB);
}

/* For CALL statement */

void gen_accept_end(FILE *out,int seen,char *e_label)
{
   fprintf(out,"\n");
   for (int i=0; i<GETINTERF->optable_gen->operation_count(); i++)
      if (i != seen)
      {
         INDENT(out,1);
         fprintf(out,"case %d:\n",i);
      }
   gen_exception(out,
		 2,
		 "__ROx_RO_WRONGOPONCALL",
		 e_label,
		 GETINSTNAME);
   INDENT(out,2);
   fprintf(out,"break;\n\n");

   INDENT(out,1);
   fprintf(out,"default:\n");
   gen_exception(out,
		 2,
		 "__ROx_INTERNAL_UNKNOWNOP",
		 e_label,
		 GETINSTNAME);
   INDENT(out,2);
   fprintf(out,"break;\n\n");

   INDENT(out,1);
   fprintf(out,"} /* END switch(__RO_operation) */\n\n");
}
