Source

LIME / lib / gxf-parser.awk

Full commit
  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

# This AWK script implements GXF parsing
# It should not be aware of GXF instatiation handling
#
#Copyright (C) 2008 NXP Semiconductors B.V.
#
#This file is part of LIME.
#
#LIME is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 2
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#LIME 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 LIME.  If not, see <http://www.gnu.org/licenses/>.
##################################################################

BEGIN {
	if (!root) root="@gxf"
	if (root=="@") root=""
	if (strict=="") strict=1
}

# literal output port doubles as an input port
func is_inport(c,n,p) {
	if (port_inout[c,n,p]) return 1
	if (port_const[c,n,p]) return 1
	if (is_literal(c,n,p)) return 1
	return 0
}
func is_stateport(c,n,p) {
	if (port_volatilep[c,n,p]) return 1
	if ((c,n,p,"read") in stateport_idx) return 1
	if ((c,n,p,"write") in stateport_idx) return 1
	return 0
}
func is_outport(c,n,p) {
	if (port_inout[c,n,p]) return 1
	if (!port_const[c,n,p]) return 1
	return 0
}
func isarray(c,p) {
    	if (port_calltype[c,p]=="array_type")
		return 1
    	return 0
}
func ispointer(c,p) {
    	if (port_calltype[c,p]=="pointer_type")
		return 1
    	return 0
}
func isliteral(c,p) {
    	if (port_calltype[c,p] ~ /^(integer|enumeral)_type$/)
		return 1
    	return 0
}
func is_array(c,n,p) { return isarray(c SUBSEP n,p) }
func is_pointer(c,n,p) { return ispointer(c SUBSEP n,p) }
func is_literal(c,n,p) { return isliteral(c SUBSEP n,p) }

func is_packetport(c,n,p) {
	if (port_packetsize[c,n,p]) return 1
	return 0
}

func is_dynamic(c,n,p) {
    	if ((c,n,port_size[c,n,p]) in port_calltype) 
		return 1
    	if ((c,n,port_packetsize[c,n,p]) in port_calltype) 
		return 1
    	return 0
}

func is_dynamicpacket(c,n,p) {
    	if ((c,n,port_packetsize[c,n,p]) in port_calltype) 
		return 1
    	return 0
}

func is_dynamicport(c,n,p) {
    	if ((c,n,port_size[c,n,p]) in port_calltype) 
		return 1
    	return 0
}

func is_sizeport(c,n,p, i,pp,j,ppp,k,pppp) {
	if (!is_literal(c,n,p)) return 0
	for (i=1; i<=nports[c,n]; i++)
	{
		pp=ports[c,n,i]
		if (port_size[c,n,pp]==p || port_packetsize[c,n,pp]==p)
		    return pp
		if (is_multiport(c SUBSEP n,pp)) for (j=1; j<=nports[n,pp]; j++)
		{
		    ppp=ports[n,pp,j]
		    #print c,n,p,pp,port_size[n,pp,ppp],port_packetsize[n,pp,ppp] >STDERR
		    if (port_size[n,pp,ppp]==p || port_packetsize[n,pp,ppp]==p)
			return ppp
		    if (is_multiport(n SUBSEP pp,ppp)) for (k=1; k<=nports[pp,ppp]; k++)
		    {
		    	pppp=ports[pp,ppp,k]
		    	#print c,n,p,pp,port_size[n,pp,ppp],port_packetsize[n,pp,ppp] >STDERR
		    	if (port_size[pp,ppp,pppp]==p || port_packetsize[pp,ppp,pppp]==p)
			    return pppp
		    }
		}
	}
	return 0
}

func is_portsizeport(c,n,p, i,pp,j,ppp) {
	if (!is_literal(c,n,p)) return 0
	for (i=1; i<=nports[c,n]; i++)
	{
		pp=ports[c,n,i]
		if (port_size[c,n,pp]==p)
			return pp
		if (is_multiport(c SUBSEP n,pp)) for (j=1; j<=nports[n,pp]; j++)
		{
		    ppp=ports[n,pp,j]
		    if (port_size[n,pp,ppp]==p)
			return ppp
		}
	}
	return 0
}

func is_packetsizeport(c,n,p, i,pp,j,ppp) {
	if (!is_literal(c,n,p)) return 0
	for (i=1; i<=nports[c,n]; i++)
	{
		pp=ports[c,n,i]
		if (port_packetsize[c,n,pp]==p)
			return pp
		if (is_multiport(c SUBSEP n,pp)) for (j=1; j<=nports[n,pp]; j++)
		{
		    ppp=ports[n,pp,j]
		    if (port_packetsize[n,pp,ppp]==p)
			return ppp
		}
	}
	return 0
}

func is_groupedmultiport(s,p) { return (port_typeid[s,p] == "record_type") }
func is_collectmultiport(s,p) { return (port_typeid[s,p] == "union_type") }
func is_selectmultiport(s,p) { return (port_typeid[s,p] == "variant_type") }

func is_multiport(s,p) {
	return (port_typeid[s,p] ~ /^(record|union|variant)_type$/)
}

func multiport_tag(f,n,p) {
	return (port_typeid[f,n,p] ~ /^(record|variant)_type$/ ? "struct" : "union")
}

func is_variantport(s,p, a) {
  if (split(variants[port_type[s,p]],a,SUBSEP)==2) return 1
  return 0
}

func is_variant(c,n,p) { return is_variantport(c SUBSEP n,p) }

func is_instance(f,n) { return node_typesource[f,n]==f && is_anyinstance(f,n) }
func is_anyinstance(f,n) { return node_stereotype[f,n] }
func node_class(f,n) { return node_typesource[f,n] SUBSEP node_stereotype[f,n] }
func instance_idx(f,n) { return node_instidx[node_class(f,n),n] }

#DEPRECATED
#func is_connected(f,n) { return fanout[f,n] || fanin[f,n] }
#func is_instance(f,n) { return is_connected(f,n) || node_stereotype[f,n] }

func has_code(n,sn, x,a) 
{
       for (x in node_code) {
	    if (split(x,a,SUBSEP)!=3) fail("bad node_code")
	    if (a[1]!=n || a[2]!=sn || !a[3]) continue
	    return 1
	}
	return 0
}

func register_stateport(f,par,n,p,dir)
{
   #if (portroles[par SUBSEP n,p]) {
 	if ((par,n,p,dir) in stateport_idx); else
   	stateports[par,n,dir,stateport_idx[par,n,p,dir]=++nstateports[par,n,dir]]=p
   #} else
   #print "STATE-" dir ":",par,n,p,stateport_idx[par,n,p,dir] >STDERR

   #if (!portroles[f SUBSEP par,p]); else {
        # XXX: propagate "statefullness" to the parent
	if (par!=f) if ((f,par,p,dir) in stateport_idx); else
	stateports[f,par,dir,stateport_idx[f,par,p,dir]=++nstateports[f,par,dir]]=p
   #}

   # self-edge
   if (n==peer_node[par,n,p]) if ((par,n) in stateful_idx); else {
    statefuls[par,stateful_idx[par,n]=++nstatefuls[par]]=n
    if ((f,par) in stateful_idx); else {
   	statefuls[f,stateful_idx[f,par]=++nstatefuls[f]]=par
	peer_node[f,par,p]=par
	peer_port[f,par,p]=peer_port[par,n,p]
	peer_edge[f,par,p]=peer_edge[par,n,p]
	peer_dir[f,par,p]=peer_dir[par,n,p]
#	print "STATEFUL",f,par,p >STDERR
    }
   }
}

func register_fifoport(f,par,n,p,dir,t)
{
   if (!t) t=p
   if ((par,n,p,dir) in fifo_idx) fifos[par,n,dir,fifo_idx[par,n,p,dir]]=t
   if (portroles[par,n,p]) {
   	if ((par,n,p,dir) in fifo_idx); else {
		#print par,n,p,dir,t >STDERR
		fifos[par,n,dir,fifo_idx[par,n,p,dir]=++nfifos[par,n,dir]]=t
	}
   } 
   # print "FIFO-" dir ":",par,n,p,fifo_idx[par,n,p,dir] >STDERR

   if (par!=f) {
      if ((f,par,p,dir) in fifo_idx) fifos[f,par,dir,fifo_idx[f,par,p,dir]]=t 
      if (portroles[f,par,p]) 
      {	
      	if ((f,par,p,dir) in fifo_idx); else {
		#print f,par,p,dir,t >STDERR
		fifos[f,par,dir,fifo_idx[f,par,p,dir]=++nfifos[f,par,dir]]=t
      	}
      }
   }
}

func register_inport(c,p) {
    inports[c,inport_idx[c,p]=++ninports[c]]=p
    portroles[c,p]++
}

func register_outport(c,p) {
# literal output port doubles as an input port
    if (isliteral(c,p))
	register_inport(c,p)
    outports[c,outport_idx[c,p]=++noutports[c]]=p
    portroles[c,p]++
}

func instantiate(par,n, spar,s, i,p,x,a)
{
    if (spar!="" && s!="") {
    	node_stereotype[par,n]=s
    	node_typesource[par,n]=spar
    	node_instances[spar,s,node_instidx[spar,s,n]=++node_ninstances[spar,s]]=n

	  node_ret[par,n]=node_ret[spar,s]
  	  node_type[par,n]=node_type[spar,s]
	  node_mode[par,n]=node_mode[spar,s]

	  for (i=1; i<=nports[spar,s]; i++) {
		p=ports[spar,s,i]
		copy_port(par SUBSEP n,p,spar,s,p)
	  }

	  for (x in node_code) {
	    if (split(x,a,SUBSEP)!=3) fail("bad node_code")
	    if (a[1]!=spar || a[2]!=s || !a[3]) continue
	    #print par,n,spar,s,a[3] >STDERR
	    node_code[par,n,a[3]]=node_code[spar,s,a[3]]
	  }
    }

    if ((par,n) in node_idx) return # fail("node " n " already in " par)
    nodes[par,node_idx[par,n]=++nnodes[par]]=n
}

func handle_gxf(tag,body, n,p,i,par,fpar,fn,fp,tpar,tn,tp,ed,j,x,sn,spar,dir)
{
    if (DEBUG) print "GXF: handling",tag,"in",state >STDERR

    # attribute id
    if (tag=="@id" && (state ~ /@(gxf|callout|edge)$/))
	return id[lev]=body

    # element id
    if (tag=="id" && (state ~ /@(node|port|edge|metatype|sched|callout)$/ ||
                      state ~ /@(from|to)-node$/))
	return id[lev]=body

    if (tag=="metatype" && state ~ /@port$/) 
    {
	typeid[lev]=id[lev+1]
	delete id[lev+1]
	return type[lev]=body
    }

    if (tag=="stereo-type" && state ~ /@node$/)
    {
	return stereotype[lev]=body
    }

    if (tag=="type" && state ~ /@(node|port|edge)$/)
    {
	if (size[lev+1]) typesizes[body]=size[lev+1]
	delete size[lev+1]
	return type[lev]=body
    }

    if (tag=="calltype" && state ~ /@port$/)
    {
	return calltype[lev]=body
    }

    if (tag=="packetsize" && state ~ /@port$/)
    {
	return packetsize[lev]=body
    }

    if (tag=="mode" && state ~ /@(node|port)$/) 
	return mode[lev]=append(mode[lev],body,"|")

    if (tag=="CDATA") {
        return pcdata[lev]=body
    }

    if (tag=="context" && state ~ /@code$/)
       return context[lev-1]=body

    if (tag=="code" && (state ~ /@node$/ || state ~ /@node@meta$/))
    {
        if (body) body=fromxml(body)
	else {
		body=pcdata[lev+1]
		delete pcdata[lev+1]
	} 
	code[context[lev]]=append(code[context[lev]],body)
	delete context[lev]
	return code[context[lev]]
    }

    if (tag=="ret" && state ~ /@node$/) 
	return ret[lev]=body

    if (tag=="size" && state ~ /@(port|type)$/)
	return size[lev]=append(size[lev],body,SUBSEP)

    if (tag=="inout" && state ~ /@port$/) 
	return inout[lev]=1

    if (tag=="const" && state ~ /@port$/) 
	return const[lev]=1

    if (tag=="volatile" && state ~ /@port$/) 
	return volatile[lev]=1

    if (tag=="constp" && state ~ /@port$/) 
	return constp[lev]=1

    if (tag=="volatilep" && state ~ /@port$/) 
	return volatilep[lev]=1

    if (tag=="static" && state ~ /@port$/) 
	return static[lev]=1

    if (tag=="restrict" && state ~ /@port$/) 
	return restrict[lev]=1


    if (tag=="port" && state !~ /@edge/ &&
        (state ~ /@node$/ || state ~ /@node@port$/ || state ~ /@node@port@port$/))
    {
    	#if (Tag[lev] != "node") fail("port not in node")

	#par=lev>1 ? alt(id[lev-1],f) : f

	if (state=="@gxf@node")
		par=f
	else
		par=id[lev-1] ? id[lev-1] : f

	n=id[lev]
	#print lev,id[lev-1],f,par,n >STDERR
	if (split(n,a,"::")==2) {
		par=a[1]
		n=a[2]
	}

	p=id[lev+1]

	port_type[par,n,p]=type[lev+1]
	port_size[par,n,p]=size[lev+1]
	port_mode[par,n,p]=mode[lev+1]
	port_typeid[par,n,p]=typeid[lev+1]
	port_calltype[par,n,p]=calltype[lev+1]

	port_packetsize[par,n,p]=packetsize[lev+1]

	port_inout[par,n,p]=inout[lev+1]
	port_const[par,n,p]=const[lev+1]
	port_volatile[par,n,p]=volatile[lev+1]
	port_constp[par,n,p]=constp[lev+1]
	port_volatilep[par,n,p]=volatilep[lev+1]
	port_static[par,n,p]=static[lev+1]
	port_restrict[par,n,p]=restrict[lev+1]


	data_modi[par,n,p]=append(const[lev+1] ? "const" : "", volatile[lev+1] ? "volatile" : "")

	port_modi[par,n,p]=append(append(append(append(constp[lev+1] ? "constp" : ""),
			              volatilep[lev+1] ? "volatilep" : ""),
                              		static[lev+1] ? "static" : ""),
			    	      restrict[lev+1] ? "restrict" : "")

	ports[par,n,port_idx[par,n,p]=++nports[par,n]]=p
	#print lev,par,n,nports[par,n],p >STDERR

	if (const[lev+1]) register_inport(par SUBSEP n,p)
	else register_outport(par SUBSEP n,p)

	delete size[lev+1]; delete inout[lev+1]; delete const[lev+1]; delete static[lev+1]
	delete restrict[lev+1]; delete typeid[lev+1]; delete calltype[lev+1]; delete packetsize[lev+1]
	delete constp[lev+1]; delete volatile[lev+1]; delete volatilep[lev+1]
	delete mode[lev+1]; delete type[lev+1]; delete id[lev+1]

	return body
    }

#    if (tag=="port" && state ~ /@rule$/) {
#    	n=id[lev-1]
#	p=id[lev+1]
#	arule[f,n,arule_idx[f,n,p]=++narule[f,n]]=p
#    	return body
#    }
#
#    if (tag=="rule" && state ~ /@node$/) {
#    	return body
#    }

    # top-level node or a sub-node
    if (tag=="node" && (state==root || state ~ /@(node|gxf)$/))
    {
	#par=lev>1 ? id[lev] : f
	if (state==root)
		par=f
	else if (state ~ /@(node|gxf)$/)
		par=id[lev]

    	n=id[lev+1]
	if (split(n,a,"::")==2) {
		par=a[1]
		n=a[2]
	}

	#print par,n >STDERR

	if (stereotype[lev+1]) 
	{
	  s=stereotype[lev+1]
	  if (split(s,a,"::")!=2)
		spar=par
	  else { spar=a[1]
		 s=a[2] }

	  #print spar,s,par,n >STDERR
	  instantiate(par,n,spar,s)

	  delete stereotype[lev+1]
	} else /*(!stereotype[lev+1])*/
	{
	    instantiate(par,n)

	    node_ret[par,n]=ret[lev+1]
	    node_type[par,n]=type[lev+1]
	    node_mode[par,n]=mode[lev+1]

	    if (!nports[par,n]) nports[par,n]=0
	    if (!ninports[par,n]) ninports[par,n]=0
	    if (!noutports[par,n]) noutports[par,n]=0

	    for (x in code) {
		node_code[par,n,x]=append(node_code[par,n,x],code[x])
	    }

	    delete code
	    delete ret[lev+1]
	    delete mode[lev+1]
	    delete type[lev+1]
	}

	for (i=1; i<=ninports[par,n];i++) { p=inports[par,n,i]
	    if (is_stateport(par,n,p)) register_stateport(f,par,n,p,"read")
	    if (!is_stateport(par,n,p) || is_packetport(par,n,p) && port_inout[par,n,p]) {
		#print "REGISTER",f,par,n,p,"read" >STDERR
	    	register_fifoport(f,par,n,p,"read")
	    }
	    if (is_literal(par,n,p)) port_static[par,n,p]=1
	}
	for (i=1; i<=noutports[par,n];i++) { p=outports[par,n,i]
	    if (is_stateport(par,n,p)) register_stateport(f,par,n,p,"write")
	    if (!is_stateport(par,n,p) || is_packetport(par,n,p) && port_inout[par,n,p]) {
		#print "REGISTER",f,par,n,p,"write" >STDERR
	    	register_fifoport(f,par,n,p,"write")
	    }
	    if (is_literal(par,n,p)) port_static[par,n,p]=1
	}

        delete id[lev+1]
	return body
    }

    # Old edge handling
    if (tag ~ /port/ && state ~ /@edge@.*@node$/)
    	return port[lev]=id[lev+1]

    if (tag ~ /node/ && state ~ /@(from|to)$/)
    {
        port[lev]=port[lev+1]
    	return node[lev]=id[lev+1]
    }
    if (tag=="from" && state ~ /@edge$/) {
    	from_node[lev]=node[lev+1]
	from_port[lev]=port[lev+1]
	return body
    }
    if (tag=="to" && state ~ /@edge$/) {
    	to_node[lev]=node[lev+1]
	to_port[lev]=port[lev+1]
	return body
    }

    # New edge handling
    if (tag=="port-id" && state ~ /@(from|to)-node$/ )
    	return port[lev]=body

    if (tag=="from-node" && state ~ /@edge$/) {
    	from_node[lev]=id[lev+1]
	from_port[lev]=port[lev+1]
	return body
    }
    if (tag=="to-node" && state ~ /@edge$/) {
    	to_node[lev]=id[lev+1]
	to_port[lev]=port[lev+1]
	return body
    }

    if (tag=="edge" && (state==root || state ~ /@(node|stream)$/))
    {
	if (state ~ /@node@stream$/)
	  par=id[lev-1]
	else {
	  #par=lev>1 ? id[lev-1] : f
	  #print lev,par,"-1:",id[lev-1],"0:",id[lev],"1:",id[lev+1] >STDERR
	  if (state==root || state ~ /@stream$/) par=f
	  if (state ~ /@node$/) par=id[lev]
	}

    	edge_type[par,ed=++nedges[par]]=type[lev+1]
	# TODO remove this later
	#print ed,type[lev+1],id[lev],id[lev+1] >STDERR
	id_edge[par,edge_id[par,ed]=id[lev+1]]=ed

	fn=from_node[lev+1]
	fp=from_port[lev+1]
	edge_from[par,ed]=fn SUBSEP fp
	
	if (split(fn,a,"::")!=2)
		fpar=par
	else {	fpar=a[1]
		fn=a[2] }

	tn=to_node[lev+1]
	tp=to_port[lev+1]
	edge_to[par,ed]=tn SUBSEP tp

	if (split(tn,a,"::")!=2)
		tpar=par
	else {	tpar=a[1]
		tn=a[2] }

	if ((fpar,fn,fp) in port_type); else
		fail("no such port " fpar ":" fn ":" fp "\nCHECK YOUR GRAPHS AND MODULES")

	if ((tpar,tn,tp) in port_type); else
		fail("no such port " tpar ":" tn ":" tp "\nCHECK YOUR GRAPHS AND MODULES")

	if (!is_outport(fpar,fn,fp) || !is_inport(tpar,tn,tp))
	{
	  if (edge_type[par,ed] ~ /(fifo|state)/)
	    fail("mismatch in edge connector types " fn ":" fp "(" port_const[fpar,fn,fp] ")->("\
		    port_const[fpar,tn,tp] ")" tn ":" tp "\nCHECK YOUR GRAPHS AND MODULES")
	}

	#print par,fn,fp,"->",tn,tp >STDERR

	reg_peer(fn,fp,tn,tp,ed,"write",fpar,tpar,par)
	reg_peer(tn,tp,fn,fp,ed,"read",tpar,fpar,par)

	node_out[par,fn,out_idx[par,fn,ed]=++fanout[par,fn]]=ed
	node_in[par,tn,in_idx[par,tn,ed]=++fanin[par,tn]]=ed

	delete node; delete port
	delete from_node; delete from_port
	delete to_node; delete to_port
	delete type[lev+1]; delete id[lev+1]

	return body
    }

    if (tag=="node" && state ~ /@sched$/) {
    	s=id[lev]
	order[f,s,++norder[f,s]]=id[lev+1]
	mode[f,s,norder[f,s]]=mode[lev+1]
	delete mode[lev+1]
	return body
    }

    if (tag=="callout" && (state==root || state ~ /@node$/))
    {
	#par=lev>1 ? id[lev] : f
	if (state==root)
		par=f
	else
		par=id[lev]

	n=id[lev+1]
    	callout[par,++ncallout[par]]=n
    	callin[n,++ncallin[n]]=par
	#print "HERE:",par,ncallout[par],ncallin[n],n >STDERR
	return body
    }

    if (tag=="sched" && state==root) {
    	comps[f,++ncomps[f]]=id[lev+1]
	return body
    }

    if (relax) {
    	if (DEBUG) print "GXF: ignoring",tag,"in",state >STDERR
    	return body
    }

    fail("GXF: unknown tag " tag)
}

func eval(e) {
	return e
}

func Eval(e, cmd,res) {
    if (e) {
	cmd = "bc"
	print e |& cmd
	cmd |& getline res
	close(cmd)
    }
    return res
}

func selfport(dir,p,pp) {
	if (dir=="write") return p pp
	return pp p
}
func reg_peer(n,p,pn,pp,ed,dir,par,ppar,epar, pt,ppt,i)
{
	pt=port_type[par,n,p]
	ppt=port_type[ppar,pn,pp]

	# TODO: allow only exact matches for now
	if (pt!=ppt) {
	    if (typesizes[pt]==typesizes[ppt]) {
		if (port_constp[par,n,p])
	    	    warn("mismatch in port types " n ":" p "->" pn ":" pp " " pt "!=" ppt)
	    } else
	    	fail("mismatch in port types " n ":" p "->" pn ":" pp " " pt "!=" ppt "\n"\
				"CHECK YOUR GRAPHS AND MODULES")
	}

	# dimension has to match
	if (portdim(par,n,p) != portdim(ppar,pn,pp))
	    	fail("mismatch in port dimension " n ":" p "->" pn ":" pp " "\
			portdim(par,n,p) "!=" portdim(ppar,pn,pp) "\n"\
				"CHECK YOUR GRAPHS AND MODULES")
	
	# all dimension sizes have to match except the first one
	for (i=2; i<portdim(par,n,p); i++) 
		if (portsize(par,n,p,i)!=portsize(ppar,pn,pp,i))
	    		fail("mismatch in dimension size " n ":" p "->" pn ":" pp " "\
				portsize(par,n,p,i) "!=" portsize(ppar,pn,pp,i) "\n"\
					"CHECK YOUR GRAPHS AND MODULES")


	   # constructor edges
	if (edge_type[epar,ed] ~ /^init/) {
	    init_node[par,n,p]=pn
	    init_port[par,n,p]=pp
	    init_edge[par,n,p]=ed
	    init_dir[par,n,p]=dir
	    if (dir == "write") {
	   	cons[par,n,cons_idx[par,n,p]=++ncons[par,n]]=p
	   	if ((par,n) in constructors_idx); else
	   	constructors[par,constructors_idx[par,n]=++nconstructors[par]]=n
	    }
	} else if (edge_type[epar,ed] ~ /^deinit/) {
	   # destructor edges
	    deinit_node[par,n,p]=pn
	    deinit_port[par,n,p]=pp
	    deinit_edge[par,n,p]=ed
	    deinit_dir[par,n,p]=dir
	    if (dir == "read") {
	   	dstr[par,n,dstr_idx[par,n,p]=++ndstr[par,n]]=p
	   	if ((par,n) in destructors_idx); else
	   	destructors[par,destructors_idx[par,n]=++ndestructors[par]]=n
	    }
	} else if (edge_type[par,ed] ~ /(fifo|state)/) {
	    if (peer_node[par,n,p] && pn!=peer_node[par,n,p])
		fail("node endpoint can not be bound twice: "\
			par ":" n ":" p " " peer_node[par,n,p] "!=" pn " (" edge_type[epar,ed] ")")
	    peer_node[par,n,p]=pn

	    if (((par,n,p) in peer_port) && pp!=peer_port[par,n,p])
		fail("port endpoint can not be bound twice: "\
			par ":" n ":" p " (" peer_port[par,n,p] ")!=(" pp ")") 
	    peer_port[par,n,p]=pp

	    if (((par,n,p) in peer_edge) && ed!=peer_edge[par,n,p])
		fail("endpoint can not be bound twice: "\
			par ":" n ":" p " (" peer_edge[par,n,p] ")!=(" ed ")") 
	    peer_edge[par,n,p]=ed

	    if (((par,n,p) in peer_dir) && dir!=peer_dir[par,n,p])
		fail("endpoint sex can not be bound twice: "\
			par ":" n ":" p " (" peer_dir[par,n,p] ")!=(" dir ")") 
	    peer_dir[par,n,p]=dir
	}

	# inplace edges
	if (edge_type[epar,ed] ~ /in-place/)
	{
	   # TODO: allow only exact matches for now
	   if (port_size[par,n,p]!=port_size[ppar,pn,pp])
	     if (eval(port_size[par,n,p]) != eval(port_size[ppar,pn,pp]))
	       fail("mismatch in port cardinality " n ":" p "->" pn ":" pp " "\
	   	eval(port_size[par,n,p]) "!=" eval(port_size[ppar,pn,pp]) "\n"\
		"IN-PLACE PORTS MUST HAVE SAME CARDINALITY")

	   if ((par,n,p) in inplace_idx); else
	   inplaces[par,n,inplace_idx[par,n,p]=++ninplaces[par,n,p]]=pp
	}

	# state edges
	if (edge_type[epar,ed] ~ /state/)
	{
	   register_stateport(f,par,n,p,dir)

	   if (n==pn && (port_restrict[par,n,p] || port_restrict[ppar,pn,pp]))
	   	fail("shmem/state ports can not be restricted!")

	   # TODO: allow only exact matches for now
	   if (port_size[par,n,p]!=port_size[ppar,pn,pp])
	     if (eval(port_size[par,n,p]) != eval(port_size[ppar,pn,pp]))
	       fail("mismatch in port cardinality " n ":" p "->" pn ":" pp " "\
	   	eval(port_size[par,n,p]) "!=" eval(port_size[ppar,pn,pp]) "\n"\
		"STATE PORTS MUST HAVE SAME CARDINALITY")

	}

	if (edge_type[epar,ed] ~ /fifo/) {
	    if (edge_type[epar,ed] ~ /^init/) {
	    	if ((par,n,p) in delayport_idx); else
	    	delayports[par,n,dir,delayport_idx[par,n,p]=++ndelayports[par,n,dir]]=p
	    	# XXX: propagate "delayedness" to the parent
	    	if (par!=f) if ((f,par,p) in delayport_idx); else
	   		delayports[f,par,dir,delayport_idx[f,par,p]=++ndelayports[f,par,dir]]=p

	    	if (dir=="read") {
	   		if ((par,n) in delayed_idx); else
	   		delayed[par,delayed_idx[par,n]=++ndelayed[par]]=n
	    	}
	    } else if (edge_type[epar,ed] ~ /^deinit/) {
	    	if ((par,n,p) in drainport_idx); else
	    	drainports[par,n,dir,drainport_idx[par,n,p]=++ndrainports[par,n,dir]]=p
	    	# XXX: propagate "drainfullness" to the parent
	    	if (par!=f) if ((f,par,p) in drainport_idx); else
	   		drainports[f,par,dir,drainport_idx[f,par,p]=++ndrainports[f,par,dir]]=p

	    	if (dir=="write") {
	   		if ((par,n) in drained_idx); else
	   		drained[par,drained_idx[par,n]=++ndrained[par]]=n
	    	}
	    } else { 
	# regular fifos
		register_fifoport(f,par,n,p,dir,edge_type[epar,ed])

	   	if ((par,n) in actors_idx); else
	   	actors[par,actors_idx[par,n]=++nactors[par]]=n
		if (!is_stateport(par,n,p) &&  portroles[par,n,p]--<=0)
	    	    if (strict==1) 
			fail("port " par ":" n ":" p " can not have more connections that roles")
	    }
	}
	#print par,n,p,"CONNECTED" >STDERR
}

func portdim(f,n,p, ps) { return split(port_size[f,n,p],ps,SUBSEP) }

func multiport_def(f,n,p) {
    if (is_multiport(f SUBSEP n,p))
    {
	 return multiport_tag(f,n,p) " " f "_" n "_" p
    }
    if (is_packetport(f,n,p))
    {
    	return port_type[f,n,p] "*"
    }
    return port_type[f,n,p]
}

func new_port(f,n,p,name, new) {
        if (!name) name=p
	if (!new) new="new"
	return multiport_def(f,n,p) " *" name "=" new " " multiport_def(f,n,p) get_portsizes(f,n,p) 
}
func delete_port(f,n,p,name, del) {
        if (!name) name=p
	if (!free) del="delete"
	return del " " name
}

func alloc_port(f,n,p,name, alloc) {
        if (!name) name=p
	if (!alloc) alloc="malloc";
	return multiport_def(f,n,p) " *" name "=(" multiport_def(f,n,p) "*)" alloc "(sizeof(" multiport_def(f,n,p) get_portsizes(f,n,p) "))" 
}

func free_port(f,n,p,name, free) {
        if (!name) name=p
	if (!free) free="free"
	return free "(" name ")"
}
func get_aportsizes(f,n,p, nd,ps,i,r) {
	#if (is_literal(f,n,p)) return
	nd=split(port_size[f,n,p],ps,SUBSEP) 
	# XXX: hack for ARM/EVP dumbass compilers
	if (eval(ps[1])==0) ps[1]="ZERO"
	r=r "[" (do_modi ? suff(port_modi[f,n,p]," ") : "") ps[1] "]"
	for (i=2; i<=nd; i++) {
		# XXX: hack for ARM/EVP dumbass compilers
		if (eval(ps[i])==0) ps[i]="ZERO"
		r=r "[" ps[i] "]"
	}
	return r
}


func get_portsizes(f,n,p, d,nd,ps,i,r) {
	#if (is_literal(f,n,p)) return
	nd=split(port_size[f,n,p],ps,SUBSEP) 
	if (!d) d=1
	for (i=d; i<=nd; i++) {
		# XXX: hack for ARM/EVP dumbass compilers
		if (eval(ps[i])==0) ps[i]="ZERO"
		r=r "[" ps[i] "]"
	}
	return r
}

func xml_portsizes(s,p,l, nd,ps,i) {
	nd=split(port_size[s,p],ps,SUBSEP) 
	for (i=1; i<=nd; i++)
		print space(l) "<size>" ps[i] "</size>"
}

func portsize(c,n,p,d, nd,ps,i,r) {
	#if (is_literal(c,n,p)) return 1
	nd=split(port_size[c,n,p],ps,SUBSEP) 
	if (d>nd) fail("no such dimension")
	if (d<=nd && d>0) return ps[d]
	for (i=1; i<=nd; i++)
		r=append(r,ps[i],"*")
	return r
}

func copy_port(c,dp,f,n,p, type,size,packetsize,calltype,mode,const, P)
{
	if (!P) P=p
	if (!type) type=port_type[f,n,P]
	if (!size) size=port_size[f,n,P]
	if (!packetsize) packetsize=port_packetsize[f,n,P]
	if (!calltype) calltype=port_calltype[f,n,P]
	if (!mode) mode=port_mode[f,n,P]
	if (const=="") const=port_const[f,n,P]

	# use typeinfo from the type files
	port_type[c,dp]=type
	port_size[c,dp]=size
	# mode stuff
	port_mode[c,dp]=mode

	port_calltype[c,dp]=calltype
	port_packetsize[c,dp]=packetsize

	port_typeid[c,dp]=port_typeid[f,n,P]
	# inherit modifiers from the union
	if (const) port_const[c,dp]=const

	port_inout[c,dp]=port_inout[f,n,P]
	port_volatile[c,dp]=port_volatile[f,n,P]
	port_constp[c,dp]=port_constp[f,n,P]
	port_volatilep[c,dp]=port_volatilep[f,n,P]
	port_static[c,dp]=port_static[f,n,P]
	port_restrict[c,dp]=port_restrict[f,n,P]

	data_modi[c,dp]=data_modi[f,n,P]
	port_modi[c,dp]=port_modi[f,n,P]

	ports[c,port_idx[c,dp]=++nports[c]]=dp

	if (const) register_inport(c,dp)
	else register_outport(c,dp)

	if (is_stateport(f,n,P)) state_port[c,dp]=1

	port_source[c,dp]=append(port_source[c,dp],n SUBSEP P)
}

func base(f,c) { return alt(node_stereotype[f,c],c) }

func derive_from(f,i, fr,a) {
	if (split(edge_from[f,i],fr,SUBSEP)!=2) fail("bad edge")
	fn=fr[1]; fp=fr[2]
	if (split(fn,a,"::")!=2) {
		fpar=f
		return
	}
	fpar=a[1]
	fn=a[2]
}

func derive_to(f,i, to,a) {
	if (split(edge_to[f,i],to,SUBSEP)!=2) fail("bad edge")
	tn=to[1]; tp=to[2]
	if (split(tn,a,"::")!=2) {
		tpar=f
		return
	}
	tpar=a[1]
	tn=a[2]
}

@include xml-parser.awk