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

Subversion Repositories marca

[/] [marca/] [tags/] [INITIAL/] [spar/] [symtab.c] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jeunes2
/* This file is part of the assembler "spar" for marca.
2
   Copyright (C) 2007 Wolfgang Puffitsch
3
 
4
   This program is free software; you can redistribute it and/or modify it
5
   under the terms of the GNU Library General Public License as published
6
   by the Free Software Foundation; either version 2, or (at your option)
7
   any later version.
8
 
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Library General Public License for more details.
13
 
14
   You should have received a copy of the GNU Library General Public
15
   License along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
17
 
18
#include <stdint.h>
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include <string.h>
22
 
23
#include "spar.h"
24
#include "symtab.h"
25
 
26
static struct sym_info *symtab[SYMTAB_SIZE];
27
 
28
uint32_t hash_string (const char *str)
29
{
30
  uint32_t hash;
31
  const char *ptr = str;
32
  size_t len = strlen(str);
33
  for (hash = 0; len; len--, ptr++)
34
    {
35
      hash = 31 * hash + *ptr;
36
    }
37
  return hash;
38
}
39
 
40
char *trim_string (char *str)
41
{
42
  while ((str[0] == ' ') || (str[0] == '\t'))
43
    {
44
      str++;
45
    }
46
  while ((str[strlen(str)-1] == ' ')
47
         || (str[strlen(str)-1] == '\t'))
48
    {
49
      str[strlen(str)-1] = '\0';
50
    }
51
  return str;
52
}
53
 
54
char *localize_string (char *str)
55
{
56
  if (str[0] == '.')
57
    {
58
      char *buf = xmalloc(strlen(str)+10);
59
      sprintf(buf, "%s@%08lx", str, file_count);
60
      return buf;
61
    }
62
  return str;
63
}
64
 
65
void init_symtab(void)
66
{
67
  int i;
68
  for (i = 0; i < SYMTAB_SIZE; i++)
69
    {
70
      symtab[i] = NULL;
71
    }
72
}
73
 
74
void push_sym(const char *symbol, uint8_t type, uint32_t addr)
75
{
76
  uint32_t pos;
77
  struct sym_info *sym;
78
 
79
  pos = hash_string(symbol) % SYMTAB_SIZE;
80
  sym = xmalloc(sizeof(struct sym_info));
81
  sym->symbol = symbol;
82
  sym->type = type;
83
  sym->addr = addr;
84
  sym->next = symtab[pos];
85
  symtab[pos] = sym;
86
}
87
 
88
struct sym_info *get_sym(const char *symbol)
89
{
90
  uint32_t pos;
91
  struct sym_info *sym;
92
 
93
  pos = hash_string(symbol) % SYMTAB_SIZE;
94
  sym = symtab[pos];
95
 
96
  while ((sym != NULL) && (strcmp(sym->symbol, symbol) != 0))
97
    {
98
      sym = sym->next;
99
    }
100
 
101
  return sym;
102
}
103
 
104
void reloc_syms(uint8_t type, uint32_t offset)
105
{
106
  uint32_t pos;
107
  struct sym_info *sym;
108
 
109
  for (pos = 0; pos < SYMTAB_SIZE; pos++)
110
    {
111
      for (sym = symtab[pos]; sym != NULL; sym = sym->next)
112
        {
113
          if (sym->type == type)
114
            {
115
              sym->addr += offset;
116
            }
117
        }
118
    }
119
}

powered by: WebSVN 2.1.0

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