Changeset 0a76d84 for src/driver/as.cc
- Timestamp:
- Aug 2, 2018, 5:35:00 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- f7ac09d
- Parents:
- fde66c2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/driver/as.cc
rfde66c2 r0a76d84 10 10 // Created On : Wed Aug 1 10:49:42 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Aug 2 1 2:06:23201813 // Update Count : 7812 // Last Modified On : Thu Aug 2 16:18:14 2018 13 // Update Count : 84 14 14 // 15 15 16 #include <iostream>17 #include <fstream>18 using namespace std;19 16 #include <cstdio> // perror 20 #include <cstdlib> // exit, mkstemps 21 #include <unistd.h> // execvp, unlink 22 #include <csignal> // signal 17 #include <cstdlib> // exit 18 #include <fcntl.h> // open 19 #include <unistd.h> 20 #include <sys/stat.h> 21 #include <sys/mman.h> // mmap 22 #include <string.h> 23 23 24 24 //#define __DEBUG_H__ 25 25 26 27 #define SUFFIX ".s" 28 char tmpname[] = P_tmpdir "/CFAXXXXXX" SUFFIX; 29 int tmpfilefd = -1; 30 31 32 void rmfile( const char * fname ) { 33 if ( unlink( fname ) == -1 ) { // remove tmpname 34 perror ( "CFA Translator error: cpp failed" ); 35 exit( EXIT_FAILURE ); 36 } // if 37 } // rmtmpfile 38 39 40 void sigTermHandler( __attribute__((unused)) int signal ) { 41 if ( tmpfilefd != -1 ) { // RACE, file created ? 42 rmfile( tmpname ); // remove 43 exit( EXIT_FAILURE ); // terminate 44 } // if 45 tmpfilefd = -1; // mark closed 46 } // sigTermHandler 47 48 49 int main( const int argc, const char * argv[], __attribute__((unused)) const char * const env[] ) { 26 int main( const int argc, const char * argv[] ) { 50 27 #ifdef __DEBUG_H__ 51 for ( int i = 0; env[i] != NULL; i += 1 ) {52 cerr << env[i] << endl;53 } // for54 55 28 for ( int i = 0; i < argc; i += 1 ) { 56 29 cerr << argv[i] << endl; … … 58 31 #endif // __DEBUG_H__ 59 32 60 signal( SIGINT, sigTermHandler ); // try to delete temporary file on interrupt61 signal( SIGTERM, sigTermHandler );33 int fd = open( argv[argc - 1], O_RDWR ); 34 if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); }; 62 35 63 // Create a temporary file to store output of the modified assembler. 36 struct stat mystat = {}; 37 if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); }; 38 off_t size = mystat.st_size; 64 39 65 tmpfilefd = mkstemps( tmpname, sizeof( SUFFIX ) - 1 ); 66 if ( tmpfilefd == -1 ) { 67 perror( "CFA Translator error: cpp level, mkstemp" ); 68 exit( EXIT_FAILURE ); 69 } // if 70 FILE * tmpfilest = fdopen( tmpfilefd, "w" ); // wrap C stream (cannot wrap C++ stream) 40 if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); }; 71 41 72 #ifdef __DEBUG_H__ 73 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl; 74 #endif // __DEBUG_H__ 42 char * start = (char *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); 43 if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); }; 75 44 76 ifstream in; 77 in.exceptions( ios_base::failbit ); // use exceptions versus return codes 78 try { 79 in.open( argv[argc - 1] ); // assume .s file is last argument 80 } catch( ios_base::failure ) { 81 perror ( "CFA Translator error: cpp failed" ); 82 exit( EXIT_FAILURE ); 83 } // try 45 char * cursor = strstr( start, ".Ldebug_info0:" ); // debug information ? 46 for ( int i = 0; i < 8; i += 1 ) { // move N (magic) lines forward 47 cursor = strstr( cursor, "\n" ) + 1; 48 } // for 49 cursor -= 2; // backup over "c\n" language value 50 if ( *(cursor - 1) != 'x' ) { fprintf( stderr, "invalid C language code\n" ); exit( EXIT_FAILURE ); }; 51 memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move text 1 character right 52 *(cursor) = '2'; // replace with CFA language value 53 *(cursor + 1) = '5'; 84 54 85 try { 86 string line; 87 for ( ;; ) { // loop until end-of-file 88 getline( in, line ); 89 line += '\n'; 90 fprintf( tmpfilest, "%s", line.c_str() ); 91 if ( line == ".Ldebug_info0:\n" ) { // debug information ? 92 for ( int i = 0; i < 6; i += 1 ) { // move 6 lines forward 93 getline( in, line ); 94 line += '\n'; 95 fprintf( tmpfilest, "%s", line.c_str() ); 96 } // for 97 getline( in, line ); // ignore C language line 98 fprintf( tmpfilest, "%s", "\t.byte\t0x25\n" ); // replace with CFA language line 99 } // if 100 } // for 101 } catch ( ios_base::failure ) { 102 fclose( tmpfilest ); // flush buffers 103 } // try 104 105 rmfile( argv[argc - 1] ); // remove given file 106 107 if ( rename( tmpname, argv[argc - 1] ) == -1 ) { // rename temp file to original file 108 perror ( "CFA Translator error: cpp failed" ); 109 exit( EXIT_FAILURE ); 110 } // if 55 if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; 111 56 112 57 argv[0] = "as";
Note: See TracChangeset
for help on using the changeset viewer.