File: cmdref.py

package info (click to toggle)
yosys 0.52-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 69,796 kB
  • sloc: ansic: 696,955; cpp: 239,736; python: 14,617; yacc: 3,529; sh: 2,175; makefile: 1,945; lex: 697; perl: 445; javascript: 323; tcl: 162; vhdl: 115
file content (625 lines) | stat: -rw-r--r-- 22,733 bytes parent folder | download
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
# based on https://github.com/ofosos/sphinxrecipes/blob/master/sphinxrecipes/sphinxrecipes.py

from __future__ import annotations

import re
from typing import cast

from docutils import nodes
from docutils.nodes import Node, Element, system_message
from docutils.parsers.rst import directives
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx
from sphinx.domains import Domain, Index
from sphinx.domains.std import StandardDomain
from sphinx.environment import BuildEnvironment
from sphinx.roles import XRefRole
from sphinx.directives import ObjectDescription
from sphinx.directives.code import container_wrapper
from sphinx.util.nodes import make_refnode
from sphinx.util.docfields import Field
from sphinx import addnodes

class TocNode(ObjectDescription):    
    def add_target_and_index(
        self,
        name: str,
        sig: str,
        signode: addnodes.desc_signature
    ) -> None:
        idx = ".".join(name.split("::"))
        signode['ids'].append(idx)

    def _object_hierarchy_parts(self, sig_node: addnodes.desc_signature) -> tuple[str, ...]:
        if 'fullname' not in sig_node:
            return ()

        modname = sig_node.get('module')
        fullname = sig_node['fullname']

        if modname:
            return (modname, *fullname.split('::'))
        else:
            return tuple(fullname.split('::'))

    def _toc_entry_name(self, sig_node: addnodes.desc_signature) -> str:
        if not sig_node.get('_toc_parts'):
            return ''

        config = self.env.app.config
        objtype = sig_node.parent.get('objtype')
        *parents, name = sig_node['_toc_parts']
        if config.toc_object_entries_show_parents == 'domain':
            return sig_node.get('tocname', name)
        if config.toc_object_entries_show_parents == 'hide':
            return name
        if config.toc_object_entries_show_parents == 'all':
            return '.'.join(parents + [name])
        return ''

class CommandNode(TocNode):
    """A custom node that describes a command."""

    name = 'cmd'
    required_arguments = 1

    option_spec = {
        'title': directives.unchanged,
        'tags': directives.unchanged
    }

    def handle_signature(self, sig, signode: addnodes.desc_signature):
        signode['fullname'] = sig
        signode += addnodes.desc_addname(text="yosys> help ")
        signode += addnodes.desc_name(text=sig)
        return signode['fullname']

    def add_target_and_index(self, name_cls, sig, signode):
        idx = type(self).name + '-' + sig
        signode['ids'].append(idx)
        if 'noindex' not in self.options:
            name = "{}.{}.{}".format(self.name, type(self).__name__, sig)
            tagmap = self.env.domaindata[type(self).name]['obj2tag']
            tagmap[name] = list(self.options.get('tags', '').split(' '))
            title = self.options.get('title', sig)
            titlemap = self.env.domaindata[type(self).name]['obj2title']
            titlemap[name] = title
            objs = self.env.domaindata[type(self).name]['objects']
            # (name, sig, typ, docname, anchor, prio)
            objs.append((name,
                         sig,
                         type(self).name,
                         self.env.docname,
                         idx,
                         0))

class PropNode(TocNode):
    name = 'prop'
    fieldname = 'props'

    def handle_signature(self, sig: str, signode: addnodes.desc_signature):
        signode['fullname'] = sig
        signode['tocname'] = tocname = sig.split('::')[-1]
        signode += addnodes.desc_name(text=tocname)
        return signode['fullname']

    def add_target_and_index(
        self,
        name: str,
        sig: str,
        signode: addnodes.desc_signature
    ) -> None:
        idx = ".".join(name.split("::"))
        signode['ids'].append(idx)
        if 'noindex' not in self.options:
            tocname: str = signode.get('tocname', name)
            objs = self.env.domaindata[self.domain]['objects']
            # (name, sig, typ, docname, anchor, prio)
            objs.append((name,
                         tocname,
                         type(self).name,
                         self.env.docname,
                         idx,
                         1))

class CellGroupedField(Field):
    """Custom version of GroupedField which doesn't require content."""
    is_grouped = True
    list_type = nodes.bullet_list

    def __init__(self, name: str, names: tuple[str, ...] = (), label: str = None,
                 rolename: str = None, can_collapse: bool = False) -> None:
        super().__init__(name, names, label, True, rolename)
        self.can_collapse = can_collapse

    def make_field(self, types: dict[str, list[Node]], domain: str,
                   items: tuple, env: BuildEnvironment = None,
                   inliner: Inliner = None, location: Node = None) -> nodes.field:
        fieldname = nodes.field_name('', self.label)
        listnode = self.list_type()
        for fieldarg, content in items:
            par = nodes.paragraph()
            if fieldarg:
                par.extend(self.make_xrefs(self.rolename, domain,
                                           fieldarg, nodes.Text,
                                           env=env, inliner=inliner, location=location))

            if len(content) == 1 and (
                    isinstance(content[0], nodes.Text) or
                    (isinstance(content[0], nodes.inline) and len(content[0]) == 1 and
                    isinstance(content[0][0], nodes.Text))):
                par += nodes.Text(' -- ')
                par += content
            listnode += nodes.list_item('', par)

        if len(items) == 1 and self.can_collapse:
            list_item = cast(nodes.list_item, listnode[0])
            fieldbody = nodes.field_body('', list_item[0])
            return nodes.field('', fieldname, fieldbody)

        fieldbody = nodes.field_body('', listnode)
        return nodes.field('', fieldname, fieldbody)

class CellNode(TocNode):
    """A custom node that describes an internal cell."""

    name = 'cell'

    option_spec = {
        'title': directives.unchanged,
        'ports': directives.unchanged,
        'properties': directives.unchanged,
    }

    doc_field_types = [
        CellGroupedField('props', label='Properties', rolename='prop',
                       names=('properties', 'property', 'tag', 'tags'),
                       can_collapse=True),
    ]

    def handle_signature(self, sig: str, signode: addnodes.desc_signature):
        signode['fullname'] = sig
        signode['tocname'] = tocname = sig.split('::')[-1]
        signode += addnodes.desc_addname(text="yosys> help ")
        signode += addnodes.desc_name(text=tocname)
        return signode['fullname']

    def add_target_and_index(
        self,
        name: str,
        sig: str,
        signode: addnodes.desc_signature
    ) -> None:
        idx = ".".join(name.split("::"))
        signode['ids'].append(idx)
        if 'noindex' not in self.options:
            tocname: str = signode.get('tocname', name)
            title: str = self.options.get('title', sig)
            titlemap = self.env.domaindata[self.domain]['obj2title']
            titlemap[name] = title
            props = self.options.get('properties', '')
            if props:
                propmap = self.env.domaindata[self.domain]['obj2prop']
                propmap[name] = props.split(' ')
            objs = self.env.domaindata[self.domain]['objects']
            # (name, sig, typ, docname, anchor, prio)
            objs.append((name,
                         tocname,
                         type(self).name,
                         self.env.docname,
                         idx,
                         0))
            
    def transform_content(self, contentnode: addnodes.desc_content) -> None:
        # Add the cell title to the body
        if 'title' in self.options:
            titlenode = nodes.paragraph()
            titlenode += nodes.strong()
            titlenode[-1] += nodes.Text(self.options['title'])
            contentnode.insert(0, titlenode)

class CellSourceNode(TocNode):
    """A custom code block for including cell source."""

    name = 'cellsource'

    option_spec = {
        "source": directives.unchanged_required,
        "language": directives.unchanged_required,
        'lineno-start': int,
    }

    def handle_signature(
        self,
        sig,
        signode: addnodes.desc_signature
    ) -> str:
        language = self.options.get('language')
        signode['fullname'] = sig
        signode['tocname'] = f"{sig.split('::')[-2]} {language}"
        signode += addnodes.desc_name(text="Simulation model")
        signode += addnodes.desc_sig_space()
        signode += addnodes.desc_addname(text=f'({language})')
        return signode['fullname']

    def run(self) -> list[Node]:
        """Override run to parse content as a code block"""
        if ':' in self.name:
            self.domain, self.objtype = self.name.split(':', 1)
        else:
            self.domain, self.objtype = '', self.name
        self.indexnode = addnodes.index(entries=[])

        node = addnodes.desc()
        node.document = self.state.document
        source, line = self.get_source_info()
        if line is not None:
            line -= 1
        self.state.document.note_source(source, line)
        node['domain'] = self.domain
        # 'desctype' is a backwards compatible attribute
        node['objtype'] = node['desctype'] = self.objtype
        node['noindex'] = noindex = ('noindex' in self.options)
        node['noindexentry'] = ('noindexentry' in self.options)
        node['nocontentsentry'] = ('nocontentsentry' in self.options)
        if self.domain:
            node['classes'].append(self.domain)
        node['classes'].append(node['objtype'])

        self.names = []
        signatures = self.get_signatures()
        for sig in signatures:
            # add a signature node for each signature in the current unit
            # and add a reference target for it
            signode = addnodes.desc_signature(sig, '')
            self.set_source_info(signode)
            node.append(signode)
            try:
                # name can also be a tuple, e.g. (classname, objname);
                # this is strictly domain-specific (i.e. no assumptions may
                # be made in this base class)
                name = self.handle_signature(sig, signode)
            except ValueError:
                # signature parsing failed
                signode.clear()
                signode += addnodes.desc_name(sig, sig)
                continue  # we don't want an index entry here
            finally:
                # Private attributes for ToC generation. Will be modified or removed
                # without notice.
                if self.env.app.config.toc_object_entries:
                    signode['_toc_parts'] = self._object_hierarchy_parts(signode)
                    signode['_toc_name'] = self._toc_entry_name(signode)
                else:
                    signode['_toc_parts'] = ()
                    signode['_toc_name'] = ''
            if name not in self.names:
                self.names.append(name)
                if not noindex:
                    # only add target and index entry if this is the first
                    # description of the object with this name in this desc block
                    self.add_target_and_index(name, sig, signode)
        
        # handle code
        code = '\n'.join(self.content)
        literal: Element = nodes.literal_block(code, code)
        if 'lineno-start' in self.options:
            literal['linenos'] = True
            literal['highlight_args'] = {
                'linenostart': self.options['lineno-start']
            }
        literal['classes'] += self.options.get('class', [])
        literal['language'] = self.options.get('language')
        literal = container_wrapper(self, literal, self.options.get('source'))

        return [self.indexnode, node, literal]

class CellGroupNode(TocNode):
    name = 'cellgroup'

    option_spec = {
        'caption': directives.unchanged,
    }

    def add_target_and_index(self, name: str, sig: str, signode: addnodes.desc_signature) -> None:
        if self.options.get('caption', ''):
            super().add_target_and_index(name, sig, signode)

    def handle_signature(
        self,
        sig,
        signode: addnodes.desc_signature
    ) -> str:
        signode['fullname'] = fullname = sig
        caption = self.options.get("caption", fullname)
        if caption:
            signode['tocname'] = caption
            signode += addnodes.desc_name(text=caption)
        return fullname

class TagIndex(Index):
    """A custom directive that creates a tag matrix."""
    
    name = 'tag'
    localname = 'Tag Index'
    shortname = 'Tag'
    
    def __init__(self, *args, **kwargs):
        super(TagIndex, self).__init__(*args, **kwargs)

    def generate(self, docnames=None):
        """Return entries for the index given by *name*.  If *docnames* is
        given, restrict to entries referring to these docnames.
        The return value is a tuple of ``(content, collapse)``, where
        * collapse* is a boolean that determines if sub-entries should
        start collapsed (for output formats that support collapsing
        sub-entries).
        *content* is a sequence of ``(letter, entries)`` tuples, where *letter*
        is the "heading" for the given *entries*, usually the starting letter.
        *entries* is a sequence of single entries, where a single entry is a
        sequence ``[name, subtype, docname, anchor, extra, qualifier, descr]``.
        The items in this sequence have the following meaning:
        - `name` -- the name of the index entry to be displayed
        - `subtype` -- sub-entry related type:
          0 -- normal entry
          1 -- entry with sub-entries
          2 -- sub-entry
        - `docname` -- docname where the entry is located
        - `anchor` -- anchor for the entry within `docname`
        - `extra` -- extra info for the entry
        - `qualifier` -- qualifier for the description
        - `descr` -- description for the entry
        Qualifier and description are not rendered e.g. in LaTeX output.
        """

        content = {}

        objs = {name: (dispname, typ, docname, anchor)
                for name, dispname, typ, docname, anchor, prio
                in self.domain.get_objects()}
        
        tmap = {}
        tags = self.domain.data[f'obj2{self.name}']
        for name, tags in tags.items():
            for tag in tags:
                tmap.setdefault(tag,[])
                tmap[tag].append(name)
            
        for tag in tmap.keys():
            lis = content.setdefault(tag, [])
            objlis = tmap[tag]
            for objname in objlis:
                dispname, typ, docname, anchor = objs[objname]
                lis.append((
                    dispname, 0, docname,
                    anchor,
                    docname, '', typ
                ))
        ret = [(k, v) for k, v in sorted(content.items())]

        return (ret, True)

class CommandIndex(Index):    
    name = 'cmd'
    localname = 'Command Reference'
    shortname = 'Command'
    
    def __init__(self, *args, **kwargs):
        super(CommandIndex, self).__init__(*args, **kwargs)

    def generate(self, docnames=None):
        """Return entries for the index given by *name*.  If *docnames* is
        given, restrict to entries referring to these docnames.
        The return value is a tuple of ``(content, collapse)``, where
        * collapse* is a boolean that determines if sub-entries should
        start collapsed (for output formats that support collapsing
        sub-entries).
        *content* is a sequence of ``(letter, entries)`` tuples, where *letter*
        is the "heading" for the given *entries*, usually the starting letter.
        *entries* is a sequence of single entries, where a single entry is a
        sequence ``[name, subtype, docname, anchor, extra, qualifier, descr]``.
        The items in this sequence have the following meaning:
        - `name` -- the name of the index entry to be displayed
        - `subtype` -- sub-entry related type:
          0 -- normal entry
          1 -- entry with sub-entries
          2 -- sub-entry
        - `docname` -- docname where the entry is located
        - `anchor` -- anchor for the entry within `docname`
        - `extra` -- extra info for the entry
        - `qualifier` -- qualifier for the description
        - `descr` -- description for the entry
        Qualifier and description are not rendered e.g. in LaTeX output.
        """

        content = {}
        items = ((name, dispname, typ, docname, anchor)
                 for name, dispname, typ, docname, anchor, prio
                 in self.domain.get_objects()
                 if typ == self.name)
        items = sorted(items, key=lambda item: item[0])
        for name, dispname, typ, docname, anchor in items:
            lis = content.setdefault(self.shortname, [])
            lis.append((
                dispname, 0, docname,
                anchor,
                '', '', typ
            ))
        ret = [(k, v) for k, v in sorted(content.items())]

        return (ret, True)

class CellIndex(CommandIndex):
    name = 'cell'
    localname = 'Internal cell reference'
    shortname = 'Internal cell'

class PropIndex(TagIndex):
    """A custom directive that creates a properties matrix."""
    
    name = 'prop'
    localname = 'Property Index'
    shortname = 'Prop'
    fieldname = 'props'

    def generate(self, docnames=None):
        content = {}

        cells = {name: (dispname, docname, anchor)
                for name, dispname, typ, docname, anchor, _
                in self.domain.get_objects()
                if typ == 'cell'}
        props = {name: (dispname, docname, anchor)
                for name, dispname, typ, docname, anchor, _
                in self.domain.get_objects()
                if typ == 'prop'}

        tmap: dict[str, list[str]] = {}
        tags: dict[str, list[str]] = self.domain.data[f'obj2{self.name}']
        for name, tags in tags.items():
            for tag in tags:
                tmap.setdefault(tag,[])
                tmap[tag].append(name)

        for tag in sorted(tmap.keys()):
            test = re.match(r'^(\w+[_-])', tag)
            tag_prefix = test.group(1)
            lis = content.setdefault(tag_prefix, [])
            try:
                dispname, docname, anchor = props[tag]
            except KeyError:
                dispname = tag
                docname = anchor = ''
            lis.append((
                dispname, 1, docname,
                anchor,
                '', '', docname or 'unavailable'
            ))
            objlis = tmap[tag]
            for objname in sorted(objlis):
                dispname, docname, anchor = cells[objname]
                lis.append((
                    dispname, 2, docname,
                    anchor,
                    '', '', docname
                ))
        ret = [(k, v) for k, v in sorted(content.items())]

        return (ret, True)

class CommandDomain(Domain):
    name = 'cmd'
    label = 'Yosys commands'

    roles = {
        'ref': XRefRole()
    }

    directives = {
        'def': CommandNode,
    }

    indices = {
        CommandIndex,
        TagIndex
    }

    initial_data = {
        'objects': [],      # object list
        'obj2tag': {},      # name -> tags
        'obj2title': {},    # name -> title
    }

    def get_full_qualified_name(self, node):
        """Return full qualified name for a given node"""
        return "{}.{}.{}".format(type(self).name,
                                 type(node).__name__,
                                 node.arguments[0])

    def get_objects(self):
        for obj in self.data['objects']:
            yield(obj)

    def resolve_xref(self, env, fromdocname, builder, typ,
                     target, node, contnode):
        
        match = [(docname, anchor, name)
                 for name, sig, typ, docname, anchor, prio
                 in self.get_objects() if sig == target]

        if match:
            todocname = match[0][0]
            targ = match[0][1]
            qual_name = match[0][2]
            title = self.data['obj2title'].get(qual_name, targ)
            
            return make_refnode(builder,fromdocname,todocname,
                                targ, contnode, title)
        else:
            print(f"Missing ref for {target} in {fromdocname} ")
            return None
        
class CellDomain(CommandDomain):
    name = 'cell'
    label = 'Yosys internal cells'

    roles = CommandDomain.roles.copy()
    roles.update({
        'prop': XRefRole()
    })

    directives = {
        'def': CellNode,
        'defprop': PropNode,
        'source': CellSourceNode,
        'group': CellGroupNode,
    }

    indices = {
        CellIndex,
        PropIndex
    }

    initial_data = {
        'objects': [],      # object list
        'obj2prop': {},     # name -> properties
        'obj2title': {},    # name -> title
    }

    def get_objects(self):
        for obj in self.data['objects']:
            yield(obj)

def autoref(name, rawtext: str, text: str, lineno, inliner: Inliner,
            options=None, content=None):
    role = 'cell:ref' if text[0] == '$' else 'cmd:ref'
    if text.startswith("help ") and text.count(' ') == 1:
        _, cmd = text.split(' ', 1)
        text = f'{text} <{cmd}>'
    return inliner.interpreted(rawtext, text, role, lineno)

def setup(app: Sphinx):
    app.add_domain(CommandDomain)
    app.add_domain(CellDomain)

    StandardDomain.initial_data['labels']['commandindex'] =\
        ('cmd-cmd', '', 'Command Reference')
    StandardDomain.initial_data['labels']['tagindex'] =\
        ('cmd-tag', '', 'Tag Index')
    StandardDomain.initial_data['labels']['cellindex'] =\
        ('cell-cell', '', 'Internal cell reference')
    StandardDomain.initial_data['labels']['propindex'] =\
        ('cell-prop', '', 'Property Index')

    StandardDomain.initial_data['anonlabels']['commandindex'] =\
        ('cmd-cmd', '')
    StandardDomain.initial_data['anonlabels']['tagindex'] =\
        ('cmd-tag', '')
    StandardDomain.initial_data['anonlabels']['cellindex'] =\
        ('cell-cell', '')
    StandardDomain.initial_data['anonlabels']['propindex'] =\
        ('cell-prop', '')

    app.add_role('autoref', autoref)
    
    return {'version': '0.2'}