From 52a21b415ad95b2c4649254447388cb329cee1a4 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sat, 28 Apr 2012 23:25:57 -0400 Subject: initial commit. not currently in a working state. --- nexgb/xgbgen/context.go | 89 ++++++++++++ nexgb/xgbgen/go.go | 255 +++++++++++++++++++++++++++++++++++ nexgb/xgbgen/main.go | 64 +++++++++ nexgb/xgbgen/misc.go | 44 ++++++ nexgb/xgbgen/xgbgen | Bin 0 -> 2318165 bytes nexgb/xgbgen/xml.go | 298 +++++++++++++++++++++++++++++++++++++++++ nexgb/xgbgen/xml_expression.go | 160 ++++++++++++++++++++++ nexgb/xgbgen/xml_fields.go | 147 ++++++++++++++++++++ 8 files changed, 1057 insertions(+) create mode 100644 nexgb/xgbgen/context.go create mode 100644 nexgb/xgbgen/go.go create mode 100644 nexgb/xgbgen/main.go create mode 100644 nexgb/xgbgen/misc.go create mode 100755 nexgb/xgbgen/xgbgen create mode 100644 nexgb/xgbgen/xml.go create mode 100644 nexgb/xgbgen/xml_expression.go create mode 100644 nexgb/xgbgen/xml_fields.go (limited to 'nexgb/xgbgen') diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go new file mode 100644 index 0000000..e5acb12 --- /dev/null +++ b/nexgb/xgbgen/context.go @@ -0,0 +1,89 @@ +package main + +import ( + "bytes" + "encoding/xml" + "fmt" + "log" + "strings" +) + +type Context struct { + xml *XML + out *bytes.Buffer +} + +func newContext() *Context { + return &Context{ + xml: &XML{}, + out: bytes.NewBuffer([]byte{}), + } +} + +// Putln calls put and adds a new line to the end of 'format'. +func (c *Context) Putln(format string, v ...interface{}) { + c.Put(format + "\n", v...) +} + +// Put is a short alias to write to 'out'. +func (c *Context) Put(format string, v ...interface{}) { + _, err := c.out.WriteString(fmt.Sprintf(format, v...)) + if err != nil { + log.Fatalf("There was an error writing to context buffer: %s", err) + } +} + +// TypePrefix searches the parsed XML for a type matching 'needle'. +// It then returns the appropriate prefix to be used in source code. +// Note that the core X protocol *is* a namespace, but does not have a prefix. +// Also note that you should probably check the BaseTypeMap and TypeMap +// before calling this function. +func (c *Context) TypePrefix(needle Type) string { + // If this is xproto, quit. No prefixes needed. + if c.xml.Header == "xproto" { + return "" + } + + // First check for the type in the current namespace. + if c.xml.HasType(needle) { + return strings.Title(c.xml.Header) + } + + // Now check each of the imports... + for _, imp := range c.xml.Imports { + if imp.xml.Header != "xproto" && imp.xml.HasType(needle) { + return strings.Title(imp.xml.Header) + } + } + + return "" +} + +// Translate is the big daddy of them all. It takes in an XML byte slice +// and writes Go code to the 'out' buffer. +func (c *Context) Translate(xmlBytes []byte) { + err := xml.Unmarshal(xmlBytes, c.xml) + if err != nil { + log.Fatal(err) + } + + // Parse all imports + c.xml.Imports.Eval() + + // Make sure all top level enumerations have expressions + // (For when there are empty items.) + c.xml.Enums.Eval() + + // It's Morphin' Time! + c.xml.Morph(c) + + // for _, req := range c.xml.Requests { + // if req.Name != "CreateContext" && req.Name != "MakeCurrent" { + // continue + // } + // log.Println(req.Name) + // for _, field := range req.Fields { + // log.Println("\t", field.XMLName.Local, field.Type.Morph(c)) + // } + // } +} diff --git a/nexgb/xgbgen/go.go b/nexgb/xgbgen/go.go new file mode 100644 index 0000000..eb3f0fb --- /dev/null +++ b/nexgb/xgbgen/go.go @@ -0,0 +1,255 @@ +package main +/* + To the best of my ability, these are all of the Go specific formatting + functions. If I've designed xgbgen correctly, this should be only the + place that you change things to generate code for a new language. + + This file is organized as follows: + + * Imports and helper variables. + * Manual type and name override maps. + * Helper morphing functions. + * Morphing functions for each "unit". + * Morphing functions for collections of "units". +*/ + +import ( + "strings" +) + +/******************************************************************************/ +// Manual type and name overrides. +/******************************************************************************/ + +// BaseTypeMap is a map from X base types to Go types. +// X base types should correspond to the smallest set of X types +// that can be used to rewrite ALL X types in terms of Go types. +// That is, if you remove any of the following types, at least one +// XML protocol description will produce an invalid Go program. +// The types on the left *never* show themselves in the source. +var BaseTypeMap = map[string]string{ + "CARD8": "byte", + "CARD16": "uint16", + "CARD32": "uint32", + "INT8": "int8", + "INT16": "int16", + "INT32": "int32", + "BYTE": "byte", + "BOOL": "bool", + "float": "float64", + "double": "float64", +} + +// TypeMap is a map from types in the XML to type names that is used +// in the functions that follow. Basically, every occurrence of the key +// type is replaced with the value type. +var TypeMap = map[string]string{ + "VISUALTYPE": "VisualInfo", + "DEPTH": "DepthInfo", + "SCREEN": "ScreenInfo", + "Setup": "SetupInfo", +} + +// NameMap is the same as TypeMap, but for names. +var NameMap = map[string]string{ } + +/******************************************************************************/ +// Helper functions that aide in morphing repetive constructs. +// i.e., "structure contents", expressions, type and identifier names, etc. +/******************************************************************************/ + +// Morph changes every TYPE (not names) into something suitable +// for your language. +func (typ Type) Morph(c *Context) string { + t := string(typ) + + // If this is a base type, then write the raw Go type. + if newt, ok := BaseTypeMap[t]; ok { + return newt + } + + // If it's in the type map, use that translation. + if newt, ok := TypeMap[t]; ok { + return newt + } + + // If it's a resource type, just use 'Id'. + if c.xml.IsResource(typ) { + return "Id" + } + + // If there's a namespace to this type, just use it and be done. + if colon := strings.Index(t, ":"); colon > -1 { + namespace := t[:colon] + rest := t[colon+1:] + return splitAndTitle(namespace) + splitAndTitle(rest) + } + + // Since there is no namespace, we need to look for a namespace + // in the current context. + return c.TypePrefix(typ) + splitAndTitle(t) +} + +// Morph changes every identifier (NOT type) into something suitable +// for your language. +func (name Name) Morph(c *Context) string { + n := string(name) + + // If it's in the name map, use that translation. + if newn, ok := NameMap[n]; ok { + return newn + } + + return splitAndTitle(n) +} + +/******************************************************************************/ +// Per element morphing. +// Below are functions that morph a single unit. +/******************************************************************************/ + +// Import morphing. +func (imp *Import) Morph(c *Context) { + c.Putln("// import \"%s\"", imp.Name) +} + +// Enum morphing. +func (enum *Enum) Morph(c *Context) { + c.Putln("const (") + for _, item := range enum.Items { + c.Putln("%s%s = %d", enum.Name.Morph(c), item.Name.Morph(c), + item.Expr.Eval()) + } + c.Putln(")\n") +} + +// Xid morphing. +func (xid *Xid) Morph(c *Context) { + // Don't emit anything for xid types for now. + // We're going to force them all to simply be 'Id' + // to avoid excessive type converting. + // c.Putln("type %s Id", xid.Name.Morph(c)) +} + +// TypeDef morphing. +func (typedef *TypeDef) Morph(c *Context) { + c.Putln("type %s %s", typedef.Old.Morph(c), typedef.New.Morph(c)) +} + +// Struct morphing. +func (strct *Struct) Morph(c *Context) { +} + +// Union morphing. +func (union *Union) Morph(c *Context) { +} + +// Request morphing. +func (request *Request) Morph(c *Context) { +} + +// Event morphing. +func (ev *Event) Morph(c *Context) { +} + +// EventCopy morphing. +func (evcopy *EventCopy) Morph(c *Context) { +} + +// Error morphing. +func (err *Error) Morph(c *Context) { +} + +// ErrorCopy morphing. +func (errcopy *ErrorCopy) Morph(c *Context) { +} + +/******************************************************************************/ +// Collection morphing. +// Below are functions that morph a collections of units. +// Most of these can probably remain unchanged, but they are useful if you +// need to group all of some "unit" in a single block or something. +/******************************************************************************/ +func (imports Imports) Morph(c *Context) { + if len(imports) == 0 { + return + } + + c.Putln("// Imports are not required for XGB since everything is in") + c.Putln("// a single package. Still these may be useful for ") + c.Putln("// reference purposes.") + for _, imp := range imports { + imp.Morph(c) + } +} + +func (enums Enums) Morph(c *Context) { + c.Putln("// Enums\n") + for _, enum := range enums { + enum.Morph(c) + } +} + +func (xids Xids) Morph(c *Context) { + c.Putln("// Xids\n") + for _, xid := range xids { + xid.Morph(c) + } +} + +func (typedefs TypeDefs) Morph(c *Context) { + c.Putln("// TypeDefs\n") + for _, typedef := range typedefs { + typedef.Morph(c) + } +} + +func (strct Structs) Morph(c *Context) { + c.Putln("// Structs\n") + for _, typedef := range strct { + typedef.Morph(c) + } +} + +func (union Unions) Morph(c *Context) { + c.Putln("// Unions\n") + for _, typedef := range union { + typedef.Morph(c) + } +} + +func (request Requests) Morph(c *Context) { + c.Putln("// Requests\n") + for _, typedef := range request { + typedef.Morph(c) + } +} + +func (event Events) Morph(c *Context) { + c.Putln("// Events\n") + for _, typedef := range event { + typedef.Morph(c) + } +} + +func (evcopy EventCopies) Morph(c *Context) { + c.Putln("// Event Copies\n") + for _, typedef := range evcopy { + typedef.Morph(c) + } +} + +func (err Errors) Morph(c *Context) { + c.Putln("// Errors\n") + for _, typedef := range err { + typedef.Morph(c) + } +} + +func (errcopy ErrorCopies) Morph(c *Context) { + c.Putln("// Error copies\n") + for _, typedef := range errcopy { + typedef.Morph(c) + } +} + diff --git a/nexgb/xgbgen/main.go b/nexgb/xgbgen/main.go new file mode 100644 index 0000000..69579a4 --- /dev/null +++ b/nexgb/xgbgen/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "flag" + "io/ioutil" + "log" + "os" + "os/exec" + "strings" +) + +var ( + protoPath = flag.String("proto-path", + "/usr/share/xcb", "path to directory of X protocol XML files") + gofmt = flag.Bool("gofmt", true, + "When disabled, gofmt will not be run before outputting Go code") +) + +func usage() { + basename := os.Args[0] + if lastSlash := strings.LastIndex(basename, "/"); lastSlash > -1 { + basename = basename[lastSlash+1:] + } + log.Printf("Usage: %s [flags] xml-file", basename) + flag.PrintDefaults() + os.Exit(1) +} + +func init() { + log.SetFlags(0) +} + +func main() { + flag.Usage = usage + flag.Parse() + + if flag.NArg() != 1 { + log.Printf("A single XML protocol file can be processed at once.") + flag.Usage() + } + + // Read the single XML file into []byte + xmlBytes, err := ioutil.ReadFile(flag.Arg(0)) + if err != nil { + log.Fatal(err) + } + + // Initialize the buffer, parse it, and filter it through gofmt. + c := newContext() + c.Translate(xmlBytes) + + if !*gofmt { + c.out.WriteTo(os.Stdout) + } else { + cmdGofmt := exec.Command("gofmt") + cmdGofmt.Stdin = c.out + cmdGofmt.Stdout = os.Stdout + err = cmdGofmt.Run() + if err != nil { + log.Fatal(err) + } + } +} + diff --git a/nexgb/xgbgen/misc.go b/nexgb/xgbgen/misc.go new file mode 100644 index 0000000..9adcf5d --- /dev/null +++ b/nexgb/xgbgen/misc.go @@ -0,0 +1,44 @@ +package main + +import ( + "regexp" + "strings" +) + +// AllCaps is a regex to test if a string identifier is made of +// all upper case letters. +var AllCaps = regexp.MustCompile("^[A-Z0-9]+$") + +// popCount counts number of bits 'set' in mask. +func popCount(mask uint) uint { + m := uint32(mask) + n := uint(0) + for i := uint32(0); i < 32; i++ { + if m&(1< 31 { + log.Panicf("A 'bit' literal must be in the range [0, 31], but " + + " is %d", bit) + } + return 1 << uint(bit) + case "fieldref": + log.Panicf("Cannot compute concrete value of 'fieldref' in " + + "expression '%s'.", e) + case "enumref": + log.Panicf("Cannot compute concrete value of 'enumref' in " + + "expression '%s'.", e) + case "sumof": + log.Panicf("Cannot compute concrete value of 'sumof' in " + + "expression '%s'.", e) + } + + log.Panicf("Unrecognized tag '%s' in expression context. Expected one of " + + "op, fieldref, value, bit, enumref, unop, sumof or popcount.", + e.XMLName.Local) + panic("unreachable") +} + +func (e *Expression) BinaryOp(operand1, operand2 *Expression) *Expression { + if e.XMLName.Local != "op" { + log.Panicf("Cannot perform binary operation on non-op expression: %s", + e.XMLName.Local) + } + if len(e.Op) == 0 { + log.Panicf("Cannot perform binary operation without operator for: %s", + e.XMLName.Local) + } + + wrap := newValueExpression + switch e.Op { + case "+": + return wrap(operand1.Eval() + operand2.Eval()) + case "-": + return wrap(operand1.Eval() + operand2.Eval()) + case "*": + return wrap(operand1.Eval() * operand2.Eval()) + case "/": + return wrap(operand1.Eval() / operand2.Eval()) + case "&": + return wrap(operand1.Eval() & operand2.Eval()) + case "<<": + return wrap(operand1.Eval() << operand2.Eval()) + } + + log.Panicf("Invalid binary operator '%s' for '%s' expression.", + e.Op, e.XMLName.Local) + panic("unreachable") +} + +func (e *Expression) UnaryOp(operand *Expression) *Expression { + if e.XMLName.Local != "unop" { + log.Panicf("Cannot perform unary operation on non-unop expression: %s", + e.XMLName.Local) + } + if len(e.Op) == 0 { + log.Panicf("Cannot perform unary operation without operator for: %s", + e.XMLName.Local) + } + + switch e.Op { + case "~": + return newValueExpression(^operand.Eval()) + } + + log.Panicf("Invalid unary operator '%s' for '%s' expression.", + e.Op, e.XMLName.Local) + panic("unreachable") +} diff --git a/nexgb/xgbgen/xml_fields.go b/nexgb/xgbgen/xml_fields.go new file mode 100644 index 0000000..18be6e3 --- /dev/null +++ b/nexgb/xgbgen/xml_fields.go @@ -0,0 +1,147 @@ +package main +/* + A series of fields should be taken as "structure contents", and *not* + just the single 'field' elements. Namely, 'fields' subsumes 'field' + elements. + + More particularly, 'fields' corresponds to list, in order, of any of the + follow elements: pad, field, list, localfield, exprfield, valueparm + and switch. + + Thus, the 'Field' type must contain the union of information corresponding + to all aforementioned fields. + + This would ideally be a better job for interfaces, but I could not figure + out how to make them jive with Go's XML package. (And I don't really feel + up to type translation.) +*/ + +import ( + "encoding/xml" + "fmt" + "log" + "strings" +) + +type Field struct { + XMLName xml.Name + + // For 'pad' element + Bytes int `xml:"bytes,attr"` + + // For 'field', 'list', 'localfield', 'exprfield' and 'switch' elements. + Name string `xml:"name,attr"` + + // For 'field', 'list', 'localfield', and 'exprfield' elements. + Type Type `xml:"type,attr"` + + // For 'list', 'exprfield' and 'switch' elements. + Expr *Expression `xml:",any"` + + // For 'valueparm' element. + ValueMaskType Type `xml:"value-mask-type,attr"` + ValueMaskName string `xml:"value-mask-name,attr"` + ValueListName string `xml:"value-list-name,attr"` + + // For 'switch' element. + Bitcases []*Bitcase `xml:"bitcase"` + + // I don't know which elements these are for. The documentation is vague. + // They also seem to be completely optional. + OptEnum Type `xml:"enum,attr"` + OptMask Type `xml:"mask,attr"` + OptAltEnum Type `xml:"altenum,attr"` +} + +// String is for debugging purposes. +func (f *Field) String() string { + switch f.XMLName.Local { + case "pad": + return fmt.Sprintf("pad (%d bytes)", f.Bytes) + case "field": + return fmt.Sprintf("field (type = '%s', name = '%s')", f.Type, f.Name) + case "list": + return fmt.Sprintf("list (type = '%s', name = '%s', length = '%s')", + f.Type, f.Name, f.Expr) + case "localfield": + return fmt.Sprintf("localfield (type = '%s', name = '%s')", + f.Type, f.Name) + case "exprfield": + return fmt.Sprintf("exprfield (type = '%s', name = '%s', expr = '%s')", + f.Type, f.Name, f.Expr) + case "valueparam": + return fmt.Sprintf("valueparam (type = '%s', name = '%s', list = '%s')", + f.ValueMaskType, f.ValueMaskName, f.ValueListName) + case "switch": + bitcases := make([]string, len(f.Bitcases)) + for i, bitcase := range f.Bitcases { + bitcases[i] = bitcase.StringPrefix("\t") + } + return fmt.Sprintf("switch (name = '%s', expr = '%s')\n\t%s", + f.Name, f.Expr, strings.Join(bitcases, "\n\t")) + default: + log.Panicf("Unrecognized field element: %s", f.XMLName.Local) + } + + panic("unreachable") +} + +// Bitcase represents a single expression followed by any number of fields. +// Namely, if the switch's expression (all bitcases are inside a switch), +// and'd with the bitcase's expression is equal to the bitcase expression, +// then the fields should be included in its parent structure. +// Note that since a bitcase is unique in that expressions and fields are +// siblings, we must exhaustively search for one of them. Essentially, +// it's the closest thing to a Union I can get to in Go without interfaces. +// Would an '' tag have been too much to ask? :-( +type Bitcase struct { + Fields []*Field `xml:",any"` + + // All the different expressions. + // When it comes time to choose one, use the 'Expr' method. + ExprOp *Expression `xml:"op"` + ExprUnOp *Expression `xml:"unop"` + ExprField *Expression `xml:"fieldref"` + ExprValue *Expression `xml:"value"` + ExprBit *Expression `xml:"bit"` + ExprEnum *Expression `xml:"enumref"` + ExprSum *Expression `xml:"sumof"` + ExprPop *Expression `xml:"popcount"` +} + +// StringPrefix is for debugging purposes only. +// StringPrefix takes a string to prefix to every extra line for formatting. +func (b *Bitcase) StringPrefix(prefix string) string { + fields := make([]string, len(b.Fields)) + for i, field := range b.Fields { + fields[i] = fmt.Sprintf("%s%s", prefix, field) + } + return fmt.Sprintf("%s\n\t%s%s", b.Expr(), prefix, + strings.Join(fields, "\n\t")) +} + +// Expr chooses the only non-nil Expr* field from Bitcase. +// Panic if there is more than one non-nil expression. +func (b *Bitcase) Expr() *Expression { + choices := []*Expression{ + b.ExprOp, b.ExprUnOp, b.ExprField, b.ExprValue, + b.ExprBit, b.ExprEnum, b.ExprSum, b.ExprPop, + } + + var choice *Expression = nil + numNonNil := 0 + for _, c := range choices { + if c != nil { + numNonNil++ + choice = c + } + } + + if choice == nil { + log.Panicf("No top level expression found in a bitcase.") + } + if numNonNil > 1 { + log.Panicf("More than one top-level expression was found in a bitcase.") + } + return choice +} -- cgit v1.2.3-70-g09d2