[a2f146ee] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // as.c --
|
---|
| 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Wed Aug 1 10:49:42 2018
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[f7ac09d] | 12 | // Last Modified On : Thu Aug 2 17:50:09 2018
|
---|
| 13 | // Update Count : 90
|
---|
[a2f146ee] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include <cstdio> // perror
|
---|
[0a76d84] | 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>
|
---|
[a2f146ee] | 23 |
|
---|
| 24 | //#define __DEBUG_H__
|
---|
| 25 |
|
---|
[0a76d84] | 26 | int main( const int argc, const char * argv[] ) {
|
---|
[a2f146ee] | 27 | #ifdef __DEBUG_H__
|
---|
| 28 | for ( int i = 0; i < argc; i += 1 ) {
|
---|
| 29 | cerr << argv[i] << endl;
|
---|
| 30 | } // for
|
---|
| 31 | #endif // __DEBUG_H__
|
---|
| 32 |
|
---|
[0a76d84] | 33 | int fd = open( argv[argc - 1], O_RDWR );
|
---|
| 34 | if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); };
|
---|
[a2f146ee] | 35 |
|
---|
[0a76d84] | 36 | struct stat mystat = {};
|
---|
| 37 | if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); };
|
---|
| 38 | off_t size = mystat.st_size;
|
---|
[a2f146ee] | 39 |
|
---|
[0a76d84] | 40 | char * start = (char *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
|
---|
| 41 | if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); };
|
---|
[a2f146ee] | 42 |
|
---|
[f7ac09d] | 43 | if ( char * cursor = strstr( start, ".Ldebug_info0:" ) ) { // debug information ?
|
---|
| 44 | // Expand file by one byte to hold 2 character Cforall language code.
|
---|
| 45 | if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); };
|
---|
| 46 |
|
---|
| 47 | for ( int i = 0; i < 8; i += 1 ) { // move N (magic) lines forward
|
---|
| 48 | cursor = strstr( cursor, "\n" ) + 1;
|
---|
| 49 | } // for
|
---|
| 50 |
|
---|
| 51 | cursor -= 2; // backup over "c\n" language value
|
---|
| 52 | if ( *(cursor - 1) != 'x' ) { fprintf( stderr, "invalid C language code\n" ); exit( EXIT_FAILURE ); };
|
---|
| 53 |
|
---|
| 54 | memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move remaining text 1 character right
|
---|
| 55 |
|
---|
| 56 | *(cursor) = '2'; // replace C language value with CFA
|
---|
| 57 | *(cursor + 1) = '5';
|
---|
| 58 | } // if
|
---|
[a2f146ee] | 59 |
|
---|
[f7ac09d] | 60 | if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
|
---|
[a2f146ee] | 61 |
|
---|
| 62 | argv[0] = "as";
|
---|
| 63 | execvp( argv[0], (char * const *)argv ); // should not return
|
---|
| 64 | perror( "CFA Translator error: cpp level, execvp" );
|
---|
| 65 | exit( EXIT_FAILURE ); // tell gcc not to go any further
|
---|
| 66 | } // main
|
---|
| 67 |
|
---|
| 68 | // Local Variables: //
|
---|
| 69 | // tab-width: 4 //
|
---|
| 70 | // mode: c++ //
|
---|
| 71 | // compile-command: "g++ -Wall -Wextra as.c -o as" //
|
---|
| 72 | // End: //
|
---|