Skip to content

Commit 3a2cc63

Browse files
authored
Merge pull request #102 from cmsc430/outlaw
Outlaw
2 parents 57c7851 + 579cbbf commit 3a2cc63

Some content is hidden

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

47 files changed

+8579
-2
lines changed

.github/workflows/langs.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ jobs:
1515
uses: actions/checkout@main
1616
- name: Install nasm
1717
run: sudo apt-get install nasm
18+
- name: Install libunistring
19+
run: |
20+
sudo apt-get install libunistring2
21+
sudo apt-get install libunistring-dev
1822
- name: Install Racket
1923
uses: Bogdanp/setup-racket@v1.5
2024
with:
@@ -29,4 +33,6 @@ jobs:
2933
- name: Install langs package
3034
run: raco pkg install langs/
3135
- name: Run tests
32-
run: raco test -p langs
36+
run: |
37+
export LINK_DIR=/usr/lib/x86_64-linux-gnu
38+
raco test -p langs

.github/workflows/macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
on:
2-
- push
2+
- workflow_dispatch
33

44
jobs:
55
build-and-test:

.github/workflows/push.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
sudo dpkg -i pandoc.deb
1414
sudo apt-get install nasm
1515
sudo apt-get install fonts-stix
16+
sudo apt-get install libunistring-dev
1617
- name: Install Racket
1718
uses: Bogdanp/setup-racket@v1.5
1819
with:
@@ -22,6 +23,7 @@ jobs:
2223
version: '8.1'
2324
- name: Build and test
2425
run: |
26+
export LINK_DIR=/usr/lib/x86_64-linux-gnu
2527
raco pkg install langs/
2628
raco make www/main.scrbl
2729
raco test langs

langs/outlaw/Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# NOTES:
2+
# - You will need a static version of libunistring to link against; on Mac
3+
# ld will always choose .dylib over .a to link, so either rename or remove
4+
# the .dylib versions.
5+
6+
UNAME := $(shell uname)
7+
.PHONY: test
8+
9+
ifeq ($(UNAME), Darwin)
10+
format=macho64
11+
else
12+
format=elf64
13+
endif
14+
15+
objs = \
16+
main.o \
17+
values.o \
18+
print.o \
19+
symbol.o \
20+
string.o \
21+
io.o \
22+
error.o \
23+
os.o \
24+
stdlib.o
25+
26+
default: runtime.o
27+
28+
runtime.o: $(objs)
29+
ld -r $(objs) -o runtime.o
30+
31+
%.run: %.o runtime.o
32+
gcc -lunistring runtime.o $< -o $@
33+
34+
.c.o:
35+
gcc -fPIC -c -g -o $@ $<
36+
37+
.s.o:
38+
nasm -g -f $(format) -o $@ $<
39+
40+
stdlib.s: stdlib.rkt
41+
cat stdlib.rkt | racket -t compile-library.rkt -m > stdlib.s
42+
43+
%.s: %.rkt
44+
cat $< | racket -t compile-stdin.rkt -m > $@
45+
46+
clean:
47+
rm *.o *.s *.run
48+
49+
test: example.run
50+
@test "$(shell ./example.run)" = "$(shell racket example.rkt)"

langs/outlaw/a86/ast.rkt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#lang racket
2+
(provide (all-defined-out))
3+
4+
(struct Text ())
5+
(struct Data ())
6+
7+
(struct Global (x))
8+
(struct Label (x))
9+
(struct Call (x))
10+
(struct Ret ())
11+
(struct Mov (dst src))
12+
(struct Add (dst src))
13+
(struct Sub (dst src))
14+
(struct Cmp (a1 a2))
15+
(struct Jmp (x))
16+
(struct Je (x))
17+
(struct Jne (x))
18+
(struct Jl (x))
19+
(struct Jle (x))
20+
(struct Jg (x))
21+
(struct Jge (x))
22+
(struct And (dst src))
23+
(struct Or (dst src))
24+
(struct Xor (dst src))
25+
(struct Sal (dst i))
26+
(struct Sar (dst i))
27+
(struct Push (a1))
28+
(struct Pop (a1))
29+
(struct Lea (dst x))
30+
(struct Div (den))
31+
32+
(struct Offset (r i))
33+
(struct Extern (x))
34+
35+
(struct Equ (x v))
36+
(struct Const (x))
37+
(struct Dd (x))
38+
(struct Dq (x))
39+
(struct Plus (e1 e2))
40+
41+
;; (U Instruction Asm) ... -> Asm
42+
;; Convenient for sequencing instructions or groups of instructions
43+
(define (seq . xs)
44+
(foldr (λ (x is)
45+
(if (list? x)
46+
(append x is)
47+
(cons x is)))
48+
'()
49+
xs))
50+
51+
(define (register? x)
52+
(and (memq x '(cl eax rax rbx rcx rdx rbp rsp rsi rdi r8 r9 r10 r11 r12 r13 r14 r15))
53+
#t))
54+
55+
(define (exp? x)
56+
(or (Offset? x)
57+
(and (Plus? x)
58+
(exp? (Plus-e1 x))
59+
(exp? (Plus-e2 x)))
60+
(symbol? x)
61+
(integer? x)))
62+
63+
(define offset? Offset?)
64+
65+
(define (label? x)
66+
(and (symbol? x)
67+
(not (register? x))))
68+
69+
(define (instruction? x)
70+
(or (Text? x)
71+
(Data? x)
72+
(Global? x)
73+
(Label? x)
74+
(Extern? x)
75+
(Call? x)
76+
(Ret? x)
77+
(Mov? x)
78+
(Add? x)
79+
(Sub? x)
80+
(Cmp? x)
81+
(Jmp? x)
82+
(Je? x)
83+
(Jne? x)
84+
(Jl? x)
85+
(Jle? x)
86+
(Jg? x)
87+
(Jge? x)
88+
(And? x)
89+
(Or? x)
90+
(Xor? x)
91+
(Sal? x)
92+
(Sar? x)
93+
(Push? x)
94+
(Pop? x)
95+
(Lea? x)
96+
(Div? x)
97+
;(Comment? x)
98+
(Equ? x)
99+
(Dd? x)
100+
(Dq? x)))

langs/outlaw/a86/callback.rkt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#lang racket
2+
;; based on racket/draw/unsafe/callback
3+
(provide guard-foreign-escape)
4+
(require ffi/unsafe
5+
ffi/unsafe/atomic)
6+
7+
(define callback-atomic? (eq? 'chez-scheme (system-type 'vm)))
8+
9+
(define-syntax-rule (guard-foreign-escape e0 e ...)
10+
(call-guarding-foreign-escape (lambda () e0 e ...)))
11+
12+
(define (call-guarding-foreign-escape thunk)
13+
(if callback-atomic?
14+
((call-with-c-return
15+
(lambda ()
16+
(with-handlers ([(lambda (x) #t)
17+
(lambda (x)
18+
;; Deliver an exception re-raise after returning back
19+
;; from `call-with-c-return`:
20+
(lambda ()
21+
(when (in-atomic-mode?)
22+
(end-atomic)) ; error happened during atomic mode
23+
;(enable-interrupts) ; ... with interrupts disabled
24+
(void/reference-sink call-with-c-return-box)
25+
(raise x)))])
26+
(let ([vs (call-with-values thunk list)])
27+
;; Deliver successful values after returning back from
28+
;; `call-with-c-return`:
29+
(lambda ()
30+
(void/reference-sink call-with-c-return-box)
31+
(apply values vs)))))))
32+
(thunk)))
33+
34+
(define call-with-c-return-box (box #f))
35+
36+
;; `call-with-c-return` looks like a foreign function, due to a cast
37+
;; to and from a callback, so returning from `call-with-c-return` will
38+
;; pop and C frame stacks (via longjmp internally) that were pushed
39+
;; since `call-with-c-return` was called.
40+
(define call-with-c-return
41+
(and callback-atomic?
42+
(cast (lambda (thunk) (thunk))
43+
(_fun #:atomic? #t
44+
#:keep call-with-c-return-box
45+
_racket -> _racket)
46+
(_fun _racket -> _racket))))

0 commit comments

Comments
 (0)