blob: ec9965d3c3fcfa994b39ff369495cde394ab907e [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)2f171dd2020-07-09 20:03:55 -070046
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 */
52static inline bool
53src_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)5b7369d2020-07-05 02:16:13 -070058struct src_location_stack {
59 struct src_location l;
60 struct src_location_stack *up, *down;
61 const void *macro;
62};
63extern struct src_location_stack _src_top;
64extern struct src_location_stack *_src_bottom;
65extern struct src_location_stack *_src_error;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080066
H. Peter Anvin1aa79452018-12-10 23:00:10 -080067void src_init(void);
68void src_free(void);
69const char *src_set_fname(const char *newname);
H. Peter Anvin026b62f2018-12-10 23:28:01 -080070static inline const char *src_get_fname(void)
71{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070072 return _src_bottom->l.filename;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080073}
74static inline int32_t src_set_linnum(int32_t newline)
75{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070076 int32_t oldline = _src_bottom->l.lineno;
77 _src_bottom->l.lineno = newline;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080078 return oldline;
79}
80static inline int32_t src_get_linnum(void)
81{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -070082 return _src_bottom->l.lineno;
H. Peter Anvin026b62f2018-12-10 23:28:01 -080083}
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -070084
H. Peter Anvin1aa79452018-12-10 23:00:10 -080085/* Can be used when there is no need for the old information */
86void src_set(int32_t line, const char *filename);
H. Peter Anvin026b62f2018-12-10 23:28:01 -080087
H. Peter Anvin1aa79452018-12-10 23:00:10 -080088/*
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 Anvin026b62f2018-12-10 23:28:01 -080093 *
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 Anvin1aa79452018-12-10 23:00:10 -080097 */
H. Peter Anvin026b62f2018-12-10 23:28:01 -080098static 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)5b7369d2020-07-05 02:16:13 -0700102 int32_t line = _src_bottom->l.lineno;
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800103
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700104 *xline = line;
105 *xname = _src_bottom->l.filename;
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800106
107 /* The return value is expected to be optimized out almost everywhere */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700108 if (!xn || xn != _src_bottom->l.filename)
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800109 return -2;
110 else
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700111 return line - xl;
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800112}
113
114/*
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700115 * Returns the current information as a structure.
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800116 */
117static inline struct src_location src_where(void)
118{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700119 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 */
126static 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 */
136static inline struct src_location src_where_error(void)
137{
138 return _src_error->l;
139}
140static 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}
149static inline void src_error_reset(void)
150{
151 _src_error = &_src_top;
H. Peter Anvin026b62f2018-12-10 23:28:01 -0800152}
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700153
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 */
159static inline struct src_location src_update(struct src_location whence)
160{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700161 struct src_location old = _src_bottom->l;
162 _src_bottom->l = whence;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700163 return old;
164}
H. Peter Anvin1aa79452018-12-10 23:00:10 -0800165
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700166/*
167 * Push/pop macro expansion level. "macroname" must remain constant at
168 * least until the same macro expansion level is popped.
169 */
170void src_macro_push(const void *macroname, struct src_location where);
171static inline const void *src_macro_current(void)
172{
173 return _src_bottom->macro;
174}
175void src_macro_pop(void);
176
H. Peter Anvin1aa79452018-12-10 23:00:10 -0800177#endif /* ASM_SRCFILE_H */