Skip to content

[General]: The manual forbids slice-by-slice initialization that the style guide recommends #103

Open

Description

@shuklaayush

Problem

The language manual requires every variable to be initialized before it is read
(ISA_LRM, "All variables must be initialized before use"), and it describes a
slice assignment as a read of the whole variable (ISA_LRM, "Assignments"):

RIP[0] := 0b0;   Set the bottom bit of a variable RIP to zero. Note that this reads
                 the current value of RIP, clears the bottom bit, and writes the value
                 back to RIP.

Under these two rules, assigning a slice of a variable that has not been initialized is
an error.

The published style guide says the opposite. It defines the error per bit
("It is an .isa language error to read a variable (or field or array element or
bitslice) if it has not been initialized"), recommends against "a spurious
initializer", and gives this as the preferred style:

var x : Bits(32);
if ... then
    x := Foo32(32);
else
    x[31:16] := Bar16();
    x[15:0] := Zeros();
end

The sources follow the style guide:

  • AESDEC.xml lines 193–195: var result : Bits(register_size); then
    result[i *: 128] := AESDEC_128(…) in a loop.
  • ENQCMD.xml lines 94–95: var src : Bits(512); then
    src[0 *: 128] := Logical_Mem_Read?(…).
  • Mem_Fetch: var value : Bits(size); then value[i *: 8] := Physical_Mem_Fetch(physical);
    in a loop.

Proposed fix

In the manual, define initialization per bit, as the style guide does. For example, add
to "All variables must be initialized before use":

Assigning a slice of a local bitvector variable writes and initializes the selected bits
without reading the remaining bits. The remaining bits retain their values and
initialization state. Reading a bit requires that bit to have been initialized.

and qualify the RIP[0] description to apply to an initialized variable.

AI disclosure

Assisted-by: Claude

Claude assisted with source analysis, the proposed correction and drafting this report.
Reviewed with assistance from Codex.

Pinned source: definitions.xml.gz at revision 35eface1b7ee003a1cda95005ec5fd83de658a64.

Activity

  1. alastairreid commented on Dec 4, 2026

    @alastairreid
    Contributor

    Thanks for the report.
    This is a subtle issue that is hard to define in a way that is both precise and useful to readers and writers of specs.
    And this is not helped by the fact that our usual testing framework does not try to detect violations.

    What we are aiming for is a clear distinction between UNSPECIFIED and uninitialized

    • Using an UNSPECIFIED value is not an error.
      • Furthermore, the normal algebraic properties apply: if RFLAGS.CF is UNSPECIFIED, then RFLAGS.CF xor RFLAGS.CF == 0
    • Using an uninitialized field of a variable is an error in the specification that needs to be fixed.

    For UNSPECIFIED, the semantics is that the expression UNSPECIFIED : T evaluates to a value of the type T.
    This could be random or deterministic or under control of the surrounding test/execution/proof framework.
    It could yield a different value each time you execute an instruction.
    You cannot assume that UNSPECIFIED : T == UNSPECIFIED : T
    As a user of a piece of hardware, you should not make any assumptions.

    But, in practice, different uses may want to use a different implementation.
    For example, our usual testing framework provides control over the value chosen (because determinism makes regression testing and debugging easier) but it also records which locations have been assigned an UNSPECIFIED value so that, after execution of each instruction we can

    • Compare all locations that are not unspecified against a reference model
    • Copy the state of the reference model into the state of the spec so that we are back in sync.

    In another tool, you might want to treat use of an UNSPECIFIED value like poison - you can't continue reasoning if you use it.
    Or you might use a three-valued logic.
    Both of these are approximations to the semantics but they may be useful approximations depending on your goal.


    For uninitialized values, the semantics is less clear because

    1. Uninitialized is one of those things that is hard to define but obvious when you see it
    2. We have not implemented any runtime checks to really test whether the semantics we write is practical to use

    I suspect that the right model is

    • "uninitialized" is a property of an (atomic) location.
      (Which is different from it being a property of a value or an expression)
    • The granularity of those locations should be the smallest granularity at which the value can be accessed
      • For Bits(size), each individual bit should be a location
      • Arrays and records are made up of multiple locations. eg 'record x: Bits(4), y : Bits(4) end' has 8 atomic locations
      • For Integer, Enums, Bools, the entire value is a location
    • Although we often implement a bitvector assignment b[0] := v; by reading all of b, masking out the bottom bit, and or-ing v with b, from an initialization point of view, we should think of it as "bit 0 of b is now initialized" even though an implementation may have read all of b (which might have been uninitialized) and written all of b.
    • Reading an uninitialized location is an error.

    But there are one place where this semantics does not quite work: .isa allows you to define abstractions like the AL reader and assignment functions that implement the aliasing of AL with RAX (which is, itself, an alias of GPR[0]).

    function AL -> Bits(8)
    begin
        return RAX[7:0];
    end
    
    function AL := val : Bits(8)
    begin
        RAX[7:0] := val;
    end
    

    This is especially useful for registers that have fields that cannot be changed by assignment or that contain counters/timers/...

    But, it breaks the simple location-based semantics that I was sketching above because AL[0] := v; would read RAX[7:0] as well which would be an error if any of those 8 bits were uninitialized.

    The most obvious way to fix this problem is to change the model so that being initialized is a property of a value.
    Under that model, if RAX is completely uninitialized, then executing AL[0] := v; would

    • Call the AL reader function
    • Read RAX - returning 64 uninitialized bits (in a higher quality implementation, maybe each uninitialized bit records the source location of the read?)
    • Slice [7:0] - returning 8 uninitialized bits
    • return 8 uninitialized bits from the AL function
    • Replace the bottom bit with 'v' resulting in 7 uninitialized bits and v
    • Call the AL writer function
    • Read RAX - returning 64 uninitialized bits
    • Replace the bottom 8 bits with the (7 uninitialized bits and v) - resulting in 63 uninitialized bits and v
    • Write the 63 uninitialized bits and v to RAX.

    I think that this works but, once we have made initialization a property of values, we have to work through all of the other operations on values and decide how they handle partially uninitialized values.
    Cases involving the standard library functions are, I think, easy

    • Is 'x or y', 'x + y', 'x == y', ... an error if
      • Any bit of x or y is uninitialized?
      • or if the result would be changed depending on what value we assign to the bit?
    • Is 'Std::Integer::Unsigned(x)' an error if any bit of x is uninitialized?
    • Is a conditional expression like "if c then x else y" or "x and then y" (equivalent to C's 'x && y'), is it always an error if y is uninitialized?
      In all cases except the last, the answer should probably be 'it is an error'.
      In the last case, I am tempted to say that the answer should be 'it is not an error'

    But for a call to a user-defined function "F", is it ok if any of the arguments is uninitialized?

    • If we say "it is ok", then we seem to have some form of lazy evaluation - which is an odd property in a strict language.
    • If we say "it is an error", then the example above with AL does not work.
      So it seems that we are forced to say that arguments to user-defined functions can be uninitialized
      (and, also that values returned from user-defined functions can be uninitialized).

    Incidentally, I believe that the SAIL and ASL languages avoid this issue as follows

    • In SAIL, you must explicitly provide an initializer when you declare a variable.
      What I like about this is that you never have to think about uninitialized values.
      What I don't like about this is that, in principle, it makes it harder to detect that some branch of an if-statement (say) failed to write a value to a variable.
      But, until I actually implement a mechanism to detect uninitialized values, this 'disadvantage' is not really relevant :-)

    • In ASL, the language implicitly initializes all variables to zero.
      I like this less than the SAIL solution because there is no clue for the reader about what the initial value is: they need to have read the relevant section of the ASL language reference manual to know that it will definitely be zero.


    Conclusion: I am still not clear how best to define the semantics of initialization.
    The best approximation that I have come up with so far is to treat being initialized/uninitialized as a property of values and that builtin functions treat uninitialized bits as poison.
    But, without implementing this semantics and testing it at scale, it is hard to be certain that this works in practice.
    And also to be certain that human readers of the specification are not surprised by the behavior.

    I will leave this issue open for now as a reminder that we need to improve this situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions