diff --git a/src/code/README.txt b/src/code/README.txt
index 8bbaf69a829f7c56e7b4d63b07cf3df25656402d..95f245cf2e091e514d604d222d9059be2d772162 100644
--- a/src/code/README.txt
+++ b/src/code/README.txt
@@ -48,3 +48,12 @@ void RELOCATE_program(struct program *p, PIKE_OPCODE_T *new);
 
 void FLUSH_INSTRUCTION_CACHE(void *addr, size_t len);
 	Flush the memory at 'addr' from the instruction cache.
+
+void ENCODE_PROGRAM(struct program *p, struct dynamic_buffer_s *buf);
+	Encode 'p'->program in a way accepted by DECODE_PROGRAM().
+	NOTE: The encoded data MUST have the length p->num_program *
+	      sizeof(PIKE_OPCODE_T).
+
+void DECODE_PROGRAM(struct program *p)
+	Decode 'p'->program as encoded by ENCODE_PROGRAM().
+	NOTE: 'p'->relocations is valid at this point.
diff --git a/src/code/sparc.c b/src/code/sparc.c
index dafec8e71161c3995b2cf60e6a32a7cf537cbb01..c368cb321f14f4b742d7c18c67871afd6500ef47 100644
--- a/src/code/sparc.c
+++ b/src/code/sparc.c
@@ -1,5 +1,5 @@
 /*
- * $Id: sparc.c,v 1.2 2001/07/20 13:16:49 grubba Exp $
+ * $Id: sparc.c,v 1.3 2001/07/20 16:21:32 grubba Exp $
  *
  * Machine code generator for sparc.
  *
@@ -64,3 +64,55 @@ void ins_f_byte_with_2_args(unsigned int a,
   ins_f_byte(a);
   return;
 }
+
+#define addstr(s, l) low_my_binary_strcat((s), (l), buf)
+#define adddata2(s,l) addstr((char *)(s),(l) * sizeof((s)[0]));
+
+void sparc_encode_program(struct program *p, struct dynamic_buffer_s *buf)
+{
+  size_t prev = 0, rel;
+  /* De-relocate the program... */
+  for (rel = 0; rel < p->num_relocations; rel++) {
+    size_t off = p->relocations[rel];
+    INT32 opcode;
+#ifdef PIKE_DEBUG
+    if (off < prev) {
+      fatal("Relocations in bad order!\n");
+    }
+#endif /* PIKE_DEBUG */
+    adddata2(p->program + prev, off - prev);
+
+#ifdef PIKE_DEBUG
+    if ((p->program[off] & 0xc0000000) != 0x40000000) {
+      fatal("Bad relocation!\n");
+    }
+#endif /* PIKE_DEBUG */
+    /* Relocate to being relative to NULL */
+    opcode = 0x40000000 |
+      ((p->program[off] + (((INT32)(p->program)>>2))) & 0x3fffffff);
+    adddata2(&opcode, 1);
+    prev = off+1;
+  }
+  adddata2(p->program + prev, p->num_program - prev);
+}
+
+void sparc_decode_program(struct program *p)
+{
+  /* Relocate the program... */
+  PIKE_OPCODE_T *prog = p->program;
+  INT32 delta = ((INT32)p->program)>>2;
+  size_t rel = p->num_relocations;
+  while (rel--) {
+#ifdef PIKE_DEBUG
+    if ((prog[p->relocations[rel]] & 0xc0000000) != 0x40000000) {
+      Pike_error("Bad relocation: %d, off:%d, opcode: 0x%08x\n",
+		 rel, p->relocations[rel],
+		 prog[p->relocations[rel]]);
+    }
+#endif /* PIKE_DEBUG */
+    prog[p->relocations[rel]] = 0x40000000 |
+      (((prog[p->relocations[rel]] & 0x3fffffff) - delta) &
+       0x3fffffff);
+  }
+#endif /* sparc */
+}
diff --git a/src/code/sparc.h b/src/code/sparc.h
index 24bde51d56cd14b52f8f67294f7d735ff00a0358..c71f119f27b7a9d7e497c9384992d3e7e92d0562 100644
--- a/src/code/sparc.h
+++ b/src/code/sparc.h
@@ -1,5 +1,5 @@
 /*
- * $Id: sparc.h,v 1.3 2001/07/20 15:49:00 grubba Exp $
+ * $Id: sparc.h,v 1.4 2001/07/20 16:21:32 grubba Exp $
  */
 
 #define REG_O0	8
@@ -84,3 +84,11 @@
       cnt_ += 8;					\
     } while (cnt_ < max_);				\
   } while(0)
+
+struct dynamic_buffer_s;
+
+void sparc_encode_program(struct program *p, struct dynamic_buffer_s *buf);
+void sparc_decode_program(struct program *p);
+
+#define ENCODE_PROGRAM(P, BUF)	sparc_encode_program(P, BUF)
+#define DECODE_PROGRAM(P)	sparc_decode_program(p)
diff --git a/src/encode.c b/src/encode.c
index 5dbc3073ca49ba21995a8f249114423c94e7fb8a..47ed5b91ab3cac1cd9ebca0fe7d570ddf948bb27 100644
--- a/src/encode.c
+++ b/src/encode.c
@@ -25,7 +25,7 @@
 #include "version.h"
 #include "bignum.h"
 
-RCSID("$Id: encode.c,v 1.114 2001/07/19 19:05:37 grubba Exp $");
+RCSID("$Id: encode.c,v 1.115 2001/07/20 16:21:31 grubba Exp $");
 
 /* #define ENCODE_DEBUG */
 
@@ -914,43 +914,13 @@ static void encode_value2(struct svalue *val, struct encode_data *data)
 	code_number( p->PIKE_CONCAT(num_,Z), data);
 #include "program_areas.h"
 
-#ifdef PIKE_USE_MACHINE_CODE
-#ifdef sparc
-	/* De-relocate the program... */
-	{
-	  size_t prev = 0, rel;
-	  for (rel = 0; rel < p->num_relocations; rel++) {
-	    size_t off = p->relocations[rel];
-	    INT32 opcode;
-#ifdef PIKE_DEBUG
-	    if (off < prev) {
-	      fatal("Relocations in bad order!\n");
-	    }
-#endif /* PIKE_DEBUG */
-	    adddata2(p->program + prev, off - prev);
-
-#ifdef PIKE_DEBUG
-	    if ((p->program[off] & 0xc0000000) != 0x40000000) {
-	      fatal("Bad relocation!\n");
-	    }
-#endif /* PIKE_DEBUG */
-	    /* Relocate to being relative to NULL */
-	    opcode = 0x40000000 |
-	      ((p->program[off] + (((INT32)(p->program)>>2))) & 0x3fffffff);
-	    adddata2(&opcode, 1);
-	    prev = off+1;
-	  }
-	  adddata2(p->program + prev, p->num_program - prev);
-	}
-#else
+#ifdef ENCODE_PROGRAM
+	ENCODE_PROGRAM(p, &(data->buf));
+#else /* !ENCODE_PROGRAM */
 	adddata2(p->program, p->num_program);
-#endif /* sparc */
-
+#endif /* ENCODE_PROGRAM */
+#ifdef PIKE_USE_MACHINE_CODE
 	adddata2(p->relocations, p->num_relocations);
-#else /* !PIKE_USE_MACHINE_CODE */
-
-	adddata2(p->program, p->num_program);
-
 #endif /* PIKE_USE_MACHINE_CODE */
 
 	adddata2(p->linenumbers, p->num_linenumbers);
@@ -2089,28 +2059,12 @@ static void decode_value2(struct decode_data *data)
 	  getdata2(p->program, p->num_program);
 #ifdef PIKE_USE_MACHINE_CODE
 	  getdata2(p->relocations, p->num_relocations);
-
-#ifdef sparc
-	  /* Relocate the program... */
-	  {
-	    PIKE_OPCODE_T *prog = p->program;
-	    INT32 delta = ((INT32)p->program)>>2;
-	    size_t rel = p->num_relocations;
-	    while (rel--) {
-#ifdef PIKE_DEBUG
-	      if ((prog[p->relocations[rel]] & 0xc0000000) != 0x40000000) {
-		fatal("Bad relocation: %d, off:%d, opcode: 0x%08x\n",
-		      rel, p->relocations[rel],
-		      prog[p->relocations[rel]]);
-	      }
-#endif /* PIKE_DEBUG */
-	      prog[p->relocations[rel]] = 0x40000000 |
-		(((prog[p->relocations[rel]] & 0x3fffffff) - delta) &
-		 0x3fffffff);
-	    }
-	  }
-#endif /* sparc */
 #endif /* PIKE_USE_MACHINE_CODE */
+
+#ifdef DECODE_PROGRAM
+	  DECODE_PROGRAM(p);
+#endif /* DECODE_PROGRAM */
+
 	  getdata2(p->linenumbers, p->num_linenumbers);
 
 #ifdef DEBUG_MALLOC