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

Subversion Repositories c16

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

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

powered by: WebSVN 2.1.0

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