codgen.red 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. module codgen;
  2. % ------------------------------------------------------------------- ;
  3. % Copyright : J.A. van Hulzen, Twente University, Dept. of Computer ;
  4. % Science, P.O.Box 217, 7500 AE Enschede, The Netherlands.;
  5. % Author: J.A. van Hulzen. ;
  6. % ------------------------------------------------------------------- ;
  7. lisp$
  8. global '(!*for!* !*do!*)$ % Gentran-globals used in makedecs.
  9. global '(!*currout!*)$ % Gentran global used in redefinition
  10. % of symbolic procedure gentran.
  11. fluid '(!*gentranseg)$ % Gentran fluid introduced.
  12. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  13. % Patch 8 november 94 HvH.
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. put('c,'preproc,'preproc)$
  16. put('ratfor,'preproc,'preproc)$
  17. put('fortran,'preproc,'preproc)$
  18. put('pascal,'preproc,'preproc)$
  19. put('c,'parser,'gentranparse)$
  20. put('ratfor,'parser,'gentranparse)$
  21. put('fortran,'parser,'gentranparse)$
  22. put('pascal,'parser,'gentranparse)$
  23. put('c,'lispcode,'lispcode)$
  24. put('ratfor,'lispcode,'lispcode)$
  25. put('fortran,'lispcode,'lispcode)$
  26. put('pascal,'lispcode,'lispcode)$
  27. global '(!*wrappers!*)$
  28. !*wrappers!*:='(optimization segmentation)$
  29. symbolic procedure optimization forms;
  30. if !*gentranopt then opt forms else forms$
  31. symbolic procedure segmentation forms;
  32. if !*gentranseg then seg forms else forms$
  33. symbolic procedure gentran!-wrappers!* forms;
  34. begin
  35. if !*wrappers!* then
  36. foreach proc_name in !*wrappers!* do
  37. forms:=apply1(proc_name,forms);
  38. return forms
  39. end$
  40. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  41. %%%%
  42. %%%% Herbert's facility can now be added:
  43. %%%%
  44. %%%% !*wrappers!*:=append(list('differentiate),!*wrappers!*)$
  45. %%%% symbolic procedure differentiate forms;
  46. %%%% << load!-package adiff; adiff!-eval forms>>$
  47. %%%%
  48. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  49. symbolic procedure gentran(forms, flist);
  50. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  51. %%%% Redefinition of the main gentran procedure %%%%
  52. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  53. begin scalar !:print!-prec!: ; % Gentran ignores print_precision
  54. if flist then
  55. lispeval list('gentranoutpush, list('quote, flist));
  56. forms:=
  57. apply1(get(gentranlang!*,'preproc) or get('fortran,'preproc),
  58. list forms);
  59. apply1(get(gentranlang!*,'parser) or get('fortran,'parser),forms);
  60. forms:=
  61. apply1(get(gentranlang!*,'lispcode) or get('fortran,'lispcode),forms);
  62. forms:=gentran!-wrappers!* forms;
  63. apply1(get(gentranlang!*,'formatter) or get('fortran,'formatter),
  64. apply1(get(gentranlang!*,'codegen) or get('fortran,'codegen),
  65. forms));
  66. if flist then
  67. <<
  68. flist := car !*currout!* or ('list . cdr !*currout!*);
  69. lispeval '(gentranpop '(nil));
  70. return flist
  71. >>
  72. else
  73. return car !*currout!* or ('list . cdr !*currout!*)
  74. end$
  75. %=================================================================
  76. %=== The codgen.red module itself!!!
  77. %=================================================================
  78. symbolic procedure interchange_defs(def1,def2);
  79. begin scalar temp1,temp2;
  80. temp1:=getd def1; remd def1;
  81. temp2:=getd def2; remd def2;
  82. putd(def1,car temp2,cdr temp2);
  83. putd(def2,car temp1,cdr temp1);
  84. end$
  85. symbolic procedure strip_progn(lst);
  86. if pairp lst
  87. then if pairp(car lst) and caar(lst)='progn
  88. then cdar(lst)
  89. else if pairp(car lst) and
  90. caar(lst)='prog and
  91. cadar(lst)='nil
  92. then cddar(lst)
  93. else lst;
  94. symbolic procedure add_progn(lst);
  95. if pairp lst then append(list('progn),lst) else lst;
  96. switch gentranopt$
  97. !*gentranopt:=nil$
  98. fluid '(delaylist!* delayoptlist!* delaydecs!* !*gendecs !*period!*)$
  99. symbolic procedure delaydecs;
  100. % ------------------------------------------------------------------- ;
  101. % Effect: Redefinition of codegeneration functions. ;
  102. % ------------------------------------------------------------------- ;
  103. begin
  104. !*period!*:=!*period; !*period:=nil;
  105. delaydecs!*:=t; delaylist!*:=nil;
  106. symtabrem('!*main!*,'!*decs!*);
  107. symtabrem('!*main!*,'!*params!*);
  108. symtabrem('!*main!*,'!*type!*);
  109. !*wrappers!*:=
  110. delete('optimization,delete('segmentation,!*wrappers!*));
  111. interchange_defs('gentran,'gentran_delaydecs);
  112. end;
  113. put('delaydecs,'stat,'endstat)$
  114. symbolic procedure gentran_delaydecs(forms,flist);
  115. % ------------------------------------------------------------------- ;
  116. % This procedure replaces the gentran-evaluator when production of ;
  117. % delcarations has to be delayed. The results of all gentran eval.s ;
  118. % are collected in the list delaylist!* and processed together by ;
  119. % activating thre function make decs. ;
  120. % ------------------------------------------------------------------- ;
  121. begin
  122. forms:= apply1(get(gentranlang!*,'preproc) or
  123. get('fortran,'preproc),
  124. list forms);
  125. apply1(get(gentranlang!*,'parser) or get('fortran,'parser),forms);
  126. forms:= apply1(get(gentranlang!*,'lispcode) or
  127. get('fortran,'lispcode),
  128. forms);
  129. forms:=gentran!-wrappers!* forms;
  130. if !*gentranopt then forms:=opt strip_progn forms;
  131. if !*gentranseg then forms:=seg forms;
  132. forms:=strip_progn forms;
  133. if delaylist!*
  134. then delaylist!*:=append(delaylist!*,forms)
  135. else delaylist!*:=forms
  136. end;
  137. symbolic procedure makedecs;
  138. % ------------------------------------------------------------------- ;
  139. % Effect: Original situation restored. Template processing performed. ;
  140. % Symboltable cleaned up. ;
  141. % ------------------------------------------------------------------- ;
  142. begin scalar gentranopt,gentranseg;
  143. if delayoptlist!*
  144. then gentranerr(nil,nil,"DELAYOPT ACTIVE",nil)
  145. else
  146. << !*period:=!*period!*;
  147. !*gendecs:=t; delaydecs!*:=nil;
  148. gentranopt:=!*gentranopt;!*gentranopt:=nil;
  149. gentranseg:=!*gentranseg;!*gentranseg:=nil;
  150. interchange_defs('gentran,'gentran_delaydecs);
  151. delaylist!* := subst('for,!*for!*, delaylist!*); % JB 9/3/94
  152. delaylist!* := subst('do, !*do!*, delaylist!*); % JB 9/3/94
  153. apply('gentran,list(add_progn delaylist!*,nil));
  154. delaylist!*:=nil;
  155. !*wrappers!*:=
  156. append(!*wrappers!*,list('optimization,'segmentation));
  157. !*gentranopt:=gentranopt;!*gentranseg:=gentranseg;
  158. >>
  159. end;
  160. put('makedecs,'stat,'endstat)$
  161. symbolic procedure delayopts;
  162. % ------------------------------------------------------------------- ;
  163. % This procedure allows to avoid optimization until further notice, ;
  164. % i.e. until the command makeopts is executed. ;
  165. % All gentran evaluations are collected in the list delayoptlist!*. ;
  166. % Through makeopts this colection is processed in one run. ;
  167. % ------------------------------------------------------------------- ;
  168. begin
  169. if not delaydecs!*
  170. then !*wrappers!*:=
  171. delete('optimization,delete('segmentation,!*wrappers!*));
  172. interchange_defs('gentran,'gentran_delayopt);
  173. delayoptlist!*:=nil
  174. end;
  175. put('delayopts,'stat,'endstat)$
  176. symbolic procedure gentran_delayopt(forms,flist);
  177. % ------------------------------------------------------------------- ;
  178. % This procedure replaces the current gentran evaluator when produc- ;
  179. % tion of optimizwd code has to be delayed. We informally introduce a ;
  180. % two-pass evaluation mechanism by doing so: one for gentran treatable;
  181. % prefix statements and a second for optimization of this set of sta- ;
  182. % tements. ;
  183. % ------------------------------------------------------------------- ;
  184. begin
  185. forms:= apply1(get(gentranlang!*,'preproc) or
  186. get('fortran,'preproc),
  187. list forms);
  188. apply1(get(gentranlang!*,'parser) or get('fortran,'parser),forms);
  189. if delayoptlist!*
  190. then delayoptlist!*:=
  191. append(delayoptlist!*,
  192. strip_progn(gentran!-wrappers!* lispcode forms))
  193. else delayoptlist!*:=strip_progn(gentran!-wrappers!* lispcode forms);
  194. end;
  195. symbolic procedure makeopts;
  196. % ------------------------------------------------------------------- ;
  197. % The previous gentran environment is restored and the list of state- ;
  198. % ments delayoptlist!* is treated in this environment. ;
  199. % ------------------------------------------------------------------- ;
  200. begin scalar gendecs,gentranopt;
  201. interchange_defs('gentran,'gentran_delayopt);
  202. gentranopt:=!*gentranopt;!*gentranopt:=t;
  203. gendecs:=!*gendecs; !*gendecs:=nil;
  204. if delaydecs!*
  205. then
  206. if delaylist!*
  207. then delaylist!*:=
  208. append(delaylist!*,strip_progn opt delayoptlist!*)
  209. else delaylist!*:=strip_progn opt delayoptlist!*
  210. else << !*wrappers!*:=
  211. append(!*wrappers!*,list('optimization,'segmentation));
  212. apply('gentran,list(add_progn delayoptlist!*,nil))
  213. >>;
  214. delayoptlist!*:=nil; !*gentranopt:=gentranopt ; !*gendecs:=gendecs;
  215. end;
  216. put('makeopts,'stat,'endstat)$
  217. endmodule;
  218. end;