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) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 46 | struct src_location_stack { |
| 47 | struct src_location l; |
| 48 | struct src_location_stack *up, *down; |
| 49 | const void *macro; |
| 50 | }; |
| 51 | extern struct src_location_stack _src_top; |
| 52 | extern struct src_location_stack *_src_bottom; |
| 53 | extern struct src_location_stack *_src_error; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 54 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 55 | void src_init(void); |
| 56 | void src_free(void); |
| 57 | const char *src_set_fname(const char *newname); |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 58 | static inline const char *src_get_fname(void) |
| 59 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 60 | return _src_bottom->l.filename; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 61 | } |
| 62 | static inline int32_t src_set_linnum(int32_t newline) |
| 63 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 64 | int32_t oldline = _src_bottom->l.lineno; |
| 65 | _src_bottom->l.lineno = newline; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 66 | return oldline; |
| 67 | } |
| 68 | static inline int32_t src_get_linnum(void) |
| 69 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 70 | return _src_bottom->l.lineno; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 71 | } |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 72 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 73 | /* Can be used when there is no need for the old information */ |
| 74 | void src_set(int32_t line, const char *filename); |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 75 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 76 | /* |
| 77 | * src_get gets both the source file name and line. |
| 78 | * It is also used if you maintain private status about the source location |
| 79 | * It return 0 if the information was the same as the last time you |
| 80 | * 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] | 81 | * |
| 82 | * xname must point to a filename string previously returned from any |
| 83 | * function of this subsystem or be NULL; another string value will |
| 84 | * not work. |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 85 | */ |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 86 | static inline int32_t src_get(int32_t *xline, const char **xname) |
| 87 | { |
| 88 | const char *xn = *xname; |
| 89 | int32_t xl = *xline; |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 90 | int32_t line = _src_bottom->l.lineno; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 91 | |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 92 | *xline = line; |
| 93 | *xname = _src_bottom->l.filename; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 94 | |
| 95 | /* 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] | 96 | if (!xn || xn != _src_bottom->l.filename) |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 97 | return -2; |
| 98 | else |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 99 | return line - xl; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 103 | * Returns the current information as a structure. |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 104 | */ |
| 105 | static inline struct src_location src_where(void) |
| 106 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 107 | return _src_bottom->l; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Returns the top-level information as a structure. Use this for panic |
| 112 | * errors, since descent is not possible there. |
| 113 | */ |
| 114 | static inline struct src_location src_where_top(void) |
| 115 | { |
| 116 | return _src_top.l; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Returns the appropriate level of the location stack to use for error |
| 121 | * messages. This is the same as the top level except during the descent |
| 122 | * through the macro hierarchy for elucidation; |
| 123 | */ |
| 124 | static inline struct src_location src_where_error(void) |
| 125 | { |
| 126 | return _src_error->l; |
| 127 | } |
| 128 | static inline const void *src_error_down(void) |
| 129 | { |
| 130 | if (_src_error->down) { |
| 131 | _src_error = _src_error->down; |
| 132 | return _src_error->macro; |
| 133 | } else { |
| 134 | return NULL; |
| 135 | } |
| 136 | } |
| 137 | static inline void src_error_reset(void) |
| 138 | { |
| 139 | _src_error = &_src_top; |
H. Peter Anvin | 026b62f | 2018-12-10 23:28:01 -0800 | [diff] [blame] | 140 | } |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Sets the current information. The filename member of the structure |
| 144 | * *must* have been previously returned by src_get(), src_where(), or |
| 145 | * src_get_fname() and therefore be present in the hash. |
| 146 | */ |
| 147 | static inline struct src_location src_update(struct src_location whence) |
| 148 | { |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 149 | struct src_location old = _src_bottom->l; |
| 150 | _src_bottom->l = whence; |
H. Peter Anvin (Intel) | b292748 | 2020-06-14 20:09:11 -0700 | [diff] [blame] | 151 | return old; |
| 152 | } |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 153 | |
H. Peter Anvin (Intel) | 5b7369d | 2020-07-05 02:16:13 -0700 | [diff] [blame] | 154 | /* |
| 155 | * Push/pop macro expansion level. "macroname" must remain constant at |
| 156 | * least until the same macro expansion level is popped. |
| 157 | */ |
| 158 | void src_macro_push(const void *macroname, struct src_location where); |
| 159 | static inline const void *src_macro_current(void) |
| 160 | { |
| 161 | return _src_bottom->macro; |
| 162 | } |
| 163 | void src_macro_pop(void); |
| 164 | |
H. Peter Anvin | 1aa7945 | 2018-12-10 23:00:10 -0800 | [diff] [blame] | 165 | #endif /* ASM_SRCFILE_H */ |