-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonObjects.cpp
403 lines (367 loc) · 13.9 KB
/
JsonObjects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "lib/json.h"
#include "JsonObjects.h"
#include "helpers.h"
namespace BMV2 {
const int JSON_MAJOR_VERSION = 2;
const int JSON_MINOR_VERSION = 7;
JsonObjects::JsonObjects() {
toplevel = new Util::JsonObject();
meta = new Util::JsonObject();
header_types = insert_array_field(toplevel, "header_types");
headers = insert_array_field(toplevel, "headers");
header_stacks = insert_array_field(toplevel, "header_stacks");
header_union_types = insert_array_field(toplevel, "header_union_types");
header_unions = insert_array_field(toplevel, "header_unions");
header_union_stacks = insert_array_field(toplevel, "header_union_stacks");
field_lists = insert_array_field(toplevel, "field_lists");
errors = insert_array_field(toplevel, "errors");
enums = insert_array_field(toplevel, "enums");
parsers = insert_array_field(toplevel, "parsers");
deparsers = insert_array_field(toplevel, "deparsers");
meter_arrays = insert_array_field(toplevel, "meter_arrays");
counters = insert_array_field(toplevel, "counter_arrays");
register_arrays = insert_array_field(toplevel, "register_arrays");
calculations = insert_array_field(toplevel, "calculations");
learn_lists = insert_array_field(toplevel, "learn_lists");
actions = insert_array_field(toplevel, "actions");
pipelines = insert_array_field(toplevel, "pipelines");
checksums = insert_array_field(toplevel, "checksums");
force_arith = insert_array_field(toplevel, "force_arith");
externs = insert_array_field(toplevel, "extern_instances");
field_aliases = insert_array_field(toplevel, "field_aliases");
}
Util::JsonObject*
JsonObjects::find_object_by_name(Util::JsonArray* array, const cstring& name) {
for (auto e : *array) {
auto obj = e->to<Util::JsonObject>();
auto val = obj->get("name")->to<Util::JsonValue>();
if (val != nullptr && val->isString() && val->getString() == name) {
return obj;
}
}
return nullptr;
}
/// Insert a json array to a parent object under key 'name'.
Util::JsonArray*
JsonObjects::insert_array_field(Util::JsonObject* parent, cstring name) {
auto result = new Util::JsonArray();
parent->emplace(name, result);
return result;
}
/// Append a json array to a parent json array.
Util::JsonArray*
JsonObjects::append_array(Util::JsonArray* parent) {
auto result = new Util::JsonArray();
parent->append(result);
return result;
}
/// Insert a json array named 'parameters' in a parent json object.
Util::JsonArray*
JsonObjects::create_parameters(Util::JsonObject* object) {
return insert_array_field(object, "parameters");
}
/// Append a json object r to a parent json array,
/// insert a field 'op' with 'name' to parent.
Util::JsonObject*
JsonObjects::create_primitive(Util::JsonArray* parent, cstring name) {
auto result = new Util::JsonObject();
result->emplace("op", name);
parent->append(result);
return result;
}
void
JsonObjects::add_program_info(const cstring& name) {
toplevel->emplace("program", name);
}
void
JsonObjects::add_meta_info() {
static constexpr int version_major = JSON_MAJOR_VERSION;
static constexpr int version_minor = JSON_MINOR_VERSION;
auto version = insert_array_field(meta, "version");
version->append(version_major);
version->append(version_minor);
meta->emplace("compiler", "https://github.com/p4lang/p4c");
toplevel->emplace("__meta__", meta);
}
/**
* Create a header type in json.
* @param name header name
* @param type header type
* @param max_length maximum length for a header with varbit fields;
* if 0 header does not contain varbit fields
* @param fields a JsonArray for the fields in the header
*/
unsigned
JsonObjects::add_header_type(const cstring& name, Util::JsonArray*& fields, unsigned max_length) {
std::string sname(name, name.size());
auto header_type_id_it = header_type_id.find(sname);
if (header_type_id_it != header_type_id.end()) {
return header_type_id_it->second;
}
auto header_type = new Util::JsonObject();
unsigned id = BMV2::nextId("header_types");
header_type_id[sname] = id;
header_type->emplace("name", name);
header_type->emplace("id", id);
if (fields != nullptr) {
header_type->emplace("fields", fields);
} else {
auto temp = new Util::JsonArray();
header_type->emplace("fields", temp);
}
if (max_length > 0)
header_type->emplace("max_length", max_length);
header_types->append(header_type);
return id;
}
unsigned
JsonObjects::add_union_type(const cstring& name, Util::JsonArray*& fields) {
std::string sname(name, name.size());
auto it = union_type_id.find(sname);
if (it != union_type_id.end())
return it->second;
auto union_type = new Util::JsonObject();
unsigned id = BMV2::nextId("header_union_types");
union_type_id[sname] = id;
union_type->emplace("name", name);
union_type->emplace("id", id);
if (fields != nullptr) {
union_type->emplace("headers", fields);
} else {
auto temp = new Util::JsonArray();
union_type->emplace("headers", temp);
}
header_union_types->append(union_type);
return id;
}
/// Create a header type with empty field list.
unsigned
JsonObjects::add_header_type(const cstring& name) {
std::string sname(name, name.size());
auto header_type_id_it = header_type_id.find(sname);
if (header_type_id_it != header_type_id.end()) {
return header_type_id_it->second;
}
auto header_type = new Util::JsonObject();
unsigned id = BMV2::nextId("header_types");
header_type_id[sname] = id;
header_type->emplace("name", name);
header_type->emplace("id", id);
auto temp = new Util::JsonArray();
header_type->emplace("fields", temp);
header_types->append(header_type);
return id;
}
/// Create a set of fields to an existing header type.
/// The header type is decribed by the name.
void
JsonObjects::add_header_field(const cstring& name, Util::JsonArray*& field) {
CHECK_NULL(field);
Util::JsonObject* headerType = find_object_by_name(header_types, name);
Util::JsonArray* fields = headerType->get("fields")->to<Util::JsonArray>();
BUG_CHECK(fields != nullptr, "header '%1%' not found", name);
fields->append(field);
}
/// Create a header instance in json.
unsigned
JsonObjects::add_header(const cstring& type, const cstring& name) {
auto header = new Util::JsonObject();
unsigned id = BMV2::nextId("headers");
LOG1("add header id " << id);
header->emplace("name", name);
header->emplace("id", id);
header->emplace("header_type", type);
header->emplace("metadata", false);
header->emplace("pi_omit", true);
headers->append(header);
return id;
}
/// Create a header_union instance in json.
unsigned
JsonObjects::add_union(const cstring& type, Util::JsonArray*& headers, const cstring& name) {
auto u = new Util::JsonObject();
unsigned id = BMV2::nextId("header_unions");
LOG3("add header_union id " << id);
u->emplace("name", name);
u->emplace("id", id);
u->emplace("union_type", type);
u->emplace("header_ids", headers);
u->emplace("pi_omit", true);
header_unions->append(u);
return id;
}
unsigned
JsonObjects::add_metadata(const cstring& type, const cstring& name) {
auto header = new Util::JsonObject();
unsigned id = BMV2::nextId("headers");
LOG1("add metadata header id " << id);
header->emplace("name", name);
header->emplace("id", id);
header->emplace("header_type", type);
header->emplace("metadata", true);
header->emplace("pi_omit", true); // Don't expose in PI.
headers->append(header);
return id;
}
void
JsonObjects::add_header_stack(const cstring& type, const cstring& name,
const unsigned size, std::vector<unsigned>& ids) {
auto stack = new Util::JsonObject();
unsigned id = BMV2::nextId("stack");
stack->emplace("name", name);
stack->emplace("id", id);
stack->emplace("header_type", type);
stack->emplace("size", size);
auto members = new Util::JsonArray();
stack->emplace("header_ids", members);
for (auto id : ids) {
members->append(id);
}
header_stacks->append(stack);
}
/// Add an error to json.
void
JsonObjects::add_error(const cstring& name, const unsigned type) {
auto arr = append_array(errors);
arr->append(name);
arr->append(type);
}
/// Add a single enum entry to json.
/// A enum entry is identified with { enum_name, entry_name, entry_value }
void
JsonObjects::add_enum(const cstring& enum_name, const cstring& entry_name,
const unsigned entry_value) {
// look up enum in json by name
Util::JsonObject* enum_json = find_object_by_name(enums, enum_name);
if (enum_json == nullptr) { // first entry in a new enum
enum_json = new Util::JsonObject();
enum_json->emplace("name", enum_name);
auto entries = insert_array_field(enum_json, "entries");
auto entry = new Util::JsonArray();
entry->append(entry_name);
entry->append(entry_value);
entries->append(entry);
enums->append(enum_json);
LOG3("new enum object: " << enum_name << " " << entry_name << " " << entry_value);
} else { // add entry to existing enum
auto entries = enum_json->get("entries")->to<Util::JsonArray>();
auto entry = new Util::JsonArray();
entry->append(entry_name);
entry->append(entry_value);
entries->append(entry);
LOG3("new enum entry: " << enum_name << " " << entry_name << " " << entry_value);
}
}
unsigned
JsonObjects::add_parser(const cstring& name) {
auto parser = new Util::JsonObject();
unsigned id = BMV2::nextId("parser");
parser->emplace("name", name);
parser->emplace("id", id);
parser->emplace("init_state", IR::ParserState::start);
auto parse_states = new Util::JsonArray();
parser->emplace("parse_states", parse_states);
parsers->append(parser);
map_parser.emplace(id, parser);
return id;
}
/// insert parser state into a parser identified by parser_id
/// return the id of the parser state
unsigned
JsonObjects::add_parser_state(const unsigned parser_id, const cstring& state_name) {
if (map_parser.find(parser_id) == map_parser.end())
BUG("parser %1% not found.", parser_id);
auto parser = map_parser[parser_id];
auto states = parser->get("parse_states")->to<Util::JsonArray>();
auto state = new Util::JsonObject();
unsigned state_id = BMV2::nextId("parse_states");
state->emplace("name", state_name);
state->emplace("id", state_id);
auto operations = new Util::JsonArray();
state->emplace("parser_ops", operations);
auto transitions = new Util::JsonArray();
state->emplace("transitions", transitions);
states->append(state);
auto key = new Util::JsonArray();
state->emplace("transition_key", key);
map_parser_state.emplace(state_id, state);
return state_id;
}
void
JsonObjects::add_parser_transition(const unsigned state_id, Util::IJson* transition) {
if (map_parser_state.find(state_id) == map_parser_state.end())
BUG("parser state %1% not found.", state_id);
auto state = map_parser_state[state_id];
auto transitions = state->get("transitions")->to<Util::JsonArray>();
CHECK_NULL(transitions);
auto trans = transition->to<Util::JsonObject>();
CHECK_NULL(trans);
transitions->append(trans);
}
void
JsonObjects::add_parser_op(const unsigned state_id, Util::IJson* op) {
if (map_parser_state.find(state_id) == map_parser_state.end())
BUG("parser state %1% not found.", state_id);
auto state = map_parser_state[state_id];
auto statements = state->get("parser_ops")->to<Util::JsonArray>();
CHECK_NULL(statements);
statements->append(op);
}
void
JsonObjects::add_parser_transition_key(const unsigned state_id, Util::IJson* newKey) {
if (map_parser_state.find(state_id) != map_parser_state.end()) {
auto state = map_parser_state[state_id];
auto keys = state->get("transition_key")->to<Util::JsonArray>();
CHECK_NULL(keys);
auto new_keys = newKey->to<Util::JsonArray>();
for (auto k : *new_keys) {
keys->append(k);
}
}
}
unsigned
JsonObjects::add_action(const cstring& name, Util::JsonArray*& params, Util::JsonArray*& body) {
CHECK_NULL(params);
CHECK_NULL(body);
auto action = new Util::JsonObject();
action->emplace("name", name);
unsigned id = BMV2::nextId("actions");
action->emplace("id", id);
action->emplace("runtime_data", params);
action->emplace("primitives", body);
actions->append(action);
return id;
}
void
JsonObjects::add_extern_attribute(const cstring& name, const cstring& type,
const cstring& value, Util::JsonArray* attributes) {
auto attr = new Util::JsonObject();
attr->emplace("name", name);
attr->emplace("type", type);
attr->emplace("value", value);
attributes->append(attr);
}
void
JsonObjects::add_extern(const cstring& name, const cstring& type,
Util::JsonArray*& attributes) {
auto extn = new Util::JsonObject();
unsigned id = BMV2::nextId("extern_instances");
extn->emplace("name", name);
extn->emplace("id", id);
extn->emplace("type", type);
extn->emplace("attribute_values", attributes);
externs->append(extn);
}
} // namespace BMV2