File: djvm-model.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 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,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (235 lines) | stat: -rw-r--r-- 7,087 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
(in-package "ACL2")
(include-book "jvm-model")
(include-book "consistent-state")


;----------------------------------------------------------------------
;
;   defensive-JVM
;
;----------------------------------------------------------------------

(defun djvm-check-IFEQ (inst st)
  (let* ((target (arg inst))
         (pc     (get-pc st)))
    (and (consistent-state st)
         (consp (op-stack st))
         (pc-in-range (set-pc (+ 1 pc) st))
         (pc-in-range (set-pc target st)))))



(defun djvm-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 djvm-check-PUSH (inst st)
  (declare (ignore inst))
  (let* ((pc     (get-pc st)))
    (and (consistent-state st)
         (<= (+ 1 (len (op-stack st)))
             (g 'max-stack (topx (g 'call-stack st))))
         (pc-in-range (set-pc (+ 1 pc) st)))))


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

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

(defun djvm-check-INVOKE (inst st)
  (let* ((method-name  (arg inst))
         (method-table (g 'method-table st))
         (method   (binding method-name method-table))
         (nargs    (g 'nargs method)))
    (and (consistent-state st)
         (bound? method-name method-table)
         (<= 0 (g 'max-stack (binding method-name method-table)))
         (integerp nargs)
         (<= 0 nargs)
         (<= nargs (len (op-stack st)))
         (<= (+ 1 (- (len (op-stack st))
                     nargs))
             (g 'max-stack (topx (g 'call-stack st))))
         (pc-in-range (set-pc (+ 1 (get-pc st))
                              st)))))

;; this however does not guarantee that executing djvm-check will preserve
;; consistent-state
;;
;; because in the consistent-state we require that 

;;
;;          ;; the following two need to has to be true!!!  In the new BCV, we can
;;          ;; calculate the signiture at the each PC location!!
;;          ;; 
;;          (bound? pc (get-sig-vector caller method-table))
;;          (sig-frame-compatible
;;           (sig-frame-push-value (g 'ret callee) sig-frame)
;;           record-frame-sig))))
;;
;; However if we don't require this, we will have no way to show that BCV is
;; correct. 
;;
;; We know this is true, although djvm-check-INVOKE does not explicitly check
;; it!! 
;; ..... 
         
;;; Sun Nov 20 19:23:33 2005, we update consistent-state that assert bcv-method
;;; instead of bcv-simple-method!!!
;;;

(defun djvm-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   ;; Thu Nov  3 21:44:09 2005. note. 
     ;; what if the method-table is not well formed, 
     ;; binding method-name does not give a method of the correct 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 djvm-check-HALT (inst st)
  (declare (ignore inst))
  (consistent-state st))


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

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

(defun djvm-check-POP (inst st)
  (declare (ignore inst))
  (and (consistent-state st)
       (consp (op-stack st))
       (pc-in-range (set-pc (+ 1 (get-pc st))
                            st))))


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

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

(defun djvm-check-RETURN (inst st)
  (declare (ignore inst))
  (and (consistent-state st)
       (or (not (consp (popx (g 'call-stack st))))
           (and (consp (op-stack st))
                (<= (+ 1 (len (g 'op-stack (topx (popx (g 'call-stack st))))))
                    (g 'max-stack (topx (popx (g 'call-stack st)))))))))
                      


(defun djvm-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 djvm-check-step-g (inst st)
  (let* ((opcode (op-code inst)))
    (cond ((equal opcode 'INVOKE)
           (djvm-check-INVOKE inst st))
          ((equal opcode 'PUSH)
           (djvm-check-PUSH inst st))
          ((equal opcode 'IFEQ)
           (djvm-check-IFEQ inst st))
          ((equal opcode 'HALT)
           (djvm-check-HALT inst st))
          ((equal opcode 'POP)
           (djvm-check-POP inst st))
          ((equal opcode 'RETURN)
           (djvm-check-RETURN inst st))
          (t nil))))



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


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


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

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

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

;;
;; testing.
;;