cvsuser 03/11/22 03:20:13
Modified: include/parrot packfile.h
pf pf_items.c
src packfile.c packout.c
Log:
PackFile-20
* PF_store_item functions
* PF_size_item functions
Revision Changes Path
1.53 +13 -1 parrot/include/parrot/packfile.h
Index: packfile.h
===================================================================
RCS file: /cvs/public/parrot/include/parrot/packfile.h,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -w -r1.52 -r1.53
--- packfile.h 21 Nov 2003 16:28:28 -0000 1.52
+++ packfile.h 22 Nov 2003 11:19:59 -0000 1.53
@@ -1,6 +1,6 @@
/* packfile.h
*
-* $Id: packfile.h,v 1.52 2003/11/21 16:28:28 leo Exp $
+* $Id: packfile.h,v 1.53 2003/11/22 11:19:59 leo Exp $
*
* History:
* Rework by Melvin; new bytecode format, make bytecode portable.
@@ -348,6 +348,18 @@
FLOATVAL PF_fetch_number(struct PackFile *pf, opcode_t **stream);
STRING* PF_fetch_string(Parrot_Interp, struct PackFile *pf, opcode_t **stream);
char * PF_fetch_cstring(struct PackFile *pf, opcode_t **stream);
+
+size_t PF_size_opcode(void);
+size_t PF_size_integer(void);
+size_t PF_size_number(void);
+size_t PF_size_string(STRING *);
+size_t PF_size_cstring(const char *);
+
+opcode_t* PF_store_opcode(opcode_t *, opcode_t);
+opcode_t* PF_store_integer(opcode_t *, INTVAL);
+opcode_t* PF_store_number (opcode_t *, FLOATVAL *);
+opcode_t* PF_store_string (opcode_t *, STRING *);
+opcode_t* PF_store_cstring(opcode_t *, const char *);
void PackFile_assign_transforms(struct PackFile *pf);
1.2 +178 -7 parrot/pf/pf_items.c
Index: pf_items.c
===================================================================
RCS file: /cvs/public/parrot/pf/pf_items.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- pf_items.c 21 Nov 2003 13:49:28 -0000 1.1
+++ pf_items.c 22 Nov 2003 11:20:09 -0000 1.2
@@ -1,7 +1,7 @@
/* pf_items.c
* Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
* CVS Info
- * $Id: pf_items.c,v 1.1 2003/11/21 13:49:28 leo Exp $
+ * $Id: pf_items.c,v 1.2 2003/11/22 11:20:09 leo Exp $
* Overview:
* Low level packfile functions to fetch and store Parrot data, i.e.
* INTVAL, FLOATVAL, STRING ...
@@ -15,20 +15,41 @@
* byte-ordering
* PF_fetch_<type> read items and possibly convert the foreign
* format
+ * PF_size_<type> return the needed size in opcode_t units
+ *
* References:
*/
#include "parrot/parrot.h"
+#include <assert.h>
#define TRACE_PACKFILE 0
#define TRACE_PACKFILE_PMC 0
-#define ROUND_UP(val,size) ((((val) + (size - 1))/(size)) * (size))
+/*
+ * round val up to whole size, return result in bytes
+ */
+#define ROUND_UP_B(val,size) ((((val) + (size - 1))/(size)) * (size))
+
+/*
+ * round val up to whole opcode_t, return result in opcodes
+ */
+#define ROUND_UP(val,size) (((val) + ((size) - 1))/(size))
+
/*
=head1 pf_items
-Parrot data items fetch and store functions
+Parrot data items fetch and store functions.
+
+PF_fetch_<item> functions retrieve the datatype item from the opcode
+stream and convert byteordering or binary format on the fly, depending
+on the packfile header.
+
+PF_store_<item> functions write the datatype item to the stream as is.
+These functions don't check the available size.
+
+PF_size_<item> functions return the store size of item in opcode_t units.
=over 4
@@ -148,6 +169,14 @@
Fetch an opcode_t from the stream, converting byteorder if needed.
+=item PF_store_opcode
+
+Store an opcode_t to stream as is.
+
+=item PF_size_opcode
+
+Return size of item in opcode_t units, which is 1 per definitionem.
+
=cut
***************************************/
@@ -165,16 +194,36 @@
return o;
}
+opcode_t*
+PF_store_opcode(opcode_t *cursor, opcode_t val)
+{
+ *cursor++ = val;
+ return cursor;
+}
+
+size_t
+PF_size_opcode(void)
+{
+ return 1;
+}
+
/***************************************
=item PF_fetch_integer
-Fetch an INTVAL from the stream, converting
-byteorder if needed.
+Fetch an INTVAL from the stream, converting byteorder if needed.
XXX assumes sizeof(INTVAL) == sizeof(opcode_t) - we don't have
INTVAL size in PF header
+=item PF_store_integer
+
+Store an INTVAL to stream as is.
+
+=item PF_size_integer
+
+Return store size of INTVAL in opcode_t units.
+
=cut
***************************************/
@@ -193,6 +242,19 @@
}
+opcode_t*
+PF_store_integer(opcode_t *cursor, INTVAL val)
+{
+ *cursor++ = (opcode_t)val; /* XXX */
+ return cursor;
+}
+
+size_t
+PF_size_integer(void)
+{
+ size_t s = sizeof(INTVAL) / sizeof(opcode_t);
+ return s ? s : 1;
+}
/***************************************
@@ -202,6 +264,14 @@
byteorder if needed. Then advance stream pointer by
amount of packfile float size.
+=item PF_store_number
+
+Write a FLOATVAL to the opcode stream as is.
+
+=item PF_size_number
+
+Return store size of FLOATVAL in opcode_t units.
+
=cut
***************************************/
@@ -239,6 +309,23 @@
}
return f;
}
+
+opcode_t*
+PF_store_number(opcode_t *cursor, FLOATVAL *val)
+{
+ opcode_t padded_size = (sizeof(FLOATVAL) + sizeof(opcode_t) - 1) /
+ sizeof(opcode_t);
+ mem_sys_memcopy(cursor, val, sizeof(FLOATVAL));
+ cursor += padded_size;
+ return cursor;
+}
+
+size_t
+PF_size_number(void)
+{
+ return ROUND_UP(sizeof(FLOATVAL), sizeof(opcode_t));
+}
+
/*
=item PF_fetch_string
@@ -252,6 +339,14 @@
opcode_t size
* data
+=item PF_store_string
+
+Write a STRING to the opcode stream.
+
+=item PF_size_string
+
+Report store size of STRING in opcode_t units.
+
=cut
*/
@@ -288,19 +383,77 @@
flags,
chartype_lookup_index(type));
- size = ROUND_UP(size, wordsize) / sizeof(opcode_t);
+ size = ROUND_UP_B(size, wordsize) / sizeof(opcode_t);
*cursor += size;
return s;
}
+opcode_t*
+PF_store_string(opcode_t *cursor, STRING *s)
+{
+ opcode_t padded_size = s->bufused;
+ char *charcursor;
+ size_t i;
+
+ if (padded_size % sizeof(opcode_t)) {
+ padded_size += sizeof(opcode_t) - (padded_size % sizeof(opcode_t));
+ }
+
+ *cursor++ = PObj_get_FLAGS(s); /* only constant_FLAG */
+ *cursor++ = s->encoding->index;
+ *cursor++ = s->type->index;
+ *cursor++ = s->bufused;
+
+ /* Switch to char * since rest of string is addressed by
+ * characters to ensure padding. */
+ charcursor = (char *)cursor;
+
+ if (s->strstart) {
+ mem_sys_memcopy(charcursor, s->strstart, s->bufused);
+ charcursor += s->bufused;
+
+ if (s->bufused % sizeof(opcode_t)) {
+ for (i = 0; i < (sizeof(opcode_t) -
+ (s->bufused % sizeof(opcode_t))); i++) {
+ *charcursor++ = 0;
+ }
+ }
+ }
+ assert( ((int)charcursor & 3) == 0);
+ LVALUE_CAST(char *, cursor) = charcursor;
+ return cursor;
+}
+
+size_t
+PF_size_string(STRING *s)
+{
+ opcode_t padded_size = s->bufused;
+
+ if (padded_size % sizeof(opcode_t)) {
+ padded_size += sizeof(opcode_t) - (padded_size % sizeof(opcode_t));
+ }
+
+ /* Include space for flags, encoding, type, and size fields. */
+ return 4 + (size_t)padded_size / sizeof(opcode_t);
+}
+
/*
=item PF_fetch_cstring
Fetch a cstring from bytecode and return an allocated copy
+=item PF_store_cstring
+
+Write a 0-terminate string to the stream.
+
+=item PF_size_cstring
+
+Return store size of a C-string in opcode_t units.
+
=cut
*/
+
char *
PF_fetch_cstring(struct PackFile *pf, opcode_t **cursor)
{
@@ -309,9 +462,27 @@
int wordsize = pf->header->wordsize;
strcpy(p, (char*) (*cursor));
- (*cursor) += ROUND_UP(str_len, wordsize) / sizeof(opcode_t);
+ (*cursor) += ROUND_UP_B(str_len, wordsize) / sizeof(opcode_t);
return p;
}
+
+opcode_t*
+PF_store_cstring(opcode_t *cursor, const char *s)
+{
+ strcpy((char *) cursor, s);
+ return cursor + PF_size_cstring(s);
+}
+
+size_t
+PF_size_cstring(const char *s)
+{
+ size_t str_len;
+
+ assert(s);
+ str_len = strlen(s);
+ return ROUND_UP(str_len + 1, sizeof(opcode_t));
+}
+
/*
=back
1.122 +12 -29 parrot/src/packfile.c
Index: packfile.c
===================================================================
RCS file: /cvs/public/parrot/src/packfile.c,v
retrieving revision 1.121
retrieving revision 1.122
diff -u -w -r1.121 -r1.122
--- packfile.c 22 Nov 2003 09:55:49 -0000 1.121
+++ packfile.c 22 Nov 2003 11:20:13 -0000 1.122
@@ -7,7 +7,7 @@
** This program is free software. It is subject to the same
** license as Parrot itself.
**
-** $Id: packfile.c,v 1.121 2003/11/22 09:55:49 leo Exp $
+** $Id: packfile.c,v 1.122 2003/11/22 11:20:13 leo Exp $
**
** History:
** Rework by Melvin; new bytecode format, make bytecode portable.
@@ -75,10 +75,11 @@
struct PackFile_Segment *self, opcode_t *);
static void pf_debug_destroy (struct PackFile_Segment *self);
-/* internal definitions */
+/*
+ * round val up to whole opcode_t, return result in opcodes
+ */
#define ROUND_UP(val,size) (((val) + ((size) - 1))/(size))
-size_t cstring_packed_size(const char *s);
/******************************************************************************
=head1 PackFile Manipulation Functions
@@ -95,17 +96,6 @@
*/
-size_t
-cstring_packed_size(const char *s)
-{
- UINTVAL str_len;
-
- if (!s)
- return 0;
-
- str_len = strlen(s);
- return ROUND_UP(str_len + 1, sizeof(opcode_t));
-}
/***************************************
@@ -1218,11 +1208,12 @@
debug->filename = NULL;
return (struct PackFile_Segment *)debug;
}
+
static size_t
pf_debug_packed_size (struct PackFile_Segment *self)
{
struct PackFile_Debug *debug = (struct PackFile_Debug *) self;
- return cstring_packed_size(debug->filename);
+ return PF_size_cstring(debug->filename);
}
static opcode_t *
@@ -1230,7 +1221,7 @@
{
struct PackFile_Debug *debug = (struct PackFile_Debug *) self;
strcpy ((char *)cursor, debug->filename);
- cursor += cstring_packed_size(debug->filename);
+ cursor += PF_size_cstring(debug->filename);
return cursor;
}
@@ -1418,7 +1409,7 @@
switch (ft->fixups[i]->type) {
case enum_fixup_label:
case enum_fixup_sub:
- size += cstring_packed_size(ft->fixups[i]->name);
+ size += PF_size_cstring(ft->fixups[i]->name);
size ++; /* offset */
break;
default:
@@ -1442,7 +1433,7 @@
case enum_fixup_label:
case enum_fixup_sub:
strcpy ((char *)cursor, ft->fixups[i]->name);
- cursor += cstring_packed_size(ft->fixups[i]->name);
+ cursor += PF_size_cstring(ft->fixups[i]->name);
*cursor++ = ft->fixups[i]->offset;
break;
default:
@@ -1813,19 +1804,11 @@
switch (self->type) {
case PFC_NUMBER:
- packed_size = (sizeof(FLOATVAL) + sizeof(opcode_t) - 1)/
- sizeof(opcode_t);
+ packed_size = PF_size_number();
break;
case PFC_STRING:
- padded_size = self->u.string->bufused;
-
- if (padded_size % sizeof(opcode_t)) {
- padded_size += sizeof(opcode_t) - (padded_size % sizeof(opcode_t));
- }
-
- /* Include space for flags, encoding, type, and size fields. */
- packed_size = 4 + (size_t)padded_size / sizeof(opcode_t);
+ packed_size = PF_size_string(self->u.string);
break;
case PFC_KEY:
@@ -1846,7 +1829,7 @@
case enum_class_Closure:
case enum_class_Continuation:
case enum_class_Coroutine:
- packed_size = cstring_packed_size(
+ packed_size = PF_size_cstring(
((struct Parrot_Sub*)PMC_sub(component))->packed);
break;
default:
1.32 +5 -41 parrot/src/packout.c
Index: packout.c
===================================================================
RCS file: /cvs/public/parrot/src/packout.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -w -r1.31 -r1.32
--- packout.c 21 Nov 2003 16:28:30 -0000 1.31
+++ packout.c 22 Nov 2003 11:20:13 -0000 1.32
@@ -7,7 +7,7 @@
** This program is free software. It is subject to the same
** license as Parrot itself.
**
-** $Id: packout.c,v 1.31 2003/11/21 16:28:30 leo Exp $
+** $Id: packout.c,v 1.32 2003/11/22 11:20:13 leo Exp $
** History:
** Rework by Melvin; new bytecode format, make bytecode portable.
** (Do endian conversion and wordsize transforms on the fly.)
@@ -159,58 +159,23 @@
(Note this padding is not yet implemented for FLOATVALs.)
***************************************/
-/* this will go away soon */
-extern size_t cstring_packed_size(const char *s);
opcode_t *
PackFile_Constant_pack(struct PackFile_Constant *self, opcode_t *cursor)
{
- char *charcursor;
- size_t i;
- opcode_t padded_size;
struct PMC *key;
+ size_t i;
*cursor++ = self->type;
switch (self->type) {
case PFC_NUMBER:
- padded_size = (sizeof(FLOATVAL) + sizeof(opcode_t) - 1) /
- sizeof(opcode_t);
- mem_sys_memcopy(cursor, &self->u.number, sizeof(FLOATVAL));
- cursor += padded_size;
+ cursor = PF_store_number(cursor, &self->u.number);
break;
case PFC_STRING:
- padded_size = self->u.string->bufused;
-
- if (padded_size % sizeof(opcode_t)) {
- padded_size += sizeof(opcode_t) - (padded_size % sizeof(opcode_t));
- }
-
- *cursor++ = PObj_get_FLAGS(self->u.string); /* only constant_FLAG */
- *cursor++ = self->u.string->encoding->index;
- *cursor++ = self->u.string->type->index;
- *cursor++ = self->u.string->bufused;
-
- /* Switch to char * since rest of string is addressed by
- * characters to ensure padding. */
- charcursor = (char *)cursor;
-
- if (self->u.string->strstart) {
- mem_sys_memcopy(charcursor, self->u.string->strstart,
- self->u.string->bufused);
- charcursor += self->u.string->bufused;
-
- if (self->u.string->bufused % sizeof(opcode_t)) {
- for (i = 0; i < (sizeof(opcode_t) -
- (self->u.string->bufused % sizeof(opcode_t))); i++) {
- *charcursor++ = 0;
- }
- }
- }
- assert( ((int)charcursor & 3) == 0);
- LVALUE_CAST(char *, cursor) = charcursor;
+ cursor = PF_store_string(cursor, self->u.string);
break;
case PFC_PMC:
@@ -222,11 +187,10 @@
case enum_class_Coroutine:
{
char *s = ((struct Parrot_Sub*)PMC_sub(key))->packed;
- strcpy((char *) cursor, s);
#if TRACE_PACKFILE_PMC
fprintf(stderr, "PMC_packed '%s'\n", (char*) cursor);
#endif
- cursor += cstring_packed_size(s);
+ cursor = PF_store_cstring(cursor, s);
}
break;
default: