1 /*      Define system dependent parameters
2  *
3  * $Id: define.h,v 1.18 2016-09-19 09:17:50 dom Exp $
4  */
5 
6 
7  #ifndef DEFINE_H
8  #define DEFINE_H
9 
10  #include "uthash.h"
11  #include "utstring.h"
12 
13 
14 
15 #define MALLOC(x)   malloc(x)
16 #define CALLOC(x,y) calloc(x,y)
17 #define REALLOC(x,y) realloc(x,y)
18 #define STRDUP(x) strdup(x)
19 #define FREENULL(x) do { if  (x != NULL ) { free(x); x = NULL; } } while (0)
20 
21 /*      Stand-alone definitions                 */
22 
23 #define NO              0
24 #define YES             1
25 
26 /* Maximum size of the mantissa, write_double_queue() doesn't respect this yet */
27 #define MAX_MANTISSA_SIZE  7
28 
29 /*      System wide name size (for symbols)     */
30 
31 #if defined(__MSDOS__) && defined(__TURBOC__)
32  #define NAMESIZE 33
33 #else
34  #define NAMESIZE 127
35 #endif
36 
37 
38 /*      Define the symbol table parameters      */
39 
40 
41 
42 #if defined(__MSDOS__) && defined(__TURBOC__)
43 #define NUMLOC          33
44 #else
45 #define NUMLOC		512
46 #endif
47 #define STARTLOC        loctab
48 #define ENDLOC          (STARTLOC+NUMLOC)
49 
50 typedef enum {
51     MODE_NONE,
52     MODE_TYPEDEF,
53     MODE_EXTERN,
54     MODE_CAST
55 } decl_mode;
56 
57 
58 
59 typedef enum {
60     KIND_NONE,
61     KIND_VOID,
62     KIND_CHAR,
63     KIND_SHORT,
64     KIND_INT,
65     KIND_LONG,
66     KIND_FLOAT,
67     KIND_DOUBLE,
68     KIND_ARRAY,
69     KIND_PTR,
70     KIND_CPTR,
71     KIND_STRUCT, /* 11 */
72     KIND_FUNC,
73     KIND_ELLIPSES,
74     KIND_PORT8,
75     KIND_PORT16,
76     KIND_ENUM,
77     KIND_CARRY,
78     KIND_FLOAT16,
79     KIND_LONGLONG,
80 } Kind;
81 
82 #define kind_is_floating(x)  ( (x) == KIND_DOUBLE || (x) == KIND_FLOAT16)
83 #define kind_is_integer(k) ( k == KIND_CHAR || k == KIND_INT || k == KIND_SHORT || k == KIND_LONG || k == KIND_LONGLONG )
84 
85 #define get_float_type(k) (k == KIND_DOUBLE || k == KIND_FLOAT) ? type_double : type_float16
86 
87 typedef struct {
88     size_t    size;
89     void    **elems;
90     void    (*destructor)(void *);
91 } array;
92 
93 typedef struct type_s Type;
94 
95 
96 
97 struct type_s {
98     Kind      kind;
99     int       size;
100     char      isunsigned;
101     char      explicitly_signed;  // Set if "signed" in type definition
102     char      isconst;
103     char      isfar;  // Valid for pointers/array
104     char      name[NAMESIZE];
105     char     *namespace; // Which namespace is this object in
106 
107     Type     *ptr;   // For array, or pointer
108     int       len;   // Length of the array
109 
110     int32_t   value; // For enum, goto position, short call value
111 
112     // bitfields
113     int       bit_offset;
114     int       bit_size;
115 
116     // Structures
117     Type   *tag;     // Reference to the structure type
118     array    *fields; // Fields within the structure (Type)
119     size_t    offset;  // Offset to the member
120     char      weak;
121     char      isstruct;
122 
123     // Function
124     Type    *return_type;
125     array    *parameters; // (Type)
126     uint32_t  flags;        // Fast call etc
127     struct {
128         char  hasva;
129         char  oldstyle;
130         int   params_offset;
131         int       interrupt;  // IRQ number?
132         uint8_t  shortcall_rst;
133         uint16_t shortcall_value;
134         uint16_t hlcall_module;
135         uint16_t hlcall_addr;
136     } funcattrs;
137 
138     UT_hash_handle hh;
139 };
140 
141 extern Type *type_void, *type_carry, *type_char, *type_uchar, *type_int, *type_uint, *type_long, *type_ulong, *type_double, *type_float16, *type_longlong, *type_ulonglong;
142 
143 
144 enum ident_type {
145         ID_VARIABLE = 1,
146         ID_MACRO,
147         ID_GOTOLABEL,
148         ID_ENUM
149     };
150 
151 
152 enum storage_type {
153     STATIK,        /* Implemented in this file, export */
154     STKLOC,        /* On the stack */
155     EXTERNAL,      /* External to this file */
156     EXTERNP,       /* Extern @ */
157     LSTATIC,       /* Static to this file */
158     TYPDEF
159 };
160 
161 
162 /* Symbol flags, | against each other */
163 enum symbol_flags {
164         FLAGS_NONE = 0,
165     //    UNSIGNED = 1,
166     //    FARPTR = 0x02,
167         FARACC = 0x04,
168         FASTCALL = 0x08,      /* for certain lib calls only */
169         CALLEE = 0x40,        /* Called function pops regs */
170         LIBRARY = 0x80,       /* Lib routine */
171         SAVEFRAME = 0x100,    /* Save framepointer */
172         SMALLC = 0x200,       /* L->R calling order */
173         FLOATINGDECL = 0x400, /* For a function pointer, the calling convention is floating */
174         NAKED = 0x800,        /* Function is naked - don't generate any code */
175         CRITICAL = 0x1000,    /* Disable interrupts around the function */
176         SDCCDECL = 0x2000,    /* Function uses sdcc convention for chars */
177         SHORTCALL = 0x4000,   /* Function uses short call (via rst) */
178         SHORTCALL_HL = 0x8000,   /* Use ld HL,$addr style of shortcall */
179         BANKED = 0x10000,      /* Call via the banked_call function */
180         HL_CALL = 0x20000,    /* Call via ld hl, (module) call (addr) */
181         INTERRUPT = 0x40000   /* Function is used for interrupts */
182 };
183 
184 
185 
186 /*      Define symbol table entry format        */
187 
188 typedef struct symbol_s SYMBOL;
189 
190 
191 struct symbol_s {
192         char name[NAMESIZE] ;
193         enum ident_type ident;
194         Kind type;
195         Type *ctype;                     /* Type of this symbol */
196         enum storage_type storage ;       /* STATIK, STKLOC, EXTERNAL */
197         union xx  {          /* offset has a number of interpretations: */
198                 int i ;      /* local symbol:  offset into stack */
199                              /* struct member: offset into struct */
200                              /* global symbol: FUNCTION if symbol is                                 declared fn  */
201                              /* or offset into macro table, else 0 */
202                 SYMBOL *p ;  /* also used to form linked list of fn args */
203         } offset ;
204         char  declared_location[1024];  /* Where it was declared, this will truncated with a silly long path */
205         char  *bss_section;      /* Section that this symbol is in */
206         int  more ;          /* index of linked entry in dummy_sym */
207         char tag_idx ;       /* index of struct tag in tag table */
208         int  size ;          /* djm, storage reqd! */
209         char isconst;        /* Set if const, affects the section the data goes into */
210         char isassigned;     /* Set if we have assigned to it once */
211         char initialised;    /* Initialised at compile time */
212         char func_defined;   /* The function has been defined */
213         enum symbol_flags flags ;         /* djm, various flags:
214                                 bit 0 = unsigned
215                                 bit 1 = far data/pointer
216                                 bit 2 = access via far methods
217                               */
218         int level;           /* Compound level that this variable is declared at */
219         int scope_block;     /* Scope block throughout file? */
220         UT_hash_handle  hh;
221 
222 };
223 
224 
225 typedef struct namespace_s namespace;
226 
227 struct namespace_s {
228     char        *name;
229     SYMBOL      *bank_function;
230     namespace   *next;
231 };
232 
233 
234 
235 
236 
237 
238 
239 /* switch table */
240 
241 #define NUMCASE 256
242 
243 typedef struct switchtab_s SW_TAB;
244 
245 struct switchtab_s {
246         int label ;             /* label for start of case */
247         int64_t value ;             /* value associated with case */
248 } ;
249 
250 
251 /*      Define the "while" statement queue      */
252 
253 #define NUMWHILE        100
254 #define WQMAX           wqueue+(NUMWHILE-1)
255 typedef struct whiletab_s WHILE_TAB;
256 
257 struct whiletab_s {
258         int sp ;                /* stack pointer */
259         int loop ;              /* label for top of loop */
260         int exit ;              /* label at end of loop */
261 } ;
262 
263 #define NUMGOTO         100
264 
265 typedef struct gototab_s GOTO_TAB;
266 
267 struct gototab_s {
268         int     sp;             /* Stack pointer to correct to */
269         SYMBOL *sym;            /* Pointer to goto label       */
270         int     lineno;         /* line where goto was         */
271         int     next;           /* Link to next in goto chain  */
272         int     label;          /* Literal label               */
273 };
274 
275 
276 
277 /*      Define the literal pool                 */
278 
279 #if defined(__MSDOS__) && defined(__TURBOC__)
280  #define LITABSZ 950
281 #else
282  #define LITABSZ 49152
283 #endif
284 #define LITMAX  LITABSZ-1
285 
286 /*      For the function literal queues... */
287 #if defined(__MSDOS__) && defined(__TURBOC__)
288  #define FNLITQ 5000
289 #else
290  #define FNLITQ 49152
291 #endif
292 #define FNMAX FNLITQ-1
293 
294 /*      Define the input line                   */
295 
296 #define LINESIZE        65536
297 #define LINEMAX         (LINESIZE-1)
298 #define MPMAX           LINEMAX
299 
300 /*  Output staging buffer size */
301 
302 #define STAGESIZE       7000
303 #define STAGELIMIT      (STAGESIZE-1)
304 
305 /*      Define the macro (define) pool          */
306 
307 #define MACQSIZE        500
308 #define MACMAX          MACQSIZE-1
309 
310 /*      Define statement types (tokens)         */
311 
312 #define STIF            1
313 #define STWHILE         2
314 #define STRETURN        3
315 #define STBREAK         4
316 #define STCONT          5
317 #define STASM           6
318 #define STEXP           7
319 #define STDO            8
320 #define STFOR           9
321 #define STSWITCH        10
322 #define STCASE          11
323 #define STDEF           12
324 #define STGOTO          13
325 #define STCRITICAL      14
326 #define STASSERT        15
327 
328 
329 /* Maximum number of (non fatal) errors before we quit */
330 #define MAXERRORS 10
331 
332 /* Maximum number of nested levels */
333 #define MAX_LEVELS 100
334 
335 
336 
337 
338 /* Defines for debugging */
339 
340 #define DBG_CAST1 1
341 #define DBG_CAST2 2
342 
343 #define DBG_ARG1  11
344 #define DBG_ARG2  12
345 #define DBG_ARG3  13
346 
347 #define DBG_GOTO  14
348 
349 #define DBG_FAR1  21
350 #define DBG_ALL   99
351 
352 #define Z80ASM_PREFIX "_"
353 
354 
355 
356 #define CPU_Z80      1
357 #define CPU_Z180     2
358 #define CPU_R2K      4
359 #define CPU_R3K      8
360 #define CPU_Z80N     16
361 #define CPU_8080     32
362 #define CPU_8085     34
363 #define CPU_GBZ80    128
364 
365 #define CPU_RABBIT (CPU_R2K|CPU_R3K)
366 
367 #define IS_8080() (c_cpu == CPU_8080 )
368 #define IS_8085() (c_cpu == CPU_8085 )
369 #define IS_808x() (c_cpu == CPU_8080 || c_cpu == CPU_8085)
370 #define IS_GBZ80() (c_cpu == CPU_GBZ80)
371 #define IS_Z80N() (c_cpu == CPU_Z80N)
372 
373 
374 
375 #define INLINE_ALL   255
376 
377 struct parser_stack;
378 
379 struct parser_stack {
380     FILE *sinput;
381     char sline[LINESIZE]; /* copy of line when swapping out */
382     int  slptr;           /* copy of the save line pointer when swapping out */
383     int  slineno;
384     struct parser_stack *next;
385 };
386 
387 
388 
389 typedef struct lvalue_s LVALUE;
390 
391 struct lvalue_s {
392         SYMBOL *symbol ;                /* symbol table address, or 0 for constant */
393         Type   *ltype;
394         Kind    indirect_kind;                  /* type of indirect object, 0 for static object */
395         Kind ptr_type ;                  /* type of pointer or array, 0 for other idents */
396         int is_const ;                  /* true if constant expression */
397         zdouble const_val ;                        /* value of constant expression (& other uses) */
398         void (*binop)(LVALUE *lval) ;                /* function address of highest/last binary operator */
399         char *stage_add ;               /* stage addess of "oper 0" code, else 0 */
400         Type *stage_add_ltype;          /* Type at stage_add being set */
401         Kind val_type ;                  /* type of value calculated */
402 	    Kind oldval_kind;		/* What the valtype was */
403         enum symbol_flags flags;        /* As per symbol */
404         char oflags;                    /* Needed for deref of far str*/
405         int type;                       /* type (from symbol table) */
406         Type *cast_type;
407         int  offset;
408         int  base_offset;               /* Where the variable is located on the stack */
409 } ;
410 
411 /* Enable optimisations that are longer than the conventional sequence */
412 enum optimisation {
413         OPT_LSHIFT32       = (1 << 0),
414         OPT_RSHIFT32       = (1 << 1),
415         OPT_ADD32          = (1 << 2),
416         OPT_SUB16          = (1 << 3),
417         OPT_SUB32          = (1 << 4),
418         OPT_INT_COMPARE    = (1 << 5),
419         OPT_LONG_COMPARE   = (1 << 6),
420         OPT_UCHAR_MULT     = (1 << 7),
421         OPT_DOUBLE_CONST   = (1 << 8),
422         OPT_CHAR_COMPARE   = (1 << 9),
423 };
424 
425 enum maths_mode {
426     MATHS_Z80,  // Classic z80 mode
427     MATHS_IEEE, // 32 bit ieee
428     MATHS_MBFS,  // 32 bit Microsoft single precision
429     MATHS_MBF40, // 40 bit Microsoft
430     MATHS_MBF64, // 64 bit Microsoft double precision
431     MATHS_Z88,   // Special handling for z88 (subtype of MATHS_Z80)
432     MATHS_IEEE16, // Used for _Float16
433     MATHS_AM9511  // AM9511 math processor format
434 };
435 
436 
437 
438 #define dump_type(type) do { \
439         UT_string *output; \
440         utstring_new(output); \
441         type_describe(type,output); \
442         printf("%s\n", utstring_body(output)); \
443         utstring_free(output); \
444     } while (0)
445 
446 
447 extern UT_string *debug_utstr;
448 extern UT_string *debug2_utstr;
449 extern int        scope_block;
450 
451 #endif
452