Skip to content

Commit b659f90

Browse files
committed
update label to use 2 bytes for offset
1 parent d22edea commit b659f90

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

puff/phases/labels.rkt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
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

puff/utils.rkt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
(string-append "0" hex)
4040
hex)))
4141

42+
(define (word->hex word)
43+
(let ([hex (number->string word 16)])
44+
(let ([len (string-length hex)])
45+
(cond
46+
[(= len 1) (string-append "000" hex)]
47+
[(= len 2) (string-append "00" hex)]
48+
[(= len 3) (string-append "0" hex)]
49+
[else hex]))))
50+
4251
(define (bytes->hex bytes)
4352
(string-append "0x" (apply string-append (map byte->hex bytes))))
4453

@@ -82,6 +91,7 @@
8291
byte-length
8392
format-filename
8493
byte->hex
94+
word->hex
8595
bytes->hex
8696
concat-hex
8797
zero-pad-right

0 commit comments

Comments
 (0)