1 #ifndef EXEC_H__
2 #define EXEC_H__ 1
3 
4 /*---------------------------------------------------------------------------
5  * Types and Macros used to describe and store compiled programs.
6  *
7  *---------------------------------------------------------------------------
8  * Basics
9  * ------
10  *
11  * LPMud distinguished between objects and their programs. Objects hold
12  * the live data which is unique to each of them. Programs otoh are shared
13  * among objects through inheritance or cloning and hold the program code,
14  * variable specification and similar information. Due to the separation
15  * programs maintain their own reference count, and it is this separation
16  * which allows the separate swapping of programs and variables.
17  *
18  * A compiled program consists of several data blocks which are allocated
19  * together in one memory block. Not only this is advantageous for the
20  * memory locality, but also simplifies swapping as all data for a program
21  * can be read or written in one go, with a simple relocation computation
22  * on the pointers involved.
23  *
24  * The blocks of a program in correct allocation order are these:
25  *
26  *   struct program_s: The basic structure of the program, with pointers to
27  *       the other data blocks.
28  *
29  *   bytecode program[]: the actual bytecode for the program.
30  *
31  *   unsigned short function_names[]: Lookup table of function name index
32  *       to the offset of the function within the functions[] table.
33  *       function_names[] is allocated together with and right after the
34  *       bytecode. If a function redefines an inherited function, this table
35  *       points to the redefinition.
36  *       Read 'function_name' as 'function_index'.
37  *
38  *   uint32 functions[]: One word for each function, giving the type
39  *       and the offset within the program[] codearray. Additional information
40  *       for each function is embedded into the program code.
41  *       If a program uses undefined functions, the compiler generates
42  *       dummy functions.
43  *       Through inheritance and cross-definition these functions can
44  *       be resolved on a higher level.
45  *
46  *   string_t *strings[]: An array of pointers to the string literals (stored
47  *       as shared strings) used by the program. This way the program can
48  *       use the strings simply by an (easily swappable) index. The compiler
49  *       makes sure that each string is unique.
50  *
51  *       When a program is swapped, the reference counts to these strings are
52  *       not removed so that the string literals stay in memory all the time.
53  *       This saves time on swap-in, and the string sharing saves more memory
54  *       than swapping might gain.
55  * TODO: Does it?
56  *
57  *   struct variable_s variables[]: an array describing all variables
58  *       with type and name, inherited or own. When a program is swapped, the
59  *       reference counts to these strings are not removed so that the
60  *       string literals stay in memory all the time.
61  *
62  *   struct inherit inherit[]: an array describing all inherited programs.
63  *
64  *   vartype_t argument_types[]: (only with #pragma save_types)
65  *       The types of all function arguments of the program in the
66  *       order they were encountered.
67  *
68  *   unsigned short type_start[]: (only with #pragma save_types)
69  *       Lookup table [.num_function_names] function index
70  *       -> index in .argument_types[], which gives the index of
71  *       the first argument type. If this index is INDEX_START_NONE,
72  *       the function has no type information.
73  *
74  *   include_t includes[]: an array listing all include files used
75  *       to compile the program, in the order they were encountered.
76  *       When a program is swapped, the reference counts to these strings
77  *       are not removed so that the string literals stay in memory all
78  *       the time.
79  *
80  * The linenumber information is allocated separately from the main program
81  * block so that it can be swapped separately more easily. The struct
82  * linenumbers_s is allocated to size and holds the line number information
83  * as an array of bytecodes:
84  *
85  *   bytecode_t line_numbers[]: the line number information,
86  *       encoded in a kind of delta compression. When a program
87  *       is swapped in, the line numbers are allocated separately
88  *       and not swapped in until needed.
89  *
90  * TODO: If the program_s is allocated separately from the rest,
91  * TODO:: we could swap even if a program is used by clones.
92  *
93  *
94  * Bytecode
95  * --------
96  *
97  * The bytecode is described and defined in bytecode.h .
98  *
99  *
100  * Inheritance
101  * -----------
102  *
103  * Inheritance is handled such that all the variable and function
104  * information of the inherited programs are appended to the programs
105  * own variables and functions. The inherit entries then give away
106  * where in the programs tables the inherited information can be found.
107  *
108  * Imagine: A and B inherit nothing, C inherits A, D inherits C and B.
109  * Then the function and variable blocks are set up like this ('-funs' are
110  * real function entries, '-desc' are pointers to inherit descriptors):
111  *
112  *     A-fblock: A-funs  (B similar)
113  *     A-vblock: A-vars  (B similar)
114  *
115  *     C-fblock: C-funs A-desc
116  *     C-vblock: C-vars A-vars
117  *
118  *     D-fblock: D-funs (C-desc A-desc) B-desc
119  *     D-vblock: D-vars  C-vars A-vars  B-vars
120  *       and fblock has the twist that (C-desc A-desc) together are
121  *       considered being 'the' C-desc block.
122  *
123  * If a program is inherited virtually several times, each virtual inherit
124  * gets its own struct inherit; the variable block however is reserved
125  * just once. To mark the virtual inherit, the variable types receive the
126  * modifier TYPE_MOD_VIRTUAL. During the inheritance of the compiler, functions
127  * from non-virtual inherits temporarily receive the flag
128  * NON_VIRTUAL_OFFSET_TAG in their variable indices, too.
129  *
130  * However, accesses to virtually inherited variables only use the first
131  * inherit instance - if the variable index in the child's program code indexes
132  * the variable block associated with a later instance, the driver finds
133  * the first instance by comparing the .prog entries and uses the 'real'
134  * variable associated with this first instance.
135  *
136  * The compiler places virtual variables always at the begin of the variable
137  * block and limits the number to 256.
138  *
139  * TODO: Find a way to avoid the virtual var searching - either by encoding the
140  * TODO:: inherit index in the code, or by adding a ref to the 'first
141  * TODO:: instance' to this structure. See interpret.c:find_virtual_value().
142  *
143  * Old Comments:
144  * -------------
145  * When an object is compiled with type testing (#pragma strict_types), all
146  * types are saved of the arguments for that function during compilation.
147  * If the #pragma save_types is specified, then the types are saved even
148  * after compilation, to be used when the object is inherited.
149  *---------------------------------------------------------------------------
150  */
151 
152 #include "driver.h"
153 #include "typedefs.h"
154 
155 #include "bytecode.h"
156 
157 
158 typedef unsigned short    typeid_t;
159 typedef uint32            typeflags_t;
160 typedef struct vartype_s  vartype_t;
161 typedef struct fulltype_s fulltype_t;
162 
163 
164 /* --- Type Constants ---
165  *
166  * These constants and types are used to encode types and visibility
167  * of variables and functions. They are used by both the compiler and
168  * the interpreter.
169  *
170  * Variable/value data types consist of three pieces:
171  *  - the primary type constant, optionally modified as
172  *      TYPE_MOD_POINTER or TYPE_MOD_REFERENCE;
173  *  - the visibility (see below with the function flags for the constants)
174  *  - the type object pointer (depending on context either counted or not
175  *      counted).
176  *
177  * The pieces show up in these types:
178  *  - vartype:  datatype + type object pointer
179  *  - fulltype: vartype + visibility flags
180  *
181  * TODO: A clean implementation would use just type objects for types.
182  */
183 
184 /* --- struct vartype_s: Basic type information
185  *
186  * This structure holds the type number, the flags concerning _MOD_POINTER
187  * and _MOD_REFERENCE, and the virtual variable flag (for use in variable_t).
188  * It also holds a pointer to the type object; depending on the context
189  * this reference may be counted or not.
190  */
191 
192 struct vartype_s {
193     typeid_t        type;
194 #ifdef USE_STRUCTS
195     struct_type_t * t_struct;
196       /* For now, only structs have type objects */
197 #endif
198 };
199 
200 /* --- struct fulltype_s: Full type information
201  *
202  * This structure holds the type number and all flags: type modifiers and
203  * visibility.
204  * It also holds a pointer to the type object; depending on the context
205  * this reference may be counted or not.
206  *
207  * We do not reuse vartype_t her even though it logically should be, for
208  * two reasons:
209  *  - the visibility modifier flags expected that type and flags are
210  *    in one single long
211  *  - some compilers would add 2 bytes of padding
212  *
213  * TODO: Move the basic visibility  into the lower bits so we don't have
214  * TODO:: to use a short for .typeflags.
215  */
216 struct fulltype_s {
217     typeflags_t     typeflags;
218 #ifdef USE_STRUCTS
219     struct_type_t * t_struct;
220       /* For now, only structs have type objects */
221 #endif
222 };
223 
224 /* --- Primary type values --- */
225 enum primary_types {
226     TYPE_UNKNOWN      =  0,   /* This type must be casted */
227     TYPE_NUMBER       =  1,
228     TYPE_STRING       =  2,
229     TYPE_VOID         =  3,
230     TYPE_OBJECT       =  4,
231     TYPE_MAPPING      =  5,
232     TYPE_FLOAT        =  6,
233     TYPE_ANY          =  7,  /* Will match any type */
234     TYPE_CLOSURE      =  8,
235     TYPE_SYMBOL       =  9,
236     TYPE_QUOTED_ARRAY = 10,
237 #ifdef USE_STRUCTS
238     TYPE_STRUCT       = 11,   /* Secondary info is the struct id */
239 
240     TYPEMAP_SIZE      = 12,   /* Number of types */
241 #else
242 
243     TYPEMAP_SIZE      = 11,   /* Number of types */
244 #endif /* USE_STRUCTS */
245 
246 };
247 
248 /* Flags, or'ed on top of the basic type */
249 enum type_flags {
250     TYPE_MOD_POINTER   = 0x0040,       /* Pointer to a basic type */
251     TYPE_MOD_REFERENCE = 0x0080,       /* Reference to a type */
252     TYPE_MOD_MASK      = 0x000000ff,   /* Mask for basic type and flags. */
253 
254     /* Mask for the primary type info (sans modifiers) */
255     PRIMARY_TYPE_MASK  = 0x0F,
256 
257     /* Mask to delete TYPE_MOD_REFERENCE and the visibility mods from
258      * a type value. */
259     TYPE_MOD_RMASK     = (TYPE_MOD_MASK & ~TYPE_MOD_REFERENCE),
260 
261      /* Mask to mask out just the typeid_t from a typeflags_t.
262       */
263     TYPEID_MASK        = 0x0000ffff,
264 
265      /* Flag set in virtual variables, also interpreted as offset
266       * in the variable index for virtual variables. */
267     VIRTUAL_VAR_TAG    = 0x4000,
268 };
269 
270 
271 
272 #ifdef USE_STRUCTS
273 /* Macros to check a type value for a certain primary type.
274  */
275 static INLINE Bool IS_TYPE_STRUCT(fulltype_t t) __attribute__((pure));
IS_TYPE_STRUCT(fulltype_t t)276 static INLINE Bool IS_TYPE_STRUCT(fulltype_t t) {
277     return (t.typeflags & (PRIMARY_TYPE_MASK|TYPE_MOD_POINTER|TYPE_MOD_REFERENCE))
278              == TYPE_STRUCT;
279 }
280 static INLINE Bool IS_TYPE_ANY(fulltype_t t) __attribute__((pure));
IS_TYPE_ANY(fulltype_t t)281 static INLINE Bool IS_TYPE_ANY(fulltype_t t) {
282     return (t.typeflags & (PRIMARY_TYPE_MASK|TYPE_MOD_POINTER|TYPE_MOD_REFERENCE))
283              == TYPE_ANY;
284 }
285 
286 /* Other type related defines */
287 enum {
288      STRUCT_MAX_MEMBERS = 255,
289   /* We allow up to this number of members per struct, so that
290    * we can encode the number of actual members, where needed,
291    * in a single bytecode.
292    */
293 };
294 
295 
296 /* void ref_vartype_data(vartype_t *v)
297  *   Add another reference to the data associated with vartype <v>.
298  *
299  * void ref_fulltype_data(fulltype_t *v)
300  *   Add another reference to the data associated with fulltype <v>.
301  */
302 
303 #define ref_vartype_data(v) \
304     do{ vartype_t *fvt = v; \
305         if (fvt->t_struct) (void)ref_struct_type(fvt->t_struct);\
306     } while(0)
307 
308 #define ref_fulltype_data(v) \
309     do{ fulltype_t *fvt = v; \
310         if (fvt->t_struct) (void)ref_struct_type(fvt->t_struct);\
311     } while(0)
312 
313 
314 /* void free_vartype_data(vartype_t *v)
315  *   Free all data associated with vartype <v>.
316  *
317  * void free_fulltype_data(fulltype_t *v)
318  *   Free all data associated with fulltype <v>.
319  */
320 
321 #define free_vartype_data(v) \
322     do{ vartype_t *fvt = v; \
323         if (fvt->t_struct) { /* printf("DEBUG: free_vartype(%s) %s %d\n", get_txt(fvt->t_struct->name), __FILE__, __LINE__); */ free_struct_type(fvt->t_struct); }\
324         fvt->t_struct = NULL;\
325     } while(0)
326 
327 #define free_fulltype_data(v) \
328     do{ fulltype_t *fvt = v; \
329         if (fvt->t_struct) { /* printf("DEBUG: free_fulltype(%s) %s %d\n", get_txt(fvt->t_struct->name), __FILE__, __LINE__); */ free_struct_type(fvt->t_struct); }\
330         fvt->t_struct = NULL;\
331     } while(0)
332 
333 
334 #ifdef GC_SUPPORT
335 
336 /* void clear_vartype_ref(vartype_t *v)
337  *   Clear all references associated with vartype <v>.
338  *
339  * void clear_fulltype_ref(fulltype_t *v)
340  *   Clear all references associated with fulltype <v>.
341  */
342 
343 #define clear_vartype_ref(v) \
344     do{ vartype_t *fvt = v; \
345         if (fvt->t_struct) clear_struct_type_ref(fvt->t_struct);\
346     } while(0)
347 
348 #define clear_fulltype_ref(v) \
349     do{ fulltype_t *fvt = v; \
350         if (fvt->t_struct) clear_struct_type_ref(fvt->t_struct);\
351     } while(0)
352 
353 
354 /* void count_vartype_ref(vartype_t *v)
355  *   Count all references associated with vartype <v>.
356  *
357  * void count_fulltype_ref(fulltype_t *v)
358  *   Count all references associated with fulltype <v>.
359  */
360 
361 #define count_vartype_ref(v) \
362     do{ vartype_t *fvt = v; \
363         if (fvt->t_struct) count_struct_type_ref(fvt->t_struct);\
364     } while(0)
365 
366 #define count_fulltype_ref(v) \
367     do{ fulltype_t *fvt = v; \
368         if (fvt->t_struct) count_struct_type_ref(fvt->t_struct);\
369     } while(0)
370 
371 #endif /* GC_SUPPORT */
372 
373 #else /* !USE_STRUCTS */
374 
375 #define ref_vartype_data(v) NOOP
376 #define ref_fulltype_data(v) NOOP
377 
378 #define free_vartype_data(v) NOOP
379 #define free_fulltype_data(v) NOOP
380 
381 #ifdef GC_SUPPORT
382 
383 #define clear_vartype_ref(v) NOOP
384 #define clear_fulltype_ref(v) NOOP
385 
386 #define count_vartype_ref(v) NOOP
387 #define count_fulltype_ref(v) NOOP
388 
389 #endif /* GC_SUPPORT */
390 
391 #endif /* USE_STRUCTS */
392 
393 
394 /* --- struct instr_s: description of stackmachine instructions ---
395  *
396  * Stackmachine instructions are both 'internal' codes with no external
397  * representation, as well as efuns.
398  *
399  * The information about all instructions is collected in the table
400  * instrs[] which is indexed by the code of the instructions.
401  * The .prefix and .opcode fields give the proper stackmachine opcodes
402  * for every instruction.
403  *
404  * The table is declared in instrs.h and defined in the file efun_defs.c
405  * both of which are created by make_func from the func_spec file.
406  * The table is compiled into the lexer module and exported from there.
407  */
408 
409 struct instr_s
410 {
411     bytecode_t prefix;  /* 0 or prefix code if required */
412     bytecode_t opcode;  /* The instructions (sub-)opcode */
413     short max_arg;    /* Maximum number of arguments, -1 for '...' */
414     short min_arg;
415       /* Minimum number of arguments.
416        * The number 0 marks incallable closures: instructions which
417        * are used as syntactic markers in lambda closures, but by
418        * themselves can't be executed.
419        */
420     short Default;
421       /* An efun to use as default value for last argument.
422        * > 0: index into instrs[] to the efun to use.
423        *   0: no default value.
424        *  -1: this whole entry describes an internal stackmachine code,
425        *      not a normal efun (an 'operator' in closure lingo).
426        */
427     fulltype_t ret_type;  /* The return type used by the compiler. */
428     short arg_index;     /* Indexes the efun_arg_types[] arrays. */
429     short lpc_arg_index; /* Indexes the efun_lpc_types[] arrays. */
430                          /* A '-1' index means that no type information
431                           * is available.
432                           */
433     char *name;          /* The printable name of the instruction. */
434     char *deprecated;    /* Usually NULL, for deprecated efuns this is
435                           * the warning message to print.
436                           */
437 };
438 
439 
440 /* --- Function flags ---
441  *
442  * Programs know about their functions from an array of uint32s which hold
443  * the flags for the function and, in one field in the low bits, the
444  * address of the code.
445  *
446  * Some flags (TYPE_MOD_*) are also used for variable types.
447  *
448  * Using a bitfield is tempting, but generates inefficient code due to the
449  * lack of a real 'bool' datatype.
450  */
451 
452 typedef uint32 funflag_t;  /* Function flags */
453 typedef int32 sfunflag_t;  /* signed version of Function flags */
454 
455 enum function_flags {
456     NAME_INHERITED     = 0x80000000,  /* defined by inheritance */
457     TYPE_MOD_STATIC    = 0x40000000,  /* Static function or variable    */
458     TYPE_MOD_NO_MASK   = 0x20000000,  /* The nomask => not redefineable */
459     TYPE_MOD_PRIVATE   = 0x10000000,  /* Can't be inherited             */
460     TYPE_MOD_PUBLIC    = 0x08000000,  /* Force inherit through private  */
461     TYPE_MOD_VARARGS   = 0x04000000,  /* Used for type checking         */
462     VAR_INITIALIZED    = 0x04000000,  /* Variable is not shared         */
463     TYPE_MOD_VIRTUAL   = 0x02000000,  /* can be re- and cross- defined  */
464     TYPE_MOD_PROTECTED = 0x01000000,  /* cannot be called externally    */
465     TYPE_MOD_XVARARGS  = 0x00800000,  /* accepts optional arguments     */
466     TYPE_MOD_NOSAVE    = 0x00400000,  /* vars: can't be saved           */
467     NAME_CROSS_DEFINED = 0x00200000,
468     TYPE_MOD_DEPRECATED  = 0x00100000, /* lfun is marked deprecated     */
469   /* Two functions with the same name inherited from A and B into C.
470    * The compiler usually prefers A, and the value 'flags & INHERIT_MASK'
471    * (in bias-0x20000 representation) stored as B.offset.func is the
472    * difference between to the real functions index (also see below).
473    * A special use is A uses a function from B. The function is marked
474    * in A as undefined, but the compiler will use cross-defining in C
475    * to resolve the function calls in A to call the function in B.
476    */
477 
478     FUNSTART_MASK      = 0x000fffff,
479   /* Function not inherited: unsigned address of the function code relative
480    * to the begin of the program block (struct program_s->program).
481    */
482 
483     INHERIT_MASK        = 0x0003ffff,
484   /* Function inherited: unsigned index of the parent program descriptor
485    * in the struct program_s->inherit[] array. In the parent program, this
486    * function may again be inherited.
487    * Function crossdefined: signed difference (in bias-0x20000 notation)
488    * from the current function index to the real function index
489    * (real = this + offset).
490    */
491 
492 #ifdef USE_STRUCTS
493     NAME_HIDDEN        = 0x00020000, /* Not visible for inheritance    */
494     NAME_PROTOTYPE     = 0x00010000, /* Defined by a prototype only    */
495     NAME_UNDEFINED     = 0x00008000, /* Not defined yet                */
496     NAME_TYPES_LOST    = 0x00004000, /* inherited, no save_types       */
497 #else /* !USE_STRUCTS */
498     NAME_HIDDEN        = 0x00000800, /* Not visible for inheritance    */
499     NAME_PROTOTYPE     = 0x00000400, /* Defined by a prototype only    */
500     NAME_UNDEFINED     = 0x00000200, /* Not defined yet                */
501     NAME_TYPES_LOST    = 0x00000100, /* inherited, no save_types       */
502 #endif /* USE_STRUCTS */
503 };
504 
505 
506 /* --- Function header ---
507  *
508  * The bytecode for every function is preceeded by a header with the name
509  * and information about arguments and types:
510  *
511  * struct fun_hdr {
512  *     shared string_t * name_of_function;   (4 Bytes)
513 #ifdef USE_STRUCTS
514  *     vartype_t         return_type;        (6 Byte)
515  *         References from the return_type are not counted!
516 #else
517  *     vartype_t         return_type;        (2 Byte)
518 #endif
519  * --> byte              number_formal_args; (1 Byte)
520  *         Bit 7: set if the function has a 'varargs' argument
521  * TODO: some code makes use of the fact that this makes the number negative
522  *         Bit 6..0: the number of arguments
523  *     byte              number_local_vars;  (1 Byte)
524  *         This includes the svalues needed for the break stack for
525  *         switch() statements.
526  *     bytecode_t        opcode[...]
527  * }
528  *
529  * The function address given in the program's function block points to
530  * .number_formal_args.
531  *
532  * Since structs introduce uncontrollable padding, access of all fields
533  * is implemented using macros taking the 'function address', typedef'd
534  * as fun_hdr_p, as argument and evaluate to the desired value.
535  *
536  * The whole header structure is aligned properly so that the name_of_function
537  * pointer and the return_type short can be used directly.
538  *
539  * Note: Changes here can affect the struct lambda layout and associated
540  *       constants.
541  * TODO: the other fields should have proper types, too.
542  * TODO: the whole information should be in a table, and not in the
543  * TODO:: bytecode. See struct program_s.
544  */
545 
546 typedef bytecode_p fun_hdr_p;
547   /* TODO: Make this a void* for now? */
548 
549 /* TODO: Ugh. I am not convinced that this is a good idea, although I don't
550  * TODO::have a better one right now. */
551 static const bytecode_p SIMUL_EFUN_FUNSTART = (bytecode_p)-1;
552   /* Special value used for inter_pc and funstart to mark simul_efuns
553    * for dump_trace().
554    * TODO: Invent something similar for efun/operator closures?
555    */
556 
557 static const bytecode_p EFUN_FUNSTART = (bytecode_p)-2;
558   /* Special value used for funstart to mark efuns for dump_trace.
559    */
560 
561 
562 static INLINE void * FUNCTION_NAMEP(const fun_hdr_p const p)
563                           __attribute__((nonnull(1))) __attribute__((const));
FUNCTION_NAMEP(const fun_hdr_p const p)564 static INLINE void * FUNCTION_NAMEP(const fun_hdr_p const p)
565 {
566     return (char *)p - sizeof(vartype_t) - sizeof(string_t *);
567 }
568 static INLINE void * FUNCTION_TYPEP(const fun_hdr_p const p)
569                           __attribute__((nonnull(1))) __attribute__((const));
FUNCTION_TYPEP(const fun_hdr_p const p)570 static INLINE void * FUNCTION_TYPEP(const fun_hdr_p const p)
571 {
572     return (char *)p - sizeof(vartype_t);
573 }
574 static INLINE int FUNCTION_NUM_ARGS(const fun_hdr_p const p)
575                           __attribute__((nonnull(1))) __attribute__((pure));
FUNCTION_NUM_ARGS(const fun_hdr_p const p)576 static INLINE int FUNCTION_NUM_ARGS(const fun_hdr_p const p)
577 {
578     return EXTRACT_SCHAR((char *)p);
579 }
580 static INLINE int FUNCTION_NUM_VARS(const fun_hdr_p const p)
581                           __attribute__((nonnull(1))) __attribute__((pure));
FUNCTION_NUM_VARS(const fun_hdr_p const p)582 static INLINE int FUNCTION_NUM_VARS(const fun_hdr_p const p)
583 {
584     return EXTRACT_UCHAR((char *)p + sizeof(char));
585 }
586 static INLINE bytecode_p FUNCTION_CODE(const fun_hdr_p const p)
587                           __attribute__((nonnull(1))) __attribute__((const));
FUNCTION_CODE(const fun_hdr_p const p)588 static INLINE bytecode_p FUNCTION_CODE(const fun_hdr_p const p)
589 {
590     return (unsigned char *)p + 2 * sizeof(char);
591 }
592 static INLINE bytecode_p FUNCTION_FROM_CODE(const fun_hdr_p const p)
593                           __attribute__((nonnull(1))) __attribute__((const));
FUNCTION_FROM_CODE(const fun_hdr_p const p)594 static INLINE bytecode_p FUNCTION_FROM_CODE(const fun_hdr_p const p)
595 {
596     return (unsigned char *)p - 2 * sizeof(char);
597 }
598 
599 enum function_header_sizes {
600     /* Number of function header bytes before the function pointer. */
601     FUNCTION_PRE_HDR_SIZE = sizeof(string_t*) + sizeof(vartype_t),
602     /* Number of function header bytes after the function pointer. */
603     FUNCTION_POST_HDR_SIZE = 2 * sizeof(char),
604     /* Total size of the function header. */
605     FUNCTION_HDR_SIZE = FUNCTION_PRE_HDR_SIZE + FUNCTION_POST_HDR_SIZE,
606 };
607 
608 
609 /* --- struct variable_s: description of one variable
610  *
611  * This structure describes one variable, inherited or own.
612  * The type part of the .flags is used just by the compiler.
613  */
614 
615 struct variable_s
616 {
617     string_t   *name;   /* Name of the variable (shared string) */
618     fulltype_t  type;
619       /* Type and visibility of the variable (type object counted).
620        * If a variable is inherited virtually, the function flag
621        * TYPE_MOD_VIRTUAL is or'ed .type.typeflags.
622        */
623 };
624 
625 
626 /* --- struct inherit: description of one inherited program
627  *
628  * The information about inherited programs ("objects") for a given
629  * program is stored in an array of these structure, and the inherited
630  * programs are accessed from the childs' program code by indexing this array.
631  */
632 
633 struct inherit_s
634 {
635     program_t *prog;  /* Pointer to the inherited program */
636     unsigned short function_index_offset;
637       /* Offset of the inherited program's function block within the
638        * inheriting program's function block.
639        */
640     unsigned short variable_index_offset;
641       /* Offset of the inherited program's variables block within the
642        * inheriting program's variable block.
643        * The NON_VIRTUAL_OFFSET_TAG marks the variables of non-virtual
644        * inherits temporarily during compiles.
645        */
646     unsigned short inherit_type;            /* Type of inherit */
647 
648     unsigned short inherit_depth;           /* Depth of inherit */
649 };
650 /* Constants for inherit_type in inherit_s */
651 enum inherit_types {
652     INHERIT_TYPE_NORMAL    = 0x0000,  /* Type: Normal inherit */
653     INHERIT_TYPE_VIRTUAL   = 0x0001,  /* Type: Virtual inherit */
654     INHERIT_TYPE_EXTRA     = 0x0002,  /* Type: Extra inherit added by
655                                               * copy_variables()
656                                               */
657     INHERIT_TYPE_DUPLICATE = 0x0004,  /* Flag: Duplicate virt inherit */
658 };
659 
660 #ifdef USE_STRUCTS
661 /* --- struct struct_def: description of one struct
662  *
663  * The information about structs visible in the program are stored
664  * in an array of these structures; this includes all inherited structs.
665  */
666 
667 struct struct_def_s
668 {
669     funflag_t       flags;  /* Visibility */
670     struct_type_t * type;   /* The struct definition itself (counted). */
671     int             inh;    /* -1, or the index of the inherit where this
672                              * struct came from.
673                              */
674 };
675 #endif /* USE_STRUCTS */
676 
677 
678 /* --- struct include_s: description of one include file
679  *
680  * This structure describes one include file used to compile the
681  * program.
682  */
683 
684 struct include_s
685 {
686     string_t   *name;
687       /* Name as it was found in the program (tabled string). First and last
688        * character are the delimiters - either "" or <>.
689        */
690 
691     string_t   *filename;
692       /* Actual filename of the include file, in compat mode
693        * without leading slash (tabled string).
694        */
695     int         depth;
696       /* The absolute value is the include depth, counting from 1 upwards.
697        * If the include did not generate code, the value is stored negative.
698        */
699 };
700 
701 
702 /* --- struct program_s: the program head structure
703  *
704  * This structure is actually just the head of the memory block
705  * with all the programs data.
706  */
707 
708 /* TODO: We seem to need a datatype for program offsets (right now: unsigned short).
709  * TODO:: It shows up in quite a lot of places.
710  * TODO: Replace the on program_s structure with a header/data couple. The
711  * TODO:: header keeps vars likes refs, blueprint, swap_num, *data; and the
712  * TODO:: data keeps the static bytecodes and similar. This way we can swap
713  * TODO:: the program even for clones.
714  */
715 
716 struct program_s
717 {
718     p_int           ref;           /* Reference count */
719     p_int           total_size;
720       /* Total size of the memory block allocated for this program.
721        */
722 #ifdef DEBUG
723     p_int           extra_ref;     /* Used to verify ref count */
724 #endif
725     bytecode_p      program;       /* The binary instructions */
726     string_t       *name;
727       /* Name of file that defined prog (untabled, no leading '/',
728        * but a trailing '.c', no embedded '\0')
729        */
730     object_t       *blueprint;
731       /* Counted pointer to the (possibly destructed) blueprint object,
732        * or NULL if the blueprint has been destructed in a previous cycle.
733        * Note: Whenever this member is changed, make sure to remove
734        * the program from the swap ('remove_prog_swap(prog, MY_TRUE)').
735        */
736     int32           id_number;
737       /* The id-number is unique among all programs and used to store
738        * information for this program without actually pointing to
739        * the structure.
740        */
741     mp_int          load_time;     /* When has it been compiled ? */
742     linenumbers_t  *line_numbers;
743       /* Line number information, NULL when not swapped in.
744        * If swapped out, the data is stored in the swap file at
745        * .swapnum+.total_size .
746        */
747     unsigned short *function_names;
748       /* Lookup table [.num_function_names] function-index -> offset of
749        * the function within the functions[] table. function_names[] is
750        * stored right after the the bytecode within the same allocation
751        * unit.
752        * The table is sorted in descending order of the pointers(!)
753        * of the shared function name strings. If the program contains
754        * redefinitions of inherited functions, the entry here points
755        * to the redefinition, the inherited function can then be
756        * found from there.
757        */
758     funflag_t *functions;
759       /* Array [.num_functions] with the flags and addresses of all
760        * functions, inherited and own.
761        * Nameless functions (those without an entry in function_names[])
762        * are collected at the end of the table.
763        * TODO: Instead of hiding the function information in the bytecode
764        * TODO:: it should be tabled here.
765        */
766     string_t **strings;
767       /* Array [.num_strings] of the shared strings used by the program.
768        * Stored in reverse order at the end of the array are the pointers
769        * to the names of all included files which generated code - these
770        * are used when retrieving line numbers.
771        */
772     variable_t *variables;
773       /* Array [.num_variables] with the flags, types and names of all
774        * variables.
775        */
776     inherit_t *inherit;
777       /* Array [.num_inherited] of descriptors for (directly) inherited programs.
778        */
779     include_t *includes;
780       /* Array [.num_includes] of descriptors for included files.
781        */
782 #ifdef USE_STRUCTS
783     struct_def_t *struct_defs;
784       /* Array [.num_structs] of struct descriptors */
785 #endif /* USE_STRUCTS */
786 
787     unsigned short flags;
788       /* Flags for the program: */
789 
790     short heart_beat;
791       /* Index of the heart beat function. -1 means no heart beat
792        */
793 
794     /* The types of all function arguments are saved in the
795      * .argument_types[]. To look up the arguments types for
796      * function <n>, retrieve the start index from the .type_start[]
797      * as .type_start[n]. If this index is INDEX_START_NONE, the function
798      * has no type information.
799      *
800      * Both arrays will only be allocated if '#pragma save_types' has
801      * been specified.
802      */
803     vartype_t *argument_types;
804     unsigned short *type_start;
805       /* TODO: Some code relies on this being unsigned short */
806 
807     p_int swap_num;
808       /* The swap number (swap file offset) for an unswapped program
809        * It is set to -1 if it hasn't been swapped yet.
810        */
811 
812     /*
813      * And now some general size information.
814      */
815     unsigned short num_function_names;
816       /* Number of function names listed in the lookup table.
817        * This number is <= .num_functions as the redefinition of
818        * of inherited functions does not need an additional name
819        * entry. Also, private functions have no visible name.
820        */
821     unsigned short num_functions;
822       /* Number of functions (inherited and own) of this program */
823     unsigned short num_strings;
824       /* Number of shared strings (including filenames) used by the program */
825     unsigned short num_includes;
826       /* Number of stored include filenames */
827     unsigned short num_variables;
828       /* Number of variables (inherited and own) of this program */
829     unsigned short num_inherited;
830       /* Number of (directly) inherited programs */
831 #ifdef USE_STRUCTS
832     unsigned short num_structs;
833       /* Number of listed struct definitions */
834 #endif /* USE_STRUCTS */
835 };
836 
837 /* Constants for flags in program_s. */
838 enum program_flags {
839     P_REPLACE_ACTIVE  = 0x0001,
840       /* This program will be or has been replaced at least once.
841        * As this flag is never reset, the caller must check the
842        * obj_list_replace if his object is affected or not.
843        */
844     P_NO_INHERIT      = 0x0002, /* Program must not be inherited */
845     P_NO_CLONE        = 0x0004, /* No clones allowed */
846     P_NO_SHADOW       = 0x0008, /* No shadows allowed */
847     P_SHARE_VARIABLES = 0x0010, /* Clone vars are assigned from
848                                  * the current blueprint vars. */
849 };
850 /* Special value for type_start in program_s designating that the function has
851  * no type info. */
852 enum {
853     INDEX_START_NONE = 65535,
854 };
855 
856 static INLINE bytecode_p PROGRAM_END(program_t program) __attribute__((pure));
PROGRAM_END(program_t program)857 static INLINE bytecode_p PROGRAM_END(program_t program) {
858     return (bytecode_p)(program.function_names);
859 }
860 
861 
862 /* --- struct linenumbers_s: the linenumber head structure
863  *
864  * This structure is the head of the memory block with the linenumbers
865  * data.
866  */
867 
868 struct linenumbers_s
869 {
870     size_t     size;             /* Total allocated size of this structure */
871     bytecode_t line_numbers[1];
872       /* Array [.size - sizeof(.size)] with the delta-compressed
873        * line number information. This is actually one byte too many, but
874        * that simplifies the swapping code.
875        */
876 };
877 
878 
879 /* --- struct function_s: Function description
880  *
881  * Structures of this type hold various important pieces of
882  * information about a function.
883  * The compiler uses this structure to collect the function information
884  * of newly defined and inherited functions, of which the former will also
885  * be compiled into the function header
886  * The simul_efun module uses this structure to look up quickly functions.
887  */
888 
889 struct function_s
890 {
891     string_t *name;  /* Name of function (shared string) */
892     union {
893         uint32 pc;       /* lfuns: Address of function header */
894         uint32 inherit;  /* Inherit table index from where inherited. */
895          int32 func;
896            /* For cross-defined functions, this is the index offset
897             * to the original function in bias-0x200000 notation.
898             * Semantik: real-index = this-index + offset.
899             * The offset is also stored in the function flags in
900             * the program_t.functions[] array.
901             *
902             * simul_efun.c also uses this field as a 'next'
903             * index in the simul_efun function table for
904             * functions that have been discarded due to a
905             * change in the number of arguments.
906             */
907         function_t *next;        /* used for mergesort */
908     } offset;
909     funflag_t     flags;      /* Function flags */
910     fulltype_t    type;       /* Return type of function (counted). */
911     unsigned char num_local;  /* Number of local variables */
912     unsigned char num_arg;    /* Number of arguments needed. */
913 };
914 
915 enum {
916     SIMUL_EFUN_VARARGS = 0xff,  /* Magic num_arg value for varargs */
917 };
918 
919 /* --- Bytecodes used to encode line numbers ---
920  *
921  * The line number information is a block of bytecode associating small
922  * blocks of bytecode with the generating line number ranges.
923  *
924  * To save space, the information uses a delta compression, necessitating
925  * to read all information from the beginning and counting up a line number
926  * and bytecode offset counter to retrieve a specific bytecode/line number
927  * association.
928  *
929  * The names of included files are stored in the order of appearance
930  * at the end of the string table. Multiple included files are stored
931  * several times.
932  */
933 
934 /* Bytecodes 0x00..0x3a: The amount of program bytes generated for the
935  * current line; this entry is complete with that.
936  */
937 enum line_numbe_bytecodes {
938     LI_MAXOFFSET     = 0x3b,
939   /* The current line generated 0x3b bytes (more), but the entry
940    * is not complete. This code used after the linecodes 0xC0..0xFF.
941    */
942 
943     LI_BACK          = 0x3c,
944   /* Followed by unsigned byte <off>.
945    * The current line counter was set back by <off>+1.
946    */
947 
948     LI_INCLUDE       = 0x3d,
949   /* The following program bytes were generated from the next included
950    * file. Reset the current line number to 0.
951    * LI_INCLUDEs can be nested.
952    */
953 
954     LI_INCLUDE_END   = 0x3e,
955   /* End of the included program part.
956    */
957 
958     LI_L_RELOCATED   = 0x3f,
959   /* Followed by bytes <hi> <lo> LI_L_RELOCATED
960    * The lines were relocated from a line by a delta to the current_line-2.
961    * The delta, an signed int16, is encoded in <hi> and <lo>.
962    */
963 
964 /* Bytecodes 0x40..0x7f encode actual code/line number relations:
965  *   codesize:  1..8
966  *   line incr: 2..9
967  * -> bytecode = (lineincr+6) << 3 | (codesize-1)
968  * Each code is a complete entry.
969  */
970 
971     LI_RELOCATED     = 0xc0,
972   /* Bytecodes 0x80..0xDF: a small line relocation.
973    * The lines were relocated from a line by a delta -0x20..0x1F
974    * to the current_line-2.
975    * The delta is encoded as delta+LI_RELOCATED.
976    */
977 
978     LI_SMALL_REL     = 0x20,
979   /* The allowed magnitude of a relocation to fit into a small
980    * relocation code.
981    */
982 
983     LI_MAXEMPTY      = 0x20,
984   /* Bytecodes 0xE0..0xFF: 'use' a number of lines between 1 and 32
985    * -> bytecode = (0x100 - lines)
986    * The entry is not complete.
987    */
988 };
989 
990 /***************************************************************************/
991 
992 /* Static helper functions */
993 static INLINE intptr_t CROSSDEF_NAME_OFFSET(const funflag_t flags)
994                                           __attribute__((const));
CROSSDEF_NAME_OFFSET(const funflag_t flags)995 static INLINE intptr_t CROSSDEF_NAME_OFFSET(const funflag_t flags)
996   /* For a given NAME_CROSS_DEFINED function, extract the difference to
997    * the real functions index from the flags.
998    */
999 {
1000     // flags is unsigned, but the offset _must_ be signed
1001     return ((sfunflag_t)(flags & INHERIT_MASK)) -
1002                           ((INHERIT_MASK + 1) >> 1);
1003 }
1004 
1005 static INLINE intptr_t GET_CROSSDEF_OFFSET(const funflag_t value)
1006                                           __attribute__((const));
GET_CROSSDEF_OFFSET(const funflag_t value)1007 static INLINE intptr_t GET_CROSSDEF_OFFSET(const funflag_t value)
1008   /* The index difference <value> in bias-notation is converted back into
1009    * the real number. The difference to CROSSDEF_NAME_OFFSET is that <value>
1010    * is supposed to be the plain number, not a real function flags word.
1011    */
1012 {
1013     // value is unsigned, but the offset _must_ be signed
1014     return ((sfunflag_t)(value)) - ((INHERIT_MASK + 1) >> 1);
1015 }
1016 
1017 static INLINE funflag_t MAKE_CROSSDEF_OFFSET(const intptr_t value)
1018                                           __attribute__((const));
MAKE_CROSSDEF_OFFSET(const intptr_t value)1019 static INLINE funflag_t MAKE_CROSSDEF_OFFSET(const intptr_t value)
1020   /* Convert the index difference <value> into the bias-notation for storage
1021    * in the function flags.
1022    */
1023 {
1024     // flags is unsigned, but the offset _must_ be signed
1025     return ((value) + ((INHERIT_MASK + 1) >> 1));
1026 }
1027 
1028 static INLINE funflag_t MAKE_CROSSDEF_ROFFSET(const intptr_t value)
1029                                           __attribute__((const));
MAKE_CROSSDEF_ROFFSET(const intptr_t value)1030 static INLINE funflag_t MAKE_CROSSDEF_ROFFSET(const intptr_t value)
1031   /* Convert the reverse index difference <value> into the bias-notation for
1032    * storage in the function flags.
1033    * TODO: What is this exactly?
1034    */
1035 {
1036     // flags is unsigned, but the offset _must_ be signed
1037     return ((value) - ((INHERIT_MASK + 1) >> 1));
1038 }
1039 
1040 static INLINE inherit_t * search_function_inherit(const program_t *const progp,
1041                                                   const int fx)
1042                                                 __attribute__((pure))
1043                                                 __attribute__((nonnull(1)));
search_function_inherit(const program_t * const progp,const int fx)1044 static INLINE inherit_t * search_function_inherit(const program_t *const progp,
1045                                                   const int fx)
1046   /* Look for the right inherit index, which contains the function with
1047    * the index <fx>. It is assumed, that progp has at least one inherit
1048    * and that <fx> points indeed to a function in an inherit of <progp>.
1049    */
1050 {
1051     int inum = progp->num_inherited;
1052     inherit_t *inheritp;
1053 
1054     for (inheritp = progp->inherit ;
1055            inum > 0 && (fx < inheritp->function_index_offset ||
1056            fx >= inheritp->function_index_offset + inheritp->prog->num_functions);
1057            inheritp++, inum--) NOOP;
1058 
1059     return inheritp;
1060 }
1061 
1062 #endif /* EXEC_H__ */
1063