コード例 #1
0
ファイル: symtab.c プロジェクト: bjnix/pedogogy
/**
 *
 * Returns a pointer to a symbol table instance
 *
 * @param size the size of the symbol table
 * @return symbol table with 'size' entries
 */
SymTable SymInit(unsigned int size)
{
  register SymTable ip;
  register int power, i;

  if (size < 8)
     size = 8;

  ip = (SymTable) malloc(sizeof(struct SymTable_internal_structure));

  /* compute a size for the sparse index set */
  power = 0;
  i = 31;

  while(i > 1)
  {
    if (size & 1<<i)
    {
      if (i < 20)		/* make the index set 2 to 4 * size */
	 power = (1 << (i+2)) -1;
      else 			/* the set is too large, anyway */
	 power = (1 << i) -1;

      i = 0;			/* exit the little loop */
    }
    i--;
  }

  ip->NumFields = FS;
  ip->FieldVals  = (Generic**)malloc(FS*sizeof(Generic*));
  ip->FieldNames = (char**)malloc(FS*sizeof(char*));
  ip->CleanupFns = (SymCleanupFunc*)malloc(FS*sizeof(void*));
  ip->InitVals   = (Generic*)malloc(FS*sizeof(Generic*));

  bzero((char *)ip->FieldVals, FS*sizeof(Generic*));
  bzero((char *)ip->FieldNames, FS*sizeof(char*));
  bzero((char *)ip->CleanupFns, FS*sizeof(void*));
  bzero((char *)ip->InitVals, FS*sizeof(Generic*));

  ip->NumSlots   = size;
  ip->NextSlot   = FIRST_SLOT;      /* added JMC  14 March 1991 */

  ip->NumIndices = power;
  ip->Index =      (int*) malloc(power*sizeof(int));

  for (i=0; i<power;i++)	/* fill in the index set */
      ip->Index[i] = -1;

  SymInitField(ip,SYM_NAME_FIELD, 0, sfree_if_nonzero);
  ip->NameField = SymFieldIndex(ip, SYM_NAME_FIELD);

#ifdef DEBUG
  fprintf(stderr, "SymInit(%d): sparse index size set to %d.\n", size, power);
  fprintf(stderr, "\tFields: %d, Slots: %d, Next Slot: %d, Name Field: %d.\n",
	  ip->NumFields, ip->NumSlots, ip->NextSlot, ip->NameField);
#endif

  return ip;
}
コード例 #2
0
/**
 * Push a new symbol table on the stack when entering a new scope region
 *
 * @param stack a stack of symbol tables
 * @return a new symbol table 
 */
SymTable beginScope(SymtabStack stack) {
	SymTable symtab = SymInit(SYMTABLE_SIZE);

	SymInitField(symtab,SYMTAB_OFFSET_FIELD,(Generic)-1,NULL);
	SymInitField(symtab,SYMTAB_REGISTER_INDEX_FIELD,(Generic)-1,NULL);

	int intIndex = SymIndex(symtab,SYMTAB_INTEGER_TYPE_STRING);
    int errorIndex = SymIndex(symtab,SYMTAB_ERROR_TYPE_STRING);
    int voidIndex = SymIndex(symtab,SYMTAB_VOID_TYPE_STRING);

    SymPutFieldByIndex(symtab,intIndex,SYMTAB_SIZE_FIELD,(Generic)INTEGER_SIZE);
    SymPutFieldByIndex(symtab,errorIndex,SYMTAB_SIZE_FIELD,(Generic)0);
    SymPutFieldByIndex(symtab,voidIndex,SYMTAB_SIZE_FIELD,(Generic)0);

    SymPutFieldByIndex(symtab,intIndex,SYMTAB_BASIC_TYPE_FIELD,(Generic)INTEGER_TYPE);
    SymPutFieldByIndex(symtab,errorIndex,SYMTAB_BASIC_TYPE_FIELD,(Generic)ERROR_TYPE);
    SymPutFieldByIndex(symtab,voidIndex,SYMTAB_BASIC_TYPE_FIELD,(Generic)VOID_TYPE);

	dlinkPush(dlinkNodeAlloc((Generic)symtab),stack);
	int *size = (int*)dlinkListAtom(stack);
	(*size)++;

	return symtab;
}