File: jvm-model.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (236 lines) | stat: -rw-r--r-- 6,284 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
(in-package "ACL2")
;; definition of a tiny machine + its safe version + its bcv.
;; 
;; with ifeq, push, pop, invoke, return, halt

;------------------------------------------------------------
;
;   The SMALL Machine (with a more "correct" BCV)
;
;------------------------------------------------------------

(include-book "misc/records" :dir :system)

; Stacks
(defun pushx (obj stack) (cons obj stack))
(defun topx (stack) (car stack))
(defun popx (stack) (cdr stack))


; Alists
(defun bound? (x alist) (assoc-equal x alist))

(defun bind (x y alist)
  (cond ((endp alist) (list (cons x y)))
        ((equal x (car (car alist)))
         (cons (cons x y) (cdr alist)))
        (t (cons (car alist) (bind x y (cdr alist))))))

(defun binding (x alist) (cdr (bound? x alist))) 

; Instructions
(defun op-code (inst) (car inst))
(defun arg (inst) (car (cdr inst)))


(defun popStack (st)
  (let* ((call-stack (g 'call-stack st))
         (top-frame  (topx call-stack))
         (op-stack   (g 'op-stack top-frame)))
    (s 'call-stack 
       (pushx (s 'op-stack 
                 (popx op-stack)
                 top-frame)
              (popx call-stack))
       st)))

(defun pushStack (v st)
  (let* ((call-stack (g 'call-stack st))
         (top-frame  (topx call-stack))
         (op-stack   (g 'op-stack top-frame)))
    (s 'call-stack 
       (pushx (s 'op-stack 
                 (pushx v op-stack)
                 top-frame)
              (popx call-stack))
       st)))

(defun topStack (st)
  (let* ((call-stack (g 'call-stack st))
         (top-frame  (topx call-stack))
         (op-stack   (g 'op-stack top-frame)))
    (topx op-stack)))


(defun set-pc (pc st)
  (let* ((call-stack (g 'call-stack st))
         (top-frame  (topx call-stack)))
    (s 'call-stack 
       (pushx (s 'pc pc top-frame)
              (popx call-stack))
       st)))


(defun get-pc (st)
  (g 'pc (topx (g 'call-stack st))))


;----------------------------------------------------------------------


;----------------------------------------------------------------------

(defun execute-IFEQ (inst st)
  (let* ((target     (arg inst))
         (v          (topstack st))
         (pc         (get-pc st)))
    (if (equal v 0) 
        (set-pc target
                (popStack st))
      (set-pc (+ 1 pc)
              (popStack st)))))

;----------------------------------------------------------------------


(defun execute-PUSH (inst st)
  (let* ((v     (arg inst))
         (pc    (get-pc st)))
    (set-pc (+ 1 pc)
            (pushStack v st))))

;----------------------------------------------------------------------

(defun createInitFrame (method-name initial-locals st)
  (s 'max-stack (g 'max-stack 
                   (binding method-name (g 'method-table st)))
     (s 'pc 0
        (s 'method-name method-name
           (s 'locals initial-locals
              (s 'op-stack nil nil))))))
  

(defun pushInitFrame (method-name initial-locals st)
  (s 'call-stack  
     (pushx 
      (createInitFrame method-name initial-locals st)
      (g 'call-stack st))
     st))


(defun op-stack (st)
  (g 'op-stack 
     (topx (g 'call-stack st))))

(defun popStack-n (st n) 
  (if (zp n) st
    (popStack-n (popStack st)
                (- n 1))))

(defun init-locals (stack n)
  (if (zp n) nil
    (s n (topx stack)
       (init-locals (popx stack) (- n 1)))))


(defun execute-INVOKE (inst st)
  (let* ((method-name  (arg inst))
         (method-table (g 'method-table st))
         (method   (binding method-name method-table))
         (nargs    (g 'nargs method)))
    (pushInitFrame
     method-name 
     (init-locals (op-stack st) nargs)
     (set-pc (+ 1 (get-pc st))  
             ;;
             ;; we could avoid modifying the pc
             ;; and make the return instruction to modify the pc of caller
             ;; however we still face the problem that 
             ;; the caller's operand stack does not comply with the signature
             ;;
             (popStack-n st nargs)))))

;----------------------------------------------------------------------

(defun execute-HALT (inst st)
  (declare (ignore inst))
  st)

;----------------------------------------------------------------------


(defun execute-POP (inst st)
  (declare (ignore inst))
  (set-pc (+ 1 (get-pc st))
          (popStack st)))

;----------------------------------------------------------------------


(defun execute-RETURN (inst st)
  (declare (ignore inst))
  ;; update it to halt, when we know this is the very first frame!!  or update
  ;; our consistent-state predicate!
  (if (not (consp (popx (g 'call-stack st))))
      st
    (pushStack 
     (topStack st)
     (s 'call-stack
        (popx (g 'call-stack st))
        st))))

;----------------------------------------------------------------------
  
    
(defun current-method (st)
  (let* ((method-name (g 'method-name (topx (g 'call-stack st))))
         (method-table (g 'method-table st)))
    (binding method-name method-table)))


(defun pc-in-range (st)
  (let ((pc (g 'pc (topx (g 'call-stack st))))
        (code (g 'code (binding (g 'method-name 
                                   (topx (g 'call-stack st)))
                                (g 'method-table st)))))
    (and (integerp pc)
         (<= 0 pc)
         (< pc (len code)))))


(defun next-inst (st)
  (let* ((pc (get-pc st))
         (code (g 'code (current-method st))))
    (if (not (pc-in-range st))
        nil
      (nth pc code))))

;----------------------------------------------------------------------

(defun m-step (st)
  (let* ((inst (next-inst st))
         (opcode (op-code inst)))
    (cond ((equal opcode 'INVOKE) 
           (execute-INVOKE inst st))
          ((equal opcode 'PUSH)
           (execute-PUSH inst st))
          ((equal opcode 'IFEQ)
           (execute-IFEQ inst st))
          ((equal opcode 'HALT)
           (execute-HALT inst st))
          ((equal opcode 'POP)
           (execute-POP inst st))
          ((equal opcode 'RETURN)
           (execute-RETURN inst st))
          (t st))))


;----------------------------------------------------------------------

(defun m-run (st n)
  (if (zp n) st
    (m-run (m-step st) (- n 1))))

;----------------------------------------------------------------------