2012-04-30 08:40:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Expression represents all the different forms of expressions possible in
|
|
|
|
// side an XML protocol description file. It's also received a few custom
|
|
|
|
// addendums to make applying special functions (like padding) easier.
|
2012-04-30 08:40:55 +02:00
|
|
|
type Expression interface {
|
2012-05-06 08:21:31 +02:00
|
|
|
// Concrete determines whether this particular expression can be computed
|
|
|
|
// to some constant value inside xgbgen. (The alternative is that the
|
|
|
|
// expression can only be computed with values at run time of the
|
|
|
|
// generated code.)
|
2012-04-30 08:40:55 +02:00
|
|
|
Concrete() bool
|
2012-05-06 08:21:31 +02:00
|
|
|
|
|
|
|
// Eval evaluates a concrete expression. It is an error to call Eval
|
|
|
|
// on any expression that is not concrete (or contains any sub-expression
|
|
|
|
// that is not concrete).
|
2012-05-06 09:06:02 +02:00
|
|
|
Eval() int
|
2012-05-06 08:21:31 +02:00
|
|
|
|
|
|
|
// Reduce attempts to evaluate any concrete sub-expressions.
|
|
|
|
// i.e., (1 + 2 * (5 + 1 + someSizeOfStruct) reduces to
|
|
|
|
// (3 * (6 + someSizeOfStruct)).
|
|
|
|
// 'prefix' is used preprended to any field reference name.
|
2012-05-03 07:00:01 +02:00
|
|
|
Reduce(prefix string) string
|
2012-05-06 08:21:31 +02:00
|
|
|
|
|
|
|
// String is an alias for Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
String() string
|
2012-05-06 08:21:31 +02:00
|
|
|
|
|
|
|
// Initialize makes sure all names in this expression and any subexpressions
|
|
|
|
// have been translated to Go source names.
|
2012-04-30 08:40:55 +02:00
|
|
|
Initialize(p *Protocol)
|
|
|
|
}
|
|
|
|
|
2012-04-30 22:18:17 +02:00
|
|
|
// Function is a custom expression not found in the XML. It's simply used
|
|
|
|
// to apply a function named in 'Name' to the Expr expression.
|
|
|
|
type Function struct {
|
|
|
|
Name string
|
|
|
|
Expr Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Function) Concrete() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *Function) Eval() int {
|
2012-04-30 22:18:17 +02:00
|
|
|
log.Fatalf("Cannot evaluate a 'Function'. It is not concrete.")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *Function) Reduce(prefix string) string {
|
|
|
|
return fmt.Sprintf("%s(%s)", e.Name, e.Expr.Reduce(prefix))
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Function) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Function) Initialize(p *Protocol) {
|
|
|
|
e.Expr.Initialize(p)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// BinaryOp is an expression that performs some operation (defined in the XML
|
|
|
|
// file) with Expr1 and Expr2 as operands.
|
2012-04-30 08:40:55 +02:00
|
|
|
type BinaryOp struct {
|
2012-04-30 08:44:31 +02:00
|
|
|
Op string
|
2012-04-30 08:40:55 +02:00
|
|
|
Expr1 Expression
|
|
|
|
Expr2 Expression
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// newBinaryOp constructs a new binary expression when both expr1 and expr2
|
|
|
|
// are not nil. If one or both are nil, then the non-nil expression is
|
|
|
|
// returned unchanged or nil is returned.
|
2012-04-30 08:40:55 +02:00
|
|
|
func newBinaryOp(op string, expr1, expr2 Expression) Expression {
|
|
|
|
switch {
|
|
|
|
case expr1 != nil && expr2 != nil:
|
|
|
|
return &BinaryOp{
|
2012-04-30 08:44:31 +02:00
|
|
|
Op: op,
|
2012-04-30 08:40:55 +02:00
|
|
|
Expr1: expr1,
|
|
|
|
Expr2: expr2,
|
|
|
|
}
|
|
|
|
case expr1 != nil && expr2 == nil:
|
|
|
|
return expr1
|
|
|
|
case expr1 == nil && expr2 != nil:
|
|
|
|
return expr2
|
|
|
|
case expr1 == nil && expr2 == nil:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BinaryOp) Concrete() bool {
|
|
|
|
return e.Expr1.Concrete() && e.Expr2.Concrete()
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *BinaryOp) Eval() int {
|
2012-04-30 08:40:55 +02:00
|
|
|
switch e.Op {
|
|
|
|
case "+":
|
|
|
|
return e.Expr1.Eval() + e.Expr2.Eval()
|
|
|
|
case "-":
|
|
|
|
return e.Expr1.Eval() - e.Expr2.Eval()
|
|
|
|
case "*":
|
|
|
|
return e.Expr1.Eval() * e.Expr2.Eval()
|
|
|
|
case "/":
|
|
|
|
return e.Expr1.Eval() / e.Expr2.Eval()
|
|
|
|
case "&":
|
|
|
|
return e.Expr1.Eval() & e.Expr2.Eval()
|
|
|
|
case "<<":
|
2012-05-06 09:06:02 +02:00
|
|
|
return int(uint(e.Expr1.Eval()) << uint(e.Expr2.Eval()))
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Fatalf("Invalid binary operator '%s' for expression.", e.Op)
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *BinaryOp) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
if e.Concrete() {
|
|
|
|
return fmt.Sprintf("%d", e.Eval())
|
|
|
|
}
|
2012-05-03 07:00:01 +02:00
|
|
|
|
|
|
|
// An incredibly dirty hack to make sure any time we perform an operation
|
|
|
|
// on a field, we're dealing with ints...
|
|
|
|
expr1, expr2 := e.Expr1, e.Expr2
|
|
|
|
switch expr1.(type) {
|
|
|
|
case *FieldRef:
|
|
|
|
expr1 = &Function{
|
|
|
|
Name: "int",
|
|
|
|
Expr: expr1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch expr2.(type) {
|
|
|
|
case *FieldRef:
|
|
|
|
expr2 = &Function{
|
|
|
|
Name: "int",
|
|
|
|
Expr: expr2,
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 08:40:55 +02:00
|
|
|
return fmt.Sprintf("(%s %s %s)",
|
2012-05-03 07:00:01 +02:00
|
|
|
expr1.Reduce(prefix), e.Op, expr2.Reduce(prefix))
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BinaryOp) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BinaryOp) Initialize(p *Protocol) {
|
|
|
|
e.Expr1.Initialize(p)
|
|
|
|
e.Expr2.Initialize(p)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// UnaryOp is the same as BinaryOp, except it's a unary operator with only
|
|
|
|
// one sub-expression.
|
2012-04-30 08:40:55 +02:00
|
|
|
type UnaryOp struct {
|
2012-04-30 08:44:31 +02:00
|
|
|
Op string
|
2012-04-30 08:40:55 +02:00
|
|
|
Expr Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnaryOp) Concrete() bool {
|
|
|
|
return e.Expr.Concrete()
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *UnaryOp) Eval() int {
|
2012-04-30 08:40:55 +02:00
|
|
|
switch e.Op {
|
|
|
|
case "~":
|
|
|
|
return ^e.Expr.Eval()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Fatalf("Invalid unary operator '%s' for expression.", e.Op)
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *UnaryOp) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
if e.Concrete() {
|
|
|
|
return fmt.Sprintf("%d", e.Eval())
|
|
|
|
}
|
2012-05-03 07:00:01 +02:00
|
|
|
return fmt.Sprintf("(%s (%s))", e.Op, e.Expr.Reduce(prefix))
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnaryOp) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnaryOp) Initialize(p *Protocol) {
|
|
|
|
e.Expr.Initialize(p)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Padding represents the application of the 'pad' function to some
|
|
|
|
// sub-expression.
|
2012-05-05 08:55:38 +02:00
|
|
|
type Padding struct {
|
|
|
|
Expr Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Padding) Concrete() bool {
|
|
|
|
return e.Expr.Concrete()
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *Padding) Eval() int {
|
|
|
|
return pad(e.Expr.Eval())
|
2012-05-05 08:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Padding) Reduce(prefix string) string {
|
|
|
|
if e.Concrete() {
|
|
|
|
return fmt.Sprintf("%d", e.Eval())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("pad(%s)", e.Expr.Reduce(prefix))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Padding) String() string {
|
|
|
|
return e.Reduce("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Padding) Initialize(p *Protocol) {
|
|
|
|
e.Expr.Initialize(p)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// PopCount represents the application of the 'PopCount' function to
|
|
|
|
// some sub-expression.
|
2012-04-30 08:40:55 +02:00
|
|
|
type PopCount struct {
|
|
|
|
Expr Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PopCount) Concrete() bool {
|
|
|
|
return e.Expr.Concrete()
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *PopCount) Eval() int {
|
|
|
|
return int(popCount(uint(e.Expr.Eval())))
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *PopCount) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
if e.Concrete() {
|
|
|
|
return fmt.Sprintf("%d", e.Eval())
|
|
|
|
}
|
2012-05-03 07:00:01 +02:00
|
|
|
return fmt.Sprintf("popCount(%s)", e.Expr.Reduce(prefix))
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PopCount) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PopCount) Initialize(p *Protocol) {
|
|
|
|
e.Expr.Initialize(p)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Value represents some constant integer.
|
2012-04-30 08:40:55 +02:00
|
|
|
type Value struct {
|
2012-05-06 09:06:02 +02:00
|
|
|
v int
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Value) Concrete() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *Value) Eval() int {
|
2012-04-30 08:40:55 +02:00
|
|
|
return e.v
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *Value) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
return fmt.Sprintf("%d", e.v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Value) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 08:44:31 +02:00
|
|
|
func (e *Value) Initialize(p *Protocol) {}
|
2012-04-30 08:40:55 +02:00
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Bit represents some bit whose value is computed by '1 << bit'.
|
2012-04-30 08:40:55 +02:00
|
|
|
type Bit struct {
|
2012-05-06 09:06:02 +02:00
|
|
|
b int
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Bit) Concrete() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *Bit) Eval() int {
|
|
|
|
return int(1 << uint(e.b))
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *Bit) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
return fmt.Sprintf("%d", e.Eval())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Bit) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 08:44:31 +02:00
|
|
|
func (e *Bit) Initialize(p *Protocol) {}
|
2012-04-30 08:40:55 +02:00
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// FieldRef represents a reference to some variable in the generated code
|
|
|
|
// with name Name.
|
2012-04-30 08:40:55 +02:00
|
|
|
type FieldRef struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FieldRef) Concrete() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *FieldRef) Eval() int {
|
2012-04-30 08:40:55 +02:00
|
|
|
log.Fatalf("Cannot evaluate a 'FieldRef'. It is not concrete.")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *FieldRef) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
val := e.Name
|
|
|
|
if len(prefix) > 0 {
|
|
|
|
val = fmt.Sprintf("%s%s", prefix, val)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FieldRef) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FieldRef) Initialize(p *Protocol) {
|
2012-05-06 00:21:48 +02:00
|
|
|
e.Name = SrcName(p, e.Name)
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// EnumRef represents a reference to some enumeration field.
|
|
|
|
// EnumKind is the "group" an EnumItem is the name of the specific enumeration
|
|
|
|
// value inside that group.
|
2012-04-30 08:40:55 +02:00
|
|
|
type EnumRef struct {
|
|
|
|
EnumKind Type
|
|
|
|
EnumItem string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EnumRef) Concrete() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *EnumRef) Eval() int {
|
2012-04-30 08:40:55 +02:00
|
|
|
log.Fatalf("Cannot evaluate an 'EnumRef'. It is not concrete.")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *EnumRef) Reduce(prefix string) string {
|
|
|
|
return fmt.Sprintf("%s%s", e.EnumKind, e.EnumItem)
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EnumRef) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EnumRef) Initialize(p *Protocol) {
|
|
|
|
e.EnumKind = e.EnumKind.(*Translation).RealType(p)
|
2012-05-06 00:21:48 +02:00
|
|
|
e.EnumItem = SrcName(p, e.EnumItem)
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// SumOf represents a summation of the variable in the generated code named by
|
|
|
|
// Name. It is not currently used. (It's XKB voodoo.)
|
2012-04-30 08:40:55 +02:00
|
|
|
type SumOf struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SumOf) Concrete() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-05-06 09:06:02 +02:00
|
|
|
func (e *SumOf) Eval() int {
|
2012-04-30 08:40:55 +02:00
|
|
|
log.Fatalf("Cannot evaluate a 'SumOf'. It is not concrete.")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (e *SumOf) Reduce(prefix string) string {
|
2012-04-30 08:40:55 +02:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
return fmt.Sprintf("sum(%s%s)", prefix, e.Name)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("sum(%s)", e.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SumOf) String() string {
|
2012-05-03 07:00:01 +02:00
|
|
|
return e.Reduce("")
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SumOf) Initialize(p *Protocol) {
|
2012-05-06 00:21:48 +02:00
|
|
|
e.Name = SrcName(p, e.Name)
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|