blob: a26e1e4fa041eea978e462fb83c06eb7dab82c97 [file] [log] [blame]
H. Peter Anvin1aa79452018-12-10 23:00:10 -08001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
H. Peter Anvin1aa79452018-12-10 23:00:10 -08004 * 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 Anvin026b62f2018-12-10 23:28:01 -080042struct src_location {
43 const char *filename;
44 int32_t lineno;
45};
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070046struct src_location_stack {
47 struct src_location l;
48 struct src_location_stack *up, *down;
49 const void *macro;
50};
51extern struct src_location_stack _src_top;
52extern struct src_location_stack *_src_bottom;
53extern struct src_location_stack *_src_error;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080054
H. Peter Anvin1aa79452018-12-10 23:00:10 -080055void src_init(void);
56void src_free(void);
57const char *src_set_fname(const char *newname);
H. Peter Anvin026b62f2018-12-10 23:28:01 -080058static inline const char *src_get_fname(void)
59{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070060 return _src_bottom->l.filename;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080061}
62static inline int32_t src_set_linnum(int32_t newline)
63{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070064 int32_t oldline = _src_bottom->l.lineno;
65 _src_bottom->l.lineno = newline;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080066 return oldline;
67}
68static inline int32_t src_get_linnum(void)
69{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070070 return _src_bottom->l.lineno;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080071}
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -070072
H. Peter Anvin1aa79452018-12-10 23:00:10 -080073/* Can be used when there is no need for the old information */
74void src_set(int32_t line, const char *filename);
H. Peter Anvin026b62f2018-12-10 23:28:01 -080075
H. Peter Anvin1aa79452018-12-10 23:00:10 -080076/*
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 Anvin026b62f2018-12-10 23:28:01 -080081 *
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 Anvin1aa79452018-12-10 23:00:10 -080085 */
H. Peter Anvin026b62f2018-12-10 23:28:01 -080086static 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)5b7369d2020-07-05 02:16:13 -070090 int32_t line = _src_bottom->l.lineno;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080091
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070092 *xline = line;
93 *xname = _src_bottom->l.filename;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080094
95 /* The return value is expected to be optimized out almost everywhere */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070096 if (!xn || xn != _src_bottom->l.filename)
H. Peter Anvin026b62f2018-12-10 23:28:01 -080097 return -2;
98 else
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070099 return line - xl;
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800100}
101
102/*
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700103 * Returns the current information as a structure.
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800104 */
105static inline struct src_location src_where(void)
106{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700107 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 */
114static 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 */
124static inline struct src_location src_where_error(void)
125{
126 return _src_error->l;
127}
128static 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}
137static inline void src_error_reset(void)
138{
139 _src_error = &_src_top;
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800140}
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700141
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 */
147static inline struct src_location src_update(struct src_location whence)
148{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700149 struct src_location old = _src_bottom->l;
150 _src_bottom->l = whence;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700151 return old;
152}
H. Peter Anvin1aa79452018-12-10 23:00:10 -0800153
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700154/*
155 * Push/pop macro expansion level. "macroname" must remain constant at
156 * least until the same macro expansion level is popped.
157 */
158void src_macro_push(const void *macroname, struct src_location where);
159static inline const void *src_macro_current(void)
160{
161 return _src_bottom->macro;
162}
163void src_macro_pop(void);
164
H. Peter Anvin1aa79452018-12-10 23:00:10 -0800165#endif /* ASM_SRCFILE_H */