anyreg-cmacro.sl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. (*
  2. "% ANYREG-CMACRO.SL - Table-driven Anyreg and C-macro expander
  3. %
  4. % Author: Eric Benson
  5. % Symbolic Computation Group
  6. % Computer Science Dept.
  7. % University of Utah
  8. % Date: 17 December 1981
  9. % Copyright (c) 1981 University of Utah
  10. %")
  11. (fluid '(ResultingCode!* TempLabel!* TempLabel2!*))
  12. (* "Generated code is collected in reverse order in ResultingCode*")
  13. (CompileTime (flag '(SafePair PatternSublA WConstEvaluabLis
  14. AnyregPatternMatch1 MatchAll AnyregSubstitute1
  15. TempLabelGen
  16. CMacroSubstitute1)
  17. 'InternalFunction))
  18. (dm DefAnyreg (Form)
  19. (prog (AnyregName FunctionName Pattern)
  20. (setq Form (cdr Form))
  21. (setq AnyregName (car Form))
  22. (setq Form (cdr Form))
  23. (setq FunctionName (car Form))
  24. (setq Pattern (cdr Form))
  25. (return (list 'progn
  26. (list 'put
  27. (MkQuote AnyregName)
  28. '(quote AnyregResolutionFunction)
  29. (MkQuote FunctionName))
  30. (list 'put
  31. (MkQuote AnyregName)
  32. '(quote AnyregPatternTable)
  33. (MkQuote Pattern))))))
  34. (dm DefCMacro (Form)
  35. (prog (CMacroName Pattern)
  36. (setq Form (cdr Form))
  37. (setq CMacroName (car Form))
  38. (setq Pattern (cdr Form))
  39. (return (list 'progn
  40. (list 'flag
  41. (MkQuote (list CMacroName))
  42. '(quote MC))
  43. (list 'put
  44. (MkQuote CMacroName)
  45. '(quote CMacroPatternTable)
  46. (MkQuote Pattern))))))
  47. (de ResolveOperand (Register Source)
  48. (prog (ResolveAnyregFunction)
  49. (return (cond ((IDP Source) (ResolveWConst Source))
  50. ((atom Source) Source)
  51. ((FlagP (car Source) 'TerminalOperand) Source)
  52. ((setq ResolveAnyregFunction
  53. (get (car Source) 'AnyregResolutionFunction))
  54. (Apply ResolveAnyregFunction
  55. (cons Register (cdr Source))))
  56. (t (ResolveWConst Source))))))
  57. (de ResolveWConst (Expression)
  58. (prog (ResolvedExpression)
  59. (setq ResolvedExpression (ResolveWConstExpression Expression))
  60. (return (cond ((NumberP ResolvedExpression) ResolvedExpression)
  61. (t (list 'Immediate Expression))))))
  62. (de ResolveWConstExpression (Expression)
  63. (cond ((EqCar Expression 'WConst)
  64. (ResolveWConstExpression (cadr Expression)))
  65. (t (prog (ResultExpression)
  66. (return
  67. (cond
  68. ((or (NumberP Expression) (StringP Expression)) Expression)
  69. ((IDP Expression)
  70. (cond ((setq ResultExpression (get Expression 'WConst))
  71. ResultExpression)
  72. (t Expression)))
  73. (t (progn
  74. (cond
  75. ((MacroP (car Expression))
  76. (return
  77. (ResolveWConstExpression (Apply (car Expression)
  78. (list Expression))))))
  79. (setq Expression
  80. (cons (car Expression)
  81. (MapCar (cdr Expression)
  82. (Function ResolveWConstExpression))))
  83. (cond ((setq ResultExpression
  84. (WConstEvaluable Expression))
  85. ResultExpression)
  86. (t Expression))))))))))
  87. (de WConstEvaluable (Expression)
  88. (prog (WC WCLis DoFn)
  89. (return
  90. (cond ((NumberP Expression) Expression)
  91. ((and (IDP Expression) (setq WC (get Expression 'WConst)))
  92. WC)
  93. ((and (PairP Expression) (IDP (setq WC (car Expression))))
  94. (cond ((MacroP WC)
  95. (WConstEvaluable (apply (car Expression)
  96. (list Expression))))
  97. ((and (or (and (setq DoFn (get WC 'DoFn))
  98. (setq WC DoFn))
  99. (not (FUnBoundP WC)))
  100. (not (eq (setq WCLis
  101. (WConstEvaluabLis (cdr
  102. Expression)))
  103. 'not)))
  104. (Eval (cons WC WCLis)))
  105. (T NIL)))
  106. (T NIL)))))
  107. (de WConstEvaluabLis (ExpressionTail)
  108. (prog (WC WCLis)
  109. (return
  110. (cond ((null ExpressionTail) NIL)
  111. ((not (setq WC (WConstEvaluable (car ExpressionTail)))) 'not)
  112. ((eq (setq WCLis (WConstEvaluabLis (cdr ExpressionTail)))
  113. 'not)
  114. 'not)
  115. (T (cons WC WCLis))))))
  116. (de OneOperandAnyreg (Register Source AnyregName)
  117. (ExpandOneArgumentAnyreg Register
  118. (ResolveOperand Register Source)
  119. AnyregName))
  120. (* "SecondArg must not require a register for evaluation.
  121. It is currently used only for (MEMORY reg const).")
  122. (de TwoOperandAnyreg (Register Source SecondArg AnyregName)
  123. (ExpandTwoArgumentAnyreg Register
  124. (ResolveOperand Register Source)
  125. (ResolveOperand '(REG Error) SecondArg)
  126. AnyregName))
  127. (de ExpandOneArgumentAnyreg (Register Source AnyregName)
  128. (AnyregPatternExpand (list Register Source)
  129. (get AnyregName 'AnyregPatternTable)))
  130. (de ExpandTwoArgumentAnyreg (Register Source SecondArg AnyregName)
  131. (AnyregPatternExpand (list Register Source SecondArg)
  132. (get AnyregName 'AnyregPatternTable)))
  133. (de ExpandThreeArgumentAnyreg (Register Source SecondArg ThirdArg AnyregName)
  134. (AnyregPatternExpand (list Register Source SecondArg ThirdArg)
  135. (get AnyregName 'AnyregPatternTable)))
  136. (de AnyregPatternExpand (ArgumentList PatternTable)
  137. (AnyregSubstitute ArgumentList
  138. (AnyregPatternMatch (cdr ArgumentList) PatternTable)))
  139. (* "The label operand must not require a register to resolve.")
  140. (de Expand2OperandAndLabelCMacro (Arg1 Arg2 Label CMacroName)
  141. (prog (ResultingCode!*)
  142. (return (CMacroPatternExpand (list (ResolveOperand '(REG t1) Arg1)
  143. (ResolveOperand '(REG t2) Arg2)
  144. (ResolveOperand '(REG Error) Label))
  145. (get CMacroName 'CMacroPatternTable)))))
  146. (de Expand4OperandCMacro (Arg1 Arg2 Arg3 Arg4 CMacroName)
  147. (prog (ResultingCode!*)
  148. (return (CMacroPatternExpand (list (ResolveOperand '(REG t1) Arg1)
  149. (ResolveOperand '(REG t2) Arg2)
  150. (ResolveOperand '(REG Error) Arg3)
  151. (ResolveOperand '(REG Error) Arg4))
  152. (get CMacroName 'CMacroPatternTable)))))
  153. (de Expand2OperandCMacro (Arg1 Arg2 CMacroName)
  154. (prog (ResultingCode!*)
  155. (return (CMacroPatternExpand (list (ResolveOperand '(REG t1) Arg1)
  156. (ResolveOperand '(REG t2) Arg2))
  157. (get CMacroName 'CMacroPatternTable)))))
  158. (de Expand1OperandCMacro (Arg1 CMacroName)
  159. (prog (ResultingCode!*)
  160. (return (CMacroPatternExpand (list (ResolveOperand '(REG t1) Arg1))
  161. (get CMacroName 'CMacroPatternTable)))))
  162. (de CMacroPatternExpand (ArgumentList PatternTable)
  163. (CMacroSubstitute ArgumentList
  164. (AnyregPatternMatch ArgumentList PatternTable)))
  165. (de AnyregPatternMatch (ArgumentList PatternTable)
  166. (cond ((null (cdr PatternTable)) (car PatternTable))
  167. ((AnyregPatternMatch1 ArgumentList (caar PatternTable))
  168. (cdar PatternTable))
  169. (t (AnyregPatternMatch ArgumentList (cdr PatternTable)))))
  170. (de AnyregPatternMatch1 (ArgumentList PredicateOrPredicateList)
  171. (cond ((atom PredicateOrPredicateList)
  172. (Apply PredicateOrPredicateList ArgumentList))
  173. (t (MatchAll ArgumentList PredicateOrPredicateList))))
  174. (de MatchAll (ArgumentList PredicateList)
  175. (or (atom ArgumentList)
  176. (atom PredicateList)
  177. (and (Apply (car PredicateList) (list (car ArgumentList)))
  178. (MatchAll (cdr ArgumentList) (cdr PredicateList)))))
  179. (de AnyregSubstitute (ArgumentList CodeAndAddressExpressionList)
  180. (AnyregSubstitute1 (SafePair '(Register Source ArgTwo ArgThree)
  181. ArgumentList)
  182. CodeAndAddressExpressionList))
  183. (de AnyregSubstitute1 (NameExpressionAList CodeAndAddressExpressionList)
  184. (cond ((null (cdr CodeAndAddressExpressionList))
  185. (SublA NameExpressionAList (car CodeAndAddressExpressionList)))
  186. (t (progn (setq ResultingCode!*
  187. (cons (SublA NameExpressionAList
  188. (car CodeAndAddressExpressionList))
  189. ResultingCode!*))
  190. (AnyregSubstitute1 NameExpressionAList
  191. (cdr CodeAndAddressExpressionList))))))
  192. (de CMacroSubstitute (ArgumentList CodeTemplateList)
  193. (prog (TempLabel!* TempLabel2!*)
  194. (return (CMacroSubstitute1 (SafePair '(ArgOne ArgTwo
  195. ArgThree
  196. ArgFour
  197. ArgFive)
  198. ArgumentList)
  199. CodeTemplateList))))
  200. (de CMacroSubstitute1 (NameExpressionAList CodeTemplateList)
  201. (cond ((null CodeTemplateList) (ReversIP ResultingCode!*))
  202. (t (progn (setq ResultingCode!*
  203. (cons (PatternSublA NameExpressionAList
  204. (car CodeTemplateList))
  205. ResultingCode!*))
  206. (CMacroSubstitute1 NameExpressionAList
  207. (cdr CodeTemplateList))))))
  208. (de SafePair (CarList CdrList)
  209. (cond ((and (PairP CarList) (PairP CdrList))
  210. (cons (cons (car CarList) (car CdrList))
  211. (SafePair (cdr CarList) (cdr CdrList))))
  212. (t NIL)))
  213. (de PatternSublA (AList Expression)
  214. (prog (X)
  215. (return (cond ((null Expression) Expression)
  216. ((atom Expression)
  217. (cond ((eq Expression 'TempLabel)
  218. (TempLabelGen 'TempLabel!*))
  219. ((eq Expression 'TempLabel2)
  220. (TempLabelGen 'TempLabel2!*))
  221. ((setq X (atsoc Expression AList))
  222. (cdr X))
  223. (t Expression)))
  224. (t (cons (PatternSublA AList (car Expression))
  225. (PatternSublA AList (cdr Expression))))))))
  226. (de TempLabelGen (X)
  227. ((lambda (Y)
  228. (cond ((StringP Y) Y)
  229. (T (set X (StringGensym)))))
  230. (Eval X)))