Module LibASL.TcheckSource

Type inference and checker for ASL language

Sourcemodule PE = PPrintEngine
Sourcemodule AST = Asl_ast
Sourcemodule Visitor = Asl_visitor
Sourceval verbose : bool

Exceptions thrown by typechecker

Sourceexception UnknownObject of AST.l * string * string
Sourceexception DoesNotMatch of AST.l * string * string * string
Sourceexception IsNotA of AST.l * string * string
Sourceexception Ambiguous of AST.l * string * string
Sourceexception TypeError of AST.l * string
Sourceexception InternalError of string

AST construction utilities

Sourceval type_unit : AST.ty
Sourceval type_integer : AST.ty
Sourceval type_bool : AST.ty
Sourceval type_real : AST.ty
Sourceval type_string : AST.ty
Sourceval type_bits : AST.expr -> AST.ty
Sourceval type_exn : AST.ty

Construct expression "eq_int(x, y)"

Construct expression "add_int(x, y)"

Construct expression "sub_int(x, y)"

Construct expression "(0 + x1) + ... + xn"

Prettyprinting support

Table of binary operators used for resugaring expressions when printing error messages.

Sourceval add_binop : AST.binop -> AST.ident -> unit
Sourceval ppp_expr : AST.expr -> string

Very pretty print expression (resugaring expressions)

Sourceval ppp_type : LibASL.Asl_utils.AST.ty -> string

Very pretty print type (resugaring expressions)

Environment representing global and local objects

Sourcetype typedef =
  1. | Type_Builtin of AST.ident
  2. | Type_Forward
  3. | Type_Record of (AST.ty * AST.ident) list
  4. | Type_Enumeration of AST.ident list
  5. | Type_Abbreviation of AST.ty
Sourceval pp_typedef : typedef -> string
Sourceval pp_funtype : funtype -> PE.document
Sourceval pp_sfuntype : sfuntype -> PE.document
Sourceval funtype_of_sfuntype : sfuntype -> funtype
Sourcemodule Operator1 : sig ... end
Sourcemodule Operators1 : sig ... end
Sourcemodule Operator2 : sig ... end
Sourcemodule Operators2 : sig ... end

Global Environment (aka the Global Symbol Table)

Sourcemodule GlobalEnv : sig ... end
Sourceval isConstant : GlobalEnv.t -> LibASL.Asl_utils.AST.ident -> bool

dereference typedef

compare index types

structural match on two types - ignoring the dependent type part

Field typechecking support

Sourcetype fieldtypes =
  1. | FT_Record of (AST.ty * AST.ident) list
  2. | FT_Register of (LibASL.Asl_utils.AST.slice list * AST.ident) list

Field accesses can be either record fields or fields of registers

This type captures the information needed to typecheck either of these

  • a list of fieldname/type pairs for records
  • a list of fieldname/slice pairs for registers

Get fieldtype information for a record/register type

Get fieldtype information for a named field of a record

Get fieldtype information for a named field of a slice

Get named field of a register and calculate type

Get named fields of a register and calculate type of concatenating them

Environment (aka the Local+Global Symbol Table)

Sourcemodule Env : sig ... end

Unification

Expression simplification

Perform simple constant folding of expression

It's primary role is to enable the 'DIV' hacks in z3_of_expr which rely on shallow syntactic transforms. It has a secondary benefit of sometimes causing constraints to become so trivial that we don't even need to invoke Z3 which gives a performance benefit.

Perform simple constant folding of expressions within a type

Z3 support code

Convert ASL expression to Z3 expression. This only copes with a limited set of operations: ==, +, -, * and DIV. (It is possible that we will need to extend this list in the future but it is sufficient for the current ASL specifications.)

The support for DIV is not sound - it is a hack needed to cope with the way ASL code is written and generally needs a side condition that the division is exact (no remainder).

ufs is a mutable list of conversions used to handle subexpressions that cannot be translated. We treat such subexpressions as uninterpreted functions and add them to the 'ufs' list so that we can reason that "F(x) == F(x)" without knowing "F".

Sourceval z3_of_expr : Z3.context -> (LibASL.Asl_utils.AST.expr * Z3.Expr.expr) list ref -> LibASL.Asl_utils.AST.expr -> Z3.Expr.expr
Sourceval check_constraints : AST.expr list -> AST.expr list -> bool

check that bs => cs

Unification support code

Sourceclass unifier : LibASL.Asl_utils.AST.l -> AST.expr list -> object ... end

Unifier

Create a fresh unifier, invoke a function that uses the unifier and check that the constraints are satisfied. Returns the synthesized bindings and result of function

Type Unification

Notes on how type inference works:

Note that there is a choice of what type arguments to add to a function

bits(N) ZeroExtend(bits(M) x, integer N)

We can either:

Unify two index types

Unify two types

This performs a structural match on two types - ignoring the dependent type part

Apply substitutions to an expression

Apply substitutions to an L-expression

Apply substitutions to a type

Sourceval mkfresh_funtype : unifier -> funtype -> funtype

Replace all type variables in function type with fresh variables

Sourceval mkfresh_sfuntype : unifier -> sfuntype -> sfuntype

Replace all type variables in setter function type with fresh variables

Check that ty2 is a subtype of ty1: ty1 >= ty2

Check that ty1 is identical to ty2

Disambiguation of functions and operators

Sourceval reportChoices : LibASL.Asl_utils.AST.l -> string -> string -> LibASL.Asl_utils.AST.ty list -> funtype list -> unit

Generate error message when function disambiguation fails

Sourceval isCompatibleFunction : GlobalEnv.t -> bool -> LibASL.Asl_utils.AST.ty list -> funtype -> bool

Check whether a list of function argument types is compatible with the type of a function.

One function type is compatible with another if they have the same number of arguments and each argument has the same base type

Sourceval chooseFunction : GlobalEnv.t -> LibASL.Asl_utils.AST.l -> string -> string -> bool -> LibASL.Asl_utils.AST.ty list -> funtype list -> funtype option

Disambiguate a function name based on the number and type of arguments

Sourceval isCompatibleSetterFunction : GlobalEnv.t -> LibASL.Asl_utils.AST.ty list -> sfuntype -> bool

Check whether a list of function argument types is compatible with the type of a setter function.

One function type is compatible with another if they have the same number of arguments and each argument has the same base type

Sourceval chooseSetterFunction : GlobalEnv.t -> LibASL.Asl_utils.AST.l -> string -> AST.ident -> LibASL.Asl_utils.AST.ty list -> sfuntype list -> sfuntype option

Disambiguate a setter function name based on the number and type of arguments

Instantiate type of function using unifier 'u'

Instantiate type of setter function using unifier 'u'

Disambiguate and typecheck application of a function to a list of arguments

Disambiguate and typecheck application of a unary operator to argument

Disambiguate and typecheck application of a binary operator to arguments

Typecheck expressions

Lookup a variable in environment

Typecheck list of expressions

Typecheck expression and check that it is a subtype of ty

Typecheck 'if c then expr'

Typecheck bitslice indices

Typecheck pattern against type ty

Typecheck bitslice syntax This primarily consists of disambiguating between array indexing and bitslicing Note that this function is almost identical to tc_slice_lexpr

Typecheck expression

Typecheck list of types

Typecheck type

Typecheck L-expressions

Typecheck bitslice syntax

This primarily consists of disambiguating between array indexing and bitslicing Note that this function is almost identical to tc_slice_expr

Typecheck left hand side of expression in context where type of right hand side is not yet known

Typecheck statements

Typecheck left hand side of expression and check that rhs type 'ty' is compatible. Return set of variables assigned to in this expression

Typecheck list of statements

Typecheck 'if expr then stmt'

Typecheck case alternative

Typecheck exception catcher 'when expr stmt'

Typecheck statement

Typecheck function definition

Typecheck function body (list of statements)

Typecheck function argument

Typecheck list of function arguments

Typecheck setter procedure argument

Sourceval tc_sformals : Env.t -> LibASL.Asl_utils.AST.l -> AST.sformal list -> AST.sformal list

Typecheck list of setter procedure arguments

Add function definition to environment

Typecheck instruction

Typecheck instruction encoding

Typecheck bitslice of instruction opcode

Sourceval check_width : LibASL.Asl_utils.AST.l -> int -> int -> unit

Typecheck instruction decode pattern match

Typecheck instruction decode body

Typecheck instruction decode case alternative

Typecheck instruction decode case

Typecheck global declaration

Typecheck global declaration, extending environment as needed

Generate function prototype declarations.

This allows function declarations within a translation unit to be placed in any order.

Overall typechecking environment shared by all invocations of typechecker

Typecheck a list of declarations - main entrypoint into typechecker