blob: 7cd068f4e6fd9931b16e9e763af0012946d66a80 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -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
28from collections import namedtuple
Andreas Wundsam5630c422013-11-15 13:43:11 -080029import loxi_globals
Rich Lanec2685792013-04-30 14:08:33 -070030import struct
Andreas Wundsam5420b952013-11-15 13:41:01 -080031import template_utils
Rich Lanea06d0c32013-03-25 08:52:03 -070032import loxi_utils.loxi_utils as utils
33import util
34import oftype
Rich Lane002e70c2013-05-09 17:08:16 -070035from loxi_ir import *
Rich Lanea06d0c32013-03-25 08:52:03 -070036
Rich Lanefa931272013-11-10 17:43:50 -080037ofclasses_by_version = {}
38
Rich Lane002e70c2013-05-09 17:08:16 -070039PyOFClass = namedtuple('PyOFClass', ['name', 'pyname', 'members', 'type_members',
Rich Laneb564efc2013-07-23 14:19:29 -070040 'min_length', 'is_fixed_length',
41 'has_internal_alignment', 'has_external_alignment'])
Rich Lanea06d0c32013-03-25 08:52:03 -070042
Rich Lane002e70c2013-05-09 17:08:16 -070043# Return the name for the generated Python class
44def generate_pyname(cls):
45 if utils.class_is_action(cls):
46 return cls[10:]
Rich Laneea693752013-03-18 11:05:45 -070047 elif utils.class_is_oxm(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070048 return cls[7:]
Rich Laned82c0a62013-05-02 15:40:35 -070049 elif utils.class_is_meter_band(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070050 return cls[14:]
Rich Lanee02314c2013-05-02 16:42:04 -070051 elif utils.class_is_instruction(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070052 return cls[15:]
53 else:
54 return cls[3:]
Rich Lanea06d0c32013-03-25 08:52:03 -070055
Rich Lane002e70c2013-05-09 17:08:16 -070056# Create intermediate representation, extended from the LOXI IR
57# HACK the oftype member attribute is replaced with an OFType instance
Rich Lanea06d0c32013-03-25 08:52:03 -070058def build_ofclasses(version):
Rich Lanea06d0c32013-03-25 08:52:03 -070059 ofclasses = []
Andreas Wundsam5630c422013-11-15 13:43:11 -080060 for ofclass in loxi_globals.ir[version].classes:
Rich Lane002e70c2013-05-09 17:08:16 -070061 cls = ofclass.name
Rich Laneb25d07c2013-08-22 17:22:43 -070062 if ofclass.virtual:
Rich Lanea06d0c32013-03-25 08:52:03 -070063 continue
Rich Lanea06d0c32013-03-25 08:52:03 -070064
Rich Lanea06d0c32013-03-25 08:52:03 -070065 members = []
Rich Lanea06d0c32013-03-25 08:52:03 -070066 type_members = []
Rich Lane8ca3b772013-04-30 13:36:55 -070067
Rich Lane002e70c2013-05-09 17:08:16 -070068 for m in ofclass.members:
69 if type(m) == OFTypeMember:
Rich Laneda11f8b2013-06-19 15:53:25 -070070 members.append(m)
Rich Lane8ca3b772013-04-30 13:36:55 -070071 type_members.append(members[-1])
Rich Lane002e70c2013-05-09 17:08:16 -070072 elif type(m) == OFLengthMember:
Rich Laneda11f8b2013-06-19 15:53:25 -070073 members.append(m)
Rich Lane002e70c2013-05-09 17:08:16 -070074 elif type(m) == OFFieldLengthMember:
Rich Laneda11f8b2013-06-19 15:53:25 -070075 members.append(m)
Rich Lane002e70c2013-05-09 17:08:16 -070076 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 Laneda11f8b2013-06-19 15:53:25 -070083 oftype=m.oftype,
Andreas Wundsam5630c422013-11-15 13:43:11 -080084 value=version.wire_version,
85 base_length=m.base_length,
86 is_fixed_length=m.is_fixed_length,
87 offset=m.offset))
Rich Lane002e70c2013-05-09 17:08:16 -070088 type_members.append(members[-1])
89 else:
Rich Laneda11f8b2013-06-19 15:53:25 -070090 members.append(m)
Rich Lanea06d0c32013-03-25 08:52:03 -070091
92 ofclasses.append(
Rich Lane002e70c2013-05-09 17:08:16 -070093 PyOFClass(name=cls,
94 pyname=generate_pyname(cls),
95 members=members,
96 type_members=type_members,
Andreas Wundsam5630c422013-11-15 13:43:11 -080097 min_length=ofclass.base_length,
98 is_fixed_length=ofclass.is_fixed_length,
Rich Laneb564efc2013-07-23 14:19:29 -070099 has_internal_alignment=cls == 'of_action_set_field',
100 has_external_alignment=cls == 'of_match_v3'))
Rich Lanea06d0c32013-03-25 08:52:03 -0700101 return ofclasses
102
103def generate_init(out, name, version):
Rich Laneea693752013-03-18 11:05:45 -0700104 util.render_template(out, 'init.py', version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700105
106def generate_action(out, name, version):
Rich Lanefa931272013-11-10 17:43:50 -0800107 ofclasses = [x for x in ofclasses_by_version[version]
Rich Lanea06d0c32013-03-25 08:52:03 -0700108 if utils.class_is_action(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700109 util.render_template(out, 'action.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700110
Rich Laneea693752013-03-18 11:05:45 -0700111def generate_oxm(out, name, version):
Rich Lanefa931272013-11-10 17:43:50 -0800112 ofclasses = [x for x in ofclasses_by_version[version]
Rich Laneea693752013-03-18 11:05:45 -0700113 if utils.class_is_oxm(x.name)]
114 util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version)
115
Rich Lanea06d0c32013-03-25 08:52:03 -0700116def generate_common(out, name, version):
Rich Lanefa931272013-11-10 17:43:50 -0800117 ofclasses = [x for x in ofclasses_by_version[version]
Rich Lanea06d0c32013-03-25 08:52:03 -0700118 if not utils.class_is_message(x.name)
119 and not utils.class_is_action(x.name)
Rich Lanee02314c2013-05-02 16:42:04 -0700120 and not utils.class_is_instruction(x.name)
Rich Laned82c0a62013-05-02 15:40:35 -0700121 and not utils.class_is_meter_band(x.name)
Rich Laneea693752013-03-18 11:05:45 -0700122 and not utils.class_is_oxm(x.name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700123 and not utils.class_is_list(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700124 util.render_template(out, 'common.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700125
126def generate_const(out, name, version):
Rich Lanedffcf852013-05-10 10:40:46 -0700127 util.render_template(out, 'const.py', version=version,
Andreas Wundsam5630c422013-11-15 13:43:11 -0800128 enums=loxi_globals.ir[version].enums)
Rich Lanea06d0c32013-03-25 08:52:03 -0700129
Rich Lanee02314c2013-05-02 16:42:04 -0700130def generate_instruction(out, name, version):
Rich Lanefa931272013-11-10 17:43:50 -0800131 ofclasses = [x for x in ofclasses_by_version[version]
Rich Lanee02314c2013-05-02 16:42:04 -0700132 if utils.class_is_instruction(x.name)]
133 util.render_template(out, 'instruction.py', ofclasses=ofclasses, version=version)
134
Rich Lanea06d0c32013-03-25 08:52:03 -0700135def generate_message(out, name, version):
Rich Lanefa931272013-11-10 17:43:50 -0800136 ofclasses = [x for x in ofclasses_by_version[version]
Rich Lanea06d0c32013-03-25 08:52:03 -0700137 if utils.class_is_message(x.name)]
138 util.render_template(out, 'message.py', ofclasses=ofclasses, version=version)
139
Rich Laned82c0a62013-05-02 15:40:35 -0700140def generate_meter_band(out, name, version):
Rich Lanefa931272013-11-10 17:43:50 -0800141 ofclasses = [x for x in ofclasses_by_version[version]
Rich Laned82c0a62013-05-02 15:40:35 -0700142 if utils.class_is_meter_band(x.name)]
143 util.render_template(out, 'meter_band.py', ofclasses=ofclasses, version=version)
144
Rich Lanea06d0c32013-03-25 08:52:03 -0700145def generate_pp(out, name, version):
146 util.render_template(out, 'pp.py')
147
148def generate_util(out, name, version):
Rich Laneadb79832013-05-02 17:14:33 -0700149 util.render_template(out, 'util.py', version=version)
Rich Lanefa931272013-11-10 17:43:50 -0800150
151def init():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800152 for version in loxi_globals.OFVersions.target_versions:
Rich Lanefa931272013-11-10 17:43:50 -0800153 ofclasses_by_version[version] = build_ofclasses(version)