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

Subversion Repositories c16

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

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

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

powered by: WebSVN 2.1.0

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