ECMA-262 in Lean

Reading page · generated from spec/spectec/prose/07-memory-model.spectec, 08-memory-read.spectec, 09-memory-view.spectec

The memory model (§29)

ECMA-262 §29 formalized in SpecTec: candidate executions (Exec), Agent Events Records, happens-before (HB), sequentially consistent memory order (MemoryOrder), valid executions (ValidWith), and races (§29.1–§29.10), with 19 quoted laws, plus how chooseReadsValid reads a candidate execution from the store and appends a shared read.

Candidate executions, relations and validity (§29.1–§29.10)

From spec/spectec/prose/07-memory-model.spectec

A candidate execution as the memory model sees it (Exec): its Agent Events Records, its Chosen Value Records and its events' fields, each event by identity. Over it, with a reads-bytes-from function and the host's host-synchronizes-with: the relations of §29.6, the properties of §29.7 and races, data races and data race freedom (§29.8-29.10). The errata, each at one reading (none of them switchable): MemoryRangeSameBlock at its proposed text (Rbf.Valid, CoherentReads), RacesNotBoth and InitHappensBeforeOverlap at their literal text (Race, HB.init). ValueOfReadEvent and ComposeWriteEventBytes are a least fixpoint (ValueOf, Compose): a cycle of ReadModifyWriteSharedMemory events reading from each other has no value, so it fails valid chosen reads.

syntax

MemoryModel.Kind

07-memory-model.spectec:17

A Memory event's kind (§29.1, sec-memory-model-fundamentals).

syntax Kindderiving [DecidableEq, Repr]
syntax

MemoryModel.Order

07-memory-model.spectec:27

A Shared Data Block event's [[Order]] (§29.1, sec-memory-model-fundamentals).

syntax Orderderiving [DecidableEq, Repr]
syntax record

MemoryModel.EventsRecord

07-memory-model.spectec:35

An Agent Events Record (§29.2, sec-agent-event-records): its [[EventList]] and [[AgentSynchronizesWith]].

syntax EventsRecord (E : Type) record
syncs : List(E times E);
syntax record

MemoryModel.Exec

07-memory-model.spectec:42

A candidate execution (§29.4, sec-candidate-executions) and the fields of its events (§29.1, sec-memory-model-fundamentals), events E by identity.

syntax Exec (M : Model) (E : Type) record

[[ModifyOp]] called on a List of byte values and [[Payload]]; none where the call has no result

modify : E -> List(M.Math) -> List(M.Math) -> Option(List(M.Math));
syntax

MemoryModel.Rbf

07-memory-model.spectec:58

A reads-bytes-from function (§29.6.2, sec-reads-bytes-from).

syntax Rbf (E : Type) = E -> List(E)
syntax

MemoryModel.Rel

07-memory-model.spectec:61

A Relation on Memory events (§29.1, sec-memory-model-fundamentals; §29.6, sec-relations-of-candidate-executions).

syntax Rel (E : Type) = E -> E -> Prop
relation

MemoryModel.Plus

07-memory-model.spectec:64

The transitive closure of a Relation (§29.6.4, sec-host-synchronizes-with; §29.6.6, sec-happens-before).

relation Plus {E : Type} (r : Rel(E)) : Rel(E)
rule step {a b : E} : Plus(r, a, b) :- r(a, b);
laws

MemoryModel.StrictPartialOrder

07-memory-model.spectec:71

A strict partial order on Memory events (§29.6.4, sec-host-synchronizes-with; §29.7.5, sec-valid-executions): irreflexive and transitive.

Proved for happens-before and memory-order across shared reads in Ecma.MemoryModel.snoc_valid.

laws StrictPartialOrder {E : Type} (r : Rel(E))

2 laws

29.7.5 Valid Executions sec-valid-executions

execution admits a happens-before Relation that is a strict partial order.

A strict partial order relates no event to itself.

irrefl
SpecTec
law irrefl : forall a, not r(a, a);

A strict partial order is transitive.

trans
SpecTec
law trans : forall a b c, r(a, b) -> r(b, c) -> r(a, c);
Auxiliary definitions (15): MemoryModel.At, MemoryModel.isSdb, MemoryModel.isRead, MemoryModel.isWrite, …
def

MemoryModel.At

07-memory-model.spectec:83

l[k] for a mathematical value k: the element at the integer index k.

def At {M : Model} (l : List(M.Math)) (k : M.Math) (b : M.Math) : Prop := exists (i : Nat), M.eq(M.ofNat(i), k) = true /\ l[i]? = some(b)
def

MemoryModel.isSdb

07-memory-model.spectec:88

A Shared Data Block event: a ReadSharedMemory, WriteSharedMemory or ReadModifyWriteSharedMemory.

def isSdb (e : E) : Prop := x.kind(e) = .read \/ x.kind(e) = .write \/ x.kind(e) = .rmw
def

MemoryModel.isRead

07-memory-model.spectec:91

A read event: a ReadSharedMemory or a ReadModifyWriteSharedMemory.

def isRead (e : E) : Prop := x.kind(e) = .read \/ x.kind(e) = .rmw
def

MemoryModel.isWrite

07-memory-model.spectec:94

A write event: a WriteSharedMemory or a ReadModifyWriteSharedMemory.

def isWrite (e : E) : Prop := x.kind(e) = .write \/ x.kind(e) = .rmw
def

MemoryModel.inEvents

07-memory-model.spectec:97

EventSet(execution) contains e (§29.5.1).

def inEvents (e : E) : Prop := exists r mem x.records, e mem r.events
def

MemoryModel.inSdb

07-memory-model.spectec:100

SharedDataBlockEventSet(execution) contains e (§29.5.2).

def inSdb (e : E) : Prop := inEvents(x, e) /\ isSdb(x, e)
def

MemoryModel.inHost

07-memory-model.spectec:103

HostEventSet(execution) contains e (§29.5.3).

def inHost (e : E) : Prop := inEvents(x, e) /\ not inSdb(x, e)
def

MemoryModel.inRange

07-memory-model.spectec:107

e has b in its memory range: an integer from [[ByteIndex]] (inclusive) to [[ByteIndex]] + [[ElementSize]] (exclusive) (§29.1).

def inRange (e : E) (b : M.Math) : Prop := M.isInt(b) = true /\ M.lt(b, x.byteIndex(e)) = false /\ M.lt(b, M.add(x.byteIndex(e), x.elementSize(e))) = true
def

MemoryModel.rangesEqual

07-memory-model.spectec:110

Equal memory ranges: the same [[Block]], [[ByteIndex]] and [[ElementSize]] (§29.1).

def rangesEqual (a b : E) : Prop := x.block(a) = x.block(b) /\ M.eq(x.byteIndex(a), x.byteIndex(b)) = true /\ M.eq(x.elementSize(a), x.elementSize(b)) = true
def

MemoryModel.rangesOverlapping

07-memory-model.spectec:113

Overlapping memory ranges: the same [[Block]], not equal, a common byte (§29.1).

def rangesOverlapping (a b : E) : Prop := x.block(a) = x.block(b) /\ not rangesEqual(x, a, b) /\ exists k, inRange(x, a, k) /\ inRange(x, b, k)
def

MemoryModel.rangesDisjoint

07-memory-model.spectec:116

Disjoint memory ranges: another [[Block]], or neither equal nor overlapping (§29.1).

def rangesDisjoint (a b : E) : Prop := x.block(a) <> x.block(b) \/ not rangesEqual(x, a, b) /\ not rangesOverlapping(x, a, b)
def

MemoryModel.agentOrder

07-memory-model.spectec:119

a is-agent-order-before b (§29.6.1): before it in some Agent Events Record's [[EventList]].

def agentOrder (a b : E) : Prop := exists r mem x.records, a mem r.events /\ b mem r.events /\ exists (i j : Nat), i < j /\ r.events[i]? = some(a) /\ r.events[j]? = some(b)
def

MemoryModel.Rbf.Valid

07-memory-model.spectec:124

rbf is a reads-bytes-from function of x (§29.6.2): each read event's List has its [[ElementSize]] write events, the ith of its [[Block]] (MemoryRangeSameBlock) with [[ByteIndex]] + i in its memory range, and not the read event itself ("readEvent is not in writes").

def Rbf.Valid (rbf : Rbf(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> M.eq(M.ofNat(rbf(r).length), x.elementSize(r)) = true /\ (forall w mem rbf(r), inSdb(x, w) /\ isWrite(x, w)) /\ (forall (i : Nat) w, rbf(r)[i]? = some(w) -> x.block(w) = x.block(r) /\ inRange(x, w, M.add(x.byteIndex(r), M.ofNat(i)))) /\ r notmem rbf(r)
def

MemoryModel.readsFrom

07-memory-model.spectec:127

r reads-from w (§29.6.3).

def readsFrom (rbf : Rbf(E)) (r w : E) : Prop := inSdb(x, r) /\ inSdb(x, w) /\ w mem rbf(r)
def

MemoryModel.HostSync

07-memory-model.spectec:131

hsw is a host-synchronizes-with of x (§29.6.4): a strict partial order on host-specific events with no cycle in its union with is-agent-order-before.

def HostSync (hsw : Rel(E)) : Prop := StrictPartialOrder(hsw) /\ (forall a b, hsw(a, b) -> inHost(x, a) /\ inHost(x, b)) /\ forall a, not Plus(fun a b => hsw(a, b) \/ agentOrder(x, a, b), a, a)
relation

MemoryModel.SyncWith

07-memory-model.spectec:134

synchronizes-with (§29.6.5, sec-synchronizes-with): the least Relation closed under its three rules.

relation SyncWith (rbf : Rbf(E)) (hsw : Rel(E)) : Rel(E)
rule host {a b : E} : SyncWith(rbf, hsw, a, b) :- hsw(a, b);
relation

MemoryModel.HB

07-memory-model.spectec:141

happens-before (§29.6.6, sec-happens-before): the least Relation closed under its four rules.

relation HB (rbf : Rbf(E)) (hsw : Rel(E)) : Rel(E)
rule sync {a b : E} : HB(rbf, hsw, a, b) :- SyncWith(x, rbf, hsw, a, b);
rule trans {a b c : E} : HB(rbf, hsw, a, b) :- HB(rbf, hsw, a, c), HB(rbf, hsw, c, b);
relation

MemoryModel.Compose

07-memory-model.spectec:150

ComposeWriteEventBytes(execution, loc, ws) returns bs (§29.5.4, sec-composewriteeventbytes).

relation Compose (rbf : Rbf(E)) : M.Math -> List(E) -> List(M.Math) -> Prop
relation

MemoryModel.ValueOf

07-memory-model.spectec:157

ValueOfReadEvent(execution, r) returns v (§29.5.5, sec-valueofreadevent).

relation ValueOf (rbf : Rbf(E)) : E -> List(M.Math) -> Prop
Auxiliary definitions (3): MemoryModel.ValidChosenReads, MemoryModel.CoherentReads, MemoryModel.TearFreeReads
def

MemoryModel.ValidChosenReads

07-memory-model.spectec:164

x has valid chosen reads (§29.7.1): the algorithm returns true. Each read event has one Chosen Value Record, whose [[ChosenValue]] is the value it reads, byte for byte.

def ValidChosenReads (rbf : Rbf(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> (exists (i : Nat) (cv : List(M.Math)), x.chosen[i]? = some((r, cv)) /\ (forall (j : Nat) cv', x.chosen[j]? = some((r, cv')) -> j = i) /\ exists v, ValueOf(x, rbf, r, v) /\ cv.length = v.length /\ forall (k : Nat) a b, cv[k]? = some(a) -> v[k]? = some(b) -> M.eq(a, b) = true)
def

MemoryModel.CoherentReads

07-memory-model.spectec:169

x has coherent reads (§29.7.2): no read event reads a byte from a write it happens-before, or from one that another write of the byte, on its [[Block]] (step 1.c.ii; MemoryRangeSameBlock), happens-between.

def CoherentReads (rbf : Rbf(E)) (hsw : Rel(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> (forall (i : Nat) w, rbf(r)[i]? = some(w) -> not HB(x, rbf, hsw, r, w) /\ not exists v, isWrite(x, v) /\ x.block(v) = x.block(r) /\ inRange(x, v, M.add(x.byteIndex(r), M.ofNat(i))) /\ HB(x, rbf, hsw, w, v) /\ HB(x, rbf, hsw, v, r))
def

MemoryModel.TearFreeReads

07-memory-model.spectec:173

x has tear free reads (§29.7.3): an aligned read event that cannot tear reads from at most one write that cannot tear with its memory range (step 1.a.ii.1); its Assert holds.

def TearFreeReads (rbf : Rbf(E)) : Prop := forall r, inSdb(x, r) -> isRead(x, r) -> x.noTear(r) = true -> M.isInt(M.div(x.byteIndex(r), x.elementSize(r))) = true /\ forall w, readsFrom(x, rbf, r, w) -> x.noTear(w) = true -> not (rangesEqual(x, r, w) /\ exists v, rangesEqual(x, v, w) /\ x.noTear(v) = true /\ w <> v /\ readsFrom(x, rbf, r, v))
laws

MemoryModel.MemoryOrder

07-memory-model.spectec:176

mo is an is-memory-order-before of x (§29.7.4, sec-memory-order).

Proved preserved across shared reads as part of Ecma.MemoryModel.snoc_valid.

laws MemoryOrder (rbf : Rbf(E)) (hsw : Rel(E)) (mo : Rel(E))

6 laws

For a candidate execution execution, is-memory-order-before is a strict total order of all Memory events in EventSet(execution) that satisfies the following.

Any two distinct events in EventSet(execution) (inEvents) are ordered by mo.

total
SpecTec
law total : forall a b, inEvents(x, a) -> inEvents(x, b) -> a <> b -> mo(a, b) \/ mo(b, a);

For events eventA and eventB, eventA is-memory-order-before eventB in execution if eventA happens-before eventB in execution.

mo extends happens-before (HB).

hb
SpecTec
law hb : forall a b, HB(x, rbf, hsw, a, b) -> mo(a, b);

For events readEvent and writeEvent such that readEvent reads-from writeEvent in execution, there is no WriteSharedMemory or ReadModifyWriteSharedMemory event value in SharedDataBlockEventSet(execution) such that value.[[Order]] is seq-cst, writeEvent is-memory-order-before value in execution, value is-memory-order-before readEvent in execution, and any of the following conditions are true.

No seq-cst write with equal memory range comes in mo between a read and the write it reads from under any of the three conditions.

sc
SpecTec
law sc : forall r w, readsFrom(x, rbf, r, w) -> not exists v, inSdb(x, v) /\ isWrite(x, v) /\ x.order(v) = .seqCst /\ mo(w, v) /\ mo(v, r) /\ (SyncWith(x, rbf, hsw, w, r) /\ rangesEqual(x, v, r) \/ HB(x, rbf, hsw, w, r) /\ HB(x, rbf, hsw, v, r) /\ x.order(w) = .seqCst /\ rangesEqual(x, w, v) \/ HB(x, rbf, hsw, w, r) /\ HB(x, rbf, hsw, w, v) /\ x.order(r) = .seqCst /\ rangesEqual(x, v, r));

For each WriteSharedMemory or ReadModifyWriteSharedMemory event writeEvent in SharedDataBlockEventSet(execution), if writeEvent.[[Order]] is seq-cst, then it is not the case that there is an infinite number of ReadSharedMemory or ReadModifyWriteSharedMemory events in SharedDataBlockEventSet(execution) with equal memory range that is memory-order before writeEvent.

Every seq-cst write is after only finitely many reads with equal memory range in mo.

finite
SpecTec
law finite : forall w, inSdb(x, w) -> isWrite(x, w) -> x.order(w) = .seqCst -> (exists (l : List(E)), forall r, inSdb(x, r) -> isRead(x, r) -> rangesEqual(x, r, w) -> mo(r, w) -> r mem l);
Auxiliary definition: MemoryModel.SeqCstAtomics
def

MemoryModel.SeqCstAtomics

07-memory-model.spectec:205

x has sequentially consistent atomics (§29.7.4, sec-memory-order): it admits an is-memory-order-before.

def SeqCstAtomics (rbf : Rbf(E)) (hsw : Rel(E)) : Prop := exists mo, MemoryOrder(x, rbf, hsw, mo)
laws

MemoryModel.ValidWith

07-memory-model.spectec:209

x is a valid execution (§29.7.5, sec-valid-executions) with the host's host-synchronizes-with hsw and the reads-bytes-from function rbf.

Proved preserved across shared reads by Ecma.MemoryModel.snoc_valid, which discharges Host.MemoryLaws.chooseReadsValid.

laws ValidWith (rbf : Rbf(E)) (hsw : Rel(E))

7 laws

29.7.5 Valid Executions sec-valid-executions

The host provides a host-synchronizes-with Relation for execution.

29.6.2 reads-bytes-from sec-reads-bytes-from

For a candidate execution execution, its reads-bytes-from function is a mathematical function mapping Memory events in SharedDataBlockEventSet(execution) to Lists of events in SharedDataBlockEventSet(execution) that satisfies the following conditions.

29.7.5 Valid Executions sec-valid-executions

execution admits a happens-before Relation that is a strict partial order.

29.7.5 Valid Executions sec-valid-executions

execution has valid chosen reads.

29.7.5 Valid Executions sec-valid-executions

execution has coherent reads.

29.7.5 Valid Executions sec-valid-executions

execution has tear free reads.

29.7.5 Valid Executions sec-valid-executions

execution has sequentially consistent atomics.

Auxiliary definitions (4): MemoryModel.Valid, MemoryModel.Race, MemoryModel.DataRace, MemoryModel.DataRaceFree
def

MemoryModel.Valid

07-memory-model.spectec:242

x is a valid execution (§29.7.5) with the host's host-synchronizes-with hsw: it admits a reads-bytes-from function with which it is one.

def Valid (hsw : Rel(E)) : Prop := exists rbf, ValidWith(x, rbf, hsw)
def

MemoryModel.Race

07-memory-model.spectec:246

a and b are in a race (§29.8; InRace): distinct, not both happens-before each other (RacesNotBoth, literal), and two writes whose ranges are not disjoint or one reading from the other.

def Race (rbf : Rbf(E)) (hsw : Rel(E)) (a b : E) : Prop := a <> b /\ not (HB(x, rbf, hsw, a, b) /\ HB(x, rbf, hsw, b, a)) /\ (isWrite(x, a) /\ isWrite(x, b) /\ not rangesDisjoint(x, a, b) \/ readsFrom(x, rbf, a, b) \/ readsFrom(x, rbf, b, a))
def

MemoryModel.DataRace

07-memory-model.spectec:249

a and b are in a data race (§29.9): in a race, and not both seq-cst or overlapping.

def DataRace (rbf : Rbf(E)) (hsw : Rel(E)) (a b : E) : Prop := Race(x, rbf, hsw, a, b) /\ (x.order(a) <> .seqCst \/ x.order(b) <> .seqCst \/ rangesOverlapping(x, a, b))
def

MemoryModel.DataRaceFree

07-memory-model.spectec:252

x is data race free (§29.10): no two of its Shared Data Block events are in a data race.

def DataRaceFree (rbf : Rbf(E)) (hsw : Rel(E)) : Prop := forall a b, inSdb(x, a) -> inSdb(x, b) -> not DataRace(x, rbf, hsw, a, b)

Adding a shared read to a candidate execution

From spec/spectec/prose/08-memory-read.spectec

A shared read appends its event to an Agent Events Record's [[EventList]] and its Chosen Value Record to [[ChosenValues]] (GetRawBytesFromSharedBlock, GetModifySetValueInBuffer, AtomicCompareExchangeInSharedBlock, CopyDataBlockBytes): Exec.snoc, the new event none and the others some. Over it: the claim of law L2 (ReadsValid).

syntax record

MemoryModel.NewEvent

08-memory-read.spectec:16

The new event of a shared read (§29.1, sec-memory-model-fundamentals; §25.1.3.15, sec-getrawbytesfromsharedblock; §25.1.3.19, sec-getmodifysetvalueinbuffer): its kind and fields.

syntax NewEvent (M : Model) record
laws

MemoryModel.NewEvent.IsRead

08-memory-read.spectec:30

A read event of a shared read (§29.1, sec-memory-model-fundamentals; §29.7.3, sec-tear-free-aligned-reads): a ReadSharedMemory or ReadModifyWriteSharedMemory event, not init, its [[ByteIndex]] a non-negative integer, aligned where it cannot tear.

Proved of each shared-read event in Engine.Proofs.Host.MemoryImpl.

laws NewEvent.IsRead (r : NewEvent(M))

4 laws

29.1 Memory Model Fundamentals sec-memory-model-fundamentals

A read event is either a ReadSharedMemory or a ReadModifyWriteSharedMemory.

The new event's kind is .read or .rmw.

kind
SpecTec
law kind : r.kind = .read \/ r.kind = .rmw;
29.1 Memory Model Fundamentals sec-memory-model-fundamentals

[[Order]] seq-cst or unordered

A read event's [[Order]] is not init.

order
SpecTec
law order : r.order <> .init;
29.1 Memory Model Fundamentals sec-memory-model-fundamentals

[[ByteIndex]] a non-negative integer

The event's [[ByteIndex]] is a non-negative integer.

byteIndex
SpecTec
law byteIndex : exists n, r.byteIndex = M.ofNat(n);
29.7.3 Tear Free Reads step 1.a.i sec-tear-free-aligned-reads

Assert: The remainder of dividing readEvent.[[ByteIndex]] by readEvent.[[ElementSize]] is 0.

When [[NoTear]] is true, [[ByteIndex]] is an integer multiple of [[ElementSize]].

aligned
SpecTec
law aligned : r.noTear = true -> M.isInt(M.div(r.byteIndex, r.elementSize)) = true;
Auxiliary definitions (5): MemoryModel.EventsRecord.snoc, MemoryModel.Exec.snoc, MemoryModel.Rbf.snoc, MemoryModel.Rel.lift, …
def

MemoryModel.EventsRecord.snoc

08-memory-read.spectec:50

The ith Agent Events Record er, the new event none appended where i is k.

def EventsRecord.snoc (k i : Nat) (er : EventsRecord(E)) : EventsRecord(Option(E)) := ctor(er.events.map(some) ++ (if i = k then [none] else []), er.syncs.map(fun p => (some(p.1), some(p.2))))
def

MemoryModel.Exec.snoc

08-memory-read.spectec:54

x with the event r (none) appended to the [[EventList]] of its kth Agent Events Record, and the Chosen Value Record of r and cv to its [[ChosenValues]].

SpecTec, 36 lines
def Exec.snoc (x : Exec(M, E)) (k : Nat) (r : NewEvent(M)) (cv : List(M.Math)) : Exec(M, Option(E)) {  records := x.records.mapIdx(EventsRecord.snoc(k));  chosen := x.chosen.map(fun c => (some(c.1), c.2)) ++ [(none, cv)];  kind    | some(e) => x.kind(e)    | none => r.kind  end;  order    | some(e) => x.order(e)    | none => r.order  end;  noTear    | some(e) => x.noTear(e)    | none => r.noTear  end;  block    | some(e) => x.block(e)    | none => r.block  end;  byteIndex    | some(e) => x.byteIndex(e)    | none => r.byteIndex  end;  elementSize    | some(e) => x.elementSize(e)    | none => r.elementSize  end;  payload    | some(e) => x.payload(e)    | none => r.payload  end;  modify    | some(e) => x.modify(e)    | none => r.modify  end;}
def

MemoryModel.Rbf.snoc

08-memory-read.spectec:92

rbf, the new event reading from ws.

def Rbf.snoc (rbf : Rbf(E)) (ws : List(E)) : Rbf(Option(E))  | some(e) => rbf(e).map(some)  | none => ws.map(some)end
def

MemoryModel.Rel.lift

08-memory-read.spectec:98

A Relation on the events but the new one.

def Rel.lift (r : Rel(E)) : Rel(Option(E))  | some(a), some(b) => r(a, b)  | _, _ => Falseend
def

MemoryModel.ReadsValid

08-memory-read.spectec:106

Law L2's claim: with the read r appended to the kth Agent Events Record, reading its bytes from ws, its Chosen Value Record the byte values ComposeWriteEventBytes composes of them, x is a valid execution with rbf and hsw.

def ReadsValid (x : Exec(M, E)) (rbf : Rbf(E)) (hsw : Rel(E)) (k : Nat) (r : NewEvent(M)) (ws : List(E)) : Prop := forall cv, (forall c mem cv, exists n, c = M.ofNat(n)) -> Compose(x, rbf, r.byteIndex, ws, cv) -> ValidWith(x.snoc(k, r, cv), rbf.snoc(ws), Rel.lift(hsw))

Reading a candidate execution from the store

From spec/spectec/prose/09-memory-view.spectec

A candidate execution Record of the store, as the memory model sees it: its Agent Events Records and Chosen Value Records, read through its Lists, and each event by its address, with the fields of its Record. An event's kind is its Record's sort; [[ModifyOp]] is called by d, which runs an Abstract Closure. Fields, enums and sorts are known only through a Names.

syntax record

MemoryModel.View.Names

09-memory-view.spectec:19

The name lookups of the view (§29.1, sec-memory-model-fundamentals; §29.2, sec-agent-event-records; §29.3, sec-chosen-value-records; §29.4, sec-candidate-executions).

syntax Names record

A field by its name (the first of that name).

field? : String -> Option(FieldId);

An enum by its name.

enum? : String -> Option(EnumId);

A sort by its name.

sort? : String -> Option(SortId);
Auxiliary definitions (15): MemoryModel.View.toList, MemoryModel.View.cell, MemoryModel.View.list, MemoryModel.View.field, …
def

MemoryModel.View.toList

09-memory-view.spectec:31

A sequence's elements, first to last.

def toList {α : Type} (xs : M.Seq(α)) : List(α) := M.seqFoldl(fun acc x => acc ++ [x], [], xs)
def

MemoryModel.View.cell

09-memory-view.spectec:34

The store cell a value refers to.

def cell (s : State(M)) : Val(M) -> Option(Cell(M))  | .ref(a) => M.heapGet(s.heap, a)  | _ => noneend
def

MemoryModel.View.list

09-memory-view.spectec:40

The elements of the List a value refers to; none where it refers to no List.

def list (s : State(M)) (v : Val(M)) : List(Val(M)) := match cell(s, v) with  | some(.list(xs, _)) => toList(xs)  | _ => []end
def

MemoryModel.View.field

09-memory-view.spectec:47

A Record's field by name; absent where there is no such Record or field. As Strategy's fieldOf, and unlike readField, it reads no .env cell's fields.

def field (s : State(M)) (v : Val(M)) (f : String) : Val(M) := match cell(s, v), N.field?(f) with  | some(.record(_, fs)), some(i) => M.recGet(fs, i).getD(.absent)  | _, _ => .absentend
def

MemoryModel.View.addr

09-memory-view.spectec:53

The address a value refers to.

def addr : Val(M) -> Option(M.Addr)  | .ref(a) => some(a)  | _ => noneend
def

MemoryModel.View.math

09-memory-view.spectec:59

A mathematical value, 0 where the value is none.

def math : Val(M) -> M.Math  | .math(m) => m  | _ => M.ofNat(0)end
def

MemoryModel.View.bytes

09-memory-view.spectec:65

The byte values of a List of byte values.

def bytes (s : State(M)) (v : Val(M)) : List(M.Math) := list(s, v).filterMap(fun  | .math(m) => some(m)  | _ => noneend)
def

MemoryModel.View.pair

09-memory-view.spectec:71

An « a, b » pair of events.

def pair (s : State(M)) (v : Val(M)) : Option(M.Addr times M.Addr) := match list(s, v) with  | [.ref(a), .ref(b)] => some((a, b))  | _ => noneend
def

MemoryModel.View.eventsRecord

09-memory-view.spectec:77

An Agent Events Record: its [[EventList]]'s events and [[AgentSynchronizesWith]]'s pairs.

def eventsRecord (s : State(M)) (er : Val(M)) : EventsRecord(M.Addr) := ctor(list(s, field(N, s, er, "EventList")).filterMap(addr), list(s, field(N, s, er, "AgentSynchronizesWith")).filterMap(pair(s)))
def

MemoryModel.View.chosenValue

09-memory-view.spectec:80

A Chosen Value Record: its [[Event]] and [[ChosenValue]].

def chosenValue (s : State(M)) (cv : Val(M)) : Option(M.Addr times List(M.Math)) := addr(field(N, s, cv, "Event")).map(fun e => (e, bytes(s, field(N, s, cv, "ChosenValue"))))
def

MemoryModel.View.kind

09-memory-view.spectec:83

An event's kind: its Record's sort.

def kind (s : State(M)) (e : M.Addr) : Kind := match M.heapGet(s.heap, e) with  | some(.record(k, _)) => if N.sort?("ReadSharedMemory") == some(k) then .read else if N.sort?("WriteSharedMemory") == some(k) then .write else if N.sort?("ReadModifyWriteSharedMemory") == some(k) then .rmw else if N.sort?("Synchronize event") == some(k) then .sync else .host  | _ => .hostend
def

MemoryModel.View.order

09-memory-view.spectec:89

An event's [[Order]]: unordered where it is neither seq-cst nor init.

def order (s : State(M)) (e : M.Addr) : Order := match field(N, s, .ref(e), "Order") with  | .enum(o) => if N.enum?("seq-cst") == some(o) then .seqCst else if N.enum?("init") == some(o) then .init else .unordered  | _ => .unorderedend
def

MemoryModel.View.modify

09-memory-view.spectec:95

[[ModifyOp]] of an event called on a List of byte values and a [[Payload]], in the state.

def modify (d : CloSite -> List(Val(M)) -> List(Val(M)) -> Run(M, Val(M))) (s : State(M)) (e : M.Addr) (bs pay : List(M.Math)) : Option(List(M.Math)) := match field(N, s, .ref(e), "ModifyOp") with  | .clo(site, caps) => let r : Run(M, Val(M)) := do {      let b <- listOf(bs.map(.math));      let p <- listOf(pay.map(.math));      d(site, caps, [b, p]);    } in match r(s) with      | .ok(.comp(_, v, _), s') | .ok(v, s') => some(bytes(s', v))      | _ => none    end  | _ => noneend
def

MemoryModel.View.exec

09-memory-view.spectec:109

The candidate execution Record ex of state s as the memory model sees it; an event with no [[Block]] has its own address, no Shared Data Block's.

SpecTec, 15 lines
def exec (d : CloSite -> List(Val(M)) -> List(Val(M)) -> Run(M, Val(M))) (s : State(M)) (ex : Val(M)) : Exec(M, M.Addr) {  records := list(s, field(N, s, ex, "EventsRecords")).map(eventsRecord(N, s));  chosen := list(s, field(N, s, ex, "ChosenValues")).filterMap(chosenValue(N, s));  kind := kind(N, s);  order := order(N, s);  noTear e := match field(N, s, .ref(e), "NoTear") with    | .bool(b) => b    | _ => false  end;  block e := addr(field(N, s, .ref(e), "Block")).getD(e);  byteIndex e := math(field(N, s, .ref(e), "ByteIndex"));  elementSize e := math(field(N, s, .ref(e), "ElementSize"));  payload e := bytes(s, field(N, s, .ref(e), "Payload"));  modify := modify(N, d, s);}
def

MemoryModel.View.Formed

09-memory-view.spectec:127

The Shared Data Block events of ex are formed: [[Block]] a reference, [[ByteIndex]] and [[ElementSize]] non-negative integers.

def Formed (d : CloSite -> List(Val(M)) -> List(Val(M)) -> Run(M, Val(M))) (s : State(M)) (ex : Val(M)) : Prop := forall e, MemoryModel.inSdb(exec(N, d, s, ex), e) -> (exists b, field(N, s, .ref(e), "Block") = .ref(b)) /\ (exists n, field(N, s, .ref(e), "ByteIndex") = .math(M.ofNat(n))) /\ exists n, field(N, s, .ref(e), "ElementSize") = .math(M.ofNat(n))