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>
This commit is contained in:
H. Peter Anvin 2018-12-10 23:28:01 -08:00
parent 1aa794527f
commit 026b62f264
2 changed files with 58 additions and 37 deletions

View File

@ -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)
struct src_location src_update(struct src_location whence)
{
return file_name;
}
int32_t src_get_linnum(void)
{
return line_number;
}
int32_t src_get(int32_t *xline, const char **xname)
{
const char *xn = *xname;
int32_t xl = *xline;
*xline = line_number;
*xname = file_name;
/* 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;
struct src_location oldhere = _src_here;
src_set_fname(whence.filename);
src_set_linnum(whence.lineno);
return oldhere;
}

View File

@ -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.
*/
int32_t src_get(int32_t *xline, const char **xname);
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.
*/
static inline struct src_location src_where(void)
{
return _src_here;
}
struct src_location src_update(struct src_location);
#endif /* ASM_SRCFILE_H */