Skip to content

feat: sanity check lookup bitwidths #972

Description

@DavePearce

Overview

Consider the following example:

(defcolumns (X :i16) (Y :i16) (Z :i16))

(deflookup test (Z) (- X Y))

From a field agnostic perspective, this example is problematic. Specifically, we cannot determine a suitable bitwidth for the source column because it can be negative. In order to implement lookups correctly, we must know the bitwidth of both the target and source column for each pair (see examples below for more details why).

Therefore the purpose of this issue is to report an error when the bitwidth of a source or target column in a lookup cannot be determined.

Approach

Machinery for determining the necessary bitwidth of an MIR expression already exists within go-corset. More specifically, the ValueRange() function is implemented by all IR expressions and is part of the ir.Term[T] interface:

	// ValueRange returns the interval of values that this term can evaluate to.
	// For terms accessing registers, this is determined by the declared width of
	// the register.
	ValueRange(module schema.Module) *math.Interval

Using this function, it is possible to determine the interval of values in which any evaluation of the given term lies. In particular, using this we can easily tell when a negative value is possible.

Therefore the suggested approach is to use this method in corset/compiler/translator.go to look for negative values and, if detected, to report a suitable SyntaxError.

Notes

  • (Conditional Lookups) For conditional lookups, we additionally expect that the selector expression is within the range 0..1. Hence, a similar check for this would make sense.
  • (Range Constraints) A similar check can also be made for definrange constraints, and perhaps even defcomputed.

Metadata

Metadata

Assignees

Labels

field agnosticityrelated to becoming field agnostic

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions