Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [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 | from collections import namedtuple |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 29 | import loxi_globals |
Rich Lane | c268579 | 2013-04-30 14:08:33 -0700 | [diff] [blame] | 30 | import struct |
Andreas Wundsam | 5420b95 | 2013-11-15 13:41:01 -0800 | [diff] [blame] | 31 | import template_utils |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 32 | import loxi_utils.loxi_utils as utils |
| 33 | import util |
| 34 | import oftype |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 35 | from loxi_ir import * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 36 | |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 37 | ofclasses_by_version = {} |
| 38 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 39 | PyOFClass = namedtuple('PyOFClass', ['name', 'pyname', 'members', 'type_members', |
Rich Lane | b564efc | 2013-07-23 14:19:29 -0700 | [diff] [blame] | 40 | 'min_length', 'is_fixed_length', |
| 41 | 'has_internal_alignment', 'has_external_alignment']) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 42 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 43 | # Return the name for the generated Python class |
| 44 | def generate_pyname(cls): |
| 45 | if utils.class_is_action(cls): |
| 46 | return cls[10:] |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 47 | elif utils.class_is_oxm(cls): |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 48 | return cls[7:] |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 49 | elif utils.class_is_meter_band(cls): |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 50 | return cls[14:] |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 51 | elif utils.class_is_instruction(cls): |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 52 | return cls[15:] |
| 53 | else: |
| 54 | return cls[3:] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 55 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 56 | # Create intermediate representation, extended from the LOXI IR |
| 57 | # HACK the oftype member attribute is replaced with an OFType instance |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 58 | def build_ofclasses(version): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 59 | ofclasses = [] |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 60 | for ofclass in loxi_globals.ir[version].classes: |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 61 | cls = ofclass.name |
Rich Lane | b25d07c | 2013-08-22 17:22:43 -0700 | [diff] [blame] | 62 | if ofclass.virtual: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 63 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 64 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 65 | members = [] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 66 | type_members = [] |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 67 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 68 | for m in ofclass.members: |
| 69 | if type(m) == OFTypeMember: |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 70 | members.append(m) |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 71 | type_members.append(members[-1]) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 72 | elif type(m) == OFLengthMember: |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 73 | members.append(m) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 74 | elif type(m) == OFFieldLengthMember: |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 75 | members.append(m) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 76 | elif type(m) == OFPadMember: |
| 77 | members.append(m) |
| 78 | elif type(m) == OFDataMember: |
| 79 | if utils.class_is_message(ofclass.name) and m.name == 'version': |
| 80 | # HACK move to frontend |
| 81 | members.append(OFTypeMember( |
| 82 | name=m.name, |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 83 | oftype=m.oftype, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 84 | value=version.wire_version, |
| 85 | base_length=m.base_length, |
| 86 | is_fixed_length=m.is_fixed_length, |
| 87 | offset=m.offset)) |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 88 | type_members.append(members[-1]) |
| 89 | else: |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 90 | members.append(m) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 91 | |
| 92 | ofclasses.append( |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 93 | PyOFClass(name=cls, |
| 94 | pyname=generate_pyname(cls), |
| 95 | members=members, |
| 96 | type_members=type_members, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 97 | min_length=ofclass.base_length, |
| 98 | is_fixed_length=ofclass.is_fixed_length, |
Rich Lane | b564efc | 2013-07-23 14:19:29 -0700 | [diff] [blame] | 99 | has_internal_alignment=cls == 'of_action_set_field', |
| 100 | has_external_alignment=cls == 'of_match_v3')) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 101 | return ofclasses |
| 102 | |
| 103 | def generate_init(out, name, version): |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 104 | util.render_template(out, 'init.py', version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 105 | |
| 106 | def generate_action(out, name, version): |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 107 | ofclasses = [x for x in ofclasses_by_version[version] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 108 | if utils.class_is_action(x.name)] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 109 | util.render_template(out, 'action.py', ofclasses=ofclasses, version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 110 | |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 111 | def generate_oxm(out, name, version): |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 112 | ofclasses = [x for x in ofclasses_by_version[version] |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 113 | if utils.class_is_oxm(x.name)] |
| 114 | util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version) |
| 115 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 116 | def generate_common(out, name, version): |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 117 | ofclasses = [x for x in ofclasses_by_version[version] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 118 | if not utils.class_is_message(x.name) |
| 119 | and not utils.class_is_action(x.name) |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 120 | and not utils.class_is_instruction(x.name) |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 121 | and not utils.class_is_meter_band(x.name) |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 122 | and not utils.class_is_oxm(x.name) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 123 | and not utils.class_is_list(x.name)] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 124 | util.render_template(out, 'common.py', ofclasses=ofclasses, version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 125 | |
| 126 | def generate_const(out, name, version): |
Rich Lane | dffcf85 | 2013-05-10 10:40:46 -0700 | [diff] [blame] | 127 | util.render_template(out, 'const.py', version=version, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 128 | enums=loxi_globals.ir[version].enums) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 129 | |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 130 | def generate_instruction(out, name, version): |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 131 | ofclasses = [x for x in ofclasses_by_version[version] |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 132 | if utils.class_is_instruction(x.name)] |
| 133 | util.render_template(out, 'instruction.py', ofclasses=ofclasses, version=version) |
| 134 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 135 | def generate_message(out, name, version): |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 136 | ofclasses = [x for x in ofclasses_by_version[version] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 137 | if utils.class_is_message(x.name)] |
| 138 | util.render_template(out, 'message.py', ofclasses=ofclasses, version=version) |
| 139 | |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 140 | def generate_meter_band(out, name, version): |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 141 | ofclasses = [x for x in ofclasses_by_version[version] |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 142 | if utils.class_is_meter_band(x.name)] |
| 143 | util.render_template(out, 'meter_band.py', ofclasses=ofclasses, version=version) |
| 144 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 145 | def generate_pp(out, name, version): |
| 146 | util.render_template(out, 'pp.py') |
| 147 | |
| 148 | def generate_util(out, name, version): |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 149 | util.render_template(out, 'util.py', version=version) |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 150 | |
| 151 | def init(): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 152 | for version in loxi_globals.OFVersions.target_versions: |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 153 | ofclasses_by_version[version] = build_ofclasses(version) |