; CS378 A Formal Model of the JVM Spring, 2012 / March 7, 2012 ; J Strother Moore Midterm Exam ; ; This exam is 3 pages long, plus three attachments: copies of m1.lisp, ; m1-support.lisp, and template.lisp for reference only. There are six ; problems, each worth a specified number of points. 100 points are available ; in all. Partial credit will be given. Write clearly. You are free to ; define auxiliary functions in answering any of the questions. Make sure you ; write your name on each page turned in. You have until 5:00 pm. ; (include-book "m1/m1-lemmas") (in-package "M1") ; ---------------------------------------------------------------------------- ; ; Problem 1 (30 points -- 5 points for each item below) : Express these ideas ; about an M1 state s. ; ; (a) ``the instruction that will be executed when s is stepped'' ; Answer: (next-inst s) ; (b) ``the value of local variable 2 in s'' ; Answer: (nth 2 (locals s)) ; (c) ``the item just below the topmost item on the stack of s'' ; Answer: (top (pop (stack s))) ; (d) ``the state obtained from s by incrementing the pc and setting local ; variable 2 to 0'' ; Answer: (make-state (+ 1 (pc s)) (update-nth 2 0 (locals s)) (stack s) (program s)) ; (e) ``the number of HALT instructions in the program of s'' ; Helper function: (defun how-many (e lst) (if (endp lst) 0 (if (equal e (car lst)) (+ 1 (how-many e (cdr lst))) (how-many e (cdr lst))))) ; Answer: (how-many '(HALT) (program s)) ; Test of helper function: (defconst *example-pi* '((ILOAD 1) (IFEQ 12) (ILOAD 0) (IFEQ 12) (ILOAD 0) (ICONST 1) (ISUB) (ISTORE 0) (ILOAD 1) (ICONST 1) (ISUB) (ISTORE 1) (GOTO -12) (ICONST 0) (HALT) (ICONST 1) (HALT) )) (how-many '(HALT) *example-pi*) ; = 2 ; (f) ``the highest numbered local variable used in the program of s.'' Return ; -1 if the program uses no local variables. By the way, (max i j) is an ; ACL2 expression that returns the maximum of the numbers i and j. ; Helper function: (defun max-var (program) (if (endp program) -1 (if (or (equal (op-code (car program)) 'ILOAD) (equal (op-code (car program)) 'ISTORE)) (acl2::max (arg1 (car program)) (max-var (cdr program))) (max-var (cdr program))))) ; Answer: (max-var (program s)) ; Test of helper function: (max-var *example-pi*) ; = 1 ; ---------------------------------------------------------------------------- ; ; Problem 2 (10 points): Suppose we decided to change M1 so that the machine ; halts if an ISTORE is executed when there is nothing on the stack. How would ; you redefine execute-ISTORE, which I've shown below for your reference? ; Answer: (defun execute-ISTORE (inst s) (if (endp (stack s)) s (make-state (+ 1 (pc s)) (update-nth (arg1 inst) (top (stack s)) (locals s)) (pop (stack s)) (program s)))) ; ---------------------------------------------------------------------------- ; ; Problem 3 (15 points): Extend M1 to include the following instruction. It ; suffices merely to define the semantic function, execute-IFEQDEC. ; ; Instruction: (IFEQDEC index delta) ; ; Stack: ... ==> ... ; ; Description: Decrement the local variable at index and if the new value is ; equal to 0, increment the pc by delta. (Otherwise, go to the next ; instruction.) ; Answer: (defun execute-IFEQDEC (inst s) (make-state (if (equal (- (nth (arg1 inst) (locals s)) 1) 0) (+ (pc s) (arg2 inst)) (+ (pc s) 1)) (update-nth (arg1 inst) (- (nth (arg1 inst) (locals s)) 1) (locals s)) (stack s) (program s))) ; Example: (execute-IFEQDEC '(IFEQDEC 3 20) (make-state 10 '(100 200 1 300) nil ; stack irrelevant '(inst1 inst2 etc))) ; program irrelevant ; = (11 ; <--- no jump ; (100 200 1 299) ; <--- var decremented ; NIL ; (INST1 INST2 ETC)) (execute-IFEQDEC '(IFEQDEC 2 20) (make-state 10 '(100 200 1 300) nil '(inst1 inst2 etc))) ; = (30 ; <--- jumped by 20 ; (100 200 0 300) ; <--- var decremented ; NIL ; (INST1 INST2 ETC)) ; ---------------------------------------------------------------------------- ; ; Problem 4 (15 points): Extend M1 to include the following instruction. It ; suffices merely to define the semantic function, execute-POPN: ; ; Instruction: (POPN) ; ; Stack: ... v1,---, vn, n ==> ... ; ; Description: Pop n values off the stack, where n is the topmost item on the ; stack. You may assume that n is a natural number and that there are at least ; n+1 items on the stack. ; Answer: (defun popn (n stk) ; this is actually defined in m1-support.lisp but (if (zp n) ; many students might not have noticed and would stk ; define it. (popn (- n 1) (pop stk)))) (defun execute-POPN (inst s) (declare (ignore inst)) ; For grading purposes, this (make-state (+ 1 (pc s)) ; declaration is irrelevant (locals s) (popn (top (stack s)) (pop (stack s))) (program s))) ; Example: (execute-POPN '(POPN) (make-state 0 nil ; locals irrelevant '(2 A B C D E) '(inst1 inst2 etc))) ; program irrelevant ; = (1 ; <--- pc incremented ; NIL ; (C D E) ; <--- 2 additional items removed ; (INST1 INST2 ETC)) ; (making 3 removed) ; ---------------------------------------------------------------------------- ; ; Problem 5 (15 points): Write the schedule function for the following code. ; Name the local variables x, y, and a (locals 0, 1, and 2) respectively. You ; may assume x, y, and a are natural numbers. ; ; (ILOAD 0) ; 0 ; (ILOAD 1) ; 1 ; (IADD) ; 2 ; (ISTORE 2) ; 3 ; (ILOAD 0) ; 4 ; (IFEQ 14) ; 5 ; (ILOAD 0) ; 6 ; (ICONST 1) ; 7 ; (ISUB) ; 8 ; (ISTORE 0) ; 9 ; (ILOAD 2) ; 10 ; (ILOAD 1) ; 11 ; (IFEQ 3) ; 12 ; (ICONST 1) ; 13 ; (IADD) ; 14 ; (ICONST 1) ; 15 ; (IADD) ; 16 ; (ISTORE 2) ; 17 ; (GOTO -14) ; 18 ; (ILOAD 2) ; 19 ; (HALT) ; 20 ; Answer: (defun loop-sched (x y) (if (zp x) (repeat 'tick 3) (if (zp y) (ap (repeat 'tick 13) (loop-sched (- x 1) y)) (ap (repeat 'tick 15) (loop-sched (- x 1) y))))) (defun sched (x y) (ap (repeat 'tick 4) (loop-sched x y))) ; To convince myself that this schedule is correct, I proved this program ; correct using this schedule. The proof will be posted with the other M1 ; program proofs on the class web page, under the name midterm-pi.lisp. ; ---------------------------------------------------------------------------- ; ; Problem 6 (15 points): To compute 2^n (two raised to the nth power) for a ; natural number n, we could use the following algorithm: ; ; (defun helper (n a) ; (if (zp n) ; a ; (helper (- n 1) (+ a a)))) ; ; (defun fn (n) (helper n 1)) ; ; Write down the two theorems needed to prove the algorithm correct, i.e., the ; theorems called helper-is-theta and fn-is-theta in the template. ; Answer: (defthm helper-is-theta (implies (and (ok-inputs n) (natp a)) (equal (helper n a) (* a (theta n))))) (defthm fn-is-theta (implies (ok-inputs n) (equal (fn n) (theta n)))) ; (ok-inputs n) is just (natp n). (theta n) is just (expt 2 n). ; To convince myself these are correct, I proved the corresponding M1 program ; correct. The proof will be posted with the other M1 program proofs on the ; class web page, under the name power.lisp. ; ---------------------------------------------------------------------------- ; ; [page 3 of 3] The End