recipe

package
v0.0.0-...-1dd1f65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package recipe contains 'recipes': instructions to the machine node on how to compile a subject.

Index

Examples

Constants

View Source
const PopAll = 0

PopAll is the value to pass to NPops to ask the instruction to pop all applicable files off the stack.

Variables

View Source
var ErrNotTakingInstructions = errors.New("this output type doesn't support instructions")

ErrNotTakingInstructions occurs if we try to add instructions to a recipe where they don't make sense.

Functions

This section is empty.

Types

type Instruction

type Instruction struct {
	// Op is the opcode.
	Op Op `json:"op"`

	// File is, if applicable, the file argument to the instruction.
	File string `json:"file,omitempty"`

	// FileKind is, if applicable, the file kind argument to the instruction.
	FileKind filekind.Kind `json:"file_kind,omitempty"`

	// NPops is, if applicable and nonzero, the maximum number of items to pop off the file stack.
	NPops int `json:"npops,omitempty"`
}

Instruction represents a single instruction in a recipe.

Instructions target a stack machine in the machine node.

func CompileExeInst

func CompileExeInst(npops int) Instruction

CompileExeInst produces a 'compile binary' instruction.

Example

ExampleCompileExeInst is a runnable example for CompileExeInst.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	fmt.Println(recipe.CompileExeInst(recipe.PopAll))
	fmt.Println(recipe.CompileExeInst(1))

}
Output:

CompileExe ALL
CompileExe 1

func CompileObjInst

func CompileObjInst(npops int) Instruction

CompileObjInst produces a 'compile object' instruction.

Example

ExampleCompileObjInst is a runnable example for CompileObjInst.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	fmt.Println(recipe.CompileObjInst(recipe.PopAll))
	fmt.Println(recipe.CompileObjInst(1))

}
Output:

CompileObj ALL
CompileObj 1

func PushInputInst

func PushInputInst(file string) Instruction

PushInputInst produces a 'push input' instruction.

Example

ExamplePushInputInst is a runnable example for PushInputInst.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	fmt.Println(recipe.PushInputInst("foo.c"))

}
Output:

PushInput "foo.c"

func PushInputsInst

func PushInputsInst(kind filekind.Kind) Instruction

PushInputsInst produces a 'push inputs' instruction.

Example

ExamplePushInputsInst is a runnable example for PushInputsInst.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/filekind"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	fmt.Println(recipe.PushInputsInst(filekind.C))

}
Output:

PushInputs c

func (Instruction) String

func (i Instruction) String() string

String produces a human-readable string representation of this instruction.

type Map

type Map map[id.ID]Recipe

Map is shorthand from a map from architecture IDs to recipes.

type Op

type Op uint8

Op is the enumeration of kinds of operation that can be in a recipe.

const (
	// Nop is a no-operation.
	Nop Op = iota
	// PushInputs is a compile instruction that pushes all unconsumed inputs (matching the filekind argument, if given).
	PushInputs
	// PushInput is a compile instruction that pushes a specific input onto the stack, consuming it.
	// Takes a file argument.
	PushInput
	// CompileObj pops inputs off the stack, compile them to an object, and push the name of the object onto the stack.
	CompileObj
	// CompileExe pops inputs off the stack, compile them, and output the results to the output binary.
	CompileExe

	// Last is the last operation defined.
	Last = CompileExe
)

func OpFromString

func OpFromString(s string) (Op, error)

OpFromString tries to convert a string into an Op.

Example

ExampleOpFromString is a runnable example for OpFromString.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	o, _ := recipe.OpFromString("CompileExe")
	fmt.Println(o)
	_, err := recipe.OpFromString("None-such")
	fmt.Println(err)

}
Output:

CompileExe
unknown Op: "None-such"

func (Op) MarshalJSON

func (i Op) MarshalJSON() ([]byte, error)

MarshalJSON marshals an op to JSON using its string form.

Example

ExampleOp_MarshalJSON is a runnable example for MarshalJSON.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	bs, _ := recipe.CompileExe.MarshalJSON()
	fmt.Println(string(bs))

}
Output:

"CompileExe"

func (Op) String

func (i Op) String() string
Example

ExampleOp_String is a runnable example for String.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	fmt.Println(recipe.CompileExe)
	fmt.Println(recipe.Op(42))

}
Output:

CompileExe
Op(42)

func (*Op) UnmarshalJSON

func (i *Op) UnmarshalJSON(bytes []byte) error

UnmarshalJSON unmarshals an op from JSON using its string form.

type Option

type Option func(*Recipe) error

Option is a functional option for a recipe.

func AddFiles

func AddFiles(fs ...string) Option

AddFiles adds each file in fs to the recipe.

func AddInstructions

func AddInstructions(ins ...Instruction) Option

AddInstructions adds each instruction in ins to the recipe.

func CompileAllCToExe

func CompileAllCToExe() Option

CompileAllCToExe adds a set of instructions that compile all C inputs to an executable at path.

func CompileFileToObj

func CompileFileToObj(file string) Option

CompileFileToObj adds a set of instructions that compile the named C input to an object file.

func Options

func Options(os ...Option) Option

Options applies multiple options to a recipe.

type Output

type Output uint8

Output is the enumeration of types of recipe outputs.

const (
	// OutNothing means this recipe doesn't output anything, and so need not be run.
	// The recipe can still include files to make available to the backend's run-time driver.
	OutNothing Output = iota
	// OutObj means this recipe outputs an object file that should be fed into another recipe.
	OutObj
	// OutExe means this recipe outputs an executable that should be run with output piped into the observation parser.
	OutExe
)

func (Output) String

func (i Output) String() string

type Recipe

type Recipe struct {
	// Dir is the root directory of the recipe.
	Dir string `toml:"dir" json:"dir"`

	// Files is a list of files initially available in the recipe.
	Files []string `toml:"files" json:"files,omitempty"`

	// Instructions is a list of instructions for the machine node.
	Instructions []Instruction `json:"instructions,omitempty"`

	// OutType is the type of output this recipe promises.  The output file is implicit.
	Output Output `json:"out_type,omitempty"`
}

Recipe represents information about a lifted test recipe.

func New

func New(dir string, otype Output, os ...Option) (Recipe, error)

New constructs a recipe using the input directory dir and the options os.

func (*Recipe) NeedsCompile

func (r *Recipe) NeedsCompile() bool

NeedsCompile gets whether this recipe needs to be compiled (ie, its instructions should be interpreted).

Example

ExampleRecipe_NeedsCompile is a testable example for Recipe.NeedsCompile.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	r1, _ := recipe.New("foo/bar", recipe.OutNothing, recipe.AddFiles("baz", "barbaz", "foobar"))
	fmt.Println("first recipe needs compile:", r1.NeedsCompile())
	r2, _ := recipe.New(
		"foo/bar",
		recipe.OutExe,
		recipe.AddFiles("baz", "barbaz", "foobar"),
		recipe.CompileAllCToExe(),
	)
	fmt.Println("second recipe needs compile:", r2.NeedsCompile())

}
Output:

first recipe needs compile: false
second recipe needs compile: true

func (*Recipe) Paths

func (r *Recipe) Paths() []string

Paths retrieves the slash-joined dir/file paths for each file in the recipe.

Example

ExampleRecipe_Paths is a testable example for Recipe.Paths.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/recipe"
)

func main() {
	h, _ := recipe.New("foo/bar", recipe.OutNothing, recipe.AddFiles("baz", "barbaz", "foobar"))
	for _, f := range h.Paths() {
		fmt.Println(f)
	}

}
Output:

foo/bar/baz
foo/bar/barbaz
foo/bar/foobar

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL