66(require "../huff-ops.rkt " )
77(require "../utils.rkt " )
88
9- ;; NOTE: This only handles up to 255 labels. if/when we want to support more than 255
10- ;; labels, we'll have to do it in multiple passes. this is because right now every
11- ;; label reference can be considered 2 bytes (PUSH1 + byte) but beyond that, some
12- ;; subset of labelrefs will be 3 bytes (PUSH2 + 2bytes). labelrefs can appear
13- ;; anywhere relative to where the actual labels are so we will have to record some
14- ;; more data during our first label-locating pass and also locate where labelrefs
15- ;; are, so we can correctly calculate and insert the byte offset for each label
9+ ;; NOTE: This now handles up to 65535 labels/offsets. Each label reference uses
10+ ;; PUSH2 + 2 bytes to ensure we can handle larger bytecode. This allows for bytecode
11+ ;; sizes up to 65535 bytes.
1612
1713;; TODO: Lots of hackiness in this pass. Clean up.
1814
@@ -38,7 +34,7 @@ maybe we should handle this in the assembler?
3834 (length expr)) ;; TODO: potential bug here. list length might != length of expressions it contains
3935 (if (instruction? expr)
4036 1
41- 2 ))) ;; we're treating anything that isn't an opcode as a label reference and assuming they're 2 bytes long
37+ 3 ))) ;; we're treating anything that isn't an opcode as a label reference and assuming they're 3 bytes long (PUSH2 + 2 bytes)
4238
4339(define (record-label-offsets code ht cur)
4440 (define done?
@@ -66,8 +62,8 @@ maybe we should handle this in the assembler?
6662 (cons node (recurse)))
6763 (define (labelref? node) ;; check if node is reference to a known label
6864 (hash-has-key? ht node))
69- (define (wrap-label label) ;; wrap a label offset in PUSH1 + hex
70- (list "PUSH1 " (string-append "0x " (byte ->hex (hash-ref ht label)))))
65+ (define (wrap-label label) ;; wrap a label offset in PUSH2 + hex
66+ (list "PUSH2 " (string-append "0x " (word ->hex (hash-ref ht label)))))
7167 (define (handle-labelref node)
7268 (continue (wrap-label node)))
7369 (cond
0 commit comments