Fix minor typos.
[parrot.git] / docs / pdds / pdd13_bytecode.pod
blob99b06d6309ea81fc0618e31a4ef82f2c23c33c83
1 # Copyright (C) 2001-2005, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 docs/pdds/pdd13_bytecode.pod - Parrot Bytecode
8 =head1 ABSTRACT
10 This PDD describes the file format for Parrot Bytecode (PBC) files and the
11 interface through which they may be manipulated programatically.
13 =head1 VERSION
15 $Revision$
17 =head1 DESCRIPTION
19 =over 4
21 =item - The sequence of instructions making up a Parrot program, a constants
22 table and debug data are all stored in a binary format called a packfile or
23 PBC (Parrot Bytecode File).
25 =item - A PBC file can be read by Parrot on any platform, but may be encoded
26 more optimally for a particular platform.
28 =item - It is possible to add arbitrary annotations to the instruction
29 sequence, for example line numbers in the high level language and other debug
30 data.
32 =item - PMCs will be used to represent packfiles and packfile segments to
33 provide a programming interface to them, both from Parrot programs and the
34 Parrot internals.
36 =back
39 =head1 DEFINITIONS
41 None.
44 =head1 IMPLEMENTATION
46 =head2 Changes From The Current Implementation
48 A number of things in this proposed PDD differ from what is currently
49 implemented. This section details these changes and some of the reasoning
50 behind them.
53 =head3 Packfile Header
55 The format of the packfile header has changed completely, based upon a
56 proposal at
57 L<http://groups.google.com/group/perl.perl6.internals/browse_thread/thread/1f1af615edec7449/ebfdbb5180a9d813?lnk=gst>
58 and the requirement to have a UUID. I also observed that the INT field in the
59 previous header format is used nowhere in Parrot and appears redundant, and
60 that we were missing storing a patch version number along with the major and
61 minor, which made the version number less useful. The opcode type is also gone
62 due to non-use.
64 The version number now reflects the earliest version of Parrot that is capable
65 of running the bytecode file, to enable cross-version compatibility that will
66 be needed in the future.
69 =head3 Segment Header
71 Having the type associated with the segment inside the VM is fine, but since
72 it is in the directory segment anyway it seems odd to duplicate it here. Also
73 removed the id (did not seem to be used anywhere) and the second size (always
74 computable by knowing the size of this header, so it appears redundant).
77 =head3 Fixup Segment
79 We need to support unicode sub names, so fixup labels should be an index into
80 the constants table to the relevant string instead of just a C string as they
81 are now.
84 =head3 Annotations Segment
86 This is new and replaces and builds upon the debug segment. See here for some
87 on-list discussion:
89 L<http://groups.google.com/group/perl.perl6.internals/browse_thread/thread/b0d36dafb42d96c4/4d6ad2ad2243e677?lnk=gst&rnum=2#4d6ad2ad2243e677>
92 =head3 Packfile PMCs
94 This idea will see packfiles and segments within them being represented by
95 PMCs, easing memory management and providing an interface to packfiles for
96 Parrot programs.
98 This part of the proposal is based upon a few previous discussions, mostly on
99 IRC or in realspace. Here is a mailing list comments that provide one of the
100 motivations or hints of the proposa.
102 L<http://groups.google.com/group/perl.perl6.internals/browse_thread/thread/778ea0ac4c8676f7/b249306b543b040a?lnk=gst&q=packfile+PMCs&rnum=2#b249306b543b040a>
106 =head2 Packfiles
108 This section of the documentation describes the format of Parrot packfiles.
109 These contain the bytecode (sequence of instructions), constants table, fixup
110 table, debug data, annotations and possibly more.
112 Note that, unless otherwise stated, all offsets and lengths are given in terms
113 of Parrot opcodes, not bytes. An opcode corresponds to a word size in length.
114 The word size is specified in the packfile header.
117 =head3 Packfile Header
119 PBC files start with a variable length header. All data in this header is
120 stored as strings or in a single byte so endianness and word size need not be
121 considered when reading it.
123 Note that in this section only, offsets and lengths are in bytes.
125   +--------+--------+--------------------------------------------------------+
126   | Offset | Length | Description                                            |
127   +--------+--------+--------------------------------------------------------+
128   | 0      | 8      | 0xFE 0x50 0x42 0x43 0x0D 0x0A 0x1A 0x0A                |
129   |        |        | Parrot "Magic String" to identify a PBC file. In C,    |
130   |        |        | this is the string C<\376PBC\r\n\032\n> (ASCII) or     |
131   |        |        | C<\xfe\x50\x42\x43\x0d\x0a\x1a\x0a>.                   |
132   +--------+--------+--------------------------------------------------------+
133   | 8      | 1      | Word size in bytes of words making up the segments of  |
134   |        |        | the PBC file. Must be one of:                          |
135   |        |        |    0x04 - 4 byte (32-bit) words                        |
136   |        |        |    0x08 - 8 byte (64-bit) words                        |
137   +--------+--------+--------------------------------------------------------+
138   | 9      | 1      | Byte order within the words making up the segments of  |
139   |        |        | the PBC file. Must be one of:                          |
140   |        |        |    0x00 - Little Endian                                |
141   |        |        |    0x01 - Big Endian                                   |
142   +--------+--------+--------------------------------------------------------+
143   | 10     | 1      | The encoding of floating point numbers in the file.    |
144   |        |        | Must be one of:                                        |
145   |        |        |    0x00 - IEEE 754 8 byte double                       |
146   |        |        |    0x01 - i386 little endian 12 byte long double       |
147   +--------+--------+--------------------------------------------------------+
148   | 11     | 1      | Major version number of the version of Parrot that     |
149   |        |        | wrote this bytecode file. For example, if Parrot 0.9.5 |
150   |        |        | wrote it, this byte would have the value 0.            |
151   +--------+--------+--------------------------------------------------------+
152   | 12     | 1      | Minor version number of the version of Parrot that     |
153   |        |        | wrote this bytecode file. For example, if Parrot 0.9.5 |
154   |        |        | wrote it, this byte would have the value 9.            |
155   +--------+--------+--------------------------------------------------------+
156   | 13     | 1      | Patch version number of the version of Parrot that     |
157   |        |        | wrote this bytecode file. For example, if Parrot 0.9.5 |
158   |        |        | wrote it, this byte would have the value 5.            |
159   +--------+--------+--------------------------------------------------------+
160   | 14     | 1      | Major version number of the bytecode file format. See  |
161   |        |        | the section below on bytecode file format version      |
162   |        |        | numbers.                                               |
163   +--------+--------+--------------------------------------------------------+
164   | 15     | 1      | Minor version number of the bytecode file format. See  |
165   |        |        | the section below on bytecode file format version      |
166   |        |        | numbers.                                               |
167   +--------+--------+--------------------------------------------------------+
168   | 16     | 1      | The type of the UUID associated with this packfile.    |
169   |        |        | Must be one of:                                        |
170   |        |        |    0x00 - No UUID                                      |
171   |        |        |    0x01 - MD5                                          |
172   +--------+--------+--------------------------------------------------------+
173   | 17     | 1      | Length of the UUID associated with this packfile. May  |
174   |        |        | be zero if the type of the UUID is 0x00. Maximum       |
175   |        |        | value is 255.                                          |
176   +--------+--------+--------------------------------------------------------+
177   | 18     | u      | A UUID of u bytes in length, where u was specified as  |
178   |        |        | the length of the UUID in the previous field. Be sure  |
179   |        |        | that UUIDs are stored and read as strings. The UUID is |
180   |        |        | computed by applying the hash function specified in    |
181   |        |        | the UUID type field over the entire packfile not       |
182   |        |        | including this header and its trailing zero padding.   |
183   +--------+--------+--------------------------------------------------------+
184   | 18 + u | n      | Zero-padding to make the total header length a         |
185   |        |        | multiple of 16 bytes in length.                        |
186   |        |        |    n = u % 16 == 0 ? 0 : 16 - (u % 16)                 |
187   +--------+--------+--------------------------------------------------------+
188   
189 Everything beyond the header is an opcode, with word length and byte ordering
190 as defined in the header. If the word length and byte ordering of the machine
191 that is reading the PBC file do not match these, it needs to transform the
192 words making up the rest of the packfile.
194 =head4 Bytecode File Version Numbers
196 The bytecode file version number exists to decouple the format of the bytecode
197 file from the version of the Parrot implementation that is reading/writing it.
198 It has a major and a minor part.
200 The major version number should be incremented whenever there is a change to
201 the layout of bytecode files. This includes new segments, changes to segment
202 headers or changes to the format of the data held within a segment.
204 The minor version number should be incremented in all other cases when a
205 change is made that means a previous version of Parrot would not be able to
206 run the program encoded in the packfile. This includes:
208 =over 4
210 =item Opcode renumbering
212 =item Addition of new opcodes and removal of existing ones
214 =item Addition of new core PMCs and removal of existing ones
216 =item Changes to the interface (externally visible behaviour) of an opcode or
219 =back
221 A single version of Parrot can support reading and writing of more than one
222 bytecode file format. In fact, once Parrot is in production use it will be
223 preferable to write as early a bytecode format as is possible, to allow the
224 greatest compatibility with previous Parrots.
226 These versions will be listed in the PBC_COMPAT file, sorted with the latest
227 version first in the format:
229 MAJOR.MINOR DATE DESCRIPTION
232 =head3 Directory Format Header
234 Packfiles contain a directory that describes the segments that it contains.
235 This header specifies the format of the directory.
237   +--------+--------+--------------------------------------------------------+
238   | Offset | Length | Description                                            |
239   +--------+--------+--------------------------------------------------------+
240   | 0      | 1      | The format of the directory. Must be:                  |
241   |        |        |    0x01 - Directory Format 1                           |
242   +--------+--------+--------------------------------------------------------+
243   | 1      | 3      | Must be:                                               |
244   |        |        |    0x00 0x00 0x00 - Reserved                           |
245   +--------+--------+--------------------------------------------------------+
247 Currently only Format 1 exists. In the future, the format of the directory may
248 change. A single version of Parrot may then become capable of generating and
249 reading files of more than one directory format. This header enables Parrot to
250 detect whether it is able to read the directory segment in the packfile.
252 This header must be followed immediately by a directory segment.
255 =head3 Packfile Segment Header
257 All segments, regardless of type, start with a 1 opcode segment header. All
258 other segments below are prefixed with this.
260   +--------+--------+--------------------------------------------------------+
261   | Offset | Length | Description                                            |
262   +--------+--------+--------------------------------------------------------+
263   | 0      | 1      | The total size of the segment in opcodes, including    |
264   |        |        | this header.                                           |
265   +--------+--------+--------------------------------------------------------+
268 =head3 Segment Padding
270 All segments must have trailing zero (NULL) values appended so they are a
271 multiple of 16 bytes in length. (This allows wordsize support of up to 
272 128 bits.)
275 =head3 Directory Segment
277 This segment lists the other segments that make up the packfile and where in
278 the file they are located. It must occur immediately after the directory
279 format header. Only one of these segments may occur in a packfile. In the
280 future, a hierarchy of directories may be allowed.
282 The directory segment adds one additional header after the standard packfile
283 header data, which specifies the number of entries in the directory.
285   +--------+--------+--------------------------------------------------------+
286   | Offset | Length | Description                                            |
287   +--------+--------+--------------------------------------------------------+
288   | 1      | 1      | The number of entries in the directory.                |
289   |        |        |    n                                                   |
290   +--------+--------+--------------------------------------------------------+
292 Following this are n variable length entries formatted as described in the
293 following table. Offsets are in words, but are given relative to the start of
294 an individual entry.
296   +--------+--------+--------------------------------------------------------+
297   | Offset | Length | Description                                            |
298   +--------+--------+--------------------------------------------------------+
299   | 0      | 1      | The type of the segment. Must be one of the following: |
300   |        |        |    0x00 - Reserved (Directory Segment)                 |
301   |        |        |    0x01 - Default Segment                              |
302   |        |        |    0x02 - Fixup Segment                                |
303   |        |        |    0x03 - Constant Table Segment                       |
304   |        |        |    0x04 - Bytecode Segment                             |
305   |        |        |    0x05 - Annotations Segment                          |
306   |        |        |    0x06 - PIC Data Segment                             |
307   |        |        |    0x07 - Dependencies Segment                         |
308   +--------+--------+--------------------------------------------------------+
309   | 1      | n      | The name of the segment, as a (NULL terminated) ASCII  |
310   |        |        | C string. This must be padded with trailing NULL       |
311   |        |        | (zero) values to be a full word in size.               |
312   +--------+--------+--------------------------------------------------------+
313   | n + 1  | 1      | The offset to the segment, relative to the start of    |
314   |        |        | the packfile. Specified as a number of words, where    |
315   |        |        | the word size is that specified in the header. (Parrot |
316   |        |        | may need to do some computation to transform this to   |
317   |        |        | an offset in terms of its own word size.) As segments  |
318   |        |        | must always be aligned on 16-byte boundaries, this     |
319   |        |        | scheme scales up to 128-bit platforms.                 |
320   +--------+--------+--------------------------------------------------------+
321   | n + 2  | 1      | The length of the segment, including its header, in    |
322   |        |        | words. This must match the length stored at the start  |
323   |        |        | of the header of the segment the entry is describing.  |
324   +--------+--------+--------------------------------------------------------+
327 =head3 Default Segment
329 The default segment has no additional headers. It will, if possible, be memory
330 mapped. More than one may exist in the packfile, and they are identified by
331 name. They may be used for storing any data that does not fit into any other
332 segment, for example the source code from a high level language.
335 =head3 Bytecode Segment
337 This segment has no additonal headers. It stores a stream of instructions in
338 bytecode format. Instructions have variable length. Each instruction starts
339 with an operation code.
341   +--------+--------+--------------------------------------------------------+
342   | Offset | Length | Description                                            |
343   +--------+--------+--------------------------------------------------------+
344   | 0      | 1      | A valid Parrot operation code, as specified in the     |
345   |        |        | operation codes list.                                  |
346   +--------+--------+--------------------------------------------------------+
348 Zero or more operands follow the operation code. Most instructions take a
349 fixed number of operands but several of them take a variable number, with the
350 first operand being used to determine the number of additional operands that
351 follow. This tends to be stored as a PMC constant, meaning that decoding the
352 instruction stream not only requires knowledge of the operands that each
353 instruction takes but also the ability to thaw PMCs.
355 An individual operand is always one word in length and may be of one of the
356 following forms.
358   +------------------+-------------------------------------------------------+
359   | Operand Type     | Description                                           |
360   +------------------+-------------------------------------------------------+
361   | Register         | An integer specifying a register number.              |
362   +------------------+-------------------------------------------------------+
363   | Integer Constant | An integer that is the constant itself. That is, the  |
364   |                  | constant is stored directly in the instruction        |
365   |                  | stream. Storing integer constants of length greater   |
366   |                  | than 32 bits has undefined behaviour and should be    |
367   |                  | considered unportable.                                |
368   +------------------+-------------------------------------------------------+
369   | Number Constant  | An index into the constants table.                    |
370   +------------------+-------------------------------------------------------+
371   | String Constant  | An index into the constants table.                    |
372   +------------------+-------------------------------------------------------+
373   | PMC Constant     | An index into the constants table.                    |
374   +------------------+-------------------------------------------------------+
377 =head3 Constants Segment
379 This segment stores number, string and PMC constants. It adds one extra field
380 to its header.
382   +--------+--------+--------------------------------------------------------+
383   | Offset | Length | Description                                            |
384   +--------+--------+--------------------------------------------------------+
385   | 2      | 1      | The number of constants in the table.                  |
386   |        |        |    n                                                   |
387   +--------+--------+--------------------------------------------------------+
389 Following this are n constants, each with a single word header specifying the
390 type of constant that follows.
392   +--------+--------+--------------------------------------------------------+
393   | Offset | Length | Description                                            |
394   +--------+--------+--------------------------------------------------------+
395   | 0      | 1      | The type of the constant. Must be one of:              |
396   |        |        |    0x00 - No constant                                  |
397   |        |        |    0x6E - Number constant (ASCII 'n')                  |
398   |        |        |    0x73 - String constant (ASCII 's')                  |
399   |        |        |    0x70 - PMC constant (ASCII 'p')                     |
400   |        |        |    0x6B - Key constant (ASCII 'k')                     |
401   +--------+--------+--------------------------------------------------------+
403 All constants that are not a multiple of the word size in length must be
404 padded with trailing zero bytes up to a word size boundary.
406 =head4 Number Constants
408 The number is stored in the format defined in the Packfile header. Any padding
409 that is needed will follow.
411 =head4 String Constants
413 String constants are stored in the following format, with offsets relative to
414 the start of the constant including its type.
416   +--------+--------+--------------------------------------------------------+
417   | Offset | Length | Description                                            |
418   +--------+--------+--------------------------------------------------------+
419   | 1      | 1      | Flags, copied from the string structure.               |
420   +--------+--------+--------------------------------------------------------+
421   | 2      | 1      | Character set; either the index of a built-in one or a |
422   |        |        | dynamically loaded one whose index is in a range given |
423   |        |        | in the dependencies table.                             |
424   +--------+--------+--------------------------------------------------------+
425   | 3      | 1      | Encoding, either the index of a built-in one or a      |
426   |        |        | dynamically loaded one whose index is in a range given |
427   |        |        | in the dependencies table.                             |
428   +--------+--------+--------------------------------------------------------+
429   | 4      | 1      | Length of the string data in bytes.                    |
430   +--------+--------+--------------------------------------------------------+
431   | 5      | n      | String data with trailing zero padding as required.    |
432   +--------+--------+--------------------------------------------------------+
434 =head4 PMC Constants
436 PMCs that can be saved in packfiles as constants implement the freeze and thaw
437 v-table methods. Their frozen data is placed in a string, stored in the same
438 format as a string constant.
440 =head4 Key Constants
442 Key constants are made up a number of components, where one component is a
443 "dimension" in the key. The number of components in the key is stored at the
444 start of the constant.
446   +--------+--------+--------------------------------------------------------+
447   | Offset | Length | Description                                            |
448   +--------+--------+--------------------------------------------------------+
449   | 1      | 1      | Number of key components that follow.                  |
450   |        |        |    n                                                   |
451   +--------+--------+--------------------------------------------------------+
453 Following this are n entries of two words each that specify the key's type and
454 value. The key value may be a register or another constant, but not another key
455 constant. All constants other than integer constants are indexes into the
456 constants table.
458   +--------+--------+--------------------------------------------------------+
459   | Offset | Length | Description                                            |
460   +--------+--------+--------------------------------------------------------+
461   | 0      | 1      | Type of the key. Must be one of:                       |
462   |        |        |    0x00 - Integer register                             |
463   |        |        |    0x01 - String register                              |
464   |        |        |    0x02 - PMC register                                 |
465   |        |        |    0x03 - Number register                              |
466   |        |        |    0x10 - Integer constant                             |
467   |        |        |    0x11 - String constant (constant table index)       |
468   |        |        |    0x12 - PMC constant (constant table index)          |
469   |        |        |    0x13 - Number constant (constant table index)       |
470   +--------+--------+--------------------------------------------------------+
471   | 1      | 1      | Value of the key.                                      |
472   +--------+--------+--------------------------------------------------------+
474 {{ TODO: Figure out slice bits and document them here. }}
477 =head3 Fixup Segment
479 The fixup segment maps names of subs to offsets in the bytecode stream. It
480 adds one extra field to its header.
482 {{ TODO: I think label fixups are no longer used. Check if that is so. }}
484   +--------+--------+--------------------------------------------------------+
485   | Offset | Length | Description                                            |
486   +--------+--------+--------------------------------------------------------+
487   | 1      | 1      | Number of fixup table entries that follow.             |
488   |        |        |    n                                                   |
489   +--------+--------+--------------------------------------------------------+
491 This is followed by n fixup table entries, of variable length, that take the
492 following form.
494   +--------+--------+--------------------------------------------------------+
495   | Offset | Length | Description                                            |
496   +--------+--------+--------------------------------------------------------+
497   | 0      | 1      | Type of the fixup. Must be:                            |
498   |        |        |    0x01 - Subroutine fixup                             |
499   +--------+--------+--------------------------------------------------------+
500   | 1      | 1      | The label that is being fixed up. A string constant,   |
501   |        |        | stored as an index into the constants table.           |
502   +--------+--------+--------------------------------------------------------+
503   | 2      | 1      | For subroutine fixups, this is an index into the       |
504   |        |        | constants table for the sub PMC corresponding to the   |
505   |        |        | label.                                                 |
506   +--------+--------+--------------------------------------------------------+
509 =head3 Annotations Segment
511 Annotations allow any instruction in the bytecode stream to have zero or more
512 key/value pairs associated with it. These can be retrived at runtime. High
513 level languages can use annotations to store file names, line numbers, column
514 numbers and any other data, for debug purposes or otherwise, that they need.
516 The segment comes in three parts:
518 =over 4
520 =item A list of annotation keys (for example, "line" and "file").
522 =item An annotation groups table, used to group together annotations for a
523 particular HLL source file (an annotation group starting clears all active
524 annotations, so they will not spill over between source files; it also
525 allows for faster lookup of annotations).
527 {{ TODO: Does it clear all annotations, or all annotation groups? }}
529 =item A list of indexes into the bytecode stream and key/value pairings (for
530 example, starting at instruction 235, the annotation "line" has value "42").
532 =back
534 The first word in the segment supplies the number of keys.
536   +--------+--------+--------------------------------------------------------+
537   | Offset | Length | Description                                            |
538   +--------+--------+--------------------------------------------------------+
539   | 1      | 1      | Number of annotation key entries that follow.          |
540   |        |        |    n                                                   |
541   +--------+--------+--------------------------------------------------------+
543 Following this are n annotation key entries. There is one entry per key (such
544 as "line" or "file"), but the bytecode may be annotated many times with that
545 key. Key entries take the following format.
547   +--------+--------+--------------------------------------------------------+
548   | Offset | Length | Description                                            |
549   +--------+--------+--------------------------------------------------------+
550   | 0      | 1      | Index into the constants table of a string containing  |
551   |        |        | the name of the key.                                   |
552   +--------+--------+--------------------------------------------------------+
553   | 1      | 1      | The type of value that is stored with the key.         |
554   |        |        |    0x00 - Integer                                      |
555   |        |        |    0x01 - String Constant                              |
556   |        |        |    0x02 - Number Constant                              |
557   |        |        |    0x03 - PMC Constant                                 |
558   +--------+--------+--------------------------------------------------------+
560 The annotation groups table comes next. This starts with a single integer to
561 specify the number of entries in the table.
563   +--------+--------+--------------------------------------------------------+
564   | Offset | Length | Description                                            |
565   +--------+--------+--------------------------------------------------------+
566   | 1      | 1      | Number of annotation group entries that follow.        |
567   |        |        |    n                                                   |
568   +--------+--------+--------------------------------------------------------+
570 A group entry maps an offset in the bytecode segment to an offset in the list
571 of annotations (that is, offset 0 refers to the first word following this
572 table). The list of offsets into the bytecode segment (and by the definition
573 of this segment, the offsets into the annotations list) must be in ascending
574 order.
576   +--------+--------+--------------------------------------------------------+
577   | Offset | Length | Description                                            |
578   +--------+--------+--------------------------------------------------------+
579   | 0      | 1      | Offset into the bytecode segment where the             |
580   |        |        | instructions for a particular high level source file   |
581   |        |        | start.                                                 |
582   +--------+--------+--------------------------------------------------------+
583   | 1      | 1      | Offset into the annotations list specifying where the  |
584   |        |        | annotations for the given instruction start.           |
585   +--------+--------+--------------------------------------------------------+
587 The rest of the segment is made up of a sequence of instructions to key and
588 value mappings, taking the following format.
590   +--------+--------+--------------------------------------------------------+
591   | Offset | Length | Description                                            |
592   +--------+--------+--------------------------------------------------------+
593   | 0      | 1      | Offset into the bytecode segment, in words, of the     |
594   |        |        | instruction being annotated. At runtime, this will     |
595   |        |        | correspond to the program counter.                     |
596   +--------+--------+--------------------------------------------------------+
597   | 1      | 1      | The key of the annotation, specified as an index into  |
598   |        |        | the zero-based list of keys specified in the first     |
599   |        |        | part of the segment. That is, if key "line" was the    |
600   |        |        | first entry and "file" the second, they would have     |
601   |        |        | indices 0 and 1 respectively.                          |
602   +--------+--------+--------------------------------------------------------+
603   | 2      | 2      | The value of the annotation. If the annotation type    |
604   |        |        | (specified with the key) is an integer, the value is   |
605   |        |        | placed directly into this word. Otherwise, an index    |
606   |        |        | into the constants table is used.                      |
607   +--------+--------+--------------------------------------------------------+
609 Note that the value of an annotation with a particular key is taken to apply
610 to all following instructions up to the point of a new value being specified
611 for that key with another annotation. This means that if 20 instructions make
612 up the compiled form of a single line of code, only one line annotation is
613 required. Note that this also implies that annotations must be placed in
614 the same order as the instructions.
617 =head3 Dependencies Segment
619 This segment holds a table of external and possibly dynamically loaded items
620 that are needed for this packfile to run. This includes:
622 =over 4
624 =item Dynamic PMC libraries (.loadlib)
626 =item Dynamic opcode libraries (.loadlib)
628 =item Dynamically loaded string encoding
630 =item Dynamically loaded character set
632 =back
634 The segment starts with the number of entries in the table.
636   +--------+--------+--------------------------------------------------------+
637   | Offset | Length | Description                                            |
638   +--------+--------+--------------------------------------------------------+
639   | 1      | 1      | Number of entries in the dependencies table.           |
640   |        |        |    n                                                   |
641   +--------+--------+--------------------------------------------------------+
643 Following this are n entries of variable length, taking the following format.
645   +--------+--------+--------------------------------------------------------+
646   | Offset | Length | Description                                            |
647   +--------+--------+--------------------------------------------------------+
648   | 0      | 1      | Number of entries in the dependencies table.           |
649   |        |        |    0x00 - Dynamic PMC Library                          |
650   |        |        |    0x01 - Dynamic Opcode Library                       |
651   |        |        |    0x02 - Dynamically Loaded String Encoding           |
652   |        |        |    0x03 - Dynamically Loaded Character Set             |
653   +--------+--------+--------------------------------------------------------+
654   | 1      | n      | A hint for finding and loading the resource; usually   |
655   |        |        | the name of the dynamic library, but possibly a full   |
656   |        |        | path too. Given as an ASCII NULL-terminated string,    |
657   |        |        | zero-padded to a full word.                            |
658   +--------+--------+--------------------------------------------------------+
659   | n + 1  | 1      | The lowest index for the given type of resource that   |
660   |        |        | is contained in this dependency. For example, if this  |
661   |        |        | entry was for a dynamic opcode library containing ops  |
662   |        |        | numbered 5000 through 5042, this entry would be 5000.  |
663   +--------+--------+--------------------------------------------------------+
664   | n + 2  | 1      | The highest index for the given type of resource that  |
665   |        |        | is contained in this dependency. For example, if this  |
666   |        |        | entry was for a dynamic opcode library containing ops  |
667   |        |        | numbered 5000 through 5042, this entry would be 5042.  |
668   +--------+--------+--------------------------------------------------------+
672 =head2 Packfile PMCs
674 A packfile will be represented in memory by Parrot as a tree of PMCs. These
675 will provide a programatic way to construct and walk packfiles, both for the
676 Parrot internals and from programs running on the Parrot VM.
678 {{ TODO... QUESTION: Will the CStruct PMC make it into Parrot? If so, we
679 may want to change the interface of these PMCs to take advantage of it. 
680 ANSWER: Yes it will (most likely with a different name), but it needs to
681 be prototyped first. Do you want to hold off on implementing the
682 bytecode changes until it's available? I say go ahead and implement now,
683 then revise when the new PMC is available. }}
686 =head3 Packfile.pmc
688 This PMC represents the packfile overall. It will be constructed by the VM
689 when reading a packfile. It implements the following methods.
691 =head4 get_string (v-table)
693 Serializes this packfile data structure into a bytestream ready to be written
694 to disk (that is, maps from PMCs to on-disk representation).
696 =head4 set_string_native (v-table)
698 Takes a string containing an entire packfile in the on-disk format, attempts
699 to unpack it into a tree of Packfile PMCs and sets this Packfile PMC to 
700 represent the top of that tree (that is, maps from on-disk representation to a
701 tree of PMCs).
703 =head4 get_integer_keyed_str (v-table)
705 Used to get data about fields in the header that have an integer value. Valid
706 keys are:
708 =over 4
710 =item wordsize
712 =item byteorder
714 =item fptype
716 =item version_major
718 =item version_minor
720 =item version_patch
722 =item bytecode_major
724 =item bytecode_minor
726 =item uuid_type
728 =item uuid_length
730 =back
732 =head4 get_string_keyed_str (v-table)
734 Used to get data about fields in the header that have a string value. Valid
735 keys are:
737 =over 4
739 =item uuid
741 =back
743 =head4 set_integer_keyed_str (v-table)
745 Used to set fields in the packfile header. Some fields are not allowed to be
746 written since they are determined by the VM when serializing the packfile for
747 storage on disk. The fields that may be set are:
749 =over 4
751 =item version_major
753 =item version_minor
755 =item version_patch
757 =item uuid_type
759 =back
761 Be very careful when setting a version number; you should usually trust the VM
762 to do the right thing with this.
764 Setting the uuid_type will not result in immediate re-computation of the UUID,
765 but rather will only cause it to be computed using the selected algorithm when
766 the packfile is serialized (by calling the get_string v-table method). Setting
767 an invalid uuid_type value will cause an exception to be thrown immediately.
769 =head4 get_directory()
771 Returns the PackfileDirectory PMC that represents the directory segment at the
772 start of the packfile.
775 =head3 PackfileSegment.pmc
777 An abstract PMC that is the base class for all other segments. It has two
778 abstract methods, which are to be implemented by all subclasses. They will not
779 be listed under the method list for other segment PMCs to save space.
781 =head4 STRING* pack()
783 Packs the segment into the on-disk format and returns a string holding it.
785 =head4 unpack(STRING*)
787 Takes the packed representation for a segment of the given type and then
788 unpacks it, setting this PMC to represent that segment as a result of the
789 unpacking. If an error occurs during the unpacking process, an exception will
790 be thrown.
793 =head3 PackfileDirectory.pmc (isa PackfileSegment)
795 This PMC represents a directory segment. Essentially it is an array of
796 PackfileSegment PMCs. When indexed using an integer key, it gets the segment
797 at that positiion in the segments table. When indexed using a string key, it
798 looks for a segment of that name. It implements the following methods.
800 =head4 elements (v-table)
802 Gets the number of segments listed in the directory.
804 =head4 get_pmc_keyed_int (v-table)
806 Gets a PackfileSegment PMC or an appropriate subclass of it representing the
807 segment at the specified index in the directory segment.
809 =head4 get_string_keyed_int (v-table)
811 Gets a string containing the name of the segment at the specified index in the
812 directory segment.
814 =head4 get_pmc_keyed_str (v-table)
816 Searches the directory for a segment with the given name and, if one exists,
817 returns a PackfileSegment PMC (or one of its subclasses) representing it.
819 =head4 set_pmc_keyed_str (v-table)
821 Adds a PackfileSegment PMC (or a subclass of it) to the directory with the
822 name specified by the key. This is the only way to add another segment to the
823 directory. If a segment of the given name already exists in the directory, it
824 will be replaced with the supplied PMC.
827 =head3 RawSegment.pmc (isa PackfileSegment)
829 This PMC presents a segment of a packfile as an array of integers. This is the
830 lowest possible level of access to a segment, and covers both the default and
831 bytecode segment types. It implements the following methods.
833 =head4 get_integer_keyed_int (v-table)
835 Reads the integer at the specified offset into the segment, excluding the data
836 in the common segment header but including the data making up additional
837 fields in the header for a specific type of segment.
839 =head4 set_integer_keyed_int (v-table)
841 Stores an integer at the specified offset into the segment. Will throw an
842 exception if the segment is memory mapped.
844 =head4 elements (v-table)
846 Gets the length of the segment in words, excluding the length of the common
847 segment but including the data making up additional fields in the header for a
848 specific type of segment.
851 =head3 PackfileConstantTable.pmc (isa PackfileSegment)
853 This PMC represents a constants table. It provides access to constants through
854 the keyed integer interface (the interpreter may choose to access underlying
855 structures directly to improve performance, however). 
857 The table of constants can be added to using the keyed set methods; it will
858 grow automatically.
860 The PMC implements the following methods.
862 =head4 elements (v-table)
864 Gets the number of constants contained in the table.
866 =head4 get_number_keyed_int (v-table)
868 Gets the value of the number constant at the specified index in the constants
869 table. If the constant at that position in the table is not a number, an
870 exception will be thrown.
872 =head4 get_string_keyed_int (v-table)
874 Gets the value of the string constant at the specified index in the constants
875 table. If the constant at that position in the table is not a string, an
876 exception will be thrown.
878 =head4 get_pmc_keyed_int (v-table)
880 Gets the value of the PMC or key constant at the specified index in the
881 constants table. If the constant at that position in the table is not a PMC
882 or key, an exception will be thrown.
884 =head4 set_number_keyed_int (v-table)
886 Sets the value of the number constant at the specified index in the constants
887 table. If the constant at that position in the table is not already a number
888 constant, an exception will be thrown. If it does not exist, the table will be
889 extended.
891 =head4 set_string_keyed_int (v-table)
893 Sets the value of the string constant at the specified index in the constants
894 table. If the constant at that position in the table is not already a string
895 constant, an exception will be thrown. If it does not exist, the table will be
896 extended.
898 =head4 set_pmc_keyed_int (v-table)
900 Sets the value of the PMC or key constant at the specified index in the
901 constants table. If the constant at that position in the table is not already
902 a PMC or key constant, an exception will be thrown. If it does not exist, the
903 table will be extended.
905 =head4 int get_type(int)
907 Returns an integer value denoting the type of the constant at the specified
908 index. Possible values are:
910   +--------+-----------------------------------------------------------------+
911   | Value  | Constant Type                                                   |
912   +--------+-----------------------------------------------------------------+
913   | 0x00   | No Constant                                                     |
914   +--------+-----------------------------------------------------------------+
915   | 0x6E   | Number Constant                                                 |
916   +--------+-----------------------------------------------------------------+
917   | 0x73   | String Constant                                                 |
918   +--------+-----------------------------------------------------------------+
919   | 0x70   | PMC Constant                                                    |
920   +--------+-----------------------------------------------------------------+
921   | 0x6B   | Key Constant                                                    |
922   +--------+-----------------------------------------------------------------+
925 =head3 PackfileFixupTable.pmc (isa PackfileSegment)
927 This PMC provides a keyed integer interface to the fixup table. Each entry in
928 the table is represented by a PackfileFixupEntry PMC. It implements the
929 following methods.
931 =head4 elements (v-table)
933 Gets the number of entries in the fixup table.
935 =head4 get_pmc_keyed_int (v-table)
937 Gets a PackfileFixupEntry PMC for the fixup entry at the position given in
938 the key. If the index is out of range, an exception will be thrown.
940 =head4 set_pmc_keyed_int (v-table)
942 Used to add a PackfileFixupEntry PMC to the fixups table or to replace an
943 existing one. If the PMC that is supplied is not of type PackfileFixupEntry,
944 an exception will thrown.
947 =head3 PackfileFixupEntry.pmc
949 This PMC represents an entry in the fixup table. It implements the following
950 methods.
952 =head4 get_string (v-table)
954 Gets the label field of the fixup entry.
956 =head4 set_string_native (v-table)
958 Sets the label field of the fixup entry.
960 =head4 get_integer (v-table)
962 Gets the offset field of the fixup entry.
964 =head4 set_integer_native (v-table)
966 Sets the offset field of the fixup entry.
968 =head4 int get_type()
970 Gets the type of the fixup entry. See the entries table for possible fixup
971 types.
973 =head4 set_type(int)
975 Sets the type of the fixup entry. See the entries table for possible fixup
976 types. Specifying an invalid type will result in an exception.
979 =head3 PackfileAnnotations.pmc (isa PackfileSegment)
981 This PMC represents the bytecode annotations table. The key ID to key name and
982 key type mappings are stored in a separate PackfileAnnotationKeys PMC. Each 
983 (offset, key, value) entry is represented by a PackfileAnnotation PMC. The
984 following methods are implemented.
986 =head4 PMC* get_key_list()
988 Returns a PackfileAnnotationKeys PMC containing the names and types of the
989 annotation keys. Fetch and add to this to create a new annotation key.
991 =head4 elements (v-table)
993 Gets the number of annotations in the table.
995 =head4 get_pmc_keyed_int (v-table)
997 Gets the annotation at the specified index. If there is no annotation at that
998 index, an exception will be thrown. The PMC that is returned will always be a
999 PackfileAnnotation PMC.
1001 =head4 set_pmc_keyed_int (v-table)
1003 Sets the annotation at the specified index. If there is no annotation at that
1004 index, it is added to the list of annotations. An exception will be thrown
1005 unless all of the following conditions are met:
1007 =over 4
1009 =item The type of the PMC passed is PackfileAnnotation
1011 =item The entry at the previous index is defined
1013 =item The offset of the previous entry is less than this entry
1015 =item The offset of the next entry, if it exists, is greater than this entry
1017 =item The key ID references a valid annotation key
1019 =back
1022 =head3 PackfileAnnotationKeys.pmc
1024 This PMC represents the table of keys and the type of value that is stored
1025 against that key. It implements the following methods.
1027 =head4 get_string_keyed_int (v-table)
1029 Gets the name of the annotation key specified by the index. An exception will
1030 be thrown if the index is out of range.
1032 =head4 set_string_keyed_int (v-table)
1034 Sets the name of the annotation key specified by the index. If there is no key
1035 with that index currently, a key at that position in the table will be added.
1037 =head4 get_integer_keyed_int (v-table)
1039 Gets an integer representing the type of the value that is stored with the key
1040 at the specified index. An exception will be thrown if the index is out of
1041 range.
1043 =head4 set_integer_keyed_int (v-table)
1045 Sets the type of the value this is stored with the key at the specified index.
1046 If there is no key with that index currently, a key at that position in the
1047 table will be added.
1050 =head3 PackfileAnnotation.pmc
1052 This PMC represents an individual bytecode annotation entry in the annotations
1053 segment. It implements the following methods.
1055 =head4 int get_offset()
1057 Gets the offset into the bytecode of the instruction that is being annotated.
1059 =head4 set_offset(int)
1061 Sets the offset into the bytecode of the instruction that is being annotated.
1063 =head4 int get_key_id()
1065 Gets the ID of the key of the annotation.
1067 =head4 int set_key_id()
1069 Sets the ID of the key of the annotation.
1071 =head4 get_integer (v-table)
1073 Gets the value of the annotation. This may be, depending upon the type of the
1074 annotation, an integer annotation or an index into the constants table.
1076 =head4 set_integer (v-table)
1078 Sets the value of the annotation. This may be, depending upon the type of the
1079 annotation, an integer annotation or an index into the constants table.
1082 =head1 LANGUAGE NOTES
1084 None.
1087 =head1 ATTACHMENTS
1089 None.
1092 =head1 FOOTNOTES
1094 None.
1097 =head1 REFERENCES
1099 None.
1102 =cut
1104 __END__
1105 Local Variables:
1106   fill-column:78
1107 End: