Path: blob/develop/src/sage_setup/autogen/interpreters/internal/specs/base.py
4086 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."""1213from __future__ import print_function, absolute_import1415from ..memory import (MemoryChunkConstants, MemoryChunkArguments,16MemoryChunkScratch)17from ..storage import StorageTypeAssignable, ty_int181920class InterpreterSpec(object):21r"""22Each interpreter to be generated by this module is represented23by an InterpreterSpec.24"""2526name = ''2728def __init__(self):29r"""30Initialize an InterpreterSpec.3132Initializes the following fields:3334- ``c_header`` -- a code snippet to go at the top of the C35interpreter source file36- ``pxd_header`` -- a code snippet to go at the top of the37wrapper class .pxd file38- ``pyx_header`` -- a code snippet to go at the top of the39wrapper class source file40- ``err_return`` -- a string indicating the value to be41returned in case of a Python exception42- ``mc_code`` -- a memory chunk to use for the interpreted code43- ``extra_class_members`` -- Class members for the wrapper that44don't correspond to memory chunks45- ``extra_members_initialize`` -- Code to initialize46extra_class_members4748EXAMPLES::4950sage: from sage_setup.autogen.interpreters.internal import *51sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter52sage: from sage_setup.autogen.interpreters.internal.specs.rr import RRInterpreter53sage: interp = RDFInterpreter()54sage: interp.c_header55'#include <gsl/gsl_math.h>'56sage: interp.pxd_header57''58sage: interp.pyx_header59'cimport sage.libs.gsl.math # Add dependency on GSL'60sage: interp.err_return61'-1094648009105371'62sage: interp.mc_code63{MC:code}64sage: interp = RRInterpreter()65sage: interp.extra_class_members66''67sage: interp.extra_members_initialize68''69"""70self.c_header = ''71self.pxd_header = ''72self.pyx_header = ''73self.err_return = 'NULL'74self.mc_code = MemoryChunkConstants('code', ty_int)75self.extra_class_members = ''76self.extra_members_initialize = ''7778def _set_opcodes(self):79r"""80Assign opcodes to the instructions in this interpreter.8182Must be called at the end of __init__ by any subclass of83InterpreterSpec.8485EXAMPLES::8687sage: from sage_setup.autogen.interpreters.internal import *88sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter89sage: interp = RDFInterpreter()90sage: interp.instr_descs[5].opcode91592"""93for i in range(len(self.instr_descs)):94self.instr_descs[i].opcode = i959697class StackInterpreter(InterpreterSpec):98r"""99A subclass of InterpreterSpec, specialized for stack-based100interpreters. (Currently all interpreters are stack-based.)101"""102103def __init__(self, type, mc_retval=None):104r"""105Initialize a StackInterpreter.106107INPUT:108109- type -- A StorageType; the basic type that this interpreter110operates on111- mc_retval -- default None; if not None, a special-purpose112MemoryChunk to use as a return value113114Initializes the fields described in the documentation for115InterpreterSpec.__init__, as well as the following:116117- mc_args, mc_constants, mc_stack -- MemoryChunk values118- return_type -- the type returned by the C interpreter (None for int,119where 1 means success and 0 means error)120- mc_retval -- None, or the MemoryChunk to use as a return value121- ipow_range -- the range of exponents supported by the ipow122instruction (default is False, meaning never use ipow)123- adjust_retval -- None, or a string naming a function to call124in the wrapper's __call__ to modify the return125value of the interpreter126- implement_call_c -- True if the wrapper should have a fast cdef call_c127method (that bypasses the Python call overhead)128(default: ``True``)129130EXAMPLES::131132sage: from sage_setup.autogen.interpreters.internal import *133sage: from sage_setup.autogen.interpreters.internal.specs.rdf import RDFInterpreter134sage: from sage_setup.autogen.interpreters.internal.specs.rr import RRInterpreter135sage: from sage_setup.autogen.interpreters.internal.specs.element import ElementInterpreter136sage: rdf = RDFInterpreter()137sage: rr = RRInterpreter()138sage: el = ElementInterpreter()139sage: rdf.mc_args140{MC:args}141sage: rdf.mc_constants142{MC:constants}143sage: rdf.mc_stack144{MC:stack}145sage: rr.mc_retval146{MC:retval}147sage: rr.return_type is None148True149sage: rdf.return_type.type150'double'151sage: rdf.implement_call_c152True153sage: el.implement_call_c154False155"""156super(StackInterpreter, self).__init__()157self.mc_args = MemoryChunkArguments('args', type)158self.mc_constants = MemoryChunkConstants('constants', type)159self.mc_stack = MemoryChunkScratch('stack', type, is_stack=True)160if isinstance(type, StorageTypeAssignable):161self.return_type = type162else:163self.return_type = None164self.mc_retval = mc_retval165self.ipow_range = False166self.adjust_retval = None167self.implement_call_c = True168169170