Add tests, fixes for tests, reinstate and type-convert stuff marked "bitrot"
[ccom.git] / ccom.h
1 #ifndef _CCOM_H_
2 #define _CCOM_H_
3
4 #include <stdio.h>
5
6 #ifdef pdp11
7 typedef int _INT;
8 typedef long _LONG;
9 typedef unsigned int _UNSIGNED_INT;
10 typedef unsigned long _UNSIGNED_LONG;
11 typedef float _FLOAT;
12 typedef double _DOUBLE;
13 #else
14 #include <stdint.h>
15 typedef int16_t _INT;
16 typedef int32_t _LONG;
17 typedef uint16_t _UNSIGNED_INT;
18 typedef uint32_t _UNSIGNED_LONG;
19 typedef struct { uint32_t h; } _FLOAT;
20 typedef struct { uint32_t l; uint32_t h; } _DOUBLE;
21 #endif
22
23 #ifndef NULL
24 #define NULL    0
25 #endif
26
27 /*
28  * Structure of namelist
29  */
30 struct nmlist {
31  /* note: this must be compatible with struct node in respect of the */
32  /* nl_type, nl_subsp fields, because of length(), rlength() computations */
33         char    nl_class;               /* storage class */
34         char    nl_flag;                /* various flags */
35 #if 0
36  /* note: these will be phased out in favour of nl_dtype: */
37         int     nl_type;                /* type */
38         int     *nl_subsp;              /* subscript list */
39         union   str *nl_strp;           /* structure description */
40  /* to here */
41 #endif
42         struct  type *nl_dtype;         /* detailed type */
43         int     nl_offset;              /* post-allocation location */
44         struct  nmlist *nl_nextnm;      /* next name in chain */
45         union   str *nl_sparent;        /* Structure of which this is member */
46         char    nl_blklev;              /* Block level of definition */
47         char    *nl_name;               /* ASCII name */
48 };
49
50 /*
51  * format of a structure description
52  *  Same gadget is also used for fields,
53  *  which can't be structures also.
54  * Finally, it is used for parameter collection.
55  */
56 union str {
57         struct SS {
58                 int     ssize;                  /* structure size */
59                 struct nmlist **memlist;        /* member list */
60         } S;
61         struct FS {
62                 int     flen;                   /* field width in bits */
63                 int     bitoffs;                /* shift count */
64         } F;
65         struct nmlist P;
66 };
67
68 /*
69  * Place used to keep dimensions
70  * during declarations
71  */
72 struct  tdim {
73         int     rank;
74         int     dimens[5];
75 };
76
77 /*
78  * tree nodes for detailed type descriptions, t_id can be:
79  *   INT        0
80  *   CHAR       1
81  *   FLOAT      2
82  *   DOUBLE     3
83  *   STRUCT     4       (means it's a struct stype)
84  *   RSTRUCT    5       (not sure what this is; pass 1 only)
85  *   LONG       6
86  *   UNSIGN     7
87  *   UNION      8       (adjusted later to struct; pass 0 only)
88  *   UNCHAR     8
89  *   UNLONG     9
90  *   VOID       10
91  *   REF        11      (used with PTR, FUNC or ARRAY)
92  *   BITFLD     12      (means it's a struct btype)
93  *   PTR        020     (means it's a struct rtype)
94  *   FUNC       040     (means it's a struct ftype)
95  *   ARRAY      060     (means it's a struct atype)
96  */
97 struct  type {
98         int     t_id;
99 };
100 struct  stype {
101         struct type st_type;
102 #define st_id st_type.t_id
103         struct SS st_S;                 /* structure specification */
104 #define st_ssize st_S.ssize
105 #define st_memlist st_S.memlist
106 };
107 struct  btype {
108         struct type bt_type;
109 #define bt_id bt_type.t_id
110         struct FS bt_F;                 /* bitfield specification */
111 #define bt_flen bt_F.flen
112 #define bt_bitoffs bt_F.bitoffs
113 };
114 struct  rtype {
115         struct type rt_type;
116 #define rt_id rt_type.t_id
117         struct type *rt_reftype;        /* referenced type */
118 };
119 struct  ftype {
120         struct rtype ft_rtype;
121 #define ft_id ft_rtype.rt_id
122 #define ft_reftype ft_rtype.rt_reftype
123         struct nmlist **ft_arglist;     /* argument list */
124 };
125 struct  atype {
126         struct rtype at_rtype;
127 #define at_id at_rtype.rt_id
128 #define at_reftype at_rtype.rt_reftype
129         int     at_nelt;                /* number of elements */
130 };
131
132 /*
133  *  Tree node for unary and binary
134  */
135 struct  node {
136         int     n_op;
137  /* note: these will be phased out in favour of n_dtype: */
138         int     n_type;
139         int     *n_subsp;       /* subscript list for arrays; pass 0 only */
140         union   str *n_strp;    /* structure descr for structs; pass 0 only */
141  /* to here */
142         /*struct        type *n_dtype;*/                /* detailed type */
143 };
144
145 struct  tnode {
146         struct  node tn_node;
147 #define tn_op tn_node.n_op
148 #define tn_type tn_node.n_type
149 #define tn_subsp tn_node.n_subsp
150 #define tn_strp tn_node.n_strp
151 /*#define tn_dtype tn_node.n_dtype*/
152         struct  node *tn_tr1;
153         struct  node *tn_tr2;
154         int     tn_degree;              /* pass 1 only */
155 };
156
157 /*
158  * node names
159  */
160 struct  nnode {
161         struct  node nn_node;
162 #define nn_op nn_node.n_op
163 #define nn_type nn_node.n_type
164 #define nn_subsp nn_node.n_subsp
165 #define nn_strp nn_node.n_strp
166 /*#define nn_dtype nn_node.n_dtype*/
167         struct  nmlist *nn_nmlist;      /* pass 0 only */
168         char    nn_class;               /* pass 1 only */
169         char    nn_regno;               /* pass 1 only */
170         int     nn_offset;              /* pass 1 only */
171 };
172
173 /*
174  * node names for locals
175  *
176  * locnn_class can be:
177  *   SOFFS:     %offset+L%nloc(r%regno)
178  *   OFFS:      %offset(r%regno) XXX this does not use nloc
179  *   STATIC:    %offset+L%nloc
180  *   REG:       r%nloc
181  */
182 struct  locnnode {
183         struct  nnode locnn_nnode;
184 #define locnn_op locnn_nnode.nn_op
185 #define locnn_type locnn_nnode.nn_type
186 #define locnn_subsp locnn_nnode.nn_subsp
187 #define locnn_strp locnn_nnode.nn_strp
188 /*#define locnn_dtype locnn_nnode.nn_dtype*/
189 #define locnn_nmlist locnn_nnode.nn_nmlist
190 #define locnn_class locnn_nnode.nn_class
191 #define locnn_regno locnn_nnode.nn_regno
192 #define locnn_offset locnn_nnode.nn_offset
193         int     locnn_nloc;             /* pass 1 only */
194 };
195
196 /*
197  * node names for externals
198  *
199  * extnn_class can be:
200  *   XOFFS:     %offset+%name(r%regno)
201  *   EXTERN:    %offset+%name
202  */
203 struct  extnnode {
204         struct  nnode extnn_nnode;
205 #define extnn_op extnn_nnode.nn_op
206 #define extnn_type extnn_nnode.nn_type
207 #define extnn_subsp extnn_nnode.nn_subsp
208 #define extnn_strp extnn_nnode.nn_strp
209 /*#define extnn_dtype extnn_nnode.nn_dtype*/
210 #define extnn_nmlist extnn_nnode.nn_nmlist
211 #define extnn_class extnn_nnode.nn_class
212 #define extnn_regno extnn_nnode.nn_regno
213 #define extnn_offset extnn_nnode.nn_offset
214         char    *extnn_name;            /* pass 1 only */
215 };
216
217 /*
218  * short constants
219  */
220 struct  cnode {
221         struct  node cn_node;
222 #define cn_op cn_node.n_op
223 #define cn_type cn_node.n_type
224 #define cn_subsp cn_node.n_subsp
225 #define cn_strp cn_node.n_strp
226 /*#define cn_dtype cn_node.n_dtype*/
227         _INT    cn_value;
228 };
229
230 /*
231  * long constants
232  */
233 struct  lnode {
234         struct  node ln_node;
235 #define ln_op ln_node.n_op
236 #define ln_type ln_node.n_type
237 #define ln_subsp ln_node.n_subsp
238 #define ln_strp ln_node.n_strp
239 /*#define ln_dtype ln_node.n_dtype*/
240         _LONG   ln_lvalue;
241 };
242
243 /*
244  * Floating constants
245  */
246 struct  fnode {
247         struct  cnode fn_cnode;
248 #define fn_op fn_cnode.cn_op
249 #define fn_type fn_cnode.cn_type
250 #define fn_subsp fn_cnode.cn_subsp
251 #define fn_strp fn_cnode.cn_strp
252 /*#define fn_dtype fn_cnode.cn_dtype*/
253 #define fn_value fn_cnode.cn_value
254         _DOUBLE fn_fvalue;
255 };
256
257 /*
258  * Node used for field assignments
259  */
260 struct  fasgn {
261         struct  tnode fa_tnode;
262 #define fa_op fa_tnode.tn_op
263 #define fa_type fa_tnode.tn_type
264 #define fa_subsp fa_tnode.tn_subsp
265 #define fa_strp fa_tnode.tn_strp
266 /*#define fa_dtype fa_tnode.tn_dtype*/
267 #define fa_tr1 fa_tnode.tn_tr1
268 #define fa_tr2 fa_tnode.tn_tr2
269 #define fa_degree fa_tnode.tn_degree
270         _INT    fa_mask;                /* pass 1 only */
271 };
272
273 /*
274  * Table for recording switches.
275  */
276 struct swtab {
277         int     swlab;
278         _INT    swval;
279 };
280
281 /*
282   operators
283 */
284 #define EOFC    0
285 #define SEMI    1
286 #define LBRACE  2
287 #define RBRACE  3
288 #define LBRACK  4
289 #define RBRACK  5
290 #define LPARN   6
291 #define RPARN   7
292 #define COLON   8
293 #define COMMA   9
294 #define FSEL    10
295 #define CAST    11              /* pass 0 only */
296 #define FSELR   11              /* pass 1 only */
297 #define ETYPE   12              /* pass 0 only */
298 #define FSELT   12              /* pass 1 only */
299 #define FSELA   16              /* pass 1 only */
300 #define ULSH    17              /* pass 1 only */
301 #define ASULSH  18              /* pass 1 only */
302
303 #define KEYW    19
304 #define NAME    20
305 #define CON     21
306 #define STRING  22
307 #define FCON    23
308 #define SFCON   24
309 #define LCON    25
310 #define SLCON   26
311 #define NULLOP0 0/*29*/         /* pass 0 only */
312 #define NULLOP  218     /* interface version */
313
314 #define AUTOI   27              /* pass 1 only */
315 #define AUTOD   28              /* pass 1 only */
316 #define INCBEF  30
317 #define DECBEF  31
318 #define INCAFT  32
319 #define DECAFT  33
320 #define EXCLA   34
321 #define AMPER   35
322 #define STAR    36
323 #define NEG     37
324 #define COMPL   38
325
326 #define DOT     39
327 #define PLUS    40
328 #define MINUS   41
329 #define TIMES   42
330 #define DIVIDE  43
331 #define MOD     44
332 #define RSHIFT  45
333 #define LSHIFT  46
334 #define AND     47
335 #define ANDN    55              /* pass 1 only */
336 #define OR      48
337 #define EXOR    49
338 #define ARROW   50
339 #define ITOF    51
340 #define FTOI    52
341 #define LOGAND  53
342 #define LOGOR   54
343 #define FTOL    56
344 #define LTOF    57
345 #define ITOL    58
346 #define LTOI    59
347 #define ITOP    13
348 #define PTOI    14
349 #define LTOP    15
350
351 #define EQUAL   60
352 #define NEQUAL  61
353 #define LESSEQ  62
354 #define LESS    63
355 #define GREATEQ 64
356 #define GREAT   65
357 #define LESSEQP 66
358 #define LESSP   67
359 #define GREATQP 68
360 #define GREATP  69
361
362 #define ASPLUS  70
363 #define ASMINUS 71
364 #define ASTIMES 72
365 #define ASDIV   73
366 #define ASMOD   74
367 #define ASRSH   75
368 #define ASLSH   76
369 #define ASSAND  77              /* pass 0 only */
370 #define ASAND   77              /* pass 1 only */
371 #define ASOR    78
372 #define ASXOR   79
373 #define ASSIGN  80
374 #define TAND    81              /* pass 1 only */
375 #define LTIMES  82              /* pass 1 only */
376 #define LDIV    83              /* pass 1 only */
377 #define LMOD    84              /* pass 1 only */
378 #define ASANDN  85              /* pass 1 only */
379 #define LASTIMES 86             /* pass 1 only */
380 #define LASDIV  87              /* pass 1 only */
381 #define LASMOD  88              /* pass 1 only */
382
383 #define QUEST   90
384 #define SIZEOF  91              /* pass 0 only */
385 #define LLSHIFT 91              /* pass 1 only */
386 #define ASLSHL  92              /* pass 1 only */
387 #define MAX     93              /* pass 0 only */
388 #define MAXP    94
389 #define MIN     95              /* pass 0 only */
390 #define MINP    96
391 #define SEQNC   97
392 #define CALL1   98              /* pass 1 only */
393 #define CALL2   99              /* pass 1 only */
394 #define CALL    100
395 #define MCALL   101
396 #define JUMP    102
397 #define CBRANCH 103
398 #define INIT    104
399 #define SETREG  105
400 #define LOAD    106             /* pass 1 only */
401 #define PTOI1   107             /* pass 1 only */
402 #define ITOC    109
403 #define RFORCE  110
404
405 /*
406  * Intermediate code operators
407  */
408 #define BRANCH  111
409 #define LABEL   112
410 #define NLABEL  113
411 #define RLABEL  114
412 #define STRASG  115
413 #define STRSET  116             /* pass 1 only */
414 #define UDIV    117             /* pass 1 only */
415 #define UMOD    118             /* pass 1 only */
416 #define ASUDIV  119             /* pass 1 only */
417 #define ASUMOD  120             /* pass 1 only */
418 #define ULTIMES 121     /* present for symmetry; pass 1 only */
419 #define ULDIV   122             /* pass 1 only */
420 #define ULMOD   123             /* pass 1 only */
421 #define ULASTIMES 124   /* present for symmetry; pass 1 only */
422 #define ULASDIV 125             /* pass 1 only */
423 #define ULASMOD 126             /* pass 1 only */
424 #define ULTOF   127             /* pass 1 only */
425 #define ULLSHIFT 128    /* << for unsigned long; pass 1 only */
426 #define UASLSHL 129     /* <<= for unsigned long; pass 1 only */
427
428 #define SEOF    200     /* stack EOF marker in expr compilation; pass 0 only */
429
430 /*
431  * Special operators in intermediate code
432  */
433 #define BDATA   200
434 #define WDATA   201             /* pass 0 only */
435 #define PROG    202
436 #define DATA    203
437 #define BSS     204
438 #define CSPACE  205
439 #define SSPACE  206
440 #define SYMDEF  207
441 #define SAVE    208
442 #define RETRN   209
443 #define EVEN    210
444 #define PROFIL  212
445 #define SWIT    213
446 #define EXPR    214
447 #define SNAME   215
448 #define RNAME   216
449 #define ANAME   217
450 #define SETSTK  219
451 #define SINIT   220
452 #define GLOBAL  221             /* pass 1 only */
453 #define C3BRANCH        222             /* pass 1 only */
454 #define ASSEM   223
455
456 /*
457   types
458 */
459 #define INT     0
460 #define CHAR    1
461 #define FLOAT   2
462 #define DOUBLE  3
463 #define STRUCT  4
464 #define RSTRUCT 5               /* pass 1 only */
465 #define LONG    6
466 #define UNSIGN  7
467 #define UNION   8               /* adjusted later to struct; pass 0 only */
468 #define UNCHAR  8
469 #define UNLONG  9
470 #define VOID    10
471 #define REF     11
472 #define BITFLD  12
473
474 #define ALIGN   01              /* pass 0 only */
475 #define TYPE    017
476 #define BIGTYPE 060000          /* pass 0 only */
477 #define TYLEN   2
478 #define XTYPE   (03<<4)
479 #define PTR     020
480 #define FUNC    040
481 #define ARRAY   060
482
483 /*
484   storage classes
485 */
486 #define KEYWC   1
487 #define TYPEDEF 9               /* pass 0 only */
488 #define MOS     10
489 #define AUTO    11
490 #define EXTERN  12
491 #define STATIC  13
492 #define REG     14
493 #define STRTAG  15
494 #define ARG     16
495 #define ARG1    17              /* pass 0 only */
496 #define AREG    18              /* pass 0 only */
497 #define DEFXTRN 20              /* pass 0 only */
498 #define MOU     21              /* pass 0 only */
499 #define ENUMTAG 22              /* pass 0 only */
500 #define ENUMCON 24              /* pass 0 only */
501 #define OFFS    20              /* pass 1 only */
502 #define XOFFS   21              /* pass 1 only */
503 #define SOFFS   22              /* pass 1 only */
504
505 /*
506   Flag bits
507 */
508
509 #define BINARY  01
510 #define LVALUE  02
511 #define RELAT   04
512 #define ASSGOP  010
513 #define LWORD   020
514 #define RWORD   040
515 #define COMMUTE 0100
516 #define RASSOC  0200
517 #define LEAF    0400
518 #define PCVOK   040000          /* pass 0 only */
519 #define CNVRT   01000           /* pass 1 only */
520
521 extern int temp_fi;
522 extern FILE *temp_fp[2];
523
524 #ifndef pdp11
525 #ifndef __P
526 #ifdef __STDC__
527 #define __P(args) args
528 #else
529 #define __P(args) ()
530 #endif
531 #endif
532
533 /* fp.c */
534 int fp_tst __P((_DOUBLE val));
535 _DOUBLE fp_abs __P((_DOUBLE val));
536 _DOUBLE fp_neg __P((_DOUBLE val));
537 int fp_le __P((_DOUBLE val0, _DOUBLE val1));
538 int fp_ge __P((_DOUBLE val0, _DOUBLE val1));
539 int fp_gt __P((_DOUBLE val0, _DOUBLE val1));
540 int fp_lt __P((_DOUBLE val0, _DOUBLE val1));
541 _INT fp_double_to_int __P((_DOUBLE val));
542 _LONG fp_double_to_long __P((_DOUBLE val));
543 _FLOAT fp_double_to_float __P((_DOUBLE val));
544 _DOUBLE fp_int_to_double __P((_INT val));
545 _DOUBLE fp_long_to_double __P((_LONG val));
546 _DOUBLE fp_float_to_double __P((_FLOAT val));
547 _DOUBLE fp_add __P((_DOUBLE val0, _DOUBLE val1));
548 _DOUBLE fp_sub __P((_DOUBLE val0, _DOUBLE val1));
549 _DOUBLE fp_mul __P((_DOUBLE val0, _DOUBLE val1));
550 _DOUBLE fp_div __P((_DOUBLE val0, _DOUBLE val1));
551 _DOUBLE fp_ldexp __P((_DOUBLE val, _INT exp));
552
553 /* fp_atof.c */
554 _DOUBLE fp_atof __P((register char *p));
555 #endif
556
557 #endif