frontend: generate intermediate representation

This is converted to the legacy format by read_input.
diff --git a/loxi_front_end/frontend.py b/loxi_front_end/frontend.py
index 6c7a153..209b31c 100644
--- a/loxi_front_end/frontend.py
+++ b/loxi_front_end/frontend.py
@@ -25,12 +25,26 @@
 # EPL for the specific language governing permissions and limitations
 # under the EPL.
 
+import copy
 import of_g
 import loxi_front_end.type_maps as type_maps
+from loxi_ir import *
 
 class InputError(Exception):
     pass
 
+# TODO handle type members
+def create_member(m_ast):
+    if m_ast[0] == 'pad':
+        return OFPadMember(length=m_ast[1])
+    elif m_ast[1] == 'length' or m_ast[1] == 'len': # Should be moved to parser
+        return OFLengthMember(name=m_ast[1], oftype=m_ast[0])
+    elif m_ast[1] == 'actions_len':
+        # HACK only usage so far
+        return OFFieldLengthMember(name=m_ast[1], oftype=m_ast[0], field_name='actions')
+    else:
+        return OFDataMember(name=m_ast[1], oftype=m_ast[0])
+
 def create_ofinput(ast):
     """
     Create an OFInput from an AST
@@ -40,42 +54,30 @@
     @returns An OFInput object
     """
 
-    ofinput = of_g.OFInput()
+    ofinput = OFInput(wire_versions=set(), classes=[], enums=[])
 
-    # Now for each structure, generate lists for each member
-    for s in ast:
-        if s[0] == 'struct':
-            name = s[1]
-            pad_count = 0
-            members = []
-            for x in s[2]:
-                if x[0] == 'pad':
-                    # HACK until we have a real intermediate representation
-                    m_name = 'pad%d' % pad_count
-                    if m_name == 'pad0': m_name = 'pad'
-                    members.append(dict(m_type='uint8_t[%d]' % x[1],
-                                        name=m_name))
-                    pad_count += 1
-                else:
-                    members.append(dict(m_type=x[0], name=x[1]))
-            ofinput.classes[name] = members
-            ofinput.ordered_classes.append(name)
-            if name in type_maps.inheritance_map:
+    for decl_ast in ast:
+        if decl_ast[0] == 'struct':
+            members = [create_member(m_ast) for m_ast in decl_ast[2]]
+            ofclass = OFClass(name=decl_ast[1], members=members)
+            ofinput.classes.append(ofclass)
+            if ofclass.name in type_maps.inheritance_map:
                 # Clone class into header class and add to list
-                ofinput.classes[name + "_header"] = members[:]
-                ofinput.ordered_classes.append(name + "_header")
-        if s[0] == 'enum':
-            name = s[1]
-            members = s[2]
-            ofinput.enums[name] = [(x[0], x[1]) for x in members]
-        elif s[0] == 'metadata':
-            if s[1] == 'version':
-                if s[2] == 'any':
+                # TODO figure out if these are actually used
+                ofclass_header = OFClass(ofclass.name + '_header',
+                                         copy.deepcopy(members))
+                ofinput.classes.append(ofclass_header)
+        if decl_ast[0] == 'enum':
+            enum = OFEnum(name=decl_ast[1], values=[(x[0], x[1]) for x in decl_ast[2]])
+            ofinput.enums.append(enum)
+        elif decl_ast[0] == 'metadata':
+            if decl_ast[1] == 'version':
+                if decl_ast[2] == 'any':
                     ofinput.wire_versions.update(of_g.wire_ver_map.keys())
-                elif int(s[2]) in of_g.supported_wire_protos:
-                    ofinput.wire_versions.add(int(s[2]))
+                elif int(decl_ast[2]) in of_g.supported_wire_protos:
+                    ofinput.wire_versions.add(int(decl_ast[2]))
                 else:
-                    raise InputError("Unrecognized wire protocol version %s" % repr(s[2]))
+                    raise InputError("Unrecognized wire protocol version %r" % decl_ast[2])
                 found_wire_version = True
 
     if not ofinput.wire_versions:
diff --git a/loxi_ir.py b/loxi_ir.py
new file mode 100644
index 0000000..9a27272
--- /dev/null
+++ b/loxi_ir.py
@@ -0,0 +1,136 @@
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+from collections import namedtuple
+
+# This module is intended to be imported like this: from loxi_ir import *
+# All public names are prefixed with 'OF'.
+__all__ = [
+    'OFInput',
+    'OFProtocol',
+    'OFClass',
+    'OFDataMember',
+    'OFTypeMember',
+    'OFLengthMember',
+    'OFFieldLengthMember',
+    'OFPadMember',
+    'OFEnum',
+]
+
+"""
+One input file
+
+@param wire_versions Set of integer wire versions this file applies to
+@param classes List of OFClass objects in the same order as in the file
+@param enums List of Enum objects in the same order as in the file
+"""
+OFInput = namedtuple('OFInput', ['wire_versions', 'classes', 'enums'])
+
+"""
+One version of the OpenFlow protocol
+
+Combination of multiple OFInput objects.
+
+@param wire_version
+@param classes List of OFClass objects
+@param enums List of Enum objects
+"""
+OFProtocol = namedtuple('OFProtocol', ['wire_version', 'classes', 'enums'])
+
+"""
+An OpenFlow class
+
+All compound objects like messages, actions, instructions, etc are
+uniformly represented by this class.
+
+The members are in the same order as on the wire.
+
+@param name
+@param members List of *Member objects
+"""
+OFClass = namedtuple('OFClass', ['name', 'members'])
+
+"""
+Normal field
+
+@param name
+@param oftype C-like type string
+
+Example: packet_in.buffer_id
+"""
+OFDataMember = namedtuple('OFDataMember', ['name', 'oftype'])
+
+"""
+Field used to determine the type of an OpenFlow object
+
+@param name
+@param oftype C-like type string
+@param value Fixed type value
+
+Example: packet_in.type, flow_add._command
+"""
+OFTypeMember = namedtuple('OFTypeMember', ['name', 'oftype', 'value'])
+
+"""
+Field with the length of the containing object
+
+@param name
+@param oftype C-like type string
+
+Example: packet_in.length, action_output.len
+"""
+OFLengthMember = namedtuple('OFLengthMember', ['name', 'oftype'])
+
+"""
+Field with the length of another field in the containing object
+
+@param name
+@param oftype C-like type string
+@param field_name Peer field whose length this field contains
+
+Example: packet_out.actions_len (only usage)
+"""
+OFFieldLengthMember = namedtuple('OFFieldLengthMember', ['name', 'oftype', 'field_name'])
+
+"""
+Zero-filled padding
+
+@param length Length in bytes
+
+Example: packet_in.pad
+"""
+OFPadMember = namedtuple('OFPadMember', ['length'])
+
+"""
+An OpenFlow enumeration
+
+All values are Python ints.
+
+@param name
+@param values List of (name, value) tuples in input order
+"""
+OFEnum = namedtuple('OFEnum', ['name', 'values'])
diff --git a/loxigen.py b/loxigen.py
index 2096381..79e6da8 100755
--- a/loxigen.py
+++ b/loxigen.py
@@ -86,7 +86,7 @@
 import loxi_front_end.parser as parser
 import loxi_front_end.translation as translation
 import loxi_front_end.frontend as frontend
-
+from loxi_ir import *
 from generic_utils import *
 
 root_dir = os.path.dirname(os.path.realpath(__file__))
@@ -424,14 +424,27 @@
         # Populate global state
         for wire_version in ofinput.wire_versions:
             version_name = of_g.of_version_wire2name[wire_version]
-            versions[version_name]['classes'].update(copy.deepcopy(ofinput.classes))
-            of_g.ordered_classes[wire_version].extend(ofinput.ordered_classes)
 
-            for enum_name, members in ofinput.enums.items():
-                for member_name, value in members:
+            for ofclass in ofinput.classes:
+                of_g.ordered_classes[wire_version].append(ofclass.name)
+                legacy_members = []
+                pad_count = 0
+                for m in ofclass.members:
+                    if type(m) == OFPadMember:
+                        m_name = 'pad%d' % pad_count
+                        if m_name == 'pad0': m_name = 'pad'
+                        legacy_members.append(dict(m_type='uint8_t[%d]' % m.length,
+                                                   name=m_name))
+                        pad_count += 1
+                    else:
+                        legacy_members.append(dict(m_type=m.oftype, name=m.name))
+                versions[version_name]['classes'][ofclass.name] = legacy_members
+
+            for enum in ofinput.enums:
+                for name, value in enum.values:
                     identifiers.add_identifier(
-                        translation.loxi_name(member_name),
-                        member_name, enum_name, value, wire_version,
+                        translation.loxi_name(name),
+                        name, enum.name, value, wire_version,
                         of_g.identifiers, of_g.identifiers_by_group)
 
 def add_extra_classes():
diff --git a/of_g.py b/of_g.py
index 046273c..9f44a2b 100644
--- a/of_g.py
+++ b/of_g.py
@@ -506,20 +506,3 @@
 
 loxigen_dbg_file = sys.stdout
 loxigen_log_file = sys.stdout
-
-################################################################
-#
-# Internal representation
-#
-################################################################
-
-class OFInput(object):
-    """
-    A single LOXI input file.
-    """
-
-    def __init__(self):
-        self.wire_versions = set()
-        self.classes = {}
-        self.ordered_classes = []
-        self.enums = {}