scan.c 6.4 KB

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