* include/parrot/packfile.h:
[parrot.git] / include / parrot / packfile.h
blob111d1bbd1d0dac0eef359c81991cb1cb0aefc8f1
1 /* packfile.h
3 * $Id$
5 * History:
6 * Rework by Melvin; new bytecode format, make bytecode portable.
7 * (Do endian conversion and wordsize transforms on the fly.)
8 */
10 #if !defined(PARROT_PACKFILE_H_GUARD)
11 #define PARROT_PACKFILE_H_GUARD
13 #include <parrot/parrot.h>
15 #define PF_NCONST(pf) ((pf)->const_table->const_count)
16 #define PF_CONST(pf,i) ((pf)->const_table->constants[(i)])
18 #define DIRECTORY_SEGMENT_NAME "DIRECTORY"
19 #define FIXUP_TABLE_SEGMENT_NAME "FIXUP"
20 #define CONSTANT_SEGMENT_NAME "CONSTANT"
21 #define BYTE_CODE_SEGMENT_NAME "BYTECODE"
24 ** Structure Definitions:
29 ** Bytes that we don't have to reorder
30 * PACKFILE_HEADER_BYTES must be an integer times sizeof (opcode_t).
32 #define PACKFILE_HEADER_BYTES 16
34 struct PackFile_Header {
35 unsigned char wordsize;
36 unsigned char byteorder;
37 unsigned char major;
38 unsigned char minor;
39 unsigned char intvalsize; /* was flags */
40 unsigned char floattype;
41 unsigned char pad[10]; /* fingerprint */
42 /* Start words/opcodes on 16-byte boundary */
43 opcode_t magic;
44 opcode_t opcodetype;
45 opcode_t dir_format; /* was fixup_ss */
46 opcode_t _unused_padding;
50 ** PackFile Segment:
51 * The base type of every section
52 * include it as first element of every derivated Segment
55 struct PackFile_Segment;
57 typedef struct PackFile_Segment * (*PackFile_Segment_new_func_t)
58 (Interp *, struct PackFile *, const char *, int add);
59 typedef void (*PackFile_Segment_destroy_func_t) (Interp *,
60 struct PackFile_Segment *);
61 typedef size_t (*PackFile_Segment_packed_size_func_t)(Interp *,
62 struct PackFile_Segment *);
63 typedef opcode_t * (*PackFile_Segment_pack_func_t) (Interp *,
64 struct PackFile_Segment *, opcode_t *dest);
65 typedef opcode_t * (*PackFile_Segment_unpack_func_t) (Interp *,
66 struct PackFile_Segment *, opcode_t *packed);
67 typedef void (*PackFile_Segment_dump_func_t) (Interp *,
68 struct PackFile_Segment *);
70 struct PackFile_funcs {
71 PackFile_Segment_new_func_t new_seg;
72 PackFile_Segment_destroy_func_t destroy;
73 PackFile_Segment_packed_size_func_t packed_size;
74 PackFile_Segment_pack_func_t pack;
75 PackFile_Segment_unpack_func_t unpack;
76 PackFile_Segment_dump_func_t dump;
79 PARROT_API INTVAL PackFile_funcs_register(Interp*, struct PackFile *, UINTVAL type,
80 struct PackFile_funcs);
82 typedef enum {
83 PF_DIR_SEG,
84 PF_UNKNOWN_SEG,
85 PF_FIXUP_SEG,
86 PF_CONST_SEG,
87 PF_BYTEC_SEG,
88 PF_DEBUG_SEG,
90 PF_MAX_SEG
91 } pack_file_types;
93 #define PF_DIR_FORMAT 1
95 typedef INTVAL (*PackFile_map_segments_func_t) (Interp *,
96 struct PackFile_Segment *seg, void *user_data);
98 struct PackFile_Segment {
99 struct PackFile * pf;
100 struct PackFile_Directory * dir;
101 /* directory information */
102 UINTVAL type; /* one of above defined types */
103 char * name;
104 size_t op_count; /* external size in ops */
105 size_t file_offset; /* offset in ops */
106 /* common payload of all bytecode chunks
107 * with the size above these four items are aligned to 16 byte */
108 opcode_t itype; /* internal type/version */
109 opcode_t id; /* internal id */
110 size_t size; /* internal oparray size */
111 opcode_t * data; /* oparray e.g. bytecode */
115 ** PackFile_FixupTable:
117 struct PackFile_FixupEntry {
118 opcode_t type; /* who knows, what fixups we need */
119 char *name; /* name of the label */
120 opcode_t offset; /* location of the item */
121 struct PackFile_ByteCode *seg;
125 typedef enum {
126 enum_fixup_none,
127 enum_fixup_label,
128 enum_fixup_sub
129 } enum_fixup_t;
131 struct PackFile_FixupTable {
132 struct PackFile_Segment base;
133 opcode_t fixup_count;
134 struct PackFile_FixupEntry ** fixups;
135 struct PackFile_ByteCode * code; /* where this segment belongs to */
138 #define PFC_NONE '\0'
139 /* no ascii chars use numbers: for n,s,k,p */
140 #define PFC_NUMBER '\156'
141 #define PFC_STRING '\163'
142 #define PFC_KEY '\153'
143 #define PFC_PMC '\160'
145 enum PF_VARTYPE { /* s. also imcc/symreg.h */
146 PF_VT_START_SLICE = 1 << 10, /* x .. y slice range */
147 PF_VT_END_SLICE = 1 << 11,
148 PF_VT_START_ZERO = 1 << 12, /* .. y 0..start */
149 PF_VT_END_INF = 1 << 13, /* x.. start..inf */
150 PF_VT_SLICE_BITS = PF_VT_START_SLICE | PF_VT_END_SLICE |
151 PF_VT_START_ZERO | PF_VT_END_INF
154 struct PackFile_Constant {
155 opcode_t type;
156 union {
157 opcode_t integer;
158 FLOATVAL number;
159 STRING *string;
160 PMC *key;
161 } u;
164 struct PackFile_ConstTable {
165 struct PackFile_Segment base;
166 opcode_t const_count;
167 struct PackFile_Constant ** constants;
168 struct PackFile_ByteCode * code; /* where this segment belongs to */
171 struct PackFile_ByteCode {
172 struct PackFile_Segment base;
173 Prederef prederef; /* The predereferenced code and info */
174 void *jit_info; /* JITs data */
175 Parrot_PIC_store * pic_store; /* PIC storage */
176 struct PackFile_Segment * pic_index; /* segment of indices into store */
177 struct PackFile_Debug * debugs;
178 struct PackFile_ConstTable *const_table;
179 struct PackFile_FixupTable *fixups;
182 enum PF_DEBUGMAPPINGTYPE {
183 PF_DEBUGMAPPINGTYPE_NONE = 0,
184 PF_DEBUGMAPPINGTYPE_FILENAME,
185 PF_DEBUGMAPPINGTYPE_SOURCESEG
188 struct PackFile_DebugMapping {
189 opcode_t offset;
190 opcode_t mapping_type;
191 union {
192 opcode_t filename;
193 opcode_t source_seg; /* XXX Source segments currently unimplemented. */
194 } u;
197 struct PackFile_Debug {
198 struct PackFile_Segment base;
199 opcode_t num_mappings;
200 struct PackFile_DebugMapping ** mappings;
201 struct PackFile_ByteCode * code; /* where this segment belongs to */
204 struct PackFile_Directory {
205 struct PackFile_Segment base;
206 size_t num_segments;
207 struct PackFile_Segment ** segments;
210 struct PackFile {
211 /* the packfile is its own directory */
212 struct PackFile_Directory directory;
213 struct PackFile_Directory *dirp; /* for freeing */
214 opcode_t *src; /* the possible mmap()ed start of the PF */
215 size_t size; /* size in bytes */
216 INTVAL is_mmap_ped; /* don't free it, munmap it at destroy */
218 struct PackFile_Header * header;
220 /* directory hold all Segments */
221 /* TODO make this reallocatable */
222 struct PackFile_funcs PackFuncs[PF_MAX_SEG];
224 struct PackFile_ByteCode * cur_cs; /* used during PF loading */
226 INTVAL need_wordsize;
227 INTVAL need_endianize;
228 opcode_t (*fetch_op)(unsigned char *);
229 INTVAL (*fetch_iv)(unsigned char *);
230 void (*fetch_nv)(unsigned char *, unsigned char *);
235 ** PackFile Functions:
238 PARROT_API struct PackFile *PackFile_new(Interp *, INTVAL is_mapped);
239 PARROT_API struct PackFile *PackFile_new_dummy(Interp *, const char* name);
241 PARROT_API void PackFile_destroy(Interp *, struct PackFile * self);
243 PARROT_API opcode_t PackFile_pack_size(Interp *, struct PackFile *self);
245 PARROT_API void PackFile_pack(Interp *, struct PackFile * self, opcode_t * packed);
247 PARROT_API opcode_t PackFile_unpack(Interp *interp,
248 struct PackFile *self, opcode_t *packed,
249 size_t packed_size);
251 typedef enum {
252 PBC_MAIN = 1,
253 PBC_LOADED = 2,
254 PBC_PBC = 4,
255 PBC_IMMEDIATE = 8,
256 PBC_POSTCOMP = 16,
257 PBC_INIT = 32
258 } pbc_action_enum_t;
260 PARROT_API void PackFile_fixup_subs(Interp *, pbc_action_enum_t, PMC *eval_pmc);
261 void do_sub_pragmas(Interp *, struct PackFile_ByteCode *, int, PMC *eval_pmc);
263 * directory functions
266 PARROT_API INTVAL PackFile_add_segment(Interp *, struct PackFile_Directory *,
267 struct PackFile_Segment *);
269 PARROT_API struct PackFile_Segment * PackFile_find_segment(Interp *,
270 struct PackFile_Directory *, const char *name, int recurse);
272 PARROT_API struct PackFile_Segment *
273 PackFile_remove_segment_by_name(Interp *, struct PackFile_Directory *, const char *);
275 PARROT_API INTVAL PackFile_map_segments(Interp *, struct PackFile_Directory *dir,
276 PackFile_map_segments_func_t callback,
277 void* usr_data);
279 PARROT_API struct PackFile_Segment * PackFile_Segment_new_seg(Interp *,
280 struct PackFile_Directory *, UINTVAL type, const char *name, int add);
282 PARROT_API struct PackFile_ByteCode * PF_create_default_segs(Interp*,
283 const char *file_name, int add);
285 PARROT_API void Parrot_load_bytecode(Interp *, STRING *filename);
287 ** PackFile_Segment Functions:
290 PARROT_API void PackFile_Segment_destroy(Interp *, struct PackFile_Segment * self);
291 PARROT_API size_t PackFile_Segment_packed_size(Interp *, struct PackFile_Segment * self);
292 PARROT_API opcode_t * PackFile_Segment_pack(Interp *, struct PackFile_Segment *, opcode_t *);
293 PARROT_API opcode_t * PackFile_Segment_unpack(Interp *interp,
294 struct PackFile_Segment * self, opcode_t *cursor);
295 PARROT_API void PackFile_Segment_dump(Interp *, struct PackFile_Segment *);
296 void default_dump_header(Interp *, struct PackFile_Segment *);
298 PARROT_API struct PackFile_Segment *PackFile_Segment_new(Interp *, struct PackFile *pf, const char*,
299 int);
301 /* fingerprint functions */
302 PARROT_API int PackFile_check_fingerprint(void *cursor);
303 PARROT_API size_t PackFile_write_fingerprint(void *cursor);
306 ** PackFile_FixupTable Functions:
309 PARROT_API void PackFile_FixupTable_clear(Interp *,struct PackFile_FixupTable * self);
311 PARROT_API INTVAL PackFile_FixupTable_unpack(Interp *,
312 struct PackFile_FixupTable * self, opcode_t * , opcode_t );
314 PARROT_API opcode_t PackFile_FixupTable_pack_size(Interp
315 *,struct PackFile_FixupTable * self);
317 PARROT_API void PackFile_FixupTable_pack(Interp *, struct PackFile_FixupTable * self,
318 opcode_t * packed);
320 PARROT_API void PackFile_Fixup_dump(Interp *,
321 struct PackFile_FixupTable *ft);
323 /* create new fixup entry */
324 PARROT_API void PackFile_FixupTable_new_entry(Interp *, char *label,
325 enum_fixup_t, opcode_t offs);
326 /* find entry */
327 PARROT_API struct PackFile_FixupEntry * PackFile_find_fixup_entry(Interp *,
328 enum_fixup_t type, char *);
331 ** PackFile_ByteCode Functions:
334 PARROT_API struct PackFile_ByteCode * Parrot_switch_to_cs(Interp *,
335 struct PackFile_ByteCode *, int really);
336 PARROT_API void Parrot_switch_to_cs_by_nr(Interp *, opcode_t seg);
337 PARROT_API void Parrot_destroy_constants(Interp *);
340 ** PackFile_Debug Functions:
342 PARROT_API struct PackFile_Debug * Parrot_new_debug_seg(Interp *,
343 struct PackFile_ByteCode *cs, size_t size);
344 PARROT_API STRING * Parrot_debug_pc_to_filename(Interp *interp,
345 struct PackFile_Debug *debug, opcode_t pc);
346 PARROT_API void Parrot_debug_add_mapping(Interp *interp,
347 struct PackFile_Debug *debug,
348 opcode_t offset, int mapping_type,
349 const char *filename, int source_seg);
352 ** PackFile_ConstTable Functions:
355 void mark_const_subs(Interp *interp);
356 PARROT_API void PackFile_ConstTable_clear(Interp *, struct PackFile_ConstTable * self);
358 PARROT_API void PackFile_ConstTable_dump(Interp *,
359 struct PackFile_ConstTable *);
360 PARROT_API size_t PackFile_ConstTable_pack_size(Interp *, struct PackFile_Segment * self);
362 PARROT_API opcode_t * PackFile_ConstTable_pack(Interp *, struct PackFile_Segment *, opcode_t *);
364 PARROT_API opcode_t * PackFile_ConstTable_unpack(Interp *interp,
365 struct PackFile_Segment * self,
366 opcode_t * packed);
369 ** PackFile_Constant Functions:
372 PARROT_API struct PackFile_Constant *PackFile_Constant_new(Interp *);
374 PARROT_API size_t PackFile_Constant_pack_size(Interp *, struct PackFile_Constant * self);
376 PARROT_API opcode_t * PackFile_Constant_pack(Interp *, struct PackFile_ConstTable *ct, struct PackFile_Constant *, opcode_t *);
378 PARROT_API void PackFile_Constant_destroy(Interp *, struct PackFile_Constant * self);
380 PARROT_API opcode_t * PackFile_Constant_unpack(Interp *interp,
381 struct PackFile_ConstTable *, struct PackFile_Constant *, opcode_t *);
383 PARROT_API opcode_t * PackFile_Constant_unpack_key(Interp *interp,
384 struct PackFile_ConstTable *, struct PackFile_Constant *, opcode_t *);
386 PARROT_API opcode_t * PackFile_Constant_unpack_pmc(Interp *interp,
387 struct PackFile_ConstTable *, struct PackFile_Constant *, opcode_t *);
389 PARROT_API int PackFile_find_in_const(Interp *interpreter, struct PackFile_ConstTable *ct,
390 PMC *key, int type);
393 * pf_items low level Parrot items fetch routines
395 opcode_t PF_fetch_opcode(struct PackFile *pf, opcode_t **stream);
396 INTVAL PF_fetch_integer(struct PackFile *pf, opcode_t **stream);
397 FLOATVAL PF_fetch_number(struct PackFile *pf, opcode_t **stream);
398 STRING* PF_fetch_string(Interp*, struct PackFile *pf, opcode_t **stream);
399 char * PF_fetch_cstring(struct PackFile *pf, opcode_t **stream);
401 size_t PF_size_opcode(void);
402 size_t PF_size_integer(void);
403 size_t PF_size_number(void);
404 size_t PF_size_string(STRING *);
405 size_t PF_size_cstring(const char *);
407 opcode_t* PF_store_opcode(opcode_t *, opcode_t);
408 opcode_t* PF_store_integer(opcode_t *, INTVAL);
409 opcode_t* PF_store_number(opcode_t *, FLOATVAL *);
410 opcode_t* PF_store_string(opcode_t *, STRING *);
411 opcode_t* PF_store_cstring(opcode_t *, const char *);
413 void PackFile_assign_transforms(struct PackFile *pf);
416 ** Byte Ordering Functions (byteorder.c)
419 INTVAL fetch_iv_le(INTVAL w);
420 INTVAL fetch_iv_be(INTVAL w);
421 opcode_t fetch_op_be(opcode_t w);
422 opcode_t fetch_op_le(opcode_t w);
423 void fetch_buf_be_4(unsigned char * rb, unsigned char * b);
424 void fetch_buf_le_4(unsigned char * rb, unsigned char * b);
425 void fetch_buf_be_8(unsigned char * rb, unsigned char * b);
426 void fetch_buf_le_8(unsigned char * rb, unsigned char * b);
427 void fetch_buf_le_12(unsigned char * rb, unsigned char * b);
428 void fetch_buf_be_12(unsigned char * rb, unsigned char * b);
429 void fetch_buf_le_16(unsigned char * rb, unsigned char * b);
430 void fetch_buf_be_16(unsigned char * rb, unsigned char * b);
434 #endif /* PARROT_PACKFILE_H_GUARD */
437 * Local variables:
438 * c-file-style: "parrot"
439 * End:
440 * vim: expandtab shiftwidth=4: