scan.c 6.4 KB

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