assem.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Byte-code assembler (Richard's version)
  3. ;
  4. ; This assembler can assemble the output of the disassembler (as long as you
  5. ; add the identifier and the list of free names).
  6. ;
  7. ; (lap <identifier> (<free name> ...) <insts>*)
  8. ; <inst> ::= (<op-code> . <operands>) |
  9. ; <label> |
  10. ; (global <identifier>) |
  11. ; (set-global! <identifier>) |
  12. ; (local <identifer>) | ; currently out of order
  13. ; (set-local! <identifier>) | ; currently out of order
  14. ; (literal <anything>) | (literal (quote <anything>))
  15. ; <operand> ::= <number> | <label> | <stob-name> |
  16. ; (lap <spec> <insts>*) ; only where a template is expected, currently out of order
  17. ; <label> ::= <symbol> | <integer>
  18. ; (<free name> ...) is a list of all names used in GLOBAL and SET-GLOBAL!
  19. ; instructions. These names are required.
  20. ;
  21. ; QUOTE is optional for literals, unless the value is itself quoted.
  22. ;
  23. ; The assembler uses opcode-arg-specs to check the number and type of arguments
  24. ; to the opcodes.
  25. ; This code barely works for the current VM design, because it doesn't
  26. ; really track the stack depth---it should. Among other things,
  27. ; template literals are probably almost always broken.
  28. (define-compilator 'lap syntax-type
  29. (lambda (node depth frame cont)
  30. (let* ((exp (node-form node))
  31. (bindings (map (lambda (name-node)
  32. (cons (node-form name-node)
  33. (node-ref name-node 'binding)))
  34. (caddr exp)))
  35. (insts (cdddr exp)))
  36. (if (or (null? insts)
  37. (not (eq? 'protocol (caar insts))))
  38. (error "missing protocol instruction"))
  39. (call-with-values
  40. (lambda () (assemble-protocol (cdar insts)))
  41. (lambda (protocol template? env? closure? body-depth)
  42. (let* ((id (cadr exp))
  43. (template (compile-lap id
  44. protocol
  45. (cdr insts)
  46. bindings
  47. body-depth
  48. (make-frame frame id body-depth template? env? closure?))))
  49. (fixup-template-refs! template)
  50. (deliver-value
  51. (sequentially
  52. (stack-indirect-instruction (template-offset frame depth)
  53. (literal->index frame template))
  54. (instruction (enum op push))
  55. (instruction (enum op false))
  56. (instruction (enum op make-stored-object) 2 (enum stob closure)))
  57. cont)))))))
  58. ;----------------
  59. ; To allow for circular templates, templates can be referred to by name
  60. ; (the <identifier> in <spec> above). This code fixes up the references
  61. ; after assembly is otherwise complete.
  62. ;
  63. (define (fixup-template-refs! template)
  64. (let ((templates '()))
  65. ;; find all named templates
  66. (let find ((template template))
  67. (if (symbol? (template-info template))
  68. (set! templates (cons (cons (template-info template) template)
  69. templates)))
  70. (do ((i 0 (+ i 1)))
  71. ((>= i (template-length template)))
  72. (if (template? (template-ref template i))
  73. (find (template-ref template i)))))
  74. ;; replace all template markers with the appropriate template
  75. (let replace ((template template))
  76. (do ((i 0 (+ i 1)))
  77. ((>= i (template-length template)))
  78. (let ((x (template-ref template i)))
  79. (cond ((template? x)
  80. (replace x))
  81. ((not (template-marker? x)))
  82. ((assq (template-marker-name x) templates)
  83. => (lambda (t)
  84. (template-set! template i (cdr t))))
  85. (else
  86. (error "no template of this name available"
  87. (template-marker-name x)))))))))
  88. ; Marking where a template should be inserted.
  89. (define template-marker (cons #f #f))
  90. (define (make-template-marker name)
  91. (cons template-marker name))
  92. (define (template-marker? x)
  93. (and (pair? x)
  94. (eq? (car x) template-marker)))
  95. (define template-marker-name cdr)
  96. ;----------------
  97. (define (compile-lap id header insts bindings depth frame)
  98. (segment->template (sequentially
  99. header
  100. (really-compile-lap insts bindings depth frame))
  101. frame))
  102. ; Assemble each instruction, keeping track of which ones use labels.
  103. ; STUFF is a list of lists of the form (<inst> <offset> . <preceding-insts>)
  104. ; which indicates that <inst> uses a label, that it begins at <offset>, and is
  105. ; preceded by <preceding-insts>.
  106. (define (really-compile-lap insts bindings depth frame)
  107. (let loop ((insts insts) (segments '()) (stuff '()) (offset 0) (labels '()))
  108. (cond ((null? insts)
  109. (fixup-lap-labels segments stuff labels depth frame))
  110. ((pair? (car insts))
  111. (call-with-values
  112. (lambda ()
  113. (assemble-instruction (car insts) bindings depth frame))
  114. (lambda (segment label-use?)
  115. (let ((new-offset (+ offset (segment-size segment))))
  116. (if label-use?
  117. (loop (cdr insts)
  118. '()
  119. `((,(car insts) ,offset . ,segments) . ,stuff)
  120. new-offset
  121. labels)
  122. (loop (cdr insts)
  123. (cons segment segments)
  124. stuff
  125. new-offset
  126. labels))))))
  127. ((or (symbol? (car insts))
  128. (integer? (car insts)))
  129. (loop (cdr insts) segments stuff offset
  130. (cons (cons (car insts) offset) labels)))
  131. (else
  132. (error "bad LAP instruction" (car insts))))))
  133. ; Reassemble the instruction at the beginning of each STUFF list to resolve
  134. ; the label reference and glue everything together using SEQUENTIALLY. The
  135. ; label code assumes that the machine calculates the label from the end of
  136. ; the instruction.
  137. (define (fixup-lap-labels segments stuff labels depth frame)
  138. (let loop ((stuff stuff) (segment (apply sequentially (reverse segments))))
  139. (if (null? stuff)
  140. segment
  141. (let* ((data (car stuff))
  142. (inst (car data))
  143. (offset (cadr data))
  144. (segments (cddr data)))
  145. (loop (cdr stuff)
  146. (sequentially (apply sequentially (reverse segments))
  147. (reassemble-instruction inst offset labels depth frame)
  148. segment))))))
  149. ; This returns two values, the assembled instruction and a flag indicating
  150. ; whether or not the instruction used a label.
  151. (define (assemble-instruction inst bindings depth frame)
  152. (really-assemble-instruction inst bindings (lambda (label) (values 0 0))
  153. depth frame))
  154. ; Same as the above, except that labels are resolved and no flag is returned.
  155. (define (reassemble-instruction inst offset labels depth frame)
  156. (call-with-values
  157. (lambda ()
  158. (really-assemble-instruction inst #f (resolve-label offset labels) depth frame))
  159. (lambda (inst ignore)
  160. inst)))
  161. ; Return the high and low bytes of the distance between OFFSET and LABEL,
  162. ; using the known label offsets in LABELS.
  163. (define (resolve-label offset labels)
  164. (lambda (label)
  165. (cond ((assoc label labels)
  166. => (lambda (p)
  167. (let ((delta (- (cdr p) offset)))
  168. (values (quotient delta byte-limit)
  169. (remainder delta byte-limit)))))
  170. (else
  171. (error "LAP label is not defined" label)))))
  172. ;----------------
  173. ; Actually do some assembly. A few opcodes need special handling; most just
  174. ; use the argument specifications from the architecture.
  175. (define (really-assemble-instruction inst bindings labels depth frame)
  176. (let ((opname (car inst))
  177. (args (cdr inst)))
  178. (cond ((assemble-special-op opname args bindings depth frame)
  179. => (lambda (inst)
  180. (values inst #f)))
  181. ((name->enumerand opname op)
  182. => (lambda (opcode)
  183. (assemble-general-instruction opcode inst bindings labels depth frame)))
  184. (else
  185. (error "unknown LAP instruction" inst)))))
  186. ; The optional ' is optionally stripped off the argument to LITERAL.
  187. (define (assemble-special-op opname args bindings depth frame)
  188. (case opname
  189. ((literal)
  190. (let* ((arg (car args))
  191. (obj (if (and (pair? arg)
  192. (eq? (car arg) 'quote))
  193. (cadr arg)
  194. arg)))
  195. (cond
  196. ((small-integer? obj)
  197. (integer-literal-instruction obj))
  198. (else
  199. (stack-indirect-instruction
  200. (template-offset frame depth)
  201. (literal->index frame obj))))))
  202. ((global)
  203. (lap-global #f (car args) bindings frame depth))
  204. ((set-global!)
  205. (lap-global #t (car args) bindings frame depth))
  206. ; ((local)
  207. ; (if (null? (cdr args))
  208. ; (lap-local (car args) bindings)
  209. ; #f))
  210. ; ((set-local!)
  211. ; (if (null? (cdr args))
  212. ; (lap-set-local! (car args) bindings)
  213. ; #f))
  214. (else
  215. #f)))
  216. (define (small-integer? obj)
  217. (and (integer? obj)
  218. (exact? obj)
  219. (<= 0 (+ obj 128))
  220. (< (+ obj 128) byte-limit)))
  221. ; Lookup NAME in BINDINGS to the location.
  222. (define (lap-global assign? name bindings frame depth)
  223. (let ((binding (assq bindings name)))
  224. (if (not binding)
  225. (error "LAP variable is not in free list" name)
  226. (let ((binding (cdr binding)))
  227. (cond ((and (binding? binding)
  228. (pair? (binding-place binding)))
  229. (error "LAP variable is not global" name))
  230. (assign?
  231. (let ((offset (template-offset frame depth))
  232. (index (binding->index frame
  233. binding
  234. name
  235. usual-variable-type)))
  236. (instruction (enum op set-global!)
  237. (high-byte offset)
  238. (low-byte offset)
  239. (high-byte index)
  240. (low-byte index))))
  241. (else
  242. (let ((offset (template-offset frame depth))
  243. (index (binding->index frame binding name value-type)))
  244. (instruction (enum op global)
  245. (high-byte offset)
  246. (low-byte offset)
  247. (high-byte index)
  248. (low-byte index)))))))))
  249. ; This is for an old version (< 0.53); noone seems to use it currently.
  250. ; This needs a rewrite for the current architecture.
  251. ;; Lookup NAME in BINDINGS and pick out the appropriate local op.
  252. ;
  253. ;(define (lap-local name bindings)
  254. ; (let ((binding (lookup bindings name)))
  255. ; (if (and (binding? binding)
  256. ; (pair? (binding-place binding)))
  257. ; (let* ((level+over (binding-place binding))
  258. ; (back (- (environment-level bindings)
  259. ; (car level+over)))
  260. ; (over (cdr level+over)))
  261. ; (case back
  262. ; ((0) (instruction (enum op local0) over))
  263. ; ((1) (instruction (enum op local1) over))
  264. ; ((2) (instruction (enum op local2) over))
  265. ; (else (instruction (enum op local) back over))))
  266. ; (error "LAP local variable is not locally bound" name))))
  267. ;
  268. ;; Same thing, except that there is only one opcode.
  269. ;
  270. ;(define (lap-set-local! name bindings)
  271. ; (let ((binding (lookup bindings name)))
  272. ; (if (and (binding? binding)
  273. ; (pair? (binding-place binding)))
  274. ; (let* ((level+over (binding-place binding))
  275. ; (back (- (environment-level bindings)
  276. ; (car level+over)))
  277. ; (over (cdr level+over)))
  278. ; (instruction (enum op set-local!)
  279. ; back
  280. ; (quotient over byte-limit)
  281. ; (remainder over byte-limit)))
  282. ; (error "LAP local variable is not locally bound" name))))
  283. ; Assembling protocols.
  284. (define (assemble-protocol args)
  285. (if (integer? (car args))
  286. (let ((count (car args)))
  287. (call-with-values
  288. (lambda ()
  289. (if (and (not (null? (cdr args)))
  290. (eq? '+ (cadr args)))
  291. (values #t (cddr args))
  292. (values #f (cdr args))))
  293. (lambda (nary? rest)
  294. (if (and (not (null? rest))
  295. (or (not (pair? (car rest)))
  296. (not (eq? 'push (caar rest)))))
  297. (error "unknown assembly protocol" args))
  298. (let ((push-env?
  299. (and (not (null? rest))
  300. (memq 'env (cdar rest))))
  301. (push-template?
  302. (and (not (null? rest))
  303. (memq 'template (cdar rest))))
  304. (push-closure?
  305. (and (not (null? rest))
  306. (memq 'closure (cdar rest)))))
  307. (let ((extras (+ (if push-template? 1 0)
  308. (if push-env? 1 0)
  309. (if push-closure? 1 0))))
  310. (if nary?
  311. (values (nary-lambda-protocol count push-template? push-env? push-closure?)
  312. push-template? push-env? push-closure?
  313. (+ 1 count extras))
  314. (values (lambda-protocol count push-template? push-env? push-closure?)
  315. push-template? push-env? push-closure?
  316. (+ count extras))))))))
  317. (case (car args)
  318. ((args+nargs)
  319. (values 0 ; doesn't matter
  320. (cons args+nargs-protocol (cdr args))))
  321. ((nary-dispatch)
  322. (values 0 ; doesn't matter
  323. (cons nary-dispatch-protocol
  324. (parse-nary-dispatch (cdr args)))))
  325. ((big-stack)
  326. (error "can't assemble big-stack protocol"))
  327. (else
  328. (error "unknown assembly protocol" args)))))
  329. ; This is fairly bogus, because it uses the targets as addresses instead
  330. ; of treating them as labels. Fixing this is too much work, seeing as
  331. ; no one is likely to use it.
  332. (define (parse-nary-dispatch targets)
  333. (let ((results (vector 0 0 0 0)))
  334. (warn "LAP compiler treats nary-dispatch targets as addresses, not as labels.")
  335. (for-each (lambda (target)
  336. (if (and (pair? target)
  337. (pair? (cdr target))
  338. (pair? (cddr target))
  339. (or (eq? (car target) '>2)
  340. (and (integer? (car target))
  341. (<= 0 (car target) 2)))
  342. (eq? (cadr target) '=>)
  343. (integer? (caddr target)))
  344. (vector-set! results
  345. (if (eq? (car target) '>2)
  346. 3
  347. (car target))
  348. (caddr target))
  349. (error "bad nary-dispatch label in LAP" target)))
  350. targets)
  351. (vector->list results)))
  352. ;----------------
  353. ; This returns two values, the assembled instruction and a flag indicating
  354. ; whether or not the instruction used a label.
  355. (define (assemble-general-instruction opcode inst bindings labels depth frame)
  356. (let ((specs (vector-ref opcode-arg-specs opcode))
  357. (args (cdr inst))
  358. (finish (lambda (ops label-use?)
  359. (values (apply instruction opcode (reverse ops))
  360. label-use?))))
  361. (let loop ((specs specs) (args args) (ops '()) (label-use? #f))
  362. (if (null? specs)
  363. (finish ops label-use?)
  364. (case (car specs)
  365. ((offset)
  366. (let ((label (check-lap-arg args 'label inst)))
  367. (call-with-values
  368. (lambda () (labels label))
  369. (lambda (high low)
  370. (loop (cdr specs) (cdr args) `(,low ,high . ,ops) #t)))))
  371. ((stob)
  372. (let ((byte (check-lap-arg args 'stob inst)))
  373. (loop (cdr specs) (cdr args) (cons byte ops) label-use?)))
  374. ((byte nargs stack-index index)
  375. (let ((byte (check-lap-arg args 'int inst)))
  376. (loop (cdr specs) (cdr args) (cons byte ops) label-use?)))
  377. ((two-bytes two-byte-nargs two-byte-stack-index two-byte-index)
  378. (let ((number (check-lap-arg args 'int inst)))
  379. (loop (cdr specs) (cdr args)
  380. `(,(remainder number byte-limit)
  381. ,(quotient number byte-limit)
  382. . ,ops)
  383. label-use?)))
  384. ((junk)
  385. (loop (cdr specs) args (cons 0 ops) label-use?))
  386. (else
  387. (if (or (eq? (car specs) '+)
  388. (integer? (car specs)))
  389. (finish ops label-use?)
  390. (error "LAP internal error, unknown opcode argument specification" (car specs)))))))))
  391. ; Check that the car of ARGS is an argument of the appropriate type and
  392. ; return it.
  393. (define (check-lap-arg args type inst)
  394. (if (null? args)
  395. (error "not enough arguments in LAP instruction" inst))
  396. (let ((arg (car args)))
  397. (case type
  398. ((int)
  399. (if (integer? arg)
  400. arg
  401. (error "numeric operand expected in LAP instruction" inst)))
  402. ((stob)
  403. (cond ((name->enumerand arg stob))
  404. (else
  405. (error "unknown STOB argument in LAP instruction" inst))))
  406. ((label)
  407. (cond ((symbol? arg)
  408. arg)
  409. ((and (pair? arg)
  410. (eq? (car arg) '=>))
  411. (cadr arg))
  412. (else
  413. (error "bad label in LAP instruction" inst))))
  414. (else
  415. (error "LAP internal error, unknown LAP argument specifier" type)))))