summaryrefslogtreecommitdiff
path: root/devtools/tablegen.py
blob: b8a3ed3e1055ea1650d0f018caf07bd335825661 (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/usr/bin/env python
#
# This tool is intended to automate away the drudgery in bring up support
# for a new AIS message type.  It parses the tabular description of a message
# and generates various useful code snippets from that.  It can also be used to
# correct offsets in the tables themselves.
#
# Requires the AIVDM.txt file on standard input. Takes a single argument,
# which must match a string in a //: Type comment.  Things you can generate:
#
# * -t: A corrected version of the table.  It will redo all the offsets to be
#   in conformance with the bit widths. (The other options rely only on the
#   bit widths). If the old and new tables are different, an error message
#   describing the corrections will be emitted to standard error.
#
# * -s: A structure definition capturing the message info, with member
#   names extracted from the table and types computed from it.
#
# * -c: Bit-extraction code for the AIVDM driver.  Grind out the right sequence
#   of UBITS, SBITS, and UCHARS macros, and assignments to structure members,
#   guaranteed correct if the table offsets and widths are.
#
# * -d: Code to dump the contents of the unpacked message structure as JSON. If
#   the structure has float members, you'll get an if/then/else  guarded by
#   the scaled flag.
#
# * -r: A Python initializer stanza for jsongen.py, which is in turn used to
#   generate the specification structure for a JSON parse that reads JSON
#   into an instance of the message structure.
#
# * -a: Generate all of -s, -d, -c, and -r, and -t, not to stdout but to
#   files named with 'tablegen' as a distinguishing part of the stem.
#   The stem name can be overridden with the -o option.
#
# This generates almost all the code required to support a new message type.
# It's not quite "Look, ma, no handhacking!" You'll need to add default
# values to the Python stanza. If the structure definition contains character
# arrays, you'll have to fill in the dimensions by hand.  You'll need to add
# a bit of glue to ais_json.c so that json_ais_read() actually calls the parser
# handing it the specification structure as a control argument.
#
# The -a, -c, -s, -d, and -r modes all take an argument, which should be a
# structure reference prefix to be prepended (before a dot) to each fieldname.
# Usually you'll need this to look something like "ais->typeN", but it could be
# "ais->typeN.FOO" if the generated code has to operate on a union member
# inside a type 6 or 8, or something similar.
#
# The -S and -E options allow you to generate code only for a specified span
# of fields in the table.  This may be useful for dealing with groups of
# messages that have a common head section.
#
# This code interprets magic comments in the input
#
# //: Type
#    The token following "Type" is the name of the table
# //: xxxx vocabulary
#    A subtable describing a controlled vocabulary for field xxxx in the
#    preceding table.
#
# TO-DO: generate code for ais.py.
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division

import getopt
import sys


def correct_table(wfp):
    # Writes the corrected table.
    print("Total bits:", base, file=sys.stderr)
    for (i, t) in enumerate(table):
        if offsets[i].strip():
            print("|" + offsets[i] + t[owidth+1:].rstrip(), file=wfp)
        else:
            print(t.rstrip(), file=wfp)


def make_driver_code(wfp):
    # Writes calls to bit-extraction macros.
    # Requires UBITS, SBITS, UCHARS to act as they do in the AIVDM driver.
    # Also relies on bitlen to be the message bit length, and i to be
    # available as abn index variable.
    record = after is None
    arrayname = None
    base = '\t'
    step = " " * 4
    indent = base
    for (i, t) in enumerate(table):
        if '|' in t:
            fields = [s.strip() for s in t.split('|')]
            width = fields[2]
            name = fields[4]
            ftype = fields[5]
            if after == name:
                record = True
                continue
            if before == name:
                record = False
                continue
            if not record:
                continue
            if ftype == 'x':
                print("\t/* skip %s bit%s */" % (width,
                      ["", "s"][width > '1']), file=wfp)
                continue
            if ftype[0] == 'a':
                arrayname = name
                explicit = ftype[1] == '^'
                print('#define ARRAY_BASE %s' % offsets[i].strip(), file=wfp)
                print('#define ELEMENT_SIZE %s' % trailing, file=wfp)
                if explicit:
                    lengthfield = last
                    print(indent + "for (i = 0; i < %s; i++) {" % lengthfield,
                          file=wfp)
                else:
                    lengthfield = "n" + arrayname
                    print(indent + "for (i = 0; ARRAY_BASE + "
                          "(ELEMENT_SIZE*i) < bitlen; i++) {", file=wfp)
                indent += step
                print(indent + "int a = ARRAY_BASE + (ELEMENT_SIZE*i);",
                      file=wfp)
                continue
            offset = offsets[i].split('-')[0]
            if arrayname:
                target = "%s.%s[i].%s" % (structnme, arrayname, name)
                offset = "a + " + offset
            else:
                target = "%s.%s" % (structname, name)
            if ftype[0].lower() in ('u', 'i', 'e'):
                print(indent + "%s\t= %sBITS(%s, %s);" %
                      (target,
                       {'u': 'U', 'e': 'U', 'i': 'S'}[ftype[0].lower()],
                       offset, width), file=wfp)
            elif ftype == 't':
                print(indent + "UCHARS(%s, %s);" % (offset, target), file=wfp)
            elif ftype == 'b':
                print(indent + "%s\t= (bool)UBITS(%s, 1);" % (target, offset),
                      file=wfp)
            else:
                print(indent + "/* %s bits of type %s */" %
                      (width, ftype), file=wfp)
            last = name
    if arrayname:
        indent = base
        print(indent + "}", file=wfp)
        if not explicit:
            print(indent + "%s.%s = ind;" %
                  (structname, lengthfield), file=wfp)
        print("#undef ARRAY_BASE", file=wfp)
        print("#undef ELEMENT_SIZE", file=wfp)


def make_structure(wfp):
    # Write a structure definition correponding to the table.
    global structname
    record = after is None
    baseindent = 8
    step = 4
    inwards = step
    arrayname = None

    def tabify(n):
        return ('\t' * (n // 8)) + (" " * (n % 8))

    print(tabify(baseindent) + "struct {", file=wfp)
    for (i, t) in enumerate(table):
        if '|' in t:
            fields = [s.strip() for s in t.split('|')]
            width = fields[2]
            description = fields[3].strip()
            name = fields[4]
            ftype = fields[5]
            if after == name:
                record = True
                continue
            if before == name:
                record = False
                continue
            if ftype == 'x' or not record:
                continue
            if ftype[0] == 'a':
                arrayname = name
                if ftype[1] == '^':
                    lengthfield = last
                    ftype = ftype[1:]
                else:
                    lengthfield = "n%s" % arrayname
                    print(tabify(baseindent + inwards) +
                          "signed int %s;" % lengthfield, file=wfp)
                if arrayname.endswith("s"):
                    typename = arrayname[:-1]
                else:
                    typename = arrayname
                print(tabify(baseindent + inwards) + "struct %s_t {" %
                      typename, file=wfp)
                inwards += step
                arraydim = ftype[1:]
                continue
            elif ftype == 'u' or ftype == 'e' or ftype[0] == 'U':
                decl = "unsigned int %s;\t/* %s */" % (name, description)
            elif ftype == 'i' or ftype[0] == 'I':
                decl = "signed int %s;\t/* %s */" % (name, description)
            elif ftype == 'b':
                decl = "bool %s;\t/* %s */" % (name, description)
            elif ftype == 't':
                stl = int(width) // 6
                decl = "char %s[%d+1];\t/* %s */" % (name, stl, description)
            else:
                decl = "/* %s bits of type %s */" % (width, ftype)
            print(tabify(baseindent + inwards) + decl, file=wfp)
        last = name
    if arrayname:
        inwards -= step
        print(tabify(baseindent + inwards) + "} %s[%s];"
              % (arrayname, arraydim), file=wfp)
    if "->" in structname:
        typename = structname.split("->")[1]
    if "." in typename:
        structname = structname.split(".")[1]
    print(tabify(baseindent) + "} %s;" % typename, file=wfp)


def make_json_dumper(wfp):
    # Write the skeleton of a JSON dump corresponding to the table.
    # Also, if there are subtables, some initializers
    if subtables:
        for (name, lines) in subtables:
            wfp.write("    const char *%s_vocabulary[] = {\n" % name)
            for line in lines:
                value = line[1]
                if value.endswith(" (default)"):
                    value = value[:-10]
                wfp.write('        "%s",\n' % value)
            wfp.write("    };\n")
            wfp.write('#define DISPLAY_%s(n) (((n) < '
                      '(unsigned int)NITEMS(%s_vocabulary)) ? '
                      '%s_vocabulary[n] : "INVALID %s")\n' %
                      (name.upper(), name, name, name.upper()))
        wfp.write("\n")
    record = after is None
    # Elements of each tuple type except 'a':
    #   1. variable name,
    #   2. unscaled printf format
    #   3. wrapper for unscaled variable reference
    #   4. scaled printf format
    #   5. wrapper for scaled variable reference
    # Elements of 'a' tuple:
    #   1. Name of array field
    #   2. None
    #   3. None
    #   4. None
    #   5. Name of length field
    tuples = []
    vocabularies = [x[0] for x in subtables]
    for (i, t) in enumerate(table):
        if '|' in t:
            fields = [s.strip() for s in t.split('|')]
            name = fields[4]
            ftype = fields[5]
            if after == name:
                record = True
                continue
            if before == name:
                record = False
                continue
            if ftype == 'x' or not record:
                continue
            fmt = r'\"%s\":' % name
            fmt_text = r'\"%s_text\":' % name
            if ftype == 'u':
                tuples.append((name,
                               fmt+"%u", "%s",
                               None, None))
            elif ftype == 'e':
                tuples.append((name,
                               fmt+"%u", "%s",
                               None, None))
                if vocabularies:
                    this = vocabularies.pop(0)
                    ref = "DISPLAY_%s(%%s)" % (this.upper())
                else:
                    ref = 'FOO[%s]'
                tuples.append((name,
                               fmt_text+r"\"%s\"", ref,
                               None, None))
            elif ftype == 'i':
                tuples.append((name,
                               fmt+"%d", "%s",
                               None, None))
            elif ftype == 't':
                tuples.append((name,
                               fmt+r'\"%s\"', "%s",
                               None, None))
            elif ftype == 'b':
                tuples.append((name,
                               fmt+r'\"%s\"', "JSON_BOOL(%s)",
                               None, None))
            elif ftype[0] == 'd':
                print("Cannot generate code for data members", file=sys.stderr)
                sys.exit(1)
            elif ftype[0] == 'U':
                tuples.append((name,
                               fmt+"%u", "%s",
                               fmt+"%%.%sf" % ftype[1], '%s / SCALE'))
            elif ftype[0] == 'I':
                tuples.append((name,
                               fmt+"%d", "%s",
                               fmt+"%%.%sf" % ftype[1], '%s / SCALE'))
            elif ftype[0] == 'a':
                ftype = ftype[1:]
                if ftype[0] == '^':
                    lengthfield = last
                else:
                    lengthfield = "n" + name
                tuples.append((name, None, None, None, lengthfield))
            else:
                print("Unknown type code", ftype, file=sys.stderr)
                sys.exit(1)
        last = name
    startspan = 0

    def scaled(i):
        return tuples[i][3] is not None

    def tslice(e, i):
        return [x[i] for x in tuples[startspan:e+1]]

    base = " " * 8
    step = " " * 4
    inarray = None
    header = "(void)snprintf(buf + strlen(buf), buflen - strlen(buf),"
    for (i, (var, uf, uv, sf, sv)) in enumerate(tuples):
        if uf is not None:
            print(base + "for (i = 0; i < %s.%s; i++) {" % (structname, sv),
                  file=wfp)
            inarray = var
            base = " " * 12
            startspan = i+1
            continue
        # At end of tuples, or if scaled flag changes, or if next op is array,
        # flush out dump code for a span of fields.
        if i+1 == len(tuples):
            endit = '}",'
        elif tuples[i+1][1] is not None:
            endit = r',\"%s\":[",' % tuples[i+1][0]
        elif scaled(i) != scaled(i + 1):
            endit = ',",'
        else:
            endit = None
        if endit:
            if not scaled(i):
                print(base + header, file=wfp)
                if inarray:
                    prefix = '{"'
                else:
                    prefix = '"'
                print(base + step + prefix + ','.join(tslice(i, 1)) + endit,
                      file=wfp)
                for (j, t) in enumerate(tuples[startspan:i+1]):
                    if inarray:
                        ref = structname + "." + inarray + "[i]." + t[0]
                    else:
                        ref = structname + "." + t[0]
                    wfp.write(base + step + t[2] % ref)
                    if j == i - startspan:
                        wfp.write(");\n")
                    else:
                        wfp.write(",\n")
            else:
                print(base + "if (scaled)", file=wfp)
                print(base + step + header, file=wfp)
                print(base + step * 2 + '"' + ','.join(tslice(i, 3)) + endit,
                      file=wfp)
                for (j, t) in enumerate(tuples[startspan:i+1]):
                    if inarray:
                        ref = structname + "." + inarray + "[i]." + t[0]
                    else:
                        ref = structname + "." + t[0]
                    wfp.write(base + step*2 + t[4] % ref)
                    if j == i - startspan:
                        wfp.write(");\n")
                    else:
                        wfp.write(",\n")
                print(base + "else", file=wfp)
                print(base + step + header, file=wfp)
                print(base + step * 2 + '"' + ','.join(tslice(i, 1)) + endit,
                      file=wfp)
                for (j, t) in enumerate(tuples[startspan:i+1]):
                    if inarray:
                        ref = structname + "." + inarray + "[i]." + t[0]
                    else:
                        ref = structname + "." + t[0]
                    wfp.write(base + step*2 + t[2] % ref)
                    if j == i - startspan:
                        wfp.write(");\n")
                    else:
                        wfp.write(",\n")
            startspan = i+1
    # If we were looking at a trailing array, close scope
    if inarray:
        base = " " * 8
        print(base + "}", file=wfp)
        print(base + "if (buf[strlen(buf)-1] == ',')", file=wfp)
        print(base + step + r"buf[strlen(buf)-1] = '\0';", file=wfp)
        print(base + "(void)strlcat(buf, \"]}\", buflen - strlen(buf));",
              file=wfp)


def make_json_generator(wfp):
    # Write a stanza for jsongen.py.in describing how to generate a
    # JSON parser initializer from this table. You need to fill in
    # __INITIALIZER__ and default values after this is generated.
    extra = ""
    arrayname = None
    record = after is None
    print('''\
    {
    "initname" : "__INITIALIZER__",
    "headers": ("AIS_HEADER",),
    "structname": "%s",
    "fieldmap":(
        # fieldname    type        default''' % (structname,), file=wfp)
    for (i, t) in enumerate(table):
        if '|' in t:
            fields = [s.strip() for s in t.split('|')]
            name = fields[4]
            ftype = fields[5]
            if after == name:
                record = True
                continue
            if before == name:
                record = False
                continue
            if ftype == 'x' or not record:
                continue
            if ftype[0] == 'a':
                arrayname = name
                if arrayname.endswith("s"):
                    typename = arrayname[:-1]
                else:
                    typename = arrayname
                readtype = 'array'
                dimension = ftype[1:]
                if dimension[0] == '^':
                    lengthfield = last
                    dimension = dimension[1:]
                else:
                    lengthfield = "n" + arrayname
                extra = " " * 8
                print("        ('%s',%s 'array', (" %
                      (arrayname, " "*(10-len(arrayname))), file=wfp)
                print("            ('%s_t', '%s', (" % (typename, lengthfield),
                      file=wfp)
            else:
                # Depends on the assumption that the read code
                # always sees unscaled JSON.
                readtype = {
                    'u': "uinteger",
                    'U': "uinteger",
                    'e': "uinteger",
                    'i': "integer",
                    'I': "integer",
                    'b': "boolean",
                    't': "string",
                    'd': "string",
                    }[ftype[0]]
                typedefault = {
                    'u': "'PUT_DEFAULT_HERE'",
                    'U': "'PUT_DEFAULT_HERE'",
                    'e': "'PUT DEFAULT HERE'",
                    'i': "'PUT_DEFAULT_HERE'",
                    'I': "'PUT_DEFAULT_HERE'",
                    'b': "\'false\'",
                    't': "None",
                    }[ftype[0]]
                namedefaults = {
                    "month": "'0'",
                    "day": "'0'",
                    "hour": "'24'",
                    "minute": "'60'",
                    "second": "'60'",
                    }
                default = namedefaults.get(name) or typedefault
                print(extra + "        ('%s',%s '%s',%s %s)," %
                      (name, " "*(10-len(name)), readtype,
                       " "*(8-len(readtype)), default), file=wfp)
                if ftype[0] == 'e':
                    print(extra + "        ('%s_text',%s'ignore',   None)," %
                          (name, " "*(6-len(name))), file=wfp)

            last = name
    if arrayname:
        print("                    )))),", file=wfp)
    print("        ),", file=wfp)
    print("    },", file=wfp)


if __name__ == '__main__':
    try:
        (options, arguments) = getopt.getopt(sys.argv[1:], "a:tc:s:d:S:E:r:o:")
    except getopt.GetoptError as msg:
        print("tablecheck.py: " + str(msg))
        raise SystemExit(1)
    generate = maketable = makestruct = makedump = readgen = all = False
    after = before = None
    filestem = "tablegen"
    for (switch, val) in options:
        if switch == '-a':
            all = True
            structname = val
        elif switch == '-c':
            generate = True
            structname = val
        elif switch == '-s':
            makestruct = True
            structname = val
        elif switch == '-t':
            maketable = True
        elif switch == '-d':
            makedump = True
            structname = val
        elif switch == '-r':
            readgen = True
            structname = val
        elif switch == '-S':
            after = val
        elif switch == '-E':
            before = val
        elif switch == '-o':
            filestem = val

    if ((not generate and not maketable and not makestruct and
         not makedump and not readgen and not all)):
        print("tablecheck.py: no mode selected", file=sys.stderr)
        sys.exit(1)

    # First, read in the table.
    # Sets the following:
    #    table - the table lines
    #    widths - array of table widths
    #    ranges - array of table offsets
    #    trailing - bit length of the table or trailing array element
    #    subtables - list of following vocabulary tables.
    tablename = arguments[0]
    table = []
    ranges = []
    subtables = []
    state = 0
    for line in sys.stdin:
        if state == 0 and line.startswith("//: Type") and tablename in line:
            state = 1
            continue
        elif state == 1:		# Found table tag
            if line.startswith("|="):
                state = 2
                continue
        elif state == 2:		# Found table header
            if line.startswith("|="):
                state = 3
                continue
            elif line[0] == '|':
                fields = line.split("|")
                trailing = fields[1]
                ranges.append(fields[1].strip())
                fields[1] = " " * len(fields[1])
                line = "|".join(fields)
            else:
                ranges.append('')
            table.append(line)
            continue
        elif state == 3:		# Found table end
            state = 4
            continue
        elif state == 4:		# Skipping until subsidiary table
            if line.startswith("//:") and "vocabulary" in line:
                subtable_name = line.split()[1]
                subtable_content = []
                state = 5
        elif state == 5:		# Seen subtable header
            if line.startswith("|="):
                state = 6
                continue
        elif state == 6:		# Parsing subtable content
            if line.startswith("|="):
                subtables.append((subtable_name, subtable_content))
                state = 4
                continue
            elif line[0] == '|':
                subtable_content.append(
                    [f.strip() for f in line[1:].strip().split("|")])
            continue
    if state == 0:
        print("Can't find named table.", file=sys.stderr)
        sys.exit(1)
    elif state < 3:
        print("Ill-formed table (in state %d)." % state, file=sys.stderr)
        sys.exit(1)
    table = table[1:]
    ranges = ranges[1:]
    widths = []
    for line in table:
        fields = line.split('|')
        if '|' not in line:        # Continuation line
            widths.append('')
        elif fields[5][0] == 'a':     # Array boundary indicator
            widths.append(None)
        else:
            widths.append(fields[2].strip())
    if '-' in trailing:
        trailing = trailing.split('-')[1]
    trailing = str(int(trailing)+1)

    # Compute offsets for an AIVDM message breakdown, given the bit widths.
    offsets = []
    base = 0
    corrections = False
    for w in widths:
        if w is None:
            offsets.append(repr(base))
            base = 0
        elif w == '':
            offsets.append('')
        else:
            w = int(w)
            offsets.append("%d-%d" % (base, base + w - 1))
            base += w
    if [p for p in zip(ranges, offsets) if p[0] != p[1]]:
        corrections = True
        print("Offset corrections:")
        for (old, new) in zip(ranges, offsets):
            if old != new:
                print(old, "->", new, file=sys.stderr)
    owidth = max(*list(map(len, offsets)))
    for (i, off) in enumerate(offsets):
        offsets[i] += " " * (owidth - len(offsets[i]))

    # Here's where we generate useful output.
    if all:
        if corrections:
            correct_table(open(filestem + ".txt", "w"))
        make_driver_code(open(filestem + ".c", "w"))
        make_structure(open(filestem + ".h", "w"))
        make_json_dumper(open(filestem + "_json.c", "w"))
        make_json_generator(open(filestem + ".py", "w"))
    elif maketable:
        correct_table(sys.stdout)
    elif generate:
        make_driver_code(sys.stdout)
    elif makestruct:
        make_structure(sys.stdout)
    elif makedump:
        make_json_dumper(sys.stdout)
    elif readgen:
        make_json_generator(sys.stdout)
# end