1 /* Runtime types. 2 3 Copyright (C) 2015-2019, 2021 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 #include <stdlib.h> 27 28 /* Include the special instance position value. The pos member of __obj refers 29 to the special type attribute for classes, indicating which position holds 30 the attribute describing the class type. For instances, it is set to the same 31 attribute position as __class__ and is defined in the following file. */ 32 33 #include "instancepos.h" 34 35 typedef uint16_t __code; 36 typedef uint16_t __pos; 37 typedef uint16_t __pcode; 38 typedef uint16_t __ppos; 39 40 /* Attribute tables are lists of codes confirming the presence of attributes. */ 41 42 typedef struct __table 43 { 44 const __pos size; 45 const size_t trailing_size; 46 const __code attrs[]; 47 } __table; 48 49 /* Parameter tables are lists of codes confirming the presence of parameters, as 50 well as the positions of those parameters in the list for a given function. 51 */ 52 53 typedef struct __param 54 { 55 __code code; 56 __pos pos; 57 } __param; 58 59 typedef struct __ptable 60 { 61 const __ppos min, max, size; 62 const __param params[]; 63 } __ptable; 64 65 /* Attributes are values referring to objects or encoding other information. 66 Objects are collections of attributes. 67 Object references are references to tables and collections of attributes. 68 Attribute references are references to single attributes. */ 69 70 typedef struct __obj __obj; 71 typedef struct __fragment __fragment; 72 typedef union __attr __attr; 73 typedef __obj * __ref; 74 75 /* Introduce an integer type that is interoperable with the size type. */ 76 77 typedef ssize_t __int; 78 typedef size_t __uint; 79 80 /* Introduce a floating point type. */ 81 82 typedef _Float64 __float; 83 84 /* Attribute value interpretations. */ 85 86 typedef union __attr 87 { 88 /* General attribute members. */ 89 90 __ref value; /* attribute value */ 91 uintptr_t rawvalue; /* raw attribute value used to test tagging */ 92 93 /* Special case attribute members. */ 94 95 const __ptable * ptable; /* parameter table */ 96 struct { 97 __pcode code; /* parameter table code for key */ 98 __ppos pos; /* parameter table position for key */ 99 }; 100 __attr (*fn)(); /* callable details */ 101 char * strvalue; /* string value */ 102 __fragment * seqvalue; /* sequence data */ 103 void * datavalue; /* object-specific data */ 104 __int sizevalue; /* object-specific size */ 105 106 /* Value stack pointer for parameter usage. */ 107 108 __attr **stack; 109 110 } __attr; 111 112 typedef struct __obj 113 { 114 const __table * table; /* attribute table */ 115 __ppos pos; /* position of attribute indicating class */ 116 __attr attrs[]; /* attributes */ 117 118 /* Specialisations of this type may add other members. 119 See generator.py for type generation, progops.h for definitions, and 120 the generated progtypes.h for the final details. */ 121 122 } __obj; 123 124 #define __INSTANCE_SIZE(REF) ((REF)->table->size * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos) + (REF)->table->trailing_size) 125 126 /* Fragments are simple collections of attributes employed by sequence types. 127 They provide the basis of lists and tuples. */ 128 129 typedef struct __fragment 130 { 131 __int size, capacity; 132 __attr attrs[]; 133 } __fragment; 134 135 #define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(__int)) 136 137 /* Attribute interpretation. */ 138 139 #define __NUM_TAG_BITS 2 140 #define __TAG_COPYABLE 0b01UL 141 #define __TAG_MUTABLE 0b10UL 142 #define __TAG_MASK 0b11UL 143 144 #define __COPYABLE(ATTR) ((ATTR).rawvalue & __TAG_COPYABLE) 145 #define __MUTABLE(ATTR) ((ATTR).rawvalue & __TAG_MUTABLE) 146 #define __TO_IMMUTABLE(ATTR) ((__attr) {.rawvalue=(ATTR).rawvalue & (~__TAG_MUTABLE)}) 147 #define __TO_MUTABLE(ATTR) ((__attr) {.rawvalue=(ATTR).rawvalue | __TAG_MASK}) 148 149 /* Attribute value setting. */ 150 151 #define __ATTRVALUE(VALUE) ((__attr) {.value=(__ref) VALUE}) 152 #define __NULL __ATTRVALUE(0) 153 #define __SETNULL(ATTR) ((ATTR).value = 0) 154 155 /* Value limits. */ 156 157 #define __MAXINT ((((__uint) 1) << ((sizeof(__int) * 8) - 1)) - 1) 158 #define __MININT (-(((__uint) 1) << ((sizeof(__int) * 8) - 1))) 159 160 /* Argument lists. */ 161 162 #define __ARGS(...) ((__attr[]) {__VA_ARGS__}) 163 #define __KWARGS(...) ((__param[]) {__VA_ARGS__}) 164 165 /* Attribute codes and positions for attribute names. */ 166 167 #define __ATTRCODE(ATTRNAME) __code_##ATTRNAME 168 #define __ATTRPOS(ATTRNAME) __pos_##ATTRNAME 169 #define __PARAMCODE(PARAMNAME) __pcode_##PARAMNAME 170 #define __PARAMPOS(PARAMNAME) __ppos_##PARAMNAME 171 172 #endif /* __TYPES_H__ */