Exemple #1
0
/**
 * \brief Entry point.
 * \param argc The number of input parameters.
 * \param argv The input parameters.
 * \author Niklas Assmann
 */
int main(int argc, char *argv[]) {
	/* 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);

	// open the source file
	FILE *myfile = fopen(cc_options.input_file, "r");

	// make sure it can be opened
	if (!myfile) {
		printf("ERROR! Could not open input file.\n");
		return(-1);
	}

	// set flex to read from the source file instead of defaulting to STDIN:
	yyin = myfile;
	// parse the input file
	do {
		yyparse();
	} while (!feof(yyin));

	if (cc_options.print_ir){

		FILE *ir_file = fopen(cc_options.ir_file, "w");
		print_ir_code_to_file(ir_file);	//print the final ir_code to this file
		fclose(ir_file);

		debug_printSourceCode();		//debug function for printing the source file
		debug_printSymbolTable();		//debug function for printing the symbol table
		debug_print_all_codes();		//debug function for printing the intermediate code
	}

	// close the source file
	fclose(myfile);

	rm_cleanup_resources(&resource_mgr);

	return(0);
}
Exemple #2
0
/**
 * \brief Entry point.
 * \param argc The number of input parameters.
 * \param argv The input parameters.
 */
int main(int argc, char *argv[]) {

	/* 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);
	}

	if (cc_options.print_only_errors != 1) {
		printf("Input: %s\n", cc_options.input_file);
		printf("Output: %s\n", cc_options.output_file);
		printf("IR: %s\n", cc_options.ir_file);
	}

	yyin = fopen(cc_options.input_file, "r");
	if (!yyin) {
		fprintf(stderr,
				"Input file could not be opened for reading. Maybe, file does not exist?");
	} else {
		setSymbolTable(createSymbol());
		yyparse();
		fclose(yyin);
		irCode_t* ircode = NULL;

		if (cc_options.ir_file != NULL) {
			FILE *irFile;
			irFile = fopen(cc_options.ir_file, "w+");

			// Test symbolTable
			print_symTab(irFile);

			// get ir code and print it into irFile
			struct func_t *func, *tmp;
			HASH_ITER(hh, getSymbolTable()->symFunc, func, tmp) {
				if (func->symbol != NULL) {
					fprintf(irFile, "Function %s:\n", func->id);
					printIRCode(irFile, func->symbol->ircode);
				}
			}

			fclose(irFile);
		}

		yyout = fopen(cc_options.output_file, "w+");
		if (!yyout) {
			fprintf(stderr,
					"Output file could not be opened for writing. Maybe, file does not exist?");
		} else {
			int ret = mips32gen(yyout, ircode, getSymbolTable());
			if (ret != 0) {
				fprintf(stderr, "Error generating mips32 code with code: %d\n",
						ret);
			}
			fclose(yyout);
		}
	}
Exemple #3
0
/**
 * \brief Entry point.
 * \param argc The number of input parameters.
 * \param argv The input parameters.
 */
int main(int argc, char *argv[]) {
	/* 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);

	yyin = fopen(cc_options.input_file, "r");

	if (!yyin) {
		printf("FAIL");
		exit(1);
	}

	//  yyparse();

	do {
		yyparse();
	} while (!feof(yyin));

	fclose(yyin);
	printallstart(cc_options.output_file);
	if (cc_options.print_ir == 1) {
		FILE * ir_file = fopen(cc_options.ir_file, "w");
		ir_set_file(ir_file);
		generate_ir_code();
		fclose(ir_file);
	}

	rm_cleanup_resources(&resource_mgr);
	return 0;
}
Exemple #4
0
/** 
 * \brief Process compiler options.
 * \param argc The number of input parameters.
 * \param argv The input parameter strings.
 * \return Indicates if processing was successful. 
 *         0 = processing successful
 *         1 = processing not successful
 *
 * Processes the input options passed to the compiler
 * and fill the compiler options structure as appropriate.
 * 
 * The following options are supported:
 *  -h: print help
 *  -p: print the IR to a file
 *  -t: test modus, only success/failure log
 *  -o: the output file name (different from 'input'.o)
 */
int process_options(int argc, char *argv[]) {
	int opt;
	int ret = 0;

	/* add a handler to resource manager to free resources
	 * in the case of an error during option processing */
	rm_register_handler(&resource_mgr, free_options, NULL);

	while ((opt = getopt(argc, argv, "hpto:")) != -1) {
		switch (opt) {
		case 'p':
			cc_options.print_ir = 1;
			break;
		case 't':
			/* fewer logs, for automated testing */
			cc_options.print_only_errors = 1;
			break;
		case 'o':
			/* output file */
			cc_options.output_file = strdup(optarg);
			if (cc_options.output_file == NULL) {
				fatal_os_error(OUT_OF_MEMORY, 1, __FILE__, __LINE__, "");
				return 1;
			}
			break;
		case 'h':
			/* print help */
			print_usage(argv[0]);
			rm_cleanup_resources(&resource_mgr);
			exit(EXIT_SUCCESS);

		default: /* '?' */
			/* print usage */
			fprintf(stderr, "ERROR: unknown parameter: %s\n", argv[optind]);
			print_usage(argv[0]);
			return 1;
		}
	}

	if (optind >= argc) {
		fprintf(stderr, "ERROR: missing input file\n");
		print_usage(argv[0]);
		ret = 1;
	} else if (optind < argc - 1) {
		fprintf(stderr, "ERROR: too many input files\n");
		print_usage(argv[0]);
		ret = 1;
	} else {
		cc_options.input_file = strdup(argv[optind]);
		if (cc_options.input_file == NULL) {
			fatal_os_error(OUT_OF_MEMORY, 1, __FILE__, __LINE__, "");
			return 1;
		}

		char *filebase = get_file_basename(cc_options.input_file);
		;
		if (filebase == NULL) {
			return 1;
		}

		if (!has_file_extension(cc_options.input_file, C_EXT)) {
			fprintf(stderr, "ERROR: no C file (.c) as input\n");
			ret = 1;
		} else {
			/* The file name has a valid .c extension */
			if (cc_options.output_file == NULL) {
				/* create output file name <input>.o */
				cc_options.output_file = get_filename_with_ext(filebase,
						OUTPUT_EXT);
				if (cc_options.output_file == NULL) {
					ret = 1;
				}
			}
			if (cc_options.print_ir == 1) {
				/* create IR file name <input>.ir */
				cc_options.ir_file = get_filename_with_ext(filebase, IR_EXT);
				if (cc_options.ir_file == NULL) {
					ret = 1;
				}
			}
		}

		free(filebase);
	}

	return ret;
}
Exemple #5
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;
}