pass-1-lap.sl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. (*
  2. "% PASS-1-LAP.SL - Expand c-macros and allocate quoted expressions
  3. %
  4. % Author: Eric Benson
  5. % Symbolic Computation Group
  6. % Computer Science Dept.
  7. % University of Utah
  8. % Date: 14 December 1981
  9. % Copyright (c) 1981 University of Utah
  10. %
  11. % Added MCprint and InstructionPrint - MLG
  12. % <PSL.COMP>PASS-1-LAP.SL.17, 4-Aug-82 00:35:54, Edit by BENSON
  13. % Added bignum constants; won't work for cross-compilation, though
  14. %")
  15. (*
  16. "Pass1Lap takes a list of c-macros and instructions, and attempts to simplify
  17. them whenever possible. C-macros are expanded by APPLY(CAR X, CDR X), which
  18. will return another instruction list to be processed recursively by Pass1Lap.
  19. Quoted expressions are allocated at the end of the code, in the following way:
  20. In an instruction or c-macro
  21. (.... (QUOTE (A B C)) ...)
  22. the following is tacked onto the end of the constructed code list:
  23. L2
  24. (MKITEM ID A)
  25. (MKITEM PAIR L3)
  26. L3
  27. (MKITEM ID B)
  28. (MKITEM PAIR L4)
  29. L4
  30. (MKITEM ID C)
  31. (MKITEM ID NIL)
  32. If *ImmediateQuote is NIL, the quoted reference becomes:
  33. (... L1 ...)
  34. ...
  35. L1
  36. (fullword (MKITEM PAIR L2))
  37. Otherwise, it becomes:
  38. (... (immediate (MKITEM PAIR L2)) ...)")
  39. (fluid '(!*ImmediateQuote
  40. !*PCMAC
  41. !*PrintedOneCMacro
  42. Pass1CodeList
  43. Pass1ConstantList
  44. Pass1ConstantContentsList
  45. Pass1AddedCode
  46. EntryPoints!*
  47. AddressingUnitsPerItem
  48. LastActualReg!&))
  49. (CompileTime (flag '(Pass1Code OneLapPass1 AddInstruction
  50. ExpandPseudoOps ExpandOnePseudoOp
  51. GenerateLabel GenerateCodeLabel AddCodeLabel AddCode
  52. ExpandQuote1 ExpandImmediateQuote ExpandItem
  53. ExpandNonImmediateQuote SaveConstant SaveContents
  54. AppendConstants AppendOneConstant AppendItem
  55. AddFullWord AppendContents MakeMkItem)
  56. 'InternalFunction))
  57. (CompileTime (load fast-vector))
  58. (de Pass1Lap (InstructionList)
  59. (prog (Pass1CodeList
  60. Pass1ConstantList
  61. Pass1ConstantContentsList
  62. EntryPoints!*
  63. Pass1AddedCode)
  64. (setq Pass1CodeList (cons NIL NIL)) (* "Init a TCONC pointer")
  65. (setq Pass1ConstantContentsList (cons NIL NIL))
  66. (Pass1Code InstructionList) (* "Expand macros")
  67. (Pass1Code Pass1AddedCode)
  68. (AppendConstants) (* "Tack the constants on the end")
  69. (return (car Pass1CodeList))))
  70. (* "BuildConstant takes an S-expression and returns the LAP version of it.")
  71. (* "The car is the expanded item, cdr is the contents")
  72. (de BuildConstant (Expression)
  73. (prog (Pass1CodeList
  74. Pass1ConstantList
  75. Pass1ConstantContentsList
  76. ExpandedExpression)
  77. (setq Pass1CodeList (cons NIL NIL)) (* "Init a TCONC pointer")
  78. (setq Pass1ConstantContentsList (cons NIL NIL))
  79. (setq ExpandedExpression (ExpandItem Expression)) (* "Expand the item")
  80. (AppendConstants) (* "Tack the contents on the end")
  81. (return (cons ExpandedExpression (car Pass1CodeList)))))
  82. (de Pass1Code (InstructionList)
  83. (ForEach Instruction in InstructionList do (OneLapPass1 Instruction)))
  84. (de OneLapPass1 (Instruction)
  85. (cond ((atom Instruction) (AddCodeLabel Instruction))
  86. ((eq (car Instruction) '!*ENTRY)
  87. (progn (* "ENTRY directives are passed unchanged")
  88. (cond ((and (not (or (FlagP (second Instruction)
  89. 'InternalFunction)
  90. (equal (second Instruction)
  91. '**fasl**initcode**)))
  92. (null (car Pass1CodeList)))
  93. (* "Header word says how many arguments to expect")
  94. (AddCode (list 'FULLWORD (fourth Instruction)))))
  95. (setq EntryPoints!*
  96. (cons (second Instruction) EntryPoints!*))
  97. (cond (!*PCMAC (MCPrint Instruction)))
  98. (AddCode Instruction)))
  99. ((FlagP (car Instruction) 'MC)
  100. (progn (cond ((and !*PCMAC (not !*PrintedOneCMacro))
  101. (MCPrint Instruction)))
  102. ((lambda (!*PrintedOneCMacro)
  103. (Pass1Code (Apply (car Instruction)
  104. (cdr Instruction))))
  105. T)))
  106. (t (progn (cond (!*PCMAC (InstructionPrint Instruction)))
  107. (AddInstruction Instruction)))))
  108. (de MCPrint(x) (print x))
  109. (de InstructionPrint(x) (PrintF " %p%n" x))
  110. (de AddInstruction (Instruction)
  111. (AddCode (ExpandPseudoOps Instruction)))
  112. (de ExpandPseudoOps (X)
  113. (cond ((atom X) X)
  114. (t (cons (ExpandOnePseudoOp (car X))
  115. (ExpandPseudoOps (cdr X))))))
  116. (de ExpandOnePseudoOp (X)
  117. (prog (PseudoOpFunction)
  118. (return (cond ((atom X) X)
  119. ((setq PseudoOpFunction
  120. (get (car X) 'Pass1PseudoOp))
  121. (ExpandOnePseudoOp (Apply PseudoOpFunction
  122. (list X))))
  123. ((setq PseudoOpFunction (WConstEvaluable X))
  124. PseudoOpFunction)
  125. (t (cons (car X) (ExpandPseudoOps (cdr X))))))))
  126. (de PassOneUnImmediate (X)
  127. (progn (setq X (cadr X))
  128. (cond ((EqCar X 'Immediate) (cadr X))
  129. (t X))))
  130. (put 'UnImmediate 'Pass1PseudoOp 'PassOneUnImmediate)
  131. (de PassOneLabel (U)
  132. (cadr U))
  133. (put 'Label 'Pass1PseudoOp 'PassOneLabel)
  134. (de PassOneUnDeferred (X)
  135. (progn (setq X (cadr X))
  136. (cond ((EqCar X 'Deferred) (cadr X))
  137. (t X))))
  138. (put 'UnDeferred 'Pass1PseudoOp 'PassOneUnDeferred)
  139. (* "Removed because ExtraReg has to be processed differently by resident LAP"
  140. (de PassOneExtraReg (X)
  141. (progn (setq X (cadr X))
  142. (list 'plus2
  143. '(WArray ArgumentBlock)
  144. (times (difference (Add1 LastActualReg!&) X)
  145. AddressingUnitsPerItem))))
  146. (put 'ExtraReg 'Pass1PseudoOp 'PassOneExtraReg)
  147. )
  148. (de GenerateCodeLabel ()
  149. (prog (NewLabel)
  150. (setq NewLabel (GenerateLabel))
  151. (AddCodeLabel NewLabel)
  152. (return NewLabel)))
  153. (de GenerateLabel ()
  154. (StringGenSym))
  155. (de AddCodeLabel (Label)
  156. (AddCode Label))
  157. (de AddCode (C)
  158. (TConc Pass1CodeList C))
  159. (de ExpandLit (U)
  160. (prog (L)
  161. (cond ((setq L (FindPreviousLit (cdr U))) (return L)))
  162. (setq L (GenerateLabel))
  163. (setq Pass1AddedCode (NConc Pass1AddedCode
  164. (cons L (ForEach X in (cdr U) collect X))))
  165. (return L)))
  166. (de FindPreviousLit (U)
  167. (cond ((not (null (rest U))) NIL)
  168. (t (prog (L)
  169. (setq L Pass1AddedCode)
  170. (cond ((null L) (return NIL)))
  171. (setq U (first U))
  172. loop
  173. (cond ((null (rest L)) (return NIL)))
  174. (cond ((equal U (second L))
  175. (return (cond ((atom (first L)) (first L))
  176. (t (prog (B)
  177. (setq L (rest L))
  178. (rplacd L (cons (first L) (rest L)))
  179. (rplaca L (setq B (GenerateLabel)))
  180. (return B)))))))
  181. (setq L (rest L))
  182. (go loop)))))
  183. (put 'lit 'Pass1PseudoOp 'ExpandLit)
  184. (flag '(lit) 'TerminalOperand)
  185. (de ExpandQuote (QuotedExpression)
  186. (ExpandQuote1 (cadr QuotedExpression)))
  187. (put 'Quote 'Pass1PseudoOp 'ExpandQuote)
  188. (de ExpandQuote1 (Expression)
  189. (cond (!*ImmediateQuote (ExpandImmediateQuote Expression))
  190. (t (ExpandNonImmediateQuote Expression))))
  191. (de ExpandImmediateQuote (Expression)
  192. (list 'IMMEDIATE (ExpandItem Expression)))
  193. (de ExpandItem (Expression)
  194. (prog (LabelOfContents)
  195. (return (cond ((InumP Expression) Expression)
  196. ((IDP Expression)
  197. (MakeMkItem (TagNumber Expression)
  198. (list 'IDLoc Expression)))
  199. ((CodeP Expression)
  200. (MakeMkItem (TagNumber Expression)
  201. Expression))
  202. (t (progn (setq LabelOfContents
  203. (SaveContents Expression))
  204. (MakeMkItem (TagNumber Expression)
  205. LabelOfContents)))))))
  206. (de ExpandNonImmediateQuote (Expression)
  207. (SaveConstant Expression))
  208. (de SaveConstant (Expression)
  209. (prog (TableEntry)
  210. (return (cond ((setq TableEntry
  211. (Assoc Expression Pass1ConstantList))
  212. (cdr TableEntry))
  213. (t (progn (setq TableEntry (GenerateLabel))
  214. (setq Pass1ConstantList
  215. (cons (cons Expression
  216. TableEntry)
  217. Pass1ConstantList))
  218. TableEntry))))))
  219. (de SaveContents (Expression)
  220. (prog (TableEntry)
  221. (return (cond ((setq TableEntry
  222. (Assoc Expression
  223. (car Pass1ConstantContentsList)))
  224. (cdr TableEntry))
  225. (t (progn (setq TableEntry (GenerateLabel))
  226. (TConc Pass1ConstantContentsList
  227. (cons Expression TableEntry))
  228. TableEntry))))))
  229. (de AppendConstants ()
  230. (prog (TempCodeList)
  231. (cond ((not !*ImmediateQuote)
  232. (ForEach TableEntry in Pass1ConstantList do
  233. (AppendOneConstant TableEntry))))
  234. (setq TempCodeList Pass1CodeList)
  235. (setq Pass1CodeList (cons NIL NIL))
  236. (ForEach TableEntry in (car Pass1ConstantContentsList) do
  237. (AppendContents TableEntry))
  238. (* "The contents go on the begininning of the list")
  239. (LConc Pass1CodeList (car TempCodeList))))
  240. (de AppendOneConstant (ExpressionLabelPair)
  241. (progn (AddCodeLabel (cdr ExpressionLabelPair))
  242. (AppendItem (car ExpressionLabelPair))))
  243. (de AppendItem (Expression)
  244. (AddFullWord (ExpandItem Expression)))
  245. (de AddFullWord (Expression)
  246. (AddCode (list 'FULLWORD Expression)))
  247. (de AppendContents (ExpressionLabelPair)
  248. (prog (Expression UpperBound I)
  249. (AddCodeLabel (cdr ExpressionLabelPair))
  250. (setq Expression (car ExpressionLabelPair))
  251. (cond ((PairP Expression)
  252. (progn (AppendItem (car Expression))
  253. (AppendItem (cdr Expression))))
  254. ((StringP Expression)
  255. (progn (AddFullWord (Size Expression))
  256. (AddCode (list 'STRING Expression))))
  257. ((VectorP Expression)
  258. (progn (setq UpperBound (ISizeV Expression))
  259. (AddFullWord UpperBound)
  260. (setq I 0)
  261. (while (ILEQ I UpperBound)
  262. (progn (AppendItem (IGetV Expression I))
  263. (setq I (IAdd1 I))))))
  264. ((BigP Expression)
  265. (progn (setq UpperBound (ISizeV Expression))
  266. (AddFullWord UpperBound)
  267. (setq I 0)
  268. (while (ILEQ I UpperBound)
  269. (progn (AppendItem (IGetV Expression I))
  270. (setq I (IAdd1 I))))))
  271. ((FixP Expression)
  272. (progn (AddFullWord 0) (* "Header of full word fixnum")
  273. (AddFullWord Expression)))
  274. ((FloatP Expression)
  275. (progn (AddFullWord 1) (* "Header of float")
  276. (AddCode (list 'FLOAT Expression)))))))
  277. (de MakeMkItem (TagPart InfPart)
  278. (list 'MKITEM TagPart InfPart))
  279. (de InumP (N) (IntP N)) (* "Must be changed for cross-compilation")
  280. (de TagNumber (Expression)
  281. (MkINT (Tag Expression))) (* "Must be redefined for cross-compilation")