| 453 | | === Function Contracts === |
| 454 | | |
| 455 | | [wiki:Function_Contracts] |
| 456 | | |
| 457 | | * action expressions: |
| 458 | | * `read(e)`: `e` has type `Mem`. The function reads the memory units specified by `e`. |
| 459 | | * `write(e)`: `e` has type `Mem`. The function writes the memory units specified by `e`. |
| 460 | | * `call(f, <e1, ...>)`: `f` has type `Function`. The function calls `f` with parameters `e1, ...`. |
| 461 | | * `noaction`: no action |
| 462 | | * `anyaction`: all actions |
| 463 | | * `act_union(ma0, ma1)`: the union of the two memory set actions `ma0` and `ma1`. |
| 464 | | * `act_comp(ma0, ma1)`: action complement (everything not in `ma1`) |
| 465 | | * `act_isect(ma0, ma1)`: action intersection |
| 466 | | |
| 467 | | * depends clause: `depends action0, action1, action2, ... ` |
| 468 | | * Example: `depends act_union(act_comp(read(n), call(inc, <x>))), call(dec, <y>))` |
| 469 | | * absence of depends clause: there is no information about the dependency of the function, which means that the analysis needs to figure it out by itself. |
| 470 | | * some facts about assigns-or-reads clause |
| 471 | | * absence of `reads`/`assigns` clause: there is no assumption about the read/assign access of the function, i.e., the function could read/assign anything |
| 472 | | * absence of `assigns` clause: similar to the absence of `reads` clause |
| 473 | | * `reads/assigns nothing` doesn’t necessarily means that the function never reads or assigns any variable. The function could still reads/assigns its “local” variables, including function parameters and any variable declared inside the function body. **check it** |
| 474 | | |
| 475 | | * For an independent function which has `depends coaction`, usually we also need to specify `reads nothing`, for the purpose of reachability analysis. |
| 476 | | e.g., |
| 477 | | {{{ |
| 478 | | /* Returns the size of the given bundle b. */ |
| 479 | | bundle_size(Bundle b): Integer |
| 480 | | depends nothing |
| 481 | | reads b |
| 482 | | ; |
| 483 | | }}} |
| 484 | | |
| 485 | | * Example of a function declaration with contracts: |
| 486 | | {{{ |
| 487 | | fun[atomic] sendRecv(Int cmd, Pointer buf): Integer |
| 488 | | reads deref(buf); |
| 489 | | behavior send: |
| 490 | | depends writes(buf); |
| 491 | | assigns nothing; |
| 492 | | behavior receive: |
| 493 | | depends access(deref(buf)); |
| 494 | | assigns deref(buf); |
| 495 | | { |
| 496 | | begin choose |
| 497 | | when eq(cmd, 0) |
| 498 | | do CALL send, <buf, ...>; goto L1; |
| 499 | | when eq(cmd, 1) |
| 500 | | do CALL recv, <buf, ...>; goto L1; |
| 501 | | when and(neq(cmd, 0), neq(cmd, 1)) |
| 502 | | do NOOP; goto L1; |
| 503 | | end choose |
| 504 | | L1: |
| 505 | | } |
| 506 | | }}} |
| | 453 | Function Contracts: |
| | 454 | * [wiki:Function_Contracts] |