Exemplo n.º 1
0
void save_codeblock(CodeBlock *cb, const char *fn_prefix)
{
  FILE *f;
  unsigned int v;
  char *fn = NULL;

  // open file for writing
  if (strcmp(cb->label, "") == 0) asprintf(&fn, "%s.sux", fn_prefix);
  else asprintf(&fn, "%s.%s.sux", fn_prefix, cb->label);
  f = fopen(fn, "w");
  assert(f != NULL);

  Strtab *strtab = init_strtab();

  // file header: magic + number of operations
  v = SUX_MAGIC; // magic
  fwrite(&v, sizeof(unsigned int), 1, f);
  v = cb->nops;
  fwrite(&v, sizeof(unsigned int), 1, f);

  // write operations (8 bytes each)
  int i = 0;
  while (i < cb->nops) {
    save_operation(f, &cb->code[i++], strtab);
  }

  // save string table
  save_strtab(strtab, f);

  // cleanup
  delete_strtab(strtab);
  fclose(f);
  free(fn);
}
Exemplo n.º 2
0
Strtab* load_strtab(FILE *f)
{
  unsigned int v;

  // header: check magic and get number of characters
  fread(&v, sizeof(unsigned int), 1, f);
  if (v != STRTAB_MAGIC) return NULL;

  fread(&v, sizeof(unsigned int), 1, f);

  // initialize new string table
  Strtab *st = init_strtab();
  st->elem = v;
  resize_strtab(st);

  // read data
  fread(st->data, sizeof(char), v, f);
  st->pos = v;

  return st;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    if (argc <= 1) {
        return 1;
    }

    // ./cc src.cmm out.s
    FILE *file;
    file     = fopen(argv[1], "r");
    asm_file = fopen(argv[2], "w");

    if (!file) {
        perror(argv[1]);
        return 1;
    }
    else if (!asm_file) {
        perror(argv[2]);
        return 1;
    }

    init_strtab();

    yyrestart(file);

    yyparse();

    if (!is_syn_error) {
        semantic_analysis();

        if (!semantic_error) {
            translate();
        }
    }

    free_ast();
    return 0;
}