Exemplo n.º 1
0
int main() {
  FILE *ys = fopen("./sample.ys", "w");
  int noRoot = 0;		/* 0 means we will have a root */
  symboltable_t *symtab;
  list = NULL;

  /* Build the tree */
  error_out = stderr;
  noRoot = yyparse();

  if (parseError && (!noRoot))
    fprintf(stderr, "WARNING: There were %d parse errors.\nParse tree may be ill-formed.\n", parseError);

  if (noRoot)
    fprintf(stderr, "Parsing reached fatal error. No AST was constructed\n");

  /* Set up the symbol tree */
  symtab = create_symboltable();
  symtab = build_symboltable(symtab, root, root);
  typecheck_ast(symtab, root);
  int retval = typecheck_ast(symtab, root);
  print_checked_ast(stdout, root, 0);
  if (retval != 0) {
    fprintf(stderr, "There were %d errors encountered in the parse tree. Aborting.\n", retval);
    return 1;
  } 
  print_ast(stdout, root, 0);
  print_symtab(symtab);
  code_gen(root, symtab);
  print_symtab(symtab);

  print_quad_list(stdout, list);
  generate_assembly(ys, list, symtab);
  return 0;
}
Exemplo n.º 2
0
/*-----------------------------------------------
 | printtabs
 +---------------------------------------------*/
void printtabs(struct symtabl *curr_symtab)
{
    if(curr_symtab != NULL)
    {
        print_symtab(curr_symtab);
	print_symtab(curr_symtab->labels);
        printtabs(curr_symtab->rsibling);
        printtabs(curr_symtab->child);
    }
}
Exemplo n.º 3
0
int main() {

  int noRoot = 0;		/* 0 means we will have a root */
  symboltable_t *symtab;
  list = NULL;

  /* Build the tree */
  error_out = stderr;
  noRoot = yyparse();

  if (parseError && (!noRoot))
    fprintf(stderr, "WARNING: There were %d parse errors.\nParse tree may be ill-formed.\n", parseError);

  if (noRoot)
    fprintf(stderr, "Parsing reached fatal error. No AST was constructed\n");

  /* Set up the symbol tree */
  symtab = create_symboltable();
  symtab = build_symboltable(symtab, root, root);
  print_symtab(symtab);
  print_ast(stdout, root, 0);

  code_gen(root, symtab);

  print_quad_list(stdout, list);
  return 0;
}
Exemplo n.º 4
0
void  calc_parser::term ()
{
		print_symtab (term_symb);			                  // Print the symbol table contents.
		print_ast ();							                  // Print the AST, if ast option indicates.
		traverse	 ();							                  // Traverse the AST, calling the AST actions.
		term_ast  ();
		term_symtab ();
}
Exemplo n.º 5
0
/*
 * Print the symbol table.  This function does not print the contents
 * of the symbol table but sets up the parameters and then calls
 * print_symtab to print the symbols.  This function does not assume
 * that there is only one section of type SYMTAB.  Input is an opened
 * ELF file, a pointer to the ELF header, and the filename.
 */
static void
get_symtab(Elf *elf_file, char *filename)
{
	Elf_Scn	*scn, *scnfd;
	Elf_Data *data;
	GElf_Word symtabtype;
	size_t shstrndx;

	if (elf_getshdrstrndx(elf_file, &shstrndx) == -1) {
		(void) fprintf(stderr, gettext(
		    "%s: %s: cannot get e_shstrndx\n"),
		    prog_name, filename);
		return;
	}

	/* get section header string table */
	scnfd = get_scnfd(elf_file, shstrndx, SHT_STRTAB);
	if (scnfd == NULL) {
		(void) fprintf(stderr, gettext(
		    "%s: %s: cannot get string table\n"),
		    prog_name, filename);
		return;
	}

	data = elf_getdata(scnfd, NULL);
	if (data->d_size == 0) {
		(void) fprintf(stderr, gettext(
		    "%s: %s: no data in string table\n"),
		    prog_name, filename);
		return;
	}

	if (D_flag)
		symtabtype = SHT_DYNSYM;
	else if (L_flag)
		symtabtype = SHT_SUNW_LDYNSYM;
	else
		symtabtype = SHT_SYMTAB;

	scn = 0;
	while ((scn = elf_nextscn(elf_file, scn)) != 0)	{
		GElf_Shdr shdr;

		if (gelf_getshdr(scn, &shdr) == NULL) {
			(void) fprintf(stderr, "%s: %s: %s:\n",
			    prog_name, filename, elf_errmsg(-1));
			return;
		}

		if (shdr.sh_type == symtabtype)	{
			print_symtab(elf_file, shstrndx, scn,
			    &shdr, filename);
		}
	} /* end while */
}
Exemplo n.º 6
0
static void
process_elf(Elf *elf, char *file, int fd, int member)
{
	Elf_Cmd	cmd;
	Elf	*_elf;

	switch (elf_kind(elf)) {
	case ELF_K_ELF:
		/*
		 * This is an ELF file, now attempt to find it's
		 * .comment section and to display it.
		 */
		print_symtab(elf, file);
		break;
	case ELF_K_AR:
		/*
		 * Archives contain multiple ELF files, which can each
		 * in turn be examined with libelf.
		 *
		 * The below loop will iterate over each member of the
		 * archive and recursively call process_elf().
		 */
		cmd = ELF_C_READ;
		while ((_elf = elf_begin(fd, cmd, elf)) != NULL) {
			Elf_Arhdr	*arhdr;
			char		buffer[1024];

			arhdr = elf_getarhdr(_elf);

			/*
			 * Build up file names based off of
			 * 'archivename(membername)'.
			 */
			(void) snprintf(buffer, 1024, "%s(%s)",
			    file, arhdr->ar_name);

			/*
			 * Recursively process the ELF members.
			 */
			process_elf(_elf, buffer, fd, 1);
			cmd = elf_next(_elf);
			(void) elf_end(_elf);
		}
		break;
	default:
		if (!member)
			(void) fprintf(stderr,
			    "%s: unexpected elf_kind(): 0x%x\n",
			    file, elf_kind(elf));
		return;
	}
}
Exemplo n.º 7
0
/**
 * \brief Entry point.
 * \param argc The number of input parameters.
 * \param argv The input parameters.
 */
int main (int argc, char *argv[]) {
	extern FILE* yyin;
	FILE* ir_output_file;
	//extern symtab_entry *symtab;
	//extern symtab_entry *current_symtab;

	/* the resource manager must be initialized before any
   * further actions are implemented */
  rm_init(&resource_mgr);

  if (process_options(argc, argv) == 1) {
    rm_cleanup_resources(&resource_mgr);
    exit(EXIT_FAILURE);
  }

  printf("Input: %s\n", cc_options.input_file);
  printf("Output: %s\n", cc_options.output_file);
  printf("IR: %s\n", cc_options.ir_file);

  //Set Input file and Start parsing
  yyin = fopen(cc_options.input_file,"r");
  
  //Globals
  symtab = NULL;
  current_symtab = NULL;
  pointer_to_called_function = NULL;
  MAIN_FUNCTION_FLAG = 0;
  GLOBAL_DECLARATION_FLAG = 0;
  help_var_counter = 0;
  help_label_counter = 0;
  global_help_var = (char *) malloc(sizeof(char)*15);
  global_help_label = (char *) malloc(sizeof(char)*15);
  init(&GLOBAL_HELP_STACK);
  intermediate_code = NULL;
  offset = 0;
  global_offset = 0;
  
  //Start parsing
  yyparse();
  
  //Output after Parsing
  //Symboltable
  printf("Symboltable:\n\n");
  print_symtab();
  
  //Intermediatecode
  if(intermediate_code != NULL){
  remove_nop_statements_from_ircode(intermediate_code);
  printf("INTERMEDIATE CODE:\n\n");
  print_ircode(intermediate_code);
 // print_basic_blocks(generate_basic_blocks(intermediate_code));
  }

  //Final Codegen
  if(intermediate_code != NULL){
  init_general_registers();
  show_general_registers();
  generate_final_code(intermediate_code);
  show_general_registers();


  }

   

  if(!MAIN_FUNCTION_FLAG)
  	printf("FATAL COMPILER ERROR no main function defined!\n");

  //Cleanup resources
  free(global_help_var);
  rm_cleanup_resources(&resource_mgr);
  fclose(yyin);
  return 0;
}