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 Assign
- type Binary
- type BoolLit
- type Call
- type Checked
- type Coalesce
- type Compiler
- type Cond
- type Declare
- type Expr
- type Fold
- type ForEach
- type Func
- type If
- type Index
- type IntLit
- type Lambda
- type Let
- type LibCall
- type LibOp
- type Mult
- type NullLit
- type Param
- type Program
- type Range
- type RangeExpr
- type RealLit
- type Return
- type SeqCall
- type SeqEq
- type SeqLit
- type SeqOp
- type Stmt
- type Target
- type ToMany
- type ToOne
- type ToReal
- type 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 ( 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 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
}
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, 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. The interpreter does not judge an initializer against the variable's range or multiplicity, so neither does generated code; later assignments are checked.
type Fold ¶
Fold applies a body operation (seqops.go) over the elements of Seq; T is its result type.
type ForEach ¶
ForEach runs Body once per element of Seq, bound to Var; a bare scalar is not iterable and fails, as the interpreter's `for` does.
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
Body []Stmt
}
Func is one compiled calculation.
type Index ¶
type Index struct {
Seq, I Expr
}
Index is `Seq#(I)`, one-based over the elements of Seq; I is an Integer.
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).
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.
type NullLit ¶
type NullLit struct{ T Type }
NullLit is `null`, the empty value of collection type T (or TypeNull).
type Program ¶
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. The interpreter judges Positive as Natural, so both admit every non-negative Integer and differ only in the type the refusal names.
type RangeExpr ¶
type RangeExpr struct{ Lo, Hi Expr }
RangeExpr is `Lo..Hi`, the Integers from Lo to Hi, empty when Lo > Hi.
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/core/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 Stmt ¶
type Stmt interface {
// contains filtered or unexported methods
}
Stmt is a statement of a function body.
type ToMany ¶
type ToMany struct{ X Expr }
ToMany views a scalar as the collection holding it, as the interpreter does.
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 }
ToReal widens an Integer to a Real; over a collection, every element.
type Type ¶
type Type int
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).
const ( TypeInvalid Type = iota TypeInt // Integer and its subtypes, int64 with reported overflow TypeReal // Real and Rational, IEEE 754 binary64 TypeBool TypeNull // `null` before context fixes its collection type TypeSeqInt // collection of Integers TypeSeqReal // collection of Reals TypeSeqBool // collection of Booleans )
type Unary ¶
type Unary struct {
Op ast.OperatorKind
X Expr
T Type
}
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