2019-11-07 19:05:39 +00:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-02-26 18:45:19 +00:00
|
|
|
// Package ir defines a representation of the elements of Go programs
|
2019-11-07 19:05:39 +00:00
|
|
|
// (packages, types, functions, variables and constants) using a
|
2020-02-26 18:45:19 +00:00
|
|
|
// static single-information (SSI) form intermediate representation
|
2019-11-07 19:05:39 +00:00
|
|
|
// (IR) for the bodies of functions.
|
|
|
|
//
|
|
|
|
// THIS INTERFACE IS EXPERIMENTAL AND IS LIKELY TO CHANGE.
|
|
|
|
//
|
2020-02-26 18:45:19 +00:00
|
|
|
// For an introduction to SSA form, upon which SSI builds, see
|
2019-11-07 19:05:39 +00:00
|
|
|
// http://en.wikipedia.org/wiki/Static_single_assignment_form.
|
|
|
|
// This page provides a broader reading list:
|
|
|
|
// http://www.dcs.gla.ac.uk/~jsinger/ssa.html.
|
|
|
|
//
|
2020-02-26 18:45:19 +00:00
|
|
|
// For an introduction to SSI form, see The static single information
|
|
|
|
// form by C. Scott Ananian.
|
|
|
|
//
|
|
|
|
// The level of abstraction of the IR form is intentionally close to
|
2019-11-07 19:05:39 +00:00
|
|
|
// the source language to facilitate construction of source analysis
|
|
|
|
// tools. It is not intended for machine code generation.
|
|
|
|
//
|
2020-02-26 18:45:19 +00:00
|
|
|
// The simplest way to create the IR of a package is
|
2019-11-07 19:05:39 +00:00
|
|
|
// to load typed syntax trees using golang.org/x/tools/go/packages, then
|
2020-02-26 18:45:19 +00:00
|
|
|
// invoke the irutil.Packages helper function. See ExampleLoadPackages
|
2019-11-07 19:05:39 +00:00
|
|
|
// and ExampleWholeProgram for examples.
|
2020-02-26 18:45:19 +00:00
|
|
|
// The resulting ir.Program contains all the packages and their
|
|
|
|
// members, but IR code is not created for function bodies until a
|
2019-11-07 19:05:39 +00:00
|
|
|
// subsequent call to (*Package).Build or (*Program).Build.
|
|
|
|
//
|
2020-02-26 18:45:19 +00:00
|
|
|
// The builder initially builds a naive IR form in which all local
|
2019-11-07 19:05:39 +00:00
|
|
|
// variables are addresses of stack locations with explicit loads and
|
|
|
|
// stores. Registerisation of eligible locals and φ-node insertion
|
|
|
|
// using dominance and dataflow are then performed as a second pass
|
|
|
|
// called "lifting" to improve the accuracy and performance of
|
|
|
|
// subsequent analyses; this pass can be skipped by setting the
|
|
|
|
// NaiveForm builder flag.
|
|
|
|
//
|
|
|
|
// The primary interfaces of this package are:
|
|
|
|
//
|
|
|
|
// - Member: a named member of a Go package.
|
|
|
|
// - Value: an expression that yields a value.
|
|
|
|
// - Instruction: a statement that consumes values and performs computation.
|
2020-02-26 18:45:19 +00:00
|
|
|
// - Node: a Value or Instruction (emphasizing its membership in the IR value graph)
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
|
|
|
// A computation that yields a result implements both the Value and
|
|
|
|
// Instruction interfaces. The following table shows for each
|
|
|
|
// concrete type which of these interfaces it implements.
|
|
|
|
//
|
|
|
|
// Value? Instruction? Member?
|
|
|
|
// *Alloc ✔ ✔
|
|
|
|
// *BinOp ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *BlankStore ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Builtin ✔
|
|
|
|
// *Call ✔ ✔
|
|
|
|
// *ChangeInterface ✔ ✔
|
|
|
|
// *ChangeType ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Const ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Convert ✔ ✔
|
|
|
|
// *DebugRef ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Defer ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Extract ✔ ✔
|
|
|
|
// *Field ✔ ✔
|
|
|
|
// *FieldAddr ✔ ✔
|
|
|
|
// *FreeVar ✔
|
|
|
|
// *Function ✔ ✔ (func)
|
|
|
|
// *Global ✔ ✔ (var)
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Go ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *If ✔
|
|
|
|
// *Index ✔ ✔
|
|
|
|
// *IndexAddr ✔ ✔
|
|
|
|
// *Jump ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Load ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *MakeChan ✔ ✔
|
|
|
|
// *MakeClosure ✔ ✔
|
|
|
|
// *MakeInterface ✔ ✔
|
|
|
|
// *MakeMap ✔ ✔
|
|
|
|
// *MakeSlice ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *MapLookup ✔ ✔
|
|
|
|
// *MapUpdate ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *NamedConst ✔ (const)
|
|
|
|
// *Next ✔ ✔
|
|
|
|
// *Panic ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Parameter ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Phi ✔ ✔
|
|
|
|
// *Range ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Recv ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Return ✔
|
|
|
|
// *RunDefers ✔
|
|
|
|
// *Select ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Send ✔ ✔
|
|
|
|
// *Sigma ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Slice ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Store ✔ ✔
|
|
|
|
// *StringLookup ✔ ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
// *Type ✔ (type)
|
|
|
|
// *TypeAssert ✔ ✔
|
|
|
|
// *UnOp ✔ ✔
|
2020-02-26 18:45:19 +00:00
|
|
|
// *Unreachable ✔
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
|
|
|
// Other key types in this package include: Program, Package, Function
|
|
|
|
// and BasicBlock.
|
|
|
|
//
|
|
|
|
// The program representation constructed by this package is fully
|
|
|
|
// resolved internally, i.e. it does not rely on the names of Values,
|
|
|
|
// Packages, Functions, Types or BasicBlocks for the correct
|
|
|
|
// interpretation of the program. Only the identities of objects and
|
2020-02-26 18:45:19 +00:00
|
|
|
// the topology of the IR and type graphs are semantically
|
2019-11-07 19:05:39 +00:00
|
|
|
// significant. (There is one exception: Ids, used to identify field
|
|
|
|
// and method names, contain strings.) Avoidance of name-based
|
|
|
|
// operations simplifies the implementation of subsequent passes and
|
|
|
|
// can make them very efficient. Many objects are nonetheless named
|
|
|
|
// to aid in debugging, but it is not essential that the names be
|
|
|
|
// either accurate or unambiguous. The public API exposes a number of
|
|
|
|
// name-based maps for client convenience.
|
|
|
|
//
|
2020-02-26 18:45:19 +00:00
|
|
|
// The ir/irutil package provides various utilities that depend only
|
2019-11-07 19:05:39 +00:00
|
|
|
// on the public API of this package.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): Consider the exceptional control-flow implications
|
|
|
|
// of defer and recover().
|
|
|
|
//
|
|
|
|
// TODO(adonovan): write a how-to document for all the various cases
|
|
|
|
// of trying to determine corresponding elements across the four
|
|
|
|
// domains of source locations, ast.Nodes, types.Objects,
|
2020-02-26 18:45:19 +00:00
|
|
|
// ir.Values/Instructions.
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
2020-02-26 18:45:19 +00:00
|
|
|
package ir // import "honnef.co/go/tools/ir"
|