1f08c3bdfSopenharmony_ci#!/usr/bin/env python
2f08c3bdfSopenharmony_ci# SPDX_License-Identifier: MIT
3f08c3bdfSopenharmony_ci#
4f08c3bdfSopenharmony_ci# Copyright (C) 2018 Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
5f08c3bdfSopenharmony_ci#
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci"""
8f08c3bdfSopenharmony_ci///
9f08c3bdfSopenharmony_ci// To document the instructions used in the intermediate representation
10f08c3bdfSopenharmony_ci// a new domain is defined: 'ir' with a directive::
11f08c3bdfSopenharmony_ci//
12f08c3bdfSopenharmony_ci//	.. op: <OP_NAME>
13f08c3bdfSopenharmony_ci//		<description of OP_NAME>
14f08c3bdfSopenharmony_ci//		...
15f08c3bdfSopenharmony_ci//
16f08c3bdfSopenharmony_ci// This is equivalent to using a definition list but with the name
17f08c3bdfSopenharmony_ci// also placed in the index (with 'IR instruction' as descriptions).
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci"""
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ciimport docutils
22f08c3bdfSopenharmony_ciimport sphinx
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ciclass IROpDirective(docutils.parsers.rst.Directive):
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	# use the first line of content as the argument, this allow
27f08c3bdfSopenharmony_ci	# to not have to write a blanck line after the directive
28f08c3bdfSopenharmony_ci	final_argument_whitespace = True
29f08c3bdfSopenharmony_ci	required_argument = 0
30f08c3bdfSopenharmony_ci	#optional_arguments = 0
31f08c3bdfSopenharmony_ci	has_content = True
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	objtype = None
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	def run(self):
36f08c3bdfSopenharmony_ci		self.env = self.state.document.settings.env
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci		source = self.state.document
39f08c3bdfSopenharmony_ci		lineno = self.lineno
40f08c3bdfSopenharmony_ci		text = self.content
41f08c3bdfSopenharmony_ci		name = text[0]
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci		node = docutils.nodes.section()
44f08c3bdfSopenharmony_ci		node['ids'].append(name)
45f08c3bdfSopenharmony_ci		node.document = source
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci		index = '.. index:: pair: %s; IR instruction' % name
48f08c3bdfSopenharmony_ci		content = docutils.statemachine.ViewList()
49f08c3bdfSopenharmony_ci		content.append(index, source, lineno)
50f08c3bdfSopenharmony_ci		content.append(''   , source, lineno)
51f08c3bdfSopenharmony_ci		content.append(name , source, lineno)
52f08c3bdfSopenharmony_ci		content.append(''   , source, lineno)
53f08c3bdfSopenharmony_ci		self.state.nested_parse(content, self.content_offset, node)
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci		defnode = docutils.nodes.definition()
56f08c3bdfSopenharmony_ci		self.state.nested_parse(text[1:], self.content_offset, defnode)
57f08c3bdfSopenharmony_ci		node.append(defnode)
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci		return [node]
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ciclass IRDomain(sphinx.domains.Domain):
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci    """IR domain."""
64f08c3bdfSopenharmony_ci    name = 'ir'
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cidef setup(app):
67f08c3bdfSopenharmony_ci	app.add_domain(IRDomain)
68f08c3bdfSopenharmony_ci	app.add_directive_to_domain('ir', 'op', IROpDirective)
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	return {
71f08c3bdfSopenharmony_ci		'version': '1.0',
72f08c3bdfSopenharmony_ci		'parallel_read_safe': True,
73f08c3bdfSopenharmony_ci	}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci# vim: tabstop=4
76