{"modulePath":"github.com/Open-MBEE/OpenSysML","version":"v0.8.1","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package codegen\n\nPackage codegen compiles the value subset of SysML v2 calcs to native code; see docs/project/native-compilation.md for the subset and its semantics.\n\n## Constants\n\n```go\nconst CCompilerEnvVar = \"OPENSYSML_CC\"\n```\nCCompilerEnvVar names the C compiler TargetC drives (default: cc).\n\n```go\nconst GoCommandEnvVar = \"OPENSYSML_GO\"\n```\nGoCommandEnvVar names the go command TargetGo drives (default: go).\n\n## Variables\n\n```go\nvar (\n\tMultAny = Mult{Lower: 0, Upper: -1}\n\tMultOne = Mult{Lower: 1, Upper: 1}\n)\n```\nMultAny admits every count; MultOne is \\`\\[1]\\`.\n\n```go\nvar CFlags = []string{\"-O3\", \"-flto\", \"-ffp-contract=off\", \"-std=gnu11\", \"-Wall\", \"-Wextra\", \"-Wno-unused-function\", \"-Wno-unused-parameter\"}\n```\nCFlags 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.\n\n```go\nvar ErrUnsupported = errors.New(\"not compilable\")\n```\nErrUnsupported reports notation outside the compiled subset. The message names the construct so the model can be adjusted or run interpreted.\n\n## Functions\n\n```go\nfunc Build(p *Program, target Target, output string) error\n```\nBuild compiles program for target into the executable at output, leaving the generated source beside it as output plus the target's extension.\n\n```go\nfunc EmitC(w io.Writer, p *Program, withMain bool) error\n```\nEmitC 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).\n\n```go\nfunc EmitGo(w io.Writer, p *Program) error\n```\nEmitGo writes program as a self-contained Go main package whose command line and output match the C program's.\n\n```go\nfunc Source(p *Program, target Target) ([]byte, error)\n```\nSource renders program for target, as a complete program with a main.\n\n```go\nfunc SourceExtension(target Target) string\n```\nSourceExtension is the file extension of target's source.\n\n## Types\n\n```go\ntype Arg struct {\n\tParam int\n\tValue Expr\n}\n```\nArg is one supplied argument of a Call.\n\n```go\ntype Assign struct {\n\tName  string\n\tRange Range\n\tValue Expr\n}\n```\nAssign writes a body-local variable or parameter; a scalar is checked against Range, a collection by the Checked or ToOne that Value is.\n\n```go\ntype Binary struct {\n\tOp   ast.OperatorKind\n\tL, R Expr\n\tT    Type\n}\n```\nBinary applies an arithmetic, comparison or logical operator. Operands are already coerced to a common type; T is the result type.\n\n```go\nfunc (b Binary) Type() Type\n```\n\n```go\ntype BoolLit struct{ Value bool }\n```\n\n```go\nfunc (BoolLit) Type() Type\n```\n\n```go\ntype Call struct {\n\tFn   *Func\n\tArgs []Arg\n}\n```\nCall 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.\n\n```go\nfunc (c Call) Type() Type\n```\n\n```go\ntype Checked struct {\n\tX      Expr\n\tM      Mult\n\tR      Range\n\tUnique bool\n\tWhere  string\n}\n```\nChecked 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.\n\n```go\nfunc (c Checked) Type() Type\n```\n\n```go\ntype Coalesce struct {\n\tL, R Expr\n\tT    Type\n}\n```\nCoalesce is \\`L ?? R\\`: L unless it is null. Both are of collection type T.\n\n```go\nfunc (c Coalesce) Type() Type\n```\n\n```go\ntype Compiler struct {\n\t// contains filtered or unexported fields\n}\n```\nCompiler lowers calc definitions to the codegen IR. One Compiler compiles one Program; the functions it produces are shared across calls within it.\n\n```go\nfunc New(model *semantics.Model, resolver *resolve.Resolver) *Compiler\n```\nNew returns a Compiler resolving names through resolver and typing through model.\n\n```go\nfunc (c *Compiler) Compile(entry *symbols.Symbol) (*Program, error)\n```\nCompile compiles entry and every calc it invokes, transitively.\n\n```go\ntype Cond struct {\n\tC, Then, Else Expr\n\tT             Type\n}\n```\nCond is \\`if c ? a else b\\`, both branches of type T.\n\n```go\nfunc (c Cond) Type() Type\n```\n\n```go\ntype Declare struct {\n\tName  string\n\tT     Type\n\tRange Range\n\tInit  Expr\n}\n```\nDeclare 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.\n\n```go\ntype Expr interface {\n\tType() Type\n}\n```\nExpr is a typed expression.\n\n```go\ntype Fold struct {\n\tOp   SeqOp\n\tSeq  Expr\n\tBody Lambda\n\tT    Type\n}\n```\nFold applies a body operation (seqops.go) over the elements of Seq; T is its result type.\n\n```go\nfunc (f Fold) Type() Type\n```\n\n```go\ntype ForEach struct {\n\tVar  string\n\tSeq  Expr\n\tBody []Stmt\n}\n```\nForEach runs Body once per element of Seq, bound to Var; a bare scalar is not iterable and fails, as the interpreter's \\`for\\` does.\n\n```go\ntype Framed struct{ X Expr }\n```\nFramed evaluates X as the inlined body of a library calc: one frame deeper against the recursion budget, left once X has answered.\n\n```go\nfunc (f Framed) Type() Type\n```\n\n```go\ntype Func struct {\n\tName   string // qualified SysML name\n\tIdent  string // identifier valid in every target language\n\tParams []Param\n\tResult Type\n\t// ResultRange is checked on every return; a collection result's\n\t// multiplicity is checked by the Checked the return wraps.\n\tResultRange Range\n\tBody        []Stmt\n}\n```\nFunc is one compiled calculation.\n\n```go\ntype If struct {\n\tCond Expr\n\tThen []Stmt\n\tElse []Stmt\n}\n```\nIf runs Then or Else on a Boolean condition.\n\n```go\ntype Index struct {\n\tSeq, I Expr\n}\n```\nIndex is \\`Seq#(I)\\`, one-based over the elements of Seq; I is an Integer.\n\n```go\nfunc (i Index) Type() Type\n```\n\n```go\ntype IntLit struct{ Value int64 }\n```\nIntLit, RealLit and BoolLit are literals.\n\n```go\nfunc (IntLit) Type() Type\n```\n\n```go\ntype Lambda struct {\n\tParams []Param\n\tBody   Expr\n}\n```\nLambda 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.\n\n```go\ntype Let struct {\n\tName  string\n\tValue Expr\n\tIn    Expr\n}\n```\nLet evaluates Value into the temporary Name, then In, which reads it as a Var.\n\n```go\nfunc (l Let) Type() Type\n```\n\n```go\ntype LibCall struct {\n\tOp   LibOp\n\tArgs []Arg\n}\n```\nLibCall applies a library function operation (library.go); Args bind its operands as a Call's do, each coerced to the operand's type.\n\n```go\nfunc (l LibCall) Type() Type\n```\n\n```go\ntype LibOp int\n```\nLibOp is one library function operation over scalars, chosen from the function the model calls and the types of its operands (runtime/library\\_functions.go).\n\n```go\nconst (\n\tLibSqrt LibOp = iota\n\tLibAbsReal\n\tLibAbsInt\n\tLibFloor\n\tLibRound\n\tLibMaxReal\n\tLibMinReal\n\tLibMaxInt\n\tLibMinInt\n\tLibMaxNatural\n\tLibMinNatural\n\tLibIsZero\n\tLibIsUnit\n\tLibSin\n\tLibCos\n\tLibTan\n\tLibCot\n\tLibArcsin\n\tLibArccos\n\tLibArctan\n\tLibDeg\n\tLibRad\n\tLibExp\n\tLibLn\n\tLibLog\n\tLibAtan2\n)\n```\n\n```go\nfunc (op LibOp) Operands() []Type\n```\nOperands is the types the operation's operands are coerced to.\n\n```go\nfunc (op LibOp) Result() Type\n```\nResult is the operation's result type.\n\n```go\nfunc (op LibOp) String() string\n```\n\n```go\ntype Mult struct {\n\tLower, Upper int64\n}\n```\nMult is a declared multiplicity, checked on the count of values a collection feature is bound to. Upper is negative for an unbounded feature.\n\n```go\nfunc (m Mult) Admits(n int64) bool\n```\nAdmits reports whether a value count n satisfies m.\n\n```go\ntype NullLit struct{ T Type }\n```\nNullLit is \\`null\\`, the empty value of collection type T (or TypeNull).\n\n```go\nfunc (n NullLit) Type() Type\n```\n\n```go\ntype Param struct {\n\tName   string\n\tType   Type\n\tRange  Range\n\tMult   Mult\n\tUnique bool\n}\n```\nParam is one input parameter; Range and, for a collection, Mult and Unique are checked on entry.\n\n```go\ntype Program struct {\n\tFuncs       []*Func\n\tEntry       *Func\n\tCollections bool\n}\n```\nProgram 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.\n\n```go\ntype Range int\n```\nRange narrows an Integer to the values its declared library subtype admits: Natural the non-negative Integers, Positive those above zero.\n\n```go\nconst (\n\tRangeAny Range = iota\n\tRangeNatural\n\tRangePositive\n)\n```\n\n```go\nfunc (r Range) Lower() int64\n```\nLower is the least Integer the range admits.\n\n```go\nfunc (r Range) String() string\n```\nString is the library type name the range comes from; empty for RangeAny.\n\n```go\ntype RangeExpr struct{ Lo, Hi Expr }\n```\nRangeExpr is \\`Lo..Hi\\`, the Integers from Lo to Hi, empty when Lo \u003e Hi.\n\n```go\nfunc (RangeExpr) Type() Type\n```\n\n```go\ntype RealLit struct{ Value float64 }\n```\n\n```go\nfunc (RealLit) Type() Type\n```\n\n```go\ntype Return struct{ Value Expr }\n```\nReturn answers the function's result.\n\n```go\ntype Sample struct {\n\tDom, Rng string\n\tSeq      Expr\n\tBody     Lambda\n}\n```\nSample 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.\n\n```go\nfunc (s Sample) DomType() Type\n```\nDomType and RngType are the collection types of Dom and Rng.\n\n```go\nfunc (s Sample) RngType() Type\n```\n\n```go\ntype Sampled struct {\n\tS  Sample\n\tIn Expr\n}\n```\nSampled takes the sample S for the duration of In, which reads S.Dom and S.Rng as Vars.\n\n```go\nfunc (s Sampled) Type() Type\n```\n\n```go\ntype SeqCall struct {\n\tOp   SeqOp\n\tArgs []Expr\n\tT    Type\n}\n```\nSeqCall applies a collection operation (seqops.go) to operands in parameter order; T is its result type.\n\n```go\nfunc (s SeqCall) Type() Type\n```\n\n```go\ntype SeqEq struct {\n\tL, R  Expr\n\tNeq   bool\n\tIdent bool\n}\n```\nSeqEq is \\`==\\` (Neq negated) over collections of one type: same shape, same elements in order. Ident is \\`===\\`, which no coercion precedes.\n\n```go\nfunc (SeqEq) Type() Type\n```\n\n```go\ntype SeqLit struct {\n\tElems []Expr\n\tT     Type\n}\n```\nSeqLit is \\`(a, b, …)\\`: the elements of each collection operand, in order, as a sequence of type T.\n\n```go\nfunc (s SeqLit) Type() Type\n```\n\n```go\ntype SeqOp int\n```\nSeqOp is a collection operation of the Kernel Function Library, computed by the generated runtime as internal/core/runtime/collections.go computes it.\n\n```go\nconst (\n\tSeqInvalid SeqOp = iota\n\tSeqSize\n\tSeqIsEmpty\n\tSeqNotEmpty\n\tSeqIncludes\n\tSeqIncludesOnly\n\tSeqExcludes\n\tSeqEquals\n\tSeqSame\n\tSeqUnion\n\tSeqIntersection\n\tSeqIncluding\n\tSeqIncludingAt\n\tSeqExcluding\n\tSeqSubsequence\n\tSeqExcludingAt\n\tSeqHead\n\tSeqTail\n\tSeqLast\n\tSeqAllTrue\n\tSeqAnyTrue\n\tSeqSum\n\tSeqProduct\n\t// Body operations: the last parameter is a body applied per element.\n\tSeqSelect\n\tSeqSelectOne\n\tSeqReject\n\tSeqCollect\n\tSeqForAll\n\tSeqExists\n\tSeqReduce\n\tSeqMinimize\n\tSeqMaximize\n)\n```\n\n```go\nfunc (op SeqOp) Body() int\n```\nBody is the number of parameters of the body the operation applies, 0 for a value operation.\n\n```go\nfunc (op SeqOp) Name() string\n```\nName is the qualified library function the operation computes.\n\n```go\nfunc (op SeqOp) String() string\n```\nString is Name without its library, the way the runtime's diagnostics name it.\n\n```go\ntype Stmt interface {\n\t// contains filtered or unexported methods\n}\n```\nStmt is a statement of a function body.\n\n```go\ntype Target string\n```\nTarget is a code generation backend.\n\n```go\nconst (\n\t// TargetC emits C and builds it with the system C compiler.\n\tTargetC Target = \"c\"\n\t// TargetGo emits Go and builds it with the Go toolchain.\n\tTargetGo Target = \"go\"\n)\n```\n\n```go\nfunc Targets() []Target\n```\nTargets lists the backends in the order they are documented.\n\n```go\ntype ToMany struct{ X Expr }\n```\nToMany views a scalar as the collection holding it, as the interpreter does.\n\n```go\nfunc (t ToMany) Type() Type\n```\n\n```go\ntype ToOne struct {\n\tX        Expr\n\tWhere    string\n\tFail     string\n\tBare     bool\n\tOther    Expr\n\tOtherOne string\n}\n```\nToOne 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.\n\n```go\nfunc (t ToOne) Type() Type\n```\n\n```go\ntype ToReal struct{ X Expr }\n```\nToReal widens an Integer to a Real; over a collection, every element.\n\n```go\nfunc (t ToReal) Type() Type\n```\n\n```go\ntype Type int\n```\nType 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).\n\n```go\nconst (\n\tTypeInvalid Type = iota\n\tTypeInt          // Integer and its subtypes, int64 with reported overflow\n\tTypeReal         // Real and Rational, IEEE 754 binary64\n\tTypeBool\n\tTypeNull    // `null` before context fixes its collection type\n\tTypeSeqInt  // collection of Integers\n\tTypeSeqReal // collection of Reals\n\tTypeSeqBool // collection of Booleans\n)\n```\n\n```go\nfunc (t Type) Elem() Type\n```\nElem is the scalar type of t's values: t itself for a scalar.\n\n```go\nfunc (t Type) Many() bool\n```\nMany reports whether t is a collection type.\n\n```go\nfunc (t Type) Scalar() bool\n```\nScalar reports whether t is exactly one Integer, Real or Boolean.\n\n```go\nfunc (t Type) Seq() Type\n```\nSeq is the collection type over t's scalar type.\n\n```go\nfunc (t Type) String() string\n```\n\n```go\ntype Unary struct {\n\tOp ast.OperatorKind\n\tX  Expr\n\tT  Type\n}\n```\nUnary applies \\`-\\`, \\`+\\` or \\`not\\`.\n\n```go\nfunc (u Unary) Type() Type\n```\n\n```go\ntype UnsupportedError struct {\n\tCalc string\n\tWhat string\n}\n```\nUnsupportedError is an ErrUnsupported with the construct and calc it was met in.\n\n```go\nfunc (e *UnsupportedError) Error() string\n```\n\n```go\nfunc (e *UnsupportedError) Unwrap() error\n```\n\n```go\ntype Var struct {\n\tName string\n\tT    Type\n}\n```\nVar reads a parameter or a body-local variable.\n\n```go\nfunc (v Var) Type() Type\n```\n\n```go\ntype While struct {\n\tCond  Expr\n\tUntil Expr\n\tBody  []Stmt\n}\n```\nWhile runs Body while Cond holds; Until, if set, is tested after each pass and stops the loop when it holds.\n\n","path":"github.com/Open-MBEE/OpenSysML/internal/core/codegen","name":"codegen","synopsis":"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.","isRedistributable":true}
