common-cmacros.sl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. (*
  2. "% COMMON-CMACROS.SL - C-macros and Anyregs common to all implementations
  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 '(NAlloc!* AddressingUnitsPerItem StackDirection ResultingCode!*))
  12. (de !*Link (FunctionName FunctionType NumberOfArguments)
  13. (list (cond ((FlagP FunctionName 'ForeignFunction)
  14. (list '!*ForeignLink
  15. FunctionName
  16. FunctionType
  17. NumberOfArguments))
  18. (t (list '!*Call FunctionName)))))
  19. (DefCMacro !*Link)
  20. (de !*Call (FunctionName)
  21. (prog (ResultingCode!* OpenCodeSequence)
  22. (return (cond ((setq OpenCodeSequence
  23. (get FunctionName 'OpenCode))
  24. OpenCodeSequence)
  25. (t (CMacroPatternExpand (list FunctionName)
  26. (get '!*Call
  27. 'CMacroPatternTable)))))))
  28. (de !*LinkE (DeAllocCount FunctionName FunctionType NumberOfArguments)
  29. (cons (list '!*DeAlloc DeAllocCount)
  30. (cond ((FlagP FunctionName 'ForeignFunction)
  31. (list (list '!*ForeignLink
  32. FunctionName
  33. FunctionType
  34. NumberOfArguments)
  35. '(!*Exit 0)))
  36. (t (list (list '!*JCall FunctionName))))))
  37. (DefCMacro !*LinkE)
  38. (de !*JCall (FunctionName)
  39. (prog (ResultingCode!* OpenCodeSequence)
  40. (return (cond ((setq OpenCodeSequence
  41. (get FunctionName 'ExitOpenCode))
  42. OpenCodeSequence)
  43. ((setq OpenCodeSequence
  44. (get FunctionName 'OpenCode))
  45. (Append OpenCodeSequence (list '(!*Exit 0))))
  46. (t (CMacroPatternExpand (list FunctionName)
  47. (get '!*JCall
  48. 'CMacroPatternTable)))))))
  49. (de !*DeAlloc (DeAllocCount)
  50. (Expand1OperandCMacro (times DeAllocCount AddressingUnitsPerItem)
  51. '!*DeAlloc))
  52. (de !*Alloc (N)
  53. (progn (setq NAlloc!* N)
  54. (Expand1OperandCMacro (times N AddressingUnitsPerItem) '!*Alloc)))
  55. (de !*Exit (N)
  56. (Expand1OperandCMacro (times N AddressingUnitsPerItem) '!*Exit))
  57. (de !*JumpWithin (Label LowerBound UpperBound)
  58. (prog (ExitLabel)
  59. (setq ExitLabel (list 'Label (GenSym)))
  60. (return (list (list '!*JumpWLessP ExitLabel '(Reg 1) LowerBound)
  61. (list '!*JumpWLeq Label '(Reg 1) UpperBound)
  62. (list '!*Lbl ExitLabel)))))
  63. (DefCMacro !*JumpWithin)
  64. (de !*ProgBind (FluidsList)
  65. (!*LamBind '(Registers) FluidsList))
  66. (DefCMacro !*ProgBind)
  67. (de !*FreeRstr (FluidsList)
  68. (Expand1OperandCMacro (length (cdr FluidsList)) '!*FreeRstr))
  69. (de !*Jump (Arg1)
  70. (Expand1OperandCMacro Arg1 '!*Jump))
  71. (de !*Lbl (Arg1)
  72. (cdr Arg1))
  73. (de !*Push (Arg1)
  74. (Expand1OperandCMacro Arg1 '!*Push))
  75. (de !*Pop (Arg1)
  76. (Expand1OperandCMacro Arg1 '!*Pop))
  77. (de !*Move (Source Destination)
  78. (prog (ResultingCode!* ResolvedDestination)
  79. (setq ResolvedDestination (ResolveOperand '(REG t2) Destination))
  80. (return
  81. (CMacroPatternExpand
  82. (list (ResolveOperand (cond ((RegisterP ResolvedDestination)
  83. ResolvedDestination)
  84. (t '(REG t1)))
  85. Source)
  86. ResolvedDestination)
  87. (get '!*Move 'CMacroPatternTable)))))
  88. (de !*JumpEQ (Label Arg1 Arg2)
  89. (Expand2OperandAndLabelCMacro Arg1 Arg2 Label '!*JumpEQ))
  90. (de !*JumpNotEQ (Label Arg1 Arg2)
  91. (Expand2OperandAndLabelCMacro Arg1 Arg2 Label '!*JumpNotEQ))
  92. (de !*JumpWLessP (Label Arg1 Arg2)
  93. (Expand2OperandAndLabelCMacro Arg1 Arg2 Label '!*JumpWLessP))
  94. (de !*JumpWGreaterP (Label Arg1 Arg2)
  95. (Expand2OperandAndLabelCMacro Arg1 Arg2 Label '!*JumpWGreaterP))
  96. (de !*JumpWLEQ (Label Arg1 Arg2)
  97. (Expand2OperandAndLabelCMacro Arg1 Arg2 Label '!*JumpWLEQ))
  98. (de !*JumpWGEQ (Label Arg1 Arg2)
  99. (Expand2OperandAndLabelCMacro Arg1 Arg2 Label '!*JumpWGEQ))
  100. (de !*JumpType (Label Arg TypeTag)
  101. (Expand2OperandAndLabelCMacro Arg
  102. (list 'WConst (get TypeTag 'WConst))
  103. Label
  104. '!*JumpType))
  105. (de !*JumpNotType (Label Arg TypeTag)
  106. (Expand2OperandAndLabelCMacro Arg
  107. (list 'WConst (get TypeTag 'WConst))
  108. Label
  109. '!*JumpNotType))
  110. (de !*JumpInType (Label Arg TypeTag)
  111. (Expand2OperandAndLabelCMacro Arg
  112. (list 'WConst (get TypeTag 'WConst))
  113. Label
  114. '!*JumpInType))
  115. (de !*JumpNotInType (Label Arg TypeTag)
  116. (Expand2OperandAndLabelCMacro Arg
  117. (list 'WConst (get TypeTag 'WConst))
  118. Label
  119. '!*JumpNotInType))
  120. (de !*MkItem (Arg1 Arg2)
  121. (Expand2OperandCMacro Arg1 Arg2 '!*MkItem))
  122. (de !*WPlus2 (Arg1 Arg2)
  123. (Expand2OperandCMacro Arg1 Arg2 '!*WPlus2))
  124. (de !*WDifference (Arg1 Arg2)
  125. (Expand2OperandCMacro Arg1 Arg2 '!*WDifference))
  126. (de !*WTimes2 (Arg1 Arg2)
  127. (prog (P)
  128. (return (cond ((and (or (EqCar Arg2 'Quote)
  129. (EqCar Arg2 'WConst))
  130. (setq P (PowerOf2P (cadr Arg2))))
  131. (!*AShift Arg1 (list (car Arg2) P)))
  132. (t (Expand2OperandCMacro Arg1 Arg2 '!*WTimes2))))))
  133. (* "PowerOf2P(X:integer):{integer,NIL}
  134. If X is a positive power of 2, log base 2 of X is returned. Otherwise
  135. NIL is returned.")
  136. (de PowerOf2P (X)
  137. (prog (N)
  138. (return (cond ((or (not (FixP X)) (MinusP X) (equal X 0)) NIL)
  139. (t (progn (setq N 0)
  140. (while (not (equal (lor x 1) x))
  141. (progn (setq N (add1 N))
  142. (setq X (lsh X -1))))
  143. (cond ((equal X 1) N) (T NIL))))))))
  144. (de !*AShift (Arg1 Arg2)
  145. (Expand2OperandCMacro Arg1 Arg2 '!*AShift))
  146. (de !*WShift (Arg1 Arg2)
  147. (Expand2OperandCMacro Arg1 Arg2 '!*WShift))
  148. (de !*WAnd (Arg1 Arg2)
  149. (Expand2OperandCMacro Arg1 Arg2 '!*WAnd))
  150. (de !*WOr (Arg1 Arg2)
  151. (Expand2OperandCMacro Arg1 Arg2 '!*WOr))
  152. (de !*WXOr (Arg1 Arg2)
  153. (Expand2OperandCMacro Arg1 Arg2 '!*WXOr))
  154. (de !*WMinus (Arg1 Arg2)
  155. (Expand2OperandCMacro Arg1 Arg2 '!*WMinus))
  156. (de !*WNot (Arg1 Arg2)
  157. (Expand2OperandCMacro Arg1 Arg2 '!*WNot))
  158. (de !*Loc (Arg1 Arg2)
  159. (Expand2OperandCMacro Arg1 Arg2 '!*Loc))
  160. (de !*Field (Arg1 Arg2 Arg3 Arg4)
  161. (Expand4OperandCMacro Arg1 Arg2 Arg3 Arg4 '!*Field))
  162. (de !*SignedField (Arg1 Arg2 Arg3 Arg4)
  163. (Expand4OperandCMacro Arg1 Arg2 Arg3 Arg4 '!*SignedField))
  164. (de !*PutField (Arg1 Arg2 Arg3 Arg4)
  165. (Expand4OperandCMacro Arg1 Arg2 Arg3 Arg4 '!*PutField))
  166. (de AnyregCAR (Register Source)
  167. (OneOperandAnyreg Register Source 'car))
  168. (de AnyregCDR (Register Source)
  169. (OneOperandAnyreg Register Source 'cdr))
  170. (de AnyregQUOTE (Register Source)
  171. (ExpandOneArgumentAnyreg Register Source 'quote))
  172. (de AnyregWVAR (Register Source)
  173. (ExpandOneArgumentAnyreg Register Source 'WVar))
  174. (de AnyregREG (Register Source)
  175. (ExpandOneArgumentAnyreg Register Source 'REG))
  176. (de AnyregWCONST (Register Source)
  177. (OneOperandAnyreg Register Source 'WConst))
  178. (DefAnyreg WCONST
  179. AnyregWCONST
  180. (SOURCE))
  181. (de AnyregFRAME (Register Source)
  182. (ExpandOneArgumentAnyreg Register
  183. (times StackDirection
  184. AddressingUnitsPerItem
  185. (difference 1 Source))
  186. 'Frame))
  187. (de AnyregFRAMESIZE (Register)
  188. (times NAlloc!* AddressingUnitsPerItem))
  189. (DefAnyreg FrameSize
  190. AnyregFRAMESIZE)
  191. (de AnyregMEMORY (Register Source ArgTwo)
  192. (TwoOperandAnyreg Register Source ArgTwo 'MEMORY))
  193. (flag '(FLUID !$FLUID GLOBAL !$GLOBAL ExtraReg Label) 'TerminalOperand)
  194. (fluid '(labelgen*)) % a-list of tags and labels
  195. % (labelgen tag) and (labelref tag) can be used as either ANYREG or CMACRO.
  196. % (labelgen tag) creates and returns a unique label, (labelref tag) returns
  197. % the same one. Useful for 'OpenCode lists.
  198. (de anyreglabelgen (reg name)
  199. ((lambda (lb al)
  200. (cond ((null al)
  201. (setq labelgen* (cons (cons name lb) labelgen*)))
  202. (t (rplacd al lb)))
  203. lb)
  204. (gensym)
  205. (assoc name labelgen*)))
  206. (defanyreg labelgen anyreglabelgen)
  207. (de labelgen (name)
  208. (list (anyreglabelgen nil name)))
  209. (defcmacro labelgen)
  210. (de anyreglabelref (reg name) (cdr (assoc name labelgen*)))
  211. (defanyreg labelref anyreglabelref)
  212. (de labelref (name)
  213. (list (anyreglabelref nil name)))
  214. (defcmacro labelref)