[pypy-svn] r25528 - in pypy/dist/pypy/translator/cli: . test
antocuni at codespeak.net
antocuni at codespeak.net
Fri Apr 7 21:28:53 CEST 2006
Author: antocuni
Date: Fri Apr 7 21:28:15 2006
New Revision: 25528
Modified:
pypy/dist/pypy/translator/cli/class_.py
pypy/dist/pypy/translator/cli/cts.py
pypy/dist/pypy/translator/cli/database.py
pypy/dist/pypy/translator/cli/function.py
pypy/dist/pypy/translator/cli/test/compile.py
pypy/dist/pypy/translator/cli/test/runtest.py
Log:
Some refactoring
Modified: pypy/dist/pypy/translator/cli/class_.py
==============================================================================
--- pypy/dist/pypy/translator/cli/class_.py (original)
+++ pypy/dist/pypy/translator/cli/class_.py Fri Apr 7 21:28:15 2006
@@ -1,12 +1,13 @@
from pypy.translator.cli.node import Node
from pypy.translator.cli.function import Function
-from pypy.translator.cli import cts
+from pypy.translator.cli.cts import CTS
class Class(Node):
def __init__(self, db, classdef):
self.db = db
+ self.cts = CTS(db)
self.classdef = classdef
- self.namespace, self.name = cts.split_class_name(classdef._name)
+ self.namespace, self.name = self.cts.split_class_name(classdef._name)
#0/0
def get_name(self):
@@ -26,7 +27,7 @@
ilasm.begin_class(self.name, self.get_base_class())
for f_name, (f_type, f_default) in self.classdef._fields.iteritems():
- ilasm.field(f_name, cts.lltype_to_cts(f_type))
+ ilasm.field(f_name, self.cts.lltype_to_cts(f_type))
# TODO: should the .ctor set the default values?
self._ctor()
Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py (original)
+++ pypy/dist/pypy/translator/cli/cts.py Fri Apr 7 21:28:15 2006
@@ -6,7 +6,7 @@
from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong
-from pypy.rpython.ootypesystem.ootype import Instance
+from pypy.rpython.ootypesystem.ootype import Instance, Class
from pypy.translator.cli.option import getoption
from pypy.tool.ansi_print import ansi_log
@@ -23,17 +23,15 @@
UnsignedLongLong: 'unsigned int64',
Bool: 'bool',
Float: 'float64',
+ Class: '[mscorlib]System.Type', # TODO: check this
}
-_cts_to_ilasm = {
- 'int32': 'i4',
- 'unsigned int32': 'i4',
- 'int64': 'i8',
- 'unsigned int64': 'i8',
- 'bool': 'i4',
- 'float64': 'r8',
+_pyexception_to_cts = {
+ exceptions.Exception: '[mscorlib]System.Exception',
+ exceptions.OverflowError: '[mscorlib]System.OverflowException'
}
+
def _get_from_dict(d, key, error):
try:
return d[key]
@@ -44,65 +42,43 @@
else:
assert False, error
+class CTS(object):
+ def __init__(self, db):
+ self.db = db
-def lltype_to_cts(t):
- if isinstance(t, Instance):
- name = t._name
- if 'Object_meta' in name or 'Object' in name: # TODO
- return 'object'
-
- return 'class %s' % name
-
- return _get_from_dict(_lltype_to_cts, t, 'Unknown type %s' % t)
-
-def lltype_to_ilasm(t):
- return ctstype_to_ilasm(lltype_to_cts(t))
-
-def ctstype_to_ilasm(t):
- return _get_from_dict(_cts_to_ilasm, t, 'Unknown ilasm type %s' % t)
-
-def llvar_to_cts(var):
- return lltype_to_cts(var.concretetype), var.name
-
-def llconst_to_cts(const):
- return lltype_to_cts(const.concretetype), const.value
-
-def llconst_to_ilasm(const):
- """
- Return the const as a string suitable for ilasm.
- """
- ilasm_type = lltype_to_ilasm(const.concretetype)
- if const.concretetype is Bool:
- return ilasm_type, str(int(const.value))
- elif const.concretetype is Float:
- return ilasm_type, repr(const.value)
- else:
- return ilasm_type, str(const.value)
-
-def graph_to_signature(graph, is_method = False, func_name = None):
- ret_type, ret_var = llvar_to_cts(graph.getreturnvar())
- func_name = func_name or graph.name
-
- args = graph.getargs()
- if is_method:
- args = args[1:]
-
- arg_types = [lltype_to_cts(arg.concretetype) for arg in args]
- arg_list = ', '.join(arg_types)
-
- return '%s %s(%s)' % (ret_type, func_name, arg_list)
-
-def split_class_name(class_name):
- parts = class_name.rsplit('.', 1)
- if len(parts) == 2:
- return parts
- else:
- return None, parts[0]
+ def lltype_to_cts(self, t):
+ if isinstance(t, Instance):
+ name = t._name
+ self.db.classes.add(t)
+ return 'class %s' % name
-_pyexception_to_cts = {
- exceptions.Exception: '[mscorlib]System.Exception',
- exceptions.OverflowError: '[mscorlib]System.OverflowException'
- }
+ return _get_from_dict(_lltype_to_cts, t, 'Unknown type %s' % t)
+
+ def llvar_to_cts(self, var):
+ return self.lltype_to_cts(var.concretetype), var.name
+
+ def llconst_to_cts(self, const):
+ return self.lltype_to_cts(const.concretetype), const.value
+
+ def graph_to_signature(self, graph, is_method = False, func_name = None):
+ ret_type, ret_var = self.llvar_to_cts(graph.getreturnvar())
+ func_name = func_name or graph.name
+
+ args = graph.getargs()
+ if is_method:
+ args = args[1:]
+
+ arg_types = [self.lltype_to_cts(arg.concretetype) for arg in args]
+ arg_list = ', '.join(arg_types)
+
+ return '%s %s(%s)' % (ret_type, func_name, arg_list)
+
+ def split_class_name(self, class_name):
+ parts = class_name.rsplit('.', 1)
+ if len(parts) == 2:
+ return parts
+ else:
+ return None, parts[0]
-def pyexception_to_cts(exc):
- return _get_from_dict(_pyexception_to_cts, exc, 'Unknown exception %s' % exc)
+ def pyexception_to_cts(self, exc):
+ return _get_from_dict(_pyexception_to_cts, exc, 'Unknown exception %s' % exc)
Modified: pypy/dist/pypy/translator/cli/database.py
==============================================================================
--- pypy/dist/pypy/translator/cli/database.py (original)
+++ pypy/dist/pypy/translator/cli/database.py Fri Apr 7 21:28:15 2006
@@ -10,9 +10,15 @@
self.pending_graphs = []
self.functions = {} # graph --> function_name
self.methods = {} # graph --> method_name
+ self.consts = {} # value --> const_name
def record_function(self, graph, name):
self.functions[graph] = name
def function_name(self, graph):
return self.functions.get(graph, None)
+
+ def record_const(self, const):
+ name = '__XXX__CONST__NAME__XXX__' # TODO
+ self.consts[const] = name
+ return name
Modified: pypy/dist/pypy/translator/cli/function.py
==============================================================================
--- pypy/dist/pypy/translator/cli/function.py (original)
+++ pypy/dist/pypy/translator/cli/function.py Fri Apr 7 21:28:15 2006
@@ -4,10 +4,11 @@
from sets import Set as set
from pypy.objspace.flow import model as flowmodel
-from pypy.rpython.lltypesystem.lltype import Void
-from pypy.rpython.ootypesystem.ootype import Instance
+from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
+from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong
+from pypy.rpython.ootypesystem import ootype
from pypy.translator.cli.option import getoption
-from pypy.translator.cli import cts
+from pypy.translator.cli.cts import CTS
from pypy.translator.cli.opcodes import opcodes
from pypy.translator.cli.metavm import InstructionList, Generator
from pypy.translator.cli.node import Node
@@ -20,6 +21,7 @@
class Function(Node, Generator):
def __init__(self, db, graph, name = None, is_method = False, is_entrypoint = False):
self.db = db
+ self.cts = CTS(db)
self.graph = graph
self.name = name or graph.name
self.is_method = is_method
@@ -40,7 +42,7 @@
def render(self, ilasm):
self.ilasm = ilasm
graph = self.graph
- returntype, returnvar = cts.llvar_to_cts(graph.getreturnvar())
+ returntype, returnvar = self.cts.llvar_to_cts(graph.getreturnvar())
if self.is_method:
args = self.args[1:] # self is implicit
@@ -88,7 +90,7 @@
continue # see above
assert issubclass(link.exitcase, Exception)
- cts_exc = cts.pyexception_to_cts(link.exitcase)
+ cts_exc = self.cts.pyexception_to_cts(link.exitcase)
self.ilasm.begin_catch(cts_exc)
target = link.target
@@ -180,13 +182,13 @@
for v in mix:
is_var = isinstance(v, flowmodel.Variable)
if id(v) not in seen and is_var and v.name not in args and v.concretetype is not Void:
- locals.append(cts.llvar_to_cts(v))
+ locals.append(self.cts.llvar_to_cts(v))
seen[id(v)] = True
self.locals = locals
def _set_args(self):
- self.args = map(cts.llvar_to_cts, self.graph.getargs())
+ self.args = map(self.cts.llvar_to_cts, self.graph.getargs())
self.argset = set([argname for argtype, argname in self.args])
def _get_block_name(self, block):
@@ -198,12 +200,17 @@
if isinstance(arg, flowmodel.Variable):
lltype = arg.concretetype
elif isinstance(arg, flowmodel.Constant):
+
lltype = arg.value
- if isinstance(lltype, Instance):
+ if isinstance(lltype, ootype._view) and isinstance(lltype._inst, ootype._instance):
+ lltype = lltype._inst._TYPE
+
+ if isinstance(lltype, ootype.Instance):
self.db.classes.add(lltype)
+
def _render_op(self, op):
instr_list = opcodes.get(op.opname, None)
if instr_list is not None:
@@ -218,7 +225,7 @@
def field_name(self, obj, field):
class_name = self.class_name(obj)
- field_type = cts.lltype_to_cts(obj._field_type(field))
+ field_type = self.cts.lltype_to_cts(obj._field_type(field))
return '%s %s::%s' % (field_type, class_name, field)
def ctor_name(self, ooinstance):
@@ -227,11 +234,11 @@
# following methods belongs to the Generator interface
def function_signature(self, graph):
- return cts.graph_to_signature(graph, False)
+ return self.cts.graph_to_signature(graph, False)
def method_signature(self, graph, class_name, name):
full_name = '%s::%s' % (class_name, name)
- return cts.graph_to_signature(graph, True, full_name)
+ return self.cts.graph_to_signature(graph, True, full_name)
def class_name(self, ooinstance):
return ooinstance._name
@@ -271,12 +278,29 @@
self.ilasm.opcode('ldloc', repr(v.name))
elif isinstance(v, flowmodel.Constant):
- if v.concretetype != Void:
- iltype, ilvalue = cts.llconst_to_ilasm(v)
- self.ilasm.opcode('ldc.' + iltype, ilvalue)
+ self._load_const(v)
else:
assert False
+ def _load_const(self, const):
+ type_ = const.concretetype
+ if type_ is Void:
+ pass
+ elif type_ is Bool:
+ self.ilasm.opcode('ldc.i4', str(int(const.value)))
+ elif type_ is Float:
+ self.ilasm.opcode('ldc.r8', repr(const.value))
+ elif type_ in (Signed, Unsigned):
+ self.ilasm.opcode('ldc.i4', str(const.value))
+ elif type_ in (SignedLongLong, UnsignedLongLong):
+ self.ilasm.opcode('ldc.i8', str(const.value))
+ else:
+ name = self.db.record_const(const)
+ cts_type = self.cts.lltype_to_cts(type_)
+ self.ilasm.opcode('ldsfld %s Pypy.Constants::%s' % (cts_type, name))
+ #assert False, 'Unknown constant %s' % const
+
+
def store(self, v):
if isinstance(v, flowmodel.Variable):
if v.concretetype is not Void:
Modified: pypy/dist/pypy/translator/cli/test/compile.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/compile.py (original)
+++ pypy/dist/pypy/translator/cli/test/compile.py Fri Apr 7 21:28:15 2006
@@ -41,9 +41,9 @@
return obj.compute()
def bar(x, y):
- obj = Derived(x)
+ obj = Base(x)
return obj.compute()
- #return foo(Base, x) + foo(Derived, y)
+# return foo(Base, x) + foo(Derived, y)
Modified: pypy/dist/pypy/translator/cli/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/runtest.py (original)
+++ pypy/dist/pypy/translator/cli/test/runtest.py Fri Apr 7 21:28:15 2006
@@ -8,11 +8,13 @@
from pypy.translator.cli.option import getoption
from pypy.translator.cli.gencli import GenCli
from pypy.translator.cli.node import Node
-from pypy.translator.cli.cts import graph_to_signature
-from pypy.translator.cli.cts import llvar_to_cts
+from pypy.translator.cli.cts import CTS
+from pypy.translator.cli.database import LowLevelDatabase
FLOAT_PRECISION = 8
+cts = CTS(LowLevelDatabase()) # this is a hack!
+
def check(func, annotation, args):
mono = compile_function(func, annotation)
res1 = func(*args)
@@ -43,14 +45,14 @@
ilasm.opcode('ldarg.0')
ilasm.opcode('ldc.i4.%d' % i)
ilasm.opcode('ldelem.ref')
- arg_type, arg_var = llvar_to_cts(arg)
+ arg_type, arg_var = cts.llvar_to_cts(arg)
ilasm.call('%s class [mscorlib]System.Convert::%s(string)' %
(arg_type, self.__convert_method(arg_type)))
- ilasm.call(graph_to_signature(self.graph))
+ ilasm.call(cts.graph_to_signature(self.graph))
# print the result using the appropriate WriteLine overload
- ret_type, ret_var = llvar_to_cts(self.graph.getreturnvar())
+ ret_type, ret_var = cts.llvar_to_cts(self.graph.getreturnvar())
ilasm.call('void class [mscorlib]System.Console::WriteLine(%s)' % ret_type)
ilasm.opcode('ret')
ilasm.end_function()
@@ -139,7 +141,7 @@
retval = mono.wait()
assert retval == 0, stderr
- ret_type, ret_var = llvar_to_cts(self.graph.getreturnvar())
+ ret_type, ret_var = cts.llvar_to_cts(self.graph.getreturnvar())
if 'int' in ret_type:
return int(stdout)
elif ret_type == 'float64':
More information about the Pypy-commit
mailing list