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

Subversion Repositories c16

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

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

powered by: WebSVN 2.1.0

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