66 - bounds checking

Currently baked into the processor model, but eventually will be emitted
in generated code.
This commit is contained in:
Kartik K. Agaram 2014-08-21 20:40:02 -07:00
parent 6a2edbe8ca
commit 0a8858dbc5
1 changed files with 12 additions and 5 deletions

17
mu.arc
View File

@ -87,7 +87,12 @@
(rep val@)) (rep val@))
(= (memory* dest@) src@))))) (= (memory* dest@) src@)))))
(def array-len (operand)
(m `(,v.operand integer)))
(def array-ref (operand idx) (def array-ref (operand idx)
(assert typeinfo.operand!array)
(assert (< -1 idx (array-len operand)))
(withs (elem typeinfo.operand!elem (withs (elem typeinfo.operand!elem
offset (+ 1 (* idx sz.elem))) offset (+ 1 (* idx sz.elem)))
(m `(,(+ v.operand offset) ,elem)))) (m `(,(+ v.operand offset) ,elem))))
@ -168,12 +173,14 @@
(if typeinfo.base!array (if typeinfo.base!array
; array is an integer 'sz' followed by sz elems ; array is an integer 'sz' followed by sz elems
; 'get' can only lookup its index ; 'get' can only lookup its index
(m `(,v.base integer)) (do (assert (is 0 idx))
(array-len base))
; field index ; field index
(m `(,(+ v.base (do (assert (< -1 idx (len typeinfo.base!elems)))
(apply + (map sz (m `(,(+ v.base
(firstn idx typeinfo.base!elems)))) (apply + (map sz
,typeinfo.base!elems.idx)))) (firstn idx typeinfo.base!elems))))
,typeinfo.base!elems.idx)))))
aref aref
(array-ref arg.0 (v arg.1)) (array-ref arg.0 (v arg.1))
reply reply