Ejemplo n.º 1
0
/*
 * save_kodbase: Writes out the new database file based on class list
 *              passed in from parser.  Returns True iff successful.
 */
int save_kodbase()
{
   FILE *kodbase;
   int numexternals;
   list_type external_list;

   if ((kodbase = fopen(basefile, "wt")) == NULL)
   {
      simple_error("Unable to open database file %s", basefile);
      return False;
   }
  
   /* First, write out status line of # of identifiers & resources */
   fprintf(kodbase, "T %d %d\n", st.maxid, st.maxresources);

   /* Loop over all classes, and write them out */
   save_class_list(kodbase, st.classes);

   /* Write out unresolved externals */
   external_list = table_get_all(st.missingvars);
   numexternals = save_externals(kodbase, external_list);
   
   if (numexternals != 0)
      simple_warning("%d unresolved externals", numexternals);

   fclose(kodbase);
   return True;
}
Ejemplo n.º 2
0
/* 
 * codegen: Generate code for all the classes in the symbol table.
 */
void codegen(char *kod_fname, char *bof_fname)
{
   list_type c = NULL;
   long endpos, stringpos, debugpos, namepos;

   codegen_ok = True;
   debug_lines = NULL;

   outfile = open(bof_fname, O_TRUNC | O_CREAT | O_RDWR | O_BINARY, S_IWRITE | S_IREAD);

   if (outfile == -1)
   {
      simple_error("Unable to open bof file %s!", bof_fname);
      return;
   }

   /* Write out header info */
   codegen_header();

   /* Remember position for backpatching location of kod filename, and leave room */
   namepos = FileCurPos(outfile);
   OutputInt(outfile, 0);

   /* Remember position for backpatching location of string table, and leave room */
   stringpos = FileCurPos(outfile);
   OutputInt(outfile, 0);

   /* Remember position for backpatching location of debugging info, and leave room */
   debugpos = FileCurPos(outfile);
   OutputInt(outfile, 0);
   
   codegen_classes();
   
   if (codegen_ok)
   {
      /* Backpatch location of string table */
      endpos = FileCurPos(outfile);
      FileGoto(outfile, stringpos); 
      OutputInt(outfile, endpos);

      FileGotoEnd(outfile);
      codegen_string_table();

      /* Backpatch location of debug info */
      endpos = FileCurPos(outfile);
      FileGoto(outfile, debugpos); 
      if (debug_bof)
         OutputInt(outfile, endpos);
      else OutputInt(outfile, 0);

      FileGotoEnd(outfile);
      if (debug_bof)
         codegen_debug_info();

      /* Backpatch location of kod filename */
      endpos = FileCurPos(outfile);
      FileGoto(outfile, namepos); 
      OutputInt(outfile, endpos);

      FileGotoEnd(outfile);
      codegen_filename(kod_fname);
   }

   close(outfile);

   /* If code generation failed, delete partial bof file */
   if (!codegen_ok)
   {
      if (unlink(bof_fname))
	 codegen_error("Couldn't delete file %s", bof_fname);
      else simple_warning("Deleted file %s", bof_fname);
   }

   /* Write out resources & new database if we compiled ok */
   if (codegen_ok)
   {
      char temp[256];
      set_extension(temp, bof_fname, ".rsc");
      write_resources(temp);
      save_kodbase();
   }

   /* Mark all classes as done */
   for (c = st.classes; c != NULL; c = c->next)
      ((class_type) (c->data))->is_new = False;
}
Ejemplo n.º 3
0
/*
 * load_kodbase - reads the kodbase.txt file into memory, returns success/failure
 */
int load_kodbase(void)
{
   FILE *kodbase;
   char line[MAX_LINE+1];
   char *type_char, *t1, *t2, *t3, *t4;

   /* If file is missing, give warning but continue, since this is normal
    * if no files have been compiled yet.
    */
   if ((kodbase = fopen(basefile, "rt")) == NULL)
   {
      simple_warning("Unable to open database file %s", basefile);
      return True;
   }

   kodbase_line = 0;
   while (fgets(line, MAX_LINE+1, kodbase))
   {
      kodbase_line++;

      type_char = strtok(line," \t");
      if (type_char == NULL || strlen(type_char) != 1)
      {
	 database_error("Bad type character");
	 fclose(kodbase);
	 return False;
      }

      t1 = strtok(NULL," \t\n");
      t2 = strtok(NULL," \t\n");
      t3 = strtok(NULL," \t\n");
      t4 = strtok(NULL," \t\n");

      switch (type_char[0])
      {
      case 'T' : 
	 if (t2 == NULL || !init_tables(atoi(t1), atoi(t2)))
	 {
	    database_error("Bad type character");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'C' :
	 if (t3 == NULL)
	 {
	    database_error("Bad class entry");
	    fclose(kodbase);
	    return False;
	 }
	 
	 /* Error if missing superclass name */
	 if ((atoi(t3) != 0 && t4 == NULL) || !load_add_class(t1,atoi(t2),atoi(t3), t4))
	 {
	    database_error("Bad class entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'M' :
	 if (t2 == NULL || !load_add_message(t1,atoi(t2)))
	 {
	    database_error("Bad message entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'R':
	 if (t2 == NULL || !load_add_resource(t1,atoi(t2)))
	 {
	    database_error("Bad resource entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'P' :
	 if (t2 == NULL || !load_add_parameter(t1, atoi(t2)))
	 {
	    database_error("Bad parameter entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'Y' :
	 if (t2 == NULL || !load_add_property(t1, atoi(t2)))
	 {
	    database_error("Bad property entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'V' :
	 if (t2 == NULL || !load_add_classvar(t1, atoi(t2)))
	 {
	    database_error("Bad classvar entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'c':
	 if (t2 == NULL || !load_add_external(t1, atoi(t2), I_CLASS))
	 {
	    database_error("Bad external class entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'm':
	 if (t2 == NULL || !load_add_external(t1, atoi(t2), I_MESSAGE))
	 {
	    database_error("Bad external message entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      case 'p':
	 if (t2 == NULL || !load_add_external(t1, atoi(t2), I_PARAMETER))
	 {
	    database_error("Bad external parameter entry");
	    fclose(kodbase);
	    return False;
	 }
	 break;

      default:
	 database_error("Unknown type character %c", type_char[0]);
      }
   }
   /* Now that everone's read in, make pointers to superclasses */
   if (!build_superclasses(st.classes))
   {
      fclose(kodbase);
      return False;
   }
   
   fclose(kodbase);
   return True;
}