diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2014-08-21 20:40:02 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2014-08-21 20:40:05 -0700 |
commit | 0a8858dbc5311461e0d853c00997c6bfca73f1bf (patch) | |
tree | 110069da6d589049df82b3fb9835b3c1359d63b0 | |
parent | 6a2edbe8cad4547921fa6d7307af47b5190a2b48 (diff) | |
download | mu-0a8858dbc5311461e0d853c00997c6bfca73f1bf.tar.gz |
66 - bounds checking
Currently baked into the processor model, but eventually will be emitted in generated code.
-rw-r--r-- | mu.arc | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/mu.arc b/mu.arc index 04a6af69..46bc7d44 100644 --- a/mu.arc +++ b/mu.arc @@ -87,7 +87,12 @@ (rep val@)) (= (memory* dest@) src@))))) +(def array-len (operand) + (m `(,v.operand integer))) + (def array-ref (operand idx) + (assert typeinfo.operand!array) + (assert (< -1 idx (array-len operand))) (withs (elem typeinfo.operand!elem offset (+ 1 (* idx sz.elem))) (m `(,(+ v.operand offset) ,elem)))) @@ -168,12 +173,14 @@ (if typeinfo.base!array ; array is an integer 'sz' followed by sz elems ; 'get' can only lookup its index - (m `(,v.base integer)) + (do (assert (is 0 idx)) + (array-len base)) ; field index - (m `(,(+ v.base - (apply + (map sz - (firstn idx typeinfo.base!elems)))) - ,typeinfo.base!elems.idx)))) + (do (assert (< -1 idx (len typeinfo.base!elems))) + (m `(,(+ v.base + (apply + (map sz + (firstn idx typeinfo.base!elems)))) + ,typeinfo.base!elems.idx))))) aref (array-ref arg.0 (v arg.1)) reply |