source: src/driver/as.cc @ a2f146ee

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

preprocess assembler files to add CFA language

  • Property mode set to 100644
File size: 3.5 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 12:06:23 2018
13// Update Count     : 78
14//
15
16#include <iostream>
17#include <fstream>
18using namespace std;
19#include <cstdio>                                                                               // perror
20#include <cstdlib>                                                                              // exit, mkstemps
21#include <unistd.h>                                                                             // execvp, unlink
22#include <csignal>                                                                              // signal
23
24//#define __DEBUG_H__
25
26
27#define SUFFIX ".s"
28char tmpname[] = P_tmpdir "/CFAXXXXXX" SUFFIX;
29int tmpfilefd = -1;
30
31
32void rmfile( const char * fname ) {
33        if ( unlink( fname ) == -1 ) {                                          // remove tmpname
34                perror ( "CFA Translator error: cpp failed" );
35                exit( EXIT_FAILURE );
36        } // if
37} // rmtmpfile
38
39
40void sigTermHandler( __attribute__((unused)) int signal ) {
41        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
42                rmfile( tmpname );                                                              // remove
43                exit( EXIT_FAILURE );                                                   // terminate
44        } // if
45        tmpfilefd = -1;                                                                         // mark closed
46} // sigTermHandler
47
48
49int main( const int argc, const char * argv[], __attribute__((unused)) const char * const env[] ) {
50        #ifdef __DEBUG_H__
51        for ( int i = 0; env[i] != NULL; i += 1 ) {
52                cerr << env[i] << endl;
53        } // for
54
55        for ( int i = 0; i < argc; i += 1 ) {
56                cerr << argv[i] << endl;
57        } // for
58        #endif // __DEBUG_H__
59
60        signal( SIGINT,  sigTermHandler );                                      // try to delete temporary file on interrupt
61        signal( SIGTERM, sigTermHandler );
62
63        // Create a temporary file to store output of the modified assembler.
64
65        tmpfilefd = mkstemps( tmpname, sizeof( SUFFIX ) - 1 );
66        if ( tmpfilefd == -1 ) {
67                perror( "CFA Translator error: cpp level, mkstemp" );
68                exit( EXIT_FAILURE );
69        } // if
70        FILE * tmpfilest = fdopen( tmpfilefd, "w" );            // wrap C stream (cannot wrap C++ stream)
71
72        #ifdef __DEBUG_H__
73        cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
74        #endif // __DEBUG_H__
75
76        ifstream in;
77        in.exceptions( ios_base::failbit );                                     // use exceptions versus return codes
78        try {
79                in.open( argv[argc - 1] );                                              // assume .s file is last argument
80        } catch( ios_base::failure ) {
81                perror ( "CFA Translator error: cpp failed" );
82                exit( EXIT_FAILURE );
83        } // try
84
85        try {
86                string line;
87                for ( ;; ) {                                                                    // loop until end-of-file
88                        getline( in, line );
89                        line += '\n';
90                        fprintf( tmpfilest, "%s", line.c_str() );
91                        if ( line == ".Ldebug_info0:\n" ) {                     // debug information ?
92                                for ( int i = 0; i < 6; i += 1 ) {              // move 6 lines forward
93                                        getline( in, line );
94                                        line += '\n';
95                                        fprintf( tmpfilest, "%s", line.c_str() );
96                                } // for
97                                getline( in, line );                                    // ignore C language line
98                                fprintf( tmpfilest, "%s", "\t.byte\t0x25\n" ); // replace with CFA language line
99                        } // if
100                } // for
101        } catch ( ios_base::failure ) {
102                fclose( tmpfilest );                                                    // flush buffers
103        } // try
104
105        rmfile( argv[argc - 1] );                                                       // remove given file
106
107        if ( rename( tmpname, argv[argc - 1] ) == -1 ) {        // rename temp file to original file
108                perror ( "CFA Translator error: cpp failed" );
109                exit( EXIT_FAILURE );
110        } // if
111
112        argv[0] = "as";
113        execvp( argv[0], (char * const *)argv );                        // should not return
114        perror( "CFA Translator error: cpp level, execvp" );
115        exit( EXIT_FAILURE );                                                           // tell gcc not to go any further
116} // main
117
118// Local Variables: //
119// tab-width: 4 //
120// mode: c++ //
121// compile-command: "g++ -Wall -Wextra as.c -o as" //
122// End: //
Note: See TracBrowser for help on using the repository browser.