shithub: scc

Download patch

ref: 3d4872a5f32769876bac1dbe2e2efaa85b864851
parent: eaf9eb8ade3a417bad38096f84a0460be01389ac
author: Quentin Rameau <quinq@fifth.space>
date: Sun Feb 10 09:59:00 EST 2019

[libscc] Regroup hashing routines in a single function

--- a/include/scc/scc/scc.h
+++ b/include/scc/scc/scc.h
@@ -39,3 +39,4 @@
 extern int lunpack(unsigned char *src, char *fmt, ...);
 extern int bpack(unsigned char *dst, char *fmt, ...);
 extern int bunpack(unsigned char *src, char *fmt, ...);
+extern unsigned genhash(char *name);
--- a/src/cmd/Makefile
+++ b/src/cmd/Makefile
@@ -12,36 +12,36 @@
          $(BINDIR)/objcopy \
          $(BINDIR)/addr2line \
 
-LIBS   = -lmach
 DIRS   = as ld scc
 
+LIBMACH = $(LIBDIR)/libmach.a
+LIBSCC  = $(LIBDIR)/libscc.a
+
 all: $(TARGET) $(DIRS)
 
 $(DIRS): FORCE
 	@+cd $@ && $(MAKE)
 
-$(TARGET): $(LIBDIR)/libmach.a
+$(BINDIR)/nm: nm.o $(LIBMACH) $(LIBSCC)
+	$(CC) $(SCC_LDFLAGS) nm.o -lmach -lscc -o $@
 
-$(BINDIR)/nm: nm.o
-	$(CC) $(SCC_LDFLAGS) nm.o -lmach -o $@
+$(BINDIR)/strip: strip.o $(LIBMACH) $(LIBSCC)
+	$(CC) $(SCC_LDFLAGS) strip.o -lmach -lscc -o $@
 
-$(BINDIR)/strip: strip.o
-	$(CC) $(SCC_LDFLAGS) strip.o -lmach -o $@
+$(BINDIR)/size: size.o $(LIBMACH) $(LIBSCC)
+	$(CC) $(SCC_LDFLAGS) size.o -lmach -lscc -o $@
 
-$(BINDIR)/size: size.o
-	$(CC) $(SCC_LDFLAGS) size.o -lmach -o $@
+$(BINDIR)/ranlib: ranlib.o $(DRIVER).o $(LIBMACH) $(LIBSCC)
+	$(CC) $(SCC_LDFLAGS) ranlib.o $(DRIVER).o -lmach -lscc -o $@
 
-$(BINDIR)/ranlib: ranlib.o $(DRIVER).o
-	$(CC) $(SCC_LDFLAGS) ranlib.o $(DRIVER).o -lmach -o $@
-
-$(BINDIR)/objdump: objdump.o
+$(BINDIR)/objdump: objdump.o $(LIBMACH)
 	$(CC) $(SCC_LDFLAGS) objdump.o -lmach -o $@
 
-$(BINDIR)/objcopy: objcopy.o
+$(BINDIR)/objcopy: objcopy.o $(LIBMACH)
 	$(CC) $(SCC_LDFLAGS) objcopy.o -lmach -o $@
 
-$(BINDIR)/addr2line: addr2line.o
-	$(CC) $(SCC_LDFLAGS) addr2line.o -lmach -o $@
+$(BINDIR)/addr2line: addr2line.o $(LIBMACH) $(LIBSCC)
+	$(CC) $(SCC_LDFLAGS) addr2line.o -lmach -lscc -o $@
 
 $(BINDIR)/ar: ar.o $(DRIVER).o
 	$(CC) $(SCC_LDFLAGS) ar.o $(DRIVER).o -o $@
--- a/src/cmd/as/symbol.c
+++ b/src/cmd/as/symbol.c
@@ -47,12 +47,9 @@
 	unsigned h;
 	Symbol *sym, **list;
 	int c, symtype;
-	char *t, *s;
+	char *t;
 
-	h = 0;
-	for (s = name; c = *s; ++s)
-		h = h*33 ^ c;
-	h &= HASHSIZ-1;
+	h = genhash(name) & HASHSIZ-1;
 
 	c = toupper(*name);
 	list = &hashtbl[h];
--- a/src/cmd/ld/Makefile
+++ b/src/cmd/ld/Makefile
@@ -9,10 +9,10 @@
 
 all: $(TARGET)
 
-$(TARGET): $(LIBDIR)/libscc.a
+$(TARGET): $(LIBDIR)/libscc.a $(LIBDIR)/libscc.a
 
 $(TARGET): $(OBJS)
-	$(CC) $(SCC_LDFLAGS) $(OBJS) -lmach -o $@
+	$(CC) $(SCC_LDFLAGS) $(OBJS) -lmach -lscc -o $@
 
 dep: inc-dep
 
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -92,10 +92,7 @@
 	Symbol *sym;
 	char *name = osym->name;
 
-	h = 0;
-	for (s = name; *s; s++)
-		h += *s;
-	h %= NR_SYMBOL;
+	h = genhash(name) % NR_SYMBOL;
 
 	for (sym = symtab[h]; sym; sym = sym->hash) {
 		if (!strcmp(name, sym->name))
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -10,6 +10,7 @@
 #include <scc/ar.h>
 #include <scc/arg.h>
 #include <scc/mach.h>
+#include <scc/scc.h>
 
 #include "sys.h"
 
@@ -52,10 +53,7 @@
 	char *s;
 	size_t len;
 
-	h = 0;
-	for (s = name; *s; s++)
-		h += *s;
-	h %= NR_SYMDEF;
+	h = genhash(name) % NR_SYMDEF;
 
 	for (dp = htab[h]; dp; dp = dp->next) {
 		if (!strcmp(dp->name, name))
--- a/src/cmd/scc/cc1/symbol.c
+++ b/src/cmd/scc/cc1/symbol.c
@@ -66,11 +66,10 @@
 static Symbol **
 hash(char *s, int ns)
 {
-	unsigned c, h, size;
+	unsigned h, size;
 	Symbol **tab;
 
-	for (h = 0; c = *s; ++s)
-		h = h*33 ^ c;
+	h = genhash(s);
 
 	switch (ns) {
 	case NS_CPP:
--- a/src/libmach/objlookup.c
+++ b/src/libmach/objlookup.c
@@ -3,6 +3,7 @@
 #include <string.h>
 
 #include <scc/mach.h>
+#include <scc/scc.h>
 
 Objsym *
 objlookup(Obj *obj, char *name, int install)
@@ -12,11 +13,7 @@
 	char *s;
 	Objsym *sym;
 
-	h = 0;
-	for (s = name; *s; s++)
-		h += *s;
-	h %= NR_SYMHASH;
-
+	h = genhash(name) % NR_SYMHASH;
 	for (sym = obj->htab[h]; sym; sym = sym->hash) {
 		if (!strcmp(name, sym->name))
 			return sym;
--- a/src/libscc/Makefile
+++ b/src/libscc/Makefile
@@ -12,6 +12,7 @@
        xstrdup.o \
        alloc.o \
        casecmp.o \
+       genhash.o
 
 TARGET = $(LIBDIR)/libscc.a
 
--- /dev/null
+++ b/src/libscc/genhash.c
@@ -1,0 +1,11 @@
+unsigned
+genhash(char *name)
+{
+	unsigned h;
+	char c;
+
+	for (h = 0; c = *name; ++name)
+		h = h*33 ^ c;
+
+	return h;
+}