1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// This file contains the print functionality the JIT code can use. The backend
18// can call function to emit a global var that points to the host side struct
19// containing fopen/fprintf/fclose pointers, JIT code can call instead of it's
20// own fopen/fprintf/fclose.
21
22#include <condition_variable>
23#include <cstdarg>
24#include <unordered_map>
25
26/// Structure implements file I/O related functions. Currently,
27/// it just maps C-lib fopen/fprintf/fclose onto it's members.
28struct JITFileWriterIF {
29 FILE *(*fopen)(const char *, const char *);
30 int (*fprintf)(FILE *, const char *, ...);
31 int (*fclose)(FILE *);
32};
33
34struct JITFileWriter {
35 JITFileWriterIF IF;
36 /// Open a file. Operation, arguments and return value match standard fopen
37 /// function.
38 static FILE *JITfopen(const char *, const char *);
39 /// Writes the C string pointed by format to the stream. Operation, arguments
40 /// and return value match standard fprintf function.
41 static int JITfprintf(FILE *, const char *, ...);
42 /// Close a file. Operation, arguments and return value match standard fclose
43 /// function.
44 static int JITfclose(FILE *);
45 JITFileWriter() {
46 IF.fopen = &JITfopen;
47 IF.fprintf = &JITfprintf;
48 IF.fclose = &JITfclose;
49 }
50 std::unordered_map<std::string, bool> fileLock_;
51 std::unordered_map<FILE *, std::string> fileMap_;
52 std::condition_variable readyCV;
53 std::mutex mtx_;
54};
55