blob: bc79ed94367cf0ca261bed28191c7b0145df3f5f [file] [log] [blame]
Rich Laned47e5a22013-05-09 14:21:16 -07001# 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 Wundsamdfeb5942013-09-19 13:07:49 -070028from generic_utils import find
29from collections import namedtuple
Rich Lane4d9f0f62013-05-09 15:50:57 -070030import copy
Andreas Wundsamd30c1072013-11-15 13:36:57 -080031import loxi_globals
32import loxi_front_end.frontend_ir as ir
Rich Laned47e5a22013-05-09 14:21:16 -070033
34class InputError(Exception):
35 pass
36
Andreas Wundsamdfeb5942013-09-19 13:07:49 -070037
38FrontendCtx = namedtuple("FrontendCtx", ("used_enums"))
39
40def get_type(t_ast, ctx):
41 if t_ast[0] == "enum":
42 ctx.used_enums.add(t_ast[1])
43
44 return t_ast[1]
45
46def create_member(m_ast, ctx):
Rich Lane4d9f0f62013-05-09 15:50:57 -070047 if m_ast[0] == 'pad':
Andreas Wundsamd30c1072013-11-15 13:36:57 -080048 return ir.OFPadMember(length=m_ast[1])
Rich Lane32142872013-05-09 21:16:47 -070049 elif m_ast[0] == 'type':
Andreas Wundsamd30c1072013-11-15 13:36:57 -080050 return ir.OFTypeMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), value=m_ast[3])
Rich Lanef424e972013-05-09 21:00:13 -070051 elif m_ast[0] == 'data':
52 if m_ast[2] == 'length' or m_ast[2] == 'len': # Should be moved to parser
Andreas Wundsamd30c1072013-11-15 13:36:57 -080053 return ir.OFLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
Rich Lanef424e972013-05-09 21:00:13 -070054 elif m_ast[2] == 'actions_len':
Andreas Wundsamd30c1072013-11-15 13:36:57 -080055 return ir.OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='actions')
Rich Lane7bc23772013-11-29 17:47:46 -080056 if m_ast[2] == 'version': # Should be moved to parser
57 return ir.OFVersionMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
Rich Lanefb670252013-12-03 13:03:51 -080058 elif m_ast[2] == 'key_length':
59 return ir.OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='key')
Rich Lanef424e972013-05-09 21:00:13 -070060 else:
Andreas Wundsamd30c1072013-11-15 13:36:57 -080061 return ir.OFDataMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
Andreas Wundsam780e0c92013-08-02 17:48:27 -070062 elif m_ast[0] == 'discriminator':
Andreas Wundsamd30c1072013-11-15 13:36:57 -080063 return ir.OFDiscriminatorMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
Andreas Wundsam7adebd22013-08-01 22:14:14 -070064 else:
Andreas Wundsam7933beb2013-08-02 22:36:42 -070065 raise InputError("Dont know how to create member: %s" % m_ast[0])
Rich Lane4d9f0f62013-05-09 15:50:57 -070066
Andreas Wundsamd30c1072013-11-15 13:36:57 -080067def create_ofinput(filename, ast):
Andreas Wundsamdfeb5942013-09-19 13:07:49 -070068
Rich Laned47e5a22013-05-09 14:21:16 -070069 """
70 Create an OFInput from an AST
71
72 @param ast An AST as returned by loxi_front_end.parser.parse
73
74 @returns An OFInput object
75 """
Andreas Wundsamdfeb5942013-09-19 13:07:49 -070076 ctx = FrontendCtx(set())
Andreas Wundsamd30c1072013-11-15 13:36:57 -080077 ofinput = ir.OFInput(filename, wire_versions=set(), classes=[], enums=[])
Rich Laned47e5a22013-05-09 14:21:16 -070078
Rich Lane4d9f0f62013-05-09 15:50:57 -070079 for decl_ast in ast:
80 if decl_ast[0] == 'struct':
Andreas Wundsam7933beb2013-08-02 22:36:42 -070081 # 0: "struct"
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070082 # 1: name
83 # 2: potentially list of [param_name, param_value]
Andreas Wundsam780e0c92013-08-02 17:48:27 -070084 # 3: super_class or None
Andreas Wundsam7933beb2013-08-02 22:36:42 -070085 # 4: list of members
Andreas Wundsam780e0c92013-08-02 17:48:27 -070086 superclass = decl_ast[3]
Andreas Wundsamdfeb5942013-09-19 13:07:49 -070087 members = [create_member(m_ast, ctx) for m_ast in decl_ast[4]]
Andreas Wundsam780e0c92013-08-02 17:48:27 -070088
Andreas Wundsamd30c1072013-11-15 13:36:57 -080089 discriminators = [ m for m in members if isinstance(m, ir.OFDiscriminatorMember) ]
Andreas Wundsam780e0c92013-08-02 17:48:27 -070090 if len(discriminators) > 1:
Andreas Wundsam7933beb2013-08-02 22:36:42 -070091 raise InputError("%s: Cannot support more than one discriminator by class - got %s" %
Andreas Wundsam780e0c92013-08-02 17:48:27 -070092 (decl_ast[1], repr(discriminators)))
Andreas Wundsamd30c1072013-11-15 13:36:57 -080093 ofclass = ir.OFClass(name=decl_ast[1], members=members, superclass=superclass,
Andreas Wundsam780e0c92013-08-02 17:48:27 -070094 virtual = len(discriminators) > 0,
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070095 params = { param: value for param, value in decl_ast[2] })
Rich Lane4d9f0f62013-05-09 15:50:57 -070096 ofinput.classes.append(ofclass)
Rich Lane4d9f0f62013-05-09 15:50:57 -070097 if decl_ast[0] == 'enum':
Andreas Wundsam4ee51462013-07-30 11:00:37 -070098 # 0: "enum"
99 # 1: name
100 # 2: potentially list of [param_name, param_value]
101 # 3: list of [constant_name, constant_value]+
Andreas Wundsamd30c1072013-11-15 13:36:57 -0800102 enum = ir.OFEnum(name=decl_ast[1],
103 entries=[ir.OFEnumEntry(name=x[0], value=x[2], params={param:value for param, value in x[1] }) for x in decl_ast[3]],
Andreas Wundsam4ee51462013-07-30 11:00:37 -0700104 params = { param: value for param, value in decl_ast[2] }
105 )
Rich Lane4d9f0f62013-05-09 15:50:57 -0700106 ofinput.enums.append(enum)
107 elif decl_ast[0] == 'metadata':
108 if decl_ast[1] == 'version':
109 if decl_ast[2] == 'any':
Andreas Wundsamd30c1072013-11-15 13:36:57 -0800110 ofinput.wire_versions.update(v.wire_version for v in loxi_globals.OFVersions.all_supported)
111 elif int(decl_ast[2]) in loxi_globals.OFVersions.wire_version_map:
Rich Lane4d9f0f62013-05-09 15:50:57 -0700112 ofinput.wire_versions.add(int(decl_ast[2]))
Rich Laned47e5a22013-05-09 14:21:16 -0700113 else:
Rich Lane4d9f0f62013-05-09 15:50:57 -0700114 raise InputError("Unrecognized wire protocol version %r" % decl_ast[2])
Rich Laned47e5a22013-05-09 14:21:16 -0700115 found_wire_version = True
116
117 if not ofinput.wire_versions:
118 raise InputError("Missing #version metadata")
119
Rich Laned47e5a22013-05-09 14:21:16 -0700120 return ofinput