Skip to content

Commit 56f94a6

Browse files
authored
Merge pull request #149 from cmsc430/ziggy
Ziggy This merges the Ziggy branch into main. There are still problems with Ziggy, but it's time to put them on main.
2 parents 82864f8 + 9531bdc commit 56f94a6

385 files changed

Lines changed: 4259 additions & 13159 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/langs.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
matrix:
99
os: [ubuntu-20.04, ubuntu-22.04]
1010
racket-variant: ['BC', 'CS']
11-
racket-version: ['7.8', '8.0', '8.6', '8.8']
11+
racket-version: ['8.6', '8.8']
1212
name: Test on Racket ${{ matrix.racket-variant }} ${{ matrix.racket-version }} on ${{ matrix.os }}
1313
steps:
1414
- name: Checkout
@@ -31,8 +31,11 @@ jobs:
3131
nasm --version
3232
gcc --version
3333
- name: Install langs package
34-
run: raco pkg install langs/
34+
run: |
35+
raco pkg install --auto ziggy/
36+
raco pkg install langs/
3537
- name: Run tests
3638
run: |
37-
raco test -p langs
39+
raco test -p ziggy
40+
xvfb-run raco test -p langs
3841
raco test -c outlaw

langs/a86/ast.rkt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@
238238
(instruct Sal (dst i) check:shift)
239239
(instruct Sar (dst i) check:shift)
240240
(instruct Push (a1) check:push)
241+
(instruct Pushf () check:none)
242+
(instruct Popf () check:none)
241243
(instruct Pop (a1) check:register)
242244
(instruct Lea (dst x) check:lea)
243245
(instruct Not (x) check:register)
@@ -250,6 +252,8 @@
250252
(instruct Const (x) check:label-symbol)
251253

252254
;; IMPROVE: do more checking
255+
(instruct Db (x) (lambda (a x n) (values a x)))
256+
(instruct Dw (x) (lambda (a x n) (values a x)))
253257
(instruct Dd (x) (lambda (a x n) (values a x)))
254258
(instruct Dq (x) (lambda (a x n) (values a x)))
255259

@@ -392,3 +396,26 @@
392396
(unless (member init (map (lambda (i) (match i [(Global l) l]))
393397
(filter Global? asm)))
394398
(error 'prog "initial label undeclared as global: ~v" init))]))
399+
400+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
401+
;; Symbol to Label
402+
403+
;; Symbol -> Label
404+
;; Produce a symbol that is a valid Nasm label
405+
;; Guarantees that (eq? s1 s2) <=> (eq? (symbol->label s1) (symbol->label s1))
406+
(provide symbol->label)
407+
(define (symbol->label s)
408+
(string->symbol
409+
(string-append
410+
"label_"
411+
(list->string
412+
(map (λ (c)
413+
(if (or (char<=? #\a c #\z)
414+
(char<=? #\A c #\Z)
415+
(char<=? #\0 c #\9)
416+
(memq c '(#\_ #\$ #\# #\@ #\~ #\. #\?)))
417+
c
418+
#\_))
419+
(string->list (symbol->string s))))
420+
"_"
421+
(number->string (eq-hash-code s) 16))))

langs/a86/interp.rkt

Lines changed: 133 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
[asm-interp (-> (listof instruction?) any/c)]
55
[asm-interp/io (-> (listof instruction?) string? any/c)])
66

7+
(define-logger a86)
8+
79
(require "printer.rkt" "ast.rkt" "callback.rkt" "check-nasm.rkt"
810
(rename-in ffi/unsafe [-> _->]))
911
(require (submod "printer.rkt" private))
1012

1113
;; Check NASM availability when required to fail fast.
1214
(check-nasm-available)
1315

16+
(define *debug*?
17+
(let ((r (getenv "PLTSTDERR")))
18+
(and r
19+
(string=? r "info@a86"))))
20+
1421
;; Assembly code is linked with object files in this parameter
1522
(define current-objs
1623
(make-parameter '()))
@@ -32,10 +39,43 @@
3239

3340
(define fmt (if (eq? (system-type 'os) 'macosx) 'macho64 'elf64))
3441

42+
;; WARNING: The heap is re-used, so make sure you're done with it
43+
;; before calling asm-interp again
44+
(define *heap*
45+
; IMPROVE ME: hard-coded heap size
46+
(malloc _int64 20000 'raw))
47+
48+
49+
;; Integer64 -> String
50+
(define (int64->binary-string n)
51+
(format "#b~a"
52+
(~r n #:base 2 #:min-width 64 #:pad-string "0")))
53+
54+
;; Integer64 -> String
55+
(define (int64->octal-string n)
56+
(format "#o~a"
57+
(~r n #:base 8 #:min-width 22 #:pad-string "0")))
58+
59+
;; Integer64
60+
(define (int64->hex-string n)
61+
(format "#x~a"
62+
(~r n #:base 16 #:min-width 16 #:pad-string "0")))
63+
64+
(define (show-state . regs)
65+
(format "\n~a"
66+
(map (lambda (r v)
67+
(format "(~a ~a)" r (int64->hex-string v)))
68+
'(rax rbx rcx rdx rbp rsp rsi rdi
69+
r8 r9 r10 r11 r12 r13 r14 r15 instr flags)
70+
regs)))
71+
3572
;; Asm String -> (cons Value String)
3673
;; Like asm-interp, but uses given string for input and returns
3774
;; result with string output
3875
(define (asm-interp/io a input)
76+
77+
(log-a86-info (~v a))
78+
3979
(define t.s (make-temporary-file "nasm~a.s"))
4080
(define t.o (path-replace-extension t.s #".o"))
4181
(define t.so (path-replace-extension t.s #".so"))
@@ -46,7 +86,9 @@
4686
#:exists 'truncate
4787
(λ ()
4888
(parameterize ((current-shared? #t))
49-
(asm-display a))))
89+
(asm-display (if *debug*?
90+
(debug-transform a)
91+
a)))))
5092

5193
(nasm t.s t.o)
5294
(ld t.o t.so)
@@ -69,24 +111,27 @@
69111
(set-ffi-obj! "error_handler" libt.so _pointer
70112
(function-ptr (λ () (raise 'err)) (_fun _-> _void))))
71113

114+
(when *debug*?
115+
(define log (ffi-obj-ref log-label libt.so (thunk #f)))
116+
(when log
117+
(set-ffi-obj! log-label libt.so _pointer
118+
(function-ptr
119+
(λ () (log-a86-info
120+
(apply show-state
121+
(build-list 18 (lambda (i) (ptr-ref log _int64 (add1 i)))))))
122+
(_fun _-> _void)))))
72123

73-
(define current-heap #f)
124+
(define has-heap? #f)
74125

75-
;; allocate a heap
76126
(when (ffi-obj-ref "heap" libt.so (thunk #f))
77-
(set! current-heap (make-c-parameter "heap" libt.so _pointer))
78-
79-
(if (ffi-obj-ref "from" libt.so (thunk #f))
80-
(begin
81-
(current-heap
82-
; IMPROVE ME: hard-coded heap size
83-
(malloc _int64 20000 'raw))
84-
(set-ffi-obj! "from" libt.so _pointer (current-heap))
85-
(set-ffi-obj! "to" libt.so _pointer (ptr-add (current-heap) 10000 _int64))
86-
(set-ffi-obj! "types" libt.so _pointer (malloc _int32 10000)))
87-
(current-heap
88-
; IMPROVE ME: hard-coded heap size
89-
(malloc _int64 10000 'raw))))
127+
(set! has-heap? #t)
128+
129+
;; This is a GC-enabled run-time so set from, to, and types space
130+
(when (ffi-obj-ref "from" libt.so (thunk #f))
131+
;; FIXME: leaks types memory
132+
(set-ffi-obj! "from" libt.so _pointer *heap*)
133+
(set-ffi-obj! "to" libt.so _pointer (ptr-add *heap* 10000 _int64))
134+
(set-ffi-obj! "types" libt.so _pointer (malloc _int32 10000))))
90135

91136
(delete-file t.s)
92137
(delete-file t.o)
@@ -109,15 +154,9 @@
109154
(current-out (fopen t.out "w"))
110155

111156
(define result
112-
(begin0
113-
(with-handlers ((symbol? identity))
114-
(guard-foreign-escape
115-
(if current-heap
116-
(cons (current-heap) (entry (current-heap)))
117-
(entry #f))))
118-
#;
119-
(when current-heap
120-
(free (current-heap)))))
157+
(with-handlers ((symbol? identity))
158+
(guard-foreign-escape
159+
(entry *heap*))))
121160

122161
(fflush (current-out))
123162
(fclose (current-in))
@@ -128,15 +167,9 @@
128167
(delete-file t.out)
129168
(cons result output))
130169

131-
(begin0
132-
(with-handlers ((symbol? identity))
133-
(guard-foreign-escape
134-
(if current-heap
135-
(cons (current-heap) (entry (current-heap)))
136-
(entry #f))))
137-
#;
138-
(when current-heap
139-
(free (current-heap))))))
170+
(with-handlers ((symbol? identity))
171+
(guard-foreign-escape
172+
(entry *heap*)))))
140173

141174

142175
(define (string-splice xs)
@@ -192,3 +225,69 @@
192225
(regexp-match #rx"undefined reference to `(.*)'" err-msg)) ; linux
193226
[(list _ symbol) (ld:undef-symbol symbol)]
194227
[_ (ld:error (format "unknown link error.\n\n~a" err-msg))])))
228+
229+
230+
231+
;; Debugging facilities
232+
233+
(define log-label (symbol->label (gensym 'log)))
234+
235+
(define (Log i)
236+
(seq (save-registers)
237+
(Pushf)
238+
(Mov 'rax i)
239+
(Mov (Offset log-label (* 8 17)) 'rax)
240+
(Mov 'rax (Offset 'rsp 0))
241+
(Mov (Offset log-label (* 8 18)) 'rax)
242+
(Call (Offset log-label 0))
243+
(Popf)
244+
(restore-registers)))
245+
246+
(define (instrument is)
247+
(for/fold ([ls '()]
248+
#:result (reverse ls))
249+
([idx (in-naturals)]
250+
[ins (in-list is)])
251+
(if (serious-instruction? ins)
252+
(seq ins (reverse (Log idx)) ls)
253+
(seq ins ls))))
254+
255+
(define (serious-instruction? ins)
256+
(match ins
257+
[(Label _) #f]
258+
[(Global _) #f]
259+
[(? Comment?) #f]
260+
[_ #t]))
261+
262+
(define (debug-transform is)
263+
(seq (instrument is)
264+
;; End of user program
265+
(Data)
266+
(Global log-label)
267+
(Label log-label)
268+
(Dq 0) ; callback placeholder
269+
(static-alloc-registers)
270+
(Dq 0) ; index of instruction
271+
(Dq 0) ; flags
272+
))
273+
274+
(define registers
275+
'(rax rbx rcx rdx rbp rsp rsi rdi
276+
r8 r9 r10 r11 r12 r13 r14 r15))
277+
278+
(define (static-alloc-registers)
279+
(apply seq
280+
(map (λ (r) (seq (Dq 0) (% (~a r))))
281+
registers)))
282+
283+
(define (save-registers)
284+
(apply seq
285+
(map (λ (r i) (seq (Mov (Offset log-label (* 8 i)) r)))
286+
registers
287+
(build-list (length registers) add1))))
288+
289+
(define (restore-registers)
290+
(apply seq
291+
(map (λ (r i) (seq (Mov r (Offset log-label (* 8 i)))))
292+
registers
293+
(build-list (length registers) add1))))

langs/a86/printer.rkt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
[(? reg?) (reg->string t)]
5050
[(Offset (? reg? r) i)
5151
(string-append "[" (reg->string r) " + " (number->string i) "]")]
52+
[(Offset (? label? l) i)
53+
(string-append "[" (label-symbol->string l) " + " (number->string i) "]")]
5254
[_ (label-symbol->string t)]))
5355

5456
;; Arg -> String
@@ -209,6 +211,10 @@
209211
[(Push a)
210212
(string-append tab "push "
211213
(arg->string a))]
214+
[(Pushf)
215+
(string-append tab "pushf")]
216+
[(Popf)
217+
(string-append tab "popf")]
212218
[(Pop r)
213219
(string-append tab "pop "
214220
(reg->string r))]
@@ -232,6 +238,12 @@
232238
" equ "
233239
(number->string c))]
234240

241+
[(Db (? bytes? bs))
242+
(apply string-append tab "db " (add-between (map number->string (bytes->list bs)) ", "))]
243+
[(Db x)
244+
(string-append tab "db " (arg->string x))]
245+
[(Dw x)
246+
(string-append tab "dw " (arg->string x))]
235247
[(Dd x)
236248
(string-append tab "dd " (arg->string x))]
237249
[(Dq x)

langs/a86/stepper.rkt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#lang racket
2+
(provide main)
3+
4+
(require redex)
5+
6+
(define-language L)
7+
8+
;; A reduction relation that just relates elements
9+
;; of the list to their successors
10+
(define (r ls)
11+
(define i 0)
12+
(reduction-relation L
13+
(--> any_i
14+
any_j
15+
(where any_j
16+
,(begin
17+
(set! i (add1 i))
18+
(list-ref ls (min i (sub1 (length ls)))))))))
19+
20+
21+
;; reads log file from stdin
22+
(define (main)
23+
(define ls
24+
(let loop ()
25+
(if (eof-object? (read))
26+
'()
27+
(cons (read) (loop)))))
28+
29+
;; replace instr indices with their instructions
30+
(define ls1
31+
(map (λ (s)
32+
(map (λ (p)
33+
(match p
34+
[(list 'instr i)
35+
(list 'instr (list-ref (list-ref ls 0)
36+
(add1 i)))]
37+
[_ p]))
38+
s))
39+
ls))
40+
41+
;; run the stepper
42+
(stepper (r (rest ls1)) (first (rest ls1))))

langs/abscond/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ runtime.o: $(objs)
3535
cat $< | racket -t compile-stdin.rkt -m > $@
3636

3737
clean:
38-
rm *.o *.s *.run
38+
@$(RM) *.o *.s *.run ||:
39+
@echo "$(shell basename $(shell pwd)): cleaned!"
3940

4041
%.test: %.run %.rkt
4142
@test "$(shell ./$(<))" = "$(shell racket $(word 2,$^))"

langs/abscond/ast.rkt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#lang racket
2-
(provide Int)
2+
(provide Lit)
33

4-
;; type Expr = (Int Integer)
5-
(struct Int (i) #:prefab)
4+
;; type Expr = (Lit Integer)
5+
6+
(struct Lit (i) #:prefab)

0 commit comments

Comments
 (0)