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

Subversion Repositories c16

[/] [c16/] [trunk/] [compiler/] [Node.cc] - Blame information for rev 7

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

Line No. Rev Author Line
1 2 jsauermann
 
2
#include <stdio.h>
3
#include <assert.h>
4
#include "Node.hh"
5
#include "Name.hh"
6
#include "Backend.hh"
7
 
8
int Node::indent = 0;
9
int Node::semantic_errors = 0;
10
 
11
int Enumerator::current = 0;
12
int TypeSpecifier::anonymous_number = 1;
13
 
14
extern FILE * out;
15
 
16
//-----------------------------------------------------------------------------
17
void NumericConstant::EmitValue_RR(FILE * out)
18
{
19
   Backend::load_rr_constant(value);
20
}
21
//-----------------------------------------------------------------------------
22
void NumericConstant::EmitValue_LL(FILE * out)
23
{
24
   Backend::load_ll_constant(value);
25
}
26
//-----------------------------------------------------------------------------
27
void StringConstant::EmitValue_RR(FILE * out)
28
{
29
   Backend::load_rr_string(string_number, 0);
30
}
31
//-----------------------------------------------------------------------------
32
void StringConstant::EmitValue_LL(FILE * out)
33
{
34
   Backend::load_ll_string(string_number, 0);
35
}
36
//-----------------------------------------------------------------------------
37
Node::Node(const char * ntype)
38
   : node_type(ntype)
39
{
40
   // printf("Creating %s\n", node_type);
41
}
42
//-----------------------------------------------------------------------------
43
void Node::Emit(FILE * out)
44
{
45
   EmitIndent(out);
46
   fprintf(out, "MISSING : %s\n", node_type);
47
   fprintf(stderr, "\n\nMISSING : %s::Emit()\n\n", node_type);
48
}
49
//-----------------------------------------------------------------------------
50
void Node::EmitIndent(FILE * out)
51
{
52
   fprintf(out, ";;; ");
53
   for (int i = 0; i < indent; i++)   fprintf(out, "  ");
54
}
55
//-----------------------------------------------------------------------------
56
void Node::EmitStart(FILE * out)
57
{
58
   EmitIndent(out);
59
   fprintf(out, "{ %d %s\n", indent, node_type);
60
   indent++;
61
}
62
//-----------------------------------------------------------------------------
63
void Node::EmitEnd(FILE * out)
64
{
65
   indent--;
66
   EmitIndent(out);
67
   fprintf(out, "} %d %s\n", indent, node_type);
68
}
69
//-----------------------------------------------------------------------------
70
void StatementList           ::Emit(FILE * out)   { EmitList(out); }
71
void DeclarationList         ::Emit(FILE * out)   { EmitList(out); }
72
void InitializerList         ::Emit(FILE * out)   { EmitList(out); }
73
void ParameterDeclarationList::Emit(FILE * out)   { EmitList(out); }
74
void IdentifierList          ::Emit(FILE * out)   { EmitList(out); }
75
void StructDeclaratorList    ::Emit(FILE * out)   { assert(0);     }
76
void StructDeclarationList   ::Emit(FILE * out)   { assert(0); }
77
void InitDeclaratorList      ::Emit(FILE * out)   { EmitList(out); }
78
void TypeSpecifierList       ::Emit(FILE * out)   { EmitList(out); }
79
void Declarator              ::Emit(FILE * out)   { EmitList(out); }
80
void Pointer                 ::Emit(FILE * out)   { EmitList(out); }
81
//-----------------------------------------------------------------------------
82
void StructDeclarator::Emit(FILE * out)
83
{
84
   assert(declarator);
85
   EmitStart(out);
86
   declarator->Emit(out);
87
   if (expression)
88
      {
89
        EmitIndent(out);
90
        fprintf(out, " : bitfield\n");
91
      }
92
 
93
   EmitIndent(out);
94
   fprintf(out, " at position %d\n", position);
95
   EmitEnd(out);
96
}
97
//-----------------------------------------------------------------------------
98
int StructDeclarator::EmitMember(FILE * out, const char * struct_name,
99
                            TypeSpecifier * tspec, int pos, bool is_union)
100
{
101
   position = pos;
102
   if (is_union)   assert(position == 0);
103
 
104
   if (0)
105
      {
106
        const char * member_name = GetDeclaredName(declarator);
107
 
108
        assert(struct_name);
109
        if (member_name == 0)   member_name = "anonymous member";
110
 
111
        fprintf(stderr, "%s->%s at position %d\n",
112
                struct_name, member_name, position);
113
      }
114
 
115
   return tspec->GetSize(declarator);
116
}
117
//-----------------------------------------------------------------------------
118
void EnumeratorList::Emit(FILE * out)
119
{
120
   Enumerator::current = 0;
121
 
122
   for (EnumeratorList * el = this; el; el = el->tail)
123
       el->head->Emit(out);
124
}
125
//-----------------------------------------------------------------------------
126
void Enumerator::Emit(FILE * out)
127
{
128
   assert(name);
129
 
130
int val = current++;
131
   if (value)
132
      {
133
        if (!value->IsNumericConstant())
134
           {
135
             fprintf(stderr, "enum value for %s is not constant\n", name);
136
             semantic_errors++;
137
           }
138
        else
139
           {
140
             val = value->GetConstantNumericValue();
141
             current = val + 1;
142
           }
143
      }
144
   Name::AddEnum(name, val);
145
}
146
//-----------------------------------------------------------------------------
147
ParameterDeclaration::ParameterDeclaration(TypeSpecifier * ds,
148
                                           Declarator * decl)
149
   : Node("ParameterDeclaration"),
150
     isEllipsis(false)
151
{
152
   type = new TypeName(ds, decl);
153
 
154
const char * pname = type->GetDeclaredName();
155
 
156
   if (pname)   Name::AddAuto(pname, type);
157
}
158
//-----------------------------------------------------------------------------
159
void ParameterDeclaration::Emit(FILE * out)
160
{
161
   EmitStart(out);
162
   EmitIndent(out);
163
   fprintf(out, "isEllipsis = ");
164
   if (isEllipsis)   fprintf(out, "true\r\n");
165
   else              fprintf(out, "false\r\n");
166
   type->Emit(out);
167
   EmitEnd(out);
168
}
169
//-----------------------------------------------------------------------------
170
int ParameterDeclaration::AllocateParameters(int offset)
171
{
172
const int size = type->GetSize();
173
const char * pname = GetDeclaredName(0);
174
 
175
   if (pname)   Name::SetAutoPos(pname, offset);
176
 
177
   return size;
178
}
179
//-----------------------------------------------------------------------------
180
const char * ParameterDeclaration::GetDeclaredName(int skip)
181
{
182
   for (Declarator * d = type->GetDeclarator(); d; d = d->Tail())
183
       {
184
         const char * n = d->Head()->GetName();
185
         if (n == 0)      continue;
186
         if (skip == 0)   return n;
187
         skip--;
188
       }
189
   return 0;
190
}
191
//-----------------------------------------------------------------------------
192
FunctionDefinition::FunctionDefinition(TypeSpecifier * ds,
193
                                       Declarator * decl,
194
                                       DeclarationList * dl)
195
   : Node("FunctionDefinition"),
196
     fun_declarator(decl),
197
     decl_list(dl),
198
     body(0)
199
{
200
   // these are always present...
201
   //
202
   assert(decl);
203
 
204
   // no type means int
205
   //
206
   if (ds == 0)   ds = new TypeSpecifier(TS_INT);
207
 
208
Declarator * ret_decl = 0;
209
 
210
   // copy decl to ret_decl up to FUN_DECL
211
   //
212
   for (Declarator * d = decl; d; d = d->Tail())
213
            {
214
              DeclItem * di = d->Head();
215
              assert(di);
216
              if (di->GetWhat() == DECL_FUN)   break;
217
              ret_decl = new Declarator(di, ret_decl);
218
            }
219
 
220
const char * fun_name = ::GetDeclaredName(fun_declarator);
221
   assert(fun_name);
222
 
223
   ret_type = new TypeName(ds, ret_decl);
224
 
225
TypeName * fun_type = new TypeName(ds, fun_declarator);
226
   Name::AddLocal(fun_name, fun_type);
227
}
228
//-----------------------------------------------------------------------------
229
void FunctionDefinition::Emit(FILE * out)
230
{
231
   EmitStart(out);
232
 
233
   assert(ret_type);
234
   ret_type->Emit(out);
235
 
236
   assert(fun_declarator);
237
   fun_declarator->Emit(out);
238
 
239
   if (decl_list)        decl_list->Emit(out);
240
 
241
   assert(body);
242
 
243
const char * funname = GetDeclaredName(fun_declarator);
244
   assert(funname);
245
   Backend::new_function(funname);
246
 
247
int ret_size = ret_type->GetSize();
248
   if (ret_size <= 4)   ret_size = 0;   // return value in register
249 7 jsauermann
   ret_size += 2;                       // return address
250 2 jsauermann
 
251
ParameterDeclarationList * pdl = ::GetParameters(fun_declarator);
252
 
253
int offset = ret_size;
254
   for (ParameterDeclarationList * p = pdl; p; p = p->Tail())
255
       {
256
         ParameterDeclaration * pd = p->Head();
257
         assert(pd);
258
         offset += pd->AllocateParameters(offset);
259
       }
260
 
261
   body->Emit(out);
262
 
263 7 jsauermann
   Backend::ret();
264 2 jsauermann
   Name::RemoveAuto();
265
}
266
//-----------------------------------------------------------------------------
267
void InitDeclarator::Emit(FILE * out)
268
{
269
   EmitStart(out);
270
   if (declarator)    declarator->Emit(out);
271
   // don't emit initializer
272
   EmitEnd(out);
273
}
274
//-----------------------------------------------------------------------------
275
const char * InitDeclarator::GetDeclaredName(int skip)
276
{
277
const char * ret = ::GetDeclaredName(declarator);
278
 
279
   assert(ret);
280
   return ret;
281
}
282
//-----------------------------------------------------------------------------
283
int InitDeclarator::EmitAutovars(FILE * out, TypeSpecifier * typespec)
284
{
285
   EmitStart(out);
286
   assert(declarator);
287
   declarator->Emit(out);
288
 
289
   assert(typespec);
290
TypeName type(typespec, declarator);
291
const Specifier spec = typespec->GetType();
292
 
293
   assert(!(spec & SC_TYPEDEF));
294
   assert(!(spec & SC_EXTERN));
295
 
296
const int size    = typespec->GetSize(declarator);
297
const char * name = ::GetDeclaredName(declarator);
298
   assert(name);
299
 
300
   Name::SetAutoPos(name, Backend::GetSP() - size);
301
 
302
const int ret = typespec->GetSize(declarator);
303
   if (ret < 0)
304
      {
305
        type.Print(stderr);
306
        assert(0);
307
      }
308
 
309
   if (initializer)   initializer->InitAutovar(out, type.GetSUW());
310
   else               Backend::push_zero(ret);
311
 
312
   EmitEnd(out);
313
   return ret;
314
}
315
//-----------------------------------------------------------------------------
316
void InitDeclarator::Allocate(FILE * out, TypeSpecifier * typespec)
317
{
318
const Specifier spec = typespec->GetType();
319
 
320
   if (spec & SC_TYPEDEF)   return;
321
 
322
const char * name = ::GetDeclaredName(declarator);
323
   assert(name);
324
 
325
   if (spec & SC_EXTERN)
326
      {
327
        fprintf(out, "\t.EXTERN\tC%s\n", name);
328
      }
329
   else if (spec & SC_STATIC)
330
      {
331
        // forward declaration
332
        fprintf(out, "\t.STATIC\tC%s\n", name);
333
      }
334
   else if (!IsFunction(declarator))
335
      {
336
        fprintf(out, "C%s:\t\t\t; \n", name);
337
        if (initializer)
338
           {
339
             TypeName tn(typespec, declarator);
340
             initializer->EmitValue(out, &tn);
341
           }
342
        else
343
           {
344
             const int size = typespec->GetSize(declarator);
345
             for (int b = 0; b < size; b++)
346
                 fprintf(out, "\t.BYTE\t0\t\t\t; VOID [%d]\r\n", b);
347
           }
348
      }
349
}
350
//-----------------------------------------------------------------------------
351
Declaration::Declaration(TypeSpecifier * ds, InitDeclaratorList * il)
352
   : Node("Declaration"),
353
     base_type(ds),
354
     init_list(il)
355
{
356
   assert(ds);
357
 
358
   for (InitDeclaratorList * i = init_list; i; i = i->Tail())
359
       {
360
         InitDeclarator * id = i->Head();
361
         assert(id);
362
         Declarator * decl = id->GetDeclarator();
363
 
364
         const char * dn = ::GetDeclaredName(decl);
365
         assert(dn);
366
 
367
        assert(base_type);
368
        const Specifier spec = base_type->GetType();
369
 
370
        if (spec & SC_TYPEDEF)
371
           {
372
             const Specifier real_spec = (Specifier)(spec & ~SC_TYPEDEF);
373
             TypeSpecifier * real_type = new TypeSpecifier(real_spec,
374
                                                base_type->GetName(),
375
                                                base_type->GetStructDecl());
376
             Declarator * ret = 0;
377
             for (Declarator * d = decl; d; d = d->Tail())
378
                 {
379
                   DeclItem * di = d->Head();
380
                   assert(di);
381
                   if (di->GetWhat() != DECL_NAME)
382
                      ret = new Declarator(di, ret);
383
                 }
384
 
385
             TypeName * tn = new TypeName(real_type, ret);
386
             TypedefName::Add(dn, tn);
387
           }
388
        else
389
           {
390
             TypeName * tn = new TypeName(base_type, decl);
391
 
392
             if (spec & SC_EXTERN)  Name::AddExtern(dn, tn);
393
             else if (spec & SC_STATIC)  Name::AddStatic(dn, tn);
394
             else                        Name::AddAuto(dn, tn);
395
           }
396
       }
397
}
398
//-----------------------------------------------------------------------------
399
void Declaration::Emit(FILE * out)
400
{
401
   EmitStart(out);
402
   if (base_type)   base_type->Emit(out);
403
   // if (init_list)   init_list->Emit(out);
404
 
405
   Allocate(out);
406
 
407
   EmitEnd(out);
408
   Name::AutoToLocal();
409
}
410
//-----------------------------------------------------------------------------
411
void Declaration::Allocate(FILE * out)
412
{
413
   for (InitDeclaratorList * il = init_list; il; il = il->Tail())
414
       {
415
         InitDeclarator * id = il->Head();
416
         assert(id);
417
         id->Allocate(out, base_type);
418
       }
419
}
420
//-----------------------------------------------------------------------------
421
int Declaration::EmitAutovars(FILE * out)
422
{
423
int ret = 0;
424
 
425
   for (InitDeclaratorList * il = init_list; il; il = il->Tail())
426
       {
427
         InitDeclarator * id = il->Head();
428
         assert(id);
429
         ret += id->EmitAutovars(out, base_type);
430
       }
431
 
432
   return ret;
433
}
434
//-----------------------------------------------------------------------------
435
void Ptr::Emit(FILE * out)
436
{
437
   EmitStart(out);
438
   if (decl_specs)   decl_specs->Emit(out);
439
   EmitEnd(out);
440
}
441
//-----------------------------------------------------------------------------
442
void Initializer::Emit(FILE * out)
443
{
444
   // debug only: must call EmitValue() or EmitAutovars()
445
   EmitStart(out);
446
   if (skalar_value)   skalar_value->Emit(out);
447
   if (array_value)    array_value ->Emit(out);
448
   EmitEnd(out);
449
}
450
//-----------------------------------------------------------------------------
451
void Initializer::EmitValue(FILE * out, TypeName * tn)
452
{
453
   if (tn->IsUnion())
454
      {
455
        int union_size = tn->GetSize();
456
        TypeName * first = tn->FirstUnionMember(union_size);
457
        if (first)   EmitValue(out, first);
458
        return;
459
      }
460
 
461
   if (skalar_value)
462
      {
463
        if (tn->IsArray())   // char x[] = "abc" ?
464
           {
465
             if (tn->GetElementType()->GetSize() != 1)
466
                {
467
                  fprintf(stderr,
468
                          "Initialization of array with skalar or string\n");
469
                  semantic_errors++;
470
                  return;
471
                }
472
 
473
           if (!skalar_value->IsStringConstant())
474
                {
475
                  fprintf(stderr,
476
                          "Initialization of char array with non-string\n");
477
                  semantic_errors++;
478
                  return;
479
                }
480
 
481
           int len = tn->GetSize();
482
           if (len == -1)
483
              {
484
                len = skalar_value->GetSize();   // x[] = "..."
485
                tn->SetArrayLength(len);
486
              }
487
 
488
           StringConstant * sc = skalar_value->GetStringConstant();
489
           assert(sc);
490
           sc->EmitAndRemove(out, len);
491
           return;
492
           }
493
 
494
        skalar_value->EmitInitialization(out, tn->GetSize());
495
        return;
496
      }
497
 
498
   assert(array_value);
499
 
500
   // array or struct...
501
   // check for array
502
   //
503
   if (tn->IsArray())
504
      {
505
        int elements;
506
        Expression * alen = tn->ArrayLength();
507
        if (alen)    // a[len]
508
           {
509
             elements = alen->GetConstantNumericValue();
510
           }
511
        else         // a[]
512
           {
513
             elements = List<Initializer>::Length(array_value);
514
             tn->SetArrayLength(elements);
515
           }
516
 
517
        TypeName * etype = tn->GetElementType();
518
        assert(etype);
519
        const int size = etype->GetSize();
520
        int done = 0;
521
 
522
        for (InitializerList * il = array_value; il; il = il->Tail())
523
            {
524
              if (done == elements)
525
                 {
526
                   fprintf(stderr, "Too many array initializers\n");
527
                   semantic_errors++;
528
                   return;
529
                 }
530
 
531
              Initializer * in = il->Head();
532
              assert(in);
533
              in->EmitValue(out, etype);
534
              done++;
535
            }
536
 
537
        // init unspecified array elements to 0
538
        //
539
        for (int b = done; b < elements; b++)
540
            {
541
              if (size == 1)
542
                 fprintf(out, "\t.BYTE\t0\t\t\t; VOID[%d]\r\n", b);
543
              else if (size == 2)
544
                 fprintf(out, "\t.WORD\t0\t\t\t; VOID[%d]\r\n", b);
545
              else
546
                 {
547
                   for (int i = 0; i < size; i++)
548
                   fprintf(out, "\t.BYTE\t0\t\t\t; VOID[%d]\r\n", b);
549
                 }
550
            }
551
 
552
        return;
553
      }
554
 
555
   // struct...
556
   //
557
   if (!tn->IsStruct())
558
      {
559
        fprintf(stderr, "Initialization of skalar type with array\n");
560
        semantic_errors++;
561
        return;
562
      }
563
 
564
TypeSpecifier * ts = tn->GetTypeSpecifier();
565
   assert(ts);
566
 
567
const char * sname = ts->GetName();
568
   if (sname == 0)
569
      {
570
        fprintf(stderr, "No struct name in struct initializer\n");
571
        semantic_errors++;
572
        return;
573
      }
574
 
575
StructDeclarationList * sdl = StructName::Find(sname);
576
   if (sdl == 0)
577
      {
578
        fprintf(stderr, "No struct %s defined\n", sname);
579
        semantic_errors++;
580
        return;
581
      }
582
 
583
InitializerList * il = array_value;
584
 
585
   for (; sdl; sdl = sdl->Tail())
586
       {
587
         StructDeclaration * sd = sdl->Head();
588
         assert(sd);
589
         TypeSpecifier * ts = sd->GetSpecifier();
590
         assert(ts);
591
 
592
         for (StructDeclaratorList * sr = sd->GetDeclarators(); sr;
593
              sr = sr->Tail())
594
            {
595
              StructDeclarator * sor = sr->Head();
596
              assert(sor);
597
 
598
              Declarator * decl = sor->GetDeclarator();
599
              assert(decl);
600
 
601
              const char * membname = sor->GetMemberName();
602
              if (membname == 0)   membname = "anonymous";
603
 
604
              TypeName type(ts, decl);
605
              if (il == 0)
606
                 {
607
                   const int size = type.GetSize();
608
                   if (size == 1)
609
                      fprintf(out, "\t.BYTE\t0\t\t\t; VOID %s\r\n", membname);
610
                   else if (size == 2)
611
                      fprintf(out, "\t.WORD\t0\t\t\t; VOID %s\r\n", membname);
612
                   else
613
                      {
614
                        for (int i = 0; i < size; i++)
615
                        fprintf(out, "\t.BYTE\t0\t\t\t; VOID %s\r\n", membname);
616
                      }
617
                 }
618
              else
619
                 {
620
                   Initializer * ini = il->Head();
621
                   assert(ini);
622
                   il = il->Tail();
623
                   ini->EmitValue(out, &type);
624
                 }
625
            }
626
       }
627
}
628
//-----------------------------------------------------------------------------
629
int Initializer::InitAutovar(FILE * out, SUW suw)
630
{
631
int ret = 0;
632
 
633
   assert( skalar_value ||  array_value);
634
   assert(!skalar_value || !array_value);
635
 
636
   EmitStart(out);
637
 
638
   if (skalar_value)
639
      {
640
        ret = 1;   if (suw == WO)   ret = 2;
641
 
642
        if (skalar_value->IsNumericConstant())
643
           {
644
             int value = skalar_value->GetConstantNumericValue();
645
             if (value == 0)
646
                {
647
                  Backend::push_zero(ret);
648
                }
649
            else
650
                {
651
                  skalar_value->Emit(out);
652
                  Backend::push_rr(suw);
653
                }
654
           }
655
        else
656
           {
657
             skalar_value->Emit(out);
658
             Backend::push_rr(suw);
659
           }
660
      }
661
   else
662
      {
663
        fprintf(stderr, "TODO: aggregate initializer\r\n");
664
      }
665
 
666
   EmitEnd(out);
667
   return ret;
668
}
669
//-----------------------------------------------------------------------------
670
TypeName * TypeSpecifier::GetMemberType(const char * member)
671
{
672
const int is_union  = spec & TS_UNION;
673
const int is_struct = spec & TS_STRUCT;
674
 
675
   if (!is_union && !is_struct)
676
      {
677
        fprintf(stderr, "access member %s of non-aggregate\n", member);
678
        semantic_errors++;
679
        return 0;
680
      }
681
 
682
StructDeclarationList * sdl = StructName::Find(name);
683
        if (sdl == 0)
684
            {
685
              fprintf(stderr, "No struct %s defined\n", name);
686
              semantic_errors++;
687
              return 0;
688
            }
689
 
690
   for (; sdl ; sdl = sdl->Tail())
691
       {
692
         StructDeclaration * sd = sdl->Head();
693
         assert(sd);
694
         TypeName * st = sd->GetMemberType(name, member);
695
         if (st)   return st;
696
       }
697
 
698
   fprintf(stderr, "aggregate %s has no member %s\n", name, member);
699
   semantic_errors++;
700
   return 0;
701
}
702
//-----------------------------------------------------------------------------
703
int StructDeclaration::Emit(FILE * out, const char * struct_name, int pos,
704
                            bool is_union)
705
{
706
   size = 0;
707
   for (StructDeclaratorList * sl = struct_decl_list; sl ; sl = sl->Tail())
708
       {
709
         StructDeclarator * sd = sl->Head();
710
         assert(sd);
711
 
712
         if (is_union)
713
            {
714
              int tsize = sd->EmitMember(out, struct_name,
715
                                         decl_specifiers, 0, true);
716
              if (size < tsize)   size = tsize;
717
            }
718
         else
719
            {
720
              size += sd->EmitMember(out, struct_name,
721
                                     decl_specifiers, pos + size, false);
722
            }
723
       }
724
 
725
   return size;
726
}
727
//-----------------------------------------------------------------------------
728
TypeName * StructDeclaration::GetMemberType(const char * struct_name,
729
                                                 const char * member)
730
{
731
   for (StructDeclaratorList * sl = struct_decl_list; sl ; sl = sl->Tail())
732
       {
733
         StructDeclarator * sd = sl->Head();
734
         assert(sd);
735
         TypeName * st = sd->GetMemberType(decl_specifiers, member);
736
         if (st)   return st;
737
       }
738
 
739
   return 0;
740
}
741
//-----------------------------------------------------------------------------
742
TypeName * StructDeclaration::FirstUnionMember(int union_size) const
743
{
744
   for (StructDeclaratorList * sl = struct_decl_list; sl ; sl = sl->Tail())
745
       {
746
         StructDeclarator * sd = sl->Head();
747
         assert(sd);
748
         TypeName * st = sd->FirstUnionMember(decl_specifiers, union_size);
749
         if (st)   return st;
750
       }
751
 
752
   return 0;
753
}
754
//-----------------------------------------------------------------------------
755
int StructDeclaration::GetMemberPosition(const char * struct_name,
756
                                         const char * member,
757
                                         bool is_union) const
758
{
759
   if (is_union)   return 0;
760
 
761
   for (StructDeclaratorList * sl = struct_decl_list; sl ; sl = sl->Tail())
762
       {
763
         StructDeclarator * sd = sl->Head();
764
         assert(sd);
765
         int position = sd->GetMemberPosition(member);
766
         if (position >= 0)   return position;
767
       }
768
 
769
   return -1;
770
}
771
//-----------------------------------------------------------------------------
772
TypeName * StructDeclarator::GetMemberType(TypeSpecifier * tspec,
773
                                                const char * member)
774
{
775
   for (Declarator * decl = declarator; decl; decl = decl->Tail())
776
       {
777
         DeclItem * di = decl->Head();
778
         assert(di);
779
         if (di->GetWhat() != DECL_NAME)      continue;
780
         assert(di->GetName());
781
         if (strcmp(member, di->GetName()))   continue;
782
         return new TypeName(tspec, declarator);
783
       }
784
 
785
   return 0;
786
}
787
//-----------------------------------------------------------------------------
788
TypeName * StructDeclarator::FirstUnionMember(TypeSpecifier * tspec,
789
                                              int union_size) const
790
{
791
   for (Declarator * decl = declarator; decl; decl = decl->Tail())
792
       {
793
         TypeName tn(tspec, declarator);
794
         if (tn.GetSize() != union_size)   continue;
795
 
796
         return new TypeName(tspec, declarator);
797
       }
798
 
799
   return 0;
800
}
801
//-----------------------------------------------------------------------------
802
const char * StructDeclarator::GetMemberName() const
803
{
804
   for (Declarator * decl = declarator; decl; decl = decl->Tail())
805
       {
806
         DeclItem * di = decl->Head();
807
         assert(di);
808
         if (di->GetWhat() != DECL_NAME)      continue;
809
         assert(di->GetName());
810
         return di->GetName();
811
       }
812
 
813
   return 0;
814
}
815
//-----------------------------------------------------------------------------
816
int StructDeclarator::GetMemberPosition(const char * member) const
817
{
818
   for (Declarator * decl = declarator; decl; decl = decl->Tail())
819
       {
820
         DeclItem * di = decl->Head();
821
         assert(di);
822
         if (di->GetWhat() != DECL_NAME)      continue;
823
         assert(di->GetName());
824
         if (strcmp(member, di->GetName()))   continue;
825
         return position;
826
       }
827
 
828
   return -1;
829
}
830
//-----------------------------------------------------------------------------
831
bool TypeSpecifier::IsNumericType() const
832
{
833
   if (spec & TS_NUMERIC)      return true;
834
   if (spec != TS_TYPE_NAME)   return false;
835
 
836
   assert(name);
837
TypeName * tname = TypedefName::Find(name);
838
   assert(tname);
839
   return tname->IsNumericType();
840
}
841
//-----------------------------------------------------------------------------
842
int TypeSpecifier::GetBaseSize() const
843
{
844
   if (spec & TS_VOID)      return 0;
845
   if (spec & TS_CHAR)      return 1;
846
   if (spec & TS_SHORT)     return 2;
847
   if (spec & TS_INT)       return 2;
848
   if (spec & TS_LONG)      return 4;
849
   if (spec & TS_FLOAT)     return 4;
850
   if (spec & TS_DOUBLE)    return 8;
851
   if (spec & TS_ENUM)      return 2;
852
   if (spec & (TS_STRUCT | TS_UNION))
853
      {
854
        assert(name);
855
        StructDeclarationList * sdl = StructName::Find(name);
856
        if (sdl == 0)
857
            {
858
              fprintf(stderr, "No struct %s defined\n", name);
859
              semantic_errors++;
860
              return 0;
861
            }
862
 
863
        int size = 0;
864
        for (; sdl; sdl = sdl->Tail())
865
            {
866
               assert(sdl->Head());
867
               int tsize = sdl->Head()->GetSize();
868
               if (spec & TS_UNION)
869
                  {
870
                    if (size < tsize)   size = tsize;
871
                  }
872
               else
873
                  {
874
                    size += tsize;
875
                  }
876
            }
877
         return size;
878
      }
879
 
880
   if (spec & TS_TYPE_NAME)
881
      {
882
        assert(name);
883
        TypeName * tname = TypedefName::Find(name);
884
        assert(tname);
885
        return tname->GetTypeSpecifier()->GetBaseSize();
886
      }
887
 
888
   return 2;    // no type -> int
889
}
890
//-----------------------------------------------------------------------------
891
int TypeSpecifier::GetSize(Declarator * decl) const
892
{
893
int size = -2;
894
 
895
   for (; decl; decl = decl->Tail())
896
       {
897
         const DeclItem * ditem = decl->Head();
898
         assert(ditem);
899
 
900
         switch(ditem->GetWhat())
901
            {
902
              case DECL_FUNPTR:
903
                   size = 2;
904
                   continue;
905
 
906
              case DECL_POINTER:
907
                   size = 2;
908
                   continue;
909
 
910
              case DECL_FUN:
911
                   size = 2;
912
                   continue;
913
 
914
              case DECL_ARRAY:
915
                   if (ditem->GetArraySize())     // type[num]
916
                      {
917
                        if (size == -2)   size = GetBaseSize();
918
                        size *= ditem->GetArraySize()
919
                                     ->GetConstantNumericValue();
920
                      }
921
                   else                           // type[]
922
                      {
923
                        size = -1;
924
                      }
925
                   continue;
926
 
927
              case DECL_NAME:
928
                   continue;   // varname
929
 
930
              default: assert(0 && "BAD what");
931
            }
932
       }
933
 
934
   if (size == -2)   return GetBaseSize();
935
   return size;
936
}
937
//-----------------------------------------------------------------------------
938
int TypeSpecifier::GetFunReturnSize(Declarator * decl) const
939
{
940
   assert(this);
941
   if (decl == 0)
942
      {
943
        fprintf(stderr, "Can't get parameters of (undeclared ?) function\n");
944
        semantic_errors++;
945
        return 0;
946
      }
947
 
948
int base_size = GetBaseSize();
949
 
950
   for (; decl; decl = decl->Tail())
951
       {
952
         DeclItem * di = decl->Head();
953
         assert(di);
954
         const Expression * asize = di->GetArraySize();
955
 
956
         switch(di->GetWhat())
957
            {
958
              case DECL_NAME:
959
                   continue;
960
 
961
              case DECL_FUNPTR:
962
                   base_size = 2;
963
                   continue;
964
 
965
              case DECL_ARRAY:
966
                   if (asize == 0)   // []
967
                      base_size = 2;
968
                   else                      // [const expr]
969
                      base_size *= asize->GetConstantNumericValue();
970
                   continue;
971
 
972
              case DECL_FUN:
973
                   continue;
974
 
975
              case DECL_POINTER:
976
                   base_size = 2;
977
                   continue;
978
 
979
              default: assert(0 && "Bad InitDeclarator::what");
980
            }
981
       }
982
 
983
   return base_size;
984
}
985
//-----------------------------------------------------------------------------
986
TypeSpecifier * TypeSpecifier::operator +(TypeSpecifier & other)
987
{
988
int sc_cnt = 0;
989
int tq_cnt = 0;
990
int si_cnt = 0;
991
int ty_cnt = 0;
992
 
993
   if (spec & SC_MASK)              sc_cnt++;
994
   if (spec & TQ_MASK)              tq_cnt++;
995
   if (spec & TS_SIGN_MASK)         si_cnt++;
996
   if (spec & TS_MASK)              ty_cnt++;
997
 
998
   if (other.spec & SC_MASK)        sc_cnt++;
999
   if (other.spec & TQ_MASK)        tq_cnt++;
1000
   if (other.spec & TS_SIGN_MASK)   si_cnt++;
1001
   if (other.spec & TS_MASK)        ty_cnt++;
1002
 
1003
   if (sc_cnt > 1)
1004
      {
1005
         fprintf(stderr, "Multiple or contradicting storage class (ignored)\n");
1006
         semantic_errors++;
1007
         delete other.self();
1008
         return this;
1009
      }
1010
 
1011
   if (tq_cnt > 1)
1012
      {
1013
         fprintf(stderr, "Multiple or contradicting qualifiers (ignored)\n");
1014
         semantic_errors++;
1015
         delete other.self();
1016
         return this;
1017
      }
1018
 
1019
   if (si_cnt > 1)
1020
      {
1021
         fprintf(stderr,
1022
                 "Multiple or Contradicting signed/unsigned (ignored)\n");
1023
         semantic_errors++;
1024
         delete other.self();
1025
         return this;
1026
      }
1027
 
1028
   if (ty_cnt > 1)
1029
      {
1030
         fprintf(stderr, "Multiple or Contradicting types (ignored)\n");
1031
         semantic_errors++;
1032
         delete other.self();
1033
         return this;
1034
      }
1035
 
1036
   if (other.enum_list)          enum_list        = other.enum_list;
1037
   if (other.name)               name             = other.name;
1038
   if (other.struct_decl_list)   struct_decl_list = other.struct_decl_list;
1039
 
1040
   spec = (Specifier)(spec | other.spec);
1041
   delete other.self();
1042
   return this;
1043
}
1044
//-----------------------------------------------------------------------------
1045
int TypeSpecifier::Print(FILE * out) const
1046
{
1047
int len = 0;
1048
 
1049
   if (spec & SC_TYPEDEF)   len += fprintf(out, "typedef ");
1050
   if (spec & SC_EXTERN)    len += fprintf(out, "extern ");
1051
   if (spec & SC_STATIC)    len += fprintf(out, "static ");
1052
   if (spec & SC_AUTO)      len += fprintf(out, "auto ");
1053
   if (spec & SC_REGISTER)  len += fprintf(out, "register ");
1054
 
1055
   if (spec & TQ_CONST)     len += fprintf(out, "const ");
1056
   if (spec & TQ_VOLATILE)  len += fprintf(out, "volatile ");
1057
 
1058
   if (spec & TS_SIGNED)    len += fprintf(out, "signed ");
1059
   if (spec & TS_UNSIGNED)  len += fprintf(out, "unsigned ");
1060
 
1061
   if (spec & TS_VOID)      len += fprintf(out, "void ");
1062
   if (spec & TS_CHAR)      len += fprintf(out, "char ");
1063
   if (spec & TS_SHORT)     len += fprintf(out, "short ");
1064
   if (spec & TS_INT)       len += fprintf(out, "int ");
1065
   if (spec & TS_LONG)      len += fprintf(out, "long ");
1066
   if (spec & TS_FLOAT)     len += fprintf(out, "float ");
1067
   if (spec & TS_DOUBLE)    len += fprintf(out, "double ");
1068
 
1069
   if (spec & TS_STRUCT)
1070
      {
1071
        assert(name);
1072
        len += fprintf(out, "struct '%s' ", name);
1073
      }
1074
 
1075
   if (spec & TS_UNION)
1076
      {
1077
        assert(name);
1078
        len += fprintf(out, "union '%s' ", name);
1079
      }
1080
 
1081
   if (spec & TS_ENUM)
1082
      {
1083
        if (name)   len += fprintf(out, "enum '%s' ", name);
1084
        else        len += fprintf(out, "anonymous enum ");
1085
      }
1086
 
1087
   if (spec & TS_TYPE_NAME)
1088
      {
1089
        if (name)   len += fprintf(out, "'%s' ", name);
1090
        else        len += fprintf(out, "<user type> ");
1091
      }
1092
 
1093
   return len;
1094
}
1095
//-----------------------------------------------------------------------------
1096
// struct, union, or typedef
1097
//
1098
TypeSpecifier::TypeSpecifier(Specifier sp, const char * n,
1099
                             StructDeclarationList * sdl)
1100
   : Node("TypeSpecifier (struct/union)"),
1101
     spec(sp),
1102
     name(n),
1103
     struct_decl_list(sdl),
1104
     enum_list(0)
1105
{
1106
   if (name == 0)   // anonymous struct or union
1107
      {
1108
        char * cp = new char[20];
1109
        sprintf(cp, "anonymous-%d", anonymous_number++);
1110
        name = cp;
1111
      }
1112
 
1113
   if (struct_decl_list)   StructName::Add(name, struct_decl_list);
1114
}
1115
//-----------------------------------------------------------------------------
1116
void TypeSpecifier::Emit(FILE * out)
1117
{
1118
   EmitStart(out);
1119
   EmitIndent(out);
1120
   fprintf(out, "spec = ");
1121
 
1122
   Print(out);
1123
 
1124
   fprintf(out, "(%X)\n", spec);
1125
 
1126
   if (name)
1127
      {
1128
        EmitIndent(out);
1129
        fprintf(out, "name = %s\n", name);
1130
      }
1131
 
1132
   if (struct_decl_list)
1133
      {
1134
        int pos = 0;
1135
        for (StructDeclarationList * sl = struct_decl_list; sl; sl = sl->Tail())
1136
            {
1137
              assert(sl->Head());
1138
              pos += sl->Head()->Emit(out, name, pos, IsUnion());
1139
            }
1140
      }
1141
 
1142
   if (enum_list)
1143
      {
1144
        if (name)   TypedefName::Add(name, new TypeName(TS_INT));
1145
        enum_list->Emit(out);
1146
      }
1147
   EmitEnd(out);
1148
}
1149
//-----------------------------------------------------------------------------
1150
int Ptr::Print(FILE * out) const
1151
{
1152
   return fprintf(out, "* ");
1153
}
1154
//-----------------------------------------------------------------------------
1155
void DeclItem::SetArraySize(int len)
1156
{
1157
   assert(!array_size);
1158
   assert(len >= 0);
1159
   array_size = new NumericExpression(len);
1160
}
1161
//-----------------------------------------------------------------------------
1162
int DeclItem::Print(FILE * out) const
1163
{
1164
   switch(what)
1165
      {
1166
        case DECL_FUNPTR:    return fprintf(out, "*() ");
1167
        case DECL_ARRAY:     if (!array_size) return fprintf(out, "[] ");
1168
                             return fprintf(out, "[%d] ",
1169
                                    array_size->GetConstantNumericValue());
1170
        case DECL_FUN:       return fprintf(out, "() ");
1171
        case DECL_POINTER:   {
1172
                               assert(pointer);
1173
                               int len = 0;
1174
                               for (Pointer * p = pointer; p; p = p->Tail())
1175
                                   len += p->Head()->Print(out);
1176
                               return len;
1177
                             }
1178
        case DECL_NAME:      assert(name);
1179
                             return fprintf(out, "%s ", name);
1180
      }
1181
 
1182
   assert(0 ** "Bad what");
1183
}
1184
//-----------------------------------------------------------------------------
1185
void DeclItem::Emit(FILE * out)
1186
{
1187
const char * s = "BAD DECL";
1188
 
1189
   EmitStart(out);
1190
   switch(what)
1191
      {
1192
        case DECL_NAME:      s = "DECL_NAME";       break;
1193
        case DECL_FUNPTR:    s = "DECL_FUNPTR";     break;
1194
        case DECL_ARRAY:     s = "DECL_ARRAY";      break;
1195
        case DECL_FUN:       s = "DECL_FUN";        break;
1196
        case DECL_POINTER:   s = "DECL_POINTER";    break;
1197
      }
1198
   EmitIndent(out);
1199
   fprintf(out, "what = %s\r\n", s);
1200
 
1201
   if (name)
1202
      {
1203
        EmitIndent(out);
1204
        fprintf(out, "name = %s\r\n", name);
1205
      }
1206
 
1207
   if (funptr)            funptr->Emit(out);
1208
   // don't emit array_size
1209
   if (fun_params)        fun_params->Emit(out);
1210
   if (fun_identifiers)   fun_identifiers->Emit(out);
1211
   if (pointer)           pointer->Emit(out);
1212
   EmitEnd(out);
1213
}
1214
//-----------------------------------------------------------------------------
1215
TypeName::TypeName(TypeSpecifier * ds, Declarator * ad)
1216
   : Node("TypeName"),
1217
     decl_spec(ds),
1218
     abs_declarator(ad)
1219
{
1220
   assert(ds);
1221
   if (ds->GetType() & TS_TYPE_NAME)
1222
      {
1223
        const char * name = decl_spec->GetName();
1224
        assert(name);
1225
 
1226
        TypeName * def = TypedefName::Find(name);
1227
        assert(def);
1228
 
1229
        // copy type specifier from definition...
1230
        // 
1231
        decl_spec = def->GetTypeSpecifier();
1232
        assert(decl_spec);
1233
 
1234
        // prepend declarator from definition
1235
        //
1236
        for (Declarator * decl = def->abs_declarator->Reverse();
1237
             decl; decl = decl->Tail())
1238
            abs_declarator = new Declarator(decl->Head(), abs_declarator);
1239
      }
1240
}
1241
//-----------------------------------------------------------------------------
1242
TypeName::TypeName(Specifier sp)
1243
   : Node("TypeName (internal)"),
1244
     decl_spec(new TypeSpecifier(sp)),
1245
     abs_declarator(0)
1246
{
1247
   assert((sp & TS_TYPE_NAME) == 0);
1248
}
1249
//-----------------------------------------------------------------------------
1250
void TypeName::Emit(FILE * out)
1251
{
1252
   EmitStart(out);
1253
   if (decl_spec)        decl_spec->Emit(out);
1254
   if (abs_declarator)   abs_declarator->Emit(out);
1255
   EmitEnd(out);
1256
}
1257
//-----------------------------------------------------------------------------
1258
SUW TypeName::GetSUW()
1259
{
1260
   if (IsPointer())   return WO;
1261
 
1262
const int size = GetSize();
1263
 
1264
   if (size == 2)   return WO;
1265
if (size != 1)
1266
{
1267
fprintf(stderr, "---- Size not 1 or 2:\n");
1268
Emit(stderr);
1269
fprintf(stderr, "\n====\n");
1270
*(char*)0 = 0;
1271
fprintf(stderr, "\n====\n");
1272
}
1273
   assert(size == 1);
1274
   if (IsUnsigned())   return UB;
1275
   return SB;
1276
}
1277
//-----------------------------------------------------------------------------
1278
bool TypeName::IsUnsigned() const
1279
{
1280
   if (IsPointer())   return true;
1281
   return decl_spec->IsUnsigned();
1282
}
1283
//-----------------------------------------------------------------------------
1284
bool TypeName::IsNumericType() const
1285
{
1286
   if (!decl_spec->IsNumericType())   return false;
1287
 
1288
   for (Declarator * d = abs_declarator; d; d = d->Tail())
1289
       {
1290
         DeclItem * di = d->Head();
1291
         assert(di);
1292
         switch(di->GetWhat())
1293
            {
1294
              case DECL_NAME:    continue;
1295
              case DECL_FUNPTR:  return false;
1296
              case DECL_ARRAY:   return false;
1297
              case DECL_FUN:     return true;
1298
              case DECL_POINTER: return false;
1299
              default:           assert(0 && "Bad what");
1300
            }
1301
       }
1302
   return true;
1303
}
1304
//-----------------------------------------------------------------------------
1305
TypeName * TypeName::GetFunReturnType()
1306
{
1307
   assert(this);
1308
 
1309
   if (abs_declarator)
1310
      {
1311
        Declarator * ret = 0;
1312
        for (Declarator * decl = abs_declarator; decl; decl = decl->Tail())
1313
            {
1314
              DECL_WHAT what = decl->Head()->GetWhat();
1315
              if (what == DECL_FUNPTR || what == DECL_FUN)
1316
                 return new TypeName(decl_spec, ret->Reverse());
1317
 
1318
              ret = new Declarator(decl->Head(), ret);
1319
            }
1320
      }
1321
 
1322
   Print(stderr);
1323
   fprintf(stderr, " is not a function\n");
1324
   semantic_errors++;
1325
   return new TypeName(TS_INT);
1326
}
1327
//-----------------------------------------------------------------------------
1328
TypeName * TypeName::GetMemberType(const char * member)
1329
{
1330
TypeName * tn = decl_spec->GetMemberType(member);
1331
   if (tn)   return tn;
1332
 
1333
const char * sname = decl_spec->GetName();
1334
   assert(sname);
1335
 
1336
   fprintf(stderr, "aggregate %s has no member %s\n", sname, member);
1337
   semantic_errors++;
1338
   return this;
1339
}
1340
//-----------------------------------------------------------------------------
1341
TypeName * TypeName::FirstUnionMember(int union_size) const
1342
{
1343
   assert(IsUnion());
1344
   assert(decl_spec);
1345
 
1346
const char * uname = decl_spec->GetName();
1347
   assert(uname);
1348
 
1349
StructDeclarationList * sdl = StructName::Find(uname);
1350
   if (sdl == 0)
1351
      {
1352
        fprintf(stderr, "No struct %s defined\n", uname);
1353
        semantic_errors++;
1354
        return 0;
1355
      }
1356
 
1357
   for (; sdl; sdl = sdl->Tail())
1358
       {
1359
         StructDeclaration * sd = sdl->Head();
1360
         assert(sd);
1361
 
1362
         TypeName * ret = sd->FirstUnionMember(union_size);
1363
         if (ret)   return ret;
1364
       }
1365
 
1366
   assert(0);
1367
}
1368
//-----------------------------------------------------------------------------
1369
TypeName * TypeName::AddressOf() const
1370
{
1371
   assert(this);
1372
   assert(decl_spec);
1373
 
1374
Ptr * newptr = new Ptr(0);
1375
 
1376
   if (abs_declarator == 0 || abs_declarator->Head()->GetWhat() != DECL_POINTER)
1377
      {
1378
        Pointer * pointer = new Pointer(newptr, 0);
1379
        DeclItem * di     = new DeclItem(pointer);
1380
        Declarator * decl = new Declarator(di, abs_declarator);
1381
        return new TypeName(decl_spec, decl);
1382
      }
1383
 
1384
DeclItem * hd = abs_declarator->Head();
1385
   assert(hd);
1386
   assert(hd->GetWhat() == DECL_POINTER);
1387
 
1388
Pointer * pointer  = new Pointer(newptr, hd->GetPointer());
1389
DeclItem * di      = new DeclItem(pointer);
1390
Declarator * decl  = new Declarator(di, abs_declarator->Tail());
1391
   return new TypeName(decl_spec, decl);
1392
}
1393
//-----------------------------------------------------------------------------
1394
TypeName * TypeName::ContentOf() const
1395
{
1396
   assert(this);
1397
   assert(decl_spec);
1398
 
1399
   if (!abs_declarator)
1400
      {
1401
        semantic_errors++;
1402
        Print(stderr);
1403
        fprintf(stderr, " is not a pointer\n");
1404
        return new TypeName(new TypeSpecifier(TS_INT), 0);
1405
      }
1406
 
1407
DeclItem * hd = abs_declarator->Head();
1408
   assert(hd);
1409
 
1410
   if (hd->GetWhat() != DECL_POINTER)
1411
      {
1412
        if (IsArray())
1413
           {
1414
             Declarator * ret = 0;
1415
             Declarator * last = 0;
1416
             for (Declarator * d = abs_declarator; d; d = d->Tail())
1417
                 {
1418
                   DeclItem * di = d->Head();
1419
                   assert(di);
1420
                   if (di->GetWhat() == DECL_ARRAY)  last = d;
1421
                 }
1422
 
1423
             assert(last);
1424
             for (Declarator * d = abs_declarator; d; d = d->Tail())
1425
                 {
1426
                   if (d != last)   ret = new Declarator(d->Head(), ret);
1427
                 }
1428
             ret = ret->Reverse();
1429
             return new TypeName(decl_spec, ret);
1430
           }
1431
        semantic_errors++;
1432
        Print(stderr);
1433
        fprintf(stderr, " is not a pointer\n");
1434
        return new TypeName(new TypeSpecifier(TS_INT), 0);
1435
      }
1436
 
1437
Pointer * pointer = hd->GetPointer();
1438
   assert(pointer);
1439
   pointer = pointer->Tail();
1440
   if (!pointer)   return new TypeName(decl_spec, abs_declarator->Tail());
1441
 
1442
DeclItem * new_hd = new DeclItem(pointer);
1443
Declarator * decl = new Declarator(new_hd, abs_declarator->Tail());
1444
   return new TypeName(decl_spec, decl);
1445
}
1446
//-----------------------------------------------------------------------------
1447
int TypeName::Print(FILE * out) const
1448
{
1449
   assert(this);
1450
 
1451
int len = 0;
1452
   if (decl_spec)   len += decl_spec->Print(out);
1453
 
1454
   for (Declarator * dl = abs_declarator; dl; dl = dl->Tail())
1455
       {
1456
         DeclItem * di = dl->Head();
1457
         assert(di);
1458
         len += fprintf(out, " ");
1459
         len += di->Print(out);
1460
      }
1461
 
1462
   return len;
1463
}
1464
//-----------------------------------------------------------------------------
1465
const char * TypeName::GetDeclaredName()
1466
{
1467
const char * ret = 0;
1468
 
1469
   for (Declarator * d = abs_declarator; d; d = d->Tail())
1470
       {
1471
         DeclItem * di = d->Head();
1472
         assert(di);
1473
         const char * n = di->GetName();
1474
         if (n == 0)      continue;
1475
         assert (ret == 0);
1476
         ret = n;
1477
       }
1478
 
1479
   return ret;
1480
}
1481
//-----------------------------------------------------------------------------
1482
const char * GetDeclaredName(Declarator * decl)
1483
{
1484
const char * ret = 0;
1485
int count = 0;
1486
 
1487
   for (; decl; decl = decl->Tail())
1488
       {
1489
         DeclItem * di = decl->Head();
1490
         assert(di);
1491
 
1492
         switch(di->GetWhat())
1493
            {
1494
              case DECL_NAME:    ret = di->GetName();
1495
                                 assert(ret);
1496
                                 count++;
1497
                                 continue;
1498
 
1499
              case DECL_FUNPTR:  assert(!di->GetName());
1500
                                 assert(di->GetFunptr());
1501
                                 ret = GetDeclaredName(di->GetFunptr());
1502
                                 assert(ret);
1503
                                 count++;
1504
                                 continue;
1505
 
1506
              case DECL_ARRAY:
1507
              case DECL_FUN:
1508
              case DECL_POINTER: assert(!di->GetName());
1509
                                 continue;
1510
 
1511
              default:           assert("Bad What");
1512
 
1513
            }
1514
       }
1515
 
1516
   assert(count <= 1);
1517
   return ret;
1518
}
1519
//-----------------------------------------------------------------------------
1520
int TypeName::GetPointeeSize() const
1521
{
1522
   assert(abs_declarator);
1523
   assert(IsPointer() || IsArray());
1524
 
1525
   return ContentOf()->GetSize();
1526
}
1527
//-----------------------------------------------------------------------------
1528
bool TypeName::IsPointer() const
1529
{
1530
   if (abs_declarator)   return ::IsPointer(abs_declarator);
1531
   return false;
1532
}
1533
//-----------------------------------------------------------------------------
1534
bool TypeName::IsArray() const
1535
{
1536
   if (abs_declarator)   return ::IsArray(abs_declarator);
1537
   return false;
1538
}
1539
//-----------------------------------------------------------------------------
1540
TypeName * TypeName::GetElementType() const
1541
{
1542
DeclItem * last = 0;
1543
 
1544
   assert(abs_declarator);
1545
   for (Declarator * decl = abs_declarator; decl; decl = decl->Tail())
1546
       {
1547
         DeclItem * di = decl->Head();
1548
         assert(di);
1549
 
1550
         if (di->GetWhat() == DECL_ARRAY)   last = di;
1551
       }
1552
 
1553
   assert(last);
1554
 
1555
   // copy decl items except name and last...
1556
   //
1557
Declarator * ret = 0;
1558
   for (Declarator * decl = abs_declarator; decl; decl = decl->Tail())
1559
       {
1560
         DeclItem * di = decl->Head();
1561
         if (di == last)   continue;
1562
         if (di->GetWhat() == DECL_NAME)   continue;
1563
 
1564
         ret = new Declarator(di, ret);
1565
       }
1566
 
1567
 
1568
   return new TypeName(decl_spec, ret);
1569
}
1570
//-----------------------------------------------------------------------------
1571
void TypeName::SetArrayLength(int len)
1572
{
1573
   assert(abs_declarator);
1574
   ::SetArrayLength(abs_declarator, len);
1575
}
1576
//-----------------------------------------------------------------------------
1577
Expression * TypeName::ArrayLength() const
1578
{
1579
   if (abs_declarator)   return ::ArrayLength(abs_declarator);
1580
   return 0;
1581
}
1582
//-----------------------------------------------------------------------------
1583
bool TypeName::IsStruct() const
1584
{
1585
   assert(decl_spec);
1586
   if (decl_spec->GetType() & TS_STRUCT)   return true;
1587
   return false;
1588
}
1589
//-----------------------------------------------------------------------------
1590
bool TypeName::IsUnion() const
1591
{
1592
   assert(decl_spec);
1593
   if (decl_spec->GetType() & TS_UNION)   return true;
1594
   return false;
1595
}
1596
//-----------------------------------------------------------------------------
1597
bool IsPointer(Declarator * decl)
1598
{
1599
   assert(decl);
1600
 
1601
DeclItem * di = decl->Head();
1602
   assert(di);
1603
 
1604
   if (di->GetWhat() == DECL_POINTER)   return true;
1605
   return false;
1606
}
1607
//-----------------------------------------------------------------------------
1608
bool IsArray(Declarator * decl)
1609
{
1610
   for (; decl; decl = decl->Tail())
1611
       {
1612
         DeclItem * di = decl->Head();
1613
         assert(di);
1614
 
1615
         if (di->GetWhat() == DECL_ARRAY)   return true;
1616
       }
1617
 
1618
   return false;
1619
}
1620
//-----------------------------------------------------------------------------
1621
Expression * ArrayLength(Declarator * decl)
1622
{
1623
DeclItem * last = 0;
1624
 
1625
   for (; decl; decl = decl->Tail())
1626
       {
1627
         DeclItem * di = decl->Head();
1628
         assert(di);
1629
 
1630
         if (di->GetWhat() == DECL_ARRAY)   last = di;
1631
       }
1632
 
1633
   if (last)   return last->GetArraySize();
1634
   return 0;
1635
}
1636
//-----------------------------------------------------------------------------
1637
void SetArrayLength(Declarator * decl, int len)
1638
{
1639
DeclItem * last = 0;
1640
 
1641
   for (; decl; decl = decl->Tail())
1642
       {
1643
         DeclItem * di = decl->Head();
1644
         assert(di);
1645
 
1646
         if (di->GetWhat() == DECL_ARRAY)   last = di;
1647
       }
1648
 
1649
   assert(last);
1650
   last->SetArraySize(len);
1651
}
1652
//-----------------------------------------------------------------------------
1653
bool IsFunPtr(Declarator * decl)
1654
{
1655
   for (; decl; decl = decl->Tail())
1656
       {
1657
         DeclItem * di = decl->Head();
1658
         assert(di);
1659
 
1660
         if (di->GetWhat() == DECL_FUNPTR)   return true;
1661
       }
1662
 
1663
   return false;
1664
}
1665
//-----------------------------------------------------------------------------
1666
bool IsFunction(Declarator * decl)
1667
{
1668
   for (; decl; decl = decl->Tail())
1669
       {
1670
         DeclItem * di = decl->Head();
1671
         assert(di);
1672
 
1673
         if (di->GetWhat() == DECL_FUN)   return true;
1674
       }
1675
 
1676
   return false;
1677
}
1678
//-----------------------------------------------------------------------------
1679
ParameterDeclarationList * GetParameters(Declarator * decl)
1680
{
1681
   for (; decl; decl = decl->Tail())
1682
       {
1683
         DeclItem * di = decl->Head();
1684
         assert(di);
1685
 
1686
         if (di->GetWhat() == DECL_FUN)      return di->GetParameters();
1687
         if (di->GetWhat() == DECL_FUNPTR)   return di->GetParameters();
1688
       }
1689
 
1690
   fprintf(stderr, "Can't get parameters of undeclared function\n");
1691
   return 0;
1692
}
1693
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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