source: driver/as.cc @ 130a778

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 130a778 was 130a778, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add debug C11 code to assembler check

  • Property mode set to 100644
File size: 3.3 KB
Line 
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 -- 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.
9//
10// Author           : Peter A. Buhr
11// Created On       : Wed Aug  1 10:49:42 2018
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Tue Dec  7 20:49:15 2021
14// Update Count     : 121
15//
16
17#include <cstdio>                                                                               // perror
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>
24
25//#define __DEBUG_H__
26
27#ifdef __DEBUG_H__
28#include <iostream>
29using namespace std;
30#endif // __DEBUG_H__
31
32int main( const int argc, const char * argv[] ) {
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
39        int fd = open( argv[argc - 1], O_RDWR );
40        if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); };
41
42        struct stat mystat = {};
43        if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); };
44        off_t size = mystat.st_size;
45
46        if ( size ) {                                                                           // cannot map 0 sized file
47                char * start = (char *)mmap( NULL, size + 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
48                if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); };
49
50                char * dcursor;
51                if ( (dcursor = strstr( start, ".Ldebug_info0:" ) ) ) { // debug information ?
52
53                        if ( char * cursor = strstr( dcursor, ".long\t.LASF" ) ) { // language code ?
54                                for ( int i = 0; i < 2; i += 1 ) {              // move N (magic) lines forward
55                                        cursor = strstr( cursor, "\n" ) + 1;
56                                } // for
57                                cursor -= 1;                                                    // backup over '\n'
58                                if ( (*(cursor - 3) == '0' && *(cursor - 2) == 'x' && *(cursor - 1) == 'c') ) { // 0xc => C99
59                                        // Expand file by one byte to hold 2 character Cforall language code.
60                                        if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); };
61                                        memmove( cursor + 1, cursor, start + size - cursor ); // move remaining text 1 character right
62                                } else if ( *(cursor - 4) == '0' && *(cursor - 3) == 'x' && *(cursor - 2) == '1' && *(cursor - 1) == 'd' ) { // 0x1d => C11
63                                        // replace "1d" with "25"
64                                } else {
65                                        for ( int i = 0; i < 6; i += 1 ) {      // move N (magic) lines forward
66                                                cursor = strstr( cursor, "\n" ) + 1;
67                                        } // for
68                                        fprintf( stderr, "*** ERROR *** Invalid C language code found in assembler file: %s\n"
69                                                         "Assembler debug information:\n%.*s",
70                                                         argv[argc - 1], (int)(cursor - dcursor), dcursor );
71                                        exit( EXIT_FAILURE );
72                                } // if
73
74                                *(cursor - 2) = '2';                                    // replace C/C11 language value with CFA
75                                *(cursor - 1) = '5';
76                        } // if
77                } // if
78
79                if ( munmap( start, size + 2 ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
80        } // if
81
82        argv[0] = "as";
83        execvp( argv[0], (char * const *)argv );                        // should not return
84        perror( "CFA Translator error: cpp level, execvp" );
85        exit( EXIT_FAILURE );                                                           // tell gcc not to go any further
86} // main
87
88// Local Variables: //
89// tab-width: 4 //
90// mode: c++ //
91// compile-command: "g++ -Wall -Wextra as.c -o as" //
92// End: //
Note: See TracBrowser for help on using the repository browser.