1/*****************************************************************************/
2/* */
3/* gentype.h */
4/* */
5/* Generic data type encoding */
6/* */
7/* */
8/* */
9/* (C) 2011, Ullrich von Bassewitz */
10/* Roemerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
15/* This software is provided 'as-is', without any expressed or implied */
16/* warranty. In no event will the authors be held liable for any damages */
17/* arising from the use of this software. */
18/* */
19/* Permission is granted to anyone to use this software for any purpose, */
20/* including commercial applications, and to alter it and redistribute it */
21/* freely, subject to the following restrictions: */
22/* */
23/* 1. The origin of this software must not be misrepresented; you must not */
24/* claim that you wrote the original software. If you use this software */
25/* in a product, an acknowledgment in the product documentation would be */
26/* appreciated but is not required. */
27/* 2. Altered source versions must be plainly marked as such, and must not */
28/* be misrepresented as being the original software. */
29/* 3. This notice may not be removed or altered from any source */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36/* This module implements a specification for a "generic data type". It is
37** called generic, because it doesn't mimic a high level language. Instead it
38** tries to desrcibe the type as representation on the machine.
39** The reasoning behing this type is to allow a debugger to represent the
40** data to the user, independent of the actual source.
41** C data types may be mapped to generic ones, but attributes like const or
42** volatile are (of course) lost.
43**
44** The data type is stored in a char array; and, can be terminated by a zero
45** (see definition of GT_END below). The latter is not really necessary; but,
46** allows handling of types using the usual string functions. This is in fact
47** one of the advantages of the chosen implementation:
48** String buffers may be used to dynamically build types. Types may be stored
49** as strings in the string pool of an object file. And a string pool may be
50** used to remove duplicates and reference types using unique ids.
51*/
52
53
54
55#ifndef GENTYPE_H
56#define GENTYPE_H
57
58
59
60/* common */
61#include "strbuf.h"
62
63
64
65/*****************************************************************************/
66/* Data */
67/*****************************************************************************/
68
69
70
71/* Size of a data type */
72#define GT_SIZE_1 0x00U
73#define GT_SIZE_2 0x01U
74#define GT_SIZE_3 0x02U
75#define GT_SIZE_4 0x03U
76#define GT_SIZE_MASK 0x07U
77
78#define GT_GET_SIZE(x) (((x) & GT_SIZE_MASK) + 1U)
79
80/* Sign of the data type */
81#define GT_UNSIGNED 0x00U
82#define GT_SIGNED 0x08U
83#define GT_SIGN_MASK 0x08U
84
85#define GT_HAS_SIGN(x) (((x) & GT_SIZE_MASK) == GT_SIGNED)
86
87/* Byte order */
88#define GT_LITTLE_ENDIAN 0x00U
89#define GT_BIG_ENDIAN 0x10U
90#define GT_BYTEORDER_MASK 0x10U
91
92#define GT_IS_LITTLE_ENDIAN(x) (((x) & GT_BYTEORDER_MASK) == GT_LITTLE_ENDIAN)
93#define GT_IS_BIG_ENDIAN(x) (((x) & GT_BYTEORDER_MASK) == GT_BIG_ENDIAN)
94
95/* Type of the data. */
96#define GT_TYPE_VOID 0x00U
97#define GT_TYPE_INT 0x20U
98#define GT_TYPE_PTR 0x40U
99#define GT_TYPE_FLOAT 0x60U
100#define GT_TYPE_ARRAY 0x80U
101#define GT_TYPE_FUNC 0xA0U
102#define GT_TYPE_STRUCT 0xC0U
103#define GT_TYPE_UNION 0xE0U
104#define GT_TYPE_MASK 0xE0U
105
106#define GT_GET_TYPE(x) ((x) & GT_TYPE_MASK)
107#define GT_IS_INTEGER(x) (GT_GET_TYPE(x) == GT_TYPE_INTEGER)
108#define GT_IS_POINTER(x) (GT_GET_TYPE(x) == GT_TYPE_POINTER)
109#define GT_IS_FLOAT(x) (GT_GET_TYPE(x) == GT_TYPE_FLOAT)
110#define GT_IS_ARRAY(x) (GT_GET_TYPE(x) == GT_TYPE_ARRAY)
111#define GT_IS_FUNCTION(x) (GT_GET_TYPE(x) == GT_TYPE_FUNCTION)
112#define GT_IS_STRUCT(x) (GT_GET_TYPE(x) == GT_TYPE_STRUCT)
113#define GT_IS_UNION(x) (GT_GET_TYPE(x) == GT_TYPE_UNION)
114
115/* Combined values for the 6502 family */
116#define GT_VOID (GT_TYPE_VOID)
117#define GT_BYTE (GT_TYPE_INT | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_1)
118#define GT_WORD (GT_TYPE_INT | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_2)
119#define GT_DWORD (GT_TYPE_INT | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_4)
120#define GT_DBYTE (GT_TYPE_PTR | GT_BIG_ENDIAN | GT_UNSIGNED | GT_SIZE_2)
121#define GT_PTR (GT_TYPE_PTR | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_2)
122#define GT_FAR_PTR (GT_TYPE_PTR | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_3)
123#define GT_ARRAY(size) (GT_TYPE_ARRAY | ((size) - 1))
124
125
126
127/*****************************************************************************/
128/* Code */
129/*****************************************************************************/
130
131
132
133void GT_AddArray (StrBuf* Type, unsigned ArraySize);
134/* Add an array with the given size to the type string in Type. This will
135** NOT add the element type!
136*/
137
138unsigned GT_GetElementCount (StrBuf* Type);
139/* Retrieve the element count of an array stored in Type at the current index
140** position. Note: Index must point to the array token itself, since the size
141** of the element count is encoded there. The index position will get moved
142** past the array.
143*/
144
145const char* GT_AsString (const StrBuf* Type, StrBuf* String);
146/* Convert the type into a readable representation. The target string buffer
147** will be zero terminated and a pointer to the contents are returned.
148*/
149
150
151
152/* End of gentype.h */
153
154#endif
155