symbol.c revision 178825
126497Sache/*
226497Sache * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
326497Sache * (Royal Institute of Technology, Stockholm, Sweden).
426497Sache * All rights reserved.
558310Sache *
626497Sache * Redistribution and use in source and binary forms, with or without
726497Sache * modification, are permitted provided that the following conditions
8119610Sache * are met:
9119610Sache *
10119610Sache * 1. Redistributions of source code must retain the above copyright
11119610Sache *    notice, this list of conditions and the following disclaimer.
12119610Sache *
13119610Sache * 2. Redistributions in binary form must reproduce the above copyright
14119610Sache *    notice, this list of conditions and the following disclaimer in the
15119610Sache *    documentation and/or other materials provided with the distribution.
16119610Sache *
17119610Sache * 3. Neither the name of the Institute nor the names of its contributors
18119610Sache *    may be used to endorse or promote products derived from this software
19119610Sache *    without specific prior written permission.
20119610Sache *
21119610Sache * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22119610Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23119610Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24119610Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25119610Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26119610Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27119610Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2826497Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2935486Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3026497Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3126497Sache * SUCH DAMAGE.
3226497Sache */
3326497Sache
3426497Sache#include "gen_locl.h"
35157184Sache#include "lex.h"
36157184Sache
37157184SacheRCSID("$Id: symbol.c 15617 2005-07-12 06:27:42Z lha $");
38157184Sache
39157184Sachestatic Hashtab *htab;
40157184Sache
4158310Sachestatic int
42157184Sachecmp(void *a, void *b)
4358310Sache{
4458310Sache    Symbol *s1 = (Symbol *) a;
4558310Sache    Symbol *s2 = (Symbol *) b;
46157184Sache
4758310Sache    return strcmp(s1->name, s2->name);
4858310Sache}
4958310Sache
5058310Sachestatic unsigned
5126497Sachehash(void *a)
5226497Sache{
5326497Sache    Symbol *s = (Symbol *) a;
5435486Sache
5526497Sache    return hashjpw(s->name);
5635486Sache}
5726497Sache
5826497Sachevoid
5926497Sacheinitsym(void)
6026497Sache{
6126497Sache    htab = hashtabnew(101, cmp, hash);
6226497Sache}
6326497Sache
6426497Sache
6526497Sachevoid
6626497Sacheoutput_name(char *s)
6726497Sache{
6875406Sache    char *p;
6926497Sache
7058310Sache    for (p = s; *p; ++p)
7126497Sache	if (*p == '-')
7226497Sache	    *p = '_';
7347558Sache}
7426497Sache
7526497SacheSymbol *
7658310Sacheaddsym(char *name)
7726497Sache{
7826497Sache    Symbol key, *s;
7926497Sache
8058310Sache    key.name = name;
8126497Sache    s = (Symbol *) hashtabsearch(htab, (void *) &key);
8226497Sache    if (s == NULL) {
8326497Sache	s = (Symbol *) emalloc(sizeof(*s));
8426497Sache	s->name = name;
8526497Sache	s->gen_name = estrdup(name);
8626497Sache	output_name(s->gen_name);
8758310Sache	s->stype = SUndefined;
8826497Sache	hashtabadd(htab, s);
8926497Sache    }
9026497Sache    return s;
9126497Sache}
9226497Sache
9326497Sachestatic int
9426497Sachecheckfunc(void *ptr, void *arg)
9526497Sache{
9626497Sache    Symbol *s = ptr;
9726497Sache    if (s->stype == SUndefined) {
9858310Sache	error_message("%s is still undefined\n", s->name);
9926497Sache	*(int *) arg = 1;
10026497Sache    }
10158310Sache    return 0;
10226497Sache}
10326497Sache
10426497Sacheint
10526497Sachecheckundefined(void)
10626497Sache{
10726497Sache    int f = 0;
10826497Sache    hashtabforeach(htab, checkfunc, &f);
10926497Sache    return f;
11026497Sache}
11126497Sache