next up previous contents
Next: Diffs to snoot Up: Source Code Previous: Profiling Implementation

Auxillary routines

#include "pshuang.h"

/**********************************************************************/
/* Definitions */
/**********************************************************************/

int debug_level = 0;
base_symtab *file_symbol_table = NULL;
base_symtab *global_symbol_table = NULL;

char *k_qif_statement;
char *k_qif_set_analyzed;
char *k_qif_set;
char *k_qif_variable;
char *k_qif_variable_defined;

char *k_qint_parameter;
char *k_qinfluence_statement;
char *k_qinfluence_set;

char *k_qdecision_history;
char *k_qdecision_current;

/**********************************************************************/
/* Code */
/**********************************************************************/

/* The code for my_suif_proc_iter was lifted from suif/include/suif/misc.cc.
  I needed the functionality of being able to call a function after all
  the SUIF file proc's had been processed, so I threw in the ability
  to call a function before, too.  Modifications are clearly marked. */

void my_suif_proc_iter(int argc, char * argv[], prociter_f fun,
		       /* ADDED */ void (*before_hook)(void),
		       /* ADDED */ void (*after_hook)(void),
		       boolean writeback,
		       boolean exp_trees,
		       boolean use_fortran_form)
{

// [....]

    /* ADDED */ if (before_hook != NULL) (*before_hook)();

// [....]

    /* ADDED */ if (after_hook != NULL) (*after_hook)();

// [....]

}

/**********************************************************************/

void attach_annotation_string(suif_object *obj,
			      char *the_annotation,
			      char *string_data)
{
  immed *i = new immed(string_data);
  immed_list_e *ile = new immed_list_e(*i);
  immed_list *il = new immed_list();
  il->push(ile);

  obj->append_annote(the_annotation, (void *) il);
}

/**********************************************************************/

immed_list *pCStringSet2pIL(CStringSet *pSet)
{
  Pix index;
  immed_list *il = new immed_list();
  if (pSet == NULL)
    return il;

  index = pSet->first();
  while (index != 0)
    {
      immed *i = new immed((*pSet)(index));
      immed_list_e *ile = new immed_list_e(*i);
      il->push(ile);
      pSet->next(index);
    }
  return il;
}

CStringSet *pIL2pCStringSet(immed_list *il)
{
  CStringSet *pSet = new CStringSet();

  immed_list_iter *iter = new immed_list_iter(il);
  while (! iter->is_empty())
    {
      immed i = iter->step();
      if (i.is_string())
	pSet->add(strdup(i.string()));
    }

  return pSet;
}

/**********************************************************************/

char *pCStringSet2pChar(CStringSet *pSet)
{
  if (pSet == NULL)
    return NULL; /* Seems fitting, no? */

#define INITIAL_SIZE 1024
#define INCREMENT 1024
  int length_used = 0;
  int buffer_size = INITIAL_SIZE;
  char *buffer = (char *) malloc(INITIAL_SIZE);
  assert(buffer != NULL);
  buffer[0] = 0;

  Pix index;
  index = pSet->first();
  while (index != 0)
    {
      char *str = (*pSet)(index);
      int len = strlen(str);

      while ((length_used + (len+3)) >= buffer_size)
	/* The constant 3 is for the two single quotes and potential comma */
	{
	  buffer_size = buffer_size + INCREMENT;
	  buffer = (char *) realloc(buffer, buffer_size+1); /* +1 for '\0' */
	  assert(buffer != NULL);
	}
      sprintf((char *)(buffer+length_used), "'%s'", str);
      length_used = length_used + (len+2);
      pSet->next(index);
      if (index != 0)
	{
	  sprintf((char *)(buffer+length_used), ",");
	  length_used++;
	}
    }
  buffer = (char *) realloc(buffer, length_used+1); /* +1 for '\0' */
  assert(buffer != NULL);
  return buffer;
}

CStringSet *pChar2pCStringSet(char *input_str)
{
  /* This function isn't meant to be able to handle the general case
    of converting strings to CStringSet's; it can handle the output
    format of pCStringSet2pChar, provided that none of the constituent
    strings contains single quotes and/or commas. */
  CStringSet *pSet = new CStringSet();
  char *str = strdup(input_str);
  char *ptr = strtok(str, "',");
  while (ptr != NULL)
    {
      pSet->add(strdup(ptr));
      ptr = strtok(NULL, "',");
    }
  return pSet;
}

/**********************************************************************/

void register_qannotations(void)
{
#define WRITE_ANNOTATION TRUE
  ANNOTE(k_qif_statement, "QIF STATEMENT", WRITE_ANNOTATION);
  ANNOTE(k_qif_set_analyzed, "QIF SET ANALYZED", WRITE_ANNOTATION);
  ANNOTE(k_qif_set, "QIF SET", WRITE_ANNOTATION);
  ANNOTE(k_qif_variable, "QIF VARIABLE", WRITE_ANNOTATION);
  ANNOTE(k_qif_variable_defined, "QIF VARIABLE DEFINED", WRITE_ANNOTATION);

  ANNOTE(k_qint_parameter, "QINT PARAMETER", WRITE_ANNOTATION);
  ANNOTE(k_qinfluence_statement, "QINFLUENCE STATEMENT", WRITE_ANNOTATION);
  ANNOTE(k_qinfluence_set, "QINFLUENCE SET", WRITE_ANNOTATION);

  ANNOTE(k_qdecision_history, "QDECISION HISTORY", WRITE_ANNOTATION);
  ANNOTE(k_qdecision_current, "QDECISION CURRENT", WRITE_ANNOTATION);
}

/**********************************************************************/

SubsetsGenerator::SubsetsGenerator(CStringSet *set)
{
  this->set = new CStringSet();
  (*(this->set)) |= (*set);
  current = NULL;
}

boolean SubsetsGenerator::done()
{
  if (current == NULL) return FALSE;
  return (((*set) == (*current)) ? TRUE : FALSE);
}

void SubsetsGenerator::increment_at_position(Pix index)
{
  char *var = (*set)(index);
  assert(var != NULL);
  if (current->contains(var))
    {
      set->seek(var);
      set->next(index);
      if (index == 0)
	return; /* Current is already at 11...11 */
      current->del(var);
      increment_at_position(index);
    }
  else
    {
      current->add(var);
    }
}

CStringSet *shallow_copy_CStringSet(CStringSet *set)
{
  CStringSet *new_set = new CStringSet();
  Pix index = set->first();
  while (index != 0)
    {
      new_set->add((*set)(index));
      set->next(index);
    }
  return new_set;
}

CStringSet *SubsetsGenerator::yield()
{
  if (current == NULL)
    {
      current = new CStringSet();
      return shallow_copy_CStringSet(current);
    }
  if (done())
    return NULL;

  Pix index = set->first();
  increment_at_position(index);
  return shallow_copy_CStringSet(current);
  /* There would be rep exposure if I returned current itself.... */
}

SubsetsGenerator::~SubsetsGenerator()
{
  delete set;
  delete current;
}

// [....]

/**********************************************************************/

/* Adapted from itoa(), K&R second edition, page 64 */
void lltoa(unsigned long long int n, char s[])
{
  int i = 0;
  do {
    s[i++] = (n % 10) + '0';
  } while ((n /= 10) > 0);
  s[i] = '\0';
  {
    int c,i,j;
    for (i = 0, j = strlen(s)-1; i<j; i++, j--)
      {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
      }
  }
}

/**********************************************************************/



Reinventing Computing, MIT AI Lab. Author: pshuang@ai.mit.edu (Ping Huang)