1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
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 
18 /** \file state.h
19     \brief macros to read/write the 'state' file
20 */
21 
22 /*
23  * to save/restore an array to the state file:
24  *   RW_FD( address, datatype, number-elements )
25  */
26 #define RW_FD(b, s, n)                           \
27   {                                              \
28     nw = (*p_rw)((char *)(b), sizeof(s), n, fd); \
29     if (nw != (n))                               \
30       error(10, 40, 0, "(state file)", CNULL);   \
31   }
32 
33 /*
34  * to save/restore a scalar to the state file:
35  *   RW_SCALAR( variable )
36  */
37 #define RW_SCALAR(b) RW_FD(&b, b, 1)
38 
39 /*
40  * the rw_routine should be declared:
41  *   void rw_routine( RW_ROUTINE, RW_FILE )
42  * this declares the proper names for the rw_routine and file
43  * used in the RW_ macros
44  */
45 #define RW_ROUTINE int (*p_rw)(void *, size_t, size_t, FILE *)
46 #define RW_FILE FILE *fd
47 #define RW_ROUTINE_TYPE int (*)(void *, size_t, size_t, FILE *)
48 
49 /*
50  * sometimes special action is taken on read or write.
51  * use these macros to test whether this is a write (save) or read (restore)
52  */
53 #define ISREAD() (p_rw == (RW_ROUTINE_TYPE)fread)
54 #define ISWRITE() (p_rw == (RW_ROUTINE_TYPE)fwrite)
55 
56 extern void rw_dpmout_state(RW_ROUTINE, RW_FILE);
57 extern void rw_semant_state(RW_ROUTINE, RW_FILE); /* semfin.c */
58 extern void rw_gnr_state(RW_ROUTINE, RW_FILE);    /* semgnr.c */
59 extern void rw_sym_state(RW_ROUTINE, RW_FILE);    /* symtab.c */
60 extern void rw_dtype_state(int (*p_rw)(void *, size_t, size_t, FILE *),
61                            FILE *fd);             /* dtypeutl.c */
62 extern void rw_ast_state(RW_ROUTINE, RW_FILE);    /* ast.c */
63 extern void rw_dinit_state(RW_ROUTINE, RW_FILE);  /* dinit.c */
64 extern void rw_import_state(RW_ROUTINE, RW_FILE); /* interf.c */
65 extern void rw_mod_state(RW_ROUTINE, RW_FILE);    /* module.c */
66