Path: blob/develop/src/sage_setup/autogen/interpreters/internal/specs/base.py
7417 views
#*****************************************************************************1# Copyright (C) 2009 Carl Witty <[email protected]>2# Copyright (C) 2015 Jeroen Demeyer <[email protected]>3#4# This program is free software: you can redistribute it and/or modify5# it under the terms of the GNU General Public License as published by6# the Free Software Foundation, either version 2 of the License, or7# (at your option) any later version.8# http://www.gnu.org/licenses/9#*****************************************************************************1011"""Base classes for interpreter specs."""121314from ..memory import MemoryChunkArguments, MemoryChunkConstants, MemoryChunkScratch15from ..storage import StorageTypeAssignable, ty_int161718class InterpreterSpec:19r"""20Each interpreter to be generated by this module is represented21by an InterpreterSpec.22"""2324name = ''2526def __init__(self):27r"""28Initialize an InterpreterSpec.2930Initializes the following fields:3132- ``c_header`` -- a code snippet to go at the top of the C33interpreter source file34- ``pxd_header`` -- a code snippet to go at the top of the35wrapper class .pxd file36- ``pyx_header`` -- a code snippet to go at the top of the37wrapper class source file38- ``err_return`` -- a string indicating the value to be39returned in case of a Python exception40- ``mc_code`` -- a memory chunk to use for the interpreted code41- ``extra_class_members`` -- Class members for the wrapper that42don't correspond to memory chunks43- ``extra_members_initialize`` -- Code to initialize44extra_class_members4546EXAMPLES::4748sage: from sage_setup.autogen.interpreters.internal import *49sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter50sage: from sage_setup.autogen.interpreters.internal.specs.rr import RRInterpreter51sage: interp = RDFInterpreter()52sage: interp.c_header53'#include <gsl/gsl_math.h>'54sage: interp.pxd_header55''56sage: interp.pyx_header57'cimport sage.libs.gsl.math # Add dependency on GSL'58sage: interp.err_return59'-1094648009105371'60sage: interp.mc_code61{MC:code}62sage: interp = RRInterpreter()63sage: interp.extra_class_members64''65sage: interp.extra_members_initialize66''67"""68self.c_header = ''69self.pxd_header = ''70self.pyx_header = ''71self.err_return = 'NULL'72self.mc_code = MemoryChunkConstants('code', ty_int)73self.extra_class_members = ''74self.extra_members_initialize = ''7576def _set_opcodes(self):77r"""78Assign opcodes to the instructions in this interpreter.7980Must be called at the end of __init__ by any subclass of81InterpreterSpec.8283EXAMPLES::8485sage: from sage_setup.autogen.interpreters.internal import *86sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter87sage: interp = RDFInterpreter()88sage: interp.instr_descs[5].opcode89590"""91for i in range(len(self.instr_descs)):92self.instr_descs[i].opcode = i939495class StackInterpreter(InterpreterSpec):96r"""97A subclass of InterpreterSpec, specialized for stack-based98interpreters. (Currently all interpreters are stack-based.)99"""100101def __init__(self, type, mc_retval=None):102r"""103Initialize a StackInterpreter.104105INPUT:106107- type -- A StorageType; the basic type that this interpreter108operates on109- mc_retval -- default None; if not None, a special-purpose110MemoryChunk to use as a return value111112Initializes the fields described in the documentation for113InterpreterSpec.__init__, as well as the following:114115- mc_args, mc_constants, mc_stack -- MemoryChunk values116- return_type -- the type returned by the C interpreter (None for int,117where 1 means success and 0 means error)118- mc_retval -- None, or the MemoryChunk to use as a return value119- ipow_range -- the range of exponents supported by the ipow120instruction (default is False, meaning never use ipow)121- adjust_retval -- None, or a string naming a function to call122in the wrapper's __call__ to modify the return123value of the interpreter124- implement_call_c -- True if the wrapper should have a fast cdef call_c125method (that bypasses the Python call overhead)126(default: ``True``)127128EXAMPLES::129130sage: from sage_setup.autogen.interpreters.internal import *131sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter132sage: from sage_setup.autogen.interpreters.internal.specs.rr import RRInterpreter133sage: from sage_setup.autogen.interpreters.internal.specs.element import ElementInterpreter134sage: rdf = RDFInterpreter()135sage: rr = RRInterpreter()136sage: el = ElementInterpreter()137sage: rdf.mc_args138{MC:args}139sage: rdf.mc_constants140{MC:constants}141sage: rdf.mc_stack142{MC:stack}143sage: rr.mc_retval144{MC:retval}145sage: rr.return_type is None146True147sage: rdf.return_type.type148'double'149sage: rdf.implement_call_c150True151sage: el.implement_call_c152False153"""154super().__init__()155self.mc_args = MemoryChunkArguments('args', type)156self.mc_constants = MemoryChunkConstants('constants', type)157self.mc_stack = MemoryChunkScratch('stack', type, is_stack=True)158if isinstance(type, StorageTypeAssignable):159self.return_type = type160else:161self.return_type = None162self.mc_retval = mc_retval163self.ipow_range = False164self.adjust_retval = None165self.implement_call_c = True166167168