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
|
---|
12 | // Last Modified On : Thu Aug 2 17:50:09 2018
|
---|
13 | // Update Count : 90
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cstdio> // perror
|
---|
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 |
|
---|
24 | //#define __DEBUG_H__
|
---|
25 |
|
---|
26 | int main( const int argc, const char * argv[] ) {
|
---|
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 |
|
---|
33 | int fd = open( argv[argc - 1], O_RDWR );
|
---|
34 | if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); };
|
---|
35 |
|
---|
36 | struct stat mystat = {};
|
---|
37 | if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); };
|
---|
38 | off_t size = mystat.st_size;
|
---|
39 |
|
---|
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 ); };
|
---|
42 |
|
---|
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
|
---|
59 |
|
---|
60 | if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
|
---|
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: //
|
---|