OpenCores
URL https://opencores.org/ocsvn/c16/c16/trunk

Subversion Repositories c16

[/] [c16/] [trunk/] [compiler/] [Node.hh] - Blame information for rev 31

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jsauermann
// Node.hh
2
#ifndef __NODE_HH_DEFINED__
3
#define __NODE_HH_DEFINED__
4
 
5
#include 
6
#include "List.hh"
7
 
8
//-----------------------------------------------------------------------------
9
 
10
class TypeSpecifier;
11
class DeclItem;
12
class Initializer;
13
class TypeName;
14
class Expression;
15
 
16
const char * GetDeclaredName(Declarator * decl);
17
ParameterDeclarationList * GetParameters(Declarator * decl);
18
bool         IsFunPtr(Declarator * decl);
19
bool         IsPointer(Declarator * decl);
20
Expression * ArrayLength(Declarator * decl);
21
void         SetArrayLength(Declarator * decl, int len);
22
bool         IsFunction(Declarator * decl);
23
bool         IsArray(Declarator * decl);
24
 
25
enum SUW { SB, UB, WO };
26
 
27
//-----------------------------------------------------------------------------
28
class Constant : public Node
29
{
30
public:
31
   Constant(const char * name)
32
   : Node(name)
33
   {};
34
 
35
   virtual void EmitValue_RR(FILE * out) = 0;
36
   virtual void EmitValue_LL(FILE * out) = 0;
37
};
38
//-----------------------------------------------------------------------------
39
class NumericConstant : public Constant
40
{
41
public:
42
   NumericConstant(const char * txt);
43
   NumericConstant(int val, int siz = 0)
44
   : Constant("NumericConstant"),
45
     value(val),
46
     size(siz)
47
     {};
48
 
49
   virtual void EmitValue_RR(FILE * out);
50
   virtual void EmitValue_LL(FILE * out);
51
   int GetValue() const   { return value; };
52
   int GetSize()  const   { return size;  };
53
 
54
   void Negate()       { value = -value; };
55
   void Complement()   { value = ~value; };
56
   void LogNot()       { if (value)   value = 0;   else value = -1; };
57
 
58
private:
59
   int value;
60
   int size;
61
};
62
//-----------------------------------------------------------------------------
63
class StringConstant : public Constant
64
{
65
public:
66
   StringConstant();
67
   ~StringConstant();
68
 
69
   virtual void EmitValue_RR(FILE * out);
70
   virtual void EmitValue_LL(FILE * out);
71
   void operator += (char txt);
72
   StringConstant * operator & (StringConstant * other);
73
   void operator += (const char * txt)
74
      { while (*txt)   *this += *txt++; };
75
 
76
   int GetStringNumber() const   { return string_number; };
77
   int GetLength()       const   { return value_len;     };
78
   char * Kill()
79
      { char * ret = buffer;   buffer = 0;   delete this;   return ret; };
80
 
81
   void  EmitAndRemove(FILE * out, int length);
82
 
83
   static void  EmitAll(FILE * out);
84
 
85
private:
86
   char       * buffer;
87
   int          buffer_len;
88
   int          value_len;
89
   int          string_number;
90
 
91
   static int str_count;
92
   enum { MAX_STRINGS = 5000 };
93
   static StringConstant * all_strings[MAX_STRINGS];
94
};
95
//-----------------------------------------------------------------------------
96
// how an expression is created...
97
// //
98
enum BinExprType
99
{
100
   // binary
101
   ET_LIST,             // expr  ,  expr
102
   ET_ARGLIST,          // expr  ,  expr in function argument
103
   ET_ASSIGN,           // expr  =  expr
104
   ET_MULT_ASSIGN,      // expr *=  expr
105
   ET_DIV_ASSIGN,       // expr /=  expr
106
   ET_MOD_ASSIGN,       // expr %=  expr
107
   ET_ADD_ASSIGN,       // expr +=  expr
108
   ET_SUB_ASSIGN,       // expr -=  expr
109
   ET_LEFT_ASSIGN,      // expr <<= expr
110
   ET_RIGHT_ASSIGN,     // expr >>= expr
111
   ET_AND_ASSIGN,       // expr &=  expr
112
   ET_XOR_ASSIGN,       // expr ^=  expr
113
   ET_OR_ASSIGN,        // expr |=  expr
114
   ET_BIT_OR,           // expr |   expr
115
   ET_BIT_AND,          // expr &   expr
116
   ET_BIT_XOR,          // expr ^   expr
117
   ET_LOG_OR,           // expr ||  expr
118
   ET_LOG_AND,          // expr &&  expr
119
   ET_EQUAL,            // expr ==  expr
120
   ET_NOT_EQUAL,        // expr !=  expr
121
   ET_LESS_EQUAL,       // expr <=  expr
122
   ET_LESS,             // expr <   expr
123
   ET_GREATER_EQUAL,    // expr >=  expr
124
   ET_GREATER,          // expr >   expr
125
   ET_LEFT,             // expr <<  expr
126
   ET_RIGHT,            // expr >>  expr
127
   ET_ADD,              // expr +   expr
128
   ET_SUB,              // expr -   expr
129
   ET_MULT,             // expr *   expr
130
   ET_DIV,              // expr /   expr
131
   ET_MOD,              // expr %   expr
132
   ET_ELEMENT,          // expr [   expr ]
133
   ET_FUNCALL           // expr (   ... )
134
};
135
 
136
enum UnaExprType
137
{
138
   ET_CAST,             // (type)   expr
139
   ET_ADDRESS,          //      &   expr
140
   ET_CONTENT,          //      *   expr
141
   ET_CONJUGATE,        //      +   expr
142
   ET_NEGATE,           //      -   expr
143
   ET_COMPLEMENT,       //      ~   expr
144
   ET_LOG_NOT,          //      !   expr
145
   ET_POSTINC,          //      ++  expr
146
   ET_POSTDEC,          //      --  expr
147
   ET_PREINC,           //      ++  expr
148
   ET_PREDEC            //      --  expr
149
};
150
 
151
class Expression : public Node
152
{
153
public:
154
   Expression(const char * nodename)
155
   : Node(nodename),
156
     type_name(0)
157
   {};
158
 
159
   virtual void Emit(FILE * out) = 0;
160
   virtual void Emit_to_ll(FILE * out)   { assert(0); };
161
           void EmitCall(FILE * out, Expression * right);
162
   virtual void EmitInitialization(FILE * out, int size);
163
   virtual void EmitAddress(FILE * out);
164
   virtual void AddAddress(FILE * out);
165
   virtual int  EmitPush(FILE * out, ParameterDeclarationList * params);
166
   virtual void EmitAssign(FILE * out)   { assert(0); };
167
   virtual int  GetParaLength() const    { return 1; };
168
 
169
   virtual bool IsConstant() const          { return false; };
170
   virtual bool IsNumericConstant() const   { return false; };
171
   virtual bool IsStringConstant() const    { return false; };
172
   virtual bool IsVariable() const          { return false; };
173
   virtual const char * GetVarname() const  { return 0;     };
174
   virtual int  GetConstantNumericValue() const;
175
   virtual StringConstant * GetStringConstant() const;
176
   virtual      TypeName * FunReturnType();
177
   virtual      TypeName * SetType() = 0;
178
   virtual SUW  GetSUW();
179
   void SetType(TypeName * t);
180
 
181
   bool       IsPointer();
182
   bool       IsArray();
183
   int        PointeeSize();
184
   int        GetSize();
185
   TypeName * GetType();
186
   bool       IsUnsigned();
187
   int        FunReturnSize();
188
 
189
   static const char * GetPrettyName(const char * pretty);
190
 
191
   virtual Expression * OptNegate()        { assert(0); };
192
   virtual Expression * OptComplement()    { assert(0); };
193
   virtual Expression * OptLogNot()        { assert(0); };
194
 
195
private:
196
   TypeName *  type_name;
197
};
198
//-----------------------------------------------------------------------------
199
class IdentifierExpression : public Expression
200
{
201
public:
202
   static Expression * New(const char * s);
203
 
204
   virtual void  Emit(FILE * out);
205
   virtual void  EmitAssign(FILE * out);
206
   virtual void  Emit_to_ll(FILE * out);
207
   virtual       TypeName * SetType();
208
 
209
   virtual bool             IsVariable() const   { return true;    };
210
   virtual const char *     GetVarname() const   { return varname; };
211
   virtual void             EmitAddress(FILE * out);
212
   virtual void             EmitInitialization(FILE * out, int size);
213
   virtual void             AddAddress(FILE * out);
214
   virtual TypeName *       FunReturnType();
215
 
216
private:
217
   IdentifierExpression(const char * s)
218
   : Expression("Expression (variable name)"),
219
     varname(s)
220
   {};
221
 
222
   const char * varname;
223
};
224
//-----------------------------------------------------------------------------
225
class MemberExpression : public Expression
226
{
227
public:
228
   MemberExpression(bool is_pointer, Expression * r, const char * s);
229
 
230
   virtual void  Emit(FILE * out);
231
   virtual void EmitAddress(FILE * out);
232
   void EmitAddress(FILE * out, bool content);
233
   virtual       TypeName * SetType();
234
 
235
private:
236
   Expression * left;
237
   const char * membername;
238
};
239
//-----------------------------------------------------------------------------
240
class StringExpression : public Expression
241
{
242
public:
243
   StringExpression(StringConstant * s);
244
 
245
   virtual void  Emit(FILE * out);
246
   virtual void EmitAddress(FILE * out);
247
   virtual void  Emit_to_ll(FILE * out);
248
   virtual       TypeName * SetType();
249
   virtual void  EmitInitialization(FILE * out, int size);
250
 
251
   virtual bool  IsConstant()       const   { return true; };
252
   virtual bool  IsStringConstant() const   { return true; };
253
   virtual StringConstant * GetStringConstant() const
254
      { return string_constant; }
255
 
256
private:
257
   StringConstant * string_constant;
258
};
259
//-----------------------------------------------------------------------------
260
class NumericExpression : public Expression
261
{
262
public:
263
   NumericExpression(NumericConstant * n);
264
   NumericExpression(TypeName * t);
265
   NumericExpression(Expression * r);
266
   NumericExpression(int value);
267
 
268
   virtual void Emit(FILE * out);
269
   virtual void Emit_to_ll(FILE * out);
270
   virtual bool IsConstant()        const   { return true; };
271
   virtual bool IsNumericConstant() const   { return true; };
272
   virtual int  GetConstantNumericValue() const;
273
   virtual       TypeName * SetType();
274
   virtual void EmitInitialization(FILE * out, int size);
275
   static const char * GetPretty(int value);
276
 
277
   virtual Expression * OptNegate();
278
   virtual Expression * OptComplement();
279
   virtual Expression * OptLogNot();
280
 
281
private:
282
   NumericConstant * int_value;
283
};
284
//-----------------------------------------------------------------------------
285
class CondExpression : public Expression
286
{
287
public:
288
   CondExpression(Expression * l, Expression * m, Expression * r)
289
   : Expression("Expression (l ? m : r)"),
290
     left(l),
291
     middle(m),
292
     right(r)
293
   {};
294
 
295
   virtual void Emit(FILE * out);
296
   virtual       TypeName * SetType();
297
 
298
private:
299
   Expression      * left;
300
   Expression      * middle;
301
   Expression      * right;
302
};
303
//-----------------------------------------------------------------------------
304
class BinaryExpression : public Expression
305
{
306
public:
307
   static Expression * New(BinExprType et, Expression * l, Expression * r);
308
 
309
   virtual void Emit(FILE * out);
310
   virtual       TypeName * SetType();
311
   virtual void EmitAddress(FILE * out);
312
 
313
   TypeName * MaxType(Expression * l, Expression * r);
314
 
315
   static const char * GetPretty(BinExprType expr_type);
316
   static BinExprType  MapAssign(BinExprType et);
317
 
318
protected:
319
   BinaryExpression(BinExprType et, Expression * l, Expression * r);
320
   BinExprType       expr_type;
321
   Expression      * left;
322
   Expression      * right;
323
};
324
//-----------------------------------------------------------------------------
325
class ArgListExpression : public BinaryExpression
326
{
327
public:
328
   ArgListExpression(Expression * l, Expression * r)
329
   : BinaryExpression(ET_ARGLIST, l, r)
330
   {};
331
 
332
   virtual void Emit(FILE * out)  {};   // done vy EmitPush()
333
   virtual int  EmitPush(FILE * out, ParameterDeclarationList * params);
334
   virtual int  GetParaLength() const;
335
   virtual       TypeName * SetType();
336
};
337
//-----------------------------------------------------------------------------
338
class AdditionExpression : public BinaryExpression
339
{
340
public:
341
   static Expression * New(Expression * l, Expression * r);
342
 
343
   virtual void Emit(FILE * out);
344
   virtual       TypeName * SetType();
345
 
346
private:
347
   AdditionExpression(Expression * l, Expression * r)
348
   : BinaryExpression(ET_ADD, l, r)
349
   {};
350
};
351
//-----------------------------------------------------------------------------
352
class SubtractionExpression : public BinaryExpression
353
{
354
public:
355
   static Expression * New(Expression * l, Expression * r);
356
 
357
   virtual void Emit(FILE * out);
358
   virtual       TypeName * SetType();
359
 
360
private:
361
   SubtractionExpression(Expression * l, Expression * r)
362
   : BinaryExpression(ET_SUB, l, r)
363
   {};
364
};
365
//-----------------------------------------------------------------------------
366
class UnaryExpression : public Expression
367
{
368
public:
369
   UnaryExpression(TypeName * t, Expression * r);
370
 
371
   virtual void Emit(FILE * out);
372
   virtual void EmitAddress(FILE * out);
373
   virtual void EmitInitialization(FILE * out, int size);
374
   virtual       TypeName * SetType();
375
 
376
   static const char * GetPretty(UnaExprType expr_type);
377
   static Expression * New(UnaExprType et, Expression * r);
378
 
379
private:
380
   UnaryExpression(UnaExprType et, Expression * r)
381
   : Expression(GetPrettyName(GetPretty(et))),
382
     expr_type(et),
383
     right(r)
384
   {};
385
 
386
   UnaExprType   expr_type;
387
   Expression  * right;
388
};
389
//-----------------------------------------------------------------------------
390
class AsmExpression : public Expression
391
{
392
public:
393
   AsmExpression(StringConstant * string);
394
 
395
   virtual void Emit(FILE * out);
396
   virtual       TypeName * SetType();
397
 
398
private:
399
     char * asm_string;
400
};
401
//-----------------------------------------------------------------------------
402
enum Specifier
403
{
404
   // storage class
405
   SC_TYPEDEF   = 0x00000001,
406
   SC_EXTERN    = 0x00000002,
407
   SC_STATIC    = 0x00000004,
408
   SC_AUTO      = 0x00000008,
409
   SC_REGISTER  = 0x00000010,
410
   SC_MASK      = SC_TYPEDEF | SC_EXTERN | SC_STATIC  | SC_AUTO  | SC_REGISTER,
411
 
412
   // type qualifiers
413
   TQ_CONST     = 0x00000100,
414
   TQ_VOLATILE  = 0x00000200,
415
   TQ_MASK      = TQ_CONST | TQ_VOLATILE,
416
 
417
   // type specifiers
418
   TS_SIGNED    = 0x00001000,
419
   TS_UNSIGNED  = 0x00002000,
420
   TS_SIGN_MASK = TS_SIGNED | TS_UNSIGNED,
421
 
422
   TS_VOID      = 0x00010000,
423
   TS_CHAR      = 0x00020000,
424
   TS_SHORT     = 0x00040000,
425
   TS_INT       = 0x00080000,
426
   TS_LONG      = 0x00100000,
427
   TS_FLOAT     = 0x00200000,
428
   TS_DOUBLE    = 0x00400000,
429
   TS_STRUCT    = 0x00800000,
430
   TS_UNION     = 0x01000000,
431
   TS_ENUM      = 0x02000000,
432
   TS_TYPE_NAME = 0x04000000,
433
   TS_MASK      = TS_VOID  | TS_CHAR   | TS_SHORT  | TS_INT | TS_LONG |
434
                  TS_FLOAT | TS_DOUBLE | TS_STRUCT |
435
                  TS_UNION | TS_ENUM   | TS_TYPE_NAME,
436
   TS_NUMERIC   = TS_CHAR  | TS_SHORT  | TS_INT    | TS_LONG |
437
                  TS_FLOAT | TS_DOUBLE | TS_ENUM,
438
};
439
//-----------------------------------------------------------------------------
440
class Ptr : public Node
441
{
442
public:
443
   Ptr(TypeSpecifier * ds)
444
   : Node("Ptr"),
445
     decl_specs(ds)
446
     {};
447
 
448
   virtual void Emit(FILE * out);
449
   int Print(FILE * out) const;
450
 
451
private:
452
   TypeSpecifier * decl_specs;
453
};
454
//-----------------------------------------------------------------------------
455
class Identifier : public Node
456
{
457
public:
458
   Identifier(const char * n)
459
   : Node("Identifier"),
460
     name(n)
461
     {};
462
 
463
private:
464
   const char * name;
465
};
466
//-----------------------------------------------------------------------------
467
class Initializer : public Node
468
{
469
public:
470
   Initializer(Expression * expr)
471
   : Node("Initializer (skalar)"),
472
     skalar_value(expr),
473
     array_value(0)
474
     {};
475
 
476
   Initializer(InitializerList * list)
477
   : Node("Initializer (vector)"),
478
     skalar_value(0),
479
     array_value(list)
480
     {};
481
 
482
   virtual void Emit(FILE * out);
483
   virtual void EmitValue(FILE * out, TypeName * tn);
484
 
485 8 jsauermann
   int  InitAutovar(FILE * out, TypeName * type);
486
   int  ElementCount() const;
487 2 jsauermann
 
488
private:
489
   Expression      * skalar_value;
490
   InitializerList * array_value;
491
};
492
//-----------------------------------------------------------------------------
493
class ParameterDeclaration : public Node
494
{
495
public:
496
   ParameterDeclaration(TypeSpecifier * ds, Declarator * dec);
497
 
498
   virtual void Emit(FILE * out);
499
 
500
   int AllocateParameters(int position);
501
   const char * GetDeclaredName(int skip);
502
   TypeName * GetTypeName() const   { return type; };
503
   bool       IsEllipsis() const    { return isEllipsis; };
504
   ParameterDeclaration * SetEllipsis() { isEllipsis = true; return this; };
505
 
506
private:
507
   TypeName * type;
508
   bool       isEllipsis;
509
};
510
//-----------------------------------------------------------------------------
511
enum DECL_WHAT
512
{
513
   DECL_NAME    = 1,
514
   DECL_FUNPTR  = 2,
515
   DECL_ARRAY   = 3,
516
   DECL_FUN     = 4,
517
   DECL_POINTER = 5
518
};
519
 
520
class DeclItem : public Node
521
{
522
public:
523
   DeclItem(DECL_WHAT w)
524
   : Node("DeclItem"),
525
     what(w),
526
     name(0),
527
     funptr(0),
528
     array_size(0),
529
     fun_params(0),
530
     fun_identifiers(0),
531
     pointer(0)
532
     {};
533
 
534
   DeclItem(const char * n)
535
   : Node("DeclItem"),
536
     what(DECL_NAME),
537
     name(n),
538
     funptr(0),
539
     array_size(0),
540
     fun_params(0),
541
     fun_identifiers(0),
542
     pointer(0)
543
     {};
544
 
545
   DeclItem(Declarator * fp)
546
   : Node("DeclItem"),
547
     what(DECL_FUNPTR),
548
     name(0),
549
     funptr(fp),
550
     array_size(0),
551
     fun_params(0),
552
     fun_identifiers(0),
553
     pointer(0)
554
     {};
555
 
556
   DeclItem(Expression * ep)
557
   : Node("DeclItem"),
558
     what(DECL_ARRAY),
559
     name(0),
560
     funptr(0),
561
     array_size(ep),
562
     fun_params(0),
563
     fun_identifiers(0),
564
     pointer(0)
565
     {};
566
 
567
   DeclItem(ParameterDeclarationList * pl)
568
   : Node("DeclItem"),
569
     what(DECL_FUN),
570
     name(0),
571
     funptr(0),
572
     array_size(0),
573
     fun_params(pl),
574
     fun_identifiers(0),
575
     pointer(0)
576
     {};
577
 
578
   DeclItem(IdentifierList * il)
579
   : Node("DeclItem"),
580
     what(DECL_FUN),
581
     name(0),
582
     funptr(0),
583
     array_size(0),
584
     fun_params(0),
585
     fun_identifiers(il),
586
     pointer(0)
587
     {};
588
 
589
   DeclItem(Pointer * p)
590
   : Node("DeclItem"),
591
     what(DECL_POINTER),
592
     name(0),
593
     funptr(0),
594
     array_size(0),
595
     fun_params(0),
596
     fun_identifiers(0),
597
     pointer(p)
598
     {};
599
 
600
   virtual void Emit(FILE * out);
601
   int Print(FILE * out) const;
602
 
603
   const char * GetName()      const   { return name;       };
604
   DECL_WHAT    GetWhat()      const   { return what;       };
605
   Declarator * GetFunptr()    const   { return funptr;     };
606
   Pointer    * GetPointer()   const   { return pointer;    };
607
   Expression * GetArraySize() const   { return array_size; };
608
   void         SetArraySize(int n);
609
   ParameterDeclarationList * GetParameters() const   { return fun_params; };
610
 
611
private:
612
   const DECL_WHAT            what;
613
   const char               * name;
614
   Declarator               * funptr;
615
   Expression               * array_size;
616
   ParameterDeclarationList * fun_params;
617
   IdentifierList           * fun_identifiers;
618
   Pointer                  * pointer;
619
};
620
//-----------------------------------------------------------------------------
621
class Enumerator : public Node
622
{
623
public:
624
   Enumerator(const char * n, Expression * v)
625
   : Node("Enumerator"),
626
     name(n),
627
     value(v)
628
   {};
629
 
630
   virtual void Emit(FILE * out);
631
   static int current;
632
 
633
private:
634
   const char * name;
635
   Expression * value;
636
};
637
//-----------------------------------------------------------------------------
638
class StructDeclarator : public Node
639
{
640
public:
641
   StructDeclarator(Declarator * dcl, Expression * exp)
642
   : Node("StructDeclarator"),
643
     declarator(dcl),
644
     expression(exp),
645
     position(-1)
646
     {};
647
 
648
   virtual void Emit(FILE * out);
649
   int EmitMember(FILE * out, const char * struct_name,
650
        TypeSpecifier * tspec, int pos, bool is_union);
651
   TypeName * GetMemberType(TypeSpecifier * tspec, const char * member);
652
   int GetMemberPosition(const char * member) const;
653
   Declarator * GetDeclarator() const   { return declarator; };
654
   TypeName * FirstUnionMember(TypeSpecifier * tspec, int union_size) const;
655
   const char * GetMemberName() const;
656
 
657
private:
658
   Declarator * declarator;
659
   Expression * expression;   // : bitfield
660
   int          position;
661
};
662
//-----------------------------------------------------------------------------
663
class StructDeclaration : public Node
664
{
665
public:
666
   StructDeclaration(TypeSpecifier * ds, StructDeclaratorList * sdl)
667
   : Node("StructDeclaration"),
668
     decl_specifiers(ds),
669
     struct_decl_list(sdl),
670
     size(-1)
671
     { };
672
 
673
   int Emit(FILE * out, const char * struct_name, int pos, bool is_union);
674
   TypeName * GetMemberType(const char * struct_name, const char * member);
675 8 jsauermann
   TypeName * GetMemberType(int pos);
676 2 jsauermann
   int GetMemberPosition(const char * struct_name,
677
                         const char * member, bool is_union) const;
678
   int GetSize() const   { assert(size != -1);   return size; };
679
 
680
   TypeSpecifier        * GetSpecifier()   const { return decl_specifiers; };
681
   StructDeclaratorList * GetDeclarators() const { return struct_decl_list; };
682
   TypeName * FirstUnionMember(int size) const;
683 8 jsauermann
   int GetDeclaratorCount() const
684
      { return StructDeclaratorList::Length(struct_decl_list); };
685 2 jsauermann
 
686
private:
687
   TypeSpecifier        * decl_specifiers;
688
   StructDeclaratorList * struct_decl_list;
689
   int size;
690
};
691 8 jsauermann
TypeName * GetMemberType(StructDeclarationList * sdl, int pos);
692 2 jsauermann
//-----------------------------------------------------------------------------
693
class TypeSpecifier : public Node
694
{
695
public:
696
   // all types
697
   TypeSpecifier(Specifier sp)
698
   : Node("TypeSpecifier (all)"),
699
     spec(sp),
700
     name(0),
701
     struct_decl_list(0),
702
     enum_list(0)
703
     {};
704
 
705
   // structs, unions, typedef(name)
706
   TypeSpecifier(Specifier sp, const char * n, StructDeclarationList * sdl);
707
 
708
   // enums
709
   TypeSpecifier(const char * n, EnumeratorList * el)
710
   : Node("TypeSpecifier (enum)"),
711
     spec(TS_ENUM),
712
     name(n),
713
     struct_decl_list(0),
714
     enum_list(el)
715
     {};
716
 
717
   virtual void Emit(FILE * out);
718
   int Print(FILE * out) const;
719
 
720
   TypeSpecifier * operator +(TypeSpecifier & other);
721
   TypeSpecifier * self()   { return this; };
722
 
723
   Specifier    GetType()    const   { return spec; };
724
   const char * GetName()    const   { return name; };
725
   StructDeclarationList * GetStructDecl() const
726
                                     { return struct_decl_list; };
727
   bool         IsUnsigned() const   { return spec & TS_UNSIGNED; };
728
 
729
   int          GetFunReturnSize(Declarator * decl) const;
730
   int          GetSize(Declarator * decl) const;
731
   int          GetBaseSize()              const;
732
   TypeName *   GetMemberType(const char * member);
733
   bool         IsNumericType() const;  // char, short, or int
734
   bool         IsUnion() const
735
      { if (spec & TS_UNION)    return true; return false; };
736
   bool             IsStruct() const
737
      { if (spec & TS_STRUCT)   return true; return false; };
738
 
739
private:
740
   Specifier               spec;                // all types
741
   const char            * name;                // enums, structs and unions
742
   StructDeclarationList * struct_decl_list;    // structs and unions
743
   EnumeratorList        * enum_list;           // enums
744
 
745
   static int anonymous_number;
746
};
747
//-----------------------------------------------------------------------------
748
class InitDeclarator : public Node
749
{
750
public:
751
   InitDeclarator(Declarator * decl, Initializer * init)
752
   : Node("InitDeclarator"),
753
     declarator(decl),
754
     initializer(init)
755
     {};
756
 
757
   virtual void Emit(FILE * out);
758
 
759
   Declarator * GetDeclarator() const   { return declarator; };
760
 
761
   const char * GetDeclaredName(int skip);
762
   void Allocate(FILE * out, TypeSpecifier * spec);
763
   int  EmitAutovars(FILE * out, TypeSpecifier * spec);
764
 
765
private:
766
   Declarator  * declarator;
767
   Initializer * initializer;
768
};
769
//-----------------------------------------------------------------------------
770
class Declaration : public Node
771
{
772
public:
773
   Declaration(TypeSpecifier * ds, InitDeclaratorList * il);
774
 
775
   virtual void Emit(FILE * out);
776
 
777
   void Allocate(FILE * out);
778
   int  EmitAutovars(FILE * out);
779
 
780
private:
781
   TypeSpecifier      * base_type;
782
   InitDeclaratorList * init_list;
783
};
784
//-----------------------------------------------------------------------------
785
class TypeName : public Node
786
{
787
public:
788
   TypeName(TypeSpecifier * ds, Declarator * ad);
789
   TypeName(Specifier sp);
790
 
791
   virtual void Emit(FILE * out);
792
   int Print(FILE * out) const;
793
 
794
   Declarator     * GetDeclarator()    const   { return abs_declarator; };
795
   TypeSpecifier  * GetTypeSpecifier() const   { return decl_spec;  };
796
   const char     * GetDeclaredName();
797
   TypeName       * GetFunReturnType();
798
 
799
   TypeName *       AddressOf() const;
800
   TypeName *       ContentOf() const;
801
   TypeName *       GetElementType() const;
802
   bool             IsNumericType() const;  // char, short, or int
803
   bool             IsPointer() const;
804
   Expression *     ArrayLength() const;
805
   void             SetArrayLength(int len);
806
   bool             IsStruct()    const;
807
   bool             IsUnion()     const;
808
   bool             IsArray()     const;
809
   int              GetPointeeSize() const;
810
   TypeName *       GetMemberType(const char * member);
811
   TypeName *       FirstUnionMember(int size) const;
812
 
813
   int GetFunReturnSize()
814
       { return decl_spec->GetFunReturnSize(abs_declarator); };
815
   int GetSize() const
816
       { return decl_spec->GetSize(abs_declarator); };
817
   SUW GetSUW();
818
   bool IsUnsigned() const;
819
   bool IsFunPtr() const
820
       { return ::IsFunPtr(abs_declarator); };
821
   ParameterDeclarationList * GetParameters() const
822
       { return ::GetParameters(abs_declarator); };
823
 
824
private:
825
   TypeSpecifier * decl_spec;
826
   Declarator    * abs_declarator;
827
};
828
//-----------------------------------------------------------------------------
829
class Statement : public Node
830
{
831
public:
832
   Statement(const char * ntype)
833
   : Node(ntype)
834
   {};
835
 
836
   virtual void Emit(FILE * out) = 0;
837
   virtual bool NotEmpty() const   { return true; };
838
   virtual bool EmitCaseJump(FILE * out, bool def, int loop, int size)
839
      { return false; };
840
};
841
//-----------------------------------------------------------------------------
842
class LabelStatement : public Statement
843
{
844
public:
845
   LabelStatement(const char * n, Statement * stat)
846
   : Statement("Label Statement"),
847
     label_name(n),
848
     statement(stat)
849
     {};
850
 
851
   virtual void Emit(FILE * out);
852
 
853
private:
854
   const char * label_name;
855
   Statement  * statement;
856
};
857
//-----------------------------------------------------------------------------
858
class CaseStatement : public Statement
859
{
860
public:
861
   CaseStatement(Expression * exp, Statement * stat)
862
   : Statement("case Statement"),
863
     case_value(exp),
864
     statement(stat)
865
     {};
866
 
867
   virtual void Emit(FILE * out);
868
   virtual bool EmitCaseJump(FILE * out, bool def, int loop, int size);
869
 
870
private:
871
   Expression * case_value;             // case, or 0 for default
872
   Statement  * statement;
873
};
874
//-----------------------------------------------------------------------------
875
class CompoundStatement : public Statement
876
{
877
public:
878
   CompoundStatement(DeclarationList * dl, StatementList * sl)
879
   : Statement("CompoundStatement"),
880
     decl_list(dl),
881
     stat_list(sl)
882
     {};
883
 
884
   virtual void Emit(FILE * out);
885
   virtual bool NotEmpty() const   { return decl_list || stat_list; };
886
   void EmitCaseJumps(FILE * out, int size);
887
 
888
   int  EmitAutovars(FILE * out);
889
 
890
private:
891
   DeclarationList * decl_list;
892
   StatementList   * stat_list;
893
};
894
//-----------------------------------------------------------------------------
895
class ExpressionStatement : public Statement
896
{
897
public:
898
   ExpressionStatement(Expression * expr)
899
   : Statement("ExpressionStatement"),
900
     expression(expr)
901
   {};
902
 
903
   virtual void Emit(FILE * out);
904
   virtual bool NotEmpty() const   { return expression; };
905
 
906
   Expression * GetExpression() const   { return expression; };
907
 
908
private:
909
   Expression * expression;
910
};
911
//-----------------------------------------------------------------------------
912
class SwitchStatement : public Statement
913
{
914
public:
915
   SwitchStatement(Expression * cond, CompoundStatement * cas)
916
   : Statement("SwitchStatement"),
917
     condition(cond),
918
     case_stat(cas)
919
   {};
920
 
921
   virtual void Emit(FILE * out);
922
 
923
private:
924
   Expression         * condition;
925
   CompoundStatement  * case_stat;
926
};
927
//-----------------------------------------------------------------------------
928
class IfElseStatement : public Statement
929
{
930
public:
931
   IfElseStatement(Expression * cond, Statement * ifs, Statement * els)
932
   : Statement("IfElseStatement"),
933
     condition(cond),
934
     if_stat(ifs),
935
     else_stat(els)
936
   {};
937
 
938
   virtual void Emit(FILE * out);
939
 
940
private:
941
   Expression * condition;
942
   Statement  * if_stat;
943
   Statement  * else_stat;
944
};
945
//-----------------------------------------------------------------------------
946
class DoWhileStatement : public Statement
947
{
948
public:
949
   DoWhileStatement(Statement * bdy, Expression * cond)
950
   : Statement("do while Statement"),
951
     condition(cond),
952
     body(bdy)
953
     {};
954
 
955
   virtual void Emit(FILE * out);
956
 
957
private:
958
   Expression           * condition;
959
   Statement            * body;
960
};
961
//-----------------------------------------------------------------------------
962
class WhileStatement : public Statement
963
{
964
public:
965
   WhileStatement(Expression * cond, Statement * bdy)
966
   : Statement("while Statement"),
967
     condition(cond),
968
     body(bdy)
969
     {};
970
 
971
   virtual void Emit(FILE * out);
972
 
973
private:
974
   Expression           * condition;
975
   Statement            * body;
976
};
977
//-----------------------------------------------------------------------------
978
class ForStatement : public Statement
979
{
980
public:
981
 
982
   ForStatement(ExpressionStatement * f1, ExpressionStatement * f2,
983
                      Expression * f3, Statement * bdy)
984
   : Statement("for Statement"),
985
     for_1(f1),
986
     for_2(f2),
987
     for_3(f3),
988
     body(bdy)
989
     {};
990
 
991
   virtual void Emit(FILE * out);
992
 
993
private:
994
   ExpressionStatement  * for_1;
995
   ExpressionStatement  * for_2;
996
   Expression           * for_3;
997
   Statement            * body;
998
};
999
//-----------------------------------------------------------------------------
1000
class GotoStatement : public Statement
1001
{
1002
public:
1003
   GotoStatement(const char * lab)
1004
   : Statement("goto Statement"),
1005
     label_name(lab)
1006
     {};
1007
 
1008
   virtual void Emit(FILE * out);
1009
 
1010
private:
1011
   const char * label_name;
1012
};
1013
//-----------------------------------------------------------------------------
1014
class ReturnStatement : public Statement
1015
{
1016
public:
1017
   ReturnStatement(Expression * expr)
1018
   : Statement("return Statement"),
1019
     retval(expr)
1020
     {};
1021
 
1022
   virtual void Emit(FILE * out);
1023
 
1024
private:
1025
   Expression * retval;
1026
};
1027
//-----------------------------------------------------------------------------
1028
class ContStatement : public Statement
1029
{
1030
public:
1031
   ContStatement(bool do_brk)
1032
   : Statement("break/continue Statement"),
1033
     do_break(do_brk)
1034
     {};
1035
 
1036
   virtual void Emit(FILE * out);
1037
 
1038
private:
1039
   bool do_break;   // true for break, false for continue
1040
};
1041
//-----------------------------------------------------------------------------
1042
class FunctionDefinition : public Node
1043
{
1044
public:
1045
   FunctionDefinition(TypeSpecifier * ds, Declarator * decl,
1046
                      DeclarationList * dl);
1047
 
1048
   virtual void Emit(FILE * out);
1049
 
1050
   FunctionDefinition * SetBody(CompoundStatement * bdy)
1051
      { body = bdy;  return this; };
1052
 
1053
private:
1054
   TypeName          * ret_type;
1055
   Declarator        * fun_declarator;
1056
   DeclarationList   * decl_list;
1057
   CompoundStatement * body;
1058
};
1059
//-----------------------------------------------------------------------------
1060
 
1061
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.