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