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 | |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 28 | from collections import defaultdict |
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 | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 37 | modules_by_version = {} |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 38 | |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 39 | # Map from inheritance root to module name |
| 40 | roots = { |
| 41 | 'of_header': 'message', |
| 42 | 'of_action': 'action', |
| 43 | 'of_oxm': 'oxm', |
| 44 | 'of_instruction': 'instruction', |
| 45 | 'of_meter_band': 'meter_band', |
| 46 | } |
| 47 | |
| 48 | # Return the module and class names for the generated Python class |
| 49 | def generate_pyname(ofclass): |
| 50 | for root, module_name in roots.items(): |
| 51 | if ofclass.name == root: |
| 52 | return module_name, module_name |
| 53 | elif ofclass.is_instanceof(root): |
| 54 | if root == 'of_header': |
| 55 | # The input files don't prefix message names |
| 56 | return module_name, ofclass.name[3:] |
| 57 | else: |
| 58 | return module_name, ofclass.name[len(root)+1:] |
| 59 | return 'common', ofclass.name[3:] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 60 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 61 | # Create intermediate representation, extended from the LOXI IR |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 62 | def build_ofclasses(version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 63 | modules = defaultdict(list) |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 64 | for ofclass in loxi_globals.ir[version].classes: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 65 | module_name, ofclass.pyname = generate_pyname(ofclass) |
| 66 | modules[module_name].append(ofclass) |
| 67 | return modules |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 68 | |
| 69 | def generate_init(out, name, version): |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 70 | util.render_template(out, 'init.py', version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 71 | |
| 72 | def generate_action(out, name, version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 73 | util.render_template(out, 'action.py', |
| 74 | ofclasses=modules_by_version[version]['action'], |
| 75 | version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 76 | |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 77 | def generate_oxm(out, name, version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 78 | util.render_template(out, 'oxm.py', |
| 79 | ofclasses=modules_by_version[version]['oxm'], |
| 80 | version=version) |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 81 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 82 | def generate_common(out, name, version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 83 | util.render_template(out, 'common.py', |
| 84 | ofclasses=modules_by_version[version]['common'], |
| 85 | version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 86 | |
| 87 | def generate_const(out, name, version): |
Rich Lane | dffcf85 | 2013-05-10 10:40:46 -0700 | [diff] [blame] | 88 | util.render_template(out, 'const.py', version=version, |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 89 | enums=loxi_globals.ir[version].enums) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 90 | |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 91 | def generate_instruction(out, name, version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 92 | util.render_template(out, 'instruction.py', |
| 93 | ofclasses=modules_by_version[version]['instruction'], |
| 94 | version=version) |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 95 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 96 | def generate_message(out, name, version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 97 | util.render_template(out, 'message.py', |
| 98 | ofclasses=modules_by_version[version]['message'], |
| 99 | version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 100 | |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 101 | def generate_meter_band(out, name, version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 102 | util.render_template(out, 'meter_band.py', |
| 103 | ofclasses=modules_by_version[version]['meter_band'], |
| 104 | version=version) |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame] | 105 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 106 | def generate_pp(out, name, version): |
| 107 | util.render_template(out, 'pp.py') |
| 108 | |
| 109 | def generate_util(out, name, version): |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 110 | util.render_template(out, 'util.py', version=version) |
Rich Lane | fa93127 | 2013-11-10 17:43:50 -0800 | [diff] [blame] | 111 | |
| 112 | def init(): |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 113 | for version in loxi_globals.OFVersions.target_versions: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 114 | modules_by_version[version] = build_ofclasses(version) |