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 |
Rich Lane | f51062b | 2014-02-24 23:08:26 -0800 | [diff] [blame] | 38 | from generic_utils import chunks |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 39 | import loxi_globals |
| 40 | import loxi_ir.ir as ir |
| 41 | import util |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 42 | import c_code_gen |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 43 | import c_gen.of_g_legacy as of_g |
Rich Lane | cce961d | 2013-12-15 14:20:42 -0800 | [diff] [blame] | 44 | import c_gen.type_maps as type_maps |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 45 | import c_gen.c_type_maps as c_type_maps |
Rich Lane | 2cc2b86 | 2014-06-13 14:50:17 -0700 | [diff] [blame] | 46 | import loxi_utils.loxi_utils as loxi_utils |
| 47 | import c_gen.loxi_utils_legacy as loxi_utils_legacy |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 48 | |
Rich Lane | f51062b | 2014-02-24 23:08:26 -0800 | [diff] [blame] | 49 | CLASS_CHUNK_SIZE = 32 |
| 50 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 51 | PushWireTypesData = namedtuple('PushWireTypesData', |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 52 | ['class_name', 'versioned_type_members']) |
| 53 | PushWireTypesMember = namedtuple('PushWireTypesMember', |
| 54 | ['name', 'offset', 'length', 'value']) |
| 55 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 56 | def push_wire_types_data(uclass): |
| 57 | if uclass.virtual or not uclass.has_type_members: |
| 58 | return None |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 59 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 60 | # Generate a dict of version -> list of PushWireTypesMember |
| 61 | type_members_by_version = {} |
| 62 | for version, ofclass in sorted(uclass.version_classes.items()): |
| 63 | pwtms = [] |
| 64 | for m in ofclass.members: |
| 65 | if isinstance(m, ir.OFTypeMember): |
| 66 | if m.name == "version" and m.value == version.wire_version: |
| 67 | # Special case for version |
| 68 | pwtms.append(PushWireTypesMember(m.name, m.offset, m.length, "obj->version")) |
| 69 | else: |
Rich Lane | d0c5871 | 2013-12-18 10:02:47 -0800 | [diff] [blame] | 70 | pwtms.append(PushWireTypesMember(m.name, m.offset, m.length, hex(m.value))) |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 71 | type_members_by_version[version] = pwtms |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 72 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 73 | # Merge versions with identical type members |
| 74 | all_versions = sorted(type_members_by_version.keys()) |
| 75 | versioned_type_members = [] |
| 76 | for pwtms, versions in groupby(all_versions, type_members_by_version.get): |
| 77 | versioned_type_members.append((pwtms, list(versions))) |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 78 | |
Rich Lane | 22811f5 | 2013-12-15 15:28:03 -0800 | [diff] [blame] | 79 | return PushWireTypesData( |
| 80 | class_name=uclass.name, |
| 81 | versioned_type_members=versioned_type_members) |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 82 | |
Rich Lane | b9fab4b | 2014-03-04 18:44:08 -0800 | [diff] [blame] | 83 | ParseWireTypesData = namedtuple('ParseWireTypesData', |
| 84 | ['class_name', 'versioned']) |
| 85 | ParseWireTypesVersion = namedtuple('ParseWireTypesVersion', |
| 86 | ['discriminator', 'subclasses']) |
| 87 | ParseWireTypesSubclass = namedtuple('ParseWireTypesSubclass', |
| 88 | ['class_name', 'value', 'virtual']) |
| 89 | |
| 90 | def parse_wire_types_data(uclass): |
| 91 | if not uclass.virtual: |
| 92 | return None |
| 93 | |
Rich Lane | b9fab4b | 2014-03-04 18:44:08 -0800 | [diff] [blame] | 94 | # Generate a dict of version -> ParseWireTypesVersion |
| 95 | versioned = {} |
| 96 | for version, ofclass in sorted(uclass.version_classes.items()): |
Rich Lane | a4b6830 | 2014-03-12 15:17:58 -0700 | [diff] [blame] | 97 | discriminator = ofclass.discriminator |
Rich Lane | b9fab4b | 2014-03-04 18:44:08 -0800 | [diff] [blame] | 98 | subclasses = [ParseWireTypesSubclass(class_name=subclass.name, |
| 99 | value=subclass.member_by_name(discriminator.name).value, |
| 100 | virtual=subclass.virtual) |
| 101 | for subclass in ofclass.protocol.classes if subclass.superclass and subclass.superclass.name == ofclass.name] |
| 102 | |
| 103 | subclasses.sort(key=lambda x: x.value) |
| 104 | versioned[version] = ParseWireTypesVersion(discriminator=discriminator, |
| 105 | subclasses=subclasses) |
| 106 | |
| 107 | return ParseWireTypesData(class_name=uclass.name, |
| 108 | versioned=sorted(versioned.items())) |
| 109 | |
Rich Lane | f51062b | 2014-02-24 23:08:26 -0800 | [diff] [blame] | 110 | # Output multiple LOCI classes into each C file. This reduces the overhead of |
| 111 | # parsing header files, which takes longer than compiling the actual code |
| 112 | # for many classes. It also reduces the compiled code size. |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 113 | def generate_classes(install_dir): |
Rich Lane | f51062b | 2014-02-24 23:08:26 -0800 | [diff] [blame] | 114 | for i, chunk in enumerate(chunks(loxi_globals.unified.classes, CLASS_CHUNK_SIZE)): |
| 115 | with template_utils.open_output(install_dir, "loci/src/class%02d.c" % i) as out: |
| 116 | for uclass in chunk: |
| 117 | util.render_template(out, "class.c", |
Rich Lane | b9fab4b | 2014-03-04 18:44:08 -0800 | [diff] [blame] | 118 | push_wire_types_data=push_wire_types_data(uclass), |
| 119 | parse_wire_types_data=parse_wire_types_data(uclass)) |
Rich Lane | f51062b | 2014-02-24 23:08:26 -0800 | [diff] [blame] | 120 | # Append legacy generated code |
| 121 | c_code_gen.gen_new_function_definitions(out, uclass.name) |
| 122 | c_code_gen.gen_accessor_definitions(out, uclass.name) |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 123 | |
Rich Lane | 573d2b2 | 2013-12-15 13:31:27 -0800 | [diff] [blame] | 124 | # TODO remove header classes and use the corresponding class instead |
| 125 | def generate_header_classes(install_dir): |
| 126 | for cls in of_g.standard_class_order: |
Harshmeet Singh | 19d566b | 2014-10-06 14:08:06 -0700 | [diff] [blame] | 127 | if not cls.endswith("_header") or cls in ["of_header", "of_bsn_header", "of_nicira_header"]: |
Rich Lane | 573d2b2 | 2013-12-15 13:31:27 -0800 | [diff] [blame] | 128 | continue |
| 129 | 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] | 130 | util.render_template(out, "class.c", |
Rich Lane | b9fab4b | 2014-03-04 18:44:08 -0800 | [diff] [blame] | 131 | push_wire_types_data=None, |
| 132 | parse_wire_types_data=None) |
Rich Lane | 573d2b2 | 2013-12-15 13:31:27 -0800 | [diff] [blame] | 133 | # Append legacy generated code |
| 134 | c_code_gen.gen_new_function_definitions(out, cls) |
| 135 | c_code_gen.gen_accessor_definitions(out, cls) |
| 136 | |
Rich Lane | def2e51 | 2013-12-15 15:54:02 -0800 | [diff] [blame] | 137 | def generate_classes_header(install_dir): |
| 138 | # Collect legacy code |
| 139 | tmp = StringIO() |
| 140 | c_code_gen.gen_struct_typedefs(tmp) |
| 141 | c_code_gen.gen_new_function_declarations(tmp) |
| 142 | c_code_gen.gen_accessor_declarations(tmp) |
Rich Lane | def2e51 | 2013-12-15 15:54:02 -0800 | [diff] [blame] | 143 | c_code_gen.gen_generics(tmp) |
| 144 | |
| 145 | with template_utils.open_output(install_dir, "loci/inc/loci/loci_classes.h") as out: |
| 146 | util.render_template(out, "loci_classes.h", |
| 147 | legacy_code=tmp.getvalue()) |
| 148 | |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 149 | def generate_lists(install_dir): |
Rich Lane | 2cc2b86 | 2014-06-13 14:50:17 -0700 | [diff] [blame] | 150 | # Collect all the lists in use |
| 151 | list_oftypes = set() |
| 152 | for uclass in loxi_globals.unified.classes: |
Rich Lane | deb9675 | 2014-06-25 22:27:51 -0700 | [diff] [blame] | 153 | for ofclass in uclass.version_classes.values(): |
Rich Lane | 2cc2b86 | 2014-06-13 14:50:17 -0700 | [diff] [blame] | 154 | for m in ofclass.members: |
Rich Lane | deb9675 | 2014-06-25 22:27:51 -0700 | [diff] [blame] | 155 | if isinstance(m, ir.OFDataMember) and \ |
| 156 | loxi_utils.oftype_is_list(m.oftype): |
| 157 | list_oftypes.add(m.oftype) |
Rich Lane | 2cc2b86 | 2014-06-13 14:50:17 -0700 | [diff] [blame] | 158 | |
| 159 | for oftype in sorted(list(list_oftypes)): |
| 160 | cls, e_cls = loxi_utils_legacy.list_name_extract(oftype) |
| 161 | e_cls = e_cls[:-2] |
Rich Lane | 83f958d | 2014-06-13 15:01:40 -0700 | [diff] [blame] | 162 | e_uclass = loxi_globals.unified.class_by_name(e_cls) |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 163 | with template_utils.open_output(install_dir, "loci/src/%s.c" % cls) as out: |
Rich Lane | 83f958d | 2014-06-13 15:01:40 -0700 | [diff] [blame] | 164 | util.render_template(out, "list.c", cls=cls, e_cls=e_cls, e_uclass=e_uclass, |
Rich Lane | bbf5e1d | 2014-06-13 15:34:21 -0700 | [diff] [blame] | 165 | wire_length_get=class_metadata_dict[e_cls].wire_length_get) |
Rich Lane | 8c4c23f | 2013-12-15 13:22:13 -0800 | [diff] [blame] | 166 | # Append legacy generated code |
Rich Lane | b604e33 | 2013-12-15 13:23:51 -0800 | [diff] [blame] | 167 | c_code_gen.gen_new_function_definitions(out, cls) |
Rich Lane | 8a82273 | 2013-12-15 14:06:32 -0800 | [diff] [blame] | 168 | |
| 169 | def generate_strings(install_dir): |
| 170 | object_id_strs = [] |
| 171 | object_id_strs.append("of_object") |
| 172 | object_id_strs.extend(of_g.ordered_messages) |
| 173 | object_id_strs.extend(of_g.ordered_non_messages) |
| 174 | object_id_strs.extend(of_g.ordered_list_objects) |
| 175 | object_id_strs.extend(of_g.ordered_pseudo_objects) |
| 176 | object_id_strs.append("of_unknown_object") |
| 177 | |
| 178 | with template_utils.open_output(install_dir, "loci/src/loci_strings.c") as out: |
| 179 | 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] | 180 | |
| 181 | def generate_init_map(install_dir): |
| 182 | with template_utils.open_output(install_dir, "loci/src/loci_init_map.c") as out: |
| 183 | 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] | 184 | |
| 185 | def generate_type_maps(install_dir): |
| 186 | # Collect legacy code |
| 187 | tmp = StringIO() |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 188 | c_type_maps.gen_length_array(tmp) |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 189 | |
| 190 | with template_utils.open_output(install_dir, "loci/src/of_type_maps.c") as out: |
| 191 | util.render_template(out, "of_type_maps.c", legacy_code=tmp.getvalue()) |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 192 | |
| 193 | ClassMetadata = namedtuple('ClassMetadata', |
| 194 | ['name', 'wire_length_get', 'wire_length_set', 'wire_type_get', 'wire_type_set']) |
| 195 | |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 196 | class_metadata = [] |
| 197 | class_metadata_dict = {} |
| 198 | |
Rich Lane | 127860e | 2014-10-14 11:03:49 -0700 | [diff] [blame] | 199 | # These classes have handwritten C code to get/set their length fields |
| 200 | # See templates/of_type_maps.c |
| 201 | special_length_classes = set([ |
| 202 | 'of_packet_queue', 'of_meter_stats', 'of_port_desc', |
| 203 | 'of_port_stats_entry', 'of_queue_stats_entry', |
Rich Lane | 7329b54 | 2014-10-17 18:30:13 -0700 | [diff] [blame] | 204 | 'of_queue_desc', |
Rich Lane | 127860e | 2014-10-14 11:03:49 -0700 | [diff] [blame] | 205 | ]) |
| 206 | |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 207 | def build_class_metadata(): |
| 208 | for uclass in loxi_globals.unified.classes: |
| 209 | wire_length_get = 'NULL' |
| 210 | wire_length_set = 'NULL' |
| 211 | wire_type_get = 'NULL' |
| 212 | wire_type_set = 'NULL' |
| 213 | |
| 214 | if uclass and not uclass.virtual and uclass.has_type_members: |
| 215 | wire_type_set = '%s_push_wire_types' % uclass.name |
| 216 | |
Rich Lane | 068847f | 2014-10-13 16:26:33 -0700 | [diff] [blame] | 217 | root = uclass.inheritance_root() |
| 218 | if root and root.name != 'of_header': |
| 219 | wire_type_get = root.name + '_wire_object_id_get' |
| 220 | |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 221 | if uclass.is_message: |
| 222 | wire_length_get = 'of_object_message_wire_length_get' |
| 223 | wire_length_set = 'of_object_message_wire_length_set' |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 224 | elif uclass.is_oxm: |
| 225 | wire_length_get = 'of_oxm_wire_length_get' |
Rich Lane | 127860e | 2014-10-14 11:03:49 -0700 | [diff] [blame] | 226 | elif uclass.name in special_length_classes: |
| 227 | wire_length_get = '%s_wire_length_get' % uclass.name |
| 228 | wire_length_set = '%s_wire_length_set' % uclass.name |
Rich Lane | 068847f | 2014-10-13 16:26:33 -0700 | [diff] [blame] | 229 | elif loxi_utils_legacy.class_is_tlv16(uclass.name): |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 230 | wire_length_set = 'of_tlv16_wire_length_set' |
| 231 | wire_length_get = 'of_tlv16_wire_length_get' |
Rich Lane | 068847f | 2014-10-13 16:26:33 -0700 | [diff] [blame] | 232 | elif loxi_utils_legacy.class_is_u16_len(uclass.name): |
| 233 | wire_length_get = "of_u16_len_wire_length_get" |
| 234 | wire_length_set = "of_u16_len_wire_length_set" |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 235 | |
| 236 | class_metadata.append(ClassMetadata( |
| 237 | name=uclass.name, |
| 238 | wire_length_get=wire_length_get, |
| 239 | wire_length_set=wire_length_set, |
| 240 | wire_type_get=wire_type_get, |
| 241 | wire_type_set=wire_type_set)) |
| 242 | |
Rich Lane | 068847f | 2014-10-13 16:26:33 -0700 | [diff] [blame] | 243 | # If this is the root of an inheritance hierachy, add metadata |
| 244 | # for the corresponding header class |
| 245 | if uclass.name in type_maps.inheritance_map: |
| 246 | class_metadata.append(ClassMetadata( |
| 247 | name=uclass.name + '_header', |
| 248 | wire_length_get=wire_length_get, |
| 249 | wire_length_set=wire_length_set, |
| 250 | wire_type_get=wire_type_get, |
| 251 | wire_type_set=wire_type_set)) |
Rich Lane | 08c9f3a | 2014-06-13 11:24:37 -0700 | [diff] [blame] | 252 | |
| 253 | for metadata in class_metadata: |
| 254 | class_metadata_dict[metadata.name] = metadata |
| 255 | |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 256 | def generate_class_metadata(install_dir): |
| 257 | with template_utils.open_output(install_dir, "loci/inc/loci/loci_class_metadata.h") as out: |
| 258 | util.render_template(out, "loci_class_metadata.h") |
| 259 | |
| 260 | with template_utils.open_output(install_dir, "loci/src/loci_class_metadata.c") as out: |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 261 | util.render_template(out, "loci_class_metadata.c", class_metadata=class_metadata) |