|
| 1 | +# Symbolic locals for functions |
| 2 | + |
| 3 | +This module contains rules that follow the same structure as `#init` in the KMIR module, however it reads the arguments for the signature of the start symbol and generates symbolic values for them. |
| 4 | + |
| 5 | +```k |
| 6 | +requires "../kmir.md" |
| 7 | +
|
| 8 | +module KMIR-SYMBOLIC-LOCALS [symbolic] |
| 9 | + imports KMIR-CONTROL-FLOW |
| 10 | +
|
| 11 | + rule <k> #execFunction( |
| 12 | + monoItem( |
| 13 | + SYMNAME, |
| 14 | + monoItemFn(_, _, someBody(body((FIRST:BasicBlock _) #as BLOCKS,RETURNLOCAL:LocalDecl LOCALS:LocalDecls, ARGCOUNT, _, _, _))) |
| 15 | + ), |
| 16 | + FUNCTIONNAMES |
| 17 | + ) |
| 18 | + => |
| 19 | + #reserveSymbolicsFor(LOCALS, ARGCOUNT) |
| 20 | + ~> #execBlock(FIRST) |
| 21 | + ... |
| 22 | + </k> |
| 23 | + <currentFunc> _ => #tyFromName(SYMNAME, FUNCTIONNAMES) </currentFunc> |
| 24 | + <currentFrame> |
| 25 | + <currentBody> _ => toKList(BLOCKS) </currentBody> |
| 26 | + <caller> _ => ty(-1) </caller> // no caller |
| 27 | + <dest> _ => place(local(-1), .ProjectionElems)</dest> |
| 28 | + <target> _ => noBasicBlockIdx </target> |
| 29 | + <unwind> _ => unwindActionUnreachable </unwind> |
| 30 | + <locals> _ => #reserveFor(RETURNLOCAL) </locals> |
| 31 | + </currentFrame> |
| 32 | + requires ARGCOUNT >Int 0 |
| 33 | + [priority(25)] |
| 34 | +``` |
| 35 | + |
| 36 | +## Declare symbolic arguments based on their types |
| 37 | + |
| 38 | +```k |
| 39 | + syntax KItem ::= #reserveSymbolicsFor( LocalDecls, Int ) |
| 40 | +
|
| 41 | + rule <k> #reserveSymbolicsFor( .LocalDecls, _ ) => .K ... </k> |
| 42 | +
|
| 43 | + rule <k> #reserveSymbolicsFor( LOCALS:LocalDecls, 0 ) => .K ... </k> |
| 44 | + <locals> ... .List => #reserveFor(LOCALS) </locals> // No more arguments, treat the rest of the Decls normally |
| 45 | +
|
| 46 | +``` |
| 47 | + |
| 48 | +## Integers |
| 49 | + |
| 50 | +```k |
| 51 | + // Unsigned |
| 52 | + rule <k> #reserveSymbolicsFor( localDecl(TY, _, MUT) LOCALS:LocalDecls, COUNT ) |
| 53 | + => #reserveSymbolicsFor( LOCALS:LocalDecls, COUNT -Int 1 ) |
| 54 | + ... |
| 55 | + </k> |
| 56 | + <locals> ... .List => ListItem(typedValue( Integer(?INT:Int, #bitWidth(PRIMTY), false), TY, MUT )) </locals> |
| 57 | + <types> ... TY |-> typeInfoPrimitiveType ( primTypeUint( PRIMTY ) ) ... </types> |
| 58 | + requires 0 <Int COUNT |
| 59 | + ensures #intConstraints( ?INT, PRIMTY ) |
| 60 | +
|
| 61 | + // Signed |
| 62 | + rule <k> #reserveSymbolicsFor( localDecl(TY, _, MUT) LOCALS:LocalDecls, COUNT ) |
| 63 | + => #reserveSymbolicsFor( LOCALS:LocalDecls, COUNT -Int 1 ) |
| 64 | + ... |
| 65 | + </k> |
| 66 | + <locals> ... .List => ListItem(typedValue( Integer(?INT:Int, #bitWidth(PRIMTY), true), TY, MUT )) </locals> |
| 67 | + <types> ... TY |-> typeInfoPrimitiveType ( primTypeInt( PRIMTY ) ) ... </types> |
| 68 | + requires 0 <Int COUNT |
| 69 | + ensures #intConstraints( ?INT, PRIMTY ) |
| 70 | +
|
| 71 | + syntax Bool ::= #intConstraints( Int, InTy ) [function, total] |
| 72 | +
|
| 73 | + rule #intConstraints( X, TY:IntTy ) => 0 -Int (2 ^Int (#bitWidth(TY) -Int 1)) <=Int X andBool X <Int 2 ^Int (#bitWidth(TY) -Int 1) |
| 74 | + rule #intConstraints( X, TY:UintTy ) => 0 <=Int X andBool X <Int 2 ^Int #bitWidth(TY) |
| 75 | +``` |
| 76 | + |
| 77 | +## Boolean Values |
| 78 | + |
| 79 | +```k |
| 80 | + rule <k> #reserveSymbolicsFor( localDecl(TY, _, MUT) LOCALS:LocalDecls, COUNT ) |
| 81 | + => #reserveSymbolicsFor( LOCALS:LocalDecls, COUNT -Int 1 ) |
| 82 | + ... |
| 83 | + </k> |
| 84 | + <locals> ... .List => ListItem(typedValue( BoolVal( ?_BOOL:Bool ), TY, MUT )) </locals> |
| 85 | + <types> ... TY |-> typeInfoPrimitiveType ( primTypeBool ) ... </types> |
| 86 | + requires 0 <Int COUNT |
| 87 | +``` |
| 88 | + |
| 89 | +## Arbitrary Values |
| 90 | + |
| 91 | +```k |
| 92 | + rule <k> #reserveSymbolicsFor( localDecl(TY, _, MUT) LOCALS:LocalDecls, COUNT ) |
| 93 | + => #reserveSymbolicsFor( LOCALS:LocalDecls, COUNT -Int 1 ) |
| 94 | + ... |
| 95 | + </k> |
| 96 | + <locals> ... .List => ListItem(typedValue( ?_VAL:Value, TY, MUT )) </locals> |
| 97 | + <types> ... TY |-> _:TypeInfo ... </types> |
| 98 | + requires 0 <Int COUNT |
| 99 | + [owise] |
| 100 | +
|
| 101 | +endmodule |
| 102 | +``` |
0 commit comments