H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 3 | * Copyright 1996-2020 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * These functions are used to keep track of the source code file and name. |
| 36 | */ |
| 37 | #ifndef ASM_SRCFILE_H |
| 38 | #define ASM_SRCFILE_H |
| 39 | |
| 40 | #include "compiler.h" |
| 41 | |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 42 | struct src_location { |
| 43 | const char *filename; |
| 44 | int32_t lineno; |
| 45 | }; |
H. Peter Anvin (Intel) | 2f171dd | 2020-07-09 20:03:55 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Comparing the *pointer value* of filenames is valid, because the |
| 49 | * filename hash system guarantees that each unique filename string is |
| 50 | * permanently allocated in exactly one location. |
| 51 | */ |
| 52 | static inline bool |
| 53 | src_location_same(struct src_location here, struct src_location there) |
| 54 | { |
| 55 | return here.filename == there.filename && here.lineno == there.lineno; |
| 56 | } |
| 57 | |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 58 | struct src_location_stack { |
| 59 | struct src_location l; |
| 60 | struct src_location_stack *up, *down; |
| 61 | const void *macro; |
| 62 | }; |
| 63 | extern struct src_location_stack _src_top; |
| 64 | extern struct src_location_stack *_src_bottom; |
| 65 | extern struct src_location_stack *_src_error; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 66 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 67 | void src_init(void); |
| 68 | void src_free(void); |
| 69 | const char *src_set_fname(const char *newname); |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 70 | static inline const char *src_get_fname(void) |
| 71 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 72 | return _src_bottom->l.filename; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 73 | } |
| 74 | static inline int32_t src_set_linnum(int32_t newline) |
| 75 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 76 | int32_t oldline = _src_bottom->l.lineno; |
| 77 | _src_bottom->l.lineno = newline; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 78 | return oldline; |
| 79 | } |
| 80 | static inline int32_t src_get_linnum(void) |
| 81 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 82 | return _src_bottom->l.lineno; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 83 | } |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 84 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 85 | /* Can be used when there is no need for the old information */ |
| 86 | void src_set(int32_t line, const char *filename); |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 87 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 88 | /* |
| 89 | * src_get gets both the source file name and line. |
| 90 | * It is also used if you maintain private status about the source location |
| 91 | * It return 0 if the information was the same as the last time you |
| 92 | * checked, -2 if the name changed and (new-old) if just the line changed. |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 93 | * |
| 94 | * xname must point to a filename string previously returned from any |
| 95 | * function of this subsystem or be NULL; another string value will |
| 96 | * not work. |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 97 | */ |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 98 | static inline int32_t src_get(int32_t *xline, const char **xname) |
| 99 | { |
| 100 | const char *xn = *xname; |
| 101 | int32_t xl = *xline; |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 102 | int32_t line = _src_bottom->l.lineno; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 103 | |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 104 | *xline = line; |
| 105 | *xname = _src_bottom->l.filename; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 106 | |
| 107 | /* The return value is expected to be optimized out almost everywhere */ |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 108 | if (!xn || xn != _src_bottom->l.filename) |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 109 | return -2; |
| 110 | else |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 111 | return line - xl; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 115 | * Returns the current information as a structure. |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 116 | */ |
| 117 | static inline struct src_location src_where(void) |
| 118 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 119 | return _src_bottom->l; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Returns the top-level information as a structure. Use this for panic |
| 124 | * errors, since descent is not possible there. |
| 125 | */ |
| 126 | static inline struct src_location src_where_top(void) |
| 127 | { |
| 128 | return _src_top.l; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Returns the appropriate level of the location stack to use for error |
| 133 | * messages. This is the same as the top level except during the descent |
| 134 | * through the macro hierarchy for elucidation; |
| 135 | */ |
| 136 | static inline struct src_location src_where_error(void) |
| 137 | { |
| 138 | return _src_error->l; |
| 139 | } |
| 140 | static inline const void *src_error_down(void) |
| 141 | { |
| 142 | if (_src_error->down) { |
| 143 | _src_error = _src_error->down; |
| 144 | return _src_error->macro; |
| 145 | } else { |
| 146 | return NULL; |
| 147 | } |
| 148 | } |
| 149 | static inline void src_error_reset(void) |
| 150 | { |
| 151 | _src_error = &_src_top; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 152 | } |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Sets the current information. The filename member of the structure |
| 156 | * *must* have been previously returned by src_get(), src_where(), or |
| 157 | * src_get_fname() and therefore be present in the hash. |
| 158 | */ |
| 159 | static inline struct src_location src_update(struct src_location whence) |
| 160 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 161 | struct src_location old = _src_bottom->l; |
| 162 | _src_bottom->l = whence; |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 163 | return old; |
| 164 | } |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 165 | |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 166 | /* |
| 167 | * Push/pop macro expansion level. "macroname" must remain constant at |
| 168 | * least until the same macro expansion level is popped. |
| 169 | */ |
| 170 | void src_macro_push(const void *macroname, struct src_location where); |
| 171 | static inline const void *src_macro_current(void) |
| 172 | { |
| 173 | return _src_bottom->macro; |
| 174 | } |
| 175 | void src_macro_pop(void); |
| 176 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 177 | #endif /* ASM_SRCFILE_H */ |