Documentation
¶
Overview ¶
Package codegen compiles the value subset of SysML v2 calcs to native code; see docs/project/native-compilation.md for the subset and its semantics.
Index ¶
- Constants
- Variables
- func Build(p *Program, target Target, output string) error
- func EmitC(w io.Writer, p *Program, withMain bool) error
- func EmitGo(w io.Writer, p *Program) error
- func Source(p *Program, target Target) ([]byte, error)
- func SourceExtension(target Target) string
- type Arg
- type AsInt
- type Assign
- type Binary
- type BoolLit
- type Call
- type Checked
- type Coalesce
- type Compiler
- type Cond
- type Declare
- type Enum
- type EnumLit
- type EnumText
- type Expr
- type Field
- type FnCase
- type FnDispatch
- type FnEnv
- type FnLit
- type FnSelf
- type FnSet
- type FnWiden
- type Fold
- type ForEach
- type Framed
- type Func
- type Held
- type If
- type Index
- type IntLit
- type IsUnset
- type Lambda
- type Let
- type LibCall
- type LibOp
- type Lift
- type Mult
- type Named
- type Narrowed
- type Need
- type NewUnset
- type NullLit
- type NumSplit
- type Param
- type Program
- type Range
- type RangeExpr
- type RealLit
- type RecGet
- type RecNew
- type Record
- type Refusal
- type Relabel
- type Return
- type SameUnset
- type Sample
- type SampleSteps
- type Sampled
- type SeqCall
- type SeqEq
- type SeqLit
- type SeqOp
- type Steps
- type Stmt
- type StrLit
- type Strip
- type Target
- type ToMany
- type ToNum
- type ToOne
- type ToReal
- type Type
- func (t Type) Concrete() Type
- func (t Type) Elem() Type
- func (t Type) IsEnum() bool
- func (t Type) IsFn() bool
- func (t Type) IsRec() bool
- func (t Type) Many() bool
- func (t Type) MayUnset() bool
- func (t Type) Scalar() bool
- func (t Type) Seq() Type
- func (t Type) String() string
- func (t Type) Unsettable() Type
- type Unary
- type UnsupportedError
- type Var
- type While
Constants ¶
const CCompilerEnvVar = "OPENSYSML_CC"
CCompilerEnvVar names the C compiler TargetC drives (default: cc).
const GoCommandEnvVar = "OPENSYSML_GO"
GoCommandEnvVar names the go command TargetGo drives (default: go).
Variables ¶
var ( TypeInvalid = Type{} TypeInt = Type{/* contains filtered or unexported fields */} // Integer and its subtypes, unbounded (int64 until a result leaves it) TypeReal = Type{/* contains filtered or unexported fields */} // Real and Rational, IEEE 754 binary64 TypeBool = Type{/* contains filtered or unexported fields */} // Boolean TypeNum = Type{/* contains filtered or unexported fields */} // a Real-typed value, an Integer or a Real by run-time kind TypeString = Type{/* contains filtered or unexported fields */} // a String, its characters Unicode code points held as UTF-8 TypeNull = Type{/* contains filtered or unexported fields */} // `null` before context fixes its collection type TypeRun = Type{/* contains filtered or unexported fields */} // the identity of one run of a calc body, which its closures carry TypeSeqInt = Type{/* contains filtered or unexported fields */} // collection of Integers TypeSeqReal = Type{/* contains filtered or unexported fields */} // collection of Reals TypeSeqBool = Type{/* contains filtered or unexported fields */} // collection of Booleans TypeSeqNum = Type{/* contains filtered or unexported fields */} // collection of numbers, each element of its own kind TypeSeqString = Type{/* contains filtered or unexported fields */} // collection of Strings )
var ( MultAny = Mult{Lower: 0, Upper: -1} MultOne = Mult{Lower: 1, Upper: 1} )
MultAny admits every count; MultOne is `[1]`.
var CFlags = []string{"-O3", "-flto", "-ffp-contract=off", "-std=gnu11", "-Wall", "-Wextra", "-Wno-unused-function", "-Wno-unused-parameter"}
CFlags are the C compiler options; the prelude's explicit checks keep -O3 safe, and no contraction keeps every Real operation rounded as the interpreter rounds it.
var ErrUnsupported = errors.New("not compilable")
ErrUnsupported reports notation outside the compiled subset. The message names the construct so the model can be adjusted or run interpreted.
Functions ¶
func Build ¶
Build compiles program for target into the executable at output, leaving the generated source beside it as output plus the target's extension.
func EmitC ¶
EmitC writes program as a self-contained C translation unit, with a main that reads argv and prints the result when withMain is set (errors: status 1, stderr).
func EmitGo ¶
EmitGo writes program as a self-contained Go main package whose command line and output match the C program's.
func SourceExtension ¶
SourceExtension is the file extension of target's source.
Types ¶
type AsInt ¶ added in v0.10.0
type AsInt struct{ X Expr }
AsInt is the Integer a number holds, read where NumSplit has found one.
type Assign ¶
Assign writes a body-local variable or parameter; a scalar is checked against Range, a collection by the Checked or ToOne that Value is.
type Binary ¶
type Binary struct {
Op ast.OperatorKind
L, R Expr
T Type
// Exact is a Rational operation over operands binary64 holds exactly,
// computed as one correctly rounded binary64 operation; an Integer
// quotient is then compared exactly against a whole number.
Exact bool
// Whole is an Exact sum, difference or product of whole numbers.
Whole bool
// Guard requires a Whole result below 2^53, where binary64 holds it exactly.
Guard bool
}
Binary applies an arithmetic, comparison or logical operator. Operands are already coerced to a common type; T is the result type.
type Call ¶
Call invokes another compiled function, arguments coerced to parameter types. Call invokes Fn. Args are evaluated in source order, each binding the parameter at Param; a parameter named twice takes the later value.
type Checked ¶
Checked binds a collection to a feature of multiplicity M and range R at Where, refusing a repeated element when Unique, failing as the interpreter's binding does.
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler lowers calc definitions to the codegen IR. One Compiler compiles one Program; the functions it produces are shared across calls within it.
type Declare ¶
Declare introduces a body-local variable with its initial value, null (a collection) when Init is nil. A scalar Init is checked against Range, a collection by the Checked or ToOne that Init is, as an Assign is.
type Enum ¶ added in v0.10.0
type Enum struct {
Name string // qualified name
Short string // the enumeration's own name, which a literal prints under
Literals []string // literal names in declaration order
Base int
ID int // position in Program.Enums
}
Enum is an enumeration definition whose literals are identified by themselves: literal i is the value Base+i of the program's literal table.
type EnumLit ¶ added in v0.10.0
EnumLit is literal I of the enumeration T names, identified by itself.
type EnumText ¶ added in v0.10.0
type EnumText struct{ X Expr }
EnumText is the qualified name an enumeration literal prints as, the String BaseFunctions::ToString gives of it.
type FnCase ¶ added in v0.10.0
type FnCase struct {
Name string // qualified name, which the value prints as
ID int // position in Program.FnCases
// Closure is set for a body-local calc; Env are the bindings it captures.
Closure bool
Env []Param
// Self is the record a record's calc is read off, which its value holds.
Self *Record
// contains filtered or unexported fields
}
FnCase is one function a function value may be at run time: a calc, read against no object, a body-local calc closing over the run that read it, or a record's calc closing over the record it was read off.
type FnDispatch ¶ added in v0.10.0
FnDispatch evaluates F into variable Name, then the arm of Cases its case selects: an application of that function.
func (FnDispatch) Type ¶ added in v0.10.0
func (x FnDispatch) Type() Type
type FnEnv ¶ added in v0.10.0
FnEnv is captured binding I of case Case, read from the function value in variable Name, which holds that case.
type FnLit ¶ added in v0.10.0
FnLit is a read of case Case; for a closure, Run is the identity of the run of the body declaring it and Env the values of its captured bindings; for a record's calc, Self is the record it is read off.
type FnSelf ¶ added in v0.10.0
FnSelf is the record the function value in variable Name, which holds a record's calc, was read off.
type FnSet ¶ added in v0.10.0
FnSet is the set of cases a function type ranges over, in case order; sets are interned, so equal sets are one *FnSet.
type Fold ¶
Fold applies a body operation (seqops.go) over the elements of Seq, spending Steps once Seq is evaluated; T is its result type.
type ForEach ¶
ForEach runs Body once per element of Seq, bound to Var, each pass spending a step; a bare scalar is not iterable and fails, as the interpreter's `for` does.
type Framed ¶
type Framed struct{ X Expr }
Framed evaluates X as the inlined body of a library calc: one frame deeper against the recursion budget, left once X has answered.
type Func ¶
type Func struct {
Name string // qualified SysML name
Ident string // identifier valid in every target language
Params []Param
Result Type
// ResultRange is checked on every return; a collection result's
// multiplicity is checked by the Checked the return wraps.
ResultRange Range
// HeldResult shapes a scalar Result into a one-element sequence as it leaves
// the program: the result states no multiplicity, so it is held in the
// effective `[0..*]` of the result it redefines (SysML v2 §7.6.3, §7.19.2).
HeldResult bool
Body []Stmt
// Captured is the count of trailing Params a closure's value carries, the
// bindings of its enclosing body it reads.
Captured int
// Run names the variable holding this run's identity, which the closures
// the body declares carry; empty when none is read.
Run string
// contains filtered or unexported fields
}
Func is one compiled calculation.
type Held ¶ added in v0.11.0
Held is X in the shape its binding's multiplicity gives its values: a multi-valued binding holds a sequence (one or none included), an at-most-one its sole element.
type Index ¶
type Index struct {
Seq, I Expr
}
Index is `Seq#(I)`, one-based over the elements of Seq; I is an Integer.
type IntLit ¶
IntLit, RealLit and BoolLit are literals. An IntLit beyond int64 holds its value in Big, Value then being zero.
type IsUnset ¶ added in v0.10.0
type IsUnset struct{ X Expr }
IsUnset reports whether X, a Var, is unset.
type Lambda ¶
Lambda is a body expression `{in x; …}`: Params are bound to the operation's per-element arguments in order, then Body is evaluated; locals the body declares are inlined into Body where it names them.
type LibCall ¶
LibCall applies a library function operation (library.go); Args bind its operands as a Call's do, each coerced to the operand's type.
type LibOp ¶
type LibOp int
LibOp is one library function operation over scalars, chosen from the function the model calls and the types of its operands (runtime/library_functions.go).
const ( LibSqrt LibOp = iota LibAbsReal LibAbsInt LibFloor LibRound LibMaxReal LibMinReal LibMaxInt LibMinInt LibMaxNatural LibMinNatural LibIsZero LibIsUnit LibSin LibCos LibTan LibCot LibArcsin LibArccos LibArctan LibDeg LibRad LibExp LibLn LibLog LibAtan2 LibLength LibSubstring LibIntToString LibNaturalToString LibRealToString LibBoolToString LibStringToString )
type Lift ¶ added in v0.10.0
Lift is the concrete scalar X as a value of T, its type admitting unset.
type Mult ¶
type Mult struct {
Lower, Upper int64
}
Mult is a declared multiplicity, checked on the count of values a collection feature is bound to. Upper is negative for an unbounded feature.
func (Mult) MultiValued ¶ added in v0.11.0
MultiValued reports whether m admits more than one value.
type Named ¶ added in v0.10.0
Named is X, which may be unset, under the text the interpreter names its expression by when it holds no value.
type Narrowed ¶ added in v0.10.0
Narrowed is the scalar X checked against the range R of the feature it is written to.
type NewUnset ¶ added in v0.10.0
NewUnset is a fresh unset value of the scalar type T, which MayUnset, of a feature whose Integer values R narrows.
type NullLit ¶
type NullLit struct{ T Type }
NullLit is `null`, the empty value of collection type T (or TypeNull).
type NumSplit ¶ added in v0.10.0
NumSplit is Int when every number in Nums holds an Integer, else Real; both branches are of type T and read only Vars already evaluated.
type Param ¶
type Param struct {
Name string
Type Type
Range Range
Mult Mult
Unique bool
// Held: a collection parameter is held in the shape Mult gives it (Held).
Held bool
}
Param is one input parameter; Range and, for a collection, Mult and Unique are checked on entry.
type Program ¶
type Program struct {
Funcs []*Func
Enums []*Enum
Records []*Record
FnCases []*FnCase
Entry *Func
Collections bool
// Target is the backend the program was compiled for.
Target Target
}
Program is a set of compiled functions with one entry point. Collections is set when any function handles a collection, which the emitters' element budget and per-statement release then track.
type Range ¶
type Range int
Range narrows an Integer to the values its declared library subtype admits: Natural the non-negative Integers, Positive those above zero.
type RangeExpr ¶
type RangeExpr struct{ Lo, Hi Expr }
RangeExpr is `Lo..Hi`, the Integers from Lo to Hi, empty when Lo > Hi; each element spends a step, then is charged to the element budget.
type RealLit ¶
type RecNew ¶ added in v0.10.0
RecNew is a new record of Rec, its features bound to Fields in field order, each already evaluated and checked.
type Record ¶ added in v0.10.0
type Record struct {
Name string // qualified name
Short string // the definition's own name, which diagnostics name it by
Fields []Field
ID int // position in Program.Records
// contains filtered or unexported fields
}
Record is an attribute definition whose values are data values with features, each identified by itself as the interpreter's objects are.
type Refusal ¶ added in v0.10.0
Refusal evaluates Operands in order, then fails. Its message is Parts, joined by the interpreter's description of each operand when Describe is set.
type Relabel ¶ added in v0.10.0
Relabel is the concrete V as a value of T that is unset exactly when Of is, keeping Of's identity; Of is a Var.
type SameUnset ¶ added in v0.10.0
SameUnset reports, Neq negated, whether L and R, Vars of which one is unset, are the same value.
type Sample ¶
type Sample struct {
Dom, Rng string
Seq Expr
Body Lambda
Steps SampleSteps
}
Sample takes `Sample(f, Seq)` one frame deeper: Dom gets the domain values, Rng Body at each in order, every sample charged as the interpreter's pair is. Steps are spent on entering the frame, before and after Body at each element, and once all are taken.
type SampleSteps ¶ added in v0.10.0
type SampleSteps struct {
Enter, Before, After, Done int64
}
SampleSteps are the steps a Sample spends at each point of its library body.
type Sampled ¶
Sampled takes the sample S for the duration of In, which reads S.Dom and S.Rng as Vars.
type SeqCall ¶
SeqCall applies a collection operation (seqops.go) to operands in parameter order; T is its result type.
type SeqEq ¶
SeqEq is `==` (Neq negated) over collections of one type: same shape, same elements in order. Ident is `===`, which no coercion precedes.
type SeqLit ¶
SeqLit is `(a, b, …)`: the elements of each collection operand, in order, as a sequence of type T.
type SeqOp ¶
type SeqOp int
SeqOp is a collection operation of the Kernel Function Library, computed by the generated runtime as internal/exec/runtime/collections.go computes it.
const ( SeqInvalid SeqOp = iota SeqSize SeqIsEmpty SeqNotEmpty SeqIncludes SeqIncludesOnly SeqExcludes SeqEquals SeqSame SeqUnion SeqIntersection SeqIncluding SeqIncludingAt SeqExcluding SeqSubsequence SeqExcludingAt SeqHead SeqTail SeqLast SeqAllTrue SeqAnyTrue SeqSum SeqProduct // Body operations: the last parameter is a body applied per element. SeqSelect SeqSelectOne SeqReject SeqCollect SeqForAll SeqExists SeqReduce SeqMinimize SeqMaximize )
func (SeqOp) Body ¶
Body is the number of parameters of the body the operation applies, 0 for a value operation.
type Steps ¶ added in v0.10.0
Steps spends N evaluation steps of the run's step budget, then evaluates X.
type Stmt ¶
type Stmt interface {
// contains filtered or unexported methods
}
Stmt is a statement of a function body.
type StrLit ¶ added in v0.10.0
type StrLit struct{ Value string }
StrLit is a String literal, Value its characters as UTF-8.
type Strip ¶ added in v0.10.0
type Strip struct{ X Expr }
Strip is the value X holds, which the program has found is not unset.
type ToMany ¶
type ToMany struct{ X Expr }
ToMany views a scalar as the collection holding it, as the interpreter does.
type ToNum ¶ added in v0.10.0
type ToNum struct{ X Expr }
ToNum views an Integer or a Real as a number keeping its kind; over a collection, every element.
type ToOne ¶
ToOne is the one scalar X holds; Where names a `[1]` binding, else Fail's %s is the shape found ("a sequence", bare "sequence") and Other's a second %s.
type ToReal ¶
type ToReal struct {
X Expr
// Exact requires the Integer to be one binary64 holds exactly.
Exact bool
}
ToReal widens an Integer, or a number of either kind, to a Real; over a collection, every element.
type Type ¶
Type is a type of the compiled subset: a scalar, or a collection of scalars. A collection value is the interpreter's dynamic view of a multi-valued feature: null, one bare scalar, or a sequence of any length (its shape). An enumeration-literal type names its enumeration in Enum, a function type the functions its values range over in Fns, a record type its attribute definition in Rec.
func (Type) MayUnset ¶ added in v0.10.0
MayUnset reports whether t is a scalar that may instead be unset: the interpreter's materialized value of a required feature nothing was written to, which counts as one value, has an identity, and fails where a concrete value is required.
func (Type) Scalar ¶
Scalar reports whether t is exactly one Integer, Real, Boolean, number, String, enumeration literal, function or record.
func (Type) Unsettable ¶ added in v0.10.0
Unsettable is the scalar t admitting an unset value too.
type Unary ¶
type Unary struct {
Op ast.OperatorKind
X Expr
T Type
// Exact negates an exact Rational, whose zero is unsigned.
Exact bool
}
Unary applies `-`, `+` or `not`.
type UnsupportedError ¶
UnsupportedError is an ErrUnsupported with the construct and calc it was met in.
func (*UnsupportedError) Error ¶
func (e *UnsupportedError) Error() string
func (*UnsupportedError) Unwrap ¶
func (e *UnsupportedError) Unwrap() error