Skip to content

Commit c037103

Browse files
committed
Merge branch 'main' of github.com:cmsc430/www
2 parents 771cbf8 + 27dd4b3 commit c037103

6 files changed

Lines changed: 132 additions & 71 deletions

File tree

langs/a86/ast.rkt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
(error n "expects register; given ~v" a1))
3636
(unless (or (exact-integer? a2) (register? a2) (offset? a2))
3737
(error n "expects exact integer, register, or offset; given ~v" a2))
38+
(when (and (exact-integer? a2) (> (integer-length a2) 32))
39+
(error n "literal must not exceed 32-bits; given ~v (~v bits); go through a register instead" a2 (integer-length a2)))
3840
(values a1 a2)))
3941

4042
(define check:register
@@ -51,6 +53,22 @@
5153
(error n "expects register, offset, exact integer, or defined constant; given ~v" a2))
5254
(when (and (offset? a1) (offset? a2))
5355
(error n "cannot use two memory locations; given ~v, ~v" a1 a2))
56+
(when (and (exact-integer? a2) (> (integer-length a2) 32))
57+
(error n "literal must not exceed 32-bits; given ~v (~v bits); go through a register instead" a2 (integer-length a2)))
58+
(when (and (offset? a1) (exact-integer? a2))
59+
(error n "cannot use a memory locations and literal; given ~v, ~v; go through a register instead" a1 a2))
60+
(values a1 a2)))
61+
62+
(define check:mov
63+
(λ (a1 a2 n)
64+
(unless (or (register? a1) (offset? a1))
65+
(error n "expects register or offset; given ~v" a1))
66+
(unless (or (register? a2) (offset? a2) (exact-integer? a2) (Const? a2))
67+
(error n "expects register, offset, exact integer, or defined constant; given ~v" a2))
68+
(when (and (offset? a1) (offset? a2))
69+
(error n "cannot use two memory locations; given ~v, ~v" a1 a2))
70+
(when (and (exact-integer? a2) (> (integer-length a2) 64))
71+
(error n "literal must not exceed 64-bits; given ~v (~v bits)" a2 (integer-length a2)))
5472
(when (and (offset? a1) (exact-integer? a2))
5573
(error n "cannot use a memory locations and literal; given ~v, ~v; go through a register instead" a1 a2))
5674
(values a1 a2)))
@@ -76,6 +94,8 @@
7694
(λ (a1 n)
7795
(unless (or (exact-integer? a1) (register? a1))
7896
(error n "expects exact integer or register; given ~v" a1))
97+
(when (and (exact-integer? a1) (> (integer-length a1) 32))
98+
(error n "literal must not exceed 32-bits; given ~v (~v bits); go through a register instead" a1 (integer-length a1)))
7999
a1))
80100

81101
(define check:lea
@@ -126,7 +146,7 @@
126146
(instruct Label (x) check:label-symbol)
127147
(instruct Call (x) check:target)
128148
(instruct Ret () check:none)
129-
(instruct Mov (dst src) check:src-dest)
149+
(instruct Mov (dst src) check:mov)
130150
(instruct Add (dst src) check:arith)
131151
(instruct Sub (dst src) check:arith)
132152
(instruct Cmp (a1 a2) check:src-dest)
@@ -169,14 +189,22 @@
169189
(symbol? x)
170190
(integer? x)))
171191

172-
(provide offset? register? instruction? label?)
192+
(provide offset? register? instruction? label? 64-bit-integer? 32-bit-integer?)
173193

174194
(define offset? Offset?)
175195

176196
(define (register? x)
177197
(and (memq x '(cl eax rax rbx rcx rdx rbp rsp rsi rdi r8 r9 r10 r11 r12 r13 r14 r15))
178198
#t))
179199

200+
(define (64-bit-integer? x)
201+
(and (exact-integer? x)
202+
(<= (integer-length x) 64)))
203+
204+
(define (32-bit-integer? x)
205+
(and (exact-integer? x)
206+
(<= (integer-length x) 32)))
207+
180208
(define (label? x)
181209
(and (symbol? x)
182210
(nasm-label? x)

langs/a86/test/errors.rkt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,21 @@
22
(require rackunit "../ast.rkt")
33
(check-exn exn:fail?
44
(thunk (Mov (Offset 'rax 0) 100)))
5+
6+
;; Checking literal widths
7+
(check-exn exn:fail? (thunk (Mov 'rax (expt 2 64))))
8+
(check-not-exn (thunk (Mov 'rax (sub1 (expt 2 64)))))
9+
(check-exn exn:fail? (thunk (Cmp 'rax (expt 2 32))))
10+
(check-not-exn (thunk (Cmp 'rax (sub1 (expt 2 32)))))
11+
(check-exn exn:fail? (thunk (And 'rax (expt 2 32))))
12+
(check-not-exn (thunk (And 'rax (sub1 (expt 2 32)))))
13+
(check-exn exn:fail? (thunk (Or 'rax (expt 2 32))))
14+
(check-not-exn (thunk (Or 'rax (sub1 (expt 2 32)))))
15+
(check-exn exn:fail? (thunk (Xor 'rax (expt 2 32))))
16+
(check-not-exn (thunk (Xor 'rax (sub1 (expt 2 32)))))
17+
(check-exn exn:fail? (thunk (Push (expt 2 32))))
18+
(check-not-exn (thunk (Push (sub1 (expt 2 32)))))
19+
(check-exn exn:fail? (thunk (Add 'rax (expt 2 32))))
20+
(check-not-exn (thunk (Add 'rax (sub1 (expt 2 32)))))
21+
(check-exn exn:fail? (thunk (Sub 'rax (expt 2 32))))
22+
(check-not-exn (thunk (Sub 'rax (sub1 (expt 2 32)))))

www/assignments.scrbl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@include-section{assignments/3.scrbl}
99
@include-section{assignments/4.scrbl}
1010
@include-section{assignments/5.scrbl}
11-
@;include-section{assignments/6.scrbl}
11+
@include-section{assignments/6.scrbl}
1212
@;;include-section{assignments/7.scrbl}
1313

1414
@;{assignment 8: quote in general, and quasiquote}

www/assignments/5.scrbl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'("ast.rkt" "parse.rkt" "interp.rkt"))
1111

1212

13-
@bold{Due: Monday, November 7, 11:59PM EDT}
13+
@bold{Due: @elem[#:style "strike"]{Monday, November 7, 11:59PM EST}, Tuesday, November 8, 11:59PM EST}
1414

1515
The goal of this assignment is to extend a compiler with arity
1616
checking for function calls, to add new kinds of function parameter

www/assignments/6.scrbl

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,81 @@
11
#lang scribble/manual
2-
@title[#:tag "Assignment 6" #:style 'unnumbered]{Assignment 6: Syntax Checking}
2+
@title[#:tag "Assignment 6" #:style 'unnumbered]{Assignment 6: Squid Game}
33

44
@(require (for-label (except-in racket ...)))
55
@(require "../notes/ev.rkt"
66
"../notes/utils.rkt")
77

8-
@bold{Due: Thursday, May 5th, 11:59PM EDT}
8+
@bold{Due: Monday, Nov 14, 11:59PM EST}
99

10-
The goal of this assignment is to add syntax checking to our compiler.
10+
The goal of this assignment is to hone your testing skills.
1111

12-
You are given a repository with a starter compiler similar to the
13-
@seclink["Mountebank"]{Mountebank} language we studied in class. You
14-
are tasked with:
12+
@section[#:tag-prefix "a6-" #:style 'unnumbered #:tag "game"]{The Game}
1513

16-
@itemlist[
17-
18-
@item{implementing compile-time syntax checking.}
19-
20-
]
21-
22-
@section[#:tag-prefix "a6-" #:style 'unnumbered #:tag "checking"]{Syntax Checking}
14+
The autograder for this assignment includes a collection of compilers
15+
that implement @secref["Assignment 5"] and a reference interpreter.
2316

17+
You must submit a list of programs that will be run on each compiler.
18+
If a compiler produces a result that is inconsistent with the
19+
reference interpreter, it is eliminated. Your goal is to construct
20+
a set of test programs that eliminate the largest number of compilers.
21+
The player that eliminates the largest number of compilers, wins.
2422

25-
Up until now, we've written our compiler assuming that programs are
26-
well-formed, but there has never been any code that actually checks
27-
this assumption. The assumptions go beyond the properties checked
28-
during parsing. For example, our parser will happily accept a program
29-
like @racket[(lambda (x x) x)], and the compiler will emit code for it
30-
even though this is not a well-formed program.
23+
When you submit, choose a name to display on the leaderboard. It does
24+
not need to be your real name, but please keep it appropriate for this
25+
setting.
3126

32-
The idea of this assignment is to implement a function, defined in
33-
@tt{check-syntax.rkt}:
27+
After submitting, click "Leaderboard" to see the latest standings.
3428

35-
@#reader scribble/comment-reader
36-
(racketblock
37-
;; Prog -> Prog
38-
(define (check-syntax p) ...)
39-
)
29+
There are 133 compilers included. Your score will be the number of
30+
compilers you are able to eliminate, with a maximum score of 100.
4031

41-
If the program is well-formed, it should behave like the identity
42-
function, returning the same program it was given as input. On
43-
ill-formed programs, it should signal an error using @racket[error].
32+
We reserve the right to update the reference interpreter and will
33+
announce any changes on Discord.
4434

45-
For the purposes of this assignment, the quality of the error messages
46-
doesn't matter, although in real programming language implementations,
47-
good error messages are crucial.
48-
49-
Here are the properties that should be checked of each program:
35+
The following updates have been made since the release:
5036

5137
@itemlist[
5238

53-
@item{Programs are @emph{closed}; there are no unbound variables in
54-
the program.}
39+
@item{The interpreter now checks for integer overflow and crashes when
40+
this happens, thereby making overflow behavior unspecified for the compilers.}
41+
42+
@item{The interpreter now crashes when interpreting unbound variables
43+
instead of producing @racket['err], making unbound variable behavior
44+
unspecified.}
5545

56-
@item{Every @racket[define]d function should have a distinct name.}
46+
]
5747

58-
@item{Every function parameter should be distinct from the other
59-
parameters of that function.}
48+
Submissions should be written using the following format:
6049

61-
@item{Every function's name should be distinct from all of its
62-
parameters' names.}
50+
@codeblock|{
51+
#lang info
52+
(define programs
53+
(list
54+
'[ (add1 1) ]
55+
'[ (write-byte 97) ]
56+
'[ (define (f x) (+ x x)) (f 5) ]))
57+
}|
6358

64-
@item{Every function name and variable should not clash with any of
65-
the keywords of our language, e.g. @racket[lambda], @racket[if], etc.
66-
(Note this is not a restriction Racket puts on programs; the following
67-
is a perfectly reasonable expression: @racket[(λ (λ) λ)], but we'll
68-
consider this a syntax error.)}
59+
If you'd like to include a program reads data from the standard input
60+
port, you can add an enties which are two-element lists, where the first
61+
element is a string that is used as the contents of the input port
62+
and the second element is the program, for example:
6963

70-
@item{Every pattern variable in a pattern should be distinct. (Racket
71-
also allows this, but it has a complicated run-time semantics which we
72-
don't implement so instead we just rule out these programs.)}
64+
@codeblock|{
65+
#lang info
66+
(define programs
67+
(list
68+
'[ (add1 1) ]
69+
'[ (write-byte 97) ]
70+
'[ "abc" [ (read-byte) ]]
71+
'[ (define (f x) (+ x x)) (f 5) ]))
72+
}|
7373

74-
]
7574

76-
The starter code calls @racket[check-syntax] in both
77-
@tt{compile-file.rkt} and @tt{interp-file.rkt}. The definition of
78-
@racket[check-syntax] is stubbed out in @tt{check-syntax.rkt}.
75+
You may add as many programs as you'd like to the file.
7976

80-
There are tests included in @tt{test/check-syntax.rkt}. You can also use this file
81-
to write your own tests for debugging.
8277

8378
@section[#:tag-prefix "a6-" #:style 'unnumbered]{Submitting}
8479

85-
You should submit on Gradescope. You should submit a zip file that has
86-
exactly the same structure that the stub contains. We will only use
87-
the @tt{check-syntax.rkt} files for grading, so make sure all your
88-
work is contained there!
80+
You should submit on Gradescope. You should a single file named
81+
@tt{info.rkt} that conforms to the format shown above.

www/notes/a86.scrbl

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,28 @@ Each register plays the same role as in x86, so for example
720720
A predicate for offsets.
721721
}
722722

723+
@defproc[(64-bit-integer? [x any/c]) boolean?]{
724+
A predicate for determining if a value is an integer that fits in 64-bits.
725+
726+
@ex[
727+
(64-bit-integer? 0)
728+
(64-bit-integer? (sub1 (expt 2 64)))
729+
(64-bit-integer? (expt 2 64))
730+
(64-bit-integer? (- (expt 2 63)))
731+
(64-bit-integer? (sub1 (- (expt 2 63))))]
732+
}
733+
734+
@defproc[(32-bit-integer? [x any/c]) boolean?]{
735+
A predicate for determining if a value is an integer that fits in 64-bits.
736+
737+
@ex[
738+
(32-bit-integer? 0)
739+
(32-bit-integer? (sub1 (expt 2 32)))
740+
(32-bit-integer? (expt 2 32))
741+
(32-bit-integer? (- (expt 2 32)))
742+
(32-bit-integer? (sub1 (- (expt 2 32))))]
743+
}
744+
723745
@defproc[(seq [x (or/c instruction? (listof instruction?))] ...) (listof instruction?)]{
724746
A convenience function for splicing togeter instructions and lists of instructions.
725747

@@ -866,7 +888,7 @@ Each register plays the same role as in x86, so for example
866888

867889
}
868890

869-
@defstruct*[Mov ([dst (or/c register? offset?)] [src (or/c register? offset? exact-integer?)])]{
891+
@defstruct*[Mov ([dst (or/c register? offset?)] [src (or/c register? offset? 64-bit-integer?)])]{
870892

871893
A move instruction. Moves @racket[src] to @racket[dst].
872894

@@ -885,7 +907,7 @@ Each register plays the same role as in x86, so for example
885907

886908
}
887909

888-
@defstruct*[Add ([dst register?] [src (or/c register? offset? exact-integer?)])]{
910+
@defstruct*[Add ([dst register?] [src (or/c register? offset? 32-bit-integer?)])]{
889911

890912
An addition instruction. Adds @racket[src] to @racket[dst]
891913
and writes the result to @racket[dst].
@@ -901,7 +923,7 @@ Each register plays the same role as in x86, so for example
901923
]
902924
}
903925

904-
@defstruct*[Sub ([dst register?] [src (or/c register? offset? exact-integer?)])]{
926+
@defstruct*[Sub ([dst register?] [src (or/c register? offset? 32-bit-integer?)])]{
905927

906928
A subtraction instruction. Subtracts @racket[src] frrom
907929
@racket[dst] and writes the result to @racket[dst].
@@ -917,7 +939,7 @@ Each register plays the same role as in x86, so for example
917939
]
918940
}
919941

920-
@defstruct*[Cmp ([a1 (or/c register? offset?)] [a2 (or/c register? offset? exact-integer?)])]{
942+
@defstruct*[Cmp ([a1 (or/c register? offset?)] [a2 (or/c register? offset? 32-bit-integer?)])]{
921943
Compare @racket[a1] to @racket[a2]. Doing a comparison
922944
sets the status flags that affect the conditional instructions like @racket[Je], @racket[Jl], etc.
923945

@@ -1028,7 +1050,7 @@ Each register plays the same role as in x86, so for example
10281050
]
10291051
}
10301052

1031-
@defstruct*[And ([dst (or/c register? offset?)] [src (or/c register? offset? exact-integer?)])]{
1053+
@defstruct*[And ([dst (or/c register? offset?)] [src (or/c register? offset? 32-bit-integer?)])]{
10321054
Compute logical ``and'' of @racket[dst] and @racket[src] and put result in @racket[dst].
10331055

10341056
@#reader scribble/comment-reader
@@ -1043,7 +1065,7 @@ Each register plays the same role as in x86, so for example
10431065
)
10441066
}
10451067

1046-
@defstruct*[Or ([dst (or/c register? offset?)] [src (or/c register? offset? exact-integer?)])]{
1068+
@defstruct*[Or ([dst (or/c register? offset?)] [src (or/c register? offset? 32-bit-integer?)])]{
10471069
Compute logical ``or'' of @racket[dst] and @racket[src] and put result in @racket[dst].
10481070

10491071
@#reader scribble/comment-reader
@@ -1058,7 +1080,7 @@ Each register plays the same role as in x86, so for example
10581080
)
10591081
}
10601082

1061-
@defstruct*[Xor ([dst (or/c register? offset?)] [src (or/c register? offset? exact-integer?)])]{
1083+
@defstruct*[Xor ([dst (or/c register? offset?)] [src (or/c register? offset? 32-bit-integer?)])]{
10621084
Compute logical ``exclusive or'' of @racket[dst] and @racket[src] and put result in @racket[dst].
10631085

10641086
@#reader scribble/comment-reader
@@ -1113,7 +1135,7 @@ Each register plays the same role as in x86, so for example
11131135
)
11141136
}
11151137

1116-
@defstruct*[Push ([a1 (or/c exact-integer? register?)])]{
1138+
@defstruct*[Push ([a1 (or/c 32-bit-integer? register?)])]{
11171139

11181140
Decrements the stack pointer and then stores the source
11191141
operand on the top of the stack.

0 commit comments

Comments
 (0)