Changeset 1195 for proto


Ignore:
Timestamp:
Jun 15, 2011, 10:33:45 AM (8 years ago)
Author:
cameron
Message:

C syntax mode

Location:
proto/Compiler
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/compiler2.py

    r1178 r1195  
    4444        s = ast.parse(input_string)
    4545        # Analysis and Transformation
    46         xfrmr = pablo.MainLoopTransformer(s, options.add_dump_stmts)
     46        xfrmr = pablo.MainLoopTransformer(s, options.C_syntax, options.add_dump_stmts)
    4747        xfrmr.gen_globals()
    4848        xfrmr.gen_declarations()
  • proto/Compiler/config.py

    r1178 r1195  
    3030                          )
    3131       
     32        parser.add_option('-c', '--C_syntax',
     33                          dest='C_syntax',
     34                          action='store_true',
     35                          default=False,
     36                          help='use C syntax for output',
     37                          )
     38       
    3239        # Group options
    3340        # 'dump'
  • proto/Compiler/pablo.py

    r1074 r1195  
    393393        self.xlate_type = xlate_type
    394394
    395   def xfrm(self, node, stream_function_type_names):
     395  def xfrm(self, node, stream_function_type_names, C_syntax):
    396396        self.stream_function_type_names = stream_function_type_names
     397        self.C_syntax = C_syntax
    397398        self.generic_visit(node)
    398399       
     
    401402
    402403        if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id in self.stream_function_type_names:
    403              struct_name = node.func.id[0].lower() + node.func.id[1:]
     404             name = lower1(node.func.id)
     405             node.func.id = name + ("_" if self.C_syntax else ".") + ("do_final_block" if self.xlate_type == "final" else "do_block")
     406             if self.C_syntax:
     407                     node.args = [ast.Name("&" +lower1(name) +"_data", ast.Load())] + [ast.Name("&" + a.id, ast.Load()) for a in node.args]
    404408             if self.xlate_type == "final":
    405                    node.func.id = struct_name + ".do_final_block"
    406409                   node.args = node.args + [ast.Name("EOF_mask", ast.Load())]
    407              else:
    408                    node.func.id = struct_name + ".do_block"
     410                     
    409411        return node     
    410412               
     
    437439
    438440class Emitter():
     441        def __init__(self, use_C_syntax):
     442                self.use_C_syntax = use_C_syntax
    439443
    440444        def definition(self, stream_function, icount=0):
     
    442446                constructor = ""
    443447                carry_declaration = ""
     448                self.type_name = stream_function.type_name
    444449               
    445450                if stream_function.carry_count > 0:
     
    460465                                                self.do_segment_args(stream_function.parameters))       
    461466
     467                if self.use_C_syntax:
     468                        return self.indent(icount) + "struct " + stream_function.type_name + "_data {" \
     469                               + "\n" + self.indent(icount) + carry_declaration \
     470                               + "\n" + self.indent(icount) + "};\n" \
     471                               + "\n" + self.indent(icount) + do_block_function \
     472                               + "\n" + self.indent(icount) + do_final_block_function \
     473                               + "\n" + self.indent(icount) + do_segment_function + "\n\n"
     474                               
    462475                return self.indent(icount) + "struct " + stream_function.type_name + " {" \
    463476                + "\n" + self.indent(icount) + constructor \
     
    472485                       
    473486        def do_block(self, parameters, declarations, initializations, statements, icount=0):
    474                 return self.indent(icount) + "void do_block(" + parameters + ") {" \
    475             + "\n" + self.indent(icount) + declarations \
     487                pfx = (lower1(self.type_name) + "_" if self.use_C_syntax else "")
     488                return self.indent(icount) + "void " + pfx + "do_block(" + parameters + ") {" \
     489                + "\n" + self.indent(icount) + declarations \
    476490                + "\n" + self.indent(icount) + initializations \
    477491                + "\n" + self.indent(icount) + statements \
     
    479493
    480494        def do_final_block(self, parameters, declarations, initializations, statements, icount=0):
    481                 return self.indent(icount) + "void do_final_block(" + parameters + ") {" \
    482             + "\n" + self.indent(icount) + declarations \
     495                pfx = (lower1(self.type_name) + "_" if self.use_C_syntax else "")
     496                return self.indent(icount) + "void " + pfx + "do_final_block(" + parameters + ") {" \
     497                + "\n" + self.indent(icount) + declarations \
    483498                + "\n" + self.indent(icount) + initializations \
    484499                + "\n" + self.indent(icount) + statements \
     
    486501
    487502        def do_segment(self, parameters, do_block_call_args, icount=0):
    488                 return self.indent(icount) + "void do_segment(" + parameters + ") {" \
    489             + "\n" + self.indent(icount) + "  for (int i = 0; i < SEGMENT_BLOCKS; i++)" \
    490                 + "\n" + self.indent(icount) + "    do_block(" + do_block_call_args + ");" \
     503                pfx = (lower1(self.type_name) + "_" if self.use_C_syntax else "")
     504                return self.indent(icount) + "void " + pfx + "do_segment(" + parameters + ") {" \
     505                + "\n" + self.indent(icount) + "  for (int i = 0; i < SEGMENT_BLOCKS; i++)" \
     506                + "\n" + self.indent(icount) + "    " + pfx + "do_block(" + do_block_call_args + ");" \
    491507                + "\n" + self.indent(icount + 2) + "}"
    492508
     
    509525               
    510526        def do_block_parameters(self, parameters):
    511                 return ", ".join([upper1(p) + " & " + lower1(p) for p in parameters])
     527                if self.use_C_syntax:
     528                        return ", ".join([self.type_name + "_data * " + lower1(self.type_name) + "_data"] + [upper1(p) + " * " + lower1(p) for p in parameters])
     529                else: return ", ".join([upper1(p) + " & " + lower1(p) for p in parameters])
    512530               
    513531        def do_final_block_parameters(self, parameters):
    514                 return ", ".join([upper1(p) + " & " + lower1(p) for p in parameters]+ ["BitBlock EOF_mask"])
     532                if self.use_C_syntax:
     533                        return ", ".join([self.type_name + "_data * " + lower1(self.type_name) + "_data"] + [upper1(p) + " * " + lower1(p) for p in parameters]+ ["BitBlock EOF_mask"])
     534                else: return ", ".join([upper1(p) + " & " + lower1(p) for p in parameters]+ ["BitBlock EOF_mask"])
    515535               
    516536        def do_segment_parameters(self, parameters):
    517                 return ", ".join([upper1(p) + " " + lower1(p) + "[]" for p in parameters])
     537                if self.use_C_syntax:
     538                        return ", ".join([self.type_name + "_data * " + lower1(self.type_name) + "_data"] + [upper1(p) + " " + lower1(p) + "[]" for p in parameters])
     539                else: return ", ".join([upper1(p) + " " + lower1(p) + "[]" for p in parameters])
    518540
    519541        def do_segment_args(self, parameters):
    520                 return ", ".join([lower1(p) + "[i]" for p in parameters])
     542                if self.use_C_syntax:
     543                        return ", ".join([lower1(self.type_name)] + [lower1(p) + "[i]" for p in parameters])
     544                else: return ", ".join([lower1(p) + "[i]" for p in parameters])
    521545
    522546def main(infilename, outfile = sys.stdout):
     
    531555
    532556class MainLoopTransformer:
    533   def __init__(self, main_module, add_dump_stmts=False, main_node_id='Main'):
     557  def __init__(self, main_module, C_syntax=False, add_dump_stmts=False, main_node_id='Main'):
    534558       
    535559    self.main_module = main_module
    536560    self.main_node_id = main_node_id
     561    self.use_C_syntax = C_syntax
    537562    self.add_dump_stmts = add_dump_stmts
    538563   
     
    557582                Bitwise_to_SIMD().xfrm(node)
    558583                final_block_node = copy.deepcopy(node)
    559                 CarryIntro().xfrm_fndef(node)
    560                 CarryIntro("carryQ", "_ci", "").xfrm_fndef(final_block_node)
     584                if self.use_C_syntax:
     585                        carryQname = stream_function.instance_name + "->carryQ"
     586                else: carryQname = "carryQ"
     587                CarryIntro(carryQname).xfrm_fndef(node)
     588                CarryIntro(carryQname, "_ci", "").xfrm_fndef(final_block_node)
    561589
    562590                if self.add_dump_stmts:
     
    564592               
    565593                if stream_function.carry_count > 0:
    566                         node.body += [mkCallStmt('CarryQ_Adjust', [ast.Name('carryQ', ast.Load()), ast.Num(stream_function.carry_count)])]
     594                        node.body += [mkCallStmt('CarryQ_Adjust', [ast.Name(carryQname, ast.Load()), ast.Num(stream_function.carry_count)])]
    567595               
    568596                stream_function.statements = Cgen.py2C(4).gen(node.body)
     
    570598                self.stream_functions[stream_function.type_name] = stream_function
    571599       
    572     self.emitter = Emitter()
     600    self.emitter = Emitter(self.use_C_syntax)
    573601   
    574602  def any_carry_expr(self):
     
    587615        if len(carry_test) > 0:
    588616                carry_test.pop()
    589         return "".join(carry_test)
    590    
     617                return "".join(carry_test)
    591618        return "1"
    592619
     
    594621    self.Cglobals = StreamStructGen().gen_struct_types(self.main_module)
    595622    for key in self.stream_functions.keys():
    596                 self.Cglobals += Emitter().definition(self.stream_functions[key],2)       
     623                self.Cglobals += Emitter(self.use_C_syntax).definition(self.stream_functions[key],2)       
    597624                       
    598625  def gen_declarations(self): 
     
    610637    for key in self.stream_functions.keys():
    611638                self.Cinits += self.emitter.declaration(self.stream_functions[key].type_name, self.stream_functions[key].instance_name, 2)
     639    if self.use_C_syntax:
     640                for key in self.stream_functions.keys():
     641                        self.Cinits += "CarryInit(" + self.stream_functions[key].type_name + "_data.carryQ, %i);\n" % (self.stream_functions[key].carry_count)
     642
    612643                       
    613644  def xfrm_block_stmts(self):
     
    621652        Add_SIMD_Register_Dump().xfrm(final_block_main)
    622653               
    623     StreamFunctionCallXlator().xfrm(self.main_node, self.stream_function_node.keys())
    624     StreamFunctionCallXlator('final').xfrm(final_block_main, self.stream_function_node.keys())
     654    StreamFunctionCallXlator().xfrm(self.main_node, self.stream_function_node.keys(), self.use_C_syntax)
     655    StreamFunctionCallXlator('final').xfrm(final_block_main, self.stream_function_node.keys(), self.use_C_syntax)
    625656   
    626657    if self.main_carry_count > 0:
Note: See TracChangeset for help on using the changeset viewer.