summaryrefslogtreecommitdiff
path: root/sys/src/cmd/cc
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@felloff.net>2020-04-11 05:03:49 +0200
committercinap_lenrek <cinap_lenrek@felloff.net>2020-04-11 05:03:49 +0200
commit1b8a56941708e6fe884f4649f718fae7c543c045 (patch)
tree3d7be0a7e43b01aba6dd986bff06bcc3947a6d78 /sys/src/cmd/cc
parent9d46360c9d83fa95f42f1eebbad49ecd9281fb0d (diff)
cc, ?[acl]: fix gethunk() and move common memory allocator code to cc/compat
for gethunk() to work, all allocators have to use it, including allocations done by libc thru malloc(), so the fake allocation functions are mandatory for everyone. to avoid duplication the code is moved to cc/compat and prototypes provided in new cc/compat.h header.
Diffstat (limited to 'sys/src/cmd/cc')
-rw-r--r--sys/src/cmd/cc/cc.h32
-rw-r--r--sys/src/cmd/cc/compat115
-rw-r--r--sys/src/cmd/cc/compat.c60
-rw-r--r--sys/src/cmd/cc/compat.h35
-rw-r--r--sys/src/cmd/cc/lex.c39
-rw-r--r--sys/src/cmd/cc/lexbody39
-rw-r--r--sys/src/cmd/cc/mac.c1
-rw-r--r--sys/src/cmd/cc/macbody19
-rw-r--r--sys/src/cmd/cc/mkfile5
9 files changed, 154 insertions, 191 deletions
diff --git a/sys/src/cmd/cc/cc.h b/sys/src/cmd/cc/cc.h
index 75ac42d7f..8de3c4e1a 100644
--- a/sys/src/cmd/cc/cc.h
+++ b/sys/src/cmd/cc/cc.h
@@ -5,9 +5,7 @@
#pragma lib "../cc/cc.a$O"
-#ifndef EXTERN
-#define EXTERN extern
-#endif
+#include "../cc/compat.h"
typedef struct Node Node;
typedef struct Sym Sym;
@@ -184,13 +182,6 @@ enum
NALIGN,
};
-enum /* also in ../{8a,0a}.h */
-{
- Plan9 = 1<<0,
- Unix = 1<<1,
- Windows = 1<<2,
-};
-
enum
{
DMARK,
@@ -437,7 +428,6 @@ EXTERN Decl* firstdcl;
EXTERN int fperror;
EXTERN Sym* hash[NHASH];
EXTERN int hasdoubled;
-EXTERN char* hunk;
EXTERN char* include[20];
EXTERN Io* iofree;
EXTERN Io* ionext;
@@ -451,7 +441,6 @@ EXTERN long lineno;
EXTERN long nearln;
EXTERN int nerrors;
EXTERN int newflag;
-EXTERN long nhunk;
EXTERN int ninclude;
EXTERN Node* nodproto;
EXTERN Node* nodcast;
@@ -471,7 +460,6 @@ EXTERN Type* tufield;
EXTERN int thechar;
EXTERN char* thestring;
EXTERN Type* thisfn;
-EXTERN uintptr thunk;
EXTERN Type* types[NTYPE];
EXTERN Type* fntypes[NTYPE];
EXTERN Node* initlist;
@@ -522,21 +510,6 @@ extern ulong thash3;
extern ulong thash[];
/*
- * compat.c/unix.c/windows.c
- */
-int mywait(int*);
-int mycreat(char*, int);
-int systemtype(int);
-int pathchar(void);
-int myaccess(char*);
-char* mygetwd(char*, int);
-int myexec(char*, char*[]);
-int mydup(int, int);
-int myfork(void);
-int mypipe(int*);
-void* mysbrk(ulong);
-
-/*
* parser
*/
int yyparse(void);
@@ -545,8 +518,6 @@ int mpatov(char*, vlong*);
/*
* lex.c
*/
-void* allocn(void*, long, long);
-void* alloc(long);
void cinit(void);
int compile(char*, char**, int);
void errorexit(void);
@@ -666,7 +637,6 @@ int castucom(Node*);
int deadheads(Node*);
Type* dotsearch(Sym*, Type*, Node*, long*);
long dotoffset(Type*, Type*, Node*);
-void gethunk(void);
Node* invert(Node*);
int bitno(long);
void makedot(Node*, Type*, long);
diff --git a/sys/src/cmd/cc/compat b/sys/src/cmd/cc/compat
index fc0654157..df59f0c98 100644
--- a/sys/src/cmd/cc/compat
+++ b/sys/src/cmd/cc/compat
@@ -1,4 +1,3 @@
-
int
myaccess(char *f)
{
@@ -77,3 +76,117 @@ myfork(void)
{
return fork();
}
+
+/*
+ * real allocs
+ */
+void
+gethunk(void)
+{
+ char *h;
+ ulong nh;
+
+ nh = NHUNK;
+ if(thunk >= 10L*NHUNK)
+ nh = 10L*NHUNK;
+ h = (char*)mysbrk(nh);
+ if(h == (char*)-1)
+ sysfatal("out of memory");
+ if(nhunk == 0)
+ hunk = h;
+ else
+ nh += (h - hunk) - nhunk;
+ nhunk += nh;
+ thunk += nh;
+}
+
+void*
+alloc(long n)
+{
+ void *p;
+
+ while((uintptr)hunk & 7) {
+ hunk++;
+ nhunk--;
+ }
+ while(nhunk < n)
+ gethunk();
+ p = hunk;
+ nhunk -= n;
+ hunk += n;
+ return p;
+}
+
+void*
+allocn(void *p, long on, long n)
+{
+ void *q;
+
+ q = (uchar*)p + on;
+ if(q != hunk || nhunk < n) {
+ while(nhunk < on+n)
+ gethunk();
+ memmove(hunk, p, on);
+ p = hunk;
+ hunk += on;
+ nhunk -= on;
+ }
+ hunk += n;
+ nhunk -= n;
+ return p;
+}
+
+/*
+ * fake mallocs
+ */
+void*
+malloc(ulong n)
+{
+ return alloc(n);
+}
+
+void*
+calloc(ulong m, ulong n)
+{
+ return alloc(m*n);
+}
+
+void*
+realloc(void *o, ulong n)
+{
+ ulong m;
+ void *a;
+
+ if(n == 0)
+ return nil;
+ if(o == nil)
+ return alloc(n);
+ a = alloc(n);
+ m = (char*)a - (char*)o;
+ if(m < n)
+ n = m;
+ memmove(a, o, n);
+ return a;
+}
+
+void
+free(void*)
+{
+}
+
+/* needed when profiling */
+void*
+mallocz(ulong size, int)
+{
+ return alloc(size);
+}
+
+void
+setmalloctag(void*, uintptr)
+{
+}
+
+void
+setrealloctag(void*, uintptr)
+{
+}
diff --git a/sys/src/cmd/cc/compat.c b/sys/src/cmd/cc/compat.c
index c76ab9968..1724b5b30 100644
--- a/sys/src/cmd/cc/compat.c
+++ b/sys/src/cmd/cc/compat.c
@@ -1,62 +1,2 @@
#include "cc.h"
#include "compat"
-
-/*
- * fake mallocs
- */
-void*
-malloc(ulong n)
-{
- return alloc(n);
-}
-
-void*
-calloc(ulong m, ulong n)
-{
- return alloc(m*n);
-}
-
-void*
-realloc(void *o, ulong n)
-{
- ulong m;
- void *a;
-
- if(n == 0)
- return nil;
- if(o == nil)
- return alloc(n);
- a = alloc(n);
- m = (char*)a - (char*)o;
- if(m < n)
- n = m;
- memmove(a, o, n);
- return a;
-}
-
-void
-free(void*)
-{
-}
-
-/* needed when profiling */
-void*
-mallocz(ulong size, int clr)
-{
- void *v;
-
- v = alloc(size);
- if(clr && v != nil)
- memset(v, 0, size);
- return v;
-}
-
-void
-setmalloctag(void*, uintptr)
-{
-}
-
-void
-setrealloctag(void*, uintptr)
-{
-}
diff --git a/sys/src/cmd/cc/compat.h b/sys/src/cmd/cc/compat.h
new file mode 100644
index 000000000..9f056e36a
--- /dev/null
+++ b/sys/src/cmd/cc/compat.h
@@ -0,0 +1,35 @@
+/*
+ * functions shared by compilers, linkers and assemblers.
+ */
+
+#ifndef EXTERN
+#define EXTERN extern
+#endif
+
+enum
+{
+ Plan9 = 1<<0,
+ Unix = 1<<1,
+ Windows = 1<<2
+};
+EXTERN int systemtype(int);
+EXTERN int pathchar(void);
+
+EXTERN int myaccess(char *);
+EXTERN int mywait(int*);
+EXTERN int mycreat(char*, int);
+EXTERN char* mygetwd(char*, int);
+EXTERN int myexec(char*, char*[]);
+EXTERN int mydup(int, int);
+EXTERN int myfork(void);
+EXTERN int mypipe(int*);
+EXTERN void* mysbrk(ulong);
+
+EXTERN void gethunk(void);
+
+EXTERN char* hunk;
+EXTERN uintptr nhunk;
+EXTERN uintptr thunk;
+
+EXTERN void* alloc(long n);
+EXTERN void* allocn(void *p, long on, long n);
diff --git a/sys/src/cmd/cc/lex.c b/sys/src/cmd/cc/lex.c
index 06abc8232..5419f26bf 100644
--- a/sys/src/cmd/cc/lex.c
+++ b/sys/src/cmd/cc/lex.c
@@ -1524,45 +1524,6 @@ VBconv(Fmt *fp)
return fmtstrcpy(fp, str);
}
-/*
- * real allocs
- */
-void*
-alloc(long n)
-{
- void *p;
-
- while((uintptr)hunk & MAXALIGN) {
- hunk++;
- nhunk--;
- }
- while(nhunk < n)
- gethunk();
- p = hunk;
- nhunk -= n;
- hunk += n;
- return p;
-}
-
-void*
-allocn(void *p, long on, long n)
-{
- void *q;
-
- q = (uchar*)p + on;
- if(q != hunk || nhunk < n) {
- while(nhunk < on+n)
- gethunk();
- memmove(hunk, p, on);
- p = hunk;
- hunk += on;
- nhunk -= on;
- }
- hunk += n;
- nhunk -= n;
- return p;
-}
-
void
setinclude(char *p)
{
diff --git a/sys/src/cmd/cc/lexbody b/sys/src/cmd/cc/lexbody
index a22c6efba..471041617 100644
--- a/sys/src/cmd/cc/lexbody
+++ b/sys/src/cmd/cc/lexbody
@@ -37,45 +37,6 @@ pragincomplete(void)
;
}
-/*
- * real allocs
- */
-void*
-alloc(long n)
-{
- void *p;
-
- while((uintptr)hunk & MAXALIGN) {
- hunk++;
- nhunk--;
- }
- while(nhunk < n)
- gethunk();
- p = hunk;
- nhunk -= n;
- hunk += n;
- return p;
-}
-
-void*
-allocn(void *p, long on, long n)
-{
- void *q;
-
- q = (uchar*)p + on;
- if(q != hunk || nhunk < n) {
- while(nhunk < on+n)
- gethunk();
- memmove(hunk, p, on);
- p = hunk;
- hunk += on;
- nhunk -= on;
- }
- hunk += n;
- nhunk -= n;
- return p;
-}
-
void
setinclude(char *p)
{
diff --git a/sys/src/cmd/cc/mac.c b/sys/src/cmd/cc/mac.c
index 7ec6e3937..e2ec23ace 100644
--- a/sys/src/cmd/cc/mac.c
+++ b/sys/src/cmd/cc/mac.c
@@ -1,3 +1,2 @@
#include "cc.h"
-
#include "macbody"
diff --git a/sys/src/cmd/cc/macbody b/sys/src/cmd/cc/macbody
index 7d837fb43..0a921cbb8 100644
--- a/sys/src/cmd/cc/macbody
+++ b/sys/src/cmd/cc/macbody
@@ -847,22 +847,3 @@ linehist(char *f, int offset)
ehist->link = h;
ehist = h;
}
-
-void
-gethunk(void)
-{
- char *h;
- long nh;
-
- nh = NHUNK;
- if(thunk >= 10L*NHUNK)
- nh = 10L*NHUNK;
- h = (char*)mysbrk(nh);
- if(h == (char*)-1) {
- yyerror("out of memory");
- errorexit();
- }
- hunk = h;
- nhunk = nh;
- thunk += nh;
-}
diff --git a/sys/src/cmd/cc/mkfile b/sys/src/cmd/cc/mkfile
index c7530b3de..91310d62a 100644
--- a/sys/src/cmd/cc/mkfile
+++ b/sys/src/cmd/cc/mkfile
@@ -19,6 +19,7 @@ OFILES=\
omachcap.$O\
HFILES=cc.h\
+ compat.h\
y.tab.h\
YFILES=cc.y\
@@ -34,7 +35,9 @@ install:V: $LIB
$LIB: $LIBOBJ
ar vu $LIB $OFILES
-mac.$O: macbody
+mac.$O: macbody
+
+compat.$O: compat
everything:V:
# mk the current compilers