1 /* Runtime types. 2 3 Copyright (C) 2015, 2016, 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3 of the License, or (at your option) any later 8 version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 details. 14 15 You should have received a copy of the GNU General Public License along with 16 this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #ifndef __TYPES_H__ 20 #define __TYPES_H__ 21 22 /* Define code and position types, populated by enum values defined for each 23 program specifically. */ 24 25 #include <stdint.h> 26 27 /* Include the special instance position value. The pos member of __obj refers 28 to the special type attribute for classes, indicating which position holds 29 the attribute describing the class type. For instances, it is set to the same 30 attribute position as __class__ and is defined in the following file. */ 31 32 #include "instancepos.h" 33 34 typedef uint16_t __code; 35 typedef uint16_t __pos; 36 typedef uint16_t __pcode; 37 typedef uint16_t __ppos; 38 39 /* Attribute tables are lists of codes confirming the presence of attributes. */ 40 41 typedef struct __table 42 { 43 const __pos size; 44 const __code attrs[]; 45 } __table; 46 47 /* Parameter tables are lists of codes confirming the presence of parameters, as 48 well as the positions of those parameters in the list for a given function. 49 */ 50 51 typedef struct __param 52 { 53 __code code; 54 __pos pos; 55 } __param; 56 57 typedef struct __ptable 58 { 59 const __ppos min, max, size; 60 const __param params[]; 61 } __ptable; 62 63 /* Attributes are values referring to objects or encoding other information. 64 Objects are collections of attributes. 65 Object references are references to tables and collections of attributes. 66 Attribute references are references to single attributes. */ 67 68 typedef struct __obj __obj; 69 typedef struct __fragment __fragment; 70 typedef union __attr __attr; 71 typedef __obj * __ref; 72 73 typedef union __attr 74 { 75 /* General attribute members. */ 76 77 __ref value; /* attribute value */ 78 int intvalue; /* integer value data (shifted value, tagged) */ 79 80 /* Special case attribute members. */ 81 82 const __ptable * ptable; /* parameter table */ 83 struct { 84 __pcode code; /* parameter table code for key */ 85 __ppos pos; /* parameter table position for key */ 86 }; 87 __attr (*fn)(); /* callable details */ 88 char * strvalue; /* string value */ 89 __fragment * seqvalue; /* sequence data */ 90 void * datavalue; /* object-specific data */ 91 } __attr; 92 93 typedef struct __obj 94 { 95 const __table * table; /* attribute table */ 96 __ppos pos; /* position of attribute indicating class */ 97 __attr attrs[]; /* attributes */ 98 99 /* Specialisations of this type may add other members. 100 See generator.py for type generation, progops.h for definitions, and 101 the generated progtypes.h for the final details. */ 102 103 } __obj; 104 105 #define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos)) 106 107 /* Fragments are simple collections of attributes employed by sequence types. 108 They provide the basis of lists and tuples. */ 109 110 typedef struct __fragment 111 { 112 unsigned int size, capacity; 113 __attr attrs[]; 114 } __fragment; 115 116 #define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(unsigned int)) 117 118 /* Attribute interpretation. */ 119 120 #define __NUM_TAG_BITS 1 121 #define __TAG_INT 0b1 122 #define __INTEGER(ATTR) ((ATTR).intvalue & __TAG_INT) 123 124 /* Attribute value setting. */ 125 126 #define __ATTRVALUE(VALUE) ((__attr) {.value=(__ref) VALUE}) 127 #define __NULL __ATTRVALUE(0) 128 #define __SETNULL(ATTR) ((ATTR).value = 0) 129 130 /* Attribute as instance setting. */ 131 132 #define __INTVALUE(VALUE) ((__attr) {.intvalue=((VALUE) << __NUM_TAG_BITS) | __TAG_INT}) 133 #define __TOINT(ATTR) ((ATTR).intvalue >> __NUM_TAG_BITS) 134 #define __MAXINT ((1 << ((sizeof(__attr) * 8) - 1 - __NUM_TAG_BITS)) - 1) 135 #define __MININT (-(1 << ((sizeof(__attr) * 8) - 1 - __NUM_TAG_BITS))) 136 137 /* Argument lists. */ 138 139 #define __ARGS(...) ((__attr[]) {__VA_ARGS__}) 140 #define __KWARGS(...) ((__param[]) {__VA_ARGS__}) 141 142 /* Attribute codes and positions for attribute names. */ 143 144 #define __ATTRCODE(ATTRNAME) __code_##ATTRNAME 145 #define __ATTRPOS(ATTRNAME) __pos_##ATTRNAME 146 #define __PARAMCODE(PARAMNAME) __pcode_##PARAMNAME 147 #define __PARAMPOS(PARAMNAME) __ppos_##PARAMNAME 148 149 #endif /* __TYPES_H__ */