| [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 | // | 
|---|
| [a499702] | 7 | // as.c -- map assembler file, scan for debug information, then language code, and skip N lines forward to code. If | 
|---|
|  | 8 | //         code is C dialect, possibly expand file by one character, and replace with Cforall language code. | 
|---|
| [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 | 
|---|
| [d22bf87] | 13 | // Last Modified On : Tue Oct 24 20:45:06 2023 | 
|---|
|  | 14 | // Update Count     : 159 | 
|---|
| [80219a8] | 15 | // | 
|---|
| [a2f146ee] | 16 |  | 
|---|
|  | 17 | #include <cstdio>                                                                               // perror | 
|---|
| [0a76d84] | 18 | #include <cstdlib>                                                                              // exit | 
|---|
|  | 19 | #include <fcntl.h>                                                                              // open | 
|---|
| [88792f3] | 20 | #include <cstring>                                                                              // strstr, memmove | 
|---|
|  | 21 | #include <unistd.h>                                                                             // ftruncate,execvp | 
|---|
|  | 22 | #include <sys/stat.h>                                                                   // fstat | 
|---|
| [0a76d84] | 23 | #include <sys/mman.h>                                                                   // mmap | 
|---|
| [a2f146ee] | 24 |  | 
|---|
|  | 25 | //#define __DEBUG_H__ | 
|---|
|  | 26 |  | 
|---|
| [0a76d84] | 27 | int main( const int argc, const char * argv[] ) { | 
|---|
| [a2f146ee] | 28 | #ifdef __DEBUG_H__ | 
|---|
|  | 29 | for ( int i = 0; i < argc; i += 1 ) { | 
|---|
| [88792f3] | 30 | fprintf( stderr, "%s\n", argv[i] ); | 
|---|
| [a2f146ee] | 31 | } // for | 
|---|
|  | 32 | #endif // __DEBUG_H__ | 
|---|
|  | 33 |  | 
|---|
| [0a76d84] | 34 | int fd = open( argv[argc - 1], O_RDWR ); | 
|---|
|  | 35 | if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); }; | 
|---|
| [a2f146ee] | 36 |  | 
|---|
| [0a76d84] | 37 | struct stat mystat = {}; | 
|---|
|  | 38 | if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); }; | 
|---|
|  | 39 | off_t size = mystat.st_size; | 
|---|
| [a2f146ee] | 40 |  | 
|---|
| [1da2affb] | 41 | if ( size ) {                                                                           // cannot map 0 sized file | 
|---|
|  | 42 | char * start = (char *)mmap( NULL, size + 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); | 
|---|
|  | 43 | if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); }; | 
|---|
| [a2f146ee] | 44 |  | 
|---|
| [1da2affb] | 45 | char * dcursor; | 
|---|
|  | 46 | if ( (dcursor = strstr( start, ".Ldebug_info0:" ) ) ) { // debug information ? | 
|---|
| [d22bf87] | 47 | // fprintf( stderr, "found .Ldebug_info0:\n" ); | 
|---|
| [a310ae9] | 48 | #if defined( __i386 ) || defined( __x86_64 ) | 
|---|
| [1da2affb] | 49 | if ( char * cursor = strstr( dcursor, ".long\t.LASF" ) ) { // language code ? | 
|---|
| [d22bf87] | 50 | // fprintf( stderr, ".long\t.LASF\n" ); | 
|---|
| [1da2affb] | 51 | #elif defined( __aarch64__ ) | 
|---|
|  | 52 | if ( char * cursor = strstr( dcursor, ".4byte\t.LASF" ) ) { // language code ? | 
|---|
| [d22bf87] | 53 | // fprintf( stderr, ".4byte\t.LASF\n" ); | 
|---|
| [1da2affb] | 54 | #else | 
|---|
|  | 55 | #error unsupported architecture | 
|---|
|  | 56 | #endif | 
|---|
|  | 57 | for ( int i = 0; i < 2; i += 1 ) {              // move N (magic) lines forward | 
|---|
|  | 58 | cursor = strstr( cursor, "\n" ) + 1; | 
|---|
|  | 59 | } // for | 
|---|
|  | 60 | cursor -= 2;                                                    // backup over "d\n", where d is a hex digit | 
|---|
|  | 61 | // From elfcpp/dwarf.h in the binutils source tree. | 
|---|
|  | 62 | // DW_LANG_C89 = 0x1, DW_LANG_C = 0x2, DW_LANG_C99 = 0xc, DW_LANG_C11 = 0x1d | 
|---|
|  | 63 | if ( *(cursor - 2) == '0' && *(cursor - 1) == 'x' && | 
|---|
|  | 64 | (*cursor == 'c' || *cursor == '1' || *cursor == '2') ) { // C99/C89/C | 
|---|
| [d22bf87] | 65 | // fprintf( stderr, "language code C99/C89/C %c\n", *cursor ); | 
|---|
| [1da2affb] | 66 | // Expand file by one byte to hold 2 character Cforall language code. | 
|---|
|  | 67 | if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); }; | 
|---|
|  | 68 | memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move remaining text 1 character right | 
|---|
|  | 69 | } else if ( *(cursor - 3) == '0' && *(cursor - 2) == 'x' && *(cursor - 1) == '1' && *cursor == 'd' ) { // C11 | 
|---|
| [d22bf87] | 70 | // fprintf( stderr, "language code C11 %c\n", *cursor ); | 
|---|
| [1da2affb] | 71 | } else { | 
|---|
|  | 72 | for ( int i = 0; i < 6; i += 1 ) {      // move N (magic) lines forward | 
|---|
|  | 73 | cursor = strstr( cursor, "\n" ) + 1; | 
|---|
|  | 74 | } // for | 
|---|
|  | 75 | fprintf( stderr, "*** ERROR *** Invalid C language code found in assembler file: %s\n" | 
|---|
|  | 76 | "Assembler debug information:\n%.*s", | 
|---|
|  | 77 | argv[argc - 1], (int)(cursor - dcursor), dcursor ); | 
|---|
|  | 78 | exit( EXIT_FAILURE ); | 
|---|
|  | 79 | } // if | 
|---|
| [f7ac09d] | 80 |  | 
|---|
| [1da2affb] | 81 | *(cursor - 1) = '2';                                    // replace C89/C/C99/C11 language code with CFA code | 
|---|
|  | 82 | *cursor = '5'; | 
|---|
|  | 83 | } // if | 
|---|
|  | 84 | } // if | 
|---|
| [f7ac09d] | 85 |  | 
|---|
| [1da2affb] | 86 | if ( munmap( start, size + 2 ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk | 
|---|
|  | 87 | } // if | 
|---|
| [a2f146ee] | 88 |  | 
|---|
|  | 89 | argv[0] = "as"; | 
|---|
|  | 90 | execvp( argv[0], (char * const *)argv );                        // should not return | 
|---|
|  | 91 | perror( "CFA Translator error: cpp level, execvp" ); | 
|---|
|  | 92 | exit( EXIT_FAILURE );                                                           // tell gcc not to go any further | 
|---|
|  | 93 | } // main | 
|---|
|  | 94 |  | 
|---|
|  | 95 | // Local Variables: // | 
|---|
|  | 96 | // tab-width: 4 // | 
|---|
|  | 97 | // mode: c++ // | 
|---|
|  | 98 | // compile-command: "g++ -Wall -Wextra as.c -o as" // | 
|---|
|  | 99 | // End: // | 
|---|