scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit c54377fd54f682b392f83d3f14a3b72b884045a2
parent 24aa0ca4ec25d6a8efde4ee72a61200833b2e7f9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 22 Aug 2019 18:46:30 +0100

[libmach] Add new to Obj

This is the first step to move to an object oriented interface.

Diffstat:
Minclude/scc/scc/mach.h | 2++
Msrc/libmach/Makefile | 3++-
Msrc/libmach/coff32/Makefile | 3++-
Asrc/libmach/coff32/coff32.c | 10++++++++++
Msrc/libmach/coff32/coff32.h | 2++
Msrc/libmach/libmach.h | 8+++++++-
Asrc/libmach/mach.c | 12++++++++++++
Msrc/libmach/objnew.c | 10+++++-----
8 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -54,6 +54,8 @@ struct obj { int nsecs; int nsyms; void *data; + + int (*new)(Obj *obj); }; diff --git a/src/libmach/Makefile b/src/libmach/Makefile @@ -4,7 +4,8 @@ include $(PROJECTDIR)/scripts/rules.mk TARGET = $(LIBDIR)/libmach.a -OBJS = objnew.o \ +OBJS = mach.o \ + objnew.o \ objpos.o \ objread.o \ objfree.o \ diff --git a/src/libmach/coff32/Makefile b/src/libmach/coff32/Makefile @@ -2,7 +2,8 @@ PROJECTDIR =../../.. include $(PROJECTDIR)/scripts/rules.mk -OBJS = coff32del.o \ +OBJS = coff32.o \ + coff32del.o \ coff32new.o \ coff32probe.o \ coff32read.o \ diff --git a/src/libmach/coff32/coff32.c b/src/libmach/coff32/coff32.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +#include <scc/mach.h> + +#include "../libmach.h" +#include "coff32.h" + +struct objfmt coff32 = { + .new = coff32new, +}; diff --git a/src/libmach/coff32/coff32.h b/src/libmach/coff32/coff32.h @@ -23,3 +23,5 @@ struct coff32 { char *strtbl; unsigned long strsiz; }; + +extern int coff32new(Obj *obj); diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -28,6 +28,10 @@ enum deltype { TARGETDEL = 1 << 1, }; +struct objfmt { + int (*new)(Obj *obj); +}; + /* common functions */ extern int pack(int order, unsigned char *dst, char *fmt, ...); extern int unpack(int order, unsigned char *src, char *fmt, ...); @@ -36,7 +40,6 @@ extern int objfree(Obj *obj, int what); /* coff32 functions */ /* TODO: Move this functions to a coff32 files */ extern long coff32index(int type, long nsyms, Objsymdef *head, FILE *fp); -extern int coff32new(Obj *obj); extern void coff32del(Obj *obj); extern int coff32read(Obj *obj, FILE *fp); extern int coff32write(Obj *obj, FILE *fp); @@ -51,3 +54,6 @@ extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp); extern char *coff32namidx(void); extern int coff32sync(Obj *obj); + +/* globals */ +extern struct objfmt *objfmts[]; diff --git a/src/libmach/mach.c b/src/libmach/mach.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +#include <scc/mach.h> + +#include "libmach.h" + +extern struct objfmt coff32; + +struct objfmt *objfmts[] = { + [COFF32] = &coff32, + [NFORMATS] = NULL, +}; diff --git a/src/libmach/objnew.c b/src/libmach/objnew.c @@ -6,15 +6,12 @@ #include "libmach.h" -static int (*funv[])(Obj *) = { - [COFF32] = coff32new, -}; - Obj * objnew(int type) { Obj *obj; int fmt; + struct objfmt *op; fmt = FORMAT(type); if (fmt >= NFORMATS) @@ -30,7 +27,10 @@ objnew(int type) obj->nsecs = 0; memset(obj->htab, 0, sizeof(obj->htab)); - if ((*funv[fmt])(obj) < 0) { + op = objfmts[fmt]; + obj->new = op->new; + + if ((*obj->new)(obj) < 0) { free(obj); return NULL; }