OpenCores
URL https://opencores.org/ocsvn/tms1000/tms1000/trunk

Subversion Repositories tms1000

[/] [tms1000/] [trunk/] [assembler/] [symtab.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 nand_gates
/*
2
symtab.c: a simple binary tree symbol table
3
 
4
CASM is an assembler for the TMS1000 processor.
5
*/
6
 
7
#include <stdio.h>
8
#include <string.h>
9
#include "symtab.h"
10
 
11
typedef struct sym
12
{
13
  char *name;
14
  int value;
15
  int lineno;
16
  struct sym *left;
17
  struct sym *right;
18
} sym;
19
 
20
static char *newstr (char *orig)
21
{
22
  int len;
23
  char *r;
24
 
25
  len = strlen (orig);
26
  r = (char *) malloc (len + 10);
27
 
28
  if (! r)
29
    {
30
      fprintf (stderr, "memory allocation failed\n");
31
      exit (2);
32
    }
33
 
34
  memcpy (r, orig, len + 1);
35
  return (r);
36
}
37
 
38
t_symtab alloc_symbol_table (void)
39
{
40
  sym **table;
41
  table = (sym **) calloc (1, sizeof (sym *));
42
  return (table);
43
}
44
 
45
static void free_entry (sym *p)
46
{
47
  if (p->left)
48
    free_entry (p->left);
49
  if (p->right)
50
    free_entry (p->right);
51
  free (p);
52
}
53
 
54
void free_symbol_table (t_symtab t)
55
{
56
  sym **table = t;
57
  free_entry (*table);
58
  free (table);
59
}
60
 
61
static int insert_symbol (sym **p, sym *newsym)
62
{
63
  int i;
64
 
65
  if (! *p)
66
    {
67
      (*p) = newsym;
68
      return (1);
69
    }
70
 
71
  i = stricmp (newsym->name, (*p)->name);
72
 
73
  if (i == 0)
74
    return (0);
75
  else if (i < 0)
76
    return (insert_symbol (& ((*p)->left), newsym));
77
  else
78
    return (insert_symbol (& ((*p)->right), newsym));
79
}
80
 
81
 
82
/* returns 1 for success, 0 if duplicate name */
83
int create_symbol (t_symtab t, char *name, int value, int lineno)
84
{
85
  sym **table = t;
86
  sym *p = *table;
87
  sym *newsym;
88
 
89
  newsym = (sym *) calloc (1, sizeof (sym));
90
  if (! newsym)
91
    {
92
      fprintf (stderr, "memory allocation failure\n");
93
      exit (2);
94
    }
95
 
96
  newsym->name = newstr (name);
97
  newsym->value = value;
98
  newsym->lineno = lineno;
99
 
100
  return (insert_symbol (table, newsym));
101
}
102
 
103
/* returns 1 for success, 0 if not found */
104
int lookup_symbol (t_symtab t, char *name, int *value)
105
{
106
  sym **table = t;
107
  sym *p = *table;
108
  int i;
109
 
110
  while (p)
111
    {
112
      i = stricmp (name, p->name);
113
      if (i == 0)
114
        {
115
          *value = p->value;
116
          return (1);
117
        }
118
      if (i < 0)
119
        p = p->left;
120
      else
121
        p = p->right;
122
    }
123
  return (0);
124
}
125
 
126
static void print_symbols (FILE *f, sym *p)
127
{
128
  if (! p)
129
    return;
130
  print_symbols (f, p->left);
131
  fprintf (f, "%03x %s %d\n", p->value, p->name, p->lineno);
132
  print_symbols (f, p->right);
133
}
134
 
135
void print_symbol_table (t_symtab t, FILE *f)
136
{
137
  sym **table = t;
138
  print_symbols (f, *table);
139
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.