Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 1 | # 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 | """ |
| 29 | Code generation |
| 30 | |
| 31 | These functions extract data from the IR and render templates with it. |
| 32 | """ |
| 33 | |
| 34 | from collections import namedtuple |
| 35 | from itertools import groupby |
Rich Lane | def2e51 | 2013-12-15 15:54:02 -0800 | [diff] [blame] | 36 | from StringIO import StringIO |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 37 | import template_utils |
| 38 | import loxi_globals |
| 39 | import loxi_ir.ir as ir |
| 40 | import util |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 41 | import c_code_gen |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 42 | import c_gen.of_g_legacy as of_g |
Rich Lane | cce961d | 2013-12-15 14:20:42 -0800 | [diff] [blame] | 43 | import c_gen.type_maps as type_maps |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 44 | import c_gen.c_type_maps as c_type_maps |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 45 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 46 | PushWireTypesData = namedtuple('PushWireTypesData', |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 47 | ['class_name', 'versioned_type_members']) |
| 48 | PushWireTypesMember = namedtuple('PushWireTypesMember', |
| 49 | ['name', 'offset', 'length', 'value']) |
| 50 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 51 | def push_wire_types_data(uclass): |
| 52 | if uclass.virtual or not uclass.has_type_members: |
| 53 | return None |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 54 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 55 | # Generate a dict of version -> list of PushWireTypesMember |
| 56 | type_members_by_version = {} |
| 57 | for version, ofclass in sorted(uclass.version_classes.items()): |
| 58 | pwtms = [] |
| 59 | for m in ofclass.members: |
| 60 | if isinstance(m, ir.OFTypeMember): |
| 61 | if m.name == "version" and m.value == version.wire_version: |
| 62 | # Special case for version |
| 63 | pwtms.append(PushWireTypesMember(m.name, m.offset, m.length, "obj->version")) |
| 64 | else: |
Rich Lane | d0c5871 | 2013-12-18 10:02:47 -0800 | [diff] [blame] | 65 | pwtms.append(PushWireTypesMember(m.name, m.offset, m.length, hex(m.value))) |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 66 | type_members_by_version[version] = pwtms |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 67 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 68 | # Merge versions with identical type members |
| 69 | all_versions = sorted(type_members_by_version.keys()) |
| 70 | versioned_type_members = [] |
| 71 | for pwtms, versions in groupby(all_versions, type_members_by_version.get): |
| 72 | versioned_type_members.append((pwtms, list(versions))) |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 73 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 74 | return PushWireTypesData( |
| 75 | class_name=uclass.name, |
| 76 | versioned_type_members=versioned_type_members) |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 77 | |
| 78 | def generate_classes(install_dir): |
| 79 | for uclass in loxi_globals.unified.classes: |
| 80 | with template_utils.open_output(install_dir, "loci/src/%s.c" % uclass.name) as out: |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 81 | util.render_template(out, "class.c", |
| 82 | push_wire_types_data=push_wire_types_data(uclass)) |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 83 | # Append legacy generated code |
Rich Lane | b604e33 | 2013-12-15 13:23:51 -0800 | [diff] [blame] | 84 | c_code_gen.gen_new_function_definitions(out, uclass.name) |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 85 | c_code_gen.gen_accessor_definitions(out, uclass.name) |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 86 | |
Rich Lane | 573d2b2 | 2013-12-15 13:31:27 -0800 | [diff] [blame] | 87 | # TODO remove header classes and use the corresponding class instead |
| 88 | def generate_header_classes(install_dir): |
| 89 | for cls in of_g.standard_class_order: |
| 90 | if cls.find("_header") < 0: |
| 91 | continue |
| 92 | with template_utils.open_output(install_dir, "loci/src/%s.c" % cls) as out: |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 93 | util.render_template(out, "class.c", |
| 94 | push_wire_types_data=None) |
Rich Lane | 573d2b2 | 2013-12-15 13:31:27 -0800 | [diff] [blame] | 95 | # Append legacy generated code |
| 96 | c_code_gen.gen_new_function_definitions(out, cls) |
| 97 | c_code_gen.gen_accessor_definitions(out, cls) |
| 98 | |
Rich Lane | def2e51 | 2013-12-15 15:54:02 -0800 | [diff] [blame] | 99 | def generate_classes_header(install_dir): |
| 100 | # Collect legacy code |
| 101 | tmp = StringIO() |
| 102 | c_code_gen.gen_struct_typedefs(tmp) |
| 103 | c_code_gen.gen_new_function_declarations(tmp) |
| 104 | c_code_gen.gen_accessor_declarations(tmp) |
Rich Lane | def2e51 | 2013-12-15 15:54:02 -0800 | [diff] [blame] | 105 | c_code_gen.gen_generics(tmp) |
| 106 | |
| 107 | with template_utils.open_output(install_dir, "loci/inc/loci/loci_classes.h") as out: |
| 108 | util.render_template(out, "loci_classes.h", |
| 109 | legacy_code=tmp.getvalue()) |
| 110 | |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 111 | def generate_lists(install_dir): |
| 112 | for cls in of_g.ordered_list_objects: |
| 113 | with template_utils.open_output(install_dir, "loci/src/%s.c" % cls) as out: |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 114 | util.render_template(out, "class.c", |
| 115 | push_wire_types_data=None) |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 116 | # Append legacy generated code |
Rich Lane | b604e33 | 2013-12-15 13:23:51 -0800 | [diff] [blame] | 117 | c_code_gen.gen_new_function_definitions(out, cls) |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 118 | c_code_gen.gen_list_accessors(out, cls) |
Rich Lane | 8a82273 | 2013-12-15 14:06:32 -0800 | [diff] [blame] | 119 | |
| 120 | def generate_strings(install_dir): |
| 121 | object_id_strs = [] |
| 122 | object_id_strs.append("of_object") |
| 123 | object_id_strs.extend(of_g.ordered_messages) |
| 124 | object_id_strs.extend(of_g.ordered_non_messages) |
| 125 | object_id_strs.extend(of_g.ordered_list_objects) |
| 126 | object_id_strs.extend(of_g.ordered_pseudo_objects) |
| 127 | object_id_strs.append("of_unknown_object") |
| 128 | |
| 129 | with template_utils.open_output(install_dir, "loci/src/loci_strings.c") as out: |
| 130 | util.render_template(out, "loci_strings.c", object_id_strs=object_id_strs) |
Rich Lane | cce961d | 2013-12-15 14:20:42 -0800 | [diff] [blame] | 131 | |
| 132 | def generate_init_map(install_dir): |
| 133 | with template_utils.open_output(install_dir, "loci/src/loci_init_map.c") as out: |
| 134 | util.render_template(out, "loci_init_map.c", classes=of_g.standard_class_order) |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 135 | |
| 136 | def generate_type_maps(install_dir): |
| 137 | # Collect legacy code |
| 138 | tmp = StringIO() |
| 139 | c_type_maps.gen_type_to_obj_map_functions(tmp) |
| 140 | c_type_maps.gen_type_maps(tmp) |
| 141 | c_type_maps.gen_length_array(tmp) |
| 142 | c_type_maps.gen_extra_length_array(tmp) |
| 143 | |
| 144 | with template_utils.open_output(install_dir, "loci/src/of_type_maps.c") as out: |
| 145 | util.render_template(out, "of_type_maps.c", legacy_code=tmp.getvalue()) |