Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/src/sage_setup/autogen/interpreters/internal/specs/base.py
7417 views
1
#*****************************************************************************
2
# Copyright (C) 2009 Carl Witty <[email protected]>
3
# Copyright (C) 2015 Jeroen Demeyer <[email protected]>
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 2 of the License, or
8
# (at your option) any later version.
9
# http://www.gnu.org/licenses/
10
#*****************************************************************************
11
12
"""Base classes for interpreter specs."""
13
14
15
from ..memory import MemoryChunkArguments, MemoryChunkConstants, MemoryChunkScratch
16
from ..storage import StorageTypeAssignable, ty_int
17
18
19
class InterpreterSpec:
20
r"""
21
Each interpreter to be generated by this module is represented
22
by an InterpreterSpec.
23
"""
24
25
name = ''
26
27
def __init__(self):
28
r"""
29
Initialize an InterpreterSpec.
30
31
Initializes the following fields:
32
33
- ``c_header`` -- a code snippet to go at the top of the C
34
interpreter source file
35
- ``pxd_header`` -- a code snippet to go at the top of the
36
wrapper class .pxd file
37
- ``pyx_header`` -- a code snippet to go at the top of the
38
wrapper class source file
39
- ``err_return`` -- a string indicating the value to be
40
returned in case of a Python exception
41
- ``mc_code`` -- a memory chunk to use for the interpreted code
42
- ``extra_class_members`` -- Class members for the wrapper that
43
don't correspond to memory chunks
44
- ``extra_members_initialize`` -- Code to initialize
45
extra_class_members
46
47
EXAMPLES::
48
49
sage: from sage_setup.autogen.interpreters.internal import *
50
sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter
51
sage: from sage_setup.autogen.interpreters.internal.specs.rr import RRInterpreter
52
sage: interp = RDFInterpreter()
53
sage: interp.c_header
54
'#include <gsl/gsl_math.h>'
55
sage: interp.pxd_header
56
''
57
sage: interp.pyx_header
58
'cimport sage.libs.gsl.math # Add dependency on GSL'
59
sage: interp.err_return
60
'-1094648009105371'
61
sage: interp.mc_code
62
{MC:code}
63
sage: interp = RRInterpreter()
64
sage: interp.extra_class_members
65
''
66
sage: interp.extra_members_initialize
67
''
68
"""
69
self.c_header = ''
70
self.pxd_header = ''
71
self.pyx_header = ''
72
self.err_return = 'NULL'
73
self.mc_code = MemoryChunkConstants('code', ty_int)
74
self.extra_class_members = ''
75
self.extra_members_initialize = ''
76
77
def _set_opcodes(self):
78
r"""
79
Assign opcodes to the instructions in this interpreter.
80
81
Must be called at the end of __init__ by any subclass of
82
InterpreterSpec.
83
84
EXAMPLES::
85
86
sage: from sage_setup.autogen.interpreters.internal import *
87
sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter
88
sage: interp = RDFInterpreter()
89
sage: interp.instr_descs[5].opcode
90
5
91
"""
92
for i in range(len(self.instr_descs)):
93
self.instr_descs[i].opcode = i
94
95
96
class StackInterpreter(InterpreterSpec):
97
r"""
98
A subclass of InterpreterSpec, specialized for stack-based
99
interpreters. (Currently all interpreters are stack-based.)
100
"""
101
102
def __init__(self, type, mc_retval=None):
103
r"""
104
Initialize a StackInterpreter.
105
106
INPUT:
107
108
- type -- A StorageType; the basic type that this interpreter
109
operates on
110
- mc_retval -- default None; if not None, a special-purpose
111
MemoryChunk to use as a return value
112
113
Initializes the fields described in the documentation for
114
InterpreterSpec.__init__, as well as the following:
115
116
- mc_args, mc_constants, mc_stack -- MemoryChunk values
117
- return_type -- the type returned by the C interpreter (None for int,
118
where 1 means success and 0 means error)
119
- mc_retval -- None, or the MemoryChunk to use as a return value
120
- ipow_range -- the range of exponents supported by the ipow
121
instruction (default is False, meaning never use ipow)
122
- adjust_retval -- None, or a string naming a function to call
123
in the wrapper's __call__ to modify the return
124
value of the interpreter
125
- implement_call_c -- True if the wrapper should have a fast cdef call_c
126
method (that bypasses the Python call overhead)
127
(default: ``True``)
128
129
EXAMPLES::
130
131
sage: from sage_setup.autogen.interpreters.internal import *
132
sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter
133
sage: from sage_setup.autogen.interpreters.internal.specs.rr import RRInterpreter
134
sage: from sage_setup.autogen.interpreters.internal.specs.element import ElementInterpreter
135
sage: rdf = RDFInterpreter()
136
sage: rr = RRInterpreter()
137
sage: el = ElementInterpreter()
138
sage: rdf.mc_args
139
{MC:args}
140
sage: rdf.mc_constants
141
{MC:constants}
142
sage: rdf.mc_stack
143
{MC:stack}
144
sage: rr.mc_retval
145
{MC:retval}
146
sage: rr.return_type is None
147
True
148
sage: rdf.return_type.type
149
'double'
150
sage: rdf.implement_call_c
151
True
152
sage: el.implement_call_c
153
False
154
"""
155
super().__init__()
156
self.mc_args = MemoryChunkArguments('args', type)
157
self.mc_constants = MemoryChunkConstants('constants', type)
158
self.mc_stack = MemoryChunkScratch('stack', type, is_stack=True)
159
if isinstance(type, StorageTypeAssignable):
160
self.return_type = type
161
else:
162
self.return_type = None
163
self.mc_retval = mc_retval
164
self.ipow_range = False
165
self.adjust_retval = None
166
self.implement_call_c = True
167
168