File: v2cc.cc

package info (click to toggle)
freehdl 0.0.8-2.2
  • links: PTS
  • area: main
  • in suites: buster, sid, stretch
  • size: 8,632 kB
  • ctags: 10,443
  • sloc: cpp: 45,275; sh: 11,405; yacc: 4,206; ansic: 2,026; lex: 486; perl: 430; makefile: 390; tcl: 100
file content (952 lines) | stat: -rw-r--r-- 26,773 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
 
/* VHDL to C++ translator    

   Copyright (C) 1999, 2000 Edwin Naroska.

   V2CC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   V2CC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with V2CC; see the file COPYING.  If not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.

 */

/* This program translates the original VHDL source into C++.
 
   Usage:

   v2cc [-v] [-l lib] file...

       -v             verbose
       -l lib         use lib as the WORK library, default is "."
       -o file        write output to FILE
       --depend=file  write dependency information to FILE

       file...        the design units to translate

*/

using namespace std;

#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#include <fstream>
#include <algorithm>

#include <freehdl/vaul.h>
#include "mapping.h"
#include "v2cc-chunk.h"
#include "v2cc-util.h"


// Declare here for now -- TLD
void emit_includes(string &str);
string sprint_acl(list<string> &acl_list, const string acl_object);
pIIR_LibraryUnit get_library_unit(pIIR_DeclarativeRegion d);


// ******************************************************************************************
// Implementation of member functions declared in v2cc.h
// ******************************************************************************************

// Returns true if range r and the current instance cover the same
// range
bool 
RangeDescriptor::is_equal_to(const RangeDescriptor &r) 
{
  assert(range_attribute == NULL);// Currently not supported
  return (left == r.left) && (right == r.right) && (direction == r.direction);
}

// Convert a RangeDescriptor into corresponding integer values and
// the direction. A boolean vector is returned where each item stores
// whether the corresponding bound could be statically determined
// (true).
StaticRangeDescriptor<lint, IR_Direction>
RangeDescriptor::rangedes_to_lint(RegionStack &rstack)
{
  StaticRangeDescriptor<lint, IR_Direction> range;
  range.valid.resize (3);

  string str;
  bool ok = true;

  // If range is not explicit then return false
  if (!is_explicit_range()) {
    fill(range.valid.begin(), range.valid.end(), false);
    return range;
  }

  // Try to convert the left bounds to integer values
  constant_fold(left, rstack);
  if (valid_folded_value(left))
    range.left = get_folded_value(left).long_value();
  else
    ok = false; // If the expression is not simple then flag an error
  
  range.valid [0] = ok;

  // Store range direction
  range.dir = direction;
  range.valid [1] = true;

  // Try to convert the right bounds to integer values
  ok = true;
  constant_fold(right, rstack);
  if (valid_folded_value(right))
    range.right = get_folded_value(right).long_value();
  else
    ok = false; // If the expression is not simple then flag an error

  range.valid [2] = ok;

  return range;
}


// Convert a RangeDescriptor into corresponding double values and
// the direction. A boolean vector is returned where each item stores
// whether the corresponding bound could be statically determined
// (true).
StaticRangeDescriptor<double, IR_Direction>
RangeDescriptor::rangedes_to_double(RegionStack &rstack)
{
  StaticRangeDescriptor<double, IR_Direction> range;
  range.valid.resize (3);

  string str;
  bool ok = true;
  
  // If range is not explicit then return false
  if (!is_explicit_range()) {
    fill(range.valid.begin(), range.valid.end(), false);
    return range;
  }

  // Try to convert the left bounds to integer values
  constant_fold(left, rstack);
  if (valid_folded_value(left))
    range.left = get_folded_value(left).double_value();
  else
    ok = false; // If the expression is not simple then flag an error
  
  range.valid [0] = ok;

  // Store range direction
  range.dir = direction;
  range.valid [1] = true;

  // Try to convert the right bounds to integer values
  ok = true;
  constant_fold(right, rstack);
  if (valid_folded_value(right))
    range.right = get_folded_value(right).double_value();
  else
    ok = false; // If the expression is not simple then flag an error

  range.valid [2] = ok;

  return range;
}


// Convert a RangeDescriptor into corresponding integer value strings
// and the direction strings. If a bound is not locally static then
// appropriate code is emitted to extract the corresponding values
// from the object. A boolean vector is returned where each item
// stores whether the corresponding bound could be statically
// determined (true).
StaticRangeDescriptor<string, string>
RangeDescriptor::rangedes_to_string(RegionStack &rstack, id_type t)
{
  StaticRangeDescriptor<string, string> range;
  range.valid.resize (3);

  bool ok = true;

  // First, try to constant fold the range attribute expression
  constant_fold_rangedes(rstack);

  if (range_attribute != NULL) {
    // Range is specified via the RANGE or REVERSE_RANGE attribute
    
    bool reverse_range;
    if (range_attribute->is(IR_ATTR_ARRAY_RANGE))
      reverse_range = false;
    else if (range_attribute->is(IR_ATTR_ARRAY_REVERSE_RANGE))
      reverse_range = true;
    else
      assert(false);

    string info_instance_str;
    if (range_attribute->array != NULL) {
      // The range attribute has been applied on an array
      // instance. First, emit code to extract the info instance from
      // the array.
      emit_expr (range_attribute->array, info_instance_str, rstack, t);
      info_instance_str += ".info";
      
    } else {
      // The range attribute has been applied on an array type. Emit
      // code to reference the range corresponding info instance.
      info_instance_str += qid(get_declaration(range_attribute->array_type), rstack, INFO) + "_INFO";
      
    }
    
    // Get index number. Note that the index number must be locally
    // static.
    int index_number = 1;
    if (range_attribute->index != NULL)
      index_number = (int)get_folded_value(range_attribute->index).long_value();
    // Build code to access the array info instance which
    // corresponds with the index_number
    for (int i = 1; i < index_number; i++)
      info_instance_str = "((array_info*)" + info_instance_str + "->element_type)";

    // Print left, right and direction string depending on whether the
    // attribute RANGE or REVERSE_RANGE is used. When printing the
    // bounds check whether the bounds can be determined at compile
    // time.
    if (left != NULL) {
      constant_fold(left, rstack);
      range.valid [0] = emit_expr (left, range.left, rstack, t);
    } else {
      if (reverse_range)
	range.left = info_instance_str + "->right_bound";
      else
	range.left = info_instance_str + "->left_bound";
      range.valid [0] = false;
    }

    if (right != NULL && left != NULL) {
      range.dir = direction == IR_DIRECTION_UP? "to" : "downto";
      range.valid [1] = true;
    } else {
      if (reverse_range)
	range.dir =  "(" + info_instance_str + "->index_direction==to?downto:to)";
      else
	range.dir = info_instance_str + "->index_direction";
      range.valid [1] = false;
    }

    if (right != NULL) {
      constant_fold(right, rstack);
      range.valid [2] = emit_expr (right, range.right, rstack, t);
    } else {
      if (reverse_range)
	range.right = info_instance_str + "->left_bound";
      else
	range.right = info_instance_str + "->right_bound";
      range.valid [2] = false;
    }

  } else {
    // Range is explicit defined

    // Convert the left bounds to integer values
    constant_fold(left, rstack);
    range.valid [0] = emit_expr (left, range.left, rstack, t);
    
    // Store range direction
    range.dir = direction == IR_DIRECTION_UP? "to" : "downto";
    range.valid [1] = true;
    
    constant_fold(right, rstack);
    range.valid [2] = emit_expr (right, range.right, rstack, t);
  }

  return range;
}



// Same as previos method. However, string are generated according
// to CDFG rules.
StaticRangeDescriptor<string, string>
RangeDescriptor::cdfg_rangedes_to_string(RegionStack &rstack, id_type t)
{
  StaticRangeDescriptor<string, string> range;
  range.valid.resize (3);
  bool ok = true;

  // First, try to constant fold the range attribute expression
  constant_fold_rangedes(rstack);

  if (range_attribute != NULL) {
    // Range is specified via the RANGE or REVERSE_RANGE attribute
    
    bool reverse_range;
    if (range_attribute->is(IR_ATTR_ARRAY_RANGE))
      reverse_range = false;
    else if (range_attribute->is(IR_ATTR_ARRAY_REVERSE_RANGE))
      reverse_range = true;
    else
      assert(false);

    string info_instance_str;
    if (range_attribute->array != NULL) {
      // The range attribute has been applied on an array
      // instance. First, emit code to extract the info instance from
      // the array.
      cdfg_emit_expr (range_attribute->array, info_instance_str, rstack, t);
      
    } else {
      // The range attribute has been applied on an array type. Emit
      // code to reference the range corresponding info instance.
      info_instance_str = 
	get_escaped_string(get_long_name(get_declaration(range_attribute->array_type)));
      
    }
    
    // Get index number. Note that the index number must be locally
    // static.
    int index_number = 1;
    if (range_attribute->index != NULL)
      index_number = (int)get_folded_value(range_attribute->index).long_value();
    string index_number_string = to_string(index_number);

    // Print left, right and direction string depending on whether the
    // attribute RANGE or REVERSE_RANGE is used. When printing the
    // bounds check whether the bounds can be determined directly.
    if (left != NULL) {
      constant_fold(left, rstack);
      range.valid [0] = cdfg_emit_expr (left, range.left, rstack, t);
    } else {
      if (reverse_range)
	range.left = "(create-array-attribute-call right " + info_instance_str + " " + index_number_string + ")";
      else
	range.left = "(create-array-attribute-call left " + info_instance_str + " " + index_number_string + ")";
      range.valid [0] = false;
    }

    if (right != NULL && left != NULL) {
      range.dir = direction == IR_DIRECTION_UP? "to" : "downto";
      range.valid [1] = true;
    } else {
      if (reverse_range)
	range.dir = "(create-array-attribute-call-reverse direction " + info_instance_str + " " + index_number_string + ")";
      else
	range.dir = "(create-array-attribute-call direction " + info_instance_str + " " + index_number_string + ")";
      range.valid [1] = false;
    }

    if (right != NULL) {
      constant_fold(right, rstack);
      range.valid [2] = cdfg_emit_expr (right, range.right, rstack, t);
    } else {
      if (reverse_range)
	range.right = "(create-array-attribute-call left " + info_instance_str + " " + index_number_string + ")";
      else
	range.right = "(create-array-attribute-call right " + info_instance_str + " " + index_number_string + ")";
      range.valid [2] = false;
    }

  } else {
    // Range is explicit defined

    // Convert the left bounds to integer values
    constant_fold(left, rstack);
    range.valid [0] = cdfg_emit_expr (left, range.left, rstack, t);
    
    // Store range direction
    range.dir = direction == IR_DIRECTION_UP? "to" : "downto";
    range.valid [1] = true;
    
    constant_fold(right, rstack);
    range.valid [2] = cdfg_emit_expr (right, range.right, rstack, t);
  }

  return range;
}



int
RangeDescriptor::constant_fold_rangedes(RegionStack &rstack)
{
  int error_count = 0;

  if (range_attribute != NULL) {
    vector<RangeDescriptor> range_des_vec;

    if (range_attribute->array != NULL) {
      // There is nothing to do if the array subtype is an
      // unconstrained array type
      if (!is_constrained_array_type(range_attribute->array->subtype))
	return 0;
      // Get range descriptors from array object
      range_des_vec = get_discrete_range(range_attribute->array->subtype, rstack, IR_NOT_STATIC);
    } else if (range_attribute->array_type != NULL)
      // Get range descriptors from array type
      range_des_vec = get_discrete_range(range_attribute->array_type, rstack, IR_NOT_STATIC);
    else
      assert(false);
  
    // Constant fold corresponding range descriptor
    int dim_number = 1;
    if (range_attribute->index) {
      constant_fold(range_attribute->index, rstack);
      assert(valid_folded_value(range_attribute->index));
      dim_number = folded_value(range_attribute->index).long_value();
    }
    RangeDescriptor &range_des = range_des_vec[dim_number - 1];
    error_count += range_des.constant_fold_rangedes(rstack);
  
    if (range_des.range_attribute != NULL)
      return error_count;

    if (range_attribute->is(IR_ATTR_ARRAY_RANGE)) {
      left = range_des.left;
      direction = range_des.direction;
      right = range_des.right;
    } else if (range_attribute->is(IR_ATTR_ARRAY_REVERSE_RANGE)) {
      left = range_des.right;
      direction = range_des.direction == IR_DIRECTION_UP? IR_DIRECTION_DOWN : IR_DIRECTION_UP;
      right = range_des.left;
    } else
      assert(false);
  }

  if (left != NULL)
    if (left->is(IR_EXPRESSION))
      error_count += constant_fold(pIIR_Expression(left), rstack);
    else
      error_count += constant_fold(pIIR_Literal(left), rstack);

  if (right != NULL)
    if (right->is(IR_EXPRESSION))
      error_count += constant_fold(pIIR_Expression(right), rstack);
    else
      error_count += constant_fold(pIIR_Literal(right), rstack);

  return error_count;
}



/* Exit the program with a usage message.  */
void
usage ()
{
  fprintf (stderr, "usage: %s [-v] [-l lib] [-L libdir] file...\n", vaul_application_name);
  exit (1);
}

vaul_parser_options parser_options;

bool try_vhdl_source (char *fn)
{
  for (char *cp = fn; *cp; cp++)
    *cp = tolower(*cp);
  return access (fn, R_OK) == 0;
}

char *
find_vhdl_source (char *l, char *n)
{
  char *fn = vaul_aprintf ("../libvaul/vlib/%s/%s.vhd", l, n);
  if (try_vhdl_source (fn))
    return fn;
  free (fn);
  fn = vaul_aprintf ("../libvaul/vlib/%s/%s.vhdl", l, n);
  try_vhdl_source (fn);
  return fn;
}

mypool::mypool () {
  mapper = make_v2cc_mapper ();
  dependencies_file = NULL;
}

vaul_design_unit *
mypool::get (char *l, char *n)
{
  vaul_design_unit *du = vaul_pool::get (l, n);
  if (du == NULL)
    {
      char *fn = mapper->find_design_file (l, n);

      if (fn == NULL)
	return NULL;

      if (codegen_options.get_verbose ())
	fprintf (stderr, "reading %s.%s from %s\n", l, n, fn);

      // We are only interested in packages and entities, so we
      // instruct the parser to skip over the rest.
      vaul_parser_options opts = parser_options;
      opts.set_skip_bodies (true);
      vaul_design_file df(fn, NULL, opts);
      // Skip "no such file" errors without verbosity set.
      if (df.is_error ()) {
	if (codegen_options.get_verbose ())
	  df.print_err (fn);
	df.clear_error();
      }

      begin_session (l);
      while (vaul_design_unit * du = df.read_design_unit (this))
	{
	  insert (du);
	  if (du->is_error ())
	    du->print_err (fn);
	  du->release ();
	}
      if (df.is_error ())
	df.print_err (fn);
      end_session ();

      du = vaul_pool::get (l, n);

      if (dependencies_file && du && !du->is_error ())
	fprintf (dependencies_file, " %s", fn);

      delete[] fn;
    }

  return du;
}

mypool vaul;

#ifndef HAVE_GETOPT_H
struct option {
  const char *name;
  int has_arg;
  int *flag;
  int val;
};

#define no_argument       0
#define required_argument 1
#define optional_argument 2

extern "C" int getopt_long (int argc, char * const argv[],
			    const char *optstring,
			    const struct option *longopts, int *longindex);
#endif /* HAVE_GETOPT_H */

extern int optind, opterr;
extern char *optarg;

/* Parse FILE and output translation of all contained units to
   strings. Return true on success, false otherwise.
*/
bool emit (vaul_pool *pool, char *file, string &str, string &cdfg_str, string &main_cc_str);

void init_v2cc_chunk ();

bool dry_run = false;

struct option long_options[] = {
  { "relaxed-component-visibility", 0, 0, 0 },
  { "depend", required_argument, 0, 0 },
  { 0, 0, 0, 0 }
};

int
main (int argc, char *argv[])
{
  int opt, option_index;
  const char *libname = "work";
  char *generated_cc_file_name = NULL;
  char *main_cc_file_name = NULL;
  
  vaul_application_name = "v2cc";

  opterr = 0;
  
  while ((opt=getopt_long (argc, argv, "vnl:L:RgDo:m:",
			   long_options, &option_index)) != -1)
    {
      switch (opt)
	{
	case 0:
	  {
	    switch (option_index)
	      {
	      case 0:  // allow_invisible_default_bindings_from_work
		parser_options.
		  set_allow_invisible_default_bindings_from_work (true);
		break;
	      case 1:  // depend
		{
		  FILE *depf = fopen (optarg, "w");
		  if (depf == NULL)
		    {
		      perror (optarg);
		      exit (1);
		    }
		  vaul.dependencies_file = depf;
		}
		break;
	      }
	  }
	  break;
	case 'v':
	  codegen_options.set_verbose (true);
	  parser_options.set_debug (true);
	  parser_options.set_fullnames (true);
	  tree_set_verbose (true);
	  break;
	case 'l':
	  libname =  optarg;
	  break;
	case 'L':
	  vaul.mapper->add_libdir (optarg);
	  break;
	case 'm':
	  codegen_options.set_emit_main_cc_code (true);
	  codegen_options.set_main_cc_filename (optarg);
	  break;
	case 'n':
	  dry_run = true;
	  break;
	case 'R':
	  codegen_options.set_emit_register_code (true);
	  break;
	case 'g':
	  codegen_options.set_emit_debug_code (true);
	  break;
	case 'D':
	  codegen_options.set_emit_cdfg_code (true);
	  break;
	case 'o':
	  generated_cc_file_name = strdup (optarg);
	  break;
	case '?':
	  usage ();
	  break;
	}
    }
  
  vaul.mapper->add_default_libdirs ();

  if (optind >= argc || argc-1 > optind)
    usage ();
  
  init_v2cc_chunk ();

  if (vaul.dependencies_file)
    fprintf (vaul.dependencies_file, "%s:", generated_cc_file_name);

  bool success = true;
  vaul.begin_session ((char*)libname);
  string str, cdfg_str, main_cc_str;
  vector<string> source_file_names; // Stores vhdl source files names
  emit_includes (str);					// #includes for sim
  while (optind < argc)
    {
      if (vaul.dependencies_file)
	fprintf (vaul.dependencies_file, " %s", argv[optind]);
      source_file_names.push_back (string (argv [optind]));
      success = emit (&vaul, argv[optind], str, cdfg_str, main_cc_str) && success;
      optind++;
    }

  if (success)
    {
      if (generated_cc_file_name != NULL) {
	ofstream outfile(generated_cc_file_name);
	outfile << str;
	outfile.close();
      } else {
	cout << str;
	cout.flush();
      }

      // If CDFG code shall be emitted ...
      if (codegen_options.get_emit_cdfg_code ()) 
	{
	  ofstream cdfg_file;
	  // Generate file name for CDFG code. Usually, the .vhdl
	  // extension is replaced by .cdfg.lsp
	  string file_name = source_file_names.front ();
	  file_name.erase(file_name.rfind('.'));
	  // If the file did not have any extension then use the first
	  // entire file name as base for the output file name
	  if (file_name == "")
	    file_name = source_file_names.front ();
	  file_name += ".cdfg.lsp";
	  cdfg_file.open(file_name.c_str());

	  // Write string to file
	  cdfg_file << cdfg_str;
	  cdfg_file.close(); // Close CDFG file
	}

      // If the main routine shall be emitted, then...
      if (codegen_options.get_emit_main_cc_code () &&
	  codegen_options.get_main_cc_filename () != "")
	{
	  ofstream main_cc_file;
	  main_cc_file.open (codegen_options.get_main_cc_filename ().c_str());

	  // Write string to file
	  main_cc_file << main_cc_str;
	  main_cc_file.close(); // Close CDFG file
	}
    }

  vaul.end_session ();

  if (vaul.dependencies_file)
    {
      fprintf (vaul.dependencies_file, "\n");
      fclose (vaul.dependencies_file);
      vaul.dependencies_file = NULL;
    }

  return success? 0 : 1;
}


bool
emit (vaul_pool *pool, char *file, string &str, string &cdfg_str, string &main_cc_str)
{
  vaul_design_file df(file, NULL, parser_options);

  // Create a root node to store global code. Note that it is
  // important to have an SINGLE root node for all designs stored in
  // the design file in order to keep track of global definitions
  // common to all current designs.
  pIIR_DeclarativeRegion root_node = new IIR_DeclarativeRegion(NULL, NULL, NULL, NULL, 0, NULL, NULL);
  // Protect node so that it it is not removed by the garbage
  // collector
  tree_protect(root_node);

  bool success = true;

  // Vector to store all design units that are found in the source
  // file.
  vector<vaul_design_unit *> du_vec;

  // First, parse *all* design units. This is done in order to be able
  // to find out which design units are dublicate and shall not be
  // emitted.
  while (vaul_design_unit *du = df.read_design_unit (pool))
    {
      pool->insert (du); 
      if (du->is_error ())
	{
	  du->print_err (file);
	  success = false;
	}
      du_vec.push_back (du);
    }

  // Check for duplicate design units. Note that it is legal in VHDL
  // to redefine a enitity. In this case, the last definition should
  // overide any previous ones... First, generate an id string for
  // each library unit.
  vector<string> ids (du_vec.size ());
  for (unsigned int i = 0; i < du_vec.size (); i++)
    {
      RegionStack rstack;
      rstack.push(root_node);
      // Generate a unique id string from 
      ids [i] = qid (du_vec [i]->get_tree (), rstack, id_type ());
    }
  // Finally, search for dublicates and mark all dublicates with the
  // exception of the last one.
  for (int i = du_vec.size () - 1; i >= 1; i--)
    {
      for (int j = i - 1; j >= 0; j--)
	if (ids [j] == ids [i])
	  {
	    codegen_error.info("%:warning: definition of library unit %n is overwritten by same named unit defined in line %;.", 
			       du_vec [j]->get_tree (), du_vec [j]->get_tree (),
			       du_vec [i]->get_tree ());
	    generate_code (du_vec [j]->get_tree ()) = false;
	  }
    }
  

  if (success && !dry_run)
    for (unsigned int i = 0; i < du_vec.size (); i++)
      {
	vaul_design_unit *du = du_vec [i];

	if (codegen_options.get_verbose ())
	  fprintf (stderr, "emitting %s/%s\n",
		   du->get_library (), du->get_name ());

	RegionStack rstack;
	// Remove declarations from root node and push it on the
	// context stack
	root_node->declarations = NULL;
	extended_declarations(root_node) = NULL;
	rstack.push(root_node);
	
	// Set prefix name for internal variables
	set_internal_prefix_start(du->get_tree(), rstack);

	// Check VHDL code and prepare code for code generation!
	pIIR_LibraryUnit lu = du->get_tree();
	if (explore_and_check(lu, rstack, true))
	  success = false;

	// Generate code if no error occured and if code shall be
	// generated for this library unit.
	if (success && generate_code (lu))
	  {
	    string code;
	    if (codegen_options.get_emit_debug_code ())
	      code = "#line 1000000 __FILE__\n";
	    
	    //if (lu->declarations != NULL)
	      emit_decl (lu, code, rstack, 0);
	    
	    str += code;
	  }

	// If CDFG code shall be emitted ...
	if (success && codegen_options.get_emit_cdfg_code ()) 
	  {
	    // Call cdfg_emit_impl to print CDFG code into a string
	    cdfg_emit_impl(lu, cdfg_str, rstack, 0);
	  }
	
	// If main code shall be emitted ...
	if (success && codegen_options.get_emit_main_cc_code ()) 
	  {
	    main_cc_str = ""; // Only the last emitted main code is
	    // needed. Hence, clear string before
	    // calling emit_main.
	    emit_main (lu, main_cc_str, rstack, 0);
	  }
	
	// Push context from stack
	rstack.pop();
	
	if (codegen_error.n_errors)
	  exit(1);
      }

  for (unsigned int i = 0; i < du_vec.size (); i++)
    du_vec [i]->release();
  
  // Allow garbage collector to destroy root node
  tree_unprotect(root_node);

  return success;
}




/*
 * Utility
 */

void
emit_includes(string &str)
{
  str += string ("#include <freehdl/kernel.h>\n") +
    string ("#include <freehdl/std.h>\n\n\n") 
    // + 
    //string ("#ifdef CC_FILE_NAME\n") +
    //string ("#undef CC_FILE_NAME\n") +
    //string ("#endif\n") +
    //string ("#define CC_FILE_NAME __FILE__\n")
    ;
}


// ******************************************************************************************
// Name: get_library_unit
//
// Description: returns library unit a declarative region belongs to
//
// Parameter: pIIR_DeclarativeRegion d = declarative region
// 
// Return value: library unit
//
// ******************************************************************************************

pIIR_LibraryUnit
get_library_unit(pIIR_DeclarativeRegion d)
{
  while (d != NULL && !d->is(IR_LIBRARY_UNIT)) {
    d = d->declarative_region;
  }
    
  return (pIIR_LibraryUnit)d;
}

// returns name of the library
char *
get_lib_name(pIIR_DeclarativeRegion r)
{
  pIIR_LibraryUnit lu = get_library_unit(r); 

  return lu->library_name->text.to_chars();
}


// ******************************************************************************************
// Name: m_get_operator_type (generic function)
//
// Description: returns type of an operator. Possible values are:
//   - NO_OP if the function does not denote an operator
//   - STD_OP if the funcion denotes an predefined VHDL operator
//   - BASIC_OP if the function denotes an operator defined in an 
//           IEEE library (e.g. numeric_std)
//   - USER_OP an user defined operator
//
// Parameter: pIIR_FunctionDeclaration f = pointer to function declaration
// 
// Return value: op_type
//
// ******************************************************************************************

op_type
m_get_operator_type(pIIR_ProcedureDeclaration pd)
{
  return NO_OP;
}

op_type
m_get_operator_type(pIIR_FunctionDeclaration fd)
{
  char *function_str = fd->declarator->text.to_chars(); // Get function name

  if (function_str[0] != '"') // Is function not an operator?
    return NO_OP;

  char *libname = get_lib_name(fd->declarative_region);

  if (!strcmp(libname, "std") ||
      fd->is(IR_PREDEFINED_FUNCTION_DECLARATION))
    // Function is defined in library "std" or a predefined VHDL
    // operator
    return STD_OP;
  else if (!strcmp(libname, "ieee"))
    // Function is defined in library "ieee"
    return BASIC_OP;
  else
    return USER_OP;
}