|
@@ -3,108 +3,85 @@
|
|
|
/* I/O part of em_code module.
|
|
|
Also contains C_open, C_close
|
|
|
*/
|
|
|
-#include <system.h>
|
|
|
#include <alloc.h>
|
|
|
#include <em_path.h>
|
|
|
#include <em_arith.h>
|
|
|
+#include <local.h>
|
|
|
#include "insert.h"
|
|
|
|
|
|
-int
|
|
|
- C_ontmpfile = 0,
|
|
|
- C_sequential = 1;
|
|
|
-Part
|
|
|
- *C_curr_part, *C_stable[TABSIZ];
|
|
|
-long
|
|
|
- C_current_out;
|
|
|
+int C_ontmpfile = 0;
|
|
|
+int C_sequential = 1;
|
|
|
+Part *C_curr_part;
|
|
|
+Part *C_stable[TABSIZ];
|
|
|
+char *C_tmpdir = TMP_DIR;
|
|
|
+int (*C_outpart)(), (*C_switchtoout)(), (*C_switchtotmp)();
|
|
|
|
|
|
#ifdef INCORE
|
|
|
-static char *BASE;
|
|
|
+char *C_BASE;
|
|
|
#endif
|
|
|
-static File *ofp;
|
|
|
+
|
|
|
+File *C_ofp;
|
|
|
|
|
|
#ifndef INCORE
|
|
|
-static File *tfr, *old_ofp;
|
|
|
-static char *tmpfile;
|
|
|
+File *C_tfr, *C_old_ofp;
|
|
|
+char *C_tmpfile;
|
|
|
char *strcpy(), *strcat(), *mktemp();
|
|
|
static char *ibuf = 0;
|
|
|
+long C_current_out;
|
|
|
#endif
|
|
|
|
|
|
-static char obuf[BUFSIZ];
|
|
|
-static char *opp = obuf;
|
|
|
-
|
|
|
-static
|
|
|
-flush() {
|
|
|
- if (opp != obuf && sys_write(ofp, obuf, opp - obuf) == 0) {
|
|
|
- C_failed();
|
|
|
- }
|
|
|
- opp = obuf;
|
|
|
-}
|
|
|
+#if BUFSIZ <= 1024 && BIGMACHINE
|
|
|
+#define BUFFERSIZ 8*BUFSIZ
|
|
|
+#else
|
|
|
+#define BUFFERSIZ BUFSIZ
|
|
|
+#endif
|
|
|
|
|
|
-#define put(c) if (opp == &obuf[BUFSIZ]) flush(); *opp++ = (c)
|
|
|
+static char obuf[BUFFERSIZ];
|
|
|
+char *C_top = &obuf[BUFFERSIZ];
|
|
|
+char *C_old_top;
|
|
|
+char *C_old_opp;
|
|
|
+#ifdef INCORE
|
|
|
+char *C_current_out = obuf;
|
|
|
+#endif
|
|
|
|
|
|
-C_putbyte(c)
|
|
|
- int c;
|
|
|
-{
|
|
|
- if (C_ontmpfile) {
|
|
|
+C_flush() {
|
|
|
#ifdef INCORE
|
|
|
- static unsigned sz;
|
|
|
+ static unsigned int bufsiz;
|
|
|
|
|
|
- if (BASE == 0) {
|
|
|
- BASE = Malloc(BUFSIZ);
|
|
|
- sz = BUFSIZ;
|
|
|
+ if (C_ontmpfile) {
|
|
|
+ if (C_BASE == 0) {
|
|
|
+ C_BASE = Malloc(BUFFERSIZ);
|
|
|
+ bufsiz = BUFFERSIZ;
|
|
|
+ C_current_out = C_BASE;
|
|
|
}
|
|
|
- else if (C_current_out >= sz) {
|
|
|
- BASE = Srealloc(BASE, (sz <<= 1));
|
|
|
+ else {
|
|
|
+ C_BASE = Srealloc(C_BASE, (bufsiz << 1));
|
|
|
+ C_current_out = C_BASE + bufsiz;
|
|
|
+ bufsiz <<= 1;
|
|
|
}
|
|
|
- *(BASE + C_current_out) = c;
|
|
|
-#endif
|
|
|
- C_current_out++;
|
|
|
-#ifdef INCORE
|
|
|
+ C_top = C_BASE + bufsiz;
|
|
|
return;
|
|
|
+ }
|
|
|
#endif
|
|
|
+ if (C_opp != obuf && sys_write(C_ofp, obuf, C_opp - obuf) == 0) {
|
|
|
+ C_failed();
|
|
|
}
|
|
|
- put(c);
|
|
|
+ C_opp = obuf;
|
|
|
}
|
|
|
|
|
|
#ifndef INCORE
|
|
|
-C_switchtotmp()
|
|
|
-{
|
|
|
- if (tmpfile == 0) {
|
|
|
- static char tmpbuf[64];
|
|
|
- register char *p = tmpbuf;
|
|
|
-
|
|
|
- strcpy(p, TMP_DIR);
|
|
|
- strcat(p, "/CodeXXXXXX");
|
|
|
- tmpfile = mktemp(p);
|
|
|
- if (! sys_open(p, OP_WRITE, &old_ofp)) {
|
|
|
- C_failed();
|
|
|
- }
|
|
|
- if (! sys_open(p, OP_READ, &tfr)) {
|
|
|
- C_failed();
|
|
|
- }
|
|
|
- }
|
|
|
- if (! C_ontmpfile) {
|
|
|
- File *p = ofp;
|
|
|
-
|
|
|
- flush();
|
|
|
- ofp = old_ofp;
|
|
|
- old_ofp = p;
|
|
|
- C_ontmpfile = 1;
|
|
|
- }
|
|
|
-}
|
|
|
+#define Xputbyte(c) if (C_ontmpfile) C_current_out++; put(c)
|
|
|
+#else
|
|
|
+#define Xputbyte(c) put(c)
|
|
|
+#endif
|
|
|
|
|
|
-C_switchtoout()
|
|
|
+C_putbyte(c)
|
|
|
+ int c;
|
|
|
{
|
|
|
- if (C_ontmpfile) {
|
|
|
- File *p = ofp;
|
|
|
-
|
|
|
- flush();
|
|
|
- ofp = old_ofp;
|
|
|
- old_ofp = p;
|
|
|
- C_ontmpfile = 0;
|
|
|
- }
|
|
|
+ Xputbyte(c);
|
|
|
}
|
|
|
-#endif
|
|
|
+
|
|
|
+#define C_putbyte Xputbyte
|
|
|
|
|
|
C_init(w, p)
|
|
|
arith w, p;
|
|
@@ -118,9 +95,9 @@ C_open(nm)
|
|
|
*/
|
|
|
|
|
|
if (nm == 0)
|
|
|
- ofp = STDOUT; /* standard output */
|
|
|
+ C_ofp = STDOUT; /* standard output */
|
|
|
else
|
|
|
- if (sys_open(nm, OP_WRITE, &ofp) == 0)
|
|
|
+ if (sys_open(nm, OP_WRITE, &C_ofp) == 0)
|
|
|
return 0;
|
|
|
return 1;
|
|
|
}
|
|
@@ -131,119 +108,35 @@ C_close()
|
|
|
*/
|
|
|
|
|
|
#ifndef INCORE
|
|
|
- flush();
|
|
|
+ C_flush();
|
|
|
if (tmpfile) {
|
|
|
- C_switchtotmp();
|
|
|
- sys_close(ofp);
|
|
|
- ofp = old_ofp;
|
|
|
+ (*C_switchtotmp)();
|
|
|
+ sys_close(C_ofp);
|
|
|
#else
|
|
|
- if (BASE) {
|
|
|
+ if (C_BASE) {
|
|
|
#endif
|
|
|
if (C_curr_part) {
|
|
|
- C_curr_part->p_parts->pp_end = C_current_out;
|
|
|
+ C_curr_part->p_parts->pp_end = C_current_out - C_BASE;
|
|
|
}
|
|
|
+ (*C_switchtoout)();
|
|
|
if (! C_sequential) {
|
|
|
- outpart(0);
|
|
|
+ (*C_outpart)(0);
|
|
|
}
|
|
|
#ifndef INCORE
|
|
|
- sys_close(tfr);
|
|
|
+ sys_close(C_tfr);
|
|
|
sys_remove(tmpfile);
|
|
|
if (ibuf) free(ibuf);
|
|
|
#else
|
|
|
- free(BASE);
|
|
|
+ free(C_BASE);
|
|
|
#endif
|
|
|
}
|
|
|
- flush();
|
|
|
- if (ofp != STDOUT)
|
|
|
- sys_close(ofp);
|
|
|
- ofp = 0;
|
|
|
+ C_flush();
|
|
|
+ if (C_ofp != STDOUT)
|
|
|
+ sys_close(C_ofp);
|
|
|
+ C_ofp = 0;
|
|
|
}
|
|
|
|
|
|
C_busy()
|
|
|
{
|
|
|
- return ofp != 0; /* true if code is being generated */
|
|
|
+ return C_ofp != 0; /* true if code is being generated */
|
|
|
}
|
|
|
-
|
|
|
-#ifndef INCORE
|
|
|
-static int
|
|
|
-getbyte(b)
|
|
|
- long b;
|
|
|
-{
|
|
|
- /* Get the byte at offset "b" from the start of the
|
|
|
- temporary file, and try to do so in an efficient way.
|
|
|
- */
|
|
|
- static long start_core, curr_pos;
|
|
|
-
|
|
|
- if (b < start_core || b >= curr_pos) {
|
|
|
- /* the block wanted is not in core, so get it */
|
|
|
- long nb = (b & ~(BUFSIZ - 1));
|
|
|
- int n;
|
|
|
-
|
|
|
- flush();
|
|
|
- if (nb != curr_pos) {
|
|
|
- if (sys_seek(tfr, nb, 0, &curr_pos) == 0) {
|
|
|
- C_failed();
|
|
|
- }
|
|
|
- }
|
|
|
- if (! ibuf) {
|
|
|
- ibuf = Malloc(BUFSIZ);
|
|
|
- }
|
|
|
- if (sys_read(tfr, ibuf, BUFSIZ, &n) == 0) {
|
|
|
- C_failed();
|
|
|
- }
|
|
|
- curr_pos += n;
|
|
|
- start_core = nb;
|
|
|
- }
|
|
|
-
|
|
|
- return ibuf[(int) (b - start_core)];
|
|
|
-}
|
|
|
-#endif
|
|
|
-
|
|
|
-C_out_parts(pp)
|
|
|
- register PartOfPart *pp;
|
|
|
-{
|
|
|
- /* Output the list of chunks started by "pp".
|
|
|
- The list is build in reverse order, so this routine is
|
|
|
- recursive.
|
|
|
- */
|
|
|
-
|
|
|
- if (!pp) return;
|
|
|
- if (pp->pp_next) C_out_parts(pp->pp_next);
|
|
|
-
|
|
|
- if (pp->pp_type == INSERT) {
|
|
|
- outpart(pp->pp_id);
|
|
|
- }
|
|
|
- else {
|
|
|
- /* copy the chunk to output */
|
|
|
-#ifdef INCORE
|
|
|
- register char *s = BASE + pp->pp_begin;
|
|
|
- char *se = BASE + pp->pp_end;
|
|
|
-
|
|
|
- while (s < se) {
|
|
|
- put(*s++);
|
|
|
- }
|
|
|
-#else
|
|
|
- register long b = pp->pp_begin;
|
|
|
-
|
|
|
- while (b < pp->pp_end) {
|
|
|
- put(getbyte(b++));
|
|
|
- }
|
|
|
-#endif
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-Part *
|
|
|
-C_findpart(part)
|
|
|
- int part;
|
|
|
-{
|
|
|
- /* Look for part "part" in the table.
|
|
|
- Return 0 if not present,
|
|
|
- */
|
|
|
- register Part *p = C_stable[part % TABSIZ];
|
|
|
-
|
|
|
- while (p && p->p_id != part) {
|
|
|
- p = p->p_next;
|
|
|
- }
|
|
|
- return p;
|
|
|
-}
|
|
|
-
|