Skip to content

Make linked variables non-volatile by default #327

@cardillan

Description

@cardillan

Named links in processors (such as switch1) aren't constant - their value may change (e.g., when the block gets unlinked/linked again or destroyed and rebuilt). They may change from null to non-null and back, or from one instance to another.

Due to this behavior, linked variables are created volatile (they technically are - their values may change by an external process). That precludes some optimizations from taking place, which is a greater concern now that arrays of linked blocks are available due to #324. For example, the following code

linked reactors[] = (reactor1 .. reactor2);

for reactor in reactors do
    reactor.enabled = reactor.@cryofluid > 3;
end;

now produces:

set :reactor reactor1
sensor *tmp3 :reactor @cryofluid
op greaterThan *tmp4 *tmp3 3
control enabled :reactor *tmp4 0 0 0
set :reactor reactor2
sensor *tmp3 :reactor @cryofluid
op greaterThan *tmp4 *tmp3 3
control enabled :reactor *tmp4 0 0 0

The loop got unrolled, but because the links are volatile, they are still copied to a temporary variable so that both operations in the loop are guaranteed to work on the same instance.

For linked variables, this seems to be a bit excessive. I'm pretty sure most mlog authors never even realize these variables are actually volatile and do not take special precaution handling them.

The linked variables will therefore be made non-volatile by default. In rare cases where the logic needs to react to a possible change to the linked block, it will be possible to declare a linked variable volatile to provide the volatility protection.

Note that the sensor instruction is considered deterministic for @size, @speed, @type and @id. However, if the object is a linked variable, it won't be considered deterministic even for those properties, because all of them could change (e.g., by building a logic processor over a micro processor).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions