source: src/driver/as.cc @ 0a76d84

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

second version of add CFA language

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