Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -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 | |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 28 | from generic_utils import find |
| 29 | from collections import namedtuple |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 30 | import copy |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 31 | import of_g |
| 32 | import loxi_front_end.type_maps as type_maps |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 33 | from loxi_ir import * |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 34 | |
| 35 | class InputError(Exception): |
| 36 | pass |
| 37 | |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 38 | |
| 39 | FrontendCtx = namedtuple("FrontendCtx", ("used_enums")) |
| 40 | |
| 41 | def get_type(t_ast, ctx): |
| 42 | if t_ast[0] == "enum": |
| 43 | ctx.used_enums.add(t_ast[1]) |
| 44 | |
| 45 | return t_ast[1] |
| 46 | |
| 47 | def create_member(m_ast, ctx): |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 48 | if m_ast[0] == 'pad': |
| 49 | return OFPadMember(length=m_ast[1]) |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 50 | elif m_ast[0] == 'type': |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 51 | return OFTypeMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), value=m_ast[3]) |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 52 | elif m_ast[0] == 'data': |
| 53 | if m_ast[2] == 'length' or m_ast[2] == 'len': # Should be moved to parser |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 54 | return OFLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 55 | elif m_ast[2] == 'actions_len': |
| 56 | # HACK only usage so far |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 57 | return OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='actions') |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 58 | else: |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 59 | return OFDataMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 60 | elif m_ast[0] == 'discriminator': |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 61 | return OFDiscriminatorMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) |
Andreas Wundsam | 7adebd2 | 2013-08-01 22:14:14 -0700 | [diff] [blame] | 62 | else: |
Andreas Wundsam | 7933beb | 2013-08-02 22:36:42 -0700 | [diff] [blame] | 63 | raise InputError("Dont know how to create member: %s" % m_ast[0]) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 64 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 65 | def create_ofinput(ast): |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 66 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 67 | """ |
| 68 | Create an OFInput from an AST |
| 69 | |
| 70 | @param ast An AST as returned by loxi_front_end.parser.parse |
| 71 | |
| 72 | @returns An OFInput object |
| 73 | """ |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 74 | ctx = FrontendCtx(set()) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 75 | ofinput = OFInput(wire_versions=set(), classes=[], enums=[]) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 76 | |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 77 | for decl_ast in ast: |
| 78 | if decl_ast[0] == 'struct': |
Andreas Wundsam | 7933beb | 2013-08-02 22:36:42 -0700 | [diff] [blame] | 79 | # 0: "struct" |
Andreas Wundsam | fef7d5f | 2013-08-01 22:15:44 -0700 | [diff] [blame] | 80 | # 1: name |
| 81 | # 2: potentially list of [param_name, param_value] |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 82 | # 3: super_class or None |
Andreas Wundsam | 7933beb | 2013-08-02 22:36:42 -0700 | [diff] [blame] | 83 | # 4: list of members |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 84 | superclass = decl_ast[3] |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 85 | members = [create_member(m_ast, ctx) for m_ast in decl_ast[4]] |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 86 | |
| 87 | discriminators = [ m for m in members if isinstance(m, OFDiscriminatorMember) ] |
| 88 | if len(discriminators) > 1: |
Andreas Wundsam | 7933beb | 2013-08-02 22:36:42 -0700 | [diff] [blame] | 89 | raise InputError("%s: Cannot support more than one discriminator by class - got %s" % |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 90 | (decl_ast[1], repr(discriminators))) |
| 91 | ofclass = OFClass(name=decl_ast[1], members=members, superclass=superclass, |
| 92 | virtual = len(discriminators) > 0, |
Andreas Wundsam | fef7d5f | 2013-08-01 22:15:44 -0700 | [diff] [blame] | 93 | params = { param: value for param, value in decl_ast[2] }) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 94 | ofinput.classes.append(ofclass) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 95 | if decl_ast[0] == 'enum': |
Andreas Wundsam | 4ee5146 | 2013-07-30 11:00:37 -0700 | [diff] [blame] | 96 | # 0: "enum" |
| 97 | # 1: name |
| 98 | # 2: potentially list of [param_name, param_value] |
| 99 | # 3: list of [constant_name, constant_value]+ |
| 100 | enum = OFEnum(name=decl_ast[1], |
| 101 | entries=[OFEnumEntry(name=x[0], value=x[2], params={param:value for param, value in x[1] }) for x in decl_ast[3]], |
| 102 | params = { param: value for param, value in decl_ast[2] } |
| 103 | ) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 104 | ofinput.enums.append(enum) |
| 105 | elif decl_ast[0] == 'metadata': |
| 106 | if decl_ast[1] == 'version': |
| 107 | if decl_ast[2] == 'any': |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 108 | ofinput.wire_versions.update(of_g.wire_ver_map.keys()) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 109 | elif int(decl_ast[2]) in of_g.supported_wire_protos: |
| 110 | ofinput.wire_versions.add(int(decl_ast[2])) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 111 | else: |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 112 | raise InputError("Unrecognized wire protocol version %r" % decl_ast[2]) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 113 | found_wire_version = True |
| 114 | |
| 115 | if not ofinput.wire_versions: |
| 116 | raise InputError("Missing #version metadata") |
| 117 | |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 118 | for used_enum in ctx.used_enums: |
| 119 | if not find(lambda e: e.name == used_enum, ofinput.enums): |
| 120 | raise Exception("Undeclared enum used in OFInput: {}".format(used_enum)) |
| 121 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 122 | return ofinput |