blob: 0d7832aa86f84089217a5bd2826cd229c52d8275 [file] [log] [blame]
Rich Lanebdd8e292013-12-06 17:37:39 -08001# Copyright 2013, Big Switch Networks, Inc.
2#
3# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4# the following special exception:
5#
6# LOXI Exception
7#
8# As a special exception to the terms of the EPL, you may distribute libraries
9# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10# that copyright and licensing notices generated by LoxiGen are not altered or removed
11# from the LoxiGen Libraries and the notice provided below is (i) included in
12# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13# documentation for the LoxiGen Libraries, if distributed in binary form.
14#
15# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16#
17# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18# a copy of the EPL at:
19#
20# http://www.eclipse.org/legal/epl-v10.html
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25# EPL for the specific language governing permissions and limitations
26# under the EPL.
27
28"""
29Code generation
30
31These functions extract data from the IR and render templates with it.
32"""
33
34from collections import namedtuple
35from itertools import groupby
36import template_utils
37import loxi_globals
38import loxi_ir.ir as ir
39import util
40
41PushWireTypesFn = namedtuple('PushWireTypesFn',
42 ['class_name', 'versioned_type_members'])
43PushWireTypesMember = namedtuple('PushWireTypesMember',
44 ['name', 'offset', 'length', 'value'])
45
46def gen_push_wire_types(install_dir):
47 fns = []
48 for uclass in loxi_globals.unified.classes:
49 if uclass.virtual or not uclass.has_type_members:
50 continue
51
52 # Generate a dict of version -> list of PushWireTypesMember
53 type_members_by_version = {}
54 for version, ofclass in sorted(uclass.version_classes.items()):
55 pwtms = []
56 for m in ofclass.members:
57 if isinstance(m, ir.OFTypeMember):
58 if m.name == "version" and m.value == version.wire_version:
59 # Special case for version
60 pwtms.append(PushWireTypesMember(m.name, m.offset, m.length, "obj->version"))
61 else:
62 pwtms.append(PushWireTypesMember(m.name, m.offset, m.length, m.value))
63 type_members_by_version[version] = pwtms
64
65 # Merge versions with identical type members
66 all_versions = sorted(type_members_by_version.keys())
67 versioned_type_members = []
68 for pwtms, versions in groupby(all_versions, type_members_by_version.get):
69 versioned_type_members.append((pwtms, list(versions)))
70
71 fns.append(PushWireTypesFn(
72 class_name=uclass.name,
73 versioned_type_members=versioned_type_members))
74
75 with template_utils.open_output(install_dir, "loci/src/loci_push_wire_types.c") as out:
76 util.render_template(out, "loci_push_wire_types.c", fns=fns)
77
78 with template_utils.open_output(install_dir, "loci/src/loci_push_wire_types.h") as out:
79 util.render_template(out, "loci_push_wire_types.h", fns=fns)