summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2018-12-10 23:28:01 -0800
committerH. Peter Anvin <hpa@zytor.com>2018-12-10 23:28:01 -0800
commit026b62f264b86372cc596e5a035f87385931486a (patch)
tree3d40e9239e47e8db93d9140efc6140bc7dc1d8de
parent1aa794527fbc772cbc0a4c89d53bbd5d83af914a (diff)
downloadnasm-026b62f264b86372cc596e5a035f87385931486a.tar.gz
srcfile: simplify and make most functions inline
Simplify the srcfile subsystem by making it official that any pointer passed to src_get() needs to have been obtained from the srcfile subsystem itself. Move a lot of the srcfile operations into inline code; often they amount to a single machine instruction... Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--asm/srcfile.c39
-rw-r--r--asm/srcfile.h52
2 files changed, 56 insertions, 35 deletions
diff --git a/asm/srcfile.c b/asm/srcfile.c
index 158f83d2..f548da65 100644
--- a/asm/srcfile.c
+++ b/asm/srcfile.c
@@ -44,8 +44,7 @@
#include "hashtbl.h"
#include "srcfile.h"
-static const char *file_name = NULL;
-static int32_t line_number = 0;
+struct src_location _src_here;
static struct hash_table filename_hash;
@@ -79,45 +78,23 @@ const char *src_set_fname(const char *newname)
}
}
- oldname = file_name;
- file_name = newname;
+ oldname = _src_here.filename;
+ _src_here.filename = newname;
return oldname;
}
-int32_t src_set_linnum(int32_t newline)
-{
- int32_t oldline = line_number;
- line_number = newline;
- return oldline;
-}
-
void src_set(int32_t line, const char *fname)
{
src_set_fname(fname);
src_set_linnum(line);
}
-const char *src_get_fname(void)
-{
- return file_name;
-}
-
-int32_t src_get_linnum(void)
-{
- return line_number;
-}
-
-int32_t src_get(int32_t *xline, const char **xname)
+struct src_location src_update(struct src_location whence)
{
- const char *xn = *xname;
- int32_t xl = *xline;
+ struct src_location oldhere = _src_here;
- *xline = line_number;
- *xname = file_name;
+ src_set_fname(whence.filename);
+ src_set_linnum(whence.lineno);
- /* XXX: Is the strcmp() really needed here? */
- if (!file_name || !xn || (xn != file_name && strcmp(xn, file_name)))
- return -2;
- else
- return line_number - xl;
+ return oldhere;
}
diff --git a/asm/srcfile.h b/asm/srcfile.h
index 4916730d..3af6595e 100644
--- a/asm/srcfile.h
+++ b/asm/srcfile.h
@@ -39,20 +39,64 @@
#include "compiler.h"
+struct src_location {
+ const char *filename;
+ int32_t lineno;
+};
+extern struct src_location _src_here;
+
void src_init(void);
void src_free(void);
const char *src_set_fname(const char *newname);
-const char *src_get_fname(void);
-int32_t src_set_linnum(int32_t newline);
-int32_t src_get_linnum(void);
+static inline const char *src_get_fname(void)
+{
+ return _src_here.filename;
+}
+static inline int32_t src_set_linnum(int32_t newline)
+{
+ int32_t oldline = _src_here.lineno;
+ _src_here.lineno = newline;
+ return oldline;
+}
+static inline int32_t src_get_linnum(void)
+{
+ return _src_here.lineno;
+}
/* Can be used when there is no need for the old information */
void src_set(int32_t line, const char *filename);
+
/*
* src_get gets both the source file name and line.
* It is also used if you maintain private status about the source location
* It return 0 if the information was the same as the last time you
* checked, -2 if the name changed and (new-old) if just the line changed.
+ *
+ * xname must point to a filename string previously returned from any
+ * function of this subsystem or be NULL; another string value will
+ * not work.
+ */
+static inline int32_t src_get(int32_t *xline, const char **xname)
+{
+ const char *xn = *xname;
+ int32_t xl = *xline;
+
+ *xline = _src_here.lineno;
+ *xname = _src_here.filename;
+
+ /* The return value is expected to be optimized out almost everywhere */
+ if (!xn || xn != _src_here.filename)
+ return -2;
+ else
+ return _src_here.lineno - xl;
+}
+
+/*
+ * Returns and sets/returns the current information as a structure.
*/
-int32_t src_get(int32_t *xline, const char **xname);
+static inline struct src_location src_where(void)
+{
+ return _src_here;
+}
+struct src_location src_update(struct src_location);
#endif /* ASM_SRCFILE_H */