scan.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "ack.h"
  9. #include "list.h"
  10. #include "data.h"
  11. enum f_path getpath(trf **first)
  12. {
  13. /* Try to find a transformation path */
  14. start_scan();
  15. /*
  16. The end result is the chaining of
  17. the consequtive phases with the t_next field.
  18. The list is scanned for possible transformations
  19. stopping at stopsuffix or the last transformation in the list.
  20. The scan flags are set by this process.
  21. When a transformation is found, it is compared with
  22. the last transformation found.
  23. */
  24. try(l_first(tr_list),p_suffix);
  25. return scan_end(first);
  26. }
  27. /******************** data used only while scanning *******************/
  28. static int last_pcount; /* The added priority of
  29. the best path so far */
  30. static int last_ncount; /* The # of non-optimizing transformations
  31. in the best path sofar */
  32. static int last_ocount; /* The # of optimizing transformations in the
  33. best path sofar */
  34. static int suf_found; /* Was the suffix at least recognized ? */
  35. /******************** The hard work ********************/
  36. void start_scan()
  37. {
  38. register list_elem *scan ;
  39. scanlist(l_first(tr_list),scan) {
  40. t_cont(*scan)->t_scan=NO ;
  41. }
  42. suf_found= 0 ;
  43. #ifdef DEBUG
  44. if ( debug>=3 ) vprint("Scan_start\n");
  45. #endif
  46. last_ncount= -1 ;
  47. last_ocount= 0 ;
  48. }
  49. void try(list_elem *f_scan, char *suffix)
  50. {
  51. register list_elem *scan ;
  52. register trf *trafo ;
  53. /* Try to find a transformation path starting at f_scan for a
  54. file with the indicated suffix.
  55. If the suffix is already reached or a combiner is found
  56. call scan_found() to OK the scan.
  57. If a transformation is found it calls itself recursively
  58. with as starting point the next transformation in the list.
  59. */
  60. if ( stopsuffix && *stopsuffix && strcmp(stopsuffix,suffix)==0 ) {
  61. scan_found();
  62. return ;
  63. }
  64. if (! f_scan) return;
  65. scanlist(f_scan, scan) {
  66. trafo= t_cont(*scan) ;
  67. if ( satisfy(trafo,suffix) ) {
  68. /* Found a transformation */
  69. suf_found= 1;
  70. #ifdef DEBUG
  71. if ( debug>=4 ) {
  72. vprint("Found %s for %s: result %s\n",
  73. trafo->t_name,suffix,trafo->t_out);
  74. }
  75. #endif
  76. trafo->t_scan=YES ;
  77. if ( trafo->t_prep ) {
  78. if ( !cpp_trafo ) {
  79. find_cpp() ;
  80. }
  81. if ( stopsuffix &&
  82. strcmp(stopsuffix,
  83. cpp_trafo->t_out)==0 )
  84. {
  85. scan_found() ;
  86. return ;
  87. }
  88. }
  89. if ( trafo->t_next ) {
  90. /* We know what happens from this phase on,
  91. so take a shortcut.
  92. */
  93. register trf *sneak ;
  94. sneak= trafo ;
  95. while( (sneak=sneak->t_next) ) {
  96. sneak->t_scan=YES ;
  97. }
  98. scan_found() ;
  99. sneak= trafo ;
  100. while( (sneak=sneak->t_next) ) {
  101. sneak->t_scan=NO ;
  102. }
  103. return ;
  104. }
  105. if ( trafo->t_linker && stopsuffix && !*stopsuffix ) {
  106. trafo->t_scan=NO ;
  107. scan_found() ;
  108. return ;
  109. }
  110. if (! l_next(*scan) && !stopsuffix) {
  111. scan_found() ;
  112. } else {
  113. try(l_next(*scan),trafo->t_out);
  114. }
  115. trafo->t_scan= NO ;
  116. }
  117. }
  118. }
  119. void scan_found()
  120. {
  121. register list_elem *scan;
  122. int ncount, ocount, pcount ;
  123. suf_found= 1;
  124. #ifdef DEBUG
  125. if ( debug>=3 ) vprint("Scan found\n") ;
  126. #endif
  127. /* Gather data used in comparison */
  128. ncount=0; ocount=0; pcount=0;
  129. scanlist(l_first(tr_list),scan) {
  130. if (t_cont(*scan)->t_scan) {
  131. #ifdef DEBUG
  132. if ( debug>=4 ) vprint("%s-",t_cont(*scan)->t_name) ;
  133. #endif
  134. if( t_cont(*scan)->t_optim ) ocount++ ;else ncount++ ;
  135. if( t_cont(*scan)->t_optim>Optlevel ) pcount-- ;
  136. pcount += t_cont(*scan)->t_priority ;
  137. }
  138. }
  139. #ifdef DEBUG
  140. if ( debug>=4 ) vprint("\n");
  141. #endif
  142. /* Is this transformation better then any found yet ? */
  143. #ifdef DEBUG
  144. if ( debug>=3 ) {
  145. vprint("old n:%d, o:%d, p:%d - new n:%d, o:%d, p:%d\n",
  146. last_ncount,last_ocount,last_pcount,
  147. ncount,ocount,pcount) ;
  148. }
  149. #endif
  150. if ( last_ncount== -1 || /* None found yet */
  151. last_pcount<pcount || /* Better priority */
  152. ( last_pcount==pcount && /* Same prio, and */
  153. ( last_ncount>ncount || /* Shorter nec. path */
  154. (last_ncount==ncount && /* Same nec. path, optimize?*/
  155. (Optlevel? last_ocount<ocount : last_ocount>ocount ))))) {
  156. /* Yes it is */
  157. #ifdef DEBUG
  158. if ( debug>=3 ) vprint("Better\n");
  159. #endif
  160. scanlist(l_first(tr_list),scan) {
  161. t_cont(*scan)->t_bscan=t_cont(*scan)->t_scan;
  162. }
  163. last_ncount=ncount; last_ocount=ocount; last_pcount=pcount;
  164. }
  165. }
  166. int satisfy(trf *trafo, char *suffix)
  167. {
  168. register char *f_char, *l_char ;
  169. /* Check whether this transformation is present for
  170. the current machine and the parameter suffix is among
  171. the input suffices. If so, return 1. 0 otherwise
  172. */
  173. if ( trafo->t_isprep ) return 0 ;
  174. l_char=trafo->t_in ;
  175. while ( l_char ) {
  176. f_char= l_char ;
  177. if ( *f_char!=SUFCHAR || ! *(f_char+1) ) {
  178. fuerror("Illegal input suffix entry for %s",
  179. trafo->t_name) ;
  180. }
  181. l_char=strchr(f_char+1,SUFCHAR);
  182. if ( l_char ) *l_char = 0;
  183. if ( strcmp(f_char,suffix)==0 ) {
  184. if ( l_char ) *l_char = SUFCHAR;
  185. return 1;
  186. }
  187. if ( l_char ) *l_char = SUFCHAR;
  188. }
  189. return 0 ;
  190. }
  191. enum f_path scan_end(trf **first)
  192. { /* Finalization */
  193. /* Return value indicating whether a transformation was found */
  194. /* Set the flags for the transformation up to, but not including,
  195. the combiner
  196. */
  197. register trf *prev, *curr ;
  198. register list_elem *scan;
  199. #ifdef DEBUG
  200. if ( debug>=3 ) vprint("End_scan\n");
  201. #endif
  202. if ( last_ncount== -1 ) return suf_found ? FP_NOPATH : FP_NOMATCH ;
  203. #ifdef DEBUG
  204. if ( debug>=2 ) vprint("Transformation found\n");
  205. #endif
  206. prev= (trf *)0 ; *first= prev ;
  207. scanlist(l_first(tr_list),scan) {
  208. curr= t_cont(*scan) ;
  209. if ( curr->t_bscan ) {
  210. if ( prev ) {
  211. prev->t_next= curr ;
  212. if ( curr->t_linker ) prev->t_keep=YES ;
  213. } else {
  214. *first= curr ;
  215. }
  216. if ( curr->t_next ) {
  217. return FP_OK ;
  218. }
  219. prev=curr ;
  220. }
  221. }
  222. if ( cpp_trafo && stopsuffix &&
  223. strcmp(stopsuffix,cpp_trafo->t_out)==0 ) {
  224. cpp_trafo->t_keep=YES ;
  225. }
  226. if ( prev ) {
  227. prev->t_keep=YES ;
  228. }
  229. return FP_OK ;
  230. }
  231. void find_cpp()
  232. {
  233. register list_elem *elem ;
  234. scanlist( l_first(tr_list), elem ) {
  235. if ( t_cont(*elem)->t_isprep ) {
  236. if ( cpp_trafo ) fuerror("Multiple cpp's present") ;
  237. cpp_trafo= t_cont(*elem) ;
  238. }
  239. }
  240. if ( !cpp_trafo ) fuerror("No cpp present") ;
  241. }