source: driver/as.cc @ b740f0b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since b740f0b was 92f413c, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

update debugging

  • Property mode set to 100644
File size: 2.4 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 --
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 : Wed Aug 22 17:30:24 2018
13// Update Count     : 93
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#ifdef __DEBUG_H__
27#include <iostream>
28using namespace std;
29#endif // __DEBUG_H__
30
31int main( const int argc, const char * argv[] ) {
32        #ifdef __DEBUG_H__
33        for ( int i = 0; i < argc; i += 1 ) {
34                cerr << argv[i] << endl;
35        } // for
36        #endif // __DEBUG_H__
37
38        int fd = open( argv[argc - 1], O_RDWR );
39        if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); };
40
41        struct stat mystat = {};
42        if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); };
43        off_t size = mystat.st_size;
44
45        char * start = (char *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
46        if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); };
47
48        if ( char * cursor = strstr( start, ".Ldebug_info0:" ) ) { // debug information ?
49                // Expand file by one byte to hold 2 character Cforall language code.
50                if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); };
51
52                for ( int i = 0; i < 8; i += 1 ) {                              // move N (magic) lines forward
53                        cursor = strstr( cursor, "\n" ) + 1;
54                } // for
55
56                cursor -= 2;                                                                    // backup over "c\n" language value
57                if ( *(cursor - 1) != 'x' ) { fprintf( stderr, "invalid C language code\n" ); exit( EXIT_FAILURE ); };
58
59                memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move remaining text 1 character right
60
61                *(cursor) = '2';                                                                // replace C language value with CFA
62                *(cursor + 1) = '5';
63        } // if
64
65        if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
66
67        argv[0] = "as";
68        execvp( argv[0], (char * const *)argv );                        // should not return
69        perror( "CFA Translator error: cpp level, execvp" );
70        exit( EXIT_FAILURE );                                                           // tell gcc not to go any further
71} // main
72
73// Local Variables: //
74// tab-width: 4 //
75// mode: c++ //
76// compile-command: "g++ -Wall -Wextra as.c -o as" //
77// End: //
Note: See TracBrowser for help on using the repository browser.