1
+ <?php
2
+ if (count ($ argv ) < 2 )
3
+ die ("Usage: {@ $ argv [0 ]} code.cu \n" );
4
+
5
+ $ code = file_get_contents ($ argv [1 ]);
6
+
7
+ $ jobs = preg_match_all ('/class\s+(\w+?)\: public AbstractJob\s*{(.*?)};/sm ' , $ code , $ matches , PREG_SET_ORDER );
8
+ // print_r($matches);
9
+ $ count = count ($ matches );
10
+
11
+ /// Part1: Job types enum
12
+ $ enum = [];
13
+ for ($ i =0 ;$ i < $ count ; ++$ i ) {
14
+ $ jobname = $ matches [$ i ][1 ];
15
+ $ jobname = str_replace ("Job " , "" , $ jobname );
16
+ $ enum [] = $ jobname ;
17
+ }
18
+ $ enum_str = "enum JobTypes { \n\tAbstract, \n\t" ;
19
+ $ enum_str .= join (", \n\t" , $ enum );
20
+ $ enum_str .= "\n}; \n" ;
21
+
22
+ echo $ enum_str ;
23
+
24
+
25
+
26
+ /// Part2: Iter call function
27
+ $ iter =[];
28
+ for ($ i =0 ;$ i <$ count ; ++$ i )
29
+ $ iter []= "if (job->job_type == JobTypes:: {$ enum [$ i ]}) \n\t(( {$ enum [$ i ]}Job *)job)->iter(id, d_offsets, d_edges, n, m); " ;
30
+
31
+ $ iter_str = join ("\nelse " , $ iter );
32
+ $ iter_str .= PHP_EOL . 'else printf("Unsupported job type!\n"); ' . PHP_EOL ;
33
+ echo $ iter_str ;
34
+
35
+ /// Part3: FusionJob union
36
+ $ typedata = [];
37
+ for ($ i =0 ;$ i <$ count ;++$ i ) {
38
+ preg_match ('#//PRAGMA: job data\n(.*?)\n//PRAGMA#ms ' , $ matches [$ i ][2 ], $ data );
39
+ $ data =$ data [1 ];
40
+ $ data =explode ("\n" , $ data );
41
+ $ data =array_map ('trim ' , $ data );
42
+
43
+ $ typedata [$ i ] = [];
44
+ foreach ($ data as $ datum ) {
45
+ preg_match ('/(.*?)\s+(.*?);/ ' , $ datum , $ match );
46
+ $ type = $ match [1 ];
47
+ $ name = $ match [2 ];
48
+ while ($ name [0 ] == '* ' )
49
+ {
50
+ $ name = substr ($ name , 1 );
51
+ $ type .= "* " ;
52
+ if (!(substr ($ type , -1 ) == '* ' or $ type == 'i64 ' or $ type =='double ' or $ type == 'u64 ' ))
53
+ echo "Warning: Type ' {$ type }' may not be 8 bytes. I suggest using only 8 byte types in jobs. \n" ;
54
+ }
55
+ $ typedata [$ i ][] = ['type ' => $ type , 'name ' => $ name ];
56
+ }
57
+ }
58
+ $ max = 0 ;
59
+ for ($ i =0 ; $ i <count ($ typedata ); ++$ i ) {
60
+ $ max =max ($ max , count ($ typedata [$ i ]));
61
+ }
62
+ // print_r($typedata);
63
+ $ union = [];
64
+ for ($ i =0 ;$ i <$ max ;++$ i ) {
65
+ $ values = [];
66
+ for ($ j =0 ;$ j <$ count ; ++$ j ) {
67
+ if (isset ($ typedata [$ j ][$ i ]))
68
+ $ values [] = $ typedata [$ j ][$ i ]['type ' ] . " " . $ typedata [$ j ][$ i ]['name ' ];
69
+ }
70
+ while (count ($ values ) > 1 && $ values [0 ] == $ values [1 ]) {
71
+ array_shift ($ values );
72
+ }
73
+ $ union [$ i ] = $ values ;
74
+
75
+ }
76
+ // print_r($union);
77
+
78
+ $ union_str = "struct FusionJob: public AbstractJob { \n" ;
79
+ foreach ($ union as $ u ) {
80
+ if (count ($ u ) > 1 ) {
81
+ $ union_str .= "\tunion { \n" ;
82
+ foreach ($ u as $ item ) {
83
+ $ union_str .= "\t\t{$ item }; \n" ;
84
+ }
85
+ $ union_str .= "\t}; \n" ;
86
+ }
87
+ else
88
+ $ union_str .= "\t{$ u [0 ]}; \n" ;
89
+ }
90
+ $ union_str .= "}; \n" ;
91
+ echo $ union_str ;
0 commit comments