1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

Fold trace status into the single hash table that's left.

Inline some macros/functions for speed.
So, this achieves the goal of one single lookup for macro/trace status,
which does speed up m4 in partial tracing situations somewhat.
This does also speed up m4 in large pushdef situations, since it no longer
has to lookup large chains of macros.

okay millert@
This commit is contained in:
espie 2003-06-30 22:13:32 +00:00
parent 7888c33aa2
commit 4bc640fdbb
6 changed files with 109 additions and 167 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: extern.h,v 1.37 2003/06/30 22:11:38 espie Exp $ */
/* $OpenBSD: extern.h,v 1.38 2003/06/30 22:13:32 espie Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*-
@ -58,8 +58,15 @@ extern void doesyscmd(const char *);
/* look.c */
#define FLAG_UNTRACED 0
#define FLAG_TRACED 1
#define FLAG_NO_TRACE 2
extern void init_macros(void);
extern ndptr lookup(const char *);
extern void mark_traced(const char *, int);
extern struct ohash macros;
extern struct macro_definition *lookup_macro_definition(const char *);
extern void macro_define(const char *, const char *);
@ -68,10 +75,12 @@ extern void macro_popdef(const char *);
extern void macro_undefine(const char *);
extern void setup_builtin(const char *, unsigned int);
extern void macro_for_all(void (*)(const char *, struct macro_definition *));
extern const char *macro_name(ndptr);
extern struct macro_definition *macro_getdef(ndptr);
#define macro_getdef(p) ((p)->d)
#define macro_name(p) ((p)->name)
#define macro_builtin_type(p) ((p)->builtin_type)
#define is_traced(p) ((p)->trace_flags == FLAG_NO_TRACE ? (trace_flags & TRACE_ALL) : (p)->trace_flags)
extern ndptr macro_getbuiltin(const char *);
extern int macro_builtin_type(ndptr);
/* main.c */
extern void outputstr(const char *);
@ -123,15 +132,13 @@ extern char *endpbb;
extern char *endest;
/* trace.c */
extern void mark_traced(const char *, int);
extern int is_traced(const char *);
extern unsigned int trace_flags;
#define TRACE_ALL 512
extern void trace_file(const char *);
extern ssize_t trace(const char **, int, struct input_file *);
extern void finish_trace(size_t);
extern int traced_macros;
extern void set_trace_flags(const char *);
extern FILE *traceout;
extern void init_trace(void);
extern ndptr hashtab[]; /* hash table for macros etc. */
extern stae *mstack; /* stack of m4 machine */

View File

@ -1,4 +1,4 @@
/* $OpenBSD: gnum4.c,v 1.24 2003/06/30 22:11:38 espie Exp $ */
/* $OpenBSD: gnum4.c,v 1.25 2003/06/30 22:13:32 espie Exp $ */
/*
* Copyright (c) 1999 Marc Espie
@ -159,24 +159,26 @@ fopen_trypath(struct input_file *i, const char *filename)
void
doindir(const char *argv[], int argc)
{
ndptr n;
struct macro_definition *p;
p = lookup_macro_definition(argv[2]);
if (p == NULL)
n = lookup(argv[2]);
if (n == NULL || (p = macro_getdef(n)) == NULL)
errx(1, "undefined macro %s", argv[2]);
argv[1] = p->defn;
eval(argv+1, argc-1, p->type, traced_macros && is_traced(argv[2]));
eval(argv+1, argc-1, p->type, is_traced(n));
}
void
dobuiltin(const char *argv[], int argc)
{
ndptr p;
argv[1] = NULL;
p = macro_getbuiltin(argv[2]);
if (p != NULL)
eval(argv+1, argc-1, macro_builtin_type(p), traced_macros && is_traced(argv[2]));
eval(argv+1, argc-1, macro_builtin_type(p), is_traced(p));
else
errx(1, "unknown builtin %s", argv[2]);
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: look.c,v 1.14 2003/06/30 22:11:38 espie Exp $ */
/* $OpenBSD: look.c,v 1.15 2003/06/30 22:13:32 espie Exp $ */
/*
* Copyright (c) 1989, 1993
@ -52,15 +52,9 @@ static char sccsid[] = "@(#)look.c 8.1 (Berkeley) 6/6/93";
#include "stdd.h"
#include "extern.h"
struct ndblock { /* hashtable structure */
unsigned int builtin_type;
struct macro_definition *d;
char name[1]; /* entry name.. */
};
extern void *hash_alloc(size_t, void *);
extern void hash_free(void *, size_t, void *);
extern void *element_alloc(size_t, void *);
static void *hash_alloc(size_t, void *);
static void hash_free(void *, size_t, void *);
static void *element_alloc(size_t, void *);
static void setup_definition(struct macro_definition *, const char *,
const char *);
@ -68,12 +62,41 @@ static struct ohash_info macro_info = {
offsetof(struct ndblock, name),
NULL, hash_alloc, hash_free, element_alloc };
static struct ohash macros;
struct ohash macros;
/* Support routines for hash tables. */
void *
hash_alloc(s, u)
size_t s;
void *u UNUSED;
{
void *storage = xalloc(s);
if (storage)
memset(storage, 0, s);
return storage;
}
void
hash_free(p, s, u)
void *p;
size_t s UNUSED;
void *u UNUSED;
{
free(p);
}
void *
element_alloc(s, u)
size_t s;
void *u UNUSED;
{
return xalloc(s);
}
void
init_macros()
{
ohash_init(&macros, 7, &macro_info);
ohash_init(&macros, 10, &macro_info);
}
/*
@ -90,7 +113,7 @@ lookup_macro_definition(const char *name)
{
ndptr p;
p = lookup(name);
p = ohash_find(&macros, ohash_qlookup(&macros, name));
if (p)
return p->d;
else
@ -129,6 +152,7 @@ create_entry(const char *name)
if (n == NULL) {
n = ohash_create_entry(&macro_info, name, &end);
ohash_insert(&macros, i, n);
n->trace_flags = FLAG_NO_TRACE;
n->builtin_type = MACRTYPE;
n->d = NULL;
}
@ -219,16 +243,24 @@ setup_builtin(const char *name, unsigned int type)
n->d->next = NULL;
}
const char *
macro_name(ndptr p)
void
mark_traced(const char *name, int on)
{
return p->name;
}
ndptr p;
unsigned int i;
struct macro_definition *
macro_getdef(ndptr p)
{
return p->d;
if (name == NULL) {
if (on)
trace_flags |= TRACE_ALL;
else
trace_flags &= ~TRACE_ALL;
for (p = ohash_first(&macros, &i); p != NULL;
p = ohash_next(&macros, &i))
p->trace_flags = FLAG_NO_TRACE;
} else {
p = create_entry(name);
p->trace_flags = on;
}
}
ndptr
@ -243,9 +275,3 @@ macro_getbuiltin(const char *name)
return p;
}
int
macro_builtin_type(ndptr p)
{
return p->builtin_type;
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.62 2003/06/30 22:11:38 espie Exp $ */
/* $OpenBSD: main.c,v 1.63 2003/06/30 22:13:32 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
@ -43,7 +43,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: main.c,v 1.62 2003/06/30 22:11:38 espie Exp $";
static char rcsid[] = "$OpenBSD: main.c,v 1.63 2003/06/30 22:13:32 espie Exp $";
#endif
#endif /* not lint */
@ -63,6 +63,7 @@ static char rcsid[] = "$OpenBSD: main.c,v 1.62 2003/06/30 22:11:38 espie Exp $";
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <ohash.h>
#include <err.h>
#include "mdef.h"
#include "stdd.h"
@ -89,6 +90,11 @@ char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
int synch_lines = 0; /* line synchronisation for C preprocessor */
struct keyblk {
char *knam; /* keyword name */
int ktyp; /* keyword type */
};
struct keyblk keywrds[] = { /* m4 keywords to be installed */
{ "include", INCLTYPE },
{ "sinclude", SINCTYPE },
@ -180,7 +186,6 @@ main(int argc, char *argv[])
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, onintr);
init_trace();
init_macros();
initkwds();
initspaces();
@ -329,10 +334,7 @@ macro(void)
*/
pushf(fp); /* previous call frm */
pushf(macro_getdef(p)->type); /* type of the call */
if (traced_macros && is_traced(macro_name(p)))
pushf(1);
else
pushf(0);
pushf(is_traced(p));
pushf(0); /* parenthesis level */
fp = sp; /* new frame pointer */
/*
@ -564,7 +566,7 @@ inspect(int c, char *tp)
return NULL;
}
p = lookup(name);
p = ohash_find(&macros, ohash_qlookupi(&macros, name, (const char **)&tp));
if (p == NULL)
return NULL;
if (macro_getdef(p) == NULL)

View File

@ -1,4 +1,4 @@
/* $OpenBSD: mdef.h,v 1.27 2003/06/30 22:10:21 espie Exp $ */
/* $OpenBSD: mdef.h,v 1.28 2003/06/30 22:13:33 espie Exp $ */
/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */
/*
@ -142,11 +142,14 @@ struct macro_definition {
unsigned int type; /* type of the entry.. */
};
struct keyblk {
char *knam; /* keyword name */
int ktyp; /* keyword type */
};
struct ndblock { /* hashtable structure */
unsigned int builtin_type;
unsigned int trace_flags;
struct macro_definition *d;
char name[1]; /* entry name.. */
};
typedef union { /* stack structure */
int sfra; /* frame entry */
char *sstr; /* string entry */

View File

@ -1,4 +1,4 @@
/* $OpenBSD: trace.c,v 1.9 2003/06/30 22:10:21 espie Exp $ */
/* $OpenBSD: trace.c,v 1.10 2003/06/30 22:13:33 espie Exp $ */
/*
* Copyright (c) 2001 Marc Espie.
*
@ -29,15 +29,12 @@
#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#include <ohash.h>
#include "mdef.h"
#include "stdd.h"
#include "extern.h"
FILE *traceout = stderr;
int traced_macros = 0;
#define TRACE_ARGS 1
#define TRACE_EXPANSION 2
#define TRACE_QUOTE 4
@ -47,107 +44,14 @@ int traced_macros = 0;
#define TRACE_ID 64
#define TRACE_NEWFILE 128 /* not implemented yet */
#define TRACE_INPUT 256 /* not implemented yet */
#define TRACE_ALL 512
struct t {
int on;
char name[1];
};
static unsigned int letter_to_flag(int);
static void print_header(struct input_file *);
static struct t *find_trace_entry(const char *);
static int frame_level(void);
void *hash_alloc(size_t, void *);
void hash_free(void *, size_t, void *);
void *element_alloc(size_t, void *);
static unsigned int flags = TRACE_QUOTE | TRACE_EXPANSION;
static struct ohash_info trace_info = {
offsetof(struct t, name),
NULL, hash_alloc, hash_free, element_alloc };
static struct ohash trace_hash;
/* Support routines for hash tables. */
void *
hash_alloc(s, u)
size_t s;
void *u UNUSED;
{
void *storage = xalloc(s);
if (storage)
memset(storage, 0, s);
return storage;
}
void
hash_free(p, s, u)
void *p;
size_t s UNUSED;
void *u UNUSED;
{
free(p);
}
void *
element_alloc(s, u)
size_t s;
void *u UNUSED;
{
return xalloc(s);
}
void
init_trace()
{
ohash_init(&trace_hash, 5, &trace_info);
}
void
mark_traced(const char *name, int on)
{
struct t *n;
unsigned int i;
const char *end = NULL;
traced_macros = 1;
if (name == NULL) {
if (on)
flags |= TRACE_ALL;
else {
flags &= ~TRACE_ALL;
traced_macros = 0;
}
for (n = ohash_first(&trace_hash, &i); n != NULL;
n = ohash_next(&trace_hash, &i))
free(n);
} else {
i = ohash_qlookupi(&trace_hash, name, &end);
n = ohash_find(&trace_hash, i);
if (n == NULL) {
n = ohash_create_entry(&trace_info, name, &end);
ohash_insert(&trace_hash, i, n);
}
n->on = on;
}
}
int
is_traced(const char *name)
{
struct t *n;
n = ohash_find(&trace_hash, ohash_qlookup(&trace_hash, name));
if (n)
return n->on;
else
return (flags & TRACE_ALL) ? 1 : 0;
}
unsigned int trace_flags = TRACE_QUOTE | TRACE_EXPANSION;
void
trace_file(const char *name)
@ -197,21 +101,19 @@ set_trace_flags(const char *s)
char mode = 0;
unsigned int f = 0;
traced_macros = 1;
if (*s == '+' || *s == '-')
mode = *s++;
while (*s)
f |= letter_to_flag(*s++);
switch(mode) {
case 0:
flags = f;
trace_flags = f;
break;
case '+':
flags |= f;
trace_flags |= f;
break;
case '-':
flags &= ~f;
trace_flags &= ~f;
break;
}
}
@ -232,12 +134,12 @@ static void
print_header(struct input_file *inp)
{
fprintf(traceout, "m4trace:");
if (flags & TRACE_FILENAME)
if (trace_flags & TRACE_FILENAME)
fprintf(traceout, "%s:", inp->name);
if (flags & TRACE_LINENO)
if (trace_flags & TRACE_LINENO)
fprintf(traceout, "%lu:", inp->lineno);
fprintf(traceout, " -%d- ", frame_level());
if (flags & TRACE_ID)
if (trace_flags & TRACE_ID)
fprintf(traceout, "id %lu: ", expansion_id);
}
@ -245,12 +147,12 @@ ssize_t
trace(const char *argv[], int argc, struct input_file *inp)
{
print_header(inp);
if (flags & TRACE_CONT) {
if (trace_flags & TRACE_CONT) {
fprintf(traceout, "%s ...\n", argv[1]);
print_header(inp);
}
fprintf(traceout, "%s", argv[1]);
if ((flags & TRACE_ARGS) && argc > 2) {
if ((trace_flags & TRACE_ARGS) && argc > 2) {
char delim[3];
int i;
@ -258,21 +160,21 @@ trace(const char *argv[], int argc, struct input_file *inp)
delim[1] = EOS;
for (i = 2; i < argc; i++) {
fprintf(traceout, "%s%s%s%s", delim,
(flags & TRACE_QUOTE) ? lquote : "",
(trace_flags & TRACE_QUOTE) ? lquote : "",
argv[i],
(flags & TRACE_QUOTE) ? rquote : "");
(trace_flags & TRACE_QUOTE) ? rquote : "");
delim[0] = COMMA;
delim[1] = ' ';
delim[2] = EOS;
}
fprintf(traceout, "%c", RPAREN);
}
if (flags & TRACE_CONT) {
if (trace_flags & TRACE_CONT) {
fprintf(traceout, " -> ???\n");
print_header(inp);
fprintf(traceout, argc > 2 ? "%s(...)" : "%s", argv[1]);
}
if (flags & TRACE_EXPANSION)
if (trace_flags & TRACE_EXPANSION)
return buffer_mark();
else {
fprintf(traceout, "\n");
@ -284,10 +186,10 @@ void
finish_trace(size_t mark)
{
fprintf(traceout, " -> ");
if (flags & TRACE_QUOTE)
if (trace_flags & TRACE_QUOTE)
fprintf(traceout, "%s", lquote);
dump_buffer(traceout, mark);
if (flags & TRACE_QUOTE)
if (trace_flags & TRACE_QUOTE)
fprintf(traceout, "%s", rquote);
fprintf(traceout, "\n");
}