[pypy-svn] r27529 - in pypy/dist/pypy/translator/js2: . test
fijal at codespeak.net
fijal at codespeak.net
Sun May 21 12:12:00 CEST 2006
Author: fijal
Date: Sun May 21 12:11:58 2006
New Revision: 27529
Modified:
pypy/dist/pypy/translator/js2/asmgen.py
pypy/dist/pypy/translator/js2/function.py
pypy/dist/pypy/translator/js2/js.py
pypy/dist/pypy/translator/js2/jts.py
pypy/dist/pypy/translator/js2/opcodes.py
pypy/dist/pypy/translator/js2/test/test_runtest.py
Log:
PEP8 compatibility issues.
Modified: pypy/dist/pypy/translator/js2/asmgen.py
==============================================================================
--- pypy/dist/pypy/translator/js2/asmgen.py (original)
+++ pypy/dist/pypy/translator/js2/asmgen.py Sun May 21 12:11:58 2006
@@ -4,7 +4,8 @@
from pypy.translator.js2.log import log
-from pypy.translator.squeak.node import LoopFinder
+from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
+from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong
from StringIO import StringIO
@@ -38,116 +39,123 @@
self._indent -= self._indentstep
self.writeline(self._endblock)
-class Queue ( object ):
- def __init__ ( self , l , subst_table ):
- self . l = l [:]
- self . subst_table = subst_table
-
- def pop ( self ):
- el = self . l . pop ()
- return self . subst_table . get ( el , el )
+class Queue(object):
+ def __init__(self, l, subst_table):
+ self.l = l[:]
+ self.subst_table = subst_table
+
+ def pop(self):
+ el = self.l.pop()
+ return self.subst_table.get(el, el)
- def __getattr__ ( self , attr ):
- return getattr ( self . l , attr )
+ def __getattr__(self,attr):
+ return getattr(self.l, attr)
- def __len__ ( self ):
+ def __len__(self):
return len(self.l)
-class AsmGen ( object ):
+class AsmGen(object):
""" JS 'assembler' generator routines
"""
- def __init__ ( self , outfile , name ):
- self . outfile = outfile
- self . name = name
- self . subst_table = {}
- self . right_hand = Queue ( [] , self . subst_table )
- self . codegenerator = CodeGenerator ( outfile )
+ def __init__(self, outfile, name):
+ self.outfile = outfile
+ self.name = name
+ self.subst_table = {}
+ self.right_hand = Queue([], self.subst_table)
+ self.codegenerator = CodeGenerator(outfile)
- def show_const ( self ):
+ def show_const(self):
return False
- def begin_function ( self , name, arglist, returntype, is_entrypoint = False, *args ):
- args = "," . join ( [ i [ 1 ] for i in arglist ] )
- self . codegenerator . write ( "function %s (%s) " % ( name , args ) )
- self . codegenerator . openblock ()
-
- def end_function ( self ):
- self . codegenerator . closeblock ()
-
- def locals ( self , loc ):
- self . codegenerator . writeline ( "var " + "," . join ( [ i [ 1 ] for i in loc ] ) + ";" )
-
- def load_arg ( self , v ):
- self . right_hand . append ( v.name )
-
- def store_local ( self , v ):
- name = self . subst_table . get ( v . name , v . name )
- element = self . right_hand . pop ()
+ def begin_function(self, name, arglist, returntype, is_entrypoint = False, *args):
+ args = ",".join([i[1] for i in arglist])
+ self.codegenerator.write("function %s (%s) "%(name, args))
+ self.codegenerator.openblock()
+
+ def end_function(self):
+ self.codegenerator.closeblock()
+
+ def locals(self, loc):
+ self.codegenerator.writeline("var "+",".join([i[1] for i in loc])+";")
+
+ def load_arg(self, v):
+ self.right_hand.append(v.name)
+
+ def store_local(self, v):
+ name = self.subst_table.get(v.name, v.name)
+ element = self.right_hand.pop()
if element != name:
- self . codegenerator . writeline ( "%s = %s;" % ( name , element ) )
+ self.codegenerator.writeline("%s = %s;"%(name, element))
- def load_local ( self , v ):
- self . right_hand . append ( v . name )
+ def load_local(self, v):
+ self.right_hand.append(v.name)
- def load_const ( self , _type , v ):
- self . right_hand . append ( str(v) )
+ def load_const(self, _type, v):
+ if _type is Bool:
+ if v == False:
+ val = 'false'
+ else:
+ val = 'true'
+ else:
+ val = str(v)
+ self.right_hand.append(val)
- def ret ( self ):
- self . codegenerator . writeline ( "return ( %s );" % self . right_hand . pop () )
+ def ret(self):
+ self.codegenerator.writeline("return ( %s );"%self.right_hand.pop())
- def begin_namespace ( self , namespace ):
+ def begin_namespace(self,namespace):
pass
- def end_namespace ( self ):
+ def end_namespace(self):
pass
- def begin_class ( self , cl ):
- pass
+ def begin_class(self,cl):
+ raise NotImplementedError("Class support")
- def end_class ( self ):
- pass
+ def end_class(self):
+ raise NotImplementedError("Class support")
- def emit ( self , opcode , *args ):
- v1 = self . right_hand . pop ()
- v2 = self . right_hand . pop ()
- self . right_hand . append ( "(%s%s%s)" % ( v2 , opcode , v1 ) )
-
- def call ( self , func ):
- func_name,args = func
- real_args = "," . join ( [ self . right_hand . pop () for i in xrange(len(args)) ] )
- self . right_hand . append ( "%s ( %s )" % ( func_name , real_args ) )
-
- def branch_if ( self , arg , exitcase ):
- arg_name = self . subst_table . get ( arg . name , arg . name )
- self . codegenerator . write ( "if ( %s == %s )" % ( arg_name , exitcase ) )
- self . codegenerator . openblock ()
-
- def branch_while ( self , arg , exitcase ):
- arg_name = self . subst_table . get ( arg . name , arg . name )
- self . codegenerator . write ( "while ( %s == %s )" % ( arg_name , exitcase ) )
- self . codegenerator . openblock ()
-
- def branch_else ( self ):
- self . codegenerator . closeblock ()
- self . codegenerator . write ( "else" )
- self . codegenerator . openblock ()
+ def emit(self, opcode, *args):
+ v1 = self.right_hand.pop()
+ v2 = self.right_hand.pop()
+ self.right_hand.append("(%s%s%s)"%(v2, opcode, v1))
+
+ def call(self, func):
+ func_name, args = func
+ real_args = ",".join([self.right_hand.pop() for i in xrange(len(args))] )
+ self.right_hand.append("%s ( %s )"%(func_name, real_args))
+
+ def branch_if(self, arg, exitcase):
+ arg_name = self.subst_table.get(arg.name, arg.name)
+ self.codegenerator.write("if ( %s == %s )"%(arg_name, str(exitcase).lower()))
+ self.codegenerator.openblock()
+
+ def branch_while(self, arg, exitcase):
+ arg_name = self.subst_table.get(arg.name, arg.name)
+ self.codegenerator.write("while ( %s == %s )"%(arg_name, str(exitcase).lower()))
+ self.codegenerator.openblock()
+
+ def branch_else(self):
+ self.codegenerator.closeblock()
+ self.codegenerator.write("else")
+ self.codegenerator.openblock()
- def close_branch ( self ):
- self . codegenerator . closeblock ()
+ def close_branch(self):
+ self.codegenerator.closeblock()
- def label ( self , *args ):
- self . codegenerator . openblock ()
+ def label(self, *args):
+ self.codegenerator.openblock()
- def branch ( self , *args ):
+ def branch(self, *args):
#self . codegenerator . closeblock ()
pass
- def change_name ( self , from_name , to_name ):
- self . subst_table [ from_name.name ] = to_name.name
+ def change_name(self, from_name, to_name):
+ self.subst_table[from_name.name] = to_name.name
#pass
- def cast_floor ( self ):
- self . right_hand . append ( "Math.floor ( %s )" % self . right_hand . pop() )
+ def cast_floor(self):
+ self.right_hand.append("Math.floor ( %s )"%self.right_hand.pop())
#def finish ( self ):
# self . outfile . write ( "%r" % self . right_hand )
Modified: pypy/dist/pypy/translator/js2/function.py
==============================================================================
--- pypy/dist/pypy/translator/js2/function.py (original)
+++ pypy/dist/pypy/translator/js2/function.py Sun May 21 12:11:58 2006
@@ -82,62 +82,62 @@
for op in block.operations:
self._render_op(op)
- def render_block ( self , block , stop_block = None ):
+ def render_block(self, block, stop_block = None):
if block is stop_block:
return
for op in block.operations:
self._render_op(op)
- if len ( block . exits ) == 0:
+ if len(block.exits) == 0:
# return block
return_var = block.inputargs[0]
- self . load ( return_var )
- self . ilasm . ret ()
- elif block . exitswitch is None:
+ self.load(return_var)
+ self.ilasm.ret()
+ elif block.exitswitch is None:
# single exit block
- assert ( len(block.exits) == 1 )
- link = block . exits [ 0 ]
- self . _setup_link ( link )
- self . render_block ( link . target , stop_block )
- elif block . exitswitch is flowmodel.c_last_exception:
- raise NotImplementedError ( "Exception handling" )
+ assert(len(block.exits) == 1)
+ link = block.exits[0]
+ self._setup_link(link)
+ self.render_block(link.target, stop_block)
+ elif block.exitswitch is flowmodel.c_last_exception:
+ raise NotImplementedError("Exception handling")
else:
- if self . loops . has_key ( block ):
+ if self.loops.has_key(block):
# we've got loop
- self . ilasm . branch_while ( block . exitswitch , self . loops [ block ] )
- exit_case = block . exits [ self . loops [ block ] ]
- self . _setup_link ( exit_case )
- self . render_block ( exit_case . target , block )
- for op in block . operations:
- self._render_op ( op )
- self . ilasm . close_branch ()
- exit_case = block . exits [ not self . loops [ block ] ]
- self . _setup_link ( exit_case )
+ self.ilasm.branch_while(block.exitswitch, self.loops[block])
+ exit_case = block.exits[self.loops[block]]
+ self._setup_link(exit_case)
+ self.render_block(exit_case.target, block)
+ for op in block.operations:
+ self._render_op(op)
+ self.ilasm.close_branch()
+ exit_case = block.exits[not self.loops[block]]
+ self._setup_link(exit_case)
#log ( )
- self . render_block ( exit_case . target , block )
+ self.render_block(exit_case.target,block)
#raise NotImplementedError ( "loop" )
else:
# just a simple if
- assert ( len ( block . exits ) == 2 )
- self . ilasm . branch_if ( block . exitswitch , True )
- self . _setup_link ( block . exits [ True ] )
- self . render_block ( block . exits [ True ] . target , stop_block )
- self . ilasm . branch_else ()
- self . _setup_link ( block . exits [ False ] )
- self . render_block ( block . exits [ False ] . target , stop_block )
- self . ilasm . close_branch ()
+ assert(len(block.exits) == 2)
+ self.ilasm.branch_if(block.exitswitch, True)
+ self._setup_link(block.exits[True])
+ self.render_block(block.exits[True].target, stop_block)
+ self.ilasm.branch_else()
+ self._setup_link(block.exits[False])
+ self.render_block(block.exits[False].target, stop_block)
+ self.ilasm.close_branch()
- def render ( self , ilasm ):
- if self . db . graph_name ( self . graph ) is not None and not self . is_method:
+ def render(self,ilasm):
+ if self.db.graph_name(self.graph) is not None and not self.is_method:
return # already rendered
- self . ilasm = ilasm
+ self.ilasm = ilasm
- self . loops = LoopFinder ( self . graph . startblock ) . loops
- self . ilasm . begin_function ( self . name , self . args , None , None , None )
+ self.loops = LoopFinder(self.graph.startblock).loops
+ self.ilasm.begin_function(self.name, self.args, None, None, None)
- self . render_block ( self . graph . startblock )
+ self.render_block(self.graph.startblock)
## if self._is_return_block(block):
## return_blocks.append(block)
## continue
@@ -168,110 +168,6 @@
else:
self.db.record_function(self.graph, self.name)
- def oldrender(self, ilasm):
- if self.db.graph_name(self.graph) is not None and not self.is_method:
- return # already rendered
-
- loop_finder = LoopFinder ( self . graph . startblock )
- # Work out it here
-
- self.ilasm = ilasm
- graph = self.graph
- returntype, returnvar = self.cts.llvar_to_cts(graph.getreturnvar())
-
- self.ilasm.begin_function(self.name, self.args, returntype, None , None )
- self.ilasm.locals(self.locals)
-
- return_blocks = []
- for block in graph.iterblocks():
- if self._is_return_block(block):
- return_blocks.append(block)
- continue
-
-
- #self.ilasm.label(self._get_block_name(block))
-
- handle_exc = (block.exitswitch == flowmodel.c_last_exception)
- if handle_exc:
- self.ilasm.begin_try()
-
- for op in block.operations:
- #self._search_for_classes(op)
- self._render_op(op)
-
- if self._is_raise_block(block):
- exc = block.inputargs[1]
- self.load(exc)
- self.ilasm.throw()
-
- if handle_exc:
- # search for the "default" block to be executed when no exception is raised
- for link in block.exits:
- if link.exitcase is None:
- self._setup_link(link)
- target_label = self._get_block_name(link.target)
- self.ilasm.leave(target_label)
- self.ilasm.end_try()
-
- # catch the exception and dispatch to the appropriate block
- for link in block.exits:
- if link.exitcase is None:
- continue # see above
-
- assert issubclass(link.exitcase, Exception)
- #cts_exc = self.cts.pyexception_to_cts(link.exitcase)
- #cts_exc = str(link.exitcase) # TODO: is it a bit hackish?
- ll_meta_exc = link.llexitcase
- self.db.record_const(ll_meta_exc)
- ll_exc = ll_meta_exc._inst.class_._INSTANCE
- cts_exc = self.cts.lltype_to_cts(ll_exc, False)
- self.ilasm.begin_catch(cts_exc)
-
- target = link.target
- if self._is_raise_block(target):
- # the exception value is on the stack, use it as the 2nd target arg
- assert len(link.args) == 2
- assert len(target.inputargs) == 2
- self.store(link.target.inputargs[1])
- else:
- # pop the unused exception value
- self.ilasm.pop()
- self._setup_link(link)
-
- target_label = self._get_block_name(target)
- self.ilasm.leave(target_label)
- self.ilasm.end_catch()
-
- else:
- # no exception handling, follow block links
- for link in block.exits:
- target_label = self._get_block_name(link.target)
- if link.exitcase is None:
- pass
- #self.ilasm.branch(target_label)
- else:
- assert type(link.exitcase is bool)
- assert block.exitswitch is not None
- self.ilasm.branch_if( block.exitswitch, link.exitcase, target_label)
- self._setup_link(link)
- self.ilasm.close_branch()
-
- # render return blocks at the end just to please the .NET
- # runtime that seems to need a return statement at the end of
- # the function
- for block in return_blocks:
- #self.ilasm.label(self._get_block_name(block))
- return_var = block.inputargs[0]
- if return_var.concretetype is not Void:
- self.load(return_var)
- self.ilasm.ret()
-
- self.ilasm.end_function()
- if self.is_method:
- pass # TODO
- else:
- self.db.record_function(self.graph, self.name)
-
def _setup_link(self, link):
target = link.target
for to_load, to_store in zip(link.args, target.inputargs):
Modified: pypy/dist/pypy/translator/js2/js.py
==============================================================================
--- pypy/dist/pypy/translator/js2/js.py (original)
+++ pypy/dist/pypy/translator/js2/js.py Sun May 21 12:11:58 2006
@@ -31,12 +31,12 @@
return path
class JS(object):
- def __init__ ( self , translator , functions=[] , stackless=False , compress=False , logging=False ):
- self . cli = GenCli ( udir , translator , type_system_class = JTS , opcode_dict = opcodes ,\
- name_suffix = '.js' , function_class = Function )
- self . translator = translator
+ def __init__(self, translator, functions=[], stackless=False, compress=False, logging=False):
+ self.cli = GenCli(udir, translator, type_system_class = JTS, opcode_dict = opcodes,\
+ name_suffix = '.js', function_class = Function)
+ self.translator = translator
- def write_source ( self ):
- self . cli . generate_source ( AsmGen )
- self . filename = self . cli . tmpfile
- return self . cli . tmpfile
+ def write_source(self):
+ self.cli.generate_source(AsmGen)
+ self.filename = self.cli.tmpfile
+ return self.cli.tmpfile
Modified: pypy/dist/pypy/translator/js2/jts.py
==============================================================================
--- pypy/dist/pypy/translator/js2/jts.py (original)
+++ pypy/dist/pypy/translator/js2/jts.py Sun May 21 12:11:58 2006
@@ -4,15 +4,15 @@
from pypy.rpython.ootypesystem import ootype
-class JTS ( object ):
+class JTS(object):
""" Class implementing JavaScript type system
calls with mapping similiar to cts
"""
- def __init__ ( self , db ):
- self . db = db
+ def __init__(self, db):
+ self.db = db
- def llvar_to_cts ( self , var ):
- return 'var ',var.name
+ def llvar_to_cts(self, var):
+ return 'var ', var.name
def graph_to_signature(self, graph, is_method = False, func_name = None):
ret_type, ret_var = self.llvar_to_cts(graph.getreturnvar())
Modified: pypy/dist/pypy/translator/js2/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/js2/opcodes.py (original)
+++ pypy/dist/pypy/translator/js2/opcodes.py Sun May 21 12:11:58 2006
@@ -7,16 +7,16 @@
DoNothing = [PushAllArgs]
-class _SameAs ( MicroInstruction ):
- def render ( self , generator , op ):
- generator.change_name(op.result,op.args[0])
+class _SameAs(MicroInstruction):
+ def render(self, generator, op):
+ generator.change_name(op.result, op.args[0])
-class _CastFloor ( MicroInstruction ):
- def render ( self , generator , op ):
+class _CastFloor(MicroInstruction):
+ def render(self, generator, op):
generator.cast_floor()
-CastFloor = [ PushAllArgs , _CastFloor() ]
-CopyName = [ PushAllArgs , _SameAs () ]
+CastFloor = [PushAllArgs, _CastFloor()]
+CopyName = [PushAllArgs, _SameAs ()]
SameAs = CopyName
opcodes = {'int_mul': '*',
@@ -59,6 +59,13 @@
'unichar_ne': '!=',
'unichar_ge': '>=',
'unichar_gt': '>',
+
+ 'char_lt': '<',
+ 'char_le': '<=',
+ 'char_eq': '==',
+ 'char_ne': '!=',
+ 'char_ge': '>=',
+ 'char_gt': '>',
'float_mul': '*',
'float_add': '+',
@@ -92,6 +99,7 @@
'cast_int_to_float': CopyName,
'cast_int_to_longlong': CopyName,
'cast_uint_to_int': CopyName,
+ 'cast_uint_to_float': CopyName,
'cast_float_to_int': CastFloor,
'cast_float_to_uint': CastFloor,
'truncate_longlong_to_int': CopyName,
Modified: pypy/dist/pypy/translator/js2/test/test_runtest.py
==============================================================================
--- pypy/dist/pypy/translator/js2/test/test_runtest.py (original)
+++ pypy/dist/pypy/translator/js2/test/test_runtest.py Sun May 21 12:11:58 2006
@@ -2,17 +2,16 @@
from pypy.translator.js2.test.runtest import compile_function
#test returntypes
-##def test_bool_return():
-## def bool_return_False():
-## return False
-## def bool_return_True():
-## return True
-## f_false = compile_function(bool_return_False, [])
-## assert f_false() == bool_return_False()
-## f_true = compile_function(bool_return_True , [])
-## assert f_true() == bool_return_True()
-##
-##
+def test_bool_return():
+ def bool_return_False():
+ return False
+ def bool_return_True():
+ return True
+ f_false = compile_function(bool_return_False, [])
+ assert f_false() == bool_return_False()
+ f_true = compile_function(bool_return_True , [])
+ assert f_true() == bool_return_True()
+
def test_int_return():
def int_return():
return 42
More information about the Pypy-commit
mailing list