aboutsummaryrefslogtreecommitdiff
path: root/gcc/python/py-dot-pass-genericify.c
blob: 671b28605c00d748a94ee15c4cf705cbf9257443 (plain)
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
/* This file is part of GCC.

   GCC 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 3, or (at your option) any later
   version.

   GCC 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 GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "ansidecl.h"
#include "coretypes.h"
#include "tm.h"
#include "opts.h"
#include "tree.h"
#include "tree-iterator.h"
#include "tree-pass.h"
#include "gimple.h"
#include "toplev.h"
#include "debug.h"
#include "options.h"
#include "flags.h"
#include "convert.h"
#include "diagnostic-core.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "target.h"
#include "cgraph.h"

#include <gmp.h>
#include <mpfr.h>
#include <vec.h>
#include <hashtab.h>

#include "gpython.h"
#include "py-il-dot.h"
#include "py-il-tree.h"
#include "py-vec.h"
#include "py-runtime.h"

static tree gpy_dot_pass_genericify_toplevl_functor_decl (gpy_dot_tree_t *,
							  VEC(gpy_context_t, gc) *);
static VEC(tree,gc) * gpy_dot_pass_genericify_toplevl_class_decl (gpy_dot_tree_t *,
								  VEC(gpy_context_t, gc) *,
								  gpy_hash_tab_t * );
static tree gpy_dot_pass_genericify_class_method_attrib (gpy_dot_tree_t *, const char *,
							 VEC(gpy_context_t, gc) *);

static void gpy_dot_pass_genericify_setup_runtime_globls (gpy_hash_tab_t * const);
static void gpy_dot_pass_genericify_create_offsets_globl_context (tree, tree *,
								  VEC(gpy_context_t,gc) *);
static tree gpy_dot_pass_genericify_scalar (gpy_dot_tree_t *, tree *);
static tree gpy_dot_pass_lower_expr (gpy_dot_tree_t *, tree *, VEC(gpy_context_t,gc) *);

char * gpy_dot_pass_genericify_gen_concat (const char * s1,
					   const char * s2)
{
  size_t s1len = strlen (s1);
  size_t s2len = strlen (s2);
  size_t tlen = s1len + s2len;
  
  char buffer[tlen+3]; 
  char * p;
  for (p = buffer; *s1 != '\0'; ++s1)
    {
      *p = *s1;
      ++p;
    }
  *p = '.';
  p++;
  for (; *s2 != '\0'; ++s2)
    {
      *p = *s2;
      ++p;
    }
  *p = '\0';

  return xstrdup (buffer);
}

static
tree gpy_dot_pass_genericify_get_module_type (const char * s, 
					      gpy_hash_tab_t * modules)
{
  tree retval = error_mark_node;

  gpy_hashval_t h = gpy_dd_hash_string (s);
  gpy_hash_entry_t * e = gpy_dd_hash_lookup_table (modules, h);
  if (e)
    {
      if (e->data)
        retval = (tree) e->data;
    }

  return retval;
}

static
void gpy_dot_pass_genericify_setup_runtime_globls (gpy_hash_tab_t * context)
{
  tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
			  get_identifier (GPY_RR_globl_stack),
			  gpy_unsigned_char_ptr);
  TREE_STATIC (decl) = 0;
  TREE_PUBLIC (decl) = 1;
  TREE_USED (decl) = 1;
  DECL_ARTIFICIAL (decl) = 1;
  DECL_EXTERNAL (decl) = 1;

  gcc_assert (!gpy_dd_hash_insert (gpy_dd_hash_string (GPY_RR_globl_stack),
				   decl, context)
	      );

  decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
		     get_identifier (GPY_RR_globl_stack_pointer),
		     gpy_object_type_ptr_ptr);
  TREE_STATIC (decl) = 0;
  TREE_PUBLIC (decl) = 1;
  TREE_USED (decl) = 1;
  DECL_ARTIFICIAL (decl) = 1;
  DECL_EXTERNAL (decl) = 1;

  gcc_assert (!gpy_dd_hash_insert (gpy_dd_hash_string (GPY_RR_globl_stack_pointer),
				   decl, context)
	      );
}

static
void gpy_dot_pass_genericify_create_offsets_globl_context (tree type, tree * cblock,
							   VEC(gpy_context_t, gc) * context)
{
  gpy_context_t globls = VEC_index (gpy_context_t, context, 0);
  gpy_context_t globls_symbols = VEC_index (gpy_context_t, context, 1);
  gpy_hash_entry_t * e = gpy_dd_hash_lookup_table (globls,
						   gpy_dd_hash_string (GPY_RR_globl_stack_pointer));
  gcc_assert (e);
  tree stack_pointer = (tree)e->data;

  int offset = 0, field_count = 0;
  tree field;
  for (field = TYPE_FIELDS (type); field != NULL_TREE;
       field = DECL_CHAIN (field)
       )
    {
      gcc_assert (TREE_CODE (field) == FIELD_DECL);
      const char * ident = IDENTIFIER_POINTER (DECL_NAME (field));
      debug ("calculating globl stack offset for <%s> from type <%s>!\n",
	     ident, IDENTIFIER_POINTER (TYPE_NAME (type)));

      tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (field));
      tree offs = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR, sizetype,
				   build_int_cst (sizetype, offset),
				   element_size);
      tree addr = fold_build2_loc (UNKNOWN_LOCATION, POINTER_PLUS_EXPR,
				   TREE_TYPE (stack_pointer),
				   stack_pointer, offs);
      gcc_assert (!gpy_dd_hash_insert (gpy_dd_hash_string (ident), addr, globls_symbols));
      offset--;
      field_count++;
    }
  append_to_statement_list (GPY_RR_extend_globl_stack (build_int_cst (integer_type_node,
								      field_count)),
			    cblock);
}


static
void gpy_dot_pass_genericify_class_type (tree type, tree self,
					 gpy_context_t context)
{
  const char * class_name = IDENTIFIER_POINTER (TYPE_NAME (type));
  debug ("generating toplevel addressing to class <%s>!\n", class_name);

  tree field = NULL_TREE;
  for (field = TYPE_FIELDS (type); field != NULL_TREE;
       field = DECL_CHAIN (field))
    {
      const char * ident = IDENTIFIER_POINTER (DECL_NAME (field));
      debug ("generating refernce for field <%s>!\n", ident);

      tree ref = build3 (COMPONENT_REF, TREE_TYPE (field),
			 build_fold_indirect_ref (self),
			 field, NULL_TREE);
      gcc_assert (!gpy_dd_hash_insert (gpy_dd_hash_string (ident), ref,
				       context));
    }
}

static
void gpy_dot_pass_genericify_arguments_to_context (gpy_context_t context,
						   gpy_dot_tree_t * parameters,
						   tree argdecl, tree * block)
{
  gpy_dot_tree_t *pnode = parameters;
  if (pnode)
    {
      int offset = 0;
      for (; pnode != NULL_DOT; pnode = DOT_CHAIN (pnode))
	{
	  const char * parmid = DOT_IDENTIFIER_POINTER (pnode);
	  debug ("folding parameter <%s>!\n", parmid);

	  tree vardecl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (parmid),
				     gpy_object_type_ptr);

	  tree element_size = TYPE_SIZE_UNIT (gpy_object_type_ptr_ptr);
	  tree offs = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR, sizetype,
				       build_int_cst (sizetype, offset),
				       element_size);
	  tree addr = fold_build2_loc (UNKNOWN_LOCATION, POINTER_PLUS_EXPR,
				       gpy_object_type_ptr_ptr,
				       argdecl, offs);
	  
	  append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr,
					    vardecl, 
					    build_fold_indirect_ref (addr)),
				    block);
	  gcc_assert (!gpy_dd_hash_insert (gpy_dd_hash_string (parmid), vardecl,
					   context));
	  offset++;
	}
    }
}

static
tree gpy_dot_pass_genericify_find_addr (const char * id,
					const char * parent_ident,
					VEC(tree,gc) * decls)
{
  bool found = false;
  tree retval = null_pointer_node;

  tree ident = GPY_dot_pass_genericify_gen_concat_identifier (parent_ident, id);
  const char * search = IDENTIFIER_POINTER (ident);

  int idx;
  tree decl = NULL_TREE;
  for (idx = 0; VEC_iterate (tree, decls, idx, decl); ++idx)
    {
      tree decl_name = DECL_NAME (decl);
      if (!strcmp (search, IDENTIFIER_POINTER (decl_name)))
	{
	  found = true;
	  break;
	}
    }
  if (found)
    retval = build_fold_addr_expr (decl);
  return retval;
}

static
void gpy_dot_pass_genericify_walk_class (tree * block, tree type,
					 tree decl,
					 VEC(tree,gc) * ldecls)
{
  const char * type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
  VEC(tree,gc) * attribs = VEC_alloc (tree,gc,0);

  tree field = NULL_TREE;
  int offset = 0;
  for (field = TYPE_FIELDS (type); field != NULL_TREE;
       field = DECL_CHAIN (field))
    {
      const char * ident = IDENTIFIER_POINTER (DECL_NAME (field));

      tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (field));
      tree offs = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR, sizetype,
				   build_int_cst (sizetype, offset),
				   element_size);
      tree str = gpy_dot_type_const_string_tree (ident);
      tree a = GPY_RR_fold_attrib (build_fold_addr_expr (str),
				   gpy_dot_pass_genericify_find_addr (ident, type_name, ldecls),
				   offs);
      VEC_safe_push (tree, gc, attribs, a);
      offset++;
    }

  VEC(tree,gc) * args = VEC_alloc (tree,gc,0);
  VEC_safe_push (tree, gc, args, build_int_cst (integer_type_node, VEC_length (tree, attribs)));
  GPY_VEC_stmts_append (tree, args, attribs);

  tree attribs_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("C"),
				  gpy_attrib_type_ptr_ptr);
  append_to_statement_list (build2 (MODIFY_EXPR, gpy_attrib_type_ptr_ptr,
				    attribs_decl,
				    GPY_RR_fold_attrib_list (args)),
			    block);
  tree class_str = gpy_dot_type_const_string_tree (type_name);
  tree fold_class = GPY_RR_fold_class_decl (attribs_decl, TYPE_SIZE_UNIT (type),
					    build_fold_addr_expr (class_str));

  switch (TREE_CODE (decl))
    {
    case VAR_DECL:
      {
	append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr,
					  decl, fold_class),
				  block);
      }
      break;

    default:
      {
	tree tmp = build_decl (UNKNOWN_LOCATION, VAR_DECL,
			       create_tmp_var_name ("A"),
			       gpy_object_type_ptr_ptr);
	append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
					  tmp,
					  decl),
				  block);
	tree refaddr = build_fold_indirect_ref (tmp);
	append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
					  refaddr,
					  fold_class),
				  block);
      }
      break;
    }
}

/* 
   Creates a DECL_CHAIN of stmts to fold the scalar 
   with the last tree being the address of the primitive 
*/
static
tree gpy_dot_pass_genericify_scalar (gpy_dot_tree_t * decl, tree * cblock)
{
  tree retval = error_mark_node;

  gcc_assert (DOT_TYPE (decl) == D_PRIMITIVE);
  gcc_assert (DOT_lhs_T (decl) == D_TD_COM);

  switch (DOT_lhs_TC (decl)->T)
    {
    case D_T_INTEGER:
      {
        retval = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("P"),
                             gpy_object_type_ptr);
        append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr, retval,
                                          GPY_RR_fold_integer (build_int_cst (integer_type_node,
									      DOT_lhs_TC (decl)->o.integer)
							       )),
                                  cblock);
      }
      break;

    default:
      error ("invalid scalar type!\n");
      break;
    }

  return retval;
}

static
void gpy_dot_pass_genericify_print_stmt (gpy_dot_tree_t * decl, tree * block,
					 VEC(gpy_context_t,gc) * context)
{
  gpy_dot_tree_t * arguments = decl->opa.t;
  
  VEC(tree,gc) * callvec_tmp = VEC_alloc (tree,gc,0);
  gpy_dot_tree_t * it = NULL;
  for (it = arguments; it != NULL; it = DOT_CHAIN (it))
    {
      tree lexpr = gpy_dot_pass_lower_expr (it, block, context);
      VEC_safe_push (tree, gc, callvec_tmp, lexpr);
    }
  VEC(tree,gc) * callvec = VEC_alloc (tree,gc,0);
  VEC_safe_push (tree, gc, callvec, build_int_cst (integer_type_node, 1));
  VEC_safe_push (tree, gc, callvec, build_int_cst (integer_type_node,
						   VEC_length (tree, callvec_tmp)));

  GPY_VEC_stmts_append (tree, callvec, callvec_tmp);

  append_to_statement_list (GPY_RR_eval_print (callvec), block);
}

static
int gpy_dot_pass_push_decl (tree decl, const char * ident,
			    gpy_context_t context)
{
  int retval = 0;
  gpy_hashval_t h = gpy_dd_hash_string (ident);
  void ** slot = gpy_dd_hash_insert (h, decl, context);
  if (!slot)
    {
      debug ("pushed decl <%s> into context!\n", ident);
      retval = 1;
    }
  else
    {
      debug ("error pushing decl <%s>!\n", ident);
    }
  return retval;
}

static
tree gpy_dot_pass_decl_lookup (VEC(gpy_context_t,gc) * context,
			       const char * identifier)
{
  tree retval = error_mark_node;
  gpy_hashval_t h = gpy_dd_hash_string (identifier);

  int idx, length = VEC_length (gpy_context_t, context);
  for (idx = length - 1; idx >= 0; --idx)
    {
      gpy_context_t ctx = VEC_index (gpy_context_t, context, idx);
      gpy_hash_entry_t * o = NULL;
      o = gpy_dd_hash_lookup_table (ctx, h);
      if (o)
	{
	  if (o->data)
	    {
	      debug ("found decl <%s>!\n", identifier);
	      retval = (tree) o->data;
	      break;
	    }
	}
    }
  return retval;
}

static
tree gpy_dot_pass_genericify_modify (gpy_dot_tree_t * decl, tree * block,
				     VEC(gpy_context_t,gc) * context)
{
  tree retval = error_mark_node;
  gpy_dot_tree_t * lhs = DOT_lhs_TT (decl);
  gpy_dot_tree_t * rhs = DOT_rhs_TT (decl);

  /*
    We dont handle full target lists yet
    all targets are in the lhs tree.
   
    To implment a target list such as:
    x,y,z = 1

    The lhs should be a DOT_CHAIN of identifiers!
    So we just iterate over them and deal with it as such!
   */

  if (DOT_TYPE (lhs) == D_IDENTIFIER)
    {
      tree addr = gpy_dot_pass_decl_lookup (context, DOT_IDENTIFIER_POINTER (lhs));
      /* if its not already declared we just create an instance on the stack */
      if (addr == error_mark_node)
	{
	  /* since not previously declared we need to declare the variable! */
	  gpy_hash_tab_t * current_context = VEC_index (gpy_context_t, context,
							(VEC_length (gpy_context_t, context) - 1));
	  addr = build_decl (UNKNOWN_LOCATION, VAR_DECL,
			     get_identifier (DOT_IDENTIFIER_POINTER (lhs)),
			     gpy_object_type_ptr);
	  if (!gpy_dot_pass_push_decl (addr, DOT_IDENTIFIER_POINTER (lhs), 
				       current_context))
	    error ("error pushing decl <%s>!\n", IDENTIFIER_POINTER (DECL_NAME (addr)));
	}
      gcc_assert (addr != error_mark_node);
      tree addr_rhs_tree = gpy_dot_pass_lower_expr (rhs, block, context);
      
      switch (TREE_CODE (addr))
	{
	case VAR_DECL:
	  {
	    debug ("var_decl assign!\n");
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr,
					      addr,
					      addr_rhs_tree),
				      block);
	    retval = addr;
	  }
	  break;

	case PARM_DECL:
	  fatal_error ("careful now!\n");
	  break;

	case COMPONENT_REF:
	  {
	    debug ("component_ref assign!\n");
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr,
					      addr,
					      addr_rhs_tree),
				      block);
	    retval = addr;
	  }
	  break;

	default:
	  {
	    tree tmp = build_decl (UNKNOWN_LOCATION, VAR_DECL,
				   create_tmp_var_name ("A"),
				   gpy_object_type_ptr_ptr);
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
					      tmp,
					      addr),
				      block);
	    tree refer = build_fold_indirect_ref (tmp);
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
					      refer,
					      addr_rhs_tree),
				      block);
	    retval = refer;
	  }
	  break;
	}
    }
  else if (DOT_TYPE (lhs) == D_ATTRIB_REF)
    {
      tree addr_1 = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("T"),
				gpy_object_type_ptr_ptr);

      gpy_dot_tree_t * xlhs = DOT_lhs_TT (lhs);
      gpy_dot_tree_t * xrhs = DOT_rhs_TT (lhs);

      tree addr_rhs_tree = gpy_dot_pass_lower_expr (rhs, block, context);
     
      gcc_assert (DOT_TYPE (xlhs) == D_IDENTIFIER);
      gcc_assert (DOT_TYPE (xrhs) == D_IDENTIFIER);

      tree lhs_tree = gpy_dot_pass_lower_expr (xlhs, block, context);
      char * attrib_ident = DOT_IDENTIFIER_POINTER (xrhs);
      
      tree str = gpy_dot_type_const_string_tree (attrib_ident);
      append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
					addr_1,
					GPY_RR_fold_attrib_ref (lhs_tree,
								build_fold_addr_expr (str))),
				block);
      tree refer = build_fold_indirect_ref (addr_1);
      append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
					refer, addr_rhs_tree),
				block);
      retval = refer;
    }
  else
    fatal_error ("unhandled modify target type!\n");
  
  return retval;
}

static
tree gpy_dot_pass_genericify_binary_op (gpy_dot_tree_t * decl, tree * block,
					VEC(gpy_context_t,gc) * context)
{
  tree retval = error_mark_node;

  gcc_assert (DOT_T_FIELD (decl) == D_D_EXPR);

  gpy_dot_tree_t * lhs = DOT_lhs_TT (decl);
  gpy_dot_tree_t * rhs = DOT_rhs_TT (decl);

  tree lhs_eval = gpy_dot_pass_lower_expr (lhs, block, context);
  tree rhs_eval = gpy_dot_pass_lower_expr (rhs, block, context);

  tree op = error_mark_node;
  switch (DOT_TYPE (decl))
    {
    case D_ADD_EXPR:
      op = GPY_RR_eval_expression (lhs_eval, rhs_eval, build_int_cst (integer_type_node, 1));
      break;

      // .... THE REST OF THE BIN OPERATORS 

    default:
      error ("unhandled binary operation type!\n");
      break;
    }
  gcc_assert (op != error_mark_node);
  
  tree retaddr = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("T"),
                             gpy_object_type_ptr);
  append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr, retaddr, op),
			    block);
  retval = retaddr;

  return retval;
}

static
tree gpy_dot_pass_lower_expr (gpy_dot_tree_t * decl, tree * block,
			      VEC(gpy_context_t,gc) * context)
{
  tree retval = error_mark_node;

  switch (DOT_TYPE (decl))
    {
    case D_PRIMITIVE:
      retval = gpy_dot_pass_genericify_scalar (decl, block);
      break;

    case D_IDENTIFIER:
      {
	tree lookup = gpy_dot_pass_decl_lookup (context,
						DOT_IDENTIFIER_POINTER (decl));
	switch (TREE_CODE (lookup))
	  {
	  case VAR_DECL:
	    retval = lookup;
	    break;

	  case PARM_DECL:
	    retval = lookup;
	    break;

	  case COMPONENT_REF:
	    retval = lookup;
	    break;
	    
	  default:
	    {
	      tree tmp = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("A"),
				     gpy_object_type_ptr_ptr);
	      append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr,
						tmp,
						lookup),
					block);
	      retval = build_fold_indirect_ref (tmp);
	    }
	    break;
	  }
      }
      break;

    case D_ATTRIB_REF:
      {
	tree addr_1 = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("T"),
				  gpy_object_type_ptr_ptr);

	gpy_dot_tree_t * xlhs = DOT_lhs_TT (decl);
	gpy_dot_tree_t * xrhs = DOT_rhs_TT (decl);

	gcc_assert (DOT_TYPE (xlhs) == D_IDENTIFIER);
	gcc_assert (DOT_TYPE (xrhs) == D_IDENTIFIER);
	
	tree lhs_tree = gpy_dot_pass_lower_expr (xlhs, block, context);
	char * attrib_ident = DOT_IDENTIFIER_POINTER (xrhs);
	
	tree str = gpy_dot_type_const_string_tree (attrib_ident);
	tree attrib_ref = build2 (MODIFY_EXPR, gpy_object_type_ptr_ptr, addr_1,
				  GPY_RR_fold_attrib_ref (lhs_tree,
							  build_fold_addr_expr (str))
				  );
	append_to_statement_list (attrib_ref, block);
	retval = build_fold_indirect_ref (addr_1);
      }
      break;

    case D_CALL_EXPR:
      {
	gpy_dot_tree_t * callid = DOT_lhs_TT (decl);
	if (DOT_TYPE (callid) == D_ATTRIB_REF)
	  {
	    tree call_decl = gpy_dot_pass_lower_expr (callid, block, context);
	    gcc_assert (call_decl != error_mark_node);

	    /* now we need to find the base of the attribute reference so we can pass self */
	    /* lets just assume we only handle x.y for now */
	    gpy_dot_tree_t * xlhs = DOT_lhs_TT (callid);
	    gpy_dot_tree_t * xrhs = DOT_rhs_TT (callid);
	    
	    gcc_assert (DOT_TYPE (xlhs) == D_IDENTIFIER);
	    gcc_assert (DOT_TYPE (xrhs) == D_IDENTIFIER); 

	    tree basetree = gpy_dot_pass_lower_expr (xlhs, block, context);

	    gpy_dot_tree_t * argslist;
	    VEC(tree,gc) * argsvec = VEC_alloc (tree,gc,0);
	    VEC_safe_push (tree, gc, argsvec, basetree);

	    for (argslist = DOT_rhs_TT (decl); argslist != NULL_DOT; argslist = DOT_CHAIN (argslist))
	      {
		tree lexpr = gpy_dot_pass_lower_expr (argslist, block, context);
		VEC_safe_push (tree, gc, argsvec, lexpr);
	      }
	    VEC(tree,gc) * args = VEC_alloc (tree,gc,0);
	    VEC_safe_push (tree, gc, args, call_decl);
	    VEC_safe_push (tree, gc, args, build_int_cst (integer_type_node, VEC_length (tree, argsvec)));
	    GPY_VEC_stmts_append (tree, args, argsvec);

	    tree retaddr = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("R"),
				       gpy_object_type_ptr);
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr, retaddr,
					      GPY_RR_fold_call (args)),
				      block);
	    retval = retaddr;
	  }
	else
	  {
	    tree call_decl = gpy_dot_pass_lower_expr (callid, block, context);
	    gcc_assert (call_decl != error_mark_node);

	    gpy_dot_tree_t * argslist;
	    VEC(tree,gc) * argsvec = VEC_alloc (tree,gc,0);
	    for (argslist = DOT_rhs_TT (decl); argslist != NULL_DOT; argslist = DOT_CHAIN (argslist))
	      {
		tree lexpr = gpy_dot_pass_lower_expr (argslist, block, context);
		VEC_safe_push (tree, gc, argsvec, lexpr);
	      }
	    VEC(tree,gc) * args = VEC_alloc (tree,gc,0);
	    VEC_safe_push (tree, gc, args, call_decl);
	    VEC_safe_push (tree, gc, args, build_int_cst (integer_type_node, VEC_length (tree, argsvec)));
	    GPY_VEC_stmts_append (tree, args, argsvec);

	    tree retaddr = build_decl (UNKNOWN_LOCATION, VAR_DECL, create_tmp_var_name ("R"),
				       gpy_object_type_ptr);
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr, retaddr,
					      GPY_RR_fold_call (args)),
				      block);
	    retval = retaddr;
	  }
      }
      break;

    default:
      {
	switch (DOT_TYPE (decl))
          {
          case D_MODIFY_EXPR:
            retval = gpy_dot_pass_genericify_modify (decl, block, context);
            break;

          case D_ADD_EXPR:
            retval = gpy_dot_pass_genericify_binary_op (decl, block, context);
            break;

            // ... the rest of the binary operators!

          default:
            error ("unhandled operation type!\n");
            break;
          }
      }
      break;
    }

  return retval;
}

static
tree gpy_dot_pass_genericify_toplevl_functor_decl (gpy_dot_tree_t * decl,
						   VEC(gpy_context_t, gc) * context)
{
  tree fntype = build_function_type_list (void_type_node,
					  gpy_object_type_ptr_ptr,
					  NULL_TREE);
  tree ident = GPY_dot_pass_genericify_gen_concat_identifier (GPY_current_module_name,
							      DOT_IDENTIFIER_POINTER (DOT_FIELD (decl))
							      );
  tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, ident, fntype);
  debug ("lowering toplevel function <%s> to <%s>!\n", DOT_IDENTIFIER_POINTER (DOT_FIELD (decl)),
	 IDENTIFIER_POINTER (ident));

  TREE_STATIC(fndecl) = 0;
  TREE_USED(fndecl) = 1;
  DECL_ARTIFICIAL(fndecl) = 1;
  TREE_PUBLIC(fndecl) = 1;
  
  tree arglist = NULL_TREE;
  tree self_parm_decl = build_decl (BUILTINS_LOCATION, PARM_DECL,
                                    get_identifier ("__arguments__"),
                                    gpy_object_type_ptr_ptr);

  DECL_CONTEXT (self_parm_decl) = fndecl;
  DECL_ARG_TYPE (self_parm_decl) = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
  TREE_READONLY (self_parm_decl) = 1;
  arglist = chainon (arglist, self_parm_decl);

  TREE_USED (self_parm_decl) = 1;
  DECL_ARGUMENTS (fndecl) = arglist;

  /* Define the return type (represented by RESULT_DECL) for the main functin */
  tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL,
			    NULL_TREE, TREE_TYPE(fntype));
  DECL_CONTEXT (resdecl) = fndecl;
  DECL_ARTIFICIAL (resdecl) = true;
  DECL_IGNORED_P (resdecl) = true;

  DECL_RESULT (fndecl) = resdecl;

  /* This is usually used for debugging purpose. this is currently unused */
  tree block = build_block (NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
  DECL_INITIAL (fndecl) = block;

  tree stmts = alloc_stmt_list ();

  gpy_hash_tab_t ctx;
  gpy_dd_hash_init_table (&ctx);
  gpy_dot_tree_t * pnode = DOT_lhs_TT (decl);

  gpy_dot_pass_genericify_arguments_to_context (&ctx, pnode, self_parm_decl, &stmts);
  VEC_safe_push (gpy_context_t, gc, context, &ctx);

  gpy_dot_tree_t * node = decl->opb.t;
  do {
    if (DOT_T_FIELD (node) ==  D_D_EXPR)
      {
	// append to stmt list as this goes into the module initilizer...
	gpy_dot_pass_lower_expr (node, &stmts, context);
	continue;
      }
    
    switch (DOT_TYPE (node))
      {
      case D_PRINT_STMT:
	gpy_dot_pass_genericify_print_stmt (node, &stmts, context);
	break;

      default:
	error ("unhandled syntax!\n");
	break;
      }
  } while ((node = DOT_CHAIN (node)));

  /*
    lower the function suite here and append all initilization
  */
  tree declare_vars = resdecl;

  tree bind = NULL_TREE;
  tree bl = build_block (declare_vars, NULL_TREE, fndecl, NULL_TREE);
  DECL_INITIAL (fndecl) = bl;
  TREE_USED (bl) = 1;

  bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS(bl),
		 NULL_TREE, bl);
  TREE_SIDE_EFFECTS (bind) = 1;

  /* Finalize the main function */
  BIND_EXPR_BODY (bind) = stmts;
  stmts = bind;
  DECL_SAVED_TREE (fndecl) = stmts;
  
  gimplify_function_tree (fndecl);
  cgraph_finalize_function (fndecl, false);

  VEC_pop (gpy_context_t, context);

  return fndecl;
}

static
tree gpy_dot_pass_genericify_class_method_attrib (gpy_dot_tree_t * decl,
						  const char * parent_ident,
						  VEC(gpy_context_t,gc) * context)
{
  tree fntype = build_function_type_list (void_type_node,
					  gpy_object_type_ptr,
					  gpy_object_type_ptr_ptr,
					  NULL_TREE);
  tree ident = GPY_dot_pass_genericify_gen_concat_identifier (GPY_current_module_name,
							      parent_ident);
  ident = GPY_dot_pass_genericify_gen_concat_identifier (IDENTIFIER_POINTER (ident),
							 DOT_IDENTIFIER_POINTER (DOT_FIELD (decl)));
  tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, ident, fntype);
  debug ("lowering class attribute <%s> to <%s>!\n", DOT_IDENTIFIER_POINTER (DOT_FIELD (decl)),
	 IDENTIFIER_POINTER (ident));

  TREE_STATIC(fndecl) = 0;
  TREE_USED(fndecl) = 1;
  DECL_ARTIFICIAL(fndecl) = 1;
  TREE_PUBLIC(fndecl) = 1;

  tree self_parm_decl = NULL_TREE;
  tree arglist = NULL_TREE;
  tree parm_decl = build_decl (BUILTINS_LOCATION, PARM_DECL,
			       get_identifier ("self"),
			       gpy_object_type_ptr);

  DECL_CONTEXT (parm_decl) = fndecl;
  DECL_ARG_TYPE (parm_decl) = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
  TREE_READONLY (parm_decl) = 1;
  arglist = chainon (arglist, parm_decl);
  TREE_USED (parm_decl) = 1;
  self_parm_decl = parm_decl;

  parm_decl = build_decl (BUILTINS_LOCATION, PARM_DECL,
			  get_identifier ("__arguments__"),
			  gpy_object_type_ptr_ptr);

  DECL_CONTEXT (parm_decl) = fndecl;
  DECL_ARG_TYPE (parm_decl) = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
  TREE_READONLY (parm_decl) = 1;
  arglist = chainon (arglist, parm_decl);
  TREE_USED (parm_decl) = 1;

  DECL_ARGUMENTS (fndecl) = arglist;

  /* Define the return type (represented by RESULT_DECL) for the main functin */
  tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL,
			    NULL_TREE, TREE_TYPE(fntype));
  DECL_CONTEXT (resdecl) = fndecl;
  DECL_ARTIFICIAL (resdecl) = true;
  DECL_IGNORED_P (resdecl) = true;

  DECL_RESULT (fndecl) = resdecl;

  /* This is usually used for debugging purpose. this is currently unused */
  tree block = build_block (NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
  DECL_INITIAL (fndecl) = block;

  tree stmts = alloc_stmt_list ();

  gpy_hash_tab_t ctx;
  gpy_dd_hash_init_table (&ctx);

  gpy_dot_tree_t * pnode = DOT_lhs_TT (decl);
  // we dont care about self...
  pnode = DOT_CHAIN (pnode);

  gpy_dot_pass_genericify_arguments_to_context (&ctx, pnode, parm_decl, &stmts);
  if (self_parm_decl)
    gcc_assert (!gpy_dd_hash_insert (gpy_dd_hash_string ("self"), self_parm_decl,
				     &ctx));
  else
    warning(0, "No self parameter declared!\n");
  VEC_safe_push (gpy_context_t, gc, context, &ctx);
  /*
    lower the function suite here and append all initilization
  */
  gpy_dot_tree_t * class_suite = DOT_rhs_TT (decl);
  gpy_dot_tree_t * node = class_suite;
  do {
    if (DOT_T_FIELD (node) ==  D_D_EXPR)
      {
	// append to stmt list as this goes into the module initilizer...
	gpy_dot_pass_lower_expr (node, &stmts, context);
	continue;
      }
    
    switch (DOT_TYPE (node))
      {
      case D_PRINT_STMT:
	gpy_dot_pass_genericify_print_stmt (node, &stmts, context);
	break;

      default:
	break;
      }
  } while ((node = DOT_CHAIN (node)));

  tree declare_vars = resdecl;

  tree bind = NULL_TREE;
  tree bl = build_block (declare_vars, NULL_TREE, fndecl, NULL_TREE);
  DECL_INITIAL (fndecl) = bl;
  TREE_USED (bl) = 1;

  bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS(bl),
		 NULL_TREE, bl);
  TREE_SIDE_EFFECTS (bind) = 1;

  /* Finalize the main function */
  BIND_EXPR_BODY(bind) = stmts;
  stmts = bind;
  DECL_SAVED_TREE (fndecl) = stmts;
  
  gimplify_function_tree (fndecl);
  cgraph_finalize_function (fndecl, false);

  VEC_pop (gpy_context_t, context);

  return fndecl;
}

static
VEC(tree,gc) * gpy_dot_pass_genericify_toplevl_class_decl (gpy_dot_tree_t * decl,
							   VEC(gpy_context_t,gc) * context,
							   gpy_hash_tab_t * types)
{
  VEC(tree,gc) * lowered_decls = VEC_alloc (tree,gc,0);

  tree class_name = GPY_dot_pass_genericify_gen_concat_identifier (GPY_current_module_name,
								   DOT_IDENTIFIER_POINTER (DOT_FIELD (decl))
								   );
  tree class_type = gpy_dot_pass_genericify_get_module_type (IDENTIFIER_POINTER (class_name),
							     types);
  tree class_type_ptr = build_pointer_type (class_type);
  tree fntype = build_function_type_list (void_type_node,
					  class_type_ptr,
					  NULL_TREE);
  tree ident = GPY_dot_pass_genericify_gen_concat_identifier (GPY_current_module_name,
							      DOT_IDENTIFIER_POINTER (DOT_FIELD (decl))
							      );
  ident = GPY_dot_pass_genericify_gen_concat_identifier (IDENTIFIER_POINTER (ident),
							 "__field_init__");
  tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, ident, fntype);
  debug ("lowering toplevel class <%s> to <%s>!\n", DOT_IDENTIFIER_POINTER (DOT_FIELD (decl)),
	 IDENTIFIER_POINTER (ident));

  TREE_STATIC(fndecl) = 0;
  TREE_USED(fndecl) = 1;
  DECL_ARTIFICIAL(fndecl) = 1;
  TREE_PUBLIC(fndecl) = 1;

  tree arglist = NULL_TREE;
  tree self_parm_decl = build_decl (BUILTINS_LOCATION, PARM_DECL,
                                    get_identifier ("__object_state__"),
                                    class_type_ptr);

  DECL_CONTEXT (self_parm_decl) = fndecl;
  DECL_ARG_TYPE (self_parm_decl) = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
  TREE_READONLY (self_parm_decl) = 1;
  arglist = chainon (arglist, self_parm_decl);

  TREE_USED (self_parm_decl) = 1;
  DECL_ARGUMENTS (fndecl) = arglist;
  
  /* Define the return type (represented by RESULT_DECL) for the main functin */
  tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL,
			    NULL_TREE, TREE_TYPE(fntype));
  DECL_CONTEXT (resdecl) = fndecl;
  DECL_ARTIFICIAL (resdecl) = true;
  DECL_IGNORED_P (resdecl) = true;

  DECL_RESULT (fndecl) = resdecl;

  /* SETUP class attribs references */
  /* 
     TYPE { x,y,z }:
       self.x
       self.y
       self.z
   */

  gpy_hash_tab_t field_type_namespace;
  gpy_dd_hash_init_table (&field_type_namespace);
  gpy_dot_pass_genericify_class_type (class_type, self_parm_decl, &field_type_namespace);

  VEC_safe_push (gpy_context_t, gc, context, &field_type_namespace);

  /* This is usually used for debugging purpose. this is currently unused */
  tree block = build_block (NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
  DECL_INITIAL (fndecl) = block;

  tree stmts = alloc_stmt_list ();

  /*
    lower the function suite here and append all initilization
  */
  gpy_dot_tree_t * class_suite = DOT_lhs_TT (decl);
  gpy_dot_tree_t * node = class_suite;
  do {
    if (DOT_T_FIELD (node) ==  D_D_EXPR)
      {
	// append to stmt list as this goes into the module initilizer...
	gpy_dot_pass_lower_expr (node, &stmts, context);
	continue;
      }
    
    switch (DOT_TYPE (node))
      {
      case D_PRINT_STMT:
	gpy_dot_pass_genericify_print_stmt (node, &stmts, context);
	break;
	
      case D_STRUCT_METHOD:
	{
	  tree a = gpy_dot_pass_genericify_class_method_attrib (node,
								DOT_IDENTIFIER_POINTER (DOT_FIELD (decl)),
								context);
	  VEC_safe_push (tree, gc, lowered_decls, a);
	}
	break;

      default:
	break;
      }
  } while ((node = DOT_CHAIN (node)));
  
  tree declare_vars = resdecl;

  tree bind = NULL_TREE;
  tree bl = build_block (declare_vars, NULL_TREE, fndecl, NULL_TREE);
  DECL_INITIAL (fndecl) = bl;
  TREE_USED (bl) = 1;

  bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS(bl),
		 NULL_TREE, bl);
  TREE_SIDE_EFFECTS (bind) = 1;

  /* Finalize the main function */
  BIND_EXPR_BODY(bind) = stmts;
  stmts = bind;
  DECL_SAVED_TREE (fndecl) = stmts;
  
  gimplify_function_tree (fndecl);
  cgraph_finalize_function (fndecl, false);

  VEC_safe_push (tree, gc, lowered_decls, fndecl);
  VEC_pop (gpy_context_t, context);

  return lowered_decls;
}

static
VEC(tree,gc) * gpy_dot_pass_genericify_TU (gpy_hash_tab_t * modules,
					   VEC(gpydot,gc) * decls)
{
  VEC(tree,gc) * lowered_decls = VEC_alloc (tree, gc, 0);
  
  gpy_hash_tab_t toplvl, topnxt;
  gpy_dd_hash_init_table (&toplvl);
  gpy_dd_hash_init_table (&topnxt);

  tree main_init_module = gpy_dot_pass_genericify_get_module_type (GPY_current_module_name,
								   modules);
  tree fntype = build_function_type_list (void_type_node,
					  NULL_TREE);
  tree ident = GPY_dot_pass_genericify_gen_concat_identifier (GPY_current_module_name,
								"__main_start__");
  tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, ident, fntype);
  debug ("lowering toplevel module <%s> to <%s>!\n", GPY_current_module_name,
	 IDENTIFIER_POINTER (ident));

  TREE_STATIC(fndecl) = 0;
  TREE_USED(fndecl) = 1;
  DECL_ARTIFICIAL(fndecl) = 1;
  TREE_PUBLIC(fndecl) = 1;
  
  tree argslist = NULL_TREE;
  DECL_ARGUMENTS (fndecl) = argslist;

  /* Define the return type (represented by RESULT_DECL) for the main functin */
  tree resdecl = build_decl (BUILTINS_LOCATION, RESULT_DECL,
			     NULL_TREE, TREE_TYPE(fntype));
  DECL_CONTEXT (resdecl) = fndecl;
  DECL_ARTIFICIAL (resdecl) = true;
  DECL_IGNORED_P (resdecl) = true;

  DECL_RESULT (fndecl) = resdecl;

  /* This is usually used for debugging purpose. this is currently unused */
  tree block = build_block (NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
  DECL_INITIAL (fndecl) = block;

  tree stmts = alloc_stmt_list ();

  VEC(gpy_context_t,gc) * context = VEC_alloc (gpy_context_t, gc, 0);
  VEC_safe_push (gpy_context_t, gc, context, &toplvl);
  VEC_safe_push (gpy_context_t, gc, context, &topnxt);

  gpy_dot_pass_genericify_setup_runtime_globls (&toplvl);
  gpy_dot_pass_genericify_create_offsets_globl_context (main_init_module,
							&stmts,
							context);
  int idx;
  gpy_dot_tree_t * idtx = NULL_DOT;
  /*
    Iterating over the DOT IL to lower/generate the GENERIC code
    required to compile the stmts and decls
  */
  for (idx = 0; VEC_iterate (gpydot, decls, idx, idtx); ++idx)
    {
      if (DOT_T_FIELD (idtx) ==  D_D_EXPR)
	{
	  // append to stmt list as this goes into the module initilizer...
          gpy_dot_pass_lower_expr (idtx, &stmts, context);
          continue;
	}

      switch (DOT_TYPE (idtx))
        {
	case D_PRINT_STMT:
	  gpy_dot_pass_genericify_print_stmt (idtx, &stmts, context);
	  break;

        case D_STRUCT_METHOD:
          {
	    tree t = gpy_dot_pass_genericify_toplevl_functor_decl (idtx, context);
	    VEC_safe_push (tree, gc, lowered_decls, t);

	    /* assign the function to the decl */
	    const char * funcid = DOT_IDENTIFIER_POINTER (DOT_FIELD (idtx));
	    tree funcdecl = gpy_dot_pass_decl_lookup (context, funcid);
	    tree str = gpy_dot_type_const_string_tree (funcid);
	    append_to_statement_list (build2 (MODIFY_EXPR, gpy_object_type_ptr,
					      funcdecl,
					      GPY_RR_fold_func_decl (build_fold_addr_expr (str),
								     t)),
				      &stmts);
          }
          break;

	case D_STRUCT_CLASS:
	  {
	    VEC(tree,gc) * cdecls = gpy_dot_pass_genericify_toplevl_class_decl (idtx, context,
										modules);
	    GPY_VEC_stmts_append (tree, lowered_decls, cdecls);
	    tree class_name = GPY_dot_pass_genericify_gen_concat_identifier (GPY_current_module_name,
									     DOT_IDENTIFIER_POINTER (DOT_FIELD (idtx))
									     );
	    tree class_type = gpy_dot_pass_genericify_get_module_type (IDENTIFIER_POINTER (class_name),
								       modules);
	    tree class_decl_ptr = gpy_dot_pass_decl_lookup (context,
							    DOT_IDENTIFIER_POINTER (DOT_FIELD (idtx)));
	    gpy_dot_pass_genericify_walk_class (&stmts, class_type, class_decl_ptr, cdecls);
	  }
	  break;

        default:
          fatal_error ("unhandled dot tree code <%i>!\n", DOT_TYPE (idtx));
          break;
        }
    }
  tree entry_fntype = gpy_unsigned_char_ptr;
  tree entry_decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
				get_identifier (GPY_RR_entry),
				entry_fntype);
  TREE_STATIC (entry_decl) = 1;
  TREE_PUBLIC (entry_decl) = 1;
  TREE_USED (entry_decl) = 1;
  DECL_ARTIFICIAL (entry_decl) = 1;
  DECL_EXTERNAL (entry_decl) = 0;
  DECL_INITIAL (entry_decl) = build_fold_addr_expr (fndecl);
 
  tree declare_vars = resdecl;

  tree bind = NULL_TREE;
  tree bl = build_block (declare_vars, NULL_TREE, fndecl, NULL_TREE);
  DECL_INITIAL (fndecl) = bl;
  TREE_USED (bl) = 1;

  bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS(bl),
		 NULL_TREE, bl);
  TREE_SIDE_EFFECTS (bind) = 1;

  /* Finalize the main function */
  BIND_EXPR_BODY(bind) = stmts;
  stmts = bind;
  DECL_SAVED_TREE (fndecl) = stmts;
  
  gimplify_function_tree (fndecl);
  cgraph_finalize_function (fndecl, false);

  VEC_safe_push (tree, gc, lowered_decls, fndecl);
  VEC_safe_push (tree, gc, lowered_decls, entry_decl);

  return lowered_decls;
}

VEC(tree,gc) * gpy_dot_pass_genericify (VEC(tree,gc) *modules,
					VEC(gpydot,gc) *decls)
{
  VEC(tree,gc) * retval = VEC_alloc (tree, gc, 0);

  gpy_hash_tab_t module_ctx;
  gpy_dd_hash_init_table (&module_ctx);

  int idx;
  tree itx = NULL_TREE;
  for (idx = 0; VEC_iterate (tree, modules, idx, itx); ++idx)
    {
      debug ("hashing module name <%s>!\n", IDENTIFIER_POINTER (TYPE_NAME(itx)));
      gpy_hashval_t h = gpy_dd_hash_string (IDENTIFIER_POINTER (TYPE_NAME(itx)));
      void ** e = gpy_dd_hash_insert (h, itx, &module_ctx);
      
      if (e)
        error ("module <%s> is already defined!\n",
	       IDENTIFIER_POINTER (DECL_NAME (itx))
	       );
    }

  debug ("genericification...!\n");
  retval = gpy_dot_pass_genericify_TU (&module_ctx, decls);
  debug ("finishing genericification!\n");

  if (module_ctx.array)
    free (module_ctx.array);

  return retval;
}