Skip to content

Commit

Permalink
feat: implement FREE_STORAGE_POINTER()
Browse files Browse the repository at this point in the history
and clean up some phases that don't need the `data` object

Closes #3
  • Loading branch information
z80dev committed Nov 1, 2024
1 parent 44d6dd9 commit 0a00531
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
26 changes: 26 additions & 0 deletions puff/phases/fsp.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#lang racket

(require "../utils.rkt")

;; very simple. we start a counter at 0, and for each call to FREE_STORAGE_POINTER, we return the counter value then inc
;; values should be hex strings

(define counter 0)

(define (free-storage-pointer)
(let ([result counter])
(set! counter (+ counter 1))
(list 'hex (number->hex result))))

(define (fsp-call? code)
(eq? code "FREE_STORAGE_POINTER()"))

(define (handle-fsp-call code)
(if (fsp-call? code)
(free-storage-pointer)
code))

(define (insert-fsp code)
(map handle-fsp-call code))

(provide insert-fsp fsp-call? free-storage-pointer)
2 changes: 1 addition & 1 deletion puff/phases/hexvals.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
[(list 'hex num) (hex->instrs num)]
[_ instr]))

(define (insert-hexvals code data)
(define (insert-hexvals code)
(map handle-instr code))

(provide insert-hexvals hex->instrs)
2 changes: 1 addition & 1 deletion puff/phases/labels.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ maybe we should handle this in the assembler?
(cons (list "PUSH1" (string-append "0x" (byte->hex (hash-ref ht instr)))) (replace-labels (cdr code) ht))
(cons instr (replace-labels (cdr code) ht)))))))

(define (insert-labels code data)
(define (insert-labels code)
(let* ([ht (make-hash)]
[code (record-label-offsets code ht 0)])
(replace-labels code ht)))
Expand Down
2 changes: 1 addition & 1 deletion puff/phases/opcodes.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[(instruction? instr) (instruction->opcode instr)]
[else instr]))

(define (insert-opcodes code data)
(define (insert-opcodes code)
(map handle-instr code))

(provide insert-opcodes)
8 changes: 5 additions & 3 deletions puff/puff.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
(require "phases/opcodes.rkt")
(require "phases/labels.rkt")
(require "phases/macros.rkt")
(require "phases/fsp.rkt")

; TODO: This makes a lot of passes over the code
; In the future, come up with a syntax that allows
Expand All @@ -30,12 +31,13 @@
(lambda~>
(insert-macros data)
(insert-constants data)
(insert-hexvals data)
insert-fsp
insert-hexvals
(insert-funcsigs data)
(insert-errorsigs data)
(insert-eventsigs data)
(insert-labels data)
(insert-opcodes data)
insert-labels
insert-opcodes
flatten))

(define (compile-program-data-runtime data)
Expand Down

0 comments on commit 0a00531

Please sign in to comment.