diff --git a/puff/phases/fsp.rkt b/puff/phases/fsp.rkt new file mode 100644 index 0000000..f3738ed --- /dev/null +++ b/puff/phases/fsp.rkt @@ -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) diff --git a/puff/phases/hexvals.rkt b/puff/phases/hexvals.rkt index a8da35e..4c84080 100644 --- a/puff/phases/hexvals.rkt +++ b/puff/phases/hexvals.rkt @@ -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) diff --git a/puff/phases/labels.rkt b/puff/phases/labels.rkt index 95b1807..a54fca2 100644 --- a/puff/phases/labels.rkt +++ b/puff/phases/labels.rkt @@ -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))) diff --git a/puff/phases/opcodes.rkt b/puff/phases/opcodes.rkt index 2ed1ba3..1ec716e 100644 --- a/puff/phases/opcodes.rkt +++ b/puff/phases/opcodes.rkt @@ -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) diff --git a/puff/puff.rkt b/puff/puff.rkt index 9e7af96..a69e047 100644 --- a/puff/puff.rkt +++ b/puff/puff.rkt @@ -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 @@ -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)