source: driver/as.cc@ bcb14b5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since bcb14b5 was 92f413c, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update debugging

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[a2f146ee]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
[92f413c]12// Last Modified On : Wed Aug 22 17:30:24 2018
13// Update Count : 93
[a2f146ee]14//
15
16#include <cstdio> // perror
[0a76d84]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>
[a2f146ee]23
24//#define __DEBUG_H__
25
[92f413c]26#ifdef __DEBUG_H__
27#include <iostream>
28using namespace std;
29#endif // __DEBUG_H__
30
[0a76d84]31int main( const int argc, const char * argv[] ) {
[a2f146ee]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
[0a76d84]38 int fd = open( argv[argc - 1], O_RDWR );
39 if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); };
[a2f146ee]40
[0a76d84]41 struct stat mystat = {};
42 if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); };
43 off_t size = mystat.st_size;
[a2f146ee]44
[0a76d84]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 ); };
[a2f146ee]47
[f7ac09d]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
[a2f146ee]64
[f7ac09d]65 if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
[a2f146ee]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.