| [80219a8] | 1 | //
 | 
|---|
| [a2f146ee] | 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.
 | 
|---|
| [80219a8] | 6 | //
 | 
|---|
| [245a92c] | 7 | // as.c -- map assembler file, scan for debug information. If found, expand file by one character and insert Cforall
 | 
|---|
 | 8 | //         language code on the N line from the start of the debug information.
 | 
|---|
| [80219a8] | 9 | //
 | 
|---|
| [a2f146ee] | 10 | // Author           : Peter A. Buhr
 | 
|---|
 | 11 | // Created On       : Wed Aug  1 10:49:42 2018
 | 
|---|
 | 12 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [245a92c] | 13 | // Last Modified On : Sat Sep  8 08:40:16 2018
 | 
|---|
 | 14 | // Update Count     : 97
 | 
|---|
| [80219a8] | 15 | //
 | 
|---|
| [a2f146ee] | 16 | 
 | 
|---|
 | 17 | #include <cstdio>                                                                               // perror
 | 
|---|
| [0a76d84] | 18 | #include <cstdlib>                                                                              // exit
 | 
|---|
 | 19 | #include <fcntl.h>                                                                              // open
 | 
|---|
 | 20 | #include <unistd.h>
 | 
|---|
 | 21 | #include <sys/stat.h>
 | 
|---|
 | 22 | #include <sys/mman.h>                                                                   // mmap
 | 
|---|
 | 23 | #include <string.h>
 | 
|---|
| [a2f146ee] | 24 | 
 | 
|---|
 | 25 | //#define __DEBUG_H__
 | 
|---|
 | 26 | 
 | 
|---|
| [92f413c] | 27 | #ifdef __DEBUG_H__
 | 
|---|
 | 28 | #include <iostream>
 | 
|---|
 | 29 | using namespace std;
 | 
|---|
 | 30 | #endif // __DEBUG_H__
 | 
|---|
 | 31 | 
 | 
|---|
| [0a76d84] | 32 | int main( const int argc, const char * argv[] ) {
 | 
|---|
| [a2f146ee] | 33 |         #ifdef __DEBUG_H__
 | 
|---|
 | 34 |         for ( int i = 0; i < argc; i += 1 ) {
 | 
|---|
 | 35 |                 cerr << argv[i] << endl;
 | 
|---|
 | 36 |         } // for
 | 
|---|
 | 37 |         #endif // __DEBUG_H__
 | 
|---|
 | 38 | 
 | 
|---|
| [0a76d84] | 39 |         int fd = open( argv[argc - 1], O_RDWR );
 | 
|---|
 | 40 |         if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); };
 | 
|---|
| [a2f146ee] | 41 | 
 | 
|---|
| [0a76d84] | 42 |         struct stat mystat = {};
 | 
|---|
 | 43 |         if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); };
 | 
|---|
 | 44 |         off_t size = mystat.st_size;
 | 
|---|
| [a2f146ee] | 45 | 
 | 
|---|
| [245a92c] | 46 |         if ( size ) {                                                                           // cannot map 0 sized file
 | 
|---|
| [80219a8] | 47 |                 char * start = (char *)mmap( NULL, size + 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
 | 
|---|
| [245a92c] | 48 |                 if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); };
 | 
|---|
| [a2f146ee] | 49 | 
 | 
|---|
| [245a92c] | 50 |                 if ( char * cursor = strstr( start, ".Ldebug_info0:" ) ) { // debug information ?
 | 
|---|
 | 51 |                         // Expand file by one byte to hold 2 character Cforall language code.
 | 
|---|
 | 52 |                         if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); };
 | 
|---|
| [f7ac09d] | 53 | 
 | 
|---|
| [245a92c] | 54 |                         for ( int i = 0; i < 8; i += 1 ) {                      // move N (magic) lines forward
 | 
|---|
 | 55 |                                 cursor = strstr( cursor, "\n" ) + 1;
 | 
|---|
 | 56 |                         } // for
 | 
|---|
| [f7ac09d] | 57 | 
 | 
|---|
| [245a92c] | 58 |                         cursor -= 2;                                                            // backup over "c\n" language value
 | 
|---|
 | 59 |                         if ( *(cursor - 1) != 'x' ) { fprintf( stderr, "invalid C language code\n" ); exit( EXIT_FAILURE ); };
 | 
|---|
| [f7ac09d] | 60 | 
 | 
|---|
| [245a92c] | 61 |                         memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move remaining text 1 character right
 | 
|---|
| [f7ac09d] | 62 | 
 | 
|---|
| [245a92c] | 63 |                         *(cursor) = '2';                                                        // replace C language value with CFA
 | 
|---|
 | 64 |                         *(cursor + 1) = '5';
 | 
|---|
 | 65 |                 } // if
 | 
|---|
| [a2f146ee] | 66 | 
 | 
|---|
| [80219a8] | 67 |                 if ( munmap( start, size + 2 ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
 | 
|---|
| [245a92c] | 68 |         } // if
 | 
|---|
| [a2f146ee] | 69 | 
 | 
|---|
 | 70 |         argv[0] = "as";
 | 
|---|
 | 71 |         execvp( argv[0], (char * const *)argv );                        // should not return
 | 
|---|
 | 72 |         perror( "CFA Translator error: cpp level, execvp" );
 | 
|---|
 | 73 |         exit( EXIT_FAILURE );                                                           // tell gcc not to go any further
 | 
|---|
 | 74 | } // main
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | // Local Variables: //
 | 
|---|
 | 77 | // tab-width: 4 //
 | 
|---|
 | 78 | // mode: c++ //
 | 
|---|
 | 79 | // compile-command: "g++ -Wall -Wextra as.c -o as" //
 | 
|---|
 | 80 | // End: //
 | 
|---|